* dispnew.c (set_window_cursor_after_update): Use clip_to_bounds.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef HAVE_NTGUI
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes. */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes. */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350 static Lisp_Object Qredisplay_internal;
351
352 /* Non-nil means don't actually do any redisplay. */
353
354 Lisp_Object Qinhibit_redisplay;
355
356 /* Names of text properties relevant for redisplay. */
357
358 Lisp_Object Qdisplay;
359
360 Lisp_Object Qspace, QCalign_to;
361 static Lisp_Object QCrelative_width, QCrelative_height;
362 Lisp_Object Qleft_margin, Qright_margin;
363 static Lisp_Object Qspace_width, Qraise;
364 static Lisp_Object Qslice;
365 Lisp_Object Qcenter;
366 static Lisp_Object Qmargin, Qpointer;
367 static Lisp_Object Qline_height;
368
369 /* These setters are used only in this file, so they can be private. */
370 static void
371 wset_base_line_number (struct window *w, Lisp_Object val)
372 {
373 w->base_line_number = val;
374 }
375 static void
376 wset_base_line_pos (struct window *w, Lisp_Object val)
377 {
378 w->base_line_pos = val;
379 }
380 static void
381 wset_column_number_displayed (struct window *w, Lisp_Object val)
382 {
383 w->column_number_displayed = val;
384 }
385 static void
386 wset_region_showing (struct window *w, Lisp_Object val)
387 {
388 w->region_showing = val;
389 }
390
391 #ifdef HAVE_WINDOW_SYSTEM
392
393 /* Test if overflow newline into fringe. Called with iterator IT
394 at or past right window margin, and with IT->current_x set. */
395
396 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
397 (!NILP (Voverflow_newline_into_fringe) \
398 && FRAME_WINDOW_P ((IT)->f) \
399 && ((IT)->bidi_it.paragraph_dir == R2L \
400 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
401 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
402 && (IT)->current_x == (IT)->last_visible_x \
403 && (IT)->line_wrap != WORD_WRAP)
404
405 #else /* !HAVE_WINDOW_SYSTEM */
406 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
407 #endif /* HAVE_WINDOW_SYSTEM */
408
409 /* Test if the display element loaded in IT, or the underlying buffer
410 or string character, is a space or a TAB character. This is used
411 to determine where word wrapping can occur. */
412
413 #define IT_DISPLAYING_WHITESPACE(it) \
414 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
415 || ((STRINGP (it->string) \
416 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
417 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
418 || (it->s \
419 && (it->s[IT_BYTEPOS (*it)] == ' ' \
420 || it->s[IT_BYTEPOS (*it)] == '\t')) \
421 || (IT_BYTEPOS (*it) < ZV_BYTE \
422 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
423 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
424
425 /* Name of the face used to highlight trailing whitespace. */
426
427 static Lisp_Object Qtrailing_whitespace;
428
429 /* Name and number of the face used to highlight escape glyphs. */
430
431 static Lisp_Object Qescape_glyph;
432
433 /* Name and number of the face used to highlight non-breaking spaces. */
434
435 static Lisp_Object Qnobreak_space;
436
437 /* The symbol `image' which is the car of the lists used to represent
438 images in Lisp. Also a tool bar style. */
439
440 Lisp_Object Qimage;
441
442 /* The image map types. */
443 Lisp_Object QCmap;
444 static Lisp_Object QCpointer;
445 static Lisp_Object Qrect, Qcircle, Qpoly;
446
447 /* Tool bar styles */
448 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
449
450 /* Non-zero means print newline to stdout before next mini-buffer
451 message. */
452
453 int noninteractive_need_newline;
454
455 /* Non-zero means print newline to message log before next message. */
456
457 static int message_log_need_newline;
458
459 /* Three markers that message_dolog uses.
460 It could allocate them itself, but that causes trouble
461 in handling memory-full errors. */
462 static Lisp_Object message_dolog_marker1;
463 static Lisp_Object message_dolog_marker2;
464 static Lisp_Object message_dolog_marker3;
465 \f
466 /* The buffer position of the first character appearing entirely or
467 partially on the line of the selected window which contains the
468 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
469 redisplay optimization in redisplay_internal. */
470
471 static struct text_pos this_line_start_pos;
472
473 /* Number of characters past the end of the line above, including the
474 terminating newline. */
475
476 static struct text_pos this_line_end_pos;
477
478 /* The vertical positions and the height of this line. */
479
480 static int this_line_vpos;
481 static int this_line_y;
482 static int this_line_pixel_height;
483
484 /* X position at which this display line starts. Usually zero;
485 negative if first character is partially visible. */
486
487 static int this_line_start_x;
488
489 /* The smallest character position seen by move_it_* functions as they
490 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
491 hscrolled lines, see display_line. */
492
493 static struct text_pos this_line_min_pos;
494
495 /* Buffer that this_line_.* variables are referring to. */
496
497 static struct buffer *this_line_buffer;
498
499
500 /* Values of those variables at last redisplay are stored as
501 properties on `overlay-arrow-position' symbol. However, if
502 Voverlay_arrow_position is a marker, last-arrow-position is its
503 numerical position. */
504
505 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
506
507 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
508 properties on a symbol in overlay-arrow-variable-list. */
509
510 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
511
512 Lisp_Object Qmenu_bar_update_hook;
513
514 /* Nonzero if an overlay arrow has been displayed in this window. */
515
516 static int overlay_arrow_seen;
517
518 /* Vector containing glyphs for an ellipsis `...'. */
519
520 static Lisp_Object default_invis_vector[3];
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 static Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 static int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 static int line_number_displayed;
557
558 /* The name of the *Messages* buffer, a string. */
559
560 static Lisp_Object Vmessages_buffer_name;
561
562 /* Current, index 0, and last displayed echo area message. Either
563 buffers from echo_buffers, or nil to indicate no message. */
564
565 Lisp_Object echo_area_buffer[2];
566
567 /* The buffers referenced from echo_area_buffer. */
568
569 static Lisp_Object echo_buffer[2];
570
571 /* A vector saved used in with_area_buffer to reduce consing. */
572
573 static Lisp_Object Vwith_echo_area_save_vector;
574
575 /* Non-zero means display_echo_area should display the last echo area
576 message again. Set by redisplay_preserve_echo_area. */
577
578 static int display_last_displayed_message_p;
579
580 /* Nonzero if echo area is being used by print; zero if being used by
581 message. */
582
583 static int message_buf_print;
584
585 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
586
587 static Lisp_Object Qinhibit_menubar_update;
588 static Lisp_Object Qmessage_truncate_lines;
589
590 /* Set to 1 in clear_message to make redisplay_internal aware
591 of an emptied echo area. */
592
593 static int message_cleared_p;
594
595 /* A scratch glyph row with contents used for generating truncation
596 glyphs. Also used in direct_output_for_insert. */
597
598 #define MAX_SCRATCH_GLYPHS 100
599 static struct glyph_row scratch_glyph_row;
600 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
601
602 /* Ascent and height of the last line processed by move_it_to. */
603
604 static int last_max_ascent, last_height;
605
606 /* Non-zero if there's a help-echo in the echo area. */
607
608 int help_echo_showing_p;
609
610 /* If >= 0, computed, exact values of mode-line and header-line height
611 to use in the macros CURRENT_MODE_LINE_HEIGHT and
612 CURRENT_HEADER_LINE_HEIGHT. */
613
614 int current_mode_line_height, current_header_line_height;
615
616 /* The maximum distance to look ahead for text properties. Values
617 that are too small let us call compute_char_face and similar
618 functions too often which is expensive. Values that are too large
619 let us call compute_char_face and alike too often because we
620 might not be interested in text properties that far away. */
621
622 #define TEXT_PROP_DISTANCE_LIMIT 100
623
624 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
625 iterator state and later restore it. This is needed because the
626 bidi iterator on bidi.c keeps a stacked cache of its states, which
627 is really a singleton. When we use scratch iterator objects to
628 move around the buffer, we can cause the bidi cache to be pushed or
629 popped, and therefore we need to restore the cache state when we
630 return to the original iterator. */
631 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
632 do { \
633 if (CACHE) \
634 bidi_unshelve_cache (CACHE, 1); \
635 ITCOPY = ITORIG; \
636 CACHE = bidi_shelve_cache (); \
637 } while (0)
638
639 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
640 do { \
641 if (pITORIG != pITCOPY) \
642 *(pITORIG) = *(pITCOPY); \
643 bidi_unshelve_cache (CACHE, 0); \
644 CACHE = NULL; \
645 } while (0)
646
647 #ifdef GLYPH_DEBUG
648
649 /* Non-zero means print traces of redisplay if compiled with
650 GLYPH_DEBUG defined. */
651
652 int trace_redisplay_p;
653
654 #endif /* GLYPH_DEBUG */
655
656 #ifdef DEBUG_TRACE_MOVE
657 /* Non-zero means trace with TRACE_MOVE to stderr. */
658 int trace_move;
659
660 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
661 #else
662 #define TRACE_MOVE(x) (void) 0
663 #endif
664
665 static Lisp_Object Qauto_hscroll_mode;
666
667 /* Buffer being redisplayed -- for redisplay_window_error. */
668
669 static struct buffer *displayed_buffer;
670
671 /* Value returned from text property handlers (see below). */
672
673 enum prop_handled
674 {
675 HANDLED_NORMALLY,
676 HANDLED_RECOMPUTE_PROPS,
677 HANDLED_OVERLAY_STRING_CONSUMED,
678 HANDLED_RETURN
679 };
680
681 /* A description of text properties that redisplay is interested
682 in. */
683
684 struct props
685 {
686 /* The name of the property. */
687 Lisp_Object *name;
688
689 /* A unique index for the property. */
690 enum prop_idx idx;
691
692 /* A handler function called to set up iterator IT from the property
693 at IT's current position. Value is used to steer handle_stop. */
694 enum prop_handled (*handler) (struct it *it);
695 };
696
697 static enum prop_handled handle_face_prop (struct it *);
698 static enum prop_handled handle_invisible_prop (struct it *);
699 static enum prop_handled handle_display_prop (struct it *);
700 static enum prop_handled handle_composition_prop (struct it *);
701 static enum prop_handled handle_overlay_change (struct it *);
702 static enum prop_handled handle_fontified_prop (struct it *);
703
704 /* Properties handled by iterators. */
705
706 static struct props it_props[] =
707 {
708 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
709 /* Handle `face' before `display' because some sub-properties of
710 `display' need to know the face. */
711 {&Qface, FACE_PROP_IDX, handle_face_prop},
712 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
713 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
714 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
715 {NULL, 0, NULL}
716 };
717
718 /* Value is the position described by X. If X is a marker, value is
719 the marker_position of X. Otherwise, value is X. */
720
721 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
722
723 /* Enumeration returned by some move_it_.* functions internally. */
724
725 enum move_it_result
726 {
727 /* Not used. Undefined value. */
728 MOVE_UNDEFINED,
729
730 /* Move ended at the requested buffer position or ZV. */
731 MOVE_POS_MATCH_OR_ZV,
732
733 /* Move ended at the requested X pixel position. */
734 MOVE_X_REACHED,
735
736 /* Move within a line ended at the end of a line that must be
737 continued. */
738 MOVE_LINE_CONTINUED,
739
740 /* Move within a line ended at the end of a line that would
741 be displayed truncated. */
742 MOVE_LINE_TRUNCATED,
743
744 /* Move within a line ended at a line end. */
745 MOVE_NEWLINE_OR_CR
746 };
747
748 /* This counter is used to clear the face cache every once in a while
749 in redisplay_internal. It is incremented for each redisplay.
750 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
751 cleared. */
752
753 #define CLEAR_FACE_CACHE_COUNT 500
754 static int clear_face_cache_count;
755
756 /* Similarly for the image cache. */
757
758 #ifdef HAVE_WINDOW_SYSTEM
759 #define CLEAR_IMAGE_CACHE_COUNT 101
760 static int clear_image_cache_count;
761
762 /* Null glyph slice */
763 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
764 #endif
765
766 /* True while redisplay_internal is in progress. */
767
768 bool redisplaying_p;
769
770 static Lisp_Object Qinhibit_free_realized_faces;
771 static Lisp_Object Qmode_line_default_help_echo;
772
773 /* If a string, XTread_socket generates an event to display that string.
774 (The display is done in read_char.) */
775
776 Lisp_Object help_echo_string;
777 Lisp_Object help_echo_window;
778 Lisp_Object help_echo_object;
779 ptrdiff_t help_echo_pos;
780
781 /* Temporary variable for XTread_socket. */
782
783 Lisp_Object previous_help_echo_string;
784
785 /* Platform-independent portion of hourglass implementation. */
786
787 /* Non-zero means an hourglass cursor is currently shown. */
788 int hourglass_shown_p;
789
790 /* If non-null, an asynchronous timer that, when it expires, displays
791 an hourglass cursor on all frames. */
792 struct atimer *hourglass_atimer;
793
794 /* Name of the face used to display glyphless characters. */
795 Lisp_Object Qglyphless_char;
796
797 /* Symbol for the purpose of Vglyphless_char_display. */
798 static Lisp_Object Qglyphless_char_display;
799
800 /* Method symbols for Vglyphless_char_display. */
801 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
802
803 /* Default pixel width of `thin-space' display method. */
804 #define THIN_SPACE_WIDTH 1
805
806 /* Default number of seconds to wait before displaying an hourglass
807 cursor. */
808 #define DEFAULT_HOURGLASS_DELAY 1
809
810 \f
811 /* Function prototypes. */
812
813 static void setup_for_ellipsis (struct it *, int);
814 static void set_iterator_to_next (struct it *, int);
815 static void mark_window_display_accurate_1 (struct window *, int);
816 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
817 static int display_prop_string_p (Lisp_Object, Lisp_Object);
818 static int cursor_row_p (struct glyph_row *);
819 static int redisplay_mode_lines (Lisp_Object, int);
820 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
821
822 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
823
824 static void handle_line_prefix (struct it *);
825
826 static void pint2str (char *, int, ptrdiff_t);
827 static void pint2hrstr (char *, int, ptrdiff_t);
828 static struct text_pos run_window_scroll_functions (Lisp_Object,
829 struct text_pos);
830 static void reconsider_clip_changes (struct window *, struct buffer *);
831 static int text_outside_line_unchanged_p (struct window *,
832 ptrdiff_t, ptrdiff_t);
833 static void store_mode_line_noprop_char (char);
834 static int store_mode_line_noprop (const char *, int, int);
835 static void handle_stop (struct it *);
836 static void handle_stop_backwards (struct it *, ptrdiff_t);
837 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
838 static void ensure_echo_area_buffers (void);
839 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
840 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
841 static int with_echo_area_buffer (struct window *, int,
842 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
843 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
844 static void clear_garbaged_frames (void);
845 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
846 static void pop_message (void);
847 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
848 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
849 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
850 static int display_echo_area (struct window *);
851 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
852 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static Lisp_Object unwind_redisplay (Lisp_Object);
854 static int string_char_and_length (const unsigned char *, int *);
855 static struct text_pos display_prop_end (struct it *, Lisp_Object,
856 struct text_pos);
857 static int compute_window_start_on_continuation_line (struct window *);
858 static void insert_left_trunc_glyphs (struct it *);
859 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
860 Lisp_Object);
861 static void extend_face_to_end_of_line (struct it *);
862 static int append_space_for_newline (struct it *, int);
863 static int cursor_row_fully_visible_p (struct window *, int, int);
864 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
865 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
866 static int trailing_whitespace_p (ptrdiff_t);
867 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
868 static void push_it (struct it *, struct text_pos *);
869 static void iterate_out_of_display_property (struct it *);
870 static void pop_it (struct it *);
871 static void sync_frame_with_window_matrix_rows (struct window *);
872 static void select_frame_for_redisplay (Lisp_Object);
873 static void redisplay_internal (void);
874 static int echo_area_display (int);
875 static void redisplay_windows (Lisp_Object);
876 static void redisplay_window (Lisp_Object, int);
877 static Lisp_Object redisplay_window_error (Lisp_Object);
878 static Lisp_Object redisplay_window_0 (Lisp_Object);
879 static Lisp_Object redisplay_window_1 (Lisp_Object);
880 static int set_cursor_from_row (struct window *, struct glyph_row *,
881 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
882 int, int);
883 static int update_menu_bar (struct frame *, int, int);
884 static int try_window_reusing_current_matrix (struct window *);
885 static int try_window_id (struct window *);
886 static int display_line (struct it *);
887 static int display_mode_lines (struct window *);
888 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
889 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
890 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
891 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
892 static void display_menu_bar (struct window *);
893 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
894 ptrdiff_t *);
895 static int display_string (const char *, Lisp_Object, Lisp_Object,
896 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
897 static void compute_line_metrics (struct it *);
898 static void run_redisplay_end_trigger_hook (struct it *);
899 static int get_overlay_strings (struct it *, ptrdiff_t);
900 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
901 static void next_overlay_string (struct it *);
902 static void reseat (struct it *, struct text_pos, int);
903 static void reseat_1 (struct it *, struct text_pos, int);
904 static void back_to_previous_visible_line_start (struct it *);
905 void reseat_at_previous_visible_line_start (struct it *);
906 static void reseat_at_next_visible_line_start (struct it *, int);
907 static int next_element_from_ellipsis (struct it *);
908 static int next_element_from_display_vector (struct it *);
909 static int next_element_from_string (struct it *);
910 static int next_element_from_c_string (struct it *);
911 static int next_element_from_buffer (struct it *);
912 static int next_element_from_composition (struct it *);
913 static int next_element_from_image (struct it *);
914 static int next_element_from_stretch (struct it *);
915 static void load_overlay_strings (struct it *, ptrdiff_t);
916 static int init_from_display_pos (struct it *, struct window *,
917 struct display_pos *);
918 static void reseat_to_string (struct it *, const char *,
919 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
920 static int get_next_display_element (struct it *);
921 static enum move_it_result
922 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
923 enum move_operation_enum);
924 void move_it_vertically_backward (struct it *, int);
925 static void get_visually_first_element (struct it *);
926 static void init_to_row_start (struct it *, struct window *,
927 struct glyph_row *);
928 static int init_to_row_end (struct it *, struct window *,
929 struct glyph_row *);
930 static void back_to_previous_line_start (struct it *);
931 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
932 static struct text_pos string_pos_nchars_ahead (struct text_pos,
933 Lisp_Object, ptrdiff_t);
934 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
935 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
936 static ptrdiff_t number_of_chars (const char *, int);
937 static void compute_stop_pos (struct it *);
938 static void compute_string_pos (struct text_pos *, struct text_pos,
939 Lisp_Object);
940 static int face_before_or_after_it_pos (struct it *, int);
941 static ptrdiff_t next_overlay_change (ptrdiff_t);
942 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
943 Lisp_Object, struct text_pos *, ptrdiff_t, int);
944 static int handle_single_display_spec (struct it *, Lisp_Object,
945 Lisp_Object, Lisp_Object,
946 struct text_pos *, ptrdiff_t, int, int);
947 static int underlying_face_id (struct it *);
948 static int in_ellipses_for_invisible_text_p (struct display_pos *,
949 struct window *);
950
951 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
952 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
953
954 #ifdef HAVE_WINDOW_SYSTEM
955
956 static void x_consider_frame_title (Lisp_Object);
957 static int tool_bar_lines_needed (struct frame *, int *);
958 static void update_tool_bar (struct frame *, int);
959 static void build_desired_tool_bar_string (struct frame *f);
960 static int redisplay_tool_bar (struct frame *);
961 static void display_tool_bar_line (struct it *, int);
962 static void notice_overwritten_cursor (struct window *,
963 enum glyph_row_area,
964 int, int, int, int);
965 static void append_stretch_glyph (struct it *, Lisp_Object,
966 int, int, int);
967
968
969 #endif /* HAVE_WINDOW_SYSTEM */
970
971 static void produce_special_glyphs (struct it *, enum display_element_type);
972 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
973 static int coords_in_mouse_face_p (struct window *, int, int);
974
975
976 \f
977 /***********************************************************************
978 Window display dimensions
979 ***********************************************************************/
980
981 /* Return the bottom boundary y-position for text lines in window W.
982 This is the first y position at which a line cannot start.
983 It is relative to the top of the window.
984
985 This is the height of W minus the height of a mode line, if any. */
986
987 int
988 window_text_bottom_y (struct window *w)
989 {
990 int height = WINDOW_TOTAL_HEIGHT (w);
991
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
995 }
996
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1000
1001 int
1002 window_box_width (struct window *w, int area)
1003 {
1004 int cols = XFASTINT (w->total_cols);
1005 int pixels = 0;
1006
1007 if (!w->pseudo_window_p)
1008 {
1009 cols -= WINDOW_SCROLL_BAR_COLS (w);
1010
1011 if (area == TEXT_AREA)
1012 {
1013 if (INTEGERP (w->left_margin_cols))
1014 cols -= XFASTINT (w->left_margin_cols);
1015 if (INTEGERP (w->right_margin_cols))
1016 cols -= XFASTINT (w->right_margin_cols);
1017 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1018 }
1019 else if (area == LEFT_MARGIN_AREA)
1020 {
1021 cols = (INTEGERP (w->left_margin_cols)
1022 ? XFASTINT (w->left_margin_cols) : 0);
1023 pixels = 0;
1024 }
1025 else if (area == RIGHT_MARGIN_AREA)
1026 {
1027 cols = (INTEGERP (w->right_margin_cols)
1028 ? XFASTINT (w->right_margin_cols) : 0);
1029 pixels = 0;
1030 }
1031 }
1032
1033 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1034 }
1035
1036
1037 /* Return the pixel height of the display area of window W, not
1038 including mode lines of W, if any. */
1039
1040 int
1041 window_box_height (struct window *w)
1042 {
1043 struct frame *f = XFRAME (w->frame);
1044 int height = WINDOW_TOTAL_HEIGHT (w);
1045
1046 eassert (height >= 0);
1047
1048 /* Note: the code below that determines the mode-line/header-line
1049 height is essentially the same as that contained in the macro
1050 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1051 the appropriate glyph row has its `mode_line_p' flag set,
1052 and if it doesn't, uses estimate_mode_line_height instead. */
1053
1054 if (WINDOW_WANTS_MODELINE_P (w))
1055 {
1056 struct glyph_row *ml_row
1057 = (w->current_matrix && w->current_matrix->rows
1058 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1059 : 0);
1060 if (ml_row && ml_row->mode_line_p)
1061 height -= ml_row->height;
1062 else
1063 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1064 }
1065
1066 if (WINDOW_WANTS_HEADER_LINE_P (w))
1067 {
1068 struct glyph_row *hl_row
1069 = (w->current_matrix && w->current_matrix->rows
1070 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1071 : 0);
1072 if (hl_row && hl_row->mode_line_p)
1073 height -= hl_row->height;
1074 else
1075 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1076 }
1077
1078 /* With a very small font and a mode-line that's taller than
1079 default, we might end up with a negative height. */
1080 return max (0, height);
1081 }
1082
1083 /* Return the window-relative coordinate of the left edge of display
1084 area AREA of window W. AREA < 0 means return the left edge of the
1085 whole window, to the right of the left fringe of W. */
1086
1087 int
1088 window_box_left_offset (struct window *w, int area)
1089 {
1090 int x;
1091
1092 if (w->pseudo_window_p)
1093 return 0;
1094
1095 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1096
1097 if (area == TEXT_AREA)
1098 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1099 + window_box_width (w, LEFT_MARGIN_AREA));
1100 else if (area == RIGHT_MARGIN_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA)
1103 + window_box_width (w, TEXT_AREA)
1104 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1105 ? 0
1106 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1107 else if (area == LEFT_MARGIN_AREA
1108 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1109 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the window-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right_offset (struct window *w, int area)
1121 {
1122 return window_box_left_offset (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Return the frame-relative coordinate of the left edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the right of the left fringe of W. */
1128
1129 int
1130 window_box_left (struct window *w, int area)
1131 {
1132 struct frame *f = XFRAME (w->frame);
1133 int x;
1134
1135 if (w->pseudo_window_p)
1136 return FRAME_INTERNAL_BORDER_WIDTH (f);
1137
1138 x = (WINDOW_LEFT_EDGE_X (w)
1139 + window_box_left_offset (w, area));
1140
1141 return x;
1142 }
1143
1144
1145 /* Return the frame-relative coordinate of the right edge of display
1146 area AREA of window W. AREA < 0 means return the right edge of the
1147 whole window, to the left of the right fringe of W. */
1148
1149 int
1150 window_box_right (struct window *w, int area)
1151 {
1152 return window_box_left (w, area) + window_box_width (w, area);
1153 }
1154
1155 /* Get the bounding box of the display area AREA of window W, without
1156 mode lines, in frame-relative coordinates. AREA < 0 means the
1157 whole window, not including the left and right fringes of
1158 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1159 coordinates of the upper-left corner of the box. Return in
1160 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1161
1162 void
1163 window_box (struct window *w, int area, int *box_x, int *box_y,
1164 int *box_width, int *box_height)
1165 {
1166 if (box_width)
1167 *box_width = window_box_width (w, area);
1168 if (box_height)
1169 *box_height = window_box_height (w);
1170 if (box_x)
1171 *box_x = window_box_left (w, area);
1172 if (box_y)
1173 {
1174 *box_y = WINDOW_TOP_EDGE_Y (w);
1175 if (WINDOW_WANTS_HEADER_LINE_P (w))
1176 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1177 }
1178 }
1179
1180
1181 /* Get the bounding box of the display area AREA of window W, without
1182 mode lines. AREA < 0 means the whole window, not including the
1183 left and right fringe of the window. Return in *TOP_LEFT_X
1184 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1185 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1186 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1187 box. */
1188
1189 static void
1190 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1191 int *bottom_right_x, int *bottom_right_y)
1192 {
1193 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1194 bottom_right_y);
1195 *bottom_right_x += *top_left_x;
1196 *bottom_right_y += *top_left_y;
1197 }
1198
1199
1200 \f
1201 /***********************************************************************
1202 Utilities
1203 ***********************************************************************/
1204
1205 /* Return the bottom y-position of the line the iterator IT is in.
1206 This can modify IT's settings. */
1207
1208 int
1209 line_bottom_y (struct it *it)
1210 {
1211 int line_height = it->max_ascent + it->max_descent;
1212 int line_top_y = it->current_y;
1213
1214 if (line_height == 0)
1215 {
1216 if (last_height)
1217 line_height = last_height;
1218 else if (IT_CHARPOS (*it) < ZV)
1219 {
1220 move_it_by_lines (it, 1);
1221 line_height = (it->max_ascent || it->max_descent
1222 ? it->max_ascent + it->max_descent
1223 : last_height);
1224 }
1225 else
1226 {
1227 struct glyph_row *row = it->glyph_row;
1228
1229 /* Use the default character height. */
1230 it->glyph_row = NULL;
1231 it->what = IT_CHARACTER;
1232 it->c = ' ';
1233 it->len = 1;
1234 PRODUCE_GLYPHS (it);
1235 line_height = it->ascent + it->descent;
1236 it->glyph_row = row;
1237 }
1238 }
1239
1240 return line_top_y + line_height;
1241 }
1242
1243 /* Subroutine of pos_visible_p below. Extracts a display string, if
1244 any, from the display spec given as its argument. */
1245 static Lisp_Object
1246 string_from_display_spec (Lisp_Object spec)
1247 {
1248 if (CONSP (spec))
1249 {
1250 while (CONSP (spec))
1251 {
1252 if (STRINGP (XCAR (spec)))
1253 return XCAR (spec);
1254 spec = XCDR (spec);
1255 }
1256 }
1257 else if (VECTORP (spec))
1258 {
1259 ptrdiff_t i;
1260
1261 for (i = 0; i < ASIZE (spec); i++)
1262 {
1263 if (STRINGP (AREF (spec, i)))
1264 return AREF (spec, i);
1265 }
1266 return Qnil;
1267 }
1268
1269 return spec;
1270 }
1271
1272
1273 /* Limit insanely large values of W->hscroll on frame F to the largest
1274 value that will still prevent first_visible_x and last_visible_x of
1275 'struct it' from overflowing an int. */
1276 static int
1277 window_hscroll_limited (struct window *w, struct frame *f)
1278 {
1279 ptrdiff_t window_hscroll = w->hscroll;
1280 int window_text_width = window_box_width (w, TEXT_AREA);
1281 int colwidth = FRAME_COLUMN_WIDTH (f);
1282
1283 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1284 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1285
1286 return window_hscroll;
1287 }
1288
1289 /* Return 1 if position CHARPOS is visible in window W.
1290 CHARPOS < 0 means return info about WINDOW_END position.
1291 If visible, set *X and *Y to pixel coordinates of top left corner.
1292 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1293 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1294
1295 int
1296 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1297 int *rtop, int *rbot, int *rowh, int *vpos)
1298 {
1299 struct it it;
1300 void *itdata = bidi_shelve_cache ();
1301 struct text_pos top;
1302 int visible_p = 0;
1303 struct buffer *old_buffer = NULL;
1304
1305 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1306 return visible_p;
1307
1308 if (XBUFFER (w->buffer) != current_buffer)
1309 {
1310 old_buffer = current_buffer;
1311 set_buffer_internal_1 (XBUFFER (w->buffer));
1312 }
1313
1314 SET_TEXT_POS_FROM_MARKER (top, w->start);
1315 /* Scrolling a minibuffer window via scroll bar when the echo area
1316 shows long text sometimes resets the minibuffer contents behind
1317 our backs. */
1318 if (CHARPOS (top) > ZV)
1319 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1320
1321 /* Compute exact mode line heights. */
1322 if (WINDOW_WANTS_MODELINE_P (w))
1323 current_mode_line_height
1324 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1325 BVAR (current_buffer, mode_line_format));
1326
1327 if (WINDOW_WANTS_HEADER_LINE_P (w))
1328 current_header_line_height
1329 = display_mode_line (w, HEADER_LINE_FACE_ID,
1330 BVAR (current_buffer, header_line_format));
1331
1332 start_display (&it, w, top);
1333 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1334 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1335
1336 if (charpos >= 0
1337 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1338 && IT_CHARPOS (it) >= charpos)
1339 /* When scanning backwards under bidi iteration, move_it_to
1340 stops at or _before_ CHARPOS, because it stops at or to
1341 the _right_ of the character at CHARPOS. */
1342 || (it.bidi_p && it.bidi_it.scan_dir == -1
1343 && IT_CHARPOS (it) <= charpos)))
1344 {
1345 /* We have reached CHARPOS, or passed it. How the call to
1346 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1347 or covered by a display property, move_it_to stops at the end
1348 of the invisible text, to the right of CHARPOS. (ii) If
1349 CHARPOS is in a display vector, move_it_to stops on its last
1350 glyph. */
1351 int top_x = it.current_x;
1352 int top_y = it.current_y;
1353 /* Calling line_bottom_y may change it.method, it.position, etc. */
1354 enum it_method it_method = it.method;
1355 int bottom_y = (last_height = 0, line_bottom_y (&it));
1356 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1357
1358 if (top_y < window_top_y)
1359 visible_p = bottom_y > window_top_y;
1360 else if (top_y < it.last_visible_y)
1361 visible_p = 1;
1362 if (bottom_y >= it.last_visible_y
1363 && it.bidi_p && it.bidi_it.scan_dir == -1
1364 && IT_CHARPOS (it) < charpos)
1365 {
1366 /* When the last line of the window is scanned backwards
1367 under bidi iteration, we could be duped into thinking
1368 that we have passed CHARPOS, when in fact move_it_to
1369 simply stopped short of CHARPOS because it reached
1370 last_visible_y. To see if that's what happened, we call
1371 move_it_to again with a slightly larger vertical limit,
1372 and see if it actually moved vertically; if it did, we
1373 didn't really reach CHARPOS, which is beyond window end. */
1374 struct it save_it = it;
1375 /* Why 10? because we don't know how many canonical lines
1376 will the height of the next line(s) be. So we guess. */
1377 int ten_more_lines =
1378 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1379
1380 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1381 MOVE_TO_POS | MOVE_TO_Y);
1382 if (it.current_y > top_y)
1383 visible_p = 0;
1384
1385 it = save_it;
1386 }
1387 if (visible_p)
1388 {
1389 if (it_method == GET_FROM_DISPLAY_VECTOR)
1390 {
1391 /* We stopped on the last glyph of a display vector.
1392 Try and recompute. Hack alert! */
1393 if (charpos < 2 || top.charpos >= charpos)
1394 top_x = it.glyph_row->x;
1395 else
1396 {
1397 struct it it2;
1398 start_display (&it2, w, top);
1399 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1400 get_next_display_element (&it2);
1401 PRODUCE_GLYPHS (&it2);
1402 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1403 || it2.current_x > it2.last_visible_x)
1404 top_x = it.glyph_row->x;
1405 else
1406 {
1407 top_x = it2.current_x;
1408 top_y = it2.current_y;
1409 }
1410 }
1411 }
1412 else if (IT_CHARPOS (it) != charpos)
1413 {
1414 Lisp_Object cpos = make_number (charpos);
1415 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1416 Lisp_Object string = string_from_display_spec (spec);
1417 int newline_in_string = 0;
1418
1419 if (STRINGP (string))
1420 {
1421 const char *s = SSDATA (string);
1422 const char *e = s + SBYTES (string);
1423 while (s < e)
1424 {
1425 if (*s++ == '\n')
1426 {
1427 newline_in_string = 1;
1428 break;
1429 }
1430 }
1431 }
1432 /* The tricky code below is needed because there's a
1433 discrepancy between move_it_to and how we set cursor
1434 when the display line ends in a newline from a
1435 display string. move_it_to will stop _after_ such
1436 display strings, whereas set_cursor_from_row
1437 conspires with cursor_row_p to place the cursor on
1438 the first glyph produced from the display string. */
1439
1440 /* We have overshoot PT because it is covered by a
1441 display property whose value is a string. If the
1442 string includes embedded newlines, we are also in the
1443 wrong display line. Backtrack to the correct line,
1444 where the display string begins. */
1445 if (newline_in_string)
1446 {
1447 Lisp_Object startpos, endpos;
1448 EMACS_INT start, end;
1449 struct it it3;
1450 int it3_moved;
1451
1452 /* Find the first and the last buffer positions
1453 covered by the display string. */
1454 endpos =
1455 Fnext_single_char_property_change (cpos, Qdisplay,
1456 Qnil, Qnil);
1457 startpos =
1458 Fprevious_single_char_property_change (endpos, Qdisplay,
1459 Qnil, Qnil);
1460 start = XFASTINT (startpos);
1461 end = XFASTINT (endpos);
1462 /* Move to the last buffer position before the
1463 display property. */
1464 start_display (&it3, w, top);
1465 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1466 /* Move forward one more line if the position before
1467 the display string is a newline or if it is the
1468 rightmost character on a line that is
1469 continued or word-wrapped. */
1470 if (it3.method == GET_FROM_BUFFER
1471 && it3.c == '\n')
1472 move_it_by_lines (&it3, 1);
1473 else if (move_it_in_display_line_to (&it3, -1,
1474 it3.current_x
1475 + it3.pixel_width,
1476 MOVE_TO_X)
1477 == MOVE_LINE_CONTINUED)
1478 {
1479 move_it_by_lines (&it3, 1);
1480 /* When we are under word-wrap, the #$@%!
1481 move_it_by_lines moves 2 lines, so we need to
1482 fix that up. */
1483 if (it3.line_wrap == WORD_WRAP)
1484 move_it_by_lines (&it3, -1);
1485 }
1486
1487 /* Record the vertical coordinate of the display
1488 line where we wound up. */
1489 top_y = it3.current_y;
1490 if (it3.bidi_p)
1491 {
1492 /* When characters are reordered for display,
1493 the character displayed to the left of the
1494 display string could be _after_ the display
1495 property in the logical order. Use the
1496 smallest vertical position of these two. */
1497 start_display (&it3, w, top);
1498 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1499 if (it3.current_y < top_y)
1500 top_y = it3.current_y;
1501 }
1502 /* Move from the top of the window to the beginning
1503 of the display line where the display string
1504 begins. */
1505 start_display (&it3, w, top);
1506 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1507 /* If it3_moved stays zero after the 'while' loop
1508 below, that means we already were at a newline
1509 before the loop (e.g., the display string begins
1510 with a newline), so we don't need to (and cannot)
1511 inspect the glyphs of it3.glyph_row, because
1512 PRODUCE_GLYPHS will not produce anything for a
1513 newline, and thus it3.glyph_row stays at its
1514 stale content it got at top of the window. */
1515 it3_moved = 0;
1516 /* Finally, advance the iterator until we hit the
1517 first display element whose character position is
1518 CHARPOS, or until the first newline from the
1519 display string, which signals the end of the
1520 display line. */
1521 while (get_next_display_element (&it3))
1522 {
1523 PRODUCE_GLYPHS (&it3);
1524 if (IT_CHARPOS (it3) == charpos
1525 || ITERATOR_AT_END_OF_LINE_P (&it3))
1526 break;
1527 it3_moved = 1;
1528 set_iterator_to_next (&it3, 0);
1529 }
1530 top_x = it3.current_x - it3.pixel_width;
1531 /* Normally, we would exit the above loop because we
1532 found the display element whose character
1533 position is CHARPOS. For the contingency that we
1534 didn't, and stopped at the first newline from the
1535 display string, move back over the glyphs
1536 produced from the string, until we find the
1537 rightmost glyph not from the string. */
1538 if (it3_moved
1539 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1540 {
1541 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1542 + it3.glyph_row->used[TEXT_AREA];
1543
1544 while (EQ ((g - 1)->object, string))
1545 {
1546 --g;
1547 top_x -= g->pixel_width;
1548 }
1549 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1550 + it3.glyph_row->used[TEXT_AREA]);
1551 }
1552 }
1553 }
1554
1555 *x = top_x;
1556 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1557 *rtop = max (0, window_top_y - top_y);
1558 *rbot = max (0, bottom_y - it.last_visible_y);
1559 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1560 - max (top_y, window_top_y)));
1561 *vpos = it.vpos;
1562 }
1563 }
1564 else
1565 {
1566 /* We were asked to provide info about WINDOW_END. */
1567 struct it it2;
1568 void *it2data = NULL;
1569
1570 SAVE_IT (it2, it, it2data);
1571 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1572 move_it_by_lines (&it, 1);
1573 if (charpos < IT_CHARPOS (it)
1574 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1575 {
1576 visible_p = 1;
1577 RESTORE_IT (&it2, &it2, it2data);
1578 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1579 *x = it2.current_x;
1580 *y = it2.current_y + it2.max_ascent - it2.ascent;
1581 *rtop = max (0, -it2.current_y);
1582 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1583 - it.last_visible_y));
1584 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1585 it.last_visible_y)
1586 - max (it2.current_y,
1587 WINDOW_HEADER_LINE_HEIGHT (w))));
1588 *vpos = it2.vpos;
1589 }
1590 else
1591 bidi_unshelve_cache (it2data, 1);
1592 }
1593 bidi_unshelve_cache (itdata, 0);
1594
1595 if (old_buffer)
1596 set_buffer_internal_1 (old_buffer);
1597
1598 current_header_line_height = current_mode_line_height = -1;
1599
1600 if (visible_p && w->hscroll > 0)
1601 *x -=
1602 window_hscroll_limited (w, WINDOW_XFRAME (w))
1603 * WINDOW_FRAME_COLUMN_WIDTH (w);
1604
1605 #if 0
1606 /* Debugging code. */
1607 if (visible_p)
1608 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1609 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1610 else
1611 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1612 #endif
1613
1614 return visible_p;
1615 }
1616
1617
1618 /* Return the next character from STR. Return in *LEN the length of
1619 the character. This is like STRING_CHAR_AND_LENGTH but never
1620 returns an invalid character. If we find one, we return a `?', but
1621 with the length of the invalid character. */
1622
1623 static int
1624 string_char_and_length (const unsigned char *str, int *len)
1625 {
1626 int c;
1627
1628 c = STRING_CHAR_AND_LENGTH (str, *len);
1629 if (!CHAR_VALID_P (c))
1630 /* We may not change the length here because other places in Emacs
1631 don't use this function, i.e. they silently accept invalid
1632 characters. */
1633 c = '?';
1634
1635 return c;
1636 }
1637
1638
1639
1640 /* Given a position POS containing a valid character and byte position
1641 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1642
1643 static struct text_pos
1644 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1645 {
1646 eassert (STRINGP (string) && nchars >= 0);
1647
1648 if (STRING_MULTIBYTE (string))
1649 {
1650 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1651 int len;
1652
1653 while (nchars--)
1654 {
1655 string_char_and_length (p, &len);
1656 p += len;
1657 CHARPOS (pos) += 1;
1658 BYTEPOS (pos) += len;
1659 }
1660 }
1661 else
1662 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1663
1664 return pos;
1665 }
1666
1667
1668 /* Value is the text position, i.e. character and byte position,
1669 for character position CHARPOS in STRING. */
1670
1671 static struct text_pos
1672 string_pos (ptrdiff_t charpos, Lisp_Object string)
1673 {
1674 struct text_pos pos;
1675 eassert (STRINGP (string));
1676 eassert (charpos >= 0);
1677 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1678 return pos;
1679 }
1680
1681
1682 /* Value is a text position, i.e. character and byte position, for
1683 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1684 means recognize multibyte characters. */
1685
1686 static struct text_pos
1687 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1688 {
1689 struct text_pos pos;
1690
1691 eassert (s != NULL);
1692 eassert (charpos >= 0);
1693
1694 if (multibyte_p)
1695 {
1696 int len;
1697
1698 SET_TEXT_POS (pos, 0, 0);
1699 while (charpos--)
1700 {
1701 string_char_and_length ((const unsigned char *) s, &len);
1702 s += len;
1703 CHARPOS (pos) += 1;
1704 BYTEPOS (pos) += len;
1705 }
1706 }
1707 else
1708 SET_TEXT_POS (pos, charpos, charpos);
1709
1710 return pos;
1711 }
1712
1713
1714 /* Value is the number of characters in C string S. MULTIBYTE_P
1715 non-zero means recognize multibyte characters. */
1716
1717 static ptrdiff_t
1718 number_of_chars (const char *s, int multibyte_p)
1719 {
1720 ptrdiff_t nchars;
1721
1722 if (multibyte_p)
1723 {
1724 ptrdiff_t rest = strlen (s);
1725 int len;
1726 const unsigned char *p = (const unsigned char *) s;
1727
1728 for (nchars = 0; rest > 0; ++nchars)
1729 {
1730 string_char_and_length (p, &len);
1731 rest -= len, p += len;
1732 }
1733 }
1734 else
1735 nchars = strlen (s);
1736
1737 return nchars;
1738 }
1739
1740
1741 /* Compute byte position NEWPOS->bytepos corresponding to
1742 NEWPOS->charpos. POS is a known position in string STRING.
1743 NEWPOS->charpos must be >= POS.charpos. */
1744
1745 static void
1746 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1747 {
1748 eassert (STRINGP (string));
1749 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1750
1751 if (STRING_MULTIBYTE (string))
1752 *newpos = string_pos_nchars_ahead (pos, string,
1753 CHARPOS (*newpos) - CHARPOS (pos));
1754 else
1755 BYTEPOS (*newpos) = CHARPOS (*newpos);
1756 }
1757
1758 /* EXPORT:
1759 Return an estimation of the pixel height of mode or header lines on
1760 frame F. FACE_ID specifies what line's height to estimate. */
1761
1762 int
1763 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1764 {
1765 #ifdef HAVE_WINDOW_SYSTEM
1766 if (FRAME_WINDOW_P (f))
1767 {
1768 int height = FONT_HEIGHT (FRAME_FONT (f));
1769
1770 /* This function is called so early when Emacs starts that the face
1771 cache and mode line face are not yet initialized. */
1772 if (FRAME_FACE_CACHE (f))
1773 {
1774 struct face *face = FACE_FROM_ID (f, face_id);
1775 if (face)
1776 {
1777 if (face->font)
1778 height = FONT_HEIGHT (face->font);
1779 if (face->box_line_width > 0)
1780 height += 2 * face->box_line_width;
1781 }
1782 }
1783
1784 return height;
1785 }
1786 #endif
1787
1788 return 1;
1789 }
1790
1791 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1792 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1793 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1794 not force the value into range. */
1795
1796 void
1797 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1798 int *x, int *y, NativeRectangle *bounds, int noclip)
1799 {
1800
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1803 {
1804 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1805 even for negative values. */
1806 if (pix_x < 0)
1807 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1808 if (pix_y < 0)
1809 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1810
1811 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1812 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1813
1814 if (bounds)
1815 STORE_NATIVE_RECT (*bounds,
1816 FRAME_COL_TO_PIXEL_X (f, pix_x),
1817 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1818 FRAME_COLUMN_WIDTH (f) - 1,
1819 FRAME_LINE_HEIGHT (f) - 1);
1820
1821 if (!noclip)
1822 {
1823 if (pix_x < 0)
1824 pix_x = 0;
1825 else if (pix_x > FRAME_TOTAL_COLS (f))
1826 pix_x = FRAME_TOTAL_COLS (f);
1827
1828 if (pix_y < 0)
1829 pix_y = 0;
1830 else if (pix_y > FRAME_LINES (f))
1831 pix_y = FRAME_LINES (f);
1832 }
1833 }
1834 #endif
1835
1836 *x = pix_x;
1837 *y = pix_y;
1838 }
1839
1840
1841 /* Find the glyph under window-relative coordinates X/Y in window W.
1842 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1843 strings. Return in *HPOS and *VPOS the row and column number of
1844 the glyph found. Return in *AREA the glyph area containing X.
1845 Value is a pointer to the glyph found or null if X/Y is not on
1846 text, or we can't tell because W's current matrix is not up to
1847 date. */
1848
1849 static
1850 struct glyph *
1851 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1852 int *dx, int *dy, int *area)
1853 {
1854 struct glyph *glyph, *end;
1855 struct glyph_row *row = NULL;
1856 int x0, i;
1857
1858 /* Find row containing Y. Give up if some row is not enabled. */
1859 for (i = 0; i < w->current_matrix->nrows; ++i)
1860 {
1861 row = MATRIX_ROW (w->current_matrix, i);
1862 if (!row->enabled_p)
1863 return NULL;
1864 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1865 break;
1866 }
1867
1868 *vpos = i;
1869 *hpos = 0;
1870
1871 /* Give up if Y is not in the window. */
1872 if (i == w->current_matrix->nrows)
1873 return NULL;
1874
1875 /* Get the glyph area containing X. */
1876 if (w->pseudo_window_p)
1877 {
1878 *area = TEXT_AREA;
1879 x0 = 0;
1880 }
1881 else
1882 {
1883 if (x < window_box_left_offset (w, TEXT_AREA))
1884 {
1885 *area = LEFT_MARGIN_AREA;
1886 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1887 }
1888 else if (x < window_box_right_offset (w, TEXT_AREA))
1889 {
1890 *area = TEXT_AREA;
1891 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1892 }
1893 else
1894 {
1895 *area = RIGHT_MARGIN_AREA;
1896 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1897 }
1898 }
1899
1900 /* Find glyph containing X. */
1901 glyph = row->glyphs[*area];
1902 end = glyph + row->used[*area];
1903 x -= x0;
1904 while (glyph < end && x >= glyph->pixel_width)
1905 {
1906 x -= glyph->pixel_width;
1907 ++glyph;
1908 }
1909
1910 if (glyph == end)
1911 return NULL;
1912
1913 if (dx)
1914 {
1915 *dx = x;
1916 *dy = y - (row->y + row->ascent - glyph->ascent);
1917 }
1918
1919 *hpos = glyph - row->glyphs[*area];
1920 return glyph;
1921 }
1922
1923 /* Convert frame-relative x/y to coordinates relative to window W.
1924 Takes pseudo-windows into account. */
1925
1926 static void
1927 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1928 {
1929 if (w->pseudo_window_p)
1930 {
1931 /* A pseudo-window is always full-width, and starts at the
1932 left edge of the frame, plus a frame border. */
1933 struct frame *f = XFRAME (w->frame);
1934 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1935 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1936 }
1937 else
1938 {
1939 *x -= WINDOW_LEFT_EDGE_X (w);
1940 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1941 }
1942 }
1943
1944 #ifdef HAVE_WINDOW_SYSTEM
1945
1946 /* EXPORT:
1947 Return in RECTS[] at most N clipping rectangles for glyph string S.
1948 Return the number of stored rectangles. */
1949
1950 int
1951 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1952 {
1953 XRectangle r;
1954
1955 if (n <= 0)
1956 return 0;
1957
1958 if (s->row->full_width_p)
1959 {
1960 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1961 r.x = WINDOW_LEFT_EDGE_X (s->w);
1962 r.width = WINDOW_TOTAL_WIDTH (s->w);
1963
1964 /* Unless displaying a mode or menu bar line, which are always
1965 fully visible, clip to the visible part of the row. */
1966 if (s->w->pseudo_window_p)
1967 r.height = s->row->visible_height;
1968 else
1969 r.height = s->height;
1970 }
1971 else
1972 {
1973 /* This is a text line that may be partially visible. */
1974 r.x = window_box_left (s->w, s->area);
1975 r.width = window_box_width (s->w, s->area);
1976 r.height = s->row->visible_height;
1977 }
1978
1979 if (s->clip_head)
1980 if (r.x < s->clip_head->x)
1981 {
1982 if (r.width >= s->clip_head->x - r.x)
1983 r.width -= s->clip_head->x - r.x;
1984 else
1985 r.width = 0;
1986 r.x = s->clip_head->x;
1987 }
1988 if (s->clip_tail)
1989 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1990 {
1991 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1992 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1993 else
1994 r.width = 0;
1995 }
1996
1997 /* If S draws overlapping rows, it's sufficient to use the top and
1998 bottom of the window for clipping because this glyph string
1999 intentionally draws over other lines. */
2000 if (s->for_overlaps)
2001 {
2002 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2003 r.height = window_text_bottom_y (s->w) - r.y;
2004
2005 /* Alas, the above simple strategy does not work for the
2006 environments with anti-aliased text: if the same text is
2007 drawn onto the same place multiple times, it gets thicker.
2008 If the overlap we are processing is for the erased cursor, we
2009 take the intersection with the rectangle of the cursor. */
2010 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2011 {
2012 XRectangle rc, r_save = r;
2013
2014 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2015 rc.y = s->w->phys_cursor.y;
2016 rc.width = s->w->phys_cursor_width;
2017 rc.height = s->w->phys_cursor_height;
2018
2019 x_intersect_rectangles (&r_save, &rc, &r);
2020 }
2021 }
2022 else
2023 {
2024 /* Don't use S->y for clipping because it doesn't take partially
2025 visible lines into account. For example, it can be negative for
2026 partially visible lines at the top of a window. */
2027 if (!s->row->full_width_p
2028 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2029 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2030 else
2031 r.y = max (0, s->row->y);
2032 }
2033
2034 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2035
2036 /* If drawing the cursor, don't let glyph draw outside its
2037 advertised boundaries. Cleartype does this under some circumstances. */
2038 if (s->hl == DRAW_CURSOR)
2039 {
2040 struct glyph *glyph = s->first_glyph;
2041 int height, max_y;
2042
2043 if (s->x > r.x)
2044 {
2045 r.width -= s->x - r.x;
2046 r.x = s->x;
2047 }
2048 r.width = min (r.width, glyph->pixel_width);
2049
2050 /* If r.y is below window bottom, ensure that we still see a cursor. */
2051 height = min (glyph->ascent + glyph->descent,
2052 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2053 max_y = window_text_bottom_y (s->w) - height;
2054 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2055 if (s->ybase - glyph->ascent > max_y)
2056 {
2057 r.y = max_y;
2058 r.height = height;
2059 }
2060 else
2061 {
2062 /* Don't draw cursor glyph taller than our actual glyph. */
2063 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2064 if (height < r.height)
2065 {
2066 max_y = r.y + r.height;
2067 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2068 r.height = min (max_y - r.y, height);
2069 }
2070 }
2071 }
2072
2073 if (s->row->clip)
2074 {
2075 XRectangle r_save = r;
2076
2077 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2078 r.width = 0;
2079 }
2080
2081 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2082 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2083 {
2084 #ifdef CONVERT_FROM_XRECT
2085 CONVERT_FROM_XRECT (r, *rects);
2086 #else
2087 *rects = r;
2088 #endif
2089 return 1;
2090 }
2091 else
2092 {
2093 /* If we are processing overlapping and allowed to return
2094 multiple clipping rectangles, we exclude the row of the glyph
2095 string from the clipping rectangle. This is to avoid drawing
2096 the same text on the environment with anti-aliasing. */
2097 #ifdef CONVERT_FROM_XRECT
2098 XRectangle rs[2];
2099 #else
2100 XRectangle *rs = rects;
2101 #endif
2102 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2103
2104 if (s->for_overlaps & OVERLAPS_PRED)
2105 {
2106 rs[i] = r;
2107 if (r.y + r.height > row_y)
2108 {
2109 if (r.y < row_y)
2110 rs[i].height = row_y - r.y;
2111 else
2112 rs[i].height = 0;
2113 }
2114 i++;
2115 }
2116 if (s->for_overlaps & OVERLAPS_SUCC)
2117 {
2118 rs[i] = r;
2119 if (r.y < row_y + s->row->visible_height)
2120 {
2121 if (r.y + r.height > row_y + s->row->visible_height)
2122 {
2123 rs[i].y = row_y + s->row->visible_height;
2124 rs[i].height = r.y + r.height - rs[i].y;
2125 }
2126 else
2127 rs[i].height = 0;
2128 }
2129 i++;
2130 }
2131
2132 n = i;
2133 #ifdef CONVERT_FROM_XRECT
2134 for (i = 0; i < n; i++)
2135 CONVERT_FROM_XRECT (rs[i], rects[i]);
2136 #endif
2137 return n;
2138 }
2139 }
2140
2141 /* EXPORT:
2142 Return in *NR the clipping rectangle for glyph string S. */
2143
2144 void
2145 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2146 {
2147 get_glyph_string_clip_rects (s, nr, 1);
2148 }
2149
2150
2151 /* EXPORT:
2152 Return the position and height of the phys cursor in window W.
2153 Set w->phys_cursor_width to width of phys cursor.
2154 */
2155
2156 void
2157 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2158 struct glyph *glyph, int *xp, int *yp, int *heightp)
2159 {
2160 struct frame *f = XFRAME (WINDOW_FRAME (w));
2161 int x, y, wd, h, h0, y0;
2162
2163 /* Compute the width of the rectangle to draw. If on a stretch
2164 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2165 rectangle as wide as the glyph, but use a canonical character
2166 width instead. */
2167 wd = glyph->pixel_width - 1;
2168 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2169 wd++; /* Why? */
2170 #endif
2171
2172 x = w->phys_cursor.x;
2173 if (x < 0)
2174 {
2175 wd += x;
2176 x = 0;
2177 }
2178
2179 if (glyph->type == STRETCH_GLYPH
2180 && !x_stretch_cursor_p)
2181 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2182 w->phys_cursor_width = wd;
2183
2184 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2185
2186 /* If y is below window bottom, ensure that we still see a cursor. */
2187 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2188
2189 h = max (h0, glyph->ascent + glyph->descent);
2190 h0 = min (h0, glyph->ascent + glyph->descent);
2191
2192 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2193 if (y < y0)
2194 {
2195 h = max (h - (y0 - y) + 1, h0);
2196 y = y0 - 1;
2197 }
2198 else
2199 {
2200 y0 = window_text_bottom_y (w) - h0;
2201 if (y > y0)
2202 {
2203 h += y - y0;
2204 y = y0;
2205 }
2206 }
2207
2208 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2209 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2210 *heightp = h;
2211 }
2212
2213 /*
2214 * Remember which glyph the mouse is over.
2215 */
2216
2217 void
2218 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2219 {
2220 Lisp_Object window;
2221 struct window *w;
2222 struct glyph_row *r, *gr, *end_row;
2223 enum window_part part;
2224 enum glyph_row_area area;
2225 int x, y, width, height;
2226
2227 /* Try to determine frame pixel position and size of the glyph under
2228 frame pixel coordinates X/Y on frame F. */
2229
2230 if (!f->glyphs_initialized_p
2231 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2232 NILP (window)))
2233 {
2234 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2235 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2236 goto virtual_glyph;
2237 }
2238
2239 w = XWINDOW (window);
2240 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2241 height = WINDOW_FRAME_LINE_HEIGHT (w);
2242
2243 x = window_relative_x_coord (w, part, gx);
2244 y = gy - WINDOW_TOP_EDGE_Y (w);
2245
2246 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2247 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2248
2249 if (w->pseudo_window_p)
2250 {
2251 area = TEXT_AREA;
2252 part = ON_MODE_LINE; /* Don't adjust margin. */
2253 goto text_glyph;
2254 }
2255
2256 switch (part)
2257 {
2258 case ON_LEFT_MARGIN:
2259 area = LEFT_MARGIN_AREA;
2260 goto text_glyph;
2261
2262 case ON_RIGHT_MARGIN:
2263 area = RIGHT_MARGIN_AREA;
2264 goto text_glyph;
2265
2266 case ON_HEADER_LINE:
2267 case ON_MODE_LINE:
2268 gr = (part == ON_HEADER_LINE
2269 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2270 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2271 gy = gr->y;
2272 area = TEXT_AREA;
2273 goto text_glyph_row_found;
2274
2275 case ON_TEXT:
2276 area = TEXT_AREA;
2277
2278 text_glyph:
2279 gr = 0; gy = 0;
2280 for (; r <= end_row && r->enabled_p; ++r)
2281 if (r->y + r->height > y)
2282 {
2283 gr = r; gy = r->y;
2284 break;
2285 }
2286
2287 text_glyph_row_found:
2288 if (gr && gy <= y)
2289 {
2290 struct glyph *g = gr->glyphs[area];
2291 struct glyph *end = g + gr->used[area];
2292
2293 height = gr->height;
2294 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2295 if (gx + g->pixel_width > x)
2296 break;
2297
2298 if (g < end)
2299 {
2300 if (g->type == IMAGE_GLYPH)
2301 {
2302 /* Don't remember when mouse is over image, as
2303 image may have hot-spots. */
2304 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2305 return;
2306 }
2307 width = g->pixel_width;
2308 }
2309 else
2310 {
2311 /* Use nominal char spacing at end of line. */
2312 x -= gx;
2313 gx += (x / width) * width;
2314 }
2315
2316 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2317 gx += window_box_left_offset (w, area);
2318 }
2319 else
2320 {
2321 /* Use nominal line height at end of window. */
2322 gx = (x / width) * width;
2323 y -= gy;
2324 gy += (y / height) * height;
2325 }
2326 break;
2327
2328 case ON_LEFT_FRINGE:
2329 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2330 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2331 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2332 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2333 goto row_glyph;
2334
2335 case ON_RIGHT_FRINGE:
2336 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2338 : window_box_right_offset (w, TEXT_AREA));
2339 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2340 goto row_glyph;
2341
2342 case ON_SCROLL_BAR:
2343 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2344 ? 0
2345 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2346 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2347 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2348 : 0)));
2349 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2350
2351 row_glyph:
2352 gr = 0, gy = 0;
2353 for (; r <= end_row && r->enabled_p; ++r)
2354 if (r->y + r->height > y)
2355 {
2356 gr = r; gy = r->y;
2357 break;
2358 }
2359
2360 if (gr && gy <= y)
2361 height = gr->height;
2362 else
2363 {
2364 /* Use nominal line height at end of window. */
2365 y -= gy;
2366 gy += (y / height) * height;
2367 }
2368 break;
2369
2370 default:
2371 ;
2372 virtual_glyph:
2373 /* If there is no glyph under the mouse, then we divide the screen
2374 into a grid of the smallest glyph in the frame, and use that
2375 as our "glyph". */
2376
2377 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2378 round down even for negative values. */
2379 if (gx < 0)
2380 gx -= width - 1;
2381 if (gy < 0)
2382 gy -= height - 1;
2383
2384 gx = (gx / width) * width;
2385 gy = (gy / height) * height;
2386
2387 goto store_rect;
2388 }
2389
2390 gx += WINDOW_LEFT_EDGE_X (w);
2391 gy += WINDOW_TOP_EDGE_Y (w);
2392
2393 store_rect:
2394 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2395
2396 /* Visible feedback for debugging. */
2397 #if 0
2398 #if HAVE_X_WINDOWS
2399 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2400 f->output_data.x->normal_gc,
2401 gx, gy, width, height);
2402 #endif
2403 #endif
2404 }
2405
2406
2407 #endif /* HAVE_WINDOW_SYSTEM */
2408
2409 \f
2410 /***********************************************************************
2411 Lisp form evaluation
2412 ***********************************************************************/
2413
2414 /* Error handler for safe_eval and safe_call. */
2415
2416 static Lisp_Object
2417 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2418 {
2419 add_to_log ("Error during redisplay: %S signaled %S",
2420 Flist (nargs, args), arg);
2421 return Qnil;
2422 }
2423
2424 /* Call function FUNC with the rest of NARGS - 1 arguments
2425 following. Return the result, or nil if something went
2426 wrong. Prevent redisplay during the evaluation. */
2427
2428 Lisp_Object
2429 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2430 {
2431 Lisp_Object val;
2432
2433 if (inhibit_eval_during_redisplay)
2434 val = Qnil;
2435 else
2436 {
2437 va_list ap;
2438 ptrdiff_t i;
2439 ptrdiff_t count = SPECPDL_INDEX ();
2440 struct gcpro gcpro1;
2441 Lisp_Object *args = alloca (nargs * word_size);
2442
2443 args[0] = func;
2444 va_start (ap, func);
2445 for (i = 1; i < nargs; i++)
2446 args[i] = va_arg (ap, Lisp_Object);
2447 va_end (ap);
2448
2449 GCPRO1 (args[0]);
2450 gcpro1.nvars = nargs;
2451 specbind (Qinhibit_redisplay, Qt);
2452 /* Use Qt to ensure debugger does not run,
2453 so there is no possibility of wanting to redisplay. */
2454 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2455 safe_eval_handler);
2456 UNGCPRO;
2457 val = unbind_to (count, val);
2458 }
2459
2460 return val;
2461 }
2462
2463
2464 /* Call function FN with one argument ARG.
2465 Return the result, or nil if something went wrong. */
2466
2467 Lisp_Object
2468 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2469 {
2470 return safe_call (2, fn, arg);
2471 }
2472
2473 static Lisp_Object Qeval;
2474
2475 Lisp_Object
2476 safe_eval (Lisp_Object sexpr)
2477 {
2478 return safe_call1 (Qeval, sexpr);
2479 }
2480
2481 /* Call function FN with two arguments ARG1 and ARG2.
2482 Return the result, or nil if something went wrong. */
2483
2484 Lisp_Object
2485 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2486 {
2487 return safe_call (3, fn, arg1, arg2);
2488 }
2489
2490
2491 \f
2492 /***********************************************************************
2493 Debugging
2494 ***********************************************************************/
2495
2496 #if 0
2497
2498 /* Define CHECK_IT to perform sanity checks on iterators.
2499 This is for debugging. It is too slow to do unconditionally. */
2500
2501 static void
2502 check_it (struct it *it)
2503 {
2504 if (it->method == GET_FROM_STRING)
2505 {
2506 eassert (STRINGP (it->string));
2507 eassert (IT_STRING_CHARPOS (*it) >= 0);
2508 }
2509 else
2510 {
2511 eassert (IT_STRING_CHARPOS (*it) < 0);
2512 if (it->method == GET_FROM_BUFFER)
2513 {
2514 /* Check that character and byte positions agree. */
2515 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2516 }
2517 }
2518
2519 if (it->dpvec)
2520 eassert (it->current.dpvec_index >= 0);
2521 else
2522 eassert (it->current.dpvec_index < 0);
2523 }
2524
2525 #define CHECK_IT(IT) check_it ((IT))
2526
2527 #else /* not 0 */
2528
2529 #define CHECK_IT(IT) (void) 0
2530
2531 #endif /* not 0 */
2532
2533
2534 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2535
2536 /* Check that the window end of window W is what we expect it
2537 to be---the last row in the current matrix displaying text. */
2538
2539 static void
2540 check_window_end (struct window *w)
2541 {
2542 if (!MINI_WINDOW_P (w)
2543 && !NILP (w->window_end_valid))
2544 {
2545 struct glyph_row *row;
2546 eassert ((row = MATRIX_ROW (w->current_matrix,
2547 XFASTINT (w->window_end_vpos)),
2548 !row->enabled_p
2549 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2550 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2551 }
2552 }
2553
2554 #define CHECK_WINDOW_END(W) check_window_end ((W))
2555
2556 #else
2557
2558 #define CHECK_WINDOW_END(W) (void) 0
2559
2560 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2561
2562
2563 \f
2564 /***********************************************************************
2565 Iterator initialization
2566 ***********************************************************************/
2567
2568 /* Initialize IT for displaying current_buffer in window W, starting
2569 at character position CHARPOS. CHARPOS < 0 means that no buffer
2570 position is specified which is useful when the iterator is assigned
2571 a position later. BYTEPOS is the byte position corresponding to
2572 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2573
2574 If ROW is not null, calls to produce_glyphs with IT as parameter
2575 will produce glyphs in that row.
2576
2577 BASE_FACE_ID is the id of a base face to use. It must be one of
2578 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2579 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2580 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2581
2582 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2583 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2584 will be initialized to use the corresponding mode line glyph row of
2585 the desired matrix of W. */
2586
2587 void
2588 init_iterator (struct it *it, struct window *w,
2589 ptrdiff_t charpos, ptrdiff_t bytepos,
2590 struct glyph_row *row, enum face_id base_face_id)
2591 {
2592 int highlight_region_p;
2593 enum face_id remapped_base_face_id = base_face_id;
2594
2595 /* Some precondition checks. */
2596 eassert (w != NULL && it != NULL);
2597 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2598 && charpos <= ZV));
2599
2600 /* If face attributes have been changed since the last redisplay,
2601 free realized faces now because they depend on face definitions
2602 that might have changed. Don't free faces while there might be
2603 desired matrices pending which reference these faces. */
2604 if (face_change_count && !inhibit_free_realized_faces)
2605 {
2606 face_change_count = 0;
2607 free_all_realized_faces (Qnil);
2608 }
2609
2610 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2611 if (! NILP (Vface_remapping_alist))
2612 remapped_base_face_id
2613 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2614
2615 /* Use one of the mode line rows of W's desired matrix if
2616 appropriate. */
2617 if (row == NULL)
2618 {
2619 if (base_face_id == MODE_LINE_FACE_ID
2620 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2621 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2622 else if (base_face_id == HEADER_LINE_FACE_ID)
2623 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2624 }
2625
2626 /* Clear IT. */
2627 memset (it, 0, sizeof *it);
2628 it->current.overlay_string_index = -1;
2629 it->current.dpvec_index = -1;
2630 it->base_face_id = remapped_base_face_id;
2631 it->string = Qnil;
2632 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2633 it->paragraph_embedding = L2R;
2634 it->bidi_it.string.lstring = Qnil;
2635 it->bidi_it.string.s = NULL;
2636 it->bidi_it.string.bufpos = 0;
2637
2638 /* The window in which we iterate over current_buffer: */
2639 XSETWINDOW (it->window, w);
2640 it->w = w;
2641 it->f = XFRAME (w->frame);
2642
2643 it->cmp_it.id = -1;
2644
2645 /* Extra space between lines (on window systems only). */
2646 if (base_face_id == DEFAULT_FACE_ID
2647 && FRAME_WINDOW_P (it->f))
2648 {
2649 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2650 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2651 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2652 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2653 * FRAME_LINE_HEIGHT (it->f));
2654 else if (it->f->extra_line_spacing > 0)
2655 it->extra_line_spacing = it->f->extra_line_spacing;
2656 it->max_extra_line_spacing = 0;
2657 }
2658
2659 /* If realized faces have been removed, e.g. because of face
2660 attribute changes of named faces, recompute them. When running
2661 in batch mode, the face cache of the initial frame is null. If
2662 we happen to get called, make a dummy face cache. */
2663 if (FRAME_FACE_CACHE (it->f) == NULL)
2664 init_frame_faces (it->f);
2665 if (FRAME_FACE_CACHE (it->f)->used == 0)
2666 recompute_basic_faces (it->f);
2667
2668 /* Current value of the `slice', `space-width', and 'height' properties. */
2669 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2670 it->space_width = Qnil;
2671 it->font_height = Qnil;
2672 it->override_ascent = -1;
2673
2674 /* Are control characters displayed as `^C'? */
2675 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2676
2677 /* -1 means everything between a CR and the following line end
2678 is invisible. >0 means lines indented more than this value are
2679 invisible. */
2680 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2681 ? (clip_to_bounds
2682 (-1, XINT (BVAR (current_buffer, selective_display)),
2683 PTRDIFF_MAX))
2684 : (!NILP (BVAR (current_buffer, selective_display))
2685 ? -1 : 0));
2686 it->selective_display_ellipsis_p
2687 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2688
2689 /* Display table to use. */
2690 it->dp = window_display_table (w);
2691
2692 /* Are multibyte characters enabled in current_buffer? */
2693 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2694
2695 /* Non-zero if we should highlight the region. */
2696 highlight_region_p
2697 = (!NILP (Vtransient_mark_mode)
2698 && !NILP (BVAR (current_buffer, mark_active))
2699 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2700
2701 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2702 start and end of a visible region in window IT->w. Set both to
2703 -1 to indicate no region. */
2704 if (highlight_region_p
2705 /* Maybe highlight only in selected window. */
2706 && (/* Either show region everywhere. */
2707 highlight_nonselected_windows
2708 /* Or show region in the selected window. */
2709 || w == XWINDOW (selected_window)
2710 /* Or show the region if we are in the mini-buffer and W is
2711 the window the mini-buffer refers to. */
2712 || (MINI_WINDOW_P (XWINDOW (selected_window))
2713 && WINDOWP (minibuf_selected_window)
2714 && w == XWINDOW (minibuf_selected_window))))
2715 {
2716 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2717 it->region_beg_charpos = min (PT, markpos);
2718 it->region_end_charpos = max (PT, markpos);
2719 }
2720 else
2721 it->region_beg_charpos = it->region_end_charpos = -1;
2722
2723 /* Get the position at which the redisplay_end_trigger hook should
2724 be run, if it is to be run at all. */
2725 if (MARKERP (w->redisplay_end_trigger)
2726 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2727 it->redisplay_end_trigger_charpos
2728 = marker_position (w->redisplay_end_trigger);
2729 else if (INTEGERP (w->redisplay_end_trigger))
2730 it->redisplay_end_trigger_charpos =
2731 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2732
2733 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2734
2735 /* Are lines in the display truncated? */
2736 if (base_face_id != DEFAULT_FACE_ID
2737 || it->w->hscroll
2738 || (! WINDOW_FULL_WIDTH_P (it->w)
2739 && ((!NILP (Vtruncate_partial_width_windows)
2740 && !INTEGERP (Vtruncate_partial_width_windows))
2741 || (INTEGERP (Vtruncate_partial_width_windows)
2742 && (WINDOW_TOTAL_COLS (it->w)
2743 < XINT (Vtruncate_partial_width_windows))))))
2744 it->line_wrap = TRUNCATE;
2745 else if (NILP (BVAR (current_buffer, truncate_lines)))
2746 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2747 ? WINDOW_WRAP : WORD_WRAP;
2748 else
2749 it->line_wrap = TRUNCATE;
2750
2751 /* Get dimensions of truncation and continuation glyphs. These are
2752 displayed as fringe bitmaps under X, but we need them for such
2753 frames when the fringes are turned off. But leave the dimensions
2754 zero for tooltip frames, as these glyphs look ugly there and also
2755 sabotage calculations of tooltip dimensions in x-show-tip. */
2756 #ifdef HAVE_WINDOW_SYSTEM
2757 if (!(FRAME_WINDOW_P (it->f)
2758 && FRAMEP (tip_frame)
2759 && it->f == XFRAME (tip_frame)))
2760 #endif
2761 {
2762 if (it->line_wrap == TRUNCATE)
2763 {
2764 /* We will need the truncation glyph. */
2765 eassert (it->glyph_row == NULL);
2766 produce_special_glyphs (it, IT_TRUNCATION);
2767 it->truncation_pixel_width = it->pixel_width;
2768 }
2769 else
2770 {
2771 /* We will need the continuation glyph. */
2772 eassert (it->glyph_row == NULL);
2773 produce_special_glyphs (it, IT_CONTINUATION);
2774 it->continuation_pixel_width = it->pixel_width;
2775 }
2776 }
2777
2778 /* Reset these values to zero because the produce_special_glyphs
2779 above has changed them. */
2780 it->pixel_width = it->ascent = it->descent = 0;
2781 it->phys_ascent = it->phys_descent = 0;
2782
2783 /* Set this after getting the dimensions of truncation and
2784 continuation glyphs, so that we don't produce glyphs when calling
2785 produce_special_glyphs, above. */
2786 it->glyph_row = row;
2787 it->area = TEXT_AREA;
2788
2789 /* Forget any previous info about this row being reversed. */
2790 if (it->glyph_row)
2791 it->glyph_row->reversed_p = 0;
2792
2793 /* Get the dimensions of the display area. The display area
2794 consists of the visible window area plus a horizontally scrolled
2795 part to the left of the window. All x-values are relative to the
2796 start of this total display area. */
2797 if (base_face_id != DEFAULT_FACE_ID)
2798 {
2799 /* Mode lines, menu bar in terminal frames. */
2800 it->first_visible_x = 0;
2801 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2802 }
2803 else
2804 {
2805 it->first_visible_x =
2806 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2807 it->last_visible_x = (it->first_visible_x
2808 + window_box_width (w, TEXT_AREA));
2809
2810 /* If we truncate lines, leave room for the truncation glyph(s) at
2811 the right margin. Otherwise, leave room for the continuation
2812 glyph(s). Done only if the window has no fringes. Since we
2813 don't know at this point whether there will be any R2L lines in
2814 the window, we reserve space for truncation/continuation glyphs
2815 even if only one of the fringes is absent. */
2816 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2817 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2818 {
2819 if (it->line_wrap == TRUNCATE)
2820 it->last_visible_x -= it->truncation_pixel_width;
2821 else
2822 it->last_visible_x -= it->continuation_pixel_width;
2823 }
2824
2825 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2826 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2827 }
2828
2829 /* Leave room for a border glyph. */
2830 if (!FRAME_WINDOW_P (it->f)
2831 && !WINDOW_RIGHTMOST_P (it->w))
2832 it->last_visible_x -= 1;
2833
2834 it->last_visible_y = window_text_bottom_y (w);
2835
2836 /* For mode lines and alike, arrange for the first glyph having a
2837 left box line if the face specifies a box. */
2838 if (base_face_id != DEFAULT_FACE_ID)
2839 {
2840 struct face *face;
2841
2842 it->face_id = remapped_base_face_id;
2843
2844 /* If we have a boxed mode line, make the first character appear
2845 with a left box line. */
2846 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2847 if (face->box != FACE_NO_BOX)
2848 it->start_of_box_run_p = 1;
2849 }
2850
2851 /* If a buffer position was specified, set the iterator there,
2852 getting overlays and face properties from that position. */
2853 if (charpos >= BUF_BEG (current_buffer))
2854 {
2855 it->end_charpos = ZV;
2856 IT_CHARPOS (*it) = charpos;
2857
2858 /* We will rely on `reseat' to set this up properly, via
2859 handle_face_prop. */
2860 it->face_id = it->base_face_id;
2861
2862 /* Compute byte position if not specified. */
2863 if (bytepos < charpos)
2864 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2865 else
2866 IT_BYTEPOS (*it) = bytepos;
2867
2868 it->start = it->current;
2869 /* Do we need to reorder bidirectional text? Not if this is a
2870 unibyte buffer: by definition, none of the single-byte
2871 characters are strong R2L, so no reordering is needed. And
2872 bidi.c doesn't support unibyte buffers anyway. Also, don't
2873 reorder while we are loading loadup.el, since the tables of
2874 character properties needed for reordering are not yet
2875 available. */
2876 it->bidi_p =
2877 NILP (Vpurify_flag)
2878 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2879 && it->multibyte_p;
2880
2881 /* If we are to reorder bidirectional text, init the bidi
2882 iterator. */
2883 if (it->bidi_p)
2884 {
2885 /* Note the paragraph direction that this buffer wants to
2886 use. */
2887 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2888 Qleft_to_right))
2889 it->paragraph_embedding = L2R;
2890 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2891 Qright_to_left))
2892 it->paragraph_embedding = R2L;
2893 else
2894 it->paragraph_embedding = NEUTRAL_DIR;
2895 bidi_unshelve_cache (NULL, 0);
2896 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2897 &it->bidi_it);
2898 }
2899
2900 /* Compute faces etc. */
2901 reseat (it, it->current.pos, 1);
2902 }
2903
2904 CHECK_IT (it);
2905 }
2906
2907
2908 /* Initialize IT for the display of window W with window start POS. */
2909
2910 void
2911 start_display (struct it *it, struct window *w, struct text_pos pos)
2912 {
2913 struct glyph_row *row;
2914 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2915
2916 row = w->desired_matrix->rows + first_vpos;
2917 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2918 it->first_vpos = first_vpos;
2919
2920 /* Don't reseat to previous visible line start if current start
2921 position is in a string or image. */
2922 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2923 {
2924 int start_at_line_beg_p;
2925 int first_y = it->current_y;
2926
2927 /* If window start is not at a line start, skip forward to POS to
2928 get the correct continuation lines width. */
2929 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2930 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2931 if (!start_at_line_beg_p)
2932 {
2933 int new_x;
2934
2935 reseat_at_previous_visible_line_start (it);
2936 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2937
2938 new_x = it->current_x + it->pixel_width;
2939
2940 /* If lines are continued, this line may end in the middle
2941 of a multi-glyph character (e.g. a control character
2942 displayed as \003, or in the middle of an overlay
2943 string). In this case move_it_to above will not have
2944 taken us to the start of the continuation line but to the
2945 end of the continued line. */
2946 if (it->current_x > 0
2947 && it->line_wrap != TRUNCATE /* Lines are continued. */
2948 && (/* And glyph doesn't fit on the line. */
2949 new_x > it->last_visible_x
2950 /* Or it fits exactly and we're on a window
2951 system frame. */
2952 || (new_x == it->last_visible_x
2953 && FRAME_WINDOW_P (it->f)
2954 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2955 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2956 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2957 {
2958 if ((it->current.dpvec_index >= 0
2959 || it->current.overlay_string_index >= 0)
2960 /* If we are on a newline from a display vector or
2961 overlay string, then we are already at the end of
2962 a screen line; no need to go to the next line in
2963 that case, as this line is not really continued.
2964 (If we do go to the next line, C-e will not DTRT.) */
2965 && it->c != '\n')
2966 {
2967 set_iterator_to_next (it, 1);
2968 move_it_in_display_line_to (it, -1, -1, 0);
2969 }
2970
2971 it->continuation_lines_width += it->current_x;
2972 }
2973 /* If the character at POS is displayed via a display
2974 vector, move_it_to above stops at the final glyph of
2975 IT->dpvec. To make the caller redisplay that character
2976 again (a.k.a. start at POS), we need to reset the
2977 dpvec_index to the beginning of IT->dpvec. */
2978 else if (it->current.dpvec_index >= 0)
2979 it->current.dpvec_index = 0;
2980
2981 /* We're starting a new display line, not affected by the
2982 height of the continued line, so clear the appropriate
2983 fields in the iterator structure. */
2984 it->max_ascent = it->max_descent = 0;
2985 it->max_phys_ascent = it->max_phys_descent = 0;
2986
2987 it->current_y = first_y;
2988 it->vpos = 0;
2989 it->current_x = it->hpos = 0;
2990 }
2991 }
2992 }
2993
2994
2995 /* Return 1 if POS is a position in ellipses displayed for invisible
2996 text. W is the window we display, for text property lookup. */
2997
2998 static int
2999 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3000 {
3001 Lisp_Object prop, window;
3002 int ellipses_p = 0;
3003 ptrdiff_t charpos = CHARPOS (pos->pos);
3004
3005 /* If POS specifies a position in a display vector, this might
3006 be for an ellipsis displayed for invisible text. We won't
3007 get the iterator set up for delivering that ellipsis unless
3008 we make sure that it gets aware of the invisible text. */
3009 if (pos->dpvec_index >= 0
3010 && pos->overlay_string_index < 0
3011 && CHARPOS (pos->string_pos) < 0
3012 && charpos > BEGV
3013 && (XSETWINDOW (window, w),
3014 prop = Fget_char_property (make_number (charpos),
3015 Qinvisible, window),
3016 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3017 {
3018 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3019 window);
3020 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3021 }
3022
3023 return ellipses_p;
3024 }
3025
3026
3027 /* Initialize IT for stepping through current_buffer in window W,
3028 starting at position POS that includes overlay string and display
3029 vector/ control character translation position information. Value
3030 is zero if there are overlay strings with newlines at POS. */
3031
3032 static int
3033 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3034 {
3035 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3036 int i, overlay_strings_with_newlines = 0;
3037
3038 /* If POS specifies a position in a display vector, this might
3039 be for an ellipsis displayed for invisible text. We won't
3040 get the iterator set up for delivering that ellipsis unless
3041 we make sure that it gets aware of the invisible text. */
3042 if (in_ellipses_for_invisible_text_p (pos, w))
3043 {
3044 --charpos;
3045 bytepos = 0;
3046 }
3047
3048 /* Keep in mind: the call to reseat in init_iterator skips invisible
3049 text, so we might end up at a position different from POS. This
3050 is only a problem when POS is a row start after a newline and an
3051 overlay starts there with an after-string, and the overlay has an
3052 invisible property. Since we don't skip invisible text in
3053 display_line and elsewhere immediately after consuming the
3054 newline before the row start, such a POS will not be in a string,
3055 but the call to init_iterator below will move us to the
3056 after-string. */
3057 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3058
3059 /* This only scans the current chunk -- it should scan all chunks.
3060 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3061 to 16 in 22.1 to make this a lesser problem. */
3062 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3063 {
3064 const char *s = SSDATA (it->overlay_strings[i]);
3065 const char *e = s + SBYTES (it->overlay_strings[i]);
3066
3067 while (s < e && *s != '\n')
3068 ++s;
3069
3070 if (s < e)
3071 {
3072 overlay_strings_with_newlines = 1;
3073 break;
3074 }
3075 }
3076
3077 /* If position is within an overlay string, set up IT to the right
3078 overlay string. */
3079 if (pos->overlay_string_index >= 0)
3080 {
3081 int relative_index;
3082
3083 /* If the first overlay string happens to have a `display'
3084 property for an image, the iterator will be set up for that
3085 image, and we have to undo that setup first before we can
3086 correct the overlay string index. */
3087 if (it->method == GET_FROM_IMAGE)
3088 pop_it (it);
3089
3090 /* We already have the first chunk of overlay strings in
3091 IT->overlay_strings. Load more until the one for
3092 pos->overlay_string_index is in IT->overlay_strings. */
3093 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3094 {
3095 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3096 it->current.overlay_string_index = 0;
3097 while (n--)
3098 {
3099 load_overlay_strings (it, 0);
3100 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3101 }
3102 }
3103
3104 it->current.overlay_string_index = pos->overlay_string_index;
3105 relative_index = (it->current.overlay_string_index
3106 % OVERLAY_STRING_CHUNK_SIZE);
3107 it->string = it->overlay_strings[relative_index];
3108 eassert (STRINGP (it->string));
3109 it->current.string_pos = pos->string_pos;
3110 it->method = GET_FROM_STRING;
3111 it->end_charpos = SCHARS (it->string);
3112 /* Set up the bidi iterator for this overlay string. */
3113 if (it->bidi_p)
3114 {
3115 it->bidi_it.string.lstring = it->string;
3116 it->bidi_it.string.s = NULL;
3117 it->bidi_it.string.schars = SCHARS (it->string);
3118 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3119 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3120 it->bidi_it.string.unibyte = !it->multibyte_p;
3121 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3122 FRAME_WINDOW_P (it->f), &it->bidi_it);
3123
3124 /* Synchronize the state of the bidi iterator with
3125 pos->string_pos. For any string position other than
3126 zero, this will be done automagically when we resume
3127 iteration over the string and get_visually_first_element
3128 is called. But if string_pos is zero, and the string is
3129 to be reordered for display, we need to resync manually,
3130 since it could be that the iteration state recorded in
3131 pos ended at string_pos of 0 moving backwards in string. */
3132 if (CHARPOS (pos->string_pos) == 0)
3133 {
3134 get_visually_first_element (it);
3135 if (IT_STRING_CHARPOS (*it) != 0)
3136 do {
3137 /* Paranoia. */
3138 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3139 bidi_move_to_visually_next (&it->bidi_it);
3140 } while (it->bidi_it.charpos != 0);
3141 }
3142 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3143 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3144 }
3145 }
3146
3147 if (CHARPOS (pos->string_pos) >= 0)
3148 {
3149 /* Recorded position is not in an overlay string, but in another
3150 string. This can only be a string from a `display' property.
3151 IT should already be filled with that string. */
3152 it->current.string_pos = pos->string_pos;
3153 eassert (STRINGP (it->string));
3154 if (it->bidi_p)
3155 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3156 FRAME_WINDOW_P (it->f), &it->bidi_it);
3157 }
3158
3159 /* Restore position in display vector translations, control
3160 character translations or ellipses. */
3161 if (pos->dpvec_index >= 0)
3162 {
3163 if (it->dpvec == NULL)
3164 get_next_display_element (it);
3165 eassert (it->dpvec && it->current.dpvec_index == 0);
3166 it->current.dpvec_index = pos->dpvec_index;
3167 }
3168
3169 CHECK_IT (it);
3170 return !overlay_strings_with_newlines;
3171 }
3172
3173
3174 /* Initialize IT for stepping through current_buffer in window W
3175 starting at ROW->start. */
3176
3177 static void
3178 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3179 {
3180 init_from_display_pos (it, w, &row->start);
3181 it->start = row->start;
3182 it->continuation_lines_width = row->continuation_lines_width;
3183 CHECK_IT (it);
3184 }
3185
3186
3187 /* Initialize IT for stepping through current_buffer in window W
3188 starting in the line following ROW, i.e. starting at ROW->end.
3189 Value is zero if there are overlay strings with newlines at ROW's
3190 end position. */
3191
3192 static int
3193 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3194 {
3195 int success = 0;
3196
3197 if (init_from_display_pos (it, w, &row->end))
3198 {
3199 if (row->continued_p)
3200 it->continuation_lines_width
3201 = row->continuation_lines_width + row->pixel_width;
3202 CHECK_IT (it);
3203 success = 1;
3204 }
3205
3206 return success;
3207 }
3208
3209
3210
3211 \f
3212 /***********************************************************************
3213 Text properties
3214 ***********************************************************************/
3215
3216 /* Called when IT reaches IT->stop_charpos. Handle text property and
3217 overlay changes. Set IT->stop_charpos to the next position where
3218 to stop. */
3219
3220 static void
3221 handle_stop (struct it *it)
3222 {
3223 enum prop_handled handled;
3224 int handle_overlay_change_p;
3225 struct props *p;
3226
3227 it->dpvec = NULL;
3228 it->current.dpvec_index = -1;
3229 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3230 it->ignore_overlay_strings_at_pos_p = 0;
3231 it->ellipsis_p = 0;
3232
3233 /* Use face of preceding text for ellipsis (if invisible) */
3234 if (it->selective_display_ellipsis_p)
3235 it->saved_face_id = it->face_id;
3236
3237 do
3238 {
3239 handled = HANDLED_NORMALLY;
3240
3241 /* Call text property handlers. */
3242 for (p = it_props; p->handler; ++p)
3243 {
3244 handled = p->handler (it);
3245
3246 if (handled == HANDLED_RECOMPUTE_PROPS)
3247 break;
3248 else if (handled == HANDLED_RETURN)
3249 {
3250 /* We still want to show before and after strings from
3251 overlays even if the actual buffer text is replaced. */
3252 if (!handle_overlay_change_p
3253 || it->sp > 1
3254 /* Don't call get_overlay_strings_1 if we already
3255 have overlay strings loaded, because doing so
3256 will load them again and push the iterator state
3257 onto the stack one more time, which is not
3258 expected by the rest of the code that processes
3259 overlay strings. */
3260 || (it->current.overlay_string_index < 0
3261 ? !get_overlay_strings_1 (it, 0, 0)
3262 : 0))
3263 {
3264 if (it->ellipsis_p)
3265 setup_for_ellipsis (it, 0);
3266 /* When handling a display spec, we might load an
3267 empty string. In that case, discard it here. We
3268 used to discard it in handle_single_display_spec,
3269 but that causes get_overlay_strings_1, above, to
3270 ignore overlay strings that we must check. */
3271 if (STRINGP (it->string) && !SCHARS (it->string))
3272 pop_it (it);
3273 return;
3274 }
3275 else if (STRINGP (it->string) && !SCHARS (it->string))
3276 pop_it (it);
3277 else
3278 {
3279 it->ignore_overlay_strings_at_pos_p = 1;
3280 it->string_from_display_prop_p = 0;
3281 it->from_disp_prop_p = 0;
3282 handle_overlay_change_p = 0;
3283 }
3284 handled = HANDLED_RECOMPUTE_PROPS;
3285 break;
3286 }
3287 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3288 handle_overlay_change_p = 0;
3289 }
3290
3291 if (handled != HANDLED_RECOMPUTE_PROPS)
3292 {
3293 /* Don't check for overlay strings below when set to deliver
3294 characters from a display vector. */
3295 if (it->method == GET_FROM_DISPLAY_VECTOR)
3296 handle_overlay_change_p = 0;
3297
3298 /* Handle overlay changes.
3299 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3300 if it finds overlays. */
3301 if (handle_overlay_change_p)
3302 handled = handle_overlay_change (it);
3303 }
3304
3305 if (it->ellipsis_p)
3306 {
3307 setup_for_ellipsis (it, 0);
3308 break;
3309 }
3310 }
3311 while (handled == HANDLED_RECOMPUTE_PROPS);
3312
3313 /* Determine where to stop next. */
3314 if (handled == HANDLED_NORMALLY)
3315 compute_stop_pos (it);
3316 }
3317
3318
3319 /* Compute IT->stop_charpos from text property and overlay change
3320 information for IT's current position. */
3321
3322 static void
3323 compute_stop_pos (struct it *it)
3324 {
3325 register INTERVAL iv, next_iv;
3326 Lisp_Object object, limit, position;
3327 ptrdiff_t charpos, bytepos;
3328
3329 if (STRINGP (it->string))
3330 {
3331 /* Strings are usually short, so don't limit the search for
3332 properties. */
3333 it->stop_charpos = it->end_charpos;
3334 object = it->string;
3335 limit = Qnil;
3336 charpos = IT_STRING_CHARPOS (*it);
3337 bytepos = IT_STRING_BYTEPOS (*it);
3338 }
3339 else
3340 {
3341 ptrdiff_t pos;
3342
3343 /* If end_charpos is out of range for some reason, such as a
3344 misbehaving display function, rationalize it (Bug#5984). */
3345 if (it->end_charpos > ZV)
3346 it->end_charpos = ZV;
3347 it->stop_charpos = it->end_charpos;
3348
3349 /* If next overlay change is in front of the current stop pos
3350 (which is IT->end_charpos), stop there. Note: value of
3351 next_overlay_change is point-max if no overlay change
3352 follows. */
3353 charpos = IT_CHARPOS (*it);
3354 bytepos = IT_BYTEPOS (*it);
3355 pos = next_overlay_change (charpos);
3356 if (pos < it->stop_charpos)
3357 it->stop_charpos = pos;
3358
3359 /* If showing the region, we have to stop at the region
3360 start or end because the face might change there. */
3361 if (it->region_beg_charpos > 0)
3362 {
3363 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3364 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3365 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3366 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3367 }
3368
3369 /* Set up variables for computing the stop position from text
3370 property changes. */
3371 XSETBUFFER (object, current_buffer);
3372 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3373 }
3374
3375 /* Get the interval containing IT's position. Value is a null
3376 interval if there isn't such an interval. */
3377 position = make_number (charpos);
3378 iv = validate_interval_range (object, &position, &position, 0);
3379 if (iv)
3380 {
3381 Lisp_Object values_here[LAST_PROP_IDX];
3382 struct props *p;
3383
3384 /* Get properties here. */
3385 for (p = it_props; p->handler; ++p)
3386 values_here[p->idx] = textget (iv->plist, *p->name);
3387
3388 /* Look for an interval following iv that has different
3389 properties. */
3390 for (next_iv = next_interval (iv);
3391 (next_iv
3392 && (NILP (limit)
3393 || XFASTINT (limit) > next_iv->position));
3394 next_iv = next_interval (next_iv))
3395 {
3396 for (p = it_props; p->handler; ++p)
3397 {
3398 Lisp_Object new_value;
3399
3400 new_value = textget (next_iv->plist, *p->name);
3401 if (!EQ (values_here[p->idx], new_value))
3402 break;
3403 }
3404
3405 if (p->handler)
3406 break;
3407 }
3408
3409 if (next_iv)
3410 {
3411 if (INTEGERP (limit)
3412 && next_iv->position >= XFASTINT (limit))
3413 /* No text property change up to limit. */
3414 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3415 else
3416 /* Text properties change in next_iv. */
3417 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3418 }
3419 }
3420
3421 if (it->cmp_it.id < 0)
3422 {
3423 ptrdiff_t stoppos = it->end_charpos;
3424
3425 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3426 stoppos = -1;
3427 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3428 stoppos, it->string);
3429 }
3430
3431 eassert (STRINGP (it->string)
3432 || (it->stop_charpos >= BEGV
3433 && it->stop_charpos >= IT_CHARPOS (*it)));
3434 }
3435
3436
3437 /* Return the position of the next overlay change after POS in
3438 current_buffer. Value is point-max if no overlay change
3439 follows. This is like `next-overlay-change' but doesn't use
3440 xmalloc. */
3441
3442 static ptrdiff_t
3443 next_overlay_change (ptrdiff_t pos)
3444 {
3445 ptrdiff_t i, noverlays;
3446 ptrdiff_t endpos;
3447 Lisp_Object *overlays;
3448
3449 /* Get all overlays at the given position. */
3450 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3451
3452 /* If any of these overlays ends before endpos,
3453 use its ending point instead. */
3454 for (i = 0; i < noverlays; ++i)
3455 {
3456 Lisp_Object oend;
3457 ptrdiff_t oendpos;
3458
3459 oend = OVERLAY_END (overlays[i]);
3460 oendpos = OVERLAY_POSITION (oend);
3461 endpos = min (endpos, oendpos);
3462 }
3463
3464 return endpos;
3465 }
3466
3467 /* How many characters forward to search for a display property or
3468 display string. Searching too far forward makes the bidi display
3469 sluggish, especially in small windows. */
3470 #define MAX_DISP_SCAN 250
3471
3472 /* Return the character position of a display string at or after
3473 position specified by POSITION. If no display string exists at or
3474 after POSITION, return ZV. A display string is either an overlay
3475 with `display' property whose value is a string, or a `display'
3476 text property whose value is a string. STRING is data about the
3477 string to iterate; if STRING->lstring is nil, we are iterating a
3478 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3479 on a GUI frame. DISP_PROP is set to zero if we searched
3480 MAX_DISP_SCAN characters forward without finding any display
3481 strings, non-zero otherwise. It is set to 2 if the display string
3482 uses any kind of `(space ...)' spec that will produce a stretch of
3483 white space in the text area. */
3484 ptrdiff_t
3485 compute_display_string_pos (struct text_pos *position,
3486 struct bidi_string_data *string,
3487 int frame_window_p, int *disp_prop)
3488 {
3489 /* OBJECT = nil means current buffer. */
3490 Lisp_Object object =
3491 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3492 Lisp_Object pos, spec, limpos;
3493 int string_p = (string && (STRINGP (string->lstring) || string->s));
3494 ptrdiff_t eob = string_p ? string->schars : ZV;
3495 ptrdiff_t begb = string_p ? 0 : BEGV;
3496 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3497 ptrdiff_t lim =
3498 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3499 struct text_pos tpos;
3500 int rv = 0;
3501
3502 *disp_prop = 1;
3503
3504 if (charpos >= eob
3505 /* We don't support display properties whose values are strings
3506 that have display string properties. */
3507 || string->from_disp_str
3508 /* C strings cannot have display properties. */
3509 || (string->s && !STRINGP (object)))
3510 {
3511 *disp_prop = 0;
3512 return eob;
3513 }
3514
3515 /* If the character at CHARPOS is where the display string begins,
3516 return CHARPOS. */
3517 pos = make_number (charpos);
3518 if (STRINGP (object))
3519 bufpos = string->bufpos;
3520 else
3521 bufpos = charpos;
3522 tpos = *position;
3523 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3524 && (charpos <= begb
3525 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3526 object),
3527 spec))
3528 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3529 frame_window_p)))
3530 {
3531 if (rv == 2)
3532 *disp_prop = 2;
3533 return charpos;
3534 }
3535
3536 /* Look forward for the first character with a `display' property
3537 that will replace the underlying text when displayed. */
3538 limpos = make_number (lim);
3539 do {
3540 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3541 CHARPOS (tpos) = XFASTINT (pos);
3542 if (CHARPOS (tpos) >= lim)
3543 {
3544 *disp_prop = 0;
3545 break;
3546 }
3547 if (STRINGP (object))
3548 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3549 else
3550 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3551 spec = Fget_char_property (pos, Qdisplay, object);
3552 if (!STRINGP (object))
3553 bufpos = CHARPOS (tpos);
3554 } while (NILP (spec)
3555 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3556 bufpos, frame_window_p)));
3557 if (rv == 2)
3558 *disp_prop = 2;
3559
3560 return CHARPOS (tpos);
3561 }
3562
3563 /* Return the character position of the end of the display string that
3564 started at CHARPOS. If there's no display string at CHARPOS,
3565 return -1. A display string is either an overlay with `display'
3566 property whose value is a string or a `display' text property whose
3567 value is a string. */
3568 ptrdiff_t
3569 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3570 {
3571 /* OBJECT = nil means current buffer. */
3572 Lisp_Object object =
3573 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3574 Lisp_Object pos = make_number (charpos);
3575 ptrdiff_t eob =
3576 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3577
3578 if (charpos >= eob || (string->s && !STRINGP (object)))
3579 return eob;
3580
3581 /* It could happen that the display property or overlay was removed
3582 since we found it in compute_display_string_pos above. One way
3583 this can happen is if JIT font-lock was called (through
3584 handle_fontified_prop), and jit-lock-functions remove text
3585 properties or overlays from the portion of buffer that includes
3586 CHARPOS. Muse mode is known to do that, for example. In this
3587 case, we return -1 to the caller, to signal that no display
3588 string is actually present at CHARPOS. See bidi_fetch_char for
3589 how this is handled.
3590
3591 An alternative would be to never look for display properties past
3592 it->stop_charpos. But neither compute_display_string_pos nor
3593 bidi_fetch_char that calls it know or care where the next
3594 stop_charpos is. */
3595 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3596 return -1;
3597
3598 /* Look forward for the first character where the `display' property
3599 changes. */
3600 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3601
3602 return XFASTINT (pos);
3603 }
3604
3605
3606 \f
3607 /***********************************************************************
3608 Fontification
3609 ***********************************************************************/
3610
3611 /* Handle changes in the `fontified' property of the current buffer by
3612 calling hook functions from Qfontification_functions to fontify
3613 regions of text. */
3614
3615 static enum prop_handled
3616 handle_fontified_prop (struct it *it)
3617 {
3618 Lisp_Object prop, pos;
3619 enum prop_handled handled = HANDLED_NORMALLY;
3620
3621 if (!NILP (Vmemory_full))
3622 return handled;
3623
3624 /* Get the value of the `fontified' property at IT's current buffer
3625 position. (The `fontified' property doesn't have a special
3626 meaning in strings.) If the value is nil, call functions from
3627 Qfontification_functions. */
3628 if (!STRINGP (it->string)
3629 && it->s == NULL
3630 && !NILP (Vfontification_functions)
3631 && !NILP (Vrun_hooks)
3632 && (pos = make_number (IT_CHARPOS (*it)),
3633 prop = Fget_char_property (pos, Qfontified, Qnil),
3634 /* Ignore the special cased nil value always present at EOB since
3635 no amount of fontifying will be able to change it. */
3636 NILP (prop) && IT_CHARPOS (*it) < Z))
3637 {
3638 ptrdiff_t count = SPECPDL_INDEX ();
3639 Lisp_Object val;
3640 struct buffer *obuf = current_buffer;
3641 int begv = BEGV, zv = ZV;
3642 int old_clip_changed = current_buffer->clip_changed;
3643
3644 val = Vfontification_functions;
3645 specbind (Qfontification_functions, Qnil);
3646
3647 eassert (it->end_charpos == ZV);
3648
3649 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3650 safe_call1 (val, pos);
3651 else
3652 {
3653 Lisp_Object fns, fn;
3654 struct gcpro gcpro1, gcpro2;
3655
3656 fns = Qnil;
3657 GCPRO2 (val, fns);
3658
3659 for (; CONSP (val); val = XCDR (val))
3660 {
3661 fn = XCAR (val);
3662
3663 if (EQ (fn, Qt))
3664 {
3665 /* A value of t indicates this hook has a local
3666 binding; it means to run the global binding too.
3667 In a global value, t should not occur. If it
3668 does, we must ignore it to avoid an endless
3669 loop. */
3670 for (fns = Fdefault_value (Qfontification_functions);
3671 CONSP (fns);
3672 fns = XCDR (fns))
3673 {
3674 fn = XCAR (fns);
3675 if (!EQ (fn, Qt))
3676 safe_call1 (fn, pos);
3677 }
3678 }
3679 else
3680 safe_call1 (fn, pos);
3681 }
3682
3683 UNGCPRO;
3684 }
3685
3686 unbind_to (count, Qnil);
3687
3688 /* Fontification functions routinely call `save-restriction'.
3689 Normally, this tags clip_changed, which can confuse redisplay
3690 (see discussion in Bug#6671). Since we don't perform any
3691 special handling of fontification changes in the case where
3692 `save-restriction' isn't called, there's no point doing so in
3693 this case either. So, if the buffer's restrictions are
3694 actually left unchanged, reset clip_changed. */
3695 if (obuf == current_buffer)
3696 {
3697 if (begv == BEGV && zv == ZV)
3698 current_buffer->clip_changed = old_clip_changed;
3699 }
3700 /* There isn't much we can reasonably do to protect against
3701 misbehaving fontification, but here's a fig leaf. */
3702 else if (BUFFER_LIVE_P (obuf))
3703 set_buffer_internal_1 (obuf);
3704
3705 /* The fontification code may have added/removed text.
3706 It could do even a lot worse, but let's at least protect against
3707 the most obvious case where only the text past `pos' gets changed',
3708 as is/was done in grep.el where some escapes sequences are turned
3709 into face properties (bug#7876). */
3710 it->end_charpos = ZV;
3711
3712 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3713 something. This avoids an endless loop if they failed to
3714 fontify the text for which reason ever. */
3715 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3716 handled = HANDLED_RECOMPUTE_PROPS;
3717 }
3718
3719 return handled;
3720 }
3721
3722
3723 \f
3724 /***********************************************************************
3725 Faces
3726 ***********************************************************************/
3727
3728 /* Set up iterator IT from face properties at its current position.
3729 Called from handle_stop. */
3730
3731 static enum prop_handled
3732 handle_face_prop (struct it *it)
3733 {
3734 int new_face_id;
3735 ptrdiff_t next_stop;
3736
3737 if (!STRINGP (it->string))
3738 {
3739 new_face_id
3740 = face_at_buffer_position (it->w,
3741 IT_CHARPOS (*it),
3742 it->region_beg_charpos,
3743 it->region_end_charpos,
3744 &next_stop,
3745 (IT_CHARPOS (*it)
3746 + TEXT_PROP_DISTANCE_LIMIT),
3747 0, it->base_face_id);
3748
3749 /* Is this a start of a run of characters with box face?
3750 Caveat: this can be called for a freshly initialized
3751 iterator; face_id is -1 in this case. We know that the new
3752 face will not change until limit, i.e. if the new face has a
3753 box, all characters up to limit will have one. But, as
3754 usual, we don't know whether limit is really the end. */
3755 if (new_face_id != it->face_id)
3756 {
3757 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3758 /* If it->face_id is -1, old_face below will be NULL, see
3759 the definition of FACE_FROM_ID. This will happen if this
3760 is the initial call that gets the face. */
3761 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3762
3763 /* If the value of face_id of the iterator is -1, we have to
3764 look in front of IT's position and see whether there is a
3765 face there that's different from new_face_id. */
3766 if (!old_face && IT_CHARPOS (*it) > BEG)
3767 {
3768 int prev_face_id = face_before_it_pos (it);
3769
3770 old_face = FACE_FROM_ID (it->f, prev_face_id);
3771 }
3772
3773 /* If the new face has a box, but the old face does not,
3774 this is the start of a run of characters with box face,
3775 i.e. this character has a shadow on the left side. */
3776 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3777 && (old_face == NULL || !old_face->box));
3778 it->face_box_p = new_face->box != FACE_NO_BOX;
3779 }
3780 }
3781 else
3782 {
3783 int base_face_id;
3784 ptrdiff_t bufpos;
3785 int i;
3786 Lisp_Object from_overlay
3787 = (it->current.overlay_string_index >= 0
3788 ? it->string_overlays[it->current.overlay_string_index
3789 % OVERLAY_STRING_CHUNK_SIZE]
3790 : Qnil);
3791
3792 /* See if we got to this string directly or indirectly from
3793 an overlay property. That includes the before-string or
3794 after-string of an overlay, strings in display properties
3795 provided by an overlay, their text properties, etc.
3796
3797 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3798 if (! NILP (from_overlay))
3799 for (i = it->sp - 1; i >= 0; i--)
3800 {
3801 if (it->stack[i].current.overlay_string_index >= 0)
3802 from_overlay
3803 = it->string_overlays[it->stack[i].current.overlay_string_index
3804 % OVERLAY_STRING_CHUNK_SIZE];
3805 else if (! NILP (it->stack[i].from_overlay))
3806 from_overlay = it->stack[i].from_overlay;
3807
3808 if (!NILP (from_overlay))
3809 break;
3810 }
3811
3812 if (! NILP (from_overlay))
3813 {
3814 bufpos = IT_CHARPOS (*it);
3815 /* For a string from an overlay, the base face depends
3816 only on text properties and ignores overlays. */
3817 base_face_id
3818 = face_for_overlay_string (it->w,
3819 IT_CHARPOS (*it),
3820 it->region_beg_charpos,
3821 it->region_end_charpos,
3822 &next_stop,
3823 (IT_CHARPOS (*it)
3824 + TEXT_PROP_DISTANCE_LIMIT),
3825 0,
3826 from_overlay);
3827 }
3828 else
3829 {
3830 bufpos = 0;
3831
3832 /* For strings from a `display' property, use the face at
3833 IT's current buffer position as the base face to merge
3834 with, so that overlay strings appear in the same face as
3835 surrounding text, unless they specify their own
3836 faces. */
3837 base_face_id = it->string_from_prefix_prop_p
3838 ? DEFAULT_FACE_ID
3839 : underlying_face_id (it);
3840 }
3841
3842 new_face_id = face_at_string_position (it->w,
3843 it->string,
3844 IT_STRING_CHARPOS (*it),
3845 bufpos,
3846 it->region_beg_charpos,
3847 it->region_end_charpos,
3848 &next_stop,
3849 base_face_id, 0);
3850
3851 /* Is this a start of a run of characters with box? Caveat:
3852 this can be called for a freshly allocated iterator; face_id
3853 is -1 is this case. We know that the new face will not
3854 change until the next check pos, i.e. if the new face has a
3855 box, all characters up to that position will have a
3856 box. But, as usual, we don't know whether that position
3857 is really the end. */
3858 if (new_face_id != it->face_id)
3859 {
3860 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3861 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3862
3863 /* If new face has a box but old face hasn't, this is the
3864 start of a run of characters with box, i.e. it has a
3865 shadow on the left side. */
3866 it->start_of_box_run_p
3867 = new_face->box && (old_face == NULL || !old_face->box);
3868 it->face_box_p = new_face->box != FACE_NO_BOX;
3869 }
3870 }
3871
3872 it->face_id = new_face_id;
3873 return HANDLED_NORMALLY;
3874 }
3875
3876
3877 /* Return the ID of the face ``underlying'' IT's current position,
3878 which is in a string. If the iterator is associated with a
3879 buffer, return the face at IT's current buffer position.
3880 Otherwise, use the iterator's base_face_id. */
3881
3882 static int
3883 underlying_face_id (struct it *it)
3884 {
3885 int face_id = it->base_face_id, i;
3886
3887 eassert (STRINGP (it->string));
3888
3889 for (i = it->sp - 1; i >= 0; --i)
3890 if (NILP (it->stack[i].string))
3891 face_id = it->stack[i].face_id;
3892
3893 return face_id;
3894 }
3895
3896
3897 /* Compute the face one character before or after the current position
3898 of IT, in the visual order. BEFORE_P non-zero means get the face
3899 in front (to the left in L2R paragraphs, to the right in R2L
3900 paragraphs) of IT's screen position. Value is the ID of the face. */
3901
3902 static int
3903 face_before_or_after_it_pos (struct it *it, int before_p)
3904 {
3905 int face_id, limit;
3906 ptrdiff_t next_check_charpos;
3907 struct it it_copy;
3908 void *it_copy_data = NULL;
3909
3910 eassert (it->s == NULL);
3911
3912 if (STRINGP (it->string))
3913 {
3914 ptrdiff_t bufpos, charpos;
3915 int base_face_id;
3916
3917 /* No face change past the end of the string (for the case
3918 we are padding with spaces). No face change before the
3919 string start. */
3920 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3921 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3922 return it->face_id;
3923
3924 if (!it->bidi_p)
3925 {
3926 /* Set charpos to the position before or after IT's current
3927 position, in the logical order, which in the non-bidi
3928 case is the same as the visual order. */
3929 if (before_p)
3930 charpos = IT_STRING_CHARPOS (*it) - 1;
3931 else if (it->what == IT_COMPOSITION)
3932 /* For composition, we must check the character after the
3933 composition. */
3934 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3935 else
3936 charpos = IT_STRING_CHARPOS (*it) + 1;
3937 }
3938 else
3939 {
3940 if (before_p)
3941 {
3942 /* With bidi iteration, the character before the current
3943 in the visual order cannot be found by simple
3944 iteration, because "reverse" reordering is not
3945 supported. Instead, we need to use the move_it_*
3946 family of functions. */
3947 /* Ignore face changes before the first visible
3948 character on this display line. */
3949 if (it->current_x <= it->first_visible_x)
3950 return it->face_id;
3951 SAVE_IT (it_copy, *it, it_copy_data);
3952 /* Implementation note: Since move_it_in_display_line
3953 works in the iterator geometry, and thinks the first
3954 character is always the leftmost, even in R2L lines,
3955 we don't need to distinguish between the R2L and L2R
3956 cases here. */
3957 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3958 it_copy.current_x - 1, MOVE_TO_X);
3959 charpos = IT_STRING_CHARPOS (it_copy);
3960 RESTORE_IT (it, it, it_copy_data);
3961 }
3962 else
3963 {
3964 /* Set charpos to the string position of the character
3965 that comes after IT's current position in the visual
3966 order. */
3967 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3968
3969 it_copy = *it;
3970 while (n--)
3971 bidi_move_to_visually_next (&it_copy.bidi_it);
3972
3973 charpos = it_copy.bidi_it.charpos;
3974 }
3975 }
3976 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3977
3978 if (it->current.overlay_string_index >= 0)
3979 bufpos = IT_CHARPOS (*it);
3980 else
3981 bufpos = 0;
3982
3983 base_face_id = underlying_face_id (it);
3984
3985 /* Get the face for ASCII, or unibyte. */
3986 face_id = face_at_string_position (it->w,
3987 it->string,
3988 charpos,
3989 bufpos,
3990 it->region_beg_charpos,
3991 it->region_end_charpos,
3992 &next_check_charpos,
3993 base_face_id, 0);
3994
3995 /* Correct the face for charsets different from ASCII. Do it
3996 for the multibyte case only. The face returned above is
3997 suitable for unibyte text if IT->string is unibyte. */
3998 if (STRING_MULTIBYTE (it->string))
3999 {
4000 struct text_pos pos1 = string_pos (charpos, it->string);
4001 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4002 int c, len;
4003 struct face *face = FACE_FROM_ID (it->f, face_id);
4004
4005 c = string_char_and_length (p, &len);
4006 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4007 }
4008 }
4009 else
4010 {
4011 struct text_pos pos;
4012
4013 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4014 || (IT_CHARPOS (*it) <= BEGV && before_p))
4015 return it->face_id;
4016
4017 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4018 pos = it->current.pos;
4019
4020 if (!it->bidi_p)
4021 {
4022 if (before_p)
4023 DEC_TEXT_POS (pos, it->multibyte_p);
4024 else
4025 {
4026 if (it->what == IT_COMPOSITION)
4027 {
4028 /* For composition, we must check the position after
4029 the composition. */
4030 pos.charpos += it->cmp_it.nchars;
4031 pos.bytepos += it->len;
4032 }
4033 else
4034 INC_TEXT_POS (pos, it->multibyte_p);
4035 }
4036 }
4037 else
4038 {
4039 if (before_p)
4040 {
4041 /* With bidi iteration, the character before the current
4042 in the visual order cannot be found by simple
4043 iteration, because "reverse" reordering is not
4044 supported. Instead, we need to use the move_it_*
4045 family of functions. */
4046 /* Ignore face changes before the first visible
4047 character on this display line. */
4048 if (it->current_x <= it->first_visible_x)
4049 return it->face_id;
4050 SAVE_IT (it_copy, *it, it_copy_data);
4051 /* Implementation note: Since move_it_in_display_line
4052 works in the iterator geometry, and thinks the first
4053 character is always the leftmost, even in R2L lines,
4054 we don't need to distinguish between the R2L and L2R
4055 cases here. */
4056 move_it_in_display_line (&it_copy, ZV,
4057 it_copy.current_x - 1, MOVE_TO_X);
4058 pos = it_copy.current.pos;
4059 RESTORE_IT (it, it, it_copy_data);
4060 }
4061 else
4062 {
4063 /* Set charpos to the buffer position of the character
4064 that comes after IT's current position in the visual
4065 order. */
4066 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4067
4068 it_copy = *it;
4069 while (n--)
4070 bidi_move_to_visually_next (&it_copy.bidi_it);
4071
4072 SET_TEXT_POS (pos,
4073 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4074 }
4075 }
4076 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4077
4078 /* Determine face for CHARSET_ASCII, or unibyte. */
4079 face_id = face_at_buffer_position (it->w,
4080 CHARPOS (pos),
4081 it->region_beg_charpos,
4082 it->region_end_charpos,
4083 &next_check_charpos,
4084 limit, 0, -1);
4085
4086 /* Correct the face for charsets different from ASCII. Do it
4087 for the multibyte case only. The face returned above is
4088 suitable for unibyte text if current_buffer is unibyte. */
4089 if (it->multibyte_p)
4090 {
4091 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4092 struct face *face = FACE_FROM_ID (it->f, face_id);
4093 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4094 }
4095 }
4096
4097 return face_id;
4098 }
4099
4100
4101 \f
4102 /***********************************************************************
4103 Invisible text
4104 ***********************************************************************/
4105
4106 /* Set up iterator IT from invisible properties at its current
4107 position. Called from handle_stop. */
4108
4109 static enum prop_handled
4110 handle_invisible_prop (struct it *it)
4111 {
4112 enum prop_handled handled = HANDLED_NORMALLY;
4113 int invis_p;
4114 Lisp_Object prop;
4115
4116 if (STRINGP (it->string))
4117 {
4118 Lisp_Object end_charpos, limit, charpos;
4119
4120 /* Get the value of the invisible text property at the
4121 current position. Value will be nil if there is no such
4122 property. */
4123 charpos = make_number (IT_STRING_CHARPOS (*it));
4124 prop = Fget_text_property (charpos, Qinvisible, it->string);
4125 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4126
4127 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4128 {
4129 /* Record whether we have to display an ellipsis for the
4130 invisible text. */
4131 int display_ellipsis_p = (invis_p == 2);
4132 ptrdiff_t len, endpos;
4133
4134 handled = HANDLED_RECOMPUTE_PROPS;
4135
4136 /* Get the position at which the next visible text can be
4137 found in IT->string, if any. */
4138 endpos = len = SCHARS (it->string);
4139 XSETINT (limit, len);
4140 do
4141 {
4142 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4143 it->string, limit);
4144 if (INTEGERP (end_charpos))
4145 {
4146 endpos = XFASTINT (end_charpos);
4147 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4148 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4149 if (invis_p == 2)
4150 display_ellipsis_p = 1;
4151 }
4152 }
4153 while (invis_p && endpos < len);
4154
4155 if (display_ellipsis_p)
4156 it->ellipsis_p = 1;
4157
4158 if (endpos < len)
4159 {
4160 /* Text at END_CHARPOS is visible. Move IT there. */
4161 struct text_pos old;
4162 ptrdiff_t oldpos;
4163
4164 old = it->current.string_pos;
4165 oldpos = CHARPOS (old);
4166 if (it->bidi_p)
4167 {
4168 if (it->bidi_it.first_elt
4169 && it->bidi_it.charpos < SCHARS (it->string))
4170 bidi_paragraph_init (it->paragraph_embedding,
4171 &it->bidi_it, 1);
4172 /* Bidi-iterate out of the invisible text. */
4173 do
4174 {
4175 bidi_move_to_visually_next (&it->bidi_it);
4176 }
4177 while (oldpos <= it->bidi_it.charpos
4178 && it->bidi_it.charpos < endpos);
4179
4180 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4181 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4182 if (IT_CHARPOS (*it) >= endpos)
4183 it->prev_stop = endpos;
4184 }
4185 else
4186 {
4187 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4188 compute_string_pos (&it->current.string_pos, old, it->string);
4189 }
4190 }
4191 else
4192 {
4193 /* The rest of the string is invisible. If this is an
4194 overlay string, proceed with the next overlay string
4195 or whatever comes and return a character from there. */
4196 if (it->current.overlay_string_index >= 0
4197 && !display_ellipsis_p)
4198 {
4199 next_overlay_string (it);
4200 /* Don't check for overlay strings when we just
4201 finished processing them. */
4202 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4203 }
4204 else
4205 {
4206 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4207 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4208 }
4209 }
4210 }
4211 }
4212 else
4213 {
4214 ptrdiff_t newpos, next_stop, start_charpos, tem;
4215 Lisp_Object pos, overlay;
4216
4217 /* First of all, is there invisible text at this position? */
4218 tem = start_charpos = IT_CHARPOS (*it);
4219 pos = make_number (tem);
4220 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4221 &overlay);
4222 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4223
4224 /* If we are on invisible text, skip over it. */
4225 if (invis_p && start_charpos < it->end_charpos)
4226 {
4227 /* Record whether we have to display an ellipsis for the
4228 invisible text. */
4229 int display_ellipsis_p = invis_p == 2;
4230
4231 handled = HANDLED_RECOMPUTE_PROPS;
4232
4233 /* Loop skipping over invisible text. The loop is left at
4234 ZV or with IT on the first char being visible again. */
4235 do
4236 {
4237 /* Try to skip some invisible text. Return value is the
4238 position reached which can be equal to where we start
4239 if there is nothing invisible there. This skips both
4240 over invisible text properties and overlays with
4241 invisible property. */
4242 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4243
4244 /* If we skipped nothing at all we weren't at invisible
4245 text in the first place. If everything to the end of
4246 the buffer was skipped, end the loop. */
4247 if (newpos == tem || newpos >= ZV)
4248 invis_p = 0;
4249 else
4250 {
4251 /* We skipped some characters but not necessarily
4252 all there are. Check if we ended up on visible
4253 text. Fget_char_property returns the property of
4254 the char before the given position, i.e. if we
4255 get invis_p = 0, this means that the char at
4256 newpos is visible. */
4257 pos = make_number (newpos);
4258 prop = Fget_char_property (pos, Qinvisible, it->window);
4259 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4260 }
4261
4262 /* If we ended up on invisible text, proceed to
4263 skip starting with next_stop. */
4264 if (invis_p)
4265 tem = next_stop;
4266
4267 /* If there are adjacent invisible texts, don't lose the
4268 second one's ellipsis. */
4269 if (invis_p == 2)
4270 display_ellipsis_p = 1;
4271 }
4272 while (invis_p);
4273
4274 /* The position newpos is now either ZV or on visible text. */
4275 if (it->bidi_p)
4276 {
4277 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4278 int on_newline =
4279 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4280 int after_newline =
4281 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4282
4283 /* If the invisible text ends on a newline or on a
4284 character after a newline, we can avoid the costly,
4285 character by character, bidi iteration to NEWPOS, and
4286 instead simply reseat the iterator there. That's
4287 because all bidi reordering information is tossed at
4288 the newline. This is a big win for modes that hide
4289 complete lines, like Outline, Org, etc. */
4290 if (on_newline || after_newline)
4291 {
4292 struct text_pos tpos;
4293 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4294
4295 SET_TEXT_POS (tpos, newpos, bpos);
4296 reseat_1 (it, tpos, 0);
4297 /* If we reseat on a newline/ZV, we need to prep the
4298 bidi iterator for advancing to the next character
4299 after the newline/EOB, keeping the current paragraph
4300 direction (so that PRODUCE_GLYPHS does TRT wrt
4301 prepending/appending glyphs to a glyph row). */
4302 if (on_newline)
4303 {
4304 it->bidi_it.first_elt = 0;
4305 it->bidi_it.paragraph_dir = pdir;
4306 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4307 it->bidi_it.nchars = 1;
4308 it->bidi_it.ch_len = 1;
4309 }
4310 }
4311 else /* Must use the slow method. */
4312 {
4313 /* With bidi iteration, the region of invisible text
4314 could start and/or end in the middle of a
4315 non-base embedding level. Therefore, we need to
4316 skip invisible text using the bidi iterator,
4317 starting at IT's current position, until we find
4318 ourselves outside of the invisible text.
4319 Skipping invisible text _after_ bidi iteration
4320 avoids affecting the visual order of the
4321 displayed text when invisible properties are
4322 added or removed. */
4323 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4324 {
4325 /* If we were `reseat'ed to a new paragraph,
4326 determine the paragraph base direction. We
4327 need to do it now because
4328 next_element_from_buffer may not have a
4329 chance to do it, if we are going to skip any
4330 text at the beginning, which resets the
4331 FIRST_ELT flag. */
4332 bidi_paragraph_init (it->paragraph_embedding,
4333 &it->bidi_it, 1);
4334 }
4335 do
4336 {
4337 bidi_move_to_visually_next (&it->bidi_it);
4338 }
4339 while (it->stop_charpos <= it->bidi_it.charpos
4340 && it->bidi_it.charpos < newpos);
4341 IT_CHARPOS (*it) = it->bidi_it.charpos;
4342 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4343 /* If we overstepped NEWPOS, record its position in
4344 the iterator, so that we skip invisible text if
4345 later the bidi iteration lands us in the
4346 invisible region again. */
4347 if (IT_CHARPOS (*it) >= newpos)
4348 it->prev_stop = newpos;
4349 }
4350 }
4351 else
4352 {
4353 IT_CHARPOS (*it) = newpos;
4354 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4355 }
4356
4357 /* If there are before-strings at the start of invisible
4358 text, and the text is invisible because of a text
4359 property, arrange to show before-strings because 20.x did
4360 it that way. (If the text is invisible because of an
4361 overlay property instead of a text property, this is
4362 already handled in the overlay code.) */
4363 if (NILP (overlay)
4364 && get_overlay_strings (it, it->stop_charpos))
4365 {
4366 handled = HANDLED_RECOMPUTE_PROPS;
4367 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4368 }
4369 else if (display_ellipsis_p)
4370 {
4371 /* Make sure that the glyphs of the ellipsis will get
4372 correct `charpos' values. If we would not update
4373 it->position here, the glyphs would belong to the
4374 last visible character _before_ the invisible
4375 text, which confuses `set_cursor_from_row'.
4376
4377 We use the last invisible position instead of the
4378 first because this way the cursor is always drawn on
4379 the first "." of the ellipsis, whenever PT is inside
4380 the invisible text. Otherwise the cursor would be
4381 placed _after_ the ellipsis when the point is after the
4382 first invisible character. */
4383 if (!STRINGP (it->object))
4384 {
4385 it->position.charpos = newpos - 1;
4386 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4387 }
4388 it->ellipsis_p = 1;
4389 /* Let the ellipsis display before
4390 considering any properties of the following char.
4391 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4392 handled = HANDLED_RETURN;
4393 }
4394 }
4395 }
4396
4397 return handled;
4398 }
4399
4400
4401 /* Make iterator IT return `...' next.
4402 Replaces LEN characters from buffer. */
4403
4404 static void
4405 setup_for_ellipsis (struct it *it, int len)
4406 {
4407 /* Use the display table definition for `...'. Invalid glyphs
4408 will be handled by the method returning elements from dpvec. */
4409 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4410 {
4411 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4412 it->dpvec = v->contents;
4413 it->dpend = v->contents + v->header.size;
4414 }
4415 else
4416 {
4417 /* Default `...'. */
4418 it->dpvec = default_invis_vector;
4419 it->dpend = default_invis_vector + 3;
4420 }
4421
4422 it->dpvec_char_len = len;
4423 it->current.dpvec_index = 0;
4424 it->dpvec_face_id = -1;
4425
4426 /* Remember the current face id in case glyphs specify faces.
4427 IT's face is restored in set_iterator_to_next.
4428 saved_face_id was set to preceding char's face in handle_stop. */
4429 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4430 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4431
4432 it->method = GET_FROM_DISPLAY_VECTOR;
4433 it->ellipsis_p = 1;
4434 }
4435
4436
4437 \f
4438 /***********************************************************************
4439 'display' property
4440 ***********************************************************************/
4441
4442 /* Set up iterator IT from `display' property at its current position.
4443 Called from handle_stop.
4444 We return HANDLED_RETURN if some part of the display property
4445 overrides the display of the buffer text itself.
4446 Otherwise we return HANDLED_NORMALLY. */
4447
4448 static enum prop_handled
4449 handle_display_prop (struct it *it)
4450 {
4451 Lisp_Object propval, object, overlay;
4452 struct text_pos *position;
4453 ptrdiff_t bufpos;
4454 /* Nonzero if some property replaces the display of the text itself. */
4455 int display_replaced_p = 0;
4456
4457 if (STRINGP (it->string))
4458 {
4459 object = it->string;
4460 position = &it->current.string_pos;
4461 bufpos = CHARPOS (it->current.pos);
4462 }
4463 else
4464 {
4465 XSETWINDOW (object, it->w);
4466 position = &it->current.pos;
4467 bufpos = CHARPOS (*position);
4468 }
4469
4470 /* Reset those iterator values set from display property values. */
4471 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4472 it->space_width = Qnil;
4473 it->font_height = Qnil;
4474 it->voffset = 0;
4475
4476 /* We don't support recursive `display' properties, i.e. string
4477 values that have a string `display' property, that have a string
4478 `display' property etc. */
4479 if (!it->string_from_display_prop_p)
4480 it->area = TEXT_AREA;
4481
4482 propval = get_char_property_and_overlay (make_number (position->charpos),
4483 Qdisplay, object, &overlay);
4484 if (NILP (propval))
4485 return HANDLED_NORMALLY;
4486 /* Now OVERLAY is the overlay that gave us this property, or nil
4487 if it was a text property. */
4488
4489 if (!STRINGP (it->string))
4490 object = it->w->buffer;
4491
4492 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4493 position, bufpos,
4494 FRAME_WINDOW_P (it->f));
4495
4496 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4497 }
4498
4499 /* Subroutine of handle_display_prop. Returns non-zero if the display
4500 specification in SPEC is a replacing specification, i.e. it would
4501 replace the text covered by `display' property with something else,
4502 such as an image or a display string. If SPEC includes any kind or
4503 `(space ...) specification, the value is 2; this is used by
4504 compute_display_string_pos, which see.
4505
4506 See handle_single_display_spec for documentation of arguments.
4507 frame_window_p is non-zero if the window being redisplayed is on a
4508 GUI frame; this argument is used only if IT is NULL, see below.
4509
4510 IT can be NULL, if this is called by the bidi reordering code
4511 through compute_display_string_pos, which see. In that case, this
4512 function only examines SPEC, but does not otherwise "handle" it, in
4513 the sense that it doesn't set up members of IT from the display
4514 spec. */
4515 static int
4516 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4517 Lisp_Object overlay, struct text_pos *position,
4518 ptrdiff_t bufpos, int frame_window_p)
4519 {
4520 int replacing_p = 0;
4521 int rv;
4522
4523 if (CONSP (spec)
4524 /* Simple specifications. */
4525 && !EQ (XCAR (spec), Qimage)
4526 && !EQ (XCAR (spec), Qspace)
4527 && !EQ (XCAR (spec), Qwhen)
4528 && !EQ (XCAR (spec), Qslice)
4529 && !EQ (XCAR (spec), Qspace_width)
4530 && !EQ (XCAR (spec), Qheight)
4531 && !EQ (XCAR (spec), Qraise)
4532 /* Marginal area specifications. */
4533 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4534 && !EQ (XCAR (spec), Qleft_fringe)
4535 && !EQ (XCAR (spec), Qright_fringe)
4536 && !NILP (XCAR (spec)))
4537 {
4538 for (; CONSP (spec); spec = XCDR (spec))
4539 {
4540 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4541 overlay, position, bufpos,
4542 replacing_p, frame_window_p)))
4543 {
4544 replacing_p = rv;
4545 /* If some text in a string is replaced, `position' no
4546 longer points to the position of `object'. */
4547 if (!it || STRINGP (object))
4548 break;
4549 }
4550 }
4551 }
4552 else if (VECTORP (spec))
4553 {
4554 ptrdiff_t i;
4555 for (i = 0; i < ASIZE (spec); ++i)
4556 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4557 overlay, position, bufpos,
4558 replacing_p, frame_window_p)))
4559 {
4560 replacing_p = rv;
4561 /* If some text in a string is replaced, `position' no
4562 longer points to the position of `object'. */
4563 if (!it || STRINGP (object))
4564 break;
4565 }
4566 }
4567 else
4568 {
4569 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4570 position, bufpos, 0,
4571 frame_window_p)))
4572 replacing_p = rv;
4573 }
4574
4575 return replacing_p;
4576 }
4577
4578 /* Value is the position of the end of the `display' property starting
4579 at START_POS in OBJECT. */
4580
4581 static struct text_pos
4582 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4583 {
4584 Lisp_Object end;
4585 struct text_pos end_pos;
4586
4587 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4588 Qdisplay, object, Qnil);
4589 CHARPOS (end_pos) = XFASTINT (end);
4590 if (STRINGP (object))
4591 compute_string_pos (&end_pos, start_pos, it->string);
4592 else
4593 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4594
4595 return end_pos;
4596 }
4597
4598
4599 /* Set up IT from a single `display' property specification SPEC. OBJECT
4600 is the object in which the `display' property was found. *POSITION
4601 is the position in OBJECT at which the `display' property was found.
4602 BUFPOS is the buffer position of OBJECT (different from POSITION if
4603 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4604 previously saw a display specification which already replaced text
4605 display with something else, for example an image; we ignore such
4606 properties after the first one has been processed.
4607
4608 OVERLAY is the overlay this `display' property came from,
4609 or nil if it was a text property.
4610
4611 If SPEC is a `space' or `image' specification, and in some other
4612 cases too, set *POSITION to the position where the `display'
4613 property ends.
4614
4615 If IT is NULL, only examine the property specification in SPEC, but
4616 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4617 is intended to be displayed in a window on a GUI frame.
4618
4619 Value is non-zero if something was found which replaces the display
4620 of buffer or string text. */
4621
4622 static int
4623 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4624 Lisp_Object overlay, struct text_pos *position,
4625 ptrdiff_t bufpos, int display_replaced_p,
4626 int frame_window_p)
4627 {
4628 Lisp_Object form;
4629 Lisp_Object location, value;
4630 struct text_pos start_pos = *position;
4631 int valid_p;
4632
4633 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4634 If the result is non-nil, use VALUE instead of SPEC. */
4635 form = Qt;
4636 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4637 {
4638 spec = XCDR (spec);
4639 if (!CONSP (spec))
4640 return 0;
4641 form = XCAR (spec);
4642 spec = XCDR (spec);
4643 }
4644
4645 if (!NILP (form) && !EQ (form, Qt))
4646 {
4647 ptrdiff_t count = SPECPDL_INDEX ();
4648 struct gcpro gcpro1;
4649
4650 /* Bind `object' to the object having the `display' property, a
4651 buffer or string. Bind `position' to the position in the
4652 object where the property was found, and `buffer-position'
4653 to the current position in the buffer. */
4654
4655 if (NILP (object))
4656 XSETBUFFER (object, current_buffer);
4657 specbind (Qobject, object);
4658 specbind (Qposition, make_number (CHARPOS (*position)));
4659 specbind (Qbuffer_position, make_number (bufpos));
4660 GCPRO1 (form);
4661 form = safe_eval (form);
4662 UNGCPRO;
4663 unbind_to (count, Qnil);
4664 }
4665
4666 if (NILP (form))
4667 return 0;
4668
4669 /* Handle `(height HEIGHT)' specifications. */
4670 if (CONSP (spec)
4671 && EQ (XCAR (spec), Qheight)
4672 && CONSP (XCDR (spec)))
4673 {
4674 if (it)
4675 {
4676 if (!FRAME_WINDOW_P (it->f))
4677 return 0;
4678
4679 it->font_height = XCAR (XCDR (spec));
4680 if (!NILP (it->font_height))
4681 {
4682 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4683 int new_height = -1;
4684
4685 if (CONSP (it->font_height)
4686 && (EQ (XCAR (it->font_height), Qplus)
4687 || EQ (XCAR (it->font_height), Qminus))
4688 && CONSP (XCDR (it->font_height))
4689 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4690 {
4691 /* `(+ N)' or `(- N)' where N is an integer. */
4692 int steps = XINT (XCAR (XCDR (it->font_height)));
4693 if (EQ (XCAR (it->font_height), Qplus))
4694 steps = - steps;
4695 it->face_id = smaller_face (it->f, it->face_id, steps);
4696 }
4697 else if (FUNCTIONP (it->font_height))
4698 {
4699 /* Call function with current height as argument.
4700 Value is the new height. */
4701 Lisp_Object height;
4702 height = safe_call1 (it->font_height,
4703 face->lface[LFACE_HEIGHT_INDEX]);
4704 if (NUMBERP (height))
4705 new_height = XFLOATINT (height);
4706 }
4707 else if (NUMBERP (it->font_height))
4708 {
4709 /* Value is a multiple of the canonical char height. */
4710 struct face *f;
4711
4712 f = FACE_FROM_ID (it->f,
4713 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4714 new_height = (XFLOATINT (it->font_height)
4715 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4716 }
4717 else
4718 {
4719 /* Evaluate IT->font_height with `height' bound to the
4720 current specified height to get the new height. */
4721 ptrdiff_t count = SPECPDL_INDEX ();
4722
4723 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4724 value = safe_eval (it->font_height);
4725 unbind_to (count, Qnil);
4726
4727 if (NUMBERP (value))
4728 new_height = XFLOATINT (value);
4729 }
4730
4731 if (new_height > 0)
4732 it->face_id = face_with_height (it->f, it->face_id, new_height);
4733 }
4734 }
4735
4736 return 0;
4737 }
4738
4739 /* Handle `(space-width WIDTH)'. */
4740 if (CONSP (spec)
4741 && EQ (XCAR (spec), Qspace_width)
4742 && CONSP (XCDR (spec)))
4743 {
4744 if (it)
4745 {
4746 if (!FRAME_WINDOW_P (it->f))
4747 return 0;
4748
4749 value = XCAR (XCDR (spec));
4750 if (NUMBERP (value) && XFLOATINT (value) > 0)
4751 it->space_width = value;
4752 }
4753
4754 return 0;
4755 }
4756
4757 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4758 if (CONSP (spec)
4759 && EQ (XCAR (spec), Qslice))
4760 {
4761 Lisp_Object tem;
4762
4763 if (it)
4764 {
4765 if (!FRAME_WINDOW_P (it->f))
4766 return 0;
4767
4768 if (tem = XCDR (spec), CONSP (tem))
4769 {
4770 it->slice.x = XCAR (tem);
4771 if (tem = XCDR (tem), CONSP (tem))
4772 {
4773 it->slice.y = XCAR (tem);
4774 if (tem = XCDR (tem), CONSP (tem))
4775 {
4776 it->slice.width = XCAR (tem);
4777 if (tem = XCDR (tem), CONSP (tem))
4778 it->slice.height = XCAR (tem);
4779 }
4780 }
4781 }
4782 }
4783
4784 return 0;
4785 }
4786
4787 /* Handle `(raise FACTOR)'. */
4788 if (CONSP (spec)
4789 && EQ (XCAR (spec), Qraise)
4790 && CONSP (XCDR (spec)))
4791 {
4792 if (it)
4793 {
4794 if (!FRAME_WINDOW_P (it->f))
4795 return 0;
4796
4797 #ifdef HAVE_WINDOW_SYSTEM
4798 value = XCAR (XCDR (spec));
4799 if (NUMBERP (value))
4800 {
4801 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4802 it->voffset = - (XFLOATINT (value)
4803 * (FONT_HEIGHT (face->font)));
4804 }
4805 #endif /* HAVE_WINDOW_SYSTEM */
4806 }
4807
4808 return 0;
4809 }
4810
4811 /* Don't handle the other kinds of display specifications
4812 inside a string that we got from a `display' property. */
4813 if (it && it->string_from_display_prop_p)
4814 return 0;
4815
4816 /* Characters having this form of property are not displayed, so
4817 we have to find the end of the property. */
4818 if (it)
4819 {
4820 start_pos = *position;
4821 *position = display_prop_end (it, object, start_pos);
4822 }
4823 value = Qnil;
4824
4825 /* Stop the scan at that end position--we assume that all
4826 text properties change there. */
4827 if (it)
4828 it->stop_charpos = position->charpos;
4829
4830 /* Handle `(left-fringe BITMAP [FACE])'
4831 and `(right-fringe BITMAP [FACE])'. */
4832 if (CONSP (spec)
4833 && (EQ (XCAR (spec), Qleft_fringe)
4834 || EQ (XCAR (spec), Qright_fringe))
4835 && CONSP (XCDR (spec)))
4836 {
4837 int fringe_bitmap;
4838
4839 if (it)
4840 {
4841 if (!FRAME_WINDOW_P (it->f))
4842 /* If we return here, POSITION has been advanced
4843 across the text with this property. */
4844 {
4845 /* Synchronize the bidi iterator with POSITION. This is
4846 needed because we are not going to push the iterator
4847 on behalf of this display property, so there will be
4848 no pop_it call to do this synchronization for us. */
4849 if (it->bidi_p)
4850 {
4851 it->position = *position;
4852 iterate_out_of_display_property (it);
4853 *position = it->position;
4854 }
4855 return 1;
4856 }
4857 }
4858 else if (!frame_window_p)
4859 return 1;
4860
4861 #ifdef HAVE_WINDOW_SYSTEM
4862 value = XCAR (XCDR (spec));
4863 if (!SYMBOLP (value)
4864 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4865 /* If we return here, POSITION has been advanced
4866 across the text with this property. */
4867 {
4868 if (it && it->bidi_p)
4869 {
4870 it->position = *position;
4871 iterate_out_of_display_property (it);
4872 *position = it->position;
4873 }
4874 return 1;
4875 }
4876
4877 if (it)
4878 {
4879 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4880
4881 if (CONSP (XCDR (XCDR (spec))))
4882 {
4883 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4884 int face_id2 = lookup_derived_face (it->f, face_name,
4885 FRINGE_FACE_ID, 0);
4886 if (face_id2 >= 0)
4887 face_id = face_id2;
4888 }
4889
4890 /* Save current settings of IT so that we can restore them
4891 when we are finished with the glyph property value. */
4892 push_it (it, position);
4893
4894 it->area = TEXT_AREA;
4895 it->what = IT_IMAGE;
4896 it->image_id = -1; /* no image */
4897 it->position = start_pos;
4898 it->object = NILP (object) ? it->w->buffer : object;
4899 it->method = GET_FROM_IMAGE;
4900 it->from_overlay = Qnil;
4901 it->face_id = face_id;
4902 it->from_disp_prop_p = 1;
4903
4904 /* Say that we haven't consumed the characters with
4905 `display' property yet. The call to pop_it in
4906 set_iterator_to_next will clean this up. */
4907 *position = start_pos;
4908
4909 if (EQ (XCAR (spec), Qleft_fringe))
4910 {
4911 it->left_user_fringe_bitmap = fringe_bitmap;
4912 it->left_user_fringe_face_id = face_id;
4913 }
4914 else
4915 {
4916 it->right_user_fringe_bitmap = fringe_bitmap;
4917 it->right_user_fringe_face_id = face_id;
4918 }
4919 }
4920 #endif /* HAVE_WINDOW_SYSTEM */
4921 return 1;
4922 }
4923
4924 /* Prepare to handle `((margin left-margin) ...)',
4925 `((margin right-margin) ...)' and `((margin nil) ...)'
4926 prefixes for display specifications. */
4927 location = Qunbound;
4928 if (CONSP (spec) && CONSP (XCAR (spec)))
4929 {
4930 Lisp_Object tem;
4931
4932 value = XCDR (spec);
4933 if (CONSP (value))
4934 value = XCAR (value);
4935
4936 tem = XCAR (spec);
4937 if (EQ (XCAR (tem), Qmargin)
4938 && (tem = XCDR (tem),
4939 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4940 (NILP (tem)
4941 || EQ (tem, Qleft_margin)
4942 || EQ (tem, Qright_margin))))
4943 location = tem;
4944 }
4945
4946 if (EQ (location, Qunbound))
4947 {
4948 location = Qnil;
4949 value = spec;
4950 }
4951
4952 /* After this point, VALUE is the property after any
4953 margin prefix has been stripped. It must be a string,
4954 an image specification, or `(space ...)'.
4955
4956 LOCATION specifies where to display: `left-margin',
4957 `right-margin' or nil. */
4958
4959 valid_p = (STRINGP (value)
4960 #ifdef HAVE_WINDOW_SYSTEM
4961 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4962 && valid_image_p (value))
4963 #endif /* not HAVE_WINDOW_SYSTEM */
4964 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4965
4966 if (valid_p && !display_replaced_p)
4967 {
4968 int retval = 1;
4969
4970 if (!it)
4971 {
4972 /* Callers need to know whether the display spec is any kind
4973 of `(space ...)' spec that is about to affect text-area
4974 display. */
4975 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4976 retval = 2;
4977 return retval;
4978 }
4979
4980 /* Save current settings of IT so that we can restore them
4981 when we are finished with the glyph property value. */
4982 push_it (it, position);
4983 it->from_overlay = overlay;
4984 it->from_disp_prop_p = 1;
4985
4986 if (NILP (location))
4987 it->area = TEXT_AREA;
4988 else if (EQ (location, Qleft_margin))
4989 it->area = LEFT_MARGIN_AREA;
4990 else
4991 it->area = RIGHT_MARGIN_AREA;
4992
4993 if (STRINGP (value))
4994 {
4995 it->string = value;
4996 it->multibyte_p = STRING_MULTIBYTE (it->string);
4997 it->current.overlay_string_index = -1;
4998 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4999 it->end_charpos = it->string_nchars = SCHARS (it->string);
5000 it->method = GET_FROM_STRING;
5001 it->stop_charpos = 0;
5002 it->prev_stop = 0;
5003 it->base_level_stop = 0;
5004 it->string_from_display_prop_p = 1;
5005 /* Say that we haven't consumed the characters with
5006 `display' property yet. The call to pop_it in
5007 set_iterator_to_next will clean this up. */
5008 if (BUFFERP (object))
5009 *position = start_pos;
5010
5011 /* Force paragraph direction to be that of the parent
5012 object. If the parent object's paragraph direction is
5013 not yet determined, default to L2R. */
5014 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5015 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5016 else
5017 it->paragraph_embedding = L2R;
5018
5019 /* Set up the bidi iterator for this display string. */
5020 if (it->bidi_p)
5021 {
5022 it->bidi_it.string.lstring = it->string;
5023 it->bidi_it.string.s = NULL;
5024 it->bidi_it.string.schars = it->end_charpos;
5025 it->bidi_it.string.bufpos = bufpos;
5026 it->bidi_it.string.from_disp_str = 1;
5027 it->bidi_it.string.unibyte = !it->multibyte_p;
5028 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5029 }
5030 }
5031 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5032 {
5033 it->method = GET_FROM_STRETCH;
5034 it->object = value;
5035 *position = it->position = start_pos;
5036 retval = 1 + (it->area == TEXT_AREA);
5037 }
5038 #ifdef HAVE_WINDOW_SYSTEM
5039 else
5040 {
5041 it->what = IT_IMAGE;
5042 it->image_id = lookup_image (it->f, value);
5043 it->position = start_pos;
5044 it->object = NILP (object) ? it->w->buffer : object;
5045 it->method = GET_FROM_IMAGE;
5046
5047 /* Say that we haven't consumed the characters with
5048 `display' property yet. The call to pop_it in
5049 set_iterator_to_next will clean this up. */
5050 *position = start_pos;
5051 }
5052 #endif /* HAVE_WINDOW_SYSTEM */
5053
5054 return retval;
5055 }
5056
5057 /* Invalid property or property not supported. Restore
5058 POSITION to what it was before. */
5059 *position = start_pos;
5060 return 0;
5061 }
5062
5063 /* Check if PROP is a display property value whose text should be
5064 treated as intangible. OVERLAY is the overlay from which PROP
5065 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5066 specify the buffer position covered by PROP. */
5067
5068 int
5069 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5070 ptrdiff_t charpos, ptrdiff_t bytepos)
5071 {
5072 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5073 struct text_pos position;
5074
5075 SET_TEXT_POS (position, charpos, bytepos);
5076 return handle_display_spec (NULL, prop, Qnil, overlay,
5077 &position, charpos, frame_window_p);
5078 }
5079
5080
5081 /* Return 1 if PROP is a display sub-property value containing STRING.
5082
5083 Implementation note: this and the following function are really
5084 special cases of handle_display_spec and
5085 handle_single_display_spec, and should ideally use the same code.
5086 Until they do, these two pairs must be consistent and must be
5087 modified in sync. */
5088
5089 static int
5090 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5091 {
5092 if (EQ (string, prop))
5093 return 1;
5094
5095 /* Skip over `when FORM'. */
5096 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5097 {
5098 prop = XCDR (prop);
5099 if (!CONSP (prop))
5100 return 0;
5101 /* Actually, the condition following `when' should be eval'ed,
5102 like handle_single_display_spec does, and we should return
5103 zero if it evaluates to nil. However, this function is
5104 called only when the buffer was already displayed and some
5105 glyph in the glyph matrix was found to come from a display
5106 string. Therefore, the condition was already evaluated, and
5107 the result was non-nil, otherwise the display string wouldn't
5108 have been displayed and we would have never been called for
5109 this property. Thus, we can skip the evaluation and assume
5110 its result is non-nil. */
5111 prop = XCDR (prop);
5112 }
5113
5114 if (CONSP (prop))
5115 /* Skip over `margin LOCATION'. */
5116 if (EQ (XCAR (prop), Qmargin))
5117 {
5118 prop = XCDR (prop);
5119 if (!CONSP (prop))
5120 return 0;
5121
5122 prop = XCDR (prop);
5123 if (!CONSP (prop))
5124 return 0;
5125 }
5126
5127 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5128 }
5129
5130
5131 /* Return 1 if STRING appears in the `display' property PROP. */
5132
5133 static int
5134 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5135 {
5136 if (CONSP (prop)
5137 && !EQ (XCAR (prop), Qwhen)
5138 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5139 {
5140 /* A list of sub-properties. */
5141 while (CONSP (prop))
5142 {
5143 if (single_display_spec_string_p (XCAR (prop), string))
5144 return 1;
5145 prop = XCDR (prop);
5146 }
5147 }
5148 else if (VECTORP (prop))
5149 {
5150 /* A vector of sub-properties. */
5151 ptrdiff_t i;
5152 for (i = 0; i < ASIZE (prop); ++i)
5153 if (single_display_spec_string_p (AREF (prop, i), string))
5154 return 1;
5155 }
5156 else
5157 return single_display_spec_string_p (prop, string);
5158
5159 return 0;
5160 }
5161
5162 /* Look for STRING in overlays and text properties in the current
5163 buffer, between character positions FROM and TO (excluding TO).
5164 BACK_P non-zero means look back (in this case, TO is supposed to be
5165 less than FROM).
5166 Value is the first character position where STRING was found, or
5167 zero if it wasn't found before hitting TO.
5168
5169 This function may only use code that doesn't eval because it is
5170 called asynchronously from note_mouse_highlight. */
5171
5172 static ptrdiff_t
5173 string_buffer_position_lim (Lisp_Object string,
5174 ptrdiff_t from, ptrdiff_t to, int back_p)
5175 {
5176 Lisp_Object limit, prop, pos;
5177 int found = 0;
5178
5179 pos = make_number (max (from, BEGV));
5180
5181 if (!back_p) /* looking forward */
5182 {
5183 limit = make_number (min (to, ZV));
5184 while (!found && !EQ (pos, limit))
5185 {
5186 prop = Fget_char_property (pos, Qdisplay, Qnil);
5187 if (!NILP (prop) && display_prop_string_p (prop, string))
5188 found = 1;
5189 else
5190 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5191 limit);
5192 }
5193 }
5194 else /* looking back */
5195 {
5196 limit = make_number (max (to, BEGV));
5197 while (!found && !EQ (pos, limit))
5198 {
5199 prop = Fget_char_property (pos, Qdisplay, Qnil);
5200 if (!NILP (prop) && display_prop_string_p (prop, string))
5201 found = 1;
5202 else
5203 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5204 limit);
5205 }
5206 }
5207
5208 return found ? XINT (pos) : 0;
5209 }
5210
5211 /* Determine which buffer position in current buffer STRING comes from.
5212 AROUND_CHARPOS is an approximate position where it could come from.
5213 Value is the buffer position or 0 if it couldn't be determined.
5214
5215 This function is necessary because we don't record buffer positions
5216 in glyphs generated from strings (to keep struct glyph small).
5217 This function may only use code that doesn't eval because it is
5218 called asynchronously from note_mouse_highlight. */
5219
5220 static ptrdiff_t
5221 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5222 {
5223 const int MAX_DISTANCE = 1000;
5224 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5225 around_charpos + MAX_DISTANCE,
5226 0);
5227
5228 if (!found)
5229 found = string_buffer_position_lim (string, around_charpos,
5230 around_charpos - MAX_DISTANCE, 1);
5231 return found;
5232 }
5233
5234
5235 \f
5236 /***********************************************************************
5237 `composition' property
5238 ***********************************************************************/
5239
5240 /* Set up iterator IT from `composition' property at its current
5241 position. Called from handle_stop. */
5242
5243 static enum prop_handled
5244 handle_composition_prop (struct it *it)
5245 {
5246 Lisp_Object prop, string;
5247 ptrdiff_t pos, pos_byte, start, end;
5248
5249 if (STRINGP (it->string))
5250 {
5251 unsigned char *s;
5252
5253 pos = IT_STRING_CHARPOS (*it);
5254 pos_byte = IT_STRING_BYTEPOS (*it);
5255 string = it->string;
5256 s = SDATA (string) + pos_byte;
5257 it->c = STRING_CHAR (s);
5258 }
5259 else
5260 {
5261 pos = IT_CHARPOS (*it);
5262 pos_byte = IT_BYTEPOS (*it);
5263 string = Qnil;
5264 it->c = FETCH_CHAR (pos_byte);
5265 }
5266
5267 /* If there's a valid composition and point is not inside of the
5268 composition (in the case that the composition is from the current
5269 buffer), draw a glyph composed from the composition components. */
5270 if (find_composition (pos, -1, &start, &end, &prop, string)
5271 && COMPOSITION_VALID_P (start, end, prop)
5272 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5273 {
5274 if (start < pos)
5275 /* As we can't handle this situation (perhaps font-lock added
5276 a new composition), we just return here hoping that next
5277 redisplay will detect this composition much earlier. */
5278 return HANDLED_NORMALLY;
5279 if (start != pos)
5280 {
5281 if (STRINGP (it->string))
5282 pos_byte = string_char_to_byte (it->string, start);
5283 else
5284 pos_byte = CHAR_TO_BYTE (start);
5285 }
5286 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5287 prop, string);
5288
5289 if (it->cmp_it.id >= 0)
5290 {
5291 it->cmp_it.ch = -1;
5292 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5293 it->cmp_it.nglyphs = -1;
5294 }
5295 }
5296
5297 return HANDLED_NORMALLY;
5298 }
5299
5300
5301 \f
5302 /***********************************************************************
5303 Overlay strings
5304 ***********************************************************************/
5305
5306 /* The following structure is used to record overlay strings for
5307 later sorting in load_overlay_strings. */
5308
5309 struct overlay_entry
5310 {
5311 Lisp_Object overlay;
5312 Lisp_Object string;
5313 EMACS_INT priority;
5314 int after_string_p;
5315 };
5316
5317
5318 /* Set up iterator IT from overlay strings at its current position.
5319 Called from handle_stop. */
5320
5321 static enum prop_handled
5322 handle_overlay_change (struct it *it)
5323 {
5324 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5325 return HANDLED_RECOMPUTE_PROPS;
5326 else
5327 return HANDLED_NORMALLY;
5328 }
5329
5330
5331 /* Set up the next overlay string for delivery by IT, if there is an
5332 overlay string to deliver. Called by set_iterator_to_next when the
5333 end of the current overlay string is reached. If there are more
5334 overlay strings to display, IT->string and
5335 IT->current.overlay_string_index are set appropriately here.
5336 Otherwise IT->string is set to nil. */
5337
5338 static void
5339 next_overlay_string (struct it *it)
5340 {
5341 ++it->current.overlay_string_index;
5342 if (it->current.overlay_string_index == it->n_overlay_strings)
5343 {
5344 /* No more overlay strings. Restore IT's settings to what
5345 they were before overlay strings were processed, and
5346 continue to deliver from current_buffer. */
5347
5348 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5349 pop_it (it);
5350 eassert (it->sp > 0
5351 || (NILP (it->string)
5352 && it->method == GET_FROM_BUFFER
5353 && it->stop_charpos >= BEGV
5354 && it->stop_charpos <= it->end_charpos));
5355 it->current.overlay_string_index = -1;
5356 it->n_overlay_strings = 0;
5357 it->overlay_strings_charpos = -1;
5358 /* If there's an empty display string on the stack, pop the
5359 stack, to resync the bidi iterator with IT's position. Such
5360 empty strings are pushed onto the stack in
5361 get_overlay_strings_1. */
5362 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5363 pop_it (it);
5364
5365 /* If we're at the end of the buffer, record that we have
5366 processed the overlay strings there already, so that
5367 next_element_from_buffer doesn't try it again. */
5368 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5369 it->overlay_strings_at_end_processed_p = 1;
5370 }
5371 else
5372 {
5373 /* There are more overlay strings to process. If
5374 IT->current.overlay_string_index has advanced to a position
5375 where we must load IT->overlay_strings with more strings, do
5376 it. We must load at the IT->overlay_strings_charpos where
5377 IT->n_overlay_strings was originally computed; when invisible
5378 text is present, this might not be IT_CHARPOS (Bug#7016). */
5379 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5380
5381 if (it->current.overlay_string_index && i == 0)
5382 load_overlay_strings (it, it->overlay_strings_charpos);
5383
5384 /* Initialize IT to deliver display elements from the overlay
5385 string. */
5386 it->string = it->overlay_strings[i];
5387 it->multibyte_p = STRING_MULTIBYTE (it->string);
5388 SET_TEXT_POS (it->current.string_pos, 0, 0);
5389 it->method = GET_FROM_STRING;
5390 it->stop_charpos = 0;
5391 it->end_charpos = SCHARS (it->string);
5392 if (it->cmp_it.stop_pos >= 0)
5393 it->cmp_it.stop_pos = 0;
5394 it->prev_stop = 0;
5395 it->base_level_stop = 0;
5396
5397 /* Set up the bidi iterator for this overlay string. */
5398 if (it->bidi_p)
5399 {
5400 it->bidi_it.string.lstring = it->string;
5401 it->bidi_it.string.s = NULL;
5402 it->bidi_it.string.schars = SCHARS (it->string);
5403 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5404 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5405 it->bidi_it.string.unibyte = !it->multibyte_p;
5406 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5407 }
5408 }
5409
5410 CHECK_IT (it);
5411 }
5412
5413
5414 /* Compare two overlay_entry structures E1 and E2. Used as a
5415 comparison function for qsort in load_overlay_strings. Overlay
5416 strings for the same position are sorted so that
5417
5418 1. All after-strings come in front of before-strings, except
5419 when they come from the same overlay.
5420
5421 2. Within after-strings, strings are sorted so that overlay strings
5422 from overlays with higher priorities come first.
5423
5424 2. Within before-strings, strings are sorted so that overlay
5425 strings from overlays with higher priorities come last.
5426
5427 Value is analogous to strcmp. */
5428
5429
5430 static int
5431 compare_overlay_entries (const void *e1, const void *e2)
5432 {
5433 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5434 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5435 int result;
5436
5437 if (entry1->after_string_p != entry2->after_string_p)
5438 {
5439 /* Let after-strings appear in front of before-strings if
5440 they come from different overlays. */
5441 if (EQ (entry1->overlay, entry2->overlay))
5442 result = entry1->after_string_p ? 1 : -1;
5443 else
5444 result = entry1->after_string_p ? -1 : 1;
5445 }
5446 else if (entry1->priority != entry2->priority)
5447 {
5448 if (entry1->after_string_p)
5449 /* After-strings sorted in order of decreasing priority. */
5450 result = entry2->priority < entry1->priority ? -1 : 1;
5451 else
5452 /* Before-strings sorted in order of increasing priority. */
5453 result = entry1->priority < entry2->priority ? -1 : 1;
5454 }
5455 else
5456 result = 0;
5457
5458 return result;
5459 }
5460
5461
5462 /* Load the vector IT->overlay_strings with overlay strings from IT's
5463 current buffer position, or from CHARPOS if that is > 0. Set
5464 IT->n_overlays to the total number of overlay strings found.
5465
5466 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5467 a time. On entry into load_overlay_strings,
5468 IT->current.overlay_string_index gives the number of overlay
5469 strings that have already been loaded by previous calls to this
5470 function.
5471
5472 IT->add_overlay_start contains an additional overlay start
5473 position to consider for taking overlay strings from, if non-zero.
5474 This position comes into play when the overlay has an `invisible'
5475 property, and both before and after-strings. When we've skipped to
5476 the end of the overlay, because of its `invisible' property, we
5477 nevertheless want its before-string to appear.
5478 IT->add_overlay_start will contain the overlay start position
5479 in this case.
5480
5481 Overlay strings are sorted so that after-string strings come in
5482 front of before-string strings. Within before and after-strings,
5483 strings are sorted by overlay priority. See also function
5484 compare_overlay_entries. */
5485
5486 static void
5487 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5488 {
5489 Lisp_Object overlay, window, str, invisible;
5490 struct Lisp_Overlay *ov;
5491 ptrdiff_t start, end;
5492 ptrdiff_t size = 20;
5493 ptrdiff_t n = 0, i, j;
5494 int invis_p;
5495 struct overlay_entry *entries = alloca (size * sizeof *entries);
5496 USE_SAFE_ALLOCA;
5497
5498 if (charpos <= 0)
5499 charpos = IT_CHARPOS (*it);
5500
5501 /* Append the overlay string STRING of overlay OVERLAY to vector
5502 `entries' which has size `size' and currently contains `n'
5503 elements. AFTER_P non-zero means STRING is an after-string of
5504 OVERLAY. */
5505 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5506 do \
5507 { \
5508 Lisp_Object priority; \
5509 \
5510 if (n == size) \
5511 { \
5512 struct overlay_entry *old = entries; \
5513 SAFE_NALLOCA (entries, 2, size); \
5514 memcpy (entries, old, size * sizeof *entries); \
5515 size *= 2; \
5516 } \
5517 \
5518 entries[n].string = (STRING); \
5519 entries[n].overlay = (OVERLAY); \
5520 priority = Foverlay_get ((OVERLAY), Qpriority); \
5521 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5522 entries[n].after_string_p = (AFTER_P); \
5523 ++n; \
5524 } \
5525 while (0)
5526
5527 /* Process overlay before the overlay center. */
5528 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5529 {
5530 XSETMISC (overlay, ov);
5531 eassert (OVERLAYP (overlay));
5532 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5533 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5534
5535 if (end < charpos)
5536 break;
5537
5538 /* Skip this overlay if it doesn't start or end at IT's current
5539 position. */
5540 if (end != charpos && start != charpos)
5541 continue;
5542
5543 /* Skip this overlay if it doesn't apply to IT->w. */
5544 window = Foverlay_get (overlay, Qwindow);
5545 if (WINDOWP (window) && XWINDOW (window) != it->w)
5546 continue;
5547
5548 /* If the text ``under'' the overlay is invisible, both before-
5549 and after-strings from this overlay are visible; start and
5550 end position are indistinguishable. */
5551 invisible = Foverlay_get (overlay, Qinvisible);
5552 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5553
5554 /* If overlay has a non-empty before-string, record it. */
5555 if ((start == charpos || (end == charpos && invis_p))
5556 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5557 && SCHARS (str))
5558 RECORD_OVERLAY_STRING (overlay, str, 0);
5559
5560 /* If overlay has a non-empty after-string, record it. */
5561 if ((end == charpos || (start == charpos && invis_p))
5562 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5563 && SCHARS (str))
5564 RECORD_OVERLAY_STRING (overlay, str, 1);
5565 }
5566
5567 /* Process overlays after the overlay center. */
5568 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5569 {
5570 XSETMISC (overlay, ov);
5571 eassert (OVERLAYP (overlay));
5572 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5573 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5574
5575 if (start > charpos)
5576 break;
5577
5578 /* Skip this overlay if it doesn't start or end at IT's current
5579 position. */
5580 if (end != charpos && start != charpos)
5581 continue;
5582
5583 /* Skip this overlay if it doesn't apply to IT->w. */
5584 window = Foverlay_get (overlay, Qwindow);
5585 if (WINDOWP (window) && XWINDOW (window) != it->w)
5586 continue;
5587
5588 /* If the text ``under'' the overlay is invisible, it has a zero
5589 dimension, and both before- and after-strings apply. */
5590 invisible = Foverlay_get (overlay, Qinvisible);
5591 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5592
5593 /* If overlay has a non-empty before-string, record it. */
5594 if ((start == charpos || (end == charpos && invis_p))
5595 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5596 && SCHARS (str))
5597 RECORD_OVERLAY_STRING (overlay, str, 0);
5598
5599 /* If overlay has a non-empty after-string, record it. */
5600 if ((end == charpos || (start == charpos && invis_p))
5601 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5602 && SCHARS (str))
5603 RECORD_OVERLAY_STRING (overlay, str, 1);
5604 }
5605
5606 #undef RECORD_OVERLAY_STRING
5607
5608 /* Sort entries. */
5609 if (n > 1)
5610 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5611
5612 /* Record number of overlay strings, and where we computed it. */
5613 it->n_overlay_strings = n;
5614 it->overlay_strings_charpos = charpos;
5615
5616 /* IT->current.overlay_string_index is the number of overlay strings
5617 that have already been consumed by IT. Copy some of the
5618 remaining overlay strings to IT->overlay_strings. */
5619 i = 0;
5620 j = it->current.overlay_string_index;
5621 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5622 {
5623 it->overlay_strings[i] = entries[j].string;
5624 it->string_overlays[i++] = entries[j++].overlay;
5625 }
5626
5627 CHECK_IT (it);
5628 SAFE_FREE ();
5629 }
5630
5631
5632 /* Get the first chunk of overlay strings at IT's current buffer
5633 position, or at CHARPOS if that is > 0. Value is non-zero if at
5634 least one overlay string was found. */
5635
5636 static int
5637 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5638 {
5639 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5640 process. This fills IT->overlay_strings with strings, and sets
5641 IT->n_overlay_strings to the total number of strings to process.
5642 IT->pos.overlay_string_index has to be set temporarily to zero
5643 because load_overlay_strings needs this; it must be set to -1
5644 when no overlay strings are found because a zero value would
5645 indicate a position in the first overlay string. */
5646 it->current.overlay_string_index = 0;
5647 load_overlay_strings (it, charpos);
5648
5649 /* If we found overlay strings, set up IT to deliver display
5650 elements from the first one. Otherwise set up IT to deliver
5651 from current_buffer. */
5652 if (it->n_overlay_strings)
5653 {
5654 /* Make sure we know settings in current_buffer, so that we can
5655 restore meaningful values when we're done with the overlay
5656 strings. */
5657 if (compute_stop_p)
5658 compute_stop_pos (it);
5659 eassert (it->face_id >= 0);
5660
5661 /* Save IT's settings. They are restored after all overlay
5662 strings have been processed. */
5663 eassert (!compute_stop_p || it->sp == 0);
5664
5665 /* When called from handle_stop, there might be an empty display
5666 string loaded. In that case, don't bother saving it. But
5667 don't use this optimization with the bidi iterator, since we
5668 need the corresponding pop_it call to resync the bidi
5669 iterator's position with IT's position, after we are done
5670 with the overlay strings. (The corresponding call to pop_it
5671 in case of an empty display string is in
5672 next_overlay_string.) */
5673 if (!(!it->bidi_p
5674 && STRINGP (it->string) && !SCHARS (it->string)))
5675 push_it (it, NULL);
5676
5677 /* Set up IT to deliver display elements from the first overlay
5678 string. */
5679 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5680 it->string = it->overlay_strings[0];
5681 it->from_overlay = Qnil;
5682 it->stop_charpos = 0;
5683 eassert (STRINGP (it->string));
5684 it->end_charpos = SCHARS (it->string);
5685 it->prev_stop = 0;
5686 it->base_level_stop = 0;
5687 it->multibyte_p = STRING_MULTIBYTE (it->string);
5688 it->method = GET_FROM_STRING;
5689 it->from_disp_prop_p = 0;
5690
5691 /* Force paragraph direction to be that of the parent
5692 buffer. */
5693 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5694 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5695 else
5696 it->paragraph_embedding = L2R;
5697
5698 /* Set up the bidi iterator for this overlay string. */
5699 if (it->bidi_p)
5700 {
5701 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5702
5703 it->bidi_it.string.lstring = it->string;
5704 it->bidi_it.string.s = NULL;
5705 it->bidi_it.string.schars = SCHARS (it->string);
5706 it->bidi_it.string.bufpos = pos;
5707 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5708 it->bidi_it.string.unibyte = !it->multibyte_p;
5709 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5710 }
5711 return 1;
5712 }
5713
5714 it->current.overlay_string_index = -1;
5715 return 0;
5716 }
5717
5718 static int
5719 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5720 {
5721 it->string = Qnil;
5722 it->method = GET_FROM_BUFFER;
5723
5724 (void) get_overlay_strings_1 (it, charpos, 1);
5725
5726 CHECK_IT (it);
5727
5728 /* Value is non-zero if we found at least one overlay string. */
5729 return STRINGP (it->string);
5730 }
5731
5732
5733 \f
5734 /***********************************************************************
5735 Saving and restoring state
5736 ***********************************************************************/
5737
5738 /* Save current settings of IT on IT->stack. Called, for example,
5739 before setting up IT for an overlay string, to be able to restore
5740 IT's settings to what they were after the overlay string has been
5741 processed. If POSITION is non-NULL, it is the position to save on
5742 the stack instead of IT->position. */
5743
5744 static void
5745 push_it (struct it *it, struct text_pos *position)
5746 {
5747 struct iterator_stack_entry *p;
5748
5749 eassert (it->sp < IT_STACK_SIZE);
5750 p = it->stack + it->sp;
5751
5752 p->stop_charpos = it->stop_charpos;
5753 p->prev_stop = it->prev_stop;
5754 p->base_level_stop = it->base_level_stop;
5755 p->cmp_it = it->cmp_it;
5756 eassert (it->face_id >= 0);
5757 p->face_id = it->face_id;
5758 p->string = it->string;
5759 p->method = it->method;
5760 p->from_overlay = it->from_overlay;
5761 switch (p->method)
5762 {
5763 case GET_FROM_IMAGE:
5764 p->u.image.object = it->object;
5765 p->u.image.image_id = it->image_id;
5766 p->u.image.slice = it->slice;
5767 break;
5768 case GET_FROM_STRETCH:
5769 p->u.stretch.object = it->object;
5770 break;
5771 }
5772 p->position = position ? *position : it->position;
5773 p->current = it->current;
5774 p->end_charpos = it->end_charpos;
5775 p->string_nchars = it->string_nchars;
5776 p->area = it->area;
5777 p->multibyte_p = it->multibyte_p;
5778 p->avoid_cursor_p = it->avoid_cursor_p;
5779 p->space_width = it->space_width;
5780 p->font_height = it->font_height;
5781 p->voffset = it->voffset;
5782 p->string_from_display_prop_p = it->string_from_display_prop_p;
5783 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5784 p->display_ellipsis_p = 0;
5785 p->line_wrap = it->line_wrap;
5786 p->bidi_p = it->bidi_p;
5787 p->paragraph_embedding = it->paragraph_embedding;
5788 p->from_disp_prop_p = it->from_disp_prop_p;
5789 ++it->sp;
5790
5791 /* Save the state of the bidi iterator as well. */
5792 if (it->bidi_p)
5793 bidi_push_it (&it->bidi_it);
5794 }
5795
5796 static void
5797 iterate_out_of_display_property (struct it *it)
5798 {
5799 int buffer_p = !STRINGP (it->string);
5800 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5801 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5802
5803 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5804
5805 /* Maybe initialize paragraph direction. If we are at the beginning
5806 of a new paragraph, next_element_from_buffer may not have a
5807 chance to do that. */
5808 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5809 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5810 /* prev_stop can be zero, so check against BEGV as well. */
5811 while (it->bidi_it.charpos >= bob
5812 && it->prev_stop <= it->bidi_it.charpos
5813 && it->bidi_it.charpos < CHARPOS (it->position)
5814 && it->bidi_it.charpos < eob)
5815 bidi_move_to_visually_next (&it->bidi_it);
5816 /* Record the stop_pos we just crossed, for when we cross it
5817 back, maybe. */
5818 if (it->bidi_it.charpos > CHARPOS (it->position))
5819 it->prev_stop = CHARPOS (it->position);
5820 /* If we ended up not where pop_it put us, resync IT's
5821 positional members with the bidi iterator. */
5822 if (it->bidi_it.charpos != CHARPOS (it->position))
5823 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5824 if (buffer_p)
5825 it->current.pos = it->position;
5826 else
5827 it->current.string_pos = it->position;
5828 }
5829
5830 /* Restore IT's settings from IT->stack. Called, for example, when no
5831 more overlay strings must be processed, and we return to delivering
5832 display elements from a buffer, or when the end of a string from a
5833 `display' property is reached and we return to delivering display
5834 elements from an overlay string, or from a buffer. */
5835
5836 static void
5837 pop_it (struct it *it)
5838 {
5839 struct iterator_stack_entry *p;
5840 int from_display_prop = it->from_disp_prop_p;
5841
5842 eassert (it->sp > 0);
5843 --it->sp;
5844 p = it->stack + it->sp;
5845 it->stop_charpos = p->stop_charpos;
5846 it->prev_stop = p->prev_stop;
5847 it->base_level_stop = p->base_level_stop;
5848 it->cmp_it = p->cmp_it;
5849 it->face_id = p->face_id;
5850 it->current = p->current;
5851 it->position = p->position;
5852 it->string = p->string;
5853 it->from_overlay = p->from_overlay;
5854 if (NILP (it->string))
5855 SET_TEXT_POS (it->current.string_pos, -1, -1);
5856 it->method = p->method;
5857 switch (it->method)
5858 {
5859 case GET_FROM_IMAGE:
5860 it->image_id = p->u.image.image_id;
5861 it->object = p->u.image.object;
5862 it->slice = p->u.image.slice;
5863 break;
5864 case GET_FROM_STRETCH:
5865 it->object = p->u.stretch.object;
5866 break;
5867 case GET_FROM_BUFFER:
5868 it->object = it->w->buffer;
5869 break;
5870 case GET_FROM_STRING:
5871 it->object = it->string;
5872 break;
5873 case GET_FROM_DISPLAY_VECTOR:
5874 if (it->s)
5875 it->method = GET_FROM_C_STRING;
5876 else if (STRINGP (it->string))
5877 it->method = GET_FROM_STRING;
5878 else
5879 {
5880 it->method = GET_FROM_BUFFER;
5881 it->object = it->w->buffer;
5882 }
5883 }
5884 it->end_charpos = p->end_charpos;
5885 it->string_nchars = p->string_nchars;
5886 it->area = p->area;
5887 it->multibyte_p = p->multibyte_p;
5888 it->avoid_cursor_p = p->avoid_cursor_p;
5889 it->space_width = p->space_width;
5890 it->font_height = p->font_height;
5891 it->voffset = p->voffset;
5892 it->string_from_display_prop_p = p->string_from_display_prop_p;
5893 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5894 it->line_wrap = p->line_wrap;
5895 it->bidi_p = p->bidi_p;
5896 it->paragraph_embedding = p->paragraph_embedding;
5897 it->from_disp_prop_p = p->from_disp_prop_p;
5898 if (it->bidi_p)
5899 {
5900 bidi_pop_it (&it->bidi_it);
5901 /* Bidi-iterate until we get out of the portion of text, if any,
5902 covered by a `display' text property or by an overlay with
5903 `display' property. (We cannot just jump there, because the
5904 internal coherency of the bidi iterator state can not be
5905 preserved across such jumps.) We also must determine the
5906 paragraph base direction if the overlay we just processed is
5907 at the beginning of a new paragraph. */
5908 if (from_display_prop
5909 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5910 iterate_out_of_display_property (it);
5911
5912 eassert ((BUFFERP (it->object)
5913 && IT_CHARPOS (*it) == it->bidi_it.charpos
5914 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5915 || (STRINGP (it->object)
5916 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5917 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5918 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5919 }
5920 }
5921
5922
5923 \f
5924 /***********************************************************************
5925 Moving over lines
5926 ***********************************************************************/
5927
5928 /* Set IT's current position to the previous line start. */
5929
5930 static void
5931 back_to_previous_line_start (struct it *it)
5932 {
5933 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5934 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5935 }
5936
5937
5938 /* Move IT to the next line start.
5939
5940 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5941 we skipped over part of the text (as opposed to moving the iterator
5942 continuously over the text). Otherwise, don't change the value
5943 of *SKIPPED_P.
5944
5945 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5946 iterator on the newline, if it was found.
5947
5948 Newlines may come from buffer text, overlay strings, or strings
5949 displayed via the `display' property. That's the reason we can't
5950 simply use find_next_newline_no_quit.
5951
5952 Note that this function may not skip over invisible text that is so
5953 because of text properties and immediately follows a newline. If
5954 it would, function reseat_at_next_visible_line_start, when called
5955 from set_iterator_to_next, would effectively make invisible
5956 characters following a newline part of the wrong glyph row, which
5957 leads to wrong cursor motion. */
5958
5959 static int
5960 forward_to_next_line_start (struct it *it, int *skipped_p,
5961 struct bidi_it *bidi_it_prev)
5962 {
5963 ptrdiff_t old_selective;
5964 int newline_found_p, n;
5965 const int MAX_NEWLINE_DISTANCE = 500;
5966
5967 /* If already on a newline, just consume it to avoid unintended
5968 skipping over invisible text below. */
5969 if (it->what == IT_CHARACTER
5970 && it->c == '\n'
5971 && CHARPOS (it->position) == IT_CHARPOS (*it))
5972 {
5973 if (it->bidi_p && bidi_it_prev)
5974 *bidi_it_prev = it->bidi_it;
5975 set_iterator_to_next (it, 0);
5976 it->c = 0;
5977 return 1;
5978 }
5979
5980 /* Don't handle selective display in the following. It's (a)
5981 unnecessary because it's done by the caller, and (b) leads to an
5982 infinite recursion because next_element_from_ellipsis indirectly
5983 calls this function. */
5984 old_selective = it->selective;
5985 it->selective = 0;
5986
5987 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5988 from buffer text. */
5989 for (n = newline_found_p = 0;
5990 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5991 n += STRINGP (it->string) ? 0 : 1)
5992 {
5993 if (!get_next_display_element (it))
5994 return 0;
5995 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5996 if (newline_found_p && it->bidi_p && bidi_it_prev)
5997 *bidi_it_prev = it->bidi_it;
5998 set_iterator_to_next (it, 0);
5999 }
6000
6001 /* If we didn't find a newline near enough, see if we can use a
6002 short-cut. */
6003 if (!newline_found_p)
6004 {
6005 ptrdiff_t start = IT_CHARPOS (*it);
6006 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6007 Lisp_Object pos;
6008
6009 eassert (!STRINGP (it->string));
6010
6011 /* If there isn't any `display' property in sight, and no
6012 overlays, we can just use the position of the newline in
6013 buffer text. */
6014 if (it->stop_charpos >= limit
6015 || ((pos = Fnext_single_property_change (make_number (start),
6016 Qdisplay, Qnil,
6017 make_number (limit)),
6018 NILP (pos))
6019 && next_overlay_change (start) == ZV))
6020 {
6021 if (!it->bidi_p)
6022 {
6023 IT_CHARPOS (*it) = limit;
6024 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6025 }
6026 else
6027 {
6028 struct bidi_it bprev;
6029
6030 /* Help bidi.c avoid expensive searches for display
6031 properties and overlays, by telling it that there are
6032 none up to `limit'. */
6033 if (it->bidi_it.disp_pos < limit)
6034 {
6035 it->bidi_it.disp_pos = limit;
6036 it->bidi_it.disp_prop = 0;
6037 }
6038 do {
6039 bprev = it->bidi_it;
6040 bidi_move_to_visually_next (&it->bidi_it);
6041 } while (it->bidi_it.charpos != limit);
6042 IT_CHARPOS (*it) = limit;
6043 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6044 if (bidi_it_prev)
6045 *bidi_it_prev = bprev;
6046 }
6047 *skipped_p = newline_found_p = 1;
6048 }
6049 else
6050 {
6051 while (get_next_display_element (it)
6052 && !newline_found_p)
6053 {
6054 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6055 if (newline_found_p && it->bidi_p && bidi_it_prev)
6056 *bidi_it_prev = it->bidi_it;
6057 set_iterator_to_next (it, 0);
6058 }
6059 }
6060 }
6061
6062 it->selective = old_selective;
6063 return newline_found_p;
6064 }
6065
6066
6067 /* Set IT's current position to the previous visible line start. Skip
6068 invisible text that is so either due to text properties or due to
6069 selective display. Caution: this does not change IT->current_x and
6070 IT->hpos. */
6071
6072 static void
6073 back_to_previous_visible_line_start (struct it *it)
6074 {
6075 while (IT_CHARPOS (*it) > BEGV)
6076 {
6077 back_to_previous_line_start (it);
6078
6079 if (IT_CHARPOS (*it) <= BEGV)
6080 break;
6081
6082 /* If selective > 0, then lines indented more than its value are
6083 invisible. */
6084 if (it->selective > 0
6085 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6086 it->selective))
6087 continue;
6088
6089 /* Check the newline before point for invisibility. */
6090 {
6091 Lisp_Object prop;
6092 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6093 Qinvisible, it->window);
6094 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6095 continue;
6096 }
6097
6098 if (IT_CHARPOS (*it) <= BEGV)
6099 break;
6100
6101 {
6102 struct it it2;
6103 void *it2data = NULL;
6104 ptrdiff_t pos;
6105 ptrdiff_t beg, end;
6106 Lisp_Object val, overlay;
6107
6108 SAVE_IT (it2, *it, it2data);
6109
6110 /* If newline is part of a composition, continue from start of composition */
6111 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6112 && beg < IT_CHARPOS (*it))
6113 goto replaced;
6114
6115 /* If newline is replaced by a display property, find start of overlay
6116 or interval and continue search from that point. */
6117 pos = --IT_CHARPOS (it2);
6118 --IT_BYTEPOS (it2);
6119 it2.sp = 0;
6120 bidi_unshelve_cache (NULL, 0);
6121 it2.string_from_display_prop_p = 0;
6122 it2.from_disp_prop_p = 0;
6123 if (handle_display_prop (&it2) == HANDLED_RETURN
6124 && !NILP (val = get_char_property_and_overlay
6125 (make_number (pos), Qdisplay, Qnil, &overlay))
6126 && (OVERLAYP (overlay)
6127 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6128 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6129 {
6130 RESTORE_IT (it, it, it2data);
6131 goto replaced;
6132 }
6133
6134 /* Newline is not replaced by anything -- so we are done. */
6135 RESTORE_IT (it, it, it2data);
6136 break;
6137
6138 replaced:
6139 if (beg < BEGV)
6140 beg = BEGV;
6141 IT_CHARPOS (*it) = beg;
6142 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6143 }
6144 }
6145
6146 it->continuation_lines_width = 0;
6147
6148 eassert (IT_CHARPOS (*it) >= BEGV);
6149 eassert (IT_CHARPOS (*it) == BEGV
6150 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6151 CHECK_IT (it);
6152 }
6153
6154
6155 /* Reseat iterator IT at the previous visible line start. Skip
6156 invisible text that is so either due to text properties or due to
6157 selective display. At the end, update IT's overlay information,
6158 face information etc. */
6159
6160 void
6161 reseat_at_previous_visible_line_start (struct it *it)
6162 {
6163 back_to_previous_visible_line_start (it);
6164 reseat (it, it->current.pos, 1);
6165 CHECK_IT (it);
6166 }
6167
6168
6169 /* Reseat iterator IT on the next visible line start in the current
6170 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6171 preceding the line start. Skip over invisible text that is so
6172 because of selective display. Compute faces, overlays etc at the
6173 new position. Note that this function does not skip over text that
6174 is invisible because of text properties. */
6175
6176 static void
6177 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6178 {
6179 int newline_found_p, skipped_p = 0;
6180 struct bidi_it bidi_it_prev;
6181
6182 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6183
6184 /* Skip over lines that are invisible because they are indented
6185 more than the value of IT->selective. */
6186 if (it->selective > 0)
6187 while (IT_CHARPOS (*it) < ZV
6188 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6189 it->selective))
6190 {
6191 eassert (IT_BYTEPOS (*it) == BEGV
6192 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6193 newline_found_p =
6194 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6195 }
6196
6197 /* Position on the newline if that's what's requested. */
6198 if (on_newline_p && newline_found_p)
6199 {
6200 if (STRINGP (it->string))
6201 {
6202 if (IT_STRING_CHARPOS (*it) > 0)
6203 {
6204 if (!it->bidi_p)
6205 {
6206 --IT_STRING_CHARPOS (*it);
6207 --IT_STRING_BYTEPOS (*it);
6208 }
6209 else
6210 {
6211 /* We need to restore the bidi iterator to the state
6212 it had on the newline, and resync the IT's
6213 position with that. */
6214 it->bidi_it = bidi_it_prev;
6215 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6216 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6217 }
6218 }
6219 }
6220 else if (IT_CHARPOS (*it) > BEGV)
6221 {
6222 if (!it->bidi_p)
6223 {
6224 --IT_CHARPOS (*it);
6225 --IT_BYTEPOS (*it);
6226 }
6227 else
6228 {
6229 /* We need to restore the bidi iterator to the state it
6230 had on the newline and resync IT with that. */
6231 it->bidi_it = bidi_it_prev;
6232 IT_CHARPOS (*it) = it->bidi_it.charpos;
6233 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6234 }
6235 reseat (it, it->current.pos, 0);
6236 }
6237 }
6238 else if (skipped_p)
6239 reseat (it, it->current.pos, 0);
6240
6241 CHECK_IT (it);
6242 }
6243
6244
6245 \f
6246 /***********************************************************************
6247 Changing an iterator's position
6248 ***********************************************************************/
6249
6250 /* Change IT's current position to POS in current_buffer. If FORCE_P
6251 is non-zero, always check for text properties at the new position.
6252 Otherwise, text properties are only looked up if POS >=
6253 IT->check_charpos of a property. */
6254
6255 static void
6256 reseat (struct it *it, struct text_pos pos, int force_p)
6257 {
6258 ptrdiff_t original_pos = IT_CHARPOS (*it);
6259
6260 reseat_1 (it, pos, 0);
6261
6262 /* Determine where to check text properties. Avoid doing it
6263 where possible because text property lookup is very expensive. */
6264 if (force_p
6265 || CHARPOS (pos) > it->stop_charpos
6266 || CHARPOS (pos) < original_pos)
6267 {
6268 if (it->bidi_p)
6269 {
6270 /* For bidi iteration, we need to prime prev_stop and
6271 base_level_stop with our best estimations. */
6272 /* Implementation note: Of course, POS is not necessarily a
6273 stop position, so assigning prev_pos to it is a lie; we
6274 should have called compute_stop_backwards. However, if
6275 the current buffer does not include any R2L characters,
6276 that call would be a waste of cycles, because the
6277 iterator will never move back, and thus never cross this
6278 "fake" stop position. So we delay that backward search
6279 until the time we really need it, in next_element_from_buffer. */
6280 if (CHARPOS (pos) != it->prev_stop)
6281 it->prev_stop = CHARPOS (pos);
6282 if (CHARPOS (pos) < it->base_level_stop)
6283 it->base_level_stop = 0; /* meaning it's unknown */
6284 handle_stop (it);
6285 }
6286 else
6287 {
6288 handle_stop (it);
6289 it->prev_stop = it->base_level_stop = 0;
6290 }
6291
6292 }
6293
6294 CHECK_IT (it);
6295 }
6296
6297
6298 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6299 IT->stop_pos to POS, also. */
6300
6301 static void
6302 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6303 {
6304 /* Don't call this function when scanning a C string. */
6305 eassert (it->s == NULL);
6306
6307 /* POS must be a reasonable value. */
6308 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6309
6310 it->current.pos = it->position = pos;
6311 it->end_charpos = ZV;
6312 it->dpvec = NULL;
6313 it->current.dpvec_index = -1;
6314 it->current.overlay_string_index = -1;
6315 IT_STRING_CHARPOS (*it) = -1;
6316 IT_STRING_BYTEPOS (*it) = -1;
6317 it->string = Qnil;
6318 it->method = GET_FROM_BUFFER;
6319 it->object = it->w->buffer;
6320 it->area = TEXT_AREA;
6321 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6322 it->sp = 0;
6323 it->string_from_display_prop_p = 0;
6324 it->string_from_prefix_prop_p = 0;
6325
6326 it->from_disp_prop_p = 0;
6327 it->face_before_selective_p = 0;
6328 if (it->bidi_p)
6329 {
6330 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6331 &it->bidi_it);
6332 bidi_unshelve_cache (NULL, 0);
6333 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6334 it->bidi_it.string.s = NULL;
6335 it->bidi_it.string.lstring = Qnil;
6336 it->bidi_it.string.bufpos = 0;
6337 it->bidi_it.string.unibyte = 0;
6338 }
6339
6340 if (set_stop_p)
6341 {
6342 it->stop_charpos = CHARPOS (pos);
6343 it->base_level_stop = CHARPOS (pos);
6344 }
6345 /* This make the information stored in it->cmp_it invalidate. */
6346 it->cmp_it.id = -1;
6347 }
6348
6349
6350 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6351 If S is non-null, it is a C string to iterate over. Otherwise,
6352 STRING gives a Lisp string to iterate over.
6353
6354 If PRECISION > 0, don't return more then PRECISION number of
6355 characters from the string.
6356
6357 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6358 characters have been returned. FIELD_WIDTH < 0 means an infinite
6359 field width.
6360
6361 MULTIBYTE = 0 means disable processing of multibyte characters,
6362 MULTIBYTE > 0 means enable it,
6363 MULTIBYTE < 0 means use IT->multibyte_p.
6364
6365 IT must be initialized via a prior call to init_iterator before
6366 calling this function. */
6367
6368 static void
6369 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6370 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6371 int multibyte)
6372 {
6373 /* No region in strings. */
6374 it->region_beg_charpos = it->region_end_charpos = -1;
6375
6376 /* No text property checks performed by default, but see below. */
6377 it->stop_charpos = -1;
6378
6379 /* Set iterator position and end position. */
6380 memset (&it->current, 0, sizeof it->current);
6381 it->current.overlay_string_index = -1;
6382 it->current.dpvec_index = -1;
6383 eassert (charpos >= 0);
6384
6385 /* If STRING is specified, use its multibyteness, otherwise use the
6386 setting of MULTIBYTE, if specified. */
6387 if (multibyte >= 0)
6388 it->multibyte_p = multibyte > 0;
6389
6390 /* Bidirectional reordering of strings is controlled by the default
6391 value of bidi-display-reordering. Don't try to reorder while
6392 loading loadup.el, as the necessary character property tables are
6393 not yet available. */
6394 it->bidi_p =
6395 NILP (Vpurify_flag)
6396 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6397
6398 if (s == NULL)
6399 {
6400 eassert (STRINGP (string));
6401 it->string = string;
6402 it->s = NULL;
6403 it->end_charpos = it->string_nchars = SCHARS (string);
6404 it->method = GET_FROM_STRING;
6405 it->current.string_pos = string_pos (charpos, string);
6406
6407 if (it->bidi_p)
6408 {
6409 it->bidi_it.string.lstring = string;
6410 it->bidi_it.string.s = NULL;
6411 it->bidi_it.string.schars = it->end_charpos;
6412 it->bidi_it.string.bufpos = 0;
6413 it->bidi_it.string.from_disp_str = 0;
6414 it->bidi_it.string.unibyte = !it->multibyte_p;
6415 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6416 FRAME_WINDOW_P (it->f), &it->bidi_it);
6417 }
6418 }
6419 else
6420 {
6421 it->s = (const unsigned char *) s;
6422 it->string = Qnil;
6423
6424 /* Note that we use IT->current.pos, not it->current.string_pos,
6425 for displaying C strings. */
6426 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6427 if (it->multibyte_p)
6428 {
6429 it->current.pos = c_string_pos (charpos, s, 1);
6430 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6431 }
6432 else
6433 {
6434 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6435 it->end_charpos = it->string_nchars = strlen (s);
6436 }
6437
6438 if (it->bidi_p)
6439 {
6440 it->bidi_it.string.lstring = Qnil;
6441 it->bidi_it.string.s = (const unsigned char *) s;
6442 it->bidi_it.string.schars = it->end_charpos;
6443 it->bidi_it.string.bufpos = 0;
6444 it->bidi_it.string.from_disp_str = 0;
6445 it->bidi_it.string.unibyte = !it->multibyte_p;
6446 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6447 &it->bidi_it);
6448 }
6449 it->method = GET_FROM_C_STRING;
6450 }
6451
6452 /* PRECISION > 0 means don't return more than PRECISION characters
6453 from the string. */
6454 if (precision > 0 && it->end_charpos - charpos > precision)
6455 {
6456 it->end_charpos = it->string_nchars = charpos + precision;
6457 if (it->bidi_p)
6458 it->bidi_it.string.schars = it->end_charpos;
6459 }
6460
6461 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6462 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6463 FIELD_WIDTH < 0 means infinite field width. This is useful for
6464 padding with `-' at the end of a mode line. */
6465 if (field_width < 0)
6466 field_width = INFINITY;
6467 /* Implementation note: We deliberately don't enlarge
6468 it->bidi_it.string.schars here to fit it->end_charpos, because
6469 the bidi iterator cannot produce characters out of thin air. */
6470 if (field_width > it->end_charpos - charpos)
6471 it->end_charpos = charpos + field_width;
6472
6473 /* Use the standard display table for displaying strings. */
6474 if (DISP_TABLE_P (Vstandard_display_table))
6475 it->dp = XCHAR_TABLE (Vstandard_display_table);
6476
6477 it->stop_charpos = charpos;
6478 it->prev_stop = charpos;
6479 it->base_level_stop = 0;
6480 if (it->bidi_p)
6481 {
6482 it->bidi_it.first_elt = 1;
6483 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6484 it->bidi_it.disp_pos = -1;
6485 }
6486 if (s == NULL && it->multibyte_p)
6487 {
6488 ptrdiff_t endpos = SCHARS (it->string);
6489 if (endpos > it->end_charpos)
6490 endpos = it->end_charpos;
6491 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6492 it->string);
6493 }
6494 CHECK_IT (it);
6495 }
6496
6497
6498 \f
6499 /***********************************************************************
6500 Iteration
6501 ***********************************************************************/
6502
6503 /* Map enum it_method value to corresponding next_element_from_* function. */
6504
6505 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6506 {
6507 next_element_from_buffer,
6508 next_element_from_display_vector,
6509 next_element_from_string,
6510 next_element_from_c_string,
6511 next_element_from_image,
6512 next_element_from_stretch
6513 };
6514
6515 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6516
6517
6518 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6519 (possibly with the following characters). */
6520
6521 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6522 ((IT)->cmp_it.id >= 0 \
6523 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6524 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6525 END_CHARPOS, (IT)->w, \
6526 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6527 (IT)->string)))
6528
6529
6530 /* Lookup the char-table Vglyphless_char_display for character C (-1
6531 if we want information for no-font case), and return the display
6532 method symbol. By side-effect, update it->what and
6533 it->glyphless_method. This function is called from
6534 get_next_display_element for each character element, and from
6535 x_produce_glyphs when no suitable font was found. */
6536
6537 Lisp_Object
6538 lookup_glyphless_char_display (int c, struct it *it)
6539 {
6540 Lisp_Object glyphless_method = Qnil;
6541
6542 if (CHAR_TABLE_P (Vglyphless_char_display)
6543 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6544 {
6545 if (c >= 0)
6546 {
6547 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6548 if (CONSP (glyphless_method))
6549 glyphless_method = FRAME_WINDOW_P (it->f)
6550 ? XCAR (glyphless_method)
6551 : XCDR (glyphless_method);
6552 }
6553 else
6554 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6555 }
6556
6557 retry:
6558 if (NILP (glyphless_method))
6559 {
6560 if (c >= 0)
6561 /* The default is to display the character by a proper font. */
6562 return Qnil;
6563 /* The default for the no-font case is to display an empty box. */
6564 glyphless_method = Qempty_box;
6565 }
6566 if (EQ (glyphless_method, Qzero_width))
6567 {
6568 if (c >= 0)
6569 return glyphless_method;
6570 /* This method can't be used for the no-font case. */
6571 glyphless_method = Qempty_box;
6572 }
6573 if (EQ (glyphless_method, Qthin_space))
6574 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6575 else if (EQ (glyphless_method, Qempty_box))
6576 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6577 else if (EQ (glyphless_method, Qhex_code))
6578 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6579 else if (STRINGP (glyphless_method))
6580 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6581 else
6582 {
6583 /* Invalid value. We use the default method. */
6584 glyphless_method = Qnil;
6585 goto retry;
6586 }
6587 it->what = IT_GLYPHLESS;
6588 return glyphless_method;
6589 }
6590
6591 /* Load IT's display element fields with information about the next
6592 display element from the current position of IT. Value is zero if
6593 end of buffer (or C string) is reached. */
6594
6595 static struct frame *last_escape_glyph_frame = NULL;
6596 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6597 static int last_escape_glyph_merged_face_id = 0;
6598
6599 struct frame *last_glyphless_glyph_frame = NULL;
6600 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6601 int last_glyphless_glyph_merged_face_id = 0;
6602
6603 static int
6604 get_next_display_element (struct it *it)
6605 {
6606 /* Non-zero means that we found a display element. Zero means that
6607 we hit the end of what we iterate over. Performance note: the
6608 function pointer `method' used here turns out to be faster than
6609 using a sequence of if-statements. */
6610 int success_p;
6611
6612 get_next:
6613 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6614
6615 if (it->what == IT_CHARACTER)
6616 {
6617 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6618 and only if (a) the resolved directionality of that character
6619 is R..." */
6620 /* FIXME: Do we need an exception for characters from display
6621 tables? */
6622 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6623 it->c = bidi_mirror_char (it->c);
6624 /* Map via display table or translate control characters.
6625 IT->c, IT->len etc. have been set to the next character by
6626 the function call above. If we have a display table, and it
6627 contains an entry for IT->c, translate it. Don't do this if
6628 IT->c itself comes from a display table, otherwise we could
6629 end up in an infinite recursion. (An alternative could be to
6630 count the recursion depth of this function and signal an
6631 error when a certain maximum depth is reached.) Is it worth
6632 it? */
6633 if (success_p && it->dpvec == NULL)
6634 {
6635 Lisp_Object dv;
6636 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6637 int nonascii_space_p = 0;
6638 int nonascii_hyphen_p = 0;
6639 int c = it->c; /* This is the character to display. */
6640
6641 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6642 {
6643 eassert (SINGLE_BYTE_CHAR_P (c));
6644 if (unibyte_display_via_language_environment)
6645 {
6646 c = DECODE_CHAR (unibyte, c);
6647 if (c < 0)
6648 c = BYTE8_TO_CHAR (it->c);
6649 }
6650 else
6651 c = BYTE8_TO_CHAR (it->c);
6652 }
6653
6654 if (it->dp
6655 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6656 VECTORP (dv)))
6657 {
6658 struct Lisp_Vector *v = XVECTOR (dv);
6659
6660 /* Return the first character from the display table
6661 entry, if not empty. If empty, don't display the
6662 current character. */
6663 if (v->header.size)
6664 {
6665 it->dpvec_char_len = it->len;
6666 it->dpvec = v->contents;
6667 it->dpend = v->contents + v->header.size;
6668 it->current.dpvec_index = 0;
6669 it->dpvec_face_id = -1;
6670 it->saved_face_id = it->face_id;
6671 it->method = GET_FROM_DISPLAY_VECTOR;
6672 it->ellipsis_p = 0;
6673 }
6674 else
6675 {
6676 set_iterator_to_next (it, 0);
6677 }
6678 goto get_next;
6679 }
6680
6681 if (! NILP (lookup_glyphless_char_display (c, it)))
6682 {
6683 if (it->what == IT_GLYPHLESS)
6684 goto done;
6685 /* Don't display this character. */
6686 set_iterator_to_next (it, 0);
6687 goto get_next;
6688 }
6689
6690 /* If `nobreak-char-display' is non-nil, we display
6691 non-ASCII spaces and hyphens specially. */
6692 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6693 {
6694 if (c == 0xA0)
6695 nonascii_space_p = 1;
6696 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6697 nonascii_hyphen_p = 1;
6698 }
6699
6700 /* Translate control characters into `\003' or `^C' form.
6701 Control characters coming from a display table entry are
6702 currently not translated because we use IT->dpvec to hold
6703 the translation. This could easily be changed but I
6704 don't believe that it is worth doing.
6705
6706 The characters handled by `nobreak-char-display' must be
6707 translated too.
6708
6709 Non-printable characters and raw-byte characters are also
6710 translated to octal form. */
6711 if (((c < ' ' || c == 127) /* ASCII control chars */
6712 ? (it->area != TEXT_AREA
6713 /* In mode line, treat \n, \t like other crl chars. */
6714 || (c != '\t'
6715 && it->glyph_row
6716 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6717 || (c != '\n' && c != '\t'))
6718 : (nonascii_space_p
6719 || nonascii_hyphen_p
6720 || CHAR_BYTE8_P (c)
6721 || ! CHAR_PRINTABLE_P (c))))
6722 {
6723 /* C is a control character, non-ASCII space/hyphen,
6724 raw-byte, or a non-printable character which must be
6725 displayed either as '\003' or as `^C' where the '\\'
6726 and '^' can be defined in the display table. Fill
6727 IT->ctl_chars with glyphs for what we have to
6728 display. Then, set IT->dpvec to these glyphs. */
6729 Lisp_Object gc;
6730 int ctl_len;
6731 int face_id;
6732 int lface_id = 0;
6733 int escape_glyph;
6734
6735 /* Handle control characters with ^. */
6736
6737 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6738 {
6739 int g;
6740
6741 g = '^'; /* default glyph for Control */
6742 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6743 if (it->dp
6744 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6745 {
6746 g = GLYPH_CODE_CHAR (gc);
6747 lface_id = GLYPH_CODE_FACE (gc);
6748 }
6749 if (lface_id)
6750 {
6751 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6752 }
6753 else if (it->f == last_escape_glyph_frame
6754 && it->face_id == last_escape_glyph_face_id)
6755 {
6756 face_id = last_escape_glyph_merged_face_id;
6757 }
6758 else
6759 {
6760 /* Merge the escape-glyph face into the current face. */
6761 face_id = merge_faces (it->f, Qescape_glyph, 0,
6762 it->face_id);
6763 last_escape_glyph_frame = it->f;
6764 last_escape_glyph_face_id = it->face_id;
6765 last_escape_glyph_merged_face_id = face_id;
6766 }
6767
6768 XSETINT (it->ctl_chars[0], g);
6769 XSETINT (it->ctl_chars[1], c ^ 0100);
6770 ctl_len = 2;
6771 goto display_control;
6772 }
6773
6774 /* Handle non-ascii space in the mode where it only gets
6775 highlighting. */
6776
6777 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6778 {
6779 /* Merge `nobreak-space' into the current face. */
6780 face_id = merge_faces (it->f, Qnobreak_space, 0,
6781 it->face_id);
6782 XSETINT (it->ctl_chars[0], ' ');
6783 ctl_len = 1;
6784 goto display_control;
6785 }
6786
6787 /* Handle sequences that start with the "escape glyph". */
6788
6789 /* the default escape glyph is \. */
6790 escape_glyph = '\\';
6791
6792 if (it->dp
6793 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6794 {
6795 escape_glyph = GLYPH_CODE_CHAR (gc);
6796 lface_id = GLYPH_CODE_FACE (gc);
6797 }
6798 if (lface_id)
6799 {
6800 /* The display table specified a face.
6801 Merge it into face_id and also into escape_glyph. */
6802 face_id = merge_faces (it->f, Qt, lface_id,
6803 it->face_id);
6804 }
6805 else if (it->f == last_escape_glyph_frame
6806 && it->face_id == last_escape_glyph_face_id)
6807 {
6808 face_id = last_escape_glyph_merged_face_id;
6809 }
6810 else
6811 {
6812 /* Merge the escape-glyph face into the current face. */
6813 face_id = merge_faces (it->f, Qescape_glyph, 0,
6814 it->face_id);
6815 last_escape_glyph_frame = it->f;
6816 last_escape_glyph_face_id = it->face_id;
6817 last_escape_glyph_merged_face_id = face_id;
6818 }
6819
6820 /* Draw non-ASCII hyphen with just highlighting: */
6821
6822 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6823 {
6824 XSETINT (it->ctl_chars[0], '-');
6825 ctl_len = 1;
6826 goto display_control;
6827 }
6828
6829 /* Draw non-ASCII space/hyphen with escape glyph: */
6830
6831 if (nonascii_space_p || nonascii_hyphen_p)
6832 {
6833 XSETINT (it->ctl_chars[0], escape_glyph);
6834 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6835 ctl_len = 2;
6836 goto display_control;
6837 }
6838
6839 {
6840 char str[10];
6841 int len, i;
6842
6843 if (CHAR_BYTE8_P (c))
6844 /* Display \200 instead of \17777600. */
6845 c = CHAR_TO_BYTE8 (c);
6846 len = sprintf (str, "%03o", c);
6847
6848 XSETINT (it->ctl_chars[0], escape_glyph);
6849 for (i = 0; i < len; i++)
6850 XSETINT (it->ctl_chars[i + 1], str[i]);
6851 ctl_len = len + 1;
6852 }
6853
6854 display_control:
6855 /* Set up IT->dpvec and return first character from it. */
6856 it->dpvec_char_len = it->len;
6857 it->dpvec = it->ctl_chars;
6858 it->dpend = it->dpvec + ctl_len;
6859 it->current.dpvec_index = 0;
6860 it->dpvec_face_id = face_id;
6861 it->saved_face_id = it->face_id;
6862 it->method = GET_FROM_DISPLAY_VECTOR;
6863 it->ellipsis_p = 0;
6864 goto get_next;
6865 }
6866 it->char_to_display = c;
6867 }
6868 else if (success_p)
6869 {
6870 it->char_to_display = it->c;
6871 }
6872 }
6873
6874 /* Adjust face id for a multibyte character. There are no multibyte
6875 character in unibyte text. */
6876 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6877 && it->multibyte_p
6878 && success_p
6879 && FRAME_WINDOW_P (it->f))
6880 {
6881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6882
6883 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6884 {
6885 /* Automatic composition with glyph-string. */
6886 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6887
6888 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6889 }
6890 else
6891 {
6892 ptrdiff_t pos = (it->s ? -1
6893 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6894 : IT_CHARPOS (*it));
6895 int c;
6896
6897 if (it->what == IT_CHARACTER)
6898 c = it->char_to_display;
6899 else
6900 {
6901 struct composition *cmp = composition_table[it->cmp_it.id];
6902 int i;
6903
6904 c = ' ';
6905 for (i = 0; i < cmp->glyph_len; i++)
6906 /* TAB in a composition means display glyphs with
6907 padding space on the left or right. */
6908 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6909 break;
6910 }
6911 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6912 }
6913 }
6914
6915 done:
6916 /* Is this character the last one of a run of characters with
6917 box? If yes, set IT->end_of_box_run_p to 1. */
6918 if (it->face_box_p
6919 && it->s == NULL)
6920 {
6921 if (it->method == GET_FROM_STRING && it->sp)
6922 {
6923 int face_id = underlying_face_id (it);
6924 struct face *face = FACE_FROM_ID (it->f, face_id);
6925
6926 if (face)
6927 {
6928 if (face->box == FACE_NO_BOX)
6929 {
6930 /* If the box comes from face properties in a
6931 display string, check faces in that string. */
6932 int string_face_id = face_after_it_pos (it);
6933 it->end_of_box_run_p
6934 = (FACE_FROM_ID (it->f, string_face_id)->box
6935 == FACE_NO_BOX);
6936 }
6937 /* Otherwise, the box comes from the underlying face.
6938 If this is the last string character displayed, check
6939 the next buffer location. */
6940 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6941 && (it->current.overlay_string_index
6942 == it->n_overlay_strings - 1))
6943 {
6944 ptrdiff_t ignore;
6945 int next_face_id;
6946 struct text_pos pos = it->current.pos;
6947 INC_TEXT_POS (pos, it->multibyte_p);
6948
6949 next_face_id = face_at_buffer_position
6950 (it->w, CHARPOS (pos), it->region_beg_charpos,
6951 it->region_end_charpos, &ignore,
6952 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6953 -1);
6954 it->end_of_box_run_p
6955 = (FACE_FROM_ID (it->f, next_face_id)->box
6956 == FACE_NO_BOX);
6957 }
6958 }
6959 }
6960 else
6961 {
6962 int face_id = face_after_it_pos (it);
6963 it->end_of_box_run_p
6964 = (face_id != it->face_id
6965 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6966 }
6967 }
6968 /* If we reached the end of the object we've been iterating (e.g., a
6969 display string or an overlay string), and there's something on
6970 IT->stack, proceed with what's on the stack. It doesn't make
6971 sense to return zero if there's unprocessed stuff on the stack,
6972 because otherwise that stuff will never be displayed. */
6973 if (!success_p && it->sp > 0)
6974 {
6975 set_iterator_to_next (it, 0);
6976 success_p = get_next_display_element (it);
6977 }
6978
6979 /* Value is 0 if end of buffer or string reached. */
6980 return success_p;
6981 }
6982
6983
6984 /* Move IT to the next display element.
6985
6986 RESEAT_P non-zero means if called on a newline in buffer text,
6987 skip to the next visible line start.
6988
6989 Functions get_next_display_element and set_iterator_to_next are
6990 separate because I find this arrangement easier to handle than a
6991 get_next_display_element function that also increments IT's
6992 position. The way it is we can first look at an iterator's current
6993 display element, decide whether it fits on a line, and if it does,
6994 increment the iterator position. The other way around we probably
6995 would either need a flag indicating whether the iterator has to be
6996 incremented the next time, or we would have to implement a
6997 decrement position function which would not be easy to write. */
6998
6999 void
7000 set_iterator_to_next (struct it *it, int reseat_p)
7001 {
7002 /* Reset flags indicating start and end of a sequence of characters
7003 with box. Reset them at the start of this function because
7004 moving the iterator to a new position might set them. */
7005 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7006
7007 switch (it->method)
7008 {
7009 case GET_FROM_BUFFER:
7010 /* The current display element of IT is a character from
7011 current_buffer. Advance in the buffer, and maybe skip over
7012 invisible lines that are so because of selective display. */
7013 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7014 reseat_at_next_visible_line_start (it, 0);
7015 else if (it->cmp_it.id >= 0)
7016 {
7017 /* We are currently getting glyphs from a composition. */
7018 int i;
7019
7020 if (! it->bidi_p)
7021 {
7022 IT_CHARPOS (*it) += it->cmp_it.nchars;
7023 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7024 if (it->cmp_it.to < it->cmp_it.nglyphs)
7025 {
7026 it->cmp_it.from = it->cmp_it.to;
7027 }
7028 else
7029 {
7030 it->cmp_it.id = -1;
7031 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7032 IT_BYTEPOS (*it),
7033 it->end_charpos, Qnil);
7034 }
7035 }
7036 else if (! it->cmp_it.reversed_p)
7037 {
7038 /* Composition created while scanning forward. */
7039 /* Update IT's char/byte positions to point to the first
7040 character of the next grapheme cluster, or to the
7041 character visually after the current composition. */
7042 for (i = 0; i < it->cmp_it.nchars; i++)
7043 bidi_move_to_visually_next (&it->bidi_it);
7044 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7045 IT_CHARPOS (*it) = it->bidi_it.charpos;
7046
7047 if (it->cmp_it.to < it->cmp_it.nglyphs)
7048 {
7049 /* Proceed to the next grapheme cluster. */
7050 it->cmp_it.from = it->cmp_it.to;
7051 }
7052 else
7053 {
7054 /* No more grapheme clusters in this composition.
7055 Find the next stop position. */
7056 ptrdiff_t stop = it->end_charpos;
7057 if (it->bidi_it.scan_dir < 0)
7058 /* Now we are scanning backward and don't know
7059 where to stop. */
7060 stop = -1;
7061 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7062 IT_BYTEPOS (*it), stop, Qnil);
7063 }
7064 }
7065 else
7066 {
7067 /* Composition created while scanning backward. */
7068 /* Update IT's char/byte positions to point to the last
7069 character of the previous grapheme cluster, or the
7070 character visually after the current composition. */
7071 for (i = 0; i < it->cmp_it.nchars; i++)
7072 bidi_move_to_visually_next (&it->bidi_it);
7073 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7074 IT_CHARPOS (*it) = it->bidi_it.charpos;
7075 if (it->cmp_it.from > 0)
7076 {
7077 /* Proceed to the previous grapheme cluster. */
7078 it->cmp_it.to = it->cmp_it.from;
7079 }
7080 else
7081 {
7082 /* No more grapheme clusters in this composition.
7083 Find the next stop position. */
7084 ptrdiff_t stop = it->end_charpos;
7085 if (it->bidi_it.scan_dir < 0)
7086 /* Now we are scanning backward and don't know
7087 where to stop. */
7088 stop = -1;
7089 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7090 IT_BYTEPOS (*it), stop, Qnil);
7091 }
7092 }
7093 }
7094 else
7095 {
7096 eassert (it->len != 0);
7097
7098 if (!it->bidi_p)
7099 {
7100 IT_BYTEPOS (*it) += it->len;
7101 IT_CHARPOS (*it) += 1;
7102 }
7103 else
7104 {
7105 int prev_scan_dir = it->bidi_it.scan_dir;
7106 /* If this is a new paragraph, determine its base
7107 direction (a.k.a. its base embedding level). */
7108 if (it->bidi_it.new_paragraph)
7109 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7110 bidi_move_to_visually_next (&it->bidi_it);
7111 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7112 IT_CHARPOS (*it) = it->bidi_it.charpos;
7113 if (prev_scan_dir != it->bidi_it.scan_dir)
7114 {
7115 /* As the scan direction was changed, we must
7116 re-compute the stop position for composition. */
7117 ptrdiff_t stop = it->end_charpos;
7118 if (it->bidi_it.scan_dir < 0)
7119 stop = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it), stop, Qnil);
7122 }
7123 }
7124 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7125 }
7126 break;
7127
7128 case GET_FROM_C_STRING:
7129 /* Current display element of IT is from a C string. */
7130 if (!it->bidi_p
7131 /* If the string position is beyond string's end, it means
7132 next_element_from_c_string is padding the string with
7133 blanks, in which case we bypass the bidi iterator,
7134 because it cannot deal with such virtual characters. */
7135 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7136 {
7137 IT_BYTEPOS (*it) += it->len;
7138 IT_CHARPOS (*it) += 1;
7139 }
7140 else
7141 {
7142 bidi_move_to_visually_next (&it->bidi_it);
7143 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7144 IT_CHARPOS (*it) = it->bidi_it.charpos;
7145 }
7146 break;
7147
7148 case GET_FROM_DISPLAY_VECTOR:
7149 /* Current display element of IT is from a display table entry.
7150 Advance in the display table definition. Reset it to null if
7151 end reached, and continue with characters from buffers/
7152 strings. */
7153 ++it->current.dpvec_index;
7154
7155 /* Restore face of the iterator to what they were before the
7156 display vector entry (these entries may contain faces). */
7157 it->face_id = it->saved_face_id;
7158
7159 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7160 {
7161 int recheck_faces = it->ellipsis_p;
7162
7163 if (it->s)
7164 it->method = GET_FROM_C_STRING;
7165 else if (STRINGP (it->string))
7166 it->method = GET_FROM_STRING;
7167 else
7168 {
7169 it->method = GET_FROM_BUFFER;
7170 it->object = it->w->buffer;
7171 }
7172
7173 it->dpvec = NULL;
7174 it->current.dpvec_index = -1;
7175
7176 /* Skip over characters which were displayed via IT->dpvec. */
7177 if (it->dpvec_char_len < 0)
7178 reseat_at_next_visible_line_start (it, 1);
7179 else if (it->dpvec_char_len > 0)
7180 {
7181 if (it->method == GET_FROM_STRING
7182 && it->n_overlay_strings > 0)
7183 it->ignore_overlay_strings_at_pos_p = 1;
7184 it->len = it->dpvec_char_len;
7185 set_iterator_to_next (it, reseat_p);
7186 }
7187
7188 /* Maybe recheck faces after display vector */
7189 if (recheck_faces)
7190 it->stop_charpos = IT_CHARPOS (*it);
7191 }
7192 break;
7193
7194 case GET_FROM_STRING:
7195 /* Current display element is a character from a Lisp string. */
7196 eassert (it->s == NULL && STRINGP (it->string));
7197 /* Don't advance past string end. These conditions are true
7198 when set_iterator_to_next is called at the end of
7199 get_next_display_element, in which case the Lisp string is
7200 already exhausted, and all we want is pop the iterator
7201 stack. */
7202 if (it->current.overlay_string_index >= 0)
7203 {
7204 /* This is an overlay string, so there's no padding with
7205 spaces, and the number of characters in the string is
7206 where the string ends. */
7207 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7208 goto consider_string_end;
7209 }
7210 else
7211 {
7212 /* Not an overlay string. There could be padding, so test
7213 against it->end_charpos . */
7214 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7215 goto consider_string_end;
7216 }
7217 if (it->cmp_it.id >= 0)
7218 {
7219 int i;
7220
7221 if (! it->bidi_p)
7222 {
7223 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7224 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7225 if (it->cmp_it.to < it->cmp_it.nglyphs)
7226 it->cmp_it.from = it->cmp_it.to;
7227 else
7228 {
7229 it->cmp_it.id = -1;
7230 composition_compute_stop_pos (&it->cmp_it,
7231 IT_STRING_CHARPOS (*it),
7232 IT_STRING_BYTEPOS (*it),
7233 it->end_charpos, it->string);
7234 }
7235 }
7236 else if (! it->cmp_it.reversed_p)
7237 {
7238 for (i = 0; i < it->cmp_it.nchars; i++)
7239 bidi_move_to_visually_next (&it->bidi_it);
7240 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7241 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7242
7243 if (it->cmp_it.to < it->cmp_it.nglyphs)
7244 it->cmp_it.from = it->cmp_it.to;
7245 else
7246 {
7247 ptrdiff_t stop = it->end_charpos;
7248 if (it->bidi_it.scan_dir < 0)
7249 stop = -1;
7250 composition_compute_stop_pos (&it->cmp_it,
7251 IT_STRING_CHARPOS (*it),
7252 IT_STRING_BYTEPOS (*it), stop,
7253 it->string);
7254 }
7255 }
7256 else
7257 {
7258 for (i = 0; i < it->cmp_it.nchars; i++)
7259 bidi_move_to_visually_next (&it->bidi_it);
7260 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7261 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7262 if (it->cmp_it.from > 0)
7263 it->cmp_it.to = it->cmp_it.from;
7264 else
7265 {
7266 ptrdiff_t stop = it->end_charpos;
7267 if (it->bidi_it.scan_dir < 0)
7268 stop = -1;
7269 composition_compute_stop_pos (&it->cmp_it,
7270 IT_STRING_CHARPOS (*it),
7271 IT_STRING_BYTEPOS (*it), stop,
7272 it->string);
7273 }
7274 }
7275 }
7276 else
7277 {
7278 if (!it->bidi_p
7279 /* If the string position is beyond string's end, it
7280 means next_element_from_string is padding the string
7281 with blanks, in which case we bypass the bidi
7282 iterator, because it cannot deal with such virtual
7283 characters. */
7284 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7285 {
7286 IT_STRING_BYTEPOS (*it) += it->len;
7287 IT_STRING_CHARPOS (*it) += 1;
7288 }
7289 else
7290 {
7291 int prev_scan_dir = it->bidi_it.scan_dir;
7292
7293 bidi_move_to_visually_next (&it->bidi_it);
7294 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7295 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7296 if (prev_scan_dir != it->bidi_it.scan_dir)
7297 {
7298 ptrdiff_t stop = it->end_charpos;
7299
7300 if (it->bidi_it.scan_dir < 0)
7301 stop = -1;
7302 composition_compute_stop_pos (&it->cmp_it,
7303 IT_STRING_CHARPOS (*it),
7304 IT_STRING_BYTEPOS (*it), stop,
7305 it->string);
7306 }
7307 }
7308 }
7309
7310 consider_string_end:
7311
7312 if (it->current.overlay_string_index >= 0)
7313 {
7314 /* IT->string is an overlay string. Advance to the
7315 next, if there is one. */
7316 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7317 {
7318 it->ellipsis_p = 0;
7319 next_overlay_string (it);
7320 if (it->ellipsis_p)
7321 setup_for_ellipsis (it, 0);
7322 }
7323 }
7324 else
7325 {
7326 /* IT->string is not an overlay string. If we reached
7327 its end, and there is something on IT->stack, proceed
7328 with what is on the stack. This can be either another
7329 string, this time an overlay string, or a buffer. */
7330 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7331 && it->sp > 0)
7332 {
7333 pop_it (it);
7334 if (it->method == GET_FROM_STRING)
7335 goto consider_string_end;
7336 }
7337 }
7338 break;
7339
7340 case GET_FROM_IMAGE:
7341 case GET_FROM_STRETCH:
7342 /* The position etc with which we have to proceed are on
7343 the stack. The position may be at the end of a string,
7344 if the `display' property takes up the whole string. */
7345 eassert (it->sp > 0);
7346 pop_it (it);
7347 if (it->method == GET_FROM_STRING)
7348 goto consider_string_end;
7349 break;
7350
7351 default:
7352 /* There are no other methods defined, so this should be a bug. */
7353 emacs_abort ();
7354 }
7355
7356 eassert (it->method != GET_FROM_STRING
7357 || (STRINGP (it->string)
7358 && IT_STRING_CHARPOS (*it) >= 0));
7359 }
7360
7361 /* Load IT's display element fields with information about the next
7362 display element which comes from a display table entry or from the
7363 result of translating a control character to one of the forms `^C'
7364 or `\003'.
7365
7366 IT->dpvec holds the glyphs to return as characters.
7367 IT->saved_face_id holds the face id before the display vector--it
7368 is restored into IT->face_id in set_iterator_to_next. */
7369
7370 static int
7371 next_element_from_display_vector (struct it *it)
7372 {
7373 Lisp_Object gc;
7374
7375 /* Precondition. */
7376 eassert (it->dpvec && it->current.dpvec_index >= 0);
7377
7378 it->face_id = it->saved_face_id;
7379
7380 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7381 That seemed totally bogus - so I changed it... */
7382 gc = it->dpvec[it->current.dpvec_index];
7383
7384 if (GLYPH_CODE_P (gc))
7385 {
7386 it->c = GLYPH_CODE_CHAR (gc);
7387 it->len = CHAR_BYTES (it->c);
7388
7389 /* The entry may contain a face id to use. Such a face id is
7390 the id of a Lisp face, not a realized face. A face id of
7391 zero means no face is specified. */
7392 if (it->dpvec_face_id >= 0)
7393 it->face_id = it->dpvec_face_id;
7394 else
7395 {
7396 int lface_id = GLYPH_CODE_FACE (gc);
7397 if (lface_id > 0)
7398 it->face_id = merge_faces (it->f, Qt, lface_id,
7399 it->saved_face_id);
7400 }
7401 }
7402 else
7403 /* Display table entry is invalid. Return a space. */
7404 it->c = ' ', it->len = 1;
7405
7406 /* Don't change position and object of the iterator here. They are
7407 still the values of the character that had this display table
7408 entry or was translated, and that's what we want. */
7409 it->what = IT_CHARACTER;
7410 return 1;
7411 }
7412
7413 /* Get the first element of string/buffer in the visual order, after
7414 being reseated to a new position in a string or a buffer. */
7415 static void
7416 get_visually_first_element (struct it *it)
7417 {
7418 int string_p = STRINGP (it->string) || it->s;
7419 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7420 ptrdiff_t bob = (string_p ? 0 : BEGV);
7421
7422 if (STRINGP (it->string))
7423 {
7424 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7425 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7426 }
7427 else
7428 {
7429 it->bidi_it.charpos = IT_CHARPOS (*it);
7430 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7431 }
7432
7433 if (it->bidi_it.charpos == eob)
7434 {
7435 /* Nothing to do, but reset the FIRST_ELT flag, like
7436 bidi_paragraph_init does, because we are not going to
7437 call it. */
7438 it->bidi_it.first_elt = 0;
7439 }
7440 else if (it->bidi_it.charpos == bob
7441 || (!string_p
7442 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7443 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7444 {
7445 /* If we are at the beginning of a line/string, we can produce
7446 the next element right away. */
7447 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7448 bidi_move_to_visually_next (&it->bidi_it);
7449 }
7450 else
7451 {
7452 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7453
7454 /* We need to prime the bidi iterator starting at the line's or
7455 string's beginning, before we will be able to produce the
7456 next element. */
7457 if (string_p)
7458 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7459 else
7460 {
7461 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7462 -1);
7463 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7464 }
7465 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7466 do
7467 {
7468 /* Now return to buffer/string position where we were asked
7469 to get the next display element, and produce that. */
7470 bidi_move_to_visually_next (&it->bidi_it);
7471 }
7472 while (it->bidi_it.bytepos != orig_bytepos
7473 && it->bidi_it.charpos < eob);
7474 }
7475
7476 /* Adjust IT's position information to where we ended up. */
7477 if (STRINGP (it->string))
7478 {
7479 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7480 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7481 }
7482 else
7483 {
7484 IT_CHARPOS (*it) = it->bidi_it.charpos;
7485 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7486 }
7487
7488 if (STRINGP (it->string) || !it->s)
7489 {
7490 ptrdiff_t stop, charpos, bytepos;
7491
7492 if (STRINGP (it->string))
7493 {
7494 eassert (!it->s);
7495 stop = SCHARS (it->string);
7496 if (stop > it->end_charpos)
7497 stop = it->end_charpos;
7498 charpos = IT_STRING_CHARPOS (*it);
7499 bytepos = IT_STRING_BYTEPOS (*it);
7500 }
7501 else
7502 {
7503 stop = it->end_charpos;
7504 charpos = IT_CHARPOS (*it);
7505 bytepos = IT_BYTEPOS (*it);
7506 }
7507 if (it->bidi_it.scan_dir < 0)
7508 stop = -1;
7509 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7510 it->string);
7511 }
7512 }
7513
7514 /* Load IT with the next display element from Lisp string IT->string.
7515 IT->current.string_pos is the current position within the string.
7516 If IT->current.overlay_string_index >= 0, the Lisp string is an
7517 overlay string. */
7518
7519 static int
7520 next_element_from_string (struct it *it)
7521 {
7522 struct text_pos position;
7523
7524 eassert (STRINGP (it->string));
7525 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7526 eassert (IT_STRING_CHARPOS (*it) >= 0);
7527 position = it->current.string_pos;
7528
7529 /* With bidi reordering, the character to display might not be the
7530 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7531 that we were reseat()ed to a new string, whose paragraph
7532 direction is not known. */
7533 if (it->bidi_p && it->bidi_it.first_elt)
7534 {
7535 get_visually_first_element (it);
7536 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7537 }
7538
7539 /* Time to check for invisible text? */
7540 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7541 {
7542 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7543 {
7544 if (!(!it->bidi_p
7545 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7546 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7547 {
7548 /* With bidi non-linear iteration, we could find
7549 ourselves far beyond the last computed stop_charpos,
7550 with several other stop positions in between that we
7551 missed. Scan them all now, in buffer's logical
7552 order, until we find and handle the last stop_charpos
7553 that precedes our current position. */
7554 handle_stop_backwards (it, it->stop_charpos);
7555 return GET_NEXT_DISPLAY_ELEMENT (it);
7556 }
7557 else
7558 {
7559 if (it->bidi_p)
7560 {
7561 /* Take note of the stop position we just moved
7562 across, for when we will move back across it. */
7563 it->prev_stop = it->stop_charpos;
7564 /* If we are at base paragraph embedding level, take
7565 note of the last stop position seen at this
7566 level. */
7567 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7568 it->base_level_stop = it->stop_charpos;
7569 }
7570 handle_stop (it);
7571
7572 /* Since a handler may have changed IT->method, we must
7573 recurse here. */
7574 return GET_NEXT_DISPLAY_ELEMENT (it);
7575 }
7576 }
7577 else if (it->bidi_p
7578 /* If we are before prev_stop, we may have overstepped
7579 on our way backwards a stop_pos, and if so, we need
7580 to handle that stop_pos. */
7581 && IT_STRING_CHARPOS (*it) < it->prev_stop
7582 /* We can sometimes back up for reasons that have nothing
7583 to do with bidi reordering. E.g., compositions. The
7584 code below is only needed when we are above the base
7585 embedding level, so test for that explicitly. */
7586 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7587 {
7588 /* If we lost track of base_level_stop, we have no better
7589 place for handle_stop_backwards to start from than string
7590 beginning. This happens, e.g., when we were reseated to
7591 the previous screenful of text by vertical-motion. */
7592 if (it->base_level_stop <= 0
7593 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7594 it->base_level_stop = 0;
7595 handle_stop_backwards (it, it->base_level_stop);
7596 return GET_NEXT_DISPLAY_ELEMENT (it);
7597 }
7598 }
7599
7600 if (it->current.overlay_string_index >= 0)
7601 {
7602 /* Get the next character from an overlay string. In overlay
7603 strings, there is no field width or padding with spaces to
7604 do. */
7605 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7606 {
7607 it->what = IT_EOB;
7608 return 0;
7609 }
7610 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7611 IT_STRING_BYTEPOS (*it),
7612 it->bidi_it.scan_dir < 0
7613 ? -1
7614 : SCHARS (it->string))
7615 && next_element_from_composition (it))
7616 {
7617 return 1;
7618 }
7619 else if (STRING_MULTIBYTE (it->string))
7620 {
7621 const unsigned char *s = (SDATA (it->string)
7622 + IT_STRING_BYTEPOS (*it));
7623 it->c = string_char_and_length (s, &it->len);
7624 }
7625 else
7626 {
7627 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7628 it->len = 1;
7629 }
7630 }
7631 else
7632 {
7633 /* Get the next character from a Lisp string that is not an
7634 overlay string. Such strings come from the mode line, for
7635 example. We may have to pad with spaces, or truncate the
7636 string. See also next_element_from_c_string. */
7637 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7638 {
7639 it->what = IT_EOB;
7640 return 0;
7641 }
7642 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7643 {
7644 /* Pad with spaces. */
7645 it->c = ' ', it->len = 1;
7646 CHARPOS (position) = BYTEPOS (position) = -1;
7647 }
7648 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7649 IT_STRING_BYTEPOS (*it),
7650 it->bidi_it.scan_dir < 0
7651 ? -1
7652 : it->string_nchars)
7653 && next_element_from_composition (it))
7654 {
7655 return 1;
7656 }
7657 else if (STRING_MULTIBYTE (it->string))
7658 {
7659 const unsigned char *s = (SDATA (it->string)
7660 + IT_STRING_BYTEPOS (*it));
7661 it->c = string_char_and_length (s, &it->len);
7662 }
7663 else
7664 {
7665 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7666 it->len = 1;
7667 }
7668 }
7669
7670 /* Record what we have and where it came from. */
7671 it->what = IT_CHARACTER;
7672 it->object = it->string;
7673 it->position = position;
7674 return 1;
7675 }
7676
7677
7678 /* Load IT with next display element from C string IT->s.
7679 IT->string_nchars is the maximum number of characters to return
7680 from the string. IT->end_charpos may be greater than
7681 IT->string_nchars when this function is called, in which case we
7682 may have to return padding spaces. Value is zero if end of string
7683 reached, including padding spaces. */
7684
7685 static int
7686 next_element_from_c_string (struct it *it)
7687 {
7688 int success_p = 1;
7689
7690 eassert (it->s);
7691 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7692 it->what = IT_CHARACTER;
7693 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7694 it->object = Qnil;
7695
7696 /* With bidi reordering, the character to display might not be the
7697 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7698 we were reseated to a new string, whose paragraph direction is
7699 not known. */
7700 if (it->bidi_p && it->bidi_it.first_elt)
7701 get_visually_first_element (it);
7702
7703 /* IT's position can be greater than IT->string_nchars in case a
7704 field width or precision has been specified when the iterator was
7705 initialized. */
7706 if (IT_CHARPOS (*it) >= it->end_charpos)
7707 {
7708 /* End of the game. */
7709 it->what = IT_EOB;
7710 success_p = 0;
7711 }
7712 else if (IT_CHARPOS (*it) >= it->string_nchars)
7713 {
7714 /* Pad with spaces. */
7715 it->c = ' ', it->len = 1;
7716 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7717 }
7718 else if (it->multibyte_p)
7719 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7720 else
7721 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7722
7723 return success_p;
7724 }
7725
7726
7727 /* Set up IT to return characters from an ellipsis, if appropriate.
7728 The definition of the ellipsis glyphs may come from a display table
7729 entry. This function fills IT with the first glyph from the
7730 ellipsis if an ellipsis is to be displayed. */
7731
7732 static int
7733 next_element_from_ellipsis (struct it *it)
7734 {
7735 if (it->selective_display_ellipsis_p)
7736 setup_for_ellipsis (it, it->len);
7737 else
7738 {
7739 /* The face at the current position may be different from the
7740 face we find after the invisible text. Remember what it
7741 was in IT->saved_face_id, and signal that it's there by
7742 setting face_before_selective_p. */
7743 it->saved_face_id = it->face_id;
7744 it->method = GET_FROM_BUFFER;
7745 it->object = it->w->buffer;
7746 reseat_at_next_visible_line_start (it, 1);
7747 it->face_before_selective_p = 1;
7748 }
7749
7750 return GET_NEXT_DISPLAY_ELEMENT (it);
7751 }
7752
7753
7754 /* Deliver an image display element. The iterator IT is already
7755 filled with image information (done in handle_display_prop). Value
7756 is always 1. */
7757
7758
7759 static int
7760 next_element_from_image (struct it *it)
7761 {
7762 it->what = IT_IMAGE;
7763 it->ignore_overlay_strings_at_pos_p = 0;
7764 return 1;
7765 }
7766
7767
7768 /* Fill iterator IT with next display element from a stretch glyph
7769 property. IT->object is the value of the text property. Value is
7770 always 1. */
7771
7772 static int
7773 next_element_from_stretch (struct it *it)
7774 {
7775 it->what = IT_STRETCH;
7776 return 1;
7777 }
7778
7779 /* Scan backwards from IT's current position until we find a stop
7780 position, or until BEGV. This is called when we find ourself
7781 before both the last known prev_stop and base_level_stop while
7782 reordering bidirectional text. */
7783
7784 static void
7785 compute_stop_pos_backwards (struct it *it)
7786 {
7787 const int SCAN_BACK_LIMIT = 1000;
7788 struct text_pos pos;
7789 struct display_pos save_current = it->current;
7790 struct text_pos save_position = it->position;
7791 ptrdiff_t charpos = IT_CHARPOS (*it);
7792 ptrdiff_t where_we_are = charpos;
7793 ptrdiff_t save_stop_pos = it->stop_charpos;
7794 ptrdiff_t save_end_pos = it->end_charpos;
7795
7796 eassert (NILP (it->string) && !it->s);
7797 eassert (it->bidi_p);
7798 it->bidi_p = 0;
7799 do
7800 {
7801 it->end_charpos = min (charpos + 1, ZV);
7802 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7803 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7804 reseat_1 (it, pos, 0);
7805 compute_stop_pos (it);
7806 /* We must advance forward, right? */
7807 if (it->stop_charpos <= charpos)
7808 emacs_abort ();
7809 }
7810 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7811
7812 if (it->stop_charpos <= where_we_are)
7813 it->prev_stop = it->stop_charpos;
7814 else
7815 it->prev_stop = BEGV;
7816 it->bidi_p = 1;
7817 it->current = save_current;
7818 it->position = save_position;
7819 it->stop_charpos = save_stop_pos;
7820 it->end_charpos = save_end_pos;
7821 }
7822
7823 /* Scan forward from CHARPOS in the current buffer/string, until we
7824 find a stop position > current IT's position. Then handle the stop
7825 position before that. This is called when we bump into a stop
7826 position while reordering bidirectional text. CHARPOS should be
7827 the last previously processed stop_pos (or BEGV/0, if none were
7828 processed yet) whose position is less that IT's current
7829 position. */
7830
7831 static void
7832 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7833 {
7834 int bufp = !STRINGP (it->string);
7835 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7836 struct display_pos save_current = it->current;
7837 struct text_pos save_position = it->position;
7838 struct text_pos pos1;
7839 ptrdiff_t next_stop;
7840
7841 /* Scan in strict logical order. */
7842 eassert (it->bidi_p);
7843 it->bidi_p = 0;
7844 do
7845 {
7846 it->prev_stop = charpos;
7847 if (bufp)
7848 {
7849 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7850 reseat_1 (it, pos1, 0);
7851 }
7852 else
7853 it->current.string_pos = string_pos (charpos, it->string);
7854 compute_stop_pos (it);
7855 /* We must advance forward, right? */
7856 if (it->stop_charpos <= it->prev_stop)
7857 emacs_abort ();
7858 charpos = it->stop_charpos;
7859 }
7860 while (charpos <= where_we_are);
7861
7862 it->bidi_p = 1;
7863 it->current = save_current;
7864 it->position = save_position;
7865 next_stop = it->stop_charpos;
7866 it->stop_charpos = it->prev_stop;
7867 handle_stop (it);
7868 it->stop_charpos = next_stop;
7869 }
7870
7871 /* Load IT with the next display element from current_buffer. Value
7872 is zero if end of buffer reached. IT->stop_charpos is the next
7873 position at which to stop and check for text properties or buffer
7874 end. */
7875
7876 static int
7877 next_element_from_buffer (struct it *it)
7878 {
7879 int success_p = 1;
7880
7881 eassert (IT_CHARPOS (*it) >= BEGV);
7882 eassert (NILP (it->string) && !it->s);
7883 eassert (!it->bidi_p
7884 || (EQ (it->bidi_it.string.lstring, Qnil)
7885 && it->bidi_it.string.s == NULL));
7886
7887 /* With bidi reordering, the character to display might not be the
7888 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7889 we were reseat()ed to a new buffer position, which is potentially
7890 a different paragraph. */
7891 if (it->bidi_p && it->bidi_it.first_elt)
7892 {
7893 get_visually_first_element (it);
7894 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7895 }
7896
7897 if (IT_CHARPOS (*it) >= it->stop_charpos)
7898 {
7899 if (IT_CHARPOS (*it) >= it->end_charpos)
7900 {
7901 int overlay_strings_follow_p;
7902
7903 /* End of the game, except when overlay strings follow that
7904 haven't been returned yet. */
7905 if (it->overlay_strings_at_end_processed_p)
7906 overlay_strings_follow_p = 0;
7907 else
7908 {
7909 it->overlay_strings_at_end_processed_p = 1;
7910 overlay_strings_follow_p = get_overlay_strings (it, 0);
7911 }
7912
7913 if (overlay_strings_follow_p)
7914 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7915 else
7916 {
7917 it->what = IT_EOB;
7918 it->position = it->current.pos;
7919 success_p = 0;
7920 }
7921 }
7922 else if (!(!it->bidi_p
7923 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7924 || IT_CHARPOS (*it) == it->stop_charpos))
7925 {
7926 /* With bidi non-linear iteration, we could find ourselves
7927 far beyond the last computed stop_charpos, with several
7928 other stop positions in between that we missed. Scan
7929 them all now, in buffer's logical order, until we find
7930 and handle the last stop_charpos that precedes our
7931 current position. */
7932 handle_stop_backwards (it, it->stop_charpos);
7933 return GET_NEXT_DISPLAY_ELEMENT (it);
7934 }
7935 else
7936 {
7937 if (it->bidi_p)
7938 {
7939 /* Take note of the stop position we just moved across,
7940 for when we will move back across it. */
7941 it->prev_stop = it->stop_charpos;
7942 /* If we are at base paragraph embedding level, take
7943 note of the last stop position seen at this
7944 level. */
7945 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7946 it->base_level_stop = it->stop_charpos;
7947 }
7948 handle_stop (it);
7949 return GET_NEXT_DISPLAY_ELEMENT (it);
7950 }
7951 }
7952 else if (it->bidi_p
7953 /* If we are before prev_stop, we may have overstepped on
7954 our way backwards a stop_pos, and if so, we need to
7955 handle that stop_pos. */
7956 && IT_CHARPOS (*it) < it->prev_stop
7957 /* We can sometimes back up for reasons that have nothing
7958 to do with bidi reordering. E.g., compositions. The
7959 code below is only needed when we are above the base
7960 embedding level, so test for that explicitly. */
7961 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7962 {
7963 if (it->base_level_stop <= 0
7964 || IT_CHARPOS (*it) < it->base_level_stop)
7965 {
7966 /* If we lost track of base_level_stop, we need to find
7967 prev_stop by looking backwards. This happens, e.g., when
7968 we were reseated to the previous screenful of text by
7969 vertical-motion. */
7970 it->base_level_stop = BEGV;
7971 compute_stop_pos_backwards (it);
7972 handle_stop_backwards (it, it->prev_stop);
7973 }
7974 else
7975 handle_stop_backwards (it, it->base_level_stop);
7976 return GET_NEXT_DISPLAY_ELEMENT (it);
7977 }
7978 else
7979 {
7980 /* No face changes, overlays etc. in sight, so just return a
7981 character from current_buffer. */
7982 unsigned char *p;
7983 ptrdiff_t stop;
7984
7985 /* Maybe run the redisplay end trigger hook. Performance note:
7986 This doesn't seem to cost measurable time. */
7987 if (it->redisplay_end_trigger_charpos
7988 && it->glyph_row
7989 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7990 run_redisplay_end_trigger_hook (it);
7991
7992 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7993 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7994 stop)
7995 && next_element_from_composition (it))
7996 {
7997 return 1;
7998 }
7999
8000 /* Get the next character, maybe multibyte. */
8001 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8002 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8003 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8004 else
8005 it->c = *p, it->len = 1;
8006
8007 /* Record what we have and where it came from. */
8008 it->what = IT_CHARACTER;
8009 it->object = it->w->buffer;
8010 it->position = it->current.pos;
8011
8012 /* Normally we return the character found above, except when we
8013 really want to return an ellipsis for selective display. */
8014 if (it->selective)
8015 {
8016 if (it->c == '\n')
8017 {
8018 /* A value of selective > 0 means hide lines indented more
8019 than that number of columns. */
8020 if (it->selective > 0
8021 && IT_CHARPOS (*it) + 1 < ZV
8022 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8023 IT_BYTEPOS (*it) + 1,
8024 it->selective))
8025 {
8026 success_p = next_element_from_ellipsis (it);
8027 it->dpvec_char_len = -1;
8028 }
8029 }
8030 else if (it->c == '\r' && it->selective == -1)
8031 {
8032 /* A value of selective == -1 means that everything from the
8033 CR to the end of the line is invisible, with maybe an
8034 ellipsis displayed for it. */
8035 success_p = next_element_from_ellipsis (it);
8036 it->dpvec_char_len = -1;
8037 }
8038 }
8039 }
8040
8041 /* Value is zero if end of buffer reached. */
8042 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8043 return success_p;
8044 }
8045
8046
8047 /* Run the redisplay end trigger hook for IT. */
8048
8049 static void
8050 run_redisplay_end_trigger_hook (struct it *it)
8051 {
8052 Lisp_Object args[3];
8053
8054 /* IT->glyph_row should be non-null, i.e. we should be actually
8055 displaying something, or otherwise we should not run the hook. */
8056 eassert (it->glyph_row);
8057
8058 /* Set up hook arguments. */
8059 args[0] = Qredisplay_end_trigger_functions;
8060 args[1] = it->window;
8061 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8062 it->redisplay_end_trigger_charpos = 0;
8063
8064 /* Since we are *trying* to run these functions, don't try to run
8065 them again, even if they get an error. */
8066 wset_redisplay_end_trigger (it->w, Qnil);
8067 Frun_hook_with_args (3, args);
8068
8069 /* Notice if it changed the face of the character we are on. */
8070 handle_face_prop (it);
8071 }
8072
8073
8074 /* Deliver a composition display element. Unlike the other
8075 next_element_from_XXX, this function is not registered in the array
8076 get_next_element[]. It is called from next_element_from_buffer and
8077 next_element_from_string when necessary. */
8078
8079 static int
8080 next_element_from_composition (struct it *it)
8081 {
8082 it->what = IT_COMPOSITION;
8083 it->len = it->cmp_it.nbytes;
8084 if (STRINGP (it->string))
8085 {
8086 if (it->c < 0)
8087 {
8088 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8089 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8090 return 0;
8091 }
8092 it->position = it->current.string_pos;
8093 it->object = it->string;
8094 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8095 IT_STRING_BYTEPOS (*it), it->string);
8096 }
8097 else
8098 {
8099 if (it->c < 0)
8100 {
8101 IT_CHARPOS (*it) += it->cmp_it.nchars;
8102 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8103 if (it->bidi_p)
8104 {
8105 if (it->bidi_it.new_paragraph)
8106 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8107 /* Resync the bidi iterator with IT's new position.
8108 FIXME: this doesn't support bidirectional text. */
8109 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8110 bidi_move_to_visually_next (&it->bidi_it);
8111 }
8112 return 0;
8113 }
8114 it->position = it->current.pos;
8115 it->object = it->w->buffer;
8116 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8117 IT_BYTEPOS (*it), Qnil);
8118 }
8119 return 1;
8120 }
8121
8122
8123 \f
8124 /***********************************************************************
8125 Moving an iterator without producing glyphs
8126 ***********************************************************************/
8127
8128 /* Check if iterator is at a position corresponding to a valid buffer
8129 position after some move_it_ call. */
8130
8131 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8132 ((it)->method == GET_FROM_STRING \
8133 ? IT_STRING_CHARPOS (*it) == 0 \
8134 : 1)
8135
8136
8137 /* Move iterator IT to a specified buffer or X position within one
8138 line on the display without producing glyphs.
8139
8140 OP should be a bit mask including some or all of these bits:
8141 MOVE_TO_X: Stop upon reaching x-position TO_X.
8142 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8143 Regardless of OP's value, stop upon reaching the end of the display line.
8144
8145 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8146 This means, in particular, that TO_X includes window's horizontal
8147 scroll amount.
8148
8149 The return value has several possible values that
8150 say what condition caused the scan to stop:
8151
8152 MOVE_POS_MATCH_OR_ZV
8153 - when TO_POS or ZV was reached.
8154
8155 MOVE_X_REACHED
8156 -when TO_X was reached before TO_POS or ZV were reached.
8157
8158 MOVE_LINE_CONTINUED
8159 - when we reached the end of the display area and the line must
8160 be continued.
8161
8162 MOVE_LINE_TRUNCATED
8163 - when we reached the end of the display area and the line is
8164 truncated.
8165
8166 MOVE_NEWLINE_OR_CR
8167 - when we stopped at a line end, i.e. a newline or a CR and selective
8168 display is on. */
8169
8170 static enum move_it_result
8171 move_it_in_display_line_to (struct it *it,
8172 ptrdiff_t to_charpos, int to_x,
8173 enum move_operation_enum op)
8174 {
8175 enum move_it_result result = MOVE_UNDEFINED;
8176 struct glyph_row *saved_glyph_row;
8177 struct it wrap_it, atpos_it, atx_it, ppos_it;
8178 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8179 void *ppos_data = NULL;
8180 int may_wrap = 0;
8181 enum it_method prev_method = it->method;
8182 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8183 int saw_smaller_pos = prev_pos < to_charpos;
8184
8185 /* Don't produce glyphs in produce_glyphs. */
8186 saved_glyph_row = it->glyph_row;
8187 it->glyph_row = NULL;
8188
8189 /* Use wrap_it to save a copy of IT wherever a word wrap could
8190 occur. Use atpos_it to save a copy of IT at the desired buffer
8191 position, if found, so that we can scan ahead and check if the
8192 word later overshoots the window edge. Use atx_it similarly, for
8193 pixel positions. */
8194 wrap_it.sp = -1;
8195 atpos_it.sp = -1;
8196 atx_it.sp = -1;
8197
8198 /* Use ppos_it under bidi reordering to save a copy of IT for the
8199 position > CHARPOS that is the closest to CHARPOS. We restore
8200 that position in IT when we have scanned the entire display line
8201 without finding a match for CHARPOS and all the character
8202 positions are greater than CHARPOS. */
8203 if (it->bidi_p)
8204 {
8205 SAVE_IT (ppos_it, *it, ppos_data);
8206 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8207 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8208 SAVE_IT (ppos_it, *it, ppos_data);
8209 }
8210
8211 #define BUFFER_POS_REACHED_P() \
8212 ((op & MOVE_TO_POS) != 0 \
8213 && BUFFERP (it->object) \
8214 && (IT_CHARPOS (*it) == to_charpos \
8215 || ((!it->bidi_p \
8216 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8217 && IT_CHARPOS (*it) > to_charpos) \
8218 || (it->what == IT_COMPOSITION \
8219 && ((IT_CHARPOS (*it) > to_charpos \
8220 && to_charpos >= it->cmp_it.charpos) \
8221 || (IT_CHARPOS (*it) < to_charpos \
8222 && to_charpos <= it->cmp_it.charpos)))) \
8223 && (it->method == GET_FROM_BUFFER \
8224 || (it->method == GET_FROM_DISPLAY_VECTOR \
8225 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8226
8227 /* If there's a line-/wrap-prefix, handle it. */
8228 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8229 && it->current_y < it->last_visible_y)
8230 handle_line_prefix (it);
8231
8232 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8233 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8234
8235 while (1)
8236 {
8237 int x, i, ascent = 0, descent = 0;
8238
8239 /* Utility macro to reset an iterator with x, ascent, and descent. */
8240 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8241 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8242 (IT)->max_descent = descent)
8243
8244 /* Stop if we move beyond TO_CHARPOS (after an image or a
8245 display string or stretch glyph). */
8246 if ((op & MOVE_TO_POS) != 0
8247 && BUFFERP (it->object)
8248 && it->method == GET_FROM_BUFFER
8249 && (((!it->bidi_p
8250 /* When the iterator is at base embedding level, we
8251 are guaranteed that characters are delivered for
8252 display in strictly increasing order of their
8253 buffer positions. */
8254 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8255 && IT_CHARPOS (*it) > to_charpos)
8256 || (it->bidi_p
8257 && (prev_method == GET_FROM_IMAGE
8258 || prev_method == GET_FROM_STRETCH
8259 || prev_method == GET_FROM_STRING)
8260 /* Passed TO_CHARPOS from left to right. */
8261 && ((prev_pos < to_charpos
8262 && IT_CHARPOS (*it) > to_charpos)
8263 /* Passed TO_CHARPOS from right to left. */
8264 || (prev_pos > to_charpos
8265 && IT_CHARPOS (*it) < to_charpos)))))
8266 {
8267 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8268 {
8269 result = MOVE_POS_MATCH_OR_ZV;
8270 break;
8271 }
8272 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8273 /* If wrap_it is valid, the current position might be in a
8274 word that is wrapped. So, save the iterator in
8275 atpos_it and continue to see if wrapping happens. */
8276 SAVE_IT (atpos_it, *it, atpos_data);
8277 }
8278
8279 /* Stop when ZV reached.
8280 We used to stop here when TO_CHARPOS reached as well, but that is
8281 too soon if this glyph does not fit on this line. So we handle it
8282 explicitly below. */
8283 if (!get_next_display_element (it))
8284 {
8285 result = MOVE_POS_MATCH_OR_ZV;
8286 break;
8287 }
8288
8289 if (it->line_wrap == TRUNCATE)
8290 {
8291 if (BUFFER_POS_REACHED_P ())
8292 {
8293 result = MOVE_POS_MATCH_OR_ZV;
8294 break;
8295 }
8296 }
8297 else
8298 {
8299 if (it->line_wrap == WORD_WRAP)
8300 {
8301 if (IT_DISPLAYING_WHITESPACE (it))
8302 may_wrap = 1;
8303 else if (may_wrap)
8304 {
8305 /* We have reached a glyph that follows one or more
8306 whitespace characters. If the position is
8307 already found, we are done. */
8308 if (atpos_it.sp >= 0)
8309 {
8310 RESTORE_IT (it, &atpos_it, atpos_data);
8311 result = MOVE_POS_MATCH_OR_ZV;
8312 goto done;
8313 }
8314 if (atx_it.sp >= 0)
8315 {
8316 RESTORE_IT (it, &atx_it, atx_data);
8317 result = MOVE_X_REACHED;
8318 goto done;
8319 }
8320 /* Otherwise, we can wrap here. */
8321 SAVE_IT (wrap_it, *it, wrap_data);
8322 may_wrap = 0;
8323 }
8324 }
8325 }
8326
8327 /* Remember the line height for the current line, in case
8328 the next element doesn't fit on the line. */
8329 ascent = it->max_ascent;
8330 descent = it->max_descent;
8331
8332 /* The call to produce_glyphs will get the metrics of the
8333 display element IT is loaded with. Record the x-position
8334 before this display element, in case it doesn't fit on the
8335 line. */
8336 x = it->current_x;
8337
8338 PRODUCE_GLYPHS (it);
8339
8340 if (it->area != TEXT_AREA)
8341 {
8342 prev_method = it->method;
8343 if (it->method == GET_FROM_BUFFER)
8344 prev_pos = IT_CHARPOS (*it);
8345 set_iterator_to_next (it, 1);
8346 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8347 SET_TEXT_POS (this_line_min_pos,
8348 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8349 if (it->bidi_p
8350 && (op & MOVE_TO_POS)
8351 && IT_CHARPOS (*it) > to_charpos
8352 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8353 SAVE_IT (ppos_it, *it, ppos_data);
8354 continue;
8355 }
8356
8357 /* The number of glyphs we get back in IT->nglyphs will normally
8358 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8359 character on a terminal frame, or (iii) a line end. For the
8360 second case, IT->nglyphs - 1 padding glyphs will be present.
8361 (On X frames, there is only one glyph produced for a
8362 composite character.)
8363
8364 The behavior implemented below means, for continuation lines,
8365 that as many spaces of a TAB as fit on the current line are
8366 displayed there. For terminal frames, as many glyphs of a
8367 multi-glyph character are displayed in the current line, too.
8368 This is what the old redisplay code did, and we keep it that
8369 way. Under X, the whole shape of a complex character must
8370 fit on the line or it will be completely displayed in the
8371 next line.
8372
8373 Note that both for tabs and padding glyphs, all glyphs have
8374 the same width. */
8375 if (it->nglyphs)
8376 {
8377 /* More than one glyph or glyph doesn't fit on line. All
8378 glyphs have the same width. */
8379 int single_glyph_width = it->pixel_width / it->nglyphs;
8380 int new_x;
8381 int x_before_this_char = x;
8382 int hpos_before_this_char = it->hpos;
8383
8384 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8385 {
8386 new_x = x + single_glyph_width;
8387
8388 /* We want to leave anything reaching TO_X to the caller. */
8389 if ((op & MOVE_TO_X) && new_x > to_x)
8390 {
8391 if (BUFFER_POS_REACHED_P ())
8392 {
8393 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8394 goto buffer_pos_reached;
8395 if (atpos_it.sp < 0)
8396 {
8397 SAVE_IT (atpos_it, *it, atpos_data);
8398 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8399 }
8400 }
8401 else
8402 {
8403 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8404 {
8405 it->current_x = x;
8406 result = MOVE_X_REACHED;
8407 break;
8408 }
8409 if (atx_it.sp < 0)
8410 {
8411 SAVE_IT (atx_it, *it, atx_data);
8412 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8413 }
8414 }
8415 }
8416
8417 if (/* Lines are continued. */
8418 it->line_wrap != TRUNCATE
8419 && (/* And glyph doesn't fit on the line. */
8420 new_x > it->last_visible_x
8421 /* Or it fits exactly and we're on a window
8422 system frame. */
8423 || (new_x == it->last_visible_x
8424 && FRAME_WINDOW_P (it->f)
8425 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8426 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8427 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8428 {
8429 if (/* IT->hpos == 0 means the very first glyph
8430 doesn't fit on the line, e.g. a wide image. */
8431 it->hpos == 0
8432 || (new_x == it->last_visible_x
8433 && FRAME_WINDOW_P (it->f)))
8434 {
8435 ++it->hpos;
8436 it->current_x = new_x;
8437
8438 /* The character's last glyph just barely fits
8439 in this row. */
8440 if (i == it->nglyphs - 1)
8441 {
8442 /* If this is the destination position,
8443 return a position *before* it in this row,
8444 now that we know it fits in this row. */
8445 if (BUFFER_POS_REACHED_P ())
8446 {
8447 if (it->line_wrap != WORD_WRAP
8448 || wrap_it.sp < 0)
8449 {
8450 it->hpos = hpos_before_this_char;
8451 it->current_x = x_before_this_char;
8452 result = MOVE_POS_MATCH_OR_ZV;
8453 break;
8454 }
8455 if (it->line_wrap == WORD_WRAP
8456 && atpos_it.sp < 0)
8457 {
8458 SAVE_IT (atpos_it, *it, atpos_data);
8459 atpos_it.current_x = x_before_this_char;
8460 atpos_it.hpos = hpos_before_this_char;
8461 }
8462 }
8463
8464 prev_method = it->method;
8465 if (it->method == GET_FROM_BUFFER)
8466 prev_pos = IT_CHARPOS (*it);
8467 set_iterator_to_next (it, 1);
8468 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8469 SET_TEXT_POS (this_line_min_pos,
8470 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8471 /* On graphical terminals, newlines may
8472 "overflow" into the fringe if
8473 overflow-newline-into-fringe is non-nil.
8474 On text terminals, and on graphical
8475 terminals with no right margin, newlines
8476 may overflow into the last glyph on the
8477 display line.*/
8478 if (!FRAME_WINDOW_P (it->f)
8479 || ((it->bidi_p
8480 && it->bidi_it.paragraph_dir == R2L)
8481 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8482 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8483 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8484 {
8485 if (!get_next_display_element (it))
8486 {
8487 result = MOVE_POS_MATCH_OR_ZV;
8488 break;
8489 }
8490 if (BUFFER_POS_REACHED_P ())
8491 {
8492 if (ITERATOR_AT_END_OF_LINE_P (it))
8493 result = MOVE_POS_MATCH_OR_ZV;
8494 else
8495 result = MOVE_LINE_CONTINUED;
8496 break;
8497 }
8498 if (ITERATOR_AT_END_OF_LINE_P (it))
8499 {
8500 result = MOVE_NEWLINE_OR_CR;
8501 break;
8502 }
8503 }
8504 }
8505 }
8506 else
8507 IT_RESET_X_ASCENT_DESCENT (it);
8508
8509 if (wrap_it.sp >= 0)
8510 {
8511 RESTORE_IT (it, &wrap_it, wrap_data);
8512 atpos_it.sp = -1;
8513 atx_it.sp = -1;
8514 }
8515
8516 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8517 IT_CHARPOS (*it)));
8518 result = MOVE_LINE_CONTINUED;
8519 break;
8520 }
8521
8522 if (BUFFER_POS_REACHED_P ())
8523 {
8524 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8525 goto buffer_pos_reached;
8526 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8527 {
8528 SAVE_IT (atpos_it, *it, atpos_data);
8529 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8530 }
8531 }
8532
8533 if (new_x > it->first_visible_x)
8534 {
8535 /* Glyph is visible. Increment number of glyphs that
8536 would be displayed. */
8537 ++it->hpos;
8538 }
8539 }
8540
8541 if (result != MOVE_UNDEFINED)
8542 break;
8543 }
8544 else if (BUFFER_POS_REACHED_P ())
8545 {
8546 buffer_pos_reached:
8547 IT_RESET_X_ASCENT_DESCENT (it);
8548 result = MOVE_POS_MATCH_OR_ZV;
8549 break;
8550 }
8551 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8552 {
8553 /* Stop when TO_X specified and reached. This check is
8554 necessary here because of lines consisting of a line end,
8555 only. The line end will not produce any glyphs and we
8556 would never get MOVE_X_REACHED. */
8557 eassert (it->nglyphs == 0);
8558 result = MOVE_X_REACHED;
8559 break;
8560 }
8561
8562 /* Is this a line end? If yes, we're done. */
8563 if (ITERATOR_AT_END_OF_LINE_P (it))
8564 {
8565 /* If we are past TO_CHARPOS, but never saw any character
8566 positions smaller than TO_CHARPOS, return
8567 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8568 did. */
8569 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8570 {
8571 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8572 {
8573 if (IT_CHARPOS (ppos_it) < ZV)
8574 {
8575 RESTORE_IT (it, &ppos_it, ppos_data);
8576 result = MOVE_POS_MATCH_OR_ZV;
8577 }
8578 else
8579 goto buffer_pos_reached;
8580 }
8581 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8582 && IT_CHARPOS (*it) > to_charpos)
8583 goto buffer_pos_reached;
8584 else
8585 result = MOVE_NEWLINE_OR_CR;
8586 }
8587 else
8588 result = MOVE_NEWLINE_OR_CR;
8589 break;
8590 }
8591
8592 prev_method = it->method;
8593 if (it->method == GET_FROM_BUFFER)
8594 prev_pos = IT_CHARPOS (*it);
8595 /* The current display element has been consumed. Advance
8596 to the next. */
8597 set_iterator_to_next (it, 1);
8598 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8599 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8600 if (IT_CHARPOS (*it) < to_charpos)
8601 saw_smaller_pos = 1;
8602 if (it->bidi_p
8603 && (op & MOVE_TO_POS)
8604 && IT_CHARPOS (*it) >= to_charpos
8605 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8606 SAVE_IT (ppos_it, *it, ppos_data);
8607
8608 /* Stop if lines are truncated and IT's current x-position is
8609 past the right edge of the window now. */
8610 if (it->line_wrap == TRUNCATE
8611 && it->current_x >= it->last_visible_x)
8612 {
8613 if (!FRAME_WINDOW_P (it->f)
8614 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8617 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8618 {
8619 int at_eob_p = 0;
8620
8621 if ((at_eob_p = !get_next_display_element (it))
8622 || BUFFER_POS_REACHED_P ()
8623 /* If we are past TO_CHARPOS, but never saw any
8624 character positions smaller than TO_CHARPOS,
8625 return MOVE_POS_MATCH_OR_ZV, like the
8626 unidirectional display did. */
8627 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8628 && !saw_smaller_pos
8629 && IT_CHARPOS (*it) > to_charpos))
8630 {
8631 if (it->bidi_p
8632 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8633 RESTORE_IT (it, &ppos_it, ppos_data);
8634 result = MOVE_POS_MATCH_OR_ZV;
8635 break;
8636 }
8637 if (ITERATOR_AT_END_OF_LINE_P (it))
8638 {
8639 result = MOVE_NEWLINE_OR_CR;
8640 break;
8641 }
8642 }
8643 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8644 && !saw_smaller_pos
8645 && IT_CHARPOS (*it) > to_charpos)
8646 {
8647 if (IT_CHARPOS (ppos_it) < ZV)
8648 RESTORE_IT (it, &ppos_it, ppos_data);
8649 result = MOVE_POS_MATCH_OR_ZV;
8650 break;
8651 }
8652 result = MOVE_LINE_TRUNCATED;
8653 break;
8654 }
8655 #undef IT_RESET_X_ASCENT_DESCENT
8656 }
8657
8658 #undef BUFFER_POS_REACHED_P
8659
8660 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8661 restore the saved iterator. */
8662 if (atpos_it.sp >= 0)
8663 RESTORE_IT (it, &atpos_it, atpos_data);
8664 else if (atx_it.sp >= 0)
8665 RESTORE_IT (it, &atx_it, atx_data);
8666
8667 done:
8668
8669 if (atpos_data)
8670 bidi_unshelve_cache (atpos_data, 1);
8671 if (atx_data)
8672 bidi_unshelve_cache (atx_data, 1);
8673 if (wrap_data)
8674 bidi_unshelve_cache (wrap_data, 1);
8675 if (ppos_data)
8676 bidi_unshelve_cache (ppos_data, 1);
8677
8678 /* Restore the iterator settings altered at the beginning of this
8679 function. */
8680 it->glyph_row = saved_glyph_row;
8681 return result;
8682 }
8683
8684 /* For external use. */
8685 void
8686 move_it_in_display_line (struct it *it,
8687 ptrdiff_t to_charpos, int to_x,
8688 enum move_operation_enum op)
8689 {
8690 if (it->line_wrap == WORD_WRAP
8691 && (op & MOVE_TO_X))
8692 {
8693 struct it save_it;
8694 void *save_data = NULL;
8695 int skip;
8696
8697 SAVE_IT (save_it, *it, save_data);
8698 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8699 /* When word-wrap is on, TO_X may lie past the end
8700 of a wrapped line. Then it->current is the
8701 character on the next line, so backtrack to the
8702 space before the wrap point. */
8703 if (skip == MOVE_LINE_CONTINUED)
8704 {
8705 int prev_x = max (it->current_x - 1, 0);
8706 RESTORE_IT (it, &save_it, save_data);
8707 move_it_in_display_line_to
8708 (it, -1, prev_x, MOVE_TO_X);
8709 }
8710 else
8711 bidi_unshelve_cache (save_data, 1);
8712 }
8713 else
8714 move_it_in_display_line_to (it, to_charpos, to_x, op);
8715 }
8716
8717
8718 /* Move IT forward until it satisfies one or more of the criteria in
8719 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8720
8721 OP is a bit-mask that specifies where to stop, and in particular,
8722 which of those four position arguments makes a difference. See the
8723 description of enum move_operation_enum.
8724
8725 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8726 screen line, this function will set IT to the next position that is
8727 displayed to the right of TO_CHARPOS on the screen. */
8728
8729 void
8730 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8731 {
8732 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8733 int line_height, line_start_x = 0, reached = 0;
8734 void *backup_data = NULL;
8735
8736 for (;;)
8737 {
8738 if (op & MOVE_TO_VPOS)
8739 {
8740 /* If no TO_CHARPOS and no TO_X specified, stop at the
8741 start of the line TO_VPOS. */
8742 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8743 {
8744 if (it->vpos == to_vpos)
8745 {
8746 reached = 1;
8747 break;
8748 }
8749 else
8750 skip = move_it_in_display_line_to (it, -1, -1, 0);
8751 }
8752 else
8753 {
8754 /* TO_VPOS >= 0 means stop at TO_X in the line at
8755 TO_VPOS, or at TO_POS, whichever comes first. */
8756 if (it->vpos == to_vpos)
8757 {
8758 reached = 2;
8759 break;
8760 }
8761
8762 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8763
8764 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8765 {
8766 reached = 3;
8767 break;
8768 }
8769 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8770 {
8771 /* We have reached TO_X but not in the line we want. */
8772 skip = move_it_in_display_line_to (it, to_charpos,
8773 -1, MOVE_TO_POS);
8774 if (skip == MOVE_POS_MATCH_OR_ZV)
8775 {
8776 reached = 4;
8777 break;
8778 }
8779 }
8780 }
8781 }
8782 else if (op & MOVE_TO_Y)
8783 {
8784 struct it it_backup;
8785
8786 if (it->line_wrap == WORD_WRAP)
8787 SAVE_IT (it_backup, *it, backup_data);
8788
8789 /* TO_Y specified means stop at TO_X in the line containing
8790 TO_Y---or at TO_CHARPOS if this is reached first. The
8791 problem is that we can't really tell whether the line
8792 contains TO_Y before we have completely scanned it, and
8793 this may skip past TO_X. What we do is to first scan to
8794 TO_X.
8795
8796 If TO_X is not specified, use a TO_X of zero. The reason
8797 is to make the outcome of this function more predictable.
8798 If we didn't use TO_X == 0, we would stop at the end of
8799 the line which is probably not what a caller would expect
8800 to happen. */
8801 skip = move_it_in_display_line_to
8802 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8803 (MOVE_TO_X | (op & MOVE_TO_POS)));
8804
8805 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8806 if (skip == MOVE_POS_MATCH_OR_ZV)
8807 reached = 5;
8808 else if (skip == MOVE_X_REACHED)
8809 {
8810 /* If TO_X was reached, we want to know whether TO_Y is
8811 in the line. We know this is the case if the already
8812 scanned glyphs make the line tall enough. Otherwise,
8813 we must check by scanning the rest of the line. */
8814 line_height = it->max_ascent + it->max_descent;
8815 if (to_y >= it->current_y
8816 && to_y < it->current_y + line_height)
8817 {
8818 reached = 6;
8819 break;
8820 }
8821 SAVE_IT (it_backup, *it, backup_data);
8822 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8823 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8824 op & MOVE_TO_POS);
8825 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8826 line_height = it->max_ascent + it->max_descent;
8827 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8828
8829 if (to_y >= it->current_y
8830 && to_y < it->current_y + line_height)
8831 {
8832 /* If TO_Y is in this line and TO_X was reached
8833 above, we scanned too far. We have to restore
8834 IT's settings to the ones before skipping. But
8835 keep the more accurate values of max_ascent and
8836 max_descent we've found while skipping the rest
8837 of the line, for the sake of callers, such as
8838 pos_visible_p, that need to know the line
8839 height. */
8840 int max_ascent = it->max_ascent;
8841 int max_descent = it->max_descent;
8842
8843 RESTORE_IT (it, &it_backup, backup_data);
8844 it->max_ascent = max_ascent;
8845 it->max_descent = max_descent;
8846 reached = 6;
8847 }
8848 else
8849 {
8850 skip = skip2;
8851 if (skip == MOVE_POS_MATCH_OR_ZV)
8852 reached = 7;
8853 }
8854 }
8855 else
8856 {
8857 /* Check whether TO_Y is in this line. */
8858 line_height = it->max_ascent + it->max_descent;
8859 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8860
8861 if (to_y >= it->current_y
8862 && to_y < it->current_y + line_height)
8863 {
8864 /* When word-wrap is on, TO_X may lie past the end
8865 of a wrapped line. Then it->current is the
8866 character on the next line, so backtrack to the
8867 space before the wrap point. */
8868 if (skip == MOVE_LINE_CONTINUED
8869 && it->line_wrap == WORD_WRAP)
8870 {
8871 int prev_x = max (it->current_x - 1, 0);
8872 RESTORE_IT (it, &it_backup, backup_data);
8873 skip = move_it_in_display_line_to
8874 (it, -1, prev_x, MOVE_TO_X);
8875 }
8876 reached = 6;
8877 }
8878 }
8879
8880 if (reached)
8881 break;
8882 }
8883 else if (BUFFERP (it->object)
8884 && (it->method == GET_FROM_BUFFER
8885 || it->method == GET_FROM_STRETCH)
8886 && IT_CHARPOS (*it) >= to_charpos
8887 /* Under bidi iteration, a call to set_iterator_to_next
8888 can scan far beyond to_charpos if the initial
8889 portion of the next line needs to be reordered. In
8890 that case, give move_it_in_display_line_to another
8891 chance below. */
8892 && !(it->bidi_p
8893 && it->bidi_it.scan_dir == -1))
8894 skip = MOVE_POS_MATCH_OR_ZV;
8895 else
8896 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8897
8898 switch (skip)
8899 {
8900 case MOVE_POS_MATCH_OR_ZV:
8901 reached = 8;
8902 goto out;
8903
8904 case MOVE_NEWLINE_OR_CR:
8905 set_iterator_to_next (it, 1);
8906 it->continuation_lines_width = 0;
8907 break;
8908
8909 case MOVE_LINE_TRUNCATED:
8910 it->continuation_lines_width = 0;
8911 reseat_at_next_visible_line_start (it, 0);
8912 if ((op & MOVE_TO_POS) != 0
8913 && IT_CHARPOS (*it) > to_charpos)
8914 {
8915 reached = 9;
8916 goto out;
8917 }
8918 break;
8919
8920 case MOVE_LINE_CONTINUED:
8921 /* For continued lines ending in a tab, some of the glyphs
8922 associated with the tab are displayed on the current
8923 line. Since it->current_x does not include these glyphs,
8924 we use it->last_visible_x instead. */
8925 if (it->c == '\t')
8926 {
8927 it->continuation_lines_width += it->last_visible_x;
8928 /* When moving by vpos, ensure that the iterator really
8929 advances to the next line (bug#847, bug#969). Fixme:
8930 do we need to do this in other circumstances? */
8931 if (it->current_x != it->last_visible_x
8932 && (op & MOVE_TO_VPOS)
8933 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8934 {
8935 line_start_x = it->current_x + it->pixel_width
8936 - it->last_visible_x;
8937 set_iterator_to_next (it, 0);
8938 }
8939 }
8940 else
8941 it->continuation_lines_width += it->current_x;
8942 break;
8943
8944 default:
8945 emacs_abort ();
8946 }
8947
8948 /* Reset/increment for the next run. */
8949 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8950 it->current_x = line_start_x;
8951 line_start_x = 0;
8952 it->hpos = 0;
8953 it->current_y += it->max_ascent + it->max_descent;
8954 ++it->vpos;
8955 last_height = it->max_ascent + it->max_descent;
8956 last_max_ascent = it->max_ascent;
8957 it->max_ascent = it->max_descent = 0;
8958 }
8959
8960 out:
8961
8962 /* On text terminals, we may stop at the end of a line in the middle
8963 of a multi-character glyph. If the glyph itself is continued,
8964 i.e. it is actually displayed on the next line, don't treat this
8965 stopping point as valid; move to the next line instead (unless
8966 that brings us offscreen). */
8967 if (!FRAME_WINDOW_P (it->f)
8968 && op & MOVE_TO_POS
8969 && IT_CHARPOS (*it) == to_charpos
8970 && it->what == IT_CHARACTER
8971 && it->nglyphs > 1
8972 && it->line_wrap == WINDOW_WRAP
8973 && it->current_x == it->last_visible_x - 1
8974 && it->c != '\n'
8975 && it->c != '\t'
8976 && it->vpos < XFASTINT (it->w->window_end_vpos))
8977 {
8978 it->continuation_lines_width += it->current_x;
8979 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8980 it->current_y += it->max_ascent + it->max_descent;
8981 ++it->vpos;
8982 last_height = it->max_ascent + it->max_descent;
8983 last_max_ascent = it->max_ascent;
8984 }
8985
8986 if (backup_data)
8987 bidi_unshelve_cache (backup_data, 1);
8988
8989 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8990 }
8991
8992
8993 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8994
8995 If DY > 0, move IT backward at least that many pixels. DY = 0
8996 means move IT backward to the preceding line start or BEGV. This
8997 function may move over more than DY pixels if IT->current_y - DY
8998 ends up in the middle of a line; in this case IT->current_y will be
8999 set to the top of the line moved to. */
9000
9001 void
9002 move_it_vertically_backward (struct it *it, int dy)
9003 {
9004 int nlines, h;
9005 struct it it2, it3;
9006 void *it2data = NULL, *it3data = NULL;
9007 ptrdiff_t start_pos;
9008
9009 move_further_back:
9010 eassert (dy >= 0);
9011
9012 start_pos = IT_CHARPOS (*it);
9013
9014 /* Estimate how many newlines we must move back. */
9015 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9016
9017 /* Set the iterator's position that many lines back. */
9018 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9019 back_to_previous_visible_line_start (it);
9020
9021 /* Reseat the iterator here. When moving backward, we don't want
9022 reseat to skip forward over invisible text, set up the iterator
9023 to deliver from overlay strings at the new position etc. So,
9024 use reseat_1 here. */
9025 reseat_1 (it, it->current.pos, 1);
9026
9027 /* We are now surely at a line start. */
9028 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9029 reordering is in effect. */
9030 it->continuation_lines_width = 0;
9031
9032 /* Move forward and see what y-distance we moved. First move to the
9033 start of the next line so that we get its height. We need this
9034 height to be able to tell whether we reached the specified
9035 y-distance. */
9036 SAVE_IT (it2, *it, it2data);
9037 it2.max_ascent = it2.max_descent = 0;
9038 do
9039 {
9040 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9041 MOVE_TO_POS | MOVE_TO_VPOS);
9042 }
9043 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9044 /* If we are in a display string which starts at START_POS,
9045 and that display string includes a newline, and we are
9046 right after that newline (i.e. at the beginning of a
9047 display line), exit the loop, because otherwise we will
9048 infloop, since move_it_to will see that it is already at
9049 START_POS and will not move. */
9050 || (it2.method == GET_FROM_STRING
9051 && IT_CHARPOS (it2) == start_pos
9052 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9053 eassert (IT_CHARPOS (*it) >= BEGV);
9054 SAVE_IT (it3, it2, it3data);
9055
9056 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9057 eassert (IT_CHARPOS (*it) >= BEGV);
9058 /* H is the actual vertical distance from the position in *IT
9059 and the starting position. */
9060 h = it2.current_y - it->current_y;
9061 /* NLINES is the distance in number of lines. */
9062 nlines = it2.vpos - it->vpos;
9063
9064 /* Correct IT's y and vpos position
9065 so that they are relative to the starting point. */
9066 it->vpos -= nlines;
9067 it->current_y -= h;
9068
9069 if (dy == 0)
9070 {
9071 /* DY == 0 means move to the start of the screen line. The
9072 value of nlines is > 0 if continuation lines were involved,
9073 or if the original IT position was at start of a line. */
9074 RESTORE_IT (it, it, it2data);
9075 if (nlines > 0)
9076 move_it_by_lines (it, nlines);
9077 /* The above code moves us to some position NLINES down,
9078 usually to its first glyph (leftmost in an L2R line), but
9079 that's not necessarily the start of the line, under bidi
9080 reordering. We want to get to the character position
9081 that is immediately after the newline of the previous
9082 line. */
9083 if (it->bidi_p
9084 && !it->continuation_lines_width
9085 && !STRINGP (it->string)
9086 && IT_CHARPOS (*it) > BEGV
9087 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9088 {
9089 ptrdiff_t nl_pos =
9090 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9091
9092 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9093 }
9094 bidi_unshelve_cache (it3data, 1);
9095 }
9096 else
9097 {
9098 /* The y-position we try to reach, relative to *IT.
9099 Note that H has been subtracted in front of the if-statement. */
9100 int target_y = it->current_y + h - dy;
9101 int y0 = it3.current_y;
9102 int y1;
9103 int line_height;
9104
9105 RESTORE_IT (&it3, &it3, it3data);
9106 y1 = line_bottom_y (&it3);
9107 line_height = y1 - y0;
9108 RESTORE_IT (it, it, it2data);
9109 /* If we did not reach target_y, try to move further backward if
9110 we can. If we moved too far backward, try to move forward. */
9111 if (target_y < it->current_y
9112 /* This is heuristic. In a window that's 3 lines high, with
9113 a line height of 13 pixels each, recentering with point
9114 on the bottom line will try to move -39/2 = 19 pixels
9115 backward. Try to avoid moving into the first line. */
9116 && (it->current_y - target_y
9117 > min (window_box_height (it->w), line_height * 2 / 3))
9118 && IT_CHARPOS (*it) > BEGV)
9119 {
9120 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9121 target_y - it->current_y));
9122 dy = it->current_y - target_y;
9123 goto move_further_back;
9124 }
9125 else if (target_y >= it->current_y + line_height
9126 && IT_CHARPOS (*it) < ZV)
9127 {
9128 /* Should move forward by at least one line, maybe more.
9129
9130 Note: Calling move_it_by_lines can be expensive on
9131 terminal frames, where compute_motion is used (via
9132 vmotion) to do the job, when there are very long lines
9133 and truncate-lines is nil. That's the reason for
9134 treating terminal frames specially here. */
9135
9136 if (!FRAME_WINDOW_P (it->f))
9137 move_it_vertically (it, target_y - (it->current_y + line_height));
9138 else
9139 {
9140 do
9141 {
9142 move_it_by_lines (it, 1);
9143 }
9144 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9145 }
9146 }
9147 }
9148 }
9149
9150
9151 /* Move IT by a specified amount of pixel lines DY. DY negative means
9152 move backwards. DY = 0 means move to start of screen line. At the
9153 end, IT will be on the start of a screen line. */
9154
9155 void
9156 move_it_vertically (struct it *it, int dy)
9157 {
9158 if (dy <= 0)
9159 move_it_vertically_backward (it, -dy);
9160 else
9161 {
9162 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9163 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9164 MOVE_TO_POS | MOVE_TO_Y);
9165 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9166
9167 /* If buffer ends in ZV without a newline, move to the start of
9168 the line to satisfy the post-condition. */
9169 if (IT_CHARPOS (*it) == ZV
9170 && ZV > BEGV
9171 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9172 move_it_by_lines (it, 0);
9173 }
9174 }
9175
9176
9177 /* Move iterator IT past the end of the text line it is in. */
9178
9179 void
9180 move_it_past_eol (struct it *it)
9181 {
9182 enum move_it_result rc;
9183
9184 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9185 if (rc == MOVE_NEWLINE_OR_CR)
9186 set_iterator_to_next (it, 0);
9187 }
9188
9189
9190 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9191 negative means move up. DVPOS == 0 means move to the start of the
9192 screen line.
9193
9194 Optimization idea: If we would know that IT->f doesn't use
9195 a face with proportional font, we could be faster for
9196 truncate-lines nil. */
9197
9198 void
9199 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9200 {
9201
9202 /* The commented-out optimization uses vmotion on terminals. This
9203 gives bad results, because elements like it->what, on which
9204 callers such as pos_visible_p rely, aren't updated. */
9205 /* struct position pos;
9206 if (!FRAME_WINDOW_P (it->f))
9207 {
9208 struct text_pos textpos;
9209
9210 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9211 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9212 reseat (it, textpos, 1);
9213 it->vpos += pos.vpos;
9214 it->current_y += pos.vpos;
9215 }
9216 else */
9217
9218 if (dvpos == 0)
9219 {
9220 /* DVPOS == 0 means move to the start of the screen line. */
9221 move_it_vertically_backward (it, 0);
9222 /* Let next call to line_bottom_y calculate real line height */
9223 last_height = 0;
9224 }
9225 else if (dvpos > 0)
9226 {
9227 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9228 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9229 {
9230 /* Only move to the next buffer position if we ended up in a
9231 string from display property, not in an overlay string
9232 (before-string or after-string). That is because the
9233 latter don't conceal the underlying buffer position, so
9234 we can ask to move the iterator to the exact position we
9235 are interested in. Note that, even if we are already at
9236 IT_CHARPOS (*it), the call below is not a no-op, as it
9237 will detect that we are at the end of the string, pop the
9238 iterator, and compute it->current_x and it->hpos
9239 correctly. */
9240 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9241 -1, -1, -1, MOVE_TO_POS);
9242 }
9243 }
9244 else
9245 {
9246 struct it it2;
9247 void *it2data = NULL;
9248 ptrdiff_t start_charpos, i;
9249
9250 /* Start at the beginning of the screen line containing IT's
9251 position. This may actually move vertically backwards,
9252 in case of overlays, so adjust dvpos accordingly. */
9253 dvpos += it->vpos;
9254 move_it_vertically_backward (it, 0);
9255 dvpos -= it->vpos;
9256
9257 /* Go back -DVPOS visible lines and reseat the iterator there. */
9258 start_charpos = IT_CHARPOS (*it);
9259 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9260 back_to_previous_visible_line_start (it);
9261 reseat (it, it->current.pos, 1);
9262
9263 /* Move further back if we end up in a string or an image. */
9264 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9265 {
9266 /* First try to move to start of display line. */
9267 dvpos += it->vpos;
9268 move_it_vertically_backward (it, 0);
9269 dvpos -= it->vpos;
9270 if (IT_POS_VALID_AFTER_MOVE_P (it))
9271 break;
9272 /* If start of line is still in string or image,
9273 move further back. */
9274 back_to_previous_visible_line_start (it);
9275 reseat (it, it->current.pos, 1);
9276 dvpos--;
9277 }
9278
9279 it->current_x = it->hpos = 0;
9280
9281 /* Above call may have moved too far if continuation lines
9282 are involved. Scan forward and see if it did. */
9283 SAVE_IT (it2, *it, it2data);
9284 it2.vpos = it2.current_y = 0;
9285 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9286 it->vpos -= it2.vpos;
9287 it->current_y -= it2.current_y;
9288 it->current_x = it->hpos = 0;
9289
9290 /* If we moved too far back, move IT some lines forward. */
9291 if (it2.vpos > -dvpos)
9292 {
9293 int delta = it2.vpos + dvpos;
9294
9295 RESTORE_IT (&it2, &it2, it2data);
9296 SAVE_IT (it2, *it, it2data);
9297 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9298 /* Move back again if we got too far ahead. */
9299 if (IT_CHARPOS (*it) >= start_charpos)
9300 RESTORE_IT (it, &it2, it2data);
9301 else
9302 bidi_unshelve_cache (it2data, 1);
9303 }
9304 else
9305 RESTORE_IT (it, it, it2data);
9306 }
9307 }
9308
9309 /* Return 1 if IT points into the middle of a display vector. */
9310
9311 int
9312 in_display_vector_p (struct it *it)
9313 {
9314 return (it->method == GET_FROM_DISPLAY_VECTOR
9315 && it->current.dpvec_index > 0
9316 && it->dpvec + it->current.dpvec_index != it->dpend);
9317 }
9318
9319 \f
9320 /***********************************************************************
9321 Messages
9322 ***********************************************************************/
9323
9324
9325 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9326 to *Messages*. */
9327
9328 void
9329 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9330 {
9331 Lisp_Object args[3];
9332 Lisp_Object msg, fmt;
9333 char *buffer;
9334 ptrdiff_t len;
9335 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9336 USE_SAFE_ALLOCA;
9337
9338 fmt = msg = Qnil;
9339 GCPRO4 (fmt, msg, arg1, arg2);
9340
9341 args[0] = fmt = build_string (format);
9342 args[1] = arg1;
9343 args[2] = arg2;
9344 msg = Fformat (3, args);
9345
9346 len = SBYTES (msg) + 1;
9347 buffer = SAFE_ALLOCA (len);
9348 memcpy (buffer, SDATA (msg), len);
9349
9350 message_dolog (buffer, len - 1, 1, 0);
9351 SAFE_FREE ();
9352
9353 UNGCPRO;
9354 }
9355
9356
9357 /* Output a newline in the *Messages* buffer if "needs" one. */
9358
9359 void
9360 message_log_maybe_newline (void)
9361 {
9362 if (message_log_need_newline)
9363 message_dolog ("", 0, 1, 0);
9364 }
9365
9366
9367 /* Add a string M of length NBYTES to the message log, optionally
9368 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9369 nonzero, means interpret the contents of M as multibyte. This
9370 function calls low-level routines in order to bypass text property
9371 hooks, etc. which might not be safe to run.
9372
9373 This may GC (insert may run before/after change hooks),
9374 so the buffer M must NOT point to a Lisp string. */
9375
9376 void
9377 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9378 {
9379 const unsigned char *msg = (const unsigned char *) m;
9380
9381 if (!NILP (Vmemory_full))
9382 return;
9383
9384 if (!NILP (Vmessage_log_max))
9385 {
9386 struct buffer *oldbuf;
9387 Lisp_Object oldpoint, oldbegv, oldzv;
9388 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9389 ptrdiff_t point_at_end = 0;
9390 ptrdiff_t zv_at_end = 0;
9391 Lisp_Object old_deactivate_mark, tem;
9392 struct gcpro gcpro1;
9393
9394 old_deactivate_mark = Vdeactivate_mark;
9395 oldbuf = current_buffer;
9396 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9397 bset_undo_list (current_buffer, Qt);
9398
9399 oldpoint = message_dolog_marker1;
9400 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9401 oldbegv = message_dolog_marker2;
9402 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9403 oldzv = message_dolog_marker3;
9404 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9405 GCPRO1 (old_deactivate_mark);
9406
9407 if (PT == Z)
9408 point_at_end = 1;
9409 if (ZV == Z)
9410 zv_at_end = 1;
9411
9412 BEGV = BEG;
9413 BEGV_BYTE = BEG_BYTE;
9414 ZV = Z;
9415 ZV_BYTE = Z_BYTE;
9416 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9417
9418 /* Insert the string--maybe converting multibyte to single byte
9419 or vice versa, so that all the text fits the buffer. */
9420 if (multibyte
9421 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9422 {
9423 ptrdiff_t i;
9424 int c, char_bytes;
9425 char work[1];
9426
9427 /* Convert a multibyte string to single-byte
9428 for the *Message* buffer. */
9429 for (i = 0; i < nbytes; i += char_bytes)
9430 {
9431 c = string_char_and_length (msg + i, &char_bytes);
9432 work[0] = (ASCII_CHAR_P (c)
9433 ? c
9434 : multibyte_char_to_unibyte (c));
9435 insert_1_both (work, 1, 1, 1, 0, 0);
9436 }
9437 }
9438 else if (! multibyte
9439 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9440 {
9441 ptrdiff_t i;
9442 int c, char_bytes;
9443 unsigned char str[MAX_MULTIBYTE_LENGTH];
9444 /* Convert a single-byte string to multibyte
9445 for the *Message* buffer. */
9446 for (i = 0; i < nbytes; i++)
9447 {
9448 c = msg[i];
9449 MAKE_CHAR_MULTIBYTE (c);
9450 char_bytes = CHAR_STRING (c, str);
9451 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9452 }
9453 }
9454 else if (nbytes)
9455 insert_1 (m, nbytes, 1, 0, 0);
9456
9457 if (nlflag)
9458 {
9459 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9460 printmax_t dups;
9461 insert_1 ("\n", 1, 1, 0, 0);
9462
9463 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9464 this_bol = PT;
9465 this_bol_byte = PT_BYTE;
9466
9467 /* See if this line duplicates the previous one.
9468 If so, combine duplicates. */
9469 if (this_bol > BEG)
9470 {
9471 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9472 prev_bol = PT;
9473 prev_bol_byte = PT_BYTE;
9474
9475 dups = message_log_check_duplicate (prev_bol_byte,
9476 this_bol_byte);
9477 if (dups)
9478 {
9479 del_range_both (prev_bol, prev_bol_byte,
9480 this_bol, this_bol_byte, 0);
9481 if (dups > 1)
9482 {
9483 char dupstr[sizeof " [ times]"
9484 + INT_STRLEN_BOUND (printmax_t)];
9485
9486 /* If you change this format, don't forget to also
9487 change message_log_check_duplicate. */
9488 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9489 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9490 insert_1 (dupstr, duplen, 1, 0, 1);
9491 }
9492 }
9493 }
9494
9495 /* If we have more than the desired maximum number of lines
9496 in the *Messages* buffer now, delete the oldest ones.
9497 This is safe because we don't have undo in this buffer. */
9498
9499 if (NATNUMP (Vmessage_log_max))
9500 {
9501 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9502 -XFASTINT (Vmessage_log_max) - 1, 0);
9503 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9504 }
9505 }
9506 BEGV = marker_position (oldbegv);
9507 BEGV_BYTE = marker_byte_position (oldbegv);
9508
9509 if (zv_at_end)
9510 {
9511 ZV = Z;
9512 ZV_BYTE = Z_BYTE;
9513 }
9514 else
9515 {
9516 ZV = marker_position (oldzv);
9517 ZV_BYTE = marker_byte_position (oldzv);
9518 }
9519
9520 if (point_at_end)
9521 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9522 else
9523 /* We can't do Fgoto_char (oldpoint) because it will run some
9524 Lisp code. */
9525 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9526 marker_byte_position (oldpoint));
9527
9528 UNGCPRO;
9529 unchain_marker (XMARKER (oldpoint));
9530 unchain_marker (XMARKER (oldbegv));
9531 unchain_marker (XMARKER (oldzv));
9532
9533 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9534 set_buffer_internal (oldbuf);
9535 if (NILP (tem))
9536 windows_or_buffers_changed = old_windows_or_buffers_changed;
9537 message_log_need_newline = !nlflag;
9538 Vdeactivate_mark = old_deactivate_mark;
9539 }
9540 }
9541
9542
9543 /* We are at the end of the buffer after just having inserted a newline.
9544 (Note: We depend on the fact we won't be crossing the gap.)
9545 Check to see if the most recent message looks a lot like the previous one.
9546 Return 0 if different, 1 if the new one should just replace it, or a
9547 value N > 1 if we should also append " [N times]". */
9548
9549 static intmax_t
9550 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9551 {
9552 ptrdiff_t i;
9553 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9554 int seen_dots = 0;
9555 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9556 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9557
9558 for (i = 0; i < len; i++)
9559 {
9560 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9561 seen_dots = 1;
9562 if (p1[i] != p2[i])
9563 return seen_dots;
9564 }
9565 p1 += len;
9566 if (*p1 == '\n')
9567 return 2;
9568 if (*p1++ == ' ' && *p1++ == '[')
9569 {
9570 char *pend;
9571 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9572 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9573 return n+1;
9574 }
9575 return 0;
9576 }
9577 \f
9578
9579 /* Display an echo area message M with a specified length of NBYTES
9580 bytes. The string may include null characters. If M is 0, clear
9581 out any existing message, and let the mini-buffer text show
9582 through.
9583
9584 This may GC, so the buffer M must NOT point to a Lisp string. */
9585
9586 void
9587 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9588 {
9589 /* First flush out any partial line written with print. */
9590 message_log_maybe_newline ();
9591 if (m)
9592 message_dolog (m, nbytes, 1, multibyte);
9593 message2_nolog (m, nbytes, multibyte);
9594 }
9595
9596
9597 /* The non-logging counterpart of message2. */
9598
9599 void
9600 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9601 {
9602 struct frame *sf = SELECTED_FRAME ();
9603 message_enable_multibyte = multibyte;
9604
9605 if (FRAME_INITIAL_P (sf))
9606 {
9607 if (noninteractive_need_newline)
9608 putc ('\n', stderr);
9609 noninteractive_need_newline = 0;
9610 if (m)
9611 fwrite (m, nbytes, 1, stderr);
9612 if (cursor_in_echo_area == 0)
9613 fprintf (stderr, "\n");
9614 fflush (stderr);
9615 }
9616 /* A null message buffer means that the frame hasn't really been
9617 initialized yet. Error messages get reported properly by
9618 cmd_error, so this must be just an informative message; toss it. */
9619 else if (INTERACTIVE
9620 && sf->glyphs_initialized_p
9621 && FRAME_MESSAGE_BUF (sf))
9622 {
9623 Lisp_Object mini_window;
9624 struct frame *f;
9625
9626 /* Get the frame containing the mini-buffer
9627 that the selected frame is using. */
9628 mini_window = FRAME_MINIBUF_WINDOW (sf);
9629 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9630
9631 FRAME_SAMPLE_VISIBILITY (f);
9632 if (FRAME_VISIBLE_P (sf)
9633 && ! FRAME_VISIBLE_P (f))
9634 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9635
9636 if (m)
9637 {
9638 set_message (m, Qnil, nbytes, multibyte);
9639 if (minibuffer_auto_raise)
9640 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9641 }
9642 else
9643 clear_message (1, 1);
9644
9645 do_pending_window_change (0);
9646 echo_area_display (1);
9647 do_pending_window_change (0);
9648 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9649 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9650 }
9651 }
9652
9653
9654 /* Display an echo area message M with a specified length of NBYTES
9655 bytes. The string may include null characters. If M is not a
9656 string, clear out any existing message, and let the mini-buffer
9657 text show through.
9658
9659 This function cancels echoing. */
9660
9661 void
9662 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9663 {
9664 struct gcpro gcpro1;
9665
9666 GCPRO1 (m);
9667 clear_message (1,1);
9668 cancel_echoing ();
9669
9670 /* First flush out any partial line written with print. */
9671 message_log_maybe_newline ();
9672 if (STRINGP (m))
9673 {
9674 USE_SAFE_ALLOCA;
9675 char *buffer = SAFE_ALLOCA (nbytes);
9676 memcpy (buffer, SDATA (m), nbytes);
9677 message_dolog (buffer, nbytes, 1, multibyte);
9678 SAFE_FREE ();
9679 }
9680 message3_nolog (m, nbytes, multibyte);
9681
9682 UNGCPRO;
9683 }
9684
9685
9686 /* The non-logging version of message3.
9687 This does not cancel echoing, because it is used for echoing.
9688 Perhaps we need to make a separate function for echoing
9689 and make this cancel echoing. */
9690
9691 void
9692 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9693 {
9694 struct frame *sf = SELECTED_FRAME ();
9695 message_enable_multibyte = multibyte;
9696
9697 if (FRAME_INITIAL_P (sf))
9698 {
9699 if (noninteractive_need_newline)
9700 putc ('\n', stderr);
9701 noninteractive_need_newline = 0;
9702 if (STRINGP (m))
9703 fwrite (SDATA (m), nbytes, 1, stderr);
9704 if (cursor_in_echo_area == 0)
9705 fprintf (stderr, "\n");
9706 fflush (stderr);
9707 }
9708 /* A null message buffer means that the frame hasn't really been
9709 initialized yet. Error messages get reported properly by
9710 cmd_error, so this must be just an informative message; toss it. */
9711 else if (INTERACTIVE
9712 && sf->glyphs_initialized_p
9713 && FRAME_MESSAGE_BUF (sf))
9714 {
9715 Lisp_Object mini_window;
9716 Lisp_Object frame;
9717 struct frame *f;
9718
9719 /* Get the frame containing the mini-buffer
9720 that the selected frame is using. */
9721 mini_window = FRAME_MINIBUF_WINDOW (sf);
9722 frame = XWINDOW (mini_window)->frame;
9723 f = XFRAME (frame);
9724
9725 FRAME_SAMPLE_VISIBILITY (f);
9726 if (FRAME_VISIBLE_P (sf)
9727 && !FRAME_VISIBLE_P (f))
9728 Fmake_frame_visible (frame);
9729
9730 if (STRINGP (m) && SCHARS (m) > 0)
9731 {
9732 set_message (NULL, m, nbytes, multibyte);
9733 if (minibuffer_auto_raise)
9734 Fraise_frame (frame);
9735 /* Assume we are not echoing.
9736 (If we are, echo_now will override this.) */
9737 echo_message_buffer = Qnil;
9738 }
9739 else
9740 clear_message (1, 1);
9741
9742 do_pending_window_change (0);
9743 echo_area_display (1);
9744 do_pending_window_change (0);
9745 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9746 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9747 }
9748 }
9749
9750
9751 /* Display a null-terminated echo area message M. If M is 0, clear
9752 out any existing message, and let the mini-buffer text show through.
9753
9754 The buffer M must continue to exist until after the echo area gets
9755 cleared or some other message gets displayed there. Do not pass
9756 text that is stored in a Lisp string. Do not pass text in a buffer
9757 that was alloca'd. */
9758
9759 void
9760 message1 (const char *m)
9761 {
9762 message2 (m, (m ? strlen (m) : 0), 0);
9763 }
9764
9765
9766 /* The non-logging counterpart of message1. */
9767
9768 void
9769 message1_nolog (const char *m)
9770 {
9771 message2_nolog (m, (m ? strlen (m) : 0), 0);
9772 }
9773
9774 /* Display a message M which contains a single %s
9775 which gets replaced with STRING. */
9776
9777 void
9778 message_with_string (const char *m, Lisp_Object string, int log)
9779 {
9780 CHECK_STRING (string);
9781
9782 if (noninteractive)
9783 {
9784 if (m)
9785 {
9786 if (noninteractive_need_newline)
9787 putc ('\n', stderr);
9788 noninteractive_need_newline = 0;
9789 fprintf (stderr, m, SDATA (string));
9790 if (!cursor_in_echo_area)
9791 fprintf (stderr, "\n");
9792 fflush (stderr);
9793 }
9794 }
9795 else if (INTERACTIVE)
9796 {
9797 /* The frame whose minibuffer we're going to display the message on.
9798 It may be larger than the selected frame, so we need
9799 to use its buffer, not the selected frame's buffer. */
9800 Lisp_Object mini_window;
9801 struct frame *f, *sf = SELECTED_FRAME ();
9802
9803 /* Get the frame containing the minibuffer
9804 that the selected frame is using. */
9805 mini_window = FRAME_MINIBUF_WINDOW (sf);
9806 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9807
9808 /* A null message buffer means that the frame hasn't really been
9809 initialized yet. Error messages get reported properly by
9810 cmd_error, so this must be just an informative message; toss it. */
9811 if (FRAME_MESSAGE_BUF (f))
9812 {
9813 Lisp_Object args[2], msg;
9814 struct gcpro gcpro1, gcpro2;
9815
9816 args[0] = build_string (m);
9817 args[1] = msg = string;
9818 GCPRO2 (args[0], msg);
9819 gcpro1.nvars = 2;
9820
9821 msg = Fformat (2, args);
9822
9823 if (log)
9824 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9825 else
9826 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9827
9828 UNGCPRO;
9829
9830 /* Print should start at the beginning of the message
9831 buffer next time. */
9832 message_buf_print = 0;
9833 }
9834 }
9835 }
9836
9837
9838 /* Dump an informative message to the minibuf. If M is 0, clear out
9839 any existing message, and let the mini-buffer text show through. */
9840
9841 static void
9842 vmessage (const char *m, va_list ap)
9843 {
9844 if (noninteractive)
9845 {
9846 if (m)
9847 {
9848 if (noninteractive_need_newline)
9849 putc ('\n', stderr);
9850 noninteractive_need_newline = 0;
9851 vfprintf (stderr, m, ap);
9852 if (cursor_in_echo_area == 0)
9853 fprintf (stderr, "\n");
9854 fflush (stderr);
9855 }
9856 }
9857 else if (INTERACTIVE)
9858 {
9859 /* The frame whose mini-buffer we're going to display the message
9860 on. It may be larger than the selected frame, so we need to
9861 use its buffer, not the selected frame's buffer. */
9862 Lisp_Object mini_window;
9863 struct frame *f, *sf = SELECTED_FRAME ();
9864
9865 /* Get the frame containing the mini-buffer
9866 that the selected frame is using. */
9867 mini_window = FRAME_MINIBUF_WINDOW (sf);
9868 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9869
9870 /* A null message buffer means that the frame hasn't really been
9871 initialized yet. Error messages get reported properly by
9872 cmd_error, so this must be just an informative message; toss
9873 it. */
9874 if (FRAME_MESSAGE_BUF (f))
9875 {
9876 if (m)
9877 {
9878 ptrdiff_t len;
9879
9880 len = doprnt (FRAME_MESSAGE_BUF (f),
9881 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9882
9883 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9884 }
9885 else
9886 message1 (0);
9887
9888 /* Print should start at the beginning of the message
9889 buffer next time. */
9890 message_buf_print = 0;
9891 }
9892 }
9893 }
9894
9895 void
9896 message (const char *m, ...)
9897 {
9898 va_list ap;
9899 va_start (ap, m);
9900 vmessage (m, ap);
9901 va_end (ap);
9902 }
9903
9904
9905 #if 0
9906 /* The non-logging version of message. */
9907
9908 void
9909 message_nolog (const char *m, ...)
9910 {
9911 Lisp_Object old_log_max;
9912 va_list ap;
9913 va_start (ap, m);
9914 old_log_max = Vmessage_log_max;
9915 Vmessage_log_max = Qnil;
9916 vmessage (m, ap);
9917 Vmessage_log_max = old_log_max;
9918 va_end (ap);
9919 }
9920 #endif
9921
9922
9923 /* Display the current message in the current mini-buffer. This is
9924 only called from error handlers in process.c, and is not time
9925 critical. */
9926
9927 void
9928 update_echo_area (void)
9929 {
9930 if (!NILP (echo_area_buffer[0]))
9931 {
9932 Lisp_Object string;
9933 string = Fcurrent_message ();
9934 message3 (string, SBYTES (string),
9935 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9936 }
9937 }
9938
9939
9940 /* Make sure echo area buffers in `echo_buffers' are live.
9941 If they aren't, make new ones. */
9942
9943 static void
9944 ensure_echo_area_buffers (void)
9945 {
9946 int i;
9947
9948 for (i = 0; i < 2; ++i)
9949 if (!BUFFERP (echo_buffer[i])
9950 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9951 {
9952 char name[30];
9953 Lisp_Object old_buffer;
9954 int j;
9955
9956 old_buffer = echo_buffer[i];
9957 echo_buffer[i] = Fget_buffer_create
9958 (make_formatted_string (name, " *Echo Area %d*", i));
9959 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9960 /* to force word wrap in echo area -
9961 it was decided to postpone this*/
9962 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9963
9964 for (j = 0; j < 2; ++j)
9965 if (EQ (old_buffer, echo_area_buffer[j]))
9966 echo_area_buffer[j] = echo_buffer[i];
9967 }
9968 }
9969
9970
9971 /* Call FN with args A1..A4 with either the current or last displayed
9972 echo_area_buffer as current buffer.
9973
9974 WHICH zero means use the current message buffer
9975 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9976 from echo_buffer[] and clear it.
9977
9978 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9979 suitable buffer from echo_buffer[] and clear it.
9980
9981 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9982 that the current message becomes the last displayed one, make
9983 choose a suitable buffer for echo_area_buffer[0], and clear it.
9984
9985 Value is what FN returns. */
9986
9987 static int
9988 with_echo_area_buffer (struct window *w, int which,
9989 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9990 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9991 {
9992 Lisp_Object buffer;
9993 int this_one, the_other, clear_buffer_p, rc;
9994 ptrdiff_t count = SPECPDL_INDEX ();
9995
9996 /* If buffers aren't live, make new ones. */
9997 ensure_echo_area_buffers ();
9998
9999 clear_buffer_p = 0;
10000
10001 if (which == 0)
10002 this_one = 0, the_other = 1;
10003 else if (which > 0)
10004 this_one = 1, the_other = 0;
10005 else
10006 {
10007 this_one = 0, the_other = 1;
10008 clear_buffer_p = 1;
10009
10010 /* We need a fresh one in case the current echo buffer equals
10011 the one containing the last displayed echo area message. */
10012 if (!NILP (echo_area_buffer[this_one])
10013 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10014 echo_area_buffer[this_one] = Qnil;
10015 }
10016
10017 /* Choose a suitable buffer from echo_buffer[] is we don't
10018 have one. */
10019 if (NILP (echo_area_buffer[this_one]))
10020 {
10021 echo_area_buffer[this_one]
10022 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10023 ? echo_buffer[the_other]
10024 : echo_buffer[this_one]);
10025 clear_buffer_p = 1;
10026 }
10027
10028 buffer = echo_area_buffer[this_one];
10029
10030 /* Don't get confused by reusing the buffer used for echoing
10031 for a different purpose. */
10032 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10033 cancel_echoing ();
10034
10035 record_unwind_protect (unwind_with_echo_area_buffer,
10036 with_echo_area_buffer_unwind_data (w));
10037
10038 /* Make the echo area buffer current. Note that for display
10039 purposes, it is not necessary that the displayed window's buffer
10040 == current_buffer, except for text property lookup. So, let's
10041 only set that buffer temporarily here without doing a full
10042 Fset_window_buffer. We must also change w->pointm, though,
10043 because otherwise an assertions in unshow_buffer fails, and Emacs
10044 aborts. */
10045 set_buffer_internal_1 (XBUFFER (buffer));
10046 if (w)
10047 {
10048 wset_buffer (w, buffer);
10049 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10050 }
10051
10052 bset_undo_list (current_buffer, Qt);
10053 bset_read_only (current_buffer, Qnil);
10054 specbind (Qinhibit_read_only, Qt);
10055 specbind (Qinhibit_modification_hooks, Qt);
10056
10057 if (clear_buffer_p && Z > BEG)
10058 del_range (BEG, Z);
10059
10060 eassert (BEGV >= BEG);
10061 eassert (ZV <= Z && ZV >= BEGV);
10062
10063 rc = fn (a1, a2, a3, a4);
10064
10065 eassert (BEGV >= BEG);
10066 eassert (ZV <= Z && ZV >= BEGV);
10067
10068 unbind_to (count, Qnil);
10069 return rc;
10070 }
10071
10072
10073 /* Save state that should be preserved around the call to the function
10074 FN called in with_echo_area_buffer. */
10075
10076 static Lisp_Object
10077 with_echo_area_buffer_unwind_data (struct window *w)
10078 {
10079 int i = 0;
10080 Lisp_Object vector, tmp;
10081
10082 /* Reduce consing by keeping one vector in
10083 Vwith_echo_area_save_vector. */
10084 vector = Vwith_echo_area_save_vector;
10085 Vwith_echo_area_save_vector = Qnil;
10086
10087 if (NILP (vector))
10088 vector = Fmake_vector (make_number (7), Qnil);
10089
10090 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10091 ASET (vector, i, Vdeactivate_mark); ++i;
10092 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10093
10094 if (w)
10095 {
10096 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10097 ASET (vector, i, w->buffer); ++i;
10098 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10099 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10100 }
10101 else
10102 {
10103 int end = i + 4;
10104 for (; i < end; ++i)
10105 ASET (vector, i, Qnil);
10106 }
10107
10108 eassert (i == ASIZE (vector));
10109 return vector;
10110 }
10111
10112
10113 /* Restore global state from VECTOR which was created by
10114 with_echo_area_buffer_unwind_data. */
10115
10116 static Lisp_Object
10117 unwind_with_echo_area_buffer (Lisp_Object vector)
10118 {
10119 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10120 Vdeactivate_mark = AREF (vector, 1);
10121 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10122
10123 if (WINDOWP (AREF (vector, 3)))
10124 {
10125 struct window *w;
10126 Lisp_Object buffer, charpos, bytepos;
10127
10128 w = XWINDOW (AREF (vector, 3));
10129 buffer = AREF (vector, 4);
10130 charpos = AREF (vector, 5);
10131 bytepos = AREF (vector, 6);
10132
10133 wset_buffer (w, buffer);
10134 set_marker_both (w->pointm, buffer,
10135 XFASTINT (charpos), XFASTINT (bytepos));
10136 }
10137
10138 Vwith_echo_area_save_vector = vector;
10139 return Qnil;
10140 }
10141
10142
10143 /* Set up the echo area for use by print functions. MULTIBYTE_P
10144 non-zero means we will print multibyte. */
10145
10146 void
10147 setup_echo_area_for_printing (int multibyte_p)
10148 {
10149 /* If we can't find an echo area any more, exit. */
10150 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10151 Fkill_emacs (Qnil);
10152
10153 ensure_echo_area_buffers ();
10154
10155 if (!message_buf_print)
10156 {
10157 /* A message has been output since the last time we printed.
10158 Choose a fresh echo area buffer. */
10159 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10160 echo_area_buffer[0] = echo_buffer[1];
10161 else
10162 echo_area_buffer[0] = echo_buffer[0];
10163
10164 /* Switch to that buffer and clear it. */
10165 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10166 bset_truncate_lines (current_buffer, Qnil);
10167
10168 if (Z > BEG)
10169 {
10170 ptrdiff_t count = SPECPDL_INDEX ();
10171 specbind (Qinhibit_read_only, Qt);
10172 /* Note that undo recording is always disabled. */
10173 del_range (BEG, Z);
10174 unbind_to (count, Qnil);
10175 }
10176 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10177
10178 /* Set up the buffer for the multibyteness we need. */
10179 if (multibyte_p
10180 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10181 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10182
10183 /* Raise the frame containing the echo area. */
10184 if (minibuffer_auto_raise)
10185 {
10186 struct frame *sf = SELECTED_FRAME ();
10187 Lisp_Object mini_window;
10188 mini_window = FRAME_MINIBUF_WINDOW (sf);
10189 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10190 }
10191
10192 message_log_maybe_newline ();
10193 message_buf_print = 1;
10194 }
10195 else
10196 {
10197 if (NILP (echo_area_buffer[0]))
10198 {
10199 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10200 echo_area_buffer[0] = echo_buffer[1];
10201 else
10202 echo_area_buffer[0] = echo_buffer[0];
10203 }
10204
10205 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10206 {
10207 /* Someone switched buffers between print requests. */
10208 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10209 bset_truncate_lines (current_buffer, Qnil);
10210 }
10211 }
10212 }
10213
10214
10215 /* Display an echo area message in window W. Value is non-zero if W's
10216 height is changed. If display_last_displayed_message_p is
10217 non-zero, display the message that was last displayed, otherwise
10218 display the current message. */
10219
10220 static int
10221 display_echo_area (struct window *w)
10222 {
10223 int i, no_message_p, window_height_changed_p;
10224
10225 /* Temporarily disable garbage collections while displaying the echo
10226 area. This is done because a GC can print a message itself.
10227 That message would modify the echo area buffer's contents while a
10228 redisplay of the buffer is going on, and seriously confuse
10229 redisplay. */
10230 ptrdiff_t count = inhibit_garbage_collection ();
10231
10232 /* If there is no message, we must call display_echo_area_1
10233 nevertheless because it resizes the window. But we will have to
10234 reset the echo_area_buffer in question to nil at the end because
10235 with_echo_area_buffer will sets it to an empty buffer. */
10236 i = display_last_displayed_message_p ? 1 : 0;
10237 no_message_p = NILP (echo_area_buffer[i]);
10238
10239 window_height_changed_p
10240 = with_echo_area_buffer (w, display_last_displayed_message_p,
10241 display_echo_area_1,
10242 (intptr_t) w, Qnil, 0, 0);
10243
10244 if (no_message_p)
10245 echo_area_buffer[i] = Qnil;
10246
10247 unbind_to (count, Qnil);
10248 return window_height_changed_p;
10249 }
10250
10251
10252 /* Helper for display_echo_area. Display the current buffer which
10253 contains the current echo area message in window W, a mini-window,
10254 a pointer to which is passed in A1. A2..A4 are currently not used.
10255 Change the height of W so that all of the message is displayed.
10256 Value is non-zero if height of W was changed. */
10257
10258 static int
10259 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10260 {
10261 intptr_t i1 = a1;
10262 struct window *w = (struct window *) i1;
10263 Lisp_Object window;
10264 struct text_pos start;
10265 int window_height_changed_p = 0;
10266
10267 /* Do this before displaying, so that we have a large enough glyph
10268 matrix for the display. If we can't get enough space for the
10269 whole text, display the last N lines. That works by setting w->start. */
10270 window_height_changed_p = resize_mini_window (w, 0);
10271
10272 /* Use the starting position chosen by resize_mini_window. */
10273 SET_TEXT_POS_FROM_MARKER (start, w->start);
10274
10275 /* Display. */
10276 clear_glyph_matrix (w->desired_matrix);
10277 XSETWINDOW (window, w);
10278 try_window (window, start, 0);
10279
10280 return window_height_changed_p;
10281 }
10282
10283
10284 /* Resize the echo area window to exactly the size needed for the
10285 currently displayed message, if there is one. If a mini-buffer
10286 is active, don't shrink it. */
10287
10288 void
10289 resize_echo_area_exactly (void)
10290 {
10291 if (BUFFERP (echo_area_buffer[0])
10292 && WINDOWP (echo_area_window))
10293 {
10294 struct window *w = XWINDOW (echo_area_window);
10295 int resized_p;
10296 Lisp_Object resize_exactly;
10297
10298 if (minibuf_level == 0)
10299 resize_exactly = Qt;
10300 else
10301 resize_exactly = Qnil;
10302
10303 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10304 (intptr_t) w, resize_exactly,
10305 0, 0);
10306 if (resized_p)
10307 {
10308 ++windows_or_buffers_changed;
10309 ++update_mode_lines;
10310 redisplay_internal ();
10311 }
10312 }
10313 }
10314
10315
10316 /* Callback function for with_echo_area_buffer, when used from
10317 resize_echo_area_exactly. A1 contains a pointer to the window to
10318 resize, EXACTLY non-nil means resize the mini-window exactly to the
10319 size of the text displayed. A3 and A4 are not used. Value is what
10320 resize_mini_window returns. */
10321
10322 static int
10323 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10324 {
10325 intptr_t i1 = a1;
10326 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10327 }
10328
10329
10330 /* Resize mini-window W to fit the size of its contents. EXACT_P
10331 means size the window exactly to the size needed. Otherwise, it's
10332 only enlarged until W's buffer is empty.
10333
10334 Set W->start to the right place to begin display. If the whole
10335 contents fit, start at the beginning. Otherwise, start so as
10336 to make the end of the contents appear. This is particularly
10337 important for y-or-n-p, but seems desirable generally.
10338
10339 Value is non-zero if the window height has been changed. */
10340
10341 int
10342 resize_mini_window (struct window *w, int exact_p)
10343 {
10344 struct frame *f = XFRAME (w->frame);
10345 int window_height_changed_p = 0;
10346
10347 eassert (MINI_WINDOW_P (w));
10348
10349 /* By default, start display at the beginning. */
10350 set_marker_both (w->start, w->buffer,
10351 BUF_BEGV (XBUFFER (w->buffer)),
10352 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10353
10354 /* Don't resize windows while redisplaying a window; it would
10355 confuse redisplay functions when the size of the window they are
10356 displaying changes from under them. Such a resizing can happen,
10357 for instance, when which-func prints a long message while
10358 we are running fontification-functions. We're running these
10359 functions with safe_call which binds inhibit-redisplay to t. */
10360 if (!NILP (Vinhibit_redisplay))
10361 return 0;
10362
10363 /* Nil means don't try to resize. */
10364 if (NILP (Vresize_mini_windows)
10365 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10366 return 0;
10367
10368 if (!FRAME_MINIBUF_ONLY_P (f))
10369 {
10370 struct it it;
10371 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10372 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10373 int height;
10374 EMACS_INT max_height;
10375 int unit = FRAME_LINE_HEIGHT (f);
10376 struct text_pos start;
10377 struct buffer *old_current_buffer = NULL;
10378
10379 if (current_buffer != XBUFFER (w->buffer))
10380 {
10381 old_current_buffer = current_buffer;
10382 set_buffer_internal (XBUFFER (w->buffer));
10383 }
10384
10385 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10386
10387 /* Compute the max. number of lines specified by the user. */
10388 if (FLOATP (Vmax_mini_window_height))
10389 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10390 else if (INTEGERP (Vmax_mini_window_height))
10391 max_height = XINT (Vmax_mini_window_height);
10392 else
10393 max_height = total_height / 4;
10394
10395 /* Correct that max. height if it's bogus. */
10396 max_height = clip_to_bounds (1, max_height, total_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 redraw_frame (f);
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 clear_garbaged_frames ();
10821
10822 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10823 {
10824 echo_area_window = mini_window;
10825 window_height_changed_p = display_echo_area (w);
10826 w->must_be_updated_p = 1;
10827
10828 /* Update the display, unless called from redisplay_internal.
10829 Also don't update the screen during redisplay itself. The
10830 update will happen at the end of redisplay, and an update
10831 here could cause confusion. */
10832 if (update_frame_p && !redisplaying_p)
10833 {
10834 int n = 0;
10835
10836 /* If the display update has been interrupted by pending
10837 input, update mode lines in the frame. Due to the
10838 pending input, it might have been that redisplay hasn't
10839 been called, so that mode lines above the echo area are
10840 garbaged. This looks odd, so we prevent it here. */
10841 if (!display_completed)
10842 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10843
10844 if (window_height_changed_p
10845 /* Don't do this if Emacs is shutting down. Redisplay
10846 needs to run hooks. */
10847 && !NILP (Vrun_hooks))
10848 {
10849 /* Must update other windows. Likewise as in other
10850 cases, don't let this update be interrupted by
10851 pending input. */
10852 ptrdiff_t count = SPECPDL_INDEX ();
10853 specbind (Qredisplay_dont_pause, Qt);
10854 windows_or_buffers_changed = 1;
10855 redisplay_internal ();
10856 unbind_to (count, Qnil);
10857 }
10858 else if (FRAME_WINDOW_P (f) && n == 0)
10859 {
10860 /* Window configuration is the same as before.
10861 Can do with a display update of the echo area,
10862 unless we displayed some mode lines. */
10863 update_single_window (w, 1);
10864 FRAME_RIF (f)->flush_display (f);
10865 }
10866 else
10867 update_frame (f, 1, 1);
10868
10869 /* If cursor is in the echo area, make sure that the next
10870 redisplay displays the minibuffer, so that the cursor will
10871 be replaced with what the minibuffer wants. */
10872 if (cursor_in_echo_area)
10873 ++windows_or_buffers_changed;
10874 }
10875 }
10876 else if (!EQ (mini_window, selected_window))
10877 windows_or_buffers_changed++;
10878
10879 /* Last displayed message is now the current message. */
10880 echo_area_buffer[1] = echo_area_buffer[0];
10881 /* Inform read_char that we're not echoing. */
10882 echo_message_buffer = Qnil;
10883
10884 /* Prevent redisplay optimization in redisplay_internal by resetting
10885 this_line_start_pos. This is done because the mini-buffer now
10886 displays the message instead of its buffer text. */
10887 if (EQ (mini_window, selected_window))
10888 CHARPOS (this_line_start_pos) = 0;
10889
10890 return window_height_changed_p;
10891 }
10892
10893 /* Nonzero if the current window's buffer is shown in more than one
10894 window and was modified since last redisplay. */
10895
10896 static int
10897 buffer_shared_and_changed (void)
10898 {
10899 return (buffer_window_count (current_buffer) > 1
10900 && UNCHANGED_MODIFIED < MODIFF);
10901 }
10902
10903 /* Nonzero if W doesn't reflect the actual state of current buffer due
10904 to its text or overlays change. FIXME: this may be called when
10905 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10906
10907 static int
10908 window_outdated (struct window *w)
10909 {
10910 return (w->last_modified < MODIFF
10911 || w->last_overlay_modified < OVERLAY_MODIFF);
10912 }
10913
10914 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10915 is enabled and mark of W's buffer was changed since last W's update. */
10916
10917 static int
10918 window_buffer_changed (struct window *w)
10919 {
10920 struct buffer *b = XBUFFER (w->buffer);
10921
10922 eassert (BUFFER_LIVE_P (b));
10923
10924 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10925 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10926 != !NILP (w->region_showing)));
10927 }
10928
10929 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10930
10931 static int
10932 mode_line_update_needed (struct window *w)
10933 {
10934 return (!NILP (w->column_number_displayed)
10935 && !(PT == w->last_point && !window_outdated (w))
10936 && (XFASTINT (w->column_number_displayed) != current_column ()));
10937 }
10938
10939 /***********************************************************************
10940 Mode Lines and Frame Titles
10941 ***********************************************************************/
10942
10943 /* A buffer for constructing non-propertized mode-line strings and
10944 frame titles in it; allocated from the heap in init_xdisp and
10945 resized as needed in store_mode_line_noprop_char. */
10946
10947 static char *mode_line_noprop_buf;
10948
10949 /* The buffer's end, and a current output position in it. */
10950
10951 static char *mode_line_noprop_buf_end;
10952 static char *mode_line_noprop_ptr;
10953
10954 #define MODE_LINE_NOPROP_LEN(start) \
10955 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10956
10957 static enum {
10958 MODE_LINE_DISPLAY = 0,
10959 MODE_LINE_TITLE,
10960 MODE_LINE_NOPROP,
10961 MODE_LINE_STRING
10962 } mode_line_target;
10963
10964 /* Alist that caches the results of :propertize.
10965 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10966 static Lisp_Object mode_line_proptrans_alist;
10967
10968 /* List of strings making up the mode-line. */
10969 static Lisp_Object mode_line_string_list;
10970
10971 /* Base face property when building propertized mode line string. */
10972 static Lisp_Object mode_line_string_face;
10973 static Lisp_Object mode_line_string_face_prop;
10974
10975
10976 /* Unwind data for mode line strings */
10977
10978 static Lisp_Object Vmode_line_unwind_vector;
10979
10980 static Lisp_Object
10981 format_mode_line_unwind_data (struct frame *target_frame,
10982 struct buffer *obuf,
10983 Lisp_Object owin,
10984 int save_proptrans)
10985 {
10986 Lisp_Object vector, tmp;
10987
10988 /* Reduce consing by keeping one vector in
10989 Vwith_echo_area_save_vector. */
10990 vector = Vmode_line_unwind_vector;
10991 Vmode_line_unwind_vector = Qnil;
10992
10993 if (NILP (vector))
10994 vector = Fmake_vector (make_number (10), Qnil);
10995
10996 ASET (vector, 0, make_number (mode_line_target));
10997 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10998 ASET (vector, 2, mode_line_string_list);
10999 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11000 ASET (vector, 4, mode_line_string_face);
11001 ASET (vector, 5, mode_line_string_face_prop);
11002
11003 if (obuf)
11004 XSETBUFFER (tmp, obuf);
11005 else
11006 tmp = Qnil;
11007 ASET (vector, 6, tmp);
11008 ASET (vector, 7, owin);
11009 if (target_frame)
11010 {
11011 /* Similarly to `with-selected-window', if the operation selects
11012 a window on another frame, we must restore that frame's
11013 selected window, and (for a tty) the top-frame. */
11014 ASET (vector, 8, target_frame->selected_window);
11015 if (FRAME_TERMCAP_P (target_frame))
11016 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11017 }
11018
11019 return vector;
11020 }
11021
11022 static Lisp_Object
11023 unwind_format_mode_line (Lisp_Object vector)
11024 {
11025 Lisp_Object old_window = AREF (vector, 7);
11026 Lisp_Object target_frame_window = AREF (vector, 8);
11027 Lisp_Object old_top_frame = AREF (vector, 9);
11028
11029 mode_line_target = XINT (AREF (vector, 0));
11030 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11031 mode_line_string_list = AREF (vector, 2);
11032 if (! EQ (AREF (vector, 3), Qt))
11033 mode_line_proptrans_alist = AREF (vector, 3);
11034 mode_line_string_face = AREF (vector, 4);
11035 mode_line_string_face_prop = AREF (vector, 5);
11036
11037 /* Select window before buffer, since it may change the buffer. */
11038 if (!NILP (old_window))
11039 {
11040 /* If the operation that we are unwinding had selected a window
11041 on a different frame, reset its frame-selected-window. For a
11042 text terminal, reset its top-frame if necessary. */
11043 if (!NILP (target_frame_window))
11044 {
11045 Lisp_Object frame
11046 = WINDOW_FRAME (XWINDOW (target_frame_window));
11047
11048 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11049 Fselect_window (target_frame_window, Qt);
11050
11051 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11052 Fselect_frame (old_top_frame, Qt);
11053 }
11054
11055 Fselect_window (old_window, Qt);
11056 }
11057
11058 if (!NILP (AREF (vector, 6)))
11059 {
11060 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11061 ASET (vector, 6, Qnil);
11062 }
11063
11064 Vmode_line_unwind_vector = vector;
11065 return Qnil;
11066 }
11067
11068
11069 /* Store a single character C for the frame title in mode_line_noprop_buf.
11070 Re-allocate mode_line_noprop_buf if necessary. */
11071
11072 static void
11073 store_mode_line_noprop_char (char c)
11074 {
11075 /* If output position has reached the end of the allocated buffer,
11076 increase the buffer's size. */
11077 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11078 {
11079 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11080 ptrdiff_t size = len;
11081 mode_line_noprop_buf =
11082 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11083 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11084 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11085 }
11086
11087 *mode_line_noprop_ptr++ = c;
11088 }
11089
11090
11091 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11092 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11093 characters that yield more columns than PRECISION; PRECISION <= 0
11094 means copy the whole string. Pad with spaces until FIELD_WIDTH
11095 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11096 pad. Called from display_mode_element when it is used to build a
11097 frame title. */
11098
11099 static int
11100 store_mode_line_noprop (const char *string, int field_width, int precision)
11101 {
11102 const unsigned char *str = (const unsigned char *) string;
11103 int n = 0;
11104 ptrdiff_t dummy, nbytes;
11105
11106 /* Copy at most PRECISION chars from STR. */
11107 nbytes = strlen (string);
11108 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11109 while (nbytes--)
11110 store_mode_line_noprop_char (*str++);
11111
11112 /* Fill up with spaces until FIELD_WIDTH reached. */
11113 while (field_width > 0
11114 && n < field_width)
11115 {
11116 store_mode_line_noprop_char (' ');
11117 ++n;
11118 }
11119
11120 return n;
11121 }
11122
11123 /***********************************************************************
11124 Frame Titles
11125 ***********************************************************************/
11126
11127 #ifdef HAVE_WINDOW_SYSTEM
11128
11129 /* Set the title of FRAME, if it has changed. The title format is
11130 Vicon_title_format if FRAME is iconified, otherwise it is
11131 frame_title_format. */
11132
11133 static void
11134 x_consider_frame_title (Lisp_Object frame)
11135 {
11136 struct frame *f = XFRAME (frame);
11137
11138 if (FRAME_WINDOW_P (f)
11139 || FRAME_MINIBUF_ONLY_P (f)
11140 || f->explicit_name)
11141 {
11142 /* Do we have more than one visible frame on this X display? */
11143 Lisp_Object tail, other_frame, fmt;
11144 ptrdiff_t title_start;
11145 char *title;
11146 ptrdiff_t len;
11147 struct it it;
11148 ptrdiff_t count = SPECPDL_INDEX ();
11149
11150 FOR_EACH_FRAME (tail, other_frame)
11151 {
11152 struct frame *tf = XFRAME (other_frame);
11153
11154 if (tf != f
11155 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11156 && !FRAME_MINIBUF_ONLY_P (tf)
11157 && !EQ (other_frame, tip_frame)
11158 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11159 break;
11160 }
11161
11162 /* Set global variable indicating that multiple frames exist. */
11163 multiple_frames = CONSP (tail);
11164
11165 /* Switch to the buffer of selected window of the frame. Set up
11166 mode_line_target so that display_mode_element will output into
11167 mode_line_noprop_buf; then display the title. */
11168 record_unwind_protect (unwind_format_mode_line,
11169 format_mode_line_unwind_data
11170 (f, current_buffer, selected_window, 0));
11171
11172 Fselect_window (f->selected_window, Qt);
11173 set_buffer_internal_1
11174 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11175 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11176
11177 mode_line_target = MODE_LINE_TITLE;
11178 title_start = MODE_LINE_NOPROP_LEN (0);
11179 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11180 NULL, DEFAULT_FACE_ID);
11181 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11182 len = MODE_LINE_NOPROP_LEN (title_start);
11183 title = mode_line_noprop_buf + title_start;
11184 unbind_to (count, Qnil);
11185
11186 /* Set the title only if it's changed. This avoids consing in
11187 the common case where it hasn't. (If it turns out that we've
11188 already wasted too much time by walking through the list with
11189 display_mode_element, then we might need to optimize at a
11190 higher level than this.) */
11191 if (! STRINGP (f->name)
11192 || SBYTES (f->name) != len
11193 || memcmp (title, SDATA (f->name), len) != 0)
11194 x_implicitly_set_name (f, make_string (title, len), Qnil);
11195 }
11196 }
11197
11198 #endif /* not HAVE_WINDOW_SYSTEM */
11199
11200 \f
11201 /***********************************************************************
11202 Menu Bars
11203 ***********************************************************************/
11204
11205
11206 /* Prepare for redisplay by updating menu-bar item lists when
11207 appropriate. This can call eval. */
11208
11209 void
11210 prepare_menu_bars (void)
11211 {
11212 int all_windows;
11213 struct gcpro gcpro1, gcpro2;
11214 struct frame *f;
11215 Lisp_Object tooltip_frame;
11216
11217 #ifdef HAVE_WINDOW_SYSTEM
11218 tooltip_frame = tip_frame;
11219 #else
11220 tooltip_frame = Qnil;
11221 #endif
11222
11223 /* Update all frame titles based on their buffer names, etc. We do
11224 this before the menu bars so that the buffer-menu will show the
11225 up-to-date frame titles. */
11226 #ifdef HAVE_WINDOW_SYSTEM
11227 if (windows_or_buffers_changed || update_mode_lines)
11228 {
11229 Lisp_Object tail, frame;
11230
11231 FOR_EACH_FRAME (tail, frame)
11232 {
11233 f = XFRAME (frame);
11234 if (!EQ (frame, tooltip_frame)
11235 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11236 x_consider_frame_title (frame);
11237 }
11238 }
11239 #endif /* HAVE_WINDOW_SYSTEM */
11240
11241 /* Update the menu bar item lists, if appropriate. This has to be
11242 done before any actual redisplay or generation of display lines. */
11243 all_windows = (update_mode_lines
11244 || buffer_shared_and_changed ()
11245 || windows_or_buffers_changed);
11246 if (all_windows)
11247 {
11248 Lisp_Object tail, frame;
11249 ptrdiff_t count = SPECPDL_INDEX ();
11250 /* 1 means that update_menu_bar has run its hooks
11251 so any further calls to update_menu_bar shouldn't do so again. */
11252 int menu_bar_hooks_run = 0;
11253
11254 record_unwind_save_match_data ();
11255
11256 FOR_EACH_FRAME (tail, frame)
11257 {
11258 f = XFRAME (frame);
11259
11260 /* Ignore tooltip frame. */
11261 if (EQ (frame, tooltip_frame))
11262 continue;
11263
11264 /* If a window on this frame changed size, report that to
11265 the user and clear the size-change flag. */
11266 if (FRAME_WINDOW_SIZES_CHANGED (f))
11267 {
11268 Lisp_Object functions;
11269
11270 /* Clear flag first in case we get an error below. */
11271 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11272 functions = Vwindow_size_change_functions;
11273 GCPRO2 (tail, functions);
11274
11275 while (CONSP (functions))
11276 {
11277 if (!EQ (XCAR (functions), Qt))
11278 call1 (XCAR (functions), frame);
11279 functions = XCDR (functions);
11280 }
11281 UNGCPRO;
11282 }
11283
11284 GCPRO1 (tail);
11285 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11286 #ifdef HAVE_WINDOW_SYSTEM
11287 update_tool_bar (f, 0);
11288 #endif
11289 #ifdef HAVE_NS
11290 if (windows_or_buffers_changed
11291 && FRAME_NS_P (f))
11292 ns_set_doc_edited
11293 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11294 #endif
11295 UNGCPRO;
11296 }
11297
11298 unbind_to (count, Qnil);
11299 }
11300 else
11301 {
11302 struct frame *sf = SELECTED_FRAME ();
11303 update_menu_bar (sf, 1, 0);
11304 #ifdef HAVE_WINDOW_SYSTEM
11305 update_tool_bar (sf, 1);
11306 #endif
11307 }
11308 }
11309
11310
11311 /* Update the menu bar item list for frame F. This has to be done
11312 before we start to fill in any display lines, because it can call
11313 eval.
11314
11315 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11316
11317 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11318 already ran the menu bar hooks for this redisplay, so there
11319 is no need to run them again. The return value is the
11320 updated value of this flag, to pass to the next call. */
11321
11322 static int
11323 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11324 {
11325 Lisp_Object window;
11326 register struct window *w;
11327
11328 /* If called recursively during a menu update, do nothing. This can
11329 happen when, for instance, an activate-menubar-hook causes a
11330 redisplay. */
11331 if (inhibit_menubar_update)
11332 return hooks_run;
11333
11334 window = FRAME_SELECTED_WINDOW (f);
11335 w = XWINDOW (window);
11336
11337 if (FRAME_WINDOW_P (f)
11338 ?
11339 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11340 || defined (HAVE_NS) || defined (USE_GTK)
11341 FRAME_EXTERNAL_MENU_BAR (f)
11342 #else
11343 FRAME_MENU_BAR_LINES (f) > 0
11344 #endif
11345 : FRAME_MENU_BAR_LINES (f) > 0)
11346 {
11347 /* If the user has switched buffers or windows, we need to
11348 recompute to reflect the new bindings. But we'll
11349 recompute when update_mode_lines is set too; that means
11350 that people can use force-mode-line-update to request
11351 that the menu bar be recomputed. The adverse effect on
11352 the rest of the redisplay algorithm is about the same as
11353 windows_or_buffers_changed anyway. */
11354 if (windows_or_buffers_changed
11355 /* This used to test w->update_mode_line, but we believe
11356 there is no need to recompute the menu in that case. */
11357 || update_mode_lines
11358 || window_buffer_changed (w))
11359 {
11360 struct buffer *prev = current_buffer;
11361 ptrdiff_t count = SPECPDL_INDEX ();
11362
11363 specbind (Qinhibit_menubar_update, Qt);
11364
11365 set_buffer_internal_1 (XBUFFER (w->buffer));
11366 if (save_match_data)
11367 record_unwind_save_match_data ();
11368 if (NILP (Voverriding_local_map_menu_flag))
11369 {
11370 specbind (Qoverriding_terminal_local_map, Qnil);
11371 specbind (Qoverriding_local_map, Qnil);
11372 }
11373
11374 if (!hooks_run)
11375 {
11376 /* Run the Lucid hook. */
11377 safe_run_hooks (Qactivate_menubar_hook);
11378
11379 /* If it has changed current-menubar from previous value,
11380 really recompute the menu-bar from the value. */
11381 if (! NILP (Vlucid_menu_bar_dirty_flag))
11382 call0 (Qrecompute_lucid_menubar);
11383
11384 safe_run_hooks (Qmenu_bar_update_hook);
11385
11386 hooks_run = 1;
11387 }
11388
11389 XSETFRAME (Vmenu_updating_frame, f);
11390 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11391
11392 /* Redisplay the menu bar in case we changed it. */
11393 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11394 || defined (HAVE_NS) || defined (USE_GTK)
11395 if (FRAME_WINDOW_P (f))
11396 {
11397 #if defined (HAVE_NS)
11398 /* All frames on Mac OS share the same menubar. So only
11399 the selected frame should be allowed to set it. */
11400 if (f == SELECTED_FRAME ())
11401 #endif
11402 set_frame_menubar (f, 0, 0);
11403 }
11404 else
11405 /* On a terminal screen, the menu bar is an ordinary screen
11406 line, and this makes it get updated. */
11407 w->update_mode_line = 1;
11408 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11409 /* In the non-toolkit version, the menu bar is an ordinary screen
11410 line, and this makes it get updated. */
11411 w->update_mode_line = 1;
11412 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11413
11414 unbind_to (count, Qnil);
11415 set_buffer_internal_1 (prev);
11416 }
11417 }
11418
11419 return hooks_run;
11420 }
11421
11422
11423 \f
11424 /***********************************************************************
11425 Output Cursor
11426 ***********************************************************************/
11427
11428 #ifdef HAVE_WINDOW_SYSTEM
11429
11430 /* EXPORT:
11431 Nominal cursor position -- where to draw output.
11432 HPOS and VPOS are window relative glyph matrix coordinates.
11433 X and Y are window relative pixel coordinates. */
11434
11435 struct cursor_pos output_cursor;
11436
11437
11438 /* EXPORT:
11439 Set the global variable output_cursor to CURSOR. All cursor
11440 positions are relative to updated_window. */
11441
11442 void
11443 set_output_cursor (struct cursor_pos *cursor)
11444 {
11445 output_cursor.hpos = cursor->hpos;
11446 output_cursor.vpos = cursor->vpos;
11447 output_cursor.x = cursor->x;
11448 output_cursor.y = cursor->y;
11449 }
11450
11451
11452 /* EXPORT for RIF:
11453 Set a nominal cursor position.
11454
11455 HPOS and VPOS are column/row positions in a window glyph matrix. X
11456 and Y are window text area relative pixel positions.
11457
11458 If this is done during an update, updated_window will contain the
11459 window that is being updated and the position is the future output
11460 cursor position for that window. If updated_window is null, use
11461 selected_window and display the cursor at the given position. */
11462
11463 void
11464 x_cursor_to (int vpos, int hpos, int y, int x)
11465 {
11466 struct window *w;
11467
11468 /* If updated_window is not set, work on selected_window. */
11469 if (updated_window)
11470 w = updated_window;
11471 else
11472 w = XWINDOW (selected_window);
11473
11474 /* Set the output cursor. */
11475 output_cursor.hpos = hpos;
11476 output_cursor.vpos = vpos;
11477 output_cursor.x = x;
11478 output_cursor.y = y;
11479
11480 /* If not called as part of an update, really display the cursor.
11481 This will also set the cursor position of W. */
11482 if (updated_window == NULL)
11483 {
11484 block_input ();
11485 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11486 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11487 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11488 unblock_input ();
11489 }
11490 }
11491
11492 #endif /* HAVE_WINDOW_SYSTEM */
11493
11494 \f
11495 /***********************************************************************
11496 Tool-bars
11497 ***********************************************************************/
11498
11499 #ifdef HAVE_WINDOW_SYSTEM
11500
11501 /* Where the mouse was last time we reported a mouse event. */
11502
11503 FRAME_PTR last_mouse_frame;
11504
11505 /* Tool-bar item index of the item on which a mouse button was pressed
11506 or -1. */
11507
11508 int last_tool_bar_item;
11509
11510 /* Select `frame' temporarily without running all the code in
11511 do_switch_frame.
11512 FIXME: Maybe do_switch_frame should be trimmed down similarly
11513 when `norecord' is set. */
11514 static Lisp_Object
11515 fast_set_selected_frame (Lisp_Object frame)
11516 {
11517 if (!EQ (selected_frame, frame))
11518 {
11519 selected_frame = frame;
11520 selected_window = XFRAME (frame)->selected_window;
11521 }
11522 return Qnil;
11523 }
11524
11525 /* Update the tool-bar item list for frame F. This has to be done
11526 before we start to fill in any display lines. Called from
11527 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11528 and restore it here. */
11529
11530 static void
11531 update_tool_bar (struct frame *f, int save_match_data)
11532 {
11533 #if defined (USE_GTK) || defined (HAVE_NS)
11534 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11535 #else
11536 int do_update = WINDOWP (f->tool_bar_window)
11537 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11538 #endif
11539
11540 if (do_update)
11541 {
11542 Lisp_Object window;
11543 struct window *w;
11544
11545 window = FRAME_SELECTED_WINDOW (f);
11546 w = XWINDOW (window);
11547
11548 /* If the user has switched buffers or windows, we need to
11549 recompute to reflect the new bindings. But we'll
11550 recompute when update_mode_lines is set too; that means
11551 that people can use force-mode-line-update to request
11552 that the menu bar be recomputed. The adverse effect on
11553 the rest of the redisplay algorithm is about the same as
11554 windows_or_buffers_changed anyway. */
11555 if (windows_or_buffers_changed
11556 || w->update_mode_line
11557 || update_mode_lines
11558 || window_buffer_changed (w))
11559 {
11560 struct buffer *prev = current_buffer;
11561 ptrdiff_t count = SPECPDL_INDEX ();
11562 Lisp_Object frame, new_tool_bar;
11563 int new_n_tool_bar;
11564 struct gcpro gcpro1;
11565
11566 /* Set current_buffer to the buffer of the selected
11567 window of the frame, so that we get the right local
11568 keymaps. */
11569 set_buffer_internal_1 (XBUFFER (w->buffer));
11570
11571 /* Save match data, if we must. */
11572 if (save_match_data)
11573 record_unwind_save_match_data ();
11574
11575 /* Make sure that we don't accidentally use bogus keymaps. */
11576 if (NILP (Voverriding_local_map_menu_flag))
11577 {
11578 specbind (Qoverriding_terminal_local_map, Qnil);
11579 specbind (Qoverriding_local_map, Qnil);
11580 }
11581
11582 GCPRO1 (new_tool_bar);
11583
11584 /* We must temporarily set the selected frame to this frame
11585 before calling tool_bar_items, because the calculation of
11586 the tool-bar keymap uses the selected frame (see
11587 `tool-bar-make-keymap' in tool-bar.el). */
11588 eassert (EQ (selected_window,
11589 /* Since we only explicitly preserve selected_frame,
11590 check that selected_window would be redundant. */
11591 XFRAME (selected_frame)->selected_window));
11592 record_unwind_protect (fast_set_selected_frame, selected_frame);
11593 XSETFRAME (frame, f);
11594 fast_set_selected_frame (frame);
11595
11596 /* Build desired tool-bar items from keymaps. */
11597 new_tool_bar
11598 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11599 &new_n_tool_bar);
11600
11601 /* Redisplay the tool-bar if we changed it. */
11602 if (new_n_tool_bar != f->n_tool_bar_items
11603 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11604 {
11605 /* Redisplay that happens asynchronously due to an expose event
11606 may access f->tool_bar_items. Make sure we update both
11607 variables within BLOCK_INPUT so no such event interrupts. */
11608 block_input ();
11609 fset_tool_bar_items (f, new_tool_bar);
11610 f->n_tool_bar_items = new_n_tool_bar;
11611 w->update_mode_line = 1;
11612 unblock_input ();
11613 }
11614
11615 UNGCPRO;
11616
11617 unbind_to (count, Qnil);
11618 set_buffer_internal_1 (prev);
11619 }
11620 }
11621 }
11622
11623
11624 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11625 F's desired tool-bar contents. F->tool_bar_items must have
11626 been set up previously by calling prepare_menu_bars. */
11627
11628 static void
11629 build_desired_tool_bar_string (struct frame *f)
11630 {
11631 int i, size, size_needed;
11632 struct gcpro gcpro1, gcpro2, gcpro3;
11633 Lisp_Object image, plist, props;
11634
11635 image = plist = props = Qnil;
11636 GCPRO3 (image, plist, props);
11637
11638 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11639 Otherwise, make a new string. */
11640
11641 /* The size of the string we might be able to reuse. */
11642 size = (STRINGP (f->desired_tool_bar_string)
11643 ? SCHARS (f->desired_tool_bar_string)
11644 : 0);
11645
11646 /* We need one space in the string for each image. */
11647 size_needed = f->n_tool_bar_items;
11648
11649 /* Reuse f->desired_tool_bar_string, if possible. */
11650 if (size < size_needed || NILP (f->desired_tool_bar_string))
11651 fset_desired_tool_bar_string
11652 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11653 else
11654 {
11655 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11656 Fremove_text_properties (make_number (0), make_number (size),
11657 props, f->desired_tool_bar_string);
11658 }
11659
11660 /* Put a `display' property on the string for the images to display,
11661 put a `menu_item' property on tool-bar items with a value that
11662 is the index of the item in F's tool-bar item vector. */
11663 for (i = 0; i < f->n_tool_bar_items; ++i)
11664 {
11665 #define PROP(IDX) \
11666 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11667
11668 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11669 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11670 int hmargin, vmargin, relief, idx, end;
11671
11672 /* If image is a vector, choose the image according to the
11673 button state. */
11674 image = PROP (TOOL_BAR_ITEM_IMAGES);
11675 if (VECTORP (image))
11676 {
11677 if (enabled_p)
11678 idx = (selected_p
11679 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11680 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11681 else
11682 idx = (selected_p
11683 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11684 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11685
11686 eassert (ASIZE (image) >= idx);
11687 image = AREF (image, idx);
11688 }
11689 else
11690 idx = -1;
11691
11692 /* Ignore invalid image specifications. */
11693 if (!valid_image_p (image))
11694 continue;
11695
11696 /* Display the tool-bar button pressed, or depressed. */
11697 plist = Fcopy_sequence (XCDR (image));
11698
11699 /* Compute margin and relief to draw. */
11700 relief = (tool_bar_button_relief >= 0
11701 ? tool_bar_button_relief
11702 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11703 hmargin = vmargin = relief;
11704
11705 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11706 INT_MAX - max (hmargin, vmargin)))
11707 {
11708 hmargin += XFASTINT (Vtool_bar_button_margin);
11709 vmargin += XFASTINT (Vtool_bar_button_margin);
11710 }
11711 else if (CONSP (Vtool_bar_button_margin))
11712 {
11713 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11714 INT_MAX - hmargin))
11715 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11716
11717 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11718 INT_MAX - vmargin))
11719 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11720 }
11721
11722 if (auto_raise_tool_bar_buttons_p)
11723 {
11724 /* Add a `:relief' property to the image spec if the item is
11725 selected. */
11726 if (selected_p)
11727 {
11728 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11729 hmargin -= relief;
11730 vmargin -= relief;
11731 }
11732 }
11733 else
11734 {
11735 /* If image is selected, display it pressed, i.e. with a
11736 negative relief. If it's not selected, display it with a
11737 raised relief. */
11738 plist = Fplist_put (plist, QCrelief,
11739 (selected_p
11740 ? make_number (-relief)
11741 : make_number (relief)));
11742 hmargin -= relief;
11743 vmargin -= relief;
11744 }
11745
11746 /* Put a margin around the image. */
11747 if (hmargin || vmargin)
11748 {
11749 if (hmargin == vmargin)
11750 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11751 else
11752 plist = Fplist_put (plist, QCmargin,
11753 Fcons (make_number (hmargin),
11754 make_number (vmargin)));
11755 }
11756
11757 /* If button is not enabled, and we don't have special images
11758 for the disabled state, make the image appear disabled by
11759 applying an appropriate algorithm to it. */
11760 if (!enabled_p && idx < 0)
11761 plist = Fplist_put (plist, QCconversion, Qdisabled);
11762
11763 /* Put a `display' text property on the string for the image to
11764 display. Put a `menu-item' property on the string that gives
11765 the start of this item's properties in the tool-bar items
11766 vector. */
11767 image = Fcons (Qimage, plist);
11768 props = list4 (Qdisplay, image,
11769 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11770
11771 /* Let the last image hide all remaining spaces in the tool bar
11772 string. The string can be longer than needed when we reuse a
11773 previous string. */
11774 if (i + 1 == f->n_tool_bar_items)
11775 end = SCHARS (f->desired_tool_bar_string);
11776 else
11777 end = i + 1;
11778 Fadd_text_properties (make_number (i), make_number (end),
11779 props, f->desired_tool_bar_string);
11780 #undef PROP
11781 }
11782
11783 UNGCPRO;
11784 }
11785
11786
11787 /* Display one line of the tool-bar of frame IT->f.
11788
11789 HEIGHT specifies the desired height of the tool-bar line.
11790 If the actual height of the glyph row is less than HEIGHT, the
11791 row's height is increased to HEIGHT, and the icons are centered
11792 vertically in the new height.
11793
11794 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11795 count a final empty row in case the tool-bar width exactly matches
11796 the window width.
11797 */
11798
11799 static void
11800 display_tool_bar_line (struct it *it, int height)
11801 {
11802 struct glyph_row *row = it->glyph_row;
11803 int max_x = it->last_visible_x;
11804 struct glyph *last;
11805
11806 prepare_desired_row (row);
11807 row->y = it->current_y;
11808
11809 /* Note that this isn't made use of if the face hasn't a box,
11810 so there's no need to check the face here. */
11811 it->start_of_box_run_p = 1;
11812
11813 while (it->current_x < max_x)
11814 {
11815 int x, n_glyphs_before, i, nglyphs;
11816 struct it it_before;
11817
11818 /* Get the next display element. */
11819 if (!get_next_display_element (it))
11820 {
11821 /* Don't count empty row if we are counting needed tool-bar lines. */
11822 if (height < 0 && !it->hpos)
11823 return;
11824 break;
11825 }
11826
11827 /* Produce glyphs. */
11828 n_glyphs_before = row->used[TEXT_AREA];
11829 it_before = *it;
11830
11831 PRODUCE_GLYPHS (it);
11832
11833 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11834 i = 0;
11835 x = it_before.current_x;
11836 while (i < nglyphs)
11837 {
11838 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11839
11840 if (x + glyph->pixel_width > max_x)
11841 {
11842 /* Glyph doesn't fit on line. Backtrack. */
11843 row->used[TEXT_AREA] = n_glyphs_before;
11844 *it = it_before;
11845 /* If this is the only glyph on this line, it will never fit on the
11846 tool-bar, so skip it. But ensure there is at least one glyph,
11847 so we don't accidentally disable the tool-bar. */
11848 if (n_glyphs_before == 0
11849 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11850 break;
11851 goto out;
11852 }
11853
11854 ++it->hpos;
11855 x += glyph->pixel_width;
11856 ++i;
11857 }
11858
11859 /* Stop at line end. */
11860 if (ITERATOR_AT_END_OF_LINE_P (it))
11861 break;
11862
11863 set_iterator_to_next (it, 1);
11864 }
11865
11866 out:;
11867
11868 row->displays_text_p = row->used[TEXT_AREA] != 0;
11869
11870 /* Use default face for the border below the tool bar.
11871
11872 FIXME: When auto-resize-tool-bars is grow-only, there is
11873 no additional border below the possibly empty tool-bar lines.
11874 So to make the extra empty lines look "normal", we have to
11875 use the tool-bar face for the border too. */
11876 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11877 it->face_id = DEFAULT_FACE_ID;
11878
11879 extend_face_to_end_of_line (it);
11880 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11881 last->right_box_line_p = 1;
11882 if (last == row->glyphs[TEXT_AREA])
11883 last->left_box_line_p = 1;
11884
11885 /* Make line the desired height and center it vertically. */
11886 if ((height -= it->max_ascent + it->max_descent) > 0)
11887 {
11888 /* Don't add more than one line height. */
11889 height %= FRAME_LINE_HEIGHT (it->f);
11890 it->max_ascent += height / 2;
11891 it->max_descent += (height + 1) / 2;
11892 }
11893
11894 compute_line_metrics (it);
11895
11896 /* If line is empty, make it occupy the rest of the tool-bar. */
11897 if (!row->displays_text_p)
11898 {
11899 row->height = row->phys_height = it->last_visible_y - row->y;
11900 row->visible_height = row->height;
11901 row->ascent = row->phys_ascent = 0;
11902 row->extra_line_spacing = 0;
11903 }
11904
11905 row->full_width_p = 1;
11906 row->continued_p = 0;
11907 row->truncated_on_left_p = 0;
11908 row->truncated_on_right_p = 0;
11909
11910 it->current_x = it->hpos = 0;
11911 it->current_y += row->height;
11912 ++it->vpos;
11913 ++it->glyph_row;
11914 }
11915
11916
11917 /* Max tool-bar height. */
11918
11919 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11920 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11921
11922 /* Value is the number of screen lines needed to make all tool-bar
11923 items of frame F visible. The number of actual rows needed is
11924 returned in *N_ROWS if non-NULL. */
11925
11926 static int
11927 tool_bar_lines_needed (struct frame *f, int *n_rows)
11928 {
11929 struct window *w = XWINDOW (f->tool_bar_window);
11930 struct it it;
11931 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11932 the desired matrix, so use (unused) mode-line row as temporary row to
11933 avoid destroying the first tool-bar row. */
11934 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11935
11936 /* Initialize an iterator for iteration over
11937 F->desired_tool_bar_string in the tool-bar window of frame F. */
11938 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11939 it.first_visible_x = 0;
11940 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11941 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11942 it.paragraph_embedding = L2R;
11943
11944 while (!ITERATOR_AT_END_P (&it))
11945 {
11946 clear_glyph_row (temp_row);
11947 it.glyph_row = temp_row;
11948 display_tool_bar_line (&it, -1);
11949 }
11950 clear_glyph_row (temp_row);
11951
11952 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11953 if (n_rows)
11954 *n_rows = it.vpos > 0 ? it.vpos : -1;
11955
11956 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11957 }
11958
11959
11960 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11961 0, 1, 0,
11962 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11963 If FRAME is nil or omitted, use the selected frame. */)
11964 (Lisp_Object frame)
11965 {
11966 struct frame *f = decode_any_frame (frame);
11967 struct window *w;
11968 int nlines = 0;
11969
11970 if (WINDOWP (f->tool_bar_window)
11971 && (w = XWINDOW (f->tool_bar_window),
11972 WINDOW_TOTAL_LINES (w) > 0))
11973 {
11974 update_tool_bar (f, 1);
11975 if (f->n_tool_bar_items)
11976 {
11977 build_desired_tool_bar_string (f);
11978 nlines = tool_bar_lines_needed (f, NULL);
11979 }
11980 }
11981
11982 return make_number (nlines);
11983 }
11984
11985
11986 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11987 height should be changed. */
11988
11989 static int
11990 redisplay_tool_bar (struct frame *f)
11991 {
11992 struct window *w;
11993 struct it it;
11994 struct glyph_row *row;
11995
11996 #if defined (USE_GTK) || defined (HAVE_NS)
11997 if (FRAME_EXTERNAL_TOOL_BAR (f))
11998 update_frame_tool_bar (f);
11999 return 0;
12000 #endif
12001
12002 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12003 do anything. This means you must start with tool-bar-lines
12004 non-zero to get the auto-sizing effect. Or in other words, you
12005 can turn off tool-bars by specifying tool-bar-lines zero. */
12006 if (!WINDOWP (f->tool_bar_window)
12007 || (w = XWINDOW (f->tool_bar_window),
12008 WINDOW_TOTAL_LINES (w) == 0))
12009 return 0;
12010
12011 /* Set up an iterator for the tool-bar window. */
12012 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12013 it.first_visible_x = 0;
12014 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12015 row = it.glyph_row;
12016
12017 /* Build a string that represents the contents of the tool-bar. */
12018 build_desired_tool_bar_string (f);
12019 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12020 /* FIXME: This should be controlled by a user option. But it
12021 doesn't make sense to have an R2L tool bar if the menu bar cannot
12022 be drawn also R2L, and making the menu bar R2L is tricky due
12023 toolkit-specific code that implements it. If an R2L tool bar is
12024 ever supported, display_tool_bar_line should also be augmented to
12025 call unproduce_glyphs like display_line and display_string
12026 do. */
12027 it.paragraph_embedding = L2R;
12028
12029 if (f->n_tool_bar_rows == 0)
12030 {
12031 int nlines;
12032
12033 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12034 nlines != WINDOW_TOTAL_LINES (w)))
12035 {
12036 Lisp_Object frame;
12037 int old_height = WINDOW_TOTAL_LINES (w);
12038
12039 XSETFRAME (frame, f);
12040 Fmodify_frame_parameters (frame,
12041 Fcons (Fcons (Qtool_bar_lines,
12042 make_number (nlines)),
12043 Qnil));
12044 if (WINDOW_TOTAL_LINES (w) != old_height)
12045 {
12046 clear_glyph_matrix (w->desired_matrix);
12047 fonts_changed_p = 1;
12048 return 1;
12049 }
12050 }
12051 }
12052
12053 /* Display as many lines as needed to display all tool-bar items. */
12054
12055 if (f->n_tool_bar_rows > 0)
12056 {
12057 int border, rows, height, extra;
12058
12059 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12060 border = XINT (Vtool_bar_border);
12061 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12062 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12063 else if (EQ (Vtool_bar_border, Qborder_width))
12064 border = f->border_width;
12065 else
12066 border = 0;
12067 if (border < 0)
12068 border = 0;
12069
12070 rows = f->n_tool_bar_rows;
12071 height = max (1, (it.last_visible_y - border) / rows);
12072 extra = it.last_visible_y - border - height * rows;
12073
12074 while (it.current_y < it.last_visible_y)
12075 {
12076 int h = 0;
12077 if (extra > 0 && rows-- > 0)
12078 {
12079 h = (extra + rows - 1) / rows;
12080 extra -= h;
12081 }
12082 display_tool_bar_line (&it, height + h);
12083 }
12084 }
12085 else
12086 {
12087 while (it.current_y < it.last_visible_y)
12088 display_tool_bar_line (&it, 0);
12089 }
12090
12091 /* It doesn't make much sense to try scrolling in the tool-bar
12092 window, so don't do it. */
12093 w->desired_matrix->no_scrolling_p = 1;
12094 w->must_be_updated_p = 1;
12095
12096 if (!NILP (Vauto_resize_tool_bars))
12097 {
12098 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12099 int change_height_p = 0;
12100
12101 /* If we couldn't display everything, change the tool-bar's
12102 height if there is room for more. */
12103 if (IT_STRING_CHARPOS (it) < it.end_charpos
12104 && it.current_y < max_tool_bar_height)
12105 change_height_p = 1;
12106
12107 row = it.glyph_row - 1;
12108
12109 /* If there are blank lines at the end, except for a partially
12110 visible blank line at the end that is smaller than
12111 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12112 if (!row->displays_text_p
12113 && row->height >= FRAME_LINE_HEIGHT (f))
12114 change_height_p = 1;
12115
12116 /* If row displays tool-bar items, but is partially visible,
12117 change the tool-bar's height. */
12118 if (row->displays_text_p
12119 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12120 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12121 change_height_p = 1;
12122
12123 /* Resize windows as needed by changing the `tool-bar-lines'
12124 frame parameter. */
12125 if (change_height_p)
12126 {
12127 Lisp_Object frame;
12128 int old_height = WINDOW_TOTAL_LINES (w);
12129 int nrows;
12130 int nlines = tool_bar_lines_needed (f, &nrows);
12131
12132 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12133 && !f->minimize_tool_bar_window_p)
12134 ? (nlines > old_height)
12135 : (nlines != old_height));
12136 f->minimize_tool_bar_window_p = 0;
12137
12138 if (change_height_p)
12139 {
12140 XSETFRAME (frame, f);
12141 Fmodify_frame_parameters (frame,
12142 Fcons (Fcons (Qtool_bar_lines,
12143 make_number (nlines)),
12144 Qnil));
12145 if (WINDOW_TOTAL_LINES (w) != old_height)
12146 {
12147 clear_glyph_matrix (w->desired_matrix);
12148 f->n_tool_bar_rows = nrows;
12149 fonts_changed_p = 1;
12150 return 1;
12151 }
12152 }
12153 }
12154 }
12155
12156 f->minimize_tool_bar_window_p = 0;
12157 return 0;
12158 }
12159
12160
12161 /* Get information about the tool-bar item which is displayed in GLYPH
12162 on frame F. Return in *PROP_IDX the index where tool-bar item
12163 properties start in F->tool_bar_items. Value is zero if
12164 GLYPH doesn't display a tool-bar item. */
12165
12166 static int
12167 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12168 {
12169 Lisp_Object prop;
12170 int success_p;
12171 int charpos;
12172
12173 /* This function can be called asynchronously, which means we must
12174 exclude any possibility that Fget_text_property signals an
12175 error. */
12176 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12177 charpos = max (0, charpos);
12178
12179 /* Get the text property `menu-item' at pos. The value of that
12180 property is the start index of this item's properties in
12181 F->tool_bar_items. */
12182 prop = Fget_text_property (make_number (charpos),
12183 Qmenu_item, f->current_tool_bar_string);
12184 if (INTEGERP (prop))
12185 {
12186 *prop_idx = XINT (prop);
12187 success_p = 1;
12188 }
12189 else
12190 success_p = 0;
12191
12192 return success_p;
12193 }
12194
12195 \f
12196 /* Get information about the tool-bar item at position X/Y on frame F.
12197 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12198 the current matrix of the tool-bar window of F, or NULL if not
12199 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12200 item in F->tool_bar_items. Value is
12201
12202 -1 if X/Y is not on a tool-bar item
12203 0 if X/Y is on the same item that was highlighted before.
12204 1 otherwise. */
12205
12206 static int
12207 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12208 int *hpos, int *vpos, int *prop_idx)
12209 {
12210 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12211 struct window *w = XWINDOW (f->tool_bar_window);
12212 int area;
12213
12214 /* Find the glyph under X/Y. */
12215 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12216 if (*glyph == NULL)
12217 return -1;
12218
12219 /* Get the start of this tool-bar item's properties in
12220 f->tool_bar_items. */
12221 if (!tool_bar_item_info (f, *glyph, prop_idx))
12222 return -1;
12223
12224 /* Is mouse on the highlighted item? */
12225 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12226 && *vpos >= hlinfo->mouse_face_beg_row
12227 && *vpos <= hlinfo->mouse_face_end_row
12228 && (*vpos > hlinfo->mouse_face_beg_row
12229 || *hpos >= hlinfo->mouse_face_beg_col)
12230 && (*vpos < hlinfo->mouse_face_end_row
12231 || *hpos < hlinfo->mouse_face_end_col
12232 || hlinfo->mouse_face_past_end))
12233 return 0;
12234
12235 return 1;
12236 }
12237
12238
12239 /* EXPORT:
12240 Handle mouse button event on the tool-bar of frame F, at
12241 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12242 0 for button release. MODIFIERS is event modifiers for button
12243 release. */
12244
12245 void
12246 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12247 int modifiers)
12248 {
12249 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12250 struct window *w = XWINDOW (f->tool_bar_window);
12251 int hpos, vpos, prop_idx;
12252 struct glyph *glyph;
12253 Lisp_Object enabled_p;
12254
12255 /* If not on the highlighted tool-bar item, return. */
12256 frame_to_window_pixel_xy (w, &x, &y);
12257 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12258 return;
12259
12260 /* If item is disabled, do nothing. */
12261 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12262 if (NILP (enabled_p))
12263 return;
12264
12265 if (down_p)
12266 {
12267 /* Show item in pressed state. */
12268 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12269 last_tool_bar_item = prop_idx;
12270 }
12271 else
12272 {
12273 Lisp_Object key, frame;
12274 struct input_event event;
12275 EVENT_INIT (event);
12276
12277 /* Show item in released state. */
12278 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12279
12280 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12281
12282 XSETFRAME (frame, f);
12283 event.kind = TOOL_BAR_EVENT;
12284 event.frame_or_window = frame;
12285 event.arg = frame;
12286 kbd_buffer_store_event (&event);
12287
12288 event.kind = TOOL_BAR_EVENT;
12289 event.frame_or_window = frame;
12290 event.arg = key;
12291 event.modifiers = modifiers;
12292 kbd_buffer_store_event (&event);
12293 last_tool_bar_item = -1;
12294 }
12295 }
12296
12297
12298 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12299 tool-bar window-relative coordinates X/Y. Called from
12300 note_mouse_highlight. */
12301
12302 static void
12303 note_tool_bar_highlight (struct frame *f, int x, int y)
12304 {
12305 Lisp_Object window = f->tool_bar_window;
12306 struct window *w = XWINDOW (window);
12307 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12308 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12309 int hpos, vpos;
12310 struct glyph *glyph;
12311 struct glyph_row *row;
12312 int i;
12313 Lisp_Object enabled_p;
12314 int prop_idx;
12315 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12316 int mouse_down_p, rc;
12317
12318 /* Function note_mouse_highlight is called with negative X/Y
12319 values when mouse moves outside of the frame. */
12320 if (x <= 0 || y <= 0)
12321 {
12322 clear_mouse_face (hlinfo);
12323 return;
12324 }
12325
12326 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12327 if (rc < 0)
12328 {
12329 /* Not on tool-bar item. */
12330 clear_mouse_face (hlinfo);
12331 return;
12332 }
12333 else if (rc == 0)
12334 /* On same tool-bar item as before. */
12335 goto set_help_echo;
12336
12337 clear_mouse_face (hlinfo);
12338
12339 /* Mouse is down, but on different tool-bar item? */
12340 mouse_down_p = (dpyinfo->grabbed
12341 && f == last_mouse_frame
12342 && FRAME_LIVE_P (f));
12343 if (mouse_down_p
12344 && last_tool_bar_item != prop_idx)
12345 return;
12346
12347 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12348
12349 /* If tool-bar item is not enabled, don't highlight it. */
12350 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12351 if (!NILP (enabled_p))
12352 {
12353 /* Compute the x-position of the glyph. In front and past the
12354 image is a space. We include this in the highlighted area. */
12355 row = MATRIX_ROW (w->current_matrix, vpos);
12356 for (i = x = 0; i < hpos; ++i)
12357 x += row->glyphs[TEXT_AREA][i].pixel_width;
12358
12359 /* Record this as the current active region. */
12360 hlinfo->mouse_face_beg_col = hpos;
12361 hlinfo->mouse_face_beg_row = vpos;
12362 hlinfo->mouse_face_beg_x = x;
12363 hlinfo->mouse_face_beg_y = row->y;
12364 hlinfo->mouse_face_past_end = 0;
12365
12366 hlinfo->mouse_face_end_col = hpos + 1;
12367 hlinfo->mouse_face_end_row = vpos;
12368 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12369 hlinfo->mouse_face_end_y = row->y;
12370 hlinfo->mouse_face_window = window;
12371 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12372
12373 /* Display it as active. */
12374 show_mouse_face (hlinfo, draw);
12375 }
12376
12377 set_help_echo:
12378
12379 /* Set help_echo_string to a help string to display for this tool-bar item.
12380 XTread_socket does the rest. */
12381 help_echo_object = help_echo_window = Qnil;
12382 help_echo_pos = -1;
12383 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12384 if (NILP (help_echo_string))
12385 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12386 }
12387
12388 #endif /* HAVE_WINDOW_SYSTEM */
12389
12390
12391 \f
12392 /************************************************************************
12393 Horizontal scrolling
12394 ************************************************************************/
12395
12396 static int hscroll_window_tree (Lisp_Object);
12397 static int hscroll_windows (Lisp_Object);
12398
12399 /* For all leaf windows in the window tree rooted at WINDOW, set their
12400 hscroll value so that PT is (i) visible in the window, and (ii) so
12401 that it is not within a certain margin at the window's left and
12402 right border. Value is non-zero if any window's hscroll has been
12403 changed. */
12404
12405 static int
12406 hscroll_window_tree (Lisp_Object window)
12407 {
12408 int hscrolled_p = 0;
12409 int hscroll_relative_p = FLOATP (Vhscroll_step);
12410 int hscroll_step_abs = 0;
12411 double hscroll_step_rel = 0;
12412
12413 if (hscroll_relative_p)
12414 {
12415 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12416 if (hscroll_step_rel < 0)
12417 {
12418 hscroll_relative_p = 0;
12419 hscroll_step_abs = 0;
12420 }
12421 }
12422 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12423 {
12424 hscroll_step_abs = XINT (Vhscroll_step);
12425 if (hscroll_step_abs < 0)
12426 hscroll_step_abs = 0;
12427 }
12428 else
12429 hscroll_step_abs = 0;
12430
12431 while (WINDOWP (window))
12432 {
12433 struct window *w = XWINDOW (window);
12434
12435 if (WINDOWP (w->hchild))
12436 hscrolled_p |= hscroll_window_tree (w->hchild);
12437 else if (WINDOWP (w->vchild))
12438 hscrolled_p |= hscroll_window_tree (w->vchild);
12439 else if (w->cursor.vpos >= 0)
12440 {
12441 int h_margin;
12442 int text_area_width;
12443 struct glyph_row *current_cursor_row
12444 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12445 struct glyph_row *desired_cursor_row
12446 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12447 struct glyph_row *cursor_row
12448 = (desired_cursor_row->enabled_p
12449 ? desired_cursor_row
12450 : current_cursor_row);
12451 int row_r2l_p = cursor_row->reversed_p;
12452
12453 text_area_width = window_box_width (w, TEXT_AREA);
12454
12455 /* Scroll when cursor is inside this scroll margin. */
12456 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12457
12458 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12459 /* For left-to-right rows, hscroll when cursor is either
12460 (i) inside the right hscroll margin, or (ii) if it is
12461 inside the left margin and the window is already
12462 hscrolled. */
12463 && ((!row_r2l_p
12464 && ((w->hscroll
12465 && w->cursor.x <= h_margin)
12466 || (cursor_row->enabled_p
12467 && cursor_row->truncated_on_right_p
12468 && (w->cursor.x >= text_area_width - h_margin))))
12469 /* For right-to-left rows, the logic is similar,
12470 except that rules for scrolling to left and right
12471 are reversed. E.g., if cursor.x <= h_margin, we
12472 need to hscroll "to the right" unconditionally,
12473 and that will scroll the screen to the left so as
12474 to reveal the next portion of the row. */
12475 || (row_r2l_p
12476 && ((cursor_row->enabled_p
12477 /* FIXME: It is confusing to set the
12478 truncated_on_right_p flag when R2L rows
12479 are actually truncated on the left. */
12480 && cursor_row->truncated_on_right_p
12481 && w->cursor.x <= h_margin)
12482 || (w->hscroll
12483 && (w->cursor.x >= text_area_width - h_margin))))))
12484 {
12485 struct it it;
12486 ptrdiff_t hscroll;
12487 struct buffer *saved_current_buffer;
12488 ptrdiff_t pt;
12489 int wanted_x;
12490
12491 /* Find point in a display of infinite width. */
12492 saved_current_buffer = current_buffer;
12493 current_buffer = XBUFFER (w->buffer);
12494
12495 if (w == XWINDOW (selected_window))
12496 pt = PT;
12497 else
12498 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12499
12500 /* Move iterator to pt starting at cursor_row->start in
12501 a line with infinite width. */
12502 init_to_row_start (&it, w, cursor_row);
12503 it.last_visible_x = INFINITY;
12504 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12505 current_buffer = saved_current_buffer;
12506
12507 /* Position cursor in window. */
12508 if (!hscroll_relative_p && hscroll_step_abs == 0)
12509 hscroll = max (0, (it.current_x
12510 - (ITERATOR_AT_END_OF_LINE_P (&it)
12511 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12512 : (text_area_width / 2))))
12513 / FRAME_COLUMN_WIDTH (it.f);
12514 else if ((!row_r2l_p
12515 && w->cursor.x >= text_area_width - h_margin)
12516 || (row_r2l_p && w->cursor.x <= h_margin))
12517 {
12518 if (hscroll_relative_p)
12519 wanted_x = text_area_width * (1 - hscroll_step_rel)
12520 - h_margin;
12521 else
12522 wanted_x = text_area_width
12523 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12524 - h_margin;
12525 hscroll
12526 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12527 }
12528 else
12529 {
12530 if (hscroll_relative_p)
12531 wanted_x = text_area_width * hscroll_step_rel
12532 + h_margin;
12533 else
12534 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12535 + h_margin;
12536 hscroll
12537 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12538 }
12539 hscroll = max (hscroll, w->min_hscroll);
12540
12541 /* Don't prevent redisplay optimizations if hscroll
12542 hasn't changed, as it will unnecessarily slow down
12543 redisplay. */
12544 if (w->hscroll != hscroll)
12545 {
12546 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12547 w->hscroll = hscroll;
12548 hscrolled_p = 1;
12549 }
12550 }
12551 }
12552
12553 window = w->next;
12554 }
12555
12556 /* Value is non-zero if hscroll of any leaf window has been changed. */
12557 return hscrolled_p;
12558 }
12559
12560
12561 /* Set hscroll so that cursor is visible and not inside horizontal
12562 scroll margins for all windows in the tree rooted at WINDOW. See
12563 also hscroll_window_tree above. Value is non-zero if any window's
12564 hscroll has been changed. If it has, desired matrices on the frame
12565 of WINDOW are cleared. */
12566
12567 static int
12568 hscroll_windows (Lisp_Object window)
12569 {
12570 int hscrolled_p = hscroll_window_tree (window);
12571 if (hscrolled_p)
12572 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12573 return hscrolled_p;
12574 }
12575
12576
12577 \f
12578 /************************************************************************
12579 Redisplay
12580 ************************************************************************/
12581
12582 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12583 to a non-zero value. This is sometimes handy to have in a debugger
12584 session. */
12585
12586 #ifdef GLYPH_DEBUG
12587
12588 /* First and last unchanged row for try_window_id. */
12589
12590 static int debug_first_unchanged_at_end_vpos;
12591 static int debug_last_unchanged_at_beg_vpos;
12592
12593 /* Delta vpos and y. */
12594
12595 static int debug_dvpos, debug_dy;
12596
12597 /* Delta in characters and bytes for try_window_id. */
12598
12599 static ptrdiff_t debug_delta, debug_delta_bytes;
12600
12601 /* Values of window_end_pos and window_end_vpos at the end of
12602 try_window_id. */
12603
12604 static ptrdiff_t debug_end_vpos;
12605
12606 /* Append a string to W->desired_matrix->method. FMT is a printf
12607 format string. If trace_redisplay_p is non-zero also printf the
12608 resulting string to stderr. */
12609
12610 static void debug_method_add (struct window *, char const *, ...)
12611 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12612
12613 static void
12614 debug_method_add (struct window *w, char const *fmt, ...)
12615 {
12616 char *method = w->desired_matrix->method;
12617 int len = strlen (method);
12618 int size = sizeof w->desired_matrix->method;
12619 int remaining = size - len - 1;
12620 va_list ap;
12621
12622 if (len && remaining)
12623 {
12624 method[len] = '|';
12625 --remaining, ++len;
12626 }
12627
12628 va_start (ap, fmt);
12629 vsnprintf (method + len, remaining + 1, fmt, ap);
12630 va_end (ap);
12631
12632 if (trace_redisplay_p)
12633 fprintf (stderr, "%p (%s): %s\n",
12634 w,
12635 ((BUFFERP (w->buffer)
12636 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12637 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12638 : "no buffer"),
12639 method + len);
12640 }
12641
12642 #endif /* GLYPH_DEBUG */
12643
12644
12645 /* Value is non-zero if all changes in window W, which displays
12646 current_buffer, are in the text between START and END. START is a
12647 buffer position, END is given as a distance from Z. Used in
12648 redisplay_internal for display optimization. */
12649
12650 static int
12651 text_outside_line_unchanged_p (struct window *w,
12652 ptrdiff_t start, ptrdiff_t end)
12653 {
12654 int unchanged_p = 1;
12655
12656 /* If text or overlays have changed, see where. */
12657 if (window_outdated (w))
12658 {
12659 /* Gap in the line? */
12660 if (GPT < start || Z - GPT < end)
12661 unchanged_p = 0;
12662
12663 /* Changes start in front of the line, or end after it? */
12664 if (unchanged_p
12665 && (BEG_UNCHANGED < start - 1
12666 || END_UNCHANGED < end))
12667 unchanged_p = 0;
12668
12669 /* If selective display, can't optimize if changes start at the
12670 beginning of the line. */
12671 if (unchanged_p
12672 && INTEGERP (BVAR (current_buffer, selective_display))
12673 && XINT (BVAR (current_buffer, selective_display)) > 0
12674 && (BEG_UNCHANGED < start || GPT <= start))
12675 unchanged_p = 0;
12676
12677 /* If there are overlays at the start or end of the line, these
12678 may have overlay strings with newlines in them. A change at
12679 START, for instance, may actually concern the display of such
12680 overlay strings as well, and they are displayed on different
12681 lines. So, quickly rule out this case. (For the future, it
12682 might be desirable to implement something more telling than
12683 just BEG/END_UNCHANGED.) */
12684 if (unchanged_p)
12685 {
12686 if (BEG + BEG_UNCHANGED == start
12687 && overlay_touches_p (start))
12688 unchanged_p = 0;
12689 if (END_UNCHANGED == end
12690 && overlay_touches_p (Z - end))
12691 unchanged_p = 0;
12692 }
12693
12694 /* Under bidi reordering, adding or deleting a character in the
12695 beginning of a paragraph, before the first strong directional
12696 character, can change the base direction of the paragraph (unless
12697 the buffer specifies a fixed paragraph direction), which will
12698 require to redisplay the whole paragraph. It might be worthwhile
12699 to find the paragraph limits and widen the range of redisplayed
12700 lines to that, but for now just give up this optimization. */
12701 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12702 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12703 unchanged_p = 0;
12704 }
12705
12706 return unchanged_p;
12707 }
12708
12709
12710 /* Do a frame update, taking possible shortcuts into account. This is
12711 the main external entry point for redisplay.
12712
12713 If the last redisplay displayed an echo area message and that message
12714 is no longer requested, we clear the echo area or bring back the
12715 mini-buffer if that is in use. */
12716
12717 void
12718 redisplay (void)
12719 {
12720 redisplay_internal ();
12721 }
12722
12723
12724 static Lisp_Object
12725 overlay_arrow_string_or_property (Lisp_Object var)
12726 {
12727 Lisp_Object val;
12728
12729 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12730 return val;
12731
12732 return Voverlay_arrow_string;
12733 }
12734
12735 /* Return 1 if there are any overlay-arrows in current_buffer. */
12736 static int
12737 overlay_arrow_in_current_buffer_p (void)
12738 {
12739 Lisp_Object vlist;
12740
12741 for (vlist = Voverlay_arrow_variable_list;
12742 CONSP (vlist);
12743 vlist = XCDR (vlist))
12744 {
12745 Lisp_Object var = XCAR (vlist);
12746 Lisp_Object val;
12747
12748 if (!SYMBOLP (var))
12749 continue;
12750 val = find_symbol_value (var);
12751 if (MARKERP (val)
12752 && current_buffer == XMARKER (val)->buffer)
12753 return 1;
12754 }
12755 return 0;
12756 }
12757
12758
12759 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12760 has changed. */
12761
12762 static int
12763 overlay_arrows_changed_p (void)
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 Lisp_Object val, pstr;
12773
12774 if (!SYMBOLP (var))
12775 continue;
12776 val = find_symbol_value (var);
12777 if (!MARKERP (val))
12778 continue;
12779 if (! EQ (COERCE_MARKER (val),
12780 Fget (var, Qlast_arrow_position))
12781 || ! (pstr = overlay_arrow_string_or_property (var),
12782 EQ (pstr, Fget (var, Qlast_arrow_string))))
12783 return 1;
12784 }
12785 return 0;
12786 }
12787
12788 /* Mark overlay arrows to be updated on next redisplay. */
12789
12790 static void
12791 update_overlay_arrows (int up_to_date)
12792 {
12793 Lisp_Object vlist;
12794
12795 for (vlist = Voverlay_arrow_variable_list;
12796 CONSP (vlist);
12797 vlist = XCDR (vlist))
12798 {
12799 Lisp_Object var = XCAR (vlist);
12800
12801 if (!SYMBOLP (var))
12802 continue;
12803
12804 if (up_to_date > 0)
12805 {
12806 Lisp_Object val = find_symbol_value (var);
12807 Fput (var, Qlast_arrow_position,
12808 COERCE_MARKER (val));
12809 Fput (var, Qlast_arrow_string,
12810 overlay_arrow_string_or_property (var));
12811 }
12812 else if (up_to_date < 0
12813 || !NILP (Fget (var, Qlast_arrow_position)))
12814 {
12815 Fput (var, Qlast_arrow_position, Qt);
12816 Fput (var, Qlast_arrow_string, Qt);
12817 }
12818 }
12819 }
12820
12821
12822 /* Return overlay arrow string to display at row.
12823 Return integer (bitmap number) for arrow bitmap in left fringe.
12824 Return nil if no overlay arrow. */
12825
12826 static Lisp_Object
12827 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12828 {
12829 Lisp_Object vlist;
12830
12831 for (vlist = Voverlay_arrow_variable_list;
12832 CONSP (vlist);
12833 vlist = XCDR (vlist))
12834 {
12835 Lisp_Object var = XCAR (vlist);
12836 Lisp_Object val;
12837
12838 if (!SYMBOLP (var))
12839 continue;
12840
12841 val = find_symbol_value (var);
12842
12843 if (MARKERP (val)
12844 && current_buffer == XMARKER (val)->buffer
12845 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12846 {
12847 if (FRAME_WINDOW_P (it->f)
12848 /* FIXME: if ROW->reversed_p is set, this should test
12849 the right fringe, not the left one. */
12850 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12851 {
12852 #ifdef HAVE_WINDOW_SYSTEM
12853 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12854 {
12855 int fringe_bitmap;
12856 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12857 return make_number (fringe_bitmap);
12858 }
12859 #endif
12860 return make_number (-1); /* Use default arrow bitmap. */
12861 }
12862 return overlay_arrow_string_or_property (var);
12863 }
12864 }
12865
12866 return Qnil;
12867 }
12868
12869 /* Return 1 if point moved out of or into a composition. Otherwise
12870 return 0. PREV_BUF and PREV_PT are the last point buffer and
12871 position. BUF and PT are the current point buffer and position. */
12872
12873 static int
12874 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12875 struct buffer *buf, ptrdiff_t pt)
12876 {
12877 ptrdiff_t start, end;
12878 Lisp_Object prop;
12879 Lisp_Object buffer;
12880
12881 XSETBUFFER (buffer, buf);
12882 /* Check a composition at the last point if point moved within the
12883 same buffer. */
12884 if (prev_buf == buf)
12885 {
12886 if (prev_pt == pt)
12887 /* Point didn't move. */
12888 return 0;
12889
12890 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12891 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12892 && COMPOSITION_VALID_P (start, end, prop)
12893 && start < prev_pt && end > prev_pt)
12894 /* The last point was within the composition. Return 1 iff
12895 point moved out of the composition. */
12896 return (pt <= start || pt >= end);
12897 }
12898
12899 /* Check a composition at the current point. */
12900 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12901 && find_composition (pt, -1, &start, &end, &prop, buffer)
12902 && COMPOSITION_VALID_P (start, end, prop)
12903 && start < pt && end > pt);
12904 }
12905
12906
12907 /* Reconsider the setting of B->clip_changed which is displayed
12908 in window W. */
12909
12910 static void
12911 reconsider_clip_changes (struct window *w, struct buffer *b)
12912 {
12913 if (b->clip_changed
12914 && !NILP (w->window_end_valid)
12915 && w->current_matrix->buffer == b
12916 && w->current_matrix->zv == BUF_ZV (b)
12917 && w->current_matrix->begv == BUF_BEGV (b))
12918 b->clip_changed = 0;
12919
12920 /* If display wasn't paused, and W is not a tool bar window, see if
12921 point has been moved into or out of a composition. In that case,
12922 we set b->clip_changed to 1 to force updating the screen. If
12923 b->clip_changed has already been set to 1, we can skip this
12924 check. */
12925 if (!b->clip_changed
12926 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12927 {
12928 ptrdiff_t pt;
12929
12930 if (w == XWINDOW (selected_window))
12931 pt = PT;
12932 else
12933 pt = marker_position (w->pointm);
12934
12935 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12936 || pt != w->last_point)
12937 && check_point_in_composition (w->current_matrix->buffer,
12938 w->last_point,
12939 XBUFFER (w->buffer), pt))
12940 b->clip_changed = 1;
12941 }
12942 }
12943 \f
12944
12945 /* Select FRAME to forward the values of frame-local variables into C
12946 variables so that the redisplay routines can access those values
12947 directly. */
12948
12949 static void
12950 select_frame_for_redisplay (Lisp_Object frame)
12951 {
12952 Lisp_Object tail, tem;
12953 Lisp_Object old = selected_frame;
12954 struct Lisp_Symbol *sym;
12955
12956 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12957
12958 selected_frame = frame;
12959
12960 do {
12961 for (tail = XFRAME (frame)->param_alist;
12962 CONSP (tail); tail = XCDR (tail))
12963 if (CONSP (XCAR (tail))
12964 && (tem = XCAR (XCAR (tail)),
12965 SYMBOLP (tem))
12966 && (sym = indirect_variable (XSYMBOL (tem)),
12967 sym->redirect == SYMBOL_LOCALIZED)
12968 && sym->val.blv->frame_local)
12969 /* Use find_symbol_value rather than Fsymbol_value
12970 to avoid an error if it is void. */
12971 find_symbol_value (tem);
12972 } while (!EQ (frame, old) && (frame = old, 1));
12973 }
12974
12975 /* Make sure that previously selected OLD_FRAME is selected unless it has been
12976 deleted (by an X connection failure during redisplay, for example). */
12977
12978 static void
12979 ensure_selected_frame (Lisp_Object old_frame)
12980 {
12981 if (!EQ (old_frame, selected_frame) && FRAME_LIVE_P (XFRAME (old_frame)))
12982 select_frame_for_redisplay (old_frame);
12983 }
12984
12985 #define STOP_POLLING \
12986 do { if (! polling_stopped_here) stop_polling (); \
12987 polling_stopped_here = 1; } while (0)
12988
12989 #define RESUME_POLLING \
12990 do { if (polling_stopped_here) start_polling (); \
12991 polling_stopped_here = 0; } while (0)
12992
12993
12994 /* Perhaps in the future avoid recentering windows if it
12995 is not necessary; currently that causes some problems. */
12996
12997 static void
12998 redisplay_internal (void)
12999 {
13000 struct window *w = XWINDOW (selected_window);
13001 struct window *sw;
13002 struct frame *fr;
13003 int pending;
13004 int must_finish = 0;
13005 struct text_pos tlbufpos, tlendpos;
13006 int number_of_visible_frames;
13007 ptrdiff_t count, count1;
13008 struct frame *sf;
13009 int polling_stopped_here = 0;
13010 Lisp_Object tail, frame, old_frame = selected_frame;
13011 struct backtrace backtrace;
13012
13013 /* Non-zero means redisplay has to consider all windows on all
13014 frames. Zero means, only selected_window is considered. */
13015 int consider_all_windows_p;
13016
13017 /* Non-zero means redisplay has to redisplay the miniwindow. */
13018 int update_miniwindow_p = 0;
13019
13020 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13021
13022 /* No redisplay if running in batch mode or frame is not yet fully
13023 initialized, or redisplay is explicitly turned off by setting
13024 Vinhibit_redisplay. */
13025 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13026 || !NILP (Vinhibit_redisplay))
13027 return;
13028
13029 /* Don't examine these until after testing Vinhibit_redisplay.
13030 When Emacs is shutting down, perhaps because its connection to
13031 X has dropped, we should not look at them at all. */
13032 fr = XFRAME (w->frame);
13033 sf = SELECTED_FRAME ();
13034
13035 if (!fr->glyphs_initialized_p)
13036 return;
13037
13038 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13039 if (popup_activated ())
13040 return;
13041 #endif
13042
13043 /* I don't think this happens but let's be paranoid. */
13044 if (redisplaying_p)
13045 return;
13046
13047 /* Record a function that clears redisplaying_p
13048 when we leave this function. */
13049 count = SPECPDL_INDEX ();
13050 record_unwind_protect (unwind_redisplay, selected_frame);
13051 redisplaying_p = 1;
13052 specbind (Qinhibit_free_realized_faces, Qnil);
13053
13054 /* Record this function, so it appears on the profiler's backtraces. */
13055 backtrace.next = backtrace_list;
13056 backtrace.function = Qredisplay_internal;
13057 backtrace.args = &Qnil;
13058 backtrace.nargs = 0;
13059 backtrace.debug_on_exit = 0;
13060 backtrace_list = &backtrace;
13061
13062 FOR_EACH_FRAME (tail, frame)
13063 XFRAME (frame)->already_hscrolled_p = 0;
13064
13065 retry:
13066 /* Remember the currently selected window. */
13067 sw = w;
13068
13069 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13070 selected_frame and selected_window to be temporarily out-of-sync so
13071 when we come back here via `goto retry', we need to resync because we
13072 may need to run Elisp code (via prepare_menu_bars). */
13073 ensure_selected_frame (old_frame);
13074
13075 pending = 0;
13076 reconsider_clip_changes (w, current_buffer);
13077 last_escape_glyph_frame = NULL;
13078 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13079 last_glyphless_glyph_frame = NULL;
13080 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13081
13082 /* If new fonts have been loaded that make a glyph matrix adjustment
13083 necessary, do it. */
13084 if (fonts_changed_p)
13085 {
13086 adjust_glyphs (NULL);
13087 ++windows_or_buffers_changed;
13088 fonts_changed_p = 0;
13089 }
13090
13091 /* If face_change_count is non-zero, init_iterator will free all
13092 realized faces, which includes the faces referenced from current
13093 matrices. So, we can't reuse current matrices in this case. */
13094 if (face_change_count)
13095 ++windows_or_buffers_changed;
13096
13097 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13098 && FRAME_TTY (sf)->previous_frame != sf)
13099 {
13100 /* Since frames on a single ASCII terminal share the same
13101 display area, displaying a different frame means redisplay
13102 the whole thing. */
13103 windows_or_buffers_changed++;
13104 SET_FRAME_GARBAGED (sf);
13105 #ifndef DOS_NT
13106 set_tty_color_mode (FRAME_TTY (sf), sf);
13107 #endif
13108 FRAME_TTY (sf)->previous_frame = sf;
13109 }
13110
13111 /* Set the visible flags for all frames. Do this before checking for
13112 resized or garbaged frames; they want to know if their frames are
13113 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13114 number_of_visible_frames = 0;
13115
13116 FOR_EACH_FRAME (tail, frame)
13117 {
13118 struct frame *f = XFRAME (frame);
13119
13120 FRAME_SAMPLE_VISIBILITY (f);
13121 if (FRAME_VISIBLE_P (f))
13122 ++number_of_visible_frames;
13123 clear_desired_matrices (f);
13124 }
13125
13126 /* Notice any pending interrupt request to change frame size. */
13127 do_pending_window_change (1);
13128
13129 /* do_pending_window_change could change the selected_window due to
13130 frame resizing which makes the selected window too small. */
13131 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13132 {
13133 sw = w;
13134 reconsider_clip_changes (w, current_buffer);
13135 }
13136
13137 /* Clear frames marked as garbaged. */
13138 clear_garbaged_frames ();
13139
13140 /* Build menubar and tool-bar items. */
13141 if (NILP (Vmemory_full))
13142 prepare_menu_bars ();
13143
13144 if (windows_or_buffers_changed)
13145 update_mode_lines++;
13146
13147 /* Detect case that we need to write or remove a star in the mode line. */
13148 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13149 {
13150 w->update_mode_line = 1;
13151 if (buffer_shared_and_changed ())
13152 update_mode_lines++;
13153 }
13154
13155 /* Avoid invocation of point motion hooks by `current_column' below. */
13156 count1 = SPECPDL_INDEX ();
13157 specbind (Qinhibit_point_motion_hooks, Qt);
13158
13159 if (mode_line_update_needed (w))
13160 w->update_mode_line = 1;
13161
13162 unbind_to (count1, Qnil);
13163
13164 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13165
13166 consider_all_windows_p = (update_mode_lines
13167 || buffer_shared_and_changed ()
13168 || cursor_type_changed);
13169
13170 /* If specs for an arrow have changed, do thorough redisplay
13171 to ensure we remove any arrow that should no longer exist. */
13172 if (overlay_arrows_changed_p ())
13173 consider_all_windows_p = windows_or_buffers_changed = 1;
13174
13175 /* Normally the message* functions will have already displayed and
13176 updated the echo area, but the frame may have been trashed, or
13177 the update may have been preempted, so display the echo area
13178 again here. Checking message_cleared_p captures the case that
13179 the echo area should be cleared. */
13180 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13181 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13182 || (message_cleared_p
13183 && minibuf_level == 0
13184 /* If the mini-window is currently selected, this means the
13185 echo-area doesn't show through. */
13186 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13187 {
13188 int window_height_changed_p = echo_area_display (0);
13189
13190 if (message_cleared_p)
13191 update_miniwindow_p = 1;
13192
13193 must_finish = 1;
13194
13195 /* If we don't display the current message, don't clear the
13196 message_cleared_p flag, because, if we did, we wouldn't clear
13197 the echo area in the next redisplay which doesn't preserve
13198 the echo area. */
13199 if (!display_last_displayed_message_p)
13200 message_cleared_p = 0;
13201
13202 if (fonts_changed_p)
13203 goto retry;
13204 else if (window_height_changed_p)
13205 {
13206 consider_all_windows_p = 1;
13207 ++update_mode_lines;
13208 ++windows_or_buffers_changed;
13209
13210 /* If window configuration was changed, frames may have been
13211 marked garbaged. Clear them or we will experience
13212 surprises wrt scrolling. */
13213 clear_garbaged_frames ();
13214 }
13215 }
13216 else if (EQ (selected_window, minibuf_window)
13217 && (current_buffer->clip_changed || window_outdated (w))
13218 && resize_mini_window (w, 0))
13219 {
13220 /* Resized active mini-window to fit the size of what it is
13221 showing if its contents might have changed. */
13222 must_finish = 1;
13223 /* FIXME: this causes all frames to be updated, which seems unnecessary
13224 since only the current frame needs to be considered. This function
13225 needs to be rewritten with two variables, consider_all_windows and
13226 consider_all_frames. */
13227 consider_all_windows_p = 1;
13228 ++windows_or_buffers_changed;
13229 ++update_mode_lines;
13230
13231 /* If window configuration was changed, frames may have been
13232 marked garbaged. Clear them or we will experience
13233 surprises wrt scrolling. */
13234 clear_garbaged_frames ();
13235 }
13236
13237
13238 /* If showing the region, and mark has changed, we must redisplay
13239 the whole window. The assignment to this_line_start_pos prevents
13240 the optimization directly below this if-statement. */
13241 if (((!NILP (Vtransient_mark_mode)
13242 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13243 != !NILP (w->region_showing))
13244 || (!NILP (w->region_showing)
13245 && !EQ (w->region_showing,
13246 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13247 CHARPOS (this_line_start_pos) = 0;
13248
13249 /* Optimize the case that only the line containing the cursor in the
13250 selected window has changed. Variables starting with this_ are
13251 set in display_line and record information about the line
13252 containing the cursor. */
13253 tlbufpos = this_line_start_pos;
13254 tlendpos = this_line_end_pos;
13255 if (!consider_all_windows_p
13256 && CHARPOS (tlbufpos) > 0
13257 && !w->update_mode_line
13258 && !current_buffer->clip_changed
13259 && !current_buffer->prevent_redisplay_optimizations_p
13260 && FRAME_VISIBLE_P (XFRAME (w->frame))
13261 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13262 /* Make sure recorded data applies to current buffer, etc. */
13263 && this_line_buffer == current_buffer
13264 && current_buffer == XBUFFER (w->buffer)
13265 && !w->force_start
13266 && !w->optional_new_start
13267 /* Point must be on the line that we have info recorded about. */
13268 && PT >= CHARPOS (tlbufpos)
13269 && PT <= Z - CHARPOS (tlendpos)
13270 /* All text outside that line, including its final newline,
13271 must be unchanged. */
13272 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13273 CHARPOS (tlendpos)))
13274 {
13275 if (CHARPOS (tlbufpos) > BEGV
13276 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13277 && (CHARPOS (tlbufpos) == ZV
13278 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13279 /* Former continuation line has disappeared by becoming empty. */
13280 goto cancel;
13281 else if (window_outdated (w) || MINI_WINDOW_P (w))
13282 {
13283 /* We have to handle the case of continuation around a
13284 wide-column character (see the comment in indent.c around
13285 line 1340).
13286
13287 For instance, in the following case:
13288
13289 -------- Insert --------
13290 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13291 J_I_ ==> J_I_ `^^' are cursors.
13292 ^^ ^^
13293 -------- --------
13294
13295 As we have to redraw the line above, we cannot use this
13296 optimization. */
13297
13298 struct it it;
13299 int line_height_before = this_line_pixel_height;
13300
13301 /* Note that start_display will handle the case that the
13302 line starting at tlbufpos is a continuation line. */
13303 start_display (&it, w, tlbufpos);
13304
13305 /* Implementation note: It this still necessary? */
13306 if (it.current_x != this_line_start_x)
13307 goto cancel;
13308
13309 TRACE ((stderr, "trying display optimization 1\n"));
13310 w->cursor.vpos = -1;
13311 overlay_arrow_seen = 0;
13312 it.vpos = this_line_vpos;
13313 it.current_y = this_line_y;
13314 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13315 display_line (&it);
13316
13317 /* If line contains point, is not continued,
13318 and ends at same distance from eob as before, we win. */
13319 if (w->cursor.vpos >= 0
13320 /* Line is not continued, otherwise this_line_start_pos
13321 would have been set to 0 in display_line. */
13322 && CHARPOS (this_line_start_pos)
13323 /* Line ends as before. */
13324 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13325 /* Line has same height as before. Otherwise other lines
13326 would have to be shifted up or down. */
13327 && this_line_pixel_height == line_height_before)
13328 {
13329 /* If this is not the window's last line, we must adjust
13330 the charstarts of the lines below. */
13331 if (it.current_y < it.last_visible_y)
13332 {
13333 struct glyph_row *row
13334 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13335 ptrdiff_t delta, delta_bytes;
13336
13337 /* We used to distinguish between two cases here,
13338 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13339 when the line ends in a newline or the end of the
13340 buffer's accessible portion. But both cases did
13341 the same, so they were collapsed. */
13342 delta = (Z
13343 - CHARPOS (tlendpos)
13344 - MATRIX_ROW_START_CHARPOS (row));
13345 delta_bytes = (Z_BYTE
13346 - BYTEPOS (tlendpos)
13347 - MATRIX_ROW_START_BYTEPOS (row));
13348
13349 increment_matrix_positions (w->current_matrix,
13350 this_line_vpos + 1,
13351 w->current_matrix->nrows,
13352 delta, delta_bytes);
13353 }
13354
13355 /* If this row displays text now but previously didn't,
13356 or vice versa, w->window_end_vpos may have to be
13357 adjusted. */
13358 if ((it.glyph_row - 1)->displays_text_p)
13359 {
13360 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13361 wset_window_end_vpos (w, make_number (this_line_vpos));
13362 }
13363 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13364 && this_line_vpos > 0)
13365 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13366 wset_window_end_valid (w, Qnil);
13367
13368 /* Update hint: No need to try to scroll in update_window. */
13369 w->desired_matrix->no_scrolling_p = 1;
13370
13371 #ifdef GLYPH_DEBUG
13372 *w->desired_matrix->method = 0;
13373 debug_method_add (w, "optimization 1");
13374 #endif
13375 #ifdef HAVE_WINDOW_SYSTEM
13376 update_window_fringes (w, 0);
13377 #endif
13378 goto update;
13379 }
13380 else
13381 goto cancel;
13382 }
13383 else if (/* Cursor position hasn't changed. */
13384 PT == w->last_point
13385 /* Make sure the cursor was last displayed
13386 in this window. Otherwise we have to reposition it. */
13387 && 0 <= w->cursor.vpos
13388 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13389 {
13390 if (!must_finish)
13391 {
13392 do_pending_window_change (1);
13393 /* If selected_window changed, redisplay again. */
13394 if (WINDOWP (selected_window)
13395 && (w = XWINDOW (selected_window)) != sw)
13396 goto retry;
13397
13398 /* We used to always goto end_of_redisplay here, but this
13399 isn't enough if we have a blinking cursor. */
13400 if (w->cursor_off_p == w->last_cursor_off_p)
13401 goto end_of_redisplay;
13402 }
13403 goto update;
13404 }
13405 /* If highlighting the region, or if the cursor is in the echo area,
13406 then we can't just move the cursor. */
13407 else if (! (!NILP (Vtransient_mark_mode)
13408 && !NILP (BVAR (current_buffer, mark_active)))
13409 && (EQ (selected_window,
13410 BVAR (current_buffer, last_selected_window))
13411 || highlight_nonselected_windows)
13412 && NILP (w->region_showing)
13413 && NILP (Vshow_trailing_whitespace)
13414 && !cursor_in_echo_area)
13415 {
13416 struct it it;
13417 struct glyph_row *row;
13418
13419 /* Skip from tlbufpos to PT and see where it is. Note that
13420 PT may be in invisible text. If so, we will end at the
13421 next visible position. */
13422 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13423 NULL, DEFAULT_FACE_ID);
13424 it.current_x = this_line_start_x;
13425 it.current_y = this_line_y;
13426 it.vpos = this_line_vpos;
13427
13428 /* The call to move_it_to stops in front of PT, but
13429 moves over before-strings. */
13430 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13431
13432 if (it.vpos == this_line_vpos
13433 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13434 row->enabled_p))
13435 {
13436 eassert (this_line_vpos == it.vpos);
13437 eassert (this_line_y == it.current_y);
13438 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13439 #ifdef GLYPH_DEBUG
13440 *w->desired_matrix->method = 0;
13441 debug_method_add (w, "optimization 3");
13442 #endif
13443 goto update;
13444 }
13445 else
13446 goto cancel;
13447 }
13448
13449 cancel:
13450 /* Text changed drastically or point moved off of line. */
13451 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13452 }
13453
13454 CHARPOS (this_line_start_pos) = 0;
13455 consider_all_windows_p |= buffer_shared_and_changed ();
13456 ++clear_face_cache_count;
13457 #ifdef HAVE_WINDOW_SYSTEM
13458 ++clear_image_cache_count;
13459 #endif
13460
13461 /* Build desired matrices, and update the display. If
13462 consider_all_windows_p is non-zero, do it for all windows on all
13463 frames. Otherwise do it for selected_window, only. */
13464
13465 if (consider_all_windows_p)
13466 {
13467 FOR_EACH_FRAME (tail, frame)
13468 XFRAME (frame)->updated_p = 0;
13469
13470 FOR_EACH_FRAME (tail, frame)
13471 {
13472 struct frame *f = XFRAME (frame);
13473
13474 /* We don't have to do anything for unselected terminal
13475 frames. */
13476 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13477 && !EQ (FRAME_TTY (f)->top_frame, frame))
13478 continue;
13479
13480 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13481 {
13482 if (! EQ (frame, selected_frame))
13483 /* Select the frame, for the sake of frame-local
13484 variables. */
13485 select_frame_for_redisplay (frame);
13486
13487 /* Mark all the scroll bars to be removed; we'll redeem
13488 the ones we want when we redisplay their windows. */
13489 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13490 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13491
13492 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13493 redisplay_windows (FRAME_ROOT_WINDOW (f));
13494
13495 /* The X error handler may have deleted that frame. */
13496 if (!FRAME_LIVE_P (f))
13497 continue;
13498
13499 /* Any scroll bars which redisplay_windows should have
13500 nuked should now go away. */
13501 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13502 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13503
13504 /* If fonts changed, display again. */
13505 /* ??? rms: I suspect it is a mistake to jump all the way
13506 back to retry here. It should just retry this frame. */
13507 if (fonts_changed_p)
13508 goto retry;
13509
13510 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13511 {
13512 /* See if we have to hscroll. */
13513 if (!f->already_hscrolled_p)
13514 {
13515 f->already_hscrolled_p = 1;
13516 if (hscroll_windows (f->root_window))
13517 goto retry;
13518 }
13519
13520 /* Prevent various kinds of signals during display
13521 update. stdio is not robust about handling
13522 signals, which can cause an apparent I/O
13523 error. */
13524 if (interrupt_input)
13525 unrequest_sigio ();
13526 STOP_POLLING;
13527
13528 /* Update the display. */
13529 set_window_update_flags (XWINDOW (f->root_window), 1);
13530 pending |= update_frame (f, 0, 0);
13531 f->updated_p = 1;
13532 }
13533 }
13534 }
13535
13536 /* We played a bit fast-and-loose above and allowed selected_frame
13537 and selected_window to be temporarily out-of-sync but let's make
13538 sure this stays contained. */
13539 ensure_selected_frame (old_frame);
13540 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13541
13542 if (!pending)
13543 {
13544 /* Do the mark_window_display_accurate after all windows have
13545 been redisplayed because this call resets flags in buffers
13546 which are needed for proper redisplay. */
13547 FOR_EACH_FRAME (tail, frame)
13548 {
13549 struct frame *f = XFRAME (frame);
13550 if (f->updated_p)
13551 {
13552 mark_window_display_accurate (f->root_window, 1);
13553 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13554 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13555 }
13556 }
13557 }
13558 }
13559 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13560 {
13561 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13562 struct frame *mini_frame;
13563
13564 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13565 /* Use list_of_error, not Qerror, so that
13566 we catch only errors and don't run the debugger. */
13567 internal_condition_case_1 (redisplay_window_1, selected_window,
13568 list_of_error,
13569 redisplay_window_error);
13570 if (update_miniwindow_p)
13571 internal_condition_case_1 (redisplay_window_1, mini_window,
13572 list_of_error,
13573 redisplay_window_error);
13574
13575 /* Compare desired and current matrices, perform output. */
13576
13577 update:
13578 /* If fonts changed, display again. */
13579 if (fonts_changed_p)
13580 goto retry;
13581
13582 /* Prevent various kinds of signals during display update.
13583 stdio is not robust about handling signals,
13584 which can cause an apparent I/O error. */
13585 if (interrupt_input)
13586 unrequest_sigio ();
13587 STOP_POLLING;
13588
13589 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13590 {
13591 if (hscroll_windows (selected_window))
13592 goto retry;
13593
13594 XWINDOW (selected_window)->must_be_updated_p = 1;
13595 pending = update_frame (sf, 0, 0);
13596 }
13597
13598 /* We may have called echo_area_display at the top of this
13599 function. If the echo area is on another frame, that may
13600 have put text on a frame other than the selected one, so the
13601 above call to update_frame would not have caught it. Catch
13602 it here. */
13603 mini_window = FRAME_MINIBUF_WINDOW (sf);
13604 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13605
13606 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13607 {
13608 XWINDOW (mini_window)->must_be_updated_p = 1;
13609 pending |= update_frame (mini_frame, 0, 0);
13610 if (!pending && hscroll_windows (mini_window))
13611 goto retry;
13612 }
13613 }
13614
13615 /* If display was paused because of pending input, make sure we do a
13616 thorough update the next time. */
13617 if (pending)
13618 {
13619 /* Prevent the optimization at the beginning of
13620 redisplay_internal that tries a single-line update of the
13621 line containing the cursor in the selected window. */
13622 CHARPOS (this_line_start_pos) = 0;
13623
13624 /* Let the overlay arrow be updated the next time. */
13625 update_overlay_arrows (0);
13626
13627 /* If we pause after scrolling, some rows in the current
13628 matrices of some windows are not valid. */
13629 if (!WINDOW_FULL_WIDTH_P (w)
13630 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13631 update_mode_lines = 1;
13632 }
13633 else
13634 {
13635 if (!consider_all_windows_p)
13636 {
13637 /* This has already been done above if
13638 consider_all_windows_p is set. */
13639 mark_window_display_accurate_1 (w, 1);
13640
13641 /* Say overlay arrows are up to date. */
13642 update_overlay_arrows (1);
13643
13644 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13645 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13646 }
13647
13648 update_mode_lines = 0;
13649 windows_or_buffers_changed = 0;
13650 cursor_type_changed = 0;
13651 }
13652
13653 /* Start SIGIO interrupts coming again. Having them off during the
13654 code above makes it less likely one will discard output, but not
13655 impossible, since there might be stuff in the system buffer here.
13656 But it is much hairier to try to do anything about that. */
13657 if (interrupt_input)
13658 request_sigio ();
13659 RESUME_POLLING;
13660
13661 /* If a frame has become visible which was not before, redisplay
13662 again, so that we display it. Expose events for such a frame
13663 (which it gets when becoming visible) don't call the parts of
13664 redisplay constructing glyphs, so simply exposing a frame won't
13665 display anything in this case. So, we have to display these
13666 frames here explicitly. */
13667 if (!pending)
13668 {
13669 int new_count = 0;
13670
13671 FOR_EACH_FRAME (tail, frame)
13672 {
13673 int this_is_visible = 0;
13674
13675 if (XFRAME (frame)->visible)
13676 this_is_visible = 1;
13677 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13678 if (XFRAME (frame)->visible)
13679 this_is_visible = 1;
13680
13681 if (this_is_visible)
13682 new_count++;
13683 }
13684
13685 if (new_count != number_of_visible_frames)
13686 windows_or_buffers_changed++;
13687 }
13688
13689 /* Change frame size now if a change is pending. */
13690 do_pending_window_change (1);
13691
13692 /* If we just did a pending size change, or have additional
13693 visible frames, or selected_window changed, redisplay again. */
13694 if ((windows_or_buffers_changed && !pending)
13695 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13696 goto retry;
13697
13698 /* Clear the face and image caches.
13699
13700 We used to do this only if consider_all_windows_p. But the cache
13701 needs to be cleared if a timer creates images in the current
13702 buffer (e.g. the test case in Bug#6230). */
13703
13704 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13705 {
13706 clear_face_cache (0);
13707 clear_face_cache_count = 0;
13708 }
13709
13710 #ifdef HAVE_WINDOW_SYSTEM
13711 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13712 {
13713 clear_image_caches (Qnil);
13714 clear_image_cache_count = 0;
13715 }
13716 #endif /* HAVE_WINDOW_SYSTEM */
13717
13718 end_of_redisplay:
13719 backtrace_list = backtrace.next;
13720 unbind_to (count, Qnil);
13721 RESUME_POLLING;
13722 }
13723
13724
13725 /* Redisplay, but leave alone any recent echo area message unless
13726 another message has been requested in its place.
13727
13728 This is useful in situations where you need to redisplay but no
13729 user action has occurred, making it inappropriate for the message
13730 area to be cleared. See tracking_off and
13731 wait_reading_process_output for examples of these situations.
13732
13733 FROM_WHERE is an integer saying from where this function was
13734 called. This is useful for debugging. */
13735
13736 void
13737 redisplay_preserve_echo_area (int from_where)
13738 {
13739 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13740
13741 if (!NILP (echo_area_buffer[1]))
13742 {
13743 /* We have a previously displayed message, but no current
13744 message. Redisplay the previous message. */
13745 display_last_displayed_message_p = 1;
13746 redisplay_internal ();
13747 display_last_displayed_message_p = 0;
13748 }
13749 else
13750 redisplay_internal ();
13751
13752 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13753 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13754 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13755 }
13756
13757
13758 /* Function registered with record_unwind_protect in redisplay_internal.
13759 Clear redisplaying_p. Also select the previously selected frame. */
13760
13761 static Lisp_Object
13762 unwind_redisplay (Lisp_Object old_frame)
13763 {
13764 redisplaying_p = 0;
13765 ensure_selected_frame (old_frame);
13766 return Qnil;
13767 }
13768
13769
13770 /* Mark the display of window W as accurate or inaccurate. If
13771 ACCURATE_P is non-zero mark display of W as accurate. If
13772 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13773 redisplay_internal is called. */
13774
13775 static void
13776 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13777 {
13778 if (BUFFERP (w->buffer))
13779 {
13780 struct buffer *b = XBUFFER (w->buffer);
13781
13782 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13783 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13784 w->last_had_star
13785 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13786
13787 if (accurate_p)
13788 {
13789 b->clip_changed = 0;
13790 b->prevent_redisplay_optimizations_p = 0;
13791
13792 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13793 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13794 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13795 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13796
13797 w->current_matrix->buffer = b;
13798 w->current_matrix->begv = BUF_BEGV (b);
13799 w->current_matrix->zv = BUF_ZV (b);
13800
13801 w->last_cursor = w->cursor;
13802 w->last_cursor_off_p = w->cursor_off_p;
13803
13804 if (w == XWINDOW (selected_window))
13805 w->last_point = BUF_PT (b);
13806 else
13807 w->last_point = marker_position (w->pointm);
13808 }
13809 }
13810
13811 if (accurate_p)
13812 {
13813 wset_window_end_valid (w, w->buffer);
13814 w->update_mode_line = 0;
13815 }
13816 }
13817
13818
13819 /* Mark the display of windows in the window tree rooted at WINDOW as
13820 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13821 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13822 be redisplayed the next time redisplay_internal is called. */
13823
13824 void
13825 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13826 {
13827 struct window *w;
13828
13829 for (; !NILP (window); window = w->next)
13830 {
13831 w = XWINDOW (window);
13832 mark_window_display_accurate_1 (w, accurate_p);
13833
13834 if (!NILP (w->vchild))
13835 mark_window_display_accurate (w->vchild, accurate_p);
13836 if (!NILP (w->hchild))
13837 mark_window_display_accurate (w->hchild, accurate_p);
13838 }
13839
13840 if (accurate_p)
13841 {
13842 update_overlay_arrows (1);
13843 }
13844 else
13845 {
13846 /* Force a thorough redisplay the next time by setting
13847 last_arrow_position and last_arrow_string to t, which is
13848 unequal to any useful value of Voverlay_arrow_... */
13849 update_overlay_arrows (-1);
13850 }
13851 }
13852
13853
13854 /* Return value in display table DP (Lisp_Char_Table *) for character
13855 C. Since a display table doesn't have any parent, we don't have to
13856 follow parent. Do not call this function directly but use the
13857 macro DISP_CHAR_VECTOR. */
13858
13859 Lisp_Object
13860 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13861 {
13862 Lisp_Object val;
13863
13864 if (ASCII_CHAR_P (c))
13865 {
13866 val = dp->ascii;
13867 if (SUB_CHAR_TABLE_P (val))
13868 val = XSUB_CHAR_TABLE (val)->contents[c];
13869 }
13870 else
13871 {
13872 Lisp_Object table;
13873
13874 XSETCHAR_TABLE (table, dp);
13875 val = char_table_ref (table, c);
13876 }
13877 if (NILP (val))
13878 val = dp->defalt;
13879 return val;
13880 }
13881
13882
13883 \f
13884 /***********************************************************************
13885 Window Redisplay
13886 ***********************************************************************/
13887
13888 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13889
13890 static void
13891 redisplay_windows (Lisp_Object window)
13892 {
13893 while (!NILP (window))
13894 {
13895 struct window *w = XWINDOW (window);
13896
13897 if (!NILP (w->hchild))
13898 redisplay_windows (w->hchild);
13899 else if (!NILP (w->vchild))
13900 redisplay_windows (w->vchild);
13901 else if (!NILP (w->buffer))
13902 {
13903 displayed_buffer = XBUFFER (w->buffer);
13904 /* Use list_of_error, not Qerror, so that
13905 we catch only errors and don't run the debugger. */
13906 internal_condition_case_1 (redisplay_window_0, window,
13907 list_of_error,
13908 redisplay_window_error);
13909 }
13910
13911 window = w->next;
13912 }
13913 }
13914
13915 static Lisp_Object
13916 redisplay_window_error (Lisp_Object ignore)
13917 {
13918 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13919 return Qnil;
13920 }
13921
13922 static Lisp_Object
13923 redisplay_window_0 (Lisp_Object window)
13924 {
13925 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13926 redisplay_window (window, 0);
13927 return Qnil;
13928 }
13929
13930 static Lisp_Object
13931 redisplay_window_1 (Lisp_Object window)
13932 {
13933 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13934 redisplay_window (window, 1);
13935 return Qnil;
13936 }
13937 \f
13938
13939 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13940 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13941 which positions recorded in ROW differ from current buffer
13942 positions.
13943
13944 Return 0 if cursor is not on this row, 1 otherwise. */
13945
13946 static int
13947 set_cursor_from_row (struct window *w, struct glyph_row *row,
13948 struct glyph_matrix *matrix,
13949 ptrdiff_t delta, ptrdiff_t delta_bytes,
13950 int dy, int dvpos)
13951 {
13952 struct glyph *glyph = row->glyphs[TEXT_AREA];
13953 struct glyph *end = glyph + row->used[TEXT_AREA];
13954 struct glyph *cursor = NULL;
13955 /* The last known character position in row. */
13956 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13957 int x = row->x;
13958 ptrdiff_t pt_old = PT - delta;
13959 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13960 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13961 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13962 /* A glyph beyond the edge of TEXT_AREA which we should never
13963 touch. */
13964 struct glyph *glyphs_end = end;
13965 /* Non-zero means we've found a match for cursor position, but that
13966 glyph has the avoid_cursor_p flag set. */
13967 int match_with_avoid_cursor = 0;
13968 /* Non-zero means we've seen at least one glyph that came from a
13969 display string. */
13970 int string_seen = 0;
13971 /* Largest and smallest buffer positions seen so far during scan of
13972 glyph row. */
13973 ptrdiff_t bpos_max = pos_before;
13974 ptrdiff_t bpos_min = pos_after;
13975 /* Last buffer position covered by an overlay string with an integer
13976 `cursor' property. */
13977 ptrdiff_t bpos_covered = 0;
13978 /* Non-zero means the display string on which to display the cursor
13979 comes from a text property, not from an overlay. */
13980 int string_from_text_prop = 0;
13981
13982 /* Don't even try doing anything if called for a mode-line or
13983 header-line row, since the rest of the code isn't prepared to
13984 deal with such calamities. */
13985 eassert (!row->mode_line_p);
13986 if (row->mode_line_p)
13987 return 0;
13988
13989 /* Skip over glyphs not having an object at the start and the end of
13990 the row. These are special glyphs like truncation marks on
13991 terminal frames. */
13992 if (row->displays_text_p)
13993 {
13994 if (!row->reversed_p)
13995 {
13996 while (glyph < end
13997 && INTEGERP (glyph->object)
13998 && glyph->charpos < 0)
13999 {
14000 x += glyph->pixel_width;
14001 ++glyph;
14002 }
14003 while (end > glyph
14004 && INTEGERP ((end - 1)->object)
14005 /* CHARPOS is zero for blanks and stretch glyphs
14006 inserted by extend_face_to_end_of_line. */
14007 && (end - 1)->charpos <= 0)
14008 --end;
14009 glyph_before = glyph - 1;
14010 glyph_after = end;
14011 }
14012 else
14013 {
14014 struct glyph *g;
14015
14016 /* If the glyph row is reversed, we need to process it from back
14017 to front, so swap the edge pointers. */
14018 glyphs_end = end = glyph - 1;
14019 glyph += row->used[TEXT_AREA] - 1;
14020
14021 while (glyph > end + 1
14022 && INTEGERP (glyph->object)
14023 && glyph->charpos < 0)
14024 {
14025 --glyph;
14026 x -= glyph->pixel_width;
14027 }
14028 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14029 --glyph;
14030 /* By default, in reversed rows we put the cursor on the
14031 rightmost (first in the reading order) glyph. */
14032 for (g = end + 1; g < glyph; g++)
14033 x += g->pixel_width;
14034 while (end < glyph
14035 && INTEGERP ((end + 1)->object)
14036 && (end + 1)->charpos <= 0)
14037 ++end;
14038 glyph_before = glyph + 1;
14039 glyph_after = end;
14040 }
14041 }
14042 else if (row->reversed_p)
14043 {
14044 /* In R2L rows that don't display text, put the cursor on the
14045 rightmost glyph. Case in point: an empty last line that is
14046 part of an R2L paragraph. */
14047 cursor = end - 1;
14048 /* Avoid placing the cursor on the last glyph of the row, where
14049 on terminal frames we hold the vertical border between
14050 adjacent windows. */
14051 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14052 && !WINDOW_RIGHTMOST_P (w)
14053 && cursor == row->glyphs[LAST_AREA] - 1)
14054 cursor--;
14055 x = -1; /* will be computed below, at label compute_x */
14056 }
14057
14058 /* Step 1: Try to find the glyph whose character position
14059 corresponds to point. If that's not possible, find 2 glyphs
14060 whose character positions are the closest to point, one before
14061 point, the other after it. */
14062 if (!row->reversed_p)
14063 while (/* not marched to end of glyph row */
14064 glyph < end
14065 /* glyph was not inserted by redisplay for internal purposes */
14066 && !INTEGERP (glyph->object))
14067 {
14068 if (BUFFERP (glyph->object))
14069 {
14070 ptrdiff_t dpos = glyph->charpos - pt_old;
14071
14072 if (glyph->charpos > bpos_max)
14073 bpos_max = glyph->charpos;
14074 if (glyph->charpos < bpos_min)
14075 bpos_min = glyph->charpos;
14076 if (!glyph->avoid_cursor_p)
14077 {
14078 /* If we hit point, we've found the glyph on which to
14079 display the cursor. */
14080 if (dpos == 0)
14081 {
14082 match_with_avoid_cursor = 0;
14083 break;
14084 }
14085 /* See if we've found a better approximation to
14086 POS_BEFORE or to POS_AFTER. */
14087 if (0 > dpos && dpos > pos_before - pt_old)
14088 {
14089 pos_before = glyph->charpos;
14090 glyph_before = glyph;
14091 }
14092 else if (0 < dpos && dpos < pos_after - pt_old)
14093 {
14094 pos_after = glyph->charpos;
14095 glyph_after = glyph;
14096 }
14097 }
14098 else if (dpos == 0)
14099 match_with_avoid_cursor = 1;
14100 }
14101 else if (STRINGP (glyph->object))
14102 {
14103 Lisp_Object chprop;
14104 ptrdiff_t glyph_pos = glyph->charpos;
14105
14106 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14107 glyph->object);
14108 if (!NILP (chprop))
14109 {
14110 /* If the string came from a `display' text property,
14111 look up the buffer position of that property and
14112 use that position to update bpos_max, as if we
14113 actually saw such a position in one of the row's
14114 glyphs. This helps with supporting integer values
14115 of `cursor' property on the display string in
14116 situations where most or all of the row's buffer
14117 text is completely covered by display properties,
14118 so that no glyph with valid buffer positions is
14119 ever seen in the row. */
14120 ptrdiff_t prop_pos =
14121 string_buffer_position_lim (glyph->object, pos_before,
14122 pos_after, 0);
14123
14124 if (prop_pos >= pos_before)
14125 bpos_max = prop_pos - 1;
14126 }
14127 if (INTEGERP (chprop))
14128 {
14129 bpos_covered = bpos_max + XINT (chprop);
14130 /* If the `cursor' property covers buffer positions up
14131 to and including point, we should display cursor on
14132 this glyph. Note that, if a `cursor' property on one
14133 of the string's characters has an integer value, we
14134 will break out of the loop below _before_ we get to
14135 the position match above. IOW, integer values of
14136 the `cursor' property override the "exact match for
14137 point" strategy of positioning the cursor. */
14138 /* Implementation note: bpos_max == pt_old when, e.g.,
14139 we are in an empty line, where bpos_max is set to
14140 MATRIX_ROW_START_CHARPOS, see above. */
14141 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14142 {
14143 cursor = glyph;
14144 break;
14145 }
14146 }
14147
14148 string_seen = 1;
14149 }
14150 x += glyph->pixel_width;
14151 ++glyph;
14152 }
14153 else if (glyph > end) /* row is reversed */
14154 while (!INTEGERP (glyph->object))
14155 {
14156 if (BUFFERP (glyph->object))
14157 {
14158 ptrdiff_t dpos = glyph->charpos - pt_old;
14159
14160 if (glyph->charpos > bpos_max)
14161 bpos_max = glyph->charpos;
14162 if (glyph->charpos < bpos_min)
14163 bpos_min = glyph->charpos;
14164 if (!glyph->avoid_cursor_p)
14165 {
14166 if (dpos == 0)
14167 {
14168 match_with_avoid_cursor = 0;
14169 break;
14170 }
14171 if (0 > dpos && dpos > pos_before - pt_old)
14172 {
14173 pos_before = glyph->charpos;
14174 glyph_before = glyph;
14175 }
14176 else if (0 < dpos && dpos < pos_after - pt_old)
14177 {
14178 pos_after = glyph->charpos;
14179 glyph_after = glyph;
14180 }
14181 }
14182 else if (dpos == 0)
14183 match_with_avoid_cursor = 1;
14184 }
14185 else if (STRINGP (glyph->object))
14186 {
14187 Lisp_Object chprop;
14188 ptrdiff_t glyph_pos = glyph->charpos;
14189
14190 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14191 glyph->object);
14192 if (!NILP (chprop))
14193 {
14194 ptrdiff_t prop_pos =
14195 string_buffer_position_lim (glyph->object, pos_before,
14196 pos_after, 0);
14197
14198 if (prop_pos >= pos_before)
14199 bpos_max = prop_pos - 1;
14200 }
14201 if (INTEGERP (chprop))
14202 {
14203 bpos_covered = bpos_max + XINT (chprop);
14204 /* If the `cursor' property covers buffer positions up
14205 to and including point, we should display cursor on
14206 this glyph. */
14207 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14208 {
14209 cursor = glyph;
14210 break;
14211 }
14212 }
14213 string_seen = 1;
14214 }
14215 --glyph;
14216 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14217 {
14218 x--; /* can't use any pixel_width */
14219 break;
14220 }
14221 x -= glyph->pixel_width;
14222 }
14223
14224 /* Step 2: If we didn't find an exact match for point, we need to
14225 look for a proper place to put the cursor among glyphs between
14226 GLYPH_BEFORE and GLYPH_AFTER. */
14227 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14228 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14229 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14230 {
14231 /* An empty line has a single glyph whose OBJECT is zero and
14232 whose CHARPOS is the position of a newline on that line.
14233 Note that on a TTY, there are more glyphs after that, which
14234 were produced by extend_face_to_end_of_line, but their
14235 CHARPOS is zero or negative. */
14236 int empty_line_p =
14237 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14238 && INTEGERP (glyph->object) && glyph->charpos > 0;
14239
14240 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14241 {
14242 ptrdiff_t ellipsis_pos;
14243
14244 /* Scan back over the ellipsis glyphs. */
14245 if (!row->reversed_p)
14246 {
14247 ellipsis_pos = (glyph - 1)->charpos;
14248 while (glyph > row->glyphs[TEXT_AREA]
14249 && (glyph - 1)->charpos == ellipsis_pos)
14250 glyph--, x -= glyph->pixel_width;
14251 /* That loop always goes one position too far, including
14252 the glyph before the ellipsis. So scan forward over
14253 that one. */
14254 x += glyph->pixel_width;
14255 glyph++;
14256 }
14257 else /* row is reversed */
14258 {
14259 ellipsis_pos = (glyph + 1)->charpos;
14260 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14261 && (glyph + 1)->charpos == ellipsis_pos)
14262 glyph++, x += glyph->pixel_width;
14263 x -= glyph->pixel_width;
14264 glyph--;
14265 }
14266 }
14267 else if (match_with_avoid_cursor)
14268 {
14269 cursor = glyph_after;
14270 x = -1;
14271 }
14272 else if (string_seen)
14273 {
14274 int incr = row->reversed_p ? -1 : +1;
14275
14276 /* Need to find the glyph that came out of a string which is
14277 present at point. That glyph is somewhere between
14278 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14279 positioned between POS_BEFORE and POS_AFTER in the
14280 buffer. */
14281 struct glyph *start, *stop;
14282 ptrdiff_t pos = pos_before;
14283
14284 x = -1;
14285
14286 /* If the row ends in a newline from a display string,
14287 reordering could have moved the glyphs belonging to the
14288 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14289 in this case we extend the search to the last glyph in
14290 the row that was not inserted by redisplay. */
14291 if (row->ends_in_newline_from_string_p)
14292 {
14293 glyph_after = end;
14294 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14295 }
14296
14297 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14298 correspond to POS_BEFORE and POS_AFTER, respectively. We
14299 need START and STOP in the order that corresponds to the
14300 row's direction as given by its reversed_p flag. If the
14301 directionality of characters between POS_BEFORE and
14302 POS_AFTER is the opposite of the row's base direction,
14303 these characters will have been reordered for display,
14304 and we need to reverse START and STOP. */
14305 if (!row->reversed_p)
14306 {
14307 start = min (glyph_before, glyph_after);
14308 stop = max (glyph_before, glyph_after);
14309 }
14310 else
14311 {
14312 start = max (glyph_before, glyph_after);
14313 stop = min (glyph_before, glyph_after);
14314 }
14315 for (glyph = start + incr;
14316 row->reversed_p ? glyph > stop : glyph < stop; )
14317 {
14318
14319 /* Any glyphs that come from the buffer are here because
14320 of bidi reordering. Skip them, and only pay
14321 attention to glyphs that came from some string. */
14322 if (STRINGP (glyph->object))
14323 {
14324 Lisp_Object str;
14325 ptrdiff_t tem;
14326 /* If the display property covers the newline, we
14327 need to search for it one position farther. */
14328 ptrdiff_t lim = pos_after
14329 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14330
14331 string_from_text_prop = 0;
14332 str = glyph->object;
14333 tem = string_buffer_position_lim (str, pos, lim, 0);
14334 if (tem == 0 /* from overlay */
14335 || pos <= tem)
14336 {
14337 /* If the string from which this glyph came is
14338 found in the buffer at point, or at position
14339 that is closer to point than pos_after, then
14340 we've found the glyph we've been looking for.
14341 If it comes from an overlay (tem == 0), and
14342 it has the `cursor' property on one of its
14343 glyphs, record that glyph as a candidate for
14344 displaying the cursor. (As in the
14345 unidirectional version, we will display the
14346 cursor on the last candidate we find.) */
14347 if (tem == 0
14348 || tem == pt_old
14349 || (tem - pt_old > 0 && tem < pos_after))
14350 {
14351 /* The glyphs from this string could have
14352 been reordered. Find the one with the
14353 smallest string position. Or there could
14354 be a character in the string with the
14355 `cursor' property, which means display
14356 cursor on that character's glyph. */
14357 ptrdiff_t strpos = glyph->charpos;
14358
14359 if (tem)
14360 {
14361 cursor = glyph;
14362 string_from_text_prop = 1;
14363 }
14364 for ( ;
14365 (row->reversed_p ? glyph > stop : glyph < stop)
14366 && EQ (glyph->object, str);
14367 glyph += incr)
14368 {
14369 Lisp_Object cprop;
14370 ptrdiff_t gpos = glyph->charpos;
14371
14372 cprop = Fget_char_property (make_number (gpos),
14373 Qcursor,
14374 glyph->object);
14375 if (!NILP (cprop))
14376 {
14377 cursor = glyph;
14378 break;
14379 }
14380 if (tem && glyph->charpos < strpos)
14381 {
14382 strpos = glyph->charpos;
14383 cursor = glyph;
14384 }
14385 }
14386
14387 if (tem == pt_old
14388 || (tem - pt_old > 0 && tem < pos_after))
14389 goto compute_x;
14390 }
14391 if (tem)
14392 pos = tem + 1; /* don't find previous instances */
14393 }
14394 /* This string is not what we want; skip all of the
14395 glyphs that came from it. */
14396 while ((row->reversed_p ? glyph > stop : glyph < stop)
14397 && EQ (glyph->object, str))
14398 glyph += incr;
14399 }
14400 else
14401 glyph += incr;
14402 }
14403
14404 /* If we reached the end of the line, and END was from a string,
14405 the cursor is not on this line. */
14406 if (cursor == NULL
14407 && (row->reversed_p ? glyph <= end : glyph >= end)
14408 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14409 && STRINGP (end->object)
14410 && row->continued_p)
14411 return 0;
14412 }
14413 /* A truncated row may not include PT among its character positions.
14414 Setting the cursor inside the scroll margin will trigger
14415 recalculation of hscroll in hscroll_window_tree. But if a
14416 display string covers point, defer to the string-handling
14417 code below to figure this out. */
14418 else if (row->truncated_on_left_p && pt_old < bpos_min)
14419 {
14420 cursor = glyph_before;
14421 x = -1;
14422 }
14423 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14424 /* Zero-width characters produce no glyphs. */
14425 || (!empty_line_p
14426 && (row->reversed_p
14427 ? glyph_after > glyphs_end
14428 : glyph_after < glyphs_end)))
14429 {
14430 cursor = glyph_after;
14431 x = -1;
14432 }
14433 }
14434
14435 compute_x:
14436 if (cursor != NULL)
14437 glyph = cursor;
14438 else if (glyph == glyphs_end
14439 && pos_before == pos_after
14440 && STRINGP ((row->reversed_p
14441 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14442 : row->glyphs[TEXT_AREA])->object))
14443 {
14444 /* If all the glyphs of this row came from strings, put the
14445 cursor on the first glyph of the row. This avoids having the
14446 cursor outside of the text area in this very rare and hard
14447 use case. */
14448 glyph =
14449 row->reversed_p
14450 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14451 : row->glyphs[TEXT_AREA];
14452 }
14453 if (x < 0)
14454 {
14455 struct glyph *g;
14456
14457 /* Need to compute x that corresponds to GLYPH. */
14458 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14459 {
14460 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14461 emacs_abort ();
14462 x += g->pixel_width;
14463 }
14464 }
14465
14466 /* ROW could be part of a continued line, which, under bidi
14467 reordering, might have other rows whose start and end charpos
14468 occlude point. Only set w->cursor if we found a better
14469 approximation to the cursor position than we have from previously
14470 examined candidate rows belonging to the same continued line. */
14471 if (/* we already have a candidate row */
14472 w->cursor.vpos >= 0
14473 /* that candidate is not the row we are processing */
14474 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14475 /* Make sure cursor.vpos specifies a row whose start and end
14476 charpos occlude point, and it is valid candidate for being a
14477 cursor-row. This is because some callers of this function
14478 leave cursor.vpos at the row where the cursor was displayed
14479 during the last redisplay cycle. */
14480 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14481 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14482 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14483 {
14484 struct glyph *g1 =
14485 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14486
14487 /* Don't consider glyphs that are outside TEXT_AREA. */
14488 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14489 return 0;
14490 /* Keep the candidate whose buffer position is the closest to
14491 point or has the `cursor' property. */
14492 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14493 w->cursor.hpos >= 0
14494 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14495 && ((BUFFERP (g1->object)
14496 && (g1->charpos == pt_old /* an exact match always wins */
14497 || (BUFFERP (glyph->object)
14498 && eabs (g1->charpos - pt_old)
14499 < eabs (glyph->charpos - pt_old))))
14500 /* previous candidate is a glyph from a string that has
14501 a non-nil `cursor' property */
14502 || (STRINGP (g1->object)
14503 && (!NILP (Fget_char_property (make_number (g1->charpos),
14504 Qcursor, g1->object))
14505 /* previous candidate is from the same display
14506 string as this one, and the display string
14507 came from a text property */
14508 || (EQ (g1->object, glyph->object)
14509 && string_from_text_prop)
14510 /* this candidate is from newline and its
14511 position is not an exact match */
14512 || (INTEGERP (glyph->object)
14513 && glyph->charpos != pt_old)))))
14514 return 0;
14515 /* If this candidate gives an exact match, use that. */
14516 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14517 /* If this candidate is a glyph created for the
14518 terminating newline of a line, and point is on that
14519 newline, it wins because it's an exact match. */
14520 || (!row->continued_p
14521 && INTEGERP (glyph->object)
14522 && glyph->charpos == 0
14523 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14524 /* Otherwise, keep the candidate that comes from a row
14525 spanning less buffer positions. This may win when one or
14526 both candidate positions are on glyphs that came from
14527 display strings, for which we cannot compare buffer
14528 positions. */
14529 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14530 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14531 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14532 return 0;
14533 }
14534 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14535 w->cursor.x = x;
14536 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14537 w->cursor.y = row->y + dy;
14538
14539 if (w == XWINDOW (selected_window))
14540 {
14541 if (!row->continued_p
14542 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14543 && row->x == 0)
14544 {
14545 this_line_buffer = XBUFFER (w->buffer);
14546
14547 CHARPOS (this_line_start_pos)
14548 = MATRIX_ROW_START_CHARPOS (row) + delta;
14549 BYTEPOS (this_line_start_pos)
14550 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14551
14552 CHARPOS (this_line_end_pos)
14553 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14554 BYTEPOS (this_line_end_pos)
14555 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14556
14557 this_line_y = w->cursor.y;
14558 this_line_pixel_height = row->height;
14559 this_line_vpos = w->cursor.vpos;
14560 this_line_start_x = row->x;
14561 }
14562 else
14563 CHARPOS (this_line_start_pos) = 0;
14564 }
14565
14566 return 1;
14567 }
14568
14569
14570 /* Run window scroll functions, if any, for WINDOW with new window
14571 start STARTP. Sets the window start of WINDOW to that position.
14572
14573 We assume that the window's buffer is really current. */
14574
14575 static struct text_pos
14576 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14577 {
14578 struct window *w = XWINDOW (window);
14579 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14580
14581 if (current_buffer != XBUFFER (w->buffer))
14582 emacs_abort ();
14583
14584 if (!NILP (Vwindow_scroll_functions))
14585 {
14586 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14587 make_number (CHARPOS (startp)));
14588 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14589 /* In case the hook functions switch buffers. */
14590 set_buffer_internal (XBUFFER (w->buffer));
14591 }
14592
14593 return startp;
14594 }
14595
14596
14597 /* Make sure the line containing the cursor is fully visible.
14598 A value of 1 means there is nothing to be done.
14599 (Either the line is fully visible, or it cannot be made so,
14600 or we cannot tell.)
14601
14602 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14603 is higher than window.
14604
14605 A value of 0 means the caller should do scrolling
14606 as if point had gone off the screen. */
14607
14608 static int
14609 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14610 {
14611 struct glyph_matrix *matrix;
14612 struct glyph_row *row;
14613 int window_height;
14614
14615 if (!make_cursor_line_fully_visible_p)
14616 return 1;
14617
14618 /* It's not always possible to find the cursor, e.g, when a window
14619 is full of overlay strings. Don't do anything in that case. */
14620 if (w->cursor.vpos < 0)
14621 return 1;
14622
14623 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14624 row = MATRIX_ROW (matrix, w->cursor.vpos);
14625
14626 /* If the cursor row is not partially visible, there's nothing to do. */
14627 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14628 return 1;
14629
14630 /* If the row the cursor is in is taller than the window's height,
14631 it's not clear what to do, so do nothing. */
14632 window_height = window_box_height (w);
14633 if (row->height >= window_height)
14634 {
14635 if (!force_p || MINI_WINDOW_P (w)
14636 || w->vscroll || w->cursor.vpos == 0)
14637 return 1;
14638 }
14639 return 0;
14640 }
14641
14642
14643 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14644 non-zero means only WINDOW is redisplayed in redisplay_internal.
14645 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14646 in redisplay_window to bring a partially visible line into view in
14647 the case that only the cursor has moved.
14648
14649 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14650 last screen line's vertical height extends past the end of the screen.
14651
14652 Value is
14653
14654 1 if scrolling succeeded
14655
14656 0 if scrolling didn't find point.
14657
14658 -1 if new fonts have been loaded so that we must interrupt
14659 redisplay, adjust glyph matrices, and try again. */
14660
14661 enum
14662 {
14663 SCROLLING_SUCCESS,
14664 SCROLLING_FAILED,
14665 SCROLLING_NEED_LARGER_MATRICES
14666 };
14667
14668 /* If scroll-conservatively is more than this, never recenter.
14669
14670 If you change this, don't forget to update the doc string of
14671 `scroll-conservatively' and the Emacs manual. */
14672 #define SCROLL_LIMIT 100
14673
14674 static int
14675 try_scrolling (Lisp_Object window, int just_this_one_p,
14676 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14677 int temp_scroll_step, int last_line_misfit)
14678 {
14679 struct window *w = XWINDOW (window);
14680 struct frame *f = XFRAME (w->frame);
14681 struct text_pos pos, startp;
14682 struct it it;
14683 int this_scroll_margin, scroll_max, rc, height;
14684 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14685 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14686 Lisp_Object aggressive;
14687 /* We will never try scrolling more than this number of lines. */
14688 int scroll_limit = SCROLL_LIMIT;
14689
14690 #ifdef GLYPH_DEBUG
14691 debug_method_add (w, "try_scrolling");
14692 #endif
14693
14694 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14695
14696 /* Compute scroll margin height in pixels. We scroll when point is
14697 within this distance from the top or bottom of the window. */
14698 if (scroll_margin > 0)
14699 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14700 * FRAME_LINE_HEIGHT (f);
14701 else
14702 this_scroll_margin = 0;
14703
14704 /* Force arg_scroll_conservatively to have a reasonable value, to
14705 avoid scrolling too far away with slow move_it_* functions. Note
14706 that the user can supply scroll-conservatively equal to
14707 `most-positive-fixnum', which can be larger than INT_MAX. */
14708 if (arg_scroll_conservatively > scroll_limit)
14709 {
14710 arg_scroll_conservatively = scroll_limit + 1;
14711 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14712 }
14713 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14714 /* Compute how much we should try to scroll maximally to bring
14715 point into view. */
14716 scroll_max = (max (scroll_step,
14717 max (arg_scroll_conservatively, temp_scroll_step))
14718 * FRAME_LINE_HEIGHT (f));
14719 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14720 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14721 /* We're trying to scroll because of aggressive scrolling but no
14722 scroll_step is set. Choose an arbitrary one. */
14723 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14724 else
14725 scroll_max = 0;
14726
14727 too_near_end:
14728
14729 /* Decide whether to scroll down. */
14730 if (PT > CHARPOS (startp))
14731 {
14732 int scroll_margin_y;
14733
14734 /* Compute the pixel ypos of the scroll margin, then move IT to
14735 either that ypos or PT, whichever comes first. */
14736 start_display (&it, w, startp);
14737 scroll_margin_y = it.last_visible_y - this_scroll_margin
14738 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14739 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14740 (MOVE_TO_POS | MOVE_TO_Y));
14741
14742 if (PT > CHARPOS (it.current.pos))
14743 {
14744 int y0 = line_bottom_y (&it);
14745 /* Compute how many pixels below window bottom to stop searching
14746 for PT. This avoids costly search for PT that is far away if
14747 the user limited scrolling by a small number of lines, but
14748 always finds PT if scroll_conservatively is set to a large
14749 number, such as most-positive-fixnum. */
14750 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14751 int y_to_move = it.last_visible_y + slack;
14752
14753 /* Compute the distance from the scroll margin to PT or to
14754 the scroll limit, whichever comes first. This should
14755 include the height of the cursor line, to make that line
14756 fully visible. */
14757 move_it_to (&it, PT, -1, y_to_move,
14758 -1, MOVE_TO_POS | MOVE_TO_Y);
14759 dy = line_bottom_y (&it) - y0;
14760
14761 if (dy > scroll_max)
14762 return SCROLLING_FAILED;
14763
14764 if (dy > 0)
14765 scroll_down_p = 1;
14766 }
14767 }
14768
14769 if (scroll_down_p)
14770 {
14771 /* Point is in or below the bottom scroll margin, so move the
14772 window start down. If scrolling conservatively, move it just
14773 enough down to make point visible. If scroll_step is set,
14774 move it down by scroll_step. */
14775 if (arg_scroll_conservatively)
14776 amount_to_scroll
14777 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14778 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14779 else if (scroll_step || temp_scroll_step)
14780 amount_to_scroll = scroll_max;
14781 else
14782 {
14783 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14784 height = WINDOW_BOX_TEXT_HEIGHT (w);
14785 if (NUMBERP (aggressive))
14786 {
14787 double float_amount = XFLOATINT (aggressive) * height;
14788 int aggressive_scroll = float_amount;
14789 if (aggressive_scroll == 0 && float_amount > 0)
14790 aggressive_scroll = 1;
14791 /* Don't let point enter the scroll margin near top of
14792 the window. This could happen if the value of
14793 scroll_up_aggressively is too large and there are
14794 non-zero margins, because scroll_up_aggressively
14795 means put point that fraction of window height
14796 _from_the_bottom_margin_. */
14797 if (aggressive_scroll + 2*this_scroll_margin > height)
14798 aggressive_scroll = height - 2*this_scroll_margin;
14799 amount_to_scroll = dy + aggressive_scroll;
14800 }
14801 }
14802
14803 if (amount_to_scroll <= 0)
14804 return SCROLLING_FAILED;
14805
14806 start_display (&it, w, startp);
14807 if (arg_scroll_conservatively <= scroll_limit)
14808 move_it_vertically (&it, amount_to_scroll);
14809 else
14810 {
14811 /* Extra precision for users who set scroll-conservatively
14812 to a large number: make sure the amount we scroll
14813 the window start is never less than amount_to_scroll,
14814 which was computed as distance from window bottom to
14815 point. This matters when lines at window top and lines
14816 below window bottom have different height. */
14817 struct it it1;
14818 void *it1data = NULL;
14819 /* We use a temporary it1 because line_bottom_y can modify
14820 its argument, if it moves one line down; see there. */
14821 int start_y;
14822
14823 SAVE_IT (it1, it, it1data);
14824 start_y = line_bottom_y (&it1);
14825 do {
14826 RESTORE_IT (&it, &it, it1data);
14827 move_it_by_lines (&it, 1);
14828 SAVE_IT (it1, it, it1data);
14829 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14830 }
14831
14832 /* If STARTP is unchanged, move it down another screen line. */
14833 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14834 move_it_by_lines (&it, 1);
14835 startp = it.current.pos;
14836 }
14837 else
14838 {
14839 struct text_pos scroll_margin_pos = startp;
14840
14841 /* See if point is inside the scroll margin at the top of the
14842 window. */
14843 if (this_scroll_margin)
14844 {
14845 start_display (&it, w, startp);
14846 move_it_vertically (&it, this_scroll_margin);
14847 scroll_margin_pos = it.current.pos;
14848 }
14849
14850 if (PT < CHARPOS (scroll_margin_pos))
14851 {
14852 /* Point is in the scroll margin at the top of the window or
14853 above what is displayed in the window. */
14854 int y0, y_to_move;
14855
14856 /* Compute the vertical distance from PT to the scroll
14857 margin position. Move as far as scroll_max allows, or
14858 one screenful, or 10 screen lines, whichever is largest.
14859 Give up if distance is greater than scroll_max or if we
14860 didn't reach the scroll margin position. */
14861 SET_TEXT_POS (pos, PT, PT_BYTE);
14862 start_display (&it, w, pos);
14863 y0 = it.current_y;
14864 y_to_move = max (it.last_visible_y,
14865 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14866 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14867 y_to_move, -1,
14868 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14869 dy = it.current_y - y0;
14870 if (dy > scroll_max
14871 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14872 return SCROLLING_FAILED;
14873
14874 /* Compute new window start. */
14875 start_display (&it, w, startp);
14876
14877 if (arg_scroll_conservatively)
14878 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14879 max (scroll_step, temp_scroll_step));
14880 else if (scroll_step || temp_scroll_step)
14881 amount_to_scroll = scroll_max;
14882 else
14883 {
14884 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14885 height = WINDOW_BOX_TEXT_HEIGHT (w);
14886 if (NUMBERP (aggressive))
14887 {
14888 double float_amount = XFLOATINT (aggressive) * height;
14889 int aggressive_scroll = float_amount;
14890 if (aggressive_scroll == 0 && float_amount > 0)
14891 aggressive_scroll = 1;
14892 /* Don't let point enter the scroll margin near
14893 bottom of the window, if the value of
14894 scroll_down_aggressively happens to be too
14895 large. */
14896 if (aggressive_scroll + 2*this_scroll_margin > height)
14897 aggressive_scroll = height - 2*this_scroll_margin;
14898 amount_to_scroll = dy + aggressive_scroll;
14899 }
14900 }
14901
14902 if (amount_to_scroll <= 0)
14903 return SCROLLING_FAILED;
14904
14905 move_it_vertically_backward (&it, amount_to_scroll);
14906 startp = it.current.pos;
14907 }
14908 }
14909
14910 /* Run window scroll functions. */
14911 startp = run_window_scroll_functions (window, startp);
14912
14913 /* Display the window. Give up if new fonts are loaded, or if point
14914 doesn't appear. */
14915 if (!try_window (window, startp, 0))
14916 rc = SCROLLING_NEED_LARGER_MATRICES;
14917 else if (w->cursor.vpos < 0)
14918 {
14919 clear_glyph_matrix (w->desired_matrix);
14920 rc = SCROLLING_FAILED;
14921 }
14922 else
14923 {
14924 /* Maybe forget recorded base line for line number display. */
14925 if (!just_this_one_p
14926 || current_buffer->clip_changed
14927 || BEG_UNCHANGED < CHARPOS (startp))
14928 wset_base_line_number (w, Qnil);
14929
14930 /* If cursor ends up on a partially visible line,
14931 treat that as being off the bottom of the screen. */
14932 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14933 /* It's possible that the cursor is on the first line of the
14934 buffer, which is partially obscured due to a vscroll
14935 (Bug#7537). In that case, avoid looping forever . */
14936 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14937 {
14938 clear_glyph_matrix (w->desired_matrix);
14939 ++extra_scroll_margin_lines;
14940 goto too_near_end;
14941 }
14942 rc = SCROLLING_SUCCESS;
14943 }
14944
14945 return rc;
14946 }
14947
14948
14949 /* Compute a suitable window start for window W if display of W starts
14950 on a continuation line. Value is non-zero if a new window start
14951 was computed.
14952
14953 The new window start will be computed, based on W's width, starting
14954 from the start of the continued line. It is the start of the
14955 screen line with the minimum distance from the old start W->start. */
14956
14957 static int
14958 compute_window_start_on_continuation_line (struct window *w)
14959 {
14960 struct text_pos pos, start_pos;
14961 int window_start_changed_p = 0;
14962
14963 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14964
14965 /* If window start is on a continuation line... Window start may be
14966 < BEGV in case there's invisible text at the start of the
14967 buffer (M-x rmail, for example). */
14968 if (CHARPOS (start_pos) > BEGV
14969 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14970 {
14971 struct it it;
14972 struct glyph_row *row;
14973
14974 /* Handle the case that the window start is out of range. */
14975 if (CHARPOS (start_pos) < BEGV)
14976 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14977 else if (CHARPOS (start_pos) > ZV)
14978 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14979
14980 /* Find the start of the continued line. This should be fast
14981 because scan_buffer is fast (newline cache). */
14982 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14983 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14984 row, DEFAULT_FACE_ID);
14985 reseat_at_previous_visible_line_start (&it);
14986
14987 /* If the line start is "too far" away from the window start,
14988 say it takes too much time to compute a new window start. */
14989 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14990 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14991 {
14992 int min_distance, distance;
14993
14994 /* Move forward by display lines to find the new window
14995 start. If window width was enlarged, the new start can
14996 be expected to be > the old start. If window width was
14997 decreased, the new window start will be < the old start.
14998 So, we're looking for the display line start with the
14999 minimum distance from the old window start. */
15000 pos = it.current.pos;
15001 min_distance = INFINITY;
15002 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15003 distance < min_distance)
15004 {
15005 min_distance = distance;
15006 pos = it.current.pos;
15007 move_it_by_lines (&it, 1);
15008 }
15009
15010 /* Set the window start there. */
15011 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15012 window_start_changed_p = 1;
15013 }
15014 }
15015
15016 return window_start_changed_p;
15017 }
15018
15019
15020 /* Try cursor movement in case text has not changed in window WINDOW,
15021 with window start STARTP. Value is
15022
15023 CURSOR_MOVEMENT_SUCCESS if successful
15024
15025 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15026
15027 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15028 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15029 we want to scroll as if scroll-step were set to 1. See the code.
15030
15031 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15032 which case we have to abort this redisplay, and adjust matrices
15033 first. */
15034
15035 enum
15036 {
15037 CURSOR_MOVEMENT_SUCCESS,
15038 CURSOR_MOVEMENT_CANNOT_BE_USED,
15039 CURSOR_MOVEMENT_MUST_SCROLL,
15040 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15041 };
15042
15043 static int
15044 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15045 {
15046 struct window *w = XWINDOW (window);
15047 struct frame *f = XFRAME (w->frame);
15048 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15049
15050 #ifdef GLYPH_DEBUG
15051 if (inhibit_try_cursor_movement)
15052 return rc;
15053 #endif
15054
15055 /* Previously, there was a check for Lisp integer in the
15056 if-statement below. Now, this field is converted to
15057 ptrdiff_t, thus zero means invalid position in a buffer. */
15058 eassert (w->last_point > 0);
15059
15060 /* Handle case where text has not changed, only point, and it has
15061 not moved off the frame. */
15062 if (/* Point may be in this window. */
15063 PT >= CHARPOS (startp)
15064 /* Selective display hasn't changed. */
15065 && !current_buffer->clip_changed
15066 /* Function force-mode-line-update is used to force a thorough
15067 redisplay. It sets either windows_or_buffers_changed or
15068 update_mode_lines. So don't take a shortcut here for these
15069 cases. */
15070 && !update_mode_lines
15071 && !windows_or_buffers_changed
15072 && !cursor_type_changed
15073 /* Can't use this case if highlighting a region. When a
15074 region exists, cursor movement has to do more than just
15075 set the cursor. */
15076 && !(!NILP (Vtransient_mark_mode)
15077 && !NILP (BVAR (current_buffer, mark_active)))
15078 && NILP (w->region_showing)
15079 && NILP (Vshow_trailing_whitespace)
15080 /* This code is not used for mini-buffer for the sake of the case
15081 of redisplaying to replace an echo area message; since in
15082 that case the mini-buffer contents per se are usually
15083 unchanged. This code is of no real use in the mini-buffer
15084 since the handling of this_line_start_pos, etc., in redisplay
15085 handles the same cases. */
15086 && !EQ (window, minibuf_window)
15087 /* When splitting windows or for new windows, it happens that
15088 redisplay is called with a nil window_end_vpos or one being
15089 larger than the window. This should really be fixed in
15090 window.c. I don't have this on my list, now, so we do
15091 approximately the same as the old redisplay code. --gerd. */
15092 && INTEGERP (w->window_end_vpos)
15093 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15094 && (FRAME_WINDOW_P (f)
15095 || !overlay_arrow_in_current_buffer_p ()))
15096 {
15097 int this_scroll_margin, top_scroll_margin;
15098 struct glyph_row *row = NULL;
15099
15100 #ifdef GLYPH_DEBUG
15101 debug_method_add (w, "cursor movement");
15102 #endif
15103
15104 /* Scroll if point within this distance from the top or bottom
15105 of the window. This is a pixel value. */
15106 if (scroll_margin > 0)
15107 {
15108 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15109 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15110 }
15111 else
15112 this_scroll_margin = 0;
15113
15114 top_scroll_margin = this_scroll_margin;
15115 if (WINDOW_WANTS_HEADER_LINE_P (w))
15116 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15117
15118 /* Start with the row the cursor was displayed during the last
15119 not paused redisplay. Give up if that row is not valid. */
15120 if (w->last_cursor.vpos < 0
15121 || w->last_cursor.vpos >= w->current_matrix->nrows)
15122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15123 else
15124 {
15125 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15126 if (row->mode_line_p)
15127 ++row;
15128 if (!row->enabled_p)
15129 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15130 }
15131
15132 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15133 {
15134 int scroll_p = 0, must_scroll = 0;
15135 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15136
15137 if (PT > w->last_point)
15138 {
15139 /* Point has moved forward. */
15140 while (MATRIX_ROW_END_CHARPOS (row) < PT
15141 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15142 {
15143 eassert (row->enabled_p);
15144 ++row;
15145 }
15146
15147 /* If the end position of a row equals the start
15148 position of the next row, and PT is at that position,
15149 we would rather display cursor in the next line. */
15150 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15151 && MATRIX_ROW_END_CHARPOS (row) == PT
15152 && row < w->current_matrix->rows
15153 + w->current_matrix->nrows - 1
15154 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15155 && !cursor_row_p (row))
15156 ++row;
15157
15158 /* If within the scroll margin, scroll. Note that
15159 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15160 the next line would be drawn, and that
15161 this_scroll_margin can be zero. */
15162 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15163 || PT > MATRIX_ROW_END_CHARPOS (row)
15164 /* Line is completely visible last line in window
15165 and PT is to be set in the next line. */
15166 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15167 && PT == MATRIX_ROW_END_CHARPOS (row)
15168 && !row->ends_at_zv_p
15169 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15170 scroll_p = 1;
15171 }
15172 else if (PT < w->last_point)
15173 {
15174 /* Cursor has to be moved backward. Note that PT >=
15175 CHARPOS (startp) because of the outer if-statement. */
15176 while (!row->mode_line_p
15177 && (MATRIX_ROW_START_CHARPOS (row) > PT
15178 || (MATRIX_ROW_START_CHARPOS (row) == PT
15179 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15180 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15181 row > w->current_matrix->rows
15182 && (row-1)->ends_in_newline_from_string_p))))
15183 && (row->y > top_scroll_margin
15184 || CHARPOS (startp) == BEGV))
15185 {
15186 eassert (row->enabled_p);
15187 --row;
15188 }
15189
15190 /* Consider the following case: Window starts at BEGV,
15191 there is invisible, intangible text at BEGV, so that
15192 display starts at some point START > BEGV. It can
15193 happen that we are called with PT somewhere between
15194 BEGV and START. Try to handle that case. */
15195 if (row < w->current_matrix->rows
15196 || row->mode_line_p)
15197 {
15198 row = w->current_matrix->rows;
15199 if (row->mode_line_p)
15200 ++row;
15201 }
15202
15203 /* Due to newlines in overlay strings, we may have to
15204 skip forward over overlay strings. */
15205 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15206 && MATRIX_ROW_END_CHARPOS (row) == PT
15207 && !cursor_row_p (row))
15208 ++row;
15209
15210 /* If within the scroll margin, scroll. */
15211 if (row->y < top_scroll_margin
15212 && CHARPOS (startp) != BEGV)
15213 scroll_p = 1;
15214 }
15215 else
15216 {
15217 /* Cursor did not move. So don't scroll even if cursor line
15218 is partially visible, as it was so before. */
15219 rc = CURSOR_MOVEMENT_SUCCESS;
15220 }
15221
15222 if (PT < MATRIX_ROW_START_CHARPOS (row)
15223 || PT > MATRIX_ROW_END_CHARPOS (row))
15224 {
15225 /* if PT is not in the glyph row, give up. */
15226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15227 must_scroll = 1;
15228 }
15229 else if (rc != CURSOR_MOVEMENT_SUCCESS
15230 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15231 {
15232 struct glyph_row *row1;
15233
15234 /* If rows are bidi-reordered and point moved, back up
15235 until we find a row that does not belong to a
15236 continuation line. This is because we must consider
15237 all rows of a continued line as candidates for the
15238 new cursor positioning, since row start and end
15239 positions change non-linearly with vertical position
15240 in such rows. */
15241 /* FIXME: Revisit this when glyph ``spilling'' in
15242 continuation lines' rows is implemented for
15243 bidi-reordered rows. */
15244 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15245 MATRIX_ROW_CONTINUATION_LINE_P (row);
15246 --row)
15247 {
15248 /* If we hit the beginning of the displayed portion
15249 without finding the first row of a continued
15250 line, give up. */
15251 if (row <= row1)
15252 {
15253 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15254 break;
15255 }
15256 eassert (row->enabled_p);
15257 }
15258 }
15259 if (must_scroll)
15260 ;
15261 else if (rc != CURSOR_MOVEMENT_SUCCESS
15262 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15263 /* Make sure this isn't a header line by any chance, since
15264 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15265 && !row->mode_line_p
15266 && make_cursor_line_fully_visible_p)
15267 {
15268 if (PT == MATRIX_ROW_END_CHARPOS (row)
15269 && !row->ends_at_zv_p
15270 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15271 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15272 else if (row->height > window_box_height (w))
15273 {
15274 /* If we end up in a partially visible line, let's
15275 make it fully visible, except when it's taller
15276 than the window, in which case we can't do much
15277 about it. */
15278 *scroll_step = 1;
15279 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15280 }
15281 else
15282 {
15283 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15284 if (!cursor_row_fully_visible_p (w, 0, 1))
15285 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15286 else
15287 rc = CURSOR_MOVEMENT_SUCCESS;
15288 }
15289 }
15290 else if (scroll_p)
15291 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15292 else if (rc != CURSOR_MOVEMENT_SUCCESS
15293 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15294 {
15295 /* With bidi-reordered rows, there could be more than
15296 one candidate row whose start and end positions
15297 occlude point. We need to let set_cursor_from_row
15298 find the best candidate. */
15299 /* FIXME: Revisit this when glyph ``spilling'' in
15300 continuation lines' rows is implemented for
15301 bidi-reordered rows. */
15302 int rv = 0;
15303
15304 do
15305 {
15306 int at_zv_p = 0, exact_match_p = 0;
15307
15308 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15309 && PT <= MATRIX_ROW_END_CHARPOS (row)
15310 && cursor_row_p (row))
15311 rv |= set_cursor_from_row (w, row, w->current_matrix,
15312 0, 0, 0, 0);
15313 /* As soon as we've found the exact match for point,
15314 or the first suitable row whose ends_at_zv_p flag
15315 is set, we are done. */
15316 at_zv_p =
15317 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15318 if (rv && !at_zv_p
15319 && w->cursor.hpos >= 0
15320 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15321 w->cursor.vpos))
15322 {
15323 struct glyph_row *candidate =
15324 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15325 struct glyph *g =
15326 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15327 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15328
15329 exact_match_p =
15330 (BUFFERP (g->object) && g->charpos == PT)
15331 || (INTEGERP (g->object)
15332 && (g->charpos == PT
15333 || (g->charpos == 0 && endpos - 1 == PT)));
15334 }
15335 if (rv && (at_zv_p || exact_match_p))
15336 {
15337 rc = CURSOR_MOVEMENT_SUCCESS;
15338 break;
15339 }
15340 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15341 break;
15342 ++row;
15343 }
15344 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15345 || row->continued_p)
15346 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15347 || (MATRIX_ROW_START_CHARPOS (row) == PT
15348 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15349 /* If we didn't find any candidate rows, or exited the
15350 loop before all the candidates were examined, signal
15351 to the caller that this method failed. */
15352 if (rc != CURSOR_MOVEMENT_SUCCESS
15353 && !(rv
15354 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15355 && !row->continued_p))
15356 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15357 else if (rv)
15358 rc = CURSOR_MOVEMENT_SUCCESS;
15359 }
15360 else
15361 {
15362 do
15363 {
15364 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15365 {
15366 rc = CURSOR_MOVEMENT_SUCCESS;
15367 break;
15368 }
15369 ++row;
15370 }
15371 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15372 && MATRIX_ROW_START_CHARPOS (row) == PT
15373 && cursor_row_p (row));
15374 }
15375 }
15376 }
15377
15378 return rc;
15379 }
15380
15381 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15382 static
15383 #endif
15384 void
15385 set_vertical_scroll_bar (struct window *w)
15386 {
15387 ptrdiff_t start, end, whole;
15388
15389 /* Calculate the start and end positions for the current window.
15390 At some point, it would be nice to choose between scrollbars
15391 which reflect the whole buffer size, with special markers
15392 indicating narrowing, and scrollbars which reflect only the
15393 visible region.
15394
15395 Note that mini-buffers sometimes aren't displaying any text. */
15396 if (!MINI_WINDOW_P (w)
15397 || (w == XWINDOW (minibuf_window)
15398 && NILP (echo_area_buffer[0])))
15399 {
15400 struct buffer *buf = XBUFFER (w->buffer);
15401 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15402 start = marker_position (w->start) - BUF_BEGV (buf);
15403 /* I don't think this is guaranteed to be right. For the
15404 moment, we'll pretend it is. */
15405 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15406
15407 if (end < start)
15408 end = start;
15409 if (whole < (end - start))
15410 whole = end - start;
15411 }
15412 else
15413 start = end = whole = 0;
15414
15415 /* Indicate what this scroll bar ought to be displaying now. */
15416 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15417 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15418 (w, end - start, whole, start);
15419 }
15420
15421
15422 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15423 selected_window is redisplayed.
15424
15425 We can return without actually redisplaying the window if
15426 fonts_changed_p. In that case, redisplay_internal will
15427 retry. */
15428
15429 static void
15430 redisplay_window (Lisp_Object window, int just_this_one_p)
15431 {
15432 struct window *w = XWINDOW (window);
15433 struct frame *f = XFRAME (w->frame);
15434 struct buffer *buffer = XBUFFER (w->buffer);
15435 struct buffer *old = current_buffer;
15436 struct text_pos lpoint, opoint, startp;
15437 int update_mode_line;
15438 int tem;
15439 struct it it;
15440 /* Record it now because it's overwritten. */
15441 int current_matrix_up_to_date_p = 0;
15442 int used_current_matrix_p = 0;
15443 /* This is less strict than current_matrix_up_to_date_p.
15444 It indicates that the buffer contents and narrowing are unchanged. */
15445 int buffer_unchanged_p = 0;
15446 int temp_scroll_step = 0;
15447 ptrdiff_t count = SPECPDL_INDEX ();
15448 int rc;
15449 int centering_position = -1;
15450 int last_line_misfit = 0;
15451 ptrdiff_t beg_unchanged, end_unchanged;
15452
15453 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15454 opoint = lpoint;
15455
15456 /* W must be a leaf window here. */
15457 eassert (!NILP (w->buffer));
15458 #ifdef GLYPH_DEBUG
15459 *w->desired_matrix->method = 0;
15460 #endif
15461
15462 restart:
15463 reconsider_clip_changes (w, buffer);
15464
15465 /* Has the mode line to be updated? */
15466 update_mode_line = (w->update_mode_line
15467 || update_mode_lines
15468 || buffer->clip_changed
15469 || buffer->prevent_redisplay_optimizations_p);
15470
15471 if (MINI_WINDOW_P (w))
15472 {
15473 if (w == XWINDOW (echo_area_window)
15474 && !NILP (echo_area_buffer[0]))
15475 {
15476 if (update_mode_line)
15477 /* We may have to update a tty frame's menu bar or a
15478 tool-bar. Example `M-x C-h C-h C-g'. */
15479 goto finish_menu_bars;
15480 else
15481 /* We've already displayed the echo area glyphs in this window. */
15482 goto finish_scroll_bars;
15483 }
15484 else if ((w != XWINDOW (minibuf_window)
15485 || minibuf_level == 0)
15486 /* When buffer is nonempty, redisplay window normally. */
15487 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15488 /* Quail displays non-mini buffers in minibuffer window.
15489 In that case, redisplay the window normally. */
15490 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15491 {
15492 /* W is a mini-buffer window, but it's not active, so clear
15493 it. */
15494 int yb = window_text_bottom_y (w);
15495 struct glyph_row *row;
15496 int y;
15497
15498 for (y = 0, row = w->desired_matrix->rows;
15499 y < yb;
15500 y += row->height, ++row)
15501 blank_row (w, row, y);
15502 goto finish_scroll_bars;
15503 }
15504
15505 clear_glyph_matrix (w->desired_matrix);
15506 }
15507
15508 /* Otherwise set up data on this window; select its buffer and point
15509 value. */
15510 /* Really select the buffer, for the sake of buffer-local
15511 variables. */
15512 set_buffer_internal_1 (XBUFFER (w->buffer));
15513
15514 current_matrix_up_to_date_p
15515 = (!NILP (w->window_end_valid)
15516 && !current_buffer->clip_changed
15517 && !current_buffer->prevent_redisplay_optimizations_p
15518 && !window_outdated (w));
15519
15520 /* Run the window-bottom-change-functions
15521 if it is possible that the text on the screen has changed
15522 (either due to modification of the text, or any other reason). */
15523 if (!current_matrix_up_to_date_p
15524 && !NILP (Vwindow_text_change_functions))
15525 {
15526 safe_run_hooks (Qwindow_text_change_functions);
15527 goto restart;
15528 }
15529
15530 beg_unchanged = BEG_UNCHANGED;
15531 end_unchanged = END_UNCHANGED;
15532
15533 SET_TEXT_POS (opoint, PT, PT_BYTE);
15534
15535 specbind (Qinhibit_point_motion_hooks, Qt);
15536
15537 buffer_unchanged_p
15538 = (!NILP (w->window_end_valid)
15539 && !current_buffer->clip_changed
15540 && !window_outdated (w));
15541
15542 /* When windows_or_buffers_changed is non-zero, we can't rely on
15543 the window end being valid, so set it to nil there. */
15544 if (windows_or_buffers_changed)
15545 {
15546 /* If window starts on a continuation line, maybe adjust the
15547 window start in case the window's width changed. */
15548 if (XMARKER (w->start)->buffer == current_buffer)
15549 compute_window_start_on_continuation_line (w);
15550
15551 wset_window_end_valid (w, Qnil);
15552 }
15553
15554 /* Some sanity checks. */
15555 CHECK_WINDOW_END (w);
15556 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15557 emacs_abort ();
15558 if (BYTEPOS (opoint) < CHARPOS (opoint))
15559 emacs_abort ();
15560
15561 if (mode_line_update_needed (w))
15562 update_mode_line = 1;
15563
15564 /* Point refers normally to the selected window. For any other
15565 window, set up appropriate value. */
15566 if (!EQ (window, selected_window))
15567 {
15568 ptrdiff_t new_pt = marker_position (w->pointm);
15569 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15570 if (new_pt < BEGV)
15571 {
15572 new_pt = BEGV;
15573 new_pt_byte = BEGV_BYTE;
15574 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15575 }
15576 else if (new_pt > (ZV - 1))
15577 {
15578 new_pt = ZV;
15579 new_pt_byte = ZV_BYTE;
15580 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15581 }
15582
15583 /* We don't use SET_PT so that the point-motion hooks don't run. */
15584 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15585 }
15586
15587 /* If any of the character widths specified in the display table
15588 have changed, invalidate the width run cache. It's true that
15589 this may be a bit late to catch such changes, but the rest of
15590 redisplay goes (non-fatally) haywire when the display table is
15591 changed, so why should we worry about doing any better? */
15592 if (current_buffer->width_run_cache)
15593 {
15594 struct Lisp_Char_Table *disptab = buffer_display_table ();
15595
15596 if (! disptab_matches_widthtab
15597 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15598 {
15599 invalidate_region_cache (current_buffer,
15600 current_buffer->width_run_cache,
15601 BEG, Z);
15602 recompute_width_table (current_buffer, disptab);
15603 }
15604 }
15605
15606 /* If window-start is screwed up, choose a new one. */
15607 if (XMARKER (w->start)->buffer != current_buffer)
15608 goto recenter;
15609
15610 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15611
15612 /* If someone specified a new starting point but did not insist,
15613 check whether it can be used. */
15614 if (w->optional_new_start
15615 && CHARPOS (startp) >= BEGV
15616 && CHARPOS (startp) <= ZV)
15617 {
15618 w->optional_new_start = 0;
15619 start_display (&it, w, startp);
15620 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15621 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15622 if (IT_CHARPOS (it) == PT)
15623 w->force_start = 1;
15624 /* IT may overshoot PT if text at PT is invisible. */
15625 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15626 w->force_start = 1;
15627 }
15628
15629 force_start:
15630
15631 /* Handle case where place to start displaying has been specified,
15632 unless the specified location is outside the accessible range. */
15633 if (w->force_start || w->frozen_window_start_p)
15634 {
15635 /* We set this later on if we have to adjust point. */
15636 int new_vpos = -1;
15637
15638 w->force_start = 0;
15639 w->vscroll = 0;
15640 wset_window_end_valid (w, Qnil);
15641
15642 /* Forget any recorded base line for line number display. */
15643 if (!buffer_unchanged_p)
15644 wset_base_line_number (w, Qnil);
15645
15646 /* Redisplay the mode line. Select the buffer properly for that.
15647 Also, run the hook window-scroll-functions
15648 because we have scrolled. */
15649 /* Note, we do this after clearing force_start because
15650 if there's an error, it is better to forget about force_start
15651 than to get into an infinite loop calling the hook functions
15652 and having them get more errors. */
15653 if (!update_mode_line
15654 || ! NILP (Vwindow_scroll_functions))
15655 {
15656 update_mode_line = 1;
15657 w->update_mode_line = 1;
15658 startp = run_window_scroll_functions (window, startp);
15659 }
15660
15661 w->last_modified = 0;
15662 w->last_overlay_modified = 0;
15663 if (CHARPOS (startp) < BEGV)
15664 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15665 else if (CHARPOS (startp) > ZV)
15666 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15667
15668 /* Redisplay, then check if cursor has been set during the
15669 redisplay. Give up if new fonts were loaded. */
15670 /* We used to issue a CHECK_MARGINS argument to try_window here,
15671 but this causes scrolling to fail when point begins inside
15672 the scroll margin (bug#148) -- cyd */
15673 if (!try_window (window, startp, 0))
15674 {
15675 w->force_start = 1;
15676 clear_glyph_matrix (w->desired_matrix);
15677 goto need_larger_matrices;
15678 }
15679
15680 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15681 {
15682 /* If point does not appear, try to move point so it does
15683 appear. The desired matrix has been built above, so we
15684 can use it here. */
15685 new_vpos = window_box_height (w) / 2;
15686 }
15687
15688 if (!cursor_row_fully_visible_p (w, 0, 0))
15689 {
15690 /* Point does appear, but on a line partly visible at end of window.
15691 Move it back to a fully-visible line. */
15692 new_vpos = window_box_height (w);
15693 }
15694 else if (w->cursor.vpos >=0)
15695 {
15696 /* Some people insist on not letting point enter the scroll
15697 margin, even though this part handles windows that didn't
15698 scroll at all. */
15699 struct frame *f = XFRAME (w->frame);
15700 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15701 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15702 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15703
15704 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15705 below, which finds the row to move point to, advances by
15706 the Y coordinate of the _next_ row, see the definition of
15707 MATRIX_ROW_BOTTOM_Y. */
15708 if (w->cursor.vpos < margin + header_line)
15709 new_vpos
15710 = pixel_margin + (header_line
15711 ? CURRENT_HEADER_LINE_HEIGHT (w)
15712 : 0) + FRAME_LINE_HEIGHT (f);
15713 else
15714 {
15715 int window_height = window_box_height (w);
15716
15717 if (header_line)
15718 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15719 if (w->cursor.y >= window_height - pixel_margin)
15720 new_vpos = window_height - pixel_margin;
15721 }
15722 }
15723
15724 /* If we need to move point for either of the above reasons,
15725 now actually do it. */
15726 if (new_vpos >= 0)
15727 {
15728 struct glyph_row *row;
15729
15730 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15731 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15732 ++row;
15733
15734 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15735 MATRIX_ROW_START_BYTEPOS (row));
15736
15737 if (w != XWINDOW (selected_window))
15738 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15739 else if (current_buffer == old)
15740 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15741
15742 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15743
15744 /* If we are highlighting the region, then we just changed
15745 the region, so redisplay to show it. */
15746 if (!NILP (Vtransient_mark_mode)
15747 && !NILP (BVAR (current_buffer, mark_active)))
15748 {
15749 clear_glyph_matrix (w->desired_matrix);
15750 if (!try_window (window, startp, 0))
15751 goto need_larger_matrices;
15752 }
15753 }
15754
15755 #ifdef GLYPH_DEBUG
15756 debug_method_add (w, "forced window start");
15757 #endif
15758 goto done;
15759 }
15760
15761 /* Handle case where text has not changed, only point, and it has
15762 not moved off the frame, and we are not retrying after hscroll.
15763 (current_matrix_up_to_date_p is nonzero when retrying.) */
15764 if (current_matrix_up_to_date_p
15765 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15766 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15767 {
15768 switch (rc)
15769 {
15770 case CURSOR_MOVEMENT_SUCCESS:
15771 used_current_matrix_p = 1;
15772 goto done;
15773
15774 case CURSOR_MOVEMENT_MUST_SCROLL:
15775 goto try_to_scroll;
15776
15777 default:
15778 emacs_abort ();
15779 }
15780 }
15781 /* If current starting point was originally the beginning of a line
15782 but no longer is, find a new starting point. */
15783 else if (w->start_at_line_beg
15784 && !(CHARPOS (startp) <= BEGV
15785 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15786 {
15787 #ifdef GLYPH_DEBUG
15788 debug_method_add (w, "recenter 1");
15789 #endif
15790 goto recenter;
15791 }
15792
15793 /* Try scrolling with try_window_id. Value is > 0 if update has
15794 been done, it is -1 if we know that the same window start will
15795 not work. It is 0 if unsuccessful for some other reason. */
15796 else if ((tem = try_window_id (w)) != 0)
15797 {
15798 #ifdef GLYPH_DEBUG
15799 debug_method_add (w, "try_window_id %d", tem);
15800 #endif
15801
15802 if (fonts_changed_p)
15803 goto need_larger_matrices;
15804 if (tem > 0)
15805 goto done;
15806
15807 /* Otherwise try_window_id has returned -1 which means that we
15808 don't want the alternative below this comment to execute. */
15809 }
15810 else if (CHARPOS (startp) >= BEGV
15811 && CHARPOS (startp) <= ZV
15812 && PT >= CHARPOS (startp)
15813 && (CHARPOS (startp) < ZV
15814 /* Avoid starting at end of buffer. */
15815 || CHARPOS (startp) == BEGV
15816 || !window_outdated (w)))
15817 {
15818 int d1, d2, d3, d4, d5, d6;
15819
15820 /* If first window line is a continuation line, and window start
15821 is inside the modified region, but the first change is before
15822 current window start, we must select a new window start.
15823
15824 However, if this is the result of a down-mouse event (e.g. by
15825 extending the mouse-drag-overlay), we don't want to select a
15826 new window start, since that would change the position under
15827 the mouse, resulting in an unwanted mouse-movement rather
15828 than a simple mouse-click. */
15829 if (!w->start_at_line_beg
15830 && NILP (do_mouse_tracking)
15831 && CHARPOS (startp) > BEGV
15832 && CHARPOS (startp) > BEG + beg_unchanged
15833 && CHARPOS (startp) <= Z - end_unchanged
15834 /* Even if w->start_at_line_beg is nil, a new window may
15835 start at a line_beg, since that's how set_buffer_window
15836 sets it. So, we need to check the return value of
15837 compute_window_start_on_continuation_line. (See also
15838 bug#197). */
15839 && XMARKER (w->start)->buffer == current_buffer
15840 && compute_window_start_on_continuation_line (w)
15841 /* It doesn't make sense to force the window start like we
15842 do at label force_start if it is already known that point
15843 will not be visible in the resulting window, because
15844 doing so will move point from its correct position
15845 instead of scrolling the window to bring point into view.
15846 See bug#9324. */
15847 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15848 {
15849 w->force_start = 1;
15850 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15851 goto force_start;
15852 }
15853
15854 #ifdef GLYPH_DEBUG
15855 debug_method_add (w, "same window start");
15856 #endif
15857
15858 /* Try to redisplay starting at same place as before.
15859 If point has not moved off frame, accept the results. */
15860 if (!current_matrix_up_to_date_p
15861 /* Don't use try_window_reusing_current_matrix in this case
15862 because a window scroll function can have changed the
15863 buffer. */
15864 || !NILP (Vwindow_scroll_functions)
15865 || MINI_WINDOW_P (w)
15866 || !(used_current_matrix_p
15867 = try_window_reusing_current_matrix (w)))
15868 {
15869 IF_DEBUG (debug_method_add (w, "1"));
15870 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15871 /* -1 means we need to scroll.
15872 0 means we need new matrices, but fonts_changed_p
15873 is set in that case, so we will detect it below. */
15874 goto try_to_scroll;
15875 }
15876
15877 if (fonts_changed_p)
15878 goto need_larger_matrices;
15879
15880 if (w->cursor.vpos >= 0)
15881 {
15882 if (!just_this_one_p
15883 || current_buffer->clip_changed
15884 || BEG_UNCHANGED < CHARPOS (startp))
15885 /* Forget any recorded base line for line number display. */
15886 wset_base_line_number (w, Qnil);
15887
15888 if (!cursor_row_fully_visible_p (w, 1, 0))
15889 {
15890 clear_glyph_matrix (w->desired_matrix);
15891 last_line_misfit = 1;
15892 }
15893 /* Drop through and scroll. */
15894 else
15895 goto done;
15896 }
15897 else
15898 clear_glyph_matrix (w->desired_matrix);
15899 }
15900
15901 try_to_scroll:
15902
15903 w->last_modified = 0;
15904 w->last_overlay_modified = 0;
15905
15906 /* Redisplay the mode line. Select the buffer properly for that. */
15907 if (!update_mode_line)
15908 {
15909 update_mode_line = 1;
15910 w->update_mode_line = 1;
15911 }
15912
15913 /* Try to scroll by specified few lines. */
15914 if ((scroll_conservatively
15915 || emacs_scroll_step
15916 || temp_scroll_step
15917 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15918 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15919 && CHARPOS (startp) >= BEGV
15920 && CHARPOS (startp) <= ZV)
15921 {
15922 /* The function returns -1 if new fonts were loaded, 1 if
15923 successful, 0 if not successful. */
15924 int ss = try_scrolling (window, just_this_one_p,
15925 scroll_conservatively,
15926 emacs_scroll_step,
15927 temp_scroll_step, last_line_misfit);
15928 switch (ss)
15929 {
15930 case SCROLLING_SUCCESS:
15931 goto done;
15932
15933 case SCROLLING_NEED_LARGER_MATRICES:
15934 goto need_larger_matrices;
15935
15936 case SCROLLING_FAILED:
15937 break;
15938
15939 default:
15940 emacs_abort ();
15941 }
15942 }
15943
15944 /* Finally, just choose a place to start which positions point
15945 according to user preferences. */
15946
15947 recenter:
15948
15949 #ifdef GLYPH_DEBUG
15950 debug_method_add (w, "recenter");
15951 #endif
15952
15953 /* w->vscroll = 0; */
15954
15955 /* Forget any previously recorded base line for line number display. */
15956 if (!buffer_unchanged_p)
15957 wset_base_line_number (w, Qnil);
15958
15959 /* Determine the window start relative to point. */
15960 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15961 it.current_y = it.last_visible_y;
15962 if (centering_position < 0)
15963 {
15964 int margin =
15965 scroll_margin > 0
15966 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15967 : 0;
15968 ptrdiff_t margin_pos = CHARPOS (startp);
15969 Lisp_Object aggressive;
15970 int scrolling_up;
15971
15972 /* If there is a scroll margin at the top of the window, find
15973 its character position. */
15974 if (margin
15975 /* Cannot call start_display if startp is not in the
15976 accessible region of the buffer. This can happen when we
15977 have just switched to a different buffer and/or changed
15978 its restriction. In that case, startp is initialized to
15979 the character position 1 (BEGV) because we did not yet
15980 have chance to display the buffer even once. */
15981 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15982 {
15983 struct it it1;
15984 void *it1data = NULL;
15985
15986 SAVE_IT (it1, it, it1data);
15987 start_display (&it1, w, startp);
15988 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15989 margin_pos = IT_CHARPOS (it1);
15990 RESTORE_IT (&it, &it, it1data);
15991 }
15992 scrolling_up = PT > margin_pos;
15993 aggressive =
15994 scrolling_up
15995 ? BVAR (current_buffer, scroll_up_aggressively)
15996 : BVAR (current_buffer, scroll_down_aggressively);
15997
15998 if (!MINI_WINDOW_P (w)
15999 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16000 {
16001 int pt_offset = 0;
16002
16003 /* Setting scroll-conservatively overrides
16004 scroll-*-aggressively. */
16005 if (!scroll_conservatively && NUMBERP (aggressive))
16006 {
16007 double float_amount = XFLOATINT (aggressive);
16008
16009 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16010 if (pt_offset == 0 && float_amount > 0)
16011 pt_offset = 1;
16012 if (pt_offset && margin > 0)
16013 margin -= 1;
16014 }
16015 /* Compute how much to move the window start backward from
16016 point so that point will be displayed where the user
16017 wants it. */
16018 if (scrolling_up)
16019 {
16020 centering_position = it.last_visible_y;
16021 if (pt_offset)
16022 centering_position -= pt_offset;
16023 centering_position -=
16024 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16025 + WINDOW_HEADER_LINE_HEIGHT (w);
16026 /* Don't let point enter the scroll margin near top of
16027 the window. */
16028 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16029 centering_position = margin * FRAME_LINE_HEIGHT (f);
16030 }
16031 else
16032 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16033 }
16034 else
16035 /* Set the window start half the height of the window backward
16036 from point. */
16037 centering_position = window_box_height (w) / 2;
16038 }
16039 move_it_vertically_backward (&it, centering_position);
16040
16041 eassert (IT_CHARPOS (it) >= BEGV);
16042
16043 /* The function move_it_vertically_backward may move over more
16044 than the specified y-distance. If it->w is small, e.g. a
16045 mini-buffer window, we may end up in front of the window's
16046 display area. Start displaying at the start of the line
16047 containing PT in this case. */
16048 if (it.current_y <= 0)
16049 {
16050 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16051 move_it_vertically_backward (&it, 0);
16052 it.current_y = 0;
16053 }
16054
16055 it.current_x = it.hpos = 0;
16056
16057 /* Set the window start position here explicitly, to avoid an
16058 infinite loop in case the functions in window-scroll-functions
16059 get errors. */
16060 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16061
16062 /* Run scroll hooks. */
16063 startp = run_window_scroll_functions (window, it.current.pos);
16064
16065 /* Redisplay the window. */
16066 if (!current_matrix_up_to_date_p
16067 || windows_or_buffers_changed
16068 || cursor_type_changed
16069 /* Don't use try_window_reusing_current_matrix in this case
16070 because it can have changed the buffer. */
16071 || !NILP (Vwindow_scroll_functions)
16072 || !just_this_one_p
16073 || MINI_WINDOW_P (w)
16074 || !(used_current_matrix_p
16075 = try_window_reusing_current_matrix (w)))
16076 try_window (window, startp, 0);
16077
16078 /* If new fonts have been loaded (due to fontsets), give up. We
16079 have to start a new redisplay since we need to re-adjust glyph
16080 matrices. */
16081 if (fonts_changed_p)
16082 goto need_larger_matrices;
16083
16084 /* If cursor did not appear assume that the middle of the window is
16085 in the first line of the window. Do it again with the next line.
16086 (Imagine a window of height 100, displaying two lines of height
16087 60. Moving back 50 from it->last_visible_y will end in the first
16088 line.) */
16089 if (w->cursor.vpos < 0)
16090 {
16091 if (!NILP (w->window_end_valid)
16092 && PT >= Z - XFASTINT (w->window_end_pos))
16093 {
16094 clear_glyph_matrix (w->desired_matrix);
16095 move_it_by_lines (&it, 1);
16096 try_window (window, it.current.pos, 0);
16097 }
16098 else if (PT < IT_CHARPOS (it))
16099 {
16100 clear_glyph_matrix (w->desired_matrix);
16101 move_it_by_lines (&it, -1);
16102 try_window (window, it.current.pos, 0);
16103 }
16104 else
16105 {
16106 /* Not much we can do about it. */
16107 }
16108 }
16109
16110 /* Consider the following case: Window starts at BEGV, there is
16111 invisible, intangible text at BEGV, so that display starts at
16112 some point START > BEGV. It can happen that we are called with
16113 PT somewhere between BEGV and START. Try to handle that case. */
16114 if (w->cursor.vpos < 0)
16115 {
16116 struct glyph_row *row = w->current_matrix->rows;
16117 if (row->mode_line_p)
16118 ++row;
16119 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16120 }
16121
16122 if (!cursor_row_fully_visible_p (w, 0, 0))
16123 {
16124 /* If vscroll is enabled, disable it and try again. */
16125 if (w->vscroll)
16126 {
16127 w->vscroll = 0;
16128 clear_glyph_matrix (w->desired_matrix);
16129 goto recenter;
16130 }
16131
16132 /* Users who set scroll-conservatively to a large number want
16133 point just above/below the scroll margin. If we ended up
16134 with point's row partially visible, move the window start to
16135 make that row fully visible and out of the margin. */
16136 if (scroll_conservatively > SCROLL_LIMIT)
16137 {
16138 int margin =
16139 scroll_margin > 0
16140 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16141 : 0;
16142 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16143
16144 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16145 clear_glyph_matrix (w->desired_matrix);
16146 if (1 == try_window (window, it.current.pos,
16147 TRY_WINDOW_CHECK_MARGINS))
16148 goto done;
16149 }
16150
16151 /* If centering point failed to make the whole line visible,
16152 put point at the top instead. That has to make the whole line
16153 visible, if it can be done. */
16154 if (centering_position == 0)
16155 goto done;
16156
16157 clear_glyph_matrix (w->desired_matrix);
16158 centering_position = 0;
16159 goto recenter;
16160 }
16161
16162 done:
16163
16164 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16165 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16166 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16167
16168 /* Display the mode line, if we must. */
16169 if ((update_mode_line
16170 /* If window not full width, must redo its mode line
16171 if (a) the window to its side is being redone and
16172 (b) we do a frame-based redisplay. This is a consequence
16173 of how inverted lines are drawn in frame-based redisplay. */
16174 || (!just_this_one_p
16175 && !FRAME_WINDOW_P (f)
16176 && !WINDOW_FULL_WIDTH_P (w))
16177 /* Line number to display. */
16178 || INTEGERP (w->base_line_pos)
16179 /* Column number is displayed and different from the one displayed. */
16180 || (!NILP (w->column_number_displayed)
16181 && (XFASTINT (w->column_number_displayed) != current_column ())))
16182 /* This means that the window has a mode line. */
16183 && (WINDOW_WANTS_MODELINE_P (w)
16184 || WINDOW_WANTS_HEADER_LINE_P (w)))
16185 {
16186 display_mode_lines (w);
16187
16188 /* If mode line height has changed, arrange for a thorough
16189 immediate redisplay using the correct mode line height. */
16190 if (WINDOW_WANTS_MODELINE_P (w)
16191 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16192 {
16193 fonts_changed_p = 1;
16194 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16195 = DESIRED_MODE_LINE_HEIGHT (w);
16196 }
16197
16198 /* If header line height has changed, arrange for a thorough
16199 immediate redisplay using the correct header line height. */
16200 if (WINDOW_WANTS_HEADER_LINE_P (w)
16201 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16202 {
16203 fonts_changed_p = 1;
16204 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16205 = DESIRED_HEADER_LINE_HEIGHT (w);
16206 }
16207
16208 if (fonts_changed_p)
16209 goto need_larger_matrices;
16210 }
16211
16212 if (!line_number_displayed
16213 && !BUFFERP (w->base_line_pos))
16214 {
16215 wset_base_line_pos (w, Qnil);
16216 wset_base_line_number (w, Qnil);
16217 }
16218
16219 finish_menu_bars:
16220
16221 /* When we reach a frame's selected window, redo the frame's menu bar. */
16222 if (update_mode_line
16223 && EQ (FRAME_SELECTED_WINDOW (f), window))
16224 {
16225 int redisplay_menu_p = 0;
16226
16227 if (FRAME_WINDOW_P (f))
16228 {
16229 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16230 || defined (HAVE_NS) || defined (USE_GTK)
16231 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16232 #else
16233 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16234 #endif
16235 }
16236 else
16237 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16238
16239 if (redisplay_menu_p)
16240 display_menu_bar (w);
16241
16242 #ifdef HAVE_WINDOW_SYSTEM
16243 if (FRAME_WINDOW_P (f))
16244 {
16245 #if defined (USE_GTK) || defined (HAVE_NS)
16246 if (FRAME_EXTERNAL_TOOL_BAR (f))
16247 redisplay_tool_bar (f);
16248 #else
16249 if (WINDOWP (f->tool_bar_window)
16250 && (FRAME_TOOL_BAR_LINES (f) > 0
16251 || !NILP (Vauto_resize_tool_bars))
16252 && redisplay_tool_bar (f))
16253 ignore_mouse_drag_p = 1;
16254 #endif
16255 }
16256 #endif
16257 }
16258
16259 #ifdef HAVE_WINDOW_SYSTEM
16260 if (FRAME_WINDOW_P (f)
16261 && update_window_fringes (w, (just_this_one_p
16262 || (!used_current_matrix_p && !overlay_arrow_seen)
16263 || w->pseudo_window_p)))
16264 {
16265 update_begin (f);
16266 block_input ();
16267 if (draw_window_fringes (w, 1))
16268 x_draw_vertical_border (w);
16269 unblock_input ();
16270 update_end (f);
16271 }
16272 #endif /* HAVE_WINDOW_SYSTEM */
16273
16274 /* We go to this label, with fonts_changed_p set,
16275 if it is necessary to try again using larger glyph matrices.
16276 We have to redeem the scroll bar even in this case,
16277 because the loop in redisplay_internal expects that. */
16278 need_larger_matrices:
16279 ;
16280 finish_scroll_bars:
16281
16282 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16283 {
16284 /* Set the thumb's position and size. */
16285 set_vertical_scroll_bar (w);
16286
16287 /* Note that we actually used the scroll bar attached to this
16288 window, so it shouldn't be deleted at the end of redisplay. */
16289 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16290 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16291 }
16292
16293 /* Restore current_buffer and value of point in it. The window
16294 update may have changed the buffer, so first make sure `opoint'
16295 is still valid (Bug#6177). */
16296 if (CHARPOS (opoint) < BEGV)
16297 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16298 else if (CHARPOS (opoint) > ZV)
16299 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16300 else
16301 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16302
16303 set_buffer_internal_1 (old);
16304 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16305 shorter. This can be caused by log truncation in *Messages*. */
16306 if (CHARPOS (lpoint) <= ZV)
16307 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16308
16309 unbind_to (count, Qnil);
16310 }
16311
16312
16313 /* Build the complete desired matrix of WINDOW with a window start
16314 buffer position POS.
16315
16316 Value is 1 if successful. It is zero if fonts were loaded during
16317 redisplay which makes re-adjusting glyph matrices necessary, and -1
16318 if point would appear in the scroll margins.
16319 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16320 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16321 set in FLAGS.) */
16322
16323 int
16324 try_window (Lisp_Object window, struct text_pos pos, int flags)
16325 {
16326 struct window *w = XWINDOW (window);
16327 struct it it;
16328 struct glyph_row *last_text_row = NULL;
16329 struct frame *f = XFRAME (w->frame);
16330
16331 /* Make POS the new window start. */
16332 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16333
16334 /* Mark cursor position as unknown. No overlay arrow seen. */
16335 w->cursor.vpos = -1;
16336 overlay_arrow_seen = 0;
16337
16338 /* Initialize iterator and info to start at POS. */
16339 start_display (&it, w, pos);
16340
16341 /* Display all lines of W. */
16342 while (it.current_y < it.last_visible_y)
16343 {
16344 if (display_line (&it))
16345 last_text_row = it.glyph_row - 1;
16346 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16347 return 0;
16348 }
16349
16350 /* Don't let the cursor end in the scroll margins. */
16351 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16352 && !MINI_WINDOW_P (w))
16353 {
16354 int this_scroll_margin;
16355
16356 if (scroll_margin > 0)
16357 {
16358 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16359 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16360 }
16361 else
16362 this_scroll_margin = 0;
16363
16364 if ((w->cursor.y >= 0 /* not vscrolled */
16365 && w->cursor.y < this_scroll_margin
16366 && CHARPOS (pos) > BEGV
16367 && IT_CHARPOS (it) < ZV)
16368 /* rms: considering make_cursor_line_fully_visible_p here
16369 seems to give wrong results. We don't want to recenter
16370 when the last line is partly visible, we want to allow
16371 that case to be handled in the usual way. */
16372 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16373 {
16374 w->cursor.vpos = -1;
16375 clear_glyph_matrix (w->desired_matrix);
16376 return -1;
16377 }
16378 }
16379
16380 /* If bottom moved off end of frame, change mode line percentage. */
16381 if (XFASTINT (w->window_end_pos) <= 0
16382 && Z != IT_CHARPOS (it))
16383 w->update_mode_line = 1;
16384
16385 /* Set window_end_pos to the offset of the last character displayed
16386 on the window from the end of current_buffer. Set
16387 window_end_vpos to its row number. */
16388 if (last_text_row)
16389 {
16390 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16391 w->window_end_bytepos
16392 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16393 wset_window_end_pos
16394 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16395 wset_window_end_vpos
16396 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16397 eassert
16398 (MATRIX_ROW (w->desired_matrix,
16399 XFASTINT (w->window_end_vpos))->displays_text_p);
16400 }
16401 else
16402 {
16403 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16404 wset_window_end_pos (w, make_number (Z - ZV));
16405 wset_window_end_vpos (w, make_number (0));
16406 }
16407
16408 /* But that is not valid info until redisplay finishes. */
16409 wset_window_end_valid (w, Qnil);
16410 return 1;
16411 }
16412
16413
16414 \f
16415 /************************************************************************
16416 Window redisplay reusing current matrix when buffer has not changed
16417 ************************************************************************/
16418
16419 /* Try redisplay of window W showing an unchanged buffer with a
16420 different window start than the last time it was displayed by
16421 reusing its current matrix. Value is non-zero if successful.
16422 W->start is the new window start. */
16423
16424 static int
16425 try_window_reusing_current_matrix (struct window *w)
16426 {
16427 struct frame *f = XFRAME (w->frame);
16428 struct glyph_row *bottom_row;
16429 struct it it;
16430 struct run run;
16431 struct text_pos start, new_start;
16432 int nrows_scrolled, i;
16433 struct glyph_row *last_text_row;
16434 struct glyph_row *last_reused_text_row;
16435 struct glyph_row *start_row;
16436 int start_vpos, min_y, max_y;
16437
16438 #ifdef GLYPH_DEBUG
16439 if (inhibit_try_window_reusing)
16440 return 0;
16441 #endif
16442
16443 if (/* This function doesn't handle terminal frames. */
16444 !FRAME_WINDOW_P (f)
16445 /* Don't try to reuse the display if windows have been split
16446 or such. */
16447 || windows_or_buffers_changed
16448 || cursor_type_changed)
16449 return 0;
16450
16451 /* Can't do this if region may have changed. */
16452 if ((!NILP (Vtransient_mark_mode)
16453 && !NILP (BVAR (current_buffer, mark_active)))
16454 || !NILP (w->region_showing)
16455 || !NILP (Vshow_trailing_whitespace))
16456 return 0;
16457
16458 /* If top-line visibility has changed, give up. */
16459 if (WINDOW_WANTS_HEADER_LINE_P (w)
16460 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16461 return 0;
16462
16463 /* Give up if old or new display is scrolled vertically. We could
16464 make this function handle this, but right now it doesn't. */
16465 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16466 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16467 return 0;
16468
16469 /* The variable new_start now holds the new window start. The old
16470 start `start' can be determined from the current matrix. */
16471 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16472 start = start_row->minpos;
16473 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16474
16475 /* Clear the desired matrix for the display below. */
16476 clear_glyph_matrix (w->desired_matrix);
16477
16478 if (CHARPOS (new_start) <= CHARPOS (start))
16479 {
16480 /* Don't use this method if the display starts with an ellipsis
16481 displayed for invisible text. It's not easy to handle that case
16482 below, and it's certainly not worth the effort since this is
16483 not a frequent case. */
16484 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16485 return 0;
16486
16487 IF_DEBUG (debug_method_add (w, "twu1"));
16488
16489 /* Display up to a row that can be reused. The variable
16490 last_text_row is set to the last row displayed that displays
16491 text. Note that it.vpos == 0 if or if not there is a
16492 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16493 start_display (&it, w, new_start);
16494 w->cursor.vpos = -1;
16495 last_text_row = last_reused_text_row = NULL;
16496
16497 while (it.current_y < it.last_visible_y
16498 && !fonts_changed_p)
16499 {
16500 /* If we have reached into the characters in the START row,
16501 that means the line boundaries have changed. So we
16502 can't start copying with the row START. Maybe it will
16503 work to start copying with the following row. */
16504 while (IT_CHARPOS (it) > CHARPOS (start))
16505 {
16506 /* Advance to the next row as the "start". */
16507 start_row++;
16508 start = start_row->minpos;
16509 /* If there are no more rows to try, or just one, give up. */
16510 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16511 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16512 || CHARPOS (start) == ZV)
16513 {
16514 clear_glyph_matrix (w->desired_matrix);
16515 return 0;
16516 }
16517
16518 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16519 }
16520 /* If we have reached alignment, we can copy the rest of the
16521 rows. */
16522 if (IT_CHARPOS (it) == CHARPOS (start)
16523 /* Don't accept "alignment" inside a display vector,
16524 since start_row could have started in the middle of
16525 that same display vector (thus their character
16526 positions match), and we have no way of telling if
16527 that is the case. */
16528 && it.current.dpvec_index < 0)
16529 break;
16530
16531 if (display_line (&it))
16532 last_text_row = it.glyph_row - 1;
16533
16534 }
16535
16536 /* A value of current_y < last_visible_y means that we stopped
16537 at the previous window start, which in turn means that we
16538 have at least one reusable row. */
16539 if (it.current_y < it.last_visible_y)
16540 {
16541 struct glyph_row *row;
16542
16543 /* IT.vpos always starts from 0; it counts text lines. */
16544 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16545
16546 /* Find PT if not already found in the lines displayed. */
16547 if (w->cursor.vpos < 0)
16548 {
16549 int dy = it.current_y - start_row->y;
16550
16551 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16552 row = row_containing_pos (w, PT, row, NULL, dy);
16553 if (row)
16554 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16555 dy, nrows_scrolled);
16556 else
16557 {
16558 clear_glyph_matrix (w->desired_matrix);
16559 return 0;
16560 }
16561 }
16562
16563 /* Scroll the display. Do it before the current matrix is
16564 changed. The problem here is that update has not yet
16565 run, i.e. part of the current matrix is not up to date.
16566 scroll_run_hook will clear the cursor, and use the
16567 current matrix to get the height of the row the cursor is
16568 in. */
16569 run.current_y = start_row->y;
16570 run.desired_y = it.current_y;
16571 run.height = it.last_visible_y - it.current_y;
16572
16573 if (run.height > 0 && run.current_y != run.desired_y)
16574 {
16575 update_begin (f);
16576 FRAME_RIF (f)->update_window_begin_hook (w);
16577 FRAME_RIF (f)->clear_window_mouse_face (w);
16578 FRAME_RIF (f)->scroll_run_hook (w, &run);
16579 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16580 update_end (f);
16581 }
16582
16583 /* Shift current matrix down by nrows_scrolled lines. */
16584 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16585 rotate_matrix (w->current_matrix,
16586 start_vpos,
16587 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16588 nrows_scrolled);
16589
16590 /* Disable lines that must be updated. */
16591 for (i = 0; i < nrows_scrolled; ++i)
16592 (start_row + i)->enabled_p = 0;
16593
16594 /* Re-compute Y positions. */
16595 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16596 max_y = it.last_visible_y;
16597 for (row = start_row + nrows_scrolled;
16598 row < bottom_row;
16599 ++row)
16600 {
16601 row->y = it.current_y;
16602 row->visible_height = row->height;
16603
16604 if (row->y < min_y)
16605 row->visible_height -= min_y - row->y;
16606 if (row->y + row->height > max_y)
16607 row->visible_height -= row->y + row->height - max_y;
16608 if (row->fringe_bitmap_periodic_p)
16609 row->redraw_fringe_bitmaps_p = 1;
16610
16611 it.current_y += row->height;
16612
16613 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16614 last_reused_text_row = row;
16615 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16616 break;
16617 }
16618
16619 /* Disable lines in the current matrix which are now
16620 below the window. */
16621 for (++row; row < bottom_row; ++row)
16622 row->enabled_p = row->mode_line_p = 0;
16623 }
16624
16625 /* Update window_end_pos etc.; last_reused_text_row is the last
16626 reused row from the current matrix containing text, if any.
16627 The value of last_text_row is the last displayed line
16628 containing text. */
16629 if (last_reused_text_row)
16630 {
16631 w->window_end_bytepos
16632 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16633 wset_window_end_pos
16634 (w, make_number (Z
16635 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16636 wset_window_end_vpos
16637 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16638 w->current_matrix)));
16639 }
16640 else if (last_text_row)
16641 {
16642 w->window_end_bytepos
16643 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16644 wset_window_end_pos
16645 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16646 wset_window_end_vpos
16647 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16648 w->desired_matrix)));
16649 }
16650 else
16651 {
16652 /* This window must be completely empty. */
16653 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16654 wset_window_end_pos (w, make_number (Z - ZV));
16655 wset_window_end_vpos (w, make_number (0));
16656 }
16657 wset_window_end_valid (w, Qnil);
16658
16659 /* Update hint: don't try scrolling again in update_window. */
16660 w->desired_matrix->no_scrolling_p = 1;
16661
16662 #ifdef GLYPH_DEBUG
16663 debug_method_add (w, "try_window_reusing_current_matrix 1");
16664 #endif
16665 return 1;
16666 }
16667 else if (CHARPOS (new_start) > CHARPOS (start))
16668 {
16669 struct glyph_row *pt_row, *row;
16670 struct glyph_row *first_reusable_row;
16671 struct glyph_row *first_row_to_display;
16672 int dy;
16673 int yb = window_text_bottom_y (w);
16674
16675 /* Find the row starting at new_start, if there is one. Don't
16676 reuse a partially visible line at the end. */
16677 first_reusable_row = start_row;
16678 while (first_reusable_row->enabled_p
16679 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16680 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16681 < CHARPOS (new_start)))
16682 ++first_reusable_row;
16683
16684 /* Give up if there is no row to reuse. */
16685 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16686 || !first_reusable_row->enabled_p
16687 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16688 != CHARPOS (new_start)))
16689 return 0;
16690
16691 /* We can reuse fully visible rows beginning with
16692 first_reusable_row to the end of the window. Set
16693 first_row_to_display to the first row that cannot be reused.
16694 Set pt_row to the row containing point, if there is any. */
16695 pt_row = NULL;
16696 for (first_row_to_display = first_reusable_row;
16697 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16698 ++first_row_to_display)
16699 {
16700 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16701 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16702 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16703 && first_row_to_display->ends_at_zv_p
16704 && pt_row == NULL)))
16705 pt_row = first_row_to_display;
16706 }
16707
16708 /* Start displaying at the start of first_row_to_display. */
16709 eassert (first_row_to_display->y < yb);
16710 init_to_row_start (&it, w, first_row_to_display);
16711
16712 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16713 - start_vpos);
16714 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16715 - nrows_scrolled);
16716 it.current_y = (first_row_to_display->y - first_reusable_row->y
16717 + WINDOW_HEADER_LINE_HEIGHT (w));
16718
16719 /* Display lines beginning with first_row_to_display in the
16720 desired matrix. Set last_text_row to the last row displayed
16721 that displays text. */
16722 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16723 if (pt_row == NULL)
16724 w->cursor.vpos = -1;
16725 last_text_row = NULL;
16726 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16727 if (display_line (&it))
16728 last_text_row = it.glyph_row - 1;
16729
16730 /* If point is in a reused row, adjust y and vpos of the cursor
16731 position. */
16732 if (pt_row)
16733 {
16734 w->cursor.vpos -= nrows_scrolled;
16735 w->cursor.y -= first_reusable_row->y - start_row->y;
16736 }
16737
16738 /* Give up if point isn't in a row displayed or reused. (This
16739 also handles the case where w->cursor.vpos < nrows_scrolled
16740 after the calls to display_line, which can happen with scroll
16741 margins. See bug#1295.) */
16742 if (w->cursor.vpos < 0)
16743 {
16744 clear_glyph_matrix (w->desired_matrix);
16745 return 0;
16746 }
16747
16748 /* Scroll the display. */
16749 run.current_y = first_reusable_row->y;
16750 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16751 run.height = it.last_visible_y - run.current_y;
16752 dy = run.current_y - run.desired_y;
16753
16754 if (run.height)
16755 {
16756 update_begin (f);
16757 FRAME_RIF (f)->update_window_begin_hook (w);
16758 FRAME_RIF (f)->clear_window_mouse_face (w);
16759 FRAME_RIF (f)->scroll_run_hook (w, &run);
16760 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16761 update_end (f);
16762 }
16763
16764 /* Adjust Y positions of reused rows. */
16765 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16766 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16767 max_y = it.last_visible_y;
16768 for (row = first_reusable_row; row < first_row_to_display; ++row)
16769 {
16770 row->y -= dy;
16771 row->visible_height = row->height;
16772 if (row->y < min_y)
16773 row->visible_height -= min_y - row->y;
16774 if (row->y + row->height > max_y)
16775 row->visible_height -= row->y + row->height - max_y;
16776 if (row->fringe_bitmap_periodic_p)
16777 row->redraw_fringe_bitmaps_p = 1;
16778 }
16779
16780 /* Scroll the current matrix. */
16781 eassert (nrows_scrolled > 0);
16782 rotate_matrix (w->current_matrix,
16783 start_vpos,
16784 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16785 -nrows_scrolled);
16786
16787 /* Disable rows not reused. */
16788 for (row -= nrows_scrolled; row < bottom_row; ++row)
16789 row->enabled_p = 0;
16790
16791 /* Point may have moved to a different line, so we cannot assume that
16792 the previous cursor position is valid; locate the correct row. */
16793 if (pt_row)
16794 {
16795 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16796 row < bottom_row
16797 && PT >= MATRIX_ROW_END_CHARPOS (row)
16798 && !row->ends_at_zv_p;
16799 row++)
16800 {
16801 w->cursor.vpos++;
16802 w->cursor.y = row->y;
16803 }
16804 if (row < bottom_row)
16805 {
16806 /* Can't simply scan the row for point with
16807 bidi-reordered glyph rows. Let set_cursor_from_row
16808 figure out where to put the cursor, and if it fails,
16809 give up. */
16810 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16811 {
16812 if (!set_cursor_from_row (w, row, w->current_matrix,
16813 0, 0, 0, 0))
16814 {
16815 clear_glyph_matrix (w->desired_matrix);
16816 return 0;
16817 }
16818 }
16819 else
16820 {
16821 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16822 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16823
16824 for (; glyph < end
16825 && (!BUFFERP (glyph->object)
16826 || glyph->charpos < PT);
16827 glyph++)
16828 {
16829 w->cursor.hpos++;
16830 w->cursor.x += glyph->pixel_width;
16831 }
16832 }
16833 }
16834 }
16835
16836 /* Adjust window end. A null value of last_text_row means that
16837 the window end is in reused rows which in turn means that
16838 only its vpos can have changed. */
16839 if (last_text_row)
16840 {
16841 w->window_end_bytepos
16842 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16843 wset_window_end_pos
16844 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16845 wset_window_end_vpos
16846 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16847 w->desired_matrix)));
16848 }
16849 else
16850 {
16851 wset_window_end_vpos
16852 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16853 }
16854
16855 wset_window_end_valid (w, Qnil);
16856 w->desired_matrix->no_scrolling_p = 1;
16857
16858 #ifdef GLYPH_DEBUG
16859 debug_method_add (w, "try_window_reusing_current_matrix 2");
16860 #endif
16861 return 1;
16862 }
16863
16864 return 0;
16865 }
16866
16867
16868 \f
16869 /************************************************************************
16870 Window redisplay reusing current matrix when buffer has changed
16871 ************************************************************************/
16872
16873 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16874 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16875 ptrdiff_t *, ptrdiff_t *);
16876 static struct glyph_row *
16877 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16878 struct glyph_row *);
16879
16880
16881 /* Return the last row in MATRIX displaying text. If row START is
16882 non-null, start searching with that row. IT gives the dimensions
16883 of the display. Value is null if matrix is empty; otherwise it is
16884 a pointer to the row found. */
16885
16886 static struct glyph_row *
16887 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16888 struct glyph_row *start)
16889 {
16890 struct glyph_row *row, *row_found;
16891
16892 /* Set row_found to the last row in IT->w's current matrix
16893 displaying text. The loop looks funny but think of partially
16894 visible lines. */
16895 row_found = NULL;
16896 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16897 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16898 {
16899 eassert (row->enabled_p);
16900 row_found = row;
16901 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16902 break;
16903 ++row;
16904 }
16905
16906 return row_found;
16907 }
16908
16909
16910 /* Return the last row in the current matrix of W that is not affected
16911 by changes at the start of current_buffer that occurred since W's
16912 current matrix was built. Value is null if no such row exists.
16913
16914 BEG_UNCHANGED us the number of characters unchanged at the start of
16915 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16916 first changed character in current_buffer. Characters at positions <
16917 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16918 when the current matrix was built. */
16919
16920 static struct glyph_row *
16921 find_last_unchanged_at_beg_row (struct window *w)
16922 {
16923 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16924 struct glyph_row *row;
16925 struct glyph_row *row_found = NULL;
16926 int yb = window_text_bottom_y (w);
16927
16928 /* Find the last row displaying unchanged text. */
16929 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16930 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16931 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16932 ++row)
16933 {
16934 if (/* If row ends before first_changed_pos, it is unchanged,
16935 except in some case. */
16936 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16937 /* When row ends in ZV and we write at ZV it is not
16938 unchanged. */
16939 && !row->ends_at_zv_p
16940 /* When first_changed_pos is the end of a continued line,
16941 row is not unchanged because it may be no longer
16942 continued. */
16943 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16944 && (row->continued_p
16945 || row->exact_window_width_line_p))
16946 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16947 needs to be recomputed, so don't consider this row as
16948 unchanged. This happens when the last line was
16949 bidi-reordered and was killed immediately before this
16950 redisplay cycle. In that case, ROW->end stores the
16951 buffer position of the first visual-order character of
16952 the killed text, which is now beyond ZV. */
16953 && CHARPOS (row->end.pos) <= ZV)
16954 row_found = row;
16955
16956 /* Stop if last visible row. */
16957 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16958 break;
16959 }
16960
16961 return row_found;
16962 }
16963
16964
16965 /* Find the first glyph row in the current matrix of W that is not
16966 affected by changes at the end of current_buffer since the
16967 time W's current matrix was built.
16968
16969 Return in *DELTA the number of chars by which buffer positions in
16970 unchanged text at the end of current_buffer must be adjusted.
16971
16972 Return in *DELTA_BYTES the corresponding number of bytes.
16973
16974 Value is null if no such row exists, i.e. all rows are affected by
16975 changes. */
16976
16977 static struct glyph_row *
16978 find_first_unchanged_at_end_row (struct window *w,
16979 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16980 {
16981 struct glyph_row *row;
16982 struct glyph_row *row_found = NULL;
16983
16984 *delta = *delta_bytes = 0;
16985
16986 /* Display must not have been paused, otherwise the current matrix
16987 is not up to date. */
16988 eassert (!NILP (w->window_end_valid));
16989
16990 /* A value of window_end_pos >= END_UNCHANGED means that the window
16991 end is in the range of changed text. If so, there is no
16992 unchanged row at the end of W's current matrix. */
16993 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16994 return NULL;
16995
16996 /* Set row to the last row in W's current matrix displaying text. */
16997 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16998
16999 /* If matrix is entirely empty, no unchanged row exists. */
17000 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17001 {
17002 /* The value of row is the last glyph row in the matrix having a
17003 meaningful buffer position in it. The end position of row
17004 corresponds to window_end_pos. This allows us to translate
17005 buffer positions in the current matrix to current buffer
17006 positions for characters not in changed text. */
17007 ptrdiff_t Z_old =
17008 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17009 ptrdiff_t Z_BYTE_old =
17010 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17011 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17012 struct glyph_row *first_text_row
17013 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17014
17015 *delta = Z - Z_old;
17016 *delta_bytes = Z_BYTE - Z_BYTE_old;
17017
17018 /* Set last_unchanged_pos to the buffer position of the last
17019 character in the buffer that has not been changed. Z is the
17020 index + 1 of the last character in current_buffer, i.e. by
17021 subtracting END_UNCHANGED we get the index of the last
17022 unchanged character, and we have to add BEG to get its buffer
17023 position. */
17024 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17025 last_unchanged_pos_old = last_unchanged_pos - *delta;
17026
17027 /* Search backward from ROW for a row displaying a line that
17028 starts at a minimum position >= last_unchanged_pos_old. */
17029 for (; row > first_text_row; --row)
17030 {
17031 /* This used to abort, but it can happen.
17032 It is ok to just stop the search instead here. KFS. */
17033 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17034 break;
17035
17036 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17037 row_found = row;
17038 }
17039 }
17040
17041 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17042
17043 return row_found;
17044 }
17045
17046
17047 /* Make sure that glyph rows in the current matrix of window W
17048 reference the same glyph memory as corresponding rows in the
17049 frame's frame matrix. This function is called after scrolling W's
17050 current matrix on a terminal frame in try_window_id and
17051 try_window_reusing_current_matrix. */
17052
17053 static void
17054 sync_frame_with_window_matrix_rows (struct window *w)
17055 {
17056 struct frame *f = XFRAME (w->frame);
17057 struct glyph_row *window_row, *window_row_end, *frame_row;
17058
17059 /* Preconditions: W must be a leaf window and full-width. Its frame
17060 must have a frame matrix. */
17061 eassert (NILP (w->hchild) && NILP (w->vchild));
17062 eassert (WINDOW_FULL_WIDTH_P (w));
17063 eassert (!FRAME_WINDOW_P (f));
17064
17065 /* If W is a full-width window, glyph pointers in W's current matrix
17066 have, by definition, to be the same as glyph pointers in the
17067 corresponding frame matrix. Note that frame matrices have no
17068 marginal areas (see build_frame_matrix). */
17069 window_row = w->current_matrix->rows;
17070 window_row_end = window_row + w->current_matrix->nrows;
17071 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17072 while (window_row < window_row_end)
17073 {
17074 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17075 struct glyph *end = window_row->glyphs[LAST_AREA];
17076
17077 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17078 frame_row->glyphs[TEXT_AREA] = start;
17079 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17080 frame_row->glyphs[LAST_AREA] = end;
17081
17082 /* Disable frame rows whose corresponding window rows have
17083 been disabled in try_window_id. */
17084 if (!window_row->enabled_p)
17085 frame_row->enabled_p = 0;
17086
17087 ++window_row, ++frame_row;
17088 }
17089 }
17090
17091
17092 /* Find the glyph row in window W containing CHARPOS. Consider all
17093 rows between START and END (not inclusive). END null means search
17094 all rows to the end of the display area of W. Value is the row
17095 containing CHARPOS or null. */
17096
17097 struct glyph_row *
17098 row_containing_pos (struct window *w, ptrdiff_t charpos,
17099 struct glyph_row *start, struct glyph_row *end, int dy)
17100 {
17101 struct glyph_row *row = start;
17102 struct glyph_row *best_row = NULL;
17103 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17104 int last_y;
17105
17106 /* If we happen to start on a header-line, skip that. */
17107 if (row->mode_line_p)
17108 ++row;
17109
17110 if ((end && row >= end) || !row->enabled_p)
17111 return NULL;
17112
17113 last_y = window_text_bottom_y (w) - dy;
17114
17115 while (1)
17116 {
17117 /* Give up if we have gone too far. */
17118 if (end && row >= end)
17119 return NULL;
17120 /* This formerly returned if they were equal.
17121 I think that both quantities are of a "last plus one" type;
17122 if so, when they are equal, the row is within the screen. -- rms. */
17123 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17124 return NULL;
17125
17126 /* If it is in this row, return this row. */
17127 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17128 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17129 /* The end position of a row equals the start
17130 position of the next row. If CHARPOS is there, we
17131 would rather display it in the next line, except
17132 when this line ends in ZV. */
17133 && !row->ends_at_zv_p
17134 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17135 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17136 {
17137 struct glyph *g;
17138
17139 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17140 || (!best_row && !row->continued_p))
17141 return row;
17142 /* In bidi-reordered rows, there could be several rows
17143 occluding point, all of them belonging to the same
17144 continued line. We need to find the row which fits
17145 CHARPOS the best. */
17146 for (g = row->glyphs[TEXT_AREA];
17147 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17148 g++)
17149 {
17150 if (!STRINGP (g->object))
17151 {
17152 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17153 {
17154 mindif = eabs (g->charpos - charpos);
17155 best_row = row;
17156 /* Exact match always wins. */
17157 if (mindif == 0)
17158 return best_row;
17159 }
17160 }
17161 }
17162 }
17163 else if (best_row && !row->continued_p)
17164 return best_row;
17165 ++row;
17166 }
17167 }
17168
17169
17170 /* Try to redisplay window W by reusing its existing display. W's
17171 current matrix must be up to date when this function is called,
17172 i.e. window_end_valid must not be nil.
17173
17174 Value is
17175
17176 1 if display has been updated
17177 0 if otherwise unsuccessful
17178 -1 if redisplay with same window start is known not to succeed
17179
17180 The following steps are performed:
17181
17182 1. Find the last row in the current matrix of W that is not
17183 affected by changes at the start of current_buffer. If no such row
17184 is found, give up.
17185
17186 2. Find the first row in W's current matrix that is not affected by
17187 changes at the end of current_buffer. Maybe there is no such row.
17188
17189 3. Display lines beginning with the row + 1 found in step 1 to the
17190 row found in step 2 or, if step 2 didn't find a row, to the end of
17191 the window.
17192
17193 4. If cursor is not known to appear on the window, give up.
17194
17195 5. If display stopped at the row found in step 2, scroll the
17196 display and current matrix as needed.
17197
17198 6. Maybe display some lines at the end of W, if we must. This can
17199 happen under various circumstances, like a partially visible line
17200 becoming fully visible, or because newly displayed lines are displayed
17201 in smaller font sizes.
17202
17203 7. Update W's window end information. */
17204
17205 static int
17206 try_window_id (struct window *w)
17207 {
17208 struct frame *f = XFRAME (w->frame);
17209 struct glyph_matrix *current_matrix = w->current_matrix;
17210 struct glyph_matrix *desired_matrix = w->desired_matrix;
17211 struct glyph_row *last_unchanged_at_beg_row;
17212 struct glyph_row *first_unchanged_at_end_row;
17213 struct glyph_row *row;
17214 struct glyph_row *bottom_row;
17215 int bottom_vpos;
17216 struct it it;
17217 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17218 int dvpos, dy;
17219 struct text_pos start_pos;
17220 struct run run;
17221 int first_unchanged_at_end_vpos = 0;
17222 struct glyph_row *last_text_row, *last_text_row_at_end;
17223 struct text_pos start;
17224 ptrdiff_t first_changed_charpos, last_changed_charpos;
17225
17226 #ifdef GLYPH_DEBUG
17227 if (inhibit_try_window_id)
17228 return 0;
17229 #endif
17230
17231 /* This is handy for debugging. */
17232 #if 0
17233 #define GIVE_UP(X) \
17234 do { \
17235 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17236 return 0; \
17237 } while (0)
17238 #else
17239 #define GIVE_UP(X) return 0
17240 #endif
17241
17242 SET_TEXT_POS_FROM_MARKER (start, w->start);
17243
17244 /* Don't use this for mini-windows because these can show
17245 messages and mini-buffers, and we don't handle that here. */
17246 if (MINI_WINDOW_P (w))
17247 GIVE_UP (1);
17248
17249 /* This flag is used to prevent redisplay optimizations. */
17250 if (windows_or_buffers_changed || cursor_type_changed)
17251 GIVE_UP (2);
17252
17253 /* Verify that narrowing has not changed.
17254 Also verify that we were not told to prevent redisplay optimizations.
17255 It would be nice to further
17256 reduce the number of cases where this prevents try_window_id. */
17257 if (current_buffer->clip_changed
17258 || current_buffer->prevent_redisplay_optimizations_p)
17259 GIVE_UP (3);
17260
17261 /* Window must either use window-based redisplay or be full width. */
17262 if (!FRAME_WINDOW_P (f)
17263 && (!FRAME_LINE_INS_DEL_OK (f)
17264 || !WINDOW_FULL_WIDTH_P (w)))
17265 GIVE_UP (4);
17266
17267 /* Give up if point is known NOT to appear in W. */
17268 if (PT < CHARPOS (start))
17269 GIVE_UP (5);
17270
17271 /* Another way to prevent redisplay optimizations. */
17272 if (w->last_modified == 0)
17273 GIVE_UP (6);
17274
17275 /* Verify that window is not hscrolled. */
17276 if (w->hscroll != 0)
17277 GIVE_UP (7);
17278
17279 /* Verify that display wasn't paused. */
17280 if (NILP (w->window_end_valid))
17281 GIVE_UP (8);
17282
17283 /* Can't use this if highlighting a region because a cursor movement
17284 will do more than just set the cursor. */
17285 if (!NILP (Vtransient_mark_mode)
17286 && !NILP (BVAR (current_buffer, mark_active)))
17287 GIVE_UP (9);
17288
17289 /* Likewise if highlighting trailing whitespace. */
17290 if (!NILP (Vshow_trailing_whitespace))
17291 GIVE_UP (11);
17292
17293 /* Likewise if showing a region. */
17294 if (!NILP (w->region_showing))
17295 GIVE_UP (10);
17296
17297 /* Can't use this if overlay arrow position and/or string have
17298 changed. */
17299 if (overlay_arrows_changed_p ())
17300 GIVE_UP (12);
17301
17302 /* When word-wrap is on, adding a space to the first word of a
17303 wrapped line can change the wrap position, altering the line
17304 above it. It might be worthwhile to handle this more
17305 intelligently, but for now just redisplay from scratch. */
17306 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17307 GIVE_UP (21);
17308
17309 /* Under bidi reordering, adding or deleting a character in the
17310 beginning of a paragraph, before the first strong directional
17311 character, can change the base direction of the paragraph (unless
17312 the buffer specifies a fixed paragraph direction), which will
17313 require to redisplay the whole paragraph. It might be worthwhile
17314 to find the paragraph limits and widen the range of redisplayed
17315 lines to that, but for now just give up this optimization and
17316 redisplay from scratch. */
17317 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17318 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17319 GIVE_UP (22);
17320
17321 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17322 only if buffer has really changed. The reason is that the gap is
17323 initially at Z for freshly visited files. The code below would
17324 set end_unchanged to 0 in that case. */
17325 if (MODIFF > SAVE_MODIFF
17326 /* This seems to happen sometimes after saving a buffer. */
17327 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17328 {
17329 if (GPT - BEG < BEG_UNCHANGED)
17330 BEG_UNCHANGED = GPT - BEG;
17331 if (Z - GPT < END_UNCHANGED)
17332 END_UNCHANGED = Z - GPT;
17333 }
17334
17335 /* The position of the first and last character that has been changed. */
17336 first_changed_charpos = BEG + BEG_UNCHANGED;
17337 last_changed_charpos = Z - END_UNCHANGED;
17338
17339 /* If window starts after a line end, and the last change is in
17340 front of that newline, then changes don't affect the display.
17341 This case happens with stealth-fontification. Note that although
17342 the display is unchanged, glyph positions in the matrix have to
17343 be adjusted, of course. */
17344 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17345 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17346 && ((last_changed_charpos < CHARPOS (start)
17347 && CHARPOS (start) == BEGV)
17348 || (last_changed_charpos < CHARPOS (start) - 1
17349 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17350 {
17351 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17352 struct glyph_row *r0;
17353
17354 /* Compute how many chars/bytes have been added to or removed
17355 from the buffer. */
17356 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17357 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17358 Z_delta = Z - Z_old;
17359 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17360
17361 /* Give up if PT is not in the window. Note that it already has
17362 been checked at the start of try_window_id that PT is not in
17363 front of the window start. */
17364 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17365 GIVE_UP (13);
17366
17367 /* If window start is unchanged, we can reuse the whole matrix
17368 as is, after adjusting glyph positions. No need to compute
17369 the window end again, since its offset from Z hasn't changed. */
17370 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17371 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17372 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17373 /* PT must not be in a partially visible line. */
17374 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17375 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17376 {
17377 /* Adjust positions in the glyph matrix. */
17378 if (Z_delta || Z_delta_bytes)
17379 {
17380 struct glyph_row *r1
17381 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17382 increment_matrix_positions (w->current_matrix,
17383 MATRIX_ROW_VPOS (r0, current_matrix),
17384 MATRIX_ROW_VPOS (r1, current_matrix),
17385 Z_delta, Z_delta_bytes);
17386 }
17387
17388 /* Set the cursor. */
17389 row = row_containing_pos (w, PT, r0, NULL, 0);
17390 if (row)
17391 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17392 else
17393 emacs_abort ();
17394 return 1;
17395 }
17396 }
17397
17398 /* Handle the case that changes are all below what is displayed in
17399 the window, and that PT is in the window. This shortcut cannot
17400 be taken if ZV is visible in the window, and text has been added
17401 there that is visible in the window. */
17402 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17403 /* ZV is not visible in the window, or there are no
17404 changes at ZV, actually. */
17405 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17406 || first_changed_charpos == last_changed_charpos))
17407 {
17408 struct glyph_row *r0;
17409
17410 /* Give up if PT is not in the window. Note that it already has
17411 been checked at the start of try_window_id that PT is not in
17412 front of the window start. */
17413 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17414 GIVE_UP (14);
17415
17416 /* If window start is unchanged, we can reuse the whole matrix
17417 as is, without changing glyph positions since no text has
17418 been added/removed in front of the window end. */
17419 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17420 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17421 /* PT must not be in a partially visible line. */
17422 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17423 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17424 {
17425 /* We have to compute the window end anew since text
17426 could have been added/removed after it. */
17427 wset_window_end_pos
17428 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17429 w->window_end_bytepos
17430 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17431
17432 /* Set the cursor. */
17433 row = row_containing_pos (w, PT, r0, NULL, 0);
17434 if (row)
17435 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17436 else
17437 emacs_abort ();
17438 return 2;
17439 }
17440 }
17441
17442 /* Give up if window start is in the changed area.
17443
17444 The condition used to read
17445
17446 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17447
17448 but why that was tested escapes me at the moment. */
17449 if (CHARPOS (start) >= first_changed_charpos
17450 && CHARPOS (start) <= last_changed_charpos)
17451 GIVE_UP (15);
17452
17453 /* Check that window start agrees with the start of the first glyph
17454 row in its current matrix. Check this after we know the window
17455 start is not in changed text, otherwise positions would not be
17456 comparable. */
17457 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17458 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17459 GIVE_UP (16);
17460
17461 /* Give up if the window ends in strings. Overlay strings
17462 at the end are difficult to handle, so don't try. */
17463 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17464 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17465 GIVE_UP (20);
17466
17467 /* Compute the position at which we have to start displaying new
17468 lines. Some of the lines at the top of the window might be
17469 reusable because they are not displaying changed text. Find the
17470 last row in W's current matrix not affected by changes at the
17471 start of current_buffer. Value is null if changes start in the
17472 first line of window. */
17473 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17474 if (last_unchanged_at_beg_row)
17475 {
17476 /* Avoid starting to display in the middle of a character, a TAB
17477 for instance. This is easier than to set up the iterator
17478 exactly, and it's not a frequent case, so the additional
17479 effort wouldn't really pay off. */
17480 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17481 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17482 && last_unchanged_at_beg_row > w->current_matrix->rows)
17483 --last_unchanged_at_beg_row;
17484
17485 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17486 GIVE_UP (17);
17487
17488 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17489 GIVE_UP (18);
17490 start_pos = it.current.pos;
17491
17492 /* Start displaying new lines in the desired matrix at the same
17493 vpos we would use in the current matrix, i.e. below
17494 last_unchanged_at_beg_row. */
17495 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17496 current_matrix);
17497 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17498 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17499
17500 eassert (it.hpos == 0 && it.current_x == 0);
17501 }
17502 else
17503 {
17504 /* There are no reusable lines at the start of the window.
17505 Start displaying in the first text line. */
17506 start_display (&it, w, start);
17507 it.vpos = it.first_vpos;
17508 start_pos = it.current.pos;
17509 }
17510
17511 /* Find the first row that is not affected by changes at the end of
17512 the buffer. Value will be null if there is no unchanged row, in
17513 which case we must redisplay to the end of the window. delta
17514 will be set to the value by which buffer positions beginning with
17515 first_unchanged_at_end_row have to be adjusted due to text
17516 changes. */
17517 first_unchanged_at_end_row
17518 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17519 IF_DEBUG (debug_delta = delta);
17520 IF_DEBUG (debug_delta_bytes = delta_bytes);
17521
17522 /* Set stop_pos to the buffer position up to which we will have to
17523 display new lines. If first_unchanged_at_end_row != NULL, this
17524 is the buffer position of the start of the line displayed in that
17525 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17526 that we don't stop at a buffer position. */
17527 stop_pos = 0;
17528 if (first_unchanged_at_end_row)
17529 {
17530 eassert (last_unchanged_at_beg_row == NULL
17531 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17532
17533 /* If this is a continuation line, move forward to the next one
17534 that isn't. Changes in lines above affect this line.
17535 Caution: this may move first_unchanged_at_end_row to a row
17536 not displaying text. */
17537 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17538 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17539 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17540 < it.last_visible_y))
17541 ++first_unchanged_at_end_row;
17542
17543 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17544 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17545 >= it.last_visible_y))
17546 first_unchanged_at_end_row = NULL;
17547 else
17548 {
17549 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17550 + delta);
17551 first_unchanged_at_end_vpos
17552 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17553 eassert (stop_pos >= Z - END_UNCHANGED);
17554 }
17555 }
17556 else if (last_unchanged_at_beg_row == NULL)
17557 GIVE_UP (19);
17558
17559
17560 #ifdef GLYPH_DEBUG
17561
17562 /* Either there is no unchanged row at the end, or the one we have
17563 now displays text. This is a necessary condition for the window
17564 end pos calculation at the end of this function. */
17565 eassert (first_unchanged_at_end_row == NULL
17566 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17567
17568 debug_last_unchanged_at_beg_vpos
17569 = (last_unchanged_at_beg_row
17570 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17571 : -1);
17572 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17573
17574 #endif /* GLYPH_DEBUG */
17575
17576
17577 /* Display new lines. Set last_text_row to the last new line
17578 displayed which has text on it, i.e. might end up as being the
17579 line where the window_end_vpos is. */
17580 w->cursor.vpos = -1;
17581 last_text_row = NULL;
17582 overlay_arrow_seen = 0;
17583 while (it.current_y < it.last_visible_y
17584 && !fonts_changed_p
17585 && (first_unchanged_at_end_row == NULL
17586 || IT_CHARPOS (it) < stop_pos))
17587 {
17588 if (display_line (&it))
17589 last_text_row = it.glyph_row - 1;
17590 }
17591
17592 if (fonts_changed_p)
17593 return -1;
17594
17595
17596 /* Compute differences in buffer positions, y-positions etc. for
17597 lines reused at the bottom of the window. Compute what we can
17598 scroll. */
17599 if (first_unchanged_at_end_row
17600 /* No lines reused because we displayed everything up to the
17601 bottom of the window. */
17602 && it.current_y < it.last_visible_y)
17603 {
17604 dvpos = (it.vpos
17605 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17606 current_matrix));
17607 dy = it.current_y - first_unchanged_at_end_row->y;
17608 run.current_y = first_unchanged_at_end_row->y;
17609 run.desired_y = run.current_y + dy;
17610 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17611 }
17612 else
17613 {
17614 delta = delta_bytes = dvpos = dy
17615 = run.current_y = run.desired_y = run.height = 0;
17616 first_unchanged_at_end_row = NULL;
17617 }
17618 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17619
17620
17621 /* Find the cursor if not already found. We have to decide whether
17622 PT will appear on this window (it sometimes doesn't, but this is
17623 not a very frequent case.) This decision has to be made before
17624 the current matrix is altered. A value of cursor.vpos < 0 means
17625 that PT is either in one of the lines beginning at
17626 first_unchanged_at_end_row or below the window. Don't care for
17627 lines that might be displayed later at the window end; as
17628 mentioned, this is not a frequent case. */
17629 if (w->cursor.vpos < 0)
17630 {
17631 /* Cursor in unchanged rows at the top? */
17632 if (PT < CHARPOS (start_pos)
17633 && last_unchanged_at_beg_row)
17634 {
17635 row = row_containing_pos (w, PT,
17636 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17637 last_unchanged_at_beg_row + 1, 0);
17638 if (row)
17639 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17640 }
17641
17642 /* Start from first_unchanged_at_end_row looking for PT. */
17643 else if (first_unchanged_at_end_row)
17644 {
17645 row = row_containing_pos (w, PT - delta,
17646 first_unchanged_at_end_row, NULL, 0);
17647 if (row)
17648 set_cursor_from_row (w, row, w->current_matrix, delta,
17649 delta_bytes, dy, dvpos);
17650 }
17651
17652 /* Give up if cursor was not found. */
17653 if (w->cursor.vpos < 0)
17654 {
17655 clear_glyph_matrix (w->desired_matrix);
17656 return -1;
17657 }
17658 }
17659
17660 /* Don't let the cursor end in the scroll margins. */
17661 {
17662 int this_scroll_margin, cursor_height;
17663
17664 this_scroll_margin =
17665 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17666 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17667 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17668
17669 if ((w->cursor.y < this_scroll_margin
17670 && CHARPOS (start) > BEGV)
17671 /* Old redisplay didn't take scroll margin into account at the bottom,
17672 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17673 || (w->cursor.y + (make_cursor_line_fully_visible_p
17674 ? cursor_height + this_scroll_margin
17675 : 1)) > it.last_visible_y)
17676 {
17677 w->cursor.vpos = -1;
17678 clear_glyph_matrix (w->desired_matrix);
17679 return -1;
17680 }
17681 }
17682
17683 /* Scroll the display. Do it before changing the current matrix so
17684 that xterm.c doesn't get confused about where the cursor glyph is
17685 found. */
17686 if (dy && run.height)
17687 {
17688 update_begin (f);
17689
17690 if (FRAME_WINDOW_P (f))
17691 {
17692 FRAME_RIF (f)->update_window_begin_hook (w);
17693 FRAME_RIF (f)->clear_window_mouse_face (w);
17694 FRAME_RIF (f)->scroll_run_hook (w, &run);
17695 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17696 }
17697 else
17698 {
17699 /* Terminal frame. In this case, dvpos gives the number of
17700 lines to scroll by; dvpos < 0 means scroll up. */
17701 int from_vpos
17702 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17703 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17704 int end = (WINDOW_TOP_EDGE_LINE (w)
17705 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17706 + window_internal_height (w));
17707
17708 #if defined (HAVE_GPM) || defined (MSDOS)
17709 x_clear_window_mouse_face (w);
17710 #endif
17711 /* Perform the operation on the screen. */
17712 if (dvpos > 0)
17713 {
17714 /* Scroll last_unchanged_at_beg_row to the end of the
17715 window down dvpos lines. */
17716 set_terminal_window (f, end);
17717
17718 /* On dumb terminals delete dvpos lines at the end
17719 before inserting dvpos empty lines. */
17720 if (!FRAME_SCROLL_REGION_OK (f))
17721 ins_del_lines (f, end - dvpos, -dvpos);
17722
17723 /* Insert dvpos empty lines in front of
17724 last_unchanged_at_beg_row. */
17725 ins_del_lines (f, from, dvpos);
17726 }
17727 else if (dvpos < 0)
17728 {
17729 /* Scroll up last_unchanged_at_beg_vpos to the end of
17730 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17731 set_terminal_window (f, end);
17732
17733 /* Delete dvpos lines in front of
17734 last_unchanged_at_beg_vpos. ins_del_lines will set
17735 the cursor to the given vpos and emit |dvpos| delete
17736 line sequences. */
17737 ins_del_lines (f, from + dvpos, dvpos);
17738
17739 /* On a dumb terminal insert dvpos empty lines at the
17740 end. */
17741 if (!FRAME_SCROLL_REGION_OK (f))
17742 ins_del_lines (f, end + dvpos, -dvpos);
17743 }
17744
17745 set_terminal_window (f, 0);
17746 }
17747
17748 update_end (f);
17749 }
17750
17751 /* Shift reused rows of the current matrix to the right position.
17752 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17753 text. */
17754 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17755 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17756 if (dvpos < 0)
17757 {
17758 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17759 bottom_vpos, dvpos);
17760 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17761 bottom_vpos);
17762 }
17763 else if (dvpos > 0)
17764 {
17765 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17766 bottom_vpos, dvpos);
17767 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17768 first_unchanged_at_end_vpos + dvpos);
17769 }
17770
17771 /* For frame-based redisplay, make sure that current frame and window
17772 matrix are in sync with respect to glyph memory. */
17773 if (!FRAME_WINDOW_P (f))
17774 sync_frame_with_window_matrix_rows (w);
17775
17776 /* Adjust buffer positions in reused rows. */
17777 if (delta || delta_bytes)
17778 increment_matrix_positions (current_matrix,
17779 first_unchanged_at_end_vpos + dvpos,
17780 bottom_vpos, delta, delta_bytes);
17781
17782 /* Adjust Y positions. */
17783 if (dy)
17784 shift_glyph_matrix (w, current_matrix,
17785 first_unchanged_at_end_vpos + dvpos,
17786 bottom_vpos, dy);
17787
17788 if (first_unchanged_at_end_row)
17789 {
17790 first_unchanged_at_end_row += dvpos;
17791 if (first_unchanged_at_end_row->y >= it.last_visible_y
17792 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17793 first_unchanged_at_end_row = NULL;
17794 }
17795
17796 /* If scrolling up, there may be some lines to display at the end of
17797 the window. */
17798 last_text_row_at_end = NULL;
17799 if (dy < 0)
17800 {
17801 /* Scrolling up can leave for example a partially visible line
17802 at the end of the window to be redisplayed. */
17803 /* Set last_row to the glyph row in the current matrix where the
17804 window end line is found. It has been moved up or down in
17805 the matrix by dvpos. */
17806 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17807 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17808
17809 /* If last_row is the window end line, it should display text. */
17810 eassert (last_row->displays_text_p);
17811
17812 /* If window end line was partially visible before, begin
17813 displaying at that line. Otherwise begin displaying with the
17814 line following it. */
17815 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17816 {
17817 init_to_row_start (&it, w, last_row);
17818 it.vpos = last_vpos;
17819 it.current_y = last_row->y;
17820 }
17821 else
17822 {
17823 init_to_row_end (&it, w, last_row);
17824 it.vpos = 1 + last_vpos;
17825 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17826 ++last_row;
17827 }
17828
17829 /* We may start in a continuation line. If so, we have to
17830 get the right continuation_lines_width and current_x. */
17831 it.continuation_lines_width = last_row->continuation_lines_width;
17832 it.hpos = it.current_x = 0;
17833
17834 /* Display the rest of the lines at the window end. */
17835 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17836 while (it.current_y < it.last_visible_y
17837 && !fonts_changed_p)
17838 {
17839 /* Is it always sure that the display agrees with lines in
17840 the current matrix? I don't think so, so we mark rows
17841 displayed invalid in the current matrix by setting their
17842 enabled_p flag to zero. */
17843 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17844 if (display_line (&it))
17845 last_text_row_at_end = it.glyph_row - 1;
17846 }
17847 }
17848
17849 /* Update window_end_pos and window_end_vpos. */
17850 if (first_unchanged_at_end_row
17851 && !last_text_row_at_end)
17852 {
17853 /* Window end line if one of the preserved rows from the current
17854 matrix. Set row to the last row displaying text in current
17855 matrix starting at first_unchanged_at_end_row, after
17856 scrolling. */
17857 eassert (first_unchanged_at_end_row->displays_text_p);
17858 row = find_last_row_displaying_text (w->current_matrix, &it,
17859 first_unchanged_at_end_row);
17860 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17861
17862 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17863 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17864 wset_window_end_vpos
17865 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17866 eassert (w->window_end_bytepos >= 0);
17867 IF_DEBUG (debug_method_add (w, "A"));
17868 }
17869 else if (last_text_row_at_end)
17870 {
17871 wset_window_end_pos
17872 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17873 w->window_end_bytepos
17874 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17875 wset_window_end_vpos
17876 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17877 desired_matrix)));
17878 eassert (w->window_end_bytepos >= 0);
17879 IF_DEBUG (debug_method_add (w, "B"));
17880 }
17881 else if (last_text_row)
17882 {
17883 /* We have displayed either to the end of the window or at the
17884 end of the window, i.e. the last row with text is to be found
17885 in the desired matrix. */
17886 wset_window_end_pos
17887 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17888 w->window_end_bytepos
17889 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17890 wset_window_end_vpos
17891 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17892 eassert (w->window_end_bytepos >= 0);
17893 }
17894 else if (first_unchanged_at_end_row == NULL
17895 && last_text_row == NULL
17896 && last_text_row_at_end == NULL)
17897 {
17898 /* Displayed to end of window, but no line containing text was
17899 displayed. Lines were deleted at the end of the window. */
17900 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17901 int vpos = XFASTINT (w->window_end_vpos);
17902 struct glyph_row *current_row = current_matrix->rows + vpos;
17903 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17904
17905 for (row = NULL;
17906 row == NULL && vpos >= first_vpos;
17907 --vpos, --current_row, --desired_row)
17908 {
17909 if (desired_row->enabled_p)
17910 {
17911 if (desired_row->displays_text_p)
17912 row = desired_row;
17913 }
17914 else if (current_row->displays_text_p)
17915 row = current_row;
17916 }
17917
17918 eassert (row != NULL);
17919 wset_window_end_vpos (w, make_number (vpos + 1));
17920 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17921 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17922 eassert (w->window_end_bytepos >= 0);
17923 IF_DEBUG (debug_method_add (w, "C"));
17924 }
17925 else
17926 emacs_abort ();
17927
17928 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17929 debug_end_vpos = XFASTINT (w->window_end_vpos));
17930
17931 /* Record that display has not been completed. */
17932 wset_window_end_valid (w, Qnil);
17933 w->desired_matrix->no_scrolling_p = 1;
17934 return 3;
17935
17936 #undef GIVE_UP
17937 }
17938
17939
17940 \f
17941 /***********************************************************************
17942 More debugging support
17943 ***********************************************************************/
17944
17945 #ifdef GLYPH_DEBUG
17946
17947 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17948 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17949 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17950
17951
17952 /* Dump the contents of glyph matrix MATRIX on stderr.
17953
17954 GLYPHS 0 means don't show glyph contents.
17955 GLYPHS 1 means show glyphs in short form
17956 GLYPHS > 1 means show glyphs in long form. */
17957
17958 void
17959 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17960 {
17961 int i;
17962 for (i = 0; i < matrix->nrows; ++i)
17963 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17964 }
17965
17966
17967 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17968 the glyph row and area where the glyph comes from. */
17969
17970 void
17971 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17972 {
17973 if (glyph->type == CHAR_GLYPH)
17974 {
17975 fprintf (stderr,
17976 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17977 glyph - row->glyphs[TEXT_AREA],
17978 'C',
17979 glyph->charpos,
17980 (BUFFERP (glyph->object)
17981 ? 'B'
17982 : (STRINGP (glyph->object)
17983 ? 'S'
17984 : '-')),
17985 glyph->pixel_width,
17986 glyph->u.ch,
17987 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17988 ? glyph->u.ch
17989 : '.'),
17990 glyph->face_id,
17991 glyph->left_box_line_p,
17992 glyph->right_box_line_p);
17993 }
17994 else if (glyph->type == STRETCH_GLYPH)
17995 {
17996 fprintf (stderr,
17997 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17998 glyph - row->glyphs[TEXT_AREA],
17999 'S',
18000 glyph->charpos,
18001 (BUFFERP (glyph->object)
18002 ? 'B'
18003 : (STRINGP (glyph->object)
18004 ? 'S'
18005 : '-')),
18006 glyph->pixel_width,
18007 0,
18008 '.',
18009 glyph->face_id,
18010 glyph->left_box_line_p,
18011 glyph->right_box_line_p);
18012 }
18013 else if (glyph->type == IMAGE_GLYPH)
18014 {
18015 fprintf (stderr,
18016 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18017 glyph - row->glyphs[TEXT_AREA],
18018 'I',
18019 glyph->charpos,
18020 (BUFFERP (glyph->object)
18021 ? 'B'
18022 : (STRINGP (glyph->object)
18023 ? 'S'
18024 : '-')),
18025 glyph->pixel_width,
18026 glyph->u.img_id,
18027 '.',
18028 glyph->face_id,
18029 glyph->left_box_line_p,
18030 glyph->right_box_line_p);
18031 }
18032 else if (glyph->type == COMPOSITE_GLYPH)
18033 {
18034 fprintf (stderr,
18035 " %5td %4c %6"pI"d %c %3d 0x%05x",
18036 glyph - row->glyphs[TEXT_AREA],
18037 '+',
18038 glyph->charpos,
18039 (BUFFERP (glyph->object)
18040 ? 'B'
18041 : (STRINGP (glyph->object)
18042 ? 'S'
18043 : '-')),
18044 glyph->pixel_width,
18045 glyph->u.cmp.id);
18046 if (glyph->u.cmp.automatic)
18047 fprintf (stderr,
18048 "[%d-%d]",
18049 glyph->slice.cmp.from, glyph->slice.cmp.to);
18050 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18051 glyph->face_id,
18052 glyph->left_box_line_p,
18053 glyph->right_box_line_p);
18054 }
18055 }
18056
18057
18058 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18059 GLYPHS 0 means don't show glyph contents.
18060 GLYPHS 1 means show glyphs in short form
18061 GLYPHS > 1 means show glyphs in long form. */
18062
18063 void
18064 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18065 {
18066 if (glyphs != 1)
18067 {
18068 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18069 fprintf (stderr, "======================================================================\n");
18070
18071 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18072 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18073 vpos,
18074 MATRIX_ROW_START_CHARPOS (row),
18075 MATRIX_ROW_END_CHARPOS (row),
18076 row->used[TEXT_AREA],
18077 row->contains_overlapping_glyphs_p,
18078 row->enabled_p,
18079 row->truncated_on_left_p,
18080 row->truncated_on_right_p,
18081 row->continued_p,
18082 MATRIX_ROW_CONTINUATION_LINE_P (row),
18083 row->displays_text_p,
18084 row->ends_at_zv_p,
18085 row->fill_line_p,
18086 row->ends_in_middle_of_char_p,
18087 row->starts_in_middle_of_char_p,
18088 row->mouse_face_p,
18089 row->x,
18090 row->y,
18091 row->pixel_width,
18092 row->height,
18093 row->visible_height,
18094 row->ascent,
18095 row->phys_ascent);
18096 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18097 row->end.overlay_string_index,
18098 row->continuation_lines_width);
18099 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18100 CHARPOS (row->start.string_pos),
18101 CHARPOS (row->end.string_pos));
18102 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18103 row->end.dpvec_index);
18104 }
18105
18106 if (glyphs > 1)
18107 {
18108 int area;
18109
18110 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18111 {
18112 struct glyph *glyph = row->glyphs[area];
18113 struct glyph *glyph_end = glyph + row->used[area];
18114
18115 /* Glyph for a line end in text. */
18116 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18117 ++glyph_end;
18118
18119 if (glyph < glyph_end)
18120 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18121
18122 for (; glyph < glyph_end; ++glyph)
18123 dump_glyph (row, glyph, area);
18124 }
18125 }
18126 else if (glyphs == 1)
18127 {
18128 int area;
18129
18130 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18131 {
18132 char *s = alloca (row->used[area] + 1);
18133 int i;
18134
18135 for (i = 0; i < row->used[area]; ++i)
18136 {
18137 struct glyph *glyph = row->glyphs[area] + i;
18138 if (glyph->type == CHAR_GLYPH
18139 && glyph->u.ch < 0x80
18140 && glyph->u.ch >= ' ')
18141 s[i] = glyph->u.ch;
18142 else
18143 s[i] = '.';
18144 }
18145
18146 s[i] = '\0';
18147 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18148 }
18149 }
18150 }
18151
18152
18153 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18154 Sdump_glyph_matrix, 0, 1, "p",
18155 doc: /* Dump the current matrix of the selected window to stderr.
18156 Shows contents of glyph row structures. With non-nil
18157 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18158 glyphs in short form, otherwise show glyphs in long form. */)
18159 (Lisp_Object glyphs)
18160 {
18161 struct window *w = XWINDOW (selected_window);
18162 struct buffer *buffer = XBUFFER (w->buffer);
18163
18164 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18165 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18166 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18167 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18168 fprintf (stderr, "=============================================\n");
18169 dump_glyph_matrix (w->current_matrix,
18170 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18171 return Qnil;
18172 }
18173
18174
18175 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18176 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18177 (void)
18178 {
18179 struct frame *f = XFRAME (selected_frame);
18180 dump_glyph_matrix (f->current_matrix, 1);
18181 return Qnil;
18182 }
18183
18184
18185 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18186 doc: /* Dump glyph row ROW to stderr.
18187 GLYPH 0 means don't dump glyphs.
18188 GLYPH 1 means dump glyphs in short form.
18189 GLYPH > 1 or omitted means dump glyphs in long form. */)
18190 (Lisp_Object row, Lisp_Object glyphs)
18191 {
18192 struct glyph_matrix *matrix;
18193 EMACS_INT vpos;
18194
18195 CHECK_NUMBER (row);
18196 matrix = XWINDOW (selected_window)->current_matrix;
18197 vpos = XINT (row);
18198 if (vpos >= 0 && vpos < matrix->nrows)
18199 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18200 vpos,
18201 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18202 return Qnil;
18203 }
18204
18205
18206 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18207 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18208 GLYPH 0 means don't dump glyphs.
18209 GLYPH 1 means dump glyphs in short form.
18210 GLYPH > 1 or omitted means dump glyphs in long form. */)
18211 (Lisp_Object row, Lisp_Object glyphs)
18212 {
18213 struct frame *sf = SELECTED_FRAME ();
18214 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18215 EMACS_INT vpos;
18216
18217 CHECK_NUMBER (row);
18218 vpos = XINT (row);
18219 if (vpos >= 0 && vpos < m->nrows)
18220 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18221 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18222 return Qnil;
18223 }
18224
18225
18226 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18227 doc: /* Toggle tracing of redisplay.
18228 With ARG, turn tracing on if and only if ARG is positive. */)
18229 (Lisp_Object arg)
18230 {
18231 if (NILP (arg))
18232 trace_redisplay_p = !trace_redisplay_p;
18233 else
18234 {
18235 arg = Fprefix_numeric_value (arg);
18236 trace_redisplay_p = XINT (arg) > 0;
18237 }
18238
18239 return Qnil;
18240 }
18241
18242
18243 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18244 doc: /* Like `format', but print result to stderr.
18245 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18246 (ptrdiff_t nargs, Lisp_Object *args)
18247 {
18248 Lisp_Object s = Fformat (nargs, args);
18249 fprintf (stderr, "%s", SDATA (s));
18250 return Qnil;
18251 }
18252
18253 #endif /* GLYPH_DEBUG */
18254
18255
18256 \f
18257 /***********************************************************************
18258 Building Desired Matrix Rows
18259 ***********************************************************************/
18260
18261 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18262 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18263
18264 static struct glyph_row *
18265 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18266 {
18267 struct frame *f = XFRAME (WINDOW_FRAME (w));
18268 struct buffer *buffer = XBUFFER (w->buffer);
18269 struct buffer *old = current_buffer;
18270 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18271 int arrow_len = SCHARS (overlay_arrow_string);
18272 const unsigned char *arrow_end = arrow_string + arrow_len;
18273 const unsigned char *p;
18274 struct it it;
18275 int multibyte_p;
18276 int n_glyphs_before;
18277
18278 set_buffer_temp (buffer);
18279 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18280 it.glyph_row->used[TEXT_AREA] = 0;
18281 SET_TEXT_POS (it.position, 0, 0);
18282
18283 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18284 p = arrow_string;
18285 while (p < arrow_end)
18286 {
18287 Lisp_Object face, ilisp;
18288
18289 /* Get the next character. */
18290 if (multibyte_p)
18291 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18292 else
18293 {
18294 it.c = it.char_to_display = *p, it.len = 1;
18295 if (! ASCII_CHAR_P (it.c))
18296 it.char_to_display = BYTE8_TO_CHAR (it.c);
18297 }
18298 p += it.len;
18299
18300 /* Get its face. */
18301 ilisp = make_number (p - arrow_string);
18302 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18303 it.face_id = compute_char_face (f, it.char_to_display, face);
18304
18305 /* Compute its width, get its glyphs. */
18306 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18307 SET_TEXT_POS (it.position, -1, -1);
18308 PRODUCE_GLYPHS (&it);
18309
18310 /* If this character doesn't fit any more in the line, we have
18311 to remove some glyphs. */
18312 if (it.current_x > it.last_visible_x)
18313 {
18314 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18315 break;
18316 }
18317 }
18318
18319 set_buffer_temp (old);
18320 return it.glyph_row;
18321 }
18322
18323
18324 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18325 glyphs to insert is determined by produce_special_glyphs. */
18326
18327 static void
18328 insert_left_trunc_glyphs (struct it *it)
18329 {
18330 struct it truncate_it;
18331 struct glyph *from, *end, *to, *toend;
18332
18333 eassert (!FRAME_WINDOW_P (it->f)
18334 || (!it->glyph_row->reversed_p
18335 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18336 || (it->glyph_row->reversed_p
18337 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18338
18339 /* Get the truncation glyphs. */
18340 truncate_it = *it;
18341 truncate_it.current_x = 0;
18342 truncate_it.face_id = DEFAULT_FACE_ID;
18343 truncate_it.glyph_row = &scratch_glyph_row;
18344 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18345 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18346 truncate_it.object = make_number (0);
18347 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18348
18349 /* Overwrite glyphs from IT with truncation glyphs. */
18350 if (!it->glyph_row->reversed_p)
18351 {
18352 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18353
18354 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18355 end = from + tused;
18356 to = it->glyph_row->glyphs[TEXT_AREA];
18357 toend = to + it->glyph_row->used[TEXT_AREA];
18358 if (FRAME_WINDOW_P (it->f))
18359 {
18360 /* On GUI frames, when variable-size fonts are displayed,
18361 the truncation glyphs may need more pixels than the row's
18362 glyphs they overwrite. We overwrite more glyphs to free
18363 enough screen real estate, and enlarge the stretch glyph
18364 on the right (see display_line), if there is one, to
18365 preserve the screen position of the truncation glyphs on
18366 the right. */
18367 int w = 0;
18368 struct glyph *g = to;
18369 short used;
18370
18371 /* The first glyph could be partially visible, in which case
18372 it->glyph_row->x will be negative. But we want the left
18373 truncation glyphs to be aligned at the left margin of the
18374 window, so we override the x coordinate at which the row
18375 will begin. */
18376 it->glyph_row->x = 0;
18377 while (g < toend && w < it->truncation_pixel_width)
18378 {
18379 w += g->pixel_width;
18380 ++g;
18381 }
18382 if (g - to - tused > 0)
18383 {
18384 memmove (to + tused, g, (toend - g) * sizeof(*g));
18385 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18386 }
18387 used = it->glyph_row->used[TEXT_AREA];
18388 if (it->glyph_row->truncated_on_right_p
18389 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18390 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18391 == STRETCH_GLYPH)
18392 {
18393 int extra = w - it->truncation_pixel_width;
18394
18395 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18396 }
18397 }
18398
18399 while (from < end)
18400 *to++ = *from++;
18401
18402 /* There may be padding glyphs left over. Overwrite them too. */
18403 if (!FRAME_WINDOW_P (it->f))
18404 {
18405 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18406 {
18407 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18408 while (from < end)
18409 *to++ = *from++;
18410 }
18411 }
18412
18413 if (to > toend)
18414 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18415 }
18416 else
18417 {
18418 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18419
18420 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18421 that back to front. */
18422 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18423 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18424 toend = it->glyph_row->glyphs[TEXT_AREA];
18425 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18426 if (FRAME_WINDOW_P (it->f))
18427 {
18428 int w = 0;
18429 struct glyph *g = to;
18430
18431 while (g >= toend && w < it->truncation_pixel_width)
18432 {
18433 w += g->pixel_width;
18434 --g;
18435 }
18436 if (to - g - tused > 0)
18437 to = g + tused;
18438 if (it->glyph_row->truncated_on_right_p
18439 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18440 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18441 {
18442 int extra = w - it->truncation_pixel_width;
18443
18444 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18445 }
18446 }
18447
18448 while (from >= end && to >= toend)
18449 *to-- = *from--;
18450 if (!FRAME_WINDOW_P (it->f))
18451 {
18452 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18453 {
18454 from =
18455 truncate_it.glyph_row->glyphs[TEXT_AREA]
18456 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18457 while (from >= end && to >= toend)
18458 *to-- = *from--;
18459 }
18460 }
18461 if (from >= end)
18462 {
18463 /* Need to free some room before prepending additional
18464 glyphs. */
18465 int move_by = from - end + 1;
18466 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18467 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18468
18469 for ( ; g >= g0; g--)
18470 g[move_by] = *g;
18471 while (from >= end)
18472 *to-- = *from--;
18473 it->glyph_row->used[TEXT_AREA] += move_by;
18474 }
18475 }
18476 }
18477
18478 /* Compute the hash code for ROW. */
18479 unsigned
18480 row_hash (struct glyph_row *row)
18481 {
18482 int area, k;
18483 unsigned hashval = 0;
18484
18485 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18486 for (k = 0; k < row->used[area]; ++k)
18487 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18488 + row->glyphs[area][k].u.val
18489 + row->glyphs[area][k].face_id
18490 + row->glyphs[area][k].padding_p
18491 + (row->glyphs[area][k].type << 2));
18492
18493 return hashval;
18494 }
18495
18496 /* Compute the pixel height and width of IT->glyph_row.
18497
18498 Most of the time, ascent and height of a display line will be equal
18499 to the max_ascent and max_height values of the display iterator
18500 structure. This is not the case if
18501
18502 1. We hit ZV without displaying anything. In this case, max_ascent
18503 and max_height will be zero.
18504
18505 2. We have some glyphs that don't contribute to the line height.
18506 (The glyph row flag contributes_to_line_height_p is for future
18507 pixmap extensions).
18508
18509 The first case is easily covered by using default values because in
18510 these cases, the line height does not really matter, except that it
18511 must not be zero. */
18512
18513 static void
18514 compute_line_metrics (struct it *it)
18515 {
18516 struct glyph_row *row = it->glyph_row;
18517
18518 if (FRAME_WINDOW_P (it->f))
18519 {
18520 int i, min_y, max_y;
18521
18522 /* The line may consist of one space only, that was added to
18523 place the cursor on it. If so, the row's height hasn't been
18524 computed yet. */
18525 if (row->height == 0)
18526 {
18527 if (it->max_ascent + it->max_descent == 0)
18528 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18529 row->ascent = it->max_ascent;
18530 row->height = it->max_ascent + it->max_descent;
18531 row->phys_ascent = it->max_phys_ascent;
18532 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18533 row->extra_line_spacing = it->max_extra_line_spacing;
18534 }
18535
18536 /* Compute the width of this line. */
18537 row->pixel_width = row->x;
18538 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18539 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18540
18541 eassert (row->pixel_width >= 0);
18542 eassert (row->ascent >= 0 && row->height > 0);
18543
18544 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18545 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18546
18547 /* If first line's physical ascent is larger than its logical
18548 ascent, use the physical ascent, and make the row taller.
18549 This makes accented characters fully visible. */
18550 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18551 && row->phys_ascent > row->ascent)
18552 {
18553 row->height += row->phys_ascent - row->ascent;
18554 row->ascent = row->phys_ascent;
18555 }
18556
18557 /* Compute how much of the line is visible. */
18558 row->visible_height = row->height;
18559
18560 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18561 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18562
18563 if (row->y < min_y)
18564 row->visible_height -= min_y - row->y;
18565 if (row->y + row->height > max_y)
18566 row->visible_height -= row->y + row->height - max_y;
18567 }
18568 else
18569 {
18570 row->pixel_width = row->used[TEXT_AREA];
18571 if (row->continued_p)
18572 row->pixel_width -= it->continuation_pixel_width;
18573 else if (row->truncated_on_right_p)
18574 row->pixel_width -= it->truncation_pixel_width;
18575 row->ascent = row->phys_ascent = 0;
18576 row->height = row->phys_height = row->visible_height = 1;
18577 row->extra_line_spacing = 0;
18578 }
18579
18580 /* Compute a hash code for this row. */
18581 row->hash = row_hash (row);
18582
18583 it->max_ascent = it->max_descent = 0;
18584 it->max_phys_ascent = it->max_phys_descent = 0;
18585 }
18586
18587
18588 /* Append one space to the glyph row of iterator IT if doing a
18589 window-based redisplay. The space has the same face as
18590 IT->face_id. Value is non-zero if a space was added.
18591
18592 This function is called to make sure that there is always one glyph
18593 at the end of a glyph row that the cursor can be set on under
18594 window-systems. (If there weren't such a glyph we would not know
18595 how wide and tall a box cursor should be displayed).
18596
18597 At the same time this space let's a nicely handle clearing to the
18598 end of the line if the row ends in italic text. */
18599
18600 static int
18601 append_space_for_newline (struct it *it, int default_face_p)
18602 {
18603 if (FRAME_WINDOW_P (it->f))
18604 {
18605 int n = it->glyph_row->used[TEXT_AREA];
18606
18607 if (it->glyph_row->glyphs[TEXT_AREA] + n
18608 < it->glyph_row->glyphs[1 + TEXT_AREA])
18609 {
18610 /* Save some values that must not be changed.
18611 Must save IT->c and IT->len because otherwise
18612 ITERATOR_AT_END_P wouldn't work anymore after
18613 append_space_for_newline has been called. */
18614 enum display_element_type saved_what = it->what;
18615 int saved_c = it->c, saved_len = it->len;
18616 int saved_char_to_display = it->char_to_display;
18617 int saved_x = it->current_x;
18618 int saved_face_id = it->face_id;
18619 int saved_box_end = it->end_of_box_run_p;
18620 struct text_pos saved_pos;
18621 Lisp_Object saved_object;
18622 struct face *face;
18623
18624 saved_object = it->object;
18625 saved_pos = it->position;
18626
18627 it->what = IT_CHARACTER;
18628 memset (&it->position, 0, sizeof it->position);
18629 it->object = make_number (0);
18630 it->c = it->char_to_display = ' ';
18631 it->len = 1;
18632
18633 /* If the default face was remapped, be sure to use the
18634 remapped face for the appended newline. */
18635 if (default_face_p)
18636 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18637 else if (it->face_before_selective_p)
18638 it->face_id = it->saved_face_id;
18639 face = FACE_FROM_ID (it->f, it->face_id);
18640 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18641 /* In R2L rows, we will prepend a stretch glyph that will
18642 have the end_of_box_run_p flag set for it, so there's no
18643 need for the appended newline glyph to have that flag
18644 set. */
18645 if (it->glyph_row->reversed_p
18646 /* But if the appended newline glyph goes all the way to
18647 the end of the row, there will be no stretch glyph,
18648 so leave the box flag set. */
18649 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18650 it->end_of_box_run_p = 0;
18651
18652 PRODUCE_GLYPHS (it);
18653
18654 it->override_ascent = -1;
18655 it->constrain_row_ascent_descent_p = 0;
18656 it->current_x = saved_x;
18657 it->object = saved_object;
18658 it->position = saved_pos;
18659 it->what = saved_what;
18660 it->face_id = saved_face_id;
18661 it->len = saved_len;
18662 it->c = saved_c;
18663 it->char_to_display = saved_char_to_display;
18664 it->end_of_box_run_p = saved_box_end;
18665 return 1;
18666 }
18667 }
18668
18669 return 0;
18670 }
18671
18672
18673 /* Extend the face of the last glyph in the text area of IT->glyph_row
18674 to the end of the display line. Called from display_line. If the
18675 glyph row is empty, add a space glyph to it so that we know the
18676 face to draw. Set the glyph row flag fill_line_p. If the glyph
18677 row is R2L, prepend a stretch glyph to cover the empty space to the
18678 left of the leftmost glyph. */
18679
18680 static void
18681 extend_face_to_end_of_line (struct it *it)
18682 {
18683 struct face *face, *default_face;
18684 struct frame *f = it->f;
18685
18686 /* If line is already filled, do nothing. Non window-system frames
18687 get a grace of one more ``pixel'' because their characters are
18688 1-``pixel'' wide, so they hit the equality too early. This grace
18689 is needed only for R2L rows that are not continued, to produce
18690 one extra blank where we could display the cursor. */
18691 if (it->current_x >= it->last_visible_x
18692 + (!FRAME_WINDOW_P (f)
18693 && it->glyph_row->reversed_p
18694 && !it->glyph_row->continued_p))
18695 return;
18696
18697 /* The default face, possibly remapped. */
18698 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18699
18700 /* Face extension extends the background and box of IT->face_id
18701 to the end of the line. If the background equals the background
18702 of the frame, we don't have to do anything. */
18703 if (it->face_before_selective_p)
18704 face = FACE_FROM_ID (f, it->saved_face_id);
18705 else
18706 face = FACE_FROM_ID (f, it->face_id);
18707
18708 if (FRAME_WINDOW_P (f)
18709 && it->glyph_row->displays_text_p
18710 && face->box == FACE_NO_BOX
18711 && face->background == FRAME_BACKGROUND_PIXEL (f)
18712 && !face->stipple
18713 && !it->glyph_row->reversed_p)
18714 return;
18715
18716 /* Set the glyph row flag indicating that the face of the last glyph
18717 in the text area has to be drawn to the end of the text area. */
18718 it->glyph_row->fill_line_p = 1;
18719
18720 /* If current character of IT is not ASCII, make sure we have the
18721 ASCII face. This will be automatically undone the next time
18722 get_next_display_element returns a multibyte character. Note
18723 that the character will always be single byte in unibyte
18724 text. */
18725 if (!ASCII_CHAR_P (it->c))
18726 {
18727 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18728 }
18729
18730 if (FRAME_WINDOW_P (f))
18731 {
18732 /* If the row is empty, add a space with the current face of IT,
18733 so that we know which face to draw. */
18734 if (it->glyph_row->used[TEXT_AREA] == 0)
18735 {
18736 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18737 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18738 it->glyph_row->used[TEXT_AREA] = 1;
18739 }
18740 #ifdef HAVE_WINDOW_SYSTEM
18741 if (it->glyph_row->reversed_p)
18742 {
18743 /* Prepend a stretch glyph to the row, such that the
18744 rightmost glyph will be drawn flushed all the way to the
18745 right margin of the window. The stretch glyph that will
18746 occupy the empty space, if any, to the left of the
18747 glyphs. */
18748 struct font *font = face->font ? face->font : FRAME_FONT (f);
18749 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18750 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18751 struct glyph *g;
18752 int row_width, stretch_ascent, stretch_width;
18753 struct text_pos saved_pos;
18754 int saved_face_id, saved_avoid_cursor, saved_box_start;
18755
18756 for (row_width = 0, g = row_start; g < row_end; g++)
18757 row_width += g->pixel_width;
18758 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18759 if (stretch_width > 0)
18760 {
18761 stretch_ascent =
18762 (((it->ascent + it->descent)
18763 * FONT_BASE (font)) / FONT_HEIGHT (font));
18764 saved_pos = it->position;
18765 memset (&it->position, 0, sizeof it->position);
18766 saved_avoid_cursor = it->avoid_cursor_p;
18767 it->avoid_cursor_p = 1;
18768 saved_face_id = it->face_id;
18769 saved_box_start = it->start_of_box_run_p;
18770 /* The last row's stretch glyph should get the default
18771 face, to avoid painting the rest of the window with
18772 the region face, if the region ends at ZV. */
18773 if (it->glyph_row->ends_at_zv_p)
18774 it->face_id = default_face->id;
18775 else
18776 it->face_id = face->id;
18777 it->start_of_box_run_p = 0;
18778 append_stretch_glyph (it, make_number (0), stretch_width,
18779 it->ascent + it->descent, stretch_ascent);
18780 it->position = saved_pos;
18781 it->avoid_cursor_p = saved_avoid_cursor;
18782 it->face_id = saved_face_id;
18783 it->start_of_box_run_p = saved_box_start;
18784 }
18785 }
18786 #endif /* HAVE_WINDOW_SYSTEM */
18787 }
18788 else
18789 {
18790 /* Save some values that must not be changed. */
18791 int saved_x = it->current_x;
18792 struct text_pos saved_pos;
18793 Lisp_Object saved_object;
18794 enum display_element_type saved_what = it->what;
18795 int saved_face_id = it->face_id;
18796
18797 saved_object = it->object;
18798 saved_pos = it->position;
18799
18800 it->what = IT_CHARACTER;
18801 memset (&it->position, 0, sizeof it->position);
18802 it->object = make_number (0);
18803 it->c = it->char_to_display = ' ';
18804 it->len = 1;
18805 /* The last row's blank glyphs should get the default face, to
18806 avoid painting the rest of the window with the region face,
18807 if the region ends at ZV. */
18808 if (it->glyph_row->ends_at_zv_p)
18809 it->face_id = default_face->id;
18810 else
18811 it->face_id = face->id;
18812
18813 PRODUCE_GLYPHS (it);
18814
18815 while (it->current_x <= it->last_visible_x)
18816 PRODUCE_GLYPHS (it);
18817
18818 /* Don't count these blanks really. It would let us insert a left
18819 truncation glyph below and make us set the cursor on them, maybe. */
18820 it->current_x = saved_x;
18821 it->object = saved_object;
18822 it->position = saved_pos;
18823 it->what = saved_what;
18824 it->face_id = saved_face_id;
18825 }
18826 }
18827
18828
18829 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18830 trailing whitespace. */
18831
18832 static int
18833 trailing_whitespace_p (ptrdiff_t charpos)
18834 {
18835 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18836 int c = 0;
18837
18838 while (bytepos < ZV_BYTE
18839 && (c = FETCH_CHAR (bytepos),
18840 c == ' ' || c == '\t'))
18841 ++bytepos;
18842
18843 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18844 {
18845 if (bytepos != PT_BYTE)
18846 return 1;
18847 }
18848 return 0;
18849 }
18850
18851
18852 /* Highlight trailing whitespace, if any, in ROW. */
18853
18854 static void
18855 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18856 {
18857 int used = row->used[TEXT_AREA];
18858
18859 if (used)
18860 {
18861 struct glyph *start = row->glyphs[TEXT_AREA];
18862 struct glyph *glyph = start + used - 1;
18863
18864 if (row->reversed_p)
18865 {
18866 /* Right-to-left rows need to be processed in the opposite
18867 direction, so swap the edge pointers. */
18868 glyph = start;
18869 start = row->glyphs[TEXT_AREA] + used - 1;
18870 }
18871
18872 /* Skip over glyphs inserted to display the cursor at the
18873 end of a line, for extending the face of the last glyph
18874 to the end of the line on terminals, and for truncation
18875 and continuation glyphs. */
18876 if (!row->reversed_p)
18877 {
18878 while (glyph >= start
18879 && glyph->type == CHAR_GLYPH
18880 && INTEGERP (glyph->object))
18881 --glyph;
18882 }
18883 else
18884 {
18885 while (glyph <= start
18886 && glyph->type == CHAR_GLYPH
18887 && INTEGERP (glyph->object))
18888 ++glyph;
18889 }
18890
18891 /* If last glyph is a space or stretch, and it's trailing
18892 whitespace, set the face of all trailing whitespace glyphs in
18893 IT->glyph_row to `trailing-whitespace'. */
18894 if ((row->reversed_p ? glyph <= start : glyph >= start)
18895 && BUFFERP (glyph->object)
18896 && (glyph->type == STRETCH_GLYPH
18897 || (glyph->type == CHAR_GLYPH
18898 && glyph->u.ch == ' '))
18899 && trailing_whitespace_p (glyph->charpos))
18900 {
18901 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18902 if (face_id < 0)
18903 return;
18904
18905 if (!row->reversed_p)
18906 {
18907 while (glyph >= start
18908 && BUFFERP (glyph->object)
18909 && (glyph->type == STRETCH_GLYPH
18910 || (glyph->type == CHAR_GLYPH
18911 && glyph->u.ch == ' ')))
18912 (glyph--)->face_id = face_id;
18913 }
18914 else
18915 {
18916 while (glyph <= start
18917 && BUFFERP (glyph->object)
18918 && (glyph->type == STRETCH_GLYPH
18919 || (glyph->type == CHAR_GLYPH
18920 && glyph->u.ch == ' ')))
18921 (glyph++)->face_id = face_id;
18922 }
18923 }
18924 }
18925 }
18926
18927
18928 /* Value is non-zero if glyph row ROW should be
18929 used to hold the cursor. */
18930
18931 static int
18932 cursor_row_p (struct glyph_row *row)
18933 {
18934 int result = 1;
18935
18936 if (PT == CHARPOS (row->end.pos)
18937 || PT == MATRIX_ROW_END_CHARPOS (row))
18938 {
18939 /* Suppose the row ends on a string.
18940 Unless the row is continued, that means it ends on a newline
18941 in the string. If it's anything other than a display string
18942 (e.g., a before-string from an overlay), we don't want the
18943 cursor there. (This heuristic seems to give the optimal
18944 behavior for the various types of multi-line strings.)
18945 One exception: if the string has `cursor' property on one of
18946 its characters, we _do_ want the cursor there. */
18947 if (CHARPOS (row->end.string_pos) >= 0)
18948 {
18949 if (row->continued_p)
18950 result = 1;
18951 else
18952 {
18953 /* Check for `display' property. */
18954 struct glyph *beg = row->glyphs[TEXT_AREA];
18955 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18956 struct glyph *glyph;
18957
18958 result = 0;
18959 for (glyph = end; glyph >= beg; --glyph)
18960 if (STRINGP (glyph->object))
18961 {
18962 Lisp_Object prop
18963 = Fget_char_property (make_number (PT),
18964 Qdisplay, Qnil);
18965 result =
18966 (!NILP (prop)
18967 && display_prop_string_p (prop, glyph->object));
18968 /* If there's a `cursor' property on one of the
18969 string's characters, this row is a cursor row,
18970 even though this is not a display string. */
18971 if (!result)
18972 {
18973 Lisp_Object s = glyph->object;
18974
18975 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18976 {
18977 ptrdiff_t gpos = glyph->charpos;
18978
18979 if (!NILP (Fget_char_property (make_number (gpos),
18980 Qcursor, s)))
18981 {
18982 result = 1;
18983 break;
18984 }
18985 }
18986 }
18987 break;
18988 }
18989 }
18990 }
18991 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18992 {
18993 /* If the row ends in middle of a real character,
18994 and the line is continued, we want the cursor here.
18995 That's because CHARPOS (ROW->end.pos) would equal
18996 PT if PT is before the character. */
18997 if (!row->ends_in_ellipsis_p)
18998 result = row->continued_p;
18999 else
19000 /* If the row ends in an ellipsis, then
19001 CHARPOS (ROW->end.pos) will equal point after the
19002 invisible text. We want that position to be displayed
19003 after the ellipsis. */
19004 result = 0;
19005 }
19006 /* If the row ends at ZV, display the cursor at the end of that
19007 row instead of at the start of the row below. */
19008 else if (row->ends_at_zv_p)
19009 result = 1;
19010 else
19011 result = 0;
19012 }
19013
19014 return result;
19015 }
19016
19017 \f
19018
19019 /* Push the property PROP so that it will be rendered at the current
19020 position in IT. Return 1 if PROP was successfully pushed, 0
19021 otherwise. Called from handle_line_prefix to handle the
19022 `line-prefix' and `wrap-prefix' properties. */
19023
19024 static int
19025 push_prefix_prop (struct it *it, Lisp_Object prop)
19026 {
19027 struct text_pos pos =
19028 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19029
19030 eassert (it->method == GET_FROM_BUFFER
19031 || it->method == GET_FROM_DISPLAY_VECTOR
19032 || it->method == GET_FROM_STRING);
19033
19034 /* We need to save the current buffer/string position, so it will be
19035 restored by pop_it, because iterate_out_of_display_property
19036 depends on that being set correctly, but some situations leave
19037 it->position not yet set when this function is called. */
19038 push_it (it, &pos);
19039
19040 if (STRINGP (prop))
19041 {
19042 if (SCHARS (prop) == 0)
19043 {
19044 pop_it (it);
19045 return 0;
19046 }
19047
19048 it->string = prop;
19049 it->string_from_prefix_prop_p = 1;
19050 it->multibyte_p = STRING_MULTIBYTE (it->string);
19051 it->current.overlay_string_index = -1;
19052 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19053 it->end_charpos = it->string_nchars = SCHARS (it->string);
19054 it->method = GET_FROM_STRING;
19055 it->stop_charpos = 0;
19056 it->prev_stop = 0;
19057 it->base_level_stop = 0;
19058
19059 /* Force paragraph direction to be that of the parent
19060 buffer/string. */
19061 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19062 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19063 else
19064 it->paragraph_embedding = L2R;
19065
19066 /* Set up the bidi iterator for this display string. */
19067 if (it->bidi_p)
19068 {
19069 it->bidi_it.string.lstring = it->string;
19070 it->bidi_it.string.s = NULL;
19071 it->bidi_it.string.schars = it->end_charpos;
19072 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19073 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19074 it->bidi_it.string.unibyte = !it->multibyte_p;
19075 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19076 }
19077 }
19078 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19079 {
19080 it->method = GET_FROM_STRETCH;
19081 it->object = prop;
19082 }
19083 #ifdef HAVE_WINDOW_SYSTEM
19084 else if (IMAGEP (prop))
19085 {
19086 it->what = IT_IMAGE;
19087 it->image_id = lookup_image (it->f, prop);
19088 it->method = GET_FROM_IMAGE;
19089 }
19090 #endif /* HAVE_WINDOW_SYSTEM */
19091 else
19092 {
19093 pop_it (it); /* bogus display property, give up */
19094 return 0;
19095 }
19096
19097 return 1;
19098 }
19099
19100 /* Return the character-property PROP at the current position in IT. */
19101
19102 static Lisp_Object
19103 get_it_property (struct it *it, Lisp_Object prop)
19104 {
19105 Lisp_Object position;
19106
19107 if (STRINGP (it->object))
19108 position = make_number (IT_STRING_CHARPOS (*it));
19109 else if (BUFFERP (it->object))
19110 position = make_number (IT_CHARPOS (*it));
19111 else
19112 return Qnil;
19113
19114 return Fget_char_property (position, prop, it->object);
19115 }
19116
19117 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19118
19119 static void
19120 handle_line_prefix (struct it *it)
19121 {
19122 Lisp_Object prefix;
19123
19124 if (it->continuation_lines_width > 0)
19125 {
19126 prefix = get_it_property (it, Qwrap_prefix);
19127 if (NILP (prefix))
19128 prefix = Vwrap_prefix;
19129 }
19130 else
19131 {
19132 prefix = get_it_property (it, Qline_prefix);
19133 if (NILP (prefix))
19134 prefix = Vline_prefix;
19135 }
19136 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19137 {
19138 /* If the prefix is wider than the window, and we try to wrap
19139 it, it would acquire its own wrap prefix, and so on till the
19140 iterator stack overflows. So, don't wrap the prefix. */
19141 it->line_wrap = TRUNCATE;
19142 it->avoid_cursor_p = 1;
19143 }
19144 }
19145
19146 \f
19147
19148 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19149 only for R2L lines from display_line and display_string, when they
19150 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19151 the line/string needs to be continued on the next glyph row. */
19152 static void
19153 unproduce_glyphs (struct it *it, int n)
19154 {
19155 struct glyph *glyph, *end;
19156
19157 eassert (it->glyph_row);
19158 eassert (it->glyph_row->reversed_p);
19159 eassert (it->area == TEXT_AREA);
19160 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19161
19162 if (n > it->glyph_row->used[TEXT_AREA])
19163 n = it->glyph_row->used[TEXT_AREA];
19164 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19165 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19166 for ( ; glyph < end; glyph++)
19167 glyph[-n] = *glyph;
19168 }
19169
19170 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19171 and ROW->maxpos. */
19172 static void
19173 find_row_edges (struct it *it, struct glyph_row *row,
19174 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19175 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19176 {
19177 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19178 lines' rows is implemented for bidi-reordered rows. */
19179
19180 /* ROW->minpos is the value of min_pos, the minimal buffer position
19181 we have in ROW, or ROW->start.pos if that is smaller. */
19182 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19183 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19184 else
19185 /* We didn't find buffer positions smaller than ROW->start, or
19186 didn't find _any_ valid buffer positions in any of the glyphs,
19187 so we must trust the iterator's computed positions. */
19188 row->minpos = row->start.pos;
19189 if (max_pos <= 0)
19190 {
19191 max_pos = CHARPOS (it->current.pos);
19192 max_bpos = BYTEPOS (it->current.pos);
19193 }
19194
19195 /* Here are the various use-cases for ending the row, and the
19196 corresponding values for ROW->maxpos:
19197
19198 Line ends in a newline from buffer eol_pos + 1
19199 Line is continued from buffer max_pos + 1
19200 Line is truncated on right it->current.pos
19201 Line ends in a newline from string max_pos + 1(*)
19202 (*) + 1 only when line ends in a forward scan
19203 Line is continued from string max_pos
19204 Line is continued from display vector max_pos
19205 Line is entirely from a string min_pos == max_pos
19206 Line is entirely from a display vector min_pos == max_pos
19207 Line that ends at ZV ZV
19208
19209 If you discover other use-cases, please add them here as
19210 appropriate. */
19211 if (row->ends_at_zv_p)
19212 row->maxpos = it->current.pos;
19213 else if (row->used[TEXT_AREA])
19214 {
19215 int seen_this_string = 0;
19216 struct glyph_row *r1 = row - 1;
19217
19218 /* Did we see the same display string on the previous row? */
19219 if (STRINGP (it->object)
19220 /* this is not the first row */
19221 && row > it->w->desired_matrix->rows
19222 /* previous row is not the header line */
19223 && !r1->mode_line_p
19224 /* previous row also ends in a newline from a string */
19225 && r1->ends_in_newline_from_string_p)
19226 {
19227 struct glyph *start, *end;
19228
19229 /* Search for the last glyph of the previous row that came
19230 from buffer or string. Depending on whether the row is
19231 L2R or R2L, we need to process it front to back or the
19232 other way round. */
19233 if (!r1->reversed_p)
19234 {
19235 start = r1->glyphs[TEXT_AREA];
19236 end = start + r1->used[TEXT_AREA];
19237 /* Glyphs inserted by redisplay have an integer (zero)
19238 as their object. */
19239 while (end > start
19240 && INTEGERP ((end - 1)->object)
19241 && (end - 1)->charpos <= 0)
19242 --end;
19243 if (end > start)
19244 {
19245 if (EQ ((end - 1)->object, it->object))
19246 seen_this_string = 1;
19247 }
19248 else
19249 /* If all the glyphs of the previous row were inserted
19250 by redisplay, it means the previous row was
19251 produced from a single newline, which is only
19252 possible if that newline came from the same string
19253 as the one which produced this ROW. */
19254 seen_this_string = 1;
19255 }
19256 else
19257 {
19258 end = r1->glyphs[TEXT_AREA] - 1;
19259 start = end + r1->used[TEXT_AREA];
19260 while (end < start
19261 && INTEGERP ((end + 1)->object)
19262 && (end + 1)->charpos <= 0)
19263 ++end;
19264 if (end < start)
19265 {
19266 if (EQ ((end + 1)->object, it->object))
19267 seen_this_string = 1;
19268 }
19269 else
19270 seen_this_string = 1;
19271 }
19272 }
19273 /* Take note of each display string that covers a newline only
19274 once, the first time we see it. This is for when a display
19275 string includes more than one newline in it. */
19276 if (row->ends_in_newline_from_string_p && !seen_this_string)
19277 {
19278 /* If we were scanning the buffer forward when we displayed
19279 the string, we want to account for at least one buffer
19280 position that belongs to this row (position covered by
19281 the display string), so that cursor positioning will
19282 consider this row as a candidate when point is at the end
19283 of the visual line represented by this row. This is not
19284 required when scanning back, because max_pos will already
19285 have a much larger value. */
19286 if (CHARPOS (row->end.pos) > max_pos)
19287 INC_BOTH (max_pos, max_bpos);
19288 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19289 }
19290 else if (CHARPOS (it->eol_pos) > 0)
19291 SET_TEXT_POS (row->maxpos,
19292 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19293 else if (row->continued_p)
19294 {
19295 /* If max_pos is different from IT's current position, it
19296 means IT->method does not belong to the display element
19297 at max_pos. However, it also means that the display
19298 element at max_pos was displayed in its entirety on this
19299 line, which is equivalent to saying that the next line
19300 starts at the next buffer position. */
19301 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19302 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19303 else
19304 {
19305 INC_BOTH (max_pos, max_bpos);
19306 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19307 }
19308 }
19309 else if (row->truncated_on_right_p)
19310 /* display_line already called reseat_at_next_visible_line_start,
19311 which puts the iterator at the beginning of the next line, in
19312 the logical order. */
19313 row->maxpos = it->current.pos;
19314 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19315 /* A line that is entirely from a string/image/stretch... */
19316 row->maxpos = row->minpos;
19317 else
19318 emacs_abort ();
19319 }
19320 else
19321 row->maxpos = it->current.pos;
19322 }
19323
19324 /* Construct the glyph row IT->glyph_row in the desired matrix of
19325 IT->w from text at the current position of IT. See dispextern.h
19326 for an overview of struct it. Value is non-zero if
19327 IT->glyph_row displays text, as opposed to a line displaying ZV
19328 only. */
19329
19330 static int
19331 display_line (struct it *it)
19332 {
19333 struct glyph_row *row = it->glyph_row;
19334 Lisp_Object overlay_arrow_string;
19335 struct it wrap_it;
19336 void *wrap_data = NULL;
19337 int may_wrap = 0, wrap_x IF_LINT (= 0);
19338 int wrap_row_used = -1;
19339 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19340 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19341 int wrap_row_extra_line_spacing IF_LINT (= 0);
19342 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19343 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19344 int cvpos;
19345 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19346 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19347
19348 /* We always start displaying at hpos zero even if hscrolled. */
19349 eassert (it->hpos == 0 && it->current_x == 0);
19350
19351 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19352 >= it->w->desired_matrix->nrows)
19353 {
19354 it->w->nrows_scale_factor++;
19355 fonts_changed_p = 1;
19356 return 0;
19357 }
19358
19359 /* Is IT->w showing the region? */
19360 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19361
19362 /* Clear the result glyph row and enable it. */
19363 prepare_desired_row (row);
19364
19365 row->y = it->current_y;
19366 row->start = it->start;
19367 row->continuation_lines_width = it->continuation_lines_width;
19368 row->displays_text_p = 1;
19369 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19370 it->starts_in_middle_of_char_p = 0;
19371
19372 /* Arrange the overlays nicely for our purposes. Usually, we call
19373 display_line on only one line at a time, in which case this
19374 can't really hurt too much, or we call it on lines which appear
19375 one after another in the buffer, in which case all calls to
19376 recenter_overlay_lists but the first will be pretty cheap. */
19377 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19378
19379 /* Move over display elements that are not visible because we are
19380 hscrolled. This may stop at an x-position < IT->first_visible_x
19381 if the first glyph is partially visible or if we hit a line end. */
19382 if (it->current_x < it->first_visible_x)
19383 {
19384 enum move_it_result move_result;
19385
19386 this_line_min_pos = row->start.pos;
19387 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19388 MOVE_TO_POS | MOVE_TO_X);
19389 /* If we are under a large hscroll, move_it_in_display_line_to
19390 could hit the end of the line without reaching
19391 it->first_visible_x. Pretend that we did reach it. This is
19392 especially important on a TTY, where we will call
19393 extend_face_to_end_of_line, which needs to know how many
19394 blank glyphs to produce. */
19395 if (it->current_x < it->first_visible_x
19396 && (move_result == MOVE_NEWLINE_OR_CR
19397 || move_result == MOVE_POS_MATCH_OR_ZV))
19398 it->current_x = it->first_visible_x;
19399
19400 /* Record the smallest positions seen while we moved over
19401 display elements that are not visible. This is needed by
19402 redisplay_internal for optimizing the case where the cursor
19403 stays inside the same line. The rest of this function only
19404 considers positions that are actually displayed, so
19405 RECORD_MAX_MIN_POS will not otherwise record positions that
19406 are hscrolled to the left of the left edge of the window. */
19407 min_pos = CHARPOS (this_line_min_pos);
19408 min_bpos = BYTEPOS (this_line_min_pos);
19409 }
19410 else
19411 {
19412 /* We only do this when not calling `move_it_in_display_line_to'
19413 above, because move_it_in_display_line_to calls
19414 handle_line_prefix itself. */
19415 handle_line_prefix (it);
19416 }
19417
19418 /* Get the initial row height. This is either the height of the
19419 text hscrolled, if there is any, or zero. */
19420 row->ascent = it->max_ascent;
19421 row->height = it->max_ascent + it->max_descent;
19422 row->phys_ascent = it->max_phys_ascent;
19423 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19424 row->extra_line_spacing = it->max_extra_line_spacing;
19425
19426 /* Utility macro to record max and min buffer positions seen until now. */
19427 #define RECORD_MAX_MIN_POS(IT) \
19428 do \
19429 { \
19430 int composition_p = !STRINGP ((IT)->string) \
19431 && ((IT)->what == IT_COMPOSITION); \
19432 ptrdiff_t current_pos = \
19433 composition_p ? (IT)->cmp_it.charpos \
19434 : IT_CHARPOS (*(IT)); \
19435 ptrdiff_t current_bpos = \
19436 composition_p ? CHAR_TO_BYTE (current_pos) \
19437 : IT_BYTEPOS (*(IT)); \
19438 if (current_pos < min_pos) \
19439 { \
19440 min_pos = current_pos; \
19441 min_bpos = current_bpos; \
19442 } \
19443 if (IT_CHARPOS (*it) > max_pos) \
19444 { \
19445 max_pos = IT_CHARPOS (*it); \
19446 max_bpos = IT_BYTEPOS (*it); \
19447 } \
19448 } \
19449 while (0)
19450
19451 /* Loop generating characters. The loop is left with IT on the next
19452 character to display. */
19453 while (1)
19454 {
19455 int n_glyphs_before, hpos_before, x_before;
19456 int x, nglyphs;
19457 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19458
19459 /* Retrieve the next thing to display. Value is zero if end of
19460 buffer reached. */
19461 if (!get_next_display_element (it))
19462 {
19463 /* Maybe add a space at the end of this line that is used to
19464 display the cursor there under X. Set the charpos of the
19465 first glyph of blank lines not corresponding to any text
19466 to -1. */
19467 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19468 row->exact_window_width_line_p = 1;
19469 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19470 || row->used[TEXT_AREA] == 0)
19471 {
19472 row->glyphs[TEXT_AREA]->charpos = -1;
19473 row->displays_text_p = 0;
19474
19475 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19476 && (!MINI_WINDOW_P (it->w)
19477 || (minibuf_level && EQ (it->window, minibuf_window))))
19478 row->indicate_empty_line_p = 1;
19479 }
19480
19481 it->continuation_lines_width = 0;
19482 row->ends_at_zv_p = 1;
19483 /* A row that displays right-to-left text must always have
19484 its last face extended all the way to the end of line,
19485 even if this row ends in ZV, because we still write to
19486 the screen left to right. We also need to extend the
19487 last face if the default face is remapped to some
19488 different face, otherwise the functions that clear
19489 portions of the screen will clear with the default face's
19490 background color. */
19491 if (row->reversed_p
19492 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19493 extend_face_to_end_of_line (it);
19494 break;
19495 }
19496
19497 /* Now, get the metrics of what we want to display. This also
19498 generates glyphs in `row' (which is IT->glyph_row). */
19499 n_glyphs_before = row->used[TEXT_AREA];
19500 x = it->current_x;
19501
19502 /* Remember the line height so far in case the next element doesn't
19503 fit on the line. */
19504 if (it->line_wrap != TRUNCATE)
19505 {
19506 ascent = it->max_ascent;
19507 descent = it->max_descent;
19508 phys_ascent = it->max_phys_ascent;
19509 phys_descent = it->max_phys_descent;
19510
19511 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19512 {
19513 if (IT_DISPLAYING_WHITESPACE (it))
19514 may_wrap = 1;
19515 else if (may_wrap)
19516 {
19517 SAVE_IT (wrap_it, *it, wrap_data);
19518 wrap_x = x;
19519 wrap_row_used = row->used[TEXT_AREA];
19520 wrap_row_ascent = row->ascent;
19521 wrap_row_height = row->height;
19522 wrap_row_phys_ascent = row->phys_ascent;
19523 wrap_row_phys_height = row->phys_height;
19524 wrap_row_extra_line_spacing = row->extra_line_spacing;
19525 wrap_row_min_pos = min_pos;
19526 wrap_row_min_bpos = min_bpos;
19527 wrap_row_max_pos = max_pos;
19528 wrap_row_max_bpos = max_bpos;
19529 may_wrap = 0;
19530 }
19531 }
19532 }
19533
19534 PRODUCE_GLYPHS (it);
19535
19536 /* If this display element was in marginal areas, continue with
19537 the next one. */
19538 if (it->area != TEXT_AREA)
19539 {
19540 row->ascent = max (row->ascent, it->max_ascent);
19541 row->height = max (row->height, it->max_ascent + it->max_descent);
19542 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19543 row->phys_height = max (row->phys_height,
19544 it->max_phys_ascent + it->max_phys_descent);
19545 row->extra_line_spacing = max (row->extra_line_spacing,
19546 it->max_extra_line_spacing);
19547 set_iterator_to_next (it, 1);
19548 continue;
19549 }
19550
19551 /* Does the display element fit on the line? If we truncate
19552 lines, we should draw past the right edge of the window. If
19553 we don't truncate, we want to stop so that we can display the
19554 continuation glyph before the right margin. If lines are
19555 continued, there are two possible strategies for characters
19556 resulting in more than 1 glyph (e.g. tabs): Display as many
19557 glyphs as possible in this line and leave the rest for the
19558 continuation line, or display the whole element in the next
19559 line. Original redisplay did the former, so we do it also. */
19560 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19561 hpos_before = it->hpos;
19562 x_before = x;
19563
19564 if (/* Not a newline. */
19565 nglyphs > 0
19566 /* Glyphs produced fit entirely in the line. */
19567 && it->current_x < it->last_visible_x)
19568 {
19569 it->hpos += nglyphs;
19570 row->ascent = max (row->ascent, it->max_ascent);
19571 row->height = max (row->height, it->max_ascent + it->max_descent);
19572 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19573 row->phys_height = max (row->phys_height,
19574 it->max_phys_ascent + it->max_phys_descent);
19575 row->extra_line_spacing = max (row->extra_line_spacing,
19576 it->max_extra_line_spacing);
19577 if (it->current_x - it->pixel_width < it->first_visible_x)
19578 row->x = x - it->first_visible_x;
19579 /* Record the maximum and minimum buffer positions seen so
19580 far in glyphs that will be displayed by this row. */
19581 if (it->bidi_p)
19582 RECORD_MAX_MIN_POS (it);
19583 }
19584 else
19585 {
19586 int i, new_x;
19587 struct glyph *glyph;
19588
19589 for (i = 0; i < nglyphs; ++i, x = new_x)
19590 {
19591 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19592 new_x = x + glyph->pixel_width;
19593
19594 if (/* Lines are continued. */
19595 it->line_wrap != TRUNCATE
19596 && (/* Glyph doesn't fit on the line. */
19597 new_x > it->last_visible_x
19598 /* Or it fits exactly on a window system frame. */
19599 || (new_x == it->last_visible_x
19600 && FRAME_WINDOW_P (it->f)
19601 && (row->reversed_p
19602 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19603 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19604 {
19605 /* End of a continued line. */
19606
19607 if (it->hpos == 0
19608 || (new_x == it->last_visible_x
19609 && FRAME_WINDOW_P (it->f)
19610 && (row->reversed_p
19611 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19612 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19613 {
19614 /* Current glyph is the only one on the line or
19615 fits exactly on the line. We must continue
19616 the line because we can't draw the cursor
19617 after the glyph. */
19618 row->continued_p = 1;
19619 it->current_x = new_x;
19620 it->continuation_lines_width += new_x;
19621 ++it->hpos;
19622 if (i == nglyphs - 1)
19623 {
19624 /* If line-wrap is on, check if a previous
19625 wrap point was found. */
19626 if (wrap_row_used > 0
19627 /* Even if there is a previous wrap
19628 point, continue the line here as
19629 usual, if (i) the previous character
19630 was a space or tab AND (ii) the
19631 current character is not. */
19632 && (!may_wrap
19633 || IT_DISPLAYING_WHITESPACE (it)))
19634 goto back_to_wrap;
19635
19636 /* Record the maximum and minimum buffer
19637 positions seen so far in glyphs that will be
19638 displayed by this row. */
19639 if (it->bidi_p)
19640 RECORD_MAX_MIN_POS (it);
19641 set_iterator_to_next (it, 1);
19642 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19643 {
19644 if (!get_next_display_element (it))
19645 {
19646 row->exact_window_width_line_p = 1;
19647 it->continuation_lines_width = 0;
19648 row->continued_p = 0;
19649 row->ends_at_zv_p = 1;
19650 }
19651 else if (ITERATOR_AT_END_OF_LINE_P (it))
19652 {
19653 row->continued_p = 0;
19654 row->exact_window_width_line_p = 1;
19655 }
19656 }
19657 }
19658 else if (it->bidi_p)
19659 RECORD_MAX_MIN_POS (it);
19660 }
19661 else if (CHAR_GLYPH_PADDING_P (*glyph)
19662 && !FRAME_WINDOW_P (it->f))
19663 {
19664 /* A padding glyph that doesn't fit on this line.
19665 This means the whole character doesn't fit
19666 on the line. */
19667 if (row->reversed_p)
19668 unproduce_glyphs (it, row->used[TEXT_AREA]
19669 - n_glyphs_before);
19670 row->used[TEXT_AREA] = n_glyphs_before;
19671
19672 /* Fill the rest of the row with continuation
19673 glyphs like in 20.x. */
19674 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19675 < row->glyphs[1 + TEXT_AREA])
19676 produce_special_glyphs (it, IT_CONTINUATION);
19677
19678 row->continued_p = 1;
19679 it->current_x = x_before;
19680 it->continuation_lines_width += x_before;
19681
19682 /* Restore the height to what it was before the
19683 element not fitting on the line. */
19684 it->max_ascent = ascent;
19685 it->max_descent = descent;
19686 it->max_phys_ascent = phys_ascent;
19687 it->max_phys_descent = phys_descent;
19688 }
19689 else if (wrap_row_used > 0)
19690 {
19691 back_to_wrap:
19692 if (row->reversed_p)
19693 unproduce_glyphs (it,
19694 row->used[TEXT_AREA] - wrap_row_used);
19695 RESTORE_IT (it, &wrap_it, wrap_data);
19696 it->continuation_lines_width += wrap_x;
19697 row->used[TEXT_AREA] = wrap_row_used;
19698 row->ascent = wrap_row_ascent;
19699 row->height = wrap_row_height;
19700 row->phys_ascent = wrap_row_phys_ascent;
19701 row->phys_height = wrap_row_phys_height;
19702 row->extra_line_spacing = wrap_row_extra_line_spacing;
19703 min_pos = wrap_row_min_pos;
19704 min_bpos = wrap_row_min_bpos;
19705 max_pos = wrap_row_max_pos;
19706 max_bpos = wrap_row_max_bpos;
19707 row->continued_p = 1;
19708 row->ends_at_zv_p = 0;
19709 row->exact_window_width_line_p = 0;
19710 it->continuation_lines_width += x;
19711
19712 /* Make sure that a non-default face is extended
19713 up to the right margin of the window. */
19714 extend_face_to_end_of_line (it);
19715 }
19716 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19717 {
19718 /* A TAB that extends past the right edge of the
19719 window. This produces a single glyph on
19720 window system frames. We leave the glyph in
19721 this row and let it fill the row, but don't
19722 consume the TAB. */
19723 if ((row->reversed_p
19724 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19725 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19726 produce_special_glyphs (it, IT_CONTINUATION);
19727 it->continuation_lines_width += it->last_visible_x;
19728 row->ends_in_middle_of_char_p = 1;
19729 row->continued_p = 1;
19730 glyph->pixel_width = it->last_visible_x - x;
19731 it->starts_in_middle_of_char_p = 1;
19732 }
19733 else
19734 {
19735 /* Something other than a TAB that draws past
19736 the right edge of the window. Restore
19737 positions to values before the element. */
19738 if (row->reversed_p)
19739 unproduce_glyphs (it, row->used[TEXT_AREA]
19740 - (n_glyphs_before + i));
19741 row->used[TEXT_AREA] = n_glyphs_before + i;
19742
19743 /* Display continuation glyphs. */
19744 it->current_x = x_before;
19745 it->continuation_lines_width += x;
19746 if (!FRAME_WINDOW_P (it->f)
19747 || (row->reversed_p
19748 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19749 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19750 produce_special_glyphs (it, IT_CONTINUATION);
19751 row->continued_p = 1;
19752
19753 extend_face_to_end_of_line (it);
19754
19755 if (nglyphs > 1 && i > 0)
19756 {
19757 row->ends_in_middle_of_char_p = 1;
19758 it->starts_in_middle_of_char_p = 1;
19759 }
19760
19761 /* Restore the height to what it was before the
19762 element not fitting on the line. */
19763 it->max_ascent = ascent;
19764 it->max_descent = descent;
19765 it->max_phys_ascent = phys_ascent;
19766 it->max_phys_descent = phys_descent;
19767 }
19768
19769 break;
19770 }
19771 else if (new_x > it->first_visible_x)
19772 {
19773 /* Increment number of glyphs actually displayed. */
19774 ++it->hpos;
19775
19776 /* Record the maximum and minimum buffer positions
19777 seen so far in glyphs that will be displayed by
19778 this row. */
19779 if (it->bidi_p)
19780 RECORD_MAX_MIN_POS (it);
19781
19782 if (x < it->first_visible_x)
19783 /* Glyph is partially visible, i.e. row starts at
19784 negative X position. */
19785 row->x = x - it->first_visible_x;
19786 }
19787 else
19788 {
19789 /* Glyph is completely off the left margin of the
19790 window. This should not happen because of the
19791 move_it_in_display_line at the start of this
19792 function, unless the text display area of the
19793 window is empty. */
19794 eassert (it->first_visible_x <= it->last_visible_x);
19795 }
19796 }
19797 /* Even if this display element produced no glyphs at all,
19798 we want to record its position. */
19799 if (it->bidi_p && nglyphs == 0)
19800 RECORD_MAX_MIN_POS (it);
19801
19802 row->ascent = max (row->ascent, it->max_ascent);
19803 row->height = max (row->height, it->max_ascent + it->max_descent);
19804 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19805 row->phys_height = max (row->phys_height,
19806 it->max_phys_ascent + it->max_phys_descent);
19807 row->extra_line_spacing = max (row->extra_line_spacing,
19808 it->max_extra_line_spacing);
19809
19810 /* End of this display line if row is continued. */
19811 if (row->continued_p || row->ends_at_zv_p)
19812 break;
19813 }
19814
19815 at_end_of_line:
19816 /* Is this a line end? If yes, we're also done, after making
19817 sure that a non-default face is extended up to the right
19818 margin of the window. */
19819 if (ITERATOR_AT_END_OF_LINE_P (it))
19820 {
19821 int used_before = row->used[TEXT_AREA];
19822
19823 row->ends_in_newline_from_string_p = STRINGP (it->object);
19824
19825 /* Add a space at the end of the line that is used to
19826 display the cursor there. */
19827 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19828 append_space_for_newline (it, 0);
19829
19830 /* Extend the face to the end of the line. */
19831 extend_face_to_end_of_line (it);
19832
19833 /* Make sure we have the position. */
19834 if (used_before == 0)
19835 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19836
19837 /* Record the position of the newline, for use in
19838 find_row_edges. */
19839 it->eol_pos = it->current.pos;
19840
19841 /* Consume the line end. This skips over invisible lines. */
19842 set_iterator_to_next (it, 1);
19843 it->continuation_lines_width = 0;
19844 break;
19845 }
19846
19847 /* Proceed with next display element. Note that this skips
19848 over lines invisible because of selective display. */
19849 set_iterator_to_next (it, 1);
19850
19851 /* If we truncate lines, we are done when the last displayed
19852 glyphs reach past the right margin of the window. */
19853 if (it->line_wrap == TRUNCATE
19854 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19855 ? (it->current_x >= it->last_visible_x)
19856 : (it->current_x > it->last_visible_x)))
19857 {
19858 /* Maybe add truncation glyphs. */
19859 if (!FRAME_WINDOW_P (it->f)
19860 || (row->reversed_p
19861 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19862 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19863 {
19864 int i, n;
19865
19866 if (!row->reversed_p)
19867 {
19868 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19869 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19870 break;
19871 }
19872 else
19873 {
19874 for (i = 0; i < row->used[TEXT_AREA]; i++)
19875 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19876 break;
19877 /* Remove any padding glyphs at the front of ROW, to
19878 make room for the truncation glyphs we will be
19879 adding below. The loop below always inserts at
19880 least one truncation glyph, so also remove the
19881 last glyph added to ROW. */
19882 unproduce_glyphs (it, i + 1);
19883 /* Adjust i for the loop below. */
19884 i = row->used[TEXT_AREA] - (i + 1);
19885 }
19886
19887 it->current_x = x_before;
19888 if (!FRAME_WINDOW_P (it->f))
19889 {
19890 for (n = row->used[TEXT_AREA]; i < n; ++i)
19891 {
19892 row->used[TEXT_AREA] = i;
19893 produce_special_glyphs (it, IT_TRUNCATION);
19894 }
19895 }
19896 else
19897 {
19898 row->used[TEXT_AREA] = i;
19899 produce_special_glyphs (it, IT_TRUNCATION);
19900 }
19901 }
19902 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19903 {
19904 /* Don't truncate if we can overflow newline into fringe. */
19905 if (!get_next_display_element (it))
19906 {
19907 it->continuation_lines_width = 0;
19908 row->ends_at_zv_p = 1;
19909 row->exact_window_width_line_p = 1;
19910 break;
19911 }
19912 if (ITERATOR_AT_END_OF_LINE_P (it))
19913 {
19914 row->exact_window_width_line_p = 1;
19915 goto at_end_of_line;
19916 }
19917 it->current_x = x_before;
19918 }
19919
19920 row->truncated_on_right_p = 1;
19921 it->continuation_lines_width = 0;
19922 reseat_at_next_visible_line_start (it, 0);
19923 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19924 it->hpos = hpos_before;
19925 break;
19926 }
19927 }
19928
19929 if (wrap_data)
19930 bidi_unshelve_cache (wrap_data, 1);
19931
19932 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19933 at the left window margin. */
19934 if (it->first_visible_x
19935 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19936 {
19937 if (!FRAME_WINDOW_P (it->f)
19938 || (row->reversed_p
19939 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19940 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19941 insert_left_trunc_glyphs (it);
19942 row->truncated_on_left_p = 1;
19943 }
19944
19945 /* Remember the position at which this line ends.
19946
19947 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19948 cannot be before the call to find_row_edges below, since that is
19949 where these positions are determined. */
19950 row->end = it->current;
19951 if (!it->bidi_p)
19952 {
19953 row->minpos = row->start.pos;
19954 row->maxpos = row->end.pos;
19955 }
19956 else
19957 {
19958 /* ROW->minpos and ROW->maxpos must be the smallest and
19959 `1 + the largest' buffer positions in ROW. But if ROW was
19960 bidi-reordered, these two positions can be anywhere in the
19961 row, so we must determine them now. */
19962 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19963 }
19964
19965 /* If the start of this line is the overlay arrow-position, then
19966 mark this glyph row as the one containing the overlay arrow.
19967 This is clearly a mess with variable size fonts. It would be
19968 better to let it be displayed like cursors under X. */
19969 if ((row->displays_text_p || !overlay_arrow_seen)
19970 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19971 !NILP (overlay_arrow_string)))
19972 {
19973 /* Overlay arrow in window redisplay is a fringe bitmap. */
19974 if (STRINGP (overlay_arrow_string))
19975 {
19976 struct glyph_row *arrow_row
19977 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19978 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19979 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19980 struct glyph *p = row->glyphs[TEXT_AREA];
19981 struct glyph *p2, *end;
19982
19983 /* Copy the arrow glyphs. */
19984 while (glyph < arrow_end)
19985 *p++ = *glyph++;
19986
19987 /* Throw away padding glyphs. */
19988 p2 = p;
19989 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19990 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19991 ++p2;
19992 if (p2 > p)
19993 {
19994 while (p2 < end)
19995 *p++ = *p2++;
19996 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19997 }
19998 }
19999 else
20000 {
20001 eassert (INTEGERP (overlay_arrow_string));
20002 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20003 }
20004 overlay_arrow_seen = 1;
20005 }
20006
20007 /* Highlight trailing whitespace. */
20008 if (!NILP (Vshow_trailing_whitespace))
20009 highlight_trailing_whitespace (it->f, it->glyph_row);
20010
20011 /* Compute pixel dimensions of this line. */
20012 compute_line_metrics (it);
20013
20014 /* Implementation note: No changes in the glyphs of ROW or in their
20015 faces can be done past this point, because compute_line_metrics
20016 computes ROW's hash value and stores it within the glyph_row
20017 structure. */
20018
20019 /* Record whether this row ends inside an ellipsis. */
20020 row->ends_in_ellipsis_p
20021 = (it->method == GET_FROM_DISPLAY_VECTOR
20022 && it->ellipsis_p);
20023
20024 /* Save fringe bitmaps in this row. */
20025 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20026 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20027 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20028 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20029
20030 it->left_user_fringe_bitmap = 0;
20031 it->left_user_fringe_face_id = 0;
20032 it->right_user_fringe_bitmap = 0;
20033 it->right_user_fringe_face_id = 0;
20034
20035 /* Maybe set the cursor. */
20036 cvpos = it->w->cursor.vpos;
20037 if ((cvpos < 0
20038 /* In bidi-reordered rows, keep checking for proper cursor
20039 position even if one has been found already, because buffer
20040 positions in such rows change non-linearly with ROW->VPOS,
20041 when a line is continued. One exception: when we are at ZV,
20042 display cursor on the first suitable glyph row, since all
20043 the empty rows after that also have their position set to ZV. */
20044 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20045 lines' rows is implemented for bidi-reordered rows. */
20046 || (it->bidi_p
20047 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20048 && PT >= MATRIX_ROW_START_CHARPOS (row)
20049 && PT <= MATRIX_ROW_END_CHARPOS (row)
20050 && cursor_row_p (row))
20051 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20052
20053 /* Prepare for the next line. This line starts horizontally at (X
20054 HPOS) = (0 0). Vertical positions are incremented. As a
20055 convenience for the caller, IT->glyph_row is set to the next
20056 row to be used. */
20057 it->current_x = it->hpos = 0;
20058 it->current_y += row->height;
20059 SET_TEXT_POS (it->eol_pos, 0, 0);
20060 ++it->vpos;
20061 ++it->glyph_row;
20062 /* The next row should by default use the same value of the
20063 reversed_p flag as this one. set_iterator_to_next decides when
20064 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20065 the flag accordingly. */
20066 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20067 it->glyph_row->reversed_p = row->reversed_p;
20068 it->start = row->end;
20069 return row->displays_text_p;
20070
20071 #undef RECORD_MAX_MIN_POS
20072 }
20073
20074 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20075 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20076 doc: /* Return paragraph direction at point in BUFFER.
20077 Value is either `left-to-right' or `right-to-left'.
20078 If BUFFER is omitted or nil, it defaults to the current buffer.
20079
20080 Paragraph direction determines how the text in the paragraph is displayed.
20081 In left-to-right paragraphs, text begins at the left margin of the window
20082 and the reading direction is generally left to right. In right-to-left
20083 paragraphs, text begins at the right margin and is read from right to left.
20084
20085 See also `bidi-paragraph-direction'. */)
20086 (Lisp_Object buffer)
20087 {
20088 struct buffer *buf = current_buffer;
20089 struct buffer *old = buf;
20090
20091 if (! NILP (buffer))
20092 {
20093 CHECK_BUFFER (buffer);
20094 buf = XBUFFER (buffer);
20095 }
20096
20097 if (NILP (BVAR (buf, bidi_display_reordering))
20098 || NILP (BVAR (buf, enable_multibyte_characters))
20099 /* When we are loading loadup.el, the character property tables
20100 needed for bidi iteration are not yet available. */
20101 || !NILP (Vpurify_flag))
20102 return Qleft_to_right;
20103 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20104 return BVAR (buf, bidi_paragraph_direction);
20105 else
20106 {
20107 /* Determine the direction from buffer text. We could try to
20108 use current_matrix if it is up to date, but this seems fast
20109 enough as it is. */
20110 struct bidi_it itb;
20111 ptrdiff_t pos = BUF_PT (buf);
20112 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20113 int c;
20114 void *itb_data = bidi_shelve_cache ();
20115
20116 set_buffer_temp (buf);
20117 /* bidi_paragraph_init finds the base direction of the paragraph
20118 by searching forward from paragraph start. We need the base
20119 direction of the current or _previous_ paragraph, so we need
20120 to make sure we are within that paragraph. To that end, find
20121 the previous non-empty line. */
20122 if (pos >= ZV && pos > BEGV)
20123 {
20124 pos--;
20125 bytepos = CHAR_TO_BYTE (pos);
20126 }
20127 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20128 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20129 {
20130 while ((c = FETCH_BYTE (bytepos)) == '\n'
20131 || c == ' ' || c == '\t' || c == '\f')
20132 {
20133 if (bytepos <= BEGV_BYTE)
20134 break;
20135 bytepos--;
20136 pos--;
20137 }
20138 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20139 bytepos--;
20140 }
20141 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20142 itb.paragraph_dir = NEUTRAL_DIR;
20143 itb.string.s = NULL;
20144 itb.string.lstring = Qnil;
20145 itb.string.bufpos = 0;
20146 itb.string.unibyte = 0;
20147 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20148 bidi_unshelve_cache (itb_data, 0);
20149 set_buffer_temp (old);
20150 switch (itb.paragraph_dir)
20151 {
20152 case L2R:
20153 return Qleft_to_right;
20154 break;
20155 case R2L:
20156 return Qright_to_left;
20157 break;
20158 default:
20159 emacs_abort ();
20160 }
20161 }
20162 }
20163
20164
20165 \f
20166 /***********************************************************************
20167 Menu Bar
20168 ***********************************************************************/
20169
20170 /* Redisplay the menu bar in the frame for window W.
20171
20172 The menu bar of X frames that don't have X toolkit support is
20173 displayed in a special window W->frame->menu_bar_window.
20174
20175 The menu bar of terminal frames is treated specially as far as
20176 glyph matrices are concerned. Menu bar lines are not part of
20177 windows, so the update is done directly on the frame matrix rows
20178 for the menu bar. */
20179
20180 static void
20181 display_menu_bar (struct window *w)
20182 {
20183 struct frame *f = XFRAME (WINDOW_FRAME (w));
20184 struct it it;
20185 Lisp_Object items;
20186 int i;
20187
20188 /* Don't do all this for graphical frames. */
20189 #ifdef HAVE_NTGUI
20190 if (FRAME_W32_P (f))
20191 return;
20192 #endif
20193 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20194 if (FRAME_X_P (f))
20195 return;
20196 #endif
20197
20198 #ifdef HAVE_NS
20199 if (FRAME_NS_P (f))
20200 return;
20201 #endif /* HAVE_NS */
20202
20203 #ifdef USE_X_TOOLKIT
20204 eassert (!FRAME_WINDOW_P (f));
20205 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20206 it.first_visible_x = 0;
20207 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20208 #else /* not USE_X_TOOLKIT */
20209 if (FRAME_WINDOW_P (f))
20210 {
20211 /* Menu bar lines are displayed in the desired matrix of the
20212 dummy window menu_bar_window. */
20213 struct window *menu_w;
20214 eassert (WINDOWP (f->menu_bar_window));
20215 menu_w = XWINDOW (f->menu_bar_window);
20216 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20217 MENU_FACE_ID);
20218 it.first_visible_x = 0;
20219 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20220 }
20221 else
20222 {
20223 /* This is a TTY frame, i.e. character hpos/vpos are used as
20224 pixel x/y. */
20225 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20226 MENU_FACE_ID);
20227 it.first_visible_x = 0;
20228 it.last_visible_x = FRAME_COLS (f);
20229 }
20230 #endif /* not USE_X_TOOLKIT */
20231
20232 /* FIXME: This should be controlled by a user option. See the
20233 comments in redisplay_tool_bar and display_mode_line about
20234 this. */
20235 it.paragraph_embedding = L2R;
20236
20237 /* Clear all rows of the menu bar. */
20238 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20239 {
20240 struct glyph_row *row = it.glyph_row + i;
20241 clear_glyph_row (row);
20242 row->enabled_p = 1;
20243 row->full_width_p = 1;
20244 }
20245
20246 /* Display all items of the menu bar. */
20247 items = FRAME_MENU_BAR_ITEMS (it.f);
20248 for (i = 0; i < ASIZE (items); i += 4)
20249 {
20250 Lisp_Object string;
20251
20252 /* Stop at nil string. */
20253 string = AREF (items, i + 1);
20254 if (NILP (string))
20255 break;
20256
20257 /* Remember where item was displayed. */
20258 ASET (items, i + 3, make_number (it.hpos));
20259
20260 /* Display the item, pad with one space. */
20261 if (it.current_x < it.last_visible_x)
20262 display_string (NULL, string, Qnil, 0, 0, &it,
20263 SCHARS (string) + 1, 0, 0, -1);
20264 }
20265
20266 /* Fill out the line with spaces. */
20267 if (it.current_x < it.last_visible_x)
20268 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20269
20270 /* Compute the total height of the lines. */
20271 compute_line_metrics (&it);
20272 }
20273
20274
20275 \f
20276 /***********************************************************************
20277 Mode Line
20278 ***********************************************************************/
20279
20280 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20281 FORCE is non-zero, redisplay mode lines unconditionally.
20282 Otherwise, redisplay only mode lines that are garbaged. Value is
20283 the number of windows whose mode lines were redisplayed. */
20284
20285 static int
20286 redisplay_mode_lines (Lisp_Object window, int force)
20287 {
20288 int nwindows = 0;
20289
20290 while (!NILP (window))
20291 {
20292 struct window *w = XWINDOW (window);
20293
20294 if (WINDOWP (w->hchild))
20295 nwindows += redisplay_mode_lines (w->hchild, force);
20296 else if (WINDOWP (w->vchild))
20297 nwindows += redisplay_mode_lines (w->vchild, force);
20298 else if (force
20299 || FRAME_GARBAGED_P (XFRAME (w->frame))
20300 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20301 {
20302 struct text_pos lpoint;
20303 struct buffer *old = current_buffer;
20304
20305 /* Set the window's buffer for the mode line display. */
20306 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20307 set_buffer_internal_1 (XBUFFER (w->buffer));
20308
20309 /* Point refers normally to the selected window. For any
20310 other window, set up appropriate value. */
20311 if (!EQ (window, selected_window))
20312 {
20313 struct text_pos pt;
20314
20315 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20316 if (CHARPOS (pt) < BEGV)
20317 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20318 else if (CHARPOS (pt) > (ZV - 1))
20319 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20320 else
20321 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20322 }
20323
20324 /* Display mode lines. */
20325 clear_glyph_matrix (w->desired_matrix);
20326 if (display_mode_lines (w))
20327 {
20328 ++nwindows;
20329 w->must_be_updated_p = 1;
20330 }
20331
20332 /* Restore old settings. */
20333 set_buffer_internal_1 (old);
20334 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20335 }
20336
20337 window = w->next;
20338 }
20339
20340 return nwindows;
20341 }
20342
20343
20344 /* Display the mode and/or header line of window W. Value is the
20345 sum number of mode lines and header lines displayed. */
20346
20347 static int
20348 display_mode_lines (struct window *w)
20349 {
20350 Lisp_Object old_selected_window, old_selected_frame;
20351 int n = 0;
20352
20353 old_selected_frame = selected_frame;
20354 selected_frame = w->frame;
20355 old_selected_window = selected_window;
20356 XSETWINDOW (selected_window, w);
20357
20358 /* These will be set while the mode line specs are processed. */
20359 line_number_displayed = 0;
20360 wset_column_number_displayed (w, Qnil);
20361
20362 if (WINDOW_WANTS_MODELINE_P (w))
20363 {
20364 struct window *sel_w = XWINDOW (old_selected_window);
20365
20366 /* Select mode line face based on the real selected window. */
20367 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20368 BVAR (current_buffer, mode_line_format));
20369 ++n;
20370 }
20371
20372 if (WINDOW_WANTS_HEADER_LINE_P (w))
20373 {
20374 display_mode_line (w, HEADER_LINE_FACE_ID,
20375 BVAR (current_buffer, header_line_format));
20376 ++n;
20377 }
20378
20379 selected_frame = old_selected_frame;
20380 selected_window = old_selected_window;
20381 return n;
20382 }
20383
20384
20385 /* Display mode or header line of window W. FACE_ID specifies which
20386 line to display; it is either MODE_LINE_FACE_ID or
20387 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20388 display. Value is the pixel height of the mode/header line
20389 displayed. */
20390
20391 static int
20392 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20393 {
20394 struct it it;
20395 struct face *face;
20396 ptrdiff_t count = SPECPDL_INDEX ();
20397
20398 init_iterator (&it, w, -1, -1, NULL, face_id);
20399 /* Don't extend on a previously drawn mode-line.
20400 This may happen if called from pos_visible_p. */
20401 it.glyph_row->enabled_p = 0;
20402 prepare_desired_row (it.glyph_row);
20403
20404 it.glyph_row->mode_line_p = 1;
20405
20406 /* FIXME: This should be controlled by a user option. But
20407 supporting such an option is not trivial, since the mode line is
20408 made up of many separate strings. */
20409 it.paragraph_embedding = L2R;
20410
20411 record_unwind_protect (unwind_format_mode_line,
20412 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20413
20414 mode_line_target = MODE_LINE_DISPLAY;
20415
20416 /* Temporarily make frame's keyboard the current kboard so that
20417 kboard-local variables in the mode_line_format will get the right
20418 values. */
20419 push_kboard (FRAME_KBOARD (it.f));
20420 record_unwind_save_match_data ();
20421 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20422 pop_kboard ();
20423
20424 unbind_to (count, Qnil);
20425
20426 /* Fill up with spaces. */
20427 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20428
20429 compute_line_metrics (&it);
20430 it.glyph_row->full_width_p = 1;
20431 it.glyph_row->continued_p = 0;
20432 it.glyph_row->truncated_on_left_p = 0;
20433 it.glyph_row->truncated_on_right_p = 0;
20434
20435 /* Make a 3D mode-line have a shadow at its right end. */
20436 face = FACE_FROM_ID (it.f, face_id);
20437 extend_face_to_end_of_line (&it);
20438 if (face->box != FACE_NO_BOX)
20439 {
20440 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20441 + it.glyph_row->used[TEXT_AREA] - 1);
20442 last->right_box_line_p = 1;
20443 }
20444
20445 return it.glyph_row->height;
20446 }
20447
20448 /* Move element ELT in LIST to the front of LIST.
20449 Return the updated list. */
20450
20451 static Lisp_Object
20452 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20453 {
20454 register Lisp_Object tail, prev;
20455 register Lisp_Object tem;
20456
20457 tail = list;
20458 prev = Qnil;
20459 while (CONSP (tail))
20460 {
20461 tem = XCAR (tail);
20462
20463 if (EQ (elt, tem))
20464 {
20465 /* Splice out the link TAIL. */
20466 if (NILP (prev))
20467 list = XCDR (tail);
20468 else
20469 Fsetcdr (prev, XCDR (tail));
20470
20471 /* Now make it the first. */
20472 Fsetcdr (tail, list);
20473 return tail;
20474 }
20475 else
20476 prev = tail;
20477 tail = XCDR (tail);
20478 QUIT;
20479 }
20480
20481 /* Not found--return unchanged LIST. */
20482 return list;
20483 }
20484
20485 /* Contribute ELT to the mode line for window IT->w. How it
20486 translates into text depends on its data type.
20487
20488 IT describes the display environment in which we display, as usual.
20489
20490 DEPTH is the depth in recursion. It is used to prevent
20491 infinite recursion here.
20492
20493 FIELD_WIDTH is the number of characters the display of ELT should
20494 occupy in the mode line, and PRECISION is the maximum number of
20495 characters to display from ELT's representation. See
20496 display_string for details.
20497
20498 Returns the hpos of the end of the text generated by ELT.
20499
20500 PROPS is a property list to add to any string we encounter.
20501
20502 If RISKY is nonzero, remove (disregard) any properties in any string
20503 we encounter, and ignore :eval and :propertize.
20504
20505 The global variable `mode_line_target' determines whether the
20506 output is passed to `store_mode_line_noprop',
20507 `store_mode_line_string', or `display_string'. */
20508
20509 static int
20510 display_mode_element (struct it *it, int depth, int field_width, int precision,
20511 Lisp_Object elt, Lisp_Object props, int risky)
20512 {
20513 int n = 0, field, prec;
20514 int literal = 0;
20515
20516 tail_recurse:
20517 if (depth > 100)
20518 elt = build_string ("*too-deep*");
20519
20520 depth++;
20521
20522 switch (XTYPE (elt))
20523 {
20524 case Lisp_String:
20525 {
20526 /* A string: output it and check for %-constructs within it. */
20527 unsigned char c;
20528 ptrdiff_t offset = 0;
20529
20530 if (SCHARS (elt) > 0
20531 && (!NILP (props) || risky))
20532 {
20533 Lisp_Object oprops, aelt;
20534 oprops = Ftext_properties_at (make_number (0), elt);
20535
20536 /* If the starting string's properties are not what
20537 we want, translate the string. Also, if the string
20538 is risky, do that anyway. */
20539
20540 if (NILP (Fequal (props, oprops)) || risky)
20541 {
20542 /* If the starting string has properties,
20543 merge the specified ones onto the existing ones. */
20544 if (! NILP (oprops) && !risky)
20545 {
20546 Lisp_Object tem;
20547
20548 oprops = Fcopy_sequence (oprops);
20549 tem = props;
20550 while (CONSP (tem))
20551 {
20552 oprops = Fplist_put (oprops, XCAR (tem),
20553 XCAR (XCDR (tem)));
20554 tem = XCDR (XCDR (tem));
20555 }
20556 props = oprops;
20557 }
20558
20559 aelt = Fassoc (elt, mode_line_proptrans_alist);
20560 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20561 {
20562 /* AELT is what we want. Move it to the front
20563 without consing. */
20564 elt = XCAR (aelt);
20565 mode_line_proptrans_alist
20566 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20567 }
20568 else
20569 {
20570 Lisp_Object tem;
20571
20572 /* If AELT has the wrong props, it is useless.
20573 so get rid of it. */
20574 if (! NILP (aelt))
20575 mode_line_proptrans_alist
20576 = Fdelq (aelt, mode_line_proptrans_alist);
20577
20578 elt = Fcopy_sequence (elt);
20579 Fset_text_properties (make_number (0), Flength (elt),
20580 props, elt);
20581 /* Add this item to mode_line_proptrans_alist. */
20582 mode_line_proptrans_alist
20583 = Fcons (Fcons (elt, props),
20584 mode_line_proptrans_alist);
20585 /* Truncate mode_line_proptrans_alist
20586 to at most 50 elements. */
20587 tem = Fnthcdr (make_number (50),
20588 mode_line_proptrans_alist);
20589 if (! NILP (tem))
20590 XSETCDR (tem, Qnil);
20591 }
20592 }
20593 }
20594
20595 offset = 0;
20596
20597 if (literal)
20598 {
20599 prec = precision - n;
20600 switch (mode_line_target)
20601 {
20602 case MODE_LINE_NOPROP:
20603 case MODE_LINE_TITLE:
20604 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20605 break;
20606 case MODE_LINE_STRING:
20607 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20608 break;
20609 case MODE_LINE_DISPLAY:
20610 n += display_string (NULL, elt, Qnil, 0, 0, it,
20611 0, prec, 0, STRING_MULTIBYTE (elt));
20612 break;
20613 }
20614
20615 break;
20616 }
20617
20618 /* Handle the non-literal case. */
20619
20620 while ((precision <= 0 || n < precision)
20621 && SREF (elt, offset) != 0
20622 && (mode_line_target != MODE_LINE_DISPLAY
20623 || it->current_x < it->last_visible_x))
20624 {
20625 ptrdiff_t last_offset = offset;
20626
20627 /* Advance to end of string or next format specifier. */
20628 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20629 ;
20630
20631 if (offset - 1 != last_offset)
20632 {
20633 ptrdiff_t nchars, nbytes;
20634
20635 /* Output to end of string or up to '%'. Field width
20636 is length of string. Don't output more than
20637 PRECISION allows us. */
20638 offset--;
20639
20640 prec = c_string_width (SDATA (elt) + last_offset,
20641 offset - last_offset, precision - n,
20642 &nchars, &nbytes);
20643
20644 switch (mode_line_target)
20645 {
20646 case MODE_LINE_NOPROP:
20647 case MODE_LINE_TITLE:
20648 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20649 break;
20650 case MODE_LINE_STRING:
20651 {
20652 ptrdiff_t bytepos = last_offset;
20653 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20654 ptrdiff_t endpos = (precision <= 0
20655 ? string_byte_to_char (elt, offset)
20656 : charpos + nchars);
20657
20658 n += store_mode_line_string (NULL,
20659 Fsubstring (elt, make_number (charpos),
20660 make_number (endpos)),
20661 0, 0, 0, Qnil);
20662 }
20663 break;
20664 case MODE_LINE_DISPLAY:
20665 {
20666 ptrdiff_t bytepos = last_offset;
20667 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20668
20669 if (precision <= 0)
20670 nchars = string_byte_to_char (elt, offset) - charpos;
20671 n += display_string (NULL, elt, Qnil, 0, charpos,
20672 it, 0, nchars, 0,
20673 STRING_MULTIBYTE (elt));
20674 }
20675 break;
20676 }
20677 }
20678 else /* c == '%' */
20679 {
20680 ptrdiff_t percent_position = offset;
20681
20682 /* Get the specified minimum width. Zero means
20683 don't pad. */
20684 field = 0;
20685 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20686 field = field * 10 + c - '0';
20687
20688 /* Don't pad beyond the total padding allowed. */
20689 if (field_width - n > 0 && field > field_width - n)
20690 field = field_width - n;
20691
20692 /* Note that either PRECISION <= 0 or N < PRECISION. */
20693 prec = precision - n;
20694
20695 if (c == 'M')
20696 n += display_mode_element (it, depth, field, prec,
20697 Vglobal_mode_string, props,
20698 risky);
20699 else if (c != 0)
20700 {
20701 int multibyte;
20702 ptrdiff_t bytepos, charpos;
20703 const char *spec;
20704 Lisp_Object string;
20705
20706 bytepos = percent_position;
20707 charpos = (STRING_MULTIBYTE (elt)
20708 ? string_byte_to_char (elt, bytepos)
20709 : bytepos);
20710 spec = decode_mode_spec (it->w, c, field, &string);
20711 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20712
20713 switch (mode_line_target)
20714 {
20715 case MODE_LINE_NOPROP:
20716 case MODE_LINE_TITLE:
20717 n += store_mode_line_noprop (spec, field, prec);
20718 break;
20719 case MODE_LINE_STRING:
20720 {
20721 Lisp_Object tem = build_string (spec);
20722 props = Ftext_properties_at (make_number (charpos), elt);
20723 /* Should only keep face property in props */
20724 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20725 }
20726 break;
20727 case MODE_LINE_DISPLAY:
20728 {
20729 int nglyphs_before, nwritten;
20730
20731 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20732 nwritten = display_string (spec, string, elt,
20733 charpos, 0, it,
20734 field, prec, 0,
20735 multibyte);
20736
20737 /* Assign to the glyphs written above the
20738 string where the `%x' came from, position
20739 of the `%'. */
20740 if (nwritten > 0)
20741 {
20742 struct glyph *glyph
20743 = (it->glyph_row->glyphs[TEXT_AREA]
20744 + nglyphs_before);
20745 int i;
20746
20747 for (i = 0; i < nwritten; ++i)
20748 {
20749 glyph[i].object = elt;
20750 glyph[i].charpos = charpos;
20751 }
20752
20753 n += nwritten;
20754 }
20755 }
20756 break;
20757 }
20758 }
20759 else /* c == 0 */
20760 break;
20761 }
20762 }
20763 }
20764 break;
20765
20766 case Lisp_Symbol:
20767 /* A symbol: process the value of the symbol recursively
20768 as if it appeared here directly. Avoid error if symbol void.
20769 Special case: if value of symbol is a string, output the string
20770 literally. */
20771 {
20772 register Lisp_Object tem;
20773
20774 /* If the variable is not marked as risky to set
20775 then its contents are risky to use. */
20776 if (NILP (Fget (elt, Qrisky_local_variable)))
20777 risky = 1;
20778
20779 tem = Fboundp (elt);
20780 if (!NILP (tem))
20781 {
20782 tem = Fsymbol_value (elt);
20783 /* If value is a string, output that string literally:
20784 don't check for % within it. */
20785 if (STRINGP (tem))
20786 literal = 1;
20787
20788 if (!EQ (tem, elt))
20789 {
20790 /* Give up right away for nil or t. */
20791 elt = tem;
20792 goto tail_recurse;
20793 }
20794 }
20795 }
20796 break;
20797
20798 case Lisp_Cons:
20799 {
20800 register Lisp_Object car, tem;
20801
20802 /* A cons cell: five distinct cases.
20803 If first element is :eval or :propertize, do something special.
20804 If first element is a string or a cons, process all the elements
20805 and effectively concatenate them.
20806 If first element is a negative number, truncate displaying cdr to
20807 at most that many characters. If positive, pad (with spaces)
20808 to at least that many characters.
20809 If first element is a symbol, process the cadr or caddr recursively
20810 according to whether the symbol's value is non-nil or nil. */
20811 car = XCAR (elt);
20812 if (EQ (car, QCeval))
20813 {
20814 /* An element of the form (:eval FORM) means evaluate FORM
20815 and use the result as mode line elements. */
20816
20817 if (risky)
20818 break;
20819
20820 if (CONSP (XCDR (elt)))
20821 {
20822 Lisp_Object spec;
20823 spec = safe_eval (XCAR (XCDR (elt)));
20824 n += display_mode_element (it, depth, field_width - n,
20825 precision - n, spec, props,
20826 risky);
20827 }
20828 }
20829 else if (EQ (car, QCpropertize))
20830 {
20831 /* An element of the form (:propertize ELT PROPS...)
20832 means display ELT but applying properties PROPS. */
20833
20834 if (risky)
20835 break;
20836
20837 if (CONSP (XCDR (elt)))
20838 n += display_mode_element (it, depth, field_width - n,
20839 precision - n, XCAR (XCDR (elt)),
20840 XCDR (XCDR (elt)), risky);
20841 }
20842 else if (SYMBOLP (car))
20843 {
20844 tem = Fboundp (car);
20845 elt = XCDR (elt);
20846 if (!CONSP (elt))
20847 goto invalid;
20848 /* elt is now the cdr, and we know it is a cons cell.
20849 Use its car if CAR has a non-nil value. */
20850 if (!NILP (tem))
20851 {
20852 tem = Fsymbol_value (car);
20853 if (!NILP (tem))
20854 {
20855 elt = XCAR (elt);
20856 goto tail_recurse;
20857 }
20858 }
20859 /* Symbol's value is nil (or symbol is unbound)
20860 Get the cddr of the original list
20861 and if possible find the caddr and use that. */
20862 elt = XCDR (elt);
20863 if (NILP (elt))
20864 break;
20865 else if (!CONSP (elt))
20866 goto invalid;
20867 elt = XCAR (elt);
20868 goto tail_recurse;
20869 }
20870 else if (INTEGERP (car))
20871 {
20872 register int lim = XINT (car);
20873 elt = XCDR (elt);
20874 if (lim < 0)
20875 {
20876 /* Negative int means reduce maximum width. */
20877 if (precision <= 0)
20878 precision = -lim;
20879 else
20880 precision = min (precision, -lim);
20881 }
20882 else if (lim > 0)
20883 {
20884 /* Padding specified. Don't let it be more than
20885 current maximum. */
20886 if (precision > 0)
20887 lim = min (precision, lim);
20888
20889 /* If that's more padding than already wanted, queue it.
20890 But don't reduce padding already specified even if
20891 that is beyond the current truncation point. */
20892 field_width = max (lim, field_width);
20893 }
20894 goto tail_recurse;
20895 }
20896 else if (STRINGP (car) || CONSP (car))
20897 {
20898 Lisp_Object halftail = elt;
20899 int len = 0;
20900
20901 while (CONSP (elt)
20902 && (precision <= 0 || n < precision))
20903 {
20904 n += display_mode_element (it, depth,
20905 /* Do padding only after the last
20906 element in the list. */
20907 (! CONSP (XCDR (elt))
20908 ? field_width - n
20909 : 0),
20910 precision - n, XCAR (elt),
20911 props, risky);
20912 elt = XCDR (elt);
20913 len++;
20914 if ((len & 1) == 0)
20915 halftail = XCDR (halftail);
20916 /* Check for cycle. */
20917 if (EQ (halftail, elt))
20918 break;
20919 }
20920 }
20921 }
20922 break;
20923
20924 default:
20925 invalid:
20926 elt = build_string ("*invalid*");
20927 goto tail_recurse;
20928 }
20929
20930 /* Pad to FIELD_WIDTH. */
20931 if (field_width > 0 && n < field_width)
20932 {
20933 switch (mode_line_target)
20934 {
20935 case MODE_LINE_NOPROP:
20936 case MODE_LINE_TITLE:
20937 n += store_mode_line_noprop ("", field_width - n, 0);
20938 break;
20939 case MODE_LINE_STRING:
20940 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20941 break;
20942 case MODE_LINE_DISPLAY:
20943 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20944 0, 0, 0);
20945 break;
20946 }
20947 }
20948
20949 return n;
20950 }
20951
20952 /* Store a mode-line string element in mode_line_string_list.
20953
20954 If STRING is non-null, display that C string. Otherwise, the Lisp
20955 string LISP_STRING is displayed.
20956
20957 FIELD_WIDTH is the minimum number of output glyphs to produce.
20958 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20959 with spaces. FIELD_WIDTH <= 0 means don't pad.
20960
20961 PRECISION is the maximum number of characters to output from
20962 STRING. PRECISION <= 0 means don't truncate the string.
20963
20964 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20965 properties to the string.
20966
20967 PROPS are the properties to add to the string.
20968 The mode_line_string_face face property is always added to the string.
20969 */
20970
20971 static int
20972 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20973 int field_width, int precision, Lisp_Object props)
20974 {
20975 ptrdiff_t len;
20976 int n = 0;
20977
20978 if (string != NULL)
20979 {
20980 len = strlen (string);
20981 if (precision > 0 && len > precision)
20982 len = precision;
20983 lisp_string = make_string (string, len);
20984 if (NILP (props))
20985 props = mode_line_string_face_prop;
20986 else if (!NILP (mode_line_string_face))
20987 {
20988 Lisp_Object face = Fplist_get (props, Qface);
20989 props = Fcopy_sequence (props);
20990 if (NILP (face))
20991 face = mode_line_string_face;
20992 else
20993 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20994 props = Fplist_put (props, Qface, face);
20995 }
20996 Fadd_text_properties (make_number (0), make_number (len),
20997 props, lisp_string);
20998 }
20999 else
21000 {
21001 len = XFASTINT (Flength (lisp_string));
21002 if (precision > 0 && len > precision)
21003 {
21004 len = precision;
21005 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21006 precision = -1;
21007 }
21008 if (!NILP (mode_line_string_face))
21009 {
21010 Lisp_Object face;
21011 if (NILP (props))
21012 props = Ftext_properties_at (make_number (0), lisp_string);
21013 face = Fplist_get (props, Qface);
21014 if (NILP (face))
21015 face = mode_line_string_face;
21016 else
21017 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21018 props = Fcons (Qface, Fcons (face, Qnil));
21019 if (copy_string)
21020 lisp_string = Fcopy_sequence (lisp_string);
21021 }
21022 if (!NILP (props))
21023 Fadd_text_properties (make_number (0), make_number (len),
21024 props, lisp_string);
21025 }
21026
21027 if (len > 0)
21028 {
21029 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21030 n += len;
21031 }
21032
21033 if (field_width > len)
21034 {
21035 field_width -= len;
21036 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21037 if (!NILP (props))
21038 Fadd_text_properties (make_number (0), make_number (field_width),
21039 props, lisp_string);
21040 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21041 n += field_width;
21042 }
21043
21044 return n;
21045 }
21046
21047
21048 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21049 1, 4, 0,
21050 doc: /* Format a string out of a mode line format specification.
21051 First arg FORMAT specifies the mode line format (see `mode-line-format'
21052 for details) to use.
21053
21054 By default, the format is evaluated for the currently selected window.
21055
21056 Optional second arg FACE specifies the face property to put on all
21057 characters for which no face is specified. The value nil means the
21058 default face. The value t means whatever face the window's mode line
21059 currently uses (either `mode-line' or `mode-line-inactive',
21060 depending on whether the window is the selected window or not).
21061 An integer value means the value string has no text
21062 properties.
21063
21064 Optional third and fourth args WINDOW and BUFFER specify the window
21065 and buffer to use as the context for the formatting (defaults
21066 are the selected window and the WINDOW's buffer). */)
21067 (Lisp_Object format, Lisp_Object face,
21068 Lisp_Object window, Lisp_Object buffer)
21069 {
21070 struct it it;
21071 int len;
21072 struct window *w;
21073 struct buffer *old_buffer = NULL;
21074 int face_id;
21075 int no_props = INTEGERP (face);
21076 ptrdiff_t count = SPECPDL_INDEX ();
21077 Lisp_Object str;
21078 int string_start = 0;
21079
21080 w = decode_any_window (window);
21081 XSETWINDOW (window, w);
21082
21083 if (NILP (buffer))
21084 buffer = w->buffer;
21085 CHECK_BUFFER (buffer);
21086
21087 /* Make formatting the modeline a non-op when noninteractive, otherwise
21088 there will be problems later caused by a partially initialized frame. */
21089 if (NILP (format) || noninteractive)
21090 return empty_unibyte_string;
21091
21092 if (no_props)
21093 face = Qnil;
21094
21095 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21096 : EQ (face, Qt) ? (EQ (window, selected_window)
21097 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21098 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21099 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21100 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21101 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21102 : DEFAULT_FACE_ID;
21103
21104 old_buffer = current_buffer;
21105
21106 /* Save things including mode_line_proptrans_alist,
21107 and set that to nil so that we don't alter the outer value. */
21108 record_unwind_protect (unwind_format_mode_line,
21109 format_mode_line_unwind_data
21110 (XFRAME (WINDOW_FRAME (w)),
21111 old_buffer, selected_window, 1));
21112 mode_line_proptrans_alist = Qnil;
21113
21114 Fselect_window (window, Qt);
21115 set_buffer_internal_1 (XBUFFER (buffer));
21116
21117 init_iterator (&it, w, -1, -1, NULL, face_id);
21118
21119 if (no_props)
21120 {
21121 mode_line_target = MODE_LINE_NOPROP;
21122 mode_line_string_face_prop = Qnil;
21123 mode_line_string_list = Qnil;
21124 string_start = MODE_LINE_NOPROP_LEN (0);
21125 }
21126 else
21127 {
21128 mode_line_target = MODE_LINE_STRING;
21129 mode_line_string_list = Qnil;
21130 mode_line_string_face = face;
21131 mode_line_string_face_prop
21132 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21133 }
21134
21135 push_kboard (FRAME_KBOARD (it.f));
21136 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21137 pop_kboard ();
21138
21139 if (no_props)
21140 {
21141 len = MODE_LINE_NOPROP_LEN (string_start);
21142 str = make_string (mode_line_noprop_buf + string_start, len);
21143 }
21144 else
21145 {
21146 mode_line_string_list = Fnreverse (mode_line_string_list);
21147 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21148 empty_unibyte_string);
21149 }
21150
21151 unbind_to (count, Qnil);
21152 return str;
21153 }
21154
21155 /* Write a null-terminated, right justified decimal representation of
21156 the positive integer D to BUF using a minimal field width WIDTH. */
21157
21158 static void
21159 pint2str (register char *buf, register int width, register ptrdiff_t d)
21160 {
21161 register char *p = buf;
21162
21163 if (d <= 0)
21164 *p++ = '0';
21165 else
21166 {
21167 while (d > 0)
21168 {
21169 *p++ = d % 10 + '0';
21170 d /= 10;
21171 }
21172 }
21173
21174 for (width -= (int) (p - buf); width > 0; --width)
21175 *p++ = ' ';
21176 *p-- = '\0';
21177 while (p > buf)
21178 {
21179 d = *buf;
21180 *buf++ = *p;
21181 *p-- = d;
21182 }
21183 }
21184
21185 /* Write a null-terminated, right justified decimal and "human
21186 readable" representation of the nonnegative integer D to BUF using
21187 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21188
21189 static const char power_letter[] =
21190 {
21191 0, /* no letter */
21192 'k', /* kilo */
21193 'M', /* mega */
21194 'G', /* giga */
21195 'T', /* tera */
21196 'P', /* peta */
21197 'E', /* exa */
21198 'Z', /* zetta */
21199 'Y' /* yotta */
21200 };
21201
21202 static void
21203 pint2hrstr (char *buf, int width, ptrdiff_t d)
21204 {
21205 /* We aim to represent the nonnegative integer D as
21206 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21207 ptrdiff_t quotient = d;
21208 int remainder = 0;
21209 /* -1 means: do not use TENTHS. */
21210 int tenths = -1;
21211 int exponent = 0;
21212
21213 /* Length of QUOTIENT.TENTHS as a string. */
21214 int length;
21215
21216 char * psuffix;
21217 char * p;
21218
21219 if (1000 <= quotient)
21220 {
21221 /* Scale to the appropriate EXPONENT. */
21222 do
21223 {
21224 remainder = quotient % 1000;
21225 quotient /= 1000;
21226 exponent++;
21227 }
21228 while (1000 <= quotient);
21229
21230 /* Round to nearest and decide whether to use TENTHS or not. */
21231 if (quotient <= 9)
21232 {
21233 tenths = remainder / 100;
21234 if (50 <= remainder % 100)
21235 {
21236 if (tenths < 9)
21237 tenths++;
21238 else
21239 {
21240 quotient++;
21241 if (quotient == 10)
21242 tenths = -1;
21243 else
21244 tenths = 0;
21245 }
21246 }
21247 }
21248 else
21249 if (500 <= remainder)
21250 {
21251 if (quotient < 999)
21252 quotient++;
21253 else
21254 {
21255 quotient = 1;
21256 exponent++;
21257 tenths = 0;
21258 }
21259 }
21260 }
21261
21262 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21263 if (tenths == -1 && quotient <= 99)
21264 if (quotient <= 9)
21265 length = 1;
21266 else
21267 length = 2;
21268 else
21269 length = 3;
21270 p = psuffix = buf + max (width, length);
21271
21272 /* Print EXPONENT. */
21273 *psuffix++ = power_letter[exponent];
21274 *psuffix = '\0';
21275
21276 /* Print TENTHS. */
21277 if (tenths >= 0)
21278 {
21279 *--p = '0' + tenths;
21280 *--p = '.';
21281 }
21282
21283 /* Print QUOTIENT. */
21284 do
21285 {
21286 int digit = quotient % 10;
21287 *--p = '0' + digit;
21288 }
21289 while ((quotient /= 10) != 0);
21290
21291 /* Print leading spaces. */
21292 while (buf < p)
21293 *--p = ' ';
21294 }
21295
21296 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21297 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21298 type of CODING_SYSTEM. Return updated pointer into BUF. */
21299
21300 static unsigned char invalid_eol_type[] = "(*invalid*)";
21301
21302 static char *
21303 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21304 {
21305 Lisp_Object val;
21306 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21307 const unsigned char *eol_str;
21308 int eol_str_len;
21309 /* The EOL conversion we are using. */
21310 Lisp_Object eoltype;
21311
21312 val = CODING_SYSTEM_SPEC (coding_system);
21313 eoltype = Qnil;
21314
21315 if (!VECTORP (val)) /* Not yet decided. */
21316 {
21317 *buf++ = multibyte ? '-' : ' ';
21318 if (eol_flag)
21319 eoltype = eol_mnemonic_undecided;
21320 /* Don't mention EOL conversion if it isn't decided. */
21321 }
21322 else
21323 {
21324 Lisp_Object attrs;
21325 Lisp_Object eolvalue;
21326
21327 attrs = AREF (val, 0);
21328 eolvalue = AREF (val, 2);
21329
21330 *buf++ = multibyte
21331 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21332 : ' ';
21333
21334 if (eol_flag)
21335 {
21336 /* The EOL conversion that is normal on this system. */
21337
21338 if (NILP (eolvalue)) /* Not yet decided. */
21339 eoltype = eol_mnemonic_undecided;
21340 else if (VECTORP (eolvalue)) /* Not yet decided. */
21341 eoltype = eol_mnemonic_undecided;
21342 else /* eolvalue is Qunix, Qdos, or Qmac. */
21343 eoltype = (EQ (eolvalue, Qunix)
21344 ? eol_mnemonic_unix
21345 : (EQ (eolvalue, Qdos) == 1
21346 ? eol_mnemonic_dos : eol_mnemonic_mac));
21347 }
21348 }
21349
21350 if (eol_flag)
21351 {
21352 /* Mention the EOL conversion if it is not the usual one. */
21353 if (STRINGP (eoltype))
21354 {
21355 eol_str = SDATA (eoltype);
21356 eol_str_len = SBYTES (eoltype);
21357 }
21358 else if (CHARACTERP (eoltype))
21359 {
21360 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21361 int c = XFASTINT (eoltype);
21362 eol_str_len = CHAR_STRING (c, tmp);
21363 eol_str = tmp;
21364 }
21365 else
21366 {
21367 eol_str = invalid_eol_type;
21368 eol_str_len = sizeof (invalid_eol_type) - 1;
21369 }
21370 memcpy (buf, eol_str, eol_str_len);
21371 buf += eol_str_len;
21372 }
21373
21374 return buf;
21375 }
21376
21377 /* Return a string for the output of a mode line %-spec for window W,
21378 generated by character C. FIELD_WIDTH > 0 means pad the string
21379 returned with spaces to that value. Return a Lisp string in
21380 *STRING if the resulting string is taken from that Lisp string.
21381
21382 Note we operate on the current buffer for most purposes,
21383 the exception being w->base_line_pos. */
21384
21385 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21386
21387 static const char *
21388 decode_mode_spec (struct window *w, register int c, int field_width,
21389 Lisp_Object *string)
21390 {
21391 Lisp_Object obj;
21392 struct frame *f = XFRAME (WINDOW_FRAME (w));
21393 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21394 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21395 produce strings from numerical values, so limit preposterously
21396 large values of FIELD_WIDTH to avoid overrunning the buffer's
21397 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21398 bytes plus the terminating null. */
21399 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21400 struct buffer *b = current_buffer;
21401
21402 obj = Qnil;
21403 *string = Qnil;
21404
21405 switch (c)
21406 {
21407 case '*':
21408 if (!NILP (BVAR (b, read_only)))
21409 return "%";
21410 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21411 return "*";
21412 return "-";
21413
21414 case '+':
21415 /* This differs from %* only for a modified read-only buffer. */
21416 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21417 return "*";
21418 if (!NILP (BVAR (b, read_only)))
21419 return "%";
21420 return "-";
21421
21422 case '&':
21423 /* This differs from %* in ignoring read-only-ness. */
21424 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21425 return "*";
21426 return "-";
21427
21428 case '%':
21429 return "%";
21430
21431 case '[':
21432 {
21433 int i;
21434 char *p;
21435
21436 if (command_loop_level > 5)
21437 return "[[[... ";
21438 p = decode_mode_spec_buf;
21439 for (i = 0; i < command_loop_level; i++)
21440 *p++ = '[';
21441 *p = 0;
21442 return decode_mode_spec_buf;
21443 }
21444
21445 case ']':
21446 {
21447 int i;
21448 char *p;
21449
21450 if (command_loop_level > 5)
21451 return " ...]]]";
21452 p = decode_mode_spec_buf;
21453 for (i = 0; i < command_loop_level; i++)
21454 *p++ = ']';
21455 *p = 0;
21456 return decode_mode_spec_buf;
21457 }
21458
21459 case '-':
21460 {
21461 register int i;
21462
21463 /* Let lots_of_dashes be a string of infinite length. */
21464 if (mode_line_target == MODE_LINE_NOPROP ||
21465 mode_line_target == MODE_LINE_STRING)
21466 return "--";
21467 if (field_width <= 0
21468 || field_width > sizeof (lots_of_dashes))
21469 {
21470 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21471 decode_mode_spec_buf[i] = '-';
21472 decode_mode_spec_buf[i] = '\0';
21473 return decode_mode_spec_buf;
21474 }
21475 else
21476 return lots_of_dashes;
21477 }
21478
21479 case 'b':
21480 obj = BVAR (b, name);
21481 break;
21482
21483 case 'c':
21484 /* %c and %l are ignored in `frame-title-format'.
21485 (In redisplay_internal, the frame title is drawn _before_ the
21486 windows are updated, so the stuff which depends on actual
21487 window contents (such as %l) may fail to render properly, or
21488 even crash emacs.) */
21489 if (mode_line_target == MODE_LINE_TITLE)
21490 return "";
21491 else
21492 {
21493 ptrdiff_t col = current_column ();
21494 wset_column_number_displayed (w, make_number (col));
21495 pint2str (decode_mode_spec_buf, width, col);
21496 return decode_mode_spec_buf;
21497 }
21498
21499 case 'e':
21500 #ifndef SYSTEM_MALLOC
21501 {
21502 if (NILP (Vmemory_full))
21503 return "";
21504 else
21505 return "!MEM FULL! ";
21506 }
21507 #else
21508 return "";
21509 #endif
21510
21511 case 'F':
21512 /* %F displays the frame name. */
21513 if (!NILP (f->title))
21514 return SSDATA (f->title);
21515 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21516 return SSDATA (f->name);
21517 return "Emacs";
21518
21519 case 'f':
21520 obj = BVAR (b, filename);
21521 break;
21522
21523 case 'i':
21524 {
21525 ptrdiff_t size = ZV - BEGV;
21526 pint2str (decode_mode_spec_buf, width, size);
21527 return decode_mode_spec_buf;
21528 }
21529
21530 case 'I':
21531 {
21532 ptrdiff_t size = ZV - BEGV;
21533 pint2hrstr (decode_mode_spec_buf, width, size);
21534 return decode_mode_spec_buf;
21535 }
21536
21537 case 'l':
21538 {
21539 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21540 ptrdiff_t topline, nlines, height;
21541 ptrdiff_t junk;
21542
21543 /* %c and %l are ignored in `frame-title-format'. */
21544 if (mode_line_target == MODE_LINE_TITLE)
21545 return "";
21546
21547 startpos = marker_position (w->start);
21548 startpos_byte = marker_byte_position (w->start);
21549 height = WINDOW_TOTAL_LINES (w);
21550
21551 /* If we decided that this buffer isn't suitable for line numbers,
21552 don't forget that too fast. */
21553 if (EQ (w->base_line_pos, w->buffer))
21554 goto no_value;
21555 /* But do forget it, if the window shows a different buffer now. */
21556 else if (BUFFERP (w->base_line_pos))
21557 wset_base_line_pos (w, Qnil);
21558
21559 /* If the buffer is very big, don't waste time. */
21560 if (INTEGERP (Vline_number_display_limit)
21561 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21562 {
21563 wset_base_line_pos (w, Qnil);
21564 wset_base_line_number (w, Qnil);
21565 goto no_value;
21566 }
21567
21568 if (INTEGERP (w->base_line_number)
21569 && INTEGERP (w->base_line_pos)
21570 && XFASTINT (w->base_line_pos) <= startpos)
21571 {
21572 line = XFASTINT (w->base_line_number);
21573 linepos = XFASTINT (w->base_line_pos);
21574 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21575 }
21576 else
21577 {
21578 line = 1;
21579 linepos = BUF_BEGV (b);
21580 linepos_byte = BUF_BEGV_BYTE (b);
21581 }
21582
21583 /* Count lines from base line to window start position. */
21584 nlines = display_count_lines (linepos_byte,
21585 startpos_byte,
21586 startpos, &junk);
21587
21588 topline = nlines + line;
21589
21590 /* Determine a new base line, if the old one is too close
21591 or too far away, or if we did not have one.
21592 "Too close" means it's plausible a scroll-down would
21593 go back past it. */
21594 if (startpos == BUF_BEGV (b))
21595 {
21596 wset_base_line_number (w, make_number (topline));
21597 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21598 }
21599 else if (nlines < height + 25 || nlines > height * 3 + 50
21600 || linepos == BUF_BEGV (b))
21601 {
21602 ptrdiff_t limit = BUF_BEGV (b);
21603 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21604 ptrdiff_t position;
21605 ptrdiff_t distance =
21606 (height * 2 + 30) * line_number_display_limit_width;
21607
21608 if (startpos - distance > limit)
21609 {
21610 limit = startpos - distance;
21611 limit_byte = CHAR_TO_BYTE (limit);
21612 }
21613
21614 nlines = display_count_lines (startpos_byte,
21615 limit_byte,
21616 - (height * 2 + 30),
21617 &position);
21618 /* If we couldn't find the lines we wanted within
21619 line_number_display_limit_width chars per line,
21620 give up on line numbers for this window. */
21621 if (position == limit_byte && limit == startpos - distance)
21622 {
21623 wset_base_line_pos (w, w->buffer);
21624 wset_base_line_number (w, Qnil);
21625 goto no_value;
21626 }
21627
21628 wset_base_line_number (w, make_number (topline - nlines));
21629 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21630 }
21631
21632 /* Now count lines from the start pos to point. */
21633 nlines = display_count_lines (startpos_byte,
21634 PT_BYTE, PT, &junk);
21635
21636 /* Record that we did display the line number. */
21637 line_number_displayed = 1;
21638
21639 /* Make the string to show. */
21640 pint2str (decode_mode_spec_buf, width, topline + nlines);
21641 return decode_mode_spec_buf;
21642 no_value:
21643 {
21644 char* p = decode_mode_spec_buf;
21645 int pad = width - 2;
21646 while (pad-- > 0)
21647 *p++ = ' ';
21648 *p++ = '?';
21649 *p++ = '?';
21650 *p = '\0';
21651 return decode_mode_spec_buf;
21652 }
21653 }
21654 break;
21655
21656 case 'm':
21657 obj = BVAR (b, mode_name);
21658 break;
21659
21660 case 'n':
21661 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21662 return " Narrow";
21663 break;
21664
21665 case 'p':
21666 {
21667 ptrdiff_t pos = marker_position (w->start);
21668 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21669
21670 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21671 {
21672 if (pos <= BUF_BEGV (b))
21673 return "All";
21674 else
21675 return "Bottom";
21676 }
21677 else if (pos <= BUF_BEGV (b))
21678 return "Top";
21679 else
21680 {
21681 if (total > 1000000)
21682 /* Do it differently for a large value, to avoid overflow. */
21683 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21684 else
21685 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21686 /* We can't normally display a 3-digit number,
21687 so get us a 2-digit number that is close. */
21688 if (total == 100)
21689 total = 99;
21690 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21691 return decode_mode_spec_buf;
21692 }
21693 }
21694
21695 /* Display percentage of size above the bottom of the screen. */
21696 case 'P':
21697 {
21698 ptrdiff_t toppos = marker_position (w->start);
21699 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21700 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21701
21702 if (botpos >= BUF_ZV (b))
21703 {
21704 if (toppos <= BUF_BEGV (b))
21705 return "All";
21706 else
21707 return "Bottom";
21708 }
21709 else
21710 {
21711 if (total > 1000000)
21712 /* Do it differently for a large value, to avoid overflow. */
21713 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21714 else
21715 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21716 /* We can't normally display a 3-digit number,
21717 so get us a 2-digit number that is close. */
21718 if (total == 100)
21719 total = 99;
21720 if (toppos <= BUF_BEGV (b))
21721 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21722 else
21723 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21724 return decode_mode_spec_buf;
21725 }
21726 }
21727
21728 case 's':
21729 /* status of process */
21730 obj = Fget_buffer_process (Fcurrent_buffer ());
21731 if (NILP (obj))
21732 return "no process";
21733 #ifndef MSDOS
21734 obj = Fsymbol_name (Fprocess_status (obj));
21735 #endif
21736 break;
21737
21738 case '@':
21739 {
21740 ptrdiff_t count = inhibit_garbage_collection ();
21741 Lisp_Object val = call1 (intern ("file-remote-p"),
21742 BVAR (current_buffer, directory));
21743 unbind_to (count, Qnil);
21744
21745 if (NILP (val))
21746 return "-";
21747 else
21748 return "@";
21749 }
21750
21751 case 't': /* indicate TEXT or BINARY */
21752 return "T";
21753
21754 case 'z':
21755 /* coding-system (not including end-of-line format) */
21756 case 'Z':
21757 /* coding-system (including end-of-line type) */
21758 {
21759 int eol_flag = (c == 'Z');
21760 char *p = decode_mode_spec_buf;
21761
21762 if (! FRAME_WINDOW_P (f))
21763 {
21764 /* No need to mention EOL here--the terminal never needs
21765 to do EOL conversion. */
21766 p = decode_mode_spec_coding (CODING_ID_NAME
21767 (FRAME_KEYBOARD_CODING (f)->id),
21768 p, 0);
21769 p = decode_mode_spec_coding (CODING_ID_NAME
21770 (FRAME_TERMINAL_CODING (f)->id),
21771 p, 0);
21772 }
21773 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21774 p, eol_flag);
21775
21776 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21777 #ifdef subprocesses
21778 obj = Fget_buffer_process (Fcurrent_buffer ());
21779 if (PROCESSP (obj))
21780 {
21781 p = decode_mode_spec_coding
21782 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21783 p = decode_mode_spec_coding
21784 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21785 }
21786 #endif /* subprocesses */
21787 #endif /* 0 */
21788 *p = 0;
21789 return decode_mode_spec_buf;
21790 }
21791 }
21792
21793 if (STRINGP (obj))
21794 {
21795 *string = obj;
21796 return SSDATA (obj);
21797 }
21798 else
21799 return "";
21800 }
21801
21802
21803 /* Count up to COUNT lines starting from START_BYTE.
21804 But don't go beyond LIMIT_BYTE.
21805 Return the number of lines thus found (always nonnegative).
21806
21807 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21808
21809 static ptrdiff_t
21810 display_count_lines (ptrdiff_t start_byte,
21811 ptrdiff_t limit_byte, ptrdiff_t count,
21812 ptrdiff_t *byte_pos_ptr)
21813 {
21814 register unsigned char *cursor;
21815 unsigned char *base;
21816
21817 register ptrdiff_t ceiling;
21818 register unsigned char *ceiling_addr;
21819 ptrdiff_t orig_count = count;
21820
21821 /* If we are not in selective display mode,
21822 check only for newlines. */
21823 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21824 && !INTEGERP (BVAR (current_buffer, selective_display)));
21825
21826 if (count > 0)
21827 {
21828 while (start_byte < limit_byte)
21829 {
21830 ceiling = BUFFER_CEILING_OF (start_byte);
21831 ceiling = min (limit_byte - 1, ceiling);
21832 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21833 base = (cursor = BYTE_POS_ADDR (start_byte));
21834 while (1)
21835 {
21836 if (selective_display)
21837 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21838 ;
21839 else
21840 while (*cursor != '\n' && ++cursor != ceiling_addr)
21841 ;
21842
21843 if (cursor != ceiling_addr)
21844 {
21845 if (--count == 0)
21846 {
21847 start_byte += cursor - base + 1;
21848 *byte_pos_ptr = start_byte;
21849 return orig_count;
21850 }
21851 else
21852 if (++cursor == ceiling_addr)
21853 break;
21854 }
21855 else
21856 break;
21857 }
21858 start_byte += cursor - base;
21859 }
21860 }
21861 else
21862 {
21863 while (start_byte > limit_byte)
21864 {
21865 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21866 ceiling = max (limit_byte, ceiling);
21867 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21868 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21869 while (1)
21870 {
21871 if (selective_display)
21872 while (--cursor != ceiling_addr
21873 && *cursor != '\n' && *cursor != 015)
21874 ;
21875 else
21876 while (--cursor != ceiling_addr && *cursor != '\n')
21877 ;
21878
21879 if (cursor != ceiling_addr)
21880 {
21881 if (++count == 0)
21882 {
21883 start_byte += cursor - base + 1;
21884 *byte_pos_ptr = start_byte;
21885 /* When scanning backwards, we should
21886 not count the newline posterior to which we stop. */
21887 return - orig_count - 1;
21888 }
21889 }
21890 else
21891 break;
21892 }
21893 /* Here we add 1 to compensate for the last decrement
21894 of CURSOR, which took it past the valid range. */
21895 start_byte += cursor - base + 1;
21896 }
21897 }
21898
21899 *byte_pos_ptr = limit_byte;
21900
21901 if (count < 0)
21902 return - orig_count + count;
21903 return orig_count - count;
21904
21905 }
21906
21907
21908 \f
21909 /***********************************************************************
21910 Displaying strings
21911 ***********************************************************************/
21912
21913 /* Display a NUL-terminated string, starting with index START.
21914
21915 If STRING is non-null, display that C string. Otherwise, the Lisp
21916 string LISP_STRING is displayed. There's a case that STRING is
21917 non-null and LISP_STRING is not nil. It means STRING is a string
21918 data of LISP_STRING. In that case, we display LISP_STRING while
21919 ignoring its text properties.
21920
21921 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21922 FACE_STRING. Display STRING or LISP_STRING with the face at
21923 FACE_STRING_POS in FACE_STRING:
21924
21925 Display the string in the environment given by IT, but use the
21926 standard display table, temporarily.
21927
21928 FIELD_WIDTH is the minimum number of output glyphs to produce.
21929 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21930 with spaces. If STRING has more characters, more than FIELD_WIDTH
21931 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21932
21933 PRECISION is the maximum number of characters to output from
21934 STRING. PRECISION < 0 means don't truncate the string.
21935
21936 This is roughly equivalent to printf format specifiers:
21937
21938 FIELD_WIDTH PRECISION PRINTF
21939 ----------------------------------------
21940 -1 -1 %s
21941 -1 10 %.10s
21942 10 -1 %10s
21943 20 10 %20.10s
21944
21945 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21946 display them, and < 0 means obey the current buffer's value of
21947 enable_multibyte_characters.
21948
21949 Value is the number of columns displayed. */
21950
21951 static int
21952 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21953 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21954 int field_width, int precision, int max_x, int multibyte)
21955 {
21956 int hpos_at_start = it->hpos;
21957 int saved_face_id = it->face_id;
21958 struct glyph_row *row = it->glyph_row;
21959 ptrdiff_t it_charpos;
21960
21961 /* Initialize the iterator IT for iteration over STRING beginning
21962 with index START. */
21963 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21964 precision, field_width, multibyte);
21965 if (string && STRINGP (lisp_string))
21966 /* LISP_STRING is the one returned by decode_mode_spec. We should
21967 ignore its text properties. */
21968 it->stop_charpos = it->end_charpos;
21969
21970 /* If displaying STRING, set up the face of the iterator from
21971 FACE_STRING, if that's given. */
21972 if (STRINGP (face_string))
21973 {
21974 ptrdiff_t endptr;
21975 struct face *face;
21976
21977 it->face_id
21978 = face_at_string_position (it->w, face_string, face_string_pos,
21979 0, it->region_beg_charpos,
21980 it->region_end_charpos,
21981 &endptr, it->base_face_id, 0);
21982 face = FACE_FROM_ID (it->f, it->face_id);
21983 it->face_box_p = face->box != FACE_NO_BOX;
21984 }
21985
21986 /* Set max_x to the maximum allowed X position. Don't let it go
21987 beyond the right edge of the window. */
21988 if (max_x <= 0)
21989 max_x = it->last_visible_x;
21990 else
21991 max_x = min (max_x, it->last_visible_x);
21992
21993 /* Skip over display elements that are not visible. because IT->w is
21994 hscrolled. */
21995 if (it->current_x < it->first_visible_x)
21996 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21997 MOVE_TO_POS | MOVE_TO_X);
21998
21999 row->ascent = it->max_ascent;
22000 row->height = it->max_ascent + it->max_descent;
22001 row->phys_ascent = it->max_phys_ascent;
22002 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22003 row->extra_line_spacing = it->max_extra_line_spacing;
22004
22005 if (STRINGP (it->string))
22006 it_charpos = IT_STRING_CHARPOS (*it);
22007 else
22008 it_charpos = IT_CHARPOS (*it);
22009
22010 /* This condition is for the case that we are called with current_x
22011 past last_visible_x. */
22012 while (it->current_x < max_x)
22013 {
22014 int x_before, x, n_glyphs_before, i, nglyphs;
22015
22016 /* Get the next display element. */
22017 if (!get_next_display_element (it))
22018 break;
22019
22020 /* Produce glyphs. */
22021 x_before = it->current_x;
22022 n_glyphs_before = row->used[TEXT_AREA];
22023 PRODUCE_GLYPHS (it);
22024
22025 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22026 i = 0;
22027 x = x_before;
22028 while (i < nglyphs)
22029 {
22030 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22031
22032 if (it->line_wrap != TRUNCATE
22033 && x + glyph->pixel_width > max_x)
22034 {
22035 /* End of continued line or max_x reached. */
22036 if (CHAR_GLYPH_PADDING_P (*glyph))
22037 {
22038 /* A wide character is unbreakable. */
22039 if (row->reversed_p)
22040 unproduce_glyphs (it, row->used[TEXT_AREA]
22041 - n_glyphs_before);
22042 row->used[TEXT_AREA] = n_glyphs_before;
22043 it->current_x = x_before;
22044 }
22045 else
22046 {
22047 if (row->reversed_p)
22048 unproduce_glyphs (it, row->used[TEXT_AREA]
22049 - (n_glyphs_before + i));
22050 row->used[TEXT_AREA] = n_glyphs_before + i;
22051 it->current_x = x;
22052 }
22053 break;
22054 }
22055 else if (x + glyph->pixel_width >= it->first_visible_x)
22056 {
22057 /* Glyph is at least partially visible. */
22058 ++it->hpos;
22059 if (x < it->first_visible_x)
22060 row->x = x - it->first_visible_x;
22061 }
22062 else
22063 {
22064 /* Glyph is off the left margin of the display area.
22065 Should not happen. */
22066 emacs_abort ();
22067 }
22068
22069 row->ascent = max (row->ascent, it->max_ascent);
22070 row->height = max (row->height, it->max_ascent + it->max_descent);
22071 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22072 row->phys_height = max (row->phys_height,
22073 it->max_phys_ascent + it->max_phys_descent);
22074 row->extra_line_spacing = max (row->extra_line_spacing,
22075 it->max_extra_line_spacing);
22076 x += glyph->pixel_width;
22077 ++i;
22078 }
22079
22080 /* Stop if max_x reached. */
22081 if (i < nglyphs)
22082 break;
22083
22084 /* Stop at line ends. */
22085 if (ITERATOR_AT_END_OF_LINE_P (it))
22086 {
22087 it->continuation_lines_width = 0;
22088 break;
22089 }
22090
22091 set_iterator_to_next (it, 1);
22092 if (STRINGP (it->string))
22093 it_charpos = IT_STRING_CHARPOS (*it);
22094 else
22095 it_charpos = IT_CHARPOS (*it);
22096
22097 /* Stop if truncating at the right edge. */
22098 if (it->line_wrap == TRUNCATE
22099 && it->current_x >= it->last_visible_x)
22100 {
22101 /* Add truncation mark, but don't do it if the line is
22102 truncated at a padding space. */
22103 if (it_charpos < it->string_nchars)
22104 {
22105 if (!FRAME_WINDOW_P (it->f))
22106 {
22107 int ii, n;
22108
22109 if (it->current_x > it->last_visible_x)
22110 {
22111 if (!row->reversed_p)
22112 {
22113 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22114 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22115 break;
22116 }
22117 else
22118 {
22119 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22120 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22121 break;
22122 unproduce_glyphs (it, ii + 1);
22123 ii = row->used[TEXT_AREA] - (ii + 1);
22124 }
22125 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22126 {
22127 row->used[TEXT_AREA] = ii;
22128 produce_special_glyphs (it, IT_TRUNCATION);
22129 }
22130 }
22131 produce_special_glyphs (it, IT_TRUNCATION);
22132 }
22133 row->truncated_on_right_p = 1;
22134 }
22135 break;
22136 }
22137 }
22138
22139 /* Maybe insert a truncation at the left. */
22140 if (it->first_visible_x
22141 && it_charpos > 0)
22142 {
22143 if (!FRAME_WINDOW_P (it->f)
22144 || (row->reversed_p
22145 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22146 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22147 insert_left_trunc_glyphs (it);
22148 row->truncated_on_left_p = 1;
22149 }
22150
22151 it->face_id = saved_face_id;
22152
22153 /* Value is number of columns displayed. */
22154 return it->hpos - hpos_at_start;
22155 }
22156
22157
22158 \f
22159 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22160 appears as an element of LIST or as the car of an element of LIST.
22161 If PROPVAL is a list, compare each element against LIST in that
22162 way, and return 1/2 if any element of PROPVAL is found in LIST.
22163 Otherwise return 0. This function cannot quit.
22164 The return value is 2 if the text is invisible but with an ellipsis
22165 and 1 if it's invisible and without an ellipsis. */
22166
22167 int
22168 invisible_p (register Lisp_Object propval, Lisp_Object list)
22169 {
22170 register Lisp_Object tail, proptail;
22171
22172 for (tail = list; CONSP (tail); tail = XCDR (tail))
22173 {
22174 register Lisp_Object tem;
22175 tem = XCAR (tail);
22176 if (EQ (propval, tem))
22177 return 1;
22178 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22179 return NILP (XCDR (tem)) ? 1 : 2;
22180 }
22181
22182 if (CONSP (propval))
22183 {
22184 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22185 {
22186 Lisp_Object propelt;
22187 propelt = XCAR (proptail);
22188 for (tail = list; CONSP (tail); tail = XCDR (tail))
22189 {
22190 register Lisp_Object tem;
22191 tem = XCAR (tail);
22192 if (EQ (propelt, tem))
22193 return 1;
22194 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22195 return NILP (XCDR (tem)) ? 1 : 2;
22196 }
22197 }
22198 }
22199
22200 return 0;
22201 }
22202
22203 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22204 doc: /* Non-nil if the property makes the text invisible.
22205 POS-OR-PROP can be a marker or number, in which case it is taken to be
22206 a position in the current buffer and the value of the `invisible' property
22207 is checked; or it can be some other value, which is then presumed to be the
22208 value of the `invisible' property of the text of interest.
22209 The non-nil value returned can be t for truly invisible text or something
22210 else if the text is replaced by an ellipsis. */)
22211 (Lisp_Object pos_or_prop)
22212 {
22213 Lisp_Object prop
22214 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22215 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22216 : pos_or_prop);
22217 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22218 return (invis == 0 ? Qnil
22219 : invis == 1 ? Qt
22220 : make_number (invis));
22221 }
22222
22223 /* Calculate a width or height in pixels from a specification using
22224 the following elements:
22225
22226 SPEC ::=
22227 NUM - a (fractional) multiple of the default font width/height
22228 (NUM) - specifies exactly NUM pixels
22229 UNIT - a fixed number of pixels, see below.
22230 ELEMENT - size of a display element in pixels, see below.
22231 (NUM . SPEC) - equals NUM * SPEC
22232 (+ SPEC SPEC ...) - add pixel values
22233 (- SPEC SPEC ...) - subtract pixel values
22234 (- SPEC) - negate pixel value
22235
22236 NUM ::=
22237 INT or FLOAT - a number constant
22238 SYMBOL - use symbol's (buffer local) variable binding.
22239
22240 UNIT ::=
22241 in - pixels per inch *)
22242 mm - pixels per 1/1000 meter *)
22243 cm - pixels per 1/100 meter *)
22244 width - width of current font in pixels.
22245 height - height of current font in pixels.
22246
22247 *) using the ratio(s) defined in display-pixels-per-inch.
22248
22249 ELEMENT ::=
22250
22251 left-fringe - left fringe width in pixels
22252 right-fringe - right fringe width in pixels
22253
22254 left-margin - left margin width in pixels
22255 right-margin - right margin width in pixels
22256
22257 scroll-bar - scroll-bar area width in pixels
22258
22259 Examples:
22260
22261 Pixels corresponding to 5 inches:
22262 (5 . in)
22263
22264 Total width of non-text areas on left side of window (if scroll-bar is on left):
22265 '(space :width (+ left-fringe left-margin scroll-bar))
22266
22267 Align to first text column (in header line):
22268 '(space :align-to 0)
22269
22270 Align to middle of text area minus half the width of variable `my-image'
22271 containing a loaded image:
22272 '(space :align-to (0.5 . (- text my-image)))
22273
22274 Width of left margin minus width of 1 character in the default font:
22275 '(space :width (- left-margin 1))
22276
22277 Width of left margin minus width of 2 characters in the current font:
22278 '(space :width (- left-margin (2 . width)))
22279
22280 Center 1 character over left-margin (in header line):
22281 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22282
22283 Different ways to express width of left fringe plus left margin minus one pixel:
22284 '(space :width (- (+ left-fringe left-margin) (1)))
22285 '(space :width (+ left-fringe left-margin (- (1))))
22286 '(space :width (+ left-fringe left-margin (-1)))
22287
22288 */
22289
22290 #define NUMVAL(X) \
22291 ((INTEGERP (X) || FLOATP (X)) \
22292 ? XFLOATINT (X) \
22293 : - 1)
22294
22295 static int
22296 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22297 struct font *font, int width_p, int *align_to)
22298 {
22299 double pixels;
22300
22301 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22302 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22303
22304 if (NILP (prop))
22305 return OK_PIXELS (0);
22306
22307 eassert (FRAME_LIVE_P (it->f));
22308
22309 if (SYMBOLP (prop))
22310 {
22311 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22312 {
22313 char *unit = SSDATA (SYMBOL_NAME (prop));
22314
22315 if (unit[0] == 'i' && unit[1] == 'n')
22316 pixels = 1.0;
22317 else if (unit[0] == 'm' && unit[1] == 'm')
22318 pixels = 25.4;
22319 else if (unit[0] == 'c' && unit[1] == 'm')
22320 pixels = 2.54;
22321 else
22322 pixels = 0;
22323 if (pixels > 0)
22324 {
22325 double ppi;
22326 #ifdef HAVE_WINDOW_SYSTEM
22327 if (FRAME_WINDOW_P (it->f)
22328 && (ppi = (width_p
22329 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22330 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22331 ppi > 0))
22332 return OK_PIXELS (ppi / pixels);
22333 #endif
22334
22335 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22336 || (CONSP (Vdisplay_pixels_per_inch)
22337 && (ppi = (width_p
22338 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22339 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22340 ppi > 0)))
22341 return OK_PIXELS (ppi / pixels);
22342
22343 return 0;
22344 }
22345 }
22346
22347 #ifdef HAVE_WINDOW_SYSTEM
22348 if (EQ (prop, Qheight))
22349 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22350 if (EQ (prop, Qwidth))
22351 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22352 #else
22353 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22354 return OK_PIXELS (1);
22355 #endif
22356
22357 if (EQ (prop, Qtext))
22358 return OK_PIXELS (width_p
22359 ? window_box_width (it->w, TEXT_AREA)
22360 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22361
22362 if (align_to && *align_to < 0)
22363 {
22364 *res = 0;
22365 if (EQ (prop, Qleft))
22366 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22367 if (EQ (prop, Qright))
22368 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22369 if (EQ (prop, Qcenter))
22370 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22371 + window_box_width (it->w, TEXT_AREA) / 2);
22372 if (EQ (prop, Qleft_fringe))
22373 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22374 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22375 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22376 if (EQ (prop, Qright_fringe))
22377 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22378 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22379 : window_box_right_offset (it->w, TEXT_AREA));
22380 if (EQ (prop, Qleft_margin))
22381 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22382 if (EQ (prop, Qright_margin))
22383 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22384 if (EQ (prop, Qscroll_bar))
22385 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22386 ? 0
22387 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22388 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22389 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22390 : 0)));
22391 }
22392 else
22393 {
22394 if (EQ (prop, Qleft_fringe))
22395 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22396 if (EQ (prop, Qright_fringe))
22397 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22398 if (EQ (prop, Qleft_margin))
22399 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22400 if (EQ (prop, Qright_margin))
22401 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22402 if (EQ (prop, Qscroll_bar))
22403 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22404 }
22405
22406 prop = buffer_local_value_1 (prop, it->w->buffer);
22407 if (EQ (prop, Qunbound))
22408 prop = Qnil;
22409 }
22410
22411 if (INTEGERP (prop) || FLOATP (prop))
22412 {
22413 int base_unit = (width_p
22414 ? FRAME_COLUMN_WIDTH (it->f)
22415 : FRAME_LINE_HEIGHT (it->f));
22416 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22417 }
22418
22419 if (CONSP (prop))
22420 {
22421 Lisp_Object car = XCAR (prop);
22422 Lisp_Object cdr = XCDR (prop);
22423
22424 if (SYMBOLP (car))
22425 {
22426 #ifdef HAVE_WINDOW_SYSTEM
22427 if (FRAME_WINDOW_P (it->f)
22428 && valid_image_p (prop))
22429 {
22430 ptrdiff_t id = lookup_image (it->f, prop);
22431 struct image *img = IMAGE_FROM_ID (it->f, id);
22432
22433 return OK_PIXELS (width_p ? img->width : img->height);
22434 }
22435 #endif
22436 if (EQ (car, Qplus) || EQ (car, Qminus))
22437 {
22438 int first = 1;
22439 double px;
22440
22441 pixels = 0;
22442 while (CONSP (cdr))
22443 {
22444 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22445 font, width_p, align_to))
22446 return 0;
22447 if (first)
22448 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22449 else
22450 pixels += px;
22451 cdr = XCDR (cdr);
22452 }
22453 if (EQ (car, Qminus))
22454 pixels = -pixels;
22455 return OK_PIXELS (pixels);
22456 }
22457
22458 car = buffer_local_value_1 (car, it->w->buffer);
22459 if (EQ (car, Qunbound))
22460 car = Qnil;
22461 }
22462
22463 if (INTEGERP (car) || FLOATP (car))
22464 {
22465 double fact;
22466 pixels = XFLOATINT (car);
22467 if (NILP (cdr))
22468 return OK_PIXELS (pixels);
22469 if (calc_pixel_width_or_height (&fact, it, cdr,
22470 font, width_p, align_to))
22471 return OK_PIXELS (pixels * fact);
22472 return 0;
22473 }
22474
22475 return 0;
22476 }
22477
22478 return 0;
22479 }
22480
22481 \f
22482 /***********************************************************************
22483 Glyph Display
22484 ***********************************************************************/
22485
22486 #ifdef HAVE_WINDOW_SYSTEM
22487
22488 #ifdef GLYPH_DEBUG
22489
22490 void
22491 dump_glyph_string (struct glyph_string *s)
22492 {
22493 fprintf (stderr, "glyph string\n");
22494 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22495 s->x, s->y, s->width, s->height);
22496 fprintf (stderr, " ybase = %d\n", s->ybase);
22497 fprintf (stderr, " hl = %d\n", s->hl);
22498 fprintf (stderr, " left overhang = %d, right = %d\n",
22499 s->left_overhang, s->right_overhang);
22500 fprintf (stderr, " nchars = %d\n", s->nchars);
22501 fprintf (stderr, " extends to end of line = %d\n",
22502 s->extends_to_end_of_line_p);
22503 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22504 fprintf (stderr, " bg width = %d\n", s->background_width);
22505 }
22506
22507 #endif /* GLYPH_DEBUG */
22508
22509 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22510 of XChar2b structures for S; it can't be allocated in
22511 init_glyph_string because it must be allocated via `alloca'. W
22512 is the window on which S is drawn. ROW and AREA are the glyph row
22513 and area within the row from which S is constructed. START is the
22514 index of the first glyph structure covered by S. HL is a
22515 face-override for drawing S. */
22516
22517 #ifdef HAVE_NTGUI
22518 #define OPTIONAL_HDC(hdc) HDC hdc,
22519 #define DECLARE_HDC(hdc) HDC hdc;
22520 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22521 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22522 #endif
22523
22524 #ifndef OPTIONAL_HDC
22525 #define OPTIONAL_HDC(hdc)
22526 #define DECLARE_HDC(hdc)
22527 #define ALLOCATE_HDC(hdc, f)
22528 #define RELEASE_HDC(hdc, f)
22529 #endif
22530
22531 static void
22532 init_glyph_string (struct glyph_string *s,
22533 OPTIONAL_HDC (hdc)
22534 XChar2b *char2b, struct window *w, struct glyph_row *row,
22535 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22536 {
22537 memset (s, 0, sizeof *s);
22538 s->w = w;
22539 s->f = XFRAME (w->frame);
22540 #ifdef HAVE_NTGUI
22541 s->hdc = hdc;
22542 #endif
22543 s->display = FRAME_X_DISPLAY (s->f);
22544 s->window = FRAME_X_WINDOW (s->f);
22545 s->char2b = char2b;
22546 s->hl = hl;
22547 s->row = row;
22548 s->area = area;
22549 s->first_glyph = row->glyphs[area] + start;
22550 s->height = row->height;
22551 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22552 s->ybase = s->y + row->ascent;
22553 }
22554
22555
22556 /* Append the list of glyph strings with head H and tail T to the list
22557 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22558
22559 static void
22560 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22561 struct glyph_string *h, struct glyph_string *t)
22562 {
22563 if (h)
22564 {
22565 if (*head)
22566 (*tail)->next = h;
22567 else
22568 *head = h;
22569 h->prev = *tail;
22570 *tail = t;
22571 }
22572 }
22573
22574
22575 /* Prepend the list of glyph strings with head H and tail T to the
22576 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22577 result. */
22578
22579 static void
22580 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22581 struct glyph_string *h, struct glyph_string *t)
22582 {
22583 if (h)
22584 {
22585 if (*head)
22586 (*head)->prev = t;
22587 else
22588 *tail = t;
22589 t->next = *head;
22590 *head = h;
22591 }
22592 }
22593
22594
22595 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22596 Set *HEAD and *TAIL to the resulting list. */
22597
22598 static void
22599 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22600 struct glyph_string *s)
22601 {
22602 s->next = s->prev = NULL;
22603 append_glyph_string_lists (head, tail, s, s);
22604 }
22605
22606
22607 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22608 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22609 make sure that X resources for the face returned are allocated.
22610 Value is a pointer to a realized face that is ready for display if
22611 DISPLAY_P is non-zero. */
22612
22613 static struct face *
22614 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22615 XChar2b *char2b, int display_p)
22616 {
22617 struct face *face = FACE_FROM_ID (f, face_id);
22618
22619 if (face->font)
22620 {
22621 unsigned code = face->font->driver->encode_char (face->font, c);
22622
22623 if (code != FONT_INVALID_CODE)
22624 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22625 else
22626 STORE_XCHAR2B (char2b, 0, 0);
22627 }
22628
22629 /* Make sure X resources of the face are allocated. */
22630 #ifdef HAVE_X_WINDOWS
22631 if (display_p)
22632 #endif
22633 {
22634 eassert (face != NULL);
22635 PREPARE_FACE_FOR_DISPLAY (f, face);
22636 }
22637
22638 return face;
22639 }
22640
22641
22642 /* Get face and two-byte form of character glyph GLYPH on frame F.
22643 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22644 a pointer to a realized face that is ready for display. */
22645
22646 static struct face *
22647 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22648 XChar2b *char2b, int *two_byte_p)
22649 {
22650 struct face *face;
22651
22652 eassert (glyph->type == CHAR_GLYPH);
22653 face = FACE_FROM_ID (f, glyph->face_id);
22654
22655 if (two_byte_p)
22656 *two_byte_p = 0;
22657
22658 if (face->font)
22659 {
22660 unsigned code;
22661
22662 if (CHAR_BYTE8_P (glyph->u.ch))
22663 code = CHAR_TO_BYTE8 (glyph->u.ch);
22664 else
22665 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22666
22667 if (code != FONT_INVALID_CODE)
22668 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22669 else
22670 STORE_XCHAR2B (char2b, 0, 0);
22671 }
22672
22673 /* Make sure X resources of the face are allocated. */
22674 eassert (face != NULL);
22675 PREPARE_FACE_FOR_DISPLAY (f, face);
22676 return face;
22677 }
22678
22679
22680 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22681 Return 1 if FONT has a glyph for C, otherwise return 0. */
22682
22683 static int
22684 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22685 {
22686 unsigned code;
22687
22688 if (CHAR_BYTE8_P (c))
22689 code = CHAR_TO_BYTE8 (c);
22690 else
22691 code = font->driver->encode_char (font, c);
22692
22693 if (code == FONT_INVALID_CODE)
22694 return 0;
22695 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22696 return 1;
22697 }
22698
22699
22700 /* Fill glyph string S with composition components specified by S->cmp.
22701
22702 BASE_FACE is the base face of the composition.
22703 S->cmp_from is the index of the first component for S.
22704
22705 OVERLAPS non-zero means S should draw the foreground only, and use
22706 its physical height for clipping. See also draw_glyphs.
22707
22708 Value is the index of a component not in S. */
22709
22710 static int
22711 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22712 int overlaps)
22713 {
22714 int i;
22715 /* For all glyphs of this composition, starting at the offset
22716 S->cmp_from, until we reach the end of the definition or encounter a
22717 glyph that requires the different face, add it to S. */
22718 struct face *face;
22719
22720 eassert (s);
22721
22722 s->for_overlaps = overlaps;
22723 s->face = NULL;
22724 s->font = NULL;
22725 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22726 {
22727 int c = COMPOSITION_GLYPH (s->cmp, i);
22728
22729 /* TAB in a composition means display glyphs with padding space
22730 on the left or right. */
22731 if (c != '\t')
22732 {
22733 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22734 -1, Qnil);
22735
22736 face = get_char_face_and_encoding (s->f, c, face_id,
22737 s->char2b + i, 1);
22738 if (face)
22739 {
22740 if (! s->face)
22741 {
22742 s->face = face;
22743 s->font = s->face->font;
22744 }
22745 else if (s->face != face)
22746 break;
22747 }
22748 }
22749 ++s->nchars;
22750 }
22751 s->cmp_to = i;
22752
22753 if (s->face == NULL)
22754 {
22755 s->face = base_face->ascii_face;
22756 s->font = s->face->font;
22757 }
22758
22759 /* All glyph strings for the same composition has the same width,
22760 i.e. the width set for the first component of the composition. */
22761 s->width = s->first_glyph->pixel_width;
22762
22763 /* If the specified font could not be loaded, use the frame's
22764 default font, but record the fact that we couldn't load it in
22765 the glyph string so that we can draw rectangles for the
22766 characters of the glyph string. */
22767 if (s->font == NULL)
22768 {
22769 s->font_not_found_p = 1;
22770 s->font = FRAME_FONT (s->f);
22771 }
22772
22773 /* Adjust base line for subscript/superscript text. */
22774 s->ybase += s->first_glyph->voffset;
22775
22776 /* This glyph string must always be drawn with 16-bit functions. */
22777 s->two_byte_p = 1;
22778
22779 return s->cmp_to;
22780 }
22781
22782 static int
22783 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22784 int start, int end, int overlaps)
22785 {
22786 struct glyph *glyph, *last;
22787 Lisp_Object lgstring;
22788 int i;
22789
22790 s->for_overlaps = overlaps;
22791 glyph = s->row->glyphs[s->area] + start;
22792 last = s->row->glyphs[s->area] + end;
22793 s->cmp_id = glyph->u.cmp.id;
22794 s->cmp_from = glyph->slice.cmp.from;
22795 s->cmp_to = glyph->slice.cmp.to + 1;
22796 s->face = FACE_FROM_ID (s->f, face_id);
22797 lgstring = composition_gstring_from_id (s->cmp_id);
22798 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22799 glyph++;
22800 while (glyph < last
22801 && glyph->u.cmp.automatic
22802 && glyph->u.cmp.id == s->cmp_id
22803 && s->cmp_to == glyph->slice.cmp.from)
22804 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22805
22806 for (i = s->cmp_from; i < s->cmp_to; i++)
22807 {
22808 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22809 unsigned code = LGLYPH_CODE (lglyph);
22810
22811 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22812 }
22813 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22814 return glyph - s->row->glyphs[s->area];
22815 }
22816
22817
22818 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22819 See the comment of fill_glyph_string for arguments.
22820 Value is the index of the first glyph not in S. */
22821
22822
22823 static int
22824 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22825 int start, int end, int overlaps)
22826 {
22827 struct glyph *glyph, *last;
22828 int voffset;
22829
22830 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22831 s->for_overlaps = overlaps;
22832 glyph = s->row->glyphs[s->area] + start;
22833 last = s->row->glyphs[s->area] + end;
22834 voffset = glyph->voffset;
22835 s->face = FACE_FROM_ID (s->f, face_id);
22836 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22837 s->nchars = 1;
22838 s->width = glyph->pixel_width;
22839 glyph++;
22840 while (glyph < last
22841 && glyph->type == GLYPHLESS_GLYPH
22842 && glyph->voffset == voffset
22843 && glyph->face_id == face_id)
22844 {
22845 s->nchars++;
22846 s->width += glyph->pixel_width;
22847 glyph++;
22848 }
22849 s->ybase += voffset;
22850 return glyph - s->row->glyphs[s->area];
22851 }
22852
22853
22854 /* Fill glyph string S from a sequence of character glyphs.
22855
22856 FACE_ID is the face id of the string. START is the index of the
22857 first glyph to consider, END is the index of the last + 1.
22858 OVERLAPS non-zero means S should draw the foreground only, and use
22859 its physical height for clipping. See also draw_glyphs.
22860
22861 Value is the index of the first glyph not in S. */
22862
22863 static int
22864 fill_glyph_string (struct glyph_string *s, int face_id,
22865 int start, int end, int overlaps)
22866 {
22867 struct glyph *glyph, *last;
22868 int voffset;
22869 int glyph_not_available_p;
22870
22871 eassert (s->f == XFRAME (s->w->frame));
22872 eassert (s->nchars == 0);
22873 eassert (start >= 0 && end > start);
22874
22875 s->for_overlaps = overlaps;
22876 glyph = s->row->glyphs[s->area] + start;
22877 last = s->row->glyphs[s->area] + end;
22878 voffset = glyph->voffset;
22879 s->padding_p = glyph->padding_p;
22880 glyph_not_available_p = glyph->glyph_not_available_p;
22881
22882 while (glyph < last
22883 && glyph->type == CHAR_GLYPH
22884 && glyph->voffset == voffset
22885 /* Same face id implies same font, nowadays. */
22886 && glyph->face_id == face_id
22887 && glyph->glyph_not_available_p == glyph_not_available_p)
22888 {
22889 int two_byte_p;
22890
22891 s->face = get_glyph_face_and_encoding (s->f, glyph,
22892 s->char2b + s->nchars,
22893 &two_byte_p);
22894 s->two_byte_p = two_byte_p;
22895 ++s->nchars;
22896 eassert (s->nchars <= end - start);
22897 s->width += glyph->pixel_width;
22898 if (glyph++->padding_p != s->padding_p)
22899 break;
22900 }
22901
22902 s->font = s->face->font;
22903
22904 /* If the specified font could not be loaded, use the frame's font,
22905 but record the fact that we couldn't load it in
22906 S->font_not_found_p so that we can draw rectangles for the
22907 characters of the glyph string. */
22908 if (s->font == NULL || glyph_not_available_p)
22909 {
22910 s->font_not_found_p = 1;
22911 s->font = FRAME_FONT (s->f);
22912 }
22913
22914 /* Adjust base line for subscript/superscript text. */
22915 s->ybase += voffset;
22916
22917 eassert (s->face && s->face->gc);
22918 return glyph - s->row->glyphs[s->area];
22919 }
22920
22921
22922 /* Fill glyph string S from image glyph S->first_glyph. */
22923
22924 static void
22925 fill_image_glyph_string (struct glyph_string *s)
22926 {
22927 eassert (s->first_glyph->type == IMAGE_GLYPH);
22928 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22929 eassert (s->img);
22930 s->slice = s->first_glyph->slice.img;
22931 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22932 s->font = s->face->font;
22933 s->width = s->first_glyph->pixel_width;
22934
22935 /* Adjust base line for subscript/superscript text. */
22936 s->ybase += s->first_glyph->voffset;
22937 }
22938
22939
22940 /* Fill glyph string S from a sequence of stretch glyphs.
22941
22942 START is the index of the first glyph to consider,
22943 END is the index of the last + 1.
22944
22945 Value is the index of the first glyph not in S. */
22946
22947 static int
22948 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22949 {
22950 struct glyph *glyph, *last;
22951 int voffset, face_id;
22952
22953 eassert (s->first_glyph->type == STRETCH_GLYPH);
22954
22955 glyph = s->row->glyphs[s->area] + start;
22956 last = s->row->glyphs[s->area] + end;
22957 face_id = glyph->face_id;
22958 s->face = FACE_FROM_ID (s->f, face_id);
22959 s->font = s->face->font;
22960 s->width = glyph->pixel_width;
22961 s->nchars = 1;
22962 voffset = glyph->voffset;
22963
22964 for (++glyph;
22965 (glyph < last
22966 && glyph->type == STRETCH_GLYPH
22967 && glyph->voffset == voffset
22968 && glyph->face_id == face_id);
22969 ++glyph)
22970 s->width += glyph->pixel_width;
22971
22972 /* Adjust base line for subscript/superscript text. */
22973 s->ybase += voffset;
22974
22975 /* The case that face->gc == 0 is handled when drawing the glyph
22976 string by calling PREPARE_FACE_FOR_DISPLAY. */
22977 eassert (s->face);
22978 return glyph - s->row->glyphs[s->area];
22979 }
22980
22981 static struct font_metrics *
22982 get_per_char_metric (struct font *font, XChar2b *char2b)
22983 {
22984 static struct font_metrics metrics;
22985 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22986
22987 if (! font || code == FONT_INVALID_CODE)
22988 return NULL;
22989 font->driver->text_extents (font, &code, 1, &metrics);
22990 return &metrics;
22991 }
22992
22993 /* EXPORT for RIF:
22994 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22995 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22996 assumed to be zero. */
22997
22998 void
22999 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23000 {
23001 *left = *right = 0;
23002
23003 if (glyph->type == CHAR_GLYPH)
23004 {
23005 struct face *face;
23006 XChar2b char2b;
23007 struct font_metrics *pcm;
23008
23009 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23010 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23011 {
23012 if (pcm->rbearing > pcm->width)
23013 *right = pcm->rbearing - pcm->width;
23014 if (pcm->lbearing < 0)
23015 *left = -pcm->lbearing;
23016 }
23017 }
23018 else if (glyph->type == COMPOSITE_GLYPH)
23019 {
23020 if (! glyph->u.cmp.automatic)
23021 {
23022 struct composition *cmp = composition_table[glyph->u.cmp.id];
23023
23024 if (cmp->rbearing > cmp->pixel_width)
23025 *right = cmp->rbearing - cmp->pixel_width;
23026 if (cmp->lbearing < 0)
23027 *left = - cmp->lbearing;
23028 }
23029 else
23030 {
23031 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23032 struct font_metrics metrics;
23033
23034 composition_gstring_width (gstring, glyph->slice.cmp.from,
23035 glyph->slice.cmp.to + 1, &metrics);
23036 if (metrics.rbearing > metrics.width)
23037 *right = metrics.rbearing - metrics.width;
23038 if (metrics.lbearing < 0)
23039 *left = - metrics.lbearing;
23040 }
23041 }
23042 }
23043
23044
23045 /* Return the index of the first glyph preceding glyph string S that
23046 is overwritten by S because of S's left overhang. Value is -1
23047 if no glyphs are overwritten. */
23048
23049 static int
23050 left_overwritten (struct glyph_string *s)
23051 {
23052 int k;
23053
23054 if (s->left_overhang)
23055 {
23056 int x = 0, i;
23057 struct glyph *glyphs = s->row->glyphs[s->area];
23058 int first = s->first_glyph - glyphs;
23059
23060 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23061 x -= glyphs[i].pixel_width;
23062
23063 k = i + 1;
23064 }
23065 else
23066 k = -1;
23067
23068 return k;
23069 }
23070
23071
23072 /* Return the index of the first glyph preceding glyph string S that
23073 is overwriting S because of its right overhang. Value is -1 if no
23074 glyph in front of S overwrites S. */
23075
23076 static int
23077 left_overwriting (struct glyph_string *s)
23078 {
23079 int i, k, x;
23080 struct glyph *glyphs = s->row->glyphs[s->area];
23081 int first = s->first_glyph - glyphs;
23082
23083 k = -1;
23084 x = 0;
23085 for (i = first - 1; i >= 0; --i)
23086 {
23087 int left, right;
23088 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23089 if (x + right > 0)
23090 k = i;
23091 x -= glyphs[i].pixel_width;
23092 }
23093
23094 return k;
23095 }
23096
23097
23098 /* Return the index of the last glyph following glyph string S that is
23099 overwritten by S because of S's right overhang. Value is -1 if
23100 no such glyph is found. */
23101
23102 static int
23103 right_overwritten (struct glyph_string *s)
23104 {
23105 int k = -1;
23106
23107 if (s->right_overhang)
23108 {
23109 int x = 0, i;
23110 struct glyph *glyphs = s->row->glyphs[s->area];
23111 int first = (s->first_glyph - glyphs
23112 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23113 int end = s->row->used[s->area];
23114
23115 for (i = first; i < end && s->right_overhang > x; ++i)
23116 x += glyphs[i].pixel_width;
23117
23118 k = i;
23119 }
23120
23121 return k;
23122 }
23123
23124
23125 /* Return the index of the last glyph following glyph string S that
23126 overwrites S because of its left overhang. Value is negative
23127 if no such glyph is found. */
23128
23129 static int
23130 right_overwriting (struct glyph_string *s)
23131 {
23132 int i, k, x;
23133 int end = s->row->used[s->area];
23134 struct glyph *glyphs = s->row->glyphs[s->area];
23135 int first = (s->first_glyph - glyphs
23136 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23137
23138 k = -1;
23139 x = 0;
23140 for (i = first; i < end; ++i)
23141 {
23142 int left, right;
23143 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23144 if (x - left < 0)
23145 k = i;
23146 x += glyphs[i].pixel_width;
23147 }
23148
23149 return k;
23150 }
23151
23152
23153 /* Set background width of glyph string S. START is the index of the
23154 first glyph following S. LAST_X is the right-most x-position + 1
23155 in the drawing area. */
23156
23157 static void
23158 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23159 {
23160 /* If the face of this glyph string has to be drawn to the end of
23161 the drawing area, set S->extends_to_end_of_line_p. */
23162
23163 if (start == s->row->used[s->area]
23164 && s->area == TEXT_AREA
23165 && ((s->row->fill_line_p
23166 && (s->hl == DRAW_NORMAL_TEXT
23167 || s->hl == DRAW_IMAGE_RAISED
23168 || s->hl == DRAW_IMAGE_SUNKEN))
23169 || s->hl == DRAW_MOUSE_FACE))
23170 s->extends_to_end_of_line_p = 1;
23171
23172 /* If S extends its face to the end of the line, set its
23173 background_width to the distance to the right edge of the drawing
23174 area. */
23175 if (s->extends_to_end_of_line_p)
23176 s->background_width = last_x - s->x + 1;
23177 else
23178 s->background_width = s->width;
23179 }
23180
23181
23182 /* Compute overhangs and x-positions for glyph string S and its
23183 predecessors, or successors. X is the starting x-position for S.
23184 BACKWARD_P non-zero means process predecessors. */
23185
23186 static void
23187 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23188 {
23189 if (backward_p)
23190 {
23191 while (s)
23192 {
23193 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23194 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23195 x -= s->width;
23196 s->x = x;
23197 s = s->prev;
23198 }
23199 }
23200 else
23201 {
23202 while (s)
23203 {
23204 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23205 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23206 s->x = x;
23207 x += s->width;
23208 s = s->next;
23209 }
23210 }
23211 }
23212
23213
23214
23215 /* The following macros are only called from draw_glyphs below.
23216 They reference the following parameters of that function directly:
23217 `w', `row', `area', and `overlap_p'
23218 as well as the following local variables:
23219 `s', `f', and `hdc' (in W32) */
23220
23221 #ifdef HAVE_NTGUI
23222 /* On W32, silently add local `hdc' variable to argument list of
23223 init_glyph_string. */
23224 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23225 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23226 #else
23227 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23228 init_glyph_string (s, char2b, w, row, area, start, hl)
23229 #endif
23230
23231 /* Add a glyph string for a stretch glyph to the list of strings
23232 between HEAD and TAIL. START is the index of the stretch glyph in
23233 row area AREA of glyph row ROW. END is the index of the last glyph
23234 in that glyph row area. X is the current output position assigned
23235 to the new glyph string constructed. HL overrides that face of the
23236 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23237 is the right-most x-position of the drawing area. */
23238
23239 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23240 and below -- keep them on one line. */
23241 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23242 do \
23243 { \
23244 s = alloca (sizeof *s); \
23245 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23246 START = fill_stretch_glyph_string (s, START, END); \
23247 append_glyph_string (&HEAD, &TAIL, s); \
23248 s->x = (X); \
23249 } \
23250 while (0)
23251
23252
23253 /* Add a glyph string for an image glyph to the list of strings
23254 between HEAD and TAIL. START is the index of the image glyph in
23255 row area AREA of glyph row ROW. END is the index of the last glyph
23256 in that glyph row area. X is the current output position assigned
23257 to the new glyph string constructed. HL overrides that face of the
23258 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23259 is the right-most x-position of the drawing area. */
23260
23261 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23262 do \
23263 { \
23264 s = alloca (sizeof *s); \
23265 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23266 fill_image_glyph_string (s); \
23267 append_glyph_string (&HEAD, &TAIL, s); \
23268 ++START; \
23269 s->x = (X); \
23270 } \
23271 while (0)
23272
23273
23274 /* Add a glyph string for a sequence of character glyphs to the list
23275 of strings between HEAD and TAIL. START is the index of the first
23276 glyph in row area AREA of glyph row ROW that is part of the new
23277 glyph string. END is the index of the last glyph in that glyph row
23278 area. X is the current output position assigned to the new glyph
23279 string constructed. HL overrides that face of the glyph; e.g. it
23280 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23281 right-most x-position of the drawing area. */
23282
23283 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23284 do \
23285 { \
23286 int face_id; \
23287 XChar2b *char2b; \
23288 \
23289 face_id = (row)->glyphs[area][START].face_id; \
23290 \
23291 s = alloca (sizeof *s); \
23292 char2b = alloca ((END - START) * sizeof *char2b); \
23293 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23294 append_glyph_string (&HEAD, &TAIL, s); \
23295 s->x = (X); \
23296 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23297 } \
23298 while (0)
23299
23300
23301 /* Add a glyph string for a composite sequence to the list of strings
23302 between HEAD and TAIL. START is the index of the first glyph in
23303 row area AREA of glyph row ROW that is part of the new glyph
23304 string. END is the index of the last glyph in that glyph row area.
23305 X is the current output position assigned to the new glyph string
23306 constructed. HL overrides that face of the glyph; e.g. it is
23307 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23308 x-position of the drawing area. */
23309
23310 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23311 do { \
23312 int face_id = (row)->glyphs[area][START].face_id; \
23313 struct face *base_face = FACE_FROM_ID (f, face_id); \
23314 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23315 struct composition *cmp = composition_table[cmp_id]; \
23316 XChar2b *char2b; \
23317 struct glyph_string *first_s = NULL; \
23318 int n; \
23319 \
23320 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23321 \
23322 /* Make glyph_strings for each glyph sequence that is drawable by \
23323 the same face, and append them to HEAD/TAIL. */ \
23324 for (n = 0; n < cmp->glyph_len;) \
23325 { \
23326 s = alloca (sizeof *s); \
23327 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23328 append_glyph_string (&(HEAD), &(TAIL), s); \
23329 s->cmp = cmp; \
23330 s->cmp_from = n; \
23331 s->x = (X); \
23332 if (n == 0) \
23333 first_s = s; \
23334 n = fill_composite_glyph_string (s, base_face, overlaps); \
23335 } \
23336 \
23337 ++START; \
23338 s = first_s; \
23339 } while (0)
23340
23341
23342 /* Add a glyph string for a glyph-string sequence to the list of strings
23343 between HEAD and TAIL. */
23344
23345 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23346 do { \
23347 int face_id; \
23348 XChar2b *char2b; \
23349 Lisp_Object gstring; \
23350 \
23351 face_id = (row)->glyphs[area][START].face_id; \
23352 gstring = (composition_gstring_from_id \
23353 ((row)->glyphs[area][START].u.cmp.id)); \
23354 s = alloca (sizeof *s); \
23355 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23356 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23357 append_glyph_string (&(HEAD), &(TAIL), s); \
23358 s->x = (X); \
23359 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23360 } while (0)
23361
23362
23363 /* Add a glyph string for a sequence of glyphless character's glyphs
23364 to the list of strings between HEAD and TAIL. The meanings of
23365 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23366
23367 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23368 do \
23369 { \
23370 int face_id; \
23371 \
23372 face_id = (row)->glyphs[area][START].face_id; \
23373 \
23374 s = alloca (sizeof *s); \
23375 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23376 append_glyph_string (&HEAD, &TAIL, s); \
23377 s->x = (X); \
23378 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23379 overlaps); \
23380 } \
23381 while (0)
23382
23383
23384 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23385 of AREA of glyph row ROW on window W between indices START and END.
23386 HL overrides the face for drawing glyph strings, e.g. it is
23387 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23388 x-positions of the drawing area.
23389
23390 This is an ugly monster macro construct because we must use alloca
23391 to allocate glyph strings (because draw_glyphs can be called
23392 asynchronously). */
23393
23394 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23395 do \
23396 { \
23397 HEAD = TAIL = NULL; \
23398 while (START < END) \
23399 { \
23400 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23401 switch (first_glyph->type) \
23402 { \
23403 case CHAR_GLYPH: \
23404 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23405 HL, X, LAST_X); \
23406 break; \
23407 \
23408 case COMPOSITE_GLYPH: \
23409 if (first_glyph->u.cmp.automatic) \
23410 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23411 HL, X, LAST_X); \
23412 else \
23413 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23414 HL, X, LAST_X); \
23415 break; \
23416 \
23417 case STRETCH_GLYPH: \
23418 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23419 HL, X, LAST_X); \
23420 break; \
23421 \
23422 case IMAGE_GLYPH: \
23423 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23424 HL, X, LAST_X); \
23425 break; \
23426 \
23427 case GLYPHLESS_GLYPH: \
23428 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23429 HL, X, LAST_X); \
23430 break; \
23431 \
23432 default: \
23433 emacs_abort (); \
23434 } \
23435 \
23436 if (s) \
23437 { \
23438 set_glyph_string_background_width (s, START, LAST_X); \
23439 (X) += s->width; \
23440 } \
23441 } \
23442 } while (0)
23443
23444
23445 /* Draw glyphs between START and END in AREA of ROW on window W,
23446 starting at x-position X. X is relative to AREA in W. HL is a
23447 face-override with the following meaning:
23448
23449 DRAW_NORMAL_TEXT draw normally
23450 DRAW_CURSOR draw in cursor face
23451 DRAW_MOUSE_FACE draw in mouse face.
23452 DRAW_INVERSE_VIDEO draw in mode line face
23453 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23454 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23455
23456 If OVERLAPS is non-zero, draw only the foreground of characters and
23457 clip to the physical height of ROW. Non-zero value also defines
23458 the overlapping part to be drawn:
23459
23460 OVERLAPS_PRED overlap with preceding rows
23461 OVERLAPS_SUCC overlap with succeeding rows
23462 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23463 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23464
23465 Value is the x-position reached, relative to AREA of W. */
23466
23467 static int
23468 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23469 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23470 enum draw_glyphs_face hl, int overlaps)
23471 {
23472 struct glyph_string *head, *tail;
23473 struct glyph_string *s;
23474 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23475 int i, j, x_reached, last_x, area_left = 0;
23476 struct frame *f = XFRAME (WINDOW_FRAME (w));
23477 DECLARE_HDC (hdc);
23478
23479 ALLOCATE_HDC (hdc, f);
23480
23481 /* Let's rather be paranoid than getting a SEGV. */
23482 end = min (end, row->used[area]);
23483 start = clip_to_bounds (0, start, end);
23484
23485 /* Translate X to frame coordinates. Set last_x to the right
23486 end of the drawing area. */
23487 if (row->full_width_p)
23488 {
23489 /* X is relative to the left edge of W, without scroll bars
23490 or fringes. */
23491 area_left = WINDOW_LEFT_EDGE_X (w);
23492 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23493 }
23494 else
23495 {
23496 area_left = window_box_left (w, area);
23497 last_x = area_left + window_box_width (w, area);
23498 }
23499 x += area_left;
23500
23501 /* Build a doubly-linked list of glyph_string structures between
23502 head and tail from what we have to draw. Note that the macro
23503 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23504 the reason we use a separate variable `i'. */
23505 i = start;
23506 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23507 if (tail)
23508 x_reached = tail->x + tail->background_width;
23509 else
23510 x_reached = x;
23511
23512 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23513 the row, redraw some glyphs in front or following the glyph
23514 strings built above. */
23515 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23516 {
23517 struct glyph_string *h, *t;
23518 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23519 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23520 int check_mouse_face = 0;
23521 int dummy_x = 0;
23522
23523 /* If mouse highlighting is on, we may need to draw adjacent
23524 glyphs using mouse-face highlighting. */
23525 if (area == TEXT_AREA && row->mouse_face_p
23526 && hlinfo->mouse_face_beg_row >= 0
23527 && hlinfo->mouse_face_end_row >= 0)
23528 {
23529 struct glyph_row *mouse_beg_row, *mouse_end_row;
23530
23531 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23532 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23533
23534 if (row >= mouse_beg_row && row <= mouse_end_row)
23535 {
23536 check_mouse_face = 1;
23537 mouse_beg_col = (row == mouse_beg_row)
23538 ? hlinfo->mouse_face_beg_col : 0;
23539 mouse_end_col = (row == mouse_end_row)
23540 ? hlinfo->mouse_face_end_col
23541 : row->used[TEXT_AREA];
23542 }
23543 }
23544
23545 /* Compute overhangs for all glyph strings. */
23546 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23547 for (s = head; s; s = s->next)
23548 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23549
23550 /* Prepend glyph strings for glyphs in front of the first glyph
23551 string that are overwritten because of the first glyph
23552 string's left overhang. The background of all strings
23553 prepended must be drawn because the first glyph string
23554 draws over it. */
23555 i = left_overwritten (head);
23556 if (i >= 0)
23557 {
23558 enum draw_glyphs_face overlap_hl;
23559
23560 /* If this row contains mouse highlighting, attempt to draw
23561 the overlapped glyphs with the correct highlight. This
23562 code fails if the overlap encompasses more than one glyph
23563 and mouse-highlight spans only some of these glyphs.
23564 However, making it work perfectly involves a lot more
23565 code, and I don't know if the pathological case occurs in
23566 practice, so we'll stick to this for now. --- cyd */
23567 if (check_mouse_face
23568 && mouse_beg_col < start && mouse_end_col > i)
23569 overlap_hl = DRAW_MOUSE_FACE;
23570 else
23571 overlap_hl = DRAW_NORMAL_TEXT;
23572
23573 j = i;
23574 BUILD_GLYPH_STRINGS (j, start, h, t,
23575 overlap_hl, dummy_x, last_x);
23576 start = i;
23577 compute_overhangs_and_x (t, head->x, 1);
23578 prepend_glyph_string_lists (&head, &tail, h, t);
23579 clip_head = head;
23580 }
23581
23582 /* Prepend glyph strings for glyphs in front of the first glyph
23583 string that overwrite that glyph string because of their
23584 right overhang. For these strings, only the foreground must
23585 be drawn, because it draws over the glyph string at `head'.
23586 The background must not be drawn because this would overwrite
23587 right overhangs of preceding glyphs for which no glyph
23588 strings exist. */
23589 i = left_overwriting (head);
23590 if (i >= 0)
23591 {
23592 enum draw_glyphs_face overlap_hl;
23593
23594 if (check_mouse_face
23595 && mouse_beg_col < start && mouse_end_col > i)
23596 overlap_hl = DRAW_MOUSE_FACE;
23597 else
23598 overlap_hl = DRAW_NORMAL_TEXT;
23599
23600 clip_head = head;
23601 BUILD_GLYPH_STRINGS (i, start, h, t,
23602 overlap_hl, dummy_x, last_x);
23603 for (s = h; s; s = s->next)
23604 s->background_filled_p = 1;
23605 compute_overhangs_and_x (t, head->x, 1);
23606 prepend_glyph_string_lists (&head, &tail, h, t);
23607 }
23608
23609 /* Append glyphs strings for glyphs following the last glyph
23610 string tail that are overwritten by tail. The background of
23611 these strings has to be drawn because tail's foreground draws
23612 over it. */
23613 i = right_overwritten (tail);
23614 if (i >= 0)
23615 {
23616 enum draw_glyphs_face overlap_hl;
23617
23618 if (check_mouse_face
23619 && mouse_beg_col < i && mouse_end_col > end)
23620 overlap_hl = DRAW_MOUSE_FACE;
23621 else
23622 overlap_hl = DRAW_NORMAL_TEXT;
23623
23624 BUILD_GLYPH_STRINGS (end, i, h, t,
23625 overlap_hl, x, last_x);
23626 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23627 we don't have `end = i;' here. */
23628 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23629 append_glyph_string_lists (&head, &tail, h, t);
23630 clip_tail = tail;
23631 }
23632
23633 /* Append glyph strings for glyphs following the last glyph
23634 string tail that overwrite tail. The foreground of such
23635 glyphs has to be drawn because it writes into the background
23636 of tail. The background must not be drawn because it could
23637 paint over the foreground of following glyphs. */
23638 i = right_overwriting (tail);
23639 if (i >= 0)
23640 {
23641 enum draw_glyphs_face overlap_hl;
23642 if (check_mouse_face
23643 && mouse_beg_col < i && mouse_end_col > end)
23644 overlap_hl = DRAW_MOUSE_FACE;
23645 else
23646 overlap_hl = DRAW_NORMAL_TEXT;
23647
23648 clip_tail = tail;
23649 i++; /* We must include the Ith glyph. */
23650 BUILD_GLYPH_STRINGS (end, i, h, t,
23651 overlap_hl, x, last_x);
23652 for (s = h; s; s = s->next)
23653 s->background_filled_p = 1;
23654 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23655 append_glyph_string_lists (&head, &tail, h, t);
23656 }
23657 if (clip_head || clip_tail)
23658 for (s = head; s; s = s->next)
23659 {
23660 s->clip_head = clip_head;
23661 s->clip_tail = clip_tail;
23662 }
23663 }
23664
23665 /* Draw all strings. */
23666 for (s = head; s; s = s->next)
23667 FRAME_RIF (f)->draw_glyph_string (s);
23668
23669 #ifndef HAVE_NS
23670 /* When focus a sole frame and move horizontally, this sets on_p to 0
23671 causing a failure to erase prev cursor position. */
23672 if (area == TEXT_AREA
23673 && !row->full_width_p
23674 /* When drawing overlapping rows, only the glyph strings'
23675 foreground is drawn, which doesn't erase a cursor
23676 completely. */
23677 && !overlaps)
23678 {
23679 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23680 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23681 : (tail ? tail->x + tail->background_width : x));
23682 x0 -= area_left;
23683 x1 -= area_left;
23684
23685 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23686 row->y, MATRIX_ROW_BOTTOM_Y (row));
23687 }
23688 #endif
23689
23690 /* Value is the x-position up to which drawn, relative to AREA of W.
23691 This doesn't include parts drawn because of overhangs. */
23692 if (row->full_width_p)
23693 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23694 else
23695 x_reached -= area_left;
23696
23697 RELEASE_HDC (hdc, f);
23698
23699 return x_reached;
23700 }
23701
23702 /* Expand row matrix if too narrow. Don't expand if area
23703 is not present. */
23704
23705 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23706 { \
23707 if (!fonts_changed_p \
23708 && (it->glyph_row->glyphs[area] \
23709 < it->glyph_row->glyphs[area + 1])) \
23710 { \
23711 it->w->ncols_scale_factor++; \
23712 fonts_changed_p = 1; \
23713 } \
23714 }
23715
23716 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23717 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23718
23719 static void
23720 append_glyph (struct it *it)
23721 {
23722 struct glyph *glyph;
23723 enum glyph_row_area area = it->area;
23724
23725 eassert (it->glyph_row);
23726 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23727
23728 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23729 if (glyph < it->glyph_row->glyphs[area + 1])
23730 {
23731 /* If the glyph row is reversed, we need to prepend the glyph
23732 rather than append it. */
23733 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23734 {
23735 struct glyph *g;
23736
23737 /* Make room for the additional glyph. */
23738 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23739 g[1] = *g;
23740 glyph = it->glyph_row->glyphs[area];
23741 }
23742 glyph->charpos = CHARPOS (it->position);
23743 glyph->object = it->object;
23744 if (it->pixel_width > 0)
23745 {
23746 glyph->pixel_width = it->pixel_width;
23747 glyph->padding_p = 0;
23748 }
23749 else
23750 {
23751 /* Assure at least 1-pixel width. Otherwise, cursor can't
23752 be displayed correctly. */
23753 glyph->pixel_width = 1;
23754 glyph->padding_p = 1;
23755 }
23756 glyph->ascent = it->ascent;
23757 glyph->descent = it->descent;
23758 glyph->voffset = it->voffset;
23759 glyph->type = CHAR_GLYPH;
23760 glyph->avoid_cursor_p = it->avoid_cursor_p;
23761 glyph->multibyte_p = it->multibyte_p;
23762 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23763 {
23764 /* In R2L rows, the left and the right box edges need to be
23765 drawn in reverse direction. */
23766 glyph->right_box_line_p = it->start_of_box_run_p;
23767 glyph->left_box_line_p = it->end_of_box_run_p;
23768 }
23769 else
23770 {
23771 glyph->left_box_line_p = it->start_of_box_run_p;
23772 glyph->right_box_line_p = it->end_of_box_run_p;
23773 }
23774 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23775 || it->phys_descent > it->descent);
23776 glyph->glyph_not_available_p = it->glyph_not_available_p;
23777 glyph->face_id = it->face_id;
23778 glyph->u.ch = it->char_to_display;
23779 glyph->slice.img = null_glyph_slice;
23780 glyph->font_type = FONT_TYPE_UNKNOWN;
23781 if (it->bidi_p)
23782 {
23783 glyph->resolved_level = it->bidi_it.resolved_level;
23784 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23785 emacs_abort ();
23786 glyph->bidi_type = it->bidi_it.type;
23787 }
23788 else
23789 {
23790 glyph->resolved_level = 0;
23791 glyph->bidi_type = UNKNOWN_BT;
23792 }
23793 ++it->glyph_row->used[area];
23794 }
23795 else
23796 IT_EXPAND_MATRIX_WIDTH (it, area);
23797 }
23798
23799 /* Store one glyph for the composition IT->cmp_it.id in
23800 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23801 non-null. */
23802
23803 static void
23804 append_composite_glyph (struct it *it)
23805 {
23806 struct glyph *glyph;
23807 enum glyph_row_area area = it->area;
23808
23809 eassert (it->glyph_row);
23810
23811 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23812 if (glyph < it->glyph_row->glyphs[area + 1])
23813 {
23814 /* If the glyph row is reversed, we need to prepend the glyph
23815 rather than append it. */
23816 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23817 {
23818 struct glyph *g;
23819
23820 /* Make room for the new glyph. */
23821 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23822 g[1] = *g;
23823 glyph = it->glyph_row->glyphs[it->area];
23824 }
23825 glyph->charpos = it->cmp_it.charpos;
23826 glyph->object = it->object;
23827 glyph->pixel_width = it->pixel_width;
23828 glyph->ascent = it->ascent;
23829 glyph->descent = it->descent;
23830 glyph->voffset = it->voffset;
23831 glyph->type = COMPOSITE_GLYPH;
23832 if (it->cmp_it.ch < 0)
23833 {
23834 glyph->u.cmp.automatic = 0;
23835 glyph->u.cmp.id = it->cmp_it.id;
23836 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23837 }
23838 else
23839 {
23840 glyph->u.cmp.automatic = 1;
23841 glyph->u.cmp.id = it->cmp_it.id;
23842 glyph->slice.cmp.from = it->cmp_it.from;
23843 glyph->slice.cmp.to = it->cmp_it.to - 1;
23844 }
23845 glyph->avoid_cursor_p = it->avoid_cursor_p;
23846 glyph->multibyte_p = it->multibyte_p;
23847 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23848 {
23849 /* In R2L rows, the left and the right box edges need to be
23850 drawn in reverse direction. */
23851 glyph->right_box_line_p = it->start_of_box_run_p;
23852 glyph->left_box_line_p = it->end_of_box_run_p;
23853 }
23854 else
23855 {
23856 glyph->left_box_line_p = it->start_of_box_run_p;
23857 glyph->right_box_line_p = it->end_of_box_run_p;
23858 }
23859 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23860 || it->phys_descent > it->descent);
23861 glyph->padding_p = 0;
23862 glyph->glyph_not_available_p = 0;
23863 glyph->face_id = it->face_id;
23864 glyph->font_type = FONT_TYPE_UNKNOWN;
23865 if (it->bidi_p)
23866 {
23867 glyph->resolved_level = it->bidi_it.resolved_level;
23868 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23869 emacs_abort ();
23870 glyph->bidi_type = it->bidi_it.type;
23871 }
23872 ++it->glyph_row->used[area];
23873 }
23874 else
23875 IT_EXPAND_MATRIX_WIDTH (it, area);
23876 }
23877
23878
23879 /* Change IT->ascent and IT->height according to the setting of
23880 IT->voffset. */
23881
23882 static void
23883 take_vertical_position_into_account (struct it *it)
23884 {
23885 if (it->voffset)
23886 {
23887 if (it->voffset < 0)
23888 /* Increase the ascent so that we can display the text higher
23889 in the line. */
23890 it->ascent -= it->voffset;
23891 else
23892 /* Increase the descent so that we can display the text lower
23893 in the line. */
23894 it->descent += it->voffset;
23895 }
23896 }
23897
23898
23899 /* Produce glyphs/get display metrics for the image IT is loaded with.
23900 See the description of struct display_iterator in dispextern.h for
23901 an overview of struct display_iterator. */
23902
23903 static void
23904 produce_image_glyph (struct it *it)
23905 {
23906 struct image *img;
23907 struct face *face;
23908 int glyph_ascent, crop;
23909 struct glyph_slice slice;
23910
23911 eassert (it->what == IT_IMAGE);
23912
23913 face = FACE_FROM_ID (it->f, it->face_id);
23914 eassert (face);
23915 /* Make sure X resources of the face is loaded. */
23916 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23917
23918 if (it->image_id < 0)
23919 {
23920 /* Fringe bitmap. */
23921 it->ascent = it->phys_ascent = 0;
23922 it->descent = it->phys_descent = 0;
23923 it->pixel_width = 0;
23924 it->nglyphs = 0;
23925 return;
23926 }
23927
23928 img = IMAGE_FROM_ID (it->f, it->image_id);
23929 eassert (img);
23930 /* Make sure X resources of the image is loaded. */
23931 prepare_image_for_display (it->f, img);
23932
23933 slice.x = slice.y = 0;
23934 slice.width = img->width;
23935 slice.height = img->height;
23936
23937 if (INTEGERP (it->slice.x))
23938 slice.x = XINT (it->slice.x);
23939 else if (FLOATP (it->slice.x))
23940 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23941
23942 if (INTEGERP (it->slice.y))
23943 slice.y = XINT (it->slice.y);
23944 else if (FLOATP (it->slice.y))
23945 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23946
23947 if (INTEGERP (it->slice.width))
23948 slice.width = XINT (it->slice.width);
23949 else if (FLOATP (it->slice.width))
23950 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23951
23952 if (INTEGERP (it->slice.height))
23953 slice.height = XINT (it->slice.height);
23954 else if (FLOATP (it->slice.height))
23955 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23956
23957 if (slice.x >= img->width)
23958 slice.x = img->width;
23959 if (slice.y >= img->height)
23960 slice.y = img->height;
23961 if (slice.x + slice.width >= img->width)
23962 slice.width = img->width - slice.x;
23963 if (slice.y + slice.height > img->height)
23964 slice.height = img->height - slice.y;
23965
23966 if (slice.width == 0 || slice.height == 0)
23967 return;
23968
23969 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23970
23971 it->descent = slice.height - glyph_ascent;
23972 if (slice.y == 0)
23973 it->descent += img->vmargin;
23974 if (slice.y + slice.height == img->height)
23975 it->descent += img->vmargin;
23976 it->phys_descent = it->descent;
23977
23978 it->pixel_width = slice.width;
23979 if (slice.x == 0)
23980 it->pixel_width += img->hmargin;
23981 if (slice.x + slice.width == img->width)
23982 it->pixel_width += img->hmargin;
23983
23984 /* It's quite possible for images to have an ascent greater than
23985 their height, so don't get confused in that case. */
23986 if (it->descent < 0)
23987 it->descent = 0;
23988
23989 it->nglyphs = 1;
23990
23991 if (face->box != FACE_NO_BOX)
23992 {
23993 if (face->box_line_width > 0)
23994 {
23995 if (slice.y == 0)
23996 it->ascent += face->box_line_width;
23997 if (slice.y + slice.height == img->height)
23998 it->descent += face->box_line_width;
23999 }
24000
24001 if (it->start_of_box_run_p && slice.x == 0)
24002 it->pixel_width += eabs (face->box_line_width);
24003 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24004 it->pixel_width += eabs (face->box_line_width);
24005 }
24006
24007 take_vertical_position_into_account (it);
24008
24009 /* Automatically crop wide image glyphs at right edge so we can
24010 draw the cursor on same display row. */
24011 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24012 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24013 {
24014 it->pixel_width -= crop;
24015 slice.width -= crop;
24016 }
24017
24018 if (it->glyph_row)
24019 {
24020 struct glyph *glyph;
24021 enum glyph_row_area area = it->area;
24022
24023 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24024 if (glyph < it->glyph_row->glyphs[area + 1])
24025 {
24026 glyph->charpos = CHARPOS (it->position);
24027 glyph->object = it->object;
24028 glyph->pixel_width = it->pixel_width;
24029 glyph->ascent = glyph_ascent;
24030 glyph->descent = it->descent;
24031 glyph->voffset = it->voffset;
24032 glyph->type = IMAGE_GLYPH;
24033 glyph->avoid_cursor_p = it->avoid_cursor_p;
24034 glyph->multibyte_p = it->multibyte_p;
24035 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24036 {
24037 /* In R2L rows, the left and the right box edges need to be
24038 drawn in reverse direction. */
24039 glyph->right_box_line_p = it->start_of_box_run_p;
24040 glyph->left_box_line_p = it->end_of_box_run_p;
24041 }
24042 else
24043 {
24044 glyph->left_box_line_p = it->start_of_box_run_p;
24045 glyph->right_box_line_p = it->end_of_box_run_p;
24046 }
24047 glyph->overlaps_vertically_p = 0;
24048 glyph->padding_p = 0;
24049 glyph->glyph_not_available_p = 0;
24050 glyph->face_id = it->face_id;
24051 glyph->u.img_id = img->id;
24052 glyph->slice.img = slice;
24053 glyph->font_type = FONT_TYPE_UNKNOWN;
24054 if (it->bidi_p)
24055 {
24056 glyph->resolved_level = it->bidi_it.resolved_level;
24057 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24058 emacs_abort ();
24059 glyph->bidi_type = it->bidi_it.type;
24060 }
24061 ++it->glyph_row->used[area];
24062 }
24063 else
24064 IT_EXPAND_MATRIX_WIDTH (it, area);
24065 }
24066 }
24067
24068
24069 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24070 of the glyph, WIDTH and HEIGHT are the width and height of the
24071 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24072
24073 static void
24074 append_stretch_glyph (struct it *it, Lisp_Object object,
24075 int width, int height, int ascent)
24076 {
24077 struct glyph *glyph;
24078 enum glyph_row_area area = it->area;
24079
24080 eassert (ascent >= 0 && ascent <= height);
24081
24082 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24083 if (glyph < it->glyph_row->glyphs[area + 1])
24084 {
24085 /* If the glyph row is reversed, we need to prepend the glyph
24086 rather than append it. */
24087 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24088 {
24089 struct glyph *g;
24090
24091 /* Make room for the additional glyph. */
24092 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24093 g[1] = *g;
24094 glyph = it->glyph_row->glyphs[area];
24095 }
24096 glyph->charpos = CHARPOS (it->position);
24097 glyph->object = object;
24098 glyph->pixel_width = width;
24099 glyph->ascent = ascent;
24100 glyph->descent = height - ascent;
24101 glyph->voffset = it->voffset;
24102 glyph->type = STRETCH_GLYPH;
24103 glyph->avoid_cursor_p = it->avoid_cursor_p;
24104 glyph->multibyte_p = it->multibyte_p;
24105 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24106 {
24107 /* In R2L rows, the left and the right box edges need to be
24108 drawn in reverse direction. */
24109 glyph->right_box_line_p = it->start_of_box_run_p;
24110 glyph->left_box_line_p = it->end_of_box_run_p;
24111 }
24112 else
24113 {
24114 glyph->left_box_line_p = it->start_of_box_run_p;
24115 glyph->right_box_line_p = it->end_of_box_run_p;
24116 }
24117 glyph->overlaps_vertically_p = 0;
24118 glyph->padding_p = 0;
24119 glyph->glyph_not_available_p = 0;
24120 glyph->face_id = it->face_id;
24121 glyph->u.stretch.ascent = ascent;
24122 glyph->u.stretch.height = height;
24123 glyph->slice.img = null_glyph_slice;
24124 glyph->font_type = FONT_TYPE_UNKNOWN;
24125 if (it->bidi_p)
24126 {
24127 glyph->resolved_level = it->bidi_it.resolved_level;
24128 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24129 emacs_abort ();
24130 glyph->bidi_type = it->bidi_it.type;
24131 }
24132 else
24133 {
24134 glyph->resolved_level = 0;
24135 glyph->bidi_type = UNKNOWN_BT;
24136 }
24137 ++it->glyph_row->used[area];
24138 }
24139 else
24140 IT_EXPAND_MATRIX_WIDTH (it, area);
24141 }
24142
24143 #endif /* HAVE_WINDOW_SYSTEM */
24144
24145 /* Produce a stretch glyph for iterator IT. IT->object is the value
24146 of the glyph property displayed. The value must be a list
24147 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24148 being recognized:
24149
24150 1. `:width WIDTH' specifies that the space should be WIDTH *
24151 canonical char width wide. WIDTH may be an integer or floating
24152 point number.
24153
24154 2. `:relative-width FACTOR' specifies that the width of the stretch
24155 should be computed from the width of the first character having the
24156 `glyph' property, and should be FACTOR times that width.
24157
24158 3. `:align-to HPOS' specifies that the space should be wide enough
24159 to reach HPOS, a value in canonical character units.
24160
24161 Exactly one of the above pairs must be present.
24162
24163 4. `:height HEIGHT' specifies that the height of the stretch produced
24164 should be HEIGHT, measured in canonical character units.
24165
24166 5. `:relative-height FACTOR' specifies that the height of the
24167 stretch should be FACTOR times the height of the characters having
24168 the glyph property.
24169
24170 Either none or exactly one of 4 or 5 must be present.
24171
24172 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24173 of the stretch should be used for the ascent of the stretch.
24174 ASCENT must be in the range 0 <= ASCENT <= 100. */
24175
24176 void
24177 produce_stretch_glyph (struct it *it)
24178 {
24179 /* (space :width WIDTH :height HEIGHT ...) */
24180 Lisp_Object prop, plist;
24181 int width = 0, height = 0, align_to = -1;
24182 int zero_width_ok_p = 0;
24183 double tem;
24184 struct font *font = NULL;
24185
24186 #ifdef HAVE_WINDOW_SYSTEM
24187 int ascent = 0;
24188 int zero_height_ok_p = 0;
24189
24190 if (FRAME_WINDOW_P (it->f))
24191 {
24192 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24193 font = face->font ? face->font : FRAME_FONT (it->f);
24194 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24195 }
24196 #endif
24197
24198 /* List should start with `space'. */
24199 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24200 plist = XCDR (it->object);
24201
24202 /* Compute the width of the stretch. */
24203 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24204 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24205 {
24206 /* Absolute width `:width WIDTH' specified and valid. */
24207 zero_width_ok_p = 1;
24208 width = (int)tem;
24209 }
24210 #ifdef HAVE_WINDOW_SYSTEM
24211 else if (FRAME_WINDOW_P (it->f)
24212 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24213 {
24214 /* Relative width `:relative-width FACTOR' specified and valid.
24215 Compute the width of the characters having the `glyph'
24216 property. */
24217 struct it it2;
24218 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24219
24220 it2 = *it;
24221 if (it->multibyte_p)
24222 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24223 else
24224 {
24225 it2.c = it2.char_to_display = *p, it2.len = 1;
24226 if (! ASCII_CHAR_P (it2.c))
24227 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24228 }
24229
24230 it2.glyph_row = NULL;
24231 it2.what = IT_CHARACTER;
24232 x_produce_glyphs (&it2);
24233 width = NUMVAL (prop) * it2.pixel_width;
24234 }
24235 #endif /* HAVE_WINDOW_SYSTEM */
24236 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24237 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24238 {
24239 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24240 align_to = (align_to < 0
24241 ? 0
24242 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24243 else if (align_to < 0)
24244 align_to = window_box_left_offset (it->w, TEXT_AREA);
24245 width = max (0, (int)tem + align_to - it->current_x);
24246 zero_width_ok_p = 1;
24247 }
24248 else
24249 /* Nothing specified -> width defaults to canonical char width. */
24250 width = FRAME_COLUMN_WIDTH (it->f);
24251
24252 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24253 width = 1;
24254
24255 #ifdef HAVE_WINDOW_SYSTEM
24256 /* Compute height. */
24257 if (FRAME_WINDOW_P (it->f))
24258 {
24259 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24260 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24261 {
24262 height = (int)tem;
24263 zero_height_ok_p = 1;
24264 }
24265 else if (prop = Fplist_get (plist, QCrelative_height),
24266 NUMVAL (prop) > 0)
24267 height = FONT_HEIGHT (font) * NUMVAL (prop);
24268 else
24269 height = FONT_HEIGHT (font);
24270
24271 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24272 height = 1;
24273
24274 /* Compute percentage of height used for ascent. If
24275 `:ascent ASCENT' is present and valid, use that. Otherwise,
24276 derive the ascent from the font in use. */
24277 if (prop = Fplist_get (plist, QCascent),
24278 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24279 ascent = height * NUMVAL (prop) / 100.0;
24280 else if (!NILP (prop)
24281 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24282 ascent = min (max (0, (int)tem), height);
24283 else
24284 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24285 }
24286 else
24287 #endif /* HAVE_WINDOW_SYSTEM */
24288 height = 1;
24289
24290 if (width > 0 && it->line_wrap != TRUNCATE
24291 && it->current_x + width > it->last_visible_x)
24292 {
24293 width = it->last_visible_x - it->current_x;
24294 #ifdef HAVE_WINDOW_SYSTEM
24295 /* Subtract one more pixel from the stretch width, but only on
24296 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24297 width -= FRAME_WINDOW_P (it->f);
24298 #endif
24299 }
24300
24301 if (width > 0 && height > 0 && it->glyph_row)
24302 {
24303 Lisp_Object o_object = it->object;
24304 Lisp_Object object = it->stack[it->sp - 1].string;
24305 int n = width;
24306
24307 if (!STRINGP (object))
24308 object = it->w->buffer;
24309 #ifdef HAVE_WINDOW_SYSTEM
24310 if (FRAME_WINDOW_P (it->f))
24311 append_stretch_glyph (it, object, width, height, ascent);
24312 else
24313 #endif
24314 {
24315 it->object = object;
24316 it->char_to_display = ' ';
24317 it->pixel_width = it->len = 1;
24318 while (n--)
24319 tty_append_glyph (it);
24320 it->object = o_object;
24321 }
24322 }
24323
24324 it->pixel_width = width;
24325 #ifdef HAVE_WINDOW_SYSTEM
24326 if (FRAME_WINDOW_P (it->f))
24327 {
24328 it->ascent = it->phys_ascent = ascent;
24329 it->descent = it->phys_descent = height - it->ascent;
24330 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24331 take_vertical_position_into_account (it);
24332 }
24333 else
24334 #endif
24335 it->nglyphs = width;
24336 }
24337
24338 /* Get information about special display element WHAT in an
24339 environment described by IT. WHAT is one of IT_TRUNCATION or
24340 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24341 non-null glyph_row member. This function ensures that fields like
24342 face_id, c, len of IT are left untouched. */
24343
24344 static void
24345 produce_special_glyphs (struct it *it, enum display_element_type what)
24346 {
24347 struct it temp_it;
24348 Lisp_Object gc;
24349 GLYPH glyph;
24350
24351 temp_it = *it;
24352 temp_it.object = make_number (0);
24353 memset (&temp_it.current, 0, sizeof temp_it.current);
24354
24355 if (what == IT_CONTINUATION)
24356 {
24357 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24358 if (it->bidi_it.paragraph_dir == R2L)
24359 SET_GLYPH_FROM_CHAR (glyph, '/');
24360 else
24361 SET_GLYPH_FROM_CHAR (glyph, '\\');
24362 if (it->dp
24363 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24364 {
24365 /* FIXME: Should we mirror GC for R2L lines? */
24366 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24367 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24368 }
24369 }
24370 else if (what == IT_TRUNCATION)
24371 {
24372 /* Truncation glyph. */
24373 SET_GLYPH_FROM_CHAR (glyph, '$');
24374 if (it->dp
24375 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24376 {
24377 /* FIXME: Should we mirror GC for R2L lines? */
24378 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24379 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24380 }
24381 }
24382 else
24383 emacs_abort ();
24384
24385 #ifdef HAVE_WINDOW_SYSTEM
24386 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24387 is turned off, we precede the truncation/continuation glyphs by a
24388 stretch glyph whose width is computed such that these special
24389 glyphs are aligned at the window margin, even when very different
24390 fonts are used in different glyph rows. */
24391 if (FRAME_WINDOW_P (temp_it.f)
24392 /* init_iterator calls this with it->glyph_row == NULL, and it
24393 wants only the pixel width of the truncation/continuation
24394 glyphs. */
24395 && temp_it.glyph_row
24396 /* insert_left_trunc_glyphs calls us at the beginning of the
24397 row, and it has its own calculation of the stretch glyph
24398 width. */
24399 && temp_it.glyph_row->used[TEXT_AREA] > 0
24400 && (temp_it.glyph_row->reversed_p
24401 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24402 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24403 {
24404 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24405
24406 if (stretch_width > 0)
24407 {
24408 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24409 struct font *font =
24410 face->font ? face->font : FRAME_FONT (temp_it.f);
24411 int stretch_ascent =
24412 (((temp_it.ascent + temp_it.descent)
24413 * FONT_BASE (font)) / FONT_HEIGHT (font));
24414
24415 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24416 temp_it.ascent + temp_it.descent,
24417 stretch_ascent);
24418 }
24419 }
24420 #endif
24421
24422 temp_it.dp = NULL;
24423 temp_it.what = IT_CHARACTER;
24424 temp_it.len = 1;
24425 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24426 temp_it.face_id = GLYPH_FACE (glyph);
24427 temp_it.len = CHAR_BYTES (temp_it.c);
24428
24429 PRODUCE_GLYPHS (&temp_it);
24430 it->pixel_width = temp_it.pixel_width;
24431 it->nglyphs = temp_it.pixel_width;
24432 }
24433
24434 #ifdef HAVE_WINDOW_SYSTEM
24435
24436 /* Calculate line-height and line-spacing properties.
24437 An integer value specifies explicit pixel value.
24438 A float value specifies relative value to current face height.
24439 A cons (float . face-name) specifies relative value to
24440 height of specified face font.
24441
24442 Returns height in pixels, or nil. */
24443
24444
24445 static Lisp_Object
24446 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24447 int boff, int override)
24448 {
24449 Lisp_Object face_name = Qnil;
24450 int ascent, descent, height;
24451
24452 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24453 return val;
24454
24455 if (CONSP (val))
24456 {
24457 face_name = XCAR (val);
24458 val = XCDR (val);
24459 if (!NUMBERP (val))
24460 val = make_number (1);
24461 if (NILP (face_name))
24462 {
24463 height = it->ascent + it->descent;
24464 goto scale;
24465 }
24466 }
24467
24468 if (NILP (face_name))
24469 {
24470 font = FRAME_FONT (it->f);
24471 boff = FRAME_BASELINE_OFFSET (it->f);
24472 }
24473 else if (EQ (face_name, Qt))
24474 {
24475 override = 0;
24476 }
24477 else
24478 {
24479 int face_id;
24480 struct face *face;
24481
24482 face_id = lookup_named_face (it->f, face_name, 0);
24483 if (face_id < 0)
24484 return make_number (-1);
24485
24486 face = FACE_FROM_ID (it->f, face_id);
24487 font = face->font;
24488 if (font == NULL)
24489 return make_number (-1);
24490 boff = font->baseline_offset;
24491 if (font->vertical_centering)
24492 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24493 }
24494
24495 ascent = FONT_BASE (font) + boff;
24496 descent = FONT_DESCENT (font) - boff;
24497
24498 if (override)
24499 {
24500 it->override_ascent = ascent;
24501 it->override_descent = descent;
24502 it->override_boff = boff;
24503 }
24504
24505 height = ascent + descent;
24506
24507 scale:
24508 if (FLOATP (val))
24509 height = (int)(XFLOAT_DATA (val) * height);
24510 else if (INTEGERP (val))
24511 height *= XINT (val);
24512
24513 return make_number (height);
24514 }
24515
24516
24517 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24518 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24519 and only if this is for a character for which no font was found.
24520
24521 If the display method (it->glyphless_method) is
24522 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24523 length of the acronym or the hexadecimal string, UPPER_XOFF and
24524 UPPER_YOFF are pixel offsets for the upper part of the string,
24525 LOWER_XOFF and LOWER_YOFF are for the lower part.
24526
24527 For the other display methods, LEN through LOWER_YOFF are zero. */
24528
24529 static void
24530 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24531 short upper_xoff, short upper_yoff,
24532 short lower_xoff, short lower_yoff)
24533 {
24534 struct glyph *glyph;
24535 enum glyph_row_area area = it->area;
24536
24537 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24538 if (glyph < it->glyph_row->glyphs[area + 1])
24539 {
24540 /* If the glyph row is reversed, we need to prepend the glyph
24541 rather than append it. */
24542 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24543 {
24544 struct glyph *g;
24545
24546 /* Make room for the additional glyph. */
24547 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24548 g[1] = *g;
24549 glyph = it->glyph_row->glyphs[area];
24550 }
24551 glyph->charpos = CHARPOS (it->position);
24552 glyph->object = it->object;
24553 glyph->pixel_width = it->pixel_width;
24554 glyph->ascent = it->ascent;
24555 glyph->descent = it->descent;
24556 glyph->voffset = it->voffset;
24557 glyph->type = GLYPHLESS_GLYPH;
24558 glyph->u.glyphless.method = it->glyphless_method;
24559 glyph->u.glyphless.for_no_font = for_no_font;
24560 glyph->u.glyphless.len = len;
24561 glyph->u.glyphless.ch = it->c;
24562 glyph->slice.glyphless.upper_xoff = upper_xoff;
24563 glyph->slice.glyphless.upper_yoff = upper_yoff;
24564 glyph->slice.glyphless.lower_xoff = lower_xoff;
24565 glyph->slice.glyphless.lower_yoff = lower_yoff;
24566 glyph->avoid_cursor_p = it->avoid_cursor_p;
24567 glyph->multibyte_p = it->multibyte_p;
24568 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24569 {
24570 /* In R2L rows, the left and the right box edges need to be
24571 drawn in reverse direction. */
24572 glyph->right_box_line_p = it->start_of_box_run_p;
24573 glyph->left_box_line_p = it->end_of_box_run_p;
24574 }
24575 else
24576 {
24577 glyph->left_box_line_p = it->start_of_box_run_p;
24578 glyph->right_box_line_p = it->end_of_box_run_p;
24579 }
24580 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24581 || it->phys_descent > it->descent);
24582 glyph->padding_p = 0;
24583 glyph->glyph_not_available_p = 0;
24584 glyph->face_id = face_id;
24585 glyph->font_type = FONT_TYPE_UNKNOWN;
24586 if (it->bidi_p)
24587 {
24588 glyph->resolved_level = it->bidi_it.resolved_level;
24589 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24590 emacs_abort ();
24591 glyph->bidi_type = it->bidi_it.type;
24592 }
24593 ++it->glyph_row->used[area];
24594 }
24595 else
24596 IT_EXPAND_MATRIX_WIDTH (it, area);
24597 }
24598
24599
24600 /* Produce a glyph for a glyphless character for iterator IT.
24601 IT->glyphless_method specifies which method to use for displaying
24602 the character. See the description of enum
24603 glyphless_display_method in dispextern.h for the detail.
24604
24605 FOR_NO_FONT is nonzero if and only if this is for a character for
24606 which no font was found. ACRONYM, if non-nil, is an acronym string
24607 for the character. */
24608
24609 static void
24610 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24611 {
24612 int face_id;
24613 struct face *face;
24614 struct font *font;
24615 int base_width, base_height, width, height;
24616 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24617 int len;
24618
24619 /* Get the metrics of the base font. We always refer to the current
24620 ASCII face. */
24621 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24622 font = face->font ? face->font : FRAME_FONT (it->f);
24623 it->ascent = FONT_BASE (font) + font->baseline_offset;
24624 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24625 base_height = it->ascent + it->descent;
24626 base_width = font->average_width;
24627
24628 /* Get a face ID for the glyph by utilizing a cache (the same way as
24629 done for `escape-glyph' in get_next_display_element). */
24630 if (it->f == last_glyphless_glyph_frame
24631 && it->face_id == last_glyphless_glyph_face_id)
24632 {
24633 face_id = last_glyphless_glyph_merged_face_id;
24634 }
24635 else
24636 {
24637 /* Merge the `glyphless-char' face into the current face. */
24638 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24639 last_glyphless_glyph_frame = it->f;
24640 last_glyphless_glyph_face_id = it->face_id;
24641 last_glyphless_glyph_merged_face_id = face_id;
24642 }
24643
24644 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24645 {
24646 it->pixel_width = THIN_SPACE_WIDTH;
24647 len = 0;
24648 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24649 }
24650 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24651 {
24652 width = CHAR_WIDTH (it->c);
24653 if (width == 0)
24654 width = 1;
24655 else if (width > 4)
24656 width = 4;
24657 it->pixel_width = base_width * width;
24658 len = 0;
24659 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24660 }
24661 else
24662 {
24663 char buf[7];
24664 const char *str;
24665 unsigned int code[6];
24666 int upper_len;
24667 int ascent, descent;
24668 struct font_metrics metrics_upper, metrics_lower;
24669
24670 face = FACE_FROM_ID (it->f, face_id);
24671 font = face->font ? face->font : FRAME_FONT (it->f);
24672 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24673
24674 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24675 {
24676 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24677 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24678 if (CONSP (acronym))
24679 acronym = XCAR (acronym);
24680 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24681 }
24682 else
24683 {
24684 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24685 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24686 str = buf;
24687 }
24688 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24689 code[len] = font->driver->encode_char (font, str[len]);
24690 upper_len = (len + 1) / 2;
24691 font->driver->text_extents (font, code, upper_len,
24692 &metrics_upper);
24693 font->driver->text_extents (font, code + upper_len, len - upper_len,
24694 &metrics_lower);
24695
24696
24697
24698 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24699 width = max (metrics_upper.width, metrics_lower.width) + 4;
24700 upper_xoff = upper_yoff = 2; /* the typical case */
24701 if (base_width >= width)
24702 {
24703 /* Align the upper to the left, the lower to the right. */
24704 it->pixel_width = base_width;
24705 lower_xoff = base_width - 2 - metrics_lower.width;
24706 }
24707 else
24708 {
24709 /* Center the shorter one. */
24710 it->pixel_width = width;
24711 if (metrics_upper.width >= metrics_lower.width)
24712 lower_xoff = (width - metrics_lower.width) / 2;
24713 else
24714 {
24715 /* FIXME: This code doesn't look right. It formerly was
24716 missing the "lower_xoff = 0;", which couldn't have
24717 been right since it left lower_xoff uninitialized. */
24718 lower_xoff = 0;
24719 upper_xoff = (width - metrics_upper.width) / 2;
24720 }
24721 }
24722
24723 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24724 top, bottom, and between upper and lower strings. */
24725 height = (metrics_upper.ascent + metrics_upper.descent
24726 + metrics_lower.ascent + metrics_lower.descent) + 5;
24727 /* Center vertically.
24728 H:base_height, D:base_descent
24729 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24730
24731 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24732 descent = D - H/2 + h/2;
24733 lower_yoff = descent - 2 - ld;
24734 upper_yoff = lower_yoff - la - 1 - ud; */
24735 ascent = - (it->descent - (base_height + height + 1) / 2);
24736 descent = it->descent - (base_height - height) / 2;
24737 lower_yoff = descent - 2 - metrics_lower.descent;
24738 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24739 - metrics_upper.descent);
24740 /* Don't make the height shorter than the base height. */
24741 if (height > base_height)
24742 {
24743 it->ascent = ascent;
24744 it->descent = descent;
24745 }
24746 }
24747
24748 it->phys_ascent = it->ascent;
24749 it->phys_descent = it->descent;
24750 if (it->glyph_row)
24751 append_glyphless_glyph (it, face_id, for_no_font, len,
24752 upper_xoff, upper_yoff,
24753 lower_xoff, lower_yoff);
24754 it->nglyphs = 1;
24755 take_vertical_position_into_account (it);
24756 }
24757
24758
24759 /* RIF:
24760 Produce glyphs/get display metrics for the display element IT is
24761 loaded with. See the description of struct it in dispextern.h
24762 for an overview of struct it. */
24763
24764 void
24765 x_produce_glyphs (struct it *it)
24766 {
24767 int extra_line_spacing = it->extra_line_spacing;
24768
24769 it->glyph_not_available_p = 0;
24770
24771 if (it->what == IT_CHARACTER)
24772 {
24773 XChar2b char2b;
24774 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24775 struct font *font = face->font;
24776 struct font_metrics *pcm = NULL;
24777 int boff; /* baseline offset */
24778
24779 if (font == NULL)
24780 {
24781 /* When no suitable font is found, display this character by
24782 the method specified in the first extra slot of
24783 Vglyphless_char_display. */
24784 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24785
24786 eassert (it->what == IT_GLYPHLESS);
24787 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24788 goto done;
24789 }
24790
24791 boff = font->baseline_offset;
24792 if (font->vertical_centering)
24793 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24794
24795 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24796 {
24797 int stretched_p;
24798
24799 it->nglyphs = 1;
24800
24801 if (it->override_ascent >= 0)
24802 {
24803 it->ascent = it->override_ascent;
24804 it->descent = it->override_descent;
24805 boff = it->override_boff;
24806 }
24807 else
24808 {
24809 it->ascent = FONT_BASE (font) + boff;
24810 it->descent = FONT_DESCENT (font) - boff;
24811 }
24812
24813 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24814 {
24815 pcm = get_per_char_metric (font, &char2b);
24816 if (pcm->width == 0
24817 && pcm->rbearing == 0 && pcm->lbearing == 0)
24818 pcm = NULL;
24819 }
24820
24821 if (pcm)
24822 {
24823 it->phys_ascent = pcm->ascent + boff;
24824 it->phys_descent = pcm->descent - boff;
24825 it->pixel_width = pcm->width;
24826 }
24827 else
24828 {
24829 it->glyph_not_available_p = 1;
24830 it->phys_ascent = it->ascent;
24831 it->phys_descent = it->descent;
24832 it->pixel_width = font->space_width;
24833 }
24834
24835 if (it->constrain_row_ascent_descent_p)
24836 {
24837 if (it->descent > it->max_descent)
24838 {
24839 it->ascent += it->descent - it->max_descent;
24840 it->descent = it->max_descent;
24841 }
24842 if (it->ascent > it->max_ascent)
24843 {
24844 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24845 it->ascent = it->max_ascent;
24846 }
24847 it->phys_ascent = min (it->phys_ascent, it->ascent);
24848 it->phys_descent = min (it->phys_descent, it->descent);
24849 extra_line_spacing = 0;
24850 }
24851
24852 /* If this is a space inside a region of text with
24853 `space-width' property, change its width. */
24854 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24855 if (stretched_p)
24856 it->pixel_width *= XFLOATINT (it->space_width);
24857
24858 /* If face has a box, add the box thickness to the character
24859 height. If character has a box line to the left and/or
24860 right, add the box line width to the character's width. */
24861 if (face->box != FACE_NO_BOX)
24862 {
24863 int thick = face->box_line_width;
24864
24865 if (thick > 0)
24866 {
24867 it->ascent += thick;
24868 it->descent += thick;
24869 }
24870 else
24871 thick = -thick;
24872
24873 if (it->start_of_box_run_p)
24874 it->pixel_width += thick;
24875 if (it->end_of_box_run_p)
24876 it->pixel_width += thick;
24877 }
24878
24879 /* If face has an overline, add the height of the overline
24880 (1 pixel) and a 1 pixel margin to the character height. */
24881 if (face->overline_p)
24882 it->ascent += overline_margin;
24883
24884 if (it->constrain_row_ascent_descent_p)
24885 {
24886 if (it->ascent > it->max_ascent)
24887 it->ascent = it->max_ascent;
24888 if (it->descent > it->max_descent)
24889 it->descent = it->max_descent;
24890 }
24891
24892 take_vertical_position_into_account (it);
24893
24894 /* If we have to actually produce glyphs, do it. */
24895 if (it->glyph_row)
24896 {
24897 if (stretched_p)
24898 {
24899 /* Translate a space with a `space-width' property
24900 into a stretch glyph. */
24901 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24902 / FONT_HEIGHT (font));
24903 append_stretch_glyph (it, it->object, it->pixel_width,
24904 it->ascent + it->descent, ascent);
24905 }
24906 else
24907 append_glyph (it);
24908
24909 /* If characters with lbearing or rbearing are displayed
24910 in this line, record that fact in a flag of the
24911 glyph row. This is used to optimize X output code. */
24912 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24913 it->glyph_row->contains_overlapping_glyphs_p = 1;
24914 }
24915 if (! stretched_p && it->pixel_width == 0)
24916 /* We assure that all visible glyphs have at least 1-pixel
24917 width. */
24918 it->pixel_width = 1;
24919 }
24920 else if (it->char_to_display == '\n')
24921 {
24922 /* A newline has no width, but we need the height of the
24923 line. But if previous part of the line sets a height,
24924 don't increase that height */
24925
24926 Lisp_Object height;
24927 Lisp_Object total_height = Qnil;
24928
24929 it->override_ascent = -1;
24930 it->pixel_width = 0;
24931 it->nglyphs = 0;
24932
24933 height = get_it_property (it, Qline_height);
24934 /* Split (line-height total-height) list */
24935 if (CONSP (height)
24936 && CONSP (XCDR (height))
24937 && NILP (XCDR (XCDR (height))))
24938 {
24939 total_height = XCAR (XCDR (height));
24940 height = XCAR (height);
24941 }
24942 height = calc_line_height_property (it, height, font, boff, 1);
24943
24944 if (it->override_ascent >= 0)
24945 {
24946 it->ascent = it->override_ascent;
24947 it->descent = it->override_descent;
24948 boff = it->override_boff;
24949 }
24950 else
24951 {
24952 it->ascent = FONT_BASE (font) + boff;
24953 it->descent = FONT_DESCENT (font) - boff;
24954 }
24955
24956 if (EQ (height, Qt))
24957 {
24958 if (it->descent > it->max_descent)
24959 {
24960 it->ascent += it->descent - it->max_descent;
24961 it->descent = it->max_descent;
24962 }
24963 if (it->ascent > it->max_ascent)
24964 {
24965 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24966 it->ascent = it->max_ascent;
24967 }
24968 it->phys_ascent = min (it->phys_ascent, it->ascent);
24969 it->phys_descent = min (it->phys_descent, it->descent);
24970 it->constrain_row_ascent_descent_p = 1;
24971 extra_line_spacing = 0;
24972 }
24973 else
24974 {
24975 Lisp_Object spacing;
24976
24977 it->phys_ascent = it->ascent;
24978 it->phys_descent = it->descent;
24979
24980 if ((it->max_ascent > 0 || it->max_descent > 0)
24981 && face->box != FACE_NO_BOX
24982 && face->box_line_width > 0)
24983 {
24984 it->ascent += face->box_line_width;
24985 it->descent += face->box_line_width;
24986 }
24987 if (!NILP (height)
24988 && XINT (height) > it->ascent + it->descent)
24989 it->ascent = XINT (height) - it->descent;
24990
24991 if (!NILP (total_height))
24992 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24993 else
24994 {
24995 spacing = get_it_property (it, Qline_spacing);
24996 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24997 }
24998 if (INTEGERP (spacing))
24999 {
25000 extra_line_spacing = XINT (spacing);
25001 if (!NILP (total_height))
25002 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25003 }
25004 }
25005 }
25006 else /* i.e. (it->char_to_display == '\t') */
25007 {
25008 if (font->space_width > 0)
25009 {
25010 int tab_width = it->tab_width * font->space_width;
25011 int x = it->current_x + it->continuation_lines_width;
25012 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25013
25014 /* If the distance from the current position to the next tab
25015 stop is less than a space character width, use the
25016 tab stop after that. */
25017 if (next_tab_x - x < font->space_width)
25018 next_tab_x += tab_width;
25019
25020 it->pixel_width = next_tab_x - x;
25021 it->nglyphs = 1;
25022 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25023 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25024
25025 if (it->glyph_row)
25026 {
25027 append_stretch_glyph (it, it->object, it->pixel_width,
25028 it->ascent + it->descent, it->ascent);
25029 }
25030 }
25031 else
25032 {
25033 it->pixel_width = 0;
25034 it->nglyphs = 1;
25035 }
25036 }
25037 }
25038 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25039 {
25040 /* A static composition.
25041
25042 Note: A composition is represented as one glyph in the
25043 glyph matrix. There are no padding glyphs.
25044
25045 Important note: pixel_width, ascent, and descent are the
25046 values of what is drawn by draw_glyphs (i.e. the values of
25047 the overall glyphs composed). */
25048 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25049 int boff; /* baseline offset */
25050 struct composition *cmp = composition_table[it->cmp_it.id];
25051 int glyph_len = cmp->glyph_len;
25052 struct font *font = face->font;
25053
25054 it->nglyphs = 1;
25055
25056 /* If we have not yet calculated pixel size data of glyphs of
25057 the composition for the current face font, calculate them
25058 now. Theoretically, we have to check all fonts for the
25059 glyphs, but that requires much time and memory space. So,
25060 here we check only the font of the first glyph. This may
25061 lead to incorrect display, but it's very rare, and C-l
25062 (recenter-top-bottom) can correct the display anyway. */
25063 if (! cmp->font || cmp->font != font)
25064 {
25065 /* Ascent and descent of the font of the first character
25066 of this composition (adjusted by baseline offset).
25067 Ascent and descent of overall glyphs should not be less
25068 than these, respectively. */
25069 int font_ascent, font_descent, font_height;
25070 /* Bounding box of the overall glyphs. */
25071 int leftmost, rightmost, lowest, highest;
25072 int lbearing, rbearing;
25073 int i, width, ascent, descent;
25074 int left_padded = 0, right_padded = 0;
25075 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25076 XChar2b char2b;
25077 struct font_metrics *pcm;
25078 int font_not_found_p;
25079 ptrdiff_t pos;
25080
25081 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25082 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25083 break;
25084 if (glyph_len < cmp->glyph_len)
25085 right_padded = 1;
25086 for (i = 0; i < glyph_len; i++)
25087 {
25088 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25089 break;
25090 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25091 }
25092 if (i > 0)
25093 left_padded = 1;
25094
25095 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25096 : IT_CHARPOS (*it));
25097 /* If no suitable font is found, use the default font. */
25098 font_not_found_p = font == NULL;
25099 if (font_not_found_p)
25100 {
25101 face = face->ascii_face;
25102 font = face->font;
25103 }
25104 boff = font->baseline_offset;
25105 if (font->vertical_centering)
25106 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25107 font_ascent = FONT_BASE (font) + boff;
25108 font_descent = FONT_DESCENT (font) - boff;
25109 font_height = FONT_HEIGHT (font);
25110
25111 cmp->font = font;
25112
25113 pcm = NULL;
25114 if (! font_not_found_p)
25115 {
25116 get_char_face_and_encoding (it->f, c, it->face_id,
25117 &char2b, 0);
25118 pcm = get_per_char_metric (font, &char2b);
25119 }
25120
25121 /* Initialize the bounding box. */
25122 if (pcm)
25123 {
25124 width = cmp->glyph_len > 0 ? pcm->width : 0;
25125 ascent = pcm->ascent;
25126 descent = pcm->descent;
25127 lbearing = pcm->lbearing;
25128 rbearing = pcm->rbearing;
25129 }
25130 else
25131 {
25132 width = cmp->glyph_len > 0 ? font->space_width : 0;
25133 ascent = FONT_BASE (font);
25134 descent = FONT_DESCENT (font);
25135 lbearing = 0;
25136 rbearing = width;
25137 }
25138
25139 rightmost = width;
25140 leftmost = 0;
25141 lowest = - descent + boff;
25142 highest = ascent + boff;
25143
25144 if (! font_not_found_p
25145 && font->default_ascent
25146 && CHAR_TABLE_P (Vuse_default_ascent)
25147 && !NILP (Faref (Vuse_default_ascent,
25148 make_number (it->char_to_display))))
25149 highest = font->default_ascent + boff;
25150
25151 /* Draw the first glyph at the normal position. It may be
25152 shifted to right later if some other glyphs are drawn
25153 at the left. */
25154 cmp->offsets[i * 2] = 0;
25155 cmp->offsets[i * 2 + 1] = boff;
25156 cmp->lbearing = lbearing;
25157 cmp->rbearing = rbearing;
25158
25159 /* Set cmp->offsets for the remaining glyphs. */
25160 for (i++; i < glyph_len; i++)
25161 {
25162 int left, right, btm, top;
25163 int ch = COMPOSITION_GLYPH (cmp, i);
25164 int face_id;
25165 struct face *this_face;
25166
25167 if (ch == '\t')
25168 ch = ' ';
25169 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25170 this_face = FACE_FROM_ID (it->f, face_id);
25171 font = this_face->font;
25172
25173 if (font == NULL)
25174 pcm = NULL;
25175 else
25176 {
25177 get_char_face_and_encoding (it->f, ch, face_id,
25178 &char2b, 0);
25179 pcm = get_per_char_metric (font, &char2b);
25180 }
25181 if (! pcm)
25182 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25183 else
25184 {
25185 width = pcm->width;
25186 ascent = pcm->ascent;
25187 descent = pcm->descent;
25188 lbearing = pcm->lbearing;
25189 rbearing = pcm->rbearing;
25190 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25191 {
25192 /* Relative composition with or without
25193 alternate chars. */
25194 left = (leftmost + rightmost - width) / 2;
25195 btm = - descent + boff;
25196 if (font->relative_compose
25197 && (! CHAR_TABLE_P (Vignore_relative_composition)
25198 || NILP (Faref (Vignore_relative_composition,
25199 make_number (ch)))))
25200 {
25201
25202 if (- descent >= font->relative_compose)
25203 /* One extra pixel between two glyphs. */
25204 btm = highest + 1;
25205 else if (ascent <= 0)
25206 /* One extra pixel between two glyphs. */
25207 btm = lowest - 1 - ascent - descent;
25208 }
25209 }
25210 else
25211 {
25212 /* A composition rule is specified by an integer
25213 value that encodes global and new reference
25214 points (GREF and NREF). GREF and NREF are
25215 specified by numbers as below:
25216
25217 0---1---2 -- ascent
25218 | |
25219 | |
25220 | |
25221 9--10--11 -- center
25222 | |
25223 ---3---4---5--- baseline
25224 | |
25225 6---7---8 -- descent
25226 */
25227 int rule = COMPOSITION_RULE (cmp, i);
25228 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25229
25230 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25231 grefx = gref % 3, nrefx = nref % 3;
25232 grefy = gref / 3, nrefy = nref / 3;
25233 if (xoff)
25234 xoff = font_height * (xoff - 128) / 256;
25235 if (yoff)
25236 yoff = font_height * (yoff - 128) / 256;
25237
25238 left = (leftmost
25239 + grefx * (rightmost - leftmost) / 2
25240 - nrefx * width / 2
25241 + xoff);
25242
25243 btm = ((grefy == 0 ? highest
25244 : grefy == 1 ? 0
25245 : grefy == 2 ? lowest
25246 : (highest + lowest) / 2)
25247 - (nrefy == 0 ? ascent + descent
25248 : nrefy == 1 ? descent - boff
25249 : nrefy == 2 ? 0
25250 : (ascent + descent) / 2)
25251 + yoff);
25252 }
25253
25254 cmp->offsets[i * 2] = left;
25255 cmp->offsets[i * 2 + 1] = btm + descent;
25256
25257 /* Update the bounding box of the overall glyphs. */
25258 if (width > 0)
25259 {
25260 right = left + width;
25261 if (left < leftmost)
25262 leftmost = left;
25263 if (right > rightmost)
25264 rightmost = right;
25265 }
25266 top = btm + descent + ascent;
25267 if (top > highest)
25268 highest = top;
25269 if (btm < lowest)
25270 lowest = btm;
25271
25272 if (cmp->lbearing > left + lbearing)
25273 cmp->lbearing = left + lbearing;
25274 if (cmp->rbearing < left + rbearing)
25275 cmp->rbearing = left + rbearing;
25276 }
25277 }
25278
25279 /* If there are glyphs whose x-offsets are negative,
25280 shift all glyphs to the right and make all x-offsets
25281 non-negative. */
25282 if (leftmost < 0)
25283 {
25284 for (i = 0; i < cmp->glyph_len; i++)
25285 cmp->offsets[i * 2] -= leftmost;
25286 rightmost -= leftmost;
25287 cmp->lbearing -= leftmost;
25288 cmp->rbearing -= leftmost;
25289 }
25290
25291 if (left_padded && cmp->lbearing < 0)
25292 {
25293 for (i = 0; i < cmp->glyph_len; i++)
25294 cmp->offsets[i * 2] -= cmp->lbearing;
25295 rightmost -= cmp->lbearing;
25296 cmp->rbearing -= cmp->lbearing;
25297 cmp->lbearing = 0;
25298 }
25299 if (right_padded && rightmost < cmp->rbearing)
25300 {
25301 rightmost = cmp->rbearing;
25302 }
25303
25304 cmp->pixel_width = rightmost;
25305 cmp->ascent = highest;
25306 cmp->descent = - lowest;
25307 if (cmp->ascent < font_ascent)
25308 cmp->ascent = font_ascent;
25309 if (cmp->descent < font_descent)
25310 cmp->descent = font_descent;
25311 }
25312
25313 if (it->glyph_row
25314 && (cmp->lbearing < 0
25315 || cmp->rbearing > cmp->pixel_width))
25316 it->glyph_row->contains_overlapping_glyphs_p = 1;
25317
25318 it->pixel_width = cmp->pixel_width;
25319 it->ascent = it->phys_ascent = cmp->ascent;
25320 it->descent = it->phys_descent = cmp->descent;
25321 if (face->box != FACE_NO_BOX)
25322 {
25323 int thick = face->box_line_width;
25324
25325 if (thick > 0)
25326 {
25327 it->ascent += thick;
25328 it->descent += thick;
25329 }
25330 else
25331 thick = - thick;
25332
25333 if (it->start_of_box_run_p)
25334 it->pixel_width += thick;
25335 if (it->end_of_box_run_p)
25336 it->pixel_width += thick;
25337 }
25338
25339 /* If face has an overline, add the height of the overline
25340 (1 pixel) and a 1 pixel margin to the character height. */
25341 if (face->overline_p)
25342 it->ascent += overline_margin;
25343
25344 take_vertical_position_into_account (it);
25345 if (it->ascent < 0)
25346 it->ascent = 0;
25347 if (it->descent < 0)
25348 it->descent = 0;
25349
25350 if (it->glyph_row && cmp->glyph_len > 0)
25351 append_composite_glyph (it);
25352 }
25353 else if (it->what == IT_COMPOSITION)
25354 {
25355 /* A dynamic (automatic) composition. */
25356 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25357 Lisp_Object gstring;
25358 struct font_metrics metrics;
25359
25360 it->nglyphs = 1;
25361
25362 gstring = composition_gstring_from_id (it->cmp_it.id);
25363 it->pixel_width
25364 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25365 &metrics);
25366 if (it->glyph_row
25367 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25368 it->glyph_row->contains_overlapping_glyphs_p = 1;
25369 it->ascent = it->phys_ascent = metrics.ascent;
25370 it->descent = it->phys_descent = metrics.descent;
25371 if (face->box != FACE_NO_BOX)
25372 {
25373 int thick = face->box_line_width;
25374
25375 if (thick > 0)
25376 {
25377 it->ascent += thick;
25378 it->descent += thick;
25379 }
25380 else
25381 thick = - thick;
25382
25383 if (it->start_of_box_run_p)
25384 it->pixel_width += thick;
25385 if (it->end_of_box_run_p)
25386 it->pixel_width += thick;
25387 }
25388 /* If face has an overline, add the height of the overline
25389 (1 pixel) and a 1 pixel margin to the character height. */
25390 if (face->overline_p)
25391 it->ascent += overline_margin;
25392 take_vertical_position_into_account (it);
25393 if (it->ascent < 0)
25394 it->ascent = 0;
25395 if (it->descent < 0)
25396 it->descent = 0;
25397
25398 if (it->glyph_row)
25399 append_composite_glyph (it);
25400 }
25401 else if (it->what == IT_GLYPHLESS)
25402 produce_glyphless_glyph (it, 0, Qnil);
25403 else if (it->what == IT_IMAGE)
25404 produce_image_glyph (it);
25405 else if (it->what == IT_STRETCH)
25406 produce_stretch_glyph (it);
25407
25408 done:
25409 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25410 because this isn't true for images with `:ascent 100'. */
25411 eassert (it->ascent >= 0 && it->descent >= 0);
25412 if (it->area == TEXT_AREA)
25413 it->current_x += it->pixel_width;
25414
25415 if (extra_line_spacing > 0)
25416 {
25417 it->descent += extra_line_spacing;
25418 if (extra_line_spacing > it->max_extra_line_spacing)
25419 it->max_extra_line_spacing = extra_line_spacing;
25420 }
25421
25422 it->max_ascent = max (it->max_ascent, it->ascent);
25423 it->max_descent = max (it->max_descent, it->descent);
25424 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25425 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25426 }
25427
25428 /* EXPORT for RIF:
25429 Output LEN glyphs starting at START at the nominal cursor position.
25430 Advance the nominal cursor over the text. The global variable
25431 updated_window contains the window being updated, updated_row is
25432 the glyph row being updated, and updated_area is the area of that
25433 row being updated. */
25434
25435 void
25436 x_write_glyphs (struct glyph *start, int len)
25437 {
25438 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25439
25440 eassert (updated_window && updated_row);
25441 /* When the window is hscrolled, cursor hpos can legitimately be out
25442 of bounds, but we draw the cursor at the corresponding window
25443 margin in that case. */
25444 if (!updated_row->reversed_p && chpos < 0)
25445 chpos = 0;
25446 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25447 chpos = updated_row->used[TEXT_AREA] - 1;
25448
25449 block_input ();
25450
25451 /* Write glyphs. */
25452
25453 hpos = start - updated_row->glyphs[updated_area];
25454 x = draw_glyphs (updated_window, output_cursor.x,
25455 updated_row, updated_area,
25456 hpos, hpos + len,
25457 DRAW_NORMAL_TEXT, 0);
25458
25459 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25460 if (updated_area == TEXT_AREA
25461 && updated_window->phys_cursor_on_p
25462 && updated_window->phys_cursor.vpos == output_cursor.vpos
25463 && chpos >= hpos
25464 && chpos < hpos + len)
25465 updated_window->phys_cursor_on_p = 0;
25466
25467 unblock_input ();
25468
25469 /* Advance the output cursor. */
25470 output_cursor.hpos += len;
25471 output_cursor.x = x;
25472 }
25473
25474
25475 /* EXPORT for RIF:
25476 Insert LEN glyphs from START at the nominal cursor position. */
25477
25478 void
25479 x_insert_glyphs (struct glyph *start, int len)
25480 {
25481 struct frame *f;
25482 struct window *w;
25483 int line_height, shift_by_width, shifted_region_width;
25484 struct glyph_row *row;
25485 struct glyph *glyph;
25486 int frame_x, frame_y;
25487 ptrdiff_t hpos;
25488
25489 eassert (updated_window && updated_row);
25490 block_input ();
25491 w = updated_window;
25492 f = XFRAME (WINDOW_FRAME (w));
25493
25494 /* Get the height of the line we are in. */
25495 row = updated_row;
25496 line_height = row->height;
25497
25498 /* Get the width of the glyphs to insert. */
25499 shift_by_width = 0;
25500 for (glyph = start; glyph < start + len; ++glyph)
25501 shift_by_width += glyph->pixel_width;
25502
25503 /* Get the width of the region to shift right. */
25504 shifted_region_width = (window_box_width (w, updated_area)
25505 - output_cursor.x
25506 - shift_by_width);
25507
25508 /* Shift right. */
25509 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25510 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25511
25512 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25513 line_height, shift_by_width);
25514
25515 /* Write the glyphs. */
25516 hpos = start - row->glyphs[updated_area];
25517 draw_glyphs (w, output_cursor.x, row, updated_area,
25518 hpos, hpos + len,
25519 DRAW_NORMAL_TEXT, 0);
25520
25521 /* Advance the output cursor. */
25522 output_cursor.hpos += len;
25523 output_cursor.x += shift_by_width;
25524 unblock_input ();
25525 }
25526
25527
25528 /* EXPORT for RIF:
25529 Erase the current text line from the nominal cursor position
25530 (inclusive) to pixel column TO_X (exclusive). The idea is that
25531 everything from TO_X onward is already erased.
25532
25533 TO_X is a pixel position relative to updated_area of
25534 updated_window. TO_X == -1 means clear to the end of this area. */
25535
25536 void
25537 x_clear_end_of_line (int to_x)
25538 {
25539 struct frame *f;
25540 struct window *w = updated_window;
25541 int max_x, min_y, max_y;
25542 int from_x, from_y, to_y;
25543
25544 eassert (updated_window && updated_row);
25545 f = XFRAME (w->frame);
25546
25547 if (updated_row->full_width_p)
25548 max_x = WINDOW_TOTAL_WIDTH (w);
25549 else
25550 max_x = window_box_width (w, updated_area);
25551 max_y = window_text_bottom_y (w);
25552
25553 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25554 of window. For TO_X > 0, truncate to end of drawing area. */
25555 if (to_x == 0)
25556 return;
25557 else if (to_x < 0)
25558 to_x = max_x;
25559 else
25560 to_x = min (to_x, max_x);
25561
25562 to_y = min (max_y, output_cursor.y + updated_row->height);
25563
25564 /* Notice if the cursor will be cleared by this operation. */
25565 if (!updated_row->full_width_p)
25566 notice_overwritten_cursor (w, updated_area,
25567 output_cursor.x, -1,
25568 updated_row->y,
25569 MATRIX_ROW_BOTTOM_Y (updated_row));
25570
25571 from_x = output_cursor.x;
25572
25573 /* Translate to frame coordinates. */
25574 if (updated_row->full_width_p)
25575 {
25576 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25577 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25578 }
25579 else
25580 {
25581 int area_left = window_box_left (w, updated_area);
25582 from_x += area_left;
25583 to_x += area_left;
25584 }
25585
25586 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25587 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25588 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25589
25590 /* Prevent inadvertently clearing to end of the X window. */
25591 if (to_x > from_x && to_y > from_y)
25592 {
25593 block_input ();
25594 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25595 to_x - from_x, to_y - from_y);
25596 unblock_input ();
25597 }
25598 }
25599
25600 #endif /* HAVE_WINDOW_SYSTEM */
25601
25602
25603 \f
25604 /***********************************************************************
25605 Cursor types
25606 ***********************************************************************/
25607
25608 /* Value is the internal representation of the specified cursor type
25609 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25610 of the bar cursor. */
25611
25612 static enum text_cursor_kinds
25613 get_specified_cursor_type (Lisp_Object arg, int *width)
25614 {
25615 enum text_cursor_kinds type;
25616
25617 if (NILP (arg))
25618 return NO_CURSOR;
25619
25620 if (EQ (arg, Qbox))
25621 return FILLED_BOX_CURSOR;
25622
25623 if (EQ (arg, Qhollow))
25624 return HOLLOW_BOX_CURSOR;
25625
25626 if (EQ (arg, Qbar))
25627 {
25628 *width = 2;
25629 return BAR_CURSOR;
25630 }
25631
25632 if (CONSP (arg)
25633 && EQ (XCAR (arg), Qbar)
25634 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25635 {
25636 *width = XINT (XCDR (arg));
25637 return BAR_CURSOR;
25638 }
25639
25640 if (EQ (arg, Qhbar))
25641 {
25642 *width = 2;
25643 return HBAR_CURSOR;
25644 }
25645
25646 if (CONSP (arg)
25647 && EQ (XCAR (arg), Qhbar)
25648 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25649 {
25650 *width = XINT (XCDR (arg));
25651 return HBAR_CURSOR;
25652 }
25653
25654 /* Treat anything unknown as "hollow box cursor".
25655 It was bad to signal an error; people have trouble fixing
25656 .Xdefaults with Emacs, when it has something bad in it. */
25657 type = HOLLOW_BOX_CURSOR;
25658
25659 return type;
25660 }
25661
25662 /* Set the default cursor types for specified frame. */
25663 void
25664 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25665 {
25666 int width = 1;
25667 Lisp_Object tem;
25668
25669 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25670 FRAME_CURSOR_WIDTH (f) = width;
25671
25672 /* By default, set up the blink-off state depending on the on-state. */
25673
25674 tem = Fassoc (arg, Vblink_cursor_alist);
25675 if (!NILP (tem))
25676 {
25677 FRAME_BLINK_OFF_CURSOR (f)
25678 = get_specified_cursor_type (XCDR (tem), &width);
25679 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25680 }
25681 else
25682 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25683 }
25684
25685
25686 #ifdef HAVE_WINDOW_SYSTEM
25687
25688 /* Return the cursor we want to be displayed in window W. Return
25689 width of bar/hbar cursor through WIDTH arg. Return with
25690 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25691 (i.e. if the `system caret' should track this cursor).
25692
25693 In a mini-buffer window, we want the cursor only to appear if we
25694 are reading input from this window. For the selected window, we
25695 want the cursor type given by the frame parameter or buffer local
25696 setting of cursor-type. If explicitly marked off, draw no cursor.
25697 In all other cases, we want a hollow box cursor. */
25698
25699 static enum text_cursor_kinds
25700 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25701 int *active_cursor)
25702 {
25703 struct frame *f = XFRAME (w->frame);
25704 struct buffer *b = XBUFFER (w->buffer);
25705 int cursor_type = DEFAULT_CURSOR;
25706 Lisp_Object alt_cursor;
25707 int non_selected = 0;
25708
25709 *active_cursor = 1;
25710
25711 /* Echo area */
25712 if (cursor_in_echo_area
25713 && FRAME_HAS_MINIBUF_P (f)
25714 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25715 {
25716 if (w == XWINDOW (echo_area_window))
25717 {
25718 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25719 {
25720 *width = FRAME_CURSOR_WIDTH (f);
25721 return FRAME_DESIRED_CURSOR (f);
25722 }
25723 else
25724 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25725 }
25726
25727 *active_cursor = 0;
25728 non_selected = 1;
25729 }
25730
25731 /* Detect a nonselected window or nonselected frame. */
25732 else if (w != XWINDOW (f->selected_window)
25733 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25734 {
25735 *active_cursor = 0;
25736
25737 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25738 return NO_CURSOR;
25739
25740 non_selected = 1;
25741 }
25742
25743 /* Never display a cursor in a window in which cursor-type is nil. */
25744 if (NILP (BVAR (b, cursor_type)))
25745 return NO_CURSOR;
25746
25747 /* Get the normal cursor type for this window. */
25748 if (EQ (BVAR (b, cursor_type), Qt))
25749 {
25750 cursor_type = FRAME_DESIRED_CURSOR (f);
25751 *width = FRAME_CURSOR_WIDTH (f);
25752 }
25753 else
25754 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25755
25756 /* Use cursor-in-non-selected-windows instead
25757 for non-selected window or frame. */
25758 if (non_selected)
25759 {
25760 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25761 if (!EQ (Qt, alt_cursor))
25762 return get_specified_cursor_type (alt_cursor, width);
25763 /* t means modify the normal cursor type. */
25764 if (cursor_type == FILLED_BOX_CURSOR)
25765 cursor_type = HOLLOW_BOX_CURSOR;
25766 else if (cursor_type == BAR_CURSOR && *width > 1)
25767 --*width;
25768 return cursor_type;
25769 }
25770
25771 /* Use normal cursor if not blinked off. */
25772 if (!w->cursor_off_p)
25773 {
25774 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25775 {
25776 if (cursor_type == FILLED_BOX_CURSOR)
25777 {
25778 /* Using a block cursor on large images can be very annoying.
25779 So use a hollow cursor for "large" images.
25780 If image is not transparent (no mask), also use hollow cursor. */
25781 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25782 if (img != NULL && IMAGEP (img->spec))
25783 {
25784 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25785 where N = size of default frame font size.
25786 This should cover most of the "tiny" icons people may use. */
25787 if (!img->mask
25788 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25789 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25790 cursor_type = HOLLOW_BOX_CURSOR;
25791 }
25792 }
25793 else if (cursor_type != NO_CURSOR)
25794 {
25795 /* Display current only supports BOX and HOLLOW cursors for images.
25796 So for now, unconditionally use a HOLLOW cursor when cursor is
25797 not a solid box cursor. */
25798 cursor_type = HOLLOW_BOX_CURSOR;
25799 }
25800 }
25801 return cursor_type;
25802 }
25803
25804 /* Cursor is blinked off, so determine how to "toggle" it. */
25805
25806 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25807 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25808 return get_specified_cursor_type (XCDR (alt_cursor), width);
25809
25810 /* Then see if frame has specified a specific blink off cursor type. */
25811 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25812 {
25813 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25814 return FRAME_BLINK_OFF_CURSOR (f);
25815 }
25816
25817 #if 0
25818 /* Some people liked having a permanently visible blinking cursor,
25819 while others had very strong opinions against it. So it was
25820 decided to remove it. KFS 2003-09-03 */
25821
25822 /* Finally perform built-in cursor blinking:
25823 filled box <-> hollow box
25824 wide [h]bar <-> narrow [h]bar
25825 narrow [h]bar <-> no cursor
25826 other type <-> no cursor */
25827
25828 if (cursor_type == FILLED_BOX_CURSOR)
25829 return HOLLOW_BOX_CURSOR;
25830
25831 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25832 {
25833 *width = 1;
25834 return cursor_type;
25835 }
25836 #endif
25837
25838 return NO_CURSOR;
25839 }
25840
25841
25842 /* Notice when the text cursor of window W has been completely
25843 overwritten by a drawing operation that outputs glyphs in AREA
25844 starting at X0 and ending at X1 in the line starting at Y0 and
25845 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25846 the rest of the line after X0 has been written. Y coordinates
25847 are window-relative. */
25848
25849 static void
25850 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25851 int x0, int x1, int y0, int y1)
25852 {
25853 int cx0, cx1, cy0, cy1;
25854 struct glyph_row *row;
25855
25856 if (!w->phys_cursor_on_p)
25857 return;
25858 if (area != TEXT_AREA)
25859 return;
25860
25861 if (w->phys_cursor.vpos < 0
25862 || w->phys_cursor.vpos >= w->current_matrix->nrows
25863 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25864 !(row->enabled_p && row->displays_text_p)))
25865 return;
25866
25867 if (row->cursor_in_fringe_p)
25868 {
25869 row->cursor_in_fringe_p = 0;
25870 draw_fringe_bitmap (w, row, row->reversed_p);
25871 w->phys_cursor_on_p = 0;
25872 return;
25873 }
25874
25875 cx0 = w->phys_cursor.x;
25876 cx1 = cx0 + w->phys_cursor_width;
25877 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25878 return;
25879
25880 /* The cursor image will be completely removed from the
25881 screen if the output area intersects the cursor area in
25882 y-direction. When we draw in [y0 y1[, and some part of
25883 the cursor is at y < y0, that part must have been drawn
25884 before. When scrolling, the cursor is erased before
25885 actually scrolling, so we don't come here. When not
25886 scrolling, the rows above the old cursor row must have
25887 changed, and in this case these rows must have written
25888 over the cursor image.
25889
25890 Likewise if part of the cursor is below y1, with the
25891 exception of the cursor being in the first blank row at
25892 the buffer and window end because update_text_area
25893 doesn't draw that row. (Except when it does, but
25894 that's handled in update_text_area.) */
25895
25896 cy0 = w->phys_cursor.y;
25897 cy1 = cy0 + w->phys_cursor_height;
25898 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25899 return;
25900
25901 w->phys_cursor_on_p = 0;
25902 }
25903
25904 #endif /* HAVE_WINDOW_SYSTEM */
25905
25906 \f
25907 /************************************************************************
25908 Mouse Face
25909 ************************************************************************/
25910
25911 #ifdef HAVE_WINDOW_SYSTEM
25912
25913 /* EXPORT for RIF:
25914 Fix the display of area AREA of overlapping row ROW in window W
25915 with respect to the overlapping part OVERLAPS. */
25916
25917 void
25918 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25919 enum glyph_row_area area, int overlaps)
25920 {
25921 int i, x;
25922
25923 block_input ();
25924
25925 x = 0;
25926 for (i = 0; i < row->used[area];)
25927 {
25928 if (row->glyphs[area][i].overlaps_vertically_p)
25929 {
25930 int start = i, start_x = x;
25931
25932 do
25933 {
25934 x += row->glyphs[area][i].pixel_width;
25935 ++i;
25936 }
25937 while (i < row->used[area]
25938 && row->glyphs[area][i].overlaps_vertically_p);
25939
25940 draw_glyphs (w, start_x, row, area,
25941 start, i,
25942 DRAW_NORMAL_TEXT, overlaps);
25943 }
25944 else
25945 {
25946 x += row->glyphs[area][i].pixel_width;
25947 ++i;
25948 }
25949 }
25950
25951 unblock_input ();
25952 }
25953
25954
25955 /* EXPORT:
25956 Draw the cursor glyph of window W in glyph row ROW. See the
25957 comment of draw_glyphs for the meaning of HL. */
25958
25959 void
25960 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25961 enum draw_glyphs_face hl)
25962 {
25963 /* If cursor hpos is out of bounds, don't draw garbage. This can
25964 happen in mini-buffer windows when switching between echo area
25965 glyphs and mini-buffer. */
25966 if ((row->reversed_p
25967 ? (w->phys_cursor.hpos >= 0)
25968 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25969 {
25970 int on_p = w->phys_cursor_on_p;
25971 int x1;
25972 int hpos = w->phys_cursor.hpos;
25973
25974 /* When the window is hscrolled, cursor hpos can legitimately be
25975 out of bounds, but we draw the cursor at the corresponding
25976 window margin in that case. */
25977 if (!row->reversed_p && hpos < 0)
25978 hpos = 0;
25979 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25980 hpos = row->used[TEXT_AREA] - 1;
25981
25982 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25983 hl, 0);
25984 w->phys_cursor_on_p = on_p;
25985
25986 if (hl == DRAW_CURSOR)
25987 w->phys_cursor_width = x1 - w->phys_cursor.x;
25988 /* When we erase the cursor, and ROW is overlapped by other
25989 rows, make sure that these overlapping parts of other rows
25990 are redrawn. */
25991 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25992 {
25993 w->phys_cursor_width = x1 - w->phys_cursor.x;
25994
25995 if (row > w->current_matrix->rows
25996 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25997 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25998 OVERLAPS_ERASED_CURSOR);
25999
26000 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26001 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26002 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26003 OVERLAPS_ERASED_CURSOR);
26004 }
26005 }
26006 }
26007
26008
26009 /* EXPORT:
26010 Erase the image of a cursor of window W from the screen. */
26011
26012 void
26013 erase_phys_cursor (struct window *w)
26014 {
26015 struct frame *f = XFRAME (w->frame);
26016 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26017 int hpos = w->phys_cursor.hpos;
26018 int vpos = w->phys_cursor.vpos;
26019 int mouse_face_here_p = 0;
26020 struct glyph_matrix *active_glyphs = w->current_matrix;
26021 struct glyph_row *cursor_row;
26022 struct glyph *cursor_glyph;
26023 enum draw_glyphs_face hl;
26024
26025 /* No cursor displayed or row invalidated => nothing to do on the
26026 screen. */
26027 if (w->phys_cursor_type == NO_CURSOR)
26028 goto mark_cursor_off;
26029
26030 /* VPOS >= active_glyphs->nrows means that window has been resized.
26031 Don't bother to erase the cursor. */
26032 if (vpos >= active_glyphs->nrows)
26033 goto mark_cursor_off;
26034
26035 /* If row containing cursor is marked invalid, there is nothing we
26036 can do. */
26037 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26038 if (!cursor_row->enabled_p)
26039 goto mark_cursor_off;
26040
26041 /* If line spacing is > 0, old cursor may only be partially visible in
26042 window after split-window. So adjust visible height. */
26043 cursor_row->visible_height = min (cursor_row->visible_height,
26044 window_text_bottom_y (w) - cursor_row->y);
26045
26046 /* If row is completely invisible, don't attempt to delete a cursor which
26047 isn't there. This can happen if cursor is at top of a window, and
26048 we switch to a buffer with a header line in that window. */
26049 if (cursor_row->visible_height <= 0)
26050 goto mark_cursor_off;
26051
26052 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26053 if (cursor_row->cursor_in_fringe_p)
26054 {
26055 cursor_row->cursor_in_fringe_p = 0;
26056 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26057 goto mark_cursor_off;
26058 }
26059
26060 /* This can happen when the new row is shorter than the old one.
26061 In this case, either draw_glyphs or clear_end_of_line
26062 should have cleared the cursor. Note that we wouldn't be
26063 able to erase the cursor in this case because we don't have a
26064 cursor glyph at hand. */
26065 if ((cursor_row->reversed_p
26066 ? (w->phys_cursor.hpos < 0)
26067 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26068 goto mark_cursor_off;
26069
26070 /* When the window is hscrolled, cursor hpos can legitimately be out
26071 of bounds, but we draw the cursor at the corresponding window
26072 margin in that case. */
26073 if (!cursor_row->reversed_p && hpos < 0)
26074 hpos = 0;
26075 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26076 hpos = cursor_row->used[TEXT_AREA] - 1;
26077
26078 /* If the cursor is in the mouse face area, redisplay that when
26079 we clear the cursor. */
26080 if (! NILP (hlinfo->mouse_face_window)
26081 && coords_in_mouse_face_p (w, hpos, vpos)
26082 /* Don't redraw the cursor's spot in mouse face if it is at the
26083 end of a line (on a newline). The cursor appears there, but
26084 mouse highlighting does not. */
26085 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26086 mouse_face_here_p = 1;
26087
26088 /* Maybe clear the display under the cursor. */
26089 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26090 {
26091 int x, y, left_x;
26092 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26093 int width;
26094
26095 cursor_glyph = get_phys_cursor_glyph (w);
26096 if (cursor_glyph == NULL)
26097 goto mark_cursor_off;
26098
26099 width = cursor_glyph->pixel_width;
26100 left_x = window_box_left_offset (w, TEXT_AREA);
26101 x = w->phys_cursor.x;
26102 if (x < left_x)
26103 width -= left_x - x;
26104 width = min (width, window_box_width (w, TEXT_AREA) - x);
26105 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26106 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26107
26108 if (width > 0)
26109 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26110 }
26111
26112 /* Erase the cursor by redrawing the character underneath it. */
26113 if (mouse_face_here_p)
26114 hl = DRAW_MOUSE_FACE;
26115 else
26116 hl = DRAW_NORMAL_TEXT;
26117 draw_phys_cursor_glyph (w, cursor_row, hl);
26118
26119 mark_cursor_off:
26120 w->phys_cursor_on_p = 0;
26121 w->phys_cursor_type = NO_CURSOR;
26122 }
26123
26124
26125 /* EXPORT:
26126 Display or clear cursor of window W. If ON is zero, clear the
26127 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26128 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26129
26130 void
26131 display_and_set_cursor (struct window *w, int on,
26132 int hpos, int vpos, int x, int y)
26133 {
26134 struct frame *f = XFRAME (w->frame);
26135 int new_cursor_type;
26136 int new_cursor_width;
26137 int active_cursor;
26138 struct glyph_row *glyph_row;
26139 struct glyph *glyph;
26140
26141 /* This is pointless on invisible frames, and dangerous on garbaged
26142 windows and frames; in the latter case, the frame or window may
26143 be in the midst of changing its size, and x and y may be off the
26144 window. */
26145 if (! FRAME_VISIBLE_P (f)
26146 || FRAME_GARBAGED_P (f)
26147 || vpos >= w->current_matrix->nrows
26148 || hpos >= w->current_matrix->matrix_w)
26149 return;
26150
26151 /* If cursor is off and we want it off, return quickly. */
26152 if (!on && !w->phys_cursor_on_p)
26153 return;
26154
26155 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26156 /* If cursor row is not enabled, we don't really know where to
26157 display the cursor. */
26158 if (!glyph_row->enabled_p)
26159 {
26160 w->phys_cursor_on_p = 0;
26161 return;
26162 }
26163
26164 glyph = NULL;
26165 if (!glyph_row->exact_window_width_line_p
26166 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26167 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26168
26169 eassert (input_blocked_p ());
26170
26171 /* Set new_cursor_type to the cursor we want to be displayed. */
26172 new_cursor_type = get_window_cursor_type (w, glyph,
26173 &new_cursor_width, &active_cursor);
26174
26175 /* If cursor is currently being shown and we don't want it to be or
26176 it is in the wrong place, or the cursor type is not what we want,
26177 erase it. */
26178 if (w->phys_cursor_on_p
26179 && (!on
26180 || w->phys_cursor.x != x
26181 || w->phys_cursor.y != y
26182 || new_cursor_type != w->phys_cursor_type
26183 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26184 && new_cursor_width != w->phys_cursor_width)))
26185 erase_phys_cursor (w);
26186
26187 /* Don't check phys_cursor_on_p here because that flag is only set
26188 to zero in some cases where we know that the cursor has been
26189 completely erased, to avoid the extra work of erasing the cursor
26190 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26191 still not be visible, or it has only been partly erased. */
26192 if (on)
26193 {
26194 w->phys_cursor_ascent = glyph_row->ascent;
26195 w->phys_cursor_height = glyph_row->height;
26196
26197 /* Set phys_cursor_.* before x_draw_.* is called because some
26198 of them may need the information. */
26199 w->phys_cursor.x = x;
26200 w->phys_cursor.y = glyph_row->y;
26201 w->phys_cursor.hpos = hpos;
26202 w->phys_cursor.vpos = vpos;
26203 }
26204
26205 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26206 new_cursor_type, new_cursor_width,
26207 on, active_cursor);
26208 }
26209
26210
26211 /* Switch the display of W's cursor on or off, according to the value
26212 of ON. */
26213
26214 static void
26215 update_window_cursor (struct window *w, int on)
26216 {
26217 /* Don't update cursor in windows whose frame is in the process
26218 of being deleted. */
26219 if (w->current_matrix)
26220 {
26221 int hpos = w->phys_cursor.hpos;
26222 int vpos = w->phys_cursor.vpos;
26223 struct glyph_row *row;
26224
26225 if (vpos >= w->current_matrix->nrows
26226 || hpos >= w->current_matrix->matrix_w)
26227 return;
26228
26229 row = MATRIX_ROW (w->current_matrix, vpos);
26230
26231 /* When the window is hscrolled, cursor hpos can legitimately be
26232 out of bounds, but we draw the cursor at the corresponding
26233 window margin in that case. */
26234 if (!row->reversed_p && hpos < 0)
26235 hpos = 0;
26236 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26237 hpos = row->used[TEXT_AREA] - 1;
26238
26239 block_input ();
26240 display_and_set_cursor (w, on, hpos, vpos,
26241 w->phys_cursor.x, w->phys_cursor.y);
26242 unblock_input ();
26243 }
26244 }
26245
26246
26247 /* Call update_window_cursor with parameter ON_P on all leaf windows
26248 in the window tree rooted at W. */
26249
26250 static void
26251 update_cursor_in_window_tree (struct window *w, int on_p)
26252 {
26253 while (w)
26254 {
26255 if (!NILP (w->hchild))
26256 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26257 else if (!NILP (w->vchild))
26258 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26259 else
26260 update_window_cursor (w, on_p);
26261
26262 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26263 }
26264 }
26265
26266
26267 /* EXPORT:
26268 Display the cursor on window W, or clear it, according to ON_P.
26269 Don't change the cursor's position. */
26270
26271 void
26272 x_update_cursor (struct frame *f, int on_p)
26273 {
26274 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26275 }
26276
26277
26278 /* EXPORT:
26279 Clear the cursor of window W to background color, and mark the
26280 cursor as not shown. This is used when the text where the cursor
26281 is about to be rewritten. */
26282
26283 void
26284 x_clear_cursor (struct window *w)
26285 {
26286 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26287 update_window_cursor (w, 0);
26288 }
26289
26290 #endif /* HAVE_WINDOW_SYSTEM */
26291
26292 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26293 and MSDOS. */
26294 static void
26295 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26296 int start_hpos, int end_hpos,
26297 enum draw_glyphs_face draw)
26298 {
26299 #ifdef HAVE_WINDOW_SYSTEM
26300 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26301 {
26302 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26303 return;
26304 }
26305 #endif
26306 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26307 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26308 #endif
26309 }
26310
26311 /* Display the active region described by mouse_face_* according to DRAW. */
26312
26313 static void
26314 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26315 {
26316 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26317 struct frame *f = XFRAME (WINDOW_FRAME (w));
26318
26319 if (/* If window is in the process of being destroyed, don't bother
26320 to do anything. */
26321 w->current_matrix != NULL
26322 /* Don't update mouse highlight if hidden */
26323 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26324 /* Recognize when we are called to operate on rows that don't exist
26325 anymore. This can happen when a window is split. */
26326 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26327 {
26328 int phys_cursor_on_p = w->phys_cursor_on_p;
26329 struct glyph_row *row, *first, *last;
26330
26331 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26332 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26333
26334 for (row = first; row <= last && row->enabled_p; ++row)
26335 {
26336 int start_hpos, end_hpos, start_x;
26337
26338 /* For all but the first row, the highlight starts at column 0. */
26339 if (row == first)
26340 {
26341 /* R2L rows have BEG and END in reversed order, but the
26342 screen drawing geometry is always left to right. So
26343 we need to mirror the beginning and end of the
26344 highlighted area in R2L rows. */
26345 if (!row->reversed_p)
26346 {
26347 start_hpos = hlinfo->mouse_face_beg_col;
26348 start_x = hlinfo->mouse_face_beg_x;
26349 }
26350 else if (row == last)
26351 {
26352 start_hpos = hlinfo->mouse_face_end_col;
26353 start_x = hlinfo->mouse_face_end_x;
26354 }
26355 else
26356 {
26357 start_hpos = 0;
26358 start_x = 0;
26359 }
26360 }
26361 else if (row->reversed_p && row == last)
26362 {
26363 start_hpos = hlinfo->mouse_face_end_col;
26364 start_x = hlinfo->mouse_face_end_x;
26365 }
26366 else
26367 {
26368 start_hpos = 0;
26369 start_x = 0;
26370 }
26371
26372 if (row == last)
26373 {
26374 if (!row->reversed_p)
26375 end_hpos = hlinfo->mouse_face_end_col;
26376 else if (row == first)
26377 end_hpos = hlinfo->mouse_face_beg_col;
26378 else
26379 {
26380 end_hpos = row->used[TEXT_AREA];
26381 if (draw == DRAW_NORMAL_TEXT)
26382 row->fill_line_p = 1; /* Clear to end of line */
26383 }
26384 }
26385 else if (row->reversed_p && row == first)
26386 end_hpos = hlinfo->mouse_face_beg_col;
26387 else
26388 {
26389 end_hpos = row->used[TEXT_AREA];
26390 if (draw == DRAW_NORMAL_TEXT)
26391 row->fill_line_p = 1; /* Clear to end of line */
26392 }
26393
26394 if (end_hpos > start_hpos)
26395 {
26396 draw_row_with_mouse_face (w, start_x, row,
26397 start_hpos, end_hpos, draw);
26398
26399 row->mouse_face_p
26400 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26401 }
26402 }
26403
26404 #ifdef HAVE_WINDOW_SYSTEM
26405 /* When we've written over the cursor, arrange for it to
26406 be displayed again. */
26407 if (FRAME_WINDOW_P (f)
26408 && phys_cursor_on_p && !w->phys_cursor_on_p)
26409 {
26410 int hpos = w->phys_cursor.hpos;
26411
26412 /* When the window is hscrolled, cursor hpos can legitimately be
26413 out of bounds, but we draw the cursor at the corresponding
26414 window margin in that case. */
26415 if (!row->reversed_p && hpos < 0)
26416 hpos = 0;
26417 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26418 hpos = row->used[TEXT_AREA] - 1;
26419
26420 block_input ();
26421 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26422 w->phys_cursor.x, w->phys_cursor.y);
26423 unblock_input ();
26424 }
26425 #endif /* HAVE_WINDOW_SYSTEM */
26426 }
26427
26428 #ifdef HAVE_WINDOW_SYSTEM
26429 /* Change the mouse cursor. */
26430 if (FRAME_WINDOW_P (f))
26431 {
26432 if (draw == DRAW_NORMAL_TEXT
26433 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26434 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26435 else if (draw == DRAW_MOUSE_FACE)
26436 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26437 else
26438 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26439 }
26440 #endif /* HAVE_WINDOW_SYSTEM */
26441 }
26442
26443 /* EXPORT:
26444 Clear out the mouse-highlighted active region.
26445 Redraw it un-highlighted first. Value is non-zero if mouse
26446 face was actually drawn unhighlighted. */
26447
26448 int
26449 clear_mouse_face (Mouse_HLInfo *hlinfo)
26450 {
26451 int cleared = 0;
26452
26453 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26454 {
26455 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26456 cleared = 1;
26457 }
26458
26459 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26460 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26461 hlinfo->mouse_face_window = Qnil;
26462 hlinfo->mouse_face_overlay = Qnil;
26463 return cleared;
26464 }
26465
26466 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26467 within the mouse face on that window. */
26468 static int
26469 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26470 {
26471 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26472
26473 /* Quickly resolve the easy cases. */
26474 if (!(WINDOWP (hlinfo->mouse_face_window)
26475 && XWINDOW (hlinfo->mouse_face_window) == w))
26476 return 0;
26477 if (vpos < hlinfo->mouse_face_beg_row
26478 || vpos > hlinfo->mouse_face_end_row)
26479 return 0;
26480 if (vpos > hlinfo->mouse_face_beg_row
26481 && vpos < hlinfo->mouse_face_end_row)
26482 return 1;
26483
26484 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26485 {
26486 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26487 {
26488 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26489 return 1;
26490 }
26491 else if ((vpos == hlinfo->mouse_face_beg_row
26492 && hpos >= hlinfo->mouse_face_beg_col)
26493 || (vpos == hlinfo->mouse_face_end_row
26494 && hpos < hlinfo->mouse_face_end_col))
26495 return 1;
26496 }
26497 else
26498 {
26499 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26500 {
26501 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26502 return 1;
26503 }
26504 else if ((vpos == hlinfo->mouse_face_beg_row
26505 && hpos <= hlinfo->mouse_face_beg_col)
26506 || (vpos == hlinfo->mouse_face_end_row
26507 && hpos > hlinfo->mouse_face_end_col))
26508 return 1;
26509 }
26510 return 0;
26511 }
26512
26513
26514 /* EXPORT:
26515 Non-zero if physical cursor of window W is within mouse face. */
26516
26517 int
26518 cursor_in_mouse_face_p (struct window *w)
26519 {
26520 int hpos = w->phys_cursor.hpos;
26521 int vpos = w->phys_cursor.vpos;
26522 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26523
26524 /* When the window is hscrolled, cursor hpos can legitimately be out
26525 of bounds, but we draw the cursor at the corresponding window
26526 margin in that case. */
26527 if (!row->reversed_p && hpos < 0)
26528 hpos = 0;
26529 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26530 hpos = row->used[TEXT_AREA] - 1;
26531
26532 return coords_in_mouse_face_p (w, hpos, vpos);
26533 }
26534
26535
26536 \f
26537 /* Find the glyph rows START_ROW and END_ROW of window W that display
26538 characters between buffer positions START_CHARPOS and END_CHARPOS
26539 (excluding END_CHARPOS). DISP_STRING is a display string that
26540 covers these buffer positions. This is similar to
26541 row_containing_pos, but is more accurate when bidi reordering makes
26542 buffer positions change non-linearly with glyph rows. */
26543 static void
26544 rows_from_pos_range (struct window *w,
26545 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26546 Lisp_Object disp_string,
26547 struct glyph_row **start, struct glyph_row **end)
26548 {
26549 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26550 int last_y = window_text_bottom_y (w);
26551 struct glyph_row *row;
26552
26553 *start = NULL;
26554 *end = NULL;
26555
26556 while (!first->enabled_p
26557 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26558 first++;
26559
26560 /* Find the START row. */
26561 for (row = first;
26562 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26563 row++)
26564 {
26565 /* A row can potentially be the START row if the range of the
26566 characters it displays intersects the range
26567 [START_CHARPOS..END_CHARPOS). */
26568 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26569 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26570 /* See the commentary in row_containing_pos, for the
26571 explanation of the complicated way to check whether
26572 some position is beyond the end of the characters
26573 displayed by a row. */
26574 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26575 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26576 && !row->ends_at_zv_p
26577 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26578 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26579 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26580 && !row->ends_at_zv_p
26581 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26582 {
26583 /* Found a candidate row. Now make sure at least one of the
26584 glyphs it displays has a charpos from the range
26585 [START_CHARPOS..END_CHARPOS).
26586
26587 This is not obvious because bidi reordering could make
26588 buffer positions of a row be 1,2,3,102,101,100, and if we
26589 want to highlight characters in [50..60), we don't want
26590 this row, even though [50..60) does intersect [1..103),
26591 the range of character positions given by the row's start
26592 and end positions. */
26593 struct glyph *g = row->glyphs[TEXT_AREA];
26594 struct glyph *e = g + row->used[TEXT_AREA];
26595
26596 while (g < e)
26597 {
26598 if (((BUFFERP (g->object) || INTEGERP (g->object))
26599 && start_charpos <= g->charpos && g->charpos < end_charpos)
26600 /* A glyph that comes from DISP_STRING is by
26601 definition to be highlighted. */
26602 || EQ (g->object, disp_string))
26603 *start = row;
26604 g++;
26605 }
26606 if (*start)
26607 break;
26608 }
26609 }
26610
26611 /* Find the END row. */
26612 if (!*start
26613 /* If the last row is partially visible, start looking for END
26614 from that row, instead of starting from FIRST. */
26615 && !(row->enabled_p
26616 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26617 row = first;
26618 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26619 {
26620 struct glyph_row *next = row + 1;
26621 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26622
26623 if (!next->enabled_p
26624 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26625 /* The first row >= START whose range of displayed characters
26626 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26627 is the row END + 1. */
26628 || (start_charpos < next_start
26629 && end_charpos < next_start)
26630 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26631 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26632 && !next->ends_at_zv_p
26633 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26634 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26635 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26636 && !next->ends_at_zv_p
26637 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26638 {
26639 *end = row;
26640 break;
26641 }
26642 else
26643 {
26644 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26645 but none of the characters it displays are in the range, it is
26646 also END + 1. */
26647 struct glyph *g = next->glyphs[TEXT_AREA];
26648 struct glyph *s = g;
26649 struct glyph *e = g + next->used[TEXT_AREA];
26650
26651 while (g < e)
26652 {
26653 if (((BUFFERP (g->object) || INTEGERP (g->object))
26654 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26655 /* If the buffer position of the first glyph in
26656 the row is equal to END_CHARPOS, it means
26657 the last character to be highlighted is the
26658 newline of ROW, and we must consider NEXT as
26659 END, not END+1. */
26660 || (((!next->reversed_p && g == s)
26661 || (next->reversed_p && g == e - 1))
26662 && (g->charpos == end_charpos
26663 /* Special case for when NEXT is an
26664 empty line at ZV. */
26665 || (g->charpos == -1
26666 && !row->ends_at_zv_p
26667 && next_start == end_charpos)))))
26668 /* A glyph that comes from DISP_STRING is by
26669 definition to be highlighted. */
26670 || EQ (g->object, disp_string))
26671 break;
26672 g++;
26673 }
26674 if (g == e)
26675 {
26676 *end = row;
26677 break;
26678 }
26679 /* The first row that ends at ZV must be the last to be
26680 highlighted. */
26681 else if (next->ends_at_zv_p)
26682 {
26683 *end = next;
26684 break;
26685 }
26686 }
26687 }
26688 }
26689
26690 /* This function sets the mouse_face_* elements of HLINFO, assuming
26691 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26692 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26693 for the overlay or run of text properties specifying the mouse
26694 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26695 before-string and after-string that must also be highlighted.
26696 DISP_STRING, if non-nil, is a display string that may cover some
26697 or all of the highlighted text. */
26698
26699 static void
26700 mouse_face_from_buffer_pos (Lisp_Object window,
26701 Mouse_HLInfo *hlinfo,
26702 ptrdiff_t mouse_charpos,
26703 ptrdiff_t start_charpos,
26704 ptrdiff_t end_charpos,
26705 Lisp_Object before_string,
26706 Lisp_Object after_string,
26707 Lisp_Object disp_string)
26708 {
26709 struct window *w = XWINDOW (window);
26710 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26711 struct glyph_row *r1, *r2;
26712 struct glyph *glyph, *end;
26713 ptrdiff_t ignore, pos;
26714 int x;
26715
26716 eassert (NILP (disp_string) || STRINGP (disp_string));
26717 eassert (NILP (before_string) || STRINGP (before_string));
26718 eassert (NILP (after_string) || STRINGP (after_string));
26719
26720 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26721 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26722 if (r1 == NULL)
26723 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26724 /* If the before-string or display-string contains newlines,
26725 rows_from_pos_range skips to its last row. Move back. */
26726 if (!NILP (before_string) || !NILP (disp_string))
26727 {
26728 struct glyph_row *prev;
26729 while ((prev = r1 - 1, prev >= first)
26730 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26731 && prev->used[TEXT_AREA] > 0)
26732 {
26733 struct glyph *beg = prev->glyphs[TEXT_AREA];
26734 glyph = beg + prev->used[TEXT_AREA];
26735 while (--glyph >= beg && INTEGERP (glyph->object));
26736 if (glyph < beg
26737 || !(EQ (glyph->object, before_string)
26738 || EQ (glyph->object, disp_string)))
26739 break;
26740 r1 = prev;
26741 }
26742 }
26743 if (r2 == NULL)
26744 {
26745 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26746 hlinfo->mouse_face_past_end = 1;
26747 }
26748 else if (!NILP (after_string))
26749 {
26750 /* If the after-string has newlines, advance to its last row. */
26751 struct glyph_row *next;
26752 struct glyph_row *last
26753 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26754
26755 for (next = r2 + 1;
26756 next <= last
26757 && next->used[TEXT_AREA] > 0
26758 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26759 ++next)
26760 r2 = next;
26761 }
26762 /* The rest of the display engine assumes that mouse_face_beg_row is
26763 either above mouse_face_end_row or identical to it. But with
26764 bidi-reordered continued lines, the row for START_CHARPOS could
26765 be below the row for END_CHARPOS. If so, swap the rows and store
26766 them in correct order. */
26767 if (r1->y > r2->y)
26768 {
26769 struct glyph_row *tem = r2;
26770
26771 r2 = r1;
26772 r1 = tem;
26773 }
26774
26775 hlinfo->mouse_face_beg_y = r1->y;
26776 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26777 hlinfo->mouse_face_end_y = r2->y;
26778 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26779
26780 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26781 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26782 could be anywhere in the row and in any order. The strategy
26783 below is to find the leftmost and the rightmost glyph that
26784 belongs to either of these 3 strings, or whose position is
26785 between START_CHARPOS and END_CHARPOS, and highlight all the
26786 glyphs between those two. This may cover more than just the text
26787 between START_CHARPOS and END_CHARPOS if the range of characters
26788 strides the bidi level boundary, e.g. if the beginning is in R2L
26789 text while the end is in L2R text or vice versa. */
26790 if (!r1->reversed_p)
26791 {
26792 /* This row is in a left to right paragraph. Scan it left to
26793 right. */
26794 glyph = r1->glyphs[TEXT_AREA];
26795 end = glyph + r1->used[TEXT_AREA];
26796 x = r1->x;
26797
26798 /* Skip truncation glyphs at the start of the glyph row. */
26799 if (r1->displays_text_p)
26800 for (; glyph < end
26801 && INTEGERP (glyph->object)
26802 && glyph->charpos < 0;
26803 ++glyph)
26804 x += glyph->pixel_width;
26805
26806 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26807 or DISP_STRING, and the first glyph from buffer whose
26808 position is between START_CHARPOS and END_CHARPOS. */
26809 for (; glyph < end
26810 && !INTEGERP (glyph->object)
26811 && !EQ (glyph->object, disp_string)
26812 && !(BUFFERP (glyph->object)
26813 && (glyph->charpos >= start_charpos
26814 && glyph->charpos < end_charpos));
26815 ++glyph)
26816 {
26817 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26818 are present at buffer positions between START_CHARPOS and
26819 END_CHARPOS, or if they come from an overlay. */
26820 if (EQ (glyph->object, before_string))
26821 {
26822 pos = string_buffer_position (before_string,
26823 start_charpos);
26824 /* If pos == 0, it means before_string came from an
26825 overlay, not from a buffer position. */
26826 if (!pos || (pos >= start_charpos && pos < end_charpos))
26827 break;
26828 }
26829 else if (EQ (glyph->object, after_string))
26830 {
26831 pos = string_buffer_position (after_string, end_charpos);
26832 if (!pos || (pos >= start_charpos && pos < end_charpos))
26833 break;
26834 }
26835 x += glyph->pixel_width;
26836 }
26837 hlinfo->mouse_face_beg_x = x;
26838 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26839 }
26840 else
26841 {
26842 /* This row is in a right to left paragraph. Scan it right to
26843 left. */
26844 struct glyph *g;
26845
26846 end = r1->glyphs[TEXT_AREA] - 1;
26847 glyph = end + r1->used[TEXT_AREA];
26848
26849 /* Skip truncation glyphs at the start of the glyph row. */
26850 if (r1->displays_text_p)
26851 for (; glyph > end
26852 && INTEGERP (glyph->object)
26853 && glyph->charpos < 0;
26854 --glyph)
26855 ;
26856
26857 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26858 or DISP_STRING, and the first glyph from buffer whose
26859 position is between START_CHARPOS and END_CHARPOS. */
26860 for (; glyph > end
26861 && !INTEGERP (glyph->object)
26862 && !EQ (glyph->object, disp_string)
26863 && !(BUFFERP (glyph->object)
26864 && (glyph->charpos >= start_charpos
26865 && glyph->charpos < end_charpos));
26866 --glyph)
26867 {
26868 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26869 are present at buffer positions between START_CHARPOS and
26870 END_CHARPOS, or if they come from an overlay. */
26871 if (EQ (glyph->object, before_string))
26872 {
26873 pos = string_buffer_position (before_string, start_charpos);
26874 /* If pos == 0, it means before_string came from an
26875 overlay, not from a buffer position. */
26876 if (!pos || (pos >= start_charpos && pos < end_charpos))
26877 break;
26878 }
26879 else if (EQ (glyph->object, after_string))
26880 {
26881 pos = string_buffer_position (after_string, end_charpos);
26882 if (!pos || (pos >= start_charpos && pos < end_charpos))
26883 break;
26884 }
26885 }
26886
26887 glyph++; /* first glyph to the right of the highlighted area */
26888 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26889 x += g->pixel_width;
26890 hlinfo->mouse_face_beg_x = x;
26891 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26892 }
26893
26894 /* If the highlight ends in a different row, compute GLYPH and END
26895 for the end row. Otherwise, reuse the values computed above for
26896 the row where the highlight begins. */
26897 if (r2 != r1)
26898 {
26899 if (!r2->reversed_p)
26900 {
26901 glyph = r2->glyphs[TEXT_AREA];
26902 end = glyph + r2->used[TEXT_AREA];
26903 x = r2->x;
26904 }
26905 else
26906 {
26907 end = r2->glyphs[TEXT_AREA] - 1;
26908 glyph = end + r2->used[TEXT_AREA];
26909 }
26910 }
26911
26912 if (!r2->reversed_p)
26913 {
26914 /* Skip truncation and continuation glyphs near the end of the
26915 row, and also blanks and stretch glyphs inserted by
26916 extend_face_to_end_of_line. */
26917 while (end > glyph
26918 && INTEGERP ((end - 1)->object))
26919 --end;
26920 /* Scan the rest of the glyph row from the end, looking for the
26921 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26922 DISP_STRING, or whose position is between START_CHARPOS
26923 and END_CHARPOS */
26924 for (--end;
26925 end > glyph
26926 && !INTEGERP (end->object)
26927 && !EQ (end->object, disp_string)
26928 && !(BUFFERP (end->object)
26929 && (end->charpos >= start_charpos
26930 && end->charpos < end_charpos));
26931 --end)
26932 {
26933 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26934 are present at buffer positions between START_CHARPOS and
26935 END_CHARPOS, or if they come from an overlay. */
26936 if (EQ (end->object, before_string))
26937 {
26938 pos = string_buffer_position (before_string, start_charpos);
26939 if (!pos || (pos >= start_charpos && pos < end_charpos))
26940 break;
26941 }
26942 else if (EQ (end->object, after_string))
26943 {
26944 pos = string_buffer_position (after_string, end_charpos);
26945 if (!pos || (pos >= start_charpos && pos < end_charpos))
26946 break;
26947 }
26948 }
26949 /* Find the X coordinate of the last glyph to be highlighted. */
26950 for (; glyph <= end; ++glyph)
26951 x += glyph->pixel_width;
26952
26953 hlinfo->mouse_face_end_x = x;
26954 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26955 }
26956 else
26957 {
26958 /* Skip truncation and continuation glyphs near the end of the
26959 row, and also blanks and stretch glyphs inserted by
26960 extend_face_to_end_of_line. */
26961 x = r2->x;
26962 end++;
26963 while (end < glyph
26964 && INTEGERP (end->object))
26965 {
26966 x += end->pixel_width;
26967 ++end;
26968 }
26969 /* Scan the rest of the glyph row from the end, looking for the
26970 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26971 DISP_STRING, or whose position is between START_CHARPOS
26972 and END_CHARPOS */
26973 for ( ;
26974 end < glyph
26975 && !INTEGERP (end->object)
26976 && !EQ (end->object, disp_string)
26977 && !(BUFFERP (end->object)
26978 && (end->charpos >= start_charpos
26979 && end->charpos < end_charpos));
26980 ++end)
26981 {
26982 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26983 are present at buffer positions between START_CHARPOS and
26984 END_CHARPOS, or if they come from an overlay. */
26985 if (EQ (end->object, before_string))
26986 {
26987 pos = string_buffer_position (before_string, start_charpos);
26988 if (!pos || (pos >= start_charpos && pos < end_charpos))
26989 break;
26990 }
26991 else if (EQ (end->object, after_string))
26992 {
26993 pos = string_buffer_position (after_string, end_charpos);
26994 if (!pos || (pos >= start_charpos && pos < end_charpos))
26995 break;
26996 }
26997 x += end->pixel_width;
26998 }
26999 /* If we exited the above loop because we arrived at the last
27000 glyph of the row, and its buffer position is still not in
27001 range, it means the last character in range is the preceding
27002 newline. Bump the end column and x values to get past the
27003 last glyph. */
27004 if (end == glyph
27005 && BUFFERP (end->object)
27006 && (end->charpos < start_charpos
27007 || end->charpos >= end_charpos))
27008 {
27009 x += end->pixel_width;
27010 ++end;
27011 }
27012 hlinfo->mouse_face_end_x = x;
27013 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27014 }
27015
27016 hlinfo->mouse_face_window = window;
27017 hlinfo->mouse_face_face_id
27018 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27019 mouse_charpos + 1,
27020 !hlinfo->mouse_face_hidden, -1);
27021 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27022 }
27023
27024 /* The following function is not used anymore (replaced with
27025 mouse_face_from_string_pos), but I leave it here for the time
27026 being, in case someone would. */
27027
27028 #if 0 /* not used */
27029
27030 /* Find the position of the glyph for position POS in OBJECT in
27031 window W's current matrix, and return in *X, *Y the pixel
27032 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27033
27034 RIGHT_P non-zero means return the position of the right edge of the
27035 glyph, RIGHT_P zero means return the left edge position.
27036
27037 If no glyph for POS exists in the matrix, return the position of
27038 the glyph with the next smaller position that is in the matrix, if
27039 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27040 exists in the matrix, return the position of the glyph with the
27041 next larger position in OBJECT.
27042
27043 Value is non-zero if a glyph was found. */
27044
27045 static int
27046 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27047 int *hpos, int *vpos, int *x, int *y, int right_p)
27048 {
27049 int yb = window_text_bottom_y (w);
27050 struct glyph_row *r;
27051 struct glyph *best_glyph = NULL;
27052 struct glyph_row *best_row = NULL;
27053 int best_x = 0;
27054
27055 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27056 r->enabled_p && r->y < yb;
27057 ++r)
27058 {
27059 struct glyph *g = r->glyphs[TEXT_AREA];
27060 struct glyph *e = g + r->used[TEXT_AREA];
27061 int gx;
27062
27063 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27064 if (EQ (g->object, object))
27065 {
27066 if (g->charpos == pos)
27067 {
27068 best_glyph = g;
27069 best_x = gx;
27070 best_row = r;
27071 goto found;
27072 }
27073 else if (best_glyph == NULL
27074 || ((eabs (g->charpos - pos)
27075 < eabs (best_glyph->charpos - pos))
27076 && (right_p
27077 ? g->charpos < pos
27078 : g->charpos > pos)))
27079 {
27080 best_glyph = g;
27081 best_x = gx;
27082 best_row = r;
27083 }
27084 }
27085 }
27086
27087 found:
27088
27089 if (best_glyph)
27090 {
27091 *x = best_x;
27092 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27093
27094 if (right_p)
27095 {
27096 *x += best_glyph->pixel_width;
27097 ++*hpos;
27098 }
27099
27100 *y = best_row->y;
27101 *vpos = best_row - w->current_matrix->rows;
27102 }
27103
27104 return best_glyph != NULL;
27105 }
27106 #endif /* not used */
27107
27108 /* Find the positions of the first and the last glyphs in window W's
27109 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27110 (assumed to be a string), and return in HLINFO's mouse_face_*
27111 members the pixel and column/row coordinates of those glyphs. */
27112
27113 static void
27114 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27115 Lisp_Object object,
27116 ptrdiff_t startpos, ptrdiff_t endpos)
27117 {
27118 int yb = window_text_bottom_y (w);
27119 struct glyph_row *r;
27120 struct glyph *g, *e;
27121 int gx;
27122 int found = 0;
27123
27124 /* Find the glyph row with at least one position in the range
27125 [STARTPOS..ENDPOS], and the first glyph in that row whose
27126 position belongs to that range. */
27127 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27128 r->enabled_p && r->y < yb;
27129 ++r)
27130 {
27131 if (!r->reversed_p)
27132 {
27133 g = r->glyphs[TEXT_AREA];
27134 e = g + r->used[TEXT_AREA];
27135 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27136 if (EQ (g->object, object)
27137 && startpos <= g->charpos && g->charpos <= endpos)
27138 {
27139 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27140 hlinfo->mouse_face_beg_y = r->y;
27141 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27142 hlinfo->mouse_face_beg_x = gx;
27143 found = 1;
27144 break;
27145 }
27146 }
27147 else
27148 {
27149 struct glyph *g1;
27150
27151 e = r->glyphs[TEXT_AREA];
27152 g = e + r->used[TEXT_AREA];
27153 for ( ; g > e; --g)
27154 if (EQ ((g-1)->object, object)
27155 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27156 {
27157 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27158 hlinfo->mouse_face_beg_y = r->y;
27159 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27160 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27161 gx += g1->pixel_width;
27162 hlinfo->mouse_face_beg_x = gx;
27163 found = 1;
27164 break;
27165 }
27166 }
27167 if (found)
27168 break;
27169 }
27170
27171 if (!found)
27172 return;
27173
27174 /* Starting with the next row, look for the first row which does NOT
27175 include any glyphs whose positions are in the range. */
27176 for (++r; r->enabled_p && r->y < yb; ++r)
27177 {
27178 g = r->glyphs[TEXT_AREA];
27179 e = g + r->used[TEXT_AREA];
27180 found = 0;
27181 for ( ; g < e; ++g)
27182 if (EQ (g->object, object)
27183 && startpos <= g->charpos && g->charpos <= endpos)
27184 {
27185 found = 1;
27186 break;
27187 }
27188 if (!found)
27189 break;
27190 }
27191
27192 /* The highlighted region ends on the previous row. */
27193 r--;
27194
27195 /* Set the end row and its vertical pixel coordinate. */
27196 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27197 hlinfo->mouse_face_end_y = r->y;
27198
27199 /* Compute and set the end column and the end column's horizontal
27200 pixel coordinate. */
27201 if (!r->reversed_p)
27202 {
27203 g = r->glyphs[TEXT_AREA];
27204 e = g + r->used[TEXT_AREA];
27205 for ( ; e > g; --e)
27206 if (EQ ((e-1)->object, object)
27207 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27208 break;
27209 hlinfo->mouse_face_end_col = e - g;
27210
27211 for (gx = r->x; g < e; ++g)
27212 gx += g->pixel_width;
27213 hlinfo->mouse_face_end_x = gx;
27214 }
27215 else
27216 {
27217 e = r->glyphs[TEXT_AREA];
27218 g = e + r->used[TEXT_AREA];
27219 for (gx = r->x ; e < g; ++e)
27220 {
27221 if (EQ (e->object, object)
27222 && startpos <= e->charpos && e->charpos <= endpos)
27223 break;
27224 gx += e->pixel_width;
27225 }
27226 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27227 hlinfo->mouse_face_end_x = gx;
27228 }
27229 }
27230
27231 #ifdef HAVE_WINDOW_SYSTEM
27232
27233 /* See if position X, Y is within a hot-spot of an image. */
27234
27235 static int
27236 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27237 {
27238 if (!CONSP (hot_spot))
27239 return 0;
27240
27241 if (EQ (XCAR (hot_spot), Qrect))
27242 {
27243 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27244 Lisp_Object rect = XCDR (hot_spot);
27245 Lisp_Object tem;
27246 if (!CONSP (rect))
27247 return 0;
27248 if (!CONSP (XCAR (rect)))
27249 return 0;
27250 if (!CONSP (XCDR (rect)))
27251 return 0;
27252 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27253 return 0;
27254 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27255 return 0;
27256 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27257 return 0;
27258 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27259 return 0;
27260 return 1;
27261 }
27262 else if (EQ (XCAR (hot_spot), Qcircle))
27263 {
27264 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27265 Lisp_Object circ = XCDR (hot_spot);
27266 Lisp_Object lr, lx0, ly0;
27267 if (CONSP (circ)
27268 && CONSP (XCAR (circ))
27269 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27270 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27271 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27272 {
27273 double r = XFLOATINT (lr);
27274 double dx = XINT (lx0) - x;
27275 double dy = XINT (ly0) - y;
27276 return (dx * dx + dy * dy <= r * r);
27277 }
27278 }
27279 else if (EQ (XCAR (hot_spot), Qpoly))
27280 {
27281 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27282 if (VECTORP (XCDR (hot_spot)))
27283 {
27284 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27285 Lisp_Object *poly = v->contents;
27286 ptrdiff_t n = v->header.size;
27287 ptrdiff_t i;
27288 int inside = 0;
27289 Lisp_Object lx, ly;
27290 int x0, y0;
27291
27292 /* Need an even number of coordinates, and at least 3 edges. */
27293 if (n < 6 || n & 1)
27294 return 0;
27295
27296 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27297 If count is odd, we are inside polygon. Pixels on edges
27298 may or may not be included depending on actual geometry of the
27299 polygon. */
27300 if ((lx = poly[n-2], !INTEGERP (lx))
27301 || (ly = poly[n-1], !INTEGERP (lx)))
27302 return 0;
27303 x0 = XINT (lx), y0 = XINT (ly);
27304 for (i = 0; i < n; i += 2)
27305 {
27306 int x1 = x0, y1 = y0;
27307 if ((lx = poly[i], !INTEGERP (lx))
27308 || (ly = poly[i+1], !INTEGERP (ly)))
27309 return 0;
27310 x0 = XINT (lx), y0 = XINT (ly);
27311
27312 /* Does this segment cross the X line? */
27313 if (x0 >= x)
27314 {
27315 if (x1 >= x)
27316 continue;
27317 }
27318 else if (x1 < x)
27319 continue;
27320 if (y > y0 && y > y1)
27321 continue;
27322 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27323 inside = !inside;
27324 }
27325 return inside;
27326 }
27327 }
27328 return 0;
27329 }
27330
27331 Lisp_Object
27332 find_hot_spot (Lisp_Object map, int x, int y)
27333 {
27334 while (CONSP (map))
27335 {
27336 if (CONSP (XCAR (map))
27337 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27338 return XCAR (map);
27339 map = XCDR (map);
27340 }
27341
27342 return Qnil;
27343 }
27344
27345 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27346 3, 3, 0,
27347 doc: /* Lookup in image map MAP coordinates X and Y.
27348 An image map is an alist where each element has the format (AREA ID PLIST).
27349 An AREA is specified as either a rectangle, a circle, or a polygon:
27350 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27351 pixel coordinates of the upper left and bottom right corners.
27352 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27353 and the radius of the circle; r may be a float or integer.
27354 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27355 vector describes one corner in the polygon.
27356 Returns the alist element for the first matching AREA in MAP. */)
27357 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27358 {
27359 if (NILP (map))
27360 return Qnil;
27361
27362 CHECK_NUMBER (x);
27363 CHECK_NUMBER (y);
27364
27365 return find_hot_spot (map,
27366 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27367 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27368 }
27369
27370
27371 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27372 static void
27373 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27374 {
27375 /* Do not change cursor shape while dragging mouse. */
27376 if (!NILP (do_mouse_tracking))
27377 return;
27378
27379 if (!NILP (pointer))
27380 {
27381 if (EQ (pointer, Qarrow))
27382 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27383 else if (EQ (pointer, Qhand))
27384 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27385 else if (EQ (pointer, Qtext))
27386 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27387 else if (EQ (pointer, intern ("hdrag")))
27388 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27389 #ifdef HAVE_X_WINDOWS
27390 else if (EQ (pointer, intern ("vdrag")))
27391 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27392 #endif
27393 else if (EQ (pointer, intern ("hourglass")))
27394 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27395 else if (EQ (pointer, Qmodeline))
27396 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27397 else
27398 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27399 }
27400
27401 if (cursor != No_Cursor)
27402 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27403 }
27404
27405 #endif /* HAVE_WINDOW_SYSTEM */
27406
27407 /* Take proper action when mouse has moved to the mode or header line
27408 or marginal area AREA of window W, x-position X and y-position Y.
27409 X is relative to the start of the text display area of W, so the
27410 width of bitmap areas and scroll bars must be subtracted to get a
27411 position relative to the start of the mode line. */
27412
27413 static void
27414 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27415 enum window_part area)
27416 {
27417 struct window *w = XWINDOW (window);
27418 struct frame *f = XFRAME (w->frame);
27419 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27420 #ifdef HAVE_WINDOW_SYSTEM
27421 Display_Info *dpyinfo;
27422 #endif
27423 Cursor cursor = No_Cursor;
27424 Lisp_Object pointer = Qnil;
27425 int dx, dy, width, height;
27426 ptrdiff_t charpos;
27427 Lisp_Object string, object = Qnil;
27428 Lisp_Object pos IF_LINT (= Qnil), help;
27429
27430 Lisp_Object mouse_face;
27431 int original_x_pixel = x;
27432 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27433 struct glyph_row *row IF_LINT (= 0);
27434
27435 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27436 {
27437 int x0;
27438 struct glyph *end;
27439
27440 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27441 returns them in row/column units! */
27442 string = mode_line_string (w, area, &x, &y, &charpos,
27443 &object, &dx, &dy, &width, &height);
27444
27445 row = (area == ON_MODE_LINE
27446 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27447 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27448
27449 /* Find the glyph under the mouse pointer. */
27450 if (row->mode_line_p && row->enabled_p)
27451 {
27452 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27453 end = glyph + row->used[TEXT_AREA];
27454
27455 for (x0 = original_x_pixel;
27456 glyph < end && x0 >= glyph->pixel_width;
27457 ++glyph)
27458 x0 -= glyph->pixel_width;
27459
27460 if (glyph >= end)
27461 glyph = NULL;
27462 }
27463 }
27464 else
27465 {
27466 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27467 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27468 returns them in row/column units! */
27469 string = marginal_area_string (w, area, &x, &y, &charpos,
27470 &object, &dx, &dy, &width, &height);
27471 }
27472
27473 help = Qnil;
27474
27475 #ifdef HAVE_WINDOW_SYSTEM
27476 if (IMAGEP (object))
27477 {
27478 Lisp_Object image_map, hotspot;
27479 if ((image_map = Fplist_get (XCDR (object), QCmap),
27480 !NILP (image_map))
27481 && (hotspot = find_hot_spot (image_map, dx, dy),
27482 CONSP (hotspot))
27483 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27484 {
27485 Lisp_Object plist;
27486
27487 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27488 If so, we could look for mouse-enter, mouse-leave
27489 properties in PLIST (and do something...). */
27490 hotspot = XCDR (hotspot);
27491 if (CONSP (hotspot)
27492 && (plist = XCAR (hotspot), CONSP (plist)))
27493 {
27494 pointer = Fplist_get (plist, Qpointer);
27495 if (NILP (pointer))
27496 pointer = Qhand;
27497 help = Fplist_get (plist, Qhelp_echo);
27498 if (!NILP (help))
27499 {
27500 help_echo_string = help;
27501 XSETWINDOW (help_echo_window, w);
27502 help_echo_object = w->buffer;
27503 help_echo_pos = charpos;
27504 }
27505 }
27506 }
27507 if (NILP (pointer))
27508 pointer = Fplist_get (XCDR (object), QCpointer);
27509 }
27510 #endif /* HAVE_WINDOW_SYSTEM */
27511
27512 if (STRINGP (string))
27513 pos = make_number (charpos);
27514
27515 /* Set the help text and mouse pointer. If the mouse is on a part
27516 of the mode line without any text (e.g. past the right edge of
27517 the mode line text), use the default help text and pointer. */
27518 if (STRINGP (string) || area == ON_MODE_LINE)
27519 {
27520 /* Arrange to display the help by setting the global variables
27521 help_echo_string, help_echo_object, and help_echo_pos. */
27522 if (NILP (help))
27523 {
27524 if (STRINGP (string))
27525 help = Fget_text_property (pos, Qhelp_echo, string);
27526
27527 if (!NILP (help))
27528 {
27529 help_echo_string = help;
27530 XSETWINDOW (help_echo_window, w);
27531 help_echo_object = string;
27532 help_echo_pos = charpos;
27533 }
27534 else if (area == ON_MODE_LINE)
27535 {
27536 Lisp_Object default_help
27537 = buffer_local_value_1 (Qmode_line_default_help_echo,
27538 w->buffer);
27539
27540 if (STRINGP (default_help))
27541 {
27542 help_echo_string = default_help;
27543 XSETWINDOW (help_echo_window, w);
27544 help_echo_object = Qnil;
27545 help_echo_pos = -1;
27546 }
27547 }
27548 }
27549
27550 #ifdef HAVE_WINDOW_SYSTEM
27551 /* Change the mouse pointer according to what is under it. */
27552 if (FRAME_WINDOW_P (f))
27553 {
27554 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27555 if (STRINGP (string))
27556 {
27557 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27558
27559 if (NILP (pointer))
27560 pointer = Fget_text_property (pos, Qpointer, string);
27561
27562 /* Change the mouse pointer according to what is under X/Y. */
27563 if (NILP (pointer)
27564 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27565 {
27566 Lisp_Object map;
27567 map = Fget_text_property (pos, Qlocal_map, string);
27568 if (!KEYMAPP (map))
27569 map = Fget_text_property (pos, Qkeymap, string);
27570 if (!KEYMAPP (map))
27571 cursor = dpyinfo->vertical_scroll_bar_cursor;
27572 }
27573 }
27574 else
27575 /* Default mode-line pointer. */
27576 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27577 }
27578 #endif
27579 }
27580
27581 /* Change the mouse face according to what is under X/Y. */
27582 if (STRINGP (string))
27583 {
27584 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27585 if (!NILP (mouse_face)
27586 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27587 && glyph)
27588 {
27589 Lisp_Object b, e;
27590
27591 struct glyph * tmp_glyph;
27592
27593 int gpos;
27594 int gseq_length;
27595 int total_pixel_width;
27596 ptrdiff_t begpos, endpos, ignore;
27597
27598 int vpos, hpos;
27599
27600 b = Fprevious_single_property_change (make_number (charpos + 1),
27601 Qmouse_face, string, Qnil);
27602 if (NILP (b))
27603 begpos = 0;
27604 else
27605 begpos = XINT (b);
27606
27607 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27608 if (NILP (e))
27609 endpos = SCHARS (string);
27610 else
27611 endpos = XINT (e);
27612
27613 /* Calculate the glyph position GPOS of GLYPH in the
27614 displayed string, relative to the beginning of the
27615 highlighted part of the string.
27616
27617 Note: GPOS is different from CHARPOS. CHARPOS is the
27618 position of GLYPH in the internal string object. A mode
27619 line string format has structures which are converted to
27620 a flattened string by the Emacs Lisp interpreter. The
27621 internal string is an element of those structures. The
27622 displayed string is the flattened string. */
27623 tmp_glyph = row_start_glyph;
27624 while (tmp_glyph < glyph
27625 && (!(EQ (tmp_glyph->object, glyph->object)
27626 && begpos <= tmp_glyph->charpos
27627 && tmp_glyph->charpos < endpos)))
27628 tmp_glyph++;
27629 gpos = glyph - tmp_glyph;
27630
27631 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27632 the highlighted part of the displayed string to which
27633 GLYPH belongs. Note: GSEQ_LENGTH is different from
27634 SCHARS (STRING), because the latter returns the length of
27635 the internal string. */
27636 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27637 tmp_glyph > glyph
27638 && (!(EQ (tmp_glyph->object, glyph->object)
27639 && begpos <= tmp_glyph->charpos
27640 && tmp_glyph->charpos < endpos));
27641 tmp_glyph--)
27642 ;
27643 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27644
27645 /* Calculate the total pixel width of all the glyphs between
27646 the beginning of the highlighted area and GLYPH. */
27647 total_pixel_width = 0;
27648 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27649 total_pixel_width += tmp_glyph->pixel_width;
27650
27651 /* Pre calculation of re-rendering position. Note: X is in
27652 column units here, after the call to mode_line_string or
27653 marginal_area_string. */
27654 hpos = x - gpos;
27655 vpos = (area == ON_MODE_LINE
27656 ? (w->current_matrix)->nrows - 1
27657 : 0);
27658
27659 /* If GLYPH's position is included in the region that is
27660 already drawn in mouse face, we have nothing to do. */
27661 if ( EQ (window, hlinfo->mouse_face_window)
27662 && (!row->reversed_p
27663 ? (hlinfo->mouse_face_beg_col <= hpos
27664 && hpos < hlinfo->mouse_face_end_col)
27665 /* In R2L rows we swap BEG and END, see below. */
27666 : (hlinfo->mouse_face_end_col <= hpos
27667 && hpos < hlinfo->mouse_face_beg_col))
27668 && hlinfo->mouse_face_beg_row == vpos )
27669 return;
27670
27671 if (clear_mouse_face (hlinfo))
27672 cursor = No_Cursor;
27673
27674 if (!row->reversed_p)
27675 {
27676 hlinfo->mouse_face_beg_col = hpos;
27677 hlinfo->mouse_face_beg_x = original_x_pixel
27678 - (total_pixel_width + dx);
27679 hlinfo->mouse_face_end_col = hpos + gseq_length;
27680 hlinfo->mouse_face_end_x = 0;
27681 }
27682 else
27683 {
27684 /* In R2L rows, show_mouse_face expects BEG and END
27685 coordinates to be swapped. */
27686 hlinfo->mouse_face_end_col = hpos;
27687 hlinfo->mouse_face_end_x = original_x_pixel
27688 - (total_pixel_width + dx);
27689 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27690 hlinfo->mouse_face_beg_x = 0;
27691 }
27692
27693 hlinfo->mouse_face_beg_row = vpos;
27694 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27695 hlinfo->mouse_face_beg_y = 0;
27696 hlinfo->mouse_face_end_y = 0;
27697 hlinfo->mouse_face_past_end = 0;
27698 hlinfo->mouse_face_window = window;
27699
27700 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27701 charpos,
27702 0, 0, 0,
27703 &ignore,
27704 glyph->face_id,
27705 1);
27706 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27707
27708 if (NILP (pointer))
27709 pointer = Qhand;
27710 }
27711 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27712 clear_mouse_face (hlinfo);
27713 }
27714 #ifdef HAVE_WINDOW_SYSTEM
27715 if (FRAME_WINDOW_P (f))
27716 define_frame_cursor1 (f, cursor, pointer);
27717 #endif
27718 }
27719
27720
27721 /* EXPORT:
27722 Take proper action when the mouse has moved to position X, Y on
27723 frame F as regards highlighting characters that have mouse-face
27724 properties. Also de-highlighting chars where the mouse was before.
27725 X and Y can be negative or out of range. */
27726
27727 void
27728 note_mouse_highlight (struct frame *f, int x, int y)
27729 {
27730 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27731 enum window_part part = ON_NOTHING;
27732 Lisp_Object window;
27733 struct window *w;
27734 Cursor cursor = No_Cursor;
27735 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27736 struct buffer *b;
27737
27738 /* When a menu is active, don't highlight because this looks odd. */
27739 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27740 if (popup_activated ())
27741 return;
27742 #endif
27743
27744 if (NILP (Vmouse_highlight)
27745 || !f->glyphs_initialized_p
27746 || f->pointer_invisible)
27747 return;
27748
27749 hlinfo->mouse_face_mouse_x = x;
27750 hlinfo->mouse_face_mouse_y = y;
27751 hlinfo->mouse_face_mouse_frame = f;
27752
27753 if (hlinfo->mouse_face_defer)
27754 return;
27755
27756 /* Which window is that in? */
27757 window = window_from_coordinates (f, x, y, &part, 1);
27758
27759 /* If displaying active text in another window, clear that. */
27760 if (! EQ (window, hlinfo->mouse_face_window)
27761 /* Also clear if we move out of text area in same window. */
27762 || (!NILP (hlinfo->mouse_face_window)
27763 && !NILP (window)
27764 && part != ON_TEXT
27765 && part != ON_MODE_LINE
27766 && part != ON_HEADER_LINE))
27767 clear_mouse_face (hlinfo);
27768
27769 /* Not on a window -> return. */
27770 if (!WINDOWP (window))
27771 return;
27772
27773 /* Reset help_echo_string. It will get recomputed below. */
27774 help_echo_string = Qnil;
27775
27776 /* Convert to window-relative pixel coordinates. */
27777 w = XWINDOW (window);
27778 frame_to_window_pixel_xy (w, &x, &y);
27779
27780 #ifdef HAVE_WINDOW_SYSTEM
27781 /* Handle tool-bar window differently since it doesn't display a
27782 buffer. */
27783 if (EQ (window, f->tool_bar_window))
27784 {
27785 note_tool_bar_highlight (f, x, y);
27786 return;
27787 }
27788 #endif
27789
27790 /* Mouse is on the mode, header line or margin? */
27791 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27792 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27793 {
27794 note_mode_line_or_margin_highlight (window, x, y, part);
27795 return;
27796 }
27797
27798 #ifdef HAVE_WINDOW_SYSTEM
27799 if (part == ON_VERTICAL_BORDER)
27800 {
27801 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27802 help_echo_string = build_string ("drag-mouse-1: resize");
27803 }
27804 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27805 || part == ON_SCROLL_BAR)
27806 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27807 else
27808 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27809 #endif
27810
27811 /* Are we in a window whose display is up to date?
27812 And verify the buffer's text has not changed. */
27813 b = XBUFFER (w->buffer);
27814 if (part == ON_TEXT
27815 && EQ (w->window_end_valid, w->buffer)
27816 && w->last_modified == BUF_MODIFF (b)
27817 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27818 {
27819 int hpos, vpos, dx, dy, area = LAST_AREA;
27820 ptrdiff_t pos;
27821 struct glyph *glyph;
27822 Lisp_Object object;
27823 Lisp_Object mouse_face = Qnil, position;
27824 Lisp_Object *overlay_vec = NULL;
27825 ptrdiff_t i, noverlays;
27826 struct buffer *obuf;
27827 ptrdiff_t obegv, ozv;
27828 int same_region;
27829
27830 /* Find the glyph under X/Y. */
27831 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27832
27833 #ifdef HAVE_WINDOW_SYSTEM
27834 /* Look for :pointer property on image. */
27835 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27836 {
27837 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27838 if (img != NULL && IMAGEP (img->spec))
27839 {
27840 Lisp_Object image_map, hotspot;
27841 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27842 !NILP (image_map))
27843 && (hotspot = find_hot_spot (image_map,
27844 glyph->slice.img.x + dx,
27845 glyph->slice.img.y + dy),
27846 CONSP (hotspot))
27847 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27848 {
27849 Lisp_Object plist;
27850
27851 /* Could check XCAR (hotspot) to see if we enter/leave
27852 this hot-spot.
27853 If so, we could look for mouse-enter, mouse-leave
27854 properties in PLIST (and do something...). */
27855 hotspot = XCDR (hotspot);
27856 if (CONSP (hotspot)
27857 && (plist = XCAR (hotspot), CONSP (plist)))
27858 {
27859 pointer = Fplist_get (plist, Qpointer);
27860 if (NILP (pointer))
27861 pointer = Qhand;
27862 help_echo_string = Fplist_get (plist, Qhelp_echo);
27863 if (!NILP (help_echo_string))
27864 {
27865 help_echo_window = window;
27866 help_echo_object = glyph->object;
27867 help_echo_pos = glyph->charpos;
27868 }
27869 }
27870 }
27871 if (NILP (pointer))
27872 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27873 }
27874 }
27875 #endif /* HAVE_WINDOW_SYSTEM */
27876
27877 /* Clear mouse face if X/Y not over text. */
27878 if (glyph == NULL
27879 || area != TEXT_AREA
27880 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27881 /* Glyph's OBJECT is an integer for glyphs inserted by the
27882 display engine for its internal purposes, like truncation
27883 and continuation glyphs and blanks beyond the end of
27884 line's text on text terminals. If we are over such a
27885 glyph, we are not over any text. */
27886 || INTEGERP (glyph->object)
27887 /* R2L rows have a stretch glyph at their front, which
27888 stands for no text, whereas L2R rows have no glyphs at
27889 all beyond the end of text. Treat such stretch glyphs
27890 like we do with NULL glyphs in L2R rows. */
27891 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27892 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27893 && glyph->type == STRETCH_GLYPH
27894 && glyph->avoid_cursor_p))
27895 {
27896 if (clear_mouse_face (hlinfo))
27897 cursor = No_Cursor;
27898 #ifdef HAVE_WINDOW_SYSTEM
27899 if (FRAME_WINDOW_P (f) && NILP (pointer))
27900 {
27901 if (area != TEXT_AREA)
27902 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27903 else
27904 pointer = Vvoid_text_area_pointer;
27905 }
27906 #endif
27907 goto set_cursor;
27908 }
27909
27910 pos = glyph->charpos;
27911 object = glyph->object;
27912 if (!STRINGP (object) && !BUFFERP (object))
27913 goto set_cursor;
27914
27915 /* If we get an out-of-range value, return now; avoid an error. */
27916 if (BUFFERP (object) && pos > BUF_Z (b))
27917 goto set_cursor;
27918
27919 /* Make the window's buffer temporarily current for
27920 overlays_at and compute_char_face. */
27921 obuf = current_buffer;
27922 current_buffer = b;
27923 obegv = BEGV;
27924 ozv = ZV;
27925 BEGV = BEG;
27926 ZV = Z;
27927
27928 /* Is this char mouse-active or does it have help-echo? */
27929 position = make_number (pos);
27930
27931 if (BUFFERP (object))
27932 {
27933 /* Put all the overlays we want in a vector in overlay_vec. */
27934 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27935 /* Sort overlays into increasing priority order. */
27936 noverlays = sort_overlays (overlay_vec, noverlays, w);
27937 }
27938 else
27939 noverlays = 0;
27940
27941 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27942
27943 if (same_region)
27944 cursor = No_Cursor;
27945
27946 /* Check mouse-face highlighting. */
27947 if (! same_region
27948 /* If there exists an overlay with mouse-face overlapping
27949 the one we are currently highlighting, we have to
27950 check if we enter the overlapping overlay, and then
27951 highlight only that. */
27952 || (OVERLAYP (hlinfo->mouse_face_overlay)
27953 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27954 {
27955 /* Find the highest priority overlay with a mouse-face. */
27956 Lisp_Object overlay = Qnil;
27957 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27958 {
27959 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27960 if (!NILP (mouse_face))
27961 overlay = overlay_vec[i];
27962 }
27963
27964 /* If we're highlighting the same overlay as before, there's
27965 no need to do that again. */
27966 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27967 goto check_help_echo;
27968 hlinfo->mouse_face_overlay = overlay;
27969
27970 /* Clear the display of the old active region, if any. */
27971 if (clear_mouse_face (hlinfo))
27972 cursor = No_Cursor;
27973
27974 /* If no overlay applies, get a text property. */
27975 if (NILP (overlay))
27976 mouse_face = Fget_text_property (position, Qmouse_face, object);
27977
27978 /* Next, compute the bounds of the mouse highlighting and
27979 display it. */
27980 if (!NILP (mouse_face) && STRINGP (object))
27981 {
27982 /* The mouse-highlighting comes from a display string
27983 with a mouse-face. */
27984 Lisp_Object s, e;
27985 ptrdiff_t ignore;
27986
27987 s = Fprevious_single_property_change
27988 (make_number (pos + 1), Qmouse_face, object, Qnil);
27989 e = Fnext_single_property_change
27990 (position, Qmouse_face, object, Qnil);
27991 if (NILP (s))
27992 s = make_number (0);
27993 if (NILP (e))
27994 e = make_number (SCHARS (object) - 1);
27995 mouse_face_from_string_pos (w, hlinfo, object,
27996 XINT (s), XINT (e));
27997 hlinfo->mouse_face_past_end = 0;
27998 hlinfo->mouse_face_window = window;
27999 hlinfo->mouse_face_face_id
28000 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28001 glyph->face_id, 1);
28002 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28003 cursor = No_Cursor;
28004 }
28005 else
28006 {
28007 /* The mouse-highlighting, if any, comes from an overlay
28008 or text property in the buffer. */
28009 Lisp_Object buffer IF_LINT (= Qnil);
28010 Lisp_Object disp_string IF_LINT (= Qnil);
28011
28012 if (STRINGP (object))
28013 {
28014 /* If we are on a display string with no mouse-face,
28015 check if the text under it has one. */
28016 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28017 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28018 pos = string_buffer_position (object, start);
28019 if (pos > 0)
28020 {
28021 mouse_face = get_char_property_and_overlay
28022 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28023 buffer = w->buffer;
28024 disp_string = object;
28025 }
28026 }
28027 else
28028 {
28029 buffer = object;
28030 disp_string = Qnil;
28031 }
28032
28033 if (!NILP (mouse_face))
28034 {
28035 Lisp_Object before, after;
28036 Lisp_Object before_string, after_string;
28037 /* To correctly find the limits of mouse highlight
28038 in a bidi-reordered buffer, we must not use the
28039 optimization of limiting the search in
28040 previous-single-property-change and
28041 next-single-property-change, because
28042 rows_from_pos_range needs the real start and end
28043 positions to DTRT in this case. That's because
28044 the first row visible in a window does not
28045 necessarily display the character whose position
28046 is the smallest. */
28047 Lisp_Object lim1 =
28048 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28049 ? Fmarker_position (w->start)
28050 : Qnil;
28051 Lisp_Object lim2 =
28052 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28053 ? make_number (BUF_Z (XBUFFER (buffer))
28054 - XFASTINT (w->window_end_pos))
28055 : Qnil;
28056
28057 if (NILP (overlay))
28058 {
28059 /* Handle the text property case. */
28060 before = Fprevious_single_property_change
28061 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28062 after = Fnext_single_property_change
28063 (make_number (pos), Qmouse_face, buffer, lim2);
28064 before_string = after_string = Qnil;
28065 }
28066 else
28067 {
28068 /* Handle the overlay case. */
28069 before = Foverlay_start (overlay);
28070 after = Foverlay_end (overlay);
28071 before_string = Foverlay_get (overlay, Qbefore_string);
28072 after_string = Foverlay_get (overlay, Qafter_string);
28073
28074 if (!STRINGP (before_string)) before_string = Qnil;
28075 if (!STRINGP (after_string)) after_string = Qnil;
28076 }
28077
28078 mouse_face_from_buffer_pos (window, hlinfo, pos,
28079 NILP (before)
28080 ? 1
28081 : XFASTINT (before),
28082 NILP (after)
28083 ? BUF_Z (XBUFFER (buffer))
28084 : XFASTINT (after),
28085 before_string, after_string,
28086 disp_string);
28087 cursor = No_Cursor;
28088 }
28089 }
28090 }
28091
28092 check_help_echo:
28093
28094 /* Look for a `help-echo' property. */
28095 if (NILP (help_echo_string)) {
28096 Lisp_Object help, overlay;
28097
28098 /* Check overlays first. */
28099 help = overlay = Qnil;
28100 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28101 {
28102 overlay = overlay_vec[i];
28103 help = Foverlay_get (overlay, Qhelp_echo);
28104 }
28105
28106 if (!NILP (help))
28107 {
28108 help_echo_string = help;
28109 help_echo_window = window;
28110 help_echo_object = overlay;
28111 help_echo_pos = pos;
28112 }
28113 else
28114 {
28115 Lisp_Object obj = glyph->object;
28116 ptrdiff_t charpos = glyph->charpos;
28117
28118 /* Try text properties. */
28119 if (STRINGP (obj)
28120 && charpos >= 0
28121 && charpos < SCHARS (obj))
28122 {
28123 help = Fget_text_property (make_number (charpos),
28124 Qhelp_echo, obj);
28125 if (NILP (help))
28126 {
28127 /* If the string itself doesn't specify a help-echo,
28128 see if the buffer text ``under'' it does. */
28129 struct glyph_row *r
28130 = MATRIX_ROW (w->current_matrix, vpos);
28131 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28132 ptrdiff_t p = string_buffer_position (obj, start);
28133 if (p > 0)
28134 {
28135 help = Fget_char_property (make_number (p),
28136 Qhelp_echo, w->buffer);
28137 if (!NILP (help))
28138 {
28139 charpos = p;
28140 obj = w->buffer;
28141 }
28142 }
28143 }
28144 }
28145 else if (BUFFERP (obj)
28146 && charpos >= BEGV
28147 && charpos < ZV)
28148 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28149 obj);
28150
28151 if (!NILP (help))
28152 {
28153 help_echo_string = help;
28154 help_echo_window = window;
28155 help_echo_object = obj;
28156 help_echo_pos = charpos;
28157 }
28158 }
28159 }
28160
28161 #ifdef HAVE_WINDOW_SYSTEM
28162 /* Look for a `pointer' property. */
28163 if (FRAME_WINDOW_P (f) && NILP (pointer))
28164 {
28165 /* Check overlays first. */
28166 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28167 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28168
28169 if (NILP (pointer))
28170 {
28171 Lisp_Object obj = glyph->object;
28172 ptrdiff_t charpos = glyph->charpos;
28173
28174 /* Try text properties. */
28175 if (STRINGP (obj)
28176 && charpos >= 0
28177 && charpos < SCHARS (obj))
28178 {
28179 pointer = Fget_text_property (make_number (charpos),
28180 Qpointer, obj);
28181 if (NILP (pointer))
28182 {
28183 /* If the string itself doesn't specify a pointer,
28184 see if the buffer text ``under'' it does. */
28185 struct glyph_row *r
28186 = MATRIX_ROW (w->current_matrix, vpos);
28187 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28188 ptrdiff_t p = string_buffer_position (obj, start);
28189 if (p > 0)
28190 pointer = Fget_char_property (make_number (p),
28191 Qpointer, w->buffer);
28192 }
28193 }
28194 else if (BUFFERP (obj)
28195 && charpos >= BEGV
28196 && charpos < ZV)
28197 pointer = Fget_text_property (make_number (charpos),
28198 Qpointer, obj);
28199 }
28200 }
28201 #endif /* HAVE_WINDOW_SYSTEM */
28202
28203 BEGV = obegv;
28204 ZV = ozv;
28205 current_buffer = obuf;
28206 }
28207
28208 set_cursor:
28209
28210 #ifdef HAVE_WINDOW_SYSTEM
28211 if (FRAME_WINDOW_P (f))
28212 define_frame_cursor1 (f, cursor, pointer);
28213 #else
28214 /* This is here to prevent a compiler error, about "label at end of
28215 compound statement". */
28216 return;
28217 #endif
28218 }
28219
28220
28221 /* EXPORT for RIF:
28222 Clear any mouse-face on window W. This function is part of the
28223 redisplay interface, and is called from try_window_id and similar
28224 functions to ensure the mouse-highlight is off. */
28225
28226 void
28227 x_clear_window_mouse_face (struct window *w)
28228 {
28229 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28230 Lisp_Object window;
28231
28232 block_input ();
28233 XSETWINDOW (window, w);
28234 if (EQ (window, hlinfo->mouse_face_window))
28235 clear_mouse_face (hlinfo);
28236 unblock_input ();
28237 }
28238
28239
28240 /* EXPORT:
28241 Just discard the mouse face information for frame F, if any.
28242 This is used when the size of F is changed. */
28243
28244 void
28245 cancel_mouse_face (struct frame *f)
28246 {
28247 Lisp_Object window;
28248 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28249
28250 window = hlinfo->mouse_face_window;
28251 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28252 {
28253 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28254 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28255 hlinfo->mouse_face_window = Qnil;
28256 }
28257 }
28258
28259
28260 \f
28261 /***********************************************************************
28262 Exposure Events
28263 ***********************************************************************/
28264
28265 #ifdef HAVE_WINDOW_SYSTEM
28266
28267 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28268 which intersects rectangle R. R is in window-relative coordinates. */
28269
28270 static void
28271 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28272 enum glyph_row_area area)
28273 {
28274 struct glyph *first = row->glyphs[area];
28275 struct glyph *end = row->glyphs[area] + row->used[area];
28276 struct glyph *last;
28277 int first_x, start_x, x;
28278
28279 if (area == TEXT_AREA && row->fill_line_p)
28280 /* If row extends face to end of line write the whole line. */
28281 draw_glyphs (w, 0, row, area,
28282 0, row->used[area],
28283 DRAW_NORMAL_TEXT, 0);
28284 else
28285 {
28286 /* Set START_X to the window-relative start position for drawing glyphs of
28287 AREA. The first glyph of the text area can be partially visible.
28288 The first glyphs of other areas cannot. */
28289 start_x = window_box_left_offset (w, area);
28290 x = start_x;
28291 if (area == TEXT_AREA)
28292 x += row->x;
28293
28294 /* Find the first glyph that must be redrawn. */
28295 while (first < end
28296 && x + first->pixel_width < r->x)
28297 {
28298 x += first->pixel_width;
28299 ++first;
28300 }
28301
28302 /* Find the last one. */
28303 last = first;
28304 first_x = x;
28305 while (last < end
28306 && x < r->x + r->width)
28307 {
28308 x += last->pixel_width;
28309 ++last;
28310 }
28311
28312 /* Repaint. */
28313 if (last > first)
28314 draw_glyphs (w, first_x - start_x, row, area,
28315 first - row->glyphs[area], last - row->glyphs[area],
28316 DRAW_NORMAL_TEXT, 0);
28317 }
28318 }
28319
28320
28321 /* Redraw the parts of the glyph row ROW on window W intersecting
28322 rectangle R. R is in window-relative coordinates. Value is
28323 non-zero if mouse-face was overwritten. */
28324
28325 static int
28326 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28327 {
28328 eassert (row->enabled_p);
28329
28330 if (row->mode_line_p || w->pseudo_window_p)
28331 draw_glyphs (w, 0, row, TEXT_AREA,
28332 0, row->used[TEXT_AREA],
28333 DRAW_NORMAL_TEXT, 0);
28334 else
28335 {
28336 if (row->used[LEFT_MARGIN_AREA])
28337 expose_area (w, row, r, LEFT_MARGIN_AREA);
28338 if (row->used[TEXT_AREA])
28339 expose_area (w, row, r, TEXT_AREA);
28340 if (row->used[RIGHT_MARGIN_AREA])
28341 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28342 draw_row_fringe_bitmaps (w, row);
28343 }
28344
28345 return row->mouse_face_p;
28346 }
28347
28348
28349 /* Redraw those parts of glyphs rows during expose event handling that
28350 overlap other rows. Redrawing of an exposed line writes over parts
28351 of lines overlapping that exposed line; this function fixes that.
28352
28353 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28354 row in W's current matrix that is exposed and overlaps other rows.
28355 LAST_OVERLAPPING_ROW is the last such row. */
28356
28357 static void
28358 expose_overlaps (struct window *w,
28359 struct glyph_row *first_overlapping_row,
28360 struct glyph_row *last_overlapping_row,
28361 XRectangle *r)
28362 {
28363 struct glyph_row *row;
28364
28365 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28366 if (row->overlapping_p)
28367 {
28368 eassert (row->enabled_p && !row->mode_line_p);
28369
28370 row->clip = r;
28371 if (row->used[LEFT_MARGIN_AREA])
28372 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28373
28374 if (row->used[TEXT_AREA])
28375 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28376
28377 if (row->used[RIGHT_MARGIN_AREA])
28378 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28379 row->clip = NULL;
28380 }
28381 }
28382
28383
28384 /* Return non-zero if W's cursor intersects rectangle R. */
28385
28386 static int
28387 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28388 {
28389 XRectangle cr, result;
28390 struct glyph *cursor_glyph;
28391 struct glyph_row *row;
28392
28393 if (w->phys_cursor.vpos >= 0
28394 && w->phys_cursor.vpos < w->current_matrix->nrows
28395 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28396 row->enabled_p)
28397 && row->cursor_in_fringe_p)
28398 {
28399 /* Cursor is in the fringe. */
28400 cr.x = window_box_right_offset (w,
28401 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28402 ? RIGHT_MARGIN_AREA
28403 : TEXT_AREA));
28404 cr.y = row->y;
28405 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28406 cr.height = row->height;
28407 return x_intersect_rectangles (&cr, r, &result);
28408 }
28409
28410 cursor_glyph = get_phys_cursor_glyph (w);
28411 if (cursor_glyph)
28412 {
28413 /* r is relative to W's box, but w->phys_cursor.x is relative
28414 to left edge of W's TEXT area. Adjust it. */
28415 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28416 cr.y = w->phys_cursor.y;
28417 cr.width = cursor_glyph->pixel_width;
28418 cr.height = w->phys_cursor_height;
28419 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28420 I assume the effect is the same -- and this is portable. */
28421 return x_intersect_rectangles (&cr, r, &result);
28422 }
28423 /* If we don't understand the format, pretend we're not in the hot-spot. */
28424 return 0;
28425 }
28426
28427
28428 /* EXPORT:
28429 Draw a vertical window border to the right of window W if W doesn't
28430 have vertical scroll bars. */
28431
28432 void
28433 x_draw_vertical_border (struct window *w)
28434 {
28435 struct frame *f = XFRAME (WINDOW_FRAME (w));
28436
28437 /* We could do better, if we knew what type of scroll-bar the adjacent
28438 windows (on either side) have... But we don't :-(
28439 However, I think this works ok. ++KFS 2003-04-25 */
28440
28441 /* Redraw borders between horizontally adjacent windows. Don't
28442 do it for frames with vertical scroll bars because either the
28443 right scroll bar of a window, or the left scroll bar of its
28444 neighbor will suffice as a border. */
28445 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28446 return;
28447
28448 if (!WINDOW_RIGHTMOST_P (w)
28449 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28450 {
28451 int x0, x1, y0, y1;
28452
28453 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28454 y1 -= 1;
28455
28456 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28457 x1 -= 1;
28458
28459 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28460 }
28461 else if (!WINDOW_LEFTMOST_P (w)
28462 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28463 {
28464 int x0, x1, y0, y1;
28465
28466 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28467 y1 -= 1;
28468
28469 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28470 x0 -= 1;
28471
28472 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28473 }
28474 }
28475
28476
28477 /* Redraw the part of window W intersection rectangle FR. Pixel
28478 coordinates in FR are frame-relative. Call this function with
28479 input blocked. Value is non-zero if the exposure overwrites
28480 mouse-face. */
28481
28482 static int
28483 expose_window (struct window *w, XRectangle *fr)
28484 {
28485 struct frame *f = XFRAME (w->frame);
28486 XRectangle wr, r;
28487 int mouse_face_overwritten_p = 0;
28488
28489 /* If window is not yet fully initialized, do nothing. This can
28490 happen when toolkit scroll bars are used and a window is split.
28491 Reconfiguring the scroll bar will generate an expose for a newly
28492 created window. */
28493 if (w->current_matrix == NULL)
28494 return 0;
28495
28496 /* When we're currently updating the window, display and current
28497 matrix usually don't agree. Arrange for a thorough display
28498 later. */
28499 if (w == updated_window)
28500 {
28501 SET_FRAME_GARBAGED (f);
28502 return 0;
28503 }
28504
28505 /* Frame-relative pixel rectangle of W. */
28506 wr.x = WINDOW_LEFT_EDGE_X (w);
28507 wr.y = WINDOW_TOP_EDGE_Y (w);
28508 wr.width = WINDOW_TOTAL_WIDTH (w);
28509 wr.height = WINDOW_TOTAL_HEIGHT (w);
28510
28511 if (x_intersect_rectangles (fr, &wr, &r))
28512 {
28513 int yb = window_text_bottom_y (w);
28514 struct glyph_row *row;
28515 int cursor_cleared_p, phys_cursor_on_p;
28516 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28517
28518 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28519 r.x, r.y, r.width, r.height));
28520
28521 /* Convert to window coordinates. */
28522 r.x -= WINDOW_LEFT_EDGE_X (w);
28523 r.y -= WINDOW_TOP_EDGE_Y (w);
28524
28525 /* Turn off the cursor. */
28526 if (!w->pseudo_window_p
28527 && phys_cursor_in_rect_p (w, &r))
28528 {
28529 x_clear_cursor (w);
28530 cursor_cleared_p = 1;
28531 }
28532 else
28533 cursor_cleared_p = 0;
28534
28535 /* If the row containing the cursor extends face to end of line,
28536 then expose_area might overwrite the cursor outside the
28537 rectangle and thus notice_overwritten_cursor might clear
28538 w->phys_cursor_on_p. We remember the original value and
28539 check later if it is changed. */
28540 phys_cursor_on_p = w->phys_cursor_on_p;
28541
28542 /* Update lines intersecting rectangle R. */
28543 first_overlapping_row = last_overlapping_row = NULL;
28544 for (row = w->current_matrix->rows;
28545 row->enabled_p;
28546 ++row)
28547 {
28548 int y0 = row->y;
28549 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28550
28551 if ((y0 >= r.y && y0 < r.y + r.height)
28552 || (y1 > r.y && y1 < r.y + r.height)
28553 || (r.y >= y0 && r.y < y1)
28554 || (r.y + r.height > y0 && r.y + r.height < y1))
28555 {
28556 /* A header line may be overlapping, but there is no need
28557 to fix overlapping areas for them. KFS 2005-02-12 */
28558 if (row->overlapping_p && !row->mode_line_p)
28559 {
28560 if (first_overlapping_row == NULL)
28561 first_overlapping_row = row;
28562 last_overlapping_row = row;
28563 }
28564
28565 row->clip = fr;
28566 if (expose_line (w, row, &r))
28567 mouse_face_overwritten_p = 1;
28568 row->clip = NULL;
28569 }
28570 else if (row->overlapping_p)
28571 {
28572 /* We must redraw a row overlapping the exposed area. */
28573 if (y0 < r.y
28574 ? y0 + row->phys_height > r.y
28575 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28576 {
28577 if (first_overlapping_row == NULL)
28578 first_overlapping_row = row;
28579 last_overlapping_row = row;
28580 }
28581 }
28582
28583 if (y1 >= yb)
28584 break;
28585 }
28586
28587 /* Display the mode line if there is one. */
28588 if (WINDOW_WANTS_MODELINE_P (w)
28589 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28590 row->enabled_p)
28591 && row->y < r.y + r.height)
28592 {
28593 if (expose_line (w, row, &r))
28594 mouse_face_overwritten_p = 1;
28595 }
28596
28597 if (!w->pseudo_window_p)
28598 {
28599 /* Fix the display of overlapping rows. */
28600 if (first_overlapping_row)
28601 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28602 fr);
28603
28604 /* Draw border between windows. */
28605 x_draw_vertical_border (w);
28606
28607 /* Turn the cursor on again. */
28608 if (cursor_cleared_p
28609 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28610 update_window_cursor (w, 1);
28611 }
28612 }
28613
28614 return mouse_face_overwritten_p;
28615 }
28616
28617
28618
28619 /* Redraw (parts) of all windows in the window tree rooted at W that
28620 intersect R. R contains frame pixel coordinates. Value is
28621 non-zero if the exposure overwrites mouse-face. */
28622
28623 static int
28624 expose_window_tree (struct window *w, XRectangle *r)
28625 {
28626 struct frame *f = XFRAME (w->frame);
28627 int mouse_face_overwritten_p = 0;
28628
28629 while (w && !FRAME_GARBAGED_P (f))
28630 {
28631 if (!NILP (w->hchild))
28632 mouse_face_overwritten_p
28633 |= expose_window_tree (XWINDOW (w->hchild), r);
28634 else if (!NILP (w->vchild))
28635 mouse_face_overwritten_p
28636 |= expose_window_tree (XWINDOW (w->vchild), r);
28637 else
28638 mouse_face_overwritten_p |= expose_window (w, r);
28639
28640 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28641 }
28642
28643 return mouse_face_overwritten_p;
28644 }
28645
28646
28647 /* EXPORT:
28648 Redisplay an exposed area of frame F. X and Y are the upper-left
28649 corner of the exposed rectangle. W and H are width and height of
28650 the exposed area. All are pixel values. W or H zero means redraw
28651 the entire frame. */
28652
28653 void
28654 expose_frame (struct frame *f, int x, int y, int w, int h)
28655 {
28656 XRectangle r;
28657 int mouse_face_overwritten_p = 0;
28658
28659 TRACE ((stderr, "expose_frame "));
28660
28661 /* No need to redraw if frame will be redrawn soon. */
28662 if (FRAME_GARBAGED_P (f))
28663 {
28664 TRACE ((stderr, " garbaged\n"));
28665 return;
28666 }
28667
28668 /* If basic faces haven't been realized yet, there is no point in
28669 trying to redraw anything. This can happen when we get an expose
28670 event while Emacs is starting, e.g. by moving another window. */
28671 if (FRAME_FACE_CACHE (f) == NULL
28672 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28673 {
28674 TRACE ((stderr, " no faces\n"));
28675 return;
28676 }
28677
28678 if (w == 0 || h == 0)
28679 {
28680 r.x = r.y = 0;
28681 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28682 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28683 }
28684 else
28685 {
28686 r.x = x;
28687 r.y = y;
28688 r.width = w;
28689 r.height = h;
28690 }
28691
28692 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28693 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28694
28695 if (WINDOWP (f->tool_bar_window))
28696 mouse_face_overwritten_p
28697 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28698
28699 #ifdef HAVE_X_WINDOWS
28700 #ifndef MSDOS
28701 #ifndef USE_X_TOOLKIT
28702 if (WINDOWP (f->menu_bar_window))
28703 mouse_face_overwritten_p
28704 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28705 #endif /* not USE_X_TOOLKIT */
28706 #endif
28707 #endif
28708
28709 /* Some window managers support a focus-follows-mouse style with
28710 delayed raising of frames. Imagine a partially obscured frame,
28711 and moving the mouse into partially obscured mouse-face on that
28712 frame. The visible part of the mouse-face will be highlighted,
28713 then the WM raises the obscured frame. With at least one WM, KDE
28714 2.1, Emacs is not getting any event for the raising of the frame
28715 (even tried with SubstructureRedirectMask), only Expose events.
28716 These expose events will draw text normally, i.e. not
28717 highlighted. Which means we must redo the highlight here.
28718 Subsume it under ``we love X''. --gerd 2001-08-15 */
28719 /* Included in Windows version because Windows most likely does not
28720 do the right thing if any third party tool offers
28721 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28722 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28723 {
28724 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28725 if (f == hlinfo->mouse_face_mouse_frame)
28726 {
28727 int mouse_x = hlinfo->mouse_face_mouse_x;
28728 int mouse_y = hlinfo->mouse_face_mouse_y;
28729 clear_mouse_face (hlinfo);
28730 note_mouse_highlight (f, mouse_x, mouse_y);
28731 }
28732 }
28733 }
28734
28735
28736 /* EXPORT:
28737 Determine the intersection of two rectangles R1 and R2. Return
28738 the intersection in *RESULT. Value is non-zero if RESULT is not
28739 empty. */
28740
28741 int
28742 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28743 {
28744 XRectangle *left, *right;
28745 XRectangle *upper, *lower;
28746 int intersection_p = 0;
28747
28748 /* Rearrange so that R1 is the left-most rectangle. */
28749 if (r1->x < r2->x)
28750 left = r1, right = r2;
28751 else
28752 left = r2, right = r1;
28753
28754 /* X0 of the intersection is right.x0, if this is inside R1,
28755 otherwise there is no intersection. */
28756 if (right->x <= left->x + left->width)
28757 {
28758 result->x = right->x;
28759
28760 /* The right end of the intersection is the minimum of
28761 the right ends of left and right. */
28762 result->width = (min (left->x + left->width, right->x + right->width)
28763 - result->x);
28764
28765 /* Same game for Y. */
28766 if (r1->y < r2->y)
28767 upper = r1, lower = r2;
28768 else
28769 upper = r2, lower = r1;
28770
28771 /* The upper end of the intersection is lower.y0, if this is inside
28772 of upper. Otherwise, there is no intersection. */
28773 if (lower->y <= upper->y + upper->height)
28774 {
28775 result->y = lower->y;
28776
28777 /* The lower end of the intersection is the minimum of the lower
28778 ends of upper and lower. */
28779 result->height = (min (lower->y + lower->height,
28780 upper->y + upper->height)
28781 - result->y);
28782 intersection_p = 1;
28783 }
28784 }
28785
28786 return intersection_p;
28787 }
28788
28789 #endif /* HAVE_WINDOW_SYSTEM */
28790
28791 \f
28792 /***********************************************************************
28793 Initialization
28794 ***********************************************************************/
28795
28796 void
28797 syms_of_xdisp (void)
28798 {
28799 Vwith_echo_area_save_vector = Qnil;
28800 staticpro (&Vwith_echo_area_save_vector);
28801
28802 Vmessage_stack = Qnil;
28803 staticpro (&Vmessage_stack);
28804
28805 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28806 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28807
28808 message_dolog_marker1 = Fmake_marker ();
28809 staticpro (&message_dolog_marker1);
28810 message_dolog_marker2 = Fmake_marker ();
28811 staticpro (&message_dolog_marker2);
28812 message_dolog_marker3 = Fmake_marker ();
28813 staticpro (&message_dolog_marker3);
28814
28815 #ifdef GLYPH_DEBUG
28816 defsubr (&Sdump_frame_glyph_matrix);
28817 defsubr (&Sdump_glyph_matrix);
28818 defsubr (&Sdump_glyph_row);
28819 defsubr (&Sdump_tool_bar_row);
28820 defsubr (&Strace_redisplay);
28821 defsubr (&Strace_to_stderr);
28822 #endif
28823 #ifdef HAVE_WINDOW_SYSTEM
28824 defsubr (&Stool_bar_lines_needed);
28825 defsubr (&Slookup_image_map);
28826 #endif
28827 defsubr (&Sformat_mode_line);
28828 defsubr (&Sinvisible_p);
28829 defsubr (&Scurrent_bidi_paragraph_direction);
28830
28831 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28832 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28833 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28834 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28835 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28836 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28837 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28838 DEFSYM (Qeval, "eval");
28839 DEFSYM (QCdata, ":data");
28840 DEFSYM (Qdisplay, "display");
28841 DEFSYM (Qspace_width, "space-width");
28842 DEFSYM (Qraise, "raise");
28843 DEFSYM (Qslice, "slice");
28844 DEFSYM (Qspace, "space");
28845 DEFSYM (Qmargin, "margin");
28846 DEFSYM (Qpointer, "pointer");
28847 DEFSYM (Qleft_margin, "left-margin");
28848 DEFSYM (Qright_margin, "right-margin");
28849 DEFSYM (Qcenter, "center");
28850 DEFSYM (Qline_height, "line-height");
28851 DEFSYM (QCalign_to, ":align-to");
28852 DEFSYM (QCrelative_width, ":relative-width");
28853 DEFSYM (QCrelative_height, ":relative-height");
28854 DEFSYM (QCeval, ":eval");
28855 DEFSYM (QCpropertize, ":propertize");
28856 DEFSYM (QCfile, ":file");
28857 DEFSYM (Qfontified, "fontified");
28858 DEFSYM (Qfontification_functions, "fontification-functions");
28859 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28860 DEFSYM (Qescape_glyph, "escape-glyph");
28861 DEFSYM (Qnobreak_space, "nobreak-space");
28862 DEFSYM (Qimage, "image");
28863 DEFSYM (Qtext, "text");
28864 DEFSYM (Qboth, "both");
28865 DEFSYM (Qboth_horiz, "both-horiz");
28866 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28867 DEFSYM (QCmap, ":map");
28868 DEFSYM (QCpointer, ":pointer");
28869 DEFSYM (Qrect, "rect");
28870 DEFSYM (Qcircle, "circle");
28871 DEFSYM (Qpoly, "poly");
28872 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28873 DEFSYM (Qgrow_only, "grow-only");
28874 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28875 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28876 DEFSYM (Qposition, "position");
28877 DEFSYM (Qbuffer_position, "buffer-position");
28878 DEFSYM (Qobject, "object");
28879 DEFSYM (Qbar, "bar");
28880 DEFSYM (Qhbar, "hbar");
28881 DEFSYM (Qbox, "box");
28882 DEFSYM (Qhollow, "hollow");
28883 DEFSYM (Qhand, "hand");
28884 DEFSYM (Qarrow, "arrow");
28885 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28886
28887 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28888 Fcons (intern_c_string ("void-variable"), Qnil)),
28889 Qnil);
28890 staticpro (&list_of_error);
28891
28892 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28893 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28894 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28895 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28896
28897 echo_buffer[0] = echo_buffer[1] = Qnil;
28898 staticpro (&echo_buffer[0]);
28899 staticpro (&echo_buffer[1]);
28900
28901 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28902 staticpro (&echo_area_buffer[0]);
28903 staticpro (&echo_area_buffer[1]);
28904
28905 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28906 staticpro (&Vmessages_buffer_name);
28907
28908 mode_line_proptrans_alist = Qnil;
28909 staticpro (&mode_line_proptrans_alist);
28910 mode_line_string_list = Qnil;
28911 staticpro (&mode_line_string_list);
28912 mode_line_string_face = Qnil;
28913 staticpro (&mode_line_string_face);
28914 mode_line_string_face_prop = Qnil;
28915 staticpro (&mode_line_string_face_prop);
28916 Vmode_line_unwind_vector = Qnil;
28917 staticpro (&Vmode_line_unwind_vector);
28918
28919 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28920
28921 help_echo_string = Qnil;
28922 staticpro (&help_echo_string);
28923 help_echo_object = Qnil;
28924 staticpro (&help_echo_object);
28925 help_echo_window = Qnil;
28926 staticpro (&help_echo_window);
28927 previous_help_echo_string = Qnil;
28928 staticpro (&previous_help_echo_string);
28929 help_echo_pos = -1;
28930
28931 DEFSYM (Qright_to_left, "right-to-left");
28932 DEFSYM (Qleft_to_right, "left-to-right");
28933
28934 #ifdef HAVE_WINDOW_SYSTEM
28935 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28936 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28937 For example, if a block cursor is over a tab, it will be drawn as
28938 wide as that tab on the display. */);
28939 x_stretch_cursor_p = 0;
28940 #endif
28941
28942 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28943 doc: /* Non-nil means highlight trailing whitespace.
28944 The face used for trailing whitespace is `trailing-whitespace'. */);
28945 Vshow_trailing_whitespace = Qnil;
28946
28947 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28948 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28949 If the value is t, Emacs highlights non-ASCII chars which have the
28950 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28951 or `escape-glyph' face respectively.
28952
28953 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28954 U+2011 (non-breaking hyphen) are affected.
28955
28956 Any other non-nil value means to display these characters as a escape
28957 glyph followed by an ordinary space or hyphen.
28958
28959 A value of nil means no special handling of these characters. */);
28960 Vnobreak_char_display = Qt;
28961
28962 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28963 doc: /* The pointer shape to show in void text areas.
28964 A value of nil means to show the text pointer. Other options are `arrow',
28965 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28966 Vvoid_text_area_pointer = Qarrow;
28967
28968 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28969 doc: /* Non-nil means don't actually do any redisplay.
28970 This is used for internal purposes. */);
28971 Vinhibit_redisplay = Qnil;
28972
28973 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28974 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28975 Vglobal_mode_string = Qnil;
28976
28977 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28978 doc: /* Marker for where to display an arrow on top of the buffer text.
28979 This must be the beginning of a line in order to work.
28980 See also `overlay-arrow-string'. */);
28981 Voverlay_arrow_position = Qnil;
28982
28983 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28984 doc: /* String to display as an arrow in non-window frames.
28985 See also `overlay-arrow-position'. */);
28986 Voverlay_arrow_string = build_pure_c_string ("=>");
28987
28988 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28989 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28990 The symbols on this list are examined during redisplay to determine
28991 where to display overlay arrows. */);
28992 Voverlay_arrow_variable_list
28993 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28994
28995 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28996 doc: /* The number of lines to try scrolling a window by when point moves out.
28997 If that fails to bring point back on frame, point is centered instead.
28998 If this is zero, point is always centered after it moves off frame.
28999 If you want scrolling to always be a line at a time, you should set
29000 `scroll-conservatively' to a large value rather than set this to 1. */);
29001
29002 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29003 doc: /* Scroll up to this many lines, to bring point back on screen.
29004 If point moves off-screen, redisplay will scroll by up to
29005 `scroll-conservatively' lines in order to bring point just barely
29006 onto the screen again. If that cannot be done, then redisplay
29007 recenters point as usual.
29008
29009 If the value is greater than 100, redisplay will never recenter point,
29010 but will always scroll just enough text to bring point into view, even
29011 if you move far away.
29012
29013 A value of zero means always recenter point if it moves off screen. */);
29014 scroll_conservatively = 0;
29015
29016 DEFVAR_INT ("scroll-margin", scroll_margin,
29017 doc: /* Number of lines of margin at the top and bottom of a window.
29018 Recenter the window whenever point gets within this many lines
29019 of the top or bottom of the window. */);
29020 scroll_margin = 0;
29021
29022 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29023 doc: /* Pixels per inch value for non-window system displays.
29024 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29025 Vdisplay_pixels_per_inch = make_float (72.0);
29026
29027 #ifdef GLYPH_DEBUG
29028 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29029 #endif
29030
29031 DEFVAR_LISP ("truncate-partial-width-windows",
29032 Vtruncate_partial_width_windows,
29033 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29034 For an integer value, truncate lines in each window narrower than the
29035 full frame width, provided the window width is less than that integer;
29036 otherwise, respect the value of `truncate-lines'.
29037
29038 For any other non-nil value, truncate lines in all windows that do
29039 not span the full frame width.
29040
29041 A value of nil means to respect the value of `truncate-lines'.
29042
29043 If `word-wrap' is enabled, you might want to reduce this. */);
29044 Vtruncate_partial_width_windows = make_number (50);
29045
29046 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29047 doc: /* Maximum buffer size for which line number should be displayed.
29048 If the buffer is bigger than this, the line number does not appear
29049 in the mode line. A value of nil means no limit. */);
29050 Vline_number_display_limit = Qnil;
29051
29052 DEFVAR_INT ("line-number-display-limit-width",
29053 line_number_display_limit_width,
29054 doc: /* Maximum line width (in characters) for line number display.
29055 If the average length of the lines near point is bigger than this, then the
29056 line number may be omitted from the mode line. */);
29057 line_number_display_limit_width = 200;
29058
29059 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29060 doc: /* Non-nil means highlight region even in nonselected windows. */);
29061 highlight_nonselected_windows = 0;
29062
29063 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29064 doc: /* Non-nil if more than one frame is visible on this display.
29065 Minibuffer-only frames don't count, but iconified frames do.
29066 This variable is not guaranteed to be accurate except while processing
29067 `frame-title-format' and `icon-title-format'. */);
29068
29069 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29070 doc: /* Template for displaying the title bar of visible frames.
29071 \(Assuming the window manager supports this feature.)
29072
29073 This variable has the same structure as `mode-line-format', except that
29074 the %c and %l constructs are ignored. It is used only on frames for
29075 which no explicit name has been set \(see `modify-frame-parameters'). */);
29076
29077 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29078 doc: /* Template for displaying the title bar of an iconified frame.
29079 \(Assuming the window manager supports this feature.)
29080 This variable has the same structure as `mode-line-format' (which see),
29081 and is used only on frames for which no explicit name has been set
29082 \(see `modify-frame-parameters'). */);
29083 Vicon_title_format
29084 = Vframe_title_format
29085 = listn (CONSTYPE_PURE, 3,
29086 intern_c_string ("multiple-frames"),
29087 build_pure_c_string ("%b"),
29088 listn (CONSTYPE_PURE, 4,
29089 empty_unibyte_string,
29090 intern_c_string ("invocation-name"),
29091 build_pure_c_string ("@"),
29092 intern_c_string ("system-name")));
29093
29094 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29095 doc: /* Maximum number of lines to keep in the message log buffer.
29096 If nil, disable message logging. If t, log messages but don't truncate
29097 the buffer when it becomes large. */);
29098 Vmessage_log_max = make_number (1000);
29099
29100 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29101 doc: /* Functions called before redisplay, if window sizes have changed.
29102 The value should be a list of functions that take one argument.
29103 Just before redisplay, for each frame, if any of its windows have changed
29104 size since the last redisplay, or have been split or deleted,
29105 all the functions in the list are called, with the frame as argument. */);
29106 Vwindow_size_change_functions = Qnil;
29107
29108 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29109 doc: /* List of functions to call before redisplaying a window with scrolling.
29110 Each function is called with two arguments, the window and its new
29111 display-start position. Note that these functions are also called by
29112 `set-window-buffer'. Also note that the value of `window-end' is not
29113 valid when these functions are called.
29114
29115 Warning: Do not use this feature to alter the way the window
29116 is scrolled. It is not designed for that, and such use probably won't
29117 work. */);
29118 Vwindow_scroll_functions = Qnil;
29119
29120 DEFVAR_LISP ("window-text-change-functions",
29121 Vwindow_text_change_functions,
29122 doc: /* Functions to call in redisplay when text in the window might change. */);
29123 Vwindow_text_change_functions = Qnil;
29124
29125 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29126 doc: /* Functions called when redisplay of a window reaches the end trigger.
29127 Each function is called with two arguments, the window and the end trigger value.
29128 See `set-window-redisplay-end-trigger'. */);
29129 Vredisplay_end_trigger_functions = Qnil;
29130
29131 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29132 doc: /* Non-nil means autoselect window with mouse pointer.
29133 If nil, do not autoselect windows.
29134 A positive number means delay autoselection by that many seconds: a
29135 window is autoselected only after the mouse has remained in that
29136 window for the duration of the delay.
29137 A negative number has a similar effect, but causes windows to be
29138 autoselected only after the mouse has stopped moving. \(Because of
29139 the way Emacs compares mouse events, you will occasionally wait twice
29140 that time before the window gets selected.\)
29141 Any other value means to autoselect window instantaneously when the
29142 mouse pointer enters it.
29143
29144 Autoselection selects the minibuffer only if it is active, and never
29145 unselects the minibuffer if it is active.
29146
29147 When customizing this variable make sure that the actual value of
29148 `focus-follows-mouse' matches the behavior of your window manager. */);
29149 Vmouse_autoselect_window = Qnil;
29150
29151 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29152 doc: /* Non-nil means automatically resize tool-bars.
29153 This dynamically changes the tool-bar's height to the minimum height
29154 that is needed to make all tool-bar items visible.
29155 If value is `grow-only', the tool-bar's height is only increased
29156 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29157 Vauto_resize_tool_bars = Qt;
29158
29159 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29160 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29161 auto_raise_tool_bar_buttons_p = 1;
29162
29163 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29164 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29165 make_cursor_line_fully_visible_p = 1;
29166
29167 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29168 doc: /* Border below tool-bar in pixels.
29169 If an integer, use it as the height of the border.
29170 If it is one of `internal-border-width' or `border-width', use the
29171 value of the corresponding frame parameter.
29172 Otherwise, no border is added below the tool-bar. */);
29173 Vtool_bar_border = Qinternal_border_width;
29174
29175 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29176 doc: /* Margin around tool-bar buttons in pixels.
29177 If an integer, use that for both horizontal and vertical margins.
29178 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29179 HORZ specifying the horizontal margin, and VERT specifying the
29180 vertical margin. */);
29181 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29182
29183 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29184 doc: /* Relief thickness of tool-bar buttons. */);
29185 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29186
29187 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29188 doc: /* Tool bar style to use.
29189 It can be one of
29190 image - show images only
29191 text - show text only
29192 both - show both, text below image
29193 both-horiz - show text to the right of the image
29194 text-image-horiz - show text to the left of the image
29195 any other - use system default or image if no system default.
29196
29197 This variable only affects the GTK+ toolkit version of Emacs. */);
29198 Vtool_bar_style = Qnil;
29199
29200 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29201 doc: /* Maximum number of characters a label can have to be shown.
29202 The tool bar style must also show labels for this to have any effect, see
29203 `tool-bar-style'. */);
29204 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29205
29206 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29207 doc: /* List of functions to call to fontify regions of text.
29208 Each function is called with one argument POS. Functions must
29209 fontify a region starting at POS in the current buffer, and give
29210 fontified regions the property `fontified'. */);
29211 Vfontification_functions = Qnil;
29212 Fmake_variable_buffer_local (Qfontification_functions);
29213
29214 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29215 unibyte_display_via_language_environment,
29216 doc: /* Non-nil means display unibyte text according to language environment.
29217 Specifically, this means that raw bytes in the range 160-255 decimal
29218 are displayed by converting them to the equivalent multibyte characters
29219 according to the current language environment. As a result, they are
29220 displayed according to the current fontset.
29221
29222 Note that this variable affects only how these bytes are displayed,
29223 but does not change the fact they are interpreted as raw bytes. */);
29224 unibyte_display_via_language_environment = 0;
29225
29226 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29227 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29228 If a float, it specifies a fraction of the mini-window frame's height.
29229 If an integer, it specifies a number of lines. */);
29230 Vmax_mini_window_height = make_float (0.25);
29231
29232 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29233 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29234 A value of nil means don't automatically resize mini-windows.
29235 A value of t means resize them to fit the text displayed in them.
29236 A value of `grow-only', the default, means let mini-windows grow only;
29237 they return to their normal size when the minibuffer is closed, or the
29238 echo area becomes empty. */);
29239 Vresize_mini_windows = Qgrow_only;
29240
29241 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29242 doc: /* Alist specifying how to blink the cursor off.
29243 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29244 `cursor-type' frame-parameter or variable equals ON-STATE,
29245 comparing using `equal', Emacs uses OFF-STATE to specify
29246 how to blink it off. ON-STATE and OFF-STATE are values for
29247 the `cursor-type' frame parameter.
29248
29249 If a frame's ON-STATE has no entry in this list,
29250 the frame's other specifications determine how to blink the cursor off. */);
29251 Vblink_cursor_alist = Qnil;
29252
29253 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29254 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29255 If non-nil, windows are automatically scrolled horizontally to make
29256 point visible. */);
29257 automatic_hscrolling_p = 1;
29258 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29259
29260 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29261 doc: /* How many columns away from the window edge point is allowed to get
29262 before automatic hscrolling will horizontally scroll the window. */);
29263 hscroll_margin = 5;
29264
29265 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29266 doc: /* How many columns to scroll the window when point gets too close to the edge.
29267 When point is less than `hscroll-margin' columns from the window
29268 edge, automatic hscrolling will scroll the window by the amount of columns
29269 determined by this variable. If its value is a positive integer, scroll that
29270 many columns. If it's a positive floating-point number, it specifies the
29271 fraction of the window's width to scroll. If it's nil or zero, point will be
29272 centered horizontally after the scroll. Any other value, including negative
29273 numbers, are treated as if the value were zero.
29274
29275 Automatic hscrolling always moves point outside the scroll margin, so if
29276 point was more than scroll step columns inside the margin, the window will
29277 scroll more than the value given by the scroll step.
29278
29279 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29280 and `scroll-right' overrides this variable's effect. */);
29281 Vhscroll_step = make_number (0);
29282
29283 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29284 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29285 Bind this around calls to `message' to let it take effect. */);
29286 message_truncate_lines = 0;
29287
29288 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29289 doc: /* Normal hook run to update the menu bar definitions.
29290 Redisplay runs this hook before it redisplays the menu bar.
29291 This is used to update submenus such as Buffers,
29292 whose contents depend on various data. */);
29293 Vmenu_bar_update_hook = Qnil;
29294
29295 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29296 doc: /* Frame for which we are updating a menu.
29297 The enable predicate for a menu binding should check this variable. */);
29298 Vmenu_updating_frame = Qnil;
29299
29300 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29301 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29302 inhibit_menubar_update = 0;
29303
29304 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29305 doc: /* Prefix prepended to all continuation lines at display time.
29306 The value may be a string, an image, or a stretch-glyph; it is
29307 interpreted in the same way as the value of a `display' text property.
29308
29309 This variable is overridden by any `wrap-prefix' text or overlay
29310 property.
29311
29312 To add a prefix to non-continuation lines, use `line-prefix'. */);
29313 Vwrap_prefix = Qnil;
29314 DEFSYM (Qwrap_prefix, "wrap-prefix");
29315 Fmake_variable_buffer_local (Qwrap_prefix);
29316
29317 DEFVAR_LISP ("line-prefix", Vline_prefix,
29318 doc: /* Prefix prepended to all non-continuation lines at display time.
29319 The value may be a string, an image, or a stretch-glyph; it is
29320 interpreted in the same way as the value of a `display' text property.
29321
29322 This variable is overridden by any `line-prefix' text or overlay
29323 property.
29324
29325 To add a prefix to continuation lines, use `wrap-prefix'. */);
29326 Vline_prefix = Qnil;
29327 DEFSYM (Qline_prefix, "line-prefix");
29328 Fmake_variable_buffer_local (Qline_prefix);
29329
29330 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29331 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29332 inhibit_eval_during_redisplay = 0;
29333
29334 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29335 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29336 inhibit_free_realized_faces = 0;
29337
29338 #ifdef GLYPH_DEBUG
29339 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29340 doc: /* Inhibit try_window_id display optimization. */);
29341 inhibit_try_window_id = 0;
29342
29343 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29344 doc: /* Inhibit try_window_reusing display optimization. */);
29345 inhibit_try_window_reusing = 0;
29346
29347 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29348 doc: /* Inhibit try_cursor_movement display optimization. */);
29349 inhibit_try_cursor_movement = 0;
29350 #endif /* GLYPH_DEBUG */
29351
29352 DEFVAR_INT ("overline-margin", overline_margin,
29353 doc: /* Space between overline and text, in pixels.
29354 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29355 margin to the character height. */);
29356 overline_margin = 2;
29357
29358 DEFVAR_INT ("underline-minimum-offset",
29359 underline_minimum_offset,
29360 doc: /* Minimum distance between baseline and underline.
29361 This can improve legibility of underlined text at small font sizes,
29362 particularly when using variable `x-use-underline-position-properties'
29363 with fonts that specify an UNDERLINE_POSITION relatively close to the
29364 baseline. The default value is 1. */);
29365 underline_minimum_offset = 1;
29366
29367 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29368 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29369 This feature only works when on a window system that can change
29370 cursor shapes. */);
29371 display_hourglass_p = 1;
29372
29373 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29374 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29375 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29376
29377 hourglass_atimer = NULL;
29378 hourglass_shown_p = 0;
29379
29380 DEFSYM (Qglyphless_char, "glyphless-char");
29381 DEFSYM (Qhex_code, "hex-code");
29382 DEFSYM (Qempty_box, "empty-box");
29383 DEFSYM (Qthin_space, "thin-space");
29384 DEFSYM (Qzero_width, "zero-width");
29385
29386 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29387 /* Intern this now in case it isn't already done.
29388 Setting this variable twice is harmless.
29389 But don't staticpro it here--that is done in alloc.c. */
29390 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29391 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29392
29393 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29394 doc: /* Char-table defining glyphless characters.
29395 Each element, if non-nil, should be one of the following:
29396 an ASCII acronym string: display this string in a box
29397 `hex-code': display the hexadecimal code of a character in a box
29398 `empty-box': display as an empty box
29399 `thin-space': display as 1-pixel width space
29400 `zero-width': don't display
29401 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29402 display method for graphical terminals and text terminals respectively.
29403 GRAPHICAL and TEXT should each have one of the values listed above.
29404
29405 The char-table has one extra slot to control the display of a character for
29406 which no font is found. This slot only takes effect on graphical terminals.
29407 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29408 `thin-space'. The default is `empty-box'. */);
29409 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29410 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29411 Qempty_box);
29412
29413 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29414 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29415 Vdebug_on_message = Qnil;
29416 }
29417
29418
29419 /* Initialize this module when Emacs starts. */
29420
29421 void
29422 init_xdisp (void)
29423 {
29424 current_header_line_height = current_mode_line_height = -1;
29425
29426 CHARPOS (this_line_start_pos) = 0;
29427
29428 if (!noninteractive)
29429 {
29430 struct window *m = XWINDOW (minibuf_window);
29431 Lisp_Object frame = m->frame;
29432 struct frame *f = XFRAME (frame);
29433 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29434 struct window *r = XWINDOW (root);
29435 int i;
29436
29437 echo_area_window = minibuf_window;
29438
29439 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29440 wset_total_lines
29441 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29442 wset_total_cols (r, make_number (FRAME_COLS (f)));
29443 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29444 wset_total_lines (m, make_number (1));
29445 wset_total_cols (m, make_number (FRAME_COLS (f)));
29446
29447 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29448 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29449 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29450
29451 /* The default ellipsis glyphs `...'. */
29452 for (i = 0; i < 3; ++i)
29453 default_invis_vector[i] = make_number ('.');
29454 }
29455
29456 {
29457 /* Allocate the buffer for frame titles.
29458 Also used for `format-mode-line'. */
29459 int size = 100;
29460 mode_line_noprop_buf = xmalloc (size);
29461 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29462 mode_line_noprop_ptr = mode_line_noprop_buf;
29463 mode_line_target = MODE_LINE_DISPLAY;
29464 }
29465
29466 help_echo_showing_p = 0;
29467 }
29468
29469 /* Platform-independent portion of hourglass implementation. */
29470
29471 /* Cancel a currently active hourglass timer, and start a new one. */
29472 void
29473 start_hourglass (void)
29474 {
29475 #if defined (HAVE_WINDOW_SYSTEM)
29476 EMACS_TIME delay;
29477
29478 cancel_hourglass ();
29479
29480 if (INTEGERP (Vhourglass_delay)
29481 && XINT (Vhourglass_delay) > 0)
29482 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29483 TYPE_MAXIMUM (time_t)),
29484 0);
29485 else if (FLOATP (Vhourglass_delay)
29486 && XFLOAT_DATA (Vhourglass_delay) > 0)
29487 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29488 else
29489 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29490
29491 #ifdef HAVE_NTGUI
29492 {
29493 extern void w32_note_current_window (void);
29494 w32_note_current_window ();
29495 }
29496 #endif /* HAVE_NTGUI */
29497
29498 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29499 show_hourglass, NULL);
29500 #endif
29501 }
29502
29503
29504 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29505 shown. */
29506 void
29507 cancel_hourglass (void)
29508 {
29509 #if defined (HAVE_WINDOW_SYSTEM)
29510 if (hourglass_atimer)
29511 {
29512 cancel_atimer (hourglass_atimer);
29513 hourglass_atimer = NULL;
29514 }
29515
29516 if (hourglass_shown_p)
29517 hide_hourglass ();
29518 #endif
29519 }