]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Update copyright notices for 2013.
[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 /* Number of windows showing the buffer of the selected window (or
520 another buffer with the same base buffer). keyboard.c refers to
521 this. */
522
523 int buffer_shared;
524
525 /* Vector containing glyphs for an ellipsis `...'. */
526
527 static Lisp_Object default_invis_vector[3];
528
529 /* This is the window where the echo area message was displayed. It
530 is always a mini-buffer window, but it may not be the same window
531 currently active as a mini-buffer. */
532
533 Lisp_Object echo_area_window;
534
535 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
536 pushes the current message and the value of
537 message_enable_multibyte on the stack, the function restore_message
538 pops the stack and displays MESSAGE again. */
539
540 static Lisp_Object Vmessage_stack;
541
542 /* Nonzero means multibyte characters were enabled when the echo area
543 message was specified. */
544
545 static int message_enable_multibyte;
546
547 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548
549 int update_mode_lines;
550
551 /* Nonzero if window sizes or contents have changed since last
552 redisplay that finished. */
553
554 int windows_or_buffers_changed;
555
556 /* Nonzero means a frame's cursor type has been changed. */
557
558 int cursor_type_changed;
559
560 /* Nonzero after display_mode_line if %l was used and it displayed a
561 line number. */
562
563 static int line_number_displayed;
564
565 /* The name of the *Messages* buffer, a string. */
566
567 static Lisp_Object Vmessages_buffer_name;
568
569 /* Current, index 0, and last displayed echo area message. Either
570 buffers from echo_buffers, or nil to indicate no message. */
571
572 Lisp_Object echo_area_buffer[2];
573
574 /* The buffers referenced from echo_area_buffer. */
575
576 static Lisp_Object echo_buffer[2];
577
578 /* A vector saved used in with_area_buffer to reduce consing. */
579
580 static Lisp_Object Vwith_echo_area_save_vector;
581
582 /* Non-zero means display_echo_area should display the last echo area
583 message again. Set by redisplay_preserve_echo_area. */
584
585 static int display_last_displayed_message_p;
586
587 /* Nonzero if echo area is being used by print; zero if being used by
588 message. */
589
590 static int message_buf_print;
591
592 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
593
594 static Lisp_Object Qinhibit_menubar_update;
595 static Lisp_Object Qmessage_truncate_lines;
596
597 /* Set to 1 in clear_message to make redisplay_internal aware
598 of an emptied echo area. */
599
600 static int message_cleared_p;
601
602 /* A scratch glyph row with contents used for generating truncation
603 glyphs. Also used in direct_output_for_insert. */
604
605 #define MAX_SCRATCH_GLYPHS 100
606 static struct glyph_row scratch_glyph_row;
607 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
608
609 /* Ascent and height of the last line processed by move_it_to. */
610
611 static int last_max_ascent, last_height;
612
613 /* Non-zero if there's a help-echo in the echo area. */
614
615 int help_echo_showing_p;
616
617 /* If >= 0, computed, exact values of mode-line and header-line height
618 to use in the macros CURRENT_MODE_LINE_HEIGHT and
619 CURRENT_HEADER_LINE_HEIGHT. */
620
621 int current_mode_line_height, current_header_line_height;
622
623 /* The maximum distance to look ahead for text properties. Values
624 that are too small let us call compute_char_face and similar
625 functions too often which is expensive. Values that are too large
626 let us call compute_char_face and alike too often because we
627 might not be interested in text properties that far away. */
628
629 #define TEXT_PROP_DISTANCE_LIMIT 100
630
631 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
632 iterator state and later restore it. This is needed because the
633 bidi iterator on bidi.c keeps a stacked cache of its states, which
634 is really a singleton. When we use scratch iterator objects to
635 move around the buffer, we can cause the bidi cache to be pushed or
636 popped, and therefore we need to restore the cache state when we
637 return to the original iterator. */
638 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
639 do { \
640 if (CACHE) \
641 bidi_unshelve_cache (CACHE, 1); \
642 ITCOPY = ITORIG; \
643 CACHE = bidi_shelve_cache (); \
644 } while (0)
645
646 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
647 do { \
648 if (pITORIG != pITCOPY) \
649 *(pITORIG) = *(pITCOPY); \
650 bidi_unshelve_cache (CACHE, 0); \
651 CACHE = NULL; \
652 } while (0)
653
654 #ifdef GLYPH_DEBUG
655
656 /* Non-zero means print traces of redisplay if compiled with
657 GLYPH_DEBUG defined. */
658
659 int trace_redisplay_p;
660
661 #endif /* GLYPH_DEBUG */
662
663 #ifdef DEBUG_TRACE_MOVE
664 /* Non-zero means trace with TRACE_MOVE to stderr. */
665 int trace_move;
666
667 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
668 #else
669 #define TRACE_MOVE(x) (void) 0
670 #endif
671
672 static Lisp_Object Qauto_hscroll_mode;
673
674 /* Buffer being redisplayed -- for redisplay_window_error. */
675
676 static struct buffer *displayed_buffer;
677
678 /* Value returned from text property handlers (see below). */
679
680 enum prop_handled
681 {
682 HANDLED_NORMALLY,
683 HANDLED_RECOMPUTE_PROPS,
684 HANDLED_OVERLAY_STRING_CONSUMED,
685 HANDLED_RETURN
686 };
687
688 /* A description of text properties that redisplay is interested
689 in. */
690
691 struct props
692 {
693 /* The name of the property. */
694 Lisp_Object *name;
695
696 /* A unique index for the property. */
697 enum prop_idx idx;
698
699 /* A handler function called to set up iterator IT from the property
700 at IT's current position. Value is used to steer handle_stop. */
701 enum prop_handled (*handler) (struct it *it);
702 };
703
704 static enum prop_handled handle_face_prop (struct it *);
705 static enum prop_handled handle_invisible_prop (struct it *);
706 static enum prop_handled handle_display_prop (struct it *);
707 static enum prop_handled handle_composition_prop (struct it *);
708 static enum prop_handled handle_overlay_change (struct it *);
709 static enum prop_handled handle_fontified_prop (struct it *);
710
711 /* Properties handled by iterators. */
712
713 static struct props it_props[] =
714 {
715 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
716 /* Handle `face' before `display' because some sub-properties of
717 `display' need to know the face. */
718 {&Qface, FACE_PROP_IDX, handle_face_prop},
719 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
720 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
721 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
722 {NULL, 0, NULL}
723 };
724
725 /* Value is the position described by X. If X is a marker, value is
726 the marker_position of X. Otherwise, value is X. */
727
728 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
729
730 /* Enumeration returned by some move_it_.* functions internally. */
731
732 enum move_it_result
733 {
734 /* Not used. Undefined value. */
735 MOVE_UNDEFINED,
736
737 /* Move ended at the requested buffer position or ZV. */
738 MOVE_POS_MATCH_OR_ZV,
739
740 /* Move ended at the requested X pixel position. */
741 MOVE_X_REACHED,
742
743 /* Move within a line ended at the end of a line that must be
744 continued. */
745 MOVE_LINE_CONTINUED,
746
747 /* Move within a line ended at the end of a line that would
748 be displayed truncated. */
749 MOVE_LINE_TRUNCATED,
750
751 /* Move within a line ended at a line end. */
752 MOVE_NEWLINE_OR_CR
753 };
754
755 /* This counter is used to clear the face cache every once in a while
756 in redisplay_internal. It is incremented for each redisplay.
757 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
758 cleared. */
759
760 #define CLEAR_FACE_CACHE_COUNT 500
761 static int clear_face_cache_count;
762
763 /* Similarly for the image cache. */
764
765 #ifdef HAVE_WINDOW_SYSTEM
766 #define CLEAR_IMAGE_CACHE_COUNT 101
767 static int clear_image_cache_count;
768
769 /* Null glyph slice */
770 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
771 #endif
772
773 /* True while redisplay_internal is in progress. */
774
775 bool redisplaying_p;
776
777 static Lisp_Object Qinhibit_free_realized_faces;
778 static Lisp_Object Qmode_line_default_help_echo;
779
780 /* If a string, XTread_socket generates an event to display that string.
781 (The display is done in read_char.) */
782
783 Lisp_Object help_echo_string;
784 Lisp_Object help_echo_window;
785 Lisp_Object help_echo_object;
786 ptrdiff_t help_echo_pos;
787
788 /* Temporary variable for XTread_socket. */
789
790 Lisp_Object previous_help_echo_string;
791
792 /* Platform-independent portion of hourglass implementation. */
793
794 /* Non-zero means an hourglass cursor is currently shown. */
795 int hourglass_shown_p;
796
797 /* If non-null, an asynchronous timer that, when it expires, displays
798 an hourglass cursor on all frames. */
799 struct atimer *hourglass_atimer;
800
801 /* Name of the face used to display glyphless characters. */
802 Lisp_Object Qglyphless_char;
803
804 /* Symbol for the purpose of Vglyphless_char_display. */
805 static Lisp_Object Qglyphless_char_display;
806
807 /* Method symbols for Vglyphless_char_display. */
808 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
809
810 /* Default pixel width of `thin-space' display method. */
811 #define THIN_SPACE_WIDTH 1
812
813 /* Default number of seconds to wait before displaying an hourglass
814 cursor. */
815 #define DEFAULT_HOURGLASS_DELAY 1
816
817 \f
818 /* Function prototypes. */
819
820 static void setup_for_ellipsis (struct it *, int);
821 static void set_iterator_to_next (struct it *, int);
822 static void mark_window_display_accurate_1 (struct window *, int);
823 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
824 static int display_prop_string_p (Lisp_Object, Lisp_Object);
825 static int cursor_row_p (struct glyph_row *);
826 static int redisplay_mode_lines (Lisp_Object, int);
827 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
828
829 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
830
831 static void handle_line_prefix (struct it *);
832
833 static void pint2str (char *, int, ptrdiff_t);
834 static void pint2hrstr (char *, int, ptrdiff_t);
835 static struct text_pos run_window_scroll_functions (Lisp_Object,
836 struct text_pos);
837 static void reconsider_clip_changes (struct window *, struct buffer *);
838 static int text_outside_line_unchanged_p (struct window *,
839 ptrdiff_t, ptrdiff_t);
840 static void store_mode_line_noprop_char (char);
841 static int store_mode_line_noprop (const char *, int, int);
842 static void handle_stop (struct it *);
843 static void handle_stop_backwards (struct it *, ptrdiff_t);
844 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
845 static void ensure_echo_area_buffers (void);
846 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
847 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
848 static int with_echo_area_buffer (struct window *, int,
849 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
850 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
851 static void clear_garbaged_frames (void);
852 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static void pop_message (void);
854 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
855 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
856 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
857 static int display_echo_area (struct window *);
858 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
859 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
860 static Lisp_Object unwind_redisplay (Lisp_Object);
861 static int string_char_and_length (const unsigned char *, int *);
862 static struct text_pos display_prop_end (struct it *, Lisp_Object,
863 struct text_pos);
864 static int compute_window_start_on_continuation_line (struct window *);
865 static void insert_left_trunc_glyphs (struct it *);
866 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
867 Lisp_Object);
868 static void extend_face_to_end_of_line (struct it *);
869 static int append_space_for_newline (struct it *, int);
870 static int cursor_row_fully_visible_p (struct window *, int, int);
871 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
872 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
873 static int trailing_whitespace_p (ptrdiff_t);
874 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
875 static void push_it (struct it *, struct text_pos *);
876 static void iterate_out_of_display_property (struct it *);
877 static void pop_it (struct it *);
878 static void sync_frame_with_window_matrix_rows (struct window *);
879 static void select_frame_for_redisplay (Lisp_Object);
880 static void redisplay_internal (void);
881 static int echo_area_display (int);
882 static void redisplay_windows (Lisp_Object);
883 static void redisplay_window (Lisp_Object, int);
884 static Lisp_Object redisplay_window_error (Lisp_Object);
885 static Lisp_Object redisplay_window_0 (Lisp_Object);
886 static Lisp_Object redisplay_window_1 (Lisp_Object);
887 static int set_cursor_from_row (struct window *, struct glyph_row *,
888 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
889 int, int);
890 static int update_menu_bar (struct frame *, int, int);
891 static int try_window_reusing_current_matrix (struct window *);
892 static int try_window_id (struct window *);
893 static int display_line (struct it *);
894 static int display_mode_lines (struct window *);
895 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
896 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
897 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
898 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
899 static void display_menu_bar (struct window *);
900 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
901 ptrdiff_t *);
902 static int display_string (const char *, Lisp_Object, Lisp_Object,
903 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
904 static void compute_line_metrics (struct it *);
905 static void run_redisplay_end_trigger_hook (struct it *);
906 static int get_overlay_strings (struct it *, ptrdiff_t);
907 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
908 static void next_overlay_string (struct it *);
909 static void reseat (struct it *, struct text_pos, int);
910 static void reseat_1 (struct it *, struct text_pos, int);
911 static void back_to_previous_visible_line_start (struct it *);
912 void reseat_at_previous_visible_line_start (struct it *);
913 static void reseat_at_next_visible_line_start (struct it *, int);
914 static int next_element_from_ellipsis (struct it *);
915 static int next_element_from_display_vector (struct it *);
916 static int next_element_from_string (struct it *);
917 static int next_element_from_c_string (struct it *);
918 static int next_element_from_buffer (struct it *);
919 static int next_element_from_composition (struct it *);
920 static int next_element_from_image (struct it *);
921 static int next_element_from_stretch (struct it *);
922 static void load_overlay_strings (struct it *, ptrdiff_t);
923 static int init_from_display_pos (struct it *, struct window *,
924 struct display_pos *);
925 static void reseat_to_string (struct it *, const char *,
926 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
927 static int get_next_display_element (struct it *);
928 static enum move_it_result
929 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
930 enum move_operation_enum);
931 void move_it_vertically_backward (struct it *, int);
932 static void get_visually_first_element (struct it *);
933 static void init_to_row_start (struct it *, struct window *,
934 struct glyph_row *);
935 static int init_to_row_end (struct it *, struct window *,
936 struct glyph_row *);
937 static void back_to_previous_line_start (struct it *);
938 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
939 static struct text_pos string_pos_nchars_ahead (struct text_pos,
940 Lisp_Object, ptrdiff_t);
941 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
942 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
943 static ptrdiff_t number_of_chars (const char *, int);
944 static void compute_stop_pos (struct it *);
945 static void compute_string_pos (struct text_pos *, struct text_pos,
946 Lisp_Object);
947 static int face_before_or_after_it_pos (struct it *, int);
948 static ptrdiff_t next_overlay_change (ptrdiff_t);
949 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
950 Lisp_Object, struct text_pos *, ptrdiff_t, int);
951 static int handle_single_display_spec (struct it *, Lisp_Object,
952 Lisp_Object, Lisp_Object,
953 struct text_pos *, ptrdiff_t, int, int);
954 static int underlying_face_id (struct it *);
955 static int in_ellipses_for_invisible_text_p (struct display_pos *,
956 struct window *);
957
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960
961 #ifdef HAVE_WINDOW_SYSTEM
962
963 static void x_consider_frame_title (Lisp_Object);
964 static int tool_bar_lines_needed (struct frame *, int *);
965 static void update_tool_bar (struct frame *, int);
966 static void build_desired_tool_bar_string (struct frame *f);
967 static int redisplay_tool_bar (struct frame *);
968 static void display_tool_bar_line (struct it *, int);
969 static void notice_overwritten_cursor (struct window *,
970 enum glyph_row_area,
971 int, int, int, int);
972 static void append_stretch_glyph (struct it *, Lisp_Object,
973 int, int, int);
974
975
976 #endif /* HAVE_WINDOW_SYSTEM */
977
978 static void produce_special_glyphs (struct it *, enum display_element_type);
979 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
980 static int coords_in_mouse_face_p (struct window *, int, int);
981
982
983 \f
984 /***********************************************************************
985 Window display dimensions
986 ***********************************************************************/
987
988 /* Return the bottom boundary y-position for text lines in window W.
989 This is the first y position at which a line cannot start.
990 It is relative to the top of the window.
991
992 This is the height of W minus the height of a mode line, if any. */
993
994 int
995 window_text_bottom_y (struct window *w)
996 {
997 int height = WINDOW_TOTAL_HEIGHT (w);
998
999 if (WINDOW_WANTS_MODELINE_P (w))
1000 height -= CURRENT_MODE_LINE_HEIGHT (w);
1001 return height;
1002 }
1003
1004 /* Return the pixel width of display area AREA of window W. AREA < 0
1005 means return the total width of W, not including fringes to
1006 the left and right of the window. */
1007
1008 int
1009 window_box_width (struct window *w, int area)
1010 {
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1013
1014 if (!w->pseudo_window_p)
1015 {
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1017
1018 if (area == TEXT_AREA)
1019 {
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1025 }
1026 else if (area == LEFT_MARGIN_AREA)
1027 {
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1031 }
1032 else if (area == RIGHT_MARGIN_AREA)
1033 {
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1037 }
1038 }
1039
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 int
1048 window_box_height (struct window *w)
1049 {
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_TOTAL_HEIGHT (w);
1052
1053 eassert (height >= 0);
1054
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1060
1061 if (WINDOW_WANTS_MODELINE_P (w))
1062 {
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1071 }
1072
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 {
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1083 }
1084
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1088 }
1089
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1093
1094 int
1095 window_box_left_offset (struct window *w, int area)
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right_offset (struct window *w, int area)
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 int
1137 window_box_left (struct window *w, int area)
1138 {
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1141
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. AREA < 0 means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 int
1157 window_box_right (struct window *w, int area)
1158 {
1159 return window_box_left (w, area) + window_box_width (w, area);
1160 }
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. AREA < 0 means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168
1169 void
1170 window_box (struct window *w, int area, int *box_x, int *box_y,
1171 int *box_width, int *box_height)
1172 {
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1180 {
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 }
1185 }
1186
1187
1188 /* Get the bounding box of the display area AREA of window W, without
1189 mode lines. AREA < 0 means the whole window, not including the
1190 left and right fringe of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1195
1196 static void
1197 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1199 {
1200 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1201 bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1204 }
1205
1206
1207 \f
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1211
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1214
1215 int
1216 line_bottom_y (struct it *it)
1217 {
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1220
1221 if (line_height == 0)
1222 {
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1226 {
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1231 }
1232 else
1233 {
1234 struct glyph_row *row = it->glyph_row;
1235
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1244 }
1245 }
1246
1247 return line_top_y + line_height;
1248 }
1249
1250 /* Subroutine of pos_visible_p below. Extracts a display string, if
1251 any, from the display spec given as its argument. */
1252 static Lisp_Object
1253 string_from_display_spec (Lisp_Object spec)
1254 {
1255 if (CONSP (spec))
1256 {
1257 while (CONSP (spec))
1258 {
1259 if (STRINGP (XCAR (spec)))
1260 return XCAR (spec);
1261 spec = XCDR (spec);
1262 }
1263 }
1264 else if (VECTORP (spec))
1265 {
1266 ptrdiff_t i;
1267
1268 for (i = 0; i < ASIZE (spec); i++)
1269 {
1270 if (STRINGP (AREF (spec, i)))
1271 return AREF (spec, i);
1272 }
1273 return Qnil;
1274 }
1275
1276 return spec;
1277 }
1278
1279
1280 /* Limit insanely large values of W->hscroll on frame F to the largest
1281 value that will still prevent first_visible_x and last_visible_x of
1282 'struct it' from overflowing an int. */
1283 static int
1284 window_hscroll_limited (struct window *w, struct frame *f)
1285 {
1286 ptrdiff_t window_hscroll = w->hscroll;
1287 int window_text_width = window_box_width (w, TEXT_AREA);
1288 int colwidth = FRAME_COLUMN_WIDTH (f);
1289
1290 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1291 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1292
1293 return window_hscroll;
1294 }
1295
1296 /* Return 1 if position CHARPOS is visible in window W.
1297 CHARPOS < 0 means return info about WINDOW_END position.
1298 If visible, set *X and *Y to pixel coordinates of top left corner.
1299 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1300 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1301
1302 int
1303 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1304 int *rtop, int *rbot, int *rowh, int *vpos)
1305 {
1306 struct it it;
1307 void *itdata = bidi_shelve_cache ();
1308 struct text_pos top;
1309 int visible_p = 0;
1310 struct buffer *old_buffer = NULL;
1311
1312 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1313 return visible_p;
1314
1315 if (XBUFFER (w->buffer) != current_buffer)
1316 {
1317 old_buffer = current_buffer;
1318 set_buffer_internal_1 (XBUFFER (w->buffer));
1319 }
1320
1321 SET_TEXT_POS_FROM_MARKER (top, w->start);
1322 /* Scrolling a minibuffer window via scroll bar when the echo area
1323 shows long text sometimes resets the minibuffer contents behind
1324 our backs. */
1325 if (CHARPOS (top) > ZV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1327
1328 /* Compute exact mode line heights. */
1329 if (WINDOW_WANTS_MODELINE_P (w))
1330 current_mode_line_height
1331 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1332 BVAR (current_buffer, mode_line_format));
1333
1334 if (WINDOW_WANTS_HEADER_LINE_P (w))
1335 current_header_line_height
1336 = display_mode_line (w, HEADER_LINE_FACE_ID,
1337 BVAR (current_buffer, header_line_format));
1338
1339 start_display (&it, w, top);
1340 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1341 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1342
1343 if (charpos >= 0
1344 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1345 && IT_CHARPOS (it) >= charpos)
1346 /* When scanning backwards under bidi iteration, move_it_to
1347 stops at or _before_ CHARPOS, because it stops at or to
1348 the _right_ of the character at CHARPOS. */
1349 || (it.bidi_p && it.bidi_it.scan_dir == -1
1350 && IT_CHARPOS (it) <= charpos)))
1351 {
1352 /* We have reached CHARPOS, or passed it. How the call to
1353 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1354 or covered by a display property, move_it_to stops at the end
1355 of the invisible text, to the right of CHARPOS. (ii) If
1356 CHARPOS is in a display vector, move_it_to stops on its last
1357 glyph. */
1358 int top_x = it.current_x;
1359 int top_y = it.current_y;
1360 /* Calling line_bottom_y may change it.method, it.position, etc. */
1361 enum it_method it_method = it.method;
1362 int bottom_y = (last_height = 0, line_bottom_y (&it));
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364
1365 if (top_y < window_top_y)
1366 visible_p = bottom_y > window_top_y;
1367 else if (top_y < it.last_visible_y)
1368 visible_p = 1;
1369 if (bottom_y >= it.last_visible_y
1370 && it.bidi_p && it.bidi_it.scan_dir == -1
1371 && IT_CHARPOS (it) < charpos)
1372 {
1373 /* When the last line of the window is scanned backwards
1374 under bidi iteration, we could be duped into thinking
1375 that we have passed CHARPOS, when in fact move_it_to
1376 simply stopped short of CHARPOS because it reached
1377 last_visible_y. To see if that's what happened, we call
1378 move_it_to again with a slightly larger vertical limit,
1379 and see if it actually moved vertically; if it did, we
1380 didn't really reach CHARPOS, which is beyond window end. */
1381 struct it save_it = it;
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines =
1385 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1386
1387 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1388 MOVE_TO_POS | MOVE_TO_Y);
1389 if (it.current_y > top_y)
1390 visible_p = 0;
1391
1392 it = save_it;
1393 }
1394 if (visible_p)
1395 {
1396 if (it_method == GET_FROM_DISPLAY_VECTOR)
1397 {
1398 /* We stopped on the last glyph of a display vector.
1399 Try and recompute. Hack alert! */
1400 if (charpos < 2 || top.charpos >= charpos)
1401 top_x = it.glyph_row->x;
1402 else
1403 {
1404 struct it it2;
1405 start_display (&it2, w, top);
1406 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1407 get_next_display_element (&it2);
1408 PRODUCE_GLYPHS (&it2);
1409 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1410 || it2.current_x > it2.last_visible_x)
1411 top_x = it.glyph_row->x;
1412 else
1413 {
1414 top_x = it2.current_x;
1415 top_y = it2.current_y;
1416 }
1417 }
1418 }
1419 else if (IT_CHARPOS (it) != charpos)
1420 {
1421 Lisp_Object cpos = make_number (charpos);
1422 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1423 Lisp_Object string = string_from_display_spec (spec);
1424 int newline_in_string = 0;
1425
1426 if (STRINGP (string))
1427 {
1428 const char *s = SSDATA (string);
1429 const char *e = s + SBYTES (string);
1430 while (s < e)
1431 {
1432 if (*s++ == '\n')
1433 {
1434 newline_in_string = 1;
1435 break;
1436 }
1437 }
1438 }
1439 /* The tricky code below is needed because there's a
1440 discrepancy between move_it_to and how we set cursor
1441 when the display line ends in a newline from a
1442 display string. move_it_to will stop _after_ such
1443 display strings, whereas set_cursor_from_row
1444 conspires with cursor_row_p to place the cursor on
1445 the first glyph produced from the display string. */
1446
1447 /* We have overshoot PT because it is covered by a
1448 display property whose value is a string. If the
1449 string includes embedded newlines, we are also in the
1450 wrong display line. Backtrack to the correct line,
1451 where the display string begins. */
1452 if (newline_in_string)
1453 {
1454 Lisp_Object startpos, endpos;
1455 EMACS_INT start, end;
1456 struct it it3;
1457 int it3_moved;
1458
1459 /* Find the first and the last buffer positions
1460 covered by the display string. */
1461 endpos =
1462 Fnext_single_char_property_change (cpos, Qdisplay,
1463 Qnil, Qnil);
1464 startpos =
1465 Fprevious_single_char_property_change (endpos, Qdisplay,
1466 Qnil, Qnil);
1467 start = XFASTINT (startpos);
1468 end = XFASTINT (endpos);
1469 /* Move to the last buffer position before the
1470 display property. */
1471 start_display (&it3, w, top);
1472 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1473 /* Move forward one more line if the position before
1474 the display string is a newline or if it is the
1475 rightmost character on a line that is
1476 continued or word-wrapped. */
1477 if (it3.method == GET_FROM_BUFFER
1478 && it3.c == '\n')
1479 move_it_by_lines (&it3, 1);
1480 else if (move_it_in_display_line_to (&it3, -1,
1481 it3.current_x
1482 + it3.pixel_width,
1483 MOVE_TO_X)
1484 == MOVE_LINE_CONTINUED)
1485 {
1486 move_it_by_lines (&it3, 1);
1487 /* When we are under word-wrap, the #$@%!
1488 move_it_by_lines moves 2 lines, so we need to
1489 fix that up. */
1490 if (it3.line_wrap == WORD_WRAP)
1491 move_it_by_lines (&it3, -1);
1492 }
1493
1494 /* Record the vertical coordinate of the display
1495 line where we wound up. */
1496 top_y = it3.current_y;
1497 if (it3.bidi_p)
1498 {
1499 /* When characters are reordered for display,
1500 the character displayed to the left of the
1501 display string could be _after_ the display
1502 property in the logical order. Use the
1503 smallest vertical position of these two. */
1504 start_display (&it3, w, top);
1505 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1506 if (it3.current_y < top_y)
1507 top_y = it3.current_y;
1508 }
1509 /* Move from the top of the window to the beginning
1510 of the display line where the display string
1511 begins. */
1512 start_display (&it3, w, top);
1513 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1514 /* If it3_moved stays zero after the 'while' loop
1515 below, that means we already were at a newline
1516 before the loop (e.g., the display string begins
1517 with a newline), so we don't need to (and cannot)
1518 inspect the glyphs of it3.glyph_row, because
1519 PRODUCE_GLYPHS will not produce anything for a
1520 newline, and thus it3.glyph_row stays at its
1521 stale content it got at top of the window. */
1522 it3_moved = 0;
1523 /* Finally, advance the iterator until we hit the
1524 first display element whose character position is
1525 CHARPOS, or until the first newline from the
1526 display string, which signals the end of the
1527 display line. */
1528 while (get_next_display_element (&it3))
1529 {
1530 PRODUCE_GLYPHS (&it3);
1531 if (IT_CHARPOS (it3) == charpos
1532 || ITERATOR_AT_END_OF_LINE_P (&it3))
1533 break;
1534 it3_moved = 1;
1535 set_iterator_to_next (&it3, 0);
1536 }
1537 top_x = it3.current_x - it3.pixel_width;
1538 /* Normally, we would exit the above loop because we
1539 found the display element whose character
1540 position is CHARPOS. For the contingency that we
1541 didn't, and stopped at the first newline from the
1542 display string, move back over the glyphs
1543 produced from the string, until we find the
1544 rightmost glyph not from the string. */
1545 if (it3_moved
1546 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1547 {
1548 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1549 + it3.glyph_row->used[TEXT_AREA];
1550
1551 while (EQ ((g - 1)->object, string))
1552 {
1553 --g;
1554 top_x -= g->pixel_width;
1555 }
1556 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1557 + it3.glyph_row->used[TEXT_AREA]);
1558 }
1559 }
1560 }
1561
1562 *x = top_x;
1563 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1564 *rtop = max (0, window_top_y - top_y);
1565 *rbot = max (0, bottom_y - it.last_visible_y);
1566 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1567 - max (top_y, window_top_y)));
1568 *vpos = it.vpos;
1569 }
1570 }
1571 else
1572 {
1573 /* We were asked to provide info about WINDOW_END. */
1574 struct it it2;
1575 void *it2data = NULL;
1576
1577 SAVE_IT (it2, it, it2data);
1578 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1579 move_it_by_lines (&it, 1);
1580 if (charpos < IT_CHARPOS (it)
1581 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1582 {
1583 visible_p = 1;
1584 RESTORE_IT (&it2, &it2, it2data);
1585 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1586 *x = it2.current_x;
1587 *y = it2.current_y + it2.max_ascent - it2.ascent;
1588 *rtop = max (0, -it2.current_y);
1589 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1590 - it.last_visible_y));
1591 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1592 it.last_visible_y)
1593 - max (it2.current_y,
1594 WINDOW_HEADER_LINE_HEIGHT (w))));
1595 *vpos = it2.vpos;
1596 }
1597 else
1598 bidi_unshelve_cache (it2data, 1);
1599 }
1600 bidi_unshelve_cache (itdata, 0);
1601
1602 if (old_buffer)
1603 set_buffer_internal_1 (old_buffer);
1604
1605 current_header_line_height = current_mode_line_height = -1;
1606
1607 if (visible_p && w->hscroll > 0)
1608 *x -=
1609 window_hscroll_limited (w, WINDOW_XFRAME (w))
1610 * WINDOW_FRAME_COLUMN_WIDTH (w);
1611
1612 #if 0
1613 /* Debugging code. */
1614 if (visible_p)
1615 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1616 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1617 else
1618 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1619 #endif
1620
1621 return visible_p;
1622 }
1623
1624
1625 /* Return the next character from STR. Return in *LEN the length of
1626 the character. This is like STRING_CHAR_AND_LENGTH but never
1627 returns an invalid character. If we find one, we return a `?', but
1628 with the length of the invalid character. */
1629
1630 static int
1631 string_char_and_length (const unsigned char *str, int *len)
1632 {
1633 int c;
1634
1635 c = STRING_CHAR_AND_LENGTH (str, *len);
1636 if (!CHAR_VALID_P (c))
1637 /* We may not change the length here because other places in Emacs
1638 don't use this function, i.e. they silently accept invalid
1639 characters. */
1640 c = '?';
1641
1642 return c;
1643 }
1644
1645
1646
1647 /* Given a position POS containing a valid character and byte position
1648 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1649
1650 static struct text_pos
1651 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1652 {
1653 eassert (STRINGP (string) && nchars >= 0);
1654
1655 if (STRING_MULTIBYTE (string))
1656 {
1657 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1658 int len;
1659
1660 while (nchars--)
1661 {
1662 string_char_and_length (p, &len);
1663 p += len;
1664 CHARPOS (pos) += 1;
1665 BYTEPOS (pos) += len;
1666 }
1667 }
1668 else
1669 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1670
1671 return pos;
1672 }
1673
1674
1675 /* Value is the text position, i.e. character and byte position,
1676 for character position CHARPOS in STRING. */
1677
1678 static struct text_pos
1679 string_pos (ptrdiff_t charpos, Lisp_Object string)
1680 {
1681 struct text_pos pos;
1682 eassert (STRINGP (string));
1683 eassert (charpos >= 0);
1684 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1685 return pos;
1686 }
1687
1688
1689 /* Value is a text position, i.e. character and byte position, for
1690 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1691 means recognize multibyte characters. */
1692
1693 static struct text_pos
1694 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1695 {
1696 struct text_pos pos;
1697
1698 eassert (s != NULL);
1699 eassert (charpos >= 0);
1700
1701 if (multibyte_p)
1702 {
1703 int len;
1704
1705 SET_TEXT_POS (pos, 0, 0);
1706 while (charpos--)
1707 {
1708 string_char_and_length ((const unsigned char *) s, &len);
1709 s += len;
1710 CHARPOS (pos) += 1;
1711 BYTEPOS (pos) += len;
1712 }
1713 }
1714 else
1715 SET_TEXT_POS (pos, charpos, charpos);
1716
1717 return pos;
1718 }
1719
1720
1721 /* Value is the number of characters in C string S. MULTIBYTE_P
1722 non-zero means recognize multibyte characters. */
1723
1724 static ptrdiff_t
1725 number_of_chars (const char *s, int multibyte_p)
1726 {
1727 ptrdiff_t nchars;
1728
1729 if (multibyte_p)
1730 {
1731 ptrdiff_t rest = strlen (s);
1732 int len;
1733 const unsigned char *p = (const unsigned char *) s;
1734
1735 for (nchars = 0; rest > 0; ++nchars)
1736 {
1737 string_char_and_length (p, &len);
1738 rest -= len, p += len;
1739 }
1740 }
1741 else
1742 nchars = strlen (s);
1743
1744 return nchars;
1745 }
1746
1747
1748 /* Compute byte position NEWPOS->bytepos corresponding to
1749 NEWPOS->charpos. POS is a known position in string STRING.
1750 NEWPOS->charpos must be >= POS.charpos. */
1751
1752 static void
1753 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1754 {
1755 eassert (STRINGP (string));
1756 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1757
1758 if (STRING_MULTIBYTE (string))
1759 *newpos = string_pos_nchars_ahead (pos, string,
1760 CHARPOS (*newpos) - CHARPOS (pos));
1761 else
1762 BYTEPOS (*newpos) = CHARPOS (*newpos);
1763 }
1764
1765 /* EXPORT:
1766 Return an estimation of the pixel height of mode or header lines on
1767 frame F. FACE_ID specifies what line's height to estimate. */
1768
1769 int
1770 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1771 {
1772 #ifdef HAVE_WINDOW_SYSTEM
1773 if (FRAME_WINDOW_P (f))
1774 {
1775 int height = FONT_HEIGHT (FRAME_FONT (f));
1776
1777 /* This function is called so early when Emacs starts that the face
1778 cache and mode line face are not yet initialized. */
1779 if (FRAME_FACE_CACHE (f))
1780 {
1781 struct face *face = FACE_FROM_ID (f, face_id);
1782 if (face)
1783 {
1784 if (face->font)
1785 height = FONT_HEIGHT (face->font);
1786 if (face->box_line_width > 0)
1787 height += 2 * face->box_line_width;
1788 }
1789 }
1790
1791 return height;
1792 }
1793 #endif
1794
1795 return 1;
1796 }
1797
1798 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1799 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1800 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1801 not force the value into range. */
1802
1803 void
1804 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1805 int *x, int *y, NativeRectangle *bounds, int noclip)
1806 {
1807
1808 #ifdef HAVE_WINDOW_SYSTEM
1809 if (FRAME_WINDOW_P (f))
1810 {
1811 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1812 even for negative values. */
1813 if (pix_x < 0)
1814 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1815 if (pix_y < 0)
1816 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1817
1818 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1819 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1820
1821 if (bounds)
1822 STORE_NATIVE_RECT (*bounds,
1823 FRAME_COL_TO_PIXEL_X (f, pix_x),
1824 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1825 FRAME_COLUMN_WIDTH (f) - 1,
1826 FRAME_LINE_HEIGHT (f) - 1);
1827
1828 if (!noclip)
1829 {
1830 if (pix_x < 0)
1831 pix_x = 0;
1832 else if (pix_x > FRAME_TOTAL_COLS (f))
1833 pix_x = FRAME_TOTAL_COLS (f);
1834
1835 if (pix_y < 0)
1836 pix_y = 0;
1837 else if (pix_y > FRAME_LINES (f))
1838 pix_y = FRAME_LINES (f);
1839 }
1840 }
1841 #endif
1842
1843 *x = pix_x;
1844 *y = pix_y;
1845 }
1846
1847
1848 /* Find the glyph under window-relative coordinates X/Y in window W.
1849 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1850 strings. Return in *HPOS and *VPOS the row and column number of
1851 the glyph found. Return in *AREA the glyph area containing X.
1852 Value is a pointer to the glyph found or null if X/Y is not on
1853 text, or we can't tell because W's current matrix is not up to
1854 date. */
1855
1856 static
1857 struct glyph *
1858 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1859 int *dx, int *dy, int *area)
1860 {
1861 struct glyph *glyph, *end;
1862 struct glyph_row *row = NULL;
1863 int x0, i;
1864
1865 /* Find row containing Y. Give up if some row is not enabled. */
1866 for (i = 0; i < w->current_matrix->nrows; ++i)
1867 {
1868 row = MATRIX_ROW (w->current_matrix, i);
1869 if (!row->enabled_p)
1870 return NULL;
1871 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1872 break;
1873 }
1874
1875 *vpos = i;
1876 *hpos = 0;
1877
1878 /* Give up if Y is not in the window. */
1879 if (i == w->current_matrix->nrows)
1880 return NULL;
1881
1882 /* Get the glyph area containing X. */
1883 if (w->pseudo_window_p)
1884 {
1885 *area = TEXT_AREA;
1886 x0 = 0;
1887 }
1888 else
1889 {
1890 if (x < window_box_left_offset (w, TEXT_AREA))
1891 {
1892 *area = LEFT_MARGIN_AREA;
1893 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1894 }
1895 else if (x < window_box_right_offset (w, TEXT_AREA))
1896 {
1897 *area = TEXT_AREA;
1898 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1899 }
1900 else
1901 {
1902 *area = RIGHT_MARGIN_AREA;
1903 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1904 }
1905 }
1906
1907 /* Find glyph containing X. */
1908 glyph = row->glyphs[*area];
1909 end = glyph + row->used[*area];
1910 x -= x0;
1911 while (glyph < end && x >= glyph->pixel_width)
1912 {
1913 x -= glyph->pixel_width;
1914 ++glyph;
1915 }
1916
1917 if (glyph == end)
1918 return NULL;
1919
1920 if (dx)
1921 {
1922 *dx = x;
1923 *dy = y - (row->y + row->ascent - glyph->ascent);
1924 }
1925
1926 *hpos = glyph - row->glyphs[*area];
1927 return glyph;
1928 }
1929
1930 /* Convert frame-relative x/y to coordinates relative to window W.
1931 Takes pseudo-windows into account. */
1932
1933 static void
1934 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1935 {
1936 if (w->pseudo_window_p)
1937 {
1938 /* A pseudo-window is always full-width, and starts at the
1939 left edge of the frame, plus a frame border. */
1940 struct frame *f = XFRAME (w->frame);
1941 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1942 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1943 }
1944 else
1945 {
1946 *x -= WINDOW_LEFT_EDGE_X (w);
1947 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1948 }
1949 }
1950
1951 #ifdef HAVE_WINDOW_SYSTEM
1952
1953 /* EXPORT:
1954 Return in RECTS[] at most N clipping rectangles for glyph string S.
1955 Return the number of stored rectangles. */
1956
1957 int
1958 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1959 {
1960 XRectangle r;
1961
1962 if (n <= 0)
1963 return 0;
1964
1965 if (s->row->full_width_p)
1966 {
1967 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1968 r.x = WINDOW_LEFT_EDGE_X (s->w);
1969 r.width = WINDOW_TOTAL_WIDTH (s->w);
1970
1971 /* Unless displaying a mode or menu bar line, which are always
1972 fully visible, clip to the visible part of the row. */
1973 if (s->w->pseudo_window_p)
1974 r.height = s->row->visible_height;
1975 else
1976 r.height = s->height;
1977 }
1978 else
1979 {
1980 /* This is a text line that may be partially visible. */
1981 r.x = window_box_left (s->w, s->area);
1982 r.width = window_box_width (s->w, s->area);
1983 r.height = s->row->visible_height;
1984 }
1985
1986 if (s->clip_head)
1987 if (r.x < s->clip_head->x)
1988 {
1989 if (r.width >= s->clip_head->x - r.x)
1990 r.width -= s->clip_head->x - r.x;
1991 else
1992 r.width = 0;
1993 r.x = s->clip_head->x;
1994 }
1995 if (s->clip_tail)
1996 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1997 {
1998 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1999 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2000 else
2001 r.width = 0;
2002 }
2003
2004 /* If S draws overlapping rows, it's sufficient to use the top and
2005 bottom of the window for clipping because this glyph string
2006 intentionally draws over other lines. */
2007 if (s->for_overlaps)
2008 {
2009 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2010 r.height = window_text_bottom_y (s->w) - r.y;
2011
2012 /* Alas, the above simple strategy does not work for the
2013 environments with anti-aliased text: if the same text is
2014 drawn onto the same place multiple times, it gets thicker.
2015 If the overlap we are processing is for the erased cursor, we
2016 take the intersection with the rectangle of the cursor. */
2017 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2018 {
2019 XRectangle rc, r_save = r;
2020
2021 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2022 rc.y = s->w->phys_cursor.y;
2023 rc.width = s->w->phys_cursor_width;
2024 rc.height = s->w->phys_cursor_height;
2025
2026 x_intersect_rectangles (&r_save, &rc, &r);
2027 }
2028 }
2029 else
2030 {
2031 /* Don't use S->y for clipping because it doesn't take partially
2032 visible lines into account. For example, it can be negative for
2033 partially visible lines at the top of a window. */
2034 if (!s->row->full_width_p
2035 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2036 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2037 else
2038 r.y = max (0, s->row->y);
2039 }
2040
2041 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2042
2043 /* If drawing the cursor, don't let glyph draw outside its
2044 advertised boundaries. Cleartype does this under some circumstances. */
2045 if (s->hl == DRAW_CURSOR)
2046 {
2047 struct glyph *glyph = s->first_glyph;
2048 int height, max_y;
2049
2050 if (s->x > r.x)
2051 {
2052 r.width -= s->x - r.x;
2053 r.x = s->x;
2054 }
2055 r.width = min (r.width, glyph->pixel_width);
2056
2057 /* If r.y is below window bottom, ensure that we still see a cursor. */
2058 height = min (glyph->ascent + glyph->descent,
2059 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2060 max_y = window_text_bottom_y (s->w) - height;
2061 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2062 if (s->ybase - glyph->ascent > max_y)
2063 {
2064 r.y = max_y;
2065 r.height = height;
2066 }
2067 else
2068 {
2069 /* Don't draw cursor glyph taller than our actual glyph. */
2070 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2071 if (height < r.height)
2072 {
2073 max_y = r.y + r.height;
2074 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2075 r.height = min (max_y - r.y, height);
2076 }
2077 }
2078 }
2079
2080 if (s->row->clip)
2081 {
2082 XRectangle r_save = r;
2083
2084 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2085 r.width = 0;
2086 }
2087
2088 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2089 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2090 {
2091 #ifdef CONVERT_FROM_XRECT
2092 CONVERT_FROM_XRECT (r, *rects);
2093 #else
2094 *rects = r;
2095 #endif
2096 return 1;
2097 }
2098 else
2099 {
2100 /* If we are processing overlapping and allowed to return
2101 multiple clipping rectangles, we exclude the row of the glyph
2102 string from the clipping rectangle. This is to avoid drawing
2103 the same text on the environment with anti-aliasing. */
2104 #ifdef CONVERT_FROM_XRECT
2105 XRectangle rs[2];
2106 #else
2107 XRectangle *rs = rects;
2108 #endif
2109 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2110
2111 if (s->for_overlaps & OVERLAPS_PRED)
2112 {
2113 rs[i] = r;
2114 if (r.y + r.height > row_y)
2115 {
2116 if (r.y < row_y)
2117 rs[i].height = row_y - r.y;
2118 else
2119 rs[i].height = 0;
2120 }
2121 i++;
2122 }
2123 if (s->for_overlaps & OVERLAPS_SUCC)
2124 {
2125 rs[i] = r;
2126 if (r.y < row_y + s->row->visible_height)
2127 {
2128 if (r.y + r.height > row_y + s->row->visible_height)
2129 {
2130 rs[i].y = row_y + s->row->visible_height;
2131 rs[i].height = r.y + r.height - rs[i].y;
2132 }
2133 else
2134 rs[i].height = 0;
2135 }
2136 i++;
2137 }
2138
2139 n = i;
2140 #ifdef CONVERT_FROM_XRECT
2141 for (i = 0; i < n; i++)
2142 CONVERT_FROM_XRECT (rs[i], rects[i]);
2143 #endif
2144 return n;
2145 }
2146 }
2147
2148 /* EXPORT:
2149 Return in *NR the clipping rectangle for glyph string S. */
2150
2151 void
2152 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2153 {
2154 get_glyph_string_clip_rects (s, nr, 1);
2155 }
2156
2157
2158 /* EXPORT:
2159 Return the position and height of the phys cursor in window W.
2160 Set w->phys_cursor_width to width of phys cursor.
2161 */
2162
2163 void
2164 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2165 struct glyph *glyph, int *xp, int *yp, int *heightp)
2166 {
2167 struct frame *f = XFRAME (WINDOW_FRAME (w));
2168 int x, y, wd, h, h0, y0;
2169
2170 /* Compute the width of the rectangle to draw. If on a stretch
2171 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2172 rectangle as wide as the glyph, but use a canonical character
2173 width instead. */
2174 wd = glyph->pixel_width - 1;
2175 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2176 wd++; /* Why? */
2177 #endif
2178
2179 x = w->phys_cursor.x;
2180 if (x < 0)
2181 {
2182 wd += x;
2183 x = 0;
2184 }
2185
2186 if (glyph->type == STRETCH_GLYPH
2187 && !x_stretch_cursor_p)
2188 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2189 w->phys_cursor_width = wd;
2190
2191 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2192
2193 /* If y is below window bottom, ensure that we still see a cursor. */
2194 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2195
2196 h = max (h0, glyph->ascent + glyph->descent);
2197 h0 = min (h0, glyph->ascent + glyph->descent);
2198
2199 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2200 if (y < y0)
2201 {
2202 h = max (h - (y0 - y) + 1, h0);
2203 y = y0 - 1;
2204 }
2205 else
2206 {
2207 y0 = window_text_bottom_y (w) - h0;
2208 if (y > y0)
2209 {
2210 h += y - y0;
2211 y = y0;
2212 }
2213 }
2214
2215 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2216 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2217 *heightp = h;
2218 }
2219
2220 /*
2221 * Remember which glyph the mouse is over.
2222 */
2223
2224 void
2225 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2226 {
2227 Lisp_Object window;
2228 struct window *w;
2229 struct glyph_row *r, *gr, *end_row;
2230 enum window_part part;
2231 enum glyph_row_area area;
2232 int x, y, width, height;
2233
2234 /* Try to determine frame pixel position and size of the glyph under
2235 frame pixel coordinates X/Y on frame F. */
2236
2237 if (!f->glyphs_initialized_p
2238 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2239 NILP (window)))
2240 {
2241 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2242 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2243 goto virtual_glyph;
2244 }
2245
2246 w = XWINDOW (window);
2247 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2248 height = WINDOW_FRAME_LINE_HEIGHT (w);
2249
2250 x = window_relative_x_coord (w, part, gx);
2251 y = gy - WINDOW_TOP_EDGE_Y (w);
2252
2253 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2254 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2255
2256 if (w->pseudo_window_p)
2257 {
2258 area = TEXT_AREA;
2259 part = ON_MODE_LINE; /* Don't adjust margin. */
2260 goto text_glyph;
2261 }
2262
2263 switch (part)
2264 {
2265 case ON_LEFT_MARGIN:
2266 area = LEFT_MARGIN_AREA;
2267 goto text_glyph;
2268
2269 case ON_RIGHT_MARGIN:
2270 area = RIGHT_MARGIN_AREA;
2271 goto text_glyph;
2272
2273 case ON_HEADER_LINE:
2274 case ON_MODE_LINE:
2275 gr = (part == ON_HEADER_LINE
2276 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2277 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2278 gy = gr->y;
2279 area = TEXT_AREA;
2280 goto text_glyph_row_found;
2281
2282 case ON_TEXT:
2283 area = TEXT_AREA;
2284
2285 text_glyph:
2286 gr = 0; gy = 0;
2287 for (; r <= end_row && r->enabled_p; ++r)
2288 if (r->y + r->height > y)
2289 {
2290 gr = r; gy = r->y;
2291 break;
2292 }
2293
2294 text_glyph_row_found:
2295 if (gr && gy <= y)
2296 {
2297 struct glyph *g = gr->glyphs[area];
2298 struct glyph *end = g + gr->used[area];
2299
2300 height = gr->height;
2301 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2302 if (gx + g->pixel_width > x)
2303 break;
2304
2305 if (g < end)
2306 {
2307 if (g->type == IMAGE_GLYPH)
2308 {
2309 /* Don't remember when mouse is over image, as
2310 image may have hot-spots. */
2311 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2312 return;
2313 }
2314 width = g->pixel_width;
2315 }
2316 else
2317 {
2318 /* Use nominal char spacing at end of line. */
2319 x -= gx;
2320 gx += (x / width) * width;
2321 }
2322
2323 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2324 gx += window_box_left_offset (w, area);
2325 }
2326 else
2327 {
2328 /* Use nominal line height at end of window. */
2329 gx = (x / width) * width;
2330 y -= gy;
2331 gy += (y / height) * height;
2332 }
2333 break;
2334
2335 case ON_LEFT_FRINGE:
2336 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2338 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2339 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2340 goto row_glyph;
2341
2342 case ON_RIGHT_FRINGE:
2343 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2344 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2345 : window_box_right_offset (w, TEXT_AREA));
2346 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2347 goto row_glyph;
2348
2349 case ON_SCROLL_BAR:
2350 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2351 ? 0
2352 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2353 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2354 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2355 : 0)));
2356 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2357
2358 row_glyph:
2359 gr = 0, gy = 0;
2360 for (; r <= end_row && r->enabled_p; ++r)
2361 if (r->y + r->height > y)
2362 {
2363 gr = r; gy = r->y;
2364 break;
2365 }
2366
2367 if (gr && gy <= y)
2368 height = gr->height;
2369 else
2370 {
2371 /* Use nominal line height at end of window. */
2372 y -= gy;
2373 gy += (y / height) * height;
2374 }
2375 break;
2376
2377 default:
2378 ;
2379 virtual_glyph:
2380 /* If there is no glyph under the mouse, then we divide the screen
2381 into a grid of the smallest glyph in the frame, and use that
2382 as our "glyph". */
2383
2384 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2385 round down even for negative values. */
2386 if (gx < 0)
2387 gx -= width - 1;
2388 if (gy < 0)
2389 gy -= height - 1;
2390
2391 gx = (gx / width) * width;
2392 gy = (gy / height) * height;
2393
2394 goto store_rect;
2395 }
2396
2397 gx += WINDOW_LEFT_EDGE_X (w);
2398 gy += WINDOW_TOP_EDGE_Y (w);
2399
2400 store_rect:
2401 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2402
2403 /* Visible feedback for debugging. */
2404 #if 0
2405 #if HAVE_X_WINDOWS
2406 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2407 f->output_data.x->normal_gc,
2408 gx, gy, width, height);
2409 #endif
2410 #endif
2411 }
2412
2413
2414 #endif /* HAVE_WINDOW_SYSTEM */
2415
2416 \f
2417 /***********************************************************************
2418 Lisp form evaluation
2419 ***********************************************************************/
2420
2421 /* Error handler for safe_eval and safe_call. */
2422
2423 static Lisp_Object
2424 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2425 {
2426 add_to_log ("Error during redisplay: %S signaled %S",
2427 Flist (nargs, args), arg);
2428 return Qnil;
2429 }
2430
2431 /* Call function FUNC with the rest of NARGS - 1 arguments
2432 following. Return the result, or nil if something went
2433 wrong. Prevent redisplay during the evaluation. */
2434
2435 Lisp_Object
2436 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2437 {
2438 Lisp_Object val;
2439
2440 if (inhibit_eval_during_redisplay)
2441 val = Qnil;
2442 else
2443 {
2444 va_list ap;
2445 ptrdiff_t i;
2446 ptrdiff_t count = SPECPDL_INDEX ();
2447 struct gcpro gcpro1;
2448 Lisp_Object *args = alloca (nargs * word_size);
2449
2450 args[0] = func;
2451 va_start (ap, func);
2452 for (i = 1; i < nargs; i++)
2453 args[i] = va_arg (ap, Lisp_Object);
2454 va_end (ap);
2455
2456 GCPRO1 (args[0]);
2457 gcpro1.nvars = nargs;
2458 specbind (Qinhibit_redisplay, Qt);
2459 /* Use Qt to ensure debugger does not run,
2460 so there is no possibility of wanting to redisplay. */
2461 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2462 safe_eval_handler);
2463 UNGCPRO;
2464 val = unbind_to (count, val);
2465 }
2466
2467 return val;
2468 }
2469
2470
2471 /* Call function FN with one argument ARG.
2472 Return the result, or nil if something went wrong. */
2473
2474 Lisp_Object
2475 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2476 {
2477 return safe_call (2, fn, arg);
2478 }
2479
2480 static Lisp_Object Qeval;
2481
2482 Lisp_Object
2483 safe_eval (Lisp_Object sexpr)
2484 {
2485 return safe_call1 (Qeval, sexpr);
2486 }
2487
2488 /* Call function FN with two arguments ARG1 and ARG2.
2489 Return the result, or nil if something went wrong. */
2490
2491 Lisp_Object
2492 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2493 {
2494 return safe_call (3, fn, arg1, arg2);
2495 }
2496
2497
2498 \f
2499 /***********************************************************************
2500 Debugging
2501 ***********************************************************************/
2502
2503 #if 0
2504
2505 /* Define CHECK_IT to perform sanity checks on iterators.
2506 This is for debugging. It is too slow to do unconditionally. */
2507
2508 static void
2509 check_it (struct it *it)
2510 {
2511 if (it->method == GET_FROM_STRING)
2512 {
2513 eassert (STRINGP (it->string));
2514 eassert (IT_STRING_CHARPOS (*it) >= 0);
2515 }
2516 else
2517 {
2518 eassert (IT_STRING_CHARPOS (*it) < 0);
2519 if (it->method == GET_FROM_BUFFER)
2520 {
2521 /* Check that character and byte positions agree. */
2522 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2523 }
2524 }
2525
2526 if (it->dpvec)
2527 eassert (it->current.dpvec_index >= 0);
2528 else
2529 eassert (it->current.dpvec_index < 0);
2530 }
2531
2532 #define CHECK_IT(IT) check_it ((IT))
2533
2534 #else /* not 0 */
2535
2536 #define CHECK_IT(IT) (void) 0
2537
2538 #endif /* not 0 */
2539
2540
2541 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2542
2543 /* Check that the window end of window W is what we expect it
2544 to be---the last row in the current matrix displaying text. */
2545
2546 static void
2547 check_window_end (struct window *w)
2548 {
2549 if (!MINI_WINDOW_P (w)
2550 && !NILP (w->window_end_valid))
2551 {
2552 struct glyph_row *row;
2553 eassert ((row = MATRIX_ROW (w->current_matrix,
2554 XFASTINT (w->window_end_vpos)),
2555 !row->enabled_p
2556 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2557 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2558 }
2559 }
2560
2561 #define CHECK_WINDOW_END(W) check_window_end ((W))
2562
2563 #else
2564
2565 #define CHECK_WINDOW_END(W) (void) 0
2566
2567 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2568
2569
2570 \f
2571 /***********************************************************************
2572 Iterator initialization
2573 ***********************************************************************/
2574
2575 /* Initialize IT for displaying current_buffer in window W, starting
2576 at character position CHARPOS. CHARPOS < 0 means that no buffer
2577 position is specified which is useful when the iterator is assigned
2578 a position later. BYTEPOS is the byte position corresponding to
2579 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2580
2581 If ROW is not null, calls to produce_glyphs with IT as parameter
2582 will produce glyphs in that row.
2583
2584 BASE_FACE_ID is the id of a base face to use. It must be one of
2585 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2586 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2587 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2588
2589 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2590 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2591 will be initialized to use the corresponding mode line glyph row of
2592 the desired matrix of W. */
2593
2594 void
2595 init_iterator (struct it *it, struct window *w,
2596 ptrdiff_t charpos, ptrdiff_t bytepos,
2597 struct glyph_row *row, enum face_id base_face_id)
2598 {
2599 int highlight_region_p;
2600 enum face_id remapped_base_face_id = base_face_id;
2601
2602 /* Some precondition checks. */
2603 eassert (w != NULL && it != NULL);
2604 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2605 && charpos <= ZV));
2606
2607 /* If face attributes have been changed since the last redisplay,
2608 free realized faces now because they depend on face definitions
2609 that might have changed. Don't free faces while there might be
2610 desired matrices pending which reference these faces. */
2611 if (face_change_count && !inhibit_free_realized_faces)
2612 {
2613 face_change_count = 0;
2614 free_all_realized_faces (Qnil);
2615 }
2616
2617 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2618 if (! NILP (Vface_remapping_alist))
2619 remapped_base_face_id
2620 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2621
2622 /* Use one of the mode line rows of W's desired matrix if
2623 appropriate. */
2624 if (row == NULL)
2625 {
2626 if (base_face_id == MODE_LINE_FACE_ID
2627 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2628 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2629 else if (base_face_id == HEADER_LINE_FACE_ID)
2630 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2631 }
2632
2633 /* Clear IT. */
2634 memset (it, 0, sizeof *it);
2635 it->current.overlay_string_index = -1;
2636 it->current.dpvec_index = -1;
2637 it->base_face_id = remapped_base_face_id;
2638 it->string = Qnil;
2639 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2640 it->paragraph_embedding = L2R;
2641 it->bidi_it.string.lstring = Qnil;
2642 it->bidi_it.string.s = NULL;
2643 it->bidi_it.string.bufpos = 0;
2644
2645 /* The window in which we iterate over current_buffer: */
2646 XSETWINDOW (it->window, w);
2647 it->w = w;
2648 it->f = XFRAME (w->frame);
2649
2650 it->cmp_it.id = -1;
2651
2652 /* Extra space between lines (on window systems only). */
2653 if (base_face_id == DEFAULT_FACE_ID
2654 && FRAME_WINDOW_P (it->f))
2655 {
2656 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2657 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2658 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2659 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2660 * FRAME_LINE_HEIGHT (it->f));
2661 else if (it->f->extra_line_spacing > 0)
2662 it->extra_line_spacing = it->f->extra_line_spacing;
2663 it->max_extra_line_spacing = 0;
2664 }
2665
2666 /* If realized faces have been removed, e.g. because of face
2667 attribute changes of named faces, recompute them. When running
2668 in batch mode, the face cache of the initial frame is null. If
2669 we happen to get called, make a dummy face cache. */
2670 if (FRAME_FACE_CACHE (it->f) == NULL)
2671 init_frame_faces (it->f);
2672 if (FRAME_FACE_CACHE (it->f)->used == 0)
2673 recompute_basic_faces (it->f);
2674
2675 /* Current value of the `slice', `space-width', and 'height' properties. */
2676 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2677 it->space_width = Qnil;
2678 it->font_height = Qnil;
2679 it->override_ascent = -1;
2680
2681 /* Are control characters displayed as `^C'? */
2682 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2683
2684 /* -1 means everything between a CR and the following line end
2685 is invisible. >0 means lines indented more than this value are
2686 invisible. */
2687 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2688 ? (clip_to_bounds
2689 (-1, XINT (BVAR (current_buffer, selective_display)),
2690 PTRDIFF_MAX))
2691 : (!NILP (BVAR (current_buffer, selective_display))
2692 ? -1 : 0));
2693 it->selective_display_ellipsis_p
2694 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2695
2696 /* Display table to use. */
2697 it->dp = window_display_table (w);
2698
2699 /* Are multibyte characters enabled in current_buffer? */
2700 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2701
2702 /* Non-zero if we should highlight the region. */
2703 highlight_region_p
2704 = (!NILP (Vtransient_mark_mode)
2705 && !NILP (BVAR (current_buffer, mark_active))
2706 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2707
2708 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2709 start and end of a visible region in window IT->w. Set both to
2710 -1 to indicate no region. */
2711 if (highlight_region_p
2712 /* Maybe highlight only in selected window. */
2713 && (/* Either show region everywhere. */
2714 highlight_nonselected_windows
2715 /* Or show region in the selected window. */
2716 || w == XWINDOW (selected_window)
2717 /* Or show the region if we are in the mini-buffer and W is
2718 the window the mini-buffer refers to. */
2719 || (MINI_WINDOW_P (XWINDOW (selected_window))
2720 && WINDOWP (minibuf_selected_window)
2721 && w == XWINDOW (minibuf_selected_window))))
2722 {
2723 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2724 it->region_beg_charpos = min (PT, markpos);
2725 it->region_end_charpos = max (PT, markpos);
2726 }
2727 else
2728 it->region_beg_charpos = it->region_end_charpos = -1;
2729
2730 /* Get the position at which the redisplay_end_trigger hook should
2731 be run, if it is to be run at all. */
2732 if (MARKERP (w->redisplay_end_trigger)
2733 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2734 it->redisplay_end_trigger_charpos
2735 = marker_position (w->redisplay_end_trigger);
2736 else if (INTEGERP (w->redisplay_end_trigger))
2737 it->redisplay_end_trigger_charpos =
2738 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2739
2740 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2741
2742 /* Are lines in the display truncated? */
2743 if (base_face_id != DEFAULT_FACE_ID
2744 || it->w->hscroll
2745 || (! WINDOW_FULL_WIDTH_P (it->w)
2746 && ((!NILP (Vtruncate_partial_width_windows)
2747 && !INTEGERP (Vtruncate_partial_width_windows))
2748 || (INTEGERP (Vtruncate_partial_width_windows)
2749 && (WINDOW_TOTAL_COLS (it->w)
2750 < XINT (Vtruncate_partial_width_windows))))))
2751 it->line_wrap = TRUNCATE;
2752 else if (NILP (BVAR (current_buffer, truncate_lines)))
2753 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2754 ? WINDOW_WRAP : WORD_WRAP;
2755 else
2756 it->line_wrap = TRUNCATE;
2757
2758 /* Get dimensions of truncation and continuation glyphs. These are
2759 displayed as fringe bitmaps under X, but we need them for such
2760 frames when the fringes are turned off. But leave the dimensions
2761 zero for tooltip frames, as these glyphs look ugly there and also
2762 sabotage calculations of tooltip dimensions in x-show-tip. */
2763 #ifdef HAVE_WINDOW_SYSTEM
2764 if (!(FRAME_WINDOW_P (it->f)
2765 && FRAMEP (tip_frame)
2766 && it->f == XFRAME (tip_frame)))
2767 #endif
2768 {
2769 if (it->line_wrap == TRUNCATE)
2770 {
2771 /* We will need the truncation glyph. */
2772 eassert (it->glyph_row == NULL);
2773 produce_special_glyphs (it, IT_TRUNCATION);
2774 it->truncation_pixel_width = it->pixel_width;
2775 }
2776 else
2777 {
2778 /* We will need the continuation glyph. */
2779 eassert (it->glyph_row == NULL);
2780 produce_special_glyphs (it, IT_CONTINUATION);
2781 it->continuation_pixel_width = it->pixel_width;
2782 }
2783 }
2784
2785 /* Reset these values to zero because the produce_special_glyphs
2786 above has changed them. */
2787 it->pixel_width = it->ascent = it->descent = 0;
2788 it->phys_ascent = it->phys_descent = 0;
2789
2790 /* Set this after getting the dimensions of truncation and
2791 continuation glyphs, so that we don't produce glyphs when calling
2792 produce_special_glyphs, above. */
2793 it->glyph_row = row;
2794 it->area = TEXT_AREA;
2795
2796 /* Forget any previous info about this row being reversed. */
2797 if (it->glyph_row)
2798 it->glyph_row->reversed_p = 0;
2799
2800 /* Get the dimensions of the display area. The display area
2801 consists of the visible window area plus a horizontally scrolled
2802 part to the left of the window. All x-values are relative to the
2803 start of this total display area. */
2804 if (base_face_id != DEFAULT_FACE_ID)
2805 {
2806 /* Mode lines, menu bar in terminal frames. */
2807 it->first_visible_x = 0;
2808 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2809 }
2810 else
2811 {
2812 it->first_visible_x =
2813 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2814 it->last_visible_x = (it->first_visible_x
2815 + window_box_width (w, TEXT_AREA));
2816
2817 /* If we truncate lines, leave room for the truncation glyph(s) at
2818 the right margin. Otherwise, leave room for the continuation
2819 glyph(s). Done only if the window has no fringes. Since we
2820 don't know at this point whether there will be any R2L lines in
2821 the window, we reserve space for truncation/continuation glyphs
2822 even if only one of the fringes is absent. */
2823 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2824 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2825 {
2826 if (it->line_wrap == TRUNCATE)
2827 it->last_visible_x -= it->truncation_pixel_width;
2828 else
2829 it->last_visible_x -= it->continuation_pixel_width;
2830 }
2831
2832 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2833 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2834 }
2835
2836 /* Leave room for a border glyph. */
2837 if (!FRAME_WINDOW_P (it->f)
2838 && !WINDOW_RIGHTMOST_P (it->w))
2839 it->last_visible_x -= 1;
2840
2841 it->last_visible_y = window_text_bottom_y (w);
2842
2843 /* For mode lines and alike, arrange for the first glyph having a
2844 left box line if the face specifies a box. */
2845 if (base_face_id != DEFAULT_FACE_ID)
2846 {
2847 struct face *face;
2848
2849 it->face_id = remapped_base_face_id;
2850
2851 /* If we have a boxed mode line, make the first character appear
2852 with a left box line. */
2853 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2854 if (face->box != FACE_NO_BOX)
2855 it->start_of_box_run_p = 1;
2856 }
2857
2858 /* If a buffer position was specified, set the iterator there,
2859 getting overlays and face properties from that position. */
2860 if (charpos >= BUF_BEG (current_buffer))
2861 {
2862 it->end_charpos = ZV;
2863 IT_CHARPOS (*it) = charpos;
2864
2865 /* We will rely on `reseat' to set this up properly, via
2866 handle_face_prop. */
2867 it->face_id = it->base_face_id;
2868
2869 /* Compute byte position if not specified. */
2870 if (bytepos < charpos)
2871 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2872 else
2873 IT_BYTEPOS (*it) = bytepos;
2874
2875 it->start = it->current;
2876 /* Do we need to reorder bidirectional text? Not if this is a
2877 unibyte buffer: by definition, none of the single-byte
2878 characters are strong R2L, so no reordering is needed. And
2879 bidi.c doesn't support unibyte buffers anyway. Also, don't
2880 reorder while we are loading loadup.el, since the tables of
2881 character properties needed for reordering are not yet
2882 available. */
2883 it->bidi_p =
2884 NILP (Vpurify_flag)
2885 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2886 && it->multibyte_p;
2887
2888 /* If we are to reorder bidirectional text, init the bidi
2889 iterator. */
2890 if (it->bidi_p)
2891 {
2892 /* Note the paragraph direction that this buffer wants to
2893 use. */
2894 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2895 Qleft_to_right))
2896 it->paragraph_embedding = L2R;
2897 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qright_to_left))
2899 it->paragraph_embedding = R2L;
2900 else
2901 it->paragraph_embedding = NEUTRAL_DIR;
2902 bidi_unshelve_cache (NULL, 0);
2903 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2904 &it->bidi_it);
2905 }
2906
2907 /* Compute faces etc. */
2908 reseat (it, it->current.pos, 1);
2909 }
2910
2911 CHECK_IT (it);
2912 }
2913
2914
2915 /* Initialize IT for the display of window W with window start POS. */
2916
2917 void
2918 start_display (struct it *it, struct window *w, struct text_pos pos)
2919 {
2920 struct glyph_row *row;
2921 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2922
2923 row = w->desired_matrix->rows + first_vpos;
2924 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2925 it->first_vpos = first_vpos;
2926
2927 /* Don't reseat to previous visible line start if current start
2928 position is in a string or image. */
2929 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2930 {
2931 int start_at_line_beg_p;
2932 int first_y = it->current_y;
2933
2934 /* If window start is not at a line start, skip forward to POS to
2935 get the correct continuation lines width. */
2936 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2937 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2938 if (!start_at_line_beg_p)
2939 {
2940 int new_x;
2941
2942 reseat_at_previous_visible_line_start (it);
2943 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2944
2945 new_x = it->current_x + it->pixel_width;
2946
2947 /* If lines are continued, this line may end in the middle
2948 of a multi-glyph character (e.g. a control character
2949 displayed as \003, or in the middle of an overlay
2950 string). In this case move_it_to above will not have
2951 taken us to the start of the continuation line but to the
2952 end of the continued line. */
2953 if (it->current_x > 0
2954 && it->line_wrap != TRUNCATE /* Lines are continued. */
2955 && (/* And glyph doesn't fit on the line. */
2956 new_x > it->last_visible_x
2957 /* Or it fits exactly and we're on a window
2958 system frame. */
2959 || (new_x == it->last_visible_x
2960 && FRAME_WINDOW_P (it->f)
2961 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2962 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2963 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2964 {
2965 if ((it->current.dpvec_index >= 0
2966 || it->current.overlay_string_index >= 0)
2967 /* If we are on a newline from a display vector or
2968 overlay string, then we are already at the end of
2969 a screen line; no need to go to the next line in
2970 that case, as this line is not really continued.
2971 (If we do go to the next line, C-e will not DTRT.) */
2972 && it->c != '\n')
2973 {
2974 set_iterator_to_next (it, 1);
2975 move_it_in_display_line_to (it, -1, -1, 0);
2976 }
2977
2978 it->continuation_lines_width += it->current_x;
2979 }
2980 /* If the character at POS is displayed via a display
2981 vector, move_it_to above stops at the final glyph of
2982 IT->dpvec. To make the caller redisplay that character
2983 again (a.k.a. start at POS), we need to reset the
2984 dpvec_index to the beginning of IT->dpvec. */
2985 else if (it->current.dpvec_index >= 0)
2986 it->current.dpvec_index = 0;
2987
2988 /* We're starting a new display line, not affected by the
2989 height of the continued line, so clear the appropriate
2990 fields in the iterator structure. */
2991 it->max_ascent = it->max_descent = 0;
2992 it->max_phys_ascent = it->max_phys_descent = 0;
2993
2994 it->current_y = first_y;
2995 it->vpos = 0;
2996 it->current_x = it->hpos = 0;
2997 }
2998 }
2999 }
3000
3001
3002 /* Return 1 if POS is a position in ellipses displayed for invisible
3003 text. W is the window we display, for text property lookup. */
3004
3005 static int
3006 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3007 {
3008 Lisp_Object prop, window;
3009 int ellipses_p = 0;
3010 ptrdiff_t charpos = CHARPOS (pos->pos);
3011
3012 /* If POS specifies a position in a display vector, this might
3013 be for an ellipsis displayed for invisible text. We won't
3014 get the iterator set up for delivering that ellipsis unless
3015 we make sure that it gets aware of the invisible text. */
3016 if (pos->dpvec_index >= 0
3017 && pos->overlay_string_index < 0
3018 && CHARPOS (pos->string_pos) < 0
3019 && charpos > BEGV
3020 && (XSETWINDOW (window, w),
3021 prop = Fget_char_property (make_number (charpos),
3022 Qinvisible, window),
3023 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3024 {
3025 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3026 window);
3027 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3028 }
3029
3030 return ellipses_p;
3031 }
3032
3033
3034 /* Initialize IT for stepping through current_buffer in window W,
3035 starting at position POS that includes overlay string and display
3036 vector/ control character translation position information. Value
3037 is zero if there are overlay strings with newlines at POS. */
3038
3039 static int
3040 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3041 {
3042 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3043 int i, overlay_strings_with_newlines = 0;
3044
3045 /* If POS specifies a position in a display vector, this might
3046 be for an ellipsis displayed for invisible text. We won't
3047 get the iterator set up for delivering that ellipsis unless
3048 we make sure that it gets aware of the invisible text. */
3049 if (in_ellipses_for_invisible_text_p (pos, w))
3050 {
3051 --charpos;
3052 bytepos = 0;
3053 }
3054
3055 /* Keep in mind: the call to reseat in init_iterator skips invisible
3056 text, so we might end up at a position different from POS. This
3057 is only a problem when POS is a row start after a newline and an
3058 overlay starts there with an after-string, and the overlay has an
3059 invisible property. Since we don't skip invisible text in
3060 display_line and elsewhere immediately after consuming the
3061 newline before the row start, such a POS will not be in a string,
3062 but the call to init_iterator below will move us to the
3063 after-string. */
3064 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3065
3066 /* This only scans the current chunk -- it should scan all chunks.
3067 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3068 to 16 in 22.1 to make this a lesser problem. */
3069 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3070 {
3071 const char *s = SSDATA (it->overlay_strings[i]);
3072 const char *e = s + SBYTES (it->overlay_strings[i]);
3073
3074 while (s < e && *s != '\n')
3075 ++s;
3076
3077 if (s < e)
3078 {
3079 overlay_strings_with_newlines = 1;
3080 break;
3081 }
3082 }
3083
3084 /* If position is within an overlay string, set up IT to the right
3085 overlay string. */
3086 if (pos->overlay_string_index >= 0)
3087 {
3088 int relative_index;
3089
3090 /* If the first overlay string happens to have a `display'
3091 property for an image, the iterator will be set up for that
3092 image, and we have to undo that setup first before we can
3093 correct the overlay string index. */
3094 if (it->method == GET_FROM_IMAGE)
3095 pop_it (it);
3096
3097 /* We already have the first chunk of overlay strings in
3098 IT->overlay_strings. Load more until the one for
3099 pos->overlay_string_index is in IT->overlay_strings. */
3100 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3101 {
3102 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3103 it->current.overlay_string_index = 0;
3104 while (n--)
3105 {
3106 load_overlay_strings (it, 0);
3107 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3108 }
3109 }
3110
3111 it->current.overlay_string_index = pos->overlay_string_index;
3112 relative_index = (it->current.overlay_string_index
3113 % OVERLAY_STRING_CHUNK_SIZE);
3114 it->string = it->overlay_strings[relative_index];
3115 eassert (STRINGP (it->string));
3116 it->current.string_pos = pos->string_pos;
3117 it->method = GET_FROM_STRING;
3118 it->end_charpos = SCHARS (it->string);
3119 /* Set up the bidi iterator for this overlay string. */
3120 if (it->bidi_p)
3121 {
3122 it->bidi_it.string.lstring = it->string;
3123 it->bidi_it.string.s = NULL;
3124 it->bidi_it.string.schars = SCHARS (it->string);
3125 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3126 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3127 it->bidi_it.string.unibyte = !it->multibyte_p;
3128 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3129 FRAME_WINDOW_P (it->f), &it->bidi_it);
3130
3131 /* Synchronize the state of the bidi iterator with
3132 pos->string_pos. For any string position other than
3133 zero, this will be done automagically when we resume
3134 iteration over the string and get_visually_first_element
3135 is called. But if string_pos is zero, and the string is
3136 to be reordered for display, we need to resync manually,
3137 since it could be that the iteration state recorded in
3138 pos ended at string_pos of 0 moving backwards in string. */
3139 if (CHARPOS (pos->string_pos) == 0)
3140 {
3141 get_visually_first_element (it);
3142 if (IT_STRING_CHARPOS (*it) != 0)
3143 do {
3144 /* Paranoia. */
3145 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3146 bidi_move_to_visually_next (&it->bidi_it);
3147 } while (it->bidi_it.charpos != 0);
3148 }
3149 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3150 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3151 }
3152 }
3153
3154 if (CHARPOS (pos->string_pos) >= 0)
3155 {
3156 /* Recorded position is not in an overlay string, but in another
3157 string. This can only be a string from a `display' property.
3158 IT should already be filled with that string. */
3159 it->current.string_pos = pos->string_pos;
3160 eassert (STRINGP (it->string));
3161 if (it->bidi_p)
3162 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3163 FRAME_WINDOW_P (it->f), &it->bidi_it);
3164 }
3165
3166 /* Restore position in display vector translations, control
3167 character translations or ellipses. */
3168 if (pos->dpvec_index >= 0)
3169 {
3170 if (it->dpvec == NULL)
3171 get_next_display_element (it);
3172 eassert (it->dpvec && it->current.dpvec_index == 0);
3173 it->current.dpvec_index = pos->dpvec_index;
3174 }
3175
3176 CHECK_IT (it);
3177 return !overlay_strings_with_newlines;
3178 }
3179
3180
3181 /* Initialize IT for stepping through current_buffer in window W
3182 starting at ROW->start. */
3183
3184 static void
3185 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3186 {
3187 init_from_display_pos (it, w, &row->start);
3188 it->start = row->start;
3189 it->continuation_lines_width = row->continuation_lines_width;
3190 CHECK_IT (it);
3191 }
3192
3193
3194 /* Initialize IT for stepping through current_buffer in window W
3195 starting in the line following ROW, i.e. starting at ROW->end.
3196 Value is zero if there are overlay strings with newlines at ROW's
3197 end position. */
3198
3199 static int
3200 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3201 {
3202 int success = 0;
3203
3204 if (init_from_display_pos (it, w, &row->end))
3205 {
3206 if (row->continued_p)
3207 it->continuation_lines_width
3208 = row->continuation_lines_width + row->pixel_width;
3209 CHECK_IT (it);
3210 success = 1;
3211 }
3212
3213 return success;
3214 }
3215
3216
3217
3218 \f
3219 /***********************************************************************
3220 Text properties
3221 ***********************************************************************/
3222
3223 /* Called when IT reaches IT->stop_charpos. Handle text property and
3224 overlay changes. Set IT->stop_charpos to the next position where
3225 to stop. */
3226
3227 static void
3228 handle_stop (struct it *it)
3229 {
3230 enum prop_handled handled;
3231 int handle_overlay_change_p;
3232 struct props *p;
3233
3234 it->dpvec = NULL;
3235 it->current.dpvec_index = -1;
3236 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3237 it->ignore_overlay_strings_at_pos_p = 0;
3238 it->ellipsis_p = 0;
3239
3240 /* Use face of preceding text for ellipsis (if invisible) */
3241 if (it->selective_display_ellipsis_p)
3242 it->saved_face_id = it->face_id;
3243
3244 do
3245 {
3246 handled = HANDLED_NORMALLY;
3247
3248 /* Call text property handlers. */
3249 for (p = it_props; p->handler; ++p)
3250 {
3251 handled = p->handler (it);
3252
3253 if (handled == HANDLED_RECOMPUTE_PROPS)
3254 break;
3255 else if (handled == HANDLED_RETURN)
3256 {
3257 /* We still want to show before and after strings from
3258 overlays even if the actual buffer text is replaced. */
3259 if (!handle_overlay_change_p
3260 || it->sp > 1
3261 /* Don't call get_overlay_strings_1 if we already
3262 have overlay strings loaded, because doing so
3263 will load them again and push the iterator state
3264 onto the stack one more time, which is not
3265 expected by the rest of the code that processes
3266 overlay strings. */
3267 || (it->current.overlay_string_index < 0
3268 ? !get_overlay_strings_1 (it, 0, 0)
3269 : 0))
3270 {
3271 if (it->ellipsis_p)
3272 setup_for_ellipsis (it, 0);
3273 /* When handling a display spec, we might load an
3274 empty string. In that case, discard it here. We
3275 used to discard it in handle_single_display_spec,
3276 but that causes get_overlay_strings_1, above, to
3277 ignore overlay strings that we must check. */
3278 if (STRINGP (it->string) && !SCHARS (it->string))
3279 pop_it (it);
3280 return;
3281 }
3282 else if (STRINGP (it->string) && !SCHARS (it->string))
3283 pop_it (it);
3284 else
3285 {
3286 it->ignore_overlay_strings_at_pos_p = 1;
3287 it->string_from_display_prop_p = 0;
3288 it->from_disp_prop_p = 0;
3289 handle_overlay_change_p = 0;
3290 }
3291 handled = HANDLED_RECOMPUTE_PROPS;
3292 break;
3293 }
3294 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3295 handle_overlay_change_p = 0;
3296 }
3297
3298 if (handled != HANDLED_RECOMPUTE_PROPS)
3299 {
3300 /* Don't check for overlay strings below when set to deliver
3301 characters from a display vector. */
3302 if (it->method == GET_FROM_DISPLAY_VECTOR)
3303 handle_overlay_change_p = 0;
3304
3305 /* Handle overlay changes.
3306 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3307 if it finds overlays. */
3308 if (handle_overlay_change_p)
3309 handled = handle_overlay_change (it);
3310 }
3311
3312 if (it->ellipsis_p)
3313 {
3314 setup_for_ellipsis (it, 0);
3315 break;
3316 }
3317 }
3318 while (handled == HANDLED_RECOMPUTE_PROPS);
3319
3320 /* Determine where to stop next. */
3321 if (handled == HANDLED_NORMALLY)
3322 compute_stop_pos (it);
3323 }
3324
3325
3326 /* Compute IT->stop_charpos from text property and overlay change
3327 information for IT's current position. */
3328
3329 static void
3330 compute_stop_pos (struct it *it)
3331 {
3332 register INTERVAL iv, next_iv;
3333 Lisp_Object object, limit, position;
3334 ptrdiff_t charpos, bytepos;
3335
3336 if (STRINGP (it->string))
3337 {
3338 /* Strings are usually short, so don't limit the search for
3339 properties. */
3340 it->stop_charpos = it->end_charpos;
3341 object = it->string;
3342 limit = Qnil;
3343 charpos = IT_STRING_CHARPOS (*it);
3344 bytepos = IT_STRING_BYTEPOS (*it);
3345 }
3346 else
3347 {
3348 ptrdiff_t pos;
3349
3350 /* If end_charpos is out of range for some reason, such as a
3351 misbehaving display function, rationalize it (Bug#5984). */
3352 if (it->end_charpos > ZV)
3353 it->end_charpos = ZV;
3354 it->stop_charpos = it->end_charpos;
3355
3356 /* If next overlay change is in front of the current stop pos
3357 (which is IT->end_charpos), stop there. Note: value of
3358 next_overlay_change is point-max if no overlay change
3359 follows. */
3360 charpos = IT_CHARPOS (*it);
3361 bytepos = IT_BYTEPOS (*it);
3362 pos = next_overlay_change (charpos);
3363 if (pos < it->stop_charpos)
3364 it->stop_charpos = pos;
3365
3366 /* If showing the region, we have to stop at the region
3367 start or end because the face might change there. */
3368 if (it->region_beg_charpos > 0)
3369 {
3370 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3371 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3372 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3373 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3374 }
3375
3376 /* Set up variables for computing the stop position from text
3377 property changes. */
3378 XSETBUFFER (object, current_buffer);
3379 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3380 }
3381
3382 /* Get the interval containing IT's position. Value is a null
3383 interval if there isn't such an interval. */
3384 position = make_number (charpos);
3385 iv = validate_interval_range (object, &position, &position, 0);
3386 if (iv)
3387 {
3388 Lisp_Object values_here[LAST_PROP_IDX];
3389 struct props *p;
3390
3391 /* Get properties here. */
3392 for (p = it_props; p->handler; ++p)
3393 values_here[p->idx] = textget (iv->plist, *p->name);
3394
3395 /* Look for an interval following iv that has different
3396 properties. */
3397 for (next_iv = next_interval (iv);
3398 (next_iv
3399 && (NILP (limit)
3400 || XFASTINT (limit) > next_iv->position));
3401 next_iv = next_interval (next_iv))
3402 {
3403 for (p = it_props; p->handler; ++p)
3404 {
3405 Lisp_Object new_value;
3406
3407 new_value = textget (next_iv->plist, *p->name);
3408 if (!EQ (values_here[p->idx], new_value))
3409 break;
3410 }
3411
3412 if (p->handler)
3413 break;
3414 }
3415
3416 if (next_iv)
3417 {
3418 if (INTEGERP (limit)
3419 && next_iv->position >= XFASTINT (limit))
3420 /* No text property change up to limit. */
3421 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3422 else
3423 /* Text properties change in next_iv. */
3424 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3425 }
3426 }
3427
3428 if (it->cmp_it.id < 0)
3429 {
3430 ptrdiff_t stoppos = it->end_charpos;
3431
3432 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3433 stoppos = -1;
3434 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3435 stoppos, it->string);
3436 }
3437
3438 eassert (STRINGP (it->string)
3439 || (it->stop_charpos >= BEGV
3440 && it->stop_charpos >= IT_CHARPOS (*it)));
3441 }
3442
3443
3444 /* Return the position of the next overlay change after POS in
3445 current_buffer. Value is point-max if no overlay change
3446 follows. This is like `next-overlay-change' but doesn't use
3447 xmalloc. */
3448
3449 static ptrdiff_t
3450 next_overlay_change (ptrdiff_t pos)
3451 {
3452 ptrdiff_t i, noverlays;
3453 ptrdiff_t endpos;
3454 Lisp_Object *overlays;
3455
3456 /* Get all overlays at the given position. */
3457 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3458
3459 /* If any of these overlays ends before endpos,
3460 use its ending point instead. */
3461 for (i = 0; i < noverlays; ++i)
3462 {
3463 Lisp_Object oend;
3464 ptrdiff_t oendpos;
3465
3466 oend = OVERLAY_END (overlays[i]);
3467 oendpos = OVERLAY_POSITION (oend);
3468 endpos = min (endpos, oendpos);
3469 }
3470
3471 return endpos;
3472 }
3473
3474 /* How many characters forward to search for a display property or
3475 display string. Searching too far forward makes the bidi display
3476 sluggish, especially in small windows. */
3477 #define MAX_DISP_SCAN 250
3478
3479 /* Return the character position of a display string at or after
3480 position specified by POSITION. If no display string exists at or
3481 after POSITION, return ZV. A display string is either an overlay
3482 with `display' property whose value is a string, or a `display'
3483 text property whose value is a string. STRING is data about the
3484 string to iterate; if STRING->lstring is nil, we are iterating a
3485 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3486 on a GUI frame. DISP_PROP is set to zero if we searched
3487 MAX_DISP_SCAN characters forward without finding any display
3488 strings, non-zero otherwise. It is set to 2 if the display string
3489 uses any kind of `(space ...)' spec that will produce a stretch of
3490 white space in the text area. */
3491 ptrdiff_t
3492 compute_display_string_pos (struct text_pos *position,
3493 struct bidi_string_data *string,
3494 int frame_window_p, int *disp_prop)
3495 {
3496 /* OBJECT = nil means current buffer. */
3497 Lisp_Object object =
3498 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3499 Lisp_Object pos, spec, limpos;
3500 int string_p = (string && (STRINGP (string->lstring) || string->s));
3501 ptrdiff_t eob = string_p ? string->schars : ZV;
3502 ptrdiff_t begb = string_p ? 0 : BEGV;
3503 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3504 ptrdiff_t lim =
3505 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3506 struct text_pos tpos;
3507 int rv = 0;
3508
3509 *disp_prop = 1;
3510
3511 if (charpos >= eob
3512 /* We don't support display properties whose values are strings
3513 that have display string properties. */
3514 || string->from_disp_str
3515 /* C strings cannot have display properties. */
3516 || (string->s && !STRINGP (object)))
3517 {
3518 *disp_prop = 0;
3519 return eob;
3520 }
3521
3522 /* If the character at CHARPOS is where the display string begins,
3523 return CHARPOS. */
3524 pos = make_number (charpos);
3525 if (STRINGP (object))
3526 bufpos = string->bufpos;
3527 else
3528 bufpos = charpos;
3529 tpos = *position;
3530 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3531 && (charpos <= begb
3532 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3533 object),
3534 spec))
3535 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3536 frame_window_p)))
3537 {
3538 if (rv == 2)
3539 *disp_prop = 2;
3540 return charpos;
3541 }
3542
3543 /* Look forward for the first character with a `display' property
3544 that will replace the underlying text when displayed. */
3545 limpos = make_number (lim);
3546 do {
3547 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3548 CHARPOS (tpos) = XFASTINT (pos);
3549 if (CHARPOS (tpos) >= lim)
3550 {
3551 *disp_prop = 0;
3552 break;
3553 }
3554 if (STRINGP (object))
3555 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3556 else
3557 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3558 spec = Fget_char_property (pos, Qdisplay, object);
3559 if (!STRINGP (object))
3560 bufpos = CHARPOS (tpos);
3561 } while (NILP (spec)
3562 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3563 bufpos, frame_window_p)));
3564 if (rv == 2)
3565 *disp_prop = 2;
3566
3567 return CHARPOS (tpos);
3568 }
3569
3570 /* Return the character position of the end of the display string that
3571 started at CHARPOS. If there's no display string at CHARPOS,
3572 return -1. A display string is either an overlay with `display'
3573 property whose value is a string or a `display' text property whose
3574 value is a string. */
3575 ptrdiff_t
3576 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3577 {
3578 /* OBJECT = nil means current buffer. */
3579 Lisp_Object object =
3580 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3581 Lisp_Object pos = make_number (charpos);
3582 ptrdiff_t eob =
3583 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3584
3585 if (charpos >= eob || (string->s && !STRINGP (object)))
3586 return eob;
3587
3588 /* It could happen that the display property or overlay was removed
3589 since we found it in compute_display_string_pos above. One way
3590 this can happen is if JIT font-lock was called (through
3591 handle_fontified_prop), and jit-lock-functions remove text
3592 properties or overlays from the portion of buffer that includes
3593 CHARPOS. Muse mode is known to do that, for example. In this
3594 case, we return -1 to the caller, to signal that no display
3595 string is actually present at CHARPOS. See bidi_fetch_char for
3596 how this is handled.
3597
3598 An alternative would be to never look for display properties past
3599 it->stop_charpos. But neither compute_display_string_pos nor
3600 bidi_fetch_char that calls it know or care where the next
3601 stop_charpos is. */
3602 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3603 return -1;
3604
3605 /* Look forward for the first character where the `display' property
3606 changes. */
3607 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3608
3609 return XFASTINT (pos);
3610 }
3611
3612
3613 \f
3614 /***********************************************************************
3615 Fontification
3616 ***********************************************************************/
3617
3618 /* Handle changes in the `fontified' property of the current buffer by
3619 calling hook functions from Qfontification_functions to fontify
3620 regions of text. */
3621
3622 static enum prop_handled
3623 handle_fontified_prop (struct it *it)
3624 {
3625 Lisp_Object prop, pos;
3626 enum prop_handled handled = HANDLED_NORMALLY;
3627
3628 if (!NILP (Vmemory_full))
3629 return handled;
3630
3631 /* Get the value of the `fontified' property at IT's current buffer
3632 position. (The `fontified' property doesn't have a special
3633 meaning in strings.) If the value is nil, call functions from
3634 Qfontification_functions. */
3635 if (!STRINGP (it->string)
3636 && it->s == NULL
3637 && !NILP (Vfontification_functions)
3638 && !NILP (Vrun_hooks)
3639 && (pos = make_number (IT_CHARPOS (*it)),
3640 prop = Fget_char_property (pos, Qfontified, Qnil),
3641 /* Ignore the special cased nil value always present at EOB since
3642 no amount of fontifying will be able to change it. */
3643 NILP (prop) && IT_CHARPOS (*it) < Z))
3644 {
3645 ptrdiff_t count = SPECPDL_INDEX ();
3646 Lisp_Object val;
3647 struct buffer *obuf = current_buffer;
3648 int begv = BEGV, zv = ZV;
3649 int old_clip_changed = current_buffer->clip_changed;
3650
3651 val = Vfontification_functions;
3652 specbind (Qfontification_functions, Qnil);
3653
3654 eassert (it->end_charpos == ZV);
3655
3656 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3657 safe_call1 (val, pos);
3658 else
3659 {
3660 Lisp_Object fns, fn;
3661 struct gcpro gcpro1, gcpro2;
3662
3663 fns = Qnil;
3664 GCPRO2 (val, fns);
3665
3666 for (; CONSP (val); val = XCDR (val))
3667 {
3668 fn = XCAR (val);
3669
3670 if (EQ (fn, Qt))
3671 {
3672 /* A value of t indicates this hook has a local
3673 binding; it means to run the global binding too.
3674 In a global value, t should not occur. If it
3675 does, we must ignore it to avoid an endless
3676 loop. */
3677 for (fns = Fdefault_value (Qfontification_functions);
3678 CONSP (fns);
3679 fns = XCDR (fns))
3680 {
3681 fn = XCAR (fns);
3682 if (!EQ (fn, Qt))
3683 safe_call1 (fn, pos);
3684 }
3685 }
3686 else
3687 safe_call1 (fn, pos);
3688 }
3689
3690 UNGCPRO;
3691 }
3692
3693 unbind_to (count, Qnil);
3694
3695 /* Fontification functions routinely call `save-restriction'.
3696 Normally, this tags clip_changed, which can confuse redisplay
3697 (see discussion in Bug#6671). Since we don't perform any
3698 special handling of fontification changes in the case where
3699 `save-restriction' isn't called, there's no point doing so in
3700 this case either. So, if the buffer's restrictions are
3701 actually left unchanged, reset clip_changed. */
3702 if (obuf == current_buffer)
3703 {
3704 if (begv == BEGV && zv == ZV)
3705 current_buffer->clip_changed = old_clip_changed;
3706 }
3707 /* There isn't much we can reasonably do to protect against
3708 misbehaving fontification, but here's a fig leaf. */
3709 else if (BUFFER_LIVE_P (obuf))
3710 set_buffer_internal_1 (obuf);
3711
3712 /* The fontification code may have added/removed text.
3713 It could do even a lot worse, but let's at least protect against
3714 the most obvious case where only the text past `pos' gets changed',
3715 as is/was done in grep.el where some escapes sequences are turned
3716 into face properties (bug#7876). */
3717 it->end_charpos = ZV;
3718
3719 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3720 something. This avoids an endless loop if they failed to
3721 fontify the text for which reason ever. */
3722 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3723 handled = HANDLED_RECOMPUTE_PROPS;
3724 }
3725
3726 return handled;
3727 }
3728
3729
3730 \f
3731 /***********************************************************************
3732 Faces
3733 ***********************************************************************/
3734
3735 /* Set up iterator IT from face properties at its current position.
3736 Called from handle_stop. */
3737
3738 static enum prop_handled
3739 handle_face_prop (struct it *it)
3740 {
3741 int new_face_id;
3742 ptrdiff_t next_stop;
3743
3744 if (!STRINGP (it->string))
3745 {
3746 new_face_id
3747 = face_at_buffer_position (it->w,
3748 IT_CHARPOS (*it),
3749 it->region_beg_charpos,
3750 it->region_end_charpos,
3751 &next_stop,
3752 (IT_CHARPOS (*it)
3753 + TEXT_PROP_DISTANCE_LIMIT),
3754 0, it->base_face_id);
3755
3756 /* Is this a start of a run of characters with box face?
3757 Caveat: this can be called for a freshly initialized
3758 iterator; face_id is -1 in this case. We know that the new
3759 face will not change until limit, i.e. if the new face has a
3760 box, all characters up to limit will have one. But, as
3761 usual, we don't know whether limit is really the end. */
3762 if (new_face_id != it->face_id)
3763 {
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3765
3766 /* If new face has a box but old face has not, this is
3767 the start of a run of characters with box, i.e. it has
3768 a shadow on the left side. The value of face_id of the
3769 iterator will be -1 if this is the initial call that gets
3770 the face. In this case, we have to look in front of IT's
3771 position and see whether there is a face != new_face_id. */
3772 it->start_of_box_run_p
3773 = (new_face->box != FACE_NO_BOX
3774 && (it->face_id >= 0
3775 || IT_CHARPOS (*it) == BEG
3776 || new_face_id != face_before_it_pos (it)));
3777 it->face_box_p = new_face->box != FACE_NO_BOX;
3778 }
3779 }
3780 else
3781 {
3782 int base_face_id;
3783 ptrdiff_t bufpos;
3784 int i;
3785 Lisp_Object from_overlay
3786 = (it->current.overlay_string_index >= 0
3787 ? it->string_overlays[it->current.overlay_string_index
3788 % OVERLAY_STRING_CHUNK_SIZE]
3789 : Qnil);
3790
3791 /* See if we got to this string directly or indirectly from
3792 an overlay property. That includes the before-string or
3793 after-string of an overlay, strings in display properties
3794 provided by an overlay, their text properties, etc.
3795
3796 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3797 if (! NILP (from_overlay))
3798 for (i = it->sp - 1; i >= 0; i--)
3799 {
3800 if (it->stack[i].current.overlay_string_index >= 0)
3801 from_overlay
3802 = it->string_overlays[it->stack[i].current.overlay_string_index
3803 % OVERLAY_STRING_CHUNK_SIZE];
3804 else if (! NILP (it->stack[i].from_overlay))
3805 from_overlay = it->stack[i].from_overlay;
3806
3807 if (!NILP (from_overlay))
3808 break;
3809 }
3810
3811 if (! NILP (from_overlay))
3812 {
3813 bufpos = IT_CHARPOS (*it);
3814 /* For a string from an overlay, the base face depends
3815 only on text properties and ignores overlays. */
3816 base_face_id
3817 = face_for_overlay_string (it->w,
3818 IT_CHARPOS (*it),
3819 it->region_beg_charpos,
3820 it->region_end_charpos,
3821 &next_stop,
3822 (IT_CHARPOS (*it)
3823 + TEXT_PROP_DISTANCE_LIMIT),
3824 0,
3825 from_overlay);
3826 }
3827 else
3828 {
3829 bufpos = 0;
3830
3831 /* For strings from a `display' property, use the face at
3832 IT's current buffer position as the base face to merge
3833 with, so that overlay strings appear in the same face as
3834 surrounding text, unless they specify their own
3835 faces. */
3836 base_face_id = it->string_from_prefix_prop_p
3837 ? DEFAULT_FACE_ID
3838 : underlying_face_id (it);
3839 }
3840
3841 new_face_id = face_at_string_position (it->w,
3842 it->string,
3843 IT_STRING_CHARPOS (*it),
3844 bufpos,
3845 it->region_beg_charpos,
3846 it->region_end_charpos,
3847 &next_stop,
3848 base_face_id, 0);
3849
3850 /* Is this a start of a run of characters with box? Caveat:
3851 this can be called for a freshly allocated iterator; face_id
3852 is -1 is this case. We know that the new face will not
3853 change until the next check pos, i.e. if the new face has a
3854 box, all characters up to that position will have a
3855 box. But, as usual, we don't know whether that position
3856 is really the end. */
3857 if (new_face_id != it->face_id)
3858 {
3859 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3860 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3861
3862 /* If new face has a box but old face hasn't, this is the
3863 start of a run of characters with box, i.e. it has a
3864 shadow on the left side. */
3865 it->start_of_box_run_p
3866 = new_face->box && (old_face == NULL || !old_face->box);
3867 it->face_box_p = new_face->box != FACE_NO_BOX;
3868 }
3869 }
3870
3871 it->face_id = new_face_id;
3872 return HANDLED_NORMALLY;
3873 }
3874
3875
3876 /* Return the ID of the face ``underlying'' IT's current position,
3877 which is in a string. If the iterator is associated with a
3878 buffer, return the face at IT's current buffer position.
3879 Otherwise, use the iterator's base_face_id. */
3880
3881 static int
3882 underlying_face_id (struct it *it)
3883 {
3884 int face_id = it->base_face_id, i;
3885
3886 eassert (STRINGP (it->string));
3887
3888 for (i = it->sp - 1; i >= 0; --i)
3889 if (NILP (it->stack[i].string))
3890 face_id = it->stack[i].face_id;
3891
3892 return face_id;
3893 }
3894
3895
3896 /* Compute the face one character before or after the current position
3897 of IT, in the visual order. BEFORE_P non-zero means get the face
3898 in front (to the left in L2R paragraphs, to the right in R2L
3899 paragraphs) of IT's screen position. Value is the ID of the face. */
3900
3901 static int
3902 face_before_or_after_it_pos (struct it *it, int before_p)
3903 {
3904 int face_id, limit;
3905 ptrdiff_t next_check_charpos;
3906 struct it it_copy;
3907 void *it_copy_data = NULL;
3908
3909 eassert (it->s == NULL);
3910
3911 if (STRINGP (it->string))
3912 {
3913 ptrdiff_t bufpos, charpos;
3914 int base_face_id;
3915
3916 /* No face change past the end of the string (for the case
3917 we are padding with spaces). No face change before the
3918 string start. */
3919 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3920 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3921 return it->face_id;
3922
3923 if (!it->bidi_p)
3924 {
3925 /* Set charpos to the position before or after IT's current
3926 position, in the logical order, which in the non-bidi
3927 case is the same as the visual order. */
3928 if (before_p)
3929 charpos = IT_STRING_CHARPOS (*it) - 1;
3930 else if (it->what == IT_COMPOSITION)
3931 /* For composition, we must check the character after the
3932 composition. */
3933 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3934 else
3935 charpos = IT_STRING_CHARPOS (*it) + 1;
3936 }
3937 else
3938 {
3939 if (before_p)
3940 {
3941 /* With bidi iteration, the character before the current
3942 in the visual order cannot be found by simple
3943 iteration, because "reverse" reordering is not
3944 supported. Instead, we need to use the move_it_*
3945 family of functions. */
3946 /* Ignore face changes before the first visible
3947 character on this display line. */
3948 if (it->current_x <= it->first_visible_x)
3949 return it->face_id;
3950 SAVE_IT (it_copy, *it, it_copy_data);
3951 /* Implementation note: Since move_it_in_display_line
3952 works in the iterator geometry, and thinks the first
3953 character is always the leftmost, even in R2L lines,
3954 we don't need to distinguish between the R2L and L2R
3955 cases here. */
3956 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3957 it_copy.current_x - 1, MOVE_TO_X);
3958 charpos = IT_STRING_CHARPOS (it_copy);
3959 RESTORE_IT (it, it, it_copy_data);
3960 }
3961 else
3962 {
3963 /* Set charpos to the string position of the character
3964 that comes after IT's current position in the visual
3965 order. */
3966 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3967
3968 it_copy = *it;
3969 while (n--)
3970 bidi_move_to_visually_next (&it_copy.bidi_it);
3971
3972 charpos = it_copy.bidi_it.charpos;
3973 }
3974 }
3975 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3976
3977 if (it->current.overlay_string_index >= 0)
3978 bufpos = IT_CHARPOS (*it);
3979 else
3980 bufpos = 0;
3981
3982 base_face_id = underlying_face_id (it);
3983
3984 /* Get the face for ASCII, or unibyte. */
3985 face_id = face_at_string_position (it->w,
3986 it->string,
3987 charpos,
3988 bufpos,
3989 it->region_beg_charpos,
3990 it->region_end_charpos,
3991 &next_check_charpos,
3992 base_face_id, 0);
3993
3994 /* Correct the face for charsets different from ASCII. Do it
3995 for the multibyte case only. The face returned above is
3996 suitable for unibyte text if IT->string is unibyte. */
3997 if (STRING_MULTIBYTE (it->string))
3998 {
3999 struct text_pos pos1 = string_pos (charpos, it->string);
4000 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4001 int c, len;
4002 struct face *face = FACE_FROM_ID (it->f, face_id);
4003
4004 c = string_char_and_length (p, &len);
4005 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4006 }
4007 }
4008 else
4009 {
4010 struct text_pos pos;
4011
4012 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4013 || (IT_CHARPOS (*it) <= BEGV && before_p))
4014 return it->face_id;
4015
4016 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4017 pos = it->current.pos;
4018
4019 if (!it->bidi_p)
4020 {
4021 if (before_p)
4022 DEC_TEXT_POS (pos, it->multibyte_p);
4023 else
4024 {
4025 if (it->what == IT_COMPOSITION)
4026 {
4027 /* For composition, we must check the position after
4028 the composition. */
4029 pos.charpos += it->cmp_it.nchars;
4030 pos.bytepos += it->len;
4031 }
4032 else
4033 INC_TEXT_POS (pos, it->multibyte_p);
4034 }
4035 }
4036 else
4037 {
4038 if (before_p)
4039 {
4040 /* With bidi iteration, the character before the current
4041 in the visual order cannot be found by simple
4042 iteration, because "reverse" reordering is not
4043 supported. Instead, we need to use the move_it_*
4044 family of functions. */
4045 /* Ignore face changes before the first visible
4046 character on this display line. */
4047 if (it->current_x <= it->first_visible_x)
4048 return it->face_id;
4049 SAVE_IT (it_copy, *it, it_copy_data);
4050 /* Implementation note: Since move_it_in_display_line
4051 works in the iterator geometry, and thinks the first
4052 character is always the leftmost, even in R2L lines,
4053 we don't need to distinguish between the R2L and L2R
4054 cases here. */
4055 move_it_in_display_line (&it_copy, ZV,
4056 it_copy.current_x - 1, MOVE_TO_X);
4057 pos = it_copy.current.pos;
4058 RESTORE_IT (it, it, it_copy_data);
4059 }
4060 else
4061 {
4062 /* Set charpos to the buffer position of the character
4063 that comes after IT's current position in the visual
4064 order. */
4065 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4066
4067 it_copy = *it;
4068 while (n--)
4069 bidi_move_to_visually_next (&it_copy.bidi_it);
4070
4071 SET_TEXT_POS (pos,
4072 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4073 }
4074 }
4075 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4076
4077 /* Determine face for CHARSET_ASCII, or unibyte. */
4078 face_id = face_at_buffer_position (it->w,
4079 CHARPOS (pos),
4080 it->region_beg_charpos,
4081 it->region_end_charpos,
4082 &next_check_charpos,
4083 limit, 0, -1);
4084
4085 /* Correct the face for charsets different from ASCII. Do it
4086 for the multibyte case only. The face returned above is
4087 suitable for unibyte text if current_buffer is unibyte. */
4088 if (it->multibyte_p)
4089 {
4090 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4091 struct face *face = FACE_FROM_ID (it->f, face_id);
4092 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4093 }
4094 }
4095
4096 return face_id;
4097 }
4098
4099
4100 \f
4101 /***********************************************************************
4102 Invisible text
4103 ***********************************************************************/
4104
4105 /* Set up iterator IT from invisible properties at its current
4106 position. Called from handle_stop. */
4107
4108 static enum prop_handled
4109 handle_invisible_prop (struct it *it)
4110 {
4111 enum prop_handled handled = HANDLED_NORMALLY;
4112 int invis_p;
4113 Lisp_Object prop;
4114
4115 if (STRINGP (it->string))
4116 {
4117 Lisp_Object end_charpos, limit, charpos;
4118
4119 /* Get the value of the invisible text property at the
4120 current position. Value will be nil if there is no such
4121 property. */
4122 charpos = make_number (IT_STRING_CHARPOS (*it));
4123 prop = Fget_text_property (charpos, Qinvisible, it->string);
4124 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4125
4126 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4127 {
4128 /* Record whether we have to display an ellipsis for the
4129 invisible text. */
4130 int display_ellipsis_p = (invis_p == 2);
4131 ptrdiff_t len, endpos;
4132
4133 handled = HANDLED_RECOMPUTE_PROPS;
4134
4135 /* Get the position at which the next visible text can be
4136 found in IT->string, if any. */
4137 endpos = len = SCHARS (it->string);
4138 XSETINT (limit, len);
4139 do
4140 {
4141 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4142 it->string, limit);
4143 if (INTEGERP (end_charpos))
4144 {
4145 endpos = XFASTINT (end_charpos);
4146 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4147 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4148 if (invis_p == 2)
4149 display_ellipsis_p = 1;
4150 }
4151 }
4152 while (invis_p && endpos < len);
4153
4154 if (display_ellipsis_p)
4155 it->ellipsis_p = 1;
4156
4157 if (endpos < len)
4158 {
4159 /* Text at END_CHARPOS is visible. Move IT there. */
4160 struct text_pos old;
4161 ptrdiff_t oldpos;
4162
4163 old = it->current.string_pos;
4164 oldpos = CHARPOS (old);
4165 if (it->bidi_p)
4166 {
4167 if (it->bidi_it.first_elt
4168 && it->bidi_it.charpos < SCHARS (it->string))
4169 bidi_paragraph_init (it->paragraph_embedding,
4170 &it->bidi_it, 1);
4171 /* Bidi-iterate out of the invisible text. */
4172 do
4173 {
4174 bidi_move_to_visually_next (&it->bidi_it);
4175 }
4176 while (oldpos <= it->bidi_it.charpos
4177 && it->bidi_it.charpos < endpos);
4178
4179 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4180 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4181 if (IT_CHARPOS (*it) >= endpos)
4182 it->prev_stop = endpos;
4183 }
4184 else
4185 {
4186 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4187 compute_string_pos (&it->current.string_pos, old, it->string);
4188 }
4189 }
4190 else
4191 {
4192 /* The rest of the string is invisible. If this is an
4193 overlay string, proceed with the next overlay string
4194 or whatever comes and return a character from there. */
4195 if (it->current.overlay_string_index >= 0
4196 && !display_ellipsis_p)
4197 {
4198 next_overlay_string (it);
4199 /* Don't check for overlay strings when we just
4200 finished processing them. */
4201 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4202 }
4203 else
4204 {
4205 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4206 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4207 }
4208 }
4209 }
4210 }
4211 else
4212 {
4213 ptrdiff_t newpos, next_stop, start_charpos, tem;
4214 Lisp_Object pos, overlay;
4215
4216 /* First of all, is there invisible text at this position? */
4217 tem = start_charpos = IT_CHARPOS (*it);
4218 pos = make_number (tem);
4219 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4220 &overlay);
4221 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4222
4223 /* If we are on invisible text, skip over it. */
4224 if (invis_p && start_charpos < it->end_charpos)
4225 {
4226 /* Record whether we have to display an ellipsis for the
4227 invisible text. */
4228 int display_ellipsis_p = invis_p == 2;
4229
4230 handled = HANDLED_RECOMPUTE_PROPS;
4231
4232 /* Loop skipping over invisible text. The loop is left at
4233 ZV or with IT on the first char being visible again. */
4234 do
4235 {
4236 /* Try to skip some invisible text. Return value is the
4237 position reached which can be equal to where we start
4238 if there is nothing invisible there. This skips both
4239 over invisible text properties and overlays with
4240 invisible property. */
4241 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4242
4243 /* If we skipped nothing at all we weren't at invisible
4244 text in the first place. If everything to the end of
4245 the buffer was skipped, end the loop. */
4246 if (newpos == tem || newpos >= ZV)
4247 invis_p = 0;
4248 else
4249 {
4250 /* We skipped some characters but not necessarily
4251 all there are. Check if we ended up on visible
4252 text. Fget_char_property returns the property of
4253 the char before the given position, i.e. if we
4254 get invis_p = 0, this means that the char at
4255 newpos is visible. */
4256 pos = make_number (newpos);
4257 prop = Fget_char_property (pos, Qinvisible, it->window);
4258 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4259 }
4260
4261 /* If we ended up on invisible text, proceed to
4262 skip starting with next_stop. */
4263 if (invis_p)
4264 tem = next_stop;
4265
4266 /* If there are adjacent invisible texts, don't lose the
4267 second one's ellipsis. */
4268 if (invis_p == 2)
4269 display_ellipsis_p = 1;
4270 }
4271 while (invis_p);
4272
4273 /* The position newpos is now either ZV or on visible text. */
4274 if (it->bidi_p)
4275 {
4276 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4277 int on_newline =
4278 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4279 int after_newline =
4280 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4281
4282 /* If the invisible text ends on a newline or on a
4283 character after a newline, we can avoid the costly,
4284 character by character, bidi iteration to NEWPOS, and
4285 instead simply reseat the iterator there. That's
4286 because all bidi reordering information is tossed at
4287 the newline. This is a big win for modes that hide
4288 complete lines, like Outline, Org, etc. */
4289 if (on_newline || after_newline)
4290 {
4291 struct text_pos tpos;
4292 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4293
4294 SET_TEXT_POS (tpos, newpos, bpos);
4295 reseat_1 (it, tpos, 0);
4296 /* If we reseat on a newline/ZV, we need to prep the
4297 bidi iterator for advancing to the next character
4298 after the newline/EOB, keeping the current paragraph
4299 direction (so that PRODUCE_GLYPHS does TRT wrt
4300 prepending/appending glyphs to a glyph row). */
4301 if (on_newline)
4302 {
4303 it->bidi_it.first_elt = 0;
4304 it->bidi_it.paragraph_dir = pdir;
4305 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4306 it->bidi_it.nchars = 1;
4307 it->bidi_it.ch_len = 1;
4308 }
4309 }
4310 else /* Must use the slow method. */
4311 {
4312 /* With bidi iteration, the region of invisible text
4313 could start and/or end in the middle of a
4314 non-base embedding level. Therefore, we need to
4315 skip invisible text using the bidi iterator,
4316 starting at IT's current position, until we find
4317 ourselves outside of the invisible text.
4318 Skipping invisible text _after_ bidi iteration
4319 avoids affecting the visual order of the
4320 displayed text when invisible properties are
4321 added or removed. */
4322 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4323 {
4324 /* If we were `reseat'ed to a new paragraph,
4325 determine the paragraph base direction. We
4326 need to do it now because
4327 next_element_from_buffer may not have a
4328 chance to do it, if we are going to skip any
4329 text at the beginning, which resets the
4330 FIRST_ELT flag. */
4331 bidi_paragraph_init (it->paragraph_embedding,
4332 &it->bidi_it, 1);
4333 }
4334 do
4335 {
4336 bidi_move_to_visually_next (&it->bidi_it);
4337 }
4338 while (it->stop_charpos <= it->bidi_it.charpos
4339 && it->bidi_it.charpos < newpos);
4340 IT_CHARPOS (*it) = it->bidi_it.charpos;
4341 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4342 /* If we overstepped NEWPOS, record its position in
4343 the iterator, so that we skip invisible text if
4344 later the bidi iteration lands us in the
4345 invisible region again. */
4346 if (IT_CHARPOS (*it) >= newpos)
4347 it->prev_stop = newpos;
4348 }
4349 }
4350 else
4351 {
4352 IT_CHARPOS (*it) = newpos;
4353 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4354 }
4355
4356 /* If there are before-strings at the start of invisible
4357 text, and the text is invisible because of a text
4358 property, arrange to show before-strings because 20.x did
4359 it that way. (If the text is invisible because of an
4360 overlay property instead of a text property, this is
4361 already handled in the overlay code.) */
4362 if (NILP (overlay)
4363 && get_overlay_strings (it, it->stop_charpos))
4364 {
4365 handled = HANDLED_RECOMPUTE_PROPS;
4366 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4367 }
4368 else if (display_ellipsis_p)
4369 {
4370 /* Make sure that the glyphs of the ellipsis will get
4371 correct `charpos' values. If we would not update
4372 it->position here, the glyphs would belong to the
4373 last visible character _before_ the invisible
4374 text, which confuses `set_cursor_from_row'.
4375
4376 We use the last invisible position instead of the
4377 first because this way the cursor is always drawn on
4378 the first "." of the ellipsis, whenever PT is inside
4379 the invisible text. Otherwise the cursor would be
4380 placed _after_ the ellipsis when the point is after the
4381 first invisible character. */
4382 if (!STRINGP (it->object))
4383 {
4384 it->position.charpos = newpos - 1;
4385 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4386 }
4387 it->ellipsis_p = 1;
4388 /* Let the ellipsis display before
4389 considering any properties of the following char.
4390 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4391 handled = HANDLED_RETURN;
4392 }
4393 }
4394 }
4395
4396 return handled;
4397 }
4398
4399
4400 /* Make iterator IT return `...' next.
4401 Replaces LEN characters from buffer. */
4402
4403 static void
4404 setup_for_ellipsis (struct it *it, int len)
4405 {
4406 /* Use the display table definition for `...'. Invalid glyphs
4407 will be handled by the method returning elements from dpvec. */
4408 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4409 {
4410 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4411 it->dpvec = v->contents;
4412 it->dpend = v->contents + v->header.size;
4413 }
4414 else
4415 {
4416 /* Default `...'. */
4417 it->dpvec = default_invis_vector;
4418 it->dpend = default_invis_vector + 3;
4419 }
4420
4421 it->dpvec_char_len = len;
4422 it->current.dpvec_index = 0;
4423 it->dpvec_face_id = -1;
4424
4425 /* Remember the current face id in case glyphs specify faces.
4426 IT's face is restored in set_iterator_to_next.
4427 saved_face_id was set to preceding char's face in handle_stop. */
4428 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4429 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4430
4431 it->method = GET_FROM_DISPLAY_VECTOR;
4432 it->ellipsis_p = 1;
4433 }
4434
4435
4436 \f
4437 /***********************************************************************
4438 'display' property
4439 ***********************************************************************/
4440
4441 /* Set up iterator IT from `display' property at its current position.
4442 Called from handle_stop.
4443 We return HANDLED_RETURN if some part of the display property
4444 overrides the display of the buffer text itself.
4445 Otherwise we return HANDLED_NORMALLY. */
4446
4447 static enum prop_handled
4448 handle_display_prop (struct it *it)
4449 {
4450 Lisp_Object propval, object, overlay;
4451 struct text_pos *position;
4452 ptrdiff_t bufpos;
4453 /* Nonzero if some property replaces the display of the text itself. */
4454 int display_replaced_p = 0;
4455
4456 if (STRINGP (it->string))
4457 {
4458 object = it->string;
4459 position = &it->current.string_pos;
4460 bufpos = CHARPOS (it->current.pos);
4461 }
4462 else
4463 {
4464 XSETWINDOW (object, it->w);
4465 position = &it->current.pos;
4466 bufpos = CHARPOS (*position);
4467 }
4468
4469 /* Reset those iterator values set from display property values. */
4470 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4471 it->space_width = Qnil;
4472 it->font_height = Qnil;
4473 it->voffset = 0;
4474
4475 /* We don't support recursive `display' properties, i.e. string
4476 values that have a string `display' property, that have a string
4477 `display' property etc. */
4478 if (!it->string_from_display_prop_p)
4479 it->area = TEXT_AREA;
4480
4481 propval = get_char_property_and_overlay (make_number (position->charpos),
4482 Qdisplay, object, &overlay);
4483 if (NILP (propval))
4484 return HANDLED_NORMALLY;
4485 /* Now OVERLAY is the overlay that gave us this property, or nil
4486 if it was a text property. */
4487
4488 if (!STRINGP (it->string))
4489 object = it->w->buffer;
4490
4491 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4492 position, bufpos,
4493 FRAME_WINDOW_P (it->f));
4494
4495 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4496 }
4497
4498 /* Subroutine of handle_display_prop. Returns non-zero if the display
4499 specification in SPEC is a replacing specification, i.e. it would
4500 replace the text covered by `display' property with something else,
4501 such as an image or a display string. If SPEC includes any kind or
4502 `(space ...) specification, the value is 2; this is used by
4503 compute_display_string_pos, which see.
4504
4505 See handle_single_display_spec for documentation of arguments.
4506 frame_window_p is non-zero if the window being redisplayed is on a
4507 GUI frame; this argument is used only if IT is NULL, see below.
4508
4509 IT can be NULL, if this is called by the bidi reordering code
4510 through compute_display_string_pos, which see. In that case, this
4511 function only examines SPEC, but does not otherwise "handle" it, in
4512 the sense that it doesn't set up members of IT from the display
4513 spec. */
4514 static int
4515 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4516 Lisp_Object overlay, struct text_pos *position,
4517 ptrdiff_t bufpos, int frame_window_p)
4518 {
4519 int replacing_p = 0;
4520 int rv;
4521
4522 if (CONSP (spec)
4523 /* Simple specifications. */
4524 && !EQ (XCAR (spec), Qimage)
4525 && !EQ (XCAR (spec), Qspace)
4526 && !EQ (XCAR (spec), Qwhen)
4527 && !EQ (XCAR (spec), Qslice)
4528 && !EQ (XCAR (spec), Qspace_width)
4529 && !EQ (XCAR (spec), Qheight)
4530 && !EQ (XCAR (spec), Qraise)
4531 /* Marginal area specifications. */
4532 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4533 && !EQ (XCAR (spec), Qleft_fringe)
4534 && !EQ (XCAR (spec), Qright_fringe)
4535 && !NILP (XCAR (spec)))
4536 {
4537 for (; CONSP (spec); spec = XCDR (spec))
4538 {
4539 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4540 overlay, position, bufpos,
4541 replacing_p, frame_window_p)))
4542 {
4543 replacing_p = rv;
4544 /* If some text in a string is replaced, `position' no
4545 longer points to the position of `object'. */
4546 if (!it || STRINGP (object))
4547 break;
4548 }
4549 }
4550 }
4551 else if (VECTORP (spec))
4552 {
4553 ptrdiff_t i;
4554 for (i = 0; i < ASIZE (spec); ++i)
4555 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4556 overlay, position, bufpos,
4557 replacing_p, frame_window_p)))
4558 {
4559 replacing_p = rv;
4560 /* If some text in a string is replaced, `position' no
4561 longer points to the position of `object'. */
4562 if (!it || STRINGP (object))
4563 break;
4564 }
4565 }
4566 else
4567 {
4568 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4569 position, bufpos, 0,
4570 frame_window_p)))
4571 replacing_p = rv;
4572 }
4573
4574 return replacing_p;
4575 }
4576
4577 /* Value is the position of the end of the `display' property starting
4578 at START_POS in OBJECT. */
4579
4580 static struct text_pos
4581 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4582 {
4583 Lisp_Object end;
4584 struct text_pos end_pos;
4585
4586 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4587 Qdisplay, object, Qnil);
4588 CHARPOS (end_pos) = XFASTINT (end);
4589 if (STRINGP (object))
4590 compute_string_pos (&end_pos, start_pos, it->string);
4591 else
4592 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4593
4594 return end_pos;
4595 }
4596
4597
4598 /* Set up IT from a single `display' property specification SPEC. OBJECT
4599 is the object in which the `display' property was found. *POSITION
4600 is the position in OBJECT at which the `display' property was found.
4601 BUFPOS is the buffer position of OBJECT (different from POSITION if
4602 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4603 previously saw a display specification which already replaced text
4604 display with something else, for example an image; we ignore such
4605 properties after the first one has been processed.
4606
4607 OVERLAY is the overlay this `display' property came from,
4608 or nil if it was a text property.
4609
4610 If SPEC is a `space' or `image' specification, and in some other
4611 cases too, set *POSITION to the position where the `display'
4612 property ends.
4613
4614 If IT is NULL, only examine the property specification in SPEC, but
4615 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4616 is intended to be displayed in a window on a GUI frame.
4617
4618 Value is non-zero if something was found which replaces the display
4619 of buffer or string text. */
4620
4621 static int
4622 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4623 Lisp_Object overlay, struct text_pos *position,
4624 ptrdiff_t bufpos, int display_replaced_p,
4625 int frame_window_p)
4626 {
4627 Lisp_Object form;
4628 Lisp_Object location, value;
4629 struct text_pos start_pos = *position;
4630 int valid_p;
4631
4632 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4633 If the result is non-nil, use VALUE instead of SPEC. */
4634 form = Qt;
4635 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4636 {
4637 spec = XCDR (spec);
4638 if (!CONSP (spec))
4639 return 0;
4640 form = XCAR (spec);
4641 spec = XCDR (spec);
4642 }
4643
4644 if (!NILP (form) && !EQ (form, Qt))
4645 {
4646 ptrdiff_t count = SPECPDL_INDEX ();
4647 struct gcpro gcpro1;
4648
4649 /* Bind `object' to the object having the `display' property, a
4650 buffer or string. Bind `position' to the position in the
4651 object where the property was found, and `buffer-position'
4652 to the current position in the buffer. */
4653
4654 if (NILP (object))
4655 XSETBUFFER (object, current_buffer);
4656 specbind (Qobject, object);
4657 specbind (Qposition, make_number (CHARPOS (*position)));
4658 specbind (Qbuffer_position, make_number (bufpos));
4659 GCPRO1 (form);
4660 form = safe_eval (form);
4661 UNGCPRO;
4662 unbind_to (count, Qnil);
4663 }
4664
4665 if (NILP (form))
4666 return 0;
4667
4668 /* Handle `(height HEIGHT)' specifications. */
4669 if (CONSP (spec)
4670 && EQ (XCAR (spec), Qheight)
4671 && CONSP (XCDR (spec)))
4672 {
4673 if (it)
4674 {
4675 if (!FRAME_WINDOW_P (it->f))
4676 return 0;
4677
4678 it->font_height = XCAR (XCDR (spec));
4679 if (!NILP (it->font_height))
4680 {
4681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4682 int new_height = -1;
4683
4684 if (CONSP (it->font_height)
4685 && (EQ (XCAR (it->font_height), Qplus)
4686 || EQ (XCAR (it->font_height), Qminus))
4687 && CONSP (XCDR (it->font_height))
4688 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4689 {
4690 /* `(+ N)' or `(- N)' where N is an integer. */
4691 int steps = XINT (XCAR (XCDR (it->font_height)));
4692 if (EQ (XCAR (it->font_height), Qplus))
4693 steps = - steps;
4694 it->face_id = smaller_face (it->f, it->face_id, steps);
4695 }
4696 else if (FUNCTIONP (it->font_height))
4697 {
4698 /* Call function with current height as argument.
4699 Value is the new height. */
4700 Lisp_Object height;
4701 height = safe_call1 (it->font_height,
4702 face->lface[LFACE_HEIGHT_INDEX]);
4703 if (NUMBERP (height))
4704 new_height = XFLOATINT (height);
4705 }
4706 else if (NUMBERP (it->font_height))
4707 {
4708 /* Value is a multiple of the canonical char height. */
4709 struct face *f;
4710
4711 f = FACE_FROM_ID (it->f,
4712 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4713 new_height = (XFLOATINT (it->font_height)
4714 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4715 }
4716 else
4717 {
4718 /* Evaluate IT->font_height with `height' bound to the
4719 current specified height to get the new height. */
4720 ptrdiff_t count = SPECPDL_INDEX ();
4721
4722 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4723 value = safe_eval (it->font_height);
4724 unbind_to (count, Qnil);
4725
4726 if (NUMBERP (value))
4727 new_height = XFLOATINT (value);
4728 }
4729
4730 if (new_height > 0)
4731 it->face_id = face_with_height (it->f, it->face_id, new_height);
4732 }
4733 }
4734
4735 return 0;
4736 }
4737
4738 /* Handle `(space-width WIDTH)'. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qspace_width)
4741 && CONSP (XCDR (spec)))
4742 {
4743 if (it)
4744 {
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4747
4748 value = XCAR (XCDR (spec));
4749 if (NUMBERP (value) && XFLOATINT (value) > 0)
4750 it->space_width = value;
4751 }
4752
4753 return 0;
4754 }
4755
4756 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4757 if (CONSP (spec)
4758 && EQ (XCAR (spec), Qslice))
4759 {
4760 Lisp_Object tem;
4761
4762 if (it)
4763 {
4764 if (!FRAME_WINDOW_P (it->f))
4765 return 0;
4766
4767 if (tem = XCDR (spec), CONSP (tem))
4768 {
4769 it->slice.x = XCAR (tem);
4770 if (tem = XCDR (tem), CONSP (tem))
4771 {
4772 it->slice.y = XCAR (tem);
4773 if (tem = XCDR (tem), CONSP (tem))
4774 {
4775 it->slice.width = XCAR (tem);
4776 if (tem = XCDR (tem), CONSP (tem))
4777 it->slice.height = XCAR (tem);
4778 }
4779 }
4780 }
4781 }
4782
4783 return 0;
4784 }
4785
4786 /* Handle `(raise FACTOR)'. */
4787 if (CONSP (spec)
4788 && EQ (XCAR (spec), Qraise)
4789 && CONSP (XCDR (spec)))
4790 {
4791 if (it)
4792 {
4793 if (!FRAME_WINDOW_P (it->f))
4794 return 0;
4795
4796 #ifdef HAVE_WINDOW_SYSTEM
4797 value = XCAR (XCDR (spec));
4798 if (NUMBERP (value))
4799 {
4800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4801 it->voffset = - (XFLOATINT (value)
4802 * (FONT_HEIGHT (face->font)));
4803 }
4804 #endif /* HAVE_WINDOW_SYSTEM */
4805 }
4806
4807 return 0;
4808 }
4809
4810 /* Don't handle the other kinds of display specifications
4811 inside a string that we got from a `display' property. */
4812 if (it && it->string_from_display_prop_p)
4813 return 0;
4814
4815 /* Characters having this form of property are not displayed, so
4816 we have to find the end of the property. */
4817 if (it)
4818 {
4819 start_pos = *position;
4820 *position = display_prop_end (it, object, start_pos);
4821 }
4822 value = Qnil;
4823
4824 /* Stop the scan at that end position--we assume that all
4825 text properties change there. */
4826 if (it)
4827 it->stop_charpos = position->charpos;
4828
4829 /* Handle `(left-fringe BITMAP [FACE])'
4830 and `(right-fringe BITMAP [FACE])'. */
4831 if (CONSP (spec)
4832 && (EQ (XCAR (spec), Qleft_fringe)
4833 || EQ (XCAR (spec), Qright_fringe))
4834 && CONSP (XCDR (spec)))
4835 {
4836 int fringe_bitmap;
4837
4838 if (it)
4839 {
4840 if (!FRAME_WINDOW_P (it->f))
4841 /* If we return here, POSITION has been advanced
4842 across the text with this property. */
4843 {
4844 /* Synchronize the bidi iterator with POSITION. This is
4845 needed because we are not going to push the iterator
4846 on behalf of this display property, so there will be
4847 no pop_it call to do this synchronization for us. */
4848 if (it->bidi_p)
4849 {
4850 it->position = *position;
4851 iterate_out_of_display_property (it);
4852 *position = it->position;
4853 }
4854 return 1;
4855 }
4856 }
4857 else if (!frame_window_p)
4858 return 1;
4859
4860 #ifdef HAVE_WINDOW_SYSTEM
4861 value = XCAR (XCDR (spec));
4862 if (!SYMBOLP (value)
4863 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4864 /* If we return here, POSITION has been advanced
4865 across the text with this property. */
4866 {
4867 if (it && it->bidi_p)
4868 {
4869 it->position = *position;
4870 iterate_out_of_display_property (it);
4871 *position = it->position;
4872 }
4873 return 1;
4874 }
4875
4876 if (it)
4877 {
4878 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4879
4880 if (CONSP (XCDR (XCDR (spec))))
4881 {
4882 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4883 int face_id2 = lookup_derived_face (it->f, face_name,
4884 FRINGE_FACE_ID, 0);
4885 if (face_id2 >= 0)
4886 face_id = face_id2;
4887 }
4888
4889 /* Save current settings of IT so that we can restore them
4890 when we are finished with the glyph property value. */
4891 push_it (it, position);
4892
4893 it->area = TEXT_AREA;
4894 it->what = IT_IMAGE;
4895 it->image_id = -1; /* no image */
4896 it->position = start_pos;
4897 it->object = NILP (object) ? it->w->buffer : object;
4898 it->method = GET_FROM_IMAGE;
4899 it->from_overlay = Qnil;
4900 it->face_id = face_id;
4901 it->from_disp_prop_p = 1;
4902
4903 /* Say that we haven't consumed the characters with
4904 `display' property yet. The call to pop_it in
4905 set_iterator_to_next will clean this up. */
4906 *position = start_pos;
4907
4908 if (EQ (XCAR (spec), Qleft_fringe))
4909 {
4910 it->left_user_fringe_bitmap = fringe_bitmap;
4911 it->left_user_fringe_face_id = face_id;
4912 }
4913 else
4914 {
4915 it->right_user_fringe_bitmap = fringe_bitmap;
4916 it->right_user_fringe_face_id = face_id;
4917 }
4918 }
4919 #endif /* HAVE_WINDOW_SYSTEM */
4920 return 1;
4921 }
4922
4923 /* Prepare to handle `((margin left-margin) ...)',
4924 `((margin right-margin) ...)' and `((margin nil) ...)'
4925 prefixes for display specifications. */
4926 location = Qunbound;
4927 if (CONSP (spec) && CONSP (XCAR (spec)))
4928 {
4929 Lisp_Object tem;
4930
4931 value = XCDR (spec);
4932 if (CONSP (value))
4933 value = XCAR (value);
4934
4935 tem = XCAR (spec);
4936 if (EQ (XCAR (tem), Qmargin)
4937 && (tem = XCDR (tem),
4938 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4939 (NILP (tem)
4940 || EQ (tem, Qleft_margin)
4941 || EQ (tem, Qright_margin))))
4942 location = tem;
4943 }
4944
4945 if (EQ (location, Qunbound))
4946 {
4947 location = Qnil;
4948 value = spec;
4949 }
4950
4951 /* After this point, VALUE is the property after any
4952 margin prefix has been stripped. It must be a string,
4953 an image specification, or `(space ...)'.
4954
4955 LOCATION specifies where to display: `left-margin',
4956 `right-margin' or nil. */
4957
4958 valid_p = (STRINGP (value)
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4961 && valid_image_p (value))
4962 #endif /* not HAVE_WINDOW_SYSTEM */
4963 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4964
4965 if (valid_p && !display_replaced_p)
4966 {
4967 int retval = 1;
4968
4969 if (!it)
4970 {
4971 /* Callers need to know whether the display spec is any kind
4972 of `(space ...)' spec that is about to affect text-area
4973 display. */
4974 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4975 retval = 2;
4976 return retval;
4977 }
4978
4979 /* Save current settings of IT so that we can restore them
4980 when we are finished with the glyph property value. */
4981 push_it (it, position);
4982 it->from_overlay = overlay;
4983 it->from_disp_prop_p = 1;
4984
4985 if (NILP (location))
4986 it->area = TEXT_AREA;
4987 else if (EQ (location, Qleft_margin))
4988 it->area = LEFT_MARGIN_AREA;
4989 else
4990 it->area = RIGHT_MARGIN_AREA;
4991
4992 if (STRINGP (value))
4993 {
4994 it->string = value;
4995 it->multibyte_p = STRING_MULTIBYTE (it->string);
4996 it->current.overlay_string_index = -1;
4997 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4998 it->end_charpos = it->string_nchars = SCHARS (it->string);
4999 it->method = GET_FROM_STRING;
5000 it->stop_charpos = 0;
5001 it->prev_stop = 0;
5002 it->base_level_stop = 0;
5003 it->string_from_display_prop_p = 1;
5004 /* Say that we haven't consumed the characters with
5005 `display' property yet. The call to pop_it in
5006 set_iterator_to_next will clean this up. */
5007 if (BUFFERP (object))
5008 *position = start_pos;
5009
5010 /* Force paragraph direction to be that of the parent
5011 object. If the parent object's paragraph direction is
5012 not yet determined, default to L2R. */
5013 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5014 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5015 else
5016 it->paragraph_embedding = L2R;
5017
5018 /* Set up the bidi iterator for this display string. */
5019 if (it->bidi_p)
5020 {
5021 it->bidi_it.string.lstring = it->string;
5022 it->bidi_it.string.s = NULL;
5023 it->bidi_it.string.schars = it->end_charpos;
5024 it->bidi_it.string.bufpos = bufpos;
5025 it->bidi_it.string.from_disp_str = 1;
5026 it->bidi_it.string.unibyte = !it->multibyte_p;
5027 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5028 }
5029 }
5030 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5031 {
5032 it->method = GET_FROM_STRETCH;
5033 it->object = value;
5034 *position = it->position = start_pos;
5035 retval = 1 + (it->area == TEXT_AREA);
5036 }
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 else
5039 {
5040 it->what = IT_IMAGE;
5041 it->image_id = lookup_image (it->f, value);
5042 it->position = start_pos;
5043 it->object = NILP (object) ? it->w->buffer : object;
5044 it->method = GET_FROM_IMAGE;
5045
5046 /* Say that we haven't consumed the characters with
5047 `display' property yet. The call to pop_it in
5048 set_iterator_to_next will clean this up. */
5049 *position = start_pos;
5050 }
5051 #endif /* HAVE_WINDOW_SYSTEM */
5052
5053 return retval;
5054 }
5055
5056 /* Invalid property or property not supported. Restore
5057 POSITION to what it was before. */
5058 *position = start_pos;
5059 return 0;
5060 }
5061
5062 /* Check if PROP is a display property value whose text should be
5063 treated as intangible. OVERLAY is the overlay from which PROP
5064 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5065 specify the buffer position covered by PROP. */
5066
5067 int
5068 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5069 ptrdiff_t charpos, ptrdiff_t bytepos)
5070 {
5071 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5072 struct text_pos position;
5073
5074 SET_TEXT_POS (position, charpos, bytepos);
5075 return handle_display_spec (NULL, prop, Qnil, overlay,
5076 &position, charpos, frame_window_p);
5077 }
5078
5079
5080 /* Return 1 if PROP is a display sub-property value containing STRING.
5081
5082 Implementation note: this and the following function are really
5083 special cases of handle_display_spec and
5084 handle_single_display_spec, and should ideally use the same code.
5085 Until they do, these two pairs must be consistent and must be
5086 modified in sync. */
5087
5088 static int
5089 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5090 {
5091 if (EQ (string, prop))
5092 return 1;
5093
5094 /* Skip over `when FORM'. */
5095 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5096 {
5097 prop = XCDR (prop);
5098 if (!CONSP (prop))
5099 return 0;
5100 /* Actually, the condition following `when' should be eval'ed,
5101 like handle_single_display_spec does, and we should return
5102 zero if it evaluates to nil. However, this function is
5103 called only when the buffer was already displayed and some
5104 glyph in the glyph matrix was found to come from a display
5105 string. Therefore, the condition was already evaluated, and
5106 the result was non-nil, otherwise the display string wouldn't
5107 have been displayed and we would have never been called for
5108 this property. Thus, we can skip the evaluation and assume
5109 its result is non-nil. */
5110 prop = XCDR (prop);
5111 }
5112
5113 if (CONSP (prop))
5114 /* Skip over `margin LOCATION'. */
5115 if (EQ (XCAR (prop), Qmargin))
5116 {
5117 prop = XCDR (prop);
5118 if (!CONSP (prop))
5119 return 0;
5120
5121 prop = XCDR (prop);
5122 if (!CONSP (prop))
5123 return 0;
5124 }
5125
5126 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5127 }
5128
5129
5130 /* Return 1 if STRING appears in the `display' property PROP. */
5131
5132 static int
5133 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5134 {
5135 if (CONSP (prop)
5136 && !EQ (XCAR (prop), Qwhen)
5137 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5138 {
5139 /* A list of sub-properties. */
5140 while (CONSP (prop))
5141 {
5142 if (single_display_spec_string_p (XCAR (prop), string))
5143 return 1;
5144 prop = XCDR (prop);
5145 }
5146 }
5147 else if (VECTORP (prop))
5148 {
5149 /* A vector of sub-properties. */
5150 ptrdiff_t i;
5151 for (i = 0; i < ASIZE (prop); ++i)
5152 if (single_display_spec_string_p (AREF (prop, i), string))
5153 return 1;
5154 }
5155 else
5156 return single_display_spec_string_p (prop, string);
5157
5158 return 0;
5159 }
5160
5161 /* Look for STRING in overlays and text properties in the current
5162 buffer, between character positions FROM and TO (excluding TO).
5163 BACK_P non-zero means look back (in this case, TO is supposed to be
5164 less than FROM).
5165 Value is the first character position where STRING was found, or
5166 zero if it wasn't found before hitting TO.
5167
5168 This function may only use code that doesn't eval because it is
5169 called asynchronously from note_mouse_highlight. */
5170
5171 static ptrdiff_t
5172 string_buffer_position_lim (Lisp_Object string,
5173 ptrdiff_t from, ptrdiff_t to, int back_p)
5174 {
5175 Lisp_Object limit, prop, pos;
5176 int found = 0;
5177
5178 pos = make_number (max (from, BEGV));
5179
5180 if (!back_p) /* looking forward */
5181 {
5182 limit = make_number (min (to, ZV));
5183 while (!found && !EQ (pos, limit))
5184 {
5185 prop = Fget_char_property (pos, Qdisplay, Qnil);
5186 if (!NILP (prop) && display_prop_string_p (prop, string))
5187 found = 1;
5188 else
5189 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5190 limit);
5191 }
5192 }
5193 else /* looking back */
5194 {
5195 limit = make_number (max (to, BEGV));
5196 while (!found && !EQ (pos, limit))
5197 {
5198 prop = Fget_char_property (pos, Qdisplay, Qnil);
5199 if (!NILP (prop) && display_prop_string_p (prop, string))
5200 found = 1;
5201 else
5202 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5203 limit);
5204 }
5205 }
5206
5207 return found ? XINT (pos) : 0;
5208 }
5209
5210 /* Determine which buffer position in current buffer STRING comes from.
5211 AROUND_CHARPOS is an approximate position where it could come from.
5212 Value is the buffer position or 0 if it couldn't be determined.
5213
5214 This function is necessary because we don't record buffer positions
5215 in glyphs generated from strings (to keep struct glyph small).
5216 This function may only use code that doesn't eval because it is
5217 called asynchronously from note_mouse_highlight. */
5218
5219 static ptrdiff_t
5220 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5221 {
5222 const int MAX_DISTANCE = 1000;
5223 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5224 around_charpos + MAX_DISTANCE,
5225 0);
5226
5227 if (!found)
5228 found = string_buffer_position_lim (string, around_charpos,
5229 around_charpos - MAX_DISTANCE, 1);
5230 return found;
5231 }
5232
5233
5234 \f
5235 /***********************************************************************
5236 `composition' property
5237 ***********************************************************************/
5238
5239 /* Set up iterator IT from `composition' property at its current
5240 position. Called from handle_stop. */
5241
5242 static enum prop_handled
5243 handle_composition_prop (struct it *it)
5244 {
5245 Lisp_Object prop, string;
5246 ptrdiff_t pos, pos_byte, start, end;
5247
5248 if (STRINGP (it->string))
5249 {
5250 unsigned char *s;
5251
5252 pos = IT_STRING_CHARPOS (*it);
5253 pos_byte = IT_STRING_BYTEPOS (*it);
5254 string = it->string;
5255 s = SDATA (string) + pos_byte;
5256 it->c = STRING_CHAR (s);
5257 }
5258 else
5259 {
5260 pos = IT_CHARPOS (*it);
5261 pos_byte = IT_BYTEPOS (*it);
5262 string = Qnil;
5263 it->c = FETCH_CHAR (pos_byte);
5264 }
5265
5266 /* If there's a valid composition and point is not inside of the
5267 composition (in the case that the composition is from the current
5268 buffer), draw a glyph composed from the composition components. */
5269 if (find_composition (pos, -1, &start, &end, &prop, string)
5270 && COMPOSITION_VALID_P (start, end, prop)
5271 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5272 {
5273 if (start < pos)
5274 /* As we can't handle this situation (perhaps font-lock added
5275 a new composition), we just return here hoping that next
5276 redisplay will detect this composition much earlier. */
5277 return HANDLED_NORMALLY;
5278 if (start != pos)
5279 {
5280 if (STRINGP (it->string))
5281 pos_byte = string_char_to_byte (it->string, start);
5282 else
5283 pos_byte = CHAR_TO_BYTE (start);
5284 }
5285 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5286 prop, string);
5287
5288 if (it->cmp_it.id >= 0)
5289 {
5290 it->cmp_it.ch = -1;
5291 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5292 it->cmp_it.nglyphs = -1;
5293 }
5294 }
5295
5296 return HANDLED_NORMALLY;
5297 }
5298
5299
5300 \f
5301 /***********************************************************************
5302 Overlay strings
5303 ***********************************************************************/
5304
5305 /* The following structure is used to record overlay strings for
5306 later sorting in load_overlay_strings. */
5307
5308 struct overlay_entry
5309 {
5310 Lisp_Object overlay;
5311 Lisp_Object string;
5312 EMACS_INT priority;
5313 int after_string_p;
5314 };
5315
5316
5317 /* Set up iterator IT from overlay strings at its current position.
5318 Called from handle_stop. */
5319
5320 static enum prop_handled
5321 handle_overlay_change (struct it *it)
5322 {
5323 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5324 return HANDLED_RECOMPUTE_PROPS;
5325 else
5326 return HANDLED_NORMALLY;
5327 }
5328
5329
5330 /* Set up the next overlay string for delivery by IT, if there is an
5331 overlay string to deliver. Called by set_iterator_to_next when the
5332 end of the current overlay string is reached. If there are more
5333 overlay strings to display, IT->string and
5334 IT->current.overlay_string_index are set appropriately here.
5335 Otherwise IT->string is set to nil. */
5336
5337 static void
5338 next_overlay_string (struct it *it)
5339 {
5340 ++it->current.overlay_string_index;
5341 if (it->current.overlay_string_index == it->n_overlay_strings)
5342 {
5343 /* No more overlay strings. Restore IT's settings to what
5344 they were before overlay strings were processed, and
5345 continue to deliver from current_buffer. */
5346
5347 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5348 pop_it (it);
5349 eassert (it->sp > 0
5350 || (NILP (it->string)
5351 && it->method == GET_FROM_BUFFER
5352 && it->stop_charpos >= BEGV
5353 && it->stop_charpos <= it->end_charpos));
5354 it->current.overlay_string_index = -1;
5355 it->n_overlay_strings = 0;
5356 it->overlay_strings_charpos = -1;
5357 /* If there's an empty display string on the stack, pop the
5358 stack, to resync the bidi iterator with IT's position. Such
5359 empty strings are pushed onto the stack in
5360 get_overlay_strings_1. */
5361 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5362 pop_it (it);
5363
5364 /* If we're at the end of the buffer, record that we have
5365 processed the overlay strings there already, so that
5366 next_element_from_buffer doesn't try it again. */
5367 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5368 it->overlay_strings_at_end_processed_p = 1;
5369 }
5370 else
5371 {
5372 /* There are more overlay strings to process. If
5373 IT->current.overlay_string_index has advanced to a position
5374 where we must load IT->overlay_strings with more strings, do
5375 it. We must load at the IT->overlay_strings_charpos where
5376 IT->n_overlay_strings was originally computed; when invisible
5377 text is present, this might not be IT_CHARPOS (Bug#7016). */
5378 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5379
5380 if (it->current.overlay_string_index && i == 0)
5381 load_overlay_strings (it, it->overlay_strings_charpos);
5382
5383 /* Initialize IT to deliver display elements from the overlay
5384 string. */
5385 it->string = it->overlay_strings[i];
5386 it->multibyte_p = STRING_MULTIBYTE (it->string);
5387 SET_TEXT_POS (it->current.string_pos, 0, 0);
5388 it->method = GET_FROM_STRING;
5389 it->stop_charpos = 0;
5390 it->end_charpos = SCHARS (it->string);
5391 if (it->cmp_it.stop_pos >= 0)
5392 it->cmp_it.stop_pos = 0;
5393 it->prev_stop = 0;
5394 it->base_level_stop = 0;
5395
5396 /* Set up the bidi iterator for this overlay string. */
5397 if (it->bidi_p)
5398 {
5399 it->bidi_it.string.lstring = it->string;
5400 it->bidi_it.string.s = NULL;
5401 it->bidi_it.string.schars = SCHARS (it->string);
5402 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5403 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5404 it->bidi_it.string.unibyte = !it->multibyte_p;
5405 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5406 }
5407 }
5408
5409 CHECK_IT (it);
5410 }
5411
5412
5413 /* Compare two overlay_entry structures E1 and E2. Used as a
5414 comparison function for qsort in load_overlay_strings. Overlay
5415 strings for the same position are sorted so that
5416
5417 1. All after-strings come in front of before-strings, except
5418 when they come from the same overlay.
5419
5420 2. Within after-strings, strings are sorted so that overlay strings
5421 from overlays with higher priorities come first.
5422
5423 2. Within before-strings, strings are sorted so that overlay
5424 strings from overlays with higher priorities come last.
5425
5426 Value is analogous to strcmp. */
5427
5428
5429 static int
5430 compare_overlay_entries (const void *e1, const void *e2)
5431 {
5432 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5433 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5434 int result;
5435
5436 if (entry1->after_string_p != entry2->after_string_p)
5437 {
5438 /* Let after-strings appear in front of before-strings if
5439 they come from different overlays. */
5440 if (EQ (entry1->overlay, entry2->overlay))
5441 result = entry1->after_string_p ? 1 : -1;
5442 else
5443 result = entry1->after_string_p ? -1 : 1;
5444 }
5445 else if (entry1->priority != entry2->priority)
5446 {
5447 if (entry1->after_string_p)
5448 /* After-strings sorted in order of decreasing priority. */
5449 result = entry2->priority < entry1->priority ? -1 : 1;
5450 else
5451 /* Before-strings sorted in order of increasing priority. */
5452 result = entry1->priority < entry2->priority ? -1 : 1;
5453 }
5454 else
5455 result = 0;
5456
5457 return result;
5458 }
5459
5460
5461 /* Load the vector IT->overlay_strings with overlay strings from IT's
5462 current buffer position, or from CHARPOS if that is > 0. Set
5463 IT->n_overlays to the total number of overlay strings found.
5464
5465 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5466 a time. On entry into load_overlay_strings,
5467 IT->current.overlay_string_index gives the number of overlay
5468 strings that have already been loaded by previous calls to this
5469 function.
5470
5471 IT->add_overlay_start contains an additional overlay start
5472 position to consider for taking overlay strings from, if non-zero.
5473 This position comes into play when the overlay has an `invisible'
5474 property, and both before and after-strings. When we've skipped to
5475 the end of the overlay, because of its `invisible' property, we
5476 nevertheless want its before-string to appear.
5477 IT->add_overlay_start will contain the overlay start position
5478 in this case.
5479
5480 Overlay strings are sorted so that after-string strings come in
5481 front of before-string strings. Within before and after-strings,
5482 strings are sorted by overlay priority. See also function
5483 compare_overlay_entries. */
5484
5485 static void
5486 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5487 {
5488 Lisp_Object overlay, window, str, invisible;
5489 struct Lisp_Overlay *ov;
5490 ptrdiff_t start, end;
5491 ptrdiff_t size = 20;
5492 ptrdiff_t n = 0, i, j;
5493 int invis_p;
5494 struct overlay_entry *entries = alloca (size * sizeof *entries);
5495 USE_SAFE_ALLOCA;
5496
5497 if (charpos <= 0)
5498 charpos = IT_CHARPOS (*it);
5499
5500 /* Append the overlay string STRING of overlay OVERLAY to vector
5501 `entries' which has size `size' and currently contains `n'
5502 elements. AFTER_P non-zero means STRING is an after-string of
5503 OVERLAY. */
5504 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5505 do \
5506 { \
5507 Lisp_Object priority; \
5508 \
5509 if (n == size) \
5510 { \
5511 struct overlay_entry *old = entries; \
5512 SAFE_NALLOCA (entries, 2, size); \
5513 memcpy (entries, old, size * sizeof *entries); \
5514 size *= 2; \
5515 } \
5516 \
5517 entries[n].string = (STRING); \
5518 entries[n].overlay = (OVERLAY); \
5519 priority = Foverlay_get ((OVERLAY), Qpriority); \
5520 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5521 entries[n].after_string_p = (AFTER_P); \
5522 ++n; \
5523 } \
5524 while (0)
5525
5526 /* Process overlay before the overlay center. */
5527 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5528 {
5529 XSETMISC (overlay, ov);
5530 eassert (OVERLAYP (overlay));
5531 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5532 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5533
5534 if (end < charpos)
5535 break;
5536
5537 /* Skip this overlay if it doesn't start or end at IT's current
5538 position. */
5539 if (end != charpos && start != charpos)
5540 continue;
5541
5542 /* Skip this overlay if it doesn't apply to IT->w. */
5543 window = Foverlay_get (overlay, Qwindow);
5544 if (WINDOWP (window) && XWINDOW (window) != it->w)
5545 continue;
5546
5547 /* If the text ``under'' the overlay is invisible, both before-
5548 and after-strings from this overlay are visible; start and
5549 end position are indistinguishable. */
5550 invisible = Foverlay_get (overlay, Qinvisible);
5551 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5552
5553 /* If overlay has a non-empty before-string, record it. */
5554 if ((start == charpos || (end == charpos && invis_p))
5555 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5556 && SCHARS (str))
5557 RECORD_OVERLAY_STRING (overlay, str, 0);
5558
5559 /* If overlay has a non-empty after-string, record it. */
5560 if ((end == charpos || (start == charpos && invis_p))
5561 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5562 && SCHARS (str))
5563 RECORD_OVERLAY_STRING (overlay, str, 1);
5564 }
5565
5566 /* Process overlays after the overlay center. */
5567 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5568 {
5569 XSETMISC (overlay, ov);
5570 eassert (OVERLAYP (overlay));
5571 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5572 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5573
5574 if (start > charpos)
5575 break;
5576
5577 /* Skip this overlay if it doesn't start or end at IT's current
5578 position. */
5579 if (end != charpos && start != charpos)
5580 continue;
5581
5582 /* Skip this overlay if it doesn't apply to IT->w. */
5583 window = Foverlay_get (overlay, Qwindow);
5584 if (WINDOWP (window) && XWINDOW (window) != it->w)
5585 continue;
5586
5587 /* If the text ``under'' the overlay is invisible, it has a zero
5588 dimension, and both before- and after-strings apply. */
5589 invisible = Foverlay_get (overlay, Qinvisible);
5590 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5591
5592 /* If overlay has a non-empty before-string, record it. */
5593 if ((start == charpos || (end == charpos && invis_p))
5594 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5595 && SCHARS (str))
5596 RECORD_OVERLAY_STRING (overlay, str, 0);
5597
5598 /* If overlay has a non-empty after-string, record it. */
5599 if ((end == charpos || (start == charpos && invis_p))
5600 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5601 && SCHARS (str))
5602 RECORD_OVERLAY_STRING (overlay, str, 1);
5603 }
5604
5605 #undef RECORD_OVERLAY_STRING
5606
5607 /* Sort entries. */
5608 if (n > 1)
5609 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5610
5611 /* Record number of overlay strings, and where we computed it. */
5612 it->n_overlay_strings = n;
5613 it->overlay_strings_charpos = charpos;
5614
5615 /* IT->current.overlay_string_index is the number of overlay strings
5616 that have already been consumed by IT. Copy some of the
5617 remaining overlay strings to IT->overlay_strings. */
5618 i = 0;
5619 j = it->current.overlay_string_index;
5620 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5621 {
5622 it->overlay_strings[i] = entries[j].string;
5623 it->string_overlays[i++] = entries[j++].overlay;
5624 }
5625
5626 CHECK_IT (it);
5627 SAFE_FREE ();
5628 }
5629
5630
5631 /* Get the first chunk of overlay strings at IT's current buffer
5632 position, or at CHARPOS if that is > 0. Value is non-zero if at
5633 least one overlay string was found. */
5634
5635 static int
5636 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5637 {
5638 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5639 process. This fills IT->overlay_strings with strings, and sets
5640 IT->n_overlay_strings to the total number of strings to process.
5641 IT->pos.overlay_string_index has to be set temporarily to zero
5642 because load_overlay_strings needs this; it must be set to -1
5643 when no overlay strings are found because a zero value would
5644 indicate a position in the first overlay string. */
5645 it->current.overlay_string_index = 0;
5646 load_overlay_strings (it, charpos);
5647
5648 /* If we found overlay strings, set up IT to deliver display
5649 elements from the first one. Otherwise set up IT to deliver
5650 from current_buffer. */
5651 if (it->n_overlay_strings)
5652 {
5653 /* Make sure we know settings in current_buffer, so that we can
5654 restore meaningful values when we're done with the overlay
5655 strings. */
5656 if (compute_stop_p)
5657 compute_stop_pos (it);
5658 eassert (it->face_id >= 0);
5659
5660 /* Save IT's settings. They are restored after all overlay
5661 strings have been processed. */
5662 eassert (!compute_stop_p || it->sp == 0);
5663
5664 /* When called from handle_stop, there might be an empty display
5665 string loaded. In that case, don't bother saving it. But
5666 don't use this optimization with the bidi iterator, since we
5667 need the corresponding pop_it call to resync the bidi
5668 iterator's position with IT's position, after we are done
5669 with the overlay strings. (The corresponding call to pop_it
5670 in case of an empty display string is in
5671 next_overlay_string.) */
5672 if (!(!it->bidi_p
5673 && STRINGP (it->string) && !SCHARS (it->string)))
5674 push_it (it, NULL);
5675
5676 /* Set up IT to deliver display elements from the first overlay
5677 string. */
5678 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5679 it->string = it->overlay_strings[0];
5680 it->from_overlay = Qnil;
5681 it->stop_charpos = 0;
5682 eassert (STRINGP (it->string));
5683 it->end_charpos = SCHARS (it->string);
5684 it->prev_stop = 0;
5685 it->base_level_stop = 0;
5686 it->multibyte_p = STRING_MULTIBYTE (it->string);
5687 it->method = GET_FROM_STRING;
5688 it->from_disp_prop_p = 0;
5689
5690 /* Force paragraph direction to be that of the parent
5691 buffer. */
5692 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5693 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5694 else
5695 it->paragraph_embedding = L2R;
5696
5697 /* Set up the bidi iterator for this overlay string. */
5698 if (it->bidi_p)
5699 {
5700 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5701
5702 it->bidi_it.string.lstring = it->string;
5703 it->bidi_it.string.s = NULL;
5704 it->bidi_it.string.schars = SCHARS (it->string);
5705 it->bidi_it.string.bufpos = pos;
5706 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5707 it->bidi_it.string.unibyte = !it->multibyte_p;
5708 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5709 }
5710 return 1;
5711 }
5712
5713 it->current.overlay_string_index = -1;
5714 return 0;
5715 }
5716
5717 static int
5718 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5719 {
5720 it->string = Qnil;
5721 it->method = GET_FROM_BUFFER;
5722
5723 (void) get_overlay_strings_1 (it, charpos, 1);
5724
5725 CHECK_IT (it);
5726
5727 /* Value is non-zero if we found at least one overlay string. */
5728 return STRINGP (it->string);
5729 }
5730
5731
5732 \f
5733 /***********************************************************************
5734 Saving and restoring state
5735 ***********************************************************************/
5736
5737 /* Save current settings of IT on IT->stack. Called, for example,
5738 before setting up IT for an overlay string, to be able to restore
5739 IT's settings to what they were after the overlay string has been
5740 processed. If POSITION is non-NULL, it is the position to save on
5741 the stack instead of IT->position. */
5742
5743 static void
5744 push_it (struct it *it, struct text_pos *position)
5745 {
5746 struct iterator_stack_entry *p;
5747
5748 eassert (it->sp < IT_STACK_SIZE);
5749 p = it->stack + it->sp;
5750
5751 p->stop_charpos = it->stop_charpos;
5752 p->prev_stop = it->prev_stop;
5753 p->base_level_stop = it->base_level_stop;
5754 p->cmp_it = it->cmp_it;
5755 eassert (it->face_id >= 0);
5756 p->face_id = it->face_id;
5757 p->string = it->string;
5758 p->method = it->method;
5759 p->from_overlay = it->from_overlay;
5760 switch (p->method)
5761 {
5762 case GET_FROM_IMAGE:
5763 p->u.image.object = it->object;
5764 p->u.image.image_id = it->image_id;
5765 p->u.image.slice = it->slice;
5766 break;
5767 case GET_FROM_STRETCH:
5768 p->u.stretch.object = it->object;
5769 break;
5770 }
5771 p->position = position ? *position : it->position;
5772 p->current = it->current;
5773 p->end_charpos = it->end_charpos;
5774 p->string_nchars = it->string_nchars;
5775 p->area = it->area;
5776 p->multibyte_p = it->multibyte_p;
5777 p->avoid_cursor_p = it->avoid_cursor_p;
5778 p->space_width = it->space_width;
5779 p->font_height = it->font_height;
5780 p->voffset = it->voffset;
5781 p->string_from_display_prop_p = it->string_from_display_prop_p;
5782 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5783 p->display_ellipsis_p = 0;
5784 p->line_wrap = it->line_wrap;
5785 p->bidi_p = it->bidi_p;
5786 p->paragraph_embedding = it->paragraph_embedding;
5787 p->from_disp_prop_p = it->from_disp_prop_p;
5788 ++it->sp;
5789
5790 /* Save the state of the bidi iterator as well. */
5791 if (it->bidi_p)
5792 bidi_push_it (&it->bidi_it);
5793 }
5794
5795 static void
5796 iterate_out_of_display_property (struct it *it)
5797 {
5798 int buffer_p = !STRINGP (it->string);
5799 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5800 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5801
5802 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5803
5804 /* Maybe initialize paragraph direction. If we are at the beginning
5805 of a new paragraph, next_element_from_buffer may not have a
5806 chance to do that. */
5807 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5808 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5809 /* prev_stop can be zero, so check against BEGV as well. */
5810 while (it->bidi_it.charpos >= bob
5811 && it->prev_stop <= it->bidi_it.charpos
5812 && it->bidi_it.charpos < CHARPOS (it->position)
5813 && it->bidi_it.charpos < eob)
5814 bidi_move_to_visually_next (&it->bidi_it);
5815 /* Record the stop_pos we just crossed, for when we cross it
5816 back, maybe. */
5817 if (it->bidi_it.charpos > CHARPOS (it->position))
5818 it->prev_stop = CHARPOS (it->position);
5819 /* If we ended up not where pop_it put us, resync IT's
5820 positional members with the bidi iterator. */
5821 if (it->bidi_it.charpos != CHARPOS (it->position))
5822 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5823 if (buffer_p)
5824 it->current.pos = it->position;
5825 else
5826 it->current.string_pos = it->position;
5827 }
5828
5829 /* Restore IT's settings from IT->stack. Called, for example, when no
5830 more overlay strings must be processed, and we return to delivering
5831 display elements from a buffer, or when the end of a string from a
5832 `display' property is reached and we return to delivering display
5833 elements from an overlay string, or from a buffer. */
5834
5835 static void
5836 pop_it (struct it *it)
5837 {
5838 struct iterator_stack_entry *p;
5839 int from_display_prop = it->from_disp_prop_p;
5840
5841 eassert (it->sp > 0);
5842 --it->sp;
5843 p = it->stack + it->sp;
5844 it->stop_charpos = p->stop_charpos;
5845 it->prev_stop = p->prev_stop;
5846 it->base_level_stop = p->base_level_stop;
5847 it->cmp_it = p->cmp_it;
5848 it->face_id = p->face_id;
5849 it->current = p->current;
5850 it->position = p->position;
5851 it->string = p->string;
5852 it->from_overlay = p->from_overlay;
5853 if (NILP (it->string))
5854 SET_TEXT_POS (it->current.string_pos, -1, -1);
5855 it->method = p->method;
5856 switch (it->method)
5857 {
5858 case GET_FROM_IMAGE:
5859 it->image_id = p->u.image.image_id;
5860 it->object = p->u.image.object;
5861 it->slice = p->u.image.slice;
5862 break;
5863 case GET_FROM_STRETCH:
5864 it->object = p->u.stretch.object;
5865 break;
5866 case GET_FROM_BUFFER:
5867 it->object = it->w->buffer;
5868 break;
5869 case GET_FROM_STRING:
5870 it->object = it->string;
5871 break;
5872 case GET_FROM_DISPLAY_VECTOR:
5873 if (it->s)
5874 it->method = GET_FROM_C_STRING;
5875 else if (STRINGP (it->string))
5876 it->method = GET_FROM_STRING;
5877 else
5878 {
5879 it->method = GET_FROM_BUFFER;
5880 it->object = it->w->buffer;
5881 }
5882 }
5883 it->end_charpos = p->end_charpos;
5884 it->string_nchars = p->string_nchars;
5885 it->area = p->area;
5886 it->multibyte_p = p->multibyte_p;
5887 it->avoid_cursor_p = p->avoid_cursor_p;
5888 it->space_width = p->space_width;
5889 it->font_height = p->font_height;
5890 it->voffset = p->voffset;
5891 it->string_from_display_prop_p = p->string_from_display_prop_p;
5892 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5893 it->line_wrap = p->line_wrap;
5894 it->bidi_p = p->bidi_p;
5895 it->paragraph_embedding = p->paragraph_embedding;
5896 it->from_disp_prop_p = p->from_disp_prop_p;
5897 if (it->bidi_p)
5898 {
5899 bidi_pop_it (&it->bidi_it);
5900 /* Bidi-iterate until we get out of the portion of text, if any,
5901 covered by a `display' text property or by an overlay with
5902 `display' property. (We cannot just jump there, because the
5903 internal coherency of the bidi iterator state can not be
5904 preserved across such jumps.) We also must determine the
5905 paragraph base direction if the overlay we just processed is
5906 at the beginning of a new paragraph. */
5907 if (from_display_prop
5908 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5909 iterate_out_of_display_property (it);
5910
5911 eassert ((BUFFERP (it->object)
5912 && IT_CHARPOS (*it) == it->bidi_it.charpos
5913 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5914 || (STRINGP (it->object)
5915 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5916 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5917 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5918 }
5919 }
5920
5921
5922 \f
5923 /***********************************************************************
5924 Moving over lines
5925 ***********************************************************************/
5926
5927 /* Set IT's current position to the previous line start. */
5928
5929 static void
5930 back_to_previous_line_start (struct it *it)
5931 {
5932 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5934 }
5935
5936
5937 /* Move IT to the next line start.
5938
5939 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5940 we skipped over part of the text (as opposed to moving the iterator
5941 continuously over the text). Otherwise, don't change the value
5942 of *SKIPPED_P.
5943
5944 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5945 iterator on the newline, if it was found.
5946
5947 Newlines may come from buffer text, overlay strings, or strings
5948 displayed via the `display' property. That's the reason we can't
5949 simply use find_next_newline_no_quit.
5950
5951 Note that this function may not skip over invisible text that is so
5952 because of text properties and immediately follows a newline. If
5953 it would, function reseat_at_next_visible_line_start, when called
5954 from set_iterator_to_next, would effectively make invisible
5955 characters following a newline part of the wrong glyph row, which
5956 leads to wrong cursor motion. */
5957
5958 static int
5959 forward_to_next_line_start (struct it *it, int *skipped_p,
5960 struct bidi_it *bidi_it_prev)
5961 {
5962 ptrdiff_t old_selective;
5963 int newline_found_p, n;
5964 const int MAX_NEWLINE_DISTANCE = 500;
5965
5966 /* If already on a newline, just consume it to avoid unintended
5967 skipping over invisible text below. */
5968 if (it->what == IT_CHARACTER
5969 && it->c == '\n'
5970 && CHARPOS (it->position) == IT_CHARPOS (*it))
5971 {
5972 if (it->bidi_p && bidi_it_prev)
5973 *bidi_it_prev = it->bidi_it;
5974 set_iterator_to_next (it, 0);
5975 it->c = 0;
5976 return 1;
5977 }
5978
5979 /* Don't handle selective display in the following. It's (a)
5980 unnecessary because it's done by the caller, and (b) leads to an
5981 infinite recursion because next_element_from_ellipsis indirectly
5982 calls this function. */
5983 old_selective = it->selective;
5984 it->selective = 0;
5985
5986 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5987 from buffer text. */
5988 for (n = newline_found_p = 0;
5989 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5990 n += STRINGP (it->string) ? 0 : 1)
5991 {
5992 if (!get_next_display_element (it))
5993 return 0;
5994 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5995 if (newline_found_p && it->bidi_p && bidi_it_prev)
5996 *bidi_it_prev = it->bidi_it;
5997 set_iterator_to_next (it, 0);
5998 }
5999
6000 /* If we didn't find a newline near enough, see if we can use a
6001 short-cut. */
6002 if (!newline_found_p)
6003 {
6004 ptrdiff_t start = IT_CHARPOS (*it);
6005 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6006 Lisp_Object pos;
6007
6008 eassert (!STRINGP (it->string));
6009
6010 /* If there isn't any `display' property in sight, and no
6011 overlays, we can just use the position of the newline in
6012 buffer text. */
6013 if (it->stop_charpos >= limit
6014 || ((pos = Fnext_single_property_change (make_number (start),
6015 Qdisplay, Qnil,
6016 make_number (limit)),
6017 NILP (pos))
6018 && next_overlay_change (start) == ZV))
6019 {
6020 if (!it->bidi_p)
6021 {
6022 IT_CHARPOS (*it) = limit;
6023 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6024 }
6025 else
6026 {
6027 struct bidi_it bprev;
6028
6029 /* Help bidi.c avoid expensive searches for display
6030 properties and overlays, by telling it that there are
6031 none up to `limit'. */
6032 if (it->bidi_it.disp_pos < limit)
6033 {
6034 it->bidi_it.disp_pos = limit;
6035 it->bidi_it.disp_prop = 0;
6036 }
6037 do {
6038 bprev = it->bidi_it;
6039 bidi_move_to_visually_next (&it->bidi_it);
6040 } while (it->bidi_it.charpos != limit);
6041 IT_CHARPOS (*it) = limit;
6042 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6043 if (bidi_it_prev)
6044 *bidi_it_prev = bprev;
6045 }
6046 *skipped_p = newline_found_p = 1;
6047 }
6048 else
6049 {
6050 while (get_next_display_element (it)
6051 && !newline_found_p)
6052 {
6053 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6054 if (newline_found_p && it->bidi_p && bidi_it_prev)
6055 *bidi_it_prev = it->bidi_it;
6056 set_iterator_to_next (it, 0);
6057 }
6058 }
6059 }
6060
6061 it->selective = old_selective;
6062 return newline_found_p;
6063 }
6064
6065
6066 /* Set IT's current position to the previous visible line start. Skip
6067 invisible text that is so either due to text properties or due to
6068 selective display. Caution: this does not change IT->current_x and
6069 IT->hpos. */
6070
6071 static void
6072 back_to_previous_visible_line_start (struct it *it)
6073 {
6074 while (IT_CHARPOS (*it) > BEGV)
6075 {
6076 back_to_previous_line_start (it);
6077
6078 if (IT_CHARPOS (*it) <= BEGV)
6079 break;
6080
6081 /* If selective > 0, then lines indented more than its value are
6082 invisible. */
6083 if (it->selective > 0
6084 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6085 it->selective))
6086 continue;
6087
6088 /* Check the newline before point for invisibility. */
6089 {
6090 Lisp_Object prop;
6091 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6092 Qinvisible, it->window);
6093 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6094 continue;
6095 }
6096
6097 if (IT_CHARPOS (*it) <= BEGV)
6098 break;
6099
6100 {
6101 struct it it2;
6102 void *it2data = NULL;
6103 ptrdiff_t pos;
6104 ptrdiff_t beg, end;
6105 Lisp_Object val, overlay;
6106
6107 SAVE_IT (it2, *it, it2data);
6108
6109 /* If newline is part of a composition, continue from start of composition */
6110 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6111 && beg < IT_CHARPOS (*it))
6112 goto replaced;
6113
6114 /* If newline is replaced by a display property, find start of overlay
6115 or interval and continue search from that point. */
6116 pos = --IT_CHARPOS (it2);
6117 --IT_BYTEPOS (it2);
6118 it2.sp = 0;
6119 bidi_unshelve_cache (NULL, 0);
6120 it2.string_from_display_prop_p = 0;
6121 it2.from_disp_prop_p = 0;
6122 if (handle_display_prop (&it2) == HANDLED_RETURN
6123 && !NILP (val = get_char_property_and_overlay
6124 (make_number (pos), Qdisplay, Qnil, &overlay))
6125 && (OVERLAYP (overlay)
6126 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6127 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6128 {
6129 RESTORE_IT (it, it, it2data);
6130 goto replaced;
6131 }
6132
6133 /* Newline is not replaced by anything -- so we are done. */
6134 RESTORE_IT (it, it, it2data);
6135 break;
6136
6137 replaced:
6138 if (beg < BEGV)
6139 beg = BEGV;
6140 IT_CHARPOS (*it) = beg;
6141 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6142 }
6143 }
6144
6145 it->continuation_lines_width = 0;
6146
6147 eassert (IT_CHARPOS (*it) >= BEGV);
6148 eassert (IT_CHARPOS (*it) == BEGV
6149 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6150 CHECK_IT (it);
6151 }
6152
6153
6154 /* Reseat iterator IT at the previous visible line start. Skip
6155 invisible text that is so either due to text properties or due to
6156 selective display. At the end, update IT's overlay information,
6157 face information etc. */
6158
6159 void
6160 reseat_at_previous_visible_line_start (struct it *it)
6161 {
6162 back_to_previous_visible_line_start (it);
6163 reseat (it, it->current.pos, 1);
6164 CHECK_IT (it);
6165 }
6166
6167
6168 /* Reseat iterator IT on the next visible line start in the current
6169 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6170 preceding the line start. Skip over invisible text that is so
6171 because of selective display. Compute faces, overlays etc at the
6172 new position. Note that this function does not skip over text that
6173 is invisible because of text properties. */
6174
6175 static void
6176 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6177 {
6178 int newline_found_p, skipped_p = 0;
6179 struct bidi_it bidi_it_prev;
6180
6181 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6182
6183 /* Skip over lines that are invisible because they are indented
6184 more than the value of IT->selective. */
6185 if (it->selective > 0)
6186 while (IT_CHARPOS (*it) < ZV
6187 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6188 it->selective))
6189 {
6190 eassert (IT_BYTEPOS (*it) == BEGV
6191 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6192 newline_found_p =
6193 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6194 }
6195
6196 /* Position on the newline if that's what's requested. */
6197 if (on_newline_p && newline_found_p)
6198 {
6199 if (STRINGP (it->string))
6200 {
6201 if (IT_STRING_CHARPOS (*it) > 0)
6202 {
6203 if (!it->bidi_p)
6204 {
6205 --IT_STRING_CHARPOS (*it);
6206 --IT_STRING_BYTEPOS (*it);
6207 }
6208 else
6209 {
6210 /* We need to restore the bidi iterator to the state
6211 it had on the newline, and resync the IT's
6212 position with that. */
6213 it->bidi_it = bidi_it_prev;
6214 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6215 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6216 }
6217 }
6218 }
6219 else if (IT_CHARPOS (*it) > BEGV)
6220 {
6221 if (!it->bidi_p)
6222 {
6223 --IT_CHARPOS (*it);
6224 --IT_BYTEPOS (*it);
6225 }
6226 else
6227 {
6228 /* We need to restore the bidi iterator to the state it
6229 had on the newline and resync IT with that. */
6230 it->bidi_it = bidi_it_prev;
6231 IT_CHARPOS (*it) = it->bidi_it.charpos;
6232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6233 }
6234 reseat (it, it->current.pos, 0);
6235 }
6236 }
6237 else if (skipped_p)
6238 reseat (it, it->current.pos, 0);
6239
6240 CHECK_IT (it);
6241 }
6242
6243
6244 \f
6245 /***********************************************************************
6246 Changing an iterator's position
6247 ***********************************************************************/
6248
6249 /* Change IT's current position to POS in current_buffer. If FORCE_P
6250 is non-zero, always check for text properties at the new position.
6251 Otherwise, text properties are only looked up if POS >=
6252 IT->check_charpos of a property. */
6253
6254 static void
6255 reseat (struct it *it, struct text_pos pos, int force_p)
6256 {
6257 ptrdiff_t original_pos = IT_CHARPOS (*it);
6258
6259 reseat_1 (it, pos, 0);
6260
6261 /* Determine where to check text properties. Avoid doing it
6262 where possible because text property lookup is very expensive. */
6263 if (force_p
6264 || CHARPOS (pos) > it->stop_charpos
6265 || CHARPOS (pos) < original_pos)
6266 {
6267 if (it->bidi_p)
6268 {
6269 /* For bidi iteration, we need to prime prev_stop and
6270 base_level_stop with our best estimations. */
6271 /* Implementation note: Of course, POS is not necessarily a
6272 stop position, so assigning prev_pos to it is a lie; we
6273 should have called compute_stop_backwards. However, if
6274 the current buffer does not include any R2L characters,
6275 that call would be a waste of cycles, because the
6276 iterator will never move back, and thus never cross this
6277 "fake" stop position. So we delay that backward search
6278 until the time we really need it, in next_element_from_buffer. */
6279 if (CHARPOS (pos) != it->prev_stop)
6280 it->prev_stop = CHARPOS (pos);
6281 if (CHARPOS (pos) < it->base_level_stop)
6282 it->base_level_stop = 0; /* meaning it's unknown */
6283 handle_stop (it);
6284 }
6285 else
6286 {
6287 handle_stop (it);
6288 it->prev_stop = it->base_level_stop = 0;
6289 }
6290
6291 }
6292
6293 CHECK_IT (it);
6294 }
6295
6296
6297 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6298 IT->stop_pos to POS, also. */
6299
6300 static void
6301 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6302 {
6303 /* Don't call this function when scanning a C string. */
6304 eassert (it->s == NULL);
6305
6306 /* POS must be a reasonable value. */
6307 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6308
6309 it->current.pos = it->position = pos;
6310 it->end_charpos = ZV;
6311 it->dpvec = NULL;
6312 it->current.dpvec_index = -1;
6313 it->current.overlay_string_index = -1;
6314 IT_STRING_CHARPOS (*it) = -1;
6315 IT_STRING_BYTEPOS (*it) = -1;
6316 it->string = Qnil;
6317 it->method = GET_FROM_BUFFER;
6318 it->object = it->w->buffer;
6319 it->area = TEXT_AREA;
6320 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6321 it->sp = 0;
6322 it->string_from_display_prop_p = 0;
6323 it->string_from_prefix_prop_p = 0;
6324
6325 it->from_disp_prop_p = 0;
6326 it->face_before_selective_p = 0;
6327 if (it->bidi_p)
6328 {
6329 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6330 &it->bidi_it);
6331 bidi_unshelve_cache (NULL, 0);
6332 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6333 it->bidi_it.string.s = NULL;
6334 it->bidi_it.string.lstring = Qnil;
6335 it->bidi_it.string.bufpos = 0;
6336 it->bidi_it.string.unibyte = 0;
6337 }
6338
6339 if (set_stop_p)
6340 {
6341 it->stop_charpos = CHARPOS (pos);
6342 it->base_level_stop = CHARPOS (pos);
6343 }
6344 /* This make the information stored in it->cmp_it invalidate. */
6345 it->cmp_it.id = -1;
6346 }
6347
6348
6349 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6350 If S is non-null, it is a C string to iterate over. Otherwise,
6351 STRING gives a Lisp string to iterate over.
6352
6353 If PRECISION > 0, don't return more then PRECISION number of
6354 characters from the string.
6355
6356 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6357 characters have been returned. FIELD_WIDTH < 0 means an infinite
6358 field width.
6359
6360 MULTIBYTE = 0 means disable processing of multibyte characters,
6361 MULTIBYTE > 0 means enable it,
6362 MULTIBYTE < 0 means use IT->multibyte_p.
6363
6364 IT must be initialized via a prior call to init_iterator before
6365 calling this function. */
6366
6367 static void
6368 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6369 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6370 int multibyte)
6371 {
6372 /* No region in strings. */
6373 it->region_beg_charpos = it->region_end_charpos = -1;
6374
6375 /* No text property checks performed by default, but see below. */
6376 it->stop_charpos = -1;
6377
6378 /* Set iterator position and end position. */
6379 memset (&it->current, 0, sizeof it->current);
6380 it->current.overlay_string_index = -1;
6381 it->current.dpvec_index = -1;
6382 eassert (charpos >= 0);
6383
6384 /* If STRING is specified, use its multibyteness, otherwise use the
6385 setting of MULTIBYTE, if specified. */
6386 if (multibyte >= 0)
6387 it->multibyte_p = multibyte > 0;
6388
6389 /* Bidirectional reordering of strings is controlled by the default
6390 value of bidi-display-reordering. Don't try to reorder while
6391 loading loadup.el, as the necessary character property tables are
6392 not yet available. */
6393 it->bidi_p =
6394 NILP (Vpurify_flag)
6395 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6396
6397 if (s == NULL)
6398 {
6399 eassert (STRINGP (string));
6400 it->string = string;
6401 it->s = NULL;
6402 it->end_charpos = it->string_nchars = SCHARS (string);
6403 it->method = GET_FROM_STRING;
6404 it->current.string_pos = string_pos (charpos, string);
6405
6406 if (it->bidi_p)
6407 {
6408 it->bidi_it.string.lstring = string;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.schars = it->end_charpos;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.from_disp_str = 0;
6413 it->bidi_it.string.unibyte = !it->multibyte_p;
6414 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6415 FRAME_WINDOW_P (it->f), &it->bidi_it);
6416 }
6417 }
6418 else
6419 {
6420 it->s = (const unsigned char *) s;
6421 it->string = Qnil;
6422
6423 /* Note that we use IT->current.pos, not it->current.string_pos,
6424 for displaying C strings. */
6425 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6426 if (it->multibyte_p)
6427 {
6428 it->current.pos = c_string_pos (charpos, s, 1);
6429 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6430 }
6431 else
6432 {
6433 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6434 it->end_charpos = it->string_nchars = strlen (s);
6435 }
6436
6437 if (it->bidi_p)
6438 {
6439 it->bidi_it.string.lstring = Qnil;
6440 it->bidi_it.string.s = (const unsigned char *) s;
6441 it->bidi_it.string.schars = it->end_charpos;
6442 it->bidi_it.string.bufpos = 0;
6443 it->bidi_it.string.from_disp_str = 0;
6444 it->bidi_it.string.unibyte = !it->multibyte_p;
6445 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6446 &it->bidi_it);
6447 }
6448 it->method = GET_FROM_C_STRING;
6449 }
6450
6451 /* PRECISION > 0 means don't return more than PRECISION characters
6452 from the string. */
6453 if (precision > 0 && it->end_charpos - charpos > precision)
6454 {
6455 it->end_charpos = it->string_nchars = charpos + precision;
6456 if (it->bidi_p)
6457 it->bidi_it.string.schars = it->end_charpos;
6458 }
6459
6460 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6461 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6462 FIELD_WIDTH < 0 means infinite field width. This is useful for
6463 padding with `-' at the end of a mode line. */
6464 if (field_width < 0)
6465 field_width = INFINITY;
6466 /* Implementation note: We deliberately don't enlarge
6467 it->bidi_it.string.schars here to fit it->end_charpos, because
6468 the bidi iterator cannot produce characters out of thin air. */
6469 if (field_width > it->end_charpos - charpos)
6470 it->end_charpos = charpos + field_width;
6471
6472 /* Use the standard display table for displaying strings. */
6473 if (DISP_TABLE_P (Vstandard_display_table))
6474 it->dp = XCHAR_TABLE (Vstandard_display_table);
6475
6476 it->stop_charpos = charpos;
6477 it->prev_stop = charpos;
6478 it->base_level_stop = 0;
6479 if (it->bidi_p)
6480 {
6481 it->bidi_it.first_elt = 1;
6482 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6483 it->bidi_it.disp_pos = -1;
6484 }
6485 if (s == NULL && it->multibyte_p)
6486 {
6487 ptrdiff_t endpos = SCHARS (it->string);
6488 if (endpos > it->end_charpos)
6489 endpos = it->end_charpos;
6490 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6491 it->string);
6492 }
6493 CHECK_IT (it);
6494 }
6495
6496
6497 \f
6498 /***********************************************************************
6499 Iteration
6500 ***********************************************************************/
6501
6502 /* Map enum it_method value to corresponding next_element_from_* function. */
6503
6504 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6505 {
6506 next_element_from_buffer,
6507 next_element_from_display_vector,
6508 next_element_from_string,
6509 next_element_from_c_string,
6510 next_element_from_image,
6511 next_element_from_stretch
6512 };
6513
6514 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6515
6516
6517 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6518 (possibly with the following characters). */
6519
6520 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6521 ((IT)->cmp_it.id >= 0 \
6522 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6523 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6524 END_CHARPOS, (IT)->w, \
6525 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6526 (IT)->string)))
6527
6528
6529 /* Lookup the char-table Vglyphless_char_display for character C (-1
6530 if we want information for no-font case), and return the display
6531 method symbol. By side-effect, update it->what and
6532 it->glyphless_method. This function is called from
6533 get_next_display_element for each character element, and from
6534 x_produce_glyphs when no suitable font was found. */
6535
6536 Lisp_Object
6537 lookup_glyphless_char_display (int c, struct it *it)
6538 {
6539 Lisp_Object glyphless_method = Qnil;
6540
6541 if (CHAR_TABLE_P (Vglyphless_char_display)
6542 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6543 {
6544 if (c >= 0)
6545 {
6546 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6547 if (CONSP (glyphless_method))
6548 glyphless_method = FRAME_WINDOW_P (it->f)
6549 ? XCAR (glyphless_method)
6550 : XCDR (glyphless_method);
6551 }
6552 else
6553 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6554 }
6555
6556 retry:
6557 if (NILP (glyphless_method))
6558 {
6559 if (c >= 0)
6560 /* The default is to display the character by a proper font. */
6561 return Qnil;
6562 /* The default for the no-font case is to display an empty box. */
6563 glyphless_method = Qempty_box;
6564 }
6565 if (EQ (glyphless_method, Qzero_width))
6566 {
6567 if (c >= 0)
6568 return glyphless_method;
6569 /* This method can't be used for the no-font case. */
6570 glyphless_method = Qempty_box;
6571 }
6572 if (EQ (glyphless_method, Qthin_space))
6573 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6574 else if (EQ (glyphless_method, Qempty_box))
6575 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6576 else if (EQ (glyphless_method, Qhex_code))
6577 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6578 else if (STRINGP (glyphless_method))
6579 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6580 else
6581 {
6582 /* Invalid value. We use the default method. */
6583 glyphless_method = Qnil;
6584 goto retry;
6585 }
6586 it->what = IT_GLYPHLESS;
6587 return glyphless_method;
6588 }
6589
6590 /* Load IT's display element fields with information about the next
6591 display element from the current position of IT. Value is zero if
6592 end of buffer (or C string) is reached. */
6593
6594 static struct frame *last_escape_glyph_frame = NULL;
6595 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6596 static int last_escape_glyph_merged_face_id = 0;
6597
6598 struct frame *last_glyphless_glyph_frame = NULL;
6599 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6600 int last_glyphless_glyph_merged_face_id = 0;
6601
6602 static int
6603 get_next_display_element (struct it *it)
6604 {
6605 /* Non-zero means that we found a display element. Zero means that
6606 we hit the end of what we iterate over. Performance note: the
6607 function pointer `method' used here turns out to be faster than
6608 using a sequence of if-statements. */
6609 int success_p;
6610
6611 get_next:
6612 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6613
6614 if (it->what == IT_CHARACTER)
6615 {
6616 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6617 and only if (a) the resolved directionality of that character
6618 is R..." */
6619 /* FIXME: Do we need an exception for characters from display
6620 tables? */
6621 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6622 it->c = bidi_mirror_char (it->c);
6623 /* Map via display table or translate control characters.
6624 IT->c, IT->len etc. have been set to the next character by
6625 the function call above. If we have a display table, and it
6626 contains an entry for IT->c, translate it. Don't do this if
6627 IT->c itself comes from a display table, otherwise we could
6628 end up in an infinite recursion. (An alternative could be to
6629 count the recursion depth of this function and signal an
6630 error when a certain maximum depth is reached.) Is it worth
6631 it? */
6632 if (success_p && it->dpvec == NULL)
6633 {
6634 Lisp_Object dv;
6635 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6636 int nonascii_space_p = 0;
6637 int nonascii_hyphen_p = 0;
6638 int c = it->c; /* This is the character to display. */
6639
6640 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6641 {
6642 eassert (SINGLE_BYTE_CHAR_P (c));
6643 if (unibyte_display_via_language_environment)
6644 {
6645 c = DECODE_CHAR (unibyte, c);
6646 if (c < 0)
6647 c = BYTE8_TO_CHAR (it->c);
6648 }
6649 else
6650 c = BYTE8_TO_CHAR (it->c);
6651 }
6652
6653 if (it->dp
6654 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6655 VECTORP (dv)))
6656 {
6657 struct Lisp_Vector *v = XVECTOR (dv);
6658
6659 /* Return the first character from the display table
6660 entry, if not empty. If empty, don't display the
6661 current character. */
6662 if (v->header.size)
6663 {
6664 it->dpvec_char_len = it->len;
6665 it->dpvec = v->contents;
6666 it->dpend = v->contents + v->header.size;
6667 it->current.dpvec_index = 0;
6668 it->dpvec_face_id = -1;
6669 it->saved_face_id = it->face_id;
6670 it->method = GET_FROM_DISPLAY_VECTOR;
6671 it->ellipsis_p = 0;
6672 }
6673 else
6674 {
6675 set_iterator_to_next (it, 0);
6676 }
6677 goto get_next;
6678 }
6679
6680 if (! NILP (lookup_glyphless_char_display (c, it)))
6681 {
6682 if (it->what == IT_GLYPHLESS)
6683 goto done;
6684 /* Don't display this character. */
6685 set_iterator_to_next (it, 0);
6686 goto get_next;
6687 }
6688
6689 /* If `nobreak-char-display' is non-nil, we display
6690 non-ASCII spaces and hyphens specially. */
6691 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6692 {
6693 if (c == 0xA0)
6694 nonascii_space_p = 1;
6695 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6696 nonascii_hyphen_p = 1;
6697 }
6698
6699 /* Translate control characters into `\003' or `^C' form.
6700 Control characters coming from a display table entry are
6701 currently not translated because we use IT->dpvec to hold
6702 the translation. This could easily be changed but I
6703 don't believe that it is worth doing.
6704
6705 The characters handled by `nobreak-char-display' must be
6706 translated too.
6707
6708 Non-printable characters and raw-byte characters are also
6709 translated to octal form. */
6710 if (((c < ' ' || c == 127) /* ASCII control chars */
6711 ? (it->area != TEXT_AREA
6712 /* In mode line, treat \n, \t like other crl chars. */
6713 || (c != '\t'
6714 && it->glyph_row
6715 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6716 || (c != '\n' && c != '\t'))
6717 : (nonascii_space_p
6718 || nonascii_hyphen_p
6719 || CHAR_BYTE8_P (c)
6720 || ! CHAR_PRINTABLE_P (c))))
6721 {
6722 /* C is a control character, non-ASCII space/hyphen,
6723 raw-byte, or a non-printable character which must be
6724 displayed either as '\003' or as `^C' where the '\\'
6725 and '^' can be defined in the display table. Fill
6726 IT->ctl_chars with glyphs for what we have to
6727 display. Then, set IT->dpvec to these glyphs. */
6728 Lisp_Object gc;
6729 int ctl_len;
6730 int face_id;
6731 int lface_id = 0;
6732 int escape_glyph;
6733
6734 /* Handle control characters with ^. */
6735
6736 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6737 {
6738 int g;
6739
6740 g = '^'; /* default glyph for Control */
6741 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6742 if (it->dp
6743 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6744 {
6745 g = GLYPH_CODE_CHAR (gc);
6746 lface_id = GLYPH_CODE_FACE (gc);
6747 }
6748 if (lface_id)
6749 {
6750 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6751 }
6752 else if (it->f == last_escape_glyph_frame
6753 && it->face_id == last_escape_glyph_face_id)
6754 {
6755 face_id = last_escape_glyph_merged_face_id;
6756 }
6757 else
6758 {
6759 /* Merge the escape-glyph face into the current face. */
6760 face_id = merge_faces (it->f, Qescape_glyph, 0,
6761 it->face_id);
6762 last_escape_glyph_frame = it->f;
6763 last_escape_glyph_face_id = it->face_id;
6764 last_escape_glyph_merged_face_id = face_id;
6765 }
6766
6767 XSETINT (it->ctl_chars[0], g);
6768 XSETINT (it->ctl_chars[1], c ^ 0100);
6769 ctl_len = 2;
6770 goto display_control;
6771 }
6772
6773 /* Handle non-ascii space in the mode where it only gets
6774 highlighting. */
6775
6776 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6777 {
6778 /* Merge `nobreak-space' into the current face. */
6779 face_id = merge_faces (it->f, Qnobreak_space, 0,
6780 it->face_id);
6781 XSETINT (it->ctl_chars[0], ' ');
6782 ctl_len = 1;
6783 goto display_control;
6784 }
6785
6786 /* Handle sequences that start with the "escape glyph". */
6787
6788 /* the default escape glyph is \. */
6789 escape_glyph = '\\';
6790
6791 if (it->dp
6792 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6793 {
6794 escape_glyph = GLYPH_CODE_CHAR (gc);
6795 lface_id = GLYPH_CODE_FACE (gc);
6796 }
6797 if (lface_id)
6798 {
6799 /* The display table specified a face.
6800 Merge it into face_id and also into escape_glyph. */
6801 face_id = merge_faces (it->f, Qt, lface_id,
6802 it->face_id);
6803 }
6804 else if (it->f == last_escape_glyph_frame
6805 && it->face_id == last_escape_glyph_face_id)
6806 {
6807 face_id = last_escape_glyph_merged_face_id;
6808 }
6809 else
6810 {
6811 /* Merge the escape-glyph face into the current face. */
6812 face_id = merge_faces (it->f, Qescape_glyph, 0,
6813 it->face_id);
6814 last_escape_glyph_frame = it->f;
6815 last_escape_glyph_face_id = it->face_id;
6816 last_escape_glyph_merged_face_id = face_id;
6817 }
6818
6819 /* Draw non-ASCII hyphen with just highlighting: */
6820
6821 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6822 {
6823 XSETINT (it->ctl_chars[0], '-');
6824 ctl_len = 1;
6825 goto display_control;
6826 }
6827
6828 /* Draw non-ASCII space/hyphen with escape glyph: */
6829
6830 if (nonascii_space_p || nonascii_hyphen_p)
6831 {
6832 XSETINT (it->ctl_chars[0], escape_glyph);
6833 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6834 ctl_len = 2;
6835 goto display_control;
6836 }
6837
6838 {
6839 char str[10];
6840 int len, i;
6841
6842 if (CHAR_BYTE8_P (c))
6843 /* Display \200 instead of \17777600. */
6844 c = CHAR_TO_BYTE8 (c);
6845 len = sprintf (str, "%03o", c);
6846
6847 XSETINT (it->ctl_chars[0], escape_glyph);
6848 for (i = 0; i < len; i++)
6849 XSETINT (it->ctl_chars[i + 1], str[i]);
6850 ctl_len = len + 1;
6851 }
6852
6853 display_control:
6854 /* Set up IT->dpvec and return first character from it. */
6855 it->dpvec_char_len = it->len;
6856 it->dpvec = it->ctl_chars;
6857 it->dpend = it->dpvec + ctl_len;
6858 it->current.dpvec_index = 0;
6859 it->dpvec_face_id = face_id;
6860 it->saved_face_id = it->face_id;
6861 it->method = GET_FROM_DISPLAY_VECTOR;
6862 it->ellipsis_p = 0;
6863 goto get_next;
6864 }
6865 it->char_to_display = c;
6866 }
6867 else if (success_p)
6868 {
6869 it->char_to_display = it->c;
6870 }
6871 }
6872
6873 /* Adjust face id for a multibyte character. There are no multibyte
6874 character in unibyte text. */
6875 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6876 && it->multibyte_p
6877 && success_p
6878 && FRAME_WINDOW_P (it->f))
6879 {
6880 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6881
6882 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6883 {
6884 /* Automatic composition with glyph-string. */
6885 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6886
6887 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6888 }
6889 else
6890 {
6891 ptrdiff_t pos = (it->s ? -1
6892 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6893 : IT_CHARPOS (*it));
6894 int c;
6895
6896 if (it->what == IT_CHARACTER)
6897 c = it->char_to_display;
6898 else
6899 {
6900 struct composition *cmp = composition_table[it->cmp_it.id];
6901 int i;
6902
6903 c = ' ';
6904 for (i = 0; i < cmp->glyph_len; i++)
6905 /* TAB in a composition means display glyphs with
6906 padding space on the left or right. */
6907 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6908 break;
6909 }
6910 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6911 }
6912 }
6913
6914 done:
6915 /* Is this character the last one of a run of characters with
6916 box? If yes, set IT->end_of_box_run_p to 1. */
6917 if (it->face_box_p
6918 && it->s == NULL)
6919 {
6920 if (it->method == GET_FROM_STRING && it->sp)
6921 {
6922 int face_id = underlying_face_id (it);
6923 struct face *face = FACE_FROM_ID (it->f, face_id);
6924
6925 if (face)
6926 {
6927 if (face->box == FACE_NO_BOX)
6928 {
6929 /* If the box comes from face properties in a
6930 display string, check faces in that string. */
6931 int string_face_id = face_after_it_pos (it);
6932 it->end_of_box_run_p
6933 = (FACE_FROM_ID (it->f, string_face_id)->box
6934 == FACE_NO_BOX);
6935 }
6936 /* Otherwise, the box comes from the underlying face.
6937 If this is the last string character displayed, check
6938 the next buffer location. */
6939 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6940 && (it->current.overlay_string_index
6941 == it->n_overlay_strings - 1))
6942 {
6943 ptrdiff_t ignore;
6944 int next_face_id;
6945 struct text_pos pos = it->current.pos;
6946 INC_TEXT_POS (pos, it->multibyte_p);
6947
6948 next_face_id = face_at_buffer_position
6949 (it->w, CHARPOS (pos), it->region_beg_charpos,
6950 it->region_end_charpos, &ignore,
6951 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6952 -1);
6953 it->end_of_box_run_p
6954 = (FACE_FROM_ID (it->f, next_face_id)->box
6955 == FACE_NO_BOX);
6956 }
6957 }
6958 }
6959 else
6960 {
6961 int face_id = face_after_it_pos (it);
6962 it->end_of_box_run_p
6963 = (face_id != it->face_id
6964 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6965 }
6966 }
6967 /* If we reached the end of the object we've been iterating (e.g., a
6968 display string or an overlay string), and there's something on
6969 IT->stack, proceed with what's on the stack. It doesn't make
6970 sense to return zero if there's unprocessed stuff on the stack,
6971 because otherwise that stuff will never be displayed. */
6972 if (!success_p && it->sp > 0)
6973 {
6974 set_iterator_to_next (it, 0);
6975 success_p = get_next_display_element (it);
6976 }
6977
6978 /* Value is 0 if end of buffer or string reached. */
6979 return success_p;
6980 }
6981
6982
6983 /* Move IT to the next display element.
6984
6985 RESEAT_P non-zero means if called on a newline in buffer text,
6986 skip to the next visible line start.
6987
6988 Functions get_next_display_element and set_iterator_to_next are
6989 separate because I find this arrangement easier to handle than a
6990 get_next_display_element function that also increments IT's
6991 position. The way it is we can first look at an iterator's current
6992 display element, decide whether it fits on a line, and if it does,
6993 increment the iterator position. The other way around we probably
6994 would either need a flag indicating whether the iterator has to be
6995 incremented the next time, or we would have to implement a
6996 decrement position function which would not be easy to write. */
6997
6998 void
6999 set_iterator_to_next (struct it *it, int reseat_p)
7000 {
7001 /* Reset flags indicating start and end of a sequence of characters
7002 with box. Reset them at the start of this function because
7003 moving the iterator to a new position might set them. */
7004 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7005
7006 switch (it->method)
7007 {
7008 case GET_FROM_BUFFER:
7009 /* The current display element of IT is a character from
7010 current_buffer. Advance in the buffer, and maybe skip over
7011 invisible lines that are so because of selective display. */
7012 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7013 reseat_at_next_visible_line_start (it, 0);
7014 else if (it->cmp_it.id >= 0)
7015 {
7016 /* We are currently getting glyphs from a composition. */
7017 int i;
7018
7019 if (! it->bidi_p)
7020 {
7021 IT_CHARPOS (*it) += it->cmp_it.nchars;
7022 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7023 if (it->cmp_it.to < it->cmp_it.nglyphs)
7024 {
7025 it->cmp_it.from = it->cmp_it.to;
7026 }
7027 else
7028 {
7029 it->cmp_it.id = -1;
7030 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7031 IT_BYTEPOS (*it),
7032 it->end_charpos, Qnil);
7033 }
7034 }
7035 else if (! it->cmp_it.reversed_p)
7036 {
7037 /* Composition created while scanning forward. */
7038 /* Update IT's char/byte positions to point to the first
7039 character of the next grapheme cluster, or to the
7040 character visually after the current composition. */
7041 for (i = 0; i < it->cmp_it.nchars; i++)
7042 bidi_move_to_visually_next (&it->bidi_it);
7043 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7044 IT_CHARPOS (*it) = it->bidi_it.charpos;
7045
7046 if (it->cmp_it.to < it->cmp_it.nglyphs)
7047 {
7048 /* Proceed to the next grapheme cluster. */
7049 it->cmp_it.from = it->cmp_it.to;
7050 }
7051 else
7052 {
7053 /* No more grapheme clusters in this composition.
7054 Find the next stop position. */
7055 ptrdiff_t stop = it->end_charpos;
7056 if (it->bidi_it.scan_dir < 0)
7057 /* Now we are scanning backward and don't know
7058 where to stop. */
7059 stop = -1;
7060 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7061 IT_BYTEPOS (*it), stop, Qnil);
7062 }
7063 }
7064 else
7065 {
7066 /* Composition created while scanning backward. */
7067 /* Update IT's char/byte positions to point to the last
7068 character of the previous grapheme cluster, or the
7069 character visually after the current composition. */
7070 for (i = 0; i < it->cmp_it.nchars; i++)
7071 bidi_move_to_visually_next (&it->bidi_it);
7072 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7073 IT_CHARPOS (*it) = it->bidi_it.charpos;
7074 if (it->cmp_it.from > 0)
7075 {
7076 /* Proceed to the previous grapheme cluster. */
7077 it->cmp_it.to = it->cmp_it.from;
7078 }
7079 else
7080 {
7081 /* No more grapheme clusters in this composition.
7082 Find the next stop position. */
7083 ptrdiff_t stop = it->end_charpos;
7084 if (it->bidi_it.scan_dir < 0)
7085 /* Now we are scanning backward and don't know
7086 where to stop. */
7087 stop = -1;
7088 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7089 IT_BYTEPOS (*it), stop, Qnil);
7090 }
7091 }
7092 }
7093 else
7094 {
7095 eassert (it->len != 0);
7096
7097 if (!it->bidi_p)
7098 {
7099 IT_BYTEPOS (*it) += it->len;
7100 IT_CHARPOS (*it) += 1;
7101 }
7102 else
7103 {
7104 int prev_scan_dir = it->bidi_it.scan_dir;
7105 /* If this is a new paragraph, determine its base
7106 direction (a.k.a. its base embedding level). */
7107 if (it->bidi_it.new_paragraph)
7108 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7109 bidi_move_to_visually_next (&it->bidi_it);
7110 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7111 IT_CHARPOS (*it) = it->bidi_it.charpos;
7112 if (prev_scan_dir != it->bidi_it.scan_dir)
7113 {
7114 /* As the scan direction was changed, we must
7115 re-compute the stop position for composition. */
7116 ptrdiff_t stop = it->end_charpos;
7117 if (it->bidi_it.scan_dir < 0)
7118 stop = -1;
7119 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7120 IT_BYTEPOS (*it), stop, Qnil);
7121 }
7122 }
7123 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7124 }
7125 break;
7126
7127 case GET_FROM_C_STRING:
7128 /* Current display element of IT is from a C string. */
7129 if (!it->bidi_p
7130 /* If the string position is beyond string's end, it means
7131 next_element_from_c_string is padding the string with
7132 blanks, in which case we bypass the bidi iterator,
7133 because it cannot deal with such virtual characters. */
7134 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7135 {
7136 IT_BYTEPOS (*it) += it->len;
7137 IT_CHARPOS (*it) += 1;
7138 }
7139 else
7140 {
7141 bidi_move_to_visually_next (&it->bidi_it);
7142 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7143 IT_CHARPOS (*it) = it->bidi_it.charpos;
7144 }
7145 break;
7146
7147 case GET_FROM_DISPLAY_VECTOR:
7148 /* Current display element of IT is from a display table entry.
7149 Advance in the display table definition. Reset it to null if
7150 end reached, and continue with characters from buffers/
7151 strings. */
7152 ++it->current.dpvec_index;
7153
7154 /* Restore face of the iterator to what they were before the
7155 display vector entry (these entries may contain faces). */
7156 it->face_id = it->saved_face_id;
7157
7158 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7159 {
7160 int recheck_faces = it->ellipsis_p;
7161
7162 if (it->s)
7163 it->method = GET_FROM_C_STRING;
7164 else if (STRINGP (it->string))
7165 it->method = GET_FROM_STRING;
7166 else
7167 {
7168 it->method = GET_FROM_BUFFER;
7169 it->object = it->w->buffer;
7170 }
7171
7172 it->dpvec = NULL;
7173 it->current.dpvec_index = -1;
7174
7175 /* Skip over characters which were displayed via IT->dpvec. */
7176 if (it->dpvec_char_len < 0)
7177 reseat_at_next_visible_line_start (it, 1);
7178 else if (it->dpvec_char_len > 0)
7179 {
7180 if (it->method == GET_FROM_STRING
7181 && it->n_overlay_strings > 0)
7182 it->ignore_overlay_strings_at_pos_p = 1;
7183 it->len = it->dpvec_char_len;
7184 set_iterator_to_next (it, reseat_p);
7185 }
7186
7187 /* Maybe recheck faces after display vector */
7188 if (recheck_faces)
7189 it->stop_charpos = IT_CHARPOS (*it);
7190 }
7191 break;
7192
7193 case GET_FROM_STRING:
7194 /* Current display element is a character from a Lisp string. */
7195 eassert (it->s == NULL && STRINGP (it->string));
7196 /* Don't advance past string end. These conditions are true
7197 when set_iterator_to_next is called at the end of
7198 get_next_display_element, in which case the Lisp string is
7199 already exhausted, and all we want is pop the iterator
7200 stack. */
7201 if (it->current.overlay_string_index >= 0)
7202 {
7203 /* This is an overlay string, so there's no padding with
7204 spaces, and the number of characters in the string is
7205 where the string ends. */
7206 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7207 goto consider_string_end;
7208 }
7209 else
7210 {
7211 /* Not an overlay string. There could be padding, so test
7212 against it->end_charpos . */
7213 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7214 goto consider_string_end;
7215 }
7216 if (it->cmp_it.id >= 0)
7217 {
7218 int i;
7219
7220 if (! it->bidi_p)
7221 {
7222 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7223 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7224 if (it->cmp_it.to < it->cmp_it.nglyphs)
7225 it->cmp_it.from = it->cmp_it.to;
7226 else
7227 {
7228 it->cmp_it.id = -1;
7229 composition_compute_stop_pos (&it->cmp_it,
7230 IT_STRING_CHARPOS (*it),
7231 IT_STRING_BYTEPOS (*it),
7232 it->end_charpos, it->string);
7233 }
7234 }
7235 else if (! it->cmp_it.reversed_p)
7236 {
7237 for (i = 0; i < it->cmp_it.nchars; i++)
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7241
7242 if (it->cmp_it.to < it->cmp_it.nglyphs)
7243 it->cmp_it.from = it->cmp_it.to;
7244 else
7245 {
7246 ptrdiff_t stop = it->end_charpos;
7247 if (it->bidi_it.scan_dir < 0)
7248 stop = -1;
7249 composition_compute_stop_pos (&it->cmp_it,
7250 IT_STRING_CHARPOS (*it),
7251 IT_STRING_BYTEPOS (*it), stop,
7252 it->string);
7253 }
7254 }
7255 else
7256 {
7257 for (i = 0; i < it->cmp_it.nchars; i++)
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7261 if (it->cmp_it.from > 0)
7262 it->cmp_it.to = it->cmp_it.from;
7263 else
7264 {
7265 ptrdiff_t stop = it->end_charpos;
7266 if (it->bidi_it.scan_dir < 0)
7267 stop = -1;
7268 composition_compute_stop_pos (&it->cmp_it,
7269 IT_STRING_CHARPOS (*it),
7270 IT_STRING_BYTEPOS (*it), stop,
7271 it->string);
7272 }
7273 }
7274 }
7275 else
7276 {
7277 if (!it->bidi_p
7278 /* If the string position is beyond string's end, it
7279 means next_element_from_string is padding the string
7280 with blanks, in which case we bypass the bidi
7281 iterator, because it cannot deal with such virtual
7282 characters. */
7283 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7284 {
7285 IT_STRING_BYTEPOS (*it) += it->len;
7286 IT_STRING_CHARPOS (*it) += 1;
7287 }
7288 else
7289 {
7290 int prev_scan_dir = it->bidi_it.scan_dir;
7291
7292 bidi_move_to_visually_next (&it->bidi_it);
7293 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7294 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7295 if (prev_scan_dir != it->bidi_it.scan_dir)
7296 {
7297 ptrdiff_t stop = it->end_charpos;
7298
7299 if (it->bidi_it.scan_dir < 0)
7300 stop = -1;
7301 composition_compute_stop_pos (&it->cmp_it,
7302 IT_STRING_CHARPOS (*it),
7303 IT_STRING_BYTEPOS (*it), stop,
7304 it->string);
7305 }
7306 }
7307 }
7308
7309 consider_string_end:
7310
7311 if (it->current.overlay_string_index >= 0)
7312 {
7313 /* IT->string is an overlay string. Advance to the
7314 next, if there is one. */
7315 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7316 {
7317 it->ellipsis_p = 0;
7318 next_overlay_string (it);
7319 if (it->ellipsis_p)
7320 setup_for_ellipsis (it, 0);
7321 }
7322 }
7323 else
7324 {
7325 /* IT->string is not an overlay string. If we reached
7326 its end, and there is something on IT->stack, proceed
7327 with what is on the stack. This can be either another
7328 string, this time an overlay string, or a buffer. */
7329 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7330 && it->sp > 0)
7331 {
7332 pop_it (it);
7333 if (it->method == GET_FROM_STRING)
7334 goto consider_string_end;
7335 }
7336 }
7337 break;
7338
7339 case GET_FROM_IMAGE:
7340 case GET_FROM_STRETCH:
7341 /* The position etc with which we have to proceed are on
7342 the stack. The position may be at the end of a string,
7343 if the `display' property takes up the whole string. */
7344 eassert (it->sp > 0);
7345 pop_it (it);
7346 if (it->method == GET_FROM_STRING)
7347 goto consider_string_end;
7348 break;
7349
7350 default:
7351 /* There are no other methods defined, so this should be a bug. */
7352 emacs_abort ();
7353 }
7354
7355 eassert (it->method != GET_FROM_STRING
7356 || (STRINGP (it->string)
7357 && IT_STRING_CHARPOS (*it) >= 0));
7358 }
7359
7360 /* Load IT's display element fields with information about the next
7361 display element which comes from a display table entry or from the
7362 result of translating a control character to one of the forms `^C'
7363 or `\003'.
7364
7365 IT->dpvec holds the glyphs to return as characters.
7366 IT->saved_face_id holds the face id before the display vector--it
7367 is restored into IT->face_id in set_iterator_to_next. */
7368
7369 static int
7370 next_element_from_display_vector (struct it *it)
7371 {
7372 Lisp_Object gc;
7373
7374 /* Precondition. */
7375 eassert (it->dpvec && it->current.dpvec_index >= 0);
7376
7377 it->face_id = it->saved_face_id;
7378
7379 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7380 That seemed totally bogus - so I changed it... */
7381 gc = it->dpvec[it->current.dpvec_index];
7382
7383 if (GLYPH_CODE_P (gc))
7384 {
7385 it->c = GLYPH_CODE_CHAR (gc);
7386 it->len = CHAR_BYTES (it->c);
7387
7388 /* The entry may contain a face id to use. Such a face id is
7389 the id of a Lisp face, not a realized face. A face id of
7390 zero means no face is specified. */
7391 if (it->dpvec_face_id >= 0)
7392 it->face_id = it->dpvec_face_id;
7393 else
7394 {
7395 int lface_id = GLYPH_CODE_FACE (gc);
7396 if (lface_id > 0)
7397 it->face_id = merge_faces (it->f, Qt, lface_id,
7398 it->saved_face_id);
7399 }
7400 }
7401 else
7402 /* Display table entry is invalid. Return a space. */
7403 it->c = ' ', it->len = 1;
7404
7405 /* Don't change position and object of the iterator here. They are
7406 still the values of the character that had this display table
7407 entry or was translated, and that's what we want. */
7408 it->what = IT_CHARACTER;
7409 return 1;
7410 }
7411
7412 /* Get the first element of string/buffer in the visual order, after
7413 being reseated to a new position in a string or a buffer. */
7414 static void
7415 get_visually_first_element (struct it *it)
7416 {
7417 int string_p = STRINGP (it->string) || it->s;
7418 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7419 ptrdiff_t bob = (string_p ? 0 : BEGV);
7420
7421 if (STRINGP (it->string))
7422 {
7423 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7424 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7425 }
7426 else
7427 {
7428 it->bidi_it.charpos = IT_CHARPOS (*it);
7429 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7430 }
7431
7432 if (it->bidi_it.charpos == eob)
7433 {
7434 /* Nothing to do, but reset the FIRST_ELT flag, like
7435 bidi_paragraph_init does, because we are not going to
7436 call it. */
7437 it->bidi_it.first_elt = 0;
7438 }
7439 else if (it->bidi_it.charpos == bob
7440 || (!string_p
7441 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7442 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7443 {
7444 /* If we are at the beginning of a line/string, we can produce
7445 the next element right away. */
7446 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7447 bidi_move_to_visually_next (&it->bidi_it);
7448 }
7449 else
7450 {
7451 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7452
7453 /* We need to prime the bidi iterator starting at the line's or
7454 string's beginning, before we will be able to produce the
7455 next element. */
7456 if (string_p)
7457 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7458 else
7459 {
7460 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7461 -1);
7462 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7463 }
7464 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7465 do
7466 {
7467 /* Now return to buffer/string position where we were asked
7468 to get the next display element, and produce that. */
7469 bidi_move_to_visually_next (&it->bidi_it);
7470 }
7471 while (it->bidi_it.bytepos != orig_bytepos
7472 && it->bidi_it.charpos < eob);
7473 }
7474
7475 /* Adjust IT's position information to where we ended up. */
7476 if (STRINGP (it->string))
7477 {
7478 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7479 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7480 }
7481 else
7482 {
7483 IT_CHARPOS (*it) = it->bidi_it.charpos;
7484 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7485 }
7486
7487 if (STRINGP (it->string) || !it->s)
7488 {
7489 ptrdiff_t stop, charpos, bytepos;
7490
7491 if (STRINGP (it->string))
7492 {
7493 eassert (!it->s);
7494 stop = SCHARS (it->string);
7495 if (stop > it->end_charpos)
7496 stop = it->end_charpos;
7497 charpos = IT_STRING_CHARPOS (*it);
7498 bytepos = IT_STRING_BYTEPOS (*it);
7499 }
7500 else
7501 {
7502 stop = it->end_charpos;
7503 charpos = IT_CHARPOS (*it);
7504 bytepos = IT_BYTEPOS (*it);
7505 }
7506 if (it->bidi_it.scan_dir < 0)
7507 stop = -1;
7508 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7509 it->string);
7510 }
7511 }
7512
7513 /* Load IT with the next display element from Lisp string IT->string.
7514 IT->current.string_pos is the current position within the string.
7515 If IT->current.overlay_string_index >= 0, the Lisp string is an
7516 overlay string. */
7517
7518 static int
7519 next_element_from_string (struct it *it)
7520 {
7521 struct text_pos position;
7522
7523 eassert (STRINGP (it->string));
7524 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7525 eassert (IT_STRING_CHARPOS (*it) >= 0);
7526 position = it->current.string_pos;
7527
7528 /* With bidi reordering, the character to display might not be the
7529 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7530 that we were reseat()ed to a new string, whose paragraph
7531 direction is not known. */
7532 if (it->bidi_p && it->bidi_it.first_elt)
7533 {
7534 get_visually_first_element (it);
7535 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7536 }
7537
7538 /* Time to check for invisible text? */
7539 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7540 {
7541 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7542 {
7543 if (!(!it->bidi_p
7544 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7545 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7546 {
7547 /* With bidi non-linear iteration, we could find
7548 ourselves far beyond the last computed stop_charpos,
7549 with several other stop positions in between that we
7550 missed. Scan them all now, in buffer's logical
7551 order, until we find and handle the last stop_charpos
7552 that precedes our current position. */
7553 handle_stop_backwards (it, it->stop_charpos);
7554 return GET_NEXT_DISPLAY_ELEMENT (it);
7555 }
7556 else
7557 {
7558 if (it->bidi_p)
7559 {
7560 /* Take note of the stop position we just moved
7561 across, for when we will move back across it. */
7562 it->prev_stop = it->stop_charpos;
7563 /* If we are at base paragraph embedding level, take
7564 note of the last stop position seen at this
7565 level. */
7566 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7567 it->base_level_stop = it->stop_charpos;
7568 }
7569 handle_stop (it);
7570
7571 /* Since a handler may have changed IT->method, we must
7572 recurse here. */
7573 return GET_NEXT_DISPLAY_ELEMENT (it);
7574 }
7575 }
7576 else if (it->bidi_p
7577 /* If we are before prev_stop, we may have overstepped
7578 on our way backwards a stop_pos, and if so, we need
7579 to handle that stop_pos. */
7580 && IT_STRING_CHARPOS (*it) < it->prev_stop
7581 /* We can sometimes back up for reasons that have nothing
7582 to do with bidi reordering. E.g., compositions. The
7583 code below is only needed when we are above the base
7584 embedding level, so test for that explicitly. */
7585 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7586 {
7587 /* If we lost track of base_level_stop, we have no better
7588 place for handle_stop_backwards to start from than string
7589 beginning. This happens, e.g., when we were reseated to
7590 the previous screenful of text by vertical-motion. */
7591 if (it->base_level_stop <= 0
7592 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7593 it->base_level_stop = 0;
7594 handle_stop_backwards (it, it->base_level_stop);
7595 return GET_NEXT_DISPLAY_ELEMENT (it);
7596 }
7597 }
7598
7599 if (it->current.overlay_string_index >= 0)
7600 {
7601 /* Get the next character from an overlay string. In overlay
7602 strings, there is no field width or padding with spaces to
7603 do. */
7604 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7605 {
7606 it->what = IT_EOB;
7607 return 0;
7608 }
7609 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7610 IT_STRING_BYTEPOS (*it),
7611 it->bidi_it.scan_dir < 0
7612 ? -1
7613 : SCHARS (it->string))
7614 && next_element_from_composition (it))
7615 {
7616 return 1;
7617 }
7618 else if (STRING_MULTIBYTE (it->string))
7619 {
7620 const unsigned char *s = (SDATA (it->string)
7621 + IT_STRING_BYTEPOS (*it));
7622 it->c = string_char_and_length (s, &it->len);
7623 }
7624 else
7625 {
7626 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7627 it->len = 1;
7628 }
7629 }
7630 else
7631 {
7632 /* Get the next character from a Lisp string that is not an
7633 overlay string. Such strings come from the mode line, for
7634 example. We may have to pad with spaces, or truncate the
7635 string. See also next_element_from_c_string. */
7636 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7637 {
7638 it->what = IT_EOB;
7639 return 0;
7640 }
7641 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7642 {
7643 /* Pad with spaces. */
7644 it->c = ' ', it->len = 1;
7645 CHARPOS (position) = BYTEPOS (position) = -1;
7646 }
7647 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7648 IT_STRING_BYTEPOS (*it),
7649 it->bidi_it.scan_dir < 0
7650 ? -1
7651 : it->string_nchars)
7652 && next_element_from_composition (it))
7653 {
7654 return 1;
7655 }
7656 else if (STRING_MULTIBYTE (it->string))
7657 {
7658 const unsigned char *s = (SDATA (it->string)
7659 + IT_STRING_BYTEPOS (*it));
7660 it->c = string_char_and_length (s, &it->len);
7661 }
7662 else
7663 {
7664 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7665 it->len = 1;
7666 }
7667 }
7668
7669 /* Record what we have and where it came from. */
7670 it->what = IT_CHARACTER;
7671 it->object = it->string;
7672 it->position = position;
7673 return 1;
7674 }
7675
7676
7677 /* Load IT with next display element from C string IT->s.
7678 IT->string_nchars is the maximum number of characters to return
7679 from the string. IT->end_charpos may be greater than
7680 IT->string_nchars when this function is called, in which case we
7681 may have to return padding spaces. Value is zero if end of string
7682 reached, including padding spaces. */
7683
7684 static int
7685 next_element_from_c_string (struct it *it)
7686 {
7687 int success_p = 1;
7688
7689 eassert (it->s);
7690 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7691 it->what = IT_CHARACTER;
7692 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7693 it->object = Qnil;
7694
7695 /* With bidi reordering, the character to display might not be the
7696 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7697 we were reseated to a new string, whose paragraph direction is
7698 not known. */
7699 if (it->bidi_p && it->bidi_it.first_elt)
7700 get_visually_first_element (it);
7701
7702 /* IT's position can be greater than IT->string_nchars in case a
7703 field width or precision has been specified when the iterator was
7704 initialized. */
7705 if (IT_CHARPOS (*it) >= it->end_charpos)
7706 {
7707 /* End of the game. */
7708 it->what = IT_EOB;
7709 success_p = 0;
7710 }
7711 else if (IT_CHARPOS (*it) >= it->string_nchars)
7712 {
7713 /* Pad with spaces. */
7714 it->c = ' ', it->len = 1;
7715 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7716 }
7717 else if (it->multibyte_p)
7718 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7719 else
7720 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7721
7722 return success_p;
7723 }
7724
7725
7726 /* Set up IT to return characters from an ellipsis, if appropriate.
7727 The definition of the ellipsis glyphs may come from a display table
7728 entry. This function fills IT with the first glyph from the
7729 ellipsis if an ellipsis is to be displayed. */
7730
7731 static int
7732 next_element_from_ellipsis (struct it *it)
7733 {
7734 if (it->selective_display_ellipsis_p)
7735 setup_for_ellipsis (it, it->len);
7736 else
7737 {
7738 /* The face at the current position may be different from the
7739 face we find after the invisible text. Remember what it
7740 was in IT->saved_face_id, and signal that it's there by
7741 setting face_before_selective_p. */
7742 it->saved_face_id = it->face_id;
7743 it->method = GET_FROM_BUFFER;
7744 it->object = it->w->buffer;
7745 reseat_at_next_visible_line_start (it, 1);
7746 it->face_before_selective_p = 1;
7747 }
7748
7749 return GET_NEXT_DISPLAY_ELEMENT (it);
7750 }
7751
7752
7753 /* Deliver an image display element. The iterator IT is already
7754 filled with image information (done in handle_display_prop). Value
7755 is always 1. */
7756
7757
7758 static int
7759 next_element_from_image (struct it *it)
7760 {
7761 it->what = IT_IMAGE;
7762 it->ignore_overlay_strings_at_pos_p = 0;
7763 return 1;
7764 }
7765
7766
7767 /* Fill iterator IT with next display element from a stretch glyph
7768 property. IT->object is the value of the text property. Value is
7769 always 1. */
7770
7771 static int
7772 next_element_from_stretch (struct it *it)
7773 {
7774 it->what = IT_STRETCH;
7775 return 1;
7776 }
7777
7778 /* Scan backwards from IT's current position until we find a stop
7779 position, or until BEGV. This is called when we find ourself
7780 before both the last known prev_stop and base_level_stop while
7781 reordering bidirectional text. */
7782
7783 static void
7784 compute_stop_pos_backwards (struct it *it)
7785 {
7786 const int SCAN_BACK_LIMIT = 1000;
7787 struct text_pos pos;
7788 struct display_pos save_current = it->current;
7789 struct text_pos save_position = it->position;
7790 ptrdiff_t charpos = IT_CHARPOS (*it);
7791 ptrdiff_t where_we_are = charpos;
7792 ptrdiff_t save_stop_pos = it->stop_charpos;
7793 ptrdiff_t save_end_pos = it->end_charpos;
7794
7795 eassert (NILP (it->string) && !it->s);
7796 eassert (it->bidi_p);
7797 it->bidi_p = 0;
7798 do
7799 {
7800 it->end_charpos = min (charpos + 1, ZV);
7801 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7802 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7803 reseat_1 (it, pos, 0);
7804 compute_stop_pos (it);
7805 /* We must advance forward, right? */
7806 if (it->stop_charpos <= charpos)
7807 emacs_abort ();
7808 }
7809 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7810
7811 if (it->stop_charpos <= where_we_are)
7812 it->prev_stop = it->stop_charpos;
7813 else
7814 it->prev_stop = BEGV;
7815 it->bidi_p = 1;
7816 it->current = save_current;
7817 it->position = save_position;
7818 it->stop_charpos = save_stop_pos;
7819 it->end_charpos = save_end_pos;
7820 }
7821
7822 /* Scan forward from CHARPOS in the current buffer/string, until we
7823 find a stop position > current IT's position. Then handle the stop
7824 position before that. This is called when we bump into a stop
7825 position while reordering bidirectional text. CHARPOS should be
7826 the last previously processed stop_pos (or BEGV/0, if none were
7827 processed yet) whose position is less that IT's current
7828 position. */
7829
7830 static void
7831 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7832 {
7833 int bufp = !STRINGP (it->string);
7834 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7835 struct display_pos save_current = it->current;
7836 struct text_pos save_position = it->position;
7837 struct text_pos pos1;
7838 ptrdiff_t next_stop;
7839
7840 /* Scan in strict logical order. */
7841 eassert (it->bidi_p);
7842 it->bidi_p = 0;
7843 do
7844 {
7845 it->prev_stop = charpos;
7846 if (bufp)
7847 {
7848 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7849 reseat_1 (it, pos1, 0);
7850 }
7851 else
7852 it->current.string_pos = string_pos (charpos, it->string);
7853 compute_stop_pos (it);
7854 /* We must advance forward, right? */
7855 if (it->stop_charpos <= it->prev_stop)
7856 emacs_abort ();
7857 charpos = it->stop_charpos;
7858 }
7859 while (charpos <= where_we_are);
7860
7861 it->bidi_p = 1;
7862 it->current = save_current;
7863 it->position = save_position;
7864 next_stop = it->stop_charpos;
7865 it->stop_charpos = it->prev_stop;
7866 handle_stop (it);
7867 it->stop_charpos = next_stop;
7868 }
7869
7870 /* Load IT with the next display element from current_buffer. Value
7871 is zero if end of buffer reached. IT->stop_charpos is the next
7872 position at which to stop and check for text properties or buffer
7873 end. */
7874
7875 static int
7876 next_element_from_buffer (struct it *it)
7877 {
7878 int success_p = 1;
7879
7880 eassert (IT_CHARPOS (*it) >= BEGV);
7881 eassert (NILP (it->string) && !it->s);
7882 eassert (!it->bidi_p
7883 || (EQ (it->bidi_it.string.lstring, Qnil)
7884 && it->bidi_it.string.s == NULL));
7885
7886 /* With bidi reordering, the character to display might not be the
7887 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7888 we were reseat()ed to a new buffer position, which is potentially
7889 a different paragraph. */
7890 if (it->bidi_p && it->bidi_it.first_elt)
7891 {
7892 get_visually_first_element (it);
7893 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7894 }
7895
7896 if (IT_CHARPOS (*it) >= it->stop_charpos)
7897 {
7898 if (IT_CHARPOS (*it) >= it->end_charpos)
7899 {
7900 int overlay_strings_follow_p;
7901
7902 /* End of the game, except when overlay strings follow that
7903 haven't been returned yet. */
7904 if (it->overlay_strings_at_end_processed_p)
7905 overlay_strings_follow_p = 0;
7906 else
7907 {
7908 it->overlay_strings_at_end_processed_p = 1;
7909 overlay_strings_follow_p = get_overlay_strings (it, 0);
7910 }
7911
7912 if (overlay_strings_follow_p)
7913 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7914 else
7915 {
7916 it->what = IT_EOB;
7917 it->position = it->current.pos;
7918 success_p = 0;
7919 }
7920 }
7921 else if (!(!it->bidi_p
7922 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7923 || IT_CHARPOS (*it) == it->stop_charpos))
7924 {
7925 /* With bidi non-linear iteration, we could find ourselves
7926 far beyond the last computed stop_charpos, with several
7927 other stop positions in between that we missed. Scan
7928 them all now, in buffer's logical order, until we find
7929 and handle the last stop_charpos that precedes our
7930 current position. */
7931 handle_stop_backwards (it, it->stop_charpos);
7932 return GET_NEXT_DISPLAY_ELEMENT (it);
7933 }
7934 else
7935 {
7936 if (it->bidi_p)
7937 {
7938 /* Take note of the stop position we just moved across,
7939 for when we will move back across it. */
7940 it->prev_stop = it->stop_charpos;
7941 /* If we are at base paragraph embedding level, take
7942 note of the last stop position seen at this
7943 level. */
7944 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7945 it->base_level_stop = it->stop_charpos;
7946 }
7947 handle_stop (it);
7948 return GET_NEXT_DISPLAY_ELEMENT (it);
7949 }
7950 }
7951 else if (it->bidi_p
7952 /* If we are before prev_stop, we may have overstepped on
7953 our way backwards a stop_pos, and if so, we need to
7954 handle that stop_pos. */
7955 && IT_CHARPOS (*it) < it->prev_stop
7956 /* We can sometimes back up for reasons that have nothing
7957 to do with bidi reordering. E.g., compositions. The
7958 code below is only needed when we are above the base
7959 embedding level, so test for that explicitly. */
7960 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7961 {
7962 if (it->base_level_stop <= 0
7963 || IT_CHARPOS (*it) < it->base_level_stop)
7964 {
7965 /* If we lost track of base_level_stop, we need to find
7966 prev_stop by looking backwards. This happens, e.g., when
7967 we were reseated to the previous screenful of text by
7968 vertical-motion. */
7969 it->base_level_stop = BEGV;
7970 compute_stop_pos_backwards (it);
7971 handle_stop_backwards (it, it->prev_stop);
7972 }
7973 else
7974 handle_stop_backwards (it, it->base_level_stop);
7975 return GET_NEXT_DISPLAY_ELEMENT (it);
7976 }
7977 else
7978 {
7979 /* No face changes, overlays etc. in sight, so just return a
7980 character from current_buffer. */
7981 unsigned char *p;
7982 ptrdiff_t stop;
7983
7984 /* Maybe run the redisplay end trigger hook. Performance note:
7985 This doesn't seem to cost measurable time. */
7986 if (it->redisplay_end_trigger_charpos
7987 && it->glyph_row
7988 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7989 run_redisplay_end_trigger_hook (it);
7990
7991 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7992 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7993 stop)
7994 && next_element_from_composition (it))
7995 {
7996 return 1;
7997 }
7998
7999 /* Get the next character, maybe multibyte. */
8000 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8001 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8002 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8003 else
8004 it->c = *p, it->len = 1;
8005
8006 /* Record what we have and where it came from. */
8007 it->what = IT_CHARACTER;
8008 it->object = it->w->buffer;
8009 it->position = it->current.pos;
8010
8011 /* Normally we return the character found above, except when we
8012 really want to return an ellipsis for selective display. */
8013 if (it->selective)
8014 {
8015 if (it->c == '\n')
8016 {
8017 /* A value of selective > 0 means hide lines indented more
8018 than that number of columns. */
8019 if (it->selective > 0
8020 && IT_CHARPOS (*it) + 1 < ZV
8021 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8022 IT_BYTEPOS (*it) + 1,
8023 it->selective))
8024 {
8025 success_p = next_element_from_ellipsis (it);
8026 it->dpvec_char_len = -1;
8027 }
8028 }
8029 else if (it->c == '\r' && it->selective == -1)
8030 {
8031 /* A value of selective == -1 means that everything from the
8032 CR to the end of the line is invisible, with maybe an
8033 ellipsis displayed for it. */
8034 success_p = next_element_from_ellipsis (it);
8035 it->dpvec_char_len = -1;
8036 }
8037 }
8038 }
8039
8040 /* Value is zero if end of buffer reached. */
8041 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8042 return success_p;
8043 }
8044
8045
8046 /* Run the redisplay end trigger hook for IT. */
8047
8048 static void
8049 run_redisplay_end_trigger_hook (struct it *it)
8050 {
8051 Lisp_Object args[3];
8052
8053 /* IT->glyph_row should be non-null, i.e. we should be actually
8054 displaying something, or otherwise we should not run the hook. */
8055 eassert (it->glyph_row);
8056
8057 /* Set up hook arguments. */
8058 args[0] = Qredisplay_end_trigger_functions;
8059 args[1] = it->window;
8060 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8061 it->redisplay_end_trigger_charpos = 0;
8062
8063 /* Since we are *trying* to run these functions, don't try to run
8064 them again, even if they get an error. */
8065 wset_redisplay_end_trigger (it->w, Qnil);
8066 Frun_hook_with_args (3, args);
8067
8068 /* Notice if it changed the face of the character we are on. */
8069 handle_face_prop (it);
8070 }
8071
8072
8073 /* Deliver a composition display element. Unlike the other
8074 next_element_from_XXX, this function is not registered in the array
8075 get_next_element[]. It is called from next_element_from_buffer and
8076 next_element_from_string when necessary. */
8077
8078 static int
8079 next_element_from_composition (struct it *it)
8080 {
8081 it->what = IT_COMPOSITION;
8082 it->len = it->cmp_it.nbytes;
8083 if (STRINGP (it->string))
8084 {
8085 if (it->c < 0)
8086 {
8087 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8088 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8089 return 0;
8090 }
8091 it->position = it->current.string_pos;
8092 it->object = it->string;
8093 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8094 IT_STRING_BYTEPOS (*it), it->string);
8095 }
8096 else
8097 {
8098 if (it->c < 0)
8099 {
8100 IT_CHARPOS (*it) += it->cmp_it.nchars;
8101 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8102 if (it->bidi_p)
8103 {
8104 if (it->bidi_it.new_paragraph)
8105 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8106 /* Resync the bidi iterator with IT's new position.
8107 FIXME: this doesn't support bidirectional text. */
8108 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8109 bidi_move_to_visually_next (&it->bidi_it);
8110 }
8111 return 0;
8112 }
8113 it->position = it->current.pos;
8114 it->object = it->w->buffer;
8115 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8116 IT_BYTEPOS (*it), Qnil);
8117 }
8118 return 1;
8119 }
8120
8121
8122 \f
8123 /***********************************************************************
8124 Moving an iterator without producing glyphs
8125 ***********************************************************************/
8126
8127 /* Check if iterator is at a position corresponding to a valid buffer
8128 position after some move_it_ call. */
8129
8130 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8131 ((it)->method == GET_FROM_STRING \
8132 ? IT_STRING_CHARPOS (*it) == 0 \
8133 : 1)
8134
8135
8136 /* Move iterator IT to a specified buffer or X position within one
8137 line on the display without producing glyphs.
8138
8139 OP should be a bit mask including some or all of these bits:
8140 MOVE_TO_X: Stop upon reaching x-position TO_X.
8141 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8142 Regardless of OP's value, stop upon reaching the end of the display line.
8143
8144 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8145 This means, in particular, that TO_X includes window's horizontal
8146 scroll amount.
8147
8148 The return value has several possible values that
8149 say what condition caused the scan to stop:
8150
8151 MOVE_POS_MATCH_OR_ZV
8152 - when TO_POS or ZV was reached.
8153
8154 MOVE_X_REACHED
8155 -when TO_X was reached before TO_POS or ZV were reached.
8156
8157 MOVE_LINE_CONTINUED
8158 - when we reached the end of the display area and the line must
8159 be continued.
8160
8161 MOVE_LINE_TRUNCATED
8162 - when we reached the end of the display area and the line is
8163 truncated.
8164
8165 MOVE_NEWLINE_OR_CR
8166 - when we stopped at a line end, i.e. a newline or a CR and selective
8167 display is on. */
8168
8169 static enum move_it_result
8170 move_it_in_display_line_to (struct it *it,
8171 ptrdiff_t to_charpos, int to_x,
8172 enum move_operation_enum op)
8173 {
8174 enum move_it_result result = MOVE_UNDEFINED;
8175 struct glyph_row *saved_glyph_row;
8176 struct it wrap_it, atpos_it, atx_it, ppos_it;
8177 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8178 void *ppos_data = NULL;
8179 int may_wrap = 0;
8180 enum it_method prev_method = it->method;
8181 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8182 int saw_smaller_pos = prev_pos < to_charpos;
8183
8184 /* Don't produce glyphs in produce_glyphs. */
8185 saved_glyph_row = it->glyph_row;
8186 it->glyph_row = NULL;
8187
8188 /* Use wrap_it to save a copy of IT wherever a word wrap could
8189 occur. Use atpos_it to save a copy of IT at the desired buffer
8190 position, if found, so that we can scan ahead and check if the
8191 word later overshoots the window edge. Use atx_it similarly, for
8192 pixel positions. */
8193 wrap_it.sp = -1;
8194 atpos_it.sp = -1;
8195 atx_it.sp = -1;
8196
8197 /* Use ppos_it under bidi reordering to save a copy of IT for the
8198 position > CHARPOS that is the closest to CHARPOS. We restore
8199 that position in IT when we have scanned the entire display line
8200 without finding a match for CHARPOS and all the character
8201 positions are greater than CHARPOS. */
8202 if (it->bidi_p)
8203 {
8204 SAVE_IT (ppos_it, *it, ppos_data);
8205 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8206 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8207 SAVE_IT (ppos_it, *it, ppos_data);
8208 }
8209
8210 #define BUFFER_POS_REACHED_P() \
8211 ((op & MOVE_TO_POS) != 0 \
8212 && BUFFERP (it->object) \
8213 && (IT_CHARPOS (*it) == to_charpos \
8214 || ((!it->bidi_p \
8215 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8216 && IT_CHARPOS (*it) > to_charpos) \
8217 || (it->what == IT_COMPOSITION \
8218 && ((IT_CHARPOS (*it) > to_charpos \
8219 && to_charpos >= it->cmp_it.charpos) \
8220 || (IT_CHARPOS (*it) < to_charpos \
8221 && to_charpos <= it->cmp_it.charpos)))) \
8222 && (it->method == GET_FROM_BUFFER \
8223 || (it->method == GET_FROM_DISPLAY_VECTOR \
8224 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8225
8226 /* If there's a line-/wrap-prefix, handle it. */
8227 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8228 && it->current_y < it->last_visible_y)
8229 handle_line_prefix (it);
8230
8231 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8232 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8233
8234 while (1)
8235 {
8236 int x, i, ascent = 0, descent = 0;
8237
8238 /* Utility macro to reset an iterator with x, ascent, and descent. */
8239 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8240 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8241 (IT)->max_descent = descent)
8242
8243 /* Stop if we move beyond TO_CHARPOS (after an image or a
8244 display string or stretch glyph). */
8245 if ((op & MOVE_TO_POS) != 0
8246 && BUFFERP (it->object)
8247 && it->method == GET_FROM_BUFFER
8248 && (((!it->bidi_p
8249 /* When the iterator is at base embedding level, we
8250 are guaranteed that characters are delivered for
8251 display in strictly increasing order of their
8252 buffer positions. */
8253 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8254 && IT_CHARPOS (*it) > to_charpos)
8255 || (it->bidi_p
8256 && (prev_method == GET_FROM_IMAGE
8257 || prev_method == GET_FROM_STRETCH
8258 || prev_method == GET_FROM_STRING)
8259 /* Passed TO_CHARPOS from left to right. */
8260 && ((prev_pos < to_charpos
8261 && IT_CHARPOS (*it) > to_charpos)
8262 /* Passed TO_CHARPOS from right to left. */
8263 || (prev_pos > to_charpos
8264 && IT_CHARPOS (*it) < to_charpos)))))
8265 {
8266 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8267 {
8268 result = MOVE_POS_MATCH_OR_ZV;
8269 break;
8270 }
8271 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8272 /* If wrap_it is valid, the current position might be in a
8273 word that is wrapped. So, save the iterator in
8274 atpos_it and continue to see if wrapping happens. */
8275 SAVE_IT (atpos_it, *it, atpos_data);
8276 }
8277
8278 /* Stop when ZV reached.
8279 We used to stop here when TO_CHARPOS reached as well, but that is
8280 too soon if this glyph does not fit on this line. So we handle it
8281 explicitly below. */
8282 if (!get_next_display_element (it))
8283 {
8284 result = MOVE_POS_MATCH_OR_ZV;
8285 break;
8286 }
8287
8288 if (it->line_wrap == TRUNCATE)
8289 {
8290 if (BUFFER_POS_REACHED_P ())
8291 {
8292 result = MOVE_POS_MATCH_OR_ZV;
8293 break;
8294 }
8295 }
8296 else
8297 {
8298 if (it->line_wrap == WORD_WRAP)
8299 {
8300 if (IT_DISPLAYING_WHITESPACE (it))
8301 may_wrap = 1;
8302 else if (may_wrap)
8303 {
8304 /* We have reached a glyph that follows one or more
8305 whitespace characters. If the position is
8306 already found, we are done. */
8307 if (atpos_it.sp >= 0)
8308 {
8309 RESTORE_IT (it, &atpos_it, atpos_data);
8310 result = MOVE_POS_MATCH_OR_ZV;
8311 goto done;
8312 }
8313 if (atx_it.sp >= 0)
8314 {
8315 RESTORE_IT (it, &atx_it, atx_data);
8316 result = MOVE_X_REACHED;
8317 goto done;
8318 }
8319 /* Otherwise, we can wrap here. */
8320 SAVE_IT (wrap_it, *it, wrap_data);
8321 may_wrap = 0;
8322 }
8323 }
8324 }
8325
8326 /* Remember the line height for the current line, in case
8327 the next element doesn't fit on the line. */
8328 ascent = it->max_ascent;
8329 descent = it->max_descent;
8330
8331 /* The call to produce_glyphs will get the metrics of the
8332 display element IT is loaded with. Record the x-position
8333 before this display element, in case it doesn't fit on the
8334 line. */
8335 x = it->current_x;
8336
8337 PRODUCE_GLYPHS (it);
8338
8339 if (it->area != TEXT_AREA)
8340 {
8341 prev_method = it->method;
8342 if (it->method == GET_FROM_BUFFER)
8343 prev_pos = IT_CHARPOS (*it);
8344 set_iterator_to_next (it, 1);
8345 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8346 SET_TEXT_POS (this_line_min_pos,
8347 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8348 if (it->bidi_p
8349 && (op & MOVE_TO_POS)
8350 && IT_CHARPOS (*it) > to_charpos
8351 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8352 SAVE_IT (ppos_it, *it, ppos_data);
8353 continue;
8354 }
8355
8356 /* The number of glyphs we get back in IT->nglyphs will normally
8357 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8358 character on a terminal frame, or (iii) a line end. For the
8359 second case, IT->nglyphs - 1 padding glyphs will be present.
8360 (On X frames, there is only one glyph produced for a
8361 composite character.)
8362
8363 The behavior implemented below means, for continuation lines,
8364 that as many spaces of a TAB as fit on the current line are
8365 displayed there. For terminal frames, as many glyphs of a
8366 multi-glyph character are displayed in the current line, too.
8367 This is what the old redisplay code did, and we keep it that
8368 way. Under X, the whole shape of a complex character must
8369 fit on the line or it will be completely displayed in the
8370 next line.
8371
8372 Note that both for tabs and padding glyphs, all glyphs have
8373 the same width. */
8374 if (it->nglyphs)
8375 {
8376 /* More than one glyph or glyph doesn't fit on line. All
8377 glyphs have the same width. */
8378 int single_glyph_width = it->pixel_width / it->nglyphs;
8379 int new_x;
8380 int x_before_this_char = x;
8381 int hpos_before_this_char = it->hpos;
8382
8383 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8384 {
8385 new_x = x + single_glyph_width;
8386
8387 /* We want to leave anything reaching TO_X to the caller. */
8388 if ((op & MOVE_TO_X) && new_x > to_x)
8389 {
8390 if (BUFFER_POS_REACHED_P ())
8391 {
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8393 goto buffer_pos_reached;
8394 if (atpos_it.sp < 0)
8395 {
8396 SAVE_IT (atpos_it, *it, atpos_data);
8397 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8398 }
8399 }
8400 else
8401 {
8402 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8403 {
8404 it->current_x = x;
8405 result = MOVE_X_REACHED;
8406 break;
8407 }
8408 if (atx_it.sp < 0)
8409 {
8410 SAVE_IT (atx_it, *it, atx_data);
8411 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8412 }
8413 }
8414 }
8415
8416 if (/* Lines are continued. */
8417 it->line_wrap != TRUNCATE
8418 && (/* And glyph doesn't fit on the line. */
8419 new_x > it->last_visible_x
8420 /* Or it fits exactly and we're on a window
8421 system frame. */
8422 || (new_x == it->last_visible_x
8423 && FRAME_WINDOW_P (it->f)
8424 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8425 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8426 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8427 {
8428 if (/* IT->hpos == 0 means the very first glyph
8429 doesn't fit on the line, e.g. a wide image. */
8430 it->hpos == 0
8431 || (new_x == it->last_visible_x
8432 && FRAME_WINDOW_P (it->f)))
8433 {
8434 ++it->hpos;
8435 it->current_x = new_x;
8436
8437 /* The character's last glyph just barely fits
8438 in this row. */
8439 if (i == it->nglyphs - 1)
8440 {
8441 /* If this is the destination position,
8442 return a position *before* it in this row,
8443 now that we know it fits in this row. */
8444 if (BUFFER_POS_REACHED_P ())
8445 {
8446 if (it->line_wrap != WORD_WRAP
8447 || wrap_it.sp < 0)
8448 {
8449 it->hpos = hpos_before_this_char;
8450 it->current_x = x_before_this_char;
8451 result = MOVE_POS_MATCH_OR_ZV;
8452 break;
8453 }
8454 if (it->line_wrap == WORD_WRAP
8455 && atpos_it.sp < 0)
8456 {
8457 SAVE_IT (atpos_it, *it, atpos_data);
8458 atpos_it.current_x = x_before_this_char;
8459 atpos_it.hpos = hpos_before_this_char;
8460 }
8461 }
8462
8463 prev_method = it->method;
8464 if (it->method == GET_FROM_BUFFER)
8465 prev_pos = IT_CHARPOS (*it);
8466 set_iterator_to_next (it, 1);
8467 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8468 SET_TEXT_POS (this_line_min_pos,
8469 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8470 /* On graphical terminals, newlines may
8471 "overflow" into the fringe if
8472 overflow-newline-into-fringe is non-nil.
8473 On text terminals, and on graphical
8474 terminals with no right margin, newlines
8475 may overflow into the last glyph on the
8476 display line.*/
8477 if (!FRAME_WINDOW_P (it->f)
8478 || ((it->bidi_p
8479 && it->bidi_it.paragraph_dir == R2L)
8480 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8481 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8482 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8483 {
8484 if (!get_next_display_element (it))
8485 {
8486 result = MOVE_POS_MATCH_OR_ZV;
8487 break;
8488 }
8489 if (BUFFER_POS_REACHED_P ())
8490 {
8491 if (ITERATOR_AT_END_OF_LINE_P (it))
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 else
8494 result = MOVE_LINE_CONTINUED;
8495 break;
8496 }
8497 if (ITERATOR_AT_END_OF_LINE_P (it))
8498 {
8499 result = MOVE_NEWLINE_OR_CR;
8500 break;
8501 }
8502 }
8503 }
8504 }
8505 else
8506 IT_RESET_X_ASCENT_DESCENT (it);
8507
8508 if (wrap_it.sp >= 0)
8509 {
8510 RESTORE_IT (it, &wrap_it, wrap_data);
8511 atpos_it.sp = -1;
8512 atx_it.sp = -1;
8513 }
8514
8515 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8516 IT_CHARPOS (*it)));
8517 result = MOVE_LINE_CONTINUED;
8518 break;
8519 }
8520
8521 if (BUFFER_POS_REACHED_P ())
8522 {
8523 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8524 goto buffer_pos_reached;
8525 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8526 {
8527 SAVE_IT (atpos_it, *it, atpos_data);
8528 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8529 }
8530 }
8531
8532 if (new_x > it->first_visible_x)
8533 {
8534 /* Glyph is visible. Increment number of glyphs that
8535 would be displayed. */
8536 ++it->hpos;
8537 }
8538 }
8539
8540 if (result != MOVE_UNDEFINED)
8541 break;
8542 }
8543 else if (BUFFER_POS_REACHED_P ())
8544 {
8545 buffer_pos_reached:
8546 IT_RESET_X_ASCENT_DESCENT (it);
8547 result = MOVE_POS_MATCH_OR_ZV;
8548 break;
8549 }
8550 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8551 {
8552 /* Stop when TO_X specified and reached. This check is
8553 necessary here because of lines consisting of a line end,
8554 only. The line end will not produce any glyphs and we
8555 would never get MOVE_X_REACHED. */
8556 eassert (it->nglyphs == 0);
8557 result = MOVE_X_REACHED;
8558 break;
8559 }
8560
8561 /* Is this a line end? If yes, we're done. */
8562 if (ITERATOR_AT_END_OF_LINE_P (it))
8563 {
8564 /* If we are past TO_CHARPOS, but never saw any character
8565 positions smaller than TO_CHARPOS, return
8566 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8567 did. */
8568 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8569 {
8570 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8571 {
8572 if (IT_CHARPOS (ppos_it) < ZV)
8573 {
8574 RESTORE_IT (it, &ppos_it, ppos_data);
8575 result = MOVE_POS_MATCH_OR_ZV;
8576 }
8577 else
8578 goto buffer_pos_reached;
8579 }
8580 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8581 && IT_CHARPOS (*it) > to_charpos)
8582 goto buffer_pos_reached;
8583 else
8584 result = MOVE_NEWLINE_OR_CR;
8585 }
8586 else
8587 result = MOVE_NEWLINE_OR_CR;
8588 break;
8589 }
8590
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 /* The current display element has been consumed. Advance
8595 to the next. */
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8599 if (IT_CHARPOS (*it) < to_charpos)
8600 saw_smaller_pos = 1;
8601 if (it->bidi_p
8602 && (op & MOVE_TO_POS)
8603 && IT_CHARPOS (*it) >= to_charpos
8604 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8605 SAVE_IT (ppos_it, *it, ppos_data);
8606
8607 /* Stop if lines are truncated and IT's current x-position is
8608 past the right edge of the window now. */
8609 if (it->line_wrap == TRUNCATE
8610 && it->current_x >= it->last_visible_x)
8611 {
8612 if (!FRAME_WINDOW_P (it->f)
8613 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8616 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8617 {
8618 int at_eob_p = 0;
8619
8620 if ((at_eob_p = !get_next_display_element (it))
8621 || BUFFER_POS_REACHED_P ()
8622 /* If we are past TO_CHARPOS, but never saw any
8623 character positions smaller than TO_CHARPOS,
8624 return MOVE_POS_MATCH_OR_ZV, like the
8625 unidirectional display did. */
8626 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8627 && !saw_smaller_pos
8628 && IT_CHARPOS (*it) > to_charpos))
8629 {
8630 if (it->bidi_p
8631 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8632 RESTORE_IT (it, &ppos_it, ppos_data);
8633 result = MOVE_POS_MATCH_OR_ZV;
8634 break;
8635 }
8636 if (ITERATOR_AT_END_OF_LINE_P (it))
8637 {
8638 result = MOVE_NEWLINE_OR_CR;
8639 break;
8640 }
8641 }
8642 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8643 && !saw_smaller_pos
8644 && IT_CHARPOS (*it) > to_charpos)
8645 {
8646 if (IT_CHARPOS (ppos_it) < ZV)
8647 RESTORE_IT (it, &ppos_it, ppos_data);
8648 result = MOVE_POS_MATCH_OR_ZV;
8649 break;
8650 }
8651 result = MOVE_LINE_TRUNCATED;
8652 break;
8653 }
8654 #undef IT_RESET_X_ASCENT_DESCENT
8655 }
8656
8657 #undef BUFFER_POS_REACHED_P
8658
8659 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8660 restore the saved iterator. */
8661 if (atpos_it.sp >= 0)
8662 RESTORE_IT (it, &atpos_it, atpos_data);
8663 else if (atx_it.sp >= 0)
8664 RESTORE_IT (it, &atx_it, atx_data);
8665
8666 done:
8667
8668 if (atpos_data)
8669 bidi_unshelve_cache (atpos_data, 1);
8670 if (atx_data)
8671 bidi_unshelve_cache (atx_data, 1);
8672 if (wrap_data)
8673 bidi_unshelve_cache (wrap_data, 1);
8674 if (ppos_data)
8675 bidi_unshelve_cache (ppos_data, 1);
8676
8677 /* Restore the iterator settings altered at the beginning of this
8678 function. */
8679 it->glyph_row = saved_glyph_row;
8680 return result;
8681 }
8682
8683 /* For external use. */
8684 void
8685 move_it_in_display_line (struct it *it,
8686 ptrdiff_t to_charpos, int to_x,
8687 enum move_operation_enum op)
8688 {
8689 if (it->line_wrap == WORD_WRAP
8690 && (op & MOVE_TO_X))
8691 {
8692 struct it save_it;
8693 void *save_data = NULL;
8694 int skip;
8695
8696 SAVE_IT (save_it, *it, save_data);
8697 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8698 /* When word-wrap is on, TO_X may lie past the end
8699 of a wrapped line. Then it->current is the
8700 character on the next line, so backtrack to the
8701 space before the wrap point. */
8702 if (skip == MOVE_LINE_CONTINUED)
8703 {
8704 int prev_x = max (it->current_x - 1, 0);
8705 RESTORE_IT (it, &save_it, save_data);
8706 move_it_in_display_line_to
8707 (it, -1, prev_x, MOVE_TO_X);
8708 }
8709 else
8710 bidi_unshelve_cache (save_data, 1);
8711 }
8712 else
8713 move_it_in_display_line_to (it, to_charpos, to_x, op);
8714 }
8715
8716
8717 /* Move IT forward until it satisfies one or more of the criteria in
8718 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8719
8720 OP is a bit-mask that specifies where to stop, and in particular,
8721 which of those four position arguments makes a difference. See the
8722 description of enum move_operation_enum.
8723
8724 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8725 screen line, this function will set IT to the next position that is
8726 displayed to the right of TO_CHARPOS on the screen. */
8727
8728 void
8729 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8730 {
8731 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8732 int line_height, line_start_x = 0, reached = 0;
8733 void *backup_data = NULL;
8734
8735 for (;;)
8736 {
8737 if (op & MOVE_TO_VPOS)
8738 {
8739 /* If no TO_CHARPOS and no TO_X specified, stop at the
8740 start of the line TO_VPOS. */
8741 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8742 {
8743 if (it->vpos == to_vpos)
8744 {
8745 reached = 1;
8746 break;
8747 }
8748 else
8749 skip = move_it_in_display_line_to (it, -1, -1, 0);
8750 }
8751 else
8752 {
8753 /* TO_VPOS >= 0 means stop at TO_X in the line at
8754 TO_VPOS, or at TO_POS, whichever comes first. */
8755 if (it->vpos == to_vpos)
8756 {
8757 reached = 2;
8758 break;
8759 }
8760
8761 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8762
8763 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8764 {
8765 reached = 3;
8766 break;
8767 }
8768 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8769 {
8770 /* We have reached TO_X but not in the line we want. */
8771 skip = move_it_in_display_line_to (it, to_charpos,
8772 -1, MOVE_TO_POS);
8773 if (skip == MOVE_POS_MATCH_OR_ZV)
8774 {
8775 reached = 4;
8776 break;
8777 }
8778 }
8779 }
8780 }
8781 else if (op & MOVE_TO_Y)
8782 {
8783 struct it it_backup;
8784
8785 if (it->line_wrap == WORD_WRAP)
8786 SAVE_IT (it_backup, *it, backup_data);
8787
8788 /* TO_Y specified means stop at TO_X in the line containing
8789 TO_Y---or at TO_CHARPOS if this is reached first. The
8790 problem is that we can't really tell whether the line
8791 contains TO_Y before we have completely scanned it, and
8792 this may skip past TO_X. What we do is to first scan to
8793 TO_X.
8794
8795 If TO_X is not specified, use a TO_X of zero. The reason
8796 is to make the outcome of this function more predictable.
8797 If we didn't use TO_X == 0, we would stop at the end of
8798 the line which is probably not what a caller would expect
8799 to happen. */
8800 skip = move_it_in_display_line_to
8801 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8802 (MOVE_TO_X | (op & MOVE_TO_POS)));
8803
8804 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8805 if (skip == MOVE_POS_MATCH_OR_ZV)
8806 reached = 5;
8807 else if (skip == MOVE_X_REACHED)
8808 {
8809 /* If TO_X was reached, we want to know whether TO_Y is
8810 in the line. We know this is the case if the already
8811 scanned glyphs make the line tall enough. Otherwise,
8812 we must check by scanning the rest of the line. */
8813 line_height = it->max_ascent + it->max_descent;
8814 if (to_y >= it->current_y
8815 && to_y < it->current_y + line_height)
8816 {
8817 reached = 6;
8818 break;
8819 }
8820 SAVE_IT (it_backup, *it, backup_data);
8821 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8822 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8823 op & MOVE_TO_POS);
8824 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8825 line_height = it->max_ascent + it->max_descent;
8826 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8827
8828 if (to_y >= it->current_y
8829 && to_y < it->current_y + line_height)
8830 {
8831 /* If TO_Y is in this line and TO_X was reached
8832 above, we scanned too far. We have to restore
8833 IT's settings to the ones before skipping. But
8834 keep the more accurate values of max_ascent and
8835 max_descent we've found while skipping the rest
8836 of the line, for the sake of callers, such as
8837 pos_visible_p, that need to know the line
8838 height. */
8839 int max_ascent = it->max_ascent;
8840 int max_descent = it->max_descent;
8841
8842 RESTORE_IT (it, &it_backup, backup_data);
8843 it->max_ascent = max_ascent;
8844 it->max_descent = max_descent;
8845 reached = 6;
8846 }
8847 else
8848 {
8849 skip = skip2;
8850 if (skip == MOVE_POS_MATCH_OR_ZV)
8851 reached = 7;
8852 }
8853 }
8854 else
8855 {
8856 /* Check whether TO_Y is in this line. */
8857 line_height = it->max_ascent + it->max_descent;
8858 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8859
8860 if (to_y >= it->current_y
8861 && to_y < it->current_y + line_height)
8862 {
8863 /* When word-wrap is on, TO_X may lie past the end
8864 of a wrapped line. Then it->current is the
8865 character on the next line, so backtrack to the
8866 space before the wrap point. */
8867 if (skip == MOVE_LINE_CONTINUED
8868 && it->line_wrap == WORD_WRAP)
8869 {
8870 int prev_x = max (it->current_x - 1, 0);
8871 RESTORE_IT (it, &it_backup, backup_data);
8872 skip = move_it_in_display_line_to
8873 (it, -1, prev_x, MOVE_TO_X);
8874 }
8875 reached = 6;
8876 }
8877 }
8878
8879 if (reached)
8880 break;
8881 }
8882 else if (BUFFERP (it->object)
8883 && (it->method == GET_FROM_BUFFER
8884 || it->method == GET_FROM_STRETCH)
8885 && IT_CHARPOS (*it) >= to_charpos
8886 /* Under bidi iteration, a call to set_iterator_to_next
8887 can scan far beyond to_charpos if the initial
8888 portion of the next line needs to be reordered. In
8889 that case, give move_it_in_display_line_to another
8890 chance below. */
8891 && !(it->bidi_p
8892 && it->bidi_it.scan_dir == -1))
8893 skip = MOVE_POS_MATCH_OR_ZV;
8894 else
8895 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8896
8897 switch (skip)
8898 {
8899 case MOVE_POS_MATCH_OR_ZV:
8900 reached = 8;
8901 goto out;
8902
8903 case MOVE_NEWLINE_OR_CR:
8904 set_iterator_to_next (it, 1);
8905 it->continuation_lines_width = 0;
8906 break;
8907
8908 case MOVE_LINE_TRUNCATED:
8909 it->continuation_lines_width = 0;
8910 reseat_at_next_visible_line_start (it, 0);
8911 if ((op & MOVE_TO_POS) != 0
8912 && IT_CHARPOS (*it) > to_charpos)
8913 {
8914 reached = 9;
8915 goto out;
8916 }
8917 break;
8918
8919 case MOVE_LINE_CONTINUED:
8920 /* For continued lines ending in a tab, some of the glyphs
8921 associated with the tab are displayed on the current
8922 line. Since it->current_x does not include these glyphs,
8923 we use it->last_visible_x instead. */
8924 if (it->c == '\t')
8925 {
8926 it->continuation_lines_width += it->last_visible_x;
8927 /* When moving by vpos, ensure that the iterator really
8928 advances to the next line (bug#847, bug#969). Fixme:
8929 do we need to do this in other circumstances? */
8930 if (it->current_x != it->last_visible_x
8931 && (op & MOVE_TO_VPOS)
8932 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8933 {
8934 line_start_x = it->current_x + it->pixel_width
8935 - it->last_visible_x;
8936 set_iterator_to_next (it, 0);
8937 }
8938 }
8939 else
8940 it->continuation_lines_width += it->current_x;
8941 break;
8942
8943 default:
8944 emacs_abort ();
8945 }
8946
8947 /* Reset/increment for the next run. */
8948 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8949 it->current_x = line_start_x;
8950 line_start_x = 0;
8951 it->hpos = 0;
8952 it->current_y += it->max_ascent + it->max_descent;
8953 ++it->vpos;
8954 last_height = it->max_ascent + it->max_descent;
8955 last_max_ascent = it->max_ascent;
8956 it->max_ascent = it->max_descent = 0;
8957 }
8958
8959 out:
8960
8961 /* On text terminals, we may stop at the end of a line in the middle
8962 of a multi-character glyph. If the glyph itself is continued,
8963 i.e. it is actually displayed on the next line, don't treat this
8964 stopping point as valid; move to the next line instead (unless
8965 that brings us offscreen). */
8966 if (!FRAME_WINDOW_P (it->f)
8967 && op & MOVE_TO_POS
8968 && IT_CHARPOS (*it) == to_charpos
8969 && it->what == IT_CHARACTER
8970 && it->nglyphs > 1
8971 && it->line_wrap == WINDOW_WRAP
8972 && it->current_x == it->last_visible_x - 1
8973 && it->c != '\n'
8974 && it->c != '\t'
8975 && it->vpos < XFASTINT (it->w->window_end_vpos))
8976 {
8977 it->continuation_lines_width += it->current_x;
8978 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8979 it->current_y += it->max_ascent + it->max_descent;
8980 ++it->vpos;
8981 last_height = it->max_ascent + it->max_descent;
8982 last_max_ascent = it->max_ascent;
8983 }
8984
8985 if (backup_data)
8986 bidi_unshelve_cache (backup_data, 1);
8987
8988 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8989 }
8990
8991
8992 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8993
8994 If DY > 0, move IT backward at least that many pixels. DY = 0
8995 means move IT backward to the preceding line start or BEGV. This
8996 function may move over more than DY pixels if IT->current_y - DY
8997 ends up in the middle of a line; in this case IT->current_y will be
8998 set to the top of the line moved to. */
8999
9000 void
9001 move_it_vertically_backward (struct it *it, int dy)
9002 {
9003 int nlines, h;
9004 struct it it2, it3;
9005 void *it2data = NULL, *it3data = NULL;
9006 ptrdiff_t start_pos;
9007
9008 move_further_back:
9009 eassert (dy >= 0);
9010
9011 start_pos = IT_CHARPOS (*it);
9012
9013 /* Estimate how many newlines we must move back. */
9014 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9015
9016 /* Set the iterator's position that many lines back. */
9017 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9018 back_to_previous_visible_line_start (it);
9019
9020 /* Reseat the iterator here. When moving backward, we don't want
9021 reseat to skip forward over invisible text, set up the iterator
9022 to deliver from overlay strings at the new position etc. So,
9023 use reseat_1 here. */
9024 reseat_1 (it, it->current.pos, 1);
9025
9026 /* We are now surely at a line start. */
9027 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9028 reordering is in effect. */
9029 it->continuation_lines_width = 0;
9030
9031 /* Move forward and see what y-distance we moved. First move to the
9032 start of the next line so that we get its height. We need this
9033 height to be able to tell whether we reached the specified
9034 y-distance. */
9035 SAVE_IT (it2, *it, it2data);
9036 it2.max_ascent = it2.max_descent = 0;
9037 do
9038 {
9039 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9040 MOVE_TO_POS | MOVE_TO_VPOS);
9041 }
9042 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9043 /* If we are in a display string which starts at START_POS,
9044 and that display string includes a newline, and we are
9045 right after that newline (i.e. at the beginning of a
9046 display line), exit the loop, because otherwise we will
9047 infloop, since move_it_to will see that it is already at
9048 START_POS and will not move. */
9049 || (it2.method == GET_FROM_STRING
9050 && IT_CHARPOS (it2) == start_pos
9051 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9052 eassert (IT_CHARPOS (*it) >= BEGV);
9053 SAVE_IT (it3, it2, it3data);
9054
9055 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9056 eassert (IT_CHARPOS (*it) >= BEGV);
9057 /* H is the actual vertical distance from the position in *IT
9058 and the starting position. */
9059 h = it2.current_y - it->current_y;
9060 /* NLINES is the distance in number of lines. */
9061 nlines = it2.vpos - it->vpos;
9062
9063 /* Correct IT's y and vpos position
9064 so that they are relative to the starting point. */
9065 it->vpos -= nlines;
9066 it->current_y -= h;
9067
9068 if (dy == 0)
9069 {
9070 /* DY == 0 means move to the start of the screen line. The
9071 value of nlines is > 0 if continuation lines were involved,
9072 or if the original IT position was at start of a line. */
9073 RESTORE_IT (it, it, it2data);
9074 if (nlines > 0)
9075 move_it_by_lines (it, nlines);
9076 /* The above code moves us to some position NLINES down,
9077 usually to its first glyph (leftmost in an L2R line), but
9078 that's not necessarily the start of the line, under bidi
9079 reordering. We want to get to the character position
9080 that is immediately after the newline of the previous
9081 line. */
9082 if (it->bidi_p
9083 && !it->continuation_lines_width
9084 && !STRINGP (it->string)
9085 && IT_CHARPOS (*it) > BEGV
9086 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9087 {
9088 ptrdiff_t nl_pos =
9089 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9090
9091 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9092 }
9093 bidi_unshelve_cache (it3data, 1);
9094 }
9095 else
9096 {
9097 /* The y-position we try to reach, relative to *IT.
9098 Note that H has been subtracted in front of the if-statement. */
9099 int target_y = it->current_y + h - dy;
9100 int y0 = it3.current_y;
9101 int y1;
9102 int line_height;
9103
9104 RESTORE_IT (&it3, &it3, it3data);
9105 y1 = line_bottom_y (&it3);
9106 line_height = y1 - y0;
9107 RESTORE_IT (it, it, it2data);
9108 /* If we did not reach target_y, try to move further backward if
9109 we can. If we moved too far backward, try to move forward. */
9110 if (target_y < it->current_y
9111 /* This is heuristic. In a window that's 3 lines high, with
9112 a line height of 13 pixels each, recentering with point
9113 on the bottom line will try to move -39/2 = 19 pixels
9114 backward. Try to avoid moving into the first line. */
9115 && (it->current_y - target_y
9116 > min (window_box_height (it->w), line_height * 2 / 3))
9117 && IT_CHARPOS (*it) > BEGV)
9118 {
9119 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9120 target_y - it->current_y));
9121 dy = it->current_y - target_y;
9122 goto move_further_back;
9123 }
9124 else if (target_y >= it->current_y + line_height
9125 && IT_CHARPOS (*it) < ZV)
9126 {
9127 /* Should move forward by at least one line, maybe more.
9128
9129 Note: Calling move_it_by_lines can be expensive on
9130 terminal frames, where compute_motion is used (via
9131 vmotion) to do the job, when there are very long lines
9132 and truncate-lines is nil. That's the reason for
9133 treating terminal frames specially here. */
9134
9135 if (!FRAME_WINDOW_P (it->f))
9136 move_it_vertically (it, target_y - (it->current_y + line_height));
9137 else
9138 {
9139 do
9140 {
9141 move_it_by_lines (it, 1);
9142 }
9143 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9144 }
9145 }
9146 }
9147 }
9148
9149
9150 /* Move IT by a specified amount of pixel lines DY. DY negative means
9151 move backwards. DY = 0 means move to start of screen line. At the
9152 end, IT will be on the start of a screen line. */
9153
9154 void
9155 move_it_vertically (struct it *it, int dy)
9156 {
9157 if (dy <= 0)
9158 move_it_vertically_backward (it, -dy);
9159 else
9160 {
9161 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9162 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9163 MOVE_TO_POS | MOVE_TO_Y);
9164 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9165
9166 /* If buffer ends in ZV without a newline, move to the start of
9167 the line to satisfy the post-condition. */
9168 if (IT_CHARPOS (*it) == ZV
9169 && ZV > BEGV
9170 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9171 move_it_by_lines (it, 0);
9172 }
9173 }
9174
9175
9176 /* Move iterator IT past the end of the text line it is in. */
9177
9178 void
9179 move_it_past_eol (struct it *it)
9180 {
9181 enum move_it_result rc;
9182
9183 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9184 if (rc == MOVE_NEWLINE_OR_CR)
9185 set_iterator_to_next (it, 0);
9186 }
9187
9188
9189 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9190 negative means move up. DVPOS == 0 means move to the start of the
9191 screen line.
9192
9193 Optimization idea: If we would know that IT->f doesn't use
9194 a face with proportional font, we could be faster for
9195 truncate-lines nil. */
9196
9197 void
9198 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9199 {
9200
9201 /* The commented-out optimization uses vmotion on terminals. This
9202 gives bad results, because elements like it->what, on which
9203 callers such as pos_visible_p rely, aren't updated. */
9204 /* struct position pos;
9205 if (!FRAME_WINDOW_P (it->f))
9206 {
9207 struct text_pos textpos;
9208
9209 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9210 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9211 reseat (it, textpos, 1);
9212 it->vpos += pos.vpos;
9213 it->current_y += pos.vpos;
9214 }
9215 else */
9216
9217 if (dvpos == 0)
9218 {
9219 /* DVPOS == 0 means move to the start of the screen line. */
9220 move_it_vertically_backward (it, 0);
9221 /* Let next call to line_bottom_y calculate real line height */
9222 last_height = 0;
9223 }
9224 else if (dvpos > 0)
9225 {
9226 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9227 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9228 {
9229 /* Only move to the next buffer position if we ended up in a
9230 string from display property, not in an overlay string
9231 (before-string or after-string). That is because the
9232 latter don't conceal the underlying buffer position, so
9233 we can ask to move the iterator to the exact position we
9234 are interested in. Note that, even if we are already at
9235 IT_CHARPOS (*it), the call below is not a no-op, as it
9236 will detect that we are at the end of the string, pop the
9237 iterator, and compute it->current_x and it->hpos
9238 correctly. */
9239 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9240 -1, -1, -1, MOVE_TO_POS);
9241 }
9242 }
9243 else
9244 {
9245 struct it it2;
9246 void *it2data = NULL;
9247 ptrdiff_t start_charpos, i;
9248
9249 /* Start at the beginning of the screen line containing IT's
9250 position. This may actually move vertically backwards,
9251 in case of overlays, so adjust dvpos accordingly. */
9252 dvpos += it->vpos;
9253 move_it_vertically_backward (it, 0);
9254 dvpos -= it->vpos;
9255
9256 /* Go back -DVPOS visible lines and reseat the iterator there. */
9257 start_charpos = IT_CHARPOS (*it);
9258 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9259 back_to_previous_visible_line_start (it);
9260 reseat (it, it->current.pos, 1);
9261
9262 /* Move further back if we end up in a string or an image. */
9263 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9264 {
9265 /* First try to move to start of display line. */
9266 dvpos += it->vpos;
9267 move_it_vertically_backward (it, 0);
9268 dvpos -= it->vpos;
9269 if (IT_POS_VALID_AFTER_MOVE_P (it))
9270 break;
9271 /* If start of line is still in string or image,
9272 move further back. */
9273 back_to_previous_visible_line_start (it);
9274 reseat (it, it->current.pos, 1);
9275 dvpos--;
9276 }
9277
9278 it->current_x = it->hpos = 0;
9279
9280 /* Above call may have moved too far if continuation lines
9281 are involved. Scan forward and see if it did. */
9282 SAVE_IT (it2, *it, it2data);
9283 it2.vpos = it2.current_y = 0;
9284 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9285 it->vpos -= it2.vpos;
9286 it->current_y -= it2.current_y;
9287 it->current_x = it->hpos = 0;
9288
9289 /* If we moved too far back, move IT some lines forward. */
9290 if (it2.vpos > -dvpos)
9291 {
9292 int delta = it2.vpos + dvpos;
9293
9294 RESTORE_IT (&it2, &it2, it2data);
9295 SAVE_IT (it2, *it, it2data);
9296 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9297 /* Move back again if we got too far ahead. */
9298 if (IT_CHARPOS (*it) >= start_charpos)
9299 RESTORE_IT (it, &it2, it2data);
9300 else
9301 bidi_unshelve_cache (it2data, 1);
9302 }
9303 else
9304 RESTORE_IT (it, it, it2data);
9305 }
9306 }
9307
9308 /* Return 1 if IT points into the middle of a display vector. */
9309
9310 int
9311 in_display_vector_p (struct it *it)
9312 {
9313 return (it->method == GET_FROM_DISPLAY_VECTOR
9314 && it->current.dpvec_index > 0
9315 && it->dpvec + it->current.dpvec_index != it->dpend);
9316 }
9317
9318 \f
9319 /***********************************************************************
9320 Messages
9321 ***********************************************************************/
9322
9323
9324 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9325 to *Messages*. */
9326
9327 void
9328 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9329 {
9330 Lisp_Object args[3];
9331 Lisp_Object msg, fmt;
9332 char *buffer;
9333 ptrdiff_t len;
9334 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9335 USE_SAFE_ALLOCA;
9336
9337 fmt = msg = Qnil;
9338 GCPRO4 (fmt, msg, arg1, arg2);
9339
9340 args[0] = fmt = build_string (format);
9341 args[1] = arg1;
9342 args[2] = arg2;
9343 msg = Fformat (3, args);
9344
9345 len = SBYTES (msg) + 1;
9346 buffer = SAFE_ALLOCA (len);
9347 memcpy (buffer, SDATA (msg), len);
9348
9349 message_dolog (buffer, len - 1, 1, 0);
9350 SAFE_FREE ();
9351
9352 UNGCPRO;
9353 }
9354
9355
9356 /* Output a newline in the *Messages* buffer if "needs" one. */
9357
9358 void
9359 message_log_maybe_newline (void)
9360 {
9361 if (message_log_need_newline)
9362 message_dolog ("", 0, 1, 0);
9363 }
9364
9365
9366 /* Add a string M of length NBYTES to the message log, optionally
9367 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9368 nonzero, means interpret the contents of M as multibyte. This
9369 function calls low-level routines in order to bypass text property
9370 hooks, etc. which might not be safe to run.
9371
9372 This may GC (insert may run before/after change hooks),
9373 so the buffer M must NOT point to a Lisp string. */
9374
9375 void
9376 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9377 {
9378 const unsigned char *msg = (const unsigned char *) m;
9379
9380 if (!NILP (Vmemory_full))
9381 return;
9382
9383 if (!NILP (Vmessage_log_max))
9384 {
9385 struct buffer *oldbuf;
9386 Lisp_Object oldpoint, oldbegv, oldzv;
9387 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9388 ptrdiff_t point_at_end = 0;
9389 ptrdiff_t zv_at_end = 0;
9390 Lisp_Object old_deactivate_mark, tem;
9391 struct gcpro gcpro1;
9392
9393 old_deactivate_mark = Vdeactivate_mark;
9394 oldbuf = current_buffer;
9395 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9396 bset_undo_list (current_buffer, Qt);
9397
9398 oldpoint = message_dolog_marker1;
9399 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9400 oldbegv = message_dolog_marker2;
9401 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9402 oldzv = message_dolog_marker3;
9403 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9404 GCPRO1 (old_deactivate_mark);
9405
9406 if (PT == Z)
9407 point_at_end = 1;
9408 if (ZV == Z)
9409 zv_at_end = 1;
9410
9411 BEGV = BEG;
9412 BEGV_BYTE = BEG_BYTE;
9413 ZV = Z;
9414 ZV_BYTE = Z_BYTE;
9415 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9416
9417 /* Insert the string--maybe converting multibyte to single byte
9418 or vice versa, so that all the text fits the buffer. */
9419 if (multibyte
9420 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9421 {
9422 ptrdiff_t i;
9423 int c, char_bytes;
9424 char work[1];
9425
9426 /* Convert a multibyte string to single-byte
9427 for the *Message* buffer. */
9428 for (i = 0; i < nbytes; i += char_bytes)
9429 {
9430 c = string_char_and_length (msg + i, &char_bytes);
9431 work[0] = (ASCII_CHAR_P (c)
9432 ? c
9433 : multibyte_char_to_unibyte (c));
9434 insert_1_both (work, 1, 1, 1, 0, 0);
9435 }
9436 }
9437 else if (! multibyte
9438 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9439 {
9440 ptrdiff_t i;
9441 int c, char_bytes;
9442 unsigned char str[MAX_MULTIBYTE_LENGTH];
9443 /* Convert a single-byte string to multibyte
9444 for the *Message* buffer. */
9445 for (i = 0; i < nbytes; i++)
9446 {
9447 c = msg[i];
9448 MAKE_CHAR_MULTIBYTE (c);
9449 char_bytes = CHAR_STRING (c, str);
9450 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9451 }
9452 }
9453 else if (nbytes)
9454 insert_1 (m, nbytes, 1, 0, 0);
9455
9456 if (nlflag)
9457 {
9458 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9459 printmax_t dups;
9460 insert_1 ("\n", 1, 1, 0, 0);
9461
9462 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9463 this_bol = PT;
9464 this_bol_byte = PT_BYTE;
9465
9466 /* See if this line duplicates the previous one.
9467 If so, combine duplicates. */
9468 if (this_bol > BEG)
9469 {
9470 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9471 prev_bol = PT;
9472 prev_bol_byte = PT_BYTE;
9473
9474 dups = message_log_check_duplicate (prev_bol_byte,
9475 this_bol_byte);
9476 if (dups)
9477 {
9478 del_range_both (prev_bol, prev_bol_byte,
9479 this_bol, this_bol_byte, 0);
9480 if (dups > 1)
9481 {
9482 char dupstr[sizeof " [ times]"
9483 + INT_STRLEN_BOUND (printmax_t)];
9484
9485 /* If you change this format, don't forget to also
9486 change message_log_check_duplicate. */
9487 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9488 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9489 insert_1 (dupstr, duplen, 1, 0, 1);
9490 }
9491 }
9492 }
9493
9494 /* If we have more than the desired maximum number of lines
9495 in the *Messages* buffer now, delete the oldest ones.
9496 This is safe because we don't have undo in this buffer. */
9497
9498 if (NATNUMP (Vmessage_log_max))
9499 {
9500 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9501 -XFASTINT (Vmessage_log_max) - 1, 0);
9502 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9503 }
9504 }
9505 BEGV = XMARKER (oldbegv)->charpos;
9506 BEGV_BYTE = marker_byte_position (oldbegv);
9507
9508 if (zv_at_end)
9509 {
9510 ZV = Z;
9511 ZV_BYTE = Z_BYTE;
9512 }
9513 else
9514 {
9515 ZV = XMARKER (oldzv)->charpos;
9516 ZV_BYTE = marker_byte_position (oldzv);
9517 }
9518
9519 if (point_at_end)
9520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9521 else
9522 /* We can't do Fgoto_char (oldpoint) because it will run some
9523 Lisp code. */
9524 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9525 XMARKER (oldpoint)->bytepos);
9526
9527 UNGCPRO;
9528 unchain_marker (XMARKER (oldpoint));
9529 unchain_marker (XMARKER (oldbegv));
9530 unchain_marker (XMARKER (oldzv));
9531
9532 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9533 set_buffer_internal (oldbuf);
9534 if (NILP (tem))
9535 windows_or_buffers_changed = old_windows_or_buffers_changed;
9536 message_log_need_newline = !nlflag;
9537 Vdeactivate_mark = old_deactivate_mark;
9538 }
9539 }
9540
9541
9542 /* We are at the end of the buffer after just having inserted a newline.
9543 (Note: We depend on the fact we won't be crossing the gap.)
9544 Check to see if the most recent message looks a lot like the previous one.
9545 Return 0 if different, 1 if the new one should just replace it, or a
9546 value N > 1 if we should also append " [N times]". */
9547
9548 static intmax_t
9549 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9550 {
9551 ptrdiff_t i;
9552 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9553 int seen_dots = 0;
9554 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9555 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9556
9557 for (i = 0; i < len; i++)
9558 {
9559 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9560 seen_dots = 1;
9561 if (p1[i] != p2[i])
9562 return seen_dots;
9563 }
9564 p1 += len;
9565 if (*p1 == '\n')
9566 return 2;
9567 if (*p1++ == ' ' && *p1++ == '[')
9568 {
9569 char *pend;
9570 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9571 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9572 return n+1;
9573 }
9574 return 0;
9575 }
9576 \f
9577
9578 /* Display an echo area message M with a specified length of NBYTES
9579 bytes. The string may include null characters. If M is 0, clear
9580 out any existing message, and let the mini-buffer text show
9581 through.
9582
9583 This may GC, so the buffer M must NOT point to a Lisp string. */
9584
9585 void
9586 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9587 {
9588 /* First flush out any partial line written with print. */
9589 message_log_maybe_newline ();
9590 if (m)
9591 message_dolog (m, nbytes, 1, multibyte);
9592 message2_nolog (m, nbytes, multibyte);
9593 }
9594
9595
9596 /* The non-logging counterpart of message2. */
9597
9598 void
9599 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9600 {
9601 struct frame *sf = SELECTED_FRAME ();
9602 message_enable_multibyte = multibyte;
9603
9604 if (FRAME_INITIAL_P (sf))
9605 {
9606 if (noninteractive_need_newline)
9607 putc ('\n', stderr);
9608 noninteractive_need_newline = 0;
9609 if (m)
9610 fwrite (m, nbytes, 1, stderr);
9611 if (cursor_in_echo_area == 0)
9612 fprintf (stderr, "\n");
9613 fflush (stderr);
9614 }
9615 /* A null message buffer means that the frame hasn't really been
9616 initialized yet. Error messages get reported properly by
9617 cmd_error, so this must be just an informative message; toss it. */
9618 else if (INTERACTIVE
9619 && sf->glyphs_initialized_p
9620 && FRAME_MESSAGE_BUF (sf))
9621 {
9622 Lisp_Object mini_window;
9623 struct frame *f;
9624
9625 /* Get the frame containing the mini-buffer
9626 that the selected frame is using. */
9627 mini_window = FRAME_MINIBUF_WINDOW (sf);
9628 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9629
9630 FRAME_SAMPLE_VISIBILITY (f);
9631 if (FRAME_VISIBLE_P (sf)
9632 && ! FRAME_VISIBLE_P (f))
9633 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9634
9635 if (m)
9636 {
9637 set_message (m, Qnil, nbytes, multibyte);
9638 if (minibuffer_auto_raise)
9639 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9640 }
9641 else
9642 clear_message (1, 1);
9643
9644 do_pending_window_change (0);
9645 echo_area_display (1);
9646 do_pending_window_change (0);
9647 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9648 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9649 }
9650 }
9651
9652
9653 /* Display an echo area message M with a specified length of NBYTES
9654 bytes. The string may include null characters. If M is not a
9655 string, clear out any existing message, and let the mini-buffer
9656 text show through.
9657
9658 This function cancels echoing. */
9659
9660 void
9661 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9662 {
9663 struct gcpro gcpro1;
9664
9665 GCPRO1 (m);
9666 clear_message (1,1);
9667 cancel_echoing ();
9668
9669 /* First flush out any partial line written with print. */
9670 message_log_maybe_newline ();
9671 if (STRINGP (m))
9672 {
9673 USE_SAFE_ALLOCA;
9674 char *buffer = SAFE_ALLOCA (nbytes);
9675 memcpy (buffer, SDATA (m), nbytes);
9676 message_dolog (buffer, nbytes, 1, multibyte);
9677 SAFE_FREE ();
9678 }
9679 message3_nolog (m, nbytes, multibyte);
9680
9681 UNGCPRO;
9682 }
9683
9684
9685 /* The non-logging version of message3.
9686 This does not cancel echoing, because it is used for echoing.
9687 Perhaps we need to make a separate function for echoing
9688 and make this cancel echoing. */
9689
9690 void
9691 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9692 {
9693 struct frame *sf = SELECTED_FRAME ();
9694 message_enable_multibyte = multibyte;
9695
9696 if (FRAME_INITIAL_P (sf))
9697 {
9698 if (noninteractive_need_newline)
9699 putc ('\n', stderr);
9700 noninteractive_need_newline = 0;
9701 if (STRINGP (m))
9702 fwrite (SDATA (m), nbytes, 1, stderr);
9703 if (cursor_in_echo_area == 0)
9704 fprintf (stderr, "\n");
9705 fflush (stderr);
9706 }
9707 /* A null message buffer means that the frame hasn't really been
9708 initialized yet. Error messages get reported properly by
9709 cmd_error, so this must be just an informative message; toss it. */
9710 else if (INTERACTIVE
9711 && sf->glyphs_initialized_p
9712 && FRAME_MESSAGE_BUF (sf))
9713 {
9714 Lisp_Object mini_window;
9715 Lisp_Object frame;
9716 struct frame *f;
9717
9718 /* Get the frame containing the mini-buffer
9719 that the selected frame is using. */
9720 mini_window = FRAME_MINIBUF_WINDOW (sf);
9721 frame = XWINDOW (mini_window)->frame;
9722 f = XFRAME (frame);
9723
9724 FRAME_SAMPLE_VISIBILITY (f);
9725 if (FRAME_VISIBLE_P (sf)
9726 && !FRAME_VISIBLE_P (f))
9727 Fmake_frame_visible (frame);
9728
9729 if (STRINGP (m) && SCHARS (m) > 0)
9730 {
9731 set_message (NULL, m, nbytes, multibyte);
9732 if (minibuffer_auto_raise)
9733 Fraise_frame (frame);
9734 /* Assume we are not echoing.
9735 (If we are, echo_now will override this.) */
9736 echo_message_buffer = Qnil;
9737 }
9738 else
9739 clear_message (1, 1);
9740
9741 do_pending_window_change (0);
9742 echo_area_display (1);
9743 do_pending_window_change (0);
9744 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9745 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9746 }
9747 }
9748
9749
9750 /* Display a null-terminated echo area message M. If M is 0, clear
9751 out any existing message, and let the mini-buffer text show through.
9752
9753 The buffer M must continue to exist until after the echo area gets
9754 cleared or some other message gets displayed there. Do not pass
9755 text that is stored in a Lisp string. Do not pass text in a buffer
9756 that was alloca'd. */
9757
9758 void
9759 message1 (const char *m)
9760 {
9761 message2 (m, (m ? strlen (m) : 0), 0);
9762 }
9763
9764
9765 /* The non-logging counterpart of message1. */
9766
9767 void
9768 message1_nolog (const char *m)
9769 {
9770 message2_nolog (m, (m ? strlen (m) : 0), 0);
9771 }
9772
9773 /* Display a message M which contains a single %s
9774 which gets replaced with STRING. */
9775
9776 void
9777 message_with_string (const char *m, Lisp_Object string, int log)
9778 {
9779 CHECK_STRING (string);
9780
9781 if (noninteractive)
9782 {
9783 if (m)
9784 {
9785 if (noninteractive_need_newline)
9786 putc ('\n', stderr);
9787 noninteractive_need_newline = 0;
9788 fprintf (stderr, m, SDATA (string));
9789 if (!cursor_in_echo_area)
9790 fprintf (stderr, "\n");
9791 fflush (stderr);
9792 }
9793 }
9794 else if (INTERACTIVE)
9795 {
9796 /* The frame whose minibuffer we're going to display the message on.
9797 It may be larger than the selected frame, so we need
9798 to use its buffer, not the selected frame's buffer. */
9799 Lisp_Object mini_window;
9800 struct frame *f, *sf = SELECTED_FRAME ();
9801
9802 /* Get the frame containing the minibuffer
9803 that the selected frame is using. */
9804 mini_window = FRAME_MINIBUF_WINDOW (sf);
9805 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9806
9807 /* A null message buffer means that the frame hasn't really been
9808 initialized yet. Error messages get reported properly by
9809 cmd_error, so this must be just an informative message; toss it. */
9810 if (FRAME_MESSAGE_BUF (f))
9811 {
9812 Lisp_Object args[2], msg;
9813 struct gcpro gcpro1, gcpro2;
9814
9815 args[0] = build_string (m);
9816 args[1] = msg = string;
9817 GCPRO2 (args[0], msg);
9818 gcpro1.nvars = 2;
9819
9820 msg = Fformat (2, args);
9821
9822 if (log)
9823 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9824 else
9825 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9826
9827 UNGCPRO;
9828
9829 /* Print should start at the beginning of the message
9830 buffer next time. */
9831 message_buf_print = 0;
9832 }
9833 }
9834 }
9835
9836
9837 /* Dump an informative message to the minibuf. If M is 0, clear out
9838 any existing message, and let the mini-buffer text show through. */
9839
9840 static void
9841 vmessage (const char *m, va_list ap)
9842 {
9843 if (noninteractive)
9844 {
9845 if (m)
9846 {
9847 if (noninteractive_need_newline)
9848 putc ('\n', stderr);
9849 noninteractive_need_newline = 0;
9850 vfprintf (stderr, m, ap);
9851 if (cursor_in_echo_area == 0)
9852 fprintf (stderr, "\n");
9853 fflush (stderr);
9854 }
9855 }
9856 else if (INTERACTIVE)
9857 {
9858 /* The frame whose mini-buffer we're going to display the message
9859 on. It may be larger than the selected frame, so we need to
9860 use its buffer, not the selected frame's buffer. */
9861 Lisp_Object mini_window;
9862 struct frame *f, *sf = SELECTED_FRAME ();
9863
9864 /* Get the frame containing the mini-buffer
9865 that the selected frame is using. */
9866 mini_window = FRAME_MINIBUF_WINDOW (sf);
9867 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9868
9869 /* A null message buffer means that the frame hasn't really been
9870 initialized yet. Error messages get reported properly by
9871 cmd_error, so this must be just an informative message; toss
9872 it. */
9873 if (FRAME_MESSAGE_BUF (f))
9874 {
9875 if (m)
9876 {
9877 ptrdiff_t len;
9878
9879 len = doprnt (FRAME_MESSAGE_BUF (f),
9880 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9881
9882 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9883 }
9884 else
9885 message1 (0);
9886
9887 /* Print should start at the beginning of the message
9888 buffer next time. */
9889 message_buf_print = 0;
9890 }
9891 }
9892 }
9893
9894 void
9895 message (const char *m, ...)
9896 {
9897 va_list ap;
9898 va_start (ap, m);
9899 vmessage (m, ap);
9900 va_end (ap);
9901 }
9902
9903
9904 #if 0
9905 /* The non-logging version of message. */
9906
9907 void
9908 message_nolog (const char *m, ...)
9909 {
9910 Lisp_Object old_log_max;
9911 va_list ap;
9912 va_start (ap, m);
9913 old_log_max = Vmessage_log_max;
9914 Vmessage_log_max = Qnil;
9915 vmessage (m, ap);
9916 Vmessage_log_max = old_log_max;
9917 va_end (ap);
9918 }
9919 #endif
9920
9921
9922 /* Display the current message in the current mini-buffer. This is
9923 only called from error handlers in process.c, and is not time
9924 critical. */
9925
9926 void
9927 update_echo_area (void)
9928 {
9929 if (!NILP (echo_area_buffer[0]))
9930 {
9931 Lisp_Object string;
9932 string = Fcurrent_message ();
9933 message3 (string, SBYTES (string),
9934 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9935 }
9936 }
9937
9938
9939 /* Make sure echo area buffers in `echo_buffers' are live.
9940 If they aren't, make new ones. */
9941
9942 static void
9943 ensure_echo_area_buffers (void)
9944 {
9945 int i;
9946
9947 for (i = 0; i < 2; ++i)
9948 if (!BUFFERP (echo_buffer[i])
9949 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9950 {
9951 char name[30];
9952 Lisp_Object old_buffer;
9953 int j;
9954
9955 old_buffer = echo_buffer[i];
9956 echo_buffer[i] = Fget_buffer_create
9957 (make_formatted_string (name, " *Echo Area %d*", i));
9958 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9959 /* to force word wrap in echo area -
9960 it was decided to postpone this*/
9961 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9962
9963 for (j = 0; j < 2; ++j)
9964 if (EQ (old_buffer, echo_area_buffer[j]))
9965 echo_area_buffer[j] = echo_buffer[i];
9966 }
9967 }
9968
9969
9970 /* Call FN with args A1..A4 with either the current or last displayed
9971 echo_area_buffer as current buffer.
9972
9973 WHICH zero means use the current message buffer
9974 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9975 from echo_buffer[] and clear it.
9976
9977 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9978 suitable buffer from echo_buffer[] and clear it.
9979
9980 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9981 that the current message becomes the last displayed one, make
9982 choose a suitable buffer for echo_area_buffer[0], and clear it.
9983
9984 Value is what FN returns. */
9985
9986 static int
9987 with_echo_area_buffer (struct window *w, int which,
9988 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9989 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9990 {
9991 Lisp_Object buffer;
9992 int this_one, the_other, clear_buffer_p, rc;
9993 ptrdiff_t count = SPECPDL_INDEX ();
9994
9995 /* If buffers aren't live, make new ones. */
9996 ensure_echo_area_buffers ();
9997
9998 clear_buffer_p = 0;
9999
10000 if (which == 0)
10001 this_one = 0, the_other = 1;
10002 else if (which > 0)
10003 this_one = 1, the_other = 0;
10004 else
10005 {
10006 this_one = 0, the_other = 1;
10007 clear_buffer_p = 1;
10008
10009 /* We need a fresh one in case the current echo buffer equals
10010 the one containing the last displayed echo area message. */
10011 if (!NILP (echo_area_buffer[this_one])
10012 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10013 echo_area_buffer[this_one] = Qnil;
10014 }
10015
10016 /* Choose a suitable buffer from echo_buffer[] is we don't
10017 have one. */
10018 if (NILP (echo_area_buffer[this_one]))
10019 {
10020 echo_area_buffer[this_one]
10021 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10022 ? echo_buffer[the_other]
10023 : echo_buffer[this_one]);
10024 clear_buffer_p = 1;
10025 }
10026
10027 buffer = echo_area_buffer[this_one];
10028
10029 /* Don't get confused by reusing the buffer used for echoing
10030 for a different purpose. */
10031 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10032 cancel_echoing ();
10033
10034 record_unwind_protect (unwind_with_echo_area_buffer,
10035 with_echo_area_buffer_unwind_data (w));
10036
10037 /* Make the echo area buffer current. Note that for display
10038 purposes, it is not necessary that the displayed window's buffer
10039 == current_buffer, except for text property lookup. So, let's
10040 only set that buffer temporarily here without doing a full
10041 Fset_window_buffer. We must also change w->pointm, though,
10042 because otherwise an assertions in unshow_buffer fails, and Emacs
10043 aborts. */
10044 set_buffer_internal_1 (XBUFFER (buffer));
10045 if (w)
10046 {
10047 wset_buffer (w, buffer);
10048 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10049 }
10050
10051 bset_undo_list (current_buffer, Qt);
10052 bset_read_only (current_buffer, Qnil);
10053 specbind (Qinhibit_read_only, Qt);
10054 specbind (Qinhibit_modification_hooks, Qt);
10055
10056 if (clear_buffer_p && Z > BEG)
10057 del_range (BEG, Z);
10058
10059 eassert (BEGV >= BEG);
10060 eassert (ZV <= Z && ZV >= BEGV);
10061
10062 rc = fn (a1, a2, a3, a4);
10063
10064 eassert (BEGV >= BEG);
10065 eassert (ZV <= Z && ZV >= BEGV);
10066
10067 unbind_to (count, Qnil);
10068 return rc;
10069 }
10070
10071
10072 /* Save state that should be preserved around the call to the function
10073 FN called in with_echo_area_buffer. */
10074
10075 static Lisp_Object
10076 with_echo_area_buffer_unwind_data (struct window *w)
10077 {
10078 int i = 0;
10079 Lisp_Object vector, tmp;
10080
10081 /* Reduce consing by keeping one vector in
10082 Vwith_echo_area_save_vector. */
10083 vector = Vwith_echo_area_save_vector;
10084 Vwith_echo_area_save_vector = Qnil;
10085
10086 if (NILP (vector))
10087 vector = Fmake_vector (make_number (7), Qnil);
10088
10089 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10090 ASET (vector, i, Vdeactivate_mark); ++i;
10091 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10092
10093 if (w)
10094 {
10095 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10096 ASET (vector, i, w->buffer); ++i;
10097 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10098 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10099 }
10100 else
10101 {
10102 int end = i + 4;
10103 for (; i < end; ++i)
10104 ASET (vector, i, Qnil);
10105 }
10106
10107 eassert (i == ASIZE (vector));
10108 return vector;
10109 }
10110
10111
10112 /* Restore global state from VECTOR which was created by
10113 with_echo_area_buffer_unwind_data. */
10114
10115 static Lisp_Object
10116 unwind_with_echo_area_buffer (Lisp_Object vector)
10117 {
10118 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10119 Vdeactivate_mark = AREF (vector, 1);
10120 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10121
10122 if (WINDOWP (AREF (vector, 3)))
10123 {
10124 struct window *w;
10125 Lisp_Object buffer, charpos, bytepos;
10126
10127 w = XWINDOW (AREF (vector, 3));
10128 buffer = AREF (vector, 4);
10129 charpos = AREF (vector, 5);
10130 bytepos = AREF (vector, 6);
10131
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer,
10134 XFASTINT (charpos), XFASTINT (bytepos));
10135 }
10136
10137 Vwith_echo_area_save_vector = vector;
10138 return Qnil;
10139 }
10140
10141
10142 /* Set up the echo area for use by print functions. MULTIBYTE_P
10143 non-zero means we will print multibyte. */
10144
10145 void
10146 setup_echo_area_for_printing (int multibyte_p)
10147 {
10148 /* If we can't find an echo area any more, exit. */
10149 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10150 Fkill_emacs (Qnil);
10151
10152 ensure_echo_area_buffers ();
10153
10154 if (!message_buf_print)
10155 {
10156 /* A message has been output since the last time we printed.
10157 Choose a fresh echo area buffer. */
10158 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10159 echo_area_buffer[0] = echo_buffer[1];
10160 else
10161 echo_area_buffer[0] = echo_buffer[0];
10162
10163 /* Switch to that buffer and clear it. */
10164 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10165 bset_truncate_lines (current_buffer, Qnil);
10166
10167 if (Z > BEG)
10168 {
10169 ptrdiff_t count = SPECPDL_INDEX ();
10170 specbind (Qinhibit_read_only, Qt);
10171 /* Note that undo recording is always disabled. */
10172 del_range (BEG, Z);
10173 unbind_to (count, Qnil);
10174 }
10175 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10176
10177 /* Set up the buffer for the multibyteness we need. */
10178 if (multibyte_p
10179 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10180 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10181
10182 /* Raise the frame containing the echo area. */
10183 if (minibuffer_auto_raise)
10184 {
10185 struct frame *sf = SELECTED_FRAME ();
10186 Lisp_Object mini_window;
10187 mini_window = FRAME_MINIBUF_WINDOW (sf);
10188 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10189 }
10190
10191 message_log_maybe_newline ();
10192 message_buf_print = 1;
10193 }
10194 else
10195 {
10196 if (NILP (echo_area_buffer[0]))
10197 {
10198 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10199 echo_area_buffer[0] = echo_buffer[1];
10200 else
10201 echo_area_buffer[0] = echo_buffer[0];
10202 }
10203
10204 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10205 {
10206 /* Someone switched buffers between print requests. */
10207 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10208 bset_truncate_lines (current_buffer, Qnil);
10209 }
10210 }
10211 }
10212
10213
10214 /* Display an echo area message in window W. Value is non-zero if W's
10215 height is changed. If display_last_displayed_message_p is
10216 non-zero, display the message that was last displayed, otherwise
10217 display the current message. */
10218
10219 static int
10220 display_echo_area (struct window *w)
10221 {
10222 int i, no_message_p, window_height_changed_p;
10223
10224 /* Temporarily disable garbage collections while displaying the echo
10225 area. This is done because a GC can print a message itself.
10226 That message would modify the echo area buffer's contents while a
10227 redisplay of the buffer is going on, and seriously confuse
10228 redisplay. */
10229 ptrdiff_t count = inhibit_garbage_collection ();
10230
10231 /* If there is no message, we must call display_echo_area_1
10232 nevertheless because it resizes the window. But we will have to
10233 reset the echo_area_buffer in question to nil at the end because
10234 with_echo_area_buffer will sets it to an empty buffer. */
10235 i = display_last_displayed_message_p ? 1 : 0;
10236 no_message_p = NILP (echo_area_buffer[i]);
10237
10238 window_height_changed_p
10239 = with_echo_area_buffer (w, display_last_displayed_message_p,
10240 display_echo_area_1,
10241 (intptr_t) w, Qnil, 0, 0);
10242
10243 if (no_message_p)
10244 echo_area_buffer[i] = Qnil;
10245
10246 unbind_to (count, Qnil);
10247 return window_height_changed_p;
10248 }
10249
10250
10251 /* Helper for display_echo_area. Display the current buffer which
10252 contains the current echo area message in window W, a mini-window,
10253 a pointer to which is passed in A1. A2..A4 are currently not used.
10254 Change the height of W so that all of the message is displayed.
10255 Value is non-zero if height of W was changed. */
10256
10257 static int
10258 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10259 {
10260 intptr_t i1 = a1;
10261 struct window *w = (struct window *) i1;
10262 Lisp_Object window;
10263 struct text_pos start;
10264 int window_height_changed_p = 0;
10265
10266 /* Do this before displaying, so that we have a large enough glyph
10267 matrix for the display. If we can't get enough space for the
10268 whole text, display the last N lines. That works by setting w->start. */
10269 window_height_changed_p = resize_mini_window (w, 0);
10270
10271 /* Use the starting position chosen by resize_mini_window. */
10272 SET_TEXT_POS_FROM_MARKER (start, w->start);
10273
10274 /* Display. */
10275 clear_glyph_matrix (w->desired_matrix);
10276 XSETWINDOW (window, w);
10277 try_window (window, start, 0);
10278
10279 return window_height_changed_p;
10280 }
10281
10282
10283 /* Resize the echo area window to exactly the size needed for the
10284 currently displayed message, if there is one. If a mini-buffer
10285 is active, don't shrink it. */
10286
10287 void
10288 resize_echo_area_exactly (void)
10289 {
10290 if (BUFFERP (echo_area_buffer[0])
10291 && WINDOWP (echo_area_window))
10292 {
10293 struct window *w = XWINDOW (echo_area_window);
10294 int resized_p;
10295 Lisp_Object resize_exactly;
10296
10297 if (minibuf_level == 0)
10298 resize_exactly = Qt;
10299 else
10300 resize_exactly = Qnil;
10301
10302 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10303 (intptr_t) w, resize_exactly,
10304 0, 0);
10305 if (resized_p)
10306 {
10307 ++windows_or_buffers_changed;
10308 ++update_mode_lines;
10309 redisplay_internal ();
10310 }
10311 }
10312 }
10313
10314
10315 /* Callback function for with_echo_area_buffer, when used from
10316 resize_echo_area_exactly. A1 contains a pointer to the window to
10317 resize, EXACTLY non-nil means resize the mini-window exactly to the
10318 size of the text displayed. A3 and A4 are not used. Value is what
10319 resize_mini_window returns. */
10320
10321 static int
10322 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10323 {
10324 intptr_t i1 = a1;
10325 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10326 }
10327
10328
10329 /* Resize mini-window W to fit the size of its contents. EXACT_P
10330 means size the window exactly to the size needed. Otherwise, it's
10331 only enlarged until W's buffer is empty.
10332
10333 Set W->start to the right place to begin display. If the whole
10334 contents fit, start at the beginning. Otherwise, start so as
10335 to make the end of the contents appear. This is particularly
10336 important for y-or-n-p, but seems desirable generally.
10337
10338 Value is non-zero if the window height has been changed. */
10339
10340 int
10341 resize_mini_window (struct window *w, int exact_p)
10342 {
10343 struct frame *f = XFRAME (w->frame);
10344 int window_height_changed_p = 0;
10345
10346 eassert (MINI_WINDOW_P (w));
10347
10348 /* By default, start display at the beginning. */
10349 set_marker_both (w->start, w->buffer,
10350 BUF_BEGV (XBUFFER (w->buffer)),
10351 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10352
10353 /* Don't resize windows while redisplaying a window; it would
10354 confuse redisplay functions when the size of the window they are
10355 displaying changes from under them. Such a resizing can happen,
10356 for instance, when which-func prints a long message while
10357 we are running fontification-functions. We're running these
10358 functions with safe_call which binds inhibit-redisplay to t. */
10359 if (!NILP (Vinhibit_redisplay))
10360 return 0;
10361
10362 /* Nil means don't try to resize. */
10363 if (NILP (Vresize_mini_windows)
10364 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10365 return 0;
10366
10367 if (!FRAME_MINIBUF_ONLY_P (f))
10368 {
10369 struct it it;
10370 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10371 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10372 int height;
10373 EMACS_INT max_height;
10374 int unit = FRAME_LINE_HEIGHT (f);
10375 struct text_pos start;
10376 struct buffer *old_current_buffer = NULL;
10377
10378 if (current_buffer != XBUFFER (w->buffer))
10379 {
10380 old_current_buffer = current_buffer;
10381 set_buffer_internal (XBUFFER (w->buffer));
10382 }
10383
10384 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10385
10386 /* Compute the max. number of lines specified by the user. */
10387 if (FLOATP (Vmax_mini_window_height))
10388 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10389 else if (INTEGERP (Vmax_mini_window_height))
10390 max_height = XINT (Vmax_mini_window_height);
10391 else
10392 max_height = total_height / 4;
10393
10394 /* Correct that max. height if it's bogus. */
10395 max_height = max (1, max_height);
10396 max_height = min (total_height, max_height);
10397
10398 /* Find out the height of the text in the window. */
10399 if (it.line_wrap == TRUNCATE)
10400 height = 1;
10401 else
10402 {
10403 last_height = 0;
10404 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10405 if (it.max_ascent == 0 && it.max_descent == 0)
10406 height = it.current_y + last_height;
10407 else
10408 height = it.current_y + it.max_ascent + it.max_descent;
10409 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10410 height = (height + unit - 1) / unit;
10411 }
10412
10413 /* Compute a suitable window start. */
10414 if (height > max_height)
10415 {
10416 height = max_height;
10417 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10418 move_it_vertically_backward (&it, (height - 1) * unit);
10419 start = it.current.pos;
10420 }
10421 else
10422 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10423 SET_MARKER_FROM_TEXT_POS (w->start, start);
10424
10425 if (EQ (Vresize_mini_windows, Qgrow_only))
10426 {
10427 /* Let it grow only, until we display an empty message, in which
10428 case the window shrinks again. */
10429 if (height > WINDOW_TOTAL_LINES (w))
10430 {
10431 int old_height = WINDOW_TOTAL_LINES (w);
10432 freeze_window_starts (f, 1);
10433 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10434 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10435 }
10436 else if (height < WINDOW_TOTAL_LINES (w)
10437 && (exact_p || BEGV == ZV))
10438 {
10439 int old_height = WINDOW_TOTAL_LINES (w);
10440 freeze_window_starts (f, 0);
10441 shrink_mini_window (w);
10442 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10443 }
10444 }
10445 else
10446 {
10447 /* Always resize to exact size needed. */
10448 if (height > WINDOW_TOTAL_LINES (w))
10449 {
10450 int old_height = WINDOW_TOTAL_LINES (w);
10451 freeze_window_starts (f, 1);
10452 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10453 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10454 }
10455 else if (height < WINDOW_TOTAL_LINES (w))
10456 {
10457 int old_height = WINDOW_TOTAL_LINES (w);
10458 freeze_window_starts (f, 0);
10459 shrink_mini_window (w);
10460
10461 if (height)
10462 {
10463 freeze_window_starts (f, 1);
10464 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10465 }
10466
10467 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10468 }
10469 }
10470
10471 if (old_current_buffer)
10472 set_buffer_internal (old_current_buffer);
10473 }
10474
10475 return window_height_changed_p;
10476 }
10477
10478
10479 /* Value is the current message, a string, or nil if there is no
10480 current message. */
10481
10482 Lisp_Object
10483 current_message (void)
10484 {
10485 Lisp_Object msg;
10486
10487 if (!BUFFERP (echo_area_buffer[0]))
10488 msg = Qnil;
10489 else
10490 {
10491 with_echo_area_buffer (0, 0, current_message_1,
10492 (intptr_t) &msg, Qnil, 0, 0);
10493 if (NILP (msg))
10494 echo_area_buffer[0] = Qnil;
10495 }
10496
10497 return msg;
10498 }
10499
10500
10501 static int
10502 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10503 {
10504 intptr_t i1 = a1;
10505 Lisp_Object *msg = (Lisp_Object *) i1;
10506
10507 if (Z > BEG)
10508 *msg = make_buffer_string (BEG, Z, 1);
10509 else
10510 *msg = Qnil;
10511 return 0;
10512 }
10513
10514
10515 /* Push the current message on Vmessage_stack for later restoration
10516 by restore_message. Value is non-zero if the current message isn't
10517 empty. This is a relatively infrequent operation, so it's not
10518 worth optimizing. */
10519
10520 bool
10521 push_message (void)
10522 {
10523 Lisp_Object msg = current_message ();
10524 Vmessage_stack = Fcons (msg, Vmessage_stack);
10525 return STRINGP (msg);
10526 }
10527
10528
10529 /* Restore message display from the top of Vmessage_stack. */
10530
10531 void
10532 restore_message (void)
10533 {
10534 Lisp_Object msg;
10535
10536 eassert (CONSP (Vmessage_stack));
10537 msg = XCAR (Vmessage_stack);
10538 if (STRINGP (msg))
10539 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10540 else
10541 message3_nolog (msg, 0, 0);
10542 }
10543
10544
10545 /* Handler for record_unwind_protect calling pop_message. */
10546
10547 Lisp_Object
10548 pop_message_unwind (Lisp_Object dummy)
10549 {
10550 pop_message ();
10551 return Qnil;
10552 }
10553
10554 /* Pop the top-most entry off Vmessage_stack. */
10555
10556 static void
10557 pop_message (void)
10558 {
10559 eassert (CONSP (Vmessage_stack));
10560 Vmessage_stack = XCDR (Vmessage_stack);
10561 }
10562
10563
10564 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10565 exits. If the stack is not empty, we have a missing pop_message
10566 somewhere. */
10567
10568 void
10569 check_message_stack (void)
10570 {
10571 if (!NILP (Vmessage_stack))
10572 emacs_abort ();
10573 }
10574
10575
10576 /* Truncate to NCHARS what will be displayed in the echo area the next
10577 time we display it---but don't redisplay it now. */
10578
10579 void
10580 truncate_echo_area (ptrdiff_t nchars)
10581 {
10582 if (nchars == 0)
10583 echo_area_buffer[0] = Qnil;
10584 /* A null message buffer means that the frame hasn't really been
10585 initialized yet. Error messages get reported properly by
10586 cmd_error, so this must be just an informative message; toss it. */
10587 else if (!noninteractive
10588 && INTERACTIVE
10589 && !NILP (echo_area_buffer[0]))
10590 {
10591 struct frame *sf = SELECTED_FRAME ();
10592 if (FRAME_MESSAGE_BUF (sf))
10593 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10594 }
10595 }
10596
10597
10598 /* Helper function for truncate_echo_area. Truncate the current
10599 message to at most NCHARS characters. */
10600
10601 static int
10602 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10603 {
10604 if (BEG + nchars < Z)
10605 del_range (BEG + nchars, Z);
10606 if (Z == BEG)
10607 echo_area_buffer[0] = Qnil;
10608 return 0;
10609 }
10610
10611 /* Set the current message to a substring of S or STRING.
10612
10613 If STRING is a Lisp string, set the message to the first NBYTES
10614 bytes from STRING. NBYTES zero means use the whole string. If
10615 STRING is multibyte, the message will be displayed multibyte.
10616
10617 If S is not null, set the message to the first LEN bytes of S. LEN
10618 zero means use the whole string. MULTIBYTE_P non-zero means S is
10619 multibyte. Display the message multibyte in that case.
10620
10621 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10622 to t before calling set_message_1 (which calls insert).
10623 */
10624
10625 static void
10626 set_message (const char *s, Lisp_Object string,
10627 ptrdiff_t nbytes, int multibyte_p)
10628 {
10629 message_enable_multibyte
10630 = ((s && multibyte_p)
10631 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10632
10633 with_echo_area_buffer (0, -1, set_message_1,
10634 (intptr_t) s, string, nbytes, multibyte_p);
10635 message_buf_print = 0;
10636 help_echo_showing_p = 0;
10637
10638 if (STRINGP (Vdebug_on_message)
10639 && fast_string_match (Vdebug_on_message, string) >= 0)
10640 call_debugger (list2 (Qerror, string));
10641 }
10642
10643
10644 /* Helper function for set_message. Arguments have the same meaning
10645 as there, with A1 corresponding to S and A2 corresponding to STRING
10646 This function is called with the echo area buffer being
10647 current. */
10648
10649 static int
10650 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10651 {
10652 intptr_t i1 = a1;
10653 const char *s = (const char *) i1;
10654 const unsigned char *msg = (const unsigned char *) s;
10655 Lisp_Object string = a2;
10656
10657 /* Change multibyteness of the echo buffer appropriately. */
10658 if (message_enable_multibyte
10659 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10660 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10661
10662 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10663 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10664 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10665
10666 /* Insert new message at BEG. */
10667 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10668
10669 if (STRINGP (string))
10670 {
10671 ptrdiff_t nchars;
10672
10673 if (nbytes == 0)
10674 nbytes = SBYTES (string);
10675 nchars = string_byte_to_char (string, nbytes);
10676
10677 /* This function takes care of single/multibyte conversion. We
10678 just have to ensure that the echo area buffer has the right
10679 setting of enable_multibyte_characters. */
10680 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10681 }
10682 else if (s)
10683 {
10684 if (nbytes == 0)
10685 nbytes = strlen (s);
10686
10687 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10688 {
10689 /* Convert from multi-byte to single-byte. */
10690 ptrdiff_t i;
10691 int c, n;
10692 char work[1];
10693
10694 /* Convert a multibyte string to single-byte. */
10695 for (i = 0; i < nbytes; i += n)
10696 {
10697 c = string_char_and_length (msg + i, &n);
10698 work[0] = (ASCII_CHAR_P (c)
10699 ? c
10700 : multibyte_char_to_unibyte (c));
10701 insert_1_both (work, 1, 1, 1, 0, 0);
10702 }
10703 }
10704 else if (!multibyte_p
10705 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10706 {
10707 /* Convert from single-byte to multi-byte. */
10708 ptrdiff_t i;
10709 int c, n;
10710 unsigned char str[MAX_MULTIBYTE_LENGTH];
10711
10712 /* Convert a single-byte string to multibyte. */
10713 for (i = 0; i < nbytes; i++)
10714 {
10715 c = msg[i];
10716 MAKE_CHAR_MULTIBYTE (c);
10717 n = CHAR_STRING (c, str);
10718 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10719 }
10720 }
10721 else
10722 insert_1 (s, nbytes, 1, 0, 0);
10723 }
10724
10725 return 0;
10726 }
10727
10728
10729 /* Clear messages. CURRENT_P non-zero means clear the current
10730 message. LAST_DISPLAYED_P non-zero means clear the message
10731 last displayed. */
10732
10733 void
10734 clear_message (int current_p, int last_displayed_p)
10735 {
10736 if (current_p)
10737 {
10738 echo_area_buffer[0] = Qnil;
10739 message_cleared_p = 1;
10740 }
10741
10742 if (last_displayed_p)
10743 echo_area_buffer[1] = Qnil;
10744
10745 message_buf_print = 0;
10746 }
10747
10748 /* Clear garbaged frames.
10749
10750 This function is used where the old redisplay called
10751 redraw_garbaged_frames which in turn called redraw_frame which in
10752 turn called clear_frame. The call to clear_frame was a source of
10753 flickering. I believe a clear_frame is not necessary. It should
10754 suffice in the new redisplay to invalidate all current matrices,
10755 and ensure a complete redisplay of all windows. */
10756
10757 static void
10758 clear_garbaged_frames (void)
10759 {
10760 if (frame_garbaged)
10761 {
10762 Lisp_Object tail, frame;
10763 int changed_count = 0;
10764
10765 FOR_EACH_FRAME (tail, frame)
10766 {
10767 struct frame *f = XFRAME (frame);
10768
10769 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10770 {
10771 if (f->resized_p)
10772 {
10773 Fredraw_frame (frame);
10774 f->force_flush_display_p = 1;
10775 }
10776 clear_current_matrices (f);
10777 changed_count++;
10778 f->garbaged = 0;
10779 f->resized_p = 0;
10780 }
10781 }
10782
10783 frame_garbaged = 0;
10784 if (changed_count)
10785 ++windows_or_buffers_changed;
10786 }
10787 }
10788
10789
10790 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10791 is non-zero update selected_frame. Value is non-zero if the
10792 mini-windows height has been changed. */
10793
10794 static int
10795 echo_area_display (int update_frame_p)
10796 {
10797 Lisp_Object mini_window;
10798 struct window *w;
10799 struct frame *f;
10800 int window_height_changed_p = 0;
10801 struct frame *sf = SELECTED_FRAME ();
10802
10803 mini_window = FRAME_MINIBUF_WINDOW (sf);
10804 w = XWINDOW (mini_window);
10805 f = XFRAME (WINDOW_FRAME (w));
10806
10807 /* Don't display if frame is invisible or not yet initialized. */
10808 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10809 return 0;
10810
10811 #ifdef HAVE_WINDOW_SYSTEM
10812 /* When Emacs starts, selected_frame may be the initial terminal
10813 frame. If we let this through, a message would be displayed on
10814 the terminal. */
10815 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10816 return 0;
10817 #endif /* HAVE_WINDOW_SYSTEM */
10818
10819 /* Redraw garbaged frames. */
10820 if (frame_garbaged)
10821 clear_garbaged_frames ();
10822
10823 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10824 {
10825 echo_area_window = mini_window;
10826 window_height_changed_p = display_echo_area (w);
10827 w->must_be_updated_p = 1;
10828
10829 /* Update the display, unless called from redisplay_internal.
10830 Also don't update the screen during redisplay itself. The
10831 update will happen at the end of redisplay, and an update
10832 here could cause confusion. */
10833 if (update_frame_p && !redisplaying_p)
10834 {
10835 int n = 0;
10836
10837 /* If the display update has been interrupted by pending
10838 input, update mode lines in the frame. Due to the
10839 pending input, it might have been that redisplay hasn't
10840 been called, so that mode lines above the echo area are
10841 garbaged. This looks odd, so we prevent it here. */
10842 if (!display_completed)
10843 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10844
10845 if (window_height_changed_p
10846 /* Don't do this if Emacs is shutting down. Redisplay
10847 needs to run hooks. */
10848 && !NILP (Vrun_hooks))
10849 {
10850 /* Must update other windows. Likewise as in other
10851 cases, don't let this update be interrupted by
10852 pending input. */
10853 ptrdiff_t count = SPECPDL_INDEX ();
10854 specbind (Qredisplay_dont_pause, Qt);
10855 windows_or_buffers_changed = 1;
10856 redisplay_internal ();
10857 unbind_to (count, Qnil);
10858 }
10859 else if (FRAME_WINDOW_P (f) && n == 0)
10860 {
10861 /* Window configuration is the same as before.
10862 Can do with a display update of the echo area,
10863 unless we displayed some mode lines. */
10864 update_single_window (w, 1);
10865 FRAME_RIF (f)->flush_display (f);
10866 }
10867 else
10868 update_frame (f, 1, 1);
10869
10870 /* If cursor is in the echo area, make sure that the next
10871 redisplay displays the minibuffer, so that the cursor will
10872 be replaced with what the minibuffer wants. */
10873 if (cursor_in_echo_area)
10874 ++windows_or_buffers_changed;
10875 }
10876 }
10877 else if (!EQ (mini_window, selected_window))
10878 windows_or_buffers_changed++;
10879
10880 /* Last displayed message is now the current message. */
10881 echo_area_buffer[1] = echo_area_buffer[0];
10882 /* Inform read_char that we're not echoing. */
10883 echo_message_buffer = Qnil;
10884
10885 /* Prevent redisplay optimization in redisplay_internal by resetting
10886 this_line_start_pos. This is done because the mini-buffer now
10887 displays the message instead of its buffer text. */
10888 if (EQ (mini_window, selected_window))
10889 CHARPOS (this_line_start_pos) = 0;
10890
10891 return window_height_changed_p;
10892 }
10893
10894
10895 \f
10896 /***********************************************************************
10897 Mode Lines and Frame Titles
10898 ***********************************************************************/
10899
10900 /* A buffer for constructing non-propertized mode-line strings and
10901 frame titles in it; allocated from the heap in init_xdisp and
10902 resized as needed in store_mode_line_noprop_char. */
10903
10904 static char *mode_line_noprop_buf;
10905
10906 /* The buffer's end, and a current output position in it. */
10907
10908 static char *mode_line_noprop_buf_end;
10909 static char *mode_line_noprop_ptr;
10910
10911 #define MODE_LINE_NOPROP_LEN(start) \
10912 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10913
10914 static enum {
10915 MODE_LINE_DISPLAY = 0,
10916 MODE_LINE_TITLE,
10917 MODE_LINE_NOPROP,
10918 MODE_LINE_STRING
10919 } mode_line_target;
10920
10921 /* Alist that caches the results of :propertize.
10922 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10923 static Lisp_Object mode_line_proptrans_alist;
10924
10925 /* List of strings making up the mode-line. */
10926 static Lisp_Object mode_line_string_list;
10927
10928 /* Base face property when building propertized mode line string. */
10929 static Lisp_Object mode_line_string_face;
10930 static Lisp_Object mode_line_string_face_prop;
10931
10932
10933 /* Unwind data for mode line strings */
10934
10935 static Lisp_Object Vmode_line_unwind_vector;
10936
10937 static Lisp_Object
10938 format_mode_line_unwind_data (struct frame *target_frame,
10939 struct buffer *obuf,
10940 Lisp_Object owin,
10941 int save_proptrans)
10942 {
10943 Lisp_Object vector, tmp;
10944
10945 /* Reduce consing by keeping one vector in
10946 Vwith_echo_area_save_vector. */
10947 vector = Vmode_line_unwind_vector;
10948 Vmode_line_unwind_vector = Qnil;
10949
10950 if (NILP (vector))
10951 vector = Fmake_vector (make_number (10), Qnil);
10952
10953 ASET (vector, 0, make_number (mode_line_target));
10954 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10955 ASET (vector, 2, mode_line_string_list);
10956 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10957 ASET (vector, 4, mode_line_string_face);
10958 ASET (vector, 5, mode_line_string_face_prop);
10959
10960 if (obuf)
10961 XSETBUFFER (tmp, obuf);
10962 else
10963 tmp = Qnil;
10964 ASET (vector, 6, tmp);
10965 ASET (vector, 7, owin);
10966 if (target_frame)
10967 {
10968 /* Similarly to `with-selected-window', if the operation selects
10969 a window on another frame, we must restore that frame's
10970 selected window, and (for a tty) the top-frame. */
10971 ASET (vector, 8, target_frame->selected_window);
10972 if (FRAME_TERMCAP_P (target_frame))
10973 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10974 }
10975
10976 return vector;
10977 }
10978
10979 static Lisp_Object
10980 unwind_format_mode_line (Lisp_Object vector)
10981 {
10982 Lisp_Object old_window = AREF (vector, 7);
10983 Lisp_Object target_frame_window = AREF (vector, 8);
10984 Lisp_Object old_top_frame = AREF (vector, 9);
10985
10986 mode_line_target = XINT (AREF (vector, 0));
10987 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10988 mode_line_string_list = AREF (vector, 2);
10989 if (! EQ (AREF (vector, 3), Qt))
10990 mode_line_proptrans_alist = AREF (vector, 3);
10991 mode_line_string_face = AREF (vector, 4);
10992 mode_line_string_face_prop = AREF (vector, 5);
10993
10994 /* Select window before buffer, since it may change the buffer. */
10995 if (!NILP (old_window))
10996 {
10997 /* If the operation that we are unwinding had selected a window
10998 on a different frame, reset its frame-selected-window. For a
10999 text terminal, reset its top-frame if necessary. */
11000 if (!NILP (target_frame_window))
11001 {
11002 Lisp_Object frame
11003 = WINDOW_FRAME (XWINDOW (target_frame_window));
11004
11005 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11006 Fselect_window (target_frame_window, Qt);
11007
11008 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11009 Fselect_frame (old_top_frame, Qt);
11010 }
11011
11012 Fselect_window (old_window, Qt);
11013 }
11014
11015 if (!NILP (AREF (vector, 6)))
11016 {
11017 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11018 ASET (vector, 6, Qnil);
11019 }
11020
11021 Vmode_line_unwind_vector = vector;
11022 return Qnil;
11023 }
11024
11025
11026 /* Store a single character C for the frame title in mode_line_noprop_buf.
11027 Re-allocate mode_line_noprop_buf if necessary. */
11028
11029 static void
11030 store_mode_line_noprop_char (char c)
11031 {
11032 /* If output position has reached the end of the allocated buffer,
11033 increase the buffer's size. */
11034 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11035 {
11036 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11037 ptrdiff_t size = len;
11038 mode_line_noprop_buf =
11039 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11040 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11041 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11042 }
11043
11044 *mode_line_noprop_ptr++ = c;
11045 }
11046
11047
11048 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11049 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11050 characters that yield more columns than PRECISION; PRECISION <= 0
11051 means copy the whole string. Pad with spaces until FIELD_WIDTH
11052 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11053 pad. Called from display_mode_element when it is used to build a
11054 frame title. */
11055
11056 static int
11057 store_mode_line_noprop (const char *string, int field_width, int precision)
11058 {
11059 const unsigned char *str = (const unsigned char *) string;
11060 int n = 0;
11061 ptrdiff_t dummy, nbytes;
11062
11063 /* Copy at most PRECISION chars from STR. */
11064 nbytes = strlen (string);
11065 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11066 while (nbytes--)
11067 store_mode_line_noprop_char (*str++);
11068
11069 /* Fill up with spaces until FIELD_WIDTH reached. */
11070 while (field_width > 0
11071 && n < field_width)
11072 {
11073 store_mode_line_noprop_char (' ');
11074 ++n;
11075 }
11076
11077 return n;
11078 }
11079
11080 /***********************************************************************
11081 Frame Titles
11082 ***********************************************************************/
11083
11084 #ifdef HAVE_WINDOW_SYSTEM
11085
11086 /* Set the title of FRAME, if it has changed. The title format is
11087 Vicon_title_format if FRAME is iconified, otherwise it is
11088 frame_title_format. */
11089
11090 static void
11091 x_consider_frame_title (Lisp_Object frame)
11092 {
11093 struct frame *f = XFRAME (frame);
11094
11095 if (FRAME_WINDOW_P (f)
11096 || FRAME_MINIBUF_ONLY_P (f)
11097 || f->explicit_name)
11098 {
11099 /* Do we have more than one visible frame on this X display? */
11100 Lisp_Object tail;
11101 Lisp_Object fmt;
11102 ptrdiff_t title_start;
11103 char *title;
11104 ptrdiff_t len;
11105 struct it it;
11106 ptrdiff_t count = SPECPDL_INDEX ();
11107
11108 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11109 {
11110 Lisp_Object other_frame = XCAR (tail);
11111 struct frame *tf = XFRAME (other_frame);
11112
11113 if (tf != f
11114 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11115 && !FRAME_MINIBUF_ONLY_P (tf)
11116 && !EQ (other_frame, tip_frame)
11117 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11118 break;
11119 }
11120
11121 /* Set global variable indicating that multiple frames exist. */
11122 multiple_frames = CONSP (tail);
11123
11124 /* Switch to the buffer of selected window of the frame. Set up
11125 mode_line_target so that display_mode_element will output into
11126 mode_line_noprop_buf; then display the title. */
11127 record_unwind_protect (unwind_format_mode_line,
11128 format_mode_line_unwind_data
11129 (f, current_buffer, selected_window, 0));
11130
11131 Fselect_window (f->selected_window, Qt);
11132 set_buffer_internal_1
11133 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11134 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11135
11136 mode_line_target = MODE_LINE_TITLE;
11137 title_start = MODE_LINE_NOPROP_LEN (0);
11138 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11139 NULL, DEFAULT_FACE_ID);
11140 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11141 len = MODE_LINE_NOPROP_LEN (title_start);
11142 title = mode_line_noprop_buf + title_start;
11143 unbind_to (count, Qnil);
11144
11145 /* Set the title only if it's changed. This avoids consing in
11146 the common case where it hasn't. (If it turns out that we've
11147 already wasted too much time by walking through the list with
11148 display_mode_element, then we might need to optimize at a
11149 higher level than this.) */
11150 if (! STRINGP (f->name)
11151 || SBYTES (f->name) != len
11152 || memcmp (title, SDATA (f->name), len) != 0)
11153 x_implicitly_set_name (f, make_string (title, len), Qnil);
11154 }
11155 }
11156
11157 #endif /* not HAVE_WINDOW_SYSTEM */
11158
11159 \f
11160 /***********************************************************************
11161 Menu Bars
11162 ***********************************************************************/
11163
11164
11165 /* Prepare for redisplay by updating menu-bar item lists when
11166 appropriate. This can call eval. */
11167
11168 void
11169 prepare_menu_bars (void)
11170 {
11171 int all_windows;
11172 struct gcpro gcpro1, gcpro2;
11173 struct frame *f;
11174 Lisp_Object tooltip_frame;
11175
11176 #ifdef HAVE_WINDOW_SYSTEM
11177 tooltip_frame = tip_frame;
11178 #else
11179 tooltip_frame = Qnil;
11180 #endif
11181
11182 /* Update all frame titles based on their buffer names, etc. We do
11183 this before the menu bars so that the buffer-menu will show the
11184 up-to-date frame titles. */
11185 #ifdef HAVE_WINDOW_SYSTEM
11186 if (windows_or_buffers_changed || update_mode_lines)
11187 {
11188 Lisp_Object tail, frame;
11189
11190 FOR_EACH_FRAME (tail, frame)
11191 {
11192 f = XFRAME (frame);
11193 if (!EQ (frame, tooltip_frame)
11194 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11195 x_consider_frame_title (frame);
11196 }
11197 }
11198 #endif /* HAVE_WINDOW_SYSTEM */
11199
11200 /* Update the menu bar item lists, if appropriate. This has to be
11201 done before any actual redisplay or generation of display lines. */
11202 all_windows = (update_mode_lines
11203 || buffer_shared > 1
11204 || windows_or_buffers_changed);
11205 if (all_windows)
11206 {
11207 Lisp_Object tail, frame;
11208 ptrdiff_t count = SPECPDL_INDEX ();
11209 /* 1 means that update_menu_bar has run its hooks
11210 so any further calls to update_menu_bar shouldn't do so again. */
11211 int menu_bar_hooks_run = 0;
11212
11213 record_unwind_save_match_data ();
11214
11215 FOR_EACH_FRAME (tail, frame)
11216 {
11217 f = XFRAME (frame);
11218
11219 /* Ignore tooltip frame. */
11220 if (EQ (frame, tooltip_frame))
11221 continue;
11222
11223 /* If a window on this frame changed size, report that to
11224 the user and clear the size-change flag. */
11225 if (FRAME_WINDOW_SIZES_CHANGED (f))
11226 {
11227 Lisp_Object functions;
11228
11229 /* Clear flag first in case we get an error below. */
11230 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11231 functions = Vwindow_size_change_functions;
11232 GCPRO2 (tail, functions);
11233
11234 while (CONSP (functions))
11235 {
11236 if (!EQ (XCAR (functions), Qt))
11237 call1 (XCAR (functions), frame);
11238 functions = XCDR (functions);
11239 }
11240 UNGCPRO;
11241 }
11242
11243 GCPRO1 (tail);
11244 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11245 #ifdef HAVE_WINDOW_SYSTEM
11246 update_tool_bar (f, 0);
11247 #endif
11248 #ifdef HAVE_NS
11249 if (windows_or_buffers_changed
11250 && FRAME_NS_P (f))
11251 ns_set_doc_edited
11252 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11253 #endif
11254 UNGCPRO;
11255 }
11256
11257 unbind_to (count, Qnil);
11258 }
11259 else
11260 {
11261 struct frame *sf = SELECTED_FRAME ();
11262 update_menu_bar (sf, 1, 0);
11263 #ifdef HAVE_WINDOW_SYSTEM
11264 update_tool_bar (sf, 1);
11265 #endif
11266 }
11267 }
11268
11269
11270 /* Update the menu bar item list for frame F. This has to be done
11271 before we start to fill in any display lines, because it can call
11272 eval.
11273
11274 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11275
11276 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11277 already ran the menu bar hooks for this redisplay, so there
11278 is no need to run them again. The return value is the
11279 updated value of this flag, to pass to the next call. */
11280
11281 static int
11282 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11283 {
11284 Lisp_Object window;
11285 register struct window *w;
11286
11287 /* If called recursively during a menu update, do nothing. This can
11288 happen when, for instance, an activate-menubar-hook causes a
11289 redisplay. */
11290 if (inhibit_menubar_update)
11291 return hooks_run;
11292
11293 window = FRAME_SELECTED_WINDOW (f);
11294 w = XWINDOW (window);
11295
11296 if (FRAME_WINDOW_P (f)
11297 ?
11298 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11299 || defined (HAVE_NS) || defined (USE_GTK)
11300 FRAME_EXTERNAL_MENU_BAR (f)
11301 #else
11302 FRAME_MENU_BAR_LINES (f) > 0
11303 #endif
11304 : FRAME_MENU_BAR_LINES (f) > 0)
11305 {
11306 /* If the user has switched buffers or windows, we need to
11307 recompute to reflect the new bindings. But we'll
11308 recompute when update_mode_lines is set too; that means
11309 that people can use force-mode-line-update to request
11310 that the menu bar be recomputed. The adverse effect on
11311 the rest of the redisplay algorithm is about the same as
11312 windows_or_buffers_changed anyway. */
11313 if (windows_or_buffers_changed
11314 /* This used to test w->update_mode_line, but we believe
11315 there is no need to recompute the menu in that case. */
11316 || update_mode_lines
11317 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11318 < BUF_MODIFF (XBUFFER (w->buffer)))
11319 != w->last_had_star)
11320 || ((!NILP (Vtransient_mark_mode)
11321 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11322 != !NILP (w->region_showing)))
11323 {
11324 struct buffer *prev = current_buffer;
11325 ptrdiff_t count = SPECPDL_INDEX ();
11326
11327 specbind (Qinhibit_menubar_update, Qt);
11328
11329 set_buffer_internal_1 (XBUFFER (w->buffer));
11330 if (save_match_data)
11331 record_unwind_save_match_data ();
11332 if (NILP (Voverriding_local_map_menu_flag))
11333 {
11334 specbind (Qoverriding_terminal_local_map, Qnil);
11335 specbind (Qoverriding_local_map, Qnil);
11336 }
11337
11338 if (!hooks_run)
11339 {
11340 /* Run the Lucid hook. */
11341 safe_run_hooks (Qactivate_menubar_hook);
11342
11343 /* If it has changed current-menubar from previous value,
11344 really recompute the menu-bar from the value. */
11345 if (! NILP (Vlucid_menu_bar_dirty_flag))
11346 call0 (Qrecompute_lucid_menubar);
11347
11348 safe_run_hooks (Qmenu_bar_update_hook);
11349
11350 hooks_run = 1;
11351 }
11352
11353 XSETFRAME (Vmenu_updating_frame, f);
11354 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11355
11356 /* Redisplay the menu bar in case we changed it. */
11357 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11358 || defined (HAVE_NS) || defined (USE_GTK)
11359 if (FRAME_WINDOW_P (f))
11360 {
11361 #if defined (HAVE_NS)
11362 /* All frames on Mac OS share the same menubar. So only
11363 the selected frame should be allowed to set it. */
11364 if (f == SELECTED_FRAME ())
11365 #endif
11366 set_frame_menubar (f, 0, 0);
11367 }
11368 else
11369 /* On a terminal screen, the menu bar is an ordinary screen
11370 line, and this makes it get updated. */
11371 w->update_mode_line = 1;
11372 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11373 /* In the non-toolkit version, the menu bar is an ordinary screen
11374 line, and this makes it get updated. */
11375 w->update_mode_line = 1;
11376 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11377
11378 unbind_to (count, Qnil);
11379 set_buffer_internal_1 (prev);
11380 }
11381 }
11382
11383 return hooks_run;
11384 }
11385
11386
11387 \f
11388 /***********************************************************************
11389 Output Cursor
11390 ***********************************************************************/
11391
11392 #ifdef HAVE_WINDOW_SYSTEM
11393
11394 /* EXPORT:
11395 Nominal cursor position -- where to draw output.
11396 HPOS and VPOS are window relative glyph matrix coordinates.
11397 X and Y are window relative pixel coordinates. */
11398
11399 struct cursor_pos output_cursor;
11400
11401
11402 /* EXPORT:
11403 Set the global variable output_cursor to CURSOR. All cursor
11404 positions are relative to updated_window. */
11405
11406 void
11407 set_output_cursor (struct cursor_pos *cursor)
11408 {
11409 output_cursor.hpos = cursor->hpos;
11410 output_cursor.vpos = cursor->vpos;
11411 output_cursor.x = cursor->x;
11412 output_cursor.y = cursor->y;
11413 }
11414
11415
11416 /* EXPORT for RIF:
11417 Set a nominal cursor position.
11418
11419 HPOS and VPOS are column/row positions in a window glyph matrix. X
11420 and Y are window text area relative pixel positions.
11421
11422 If this is done during an update, updated_window will contain the
11423 window that is being updated and the position is the future output
11424 cursor position for that window. If updated_window is null, use
11425 selected_window and display the cursor at the given position. */
11426
11427 void
11428 x_cursor_to (int vpos, int hpos, int y, int x)
11429 {
11430 struct window *w;
11431
11432 /* If updated_window is not set, work on selected_window. */
11433 if (updated_window)
11434 w = updated_window;
11435 else
11436 w = XWINDOW (selected_window);
11437
11438 /* Set the output cursor. */
11439 output_cursor.hpos = hpos;
11440 output_cursor.vpos = vpos;
11441 output_cursor.x = x;
11442 output_cursor.y = y;
11443
11444 /* If not called as part of an update, really display the cursor.
11445 This will also set the cursor position of W. */
11446 if (updated_window == NULL)
11447 {
11448 block_input ();
11449 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11450 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11451 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11452 unblock_input ();
11453 }
11454 }
11455
11456 #endif /* HAVE_WINDOW_SYSTEM */
11457
11458 \f
11459 /***********************************************************************
11460 Tool-bars
11461 ***********************************************************************/
11462
11463 #ifdef HAVE_WINDOW_SYSTEM
11464
11465 /* Where the mouse was last time we reported a mouse event. */
11466
11467 FRAME_PTR last_mouse_frame;
11468
11469 /* Tool-bar item index of the item on which a mouse button was pressed
11470 or -1. */
11471
11472 int last_tool_bar_item;
11473
11474
11475 static Lisp_Object
11476 update_tool_bar_unwind (Lisp_Object frame)
11477 {
11478 selected_frame = frame;
11479 return Qnil;
11480 }
11481
11482 /* Update the tool-bar item list for frame F. This has to be done
11483 before we start to fill in any display lines. Called from
11484 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11485 and restore it here. */
11486
11487 static void
11488 update_tool_bar (struct frame *f, int save_match_data)
11489 {
11490 #if defined (USE_GTK) || defined (HAVE_NS)
11491 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11492 #else
11493 int do_update = WINDOWP (f->tool_bar_window)
11494 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11495 #endif
11496
11497 if (do_update)
11498 {
11499 Lisp_Object window;
11500 struct window *w;
11501
11502 window = FRAME_SELECTED_WINDOW (f);
11503 w = XWINDOW (window);
11504
11505 /* If the user has switched buffers or windows, we need to
11506 recompute to reflect the new bindings. But we'll
11507 recompute when update_mode_lines is set too; that means
11508 that people can use force-mode-line-update to request
11509 that the menu bar be recomputed. The adverse effect on
11510 the rest of the redisplay algorithm is about the same as
11511 windows_or_buffers_changed anyway. */
11512 if (windows_or_buffers_changed
11513 || w->update_mode_line
11514 || update_mode_lines
11515 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11516 < BUF_MODIFF (XBUFFER (w->buffer)))
11517 != w->last_had_star)
11518 || ((!NILP (Vtransient_mark_mode)
11519 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11520 != !NILP (w->region_showing)))
11521 {
11522 struct buffer *prev = current_buffer;
11523 ptrdiff_t count = SPECPDL_INDEX ();
11524 Lisp_Object frame, new_tool_bar;
11525 int new_n_tool_bar;
11526 struct gcpro gcpro1;
11527
11528 /* Set current_buffer to the buffer of the selected
11529 window of the frame, so that we get the right local
11530 keymaps. */
11531 set_buffer_internal_1 (XBUFFER (w->buffer));
11532
11533 /* Save match data, if we must. */
11534 if (save_match_data)
11535 record_unwind_save_match_data ();
11536
11537 /* Make sure that we don't accidentally use bogus keymaps. */
11538 if (NILP (Voverriding_local_map_menu_flag))
11539 {
11540 specbind (Qoverriding_terminal_local_map, Qnil);
11541 specbind (Qoverriding_local_map, Qnil);
11542 }
11543
11544 GCPRO1 (new_tool_bar);
11545
11546 /* We must temporarily set the selected frame to this frame
11547 before calling tool_bar_items, because the calculation of
11548 the tool-bar keymap uses the selected frame (see
11549 `tool-bar-make-keymap' in tool-bar.el). */
11550 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11551 XSETFRAME (frame, f);
11552 selected_frame = frame;
11553
11554 /* Build desired tool-bar items from keymaps. */
11555 new_tool_bar
11556 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11557 &new_n_tool_bar);
11558
11559 /* Redisplay the tool-bar if we changed it. */
11560 if (new_n_tool_bar != f->n_tool_bar_items
11561 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11562 {
11563 /* Redisplay that happens asynchronously due to an expose event
11564 may access f->tool_bar_items. Make sure we update both
11565 variables within BLOCK_INPUT so no such event interrupts. */
11566 block_input ();
11567 fset_tool_bar_items (f, new_tool_bar);
11568 f->n_tool_bar_items = new_n_tool_bar;
11569 w->update_mode_line = 1;
11570 unblock_input ();
11571 }
11572
11573 UNGCPRO;
11574
11575 unbind_to (count, Qnil);
11576 set_buffer_internal_1 (prev);
11577 }
11578 }
11579 }
11580
11581
11582 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11583 F's desired tool-bar contents. F->tool_bar_items must have
11584 been set up previously by calling prepare_menu_bars. */
11585
11586 static void
11587 build_desired_tool_bar_string (struct frame *f)
11588 {
11589 int i, size, size_needed;
11590 struct gcpro gcpro1, gcpro2, gcpro3;
11591 Lisp_Object image, plist, props;
11592
11593 image = plist = props = Qnil;
11594 GCPRO3 (image, plist, props);
11595
11596 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11597 Otherwise, make a new string. */
11598
11599 /* The size of the string we might be able to reuse. */
11600 size = (STRINGP (f->desired_tool_bar_string)
11601 ? SCHARS (f->desired_tool_bar_string)
11602 : 0);
11603
11604 /* We need one space in the string for each image. */
11605 size_needed = f->n_tool_bar_items;
11606
11607 /* Reuse f->desired_tool_bar_string, if possible. */
11608 if (size < size_needed || NILP (f->desired_tool_bar_string))
11609 fset_desired_tool_bar_string
11610 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11611 else
11612 {
11613 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11614 Fremove_text_properties (make_number (0), make_number (size),
11615 props, f->desired_tool_bar_string);
11616 }
11617
11618 /* Put a `display' property on the string for the images to display,
11619 put a `menu_item' property on tool-bar items with a value that
11620 is the index of the item in F's tool-bar item vector. */
11621 for (i = 0; i < f->n_tool_bar_items; ++i)
11622 {
11623 #define PROP(IDX) \
11624 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11625
11626 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11627 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11628 int hmargin, vmargin, relief, idx, end;
11629
11630 /* If image is a vector, choose the image according to the
11631 button state. */
11632 image = PROP (TOOL_BAR_ITEM_IMAGES);
11633 if (VECTORP (image))
11634 {
11635 if (enabled_p)
11636 idx = (selected_p
11637 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11638 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11639 else
11640 idx = (selected_p
11641 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11642 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11643
11644 eassert (ASIZE (image) >= idx);
11645 image = AREF (image, idx);
11646 }
11647 else
11648 idx = -1;
11649
11650 /* Ignore invalid image specifications. */
11651 if (!valid_image_p (image))
11652 continue;
11653
11654 /* Display the tool-bar button pressed, or depressed. */
11655 plist = Fcopy_sequence (XCDR (image));
11656
11657 /* Compute margin and relief to draw. */
11658 relief = (tool_bar_button_relief >= 0
11659 ? tool_bar_button_relief
11660 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11661 hmargin = vmargin = relief;
11662
11663 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11664 INT_MAX - max (hmargin, vmargin)))
11665 {
11666 hmargin += XFASTINT (Vtool_bar_button_margin);
11667 vmargin += XFASTINT (Vtool_bar_button_margin);
11668 }
11669 else if (CONSP (Vtool_bar_button_margin))
11670 {
11671 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11672 INT_MAX - hmargin))
11673 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11674
11675 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11676 INT_MAX - vmargin))
11677 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11678 }
11679
11680 if (auto_raise_tool_bar_buttons_p)
11681 {
11682 /* Add a `:relief' property to the image spec if the item is
11683 selected. */
11684 if (selected_p)
11685 {
11686 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11687 hmargin -= relief;
11688 vmargin -= relief;
11689 }
11690 }
11691 else
11692 {
11693 /* If image is selected, display it pressed, i.e. with a
11694 negative relief. If it's not selected, display it with a
11695 raised relief. */
11696 plist = Fplist_put (plist, QCrelief,
11697 (selected_p
11698 ? make_number (-relief)
11699 : make_number (relief)));
11700 hmargin -= relief;
11701 vmargin -= relief;
11702 }
11703
11704 /* Put a margin around the image. */
11705 if (hmargin || vmargin)
11706 {
11707 if (hmargin == vmargin)
11708 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11709 else
11710 plist = Fplist_put (plist, QCmargin,
11711 Fcons (make_number (hmargin),
11712 make_number (vmargin)));
11713 }
11714
11715 /* If button is not enabled, and we don't have special images
11716 for the disabled state, make the image appear disabled by
11717 applying an appropriate algorithm to it. */
11718 if (!enabled_p && idx < 0)
11719 plist = Fplist_put (plist, QCconversion, Qdisabled);
11720
11721 /* Put a `display' text property on the string for the image to
11722 display. Put a `menu-item' property on the string that gives
11723 the start of this item's properties in the tool-bar items
11724 vector. */
11725 image = Fcons (Qimage, plist);
11726 props = list4 (Qdisplay, image,
11727 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11728
11729 /* Let the last image hide all remaining spaces in the tool bar
11730 string. The string can be longer than needed when we reuse a
11731 previous string. */
11732 if (i + 1 == f->n_tool_bar_items)
11733 end = SCHARS (f->desired_tool_bar_string);
11734 else
11735 end = i + 1;
11736 Fadd_text_properties (make_number (i), make_number (end),
11737 props, f->desired_tool_bar_string);
11738 #undef PROP
11739 }
11740
11741 UNGCPRO;
11742 }
11743
11744
11745 /* Display one line of the tool-bar of frame IT->f.
11746
11747 HEIGHT specifies the desired height of the tool-bar line.
11748 If the actual height of the glyph row is less than HEIGHT, the
11749 row's height is increased to HEIGHT, and the icons are centered
11750 vertically in the new height.
11751
11752 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11753 count a final empty row in case the tool-bar width exactly matches
11754 the window width.
11755 */
11756
11757 static void
11758 display_tool_bar_line (struct it *it, int height)
11759 {
11760 struct glyph_row *row = it->glyph_row;
11761 int max_x = it->last_visible_x;
11762 struct glyph *last;
11763
11764 prepare_desired_row (row);
11765 row->y = it->current_y;
11766
11767 /* Note that this isn't made use of if the face hasn't a box,
11768 so there's no need to check the face here. */
11769 it->start_of_box_run_p = 1;
11770
11771 while (it->current_x < max_x)
11772 {
11773 int x, n_glyphs_before, i, nglyphs;
11774 struct it it_before;
11775
11776 /* Get the next display element. */
11777 if (!get_next_display_element (it))
11778 {
11779 /* Don't count empty row if we are counting needed tool-bar lines. */
11780 if (height < 0 && !it->hpos)
11781 return;
11782 break;
11783 }
11784
11785 /* Produce glyphs. */
11786 n_glyphs_before = row->used[TEXT_AREA];
11787 it_before = *it;
11788
11789 PRODUCE_GLYPHS (it);
11790
11791 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11792 i = 0;
11793 x = it_before.current_x;
11794 while (i < nglyphs)
11795 {
11796 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11797
11798 if (x + glyph->pixel_width > max_x)
11799 {
11800 /* Glyph doesn't fit on line. Backtrack. */
11801 row->used[TEXT_AREA] = n_glyphs_before;
11802 *it = it_before;
11803 /* If this is the only glyph on this line, it will never fit on the
11804 tool-bar, so skip it. But ensure there is at least one glyph,
11805 so we don't accidentally disable the tool-bar. */
11806 if (n_glyphs_before == 0
11807 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11808 break;
11809 goto out;
11810 }
11811
11812 ++it->hpos;
11813 x += glyph->pixel_width;
11814 ++i;
11815 }
11816
11817 /* Stop at line end. */
11818 if (ITERATOR_AT_END_OF_LINE_P (it))
11819 break;
11820
11821 set_iterator_to_next (it, 1);
11822 }
11823
11824 out:;
11825
11826 row->displays_text_p = row->used[TEXT_AREA] != 0;
11827
11828 /* Use default face for the border below the tool bar.
11829
11830 FIXME: When auto-resize-tool-bars is grow-only, there is
11831 no additional border below the possibly empty tool-bar lines.
11832 So to make the extra empty lines look "normal", we have to
11833 use the tool-bar face for the border too. */
11834 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11835 it->face_id = DEFAULT_FACE_ID;
11836
11837 extend_face_to_end_of_line (it);
11838 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11839 last->right_box_line_p = 1;
11840 if (last == row->glyphs[TEXT_AREA])
11841 last->left_box_line_p = 1;
11842
11843 /* Make line the desired height and center it vertically. */
11844 if ((height -= it->max_ascent + it->max_descent) > 0)
11845 {
11846 /* Don't add more than one line height. */
11847 height %= FRAME_LINE_HEIGHT (it->f);
11848 it->max_ascent += height / 2;
11849 it->max_descent += (height + 1) / 2;
11850 }
11851
11852 compute_line_metrics (it);
11853
11854 /* If line is empty, make it occupy the rest of the tool-bar. */
11855 if (!row->displays_text_p)
11856 {
11857 row->height = row->phys_height = it->last_visible_y - row->y;
11858 row->visible_height = row->height;
11859 row->ascent = row->phys_ascent = 0;
11860 row->extra_line_spacing = 0;
11861 }
11862
11863 row->full_width_p = 1;
11864 row->continued_p = 0;
11865 row->truncated_on_left_p = 0;
11866 row->truncated_on_right_p = 0;
11867
11868 it->current_x = it->hpos = 0;
11869 it->current_y += row->height;
11870 ++it->vpos;
11871 ++it->glyph_row;
11872 }
11873
11874
11875 /* Max tool-bar height. */
11876
11877 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11878 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11879
11880 /* Value is the number of screen lines needed to make all tool-bar
11881 items of frame F visible. The number of actual rows needed is
11882 returned in *N_ROWS if non-NULL. */
11883
11884 static int
11885 tool_bar_lines_needed (struct frame *f, int *n_rows)
11886 {
11887 struct window *w = XWINDOW (f->tool_bar_window);
11888 struct it it;
11889 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11890 the desired matrix, so use (unused) mode-line row as temporary row to
11891 avoid destroying the first tool-bar row. */
11892 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11893
11894 /* Initialize an iterator for iteration over
11895 F->desired_tool_bar_string in the tool-bar window of frame F. */
11896 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11897 it.first_visible_x = 0;
11898 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11899 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11900 it.paragraph_embedding = L2R;
11901
11902 while (!ITERATOR_AT_END_P (&it))
11903 {
11904 clear_glyph_row (temp_row);
11905 it.glyph_row = temp_row;
11906 display_tool_bar_line (&it, -1);
11907 }
11908 clear_glyph_row (temp_row);
11909
11910 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11911 if (n_rows)
11912 *n_rows = it.vpos > 0 ? it.vpos : -1;
11913
11914 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11915 }
11916
11917
11918 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11919 0, 1, 0,
11920 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11921 (Lisp_Object frame)
11922 {
11923 struct frame *f;
11924 struct window *w;
11925 int nlines = 0;
11926
11927 if (NILP (frame))
11928 frame = selected_frame;
11929 else
11930 CHECK_FRAME (frame);
11931 f = XFRAME (frame);
11932
11933 if (WINDOWP (f->tool_bar_window)
11934 && (w = XWINDOW (f->tool_bar_window),
11935 WINDOW_TOTAL_LINES (w) > 0))
11936 {
11937 update_tool_bar (f, 1);
11938 if (f->n_tool_bar_items)
11939 {
11940 build_desired_tool_bar_string (f);
11941 nlines = tool_bar_lines_needed (f, NULL);
11942 }
11943 }
11944
11945 return make_number (nlines);
11946 }
11947
11948
11949 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11950 height should be changed. */
11951
11952 static int
11953 redisplay_tool_bar (struct frame *f)
11954 {
11955 struct window *w;
11956 struct it it;
11957 struct glyph_row *row;
11958
11959 #if defined (USE_GTK) || defined (HAVE_NS)
11960 if (FRAME_EXTERNAL_TOOL_BAR (f))
11961 update_frame_tool_bar (f);
11962 return 0;
11963 #endif
11964
11965 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11966 do anything. This means you must start with tool-bar-lines
11967 non-zero to get the auto-sizing effect. Or in other words, you
11968 can turn off tool-bars by specifying tool-bar-lines zero. */
11969 if (!WINDOWP (f->tool_bar_window)
11970 || (w = XWINDOW (f->tool_bar_window),
11971 WINDOW_TOTAL_LINES (w) == 0))
11972 return 0;
11973
11974 /* Set up an iterator for the tool-bar window. */
11975 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11976 it.first_visible_x = 0;
11977 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11978 row = it.glyph_row;
11979
11980 /* Build a string that represents the contents of the tool-bar. */
11981 build_desired_tool_bar_string (f);
11982 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11983 /* FIXME: This should be controlled by a user option. But it
11984 doesn't make sense to have an R2L tool bar if the menu bar cannot
11985 be drawn also R2L, and making the menu bar R2L is tricky due
11986 toolkit-specific code that implements it. If an R2L tool bar is
11987 ever supported, display_tool_bar_line should also be augmented to
11988 call unproduce_glyphs like display_line and display_string
11989 do. */
11990 it.paragraph_embedding = L2R;
11991
11992 if (f->n_tool_bar_rows == 0)
11993 {
11994 int nlines;
11995
11996 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11997 nlines != WINDOW_TOTAL_LINES (w)))
11998 {
11999 Lisp_Object frame;
12000 int old_height = WINDOW_TOTAL_LINES (w);
12001
12002 XSETFRAME (frame, f);
12003 Fmodify_frame_parameters (frame,
12004 Fcons (Fcons (Qtool_bar_lines,
12005 make_number (nlines)),
12006 Qnil));
12007 if (WINDOW_TOTAL_LINES (w) != old_height)
12008 {
12009 clear_glyph_matrix (w->desired_matrix);
12010 fonts_changed_p = 1;
12011 return 1;
12012 }
12013 }
12014 }
12015
12016 /* Display as many lines as needed to display all tool-bar items. */
12017
12018 if (f->n_tool_bar_rows > 0)
12019 {
12020 int border, rows, height, extra;
12021
12022 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12023 border = XINT (Vtool_bar_border);
12024 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12025 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12026 else if (EQ (Vtool_bar_border, Qborder_width))
12027 border = f->border_width;
12028 else
12029 border = 0;
12030 if (border < 0)
12031 border = 0;
12032
12033 rows = f->n_tool_bar_rows;
12034 height = max (1, (it.last_visible_y - border) / rows);
12035 extra = it.last_visible_y - border - height * rows;
12036
12037 while (it.current_y < it.last_visible_y)
12038 {
12039 int h = 0;
12040 if (extra > 0 && rows-- > 0)
12041 {
12042 h = (extra + rows - 1) / rows;
12043 extra -= h;
12044 }
12045 display_tool_bar_line (&it, height + h);
12046 }
12047 }
12048 else
12049 {
12050 while (it.current_y < it.last_visible_y)
12051 display_tool_bar_line (&it, 0);
12052 }
12053
12054 /* It doesn't make much sense to try scrolling in the tool-bar
12055 window, so don't do it. */
12056 w->desired_matrix->no_scrolling_p = 1;
12057 w->must_be_updated_p = 1;
12058
12059 if (!NILP (Vauto_resize_tool_bars))
12060 {
12061 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12062 int change_height_p = 0;
12063
12064 /* If we couldn't display everything, change the tool-bar's
12065 height if there is room for more. */
12066 if (IT_STRING_CHARPOS (it) < it.end_charpos
12067 && it.current_y < max_tool_bar_height)
12068 change_height_p = 1;
12069
12070 row = it.glyph_row - 1;
12071
12072 /* If there are blank lines at the end, except for a partially
12073 visible blank line at the end that is smaller than
12074 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12075 if (!row->displays_text_p
12076 && row->height >= FRAME_LINE_HEIGHT (f))
12077 change_height_p = 1;
12078
12079 /* If row displays tool-bar items, but is partially visible,
12080 change the tool-bar's height. */
12081 if (row->displays_text_p
12082 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12083 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12084 change_height_p = 1;
12085
12086 /* Resize windows as needed by changing the `tool-bar-lines'
12087 frame parameter. */
12088 if (change_height_p)
12089 {
12090 Lisp_Object frame;
12091 int old_height = WINDOW_TOTAL_LINES (w);
12092 int nrows;
12093 int nlines = tool_bar_lines_needed (f, &nrows);
12094
12095 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12096 && !f->minimize_tool_bar_window_p)
12097 ? (nlines > old_height)
12098 : (nlines != old_height));
12099 f->minimize_tool_bar_window_p = 0;
12100
12101 if (change_height_p)
12102 {
12103 XSETFRAME (frame, f);
12104 Fmodify_frame_parameters (frame,
12105 Fcons (Fcons (Qtool_bar_lines,
12106 make_number (nlines)),
12107 Qnil));
12108 if (WINDOW_TOTAL_LINES (w) != old_height)
12109 {
12110 clear_glyph_matrix (w->desired_matrix);
12111 f->n_tool_bar_rows = nrows;
12112 fonts_changed_p = 1;
12113 return 1;
12114 }
12115 }
12116 }
12117 }
12118
12119 f->minimize_tool_bar_window_p = 0;
12120 return 0;
12121 }
12122
12123
12124 /* Get information about the tool-bar item which is displayed in GLYPH
12125 on frame F. Return in *PROP_IDX the index where tool-bar item
12126 properties start in F->tool_bar_items. Value is zero if
12127 GLYPH doesn't display a tool-bar item. */
12128
12129 static int
12130 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12131 {
12132 Lisp_Object prop;
12133 int success_p;
12134 int charpos;
12135
12136 /* This function can be called asynchronously, which means we must
12137 exclude any possibility that Fget_text_property signals an
12138 error. */
12139 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12140 charpos = max (0, charpos);
12141
12142 /* Get the text property `menu-item' at pos. The value of that
12143 property is the start index of this item's properties in
12144 F->tool_bar_items. */
12145 prop = Fget_text_property (make_number (charpos),
12146 Qmenu_item, f->current_tool_bar_string);
12147 if (INTEGERP (prop))
12148 {
12149 *prop_idx = XINT (prop);
12150 success_p = 1;
12151 }
12152 else
12153 success_p = 0;
12154
12155 return success_p;
12156 }
12157
12158 \f
12159 /* Get information about the tool-bar item at position X/Y on frame F.
12160 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12161 the current matrix of the tool-bar window of F, or NULL if not
12162 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12163 item in F->tool_bar_items. Value is
12164
12165 -1 if X/Y is not on a tool-bar item
12166 0 if X/Y is on the same item that was highlighted before.
12167 1 otherwise. */
12168
12169 static int
12170 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12171 int *hpos, int *vpos, int *prop_idx)
12172 {
12173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12174 struct window *w = XWINDOW (f->tool_bar_window);
12175 int area;
12176
12177 /* Find the glyph under X/Y. */
12178 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12179 if (*glyph == NULL)
12180 return -1;
12181
12182 /* Get the start of this tool-bar item's properties in
12183 f->tool_bar_items. */
12184 if (!tool_bar_item_info (f, *glyph, prop_idx))
12185 return -1;
12186
12187 /* Is mouse on the highlighted item? */
12188 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12189 && *vpos >= hlinfo->mouse_face_beg_row
12190 && *vpos <= hlinfo->mouse_face_end_row
12191 && (*vpos > hlinfo->mouse_face_beg_row
12192 || *hpos >= hlinfo->mouse_face_beg_col)
12193 && (*vpos < hlinfo->mouse_face_end_row
12194 || *hpos < hlinfo->mouse_face_end_col
12195 || hlinfo->mouse_face_past_end))
12196 return 0;
12197
12198 return 1;
12199 }
12200
12201
12202 /* EXPORT:
12203 Handle mouse button event on the tool-bar of frame F, at
12204 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12205 0 for button release. MODIFIERS is event modifiers for button
12206 release. */
12207
12208 void
12209 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12210 int modifiers)
12211 {
12212 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12213 struct window *w = XWINDOW (f->tool_bar_window);
12214 int hpos, vpos, prop_idx;
12215 struct glyph *glyph;
12216 Lisp_Object enabled_p;
12217
12218 /* If not on the highlighted tool-bar item, return. */
12219 frame_to_window_pixel_xy (w, &x, &y);
12220 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12221 return;
12222
12223 /* If item is disabled, do nothing. */
12224 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12225 if (NILP (enabled_p))
12226 return;
12227
12228 if (down_p)
12229 {
12230 /* Show item in pressed state. */
12231 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12232 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12233 last_tool_bar_item = prop_idx;
12234 }
12235 else
12236 {
12237 Lisp_Object key, frame;
12238 struct input_event event;
12239 EVENT_INIT (event);
12240
12241 /* Show item in released state. */
12242 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12243 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12244
12245 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12246
12247 XSETFRAME (frame, f);
12248 event.kind = TOOL_BAR_EVENT;
12249 event.frame_or_window = frame;
12250 event.arg = frame;
12251 kbd_buffer_store_event (&event);
12252
12253 event.kind = TOOL_BAR_EVENT;
12254 event.frame_or_window = frame;
12255 event.arg = key;
12256 event.modifiers = modifiers;
12257 kbd_buffer_store_event (&event);
12258 last_tool_bar_item = -1;
12259 }
12260 }
12261
12262
12263 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12264 tool-bar window-relative coordinates X/Y. Called from
12265 note_mouse_highlight. */
12266
12267 static void
12268 note_tool_bar_highlight (struct frame *f, int x, int y)
12269 {
12270 Lisp_Object window = f->tool_bar_window;
12271 struct window *w = XWINDOW (window);
12272 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12273 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12274 int hpos, vpos;
12275 struct glyph *glyph;
12276 struct glyph_row *row;
12277 int i;
12278 Lisp_Object enabled_p;
12279 int prop_idx;
12280 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12281 int mouse_down_p, rc;
12282
12283 /* Function note_mouse_highlight is called with negative X/Y
12284 values when mouse moves outside of the frame. */
12285 if (x <= 0 || y <= 0)
12286 {
12287 clear_mouse_face (hlinfo);
12288 return;
12289 }
12290
12291 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12292 if (rc < 0)
12293 {
12294 /* Not on tool-bar item. */
12295 clear_mouse_face (hlinfo);
12296 return;
12297 }
12298 else if (rc == 0)
12299 /* On same tool-bar item as before. */
12300 goto set_help_echo;
12301
12302 clear_mouse_face (hlinfo);
12303
12304 /* Mouse is down, but on different tool-bar item? */
12305 mouse_down_p = (dpyinfo->grabbed
12306 && f == last_mouse_frame
12307 && FRAME_LIVE_P (f));
12308 if (mouse_down_p
12309 && last_tool_bar_item != prop_idx)
12310 return;
12311
12312 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12313 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12314
12315 /* If tool-bar item is not enabled, don't highlight it. */
12316 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12317 if (!NILP (enabled_p))
12318 {
12319 /* Compute the x-position of the glyph. In front and past the
12320 image is a space. We include this in the highlighted area. */
12321 row = MATRIX_ROW (w->current_matrix, vpos);
12322 for (i = x = 0; i < hpos; ++i)
12323 x += row->glyphs[TEXT_AREA][i].pixel_width;
12324
12325 /* Record this as the current active region. */
12326 hlinfo->mouse_face_beg_col = hpos;
12327 hlinfo->mouse_face_beg_row = vpos;
12328 hlinfo->mouse_face_beg_x = x;
12329 hlinfo->mouse_face_beg_y = row->y;
12330 hlinfo->mouse_face_past_end = 0;
12331
12332 hlinfo->mouse_face_end_col = hpos + 1;
12333 hlinfo->mouse_face_end_row = vpos;
12334 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12335 hlinfo->mouse_face_end_y = row->y;
12336 hlinfo->mouse_face_window = window;
12337 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12338
12339 /* Display it as active. */
12340 show_mouse_face (hlinfo, draw);
12341 hlinfo->mouse_face_image_state = draw;
12342 }
12343
12344 set_help_echo:
12345
12346 /* Set help_echo_string to a help string to display for this tool-bar item.
12347 XTread_socket does the rest. */
12348 help_echo_object = help_echo_window = Qnil;
12349 help_echo_pos = -1;
12350 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12351 if (NILP (help_echo_string))
12352 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12353 }
12354
12355 #endif /* HAVE_WINDOW_SYSTEM */
12356
12357
12358 \f
12359 /************************************************************************
12360 Horizontal scrolling
12361 ************************************************************************/
12362
12363 static int hscroll_window_tree (Lisp_Object);
12364 static int hscroll_windows (Lisp_Object);
12365
12366 /* For all leaf windows in the window tree rooted at WINDOW, set their
12367 hscroll value so that PT is (i) visible in the window, and (ii) so
12368 that it is not within a certain margin at the window's left and
12369 right border. Value is non-zero if any window's hscroll has been
12370 changed. */
12371
12372 static int
12373 hscroll_window_tree (Lisp_Object window)
12374 {
12375 int hscrolled_p = 0;
12376 int hscroll_relative_p = FLOATP (Vhscroll_step);
12377 int hscroll_step_abs = 0;
12378 double hscroll_step_rel = 0;
12379
12380 if (hscroll_relative_p)
12381 {
12382 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12383 if (hscroll_step_rel < 0)
12384 {
12385 hscroll_relative_p = 0;
12386 hscroll_step_abs = 0;
12387 }
12388 }
12389 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12390 {
12391 hscroll_step_abs = XINT (Vhscroll_step);
12392 if (hscroll_step_abs < 0)
12393 hscroll_step_abs = 0;
12394 }
12395 else
12396 hscroll_step_abs = 0;
12397
12398 while (WINDOWP (window))
12399 {
12400 struct window *w = XWINDOW (window);
12401
12402 if (WINDOWP (w->hchild))
12403 hscrolled_p |= hscroll_window_tree (w->hchild);
12404 else if (WINDOWP (w->vchild))
12405 hscrolled_p |= hscroll_window_tree (w->vchild);
12406 else if (w->cursor.vpos >= 0)
12407 {
12408 int h_margin;
12409 int text_area_width;
12410 struct glyph_row *current_cursor_row
12411 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12412 struct glyph_row *desired_cursor_row
12413 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12414 struct glyph_row *cursor_row
12415 = (desired_cursor_row->enabled_p
12416 ? desired_cursor_row
12417 : current_cursor_row);
12418 int row_r2l_p = cursor_row->reversed_p;
12419
12420 text_area_width = window_box_width (w, TEXT_AREA);
12421
12422 /* Scroll when cursor is inside this scroll margin. */
12423 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12424
12425 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12426 /* For left-to-right rows, hscroll when cursor is either
12427 (i) inside the right hscroll margin, or (ii) if it is
12428 inside the left margin and the window is already
12429 hscrolled. */
12430 && ((!row_r2l_p
12431 && ((w->hscroll
12432 && w->cursor.x <= h_margin)
12433 || (cursor_row->enabled_p
12434 && cursor_row->truncated_on_right_p
12435 && (w->cursor.x >= text_area_width - h_margin))))
12436 /* For right-to-left rows, the logic is similar,
12437 except that rules for scrolling to left and right
12438 are reversed. E.g., if cursor.x <= h_margin, we
12439 need to hscroll "to the right" unconditionally,
12440 and that will scroll the screen to the left so as
12441 to reveal the next portion of the row. */
12442 || (row_r2l_p
12443 && ((cursor_row->enabled_p
12444 /* FIXME: It is confusing to set the
12445 truncated_on_right_p flag when R2L rows
12446 are actually truncated on the left. */
12447 && cursor_row->truncated_on_right_p
12448 && w->cursor.x <= h_margin)
12449 || (w->hscroll
12450 && (w->cursor.x >= text_area_width - h_margin))))))
12451 {
12452 struct it it;
12453 ptrdiff_t hscroll;
12454 struct buffer *saved_current_buffer;
12455 ptrdiff_t pt;
12456 int wanted_x;
12457
12458 /* Find point in a display of infinite width. */
12459 saved_current_buffer = current_buffer;
12460 current_buffer = XBUFFER (w->buffer);
12461
12462 if (w == XWINDOW (selected_window))
12463 pt = PT;
12464 else
12465 {
12466 pt = marker_position (w->pointm);
12467 pt = max (BEGV, pt);
12468 pt = min (ZV, pt);
12469 }
12470
12471 /* Move iterator to pt starting at cursor_row->start in
12472 a line with infinite width. */
12473 init_to_row_start (&it, w, cursor_row);
12474 it.last_visible_x = INFINITY;
12475 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12476 current_buffer = saved_current_buffer;
12477
12478 /* Position cursor in window. */
12479 if (!hscroll_relative_p && hscroll_step_abs == 0)
12480 hscroll = max (0, (it.current_x
12481 - (ITERATOR_AT_END_OF_LINE_P (&it)
12482 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12483 : (text_area_width / 2))))
12484 / FRAME_COLUMN_WIDTH (it.f);
12485 else if ((!row_r2l_p
12486 && w->cursor.x >= text_area_width - h_margin)
12487 || (row_r2l_p && w->cursor.x <= h_margin))
12488 {
12489 if (hscroll_relative_p)
12490 wanted_x = text_area_width * (1 - hscroll_step_rel)
12491 - h_margin;
12492 else
12493 wanted_x = text_area_width
12494 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12495 - h_margin;
12496 hscroll
12497 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12498 }
12499 else
12500 {
12501 if (hscroll_relative_p)
12502 wanted_x = text_area_width * hscroll_step_rel
12503 + h_margin;
12504 else
12505 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12506 + h_margin;
12507 hscroll
12508 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12509 }
12510 hscroll = max (hscroll, w->min_hscroll);
12511
12512 /* Don't prevent redisplay optimizations if hscroll
12513 hasn't changed, as it will unnecessarily slow down
12514 redisplay. */
12515 if (w->hscroll != hscroll)
12516 {
12517 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12518 w->hscroll = hscroll;
12519 hscrolled_p = 1;
12520 }
12521 }
12522 }
12523
12524 window = w->next;
12525 }
12526
12527 /* Value is non-zero if hscroll of any leaf window has been changed. */
12528 return hscrolled_p;
12529 }
12530
12531
12532 /* Set hscroll so that cursor is visible and not inside horizontal
12533 scroll margins for all windows in the tree rooted at WINDOW. See
12534 also hscroll_window_tree above. Value is non-zero if any window's
12535 hscroll has been changed. If it has, desired matrices on the frame
12536 of WINDOW are cleared. */
12537
12538 static int
12539 hscroll_windows (Lisp_Object window)
12540 {
12541 int hscrolled_p = hscroll_window_tree (window);
12542 if (hscrolled_p)
12543 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12544 return hscrolled_p;
12545 }
12546
12547
12548 \f
12549 /************************************************************************
12550 Redisplay
12551 ************************************************************************/
12552
12553 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12554 to a non-zero value. This is sometimes handy to have in a debugger
12555 session. */
12556
12557 #ifdef GLYPH_DEBUG
12558
12559 /* First and last unchanged row for try_window_id. */
12560
12561 static int debug_first_unchanged_at_end_vpos;
12562 static int debug_last_unchanged_at_beg_vpos;
12563
12564 /* Delta vpos and y. */
12565
12566 static int debug_dvpos, debug_dy;
12567
12568 /* Delta in characters and bytes for try_window_id. */
12569
12570 static ptrdiff_t debug_delta, debug_delta_bytes;
12571
12572 /* Values of window_end_pos and window_end_vpos at the end of
12573 try_window_id. */
12574
12575 static ptrdiff_t debug_end_vpos;
12576
12577 /* Append a string to W->desired_matrix->method. FMT is a printf
12578 format string. If trace_redisplay_p is non-zero also printf the
12579 resulting string to stderr. */
12580
12581 static void debug_method_add (struct window *, char const *, ...)
12582 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12583
12584 static void
12585 debug_method_add (struct window *w, char const *fmt, ...)
12586 {
12587 char *method = w->desired_matrix->method;
12588 int len = strlen (method);
12589 int size = sizeof w->desired_matrix->method;
12590 int remaining = size - len - 1;
12591 va_list ap;
12592
12593 if (len && remaining)
12594 {
12595 method[len] = '|';
12596 --remaining, ++len;
12597 }
12598
12599 va_start (ap, fmt);
12600 vsnprintf (method + len, remaining + 1, fmt, ap);
12601 va_end (ap);
12602
12603 if (trace_redisplay_p)
12604 fprintf (stderr, "%p (%s): %s\n",
12605 w,
12606 ((BUFFERP (w->buffer)
12607 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12608 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12609 : "no buffer"),
12610 method + len);
12611 }
12612
12613 #endif /* GLYPH_DEBUG */
12614
12615
12616 /* Value is non-zero if all changes in window W, which displays
12617 current_buffer, are in the text between START and END. START is a
12618 buffer position, END is given as a distance from Z. Used in
12619 redisplay_internal for display optimization. */
12620
12621 static int
12622 text_outside_line_unchanged_p (struct window *w,
12623 ptrdiff_t start, ptrdiff_t end)
12624 {
12625 int unchanged_p = 1;
12626
12627 /* If text or overlays have changed, see where. */
12628 if (w->last_modified < MODIFF
12629 || w->last_overlay_modified < OVERLAY_MODIFF)
12630 {
12631 /* Gap in the line? */
12632 if (GPT < start || Z - GPT < end)
12633 unchanged_p = 0;
12634
12635 /* Changes start in front of the line, or end after it? */
12636 if (unchanged_p
12637 && (BEG_UNCHANGED < start - 1
12638 || END_UNCHANGED < end))
12639 unchanged_p = 0;
12640
12641 /* If selective display, can't optimize if changes start at the
12642 beginning of the line. */
12643 if (unchanged_p
12644 && INTEGERP (BVAR (current_buffer, selective_display))
12645 && XINT (BVAR (current_buffer, selective_display)) > 0
12646 && (BEG_UNCHANGED < start || GPT <= start))
12647 unchanged_p = 0;
12648
12649 /* If there are overlays at the start or end of the line, these
12650 may have overlay strings with newlines in them. A change at
12651 START, for instance, may actually concern the display of such
12652 overlay strings as well, and they are displayed on different
12653 lines. So, quickly rule out this case. (For the future, it
12654 might be desirable to implement something more telling than
12655 just BEG/END_UNCHANGED.) */
12656 if (unchanged_p)
12657 {
12658 if (BEG + BEG_UNCHANGED == start
12659 && overlay_touches_p (start))
12660 unchanged_p = 0;
12661 if (END_UNCHANGED == end
12662 && overlay_touches_p (Z - end))
12663 unchanged_p = 0;
12664 }
12665
12666 /* Under bidi reordering, adding or deleting a character in the
12667 beginning of a paragraph, before the first strong directional
12668 character, can change the base direction of the paragraph (unless
12669 the buffer specifies a fixed paragraph direction), which will
12670 require to redisplay the whole paragraph. It might be worthwhile
12671 to find the paragraph limits and widen the range of redisplayed
12672 lines to that, but for now just give up this optimization. */
12673 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12674 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12675 unchanged_p = 0;
12676 }
12677
12678 return unchanged_p;
12679 }
12680
12681
12682 /* Do a frame update, taking possible shortcuts into account. This is
12683 the main external entry point for redisplay.
12684
12685 If the last redisplay displayed an echo area message and that message
12686 is no longer requested, we clear the echo area or bring back the
12687 mini-buffer if that is in use. */
12688
12689 void
12690 redisplay (void)
12691 {
12692 redisplay_internal ();
12693 }
12694
12695
12696 static Lisp_Object
12697 overlay_arrow_string_or_property (Lisp_Object var)
12698 {
12699 Lisp_Object val;
12700
12701 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12702 return val;
12703
12704 return Voverlay_arrow_string;
12705 }
12706
12707 /* Return 1 if there are any overlay-arrows in current_buffer. */
12708 static int
12709 overlay_arrow_in_current_buffer_p (void)
12710 {
12711 Lisp_Object vlist;
12712
12713 for (vlist = Voverlay_arrow_variable_list;
12714 CONSP (vlist);
12715 vlist = XCDR (vlist))
12716 {
12717 Lisp_Object var = XCAR (vlist);
12718 Lisp_Object val;
12719
12720 if (!SYMBOLP (var))
12721 continue;
12722 val = find_symbol_value (var);
12723 if (MARKERP (val)
12724 && current_buffer == XMARKER (val)->buffer)
12725 return 1;
12726 }
12727 return 0;
12728 }
12729
12730
12731 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12732 has changed. */
12733
12734 static int
12735 overlay_arrows_changed_p (void)
12736 {
12737 Lisp_Object vlist;
12738
12739 for (vlist = Voverlay_arrow_variable_list;
12740 CONSP (vlist);
12741 vlist = XCDR (vlist))
12742 {
12743 Lisp_Object var = XCAR (vlist);
12744 Lisp_Object val, pstr;
12745
12746 if (!SYMBOLP (var))
12747 continue;
12748 val = find_symbol_value (var);
12749 if (!MARKERP (val))
12750 continue;
12751 if (! EQ (COERCE_MARKER (val),
12752 Fget (var, Qlast_arrow_position))
12753 || ! (pstr = overlay_arrow_string_or_property (var),
12754 EQ (pstr, Fget (var, Qlast_arrow_string))))
12755 return 1;
12756 }
12757 return 0;
12758 }
12759
12760 /* Mark overlay arrows to be updated on next redisplay. */
12761
12762 static void
12763 update_overlay_arrows (int up_to_date)
12764 {
12765 Lisp_Object vlist;
12766
12767 for (vlist = Voverlay_arrow_variable_list;
12768 CONSP (vlist);
12769 vlist = XCDR (vlist))
12770 {
12771 Lisp_Object var = XCAR (vlist);
12772
12773 if (!SYMBOLP (var))
12774 continue;
12775
12776 if (up_to_date > 0)
12777 {
12778 Lisp_Object val = find_symbol_value (var);
12779 Fput (var, Qlast_arrow_position,
12780 COERCE_MARKER (val));
12781 Fput (var, Qlast_arrow_string,
12782 overlay_arrow_string_or_property (var));
12783 }
12784 else if (up_to_date < 0
12785 || !NILP (Fget (var, Qlast_arrow_position)))
12786 {
12787 Fput (var, Qlast_arrow_position, Qt);
12788 Fput (var, Qlast_arrow_string, Qt);
12789 }
12790 }
12791 }
12792
12793
12794 /* Return overlay arrow string to display at row.
12795 Return integer (bitmap number) for arrow bitmap in left fringe.
12796 Return nil if no overlay arrow. */
12797
12798 static Lisp_Object
12799 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12800 {
12801 Lisp_Object vlist;
12802
12803 for (vlist = Voverlay_arrow_variable_list;
12804 CONSP (vlist);
12805 vlist = XCDR (vlist))
12806 {
12807 Lisp_Object var = XCAR (vlist);
12808 Lisp_Object val;
12809
12810 if (!SYMBOLP (var))
12811 continue;
12812
12813 val = find_symbol_value (var);
12814
12815 if (MARKERP (val)
12816 && current_buffer == XMARKER (val)->buffer
12817 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12818 {
12819 if (FRAME_WINDOW_P (it->f)
12820 /* FIXME: if ROW->reversed_p is set, this should test
12821 the right fringe, not the left one. */
12822 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12823 {
12824 #ifdef HAVE_WINDOW_SYSTEM
12825 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12826 {
12827 int fringe_bitmap;
12828 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12829 return make_number (fringe_bitmap);
12830 }
12831 #endif
12832 return make_number (-1); /* Use default arrow bitmap. */
12833 }
12834 return overlay_arrow_string_or_property (var);
12835 }
12836 }
12837
12838 return Qnil;
12839 }
12840
12841 /* Return 1 if point moved out of or into a composition. Otherwise
12842 return 0. PREV_BUF and PREV_PT are the last point buffer and
12843 position. BUF and PT are the current point buffer and position. */
12844
12845 static int
12846 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12847 struct buffer *buf, ptrdiff_t pt)
12848 {
12849 ptrdiff_t start, end;
12850 Lisp_Object prop;
12851 Lisp_Object buffer;
12852
12853 XSETBUFFER (buffer, buf);
12854 /* Check a composition at the last point if point moved within the
12855 same buffer. */
12856 if (prev_buf == buf)
12857 {
12858 if (prev_pt == pt)
12859 /* Point didn't move. */
12860 return 0;
12861
12862 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12863 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12864 && COMPOSITION_VALID_P (start, end, prop)
12865 && start < prev_pt && end > prev_pt)
12866 /* The last point was within the composition. Return 1 iff
12867 point moved out of the composition. */
12868 return (pt <= start || pt >= end);
12869 }
12870
12871 /* Check a composition at the current point. */
12872 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12873 && find_composition (pt, -1, &start, &end, &prop, buffer)
12874 && COMPOSITION_VALID_P (start, end, prop)
12875 && start < pt && end > pt);
12876 }
12877
12878
12879 /* Reconsider the setting of B->clip_changed which is displayed
12880 in window W. */
12881
12882 static void
12883 reconsider_clip_changes (struct window *w, struct buffer *b)
12884 {
12885 if (b->clip_changed
12886 && !NILP (w->window_end_valid)
12887 && w->current_matrix->buffer == b
12888 && w->current_matrix->zv == BUF_ZV (b)
12889 && w->current_matrix->begv == BUF_BEGV (b))
12890 b->clip_changed = 0;
12891
12892 /* If display wasn't paused, and W is not a tool bar window, see if
12893 point has been moved into or out of a composition. In that case,
12894 we set b->clip_changed to 1 to force updating the screen. If
12895 b->clip_changed has already been set to 1, we can skip this
12896 check. */
12897 if (!b->clip_changed
12898 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12899 {
12900 ptrdiff_t pt;
12901
12902 if (w == XWINDOW (selected_window))
12903 pt = PT;
12904 else
12905 pt = marker_position (w->pointm);
12906
12907 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12908 || pt != w->last_point)
12909 && check_point_in_composition (w->current_matrix->buffer,
12910 w->last_point,
12911 XBUFFER (w->buffer), pt))
12912 b->clip_changed = 1;
12913 }
12914 }
12915 \f
12916
12917 /* Select FRAME to forward the values of frame-local variables into C
12918 variables so that the redisplay routines can access those values
12919 directly. */
12920
12921 static void
12922 select_frame_for_redisplay (Lisp_Object frame)
12923 {
12924 Lisp_Object tail, tem;
12925 Lisp_Object old = selected_frame;
12926 struct Lisp_Symbol *sym;
12927
12928 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12929
12930 selected_frame = frame;
12931
12932 do {
12933 for (tail = XFRAME (frame)->param_alist;
12934 CONSP (tail); tail = XCDR (tail))
12935 if (CONSP (XCAR (tail))
12936 && (tem = XCAR (XCAR (tail)),
12937 SYMBOLP (tem))
12938 && (sym = indirect_variable (XSYMBOL (tem)),
12939 sym->redirect == SYMBOL_LOCALIZED)
12940 && sym->val.blv->frame_local)
12941 /* Use find_symbol_value rather than Fsymbol_value
12942 to avoid an error if it is void. */
12943 find_symbol_value (tem);
12944 } while (!EQ (frame, old) && (frame = old, 1));
12945 }
12946
12947
12948 #define STOP_POLLING \
12949 do { if (! polling_stopped_here) stop_polling (); \
12950 polling_stopped_here = 1; } while (0)
12951
12952 #define RESUME_POLLING \
12953 do { if (polling_stopped_here) start_polling (); \
12954 polling_stopped_here = 0; } while (0)
12955
12956
12957 /* Perhaps in the future avoid recentering windows if it
12958 is not necessary; currently that causes some problems. */
12959
12960 static void
12961 redisplay_internal (void)
12962 {
12963 struct window *w = XWINDOW (selected_window);
12964 struct window *sw;
12965 struct frame *fr;
12966 int pending;
12967 int must_finish = 0;
12968 struct text_pos tlbufpos, tlendpos;
12969 int number_of_visible_frames;
12970 ptrdiff_t count, count1;
12971 struct frame *sf;
12972 int polling_stopped_here = 0;
12973 Lisp_Object old_frame = selected_frame;
12974 struct backtrace backtrace;
12975
12976 /* Non-zero means redisplay has to consider all windows on all
12977 frames. Zero means, only selected_window is considered. */
12978 int consider_all_windows_p;
12979
12980 /* Non-zero means redisplay has to redisplay the miniwindow. */
12981 int update_miniwindow_p = 0;
12982
12983 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12984
12985 /* No redisplay if running in batch mode or frame is not yet fully
12986 initialized, or redisplay is explicitly turned off by setting
12987 Vinhibit_redisplay. */
12988 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12989 || !NILP (Vinhibit_redisplay))
12990 return;
12991
12992 /* Don't examine these until after testing Vinhibit_redisplay.
12993 When Emacs is shutting down, perhaps because its connection to
12994 X has dropped, we should not look at them at all. */
12995 fr = XFRAME (w->frame);
12996 sf = SELECTED_FRAME ();
12997
12998 if (!fr->glyphs_initialized_p)
12999 return;
13000
13001 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13002 if (popup_activated ())
13003 return;
13004 #endif
13005
13006 /* I don't think this happens but let's be paranoid. */
13007 if (redisplaying_p)
13008 return;
13009
13010 /* Record a function that clears redisplaying_p
13011 when we leave this function. */
13012 count = SPECPDL_INDEX ();
13013 record_unwind_protect (unwind_redisplay, selected_frame);
13014 redisplaying_p = 1;
13015 specbind (Qinhibit_free_realized_faces, Qnil);
13016
13017 /* Record this function, so it appears on the profiler's backtraces. */
13018 backtrace.next = backtrace_list;
13019 backtrace.function = Qredisplay_internal;
13020 backtrace.args = &Qnil;
13021 backtrace.nargs = 0;
13022 backtrace.debug_on_exit = 0;
13023 backtrace_list = &backtrace;
13024
13025 {
13026 Lisp_Object tail, frame;
13027
13028 FOR_EACH_FRAME (tail, frame)
13029 {
13030 struct frame *f = XFRAME (frame);
13031 f->already_hscrolled_p = 0;
13032 }
13033 }
13034
13035 retry:
13036 /* Remember the currently selected window. */
13037 sw = w;
13038
13039 if (!EQ (old_frame, selected_frame)
13040 && FRAME_LIVE_P (XFRAME (old_frame)))
13041 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13042 selected_frame and selected_window to be temporarily out-of-sync so
13043 when we come back here via `goto retry', we need to resync because we
13044 may need to run Elisp code (via prepare_menu_bars). */
13045 select_frame_for_redisplay (old_frame);
13046
13047 pending = 0;
13048 reconsider_clip_changes (w, current_buffer);
13049 last_escape_glyph_frame = NULL;
13050 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13051 last_glyphless_glyph_frame = NULL;
13052 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13053
13054 /* If new fonts have been loaded that make a glyph matrix adjustment
13055 necessary, do it. */
13056 if (fonts_changed_p)
13057 {
13058 adjust_glyphs (NULL);
13059 ++windows_or_buffers_changed;
13060 fonts_changed_p = 0;
13061 }
13062
13063 /* If face_change_count is non-zero, init_iterator will free all
13064 realized faces, which includes the faces referenced from current
13065 matrices. So, we can't reuse current matrices in this case. */
13066 if (face_change_count)
13067 ++windows_or_buffers_changed;
13068
13069 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13070 && FRAME_TTY (sf)->previous_frame != sf)
13071 {
13072 /* Since frames on a single ASCII terminal share the same
13073 display area, displaying a different frame means redisplay
13074 the whole thing. */
13075 windows_or_buffers_changed++;
13076 SET_FRAME_GARBAGED (sf);
13077 #ifndef DOS_NT
13078 set_tty_color_mode (FRAME_TTY (sf), sf);
13079 #endif
13080 FRAME_TTY (sf)->previous_frame = sf;
13081 }
13082
13083 /* Set the visible flags for all frames. Do this before checking
13084 for resized or garbaged frames; they want to know if their frames
13085 are visible. See the comment in frame.h for
13086 FRAME_SAMPLE_VISIBILITY. */
13087 {
13088 Lisp_Object tail, frame;
13089
13090 number_of_visible_frames = 0;
13091
13092 FOR_EACH_FRAME (tail, frame)
13093 {
13094 struct frame *f = XFRAME (frame);
13095
13096 FRAME_SAMPLE_VISIBILITY (f);
13097 if (FRAME_VISIBLE_P (f))
13098 ++number_of_visible_frames;
13099 clear_desired_matrices (f);
13100 }
13101 }
13102
13103 /* Notice any pending interrupt request to change frame size. */
13104 do_pending_window_change (1);
13105
13106 /* do_pending_window_change could change the selected_window due to
13107 frame resizing which makes the selected window too small. */
13108 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13109 {
13110 sw = w;
13111 reconsider_clip_changes (w, current_buffer);
13112 }
13113
13114 /* Clear frames marked as garbaged. */
13115 if (frame_garbaged)
13116 clear_garbaged_frames ();
13117
13118 /* Build menubar and tool-bar items. */
13119 if (NILP (Vmemory_full))
13120 prepare_menu_bars ();
13121
13122 if (windows_or_buffers_changed)
13123 update_mode_lines++;
13124
13125 /* Detect case that we need to write or remove a star in the mode line. */
13126 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13127 {
13128 w->update_mode_line = 1;
13129 if (buffer_shared > 1)
13130 update_mode_lines++;
13131 }
13132
13133 /* Avoid invocation of point motion hooks by `current_column' below. */
13134 count1 = SPECPDL_INDEX ();
13135 specbind (Qinhibit_point_motion_hooks, Qt);
13136
13137 /* If %c is in the mode line, update it if needed. */
13138 if (!NILP (w->column_number_displayed)
13139 /* This alternative quickly identifies a common case
13140 where no change is needed. */
13141 && !(PT == w->last_point
13142 && w->last_modified >= MODIFF
13143 && w->last_overlay_modified >= OVERLAY_MODIFF)
13144 && (XFASTINT (w->column_number_displayed) != current_column ()))
13145 w->update_mode_line = 1;
13146
13147 unbind_to (count1, Qnil);
13148
13149 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13150
13151 /* The variable buffer_shared is set in redisplay_window and
13152 indicates that we redisplay a buffer in different windows. See
13153 there. */
13154 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13155 || cursor_type_changed);
13156
13157 /* If specs for an arrow have changed, do thorough redisplay
13158 to ensure we remove any arrow that should no longer exist. */
13159 if (overlay_arrows_changed_p ())
13160 consider_all_windows_p = windows_or_buffers_changed = 1;
13161
13162 /* Normally the message* functions will have already displayed and
13163 updated the echo area, but the frame may have been trashed, or
13164 the update may have been preempted, so display the echo area
13165 again here. Checking message_cleared_p captures the case that
13166 the echo area should be cleared. */
13167 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13168 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13169 || (message_cleared_p
13170 && minibuf_level == 0
13171 /* If the mini-window is currently selected, this means the
13172 echo-area doesn't show through. */
13173 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13174 {
13175 int window_height_changed_p = echo_area_display (0);
13176
13177 if (message_cleared_p)
13178 update_miniwindow_p = 1;
13179
13180 must_finish = 1;
13181
13182 /* If we don't display the current message, don't clear the
13183 message_cleared_p flag, because, if we did, we wouldn't clear
13184 the echo area in the next redisplay which doesn't preserve
13185 the echo area. */
13186 if (!display_last_displayed_message_p)
13187 message_cleared_p = 0;
13188
13189 if (fonts_changed_p)
13190 goto retry;
13191 else if (window_height_changed_p)
13192 {
13193 consider_all_windows_p = 1;
13194 ++update_mode_lines;
13195 ++windows_or_buffers_changed;
13196
13197 /* If window configuration was changed, frames may have been
13198 marked garbaged. Clear them or we will experience
13199 surprises wrt scrolling. */
13200 if (frame_garbaged)
13201 clear_garbaged_frames ();
13202 }
13203 }
13204 else if (EQ (selected_window, minibuf_window)
13205 && (current_buffer->clip_changed
13206 || w->last_modified < MODIFF
13207 || w->last_overlay_modified < OVERLAY_MODIFF)
13208 && resize_mini_window (w, 0))
13209 {
13210 /* Resized active mini-window to fit the size of what it is
13211 showing if its contents might have changed. */
13212 must_finish = 1;
13213 /* FIXME: this causes all frames to be updated, which seems unnecessary
13214 since only the current frame needs to be considered. This function needs
13215 to be rewritten with two variables, consider_all_windows and
13216 consider_all_frames. */
13217 consider_all_windows_p = 1;
13218 ++windows_or_buffers_changed;
13219 ++update_mode_lines;
13220
13221 /* If window configuration was changed, frames may have been
13222 marked garbaged. Clear them or we will experience
13223 surprises wrt scrolling. */
13224 if (frame_garbaged)
13225 clear_garbaged_frames ();
13226 }
13227
13228
13229 /* If showing the region, and mark has changed, we must redisplay
13230 the whole window. The assignment to this_line_start_pos prevents
13231 the optimization directly below this if-statement. */
13232 if (((!NILP (Vtransient_mark_mode)
13233 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13234 != !NILP (w->region_showing))
13235 || (!NILP (w->region_showing)
13236 && !EQ (w->region_showing,
13237 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13238 CHARPOS (this_line_start_pos) = 0;
13239
13240 /* Optimize the case that only the line containing the cursor in the
13241 selected window has changed. Variables starting with this_ are
13242 set in display_line and record information about the line
13243 containing the cursor. */
13244 tlbufpos = this_line_start_pos;
13245 tlendpos = this_line_end_pos;
13246 if (!consider_all_windows_p
13247 && CHARPOS (tlbufpos) > 0
13248 && !w->update_mode_line
13249 && !current_buffer->clip_changed
13250 && !current_buffer->prevent_redisplay_optimizations_p
13251 && FRAME_VISIBLE_P (XFRAME (w->frame))
13252 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13253 /* Make sure recorded data applies to current buffer, etc. */
13254 && this_line_buffer == current_buffer
13255 && current_buffer == XBUFFER (w->buffer)
13256 && !w->force_start
13257 && !w->optional_new_start
13258 /* Point must be on the line that we have info recorded about. */
13259 && PT >= CHARPOS (tlbufpos)
13260 && PT <= Z - CHARPOS (tlendpos)
13261 /* All text outside that line, including its final newline,
13262 must be unchanged. */
13263 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13264 CHARPOS (tlendpos)))
13265 {
13266 if (CHARPOS (tlbufpos) > BEGV
13267 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13268 && (CHARPOS (tlbufpos) == ZV
13269 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13270 /* Former continuation line has disappeared by becoming empty. */
13271 goto cancel;
13272 else if (w->last_modified < MODIFF
13273 || w->last_overlay_modified < OVERLAY_MODIFF
13274 || MINI_WINDOW_P (w))
13275 {
13276 /* We have to handle the case of continuation around a
13277 wide-column character (see the comment in indent.c around
13278 line 1340).
13279
13280 For instance, in the following case:
13281
13282 -------- Insert --------
13283 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13284 J_I_ ==> J_I_ `^^' are cursors.
13285 ^^ ^^
13286 -------- --------
13287
13288 As we have to redraw the line above, we cannot use this
13289 optimization. */
13290
13291 struct it it;
13292 int line_height_before = this_line_pixel_height;
13293
13294 /* Note that start_display will handle the case that the
13295 line starting at tlbufpos is a continuation line. */
13296 start_display (&it, w, tlbufpos);
13297
13298 /* Implementation note: It this still necessary? */
13299 if (it.current_x != this_line_start_x)
13300 goto cancel;
13301
13302 TRACE ((stderr, "trying display optimization 1\n"));
13303 w->cursor.vpos = -1;
13304 overlay_arrow_seen = 0;
13305 it.vpos = this_line_vpos;
13306 it.current_y = this_line_y;
13307 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13308 display_line (&it);
13309
13310 /* If line contains point, is not continued,
13311 and ends at same distance from eob as before, we win. */
13312 if (w->cursor.vpos >= 0
13313 /* Line is not continued, otherwise this_line_start_pos
13314 would have been set to 0 in display_line. */
13315 && CHARPOS (this_line_start_pos)
13316 /* Line ends as before. */
13317 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13318 /* Line has same height as before. Otherwise other lines
13319 would have to be shifted up or down. */
13320 && this_line_pixel_height == line_height_before)
13321 {
13322 /* If this is not the window's last line, we must adjust
13323 the charstarts of the lines below. */
13324 if (it.current_y < it.last_visible_y)
13325 {
13326 struct glyph_row *row
13327 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13328 ptrdiff_t delta, delta_bytes;
13329
13330 /* We used to distinguish between two cases here,
13331 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13332 when the line ends in a newline or the end of the
13333 buffer's accessible portion. But both cases did
13334 the same, so they were collapsed. */
13335 delta = (Z
13336 - CHARPOS (tlendpos)
13337 - MATRIX_ROW_START_CHARPOS (row));
13338 delta_bytes = (Z_BYTE
13339 - BYTEPOS (tlendpos)
13340 - MATRIX_ROW_START_BYTEPOS (row));
13341
13342 increment_matrix_positions (w->current_matrix,
13343 this_line_vpos + 1,
13344 w->current_matrix->nrows,
13345 delta, delta_bytes);
13346 }
13347
13348 /* If this row displays text now but previously didn't,
13349 or vice versa, w->window_end_vpos may have to be
13350 adjusted. */
13351 if ((it.glyph_row - 1)->displays_text_p)
13352 {
13353 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13354 wset_window_end_vpos (w, make_number (this_line_vpos));
13355 }
13356 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13357 && this_line_vpos > 0)
13358 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13359 wset_window_end_valid (w, Qnil);
13360
13361 /* Update hint: No need to try to scroll in update_window. */
13362 w->desired_matrix->no_scrolling_p = 1;
13363
13364 #ifdef GLYPH_DEBUG
13365 *w->desired_matrix->method = 0;
13366 debug_method_add (w, "optimization 1");
13367 #endif
13368 #ifdef HAVE_WINDOW_SYSTEM
13369 update_window_fringes (w, 0);
13370 #endif
13371 goto update;
13372 }
13373 else
13374 goto cancel;
13375 }
13376 else if (/* Cursor position hasn't changed. */
13377 PT == w->last_point
13378 /* Make sure the cursor was last displayed
13379 in this window. Otherwise we have to reposition it. */
13380 && 0 <= w->cursor.vpos
13381 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13382 {
13383 if (!must_finish)
13384 {
13385 do_pending_window_change (1);
13386 /* If selected_window changed, redisplay again. */
13387 if (WINDOWP (selected_window)
13388 && (w = XWINDOW (selected_window)) != sw)
13389 goto retry;
13390
13391 /* We used to always goto end_of_redisplay here, but this
13392 isn't enough if we have a blinking cursor. */
13393 if (w->cursor_off_p == w->last_cursor_off_p)
13394 goto end_of_redisplay;
13395 }
13396 goto update;
13397 }
13398 /* If highlighting the region, or if the cursor is in the echo area,
13399 then we can't just move the cursor. */
13400 else if (! (!NILP (Vtransient_mark_mode)
13401 && !NILP (BVAR (current_buffer, mark_active)))
13402 && (EQ (selected_window,
13403 BVAR (current_buffer, last_selected_window))
13404 || highlight_nonselected_windows)
13405 && NILP (w->region_showing)
13406 && NILP (Vshow_trailing_whitespace)
13407 && !cursor_in_echo_area)
13408 {
13409 struct it it;
13410 struct glyph_row *row;
13411
13412 /* Skip from tlbufpos to PT and see where it is. Note that
13413 PT may be in invisible text. If so, we will end at the
13414 next visible position. */
13415 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13416 NULL, DEFAULT_FACE_ID);
13417 it.current_x = this_line_start_x;
13418 it.current_y = this_line_y;
13419 it.vpos = this_line_vpos;
13420
13421 /* The call to move_it_to stops in front of PT, but
13422 moves over before-strings. */
13423 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13424
13425 if (it.vpos == this_line_vpos
13426 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13427 row->enabled_p))
13428 {
13429 eassert (this_line_vpos == it.vpos);
13430 eassert (this_line_y == it.current_y);
13431 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13432 #ifdef GLYPH_DEBUG
13433 *w->desired_matrix->method = 0;
13434 debug_method_add (w, "optimization 3");
13435 #endif
13436 goto update;
13437 }
13438 else
13439 goto cancel;
13440 }
13441
13442 cancel:
13443 /* Text changed drastically or point moved off of line. */
13444 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13445 }
13446
13447 CHARPOS (this_line_start_pos) = 0;
13448 consider_all_windows_p |= buffer_shared > 1;
13449 ++clear_face_cache_count;
13450 #ifdef HAVE_WINDOW_SYSTEM
13451 ++clear_image_cache_count;
13452 #endif
13453
13454 /* Build desired matrices, and update the display. If
13455 consider_all_windows_p is non-zero, do it for all windows on all
13456 frames. Otherwise do it for selected_window, only. */
13457
13458 if (consider_all_windows_p)
13459 {
13460 Lisp_Object tail, frame;
13461
13462 FOR_EACH_FRAME (tail, frame)
13463 XFRAME (frame)->updated_p = 0;
13464
13465 /* Recompute # windows showing selected buffer. This will be
13466 incremented each time such a window is displayed. */
13467 buffer_shared = 0;
13468
13469 FOR_EACH_FRAME (tail, frame)
13470 {
13471 struct frame *f = XFRAME (frame);
13472
13473 /* We don't have to do anything for unselected terminal
13474 frames. */
13475 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13476 && !EQ (FRAME_TTY (f)->top_frame, frame))
13477 continue;
13478
13479 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13480 {
13481 if (! EQ (frame, selected_frame))
13482 /* Select the frame, for the sake of frame-local
13483 variables. */
13484 select_frame_for_redisplay (frame);
13485
13486 /* Mark all the scroll bars to be removed; we'll redeem
13487 the ones we want when we redisplay their windows. */
13488 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13489 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13490
13491 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13492 redisplay_windows (FRAME_ROOT_WINDOW (f));
13493
13494 /* The X error handler may have deleted that frame. */
13495 if (!FRAME_LIVE_P (f))
13496 continue;
13497
13498 /* Any scroll bars which redisplay_windows should have
13499 nuked should now go away. */
13500 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13501 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13502
13503 /* If fonts changed, display again. */
13504 /* ??? rms: I suspect it is a mistake to jump all the way
13505 back to retry here. It should just retry this frame. */
13506 if (fonts_changed_p)
13507 goto retry;
13508
13509 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13510 {
13511 /* See if we have to hscroll. */
13512 if (!f->already_hscrolled_p)
13513 {
13514 f->already_hscrolled_p = 1;
13515 if (hscroll_windows (f->root_window))
13516 goto retry;
13517 }
13518
13519 /* Prevent various kinds of signals during display
13520 update. stdio is not robust about handling
13521 signals, which can cause an apparent I/O
13522 error. */
13523 if (interrupt_input)
13524 unrequest_sigio ();
13525 STOP_POLLING;
13526
13527 /* Update the display. */
13528 set_window_update_flags (XWINDOW (f->root_window), 1);
13529 pending |= update_frame (f, 0, 0);
13530 f->updated_p = 1;
13531 }
13532 }
13533 }
13534
13535 if (!EQ (old_frame, selected_frame)
13536 && FRAME_LIVE_P (XFRAME (old_frame)))
13537 /* We played a bit fast-and-loose above and allowed selected_frame
13538 and selected_window to be temporarily out-of-sync but let's make
13539 sure this stays contained. */
13540 select_frame_for_redisplay (old_frame);
13541 eassert (EQ (XFRAME (selected_frame)->selected_window,
13542 selected_window));
13543
13544 if (!pending)
13545 {
13546 /* Do the mark_window_display_accurate after all windows have
13547 been redisplayed because this call resets flags in buffers
13548 which are needed for proper redisplay. */
13549 FOR_EACH_FRAME (tail, frame)
13550 {
13551 struct frame *f = XFRAME (frame);
13552 if (f->updated_p)
13553 {
13554 mark_window_display_accurate (f->root_window, 1);
13555 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13556 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13557 }
13558 }
13559 }
13560 }
13561 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13562 {
13563 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13564 struct frame *mini_frame;
13565
13566 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13567 /* Use list_of_error, not Qerror, so that
13568 we catch only errors and don't run the debugger. */
13569 internal_condition_case_1 (redisplay_window_1, selected_window,
13570 list_of_error,
13571 redisplay_window_error);
13572 if (update_miniwindow_p)
13573 internal_condition_case_1 (redisplay_window_1, mini_window,
13574 list_of_error,
13575 redisplay_window_error);
13576
13577 /* Compare desired and current matrices, perform output. */
13578
13579 update:
13580 /* If fonts changed, display again. */
13581 if (fonts_changed_p)
13582 goto retry;
13583
13584 /* Prevent various kinds of signals during display update.
13585 stdio is not robust about handling signals,
13586 which can cause an apparent I/O error. */
13587 if (interrupt_input)
13588 unrequest_sigio ();
13589 STOP_POLLING;
13590
13591 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13592 {
13593 if (hscroll_windows (selected_window))
13594 goto retry;
13595
13596 XWINDOW (selected_window)->must_be_updated_p = 1;
13597 pending = update_frame (sf, 0, 0);
13598 }
13599
13600 /* We may have called echo_area_display at the top of this
13601 function. If the echo area is on another frame, that may
13602 have put text on a frame other than the selected one, so the
13603 above call to update_frame would not have caught it. Catch
13604 it here. */
13605 mini_window = FRAME_MINIBUF_WINDOW (sf);
13606 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13607
13608 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13609 {
13610 XWINDOW (mini_window)->must_be_updated_p = 1;
13611 pending |= update_frame (mini_frame, 0, 0);
13612 if (!pending && hscroll_windows (mini_window))
13613 goto retry;
13614 }
13615 }
13616
13617 /* If display was paused because of pending input, make sure we do a
13618 thorough update the next time. */
13619 if (pending)
13620 {
13621 /* Prevent the optimization at the beginning of
13622 redisplay_internal that tries a single-line update of the
13623 line containing the cursor in the selected window. */
13624 CHARPOS (this_line_start_pos) = 0;
13625
13626 /* Let the overlay arrow be updated the next time. */
13627 update_overlay_arrows (0);
13628
13629 /* If we pause after scrolling, some rows in the current
13630 matrices of some windows are not valid. */
13631 if (!WINDOW_FULL_WIDTH_P (w)
13632 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13633 update_mode_lines = 1;
13634 }
13635 else
13636 {
13637 if (!consider_all_windows_p)
13638 {
13639 /* This has already been done above if
13640 consider_all_windows_p is set. */
13641 mark_window_display_accurate_1 (w, 1);
13642
13643 /* Say overlay arrows are up to date. */
13644 update_overlay_arrows (1);
13645
13646 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13647 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13648 }
13649
13650 update_mode_lines = 0;
13651 windows_or_buffers_changed = 0;
13652 cursor_type_changed = 0;
13653 }
13654
13655 /* Start SIGIO interrupts coming again. Having them off during the
13656 code above makes it less likely one will discard output, but not
13657 impossible, since there might be stuff in the system buffer here.
13658 But it is much hairier to try to do anything about that. */
13659 if (interrupt_input)
13660 request_sigio ();
13661 RESUME_POLLING;
13662
13663 /* If a frame has become visible which was not before, redisplay
13664 again, so that we display it. Expose events for such a frame
13665 (which it gets when becoming visible) don't call the parts of
13666 redisplay constructing glyphs, so simply exposing a frame won't
13667 display anything in this case. So, we have to display these
13668 frames here explicitly. */
13669 if (!pending)
13670 {
13671 Lisp_Object tail, frame;
13672 int new_count = 0;
13673
13674 FOR_EACH_FRAME (tail, frame)
13675 {
13676 int this_is_visible = 0;
13677
13678 if (XFRAME (frame)->visible)
13679 this_is_visible = 1;
13680 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13681 if (XFRAME (frame)->visible)
13682 this_is_visible = 1;
13683
13684 if (this_is_visible)
13685 new_count++;
13686 }
13687
13688 if (new_count != number_of_visible_frames)
13689 windows_or_buffers_changed++;
13690 }
13691
13692 /* Change frame size now if a change is pending. */
13693 do_pending_window_change (1);
13694
13695 /* If we just did a pending size change, or have additional
13696 visible frames, or selected_window changed, redisplay again. */
13697 if ((windows_or_buffers_changed && !pending)
13698 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13699 goto retry;
13700
13701 /* Clear the face and image caches.
13702
13703 We used to do this only if consider_all_windows_p. But the cache
13704 needs to be cleared if a timer creates images in the current
13705 buffer (e.g. the test case in Bug#6230). */
13706
13707 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13708 {
13709 clear_face_cache (0);
13710 clear_face_cache_count = 0;
13711 }
13712
13713 #ifdef HAVE_WINDOW_SYSTEM
13714 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13715 {
13716 clear_image_caches (Qnil);
13717 clear_image_cache_count = 0;
13718 }
13719 #endif /* HAVE_WINDOW_SYSTEM */
13720
13721 end_of_redisplay:
13722 backtrace_list = backtrace.next;
13723 unbind_to (count, Qnil);
13724 RESUME_POLLING;
13725 }
13726
13727
13728 /* Redisplay, but leave alone any recent echo area message unless
13729 another message has been requested in its place.
13730
13731 This is useful in situations where you need to redisplay but no
13732 user action has occurred, making it inappropriate for the message
13733 area to be cleared. See tracking_off and
13734 wait_reading_process_output for examples of these situations.
13735
13736 FROM_WHERE is an integer saying from where this function was
13737 called. This is useful for debugging. */
13738
13739 void
13740 redisplay_preserve_echo_area (int from_where)
13741 {
13742 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13743
13744 if (!NILP (echo_area_buffer[1]))
13745 {
13746 /* We have a previously displayed message, but no current
13747 message. Redisplay the previous message. */
13748 display_last_displayed_message_p = 1;
13749 redisplay_internal ();
13750 display_last_displayed_message_p = 0;
13751 }
13752 else
13753 redisplay_internal ();
13754
13755 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13756 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13757 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13758 }
13759
13760
13761 /* Function registered with record_unwind_protect in redisplay_internal.
13762 Clear redisplaying_p. Also, select the previously
13763 selected frame, unless it has been deleted (by an X connection
13764 failure during redisplay, for example). */
13765
13766 static Lisp_Object
13767 unwind_redisplay (Lisp_Object old_frame)
13768 {
13769 redisplaying_p = 0;
13770 if (! EQ (old_frame, selected_frame)
13771 && FRAME_LIVE_P (XFRAME (old_frame)))
13772 select_frame_for_redisplay (old_frame);
13773 return Qnil;
13774 }
13775
13776
13777 /* Mark the display of window W as accurate or inaccurate. If
13778 ACCURATE_P is non-zero mark display of W as accurate. If
13779 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13780 redisplay_internal is called. */
13781
13782 static void
13783 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13784 {
13785 if (BUFFERP (w->buffer))
13786 {
13787 struct buffer *b = XBUFFER (w->buffer);
13788
13789 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13790 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13791 w->last_had_star
13792 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13793
13794 if (accurate_p)
13795 {
13796 b->clip_changed = 0;
13797 b->prevent_redisplay_optimizations_p = 0;
13798
13799 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13800 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13801 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13802 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13803
13804 w->current_matrix->buffer = b;
13805 w->current_matrix->begv = BUF_BEGV (b);
13806 w->current_matrix->zv = BUF_ZV (b);
13807
13808 w->last_cursor = w->cursor;
13809 w->last_cursor_off_p = w->cursor_off_p;
13810
13811 if (w == XWINDOW (selected_window))
13812 w->last_point = BUF_PT (b);
13813 else
13814 w->last_point = XMARKER (w->pointm)->charpos;
13815 }
13816 }
13817
13818 if (accurate_p)
13819 {
13820 wset_window_end_valid (w, w->buffer);
13821 w->update_mode_line = 0;
13822 }
13823 }
13824
13825
13826 /* Mark the display of windows in the window tree rooted at WINDOW as
13827 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13828 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13829 be redisplayed the next time redisplay_internal is called. */
13830
13831 void
13832 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13833 {
13834 struct window *w;
13835
13836 for (; !NILP (window); window = w->next)
13837 {
13838 w = XWINDOW (window);
13839 mark_window_display_accurate_1 (w, accurate_p);
13840
13841 if (!NILP (w->vchild))
13842 mark_window_display_accurate (w->vchild, accurate_p);
13843 if (!NILP (w->hchild))
13844 mark_window_display_accurate (w->hchild, accurate_p);
13845 }
13846
13847 if (accurate_p)
13848 {
13849 update_overlay_arrows (1);
13850 }
13851 else
13852 {
13853 /* Force a thorough redisplay the next time by setting
13854 last_arrow_position and last_arrow_string to t, which is
13855 unequal to any useful value of Voverlay_arrow_... */
13856 update_overlay_arrows (-1);
13857 }
13858 }
13859
13860
13861 /* Return value in display table DP (Lisp_Char_Table *) for character
13862 C. Since a display table doesn't have any parent, we don't have to
13863 follow parent. Do not call this function directly but use the
13864 macro DISP_CHAR_VECTOR. */
13865
13866 Lisp_Object
13867 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13868 {
13869 Lisp_Object val;
13870
13871 if (ASCII_CHAR_P (c))
13872 {
13873 val = dp->ascii;
13874 if (SUB_CHAR_TABLE_P (val))
13875 val = XSUB_CHAR_TABLE (val)->contents[c];
13876 }
13877 else
13878 {
13879 Lisp_Object table;
13880
13881 XSETCHAR_TABLE (table, dp);
13882 val = char_table_ref (table, c);
13883 }
13884 if (NILP (val))
13885 val = dp->defalt;
13886 return val;
13887 }
13888
13889
13890 \f
13891 /***********************************************************************
13892 Window Redisplay
13893 ***********************************************************************/
13894
13895 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13896
13897 static void
13898 redisplay_windows (Lisp_Object window)
13899 {
13900 while (!NILP (window))
13901 {
13902 struct window *w = XWINDOW (window);
13903
13904 if (!NILP (w->hchild))
13905 redisplay_windows (w->hchild);
13906 else if (!NILP (w->vchild))
13907 redisplay_windows (w->vchild);
13908 else if (!NILP (w->buffer))
13909 {
13910 displayed_buffer = XBUFFER (w->buffer);
13911 /* Use list_of_error, not Qerror, so that
13912 we catch only errors and don't run the debugger. */
13913 internal_condition_case_1 (redisplay_window_0, window,
13914 list_of_error,
13915 redisplay_window_error);
13916 }
13917
13918 window = w->next;
13919 }
13920 }
13921
13922 static Lisp_Object
13923 redisplay_window_error (Lisp_Object ignore)
13924 {
13925 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13926 return Qnil;
13927 }
13928
13929 static Lisp_Object
13930 redisplay_window_0 (Lisp_Object window)
13931 {
13932 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13933 redisplay_window (window, 0);
13934 return Qnil;
13935 }
13936
13937 static Lisp_Object
13938 redisplay_window_1 (Lisp_Object window)
13939 {
13940 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13941 redisplay_window (window, 1);
13942 return Qnil;
13943 }
13944 \f
13945
13946 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13947 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13948 which positions recorded in ROW differ from current buffer
13949 positions.
13950
13951 Return 0 if cursor is not on this row, 1 otherwise. */
13952
13953 static int
13954 set_cursor_from_row (struct window *w, struct glyph_row *row,
13955 struct glyph_matrix *matrix,
13956 ptrdiff_t delta, ptrdiff_t delta_bytes,
13957 int dy, int dvpos)
13958 {
13959 struct glyph *glyph = row->glyphs[TEXT_AREA];
13960 struct glyph *end = glyph + row->used[TEXT_AREA];
13961 struct glyph *cursor = NULL;
13962 /* The last known character position in row. */
13963 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13964 int x = row->x;
13965 ptrdiff_t pt_old = PT - delta;
13966 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13967 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13968 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13969 /* A glyph beyond the edge of TEXT_AREA which we should never
13970 touch. */
13971 struct glyph *glyphs_end = end;
13972 /* Non-zero means we've found a match for cursor position, but that
13973 glyph has the avoid_cursor_p flag set. */
13974 int match_with_avoid_cursor = 0;
13975 /* Non-zero means we've seen at least one glyph that came from a
13976 display string. */
13977 int string_seen = 0;
13978 /* Largest and smallest buffer positions seen so far during scan of
13979 glyph row. */
13980 ptrdiff_t bpos_max = pos_before;
13981 ptrdiff_t bpos_min = pos_after;
13982 /* Last buffer position covered by an overlay string with an integer
13983 `cursor' property. */
13984 ptrdiff_t bpos_covered = 0;
13985 /* Non-zero means the display string on which to display the cursor
13986 comes from a text property, not from an overlay. */
13987 int string_from_text_prop = 0;
13988
13989 /* Don't even try doing anything if called for a mode-line or
13990 header-line row, since the rest of the code isn't prepared to
13991 deal with such calamities. */
13992 eassert (!row->mode_line_p);
13993 if (row->mode_line_p)
13994 return 0;
13995
13996 /* Skip over glyphs not having an object at the start and the end of
13997 the row. These are special glyphs like truncation marks on
13998 terminal frames. */
13999 if (row->displays_text_p)
14000 {
14001 if (!row->reversed_p)
14002 {
14003 while (glyph < end
14004 && INTEGERP (glyph->object)
14005 && glyph->charpos < 0)
14006 {
14007 x += glyph->pixel_width;
14008 ++glyph;
14009 }
14010 while (end > glyph
14011 && INTEGERP ((end - 1)->object)
14012 /* CHARPOS is zero for blanks and stretch glyphs
14013 inserted by extend_face_to_end_of_line. */
14014 && (end - 1)->charpos <= 0)
14015 --end;
14016 glyph_before = glyph - 1;
14017 glyph_after = end;
14018 }
14019 else
14020 {
14021 struct glyph *g;
14022
14023 /* If the glyph row is reversed, we need to process it from back
14024 to front, so swap the edge pointers. */
14025 glyphs_end = end = glyph - 1;
14026 glyph += row->used[TEXT_AREA] - 1;
14027
14028 while (glyph > end + 1
14029 && INTEGERP (glyph->object)
14030 && glyph->charpos < 0)
14031 {
14032 --glyph;
14033 x -= glyph->pixel_width;
14034 }
14035 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14036 --glyph;
14037 /* By default, in reversed rows we put the cursor on the
14038 rightmost (first in the reading order) glyph. */
14039 for (g = end + 1; g < glyph; g++)
14040 x += g->pixel_width;
14041 while (end < glyph
14042 && INTEGERP ((end + 1)->object)
14043 && (end + 1)->charpos <= 0)
14044 ++end;
14045 glyph_before = glyph + 1;
14046 glyph_after = end;
14047 }
14048 }
14049 else if (row->reversed_p)
14050 {
14051 /* In R2L rows that don't display text, put the cursor on the
14052 rightmost glyph. Case in point: an empty last line that is
14053 part of an R2L paragraph. */
14054 cursor = end - 1;
14055 /* Avoid placing the cursor on the last glyph of the row, where
14056 on terminal frames we hold the vertical border between
14057 adjacent windows. */
14058 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14059 && !WINDOW_RIGHTMOST_P (w)
14060 && cursor == row->glyphs[LAST_AREA] - 1)
14061 cursor--;
14062 x = -1; /* will be computed below, at label compute_x */
14063 }
14064
14065 /* Step 1: Try to find the glyph whose character position
14066 corresponds to point. If that's not possible, find 2 glyphs
14067 whose character positions are the closest to point, one before
14068 point, the other after it. */
14069 if (!row->reversed_p)
14070 while (/* not marched to end of glyph row */
14071 glyph < end
14072 /* glyph was not inserted by redisplay for internal purposes */
14073 && !INTEGERP (glyph->object))
14074 {
14075 if (BUFFERP (glyph->object))
14076 {
14077 ptrdiff_t dpos = glyph->charpos - pt_old;
14078
14079 if (glyph->charpos > bpos_max)
14080 bpos_max = glyph->charpos;
14081 if (glyph->charpos < bpos_min)
14082 bpos_min = glyph->charpos;
14083 if (!glyph->avoid_cursor_p)
14084 {
14085 /* If we hit point, we've found the glyph on which to
14086 display the cursor. */
14087 if (dpos == 0)
14088 {
14089 match_with_avoid_cursor = 0;
14090 break;
14091 }
14092 /* See if we've found a better approximation to
14093 POS_BEFORE or to POS_AFTER. */
14094 if (0 > dpos && dpos > pos_before - pt_old)
14095 {
14096 pos_before = glyph->charpos;
14097 glyph_before = glyph;
14098 }
14099 else if (0 < dpos && dpos < pos_after - pt_old)
14100 {
14101 pos_after = glyph->charpos;
14102 glyph_after = glyph;
14103 }
14104 }
14105 else if (dpos == 0)
14106 match_with_avoid_cursor = 1;
14107 }
14108 else if (STRINGP (glyph->object))
14109 {
14110 Lisp_Object chprop;
14111 ptrdiff_t glyph_pos = glyph->charpos;
14112
14113 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14114 glyph->object);
14115 if (!NILP (chprop))
14116 {
14117 /* If the string came from a `display' text property,
14118 look up the buffer position of that property and
14119 use that position to update bpos_max, as if we
14120 actually saw such a position in one of the row's
14121 glyphs. This helps with supporting integer values
14122 of `cursor' property on the display string in
14123 situations where most or all of the row's buffer
14124 text is completely covered by display properties,
14125 so that no glyph with valid buffer positions is
14126 ever seen in the row. */
14127 ptrdiff_t prop_pos =
14128 string_buffer_position_lim (glyph->object, pos_before,
14129 pos_after, 0);
14130
14131 if (prop_pos >= pos_before)
14132 bpos_max = prop_pos - 1;
14133 }
14134 if (INTEGERP (chprop))
14135 {
14136 bpos_covered = bpos_max + XINT (chprop);
14137 /* If the `cursor' property covers buffer positions up
14138 to and including point, we should display cursor on
14139 this glyph. Note that, if a `cursor' property on one
14140 of the string's characters has an integer value, we
14141 will break out of the loop below _before_ we get to
14142 the position match above. IOW, integer values of
14143 the `cursor' property override the "exact match for
14144 point" strategy of positioning the cursor. */
14145 /* Implementation note: bpos_max == pt_old when, e.g.,
14146 we are in an empty line, where bpos_max is set to
14147 MATRIX_ROW_START_CHARPOS, see above. */
14148 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14149 {
14150 cursor = glyph;
14151 break;
14152 }
14153 }
14154
14155 string_seen = 1;
14156 }
14157 x += glyph->pixel_width;
14158 ++glyph;
14159 }
14160 else if (glyph > end) /* row is reversed */
14161 while (!INTEGERP (glyph->object))
14162 {
14163 if (BUFFERP (glyph->object))
14164 {
14165 ptrdiff_t dpos = glyph->charpos - pt_old;
14166
14167 if (glyph->charpos > bpos_max)
14168 bpos_max = glyph->charpos;
14169 if (glyph->charpos < bpos_min)
14170 bpos_min = glyph->charpos;
14171 if (!glyph->avoid_cursor_p)
14172 {
14173 if (dpos == 0)
14174 {
14175 match_with_avoid_cursor = 0;
14176 break;
14177 }
14178 if (0 > dpos && dpos > pos_before - pt_old)
14179 {
14180 pos_before = glyph->charpos;
14181 glyph_before = glyph;
14182 }
14183 else if (0 < dpos && dpos < pos_after - pt_old)
14184 {
14185 pos_after = glyph->charpos;
14186 glyph_after = glyph;
14187 }
14188 }
14189 else if (dpos == 0)
14190 match_with_avoid_cursor = 1;
14191 }
14192 else if (STRINGP (glyph->object))
14193 {
14194 Lisp_Object chprop;
14195 ptrdiff_t glyph_pos = glyph->charpos;
14196
14197 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14198 glyph->object);
14199 if (!NILP (chprop))
14200 {
14201 ptrdiff_t prop_pos =
14202 string_buffer_position_lim (glyph->object, pos_before,
14203 pos_after, 0);
14204
14205 if (prop_pos >= pos_before)
14206 bpos_max = prop_pos - 1;
14207 }
14208 if (INTEGERP (chprop))
14209 {
14210 bpos_covered = bpos_max + XINT (chprop);
14211 /* If the `cursor' property covers buffer positions up
14212 to and including point, we should display cursor on
14213 this glyph. */
14214 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14215 {
14216 cursor = glyph;
14217 break;
14218 }
14219 }
14220 string_seen = 1;
14221 }
14222 --glyph;
14223 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14224 {
14225 x--; /* can't use any pixel_width */
14226 break;
14227 }
14228 x -= glyph->pixel_width;
14229 }
14230
14231 /* Step 2: If we didn't find an exact match for point, we need to
14232 look for a proper place to put the cursor among glyphs between
14233 GLYPH_BEFORE and GLYPH_AFTER. */
14234 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14235 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14236 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14237 {
14238 /* An empty line has a single glyph whose OBJECT is zero and
14239 whose CHARPOS is the position of a newline on that line.
14240 Note that on a TTY, there are more glyphs after that, which
14241 were produced by extend_face_to_end_of_line, but their
14242 CHARPOS is zero or negative. */
14243 int empty_line_p =
14244 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14245 && INTEGERP (glyph->object) && glyph->charpos > 0
14246 /* On a TTY, continued and truncated rows also have a glyph at
14247 their end whose OBJECT is zero and whose CHARPOS is
14248 positive (the continuation and truncation glyphs), but such
14249 rows are obviously not "empty". */
14250 && !(row->continued_p || row->truncated_on_right_p);
14251
14252 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14253 {
14254 ptrdiff_t ellipsis_pos;
14255
14256 /* Scan back over the ellipsis glyphs. */
14257 if (!row->reversed_p)
14258 {
14259 ellipsis_pos = (glyph - 1)->charpos;
14260 while (glyph > row->glyphs[TEXT_AREA]
14261 && (glyph - 1)->charpos == ellipsis_pos)
14262 glyph--, x -= glyph->pixel_width;
14263 /* That loop always goes one position too far, including
14264 the glyph before the ellipsis. So scan forward over
14265 that one. */
14266 x += glyph->pixel_width;
14267 glyph++;
14268 }
14269 else /* row is reversed */
14270 {
14271 ellipsis_pos = (glyph + 1)->charpos;
14272 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14273 && (glyph + 1)->charpos == ellipsis_pos)
14274 glyph++, x += glyph->pixel_width;
14275 x -= glyph->pixel_width;
14276 glyph--;
14277 }
14278 }
14279 else if (match_with_avoid_cursor)
14280 {
14281 cursor = glyph_after;
14282 x = -1;
14283 }
14284 else if (string_seen)
14285 {
14286 int incr = row->reversed_p ? -1 : +1;
14287
14288 /* Need to find the glyph that came out of a string which is
14289 present at point. That glyph is somewhere between
14290 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14291 positioned between POS_BEFORE and POS_AFTER in the
14292 buffer. */
14293 struct glyph *start, *stop;
14294 ptrdiff_t pos = pos_before;
14295
14296 x = -1;
14297
14298 /* If the row ends in a newline from a display string,
14299 reordering could have moved the glyphs belonging to the
14300 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14301 in this case we extend the search to the last glyph in
14302 the row that was not inserted by redisplay. */
14303 if (row->ends_in_newline_from_string_p)
14304 {
14305 glyph_after = end;
14306 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14307 }
14308
14309 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14310 correspond to POS_BEFORE and POS_AFTER, respectively. We
14311 need START and STOP in the order that corresponds to the
14312 row's direction as given by its reversed_p flag. If the
14313 directionality of characters between POS_BEFORE and
14314 POS_AFTER is the opposite of the row's base direction,
14315 these characters will have been reordered for display,
14316 and we need to reverse START and STOP. */
14317 if (!row->reversed_p)
14318 {
14319 start = min (glyph_before, glyph_after);
14320 stop = max (glyph_before, glyph_after);
14321 }
14322 else
14323 {
14324 start = max (glyph_before, glyph_after);
14325 stop = min (glyph_before, glyph_after);
14326 }
14327 for (glyph = start + incr;
14328 row->reversed_p ? glyph > stop : glyph < stop; )
14329 {
14330
14331 /* Any glyphs that come from the buffer are here because
14332 of bidi reordering. Skip them, and only pay
14333 attention to glyphs that came from some string. */
14334 if (STRINGP (glyph->object))
14335 {
14336 Lisp_Object str;
14337 ptrdiff_t tem;
14338 /* If the display property covers the newline, we
14339 need to search for it one position farther. */
14340 ptrdiff_t lim = pos_after
14341 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14342
14343 string_from_text_prop = 0;
14344 str = glyph->object;
14345 tem = string_buffer_position_lim (str, pos, lim, 0);
14346 if (tem == 0 /* from overlay */
14347 || pos <= tem)
14348 {
14349 /* If the string from which this glyph came is
14350 found in the buffer at point, or at position
14351 that is closer to point than pos_after, then
14352 we've found the glyph we've been looking for.
14353 If it comes from an overlay (tem == 0), and
14354 it has the `cursor' property on one of its
14355 glyphs, record that glyph as a candidate for
14356 displaying the cursor. (As in the
14357 unidirectional version, we will display the
14358 cursor on the last candidate we find.) */
14359 if (tem == 0
14360 || tem == pt_old
14361 || (tem - pt_old > 0 && tem < pos_after))
14362 {
14363 /* The glyphs from this string could have
14364 been reordered. Find the one with the
14365 smallest string position. Or there could
14366 be a character in the string with the
14367 `cursor' property, which means display
14368 cursor on that character's glyph. */
14369 ptrdiff_t strpos = glyph->charpos;
14370
14371 if (tem)
14372 {
14373 cursor = glyph;
14374 string_from_text_prop = 1;
14375 }
14376 for ( ;
14377 (row->reversed_p ? glyph > stop : glyph < stop)
14378 && EQ (glyph->object, str);
14379 glyph += incr)
14380 {
14381 Lisp_Object cprop;
14382 ptrdiff_t gpos = glyph->charpos;
14383
14384 cprop = Fget_char_property (make_number (gpos),
14385 Qcursor,
14386 glyph->object);
14387 if (!NILP (cprop))
14388 {
14389 cursor = glyph;
14390 break;
14391 }
14392 if (tem && glyph->charpos < strpos)
14393 {
14394 strpos = glyph->charpos;
14395 cursor = glyph;
14396 }
14397 }
14398
14399 if (tem == pt_old
14400 || (tem - pt_old > 0 && tem < pos_after))
14401 goto compute_x;
14402 }
14403 if (tem)
14404 pos = tem + 1; /* don't find previous instances */
14405 }
14406 /* This string is not what we want; skip all of the
14407 glyphs that came from it. */
14408 while ((row->reversed_p ? glyph > stop : glyph < stop)
14409 && EQ (glyph->object, str))
14410 glyph += incr;
14411 }
14412 else
14413 glyph += incr;
14414 }
14415
14416 /* If we reached the end of the line, and END was from a string,
14417 the cursor is not on this line. */
14418 if (cursor == NULL
14419 && (row->reversed_p ? glyph <= end : glyph >= end)
14420 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14421 && STRINGP (end->object)
14422 && row->continued_p)
14423 return 0;
14424 }
14425 /* A truncated row may not include PT among its character positions.
14426 Setting the cursor inside the scroll margin will trigger
14427 recalculation of hscroll in hscroll_window_tree. But if a
14428 display string covers point, defer to the string-handling
14429 code below to figure this out. */
14430 else if (row->truncated_on_left_p && pt_old < bpos_min)
14431 {
14432 cursor = glyph_before;
14433 x = -1;
14434 }
14435 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14436 /* Zero-width characters produce no glyphs. */
14437 || (!empty_line_p
14438 && (row->reversed_p
14439 ? glyph_after > glyphs_end
14440 : glyph_after < glyphs_end)))
14441 {
14442 cursor = glyph_after;
14443 x = -1;
14444 }
14445 }
14446
14447 compute_x:
14448 if (cursor != NULL)
14449 glyph = cursor;
14450 else if (glyph == glyphs_end
14451 && pos_before == pos_after
14452 && STRINGP ((row->reversed_p
14453 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14454 : row->glyphs[TEXT_AREA])->object))
14455 {
14456 /* If all the glyphs of this row came from strings, put the
14457 cursor on the first glyph of the row. This avoids having the
14458 cursor outside of the text area in this very rare and hard
14459 use case. */
14460 glyph =
14461 row->reversed_p
14462 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14463 : row->glyphs[TEXT_AREA];
14464 }
14465 if (x < 0)
14466 {
14467 struct glyph *g;
14468
14469 /* Need to compute x that corresponds to GLYPH. */
14470 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14471 {
14472 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14473 emacs_abort ();
14474 x += g->pixel_width;
14475 }
14476 }
14477
14478 /* ROW could be part of a continued line, which, under bidi
14479 reordering, might have other rows whose start and end charpos
14480 occlude point. Only set w->cursor if we found a better
14481 approximation to the cursor position than we have from previously
14482 examined candidate rows belonging to the same continued line. */
14483 if (/* we already have a candidate row */
14484 w->cursor.vpos >= 0
14485 /* that candidate is not the row we are processing */
14486 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14487 /* Make sure cursor.vpos specifies a row whose start and end
14488 charpos occlude point, and it is valid candidate for being a
14489 cursor-row. This is because some callers of this function
14490 leave cursor.vpos at the row where the cursor was displayed
14491 during the last redisplay cycle. */
14492 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14493 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14494 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14495 {
14496 struct glyph *g1 =
14497 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14498
14499 /* Don't consider glyphs that are outside TEXT_AREA. */
14500 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14501 return 0;
14502 /* Keep the candidate whose buffer position is the closest to
14503 point or has the `cursor' property. */
14504 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14505 w->cursor.hpos >= 0
14506 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14507 && ((BUFFERP (g1->object)
14508 && (g1->charpos == pt_old /* an exact match always wins */
14509 || (BUFFERP (glyph->object)
14510 && eabs (g1->charpos - pt_old)
14511 < eabs (glyph->charpos - pt_old))))
14512 /* previous candidate is a glyph from a string that has
14513 a non-nil `cursor' property */
14514 || (STRINGP (g1->object)
14515 && (!NILP (Fget_char_property (make_number (g1->charpos),
14516 Qcursor, g1->object))
14517 /* previous candidate is from the same display
14518 string as this one, and the display string
14519 came from a text property */
14520 || (EQ (g1->object, glyph->object)
14521 && string_from_text_prop)
14522 /* this candidate is from newline and its
14523 position is not an exact match */
14524 || (INTEGERP (glyph->object)
14525 && glyph->charpos != pt_old)))))
14526 return 0;
14527 /* If this candidate gives an exact match, use that. */
14528 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14529 /* If this candidate is a glyph created for the
14530 terminating newline of a line, and point is on that
14531 newline, it wins because it's an exact match. */
14532 || (!row->continued_p
14533 && INTEGERP (glyph->object)
14534 && glyph->charpos == 0
14535 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14536 /* Otherwise, keep the candidate that comes from a row
14537 spanning less buffer positions. This may win when one or
14538 both candidate positions are on glyphs that came from
14539 display strings, for which we cannot compare buffer
14540 positions. */
14541 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14542 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14543 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14544 return 0;
14545 }
14546 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14547 w->cursor.x = x;
14548 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14549 w->cursor.y = row->y + dy;
14550
14551 if (w == XWINDOW (selected_window))
14552 {
14553 if (!row->continued_p
14554 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14555 && row->x == 0)
14556 {
14557 this_line_buffer = XBUFFER (w->buffer);
14558
14559 CHARPOS (this_line_start_pos)
14560 = MATRIX_ROW_START_CHARPOS (row) + delta;
14561 BYTEPOS (this_line_start_pos)
14562 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14563
14564 CHARPOS (this_line_end_pos)
14565 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14566 BYTEPOS (this_line_end_pos)
14567 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14568
14569 this_line_y = w->cursor.y;
14570 this_line_pixel_height = row->height;
14571 this_line_vpos = w->cursor.vpos;
14572 this_line_start_x = row->x;
14573 }
14574 else
14575 CHARPOS (this_line_start_pos) = 0;
14576 }
14577
14578 return 1;
14579 }
14580
14581
14582 /* Run window scroll functions, if any, for WINDOW with new window
14583 start STARTP. Sets the window start of WINDOW to that position.
14584
14585 We assume that the window's buffer is really current. */
14586
14587 static struct text_pos
14588 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14589 {
14590 struct window *w = XWINDOW (window);
14591 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14592
14593 if (current_buffer != XBUFFER (w->buffer))
14594 emacs_abort ();
14595
14596 if (!NILP (Vwindow_scroll_functions))
14597 {
14598 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14599 make_number (CHARPOS (startp)));
14600 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14601 /* In case the hook functions switch buffers. */
14602 set_buffer_internal (XBUFFER (w->buffer));
14603 }
14604
14605 return startp;
14606 }
14607
14608
14609 /* Make sure the line containing the cursor is fully visible.
14610 A value of 1 means there is nothing to be done.
14611 (Either the line is fully visible, or it cannot be made so,
14612 or we cannot tell.)
14613
14614 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14615 is higher than window.
14616
14617 A value of 0 means the caller should do scrolling
14618 as if point had gone off the screen. */
14619
14620 static int
14621 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14622 {
14623 struct glyph_matrix *matrix;
14624 struct glyph_row *row;
14625 int window_height;
14626
14627 if (!make_cursor_line_fully_visible_p)
14628 return 1;
14629
14630 /* It's not always possible to find the cursor, e.g, when a window
14631 is full of overlay strings. Don't do anything in that case. */
14632 if (w->cursor.vpos < 0)
14633 return 1;
14634
14635 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14636 row = MATRIX_ROW (matrix, w->cursor.vpos);
14637
14638 /* If the cursor row is not partially visible, there's nothing to do. */
14639 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14640 return 1;
14641
14642 /* If the row the cursor is in is taller than the window's height,
14643 it's not clear what to do, so do nothing. */
14644 window_height = window_box_height (w);
14645 if (row->height >= window_height)
14646 {
14647 if (!force_p || MINI_WINDOW_P (w)
14648 || w->vscroll || w->cursor.vpos == 0)
14649 return 1;
14650 }
14651 return 0;
14652 }
14653
14654
14655 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14656 non-zero means only WINDOW is redisplayed in redisplay_internal.
14657 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14658 in redisplay_window to bring a partially visible line into view in
14659 the case that only the cursor has moved.
14660
14661 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14662 last screen line's vertical height extends past the end of the screen.
14663
14664 Value is
14665
14666 1 if scrolling succeeded
14667
14668 0 if scrolling didn't find point.
14669
14670 -1 if new fonts have been loaded so that we must interrupt
14671 redisplay, adjust glyph matrices, and try again. */
14672
14673 enum
14674 {
14675 SCROLLING_SUCCESS,
14676 SCROLLING_FAILED,
14677 SCROLLING_NEED_LARGER_MATRICES
14678 };
14679
14680 /* If scroll-conservatively is more than this, never recenter.
14681
14682 If you change this, don't forget to update the doc string of
14683 `scroll-conservatively' and the Emacs manual. */
14684 #define SCROLL_LIMIT 100
14685
14686 static int
14687 try_scrolling (Lisp_Object window, int just_this_one_p,
14688 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14689 int temp_scroll_step, int last_line_misfit)
14690 {
14691 struct window *w = XWINDOW (window);
14692 struct frame *f = XFRAME (w->frame);
14693 struct text_pos pos, startp;
14694 struct it it;
14695 int this_scroll_margin, scroll_max, rc, height;
14696 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14697 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14698 Lisp_Object aggressive;
14699 /* We will never try scrolling more than this number of lines. */
14700 int scroll_limit = SCROLL_LIMIT;
14701
14702 #ifdef GLYPH_DEBUG
14703 debug_method_add (w, "try_scrolling");
14704 #endif
14705
14706 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14707
14708 /* Compute scroll margin height in pixels. We scroll when point is
14709 within this distance from the top or bottom of the window. */
14710 if (scroll_margin > 0)
14711 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14712 * FRAME_LINE_HEIGHT (f);
14713 else
14714 this_scroll_margin = 0;
14715
14716 /* Force arg_scroll_conservatively to have a reasonable value, to
14717 avoid scrolling too far away with slow move_it_* functions. Note
14718 that the user can supply scroll-conservatively equal to
14719 `most-positive-fixnum', which can be larger than INT_MAX. */
14720 if (arg_scroll_conservatively > scroll_limit)
14721 {
14722 arg_scroll_conservatively = scroll_limit + 1;
14723 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14724 }
14725 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14726 /* Compute how much we should try to scroll maximally to bring
14727 point into view. */
14728 scroll_max = (max (scroll_step,
14729 max (arg_scroll_conservatively, temp_scroll_step))
14730 * FRAME_LINE_HEIGHT (f));
14731 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14732 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14733 /* We're trying to scroll because of aggressive scrolling but no
14734 scroll_step is set. Choose an arbitrary one. */
14735 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14736 else
14737 scroll_max = 0;
14738
14739 too_near_end:
14740
14741 /* Decide whether to scroll down. */
14742 if (PT > CHARPOS (startp))
14743 {
14744 int scroll_margin_y;
14745
14746 /* Compute the pixel ypos of the scroll margin, then move IT to
14747 either that ypos or PT, whichever comes first. */
14748 start_display (&it, w, startp);
14749 scroll_margin_y = it.last_visible_y - this_scroll_margin
14750 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14751 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14752 (MOVE_TO_POS | MOVE_TO_Y));
14753
14754 if (PT > CHARPOS (it.current.pos))
14755 {
14756 int y0 = line_bottom_y (&it);
14757 /* Compute how many pixels below window bottom to stop searching
14758 for PT. This avoids costly search for PT that is far away if
14759 the user limited scrolling by a small number of lines, but
14760 always finds PT if scroll_conservatively is set to a large
14761 number, such as most-positive-fixnum. */
14762 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14763 int y_to_move = it.last_visible_y + slack;
14764
14765 /* Compute the distance from the scroll margin to PT or to
14766 the scroll limit, whichever comes first. This should
14767 include the height of the cursor line, to make that line
14768 fully visible. */
14769 move_it_to (&it, PT, -1, y_to_move,
14770 -1, MOVE_TO_POS | MOVE_TO_Y);
14771 dy = line_bottom_y (&it) - y0;
14772
14773 if (dy > scroll_max)
14774 return SCROLLING_FAILED;
14775
14776 if (dy > 0)
14777 scroll_down_p = 1;
14778 }
14779 }
14780
14781 if (scroll_down_p)
14782 {
14783 /* Point is in or below the bottom scroll margin, so move the
14784 window start down. If scrolling conservatively, move it just
14785 enough down to make point visible. If scroll_step is set,
14786 move it down by scroll_step. */
14787 if (arg_scroll_conservatively)
14788 amount_to_scroll
14789 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14790 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14791 else if (scroll_step || temp_scroll_step)
14792 amount_to_scroll = scroll_max;
14793 else
14794 {
14795 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14796 height = WINDOW_BOX_TEXT_HEIGHT (w);
14797 if (NUMBERP (aggressive))
14798 {
14799 double float_amount = XFLOATINT (aggressive) * height;
14800 int aggressive_scroll = float_amount;
14801 if (aggressive_scroll == 0 && float_amount > 0)
14802 aggressive_scroll = 1;
14803 /* Don't let point enter the scroll margin near top of
14804 the window. This could happen if the value of
14805 scroll_up_aggressively is too large and there are
14806 non-zero margins, because scroll_up_aggressively
14807 means put point that fraction of window height
14808 _from_the_bottom_margin_. */
14809 if (aggressive_scroll + 2*this_scroll_margin > height)
14810 aggressive_scroll = height - 2*this_scroll_margin;
14811 amount_to_scroll = dy + aggressive_scroll;
14812 }
14813 }
14814
14815 if (amount_to_scroll <= 0)
14816 return SCROLLING_FAILED;
14817
14818 start_display (&it, w, startp);
14819 if (arg_scroll_conservatively <= scroll_limit)
14820 move_it_vertically (&it, amount_to_scroll);
14821 else
14822 {
14823 /* Extra precision for users who set scroll-conservatively
14824 to a large number: make sure the amount we scroll
14825 the window start is never less than amount_to_scroll,
14826 which was computed as distance from window bottom to
14827 point. This matters when lines at window top and lines
14828 below window bottom have different height. */
14829 struct it it1;
14830 void *it1data = NULL;
14831 /* We use a temporary it1 because line_bottom_y can modify
14832 its argument, if it moves one line down; see there. */
14833 int start_y;
14834
14835 SAVE_IT (it1, it, it1data);
14836 start_y = line_bottom_y (&it1);
14837 do {
14838 RESTORE_IT (&it, &it, it1data);
14839 move_it_by_lines (&it, 1);
14840 SAVE_IT (it1, it, it1data);
14841 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14842 }
14843
14844 /* If STARTP is unchanged, move it down another screen line. */
14845 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14846 move_it_by_lines (&it, 1);
14847 startp = it.current.pos;
14848 }
14849 else
14850 {
14851 struct text_pos scroll_margin_pos = startp;
14852
14853 /* See if point is inside the scroll margin at the top of the
14854 window. */
14855 if (this_scroll_margin)
14856 {
14857 start_display (&it, w, startp);
14858 move_it_vertically (&it, this_scroll_margin);
14859 scroll_margin_pos = it.current.pos;
14860 }
14861
14862 if (PT < CHARPOS (scroll_margin_pos))
14863 {
14864 /* Point is in the scroll margin at the top of the window or
14865 above what is displayed in the window. */
14866 int y0, y_to_move;
14867
14868 /* Compute the vertical distance from PT to the scroll
14869 margin position. Move as far as scroll_max allows, or
14870 one screenful, or 10 screen lines, whichever is largest.
14871 Give up if distance is greater than scroll_max or if we
14872 didn't reach the scroll margin position. */
14873 SET_TEXT_POS (pos, PT, PT_BYTE);
14874 start_display (&it, w, pos);
14875 y0 = it.current_y;
14876 y_to_move = max (it.last_visible_y,
14877 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14878 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14879 y_to_move, -1,
14880 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14881 dy = it.current_y - y0;
14882 if (dy > scroll_max
14883 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14884 return SCROLLING_FAILED;
14885
14886 /* Compute new window start. */
14887 start_display (&it, w, startp);
14888
14889 if (arg_scroll_conservatively)
14890 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14891 max (scroll_step, temp_scroll_step));
14892 else if (scroll_step || temp_scroll_step)
14893 amount_to_scroll = scroll_max;
14894 else
14895 {
14896 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14897 height = WINDOW_BOX_TEXT_HEIGHT (w);
14898 if (NUMBERP (aggressive))
14899 {
14900 double float_amount = XFLOATINT (aggressive) * height;
14901 int aggressive_scroll = float_amount;
14902 if (aggressive_scroll == 0 && float_amount > 0)
14903 aggressive_scroll = 1;
14904 /* Don't let point enter the scroll margin near
14905 bottom of the window, if the value of
14906 scroll_down_aggressively happens to be too
14907 large. */
14908 if (aggressive_scroll + 2*this_scroll_margin > height)
14909 aggressive_scroll = height - 2*this_scroll_margin;
14910 amount_to_scroll = dy + aggressive_scroll;
14911 }
14912 }
14913
14914 if (amount_to_scroll <= 0)
14915 return SCROLLING_FAILED;
14916
14917 move_it_vertically_backward (&it, amount_to_scroll);
14918 startp = it.current.pos;
14919 }
14920 }
14921
14922 /* Run window scroll functions. */
14923 startp = run_window_scroll_functions (window, startp);
14924
14925 /* Display the window. Give up if new fonts are loaded, or if point
14926 doesn't appear. */
14927 if (!try_window (window, startp, 0))
14928 rc = SCROLLING_NEED_LARGER_MATRICES;
14929 else if (w->cursor.vpos < 0)
14930 {
14931 clear_glyph_matrix (w->desired_matrix);
14932 rc = SCROLLING_FAILED;
14933 }
14934 else
14935 {
14936 /* Maybe forget recorded base line for line number display. */
14937 if (!just_this_one_p
14938 || current_buffer->clip_changed
14939 || BEG_UNCHANGED < CHARPOS (startp))
14940 wset_base_line_number (w, Qnil);
14941
14942 /* If cursor ends up on a partially visible line,
14943 treat that as being off the bottom of the screen. */
14944 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14945 /* It's possible that the cursor is on the first line of the
14946 buffer, which is partially obscured due to a vscroll
14947 (Bug#7537). In that case, avoid looping forever . */
14948 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14949 {
14950 clear_glyph_matrix (w->desired_matrix);
14951 ++extra_scroll_margin_lines;
14952 goto too_near_end;
14953 }
14954 rc = SCROLLING_SUCCESS;
14955 }
14956
14957 return rc;
14958 }
14959
14960
14961 /* Compute a suitable window start for window W if display of W starts
14962 on a continuation line. Value is non-zero if a new window start
14963 was computed.
14964
14965 The new window start will be computed, based on W's width, starting
14966 from the start of the continued line. It is the start of the
14967 screen line with the minimum distance from the old start W->start. */
14968
14969 static int
14970 compute_window_start_on_continuation_line (struct window *w)
14971 {
14972 struct text_pos pos, start_pos;
14973 int window_start_changed_p = 0;
14974
14975 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14976
14977 /* If window start is on a continuation line... Window start may be
14978 < BEGV in case there's invisible text at the start of the
14979 buffer (M-x rmail, for example). */
14980 if (CHARPOS (start_pos) > BEGV
14981 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14982 {
14983 struct it it;
14984 struct glyph_row *row;
14985
14986 /* Handle the case that the window start is out of range. */
14987 if (CHARPOS (start_pos) < BEGV)
14988 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14989 else if (CHARPOS (start_pos) > ZV)
14990 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14991
14992 /* Find the start of the continued line. This should be fast
14993 because scan_buffer is fast (newline cache). */
14994 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14995 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14996 row, DEFAULT_FACE_ID);
14997 reseat_at_previous_visible_line_start (&it);
14998
14999 /* If the line start is "too far" away from the window start,
15000 say it takes too much time to compute a new window start. */
15001 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15002 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15003 {
15004 int min_distance, distance;
15005
15006 /* Move forward by display lines to find the new window
15007 start. If window width was enlarged, the new start can
15008 be expected to be > the old start. If window width was
15009 decreased, the new window start will be < the old start.
15010 So, we're looking for the display line start with the
15011 minimum distance from the old window start. */
15012 pos = it.current.pos;
15013 min_distance = INFINITY;
15014 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15015 distance < min_distance)
15016 {
15017 min_distance = distance;
15018 pos = it.current.pos;
15019 move_it_by_lines (&it, 1);
15020 }
15021
15022 /* Set the window start there. */
15023 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15024 window_start_changed_p = 1;
15025 }
15026 }
15027
15028 return window_start_changed_p;
15029 }
15030
15031
15032 /* Try cursor movement in case text has not changed in window WINDOW,
15033 with window start STARTP. Value is
15034
15035 CURSOR_MOVEMENT_SUCCESS if successful
15036
15037 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15038
15039 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15040 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15041 we want to scroll as if scroll-step were set to 1. See the code.
15042
15043 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15044 which case we have to abort this redisplay, and adjust matrices
15045 first. */
15046
15047 enum
15048 {
15049 CURSOR_MOVEMENT_SUCCESS,
15050 CURSOR_MOVEMENT_CANNOT_BE_USED,
15051 CURSOR_MOVEMENT_MUST_SCROLL,
15052 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15053 };
15054
15055 static int
15056 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15057 {
15058 struct window *w = XWINDOW (window);
15059 struct frame *f = XFRAME (w->frame);
15060 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15061
15062 #ifdef GLYPH_DEBUG
15063 if (inhibit_try_cursor_movement)
15064 return rc;
15065 #endif
15066
15067 /* Previously, there was a check for Lisp integer in the
15068 if-statement below. Now, this field is converted to
15069 ptrdiff_t, thus zero means invalid position in a buffer. */
15070 eassert (w->last_point > 0);
15071
15072 /* Handle case where text has not changed, only point, and it has
15073 not moved off the frame. */
15074 if (/* Point may be in this window. */
15075 PT >= CHARPOS (startp)
15076 /* Selective display hasn't changed. */
15077 && !current_buffer->clip_changed
15078 /* Function force-mode-line-update is used to force a thorough
15079 redisplay. It sets either windows_or_buffers_changed or
15080 update_mode_lines. So don't take a shortcut here for these
15081 cases. */
15082 && !update_mode_lines
15083 && !windows_or_buffers_changed
15084 && !cursor_type_changed
15085 /* Can't use this case if highlighting a region. When a
15086 region exists, cursor movement has to do more than just
15087 set the cursor. */
15088 && !(!NILP (Vtransient_mark_mode)
15089 && !NILP (BVAR (current_buffer, mark_active)))
15090 && NILP (w->region_showing)
15091 && NILP (Vshow_trailing_whitespace)
15092 /* This code is not used for mini-buffer for the sake of the case
15093 of redisplaying to replace an echo area message; since in
15094 that case the mini-buffer contents per se are usually
15095 unchanged. This code is of no real use in the mini-buffer
15096 since the handling of this_line_start_pos, etc., in redisplay
15097 handles the same cases. */
15098 && !EQ (window, minibuf_window)
15099 /* When splitting windows or for new windows, it happens that
15100 redisplay is called with a nil window_end_vpos or one being
15101 larger than the window. This should really be fixed in
15102 window.c. I don't have this on my list, now, so we do
15103 approximately the same as the old redisplay code. --gerd. */
15104 && INTEGERP (w->window_end_vpos)
15105 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15106 && (FRAME_WINDOW_P (f)
15107 || !overlay_arrow_in_current_buffer_p ()))
15108 {
15109 int this_scroll_margin, top_scroll_margin;
15110 struct glyph_row *row = NULL;
15111
15112 #ifdef GLYPH_DEBUG
15113 debug_method_add (w, "cursor movement");
15114 #endif
15115
15116 /* Scroll if point within this distance from the top or bottom
15117 of the window. This is a pixel value. */
15118 if (scroll_margin > 0)
15119 {
15120 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15121 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15122 }
15123 else
15124 this_scroll_margin = 0;
15125
15126 top_scroll_margin = this_scroll_margin;
15127 if (WINDOW_WANTS_HEADER_LINE_P (w))
15128 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15129
15130 /* Start with the row the cursor was displayed during the last
15131 not paused redisplay. Give up if that row is not valid. */
15132 if (w->last_cursor.vpos < 0
15133 || w->last_cursor.vpos >= w->current_matrix->nrows)
15134 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15135 else
15136 {
15137 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15138 if (row->mode_line_p)
15139 ++row;
15140 if (!row->enabled_p)
15141 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15142 }
15143
15144 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15145 {
15146 int scroll_p = 0, must_scroll = 0;
15147 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15148
15149 if (PT > w->last_point)
15150 {
15151 /* Point has moved forward. */
15152 while (MATRIX_ROW_END_CHARPOS (row) < PT
15153 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15154 {
15155 eassert (row->enabled_p);
15156 ++row;
15157 }
15158
15159 /* If the end position of a row equals the start
15160 position of the next row, and PT is at that position,
15161 we would rather display cursor in the next line. */
15162 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15163 && MATRIX_ROW_END_CHARPOS (row) == PT
15164 && row < w->current_matrix->rows
15165 + w->current_matrix->nrows - 1
15166 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15167 && !cursor_row_p (row))
15168 ++row;
15169
15170 /* If within the scroll margin, scroll. Note that
15171 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15172 the next line would be drawn, and that
15173 this_scroll_margin can be zero. */
15174 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15175 || PT > MATRIX_ROW_END_CHARPOS (row)
15176 /* Line is completely visible last line in window
15177 and PT is to be set in the next line. */
15178 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15179 && PT == MATRIX_ROW_END_CHARPOS (row)
15180 && !row->ends_at_zv_p
15181 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15182 scroll_p = 1;
15183 }
15184 else if (PT < w->last_point)
15185 {
15186 /* Cursor has to be moved backward. Note that PT >=
15187 CHARPOS (startp) because of the outer if-statement. */
15188 while (!row->mode_line_p
15189 && (MATRIX_ROW_START_CHARPOS (row) > PT
15190 || (MATRIX_ROW_START_CHARPOS (row) == PT
15191 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15192 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15193 row > w->current_matrix->rows
15194 && (row-1)->ends_in_newline_from_string_p))))
15195 && (row->y > top_scroll_margin
15196 || CHARPOS (startp) == BEGV))
15197 {
15198 eassert (row->enabled_p);
15199 --row;
15200 }
15201
15202 /* Consider the following case: Window starts at BEGV,
15203 there is invisible, intangible text at BEGV, so that
15204 display starts at some point START > BEGV. It can
15205 happen that we are called with PT somewhere between
15206 BEGV and START. Try to handle that case. */
15207 if (row < w->current_matrix->rows
15208 || row->mode_line_p)
15209 {
15210 row = w->current_matrix->rows;
15211 if (row->mode_line_p)
15212 ++row;
15213 }
15214
15215 /* Due to newlines in overlay strings, we may have to
15216 skip forward over overlay strings. */
15217 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15218 && MATRIX_ROW_END_CHARPOS (row) == PT
15219 && !cursor_row_p (row))
15220 ++row;
15221
15222 /* If within the scroll margin, scroll. */
15223 if (row->y < top_scroll_margin
15224 && CHARPOS (startp) != BEGV)
15225 scroll_p = 1;
15226 }
15227 else
15228 {
15229 /* Cursor did not move. So don't scroll even if cursor line
15230 is partially visible, as it was so before. */
15231 rc = CURSOR_MOVEMENT_SUCCESS;
15232 }
15233
15234 if (PT < MATRIX_ROW_START_CHARPOS (row)
15235 || PT > MATRIX_ROW_END_CHARPOS (row))
15236 {
15237 /* if PT is not in the glyph row, give up. */
15238 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15239 must_scroll = 1;
15240 }
15241 else if (rc != CURSOR_MOVEMENT_SUCCESS
15242 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15243 {
15244 struct glyph_row *row1;
15245
15246 /* If rows are bidi-reordered and point moved, back up
15247 until we find a row that does not belong to a
15248 continuation line. This is because we must consider
15249 all rows of a continued line as candidates for the
15250 new cursor positioning, since row start and end
15251 positions change non-linearly with vertical position
15252 in such rows. */
15253 /* FIXME: Revisit this when glyph ``spilling'' in
15254 continuation lines' rows is implemented for
15255 bidi-reordered rows. */
15256 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15257 MATRIX_ROW_CONTINUATION_LINE_P (row);
15258 --row)
15259 {
15260 /* If we hit the beginning of the displayed portion
15261 without finding the first row of a continued
15262 line, give up. */
15263 if (row <= row1)
15264 {
15265 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15266 break;
15267 }
15268 eassert (row->enabled_p);
15269 }
15270 }
15271 if (must_scroll)
15272 ;
15273 else if (rc != CURSOR_MOVEMENT_SUCCESS
15274 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15275 /* Make sure this isn't a header line by any chance, since
15276 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15277 && !row->mode_line_p
15278 && make_cursor_line_fully_visible_p)
15279 {
15280 if (PT == MATRIX_ROW_END_CHARPOS (row)
15281 && !row->ends_at_zv_p
15282 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 else if (row->height > window_box_height (w))
15285 {
15286 /* If we end up in a partially visible line, let's
15287 make it fully visible, except when it's taller
15288 than the window, in which case we can't do much
15289 about it. */
15290 *scroll_step = 1;
15291 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15292 }
15293 else
15294 {
15295 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15296 if (!cursor_row_fully_visible_p (w, 0, 1))
15297 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15298 else
15299 rc = CURSOR_MOVEMENT_SUCCESS;
15300 }
15301 }
15302 else if (scroll_p)
15303 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15304 else if (rc != CURSOR_MOVEMENT_SUCCESS
15305 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15306 {
15307 /* With bidi-reordered rows, there could be more than
15308 one candidate row whose start and end positions
15309 occlude point. We need to let set_cursor_from_row
15310 find the best candidate. */
15311 /* FIXME: Revisit this when glyph ``spilling'' in
15312 continuation lines' rows is implemented for
15313 bidi-reordered rows. */
15314 int rv = 0;
15315
15316 do
15317 {
15318 int at_zv_p = 0, exact_match_p = 0;
15319
15320 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15321 && PT <= MATRIX_ROW_END_CHARPOS (row)
15322 && cursor_row_p (row))
15323 rv |= set_cursor_from_row (w, row, w->current_matrix,
15324 0, 0, 0, 0);
15325 /* As soon as we've found the exact match for point,
15326 or the first suitable row whose ends_at_zv_p flag
15327 is set, we are done. */
15328 at_zv_p =
15329 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15330 if (rv && !at_zv_p
15331 && w->cursor.hpos >= 0
15332 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15333 w->cursor.vpos))
15334 {
15335 struct glyph_row *candidate =
15336 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15337 struct glyph *g =
15338 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15339 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15340
15341 exact_match_p =
15342 (BUFFERP (g->object) && g->charpos == PT)
15343 || (INTEGERP (g->object)
15344 && (g->charpos == PT
15345 || (g->charpos == 0 && endpos - 1 == PT)));
15346 }
15347 if (rv && (at_zv_p || exact_match_p))
15348 {
15349 rc = CURSOR_MOVEMENT_SUCCESS;
15350 break;
15351 }
15352 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15353 break;
15354 ++row;
15355 }
15356 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15357 || row->continued_p)
15358 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15359 || (MATRIX_ROW_START_CHARPOS (row) == PT
15360 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15361 /* If we didn't find any candidate rows, or exited the
15362 loop before all the candidates were examined, signal
15363 to the caller that this method failed. */
15364 if (rc != CURSOR_MOVEMENT_SUCCESS
15365 && !(rv
15366 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15367 && !row->continued_p))
15368 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15369 else if (rv)
15370 rc = CURSOR_MOVEMENT_SUCCESS;
15371 }
15372 else
15373 {
15374 do
15375 {
15376 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15377 {
15378 rc = CURSOR_MOVEMENT_SUCCESS;
15379 break;
15380 }
15381 ++row;
15382 }
15383 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15384 && MATRIX_ROW_START_CHARPOS (row) == PT
15385 && cursor_row_p (row));
15386 }
15387 }
15388 }
15389
15390 return rc;
15391 }
15392
15393 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15394 static
15395 #endif
15396 void
15397 set_vertical_scroll_bar (struct window *w)
15398 {
15399 ptrdiff_t start, end, whole;
15400
15401 /* Calculate the start and end positions for the current window.
15402 At some point, it would be nice to choose between scrollbars
15403 which reflect the whole buffer size, with special markers
15404 indicating narrowing, and scrollbars which reflect only the
15405 visible region.
15406
15407 Note that mini-buffers sometimes aren't displaying any text. */
15408 if (!MINI_WINDOW_P (w)
15409 || (w == XWINDOW (minibuf_window)
15410 && NILP (echo_area_buffer[0])))
15411 {
15412 struct buffer *buf = XBUFFER (w->buffer);
15413 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15414 start = marker_position (w->start) - BUF_BEGV (buf);
15415 /* I don't think this is guaranteed to be right. For the
15416 moment, we'll pretend it is. */
15417 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15418
15419 if (end < start)
15420 end = start;
15421 if (whole < (end - start))
15422 whole = end - start;
15423 }
15424 else
15425 start = end = whole = 0;
15426
15427 /* Indicate what this scroll bar ought to be displaying now. */
15428 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15429 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15430 (w, end - start, whole, start);
15431 }
15432
15433
15434 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15435 selected_window is redisplayed.
15436
15437 We can return without actually redisplaying the window if
15438 fonts_changed_p. In that case, redisplay_internal will
15439 retry. */
15440
15441 static void
15442 redisplay_window (Lisp_Object window, int just_this_one_p)
15443 {
15444 struct window *w = XWINDOW (window);
15445 struct frame *f = XFRAME (w->frame);
15446 struct buffer *buffer = XBUFFER (w->buffer);
15447 struct buffer *old = current_buffer;
15448 struct text_pos lpoint, opoint, startp;
15449 int update_mode_line;
15450 int tem;
15451 struct it it;
15452 /* Record it now because it's overwritten. */
15453 int current_matrix_up_to_date_p = 0;
15454 int used_current_matrix_p = 0;
15455 /* This is less strict than current_matrix_up_to_date_p.
15456 It indicates that the buffer contents and narrowing are unchanged. */
15457 int buffer_unchanged_p = 0;
15458 int temp_scroll_step = 0;
15459 ptrdiff_t count = SPECPDL_INDEX ();
15460 int rc;
15461 int centering_position = -1;
15462 int last_line_misfit = 0;
15463 ptrdiff_t beg_unchanged, end_unchanged;
15464
15465 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15466 opoint = lpoint;
15467
15468 /* W must be a leaf window here. */
15469 eassert (!NILP (w->buffer));
15470 #ifdef GLYPH_DEBUG
15471 *w->desired_matrix->method = 0;
15472 #endif
15473
15474 restart:
15475 reconsider_clip_changes (w, buffer);
15476
15477 /* Has the mode line to be updated? */
15478 update_mode_line = (w->update_mode_line
15479 || update_mode_lines
15480 || buffer->clip_changed
15481 || buffer->prevent_redisplay_optimizations_p);
15482
15483 if (MINI_WINDOW_P (w))
15484 {
15485 if (w == XWINDOW (echo_area_window)
15486 && !NILP (echo_area_buffer[0]))
15487 {
15488 if (update_mode_line)
15489 /* We may have to update a tty frame's menu bar or a
15490 tool-bar. Example `M-x C-h C-h C-g'. */
15491 goto finish_menu_bars;
15492 else
15493 /* We've already displayed the echo area glyphs in this window. */
15494 goto finish_scroll_bars;
15495 }
15496 else if ((w != XWINDOW (minibuf_window)
15497 || minibuf_level == 0)
15498 /* When buffer is nonempty, redisplay window normally. */
15499 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15500 /* Quail displays non-mini buffers in minibuffer window.
15501 In that case, redisplay the window normally. */
15502 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15503 {
15504 /* W is a mini-buffer window, but it's not active, so clear
15505 it. */
15506 int yb = window_text_bottom_y (w);
15507 struct glyph_row *row;
15508 int y;
15509
15510 for (y = 0, row = w->desired_matrix->rows;
15511 y < yb;
15512 y += row->height, ++row)
15513 blank_row (w, row, y);
15514 goto finish_scroll_bars;
15515 }
15516
15517 clear_glyph_matrix (w->desired_matrix);
15518 }
15519
15520 /* Otherwise set up data on this window; select its buffer and point
15521 value. */
15522 /* Really select the buffer, for the sake of buffer-local
15523 variables. */
15524 set_buffer_internal_1 (XBUFFER (w->buffer));
15525
15526 current_matrix_up_to_date_p
15527 = (!NILP (w->window_end_valid)
15528 && !current_buffer->clip_changed
15529 && !current_buffer->prevent_redisplay_optimizations_p
15530 && w->last_modified >= MODIFF
15531 && w->last_overlay_modified >= OVERLAY_MODIFF);
15532
15533 /* Run the window-bottom-change-functions
15534 if it is possible that the text on the screen has changed
15535 (either due to modification of the text, or any other reason). */
15536 if (!current_matrix_up_to_date_p
15537 && !NILP (Vwindow_text_change_functions))
15538 {
15539 safe_run_hooks (Qwindow_text_change_functions);
15540 goto restart;
15541 }
15542
15543 beg_unchanged = BEG_UNCHANGED;
15544 end_unchanged = END_UNCHANGED;
15545
15546 SET_TEXT_POS (opoint, PT, PT_BYTE);
15547
15548 specbind (Qinhibit_point_motion_hooks, Qt);
15549
15550 buffer_unchanged_p
15551 = (!NILP (w->window_end_valid)
15552 && !current_buffer->clip_changed
15553 && w->last_modified >= MODIFF
15554 && w->last_overlay_modified >= OVERLAY_MODIFF);
15555
15556 /* When windows_or_buffers_changed is non-zero, we can't rely on
15557 the window end being valid, so set it to nil there. */
15558 if (windows_or_buffers_changed)
15559 {
15560 /* If window starts on a continuation line, maybe adjust the
15561 window start in case the window's width changed. */
15562 if (XMARKER (w->start)->buffer == current_buffer)
15563 compute_window_start_on_continuation_line (w);
15564
15565 wset_window_end_valid (w, Qnil);
15566 }
15567
15568 /* Some sanity checks. */
15569 CHECK_WINDOW_END (w);
15570 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15571 emacs_abort ();
15572 if (BYTEPOS (opoint) < CHARPOS (opoint))
15573 emacs_abort ();
15574
15575 /* If %c is in mode line, update it if needed. */
15576 if (!NILP (w->column_number_displayed)
15577 /* This alternative quickly identifies a common case
15578 where no change is needed. */
15579 && !(PT == w->last_point
15580 && w->last_modified >= MODIFF
15581 && w->last_overlay_modified >= OVERLAY_MODIFF)
15582 && (XFASTINT (w->column_number_displayed) != current_column ()))
15583 update_mode_line = 1;
15584
15585 /* Count number of windows showing the selected buffer. An indirect
15586 buffer counts as its base buffer. */
15587 if (!just_this_one_p)
15588 {
15589 struct buffer *current_base, *window_base;
15590 current_base = current_buffer;
15591 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15592 if (current_base->base_buffer)
15593 current_base = current_base->base_buffer;
15594 if (window_base->base_buffer)
15595 window_base = window_base->base_buffer;
15596 if (current_base == window_base)
15597 buffer_shared++;
15598 }
15599
15600 /* Point refers normally to the selected window. For any other
15601 window, set up appropriate value. */
15602 if (!EQ (window, selected_window))
15603 {
15604 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15605 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15606 if (new_pt < BEGV)
15607 {
15608 new_pt = BEGV;
15609 new_pt_byte = BEGV_BYTE;
15610 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15611 }
15612 else if (new_pt > (ZV - 1))
15613 {
15614 new_pt = ZV;
15615 new_pt_byte = ZV_BYTE;
15616 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15617 }
15618
15619 /* We don't use SET_PT so that the point-motion hooks don't run. */
15620 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15621 }
15622
15623 /* If any of the character widths specified in the display table
15624 have changed, invalidate the width run cache. It's true that
15625 this may be a bit late to catch such changes, but the rest of
15626 redisplay goes (non-fatally) haywire when the display table is
15627 changed, so why should we worry about doing any better? */
15628 if (current_buffer->width_run_cache)
15629 {
15630 struct Lisp_Char_Table *disptab = buffer_display_table ();
15631
15632 if (! disptab_matches_widthtab
15633 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15634 {
15635 invalidate_region_cache (current_buffer,
15636 current_buffer->width_run_cache,
15637 BEG, Z);
15638 recompute_width_table (current_buffer, disptab);
15639 }
15640 }
15641
15642 /* If window-start is screwed up, choose a new one. */
15643 if (XMARKER (w->start)->buffer != current_buffer)
15644 goto recenter;
15645
15646 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15647
15648 /* If someone specified a new starting point but did not insist,
15649 check whether it can be used. */
15650 if (w->optional_new_start
15651 && CHARPOS (startp) >= BEGV
15652 && CHARPOS (startp) <= ZV)
15653 {
15654 w->optional_new_start = 0;
15655 start_display (&it, w, startp);
15656 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15657 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15658 if (IT_CHARPOS (it) == PT)
15659 w->force_start = 1;
15660 /* IT may overshoot PT if text at PT is invisible. */
15661 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15662 w->force_start = 1;
15663 }
15664
15665 force_start:
15666
15667 /* Handle case where place to start displaying has been specified,
15668 unless the specified location is outside the accessible range. */
15669 if (w->force_start || w->frozen_window_start_p)
15670 {
15671 /* We set this later on if we have to adjust point. */
15672 int new_vpos = -1;
15673
15674 w->force_start = 0;
15675 w->vscroll = 0;
15676 wset_window_end_valid (w, Qnil);
15677
15678 /* Forget any recorded base line for line number display. */
15679 if (!buffer_unchanged_p)
15680 wset_base_line_number (w, Qnil);
15681
15682 /* Redisplay the mode line. Select the buffer properly for that.
15683 Also, run the hook window-scroll-functions
15684 because we have scrolled. */
15685 /* Note, we do this after clearing force_start because
15686 if there's an error, it is better to forget about force_start
15687 than to get into an infinite loop calling the hook functions
15688 and having them get more errors. */
15689 if (!update_mode_line
15690 || ! NILP (Vwindow_scroll_functions))
15691 {
15692 update_mode_line = 1;
15693 w->update_mode_line = 1;
15694 startp = run_window_scroll_functions (window, startp);
15695 }
15696
15697 w->last_modified = 0;
15698 w->last_overlay_modified = 0;
15699 if (CHARPOS (startp) < BEGV)
15700 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15701 else if (CHARPOS (startp) > ZV)
15702 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15703
15704 /* Redisplay, then check if cursor has been set during the
15705 redisplay. Give up if new fonts were loaded. */
15706 /* We used to issue a CHECK_MARGINS argument to try_window here,
15707 but this causes scrolling to fail when point begins inside
15708 the scroll margin (bug#148) -- cyd */
15709 if (!try_window (window, startp, 0))
15710 {
15711 w->force_start = 1;
15712 clear_glyph_matrix (w->desired_matrix);
15713 goto need_larger_matrices;
15714 }
15715
15716 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15717 {
15718 /* If point does not appear, try to move point so it does
15719 appear. The desired matrix has been built above, so we
15720 can use it here. */
15721 new_vpos = window_box_height (w) / 2;
15722 }
15723
15724 if (!cursor_row_fully_visible_p (w, 0, 0))
15725 {
15726 /* Point does appear, but on a line partly visible at end of window.
15727 Move it back to a fully-visible line. */
15728 new_vpos = window_box_height (w);
15729 }
15730
15731 /* If we need to move point for either of the above reasons,
15732 now actually do it. */
15733 if (new_vpos >= 0)
15734 {
15735 struct glyph_row *row;
15736
15737 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15738 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15739 ++row;
15740
15741 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15742 MATRIX_ROW_START_BYTEPOS (row));
15743
15744 if (w != XWINDOW (selected_window))
15745 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15746 else if (current_buffer == old)
15747 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15748
15749 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15750
15751 /* If we are highlighting the region, then we just changed
15752 the region, so redisplay to show it. */
15753 if (!NILP (Vtransient_mark_mode)
15754 && !NILP (BVAR (current_buffer, mark_active)))
15755 {
15756 clear_glyph_matrix (w->desired_matrix);
15757 if (!try_window (window, startp, 0))
15758 goto need_larger_matrices;
15759 }
15760 }
15761
15762 #ifdef GLYPH_DEBUG
15763 debug_method_add (w, "forced window start");
15764 #endif
15765 goto done;
15766 }
15767
15768 /* Handle case where text has not changed, only point, and it has
15769 not moved off the frame, and we are not retrying after hscroll.
15770 (current_matrix_up_to_date_p is nonzero when retrying.) */
15771 if (current_matrix_up_to_date_p
15772 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15773 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15774 {
15775 switch (rc)
15776 {
15777 case CURSOR_MOVEMENT_SUCCESS:
15778 used_current_matrix_p = 1;
15779 goto done;
15780
15781 case CURSOR_MOVEMENT_MUST_SCROLL:
15782 goto try_to_scroll;
15783
15784 default:
15785 emacs_abort ();
15786 }
15787 }
15788 /* If current starting point was originally the beginning of a line
15789 but no longer is, find a new starting point. */
15790 else if (w->start_at_line_beg
15791 && !(CHARPOS (startp) <= BEGV
15792 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15793 {
15794 #ifdef GLYPH_DEBUG
15795 debug_method_add (w, "recenter 1");
15796 #endif
15797 goto recenter;
15798 }
15799
15800 /* Try scrolling with try_window_id. Value is > 0 if update has
15801 been done, it is -1 if we know that the same window start will
15802 not work. It is 0 if unsuccessful for some other reason. */
15803 else if ((tem = try_window_id (w)) != 0)
15804 {
15805 #ifdef GLYPH_DEBUG
15806 debug_method_add (w, "try_window_id %d", tem);
15807 #endif
15808
15809 if (fonts_changed_p)
15810 goto need_larger_matrices;
15811 if (tem > 0)
15812 goto done;
15813
15814 /* Otherwise try_window_id has returned -1 which means that we
15815 don't want the alternative below this comment to execute. */
15816 }
15817 else if (CHARPOS (startp) >= BEGV
15818 && CHARPOS (startp) <= ZV
15819 && PT >= CHARPOS (startp)
15820 && (CHARPOS (startp) < ZV
15821 /* Avoid starting at end of buffer. */
15822 || CHARPOS (startp) == BEGV
15823 || (w->last_modified >= MODIFF
15824 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15825 {
15826 int d1, d2, d3, d4, d5, d6;
15827
15828 /* If first window line is a continuation line, and window start
15829 is inside the modified region, but the first change is before
15830 current window start, we must select a new window start.
15831
15832 However, if this is the result of a down-mouse event (e.g. by
15833 extending the mouse-drag-overlay), we don't want to select a
15834 new window start, since that would change the position under
15835 the mouse, resulting in an unwanted mouse-movement rather
15836 than a simple mouse-click. */
15837 if (!w->start_at_line_beg
15838 && NILP (do_mouse_tracking)
15839 && CHARPOS (startp) > BEGV
15840 && CHARPOS (startp) > BEG + beg_unchanged
15841 && CHARPOS (startp) <= Z - end_unchanged
15842 /* Even if w->start_at_line_beg is nil, a new window may
15843 start at a line_beg, since that's how set_buffer_window
15844 sets it. So, we need to check the return value of
15845 compute_window_start_on_continuation_line. (See also
15846 bug#197). */
15847 && XMARKER (w->start)->buffer == current_buffer
15848 && compute_window_start_on_continuation_line (w)
15849 /* It doesn't make sense to force the window start like we
15850 do at label force_start if it is already known that point
15851 will not be visible in the resulting window, because
15852 doing so will move point from its correct position
15853 instead of scrolling the window to bring point into view.
15854 See bug#9324. */
15855 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15856 {
15857 w->force_start = 1;
15858 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15859 goto force_start;
15860 }
15861
15862 #ifdef GLYPH_DEBUG
15863 debug_method_add (w, "same window start");
15864 #endif
15865
15866 /* Try to redisplay starting at same place as before.
15867 If point has not moved off frame, accept the results. */
15868 if (!current_matrix_up_to_date_p
15869 /* Don't use try_window_reusing_current_matrix in this case
15870 because a window scroll function can have changed the
15871 buffer. */
15872 || !NILP (Vwindow_scroll_functions)
15873 || MINI_WINDOW_P (w)
15874 || !(used_current_matrix_p
15875 = try_window_reusing_current_matrix (w)))
15876 {
15877 IF_DEBUG (debug_method_add (w, "1"));
15878 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15879 /* -1 means we need to scroll.
15880 0 means we need new matrices, but fonts_changed_p
15881 is set in that case, so we will detect it below. */
15882 goto try_to_scroll;
15883 }
15884
15885 if (fonts_changed_p)
15886 goto need_larger_matrices;
15887
15888 if (w->cursor.vpos >= 0)
15889 {
15890 if (!just_this_one_p
15891 || current_buffer->clip_changed
15892 || BEG_UNCHANGED < CHARPOS (startp))
15893 /* Forget any recorded base line for line number display. */
15894 wset_base_line_number (w, Qnil);
15895
15896 if (!cursor_row_fully_visible_p (w, 1, 0))
15897 {
15898 clear_glyph_matrix (w->desired_matrix);
15899 last_line_misfit = 1;
15900 }
15901 /* Drop through and scroll. */
15902 else
15903 goto done;
15904 }
15905 else
15906 clear_glyph_matrix (w->desired_matrix);
15907 }
15908
15909 try_to_scroll:
15910
15911 w->last_modified = 0;
15912 w->last_overlay_modified = 0;
15913
15914 /* Redisplay the mode line. Select the buffer properly for that. */
15915 if (!update_mode_line)
15916 {
15917 update_mode_line = 1;
15918 w->update_mode_line = 1;
15919 }
15920
15921 /* Try to scroll by specified few lines. */
15922 if ((scroll_conservatively
15923 || emacs_scroll_step
15924 || temp_scroll_step
15925 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15926 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15927 && CHARPOS (startp) >= BEGV
15928 && CHARPOS (startp) <= ZV)
15929 {
15930 /* The function returns -1 if new fonts were loaded, 1 if
15931 successful, 0 if not successful. */
15932 int ss = try_scrolling (window, just_this_one_p,
15933 scroll_conservatively,
15934 emacs_scroll_step,
15935 temp_scroll_step, last_line_misfit);
15936 switch (ss)
15937 {
15938 case SCROLLING_SUCCESS:
15939 goto done;
15940
15941 case SCROLLING_NEED_LARGER_MATRICES:
15942 goto need_larger_matrices;
15943
15944 case SCROLLING_FAILED:
15945 break;
15946
15947 default:
15948 emacs_abort ();
15949 }
15950 }
15951
15952 /* Finally, just choose a place to start which positions point
15953 according to user preferences. */
15954
15955 recenter:
15956
15957 #ifdef GLYPH_DEBUG
15958 debug_method_add (w, "recenter");
15959 #endif
15960
15961 /* w->vscroll = 0; */
15962
15963 /* Forget any previously recorded base line for line number display. */
15964 if (!buffer_unchanged_p)
15965 wset_base_line_number (w, Qnil);
15966
15967 /* Determine the window start relative to point. */
15968 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15969 it.current_y = it.last_visible_y;
15970 if (centering_position < 0)
15971 {
15972 int margin =
15973 scroll_margin > 0
15974 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15975 : 0;
15976 ptrdiff_t margin_pos = CHARPOS (startp);
15977 Lisp_Object aggressive;
15978 int scrolling_up;
15979
15980 /* If there is a scroll margin at the top of the window, find
15981 its character position. */
15982 if (margin
15983 /* Cannot call start_display if startp is not in the
15984 accessible region of the buffer. This can happen when we
15985 have just switched to a different buffer and/or changed
15986 its restriction. In that case, startp is initialized to
15987 the character position 1 (BEGV) because we did not yet
15988 have chance to display the buffer even once. */
15989 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15990 {
15991 struct it it1;
15992 void *it1data = NULL;
15993
15994 SAVE_IT (it1, it, it1data);
15995 start_display (&it1, w, startp);
15996 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15997 margin_pos = IT_CHARPOS (it1);
15998 RESTORE_IT (&it, &it, it1data);
15999 }
16000 scrolling_up = PT > margin_pos;
16001 aggressive =
16002 scrolling_up
16003 ? BVAR (current_buffer, scroll_up_aggressively)
16004 : BVAR (current_buffer, scroll_down_aggressively);
16005
16006 if (!MINI_WINDOW_P (w)
16007 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16008 {
16009 int pt_offset = 0;
16010
16011 /* Setting scroll-conservatively overrides
16012 scroll-*-aggressively. */
16013 if (!scroll_conservatively && NUMBERP (aggressive))
16014 {
16015 double float_amount = XFLOATINT (aggressive);
16016
16017 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16018 if (pt_offset == 0 && float_amount > 0)
16019 pt_offset = 1;
16020 if (pt_offset && margin > 0)
16021 margin -= 1;
16022 }
16023 /* Compute how much to move the window start backward from
16024 point so that point will be displayed where the user
16025 wants it. */
16026 if (scrolling_up)
16027 {
16028 centering_position = it.last_visible_y;
16029 if (pt_offset)
16030 centering_position -= pt_offset;
16031 centering_position -=
16032 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16033 + WINDOW_HEADER_LINE_HEIGHT (w);
16034 /* Don't let point enter the scroll margin near top of
16035 the window. */
16036 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16037 centering_position = margin * FRAME_LINE_HEIGHT (f);
16038 }
16039 else
16040 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16041 }
16042 else
16043 /* Set the window start half the height of the window backward
16044 from point. */
16045 centering_position = window_box_height (w) / 2;
16046 }
16047 move_it_vertically_backward (&it, centering_position);
16048
16049 eassert (IT_CHARPOS (it) >= BEGV);
16050
16051 /* The function move_it_vertically_backward may move over more
16052 than the specified y-distance. If it->w is small, e.g. a
16053 mini-buffer window, we may end up in front of the window's
16054 display area. Start displaying at the start of the line
16055 containing PT in this case. */
16056 if (it.current_y <= 0)
16057 {
16058 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16059 move_it_vertically_backward (&it, 0);
16060 it.current_y = 0;
16061 }
16062
16063 it.current_x = it.hpos = 0;
16064
16065 /* Set the window start position here explicitly, to avoid an
16066 infinite loop in case the functions in window-scroll-functions
16067 get errors. */
16068 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16069
16070 /* Run scroll hooks. */
16071 startp = run_window_scroll_functions (window, it.current.pos);
16072
16073 /* Redisplay the window. */
16074 if (!current_matrix_up_to_date_p
16075 || windows_or_buffers_changed
16076 || cursor_type_changed
16077 /* Don't use try_window_reusing_current_matrix in this case
16078 because it can have changed the buffer. */
16079 || !NILP (Vwindow_scroll_functions)
16080 || !just_this_one_p
16081 || MINI_WINDOW_P (w)
16082 || !(used_current_matrix_p
16083 = try_window_reusing_current_matrix (w)))
16084 try_window (window, startp, 0);
16085
16086 /* If new fonts have been loaded (due to fontsets), give up. We
16087 have to start a new redisplay since we need to re-adjust glyph
16088 matrices. */
16089 if (fonts_changed_p)
16090 goto need_larger_matrices;
16091
16092 /* If cursor did not appear assume that the middle of the window is
16093 in the first line of the window. Do it again with the next line.
16094 (Imagine a window of height 100, displaying two lines of height
16095 60. Moving back 50 from it->last_visible_y will end in the first
16096 line.) */
16097 if (w->cursor.vpos < 0)
16098 {
16099 if (!NILP (w->window_end_valid)
16100 && PT >= Z - XFASTINT (w->window_end_pos))
16101 {
16102 clear_glyph_matrix (w->desired_matrix);
16103 move_it_by_lines (&it, 1);
16104 try_window (window, it.current.pos, 0);
16105 }
16106 else if (PT < IT_CHARPOS (it))
16107 {
16108 clear_glyph_matrix (w->desired_matrix);
16109 move_it_by_lines (&it, -1);
16110 try_window (window, it.current.pos, 0);
16111 }
16112 else
16113 {
16114 /* Not much we can do about it. */
16115 }
16116 }
16117
16118 /* Consider the following case: Window starts at BEGV, there is
16119 invisible, intangible text at BEGV, so that display starts at
16120 some point START > BEGV. It can happen that we are called with
16121 PT somewhere between BEGV and START. Try to handle that case. */
16122 if (w->cursor.vpos < 0)
16123 {
16124 struct glyph_row *row = w->current_matrix->rows;
16125 if (row->mode_line_p)
16126 ++row;
16127 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16128 }
16129
16130 if (!cursor_row_fully_visible_p (w, 0, 0))
16131 {
16132 /* If vscroll is enabled, disable it and try again. */
16133 if (w->vscroll)
16134 {
16135 w->vscroll = 0;
16136 clear_glyph_matrix (w->desired_matrix);
16137 goto recenter;
16138 }
16139
16140 /* Users who set scroll-conservatively to a large number want
16141 point just above/below the scroll margin. If we ended up
16142 with point's row partially visible, move the window start to
16143 make that row fully visible and out of the margin. */
16144 if (scroll_conservatively > SCROLL_LIMIT)
16145 {
16146 int margin =
16147 scroll_margin > 0
16148 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16149 : 0;
16150 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16151
16152 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16153 clear_glyph_matrix (w->desired_matrix);
16154 if (1 == try_window (window, it.current.pos,
16155 TRY_WINDOW_CHECK_MARGINS))
16156 goto done;
16157 }
16158
16159 /* If centering point failed to make the whole line visible,
16160 put point at the top instead. That has to make the whole line
16161 visible, if it can be done. */
16162 if (centering_position == 0)
16163 goto done;
16164
16165 clear_glyph_matrix (w->desired_matrix);
16166 centering_position = 0;
16167 goto recenter;
16168 }
16169
16170 done:
16171
16172 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16173 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16174 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16175
16176 /* Display the mode line, if we must. */
16177 if ((update_mode_line
16178 /* If window not full width, must redo its mode line
16179 if (a) the window to its side is being redone and
16180 (b) we do a frame-based redisplay. This is a consequence
16181 of how inverted lines are drawn in frame-based redisplay. */
16182 || (!just_this_one_p
16183 && !FRAME_WINDOW_P (f)
16184 && !WINDOW_FULL_WIDTH_P (w))
16185 /* Line number to display. */
16186 || INTEGERP (w->base_line_pos)
16187 /* Column number is displayed and different from the one displayed. */
16188 || (!NILP (w->column_number_displayed)
16189 && (XFASTINT (w->column_number_displayed) != current_column ())))
16190 /* This means that the window has a mode line. */
16191 && (WINDOW_WANTS_MODELINE_P (w)
16192 || WINDOW_WANTS_HEADER_LINE_P (w)))
16193 {
16194 display_mode_lines (w);
16195
16196 /* If mode line height has changed, arrange for a thorough
16197 immediate redisplay using the correct mode line height. */
16198 if (WINDOW_WANTS_MODELINE_P (w)
16199 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16200 {
16201 fonts_changed_p = 1;
16202 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16203 = DESIRED_MODE_LINE_HEIGHT (w);
16204 }
16205
16206 /* If header line height has changed, arrange for a thorough
16207 immediate redisplay using the correct header line height. */
16208 if (WINDOW_WANTS_HEADER_LINE_P (w)
16209 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16210 {
16211 fonts_changed_p = 1;
16212 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16213 = DESIRED_HEADER_LINE_HEIGHT (w);
16214 }
16215
16216 if (fonts_changed_p)
16217 goto need_larger_matrices;
16218 }
16219
16220 if (!line_number_displayed
16221 && !BUFFERP (w->base_line_pos))
16222 {
16223 wset_base_line_pos (w, Qnil);
16224 wset_base_line_number (w, Qnil);
16225 }
16226
16227 finish_menu_bars:
16228
16229 /* When we reach a frame's selected window, redo the frame's menu bar. */
16230 if (update_mode_line
16231 && EQ (FRAME_SELECTED_WINDOW (f), window))
16232 {
16233 int redisplay_menu_p = 0;
16234
16235 if (FRAME_WINDOW_P (f))
16236 {
16237 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16238 || defined (HAVE_NS) || defined (USE_GTK)
16239 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16240 #else
16241 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16242 #endif
16243 }
16244 else
16245 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16246
16247 if (redisplay_menu_p)
16248 display_menu_bar (w);
16249
16250 #ifdef HAVE_WINDOW_SYSTEM
16251 if (FRAME_WINDOW_P (f))
16252 {
16253 #if defined (USE_GTK) || defined (HAVE_NS)
16254 if (FRAME_EXTERNAL_TOOL_BAR (f))
16255 redisplay_tool_bar (f);
16256 #else
16257 if (WINDOWP (f->tool_bar_window)
16258 && (FRAME_TOOL_BAR_LINES (f) > 0
16259 || !NILP (Vauto_resize_tool_bars))
16260 && redisplay_tool_bar (f))
16261 ignore_mouse_drag_p = 1;
16262 #endif
16263 }
16264 #endif
16265 }
16266
16267 #ifdef HAVE_WINDOW_SYSTEM
16268 if (FRAME_WINDOW_P (f)
16269 && update_window_fringes (w, (just_this_one_p
16270 || (!used_current_matrix_p && !overlay_arrow_seen)
16271 || w->pseudo_window_p)))
16272 {
16273 update_begin (f);
16274 block_input ();
16275 if (draw_window_fringes (w, 1))
16276 x_draw_vertical_border (w);
16277 unblock_input ();
16278 update_end (f);
16279 }
16280 #endif /* HAVE_WINDOW_SYSTEM */
16281
16282 /* We go to this label, with fonts_changed_p set,
16283 if it is necessary to try again using larger glyph matrices.
16284 We have to redeem the scroll bar even in this case,
16285 because the loop in redisplay_internal expects that. */
16286 need_larger_matrices:
16287 ;
16288 finish_scroll_bars:
16289
16290 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16291 {
16292 /* Set the thumb's position and size. */
16293 set_vertical_scroll_bar (w);
16294
16295 /* Note that we actually used the scroll bar attached to this
16296 window, so it shouldn't be deleted at the end of redisplay. */
16297 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16298 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16299 }
16300
16301 /* Restore current_buffer and value of point in it. The window
16302 update may have changed the buffer, so first make sure `opoint'
16303 is still valid (Bug#6177). */
16304 if (CHARPOS (opoint) < BEGV)
16305 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16306 else if (CHARPOS (opoint) > ZV)
16307 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16308 else
16309 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16310
16311 set_buffer_internal_1 (old);
16312 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16313 shorter. This can be caused by log truncation in *Messages*. */
16314 if (CHARPOS (lpoint) <= ZV)
16315 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16316
16317 unbind_to (count, Qnil);
16318 }
16319
16320
16321 /* Build the complete desired matrix of WINDOW with a window start
16322 buffer position POS.
16323
16324 Value is 1 if successful. It is zero if fonts were loaded during
16325 redisplay which makes re-adjusting glyph matrices necessary, and -1
16326 if point would appear in the scroll margins.
16327 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16328 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16329 set in FLAGS.) */
16330
16331 int
16332 try_window (Lisp_Object window, struct text_pos pos, int flags)
16333 {
16334 struct window *w = XWINDOW (window);
16335 struct it it;
16336 struct glyph_row *last_text_row = NULL;
16337 struct frame *f = XFRAME (w->frame);
16338
16339 /* Make POS the new window start. */
16340 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16341
16342 /* Mark cursor position as unknown. No overlay arrow seen. */
16343 w->cursor.vpos = -1;
16344 overlay_arrow_seen = 0;
16345
16346 /* Initialize iterator and info to start at POS. */
16347 start_display (&it, w, pos);
16348
16349 /* Display all lines of W. */
16350 while (it.current_y < it.last_visible_y)
16351 {
16352 if (display_line (&it))
16353 last_text_row = it.glyph_row - 1;
16354 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16355 return 0;
16356 }
16357
16358 /* Don't let the cursor end in the scroll margins. */
16359 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16360 && !MINI_WINDOW_P (w))
16361 {
16362 int this_scroll_margin;
16363
16364 if (scroll_margin > 0)
16365 {
16366 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16367 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16368 }
16369 else
16370 this_scroll_margin = 0;
16371
16372 if ((w->cursor.y >= 0 /* not vscrolled */
16373 && w->cursor.y < this_scroll_margin
16374 && CHARPOS (pos) > BEGV
16375 && IT_CHARPOS (it) < ZV)
16376 /* rms: considering make_cursor_line_fully_visible_p here
16377 seems to give wrong results. We don't want to recenter
16378 when the last line is partly visible, we want to allow
16379 that case to be handled in the usual way. */
16380 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16381 {
16382 w->cursor.vpos = -1;
16383 clear_glyph_matrix (w->desired_matrix);
16384 return -1;
16385 }
16386 }
16387
16388 /* If bottom moved off end of frame, change mode line percentage. */
16389 if (XFASTINT (w->window_end_pos) <= 0
16390 && Z != IT_CHARPOS (it))
16391 w->update_mode_line = 1;
16392
16393 /* Set window_end_pos to the offset of the last character displayed
16394 on the window from the end of current_buffer. Set
16395 window_end_vpos to its row number. */
16396 if (last_text_row)
16397 {
16398 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16399 w->window_end_bytepos
16400 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16401 wset_window_end_pos
16402 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16403 wset_window_end_vpos
16404 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16405 eassert
16406 (MATRIX_ROW (w->desired_matrix,
16407 XFASTINT (w->window_end_vpos))->displays_text_p);
16408 }
16409 else
16410 {
16411 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16412 wset_window_end_pos (w, make_number (Z - ZV));
16413 wset_window_end_vpos (w, make_number (0));
16414 }
16415
16416 /* But that is not valid info until redisplay finishes. */
16417 wset_window_end_valid (w, Qnil);
16418 return 1;
16419 }
16420
16421
16422 \f
16423 /************************************************************************
16424 Window redisplay reusing current matrix when buffer has not changed
16425 ************************************************************************/
16426
16427 /* Try redisplay of window W showing an unchanged buffer with a
16428 different window start than the last time it was displayed by
16429 reusing its current matrix. Value is non-zero if successful.
16430 W->start is the new window start. */
16431
16432 static int
16433 try_window_reusing_current_matrix (struct window *w)
16434 {
16435 struct frame *f = XFRAME (w->frame);
16436 struct glyph_row *bottom_row;
16437 struct it it;
16438 struct run run;
16439 struct text_pos start, new_start;
16440 int nrows_scrolled, i;
16441 struct glyph_row *last_text_row;
16442 struct glyph_row *last_reused_text_row;
16443 struct glyph_row *start_row;
16444 int start_vpos, min_y, max_y;
16445
16446 #ifdef GLYPH_DEBUG
16447 if (inhibit_try_window_reusing)
16448 return 0;
16449 #endif
16450
16451 if (/* This function doesn't handle terminal frames. */
16452 !FRAME_WINDOW_P (f)
16453 /* Don't try to reuse the display if windows have been split
16454 or such. */
16455 || windows_or_buffers_changed
16456 || cursor_type_changed)
16457 return 0;
16458
16459 /* Can't do this if region may have changed. */
16460 if ((!NILP (Vtransient_mark_mode)
16461 && !NILP (BVAR (current_buffer, mark_active)))
16462 || !NILP (w->region_showing)
16463 || !NILP (Vshow_trailing_whitespace))
16464 return 0;
16465
16466 /* If top-line visibility has changed, give up. */
16467 if (WINDOW_WANTS_HEADER_LINE_P (w)
16468 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16469 return 0;
16470
16471 /* Give up if old or new display is scrolled vertically. We could
16472 make this function handle this, but right now it doesn't. */
16473 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16474 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16475 return 0;
16476
16477 /* The variable new_start now holds the new window start. The old
16478 start `start' can be determined from the current matrix. */
16479 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16480 start = start_row->minpos;
16481 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16482
16483 /* Clear the desired matrix for the display below. */
16484 clear_glyph_matrix (w->desired_matrix);
16485
16486 if (CHARPOS (new_start) <= CHARPOS (start))
16487 {
16488 /* Don't use this method if the display starts with an ellipsis
16489 displayed for invisible text. It's not easy to handle that case
16490 below, and it's certainly not worth the effort since this is
16491 not a frequent case. */
16492 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16493 return 0;
16494
16495 IF_DEBUG (debug_method_add (w, "twu1"));
16496
16497 /* Display up to a row that can be reused. The variable
16498 last_text_row is set to the last row displayed that displays
16499 text. Note that it.vpos == 0 if or if not there is a
16500 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16501 start_display (&it, w, new_start);
16502 w->cursor.vpos = -1;
16503 last_text_row = last_reused_text_row = NULL;
16504
16505 while (it.current_y < it.last_visible_y
16506 && !fonts_changed_p)
16507 {
16508 /* If we have reached into the characters in the START row,
16509 that means the line boundaries have changed. So we
16510 can't start copying with the row START. Maybe it will
16511 work to start copying with the following row. */
16512 while (IT_CHARPOS (it) > CHARPOS (start))
16513 {
16514 /* Advance to the next row as the "start". */
16515 start_row++;
16516 start = start_row->minpos;
16517 /* If there are no more rows to try, or just one, give up. */
16518 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16519 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16520 || CHARPOS (start) == ZV)
16521 {
16522 clear_glyph_matrix (w->desired_matrix);
16523 return 0;
16524 }
16525
16526 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16527 }
16528 /* If we have reached alignment, we can copy the rest of the
16529 rows. */
16530 if (IT_CHARPOS (it) == CHARPOS (start)
16531 /* Don't accept "alignment" inside a display vector,
16532 since start_row could have started in the middle of
16533 that same display vector (thus their character
16534 positions match), and we have no way of telling if
16535 that is the case. */
16536 && it.current.dpvec_index < 0)
16537 break;
16538
16539 if (display_line (&it))
16540 last_text_row = it.glyph_row - 1;
16541
16542 }
16543
16544 /* A value of current_y < last_visible_y means that we stopped
16545 at the previous window start, which in turn means that we
16546 have at least one reusable row. */
16547 if (it.current_y < it.last_visible_y)
16548 {
16549 struct glyph_row *row;
16550
16551 /* IT.vpos always starts from 0; it counts text lines. */
16552 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16553
16554 /* Find PT if not already found in the lines displayed. */
16555 if (w->cursor.vpos < 0)
16556 {
16557 int dy = it.current_y - start_row->y;
16558
16559 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16560 row = row_containing_pos (w, PT, row, NULL, dy);
16561 if (row)
16562 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16563 dy, nrows_scrolled);
16564 else
16565 {
16566 clear_glyph_matrix (w->desired_matrix);
16567 return 0;
16568 }
16569 }
16570
16571 /* Scroll the display. Do it before the current matrix is
16572 changed. The problem here is that update has not yet
16573 run, i.e. part of the current matrix is not up to date.
16574 scroll_run_hook will clear the cursor, and use the
16575 current matrix to get the height of the row the cursor is
16576 in. */
16577 run.current_y = start_row->y;
16578 run.desired_y = it.current_y;
16579 run.height = it.last_visible_y - it.current_y;
16580
16581 if (run.height > 0 && run.current_y != run.desired_y)
16582 {
16583 update_begin (f);
16584 FRAME_RIF (f)->update_window_begin_hook (w);
16585 FRAME_RIF (f)->clear_window_mouse_face (w);
16586 FRAME_RIF (f)->scroll_run_hook (w, &run);
16587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16588 update_end (f);
16589 }
16590
16591 /* Shift current matrix down by nrows_scrolled lines. */
16592 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16593 rotate_matrix (w->current_matrix,
16594 start_vpos,
16595 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16596 nrows_scrolled);
16597
16598 /* Disable lines that must be updated. */
16599 for (i = 0; i < nrows_scrolled; ++i)
16600 (start_row + i)->enabled_p = 0;
16601
16602 /* Re-compute Y positions. */
16603 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16604 max_y = it.last_visible_y;
16605 for (row = start_row + nrows_scrolled;
16606 row < bottom_row;
16607 ++row)
16608 {
16609 row->y = it.current_y;
16610 row->visible_height = row->height;
16611
16612 if (row->y < min_y)
16613 row->visible_height -= min_y - row->y;
16614 if (row->y + row->height > max_y)
16615 row->visible_height -= row->y + row->height - max_y;
16616 if (row->fringe_bitmap_periodic_p)
16617 row->redraw_fringe_bitmaps_p = 1;
16618
16619 it.current_y += row->height;
16620
16621 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16622 last_reused_text_row = row;
16623 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16624 break;
16625 }
16626
16627 /* Disable lines in the current matrix which are now
16628 below the window. */
16629 for (++row; row < bottom_row; ++row)
16630 row->enabled_p = row->mode_line_p = 0;
16631 }
16632
16633 /* Update window_end_pos etc.; last_reused_text_row is the last
16634 reused row from the current matrix containing text, if any.
16635 The value of last_text_row is the last displayed line
16636 containing text. */
16637 if (last_reused_text_row)
16638 {
16639 w->window_end_bytepos
16640 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16641 wset_window_end_pos
16642 (w, make_number (Z
16643 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16644 wset_window_end_vpos
16645 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16646 w->current_matrix)));
16647 }
16648 else if (last_text_row)
16649 {
16650 w->window_end_bytepos
16651 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16652 wset_window_end_pos
16653 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16654 wset_window_end_vpos
16655 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16656 w->desired_matrix)));
16657 }
16658 else
16659 {
16660 /* This window must be completely empty. */
16661 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16662 wset_window_end_pos (w, make_number (Z - ZV));
16663 wset_window_end_vpos (w, make_number (0));
16664 }
16665 wset_window_end_valid (w, Qnil);
16666
16667 /* Update hint: don't try scrolling again in update_window. */
16668 w->desired_matrix->no_scrolling_p = 1;
16669
16670 #ifdef GLYPH_DEBUG
16671 debug_method_add (w, "try_window_reusing_current_matrix 1");
16672 #endif
16673 return 1;
16674 }
16675 else if (CHARPOS (new_start) > CHARPOS (start))
16676 {
16677 struct glyph_row *pt_row, *row;
16678 struct glyph_row *first_reusable_row;
16679 struct glyph_row *first_row_to_display;
16680 int dy;
16681 int yb = window_text_bottom_y (w);
16682
16683 /* Find the row starting at new_start, if there is one. Don't
16684 reuse a partially visible line at the end. */
16685 first_reusable_row = start_row;
16686 while (first_reusable_row->enabled_p
16687 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16688 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16689 < CHARPOS (new_start)))
16690 ++first_reusable_row;
16691
16692 /* Give up if there is no row to reuse. */
16693 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16694 || !first_reusable_row->enabled_p
16695 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16696 != CHARPOS (new_start)))
16697 return 0;
16698
16699 /* We can reuse fully visible rows beginning with
16700 first_reusable_row to the end of the window. Set
16701 first_row_to_display to the first row that cannot be reused.
16702 Set pt_row to the row containing point, if there is any. */
16703 pt_row = NULL;
16704 for (first_row_to_display = first_reusable_row;
16705 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16706 ++first_row_to_display)
16707 {
16708 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16709 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16710 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16711 && first_row_to_display->ends_at_zv_p
16712 && pt_row == NULL)))
16713 pt_row = first_row_to_display;
16714 }
16715
16716 /* Start displaying at the start of first_row_to_display. */
16717 eassert (first_row_to_display->y < yb);
16718 init_to_row_start (&it, w, first_row_to_display);
16719
16720 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16721 - start_vpos);
16722 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16723 - nrows_scrolled);
16724 it.current_y = (first_row_to_display->y - first_reusable_row->y
16725 + WINDOW_HEADER_LINE_HEIGHT (w));
16726
16727 /* Display lines beginning with first_row_to_display in the
16728 desired matrix. Set last_text_row to the last row displayed
16729 that displays text. */
16730 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16731 if (pt_row == NULL)
16732 w->cursor.vpos = -1;
16733 last_text_row = NULL;
16734 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16735 if (display_line (&it))
16736 last_text_row = it.glyph_row - 1;
16737
16738 /* If point is in a reused row, adjust y and vpos of the cursor
16739 position. */
16740 if (pt_row)
16741 {
16742 w->cursor.vpos -= nrows_scrolled;
16743 w->cursor.y -= first_reusable_row->y - start_row->y;
16744 }
16745
16746 /* Give up if point isn't in a row displayed or reused. (This
16747 also handles the case where w->cursor.vpos < nrows_scrolled
16748 after the calls to display_line, which can happen with scroll
16749 margins. See bug#1295.) */
16750 if (w->cursor.vpos < 0)
16751 {
16752 clear_glyph_matrix (w->desired_matrix);
16753 return 0;
16754 }
16755
16756 /* Scroll the display. */
16757 run.current_y = first_reusable_row->y;
16758 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16759 run.height = it.last_visible_y - run.current_y;
16760 dy = run.current_y - run.desired_y;
16761
16762 if (run.height)
16763 {
16764 update_begin (f);
16765 FRAME_RIF (f)->update_window_begin_hook (w);
16766 FRAME_RIF (f)->clear_window_mouse_face (w);
16767 FRAME_RIF (f)->scroll_run_hook (w, &run);
16768 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16769 update_end (f);
16770 }
16771
16772 /* Adjust Y positions of reused rows. */
16773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16774 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16775 max_y = it.last_visible_y;
16776 for (row = first_reusable_row; row < first_row_to_display; ++row)
16777 {
16778 row->y -= dy;
16779 row->visible_height = row->height;
16780 if (row->y < min_y)
16781 row->visible_height -= min_y - row->y;
16782 if (row->y + row->height > max_y)
16783 row->visible_height -= row->y + row->height - max_y;
16784 if (row->fringe_bitmap_periodic_p)
16785 row->redraw_fringe_bitmaps_p = 1;
16786 }
16787
16788 /* Scroll the current matrix. */
16789 eassert (nrows_scrolled > 0);
16790 rotate_matrix (w->current_matrix,
16791 start_vpos,
16792 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16793 -nrows_scrolled);
16794
16795 /* Disable rows not reused. */
16796 for (row -= nrows_scrolled; row < bottom_row; ++row)
16797 row->enabled_p = 0;
16798
16799 /* Point may have moved to a different line, so we cannot assume that
16800 the previous cursor position is valid; locate the correct row. */
16801 if (pt_row)
16802 {
16803 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16804 row < bottom_row
16805 && PT >= MATRIX_ROW_END_CHARPOS (row)
16806 && !row->ends_at_zv_p;
16807 row++)
16808 {
16809 w->cursor.vpos++;
16810 w->cursor.y = row->y;
16811 }
16812 if (row < bottom_row)
16813 {
16814 /* Can't simply scan the row for point with
16815 bidi-reordered glyph rows. Let set_cursor_from_row
16816 figure out where to put the cursor, and if it fails,
16817 give up. */
16818 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16819 {
16820 if (!set_cursor_from_row (w, row, w->current_matrix,
16821 0, 0, 0, 0))
16822 {
16823 clear_glyph_matrix (w->desired_matrix);
16824 return 0;
16825 }
16826 }
16827 else
16828 {
16829 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16830 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16831
16832 for (; glyph < end
16833 && (!BUFFERP (glyph->object)
16834 || glyph->charpos < PT);
16835 glyph++)
16836 {
16837 w->cursor.hpos++;
16838 w->cursor.x += glyph->pixel_width;
16839 }
16840 }
16841 }
16842 }
16843
16844 /* Adjust window end. A null value of last_text_row means that
16845 the window end is in reused rows which in turn means that
16846 only its vpos can have changed. */
16847 if (last_text_row)
16848 {
16849 w->window_end_bytepos
16850 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16851 wset_window_end_pos
16852 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16853 wset_window_end_vpos
16854 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16855 w->desired_matrix)));
16856 }
16857 else
16858 {
16859 wset_window_end_vpos
16860 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16861 }
16862
16863 wset_window_end_valid (w, Qnil);
16864 w->desired_matrix->no_scrolling_p = 1;
16865
16866 #ifdef GLYPH_DEBUG
16867 debug_method_add (w, "try_window_reusing_current_matrix 2");
16868 #endif
16869 return 1;
16870 }
16871
16872 return 0;
16873 }
16874
16875
16876 \f
16877 /************************************************************************
16878 Window redisplay reusing current matrix when buffer has changed
16879 ************************************************************************/
16880
16881 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16882 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16883 ptrdiff_t *, ptrdiff_t *);
16884 static struct glyph_row *
16885 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16886 struct glyph_row *);
16887
16888
16889 /* Return the last row in MATRIX displaying text. If row START is
16890 non-null, start searching with that row. IT gives the dimensions
16891 of the display. Value is null if matrix is empty; otherwise it is
16892 a pointer to the row found. */
16893
16894 static struct glyph_row *
16895 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16896 struct glyph_row *start)
16897 {
16898 struct glyph_row *row, *row_found;
16899
16900 /* Set row_found to the last row in IT->w's current matrix
16901 displaying text. The loop looks funny but think of partially
16902 visible lines. */
16903 row_found = NULL;
16904 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16905 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16906 {
16907 eassert (row->enabled_p);
16908 row_found = row;
16909 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16910 break;
16911 ++row;
16912 }
16913
16914 return row_found;
16915 }
16916
16917
16918 /* Return the last row in the current matrix of W that is not affected
16919 by changes at the start of current_buffer that occurred since W's
16920 current matrix was built. Value is null if no such row exists.
16921
16922 BEG_UNCHANGED us the number of characters unchanged at the start of
16923 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16924 first changed character in current_buffer. Characters at positions <
16925 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16926 when the current matrix was built. */
16927
16928 static struct glyph_row *
16929 find_last_unchanged_at_beg_row (struct window *w)
16930 {
16931 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16932 struct glyph_row *row;
16933 struct glyph_row *row_found = NULL;
16934 int yb = window_text_bottom_y (w);
16935
16936 /* Find the last row displaying unchanged text. */
16937 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16938 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16939 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16940 ++row)
16941 {
16942 if (/* If row ends before first_changed_pos, it is unchanged,
16943 except in some case. */
16944 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16945 /* When row ends in ZV and we write at ZV it is not
16946 unchanged. */
16947 && !row->ends_at_zv_p
16948 /* When first_changed_pos is the end of a continued line,
16949 row is not unchanged because it may be no longer
16950 continued. */
16951 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16952 && (row->continued_p
16953 || row->exact_window_width_line_p))
16954 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16955 needs to be recomputed, so don't consider this row as
16956 unchanged. This happens when the last line was
16957 bidi-reordered and was killed immediately before this
16958 redisplay cycle. In that case, ROW->end stores the
16959 buffer position of the first visual-order character of
16960 the killed text, which is now beyond ZV. */
16961 && CHARPOS (row->end.pos) <= ZV)
16962 row_found = row;
16963
16964 /* Stop if last visible row. */
16965 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16966 break;
16967 }
16968
16969 return row_found;
16970 }
16971
16972
16973 /* Find the first glyph row in the current matrix of W that is not
16974 affected by changes at the end of current_buffer since the
16975 time W's current matrix was built.
16976
16977 Return in *DELTA the number of chars by which buffer positions in
16978 unchanged text at the end of current_buffer must be adjusted.
16979
16980 Return in *DELTA_BYTES the corresponding number of bytes.
16981
16982 Value is null if no such row exists, i.e. all rows are affected by
16983 changes. */
16984
16985 static struct glyph_row *
16986 find_first_unchanged_at_end_row (struct window *w,
16987 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16988 {
16989 struct glyph_row *row;
16990 struct glyph_row *row_found = NULL;
16991
16992 *delta = *delta_bytes = 0;
16993
16994 /* Display must not have been paused, otherwise the current matrix
16995 is not up to date. */
16996 eassert (!NILP (w->window_end_valid));
16997
16998 /* A value of window_end_pos >= END_UNCHANGED means that the window
16999 end is in the range of changed text. If so, there is no
17000 unchanged row at the end of W's current matrix. */
17001 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
17002 return NULL;
17003
17004 /* Set row to the last row in W's current matrix displaying text. */
17005 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17006
17007 /* If matrix is entirely empty, no unchanged row exists. */
17008 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17009 {
17010 /* The value of row is the last glyph row in the matrix having a
17011 meaningful buffer position in it. The end position of row
17012 corresponds to window_end_pos. This allows us to translate
17013 buffer positions in the current matrix to current buffer
17014 positions for characters not in changed text. */
17015 ptrdiff_t Z_old =
17016 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17017 ptrdiff_t Z_BYTE_old =
17018 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17019 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17020 struct glyph_row *first_text_row
17021 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17022
17023 *delta = Z - Z_old;
17024 *delta_bytes = Z_BYTE - Z_BYTE_old;
17025
17026 /* Set last_unchanged_pos to the buffer position of the last
17027 character in the buffer that has not been changed. Z is the
17028 index + 1 of the last character in current_buffer, i.e. by
17029 subtracting END_UNCHANGED we get the index of the last
17030 unchanged character, and we have to add BEG to get its buffer
17031 position. */
17032 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17033 last_unchanged_pos_old = last_unchanged_pos - *delta;
17034
17035 /* Search backward from ROW for a row displaying a line that
17036 starts at a minimum position >= last_unchanged_pos_old. */
17037 for (; row > first_text_row; --row)
17038 {
17039 /* This used to abort, but it can happen.
17040 It is ok to just stop the search instead here. KFS. */
17041 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17042 break;
17043
17044 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17045 row_found = row;
17046 }
17047 }
17048
17049 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17050
17051 return row_found;
17052 }
17053
17054
17055 /* Make sure that glyph rows in the current matrix of window W
17056 reference the same glyph memory as corresponding rows in the
17057 frame's frame matrix. This function is called after scrolling W's
17058 current matrix on a terminal frame in try_window_id and
17059 try_window_reusing_current_matrix. */
17060
17061 static void
17062 sync_frame_with_window_matrix_rows (struct window *w)
17063 {
17064 struct frame *f = XFRAME (w->frame);
17065 struct glyph_row *window_row, *window_row_end, *frame_row;
17066
17067 /* Preconditions: W must be a leaf window and full-width. Its frame
17068 must have a frame matrix. */
17069 eassert (NILP (w->hchild) && NILP (w->vchild));
17070 eassert (WINDOW_FULL_WIDTH_P (w));
17071 eassert (!FRAME_WINDOW_P (f));
17072
17073 /* If W is a full-width window, glyph pointers in W's current matrix
17074 have, by definition, to be the same as glyph pointers in the
17075 corresponding frame matrix. Note that frame matrices have no
17076 marginal areas (see build_frame_matrix). */
17077 window_row = w->current_matrix->rows;
17078 window_row_end = window_row + w->current_matrix->nrows;
17079 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17080 while (window_row < window_row_end)
17081 {
17082 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17083 struct glyph *end = window_row->glyphs[LAST_AREA];
17084
17085 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17086 frame_row->glyphs[TEXT_AREA] = start;
17087 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17088 frame_row->glyphs[LAST_AREA] = end;
17089
17090 /* Disable frame rows whose corresponding window rows have
17091 been disabled in try_window_id. */
17092 if (!window_row->enabled_p)
17093 frame_row->enabled_p = 0;
17094
17095 ++window_row, ++frame_row;
17096 }
17097 }
17098
17099
17100 /* Find the glyph row in window W containing CHARPOS. Consider all
17101 rows between START and END (not inclusive). END null means search
17102 all rows to the end of the display area of W. Value is the row
17103 containing CHARPOS or null. */
17104
17105 struct glyph_row *
17106 row_containing_pos (struct window *w, ptrdiff_t charpos,
17107 struct glyph_row *start, struct glyph_row *end, int dy)
17108 {
17109 struct glyph_row *row = start;
17110 struct glyph_row *best_row = NULL;
17111 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17112 int last_y;
17113
17114 /* If we happen to start on a header-line, skip that. */
17115 if (row->mode_line_p)
17116 ++row;
17117
17118 if ((end && row >= end) || !row->enabled_p)
17119 return NULL;
17120
17121 last_y = window_text_bottom_y (w) - dy;
17122
17123 while (1)
17124 {
17125 /* Give up if we have gone too far. */
17126 if (end && row >= end)
17127 return NULL;
17128 /* This formerly returned if they were equal.
17129 I think that both quantities are of a "last plus one" type;
17130 if so, when they are equal, the row is within the screen. -- rms. */
17131 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17132 return NULL;
17133
17134 /* If it is in this row, return this row. */
17135 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17136 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17137 /* The end position of a row equals the start
17138 position of the next row. If CHARPOS is there, we
17139 would rather display it in the next line, except
17140 when this line ends in ZV. */
17141 && !row->ends_at_zv_p
17142 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17143 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17144 {
17145 struct glyph *g;
17146
17147 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17148 || (!best_row && !row->continued_p))
17149 return row;
17150 /* In bidi-reordered rows, there could be several rows
17151 occluding point, all of them belonging to the same
17152 continued line. We need to find the row which fits
17153 CHARPOS the best. */
17154 for (g = row->glyphs[TEXT_AREA];
17155 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17156 g++)
17157 {
17158 if (!STRINGP (g->object))
17159 {
17160 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17161 {
17162 mindif = eabs (g->charpos - charpos);
17163 best_row = row;
17164 /* Exact match always wins. */
17165 if (mindif == 0)
17166 return best_row;
17167 }
17168 }
17169 }
17170 }
17171 else if (best_row && !row->continued_p)
17172 return best_row;
17173 ++row;
17174 }
17175 }
17176
17177
17178 /* Try to redisplay window W by reusing its existing display. W's
17179 current matrix must be up to date when this function is called,
17180 i.e. window_end_valid must not be nil.
17181
17182 Value is
17183
17184 1 if display has been updated
17185 0 if otherwise unsuccessful
17186 -1 if redisplay with same window start is known not to succeed
17187
17188 The following steps are performed:
17189
17190 1. Find the last row in the current matrix of W that is not
17191 affected by changes at the start of current_buffer. If no such row
17192 is found, give up.
17193
17194 2. Find the first row in W's current matrix that is not affected by
17195 changes at the end of current_buffer. Maybe there is no such row.
17196
17197 3. Display lines beginning with the row + 1 found in step 1 to the
17198 row found in step 2 or, if step 2 didn't find a row, to the end of
17199 the window.
17200
17201 4. If cursor is not known to appear on the window, give up.
17202
17203 5. If display stopped at the row found in step 2, scroll the
17204 display and current matrix as needed.
17205
17206 6. Maybe display some lines at the end of W, if we must. This can
17207 happen under various circumstances, like a partially visible line
17208 becoming fully visible, or because newly displayed lines are displayed
17209 in smaller font sizes.
17210
17211 7. Update W's window end information. */
17212
17213 static int
17214 try_window_id (struct window *w)
17215 {
17216 struct frame *f = XFRAME (w->frame);
17217 struct glyph_matrix *current_matrix = w->current_matrix;
17218 struct glyph_matrix *desired_matrix = w->desired_matrix;
17219 struct glyph_row *last_unchanged_at_beg_row;
17220 struct glyph_row *first_unchanged_at_end_row;
17221 struct glyph_row *row;
17222 struct glyph_row *bottom_row;
17223 int bottom_vpos;
17224 struct it it;
17225 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17226 int dvpos, dy;
17227 struct text_pos start_pos;
17228 struct run run;
17229 int first_unchanged_at_end_vpos = 0;
17230 struct glyph_row *last_text_row, *last_text_row_at_end;
17231 struct text_pos start;
17232 ptrdiff_t first_changed_charpos, last_changed_charpos;
17233
17234 #ifdef GLYPH_DEBUG
17235 if (inhibit_try_window_id)
17236 return 0;
17237 #endif
17238
17239 /* This is handy for debugging. */
17240 #if 0
17241 #define GIVE_UP(X) \
17242 do { \
17243 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17244 return 0; \
17245 } while (0)
17246 #else
17247 #define GIVE_UP(X) return 0
17248 #endif
17249
17250 SET_TEXT_POS_FROM_MARKER (start, w->start);
17251
17252 /* Don't use this for mini-windows because these can show
17253 messages and mini-buffers, and we don't handle that here. */
17254 if (MINI_WINDOW_P (w))
17255 GIVE_UP (1);
17256
17257 /* This flag is used to prevent redisplay optimizations. */
17258 if (windows_or_buffers_changed || cursor_type_changed)
17259 GIVE_UP (2);
17260
17261 /* Verify that narrowing has not changed.
17262 Also verify that we were not told to prevent redisplay optimizations.
17263 It would be nice to further
17264 reduce the number of cases where this prevents try_window_id. */
17265 if (current_buffer->clip_changed
17266 || current_buffer->prevent_redisplay_optimizations_p)
17267 GIVE_UP (3);
17268
17269 /* Window must either use window-based redisplay or be full width. */
17270 if (!FRAME_WINDOW_P (f)
17271 && (!FRAME_LINE_INS_DEL_OK (f)
17272 || !WINDOW_FULL_WIDTH_P (w)))
17273 GIVE_UP (4);
17274
17275 /* Give up if point is known NOT to appear in W. */
17276 if (PT < CHARPOS (start))
17277 GIVE_UP (5);
17278
17279 /* Another way to prevent redisplay optimizations. */
17280 if (w->last_modified == 0)
17281 GIVE_UP (6);
17282
17283 /* Verify that window is not hscrolled. */
17284 if (w->hscroll != 0)
17285 GIVE_UP (7);
17286
17287 /* Verify that display wasn't paused. */
17288 if (NILP (w->window_end_valid))
17289 GIVE_UP (8);
17290
17291 /* Can't use this if highlighting a region because a cursor movement
17292 will do more than just set the cursor. */
17293 if (!NILP (Vtransient_mark_mode)
17294 && !NILP (BVAR (current_buffer, mark_active)))
17295 GIVE_UP (9);
17296
17297 /* Likewise if highlighting trailing whitespace. */
17298 if (!NILP (Vshow_trailing_whitespace))
17299 GIVE_UP (11);
17300
17301 /* Likewise if showing a region. */
17302 if (!NILP (w->region_showing))
17303 GIVE_UP (10);
17304
17305 /* Can't use this if overlay arrow position and/or string have
17306 changed. */
17307 if (overlay_arrows_changed_p ())
17308 GIVE_UP (12);
17309
17310 /* When word-wrap is on, adding a space to the first word of a
17311 wrapped line can change the wrap position, altering the line
17312 above it. It might be worthwhile to handle this more
17313 intelligently, but for now just redisplay from scratch. */
17314 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17315 GIVE_UP (21);
17316
17317 /* Under bidi reordering, adding or deleting a character in the
17318 beginning of a paragraph, before the first strong directional
17319 character, can change the base direction of the paragraph (unless
17320 the buffer specifies a fixed paragraph direction), which will
17321 require to redisplay the whole paragraph. It might be worthwhile
17322 to find the paragraph limits and widen the range of redisplayed
17323 lines to that, but for now just give up this optimization and
17324 redisplay from scratch. */
17325 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17326 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17327 GIVE_UP (22);
17328
17329 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17330 only if buffer has really changed. The reason is that the gap is
17331 initially at Z for freshly visited files. The code below would
17332 set end_unchanged to 0 in that case. */
17333 if (MODIFF > SAVE_MODIFF
17334 /* This seems to happen sometimes after saving a buffer. */
17335 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17336 {
17337 if (GPT - BEG < BEG_UNCHANGED)
17338 BEG_UNCHANGED = GPT - BEG;
17339 if (Z - GPT < END_UNCHANGED)
17340 END_UNCHANGED = Z - GPT;
17341 }
17342
17343 /* The position of the first and last character that has been changed. */
17344 first_changed_charpos = BEG + BEG_UNCHANGED;
17345 last_changed_charpos = Z - END_UNCHANGED;
17346
17347 /* If window starts after a line end, and the last change is in
17348 front of that newline, then changes don't affect the display.
17349 This case happens with stealth-fontification. Note that although
17350 the display is unchanged, glyph positions in the matrix have to
17351 be adjusted, of course. */
17352 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17353 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17354 && ((last_changed_charpos < CHARPOS (start)
17355 && CHARPOS (start) == BEGV)
17356 || (last_changed_charpos < CHARPOS (start) - 1
17357 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17358 {
17359 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17360 struct glyph_row *r0;
17361
17362 /* Compute how many chars/bytes have been added to or removed
17363 from the buffer. */
17364 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17365 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17366 Z_delta = Z - Z_old;
17367 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17368
17369 /* Give up if PT is not in the window. Note that it already has
17370 been checked at the start of try_window_id that PT is not in
17371 front of the window start. */
17372 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17373 GIVE_UP (13);
17374
17375 /* If window start is unchanged, we can reuse the whole matrix
17376 as is, after adjusting glyph positions. No need to compute
17377 the window end again, since its offset from Z hasn't changed. */
17378 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17379 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17380 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17381 /* PT must not be in a partially visible line. */
17382 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17383 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17384 {
17385 /* Adjust positions in the glyph matrix. */
17386 if (Z_delta || Z_delta_bytes)
17387 {
17388 struct glyph_row *r1
17389 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17390 increment_matrix_positions (w->current_matrix,
17391 MATRIX_ROW_VPOS (r0, current_matrix),
17392 MATRIX_ROW_VPOS (r1, current_matrix),
17393 Z_delta, Z_delta_bytes);
17394 }
17395
17396 /* Set the cursor. */
17397 row = row_containing_pos (w, PT, r0, NULL, 0);
17398 if (row)
17399 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17400 else
17401 emacs_abort ();
17402 return 1;
17403 }
17404 }
17405
17406 /* Handle the case that changes are all below what is displayed in
17407 the window, and that PT is in the window. This shortcut cannot
17408 be taken if ZV is visible in the window, and text has been added
17409 there that is visible in the window. */
17410 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17411 /* ZV is not visible in the window, or there are no
17412 changes at ZV, actually. */
17413 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17414 || first_changed_charpos == last_changed_charpos))
17415 {
17416 struct glyph_row *r0;
17417
17418 /* Give up if PT is not in the window. Note that it already has
17419 been checked at the start of try_window_id that PT is not in
17420 front of the window start. */
17421 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17422 GIVE_UP (14);
17423
17424 /* If window start is unchanged, we can reuse the whole matrix
17425 as is, without changing glyph positions since no text has
17426 been added/removed in front of the window end. */
17427 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17428 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17429 /* PT must not be in a partially visible line. */
17430 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17431 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17432 {
17433 /* We have to compute the window end anew since text
17434 could have been added/removed after it. */
17435 wset_window_end_pos
17436 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17437 w->window_end_bytepos
17438 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17439
17440 /* Set the cursor. */
17441 row = row_containing_pos (w, PT, r0, NULL, 0);
17442 if (row)
17443 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17444 else
17445 emacs_abort ();
17446 return 2;
17447 }
17448 }
17449
17450 /* Give up if window start is in the changed area.
17451
17452 The condition used to read
17453
17454 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17455
17456 but why that was tested escapes me at the moment. */
17457 if (CHARPOS (start) >= first_changed_charpos
17458 && CHARPOS (start) <= last_changed_charpos)
17459 GIVE_UP (15);
17460
17461 /* Check that window start agrees with the start of the first glyph
17462 row in its current matrix. Check this after we know the window
17463 start is not in changed text, otherwise positions would not be
17464 comparable. */
17465 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17466 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17467 GIVE_UP (16);
17468
17469 /* Give up if the window ends in strings. Overlay strings
17470 at the end are difficult to handle, so don't try. */
17471 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17472 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17473 GIVE_UP (20);
17474
17475 /* Compute the position at which we have to start displaying new
17476 lines. Some of the lines at the top of the window might be
17477 reusable because they are not displaying changed text. Find the
17478 last row in W's current matrix not affected by changes at the
17479 start of current_buffer. Value is null if changes start in the
17480 first line of window. */
17481 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17482 if (last_unchanged_at_beg_row)
17483 {
17484 /* Avoid starting to display in the middle of a character, a TAB
17485 for instance. This is easier than to set up the iterator
17486 exactly, and it's not a frequent case, so the additional
17487 effort wouldn't really pay off. */
17488 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17489 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17490 && last_unchanged_at_beg_row > w->current_matrix->rows)
17491 --last_unchanged_at_beg_row;
17492
17493 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17494 GIVE_UP (17);
17495
17496 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17497 GIVE_UP (18);
17498 start_pos = it.current.pos;
17499
17500 /* Start displaying new lines in the desired matrix at the same
17501 vpos we would use in the current matrix, i.e. below
17502 last_unchanged_at_beg_row. */
17503 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17504 current_matrix);
17505 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17506 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17507
17508 eassert (it.hpos == 0 && it.current_x == 0);
17509 }
17510 else
17511 {
17512 /* There are no reusable lines at the start of the window.
17513 Start displaying in the first text line. */
17514 start_display (&it, w, start);
17515 it.vpos = it.first_vpos;
17516 start_pos = it.current.pos;
17517 }
17518
17519 /* Find the first row that is not affected by changes at the end of
17520 the buffer. Value will be null if there is no unchanged row, in
17521 which case we must redisplay to the end of the window. delta
17522 will be set to the value by which buffer positions beginning with
17523 first_unchanged_at_end_row have to be adjusted due to text
17524 changes. */
17525 first_unchanged_at_end_row
17526 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17527 IF_DEBUG (debug_delta = delta);
17528 IF_DEBUG (debug_delta_bytes = delta_bytes);
17529
17530 /* Set stop_pos to the buffer position up to which we will have to
17531 display new lines. If first_unchanged_at_end_row != NULL, this
17532 is the buffer position of the start of the line displayed in that
17533 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17534 that we don't stop at a buffer position. */
17535 stop_pos = 0;
17536 if (first_unchanged_at_end_row)
17537 {
17538 eassert (last_unchanged_at_beg_row == NULL
17539 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17540
17541 /* If this is a continuation line, move forward to the next one
17542 that isn't. Changes in lines above affect this line.
17543 Caution: this may move first_unchanged_at_end_row to a row
17544 not displaying text. */
17545 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17546 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17547 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17548 < it.last_visible_y))
17549 ++first_unchanged_at_end_row;
17550
17551 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17552 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17553 >= it.last_visible_y))
17554 first_unchanged_at_end_row = NULL;
17555 else
17556 {
17557 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17558 + delta);
17559 first_unchanged_at_end_vpos
17560 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17561 eassert (stop_pos >= Z - END_UNCHANGED);
17562 }
17563 }
17564 else if (last_unchanged_at_beg_row == NULL)
17565 GIVE_UP (19);
17566
17567
17568 #ifdef GLYPH_DEBUG
17569
17570 /* Either there is no unchanged row at the end, or the one we have
17571 now displays text. This is a necessary condition for the window
17572 end pos calculation at the end of this function. */
17573 eassert (first_unchanged_at_end_row == NULL
17574 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17575
17576 debug_last_unchanged_at_beg_vpos
17577 = (last_unchanged_at_beg_row
17578 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17579 : -1);
17580 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17581
17582 #endif /* GLYPH_DEBUG */
17583
17584
17585 /* Display new lines. Set last_text_row to the last new line
17586 displayed which has text on it, i.e. might end up as being the
17587 line where the window_end_vpos is. */
17588 w->cursor.vpos = -1;
17589 last_text_row = NULL;
17590 overlay_arrow_seen = 0;
17591 while (it.current_y < it.last_visible_y
17592 && !fonts_changed_p
17593 && (first_unchanged_at_end_row == NULL
17594 || IT_CHARPOS (it) < stop_pos))
17595 {
17596 if (display_line (&it))
17597 last_text_row = it.glyph_row - 1;
17598 }
17599
17600 if (fonts_changed_p)
17601 return -1;
17602
17603
17604 /* Compute differences in buffer positions, y-positions etc. for
17605 lines reused at the bottom of the window. Compute what we can
17606 scroll. */
17607 if (first_unchanged_at_end_row
17608 /* No lines reused because we displayed everything up to the
17609 bottom of the window. */
17610 && it.current_y < it.last_visible_y)
17611 {
17612 dvpos = (it.vpos
17613 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17614 current_matrix));
17615 dy = it.current_y - first_unchanged_at_end_row->y;
17616 run.current_y = first_unchanged_at_end_row->y;
17617 run.desired_y = run.current_y + dy;
17618 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17619 }
17620 else
17621 {
17622 delta = delta_bytes = dvpos = dy
17623 = run.current_y = run.desired_y = run.height = 0;
17624 first_unchanged_at_end_row = NULL;
17625 }
17626 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17627
17628
17629 /* Find the cursor if not already found. We have to decide whether
17630 PT will appear on this window (it sometimes doesn't, but this is
17631 not a very frequent case.) This decision has to be made before
17632 the current matrix is altered. A value of cursor.vpos < 0 means
17633 that PT is either in one of the lines beginning at
17634 first_unchanged_at_end_row or below the window. Don't care for
17635 lines that might be displayed later at the window end; as
17636 mentioned, this is not a frequent case. */
17637 if (w->cursor.vpos < 0)
17638 {
17639 /* Cursor in unchanged rows at the top? */
17640 if (PT < CHARPOS (start_pos)
17641 && last_unchanged_at_beg_row)
17642 {
17643 row = row_containing_pos (w, PT,
17644 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17645 last_unchanged_at_beg_row + 1, 0);
17646 if (row)
17647 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17648 }
17649
17650 /* Start from first_unchanged_at_end_row looking for PT. */
17651 else if (first_unchanged_at_end_row)
17652 {
17653 row = row_containing_pos (w, PT - delta,
17654 first_unchanged_at_end_row, NULL, 0);
17655 if (row)
17656 set_cursor_from_row (w, row, w->current_matrix, delta,
17657 delta_bytes, dy, dvpos);
17658 }
17659
17660 /* Give up if cursor was not found. */
17661 if (w->cursor.vpos < 0)
17662 {
17663 clear_glyph_matrix (w->desired_matrix);
17664 return -1;
17665 }
17666 }
17667
17668 /* Don't let the cursor end in the scroll margins. */
17669 {
17670 int this_scroll_margin, cursor_height;
17671
17672 this_scroll_margin =
17673 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17674 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17675 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17676
17677 if ((w->cursor.y < this_scroll_margin
17678 && CHARPOS (start) > BEGV)
17679 /* Old redisplay didn't take scroll margin into account at the bottom,
17680 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17681 || (w->cursor.y + (make_cursor_line_fully_visible_p
17682 ? cursor_height + this_scroll_margin
17683 : 1)) > it.last_visible_y)
17684 {
17685 w->cursor.vpos = -1;
17686 clear_glyph_matrix (w->desired_matrix);
17687 return -1;
17688 }
17689 }
17690
17691 /* Scroll the display. Do it before changing the current matrix so
17692 that xterm.c doesn't get confused about where the cursor glyph is
17693 found. */
17694 if (dy && run.height)
17695 {
17696 update_begin (f);
17697
17698 if (FRAME_WINDOW_P (f))
17699 {
17700 FRAME_RIF (f)->update_window_begin_hook (w);
17701 FRAME_RIF (f)->clear_window_mouse_face (w);
17702 FRAME_RIF (f)->scroll_run_hook (w, &run);
17703 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17704 }
17705 else
17706 {
17707 /* Terminal frame. In this case, dvpos gives the number of
17708 lines to scroll by; dvpos < 0 means scroll up. */
17709 int from_vpos
17710 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17711 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17712 int end = (WINDOW_TOP_EDGE_LINE (w)
17713 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17714 + window_internal_height (w));
17715
17716 #if defined (HAVE_GPM) || defined (MSDOS)
17717 x_clear_window_mouse_face (w);
17718 #endif
17719 /* Perform the operation on the screen. */
17720 if (dvpos > 0)
17721 {
17722 /* Scroll last_unchanged_at_beg_row to the end of the
17723 window down dvpos lines. */
17724 set_terminal_window (f, end);
17725
17726 /* On dumb terminals delete dvpos lines at the end
17727 before inserting dvpos empty lines. */
17728 if (!FRAME_SCROLL_REGION_OK (f))
17729 ins_del_lines (f, end - dvpos, -dvpos);
17730
17731 /* Insert dvpos empty lines in front of
17732 last_unchanged_at_beg_row. */
17733 ins_del_lines (f, from, dvpos);
17734 }
17735 else if (dvpos < 0)
17736 {
17737 /* Scroll up last_unchanged_at_beg_vpos to the end of
17738 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17739 set_terminal_window (f, end);
17740
17741 /* Delete dvpos lines in front of
17742 last_unchanged_at_beg_vpos. ins_del_lines will set
17743 the cursor to the given vpos and emit |dvpos| delete
17744 line sequences. */
17745 ins_del_lines (f, from + dvpos, dvpos);
17746
17747 /* On a dumb terminal insert dvpos empty lines at the
17748 end. */
17749 if (!FRAME_SCROLL_REGION_OK (f))
17750 ins_del_lines (f, end + dvpos, -dvpos);
17751 }
17752
17753 set_terminal_window (f, 0);
17754 }
17755
17756 update_end (f);
17757 }
17758
17759 /* Shift reused rows of the current matrix to the right position.
17760 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17761 text. */
17762 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17763 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17764 if (dvpos < 0)
17765 {
17766 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17767 bottom_vpos, dvpos);
17768 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17769 bottom_vpos);
17770 }
17771 else if (dvpos > 0)
17772 {
17773 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17774 bottom_vpos, dvpos);
17775 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17776 first_unchanged_at_end_vpos + dvpos);
17777 }
17778
17779 /* For frame-based redisplay, make sure that current frame and window
17780 matrix are in sync with respect to glyph memory. */
17781 if (!FRAME_WINDOW_P (f))
17782 sync_frame_with_window_matrix_rows (w);
17783
17784 /* Adjust buffer positions in reused rows. */
17785 if (delta || delta_bytes)
17786 increment_matrix_positions (current_matrix,
17787 first_unchanged_at_end_vpos + dvpos,
17788 bottom_vpos, delta, delta_bytes);
17789
17790 /* Adjust Y positions. */
17791 if (dy)
17792 shift_glyph_matrix (w, current_matrix,
17793 first_unchanged_at_end_vpos + dvpos,
17794 bottom_vpos, dy);
17795
17796 if (first_unchanged_at_end_row)
17797 {
17798 first_unchanged_at_end_row += dvpos;
17799 if (first_unchanged_at_end_row->y >= it.last_visible_y
17800 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17801 first_unchanged_at_end_row = NULL;
17802 }
17803
17804 /* If scrolling up, there may be some lines to display at the end of
17805 the window. */
17806 last_text_row_at_end = NULL;
17807 if (dy < 0)
17808 {
17809 /* Scrolling up can leave for example a partially visible line
17810 at the end of the window to be redisplayed. */
17811 /* Set last_row to the glyph row in the current matrix where the
17812 window end line is found. It has been moved up or down in
17813 the matrix by dvpos. */
17814 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17815 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17816
17817 /* If last_row is the window end line, it should display text. */
17818 eassert (last_row->displays_text_p);
17819
17820 /* If window end line was partially visible before, begin
17821 displaying at that line. Otherwise begin displaying with the
17822 line following it. */
17823 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17824 {
17825 init_to_row_start (&it, w, last_row);
17826 it.vpos = last_vpos;
17827 it.current_y = last_row->y;
17828 }
17829 else
17830 {
17831 init_to_row_end (&it, w, last_row);
17832 it.vpos = 1 + last_vpos;
17833 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17834 ++last_row;
17835 }
17836
17837 /* We may start in a continuation line. If so, we have to
17838 get the right continuation_lines_width and current_x. */
17839 it.continuation_lines_width = last_row->continuation_lines_width;
17840 it.hpos = it.current_x = 0;
17841
17842 /* Display the rest of the lines at the window end. */
17843 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17844 while (it.current_y < it.last_visible_y
17845 && !fonts_changed_p)
17846 {
17847 /* Is it always sure that the display agrees with lines in
17848 the current matrix? I don't think so, so we mark rows
17849 displayed invalid in the current matrix by setting their
17850 enabled_p flag to zero. */
17851 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17852 if (display_line (&it))
17853 last_text_row_at_end = it.glyph_row - 1;
17854 }
17855 }
17856
17857 /* Update window_end_pos and window_end_vpos. */
17858 if (first_unchanged_at_end_row
17859 && !last_text_row_at_end)
17860 {
17861 /* Window end line if one of the preserved rows from the current
17862 matrix. Set row to the last row displaying text in current
17863 matrix starting at first_unchanged_at_end_row, after
17864 scrolling. */
17865 eassert (first_unchanged_at_end_row->displays_text_p);
17866 row = find_last_row_displaying_text (w->current_matrix, &it,
17867 first_unchanged_at_end_row);
17868 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17869
17870 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17871 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17872 wset_window_end_vpos
17873 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17874 eassert (w->window_end_bytepos >= 0);
17875 IF_DEBUG (debug_method_add (w, "A"));
17876 }
17877 else if (last_text_row_at_end)
17878 {
17879 wset_window_end_pos
17880 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17881 w->window_end_bytepos
17882 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17883 wset_window_end_vpos
17884 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17885 desired_matrix)));
17886 eassert (w->window_end_bytepos >= 0);
17887 IF_DEBUG (debug_method_add (w, "B"));
17888 }
17889 else if (last_text_row)
17890 {
17891 /* We have displayed either to the end of the window or at the
17892 end of the window, i.e. the last row with text is to be found
17893 in the desired matrix. */
17894 wset_window_end_pos
17895 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17896 w->window_end_bytepos
17897 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17898 wset_window_end_vpos
17899 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17900 eassert (w->window_end_bytepos >= 0);
17901 }
17902 else if (first_unchanged_at_end_row == NULL
17903 && last_text_row == NULL
17904 && last_text_row_at_end == NULL)
17905 {
17906 /* Displayed to end of window, but no line containing text was
17907 displayed. Lines were deleted at the end of the window. */
17908 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17909 int vpos = XFASTINT (w->window_end_vpos);
17910 struct glyph_row *current_row = current_matrix->rows + vpos;
17911 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17912
17913 for (row = NULL;
17914 row == NULL && vpos >= first_vpos;
17915 --vpos, --current_row, --desired_row)
17916 {
17917 if (desired_row->enabled_p)
17918 {
17919 if (desired_row->displays_text_p)
17920 row = desired_row;
17921 }
17922 else if (current_row->displays_text_p)
17923 row = current_row;
17924 }
17925
17926 eassert (row != NULL);
17927 wset_window_end_vpos (w, make_number (vpos + 1));
17928 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17929 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17930 eassert (w->window_end_bytepos >= 0);
17931 IF_DEBUG (debug_method_add (w, "C"));
17932 }
17933 else
17934 emacs_abort ();
17935
17936 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17937 debug_end_vpos = XFASTINT (w->window_end_vpos));
17938
17939 /* Record that display has not been completed. */
17940 wset_window_end_valid (w, Qnil);
17941 w->desired_matrix->no_scrolling_p = 1;
17942 return 3;
17943
17944 #undef GIVE_UP
17945 }
17946
17947
17948 \f
17949 /***********************************************************************
17950 More debugging support
17951 ***********************************************************************/
17952
17953 #ifdef GLYPH_DEBUG
17954
17955 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17956 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17957 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17958
17959
17960 /* Dump the contents of glyph matrix MATRIX on stderr.
17961
17962 GLYPHS 0 means don't show glyph contents.
17963 GLYPHS 1 means show glyphs in short form
17964 GLYPHS > 1 means show glyphs in long form. */
17965
17966 void
17967 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17968 {
17969 int i;
17970 for (i = 0; i < matrix->nrows; ++i)
17971 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17972 }
17973
17974
17975 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17976 the glyph row and area where the glyph comes from. */
17977
17978 void
17979 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17980 {
17981 if (glyph->type == CHAR_GLYPH)
17982 {
17983 fprintf (stderr,
17984 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17985 glyph - row->glyphs[TEXT_AREA],
17986 'C',
17987 glyph->charpos,
17988 (BUFFERP (glyph->object)
17989 ? 'B'
17990 : (STRINGP (glyph->object)
17991 ? 'S'
17992 : '-')),
17993 glyph->pixel_width,
17994 glyph->u.ch,
17995 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17996 ? glyph->u.ch
17997 : '.'),
17998 glyph->face_id,
17999 glyph->left_box_line_p,
18000 glyph->right_box_line_p);
18001 }
18002 else if (glyph->type == STRETCH_GLYPH)
18003 {
18004 fprintf (stderr,
18005 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18006 glyph - row->glyphs[TEXT_AREA],
18007 'S',
18008 glyph->charpos,
18009 (BUFFERP (glyph->object)
18010 ? 'B'
18011 : (STRINGP (glyph->object)
18012 ? 'S'
18013 : '-')),
18014 glyph->pixel_width,
18015 0,
18016 '.',
18017 glyph->face_id,
18018 glyph->left_box_line_p,
18019 glyph->right_box_line_p);
18020 }
18021 else if (glyph->type == IMAGE_GLYPH)
18022 {
18023 fprintf (stderr,
18024 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18025 glyph - row->glyphs[TEXT_AREA],
18026 'I',
18027 glyph->charpos,
18028 (BUFFERP (glyph->object)
18029 ? 'B'
18030 : (STRINGP (glyph->object)
18031 ? 'S'
18032 : '-')),
18033 glyph->pixel_width,
18034 glyph->u.img_id,
18035 '.',
18036 glyph->face_id,
18037 glyph->left_box_line_p,
18038 glyph->right_box_line_p);
18039 }
18040 else if (glyph->type == COMPOSITE_GLYPH)
18041 {
18042 fprintf (stderr,
18043 " %5td %4c %6"pI"d %c %3d 0x%05x",
18044 glyph - row->glyphs[TEXT_AREA],
18045 '+',
18046 glyph->charpos,
18047 (BUFFERP (glyph->object)
18048 ? 'B'
18049 : (STRINGP (glyph->object)
18050 ? 'S'
18051 : '-')),
18052 glyph->pixel_width,
18053 glyph->u.cmp.id);
18054 if (glyph->u.cmp.automatic)
18055 fprintf (stderr,
18056 "[%d-%d]",
18057 glyph->slice.cmp.from, glyph->slice.cmp.to);
18058 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18059 glyph->face_id,
18060 glyph->left_box_line_p,
18061 glyph->right_box_line_p);
18062 }
18063 }
18064
18065
18066 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18067 GLYPHS 0 means don't show glyph contents.
18068 GLYPHS 1 means show glyphs in short form
18069 GLYPHS > 1 means show glyphs in long form. */
18070
18071 void
18072 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18073 {
18074 if (glyphs != 1)
18075 {
18076 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18077 fprintf (stderr, "======================================================================\n");
18078
18079 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18080 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18081 vpos,
18082 MATRIX_ROW_START_CHARPOS (row),
18083 MATRIX_ROW_END_CHARPOS (row),
18084 row->used[TEXT_AREA],
18085 row->contains_overlapping_glyphs_p,
18086 row->enabled_p,
18087 row->truncated_on_left_p,
18088 row->truncated_on_right_p,
18089 row->continued_p,
18090 MATRIX_ROW_CONTINUATION_LINE_P (row),
18091 row->displays_text_p,
18092 row->ends_at_zv_p,
18093 row->fill_line_p,
18094 row->ends_in_middle_of_char_p,
18095 row->starts_in_middle_of_char_p,
18096 row->mouse_face_p,
18097 row->x,
18098 row->y,
18099 row->pixel_width,
18100 row->height,
18101 row->visible_height,
18102 row->ascent,
18103 row->phys_ascent);
18104 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18105 row->end.overlay_string_index,
18106 row->continuation_lines_width);
18107 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18108 CHARPOS (row->start.string_pos),
18109 CHARPOS (row->end.string_pos));
18110 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18111 row->end.dpvec_index);
18112 }
18113
18114 if (glyphs > 1)
18115 {
18116 int area;
18117
18118 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18119 {
18120 struct glyph *glyph = row->glyphs[area];
18121 struct glyph *glyph_end = glyph + row->used[area];
18122
18123 /* Glyph for a line end in text. */
18124 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18125 ++glyph_end;
18126
18127 if (glyph < glyph_end)
18128 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18129
18130 for (; glyph < glyph_end; ++glyph)
18131 dump_glyph (row, glyph, area);
18132 }
18133 }
18134 else if (glyphs == 1)
18135 {
18136 int area;
18137
18138 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18139 {
18140 char *s = alloca (row->used[area] + 1);
18141 int i;
18142
18143 for (i = 0; i < row->used[area]; ++i)
18144 {
18145 struct glyph *glyph = row->glyphs[area] + i;
18146 if (glyph->type == CHAR_GLYPH
18147 && glyph->u.ch < 0x80
18148 && glyph->u.ch >= ' ')
18149 s[i] = glyph->u.ch;
18150 else
18151 s[i] = '.';
18152 }
18153
18154 s[i] = '\0';
18155 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18156 }
18157 }
18158 }
18159
18160
18161 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18162 Sdump_glyph_matrix, 0, 1, "p",
18163 doc: /* Dump the current matrix of the selected window to stderr.
18164 Shows contents of glyph row structures. With non-nil
18165 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18166 glyphs in short form, otherwise show glyphs in long form. */)
18167 (Lisp_Object glyphs)
18168 {
18169 struct window *w = XWINDOW (selected_window);
18170 struct buffer *buffer = XBUFFER (w->buffer);
18171
18172 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18173 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18174 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18175 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18176 fprintf (stderr, "=============================================\n");
18177 dump_glyph_matrix (w->current_matrix,
18178 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18179 return Qnil;
18180 }
18181
18182
18183 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18184 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18185 (void)
18186 {
18187 struct frame *f = XFRAME (selected_frame);
18188 dump_glyph_matrix (f->current_matrix, 1);
18189 return Qnil;
18190 }
18191
18192
18193 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18194 doc: /* Dump glyph row ROW to stderr.
18195 GLYPH 0 means don't dump glyphs.
18196 GLYPH 1 means dump glyphs in short form.
18197 GLYPH > 1 or omitted means dump glyphs in long form. */)
18198 (Lisp_Object row, Lisp_Object glyphs)
18199 {
18200 struct glyph_matrix *matrix;
18201 EMACS_INT vpos;
18202
18203 CHECK_NUMBER (row);
18204 matrix = XWINDOW (selected_window)->current_matrix;
18205 vpos = XINT (row);
18206 if (vpos >= 0 && vpos < matrix->nrows)
18207 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18208 vpos,
18209 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18210 return Qnil;
18211 }
18212
18213
18214 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18215 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18216 GLYPH 0 means don't dump glyphs.
18217 GLYPH 1 means dump glyphs in short form.
18218 GLYPH > 1 or omitted means dump glyphs in long form. */)
18219 (Lisp_Object row, Lisp_Object glyphs)
18220 {
18221 struct frame *sf = SELECTED_FRAME ();
18222 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18223 EMACS_INT vpos;
18224
18225 CHECK_NUMBER (row);
18226 vpos = XINT (row);
18227 if (vpos >= 0 && vpos < m->nrows)
18228 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18229 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18230 return Qnil;
18231 }
18232
18233
18234 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18235 doc: /* Toggle tracing of redisplay.
18236 With ARG, turn tracing on if and only if ARG is positive. */)
18237 (Lisp_Object arg)
18238 {
18239 if (NILP (arg))
18240 trace_redisplay_p = !trace_redisplay_p;
18241 else
18242 {
18243 arg = Fprefix_numeric_value (arg);
18244 trace_redisplay_p = XINT (arg) > 0;
18245 }
18246
18247 return Qnil;
18248 }
18249
18250
18251 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18252 doc: /* Like `format', but print result to stderr.
18253 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18254 (ptrdiff_t nargs, Lisp_Object *args)
18255 {
18256 Lisp_Object s = Fformat (nargs, args);
18257 fprintf (stderr, "%s", SDATA (s));
18258 return Qnil;
18259 }
18260
18261 #endif /* GLYPH_DEBUG */
18262
18263
18264 \f
18265 /***********************************************************************
18266 Building Desired Matrix Rows
18267 ***********************************************************************/
18268
18269 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18270 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18271
18272 static struct glyph_row *
18273 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18274 {
18275 struct frame *f = XFRAME (WINDOW_FRAME (w));
18276 struct buffer *buffer = XBUFFER (w->buffer);
18277 struct buffer *old = current_buffer;
18278 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18279 int arrow_len = SCHARS (overlay_arrow_string);
18280 const unsigned char *arrow_end = arrow_string + arrow_len;
18281 const unsigned char *p;
18282 struct it it;
18283 int multibyte_p;
18284 int n_glyphs_before;
18285
18286 set_buffer_temp (buffer);
18287 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18288 it.glyph_row->used[TEXT_AREA] = 0;
18289 SET_TEXT_POS (it.position, 0, 0);
18290
18291 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18292 p = arrow_string;
18293 while (p < arrow_end)
18294 {
18295 Lisp_Object face, ilisp;
18296
18297 /* Get the next character. */
18298 if (multibyte_p)
18299 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18300 else
18301 {
18302 it.c = it.char_to_display = *p, it.len = 1;
18303 if (! ASCII_CHAR_P (it.c))
18304 it.char_to_display = BYTE8_TO_CHAR (it.c);
18305 }
18306 p += it.len;
18307
18308 /* Get its face. */
18309 ilisp = make_number (p - arrow_string);
18310 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18311 it.face_id = compute_char_face (f, it.char_to_display, face);
18312
18313 /* Compute its width, get its glyphs. */
18314 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18315 SET_TEXT_POS (it.position, -1, -1);
18316 PRODUCE_GLYPHS (&it);
18317
18318 /* If this character doesn't fit any more in the line, we have
18319 to remove some glyphs. */
18320 if (it.current_x > it.last_visible_x)
18321 {
18322 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18323 break;
18324 }
18325 }
18326
18327 set_buffer_temp (old);
18328 return it.glyph_row;
18329 }
18330
18331
18332 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18333 glyphs to insert is determined by produce_special_glyphs. */
18334
18335 static void
18336 insert_left_trunc_glyphs (struct it *it)
18337 {
18338 struct it truncate_it;
18339 struct glyph *from, *end, *to, *toend;
18340
18341 eassert (!FRAME_WINDOW_P (it->f)
18342 || (!it->glyph_row->reversed_p
18343 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18344 || (it->glyph_row->reversed_p
18345 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18346
18347 /* Get the truncation glyphs. */
18348 truncate_it = *it;
18349 truncate_it.current_x = 0;
18350 truncate_it.face_id = DEFAULT_FACE_ID;
18351 truncate_it.glyph_row = &scratch_glyph_row;
18352 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18353 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18354 truncate_it.object = make_number (0);
18355 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18356
18357 /* Overwrite glyphs from IT with truncation glyphs. */
18358 if (!it->glyph_row->reversed_p)
18359 {
18360 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18361
18362 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18363 end = from + tused;
18364 to = it->glyph_row->glyphs[TEXT_AREA];
18365 toend = to + it->glyph_row->used[TEXT_AREA];
18366 if (FRAME_WINDOW_P (it->f))
18367 {
18368 /* On GUI frames, when variable-size fonts are displayed,
18369 the truncation glyphs may need more pixels than the row's
18370 glyphs they overwrite. We overwrite more glyphs to free
18371 enough screen real estate, and enlarge the stretch glyph
18372 on the right (see display_line), if there is one, to
18373 preserve the screen position of the truncation glyphs on
18374 the right. */
18375 int w = 0;
18376 struct glyph *g = to;
18377 short used;
18378
18379 /* The first glyph could be partially visible, in which case
18380 it->glyph_row->x will be negative. But we want the left
18381 truncation glyphs to be aligned at the left margin of the
18382 window, so we override the x coordinate at which the row
18383 will begin. */
18384 it->glyph_row->x = 0;
18385 while (g < toend && w < it->truncation_pixel_width)
18386 {
18387 w += g->pixel_width;
18388 ++g;
18389 }
18390 if (g - to - tused > 0)
18391 {
18392 memmove (to + tused, g, (toend - g) * sizeof(*g));
18393 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18394 }
18395 used = it->glyph_row->used[TEXT_AREA];
18396 if (it->glyph_row->truncated_on_right_p
18397 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18398 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18399 == STRETCH_GLYPH)
18400 {
18401 int extra = w - it->truncation_pixel_width;
18402
18403 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18404 }
18405 }
18406
18407 while (from < end)
18408 *to++ = *from++;
18409
18410 /* There may be padding glyphs left over. Overwrite them too. */
18411 if (!FRAME_WINDOW_P (it->f))
18412 {
18413 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18414 {
18415 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18416 while (from < end)
18417 *to++ = *from++;
18418 }
18419 }
18420
18421 if (to > toend)
18422 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18423 }
18424 else
18425 {
18426 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18427
18428 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18429 that back to front. */
18430 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18431 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18432 toend = it->glyph_row->glyphs[TEXT_AREA];
18433 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18434 if (FRAME_WINDOW_P (it->f))
18435 {
18436 int w = 0;
18437 struct glyph *g = to;
18438
18439 while (g >= toend && w < it->truncation_pixel_width)
18440 {
18441 w += g->pixel_width;
18442 --g;
18443 }
18444 if (to - g - tused > 0)
18445 to = g + tused;
18446 if (it->glyph_row->truncated_on_right_p
18447 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18448 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18449 {
18450 int extra = w - it->truncation_pixel_width;
18451
18452 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18453 }
18454 }
18455
18456 while (from >= end && to >= toend)
18457 *to-- = *from--;
18458 if (!FRAME_WINDOW_P (it->f))
18459 {
18460 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18461 {
18462 from =
18463 truncate_it.glyph_row->glyphs[TEXT_AREA]
18464 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18465 while (from >= end && to >= toend)
18466 *to-- = *from--;
18467 }
18468 }
18469 if (from >= end)
18470 {
18471 /* Need to free some room before prepending additional
18472 glyphs. */
18473 int move_by = from - end + 1;
18474 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18475 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18476
18477 for ( ; g >= g0; g--)
18478 g[move_by] = *g;
18479 while (from >= end)
18480 *to-- = *from--;
18481 it->glyph_row->used[TEXT_AREA] += move_by;
18482 }
18483 }
18484 }
18485
18486 /* Compute the hash code for ROW. */
18487 unsigned
18488 row_hash (struct glyph_row *row)
18489 {
18490 int area, k;
18491 unsigned hashval = 0;
18492
18493 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18494 for (k = 0; k < row->used[area]; ++k)
18495 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18496 + row->glyphs[area][k].u.val
18497 + row->glyphs[area][k].face_id
18498 + row->glyphs[area][k].padding_p
18499 + (row->glyphs[area][k].type << 2));
18500
18501 return hashval;
18502 }
18503
18504 /* Compute the pixel height and width of IT->glyph_row.
18505
18506 Most of the time, ascent and height of a display line will be equal
18507 to the max_ascent and max_height values of the display iterator
18508 structure. This is not the case if
18509
18510 1. We hit ZV without displaying anything. In this case, max_ascent
18511 and max_height will be zero.
18512
18513 2. We have some glyphs that don't contribute to the line height.
18514 (The glyph row flag contributes_to_line_height_p is for future
18515 pixmap extensions).
18516
18517 The first case is easily covered by using default values because in
18518 these cases, the line height does not really matter, except that it
18519 must not be zero. */
18520
18521 static void
18522 compute_line_metrics (struct it *it)
18523 {
18524 struct glyph_row *row = it->glyph_row;
18525
18526 if (FRAME_WINDOW_P (it->f))
18527 {
18528 int i, min_y, max_y;
18529
18530 /* The line may consist of one space only, that was added to
18531 place the cursor on it. If so, the row's height hasn't been
18532 computed yet. */
18533 if (row->height == 0)
18534 {
18535 if (it->max_ascent + it->max_descent == 0)
18536 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18537 row->ascent = it->max_ascent;
18538 row->height = it->max_ascent + it->max_descent;
18539 row->phys_ascent = it->max_phys_ascent;
18540 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18541 row->extra_line_spacing = it->max_extra_line_spacing;
18542 }
18543
18544 /* Compute the width of this line. */
18545 row->pixel_width = row->x;
18546 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18547 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18548
18549 eassert (row->pixel_width >= 0);
18550 eassert (row->ascent >= 0 && row->height > 0);
18551
18552 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18553 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18554
18555 /* If first line's physical ascent is larger than its logical
18556 ascent, use the physical ascent, and make the row taller.
18557 This makes accented characters fully visible. */
18558 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18559 && row->phys_ascent > row->ascent)
18560 {
18561 row->height += row->phys_ascent - row->ascent;
18562 row->ascent = row->phys_ascent;
18563 }
18564
18565 /* Compute how much of the line is visible. */
18566 row->visible_height = row->height;
18567
18568 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18569 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18570
18571 if (row->y < min_y)
18572 row->visible_height -= min_y - row->y;
18573 if (row->y + row->height > max_y)
18574 row->visible_height -= row->y + row->height - max_y;
18575 }
18576 else
18577 {
18578 row->pixel_width = row->used[TEXT_AREA];
18579 if (row->continued_p)
18580 row->pixel_width -= it->continuation_pixel_width;
18581 else if (row->truncated_on_right_p)
18582 row->pixel_width -= it->truncation_pixel_width;
18583 row->ascent = row->phys_ascent = 0;
18584 row->height = row->phys_height = row->visible_height = 1;
18585 row->extra_line_spacing = 0;
18586 }
18587
18588 /* Compute a hash code for this row. */
18589 row->hash = row_hash (row);
18590
18591 it->max_ascent = it->max_descent = 0;
18592 it->max_phys_ascent = it->max_phys_descent = 0;
18593 }
18594
18595
18596 /* Append one space to the glyph row of iterator IT if doing a
18597 window-based redisplay. The space has the same face as
18598 IT->face_id. Value is non-zero if a space was added.
18599
18600 This function is called to make sure that there is always one glyph
18601 at the end of a glyph row that the cursor can be set on under
18602 window-systems. (If there weren't such a glyph we would not know
18603 how wide and tall a box cursor should be displayed).
18604
18605 At the same time this space let's a nicely handle clearing to the
18606 end of the line if the row ends in italic text. */
18607
18608 static int
18609 append_space_for_newline (struct it *it, int default_face_p)
18610 {
18611 if (FRAME_WINDOW_P (it->f))
18612 {
18613 int n = it->glyph_row->used[TEXT_AREA];
18614
18615 if (it->glyph_row->glyphs[TEXT_AREA] + n
18616 < it->glyph_row->glyphs[1 + TEXT_AREA])
18617 {
18618 /* Save some values that must not be changed.
18619 Must save IT->c and IT->len because otherwise
18620 ITERATOR_AT_END_P wouldn't work anymore after
18621 append_space_for_newline has been called. */
18622 enum display_element_type saved_what = it->what;
18623 int saved_c = it->c, saved_len = it->len;
18624 int saved_char_to_display = it->char_to_display;
18625 int saved_x = it->current_x;
18626 int saved_face_id = it->face_id;
18627 struct text_pos saved_pos;
18628 Lisp_Object saved_object;
18629 struct face *face;
18630
18631 saved_object = it->object;
18632 saved_pos = it->position;
18633
18634 it->what = IT_CHARACTER;
18635 memset (&it->position, 0, sizeof it->position);
18636 it->object = make_number (0);
18637 it->c = it->char_to_display = ' ';
18638 it->len = 1;
18639
18640 /* If the default face was remapped, be sure to use the
18641 remapped face for the appended newline. */
18642 if (default_face_p)
18643 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18644 else if (it->face_before_selective_p)
18645 it->face_id = it->saved_face_id;
18646 face = FACE_FROM_ID (it->f, it->face_id);
18647 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18648
18649 PRODUCE_GLYPHS (it);
18650
18651 it->override_ascent = -1;
18652 it->constrain_row_ascent_descent_p = 0;
18653 it->current_x = saved_x;
18654 it->object = saved_object;
18655 it->position = saved_pos;
18656 it->what = saved_what;
18657 it->face_id = saved_face_id;
18658 it->len = saved_len;
18659 it->c = saved_c;
18660 it->char_to_display = saved_char_to_display;
18661 return 1;
18662 }
18663 }
18664
18665 return 0;
18666 }
18667
18668
18669 /* Extend the face of the last glyph in the text area of IT->glyph_row
18670 to the end of the display line. Called from display_line. If the
18671 glyph row is empty, add a space glyph to it so that we know the
18672 face to draw. Set the glyph row flag fill_line_p. If the glyph
18673 row is R2L, prepend a stretch glyph to cover the empty space to the
18674 left of the leftmost glyph. */
18675
18676 static void
18677 extend_face_to_end_of_line (struct it *it)
18678 {
18679 struct face *face, *default_face;
18680 struct frame *f = it->f;
18681
18682 /* If line is already filled, do nothing. Non window-system frames
18683 get a grace of one more ``pixel'' because their characters are
18684 1-``pixel'' wide, so they hit the equality too early. This grace
18685 is needed only for R2L rows that are not continued, to produce
18686 one extra blank where we could display the cursor. */
18687 if (it->current_x >= it->last_visible_x
18688 + (!FRAME_WINDOW_P (f)
18689 && it->glyph_row->reversed_p
18690 && !it->glyph_row->continued_p))
18691 return;
18692
18693 /* The default face, possibly remapped. */
18694 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18695
18696 /* Face extension extends the background and box of IT->face_id
18697 to the end of the line. If the background equals the background
18698 of the frame, we don't have to do anything. */
18699 if (it->face_before_selective_p)
18700 face = FACE_FROM_ID (f, it->saved_face_id);
18701 else
18702 face = FACE_FROM_ID (f, it->face_id);
18703
18704 if (FRAME_WINDOW_P (f)
18705 && it->glyph_row->displays_text_p
18706 && face->box == FACE_NO_BOX
18707 && face->background == FRAME_BACKGROUND_PIXEL (f)
18708 && !face->stipple
18709 && !it->glyph_row->reversed_p)
18710 return;
18711
18712 /* Set the glyph row flag indicating that the face of the last glyph
18713 in the text area has to be drawn to the end of the text area. */
18714 it->glyph_row->fill_line_p = 1;
18715
18716 /* If current character of IT is not ASCII, make sure we have the
18717 ASCII face. This will be automatically undone the next time
18718 get_next_display_element returns a multibyte character. Note
18719 that the character will always be single byte in unibyte
18720 text. */
18721 if (!ASCII_CHAR_P (it->c))
18722 {
18723 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18724 }
18725
18726 if (FRAME_WINDOW_P (f))
18727 {
18728 /* If the row is empty, add a space with the current face of IT,
18729 so that we know which face to draw. */
18730 if (it->glyph_row->used[TEXT_AREA] == 0)
18731 {
18732 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18733 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18734 it->glyph_row->used[TEXT_AREA] = 1;
18735 }
18736 #ifdef HAVE_WINDOW_SYSTEM
18737 if (it->glyph_row->reversed_p)
18738 {
18739 /* Prepend a stretch glyph to the row, such that the
18740 rightmost glyph will be drawn flushed all the way to the
18741 right margin of the window. The stretch glyph that will
18742 occupy the empty space, if any, to the left of the
18743 glyphs. */
18744 struct font *font = face->font ? face->font : FRAME_FONT (f);
18745 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18746 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18747 struct glyph *g;
18748 int row_width, stretch_ascent, stretch_width;
18749 struct text_pos saved_pos;
18750 int saved_face_id, saved_avoid_cursor;
18751
18752 for (row_width = 0, g = row_start; g < row_end; g++)
18753 row_width += g->pixel_width;
18754 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18755 if (stretch_width > 0)
18756 {
18757 stretch_ascent =
18758 (((it->ascent + it->descent)
18759 * FONT_BASE (font)) / FONT_HEIGHT (font));
18760 saved_pos = it->position;
18761 memset (&it->position, 0, sizeof it->position);
18762 saved_avoid_cursor = it->avoid_cursor_p;
18763 it->avoid_cursor_p = 1;
18764 saved_face_id = it->face_id;
18765 /* The last row's stretch glyph should get the default
18766 face, to avoid painting the rest of the window with
18767 the region face, if the region ends at ZV. */
18768 if (it->glyph_row->ends_at_zv_p)
18769 it->face_id = default_face->id;
18770 else
18771 it->face_id = face->id;
18772 append_stretch_glyph (it, make_number (0), stretch_width,
18773 it->ascent + it->descent, stretch_ascent);
18774 it->position = saved_pos;
18775 it->avoid_cursor_p = saved_avoid_cursor;
18776 it->face_id = saved_face_id;
18777 }
18778 }
18779 #endif /* HAVE_WINDOW_SYSTEM */
18780 }
18781 else
18782 {
18783 /* Save some values that must not be changed. */
18784 int saved_x = it->current_x;
18785 struct text_pos saved_pos;
18786 Lisp_Object saved_object;
18787 enum display_element_type saved_what = it->what;
18788 int saved_face_id = it->face_id;
18789
18790 saved_object = it->object;
18791 saved_pos = it->position;
18792
18793 it->what = IT_CHARACTER;
18794 memset (&it->position, 0, sizeof it->position);
18795 it->object = make_number (0);
18796 it->c = it->char_to_display = ' ';
18797 it->len = 1;
18798 /* The last row's blank glyphs should get the default face, to
18799 avoid painting the rest of the window with the region face,
18800 if the region ends at ZV. */
18801 if (it->glyph_row->ends_at_zv_p)
18802 it->face_id = default_face->id;
18803 else
18804 it->face_id = face->id;
18805
18806 PRODUCE_GLYPHS (it);
18807
18808 while (it->current_x <= it->last_visible_x)
18809 PRODUCE_GLYPHS (it);
18810
18811 /* Don't count these blanks really. It would let us insert a left
18812 truncation glyph below and make us set the cursor on them, maybe. */
18813 it->current_x = saved_x;
18814 it->object = saved_object;
18815 it->position = saved_pos;
18816 it->what = saved_what;
18817 it->face_id = saved_face_id;
18818 }
18819 }
18820
18821
18822 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18823 trailing whitespace. */
18824
18825 static int
18826 trailing_whitespace_p (ptrdiff_t charpos)
18827 {
18828 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18829 int c = 0;
18830
18831 while (bytepos < ZV_BYTE
18832 && (c = FETCH_CHAR (bytepos),
18833 c == ' ' || c == '\t'))
18834 ++bytepos;
18835
18836 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18837 {
18838 if (bytepos != PT_BYTE)
18839 return 1;
18840 }
18841 return 0;
18842 }
18843
18844
18845 /* Highlight trailing whitespace, if any, in ROW. */
18846
18847 static void
18848 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18849 {
18850 int used = row->used[TEXT_AREA];
18851
18852 if (used)
18853 {
18854 struct glyph *start = row->glyphs[TEXT_AREA];
18855 struct glyph *glyph = start + used - 1;
18856
18857 if (row->reversed_p)
18858 {
18859 /* Right-to-left rows need to be processed in the opposite
18860 direction, so swap the edge pointers. */
18861 glyph = start;
18862 start = row->glyphs[TEXT_AREA] + used - 1;
18863 }
18864
18865 /* Skip over glyphs inserted to display the cursor at the
18866 end of a line, for extending the face of the last glyph
18867 to the end of the line on terminals, and for truncation
18868 and continuation glyphs. */
18869 if (!row->reversed_p)
18870 {
18871 while (glyph >= start
18872 && glyph->type == CHAR_GLYPH
18873 && INTEGERP (glyph->object))
18874 --glyph;
18875 }
18876 else
18877 {
18878 while (glyph <= start
18879 && glyph->type == CHAR_GLYPH
18880 && INTEGERP (glyph->object))
18881 ++glyph;
18882 }
18883
18884 /* If last glyph is a space or stretch, and it's trailing
18885 whitespace, set the face of all trailing whitespace glyphs in
18886 IT->glyph_row to `trailing-whitespace'. */
18887 if ((row->reversed_p ? glyph <= start : glyph >= start)
18888 && BUFFERP (glyph->object)
18889 && (glyph->type == STRETCH_GLYPH
18890 || (glyph->type == CHAR_GLYPH
18891 && glyph->u.ch == ' '))
18892 && trailing_whitespace_p (glyph->charpos))
18893 {
18894 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18895 if (face_id < 0)
18896 return;
18897
18898 if (!row->reversed_p)
18899 {
18900 while (glyph >= start
18901 && BUFFERP (glyph->object)
18902 && (glyph->type == STRETCH_GLYPH
18903 || (glyph->type == CHAR_GLYPH
18904 && glyph->u.ch == ' ')))
18905 (glyph--)->face_id = face_id;
18906 }
18907 else
18908 {
18909 while (glyph <= start
18910 && BUFFERP (glyph->object)
18911 && (glyph->type == STRETCH_GLYPH
18912 || (glyph->type == CHAR_GLYPH
18913 && glyph->u.ch == ' ')))
18914 (glyph++)->face_id = face_id;
18915 }
18916 }
18917 }
18918 }
18919
18920
18921 /* Value is non-zero if glyph row ROW should be
18922 used to hold the cursor. */
18923
18924 static int
18925 cursor_row_p (struct glyph_row *row)
18926 {
18927 int result = 1;
18928
18929 if (PT == CHARPOS (row->end.pos)
18930 || PT == MATRIX_ROW_END_CHARPOS (row))
18931 {
18932 /* Suppose the row ends on a string.
18933 Unless the row is continued, that means it ends on a newline
18934 in the string. If it's anything other than a display string
18935 (e.g., a before-string from an overlay), we don't want the
18936 cursor there. (This heuristic seems to give the optimal
18937 behavior for the various types of multi-line strings.)
18938 One exception: if the string has `cursor' property on one of
18939 its characters, we _do_ want the cursor there. */
18940 if (CHARPOS (row->end.string_pos) >= 0)
18941 {
18942 if (row->continued_p)
18943 result = 1;
18944 else
18945 {
18946 /* Check for `display' property. */
18947 struct glyph *beg = row->glyphs[TEXT_AREA];
18948 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18949 struct glyph *glyph;
18950
18951 result = 0;
18952 for (glyph = end; glyph >= beg; --glyph)
18953 if (STRINGP (glyph->object))
18954 {
18955 Lisp_Object prop
18956 = Fget_char_property (make_number (PT),
18957 Qdisplay, Qnil);
18958 result =
18959 (!NILP (prop)
18960 && display_prop_string_p (prop, glyph->object));
18961 /* If there's a `cursor' property on one of the
18962 string's characters, this row is a cursor row,
18963 even though this is not a display string. */
18964 if (!result)
18965 {
18966 Lisp_Object s = glyph->object;
18967
18968 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18969 {
18970 ptrdiff_t gpos = glyph->charpos;
18971
18972 if (!NILP (Fget_char_property (make_number (gpos),
18973 Qcursor, s)))
18974 {
18975 result = 1;
18976 break;
18977 }
18978 }
18979 }
18980 break;
18981 }
18982 }
18983 }
18984 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18985 {
18986 /* If the row ends in middle of a real character,
18987 and the line is continued, we want the cursor here.
18988 That's because CHARPOS (ROW->end.pos) would equal
18989 PT if PT is before the character. */
18990 if (!row->ends_in_ellipsis_p)
18991 result = row->continued_p;
18992 else
18993 /* If the row ends in an ellipsis, then
18994 CHARPOS (ROW->end.pos) will equal point after the
18995 invisible text. We want that position to be displayed
18996 after the ellipsis. */
18997 result = 0;
18998 }
18999 /* If the row ends at ZV, display the cursor at the end of that
19000 row instead of at the start of the row below. */
19001 else if (row->ends_at_zv_p)
19002 result = 1;
19003 else
19004 result = 0;
19005 }
19006
19007 return result;
19008 }
19009
19010 \f
19011
19012 /* Push the property PROP so that it will be rendered at the current
19013 position in IT. Return 1 if PROP was successfully pushed, 0
19014 otherwise. Called from handle_line_prefix to handle the
19015 `line-prefix' and `wrap-prefix' properties. */
19016
19017 static int
19018 push_prefix_prop (struct it *it, Lisp_Object prop)
19019 {
19020 struct text_pos pos =
19021 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19022
19023 eassert (it->method == GET_FROM_BUFFER
19024 || it->method == GET_FROM_DISPLAY_VECTOR
19025 || it->method == GET_FROM_STRING);
19026
19027 /* We need to save the current buffer/string position, so it will be
19028 restored by pop_it, because iterate_out_of_display_property
19029 depends on that being set correctly, but some situations leave
19030 it->position not yet set when this function is called. */
19031 push_it (it, &pos);
19032
19033 if (STRINGP (prop))
19034 {
19035 if (SCHARS (prop) == 0)
19036 {
19037 pop_it (it);
19038 return 0;
19039 }
19040
19041 it->string = prop;
19042 it->string_from_prefix_prop_p = 1;
19043 it->multibyte_p = STRING_MULTIBYTE (it->string);
19044 it->current.overlay_string_index = -1;
19045 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19046 it->end_charpos = it->string_nchars = SCHARS (it->string);
19047 it->method = GET_FROM_STRING;
19048 it->stop_charpos = 0;
19049 it->prev_stop = 0;
19050 it->base_level_stop = 0;
19051
19052 /* Force paragraph direction to be that of the parent
19053 buffer/string. */
19054 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19055 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19056 else
19057 it->paragraph_embedding = L2R;
19058
19059 /* Set up the bidi iterator for this display string. */
19060 if (it->bidi_p)
19061 {
19062 it->bidi_it.string.lstring = it->string;
19063 it->bidi_it.string.s = NULL;
19064 it->bidi_it.string.schars = it->end_charpos;
19065 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19066 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19067 it->bidi_it.string.unibyte = !it->multibyte_p;
19068 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19069 }
19070 }
19071 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19072 {
19073 it->method = GET_FROM_STRETCH;
19074 it->object = prop;
19075 }
19076 #ifdef HAVE_WINDOW_SYSTEM
19077 else if (IMAGEP (prop))
19078 {
19079 it->what = IT_IMAGE;
19080 it->image_id = lookup_image (it->f, prop);
19081 it->method = GET_FROM_IMAGE;
19082 }
19083 #endif /* HAVE_WINDOW_SYSTEM */
19084 else
19085 {
19086 pop_it (it); /* bogus display property, give up */
19087 return 0;
19088 }
19089
19090 return 1;
19091 }
19092
19093 /* Return the character-property PROP at the current position in IT. */
19094
19095 static Lisp_Object
19096 get_it_property (struct it *it, Lisp_Object prop)
19097 {
19098 Lisp_Object position;
19099
19100 if (STRINGP (it->object))
19101 position = make_number (IT_STRING_CHARPOS (*it));
19102 else if (BUFFERP (it->object))
19103 position = make_number (IT_CHARPOS (*it));
19104 else
19105 return Qnil;
19106
19107 return Fget_char_property (position, prop, it->object);
19108 }
19109
19110 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19111
19112 static void
19113 handle_line_prefix (struct it *it)
19114 {
19115 Lisp_Object prefix;
19116
19117 if (it->continuation_lines_width > 0)
19118 {
19119 prefix = get_it_property (it, Qwrap_prefix);
19120 if (NILP (prefix))
19121 prefix = Vwrap_prefix;
19122 }
19123 else
19124 {
19125 prefix = get_it_property (it, Qline_prefix);
19126 if (NILP (prefix))
19127 prefix = Vline_prefix;
19128 }
19129 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19130 {
19131 /* If the prefix is wider than the window, and we try to wrap
19132 it, it would acquire its own wrap prefix, and so on till the
19133 iterator stack overflows. So, don't wrap the prefix. */
19134 it->line_wrap = TRUNCATE;
19135 it->avoid_cursor_p = 1;
19136 }
19137 }
19138
19139 \f
19140
19141 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19142 only for R2L lines from display_line and display_string, when they
19143 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19144 the line/string needs to be continued on the next glyph row. */
19145 static void
19146 unproduce_glyphs (struct it *it, int n)
19147 {
19148 struct glyph *glyph, *end;
19149
19150 eassert (it->glyph_row);
19151 eassert (it->glyph_row->reversed_p);
19152 eassert (it->area == TEXT_AREA);
19153 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19154
19155 if (n > it->glyph_row->used[TEXT_AREA])
19156 n = it->glyph_row->used[TEXT_AREA];
19157 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19158 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19159 for ( ; glyph < end; glyph++)
19160 glyph[-n] = *glyph;
19161 }
19162
19163 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19164 and ROW->maxpos. */
19165 static void
19166 find_row_edges (struct it *it, struct glyph_row *row,
19167 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19168 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19169 {
19170 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19171 lines' rows is implemented for bidi-reordered rows. */
19172
19173 /* ROW->minpos is the value of min_pos, the minimal buffer position
19174 we have in ROW, or ROW->start.pos if that is smaller. */
19175 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19176 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19177 else
19178 /* We didn't find buffer positions smaller than ROW->start, or
19179 didn't find _any_ valid buffer positions in any of the glyphs,
19180 so we must trust the iterator's computed positions. */
19181 row->minpos = row->start.pos;
19182 if (max_pos <= 0)
19183 {
19184 max_pos = CHARPOS (it->current.pos);
19185 max_bpos = BYTEPOS (it->current.pos);
19186 }
19187
19188 /* Here are the various use-cases for ending the row, and the
19189 corresponding values for ROW->maxpos:
19190
19191 Line ends in a newline from buffer eol_pos + 1
19192 Line is continued from buffer max_pos + 1
19193 Line is truncated on right it->current.pos
19194 Line ends in a newline from string max_pos + 1(*)
19195 (*) + 1 only when line ends in a forward scan
19196 Line is continued from string max_pos
19197 Line is continued from display vector max_pos
19198 Line is entirely from a string min_pos == max_pos
19199 Line is entirely from a display vector min_pos == max_pos
19200 Line that ends at ZV ZV
19201
19202 If you discover other use-cases, please add them here as
19203 appropriate. */
19204 if (row->ends_at_zv_p)
19205 row->maxpos = it->current.pos;
19206 else if (row->used[TEXT_AREA])
19207 {
19208 int seen_this_string = 0;
19209 struct glyph_row *r1 = row - 1;
19210
19211 /* Did we see the same display string on the previous row? */
19212 if (STRINGP (it->object)
19213 /* this is not the first row */
19214 && row > it->w->desired_matrix->rows
19215 /* previous row is not the header line */
19216 && !r1->mode_line_p
19217 /* previous row also ends in a newline from a string */
19218 && r1->ends_in_newline_from_string_p)
19219 {
19220 struct glyph *start, *end;
19221
19222 /* Search for the last glyph of the previous row that came
19223 from buffer or string. Depending on whether the row is
19224 L2R or R2L, we need to process it front to back or the
19225 other way round. */
19226 if (!r1->reversed_p)
19227 {
19228 start = r1->glyphs[TEXT_AREA];
19229 end = start + r1->used[TEXT_AREA];
19230 /* Glyphs inserted by redisplay have an integer (zero)
19231 as their object. */
19232 while (end > start
19233 && INTEGERP ((end - 1)->object)
19234 && (end - 1)->charpos <= 0)
19235 --end;
19236 if (end > start)
19237 {
19238 if (EQ ((end - 1)->object, it->object))
19239 seen_this_string = 1;
19240 }
19241 else
19242 /* If all the glyphs of the previous row were inserted
19243 by redisplay, it means the previous row was
19244 produced from a single newline, which is only
19245 possible if that newline came from the same string
19246 as the one which produced this ROW. */
19247 seen_this_string = 1;
19248 }
19249 else
19250 {
19251 end = r1->glyphs[TEXT_AREA] - 1;
19252 start = end + r1->used[TEXT_AREA];
19253 while (end < start
19254 && INTEGERP ((end + 1)->object)
19255 && (end + 1)->charpos <= 0)
19256 ++end;
19257 if (end < start)
19258 {
19259 if (EQ ((end + 1)->object, it->object))
19260 seen_this_string = 1;
19261 }
19262 else
19263 seen_this_string = 1;
19264 }
19265 }
19266 /* Take note of each display string that covers a newline only
19267 once, the first time we see it. This is for when a display
19268 string includes more than one newline in it. */
19269 if (row->ends_in_newline_from_string_p && !seen_this_string)
19270 {
19271 /* If we were scanning the buffer forward when we displayed
19272 the string, we want to account for at least one buffer
19273 position that belongs to this row (position covered by
19274 the display string), so that cursor positioning will
19275 consider this row as a candidate when point is at the end
19276 of the visual line represented by this row. This is not
19277 required when scanning back, because max_pos will already
19278 have a much larger value. */
19279 if (CHARPOS (row->end.pos) > max_pos)
19280 INC_BOTH (max_pos, max_bpos);
19281 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19282 }
19283 else if (CHARPOS (it->eol_pos) > 0)
19284 SET_TEXT_POS (row->maxpos,
19285 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19286 else if (row->continued_p)
19287 {
19288 /* If max_pos is different from IT's current position, it
19289 means IT->method does not belong to the display element
19290 at max_pos. However, it also means that the display
19291 element at max_pos was displayed in its entirety on this
19292 line, which is equivalent to saying that the next line
19293 starts at the next buffer position. */
19294 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19295 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19296 else
19297 {
19298 INC_BOTH (max_pos, max_bpos);
19299 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19300 }
19301 }
19302 else if (row->truncated_on_right_p)
19303 /* display_line already called reseat_at_next_visible_line_start,
19304 which puts the iterator at the beginning of the next line, in
19305 the logical order. */
19306 row->maxpos = it->current.pos;
19307 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19308 /* A line that is entirely from a string/image/stretch... */
19309 row->maxpos = row->minpos;
19310 else
19311 emacs_abort ();
19312 }
19313 else
19314 row->maxpos = it->current.pos;
19315 }
19316
19317 /* Construct the glyph row IT->glyph_row in the desired matrix of
19318 IT->w from text at the current position of IT. See dispextern.h
19319 for an overview of struct it. Value is non-zero if
19320 IT->glyph_row displays text, as opposed to a line displaying ZV
19321 only. */
19322
19323 static int
19324 display_line (struct it *it)
19325 {
19326 struct glyph_row *row = it->glyph_row;
19327 Lisp_Object overlay_arrow_string;
19328 struct it wrap_it;
19329 void *wrap_data = NULL;
19330 int may_wrap = 0, wrap_x IF_LINT (= 0);
19331 int wrap_row_used = -1;
19332 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19333 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19334 int wrap_row_extra_line_spacing IF_LINT (= 0);
19335 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19336 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19337 int cvpos;
19338 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19339 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19340
19341 /* We always start displaying at hpos zero even if hscrolled. */
19342 eassert (it->hpos == 0 && it->current_x == 0);
19343
19344 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19345 >= it->w->desired_matrix->nrows)
19346 {
19347 it->w->nrows_scale_factor++;
19348 fonts_changed_p = 1;
19349 return 0;
19350 }
19351
19352 /* Is IT->w showing the region? */
19353 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19354
19355 /* Clear the result glyph row and enable it. */
19356 prepare_desired_row (row);
19357
19358 row->y = it->current_y;
19359 row->start = it->start;
19360 row->continuation_lines_width = it->continuation_lines_width;
19361 row->displays_text_p = 1;
19362 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19363 it->starts_in_middle_of_char_p = 0;
19364
19365 /* Arrange the overlays nicely for our purposes. Usually, we call
19366 display_line on only one line at a time, in which case this
19367 can't really hurt too much, or we call it on lines which appear
19368 one after another in the buffer, in which case all calls to
19369 recenter_overlay_lists but the first will be pretty cheap. */
19370 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19371
19372 /* Move over display elements that are not visible because we are
19373 hscrolled. This may stop at an x-position < IT->first_visible_x
19374 if the first glyph is partially visible or if we hit a line end. */
19375 if (it->current_x < it->first_visible_x)
19376 {
19377 enum move_it_result move_result;
19378
19379 this_line_min_pos = row->start.pos;
19380 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19381 MOVE_TO_POS | MOVE_TO_X);
19382 /* If we are under a large hscroll, move_it_in_display_line_to
19383 could hit the end of the line without reaching
19384 it->first_visible_x. Pretend that we did reach it. This is
19385 especially important on a TTY, where we will call
19386 extend_face_to_end_of_line, which needs to know how many
19387 blank glyphs to produce. */
19388 if (it->current_x < it->first_visible_x
19389 && (move_result == MOVE_NEWLINE_OR_CR
19390 || move_result == MOVE_POS_MATCH_OR_ZV))
19391 it->current_x = it->first_visible_x;
19392
19393 /* Record the smallest positions seen while we moved over
19394 display elements that are not visible. This is needed by
19395 redisplay_internal for optimizing the case where the cursor
19396 stays inside the same line. The rest of this function only
19397 considers positions that are actually displayed, so
19398 RECORD_MAX_MIN_POS will not otherwise record positions that
19399 are hscrolled to the left of the left edge of the window. */
19400 min_pos = CHARPOS (this_line_min_pos);
19401 min_bpos = BYTEPOS (this_line_min_pos);
19402 }
19403 else
19404 {
19405 /* We only do this when not calling `move_it_in_display_line_to'
19406 above, because move_it_in_display_line_to calls
19407 handle_line_prefix itself. */
19408 handle_line_prefix (it);
19409 }
19410
19411 /* Get the initial row height. This is either the height of the
19412 text hscrolled, if there is any, or zero. */
19413 row->ascent = it->max_ascent;
19414 row->height = it->max_ascent + it->max_descent;
19415 row->phys_ascent = it->max_phys_ascent;
19416 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19417 row->extra_line_spacing = it->max_extra_line_spacing;
19418
19419 /* Utility macro to record max and min buffer positions seen until now. */
19420 #define RECORD_MAX_MIN_POS(IT) \
19421 do \
19422 { \
19423 int composition_p = !STRINGP ((IT)->string) \
19424 && ((IT)->what == IT_COMPOSITION); \
19425 ptrdiff_t current_pos = \
19426 composition_p ? (IT)->cmp_it.charpos \
19427 : IT_CHARPOS (*(IT)); \
19428 ptrdiff_t current_bpos = \
19429 composition_p ? CHAR_TO_BYTE (current_pos) \
19430 : IT_BYTEPOS (*(IT)); \
19431 if (current_pos < min_pos) \
19432 { \
19433 min_pos = current_pos; \
19434 min_bpos = current_bpos; \
19435 } \
19436 if (IT_CHARPOS (*it) > max_pos) \
19437 { \
19438 max_pos = IT_CHARPOS (*it); \
19439 max_bpos = IT_BYTEPOS (*it); \
19440 } \
19441 } \
19442 while (0)
19443
19444 /* Loop generating characters. The loop is left with IT on the next
19445 character to display. */
19446 while (1)
19447 {
19448 int n_glyphs_before, hpos_before, x_before;
19449 int x, nglyphs;
19450 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19451
19452 /* Retrieve the next thing to display. Value is zero if end of
19453 buffer reached. */
19454 if (!get_next_display_element (it))
19455 {
19456 /* Maybe add a space at the end of this line that is used to
19457 display the cursor there under X. Set the charpos of the
19458 first glyph of blank lines not corresponding to any text
19459 to -1. */
19460 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19461 row->exact_window_width_line_p = 1;
19462 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19463 || row->used[TEXT_AREA] == 0)
19464 {
19465 row->glyphs[TEXT_AREA]->charpos = -1;
19466 row->displays_text_p = 0;
19467
19468 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19469 && (!MINI_WINDOW_P (it->w)
19470 || (minibuf_level && EQ (it->window, minibuf_window))))
19471 row->indicate_empty_line_p = 1;
19472 }
19473
19474 it->continuation_lines_width = 0;
19475 row->ends_at_zv_p = 1;
19476 /* A row that displays right-to-left text must always have
19477 its last face extended all the way to the end of line,
19478 even if this row ends in ZV, because we still write to
19479 the screen left to right. We also need to extend the
19480 last face if the default face is remapped to some
19481 different face, otherwise the functions that clear
19482 portions of the screen will clear with the default face's
19483 background color. */
19484 if (row->reversed_p
19485 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19486 extend_face_to_end_of_line (it);
19487 break;
19488 }
19489
19490 /* Now, get the metrics of what we want to display. This also
19491 generates glyphs in `row' (which is IT->glyph_row). */
19492 n_glyphs_before = row->used[TEXT_AREA];
19493 x = it->current_x;
19494
19495 /* Remember the line height so far in case the next element doesn't
19496 fit on the line. */
19497 if (it->line_wrap != TRUNCATE)
19498 {
19499 ascent = it->max_ascent;
19500 descent = it->max_descent;
19501 phys_ascent = it->max_phys_ascent;
19502 phys_descent = it->max_phys_descent;
19503
19504 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19505 {
19506 if (IT_DISPLAYING_WHITESPACE (it))
19507 may_wrap = 1;
19508 else if (may_wrap)
19509 {
19510 SAVE_IT (wrap_it, *it, wrap_data);
19511 wrap_x = x;
19512 wrap_row_used = row->used[TEXT_AREA];
19513 wrap_row_ascent = row->ascent;
19514 wrap_row_height = row->height;
19515 wrap_row_phys_ascent = row->phys_ascent;
19516 wrap_row_phys_height = row->phys_height;
19517 wrap_row_extra_line_spacing = row->extra_line_spacing;
19518 wrap_row_min_pos = min_pos;
19519 wrap_row_min_bpos = min_bpos;
19520 wrap_row_max_pos = max_pos;
19521 wrap_row_max_bpos = max_bpos;
19522 may_wrap = 0;
19523 }
19524 }
19525 }
19526
19527 PRODUCE_GLYPHS (it);
19528
19529 /* If this display element was in marginal areas, continue with
19530 the next one. */
19531 if (it->area != TEXT_AREA)
19532 {
19533 row->ascent = max (row->ascent, it->max_ascent);
19534 row->height = max (row->height, it->max_ascent + it->max_descent);
19535 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19536 row->phys_height = max (row->phys_height,
19537 it->max_phys_ascent + it->max_phys_descent);
19538 row->extra_line_spacing = max (row->extra_line_spacing,
19539 it->max_extra_line_spacing);
19540 set_iterator_to_next (it, 1);
19541 continue;
19542 }
19543
19544 /* Does the display element fit on the line? If we truncate
19545 lines, we should draw past the right edge of the window. If
19546 we don't truncate, we want to stop so that we can display the
19547 continuation glyph before the right margin. If lines are
19548 continued, there are two possible strategies for characters
19549 resulting in more than 1 glyph (e.g. tabs): Display as many
19550 glyphs as possible in this line and leave the rest for the
19551 continuation line, or display the whole element in the next
19552 line. Original redisplay did the former, so we do it also. */
19553 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19554 hpos_before = it->hpos;
19555 x_before = x;
19556
19557 if (/* Not a newline. */
19558 nglyphs > 0
19559 /* Glyphs produced fit entirely in the line. */
19560 && it->current_x < it->last_visible_x)
19561 {
19562 it->hpos += nglyphs;
19563 row->ascent = max (row->ascent, it->max_ascent);
19564 row->height = max (row->height, it->max_ascent + it->max_descent);
19565 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19566 row->phys_height = max (row->phys_height,
19567 it->max_phys_ascent + it->max_phys_descent);
19568 row->extra_line_spacing = max (row->extra_line_spacing,
19569 it->max_extra_line_spacing);
19570 if (it->current_x - it->pixel_width < it->first_visible_x)
19571 row->x = x - it->first_visible_x;
19572 /* Record the maximum and minimum buffer positions seen so
19573 far in glyphs that will be displayed by this row. */
19574 if (it->bidi_p)
19575 RECORD_MAX_MIN_POS (it);
19576 }
19577 else
19578 {
19579 int i, new_x;
19580 struct glyph *glyph;
19581
19582 for (i = 0; i < nglyphs; ++i, x = new_x)
19583 {
19584 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19585 new_x = x + glyph->pixel_width;
19586
19587 if (/* Lines are continued. */
19588 it->line_wrap != TRUNCATE
19589 && (/* Glyph doesn't fit on the line. */
19590 new_x > it->last_visible_x
19591 /* Or it fits exactly on a window system frame. */
19592 || (new_x == it->last_visible_x
19593 && FRAME_WINDOW_P (it->f)
19594 && (row->reversed_p
19595 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19596 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19597 {
19598 /* End of a continued line. */
19599
19600 if (it->hpos == 0
19601 || (new_x == it->last_visible_x
19602 && FRAME_WINDOW_P (it->f)
19603 && (row->reversed_p
19604 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19605 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19606 {
19607 /* Current glyph is the only one on the line or
19608 fits exactly on the line. We must continue
19609 the line because we can't draw the cursor
19610 after the glyph. */
19611 row->continued_p = 1;
19612 it->current_x = new_x;
19613 it->continuation_lines_width += new_x;
19614 ++it->hpos;
19615 if (i == nglyphs - 1)
19616 {
19617 /* If line-wrap is on, check if a previous
19618 wrap point was found. */
19619 if (wrap_row_used > 0
19620 /* Even if there is a previous wrap
19621 point, continue the line here as
19622 usual, if (i) the previous character
19623 was a space or tab AND (ii) the
19624 current character is not. */
19625 && (!may_wrap
19626 || IT_DISPLAYING_WHITESPACE (it)))
19627 goto back_to_wrap;
19628
19629 /* Record the maximum and minimum buffer
19630 positions seen so far in glyphs that will be
19631 displayed by this row. */
19632 if (it->bidi_p)
19633 RECORD_MAX_MIN_POS (it);
19634 set_iterator_to_next (it, 1);
19635 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19636 {
19637 if (!get_next_display_element (it))
19638 {
19639 row->exact_window_width_line_p = 1;
19640 it->continuation_lines_width = 0;
19641 row->continued_p = 0;
19642 row->ends_at_zv_p = 1;
19643 }
19644 else if (ITERATOR_AT_END_OF_LINE_P (it))
19645 {
19646 row->continued_p = 0;
19647 row->exact_window_width_line_p = 1;
19648 }
19649 }
19650 }
19651 else if (it->bidi_p)
19652 RECORD_MAX_MIN_POS (it);
19653 }
19654 else if (CHAR_GLYPH_PADDING_P (*glyph)
19655 && !FRAME_WINDOW_P (it->f))
19656 {
19657 /* A padding glyph that doesn't fit on this line.
19658 This means the whole character doesn't fit
19659 on the line. */
19660 if (row->reversed_p)
19661 unproduce_glyphs (it, row->used[TEXT_AREA]
19662 - n_glyphs_before);
19663 row->used[TEXT_AREA] = n_glyphs_before;
19664
19665 /* Fill the rest of the row with continuation
19666 glyphs like in 20.x. */
19667 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19668 < row->glyphs[1 + TEXT_AREA])
19669 produce_special_glyphs (it, IT_CONTINUATION);
19670
19671 row->continued_p = 1;
19672 it->current_x = x_before;
19673 it->continuation_lines_width += x_before;
19674
19675 /* Restore the height to what it was before the
19676 element not fitting on the line. */
19677 it->max_ascent = ascent;
19678 it->max_descent = descent;
19679 it->max_phys_ascent = phys_ascent;
19680 it->max_phys_descent = phys_descent;
19681 }
19682 else if (wrap_row_used > 0)
19683 {
19684 back_to_wrap:
19685 if (row->reversed_p)
19686 unproduce_glyphs (it,
19687 row->used[TEXT_AREA] - wrap_row_used);
19688 RESTORE_IT (it, &wrap_it, wrap_data);
19689 it->continuation_lines_width += wrap_x;
19690 row->used[TEXT_AREA] = wrap_row_used;
19691 row->ascent = wrap_row_ascent;
19692 row->height = wrap_row_height;
19693 row->phys_ascent = wrap_row_phys_ascent;
19694 row->phys_height = wrap_row_phys_height;
19695 row->extra_line_spacing = wrap_row_extra_line_spacing;
19696 min_pos = wrap_row_min_pos;
19697 min_bpos = wrap_row_min_bpos;
19698 max_pos = wrap_row_max_pos;
19699 max_bpos = wrap_row_max_bpos;
19700 row->continued_p = 1;
19701 row->ends_at_zv_p = 0;
19702 row->exact_window_width_line_p = 0;
19703 it->continuation_lines_width += x;
19704
19705 /* Make sure that a non-default face is extended
19706 up to the right margin of the window. */
19707 extend_face_to_end_of_line (it);
19708 }
19709 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19710 {
19711 /* A TAB that extends past the right edge of the
19712 window. This produces a single glyph on
19713 window system frames. We leave the glyph in
19714 this row and let it fill the row, but don't
19715 consume the TAB. */
19716 if ((row->reversed_p
19717 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19718 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19719 produce_special_glyphs (it, IT_CONTINUATION);
19720 it->continuation_lines_width += it->last_visible_x;
19721 row->ends_in_middle_of_char_p = 1;
19722 row->continued_p = 1;
19723 glyph->pixel_width = it->last_visible_x - x;
19724 it->starts_in_middle_of_char_p = 1;
19725 }
19726 else
19727 {
19728 /* Something other than a TAB that draws past
19729 the right edge of the window. Restore
19730 positions to values before the element. */
19731 if (row->reversed_p)
19732 unproduce_glyphs (it, row->used[TEXT_AREA]
19733 - (n_glyphs_before + i));
19734 row->used[TEXT_AREA] = n_glyphs_before + i;
19735
19736 /* Display continuation glyphs. */
19737 it->current_x = x_before;
19738 it->continuation_lines_width += x;
19739 if (!FRAME_WINDOW_P (it->f)
19740 || (row->reversed_p
19741 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19742 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19743 produce_special_glyphs (it, IT_CONTINUATION);
19744 row->continued_p = 1;
19745
19746 extend_face_to_end_of_line (it);
19747
19748 if (nglyphs > 1 && i > 0)
19749 {
19750 row->ends_in_middle_of_char_p = 1;
19751 it->starts_in_middle_of_char_p = 1;
19752 }
19753
19754 /* Restore the height to what it was before the
19755 element not fitting on the line. */
19756 it->max_ascent = ascent;
19757 it->max_descent = descent;
19758 it->max_phys_ascent = phys_ascent;
19759 it->max_phys_descent = phys_descent;
19760 }
19761
19762 break;
19763 }
19764 else if (new_x > it->first_visible_x)
19765 {
19766 /* Increment number of glyphs actually displayed. */
19767 ++it->hpos;
19768
19769 /* Record the maximum and minimum buffer positions
19770 seen so far in glyphs that will be displayed by
19771 this row. */
19772 if (it->bidi_p)
19773 RECORD_MAX_MIN_POS (it);
19774
19775 if (x < it->first_visible_x)
19776 /* Glyph is partially visible, i.e. row starts at
19777 negative X position. */
19778 row->x = x - it->first_visible_x;
19779 }
19780 else
19781 {
19782 /* Glyph is completely off the left margin of the
19783 window. This should not happen because of the
19784 move_it_in_display_line at the start of this
19785 function, unless the text display area of the
19786 window is empty. */
19787 eassert (it->first_visible_x <= it->last_visible_x);
19788 }
19789 }
19790 /* Even if this display element produced no glyphs at all,
19791 we want to record its position. */
19792 if (it->bidi_p && nglyphs == 0)
19793 RECORD_MAX_MIN_POS (it);
19794
19795 row->ascent = max (row->ascent, it->max_ascent);
19796 row->height = max (row->height, it->max_ascent + it->max_descent);
19797 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19798 row->phys_height = max (row->phys_height,
19799 it->max_phys_ascent + it->max_phys_descent);
19800 row->extra_line_spacing = max (row->extra_line_spacing,
19801 it->max_extra_line_spacing);
19802
19803 /* End of this display line if row is continued. */
19804 if (row->continued_p || row->ends_at_zv_p)
19805 break;
19806 }
19807
19808 at_end_of_line:
19809 /* Is this a line end? If yes, we're also done, after making
19810 sure that a non-default face is extended up to the right
19811 margin of the window. */
19812 if (ITERATOR_AT_END_OF_LINE_P (it))
19813 {
19814 int used_before = row->used[TEXT_AREA];
19815
19816 row->ends_in_newline_from_string_p = STRINGP (it->object);
19817
19818 /* Add a space at the end of the line that is used to
19819 display the cursor there. */
19820 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19821 append_space_for_newline (it, 0);
19822
19823 /* Extend the face to the end of the line. */
19824 extend_face_to_end_of_line (it);
19825
19826 /* Make sure we have the position. */
19827 if (used_before == 0)
19828 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19829
19830 /* Record the position of the newline, for use in
19831 find_row_edges. */
19832 it->eol_pos = it->current.pos;
19833
19834 /* Consume the line end. This skips over invisible lines. */
19835 set_iterator_to_next (it, 1);
19836 it->continuation_lines_width = 0;
19837 break;
19838 }
19839
19840 /* Proceed with next display element. Note that this skips
19841 over lines invisible because of selective display. */
19842 set_iterator_to_next (it, 1);
19843
19844 /* If we truncate lines, we are done when the last displayed
19845 glyphs reach past the right margin of the window. */
19846 if (it->line_wrap == TRUNCATE
19847 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19848 ? (it->current_x >= it->last_visible_x)
19849 : (it->current_x > it->last_visible_x)))
19850 {
19851 /* Maybe add truncation glyphs. */
19852 if (!FRAME_WINDOW_P (it->f)
19853 || (row->reversed_p
19854 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19855 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19856 {
19857 int i, n;
19858
19859 if (!row->reversed_p)
19860 {
19861 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19862 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19863 break;
19864 }
19865 else
19866 {
19867 for (i = 0; i < row->used[TEXT_AREA]; i++)
19868 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19869 break;
19870 /* Remove any padding glyphs at the front of ROW, to
19871 make room for the truncation glyphs we will be
19872 adding below. The loop below always inserts at
19873 least one truncation glyph, so also remove the
19874 last glyph added to ROW. */
19875 unproduce_glyphs (it, i + 1);
19876 /* Adjust i for the loop below. */
19877 i = row->used[TEXT_AREA] - (i + 1);
19878 }
19879
19880 it->current_x = x_before;
19881 if (!FRAME_WINDOW_P (it->f))
19882 {
19883 for (n = row->used[TEXT_AREA]; i < n; ++i)
19884 {
19885 row->used[TEXT_AREA] = i;
19886 produce_special_glyphs (it, IT_TRUNCATION);
19887 }
19888 }
19889 else
19890 {
19891 row->used[TEXT_AREA] = i;
19892 produce_special_glyphs (it, IT_TRUNCATION);
19893 }
19894 }
19895 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19896 {
19897 /* Don't truncate if we can overflow newline into fringe. */
19898 if (!get_next_display_element (it))
19899 {
19900 it->continuation_lines_width = 0;
19901 row->ends_at_zv_p = 1;
19902 row->exact_window_width_line_p = 1;
19903 break;
19904 }
19905 if (ITERATOR_AT_END_OF_LINE_P (it))
19906 {
19907 row->exact_window_width_line_p = 1;
19908 goto at_end_of_line;
19909 }
19910 it->current_x = x_before;
19911 }
19912
19913 row->truncated_on_right_p = 1;
19914 it->continuation_lines_width = 0;
19915 reseat_at_next_visible_line_start (it, 0);
19916 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19917 it->hpos = hpos_before;
19918 break;
19919 }
19920 }
19921
19922 if (wrap_data)
19923 bidi_unshelve_cache (wrap_data, 1);
19924
19925 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19926 at the left window margin. */
19927 if (it->first_visible_x
19928 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19929 {
19930 if (!FRAME_WINDOW_P (it->f)
19931 || (row->reversed_p
19932 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19933 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19934 insert_left_trunc_glyphs (it);
19935 row->truncated_on_left_p = 1;
19936 }
19937
19938 /* Remember the position at which this line ends.
19939
19940 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19941 cannot be before the call to find_row_edges below, since that is
19942 where these positions are determined. */
19943 row->end = it->current;
19944 if (!it->bidi_p)
19945 {
19946 row->minpos = row->start.pos;
19947 row->maxpos = row->end.pos;
19948 }
19949 else
19950 {
19951 /* ROW->minpos and ROW->maxpos must be the smallest and
19952 `1 + the largest' buffer positions in ROW. But if ROW was
19953 bidi-reordered, these two positions can be anywhere in the
19954 row, so we must determine them now. */
19955 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19956 }
19957
19958 /* If the start of this line is the overlay arrow-position, then
19959 mark this glyph row as the one containing the overlay arrow.
19960 This is clearly a mess with variable size fonts. It would be
19961 better to let it be displayed like cursors under X. */
19962 if ((row->displays_text_p || !overlay_arrow_seen)
19963 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19964 !NILP (overlay_arrow_string)))
19965 {
19966 /* Overlay arrow in window redisplay is a fringe bitmap. */
19967 if (STRINGP (overlay_arrow_string))
19968 {
19969 struct glyph_row *arrow_row
19970 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19971 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19972 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19973 struct glyph *p = row->glyphs[TEXT_AREA];
19974 struct glyph *p2, *end;
19975
19976 /* Copy the arrow glyphs. */
19977 while (glyph < arrow_end)
19978 *p++ = *glyph++;
19979
19980 /* Throw away padding glyphs. */
19981 p2 = p;
19982 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19983 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19984 ++p2;
19985 if (p2 > p)
19986 {
19987 while (p2 < end)
19988 *p++ = *p2++;
19989 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19990 }
19991 }
19992 else
19993 {
19994 eassert (INTEGERP (overlay_arrow_string));
19995 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19996 }
19997 overlay_arrow_seen = 1;
19998 }
19999
20000 /* Highlight trailing whitespace. */
20001 if (!NILP (Vshow_trailing_whitespace))
20002 highlight_trailing_whitespace (it->f, it->glyph_row);
20003
20004 /* Compute pixel dimensions of this line. */
20005 compute_line_metrics (it);
20006
20007 /* Implementation note: No changes in the glyphs of ROW or in their
20008 faces can be done past this point, because compute_line_metrics
20009 computes ROW's hash value and stores it within the glyph_row
20010 structure. */
20011
20012 /* Record whether this row ends inside an ellipsis. */
20013 row->ends_in_ellipsis_p
20014 = (it->method == GET_FROM_DISPLAY_VECTOR
20015 && it->ellipsis_p);
20016
20017 /* Save fringe bitmaps in this row. */
20018 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20019 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20020 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20021 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20022
20023 it->left_user_fringe_bitmap = 0;
20024 it->left_user_fringe_face_id = 0;
20025 it->right_user_fringe_bitmap = 0;
20026 it->right_user_fringe_face_id = 0;
20027
20028 /* Maybe set the cursor. */
20029 cvpos = it->w->cursor.vpos;
20030 if ((cvpos < 0
20031 /* In bidi-reordered rows, keep checking for proper cursor
20032 position even if one has been found already, because buffer
20033 positions in such rows change non-linearly with ROW->VPOS,
20034 when a line is continued. One exception: when we are at ZV,
20035 display cursor on the first suitable glyph row, since all
20036 the empty rows after that also have their position set to ZV. */
20037 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20038 lines' rows is implemented for bidi-reordered rows. */
20039 || (it->bidi_p
20040 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20041 && PT >= MATRIX_ROW_START_CHARPOS (row)
20042 && PT <= MATRIX_ROW_END_CHARPOS (row)
20043 && cursor_row_p (row))
20044 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20045
20046 /* Prepare for the next line. This line starts horizontally at (X
20047 HPOS) = (0 0). Vertical positions are incremented. As a
20048 convenience for the caller, IT->glyph_row is set to the next
20049 row to be used. */
20050 it->current_x = it->hpos = 0;
20051 it->current_y += row->height;
20052 SET_TEXT_POS (it->eol_pos, 0, 0);
20053 ++it->vpos;
20054 ++it->glyph_row;
20055 /* The next row should by default use the same value of the
20056 reversed_p flag as this one. set_iterator_to_next decides when
20057 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20058 the flag accordingly. */
20059 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20060 it->glyph_row->reversed_p = row->reversed_p;
20061 it->start = row->end;
20062 return row->displays_text_p;
20063
20064 #undef RECORD_MAX_MIN_POS
20065 }
20066
20067 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20068 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20069 doc: /* Return paragraph direction at point in BUFFER.
20070 Value is either `left-to-right' or `right-to-left'.
20071 If BUFFER is omitted or nil, it defaults to the current buffer.
20072
20073 Paragraph direction determines how the text in the paragraph is displayed.
20074 In left-to-right paragraphs, text begins at the left margin of the window
20075 and the reading direction is generally left to right. In right-to-left
20076 paragraphs, text begins at the right margin and is read from right to left.
20077
20078 See also `bidi-paragraph-direction'. */)
20079 (Lisp_Object buffer)
20080 {
20081 struct buffer *buf = current_buffer;
20082 struct buffer *old = buf;
20083
20084 if (! NILP (buffer))
20085 {
20086 CHECK_BUFFER (buffer);
20087 buf = XBUFFER (buffer);
20088 }
20089
20090 if (NILP (BVAR (buf, bidi_display_reordering))
20091 || NILP (BVAR (buf, enable_multibyte_characters))
20092 /* When we are loading loadup.el, the character property tables
20093 needed for bidi iteration are not yet available. */
20094 || !NILP (Vpurify_flag))
20095 return Qleft_to_right;
20096 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20097 return BVAR (buf, bidi_paragraph_direction);
20098 else
20099 {
20100 /* Determine the direction from buffer text. We could try to
20101 use current_matrix if it is up to date, but this seems fast
20102 enough as it is. */
20103 struct bidi_it itb;
20104 ptrdiff_t pos = BUF_PT (buf);
20105 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20106 int c;
20107 void *itb_data = bidi_shelve_cache ();
20108
20109 set_buffer_temp (buf);
20110 /* bidi_paragraph_init finds the base direction of the paragraph
20111 by searching forward from paragraph start. We need the base
20112 direction of the current or _previous_ paragraph, so we need
20113 to make sure we are within that paragraph. To that end, find
20114 the previous non-empty line. */
20115 if (pos >= ZV && pos > BEGV)
20116 {
20117 pos--;
20118 bytepos = CHAR_TO_BYTE (pos);
20119 }
20120 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20121 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20122 {
20123 while ((c = FETCH_BYTE (bytepos)) == '\n'
20124 || c == ' ' || c == '\t' || c == '\f')
20125 {
20126 if (bytepos <= BEGV_BYTE)
20127 break;
20128 bytepos--;
20129 pos--;
20130 }
20131 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20132 bytepos--;
20133 }
20134 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20135 itb.paragraph_dir = NEUTRAL_DIR;
20136 itb.string.s = NULL;
20137 itb.string.lstring = Qnil;
20138 itb.string.bufpos = 0;
20139 itb.string.unibyte = 0;
20140 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20141 bidi_unshelve_cache (itb_data, 0);
20142 set_buffer_temp (old);
20143 switch (itb.paragraph_dir)
20144 {
20145 case L2R:
20146 return Qleft_to_right;
20147 break;
20148 case R2L:
20149 return Qright_to_left;
20150 break;
20151 default:
20152 emacs_abort ();
20153 }
20154 }
20155 }
20156
20157
20158 \f
20159 /***********************************************************************
20160 Menu Bar
20161 ***********************************************************************/
20162
20163 /* Redisplay the menu bar in the frame for window W.
20164
20165 The menu bar of X frames that don't have X toolkit support is
20166 displayed in a special window W->frame->menu_bar_window.
20167
20168 The menu bar of terminal frames is treated specially as far as
20169 glyph matrices are concerned. Menu bar lines are not part of
20170 windows, so the update is done directly on the frame matrix rows
20171 for the menu bar. */
20172
20173 static void
20174 display_menu_bar (struct window *w)
20175 {
20176 struct frame *f = XFRAME (WINDOW_FRAME (w));
20177 struct it it;
20178 Lisp_Object items;
20179 int i;
20180
20181 /* Don't do all this for graphical frames. */
20182 #ifdef HAVE_NTGUI
20183 if (FRAME_W32_P (f))
20184 return;
20185 #endif
20186 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20187 if (FRAME_X_P (f))
20188 return;
20189 #endif
20190
20191 #ifdef HAVE_NS
20192 if (FRAME_NS_P (f))
20193 return;
20194 #endif /* HAVE_NS */
20195
20196 #ifdef USE_X_TOOLKIT
20197 eassert (!FRAME_WINDOW_P (f));
20198 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20199 it.first_visible_x = 0;
20200 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20201 #else /* not USE_X_TOOLKIT */
20202 if (FRAME_WINDOW_P (f))
20203 {
20204 /* Menu bar lines are displayed in the desired matrix of the
20205 dummy window menu_bar_window. */
20206 struct window *menu_w;
20207 eassert (WINDOWP (f->menu_bar_window));
20208 menu_w = XWINDOW (f->menu_bar_window);
20209 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20210 MENU_FACE_ID);
20211 it.first_visible_x = 0;
20212 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20213 }
20214 else
20215 {
20216 /* This is a TTY frame, i.e. character hpos/vpos are used as
20217 pixel x/y. */
20218 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20219 MENU_FACE_ID);
20220 it.first_visible_x = 0;
20221 it.last_visible_x = FRAME_COLS (f);
20222 }
20223 #endif /* not USE_X_TOOLKIT */
20224
20225 /* FIXME: This should be controlled by a user option. See the
20226 comments in redisplay_tool_bar and display_mode_line about
20227 this. */
20228 it.paragraph_embedding = L2R;
20229
20230 /* Clear all rows of the menu bar. */
20231 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20232 {
20233 struct glyph_row *row = it.glyph_row + i;
20234 clear_glyph_row (row);
20235 row->enabled_p = 1;
20236 row->full_width_p = 1;
20237 }
20238
20239 /* Display all items of the menu bar. */
20240 items = FRAME_MENU_BAR_ITEMS (it.f);
20241 for (i = 0; i < ASIZE (items); i += 4)
20242 {
20243 Lisp_Object string;
20244
20245 /* Stop at nil string. */
20246 string = AREF (items, i + 1);
20247 if (NILP (string))
20248 break;
20249
20250 /* Remember where item was displayed. */
20251 ASET (items, i + 3, make_number (it.hpos));
20252
20253 /* Display the item, pad with one space. */
20254 if (it.current_x < it.last_visible_x)
20255 display_string (NULL, string, Qnil, 0, 0, &it,
20256 SCHARS (string) + 1, 0, 0, -1);
20257 }
20258
20259 /* Fill out the line with spaces. */
20260 if (it.current_x < it.last_visible_x)
20261 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20262
20263 /* Compute the total height of the lines. */
20264 compute_line_metrics (&it);
20265 }
20266
20267
20268 \f
20269 /***********************************************************************
20270 Mode Line
20271 ***********************************************************************/
20272
20273 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20274 FORCE is non-zero, redisplay mode lines unconditionally.
20275 Otherwise, redisplay only mode lines that are garbaged. Value is
20276 the number of windows whose mode lines were redisplayed. */
20277
20278 static int
20279 redisplay_mode_lines (Lisp_Object window, int force)
20280 {
20281 int nwindows = 0;
20282
20283 while (!NILP (window))
20284 {
20285 struct window *w = XWINDOW (window);
20286
20287 if (WINDOWP (w->hchild))
20288 nwindows += redisplay_mode_lines (w->hchild, force);
20289 else if (WINDOWP (w->vchild))
20290 nwindows += redisplay_mode_lines (w->vchild, force);
20291 else if (force
20292 || FRAME_GARBAGED_P (XFRAME (w->frame))
20293 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20294 {
20295 struct text_pos lpoint;
20296 struct buffer *old = current_buffer;
20297
20298 /* Set the window's buffer for the mode line display. */
20299 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20300 set_buffer_internal_1 (XBUFFER (w->buffer));
20301
20302 /* Point refers normally to the selected window. For any
20303 other window, set up appropriate value. */
20304 if (!EQ (window, selected_window))
20305 {
20306 struct text_pos pt;
20307
20308 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20309 if (CHARPOS (pt) < BEGV)
20310 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20311 else if (CHARPOS (pt) > (ZV - 1))
20312 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20313 else
20314 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20315 }
20316
20317 /* Display mode lines. */
20318 clear_glyph_matrix (w->desired_matrix);
20319 if (display_mode_lines (w))
20320 {
20321 ++nwindows;
20322 w->must_be_updated_p = 1;
20323 }
20324
20325 /* Restore old settings. */
20326 set_buffer_internal_1 (old);
20327 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20328 }
20329
20330 window = w->next;
20331 }
20332
20333 return nwindows;
20334 }
20335
20336
20337 /* Display the mode and/or header line of window W. Value is the
20338 sum number of mode lines and header lines displayed. */
20339
20340 static int
20341 display_mode_lines (struct window *w)
20342 {
20343 Lisp_Object old_selected_window, old_selected_frame;
20344 int n = 0;
20345
20346 old_selected_frame = selected_frame;
20347 selected_frame = w->frame;
20348 old_selected_window = selected_window;
20349 XSETWINDOW (selected_window, w);
20350
20351 /* These will be set while the mode line specs are processed. */
20352 line_number_displayed = 0;
20353 wset_column_number_displayed (w, Qnil);
20354
20355 if (WINDOW_WANTS_MODELINE_P (w))
20356 {
20357 struct window *sel_w = XWINDOW (old_selected_window);
20358
20359 /* Select mode line face based on the real selected window. */
20360 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20361 BVAR (current_buffer, mode_line_format));
20362 ++n;
20363 }
20364
20365 if (WINDOW_WANTS_HEADER_LINE_P (w))
20366 {
20367 display_mode_line (w, HEADER_LINE_FACE_ID,
20368 BVAR (current_buffer, header_line_format));
20369 ++n;
20370 }
20371
20372 selected_frame = old_selected_frame;
20373 selected_window = old_selected_window;
20374 return n;
20375 }
20376
20377
20378 /* Display mode or header line of window W. FACE_ID specifies which
20379 line to display; it is either MODE_LINE_FACE_ID or
20380 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20381 display. Value is the pixel height of the mode/header line
20382 displayed. */
20383
20384 static int
20385 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20386 {
20387 struct it it;
20388 struct face *face;
20389 ptrdiff_t count = SPECPDL_INDEX ();
20390
20391 init_iterator (&it, w, -1, -1, NULL, face_id);
20392 /* Don't extend on a previously drawn mode-line.
20393 This may happen if called from pos_visible_p. */
20394 it.glyph_row->enabled_p = 0;
20395 prepare_desired_row (it.glyph_row);
20396
20397 it.glyph_row->mode_line_p = 1;
20398
20399 /* FIXME: This should be controlled by a user option. But
20400 supporting such an option is not trivial, since the mode line is
20401 made up of many separate strings. */
20402 it.paragraph_embedding = L2R;
20403
20404 record_unwind_protect (unwind_format_mode_line,
20405 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20406
20407 mode_line_target = MODE_LINE_DISPLAY;
20408
20409 /* Temporarily make frame's keyboard the current kboard so that
20410 kboard-local variables in the mode_line_format will get the right
20411 values. */
20412 push_kboard (FRAME_KBOARD (it.f));
20413 record_unwind_save_match_data ();
20414 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20415 pop_kboard ();
20416
20417 unbind_to (count, Qnil);
20418
20419 /* Fill up with spaces. */
20420 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20421
20422 compute_line_metrics (&it);
20423 it.glyph_row->full_width_p = 1;
20424 it.glyph_row->continued_p = 0;
20425 it.glyph_row->truncated_on_left_p = 0;
20426 it.glyph_row->truncated_on_right_p = 0;
20427
20428 /* Make a 3D mode-line have a shadow at its right end. */
20429 face = FACE_FROM_ID (it.f, face_id);
20430 extend_face_to_end_of_line (&it);
20431 if (face->box != FACE_NO_BOX)
20432 {
20433 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20434 + it.glyph_row->used[TEXT_AREA] - 1);
20435 last->right_box_line_p = 1;
20436 }
20437
20438 return it.glyph_row->height;
20439 }
20440
20441 /* Move element ELT in LIST to the front of LIST.
20442 Return the updated list. */
20443
20444 static Lisp_Object
20445 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20446 {
20447 register Lisp_Object tail, prev;
20448 register Lisp_Object tem;
20449
20450 tail = list;
20451 prev = Qnil;
20452 while (CONSP (tail))
20453 {
20454 tem = XCAR (tail);
20455
20456 if (EQ (elt, tem))
20457 {
20458 /* Splice out the link TAIL. */
20459 if (NILP (prev))
20460 list = XCDR (tail);
20461 else
20462 Fsetcdr (prev, XCDR (tail));
20463
20464 /* Now make it the first. */
20465 Fsetcdr (tail, list);
20466 return tail;
20467 }
20468 else
20469 prev = tail;
20470 tail = XCDR (tail);
20471 QUIT;
20472 }
20473
20474 /* Not found--return unchanged LIST. */
20475 return list;
20476 }
20477
20478 /* Contribute ELT to the mode line for window IT->w. How it
20479 translates into text depends on its data type.
20480
20481 IT describes the display environment in which we display, as usual.
20482
20483 DEPTH is the depth in recursion. It is used to prevent
20484 infinite recursion here.
20485
20486 FIELD_WIDTH is the number of characters the display of ELT should
20487 occupy in the mode line, and PRECISION is the maximum number of
20488 characters to display from ELT's representation. See
20489 display_string for details.
20490
20491 Returns the hpos of the end of the text generated by ELT.
20492
20493 PROPS is a property list to add to any string we encounter.
20494
20495 If RISKY is nonzero, remove (disregard) any properties in any string
20496 we encounter, and ignore :eval and :propertize.
20497
20498 The global variable `mode_line_target' determines whether the
20499 output is passed to `store_mode_line_noprop',
20500 `store_mode_line_string', or `display_string'. */
20501
20502 static int
20503 display_mode_element (struct it *it, int depth, int field_width, int precision,
20504 Lisp_Object elt, Lisp_Object props, int risky)
20505 {
20506 int n = 0, field, prec;
20507 int literal = 0;
20508
20509 tail_recurse:
20510 if (depth > 100)
20511 elt = build_string ("*too-deep*");
20512
20513 depth++;
20514
20515 switch (XTYPE (elt))
20516 {
20517 case Lisp_String:
20518 {
20519 /* A string: output it and check for %-constructs within it. */
20520 unsigned char c;
20521 ptrdiff_t offset = 0;
20522
20523 if (SCHARS (elt) > 0
20524 && (!NILP (props) || risky))
20525 {
20526 Lisp_Object oprops, aelt;
20527 oprops = Ftext_properties_at (make_number (0), elt);
20528
20529 /* If the starting string's properties are not what
20530 we want, translate the string. Also, if the string
20531 is risky, do that anyway. */
20532
20533 if (NILP (Fequal (props, oprops)) || risky)
20534 {
20535 /* If the starting string has properties,
20536 merge the specified ones onto the existing ones. */
20537 if (! NILP (oprops) && !risky)
20538 {
20539 Lisp_Object tem;
20540
20541 oprops = Fcopy_sequence (oprops);
20542 tem = props;
20543 while (CONSP (tem))
20544 {
20545 oprops = Fplist_put (oprops, XCAR (tem),
20546 XCAR (XCDR (tem)));
20547 tem = XCDR (XCDR (tem));
20548 }
20549 props = oprops;
20550 }
20551
20552 aelt = Fassoc (elt, mode_line_proptrans_alist);
20553 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20554 {
20555 /* AELT is what we want. Move it to the front
20556 without consing. */
20557 elt = XCAR (aelt);
20558 mode_line_proptrans_alist
20559 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20560 }
20561 else
20562 {
20563 Lisp_Object tem;
20564
20565 /* If AELT has the wrong props, it is useless.
20566 so get rid of it. */
20567 if (! NILP (aelt))
20568 mode_line_proptrans_alist
20569 = Fdelq (aelt, mode_line_proptrans_alist);
20570
20571 elt = Fcopy_sequence (elt);
20572 Fset_text_properties (make_number (0), Flength (elt),
20573 props, elt);
20574 /* Add this item to mode_line_proptrans_alist. */
20575 mode_line_proptrans_alist
20576 = Fcons (Fcons (elt, props),
20577 mode_line_proptrans_alist);
20578 /* Truncate mode_line_proptrans_alist
20579 to at most 50 elements. */
20580 tem = Fnthcdr (make_number (50),
20581 mode_line_proptrans_alist);
20582 if (! NILP (tem))
20583 XSETCDR (tem, Qnil);
20584 }
20585 }
20586 }
20587
20588 offset = 0;
20589
20590 if (literal)
20591 {
20592 prec = precision - n;
20593 switch (mode_line_target)
20594 {
20595 case MODE_LINE_NOPROP:
20596 case MODE_LINE_TITLE:
20597 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20598 break;
20599 case MODE_LINE_STRING:
20600 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20601 break;
20602 case MODE_LINE_DISPLAY:
20603 n += display_string (NULL, elt, Qnil, 0, 0, it,
20604 0, prec, 0, STRING_MULTIBYTE (elt));
20605 break;
20606 }
20607
20608 break;
20609 }
20610
20611 /* Handle the non-literal case. */
20612
20613 while ((precision <= 0 || n < precision)
20614 && SREF (elt, offset) != 0
20615 && (mode_line_target != MODE_LINE_DISPLAY
20616 || it->current_x < it->last_visible_x))
20617 {
20618 ptrdiff_t last_offset = offset;
20619
20620 /* Advance to end of string or next format specifier. */
20621 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20622 ;
20623
20624 if (offset - 1 != last_offset)
20625 {
20626 ptrdiff_t nchars, nbytes;
20627
20628 /* Output to end of string or up to '%'. Field width
20629 is length of string. Don't output more than
20630 PRECISION allows us. */
20631 offset--;
20632
20633 prec = c_string_width (SDATA (elt) + last_offset,
20634 offset - last_offset, precision - n,
20635 &nchars, &nbytes);
20636
20637 switch (mode_line_target)
20638 {
20639 case MODE_LINE_NOPROP:
20640 case MODE_LINE_TITLE:
20641 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20642 break;
20643 case MODE_LINE_STRING:
20644 {
20645 ptrdiff_t bytepos = last_offset;
20646 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20647 ptrdiff_t endpos = (precision <= 0
20648 ? string_byte_to_char (elt, offset)
20649 : charpos + nchars);
20650
20651 n += store_mode_line_string (NULL,
20652 Fsubstring (elt, make_number (charpos),
20653 make_number (endpos)),
20654 0, 0, 0, Qnil);
20655 }
20656 break;
20657 case MODE_LINE_DISPLAY:
20658 {
20659 ptrdiff_t bytepos = last_offset;
20660 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20661
20662 if (precision <= 0)
20663 nchars = string_byte_to_char (elt, offset) - charpos;
20664 n += display_string (NULL, elt, Qnil, 0, charpos,
20665 it, 0, nchars, 0,
20666 STRING_MULTIBYTE (elt));
20667 }
20668 break;
20669 }
20670 }
20671 else /* c == '%' */
20672 {
20673 ptrdiff_t percent_position = offset;
20674
20675 /* Get the specified minimum width. Zero means
20676 don't pad. */
20677 field = 0;
20678 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20679 field = field * 10 + c - '0';
20680
20681 /* Don't pad beyond the total padding allowed. */
20682 if (field_width - n > 0 && field > field_width - n)
20683 field = field_width - n;
20684
20685 /* Note that either PRECISION <= 0 or N < PRECISION. */
20686 prec = precision - n;
20687
20688 if (c == 'M')
20689 n += display_mode_element (it, depth, field, prec,
20690 Vglobal_mode_string, props,
20691 risky);
20692 else if (c != 0)
20693 {
20694 int multibyte;
20695 ptrdiff_t bytepos, charpos;
20696 const char *spec;
20697 Lisp_Object string;
20698
20699 bytepos = percent_position;
20700 charpos = (STRING_MULTIBYTE (elt)
20701 ? string_byte_to_char (elt, bytepos)
20702 : bytepos);
20703 spec = decode_mode_spec (it->w, c, field, &string);
20704 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20705
20706 switch (mode_line_target)
20707 {
20708 case MODE_LINE_NOPROP:
20709 case MODE_LINE_TITLE:
20710 n += store_mode_line_noprop (spec, field, prec);
20711 break;
20712 case MODE_LINE_STRING:
20713 {
20714 Lisp_Object tem = build_string (spec);
20715 props = Ftext_properties_at (make_number (charpos), elt);
20716 /* Should only keep face property in props */
20717 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20718 }
20719 break;
20720 case MODE_LINE_DISPLAY:
20721 {
20722 int nglyphs_before, nwritten;
20723
20724 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20725 nwritten = display_string (spec, string, elt,
20726 charpos, 0, it,
20727 field, prec, 0,
20728 multibyte);
20729
20730 /* Assign to the glyphs written above the
20731 string where the `%x' came from, position
20732 of the `%'. */
20733 if (nwritten > 0)
20734 {
20735 struct glyph *glyph
20736 = (it->glyph_row->glyphs[TEXT_AREA]
20737 + nglyphs_before);
20738 int i;
20739
20740 for (i = 0; i < nwritten; ++i)
20741 {
20742 glyph[i].object = elt;
20743 glyph[i].charpos = charpos;
20744 }
20745
20746 n += nwritten;
20747 }
20748 }
20749 break;
20750 }
20751 }
20752 else /* c == 0 */
20753 break;
20754 }
20755 }
20756 }
20757 break;
20758
20759 case Lisp_Symbol:
20760 /* A symbol: process the value of the symbol recursively
20761 as if it appeared here directly. Avoid error if symbol void.
20762 Special case: if value of symbol is a string, output the string
20763 literally. */
20764 {
20765 register Lisp_Object tem;
20766
20767 /* If the variable is not marked as risky to set
20768 then its contents are risky to use. */
20769 if (NILP (Fget (elt, Qrisky_local_variable)))
20770 risky = 1;
20771
20772 tem = Fboundp (elt);
20773 if (!NILP (tem))
20774 {
20775 tem = Fsymbol_value (elt);
20776 /* If value is a string, output that string literally:
20777 don't check for % within it. */
20778 if (STRINGP (tem))
20779 literal = 1;
20780
20781 if (!EQ (tem, elt))
20782 {
20783 /* Give up right away for nil or t. */
20784 elt = tem;
20785 goto tail_recurse;
20786 }
20787 }
20788 }
20789 break;
20790
20791 case Lisp_Cons:
20792 {
20793 register Lisp_Object car, tem;
20794
20795 /* A cons cell: five distinct cases.
20796 If first element is :eval or :propertize, do something special.
20797 If first element is a string or a cons, process all the elements
20798 and effectively concatenate them.
20799 If first element is a negative number, truncate displaying cdr to
20800 at most that many characters. If positive, pad (with spaces)
20801 to at least that many characters.
20802 If first element is a symbol, process the cadr or caddr recursively
20803 according to whether the symbol's value is non-nil or nil. */
20804 car = XCAR (elt);
20805 if (EQ (car, QCeval))
20806 {
20807 /* An element of the form (:eval FORM) means evaluate FORM
20808 and use the result as mode line elements. */
20809
20810 if (risky)
20811 break;
20812
20813 if (CONSP (XCDR (elt)))
20814 {
20815 Lisp_Object spec;
20816 spec = safe_eval (XCAR (XCDR (elt)));
20817 n += display_mode_element (it, depth, field_width - n,
20818 precision - n, spec, props,
20819 risky);
20820 }
20821 }
20822 else if (EQ (car, QCpropertize))
20823 {
20824 /* An element of the form (:propertize ELT PROPS...)
20825 means display ELT but applying properties PROPS. */
20826
20827 if (risky)
20828 break;
20829
20830 if (CONSP (XCDR (elt)))
20831 n += display_mode_element (it, depth, field_width - n,
20832 precision - n, XCAR (XCDR (elt)),
20833 XCDR (XCDR (elt)), risky);
20834 }
20835 else if (SYMBOLP (car))
20836 {
20837 tem = Fboundp (car);
20838 elt = XCDR (elt);
20839 if (!CONSP (elt))
20840 goto invalid;
20841 /* elt is now the cdr, and we know it is a cons cell.
20842 Use its car if CAR has a non-nil value. */
20843 if (!NILP (tem))
20844 {
20845 tem = Fsymbol_value (car);
20846 if (!NILP (tem))
20847 {
20848 elt = XCAR (elt);
20849 goto tail_recurse;
20850 }
20851 }
20852 /* Symbol's value is nil (or symbol is unbound)
20853 Get the cddr of the original list
20854 and if possible find the caddr and use that. */
20855 elt = XCDR (elt);
20856 if (NILP (elt))
20857 break;
20858 else if (!CONSP (elt))
20859 goto invalid;
20860 elt = XCAR (elt);
20861 goto tail_recurse;
20862 }
20863 else if (INTEGERP (car))
20864 {
20865 register int lim = XINT (car);
20866 elt = XCDR (elt);
20867 if (lim < 0)
20868 {
20869 /* Negative int means reduce maximum width. */
20870 if (precision <= 0)
20871 precision = -lim;
20872 else
20873 precision = min (precision, -lim);
20874 }
20875 else if (lim > 0)
20876 {
20877 /* Padding specified. Don't let it be more than
20878 current maximum. */
20879 if (precision > 0)
20880 lim = min (precision, lim);
20881
20882 /* If that's more padding than already wanted, queue it.
20883 But don't reduce padding already specified even if
20884 that is beyond the current truncation point. */
20885 field_width = max (lim, field_width);
20886 }
20887 goto tail_recurse;
20888 }
20889 else if (STRINGP (car) || CONSP (car))
20890 {
20891 Lisp_Object halftail = elt;
20892 int len = 0;
20893
20894 while (CONSP (elt)
20895 && (precision <= 0 || n < precision))
20896 {
20897 n += display_mode_element (it, depth,
20898 /* Do padding only after the last
20899 element in the list. */
20900 (! CONSP (XCDR (elt))
20901 ? field_width - n
20902 : 0),
20903 precision - n, XCAR (elt),
20904 props, risky);
20905 elt = XCDR (elt);
20906 len++;
20907 if ((len & 1) == 0)
20908 halftail = XCDR (halftail);
20909 /* Check for cycle. */
20910 if (EQ (halftail, elt))
20911 break;
20912 }
20913 }
20914 }
20915 break;
20916
20917 default:
20918 invalid:
20919 elt = build_string ("*invalid*");
20920 goto tail_recurse;
20921 }
20922
20923 /* Pad to FIELD_WIDTH. */
20924 if (field_width > 0 && n < field_width)
20925 {
20926 switch (mode_line_target)
20927 {
20928 case MODE_LINE_NOPROP:
20929 case MODE_LINE_TITLE:
20930 n += store_mode_line_noprop ("", field_width - n, 0);
20931 break;
20932 case MODE_LINE_STRING:
20933 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20934 break;
20935 case MODE_LINE_DISPLAY:
20936 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20937 0, 0, 0);
20938 break;
20939 }
20940 }
20941
20942 return n;
20943 }
20944
20945 /* Store a mode-line string element in mode_line_string_list.
20946
20947 If STRING is non-null, display that C string. Otherwise, the Lisp
20948 string LISP_STRING is displayed.
20949
20950 FIELD_WIDTH is the minimum number of output glyphs to produce.
20951 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20952 with spaces. FIELD_WIDTH <= 0 means don't pad.
20953
20954 PRECISION is the maximum number of characters to output from
20955 STRING. PRECISION <= 0 means don't truncate the string.
20956
20957 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20958 properties to the string.
20959
20960 PROPS are the properties to add to the string.
20961 The mode_line_string_face face property is always added to the string.
20962 */
20963
20964 static int
20965 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20966 int field_width, int precision, Lisp_Object props)
20967 {
20968 ptrdiff_t len;
20969 int n = 0;
20970
20971 if (string != NULL)
20972 {
20973 len = strlen (string);
20974 if (precision > 0 && len > precision)
20975 len = precision;
20976 lisp_string = make_string (string, len);
20977 if (NILP (props))
20978 props = mode_line_string_face_prop;
20979 else if (!NILP (mode_line_string_face))
20980 {
20981 Lisp_Object face = Fplist_get (props, Qface);
20982 props = Fcopy_sequence (props);
20983 if (NILP (face))
20984 face = mode_line_string_face;
20985 else
20986 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20987 props = Fplist_put (props, Qface, face);
20988 }
20989 Fadd_text_properties (make_number (0), make_number (len),
20990 props, lisp_string);
20991 }
20992 else
20993 {
20994 len = XFASTINT (Flength (lisp_string));
20995 if (precision > 0 && len > precision)
20996 {
20997 len = precision;
20998 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20999 precision = -1;
21000 }
21001 if (!NILP (mode_line_string_face))
21002 {
21003 Lisp_Object face;
21004 if (NILP (props))
21005 props = Ftext_properties_at (make_number (0), lisp_string);
21006 face = Fplist_get (props, Qface);
21007 if (NILP (face))
21008 face = mode_line_string_face;
21009 else
21010 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21011 props = Fcons (Qface, Fcons (face, Qnil));
21012 if (copy_string)
21013 lisp_string = Fcopy_sequence (lisp_string);
21014 }
21015 if (!NILP (props))
21016 Fadd_text_properties (make_number (0), make_number (len),
21017 props, lisp_string);
21018 }
21019
21020 if (len > 0)
21021 {
21022 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21023 n += len;
21024 }
21025
21026 if (field_width > len)
21027 {
21028 field_width -= len;
21029 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21030 if (!NILP (props))
21031 Fadd_text_properties (make_number (0), make_number (field_width),
21032 props, lisp_string);
21033 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21034 n += field_width;
21035 }
21036
21037 return n;
21038 }
21039
21040
21041 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21042 1, 4, 0,
21043 doc: /* Format a string out of a mode line format specification.
21044 First arg FORMAT specifies the mode line format (see `mode-line-format'
21045 for details) to use.
21046
21047 By default, the format is evaluated for the currently selected window.
21048
21049 Optional second arg FACE specifies the face property to put on all
21050 characters for which no face is specified. The value nil means the
21051 default face. The value t means whatever face the window's mode line
21052 currently uses (either `mode-line' or `mode-line-inactive',
21053 depending on whether the window is the selected window or not).
21054 An integer value means the value string has no text
21055 properties.
21056
21057 Optional third and fourth args WINDOW and BUFFER specify the window
21058 and buffer to use as the context for the formatting (defaults
21059 are the selected window and the WINDOW's buffer). */)
21060 (Lisp_Object format, Lisp_Object face,
21061 Lisp_Object window, Lisp_Object buffer)
21062 {
21063 struct it it;
21064 int len;
21065 struct window *w;
21066 struct buffer *old_buffer = NULL;
21067 int face_id;
21068 int no_props = INTEGERP (face);
21069 ptrdiff_t count = SPECPDL_INDEX ();
21070 Lisp_Object str;
21071 int string_start = 0;
21072
21073 if (NILP (window))
21074 window = selected_window;
21075 CHECK_WINDOW (window);
21076 w = XWINDOW (window);
21077
21078 if (NILP (buffer))
21079 buffer = w->buffer;
21080 CHECK_BUFFER (buffer);
21081
21082 /* Make formatting the modeline a non-op when noninteractive, otherwise
21083 there will be problems later caused by a partially initialized frame. */
21084 if (NILP (format) || noninteractive)
21085 return empty_unibyte_string;
21086
21087 if (no_props)
21088 face = Qnil;
21089
21090 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21091 : EQ (face, Qt) ? (EQ (window, selected_window)
21092 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21093 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21094 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21095 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21096 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21097 : DEFAULT_FACE_ID;
21098
21099 old_buffer = current_buffer;
21100
21101 /* Save things including mode_line_proptrans_alist,
21102 and set that to nil so that we don't alter the outer value. */
21103 record_unwind_protect (unwind_format_mode_line,
21104 format_mode_line_unwind_data
21105 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21106 old_buffer, selected_window, 1));
21107 mode_line_proptrans_alist = Qnil;
21108
21109 Fselect_window (window, Qt);
21110 set_buffer_internal_1 (XBUFFER (buffer));
21111
21112 init_iterator (&it, w, -1, -1, NULL, face_id);
21113
21114 if (no_props)
21115 {
21116 mode_line_target = MODE_LINE_NOPROP;
21117 mode_line_string_face_prop = Qnil;
21118 mode_line_string_list = Qnil;
21119 string_start = MODE_LINE_NOPROP_LEN (0);
21120 }
21121 else
21122 {
21123 mode_line_target = MODE_LINE_STRING;
21124 mode_line_string_list = Qnil;
21125 mode_line_string_face = face;
21126 mode_line_string_face_prop
21127 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21128 }
21129
21130 push_kboard (FRAME_KBOARD (it.f));
21131 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21132 pop_kboard ();
21133
21134 if (no_props)
21135 {
21136 len = MODE_LINE_NOPROP_LEN (string_start);
21137 str = make_string (mode_line_noprop_buf + string_start, len);
21138 }
21139 else
21140 {
21141 mode_line_string_list = Fnreverse (mode_line_string_list);
21142 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21143 empty_unibyte_string);
21144 }
21145
21146 unbind_to (count, Qnil);
21147 return str;
21148 }
21149
21150 /* Write a null-terminated, right justified decimal representation of
21151 the positive integer D to BUF using a minimal field width WIDTH. */
21152
21153 static void
21154 pint2str (register char *buf, register int width, register ptrdiff_t d)
21155 {
21156 register char *p = buf;
21157
21158 if (d <= 0)
21159 *p++ = '0';
21160 else
21161 {
21162 while (d > 0)
21163 {
21164 *p++ = d % 10 + '0';
21165 d /= 10;
21166 }
21167 }
21168
21169 for (width -= (int) (p - buf); width > 0; --width)
21170 *p++ = ' ';
21171 *p-- = '\0';
21172 while (p > buf)
21173 {
21174 d = *buf;
21175 *buf++ = *p;
21176 *p-- = d;
21177 }
21178 }
21179
21180 /* Write a null-terminated, right justified decimal and "human
21181 readable" representation of the nonnegative integer D to BUF using
21182 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21183
21184 static const char power_letter[] =
21185 {
21186 0, /* no letter */
21187 'k', /* kilo */
21188 'M', /* mega */
21189 'G', /* giga */
21190 'T', /* tera */
21191 'P', /* peta */
21192 'E', /* exa */
21193 'Z', /* zetta */
21194 'Y' /* yotta */
21195 };
21196
21197 static void
21198 pint2hrstr (char *buf, int width, ptrdiff_t d)
21199 {
21200 /* We aim to represent the nonnegative integer D as
21201 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21202 ptrdiff_t quotient = d;
21203 int remainder = 0;
21204 /* -1 means: do not use TENTHS. */
21205 int tenths = -1;
21206 int exponent = 0;
21207
21208 /* Length of QUOTIENT.TENTHS as a string. */
21209 int length;
21210
21211 char * psuffix;
21212 char * p;
21213
21214 if (1000 <= quotient)
21215 {
21216 /* Scale to the appropriate EXPONENT. */
21217 do
21218 {
21219 remainder = quotient % 1000;
21220 quotient /= 1000;
21221 exponent++;
21222 }
21223 while (1000 <= quotient);
21224
21225 /* Round to nearest and decide whether to use TENTHS or not. */
21226 if (quotient <= 9)
21227 {
21228 tenths = remainder / 100;
21229 if (50 <= remainder % 100)
21230 {
21231 if (tenths < 9)
21232 tenths++;
21233 else
21234 {
21235 quotient++;
21236 if (quotient == 10)
21237 tenths = -1;
21238 else
21239 tenths = 0;
21240 }
21241 }
21242 }
21243 else
21244 if (500 <= remainder)
21245 {
21246 if (quotient < 999)
21247 quotient++;
21248 else
21249 {
21250 quotient = 1;
21251 exponent++;
21252 tenths = 0;
21253 }
21254 }
21255 }
21256
21257 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21258 if (tenths == -1 && quotient <= 99)
21259 if (quotient <= 9)
21260 length = 1;
21261 else
21262 length = 2;
21263 else
21264 length = 3;
21265 p = psuffix = buf + max (width, length);
21266
21267 /* Print EXPONENT. */
21268 *psuffix++ = power_letter[exponent];
21269 *psuffix = '\0';
21270
21271 /* Print TENTHS. */
21272 if (tenths >= 0)
21273 {
21274 *--p = '0' + tenths;
21275 *--p = '.';
21276 }
21277
21278 /* Print QUOTIENT. */
21279 do
21280 {
21281 int digit = quotient % 10;
21282 *--p = '0' + digit;
21283 }
21284 while ((quotient /= 10) != 0);
21285
21286 /* Print leading spaces. */
21287 while (buf < p)
21288 *--p = ' ';
21289 }
21290
21291 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21292 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21293 type of CODING_SYSTEM. Return updated pointer into BUF. */
21294
21295 static unsigned char invalid_eol_type[] = "(*invalid*)";
21296
21297 static char *
21298 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21299 {
21300 Lisp_Object val;
21301 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21302 const unsigned char *eol_str;
21303 int eol_str_len;
21304 /* The EOL conversion we are using. */
21305 Lisp_Object eoltype;
21306
21307 val = CODING_SYSTEM_SPEC (coding_system);
21308 eoltype = Qnil;
21309
21310 if (!VECTORP (val)) /* Not yet decided. */
21311 {
21312 *buf++ = multibyte ? '-' : ' ';
21313 if (eol_flag)
21314 eoltype = eol_mnemonic_undecided;
21315 /* Don't mention EOL conversion if it isn't decided. */
21316 }
21317 else
21318 {
21319 Lisp_Object attrs;
21320 Lisp_Object eolvalue;
21321
21322 attrs = AREF (val, 0);
21323 eolvalue = AREF (val, 2);
21324
21325 *buf++ = multibyte
21326 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21327 : ' ';
21328
21329 if (eol_flag)
21330 {
21331 /* The EOL conversion that is normal on this system. */
21332
21333 if (NILP (eolvalue)) /* Not yet decided. */
21334 eoltype = eol_mnemonic_undecided;
21335 else if (VECTORP (eolvalue)) /* Not yet decided. */
21336 eoltype = eol_mnemonic_undecided;
21337 else /* eolvalue is Qunix, Qdos, or Qmac. */
21338 eoltype = (EQ (eolvalue, Qunix)
21339 ? eol_mnemonic_unix
21340 : (EQ (eolvalue, Qdos) == 1
21341 ? eol_mnemonic_dos : eol_mnemonic_mac));
21342 }
21343 }
21344
21345 if (eol_flag)
21346 {
21347 /* Mention the EOL conversion if it is not the usual one. */
21348 if (STRINGP (eoltype))
21349 {
21350 eol_str = SDATA (eoltype);
21351 eol_str_len = SBYTES (eoltype);
21352 }
21353 else if (CHARACTERP (eoltype))
21354 {
21355 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21356 int c = XFASTINT (eoltype);
21357 eol_str_len = CHAR_STRING (c, tmp);
21358 eol_str = tmp;
21359 }
21360 else
21361 {
21362 eol_str = invalid_eol_type;
21363 eol_str_len = sizeof (invalid_eol_type) - 1;
21364 }
21365 memcpy (buf, eol_str, eol_str_len);
21366 buf += eol_str_len;
21367 }
21368
21369 return buf;
21370 }
21371
21372 /* Return a string for the output of a mode line %-spec for window W,
21373 generated by character C. FIELD_WIDTH > 0 means pad the string
21374 returned with spaces to that value. Return a Lisp string in
21375 *STRING if the resulting string is taken from that Lisp string.
21376
21377 Note we operate on the current buffer for most purposes,
21378 the exception being w->base_line_pos. */
21379
21380 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21381
21382 static const char *
21383 decode_mode_spec (struct window *w, register int c, int field_width,
21384 Lisp_Object *string)
21385 {
21386 Lisp_Object obj;
21387 struct frame *f = XFRAME (WINDOW_FRAME (w));
21388 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21389 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21390 produce strings from numerical values, so limit preposterously
21391 large values of FIELD_WIDTH to avoid overrunning the buffer's
21392 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21393 bytes plus the terminating null. */
21394 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21395 struct buffer *b = current_buffer;
21396
21397 obj = Qnil;
21398 *string = Qnil;
21399
21400 switch (c)
21401 {
21402 case '*':
21403 if (!NILP (BVAR (b, read_only)))
21404 return "%";
21405 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21406 return "*";
21407 return "-";
21408
21409 case '+':
21410 /* This differs from %* only for a modified read-only buffer. */
21411 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21412 return "*";
21413 if (!NILP (BVAR (b, read_only)))
21414 return "%";
21415 return "-";
21416
21417 case '&':
21418 /* This differs from %* in ignoring read-only-ness. */
21419 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21420 return "*";
21421 return "-";
21422
21423 case '%':
21424 return "%";
21425
21426 case '[':
21427 {
21428 int i;
21429 char *p;
21430
21431 if (command_loop_level > 5)
21432 return "[[[... ";
21433 p = decode_mode_spec_buf;
21434 for (i = 0; i < command_loop_level; i++)
21435 *p++ = '[';
21436 *p = 0;
21437 return decode_mode_spec_buf;
21438 }
21439
21440 case ']':
21441 {
21442 int i;
21443 char *p;
21444
21445 if (command_loop_level > 5)
21446 return " ...]]]";
21447 p = decode_mode_spec_buf;
21448 for (i = 0; i < command_loop_level; i++)
21449 *p++ = ']';
21450 *p = 0;
21451 return decode_mode_spec_buf;
21452 }
21453
21454 case '-':
21455 {
21456 register int i;
21457
21458 /* Let lots_of_dashes be a string of infinite length. */
21459 if (mode_line_target == MODE_LINE_NOPROP ||
21460 mode_line_target == MODE_LINE_STRING)
21461 return "--";
21462 if (field_width <= 0
21463 || field_width > sizeof (lots_of_dashes))
21464 {
21465 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21466 decode_mode_spec_buf[i] = '-';
21467 decode_mode_spec_buf[i] = '\0';
21468 return decode_mode_spec_buf;
21469 }
21470 else
21471 return lots_of_dashes;
21472 }
21473
21474 case 'b':
21475 obj = BVAR (b, name);
21476 break;
21477
21478 case 'c':
21479 /* %c and %l are ignored in `frame-title-format'.
21480 (In redisplay_internal, the frame title is drawn _before_ the
21481 windows are updated, so the stuff which depends on actual
21482 window contents (such as %l) may fail to render properly, or
21483 even crash emacs.) */
21484 if (mode_line_target == MODE_LINE_TITLE)
21485 return "";
21486 else
21487 {
21488 ptrdiff_t col = current_column ();
21489 wset_column_number_displayed (w, make_number (col));
21490 pint2str (decode_mode_spec_buf, width, col);
21491 return decode_mode_spec_buf;
21492 }
21493
21494 case 'e':
21495 #ifndef SYSTEM_MALLOC
21496 {
21497 if (NILP (Vmemory_full))
21498 return "";
21499 else
21500 return "!MEM FULL! ";
21501 }
21502 #else
21503 return "";
21504 #endif
21505
21506 case 'F':
21507 /* %F displays the frame name. */
21508 if (!NILP (f->title))
21509 return SSDATA (f->title);
21510 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21511 return SSDATA (f->name);
21512 return "Emacs";
21513
21514 case 'f':
21515 obj = BVAR (b, filename);
21516 break;
21517
21518 case 'i':
21519 {
21520 ptrdiff_t size = ZV - BEGV;
21521 pint2str (decode_mode_spec_buf, width, size);
21522 return decode_mode_spec_buf;
21523 }
21524
21525 case 'I':
21526 {
21527 ptrdiff_t size = ZV - BEGV;
21528 pint2hrstr (decode_mode_spec_buf, width, size);
21529 return decode_mode_spec_buf;
21530 }
21531
21532 case 'l':
21533 {
21534 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21535 ptrdiff_t topline, nlines, height;
21536 ptrdiff_t junk;
21537
21538 /* %c and %l are ignored in `frame-title-format'. */
21539 if (mode_line_target == MODE_LINE_TITLE)
21540 return "";
21541
21542 startpos = XMARKER (w->start)->charpos;
21543 startpos_byte = marker_byte_position (w->start);
21544 height = WINDOW_TOTAL_LINES (w);
21545
21546 /* If we decided that this buffer isn't suitable for line numbers,
21547 don't forget that too fast. */
21548 if (EQ (w->base_line_pos, w->buffer))
21549 goto no_value;
21550 /* But do forget it, if the window shows a different buffer now. */
21551 else if (BUFFERP (w->base_line_pos))
21552 wset_base_line_pos (w, Qnil);
21553
21554 /* If the buffer is very big, don't waste time. */
21555 if (INTEGERP (Vline_number_display_limit)
21556 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21557 {
21558 wset_base_line_pos (w, Qnil);
21559 wset_base_line_number (w, Qnil);
21560 goto no_value;
21561 }
21562
21563 if (INTEGERP (w->base_line_number)
21564 && INTEGERP (w->base_line_pos)
21565 && XFASTINT (w->base_line_pos) <= startpos)
21566 {
21567 line = XFASTINT (w->base_line_number);
21568 linepos = XFASTINT (w->base_line_pos);
21569 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21570 }
21571 else
21572 {
21573 line = 1;
21574 linepos = BUF_BEGV (b);
21575 linepos_byte = BUF_BEGV_BYTE (b);
21576 }
21577
21578 /* Count lines from base line to window start position. */
21579 nlines = display_count_lines (linepos_byte,
21580 startpos_byte,
21581 startpos, &junk);
21582
21583 topline = nlines + line;
21584
21585 /* Determine a new base line, if the old one is too close
21586 or too far away, or if we did not have one.
21587 "Too close" means it's plausible a scroll-down would
21588 go back past it. */
21589 if (startpos == BUF_BEGV (b))
21590 {
21591 wset_base_line_number (w, make_number (topline));
21592 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21593 }
21594 else if (nlines < height + 25 || nlines > height * 3 + 50
21595 || linepos == BUF_BEGV (b))
21596 {
21597 ptrdiff_t limit = BUF_BEGV (b);
21598 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21599 ptrdiff_t position;
21600 ptrdiff_t distance =
21601 (height * 2 + 30) * line_number_display_limit_width;
21602
21603 if (startpos - distance > limit)
21604 {
21605 limit = startpos - distance;
21606 limit_byte = CHAR_TO_BYTE (limit);
21607 }
21608
21609 nlines = display_count_lines (startpos_byte,
21610 limit_byte,
21611 - (height * 2 + 30),
21612 &position);
21613 /* If we couldn't find the lines we wanted within
21614 line_number_display_limit_width chars per line,
21615 give up on line numbers for this window. */
21616 if (position == limit_byte && limit == startpos - distance)
21617 {
21618 wset_base_line_pos (w, w->buffer);
21619 wset_base_line_number (w, Qnil);
21620 goto no_value;
21621 }
21622
21623 wset_base_line_number (w, make_number (topline - nlines));
21624 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21625 }
21626
21627 /* Now count lines from the start pos to point. */
21628 nlines = display_count_lines (startpos_byte,
21629 PT_BYTE, PT, &junk);
21630
21631 /* Record that we did display the line number. */
21632 line_number_displayed = 1;
21633
21634 /* Make the string to show. */
21635 pint2str (decode_mode_spec_buf, width, topline + nlines);
21636 return decode_mode_spec_buf;
21637 no_value:
21638 {
21639 char* p = decode_mode_spec_buf;
21640 int pad = width - 2;
21641 while (pad-- > 0)
21642 *p++ = ' ';
21643 *p++ = '?';
21644 *p++ = '?';
21645 *p = '\0';
21646 return decode_mode_spec_buf;
21647 }
21648 }
21649 break;
21650
21651 case 'm':
21652 obj = BVAR (b, mode_name);
21653 break;
21654
21655 case 'n':
21656 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21657 return " Narrow";
21658 break;
21659
21660 case 'p':
21661 {
21662 ptrdiff_t pos = marker_position (w->start);
21663 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21664
21665 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21666 {
21667 if (pos <= BUF_BEGV (b))
21668 return "All";
21669 else
21670 return "Bottom";
21671 }
21672 else if (pos <= BUF_BEGV (b))
21673 return "Top";
21674 else
21675 {
21676 if (total > 1000000)
21677 /* Do it differently for a large value, to avoid overflow. */
21678 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21679 else
21680 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21681 /* We can't normally display a 3-digit number,
21682 so get us a 2-digit number that is close. */
21683 if (total == 100)
21684 total = 99;
21685 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21686 return decode_mode_spec_buf;
21687 }
21688 }
21689
21690 /* Display percentage of size above the bottom of the screen. */
21691 case 'P':
21692 {
21693 ptrdiff_t toppos = marker_position (w->start);
21694 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21695 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21696
21697 if (botpos >= BUF_ZV (b))
21698 {
21699 if (toppos <= BUF_BEGV (b))
21700 return "All";
21701 else
21702 return "Bottom";
21703 }
21704 else
21705 {
21706 if (total > 1000000)
21707 /* Do it differently for a large value, to avoid overflow. */
21708 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21709 else
21710 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21711 /* We can't normally display a 3-digit number,
21712 so get us a 2-digit number that is close. */
21713 if (total == 100)
21714 total = 99;
21715 if (toppos <= BUF_BEGV (b))
21716 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21717 else
21718 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21719 return decode_mode_spec_buf;
21720 }
21721 }
21722
21723 case 's':
21724 /* status of process */
21725 obj = Fget_buffer_process (Fcurrent_buffer ());
21726 if (NILP (obj))
21727 return "no process";
21728 #ifndef MSDOS
21729 obj = Fsymbol_name (Fprocess_status (obj));
21730 #endif
21731 break;
21732
21733 case '@':
21734 {
21735 ptrdiff_t count = inhibit_garbage_collection ();
21736 Lisp_Object val = call1 (intern ("file-remote-p"),
21737 BVAR (current_buffer, directory));
21738 unbind_to (count, Qnil);
21739
21740 if (NILP (val))
21741 return "-";
21742 else
21743 return "@";
21744 }
21745
21746 case 't': /* indicate TEXT or BINARY */
21747 return "T";
21748
21749 case 'z':
21750 /* coding-system (not including end-of-line format) */
21751 case 'Z':
21752 /* coding-system (including end-of-line type) */
21753 {
21754 int eol_flag = (c == 'Z');
21755 char *p = decode_mode_spec_buf;
21756
21757 if (! FRAME_WINDOW_P (f))
21758 {
21759 /* No need to mention EOL here--the terminal never needs
21760 to do EOL conversion. */
21761 p = decode_mode_spec_coding (CODING_ID_NAME
21762 (FRAME_KEYBOARD_CODING (f)->id),
21763 p, 0);
21764 p = decode_mode_spec_coding (CODING_ID_NAME
21765 (FRAME_TERMINAL_CODING (f)->id),
21766 p, 0);
21767 }
21768 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21769 p, eol_flag);
21770
21771 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21772 #ifdef subprocesses
21773 obj = Fget_buffer_process (Fcurrent_buffer ());
21774 if (PROCESSP (obj))
21775 {
21776 p = decode_mode_spec_coding
21777 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21778 p = decode_mode_spec_coding
21779 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21780 }
21781 #endif /* subprocesses */
21782 #endif /* 0 */
21783 *p = 0;
21784 return decode_mode_spec_buf;
21785 }
21786 }
21787
21788 if (STRINGP (obj))
21789 {
21790 *string = obj;
21791 return SSDATA (obj);
21792 }
21793 else
21794 return "";
21795 }
21796
21797
21798 /* Count up to COUNT lines starting from START_BYTE.
21799 But don't go beyond LIMIT_BYTE.
21800 Return the number of lines thus found (always nonnegative).
21801
21802 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21803
21804 static ptrdiff_t
21805 display_count_lines (ptrdiff_t start_byte,
21806 ptrdiff_t limit_byte, ptrdiff_t count,
21807 ptrdiff_t *byte_pos_ptr)
21808 {
21809 register unsigned char *cursor;
21810 unsigned char *base;
21811
21812 register ptrdiff_t ceiling;
21813 register unsigned char *ceiling_addr;
21814 ptrdiff_t orig_count = count;
21815
21816 /* If we are not in selective display mode,
21817 check only for newlines. */
21818 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21819 && !INTEGERP (BVAR (current_buffer, selective_display)));
21820
21821 if (count > 0)
21822 {
21823 while (start_byte < limit_byte)
21824 {
21825 ceiling = BUFFER_CEILING_OF (start_byte);
21826 ceiling = min (limit_byte - 1, ceiling);
21827 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21828 base = (cursor = BYTE_POS_ADDR (start_byte));
21829 while (1)
21830 {
21831 if (selective_display)
21832 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21833 ;
21834 else
21835 while (*cursor != '\n' && ++cursor != ceiling_addr)
21836 ;
21837
21838 if (cursor != ceiling_addr)
21839 {
21840 if (--count == 0)
21841 {
21842 start_byte += cursor - base + 1;
21843 *byte_pos_ptr = start_byte;
21844 return orig_count;
21845 }
21846 else
21847 if (++cursor == ceiling_addr)
21848 break;
21849 }
21850 else
21851 break;
21852 }
21853 start_byte += cursor - base;
21854 }
21855 }
21856 else
21857 {
21858 while (start_byte > limit_byte)
21859 {
21860 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21861 ceiling = max (limit_byte, ceiling);
21862 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21863 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21864 while (1)
21865 {
21866 if (selective_display)
21867 while (--cursor != ceiling_addr
21868 && *cursor != '\n' && *cursor != 015)
21869 ;
21870 else
21871 while (--cursor != ceiling_addr && *cursor != '\n')
21872 ;
21873
21874 if (cursor != ceiling_addr)
21875 {
21876 if (++count == 0)
21877 {
21878 start_byte += cursor - base + 1;
21879 *byte_pos_ptr = start_byte;
21880 /* When scanning backwards, we should
21881 not count the newline posterior to which we stop. */
21882 return - orig_count - 1;
21883 }
21884 }
21885 else
21886 break;
21887 }
21888 /* Here we add 1 to compensate for the last decrement
21889 of CURSOR, which took it past the valid range. */
21890 start_byte += cursor - base + 1;
21891 }
21892 }
21893
21894 *byte_pos_ptr = limit_byte;
21895
21896 if (count < 0)
21897 return - orig_count + count;
21898 return orig_count - count;
21899
21900 }
21901
21902
21903 \f
21904 /***********************************************************************
21905 Displaying strings
21906 ***********************************************************************/
21907
21908 /* Display a NUL-terminated string, starting with index START.
21909
21910 If STRING is non-null, display that C string. Otherwise, the Lisp
21911 string LISP_STRING is displayed. There's a case that STRING is
21912 non-null and LISP_STRING is not nil. It means STRING is a string
21913 data of LISP_STRING. In that case, we display LISP_STRING while
21914 ignoring its text properties.
21915
21916 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21917 FACE_STRING. Display STRING or LISP_STRING with the face at
21918 FACE_STRING_POS in FACE_STRING:
21919
21920 Display the string in the environment given by IT, but use the
21921 standard display table, temporarily.
21922
21923 FIELD_WIDTH is the minimum number of output glyphs to produce.
21924 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21925 with spaces. If STRING has more characters, more than FIELD_WIDTH
21926 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21927
21928 PRECISION is the maximum number of characters to output from
21929 STRING. PRECISION < 0 means don't truncate the string.
21930
21931 This is roughly equivalent to printf format specifiers:
21932
21933 FIELD_WIDTH PRECISION PRINTF
21934 ----------------------------------------
21935 -1 -1 %s
21936 -1 10 %.10s
21937 10 -1 %10s
21938 20 10 %20.10s
21939
21940 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21941 display them, and < 0 means obey the current buffer's value of
21942 enable_multibyte_characters.
21943
21944 Value is the number of columns displayed. */
21945
21946 static int
21947 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21948 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21949 int field_width, int precision, int max_x, int multibyte)
21950 {
21951 int hpos_at_start = it->hpos;
21952 int saved_face_id = it->face_id;
21953 struct glyph_row *row = it->glyph_row;
21954 ptrdiff_t it_charpos;
21955
21956 /* Initialize the iterator IT for iteration over STRING beginning
21957 with index START. */
21958 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21959 precision, field_width, multibyte);
21960 if (string && STRINGP (lisp_string))
21961 /* LISP_STRING is the one returned by decode_mode_spec. We should
21962 ignore its text properties. */
21963 it->stop_charpos = it->end_charpos;
21964
21965 /* If displaying STRING, set up the face of the iterator from
21966 FACE_STRING, if that's given. */
21967 if (STRINGP (face_string))
21968 {
21969 ptrdiff_t endptr;
21970 struct face *face;
21971
21972 it->face_id
21973 = face_at_string_position (it->w, face_string, face_string_pos,
21974 0, it->region_beg_charpos,
21975 it->region_end_charpos,
21976 &endptr, it->base_face_id, 0);
21977 face = FACE_FROM_ID (it->f, it->face_id);
21978 it->face_box_p = face->box != FACE_NO_BOX;
21979 }
21980
21981 /* Set max_x to the maximum allowed X position. Don't let it go
21982 beyond the right edge of the window. */
21983 if (max_x <= 0)
21984 max_x = it->last_visible_x;
21985 else
21986 max_x = min (max_x, it->last_visible_x);
21987
21988 /* Skip over display elements that are not visible. because IT->w is
21989 hscrolled. */
21990 if (it->current_x < it->first_visible_x)
21991 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21992 MOVE_TO_POS | MOVE_TO_X);
21993
21994 row->ascent = it->max_ascent;
21995 row->height = it->max_ascent + it->max_descent;
21996 row->phys_ascent = it->max_phys_ascent;
21997 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21998 row->extra_line_spacing = it->max_extra_line_spacing;
21999
22000 if (STRINGP (it->string))
22001 it_charpos = IT_STRING_CHARPOS (*it);
22002 else
22003 it_charpos = IT_CHARPOS (*it);
22004
22005 /* This condition is for the case that we are called with current_x
22006 past last_visible_x. */
22007 while (it->current_x < max_x)
22008 {
22009 int x_before, x, n_glyphs_before, i, nglyphs;
22010
22011 /* Get the next display element. */
22012 if (!get_next_display_element (it))
22013 break;
22014
22015 /* Produce glyphs. */
22016 x_before = it->current_x;
22017 n_glyphs_before = row->used[TEXT_AREA];
22018 PRODUCE_GLYPHS (it);
22019
22020 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22021 i = 0;
22022 x = x_before;
22023 while (i < nglyphs)
22024 {
22025 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22026
22027 if (it->line_wrap != TRUNCATE
22028 && x + glyph->pixel_width > max_x)
22029 {
22030 /* End of continued line or max_x reached. */
22031 if (CHAR_GLYPH_PADDING_P (*glyph))
22032 {
22033 /* A wide character is unbreakable. */
22034 if (row->reversed_p)
22035 unproduce_glyphs (it, row->used[TEXT_AREA]
22036 - n_glyphs_before);
22037 row->used[TEXT_AREA] = n_glyphs_before;
22038 it->current_x = x_before;
22039 }
22040 else
22041 {
22042 if (row->reversed_p)
22043 unproduce_glyphs (it, row->used[TEXT_AREA]
22044 - (n_glyphs_before + i));
22045 row->used[TEXT_AREA] = n_glyphs_before + i;
22046 it->current_x = x;
22047 }
22048 break;
22049 }
22050 else if (x + glyph->pixel_width >= it->first_visible_x)
22051 {
22052 /* Glyph is at least partially visible. */
22053 ++it->hpos;
22054 if (x < it->first_visible_x)
22055 row->x = x - it->first_visible_x;
22056 }
22057 else
22058 {
22059 /* Glyph is off the left margin of the display area.
22060 Should not happen. */
22061 emacs_abort ();
22062 }
22063
22064 row->ascent = max (row->ascent, it->max_ascent);
22065 row->height = max (row->height, it->max_ascent + it->max_descent);
22066 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22067 row->phys_height = max (row->phys_height,
22068 it->max_phys_ascent + it->max_phys_descent);
22069 row->extra_line_spacing = max (row->extra_line_spacing,
22070 it->max_extra_line_spacing);
22071 x += glyph->pixel_width;
22072 ++i;
22073 }
22074
22075 /* Stop if max_x reached. */
22076 if (i < nglyphs)
22077 break;
22078
22079 /* Stop at line ends. */
22080 if (ITERATOR_AT_END_OF_LINE_P (it))
22081 {
22082 it->continuation_lines_width = 0;
22083 break;
22084 }
22085
22086 set_iterator_to_next (it, 1);
22087 if (STRINGP (it->string))
22088 it_charpos = IT_STRING_CHARPOS (*it);
22089 else
22090 it_charpos = IT_CHARPOS (*it);
22091
22092 /* Stop if truncating at the right edge. */
22093 if (it->line_wrap == TRUNCATE
22094 && it->current_x >= it->last_visible_x)
22095 {
22096 /* Add truncation mark, but don't do it if the line is
22097 truncated at a padding space. */
22098 if (it_charpos < it->string_nchars)
22099 {
22100 if (!FRAME_WINDOW_P (it->f))
22101 {
22102 int ii, n;
22103
22104 if (it->current_x > it->last_visible_x)
22105 {
22106 if (!row->reversed_p)
22107 {
22108 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22109 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22110 break;
22111 }
22112 else
22113 {
22114 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22115 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22116 break;
22117 unproduce_glyphs (it, ii + 1);
22118 ii = row->used[TEXT_AREA] - (ii + 1);
22119 }
22120 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22121 {
22122 row->used[TEXT_AREA] = ii;
22123 produce_special_glyphs (it, IT_TRUNCATION);
22124 }
22125 }
22126 produce_special_glyphs (it, IT_TRUNCATION);
22127 }
22128 row->truncated_on_right_p = 1;
22129 }
22130 break;
22131 }
22132 }
22133
22134 /* Maybe insert a truncation at the left. */
22135 if (it->first_visible_x
22136 && it_charpos > 0)
22137 {
22138 if (!FRAME_WINDOW_P (it->f)
22139 || (row->reversed_p
22140 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22141 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22142 insert_left_trunc_glyphs (it);
22143 row->truncated_on_left_p = 1;
22144 }
22145
22146 it->face_id = saved_face_id;
22147
22148 /* Value is number of columns displayed. */
22149 return it->hpos - hpos_at_start;
22150 }
22151
22152
22153 \f
22154 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22155 appears as an element of LIST or as the car of an element of LIST.
22156 If PROPVAL is a list, compare each element against LIST in that
22157 way, and return 1/2 if any element of PROPVAL is found in LIST.
22158 Otherwise return 0. This function cannot quit.
22159 The return value is 2 if the text is invisible but with an ellipsis
22160 and 1 if it's invisible and without an ellipsis. */
22161
22162 int
22163 invisible_p (register Lisp_Object propval, Lisp_Object list)
22164 {
22165 register Lisp_Object tail, proptail;
22166
22167 for (tail = list; CONSP (tail); tail = XCDR (tail))
22168 {
22169 register Lisp_Object tem;
22170 tem = XCAR (tail);
22171 if (EQ (propval, tem))
22172 return 1;
22173 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22174 return NILP (XCDR (tem)) ? 1 : 2;
22175 }
22176
22177 if (CONSP (propval))
22178 {
22179 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22180 {
22181 Lisp_Object propelt;
22182 propelt = XCAR (proptail);
22183 for (tail = list; CONSP (tail); tail = XCDR (tail))
22184 {
22185 register Lisp_Object tem;
22186 tem = XCAR (tail);
22187 if (EQ (propelt, tem))
22188 return 1;
22189 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22190 return NILP (XCDR (tem)) ? 1 : 2;
22191 }
22192 }
22193 }
22194
22195 return 0;
22196 }
22197
22198 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22199 doc: /* Non-nil if the property makes the text invisible.
22200 POS-OR-PROP can be a marker or number, in which case it is taken to be
22201 a position in the current buffer and the value of the `invisible' property
22202 is checked; or it can be some other value, which is then presumed to be the
22203 value of the `invisible' property of the text of interest.
22204 The non-nil value returned can be t for truly invisible text or something
22205 else if the text is replaced by an ellipsis. */)
22206 (Lisp_Object pos_or_prop)
22207 {
22208 Lisp_Object prop
22209 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22210 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22211 : pos_or_prop);
22212 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22213 return (invis == 0 ? Qnil
22214 : invis == 1 ? Qt
22215 : make_number (invis));
22216 }
22217
22218 /* Calculate a width or height in pixels from a specification using
22219 the following elements:
22220
22221 SPEC ::=
22222 NUM - a (fractional) multiple of the default font width/height
22223 (NUM) - specifies exactly NUM pixels
22224 UNIT - a fixed number of pixels, see below.
22225 ELEMENT - size of a display element in pixels, see below.
22226 (NUM . SPEC) - equals NUM * SPEC
22227 (+ SPEC SPEC ...) - add pixel values
22228 (- SPEC SPEC ...) - subtract pixel values
22229 (- SPEC) - negate pixel value
22230
22231 NUM ::=
22232 INT or FLOAT - a number constant
22233 SYMBOL - use symbol's (buffer local) variable binding.
22234
22235 UNIT ::=
22236 in - pixels per inch *)
22237 mm - pixels per 1/1000 meter *)
22238 cm - pixels per 1/100 meter *)
22239 width - width of current font in pixels.
22240 height - height of current font in pixels.
22241
22242 *) using the ratio(s) defined in display-pixels-per-inch.
22243
22244 ELEMENT ::=
22245
22246 left-fringe - left fringe width in pixels
22247 right-fringe - right fringe width in pixels
22248
22249 left-margin - left margin width in pixels
22250 right-margin - right margin width in pixels
22251
22252 scroll-bar - scroll-bar area width in pixels
22253
22254 Examples:
22255
22256 Pixels corresponding to 5 inches:
22257 (5 . in)
22258
22259 Total width of non-text areas on left side of window (if scroll-bar is on left):
22260 '(space :width (+ left-fringe left-margin scroll-bar))
22261
22262 Align to first text column (in header line):
22263 '(space :align-to 0)
22264
22265 Align to middle of text area minus half the width of variable `my-image'
22266 containing a loaded image:
22267 '(space :align-to (0.5 . (- text my-image)))
22268
22269 Width of left margin minus width of 1 character in the default font:
22270 '(space :width (- left-margin 1))
22271
22272 Width of left margin minus width of 2 characters in the current font:
22273 '(space :width (- left-margin (2 . width)))
22274
22275 Center 1 character over left-margin (in header line):
22276 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22277
22278 Different ways to express width of left fringe plus left margin minus one pixel:
22279 '(space :width (- (+ left-fringe left-margin) (1)))
22280 '(space :width (+ left-fringe left-margin (- (1))))
22281 '(space :width (+ left-fringe left-margin (-1)))
22282
22283 */
22284
22285 #define NUMVAL(X) \
22286 ((INTEGERP (X) || FLOATP (X)) \
22287 ? XFLOATINT (X) \
22288 : - 1)
22289
22290 static int
22291 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22292 struct font *font, int width_p, int *align_to)
22293 {
22294 double pixels;
22295
22296 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22297 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22298
22299 if (NILP (prop))
22300 return OK_PIXELS (0);
22301
22302 eassert (FRAME_LIVE_P (it->f));
22303
22304 if (SYMBOLP (prop))
22305 {
22306 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22307 {
22308 char *unit = SSDATA (SYMBOL_NAME (prop));
22309
22310 if (unit[0] == 'i' && unit[1] == 'n')
22311 pixels = 1.0;
22312 else if (unit[0] == 'm' && unit[1] == 'm')
22313 pixels = 25.4;
22314 else if (unit[0] == 'c' && unit[1] == 'm')
22315 pixels = 2.54;
22316 else
22317 pixels = 0;
22318 if (pixels > 0)
22319 {
22320 double ppi;
22321 #ifdef HAVE_WINDOW_SYSTEM
22322 if (FRAME_WINDOW_P (it->f)
22323 && (ppi = (width_p
22324 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22325 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22326 ppi > 0))
22327 return OK_PIXELS (ppi / pixels);
22328 #endif
22329
22330 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22331 || (CONSP (Vdisplay_pixels_per_inch)
22332 && (ppi = (width_p
22333 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22334 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22335 ppi > 0)))
22336 return OK_PIXELS (ppi / pixels);
22337
22338 return 0;
22339 }
22340 }
22341
22342 #ifdef HAVE_WINDOW_SYSTEM
22343 if (EQ (prop, Qheight))
22344 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22345 if (EQ (prop, Qwidth))
22346 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22347 #else
22348 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22349 return OK_PIXELS (1);
22350 #endif
22351
22352 if (EQ (prop, Qtext))
22353 return OK_PIXELS (width_p
22354 ? window_box_width (it->w, TEXT_AREA)
22355 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22356
22357 if (align_to && *align_to < 0)
22358 {
22359 *res = 0;
22360 if (EQ (prop, Qleft))
22361 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22362 if (EQ (prop, Qright))
22363 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22364 if (EQ (prop, Qcenter))
22365 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22366 + window_box_width (it->w, TEXT_AREA) / 2);
22367 if (EQ (prop, Qleft_fringe))
22368 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22369 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22370 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22371 if (EQ (prop, Qright_fringe))
22372 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22373 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22374 : window_box_right_offset (it->w, TEXT_AREA));
22375 if (EQ (prop, Qleft_margin))
22376 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22377 if (EQ (prop, Qright_margin))
22378 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22379 if (EQ (prop, Qscroll_bar))
22380 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22381 ? 0
22382 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22383 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22384 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22385 : 0)));
22386 }
22387 else
22388 {
22389 if (EQ (prop, Qleft_fringe))
22390 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22391 if (EQ (prop, Qright_fringe))
22392 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22393 if (EQ (prop, Qleft_margin))
22394 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22395 if (EQ (prop, Qright_margin))
22396 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22397 if (EQ (prop, Qscroll_bar))
22398 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22399 }
22400
22401 prop = buffer_local_value_1 (prop, it->w->buffer);
22402 if (EQ (prop, Qunbound))
22403 prop = Qnil;
22404 }
22405
22406 if (INTEGERP (prop) || FLOATP (prop))
22407 {
22408 int base_unit = (width_p
22409 ? FRAME_COLUMN_WIDTH (it->f)
22410 : FRAME_LINE_HEIGHT (it->f));
22411 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22412 }
22413
22414 if (CONSP (prop))
22415 {
22416 Lisp_Object car = XCAR (prop);
22417 Lisp_Object cdr = XCDR (prop);
22418
22419 if (SYMBOLP (car))
22420 {
22421 #ifdef HAVE_WINDOW_SYSTEM
22422 if (FRAME_WINDOW_P (it->f)
22423 && valid_image_p (prop))
22424 {
22425 ptrdiff_t id = lookup_image (it->f, prop);
22426 struct image *img = IMAGE_FROM_ID (it->f, id);
22427
22428 return OK_PIXELS (width_p ? img->width : img->height);
22429 }
22430 #endif
22431 if (EQ (car, Qplus) || EQ (car, Qminus))
22432 {
22433 int first = 1;
22434 double px;
22435
22436 pixels = 0;
22437 while (CONSP (cdr))
22438 {
22439 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22440 font, width_p, align_to))
22441 return 0;
22442 if (first)
22443 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22444 else
22445 pixels += px;
22446 cdr = XCDR (cdr);
22447 }
22448 if (EQ (car, Qminus))
22449 pixels = -pixels;
22450 return OK_PIXELS (pixels);
22451 }
22452
22453 car = buffer_local_value_1 (car, it->w->buffer);
22454 if (EQ (car, Qunbound))
22455 car = Qnil;
22456 }
22457
22458 if (INTEGERP (car) || FLOATP (car))
22459 {
22460 double fact;
22461 pixels = XFLOATINT (car);
22462 if (NILP (cdr))
22463 return OK_PIXELS (pixels);
22464 if (calc_pixel_width_or_height (&fact, it, cdr,
22465 font, width_p, align_to))
22466 return OK_PIXELS (pixels * fact);
22467 return 0;
22468 }
22469
22470 return 0;
22471 }
22472
22473 return 0;
22474 }
22475
22476 \f
22477 /***********************************************************************
22478 Glyph Display
22479 ***********************************************************************/
22480
22481 #ifdef HAVE_WINDOW_SYSTEM
22482
22483 #ifdef GLYPH_DEBUG
22484
22485 void
22486 dump_glyph_string (struct glyph_string *s)
22487 {
22488 fprintf (stderr, "glyph string\n");
22489 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22490 s->x, s->y, s->width, s->height);
22491 fprintf (stderr, " ybase = %d\n", s->ybase);
22492 fprintf (stderr, " hl = %d\n", s->hl);
22493 fprintf (stderr, " left overhang = %d, right = %d\n",
22494 s->left_overhang, s->right_overhang);
22495 fprintf (stderr, " nchars = %d\n", s->nchars);
22496 fprintf (stderr, " extends to end of line = %d\n",
22497 s->extends_to_end_of_line_p);
22498 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22499 fprintf (stderr, " bg width = %d\n", s->background_width);
22500 }
22501
22502 #endif /* GLYPH_DEBUG */
22503
22504 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22505 of XChar2b structures for S; it can't be allocated in
22506 init_glyph_string because it must be allocated via `alloca'. W
22507 is the window on which S is drawn. ROW and AREA are the glyph row
22508 and area within the row from which S is constructed. START is the
22509 index of the first glyph structure covered by S. HL is a
22510 face-override for drawing S. */
22511
22512 #ifdef HAVE_NTGUI
22513 #define OPTIONAL_HDC(hdc) HDC hdc,
22514 #define DECLARE_HDC(hdc) HDC hdc;
22515 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22516 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22517 #endif
22518
22519 #ifndef OPTIONAL_HDC
22520 #define OPTIONAL_HDC(hdc)
22521 #define DECLARE_HDC(hdc)
22522 #define ALLOCATE_HDC(hdc, f)
22523 #define RELEASE_HDC(hdc, f)
22524 #endif
22525
22526 static void
22527 init_glyph_string (struct glyph_string *s,
22528 OPTIONAL_HDC (hdc)
22529 XChar2b *char2b, struct window *w, struct glyph_row *row,
22530 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22531 {
22532 memset (s, 0, sizeof *s);
22533 s->w = w;
22534 s->f = XFRAME (w->frame);
22535 #ifdef HAVE_NTGUI
22536 s->hdc = hdc;
22537 #endif
22538 s->display = FRAME_X_DISPLAY (s->f);
22539 s->window = FRAME_X_WINDOW (s->f);
22540 s->char2b = char2b;
22541 s->hl = hl;
22542 s->row = row;
22543 s->area = area;
22544 s->first_glyph = row->glyphs[area] + start;
22545 s->height = row->height;
22546 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22547 s->ybase = s->y + row->ascent;
22548 }
22549
22550
22551 /* Append the list of glyph strings with head H and tail T to the list
22552 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22553
22554 static void
22555 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22556 struct glyph_string *h, struct glyph_string *t)
22557 {
22558 if (h)
22559 {
22560 if (*head)
22561 (*tail)->next = h;
22562 else
22563 *head = h;
22564 h->prev = *tail;
22565 *tail = t;
22566 }
22567 }
22568
22569
22570 /* Prepend the list of glyph strings with head H and tail T to the
22571 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22572 result. */
22573
22574 static void
22575 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22576 struct glyph_string *h, struct glyph_string *t)
22577 {
22578 if (h)
22579 {
22580 if (*head)
22581 (*head)->prev = t;
22582 else
22583 *tail = t;
22584 t->next = *head;
22585 *head = h;
22586 }
22587 }
22588
22589
22590 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22591 Set *HEAD and *TAIL to the resulting list. */
22592
22593 static void
22594 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22595 struct glyph_string *s)
22596 {
22597 s->next = s->prev = NULL;
22598 append_glyph_string_lists (head, tail, s, s);
22599 }
22600
22601
22602 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22603 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22604 make sure that X resources for the face returned are allocated.
22605 Value is a pointer to a realized face that is ready for display if
22606 DISPLAY_P is non-zero. */
22607
22608 static struct face *
22609 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22610 XChar2b *char2b, int display_p)
22611 {
22612 struct face *face = FACE_FROM_ID (f, face_id);
22613
22614 if (face->font)
22615 {
22616 unsigned code = face->font->driver->encode_char (face->font, c);
22617
22618 if (code != FONT_INVALID_CODE)
22619 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22620 else
22621 STORE_XCHAR2B (char2b, 0, 0);
22622 }
22623
22624 /* Make sure X resources of the face are allocated. */
22625 #ifdef HAVE_X_WINDOWS
22626 if (display_p)
22627 #endif
22628 {
22629 eassert (face != NULL);
22630 PREPARE_FACE_FOR_DISPLAY (f, face);
22631 }
22632
22633 return face;
22634 }
22635
22636
22637 /* Get face and two-byte form of character glyph GLYPH on frame F.
22638 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22639 a pointer to a realized face that is ready for display. */
22640
22641 static struct face *
22642 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22643 XChar2b *char2b, int *two_byte_p)
22644 {
22645 struct face *face;
22646
22647 eassert (glyph->type == CHAR_GLYPH);
22648 face = FACE_FROM_ID (f, glyph->face_id);
22649
22650 if (two_byte_p)
22651 *two_byte_p = 0;
22652
22653 if (face->font)
22654 {
22655 unsigned code;
22656
22657 if (CHAR_BYTE8_P (glyph->u.ch))
22658 code = CHAR_TO_BYTE8 (glyph->u.ch);
22659 else
22660 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22661
22662 if (code != FONT_INVALID_CODE)
22663 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22664 else
22665 STORE_XCHAR2B (char2b, 0, 0);
22666 }
22667
22668 /* Make sure X resources of the face are allocated. */
22669 eassert (face != NULL);
22670 PREPARE_FACE_FOR_DISPLAY (f, face);
22671 return face;
22672 }
22673
22674
22675 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22676 Return 1 if FONT has a glyph for C, otherwise return 0. */
22677
22678 static int
22679 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22680 {
22681 unsigned code;
22682
22683 if (CHAR_BYTE8_P (c))
22684 code = CHAR_TO_BYTE8 (c);
22685 else
22686 code = font->driver->encode_char (font, c);
22687
22688 if (code == FONT_INVALID_CODE)
22689 return 0;
22690 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22691 return 1;
22692 }
22693
22694
22695 /* Fill glyph string S with composition components specified by S->cmp.
22696
22697 BASE_FACE is the base face of the composition.
22698 S->cmp_from is the index of the first component for S.
22699
22700 OVERLAPS non-zero means S should draw the foreground only, and use
22701 its physical height for clipping. See also draw_glyphs.
22702
22703 Value is the index of a component not in S. */
22704
22705 static int
22706 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22707 int overlaps)
22708 {
22709 int i;
22710 /* For all glyphs of this composition, starting at the offset
22711 S->cmp_from, until we reach the end of the definition or encounter a
22712 glyph that requires the different face, add it to S. */
22713 struct face *face;
22714
22715 eassert (s);
22716
22717 s->for_overlaps = overlaps;
22718 s->face = NULL;
22719 s->font = NULL;
22720 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22721 {
22722 int c = COMPOSITION_GLYPH (s->cmp, i);
22723
22724 /* TAB in a composition means display glyphs with padding space
22725 on the left or right. */
22726 if (c != '\t')
22727 {
22728 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22729 -1, Qnil);
22730
22731 face = get_char_face_and_encoding (s->f, c, face_id,
22732 s->char2b + i, 1);
22733 if (face)
22734 {
22735 if (! s->face)
22736 {
22737 s->face = face;
22738 s->font = s->face->font;
22739 }
22740 else if (s->face != face)
22741 break;
22742 }
22743 }
22744 ++s->nchars;
22745 }
22746 s->cmp_to = i;
22747
22748 if (s->face == NULL)
22749 {
22750 s->face = base_face->ascii_face;
22751 s->font = s->face->font;
22752 }
22753
22754 /* All glyph strings for the same composition has the same width,
22755 i.e. the width set for the first component of the composition. */
22756 s->width = s->first_glyph->pixel_width;
22757
22758 /* If the specified font could not be loaded, use the frame's
22759 default font, but record the fact that we couldn't load it in
22760 the glyph string so that we can draw rectangles for the
22761 characters of the glyph string. */
22762 if (s->font == NULL)
22763 {
22764 s->font_not_found_p = 1;
22765 s->font = FRAME_FONT (s->f);
22766 }
22767
22768 /* Adjust base line for subscript/superscript text. */
22769 s->ybase += s->first_glyph->voffset;
22770
22771 /* This glyph string must always be drawn with 16-bit functions. */
22772 s->two_byte_p = 1;
22773
22774 return s->cmp_to;
22775 }
22776
22777 static int
22778 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22779 int start, int end, int overlaps)
22780 {
22781 struct glyph *glyph, *last;
22782 Lisp_Object lgstring;
22783 int i;
22784
22785 s->for_overlaps = overlaps;
22786 glyph = s->row->glyphs[s->area] + start;
22787 last = s->row->glyphs[s->area] + end;
22788 s->cmp_id = glyph->u.cmp.id;
22789 s->cmp_from = glyph->slice.cmp.from;
22790 s->cmp_to = glyph->slice.cmp.to + 1;
22791 s->face = FACE_FROM_ID (s->f, face_id);
22792 lgstring = composition_gstring_from_id (s->cmp_id);
22793 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22794 glyph++;
22795 while (glyph < last
22796 && glyph->u.cmp.automatic
22797 && glyph->u.cmp.id == s->cmp_id
22798 && s->cmp_to == glyph->slice.cmp.from)
22799 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22800
22801 for (i = s->cmp_from; i < s->cmp_to; i++)
22802 {
22803 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22804 unsigned code = LGLYPH_CODE (lglyph);
22805
22806 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22807 }
22808 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22809 return glyph - s->row->glyphs[s->area];
22810 }
22811
22812
22813 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22814 See the comment of fill_glyph_string for arguments.
22815 Value is the index of the first glyph not in S. */
22816
22817
22818 static int
22819 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22820 int start, int end, int overlaps)
22821 {
22822 struct glyph *glyph, *last;
22823 int voffset;
22824
22825 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22826 s->for_overlaps = overlaps;
22827 glyph = s->row->glyphs[s->area] + start;
22828 last = s->row->glyphs[s->area] + end;
22829 voffset = glyph->voffset;
22830 s->face = FACE_FROM_ID (s->f, face_id);
22831 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22832 s->nchars = 1;
22833 s->width = glyph->pixel_width;
22834 glyph++;
22835 while (glyph < last
22836 && glyph->type == GLYPHLESS_GLYPH
22837 && glyph->voffset == voffset
22838 && glyph->face_id == face_id)
22839 {
22840 s->nchars++;
22841 s->width += glyph->pixel_width;
22842 glyph++;
22843 }
22844 s->ybase += voffset;
22845 return glyph - s->row->glyphs[s->area];
22846 }
22847
22848
22849 /* Fill glyph string S from a sequence of character glyphs.
22850
22851 FACE_ID is the face id of the string. START is the index of the
22852 first glyph to consider, END is the index of the last + 1.
22853 OVERLAPS non-zero means S should draw the foreground only, and use
22854 its physical height for clipping. See also draw_glyphs.
22855
22856 Value is the index of the first glyph not in S. */
22857
22858 static int
22859 fill_glyph_string (struct glyph_string *s, int face_id,
22860 int start, int end, int overlaps)
22861 {
22862 struct glyph *glyph, *last;
22863 int voffset;
22864 int glyph_not_available_p;
22865
22866 eassert (s->f == XFRAME (s->w->frame));
22867 eassert (s->nchars == 0);
22868 eassert (start >= 0 && end > start);
22869
22870 s->for_overlaps = overlaps;
22871 glyph = s->row->glyphs[s->area] + start;
22872 last = s->row->glyphs[s->area] + end;
22873 voffset = glyph->voffset;
22874 s->padding_p = glyph->padding_p;
22875 glyph_not_available_p = glyph->glyph_not_available_p;
22876
22877 while (glyph < last
22878 && glyph->type == CHAR_GLYPH
22879 && glyph->voffset == voffset
22880 /* Same face id implies same font, nowadays. */
22881 && glyph->face_id == face_id
22882 && glyph->glyph_not_available_p == glyph_not_available_p)
22883 {
22884 int two_byte_p;
22885
22886 s->face = get_glyph_face_and_encoding (s->f, glyph,
22887 s->char2b + s->nchars,
22888 &two_byte_p);
22889 s->two_byte_p = two_byte_p;
22890 ++s->nchars;
22891 eassert (s->nchars <= end - start);
22892 s->width += glyph->pixel_width;
22893 if (glyph++->padding_p != s->padding_p)
22894 break;
22895 }
22896
22897 s->font = s->face->font;
22898
22899 /* If the specified font could not be loaded, use the frame's font,
22900 but record the fact that we couldn't load it in
22901 S->font_not_found_p so that we can draw rectangles for the
22902 characters of the glyph string. */
22903 if (s->font == NULL || glyph_not_available_p)
22904 {
22905 s->font_not_found_p = 1;
22906 s->font = FRAME_FONT (s->f);
22907 }
22908
22909 /* Adjust base line for subscript/superscript text. */
22910 s->ybase += voffset;
22911
22912 eassert (s->face && s->face->gc);
22913 return glyph - s->row->glyphs[s->area];
22914 }
22915
22916
22917 /* Fill glyph string S from image glyph S->first_glyph. */
22918
22919 static void
22920 fill_image_glyph_string (struct glyph_string *s)
22921 {
22922 eassert (s->first_glyph->type == IMAGE_GLYPH);
22923 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22924 eassert (s->img);
22925 s->slice = s->first_glyph->slice.img;
22926 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22927 s->font = s->face->font;
22928 s->width = s->first_glyph->pixel_width;
22929
22930 /* Adjust base line for subscript/superscript text. */
22931 s->ybase += s->first_glyph->voffset;
22932 }
22933
22934
22935 /* Fill glyph string S from a sequence of stretch glyphs.
22936
22937 START is the index of the first glyph to consider,
22938 END is the index of the last + 1.
22939
22940 Value is the index of the first glyph not in S. */
22941
22942 static int
22943 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22944 {
22945 struct glyph *glyph, *last;
22946 int voffset, face_id;
22947
22948 eassert (s->first_glyph->type == STRETCH_GLYPH);
22949
22950 glyph = s->row->glyphs[s->area] + start;
22951 last = s->row->glyphs[s->area] + end;
22952 face_id = glyph->face_id;
22953 s->face = FACE_FROM_ID (s->f, face_id);
22954 s->font = s->face->font;
22955 s->width = glyph->pixel_width;
22956 s->nchars = 1;
22957 voffset = glyph->voffset;
22958
22959 for (++glyph;
22960 (glyph < last
22961 && glyph->type == STRETCH_GLYPH
22962 && glyph->voffset == voffset
22963 && glyph->face_id == face_id);
22964 ++glyph)
22965 s->width += glyph->pixel_width;
22966
22967 /* Adjust base line for subscript/superscript text. */
22968 s->ybase += voffset;
22969
22970 /* The case that face->gc == 0 is handled when drawing the glyph
22971 string by calling PREPARE_FACE_FOR_DISPLAY. */
22972 eassert (s->face);
22973 return glyph - s->row->glyphs[s->area];
22974 }
22975
22976 static struct font_metrics *
22977 get_per_char_metric (struct font *font, XChar2b *char2b)
22978 {
22979 static struct font_metrics metrics;
22980 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22981
22982 if (! font || code == FONT_INVALID_CODE)
22983 return NULL;
22984 font->driver->text_extents (font, &code, 1, &metrics);
22985 return &metrics;
22986 }
22987
22988 /* EXPORT for RIF:
22989 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22990 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22991 assumed to be zero. */
22992
22993 void
22994 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22995 {
22996 *left = *right = 0;
22997
22998 if (glyph->type == CHAR_GLYPH)
22999 {
23000 struct face *face;
23001 XChar2b char2b;
23002 struct font_metrics *pcm;
23003
23004 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23005 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23006 {
23007 if (pcm->rbearing > pcm->width)
23008 *right = pcm->rbearing - pcm->width;
23009 if (pcm->lbearing < 0)
23010 *left = -pcm->lbearing;
23011 }
23012 }
23013 else if (glyph->type == COMPOSITE_GLYPH)
23014 {
23015 if (! glyph->u.cmp.automatic)
23016 {
23017 struct composition *cmp = composition_table[glyph->u.cmp.id];
23018
23019 if (cmp->rbearing > cmp->pixel_width)
23020 *right = cmp->rbearing - cmp->pixel_width;
23021 if (cmp->lbearing < 0)
23022 *left = - cmp->lbearing;
23023 }
23024 else
23025 {
23026 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23027 struct font_metrics metrics;
23028
23029 composition_gstring_width (gstring, glyph->slice.cmp.from,
23030 glyph->slice.cmp.to + 1, &metrics);
23031 if (metrics.rbearing > metrics.width)
23032 *right = metrics.rbearing - metrics.width;
23033 if (metrics.lbearing < 0)
23034 *left = - metrics.lbearing;
23035 }
23036 }
23037 }
23038
23039
23040 /* Return the index of the first glyph preceding glyph string S that
23041 is overwritten by S because of S's left overhang. Value is -1
23042 if no glyphs are overwritten. */
23043
23044 static int
23045 left_overwritten (struct glyph_string *s)
23046 {
23047 int k;
23048
23049 if (s->left_overhang)
23050 {
23051 int x = 0, i;
23052 struct glyph *glyphs = s->row->glyphs[s->area];
23053 int first = s->first_glyph - glyphs;
23054
23055 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23056 x -= glyphs[i].pixel_width;
23057
23058 k = i + 1;
23059 }
23060 else
23061 k = -1;
23062
23063 return k;
23064 }
23065
23066
23067 /* Return the index of the first glyph preceding glyph string S that
23068 is overwriting S because of its right overhang. Value is -1 if no
23069 glyph in front of S overwrites S. */
23070
23071 static int
23072 left_overwriting (struct glyph_string *s)
23073 {
23074 int i, k, x;
23075 struct glyph *glyphs = s->row->glyphs[s->area];
23076 int first = s->first_glyph - glyphs;
23077
23078 k = -1;
23079 x = 0;
23080 for (i = first - 1; i >= 0; --i)
23081 {
23082 int left, right;
23083 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23084 if (x + right > 0)
23085 k = i;
23086 x -= glyphs[i].pixel_width;
23087 }
23088
23089 return k;
23090 }
23091
23092
23093 /* Return the index of the last glyph following glyph string S that is
23094 overwritten by S because of S's right overhang. Value is -1 if
23095 no such glyph is found. */
23096
23097 static int
23098 right_overwritten (struct glyph_string *s)
23099 {
23100 int k = -1;
23101
23102 if (s->right_overhang)
23103 {
23104 int x = 0, i;
23105 struct glyph *glyphs = s->row->glyphs[s->area];
23106 int first = (s->first_glyph - glyphs
23107 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23108 int end = s->row->used[s->area];
23109
23110 for (i = first; i < end && s->right_overhang > x; ++i)
23111 x += glyphs[i].pixel_width;
23112
23113 k = i;
23114 }
23115
23116 return k;
23117 }
23118
23119
23120 /* Return the index of the last glyph following glyph string S that
23121 overwrites S because of its left overhang. Value is negative
23122 if no such glyph is found. */
23123
23124 static int
23125 right_overwriting (struct glyph_string *s)
23126 {
23127 int i, k, x;
23128 int end = s->row->used[s->area];
23129 struct glyph *glyphs = s->row->glyphs[s->area];
23130 int first = (s->first_glyph - glyphs
23131 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23132
23133 k = -1;
23134 x = 0;
23135 for (i = first; i < end; ++i)
23136 {
23137 int left, right;
23138 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23139 if (x - left < 0)
23140 k = i;
23141 x += glyphs[i].pixel_width;
23142 }
23143
23144 return k;
23145 }
23146
23147
23148 /* Set background width of glyph string S. START is the index of the
23149 first glyph following S. LAST_X is the right-most x-position + 1
23150 in the drawing area. */
23151
23152 static void
23153 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23154 {
23155 /* If the face of this glyph string has to be drawn to the end of
23156 the drawing area, set S->extends_to_end_of_line_p. */
23157
23158 if (start == s->row->used[s->area]
23159 && s->area == TEXT_AREA
23160 && ((s->row->fill_line_p
23161 && (s->hl == DRAW_NORMAL_TEXT
23162 || s->hl == DRAW_IMAGE_RAISED
23163 || s->hl == DRAW_IMAGE_SUNKEN))
23164 || s->hl == DRAW_MOUSE_FACE))
23165 s->extends_to_end_of_line_p = 1;
23166
23167 /* If S extends its face to the end of the line, set its
23168 background_width to the distance to the right edge of the drawing
23169 area. */
23170 if (s->extends_to_end_of_line_p)
23171 s->background_width = last_x - s->x + 1;
23172 else
23173 s->background_width = s->width;
23174 }
23175
23176
23177 /* Compute overhangs and x-positions for glyph string S and its
23178 predecessors, or successors. X is the starting x-position for S.
23179 BACKWARD_P non-zero means process predecessors. */
23180
23181 static void
23182 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23183 {
23184 if (backward_p)
23185 {
23186 while (s)
23187 {
23188 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23189 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23190 x -= s->width;
23191 s->x = x;
23192 s = s->prev;
23193 }
23194 }
23195 else
23196 {
23197 while (s)
23198 {
23199 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23200 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23201 s->x = x;
23202 x += s->width;
23203 s = s->next;
23204 }
23205 }
23206 }
23207
23208
23209
23210 /* The following macros are only called from draw_glyphs below.
23211 They reference the following parameters of that function directly:
23212 `w', `row', `area', and `overlap_p'
23213 as well as the following local variables:
23214 `s', `f', and `hdc' (in W32) */
23215
23216 #ifdef HAVE_NTGUI
23217 /* On W32, silently add local `hdc' variable to argument list of
23218 init_glyph_string. */
23219 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23220 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23221 #else
23222 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23223 init_glyph_string (s, char2b, w, row, area, start, hl)
23224 #endif
23225
23226 /* Add a glyph string for a stretch glyph to the list of strings
23227 between HEAD and TAIL. START is the index of the stretch glyph in
23228 row area AREA of glyph row ROW. END is the index of the last glyph
23229 in that glyph row area. X is the current output position assigned
23230 to the new glyph string constructed. HL overrides that face of the
23231 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23232 is the right-most x-position of the drawing area. */
23233
23234 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23235 and below -- keep them on one line. */
23236 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23237 do \
23238 { \
23239 s = alloca (sizeof *s); \
23240 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23241 START = fill_stretch_glyph_string (s, START, END); \
23242 append_glyph_string (&HEAD, &TAIL, s); \
23243 s->x = (X); \
23244 } \
23245 while (0)
23246
23247
23248 /* Add a glyph string for an image glyph to the list of strings
23249 between HEAD and TAIL. START is the index of the image glyph in
23250 row area AREA of glyph row ROW. END is the index of the last glyph
23251 in that glyph row area. X is the current output position assigned
23252 to the new glyph string constructed. HL overrides that face of the
23253 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23254 is the right-most x-position of the drawing area. */
23255
23256 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23257 do \
23258 { \
23259 s = alloca (sizeof *s); \
23260 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23261 fill_image_glyph_string (s); \
23262 append_glyph_string (&HEAD, &TAIL, s); \
23263 ++START; \
23264 s->x = (X); \
23265 } \
23266 while (0)
23267
23268
23269 /* Add a glyph string for a sequence of character glyphs to the list
23270 of strings between HEAD and TAIL. START is the index of the first
23271 glyph in row area AREA of glyph row ROW that is part of the new
23272 glyph string. END is the index of the last glyph in that glyph row
23273 area. X is the current output position assigned to the new glyph
23274 string constructed. HL overrides that face of the glyph; e.g. it
23275 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23276 right-most x-position of the drawing area. */
23277
23278 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23279 do \
23280 { \
23281 int face_id; \
23282 XChar2b *char2b; \
23283 \
23284 face_id = (row)->glyphs[area][START].face_id; \
23285 \
23286 s = alloca (sizeof *s); \
23287 char2b = alloca ((END - START) * sizeof *char2b); \
23288 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23289 append_glyph_string (&HEAD, &TAIL, s); \
23290 s->x = (X); \
23291 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23292 } \
23293 while (0)
23294
23295
23296 /* Add a glyph string for a composite sequence to the list of strings
23297 between HEAD and TAIL. START is the index of the first glyph in
23298 row area AREA of glyph row ROW that is part of the new glyph
23299 string. END is the index of the last glyph in that glyph row area.
23300 X is the current output position assigned to the new glyph string
23301 constructed. HL overrides that face of the glyph; e.g. it is
23302 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23303 x-position of the drawing area. */
23304
23305 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23306 do { \
23307 int face_id = (row)->glyphs[area][START].face_id; \
23308 struct face *base_face = FACE_FROM_ID (f, face_id); \
23309 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23310 struct composition *cmp = composition_table[cmp_id]; \
23311 XChar2b *char2b; \
23312 struct glyph_string *first_s = NULL; \
23313 int n; \
23314 \
23315 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23316 \
23317 /* Make glyph_strings for each glyph sequence that is drawable by \
23318 the same face, and append them to HEAD/TAIL. */ \
23319 for (n = 0; n < cmp->glyph_len;) \
23320 { \
23321 s = alloca (sizeof *s); \
23322 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23323 append_glyph_string (&(HEAD), &(TAIL), s); \
23324 s->cmp = cmp; \
23325 s->cmp_from = n; \
23326 s->x = (X); \
23327 if (n == 0) \
23328 first_s = s; \
23329 n = fill_composite_glyph_string (s, base_face, overlaps); \
23330 } \
23331 \
23332 ++START; \
23333 s = first_s; \
23334 } while (0)
23335
23336
23337 /* Add a glyph string for a glyph-string sequence to the list of strings
23338 between HEAD and TAIL. */
23339
23340 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23341 do { \
23342 int face_id; \
23343 XChar2b *char2b; \
23344 Lisp_Object gstring; \
23345 \
23346 face_id = (row)->glyphs[area][START].face_id; \
23347 gstring = (composition_gstring_from_id \
23348 ((row)->glyphs[area][START].u.cmp.id)); \
23349 s = alloca (sizeof *s); \
23350 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23351 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23352 append_glyph_string (&(HEAD), &(TAIL), s); \
23353 s->x = (X); \
23354 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23355 } while (0)
23356
23357
23358 /* Add a glyph string for a sequence of glyphless character's glyphs
23359 to the list of strings between HEAD and TAIL. The meanings of
23360 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23361
23362 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23363 do \
23364 { \
23365 int face_id; \
23366 \
23367 face_id = (row)->glyphs[area][START].face_id; \
23368 \
23369 s = alloca (sizeof *s); \
23370 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23371 append_glyph_string (&HEAD, &TAIL, s); \
23372 s->x = (X); \
23373 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23374 overlaps); \
23375 } \
23376 while (0)
23377
23378
23379 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23380 of AREA of glyph row ROW on window W between indices START and END.
23381 HL overrides the face for drawing glyph strings, e.g. it is
23382 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23383 x-positions of the drawing area.
23384
23385 This is an ugly monster macro construct because we must use alloca
23386 to allocate glyph strings (because draw_glyphs can be called
23387 asynchronously). */
23388
23389 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23390 do \
23391 { \
23392 HEAD = TAIL = NULL; \
23393 while (START < END) \
23394 { \
23395 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23396 switch (first_glyph->type) \
23397 { \
23398 case CHAR_GLYPH: \
23399 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23400 HL, X, LAST_X); \
23401 break; \
23402 \
23403 case COMPOSITE_GLYPH: \
23404 if (first_glyph->u.cmp.automatic) \
23405 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23406 HL, X, LAST_X); \
23407 else \
23408 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23409 HL, X, LAST_X); \
23410 break; \
23411 \
23412 case STRETCH_GLYPH: \
23413 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23414 HL, X, LAST_X); \
23415 break; \
23416 \
23417 case IMAGE_GLYPH: \
23418 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23419 HL, X, LAST_X); \
23420 break; \
23421 \
23422 case GLYPHLESS_GLYPH: \
23423 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23424 HL, X, LAST_X); \
23425 break; \
23426 \
23427 default: \
23428 emacs_abort (); \
23429 } \
23430 \
23431 if (s) \
23432 { \
23433 set_glyph_string_background_width (s, START, LAST_X); \
23434 (X) += s->width; \
23435 } \
23436 } \
23437 } while (0)
23438
23439
23440 /* Draw glyphs between START and END in AREA of ROW on window W,
23441 starting at x-position X. X is relative to AREA in W. HL is a
23442 face-override with the following meaning:
23443
23444 DRAW_NORMAL_TEXT draw normally
23445 DRAW_CURSOR draw in cursor face
23446 DRAW_MOUSE_FACE draw in mouse face.
23447 DRAW_INVERSE_VIDEO draw in mode line face
23448 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23449 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23450
23451 If OVERLAPS is non-zero, draw only the foreground of characters and
23452 clip to the physical height of ROW. Non-zero value also defines
23453 the overlapping part to be drawn:
23454
23455 OVERLAPS_PRED overlap with preceding rows
23456 OVERLAPS_SUCC overlap with succeeding rows
23457 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23458 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23459
23460 Value is the x-position reached, relative to AREA of W. */
23461
23462 static int
23463 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23464 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23465 enum draw_glyphs_face hl, int overlaps)
23466 {
23467 struct glyph_string *head, *tail;
23468 struct glyph_string *s;
23469 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23470 int i, j, x_reached, last_x, area_left = 0;
23471 struct frame *f = XFRAME (WINDOW_FRAME (w));
23472 DECLARE_HDC (hdc);
23473
23474 ALLOCATE_HDC (hdc, f);
23475
23476 /* Let's rather be paranoid than getting a SEGV. */
23477 end = min (end, row->used[area]);
23478 start = max (0, start);
23479 start = min (end, start);
23480
23481 /* Translate X to frame coordinates. Set last_x to the right
23482 end of the drawing area. */
23483 if (row->full_width_p)
23484 {
23485 /* X is relative to the left edge of W, without scroll bars
23486 or fringes. */
23487 area_left = WINDOW_LEFT_EDGE_X (w);
23488 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23489 }
23490 else
23491 {
23492 area_left = window_box_left (w, area);
23493 last_x = area_left + window_box_width (w, area);
23494 }
23495 x += area_left;
23496
23497 /* Build a doubly-linked list of glyph_string structures between
23498 head and tail from what we have to draw. Note that the macro
23499 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23500 the reason we use a separate variable `i'. */
23501 i = start;
23502 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23503 if (tail)
23504 x_reached = tail->x + tail->background_width;
23505 else
23506 x_reached = x;
23507
23508 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23509 the row, redraw some glyphs in front or following the glyph
23510 strings built above. */
23511 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23512 {
23513 struct glyph_string *h, *t;
23514 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23515 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23516 int check_mouse_face = 0;
23517 int dummy_x = 0;
23518
23519 /* If mouse highlighting is on, we may need to draw adjacent
23520 glyphs using mouse-face highlighting. */
23521 if (area == TEXT_AREA && row->mouse_face_p
23522 && hlinfo->mouse_face_beg_row >= 0
23523 && hlinfo->mouse_face_end_row >= 0)
23524 {
23525 struct glyph_row *mouse_beg_row, *mouse_end_row;
23526
23527 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23528 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23529
23530 if (row >= mouse_beg_row && row <= mouse_end_row)
23531 {
23532 check_mouse_face = 1;
23533 mouse_beg_col = (row == mouse_beg_row)
23534 ? hlinfo->mouse_face_beg_col : 0;
23535 mouse_end_col = (row == mouse_end_row)
23536 ? hlinfo->mouse_face_end_col
23537 : row->used[TEXT_AREA];
23538 }
23539 }
23540
23541 /* Compute overhangs for all glyph strings. */
23542 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23543 for (s = head; s; s = s->next)
23544 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23545
23546 /* Prepend glyph strings for glyphs in front of the first glyph
23547 string that are overwritten because of the first glyph
23548 string's left overhang. The background of all strings
23549 prepended must be drawn because the first glyph string
23550 draws over it. */
23551 i = left_overwritten (head);
23552 if (i >= 0)
23553 {
23554 enum draw_glyphs_face overlap_hl;
23555
23556 /* If this row contains mouse highlighting, attempt to draw
23557 the overlapped glyphs with the correct highlight. This
23558 code fails if the overlap encompasses more than one glyph
23559 and mouse-highlight spans only some of these glyphs.
23560 However, making it work perfectly involves a lot more
23561 code, and I don't know if the pathological case occurs in
23562 practice, so we'll stick to this for now. --- cyd */
23563 if (check_mouse_face
23564 && mouse_beg_col < start && mouse_end_col > i)
23565 overlap_hl = DRAW_MOUSE_FACE;
23566 else
23567 overlap_hl = DRAW_NORMAL_TEXT;
23568
23569 j = i;
23570 BUILD_GLYPH_STRINGS (j, start, h, t,
23571 overlap_hl, dummy_x, last_x);
23572 start = i;
23573 compute_overhangs_and_x (t, head->x, 1);
23574 prepend_glyph_string_lists (&head, &tail, h, t);
23575 clip_head = head;
23576 }
23577
23578 /* Prepend glyph strings for glyphs in front of the first glyph
23579 string that overwrite that glyph string because of their
23580 right overhang. For these strings, only the foreground must
23581 be drawn, because it draws over the glyph string at `head'.
23582 The background must not be drawn because this would overwrite
23583 right overhangs of preceding glyphs for which no glyph
23584 strings exist. */
23585 i = left_overwriting (head);
23586 if (i >= 0)
23587 {
23588 enum draw_glyphs_face overlap_hl;
23589
23590 if (check_mouse_face
23591 && mouse_beg_col < start && mouse_end_col > i)
23592 overlap_hl = DRAW_MOUSE_FACE;
23593 else
23594 overlap_hl = DRAW_NORMAL_TEXT;
23595
23596 clip_head = head;
23597 BUILD_GLYPH_STRINGS (i, start, h, t,
23598 overlap_hl, dummy_x, last_x);
23599 for (s = h; s; s = s->next)
23600 s->background_filled_p = 1;
23601 compute_overhangs_and_x (t, head->x, 1);
23602 prepend_glyph_string_lists (&head, &tail, h, t);
23603 }
23604
23605 /* Append glyphs strings for glyphs following the last glyph
23606 string tail that are overwritten by tail. The background of
23607 these strings has to be drawn because tail's foreground draws
23608 over it. */
23609 i = right_overwritten (tail);
23610 if (i >= 0)
23611 {
23612 enum draw_glyphs_face overlap_hl;
23613
23614 if (check_mouse_face
23615 && mouse_beg_col < i && mouse_end_col > end)
23616 overlap_hl = DRAW_MOUSE_FACE;
23617 else
23618 overlap_hl = DRAW_NORMAL_TEXT;
23619
23620 BUILD_GLYPH_STRINGS (end, i, h, t,
23621 overlap_hl, x, last_x);
23622 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23623 we don't have `end = i;' here. */
23624 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23625 append_glyph_string_lists (&head, &tail, h, t);
23626 clip_tail = tail;
23627 }
23628
23629 /* Append glyph strings for glyphs following the last glyph
23630 string tail that overwrite tail. The foreground of such
23631 glyphs has to be drawn because it writes into the background
23632 of tail. The background must not be drawn because it could
23633 paint over the foreground of following glyphs. */
23634 i = right_overwriting (tail);
23635 if (i >= 0)
23636 {
23637 enum draw_glyphs_face overlap_hl;
23638 if (check_mouse_face
23639 && mouse_beg_col < i && mouse_end_col > end)
23640 overlap_hl = DRAW_MOUSE_FACE;
23641 else
23642 overlap_hl = DRAW_NORMAL_TEXT;
23643
23644 clip_tail = tail;
23645 i++; /* We must include the Ith glyph. */
23646 BUILD_GLYPH_STRINGS (end, i, h, t,
23647 overlap_hl, x, last_x);
23648 for (s = h; s; s = s->next)
23649 s->background_filled_p = 1;
23650 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23651 append_glyph_string_lists (&head, &tail, h, t);
23652 }
23653 if (clip_head || clip_tail)
23654 for (s = head; s; s = s->next)
23655 {
23656 s->clip_head = clip_head;
23657 s->clip_tail = clip_tail;
23658 }
23659 }
23660
23661 /* Draw all strings. */
23662 for (s = head; s; s = s->next)
23663 FRAME_RIF (f)->draw_glyph_string (s);
23664
23665 #ifndef HAVE_NS
23666 /* When focus a sole frame and move horizontally, this sets on_p to 0
23667 causing a failure to erase prev cursor position. */
23668 if (area == TEXT_AREA
23669 && !row->full_width_p
23670 /* When drawing overlapping rows, only the glyph strings'
23671 foreground is drawn, which doesn't erase a cursor
23672 completely. */
23673 && !overlaps)
23674 {
23675 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23676 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23677 : (tail ? tail->x + tail->background_width : x));
23678 x0 -= area_left;
23679 x1 -= area_left;
23680
23681 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23682 row->y, MATRIX_ROW_BOTTOM_Y (row));
23683 }
23684 #endif
23685
23686 /* Value is the x-position up to which drawn, relative to AREA of W.
23687 This doesn't include parts drawn because of overhangs. */
23688 if (row->full_width_p)
23689 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23690 else
23691 x_reached -= area_left;
23692
23693 RELEASE_HDC (hdc, f);
23694
23695 return x_reached;
23696 }
23697
23698 /* Expand row matrix if too narrow. Don't expand if area
23699 is not present. */
23700
23701 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23702 { \
23703 if (!fonts_changed_p \
23704 && (it->glyph_row->glyphs[area] \
23705 < it->glyph_row->glyphs[area + 1])) \
23706 { \
23707 it->w->ncols_scale_factor++; \
23708 fonts_changed_p = 1; \
23709 } \
23710 }
23711
23712 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23713 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23714
23715 static void
23716 append_glyph (struct it *it)
23717 {
23718 struct glyph *glyph;
23719 enum glyph_row_area area = it->area;
23720
23721 eassert (it->glyph_row);
23722 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23723
23724 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23725 if (glyph < it->glyph_row->glyphs[area + 1])
23726 {
23727 /* If the glyph row is reversed, we need to prepend the glyph
23728 rather than append it. */
23729 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23730 {
23731 struct glyph *g;
23732
23733 /* Make room for the additional glyph. */
23734 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23735 g[1] = *g;
23736 glyph = it->glyph_row->glyphs[area];
23737 }
23738 glyph->charpos = CHARPOS (it->position);
23739 glyph->object = it->object;
23740 if (it->pixel_width > 0)
23741 {
23742 glyph->pixel_width = it->pixel_width;
23743 glyph->padding_p = 0;
23744 }
23745 else
23746 {
23747 /* Assure at least 1-pixel width. Otherwise, cursor can't
23748 be displayed correctly. */
23749 glyph->pixel_width = 1;
23750 glyph->padding_p = 1;
23751 }
23752 glyph->ascent = it->ascent;
23753 glyph->descent = it->descent;
23754 glyph->voffset = it->voffset;
23755 glyph->type = CHAR_GLYPH;
23756 glyph->avoid_cursor_p = it->avoid_cursor_p;
23757 glyph->multibyte_p = it->multibyte_p;
23758 glyph->left_box_line_p = it->start_of_box_run_p;
23759 glyph->right_box_line_p = it->end_of_box_run_p;
23760 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23761 || it->phys_descent > it->descent);
23762 glyph->glyph_not_available_p = it->glyph_not_available_p;
23763 glyph->face_id = it->face_id;
23764 glyph->u.ch = it->char_to_display;
23765 glyph->slice.img = null_glyph_slice;
23766 glyph->font_type = FONT_TYPE_UNKNOWN;
23767 if (it->bidi_p)
23768 {
23769 glyph->resolved_level = it->bidi_it.resolved_level;
23770 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23771 emacs_abort ();
23772 glyph->bidi_type = it->bidi_it.type;
23773 }
23774 else
23775 {
23776 glyph->resolved_level = 0;
23777 glyph->bidi_type = UNKNOWN_BT;
23778 }
23779 ++it->glyph_row->used[area];
23780 }
23781 else
23782 IT_EXPAND_MATRIX_WIDTH (it, area);
23783 }
23784
23785 /* Store one glyph for the composition IT->cmp_it.id in
23786 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23787 non-null. */
23788
23789 static void
23790 append_composite_glyph (struct it *it)
23791 {
23792 struct glyph *glyph;
23793 enum glyph_row_area area = it->area;
23794
23795 eassert (it->glyph_row);
23796
23797 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23798 if (glyph < it->glyph_row->glyphs[area + 1])
23799 {
23800 /* If the glyph row is reversed, we need to prepend the glyph
23801 rather than append it. */
23802 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23803 {
23804 struct glyph *g;
23805
23806 /* Make room for the new glyph. */
23807 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23808 g[1] = *g;
23809 glyph = it->glyph_row->glyphs[it->area];
23810 }
23811 glyph->charpos = it->cmp_it.charpos;
23812 glyph->object = it->object;
23813 glyph->pixel_width = it->pixel_width;
23814 glyph->ascent = it->ascent;
23815 glyph->descent = it->descent;
23816 glyph->voffset = it->voffset;
23817 glyph->type = COMPOSITE_GLYPH;
23818 if (it->cmp_it.ch < 0)
23819 {
23820 glyph->u.cmp.automatic = 0;
23821 glyph->u.cmp.id = it->cmp_it.id;
23822 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23823 }
23824 else
23825 {
23826 glyph->u.cmp.automatic = 1;
23827 glyph->u.cmp.id = it->cmp_it.id;
23828 glyph->slice.cmp.from = it->cmp_it.from;
23829 glyph->slice.cmp.to = it->cmp_it.to - 1;
23830 }
23831 glyph->avoid_cursor_p = it->avoid_cursor_p;
23832 glyph->multibyte_p = it->multibyte_p;
23833 glyph->left_box_line_p = it->start_of_box_run_p;
23834 glyph->right_box_line_p = it->end_of_box_run_p;
23835 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23836 || it->phys_descent > it->descent);
23837 glyph->padding_p = 0;
23838 glyph->glyph_not_available_p = 0;
23839 glyph->face_id = it->face_id;
23840 glyph->font_type = FONT_TYPE_UNKNOWN;
23841 if (it->bidi_p)
23842 {
23843 glyph->resolved_level = it->bidi_it.resolved_level;
23844 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23845 emacs_abort ();
23846 glyph->bidi_type = it->bidi_it.type;
23847 }
23848 ++it->glyph_row->used[area];
23849 }
23850 else
23851 IT_EXPAND_MATRIX_WIDTH (it, area);
23852 }
23853
23854
23855 /* Change IT->ascent and IT->height according to the setting of
23856 IT->voffset. */
23857
23858 static void
23859 take_vertical_position_into_account (struct it *it)
23860 {
23861 if (it->voffset)
23862 {
23863 if (it->voffset < 0)
23864 /* Increase the ascent so that we can display the text higher
23865 in the line. */
23866 it->ascent -= it->voffset;
23867 else
23868 /* Increase the descent so that we can display the text lower
23869 in the line. */
23870 it->descent += it->voffset;
23871 }
23872 }
23873
23874
23875 /* Produce glyphs/get display metrics for the image IT is loaded with.
23876 See the description of struct display_iterator in dispextern.h for
23877 an overview of struct display_iterator. */
23878
23879 static void
23880 produce_image_glyph (struct it *it)
23881 {
23882 struct image *img;
23883 struct face *face;
23884 int glyph_ascent, crop;
23885 struct glyph_slice slice;
23886
23887 eassert (it->what == IT_IMAGE);
23888
23889 face = FACE_FROM_ID (it->f, it->face_id);
23890 eassert (face);
23891 /* Make sure X resources of the face is loaded. */
23892 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23893
23894 if (it->image_id < 0)
23895 {
23896 /* Fringe bitmap. */
23897 it->ascent = it->phys_ascent = 0;
23898 it->descent = it->phys_descent = 0;
23899 it->pixel_width = 0;
23900 it->nglyphs = 0;
23901 return;
23902 }
23903
23904 img = IMAGE_FROM_ID (it->f, it->image_id);
23905 eassert (img);
23906 /* Make sure X resources of the image is loaded. */
23907 prepare_image_for_display (it->f, img);
23908
23909 slice.x = slice.y = 0;
23910 slice.width = img->width;
23911 slice.height = img->height;
23912
23913 if (INTEGERP (it->slice.x))
23914 slice.x = XINT (it->slice.x);
23915 else if (FLOATP (it->slice.x))
23916 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23917
23918 if (INTEGERP (it->slice.y))
23919 slice.y = XINT (it->slice.y);
23920 else if (FLOATP (it->slice.y))
23921 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23922
23923 if (INTEGERP (it->slice.width))
23924 slice.width = XINT (it->slice.width);
23925 else if (FLOATP (it->slice.width))
23926 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23927
23928 if (INTEGERP (it->slice.height))
23929 slice.height = XINT (it->slice.height);
23930 else if (FLOATP (it->slice.height))
23931 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23932
23933 if (slice.x >= img->width)
23934 slice.x = img->width;
23935 if (slice.y >= img->height)
23936 slice.y = img->height;
23937 if (slice.x + slice.width >= img->width)
23938 slice.width = img->width - slice.x;
23939 if (slice.y + slice.height > img->height)
23940 slice.height = img->height - slice.y;
23941
23942 if (slice.width == 0 || slice.height == 0)
23943 return;
23944
23945 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23946
23947 it->descent = slice.height - glyph_ascent;
23948 if (slice.y == 0)
23949 it->descent += img->vmargin;
23950 if (slice.y + slice.height == img->height)
23951 it->descent += img->vmargin;
23952 it->phys_descent = it->descent;
23953
23954 it->pixel_width = slice.width;
23955 if (slice.x == 0)
23956 it->pixel_width += img->hmargin;
23957 if (slice.x + slice.width == img->width)
23958 it->pixel_width += img->hmargin;
23959
23960 /* It's quite possible for images to have an ascent greater than
23961 their height, so don't get confused in that case. */
23962 if (it->descent < 0)
23963 it->descent = 0;
23964
23965 it->nglyphs = 1;
23966
23967 if (face->box != FACE_NO_BOX)
23968 {
23969 if (face->box_line_width > 0)
23970 {
23971 if (slice.y == 0)
23972 it->ascent += face->box_line_width;
23973 if (slice.y + slice.height == img->height)
23974 it->descent += face->box_line_width;
23975 }
23976
23977 if (it->start_of_box_run_p && slice.x == 0)
23978 it->pixel_width += eabs (face->box_line_width);
23979 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23980 it->pixel_width += eabs (face->box_line_width);
23981 }
23982
23983 take_vertical_position_into_account (it);
23984
23985 /* Automatically crop wide image glyphs at right edge so we can
23986 draw the cursor on same display row. */
23987 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23988 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23989 {
23990 it->pixel_width -= crop;
23991 slice.width -= crop;
23992 }
23993
23994 if (it->glyph_row)
23995 {
23996 struct glyph *glyph;
23997 enum glyph_row_area area = it->area;
23998
23999 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24000 if (glyph < it->glyph_row->glyphs[area + 1])
24001 {
24002 glyph->charpos = CHARPOS (it->position);
24003 glyph->object = it->object;
24004 glyph->pixel_width = it->pixel_width;
24005 glyph->ascent = glyph_ascent;
24006 glyph->descent = it->descent;
24007 glyph->voffset = it->voffset;
24008 glyph->type = IMAGE_GLYPH;
24009 glyph->avoid_cursor_p = it->avoid_cursor_p;
24010 glyph->multibyte_p = it->multibyte_p;
24011 glyph->left_box_line_p = it->start_of_box_run_p;
24012 glyph->right_box_line_p = it->end_of_box_run_p;
24013 glyph->overlaps_vertically_p = 0;
24014 glyph->padding_p = 0;
24015 glyph->glyph_not_available_p = 0;
24016 glyph->face_id = it->face_id;
24017 glyph->u.img_id = img->id;
24018 glyph->slice.img = slice;
24019 glyph->font_type = FONT_TYPE_UNKNOWN;
24020 if (it->bidi_p)
24021 {
24022 glyph->resolved_level = it->bidi_it.resolved_level;
24023 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24024 emacs_abort ();
24025 glyph->bidi_type = it->bidi_it.type;
24026 }
24027 ++it->glyph_row->used[area];
24028 }
24029 else
24030 IT_EXPAND_MATRIX_WIDTH (it, area);
24031 }
24032 }
24033
24034
24035 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24036 of the glyph, WIDTH and HEIGHT are the width and height of the
24037 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24038
24039 static void
24040 append_stretch_glyph (struct it *it, Lisp_Object object,
24041 int width, int height, int ascent)
24042 {
24043 struct glyph *glyph;
24044 enum glyph_row_area area = it->area;
24045
24046 eassert (ascent >= 0 && ascent <= height);
24047
24048 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24049 if (glyph < it->glyph_row->glyphs[area + 1])
24050 {
24051 /* If the glyph row is reversed, we need to prepend the glyph
24052 rather than append it. */
24053 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24054 {
24055 struct glyph *g;
24056
24057 /* Make room for the additional glyph. */
24058 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24059 g[1] = *g;
24060 glyph = it->glyph_row->glyphs[area];
24061 }
24062 glyph->charpos = CHARPOS (it->position);
24063 glyph->object = object;
24064 glyph->pixel_width = width;
24065 glyph->ascent = ascent;
24066 glyph->descent = height - ascent;
24067 glyph->voffset = it->voffset;
24068 glyph->type = STRETCH_GLYPH;
24069 glyph->avoid_cursor_p = it->avoid_cursor_p;
24070 glyph->multibyte_p = it->multibyte_p;
24071 glyph->left_box_line_p = it->start_of_box_run_p;
24072 glyph->right_box_line_p = it->end_of_box_run_p;
24073 glyph->overlaps_vertically_p = 0;
24074 glyph->padding_p = 0;
24075 glyph->glyph_not_available_p = 0;
24076 glyph->face_id = it->face_id;
24077 glyph->u.stretch.ascent = ascent;
24078 glyph->u.stretch.height = height;
24079 glyph->slice.img = null_glyph_slice;
24080 glyph->font_type = FONT_TYPE_UNKNOWN;
24081 if (it->bidi_p)
24082 {
24083 glyph->resolved_level = it->bidi_it.resolved_level;
24084 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24085 emacs_abort ();
24086 glyph->bidi_type = it->bidi_it.type;
24087 }
24088 else
24089 {
24090 glyph->resolved_level = 0;
24091 glyph->bidi_type = UNKNOWN_BT;
24092 }
24093 ++it->glyph_row->used[area];
24094 }
24095 else
24096 IT_EXPAND_MATRIX_WIDTH (it, area);
24097 }
24098
24099 #endif /* HAVE_WINDOW_SYSTEM */
24100
24101 /* Produce a stretch glyph for iterator IT. IT->object is the value
24102 of the glyph property displayed. The value must be a list
24103 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24104 being recognized:
24105
24106 1. `:width WIDTH' specifies that the space should be WIDTH *
24107 canonical char width wide. WIDTH may be an integer or floating
24108 point number.
24109
24110 2. `:relative-width FACTOR' specifies that the width of the stretch
24111 should be computed from the width of the first character having the
24112 `glyph' property, and should be FACTOR times that width.
24113
24114 3. `:align-to HPOS' specifies that the space should be wide enough
24115 to reach HPOS, a value in canonical character units.
24116
24117 Exactly one of the above pairs must be present.
24118
24119 4. `:height HEIGHT' specifies that the height of the stretch produced
24120 should be HEIGHT, measured in canonical character units.
24121
24122 5. `:relative-height FACTOR' specifies that the height of the
24123 stretch should be FACTOR times the height of the characters having
24124 the glyph property.
24125
24126 Either none or exactly one of 4 or 5 must be present.
24127
24128 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24129 of the stretch should be used for the ascent of the stretch.
24130 ASCENT must be in the range 0 <= ASCENT <= 100. */
24131
24132 void
24133 produce_stretch_glyph (struct it *it)
24134 {
24135 /* (space :width WIDTH :height HEIGHT ...) */
24136 Lisp_Object prop, plist;
24137 int width = 0, height = 0, align_to = -1;
24138 int zero_width_ok_p = 0;
24139 double tem;
24140 struct font *font = NULL;
24141
24142 #ifdef HAVE_WINDOW_SYSTEM
24143 int ascent = 0;
24144 int zero_height_ok_p = 0;
24145
24146 if (FRAME_WINDOW_P (it->f))
24147 {
24148 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24149 font = face->font ? face->font : FRAME_FONT (it->f);
24150 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24151 }
24152 #endif
24153
24154 /* List should start with `space'. */
24155 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24156 plist = XCDR (it->object);
24157
24158 /* Compute the width of the stretch. */
24159 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24160 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24161 {
24162 /* Absolute width `:width WIDTH' specified and valid. */
24163 zero_width_ok_p = 1;
24164 width = (int)tem;
24165 }
24166 #ifdef HAVE_WINDOW_SYSTEM
24167 else if (FRAME_WINDOW_P (it->f)
24168 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24169 {
24170 /* Relative width `:relative-width FACTOR' specified and valid.
24171 Compute the width of the characters having the `glyph'
24172 property. */
24173 struct it it2;
24174 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24175
24176 it2 = *it;
24177 if (it->multibyte_p)
24178 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24179 else
24180 {
24181 it2.c = it2.char_to_display = *p, it2.len = 1;
24182 if (! ASCII_CHAR_P (it2.c))
24183 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24184 }
24185
24186 it2.glyph_row = NULL;
24187 it2.what = IT_CHARACTER;
24188 x_produce_glyphs (&it2);
24189 width = NUMVAL (prop) * it2.pixel_width;
24190 }
24191 #endif /* HAVE_WINDOW_SYSTEM */
24192 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24193 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24194 {
24195 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24196 align_to = (align_to < 0
24197 ? 0
24198 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24199 else if (align_to < 0)
24200 align_to = window_box_left_offset (it->w, TEXT_AREA);
24201 width = max (0, (int)tem + align_to - it->current_x);
24202 zero_width_ok_p = 1;
24203 }
24204 else
24205 /* Nothing specified -> width defaults to canonical char width. */
24206 width = FRAME_COLUMN_WIDTH (it->f);
24207
24208 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24209 width = 1;
24210
24211 #ifdef HAVE_WINDOW_SYSTEM
24212 /* Compute height. */
24213 if (FRAME_WINDOW_P (it->f))
24214 {
24215 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24216 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24217 {
24218 height = (int)tem;
24219 zero_height_ok_p = 1;
24220 }
24221 else if (prop = Fplist_get (plist, QCrelative_height),
24222 NUMVAL (prop) > 0)
24223 height = FONT_HEIGHT (font) * NUMVAL (prop);
24224 else
24225 height = FONT_HEIGHT (font);
24226
24227 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24228 height = 1;
24229
24230 /* Compute percentage of height used for ascent. If
24231 `:ascent ASCENT' is present and valid, use that. Otherwise,
24232 derive the ascent from the font in use. */
24233 if (prop = Fplist_get (plist, QCascent),
24234 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24235 ascent = height * NUMVAL (prop) / 100.0;
24236 else if (!NILP (prop)
24237 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24238 ascent = min (max (0, (int)tem), height);
24239 else
24240 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24241 }
24242 else
24243 #endif /* HAVE_WINDOW_SYSTEM */
24244 height = 1;
24245
24246 if (width > 0 && it->line_wrap != TRUNCATE
24247 && it->current_x + width > it->last_visible_x)
24248 {
24249 width = it->last_visible_x - it->current_x;
24250 #ifdef HAVE_WINDOW_SYSTEM
24251 /* Subtract one more pixel from the stretch width, but only on
24252 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24253 width -= FRAME_WINDOW_P (it->f);
24254 #endif
24255 }
24256
24257 if (width > 0 && height > 0 && it->glyph_row)
24258 {
24259 Lisp_Object o_object = it->object;
24260 Lisp_Object object = it->stack[it->sp - 1].string;
24261 int n = width;
24262
24263 if (!STRINGP (object))
24264 object = it->w->buffer;
24265 #ifdef HAVE_WINDOW_SYSTEM
24266 if (FRAME_WINDOW_P (it->f))
24267 append_stretch_glyph (it, object, width, height, ascent);
24268 else
24269 #endif
24270 {
24271 it->object = object;
24272 it->char_to_display = ' ';
24273 it->pixel_width = it->len = 1;
24274 while (n--)
24275 tty_append_glyph (it);
24276 it->object = o_object;
24277 }
24278 }
24279
24280 it->pixel_width = width;
24281 #ifdef HAVE_WINDOW_SYSTEM
24282 if (FRAME_WINDOW_P (it->f))
24283 {
24284 it->ascent = it->phys_ascent = ascent;
24285 it->descent = it->phys_descent = height - it->ascent;
24286 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24287 take_vertical_position_into_account (it);
24288 }
24289 else
24290 #endif
24291 it->nglyphs = width;
24292 }
24293
24294 /* Get information about special display element WHAT in an
24295 environment described by IT. WHAT is one of IT_TRUNCATION or
24296 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24297 non-null glyph_row member. This function ensures that fields like
24298 face_id, c, len of IT are left untouched. */
24299
24300 static void
24301 produce_special_glyphs (struct it *it, enum display_element_type what)
24302 {
24303 struct it temp_it;
24304 Lisp_Object gc;
24305 GLYPH glyph;
24306
24307 temp_it = *it;
24308 temp_it.object = make_number (0);
24309 memset (&temp_it.current, 0, sizeof temp_it.current);
24310
24311 if (what == IT_CONTINUATION)
24312 {
24313 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24314 if (it->bidi_it.paragraph_dir == R2L)
24315 SET_GLYPH_FROM_CHAR (glyph, '/');
24316 else
24317 SET_GLYPH_FROM_CHAR (glyph, '\\');
24318 if (it->dp
24319 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24320 {
24321 /* FIXME: Should we mirror GC for R2L lines? */
24322 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24323 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24324 }
24325 }
24326 else if (what == IT_TRUNCATION)
24327 {
24328 /* Truncation glyph. */
24329 SET_GLYPH_FROM_CHAR (glyph, '$');
24330 if (it->dp
24331 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24332 {
24333 /* FIXME: Should we mirror GC for R2L lines? */
24334 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24335 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24336 }
24337 }
24338 else
24339 emacs_abort ();
24340
24341 #ifdef HAVE_WINDOW_SYSTEM
24342 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24343 is turned off, we precede the truncation/continuation glyphs by a
24344 stretch glyph whose width is computed such that these special
24345 glyphs are aligned at the window margin, even when very different
24346 fonts are used in different glyph rows. */
24347 if (FRAME_WINDOW_P (temp_it.f)
24348 /* init_iterator calls this with it->glyph_row == NULL, and it
24349 wants only the pixel width of the truncation/continuation
24350 glyphs. */
24351 && temp_it.glyph_row
24352 /* insert_left_trunc_glyphs calls us at the beginning of the
24353 row, and it has its own calculation of the stretch glyph
24354 width. */
24355 && temp_it.glyph_row->used[TEXT_AREA] > 0
24356 && (temp_it.glyph_row->reversed_p
24357 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24358 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24359 {
24360 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24361
24362 if (stretch_width > 0)
24363 {
24364 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24365 struct font *font =
24366 face->font ? face->font : FRAME_FONT (temp_it.f);
24367 int stretch_ascent =
24368 (((temp_it.ascent + temp_it.descent)
24369 * FONT_BASE (font)) / FONT_HEIGHT (font));
24370
24371 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24372 temp_it.ascent + temp_it.descent,
24373 stretch_ascent);
24374 }
24375 }
24376 #endif
24377
24378 temp_it.dp = NULL;
24379 temp_it.what = IT_CHARACTER;
24380 temp_it.len = 1;
24381 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24382 temp_it.face_id = GLYPH_FACE (glyph);
24383 temp_it.len = CHAR_BYTES (temp_it.c);
24384
24385 PRODUCE_GLYPHS (&temp_it);
24386 it->pixel_width = temp_it.pixel_width;
24387 it->nglyphs = temp_it.pixel_width;
24388 }
24389
24390 #ifdef HAVE_WINDOW_SYSTEM
24391
24392 /* Calculate line-height and line-spacing properties.
24393 An integer value specifies explicit pixel value.
24394 A float value specifies relative value to current face height.
24395 A cons (float . face-name) specifies relative value to
24396 height of specified face font.
24397
24398 Returns height in pixels, or nil. */
24399
24400
24401 static Lisp_Object
24402 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24403 int boff, int override)
24404 {
24405 Lisp_Object face_name = Qnil;
24406 int ascent, descent, height;
24407
24408 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24409 return val;
24410
24411 if (CONSP (val))
24412 {
24413 face_name = XCAR (val);
24414 val = XCDR (val);
24415 if (!NUMBERP (val))
24416 val = make_number (1);
24417 if (NILP (face_name))
24418 {
24419 height = it->ascent + it->descent;
24420 goto scale;
24421 }
24422 }
24423
24424 if (NILP (face_name))
24425 {
24426 font = FRAME_FONT (it->f);
24427 boff = FRAME_BASELINE_OFFSET (it->f);
24428 }
24429 else if (EQ (face_name, Qt))
24430 {
24431 override = 0;
24432 }
24433 else
24434 {
24435 int face_id;
24436 struct face *face;
24437
24438 face_id = lookup_named_face (it->f, face_name, 0);
24439 if (face_id < 0)
24440 return make_number (-1);
24441
24442 face = FACE_FROM_ID (it->f, face_id);
24443 font = face->font;
24444 if (font == NULL)
24445 return make_number (-1);
24446 boff = font->baseline_offset;
24447 if (font->vertical_centering)
24448 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24449 }
24450
24451 ascent = FONT_BASE (font) + boff;
24452 descent = FONT_DESCENT (font) - boff;
24453
24454 if (override)
24455 {
24456 it->override_ascent = ascent;
24457 it->override_descent = descent;
24458 it->override_boff = boff;
24459 }
24460
24461 height = ascent + descent;
24462
24463 scale:
24464 if (FLOATP (val))
24465 height = (int)(XFLOAT_DATA (val) * height);
24466 else if (INTEGERP (val))
24467 height *= XINT (val);
24468
24469 return make_number (height);
24470 }
24471
24472
24473 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24474 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24475 and only if this is for a character for which no font was found.
24476
24477 If the display method (it->glyphless_method) is
24478 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24479 length of the acronym or the hexadecimal string, UPPER_XOFF and
24480 UPPER_YOFF are pixel offsets for the upper part of the string,
24481 LOWER_XOFF and LOWER_YOFF are for the lower part.
24482
24483 For the other display methods, LEN through LOWER_YOFF are zero. */
24484
24485 static void
24486 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24487 short upper_xoff, short upper_yoff,
24488 short lower_xoff, short lower_yoff)
24489 {
24490 struct glyph *glyph;
24491 enum glyph_row_area area = it->area;
24492
24493 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24494 if (glyph < it->glyph_row->glyphs[area + 1])
24495 {
24496 /* If the glyph row is reversed, we need to prepend the glyph
24497 rather than append it. */
24498 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24499 {
24500 struct glyph *g;
24501
24502 /* Make room for the additional glyph. */
24503 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24504 g[1] = *g;
24505 glyph = it->glyph_row->glyphs[area];
24506 }
24507 glyph->charpos = CHARPOS (it->position);
24508 glyph->object = it->object;
24509 glyph->pixel_width = it->pixel_width;
24510 glyph->ascent = it->ascent;
24511 glyph->descent = it->descent;
24512 glyph->voffset = it->voffset;
24513 glyph->type = GLYPHLESS_GLYPH;
24514 glyph->u.glyphless.method = it->glyphless_method;
24515 glyph->u.glyphless.for_no_font = for_no_font;
24516 glyph->u.glyphless.len = len;
24517 glyph->u.glyphless.ch = it->c;
24518 glyph->slice.glyphless.upper_xoff = upper_xoff;
24519 glyph->slice.glyphless.upper_yoff = upper_yoff;
24520 glyph->slice.glyphless.lower_xoff = lower_xoff;
24521 glyph->slice.glyphless.lower_yoff = lower_yoff;
24522 glyph->avoid_cursor_p = it->avoid_cursor_p;
24523 glyph->multibyte_p = it->multibyte_p;
24524 glyph->left_box_line_p = it->start_of_box_run_p;
24525 glyph->right_box_line_p = it->end_of_box_run_p;
24526 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24527 || it->phys_descent > it->descent);
24528 glyph->padding_p = 0;
24529 glyph->glyph_not_available_p = 0;
24530 glyph->face_id = face_id;
24531 glyph->font_type = FONT_TYPE_UNKNOWN;
24532 if (it->bidi_p)
24533 {
24534 glyph->resolved_level = it->bidi_it.resolved_level;
24535 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24536 emacs_abort ();
24537 glyph->bidi_type = it->bidi_it.type;
24538 }
24539 ++it->glyph_row->used[area];
24540 }
24541 else
24542 IT_EXPAND_MATRIX_WIDTH (it, area);
24543 }
24544
24545
24546 /* Produce a glyph for a glyphless character for iterator IT.
24547 IT->glyphless_method specifies which method to use for displaying
24548 the character. See the description of enum
24549 glyphless_display_method in dispextern.h for the detail.
24550
24551 FOR_NO_FONT is nonzero if and only if this is for a character for
24552 which no font was found. ACRONYM, if non-nil, is an acronym string
24553 for the character. */
24554
24555 static void
24556 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24557 {
24558 int face_id;
24559 struct face *face;
24560 struct font *font;
24561 int base_width, base_height, width, height;
24562 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24563 int len;
24564
24565 /* Get the metrics of the base font. We always refer to the current
24566 ASCII face. */
24567 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24568 font = face->font ? face->font : FRAME_FONT (it->f);
24569 it->ascent = FONT_BASE (font) + font->baseline_offset;
24570 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24571 base_height = it->ascent + it->descent;
24572 base_width = font->average_width;
24573
24574 /* Get a face ID for the glyph by utilizing a cache (the same way as
24575 done for `escape-glyph' in get_next_display_element). */
24576 if (it->f == last_glyphless_glyph_frame
24577 && it->face_id == last_glyphless_glyph_face_id)
24578 {
24579 face_id = last_glyphless_glyph_merged_face_id;
24580 }
24581 else
24582 {
24583 /* Merge the `glyphless-char' face into the current face. */
24584 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24585 last_glyphless_glyph_frame = it->f;
24586 last_glyphless_glyph_face_id = it->face_id;
24587 last_glyphless_glyph_merged_face_id = face_id;
24588 }
24589
24590 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24591 {
24592 it->pixel_width = THIN_SPACE_WIDTH;
24593 len = 0;
24594 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24595 }
24596 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24597 {
24598 width = CHAR_WIDTH (it->c);
24599 if (width == 0)
24600 width = 1;
24601 else if (width > 4)
24602 width = 4;
24603 it->pixel_width = base_width * width;
24604 len = 0;
24605 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24606 }
24607 else
24608 {
24609 char buf[7];
24610 const char *str;
24611 unsigned int code[6];
24612 int upper_len;
24613 int ascent, descent;
24614 struct font_metrics metrics_upper, metrics_lower;
24615
24616 face = FACE_FROM_ID (it->f, face_id);
24617 font = face->font ? face->font : FRAME_FONT (it->f);
24618 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24619
24620 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24621 {
24622 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24623 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24624 if (CONSP (acronym))
24625 acronym = XCAR (acronym);
24626 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24627 }
24628 else
24629 {
24630 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24631 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24632 str = buf;
24633 }
24634 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24635 code[len] = font->driver->encode_char (font, str[len]);
24636 upper_len = (len + 1) / 2;
24637 font->driver->text_extents (font, code, upper_len,
24638 &metrics_upper);
24639 font->driver->text_extents (font, code + upper_len, len - upper_len,
24640 &metrics_lower);
24641
24642
24643
24644 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24645 width = max (metrics_upper.width, metrics_lower.width) + 4;
24646 upper_xoff = upper_yoff = 2; /* the typical case */
24647 if (base_width >= width)
24648 {
24649 /* Align the upper to the left, the lower to the right. */
24650 it->pixel_width = base_width;
24651 lower_xoff = base_width - 2 - metrics_lower.width;
24652 }
24653 else
24654 {
24655 /* Center the shorter one. */
24656 it->pixel_width = width;
24657 if (metrics_upper.width >= metrics_lower.width)
24658 lower_xoff = (width - metrics_lower.width) / 2;
24659 else
24660 {
24661 /* FIXME: This code doesn't look right. It formerly was
24662 missing the "lower_xoff = 0;", which couldn't have
24663 been right since it left lower_xoff uninitialized. */
24664 lower_xoff = 0;
24665 upper_xoff = (width - metrics_upper.width) / 2;
24666 }
24667 }
24668
24669 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24670 top, bottom, and between upper and lower strings. */
24671 height = (metrics_upper.ascent + metrics_upper.descent
24672 + metrics_lower.ascent + metrics_lower.descent) + 5;
24673 /* Center vertically.
24674 H:base_height, D:base_descent
24675 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24676
24677 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24678 descent = D - H/2 + h/2;
24679 lower_yoff = descent - 2 - ld;
24680 upper_yoff = lower_yoff - la - 1 - ud; */
24681 ascent = - (it->descent - (base_height + height + 1) / 2);
24682 descent = it->descent - (base_height - height) / 2;
24683 lower_yoff = descent - 2 - metrics_lower.descent;
24684 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24685 - metrics_upper.descent);
24686 /* Don't make the height shorter than the base height. */
24687 if (height > base_height)
24688 {
24689 it->ascent = ascent;
24690 it->descent = descent;
24691 }
24692 }
24693
24694 it->phys_ascent = it->ascent;
24695 it->phys_descent = it->descent;
24696 if (it->glyph_row)
24697 append_glyphless_glyph (it, face_id, for_no_font, len,
24698 upper_xoff, upper_yoff,
24699 lower_xoff, lower_yoff);
24700 it->nglyphs = 1;
24701 take_vertical_position_into_account (it);
24702 }
24703
24704
24705 /* RIF:
24706 Produce glyphs/get display metrics for the display element IT is
24707 loaded with. See the description of struct it in dispextern.h
24708 for an overview of struct it. */
24709
24710 void
24711 x_produce_glyphs (struct it *it)
24712 {
24713 int extra_line_spacing = it->extra_line_spacing;
24714
24715 it->glyph_not_available_p = 0;
24716
24717 if (it->what == IT_CHARACTER)
24718 {
24719 XChar2b char2b;
24720 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24721 struct font *font = face->font;
24722 struct font_metrics *pcm = NULL;
24723 int boff; /* baseline offset */
24724
24725 if (font == NULL)
24726 {
24727 /* When no suitable font is found, display this character by
24728 the method specified in the first extra slot of
24729 Vglyphless_char_display. */
24730 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24731
24732 eassert (it->what == IT_GLYPHLESS);
24733 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24734 goto done;
24735 }
24736
24737 boff = font->baseline_offset;
24738 if (font->vertical_centering)
24739 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24740
24741 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24742 {
24743 int stretched_p;
24744
24745 it->nglyphs = 1;
24746
24747 if (it->override_ascent >= 0)
24748 {
24749 it->ascent = it->override_ascent;
24750 it->descent = it->override_descent;
24751 boff = it->override_boff;
24752 }
24753 else
24754 {
24755 it->ascent = FONT_BASE (font) + boff;
24756 it->descent = FONT_DESCENT (font) - boff;
24757 }
24758
24759 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24760 {
24761 pcm = get_per_char_metric (font, &char2b);
24762 if (pcm->width == 0
24763 && pcm->rbearing == 0 && pcm->lbearing == 0)
24764 pcm = NULL;
24765 }
24766
24767 if (pcm)
24768 {
24769 it->phys_ascent = pcm->ascent + boff;
24770 it->phys_descent = pcm->descent - boff;
24771 it->pixel_width = pcm->width;
24772 }
24773 else
24774 {
24775 it->glyph_not_available_p = 1;
24776 it->phys_ascent = it->ascent;
24777 it->phys_descent = it->descent;
24778 it->pixel_width = font->space_width;
24779 }
24780
24781 if (it->constrain_row_ascent_descent_p)
24782 {
24783 if (it->descent > it->max_descent)
24784 {
24785 it->ascent += it->descent - it->max_descent;
24786 it->descent = it->max_descent;
24787 }
24788 if (it->ascent > it->max_ascent)
24789 {
24790 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24791 it->ascent = it->max_ascent;
24792 }
24793 it->phys_ascent = min (it->phys_ascent, it->ascent);
24794 it->phys_descent = min (it->phys_descent, it->descent);
24795 extra_line_spacing = 0;
24796 }
24797
24798 /* If this is a space inside a region of text with
24799 `space-width' property, change its width. */
24800 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24801 if (stretched_p)
24802 it->pixel_width *= XFLOATINT (it->space_width);
24803
24804 /* If face has a box, add the box thickness to the character
24805 height. If character has a box line to the left and/or
24806 right, add the box line width to the character's width. */
24807 if (face->box != FACE_NO_BOX)
24808 {
24809 int thick = face->box_line_width;
24810
24811 if (thick > 0)
24812 {
24813 it->ascent += thick;
24814 it->descent += thick;
24815 }
24816 else
24817 thick = -thick;
24818
24819 if (it->start_of_box_run_p)
24820 it->pixel_width += thick;
24821 if (it->end_of_box_run_p)
24822 it->pixel_width += thick;
24823 }
24824
24825 /* If face has an overline, add the height of the overline
24826 (1 pixel) and a 1 pixel margin to the character height. */
24827 if (face->overline_p)
24828 it->ascent += overline_margin;
24829
24830 if (it->constrain_row_ascent_descent_p)
24831 {
24832 if (it->ascent > it->max_ascent)
24833 it->ascent = it->max_ascent;
24834 if (it->descent > it->max_descent)
24835 it->descent = it->max_descent;
24836 }
24837
24838 take_vertical_position_into_account (it);
24839
24840 /* If we have to actually produce glyphs, do it. */
24841 if (it->glyph_row)
24842 {
24843 if (stretched_p)
24844 {
24845 /* Translate a space with a `space-width' property
24846 into a stretch glyph. */
24847 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24848 / FONT_HEIGHT (font));
24849 append_stretch_glyph (it, it->object, it->pixel_width,
24850 it->ascent + it->descent, ascent);
24851 }
24852 else
24853 append_glyph (it);
24854
24855 /* If characters with lbearing or rbearing are displayed
24856 in this line, record that fact in a flag of the
24857 glyph row. This is used to optimize X output code. */
24858 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24859 it->glyph_row->contains_overlapping_glyphs_p = 1;
24860 }
24861 if (! stretched_p && it->pixel_width == 0)
24862 /* We assure that all visible glyphs have at least 1-pixel
24863 width. */
24864 it->pixel_width = 1;
24865 }
24866 else if (it->char_to_display == '\n')
24867 {
24868 /* A newline has no width, but we need the height of the
24869 line. But if previous part of the line sets a height,
24870 don't increase that height */
24871
24872 Lisp_Object height;
24873 Lisp_Object total_height = Qnil;
24874
24875 it->override_ascent = -1;
24876 it->pixel_width = 0;
24877 it->nglyphs = 0;
24878
24879 height = get_it_property (it, Qline_height);
24880 /* Split (line-height total-height) list */
24881 if (CONSP (height)
24882 && CONSP (XCDR (height))
24883 && NILP (XCDR (XCDR (height))))
24884 {
24885 total_height = XCAR (XCDR (height));
24886 height = XCAR (height);
24887 }
24888 height = calc_line_height_property (it, height, font, boff, 1);
24889
24890 if (it->override_ascent >= 0)
24891 {
24892 it->ascent = it->override_ascent;
24893 it->descent = it->override_descent;
24894 boff = it->override_boff;
24895 }
24896 else
24897 {
24898 it->ascent = FONT_BASE (font) + boff;
24899 it->descent = FONT_DESCENT (font) - boff;
24900 }
24901
24902 if (EQ (height, Qt))
24903 {
24904 if (it->descent > it->max_descent)
24905 {
24906 it->ascent += it->descent - it->max_descent;
24907 it->descent = it->max_descent;
24908 }
24909 if (it->ascent > it->max_ascent)
24910 {
24911 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24912 it->ascent = it->max_ascent;
24913 }
24914 it->phys_ascent = min (it->phys_ascent, it->ascent);
24915 it->phys_descent = min (it->phys_descent, it->descent);
24916 it->constrain_row_ascent_descent_p = 1;
24917 extra_line_spacing = 0;
24918 }
24919 else
24920 {
24921 Lisp_Object spacing;
24922
24923 it->phys_ascent = it->ascent;
24924 it->phys_descent = it->descent;
24925
24926 if ((it->max_ascent > 0 || it->max_descent > 0)
24927 && face->box != FACE_NO_BOX
24928 && face->box_line_width > 0)
24929 {
24930 it->ascent += face->box_line_width;
24931 it->descent += face->box_line_width;
24932 }
24933 if (!NILP (height)
24934 && XINT (height) > it->ascent + it->descent)
24935 it->ascent = XINT (height) - it->descent;
24936
24937 if (!NILP (total_height))
24938 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24939 else
24940 {
24941 spacing = get_it_property (it, Qline_spacing);
24942 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24943 }
24944 if (INTEGERP (spacing))
24945 {
24946 extra_line_spacing = XINT (spacing);
24947 if (!NILP (total_height))
24948 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24949 }
24950 }
24951 }
24952 else /* i.e. (it->char_to_display == '\t') */
24953 {
24954 if (font->space_width > 0)
24955 {
24956 int tab_width = it->tab_width * font->space_width;
24957 int x = it->current_x + it->continuation_lines_width;
24958 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24959
24960 /* If the distance from the current position to the next tab
24961 stop is less than a space character width, use the
24962 tab stop after that. */
24963 if (next_tab_x - x < font->space_width)
24964 next_tab_x += tab_width;
24965
24966 it->pixel_width = next_tab_x - x;
24967 it->nglyphs = 1;
24968 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24969 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24970
24971 if (it->glyph_row)
24972 {
24973 append_stretch_glyph (it, it->object, it->pixel_width,
24974 it->ascent + it->descent, it->ascent);
24975 }
24976 }
24977 else
24978 {
24979 it->pixel_width = 0;
24980 it->nglyphs = 1;
24981 }
24982 }
24983 }
24984 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24985 {
24986 /* A static composition.
24987
24988 Note: A composition is represented as one glyph in the
24989 glyph matrix. There are no padding glyphs.
24990
24991 Important note: pixel_width, ascent, and descent are the
24992 values of what is drawn by draw_glyphs (i.e. the values of
24993 the overall glyphs composed). */
24994 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24995 int boff; /* baseline offset */
24996 struct composition *cmp = composition_table[it->cmp_it.id];
24997 int glyph_len = cmp->glyph_len;
24998 struct font *font = face->font;
24999
25000 it->nglyphs = 1;
25001
25002 /* If we have not yet calculated pixel size data of glyphs of
25003 the composition for the current face font, calculate them
25004 now. Theoretically, we have to check all fonts for the
25005 glyphs, but that requires much time and memory space. So,
25006 here we check only the font of the first glyph. This may
25007 lead to incorrect display, but it's very rare, and C-l
25008 (recenter-top-bottom) can correct the display anyway. */
25009 if (! cmp->font || cmp->font != font)
25010 {
25011 /* Ascent and descent of the font of the first character
25012 of this composition (adjusted by baseline offset).
25013 Ascent and descent of overall glyphs should not be less
25014 than these, respectively. */
25015 int font_ascent, font_descent, font_height;
25016 /* Bounding box of the overall glyphs. */
25017 int leftmost, rightmost, lowest, highest;
25018 int lbearing, rbearing;
25019 int i, width, ascent, descent;
25020 int left_padded = 0, right_padded = 0;
25021 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25022 XChar2b char2b;
25023 struct font_metrics *pcm;
25024 int font_not_found_p;
25025 ptrdiff_t pos;
25026
25027 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25028 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25029 break;
25030 if (glyph_len < cmp->glyph_len)
25031 right_padded = 1;
25032 for (i = 0; i < glyph_len; i++)
25033 {
25034 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25035 break;
25036 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25037 }
25038 if (i > 0)
25039 left_padded = 1;
25040
25041 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25042 : IT_CHARPOS (*it));
25043 /* If no suitable font is found, use the default font. */
25044 font_not_found_p = font == NULL;
25045 if (font_not_found_p)
25046 {
25047 face = face->ascii_face;
25048 font = face->font;
25049 }
25050 boff = font->baseline_offset;
25051 if (font->vertical_centering)
25052 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25053 font_ascent = FONT_BASE (font) + boff;
25054 font_descent = FONT_DESCENT (font) - boff;
25055 font_height = FONT_HEIGHT (font);
25056
25057 cmp->font = font;
25058
25059 pcm = NULL;
25060 if (! font_not_found_p)
25061 {
25062 get_char_face_and_encoding (it->f, c, it->face_id,
25063 &char2b, 0);
25064 pcm = get_per_char_metric (font, &char2b);
25065 }
25066
25067 /* Initialize the bounding box. */
25068 if (pcm)
25069 {
25070 width = cmp->glyph_len > 0 ? pcm->width : 0;
25071 ascent = pcm->ascent;
25072 descent = pcm->descent;
25073 lbearing = pcm->lbearing;
25074 rbearing = pcm->rbearing;
25075 }
25076 else
25077 {
25078 width = cmp->glyph_len > 0 ? font->space_width : 0;
25079 ascent = FONT_BASE (font);
25080 descent = FONT_DESCENT (font);
25081 lbearing = 0;
25082 rbearing = width;
25083 }
25084
25085 rightmost = width;
25086 leftmost = 0;
25087 lowest = - descent + boff;
25088 highest = ascent + boff;
25089
25090 if (! font_not_found_p
25091 && font->default_ascent
25092 && CHAR_TABLE_P (Vuse_default_ascent)
25093 && !NILP (Faref (Vuse_default_ascent,
25094 make_number (it->char_to_display))))
25095 highest = font->default_ascent + boff;
25096
25097 /* Draw the first glyph at the normal position. It may be
25098 shifted to right later if some other glyphs are drawn
25099 at the left. */
25100 cmp->offsets[i * 2] = 0;
25101 cmp->offsets[i * 2 + 1] = boff;
25102 cmp->lbearing = lbearing;
25103 cmp->rbearing = rbearing;
25104
25105 /* Set cmp->offsets for the remaining glyphs. */
25106 for (i++; i < glyph_len; i++)
25107 {
25108 int left, right, btm, top;
25109 int ch = COMPOSITION_GLYPH (cmp, i);
25110 int face_id;
25111 struct face *this_face;
25112
25113 if (ch == '\t')
25114 ch = ' ';
25115 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25116 this_face = FACE_FROM_ID (it->f, face_id);
25117 font = this_face->font;
25118
25119 if (font == NULL)
25120 pcm = NULL;
25121 else
25122 {
25123 get_char_face_and_encoding (it->f, ch, face_id,
25124 &char2b, 0);
25125 pcm = get_per_char_metric (font, &char2b);
25126 }
25127 if (! pcm)
25128 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25129 else
25130 {
25131 width = pcm->width;
25132 ascent = pcm->ascent;
25133 descent = pcm->descent;
25134 lbearing = pcm->lbearing;
25135 rbearing = pcm->rbearing;
25136 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25137 {
25138 /* Relative composition with or without
25139 alternate chars. */
25140 left = (leftmost + rightmost - width) / 2;
25141 btm = - descent + boff;
25142 if (font->relative_compose
25143 && (! CHAR_TABLE_P (Vignore_relative_composition)
25144 || NILP (Faref (Vignore_relative_composition,
25145 make_number (ch)))))
25146 {
25147
25148 if (- descent >= font->relative_compose)
25149 /* One extra pixel between two glyphs. */
25150 btm = highest + 1;
25151 else if (ascent <= 0)
25152 /* One extra pixel between two glyphs. */
25153 btm = lowest - 1 - ascent - descent;
25154 }
25155 }
25156 else
25157 {
25158 /* A composition rule is specified by an integer
25159 value that encodes global and new reference
25160 points (GREF and NREF). GREF and NREF are
25161 specified by numbers as below:
25162
25163 0---1---2 -- ascent
25164 | |
25165 | |
25166 | |
25167 9--10--11 -- center
25168 | |
25169 ---3---4---5--- baseline
25170 | |
25171 6---7---8 -- descent
25172 */
25173 int rule = COMPOSITION_RULE (cmp, i);
25174 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25175
25176 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25177 grefx = gref % 3, nrefx = nref % 3;
25178 grefy = gref / 3, nrefy = nref / 3;
25179 if (xoff)
25180 xoff = font_height * (xoff - 128) / 256;
25181 if (yoff)
25182 yoff = font_height * (yoff - 128) / 256;
25183
25184 left = (leftmost
25185 + grefx * (rightmost - leftmost) / 2
25186 - nrefx * width / 2
25187 + xoff);
25188
25189 btm = ((grefy == 0 ? highest
25190 : grefy == 1 ? 0
25191 : grefy == 2 ? lowest
25192 : (highest + lowest) / 2)
25193 - (nrefy == 0 ? ascent + descent
25194 : nrefy == 1 ? descent - boff
25195 : nrefy == 2 ? 0
25196 : (ascent + descent) / 2)
25197 + yoff);
25198 }
25199
25200 cmp->offsets[i * 2] = left;
25201 cmp->offsets[i * 2 + 1] = btm + descent;
25202
25203 /* Update the bounding box of the overall glyphs. */
25204 if (width > 0)
25205 {
25206 right = left + width;
25207 if (left < leftmost)
25208 leftmost = left;
25209 if (right > rightmost)
25210 rightmost = right;
25211 }
25212 top = btm + descent + ascent;
25213 if (top > highest)
25214 highest = top;
25215 if (btm < lowest)
25216 lowest = btm;
25217
25218 if (cmp->lbearing > left + lbearing)
25219 cmp->lbearing = left + lbearing;
25220 if (cmp->rbearing < left + rbearing)
25221 cmp->rbearing = left + rbearing;
25222 }
25223 }
25224
25225 /* If there are glyphs whose x-offsets are negative,
25226 shift all glyphs to the right and make all x-offsets
25227 non-negative. */
25228 if (leftmost < 0)
25229 {
25230 for (i = 0; i < cmp->glyph_len; i++)
25231 cmp->offsets[i * 2] -= leftmost;
25232 rightmost -= leftmost;
25233 cmp->lbearing -= leftmost;
25234 cmp->rbearing -= leftmost;
25235 }
25236
25237 if (left_padded && cmp->lbearing < 0)
25238 {
25239 for (i = 0; i < cmp->glyph_len; i++)
25240 cmp->offsets[i * 2] -= cmp->lbearing;
25241 rightmost -= cmp->lbearing;
25242 cmp->rbearing -= cmp->lbearing;
25243 cmp->lbearing = 0;
25244 }
25245 if (right_padded && rightmost < cmp->rbearing)
25246 {
25247 rightmost = cmp->rbearing;
25248 }
25249
25250 cmp->pixel_width = rightmost;
25251 cmp->ascent = highest;
25252 cmp->descent = - lowest;
25253 if (cmp->ascent < font_ascent)
25254 cmp->ascent = font_ascent;
25255 if (cmp->descent < font_descent)
25256 cmp->descent = font_descent;
25257 }
25258
25259 if (it->glyph_row
25260 && (cmp->lbearing < 0
25261 || cmp->rbearing > cmp->pixel_width))
25262 it->glyph_row->contains_overlapping_glyphs_p = 1;
25263
25264 it->pixel_width = cmp->pixel_width;
25265 it->ascent = it->phys_ascent = cmp->ascent;
25266 it->descent = it->phys_descent = cmp->descent;
25267 if (face->box != FACE_NO_BOX)
25268 {
25269 int thick = face->box_line_width;
25270
25271 if (thick > 0)
25272 {
25273 it->ascent += thick;
25274 it->descent += thick;
25275 }
25276 else
25277 thick = - thick;
25278
25279 if (it->start_of_box_run_p)
25280 it->pixel_width += thick;
25281 if (it->end_of_box_run_p)
25282 it->pixel_width += thick;
25283 }
25284
25285 /* If face has an overline, add the height of the overline
25286 (1 pixel) and a 1 pixel margin to the character height. */
25287 if (face->overline_p)
25288 it->ascent += overline_margin;
25289
25290 take_vertical_position_into_account (it);
25291 if (it->ascent < 0)
25292 it->ascent = 0;
25293 if (it->descent < 0)
25294 it->descent = 0;
25295
25296 if (it->glyph_row && cmp->glyph_len > 0)
25297 append_composite_glyph (it);
25298 }
25299 else if (it->what == IT_COMPOSITION)
25300 {
25301 /* A dynamic (automatic) composition. */
25302 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25303 Lisp_Object gstring;
25304 struct font_metrics metrics;
25305
25306 it->nglyphs = 1;
25307
25308 gstring = composition_gstring_from_id (it->cmp_it.id);
25309 it->pixel_width
25310 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25311 &metrics);
25312 if (it->glyph_row
25313 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25314 it->glyph_row->contains_overlapping_glyphs_p = 1;
25315 it->ascent = it->phys_ascent = metrics.ascent;
25316 it->descent = it->phys_descent = metrics.descent;
25317 if (face->box != FACE_NO_BOX)
25318 {
25319 int thick = face->box_line_width;
25320
25321 if (thick > 0)
25322 {
25323 it->ascent += thick;
25324 it->descent += thick;
25325 }
25326 else
25327 thick = - thick;
25328
25329 if (it->start_of_box_run_p)
25330 it->pixel_width += thick;
25331 if (it->end_of_box_run_p)
25332 it->pixel_width += thick;
25333 }
25334 /* If face has an overline, add the height of the overline
25335 (1 pixel) and a 1 pixel margin to the character height. */
25336 if (face->overline_p)
25337 it->ascent += overline_margin;
25338 take_vertical_position_into_account (it);
25339 if (it->ascent < 0)
25340 it->ascent = 0;
25341 if (it->descent < 0)
25342 it->descent = 0;
25343
25344 if (it->glyph_row)
25345 append_composite_glyph (it);
25346 }
25347 else if (it->what == IT_GLYPHLESS)
25348 produce_glyphless_glyph (it, 0, Qnil);
25349 else if (it->what == IT_IMAGE)
25350 produce_image_glyph (it);
25351 else if (it->what == IT_STRETCH)
25352 produce_stretch_glyph (it);
25353
25354 done:
25355 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25356 because this isn't true for images with `:ascent 100'. */
25357 eassert (it->ascent >= 0 && it->descent >= 0);
25358 if (it->area == TEXT_AREA)
25359 it->current_x += it->pixel_width;
25360
25361 if (extra_line_spacing > 0)
25362 {
25363 it->descent += extra_line_spacing;
25364 if (extra_line_spacing > it->max_extra_line_spacing)
25365 it->max_extra_line_spacing = extra_line_spacing;
25366 }
25367
25368 it->max_ascent = max (it->max_ascent, it->ascent);
25369 it->max_descent = max (it->max_descent, it->descent);
25370 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25371 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25372 }
25373
25374 /* EXPORT for RIF:
25375 Output LEN glyphs starting at START at the nominal cursor position.
25376 Advance the nominal cursor over the text. The global variable
25377 updated_window contains the window being updated, updated_row is
25378 the glyph row being updated, and updated_area is the area of that
25379 row being updated. */
25380
25381 void
25382 x_write_glyphs (struct glyph *start, int len)
25383 {
25384 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25385
25386 eassert (updated_window && updated_row);
25387 /* When the window is hscrolled, cursor hpos can legitimately be out
25388 of bounds, but we draw the cursor at the corresponding window
25389 margin in that case. */
25390 if (!updated_row->reversed_p && chpos < 0)
25391 chpos = 0;
25392 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25393 chpos = updated_row->used[TEXT_AREA] - 1;
25394
25395 block_input ();
25396
25397 /* Write glyphs. */
25398
25399 hpos = start - updated_row->glyphs[updated_area];
25400 x = draw_glyphs (updated_window, output_cursor.x,
25401 updated_row, updated_area,
25402 hpos, hpos + len,
25403 DRAW_NORMAL_TEXT, 0);
25404
25405 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25406 if (updated_area == TEXT_AREA
25407 && updated_window->phys_cursor_on_p
25408 && updated_window->phys_cursor.vpos == output_cursor.vpos
25409 && chpos >= hpos
25410 && chpos < hpos + len)
25411 updated_window->phys_cursor_on_p = 0;
25412
25413 unblock_input ();
25414
25415 /* Advance the output cursor. */
25416 output_cursor.hpos += len;
25417 output_cursor.x = x;
25418 }
25419
25420
25421 /* EXPORT for RIF:
25422 Insert LEN glyphs from START at the nominal cursor position. */
25423
25424 void
25425 x_insert_glyphs (struct glyph *start, int len)
25426 {
25427 struct frame *f;
25428 struct window *w;
25429 int line_height, shift_by_width, shifted_region_width;
25430 struct glyph_row *row;
25431 struct glyph *glyph;
25432 int frame_x, frame_y;
25433 ptrdiff_t hpos;
25434
25435 eassert (updated_window && updated_row);
25436 block_input ();
25437 w = updated_window;
25438 f = XFRAME (WINDOW_FRAME (w));
25439
25440 /* Get the height of the line we are in. */
25441 row = updated_row;
25442 line_height = row->height;
25443
25444 /* Get the width of the glyphs to insert. */
25445 shift_by_width = 0;
25446 for (glyph = start; glyph < start + len; ++glyph)
25447 shift_by_width += glyph->pixel_width;
25448
25449 /* Get the width of the region to shift right. */
25450 shifted_region_width = (window_box_width (w, updated_area)
25451 - output_cursor.x
25452 - shift_by_width);
25453
25454 /* Shift right. */
25455 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25456 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25457
25458 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25459 line_height, shift_by_width);
25460
25461 /* Write the glyphs. */
25462 hpos = start - row->glyphs[updated_area];
25463 draw_glyphs (w, output_cursor.x, row, updated_area,
25464 hpos, hpos + len,
25465 DRAW_NORMAL_TEXT, 0);
25466
25467 /* Advance the output cursor. */
25468 output_cursor.hpos += len;
25469 output_cursor.x += shift_by_width;
25470 unblock_input ();
25471 }
25472
25473
25474 /* EXPORT for RIF:
25475 Erase the current text line from the nominal cursor position
25476 (inclusive) to pixel column TO_X (exclusive). The idea is that
25477 everything from TO_X onward is already erased.
25478
25479 TO_X is a pixel position relative to updated_area of
25480 updated_window. TO_X == -1 means clear to the end of this area. */
25481
25482 void
25483 x_clear_end_of_line (int to_x)
25484 {
25485 struct frame *f;
25486 struct window *w = updated_window;
25487 int max_x, min_y, max_y;
25488 int from_x, from_y, to_y;
25489
25490 eassert (updated_window && updated_row);
25491 f = XFRAME (w->frame);
25492
25493 if (updated_row->full_width_p)
25494 max_x = WINDOW_TOTAL_WIDTH (w);
25495 else
25496 max_x = window_box_width (w, updated_area);
25497 max_y = window_text_bottom_y (w);
25498
25499 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25500 of window. For TO_X > 0, truncate to end of drawing area. */
25501 if (to_x == 0)
25502 return;
25503 else if (to_x < 0)
25504 to_x = max_x;
25505 else
25506 to_x = min (to_x, max_x);
25507
25508 to_y = min (max_y, output_cursor.y + updated_row->height);
25509
25510 /* Notice if the cursor will be cleared by this operation. */
25511 if (!updated_row->full_width_p)
25512 notice_overwritten_cursor (w, updated_area,
25513 output_cursor.x, -1,
25514 updated_row->y,
25515 MATRIX_ROW_BOTTOM_Y (updated_row));
25516
25517 from_x = output_cursor.x;
25518
25519 /* Translate to frame coordinates. */
25520 if (updated_row->full_width_p)
25521 {
25522 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25523 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25524 }
25525 else
25526 {
25527 int area_left = window_box_left (w, updated_area);
25528 from_x += area_left;
25529 to_x += area_left;
25530 }
25531
25532 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25533 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25534 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25535
25536 /* Prevent inadvertently clearing to end of the X window. */
25537 if (to_x > from_x && to_y > from_y)
25538 {
25539 block_input ();
25540 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25541 to_x - from_x, to_y - from_y);
25542 unblock_input ();
25543 }
25544 }
25545
25546 #endif /* HAVE_WINDOW_SYSTEM */
25547
25548
25549 \f
25550 /***********************************************************************
25551 Cursor types
25552 ***********************************************************************/
25553
25554 /* Value is the internal representation of the specified cursor type
25555 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25556 of the bar cursor. */
25557
25558 static enum text_cursor_kinds
25559 get_specified_cursor_type (Lisp_Object arg, int *width)
25560 {
25561 enum text_cursor_kinds type;
25562
25563 if (NILP (arg))
25564 return NO_CURSOR;
25565
25566 if (EQ (arg, Qbox))
25567 return FILLED_BOX_CURSOR;
25568
25569 if (EQ (arg, Qhollow))
25570 return HOLLOW_BOX_CURSOR;
25571
25572 if (EQ (arg, Qbar))
25573 {
25574 *width = 2;
25575 return BAR_CURSOR;
25576 }
25577
25578 if (CONSP (arg)
25579 && EQ (XCAR (arg), Qbar)
25580 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25581 {
25582 *width = XINT (XCDR (arg));
25583 return BAR_CURSOR;
25584 }
25585
25586 if (EQ (arg, Qhbar))
25587 {
25588 *width = 2;
25589 return HBAR_CURSOR;
25590 }
25591
25592 if (CONSP (arg)
25593 && EQ (XCAR (arg), Qhbar)
25594 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25595 {
25596 *width = XINT (XCDR (arg));
25597 return HBAR_CURSOR;
25598 }
25599
25600 /* Treat anything unknown as "hollow box cursor".
25601 It was bad to signal an error; people have trouble fixing
25602 .Xdefaults with Emacs, when it has something bad in it. */
25603 type = HOLLOW_BOX_CURSOR;
25604
25605 return type;
25606 }
25607
25608 /* Set the default cursor types for specified frame. */
25609 void
25610 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25611 {
25612 int width = 1;
25613 Lisp_Object tem;
25614
25615 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25616 FRAME_CURSOR_WIDTH (f) = width;
25617
25618 /* By default, set up the blink-off state depending on the on-state. */
25619
25620 tem = Fassoc (arg, Vblink_cursor_alist);
25621 if (!NILP (tem))
25622 {
25623 FRAME_BLINK_OFF_CURSOR (f)
25624 = get_specified_cursor_type (XCDR (tem), &width);
25625 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25626 }
25627 else
25628 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25629 }
25630
25631
25632 #ifdef HAVE_WINDOW_SYSTEM
25633
25634 /* Return the cursor we want to be displayed in window W. Return
25635 width of bar/hbar cursor through WIDTH arg. Return with
25636 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25637 (i.e. if the `system caret' should track this cursor).
25638
25639 In a mini-buffer window, we want the cursor only to appear if we
25640 are reading input from this window. For the selected window, we
25641 want the cursor type given by the frame parameter or buffer local
25642 setting of cursor-type. If explicitly marked off, draw no cursor.
25643 In all other cases, we want a hollow box cursor. */
25644
25645 static enum text_cursor_kinds
25646 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25647 int *active_cursor)
25648 {
25649 struct frame *f = XFRAME (w->frame);
25650 struct buffer *b = XBUFFER (w->buffer);
25651 int cursor_type = DEFAULT_CURSOR;
25652 Lisp_Object alt_cursor;
25653 int non_selected = 0;
25654
25655 *active_cursor = 1;
25656
25657 /* Echo area */
25658 if (cursor_in_echo_area
25659 && FRAME_HAS_MINIBUF_P (f)
25660 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25661 {
25662 if (w == XWINDOW (echo_area_window))
25663 {
25664 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25665 {
25666 *width = FRAME_CURSOR_WIDTH (f);
25667 return FRAME_DESIRED_CURSOR (f);
25668 }
25669 else
25670 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25671 }
25672
25673 *active_cursor = 0;
25674 non_selected = 1;
25675 }
25676
25677 /* Detect a nonselected window or nonselected frame. */
25678 else if (w != XWINDOW (f->selected_window)
25679 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25680 {
25681 *active_cursor = 0;
25682
25683 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25684 return NO_CURSOR;
25685
25686 non_selected = 1;
25687 }
25688
25689 /* Never display a cursor in a window in which cursor-type is nil. */
25690 if (NILP (BVAR (b, cursor_type)))
25691 return NO_CURSOR;
25692
25693 /* Get the normal cursor type for this window. */
25694 if (EQ (BVAR (b, cursor_type), Qt))
25695 {
25696 cursor_type = FRAME_DESIRED_CURSOR (f);
25697 *width = FRAME_CURSOR_WIDTH (f);
25698 }
25699 else
25700 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25701
25702 /* Use cursor-in-non-selected-windows instead
25703 for non-selected window or frame. */
25704 if (non_selected)
25705 {
25706 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25707 if (!EQ (Qt, alt_cursor))
25708 return get_specified_cursor_type (alt_cursor, width);
25709 /* t means modify the normal cursor type. */
25710 if (cursor_type == FILLED_BOX_CURSOR)
25711 cursor_type = HOLLOW_BOX_CURSOR;
25712 else if (cursor_type == BAR_CURSOR && *width > 1)
25713 --*width;
25714 return cursor_type;
25715 }
25716
25717 /* Use normal cursor if not blinked off. */
25718 if (!w->cursor_off_p)
25719 {
25720 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25721 {
25722 if (cursor_type == FILLED_BOX_CURSOR)
25723 {
25724 /* Using a block cursor on large images can be very annoying.
25725 So use a hollow cursor for "large" images.
25726 If image is not transparent (no mask), also use hollow cursor. */
25727 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25728 if (img != NULL && IMAGEP (img->spec))
25729 {
25730 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25731 where N = size of default frame font size.
25732 This should cover most of the "tiny" icons people may use. */
25733 if (!img->mask
25734 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25735 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25736 cursor_type = HOLLOW_BOX_CURSOR;
25737 }
25738 }
25739 else if (cursor_type != NO_CURSOR)
25740 {
25741 /* Display current only supports BOX and HOLLOW cursors for images.
25742 So for now, unconditionally use a HOLLOW cursor when cursor is
25743 not a solid box cursor. */
25744 cursor_type = HOLLOW_BOX_CURSOR;
25745 }
25746 }
25747 return cursor_type;
25748 }
25749
25750 /* Cursor is blinked off, so determine how to "toggle" it. */
25751
25752 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25753 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25754 return get_specified_cursor_type (XCDR (alt_cursor), width);
25755
25756 /* Then see if frame has specified a specific blink off cursor type. */
25757 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25758 {
25759 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25760 return FRAME_BLINK_OFF_CURSOR (f);
25761 }
25762
25763 #if 0
25764 /* Some people liked having a permanently visible blinking cursor,
25765 while others had very strong opinions against it. So it was
25766 decided to remove it. KFS 2003-09-03 */
25767
25768 /* Finally perform built-in cursor blinking:
25769 filled box <-> hollow box
25770 wide [h]bar <-> narrow [h]bar
25771 narrow [h]bar <-> no cursor
25772 other type <-> no cursor */
25773
25774 if (cursor_type == FILLED_BOX_CURSOR)
25775 return HOLLOW_BOX_CURSOR;
25776
25777 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25778 {
25779 *width = 1;
25780 return cursor_type;
25781 }
25782 #endif
25783
25784 return NO_CURSOR;
25785 }
25786
25787
25788 /* Notice when the text cursor of window W has been completely
25789 overwritten by a drawing operation that outputs glyphs in AREA
25790 starting at X0 and ending at X1 in the line starting at Y0 and
25791 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25792 the rest of the line after X0 has been written. Y coordinates
25793 are window-relative. */
25794
25795 static void
25796 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25797 int x0, int x1, int y0, int y1)
25798 {
25799 int cx0, cx1, cy0, cy1;
25800 struct glyph_row *row;
25801
25802 if (!w->phys_cursor_on_p)
25803 return;
25804 if (area != TEXT_AREA)
25805 return;
25806
25807 if (w->phys_cursor.vpos < 0
25808 || w->phys_cursor.vpos >= w->current_matrix->nrows
25809 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25810 !(row->enabled_p && row->displays_text_p)))
25811 return;
25812
25813 if (row->cursor_in_fringe_p)
25814 {
25815 row->cursor_in_fringe_p = 0;
25816 draw_fringe_bitmap (w, row, row->reversed_p);
25817 w->phys_cursor_on_p = 0;
25818 return;
25819 }
25820
25821 cx0 = w->phys_cursor.x;
25822 cx1 = cx0 + w->phys_cursor_width;
25823 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25824 return;
25825
25826 /* The cursor image will be completely removed from the
25827 screen if the output area intersects the cursor area in
25828 y-direction. When we draw in [y0 y1[, and some part of
25829 the cursor is at y < y0, that part must have been drawn
25830 before. When scrolling, the cursor is erased before
25831 actually scrolling, so we don't come here. When not
25832 scrolling, the rows above the old cursor row must have
25833 changed, and in this case these rows must have written
25834 over the cursor image.
25835
25836 Likewise if part of the cursor is below y1, with the
25837 exception of the cursor being in the first blank row at
25838 the buffer and window end because update_text_area
25839 doesn't draw that row. (Except when it does, but
25840 that's handled in update_text_area.) */
25841
25842 cy0 = w->phys_cursor.y;
25843 cy1 = cy0 + w->phys_cursor_height;
25844 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25845 return;
25846
25847 w->phys_cursor_on_p = 0;
25848 }
25849
25850 #endif /* HAVE_WINDOW_SYSTEM */
25851
25852 \f
25853 /************************************************************************
25854 Mouse Face
25855 ************************************************************************/
25856
25857 #ifdef HAVE_WINDOW_SYSTEM
25858
25859 /* EXPORT for RIF:
25860 Fix the display of area AREA of overlapping row ROW in window W
25861 with respect to the overlapping part OVERLAPS. */
25862
25863 void
25864 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25865 enum glyph_row_area area, int overlaps)
25866 {
25867 int i, x;
25868
25869 block_input ();
25870
25871 x = 0;
25872 for (i = 0; i < row->used[area];)
25873 {
25874 if (row->glyphs[area][i].overlaps_vertically_p)
25875 {
25876 int start = i, start_x = x;
25877
25878 do
25879 {
25880 x += row->glyphs[area][i].pixel_width;
25881 ++i;
25882 }
25883 while (i < row->used[area]
25884 && row->glyphs[area][i].overlaps_vertically_p);
25885
25886 draw_glyphs (w, start_x, row, area,
25887 start, i,
25888 DRAW_NORMAL_TEXT, overlaps);
25889 }
25890 else
25891 {
25892 x += row->glyphs[area][i].pixel_width;
25893 ++i;
25894 }
25895 }
25896
25897 unblock_input ();
25898 }
25899
25900
25901 /* EXPORT:
25902 Draw the cursor glyph of window W in glyph row ROW. See the
25903 comment of draw_glyphs for the meaning of HL. */
25904
25905 void
25906 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25907 enum draw_glyphs_face hl)
25908 {
25909 /* If cursor hpos is out of bounds, don't draw garbage. This can
25910 happen in mini-buffer windows when switching between echo area
25911 glyphs and mini-buffer. */
25912 if ((row->reversed_p
25913 ? (w->phys_cursor.hpos >= 0)
25914 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25915 {
25916 int on_p = w->phys_cursor_on_p;
25917 int x1;
25918 int hpos = w->phys_cursor.hpos;
25919
25920 /* When the window is hscrolled, cursor hpos can legitimately be
25921 out of bounds, but we draw the cursor at the corresponding
25922 window margin in that case. */
25923 if (!row->reversed_p && hpos < 0)
25924 hpos = 0;
25925 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25926 hpos = row->used[TEXT_AREA] - 1;
25927
25928 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25929 hl, 0);
25930 w->phys_cursor_on_p = on_p;
25931
25932 if (hl == DRAW_CURSOR)
25933 w->phys_cursor_width = x1 - w->phys_cursor.x;
25934 /* When we erase the cursor, and ROW is overlapped by other
25935 rows, make sure that these overlapping parts of other rows
25936 are redrawn. */
25937 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25938 {
25939 w->phys_cursor_width = x1 - w->phys_cursor.x;
25940
25941 if (row > w->current_matrix->rows
25942 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25943 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25944 OVERLAPS_ERASED_CURSOR);
25945
25946 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25947 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25948 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25949 OVERLAPS_ERASED_CURSOR);
25950 }
25951 }
25952 }
25953
25954
25955 /* EXPORT:
25956 Erase the image of a cursor of window W from the screen. */
25957
25958 void
25959 erase_phys_cursor (struct window *w)
25960 {
25961 struct frame *f = XFRAME (w->frame);
25962 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25963 int hpos = w->phys_cursor.hpos;
25964 int vpos = w->phys_cursor.vpos;
25965 int mouse_face_here_p = 0;
25966 struct glyph_matrix *active_glyphs = w->current_matrix;
25967 struct glyph_row *cursor_row;
25968 struct glyph *cursor_glyph;
25969 enum draw_glyphs_face hl;
25970
25971 /* No cursor displayed or row invalidated => nothing to do on the
25972 screen. */
25973 if (w->phys_cursor_type == NO_CURSOR)
25974 goto mark_cursor_off;
25975
25976 /* VPOS >= active_glyphs->nrows means that window has been resized.
25977 Don't bother to erase the cursor. */
25978 if (vpos >= active_glyphs->nrows)
25979 goto mark_cursor_off;
25980
25981 /* If row containing cursor is marked invalid, there is nothing we
25982 can do. */
25983 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25984 if (!cursor_row->enabled_p)
25985 goto mark_cursor_off;
25986
25987 /* If line spacing is > 0, old cursor may only be partially visible in
25988 window after split-window. So adjust visible height. */
25989 cursor_row->visible_height = min (cursor_row->visible_height,
25990 window_text_bottom_y (w) - cursor_row->y);
25991
25992 /* If row is completely invisible, don't attempt to delete a cursor which
25993 isn't there. This can happen if cursor is at top of a window, and
25994 we switch to a buffer with a header line in that window. */
25995 if (cursor_row->visible_height <= 0)
25996 goto mark_cursor_off;
25997
25998 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25999 if (cursor_row->cursor_in_fringe_p)
26000 {
26001 cursor_row->cursor_in_fringe_p = 0;
26002 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26003 goto mark_cursor_off;
26004 }
26005
26006 /* This can happen when the new row is shorter than the old one.
26007 In this case, either draw_glyphs or clear_end_of_line
26008 should have cleared the cursor. Note that we wouldn't be
26009 able to erase the cursor in this case because we don't have a
26010 cursor glyph at hand. */
26011 if ((cursor_row->reversed_p
26012 ? (w->phys_cursor.hpos < 0)
26013 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26014 goto mark_cursor_off;
26015
26016 /* When the window is hscrolled, cursor hpos can legitimately be out
26017 of bounds, but we draw the cursor at the corresponding window
26018 margin in that case. */
26019 if (!cursor_row->reversed_p && hpos < 0)
26020 hpos = 0;
26021 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26022 hpos = cursor_row->used[TEXT_AREA] - 1;
26023
26024 /* If the cursor is in the mouse face area, redisplay that when
26025 we clear the cursor. */
26026 if (! NILP (hlinfo->mouse_face_window)
26027 && coords_in_mouse_face_p (w, hpos, vpos)
26028 /* Don't redraw the cursor's spot in mouse face if it is at the
26029 end of a line (on a newline). The cursor appears there, but
26030 mouse highlighting does not. */
26031 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26032 mouse_face_here_p = 1;
26033
26034 /* Maybe clear the display under the cursor. */
26035 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26036 {
26037 int x, y, left_x;
26038 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26039 int width;
26040
26041 cursor_glyph = get_phys_cursor_glyph (w);
26042 if (cursor_glyph == NULL)
26043 goto mark_cursor_off;
26044
26045 width = cursor_glyph->pixel_width;
26046 left_x = window_box_left_offset (w, TEXT_AREA);
26047 x = w->phys_cursor.x;
26048 if (x < left_x)
26049 width -= left_x - x;
26050 width = min (width, window_box_width (w, TEXT_AREA) - x);
26051 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26052 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26053
26054 if (width > 0)
26055 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26056 }
26057
26058 /* Erase the cursor by redrawing the character underneath it. */
26059 if (mouse_face_here_p)
26060 hl = DRAW_MOUSE_FACE;
26061 else
26062 hl = DRAW_NORMAL_TEXT;
26063 draw_phys_cursor_glyph (w, cursor_row, hl);
26064
26065 mark_cursor_off:
26066 w->phys_cursor_on_p = 0;
26067 w->phys_cursor_type = NO_CURSOR;
26068 }
26069
26070
26071 /* EXPORT:
26072 Display or clear cursor of window W. If ON is zero, clear the
26073 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26074 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26075
26076 void
26077 display_and_set_cursor (struct window *w, int on,
26078 int hpos, int vpos, int x, int y)
26079 {
26080 struct frame *f = XFRAME (w->frame);
26081 int new_cursor_type;
26082 int new_cursor_width;
26083 int active_cursor;
26084 struct glyph_row *glyph_row;
26085 struct glyph *glyph;
26086
26087 /* This is pointless on invisible frames, and dangerous on garbaged
26088 windows and frames; in the latter case, the frame or window may
26089 be in the midst of changing its size, and x and y may be off the
26090 window. */
26091 if (! FRAME_VISIBLE_P (f)
26092 || FRAME_GARBAGED_P (f)
26093 || vpos >= w->current_matrix->nrows
26094 || hpos >= w->current_matrix->matrix_w)
26095 return;
26096
26097 /* If cursor is off and we want it off, return quickly. */
26098 if (!on && !w->phys_cursor_on_p)
26099 return;
26100
26101 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26102 /* If cursor row is not enabled, we don't really know where to
26103 display the cursor. */
26104 if (!glyph_row->enabled_p)
26105 {
26106 w->phys_cursor_on_p = 0;
26107 return;
26108 }
26109
26110 glyph = NULL;
26111 if (!glyph_row->exact_window_width_line_p
26112 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26113 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26114
26115 eassert (input_blocked_p ());
26116
26117 /* Set new_cursor_type to the cursor we want to be displayed. */
26118 new_cursor_type = get_window_cursor_type (w, glyph,
26119 &new_cursor_width, &active_cursor);
26120
26121 /* If cursor is currently being shown and we don't want it to be or
26122 it is in the wrong place, or the cursor type is not what we want,
26123 erase it. */
26124 if (w->phys_cursor_on_p
26125 && (!on
26126 || w->phys_cursor.x != x
26127 || w->phys_cursor.y != y
26128 || new_cursor_type != w->phys_cursor_type
26129 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26130 && new_cursor_width != w->phys_cursor_width)))
26131 erase_phys_cursor (w);
26132
26133 /* Don't check phys_cursor_on_p here because that flag is only set
26134 to zero in some cases where we know that the cursor has been
26135 completely erased, to avoid the extra work of erasing the cursor
26136 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26137 still not be visible, or it has only been partly erased. */
26138 if (on)
26139 {
26140 w->phys_cursor_ascent = glyph_row->ascent;
26141 w->phys_cursor_height = glyph_row->height;
26142
26143 /* Set phys_cursor_.* before x_draw_.* is called because some
26144 of them may need the information. */
26145 w->phys_cursor.x = x;
26146 w->phys_cursor.y = glyph_row->y;
26147 w->phys_cursor.hpos = hpos;
26148 w->phys_cursor.vpos = vpos;
26149 }
26150
26151 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26152 new_cursor_type, new_cursor_width,
26153 on, active_cursor);
26154 }
26155
26156
26157 /* Switch the display of W's cursor on or off, according to the value
26158 of ON. */
26159
26160 static void
26161 update_window_cursor (struct window *w, int on)
26162 {
26163 /* Don't update cursor in windows whose frame is in the process
26164 of being deleted. */
26165 if (w->current_matrix)
26166 {
26167 int hpos = w->phys_cursor.hpos;
26168 int vpos = w->phys_cursor.vpos;
26169 struct glyph_row *row;
26170
26171 if (vpos >= w->current_matrix->nrows
26172 || hpos >= w->current_matrix->matrix_w)
26173 return;
26174
26175 row = MATRIX_ROW (w->current_matrix, vpos);
26176
26177 /* When the window is hscrolled, cursor hpos can legitimately be
26178 out of bounds, but we draw the cursor at the corresponding
26179 window margin in that case. */
26180 if (!row->reversed_p && hpos < 0)
26181 hpos = 0;
26182 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26183 hpos = row->used[TEXT_AREA] - 1;
26184
26185 block_input ();
26186 display_and_set_cursor (w, on, hpos, vpos,
26187 w->phys_cursor.x, w->phys_cursor.y);
26188 unblock_input ();
26189 }
26190 }
26191
26192
26193 /* Call update_window_cursor with parameter ON_P on all leaf windows
26194 in the window tree rooted at W. */
26195
26196 static void
26197 update_cursor_in_window_tree (struct window *w, int on_p)
26198 {
26199 while (w)
26200 {
26201 if (!NILP (w->hchild))
26202 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26203 else if (!NILP (w->vchild))
26204 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26205 else
26206 update_window_cursor (w, on_p);
26207
26208 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26209 }
26210 }
26211
26212
26213 /* EXPORT:
26214 Display the cursor on window W, or clear it, according to ON_P.
26215 Don't change the cursor's position. */
26216
26217 void
26218 x_update_cursor (struct frame *f, int on_p)
26219 {
26220 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26221 }
26222
26223
26224 /* EXPORT:
26225 Clear the cursor of window W to background color, and mark the
26226 cursor as not shown. This is used when the text where the cursor
26227 is about to be rewritten. */
26228
26229 void
26230 x_clear_cursor (struct window *w)
26231 {
26232 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26233 update_window_cursor (w, 0);
26234 }
26235
26236 #endif /* HAVE_WINDOW_SYSTEM */
26237
26238 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26239 and MSDOS. */
26240 static void
26241 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26242 int start_hpos, int end_hpos,
26243 enum draw_glyphs_face draw)
26244 {
26245 #ifdef HAVE_WINDOW_SYSTEM
26246 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26247 {
26248 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26249 return;
26250 }
26251 #endif
26252 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26253 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26254 #endif
26255 }
26256
26257 /* Display the active region described by mouse_face_* according to DRAW. */
26258
26259 static void
26260 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26261 {
26262 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26263 struct frame *f = XFRAME (WINDOW_FRAME (w));
26264
26265 if (/* If window is in the process of being destroyed, don't bother
26266 to do anything. */
26267 w->current_matrix != NULL
26268 /* Don't update mouse highlight if hidden */
26269 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26270 /* Recognize when we are called to operate on rows that don't exist
26271 anymore. This can happen when a window is split. */
26272 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26273 {
26274 int phys_cursor_on_p = w->phys_cursor_on_p;
26275 struct glyph_row *row, *first, *last;
26276
26277 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26278 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26279
26280 for (row = first; row <= last && row->enabled_p; ++row)
26281 {
26282 int start_hpos, end_hpos, start_x;
26283
26284 /* For all but the first row, the highlight starts at column 0. */
26285 if (row == first)
26286 {
26287 /* R2L rows have BEG and END in reversed order, but the
26288 screen drawing geometry is always left to right. So
26289 we need to mirror the beginning and end of the
26290 highlighted area in R2L rows. */
26291 if (!row->reversed_p)
26292 {
26293 start_hpos = hlinfo->mouse_face_beg_col;
26294 start_x = hlinfo->mouse_face_beg_x;
26295 }
26296 else if (row == last)
26297 {
26298 start_hpos = hlinfo->mouse_face_end_col;
26299 start_x = hlinfo->mouse_face_end_x;
26300 }
26301 else
26302 {
26303 start_hpos = 0;
26304 start_x = 0;
26305 }
26306 }
26307 else if (row->reversed_p && row == last)
26308 {
26309 start_hpos = hlinfo->mouse_face_end_col;
26310 start_x = hlinfo->mouse_face_end_x;
26311 }
26312 else
26313 {
26314 start_hpos = 0;
26315 start_x = 0;
26316 }
26317
26318 if (row == last)
26319 {
26320 if (!row->reversed_p)
26321 end_hpos = hlinfo->mouse_face_end_col;
26322 else if (row == first)
26323 end_hpos = hlinfo->mouse_face_beg_col;
26324 else
26325 {
26326 end_hpos = row->used[TEXT_AREA];
26327 if (draw == DRAW_NORMAL_TEXT)
26328 row->fill_line_p = 1; /* Clear to end of line */
26329 }
26330 }
26331 else if (row->reversed_p && row == first)
26332 end_hpos = hlinfo->mouse_face_beg_col;
26333 else
26334 {
26335 end_hpos = row->used[TEXT_AREA];
26336 if (draw == DRAW_NORMAL_TEXT)
26337 row->fill_line_p = 1; /* Clear to end of line */
26338 }
26339
26340 if (end_hpos > start_hpos)
26341 {
26342 draw_row_with_mouse_face (w, start_x, row,
26343 start_hpos, end_hpos, draw);
26344
26345 row->mouse_face_p
26346 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26347 }
26348 }
26349
26350 #ifdef HAVE_WINDOW_SYSTEM
26351 /* When we've written over the cursor, arrange for it to
26352 be displayed again. */
26353 if (FRAME_WINDOW_P (f)
26354 && phys_cursor_on_p && !w->phys_cursor_on_p)
26355 {
26356 int hpos = w->phys_cursor.hpos;
26357
26358 /* When the window is hscrolled, cursor hpos can legitimately be
26359 out of bounds, but we draw the cursor at the corresponding
26360 window margin in that case. */
26361 if (!row->reversed_p && hpos < 0)
26362 hpos = 0;
26363 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26364 hpos = row->used[TEXT_AREA] - 1;
26365
26366 block_input ();
26367 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26368 w->phys_cursor.x, w->phys_cursor.y);
26369 unblock_input ();
26370 }
26371 #endif /* HAVE_WINDOW_SYSTEM */
26372 }
26373
26374 #ifdef HAVE_WINDOW_SYSTEM
26375 /* Change the mouse cursor. */
26376 if (FRAME_WINDOW_P (f))
26377 {
26378 if (draw == DRAW_NORMAL_TEXT
26379 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26380 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26381 else if (draw == DRAW_MOUSE_FACE)
26382 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26383 else
26384 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26385 }
26386 #endif /* HAVE_WINDOW_SYSTEM */
26387 }
26388
26389 /* EXPORT:
26390 Clear out the mouse-highlighted active region.
26391 Redraw it un-highlighted first. Value is non-zero if mouse
26392 face was actually drawn unhighlighted. */
26393
26394 int
26395 clear_mouse_face (Mouse_HLInfo *hlinfo)
26396 {
26397 int cleared = 0;
26398
26399 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26400 {
26401 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26402 cleared = 1;
26403 }
26404
26405 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26406 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26407 hlinfo->mouse_face_window = Qnil;
26408 hlinfo->mouse_face_overlay = Qnil;
26409 return cleared;
26410 }
26411
26412 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26413 within the mouse face on that window. */
26414 static int
26415 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26416 {
26417 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26418
26419 /* Quickly resolve the easy cases. */
26420 if (!(WINDOWP (hlinfo->mouse_face_window)
26421 && XWINDOW (hlinfo->mouse_face_window) == w))
26422 return 0;
26423 if (vpos < hlinfo->mouse_face_beg_row
26424 || vpos > hlinfo->mouse_face_end_row)
26425 return 0;
26426 if (vpos > hlinfo->mouse_face_beg_row
26427 && vpos < hlinfo->mouse_face_end_row)
26428 return 1;
26429
26430 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26431 {
26432 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26433 {
26434 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26435 return 1;
26436 }
26437 else if ((vpos == hlinfo->mouse_face_beg_row
26438 && hpos >= hlinfo->mouse_face_beg_col)
26439 || (vpos == hlinfo->mouse_face_end_row
26440 && hpos < hlinfo->mouse_face_end_col))
26441 return 1;
26442 }
26443 else
26444 {
26445 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26446 {
26447 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26448 return 1;
26449 }
26450 else if ((vpos == hlinfo->mouse_face_beg_row
26451 && hpos <= hlinfo->mouse_face_beg_col)
26452 || (vpos == hlinfo->mouse_face_end_row
26453 && hpos > hlinfo->mouse_face_end_col))
26454 return 1;
26455 }
26456 return 0;
26457 }
26458
26459
26460 /* EXPORT:
26461 Non-zero if physical cursor of window W is within mouse face. */
26462
26463 int
26464 cursor_in_mouse_face_p (struct window *w)
26465 {
26466 int hpos = w->phys_cursor.hpos;
26467 int vpos = w->phys_cursor.vpos;
26468 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26469
26470 /* When the window is hscrolled, cursor hpos can legitimately be out
26471 of bounds, but we draw the cursor at the corresponding window
26472 margin in that case. */
26473 if (!row->reversed_p && hpos < 0)
26474 hpos = 0;
26475 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26476 hpos = row->used[TEXT_AREA] - 1;
26477
26478 return coords_in_mouse_face_p (w, hpos, vpos);
26479 }
26480
26481
26482 \f
26483 /* Find the glyph rows START_ROW and END_ROW of window W that display
26484 characters between buffer positions START_CHARPOS and END_CHARPOS
26485 (excluding END_CHARPOS). DISP_STRING is a display string that
26486 covers these buffer positions. This is similar to
26487 row_containing_pos, but is more accurate when bidi reordering makes
26488 buffer positions change non-linearly with glyph rows. */
26489 static void
26490 rows_from_pos_range (struct window *w,
26491 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26492 Lisp_Object disp_string,
26493 struct glyph_row **start, struct glyph_row **end)
26494 {
26495 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26496 int last_y = window_text_bottom_y (w);
26497 struct glyph_row *row;
26498
26499 *start = NULL;
26500 *end = NULL;
26501
26502 while (!first->enabled_p
26503 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26504 first++;
26505
26506 /* Find the START row. */
26507 for (row = first;
26508 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26509 row++)
26510 {
26511 /* A row can potentially be the START row if the range of the
26512 characters it displays intersects the range
26513 [START_CHARPOS..END_CHARPOS). */
26514 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26515 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26516 /* See the commentary in row_containing_pos, for the
26517 explanation of the complicated way to check whether
26518 some position is beyond the end of the characters
26519 displayed by a row. */
26520 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26521 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26522 && !row->ends_at_zv_p
26523 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26524 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26525 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26526 && !row->ends_at_zv_p
26527 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26528 {
26529 /* Found a candidate row. Now make sure at least one of the
26530 glyphs it displays has a charpos from the range
26531 [START_CHARPOS..END_CHARPOS).
26532
26533 This is not obvious because bidi reordering could make
26534 buffer positions of a row be 1,2,3,102,101,100, and if we
26535 want to highlight characters in [50..60), we don't want
26536 this row, even though [50..60) does intersect [1..103),
26537 the range of character positions given by the row's start
26538 and end positions. */
26539 struct glyph *g = row->glyphs[TEXT_AREA];
26540 struct glyph *e = g + row->used[TEXT_AREA];
26541
26542 while (g < e)
26543 {
26544 if (((BUFFERP (g->object) || INTEGERP (g->object))
26545 && start_charpos <= g->charpos && g->charpos < end_charpos)
26546 /* A glyph that comes from DISP_STRING is by
26547 definition to be highlighted. */
26548 || EQ (g->object, disp_string))
26549 *start = row;
26550 g++;
26551 }
26552 if (*start)
26553 break;
26554 }
26555 }
26556
26557 /* Find the END row. */
26558 if (!*start
26559 /* If the last row is partially visible, start looking for END
26560 from that row, instead of starting from FIRST. */
26561 && !(row->enabled_p
26562 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26563 row = first;
26564 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26565 {
26566 struct glyph_row *next = row + 1;
26567 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26568
26569 if (!next->enabled_p
26570 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26571 /* The first row >= START whose range of displayed characters
26572 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26573 is the row END + 1. */
26574 || (start_charpos < next_start
26575 && end_charpos < next_start)
26576 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26577 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26578 && !next->ends_at_zv_p
26579 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26580 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26581 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26582 && !next->ends_at_zv_p
26583 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26584 {
26585 *end = row;
26586 break;
26587 }
26588 else
26589 {
26590 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26591 but none of the characters it displays are in the range, it is
26592 also END + 1. */
26593 struct glyph *g = next->glyphs[TEXT_AREA];
26594 struct glyph *s = g;
26595 struct glyph *e = g + next->used[TEXT_AREA];
26596
26597 while (g < e)
26598 {
26599 if (((BUFFERP (g->object) || INTEGERP (g->object))
26600 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26601 /* If the buffer position of the first glyph in
26602 the row is equal to END_CHARPOS, it means
26603 the last character to be highlighted is the
26604 newline of ROW, and we must consider NEXT as
26605 END, not END+1. */
26606 || (((!next->reversed_p && g == s)
26607 || (next->reversed_p && g == e - 1))
26608 && (g->charpos == end_charpos
26609 /* Special case for when NEXT is an
26610 empty line at ZV. */
26611 || (g->charpos == -1
26612 && !row->ends_at_zv_p
26613 && next_start == end_charpos)))))
26614 /* A glyph that comes from DISP_STRING is by
26615 definition to be highlighted. */
26616 || EQ (g->object, disp_string))
26617 break;
26618 g++;
26619 }
26620 if (g == e)
26621 {
26622 *end = row;
26623 break;
26624 }
26625 /* The first row that ends at ZV must be the last to be
26626 highlighted. */
26627 else if (next->ends_at_zv_p)
26628 {
26629 *end = next;
26630 break;
26631 }
26632 }
26633 }
26634 }
26635
26636 /* This function sets the mouse_face_* elements of HLINFO, assuming
26637 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26638 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26639 for the overlay or run of text properties specifying the mouse
26640 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26641 before-string and after-string that must also be highlighted.
26642 DISP_STRING, if non-nil, is a display string that may cover some
26643 or all of the highlighted text. */
26644
26645 static void
26646 mouse_face_from_buffer_pos (Lisp_Object window,
26647 Mouse_HLInfo *hlinfo,
26648 ptrdiff_t mouse_charpos,
26649 ptrdiff_t start_charpos,
26650 ptrdiff_t end_charpos,
26651 Lisp_Object before_string,
26652 Lisp_Object after_string,
26653 Lisp_Object disp_string)
26654 {
26655 struct window *w = XWINDOW (window);
26656 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26657 struct glyph_row *r1, *r2;
26658 struct glyph *glyph, *end;
26659 ptrdiff_t ignore, pos;
26660 int x;
26661
26662 eassert (NILP (disp_string) || STRINGP (disp_string));
26663 eassert (NILP (before_string) || STRINGP (before_string));
26664 eassert (NILP (after_string) || STRINGP (after_string));
26665
26666 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26667 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26668 if (r1 == NULL)
26669 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26670 /* If the before-string or display-string contains newlines,
26671 rows_from_pos_range skips to its last row. Move back. */
26672 if (!NILP (before_string) || !NILP (disp_string))
26673 {
26674 struct glyph_row *prev;
26675 while ((prev = r1 - 1, prev >= first)
26676 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26677 && prev->used[TEXT_AREA] > 0)
26678 {
26679 struct glyph *beg = prev->glyphs[TEXT_AREA];
26680 glyph = beg + prev->used[TEXT_AREA];
26681 while (--glyph >= beg && INTEGERP (glyph->object));
26682 if (glyph < beg
26683 || !(EQ (glyph->object, before_string)
26684 || EQ (glyph->object, disp_string)))
26685 break;
26686 r1 = prev;
26687 }
26688 }
26689 if (r2 == NULL)
26690 {
26691 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26692 hlinfo->mouse_face_past_end = 1;
26693 }
26694 else if (!NILP (after_string))
26695 {
26696 /* If the after-string has newlines, advance to its last row. */
26697 struct glyph_row *next;
26698 struct glyph_row *last
26699 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26700
26701 for (next = r2 + 1;
26702 next <= last
26703 && next->used[TEXT_AREA] > 0
26704 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26705 ++next)
26706 r2 = next;
26707 }
26708 /* The rest of the display engine assumes that mouse_face_beg_row is
26709 either above mouse_face_end_row or identical to it. But with
26710 bidi-reordered continued lines, the row for START_CHARPOS could
26711 be below the row for END_CHARPOS. If so, swap the rows and store
26712 them in correct order. */
26713 if (r1->y > r2->y)
26714 {
26715 struct glyph_row *tem = r2;
26716
26717 r2 = r1;
26718 r1 = tem;
26719 }
26720
26721 hlinfo->mouse_face_beg_y = r1->y;
26722 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26723 hlinfo->mouse_face_end_y = r2->y;
26724 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26725
26726 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26727 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26728 could be anywhere in the row and in any order. The strategy
26729 below is to find the leftmost and the rightmost glyph that
26730 belongs to either of these 3 strings, or whose position is
26731 between START_CHARPOS and END_CHARPOS, and highlight all the
26732 glyphs between those two. This may cover more than just the text
26733 between START_CHARPOS and END_CHARPOS if the range of characters
26734 strides the bidi level boundary, e.g. if the beginning is in R2L
26735 text while the end is in L2R text or vice versa. */
26736 if (!r1->reversed_p)
26737 {
26738 /* This row is in a left to right paragraph. Scan it left to
26739 right. */
26740 glyph = r1->glyphs[TEXT_AREA];
26741 end = glyph + r1->used[TEXT_AREA];
26742 x = r1->x;
26743
26744 /* Skip truncation glyphs at the start of the glyph row. */
26745 if (r1->displays_text_p)
26746 for (; glyph < end
26747 && INTEGERP (glyph->object)
26748 && glyph->charpos < 0;
26749 ++glyph)
26750 x += glyph->pixel_width;
26751
26752 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26753 or DISP_STRING, and the first glyph from buffer whose
26754 position is between START_CHARPOS and END_CHARPOS. */
26755 for (; glyph < end
26756 && !INTEGERP (glyph->object)
26757 && !EQ (glyph->object, disp_string)
26758 && !(BUFFERP (glyph->object)
26759 && (glyph->charpos >= start_charpos
26760 && glyph->charpos < end_charpos));
26761 ++glyph)
26762 {
26763 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26764 are present at buffer positions between START_CHARPOS and
26765 END_CHARPOS, or if they come from an overlay. */
26766 if (EQ (glyph->object, before_string))
26767 {
26768 pos = string_buffer_position (before_string,
26769 start_charpos);
26770 /* If pos == 0, it means before_string came from an
26771 overlay, not from a buffer position. */
26772 if (!pos || (pos >= start_charpos && pos < end_charpos))
26773 break;
26774 }
26775 else if (EQ (glyph->object, after_string))
26776 {
26777 pos = string_buffer_position (after_string, end_charpos);
26778 if (!pos || (pos >= start_charpos && pos < end_charpos))
26779 break;
26780 }
26781 x += glyph->pixel_width;
26782 }
26783 hlinfo->mouse_face_beg_x = x;
26784 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26785 }
26786 else
26787 {
26788 /* This row is in a right to left paragraph. Scan it right to
26789 left. */
26790 struct glyph *g;
26791
26792 end = r1->glyphs[TEXT_AREA] - 1;
26793 glyph = end + r1->used[TEXT_AREA];
26794
26795 /* Skip truncation glyphs at the start of the glyph row. */
26796 if (r1->displays_text_p)
26797 for (; glyph > end
26798 && INTEGERP (glyph->object)
26799 && glyph->charpos < 0;
26800 --glyph)
26801 ;
26802
26803 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26804 or DISP_STRING, and the first glyph from buffer whose
26805 position is between START_CHARPOS and END_CHARPOS. */
26806 for (; glyph > end
26807 && !INTEGERP (glyph->object)
26808 && !EQ (glyph->object, disp_string)
26809 && !(BUFFERP (glyph->object)
26810 && (glyph->charpos >= start_charpos
26811 && glyph->charpos < end_charpos));
26812 --glyph)
26813 {
26814 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26815 are present at buffer positions between START_CHARPOS and
26816 END_CHARPOS, or if they come from an overlay. */
26817 if (EQ (glyph->object, before_string))
26818 {
26819 pos = string_buffer_position (before_string, start_charpos);
26820 /* If pos == 0, it means before_string came from an
26821 overlay, not from a buffer position. */
26822 if (!pos || (pos >= start_charpos && pos < end_charpos))
26823 break;
26824 }
26825 else if (EQ (glyph->object, after_string))
26826 {
26827 pos = string_buffer_position (after_string, end_charpos);
26828 if (!pos || (pos >= start_charpos && pos < end_charpos))
26829 break;
26830 }
26831 }
26832
26833 glyph++; /* first glyph to the right of the highlighted area */
26834 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26835 x += g->pixel_width;
26836 hlinfo->mouse_face_beg_x = x;
26837 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26838 }
26839
26840 /* If the highlight ends in a different row, compute GLYPH and END
26841 for the end row. Otherwise, reuse the values computed above for
26842 the row where the highlight begins. */
26843 if (r2 != r1)
26844 {
26845 if (!r2->reversed_p)
26846 {
26847 glyph = r2->glyphs[TEXT_AREA];
26848 end = glyph + r2->used[TEXT_AREA];
26849 x = r2->x;
26850 }
26851 else
26852 {
26853 end = r2->glyphs[TEXT_AREA] - 1;
26854 glyph = end + r2->used[TEXT_AREA];
26855 }
26856 }
26857
26858 if (!r2->reversed_p)
26859 {
26860 /* Skip truncation and continuation glyphs near the end of the
26861 row, and also blanks and stretch glyphs inserted by
26862 extend_face_to_end_of_line. */
26863 while (end > glyph
26864 && INTEGERP ((end - 1)->object))
26865 --end;
26866 /* Scan the rest of the glyph row from the end, looking for the
26867 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26868 DISP_STRING, or whose position is between START_CHARPOS
26869 and END_CHARPOS */
26870 for (--end;
26871 end > glyph
26872 && !INTEGERP (end->object)
26873 && !EQ (end->object, disp_string)
26874 && !(BUFFERP (end->object)
26875 && (end->charpos >= start_charpos
26876 && end->charpos < end_charpos));
26877 --end)
26878 {
26879 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26880 are present at buffer positions between START_CHARPOS and
26881 END_CHARPOS, or if they come from an overlay. */
26882 if (EQ (end->object, before_string))
26883 {
26884 pos = string_buffer_position (before_string, start_charpos);
26885 if (!pos || (pos >= start_charpos && pos < end_charpos))
26886 break;
26887 }
26888 else if (EQ (end->object, after_string))
26889 {
26890 pos = string_buffer_position (after_string, end_charpos);
26891 if (!pos || (pos >= start_charpos && pos < end_charpos))
26892 break;
26893 }
26894 }
26895 /* Find the X coordinate of the last glyph to be highlighted. */
26896 for (; glyph <= end; ++glyph)
26897 x += glyph->pixel_width;
26898
26899 hlinfo->mouse_face_end_x = x;
26900 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26901 }
26902 else
26903 {
26904 /* Skip truncation and continuation glyphs near the end of the
26905 row, and also blanks and stretch glyphs inserted by
26906 extend_face_to_end_of_line. */
26907 x = r2->x;
26908 end++;
26909 while (end < glyph
26910 && INTEGERP (end->object))
26911 {
26912 x += end->pixel_width;
26913 ++end;
26914 }
26915 /* Scan the rest of the glyph row from the end, looking for the
26916 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26917 DISP_STRING, or whose position is between START_CHARPOS
26918 and END_CHARPOS */
26919 for ( ;
26920 end < glyph
26921 && !INTEGERP (end->object)
26922 && !EQ (end->object, disp_string)
26923 && !(BUFFERP (end->object)
26924 && (end->charpos >= start_charpos
26925 && end->charpos < end_charpos));
26926 ++end)
26927 {
26928 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26929 are present at buffer positions between START_CHARPOS and
26930 END_CHARPOS, or if they come from an overlay. */
26931 if (EQ (end->object, before_string))
26932 {
26933 pos = string_buffer_position (before_string, start_charpos);
26934 if (!pos || (pos >= start_charpos && pos < end_charpos))
26935 break;
26936 }
26937 else if (EQ (end->object, after_string))
26938 {
26939 pos = string_buffer_position (after_string, end_charpos);
26940 if (!pos || (pos >= start_charpos && pos < end_charpos))
26941 break;
26942 }
26943 x += end->pixel_width;
26944 }
26945 /* If we exited the above loop because we arrived at the last
26946 glyph of the row, and its buffer position is still not in
26947 range, it means the last character in range is the preceding
26948 newline. Bump the end column and x values to get past the
26949 last glyph. */
26950 if (end == glyph
26951 && BUFFERP (end->object)
26952 && (end->charpos < start_charpos
26953 || end->charpos >= end_charpos))
26954 {
26955 x += end->pixel_width;
26956 ++end;
26957 }
26958 hlinfo->mouse_face_end_x = x;
26959 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26960 }
26961
26962 hlinfo->mouse_face_window = window;
26963 hlinfo->mouse_face_face_id
26964 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26965 mouse_charpos + 1,
26966 !hlinfo->mouse_face_hidden, -1);
26967 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26968 }
26969
26970 /* The following function is not used anymore (replaced with
26971 mouse_face_from_string_pos), but I leave it here for the time
26972 being, in case someone would. */
26973
26974 #if 0 /* not used */
26975
26976 /* Find the position of the glyph for position POS in OBJECT in
26977 window W's current matrix, and return in *X, *Y the pixel
26978 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26979
26980 RIGHT_P non-zero means return the position of the right edge of the
26981 glyph, RIGHT_P zero means return the left edge position.
26982
26983 If no glyph for POS exists in the matrix, return the position of
26984 the glyph with the next smaller position that is in the matrix, if
26985 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26986 exists in the matrix, return the position of the glyph with the
26987 next larger position in OBJECT.
26988
26989 Value is non-zero if a glyph was found. */
26990
26991 static int
26992 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26993 int *hpos, int *vpos, int *x, int *y, int right_p)
26994 {
26995 int yb = window_text_bottom_y (w);
26996 struct glyph_row *r;
26997 struct glyph *best_glyph = NULL;
26998 struct glyph_row *best_row = NULL;
26999 int best_x = 0;
27000
27001 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27002 r->enabled_p && r->y < yb;
27003 ++r)
27004 {
27005 struct glyph *g = r->glyphs[TEXT_AREA];
27006 struct glyph *e = g + r->used[TEXT_AREA];
27007 int gx;
27008
27009 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27010 if (EQ (g->object, object))
27011 {
27012 if (g->charpos == pos)
27013 {
27014 best_glyph = g;
27015 best_x = gx;
27016 best_row = r;
27017 goto found;
27018 }
27019 else if (best_glyph == NULL
27020 || ((eabs (g->charpos - pos)
27021 < eabs (best_glyph->charpos - pos))
27022 && (right_p
27023 ? g->charpos < pos
27024 : g->charpos > pos)))
27025 {
27026 best_glyph = g;
27027 best_x = gx;
27028 best_row = r;
27029 }
27030 }
27031 }
27032
27033 found:
27034
27035 if (best_glyph)
27036 {
27037 *x = best_x;
27038 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27039
27040 if (right_p)
27041 {
27042 *x += best_glyph->pixel_width;
27043 ++*hpos;
27044 }
27045
27046 *y = best_row->y;
27047 *vpos = best_row - w->current_matrix->rows;
27048 }
27049
27050 return best_glyph != NULL;
27051 }
27052 #endif /* not used */
27053
27054 /* Find the positions of the first and the last glyphs in window W's
27055 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27056 (assumed to be a string), and return in HLINFO's mouse_face_*
27057 members the pixel and column/row coordinates of those glyphs. */
27058
27059 static void
27060 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27061 Lisp_Object object,
27062 ptrdiff_t startpos, ptrdiff_t endpos)
27063 {
27064 int yb = window_text_bottom_y (w);
27065 struct glyph_row *r;
27066 struct glyph *g, *e;
27067 int gx;
27068 int found = 0;
27069
27070 /* Find the glyph row with at least one position in the range
27071 [STARTPOS..ENDPOS], and the first glyph in that row whose
27072 position belongs to that range. */
27073 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27074 r->enabled_p && r->y < yb;
27075 ++r)
27076 {
27077 if (!r->reversed_p)
27078 {
27079 g = r->glyphs[TEXT_AREA];
27080 e = g + r->used[TEXT_AREA];
27081 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27082 if (EQ (g->object, object)
27083 && startpos <= g->charpos && g->charpos <= endpos)
27084 {
27085 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27086 hlinfo->mouse_face_beg_y = r->y;
27087 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27088 hlinfo->mouse_face_beg_x = gx;
27089 found = 1;
27090 break;
27091 }
27092 }
27093 else
27094 {
27095 struct glyph *g1;
27096
27097 e = r->glyphs[TEXT_AREA];
27098 g = e + r->used[TEXT_AREA];
27099 for ( ; g > e; --g)
27100 if (EQ ((g-1)->object, object)
27101 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27102 {
27103 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27104 hlinfo->mouse_face_beg_y = r->y;
27105 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27106 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27107 gx += g1->pixel_width;
27108 hlinfo->mouse_face_beg_x = gx;
27109 found = 1;
27110 break;
27111 }
27112 }
27113 if (found)
27114 break;
27115 }
27116
27117 if (!found)
27118 return;
27119
27120 /* Starting with the next row, look for the first row which does NOT
27121 include any glyphs whose positions are in the range. */
27122 for (++r; r->enabled_p && r->y < yb; ++r)
27123 {
27124 g = r->glyphs[TEXT_AREA];
27125 e = g + r->used[TEXT_AREA];
27126 found = 0;
27127 for ( ; g < e; ++g)
27128 if (EQ (g->object, object)
27129 && startpos <= g->charpos && g->charpos <= endpos)
27130 {
27131 found = 1;
27132 break;
27133 }
27134 if (!found)
27135 break;
27136 }
27137
27138 /* The highlighted region ends on the previous row. */
27139 r--;
27140
27141 /* Set the end row and its vertical pixel coordinate. */
27142 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27143 hlinfo->mouse_face_end_y = r->y;
27144
27145 /* Compute and set the end column and the end column's horizontal
27146 pixel coordinate. */
27147 if (!r->reversed_p)
27148 {
27149 g = r->glyphs[TEXT_AREA];
27150 e = g + r->used[TEXT_AREA];
27151 for ( ; e > g; --e)
27152 if (EQ ((e-1)->object, object)
27153 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27154 break;
27155 hlinfo->mouse_face_end_col = e - g;
27156
27157 for (gx = r->x; g < e; ++g)
27158 gx += g->pixel_width;
27159 hlinfo->mouse_face_end_x = gx;
27160 }
27161 else
27162 {
27163 e = r->glyphs[TEXT_AREA];
27164 g = e + r->used[TEXT_AREA];
27165 for (gx = r->x ; e < g; ++e)
27166 {
27167 if (EQ (e->object, object)
27168 && startpos <= e->charpos && e->charpos <= endpos)
27169 break;
27170 gx += e->pixel_width;
27171 }
27172 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27173 hlinfo->mouse_face_end_x = gx;
27174 }
27175 }
27176
27177 #ifdef HAVE_WINDOW_SYSTEM
27178
27179 /* See if position X, Y is within a hot-spot of an image. */
27180
27181 static int
27182 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27183 {
27184 if (!CONSP (hot_spot))
27185 return 0;
27186
27187 if (EQ (XCAR (hot_spot), Qrect))
27188 {
27189 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27190 Lisp_Object rect = XCDR (hot_spot);
27191 Lisp_Object tem;
27192 if (!CONSP (rect))
27193 return 0;
27194 if (!CONSP (XCAR (rect)))
27195 return 0;
27196 if (!CONSP (XCDR (rect)))
27197 return 0;
27198 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27199 return 0;
27200 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27201 return 0;
27202 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27203 return 0;
27204 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27205 return 0;
27206 return 1;
27207 }
27208 else if (EQ (XCAR (hot_spot), Qcircle))
27209 {
27210 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27211 Lisp_Object circ = XCDR (hot_spot);
27212 Lisp_Object lr, lx0, ly0;
27213 if (CONSP (circ)
27214 && CONSP (XCAR (circ))
27215 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27216 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27217 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27218 {
27219 double r = XFLOATINT (lr);
27220 double dx = XINT (lx0) - x;
27221 double dy = XINT (ly0) - y;
27222 return (dx * dx + dy * dy <= r * r);
27223 }
27224 }
27225 else if (EQ (XCAR (hot_spot), Qpoly))
27226 {
27227 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27228 if (VECTORP (XCDR (hot_spot)))
27229 {
27230 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27231 Lisp_Object *poly = v->contents;
27232 ptrdiff_t n = v->header.size;
27233 ptrdiff_t i;
27234 int inside = 0;
27235 Lisp_Object lx, ly;
27236 int x0, y0;
27237
27238 /* Need an even number of coordinates, and at least 3 edges. */
27239 if (n < 6 || n & 1)
27240 return 0;
27241
27242 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27243 If count is odd, we are inside polygon. Pixels on edges
27244 may or may not be included depending on actual geometry of the
27245 polygon. */
27246 if ((lx = poly[n-2], !INTEGERP (lx))
27247 || (ly = poly[n-1], !INTEGERP (lx)))
27248 return 0;
27249 x0 = XINT (lx), y0 = XINT (ly);
27250 for (i = 0; i < n; i += 2)
27251 {
27252 int x1 = x0, y1 = y0;
27253 if ((lx = poly[i], !INTEGERP (lx))
27254 || (ly = poly[i+1], !INTEGERP (ly)))
27255 return 0;
27256 x0 = XINT (lx), y0 = XINT (ly);
27257
27258 /* Does this segment cross the X line? */
27259 if (x0 >= x)
27260 {
27261 if (x1 >= x)
27262 continue;
27263 }
27264 else if (x1 < x)
27265 continue;
27266 if (y > y0 && y > y1)
27267 continue;
27268 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27269 inside = !inside;
27270 }
27271 return inside;
27272 }
27273 }
27274 return 0;
27275 }
27276
27277 Lisp_Object
27278 find_hot_spot (Lisp_Object map, int x, int y)
27279 {
27280 while (CONSP (map))
27281 {
27282 if (CONSP (XCAR (map))
27283 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27284 return XCAR (map);
27285 map = XCDR (map);
27286 }
27287
27288 return Qnil;
27289 }
27290
27291 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27292 3, 3, 0,
27293 doc: /* Lookup in image map MAP coordinates X and Y.
27294 An image map is an alist where each element has the format (AREA ID PLIST).
27295 An AREA is specified as either a rectangle, a circle, or a polygon:
27296 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27297 pixel coordinates of the upper left and bottom right corners.
27298 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27299 and the radius of the circle; r may be a float or integer.
27300 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27301 vector describes one corner in the polygon.
27302 Returns the alist element for the first matching AREA in MAP. */)
27303 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27304 {
27305 if (NILP (map))
27306 return Qnil;
27307
27308 CHECK_NUMBER (x);
27309 CHECK_NUMBER (y);
27310
27311 return find_hot_spot (map,
27312 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27313 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27314 }
27315
27316
27317 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27318 static void
27319 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27320 {
27321 /* Do not change cursor shape while dragging mouse. */
27322 if (!NILP (do_mouse_tracking))
27323 return;
27324
27325 if (!NILP (pointer))
27326 {
27327 if (EQ (pointer, Qarrow))
27328 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27329 else if (EQ (pointer, Qhand))
27330 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27331 else if (EQ (pointer, Qtext))
27332 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27333 else if (EQ (pointer, intern ("hdrag")))
27334 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27335 #ifdef HAVE_X_WINDOWS
27336 else if (EQ (pointer, intern ("vdrag")))
27337 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27338 #endif
27339 else if (EQ (pointer, intern ("hourglass")))
27340 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27341 else if (EQ (pointer, Qmodeline))
27342 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27343 else
27344 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27345 }
27346
27347 if (cursor != No_Cursor)
27348 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27349 }
27350
27351 #endif /* HAVE_WINDOW_SYSTEM */
27352
27353 /* Take proper action when mouse has moved to the mode or header line
27354 or marginal area AREA of window W, x-position X and y-position Y.
27355 X is relative to the start of the text display area of W, so the
27356 width of bitmap areas and scroll bars must be subtracted to get a
27357 position relative to the start of the mode line. */
27358
27359 static void
27360 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27361 enum window_part area)
27362 {
27363 struct window *w = XWINDOW (window);
27364 struct frame *f = XFRAME (w->frame);
27365 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27366 #ifdef HAVE_WINDOW_SYSTEM
27367 Display_Info *dpyinfo;
27368 #endif
27369 Cursor cursor = No_Cursor;
27370 Lisp_Object pointer = Qnil;
27371 int dx, dy, width, height;
27372 ptrdiff_t charpos;
27373 Lisp_Object string, object = Qnil;
27374 Lisp_Object pos IF_LINT (= Qnil), help;
27375
27376 Lisp_Object mouse_face;
27377 int original_x_pixel = x;
27378 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27379 struct glyph_row *row IF_LINT (= 0);
27380
27381 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27382 {
27383 int x0;
27384 struct glyph *end;
27385
27386 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27387 returns them in row/column units! */
27388 string = mode_line_string (w, area, &x, &y, &charpos,
27389 &object, &dx, &dy, &width, &height);
27390
27391 row = (area == ON_MODE_LINE
27392 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27393 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27394
27395 /* Find the glyph under the mouse pointer. */
27396 if (row->mode_line_p && row->enabled_p)
27397 {
27398 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27399 end = glyph + row->used[TEXT_AREA];
27400
27401 for (x0 = original_x_pixel;
27402 glyph < end && x0 >= glyph->pixel_width;
27403 ++glyph)
27404 x0 -= glyph->pixel_width;
27405
27406 if (glyph >= end)
27407 glyph = NULL;
27408 }
27409 }
27410 else
27411 {
27412 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27413 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27414 returns them in row/column units! */
27415 string = marginal_area_string (w, area, &x, &y, &charpos,
27416 &object, &dx, &dy, &width, &height);
27417 }
27418
27419 help = Qnil;
27420
27421 #ifdef HAVE_WINDOW_SYSTEM
27422 if (IMAGEP (object))
27423 {
27424 Lisp_Object image_map, hotspot;
27425 if ((image_map = Fplist_get (XCDR (object), QCmap),
27426 !NILP (image_map))
27427 && (hotspot = find_hot_spot (image_map, dx, dy),
27428 CONSP (hotspot))
27429 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27430 {
27431 Lisp_Object plist;
27432
27433 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27434 If so, we could look for mouse-enter, mouse-leave
27435 properties in PLIST (and do something...). */
27436 hotspot = XCDR (hotspot);
27437 if (CONSP (hotspot)
27438 && (plist = XCAR (hotspot), CONSP (plist)))
27439 {
27440 pointer = Fplist_get (plist, Qpointer);
27441 if (NILP (pointer))
27442 pointer = Qhand;
27443 help = Fplist_get (plist, Qhelp_echo);
27444 if (!NILP (help))
27445 {
27446 help_echo_string = help;
27447 XSETWINDOW (help_echo_window, w);
27448 help_echo_object = w->buffer;
27449 help_echo_pos = charpos;
27450 }
27451 }
27452 }
27453 if (NILP (pointer))
27454 pointer = Fplist_get (XCDR (object), QCpointer);
27455 }
27456 #endif /* HAVE_WINDOW_SYSTEM */
27457
27458 if (STRINGP (string))
27459 pos = make_number (charpos);
27460
27461 /* Set the help text and mouse pointer. If the mouse is on a part
27462 of the mode line without any text (e.g. past the right edge of
27463 the mode line text), use the default help text and pointer. */
27464 if (STRINGP (string) || area == ON_MODE_LINE)
27465 {
27466 /* Arrange to display the help by setting the global variables
27467 help_echo_string, help_echo_object, and help_echo_pos. */
27468 if (NILP (help))
27469 {
27470 if (STRINGP (string))
27471 help = Fget_text_property (pos, Qhelp_echo, string);
27472
27473 if (!NILP (help))
27474 {
27475 help_echo_string = help;
27476 XSETWINDOW (help_echo_window, w);
27477 help_echo_object = string;
27478 help_echo_pos = charpos;
27479 }
27480 else if (area == ON_MODE_LINE)
27481 {
27482 Lisp_Object default_help
27483 = buffer_local_value_1 (Qmode_line_default_help_echo,
27484 w->buffer);
27485
27486 if (STRINGP (default_help))
27487 {
27488 help_echo_string = default_help;
27489 XSETWINDOW (help_echo_window, w);
27490 help_echo_object = Qnil;
27491 help_echo_pos = -1;
27492 }
27493 }
27494 }
27495
27496 #ifdef HAVE_WINDOW_SYSTEM
27497 /* Change the mouse pointer according to what is under it. */
27498 if (FRAME_WINDOW_P (f))
27499 {
27500 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27501 if (STRINGP (string))
27502 {
27503 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27504
27505 if (NILP (pointer))
27506 pointer = Fget_text_property (pos, Qpointer, string);
27507
27508 /* Change the mouse pointer according to what is under X/Y. */
27509 if (NILP (pointer)
27510 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27511 {
27512 Lisp_Object map;
27513 map = Fget_text_property (pos, Qlocal_map, string);
27514 if (!KEYMAPP (map))
27515 map = Fget_text_property (pos, Qkeymap, string);
27516 if (!KEYMAPP (map))
27517 cursor = dpyinfo->vertical_scroll_bar_cursor;
27518 }
27519 }
27520 else
27521 /* Default mode-line pointer. */
27522 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27523 }
27524 #endif
27525 }
27526
27527 /* Change the mouse face according to what is under X/Y. */
27528 if (STRINGP (string))
27529 {
27530 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27531 if (!NILP (mouse_face)
27532 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27533 && glyph)
27534 {
27535 Lisp_Object b, e;
27536
27537 struct glyph * tmp_glyph;
27538
27539 int gpos;
27540 int gseq_length;
27541 int total_pixel_width;
27542 ptrdiff_t begpos, endpos, ignore;
27543
27544 int vpos, hpos;
27545
27546 b = Fprevious_single_property_change (make_number (charpos + 1),
27547 Qmouse_face, string, Qnil);
27548 if (NILP (b))
27549 begpos = 0;
27550 else
27551 begpos = XINT (b);
27552
27553 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27554 if (NILP (e))
27555 endpos = SCHARS (string);
27556 else
27557 endpos = XINT (e);
27558
27559 /* Calculate the glyph position GPOS of GLYPH in the
27560 displayed string, relative to the beginning of the
27561 highlighted part of the string.
27562
27563 Note: GPOS is different from CHARPOS. CHARPOS is the
27564 position of GLYPH in the internal string object. A mode
27565 line string format has structures which are converted to
27566 a flattened string by the Emacs Lisp interpreter. The
27567 internal string is an element of those structures. The
27568 displayed string is the flattened string. */
27569 tmp_glyph = row_start_glyph;
27570 while (tmp_glyph < glyph
27571 && (!(EQ (tmp_glyph->object, glyph->object)
27572 && begpos <= tmp_glyph->charpos
27573 && tmp_glyph->charpos < endpos)))
27574 tmp_glyph++;
27575 gpos = glyph - tmp_glyph;
27576
27577 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27578 the highlighted part of the displayed string to which
27579 GLYPH belongs. Note: GSEQ_LENGTH is different from
27580 SCHARS (STRING), because the latter returns the length of
27581 the internal string. */
27582 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27583 tmp_glyph > glyph
27584 && (!(EQ (tmp_glyph->object, glyph->object)
27585 && begpos <= tmp_glyph->charpos
27586 && tmp_glyph->charpos < endpos));
27587 tmp_glyph--)
27588 ;
27589 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27590
27591 /* Calculate the total pixel width of all the glyphs between
27592 the beginning of the highlighted area and GLYPH. */
27593 total_pixel_width = 0;
27594 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27595 total_pixel_width += tmp_glyph->pixel_width;
27596
27597 /* Pre calculation of re-rendering position. Note: X is in
27598 column units here, after the call to mode_line_string or
27599 marginal_area_string. */
27600 hpos = x - gpos;
27601 vpos = (area == ON_MODE_LINE
27602 ? (w->current_matrix)->nrows - 1
27603 : 0);
27604
27605 /* If GLYPH's position is included in the region that is
27606 already drawn in mouse face, we have nothing to do. */
27607 if ( EQ (window, hlinfo->mouse_face_window)
27608 && (!row->reversed_p
27609 ? (hlinfo->mouse_face_beg_col <= hpos
27610 && hpos < hlinfo->mouse_face_end_col)
27611 /* In R2L rows we swap BEG and END, see below. */
27612 : (hlinfo->mouse_face_end_col <= hpos
27613 && hpos < hlinfo->mouse_face_beg_col))
27614 && hlinfo->mouse_face_beg_row == vpos )
27615 return;
27616
27617 if (clear_mouse_face (hlinfo))
27618 cursor = No_Cursor;
27619
27620 if (!row->reversed_p)
27621 {
27622 hlinfo->mouse_face_beg_col = hpos;
27623 hlinfo->mouse_face_beg_x = original_x_pixel
27624 - (total_pixel_width + dx);
27625 hlinfo->mouse_face_end_col = hpos + gseq_length;
27626 hlinfo->mouse_face_end_x = 0;
27627 }
27628 else
27629 {
27630 /* In R2L rows, show_mouse_face expects BEG and END
27631 coordinates to be swapped. */
27632 hlinfo->mouse_face_end_col = hpos;
27633 hlinfo->mouse_face_end_x = original_x_pixel
27634 - (total_pixel_width + dx);
27635 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27636 hlinfo->mouse_face_beg_x = 0;
27637 }
27638
27639 hlinfo->mouse_face_beg_row = vpos;
27640 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27641 hlinfo->mouse_face_beg_y = 0;
27642 hlinfo->mouse_face_end_y = 0;
27643 hlinfo->mouse_face_past_end = 0;
27644 hlinfo->mouse_face_window = window;
27645
27646 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27647 charpos,
27648 0, 0, 0,
27649 &ignore,
27650 glyph->face_id,
27651 1);
27652 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27653
27654 if (NILP (pointer))
27655 pointer = Qhand;
27656 }
27657 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27658 clear_mouse_face (hlinfo);
27659 }
27660 #ifdef HAVE_WINDOW_SYSTEM
27661 if (FRAME_WINDOW_P (f))
27662 define_frame_cursor1 (f, cursor, pointer);
27663 #endif
27664 }
27665
27666
27667 /* EXPORT:
27668 Take proper action when the mouse has moved to position X, Y on
27669 frame F as regards highlighting characters that have mouse-face
27670 properties. Also de-highlighting chars where the mouse was before.
27671 X and Y can be negative or out of range. */
27672
27673 void
27674 note_mouse_highlight (struct frame *f, int x, int y)
27675 {
27676 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27677 enum window_part part = ON_NOTHING;
27678 Lisp_Object window;
27679 struct window *w;
27680 Cursor cursor = No_Cursor;
27681 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27682 struct buffer *b;
27683
27684 /* When a menu is active, don't highlight because this looks odd. */
27685 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27686 if (popup_activated ())
27687 return;
27688 #endif
27689
27690 if (NILP (Vmouse_highlight)
27691 || !f->glyphs_initialized_p
27692 || f->pointer_invisible)
27693 return;
27694
27695 hlinfo->mouse_face_mouse_x = x;
27696 hlinfo->mouse_face_mouse_y = y;
27697 hlinfo->mouse_face_mouse_frame = f;
27698
27699 if (hlinfo->mouse_face_defer)
27700 return;
27701
27702 if (gc_in_progress)
27703 {
27704 hlinfo->mouse_face_deferred_gc = 1;
27705 return;
27706 }
27707
27708 /* Which window is that in? */
27709 window = window_from_coordinates (f, x, y, &part, 1);
27710
27711 /* If displaying active text in another window, clear that. */
27712 if (! EQ (window, hlinfo->mouse_face_window)
27713 /* Also clear if we move out of text area in same window. */
27714 || (!NILP (hlinfo->mouse_face_window)
27715 && !NILP (window)
27716 && part != ON_TEXT
27717 && part != ON_MODE_LINE
27718 && part != ON_HEADER_LINE))
27719 clear_mouse_face (hlinfo);
27720
27721 /* Not on a window -> return. */
27722 if (!WINDOWP (window))
27723 return;
27724
27725 /* Reset help_echo_string. It will get recomputed below. */
27726 help_echo_string = Qnil;
27727
27728 /* Convert to window-relative pixel coordinates. */
27729 w = XWINDOW (window);
27730 frame_to_window_pixel_xy (w, &x, &y);
27731
27732 #ifdef HAVE_WINDOW_SYSTEM
27733 /* Handle tool-bar window differently since it doesn't display a
27734 buffer. */
27735 if (EQ (window, f->tool_bar_window))
27736 {
27737 note_tool_bar_highlight (f, x, y);
27738 return;
27739 }
27740 #endif
27741
27742 /* Mouse is on the mode, header line or margin? */
27743 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27744 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27745 {
27746 note_mode_line_or_margin_highlight (window, x, y, part);
27747 return;
27748 }
27749
27750 #ifdef HAVE_WINDOW_SYSTEM
27751 if (part == ON_VERTICAL_BORDER)
27752 {
27753 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27754 help_echo_string = build_string ("drag-mouse-1: resize");
27755 }
27756 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27757 || part == ON_SCROLL_BAR)
27758 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27759 else
27760 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27761 #endif
27762
27763 /* Are we in a window whose display is up to date?
27764 And verify the buffer's text has not changed. */
27765 b = XBUFFER (w->buffer);
27766 if (part == ON_TEXT
27767 && EQ (w->window_end_valid, w->buffer)
27768 && w->last_modified == BUF_MODIFF (b)
27769 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27770 {
27771 int hpos, vpos, dx, dy, area = LAST_AREA;
27772 ptrdiff_t pos;
27773 struct glyph *glyph;
27774 Lisp_Object object;
27775 Lisp_Object mouse_face = Qnil, position;
27776 Lisp_Object *overlay_vec = NULL;
27777 ptrdiff_t i, noverlays;
27778 struct buffer *obuf;
27779 ptrdiff_t obegv, ozv;
27780 int same_region;
27781
27782 /* Find the glyph under X/Y. */
27783 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27784
27785 #ifdef HAVE_WINDOW_SYSTEM
27786 /* Look for :pointer property on image. */
27787 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27788 {
27789 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27790 if (img != NULL && IMAGEP (img->spec))
27791 {
27792 Lisp_Object image_map, hotspot;
27793 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27794 !NILP (image_map))
27795 && (hotspot = find_hot_spot (image_map,
27796 glyph->slice.img.x + dx,
27797 glyph->slice.img.y + dy),
27798 CONSP (hotspot))
27799 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27800 {
27801 Lisp_Object plist;
27802
27803 /* Could check XCAR (hotspot) to see if we enter/leave
27804 this hot-spot.
27805 If so, we could look for mouse-enter, mouse-leave
27806 properties in PLIST (and do something...). */
27807 hotspot = XCDR (hotspot);
27808 if (CONSP (hotspot)
27809 && (plist = XCAR (hotspot), CONSP (plist)))
27810 {
27811 pointer = Fplist_get (plist, Qpointer);
27812 if (NILP (pointer))
27813 pointer = Qhand;
27814 help_echo_string = Fplist_get (plist, Qhelp_echo);
27815 if (!NILP (help_echo_string))
27816 {
27817 help_echo_window = window;
27818 help_echo_object = glyph->object;
27819 help_echo_pos = glyph->charpos;
27820 }
27821 }
27822 }
27823 if (NILP (pointer))
27824 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27825 }
27826 }
27827 #endif /* HAVE_WINDOW_SYSTEM */
27828
27829 /* Clear mouse face if X/Y not over text. */
27830 if (glyph == NULL
27831 || area != TEXT_AREA
27832 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27833 /* Glyph's OBJECT is an integer for glyphs inserted by the
27834 display engine for its internal purposes, like truncation
27835 and continuation glyphs and blanks beyond the end of
27836 line's text on text terminals. If we are over such a
27837 glyph, we are not over any text. */
27838 || INTEGERP (glyph->object)
27839 /* R2L rows have a stretch glyph at their front, which
27840 stands for no text, whereas L2R rows have no glyphs at
27841 all beyond the end of text. Treat such stretch glyphs
27842 like we do with NULL glyphs in L2R rows. */
27843 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27844 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27845 && glyph->type == STRETCH_GLYPH
27846 && glyph->avoid_cursor_p))
27847 {
27848 if (clear_mouse_face (hlinfo))
27849 cursor = No_Cursor;
27850 #ifdef HAVE_WINDOW_SYSTEM
27851 if (FRAME_WINDOW_P (f) && NILP (pointer))
27852 {
27853 if (area != TEXT_AREA)
27854 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27855 else
27856 pointer = Vvoid_text_area_pointer;
27857 }
27858 #endif
27859 goto set_cursor;
27860 }
27861
27862 pos = glyph->charpos;
27863 object = glyph->object;
27864 if (!STRINGP (object) && !BUFFERP (object))
27865 goto set_cursor;
27866
27867 /* If we get an out-of-range value, return now; avoid an error. */
27868 if (BUFFERP (object) && pos > BUF_Z (b))
27869 goto set_cursor;
27870
27871 /* Make the window's buffer temporarily current for
27872 overlays_at and compute_char_face. */
27873 obuf = current_buffer;
27874 current_buffer = b;
27875 obegv = BEGV;
27876 ozv = ZV;
27877 BEGV = BEG;
27878 ZV = Z;
27879
27880 /* Is this char mouse-active or does it have help-echo? */
27881 position = make_number (pos);
27882
27883 if (BUFFERP (object))
27884 {
27885 /* Put all the overlays we want in a vector in overlay_vec. */
27886 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27887 /* Sort overlays into increasing priority order. */
27888 noverlays = sort_overlays (overlay_vec, noverlays, w);
27889 }
27890 else
27891 noverlays = 0;
27892
27893 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27894
27895 if (same_region)
27896 cursor = No_Cursor;
27897
27898 /* Check mouse-face highlighting. */
27899 if (! same_region
27900 /* If there exists an overlay with mouse-face overlapping
27901 the one we are currently highlighting, we have to
27902 check if we enter the overlapping overlay, and then
27903 highlight only that. */
27904 || (OVERLAYP (hlinfo->mouse_face_overlay)
27905 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27906 {
27907 /* Find the highest priority overlay with a mouse-face. */
27908 Lisp_Object overlay = Qnil;
27909 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27910 {
27911 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27912 if (!NILP (mouse_face))
27913 overlay = overlay_vec[i];
27914 }
27915
27916 /* If we're highlighting the same overlay as before, there's
27917 no need to do that again. */
27918 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27919 goto check_help_echo;
27920 hlinfo->mouse_face_overlay = overlay;
27921
27922 /* Clear the display of the old active region, if any. */
27923 if (clear_mouse_face (hlinfo))
27924 cursor = No_Cursor;
27925
27926 /* If no overlay applies, get a text property. */
27927 if (NILP (overlay))
27928 mouse_face = Fget_text_property (position, Qmouse_face, object);
27929
27930 /* Next, compute the bounds of the mouse highlighting and
27931 display it. */
27932 if (!NILP (mouse_face) && STRINGP (object))
27933 {
27934 /* The mouse-highlighting comes from a display string
27935 with a mouse-face. */
27936 Lisp_Object s, e;
27937 ptrdiff_t ignore;
27938
27939 s = Fprevious_single_property_change
27940 (make_number (pos + 1), Qmouse_face, object, Qnil);
27941 e = Fnext_single_property_change
27942 (position, Qmouse_face, object, Qnil);
27943 if (NILP (s))
27944 s = make_number (0);
27945 if (NILP (e))
27946 e = make_number (SCHARS (object) - 1);
27947 mouse_face_from_string_pos (w, hlinfo, object,
27948 XINT (s), XINT (e));
27949 hlinfo->mouse_face_past_end = 0;
27950 hlinfo->mouse_face_window = window;
27951 hlinfo->mouse_face_face_id
27952 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27953 glyph->face_id, 1);
27954 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27955 cursor = No_Cursor;
27956 }
27957 else
27958 {
27959 /* The mouse-highlighting, if any, comes from an overlay
27960 or text property in the buffer. */
27961 Lisp_Object buffer IF_LINT (= Qnil);
27962 Lisp_Object disp_string IF_LINT (= Qnil);
27963
27964 if (STRINGP (object))
27965 {
27966 /* If we are on a display string with no mouse-face,
27967 check if the text under it has one. */
27968 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27969 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27970 pos = string_buffer_position (object, start);
27971 if (pos > 0)
27972 {
27973 mouse_face = get_char_property_and_overlay
27974 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27975 buffer = w->buffer;
27976 disp_string = object;
27977 }
27978 }
27979 else
27980 {
27981 buffer = object;
27982 disp_string = Qnil;
27983 }
27984
27985 if (!NILP (mouse_face))
27986 {
27987 Lisp_Object before, after;
27988 Lisp_Object before_string, after_string;
27989 /* To correctly find the limits of mouse highlight
27990 in a bidi-reordered buffer, we must not use the
27991 optimization of limiting the search in
27992 previous-single-property-change and
27993 next-single-property-change, because
27994 rows_from_pos_range needs the real start and end
27995 positions to DTRT in this case. That's because
27996 the first row visible in a window does not
27997 necessarily display the character whose position
27998 is the smallest. */
27999 Lisp_Object lim1 =
28000 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28001 ? Fmarker_position (w->start)
28002 : Qnil;
28003 Lisp_Object lim2 =
28004 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28005 ? make_number (BUF_Z (XBUFFER (buffer))
28006 - XFASTINT (w->window_end_pos))
28007 : Qnil;
28008
28009 if (NILP (overlay))
28010 {
28011 /* Handle the text property case. */
28012 before = Fprevious_single_property_change
28013 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28014 after = Fnext_single_property_change
28015 (make_number (pos), Qmouse_face, buffer, lim2);
28016 before_string = after_string = Qnil;
28017 }
28018 else
28019 {
28020 /* Handle the overlay case. */
28021 before = Foverlay_start (overlay);
28022 after = Foverlay_end (overlay);
28023 before_string = Foverlay_get (overlay, Qbefore_string);
28024 after_string = Foverlay_get (overlay, Qafter_string);
28025
28026 if (!STRINGP (before_string)) before_string = Qnil;
28027 if (!STRINGP (after_string)) after_string = Qnil;
28028 }
28029
28030 mouse_face_from_buffer_pos (window, hlinfo, pos,
28031 NILP (before)
28032 ? 1
28033 : XFASTINT (before),
28034 NILP (after)
28035 ? BUF_Z (XBUFFER (buffer))
28036 : XFASTINT (after),
28037 before_string, after_string,
28038 disp_string);
28039 cursor = No_Cursor;
28040 }
28041 }
28042 }
28043
28044 check_help_echo:
28045
28046 /* Look for a `help-echo' property. */
28047 if (NILP (help_echo_string)) {
28048 Lisp_Object help, overlay;
28049
28050 /* Check overlays first. */
28051 help = overlay = Qnil;
28052 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28053 {
28054 overlay = overlay_vec[i];
28055 help = Foverlay_get (overlay, Qhelp_echo);
28056 }
28057
28058 if (!NILP (help))
28059 {
28060 help_echo_string = help;
28061 help_echo_window = window;
28062 help_echo_object = overlay;
28063 help_echo_pos = pos;
28064 }
28065 else
28066 {
28067 Lisp_Object obj = glyph->object;
28068 ptrdiff_t charpos = glyph->charpos;
28069
28070 /* Try text properties. */
28071 if (STRINGP (obj)
28072 && charpos >= 0
28073 && charpos < SCHARS (obj))
28074 {
28075 help = Fget_text_property (make_number (charpos),
28076 Qhelp_echo, obj);
28077 if (NILP (help))
28078 {
28079 /* If the string itself doesn't specify a help-echo,
28080 see if the buffer text ``under'' it does. */
28081 struct glyph_row *r
28082 = MATRIX_ROW (w->current_matrix, vpos);
28083 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28084 ptrdiff_t p = string_buffer_position (obj, start);
28085 if (p > 0)
28086 {
28087 help = Fget_char_property (make_number (p),
28088 Qhelp_echo, w->buffer);
28089 if (!NILP (help))
28090 {
28091 charpos = p;
28092 obj = w->buffer;
28093 }
28094 }
28095 }
28096 }
28097 else if (BUFFERP (obj)
28098 && charpos >= BEGV
28099 && charpos < ZV)
28100 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28101 obj);
28102
28103 if (!NILP (help))
28104 {
28105 help_echo_string = help;
28106 help_echo_window = window;
28107 help_echo_object = obj;
28108 help_echo_pos = charpos;
28109 }
28110 }
28111 }
28112
28113 #ifdef HAVE_WINDOW_SYSTEM
28114 /* Look for a `pointer' property. */
28115 if (FRAME_WINDOW_P (f) && NILP (pointer))
28116 {
28117 /* Check overlays first. */
28118 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28119 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28120
28121 if (NILP (pointer))
28122 {
28123 Lisp_Object obj = glyph->object;
28124 ptrdiff_t charpos = glyph->charpos;
28125
28126 /* Try text properties. */
28127 if (STRINGP (obj)
28128 && charpos >= 0
28129 && charpos < SCHARS (obj))
28130 {
28131 pointer = Fget_text_property (make_number (charpos),
28132 Qpointer, obj);
28133 if (NILP (pointer))
28134 {
28135 /* If the string itself doesn't specify a pointer,
28136 see if the buffer text ``under'' it does. */
28137 struct glyph_row *r
28138 = MATRIX_ROW (w->current_matrix, vpos);
28139 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28140 ptrdiff_t p = string_buffer_position (obj, start);
28141 if (p > 0)
28142 pointer = Fget_char_property (make_number (p),
28143 Qpointer, w->buffer);
28144 }
28145 }
28146 else if (BUFFERP (obj)
28147 && charpos >= BEGV
28148 && charpos < ZV)
28149 pointer = Fget_text_property (make_number (charpos),
28150 Qpointer, obj);
28151 }
28152 }
28153 #endif /* HAVE_WINDOW_SYSTEM */
28154
28155 BEGV = obegv;
28156 ZV = ozv;
28157 current_buffer = obuf;
28158 }
28159
28160 set_cursor:
28161
28162 #ifdef HAVE_WINDOW_SYSTEM
28163 if (FRAME_WINDOW_P (f))
28164 define_frame_cursor1 (f, cursor, pointer);
28165 #else
28166 /* This is here to prevent a compiler error, about "label at end of
28167 compound statement". */
28168 return;
28169 #endif
28170 }
28171
28172
28173 /* EXPORT for RIF:
28174 Clear any mouse-face on window W. This function is part of the
28175 redisplay interface, and is called from try_window_id and similar
28176 functions to ensure the mouse-highlight is off. */
28177
28178 void
28179 x_clear_window_mouse_face (struct window *w)
28180 {
28181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28182 Lisp_Object window;
28183
28184 block_input ();
28185 XSETWINDOW (window, w);
28186 if (EQ (window, hlinfo->mouse_face_window))
28187 clear_mouse_face (hlinfo);
28188 unblock_input ();
28189 }
28190
28191
28192 /* EXPORT:
28193 Just discard the mouse face information for frame F, if any.
28194 This is used when the size of F is changed. */
28195
28196 void
28197 cancel_mouse_face (struct frame *f)
28198 {
28199 Lisp_Object window;
28200 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28201
28202 window = hlinfo->mouse_face_window;
28203 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28204 {
28205 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28206 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28207 hlinfo->mouse_face_window = Qnil;
28208 }
28209 }
28210
28211
28212 \f
28213 /***********************************************************************
28214 Exposure Events
28215 ***********************************************************************/
28216
28217 #ifdef HAVE_WINDOW_SYSTEM
28218
28219 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28220 which intersects rectangle R. R is in window-relative coordinates. */
28221
28222 static void
28223 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28224 enum glyph_row_area area)
28225 {
28226 struct glyph *first = row->glyphs[area];
28227 struct glyph *end = row->glyphs[area] + row->used[area];
28228 struct glyph *last;
28229 int first_x, start_x, x;
28230
28231 if (area == TEXT_AREA && row->fill_line_p)
28232 /* If row extends face to end of line write the whole line. */
28233 draw_glyphs (w, 0, row, area,
28234 0, row->used[area],
28235 DRAW_NORMAL_TEXT, 0);
28236 else
28237 {
28238 /* Set START_X to the window-relative start position for drawing glyphs of
28239 AREA. The first glyph of the text area can be partially visible.
28240 The first glyphs of other areas cannot. */
28241 start_x = window_box_left_offset (w, area);
28242 x = start_x;
28243 if (area == TEXT_AREA)
28244 x += row->x;
28245
28246 /* Find the first glyph that must be redrawn. */
28247 while (first < end
28248 && x + first->pixel_width < r->x)
28249 {
28250 x += first->pixel_width;
28251 ++first;
28252 }
28253
28254 /* Find the last one. */
28255 last = first;
28256 first_x = x;
28257 while (last < end
28258 && x < r->x + r->width)
28259 {
28260 x += last->pixel_width;
28261 ++last;
28262 }
28263
28264 /* Repaint. */
28265 if (last > first)
28266 draw_glyphs (w, first_x - start_x, row, area,
28267 first - row->glyphs[area], last - row->glyphs[area],
28268 DRAW_NORMAL_TEXT, 0);
28269 }
28270 }
28271
28272
28273 /* Redraw the parts of the glyph row ROW on window W intersecting
28274 rectangle R. R is in window-relative coordinates. Value is
28275 non-zero if mouse-face was overwritten. */
28276
28277 static int
28278 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28279 {
28280 eassert (row->enabled_p);
28281
28282 if (row->mode_line_p || w->pseudo_window_p)
28283 draw_glyphs (w, 0, row, TEXT_AREA,
28284 0, row->used[TEXT_AREA],
28285 DRAW_NORMAL_TEXT, 0);
28286 else
28287 {
28288 if (row->used[LEFT_MARGIN_AREA])
28289 expose_area (w, row, r, LEFT_MARGIN_AREA);
28290 if (row->used[TEXT_AREA])
28291 expose_area (w, row, r, TEXT_AREA);
28292 if (row->used[RIGHT_MARGIN_AREA])
28293 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28294 draw_row_fringe_bitmaps (w, row);
28295 }
28296
28297 return row->mouse_face_p;
28298 }
28299
28300
28301 /* Redraw those parts of glyphs rows during expose event handling that
28302 overlap other rows. Redrawing of an exposed line writes over parts
28303 of lines overlapping that exposed line; this function fixes that.
28304
28305 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28306 row in W's current matrix that is exposed and overlaps other rows.
28307 LAST_OVERLAPPING_ROW is the last such row. */
28308
28309 static void
28310 expose_overlaps (struct window *w,
28311 struct glyph_row *first_overlapping_row,
28312 struct glyph_row *last_overlapping_row,
28313 XRectangle *r)
28314 {
28315 struct glyph_row *row;
28316
28317 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28318 if (row->overlapping_p)
28319 {
28320 eassert (row->enabled_p && !row->mode_line_p);
28321
28322 row->clip = r;
28323 if (row->used[LEFT_MARGIN_AREA])
28324 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28325
28326 if (row->used[TEXT_AREA])
28327 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28328
28329 if (row->used[RIGHT_MARGIN_AREA])
28330 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28331 row->clip = NULL;
28332 }
28333 }
28334
28335
28336 /* Return non-zero if W's cursor intersects rectangle R. */
28337
28338 static int
28339 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28340 {
28341 XRectangle cr, result;
28342 struct glyph *cursor_glyph;
28343 struct glyph_row *row;
28344
28345 if (w->phys_cursor.vpos >= 0
28346 && w->phys_cursor.vpos < w->current_matrix->nrows
28347 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28348 row->enabled_p)
28349 && row->cursor_in_fringe_p)
28350 {
28351 /* Cursor is in the fringe. */
28352 cr.x = window_box_right_offset (w,
28353 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28354 ? RIGHT_MARGIN_AREA
28355 : TEXT_AREA));
28356 cr.y = row->y;
28357 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28358 cr.height = row->height;
28359 return x_intersect_rectangles (&cr, r, &result);
28360 }
28361
28362 cursor_glyph = get_phys_cursor_glyph (w);
28363 if (cursor_glyph)
28364 {
28365 /* r is relative to W's box, but w->phys_cursor.x is relative
28366 to left edge of W's TEXT area. Adjust it. */
28367 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28368 cr.y = w->phys_cursor.y;
28369 cr.width = cursor_glyph->pixel_width;
28370 cr.height = w->phys_cursor_height;
28371 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28372 I assume the effect is the same -- and this is portable. */
28373 return x_intersect_rectangles (&cr, r, &result);
28374 }
28375 /* If we don't understand the format, pretend we're not in the hot-spot. */
28376 return 0;
28377 }
28378
28379
28380 /* EXPORT:
28381 Draw a vertical window border to the right of window W if W doesn't
28382 have vertical scroll bars. */
28383
28384 void
28385 x_draw_vertical_border (struct window *w)
28386 {
28387 struct frame *f = XFRAME (WINDOW_FRAME (w));
28388
28389 /* We could do better, if we knew what type of scroll-bar the adjacent
28390 windows (on either side) have... But we don't :-(
28391 However, I think this works ok. ++KFS 2003-04-25 */
28392
28393 /* Redraw borders between horizontally adjacent windows. Don't
28394 do it for frames with vertical scroll bars because either the
28395 right scroll bar of a window, or the left scroll bar of its
28396 neighbor will suffice as a border. */
28397 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28398 return;
28399
28400 if (!WINDOW_RIGHTMOST_P (w)
28401 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28402 {
28403 int x0, x1, y0, y1;
28404
28405 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28406 y1 -= 1;
28407
28408 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28409 x1 -= 1;
28410
28411 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28412 }
28413 else if (!WINDOW_LEFTMOST_P (w)
28414 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28415 {
28416 int x0, x1, y0, y1;
28417
28418 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28419 y1 -= 1;
28420
28421 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28422 x0 -= 1;
28423
28424 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28425 }
28426 }
28427
28428
28429 /* Redraw the part of window W intersection rectangle FR. Pixel
28430 coordinates in FR are frame-relative. Call this function with
28431 input blocked. Value is non-zero if the exposure overwrites
28432 mouse-face. */
28433
28434 static int
28435 expose_window (struct window *w, XRectangle *fr)
28436 {
28437 struct frame *f = XFRAME (w->frame);
28438 XRectangle wr, r;
28439 int mouse_face_overwritten_p = 0;
28440
28441 /* If window is not yet fully initialized, do nothing. This can
28442 happen when toolkit scroll bars are used and a window is split.
28443 Reconfiguring the scroll bar will generate an expose for a newly
28444 created window. */
28445 if (w->current_matrix == NULL)
28446 return 0;
28447
28448 /* When we're currently updating the window, display and current
28449 matrix usually don't agree. Arrange for a thorough display
28450 later. */
28451 if (w == updated_window)
28452 {
28453 SET_FRAME_GARBAGED (f);
28454 return 0;
28455 }
28456
28457 /* Frame-relative pixel rectangle of W. */
28458 wr.x = WINDOW_LEFT_EDGE_X (w);
28459 wr.y = WINDOW_TOP_EDGE_Y (w);
28460 wr.width = WINDOW_TOTAL_WIDTH (w);
28461 wr.height = WINDOW_TOTAL_HEIGHT (w);
28462
28463 if (x_intersect_rectangles (fr, &wr, &r))
28464 {
28465 int yb = window_text_bottom_y (w);
28466 struct glyph_row *row;
28467 int cursor_cleared_p, phys_cursor_on_p;
28468 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28469
28470 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28471 r.x, r.y, r.width, r.height));
28472
28473 /* Convert to window coordinates. */
28474 r.x -= WINDOW_LEFT_EDGE_X (w);
28475 r.y -= WINDOW_TOP_EDGE_Y (w);
28476
28477 /* Turn off the cursor. */
28478 if (!w->pseudo_window_p
28479 && phys_cursor_in_rect_p (w, &r))
28480 {
28481 x_clear_cursor (w);
28482 cursor_cleared_p = 1;
28483 }
28484 else
28485 cursor_cleared_p = 0;
28486
28487 /* If the row containing the cursor extends face to end of line,
28488 then expose_area might overwrite the cursor outside the
28489 rectangle and thus notice_overwritten_cursor might clear
28490 w->phys_cursor_on_p. We remember the original value and
28491 check later if it is changed. */
28492 phys_cursor_on_p = w->phys_cursor_on_p;
28493
28494 /* Update lines intersecting rectangle R. */
28495 first_overlapping_row = last_overlapping_row = NULL;
28496 for (row = w->current_matrix->rows;
28497 row->enabled_p;
28498 ++row)
28499 {
28500 int y0 = row->y;
28501 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28502
28503 if ((y0 >= r.y && y0 < r.y + r.height)
28504 || (y1 > r.y && y1 < r.y + r.height)
28505 || (r.y >= y0 && r.y < y1)
28506 || (r.y + r.height > y0 && r.y + r.height < y1))
28507 {
28508 /* A header line may be overlapping, but there is no need
28509 to fix overlapping areas for them. KFS 2005-02-12 */
28510 if (row->overlapping_p && !row->mode_line_p)
28511 {
28512 if (first_overlapping_row == NULL)
28513 first_overlapping_row = row;
28514 last_overlapping_row = row;
28515 }
28516
28517 row->clip = fr;
28518 if (expose_line (w, row, &r))
28519 mouse_face_overwritten_p = 1;
28520 row->clip = NULL;
28521 }
28522 else if (row->overlapping_p)
28523 {
28524 /* We must redraw a row overlapping the exposed area. */
28525 if (y0 < r.y
28526 ? y0 + row->phys_height > r.y
28527 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28528 {
28529 if (first_overlapping_row == NULL)
28530 first_overlapping_row = row;
28531 last_overlapping_row = row;
28532 }
28533 }
28534
28535 if (y1 >= yb)
28536 break;
28537 }
28538
28539 /* Display the mode line if there is one. */
28540 if (WINDOW_WANTS_MODELINE_P (w)
28541 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28542 row->enabled_p)
28543 && row->y < r.y + r.height)
28544 {
28545 if (expose_line (w, row, &r))
28546 mouse_face_overwritten_p = 1;
28547 }
28548
28549 if (!w->pseudo_window_p)
28550 {
28551 /* Fix the display of overlapping rows. */
28552 if (first_overlapping_row)
28553 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28554 fr);
28555
28556 /* Draw border between windows. */
28557 x_draw_vertical_border (w);
28558
28559 /* Turn the cursor on again. */
28560 if (cursor_cleared_p
28561 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28562 update_window_cursor (w, 1);
28563 }
28564 }
28565
28566 return mouse_face_overwritten_p;
28567 }
28568
28569
28570
28571 /* Redraw (parts) of all windows in the window tree rooted at W that
28572 intersect R. R contains frame pixel coordinates. Value is
28573 non-zero if the exposure overwrites mouse-face. */
28574
28575 static int
28576 expose_window_tree (struct window *w, XRectangle *r)
28577 {
28578 struct frame *f = XFRAME (w->frame);
28579 int mouse_face_overwritten_p = 0;
28580
28581 while (w && !FRAME_GARBAGED_P (f))
28582 {
28583 if (!NILP (w->hchild))
28584 mouse_face_overwritten_p
28585 |= expose_window_tree (XWINDOW (w->hchild), r);
28586 else if (!NILP (w->vchild))
28587 mouse_face_overwritten_p
28588 |= expose_window_tree (XWINDOW (w->vchild), r);
28589 else
28590 mouse_face_overwritten_p |= expose_window (w, r);
28591
28592 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28593 }
28594
28595 return mouse_face_overwritten_p;
28596 }
28597
28598
28599 /* EXPORT:
28600 Redisplay an exposed area of frame F. X and Y are the upper-left
28601 corner of the exposed rectangle. W and H are width and height of
28602 the exposed area. All are pixel values. W or H zero means redraw
28603 the entire frame. */
28604
28605 void
28606 expose_frame (struct frame *f, int x, int y, int w, int h)
28607 {
28608 XRectangle r;
28609 int mouse_face_overwritten_p = 0;
28610
28611 TRACE ((stderr, "expose_frame "));
28612
28613 /* No need to redraw if frame will be redrawn soon. */
28614 if (FRAME_GARBAGED_P (f))
28615 {
28616 TRACE ((stderr, " garbaged\n"));
28617 return;
28618 }
28619
28620 /* If basic faces haven't been realized yet, there is no point in
28621 trying to redraw anything. This can happen when we get an expose
28622 event while Emacs is starting, e.g. by moving another window. */
28623 if (FRAME_FACE_CACHE (f) == NULL
28624 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28625 {
28626 TRACE ((stderr, " no faces\n"));
28627 return;
28628 }
28629
28630 if (w == 0 || h == 0)
28631 {
28632 r.x = r.y = 0;
28633 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28634 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28635 }
28636 else
28637 {
28638 r.x = x;
28639 r.y = y;
28640 r.width = w;
28641 r.height = h;
28642 }
28643
28644 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28645 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28646
28647 if (WINDOWP (f->tool_bar_window))
28648 mouse_face_overwritten_p
28649 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28650
28651 #ifdef HAVE_X_WINDOWS
28652 #ifndef MSDOS
28653 #ifndef USE_X_TOOLKIT
28654 if (WINDOWP (f->menu_bar_window))
28655 mouse_face_overwritten_p
28656 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28657 #endif /* not USE_X_TOOLKIT */
28658 #endif
28659 #endif
28660
28661 /* Some window managers support a focus-follows-mouse style with
28662 delayed raising of frames. Imagine a partially obscured frame,
28663 and moving the mouse into partially obscured mouse-face on that
28664 frame. The visible part of the mouse-face will be highlighted,
28665 then the WM raises the obscured frame. With at least one WM, KDE
28666 2.1, Emacs is not getting any event for the raising of the frame
28667 (even tried with SubstructureRedirectMask), only Expose events.
28668 These expose events will draw text normally, i.e. not
28669 highlighted. Which means we must redo the highlight here.
28670 Subsume it under ``we love X''. --gerd 2001-08-15 */
28671 /* Included in Windows version because Windows most likely does not
28672 do the right thing if any third party tool offers
28673 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28674 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28675 {
28676 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28677 if (f == hlinfo->mouse_face_mouse_frame)
28678 {
28679 int mouse_x = hlinfo->mouse_face_mouse_x;
28680 int mouse_y = hlinfo->mouse_face_mouse_y;
28681 clear_mouse_face (hlinfo);
28682 note_mouse_highlight (f, mouse_x, mouse_y);
28683 }
28684 }
28685 }
28686
28687
28688 /* EXPORT:
28689 Determine the intersection of two rectangles R1 and R2. Return
28690 the intersection in *RESULT. Value is non-zero if RESULT is not
28691 empty. */
28692
28693 int
28694 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28695 {
28696 XRectangle *left, *right;
28697 XRectangle *upper, *lower;
28698 int intersection_p = 0;
28699
28700 /* Rearrange so that R1 is the left-most rectangle. */
28701 if (r1->x < r2->x)
28702 left = r1, right = r2;
28703 else
28704 left = r2, right = r1;
28705
28706 /* X0 of the intersection is right.x0, if this is inside R1,
28707 otherwise there is no intersection. */
28708 if (right->x <= left->x + left->width)
28709 {
28710 result->x = right->x;
28711
28712 /* The right end of the intersection is the minimum of
28713 the right ends of left and right. */
28714 result->width = (min (left->x + left->width, right->x + right->width)
28715 - result->x);
28716
28717 /* Same game for Y. */
28718 if (r1->y < r2->y)
28719 upper = r1, lower = r2;
28720 else
28721 upper = r2, lower = r1;
28722
28723 /* The upper end of the intersection is lower.y0, if this is inside
28724 of upper. Otherwise, there is no intersection. */
28725 if (lower->y <= upper->y + upper->height)
28726 {
28727 result->y = lower->y;
28728
28729 /* The lower end of the intersection is the minimum of the lower
28730 ends of upper and lower. */
28731 result->height = (min (lower->y + lower->height,
28732 upper->y + upper->height)
28733 - result->y);
28734 intersection_p = 1;
28735 }
28736 }
28737
28738 return intersection_p;
28739 }
28740
28741 #endif /* HAVE_WINDOW_SYSTEM */
28742
28743 \f
28744 /***********************************************************************
28745 Initialization
28746 ***********************************************************************/
28747
28748 void
28749 syms_of_xdisp (void)
28750 {
28751 Vwith_echo_area_save_vector = Qnil;
28752 staticpro (&Vwith_echo_area_save_vector);
28753
28754 Vmessage_stack = Qnil;
28755 staticpro (&Vmessage_stack);
28756
28757 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28758 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28759
28760 message_dolog_marker1 = Fmake_marker ();
28761 staticpro (&message_dolog_marker1);
28762 message_dolog_marker2 = Fmake_marker ();
28763 staticpro (&message_dolog_marker2);
28764 message_dolog_marker3 = Fmake_marker ();
28765 staticpro (&message_dolog_marker3);
28766
28767 #ifdef GLYPH_DEBUG
28768 defsubr (&Sdump_frame_glyph_matrix);
28769 defsubr (&Sdump_glyph_matrix);
28770 defsubr (&Sdump_glyph_row);
28771 defsubr (&Sdump_tool_bar_row);
28772 defsubr (&Strace_redisplay);
28773 defsubr (&Strace_to_stderr);
28774 #endif
28775 #ifdef HAVE_WINDOW_SYSTEM
28776 defsubr (&Stool_bar_lines_needed);
28777 defsubr (&Slookup_image_map);
28778 #endif
28779 defsubr (&Sformat_mode_line);
28780 defsubr (&Sinvisible_p);
28781 defsubr (&Scurrent_bidi_paragraph_direction);
28782
28783 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28784 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28785 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28786 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28787 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28788 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28789 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28790 DEFSYM (Qeval, "eval");
28791 DEFSYM (QCdata, ":data");
28792 DEFSYM (Qdisplay, "display");
28793 DEFSYM (Qspace_width, "space-width");
28794 DEFSYM (Qraise, "raise");
28795 DEFSYM (Qslice, "slice");
28796 DEFSYM (Qspace, "space");
28797 DEFSYM (Qmargin, "margin");
28798 DEFSYM (Qpointer, "pointer");
28799 DEFSYM (Qleft_margin, "left-margin");
28800 DEFSYM (Qright_margin, "right-margin");
28801 DEFSYM (Qcenter, "center");
28802 DEFSYM (Qline_height, "line-height");
28803 DEFSYM (QCalign_to, ":align-to");
28804 DEFSYM (QCrelative_width, ":relative-width");
28805 DEFSYM (QCrelative_height, ":relative-height");
28806 DEFSYM (QCeval, ":eval");
28807 DEFSYM (QCpropertize, ":propertize");
28808 DEFSYM (QCfile, ":file");
28809 DEFSYM (Qfontified, "fontified");
28810 DEFSYM (Qfontification_functions, "fontification-functions");
28811 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28812 DEFSYM (Qescape_glyph, "escape-glyph");
28813 DEFSYM (Qnobreak_space, "nobreak-space");
28814 DEFSYM (Qimage, "image");
28815 DEFSYM (Qtext, "text");
28816 DEFSYM (Qboth, "both");
28817 DEFSYM (Qboth_horiz, "both-horiz");
28818 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28819 DEFSYM (QCmap, ":map");
28820 DEFSYM (QCpointer, ":pointer");
28821 DEFSYM (Qrect, "rect");
28822 DEFSYM (Qcircle, "circle");
28823 DEFSYM (Qpoly, "poly");
28824 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28825 DEFSYM (Qgrow_only, "grow-only");
28826 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28827 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28828 DEFSYM (Qposition, "position");
28829 DEFSYM (Qbuffer_position, "buffer-position");
28830 DEFSYM (Qobject, "object");
28831 DEFSYM (Qbar, "bar");
28832 DEFSYM (Qhbar, "hbar");
28833 DEFSYM (Qbox, "box");
28834 DEFSYM (Qhollow, "hollow");
28835 DEFSYM (Qhand, "hand");
28836 DEFSYM (Qarrow, "arrow");
28837 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28838
28839 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28840 Fcons (intern_c_string ("void-variable"), Qnil)),
28841 Qnil);
28842 staticpro (&list_of_error);
28843
28844 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28845 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28846 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28847 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28848
28849 echo_buffer[0] = echo_buffer[1] = Qnil;
28850 staticpro (&echo_buffer[0]);
28851 staticpro (&echo_buffer[1]);
28852
28853 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28854 staticpro (&echo_area_buffer[0]);
28855 staticpro (&echo_area_buffer[1]);
28856
28857 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28858 staticpro (&Vmessages_buffer_name);
28859
28860 mode_line_proptrans_alist = Qnil;
28861 staticpro (&mode_line_proptrans_alist);
28862 mode_line_string_list = Qnil;
28863 staticpro (&mode_line_string_list);
28864 mode_line_string_face = Qnil;
28865 staticpro (&mode_line_string_face);
28866 mode_line_string_face_prop = Qnil;
28867 staticpro (&mode_line_string_face_prop);
28868 Vmode_line_unwind_vector = Qnil;
28869 staticpro (&Vmode_line_unwind_vector);
28870
28871 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28872
28873 help_echo_string = Qnil;
28874 staticpro (&help_echo_string);
28875 help_echo_object = Qnil;
28876 staticpro (&help_echo_object);
28877 help_echo_window = Qnil;
28878 staticpro (&help_echo_window);
28879 previous_help_echo_string = Qnil;
28880 staticpro (&previous_help_echo_string);
28881 help_echo_pos = -1;
28882
28883 DEFSYM (Qright_to_left, "right-to-left");
28884 DEFSYM (Qleft_to_right, "left-to-right");
28885
28886 #ifdef HAVE_WINDOW_SYSTEM
28887 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28888 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28889 For example, if a block cursor is over a tab, it will be drawn as
28890 wide as that tab on the display. */);
28891 x_stretch_cursor_p = 0;
28892 #endif
28893
28894 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28895 doc: /* Non-nil means highlight trailing whitespace.
28896 The face used for trailing whitespace is `trailing-whitespace'. */);
28897 Vshow_trailing_whitespace = Qnil;
28898
28899 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28900 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28901 If the value is t, Emacs highlights non-ASCII chars which have the
28902 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28903 or `escape-glyph' face respectively.
28904
28905 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28906 U+2011 (non-breaking hyphen) are affected.
28907
28908 Any other non-nil value means to display these characters as a escape
28909 glyph followed by an ordinary space or hyphen.
28910
28911 A value of nil means no special handling of these characters. */);
28912 Vnobreak_char_display = Qt;
28913
28914 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28915 doc: /* The pointer shape to show in void text areas.
28916 A value of nil means to show the text pointer. Other options are `arrow',
28917 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28918 Vvoid_text_area_pointer = Qarrow;
28919
28920 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28921 doc: /* Non-nil means don't actually do any redisplay.
28922 This is used for internal purposes. */);
28923 Vinhibit_redisplay = Qnil;
28924
28925 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28926 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28927 Vglobal_mode_string = Qnil;
28928
28929 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28930 doc: /* Marker for where to display an arrow on top of the buffer text.
28931 This must be the beginning of a line in order to work.
28932 See also `overlay-arrow-string'. */);
28933 Voverlay_arrow_position = Qnil;
28934
28935 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28936 doc: /* String to display as an arrow in non-window frames.
28937 See also `overlay-arrow-position'. */);
28938 Voverlay_arrow_string = build_pure_c_string ("=>");
28939
28940 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28941 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28942 The symbols on this list are examined during redisplay to determine
28943 where to display overlay arrows. */);
28944 Voverlay_arrow_variable_list
28945 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28946
28947 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28948 doc: /* The number of lines to try scrolling a window by when point moves out.
28949 If that fails to bring point back on frame, point is centered instead.
28950 If this is zero, point is always centered after it moves off frame.
28951 If you want scrolling to always be a line at a time, you should set
28952 `scroll-conservatively' to a large value rather than set this to 1. */);
28953
28954 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28955 doc: /* Scroll up to this many lines, to bring point back on screen.
28956 If point moves off-screen, redisplay will scroll by up to
28957 `scroll-conservatively' lines in order to bring point just barely
28958 onto the screen again. If that cannot be done, then redisplay
28959 recenters point as usual.
28960
28961 If the value is greater than 100, redisplay will never recenter point,
28962 but will always scroll just enough text to bring point into view, even
28963 if you move far away.
28964
28965 A value of zero means always recenter point if it moves off screen. */);
28966 scroll_conservatively = 0;
28967
28968 DEFVAR_INT ("scroll-margin", scroll_margin,
28969 doc: /* Number of lines of margin at the top and bottom of a window.
28970 Recenter the window whenever point gets within this many lines
28971 of the top or bottom of the window. */);
28972 scroll_margin = 0;
28973
28974 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28975 doc: /* Pixels per inch value for non-window system displays.
28976 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28977 Vdisplay_pixels_per_inch = make_float (72.0);
28978
28979 #ifdef GLYPH_DEBUG
28980 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28981 #endif
28982
28983 DEFVAR_LISP ("truncate-partial-width-windows",
28984 Vtruncate_partial_width_windows,
28985 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28986 For an integer value, truncate lines in each window narrower than the
28987 full frame width, provided the window width is less than that integer;
28988 otherwise, respect the value of `truncate-lines'.
28989
28990 For any other non-nil value, truncate lines in all windows that do
28991 not span the full frame width.
28992
28993 A value of nil means to respect the value of `truncate-lines'.
28994
28995 If `word-wrap' is enabled, you might want to reduce this. */);
28996 Vtruncate_partial_width_windows = make_number (50);
28997
28998 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28999 doc: /* Maximum buffer size for which line number should be displayed.
29000 If the buffer is bigger than this, the line number does not appear
29001 in the mode line. A value of nil means no limit. */);
29002 Vline_number_display_limit = Qnil;
29003
29004 DEFVAR_INT ("line-number-display-limit-width",
29005 line_number_display_limit_width,
29006 doc: /* Maximum line width (in characters) for line number display.
29007 If the average length of the lines near point is bigger than this, then the
29008 line number may be omitted from the mode line. */);
29009 line_number_display_limit_width = 200;
29010
29011 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29012 doc: /* Non-nil means highlight region even in nonselected windows. */);
29013 highlight_nonselected_windows = 0;
29014
29015 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29016 doc: /* Non-nil if more than one frame is visible on this display.
29017 Minibuffer-only frames don't count, but iconified frames do.
29018 This variable is not guaranteed to be accurate except while processing
29019 `frame-title-format' and `icon-title-format'. */);
29020
29021 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29022 doc: /* Template for displaying the title bar of visible frames.
29023 \(Assuming the window manager supports this feature.)
29024
29025 This variable has the same structure as `mode-line-format', except that
29026 the %c and %l constructs are ignored. It is used only on frames for
29027 which no explicit name has been set \(see `modify-frame-parameters'). */);
29028
29029 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29030 doc: /* Template for displaying the title bar of an iconified frame.
29031 \(Assuming the window manager supports this feature.)
29032 This variable has the same structure as `mode-line-format' (which see),
29033 and is used only on frames for which no explicit name has been set
29034 \(see `modify-frame-parameters'). */);
29035 Vicon_title_format
29036 = Vframe_title_format
29037 = listn (CONSTYPE_PURE, 3,
29038 intern_c_string ("multiple-frames"),
29039 build_pure_c_string ("%b"),
29040 listn (CONSTYPE_PURE, 4,
29041 empty_unibyte_string,
29042 intern_c_string ("invocation-name"),
29043 build_pure_c_string ("@"),
29044 intern_c_string ("system-name")));
29045
29046 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29047 doc: /* Maximum number of lines to keep in the message log buffer.
29048 If nil, disable message logging. If t, log messages but don't truncate
29049 the buffer when it becomes large. */);
29050 Vmessage_log_max = make_number (1000);
29051
29052 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29053 doc: /* Functions called before redisplay, if window sizes have changed.
29054 The value should be a list of functions that take one argument.
29055 Just before redisplay, for each frame, if any of its windows have changed
29056 size since the last redisplay, or have been split or deleted,
29057 all the functions in the list are called, with the frame as argument. */);
29058 Vwindow_size_change_functions = Qnil;
29059
29060 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29061 doc: /* List of functions to call before redisplaying a window with scrolling.
29062 Each function is called with two arguments, the window and its new
29063 display-start position. Note that these functions are also called by
29064 `set-window-buffer'. Also note that the value of `window-end' is not
29065 valid when these functions are called.
29066
29067 Warning: Do not use this feature to alter the way the window
29068 is scrolled. It is not designed for that, and such use probably won't
29069 work. */);
29070 Vwindow_scroll_functions = Qnil;
29071
29072 DEFVAR_LISP ("window-text-change-functions",
29073 Vwindow_text_change_functions,
29074 doc: /* Functions to call in redisplay when text in the window might change. */);
29075 Vwindow_text_change_functions = Qnil;
29076
29077 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29078 doc: /* Functions called when redisplay of a window reaches the end trigger.
29079 Each function is called with two arguments, the window and the end trigger value.
29080 See `set-window-redisplay-end-trigger'. */);
29081 Vredisplay_end_trigger_functions = Qnil;
29082
29083 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29084 doc: /* Non-nil means autoselect window with mouse pointer.
29085 If nil, do not autoselect windows.
29086 A positive number means delay autoselection by that many seconds: a
29087 window is autoselected only after the mouse has remained in that
29088 window for the duration of the delay.
29089 A negative number has a similar effect, but causes windows to be
29090 autoselected only after the mouse has stopped moving. \(Because of
29091 the way Emacs compares mouse events, you will occasionally wait twice
29092 that time before the window gets selected.\)
29093 Any other value means to autoselect window instantaneously when the
29094 mouse pointer enters it.
29095
29096 Autoselection selects the minibuffer only if it is active, and never
29097 unselects the minibuffer if it is active.
29098
29099 When customizing this variable make sure that the actual value of
29100 `focus-follows-mouse' matches the behavior of your window manager. */);
29101 Vmouse_autoselect_window = Qnil;
29102
29103 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29104 doc: /* Non-nil means automatically resize tool-bars.
29105 This dynamically changes the tool-bar's height to the minimum height
29106 that is needed to make all tool-bar items visible.
29107 If value is `grow-only', the tool-bar's height is only increased
29108 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29109 Vauto_resize_tool_bars = Qt;
29110
29111 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29112 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29113 auto_raise_tool_bar_buttons_p = 1;
29114
29115 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29116 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29117 make_cursor_line_fully_visible_p = 1;
29118
29119 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29120 doc: /* Border below tool-bar in pixels.
29121 If an integer, use it as the height of the border.
29122 If it is one of `internal-border-width' or `border-width', use the
29123 value of the corresponding frame parameter.
29124 Otherwise, no border is added below the tool-bar. */);
29125 Vtool_bar_border = Qinternal_border_width;
29126
29127 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29128 doc: /* Margin around tool-bar buttons in pixels.
29129 If an integer, use that for both horizontal and vertical margins.
29130 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29131 HORZ specifying the horizontal margin, and VERT specifying the
29132 vertical margin. */);
29133 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29134
29135 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29136 doc: /* Relief thickness of tool-bar buttons. */);
29137 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29138
29139 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29140 doc: /* Tool bar style to use.
29141 It can be one of
29142 image - show images only
29143 text - show text only
29144 both - show both, text below image
29145 both-horiz - show text to the right of the image
29146 text-image-horiz - show text to the left of the image
29147 any other - use system default or image if no system default.
29148
29149 This variable only affects the GTK+ toolkit version of Emacs. */);
29150 Vtool_bar_style = Qnil;
29151
29152 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29153 doc: /* Maximum number of characters a label can have to be shown.
29154 The tool bar style must also show labels for this to have any effect, see
29155 `tool-bar-style'. */);
29156 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29157
29158 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29159 doc: /* List of functions to call to fontify regions of text.
29160 Each function is called with one argument POS. Functions must
29161 fontify a region starting at POS in the current buffer, and give
29162 fontified regions the property `fontified'. */);
29163 Vfontification_functions = Qnil;
29164 Fmake_variable_buffer_local (Qfontification_functions);
29165
29166 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29167 unibyte_display_via_language_environment,
29168 doc: /* Non-nil means display unibyte text according to language environment.
29169 Specifically, this means that raw bytes in the range 160-255 decimal
29170 are displayed by converting them to the equivalent multibyte characters
29171 according to the current language environment. As a result, they are
29172 displayed according to the current fontset.
29173
29174 Note that this variable affects only how these bytes are displayed,
29175 but does not change the fact they are interpreted as raw bytes. */);
29176 unibyte_display_via_language_environment = 0;
29177
29178 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29179 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29180 If a float, it specifies a fraction of the mini-window frame's height.
29181 If an integer, it specifies a number of lines. */);
29182 Vmax_mini_window_height = make_float (0.25);
29183
29184 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29185 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29186 A value of nil means don't automatically resize mini-windows.
29187 A value of t means resize them to fit the text displayed in them.
29188 A value of `grow-only', the default, means let mini-windows grow only;
29189 they return to their normal size when the minibuffer is closed, or the
29190 echo area becomes empty. */);
29191 Vresize_mini_windows = Qgrow_only;
29192
29193 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29194 doc: /* Alist specifying how to blink the cursor off.
29195 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29196 `cursor-type' frame-parameter or variable equals ON-STATE,
29197 comparing using `equal', Emacs uses OFF-STATE to specify
29198 how to blink it off. ON-STATE and OFF-STATE are values for
29199 the `cursor-type' frame parameter.
29200
29201 If a frame's ON-STATE has no entry in this list,
29202 the frame's other specifications determine how to blink the cursor off. */);
29203 Vblink_cursor_alist = Qnil;
29204
29205 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29206 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29207 If non-nil, windows are automatically scrolled horizontally to make
29208 point visible. */);
29209 automatic_hscrolling_p = 1;
29210 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29211
29212 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29213 doc: /* How many columns away from the window edge point is allowed to get
29214 before automatic hscrolling will horizontally scroll the window. */);
29215 hscroll_margin = 5;
29216
29217 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29218 doc: /* How many columns to scroll the window when point gets too close to the edge.
29219 When point is less than `hscroll-margin' columns from the window
29220 edge, automatic hscrolling will scroll the window by the amount of columns
29221 determined by this variable. If its value is a positive integer, scroll that
29222 many columns. If it's a positive floating-point number, it specifies the
29223 fraction of the window's width to scroll. If it's nil or zero, point will be
29224 centered horizontally after the scroll. Any other value, including negative
29225 numbers, are treated as if the value were zero.
29226
29227 Automatic hscrolling always moves point outside the scroll margin, so if
29228 point was more than scroll step columns inside the margin, the window will
29229 scroll more than the value given by the scroll step.
29230
29231 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29232 and `scroll-right' overrides this variable's effect. */);
29233 Vhscroll_step = make_number (0);
29234
29235 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29236 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29237 Bind this around calls to `message' to let it take effect. */);
29238 message_truncate_lines = 0;
29239
29240 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29241 doc: /* Normal hook run to update the menu bar definitions.
29242 Redisplay runs this hook before it redisplays the menu bar.
29243 This is used to update submenus such as Buffers,
29244 whose contents depend on various data. */);
29245 Vmenu_bar_update_hook = Qnil;
29246
29247 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29248 doc: /* Frame for which we are updating a menu.
29249 The enable predicate for a menu binding should check this variable. */);
29250 Vmenu_updating_frame = Qnil;
29251
29252 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29253 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29254 inhibit_menubar_update = 0;
29255
29256 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29257 doc: /* Prefix prepended to all continuation lines at display time.
29258 The value may be a string, an image, or a stretch-glyph; it is
29259 interpreted in the same way as the value of a `display' text property.
29260
29261 This variable is overridden by any `wrap-prefix' text or overlay
29262 property.
29263
29264 To add a prefix to non-continuation lines, use `line-prefix'. */);
29265 Vwrap_prefix = Qnil;
29266 DEFSYM (Qwrap_prefix, "wrap-prefix");
29267 Fmake_variable_buffer_local (Qwrap_prefix);
29268
29269 DEFVAR_LISP ("line-prefix", Vline_prefix,
29270 doc: /* Prefix prepended to all non-continuation lines at display time.
29271 The value may be a string, an image, or a stretch-glyph; it is
29272 interpreted in the same way as the value of a `display' text property.
29273
29274 This variable is overridden by any `line-prefix' text or overlay
29275 property.
29276
29277 To add a prefix to continuation lines, use `wrap-prefix'. */);
29278 Vline_prefix = Qnil;
29279 DEFSYM (Qline_prefix, "line-prefix");
29280 Fmake_variable_buffer_local (Qline_prefix);
29281
29282 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29283 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29284 inhibit_eval_during_redisplay = 0;
29285
29286 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29287 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29288 inhibit_free_realized_faces = 0;
29289
29290 #ifdef GLYPH_DEBUG
29291 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29292 doc: /* Inhibit try_window_id display optimization. */);
29293 inhibit_try_window_id = 0;
29294
29295 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29296 doc: /* Inhibit try_window_reusing display optimization. */);
29297 inhibit_try_window_reusing = 0;
29298
29299 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29300 doc: /* Inhibit try_cursor_movement display optimization. */);
29301 inhibit_try_cursor_movement = 0;
29302 #endif /* GLYPH_DEBUG */
29303
29304 DEFVAR_INT ("overline-margin", overline_margin,
29305 doc: /* Space between overline and text, in pixels.
29306 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29307 margin to the character height. */);
29308 overline_margin = 2;
29309
29310 DEFVAR_INT ("underline-minimum-offset",
29311 underline_minimum_offset,
29312 doc: /* Minimum distance between baseline and underline.
29313 This can improve legibility of underlined text at small font sizes,
29314 particularly when using variable `x-use-underline-position-properties'
29315 with fonts that specify an UNDERLINE_POSITION relatively close to the
29316 baseline. The default value is 1. */);
29317 underline_minimum_offset = 1;
29318
29319 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29320 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29321 This feature only works when on a window system that can change
29322 cursor shapes. */);
29323 display_hourglass_p = 1;
29324
29325 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29326 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29327 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29328
29329 hourglass_atimer = NULL;
29330 hourglass_shown_p = 0;
29331
29332 DEFSYM (Qglyphless_char, "glyphless-char");
29333 DEFSYM (Qhex_code, "hex-code");
29334 DEFSYM (Qempty_box, "empty-box");
29335 DEFSYM (Qthin_space, "thin-space");
29336 DEFSYM (Qzero_width, "zero-width");
29337
29338 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29339 /* Intern this now in case it isn't already done.
29340 Setting this variable twice is harmless.
29341 But don't staticpro it here--that is done in alloc.c. */
29342 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29343 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29344
29345 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29346 doc: /* Char-table defining glyphless characters.
29347 Each element, if non-nil, should be one of the following:
29348 an ASCII acronym string: display this string in a box
29349 `hex-code': display the hexadecimal code of a character in a box
29350 `empty-box': display as an empty box
29351 `thin-space': display as 1-pixel width space
29352 `zero-width': don't display
29353 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29354 display method for graphical terminals and text terminals respectively.
29355 GRAPHICAL and TEXT should each have one of the values listed above.
29356
29357 The char-table has one extra slot to control the display of a character for
29358 which no font is found. This slot only takes effect on graphical terminals.
29359 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29360 `thin-space'. The default is `empty-box'. */);
29361 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29362 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29363 Qempty_box);
29364
29365 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29366 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29367 Vdebug_on_message = Qnil;
29368 }
29369
29370
29371 /* Initialize this module when Emacs starts. */
29372
29373 void
29374 init_xdisp (void)
29375 {
29376 current_header_line_height = current_mode_line_height = -1;
29377
29378 CHARPOS (this_line_start_pos) = 0;
29379
29380 if (!noninteractive)
29381 {
29382 struct window *m = XWINDOW (minibuf_window);
29383 Lisp_Object frame = m->frame;
29384 struct frame *f = XFRAME (frame);
29385 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29386 struct window *r = XWINDOW (root);
29387 int i;
29388
29389 echo_area_window = minibuf_window;
29390
29391 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29392 wset_total_lines
29393 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29394 wset_total_cols (r, make_number (FRAME_COLS (f)));
29395 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29396 wset_total_lines (m, make_number (1));
29397 wset_total_cols (m, make_number (FRAME_COLS (f)));
29398
29399 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29400 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29401 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29402
29403 /* The default ellipsis glyphs `...'. */
29404 for (i = 0; i < 3; ++i)
29405 default_invis_vector[i] = make_number ('.');
29406 }
29407
29408 {
29409 /* Allocate the buffer for frame titles.
29410 Also used for `format-mode-line'. */
29411 int size = 100;
29412 mode_line_noprop_buf = xmalloc (size);
29413 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29414 mode_line_noprop_ptr = mode_line_noprop_buf;
29415 mode_line_target = MODE_LINE_DISPLAY;
29416 }
29417
29418 help_echo_showing_p = 0;
29419 }
29420
29421 /* Platform-independent portion of hourglass implementation. */
29422
29423 /* Cancel a currently active hourglass timer, and start a new one. */
29424 void
29425 start_hourglass (void)
29426 {
29427 #if defined (HAVE_WINDOW_SYSTEM)
29428 EMACS_TIME delay;
29429
29430 cancel_hourglass ();
29431
29432 if (INTEGERP (Vhourglass_delay)
29433 && XINT (Vhourglass_delay) > 0)
29434 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29435 TYPE_MAXIMUM (time_t)),
29436 0);
29437 else if (FLOATP (Vhourglass_delay)
29438 && XFLOAT_DATA (Vhourglass_delay) > 0)
29439 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29440 else
29441 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29442
29443 #ifdef HAVE_NTGUI
29444 {
29445 extern void w32_note_current_window (void);
29446 w32_note_current_window ();
29447 }
29448 #endif /* HAVE_NTGUI */
29449
29450 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29451 show_hourglass, NULL);
29452 #endif
29453 }
29454
29455
29456 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29457 shown. */
29458 void
29459 cancel_hourglass (void)
29460 {
29461 #if defined (HAVE_WINDOW_SYSTEM)
29462 if (hourglass_atimer)
29463 {
29464 cancel_atimer (hourglass_atimer);
29465 hourglass_atimer = NULL;
29466 }
29467
29468 if (hourglass_shown_p)
29469 hide_hourglass ();
29470 #endif
29471 }