* xdisp.c: Minor style fixes.
[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 /* Return mark position if current buffer has the region of non-zero length,
2563 or -1 otherwise. */
2564
2565 static ptrdiff_t
2566 markpos_of_region (void)
2567 {
2568 if (!NILP (Vtransient_mark_mode)
2569 && !NILP (BVAR (current_buffer, mark_active))
2570 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2571 {
2572 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2573
2574 if (markpos != PT)
2575 return markpos;
2576 }
2577 return -1;
2578 }
2579
2580 /***********************************************************************
2581 Iterator initialization
2582 ***********************************************************************/
2583
2584 /* Initialize IT for displaying current_buffer in window W, starting
2585 at character position CHARPOS. CHARPOS < 0 means that no buffer
2586 position is specified which is useful when the iterator is assigned
2587 a position later. BYTEPOS is the byte position corresponding to
2588 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2589
2590 If ROW is not null, calls to produce_glyphs with IT as parameter
2591 will produce glyphs in that row.
2592
2593 BASE_FACE_ID is the id of a base face to use. It must be one of
2594 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2595 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2596 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2597
2598 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2599 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2600 will be initialized to use the corresponding mode line glyph row of
2601 the desired matrix of W. */
2602
2603 void
2604 init_iterator (struct it *it, struct window *w,
2605 ptrdiff_t charpos, ptrdiff_t bytepos,
2606 struct glyph_row *row, enum face_id base_face_id)
2607 {
2608 ptrdiff_t markpos;
2609 enum face_id remapped_base_face_id = base_face_id;
2610
2611 /* Some precondition checks. */
2612 eassert (w != NULL && it != NULL);
2613 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2614 && charpos <= ZV));
2615
2616 /* If face attributes have been changed since the last redisplay,
2617 free realized faces now because they depend on face definitions
2618 that might have changed. Don't free faces while there might be
2619 desired matrices pending which reference these faces. */
2620 if (face_change_count && !inhibit_free_realized_faces)
2621 {
2622 face_change_count = 0;
2623 free_all_realized_faces (Qnil);
2624 }
2625
2626 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2627 if (! NILP (Vface_remapping_alist))
2628 remapped_base_face_id
2629 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2630
2631 /* Use one of the mode line rows of W's desired matrix if
2632 appropriate. */
2633 if (row == NULL)
2634 {
2635 if (base_face_id == MODE_LINE_FACE_ID
2636 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2637 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2638 else if (base_face_id == HEADER_LINE_FACE_ID)
2639 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2640 }
2641
2642 /* Clear IT. */
2643 memset (it, 0, sizeof *it);
2644 it->current.overlay_string_index = -1;
2645 it->current.dpvec_index = -1;
2646 it->base_face_id = remapped_base_face_id;
2647 it->string = Qnil;
2648 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2649 it->paragraph_embedding = L2R;
2650 it->bidi_it.string.lstring = Qnil;
2651 it->bidi_it.string.s = NULL;
2652 it->bidi_it.string.bufpos = 0;
2653
2654 /* The window in which we iterate over current_buffer: */
2655 XSETWINDOW (it->window, w);
2656 it->w = w;
2657 it->f = XFRAME (w->frame);
2658
2659 it->cmp_it.id = -1;
2660
2661 /* Extra space between lines (on window systems only). */
2662 if (base_face_id == DEFAULT_FACE_ID
2663 && FRAME_WINDOW_P (it->f))
2664 {
2665 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2666 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2667 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2668 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2669 * FRAME_LINE_HEIGHT (it->f));
2670 else if (it->f->extra_line_spacing > 0)
2671 it->extra_line_spacing = it->f->extra_line_spacing;
2672 it->max_extra_line_spacing = 0;
2673 }
2674
2675 /* If realized faces have been removed, e.g. because of face
2676 attribute changes of named faces, recompute them. When running
2677 in batch mode, the face cache of the initial frame is null. If
2678 we happen to get called, make a dummy face cache. */
2679 if (FRAME_FACE_CACHE (it->f) == NULL)
2680 init_frame_faces (it->f);
2681 if (FRAME_FACE_CACHE (it->f)->used == 0)
2682 recompute_basic_faces (it->f);
2683
2684 /* Current value of the `slice', `space-width', and 'height' properties. */
2685 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2686 it->space_width = Qnil;
2687 it->font_height = Qnil;
2688 it->override_ascent = -1;
2689
2690 /* Are control characters displayed as `^C'? */
2691 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2692
2693 /* -1 means everything between a CR and the following line end
2694 is invisible. >0 means lines indented more than this value are
2695 invisible. */
2696 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2697 ? (clip_to_bounds
2698 (-1, XINT (BVAR (current_buffer, selective_display)),
2699 PTRDIFF_MAX))
2700 : (!NILP (BVAR (current_buffer, selective_display))
2701 ? -1 : 0));
2702 it->selective_display_ellipsis_p
2703 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2704
2705 /* Display table to use. */
2706 it->dp = window_display_table (w);
2707
2708 /* Are multibyte characters enabled in current_buffer? */
2709 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2710
2711 /* If visible region is of non-zero length, set IT->region_beg_charpos
2712 and IT->region_end_charpos to the start and end of a visible region
2713 in window IT->w. Set both to -1 to indicate no region. */
2714 markpos = markpos_of_region ();
2715 if (0 <= markpos
2716 /* Maybe highlight only in selected window. */
2717 && (/* Either show region everywhere. */
2718 highlight_nonselected_windows
2719 /* Or show region in the selected window. */
2720 || w == XWINDOW (selected_window)
2721 /* Or show the region if we are in the mini-buffer and W is
2722 the window the mini-buffer refers to. */
2723 || (MINI_WINDOW_P (XWINDOW (selected_window))
2724 && WINDOWP (minibuf_selected_window)
2725 && w == XWINDOW (minibuf_selected_window))))
2726 {
2727 it->region_beg_charpos = min (PT, markpos);
2728 it->region_end_charpos = max (PT, markpos);
2729 }
2730 else
2731 it->region_beg_charpos = it->region_end_charpos = -1;
2732
2733 /* Get the position at which the redisplay_end_trigger hook should
2734 be run, if it is to be run at all. */
2735 if (MARKERP (w->redisplay_end_trigger)
2736 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2737 it->redisplay_end_trigger_charpos
2738 = marker_position (w->redisplay_end_trigger);
2739 else if (INTEGERP (w->redisplay_end_trigger))
2740 it->redisplay_end_trigger_charpos =
2741 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2742
2743 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2744
2745 /* Are lines in the display truncated? */
2746 if (base_face_id != DEFAULT_FACE_ID
2747 || it->w->hscroll
2748 || (! WINDOW_FULL_WIDTH_P (it->w)
2749 && ((!NILP (Vtruncate_partial_width_windows)
2750 && !INTEGERP (Vtruncate_partial_width_windows))
2751 || (INTEGERP (Vtruncate_partial_width_windows)
2752 && (WINDOW_TOTAL_COLS (it->w)
2753 < XINT (Vtruncate_partial_width_windows))))))
2754 it->line_wrap = TRUNCATE;
2755 else if (NILP (BVAR (current_buffer, truncate_lines)))
2756 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2757 ? WINDOW_WRAP : WORD_WRAP;
2758 else
2759 it->line_wrap = TRUNCATE;
2760
2761 /* Get dimensions of truncation and continuation glyphs. These are
2762 displayed as fringe bitmaps under X, but we need them for such
2763 frames when the fringes are turned off. But leave the dimensions
2764 zero for tooltip frames, as these glyphs look ugly there and also
2765 sabotage calculations of tooltip dimensions in x-show-tip. */
2766 #ifdef HAVE_WINDOW_SYSTEM
2767 if (!(FRAME_WINDOW_P (it->f)
2768 && FRAMEP (tip_frame)
2769 && it->f == XFRAME (tip_frame)))
2770 #endif
2771 {
2772 if (it->line_wrap == TRUNCATE)
2773 {
2774 /* We will need the truncation glyph. */
2775 eassert (it->glyph_row == NULL);
2776 produce_special_glyphs (it, IT_TRUNCATION);
2777 it->truncation_pixel_width = it->pixel_width;
2778 }
2779 else
2780 {
2781 /* We will need the continuation glyph. */
2782 eassert (it->glyph_row == NULL);
2783 produce_special_glyphs (it, IT_CONTINUATION);
2784 it->continuation_pixel_width = it->pixel_width;
2785 }
2786 }
2787
2788 /* Reset these values to zero because the produce_special_glyphs
2789 above has changed them. */
2790 it->pixel_width = it->ascent = it->descent = 0;
2791 it->phys_ascent = it->phys_descent = 0;
2792
2793 /* Set this after getting the dimensions of truncation and
2794 continuation glyphs, so that we don't produce glyphs when calling
2795 produce_special_glyphs, above. */
2796 it->glyph_row = row;
2797 it->area = TEXT_AREA;
2798
2799 /* Forget any previous info about this row being reversed. */
2800 if (it->glyph_row)
2801 it->glyph_row->reversed_p = 0;
2802
2803 /* Get the dimensions of the display area. The display area
2804 consists of the visible window area plus a horizontally scrolled
2805 part to the left of the window. All x-values are relative to the
2806 start of this total display area. */
2807 if (base_face_id != DEFAULT_FACE_ID)
2808 {
2809 /* Mode lines, menu bar in terminal frames. */
2810 it->first_visible_x = 0;
2811 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2812 }
2813 else
2814 {
2815 it->first_visible_x =
2816 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2817 it->last_visible_x = (it->first_visible_x
2818 + window_box_width (w, TEXT_AREA));
2819
2820 /* If we truncate lines, leave room for the truncation glyph(s) at
2821 the right margin. Otherwise, leave room for the continuation
2822 glyph(s). Done only if the window has no fringes. Since we
2823 don't know at this point whether there will be any R2L lines in
2824 the window, we reserve space for truncation/continuation glyphs
2825 even if only one of the fringes is absent. */
2826 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2827 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2828 {
2829 if (it->line_wrap == TRUNCATE)
2830 it->last_visible_x -= it->truncation_pixel_width;
2831 else
2832 it->last_visible_x -= it->continuation_pixel_width;
2833 }
2834
2835 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2836 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2837 }
2838
2839 /* Leave room for a border glyph. */
2840 if (!FRAME_WINDOW_P (it->f)
2841 && !WINDOW_RIGHTMOST_P (it->w))
2842 it->last_visible_x -= 1;
2843
2844 it->last_visible_y = window_text_bottom_y (w);
2845
2846 /* For mode lines and alike, arrange for the first glyph having a
2847 left box line if the face specifies a box. */
2848 if (base_face_id != DEFAULT_FACE_ID)
2849 {
2850 struct face *face;
2851
2852 it->face_id = remapped_base_face_id;
2853
2854 /* If we have a boxed mode line, make the first character appear
2855 with a left box line. */
2856 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2857 if (face->box != FACE_NO_BOX)
2858 it->start_of_box_run_p = 1;
2859 }
2860
2861 /* If a buffer position was specified, set the iterator there,
2862 getting overlays and face properties from that position. */
2863 if (charpos >= BUF_BEG (current_buffer))
2864 {
2865 it->end_charpos = ZV;
2866 IT_CHARPOS (*it) = charpos;
2867
2868 /* We will rely on `reseat' to set this up properly, via
2869 handle_face_prop. */
2870 it->face_id = it->base_face_id;
2871
2872 /* Compute byte position if not specified. */
2873 if (bytepos < charpos)
2874 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2875 else
2876 IT_BYTEPOS (*it) = bytepos;
2877
2878 it->start = it->current;
2879 /* Do we need to reorder bidirectional text? Not if this is a
2880 unibyte buffer: by definition, none of the single-byte
2881 characters are strong R2L, so no reordering is needed. And
2882 bidi.c doesn't support unibyte buffers anyway. Also, don't
2883 reorder while we are loading loadup.el, since the tables of
2884 character properties needed for reordering are not yet
2885 available. */
2886 it->bidi_p =
2887 NILP (Vpurify_flag)
2888 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2889 && it->multibyte_p;
2890
2891 /* If we are to reorder bidirectional text, init the bidi
2892 iterator. */
2893 if (it->bidi_p)
2894 {
2895 /* Note the paragraph direction that this buffer wants to
2896 use. */
2897 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qleft_to_right))
2899 it->paragraph_embedding = L2R;
2900 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2901 Qright_to_left))
2902 it->paragraph_embedding = R2L;
2903 else
2904 it->paragraph_embedding = NEUTRAL_DIR;
2905 bidi_unshelve_cache (NULL, 0);
2906 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2907 &it->bidi_it);
2908 }
2909
2910 /* Compute faces etc. */
2911 reseat (it, it->current.pos, 1);
2912 }
2913
2914 CHECK_IT (it);
2915 }
2916
2917
2918 /* Initialize IT for the display of window W with window start POS. */
2919
2920 void
2921 start_display (struct it *it, struct window *w, struct text_pos pos)
2922 {
2923 struct glyph_row *row;
2924 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2925
2926 row = w->desired_matrix->rows + first_vpos;
2927 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2928 it->first_vpos = first_vpos;
2929
2930 /* Don't reseat to previous visible line start if current start
2931 position is in a string or image. */
2932 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2933 {
2934 int start_at_line_beg_p;
2935 int first_y = it->current_y;
2936
2937 /* If window start is not at a line start, skip forward to POS to
2938 get the correct continuation lines width. */
2939 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2940 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2941 if (!start_at_line_beg_p)
2942 {
2943 int new_x;
2944
2945 reseat_at_previous_visible_line_start (it);
2946 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2947
2948 new_x = it->current_x + it->pixel_width;
2949
2950 /* If lines are continued, this line may end in the middle
2951 of a multi-glyph character (e.g. a control character
2952 displayed as \003, or in the middle of an overlay
2953 string). In this case move_it_to above will not have
2954 taken us to the start of the continuation line but to the
2955 end of the continued line. */
2956 if (it->current_x > 0
2957 && it->line_wrap != TRUNCATE /* Lines are continued. */
2958 && (/* And glyph doesn't fit on the line. */
2959 new_x > it->last_visible_x
2960 /* Or it fits exactly and we're on a window
2961 system frame. */
2962 || (new_x == it->last_visible_x
2963 && FRAME_WINDOW_P (it->f)
2964 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2965 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2966 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2967 {
2968 if ((it->current.dpvec_index >= 0
2969 || it->current.overlay_string_index >= 0)
2970 /* If we are on a newline from a display vector or
2971 overlay string, then we are already at the end of
2972 a screen line; no need to go to the next line in
2973 that case, as this line is not really continued.
2974 (If we do go to the next line, C-e will not DTRT.) */
2975 && it->c != '\n')
2976 {
2977 set_iterator_to_next (it, 1);
2978 move_it_in_display_line_to (it, -1, -1, 0);
2979 }
2980
2981 it->continuation_lines_width += it->current_x;
2982 }
2983 /* If the character at POS is displayed via a display
2984 vector, move_it_to above stops at the final glyph of
2985 IT->dpvec. To make the caller redisplay that character
2986 again (a.k.a. start at POS), we need to reset the
2987 dpvec_index to the beginning of IT->dpvec. */
2988 else if (it->current.dpvec_index >= 0)
2989 it->current.dpvec_index = 0;
2990
2991 /* We're starting a new display line, not affected by the
2992 height of the continued line, so clear the appropriate
2993 fields in the iterator structure. */
2994 it->max_ascent = it->max_descent = 0;
2995 it->max_phys_ascent = it->max_phys_descent = 0;
2996
2997 it->current_y = first_y;
2998 it->vpos = 0;
2999 it->current_x = it->hpos = 0;
3000 }
3001 }
3002 }
3003
3004
3005 /* Return 1 if POS is a position in ellipses displayed for invisible
3006 text. W is the window we display, for text property lookup. */
3007
3008 static int
3009 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3010 {
3011 Lisp_Object prop, window;
3012 int ellipses_p = 0;
3013 ptrdiff_t charpos = CHARPOS (pos->pos);
3014
3015 /* If POS specifies a position in a display vector, this might
3016 be for an ellipsis displayed for invisible text. We won't
3017 get the iterator set up for delivering that ellipsis unless
3018 we make sure that it gets aware of the invisible text. */
3019 if (pos->dpvec_index >= 0
3020 && pos->overlay_string_index < 0
3021 && CHARPOS (pos->string_pos) < 0
3022 && charpos > BEGV
3023 && (XSETWINDOW (window, w),
3024 prop = Fget_char_property (make_number (charpos),
3025 Qinvisible, window),
3026 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3027 {
3028 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3029 window);
3030 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3031 }
3032
3033 return ellipses_p;
3034 }
3035
3036
3037 /* Initialize IT for stepping through current_buffer in window W,
3038 starting at position POS that includes overlay string and display
3039 vector/ control character translation position information. Value
3040 is zero if there are overlay strings with newlines at POS. */
3041
3042 static int
3043 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3044 {
3045 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3046 int i, overlay_strings_with_newlines = 0;
3047
3048 /* If POS specifies a position in a display vector, this might
3049 be for an ellipsis displayed for invisible text. We won't
3050 get the iterator set up for delivering that ellipsis unless
3051 we make sure that it gets aware of the invisible text. */
3052 if (in_ellipses_for_invisible_text_p (pos, w))
3053 {
3054 --charpos;
3055 bytepos = 0;
3056 }
3057
3058 /* Keep in mind: the call to reseat in init_iterator skips invisible
3059 text, so we might end up at a position different from POS. This
3060 is only a problem when POS is a row start after a newline and an
3061 overlay starts there with an after-string, and the overlay has an
3062 invisible property. Since we don't skip invisible text in
3063 display_line and elsewhere immediately after consuming the
3064 newline before the row start, such a POS will not be in a string,
3065 but the call to init_iterator below will move us to the
3066 after-string. */
3067 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3068
3069 /* This only scans the current chunk -- it should scan all chunks.
3070 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3071 to 16 in 22.1 to make this a lesser problem. */
3072 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3073 {
3074 const char *s = SSDATA (it->overlay_strings[i]);
3075 const char *e = s + SBYTES (it->overlay_strings[i]);
3076
3077 while (s < e && *s != '\n')
3078 ++s;
3079
3080 if (s < e)
3081 {
3082 overlay_strings_with_newlines = 1;
3083 break;
3084 }
3085 }
3086
3087 /* If position is within an overlay string, set up IT to the right
3088 overlay string. */
3089 if (pos->overlay_string_index >= 0)
3090 {
3091 int relative_index;
3092
3093 /* If the first overlay string happens to have a `display'
3094 property for an image, the iterator will be set up for that
3095 image, and we have to undo that setup first before we can
3096 correct the overlay string index. */
3097 if (it->method == GET_FROM_IMAGE)
3098 pop_it (it);
3099
3100 /* We already have the first chunk of overlay strings in
3101 IT->overlay_strings. Load more until the one for
3102 pos->overlay_string_index is in IT->overlay_strings. */
3103 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3104 {
3105 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3106 it->current.overlay_string_index = 0;
3107 while (n--)
3108 {
3109 load_overlay_strings (it, 0);
3110 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3111 }
3112 }
3113
3114 it->current.overlay_string_index = pos->overlay_string_index;
3115 relative_index = (it->current.overlay_string_index
3116 % OVERLAY_STRING_CHUNK_SIZE);
3117 it->string = it->overlay_strings[relative_index];
3118 eassert (STRINGP (it->string));
3119 it->current.string_pos = pos->string_pos;
3120 it->method = GET_FROM_STRING;
3121 it->end_charpos = SCHARS (it->string);
3122 /* Set up the bidi iterator for this overlay string. */
3123 if (it->bidi_p)
3124 {
3125 it->bidi_it.string.lstring = it->string;
3126 it->bidi_it.string.s = NULL;
3127 it->bidi_it.string.schars = SCHARS (it->string);
3128 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3129 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3130 it->bidi_it.string.unibyte = !it->multibyte_p;
3131 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3132 FRAME_WINDOW_P (it->f), &it->bidi_it);
3133
3134 /* Synchronize the state of the bidi iterator with
3135 pos->string_pos. For any string position other than
3136 zero, this will be done automagically when we resume
3137 iteration over the string and get_visually_first_element
3138 is called. But if string_pos is zero, and the string is
3139 to be reordered for display, we need to resync manually,
3140 since it could be that the iteration state recorded in
3141 pos ended at string_pos of 0 moving backwards in string. */
3142 if (CHARPOS (pos->string_pos) == 0)
3143 {
3144 get_visually_first_element (it);
3145 if (IT_STRING_CHARPOS (*it) != 0)
3146 do {
3147 /* Paranoia. */
3148 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3149 bidi_move_to_visually_next (&it->bidi_it);
3150 } while (it->bidi_it.charpos != 0);
3151 }
3152 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3153 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3154 }
3155 }
3156
3157 if (CHARPOS (pos->string_pos) >= 0)
3158 {
3159 /* Recorded position is not in an overlay string, but in another
3160 string. This can only be a string from a `display' property.
3161 IT should already be filled with that string. */
3162 it->current.string_pos = pos->string_pos;
3163 eassert (STRINGP (it->string));
3164 if (it->bidi_p)
3165 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3166 FRAME_WINDOW_P (it->f), &it->bidi_it);
3167 }
3168
3169 /* Restore position in display vector translations, control
3170 character translations or ellipses. */
3171 if (pos->dpvec_index >= 0)
3172 {
3173 if (it->dpvec == NULL)
3174 get_next_display_element (it);
3175 eassert (it->dpvec && it->current.dpvec_index == 0);
3176 it->current.dpvec_index = pos->dpvec_index;
3177 }
3178
3179 CHECK_IT (it);
3180 return !overlay_strings_with_newlines;
3181 }
3182
3183
3184 /* Initialize IT for stepping through current_buffer in window W
3185 starting at ROW->start. */
3186
3187 static void
3188 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3189 {
3190 init_from_display_pos (it, w, &row->start);
3191 it->start = row->start;
3192 it->continuation_lines_width = row->continuation_lines_width;
3193 CHECK_IT (it);
3194 }
3195
3196
3197 /* Initialize IT for stepping through current_buffer in window W
3198 starting in the line following ROW, i.e. starting at ROW->end.
3199 Value is zero if there are overlay strings with newlines at ROW's
3200 end position. */
3201
3202 static int
3203 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3204 {
3205 int success = 0;
3206
3207 if (init_from_display_pos (it, w, &row->end))
3208 {
3209 if (row->continued_p)
3210 it->continuation_lines_width
3211 = row->continuation_lines_width + row->pixel_width;
3212 CHECK_IT (it);
3213 success = 1;
3214 }
3215
3216 return success;
3217 }
3218
3219
3220
3221 \f
3222 /***********************************************************************
3223 Text properties
3224 ***********************************************************************/
3225
3226 /* Called when IT reaches IT->stop_charpos. Handle text property and
3227 overlay changes. Set IT->stop_charpos to the next position where
3228 to stop. */
3229
3230 static void
3231 handle_stop (struct it *it)
3232 {
3233 enum prop_handled handled;
3234 int handle_overlay_change_p;
3235 struct props *p;
3236
3237 it->dpvec = NULL;
3238 it->current.dpvec_index = -1;
3239 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3240 it->ignore_overlay_strings_at_pos_p = 0;
3241 it->ellipsis_p = 0;
3242
3243 /* Use face of preceding text for ellipsis (if invisible) */
3244 if (it->selective_display_ellipsis_p)
3245 it->saved_face_id = it->face_id;
3246
3247 do
3248 {
3249 handled = HANDLED_NORMALLY;
3250
3251 /* Call text property handlers. */
3252 for (p = it_props; p->handler; ++p)
3253 {
3254 handled = p->handler (it);
3255
3256 if (handled == HANDLED_RECOMPUTE_PROPS)
3257 break;
3258 else if (handled == HANDLED_RETURN)
3259 {
3260 /* We still want to show before and after strings from
3261 overlays even if the actual buffer text is replaced. */
3262 if (!handle_overlay_change_p
3263 || it->sp > 1
3264 /* Don't call get_overlay_strings_1 if we already
3265 have overlay strings loaded, because doing so
3266 will load them again and push the iterator state
3267 onto the stack one more time, which is not
3268 expected by the rest of the code that processes
3269 overlay strings. */
3270 || (it->current.overlay_string_index < 0
3271 ? !get_overlay_strings_1 (it, 0, 0)
3272 : 0))
3273 {
3274 if (it->ellipsis_p)
3275 setup_for_ellipsis (it, 0);
3276 /* When handling a display spec, we might load an
3277 empty string. In that case, discard it here. We
3278 used to discard it in handle_single_display_spec,
3279 but that causes get_overlay_strings_1, above, to
3280 ignore overlay strings that we must check. */
3281 if (STRINGP (it->string) && !SCHARS (it->string))
3282 pop_it (it);
3283 return;
3284 }
3285 else if (STRINGP (it->string) && !SCHARS (it->string))
3286 pop_it (it);
3287 else
3288 {
3289 it->ignore_overlay_strings_at_pos_p = 1;
3290 it->string_from_display_prop_p = 0;
3291 it->from_disp_prop_p = 0;
3292 handle_overlay_change_p = 0;
3293 }
3294 handled = HANDLED_RECOMPUTE_PROPS;
3295 break;
3296 }
3297 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3298 handle_overlay_change_p = 0;
3299 }
3300
3301 if (handled != HANDLED_RECOMPUTE_PROPS)
3302 {
3303 /* Don't check for overlay strings below when set to deliver
3304 characters from a display vector. */
3305 if (it->method == GET_FROM_DISPLAY_VECTOR)
3306 handle_overlay_change_p = 0;
3307
3308 /* Handle overlay changes.
3309 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3310 if it finds overlays. */
3311 if (handle_overlay_change_p)
3312 handled = handle_overlay_change (it);
3313 }
3314
3315 if (it->ellipsis_p)
3316 {
3317 setup_for_ellipsis (it, 0);
3318 break;
3319 }
3320 }
3321 while (handled == HANDLED_RECOMPUTE_PROPS);
3322
3323 /* Determine where to stop next. */
3324 if (handled == HANDLED_NORMALLY)
3325 compute_stop_pos (it);
3326 }
3327
3328
3329 /* Compute IT->stop_charpos from text property and overlay change
3330 information for IT's current position. */
3331
3332 static void
3333 compute_stop_pos (struct it *it)
3334 {
3335 register INTERVAL iv, next_iv;
3336 Lisp_Object object, limit, position;
3337 ptrdiff_t charpos, bytepos;
3338
3339 if (STRINGP (it->string))
3340 {
3341 /* Strings are usually short, so don't limit the search for
3342 properties. */
3343 it->stop_charpos = it->end_charpos;
3344 object = it->string;
3345 limit = Qnil;
3346 charpos = IT_STRING_CHARPOS (*it);
3347 bytepos = IT_STRING_BYTEPOS (*it);
3348 }
3349 else
3350 {
3351 ptrdiff_t pos;
3352
3353 /* If end_charpos is out of range for some reason, such as a
3354 misbehaving display function, rationalize it (Bug#5984). */
3355 if (it->end_charpos > ZV)
3356 it->end_charpos = ZV;
3357 it->stop_charpos = it->end_charpos;
3358
3359 /* If next overlay change is in front of the current stop pos
3360 (which is IT->end_charpos), stop there. Note: value of
3361 next_overlay_change is point-max if no overlay change
3362 follows. */
3363 charpos = IT_CHARPOS (*it);
3364 bytepos = IT_BYTEPOS (*it);
3365 pos = next_overlay_change (charpos);
3366 if (pos < it->stop_charpos)
3367 it->stop_charpos = pos;
3368
3369 /* If showing the region, we have to stop at the region
3370 start or end because the face might change there. */
3371 if (it->region_beg_charpos > 0)
3372 {
3373 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3374 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3375 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3376 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3377 }
3378
3379 /* Set up variables for computing the stop position from text
3380 property changes. */
3381 XSETBUFFER (object, current_buffer);
3382 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3383 }
3384
3385 /* Get the interval containing IT's position. Value is a null
3386 interval if there isn't such an interval. */
3387 position = make_number (charpos);
3388 iv = validate_interval_range (object, &position, &position, 0);
3389 if (iv)
3390 {
3391 Lisp_Object values_here[LAST_PROP_IDX];
3392 struct props *p;
3393
3394 /* Get properties here. */
3395 for (p = it_props; p->handler; ++p)
3396 values_here[p->idx] = textget (iv->plist, *p->name);
3397
3398 /* Look for an interval following iv that has different
3399 properties. */
3400 for (next_iv = next_interval (iv);
3401 (next_iv
3402 && (NILP (limit)
3403 || XFASTINT (limit) > next_iv->position));
3404 next_iv = next_interval (next_iv))
3405 {
3406 for (p = it_props; p->handler; ++p)
3407 {
3408 Lisp_Object new_value;
3409
3410 new_value = textget (next_iv->plist, *p->name);
3411 if (!EQ (values_here[p->idx], new_value))
3412 break;
3413 }
3414
3415 if (p->handler)
3416 break;
3417 }
3418
3419 if (next_iv)
3420 {
3421 if (INTEGERP (limit)
3422 && next_iv->position >= XFASTINT (limit))
3423 /* No text property change up to limit. */
3424 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3425 else
3426 /* Text properties change in next_iv. */
3427 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3428 }
3429 }
3430
3431 if (it->cmp_it.id < 0)
3432 {
3433 ptrdiff_t stoppos = it->end_charpos;
3434
3435 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3436 stoppos = -1;
3437 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3438 stoppos, it->string);
3439 }
3440
3441 eassert (STRINGP (it->string)
3442 || (it->stop_charpos >= BEGV
3443 && it->stop_charpos >= IT_CHARPOS (*it)));
3444 }
3445
3446
3447 /* Return the position of the next overlay change after POS in
3448 current_buffer. Value is point-max if no overlay change
3449 follows. This is like `next-overlay-change' but doesn't use
3450 xmalloc. */
3451
3452 static ptrdiff_t
3453 next_overlay_change (ptrdiff_t pos)
3454 {
3455 ptrdiff_t i, noverlays;
3456 ptrdiff_t endpos;
3457 Lisp_Object *overlays;
3458
3459 /* Get all overlays at the given position. */
3460 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3461
3462 /* If any of these overlays ends before endpos,
3463 use its ending point instead. */
3464 for (i = 0; i < noverlays; ++i)
3465 {
3466 Lisp_Object oend;
3467 ptrdiff_t oendpos;
3468
3469 oend = OVERLAY_END (overlays[i]);
3470 oendpos = OVERLAY_POSITION (oend);
3471 endpos = min (endpos, oendpos);
3472 }
3473
3474 return endpos;
3475 }
3476
3477 /* How many characters forward to search for a display property or
3478 display string. Searching too far forward makes the bidi display
3479 sluggish, especially in small windows. */
3480 #define MAX_DISP_SCAN 250
3481
3482 /* Return the character position of a display string at or after
3483 position specified by POSITION. If no display string exists at or
3484 after POSITION, return ZV. A display string is either an overlay
3485 with `display' property whose value is a string, or a `display'
3486 text property whose value is a string. STRING is data about the
3487 string to iterate; if STRING->lstring is nil, we are iterating a
3488 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3489 on a GUI frame. DISP_PROP is set to zero if we searched
3490 MAX_DISP_SCAN characters forward without finding any display
3491 strings, non-zero otherwise. It is set to 2 if the display string
3492 uses any kind of `(space ...)' spec that will produce a stretch of
3493 white space in the text area. */
3494 ptrdiff_t
3495 compute_display_string_pos (struct text_pos *position,
3496 struct bidi_string_data *string,
3497 int frame_window_p, int *disp_prop)
3498 {
3499 /* OBJECT = nil means current buffer. */
3500 Lisp_Object object =
3501 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3502 Lisp_Object pos, spec, limpos;
3503 int string_p = (string && (STRINGP (string->lstring) || string->s));
3504 ptrdiff_t eob = string_p ? string->schars : ZV;
3505 ptrdiff_t begb = string_p ? 0 : BEGV;
3506 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3507 ptrdiff_t lim =
3508 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3509 struct text_pos tpos;
3510 int rv = 0;
3511
3512 *disp_prop = 1;
3513
3514 if (charpos >= eob
3515 /* We don't support display properties whose values are strings
3516 that have display string properties. */
3517 || string->from_disp_str
3518 /* C strings cannot have display properties. */
3519 || (string->s && !STRINGP (object)))
3520 {
3521 *disp_prop = 0;
3522 return eob;
3523 }
3524
3525 /* If the character at CHARPOS is where the display string begins,
3526 return CHARPOS. */
3527 pos = make_number (charpos);
3528 if (STRINGP (object))
3529 bufpos = string->bufpos;
3530 else
3531 bufpos = charpos;
3532 tpos = *position;
3533 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3534 && (charpos <= begb
3535 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3536 object),
3537 spec))
3538 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3539 frame_window_p)))
3540 {
3541 if (rv == 2)
3542 *disp_prop = 2;
3543 return charpos;
3544 }
3545
3546 /* Look forward for the first character with a `display' property
3547 that will replace the underlying text when displayed. */
3548 limpos = make_number (lim);
3549 do {
3550 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3551 CHARPOS (tpos) = XFASTINT (pos);
3552 if (CHARPOS (tpos) >= lim)
3553 {
3554 *disp_prop = 0;
3555 break;
3556 }
3557 if (STRINGP (object))
3558 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3559 else
3560 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3561 spec = Fget_char_property (pos, Qdisplay, object);
3562 if (!STRINGP (object))
3563 bufpos = CHARPOS (tpos);
3564 } while (NILP (spec)
3565 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3566 bufpos, frame_window_p)));
3567 if (rv == 2)
3568 *disp_prop = 2;
3569
3570 return CHARPOS (tpos);
3571 }
3572
3573 /* Return the character position of the end of the display string that
3574 started at CHARPOS. If there's no display string at CHARPOS,
3575 return -1. A display string is either an overlay with `display'
3576 property whose value is a string or a `display' text property whose
3577 value is a string. */
3578 ptrdiff_t
3579 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3580 {
3581 /* OBJECT = nil means current buffer. */
3582 Lisp_Object object =
3583 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3584 Lisp_Object pos = make_number (charpos);
3585 ptrdiff_t eob =
3586 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3587
3588 if (charpos >= eob || (string->s && !STRINGP (object)))
3589 return eob;
3590
3591 /* It could happen that the display property or overlay was removed
3592 since we found it in compute_display_string_pos above. One way
3593 this can happen is if JIT font-lock was called (through
3594 handle_fontified_prop), and jit-lock-functions remove text
3595 properties or overlays from the portion of buffer that includes
3596 CHARPOS. Muse mode is known to do that, for example. In this
3597 case, we return -1 to the caller, to signal that no display
3598 string is actually present at CHARPOS. See bidi_fetch_char for
3599 how this is handled.
3600
3601 An alternative would be to never look for display properties past
3602 it->stop_charpos. But neither compute_display_string_pos nor
3603 bidi_fetch_char that calls it know or care where the next
3604 stop_charpos is. */
3605 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3606 return -1;
3607
3608 /* Look forward for the first character where the `display' property
3609 changes. */
3610 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3611
3612 return XFASTINT (pos);
3613 }
3614
3615
3616 \f
3617 /***********************************************************************
3618 Fontification
3619 ***********************************************************************/
3620
3621 /* Handle changes in the `fontified' property of the current buffer by
3622 calling hook functions from Qfontification_functions to fontify
3623 regions of text. */
3624
3625 static enum prop_handled
3626 handle_fontified_prop (struct it *it)
3627 {
3628 Lisp_Object prop, pos;
3629 enum prop_handled handled = HANDLED_NORMALLY;
3630
3631 if (!NILP (Vmemory_full))
3632 return handled;
3633
3634 /* Get the value of the `fontified' property at IT's current buffer
3635 position. (The `fontified' property doesn't have a special
3636 meaning in strings.) If the value is nil, call functions from
3637 Qfontification_functions. */
3638 if (!STRINGP (it->string)
3639 && it->s == NULL
3640 && !NILP (Vfontification_functions)
3641 && !NILP (Vrun_hooks)
3642 && (pos = make_number (IT_CHARPOS (*it)),
3643 prop = Fget_char_property (pos, Qfontified, Qnil),
3644 /* Ignore the special cased nil value always present at EOB since
3645 no amount of fontifying will be able to change it. */
3646 NILP (prop) && IT_CHARPOS (*it) < Z))
3647 {
3648 ptrdiff_t count = SPECPDL_INDEX ();
3649 Lisp_Object val;
3650 struct buffer *obuf = current_buffer;
3651 int begv = BEGV, zv = ZV;
3652 int old_clip_changed = current_buffer->clip_changed;
3653
3654 val = Vfontification_functions;
3655 specbind (Qfontification_functions, Qnil);
3656
3657 eassert (it->end_charpos == ZV);
3658
3659 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3660 safe_call1 (val, pos);
3661 else
3662 {
3663 Lisp_Object fns, fn;
3664 struct gcpro gcpro1, gcpro2;
3665
3666 fns = Qnil;
3667 GCPRO2 (val, fns);
3668
3669 for (; CONSP (val); val = XCDR (val))
3670 {
3671 fn = XCAR (val);
3672
3673 if (EQ (fn, Qt))
3674 {
3675 /* A value of t indicates this hook has a local
3676 binding; it means to run the global binding too.
3677 In a global value, t should not occur. If it
3678 does, we must ignore it to avoid an endless
3679 loop. */
3680 for (fns = Fdefault_value (Qfontification_functions);
3681 CONSP (fns);
3682 fns = XCDR (fns))
3683 {
3684 fn = XCAR (fns);
3685 if (!EQ (fn, Qt))
3686 safe_call1 (fn, pos);
3687 }
3688 }
3689 else
3690 safe_call1 (fn, pos);
3691 }
3692
3693 UNGCPRO;
3694 }
3695
3696 unbind_to (count, Qnil);
3697
3698 /* Fontification functions routinely call `save-restriction'.
3699 Normally, this tags clip_changed, which can confuse redisplay
3700 (see discussion in Bug#6671). Since we don't perform any
3701 special handling of fontification changes in the case where
3702 `save-restriction' isn't called, there's no point doing so in
3703 this case either. So, if the buffer's restrictions are
3704 actually left unchanged, reset clip_changed. */
3705 if (obuf == current_buffer)
3706 {
3707 if (begv == BEGV && zv == ZV)
3708 current_buffer->clip_changed = old_clip_changed;
3709 }
3710 /* There isn't much we can reasonably do to protect against
3711 misbehaving fontification, but here's a fig leaf. */
3712 else if (BUFFER_LIVE_P (obuf))
3713 set_buffer_internal_1 (obuf);
3714
3715 /* The fontification code may have added/removed text.
3716 It could do even a lot worse, but let's at least protect against
3717 the most obvious case where only the text past `pos' gets changed',
3718 as is/was done in grep.el where some escapes sequences are turned
3719 into face properties (bug#7876). */
3720 it->end_charpos = ZV;
3721
3722 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3723 something. This avoids an endless loop if they failed to
3724 fontify the text for which reason ever. */
3725 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3726 handled = HANDLED_RECOMPUTE_PROPS;
3727 }
3728
3729 return handled;
3730 }
3731
3732
3733 \f
3734 /***********************************************************************
3735 Faces
3736 ***********************************************************************/
3737
3738 /* Set up iterator IT from face properties at its current position.
3739 Called from handle_stop. */
3740
3741 static enum prop_handled
3742 handle_face_prop (struct it *it)
3743 {
3744 int new_face_id;
3745 ptrdiff_t next_stop;
3746
3747 if (!STRINGP (it->string))
3748 {
3749 new_face_id
3750 = face_at_buffer_position (it->w,
3751 IT_CHARPOS (*it),
3752 it->region_beg_charpos,
3753 it->region_end_charpos,
3754 &next_stop,
3755 (IT_CHARPOS (*it)
3756 + TEXT_PROP_DISTANCE_LIMIT),
3757 0, it->base_face_id);
3758
3759 /* Is this a start of a run of characters with box face?
3760 Caveat: this can be called for a freshly initialized
3761 iterator; face_id is -1 in this case. We know that the new
3762 face will not change until limit, i.e. if the new face has a
3763 box, all characters up to limit will have one. But, as
3764 usual, we don't know whether limit is really the end. */
3765 if (new_face_id != it->face_id)
3766 {
3767 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3768 /* If it->face_id is -1, old_face below will be NULL, see
3769 the definition of FACE_FROM_ID. This will happen if this
3770 is the initial call that gets the face. */
3771 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3772
3773 /* If the value of face_id of the iterator is -1, we have to
3774 look in front of IT's position and see whether there is a
3775 face there that's different from new_face_id. */
3776 if (!old_face && IT_CHARPOS (*it) > BEG)
3777 {
3778 int prev_face_id = face_before_it_pos (it);
3779
3780 old_face = FACE_FROM_ID (it->f, prev_face_id);
3781 }
3782
3783 /* If the new face has a box, but the old face does not,
3784 this is the start of a run of characters with box face,
3785 i.e. this character has a shadow on the left side. */
3786 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3787 && (old_face == NULL || !old_face->box));
3788 it->face_box_p = new_face->box != FACE_NO_BOX;
3789 }
3790 }
3791 else
3792 {
3793 int base_face_id;
3794 ptrdiff_t bufpos;
3795 int i;
3796 Lisp_Object from_overlay
3797 = (it->current.overlay_string_index >= 0
3798 ? it->string_overlays[it->current.overlay_string_index
3799 % OVERLAY_STRING_CHUNK_SIZE]
3800 : Qnil);
3801
3802 /* See if we got to this string directly or indirectly from
3803 an overlay property. That includes the before-string or
3804 after-string of an overlay, strings in display properties
3805 provided by an overlay, their text properties, etc.
3806
3807 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3808 if (! NILP (from_overlay))
3809 for (i = it->sp - 1; i >= 0; i--)
3810 {
3811 if (it->stack[i].current.overlay_string_index >= 0)
3812 from_overlay
3813 = it->string_overlays[it->stack[i].current.overlay_string_index
3814 % OVERLAY_STRING_CHUNK_SIZE];
3815 else if (! NILP (it->stack[i].from_overlay))
3816 from_overlay = it->stack[i].from_overlay;
3817
3818 if (!NILP (from_overlay))
3819 break;
3820 }
3821
3822 if (! NILP (from_overlay))
3823 {
3824 bufpos = IT_CHARPOS (*it);
3825 /* For a string from an overlay, the base face depends
3826 only on text properties and ignores overlays. */
3827 base_face_id
3828 = face_for_overlay_string (it->w,
3829 IT_CHARPOS (*it),
3830 it->region_beg_charpos,
3831 it->region_end_charpos,
3832 &next_stop,
3833 (IT_CHARPOS (*it)
3834 + TEXT_PROP_DISTANCE_LIMIT),
3835 0,
3836 from_overlay);
3837 }
3838 else
3839 {
3840 bufpos = 0;
3841
3842 /* For strings from a `display' property, use the face at
3843 IT's current buffer position as the base face to merge
3844 with, so that overlay strings appear in the same face as
3845 surrounding text, unless they specify their own
3846 faces. */
3847 base_face_id = it->string_from_prefix_prop_p
3848 ? DEFAULT_FACE_ID
3849 : underlying_face_id (it);
3850 }
3851
3852 new_face_id = face_at_string_position (it->w,
3853 it->string,
3854 IT_STRING_CHARPOS (*it),
3855 bufpos,
3856 it->region_beg_charpos,
3857 it->region_end_charpos,
3858 &next_stop,
3859 base_face_id, 0);
3860
3861 /* Is this a start of a run of characters with box? Caveat:
3862 this can be called for a freshly allocated iterator; face_id
3863 is -1 is this case. We know that the new face will not
3864 change until the next check pos, i.e. if the new face has a
3865 box, all characters up to that position will have a
3866 box. But, as usual, we don't know whether that position
3867 is really the end. */
3868 if (new_face_id != it->face_id)
3869 {
3870 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3871 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3872
3873 /* If new face has a box but old face hasn't, this is the
3874 start of a run of characters with box, i.e. it has a
3875 shadow on the left side. */
3876 it->start_of_box_run_p
3877 = new_face->box && (old_face == NULL || !old_face->box);
3878 it->face_box_p = new_face->box != FACE_NO_BOX;
3879 }
3880 }
3881
3882 it->face_id = new_face_id;
3883 return HANDLED_NORMALLY;
3884 }
3885
3886
3887 /* Return the ID of the face ``underlying'' IT's current position,
3888 which is in a string. If the iterator is associated with a
3889 buffer, return the face at IT's current buffer position.
3890 Otherwise, use the iterator's base_face_id. */
3891
3892 static int
3893 underlying_face_id (struct it *it)
3894 {
3895 int face_id = it->base_face_id, i;
3896
3897 eassert (STRINGP (it->string));
3898
3899 for (i = it->sp - 1; i >= 0; --i)
3900 if (NILP (it->stack[i].string))
3901 face_id = it->stack[i].face_id;
3902
3903 return face_id;
3904 }
3905
3906
3907 /* Compute the face one character before or after the current position
3908 of IT, in the visual order. BEFORE_P non-zero means get the face
3909 in front (to the left in L2R paragraphs, to the right in R2L
3910 paragraphs) of IT's screen position. Value is the ID of the face. */
3911
3912 static int
3913 face_before_or_after_it_pos (struct it *it, int before_p)
3914 {
3915 int face_id, limit;
3916 ptrdiff_t next_check_charpos;
3917 struct it it_copy;
3918 void *it_copy_data = NULL;
3919
3920 eassert (it->s == NULL);
3921
3922 if (STRINGP (it->string))
3923 {
3924 ptrdiff_t bufpos, charpos;
3925 int base_face_id;
3926
3927 /* No face change past the end of the string (for the case
3928 we are padding with spaces). No face change before the
3929 string start. */
3930 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3931 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3932 return it->face_id;
3933
3934 if (!it->bidi_p)
3935 {
3936 /* Set charpos to the position before or after IT's current
3937 position, in the logical order, which in the non-bidi
3938 case is the same as the visual order. */
3939 if (before_p)
3940 charpos = IT_STRING_CHARPOS (*it) - 1;
3941 else if (it->what == IT_COMPOSITION)
3942 /* For composition, we must check the character after the
3943 composition. */
3944 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3945 else
3946 charpos = IT_STRING_CHARPOS (*it) + 1;
3947 }
3948 else
3949 {
3950 if (before_p)
3951 {
3952 /* With bidi iteration, the character before the current
3953 in the visual order cannot be found by simple
3954 iteration, because "reverse" reordering is not
3955 supported. Instead, we need to use the move_it_*
3956 family of functions. */
3957 /* Ignore face changes before the first visible
3958 character on this display line. */
3959 if (it->current_x <= it->first_visible_x)
3960 return it->face_id;
3961 SAVE_IT (it_copy, *it, it_copy_data);
3962 /* Implementation note: Since move_it_in_display_line
3963 works in the iterator geometry, and thinks the first
3964 character is always the leftmost, even in R2L lines,
3965 we don't need to distinguish between the R2L and L2R
3966 cases here. */
3967 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3968 it_copy.current_x - 1, MOVE_TO_X);
3969 charpos = IT_STRING_CHARPOS (it_copy);
3970 RESTORE_IT (it, it, it_copy_data);
3971 }
3972 else
3973 {
3974 /* Set charpos to the string position of the character
3975 that comes after IT's current position in the visual
3976 order. */
3977 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3978
3979 it_copy = *it;
3980 while (n--)
3981 bidi_move_to_visually_next (&it_copy.bidi_it);
3982
3983 charpos = it_copy.bidi_it.charpos;
3984 }
3985 }
3986 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3987
3988 if (it->current.overlay_string_index >= 0)
3989 bufpos = IT_CHARPOS (*it);
3990 else
3991 bufpos = 0;
3992
3993 base_face_id = underlying_face_id (it);
3994
3995 /* Get the face for ASCII, or unibyte. */
3996 face_id = face_at_string_position (it->w,
3997 it->string,
3998 charpos,
3999 bufpos,
4000 it->region_beg_charpos,
4001 it->region_end_charpos,
4002 &next_check_charpos,
4003 base_face_id, 0);
4004
4005 /* Correct the face for charsets different from ASCII. Do it
4006 for the multibyte case only. The face returned above is
4007 suitable for unibyte text if IT->string is unibyte. */
4008 if (STRING_MULTIBYTE (it->string))
4009 {
4010 struct text_pos pos1 = string_pos (charpos, it->string);
4011 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4012 int c, len;
4013 struct face *face = FACE_FROM_ID (it->f, face_id);
4014
4015 c = string_char_and_length (p, &len);
4016 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4017 }
4018 }
4019 else
4020 {
4021 struct text_pos pos;
4022
4023 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4024 || (IT_CHARPOS (*it) <= BEGV && before_p))
4025 return it->face_id;
4026
4027 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4028 pos = it->current.pos;
4029
4030 if (!it->bidi_p)
4031 {
4032 if (before_p)
4033 DEC_TEXT_POS (pos, it->multibyte_p);
4034 else
4035 {
4036 if (it->what == IT_COMPOSITION)
4037 {
4038 /* For composition, we must check the position after
4039 the composition. */
4040 pos.charpos += it->cmp_it.nchars;
4041 pos.bytepos += it->len;
4042 }
4043 else
4044 INC_TEXT_POS (pos, it->multibyte_p);
4045 }
4046 }
4047 else
4048 {
4049 if (before_p)
4050 {
4051 /* With bidi iteration, the character before the current
4052 in the visual order cannot be found by simple
4053 iteration, because "reverse" reordering is not
4054 supported. Instead, we need to use the move_it_*
4055 family of functions. */
4056 /* Ignore face changes before the first visible
4057 character on this display line. */
4058 if (it->current_x <= it->first_visible_x)
4059 return it->face_id;
4060 SAVE_IT (it_copy, *it, it_copy_data);
4061 /* Implementation note: Since move_it_in_display_line
4062 works in the iterator geometry, and thinks the first
4063 character is always the leftmost, even in R2L lines,
4064 we don't need to distinguish between the R2L and L2R
4065 cases here. */
4066 move_it_in_display_line (&it_copy, ZV,
4067 it_copy.current_x - 1, MOVE_TO_X);
4068 pos = it_copy.current.pos;
4069 RESTORE_IT (it, it, it_copy_data);
4070 }
4071 else
4072 {
4073 /* Set charpos to the buffer position of the character
4074 that comes after IT's current position in the visual
4075 order. */
4076 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4077
4078 it_copy = *it;
4079 while (n--)
4080 bidi_move_to_visually_next (&it_copy.bidi_it);
4081
4082 SET_TEXT_POS (pos,
4083 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4084 }
4085 }
4086 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4087
4088 /* Determine face for CHARSET_ASCII, or unibyte. */
4089 face_id = face_at_buffer_position (it->w,
4090 CHARPOS (pos),
4091 it->region_beg_charpos,
4092 it->region_end_charpos,
4093 &next_check_charpos,
4094 limit, 0, -1);
4095
4096 /* Correct the face for charsets different from ASCII. Do it
4097 for the multibyte case only. The face returned above is
4098 suitable for unibyte text if current_buffer is unibyte. */
4099 if (it->multibyte_p)
4100 {
4101 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4102 struct face *face = FACE_FROM_ID (it->f, face_id);
4103 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4104 }
4105 }
4106
4107 return face_id;
4108 }
4109
4110
4111 \f
4112 /***********************************************************************
4113 Invisible text
4114 ***********************************************************************/
4115
4116 /* Set up iterator IT from invisible properties at its current
4117 position. Called from handle_stop. */
4118
4119 static enum prop_handled
4120 handle_invisible_prop (struct it *it)
4121 {
4122 enum prop_handled handled = HANDLED_NORMALLY;
4123 int invis_p;
4124 Lisp_Object prop;
4125
4126 if (STRINGP (it->string))
4127 {
4128 Lisp_Object end_charpos, limit, charpos;
4129
4130 /* Get the value of the invisible text property at the
4131 current position. Value will be nil if there is no such
4132 property. */
4133 charpos = make_number (IT_STRING_CHARPOS (*it));
4134 prop = Fget_text_property (charpos, Qinvisible, it->string);
4135 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136
4137 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4138 {
4139 /* Record whether we have to display an ellipsis for the
4140 invisible text. */
4141 int display_ellipsis_p = (invis_p == 2);
4142 ptrdiff_t len, endpos;
4143
4144 handled = HANDLED_RECOMPUTE_PROPS;
4145
4146 /* Get the position at which the next visible text can be
4147 found in IT->string, if any. */
4148 endpos = len = SCHARS (it->string);
4149 XSETINT (limit, len);
4150 do
4151 {
4152 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4153 it->string, limit);
4154 if (INTEGERP (end_charpos))
4155 {
4156 endpos = XFASTINT (end_charpos);
4157 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4158 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4159 if (invis_p == 2)
4160 display_ellipsis_p = 1;
4161 }
4162 }
4163 while (invis_p && endpos < len);
4164
4165 if (display_ellipsis_p)
4166 it->ellipsis_p = 1;
4167
4168 if (endpos < len)
4169 {
4170 /* Text at END_CHARPOS is visible. Move IT there. */
4171 struct text_pos old;
4172 ptrdiff_t oldpos;
4173
4174 old = it->current.string_pos;
4175 oldpos = CHARPOS (old);
4176 if (it->bidi_p)
4177 {
4178 if (it->bidi_it.first_elt
4179 && it->bidi_it.charpos < SCHARS (it->string))
4180 bidi_paragraph_init (it->paragraph_embedding,
4181 &it->bidi_it, 1);
4182 /* Bidi-iterate out of the invisible text. */
4183 do
4184 {
4185 bidi_move_to_visually_next (&it->bidi_it);
4186 }
4187 while (oldpos <= it->bidi_it.charpos
4188 && it->bidi_it.charpos < endpos);
4189
4190 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4191 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4192 if (IT_CHARPOS (*it) >= endpos)
4193 it->prev_stop = endpos;
4194 }
4195 else
4196 {
4197 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4198 compute_string_pos (&it->current.string_pos, old, it->string);
4199 }
4200 }
4201 else
4202 {
4203 /* The rest of the string is invisible. If this is an
4204 overlay string, proceed with the next overlay string
4205 or whatever comes and return a character from there. */
4206 if (it->current.overlay_string_index >= 0
4207 && !display_ellipsis_p)
4208 {
4209 next_overlay_string (it);
4210 /* Don't check for overlay strings when we just
4211 finished processing them. */
4212 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4213 }
4214 else
4215 {
4216 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4217 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4218 }
4219 }
4220 }
4221 }
4222 else
4223 {
4224 ptrdiff_t newpos, next_stop, start_charpos, tem;
4225 Lisp_Object pos, overlay;
4226
4227 /* First of all, is there invisible text at this position? */
4228 tem = start_charpos = IT_CHARPOS (*it);
4229 pos = make_number (tem);
4230 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4231 &overlay);
4232 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4233
4234 /* If we are on invisible text, skip over it. */
4235 if (invis_p && start_charpos < it->end_charpos)
4236 {
4237 /* Record whether we have to display an ellipsis for the
4238 invisible text. */
4239 int display_ellipsis_p = invis_p == 2;
4240
4241 handled = HANDLED_RECOMPUTE_PROPS;
4242
4243 /* Loop skipping over invisible text. The loop is left at
4244 ZV or with IT on the first char being visible again. */
4245 do
4246 {
4247 /* Try to skip some invisible text. Return value is the
4248 position reached which can be equal to where we start
4249 if there is nothing invisible there. This skips both
4250 over invisible text properties and overlays with
4251 invisible property. */
4252 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4253
4254 /* If we skipped nothing at all we weren't at invisible
4255 text in the first place. If everything to the end of
4256 the buffer was skipped, end the loop. */
4257 if (newpos == tem || newpos >= ZV)
4258 invis_p = 0;
4259 else
4260 {
4261 /* We skipped some characters but not necessarily
4262 all there are. Check if we ended up on visible
4263 text. Fget_char_property returns the property of
4264 the char before the given position, i.e. if we
4265 get invis_p = 0, this means that the char at
4266 newpos is visible. */
4267 pos = make_number (newpos);
4268 prop = Fget_char_property (pos, Qinvisible, it->window);
4269 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 }
4271
4272 /* If we ended up on invisible text, proceed to
4273 skip starting with next_stop. */
4274 if (invis_p)
4275 tem = next_stop;
4276
4277 /* If there are adjacent invisible texts, don't lose the
4278 second one's ellipsis. */
4279 if (invis_p == 2)
4280 display_ellipsis_p = 1;
4281 }
4282 while (invis_p);
4283
4284 /* The position newpos is now either ZV or on visible text. */
4285 if (it->bidi_p)
4286 {
4287 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4288 int on_newline =
4289 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4290 int after_newline =
4291 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4292
4293 /* If the invisible text ends on a newline or on a
4294 character after a newline, we can avoid the costly,
4295 character by character, bidi iteration to NEWPOS, and
4296 instead simply reseat the iterator there. That's
4297 because all bidi reordering information is tossed at
4298 the newline. This is a big win for modes that hide
4299 complete lines, like Outline, Org, etc. */
4300 if (on_newline || after_newline)
4301 {
4302 struct text_pos tpos;
4303 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4304
4305 SET_TEXT_POS (tpos, newpos, bpos);
4306 reseat_1 (it, tpos, 0);
4307 /* If we reseat on a newline/ZV, we need to prep the
4308 bidi iterator for advancing to the next character
4309 after the newline/EOB, keeping the current paragraph
4310 direction (so that PRODUCE_GLYPHS does TRT wrt
4311 prepending/appending glyphs to a glyph row). */
4312 if (on_newline)
4313 {
4314 it->bidi_it.first_elt = 0;
4315 it->bidi_it.paragraph_dir = pdir;
4316 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4317 it->bidi_it.nchars = 1;
4318 it->bidi_it.ch_len = 1;
4319 }
4320 }
4321 else /* Must use the slow method. */
4322 {
4323 /* With bidi iteration, the region of invisible text
4324 could start and/or end in the middle of a
4325 non-base embedding level. Therefore, we need to
4326 skip invisible text using the bidi iterator,
4327 starting at IT's current position, until we find
4328 ourselves outside of the invisible text.
4329 Skipping invisible text _after_ bidi iteration
4330 avoids affecting the visual order of the
4331 displayed text when invisible properties are
4332 added or removed. */
4333 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4334 {
4335 /* If we were `reseat'ed to a new paragraph,
4336 determine the paragraph base direction. We
4337 need to do it now because
4338 next_element_from_buffer may not have a
4339 chance to do it, if we are going to skip any
4340 text at the beginning, which resets the
4341 FIRST_ELT flag. */
4342 bidi_paragraph_init (it->paragraph_embedding,
4343 &it->bidi_it, 1);
4344 }
4345 do
4346 {
4347 bidi_move_to_visually_next (&it->bidi_it);
4348 }
4349 while (it->stop_charpos <= it->bidi_it.charpos
4350 && it->bidi_it.charpos < newpos);
4351 IT_CHARPOS (*it) = it->bidi_it.charpos;
4352 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4353 /* If we overstepped NEWPOS, record its position in
4354 the iterator, so that we skip invisible text if
4355 later the bidi iteration lands us in the
4356 invisible region again. */
4357 if (IT_CHARPOS (*it) >= newpos)
4358 it->prev_stop = newpos;
4359 }
4360 }
4361 else
4362 {
4363 IT_CHARPOS (*it) = newpos;
4364 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4365 }
4366
4367 /* If there are before-strings at the start of invisible
4368 text, and the text is invisible because of a text
4369 property, arrange to show before-strings because 20.x did
4370 it that way. (If the text is invisible because of an
4371 overlay property instead of a text property, this is
4372 already handled in the overlay code.) */
4373 if (NILP (overlay)
4374 && get_overlay_strings (it, it->stop_charpos))
4375 {
4376 handled = HANDLED_RECOMPUTE_PROPS;
4377 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4378 }
4379 else if (display_ellipsis_p)
4380 {
4381 /* Make sure that the glyphs of the ellipsis will get
4382 correct `charpos' values. If we would not update
4383 it->position here, the glyphs would belong to the
4384 last visible character _before_ the invisible
4385 text, which confuses `set_cursor_from_row'.
4386
4387 We use the last invisible position instead of the
4388 first because this way the cursor is always drawn on
4389 the first "." of the ellipsis, whenever PT is inside
4390 the invisible text. Otherwise the cursor would be
4391 placed _after_ the ellipsis when the point is after the
4392 first invisible character. */
4393 if (!STRINGP (it->object))
4394 {
4395 it->position.charpos = newpos - 1;
4396 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4397 }
4398 it->ellipsis_p = 1;
4399 /* Let the ellipsis display before
4400 considering any properties of the following char.
4401 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4402 handled = HANDLED_RETURN;
4403 }
4404 }
4405 }
4406
4407 return handled;
4408 }
4409
4410
4411 /* Make iterator IT return `...' next.
4412 Replaces LEN characters from buffer. */
4413
4414 static void
4415 setup_for_ellipsis (struct it *it, int len)
4416 {
4417 /* Use the display table definition for `...'. Invalid glyphs
4418 will be handled by the method returning elements from dpvec. */
4419 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4420 {
4421 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4422 it->dpvec = v->contents;
4423 it->dpend = v->contents + v->header.size;
4424 }
4425 else
4426 {
4427 /* Default `...'. */
4428 it->dpvec = default_invis_vector;
4429 it->dpend = default_invis_vector + 3;
4430 }
4431
4432 it->dpvec_char_len = len;
4433 it->current.dpvec_index = 0;
4434 it->dpvec_face_id = -1;
4435
4436 /* Remember the current face id in case glyphs specify faces.
4437 IT's face is restored in set_iterator_to_next.
4438 saved_face_id was set to preceding char's face in handle_stop. */
4439 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4440 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4441
4442 it->method = GET_FROM_DISPLAY_VECTOR;
4443 it->ellipsis_p = 1;
4444 }
4445
4446
4447 \f
4448 /***********************************************************************
4449 'display' property
4450 ***********************************************************************/
4451
4452 /* Set up iterator IT from `display' property at its current position.
4453 Called from handle_stop.
4454 We return HANDLED_RETURN if some part of the display property
4455 overrides the display of the buffer text itself.
4456 Otherwise we return HANDLED_NORMALLY. */
4457
4458 static enum prop_handled
4459 handle_display_prop (struct it *it)
4460 {
4461 Lisp_Object propval, object, overlay;
4462 struct text_pos *position;
4463 ptrdiff_t bufpos;
4464 /* Nonzero if some property replaces the display of the text itself. */
4465 int display_replaced_p = 0;
4466
4467 if (STRINGP (it->string))
4468 {
4469 object = it->string;
4470 position = &it->current.string_pos;
4471 bufpos = CHARPOS (it->current.pos);
4472 }
4473 else
4474 {
4475 XSETWINDOW (object, it->w);
4476 position = &it->current.pos;
4477 bufpos = CHARPOS (*position);
4478 }
4479
4480 /* Reset those iterator values set from display property values. */
4481 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4482 it->space_width = Qnil;
4483 it->font_height = Qnil;
4484 it->voffset = 0;
4485
4486 /* We don't support recursive `display' properties, i.e. string
4487 values that have a string `display' property, that have a string
4488 `display' property etc. */
4489 if (!it->string_from_display_prop_p)
4490 it->area = TEXT_AREA;
4491
4492 propval = get_char_property_and_overlay (make_number (position->charpos),
4493 Qdisplay, object, &overlay);
4494 if (NILP (propval))
4495 return HANDLED_NORMALLY;
4496 /* Now OVERLAY is the overlay that gave us this property, or nil
4497 if it was a text property. */
4498
4499 if (!STRINGP (it->string))
4500 object = it->w->buffer;
4501
4502 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4503 position, bufpos,
4504 FRAME_WINDOW_P (it->f));
4505
4506 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4507 }
4508
4509 /* Subroutine of handle_display_prop. Returns non-zero if the display
4510 specification in SPEC is a replacing specification, i.e. it would
4511 replace the text covered by `display' property with something else,
4512 such as an image or a display string. If SPEC includes any kind or
4513 `(space ...) specification, the value is 2; this is used by
4514 compute_display_string_pos, which see.
4515
4516 See handle_single_display_spec for documentation of arguments.
4517 frame_window_p is non-zero if the window being redisplayed is on a
4518 GUI frame; this argument is used only if IT is NULL, see below.
4519
4520 IT can be NULL, if this is called by the bidi reordering code
4521 through compute_display_string_pos, which see. In that case, this
4522 function only examines SPEC, but does not otherwise "handle" it, in
4523 the sense that it doesn't set up members of IT from the display
4524 spec. */
4525 static int
4526 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4527 Lisp_Object overlay, struct text_pos *position,
4528 ptrdiff_t bufpos, int frame_window_p)
4529 {
4530 int replacing_p = 0;
4531 int rv;
4532
4533 if (CONSP (spec)
4534 /* Simple specifications. */
4535 && !EQ (XCAR (spec), Qimage)
4536 && !EQ (XCAR (spec), Qspace)
4537 && !EQ (XCAR (spec), Qwhen)
4538 && !EQ (XCAR (spec), Qslice)
4539 && !EQ (XCAR (spec), Qspace_width)
4540 && !EQ (XCAR (spec), Qheight)
4541 && !EQ (XCAR (spec), Qraise)
4542 /* Marginal area specifications. */
4543 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4544 && !EQ (XCAR (spec), Qleft_fringe)
4545 && !EQ (XCAR (spec), Qright_fringe)
4546 && !NILP (XCAR (spec)))
4547 {
4548 for (; CONSP (spec); spec = XCDR (spec))
4549 {
4550 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4551 overlay, position, bufpos,
4552 replacing_p, frame_window_p)))
4553 {
4554 replacing_p = rv;
4555 /* If some text in a string is replaced, `position' no
4556 longer points to the position of `object'. */
4557 if (!it || STRINGP (object))
4558 break;
4559 }
4560 }
4561 }
4562 else if (VECTORP (spec))
4563 {
4564 ptrdiff_t i;
4565 for (i = 0; i < ASIZE (spec); ++i)
4566 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4567 overlay, position, bufpos,
4568 replacing_p, frame_window_p)))
4569 {
4570 replacing_p = rv;
4571 /* If some text in a string is replaced, `position' no
4572 longer points to the position of `object'. */
4573 if (!it || STRINGP (object))
4574 break;
4575 }
4576 }
4577 else
4578 {
4579 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4580 position, bufpos, 0,
4581 frame_window_p)))
4582 replacing_p = rv;
4583 }
4584
4585 return replacing_p;
4586 }
4587
4588 /* Value is the position of the end of the `display' property starting
4589 at START_POS in OBJECT. */
4590
4591 static struct text_pos
4592 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4593 {
4594 Lisp_Object end;
4595 struct text_pos end_pos;
4596
4597 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4598 Qdisplay, object, Qnil);
4599 CHARPOS (end_pos) = XFASTINT (end);
4600 if (STRINGP (object))
4601 compute_string_pos (&end_pos, start_pos, it->string);
4602 else
4603 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4604
4605 return end_pos;
4606 }
4607
4608
4609 /* Set up IT from a single `display' property specification SPEC. OBJECT
4610 is the object in which the `display' property was found. *POSITION
4611 is the position in OBJECT at which the `display' property was found.
4612 BUFPOS is the buffer position of OBJECT (different from POSITION if
4613 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4614 previously saw a display specification which already replaced text
4615 display with something else, for example an image; we ignore such
4616 properties after the first one has been processed.
4617
4618 OVERLAY is the overlay this `display' property came from,
4619 or nil if it was a text property.
4620
4621 If SPEC is a `space' or `image' specification, and in some other
4622 cases too, set *POSITION to the position where the `display'
4623 property ends.
4624
4625 If IT is NULL, only examine the property specification in SPEC, but
4626 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4627 is intended to be displayed in a window on a GUI frame.
4628
4629 Value is non-zero if something was found which replaces the display
4630 of buffer or string text. */
4631
4632 static int
4633 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4634 Lisp_Object overlay, struct text_pos *position,
4635 ptrdiff_t bufpos, int display_replaced_p,
4636 int frame_window_p)
4637 {
4638 Lisp_Object form;
4639 Lisp_Object location, value;
4640 struct text_pos start_pos = *position;
4641 int valid_p;
4642
4643 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4644 If the result is non-nil, use VALUE instead of SPEC. */
4645 form = Qt;
4646 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4647 {
4648 spec = XCDR (spec);
4649 if (!CONSP (spec))
4650 return 0;
4651 form = XCAR (spec);
4652 spec = XCDR (spec);
4653 }
4654
4655 if (!NILP (form) && !EQ (form, Qt))
4656 {
4657 ptrdiff_t count = SPECPDL_INDEX ();
4658 struct gcpro gcpro1;
4659
4660 /* Bind `object' to the object having the `display' property, a
4661 buffer or string. Bind `position' to the position in the
4662 object where the property was found, and `buffer-position'
4663 to the current position in the buffer. */
4664
4665 if (NILP (object))
4666 XSETBUFFER (object, current_buffer);
4667 specbind (Qobject, object);
4668 specbind (Qposition, make_number (CHARPOS (*position)));
4669 specbind (Qbuffer_position, make_number (bufpos));
4670 GCPRO1 (form);
4671 form = safe_eval (form);
4672 UNGCPRO;
4673 unbind_to (count, Qnil);
4674 }
4675
4676 if (NILP (form))
4677 return 0;
4678
4679 /* Handle `(height HEIGHT)' specifications. */
4680 if (CONSP (spec)
4681 && EQ (XCAR (spec), Qheight)
4682 && CONSP (XCDR (spec)))
4683 {
4684 if (it)
4685 {
4686 if (!FRAME_WINDOW_P (it->f))
4687 return 0;
4688
4689 it->font_height = XCAR (XCDR (spec));
4690 if (!NILP (it->font_height))
4691 {
4692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4693 int new_height = -1;
4694
4695 if (CONSP (it->font_height)
4696 && (EQ (XCAR (it->font_height), Qplus)
4697 || EQ (XCAR (it->font_height), Qminus))
4698 && CONSP (XCDR (it->font_height))
4699 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4700 {
4701 /* `(+ N)' or `(- N)' where N is an integer. */
4702 int steps = XINT (XCAR (XCDR (it->font_height)));
4703 if (EQ (XCAR (it->font_height), Qplus))
4704 steps = - steps;
4705 it->face_id = smaller_face (it->f, it->face_id, steps);
4706 }
4707 else if (FUNCTIONP (it->font_height))
4708 {
4709 /* Call function with current height as argument.
4710 Value is the new height. */
4711 Lisp_Object height;
4712 height = safe_call1 (it->font_height,
4713 face->lface[LFACE_HEIGHT_INDEX]);
4714 if (NUMBERP (height))
4715 new_height = XFLOATINT (height);
4716 }
4717 else if (NUMBERP (it->font_height))
4718 {
4719 /* Value is a multiple of the canonical char height. */
4720 struct face *f;
4721
4722 f = FACE_FROM_ID (it->f,
4723 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4724 new_height = (XFLOATINT (it->font_height)
4725 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4726 }
4727 else
4728 {
4729 /* Evaluate IT->font_height with `height' bound to the
4730 current specified height to get the new height. */
4731 ptrdiff_t count = SPECPDL_INDEX ();
4732
4733 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4734 value = safe_eval (it->font_height);
4735 unbind_to (count, Qnil);
4736
4737 if (NUMBERP (value))
4738 new_height = XFLOATINT (value);
4739 }
4740
4741 if (new_height > 0)
4742 it->face_id = face_with_height (it->f, it->face_id, new_height);
4743 }
4744 }
4745
4746 return 0;
4747 }
4748
4749 /* Handle `(space-width WIDTH)'. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qspace_width)
4752 && CONSP (XCDR (spec)))
4753 {
4754 if (it)
4755 {
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4758
4759 value = XCAR (XCDR (spec));
4760 if (NUMBERP (value) && XFLOATINT (value) > 0)
4761 it->space_width = value;
4762 }
4763
4764 return 0;
4765 }
4766
4767 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4768 if (CONSP (spec)
4769 && EQ (XCAR (spec), Qslice))
4770 {
4771 Lisp_Object tem;
4772
4773 if (it)
4774 {
4775 if (!FRAME_WINDOW_P (it->f))
4776 return 0;
4777
4778 if (tem = XCDR (spec), CONSP (tem))
4779 {
4780 it->slice.x = XCAR (tem);
4781 if (tem = XCDR (tem), CONSP (tem))
4782 {
4783 it->slice.y = XCAR (tem);
4784 if (tem = XCDR (tem), CONSP (tem))
4785 {
4786 it->slice.width = XCAR (tem);
4787 if (tem = XCDR (tem), CONSP (tem))
4788 it->slice.height = XCAR (tem);
4789 }
4790 }
4791 }
4792 }
4793
4794 return 0;
4795 }
4796
4797 /* Handle `(raise FACTOR)'. */
4798 if (CONSP (spec)
4799 && EQ (XCAR (spec), Qraise)
4800 && CONSP (XCDR (spec)))
4801 {
4802 if (it)
4803 {
4804 if (!FRAME_WINDOW_P (it->f))
4805 return 0;
4806
4807 #ifdef HAVE_WINDOW_SYSTEM
4808 value = XCAR (XCDR (spec));
4809 if (NUMBERP (value))
4810 {
4811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4812 it->voffset = - (XFLOATINT (value)
4813 * (FONT_HEIGHT (face->font)));
4814 }
4815 #endif /* HAVE_WINDOW_SYSTEM */
4816 }
4817
4818 return 0;
4819 }
4820
4821 /* Don't handle the other kinds of display specifications
4822 inside a string that we got from a `display' property. */
4823 if (it && it->string_from_display_prop_p)
4824 return 0;
4825
4826 /* Characters having this form of property are not displayed, so
4827 we have to find the end of the property. */
4828 if (it)
4829 {
4830 start_pos = *position;
4831 *position = display_prop_end (it, object, start_pos);
4832 }
4833 value = Qnil;
4834
4835 /* Stop the scan at that end position--we assume that all
4836 text properties change there. */
4837 if (it)
4838 it->stop_charpos = position->charpos;
4839
4840 /* Handle `(left-fringe BITMAP [FACE])'
4841 and `(right-fringe BITMAP [FACE])'. */
4842 if (CONSP (spec)
4843 && (EQ (XCAR (spec), Qleft_fringe)
4844 || EQ (XCAR (spec), Qright_fringe))
4845 && CONSP (XCDR (spec)))
4846 {
4847 int fringe_bitmap;
4848
4849 if (it)
4850 {
4851 if (!FRAME_WINDOW_P (it->f))
4852 /* If we return here, POSITION has been advanced
4853 across the text with this property. */
4854 {
4855 /* Synchronize the bidi iterator with POSITION. This is
4856 needed because we are not going to push the iterator
4857 on behalf of this display property, so there will be
4858 no pop_it call to do this synchronization for us. */
4859 if (it->bidi_p)
4860 {
4861 it->position = *position;
4862 iterate_out_of_display_property (it);
4863 *position = it->position;
4864 }
4865 return 1;
4866 }
4867 }
4868 else if (!frame_window_p)
4869 return 1;
4870
4871 #ifdef HAVE_WINDOW_SYSTEM
4872 value = XCAR (XCDR (spec));
4873 if (!SYMBOLP (value)
4874 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4875 /* If we return here, POSITION has been advanced
4876 across the text with this property. */
4877 {
4878 if (it && it->bidi_p)
4879 {
4880 it->position = *position;
4881 iterate_out_of_display_property (it);
4882 *position = it->position;
4883 }
4884 return 1;
4885 }
4886
4887 if (it)
4888 {
4889 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4890
4891 if (CONSP (XCDR (XCDR (spec))))
4892 {
4893 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4894 int face_id2 = lookup_derived_face (it->f, face_name,
4895 FRINGE_FACE_ID, 0);
4896 if (face_id2 >= 0)
4897 face_id = face_id2;
4898 }
4899
4900 /* Save current settings of IT so that we can restore them
4901 when we are finished with the glyph property value. */
4902 push_it (it, position);
4903
4904 it->area = TEXT_AREA;
4905 it->what = IT_IMAGE;
4906 it->image_id = -1; /* no image */
4907 it->position = start_pos;
4908 it->object = NILP (object) ? it->w->buffer : object;
4909 it->method = GET_FROM_IMAGE;
4910 it->from_overlay = Qnil;
4911 it->face_id = face_id;
4912 it->from_disp_prop_p = 1;
4913
4914 /* Say that we haven't consumed the characters with
4915 `display' property yet. The call to pop_it in
4916 set_iterator_to_next will clean this up. */
4917 *position = start_pos;
4918
4919 if (EQ (XCAR (spec), Qleft_fringe))
4920 {
4921 it->left_user_fringe_bitmap = fringe_bitmap;
4922 it->left_user_fringe_face_id = face_id;
4923 }
4924 else
4925 {
4926 it->right_user_fringe_bitmap = fringe_bitmap;
4927 it->right_user_fringe_face_id = face_id;
4928 }
4929 }
4930 #endif /* HAVE_WINDOW_SYSTEM */
4931 return 1;
4932 }
4933
4934 /* Prepare to handle `((margin left-margin) ...)',
4935 `((margin right-margin) ...)' and `((margin nil) ...)'
4936 prefixes for display specifications. */
4937 location = Qunbound;
4938 if (CONSP (spec) && CONSP (XCAR (spec)))
4939 {
4940 Lisp_Object tem;
4941
4942 value = XCDR (spec);
4943 if (CONSP (value))
4944 value = XCAR (value);
4945
4946 tem = XCAR (spec);
4947 if (EQ (XCAR (tem), Qmargin)
4948 && (tem = XCDR (tem),
4949 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4950 (NILP (tem)
4951 || EQ (tem, Qleft_margin)
4952 || EQ (tem, Qright_margin))))
4953 location = tem;
4954 }
4955
4956 if (EQ (location, Qunbound))
4957 {
4958 location = Qnil;
4959 value = spec;
4960 }
4961
4962 /* After this point, VALUE is the property after any
4963 margin prefix has been stripped. It must be a string,
4964 an image specification, or `(space ...)'.
4965
4966 LOCATION specifies where to display: `left-margin',
4967 `right-margin' or nil. */
4968
4969 valid_p = (STRINGP (value)
4970 #ifdef HAVE_WINDOW_SYSTEM
4971 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4972 && valid_image_p (value))
4973 #endif /* not HAVE_WINDOW_SYSTEM */
4974 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4975
4976 if (valid_p && !display_replaced_p)
4977 {
4978 int retval = 1;
4979
4980 if (!it)
4981 {
4982 /* Callers need to know whether the display spec is any kind
4983 of `(space ...)' spec that is about to affect text-area
4984 display. */
4985 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4986 retval = 2;
4987 return retval;
4988 }
4989
4990 /* Save current settings of IT so that we can restore them
4991 when we are finished with the glyph property value. */
4992 push_it (it, position);
4993 it->from_overlay = overlay;
4994 it->from_disp_prop_p = 1;
4995
4996 if (NILP (location))
4997 it->area = TEXT_AREA;
4998 else if (EQ (location, Qleft_margin))
4999 it->area = LEFT_MARGIN_AREA;
5000 else
5001 it->area = RIGHT_MARGIN_AREA;
5002
5003 if (STRINGP (value))
5004 {
5005 it->string = value;
5006 it->multibyte_p = STRING_MULTIBYTE (it->string);
5007 it->current.overlay_string_index = -1;
5008 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5009 it->end_charpos = it->string_nchars = SCHARS (it->string);
5010 it->method = GET_FROM_STRING;
5011 it->stop_charpos = 0;
5012 it->prev_stop = 0;
5013 it->base_level_stop = 0;
5014 it->string_from_display_prop_p = 1;
5015 /* Say that we haven't consumed the characters with
5016 `display' property yet. The call to pop_it in
5017 set_iterator_to_next will clean this up. */
5018 if (BUFFERP (object))
5019 *position = start_pos;
5020
5021 /* Force paragraph direction to be that of the parent
5022 object. If the parent object's paragraph direction is
5023 not yet determined, default to L2R. */
5024 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5025 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5026 else
5027 it->paragraph_embedding = L2R;
5028
5029 /* Set up the bidi iterator for this display string. */
5030 if (it->bidi_p)
5031 {
5032 it->bidi_it.string.lstring = it->string;
5033 it->bidi_it.string.s = NULL;
5034 it->bidi_it.string.schars = it->end_charpos;
5035 it->bidi_it.string.bufpos = bufpos;
5036 it->bidi_it.string.from_disp_str = 1;
5037 it->bidi_it.string.unibyte = !it->multibyte_p;
5038 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5039 }
5040 }
5041 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5042 {
5043 it->method = GET_FROM_STRETCH;
5044 it->object = value;
5045 *position = it->position = start_pos;
5046 retval = 1 + (it->area == TEXT_AREA);
5047 }
5048 #ifdef HAVE_WINDOW_SYSTEM
5049 else
5050 {
5051 it->what = IT_IMAGE;
5052 it->image_id = lookup_image (it->f, value);
5053 it->position = start_pos;
5054 it->object = NILP (object) ? it->w->buffer : object;
5055 it->method = GET_FROM_IMAGE;
5056
5057 /* Say that we haven't consumed the characters with
5058 `display' property yet. The call to pop_it in
5059 set_iterator_to_next will clean this up. */
5060 *position = start_pos;
5061 }
5062 #endif /* HAVE_WINDOW_SYSTEM */
5063
5064 return retval;
5065 }
5066
5067 /* Invalid property or property not supported. Restore
5068 POSITION to what it was before. */
5069 *position = start_pos;
5070 return 0;
5071 }
5072
5073 /* Check if PROP is a display property value whose text should be
5074 treated as intangible. OVERLAY is the overlay from which PROP
5075 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5076 specify the buffer position covered by PROP. */
5077
5078 int
5079 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5080 ptrdiff_t charpos, ptrdiff_t bytepos)
5081 {
5082 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5083 struct text_pos position;
5084
5085 SET_TEXT_POS (position, charpos, bytepos);
5086 return handle_display_spec (NULL, prop, Qnil, overlay,
5087 &position, charpos, frame_window_p);
5088 }
5089
5090
5091 /* Return 1 if PROP is a display sub-property value containing STRING.
5092
5093 Implementation note: this and the following function are really
5094 special cases of handle_display_spec and
5095 handle_single_display_spec, and should ideally use the same code.
5096 Until they do, these two pairs must be consistent and must be
5097 modified in sync. */
5098
5099 static int
5100 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5101 {
5102 if (EQ (string, prop))
5103 return 1;
5104
5105 /* Skip over `when FORM'. */
5106 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5107 {
5108 prop = XCDR (prop);
5109 if (!CONSP (prop))
5110 return 0;
5111 /* Actually, the condition following `when' should be eval'ed,
5112 like handle_single_display_spec does, and we should return
5113 zero if it evaluates to nil. However, this function is
5114 called only when the buffer was already displayed and some
5115 glyph in the glyph matrix was found to come from a display
5116 string. Therefore, the condition was already evaluated, and
5117 the result was non-nil, otherwise the display string wouldn't
5118 have been displayed and we would have never been called for
5119 this property. Thus, we can skip the evaluation and assume
5120 its result is non-nil. */
5121 prop = XCDR (prop);
5122 }
5123
5124 if (CONSP (prop))
5125 /* Skip over `margin LOCATION'. */
5126 if (EQ (XCAR (prop), Qmargin))
5127 {
5128 prop = XCDR (prop);
5129 if (!CONSP (prop))
5130 return 0;
5131
5132 prop = XCDR (prop);
5133 if (!CONSP (prop))
5134 return 0;
5135 }
5136
5137 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5138 }
5139
5140
5141 /* Return 1 if STRING appears in the `display' property PROP. */
5142
5143 static int
5144 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5145 {
5146 if (CONSP (prop)
5147 && !EQ (XCAR (prop), Qwhen)
5148 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5149 {
5150 /* A list of sub-properties. */
5151 while (CONSP (prop))
5152 {
5153 if (single_display_spec_string_p (XCAR (prop), string))
5154 return 1;
5155 prop = XCDR (prop);
5156 }
5157 }
5158 else if (VECTORP (prop))
5159 {
5160 /* A vector of sub-properties. */
5161 ptrdiff_t i;
5162 for (i = 0; i < ASIZE (prop); ++i)
5163 if (single_display_spec_string_p (AREF (prop, i), string))
5164 return 1;
5165 }
5166 else
5167 return single_display_spec_string_p (prop, string);
5168
5169 return 0;
5170 }
5171
5172 /* Look for STRING in overlays and text properties in the current
5173 buffer, between character positions FROM and TO (excluding TO).
5174 BACK_P non-zero means look back (in this case, TO is supposed to be
5175 less than FROM).
5176 Value is the first character position where STRING was found, or
5177 zero if it wasn't found before hitting TO.
5178
5179 This function may only use code that doesn't eval because it is
5180 called asynchronously from note_mouse_highlight. */
5181
5182 static ptrdiff_t
5183 string_buffer_position_lim (Lisp_Object string,
5184 ptrdiff_t from, ptrdiff_t to, int back_p)
5185 {
5186 Lisp_Object limit, prop, pos;
5187 int found = 0;
5188
5189 pos = make_number (max (from, BEGV));
5190
5191 if (!back_p) /* looking forward */
5192 {
5193 limit = make_number (min (to, ZV));
5194 while (!found && !EQ (pos, limit))
5195 {
5196 prop = Fget_char_property (pos, Qdisplay, Qnil);
5197 if (!NILP (prop) && display_prop_string_p (prop, string))
5198 found = 1;
5199 else
5200 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5201 limit);
5202 }
5203 }
5204 else /* looking back */
5205 {
5206 limit = make_number (max (to, BEGV));
5207 while (!found && !EQ (pos, limit))
5208 {
5209 prop = Fget_char_property (pos, Qdisplay, Qnil);
5210 if (!NILP (prop) && display_prop_string_p (prop, string))
5211 found = 1;
5212 else
5213 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5214 limit);
5215 }
5216 }
5217
5218 return found ? XINT (pos) : 0;
5219 }
5220
5221 /* Determine which buffer position in current buffer STRING comes from.
5222 AROUND_CHARPOS is an approximate position where it could come from.
5223 Value is the buffer position or 0 if it couldn't be determined.
5224
5225 This function is necessary because we don't record buffer positions
5226 in glyphs generated from strings (to keep struct glyph small).
5227 This function may only use code that doesn't eval because it is
5228 called asynchronously from note_mouse_highlight. */
5229
5230 static ptrdiff_t
5231 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5232 {
5233 const int MAX_DISTANCE = 1000;
5234 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5235 around_charpos + MAX_DISTANCE,
5236 0);
5237
5238 if (!found)
5239 found = string_buffer_position_lim (string, around_charpos,
5240 around_charpos - MAX_DISTANCE, 1);
5241 return found;
5242 }
5243
5244
5245 \f
5246 /***********************************************************************
5247 `composition' property
5248 ***********************************************************************/
5249
5250 /* Set up iterator IT from `composition' property at its current
5251 position. Called from handle_stop. */
5252
5253 static enum prop_handled
5254 handle_composition_prop (struct it *it)
5255 {
5256 Lisp_Object prop, string;
5257 ptrdiff_t pos, pos_byte, start, end;
5258
5259 if (STRINGP (it->string))
5260 {
5261 unsigned char *s;
5262
5263 pos = IT_STRING_CHARPOS (*it);
5264 pos_byte = IT_STRING_BYTEPOS (*it);
5265 string = it->string;
5266 s = SDATA (string) + pos_byte;
5267 it->c = STRING_CHAR (s);
5268 }
5269 else
5270 {
5271 pos = IT_CHARPOS (*it);
5272 pos_byte = IT_BYTEPOS (*it);
5273 string = Qnil;
5274 it->c = FETCH_CHAR (pos_byte);
5275 }
5276
5277 /* If there's a valid composition and point is not inside of the
5278 composition (in the case that the composition is from the current
5279 buffer), draw a glyph composed from the composition components. */
5280 if (find_composition (pos, -1, &start, &end, &prop, string)
5281 && COMPOSITION_VALID_P (start, end, prop)
5282 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5283 {
5284 if (start < pos)
5285 /* As we can't handle this situation (perhaps font-lock added
5286 a new composition), we just return here hoping that next
5287 redisplay will detect this composition much earlier. */
5288 return HANDLED_NORMALLY;
5289 if (start != pos)
5290 {
5291 if (STRINGP (it->string))
5292 pos_byte = string_char_to_byte (it->string, start);
5293 else
5294 pos_byte = CHAR_TO_BYTE (start);
5295 }
5296 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5297 prop, string);
5298
5299 if (it->cmp_it.id >= 0)
5300 {
5301 it->cmp_it.ch = -1;
5302 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5303 it->cmp_it.nglyphs = -1;
5304 }
5305 }
5306
5307 return HANDLED_NORMALLY;
5308 }
5309
5310
5311 \f
5312 /***********************************************************************
5313 Overlay strings
5314 ***********************************************************************/
5315
5316 /* The following structure is used to record overlay strings for
5317 later sorting in load_overlay_strings. */
5318
5319 struct overlay_entry
5320 {
5321 Lisp_Object overlay;
5322 Lisp_Object string;
5323 EMACS_INT priority;
5324 int after_string_p;
5325 };
5326
5327
5328 /* Set up iterator IT from overlay strings at its current position.
5329 Called from handle_stop. */
5330
5331 static enum prop_handled
5332 handle_overlay_change (struct it *it)
5333 {
5334 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5335 return HANDLED_RECOMPUTE_PROPS;
5336 else
5337 return HANDLED_NORMALLY;
5338 }
5339
5340
5341 /* Set up the next overlay string for delivery by IT, if there is an
5342 overlay string to deliver. Called by set_iterator_to_next when the
5343 end of the current overlay string is reached. If there are more
5344 overlay strings to display, IT->string and
5345 IT->current.overlay_string_index are set appropriately here.
5346 Otherwise IT->string is set to nil. */
5347
5348 static void
5349 next_overlay_string (struct it *it)
5350 {
5351 ++it->current.overlay_string_index;
5352 if (it->current.overlay_string_index == it->n_overlay_strings)
5353 {
5354 /* No more overlay strings. Restore IT's settings to what
5355 they were before overlay strings were processed, and
5356 continue to deliver from current_buffer. */
5357
5358 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5359 pop_it (it);
5360 eassert (it->sp > 0
5361 || (NILP (it->string)
5362 && it->method == GET_FROM_BUFFER
5363 && it->stop_charpos >= BEGV
5364 && it->stop_charpos <= it->end_charpos));
5365 it->current.overlay_string_index = -1;
5366 it->n_overlay_strings = 0;
5367 it->overlay_strings_charpos = -1;
5368 /* If there's an empty display string on the stack, pop the
5369 stack, to resync the bidi iterator with IT's position. Such
5370 empty strings are pushed onto the stack in
5371 get_overlay_strings_1. */
5372 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5373 pop_it (it);
5374
5375 /* If we're at the end of the buffer, record that we have
5376 processed the overlay strings there already, so that
5377 next_element_from_buffer doesn't try it again. */
5378 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5379 it->overlay_strings_at_end_processed_p = 1;
5380 }
5381 else
5382 {
5383 /* There are more overlay strings to process. If
5384 IT->current.overlay_string_index has advanced to a position
5385 where we must load IT->overlay_strings with more strings, do
5386 it. We must load at the IT->overlay_strings_charpos where
5387 IT->n_overlay_strings was originally computed; when invisible
5388 text is present, this might not be IT_CHARPOS (Bug#7016). */
5389 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5390
5391 if (it->current.overlay_string_index && i == 0)
5392 load_overlay_strings (it, it->overlay_strings_charpos);
5393
5394 /* Initialize IT to deliver display elements from the overlay
5395 string. */
5396 it->string = it->overlay_strings[i];
5397 it->multibyte_p = STRING_MULTIBYTE (it->string);
5398 SET_TEXT_POS (it->current.string_pos, 0, 0);
5399 it->method = GET_FROM_STRING;
5400 it->stop_charpos = 0;
5401 it->end_charpos = SCHARS (it->string);
5402 if (it->cmp_it.stop_pos >= 0)
5403 it->cmp_it.stop_pos = 0;
5404 it->prev_stop = 0;
5405 it->base_level_stop = 0;
5406
5407 /* Set up the bidi iterator for this overlay string. */
5408 if (it->bidi_p)
5409 {
5410 it->bidi_it.string.lstring = it->string;
5411 it->bidi_it.string.s = NULL;
5412 it->bidi_it.string.schars = SCHARS (it->string);
5413 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5414 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5415 it->bidi_it.string.unibyte = !it->multibyte_p;
5416 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5417 }
5418 }
5419
5420 CHECK_IT (it);
5421 }
5422
5423
5424 /* Compare two overlay_entry structures E1 and E2. Used as a
5425 comparison function for qsort in load_overlay_strings. Overlay
5426 strings for the same position are sorted so that
5427
5428 1. All after-strings come in front of before-strings, except
5429 when they come from the same overlay.
5430
5431 2. Within after-strings, strings are sorted so that overlay strings
5432 from overlays with higher priorities come first.
5433
5434 2. Within before-strings, strings are sorted so that overlay
5435 strings from overlays with higher priorities come last.
5436
5437 Value is analogous to strcmp. */
5438
5439
5440 static int
5441 compare_overlay_entries (const void *e1, const void *e2)
5442 {
5443 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5444 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5445 int result;
5446
5447 if (entry1->after_string_p != entry2->after_string_p)
5448 {
5449 /* Let after-strings appear in front of before-strings if
5450 they come from different overlays. */
5451 if (EQ (entry1->overlay, entry2->overlay))
5452 result = entry1->after_string_p ? 1 : -1;
5453 else
5454 result = entry1->after_string_p ? -1 : 1;
5455 }
5456 else if (entry1->priority != entry2->priority)
5457 {
5458 if (entry1->after_string_p)
5459 /* After-strings sorted in order of decreasing priority. */
5460 result = entry2->priority < entry1->priority ? -1 : 1;
5461 else
5462 /* Before-strings sorted in order of increasing priority. */
5463 result = entry1->priority < entry2->priority ? -1 : 1;
5464 }
5465 else
5466 result = 0;
5467
5468 return result;
5469 }
5470
5471
5472 /* Load the vector IT->overlay_strings with overlay strings from IT's
5473 current buffer position, or from CHARPOS if that is > 0. Set
5474 IT->n_overlays to the total number of overlay strings found.
5475
5476 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5477 a time. On entry into load_overlay_strings,
5478 IT->current.overlay_string_index gives the number of overlay
5479 strings that have already been loaded by previous calls to this
5480 function.
5481
5482 IT->add_overlay_start contains an additional overlay start
5483 position to consider for taking overlay strings from, if non-zero.
5484 This position comes into play when the overlay has an `invisible'
5485 property, and both before and after-strings. When we've skipped to
5486 the end of the overlay, because of its `invisible' property, we
5487 nevertheless want its before-string to appear.
5488 IT->add_overlay_start will contain the overlay start position
5489 in this case.
5490
5491 Overlay strings are sorted so that after-string strings come in
5492 front of before-string strings. Within before and after-strings,
5493 strings are sorted by overlay priority. See also function
5494 compare_overlay_entries. */
5495
5496 static void
5497 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5498 {
5499 Lisp_Object overlay, window, str, invisible;
5500 struct Lisp_Overlay *ov;
5501 ptrdiff_t start, end;
5502 ptrdiff_t size = 20;
5503 ptrdiff_t n = 0, i, j;
5504 int invis_p;
5505 struct overlay_entry *entries = alloca (size * sizeof *entries);
5506 USE_SAFE_ALLOCA;
5507
5508 if (charpos <= 0)
5509 charpos = IT_CHARPOS (*it);
5510
5511 /* Append the overlay string STRING of overlay OVERLAY to vector
5512 `entries' which has size `size' and currently contains `n'
5513 elements. AFTER_P non-zero means STRING is an after-string of
5514 OVERLAY. */
5515 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5516 do \
5517 { \
5518 Lisp_Object priority; \
5519 \
5520 if (n == size) \
5521 { \
5522 struct overlay_entry *old = entries; \
5523 SAFE_NALLOCA (entries, 2, size); \
5524 memcpy (entries, old, size * sizeof *entries); \
5525 size *= 2; \
5526 } \
5527 \
5528 entries[n].string = (STRING); \
5529 entries[n].overlay = (OVERLAY); \
5530 priority = Foverlay_get ((OVERLAY), Qpriority); \
5531 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5532 entries[n].after_string_p = (AFTER_P); \
5533 ++n; \
5534 } \
5535 while (0)
5536
5537 /* Process overlay before the overlay center. */
5538 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5539 {
5540 XSETMISC (overlay, ov);
5541 eassert (OVERLAYP (overlay));
5542 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5543 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5544
5545 if (end < charpos)
5546 break;
5547
5548 /* Skip this overlay if it doesn't start or end at IT's current
5549 position. */
5550 if (end != charpos && start != charpos)
5551 continue;
5552
5553 /* Skip this overlay if it doesn't apply to IT->w. */
5554 window = Foverlay_get (overlay, Qwindow);
5555 if (WINDOWP (window) && XWINDOW (window) != it->w)
5556 continue;
5557
5558 /* If the text ``under'' the overlay is invisible, both before-
5559 and after-strings from this overlay are visible; start and
5560 end position are indistinguishable. */
5561 invisible = Foverlay_get (overlay, Qinvisible);
5562 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5563
5564 /* If overlay has a non-empty before-string, record it. */
5565 if ((start == charpos || (end == charpos && invis_p))
5566 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5567 && SCHARS (str))
5568 RECORD_OVERLAY_STRING (overlay, str, 0);
5569
5570 /* If overlay has a non-empty after-string, record it. */
5571 if ((end == charpos || (start == charpos && invis_p))
5572 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5573 && SCHARS (str))
5574 RECORD_OVERLAY_STRING (overlay, str, 1);
5575 }
5576
5577 /* Process overlays after the overlay center. */
5578 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5579 {
5580 XSETMISC (overlay, ov);
5581 eassert (OVERLAYP (overlay));
5582 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5583 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5584
5585 if (start > charpos)
5586 break;
5587
5588 /* Skip this overlay if it doesn't start or end at IT's current
5589 position. */
5590 if (end != charpos && start != charpos)
5591 continue;
5592
5593 /* Skip this overlay if it doesn't apply to IT->w. */
5594 window = Foverlay_get (overlay, Qwindow);
5595 if (WINDOWP (window) && XWINDOW (window) != it->w)
5596 continue;
5597
5598 /* If the text ``under'' the overlay is invisible, it has a zero
5599 dimension, and both before- and after-strings apply. */
5600 invisible = Foverlay_get (overlay, Qinvisible);
5601 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5602
5603 /* If overlay has a non-empty before-string, record it. */
5604 if ((start == charpos || (end == charpos && invis_p))
5605 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5606 && SCHARS (str))
5607 RECORD_OVERLAY_STRING (overlay, str, 0);
5608
5609 /* If overlay has a non-empty after-string, record it. */
5610 if ((end == charpos || (start == charpos && invis_p))
5611 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5612 && SCHARS (str))
5613 RECORD_OVERLAY_STRING (overlay, str, 1);
5614 }
5615
5616 #undef RECORD_OVERLAY_STRING
5617
5618 /* Sort entries. */
5619 if (n > 1)
5620 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5621
5622 /* Record number of overlay strings, and where we computed it. */
5623 it->n_overlay_strings = n;
5624 it->overlay_strings_charpos = charpos;
5625
5626 /* IT->current.overlay_string_index is the number of overlay strings
5627 that have already been consumed by IT. Copy some of the
5628 remaining overlay strings to IT->overlay_strings. */
5629 i = 0;
5630 j = it->current.overlay_string_index;
5631 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5632 {
5633 it->overlay_strings[i] = entries[j].string;
5634 it->string_overlays[i++] = entries[j++].overlay;
5635 }
5636
5637 CHECK_IT (it);
5638 SAFE_FREE ();
5639 }
5640
5641
5642 /* Get the first chunk of overlay strings at IT's current buffer
5643 position, or at CHARPOS if that is > 0. Value is non-zero if at
5644 least one overlay string was found. */
5645
5646 static int
5647 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5648 {
5649 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5650 process. This fills IT->overlay_strings with strings, and sets
5651 IT->n_overlay_strings to the total number of strings to process.
5652 IT->pos.overlay_string_index has to be set temporarily to zero
5653 because load_overlay_strings needs this; it must be set to -1
5654 when no overlay strings are found because a zero value would
5655 indicate a position in the first overlay string. */
5656 it->current.overlay_string_index = 0;
5657 load_overlay_strings (it, charpos);
5658
5659 /* If we found overlay strings, set up IT to deliver display
5660 elements from the first one. Otherwise set up IT to deliver
5661 from current_buffer. */
5662 if (it->n_overlay_strings)
5663 {
5664 /* Make sure we know settings in current_buffer, so that we can
5665 restore meaningful values when we're done with the overlay
5666 strings. */
5667 if (compute_stop_p)
5668 compute_stop_pos (it);
5669 eassert (it->face_id >= 0);
5670
5671 /* Save IT's settings. They are restored after all overlay
5672 strings have been processed. */
5673 eassert (!compute_stop_p || it->sp == 0);
5674
5675 /* When called from handle_stop, there might be an empty display
5676 string loaded. In that case, don't bother saving it. But
5677 don't use this optimization with the bidi iterator, since we
5678 need the corresponding pop_it call to resync the bidi
5679 iterator's position with IT's position, after we are done
5680 with the overlay strings. (The corresponding call to pop_it
5681 in case of an empty display string is in
5682 next_overlay_string.) */
5683 if (!(!it->bidi_p
5684 && STRINGP (it->string) && !SCHARS (it->string)))
5685 push_it (it, NULL);
5686
5687 /* Set up IT to deliver display elements from the first overlay
5688 string. */
5689 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5690 it->string = it->overlay_strings[0];
5691 it->from_overlay = Qnil;
5692 it->stop_charpos = 0;
5693 eassert (STRINGP (it->string));
5694 it->end_charpos = SCHARS (it->string);
5695 it->prev_stop = 0;
5696 it->base_level_stop = 0;
5697 it->multibyte_p = STRING_MULTIBYTE (it->string);
5698 it->method = GET_FROM_STRING;
5699 it->from_disp_prop_p = 0;
5700
5701 /* Force paragraph direction to be that of the parent
5702 buffer. */
5703 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5704 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5705 else
5706 it->paragraph_embedding = L2R;
5707
5708 /* Set up the bidi iterator for this overlay string. */
5709 if (it->bidi_p)
5710 {
5711 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5712
5713 it->bidi_it.string.lstring = it->string;
5714 it->bidi_it.string.s = NULL;
5715 it->bidi_it.string.schars = SCHARS (it->string);
5716 it->bidi_it.string.bufpos = pos;
5717 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5718 it->bidi_it.string.unibyte = !it->multibyte_p;
5719 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5720 }
5721 return 1;
5722 }
5723
5724 it->current.overlay_string_index = -1;
5725 return 0;
5726 }
5727
5728 static int
5729 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5730 {
5731 it->string = Qnil;
5732 it->method = GET_FROM_BUFFER;
5733
5734 (void) get_overlay_strings_1 (it, charpos, 1);
5735
5736 CHECK_IT (it);
5737
5738 /* Value is non-zero if we found at least one overlay string. */
5739 return STRINGP (it->string);
5740 }
5741
5742
5743 \f
5744 /***********************************************************************
5745 Saving and restoring state
5746 ***********************************************************************/
5747
5748 /* Save current settings of IT on IT->stack. Called, for example,
5749 before setting up IT for an overlay string, to be able to restore
5750 IT's settings to what they were after the overlay string has been
5751 processed. If POSITION is non-NULL, it is the position to save on
5752 the stack instead of IT->position. */
5753
5754 static void
5755 push_it (struct it *it, struct text_pos *position)
5756 {
5757 struct iterator_stack_entry *p;
5758
5759 eassert (it->sp < IT_STACK_SIZE);
5760 p = it->stack + it->sp;
5761
5762 p->stop_charpos = it->stop_charpos;
5763 p->prev_stop = it->prev_stop;
5764 p->base_level_stop = it->base_level_stop;
5765 p->cmp_it = it->cmp_it;
5766 eassert (it->face_id >= 0);
5767 p->face_id = it->face_id;
5768 p->string = it->string;
5769 p->method = it->method;
5770 p->from_overlay = it->from_overlay;
5771 switch (p->method)
5772 {
5773 case GET_FROM_IMAGE:
5774 p->u.image.object = it->object;
5775 p->u.image.image_id = it->image_id;
5776 p->u.image.slice = it->slice;
5777 break;
5778 case GET_FROM_STRETCH:
5779 p->u.stretch.object = it->object;
5780 break;
5781 }
5782 p->position = position ? *position : it->position;
5783 p->current = it->current;
5784 p->end_charpos = it->end_charpos;
5785 p->string_nchars = it->string_nchars;
5786 p->area = it->area;
5787 p->multibyte_p = it->multibyte_p;
5788 p->avoid_cursor_p = it->avoid_cursor_p;
5789 p->space_width = it->space_width;
5790 p->font_height = it->font_height;
5791 p->voffset = it->voffset;
5792 p->string_from_display_prop_p = it->string_from_display_prop_p;
5793 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5794 p->display_ellipsis_p = 0;
5795 p->line_wrap = it->line_wrap;
5796 p->bidi_p = it->bidi_p;
5797 p->paragraph_embedding = it->paragraph_embedding;
5798 p->from_disp_prop_p = it->from_disp_prop_p;
5799 ++it->sp;
5800
5801 /* Save the state of the bidi iterator as well. */
5802 if (it->bidi_p)
5803 bidi_push_it (&it->bidi_it);
5804 }
5805
5806 static void
5807 iterate_out_of_display_property (struct it *it)
5808 {
5809 int buffer_p = !STRINGP (it->string);
5810 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5811 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5812
5813 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5814
5815 /* Maybe initialize paragraph direction. If we are at the beginning
5816 of a new paragraph, next_element_from_buffer may not have a
5817 chance to do that. */
5818 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5819 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5820 /* prev_stop can be zero, so check against BEGV as well. */
5821 while (it->bidi_it.charpos >= bob
5822 && it->prev_stop <= it->bidi_it.charpos
5823 && it->bidi_it.charpos < CHARPOS (it->position)
5824 && it->bidi_it.charpos < eob)
5825 bidi_move_to_visually_next (&it->bidi_it);
5826 /* Record the stop_pos we just crossed, for when we cross it
5827 back, maybe. */
5828 if (it->bidi_it.charpos > CHARPOS (it->position))
5829 it->prev_stop = CHARPOS (it->position);
5830 /* If we ended up not where pop_it put us, resync IT's
5831 positional members with the bidi iterator. */
5832 if (it->bidi_it.charpos != CHARPOS (it->position))
5833 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5834 if (buffer_p)
5835 it->current.pos = it->position;
5836 else
5837 it->current.string_pos = it->position;
5838 }
5839
5840 /* Restore IT's settings from IT->stack. Called, for example, when no
5841 more overlay strings must be processed, and we return to delivering
5842 display elements from a buffer, or when the end of a string from a
5843 `display' property is reached and we return to delivering display
5844 elements from an overlay string, or from a buffer. */
5845
5846 static void
5847 pop_it (struct it *it)
5848 {
5849 struct iterator_stack_entry *p;
5850 int from_display_prop = it->from_disp_prop_p;
5851
5852 eassert (it->sp > 0);
5853 --it->sp;
5854 p = it->stack + it->sp;
5855 it->stop_charpos = p->stop_charpos;
5856 it->prev_stop = p->prev_stop;
5857 it->base_level_stop = p->base_level_stop;
5858 it->cmp_it = p->cmp_it;
5859 it->face_id = p->face_id;
5860 it->current = p->current;
5861 it->position = p->position;
5862 it->string = p->string;
5863 it->from_overlay = p->from_overlay;
5864 if (NILP (it->string))
5865 SET_TEXT_POS (it->current.string_pos, -1, -1);
5866 it->method = p->method;
5867 switch (it->method)
5868 {
5869 case GET_FROM_IMAGE:
5870 it->image_id = p->u.image.image_id;
5871 it->object = p->u.image.object;
5872 it->slice = p->u.image.slice;
5873 break;
5874 case GET_FROM_STRETCH:
5875 it->object = p->u.stretch.object;
5876 break;
5877 case GET_FROM_BUFFER:
5878 it->object = it->w->buffer;
5879 break;
5880 case GET_FROM_STRING:
5881 it->object = it->string;
5882 break;
5883 case GET_FROM_DISPLAY_VECTOR:
5884 if (it->s)
5885 it->method = GET_FROM_C_STRING;
5886 else if (STRINGP (it->string))
5887 it->method = GET_FROM_STRING;
5888 else
5889 {
5890 it->method = GET_FROM_BUFFER;
5891 it->object = it->w->buffer;
5892 }
5893 }
5894 it->end_charpos = p->end_charpos;
5895 it->string_nchars = p->string_nchars;
5896 it->area = p->area;
5897 it->multibyte_p = p->multibyte_p;
5898 it->avoid_cursor_p = p->avoid_cursor_p;
5899 it->space_width = p->space_width;
5900 it->font_height = p->font_height;
5901 it->voffset = p->voffset;
5902 it->string_from_display_prop_p = p->string_from_display_prop_p;
5903 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5904 it->line_wrap = p->line_wrap;
5905 it->bidi_p = p->bidi_p;
5906 it->paragraph_embedding = p->paragraph_embedding;
5907 it->from_disp_prop_p = p->from_disp_prop_p;
5908 if (it->bidi_p)
5909 {
5910 bidi_pop_it (&it->bidi_it);
5911 /* Bidi-iterate until we get out of the portion of text, if any,
5912 covered by a `display' text property or by an overlay with
5913 `display' property. (We cannot just jump there, because the
5914 internal coherency of the bidi iterator state can not be
5915 preserved across such jumps.) We also must determine the
5916 paragraph base direction if the overlay we just processed is
5917 at the beginning of a new paragraph. */
5918 if (from_display_prop
5919 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5920 iterate_out_of_display_property (it);
5921
5922 eassert ((BUFFERP (it->object)
5923 && IT_CHARPOS (*it) == it->bidi_it.charpos
5924 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5925 || (STRINGP (it->object)
5926 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5927 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5928 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5929 }
5930 }
5931
5932
5933 \f
5934 /***********************************************************************
5935 Moving over lines
5936 ***********************************************************************/
5937
5938 /* Set IT's current position to the previous line start. */
5939
5940 static void
5941 back_to_previous_line_start (struct it *it)
5942 {
5943 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5945 }
5946
5947
5948 /* Move IT to the next line start.
5949
5950 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5951 we skipped over part of the text (as opposed to moving the iterator
5952 continuously over the text). Otherwise, don't change the value
5953 of *SKIPPED_P.
5954
5955 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5956 iterator on the newline, if it was found.
5957
5958 Newlines may come from buffer text, overlay strings, or strings
5959 displayed via the `display' property. That's the reason we can't
5960 simply use find_next_newline_no_quit.
5961
5962 Note that this function may not skip over invisible text that is so
5963 because of text properties and immediately follows a newline. If
5964 it would, function reseat_at_next_visible_line_start, when called
5965 from set_iterator_to_next, would effectively make invisible
5966 characters following a newline part of the wrong glyph row, which
5967 leads to wrong cursor motion. */
5968
5969 static int
5970 forward_to_next_line_start (struct it *it, int *skipped_p,
5971 struct bidi_it *bidi_it_prev)
5972 {
5973 ptrdiff_t old_selective;
5974 int newline_found_p, n;
5975 const int MAX_NEWLINE_DISTANCE = 500;
5976
5977 /* If already on a newline, just consume it to avoid unintended
5978 skipping over invisible text below. */
5979 if (it->what == IT_CHARACTER
5980 && it->c == '\n'
5981 && CHARPOS (it->position) == IT_CHARPOS (*it))
5982 {
5983 if (it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5986 it->c = 0;
5987 return 1;
5988 }
5989
5990 /* Don't handle selective display in the following. It's (a)
5991 unnecessary because it's done by the caller, and (b) leads to an
5992 infinite recursion because next_element_from_ellipsis indirectly
5993 calls this function. */
5994 old_selective = it->selective;
5995 it->selective = 0;
5996
5997 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5998 from buffer text. */
5999 for (n = newline_found_p = 0;
6000 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6001 n += STRINGP (it->string) ? 0 : 1)
6002 {
6003 if (!get_next_display_element (it))
6004 return 0;
6005 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6006 if (newline_found_p && it->bidi_p && bidi_it_prev)
6007 *bidi_it_prev = it->bidi_it;
6008 set_iterator_to_next (it, 0);
6009 }
6010
6011 /* If we didn't find a newline near enough, see if we can use a
6012 short-cut. */
6013 if (!newline_found_p)
6014 {
6015 ptrdiff_t start = IT_CHARPOS (*it);
6016 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6017 Lisp_Object pos;
6018
6019 eassert (!STRINGP (it->string));
6020
6021 /* If there isn't any `display' property in sight, and no
6022 overlays, we can just use the position of the newline in
6023 buffer text. */
6024 if (it->stop_charpos >= limit
6025 || ((pos = Fnext_single_property_change (make_number (start),
6026 Qdisplay, Qnil,
6027 make_number (limit)),
6028 NILP (pos))
6029 && next_overlay_change (start) == ZV))
6030 {
6031 if (!it->bidi_p)
6032 {
6033 IT_CHARPOS (*it) = limit;
6034 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6035 }
6036 else
6037 {
6038 struct bidi_it bprev;
6039
6040 /* Help bidi.c avoid expensive searches for display
6041 properties and overlays, by telling it that there are
6042 none up to `limit'. */
6043 if (it->bidi_it.disp_pos < limit)
6044 {
6045 it->bidi_it.disp_pos = limit;
6046 it->bidi_it.disp_prop = 0;
6047 }
6048 do {
6049 bprev = it->bidi_it;
6050 bidi_move_to_visually_next (&it->bidi_it);
6051 } while (it->bidi_it.charpos != limit);
6052 IT_CHARPOS (*it) = limit;
6053 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6054 if (bidi_it_prev)
6055 *bidi_it_prev = bprev;
6056 }
6057 *skipped_p = newline_found_p = 1;
6058 }
6059 else
6060 {
6061 while (get_next_display_element (it)
6062 && !newline_found_p)
6063 {
6064 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6065 if (newline_found_p && it->bidi_p && bidi_it_prev)
6066 *bidi_it_prev = it->bidi_it;
6067 set_iterator_to_next (it, 0);
6068 }
6069 }
6070 }
6071
6072 it->selective = old_selective;
6073 return newline_found_p;
6074 }
6075
6076
6077 /* Set IT's current position to the previous visible line start. Skip
6078 invisible text that is so either due to text properties or due to
6079 selective display. Caution: this does not change IT->current_x and
6080 IT->hpos. */
6081
6082 static void
6083 back_to_previous_visible_line_start (struct it *it)
6084 {
6085 while (IT_CHARPOS (*it) > BEGV)
6086 {
6087 back_to_previous_line_start (it);
6088
6089 if (IT_CHARPOS (*it) <= BEGV)
6090 break;
6091
6092 /* If selective > 0, then lines indented more than its value are
6093 invisible. */
6094 if (it->selective > 0
6095 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6096 it->selective))
6097 continue;
6098
6099 /* Check the newline before point for invisibility. */
6100 {
6101 Lisp_Object prop;
6102 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6103 Qinvisible, it->window);
6104 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6105 continue;
6106 }
6107
6108 if (IT_CHARPOS (*it) <= BEGV)
6109 break;
6110
6111 {
6112 struct it it2;
6113 void *it2data = NULL;
6114 ptrdiff_t pos;
6115 ptrdiff_t beg, end;
6116 Lisp_Object val, overlay;
6117
6118 SAVE_IT (it2, *it, it2data);
6119
6120 /* If newline is part of a composition, continue from start of composition */
6121 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6122 && beg < IT_CHARPOS (*it))
6123 goto replaced;
6124
6125 /* If newline is replaced by a display property, find start of overlay
6126 or interval and continue search from that point. */
6127 pos = --IT_CHARPOS (it2);
6128 --IT_BYTEPOS (it2);
6129 it2.sp = 0;
6130 bidi_unshelve_cache (NULL, 0);
6131 it2.string_from_display_prop_p = 0;
6132 it2.from_disp_prop_p = 0;
6133 if (handle_display_prop (&it2) == HANDLED_RETURN
6134 && !NILP (val = get_char_property_and_overlay
6135 (make_number (pos), Qdisplay, Qnil, &overlay))
6136 && (OVERLAYP (overlay)
6137 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6138 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6139 {
6140 RESTORE_IT (it, it, it2data);
6141 goto replaced;
6142 }
6143
6144 /* Newline is not replaced by anything -- so we are done. */
6145 RESTORE_IT (it, it, it2data);
6146 break;
6147
6148 replaced:
6149 if (beg < BEGV)
6150 beg = BEGV;
6151 IT_CHARPOS (*it) = beg;
6152 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6153 }
6154 }
6155
6156 it->continuation_lines_width = 0;
6157
6158 eassert (IT_CHARPOS (*it) >= BEGV);
6159 eassert (IT_CHARPOS (*it) == BEGV
6160 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6161 CHECK_IT (it);
6162 }
6163
6164
6165 /* Reseat iterator IT at the previous visible line start. Skip
6166 invisible text that is so either due to text properties or due to
6167 selective display. At the end, update IT's overlay information,
6168 face information etc. */
6169
6170 void
6171 reseat_at_previous_visible_line_start (struct it *it)
6172 {
6173 back_to_previous_visible_line_start (it);
6174 reseat (it, it->current.pos, 1);
6175 CHECK_IT (it);
6176 }
6177
6178
6179 /* Reseat iterator IT on the next visible line start in the current
6180 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6181 preceding the line start. Skip over invisible text that is so
6182 because of selective display. Compute faces, overlays etc at the
6183 new position. Note that this function does not skip over text that
6184 is invisible because of text properties. */
6185
6186 static void
6187 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6188 {
6189 int newline_found_p, skipped_p = 0;
6190 struct bidi_it bidi_it_prev;
6191
6192 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6193
6194 /* Skip over lines that are invisible because they are indented
6195 more than the value of IT->selective. */
6196 if (it->selective > 0)
6197 while (IT_CHARPOS (*it) < ZV
6198 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6199 it->selective))
6200 {
6201 eassert (IT_BYTEPOS (*it) == BEGV
6202 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6203 newline_found_p =
6204 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6205 }
6206
6207 /* Position on the newline if that's what's requested. */
6208 if (on_newline_p && newline_found_p)
6209 {
6210 if (STRINGP (it->string))
6211 {
6212 if (IT_STRING_CHARPOS (*it) > 0)
6213 {
6214 if (!it->bidi_p)
6215 {
6216 --IT_STRING_CHARPOS (*it);
6217 --IT_STRING_BYTEPOS (*it);
6218 }
6219 else
6220 {
6221 /* We need to restore the bidi iterator to the state
6222 it had on the newline, and resync the IT's
6223 position with that. */
6224 it->bidi_it = bidi_it_prev;
6225 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6226 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6227 }
6228 }
6229 }
6230 else if (IT_CHARPOS (*it) > BEGV)
6231 {
6232 if (!it->bidi_p)
6233 {
6234 --IT_CHARPOS (*it);
6235 --IT_BYTEPOS (*it);
6236 }
6237 else
6238 {
6239 /* We need to restore the bidi iterator to the state it
6240 had on the newline and resync IT with that. */
6241 it->bidi_it = bidi_it_prev;
6242 IT_CHARPOS (*it) = it->bidi_it.charpos;
6243 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 }
6245 reseat (it, it->current.pos, 0);
6246 }
6247 }
6248 else if (skipped_p)
6249 reseat (it, it->current.pos, 0);
6250
6251 CHECK_IT (it);
6252 }
6253
6254
6255 \f
6256 /***********************************************************************
6257 Changing an iterator's position
6258 ***********************************************************************/
6259
6260 /* Change IT's current position to POS in current_buffer. If FORCE_P
6261 is non-zero, always check for text properties at the new position.
6262 Otherwise, text properties are only looked up if POS >=
6263 IT->check_charpos of a property. */
6264
6265 static void
6266 reseat (struct it *it, struct text_pos pos, int force_p)
6267 {
6268 ptrdiff_t original_pos = IT_CHARPOS (*it);
6269
6270 reseat_1 (it, pos, 0);
6271
6272 /* Determine where to check text properties. Avoid doing it
6273 where possible because text property lookup is very expensive. */
6274 if (force_p
6275 || CHARPOS (pos) > it->stop_charpos
6276 || CHARPOS (pos) < original_pos)
6277 {
6278 if (it->bidi_p)
6279 {
6280 /* For bidi iteration, we need to prime prev_stop and
6281 base_level_stop with our best estimations. */
6282 /* Implementation note: Of course, POS is not necessarily a
6283 stop position, so assigning prev_pos to it is a lie; we
6284 should have called compute_stop_backwards. However, if
6285 the current buffer does not include any R2L characters,
6286 that call would be a waste of cycles, because the
6287 iterator will never move back, and thus never cross this
6288 "fake" stop position. So we delay that backward search
6289 until the time we really need it, in next_element_from_buffer. */
6290 if (CHARPOS (pos) != it->prev_stop)
6291 it->prev_stop = CHARPOS (pos);
6292 if (CHARPOS (pos) < it->base_level_stop)
6293 it->base_level_stop = 0; /* meaning it's unknown */
6294 handle_stop (it);
6295 }
6296 else
6297 {
6298 handle_stop (it);
6299 it->prev_stop = it->base_level_stop = 0;
6300 }
6301
6302 }
6303
6304 CHECK_IT (it);
6305 }
6306
6307
6308 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6309 IT->stop_pos to POS, also. */
6310
6311 static void
6312 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6313 {
6314 /* Don't call this function when scanning a C string. */
6315 eassert (it->s == NULL);
6316
6317 /* POS must be a reasonable value. */
6318 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6319
6320 it->current.pos = it->position = pos;
6321 it->end_charpos = ZV;
6322 it->dpvec = NULL;
6323 it->current.dpvec_index = -1;
6324 it->current.overlay_string_index = -1;
6325 IT_STRING_CHARPOS (*it) = -1;
6326 IT_STRING_BYTEPOS (*it) = -1;
6327 it->string = Qnil;
6328 it->method = GET_FROM_BUFFER;
6329 it->object = it->w->buffer;
6330 it->area = TEXT_AREA;
6331 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6332 it->sp = 0;
6333 it->string_from_display_prop_p = 0;
6334 it->string_from_prefix_prop_p = 0;
6335
6336 it->from_disp_prop_p = 0;
6337 it->face_before_selective_p = 0;
6338 if (it->bidi_p)
6339 {
6340 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6341 &it->bidi_it);
6342 bidi_unshelve_cache (NULL, 0);
6343 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6344 it->bidi_it.string.s = NULL;
6345 it->bidi_it.string.lstring = Qnil;
6346 it->bidi_it.string.bufpos = 0;
6347 it->bidi_it.string.unibyte = 0;
6348 }
6349
6350 if (set_stop_p)
6351 {
6352 it->stop_charpos = CHARPOS (pos);
6353 it->base_level_stop = CHARPOS (pos);
6354 }
6355 /* This make the information stored in it->cmp_it invalidate. */
6356 it->cmp_it.id = -1;
6357 }
6358
6359
6360 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6361 If S is non-null, it is a C string to iterate over. Otherwise,
6362 STRING gives a Lisp string to iterate over.
6363
6364 If PRECISION > 0, don't return more then PRECISION number of
6365 characters from the string.
6366
6367 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH < 0 means an infinite
6369 field width.
6370
6371 MULTIBYTE = 0 means disable processing of multibyte characters,
6372 MULTIBYTE > 0 means enable it,
6373 MULTIBYTE < 0 means use IT->multibyte_p.
6374
6375 IT must be initialized via a prior call to init_iterator before
6376 calling this function. */
6377
6378 static void
6379 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6380 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6381 int multibyte)
6382 {
6383 /* No region in strings. */
6384 it->region_beg_charpos = it->region_end_charpos = -1;
6385
6386 /* No text property checks performed by default, but see below. */
6387 it->stop_charpos = -1;
6388
6389 /* Set iterator position and end position. */
6390 memset (&it->current, 0, sizeof it->current);
6391 it->current.overlay_string_index = -1;
6392 it->current.dpvec_index = -1;
6393 eassert (charpos >= 0);
6394
6395 /* If STRING is specified, use its multibyteness, otherwise use the
6396 setting of MULTIBYTE, if specified. */
6397 if (multibyte >= 0)
6398 it->multibyte_p = multibyte > 0;
6399
6400 /* Bidirectional reordering of strings is controlled by the default
6401 value of bidi-display-reordering. Don't try to reorder while
6402 loading loadup.el, as the necessary character property tables are
6403 not yet available. */
6404 it->bidi_p =
6405 NILP (Vpurify_flag)
6406 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6407
6408 if (s == NULL)
6409 {
6410 eassert (STRINGP (string));
6411 it->string = string;
6412 it->s = NULL;
6413 it->end_charpos = it->string_nchars = SCHARS (string);
6414 it->method = GET_FROM_STRING;
6415 it->current.string_pos = string_pos (charpos, string);
6416
6417 if (it->bidi_p)
6418 {
6419 it->bidi_it.string.lstring = string;
6420 it->bidi_it.string.s = NULL;
6421 it->bidi_it.string.schars = it->end_charpos;
6422 it->bidi_it.string.bufpos = 0;
6423 it->bidi_it.string.from_disp_str = 0;
6424 it->bidi_it.string.unibyte = !it->multibyte_p;
6425 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6426 FRAME_WINDOW_P (it->f), &it->bidi_it);
6427 }
6428 }
6429 else
6430 {
6431 it->s = (const unsigned char *) s;
6432 it->string = Qnil;
6433
6434 /* Note that we use IT->current.pos, not it->current.string_pos,
6435 for displaying C strings. */
6436 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6437 if (it->multibyte_p)
6438 {
6439 it->current.pos = c_string_pos (charpos, s, 1);
6440 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6441 }
6442 else
6443 {
6444 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6445 it->end_charpos = it->string_nchars = strlen (s);
6446 }
6447
6448 if (it->bidi_p)
6449 {
6450 it->bidi_it.string.lstring = Qnil;
6451 it->bidi_it.string.s = (const unsigned char *) s;
6452 it->bidi_it.string.schars = it->end_charpos;
6453 it->bidi_it.string.bufpos = 0;
6454 it->bidi_it.string.from_disp_str = 0;
6455 it->bidi_it.string.unibyte = !it->multibyte_p;
6456 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6457 &it->bidi_it);
6458 }
6459 it->method = GET_FROM_C_STRING;
6460 }
6461
6462 /* PRECISION > 0 means don't return more than PRECISION characters
6463 from the string. */
6464 if (precision > 0 && it->end_charpos - charpos > precision)
6465 {
6466 it->end_charpos = it->string_nchars = charpos + precision;
6467 if (it->bidi_p)
6468 it->bidi_it.string.schars = it->end_charpos;
6469 }
6470
6471 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6472 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6473 FIELD_WIDTH < 0 means infinite field width. This is useful for
6474 padding with `-' at the end of a mode line. */
6475 if (field_width < 0)
6476 field_width = INFINITY;
6477 /* Implementation note: We deliberately don't enlarge
6478 it->bidi_it.string.schars here to fit it->end_charpos, because
6479 the bidi iterator cannot produce characters out of thin air. */
6480 if (field_width > it->end_charpos - charpos)
6481 it->end_charpos = charpos + field_width;
6482
6483 /* Use the standard display table for displaying strings. */
6484 if (DISP_TABLE_P (Vstandard_display_table))
6485 it->dp = XCHAR_TABLE (Vstandard_display_table);
6486
6487 it->stop_charpos = charpos;
6488 it->prev_stop = charpos;
6489 it->base_level_stop = 0;
6490 if (it->bidi_p)
6491 {
6492 it->bidi_it.first_elt = 1;
6493 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6494 it->bidi_it.disp_pos = -1;
6495 }
6496 if (s == NULL && it->multibyte_p)
6497 {
6498 ptrdiff_t endpos = SCHARS (it->string);
6499 if (endpos > it->end_charpos)
6500 endpos = it->end_charpos;
6501 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6502 it->string);
6503 }
6504 CHECK_IT (it);
6505 }
6506
6507
6508 \f
6509 /***********************************************************************
6510 Iteration
6511 ***********************************************************************/
6512
6513 /* Map enum it_method value to corresponding next_element_from_* function. */
6514
6515 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6516 {
6517 next_element_from_buffer,
6518 next_element_from_display_vector,
6519 next_element_from_string,
6520 next_element_from_c_string,
6521 next_element_from_image,
6522 next_element_from_stretch
6523 };
6524
6525 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6526
6527
6528 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6529 (possibly with the following characters). */
6530
6531 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6532 ((IT)->cmp_it.id >= 0 \
6533 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6534 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6535 END_CHARPOS, (IT)->w, \
6536 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6537 (IT)->string)))
6538
6539
6540 /* Lookup the char-table Vglyphless_char_display for character C (-1
6541 if we want information for no-font case), and return the display
6542 method symbol. By side-effect, update it->what and
6543 it->glyphless_method. This function is called from
6544 get_next_display_element for each character element, and from
6545 x_produce_glyphs when no suitable font was found. */
6546
6547 Lisp_Object
6548 lookup_glyphless_char_display (int c, struct it *it)
6549 {
6550 Lisp_Object glyphless_method = Qnil;
6551
6552 if (CHAR_TABLE_P (Vglyphless_char_display)
6553 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6554 {
6555 if (c >= 0)
6556 {
6557 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6558 if (CONSP (glyphless_method))
6559 glyphless_method = FRAME_WINDOW_P (it->f)
6560 ? XCAR (glyphless_method)
6561 : XCDR (glyphless_method);
6562 }
6563 else
6564 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6565 }
6566
6567 retry:
6568 if (NILP (glyphless_method))
6569 {
6570 if (c >= 0)
6571 /* The default is to display the character by a proper font. */
6572 return Qnil;
6573 /* The default for the no-font case is to display an empty box. */
6574 glyphless_method = Qempty_box;
6575 }
6576 if (EQ (glyphless_method, Qzero_width))
6577 {
6578 if (c >= 0)
6579 return glyphless_method;
6580 /* This method can't be used for the no-font case. */
6581 glyphless_method = Qempty_box;
6582 }
6583 if (EQ (glyphless_method, Qthin_space))
6584 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6585 else if (EQ (glyphless_method, Qempty_box))
6586 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6587 else if (EQ (glyphless_method, Qhex_code))
6588 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6589 else if (STRINGP (glyphless_method))
6590 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6591 else
6592 {
6593 /* Invalid value. We use the default method. */
6594 glyphless_method = Qnil;
6595 goto retry;
6596 }
6597 it->what = IT_GLYPHLESS;
6598 return glyphless_method;
6599 }
6600
6601 /* Load IT's display element fields with information about the next
6602 display element from the current position of IT. Value is zero if
6603 end of buffer (or C string) is reached. */
6604
6605 static struct frame *last_escape_glyph_frame = NULL;
6606 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6607 static int last_escape_glyph_merged_face_id = 0;
6608
6609 struct frame *last_glyphless_glyph_frame = NULL;
6610 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6611 int last_glyphless_glyph_merged_face_id = 0;
6612
6613 static int
6614 get_next_display_element (struct it *it)
6615 {
6616 /* Non-zero means that we found a display element. Zero means that
6617 we hit the end of what we iterate over. Performance note: the
6618 function pointer `method' used here turns out to be faster than
6619 using a sequence of if-statements. */
6620 int success_p;
6621
6622 get_next:
6623 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6624
6625 if (it->what == IT_CHARACTER)
6626 {
6627 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6628 and only if (a) the resolved directionality of that character
6629 is R..." */
6630 /* FIXME: Do we need an exception for characters from display
6631 tables? */
6632 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6633 it->c = bidi_mirror_char (it->c);
6634 /* Map via display table or translate control characters.
6635 IT->c, IT->len etc. have been set to the next character by
6636 the function call above. If we have a display table, and it
6637 contains an entry for IT->c, translate it. Don't do this if
6638 IT->c itself comes from a display table, otherwise we could
6639 end up in an infinite recursion. (An alternative could be to
6640 count the recursion depth of this function and signal an
6641 error when a certain maximum depth is reached.) Is it worth
6642 it? */
6643 if (success_p && it->dpvec == NULL)
6644 {
6645 Lisp_Object dv;
6646 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6647 int nonascii_space_p = 0;
6648 int nonascii_hyphen_p = 0;
6649 int c = it->c; /* This is the character to display. */
6650
6651 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6652 {
6653 eassert (SINGLE_BYTE_CHAR_P (c));
6654 if (unibyte_display_via_language_environment)
6655 {
6656 c = DECODE_CHAR (unibyte, c);
6657 if (c < 0)
6658 c = BYTE8_TO_CHAR (it->c);
6659 }
6660 else
6661 c = BYTE8_TO_CHAR (it->c);
6662 }
6663
6664 if (it->dp
6665 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6666 VECTORP (dv)))
6667 {
6668 struct Lisp_Vector *v = XVECTOR (dv);
6669
6670 /* Return the first character from the display table
6671 entry, if not empty. If empty, don't display the
6672 current character. */
6673 if (v->header.size)
6674 {
6675 it->dpvec_char_len = it->len;
6676 it->dpvec = v->contents;
6677 it->dpend = v->contents + v->header.size;
6678 it->current.dpvec_index = 0;
6679 it->dpvec_face_id = -1;
6680 it->saved_face_id = it->face_id;
6681 it->method = GET_FROM_DISPLAY_VECTOR;
6682 it->ellipsis_p = 0;
6683 }
6684 else
6685 {
6686 set_iterator_to_next (it, 0);
6687 }
6688 goto get_next;
6689 }
6690
6691 if (! NILP (lookup_glyphless_char_display (c, it)))
6692 {
6693 if (it->what == IT_GLYPHLESS)
6694 goto done;
6695 /* Don't display this character. */
6696 set_iterator_to_next (it, 0);
6697 goto get_next;
6698 }
6699
6700 /* If `nobreak-char-display' is non-nil, we display
6701 non-ASCII spaces and hyphens specially. */
6702 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6703 {
6704 if (c == 0xA0)
6705 nonascii_space_p = 1;
6706 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6707 nonascii_hyphen_p = 1;
6708 }
6709
6710 /* Translate control characters into `\003' or `^C' form.
6711 Control characters coming from a display table entry are
6712 currently not translated because we use IT->dpvec to hold
6713 the translation. This could easily be changed but I
6714 don't believe that it is worth doing.
6715
6716 The characters handled by `nobreak-char-display' must be
6717 translated too.
6718
6719 Non-printable characters and raw-byte characters are also
6720 translated to octal form. */
6721 if (((c < ' ' || c == 127) /* ASCII control chars */
6722 ? (it->area != TEXT_AREA
6723 /* In mode line, treat \n, \t like other crl chars. */
6724 || (c != '\t'
6725 && it->glyph_row
6726 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6727 || (c != '\n' && c != '\t'))
6728 : (nonascii_space_p
6729 || nonascii_hyphen_p
6730 || CHAR_BYTE8_P (c)
6731 || ! CHAR_PRINTABLE_P (c))))
6732 {
6733 /* C is a control character, non-ASCII space/hyphen,
6734 raw-byte, or a non-printable character which must be
6735 displayed either as '\003' or as `^C' where the '\\'
6736 and '^' can be defined in the display table. Fill
6737 IT->ctl_chars with glyphs for what we have to
6738 display. Then, set IT->dpvec to these glyphs. */
6739 Lisp_Object gc;
6740 int ctl_len;
6741 int face_id;
6742 int lface_id = 0;
6743 int escape_glyph;
6744
6745 /* Handle control characters with ^. */
6746
6747 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6748 {
6749 int g;
6750
6751 g = '^'; /* default glyph for Control */
6752 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6753 if (it->dp
6754 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6755 {
6756 g = GLYPH_CODE_CHAR (gc);
6757 lface_id = GLYPH_CODE_FACE (gc);
6758 }
6759 if (lface_id)
6760 {
6761 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6762 }
6763 else if (it->f == last_escape_glyph_frame
6764 && it->face_id == last_escape_glyph_face_id)
6765 {
6766 face_id = last_escape_glyph_merged_face_id;
6767 }
6768 else
6769 {
6770 /* Merge the escape-glyph face into the current face. */
6771 face_id = merge_faces (it->f, Qescape_glyph, 0,
6772 it->face_id);
6773 last_escape_glyph_frame = it->f;
6774 last_escape_glyph_face_id = it->face_id;
6775 last_escape_glyph_merged_face_id = face_id;
6776 }
6777
6778 XSETINT (it->ctl_chars[0], g);
6779 XSETINT (it->ctl_chars[1], c ^ 0100);
6780 ctl_len = 2;
6781 goto display_control;
6782 }
6783
6784 /* Handle non-ascii space in the mode where it only gets
6785 highlighting. */
6786
6787 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6788 {
6789 /* Merge `nobreak-space' into the current face. */
6790 face_id = merge_faces (it->f, Qnobreak_space, 0,
6791 it->face_id);
6792 XSETINT (it->ctl_chars[0], ' ');
6793 ctl_len = 1;
6794 goto display_control;
6795 }
6796
6797 /* Handle sequences that start with the "escape glyph". */
6798
6799 /* the default escape glyph is \. */
6800 escape_glyph = '\\';
6801
6802 if (it->dp
6803 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6804 {
6805 escape_glyph = GLYPH_CODE_CHAR (gc);
6806 lface_id = GLYPH_CODE_FACE (gc);
6807 }
6808 if (lface_id)
6809 {
6810 /* The display table specified a face.
6811 Merge it into face_id and also into escape_glyph. */
6812 face_id = merge_faces (it->f, Qt, lface_id,
6813 it->face_id);
6814 }
6815 else if (it->f == last_escape_glyph_frame
6816 && it->face_id == last_escape_glyph_face_id)
6817 {
6818 face_id = last_escape_glyph_merged_face_id;
6819 }
6820 else
6821 {
6822 /* Merge the escape-glyph face into the current face. */
6823 face_id = merge_faces (it->f, Qescape_glyph, 0,
6824 it->face_id);
6825 last_escape_glyph_frame = it->f;
6826 last_escape_glyph_face_id = it->face_id;
6827 last_escape_glyph_merged_face_id = face_id;
6828 }
6829
6830 /* Draw non-ASCII hyphen with just highlighting: */
6831
6832 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6833 {
6834 XSETINT (it->ctl_chars[0], '-');
6835 ctl_len = 1;
6836 goto display_control;
6837 }
6838
6839 /* Draw non-ASCII space/hyphen with escape glyph: */
6840
6841 if (nonascii_space_p || nonascii_hyphen_p)
6842 {
6843 XSETINT (it->ctl_chars[0], escape_glyph);
6844 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6845 ctl_len = 2;
6846 goto display_control;
6847 }
6848
6849 {
6850 char str[10];
6851 int len, i;
6852
6853 if (CHAR_BYTE8_P (c))
6854 /* Display \200 instead of \17777600. */
6855 c = CHAR_TO_BYTE8 (c);
6856 len = sprintf (str, "%03o", c);
6857
6858 XSETINT (it->ctl_chars[0], escape_glyph);
6859 for (i = 0; i < len; i++)
6860 XSETINT (it->ctl_chars[i + 1], str[i]);
6861 ctl_len = len + 1;
6862 }
6863
6864 display_control:
6865 /* Set up IT->dpvec and return first character from it. */
6866 it->dpvec_char_len = it->len;
6867 it->dpvec = it->ctl_chars;
6868 it->dpend = it->dpvec + ctl_len;
6869 it->current.dpvec_index = 0;
6870 it->dpvec_face_id = face_id;
6871 it->saved_face_id = it->face_id;
6872 it->method = GET_FROM_DISPLAY_VECTOR;
6873 it->ellipsis_p = 0;
6874 goto get_next;
6875 }
6876 it->char_to_display = c;
6877 }
6878 else if (success_p)
6879 {
6880 it->char_to_display = it->c;
6881 }
6882 }
6883
6884 /* Adjust face id for a multibyte character. There are no multibyte
6885 character in unibyte text. */
6886 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6887 && it->multibyte_p
6888 && success_p
6889 && FRAME_WINDOW_P (it->f))
6890 {
6891 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6892
6893 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6894 {
6895 /* Automatic composition with glyph-string. */
6896 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6897
6898 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6899 }
6900 else
6901 {
6902 ptrdiff_t pos = (it->s ? -1
6903 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6904 : IT_CHARPOS (*it));
6905 int c;
6906
6907 if (it->what == IT_CHARACTER)
6908 c = it->char_to_display;
6909 else
6910 {
6911 struct composition *cmp = composition_table[it->cmp_it.id];
6912 int i;
6913
6914 c = ' ';
6915 for (i = 0; i < cmp->glyph_len; i++)
6916 /* TAB in a composition means display glyphs with
6917 padding space on the left or right. */
6918 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6919 break;
6920 }
6921 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6922 }
6923 }
6924
6925 done:
6926 /* Is this character the last one of a run of characters with
6927 box? If yes, set IT->end_of_box_run_p to 1. */
6928 if (it->face_box_p
6929 && it->s == NULL)
6930 {
6931 if (it->method == GET_FROM_STRING && it->sp)
6932 {
6933 int face_id = underlying_face_id (it);
6934 struct face *face = FACE_FROM_ID (it->f, face_id);
6935
6936 if (face)
6937 {
6938 if (face->box == FACE_NO_BOX)
6939 {
6940 /* If the box comes from face properties in a
6941 display string, check faces in that string. */
6942 int string_face_id = face_after_it_pos (it);
6943 it->end_of_box_run_p
6944 = (FACE_FROM_ID (it->f, string_face_id)->box
6945 == FACE_NO_BOX);
6946 }
6947 /* Otherwise, the box comes from the underlying face.
6948 If this is the last string character displayed, check
6949 the next buffer location. */
6950 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6951 && (it->current.overlay_string_index
6952 == it->n_overlay_strings - 1))
6953 {
6954 ptrdiff_t ignore;
6955 int next_face_id;
6956 struct text_pos pos = it->current.pos;
6957 INC_TEXT_POS (pos, it->multibyte_p);
6958
6959 next_face_id = face_at_buffer_position
6960 (it->w, CHARPOS (pos), it->region_beg_charpos,
6961 it->region_end_charpos, &ignore,
6962 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6963 -1);
6964 it->end_of_box_run_p
6965 = (FACE_FROM_ID (it->f, next_face_id)->box
6966 == FACE_NO_BOX);
6967 }
6968 }
6969 }
6970 else
6971 {
6972 int face_id = face_after_it_pos (it);
6973 it->end_of_box_run_p
6974 = (face_id != it->face_id
6975 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6976 }
6977 }
6978 /* If we reached the end of the object we've been iterating (e.g., a
6979 display string or an overlay string), and there's something on
6980 IT->stack, proceed with what's on the stack. It doesn't make
6981 sense to return zero if there's unprocessed stuff on the stack,
6982 because otherwise that stuff will never be displayed. */
6983 if (!success_p && it->sp > 0)
6984 {
6985 set_iterator_to_next (it, 0);
6986 success_p = get_next_display_element (it);
6987 }
6988
6989 /* Value is 0 if end of buffer or string reached. */
6990 return success_p;
6991 }
6992
6993
6994 /* Move IT to the next display element.
6995
6996 RESEAT_P non-zero means if called on a newline in buffer text,
6997 skip to the next visible line start.
6998
6999 Functions get_next_display_element and set_iterator_to_next are
7000 separate because I find this arrangement easier to handle than a
7001 get_next_display_element function that also increments IT's
7002 position. The way it is we can first look at an iterator's current
7003 display element, decide whether it fits on a line, and if it does,
7004 increment the iterator position. The other way around we probably
7005 would either need a flag indicating whether the iterator has to be
7006 incremented the next time, or we would have to implement a
7007 decrement position function which would not be easy to write. */
7008
7009 void
7010 set_iterator_to_next (struct it *it, int reseat_p)
7011 {
7012 /* Reset flags indicating start and end of a sequence of characters
7013 with box. Reset them at the start of this function because
7014 moving the iterator to a new position might set them. */
7015 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7016
7017 switch (it->method)
7018 {
7019 case GET_FROM_BUFFER:
7020 /* The current display element of IT is a character from
7021 current_buffer. Advance in the buffer, and maybe skip over
7022 invisible lines that are so because of selective display. */
7023 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7024 reseat_at_next_visible_line_start (it, 0);
7025 else if (it->cmp_it.id >= 0)
7026 {
7027 /* We are currently getting glyphs from a composition. */
7028 int i;
7029
7030 if (! it->bidi_p)
7031 {
7032 IT_CHARPOS (*it) += it->cmp_it.nchars;
7033 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7035 {
7036 it->cmp_it.from = it->cmp_it.to;
7037 }
7038 else
7039 {
7040 it->cmp_it.id = -1;
7041 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7042 IT_BYTEPOS (*it),
7043 it->end_charpos, Qnil);
7044 }
7045 }
7046 else if (! it->cmp_it.reversed_p)
7047 {
7048 /* Composition created while scanning forward. */
7049 /* Update IT's char/byte positions to point to the first
7050 character of the next grapheme cluster, or to the
7051 character visually after the current composition. */
7052 for (i = 0; i < it->cmp_it.nchars; i++)
7053 bidi_move_to_visually_next (&it->bidi_it);
7054 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7055 IT_CHARPOS (*it) = it->bidi_it.charpos;
7056
7057 if (it->cmp_it.to < it->cmp_it.nglyphs)
7058 {
7059 /* Proceed to the next grapheme cluster. */
7060 it->cmp_it.from = it->cmp_it.to;
7061 }
7062 else
7063 {
7064 /* No more grapheme clusters in this composition.
7065 Find the next stop position. */
7066 ptrdiff_t stop = it->end_charpos;
7067 if (it->bidi_it.scan_dir < 0)
7068 /* Now we are scanning backward and don't know
7069 where to stop. */
7070 stop = -1;
7071 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7072 IT_BYTEPOS (*it), stop, Qnil);
7073 }
7074 }
7075 else
7076 {
7077 /* Composition created while scanning backward. */
7078 /* Update IT's char/byte positions to point to the last
7079 character of the previous grapheme cluster, or the
7080 character visually after the current composition. */
7081 for (i = 0; i < it->cmp_it.nchars; i++)
7082 bidi_move_to_visually_next (&it->bidi_it);
7083 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7084 IT_CHARPOS (*it) = it->bidi_it.charpos;
7085 if (it->cmp_it.from > 0)
7086 {
7087 /* Proceed to the previous grapheme cluster. */
7088 it->cmp_it.to = it->cmp_it.from;
7089 }
7090 else
7091 {
7092 /* No more grapheme clusters in this composition.
7093 Find the next stop position. */
7094 ptrdiff_t stop = it->end_charpos;
7095 if (it->bidi_it.scan_dir < 0)
7096 /* Now we are scanning backward and don't know
7097 where to stop. */
7098 stop = -1;
7099 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7100 IT_BYTEPOS (*it), stop, Qnil);
7101 }
7102 }
7103 }
7104 else
7105 {
7106 eassert (it->len != 0);
7107
7108 if (!it->bidi_p)
7109 {
7110 IT_BYTEPOS (*it) += it->len;
7111 IT_CHARPOS (*it) += 1;
7112 }
7113 else
7114 {
7115 int prev_scan_dir = it->bidi_it.scan_dir;
7116 /* If this is a new paragraph, determine its base
7117 direction (a.k.a. its base embedding level). */
7118 if (it->bidi_it.new_paragraph)
7119 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7120 bidi_move_to_visually_next (&it->bidi_it);
7121 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7122 IT_CHARPOS (*it) = it->bidi_it.charpos;
7123 if (prev_scan_dir != it->bidi_it.scan_dir)
7124 {
7125 /* As the scan direction was changed, we must
7126 re-compute the stop position for composition. */
7127 ptrdiff_t stop = it->end_charpos;
7128 if (it->bidi_it.scan_dir < 0)
7129 stop = -1;
7130 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7131 IT_BYTEPOS (*it), stop, Qnil);
7132 }
7133 }
7134 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7135 }
7136 break;
7137
7138 case GET_FROM_C_STRING:
7139 /* Current display element of IT is from a C string. */
7140 if (!it->bidi_p
7141 /* If the string position is beyond string's end, it means
7142 next_element_from_c_string is padding the string with
7143 blanks, in which case we bypass the bidi iterator,
7144 because it cannot deal with such virtual characters. */
7145 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7146 {
7147 IT_BYTEPOS (*it) += it->len;
7148 IT_CHARPOS (*it) += 1;
7149 }
7150 else
7151 {
7152 bidi_move_to_visually_next (&it->bidi_it);
7153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7154 IT_CHARPOS (*it) = it->bidi_it.charpos;
7155 }
7156 break;
7157
7158 case GET_FROM_DISPLAY_VECTOR:
7159 /* Current display element of IT is from a display table entry.
7160 Advance in the display table definition. Reset it to null if
7161 end reached, and continue with characters from buffers/
7162 strings. */
7163 ++it->current.dpvec_index;
7164
7165 /* Restore face of the iterator to what they were before the
7166 display vector entry (these entries may contain faces). */
7167 it->face_id = it->saved_face_id;
7168
7169 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7170 {
7171 int recheck_faces = it->ellipsis_p;
7172
7173 if (it->s)
7174 it->method = GET_FROM_C_STRING;
7175 else if (STRINGP (it->string))
7176 it->method = GET_FROM_STRING;
7177 else
7178 {
7179 it->method = GET_FROM_BUFFER;
7180 it->object = it->w->buffer;
7181 }
7182
7183 it->dpvec = NULL;
7184 it->current.dpvec_index = -1;
7185
7186 /* Skip over characters which were displayed via IT->dpvec. */
7187 if (it->dpvec_char_len < 0)
7188 reseat_at_next_visible_line_start (it, 1);
7189 else if (it->dpvec_char_len > 0)
7190 {
7191 if (it->method == GET_FROM_STRING
7192 && it->n_overlay_strings > 0)
7193 it->ignore_overlay_strings_at_pos_p = 1;
7194 it->len = it->dpvec_char_len;
7195 set_iterator_to_next (it, reseat_p);
7196 }
7197
7198 /* Maybe recheck faces after display vector */
7199 if (recheck_faces)
7200 it->stop_charpos = IT_CHARPOS (*it);
7201 }
7202 break;
7203
7204 case GET_FROM_STRING:
7205 /* Current display element is a character from a Lisp string. */
7206 eassert (it->s == NULL && STRINGP (it->string));
7207 /* Don't advance past string end. These conditions are true
7208 when set_iterator_to_next is called at the end of
7209 get_next_display_element, in which case the Lisp string is
7210 already exhausted, and all we want is pop the iterator
7211 stack. */
7212 if (it->current.overlay_string_index >= 0)
7213 {
7214 /* This is an overlay string, so there's no padding with
7215 spaces, and the number of characters in the string is
7216 where the string ends. */
7217 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7218 goto consider_string_end;
7219 }
7220 else
7221 {
7222 /* Not an overlay string. There could be padding, so test
7223 against it->end_charpos . */
7224 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7225 goto consider_string_end;
7226 }
7227 if (it->cmp_it.id >= 0)
7228 {
7229 int i;
7230
7231 if (! it->bidi_p)
7232 {
7233 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7234 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7235 if (it->cmp_it.to < it->cmp_it.nglyphs)
7236 it->cmp_it.from = it->cmp_it.to;
7237 else
7238 {
7239 it->cmp_it.id = -1;
7240 composition_compute_stop_pos (&it->cmp_it,
7241 IT_STRING_CHARPOS (*it),
7242 IT_STRING_BYTEPOS (*it),
7243 it->end_charpos, it->string);
7244 }
7245 }
7246 else if (! it->cmp_it.reversed_p)
7247 {
7248 for (i = 0; i < it->cmp_it.nchars; i++)
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7252
7253 if (it->cmp_it.to < it->cmp_it.nglyphs)
7254 it->cmp_it.from = it->cmp_it.to;
7255 else
7256 {
7257 ptrdiff_t stop = it->end_charpos;
7258 if (it->bidi_it.scan_dir < 0)
7259 stop = -1;
7260 composition_compute_stop_pos (&it->cmp_it,
7261 IT_STRING_CHARPOS (*it),
7262 IT_STRING_BYTEPOS (*it), stop,
7263 it->string);
7264 }
7265 }
7266 else
7267 {
7268 for (i = 0; i < it->cmp_it.nchars; i++)
7269 bidi_move_to_visually_next (&it->bidi_it);
7270 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7271 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7272 if (it->cmp_it.from > 0)
7273 it->cmp_it.to = it->cmp_it.from;
7274 else
7275 {
7276 ptrdiff_t stop = it->end_charpos;
7277 if (it->bidi_it.scan_dir < 0)
7278 stop = -1;
7279 composition_compute_stop_pos (&it->cmp_it,
7280 IT_STRING_CHARPOS (*it),
7281 IT_STRING_BYTEPOS (*it), stop,
7282 it->string);
7283 }
7284 }
7285 }
7286 else
7287 {
7288 if (!it->bidi_p
7289 /* If the string position is beyond string's end, it
7290 means next_element_from_string is padding the string
7291 with blanks, in which case we bypass the bidi
7292 iterator, because it cannot deal with such virtual
7293 characters. */
7294 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7295 {
7296 IT_STRING_BYTEPOS (*it) += it->len;
7297 IT_STRING_CHARPOS (*it) += 1;
7298 }
7299 else
7300 {
7301 int prev_scan_dir = it->bidi_it.scan_dir;
7302
7303 bidi_move_to_visually_next (&it->bidi_it);
7304 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7306 if (prev_scan_dir != it->bidi_it.scan_dir)
7307 {
7308 ptrdiff_t stop = it->end_charpos;
7309
7310 if (it->bidi_it.scan_dir < 0)
7311 stop = -1;
7312 composition_compute_stop_pos (&it->cmp_it,
7313 IT_STRING_CHARPOS (*it),
7314 IT_STRING_BYTEPOS (*it), stop,
7315 it->string);
7316 }
7317 }
7318 }
7319
7320 consider_string_end:
7321
7322 if (it->current.overlay_string_index >= 0)
7323 {
7324 /* IT->string is an overlay string. Advance to the
7325 next, if there is one. */
7326 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7327 {
7328 it->ellipsis_p = 0;
7329 next_overlay_string (it);
7330 if (it->ellipsis_p)
7331 setup_for_ellipsis (it, 0);
7332 }
7333 }
7334 else
7335 {
7336 /* IT->string is not an overlay string. If we reached
7337 its end, and there is something on IT->stack, proceed
7338 with what is on the stack. This can be either another
7339 string, this time an overlay string, or a buffer. */
7340 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7341 && it->sp > 0)
7342 {
7343 pop_it (it);
7344 if (it->method == GET_FROM_STRING)
7345 goto consider_string_end;
7346 }
7347 }
7348 break;
7349
7350 case GET_FROM_IMAGE:
7351 case GET_FROM_STRETCH:
7352 /* The position etc with which we have to proceed are on
7353 the stack. The position may be at the end of a string,
7354 if the `display' property takes up the whole string. */
7355 eassert (it->sp > 0);
7356 pop_it (it);
7357 if (it->method == GET_FROM_STRING)
7358 goto consider_string_end;
7359 break;
7360
7361 default:
7362 /* There are no other methods defined, so this should be a bug. */
7363 emacs_abort ();
7364 }
7365
7366 eassert (it->method != GET_FROM_STRING
7367 || (STRINGP (it->string)
7368 && IT_STRING_CHARPOS (*it) >= 0));
7369 }
7370
7371 /* Load IT's display element fields with information about the next
7372 display element which comes from a display table entry or from the
7373 result of translating a control character to one of the forms `^C'
7374 or `\003'.
7375
7376 IT->dpvec holds the glyphs to return as characters.
7377 IT->saved_face_id holds the face id before the display vector--it
7378 is restored into IT->face_id in set_iterator_to_next. */
7379
7380 static int
7381 next_element_from_display_vector (struct it *it)
7382 {
7383 Lisp_Object gc;
7384
7385 /* Precondition. */
7386 eassert (it->dpvec && it->current.dpvec_index >= 0);
7387
7388 it->face_id = it->saved_face_id;
7389
7390 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7391 That seemed totally bogus - so I changed it... */
7392 gc = it->dpvec[it->current.dpvec_index];
7393
7394 if (GLYPH_CODE_P (gc))
7395 {
7396 it->c = GLYPH_CODE_CHAR (gc);
7397 it->len = CHAR_BYTES (it->c);
7398
7399 /* The entry may contain a face id to use. Such a face id is
7400 the id of a Lisp face, not a realized face. A face id of
7401 zero means no face is specified. */
7402 if (it->dpvec_face_id >= 0)
7403 it->face_id = it->dpvec_face_id;
7404 else
7405 {
7406 int lface_id = GLYPH_CODE_FACE (gc);
7407 if (lface_id > 0)
7408 it->face_id = merge_faces (it->f, Qt, lface_id,
7409 it->saved_face_id);
7410 }
7411 }
7412 else
7413 /* Display table entry is invalid. Return a space. */
7414 it->c = ' ', it->len = 1;
7415
7416 /* Don't change position and object of the iterator here. They are
7417 still the values of the character that had this display table
7418 entry or was translated, and that's what we want. */
7419 it->what = IT_CHARACTER;
7420 return 1;
7421 }
7422
7423 /* Get the first element of string/buffer in the visual order, after
7424 being reseated to a new position in a string or a buffer. */
7425 static void
7426 get_visually_first_element (struct it *it)
7427 {
7428 int string_p = STRINGP (it->string) || it->s;
7429 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7430 ptrdiff_t bob = (string_p ? 0 : BEGV);
7431
7432 if (STRINGP (it->string))
7433 {
7434 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7435 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7436 }
7437 else
7438 {
7439 it->bidi_it.charpos = IT_CHARPOS (*it);
7440 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7441 }
7442
7443 if (it->bidi_it.charpos == eob)
7444 {
7445 /* Nothing to do, but reset the FIRST_ELT flag, like
7446 bidi_paragraph_init does, because we are not going to
7447 call it. */
7448 it->bidi_it.first_elt = 0;
7449 }
7450 else if (it->bidi_it.charpos == bob
7451 || (!string_p
7452 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7453 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7454 {
7455 /* If we are at the beginning of a line/string, we can produce
7456 the next element right away. */
7457 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7458 bidi_move_to_visually_next (&it->bidi_it);
7459 }
7460 else
7461 {
7462 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7463
7464 /* We need to prime the bidi iterator starting at the line's or
7465 string's beginning, before we will be able to produce the
7466 next element. */
7467 if (string_p)
7468 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7469 else
7470 {
7471 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7472 -1);
7473 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7474 }
7475 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7476 do
7477 {
7478 /* Now return to buffer/string position where we were asked
7479 to get the next display element, and produce that. */
7480 bidi_move_to_visually_next (&it->bidi_it);
7481 }
7482 while (it->bidi_it.bytepos != orig_bytepos
7483 && it->bidi_it.charpos < eob);
7484 }
7485
7486 /* Adjust IT's position information to where we ended up. */
7487 if (STRINGP (it->string))
7488 {
7489 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7490 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7491 }
7492 else
7493 {
7494 IT_CHARPOS (*it) = it->bidi_it.charpos;
7495 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7496 }
7497
7498 if (STRINGP (it->string) || !it->s)
7499 {
7500 ptrdiff_t stop, charpos, bytepos;
7501
7502 if (STRINGP (it->string))
7503 {
7504 eassert (!it->s);
7505 stop = SCHARS (it->string);
7506 if (stop > it->end_charpos)
7507 stop = it->end_charpos;
7508 charpos = IT_STRING_CHARPOS (*it);
7509 bytepos = IT_STRING_BYTEPOS (*it);
7510 }
7511 else
7512 {
7513 stop = it->end_charpos;
7514 charpos = IT_CHARPOS (*it);
7515 bytepos = IT_BYTEPOS (*it);
7516 }
7517 if (it->bidi_it.scan_dir < 0)
7518 stop = -1;
7519 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7520 it->string);
7521 }
7522 }
7523
7524 /* Load IT with the next display element from Lisp string IT->string.
7525 IT->current.string_pos is the current position within the string.
7526 If IT->current.overlay_string_index >= 0, the Lisp string is an
7527 overlay string. */
7528
7529 static int
7530 next_element_from_string (struct it *it)
7531 {
7532 struct text_pos position;
7533
7534 eassert (STRINGP (it->string));
7535 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7536 eassert (IT_STRING_CHARPOS (*it) >= 0);
7537 position = it->current.string_pos;
7538
7539 /* With bidi reordering, the character to display might not be the
7540 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7541 that we were reseat()ed to a new string, whose paragraph
7542 direction is not known. */
7543 if (it->bidi_p && it->bidi_it.first_elt)
7544 {
7545 get_visually_first_element (it);
7546 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7547 }
7548
7549 /* Time to check for invisible text? */
7550 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7551 {
7552 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7553 {
7554 if (!(!it->bidi_p
7555 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7556 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7557 {
7558 /* With bidi non-linear iteration, we could find
7559 ourselves far beyond the last computed stop_charpos,
7560 with several other stop positions in between that we
7561 missed. Scan them all now, in buffer's logical
7562 order, until we find and handle the last stop_charpos
7563 that precedes our current position. */
7564 handle_stop_backwards (it, it->stop_charpos);
7565 return GET_NEXT_DISPLAY_ELEMENT (it);
7566 }
7567 else
7568 {
7569 if (it->bidi_p)
7570 {
7571 /* Take note of the stop position we just moved
7572 across, for when we will move back across it. */
7573 it->prev_stop = it->stop_charpos;
7574 /* If we are at base paragraph embedding level, take
7575 note of the last stop position seen at this
7576 level. */
7577 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7578 it->base_level_stop = it->stop_charpos;
7579 }
7580 handle_stop (it);
7581
7582 /* Since a handler may have changed IT->method, we must
7583 recurse here. */
7584 return GET_NEXT_DISPLAY_ELEMENT (it);
7585 }
7586 }
7587 else if (it->bidi_p
7588 /* If we are before prev_stop, we may have overstepped
7589 on our way backwards a stop_pos, and if so, we need
7590 to handle that stop_pos. */
7591 && IT_STRING_CHARPOS (*it) < it->prev_stop
7592 /* We can sometimes back up for reasons that have nothing
7593 to do with bidi reordering. E.g., compositions. The
7594 code below is only needed when we are above the base
7595 embedding level, so test for that explicitly. */
7596 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7597 {
7598 /* If we lost track of base_level_stop, we have no better
7599 place for handle_stop_backwards to start from than string
7600 beginning. This happens, e.g., when we were reseated to
7601 the previous screenful of text by vertical-motion. */
7602 if (it->base_level_stop <= 0
7603 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7604 it->base_level_stop = 0;
7605 handle_stop_backwards (it, it->base_level_stop);
7606 return GET_NEXT_DISPLAY_ELEMENT (it);
7607 }
7608 }
7609
7610 if (it->current.overlay_string_index >= 0)
7611 {
7612 /* Get the next character from an overlay string. In overlay
7613 strings, there is no field width or padding with spaces to
7614 do. */
7615 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7616 {
7617 it->what = IT_EOB;
7618 return 0;
7619 }
7620 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7621 IT_STRING_BYTEPOS (*it),
7622 it->bidi_it.scan_dir < 0
7623 ? -1
7624 : SCHARS (it->string))
7625 && next_element_from_composition (it))
7626 {
7627 return 1;
7628 }
7629 else if (STRING_MULTIBYTE (it->string))
7630 {
7631 const unsigned char *s = (SDATA (it->string)
7632 + IT_STRING_BYTEPOS (*it));
7633 it->c = string_char_and_length (s, &it->len);
7634 }
7635 else
7636 {
7637 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7638 it->len = 1;
7639 }
7640 }
7641 else
7642 {
7643 /* Get the next character from a Lisp string that is not an
7644 overlay string. Such strings come from the mode line, for
7645 example. We may have to pad with spaces, or truncate the
7646 string. See also next_element_from_c_string. */
7647 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7648 {
7649 it->what = IT_EOB;
7650 return 0;
7651 }
7652 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7653 {
7654 /* Pad with spaces. */
7655 it->c = ' ', it->len = 1;
7656 CHARPOS (position) = BYTEPOS (position) = -1;
7657 }
7658 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7659 IT_STRING_BYTEPOS (*it),
7660 it->bidi_it.scan_dir < 0
7661 ? -1
7662 : it->string_nchars)
7663 && next_element_from_composition (it))
7664 {
7665 return 1;
7666 }
7667 else if (STRING_MULTIBYTE (it->string))
7668 {
7669 const unsigned char *s = (SDATA (it->string)
7670 + IT_STRING_BYTEPOS (*it));
7671 it->c = string_char_and_length (s, &it->len);
7672 }
7673 else
7674 {
7675 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7676 it->len = 1;
7677 }
7678 }
7679
7680 /* Record what we have and where it came from. */
7681 it->what = IT_CHARACTER;
7682 it->object = it->string;
7683 it->position = position;
7684 return 1;
7685 }
7686
7687
7688 /* Load IT with next display element from C string IT->s.
7689 IT->string_nchars is the maximum number of characters to return
7690 from the string. IT->end_charpos may be greater than
7691 IT->string_nchars when this function is called, in which case we
7692 may have to return padding spaces. Value is zero if end of string
7693 reached, including padding spaces. */
7694
7695 static int
7696 next_element_from_c_string (struct it *it)
7697 {
7698 int success_p = 1;
7699
7700 eassert (it->s);
7701 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7702 it->what = IT_CHARACTER;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7704 it->object = Qnil;
7705
7706 /* With bidi reordering, the character to display might not be the
7707 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7708 we were reseated to a new string, whose paragraph direction is
7709 not known. */
7710 if (it->bidi_p && it->bidi_it.first_elt)
7711 get_visually_first_element (it);
7712
7713 /* IT's position can be greater than IT->string_nchars in case a
7714 field width or precision has been specified when the iterator was
7715 initialized. */
7716 if (IT_CHARPOS (*it) >= it->end_charpos)
7717 {
7718 /* End of the game. */
7719 it->what = IT_EOB;
7720 success_p = 0;
7721 }
7722 else if (IT_CHARPOS (*it) >= it->string_nchars)
7723 {
7724 /* Pad with spaces. */
7725 it->c = ' ', it->len = 1;
7726 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7727 }
7728 else if (it->multibyte_p)
7729 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7730 else
7731 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7732
7733 return success_p;
7734 }
7735
7736
7737 /* Set up IT to return characters from an ellipsis, if appropriate.
7738 The definition of the ellipsis glyphs may come from a display table
7739 entry. This function fills IT with the first glyph from the
7740 ellipsis if an ellipsis is to be displayed. */
7741
7742 static int
7743 next_element_from_ellipsis (struct it *it)
7744 {
7745 if (it->selective_display_ellipsis_p)
7746 setup_for_ellipsis (it, it->len);
7747 else
7748 {
7749 /* The face at the current position may be different from the
7750 face we find after the invisible text. Remember what it
7751 was in IT->saved_face_id, and signal that it's there by
7752 setting face_before_selective_p. */
7753 it->saved_face_id = it->face_id;
7754 it->method = GET_FROM_BUFFER;
7755 it->object = it->w->buffer;
7756 reseat_at_next_visible_line_start (it, 1);
7757 it->face_before_selective_p = 1;
7758 }
7759
7760 return GET_NEXT_DISPLAY_ELEMENT (it);
7761 }
7762
7763
7764 /* Deliver an image display element. The iterator IT is already
7765 filled with image information (done in handle_display_prop). Value
7766 is always 1. */
7767
7768
7769 static int
7770 next_element_from_image (struct it *it)
7771 {
7772 it->what = IT_IMAGE;
7773 it->ignore_overlay_strings_at_pos_p = 0;
7774 return 1;
7775 }
7776
7777
7778 /* Fill iterator IT with next display element from a stretch glyph
7779 property. IT->object is the value of the text property. Value is
7780 always 1. */
7781
7782 static int
7783 next_element_from_stretch (struct it *it)
7784 {
7785 it->what = IT_STRETCH;
7786 return 1;
7787 }
7788
7789 /* Scan backwards from IT's current position until we find a stop
7790 position, or until BEGV. This is called when we find ourself
7791 before both the last known prev_stop and base_level_stop while
7792 reordering bidirectional text. */
7793
7794 static void
7795 compute_stop_pos_backwards (struct it *it)
7796 {
7797 const int SCAN_BACK_LIMIT = 1000;
7798 struct text_pos pos;
7799 struct display_pos save_current = it->current;
7800 struct text_pos save_position = it->position;
7801 ptrdiff_t charpos = IT_CHARPOS (*it);
7802 ptrdiff_t where_we_are = charpos;
7803 ptrdiff_t save_stop_pos = it->stop_charpos;
7804 ptrdiff_t save_end_pos = it->end_charpos;
7805
7806 eassert (NILP (it->string) && !it->s);
7807 eassert (it->bidi_p);
7808 it->bidi_p = 0;
7809 do
7810 {
7811 it->end_charpos = min (charpos + 1, ZV);
7812 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7813 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7814 reseat_1 (it, pos, 0);
7815 compute_stop_pos (it);
7816 /* We must advance forward, right? */
7817 if (it->stop_charpos <= charpos)
7818 emacs_abort ();
7819 }
7820 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7821
7822 if (it->stop_charpos <= where_we_are)
7823 it->prev_stop = it->stop_charpos;
7824 else
7825 it->prev_stop = BEGV;
7826 it->bidi_p = 1;
7827 it->current = save_current;
7828 it->position = save_position;
7829 it->stop_charpos = save_stop_pos;
7830 it->end_charpos = save_end_pos;
7831 }
7832
7833 /* Scan forward from CHARPOS in the current buffer/string, until we
7834 find a stop position > current IT's position. Then handle the stop
7835 position before that. This is called when we bump into a stop
7836 position while reordering bidirectional text. CHARPOS should be
7837 the last previously processed stop_pos (or BEGV/0, if none were
7838 processed yet) whose position is less that IT's current
7839 position. */
7840
7841 static void
7842 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7843 {
7844 int bufp = !STRINGP (it->string);
7845 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7846 struct display_pos save_current = it->current;
7847 struct text_pos save_position = it->position;
7848 struct text_pos pos1;
7849 ptrdiff_t next_stop;
7850
7851 /* Scan in strict logical order. */
7852 eassert (it->bidi_p);
7853 it->bidi_p = 0;
7854 do
7855 {
7856 it->prev_stop = charpos;
7857 if (bufp)
7858 {
7859 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7860 reseat_1 (it, pos1, 0);
7861 }
7862 else
7863 it->current.string_pos = string_pos (charpos, it->string);
7864 compute_stop_pos (it);
7865 /* We must advance forward, right? */
7866 if (it->stop_charpos <= it->prev_stop)
7867 emacs_abort ();
7868 charpos = it->stop_charpos;
7869 }
7870 while (charpos <= where_we_are);
7871
7872 it->bidi_p = 1;
7873 it->current = save_current;
7874 it->position = save_position;
7875 next_stop = it->stop_charpos;
7876 it->stop_charpos = it->prev_stop;
7877 handle_stop (it);
7878 it->stop_charpos = next_stop;
7879 }
7880
7881 /* Load IT with the next display element from current_buffer. Value
7882 is zero if end of buffer reached. IT->stop_charpos is the next
7883 position at which to stop and check for text properties or buffer
7884 end. */
7885
7886 static int
7887 next_element_from_buffer (struct it *it)
7888 {
7889 int success_p = 1;
7890
7891 eassert (IT_CHARPOS (*it) >= BEGV);
7892 eassert (NILP (it->string) && !it->s);
7893 eassert (!it->bidi_p
7894 || (EQ (it->bidi_it.string.lstring, Qnil)
7895 && it->bidi_it.string.s == NULL));
7896
7897 /* With bidi reordering, the character to display might not be the
7898 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7899 we were reseat()ed to a new buffer position, which is potentially
7900 a different paragraph. */
7901 if (it->bidi_p && it->bidi_it.first_elt)
7902 {
7903 get_visually_first_element (it);
7904 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7905 }
7906
7907 if (IT_CHARPOS (*it) >= it->stop_charpos)
7908 {
7909 if (IT_CHARPOS (*it) >= it->end_charpos)
7910 {
7911 int overlay_strings_follow_p;
7912
7913 /* End of the game, except when overlay strings follow that
7914 haven't been returned yet. */
7915 if (it->overlay_strings_at_end_processed_p)
7916 overlay_strings_follow_p = 0;
7917 else
7918 {
7919 it->overlay_strings_at_end_processed_p = 1;
7920 overlay_strings_follow_p = get_overlay_strings (it, 0);
7921 }
7922
7923 if (overlay_strings_follow_p)
7924 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7925 else
7926 {
7927 it->what = IT_EOB;
7928 it->position = it->current.pos;
7929 success_p = 0;
7930 }
7931 }
7932 else if (!(!it->bidi_p
7933 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7934 || IT_CHARPOS (*it) == it->stop_charpos))
7935 {
7936 /* With bidi non-linear iteration, we could find ourselves
7937 far beyond the last computed stop_charpos, with several
7938 other stop positions in between that we missed. Scan
7939 them all now, in buffer's logical order, until we find
7940 and handle the last stop_charpos that precedes our
7941 current position. */
7942 handle_stop_backwards (it, it->stop_charpos);
7943 return GET_NEXT_DISPLAY_ELEMENT (it);
7944 }
7945 else
7946 {
7947 if (it->bidi_p)
7948 {
7949 /* Take note of the stop position we just moved across,
7950 for when we will move back across it. */
7951 it->prev_stop = it->stop_charpos;
7952 /* If we are at base paragraph embedding level, take
7953 note of the last stop position seen at this
7954 level. */
7955 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7956 it->base_level_stop = it->stop_charpos;
7957 }
7958 handle_stop (it);
7959 return GET_NEXT_DISPLAY_ELEMENT (it);
7960 }
7961 }
7962 else if (it->bidi_p
7963 /* If we are before prev_stop, we may have overstepped on
7964 our way backwards a stop_pos, and if so, we need to
7965 handle that stop_pos. */
7966 && IT_CHARPOS (*it) < it->prev_stop
7967 /* We can sometimes back up for reasons that have nothing
7968 to do with bidi reordering. E.g., compositions. The
7969 code below is only needed when we are above the base
7970 embedding level, so test for that explicitly. */
7971 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7972 {
7973 if (it->base_level_stop <= 0
7974 || IT_CHARPOS (*it) < it->base_level_stop)
7975 {
7976 /* If we lost track of base_level_stop, we need to find
7977 prev_stop by looking backwards. This happens, e.g., when
7978 we were reseated to the previous screenful of text by
7979 vertical-motion. */
7980 it->base_level_stop = BEGV;
7981 compute_stop_pos_backwards (it);
7982 handle_stop_backwards (it, it->prev_stop);
7983 }
7984 else
7985 handle_stop_backwards (it, it->base_level_stop);
7986 return GET_NEXT_DISPLAY_ELEMENT (it);
7987 }
7988 else
7989 {
7990 /* No face changes, overlays etc. in sight, so just return a
7991 character from current_buffer. */
7992 unsigned char *p;
7993 ptrdiff_t stop;
7994
7995 /* Maybe run the redisplay end trigger hook. Performance note:
7996 This doesn't seem to cost measurable time. */
7997 if (it->redisplay_end_trigger_charpos
7998 && it->glyph_row
7999 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8000 run_redisplay_end_trigger_hook (it);
8001
8002 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8003 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8004 stop)
8005 && next_element_from_composition (it))
8006 {
8007 return 1;
8008 }
8009
8010 /* Get the next character, maybe multibyte. */
8011 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8012 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8013 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8014 else
8015 it->c = *p, it->len = 1;
8016
8017 /* Record what we have and where it came from. */
8018 it->what = IT_CHARACTER;
8019 it->object = it->w->buffer;
8020 it->position = it->current.pos;
8021
8022 /* Normally we return the character found above, except when we
8023 really want to return an ellipsis for selective display. */
8024 if (it->selective)
8025 {
8026 if (it->c == '\n')
8027 {
8028 /* A value of selective > 0 means hide lines indented more
8029 than that number of columns. */
8030 if (it->selective > 0
8031 && IT_CHARPOS (*it) + 1 < ZV
8032 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8033 IT_BYTEPOS (*it) + 1,
8034 it->selective))
8035 {
8036 success_p = next_element_from_ellipsis (it);
8037 it->dpvec_char_len = -1;
8038 }
8039 }
8040 else if (it->c == '\r' && it->selective == -1)
8041 {
8042 /* A value of selective == -1 means that everything from the
8043 CR to the end of the line is invisible, with maybe an
8044 ellipsis displayed for it. */
8045 success_p = next_element_from_ellipsis (it);
8046 it->dpvec_char_len = -1;
8047 }
8048 }
8049 }
8050
8051 /* Value is zero if end of buffer reached. */
8052 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8053 return success_p;
8054 }
8055
8056
8057 /* Run the redisplay end trigger hook for IT. */
8058
8059 static void
8060 run_redisplay_end_trigger_hook (struct it *it)
8061 {
8062 Lisp_Object args[3];
8063
8064 /* IT->glyph_row should be non-null, i.e. we should be actually
8065 displaying something, or otherwise we should not run the hook. */
8066 eassert (it->glyph_row);
8067
8068 /* Set up hook arguments. */
8069 args[0] = Qredisplay_end_trigger_functions;
8070 args[1] = it->window;
8071 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8072 it->redisplay_end_trigger_charpos = 0;
8073
8074 /* Since we are *trying* to run these functions, don't try to run
8075 them again, even if they get an error. */
8076 wset_redisplay_end_trigger (it->w, Qnil);
8077 Frun_hook_with_args (3, args);
8078
8079 /* Notice if it changed the face of the character we are on. */
8080 handle_face_prop (it);
8081 }
8082
8083
8084 /* Deliver a composition display element. Unlike the other
8085 next_element_from_XXX, this function is not registered in the array
8086 get_next_element[]. It is called from next_element_from_buffer and
8087 next_element_from_string when necessary. */
8088
8089 static int
8090 next_element_from_composition (struct it *it)
8091 {
8092 it->what = IT_COMPOSITION;
8093 it->len = it->cmp_it.nbytes;
8094 if (STRINGP (it->string))
8095 {
8096 if (it->c < 0)
8097 {
8098 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8099 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8100 return 0;
8101 }
8102 it->position = it->current.string_pos;
8103 it->object = it->string;
8104 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8105 IT_STRING_BYTEPOS (*it), it->string);
8106 }
8107 else
8108 {
8109 if (it->c < 0)
8110 {
8111 IT_CHARPOS (*it) += it->cmp_it.nchars;
8112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8113 if (it->bidi_p)
8114 {
8115 if (it->bidi_it.new_paragraph)
8116 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8117 /* Resync the bidi iterator with IT's new position.
8118 FIXME: this doesn't support bidirectional text. */
8119 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8120 bidi_move_to_visually_next (&it->bidi_it);
8121 }
8122 return 0;
8123 }
8124 it->position = it->current.pos;
8125 it->object = it->w->buffer;
8126 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8127 IT_BYTEPOS (*it), Qnil);
8128 }
8129 return 1;
8130 }
8131
8132
8133 \f
8134 /***********************************************************************
8135 Moving an iterator without producing glyphs
8136 ***********************************************************************/
8137
8138 /* Check if iterator is at a position corresponding to a valid buffer
8139 position after some move_it_ call. */
8140
8141 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8142 ((it)->method == GET_FROM_STRING \
8143 ? IT_STRING_CHARPOS (*it) == 0 \
8144 : 1)
8145
8146
8147 /* Move iterator IT to a specified buffer or X position within one
8148 line on the display without producing glyphs.
8149
8150 OP should be a bit mask including some or all of these bits:
8151 MOVE_TO_X: Stop upon reaching x-position TO_X.
8152 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8153 Regardless of OP's value, stop upon reaching the end of the display line.
8154
8155 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8156 This means, in particular, that TO_X includes window's horizontal
8157 scroll amount.
8158
8159 The return value has several possible values that
8160 say what condition caused the scan to stop:
8161
8162 MOVE_POS_MATCH_OR_ZV
8163 - when TO_POS or ZV was reached.
8164
8165 MOVE_X_REACHED
8166 -when TO_X was reached before TO_POS or ZV were reached.
8167
8168 MOVE_LINE_CONTINUED
8169 - when we reached the end of the display area and the line must
8170 be continued.
8171
8172 MOVE_LINE_TRUNCATED
8173 - when we reached the end of the display area and the line is
8174 truncated.
8175
8176 MOVE_NEWLINE_OR_CR
8177 - when we stopped at a line end, i.e. a newline or a CR and selective
8178 display is on. */
8179
8180 static enum move_it_result
8181 move_it_in_display_line_to (struct it *it,
8182 ptrdiff_t to_charpos, int to_x,
8183 enum move_operation_enum op)
8184 {
8185 enum move_it_result result = MOVE_UNDEFINED;
8186 struct glyph_row *saved_glyph_row;
8187 struct it wrap_it, atpos_it, atx_it, ppos_it;
8188 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8189 void *ppos_data = NULL;
8190 int may_wrap = 0;
8191 enum it_method prev_method = it->method;
8192 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8193 int saw_smaller_pos = prev_pos < to_charpos;
8194
8195 /* Don't produce glyphs in produce_glyphs. */
8196 saved_glyph_row = it->glyph_row;
8197 it->glyph_row = NULL;
8198
8199 /* Use wrap_it to save a copy of IT wherever a word wrap could
8200 occur. Use atpos_it to save a copy of IT at the desired buffer
8201 position, if found, so that we can scan ahead and check if the
8202 word later overshoots the window edge. Use atx_it similarly, for
8203 pixel positions. */
8204 wrap_it.sp = -1;
8205 atpos_it.sp = -1;
8206 atx_it.sp = -1;
8207
8208 /* Use ppos_it under bidi reordering to save a copy of IT for the
8209 position > CHARPOS that is the closest to CHARPOS. We restore
8210 that position in IT when we have scanned the entire display line
8211 without finding a match for CHARPOS and all the character
8212 positions are greater than CHARPOS. */
8213 if (it->bidi_p)
8214 {
8215 SAVE_IT (ppos_it, *it, ppos_data);
8216 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8217 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8218 SAVE_IT (ppos_it, *it, ppos_data);
8219 }
8220
8221 #define BUFFER_POS_REACHED_P() \
8222 ((op & MOVE_TO_POS) != 0 \
8223 && BUFFERP (it->object) \
8224 && (IT_CHARPOS (*it) == to_charpos \
8225 || ((!it->bidi_p \
8226 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8227 && IT_CHARPOS (*it) > to_charpos) \
8228 || (it->what == IT_COMPOSITION \
8229 && ((IT_CHARPOS (*it) > to_charpos \
8230 && to_charpos >= it->cmp_it.charpos) \
8231 || (IT_CHARPOS (*it) < to_charpos \
8232 && to_charpos <= it->cmp_it.charpos)))) \
8233 && (it->method == GET_FROM_BUFFER \
8234 || (it->method == GET_FROM_DISPLAY_VECTOR \
8235 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8236
8237 /* If there's a line-/wrap-prefix, handle it. */
8238 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8239 && it->current_y < it->last_visible_y)
8240 handle_line_prefix (it);
8241
8242 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8243 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8244
8245 while (1)
8246 {
8247 int x, i, ascent = 0, descent = 0;
8248
8249 /* Utility macro to reset an iterator with x, ascent, and descent. */
8250 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8251 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8252 (IT)->max_descent = descent)
8253
8254 /* Stop if we move beyond TO_CHARPOS (after an image or a
8255 display string or stretch glyph). */
8256 if ((op & MOVE_TO_POS) != 0
8257 && BUFFERP (it->object)
8258 && it->method == GET_FROM_BUFFER
8259 && (((!it->bidi_p
8260 /* When the iterator is at base embedding level, we
8261 are guaranteed that characters are delivered for
8262 display in strictly increasing order of their
8263 buffer positions. */
8264 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8265 && IT_CHARPOS (*it) > to_charpos)
8266 || (it->bidi_p
8267 && (prev_method == GET_FROM_IMAGE
8268 || prev_method == GET_FROM_STRETCH
8269 || prev_method == GET_FROM_STRING)
8270 /* Passed TO_CHARPOS from left to right. */
8271 && ((prev_pos < to_charpos
8272 && IT_CHARPOS (*it) > to_charpos)
8273 /* Passed TO_CHARPOS from right to left. */
8274 || (prev_pos > to_charpos
8275 && IT_CHARPOS (*it) < to_charpos)))))
8276 {
8277 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8278 {
8279 result = MOVE_POS_MATCH_OR_ZV;
8280 break;
8281 }
8282 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8283 /* If wrap_it is valid, the current position might be in a
8284 word that is wrapped. So, save the iterator in
8285 atpos_it and continue to see if wrapping happens. */
8286 SAVE_IT (atpos_it, *it, atpos_data);
8287 }
8288
8289 /* Stop when ZV reached.
8290 We used to stop here when TO_CHARPOS reached as well, but that is
8291 too soon if this glyph does not fit on this line. So we handle it
8292 explicitly below. */
8293 if (!get_next_display_element (it))
8294 {
8295 result = MOVE_POS_MATCH_OR_ZV;
8296 break;
8297 }
8298
8299 if (it->line_wrap == TRUNCATE)
8300 {
8301 if (BUFFER_POS_REACHED_P ())
8302 {
8303 result = MOVE_POS_MATCH_OR_ZV;
8304 break;
8305 }
8306 }
8307 else
8308 {
8309 if (it->line_wrap == WORD_WRAP)
8310 {
8311 if (IT_DISPLAYING_WHITESPACE (it))
8312 may_wrap = 1;
8313 else if (may_wrap)
8314 {
8315 /* We have reached a glyph that follows one or more
8316 whitespace characters. If the position is
8317 already found, we are done. */
8318 if (atpos_it.sp >= 0)
8319 {
8320 RESTORE_IT (it, &atpos_it, atpos_data);
8321 result = MOVE_POS_MATCH_OR_ZV;
8322 goto done;
8323 }
8324 if (atx_it.sp >= 0)
8325 {
8326 RESTORE_IT (it, &atx_it, atx_data);
8327 result = MOVE_X_REACHED;
8328 goto done;
8329 }
8330 /* Otherwise, we can wrap here. */
8331 SAVE_IT (wrap_it, *it, wrap_data);
8332 may_wrap = 0;
8333 }
8334 }
8335 }
8336
8337 /* Remember the line height for the current line, in case
8338 the next element doesn't fit on the line. */
8339 ascent = it->max_ascent;
8340 descent = it->max_descent;
8341
8342 /* The call to produce_glyphs will get the metrics of the
8343 display element IT is loaded with. Record the x-position
8344 before this display element, in case it doesn't fit on the
8345 line. */
8346 x = it->current_x;
8347
8348 PRODUCE_GLYPHS (it);
8349
8350 if (it->area != TEXT_AREA)
8351 {
8352 prev_method = it->method;
8353 if (it->method == GET_FROM_BUFFER)
8354 prev_pos = IT_CHARPOS (*it);
8355 set_iterator_to_next (it, 1);
8356 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8357 SET_TEXT_POS (this_line_min_pos,
8358 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8359 if (it->bidi_p
8360 && (op & MOVE_TO_POS)
8361 && IT_CHARPOS (*it) > to_charpos
8362 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8363 SAVE_IT (ppos_it, *it, ppos_data);
8364 continue;
8365 }
8366
8367 /* The number of glyphs we get back in IT->nglyphs will normally
8368 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8369 character on a terminal frame, or (iii) a line end. For the
8370 second case, IT->nglyphs - 1 padding glyphs will be present.
8371 (On X frames, there is only one glyph produced for a
8372 composite character.)
8373
8374 The behavior implemented below means, for continuation lines,
8375 that as many spaces of a TAB as fit on the current line are
8376 displayed there. For terminal frames, as many glyphs of a
8377 multi-glyph character are displayed in the current line, too.
8378 This is what the old redisplay code did, and we keep it that
8379 way. Under X, the whole shape of a complex character must
8380 fit on the line or it will be completely displayed in the
8381 next line.
8382
8383 Note that both for tabs and padding glyphs, all glyphs have
8384 the same width. */
8385 if (it->nglyphs)
8386 {
8387 /* More than one glyph or glyph doesn't fit on line. All
8388 glyphs have the same width. */
8389 int single_glyph_width = it->pixel_width / it->nglyphs;
8390 int new_x;
8391 int x_before_this_char = x;
8392 int hpos_before_this_char = it->hpos;
8393
8394 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8395 {
8396 new_x = x + single_glyph_width;
8397
8398 /* We want to leave anything reaching TO_X to the caller. */
8399 if ((op & MOVE_TO_X) && new_x > to_x)
8400 {
8401 if (BUFFER_POS_REACHED_P ())
8402 {
8403 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8404 goto buffer_pos_reached;
8405 if (atpos_it.sp < 0)
8406 {
8407 SAVE_IT (atpos_it, *it, atpos_data);
8408 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8409 }
8410 }
8411 else
8412 {
8413 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8414 {
8415 it->current_x = x;
8416 result = MOVE_X_REACHED;
8417 break;
8418 }
8419 if (atx_it.sp < 0)
8420 {
8421 SAVE_IT (atx_it, *it, atx_data);
8422 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8423 }
8424 }
8425 }
8426
8427 if (/* Lines are continued. */
8428 it->line_wrap != TRUNCATE
8429 && (/* And glyph doesn't fit on the line. */
8430 new_x > it->last_visible_x
8431 /* Or it fits exactly and we're on a window
8432 system frame. */
8433 || (new_x == it->last_visible_x
8434 && FRAME_WINDOW_P (it->f)
8435 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8436 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8437 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8438 {
8439 if (/* IT->hpos == 0 means the very first glyph
8440 doesn't fit on the line, e.g. a wide image. */
8441 it->hpos == 0
8442 || (new_x == it->last_visible_x
8443 && FRAME_WINDOW_P (it->f)))
8444 {
8445 ++it->hpos;
8446 it->current_x = new_x;
8447
8448 /* The character's last glyph just barely fits
8449 in this row. */
8450 if (i == it->nglyphs - 1)
8451 {
8452 /* If this is the destination position,
8453 return a position *before* it in this row,
8454 now that we know it fits in this row. */
8455 if (BUFFER_POS_REACHED_P ())
8456 {
8457 if (it->line_wrap != WORD_WRAP
8458 || wrap_it.sp < 0)
8459 {
8460 it->hpos = hpos_before_this_char;
8461 it->current_x = x_before_this_char;
8462 result = MOVE_POS_MATCH_OR_ZV;
8463 break;
8464 }
8465 if (it->line_wrap == WORD_WRAP
8466 && atpos_it.sp < 0)
8467 {
8468 SAVE_IT (atpos_it, *it, atpos_data);
8469 atpos_it.current_x = x_before_this_char;
8470 atpos_it.hpos = hpos_before_this_char;
8471 }
8472 }
8473
8474 prev_method = it->method;
8475 if (it->method == GET_FROM_BUFFER)
8476 prev_pos = IT_CHARPOS (*it);
8477 set_iterator_to_next (it, 1);
8478 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8479 SET_TEXT_POS (this_line_min_pos,
8480 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8481 /* On graphical terminals, newlines may
8482 "overflow" into the fringe if
8483 overflow-newline-into-fringe is non-nil.
8484 On text terminals, and on graphical
8485 terminals with no right margin, newlines
8486 may overflow into the last glyph on the
8487 display line.*/
8488 if (!FRAME_WINDOW_P (it->f)
8489 || ((it->bidi_p
8490 && it->bidi_it.paragraph_dir == R2L)
8491 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8492 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8493 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8494 {
8495 if (!get_next_display_element (it))
8496 {
8497 result = MOVE_POS_MATCH_OR_ZV;
8498 break;
8499 }
8500 if (BUFFER_POS_REACHED_P ())
8501 {
8502 if (ITERATOR_AT_END_OF_LINE_P (it))
8503 result = MOVE_POS_MATCH_OR_ZV;
8504 else
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8507 }
8508 if (ITERATOR_AT_END_OF_LINE_P (it))
8509 {
8510 result = MOVE_NEWLINE_OR_CR;
8511 break;
8512 }
8513 }
8514 }
8515 }
8516 else
8517 IT_RESET_X_ASCENT_DESCENT (it);
8518
8519 if (wrap_it.sp >= 0)
8520 {
8521 RESTORE_IT (it, &wrap_it, wrap_data);
8522 atpos_it.sp = -1;
8523 atx_it.sp = -1;
8524 }
8525
8526 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8527 IT_CHARPOS (*it)));
8528 result = MOVE_LINE_CONTINUED;
8529 break;
8530 }
8531
8532 if (BUFFER_POS_REACHED_P ())
8533 {
8534 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8535 goto buffer_pos_reached;
8536 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8537 {
8538 SAVE_IT (atpos_it, *it, atpos_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8540 }
8541 }
8542
8543 if (new_x > it->first_visible_x)
8544 {
8545 /* Glyph is visible. Increment number of glyphs that
8546 would be displayed. */
8547 ++it->hpos;
8548 }
8549 }
8550
8551 if (result != MOVE_UNDEFINED)
8552 break;
8553 }
8554 else if (BUFFER_POS_REACHED_P ())
8555 {
8556 buffer_pos_reached:
8557 IT_RESET_X_ASCENT_DESCENT (it);
8558 result = MOVE_POS_MATCH_OR_ZV;
8559 break;
8560 }
8561 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8562 {
8563 /* Stop when TO_X specified and reached. This check is
8564 necessary here because of lines consisting of a line end,
8565 only. The line end will not produce any glyphs and we
8566 would never get MOVE_X_REACHED. */
8567 eassert (it->nglyphs == 0);
8568 result = MOVE_X_REACHED;
8569 break;
8570 }
8571
8572 /* Is this a line end? If yes, we're done. */
8573 if (ITERATOR_AT_END_OF_LINE_P (it))
8574 {
8575 /* If we are past TO_CHARPOS, but never saw any character
8576 positions smaller than TO_CHARPOS, return
8577 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8578 did. */
8579 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8580 {
8581 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8582 {
8583 if (IT_CHARPOS (ppos_it) < ZV)
8584 {
8585 RESTORE_IT (it, &ppos_it, ppos_data);
8586 result = MOVE_POS_MATCH_OR_ZV;
8587 }
8588 else
8589 goto buffer_pos_reached;
8590 }
8591 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8592 && IT_CHARPOS (*it) > to_charpos)
8593 goto buffer_pos_reached;
8594 else
8595 result = MOVE_NEWLINE_OR_CR;
8596 }
8597 else
8598 result = MOVE_NEWLINE_OR_CR;
8599 break;
8600 }
8601
8602 prev_method = it->method;
8603 if (it->method == GET_FROM_BUFFER)
8604 prev_pos = IT_CHARPOS (*it);
8605 /* The current display element has been consumed. Advance
8606 to the next. */
8607 set_iterator_to_next (it, 1);
8608 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8609 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8610 if (IT_CHARPOS (*it) < to_charpos)
8611 saw_smaller_pos = 1;
8612 if (it->bidi_p
8613 && (op & MOVE_TO_POS)
8614 && IT_CHARPOS (*it) >= to_charpos
8615 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8616 SAVE_IT (ppos_it, *it, ppos_data);
8617
8618 /* Stop if lines are truncated and IT's current x-position is
8619 past the right edge of the window now. */
8620 if (it->line_wrap == TRUNCATE
8621 && it->current_x >= it->last_visible_x)
8622 {
8623 if (!FRAME_WINDOW_P (it->f)
8624 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8627 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8628 {
8629 int at_eob_p = 0;
8630
8631 if ((at_eob_p = !get_next_display_element (it))
8632 || BUFFER_POS_REACHED_P ()
8633 /* If we are past TO_CHARPOS, but never saw any
8634 character positions smaller than TO_CHARPOS,
8635 return MOVE_POS_MATCH_OR_ZV, like the
8636 unidirectional display did. */
8637 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8638 && !saw_smaller_pos
8639 && IT_CHARPOS (*it) > to_charpos))
8640 {
8641 if (it->bidi_p
8642 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8643 RESTORE_IT (it, &ppos_it, ppos_data);
8644 result = MOVE_POS_MATCH_OR_ZV;
8645 break;
8646 }
8647 if (ITERATOR_AT_END_OF_LINE_P (it))
8648 {
8649 result = MOVE_NEWLINE_OR_CR;
8650 break;
8651 }
8652 }
8653 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8654 && !saw_smaller_pos
8655 && IT_CHARPOS (*it) > to_charpos)
8656 {
8657 if (IT_CHARPOS (ppos_it) < ZV)
8658 RESTORE_IT (it, &ppos_it, ppos_data);
8659 result = MOVE_POS_MATCH_OR_ZV;
8660 break;
8661 }
8662 result = MOVE_LINE_TRUNCATED;
8663 break;
8664 }
8665 #undef IT_RESET_X_ASCENT_DESCENT
8666 }
8667
8668 #undef BUFFER_POS_REACHED_P
8669
8670 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8671 restore the saved iterator. */
8672 if (atpos_it.sp >= 0)
8673 RESTORE_IT (it, &atpos_it, atpos_data);
8674 else if (atx_it.sp >= 0)
8675 RESTORE_IT (it, &atx_it, atx_data);
8676
8677 done:
8678
8679 if (atpos_data)
8680 bidi_unshelve_cache (atpos_data, 1);
8681 if (atx_data)
8682 bidi_unshelve_cache (atx_data, 1);
8683 if (wrap_data)
8684 bidi_unshelve_cache (wrap_data, 1);
8685 if (ppos_data)
8686 bidi_unshelve_cache (ppos_data, 1);
8687
8688 /* Restore the iterator settings altered at the beginning of this
8689 function. */
8690 it->glyph_row = saved_glyph_row;
8691 return result;
8692 }
8693
8694 /* For external use. */
8695 void
8696 move_it_in_display_line (struct it *it,
8697 ptrdiff_t to_charpos, int to_x,
8698 enum move_operation_enum op)
8699 {
8700 if (it->line_wrap == WORD_WRAP
8701 && (op & MOVE_TO_X))
8702 {
8703 struct it save_it;
8704 void *save_data = NULL;
8705 int skip;
8706
8707 SAVE_IT (save_it, *it, save_data);
8708 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8709 /* When word-wrap is on, TO_X may lie past the end
8710 of a wrapped line. Then it->current is the
8711 character on the next line, so backtrack to the
8712 space before the wrap point. */
8713 if (skip == MOVE_LINE_CONTINUED)
8714 {
8715 int prev_x = max (it->current_x - 1, 0);
8716 RESTORE_IT (it, &save_it, save_data);
8717 move_it_in_display_line_to
8718 (it, -1, prev_x, MOVE_TO_X);
8719 }
8720 else
8721 bidi_unshelve_cache (save_data, 1);
8722 }
8723 else
8724 move_it_in_display_line_to (it, to_charpos, to_x, op);
8725 }
8726
8727
8728 /* Move IT forward until it satisfies one or more of the criteria in
8729 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8730
8731 OP is a bit-mask that specifies where to stop, and in particular,
8732 which of those four position arguments makes a difference. See the
8733 description of enum move_operation_enum.
8734
8735 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8736 screen line, this function will set IT to the next position that is
8737 displayed to the right of TO_CHARPOS on the screen. */
8738
8739 void
8740 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8741 {
8742 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8743 int line_height, line_start_x = 0, reached = 0;
8744 void *backup_data = NULL;
8745
8746 for (;;)
8747 {
8748 if (op & MOVE_TO_VPOS)
8749 {
8750 /* If no TO_CHARPOS and no TO_X specified, stop at the
8751 start of the line TO_VPOS. */
8752 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8753 {
8754 if (it->vpos == to_vpos)
8755 {
8756 reached = 1;
8757 break;
8758 }
8759 else
8760 skip = move_it_in_display_line_to (it, -1, -1, 0);
8761 }
8762 else
8763 {
8764 /* TO_VPOS >= 0 means stop at TO_X in the line at
8765 TO_VPOS, or at TO_POS, whichever comes first. */
8766 if (it->vpos == to_vpos)
8767 {
8768 reached = 2;
8769 break;
8770 }
8771
8772 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8773
8774 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8775 {
8776 reached = 3;
8777 break;
8778 }
8779 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8780 {
8781 /* We have reached TO_X but not in the line we want. */
8782 skip = move_it_in_display_line_to (it, to_charpos,
8783 -1, MOVE_TO_POS);
8784 if (skip == MOVE_POS_MATCH_OR_ZV)
8785 {
8786 reached = 4;
8787 break;
8788 }
8789 }
8790 }
8791 }
8792 else if (op & MOVE_TO_Y)
8793 {
8794 struct it it_backup;
8795
8796 if (it->line_wrap == WORD_WRAP)
8797 SAVE_IT (it_backup, *it, backup_data);
8798
8799 /* TO_Y specified means stop at TO_X in the line containing
8800 TO_Y---or at TO_CHARPOS if this is reached first. The
8801 problem is that we can't really tell whether the line
8802 contains TO_Y before we have completely scanned it, and
8803 this may skip past TO_X. What we do is to first scan to
8804 TO_X.
8805
8806 If TO_X is not specified, use a TO_X of zero. The reason
8807 is to make the outcome of this function more predictable.
8808 If we didn't use TO_X == 0, we would stop at the end of
8809 the line which is probably not what a caller would expect
8810 to happen. */
8811 skip = move_it_in_display_line_to
8812 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8813 (MOVE_TO_X | (op & MOVE_TO_POS)));
8814
8815 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8816 if (skip == MOVE_POS_MATCH_OR_ZV)
8817 reached = 5;
8818 else if (skip == MOVE_X_REACHED)
8819 {
8820 /* If TO_X was reached, we want to know whether TO_Y is
8821 in the line. We know this is the case if the already
8822 scanned glyphs make the line tall enough. Otherwise,
8823 we must check by scanning the rest of the line. */
8824 line_height = it->max_ascent + it->max_descent;
8825 if (to_y >= it->current_y
8826 && to_y < it->current_y + line_height)
8827 {
8828 reached = 6;
8829 break;
8830 }
8831 SAVE_IT (it_backup, *it, backup_data);
8832 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8833 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8834 op & MOVE_TO_POS);
8835 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8836 line_height = it->max_ascent + it->max_descent;
8837 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8838
8839 if (to_y >= it->current_y
8840 && to_y < it->current_y + line_height)
8841 {
8842 /* If TO_Y is in this line and TO_X was reached
8843 above, we scanned too far. We have to restore
8844 IT's settings to the ones before skipping. But
8845 keep the more accurate values of max_ascent and
8846 max_descent we've found while skipping the rest
8847 of the line, for the sake of callers, such as
8848 pos_visible_p, that need to know the line
8849 height. */
8850 int max_ascent = it->max_ascent;
8851 int max_descent = it->max_descent;
8852
8853 RESTORE_IT (it, &it_backup, backup_data);
8854 it->max_ascent = max_ascent;
8855 it->max_descent = max_descent;
8856 reached = 6;
8857 }
8858 else
8859 {
8860 skip = skip2;
8861 if (skip == MOVE_POS_MATCH_OR_ZV)
8862 reached = 7;
8863 }
8864 }
8865 else
8866 {
8867 /* Check whether TO_Y is in this line. */
8868 line_height = it->max_ascent + it->max_descent;
8869 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8870
8871 if (to_y >= it->current_y
8872 && to_y < it->current_y + line_height)
8873 {
8874 /* When word-wrap is on, TO_X may lie past the end
8875 of a wrapped line. Then it->current is the
8876 character on the next line, so backtrack to the
8877 space before the wrap point. */
8878 if (skip == MOVE_LINE_CONTINUED
8879 && it->line_wrap == WORD_WRAP)
8880 {
8881 int prev_x = max (it->current_x - 1, 0);
8882 RESTORE_IT (it, &it_backup, backup_data);
8883 skip = move_it_in_display_line_to
8884 (it, -1, prev_x, MOVE_TO_X);
8885 }
8886 reached = 6;
8887 }
8888 }
8889
8890 if (reached)
8891 break;
8892 }
8893 else if (BUFFERP (it->object)
8894 && (it->method == GET_FROM_BUFFER
8895 || it->method == GET_FROM_STRETCH)
8896 && IT_CHARPOS (*it) >= to_charpos
8897 /* Under bidi iteration, a call to set_iterator_to_next
8898 can scan far beyond to_charpos if the initial
8899 portion of the next line needs to be reordered. In
8900 that case, give move_it_in_display_line_to another
8901 chance below. */
8902 && !(it->bidi_p
8903 && it->bidi_it.scan_dir == -1))
8904 skip = MOVE_POS_MATCH_OR_ZV;
8905 else
8906 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8907
8908 switch (skip)
8909 {
8910 case MOVE_POS_MATCH_OR_ZV:
8911 reached = 8;
8912 goto out;
8913
8914 case MOVE_NEWLINE_OR_CR:
8915 set_iterator_to_next (it, 1);
8916 it->continuation_lines_width = 0;
8917 break;
8918
8919 case MOVE_LINE_TRUNCATED:
8920 it->continuation_lines_width = 0;
8921 reseat_at_next_visible_line_start (it, 0);
8922 if ((op & MOVE_TO_POS) != 0
8923 && IT_CHARPOS (*it) > to_charpos)
8924 {
8925 reached = 9;
8926 goto out;
8927 }
8928 break;
8929
8930 case MOVE_LINE_CONTINUED:
8931 /* For continued lines ending in a tab, some of the glyphs
8932 associated with the tab are displayed on the current
8933 line. Since it->current_x does not include these glyphs,
8934 we use it->last_visible_x instead. */
8935 if (it->c == '\t')
8936 {
8937 it->continuation_lines_width += it->last_visible_x;
8938 /* When moving by vpos, ensure that the iterator really
8939 advances to the next line (bug#847, bug#969). Fixme:
8940 do we need to do this in other circumstances? */
8941 if (it->current_x != it->last_visible_x
8942 && (op & MOVE_TO_VPOS)
8943 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8944 {
8945 line_start_x = it->current_x + it->pixel_width
8946 - it->last_visible_x;
8947 set_iterator_to_next (it, 0);
8948 }
8949 }
8950 else
8951 it->continuation_lines_width += it->current_x;
8952 break;
8953
8954 default:
8955 emacs_abort ();
8956 }
8957
8958 /* Reset/increment for the next run. */
8959 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8960 it->current_x = line_start_x;
8961 line_start_x = 0;
8962 it->hpos = 0;
8963 it->current_y += it->max_ascent + it->max_descent;
8964 ++it->vpos;
8965 last_height = it->max_ascent + it->max_descent;
8966 last_max_ascent = it->max_ascent;
8967 it->max_ascent = it->max_descent = 0;
8968 }
8969
8970 out:
8971
8972 /* On text terminals, we may stop at the end of a line in the middle
8973 of a multi-character glyph. If the glyph itself is continued,
8974 i.e. it is actually displayed on the next line, don't treat this
8975 stopping point as valid; move to the next line instead (unless
8976 that brings us offscreen). */
8977 if (!FRAME_WINDOW_P (it->f)
8978 && op & MOVE_TO_POS
8979 && IT_CHARPOS (*it) == to_charpos
8980 && it->what == IT_CHARACTER
8981 && it->nglyphs > 1
8982 && it->line_wrap == WINDOW_WRAP
8983 && it->current_x == it->last_visible_x - 1
8984 && it->c != '\n'
8985 && it->c != '\t'
8986 && it->vpos < XFASTINT (it->w->window_end_vpos))
8987 {
8988 it->continuation_lines_width += it->current_x;
8989 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8990 it->current_y += it->max_ascent + it->max_descent;
8991 ++it->vpos;
8992 last_height = it->max_ascent + it->max_descent;
8993 last_max_ascent = it->max_ascent;
8994 }
8995
8996 if (backup_data)
8997 bidi_unshelve_cache (backup_data, 1);
8998
8999 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9000 }
9001
9002
9003 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9004
9005 If DY > 0, move IT backward at least that many pixels. DY = 0
9006 means move IT backward to the preceding line start or BEGV. This
9007 function may move over more than DY pixels if IT->current_y - DY
9008 ends up in the middle of a line; in this case IT->current_y will be
9009 set to the top of the line moved to. */
9010
9011 void
9012 move_it_vertically_backward (struct it *it, int dy)
9013 {
9014 int nlines, h;
9015 struct it it2, it3;
9016 void *it2data = NULL, *it3data = NULL;
9017 ptrdiff_t start_pos;
9018
9019 move_further_back:
9020 eassert (dy >= 0);
9021
9022 start_pos = IT_CHARPOS (*it);
9023
9024 /* Estimate how many newlines we must move back. */
9025 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9026
9027 /* Set the iterator's position that many lines back. */
9028 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9029 back_to_previous_visible_line_start (it);
9030
9031 /* Reseat the iterator here. When moving backward, we don't want
9032 reseat to skip forward over invisible text, set up the iterator
9033 to deliver from overlay strings at the new position etc. So,
9034 use reseat_1 here. */
9035 reseat_1 (it, it->current.pos, 1);
9036
9037 /* We are now surely at a line start. */
9038 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9039 reordering is in effect. */
9040 it->continuation_lines_width = 0;
9041
9042 /* Move forward and see what y-distance we moved. First move to the
9043 start of the next line so that we get its height. We need this
9044 height to be able to tell whether we reached the specified
9045 y-distance. */
9046 SAVE_IT (it2, *it, it2data);
9047 it2.max_ascent = it2.max_descent = 0;
9048 do
9049 {
9050 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9051 MOVE_TO_POS | MOVE_TO_VPOS);
9052 }
9053 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9054 /* If we are in a display string which starts at START_POS,
9055 and that display string includes a newline, and we are
9056 right after that newline (i.e. at the beginning of a
9057 display line), exit the loop, because otherwise we will
9058 infloop, since move_it_to will see that it is already at
9059 START_POS and will not move. */
9060 || (it2.method == GET_FROM_STRING
9061 && IT_CHARPOS (it2) == start_pos
9062 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9063 eassert (IT_CHARPOS (*it) >= BEGV);
9064 SAVE_IT (it3, it2, it3data);
9065
9066 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9067 eassert (IT_CHARPOS (*it) >= BEGV);
9068 /* H is the actual vertical distance from the position in *IT
9069 and the starting position. */
9070 h = it2.current_y - it->current_y;
9071 /* NLINES is the distance in number of lines. */
9072 nlines = it2.vpos - it->vpos;
9073
9074 /* Correct IT's y and vpos position
9075 so that they are relative to the starting point. */
9076 it->vpos -= nlines;
9077 it->current_y -= h;
9078
9079 if (dy == 0)
9080 {
9081 /* DY == 0 means move to the start of the screen line. The
9082 value of nlines is > 0 if continuation lines were involved,
9083 or if the original IT position was at start of a line. */
9084 RESTORE_IT (it, it, it2data);
9085 if (nlines > 0)
9086 move_it_by_lines (it, nlines);
9087 /* The above code moves us to some position NLINES down,
9088 usually to its first glyph (leftmost in an L2R line), but
9089 that's not necessarily the start of the line, under bidi
9090 reordering. We want to get to the character position
9091 that is immediately after the newline of the previous
9092 line. */
9093 if (it->bidi_p
9094 && !it->continuation_lines_width
9095 && !STRINGP (it->string)
9096 && IT_CHARPOS (*it) > BEGV
9097 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9098 {
9099 ptrdiff_t nl_pos =
9100 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9101
9102 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9103 }
9104 bidi_unshelve_cache (it3data, 1);
9105 }
9106 else
9107 {
9108 /* The y-position we try to reach, relative to *IT.
9109 Note that H has been subtracted in front of the if-statement. */
9110 int target_y = it->current_y + h - dy;
9111 int y0 = it3.current_y;
9112 int y1;
9113 int line_height;
9114
9115 RESTORE_IT (&it3, &it3, it3data);
9116 y1 = line_bottom_y (&it3);
9117 line_height = y1 - y0;
9118 RESTORE_IT (it, it, it2data);
9119 /* If we did not reach target_y, try to move further backward if
9120 we can. If we moved too far backward, try to move forward. */
9121 if (target_y < it->current_y
9122 /* This is heuristic. In a window that's 3 lines high, with
9123 a line height of 13 pixels each, recentering with point
9124 on the bottom line will try to move -39/2 = 19 pixels
9125 backward. Try to avoid moving into the first line. */
9126 && (it->current_y - target_y
9127 > min (window_box_height (it->w), line_height * 2 / 3))
9128 && IT_CHARPOS (*it) > BEGV)
9129 {
9130 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9131 target_y - it->current_y));
9132 dy = it->current_y - target_y;
9133 goto move_further_back;
9134 }
9135 else if (target_y >= it->current_y + line_height
9136 && IT_CHARPOS (*it) < ZV)
9137 {
9138 /* Should move forward by at least one line, maybe more.
9139
9140 Note: Calling move_it_by_lines can be expensive on
9141 terminal frames, where compute_motion is used (via
9142 vmotion) to do the job, when there are very long lines
9143 and truncate-lines is nil. That's the reason for
9144 treating terminal frames specially here. */
9145
9146 if (!FRAME_WINDOW_P (it->f))
9147 move_it_vertically (it, target_y - (it->current_y + line_height));
9148 else
9149 {
9150 do
9151 {
9152 move_it_by_lines (it, 1);
9153 }
9154 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9155 }
9156 }
9157 }
9158 }
9159
9160
9161 /* Move IT by a specified amount of pixel lines DY. DY negative means
9162 move backwards. DY = 0 means move to start of screen line. At the
9163 end, IT will be on the start of a screen line. */
9164
9165 void
9166 move_it_vertically (struct it *it, int dy)
9167 {
9168 if (dy <= 0)
9169 move_it_vertically_backward (it, -dy);
9170 else
9171 {
9172 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9173 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9174 MOVE_TO_POS | MOVE_TO_Y);
9175 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9176
9177 /* If buffer ends in ZV without a newline, move to the start of
9178 the line to satisfy the post-condition. */
9179 if (IT_CHARPOS (*it) == ZV
9180 && ZV > BEGV
9181 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9182 move_it_by_lines (it, 0);
9183 }
9184 }
9185
9186
9187 /* Move iterator IT past the end of the text line it is in. */
9188
9189 void
9190 move_it_past_eol (struct it *it)
9191 {
9192 enum move_it_result rc;
9193
9194 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9195 if (rc == MOVE_NEWLINE_OR_CR)
9196 set_iterator_to_next (it, 0);
9197 }
9198
9199
9200 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9201 negative means move up. DVPOS == 0 means move to the start of the
9202 screen line.
9203
9204 Optimization idea: If we would know that IT->f doesn't use
9205 a face with proportional font, we could be faster for
9206 truncate-lines nil. */
9207
9208 void
9209 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9210 {
9211
9212 /* The commented-out optimization uses vmotion on terminals. This
9213 gives bad results, because elements like it->what, on which
9214 callers such as pos_visible_p rely, aren't updated. */
9215 /* struct position pos;
9216 if (!FRAME_WINDOW_P (it->f))
9217 {
9218 struct text_pos textpos;
9219
9220 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9221 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9222 reseat (it, textpos, 1);
9223 it->vpos += pos.vpos;
9224 it->current_y += pos.vpos;
9225 }
9226 else */
9227
9228 if (dvpos == 0)
9229 {
9230 /* DVPOS == 0 means move to the start of the screen line. */
9231 move_it_vertically_backward (it, 0);
9232 /* Let next call to line_bottom_y calculate real line height */
9233 last_height = 0;
9234 }
9235 else if (dvpos > 0)
9236 {
9237 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9238 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9239 {
9240 /* Only move to the next buffer position if we ended up in a
9241 string from display property, not in an overlay string
9242 (before-string or after-string). That is because the
9243 latter don't conceal the underlying buffer position, so
9244 we can ask to move the iterator to the exact position we
9245 are interested in. Note that, even if we are already at
9246 IT_CHARPOS (*it), the call below is not a no-op, as it
9247 will detect that we are at the end of the string, pop the
9248 iterator, and compute it->current_x and it->hpos
9249 correctly. */
9250 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9251 -1, -1, -1, MOVE_TO_POS);
9252 }
9253 }
9254 else
9255 {
9256 struct it it2;
9257 void *it2data = NULL;
9258 ptrdiff_t start_charpos, i;
9259
9260 /* Start at the beginning of the screen line containing IT's
9261 position. This may actually move vertically backwards,
9262 in case of overlays, so adjust dvpos accordingly. */
9263 dvpos += it->vpos;
9264 move_it_vertically_backward (it, 0);
9265 dvpos -= it->vpos;
9266
9267 /* Go back -DVPOS visible lines and reseat the iterator there. */
9268 start_charpos = IT_CHARPOS (*it);
9269 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9270 back_to_previous_visible_line_start (it);
9271 reseat (it, it->current.pos, 1);
9272
9273 /* Move further back if we end up in a string or an image. */
9274 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9275 {
9276 /* First try to move to start of display line. */
9277 dvpos += it->vpos;
9278 move_it_vertically_backward (it, 0);
9279 dvpos -= it->vpos;
9280 if (IT_POS_VALID_AFTER_MOVE_P (it))
9281 break;
9282 /* If start of line is still in string or image,
9283 move further back. */
9284 back_to_previous_visible_line_start (it);
9285 reseat (it, it->current.pos, 1);
9286 dvpos--;
9287 }
9288
9289 it->current_x = it->hpos = 0;
9290
9291 /* Above call may have moved too far if continuation lines
9292 are involved. Scan forward and see if it did. */
9293 SAVE_IT (it2, *it, it2data);
9294 it2.vpos = it2.current_y = 0;
9295 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9296 it->vpos -= it2.vpos;
9297 it->current_y -= it2.current_y;
9298 it->current_x = it->hpos = 0;
9299
9300 /* If we moved too far back, move IT some lines forward. */
9301 if (it2.vpos > -dvpos)
9302 {
9303 int delta = it2.vpos + dvpos;
9304
9305 RESTORE_IT (&it2, &it2, it2data);
9306 SAVE_IT (it2, *it, it2data);
9307 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9308 /* Move back again if we got too far ahead. */
9309 if (IT_CHARPOS (*it) >= start_charpos)
9310 RESTORE_IT (it, &it2, it2data);
9311 else
9312 bidi_unshelve_cache (it2data, 1);
9313 }
9314 else
9315 RESTORE_IT (it, it, it2data);
9316 }
9317 }
9318
9319 /* Return 1 if IT points into the middle of a display vector. */
9320
9321 int
9322 in_display_vector_p (struct it *it)
9323 {
9324 return (it->method == GET_FROM_DISPLAY_VECTOR
9325 && it->current.dpvec_index > 0
9326 && it->dpvec + it->current.dpvec_index != it->dpend);
9327 }
9328
9329 \f
9330 /***********************************************************************
9331 Messages
9332 ***********************************************************************/
9333
9334
9335 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9336 to *Messages*. */
9337
9338 void
9339 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9340 {
9341 Lisp_Object args[3];
9342 Lisp_Object msg, fmt;
9343 char *buffer;
9344 ptrdiff_t len;
9345 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9346 USE_SAFE_ALLOCA;
9347
9348 fmt = msg = Qnil;
9349 GCPRO4 (fmt, msg, arg1, arg2);
9350
9351 args[0] = fmt = build_string (format);
9352 args[1] = arg1;
9353 args[2] = arg2;
9354 msg = Fformat (3, args);
9355
9356 len = SBYTES (msg) + 1;
9357 buffer = SAFE_ALLOCA (len);
9358 memcpy (buffer, SDATA (msg), len);
9359
9360 message_dolog (buffer, len - 1, 1, 0);
9361 SAFE_FREE ();
9362
9363 UNGCPRO;
9364 }
9365
9366
9367 /* Output a newline in the *Messages* buffer if "needs" one. */
9368
9369 void
9370 message_log_maybe_newline (void)
9371 {
9372 if (message_log_need_newline)
9373 message_dolog ("", 0, 1, 0);
9374 }
9375
9376
9377 /* Add a string M of length NBYTES to the message log, optionally
9378 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9379 nonzero, means interpret the contents of M as multibyte. This
9380 function calls low-level routines in order to bypass text property
9381 hooks, etc. which might not be safe to run.
9382
9383 This may GC (insert may run before/after change hooks),
9384 so the buffer M must NOT point to a Lisp string. */
9385
9386 void
9387 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9388 {
9389 const unsigned char *msg = (const unsigned char *) m;
9390
9391 if (!NILP (Vmemory_full))
9392 return;
9393
9394 if (!NILP (Vmessage_log_max))
9395 {
9396 struct buffer *oldbuf;
9397 Lisp_Object oldpoint, oldbegv, oldzv;
9398 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9399 ptrdiff_t point_at_end = 0;
9400 ptrdiff_t zv_at_end = 0;
9401 Lisp_Object old_deactivate_mark, tem;
9402 struct gcpro gcpro1;
9403
9404 old_deactivate_mark = Vdeactivate_mark;
9405 oldbuf = current_buffer;
9406 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9407 bset_undo_list (current_buffer, Qt);
9408
9409 oldpoint = message_dolog_marker1;
9410 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9411 oldbegv = message_dolog_marker2;
9412 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9413 oldzv = message_dolog_marker3;
9414 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9415 GCPRO1 (old_deactivate_mark);
9416
9417 if (PT == Z)
9418 point_at_end = 1;
9419 if (ZV == Z)
9420 zv_at_end = 1;
9421
9422 BEGV = BEG;
9423 BEGV_BYTE = BEG_BYTE;
9424 ZV = Z;
9425 ZV_BYTE = Z_BYTE;
9426 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9427
9428 /* Insert the string--maybe converting multibyte to single byte
9429 or vice versa, so that all the text fits the buffer. */
9430 if (multibyte
9431 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9432 {
9433 ptrdiff_t i;
9434 int c, char_bytes;
9435 char work[1];
9436
9437 /* Convert a multibyte string to single-byte
9438 for the *Message* buffer. */
9439 for (i = 0; i < nbytes; i += char_bytes)
9440 {
9441 c = string_char_and_length (msg + i, &char_bytes);
9442 work[0] = (ASCII_CHAR_P (c)
9443 ? c
9444 : multibyte_char_to_unibyte (c));
9445 insert_1_both (work, 1, 1, 1, 0, 0);
9446 }
9447 }
9448 else if (! multibyte
9449 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9450 {
9451 ptrdiff_t i;
9452 int c, char_bytes;
9453 unsigned char str[MAX_MULTIBYTE_LENGTH];
9454 /* Convert a single-byte string to multibyte
9455 for the *Message* buffer. */
9456 for (i = 0; i < nbytes; i++)
9457 {
9458 c = msg[i];
9459 MAKE_CHAR_MULTIBYTE (c);
9460 char_bytes = CHAR_STRING (c, str);
9461 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9462 }
9463 }
9464 else if (nbytes)
9465 insert_1 (m, nbytes, 1, 0, 0);
9466
9467 if (nlflag)
9468 {
9469 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9470 printmax_t dups;
9471 insert_1 ("\n", 1, 1, 0, 0);
9472
9473 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9474 this_bol = PT;
9475 this_bol_byte = PT_BYTE;
9476
9477 /* See if this line duplicates the previous one.
9478 If so, combine duplicates. */
9479 if (this_bol > BEG)
9480 {
9481 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9482 prev_bol = PT;
9483 prev_bol_byte = PT_BYTE;
9484
9485 dups = message_log_check_duplicate (prev_bol_byte,
9486 this_bol_byte);
9487 if (dups)
9488 {
9489 del_range_both (prev_bol, prev_bol_byte,
9490 this_bol, this_bol_byte, 0);
9491 if (dups > 1)
9492 {
9493 char dupstr[sizeof " [ times]"
9494 + INT_STRLEN_BOUND (printmax_t)];
9495
9496 /* If you change this format, don't forget to also
9497 change message_log_check_duplicate. */
9498 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9499 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9500 insert_1 (dupstr, duplen, 1, 0, 1);
9501 }
9502 }
9503 }
9504
9505 /* If we have more than the desired maximum number of lines
9506 in the *Messages* buffer now, delete the oldest ones.
9507 This is safe because we don't have undo in this buffer. */
9508
9509 if (NATNUMP (Vmessage_log_max))
9510 {
9511 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9512 -XFASTINT (Vmessage_log_max) - 1, 0);
9513 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9514 }
9515 }
9516 BEGV = marker_position (oldbegv);
9517 BEGV_BYTE = marker_byte_position (oldbegv);
9518
9519 if (zv_at_end)
9520 {
9521 ZV = Z;
9522 ZV_BYTE = Z_BYTE;
9523 }
9524 else
9525 {
9526 ZV = marker_position (oldzv);
9527 ZV_BYTE = marker_byte_position (oldzv);
9528 }
9529
9530 if (point_at_end)
9531 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9532 else
9533 /* We can't do Fgoto_char (oldpoint) because it will run some
9534 Lisp code. */
9535 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9536 marker_byte_position (oldpoint));
9537
9538 UNGCPRO;
9539 unchain_marker (XMARKER (oldpoint));
9540 unchain_marker (XMARKER (oldbegv));
9541 unchain_marker (XMARKER (oldzv));
9542
9543 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9544 set_buffer_internal (oldbuf);
9545 if (NILP (tem))
9546 windows_or_buffers_changed = old_windows_or_buffers_changed;
9547 message_log_need_newline = !nlflag;
9548 Vdeactivate_mark = old_deactivate_mark;
9549 }
9550 }
9551
9552
9553 /* We are at the end of the buffer after just having inserted a newline.
9554 (Note: We depend on the fact we won't be crossing the gap.)
9555 Check to see if the most recent message looks a lot like the previous one.
9556 Return 0 if different, 1 if the new one should just replace it, or a
9557 value N > 1 if we should also append " [N times]". */
9558
9559 static intmax_t
9560 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9561 {
9562 ptrdiff_t i;
9563 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9564 int seen_dots = 0;
9565 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9566 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9567
9568 for (i = 0; i < len; i++)
9569 {
9570 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9571 seen_dots = 1;
9572 if (p1[i] != p2[i])
9573 return seen_dots;
9574 }
9575 p1 += len;
9576 if (*p1 == '\n')
9577 return 2;
9578 if (*p1++ == ' ' && *p1++ == '[')
9579 {
9580 char *pend;
9581 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9582 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9583 return n+1;
9584 }
9585 return 0;
9586 }
9587 \f
9588
9589 /* Display an echo area message M with a specified length of NBYTES
9590 bytes. The string may include null characters. If M is 0, clear
9591 out any existing message, and let the mini-buffer text show
9592 through.
9593
9594 This may GC, so the buffer M must NOT point to a Lisp string. */
9595
9596 void
9597 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9598 {
9599 /* First flush out any partial line written with print. */
9600 message_log_maybe_newline ();
9601 if (m)
9602 message_dolog (m, nbytes, 1, multibyte);
9603 message2_nolog (m, nbytes, multibyte);
9604 }
9605
9606
9607 /* The non-logging counterpart of message2. */
9608
9609 void
9610 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9611 {
9612 struct frame *sf = SELECTED_FRAME ();
9613 message_enable_multibyte = multibyte;
9614
9615 if (FRAME_INITIAL_P (sf))
9616 {
9617 if (noninteractive_need_newline)
9618 putc ('\n', stderr);
9619 noninteractive_need_newline = 0;
9620 if (m)
9621 fwrite (m, nbytes, 1, stderr);
9622 if (cursor_in_echo_area == 0)
9623 fprintf (stderr, "\n");
9624 fflush (stderr);
9625 }
9626 /* A null message buffer means that the frame hasn't really been
9627 initialized yet. Error messages get reported properly by
9628 cmd_error, so this must be just an informative message; toss it. */
9629 else if (INTERACTIVE
9630 && sf->glyphs_initialized_p
9631 && FRAME_MESSAGE_BUF (sf))
9632 {
9633 Lisp_Object mini_window;
9634 struct frame *f;
9635
9636 /* Get the frame containing the mini-buffer
9637 that the selected frame is using. */
9638 mini_window = FRAME_MINIBUF_WINDOW (sf);
9639 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9640
9641 FRAME_SAMPLE_VISIBILITY (f);
9642 if (FRAME_VISIBLE_P (sf)
9643 && ! FRAME_VISIBLE_P (f))
9644 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9645
9646 if (m)
9647 {
9648 set_message (m, Qnil, nbytes, multibyte);
9649 if (minibuffer_auto_raise)
9650 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9651 }
9652 else
9653 clear_message (1, 1);
9654
9655 do_pending_window_change (0);
9656 echo_area_display (1);
9657 do_pending_window_change (0);
9658 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9659 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9660 }
9661 }
9662
9663
9664 /* Display an echo area message M with a specified length of NBYTES
9665 bytes. The string may include null characters. If M is not a
9666 string, clear out any existing message, and let the mini-buffer
9667 text show through.
9668
9669 This function cancels echoing. */
9670
9671 void
9672 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9673 {
9674 struct gcpro gcpro1;
9675
9676 GCPRO1 (m);
9677 clear_message (1,1);
9678 cancel_echoing ();
9679
9680 /* First flush out any partial line written with print. */
9681 message_log_maybe_newline ();
9682 if (STRINGP (m))
9683 {
9684 USE_SAFE_ALLOCA;
9685 char *buffer = SAFE_ALLOCA (nbytes);
9686 memcpy (buffer, SDATA (m), nbytes);
9687 message_dolog (buffer, nbytes, 1, multibyte);
9688 SAFE_FREE ();
9689 }
9690 message3_nolog (m, nbytes, multibyte);
9691
9692 UNGCPRO;
9693 }
9694
9695
9696 /* The non-logging version of message3.
9697 This does not cancel echoing, because it is used for echoing.
9698 Perhaps we need to make a separate function for echoing
9699 and make this cancel echoing. */
9700
9701 void
9702 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9703 {
9704 struct frame *sf = SELECTED_FRAME ();
9705 message_enable_multibyte = multibyte;
9706
9707 if (FRAME_INITIAL_P (sf))
9708 {
9709 if (noninteractive_need_newline)
9710 putc ('\n', stderr);
9711 noninteractive_need_newline = 0;
9712 if (STRINGP (m))
9713 fwrite (SDATA (m), nbytes, 1, stderr);
9714 if (cursor_in_echo_area == 0)
9715 fprintf (stderr, "\n");
9716 fflush (stderr);
9717 }
9718 /* A null message buffer means that the frame hasn't really been
9719 initialized yet. Error messages get reported properly by
9720 cmd_error, so this must be just an informative message; toss it. */
9721 else if (INTERACTIVE
9722 && sf->glyphs_initialized_p
9723 && FRAME_MESSAGE_BUF (sf))
9724 {
9725 Lisp_Object mini_window;
9726 Lisp_Object frame;
9727 struct frame *f;
9728
9729 /* Get the frame containing the mini-buffer
9730 that the selected frame is using. */
9731 mini_window = FRAME_MINIBUF_WINDOW (sf);
9732 frame = XWINDOW (mini_window)->frame;
9733 f = XFRAME (frame);
9734
9735 FRAME_SAMPLE_VISIBILITY (f);
9736 if (FRAME_VISIBLE_P (sf)
9737 && !FRAME_VISIBLE_P (f))
9738 Fmake_frame_visible (frame);
9739
9740 if (STRINGP (m) && SCHARS (m) > 0)
9741 {
9742 set_message (NULL, m, nbytes, multibyte);
9743 if (minibuffer_auto_raise)
9744 Fraise_frame (frame);
9745 /* Assume we are not echoing.
9746 (If we are, echo_now will override this.) */
9747 echo_message_buffer = Qnil;
9748 }
9749 else
9750 clear_message (1, 1);
9751
9752 do_pending_window_change (0);
9753 echo_area_display (1);
9754 do_pending_window_change (0);
9755 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9756 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9757 }
9758 }
9759
9760
9761 /* Display a null-terminated echo area message M. If M is 0, clear
9762 out any existing message, and let the mini-buffer text show through.
9763
9764 The buffer M must continue to exist until after the echo area gets
9765 cleared or some other message gets displayed there. Do not pass
9766 text that is stored in a Lisp string. Do not pass text in a buffer
9767 that was alloca'd. */
9768
9769 void
9770 message1 (const char *m)
9771 {
9772 message2 (m, (m ? strlen (m) : 0), 0);
9773 }
9774
9775
9776 /* The non-logging counterpart of message1. */
9777
9778 void
9779 message1_nolog (const char *m)
9780 {
9781 message2_nolog (m, (m ? strlen (m) : 0), 0);
9782 }
9783
9784 /* Display a message M which contains a single %s
9785 which gets replaced with STRING. */
9786
9787 void
9788 message_with_string (const char *m, Lisp_Object string, int log)
9789 {
9790 CHECK_STRING (string);
9791
9792 if (noninteractive)
9793 {
9794 if (m)
9795 {
9796 if (noninteractive_need_newline)
9797 putc ('\n', stderr);
9798 noninteractive_need_newline = 0;
9799 fprintf (stderr, m, SDATA (string));
9800 if (!cursor_in_echo_area)
9801 fprintf (stderr, "\n");
9802 fflush (stderr);
9803 }
9804 }
9805 else if (INTERACTIVE)
9806 {
9807 /* The frame whose minibuffer we're going to display the message on.
9808 It may be larger than the selected frame, so we need
9809 to use its buffer, not the selected frame's buffer. */
9810 Lisp_Object mini_window;
9811 struct frame *f, *sf = SELECTED_FRAME ();
9812
9813 /* Get the frame containing the minibuffer
9814 that the selected frame is using. */
9815 mini_window = FRAME_MINIBUF_WINDOW (sf);
9816 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9817
9818 /* A null message buffer means that the frame hasn't really been
9819 initialized yet. Error messages get reported properly by
9820 cmd_error, so this must be just an informative message; toss it. */
9821 if (FRAME_MESSAGE_BUF (f))
9822 {
9823 Lisp_Object args[2], msg;
9824 struct gcpro gcpro1, gcpro2;
9825
9826 args[0] = build_string (m);
9827 args[1] = msg = string;
9828 GCPRO2 (args[0], msg);
9829 gcpro1.nvars = 2;
9830
9831 msg = Fformat (2, args);
9832
9833 if (log)
9834 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9835 else
9836 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9837
9838 UNGCPRO;
9839
9840 /* Print should start at the beginning of the message
9841 buffer next time. */
9842 message_buf_print = 0;
9843 }
9844 }
9845 }
9846
9847
9848 /* Dump an informative message to the minibuf. If M is 0, clear out
9849 any existing message, and let the mini-buffer text show through. */
9850
9851 static void
9852 vmessage (const char *m, va_list ap)
9853 {
9854 if (noninteractive)
9855 {
9856 if (m)
9857 {
9858 if (noninteractive_need_newline)
9859 putc ('\n', stderr);
9860 noninteractive_need_newline = 0;
9861 vfprintf (stderr, m, ap);
9862 if (cursor_in_echo_area == 0)
9863 fprintf (stderr, "\n");
9864 fflush (stderr);
9865 }
9866 }
9867 else if (INTERACTIVE)
9868 {
9869 /* The frame whose mini-buffer we're going to display the message
9870 on. It may be larger than the selected frame, so we need to
9871 use its buffer, not the selected frame's buffer. */
9872 Lisp_Object mini_window;
9873 struct frame *f, *sf = SELECTED_FRAME ();
9874
9875 /* Get the frame containing the mini-buffer
9876 that the selected frame is using. */
9877 mini_window = FRAME_MINIBUF_WINDOW (sf);
9878 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9879
9880 /* A null message buffer means that the frame hasn't really been
9881 initialized yet. Error messages get reported properly by
9882 cmd_error, so this must be just an informative message; toss
9883 it. */
9884 if (FRAME_MESSAGE_BUF (f))
9885 {
9886 if (m)
9887 {
9888 ptrdiff_t len;
9889
9890 len = doprnt (FRAME_MESSAGE_BUF (f),
9891 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9892
9893 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9894 }
9895 else
9896 message1 (0);
9897
9898 /* Print should start at the beginning of the message
9899 buffer next time. */
9900 message_buf_print = 0;
9901 }
9902 }
9903 }
9904
9905 void
9906 message (const char *m, ...)
9907 {
9908 va_list ap;
9909 va_start (ap, m);
9910 vmessage (m, ap);
9911 va_end (ap);
9912 }
9913
9914
9915 #if 0
9916 /* The non-logging version of message. */
9917
9918 void
9919 message_nolog (const char *m, ...)
9920 {
9921 Lisp_Object old_log_max;
9922 va_list ap;
9923 va_start (ap, m);
9924 old_log_max = Vmessage_log_max;
9925 Vmessage_log_max = Qnil;
9926 vmessage (m, ap);
9927 Vmessage_log_max = old_log_max;
9928 va_end (ap);
9929 }
9930 #endif
9931
9932
9933 /* Display the current message in the current mini-buffer. This is
9934 only called from error handlers in process.c, and is not time
9935 critical. */
9936
9937 void
9938 update_echo_area (void)
9939 {
9940 if (!NILP (echo_area_buffer[0]))
9941 {
9942 Lisp_Object string;
9943 string = Fcurrent_message ();
9944 message3 (string, SBYTES (string),
9945 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9946 }
9947 }
9948
9949
9950 /* Make sure echo area buffers in `echo_buffers' are live.
9951 If they aren't, make new ones. */
9952
9953 static void
9954 ensure_echo_area_buffers (void)
9955 {
9956 int i;
9957
9958 for (i = 0; i < 2; ++i)
9959 if (!BUFFERP (echo_buffer[i])
9960 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9961 {
9962 char name[30];
9963 Lisp_Object old_buffer;
9964 int j;
9965
9966 old_buffer = echo_buffer[i];
9967 echo_buffer[i] = Fget_buffer_create
9968 (make_formatted_string (name, " *Echo Area %d*", i));
9969 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9970 /* to force word wrap in echo area -
9971 it was decided to postpone this*/
9972 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9973
9974 for (j = 0; j < 2; ++j)
9975 if (EQ (old_buffer, echo_area_buffer[j]))
9976 echo_area_buffer[j] = echo_buffer[i];
9977 }
9978 }
9979
9980
9981 /* Call FN with args A1..A4 with either the current or last displayed
9982 echo_area_buffer as current buffer.
9983
9984 WHICH zero means use the current message buffer
9985 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9986 from echo_buffer[] and clear it.
9987
9988 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9989 suitable buffer from echo_buffer[] and clear it.
9990
9991 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9992 that the current message becomes the last displayed one, make
9993 choose a suitable buffer for echo_area_buffer[0], and clear it.
9994
9995 Value is what FN returns. */
9996
9997 static int
9998 with_echo_area_buffer (struct window *w, int which,
9999 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
10000 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10001 {
10002 Lisp_Object buffer;
10003 int this_one, the_other, clear_buffer_p, rc;
10004 ptrdiff_t count = SPECPDL_INDEX ();
10005
10006 /* If buffers aren't live, make new ones. */
10007 ensure_echo_area_buffers ();
10008
10009 clear_buffer_p = 0;
10010
10011 if (which == 0)
10012 this_one = 0, the_other = 1;
10013 else if (which > 0)
10014 this_one = 1, the_other = 0;
10015 else
10016 {
10017 this_one = 0, the_other = 1;
10018 clear_buffer_p = 1;
10019
10020 /* We need a fresh one in case the current echo buffer equals
10021 the one containing the last displayed echo area message. */
10022 if (!NILP (echo_area_buffer[this_one])
10023 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10024 echo_area_buffer[this_one] = Qnil;
10025 }
10026
10027 /* Choose a suitable buffer from echo_buffer[] is we don't
10028 have one. */
10029 if (NILP (echo_area_buffer[this_one]))
10030 {
10031 echo_area_buffer[this_one]
10032 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10033 ? echo_buffer[the_other]
10034 : echo_buffer[this_one]);
10035 clear_buffer_p = 1;
10036 }
10037
10038 buffer = echo_area_buffer[this_one];
10039
10040 /* Don't get confused by reusing the buffer used for echoing
10041 for a different purpose. */
10042 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10043 cancel_echoing ();
10044
10045 record_unwind_protect (unwind_with_echo_area_buffer,
10046 with_echo_area_buffer_unwind_data (w));
10047
10048 /* Make the echo area buffer current. Note that for display
10049 purposes, it is not necessary that the displayed window's buffer
10050 == current_buffer, except for text property lookup. So, let's
10051 only set that buffer temporarily here without doing a full
10052 Fset_window_buffer. We must also change w->pointm, though,
10053 because otherwise an assertions in unshow_buffer fails, and Emacs
10054 aborts. */
10055 set_buffer_internal_1 (XBUFFER (buffer));
10056 if (w)
10057 {
10058 wset_buffer (w, buffer);
10059 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10060 }
10061
10062 bset_undo_list (current_buffer, Qt);
10063 bset_read_only (current_buffer, Qnil);
10064 specbind (Qinhibit_read_only, Qt);
10065 specbind (Qinhibit_modification_hooks, Qt);
10066
10067 if (clear_buffer_p && Z > BEG)
10068 del_range (BEG, Z);
10069
10070 eassert (BEGV >= BEG);
10071 eassert (ZV <= Z && ZV >= BEGV);
10072
10073 rc = fn (a1, a2, a3, a4);
10074
10075 eassert (BEGV >= BEG);
10076 eassert (ZV <= Z && ZV >= BEGV);
10077
10078 unbind_to (count, Qnil);
10079 return rc;
10080 }
10081
10082
10083 /* Save state that should be preserved around the call to the function
10084 FN called in with_echo_area_buffer. */
10085
10086 static Lisp_Object
10087 with_echo_area_buffer_unwind_data (struct window *w)
10088 {
10089 int i = 0;
10090 Lisp_Object vector, tmp;
10091
10092 /* Reduce consing by keeping one vector in
10093 Vwith_echo_area_save_vector. */
10094 vector = Vwith_echo_area_save_vector;
10095 Vwith_echo_area_save_vector = Qnil;
10096
10097 if (NILP (vector))
10098 vector = Fmake_vector (make_number (7), Qnil);
10099
10100 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10101 ASET (vector, i, Vdeactivate_mark); ++i;
10102 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10103
10104 if (w)
10105 {
10106 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10107 ASET (vector, i, w->buffer); ++i;
10108 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10109 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10110 }
10111 else
10112 {
10113 int end = i + 4;
10114 for (; i < end; ++i)
10115 ASET (vector, i, Qnil);
10116 }
10117
10118 eassert (i == ASIZE (vector));
10119 return vector;
10120 }
10121
10122
10123 /* Restore global state from VECTOR which was created by
10124 with_echo_area_buffer_unwind_data. */
10125
10126 static Lisp_Object
10127 unwind_with_echo_area_buffer (Lisp_Object vector)
10128 {
10129 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10130 Vdeactivate_mark = AREF (vector, 1);
10131 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10132
10133 if (WINDOWP (AREF (vector, 3)))
10134 {
10135 struct window *w;
10136 Lisp_Object buffer, charpos, bytepos;
10137
10138 w = XWINDOW (AREF (vector, 3));
10139 buffer = AREF (vector, 4);
10140 charpos = AREF (vector, 5);
10141 bytepos = AREF (vector, 6);
10142
10143 wset_buffer (w, buffer);
10144 set_marker_both (w->pointm, buffer,
10145 XFASTINT (charpos), XFASTINT (bytepos));
10146 }
10147
10148 Vwith_echo_area_save_vector = vector;
10149 return Qnil;
10150 }
10151
10152
10153 /* Set up the echo area for use by print functions. MULTIBYTE_P
10154 non-zero means we will print multibyte. */
10155
10156 void
10157 setup_echo_area_for_printing (int multibyte_p)
10158 {
10159 /* If we can't find an echo area any more, exit. */
10160 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10161 Fkill_emacs (Qnil);
10162
10163 ensure_echo_area_buffers ();
10164
10165 if (!message_buf_print)
10166 {
10167 /* A message has been output since the last time we printed.
10168 Choose a fresh echo area buffer. */
10169 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10170 echo_area_buffer[0] = echo_buffer[1];
10171 else
10172 echo_area_buffer[0] = echo_buffer[0];
10173
10174 /* Switch to that buffer and clear it. */
10175 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10176 bset_truncate_lines (current_buffer, Qnil);
10177
10178 if (Z > BEG)
10179 {
10180 ptrdiff_t count = SPECPDL_INDEX ();
10181 specbind (Qinhibit_read_only, Qt);
10182 /* Note that undo recording is always disabled. */
10183 del_range (BEG, Z);
10184 unbind_to (count, Qnil);
10185 }
10186 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10187
10188 /* Set up the buffer for the multibyteness we need. */
10189 if (multibyte_p
10190 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10191 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10192
10193 /* Raise the frame containing the echo area. */
10194 if (minibuffer_auto_raise)
10195 {
10196 struct frame *sf = SELECTED_FRAME ();
10197 Lisp_Object mini_window;
10198 mini_window = FRAME_MINIBUF_WINDOW (sf);
10199 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10200 }
10201
10202 message_log_maybe_newline ();
10203 message_buf_print = 1;
10204 }
10205 else
10206 {
10207 if (NILP (echo_area_buffer[0]))
10208 {
10209 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10210 echo_area_buffer[0] = echo_buffer[1];
10211 else
10212 echo_area_buffer[0] = echo_buffer[0];
10213 }
10214
10215 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10216 {
10217 /* Someone switched buffers between print requests. */
10218 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10219 bset_truncate_lines (current_buffer, Qnil);
10220 }
10221 }
10222 }
10223
10224
10225 /* Display an echo area message in window W. Value is non-zero if W's
10226 height is changed. If display_last_displayed_message_p is
10227 non-zero, display the message that was last displayed, otherwise
10228 display the current message. */
10229
10230 static int
10231 display_echo_area (struct window *w)
10232 {
10233 int i, no_message_p, window_height_changed_p;
10234
10235 /* Temporarily disable garbage collections while displaying the echo
10236 area. This is done because a GC can print a message itself.
10237 That message would modify the echo area buffer's contents while a
10238 redisplay of the buffer is going on, and seriously confuse
10239 redisplay. */
10240 ptrdiff_t count = inhibit_garbage_collection ();
10241
10242 /* If there is no message, we must call display_echo_area_1
10243 nevertheless because it resizes the window. But we will have to
10244 reset the echo_area_buffer in question to nil at the end because
10245 with_echo_area_buffer will sets it to an empty buffer. */
10246 i = display_last_displayed_message_p ? 1 : 0;
10247 no_message_p = NILP (echo_area_buffer[i]);
10248
10249 window_height_changed_p
10250 = with_echo_area_buffer (w, display_last_displayed_message_p,
10251 display_echo_area_1,
10252 (intptr_t) w, Qnil, 0, 0);
10253
10254 if (no_message_p)
10255 echo_area_buffer[i] = Qnil;
10256
10257 unbind_to (count, Qnil);
10258 return window_height_changed_p;
10259 }
10260
10261
10262 /* Helper for display_echo_area. Display the current buffer which
10263 contains the current echo area message in window W, a mini-window,
10264 a pointer to which is passed in A1. A2..A4 are currently not used.
10265 Change the height of W so that all of the message is displayed.
10266 Value is non-zero if height of W was changed. */
10267
10268 static int
10269 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10270 {
10271 intptr_t i1 = a1;
10272 struct window *w = (struct window *) i1;
10273 Lisp_Object window;
10274 struct text_pos start;
10275 int window_height_changed_p = 0;
10276
10277 /* Do this before displaying, so that we have a large enough glyph
10278 matrix for the display. If we can't get enough space for the
10279 whole text, display the last N lines. That works by setting w->start. */
10280 window_height_changed_p = resize_mini_window (w, 0);
10281
10282 /* Use the starting position chosen by resize_mini_window. */
10283 SET_TEXT_POS_FROM_MARKER (start, w->start);
10284
10285 /* Display. */
10286 clear_glyph_matrix (w->desired_matrix);
10287 XSETWINDOW (window, w);
10288 try_window (window, start, 0);
10289
10290 return window_height_changed_p;
10291 }
10292
10293
10294 /* Resize the echo area window to exactly the size needed for the
10295 currently displayed message, if there is one. If a mini-buffer
10296 is active, don't shrink it. */
10297
10298 void
10299 resize_echo_area_exactly (void)
10300 {
10301 if (BUFFERP (echo_area_buffer[0])
10302 && WINDOWP (echo_area_window))
10303 {
10304 struct window *w = XWINDOW (echo_area_window);
10305 int resized_p;
10306 Lisp_Object resize_exactly;
10307
10308 if (minibuf_level == 0)
10309 resize_exactly = Qt;
10310 else
10311 resize_exactly = Qnil;
10312
10313 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10314 (intptr_t) w, resize_exactly,
10315 0, 0);
10316 if (resized_p)
10317 {
10318 ++windows_or_buffers_changed;
10319 ++update_mode_lines;
10320 redisplay_internal ();
10321 }
10322 }
10323 }
10324
10325
10326 /* Callback function for with_echo_area_buffer, when used from
10327 resize_echo_area_exactly. A1 contains a pointer to the window to
10328 resize, EXACTLY non-nil means resize the mini-window exactly to the
10329 size of the text displayed. A3 and A4 are not used. Value is what
10330 resize_mini_window returns. */
10331
10332 static int
10333 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10334 {
10335 intptr_t i1 = a1;
10336 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10337 }
10338
10339
10340 /* Resize mini-window W to fit the size of its contents. EXACT_P
10341 means size the window exactly to the size needed. Otherwise, it's
10342 only enlarged until W's buffer is empty.
10343
10344 Set W->start to the right place to begin display. If the whole
10345 contents fit, start at the beginning. Otherwise, start so as
10346 to make the end of the contents appear. This is particularly
10347 important for y-or-n-p, but seems desirable generally.
10348
10349 Value is non-zero if the window height has been changed. */
10350
10351 int
10352 resize_mini_window (struct window *w, int exact_p)
10353 {
10354 struct frame *f = XFRAME (w->frame);
10355 int window_height_changed_p = 0;
10356
10357 eassert (MINI_WINDOW_P (w));
10358
10359 /* By default, start display at the beginning. */
10360 set_marker_both (w->start, w->buffer,
10361 BUF_BEGV (XBUFFER (w->buffer)),
10362 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10363
10364 /* Don't resize windows while redisplaying a window; it would
10365 confuse redisplay functions when the size of the window they are
10366 displaying changes from under them. Such a resizing can happen,
10367 for instance, when which-func prints a long message while
10368 we are running fontification-functions. We're running these
10369 functions with safe_call which binds inhibit-redisplay to t. */
10370 if (!NILP (Vinhibit_redisplay))
10371 return 0;
10372
10373 /* Nil means don't try to resize. */
10374 if (NILP (Vresize_mini_windows)
10375 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10376 return 0;
10377
10378 if (!FRAME_MINIBUF_ONLY_P (f))
10379 {
10380 struct it it;
10381 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10382 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10383 int height;
10384 EMACS_INT max_height;
10385 int unit = FRAME_LINE_HEIGHT (f);
10386 struct text_pos start;
10387 struct buffer *old_current_buffer = NULL;
10388
10389 if (current_buffer != XBUFFER (w->buffer))
10390 {
10391 old_current_buffer = current_buffer;
10392 set_buffer_internal (XBUFFER (w->buffer));
10393 }
10394
10395 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10396
10397 /* Compute the max. number of lines specified by the user. */
10398 if (FLOATP (Vmax_mini_window_height))
10399 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10400 else if (INTEGERP (Vmax_mini_window_height))
10401 max_height = XINT (Vmax_mini_window_height);
10402 else
10403 max_height = total_height / 4;
10404
10405 /* Correct that max. height if it's bogus. */
10406 max_height = clip_to_bounds (1, max_height, total_height);
10407
10408 /* Find out the height of the text in the window. */
10409 if (it.line_wrap == TRUNCATE)
10410 height = 1;
10411 else
10412 {
10413 last_height = 0;
10414 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10415 if (it.max_ascent == 0 && it.max_descent == 0)
10416 height = it.current_y + last_height;
10417 else
10418 height = it.current_y + it.max_ascent + it.max_descent;
10419 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10420 height = (height + unit - 1) / unit;
10421 }
10422
10423 /* Compute a suitable window start. */
10424 if (height > max_height)
10425 {
10426 height = max_height;
10427 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10428 move_it_vertically_backward (&it, (height - 1) * unit);
10429 start = it.current.pos;
10430 }
10431 else
10432 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10433 SET_MARKER_FROM_TEXT_POS (w->start, start);
10434
10435 if (EQ (Vresize_mini_windows, Qgrow_only))
10436 {
10437 /* Let it grow only, until we display an empty message, in which
10438 case the window shrinks again. */
10439 if (height > WINDOW_TOTAL_LINES (w))
10440 {
10441 int old_height = WINDOW_TOTAL_LINES (w);
10442 freeze_window_starts (f, 1);
10443 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10444 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10445 }
10446 else if (height < WINDOW_TOTAL_LINES (w)
10447 && (exact_p || BEGV == ZV))
10448 {
10449 int old_height = WINDOW_TOTAL_LINES (w);
10450 freeze_window_starts (f, 0);
10451 shrink_mini_window (w);
10452 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10453 }
10454 }
10455 else
10456 {
10457 /* Always resize to exact size needed. */
10458 if (height > WINDOW_TOTAL_LINES (w))
10459 {
10460 int old_height = WINDOW_TOTAL_LINES (w);
10461 freeze_window_starts (f, 1);
10462 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10463 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10464 }
10465 else if (height < WINDOW_TOTAL_LINES (w))
10466 {
10467 int old_height = WINDOW_TOTAL_LINES (w);
10468 freeze_window_starts (f, 0);
10469 shrink_mini_window (w);
10470
10471 if (height)
10472 {
10473 freeze_window_starts (f, 1);
10474 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10475 }
10476
10477 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10478 }
10479 }
10480
10481 if (old_current_buffer)
10482 set_buffer_internal (old_current_buffer);
10483 }
10484
10485 return window_height_changed_p;
10486 }
10487
10488
10489 /* Value is the current message, a string, or nil if there is no
10490 current message. */
10491
10492 Lisp_Object
10493 current_message (void)
10494 {
10495 Lisp_Object msg;
10496
10497 if (!BUFFERP (echo_area_buffer[0]))
10498 msg = Qnil;
10499 else
10500 {
10501 with_echo_area_buffer (0, 0, current_message_1,
10502 (intptr_t) &msg, Qnil, 0, 0);
10503 if (NILP (msg))
10504 echo_area_buffer[0] = Qnil;
10505 }
10506
10507 return msg;
10508 }
10509
10510
10511 static int
10512 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10513 {
10514 intptr_t i1 = a1;
10515 Lisp_Object *msg = (Lisp_Object *) i1;
10516
10517 if (Z > BEG)
10518 *msg = make_buffer_string (BEG, Z, 1);
10519 else
10520 *msg = Qnil;
10521 return 0;
10522 }
10523
10524
10525 /* Push the current message on Vmessage_stack for later restoration
10526 by restore_message. Value is non-zero if the current message isn't
10527 empty. This is a relatively infrequent operation, so it's not
10528 worth optimizing. */
10529
10530 bool
10531 push_message (void)
10532 {
10533 Lisp_Object msg = current_message ();
10534 Vmessage_stack = Fcons (msg, Vmessage_stack);
10535 return STRINGP (msg);
10536 }
10537
10538
10539 /* Restore message display from the top of Vmessage_stack. */
10540
10541 void
10542 restore_message (void)
10543 {
10544 Lisp_Object msg;
10545
10546 eassert (CONSP (Vmessage_stack));
10547 msg = XCAR (Vmessage_stack);
10548 if (STRINGP (msg))
10549 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10550 else
10551 message3_nolog (msg, 0, 0);
10552 }
10553
10554
10555 /* Handler for record_unwind_protect calling pop_message. */
10556
10557 Lisp_Object
10558 pop_message_unwind (Lisp_Object dummy)
10559 {
10560 pop_message ();
10561 return Qnil;
10562 }
10563
10564 /* Pop the top-most entry off Vmessage_stack. */
10565
10566 static void
10567 pop_message (void)
10568 {
10569 eassert (CONSP (Vmessage_stack));
10570 Vmessage_stack = XCDR (Vmessage_stack);
10571 }
10572
10573
10574 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10575 exits. If the stack is not empty, we have a missing pop_message
10576 somewhere. */
10577
10578 void
10579 check_message_stack (void)
10580 {
10581 if (!NILP (Vmessage_stack))
10582 emacs_abort ();
10583 }
10584
10585
10586 /* Truncate to NCHARS what will be displayed in the echo area the next
10587 time we display it---but don't redisplay it now. */
10588
10589 void
10590 truncate_echo_area (ptrdiff_t nchars)
10591 {
10592 if (nchars == 0)
10593 echo_area_buffer[0] = Qnil;
10594 /* A null message buffer means that the frame hasn't really been
10595 initialized yet. Error messages get reported properly by
10596 cmd_error, so this must be just an informative message; toss it. */
10597 else if (!noninteractive
10598 && INTERACTIVE
10599 && !NILP (echo_area_buffer[0]))
10600 {
10601 struct frame *sf = SELECTED_FRAME ();
10602 if (FRAME_MESSAGE_BUF (sf))
10603 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10604 }
10605 }
10606
10607
10608 /* Helper function for truncate_echo_area. Truncate the current
10609 message to at most NCHARS characters. */
10610
10611 static int
10612 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10613 {
10614 if (BEG + nchars < Z)
10615 del_range (BEG + nchars, Z);
10616 if (Z == BEG)
10617 echo_area_buffer[0] = Qnil;
10618 return 0;
10619 }
10620
10621 /* Set the current message to a substring of S or STRING.
10622
10623 If STRING is a Lisp string, set the message to the first NBYTES
10624 bytes from STRING. NBYTES zero means use the whole string. If
10625 STRING is multibyte, the message will be displayed multibyte.
10626
10627 If S is not null, set the message to the first LEN bytes of S. LEN
10628 zero means use the whole string. MULTIBYTE_P non-zero means S is
10629 multibyte. Display the message multibyte in that case.
10630
10631 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10632 to t before calling set_message_1 (which calls insert).
10633 */
10634
10635 static void
10636 set_message (const char *s, Lisp_Object string,
10637 ptrdiff_t nbytes, int multibyte_p)
10638 {
10639 message_enable_multibyte
10640 = ((s && multibyte_p)
10641 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10642
10643 with_echo_area_buffer (0, -1, set_message_1,
10644 (intptr_t) s, string, nbytes, multibyte_p);
10645 message_buf_print = 0;
10646 help_echo_showing_p = 0;
10647
10648 if (STRINGP (Vdebug_on_message)
10649 && fast_string_match (Vdebug_on_message, string) >= 0)
10650 call_debugger (list2 (Qerror, string));
10651 }
10652
10653
10654 /* Helper function for set_message. Arguments have the same meaning
10655 as there, with A1 corresponding to S and A2 corresponding to STRING
10656 This function is called with the echo area buffer being
10657 current. */
10658
10659 static int
10660 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10661 {
10662 intptr_t i1 = a1;
10663 const char *s = (const char *) i1;
10664 const unsigned char *msg = (const unsigned char *) s;
10665 Lisp_Object string = a2;
10666
10667 /* Change multibyteness of the echo buffer appropriately. */
10668 if (message_enable_multibyte
10669 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10670 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10671
10672 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10673 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10674 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10675
10676 /* Insert new message at BEG. */
10677 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10678
10679 if (STRINGP (string))
10680 {
10681 ptrdiff_t nchars;
10682
10683 if (nbytes == 0)
10684 nbytes = SBYTES (string);
10685 nchars = string_byte_to_char (string, nbytes);
10686
10687 /* This function takes care of single/multibyte conversion. We
10688 just have to ensure that the echo area buffer has the right
10689 setting of enable_multibyte_characters. */
10690 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10691 }
10692 else if (s)
10693 {
10694 if (nbytes == 0)
10695 nbytes = strlen (s);
10696
10697 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10698 {
10699 /* Convert from multi-byte to single-byte. */
10700 ptrdiff_t i;
10701 int c, n;
10702 char work[1];
10703
10704 /* Convert a multibyte string to single-byte. */
10705 for (i = 0; i < nbytes; i += n)
10706 {
10707 c = string_char_and_length (msg + i, &n);
10708 work[0] = (ASCII_CHAR_P (c)
10709 ? c
10710 : multibyte_char_to_unibyte (c));
10711 insert_1_both (work, 1, 1, 1, 0, 0);
10712 }
10713 }
10714 else if (!multibyte_p
10715 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10716 {
10717 /* Convert from single-byte to multi-byte. */
10718 ptrdiff_t i;
10719 int c, n;
10720 unsigned char str[MAX_MULTIBYTE_LENGTH];
10721
10722 /* Convert a single-byte string to multibyte. */
10723 for (i = 0; i < nbytes; i++)
10724 {
10725 c = msg[i];
10726 MAKE_CHAR_MULTIBYTE (c);
10727 n = CHAR_STRING (c, str);
10728 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10729 }
10730 }
10731 else
10732 insert_1 (s, nbytes, 1, 0, 0);
10733 }
10734
10735 return 0;
10736 }
10737
10738
10739 /* Clear messages. CURRENT_P non-zero means clear the current
10740 message. LAST_DISPLAYED_P non-zero means clear the message
10741 last displayed. */
10742
10743 void
10744 clear_message (int current_p, int last_displayed_p)
10745 {
10746 if (current_p)
10747 {
10748 echo_area_buffer[0] = Qnil;
10749 message_cleared_p = 1;
10750 }
10751
10752 if (last_displayed_p)
10753 echo_area_buffer[1] = Qnil;
10754
10755 message_buf_print = 0;
10756 }
10757
10758 /* Clear garbaged frames.
10759
10760 This function is used where the old redisplay called
10761 redraw_garbaged_frames which in turn called redraw_frame which in
10762 turn called clear_frame. The call to clear_frame was a source of
10763 flickering. I believe a clear_frame is not necessary. It should
10764 suffice in the new redisplay to invalidate all current matrices,
10765 and ensure a complete redisplay of all windows. */
10766
10767 static void
10768 clear_garbaged_frames (void)
10769 {
10770 if (frame_garbaged)
10771 {
10772 Lisp_Object tail, frame;
10773 int changed_count = 0;
10774
10775 FOR_EACH_FRAME (tail, frame)
10776 {
10777 struct frame *f = XFRAME (frame);
10778
10779 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10780 {
10781 if (f->resized_p)
10782 {
10783 redraw_frame (f);
10784 f->force_flush_display_p = 1;
10785 }
10786 clear_current_matrices (f);
10787 changed_count++;
10788 f->garbaged = 0;
10789 f->resized_p = 0;
10790 }
10791 }
10792
10793 frame_garbaged = 0;
10794 if (changed_count)
10795 ++windows_or_buffers_changed;
10796 }
10797 }
10798
10799
10800 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10801 is non-zero update selected_frame. Value is non-zero if the
10802 mini-windows height has been changed. */
10803
10804 static int
10805 echo_area_display (int update_frame_p)
10806 {
10807 Lisp_Object mini_window;
10808 struct window *w;
10809 struct frame *f;
10810 int window_height_changed_p = 0;
10811 struct frame *sf = SELECTED_FRAME ();
10812
10813 mini_window = FRAME_MINIBUF_WINDOW (sf);
10814 w = XWINDOW (mini_window);
10815 f = XFRAME (WINDOW_FRAME (w));
10816
10817 /* Don't display if frame is invisible or not yet initialized. */
10818 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10819 return 0;
10820
10821 #ifdef HAVE_WINDOW_SYSTEM
10822 /* When Emacs starts, selected_frame may be the initial terminal
10823 frame. If we let this through, a message would be displayed on
10824 the terminal. */
10825 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10826 return 0;
10827 #endif /* HAVE_WINDOW_SYSTEM */
10828
10829 /* Redraw garbaged frames. */
10830 clear_garbaged_frames ();
10831
10832 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10833 {
10834 echo_area_window = mini_window;
10835 window_height_changed_p = display_echo_area (w);
10836 w->must_be_updated_p = 1;
10837
10838 /* Update the display, unless called from redisplay_internal.
10839 Also don't update the screen during redisplay itself. The
10840 update will happen at the end of redisplay, and an update
10841 here could cause confusion. */
10842 if (update_frame_p && !redisplaying_p)
10843 {
10844 int n = 0;
10845
10846 /* If the display update has been interrupted by pending
10847 input, update mode lines in the frame. Due to the
10848 pending input, it might have been that redisplay hasn't
10849 been called, so that mode lines above the echo area are
10850 garbaged. This looks odd, so we prevent it here. */
10851 if (!display_completed)
10852 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10853
10854 if (window_height_changed_p
10855 /* Don't do this if Emacs is shutting down. Redisplay
10856 needs to run hooks. */
10857 && !NILP (Vrun_hooks))
10858 {
10859 /* Must update other windows. Likewise as in other
10860 cases, don't let this update be interrupted by
10861 pending input. */
10862 ptrdiff_t count = SPECPDL_INDEX ();
10863 specbind (Qredisplay_dont_pause, Qt);
10864 windows_or_buffers_changed = 1;
10865 redisplay_internal ();
10866 unbind_to (count, Qnil);
10867 }
10868 else if (FRAME_WINDOW_P (f) && n == 0)
10869 {
10870 /* Window configuration is the same as before.
10871 Can do with a display update of the echo area,
10872 unless we displayed some mode lines. */
10873 update_single_window (w, 1);
10874 FRAME_RIF (f)->flush_display (f);
10875 }
10876 else
10877 update_frame (f, 1, 1);
10878
10879 /* If cursor is in the echo area, make sure that the next
10880 redisplay displays the minibuffer, so that the cursor will
10881 be replaced with what the minibuffer wants. */
10882 if (cursor_in_echo_area)
10883 ++windows_or_buffers_changed;
10884 }
10885 }
10886 else if (!EQ (mini_window, selected_window))
10887 windows_or_buffers_changed++;
10888
10889 /* Last displayed message is now the current message. */
10890 echo_area_buffer[1] = echo_area_buffer[0];
10891 /* Inform read_char that we're not echoing. */
10892 echo_message_buffer = Qnil;
10893
10894 /* Prevent redisplay optimization in redisplay_internal by resetting
10895 this_line_start_pos. This is done because the mini-buffer now
10896 displays the message instead of its buffer text. */
10897 if (EQ (mini_window, selected_window))
10898 CHARPOS (this_line_start_pos) = 0;
10899
10900 return window_height_changed_p;
10901 }
10902
10903 /* Nonzero if the current window's buffer is shown in more than one
10904 window and was modified since last redisplay. */
10905
10906 static int
10907 buffer_shared_and_changed (void)
10908 {
10909 return (buffer_window_count (current_buffer) > 1
10910 && UNCHANGED_MODIFIED < MODIFF);
10911 }
10912
10913 /* Nonzero if W doesn't reflect the actual state of current buffer due
10914 to its text or overlays change. FIXME: this may be called when
10915 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10916
10917 static int
10918 window_outdated (struct window *w)
10919 {
10920 return (w->last_modified < MODIFF
10921 || w->last_overlay_modified < OVERLAY_MODIFF);
10922 }
10923
10924 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10925 is enabled and mark of W's buffer was changed since last W's update. */
10926
10927 static int
10928 window_buffer_changed (struct window *w)
10929 {
10930 struct buffer *b = XBUFFER (w->buffer);
10931
10932 eassert (BUFFER_LIVE_P (b));
10933
10934 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10935 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10936 != !NILP (w->region_showing)));
10937 }
10938
10939 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10940
10941 static int
10942 mode_line_update_needed (struct window *w)
10943 {
10944 return (!NILP (w->column_number_displayed)
10945 && !(PT == w->last_point && !window_outdated (w))
10946 && (XFASTINT (w->column_number_displayed) != current_column ()));
10947 }
10948
10949 /***********************************************************************
10950 Mode Lines and Frame Titles
10951 ***********************************************************************/
10952
10953 /* A buffer for constructing non-propertized mode-line strings and
10954 frame titles in it; allocated from the heap in init_xdisp and
10955 resized as needed in store_mode_line_noprop_char. */
10956
10957 static char *mode_line_noprop_buf;
10958
10959 /* The buffer's end, and a current output position in it. */
10960
10961 static char *mode_line_noprop_buf_end;
10962 static char *mode_line_noprop_ptr;
10963
10964 #define MODE_LINE_NOPROP_LEN(start) \
10965 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10966
10967 static enum {
10968 MODE_LINE_DISPLAY = 0,
10969 MODE_LINE_TITLE,
10970 MODE_LINE_NOPROP,
10971 MODE_LINE_STRING
10972 } mode_line_target;
10973
10974 /* Alist that caches the results of :propertize.
10975 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10976 static Lisp_Object mode_line_proptrans_alist;
10977
10978 /* List of strings making up the mode-line. */
10979 static Lisp_Object mode_line_string_list;
10980
10981 /* Base face property when building propertized mode line string. */
10982 static Lisp_Object mode_line_string_face;
10983 static Lisp_Object mode_line_string_face_prop;
10984
10985
10986 /* Unwind data for mode line strings */
10987
10988 static Lisp_Object Vmode_line_unwind_vector;
10989
10990 static Lisp_Object
10991 format_mode_line_unwind_data (struct frame *target_frame,
10992 struct buffer *obuf,
10993 Lisp_Object owin,
10994 int save_proptrans)
10995 {
10996 Lisp_Object vector, tmp;
10997
10998 /* Reduce consing by keeping one vector in
10999 Vwith_echo_area_save_vector. */
11000 vector = Vmode_line_unwind_vector;
11001 Vmode_line_unwind_vector = Qnil;
11002
11003 if (NILP (vector))
11004 vector = Fmake_vector (make_number (10), Qnil);
11005
11006 ASET (vector, 0, make_number (mode_line_target));
11007 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11008 ASET (vector, 2, mode_line_string_list);
11009 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11010 ASET (vector, 4, mode_line_string_face);
11011 ASET (vector, 5, mode_line_string_face_prop);
11012
11013 if (obuf)
11014 XSETBUFFER (tmp, obuf);
11015 else
11016 tmp = Qnil;
11017 ASET (vector, 6, tmp);
11018 ASET (vector, 7, owin);
11019 if (target_frame)
11020 {
11021 /* Similarly to `with-selected-window', if the operation selects
11022 a window on another frame, we must restore that frame's
11023 selected window, and (for a tty) the top-frame. */
11024 ASET (vector, 8, target_frame->selected_window);
11025 if (FRAME_TERMCAP_P (target_frame))
11026 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11027 }
11028
11029 return vector;
11030 }
11031
11032 static Lisp_Object
11033 unwind_format_mode_line (Lisp_Object vector)
11034 {
11035 Lisp_Object old_window = AREF (vector, 7);
11036 Lisp_Object target_frame_window = AREF (vector, 8);
11037 Lisp_Object old_top_frame = AREF (vector, 9);
11038
11039 mode_line_target = XINT (AREF (vector, 0));
11040 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11041 mode_line_string_list = AREF (vector, 2);
11042 if (! EQ (AREF (vector, 3), Qt))
11043 mode_line_proptrans_alist = AREF (vector, 3);
11044 mode_line_string_face = AREF (vector, 4);
11045 mode_line_string_face_prop = AREF (vector, 5);
11046
11047 /* Select window before buffer, since it may change the buffer. */
11048 if (!NILP (old_window))
11049 {
11050 /* If the operation that we are unwinding had selected a window
11051 on a different frame, reset its frame-selected-window. For a
11052 text terminal, reset its top-frame if necessary. */
11053 if (!NILP (target_frame_window))
11054 {
11055 Lisp_Object frame
11056 = WINDOW_FRAME (XWINDOW (target_frame_window));
11057
11058 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11059 Fselect_window (target_frame_window, Qt);
11060
11061 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11062 Fselect_frame (old_top_frame, Qt);
11063 }
11064
11065 Fselect_window (old_window, Qt);
11066 }
11067
11068 if (!NILP (AREF (vector, 6)))
11069 {
11070 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11071 ASET (vector, 6, Qnil);
11072 }
11073
11074 Vmode_line_unwind_vector = vector;
11075 return Qnil;
11076 }
11077
11078
11079 /* Store a single character C for the frame title in mode_line_noprop_buf.
11080 Re-allocate mode_line_noprop_buf if necessary. */
11081
11082 static void
11083 store_mode_line_noprop_char (char c)
11084 {
11085 /* If output position has reached the end of the allocated buffer,
11086 increase the buffer's size. */
11087 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11088 {
11089 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11090 ptrdiff_t size = len;
11091 mode_line_noprop_buf =
11092 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11093 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11094 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11095 }
11096
11097 *mode_line_noprop_ptr++ = c;
11098 }
11099
11100
11101 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11102 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11103 characters that yield more columns than PRECISION; PRECISION <= 0
11104 means copy the whole string. Pad with spaces until FIELD_WIDTH
11105 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11106 pad. Called from display_mode_element when it is used to build a
11107 frame title. */
11108
11109 static int
11110 store_mode_line_noprop (const char *string, int field_width, int precision)
11111 {
11112 const unsigned char *str = (const unsigned char *) string;
11113 int n = 0;
11114 ptrdiff_t dummy, nbytes;
11115
11116 /* Copy at most PRECISION chars from STR. */
11117 nbytes = strlen (string);
11118 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11119 while (nbytes--)
11120 store_mode_line_noprop_char (*str++);
11121
11122 /* Fill up with spaces until FIELD_WIDTH reached. */
11123 while (field_width > 0
11124 && n < field_width)
11125 {
11126 store_mode_line_noprop_char (' ');
11127 ++n;
11128 }
11129
11130 return n;
11131 }
11132
11133 /***********************************************************************
11134 Frame Titles
11135 ***********************************************************************/
11136
11137 #ifdef HAVE_WINDOW_SYSTEM
11138
11139 /* Set the title of FRAME, if it has changed. The title format is
11140 Vicon_title_format if FRAME is iconified, otherwise it is
11141 frame_title_format. */
11142
11143 static void
11144 x_consider_frame_title (Lisp_Object frame)
11145 {
11146 struct frame *f = XFRAME (frame);
11147
11148 if (FRAME_WINDOW_P (f)
11149 || FRAME_MINIBUF_ONLY_P (f)
11150 || f->explicit_name)
11151 {
11152 /* Do we have more than one visible frame on this X display? */
11153 Lisp_Object tail, other_frame, fmt;
11154 ptrdiff_t title_start;
11155 char *title;
11156 ptrdiff_t len;
11157 struct it it;
11158 ptrdiff_t count = SPECPDL_INDEX ();
11159
11160 FOR_EACH_FRAME (tail, other_frame)
11161 {
11162 struct frame *tf = XFRAME (other_frame);
11163
11164 if (tf != f
11165 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11166 && !FRAME_MINIBUF_ONLY_P (tf)
11167 && !EQ (other_frame, tip_frame)
11168 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11169 break;
11170 }
11171
11172 /* Set global variable indicating that multiple frames exist. */
11173 multiple_frames = CONSP (tail);
11174
11175 /* Switch to the buffer of selected window of the frame. Set up
11176 mode_line_target so that display_mode_element will output into
11177 mode_line_noprop_buf; then display the title. */
11178 record_unwind_protect (unwind_format_mode_line,
11179 format_mode_line_unwind_data
11180 (f, current_buffer, selected_window, 0));
11181
11182 Fselect_window (f->selected_window, Qt);
11183 set_buffer_internal_1
11184 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11185 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11186
11187 mode_line_target = MODE_LINE_TITLE;
11188 title_start = MODE_LINE_NOPROP_LEN (0);
11189 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11190 NULL, DEFAULT_FACE_ID);
11191 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11192 len = MODE_LINE_NOPROP_LEN (title_start);
11193 title = mode_line_noprop_buf + title_start;
11194 unbind_to (count, Qnil);
11195
11196 /* Set the title only if it's changed. This avoids consing in
11197 the common case where it hasn't. (If it turns out that we've
11198 already wasted too much time by walking through the list with
11199 display_mode_element, then we might need to optimize at a
11200 higher level than this.) */
11201 if (! STRINGP (f->name)
11202 || SBYTES (f->name) != len
11203 || memcmp (title, SDATA (f->name), len) != 0)
11204 x_implicitly_set_name (f, make_string (title, len), Qnil);
11205 }
11206 }
11207
11208 #endif /* not HAVE_WINDOW_SYSTEM */
11209
11210 \f
11211 /***********************************************************************
11212 Menu Bars
11213 ***********************************************************************/
11214
11215
11216 /* Prepare for redisplay by updating menu-bar item lists when
11217 appropriate. This can call eval. */
11218
11219 void
11220 prepare_menu_bars (void)
11221 {
11222 int all_windows;
11223 struct gcpro gcpro1, gcpro2;
11224 struct frame *f;
11225 Lisp_Object tooltip_frame;
11226
11227 #ifdef HAVE_WINDOW_SYSTEM
11228 tooltip_frame = tip_frame;
11229 #else
11230 tooltip_frame = Qnil;
11231 #endif
11232
11233 /* Update all frame titles based on their buffer names, etc. We do
11234 this before the menu bars so that the buffer-menu will show the
11235 up-to-date frame titles. */
11236 #ifdef HAVE_WINDOW_SYSTEM
11237 if (windows_or_buffers_changed || update_mode_lines)
11238 {
11239 Lisp_Object tail, frame;
11240
11241 FOR_EACH_FRAME (tail, frame)
11242 {
11243 f = XFRAME (frame);
11244 if (!EQ (frame, tooltip_frame)
11245 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11246 x_consider_frame_title (frame);
11247 }
11248 }
11249 #endif /* HAVE_WINDOW_SYSTEM */
11250
11251 /* Update the menu bar item lists, if appropriate. This has to be
11252 done before any actual redisplay or generation of display lines. */
11253 all_windows = (update_mode_lines
11254 || buffer_shared_and_changed ()
11255 || windows_or_buffers_changed);
11256 if (all_windows)
11257 {
11258 Lisp_Object tail, frame;
11259 ptrdiff_t count = SPECPDL_INDEX ();
11260 /* 1 means that update_menu_bar has run its hooks
11261 so any further calls to update_menu_bar shouldn't do so again. */
11262 int menu_bar_hooks_run = 0;
11263
11264 record_unwind_save_match_data ();
11265
11266 FOR_EACH_FRAME (tail, frame)
11267 {
11268 f = XFRAME (frame);
11269
11270 /* Ignore tooltip frame. */
11271 if (EQ (frame, tooltip_frame))
11272 continue;
11273
11274 /* If a window on this frame changed size, report that to
11275 the user and clear the size-change flag. */
11276 if (FRAME_WINDOW_SIZES_CHANGED (f))
11277 {
11278 Lisp_Object functions;
11279
11280 /* Clear flag first in case we get an error below. */
11281 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11282 functions = Vwindow_size_change_functions;
11283 GCPRO2 (tail, functions);
11284
11285 while (CONSP (functions))
11286 {
11287 if (!EQ (XCAR (functions), Qt))
11288 call1 (XCAR (functions), frame);
11289 functions = XCDR (functions);
11290 }
11291 UNGCPRO;
11292 }
11293
11294 GCPRO1 (tail);
11295 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11296 #ifdef HAVE_WINDOW_SYSTEM
11297 update_tool_bar (f, 0);
11298 #endif
11299 #ifdef HAVE_NS
11300 if (windows_or_buffers_changed
11301 && FRAME_NS_P (f))
11302 ns_set_doc_edited
11303 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11304 #endif
11305 UNGCPRO;
11306 }
11307
11308 unbind_to (count, Qnil);
11309 }
11310 else
11311 {
11312 struct frame *sf = SELECTED_FRAME ();
11313 update_menu_bar (sf, 1, 0);
11314 #ifdef HAVE_WINDOW_SYSTEM
11315 update_tool_bar (sf, 1);
11316 #endif
11317 }
11318 }
11319
11320
11321 /* Update the menu bar item list for frame F. This has to be done
11322 before we start to fill in any display lines, because it can call
11323 eval.
11324
11325 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11326
11327 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11328 already ran the menu bar hooks for this redisplay, so there
11329 is no need to run them again. The return value is the
11330 updated value of this flag, to pass to the next call. */
11331
11332 static int
11333 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11334 {
11335 Lisp_Object window;
11336 register struct window *w;
11337
11338 /* If called recursively during a menu update, do nothing. This can
11339 happen when, for instance, an activate-menubar-hook causes a
11340 redisplay. */
11341 if (inhibit_menubar_update)
11342 return hooks_run;
11343
11344 window = FRAME_SELECTED_WINDOW (f);
11345 w = XWINDOW (window);
11346
11347 if (FRAME_WINDOW_P (f)
11348 ?
11349 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11350 || defined (HAVE_NS) || defined (USE_GTK)
11351 FRAME_EXTERNAL_MENU_BAR (f)
11352 #else
11353 FRAME_MENU_BAR_LINES (f) > 0
11354 #endif
11355 : FRAME_MENU_BAR_LINES (f) > 0)
11356 {
11357 /* If the user has switched buffers or windows, we need to
11358 recompute to reflect the new bindings. But we'll
11359 recompute when update_mode_lines is set too; that means
11360 that people can use force-mode-line-update to request
11361 that the menu bar be recomputed. The adverse effect on
11362 the rest of the redisplay algorithm is about the same as
11363 windows_or_buffers_changed anyway. */
11364 if (windows_or_buffers_changed
11365 /* This used to test w->update_mode_line, but we believe
11366 there is no need to recompute the menu in that case. */
11367 || update_mode_lines
11368 || window_buffer_changed (w))
11369 {
11370 struct buffer *prev = current_buffer;
11371 ptrdiff_t count = SPECPDL_INDEX ();
11372
11373 specbind (Qinhibit_menubar_update, Qt);
11374
11375 set_buffer_internal_1 (XBUFFER (w->buffer));
11376 if (save_match_data)
11377 record_unwind_save_match_data ();
11378 if (NILP (Voverriding_local_map_menu_flag))
11379 {
11380 specbind (Qoverriding_terminal_local_map, Qnil);
11381 specbind (Qoverriding_local_map, Qnil);
11382 }
11383
11384 if (!hooks_run)
11385 {
11386 /* Run the Lucid hook. */
11387 safe_run_hooks (Qactivate_menubar_hook);
11388
11389 /* If it has changed current-menubar from previous value,
11390 really recompute the menu-bar from the value. */
11391 if (! NILP (Vlucid_menu_bar_dirty_flag))
11392 call0 (Qrecompute_lucid_menubar);
11393
11394 safe_run_hooks (Qmenu_bar_update_hook);
11395
11396 hooks_run = 1;
11397 }
11398
11399 XSETFRAME (Vmenu_updating_frame, f);
11400 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11401
11402 /* Redisplay the menu bar in case we changed it. */
11403 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11404 || defined (HAVE_NS) || defined (USE_GTK)
11405 if (FRAME_WINDOW_P (f))
11406 {
11407 #if defined (HAVE_NS)
11408 /* All frames on Mac OS share the same menubar. So only
11409 the selected frame should be allowed to set it. */
11410 if (f == SELECTED_FRAME ())
11411 #endif
11412 set_frame_menubar (f, 0, 0);
11413 }
11414 else
11415 /* On a terminal screen, the menu bar is an ordinary screen
11416 line, and this makes it get updated. */
11417 w->update_mode_line = 1;
11418 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11419 /* In the non-toolkit version, the menu bar is an ordinary screen
11420 line, and this makes it get updated. */
11421 w->update_mode_line = 1;
11422 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11423
11424 unbind_to (count, Qnil);
11425 set_buffer_internal_1 (prev);
11426 }
11427 }
11428
11429 return hooks_run;
11430 }
11431
11432
11433 \f
11434 /***********************************************************************
11435 Output Cursor
11436 ***********************************************************************/
11437
11438 #ifdef HAVE_WINDOW_SYSTEM
11439
11440 /* EXPORT:
11441 Nominal cursor position -- where to draw output.
11442 HPOS and VPOS are window relative glyph matrix coordinates.
11443 X and Y are window relative pixel coordinates. */
11444
11445 struct cursor_pos output_cursor;
11446
11447
11448 /* EXPORT:
11449 Set the global variable output_cursor to CURSOR. All cursor
11450 positions are relative to updated_window. */
11451
11452 void
11453 set_output_cursor (struct cursor_pos *cursor)
11454 {
11455 output_cursor.hpos = cursor->hpos;
11456 output_cursor.vpos = cursor->vpos;
11457 output_cursor.x = cursor->x;
11458 output_cursor.y = cursor->y;
11459 }
11460
11461
11462 /* EXPORT for RIF:
11463 Set a nominal cursor position.
11464
11465 HPOS and VPOS are column/row positions in a window glyph matrix. X
11466 and Y are window text area relative pixel positions.
11467
11468 If this is done during an update, updated_window will contain the
11469 window that is being updated and the position is the future output
11470 cursor position for that window. If updated_window is null, use
11471 selected_window and display the cursor at the given position. */
11472
11473 void
11474 x_cursor_to (int vpos, int hpos, int y, int x)
11475 {
11476 struct window *w;
11477
11478 /* If updated_window is not set, work on selected_window. */
11479 if (updated_window)
11480 w = updated_window;
11481 else
11482 w = XWINDOW (selected_window);
11483
11484 /* Set the output cursor. */
11485 output_cursor.hpos = hpos;
11486 output_cursor.vpos = vpos;
11487 output_cursor.x = x;
11488 output_cursor.y = y;
11489
11490 /* If not called as part of an update, really display the cursor.
11491 This will also set the cursor position of W. */
11492 if (updated_window == NULL)
11493 {
11494 block_input ();
11495 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11496 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11497 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11498 unblock_input ();
11499 }
11500 }
11501
11502 #endif /* HAVE_WINDOW_SYSTEM */
11503
11504 \f
11505 /***********************************************************************
11506 Tool-bars
11507 ***********************************************************************/
11508
11509 #ifdef HAVE_WINDOW_SYSTEM
11510
11511 /* Where the mouse was last time we reported a mouse event. */
11512
11513 FRAME_PTR last_mouse_frame;
11514
11515 /* Tool-bar item index of the item on which a mouse button was pressed
11516 or -1. */
11517
11518 int last_tool_bar_item;
11519
11520 /* Select `frame' temporarily without running all the code in
11521 do_switch_frame.
11522 FIXME: Maybe do_switch_frame should be trimmed down similarly
11523 when `norecord' is set. */
11524 static Lisp_Object
11525 fast_set_selected_frame (Lisp_Object frame)
11526 {
11527 if (!EQ (selected_frame, frame))
11528 {
11529 selected_frame = frame;
11530 selected_window = XFRAME (frame)->selected_window;
11531 }
11532 return Qnil;
11533 }
11534
11535 /* Update the tool-bar item list for frame F. This has to be done
11536 before we start to fill in any display lines. Called from
11537 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11538 and restore it here. */
11539
11540 static void
11541 update_tool_bar (struct frame *f, int save_match_data)
11542 {
11543 #if defined (USE_GTK) || defined (HAVE_NS)
11544 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11545 #else
11546 int do_update = WINDOWP (f->tool_bar_window)
11547 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11548 #endif
11549
11550 if (do_update)
11551 {
11552 Lisp_Object window;
11553 struct window *w;
11554
11555 window = FRAME_SELECTED_WINDOW (f);
11556 w = XWINDOW (window);
11557
11558 /* If the user has switched buffers or windows, we need to
11559 recompute to reflect the new bindings. But we'll
11560 recompute when update_mode_lines is set too; that means
11561 that people can use force-mode-line-update to request
11562 that the menu bar be recomputed. The adverse effect on
11563 the rest of the redisplay algorithm is about the same as
11564 windows_or_buffers_changed anyway. */
11565 if (windows_or_buffers_changed
11566 || w->update_mode_line
11567 || update_mode_lines
11568 || window_buffer_changed (w))
11569 {
11570 struct buffer *prev = current_buffer;
11571 ptrdiff_t count = SPECPDL_INDEX ();
11572 Lisp_Object frame, new_tool_bar;
11573 int new_n_tool_bar;
11574 struct gcpro gcpro1;
11575
11576 /* Set current_buffer to the buffer of the selected
11577 window of the frame, so that we get the right local
11578 keymaps. */
11579 set_buffer_internal_1 (XBUFFER (w->buffer));
11580
11581 /* Save match data, if we must. */
11582 if (save_match_data)
11583 record_unwind_save_match_data ();
11584
11585 /* Make sure that we don't accidentally use bogus keymaps. */
11586 if (NILP (Voverriding_local_map_menu_flag))
11587 {
11588 specbind (Qoverriding_terminal_local_map, Qnil);
11589 specbind (Qoverriding_local_map, Qnil);
11590 }
11591
11592 GCPRO1 (new_tool_bar);
11593
11594 /* We must temporarily set the selected frame to this frame
11595 before calling tool_bar_items, because the calculation of
11596 the tool-bar keymap uses the selected frame (see
11597 `tool-bar-make-keymap' in tool-bar.el). */
11598 eassert (EQ (selected_window,
11599 /* Since we only explicitly preserve selected_frame,
11600 check that selected_window would be redundant. */
11601 XFRAME (selected_frame)->selected_window));
11602 record_unwind_protect (fast_set_selected_frame, selected_frame);
11603 XSETFRAME (frame, f);
11604 fast_set_selected_frame (frame);
11605
11606 /* Build desired tool-bar items from keymaps. */
11607 new_tool_bar
11608 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11609 &new_n_tool_bar);
11610
11611 /* Redisplay the tool-bar if we changed it. */
11612 if (new_n_tool_bar != f->n_tool_bar_items
11613 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11614 {
11615 /* Redisplay that happens asynchronously due to an expose event
11616 may access f->tool_bar_items. Make sure we update both
11617 variables within BLOCK_INPUT so no such event interrupts. */
11618 block_input ();
11619 fset_tool_bar_items (f, new_tool_bar);
11620 f->n_tool_bar_items = new_n_tool_bar;
11621 w->update_mode_line = 1;
11622 unblock_input ();
11623 }
11624
11625 UNGCPRO;
11626
11627 unbind_to (count, Qnil);
11628 set_buffer_internal_1 (prev);
11629 }
11630 }
11631 }
11632
11633
11634 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11635 F's desired tool-bar contents. F->tool_bar_items must have
11636 been set up previously by calling prepare_menu_bars. */
11637
11638 static void
11639 build_desired_tool_bar_string (struct frame *f)
11640 {
11641 int i, size, size_needed;
11642 struct gcpro gcpro1, gcpro2, gcpro3;
11643 Lisp_Object image, plist, props;
11644
11645 image = plist = props = Qnil;
11646 GCPRO3 (image, plist, props);
11647
11648 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11649 Otherwise, make a new string. */
11650
11651 /* The size of the string we might be able to reuse. */
11652 size = (STRINGP (f->desired_tool_bar_string)
11653 ? SCHARS (f->desired_tool_bar_string)
11654 : 0);
11655
11656 /* We need one space in the string for each image. */
11657 size_needed = f->n_tool_bar_items;
11658
11659 /* Reuse f->desired_tool_bar_string, if possible. */
11660 if (size < size_needed || NILP (f->desired_tool_bar_string))
11661 fset_desired_tool_bar_string
11662 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11663 else
11664 {
11665 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11666 Fremove_text_properties (make_number (0), make_number (size),
11667 props, f->desired_tool_bar_string);
11668 }
11669
11670 /* Put a `display' property on the string for the images to display,
11671 put a `menu_item' property on tool-bar items with a value that
11672 is the index of the item in F's tool-bar item vector. */
11673 for (i = 0; i < f->n_tool_bar_items; ++i)
11674 {
11675 #define PROP(IDX) \
11676 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11677
11678 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11679 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11680 int hmargin, vmargin, relief, idx, end;
11681
11682 /* If image is a vector, choose the image according to the
11683 button state. */
11684 image = PROP (TOOL_BAR_ITEM_IMAGES);
11685 if (VECTORP (image))
11686 {
11687 if (enabled_p)
11688 idx = (selected_p
11689 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11690 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11691 else
11692 idx = (selected_p
11693 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11694 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11695
11696 eassert (ASIZE (image) >= idx);
11697 image = AREF (image, idx);
11698 }
11699 else
11700 idx = -1;
11701
11702 /* Ignore invalid image specifications. */
11703 if (!valid_image_p (image))
11704 continue;
11705
11706 /* Display the tool-bar button pressed, or depressed. */
11707 plist = Fcopy_sequence (XCDR (image));
11708
11709 /* Compute margin and relief to draw. */
11710 relief = (tool_bar_button_relief >= 0
11711 ? tool_bar_button_relief
11712 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11713 hmargin = vmargin = relief;
11714
11715 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11716 INT_MAX - max (hmargin, vmargin)))
11717 {
11718 hmargin += XFASTINT (Vtool_bar_button_margin);
11719 vmargin += XFASTINT (Vtool_bar_button_margin);
11720 }
11721 else if (CONSP (Vtool_bar_button_margin))
11722 {
11723 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11724 INT_MAX - hmargin))
11725 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11726
11727 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11728 INT_MAX - vmargin))
11729 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11730 }
11731
11732 if (auto_raise_tool_bar_buttons_p)
11733 {
11734 /* Add a `:relief' property to the image spec if the item is
11735 selected. */
11736 if (selected_p)
11737 {
11738 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11739 hmargin -= relief;
11740 vmargin -= relief;
11741 }
11742 }
11743 else
11744 {
11745 /* If image is selected, display it pressed, i.e. with a
11746 negative relief. If it's not selected, display it with a
11747 raised relief. */
11748 plist = Fplist_put (plist, QCrelief,
11749 (selected_p
11750 ? make_number (-relief)
11751 : make_number (relief)));
11752 hmargin -= relief;
11753 vmargin -= relief;
11754 }
11755
11756 /* Put a margin around the image. */
11757 if (hmargin || vmargin)
11758 {
11759 if (hmargin == vmargin)
11760 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11761 else
11762 plist = Fplist_put (plist, QCmargin,
11763 Fcons (make_number (hmargin),
11764 make_number (vmargin)));
11765 }
11766
11767 /* If button is not enabled, and we don't have special images
11768 for the disabled state, make the image appear disabled by
11769 applying an appropriate algorithm to it. */
11770 if (!enabled_p && idx < 0)
11771 plist = Fplist_put (plist, QCconversion, Qdisabled);
11772
11773 /* Put a `display' text property on the string for the image to
11774 display. Put a `menu-item' property on the string that gives
11775 the start of this item's properties in the tool-bar items
11776 vector. */
11777 image = Fcons (Qimage, plist);
11778 props = list4 (Qdisplay, image,
11779 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11780
11781 /* Let the last image hide all remaining spaces in the tool bar
11782 string. The string can be longer than needed when we reuse a
11783 previous string. */
11784 if (i + 1 == f->n_tool_bar_items)
11785 end = SCHARS (f->desired_tool_bar_string);
11786 else
11787 end = i + 1;
11788 Fadd_text_properties (make_number (i), make_number (end),
11789 props, f->desired_tool_bar_string);
11790 #undef PROP
11791 }
11792
11793 UNGCPRO;
11794 }
11795
11796
11797 /* Display one line of the tool-bar of frame IT->f.
11798
11799 HEIGHT specifies the desired height of the tool-bar line.
11800 If the actual height of the glyph row is less than HEIGHT, the
11801 row's height is increased to HEIGHT, and the icons are centered
11802 vertically in the new height.
11803
11804 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11805 count a final empty row in case the tool-bar width exactly matches
11806 the window width.
11807 */
11808
11809 static void
11810 display_tool_bar_line (struct it *it, int height)
11811 {
11812 struct glyph_row *row = it->glyph_row;
11813 int max_x = it->last_visible_x;
11814 struct glyph *last;
11815
11816 prepare_desired_row (row);
11817 row->y = it->current_y;
11818
11819 /* Note that this isn't made use of if the face hasn't a box,
11820 so there's no need to check the face here. */
11821 it->start_of_box_run_p = 1;
11822
11823 while (it->current_x < max_x)
11824 {
11825 int x, n_glyphs_before, i, nglyphs;
11826 struct it it_before;
11827
11828 /* Get the next display element. */
11829 if (!get_next_display_element (it))
11830 {
11831 /* Don't count empty row if we are counting needed tool-bar lines. */
11832 if (height < 0 && !it->hpos)
11833 return;
11834 break;
11835 }
11836
11837 /* Produce glyphs. */
11838 n_glyphs_before = row->used[TEXT_AREA];
11839 it_before = *it;
11840
11841 PRODUCE_GLYPHS (it);
11842
11843 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11844 i = 0;
11845 x = it_before.current_x;
11846 while (i < nglyphs)
11847 {
11848 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11849
11850 if (x + glyph->pixel_width > max_x)
11851 {
11852 /* Glyph doesn't fit on line. Backtrack. */
11853 row->used[TEXT_AREA] = n_glyphs_before;
11854 *it = it_before;
11855 /* If this is the only glyph on this line, it will never fit on the
11856 tool-bar, so skip it. But ensure there is at least one glyph,
11857 so we don't accidentally disable the tool-bar. */
11858 if (n_glyphs_before == 0
11859 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11860 break;
11861 goto out;
11862 }
11863
11864 ++it->hpos;
11865 x += glyph->pixel_width;
11866 ++i;
11867 }
11868
11869 /* Stop at line end. */
11870 if (ITERATOR_AT_END_OF_LINE_P (it))
11871 break;
11872
11873 set_iterator_to_next (it, 1);
11874 }
11875
11876 out:;
11877
11878 row->displays_text_p = row->used[TEXT_AREA] != 0;
11879
11880 /* Use default face for the border below the tool bar.
11881
11882 FIXME: When auto-resize-tool-bars is grow-only, there is
11883 no additional border below the possibly empty tool-bar lines.
11884 So to make the extra empty lines look "normal", we have to
11885 use the tool-bar face for the border too. */
11886 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11887 it->face_id = DEFAULT_FACE_ID;
11888
11889 extend_face_to_end_of_line (it);
11890 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11891 last->right_box_line_p = 1;
11892 if (last == row->glyphs[TEXT_AREA])
11893 last->left_box_line_p = 1;
11894
11895 /* Make line the desired height and center it vertically. */
11896 if ((height -= it->max_ascent + it->max_descent) > 0)
11897 {
11898 /* Don't add more than one line height. */
11899 height %= FRAME_LINE_HEIGHT (it->f);
11900 it->max_ascent += height / 2;
11901 it->max_descent += (height + 1) / 2;
11902 }
11903
11904 compute_line_metrics (it);
11905
11906 /* If line is empty, make it occupy the rest of the tool-bar. */
11907 if (!row->displays_text_p)
11908 {
11909 row->height = row->phys_height = it->last_visible_y - row->y;
11910 row->visible_height = row->height;
11911 row->ascent = row->phys_ascent = 0;
11912 row->extra_line_spacing = 0;
11913 }
11914
11915 row->full_width_p = 1;
11916 row->continued_p = 0;
11917 row->truncated_on_left_p = 0;
11918 row->truncated_on_right_p = 0;
11919
11920 it->current_x = it->hpos = 0;
11921 it->current_y += row->height;
11922 ++it->vpos;
11923 ++it->glyph_row;
11924 }
11925
11926
11927 /* Max tool-bar height. */
11928
11929 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11930 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11931
11932 /* Value is the number of screen lines needed to make all tool-bar
11933 items of frame F visible. The number of actual rows needed is
11934 returned in *N_ROWS if non-NULL. */
11935
11936 static int
11937 tool_bar_lines_needed (struct frame *f, int *n_rows)
11938 {
11939 struct window *w = XWINDOW (f->tool_bar_window);
11940 struct it it;
11941 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11942 the desired matrix, so use (unused) mode-line row as temporary row to
11943 avoid destroying the first tool-bar row. */
11944 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11945
11946 /* Initialize an iterator for iteration over
11947 F->desired_tool_bar_string in the tool-bar window of frame F. */
11948 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11949 it.first_visible_x = 0;
11950 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11951 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11952 it.paragraph_embedding = L2R;
11953
11954 while (!ITERATOR_AT_END_P (&it))
11955 {
11956 clear_glyph_row (temp_row);
11957 it.glyph_row = temp_row;
11958 display_tool_bar_line (&it, -1);
11959 }
11960 clear_glyph_row (temp_row);
11961
11962 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11963 if (n_rows)
11964 *n_rows = it.vpos > 0 ? it.vpos : -1;
11965
11966 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11967 }
11968
11969
11970 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11971 0, 1, 0,
11972 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11973 If FRAME is nil or omitted, use the selected frame. */)
11974 (Lisp_Object frame)
11975 {
11976 struct frame *f = decode_any_frame (frame);
11977 struct window *w;
11978 int nlines = 0;
11979
11980 if (WINDOWP (f->tool_bar_window)
11981 && (w = XWINDOW (f->tool_bar_window),
11982 WINDOW_TOTAL_LINES (w) > 0))
11983 {
11984 update_tool_bar (f, 1);
11985 if (f->n_tool_bar_items)
11986 {
11987 build_desired_tool_bar_string (f);
11988 nlines = tool_bar_lines_needed (f, NULL);
11989 }
11990 }
11991
11992 return make_number (nlines);
11993 }
11994
11995
11996 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11997 height should be changed. */
11998
11999 static int
12000 redisplay_tool_bar (struct frame *f)
12001 {
12002 struct window *w;
12003 struct it it;
12004 struct glyph_row *row;
12005
12006 #if defined (USE_GTK) || defined (HAVE_NS)
12007 if (FRAME_EXTERNAL_TOOL_BAR (f))
12008 update_frame_tool_bar (f);
12009 return 0;
12010 #endif
12011
12012 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12013 do anything. This means you must start with tool-bar-lines
12014 non-zero to get the auto-sizing effect. Or in other words, you
12015 can turn off tool-bars by specifying tool-bar-lines zero. */
12016 if (!WINDOWP (f->tool_bar_window)
12017 || (w = XWINDOW (f->tool_bar_window),
12018 WINDOW_TOTAL_LINES (w) == 0))
12019 return 0;
12020
12021 /* Set up an iterator for the tool-bar window. */
12022 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12023 it.first_visible_x = 0;
12024 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12025 row = it.glyph_row;
12026
12027 /* Build a string that represents the contents of the tool-bar. */
12028 build_desired_tool_bar_string (f);
12029 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12030 /* FIXME: This should be controlled by a user option. But it
12031 doesn't make sense to have an R2L tool bar if the menu bar cannot
12032 be drawn also R2L, and making the menu bar R2L is tricky due
12033 toolkit-specific code that implements it. If an R2L tool bar is
12034 ever supported, display_tool_bar_line should also be augmented to
12035 call unproduce_glyphs like display_line and display_string
12036 do. */
12037 it.paragraph_embedding = L2R;
12038
12039 if (f->n_tool_bar_rows == 0)
12040 {
12041 int nlines;
12042
12043 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12044 nlines != WINDOW_TOTAL_LINES (w)))
12045 {
12046 Lisp_Object frame;
12047 int old_height = WINDOW_TOTAL_LINES (w);
12048
12049 XSETFRAME (frame, f);
12050 Fmodify_frame_parameters (frame,
12051 Fcons (Fcons (Qtool_bar_lines,
12052 make_number (nlines)),
12053 Qnil));
12054 if (WINDOW_TOTAL_LINES (w) != old_height)
12055 {
12056 clear_glyph_matrix (w->desired_matrix);
12057 fonts_changed_p = 1;
12058 return 1;
12059 }
12060 }
12061 }
12062
12063 /* Display as many lines as needed to display all tool-bar items. */
12064
12065 if (f->n_tool_bar_rows > 0)
12066 {
12067 int border, rows, height, extra;
12068
12069 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12070 border = XINT (Vtool_bar_border);
12071 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12072 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12073 else if (EQ (Vtool_bar_border, Qborder_width))
12074 border = f->border_width;
12075 else
12076 border = 0;
12077 if (border < 0)
12078 border = 0;
12079
12080 rows = f->n_tool_bar_rows;
12081 height = max (1, (it.last_visible_y - border) / rows);
12082 extra = it.last_visible_y - border - height * rows;
12083
12084 while (it.current_y < it.last_visible_y)
12085 {
12086 int h = 0;
12087 if (extra > 0 && rows-- > 0)
12088 {
12089 h = (extra + rows - 1) / rows;
12090 extra -= h;
12091 }
12092 display_tool_bar_line (&it, height + h);
12093 }
12094 }
12095 else
12096 {
12097 while (it.current_y < it.last_visible_y)
12098 display_tool_bar_line (&it, 0);
12099 }
12100
12101 /* It doesn't make much sense to try scrolling in the tool-bar
12102 window, so don't do it. */
12103 w->desired_matrix->no_scrolling_p = 1;
12104 w->must_be_updated_p = 1;
12105
12106 if (!NILP (Vauto_resize_tool_bars))
12107 {
12108 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12109 int change_height_p = 0;
12110
12111 /* If we couldn't display everything, change the tool-bar's
12112 height if there is room for more. */
12113 if (IT_STRING_CHARPOS (it) < it.end_charpos
12114 && it.current_y < max_tool_bar_height)
12115 change_height_p = 1;
12116
12117 row = it.glyph_row - 1;
12118
12119 /* If there are blank lines at the end, except for a partially
12120 visible blank line at the end that is smaller than
12121 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12122 if (!row->displays_text_p
12123 && row->height >= FRAME_LINE_HEIGHT (f))
12124 change_height_p = 1;
12125
12126 /* If row displays tool-bar items, but is partially visible,
12127 change the tool-bar's height. */
12128 if (row->displays_text_p
12129 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12130 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12131 change_height_p = 1;
12132
12133 /* Resize windows as needed by changing the `tool-bar-lines'
12134 frame parameter. */
12135 if (change_height_p)
12136 {
12137 Lisp_Object frame;
12138 int old_height = WINDOW_TOTAL_LINES (w);
12139 int nrows;
12140 int nlines = tool_bar_lines_needed (f, &nrows);
12141
12142 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12143 && !f->minimize_tool_bar_window_p)
12144 ? (nlines > old_height)
12145 : (nlines != old_height));
12146 f->minimize_tool_bar_window_p = 0;
12147
12148 if (change_height_p)
12149 {
12150 XSETFRAME (frame, f);
12151 Fmodify_frame_parameters (frame,
12152 Fcons (Fcons (Qtool_bar_lines,
12153 make_number (nlines)),
12154 Qnil));
12155 if (WINDOW_TOTAL_LINES (w) != old_height)
12156 {
12157 clear_glyph_matrix (w->desired_matrix);
12158 f->n_tool_bar_rows = nrows;
12159 fonts_changed_p = 1;
12160 return 1;
12161 }
12162 }
12163 }
12164 }
12165
12166 f->minimize_tool_bar_window_p = 0;
12167 return 0;
12168 }
12169
12170
12171 /* Get information about the tool-bar item which is displayed in GLYPH
12172 on frame F. Return in *PROP_IDX the index where tool-bar item
12173 properties start in F->tool_bar_items. Value is zero if
12174 GLYPH doesn't display a tool-bar item. */
12175
12176 static int
12177 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12178 {
12179 Lisp_Object prop;
12180 int success_p;
12181 int charpos;
12182
12183 /* This function can be called asynchronously, which means we must
12184 exclude any possibility that Fget_text_property signals an
12185 error. */
12186 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12187 charpos = max (0, charpos);
12188
12189 /* Get the text property `menu-item' at pos. The value of that
12190 property is the start index of this item's properties in
12191 F->tool_bar_items. */
12192 prop = Fget_text_property (make_number (charpos),
12193 Qmenu_item, f->current_tool_bar_string);
12194 if (INTEGERP (prop))
12195 {
12196 *prop_idx = XINT (prop);
12197 success_p = 1;
12198 }
12199 else
12200 success_p = 0;
12201
12202 return success_p;
12203 }
12204
12205 \f
12206 /* Get information about the tool-bar item at position X/Y on frame F.
12207 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12208 the current matrix of the tool-bar window of F, or NULL if not
12209 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12210 item in F->tool_bar_items. Value is
12211
12212 -1 if X/Y is not on a tool-bar item
12213 0 if X/Y is on the same item that was highlighted before.
12214 1 otherwise. */
12215
12216 static int
12217 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12218 int *hpos, int *vpos, int *prop_idx)
12219 {
12220 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12221 struct window *w = XWINDOW (f->tool_bar_window);
12222 int area;
12223
12224 /* Find the glyph under X/Y. */
12225 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12226 if (*glyph == NULL)
12227 return -1;
12228
12229 /* Get the start of this tool-bar item's properties in
12230 f->tool_bar_items. */
12231 if (!tool_bar_item_info (f, *glyph, prop_idx))
12232 return -1;
12233
12234 /* Is mouse on the highlighted item? */
12235 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12236 && *vpos >= hlinfo->mouse_face_beg_row
12237 && *vpos <= hlinfo->mouse_face_end_row
12238 && (*vpos > hlinfo->mouse_face_beg_row
12239 || *hpos >= hlinfo->mouse_face_beg_col)
12240 && (*vpos < hlinfo->mouse_face_end_row
12241 || *hpos < hlinfo->mouse_face_end_col
12242 || hlinfo->mouse_face_past_end))
12243 return 0;
12244
12245 return 1;
12246 }
12247
12248
12249 /* EXPORT:
12250 Handle mouse button event on the tool-bar of frame F, at
12251 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12252 0 for button release. MODIFIERS is event modifiers for button
12253 release. */
12254
12255 void
12256 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12257 int modifiers)
12258 {
12259 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12260 struct window *w = XWINDOW (f->tool_bar_window);
12261 int hpos, vpos, prop_idx;
12262 struct glyph *glyph;
12263 Lisp_Object enabled_p;
12264
12265 /* If not on the highlighted tool-bar item, return. */
12266 frame_to_window_pixel_xy (w, &x, &y);
12267 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12268 return;
12269
12270 /* If item is disabled, do nothing. */
12271 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12272 if (NILP (enabled_p))
12273 return;
12274
12275 if (down_p)
12276 {
12277 /* Show item in pressed state. */
12278 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12279 last_tool_bar_item = prop_idx;
12280 }
12281 else
12282 {
12283 Lisp_Object key, frame;
12284 struct input_event event;
12285 EVENT_INIT (event);
12286
12287 /* Show item in released state. */
12288 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12289
12290 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12291
12292 XSETFRAME (frame, f);
12293 event.kind = TOOL_BAR_EVENT;
12294 event.frame_or_window = frame;
12295 event.arg = frame;
12296 kbd_buffer_store_event (&event);
12297
12298 event.kind = TOOL_BAR_EVENT;
12299 event.frame_or_window = frame;
12300 event.arg = key;
12301 event.modifiers = modifiers;
12302 kbd_buffer_store_event (&event);
12303 last_tool_bar_item = -1;
12304 }
12305 }
12306
12307
12308 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12309 tool-bar window-relative coordinates X/Y. Called from
12310 note_mouse_highlight. */
12311
12312 static void
12313 note_tool_bar_highlight (struct frame *f, int x, int y)
12314 {
12315 Lisp_Object window = f->tool_bar_window;
12316 struct window *w = XWINDOW (window);
12317 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12318 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12319 int hpos, vpos;
12320 struct glyph *glyph;
12321 struct glyph_row *row;
12322 int i;
12323 Lisp_Object enabled_p;
12324 int prop_idx;
12325 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12326 int mouse_down_p, rc;
12327
12328 /* Function note_mouse_highlight is called with negative X/Y
12329 values when mouse moves outside of the frame. */
12330 if (x <= 0 || y <= 0)
12331 {
12332 clear_mouse_face (hlinfo);
12333 return;
12334 }
12335
12336 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12337 if (rc < 0)
12338 {
12339 /* Not on tool-bar item. */
12340 clear_mouse_face (hlinfo);
12341 return;
12342 }
12343 else if (rc == 0)
12344 /* On same tool-bar item as before. */
12345 goto set_help_echo;
12346
12347 clear_mouse_face (hlinfo);
12348
12349 /* Mouse is down, but on different tool-bar item? */
12350 mouse_down_p = (dpyinfo->grabbed
12351 && f == last_mouse_frame
12352 && FRAME_LIVE_P (f));
12353 if (mouse_down_p
12354 && last_tool_bar_item != prop_idx)
12355 return;
12356
12357 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12358
12359 /* If tool-bar item is not enabled, don't highlight it. */
12360 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12361 if (!NILP (enabled_p))
12362 {
12363 /* Compute the x-position of the glyph. In front and past the
12364 image is a space. We include this in the highlighted area. */
12365 row = MATRIX_ROW (w->current_matrix, vpos);
12366 for (i = x = 0; i < hpos; ++i)
12367 x += row->glyphs[TEXT_AREA][i].pixel_width;
12368
12369 /* Record this as the current active region. */
12370 hlinfo->mouse_face_beg_col = hpos;
12371 hlinfo->mouse_face_beg_row = vpos;
12372 hlinfo->mouse_face_beg_x = x;
12373 hlinfo->mouse_face_beg_y = row->y;
12374 hlinfo->mouse_face_past_end = 0;
12375
12376 hlinfo->mouse_face_end_col = hpos + 1;
12377 hlinfo->mouse_face_end_row = vpos;
12378 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12379 hlinfo->mouse_face_end_y = row->y;
12380 hlinfo->mouse_face_window = window;
12381 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12382
12383 /* Display it as active. */
12384 show_mouse_face (hlinfo, draw);
12385 }
12386
12387 set_help_echo:
12388
12389 /* Set help_echo_string to a help string to display for this tool-bar item.
12390 XTread_socket does the rest. */
12391 help_echo_object = help_echo_window = Qnil;
12392 help_echo_pos = -1;
12393 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12394 if (NILP (help_echo_string))
12395 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12396 }
12397
12398 #endif /* HAVE_WINDOW_SYSTEM */
12399
12400
12401 \f
12402 /************************************************************************
12403 Horizontal scrolling
12404 ************************************************************************/
12405
12406 static int hscroll_window_tree (Lisp_Object);
12407 static int hscroll_windows (Lisp_Object);
12408
12409 /* For all leaf windows in the window tree rooted at WINDOW, set their
12410 hscroll value so that PT is (i) visible in the window, and (ii) so
12411 that it is not within a certain margin at the window's left and
12412 right border. Value is non-zero if any window's hscroll has been
12413 changed. */
12414
12415 static int
12416 hscroll_window_tree (Lisp_Object window)
12417 {
12418 int hscrolled_p = 0;
12419 int hscroll_relative_p = FLOATP (Vhscroll_step);
12420 int hscroll_step_abs = 0;
12421 double hscroll_step_rel = 0;
12422
12423 if (hscroll_relative_p)
12424 {
12425 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12426 if (hscroll_step_rel < 0)
12427 {
12428 hscroll_relative_p = 0;
12429 hscroll_step_abs = 0;
12430 }
12431 }
12432 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12433 {
12434 hscroll_step_abs = XINT (Vhscroll_step);
12435 if (hscroll_step_abs < 0)
12436 hscroll_step_abs = 0;
12437 }
12438 else
12439 hscroll_step_abs = 0;
12440
12441 while (WINDOWP (window))
12442 {
12443 struct window *w = XWINDOW (window);
12444
12445 if (WINDOWP (w->hchild))
12446 hscrolled_p |= hscroll_window_tree (w->hchild);
12447 else if (WINDOWP (w->vchild))
12448 hscrolled_p |= hscroll_window_tree (w->vchild);
12449 else if (w->cursor.vpos >= 0)
12450 {
12451 int h_margin;
12452 int text_area_width;
12453 struct glyph_row *current_cursor_row
12454 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12455 struct glyph_row *desired_cursor_row
12456 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12457 struct glyph_row *cursor_row
12458 = (desired_cursor_row->enabled_p
12459 ? desired_cursor_row
12460 : current_cursor_row);
12461 int row_r2l_p = cursor_row->reversed_p;
12462
12463 text_area_width = window_box_width (w, TEXT_AREA);
12464
12465 /* Scroll when cursor is inside this scroll margin. */
12466 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12467
12468 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12469 /* For left-to-right rows, hscroll when cursor is either
12470 (i) inside the right hscroll margin, or (ii) if it is
12471 inside the left margin and the window is already
12472 hscrolled. */
12473 && ((!row_r2l_p
12474 && ((w->hscroll
12475 && w->cursor.x <= h_margin)
12476 || (cursor_row->enabled_p
12477 && cursor_row->truncated_on_right_p
12478 && (w->cursor.x >= text_area_width - h_margin))))
12479 /* For right-to-left rows, the logic is similar,
12480 except that rules for scrolling to left and right
12481 are reversed. E.g., if cursor.x <= h_margin, we
12482 need to hscroll "to the right" unconditionally,
12483 and that will scroll the screen to the left so as
12484 to reveal the next portion of the row. */
12485 || (row_r2l_p
12486 && ((cursor_row->enabled_p
12487 /* FIXME: It is confusing to set the
12488 truncated_on_right_p flag when R2L rows
12489 are actually truncated on the left. */
12490 && cursor_row->truncated_on_right_p
12491 && w->cursor.x <= h_margin)
12492 || (w->hscroll
12493 && (w->cursor.x >= text_area_width - h_margin))))))
12494 {
12495 struct it it;
12496 ptrdiff_t hscroll;
12497 struct buffer *saved_current_buffer;
12498 ptrdiff_t pt;
12499 int wanted_x;
12500
12501 /* Find point in a display of infinite width. */
12502 saved_current_buffer = current_buffer;
12503 current_buffer = XBUFFER (w->buffer);
12504
12505 if (w == XWINDOW (selected_window))
12506 pt = PT;
12507 else
12508 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12509
12510 /* Move iterator to pt starting at cursor_row->start in
12511 a line with infinite width. */
12512 init_to_row_start (&it, w, cursor_row);
12513 it.last_visible_x = INFINITY;
12514 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12515 current_buffer = saved_current_buffer;
12516
12517 /* Position cursor in window. */
12518 if (!hscroll_relative_p && hscroll_step_abs == 0)
12519 hscroll = max (0, (it.current_x
12520 - (ITERATOR_AT_END_OF_LINE_P (&it)
12521 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12522 : (text_area_width / 2))))
12523 / FRAME_COLUMN_WIDTH (it.f);
12524 else if ((!row_r2l_p
12525 && w->cursor.x >= text_area_width - h_margin)
12526 || (row_r2l_p && w->cursor.x <= h_margin))
12527 {
12528 if (hscroll_relative_p)
12529 wanted_x = text_area_width * (1 - hscroll_step_rel)
12530 - h_margin;
12531 else
12532 wanted_x = text_area_width
12533 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12534 - h_margin;
12535 hscroll
12536 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12537 }
12538 else
12539 {
12540 if (hscroll_relative_p)
12541 wanted_x = text_area_width * hscroll_step_rel
12542 + h_margin;
12543 else
12544 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12545 + h_margin;
12546 hscroll
12547 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12548 }
12549 hscroll = max (hscroll, w->min_hscroll);
12550
12551 /* Don't prevent redisplay optimizations if hscroll
12552 hasn't changed, as it will unnecessarily slow down
12553 redisplay. */
12554 if (w->hscroll != hscroll)
12555 {
12556 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12557 w->hscroll = hscroll;
12558 hscrolled_p = 1;
12559 }
12560 }
12561 }
12562
12563 window = w->next;
12564 }
12565
12566 /* Value is non-zero if hscroll of any leaf window has been changed. */
12567 return hscrolled_p;
12568 }
12569
12570
12571 /* Set hscroll so that cursor is visible and not inside horizontal
12572 scroll margins for all windows in the tree rooted at WINDOW. See
12573 also hscroll_window_tree above. Value is non-zero if any window's
12574 hscroll has been changed. If it has, desired matrices on the frame
12575 of WINDOW are cleared. */
12576
12577 static int
12578 hscroll_windows (Lisp_Object window)
12579 {
12580 int hscrolled_p = hscroll_window_tree (window);
12581 if (hscrolled_p)
12582 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12583 return hscrolled_p;
12584 }
12585
12586
12587 \f
12588 /************************************************************************
12589 Redisplay
12590 ************************************************************************/
12591
12592 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12593 to a non-zero value. This is sometimes handy to have in a debugger
12594 session. */
12595
12596 #ifdef GLYPH_DEBUG
12597
12598 /* First and last unchanged row for try_window_id. */
12599
12600 static int debug_first_unchanged_at_end_vpos;
12601 static int debug_last_unchanged_at_beg_vpos;
12602
12603 /* Delta vpos and y. */
12604
12605 static int debug_dvpos, debug_dy;
12606
12607 /* Delta in characters and bytes for try_window_id. */
12608
12609 static ptrdiff_t debug_delta, debug_delta_bytes;
12610
12611 /* Values of window_end_pos and window_end_vpos at the end of
12612 try_window_id. */
12613
12614 static ptrdiff_t debug_end_vpos;
12615
12616 /* Append a string to W->desired_matrix->method. FMT is a printf
12617 format string. If trace_redisplay_p is non-zero also printf the
12618 resulting string to stderr. */
12619
12620 static void debug_method_add (struct window *, char const *, ...)
12621 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12622
12623 static void
12624 debug_method_add (struct window *w, char const *fmt, ...)
12625 {
12626 char *method = w->desired_matrix->method;
12627 int len = strlen (method);
12628 int size = sizeof w->desired_matrix->method;
12629 int remaining = size - len - 1;
12630 va_list ap;
12631
12632 if (len && remaining)
12633 {
12634 method[len] = '|';
12635 --remaining, ++len;
12636 }
12637
12638 va_start (ap, fmt);
12639 vsnprintf (method + len, remaining + 1, fmt, ap);
12640 va_end (ap);
12641
12642 if (trace_redisplay_p)
12643 fprintf (stderr, "%p (%s): %s\n",
12644 w,
12645 ((BUFFERP (w->buffer)
12646 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12647 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12648 : "no buffer"),
12649 method + len);
12650 }
12651
12652 #endif /* GLYPH_DEBUG */
12653
12654
12655 /* Value is non-zero if all changes in window W, which displays
12656 current_buffer, are in the text between START and END. START is a
12657 buffer position, END is given as a distance from Z. Used in
12658 redisplay_internal for display optimization. */
12659
12660 static int
12661 text_outside_line_unchanged_p (struct window *w,
12662 ptrdiff_t start, ptrdiff_t end)
12663 {
12664 int unchanged_p = 1;
12665
12666 /* If text or overlays have changed, see where. */
12667 if (window_outdated (w))
12668 {
12669 /* Gap in the line? */
12670 if (GPT < start || Z - GPT < end)
12671 unchanged_p = 0;
12672
12673 /* Changes start in front of the line, or end after it? */
12674 if (unchanged_p
12675 && (BEG_UNCHANGED < start - 1
12676 || END_UNCHANGED < end))
12677 unchanged_p = 0;
12678
12679 /* If selective display, can't optimize if changes start at the
12680 beginning of the line. */
12681 if (unchanged_p
12682 && INTEGERP (BVAR (current_buffer, selective_display))
12683 && XINT (BVAR (current_buffer, selective_display)) > 0
12684 && (BEG_UNCHANGED < start || GPT <= start))
12685 unchanged_p = 0;
12686
12687 /* If there are overlays at the start or end of the line, these
12688 may have overlay strings with newlines in them. A change at
12689 START, for instance, may actually concern the display of such
12690 overlay strings as well, and they are displayed on different
12691 lines. So, quickly rule out this case. (For the future, it
12692 might be desirable to implement something more telling than
12693 just BEG/END_UNCHANGED.) */
12694 if (unchanged_p)
12695 {
12696 if (BEG + BEG_UNCHANGED == start
12697 && overlay_touches_p (start))
12698 unchanged_p = 0;
12699 if (END_UNCHANGED == end
12700 && overlay_touches_p (Z - end))
12701 unchanged_p = 0;
12702 }
12703
12704 /* Under bidi reordering, adding or deleting a character in the
12705 beginning of a paragraph, before the first strong directional
12706 character, can change the base direction of the paragraph (unless
12707 the buffer specifies a fixed paragraph direction), which will
12708 require to redisplay the whole paragraph. It might be worthwhile
12709 to find the paragraph limits and widen the range of redisplayed
12710 lines to that, but for now just give up this optimization. */
12711 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12712 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12713 unchanged_p = 0;
12714 }
12715
12716 return unchanged_p;
12717 }
12718
12719
12720 /* Do a frame update, taking possible shortcuts into account. This is
12721 the main external entry point for redisplay.
12722
12723 If the last redisplay displayed an echo area message and that message
12724 is no longer requested, we clear the echo area or bring back the
12725 mini-buffer if that is in use. */
12726
12727 void
12728 redisplay (void)
12729 {
12730 redisplay_internal ();
12731 }
12732
12733
12734 static Lisp_Object
12735 overlay_arrow_string_or_property (Lisp_Object var)
12736 {
12737 Lisp_Object val;
12738
12739 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12740 return val;
12741
12742 return Voverlay_arrow_string;
12743 }
12744
12745 /* Return 1 if there are any overlay-arrows in current_buffer. */
12746 static int
12747 overlay_arrow_in_current_buffer_p (void)
12748 {
12749 Lisp_Object vlist;
12750
12751 for (vlist = Voverlay_arrow_variable_list;
12752 CONSP (vlist);
12753 vlist = XCDR (vlist))
12754 {
12755 Lisp_Object var = XCAR (vlist);
12756 Lisp_Object val;
12757
12758 if (!SYMBOLP (var))
12759 continue;
12760 val = find_symbol_value (var);
12761 if (MARKERP (val)
12762 && current_buffer == XMARKER (val)->buffer)
12763 return 1;
12764 }
12765 return 0;
12766 }
12767
12768
12769 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12770 has changed. */
12771
12772 static int
12773 overlay_arrows_changed_p (void)
12774 {
12775 Lisp_Object vlist;
12776
12777 for (vlist = Voverlay_arrow_variable_list;
12778 CONSP (vlist);
12779 vlist = XCDR (vlist))
12780 {
12781 Lisp_Object var = XCAR (vlist);
12782 Lisp_Object val, pstr;
12783
12784 if (!SYMBOLP (var))
12785 continue;
12786 val = find_symbol_value (var);
12787 if (!MARKERP (val))
12788 continue;
12789 if (! EQ (COERCE_MARKER (val),
12790 Fget (var, Qlast_arrow_position))
12791 || ! (pstr = overlay_arrow_string_or_property (var),
12792 EQ (pstr, Fget (var, Qlast_arrow_string))))
12793 return 1;
12794 }
12795 return 0;
12796 }
12797
12798 /* Mark overlay arrows to be updated on next redisplay. */
12799
12800 static void
12801 update_overlay_arrows (int up_to_date)
12802 {
12803 Lisp_Object vlist;
12804
12805 for (vlist = Voverlay_arrow_variable_list;
12806 CONSP (vlist);
12807 vlist = XCDR (vlist))
12808 {
12809 Lisp_Object var = XCAR (vlist);
12810
12811 if (!SYMBOLP (var))
12812 continue;
12813
12814 if (up_to_date > 0)
12815 {
12816 Lisp_Object val = find_symbol_value (var);
12817 Fput (var, Qlast_arrow_position,
12818 COERCE_MARKER (val));
12819 Fput (var, Qlast_arrow_string,
12820 overlay_arrow_string_or_property (var));
12821 }
12822 else if (up_to_date < 0
12823 || !NILP (Fget (var, Qlast_arrow_position)))
12824 {
12825 Fput (var, Qlast_arrow_position, Qt);
12826 Fput (var, Qlast_arrow_string, Qt);
12827 }
12828 }
12829 }
12830
12831
12832 /* Return overlay arrow string to display at row.
12833 Return integer (bitmap number) for arrow bitmap in left fringe.
12834 Return nil if no overlay arrow. */
12835
12836 static Lisp_Object
12837 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12838 {
12839 Lisp_Object vlist;
12840
12841 for (vlist = Voverlay_arrow_variable_list;
12842 CONSP (vlist);
12843 vlist = XCDR (vlist))
12844 {
12845 Lisp_Object var = XCAR (vlist);
12846 Lisp_Object val;
12847
12848 if (!SYMBOLP (var))
12849 continue;
12850
12851 val = find_symbol_value (var);
12852
12853 if (MARKERP (val)
12854 && current_buffer == XMARKER (val)->buffer
12855 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12856 {
12857 if (FRAME_WINDOW_P (it->f)
12858 /* FIXME: if ROW->reversed_p is set, this should test
12859 the right fringe, not the left one. */
12860 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12861 {
12862 #ifdef HAVE_WINDOW_SYSTEM
12863 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12864 {
12865 int fringe_bitmap;
12866 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12867 return make_number (fringe_bitmap);
12868 }
12869 #endif
12870 return make_number (-1); /* Use default arrow bitmap. */
12871 }
12872 return overlay_arrow_string_or_property (var);
12873 }
12874 }
12875
12876 return Qnil;
12877 }
12878
12879 /* Return 1 if point moved out of or into a composition. Otherwise
12880 return 0. PREV_BUF and PREV_PT are the last point buffer and
12881 position. BUF and PT are the current point buffer and position. */
12882
12883 static int
12884 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12885 struct buffer *buf, ptrdiff_t pt)
12886 {
12887 ptrdiff_t start, end;
12888 Lisp_Object prop;
12889 Lisp_Object buffer;
12890
12891 XSETBUFFER (buffer, buf);
12892 /* Check a composition at the last point if point moved within the
12893 same buffer. */
12894 if (prev_buf == buf)
12895 {
12896 if (prev_pt == pt)
12897 /* Point didn't move. */
12898 return 0;
12899
12900 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12901 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12902 && COMPOSITION_VALID_P (start, end, prop)
12903 && start < prev_pt && end > prev_pt)
12904 /* The last point was within the composition. Return 1 iff
12905 point moved out of the composition. */
12906 return (pt <= start || pt >= end);
12907 }
12908
12909 /* Check a composition at the current point. */
12910 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12911 && find_composition (pt, -1, &start, &end, &prop, buffer)
12912 && COMPOSITION_VALID_P (start, end, prop)
12913 && start < pt && end > pt);
12914 }
12915
12916
12917 /* Reconsider the setting of B->clip_changed which is displayed
12918 in window W. */
12919
12920 static void
12921 reconsider_clip_changes (struct window *w, struct buffer *b)
12922 {
12923 if (b->clip_changed
12924 && !NILP (w->window_end_valid)
12925 && w->current_matrix->buffer == b
12926 && w->current_matrix->zv == BUF_ZV (b)
12927 && w->current_matrix->begv == BUF_BEGV (b))
12928 b->clip_changed = 0;
12929
12930 /* If display wasn't paused, and W is not a tool bar window, see if
12931 point has been moved into or out of a composition. In that case,
12932 we set b->clip_changed to 1 to force updating the screen. If
12933 b->clip_changed has already been set to 1, we can skip this
12934 check. */
12935 if (!b->clip_changed
12936 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12937 {
12938 ptrdiff_t pt;
12939
12940 if (w == XWINDOW (selected_window))
12941 pt = PT;
12942 else
12943 pt = marker_position (w->pointm);
12944
12945 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12946 || pt != w->last_point)
12947 && check_point_in_composition (w->current_matrix->buffer,
12948 w->last_point,
12949 XBUFFER (w->buffer), pt))
12950 b->clip_changed = 1;
12951 }
12952 }
12953 \f
12954
12955 /* Select FRAME to forward the values of frame-local variables into C
12956 variables so that the redisplay routines can access those values
12957 directly. */
12958
12959 static void
12960 select_frame_for_redisplay (Lisp_Object frame)
12961 {
12962 Lisp_Object tail, tem;
12963 Lisp_Object old = selected_frame;
12964 struct Lisp_Symbol *sym;
12965
12966 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12967
12968 selected_frame = frame;
12969
12970 do {
12971 for (tail = XFRAME (frame)->param_alist;
12972 CONSP (tail); tail = XCDR (tail))
12973 if (CONSP (XCAR (tail))
12974 && (tem = XCAR (XCAR (tail)),
12975 SYMBOLP (tem))
12976 && (sym = indirect_variable (XSYMBOL (tem)),
12977 sym->redirect == SYMBOL_LOCALIZED)
12978 && sym->val.blv->frame_local)
12979 /* Use find_symbol_value rather than Fsymbol_value
12980 to avoid an error if it is void. */
12981 find_symbol_value (tem);
12982 } while (!EQ (frame, old) && (frame = old, 1));
12983 }
12984
12985 /* Make sure that previously selected OLD_FRAME is selected unless it has been
12986 deleted (by an X connection failure during redisplay, for example). */
12987
12988 static void
12989 ensure_selected_frame (Lisp_Object old_frame)
12990 {
12991 if (!EQ (old_frame, selected_frame) && FRAME_LIVE_P (XFRAME (old_frame)))
12992 select_frame_for_redisplay (old_frame);
12993 }
12994
12995 #define STOP_POLLING \
12996 do { if (! polling_stopped_here) stop_polling (); \
12997 polling_stopped_here = 1; } while (0)
12998
12999 #define RESUME_POLLING \
13000 do { if (polling_stopped_here) start_polling (); \
13001 polling_stopped_here = 0; } while (0)
13002
13003
13004 /* Perhaps in the future avoid recentering windows if it
13005 is not necessary; currently that causes some problems. */
13006
13007 static void
13008 redisplay_internal (void)
13009 {
13010 struct window *w = XWINDOW (selected_window);
13011 struct window *sw;
13012 struct frame *fr;
13013 int pending;
13014 int must_finish = 0;
13015 struct text_pos tlbufpos, tlendpos;
13016 int number_of_visible_frames;
13017 ptrdiff_t count, count1;
13018 struct frame *sf;
13019 int polling_stopped_here = 0;
13020 Lisp_Object tail, frame, old_frame = selected_frame;
13021 struct backtrace backtrace;
13022
13023 /* Non-zero means redisplay has to consider all windows on all
13024 frames. Zero means, only selected_window is considered. */
13025 int consider_all_windows_p;
13026
13027 /* Non-zero means redisplay has to redisplay the miniwindow. */
13028 int update_miniwindow_p = 0;
13029
13030 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13031
13032 /* No redisplay if running in batch mode or frame is not yet fully
13033 initialized, or redisplay is explicitly turned off by setting
13034 Vinhibit_redisplay. */
13035 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13036 || !NILP (Vinhibit_redisplay))
13037 return;
13038
13039 /* Don't examine these until after testing Vinhibit_redisplay.
13040 When Emacs is shutting down, perhaps because its connection to
13041 X has dropped, we should not look at them at all. */
13042 fr = XFRAME (w->frame);
13043 sf = SELECTED_FRAME ();
13044
13045 if (!fr->glyphs_initialized_p)
13046 return;
13047
13048 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13049 if (popup_activated ())
13050 return;
13051 #endif
13052
13053 /* I don't think this happens but let's be paranoid. */
13054 if (redisplaying_p)
13055 return;
13056
13057 /* Record a function that clears redisplaying_p
13058 when we leave this function. */
13059 count = SPECPDL_INDEX ();
13060 record_unwind_protect (unwind_redisplay, selected_frame);
13061 redisplaying_p = 1;
13062 specbind (Qinhibit_free_realized_faces, Qnil);
13063
13064 /* Record this function, so it appears on the profiler's backtraces. */
13065 backtrace.next = backtrace_list;
13066 backtrace.function = Qredisplay_internal;
13067 backtrace.args = &Qnil;
13068 backtrace.nargs = 0;
13069 backtrace.debug_on_exit = 0;
13070 backtrace_list = &backtrace;
13071
13072 FOR_EACH_FRAME (tail, frame)
13073 XFRAME (frame)->already_hscrolled_p = 0;
13074
13075 retry:
13076 /* Remember the currently selected window. */
13077 sw = w;
13078
13079 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13080 selected_frame and selected_window to be temporarily out-of-sync so
13081 when we come back here via `goto retry', we need to resync because we
13082 may need to run Elisp code (via prepare_menu_bars). */
13083 ensure_selected_frame (old_frame);
13084
13085 pending = 0;
13086 reconsider_clip_changes (w, current_buffer);
13087 last_escape_glyph_frame = NULL;
13088 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13089 last_glyphless_glyph_frame = NULL;
13090 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13091
13092 /* If new fonts have been loaded that make a glyph matrix adjustment
13093 necessary, do it. */
13094 if (fonts_changed_p)
13095 {
13096 adjust_glyphs (NULL);
13097 ++windows_or_buffers_changed;
13098 fonts_changed_p = 0;
13099 }
13100
13101 /* If face_change_count is non-zero, init_iterator will free all
13102 realized faces, which includes the faces referenced from current
13103 matrices. So, we can't reuse current matrices in this case. */
13104 if (face_change_count)
13105 ++windows_or_buffers_changed;
13106
13107 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13108 && FRAME_TTY (sf)->previous_frame != sf)
13109 {
13110 /* Since frames on a single ASCII terminal share the same
13111 display area, displaying a different frame means redisplay
13112 the whole thing. */
13113 windows_or_buffers_changed++;
13114 SET_FRAME_GARBAGED (sf);
13115 #ifndef DOS_NT
13116 set_tty_color_mode (FRAME_TTY (sf), sf);
13117 #endif
13118 FRAME_TTY (sf)->previous_frame = sf;
13119 }
13120
13121 /* Set the visible flags for all frames. Do this before checking for
13122 resized or garbaged frames; they want to know if their frames are
13123 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13124 number_of_visible_frames = 0;
13125
13126 FOR_EACH_FRAME (tail, frame)
13127 {
13128 struct frame *f = XFRAME (frame);
13129
13130 FRAME_SAMPLE_VISIBILITY (f);
13131 if (FRAME_VISIBLE_P (f))
13132 ++number_of_visible_frames;
13133 clear_desired_matrices (f);
13134 }
13135
13136 /* Notice any pending interrupt request to change frame size. */
13137 do_pending_window_change (1);
13138
13139 /* do_pending_window_change could change the selected_window due to
13140 frame resizing which makes the selected window too small. */
13141 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13142 {
13143 sw = w;
13144 reconsider_clip_changes (w, current_buffer);
13145 }
13146
13147 /* Clear frames marked as garbaged. */
13148 clear_garbaged_frames ();
13149
13150 /* Build menubar and tool-bar items. */
13151 if (NILP (Vmemory_full))
13152 prepare_menu_bars ();
13153
13154 if (windows_or_buffers_changed)
13155 update_mode_lines++;
13156
13157 /* Detect case that we need to write or remove a star in the mode line. */
13158 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13159 {
13160 w->update_mode_line = 1;
13161 if (buffer_shared_and_changed ())
13162 update_mode_lines++;
13163 }
13164
13165 /* Avoid invocation of point motion hooks by `current_column' below. */
13166 count1 = SPECPDL_INDEX ();
13167 specbind (Qinhibit_point_motion_hooks, Qt);
13168
13169 if (mode_line_update_needed (w))
13170 w->update_mode_line = 1;
13171
13172 unbind_to (count1, Qnil);
13173
13174 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13175
13176 consider_all_windows_p = (update_mode_lines
13177 || buffer_shared_and_changed ()
13178 || cursor_type_changed);
13179
13180 /* If specs for an arrow have changed, do thorough redisplay
13181 to ensure we remove any arrow that should no longer exist. */
13182 if (overlay_arrows_changed_p ())
13183 consider_all_windows_p = windows_or_buffers_changed = 1;
13184
13185 /* Normally the message* functions will have already displayed and
13186 updated the echo area, but the frame may have been trashed, or
13187 the update may have been preempted, so display the echo area
13188 again here. Checking message_cleared_p captures the case that
13189 the echo area should be cleared. */
13190 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13191 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13192 || (message_cleared_p
13193 && minibuf_level == 0
13194 /* If the mini-window is currently selected, this means the
13195 echo-area doesn't show through. */
13196 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13197 {
13198 int window_height_changed_p = echo_area_display (0);
13199
13200 if (message_cleared_p)
13201 update_miniwindow_p = 1;
13202
13203 must_finish = 1;
13204
13205 /* If we don't display the current message, don't clear the
13206 message_cleared_p flag, because, if we did, we wouldn't clear
13207 the echo area in the next redisplay which doesn't preserve
13208 the echo area. */
13209 if (!display_last_displayed_message_p)
13210 message_cleared_p = 0;
13211
13212 if (fonts_changed_p)
13213 goto retry;
13214 else if (window_height_changed_p)
13215 {
13216 consider_all_windows_p = 1;
13217 ++update_mode_lines;
13218 ++windows_or_buffers_changed;
13219
13220 /* If window configuration was changed, frames may have been
13221 marked garbaged. Clear them or we will experience
13222 surprises wrt scrolling. */
13223 clear_garbaged_frames ();
13224 }
13225 }
13226 else if (EQ (selected_window, minibuf_window)
13227 && (current_buffer->clip_changed || window_outdated (w))
13228 && resize_mini_window (w, 0))
13229 {
13230 /* Resized active mini-window to fit the size of what it is
13231 showing if its contents might have changed. */
13232 must_finish = 1;
13233 /* FIXME: this causes all frames to be updated, which seems unnecessary
13234 since only the current frame needs to be considered. This function
13235 needs to be rewritten with two variables, consider_all_windows and
13236 consider_all_frames. */
13237 consider_all_windows_p = 1;
13238 ++windows_or_buffers_changed;
13239 ++update_mode_lines;
13240
13241 /* If window configuration was changed, frames may have been
13242 marked garbaged. Clear them or we will experience
13243 surprises wrt scrolling. */
13244 clear_garbaged_frames ();
13245 }
13246
13247
13248 /* If showing the region, and mark has changed, we must redisplay
13249 the whole window. The assignment to this_line_start_pos prevents
13250 the optimization directly below this if-statement. */
13251 if (((!NILP (Vtransient_mark_mode)
13252 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13253 != !NILP (w->region_showing))
13254 || (!NILP (w->region_showing)
13255 && !EQ (w->region_showing,
13256 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13257 CHARPOS (this_line_start_pos) = 0;
13258
13259 /* Optimize the case that only the line containing the cursor in the
13260 selected window has changed. Variables starting with this_ are
13261 set in display_line and record information about the line
13262 containing the cursor. */
13263 tlbufpos = this_line_start_pos;
13264 tlendpos = this_line_end_pos;
13265 if (!consider_all_windows_p
13266 && CHARPOS (tlbufpos) > 0
13267 && !w->update_mode_line
13268 && !current_buffer->clip_changed
13269 && !current_buffer->prevent_redisplay_optimizations_p
13270 && FRAME_VISIBLE_P (XFRAME (w->frame))
13271 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13272 /* Make sure recorded data applies to current buffer, etc. */
13273 && this_line_buffer == current_buffer
13274 && current_buffer == XBUFFER (w->buffer)
13275 && !w->force_start
13276 && !w->optional_new_start
13277 /* Point must be on the line that we have info recorded about. */
13278 && PT >= CHARPOS (tlbufpos)
13279 && PT <= Z - CHARPOS (tlendpos)
13280 /* All text outside that line, including its final newline,
13281 must be unchanged. */
13282 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13283 CHARPOS (tlendpos)))
13284 {
13285 if (CHARPOS (tlbufpos) > BEGV
13286 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13287 && (CHARPOS (tlbufpos) == ZV
13288 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13289 /* Former continuation line has disappeared by becoming empty. */
13290 goto cancel;
13291 else if (window_outdated (w) || MINI_WINDOW_P (w))
13292 {
13293 /* We have to handle the case of continuation around a
13294 wide-column character (see the comment in indent.c around
13295 line 1340).
13296
13297 For instance, in the following case:
13298
13299 -------- Insert --------
13300 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13301 J_I_ ==> J_I_ `^^' are cursors.
13302 ^^ ^^
13303 -------- --------
13304
13305 As we have to redraw the line above, we cannot use this
13306 optimization. */
13307
13308 struct it it;
13309 int line_height_before = this_line_pixel_height;
13310
13311 /* Note that start_display will handle the case that the
13312 line starting at tlbufpos is a continuation line. */
13313 start_display (&it, w, tlbufpos);
13314
13315 /* Implementation note: It this still necessary? */
13316 if (it.current_x != this_line_start_x)
13317 goto cancel;
13318
13319 TRACE ((stderr, "trying display optimization 1\n"));
13320 w->cursor.vpos = -1;
13321 overlay_arrow_seen = 0;
13322 it.vpos = this_line_vpos;
13323 it.current_y = this_line_y;
13324 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13325 display_line (&it);
13326
13327 /* If line contains point, is not continued,
13328 and ends at same distance from eob as before, we win. */
13329 if (w->cursor.vpos >= 0
13330 /* Line is not continued, otherwise this_line_start_pos
13331 would have been set to 0 in display_line. */
13332 && CHARPOS (this_line_start_pos)
13333 /* Line ends as before. */
13334 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13335 /* Line has same height as before. Otherwise other lines
13336 would have to be shifted up or down. */
13337 && this_line_pixel_height == line_height_before)
13338 {
13339 /* If this is not the window's last line, we must adjust
13340 the charstarts of the lines below. */
13341 if (it.current_y < it.last_visible_y)
13342 {
13343 struct glyph_row *row
13344 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13345 ptrdiff_t delta, delta_bytes;
13346
13347 /* We used to distinguish between two cases here,
13348 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13349 when the line ends in a newline or the end of the
13350 buffer's accessible portion. But both cases did
13351 the same, so they were collapsed. */
13352 delta = (Z
13353 - CHARPOS (tlendpos)
13354 - MATRIX_ROW_START_CHARPOS (row));
13355 delta_bytes = (Z_BYTE
13356 - BYTEPOS (tlendpos)
13357 - MATRIX_ROW_START_BYTEPOS (row));
13358
13359 increment_matrix_positions (w->current_matrix,
13360 this_line_vpos + 1,
13361 w->current_matrix->nrows,
13362 delta, delta_bytes);
13363 }
13364
13365 /* If this row displays text now but previously didn't,
13366 or vice versa, w->window_end_vpos may have to be
13367 adjusted. */
13368 if ((it.glyph_row - 1)->displays_text_p)
13369 {
13370 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13371 wset_window_end_vpos (w, make_number (this_line_vpos));
13372 }
13373 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13374 && this_line_vpos > 0)
13375 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13376 wset_window_end_valid (w, Qnil);
13377
13378 /* Update hint: No need to try to scroll in update_window. */
13379 w->desired_matrix->no_scrolling_p = 1;
13380
13381 #ifdef GLYPH_DEBUG
13382 *w->desired_matrix->method = 0;
13383 debug_method_add (w, "optimization 1");
13384 #endif
13385 #ifdef HAVE_WINDOW_SYSTEM
13386 update_window_fringes (w, 0);
13387 #endif
13388 goto update;
13389 }
13390 else
13391 goto cancel;
13392 }
13393 else if (/* Cursor position hasn't changed. */
13394 PT == w->last_point
13395 /* Make sure the cursor was last displayed
13396 in this window. Otherwise we have to reposition it. */
13397 && 0 <= w->cursor.vpos
13398 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13399 {
13400 if (!must_finish)
13401 {
13402 do_pending_window_change (1);
13403 /* If selected_window changed, redisplay again. */
13404 if (WINDOWP (selected_window)
13405 && (w = XWINDOW (selected_window)) != sw)
13406 goto retry;
13407
13408 /* We used to always goto end_of_redisplay here, but this
13409 isn't enough if we have a blinking cursor. */
13410 if (w->cursor_off_p == w->last_cursor_off_p)
13411 goto end_of_redisplay;
13412 }
13413 goto update;
13414 }
13415 /* If highlighting the region, or if the cursor is in the echo area,
13416 then we can't just move the cursor. */
13417 else if (! (!NILP (Vtransient_mark_mode)
13418 && !NILP (BVAR (current_buffer, mark_active)))
13419 && (EQ (selected_window,
13420 BVAR (current_buffer, last_selected_window))
13421 || highlight_nonselected_windows)
13422 && NILP (w->region_showing)
13423 && NILP (Vshow_trailing_whitespace)
13424 && !cursor_in_echo_area)
13425 {
13426 struct it it;
13427 struct glyph_row *row;
13428
13429 /* Skip from tlbufpos to PT and see where it is. Note that
13430 PT may be in invisible text. If so, we will end at the
13431 next visible position. */
13432 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13433 NULL, DEFAULT_FACE_ID);
13434 it.current_x = this_line_start_x;
13435 it.current_y = this_line_y;
13436 it.vpos = this_line_vpos;
13437
13438 /* The call to move_it_to stops in front of PT, but
13439 moves over before-strings. */
13440 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13441
13442 if (it.vpos == this_line_vpos
13443 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13444 row->enabled_p))
13445 {
13446 eassert (this_line_vpos == it.vpos);
13447 eassert (this_line_y == it.current_y);
13448 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13449 #ifdef GLYPH_DEBUG
13450 *w->desired_matrix->method = 0;
13451 debug_method_add (w, "optimization 3");
13452 #endif
13453 goto update;
13454 }
13455 else
13456 goto cancel;
13457 }
13458
13459 cancel:
13460 /* Text changed drastically or point moved off of line. */
13461 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13462 }
13463
13464 CHARPOS (this_line_start_pos) = 0;
13465 consider_all_windows_p |= buffer_shared_and_changed ();
13466 ++clear_face_cache_count;
13467 #ifdef HAVE_WINDOW_SYSTEM
13468 ++clear_image_cache_count;
13469 #endif
13470
13471 /* Build desired matrices, and update the display. If
13472 consider_all_windows_p is non-zero, do it for all windows on all
13473 frames. Otherwise do it for selected_window, only. */
13474
13475 if (consider_all_windows_p)
13476 {
13477 FOR_EACH_FRAME (tail, frame)
13478 XFRAME (frame)->updated_p = 0;
13479
13480 FOR_EACH_FRAME (tail, frame)
13481 {
13482 struct frame *f = XFRAME (frame);
13483
13484 /* We don't have to do anything for unselected terminal
13485 frames. */
13486 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13487 && !EQ (FRAME_TTY (f)->top_frame, frame))
13488 continue;
13489
13490 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13491 {
13492 if (! EQ (frame, selected_frame))
13493 /* Select the frame, for the sake of frame-local
13494 variables. */
13495 select_frame_for_redisplay (frame);
13496
13497 /* Mark all the scroll bars to be removed; we'll redeem
13498 the ones we want when we redisplay their windows. */
13499 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13500 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13501
13502 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13503 redisplay_windows (FRAME_ROOT_WINDOW (f));
13504
13505 /* The X error handler may have deleted that frame. */
13506 if (!FRAME_LIVE_P (f))
13507 continue;
13508
13509 /* Any scroll bars which redisplay_windows should have
13510 nuked should now go away. */
13511 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13512 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13513
13514 /* If fonts changed, display again. */
13515 /* ??? rms: I suspect it is a mistake to jump all the way
13516 back to retry here. It should just retry this frame. */
13517 if (fonts_changed_p)
13518 goto retry;
13519
13520 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13521 {
13522 /* See if we have to hscroll. */
13523 if (!f->already_hscrolled_p)
13524 {
13525 f->already_hscrolled_p = 1;
13526 if (hscroll_windows (f->root_window))
13527 goto retry;
13528 }
13529
13530 /* Prevent various kinds of signals during display
13531 update. stdio is not robust about handling
13532 signals, which can cause an apparent I/O
13533 error. */
13534 if (interrupt_input)
13535 unrequest_sigio ();
13536 STOP_POLLING;
13537
13538 /* Update the display. */
13539 set_window_update_flags (XWINDOW (f->root_window), 1);
13540 pending |= update_frame (f, 0, 0);
13541 f->updated_p = 1;
13542 }
13543 }
13544 }
13545
13546 /* We played a bit fast-and-loose above and allowed selected_frame
13547 and selected_window to be temporarily out-of-sync but let's make
13548 sure this stays contained. */
13549 ensure_selected_frame (old_frame);
13550 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13551
13552 if (!pending)
13553 {
13554 /* Do the mark_window_display_accurate after all windows have
13555 been redisplayed because this call resets flags in buffers
13556 which are needed for proper redisplay. */
13557 FOR_EACH_FRAME (tail, frame)
13558 {
13559 struct frame *f = XFRAME (frame);
13560 if (f->updated_p)
13561 {
13562 mark_window_display_accurate (f->root_window, 1);
13563 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13564 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13565 }
13566 }
13567 }
13568 }
13569 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13570 {
13571 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13572 struct frame *mini_frame;
13573
13574 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13575 /* Use list_of_error, not Qerror, so that
13576 we catch only errors and don't run the debugger. */
13577 internal_condition_case_1 (redisplay_window_1, selected_window,
13578 list_of_error,
13579 redisplay_window_error);
13580 if (update_miniwindow_p)
13581 internal_condition_case_1 (redisplay_window_1, mini_window,
13582 list_of_error,
13583 redisplay_window_error);
13584
13585 /* Compare desired and current matrices, perform output. */
13586
13587 update:
13588 /* If fonts changed, display again. */
13589 if (fonts_changed_p)
13590 goto retry;
13591
13592 /* Prevent various kinds of signals during display update.
13593 stdio is not robust about handling signals,
13594 which can cause an apparent I/O error. */
13595 if (interrupt_input)
13596 unrequest_sigio ();
13597 STOP_POLLING;
13598
13599 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13600 {
13601 if (hscroll_windows (selected_window))
13602 goto retry;
13603
13604 XWINDOW (selected_window)->must_be_updated_p = 1;
13605 pending = update_frame (sf, 0, 0);
13606 }
13607
13608 /* We may have called echo_area_display at the top of this
13609 function. If the echo area is on another frame, that may
13610 have put text on a frame other than the selected one, so the
13611 above call to update_frame would not have caught it. Catch
13612 it here. */
13613 mini_window = FRAME_MINIBUF_WINDOW (sf);
13614 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13615
13616 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13617 {
13618 XWINDOW (mini_window)->must_be_updated_p = 1;
13619 pending |= update_frame (mini_frame, 0, 0);
13620 if (!pending && hscroll_windows (mini_window))
13621 goto retry;
13622 }
13623 }
13624
13625 /* If display was paused because of pending input, make sure we do a
13626 thorough update the next time. */
13627 if (pending)
13628 {
13629 /* Prevent the optimization at the beginning of
13630 redisplay_internal that tries a single-line update of the
13631 line containing the cursor in the selected window. */
13632 CHARPOS (this_line_start_pos) = 0;
13633
13634 /* Let the overlay arrow be updated the next time. */
13635 update_overlay_arrows (0);
13636
13637 /* If we pause after scrolling, some rows in the current
13638 matrices of some windows are not valid. */
13639 if (!WINDOW_FULL_WIDTH_P (w)
13640 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13641 update_mode_lines = 1;
13642 }
13643 else
13644 {
13645 if (!consider_all_windows_p)
13646 {
13647 /* This has already been done above if
13648 consider_all_windows_p is set. */
13649 mark_window_display_accurate_1 (w, 1);
13650
13651 /* Say overlay arrows are up to date. */
13652 update_overlay_arrows (1);
13653
13654 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13655 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13656 }
13657
13658 update_mode_lines = 0;
13659 windows_or_buffers_changed = 0;
13660 cursor_type_changed = 0;
13661 }
13662
13663 /* Start SIGIO interrupts coming again. Having them off during the
13664 code above makes it less likely one will discard output, but not
13665 impossible, since there might be stuff in the system buffer here.
13666 But it is much hairier to try to do anything about that. */
13667 if (interrupt_input)
13668 request_sigio ();
13669 RESUME_POLLING;
13670
13671 /* If a frame has become visible which was not before, redisplay
13672 again, so that we display it. Expose events for such a frame
13673 (which it gets when becoming visible) don't call the parts of
13674 redisplay constructing glyphs, so simply exposing a frame won't
13675 display anything in this case. So, we have to display these
13676 frames here explicitly. */
13677 if (!pending)
13678 {
13679 int new_count = 0;
13680
13681 FOR_EACH_FRAME (tail, frame)
13682 {
13683 int this_is_visible = 0;
13684
13685 if (XFRAME (frame)->visible)
13686 this_is_visible = 1;
13687 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13688 if (XFRAME (frame)->visible)
13689 this_is_visible = 1;
13690
13691 if (this_is_visible)
13692 new_count++;
13693 }
13694
13695 if (new_count != number_of_visible_frames)
13696 windows_or_buffers_changed++;
13697 }
13698
13699 /* Change frame size now if a change is pending. */
13700 do_pending_window_change (1);
13701
13702 /* If we just did a pending size change, or have additional
13703 visible frames, or selected_window changed, redisplay again. */
13704 if ((windows_or_buffers_changed && !pending)
13705 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13706 goto retry;
13707
13708 /* Clear the face and image caches.
13709
13710 We used to do this only if consider_all_windows_p. But the cache
13711 needs to be cleared if a timer creates images in the current
13712 buffer (e.g. the test case in Bug#6230). */
13713
13714 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13715 {
13716 clear_face_cache (0);
13717 clear_face_cache_count = 0;
13718 }
13719
13720 #ifdef HAVE_WINDOW_SYSTEM
13721 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13722 {
13723 clear_image_caches (Qnil);
13724 clear_image_cache_count = 0;
13725 }
13726 #endif /* HAVE_WINDOW_SYSTEM */
13727
13728 end_of_redisplay:
13729 backtrace_list = backtrace.next;
13730 unbind_to (count, Qnil);
13731 RESUME_POLLING;
13732 }
13733
13734
13735 /* Redisplay, but leave alone any recent echo area message unless
13736 another message has been requested in its place.
13737
13738 This is useful in situations where you need to redisplay but no
13739 user action has occurred, making it inappropriate for the message
13740 area to be cleared. See tracking_off and
13741 wait_reading_process_output for examples of these situations.
13742
13743 FROM_WHERE is an integer saying from where this function was
13744 called. This is useful for debugging. */
13745
13746 void
13747 redisplay_preserve_echo_area (int from_where)
13748 {
13749 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13750
13751 if (!NILP (echo_area_buffer[1]))
13752 {
13753 /* We have a previously displayed message, but no current
13754 message. Redisplay the previous message. */
13755 display_last_displayed_message_p = 1;
13756 redisplay_internal ();
13757 display_last_displayed_message_p = 0;
13758 }
13759 else
13760 redisplay_internal ();
13761
13762 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13763 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13764 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13765 }
13766
13767
13768 /* Function registered with record_unwind_protect in redisplay_internal.
13769 Clear redisplaying_p. Also select the previously selected frame. */
13770
13771 static Lisp_Object
13772 unwind_redisplay (Lisp_Object old_frame)
13773 {
13774 redisplaying_p = 0;
13775 ensure_selected_frame (old_frame);
13776 return Qnil;
13777 }
13778
13779
13780 /* Mark the display of window W as accurate or inaccurate. If
13781 ACCURATE_P is non-zero mark display of W as accurate. If
13782 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13783 redisplay_internal is called. */
13784
13785 static void
13786 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13787 {
13788 if (BUFFERP (w->buffer))
13789 {
13790 struct buffer *b = XBUFFER (w->buffer);
13791
13792 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13793 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13794 w->last_had_star
13795 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13796
13797 if (accurate_p)
13798 {
13799 b->clip_changed = 0;
13800 b->prevent_redisplay_optimizations_p = 0;
13801
13802 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13803 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13804 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13805 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13806
13807 w->current_matrix->buffer = b;
13808 w->current_matrix->begv = BUF_BEGV (b);
13809 w->current_matrix->zv = BUF_ZV (b);
13810
13811 w->last_cursor = w->cursor;
13812 w->last_cursor_off_p = w->cursor_off_p;
13813
13814 if (w == XWINDOW (selected_window))
13815 w->last_point = BUF_PT (b);
13816 else
13817 w->last_point = marker_position (w->pointm);
13818 }
13819 }
13820
13821 if (accurate_p)
13822 {
13823 wset_window_end_valid (w, w->buffer);
13824 w->update_mode_line = 0;
13825 }
13826 }
13827
13828
13829 /* Mark the display of windows in the window tree rooted at WINDOW as
13830 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13831 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13832 be redisplayed the next time redisplay_internal is called. */
13833
13834 void
13835 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13836 {
13837 struct window *w;
13838
13839 for (; !NILP (window); window = w->next)
13840 {
13841 w = XWINDOW (window);
13842 mark_window_display_accurate_1 (w, accurate_p);
13843
13844 if (!NILP (w->vchild))
13845 mark_window_display_accurate (w->vchild, accurate_p);
13846 if (!NILP (w->hchild))
13847 mark_window_display_accurate (w->hchild, accurate_p);
13848 }
13849
13850 if (accurate_p)
13851 {
13852 update_overlay_arrows (1);
13853 }
13854 else
13855 {
13856 /* Force a thorough redisplay the next time by setting
13857 last_arrow_position and last_arrow_string to t, which is
13858 unequal to any useful value of Voverlay_arrow_... */
13859 update_overlay_arrows (-1);
13860 }
13861 }
13862
13863
13864 /* Return value in display table DP (Lisp_Char_Table *) for character
13865 C. Since a display table doesn't have any parent, we don't have to
13866 follow parent. Do not call this function directly but use the
13867 macro DISP_CHAR_VECTOR. */
13868
13869 Lisp_Object
13870 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13871 {
13872 Lisp_Object val;
13873
13874 if (ASCII_CHAR_P (c))
13875 {
13876 val = dp->ascii;
13877 if (SUB_CHAR_TABLE_P (val))
13878 val = XSUB_CHAR_TABLE (val)->contents[c];
13879 }
13880 else
13881 {
13882 Lisp_Object table;
13883
13884 XSETCHAR_TABLE (table, dp);
13885 val = char_table_ref (table, c);
13886 }
13887 if (NILP (val))
13888 val = dp->defalt;
13889 return val;
13890 }
13891
13892
13893 \f
13894 /***********************************************************************
13895 Window Redisplay
13896 ***********************************************************************/
13897
13898 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13899
13900 static void
13901 redisplay_windows (Lisp_Object window)
13902 {
13903 while (!NILP (window))
13904 {
13905 struct window *w = XWINDOW (window);
13906
13907 if (!NILP (w->hchild))
13908 redisplay_windows (w->hchild);
13909 else if (!NILP (w->vchild))
13910 redisplay_windows (w->vchild);
13911 else if (!NILP (w->buffer))
13912 {
13913 displayed_buffer = XBUFFER (w->buffer);
13914 /* Use list_of_error, not Qerror, so that
13915 we catch only errors and don't run the debugger. */
13916 internal_condition_case_1 (redisplay_window_0, window,
13917 list_of_error,
13918 redisplay_window_error);
13919 }
13920
13921 window = w->next;
13922 }
13923 }
13924
13925 static Lisp_Object
13926 redisplay_window_error (Lisp_Object ignore)
13927 {
13928 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13929 return Qnil;
13930 }
13931
13932 static Lisp_Object
13933 redisplay_window_0 (Lisp_Object window)
13934 {
13935 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13936 redisplay_window (window, 0);
13937 return Qnil;
13938 }
13939
13940 static Lisp_Object
13941 redisplay_window_1 (Lisp_Object window)
13942 {
13943 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13944 redisplay_window (window, 1);
13945 return Qnil;
13946 }
13947 \f
13948
13949 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13950 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13951 which positions recorded in ROW differ from current buffer
13952 positions.
13953
13954 Return 0 if cursor is not on this row, 1 otherwise. */
13955
13956 static int
13957 set_cursor_from_row (struct window *w, struct glyph_row *row,
13958 struct glyph_matrix *matrix,
13959 ptrdiff_t delta, ptrdiff_t delta_bytes,
13960 int dy, int dvpos)
13961 {
13962 struct glyph *glyph = row->glyphs[TEXT_AREA];
13963 struct glyph *end = glyph + row->used[TEXT_AREA];
13964 struct glyph *cursor = NULL;
13965 /* The last known character position in row. */
13966 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13967 int x = row->x;
13968 ptrdiff_t pt_old = PT - delta;
13969 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13970 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13971 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13972 /* A glyph beyond the edge of TEXT_AREA which we should never
13973 touch. */
13974 struct glyph *glyphs_end = end;
13975 /* Non-zero means we've found a match for cursor position, but that
13976 glyph has the avoid_cursor_p flag set. */
13977 int match_with_avoid_cursor = 0;
13978 /* Non-zero means we've seen at least one glyph that came from a
13979 display string. */
13980 int string_seen = 0;
13981 /* Largest and smallest buffer positions seen so far during scan of
13982 glyph row. */
13983 ptrdiff_t bpos_max = pos_before;
13984 ptrdiff_t bpos_min = pos_after;
13985 /* Last buffer position covered by an overlay string with an integer
13986 `cursor' property. */
13987 ptrdiff_t bpos_covered = 0;
13988 /* Non-zero means the display string on which to display the cursor
13989 comes from a text property, not from an overlay. */
13990 int string_from_text_prop = 0;
13991
13992 /* Don't even try doing anything if called for a mode-line or
13993 header-line row, since the rest of the code isn't prepared to
13994 deal with such calamities. */
13995 eassert (!row->mode_line_p);
13996 if (row->mode_line_p)
13997 return 0;
13998
13999 /* Skip over glyphs not having an object at the start and the end of
14000 the row. These are special glyphs like truncation marks on
14001 terminal frames. */
14002 if (row->displays_text_p)
14003 {
14004 if (!row->reversed_p)
14005 {
14006 while (glyph < end
14007 && INTEGERP (glyph->object)
14008 && glyph->charpos < 0)
14009 {
14010 x += glyph->pixel_width;
14011 ++glyph;
14012 }
14013 while (end > glyph
14014 && INTEGERP ((end - 1)->object)
14015 /* CHARPOS is zero for blanks and stretch glyphs
14016 inserted by extend_face_to_end_of_line. */
14017 && (end - 1)->charpos <= 0)
14018 --end;
14019 glyph_before = glyph - 1;
14020 glyph_after = end;
14021 }
14022 else
14023 {
14024 struct glyph *g;
14025
14026 /* If the glyph row is reversed, we need to process it from back
14027 to front, so swap the edge pointers. */
14028 glyphs_end = end = glyph - 1;
14029 glyph += row->used[TEXT_AREA] - 1;
14030
14031 while (glyph > end + 1
14032 && INTEGERP (glyph->object)
14033 && glyph->charpos < 0)
14034 {
14035 --glyph;
14036 x -= glyph->pixel_width;
14037 }
14038 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14039 --glyph;
14040 /* By default, in reversed rows we put the cursor on the
14041 rightmost (first in the reading order) glyph. */
14042 for (g = end + 1; g < glyph; g++)
14043 x += g->pixel_width;
14044 while (end < glyph
14045 && INTEGERP ((end + 1)->object)
14046 && (end + 1)->charpos <= 0)
14047 ++end;
14048 glyph_before = glyph + 1;
14049 glyph_after = end;
14050 }
14051 }
14052 else if (row->reversed_p)
14053 {
14054 /* In R2L rows that don't display text, put the cursor on the
14055 rightmost glyph. Case in point: an empty last line that is
14056 part of an R2L paragraph. */
14057 cursor = end - 1;
14058 /* Avoid placing the cursor on the last glyph of the row, where
14059 on terminal frames we hold the vertical border between
14060 adjacent windows. */
14061 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14062 && !WINDOW_RIGHTMOST_P (w)
14063 && cursor == row->glyphs[LAST_AREA] - 1)
14064 cursor--;
14065 x = -1; /* will be computed below, at label compute_x */
14066 }
14067
14068 /* Step 1: Try to find the glyph whose character position
14069 corresponds to point. If that's not possible, find 2 glyphs
14070 whose character positions are the closest to point, one before
14071 point, the other after it. */
14072 if (!row->reversed_p)
14073 while (/* not marched to end of glyph row */
14074 glyph < end
14075 /* glyph was not inserted by redisplay for internal purposes */
14076 && !INTEGERP (glyph->object))
14077 {
14078 if (BUFFERP (glyph->object))
14079 {
14080 ptrdiff_t dpos = glyph->charpos - pt_old;
14081
14082 if (glyph->charpos > bpos_max)
14083 bpos_max = glyph->charpos;
14084 if (glyph->charpos < bpos_min)
14085 bpos_min = glyph->charpos;
14086 if (!glyph->avoid_cursor_p)
14087 {
14088 /* If we hit point, we've found the glyph on which to
14089 display the cursor. */
14090 if (dpos == 0)
14091 {
14092 match_with_avoid_cursor = 0;
14093 break;
14094 }
14095 /* See if we've found a better approximation to
14096 POS_BEFORE or to POS_AFTER. */
14097 if (0 > dpos && dpos > pos_before - pt_old)
14098 {
14099 pos_before = glyph->charpos;
14100 glyph_before = glyph;
14101 }
14102 else if (0 < dpos && dpos < pos_after - pt_old)
14103 {
14104 pos_after = glyph->charpos;
14105 glyph_after = glyph;
14106 }
14107 }
14108 else if (dpos == 0)
14109 match_with_avoid_cursor = 1;
14110 }
14111 else if (STRINGP (glyph->object))
14112 {
14113 Lisp_Object chprop;
14114 ptrdiff_t glyph_pos = glyph->charpos;
14115
14116 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14117 glyph->object);
14118 if (!NILP (chprop))
14119 {
14120 /* If the string came from a `display' text property,
14121 look up the buffer position of that property and
14122 use that position to update bpos_max, as if we
14123 actually saw such a position in one of the row's
14124 glyphs. This helps with supporting integer values
14125 of `cursor' property on the display string in
14126 situations where most or all of the row's buffer
14127 text is completely covered by display properties,
14128 so that no glyph with valid buffer positions is
14129 ever seen in the row. */
14130 ptrdiff_t prop_pos =
14131 string_buffer_position_lim (glyph->object, pos_before,
14132 pos_after, 0);
14133
14134 if (prop_pos >= pos_before)
14135 bpos_max = prop_pos - 1;
14136 }
14137 if (INTEGERP (chprop))
14138 {
14139 bpos_covered = bpos_max + XINT (chprop);
14140 /* If the `cursor' property covers buffer positions up
14141 to and including point, we should display cursor on
14142 this glyph. Note that, if a `cursor' property on one
14143 of the string's characters has an integer value, we
14144 will break out of the loop below _before_ we get to
14145 the position match above. IOW, integer values of
14146 the `cursor' property override the "exact match for
14147 point" strategy of positioning the cursor. */
14148 /* Implementation note: bpos_max == pt_old when, e.g.,
14149 we are in an empty line, where bpos_max is set to
14150 MATRIX_ROW_START_CHARPOS, see above. */
14151 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14152 {
14153 cursor = glyph;
14154 break;
14155 }
14156 }
14157
14158 string_seen = 1;
14159 }
14160 x += glyph->pixel_width;
14161 ++glyph;
14162 }
14163 else if (glyph > end) /* row is reversed */
14164 while (!INTEGERP (glyph->object))
14165 {
14166 if (BUFFERP (glyph->object))
14167 {
14168 ptrdiff_t dpos = glyph->charpos - pt_old;
14169
14170 if (glyph->charpos > bpos_max)
14171 bpos_max = glyph->charpos;
14172 if (glyph->charpos < bpos_min)
14173 bpos_min = glyph->charpos;
14174 if (!glyph->avoid_cursor_p)
14175 {
14176 if (dpos == 0)
14177 {
14178 match_with_avoid_cursor = 0;
14179 break;
14180 }
14181 if (0 > dpos && dpos > pos_before - pt_old)
14182 {
14183 pos_before = glyph->charpos;
14184 glyph_before = glyph;
14185 }
14186 else if (0 < dpos && dpos < pos_after - pt_old)
14187 {
14188 pos_after = glyph->charpos;
14189 glyph_after = glyph;
14190 }
14191 }
14192 else if (dpos == 0)
14193 match_with_avoid_cursor = 1;
14194 }
14195 else if (STRINGP (glyph->object))
14196 {
14197 Lisp_Object chprop;
14198 ptrdiff_t glyph_pos = glyph->charpos;
14199
14200 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14201 glyph->object);
14202 if (!NILP (chprop))
14203 {
14204 ptrdiff_t prop_pos =
14205 string_buffer_position_lim (glyph->object, pos_before,
14206 pos_after, 0);
14207
14208 if (prop_pos >= pos_before)
14209 bpos_max = prop_pos - 1;
14210 }
14211 if (INTEGERP (chprop))
14212 {
14213 bpos_covered = bpos_max + XINT (chprop);
14214 /* If the `cursor' property covers buffer positions up
14215 to and including point, we should display cursor on
14216 this glyph. */
14217 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14218 {
14219 cursor = glyph;
14220 break;
14221 }
14222 }
14223 string_seen = 1;
14224 }
14225 --glyph;
14226 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14227 {
14228 x--; /* can't use any pixel_width */
14229 break;
14230 }
14231 x -= glyph->pixel_width;
14232 }
14233
14234 /* Step 2: If we didn't find an exact match for point, we need to
14235 look for a proper place to put the cursor among glyphs between
14236 GLYPH_BEFORE and GLYPH_AFTER. */
14237 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14238 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14239 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14240 {
14241 /* An empty line has a single glyph whose OBJECT is zero and
14242 whose CHARPOS is the position of a newline on that line.
14243 Note that on a TTY, there are more glyphs after that, which
14244 were produced by extend_face_to_end_of_line, but their
14245 CHARPOS is zero or negative. */
14246 int empty_line_p =
14247 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14248 && INTEGERP (glyph->object) && glyph->charpos > 0;
14249
14250 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14251 {
14252 ptrdiff_t ellipsis_pos;
14253
14254 /* Scan back over the ellipsis glyphs. */
14255 if (!row->reversed_p)
14256 {
14257 ellipsis_pos = (glyph - 1)->charpos;
14258 while (glyph > row->glyphs[TEXT_AREA]
14259 && (glyph - 1)->charpos == ellipsis_pos)
14260 glyph--, x -= glyph->pixel_width;
14261 /* That loop always goes one position too far, including
14262 the glyph before the ellipsis. So scan forward over
14263 that one. */
14264 x += glyph->pixel_width;
14265 glyph++;
14266 }
14267 else /* row is reversed */
14268 {
14269 ellipsis_pos = (glyph + 1)->charpos;
14270 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14271 && (glyph + 1)->charpos == ellipsis_pos)
14272 glyph++, x += glyph->pixel_width;
14273 x -= glyph->pixel_width;
14274 glyph--;
14275 }
14276 }
14277 else if (match_with_avoid_cursor)
14278 {
14279 cursor = glyph_after;
14280 x = -1;
14281 }
14282 else if (string_seen)
14283 {
14284 int incr = row->reversed_p ? -1 : +1;
14285
14286 /* Need to find the glyph that came out of a string which is
14287 present at point. That glyph is somewhere between
14288 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14289 positioned between POS_BEFORE and POS_AFTER in the
14290 buffer. */
14291 struct glyph *start, *stop;
14292 ptrdiff_t pos = pos_before;
14293
14294 x = -1;
14295
14296 /* If the row ends in a newline from a display string,
14297 reordering could have moved the glyphs belonging to the
14298 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14299 in this case we extend the search to the last glyph in
14300 the row that was not inserted by redisplay. */
14301 if (row->ends_in_newline_from_string_p)
14302 {
14303 glyph_after = end;
14304 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14305 }
14306
14307 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14308 correspond to POS_BEFORE and POS_AFTER, respectively. We
14309 need START and STOP in the order that corresponds to the
14310 row's direction as given by its reversed_p flag. If the
14311 directionality of characters between POS_BEFORE and
14312 POS_AFTER is the opposite of the row's base direction,
14313 these characters will have been reordered for display,
14314 and we need to reverse START and STOP. */
14315 if (!row->reversed_p)
14316 {
14317 start = min (glyph_before, glyph_after);
14318 stop = max (glyph_before, glyph_after);
14319 }
14320 else
14321 {
14322 start = max (glyph_before, glyph_after);
14323 stop = min (glyph_before, glyph_after);
14324 }
14325 for (glyph = start + incr;
14326 row->reversed_p ? glyph > stop : glyph < stop; )
14327 {
14328
14329 /* Any glyphs that come from the buffer are here because
14330 of bidi reordering. Skip them, and only pay
14331 attention to glyphs that came from some string. */
14332 if (STRINGP (glyph->object))
14333 {
14334 Lisp_Object str;
14335 ptrdiff_t tem;
14336 /* If the display property covers the newline, we
14337 need to search for it one position farther. */
14338 ptrdiff_t lim = pos_after
14339 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14340
14341 string_from_text_prop = 0;
14342 str = glyph->object;
14343 tem = string_buffer_position_lim (str, pos, lim, 0);
14344 if (tem == 0 /* from overlay */
14345 || pos <= tem)
14346 {
14347 /* If the string from which this glyph came is
14348 found in the buffer at point, or at position
14349 that is closer to point than pos_after, then
14350 we've found the glyph we've been looking for.
14351 If it comes from an overlay (tem == 0), and
14352 it has the `cursor' property on one of its
14353 glyphs, record that glyph as a candidate for
14354 displaying the cursor. (As in the
14355 unidirectional version, we will display the
14356 cursor on the last candidate we find.) */
14357 if (tem == 0
14358 || tem == pt_old
14359 || (tem - pt_old > 0 && tem < pos_after))
14360 {
14361 /* The glyphs from this string could have
14362 been reordered. Find the one with the
14363 smallest string position. Or there could
14364 be a character in the string with the
14365 `cursor' property, which means display
14366 cursor on that character's glyph. */
14367 ptrdiff_t strpos = glyph->charpos;
14368
14369 if (tem)
14370 {
14371 cursor = glyph;
14372 string_from_text_prop = 1;
14373 }
14374 for ( ;
14375 (row->reversed_p ? glyph > stop : glyph < stop)
14376 && EQ (glyph->object, str);
14377 glyph += incr)
14378 {
14379 Lisp_Object cprop;
14380 ptrdiff_t gpos = glyph->charpos;
14381
14382 cprop = Fget_char_property (make_number (gpos),
14383 Qcursor,
14384 glyph->object);
14385 if (!NILP (cprop))
14386 {
14387 cursor = glyph;
14388 break;
14389 }
14390 if (tem && glyph->charpos < strpos)
14391 {
14392 strpos = glyph->charpos;
14393 cursor = glyph;
14394 }
14395 }
14396
14397 if (tem == pt_old
14398 || (tem - pt_old > 0 && tem < pos_after))
14399 goto compute_x;
14400 }
14401 if (tem)
14402 pos = tem + 1; /* don't find previous instances */
14403 }
14404 /* This string is not what we want; skip all of the
14405 glyphs that came from it. */
14406 while ((row->reversed_p ? glyph > stop : glyph < stop)
14407 && EQ (glyph->object, str))
14408 glyph += incr;
14409 }
14410 else
14411 glyph += incr;
14412 }
14413
14414 /* If we reached the end of the line, and END was from a string,
14415 the cursor is not on this line. */
14416 if (cursor == NULL
14417 && (row->reversed_p ? glyph <= end : glyph >= end)
14418 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14419 && STRINGP (end->object)
14420 && row->continued_p)
14421 return 0;
14422 }
14423 /* A truncated row may not include PT among its character positions.
14424 Setting the cursor inside the scroll margin will trigger
14425 recalculation of hscroll in hscroll_window_tree. But if a
14426 display string covers point, defer to the string-handling
14427 code below to figure this out. */
14428 else if (row->truncated_on_left_p && pt_old < bpos_min)
14429 {
14430 cursor = glyph_before;
14431 x = -1;
14432 }
14433 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14434 /* Zero-width characters produce no glyphs. */
14435 || (!empty_line_p
14436 && (row->reversed_p
14437 ? glyph_after > glyphs_end
14438 : glyph_after < glyphs_end)))
14439 {
14440 cursor = glyph_after;
14441 x = -1;
14442 }
14443 }
14444
14445 compute_x:
14446 if (cursor != NULL)
14447 glyph = cursor;
14448 else if (glyph == glyphs_end
14449 && pos_before == pos_after
14450 && STRINGP ((row->reversed_p
14451 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14452 : row->glyphs[TEXT_AREA])->object))
14453 {
14454 /* If all the glyphs of this row came from strings, put the
14455 cursor on the first glyph of the row. This avoids having the
14456 cursor outside of the text area in this very rare and hard
14457 use case. */
14458 glyph =
14459 row->reversed_p
14460 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14461 : row->glyphs[TEXT_AREA];
14462 }
14463 if (x < 0)
14464 {
14465 struct glyph *g;
14466
14467 /* Need to compute x that corresponds to GLYPH. */
14468 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14469 {
14470 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14471 emacs_abort ();
14472 x += g->pixel_width;
14473 }
14474 }
14475
14476 /* ROW could be part of a continued line, which, under bidi
14477 reordering, might have other rows whose start and end charpos
14478 occlude point. Only set w->cursor if we found a better
14479 approximation to the cursor position than we have from previously
14480 examined candidate rows belonging to the same continued line. */
14481 if (/* we already have a candidate row */
14482 w->cursor.vpos >= 0
14483 /* that candidate is not the row we are processing */
14484 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14485 /* Make sure cursor.vpos specifies a row whose start and end
14486 charpos occlude point, and it is valid candidate for being a
14487 cursor-row. This is because some callers of this function
14488 leave cursor.vpos at the row where the cursor was displayed
14489 during the last redisplay cycle. */
14490 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14491 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14492 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14493 {
14494 struct glyph *g1 =
14495 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14496
14497 /* Don't consider glyphs that are outside TEXT_AREA. */
14498 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14499 return 0;
14500 /* Keep the candidate whose buffer position is the closest to
14501 point or has the `cursor' property. */
14502 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14503 w->cursor.hpos >= 0
14504 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14505 && ((BUFFERP (g1->object)
14506 && (g1->charpos == pt_old /* an exact match always wins */
14507 || (BUFFERP (glyph->object)
14508 && eabs (g1->charpos - pt_old)
14509 < eabs (glyph->charpos - pt_old))))
14510 /* previous candidate is a glyph from a string that has
14511 a non-nil `cursor' property */
14512 || (STRINGP (g1->object)
14513 && (!NILP (Fget_char_property (make_number (g1->charpos),
14514 Qcursor, g1->object))
14515 /* previous candidate is from the same display
14516 string as this one, and the display string
14517 came from a text property */
14518 || (EQ (g1->object, glyph->object)
14519 && string_from_text_prop)
14520 /* this candidate is from newline and its
14521 position is not an exact match */
14522 || (INTEGERP (glyph->object)
14523 && glyph->charpos != pt_old)))))
14524 return 0;
14525 /* If this candidate gives an exact match, use that. */
14526 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14527 /* If this candidate is a glyph created for the
14528 terminating newline of a line, and point is on that
14529 newline, it wins because it's an exact match. */
14530 || (!row->continued_p
14531 && INTEGERP (glyph->object)
14532 && glyph->charpos == 0
14533 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14534 /* Otherwise, keep the candidate that comes from a row
14535 spanning less buffer positions. This may win when one or
14536 both candidate positions are on glyphs that came from
14537 display strings, for which we cannot compare buffer
14538 positions. */
14539 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14540 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14541 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14542 return 0;
14543 }
14544 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14545 w->cursor.x = x;
14546 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14547 w->cursor.y = row->y + dy;
14548
14549 if (w == XWINDOW (selected_window))
14550 {
14551 if (!row->continued_p
14552 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14553 && row->x == 0)
14554 {
14555 this_line_buffer = XBUFFER (w->buffer);
14556
14557 CHARPOS (this_line_start_pos)
14558 = MATRIX_ROW_START_CHARPOS (row) + delta;
14559 BYTEPOS (this_line_start_pos)
14560 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14561
14562 CHARPOS (this_line_end_pos)
14563 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14564 BYTEPOS (this_line_end_pos)
14565 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14566
14567 this_line_y = w->cursor.y;
14568 this_line_pixel_height = row->height;
14569 this_line_vpos = w->cursor.vpos;
14570 this_line_start_x = row->x;
14571 }
14572 else
14573 CHARPOS (this_line_start_pos) = 0;
14574 }
14575
14576 return 1;
14577 }
14578
14579
14580 /* Run window scroll functions, if any, for WINDOW with new window
14581 start STARTP. Sets the window start of WINDOW to that position.
14582
14583 We assume that the window's buffer is really current. */
14584
14585 static struct text_pos
14586 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14587 {
14588 struct window *w = XWINDOW (window);
14589 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14590
14591 if (current_buffer != XBUFFER (w->buffer))
14592 emacs_abort ();
14593
14594 if (!NILP (Vwindow_scroll_functions))
14595 {
14596 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14597 make_number (CHARPOS (startp)));
14598 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14599 /* In case the hook functions switch buffers. */
14600 set_buffer_internal (XBUFFER (w->buffer));
14601 }
14602
14603 return startp;
14604 }
14605
14606
14607 /* Make sure the line containing the cursor is fully visible.
14608 A value of 1 means there is nothing to be done.
14609 (Either the line is fully visible, or it cannot be made so,
14610 or we cannot tell.)
14611
14612 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14613 is higher than window.
14614
14615 A value of 0 means the caller should do scrolling
14616 as if point had gone off the screen. */
14617
14618 static int
14619 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14620 {
14621 struct glyph_matrix *matrix;
14622 struct glyph_row *row;
14623 int window_height;
14624
14625 if (!make_cursor_line_fully_visible_p)
14626 return 1;
14627
14628 /* It's not always possible to find the cursor, e.g, when a window
14629 is full of overlay strings. Don't do anything in that case. */
14630 if (w->cursor.vpos < 0)
14631 return 1;
14632
14633 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14634 row = MATRIX_ROW (matrix, w->cursor.vpos);
14635
14636 /* If the cursor row is not partially visible, there's nothing to do. */
14637 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14638 return 1;
14639
14640 /* If the row the cursor is in is taller than the window's height,
14641 it's not clear what to do, so do nothing. */
14642 window_height = window_box_height (w);
14643 if (row->height >= window_height)
14644 {
14645 if (!force_p || MINI_WINDOW_P (w)
14646 || w->vscroll || w->cursor.vpos == 0)
14647 return 1;
14648 }
14649 return 0;
14650 }
14651
14652
14653 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14654 non-zero means only WINDOW is redisplayed in redisplay_internal.
14655 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14656 in redisplay_window to bring a partially visible line into view in
14657 the case that only the cursor has moved.
14658
14659 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14660 last screen line's vertical height extends past the end of the screen.
14661
14662 Value is
14663
14664 1 if scrolling succeeded
14665
14666 0 if scrolling didn't find point.
14667
14668 -1 if new fonts have been loaded so that we must interrupt
14669 redisplay, adjust glyph matrices, and try again. */
14670
14671 enum
14672 {
14673 SCROLLING_SUCCESS,
14674 SCROLLING_FAILED,
14675 SCROLLING_NEED_LARGER_MATRICES
14676 };
14677
14678 /* If scroll-conservatively is more than this, never recenter.
14679
14680 If you change this, don't forget to update the doc string of
14681 `scroll-conservatively' and the Emacs manual. */
14682 #define SCROLL_LIMIT 100
14683
14684 static int
14685 try_scrolling (Lisp_Object window, int just_this_one_p,
14686 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14687 int temp_scroll_step, int last_line_misfit)
14688 {
14689 struct window *w = XWINDOW (window);
14690 struct frame *f = XFRAME (w->frame);
14691 struct text_pos pos, startp;
14692 struct it it;
14693 int this_scroll_margin, scroll_max, rc, height;
14694 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14695 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14696 Lisp_Object aggressive;
14697 /* We will never try scrolling more than this number of lines. */
14698 int scroll_limit = SCROLL_LIMIT;
14699
14700 #ifdef GLYPH_DEBUG
14701 debug_method_add (w, "try_scrolling");
14702 #endif
14703
14704 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14705
14706 /* Compute scroll margin height in pixels. We scroll when point is
14707 within this distance from the top or bottom of the window. */
14708 if (scroll_margin > 0)
14709 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14710 * FRAME_LINE_HEIGHT (f);
14711 else
14712 this_scroll_margin = 0;
14713
14714 /* Force arg_scroll_conservatively to have a reasonable value, to
14715 avoid scrolling too far away with slow move_it_* functions. Note
14716 that the user can supply scroll-conservatively equal to
14717 `most-positive-fixnum', which can be larger than INT_MAX. */
14718 if (arg_scroll_conservatively > scroll_limit)
14719 {
14720 arg_scroll_conservatively = scroll_limit + 1;
14721 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14722 }
14723 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14724 /* Compute how much we should try to scroll maximally to bring
14725 point into view. */
14726 scroll_max = (max (scroll_step,
14727 max (arg_scroll_conservatively, temp_scroll_step))
14728 * FRAME_LINE_HEIGHT (f));
14729 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14730 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14731 /* We're trying to scroll because of aggressive scrolling but no
14732 scroll_step is set. Choose an arbitrary one. */
14733 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14734 else
14735 scroll_max = 0;
14736
14737 too_near_end:
14738
14739 /* Decide whether to scroll down. */
14740 if (PT > CHARPOS (startp))
14741 {
14742 int scroll_margin_y;
14743
14744 /* Compute the pixel ypos of the scroll margin, then move IT to
14745 either that ypos or PT, whichever comes first. */
14746 start_display (&it, w, startp);
14747 scroll_margin_y = it.last_visible_y - this_scroll_margin
14748 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14749 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14750 (MOVE_TO_POS | MOVE_TO_Y));
14751
14752 if (PT > CHARPOS (it.current.pos))
14753 {
14754 int y0 = line_bottom_y (&it);
14755 /* Compute how many pixels below window bottom to stop searching
14756 for PT. This avoids costly search for PT that is far away if
14757 the user limited scrolling by a small number of lines, but
14758 always finds PT if scroll_conservatively is set to a large
14759 number, such as most-positive-fixnum. */
14760 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14761 int y_to_move = it.last_visible_y + slack;
14762
14763 /* Compute the distance from the scroll margin to PT or to
14764 the scroll limit, whichever comes first. This should
14765 include the height of the cursor line, to make that line
14766 fully visible. */
14767 move_it_to (&it, PT, -1, y_to_move,
14768 -1, MOVE_TO_POS | MOVE_TO_Y);
14769 dy = line_bottom_y (&it) - y0;
14770
14771 if (dy > scroll_max)
14772 return SCROLLING_FAILED;
14773
14774 if (dy > 0)
14775 scroll_down_p = 1;
14776 }
14777 }
14778
14779 if (scroll_down_p)
14780 {
14781 /* Point is in or below the bottom scroll margin, so move the
14782 window start down. If scrolling conservatively, move it just
14783 enough down to make point visible. If scroll_step is set,
14784 move it down by scroll_step. */
14785 if (arg_scroll_conservatively)
14786 amount_to_scroll
14787 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14788 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14789 else if (scroll_step || temp_scroll_step)
14790 amount_to_scroll = scroll_max;
14791 else
14792 {
14793 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14794 height = WINDOW_BOX_TEXT_HEIGHT (w);
14795 if (NUMBERP (aggressive))
14796 {
14797 double float_amount = XFLOATINT (aggressive) * height;
14798 int aggressive_scroll = float_amount;
14799 if (aggressive_scroll == 0 && float_amount > 0)
14800 aggressive_scroll = 1;
14801 /* Don't let point enter the scroll margin near top of
14802 the window. This could happen if the value of
14803 scroll_up_aggressively is too large and there are
14804 non-zero margins, because scroll_up_aggressively
14805 means put point that fraction of window height
14806 _from_the_bottom_margin_. */
14807 if (aggressive_scroll + 2*this_scroll_margin > height)
14808 aggressive_scroll = height - 2*this_scroll_margin;
14809 amount_to_scroll = dy + aggressive_scroll;
14810 }
14811 }
14812
14813 if (amount_to_scroll <= 0)
14814 return SCROLLING_FAILED;
14815
14816 start_display (&it, w, startp);
14817 if (arg_scroll_conservatively <= scroll_limit)
14818 move_it_vertically (&it, amount_to_scroll);
14819 else
14820 {
14821 /* Extra precision for users who set scroll-conservatively
14822 to a large number: make sure the amount we scroll
14823 the window start is never less than amount_to_scroll,
14824 which was computed as distance from window bottom to
14825 point. This matters when lines at window top and lines
14826 below window bottom have different height. */
14827 struct it it1;
14828 void *it1data = NULL;
14829 /* We use a temporary it1 because line_bottom_y can modify
14830 its argument, if it moves one line down; see there. */
14831 int start_y;
14832
14833 SAVE_IT (it1, it, it1data);
14834 start_y = line_bottom_y (&it1);
14835 do {
14836 RESTORE_IT (&it, &it, it1data);
14837 move_it_by_lines (&it, 1);
14838 SAVE_IT (it1, it, it1data);
14839 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14840 }
14841
14842 /* If STARTP is unchanged, move it down another screen line. */
14843 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14844 move_it_by_lines (&it, 1);
14845 startp = it.current.pos;
14846 }
14847 else
14848 {
14849 struct text_pos scroll_margin_pos = startp;
14850
14851 /* See if point is inside the scroll margin at the top of the
14852 window. */
14853 if (this_scroll_margin)
14854 {
14855 start_display (&it, w, startp);
14856 move_it_vertically (&it, this_scroll_margin);
14857 scroll_margin_pos = it.current.pos;
14858 }
14859
14860 if (PT < CHARPOS (scroll_margin_pos))
14861 {
14862 /* Point is in the scroll margin at the top of the window or
14863 above what is displayed in the window. */
14864 int y0, y_to_move;
14865
14866 /* Compute the vertical distance from PT to the scroll
14867 margin position. Move as far as scroll_max allows, or
14868 one screenful, or 10 screen lines, whichever is largest.
14869 Give up if distance is greater than scroll_max or if we
14870 didn't reach the scroll margin position. */
14871 SET_TEXT_POS (pos, PT, PT_BYTE);
14872 start_display (&it, w, pos);
14873 y0 = it.current_y;
14874 y_to_move = max (it.last_visible_y,
14875 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14876 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14877 y_to_move, -1,
14878 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14879 dy = it.current_y - y0;
14880 if (dy > scroll_max
14881 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14882 return SCROLLING_FAILED;
14883
14884 /* Compute new window start. */
14885 start_display (&it, w, startp);
14886
14887 if (arg_scroll_conservatively)
14888 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14889 max (scroll_step, temp_scroll_step));
14890 else if (scroll_step || temp_scroll_step)
14891 amount_to_scroll = scroll_max;
14892 else
14893 {
14894 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14895 height = WINDOW_BOX_TEXT_HEIGHT (w);
14896 if (NUMBERP (aggressive))
14897 {
14898 double float_amount = XFLOATINT (aggressive) * height;
14899 int aggressive_scroll = float_amount;
14900 if (aggressive_scroll == 0 && float_amount > 0)
14901 aggressive_scroll = 1;
14902 /* Don't let point enter the scroll margin near
14903 bottom of the window, if the value of
14904 scroll_down_aggressively happens to be too
14905 large. */
14906 if (aggressive_scroll + 2*this_scroll_margin > height)
14907 aggressive_scroll = height - 2*this_scroll_margin;
14908 amount_to_scroll = dy + aggressive_scroll;
14909 }
14910 }
14911
14912 if (amount_to_scroll <= 0)
14913 return SCROLLING_FAILED;
14914
14915 move_it_vertically_backward (&it, amount_to_scroll);
14916 startp = it.current.pos;
14917 }
14918 }
14919
14920 /* Run window scroll functions. */
14921 startp = run_window_scroll_functions (window, startp);
14922
14923 /* Display the window. Give up if new fonts are loaded, or if point
14924 doesn't appear. */
14925 if (!try_window (window, startp, 0))
14926 rc = SCROLLING_NEED_LARGER_MATRICES;
14927 else if (w->cursor.vpos < 0)
14928 {
14929 clear_glyph_matrix (w->desired_matrix);
14930 rc = SCROLLING_FAILED;
14931 }
14932 else
14933 {
14934 /* Maybe forget recorded base line for line number display. */
14935 if (!just_this_one_p
14936 || current_buffer->clip_changed
14937 || BEG_UNCHANGED < CHARPOS (startp))
14938 wset_base_line_number (w, Qnil);
14939
14940 /* If cursor ends up on a partially visible line,
14941 treat that as being off the bottom of the screen. */
14942 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14943 /* It's possible that the cursor is on the first line of the
14944 buffer, which is partially obscured due to a vscroll
14945 (Bug#7537). In that case, avoid looping forever . */
14946 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14947 {
14948 clear_glyph_matrix (w->desired_matrix);
14949 ++extra_scroll_margin_lines;
14950 goto too_near_end;
14951 }
14952 rc = SCROLLING_SUCCESS;
14953 }
14954
14955 return rc;
14956 }
14957
14958
14959 /* Compute a suitable window start for window W if display of W starts
14960 on a continuation line. Value is non-zero if a new window start
14961 was computed.
14962
14963 The new window start will be computed, based on W's width, starting
14964 from the start of the continued line. It is the start of the
14965 screen line with the minimum distance from the old start W->start. */
14966
14967 static int
14968 compute_window_start_on_continuation_line (struct window *w)
14969 {
14970 struct text_pos pos, start_pos;
14971 int window_start_changed_p = 0;
14972
14973 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14974
14975 /* If window start is on a continuation line... Window start may be
14976 < BEGV in case there's invisible text at the start of the
14977 buffer (M-x rmail, for example). */
14978 if (CHARPOS (start_pos) > BEGV
14979 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14980 {
14981 struct it it;
14982 struct glyph_row *row;
14983
14984 /* Handle the case that the window start is out of range. */
14985 if (CHARPOS (start_pos) < BEGV)
14986 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14987 else if (CHARPOS (start_pos) > ZV)
14988 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14989
14990 /* Find the start of the continued line. This should be fast
14991 because scan_buffer is fast (newline cache). */
14992 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14993 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14994 row, DEFAULT_FACE_ID);
14995 reseat_at_previous_visible_line_start (&it);
14996
14997 /* If the line start is "too far" away from the window start,
14998 say it takes too much time to compute a new window start. */
14999 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15000 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15001 {
15002 int min_distance, distance;
15003
15004 /* Move forward by display lines to find the new window
15005 start. If window width was enlarged, the new start can
15006 be expected to be > the old start. If window width was
15007 decreased, the new window start will be < the old start.
15008 So, we're looking for the display line start with the
15009 minimum distance from the old window start. */
15010 pos = it.current.pos;
15011 min_distance = INFINITY;
15012 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15013 distance < min_distance)
15014 {
15015 min_distance = distance;
15016 pos = it.current.pos;
15017 move_it_by_lines (&it, 1);
15018 }
15019
15020 /* Set the window start there. */
15021 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15022 window_start_changed_p = 1;
15023 }
15024 }
15025
15026 return window_start_changed_p;
15027 }
15028
15029
15030 /* Try cursor movement in case text has not changed in window WINDOW,
15031 with window start STARTP. Value is
15032
15033 CURSOR_MOVEMENT_SUCCESS if successful
15034
15035 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15036
15037 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15038 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15039 we want to scroll as if scroll-step were set to 1. See the code.
15040
15041 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15042 which case we have to abort this redisplay, and adjust matrices
15043 first. */
15044
15045 enum
15046 {
15047 CURSOR_MOVEMENT_SUCCESS,
15048 CURSOR_MOVEMENT_CANNOT_BE_USED,
15049 CURSOR_MOVEMENT_MUST_SCROLL,
15050 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15051 };
15052
15053 static int
15054 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15055 {
15056 struct window *w = XWINDOW (window);
15057 struct frame *f = XFRAME (w->frame);
15058 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15059
15060 #ifdef GLYPH_DEBUG
15061 if (inhibit_try_cursor_movement)
15062 return rc;
15063 #endif
15064
15065 /* Previously, there was a check for Lisp integer in the
15066 if-statement below. Now, this field is converted to
15067 ptrdiff_t, thus zero means invalid position in a buffer. */
15068 eassert (w->last_point > 0);
15069
15070 /* Handle case where text has not changed, only point, and it has
15071 not moved off the frame. */
15072 if (/* Point may be in this window. */
15073 PT >= CHARPOS (startp)
15074 /* Selective display hasn't changed. */
15075 && !current_buffer->clip_changed
15076 /* Function force-mode-line-update is used to force a thorough
15077 redisplay. It sets either windows_or_buffers_changed or
15078 update_mode_lines. So don't take a shortcut here for these
15079 cases. */
15080 && !update_mode_lines
15081 && !windows_or_buffers_changed
15082 && !cursor_type_changed
15083 /* Can't use this case if highlighting a region. When a
15084 region exists, cursor movement has to do more than just
15085 set the cursor. */
15086 && markpos_of_region () < 0
15087 && NILP (w->region_showing)
15088 && NILP (Vshow_trailing_whitespace)
15089 /* This code is not used for mini-buffer for the sake of the case
15090 of redisplaying to replace an echo area message; since in
15091 that case the mini-buffer contents per se are usually
15092 unchanged. This code is of no real use in the mini-buffer
15093 since the handling of this_line_start_pos, etc., in redisplay
15094 handles the same cases. */
15095 && !EQ (window, minibuf_window)
15096 /* When splitting windows or for new windows, it happens that
15097 redisplay is called with a nil window_end_vpos or one being
15098 larger than the window. This should really be fixed in
15099 window.c. I don't have this on my list, now, so we do
15100 approximately the same as the old redisplay code. --gerd. */
15101 && INTEGERP (w->window_end_vpos)
15102 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15103 && (FRAME_WINDOW_P (f)
15104 || !overlay_arrow_in_current_buffer_p ()))
15105 {
15106 int this_scroll_margin, top_scroll_margin;
15107 struct glyph_row *row = NULL;
15108
15109 #ifdef GLYPH_DEBUG
15110 debug_method_add (w, "cursor movement");
15111 #endif
15112
15113 /* Scroll if point within this distance from the top or bottom
15114 of the window. This is a pixel value. */
15115 if (scroll_margin > 0)
15116 {
15117 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15118 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15119 }
15120 else
15121 this_scroll_margin = 0;
15122
15123 top_scroll_margin = this_scroll_margin;
15124 if (WINDOW_WANTS_HEADER_LINE_P (w))
15125 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15126
15127 /* Start with the row the cursor was displayed during the last
15128 not paused redisplay. Give up if that row is not valid. */
15129 if (w->last_cursor.vpos < 0
15130 || w->last_cursor.vpos >= w->current_matrix->nrows)
15131 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15132 else
15133 {
15134 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15135 if (row->mode_line_p)
15136 ++row;
15137 if (!row->enabled_p)
15138 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15139 }
15140
15141 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15142 {
15143 int scroll_p = 0, must_scroll = 0;
15144 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15145
15146 if (PT > w->last_point)
15147 {
15148 /* Point has moved forward. */
15149 while (MATRIX_ROW_END_CHARPOS (row) < PT
15150 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15151 {
15152 eassert (row->enabled_p);
15153 ++row;
15154 }
15155
15156 /* If the end position of a row equals the start
15157 position of the next row, and PT is at that position,
15158 we would rather display cursor in the next line. */
15159 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15160 && MATRIX_ROW_END_CHARPOS (row) == PT
15161 && row < w->current_matrix->rows
15162 + w->current_matrix->nrows - 1
15163 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15164 && !cursor_row_p (row))
15165 ++row;
15166
15167 /* If within the scroll margin, scroll. Note that
15168 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15169 the next line would be drawn, and that
15170 this_scroll_margin can be zero. */
15171 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15172 || PT > MATRIX_ROW_END_CHARPOS (row)
15173 /* Line is completely visible last line in window
15174 and PT is to be set in the next line. */
15175 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15176 && PT == MATRIX_ROW_END_CHARPOS (row)
15177 && !row->ends_at_zv_p
15178 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15179 scroll_p = 1;
15180 }
15181 else if (PT < w->last_point)
15182 {
15183 /* Cursor has to be moved backward. Note that PT >=
15184 CHARPOS (startp) because of the outer if-statement. */
15185 while (!row->mode_line_p
15186 && (MATRIX_ROW_START_CHARPOS (row) > PT
15187 || (MATRIX_ROW_START_CHARPOS (row) == PT
15188 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15189 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15190 row > w->current_matrix->rows
15191 && (row-1)->ends_in_newline_from_string_p))))
15192 && (row->y > top_scroll_margin
15193 || CHARPOS (startp) == BEGV))
15194 {
15195 eassert (row->enabled_p);
15196 --row;
15197 }
15198
15199 /* Consider the following case: Window starts at BEGV,
15200 there is invisible, intangible text at BEGV, so that
15201 display starts at some point START > BEGV. It can
15202 happen that we are called with PT somewhere between
15203 BEGV and START. Try to handle that case. */
15204 if (row < w->current_matrix->rows
15205 || row->mode_line_p)
15206 {
15207 row = w->current_matrix->rows;
15208 if (row->mode_line_p)
15209 ++row;
15210 }
15211
15212 /* Due to newlines in overlay strings, we may have to
15213 skip forward over overlay strings. */
15214 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15215 && MATRIX_ROW_END_CHARPOS (row) == PT
15216 && !cursor_row_p (row))
15217 ++row;
15218
15219 /* If within the scroll margin, scroll. */
15220 if (row->y < top_scroll_margin
15221 && CHARPOS (startp) != BEGV)
15222 scroll_p = 1;
15223 }
15224 else
15225 {
15226 /* Cursor did not move. So don't scroll even if cursor line
15227 is partially visible, as it was so before. */
15228 rc = CURSOR_MOVEMENT_SUCCESS;
15229 }
15230
15231 if (PT < MATRIX_ROW_START_CHARPOS (row)
15232 || PT > MATRIX_ROW_END_CHARPOS (row))
15233 {
15234 /* if PT is not in the glyph row, give up. */
15235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15236 must_scroll = 1;
15237 }
15238 else if (rc != CURSOR_MOVEMENT_SUCCESS
15239 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15240 {
15241 struct glyph_row *row1;
15242
15243 /* If rows are bidi-reordered and point moved, back up
15244 until we find a row that does not belong to a
15245 continuation line. This is because we must consider
15246 all rows of a continued line as candidates for the
15247 new cursor positioning, since row start and end
15248 positions change non-linearly with vertical position
15249 in such rows. */
15250 /* FIXME: Revisit this when glyph ``spilling'' in
15251 continuation lines' rows is implemented for
15252 bidi-reordered rows. */
15253 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15254 MATRIX_ROW_CONTINUATION_LINE_P (row);
15255 --row)
15256 {
15257 /* If we hit the beginning of the displayed portion
15258 without finding the first row of a continued
15259 line, give up. */
15260 if (row <= row1)
15261 {
15262 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15263 break;
15264 }
15265 eassert (row->enabled_p);
15266 }
15267 }
15268 if (must_scroll)
15269 ;
15270 else if (rc != CURSOR_MOVEMENT_SUCCESS
15271 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15272 /* Make sure this isn't a header line by any chance, since
15273 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15274 && !row->mode_line_p
15275 && make_cursor_line_fully_visible_p)
15276 {
15277 if (PT == MATRIX_ROW_END_CHARPOS (row)
15278 && !row->ends_at_zv_p
15279 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15280 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15281 else if (row->height > window_box_height (w))
15282 {
15283 /* If we end up in a partially visible line, let's
15284 make it fully visible, except when it's taller
15285 than the window, in which case we can't do much
15286 about it. */
15287 *scroll_step = 1;
15288 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15289 }
15290 else
15291 {
15292 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15293 if (!cursor_row_fully_visible_p (w, 0, 1))
15294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15295 else
15296 rc = CURSOR_MOVEMENT_SUCCESS;
15297 }
15298 }
15299 else if (scroll_p)
15300 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15301 else if (rc != CURSOR_MOVEMENT_SUCCESS
15302 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15303 {
15304 /* With bidi-reordered rows, there could be more than
15305 one candidate row whose start and end positions
15306 occlude point. We need to let set_cursor_from_row
15307 find the best candidate. */
15308 /* FIXME: Revisit this when glyph ``spilling'' in
15309 continuation lines' rows is implemented for
15310 bidi-reordered rows. */
15311 int rv = 0;
15312
15313 do
15314 {
15315 int at_zv_p = 0, exact_match_p = 0;
15316
15317 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15318 && PT <= MATRIX_ROW_END_CHARPOS (row)
15319 && cursor_row_p (row))
15320 rv |= set_cursor_from_row (w, row, w->current_matrix,
15321 0, 0, 0, 0);
15322 /* As soon as we've found the exact match for point,
15323 or the first suitable row whose ends_at_zv_p flag
15324 is set, we are done. */
15325 at_zv_p =
15326 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15327 if (rv && !at_zv_p
15328 && w->cursor.hpos >= 0
15329 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15330 w->cursor.vpos))
15331 {
15332 struct glyph_row *candidate =
15333 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15334 struct glyph *g =
15335 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15336 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15337
15338 exact_match_p =
15339 (BUFFERP (g->object) && g->charpos == PT)
15340 || (INTEGERP (g->object)
15341 && (g->charpos == PT
15342 || (g->charpos == 0 && endpos - 1 == PT)));
15343 }
15344 if (rv && (at_zv_p || exact_match_p))
15345 {
15346 rc = CURSOR_MOVEMENT_SUCCESS;
15347 break;
15348 }
15349 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15350 break;
15351 ++row;
15352 }
15353 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15354 || row->continued_p)
15355 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15356 || (MATRIX_ROW_START_CHARPOS (row) == PT
15357 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15358 /* If we didn't find any candidate rows, or exited the
15359 loop before all the candidates were examined, signal
15360 to the caller that this method failed. */
15361 if (rc != CURSOR_MOVEMENT_SUCCESS
15362 && !(rv
15363 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15364 && !row->continued_p))
15365 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15366 else if (rv)
15367 rc = CURSOR_MOVEMENT_SUCCESS;
15368 }
15369 else
15370 {
15371 do
15372 {
15373 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15374 {
15375 rc = CURSOR_MOVEMENT_SUCCESS;
15376 break;
15377 }
15378 ++row;
15379 }
15380 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15381 && MATRIX_ROW_START_CHARPOS (row) == PT
15382 && cursor_row_p (row));
15383 }
15384 }
15385 }
15386
15387 return rc;
15388 }
15389
15390 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15391 static
15392 #endif
15393 void
15394 set_vertical_scroll_bar (struct window *w)
15395 {
15396 ptrdiff_t start, end, whole;
15397
15398 /* Calculate the start and end positions for the current window.
15399 At some point, it would be nice to choose between scrollbars
15400 which reflect the whole buffer size, with special markers
15401 indicating narrowing, and scrollbars which reflect only the
15402 visible region.
15403
15404 Note that mini-buffers sometimes aren't displaying any text. */
15405 if (!MINI_WINDOW_P (w)
15406 || (w == XWINDOW (minibuf_window)
15407 && NILP (echo_area_buffer[0])))
15408 {
15409 struct buffer *buf = XBUFFER (w->buffer);
15410 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15411 start = marker_position (w->start) - BUF_BEGV (buf);
15412 /* I don't think this is guaranteed to be right. For the
15413 moment, we'll pretend it is. */
15414 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15415
15416 if (end < start)
15417 end = start;
15418 if (whole < (end - start))
15419 whole = end - start;
15420 }
15421 else
15422 start = end = whole = 0;
15423
15424 /* Indicate what this scroll bar ought to be displaying now. */
15425 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15426 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15427 (w, end - start, whole, start);
15428 }
15429
15430
15431 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15432 selected_window is redisplayed.
15433
15434 We can return without actually redisplaying the window if
15435 fonts_changed_p. In that case, redisplay_internal will
15436 retry. */
15437
15438 static void
15439 redisplay_window (Lisp_Object window, int just_this_one_p)
15440 {
15441 struct window *w = XWINDOW (window);
15442 struct frame *f = XFRAME (w->frame);
15443 struct buffer *buffer = XBUFFER (w->buffer);
15444 struct buffer *old = current_buffer;
15445 struct text_pos lpoint, opoint, startp;
15446 int update_mode_line;
15447 int tem;
15448 struct it it;
15449 /* Record it now because it's overwritten. */
15450 int current_matrix_up_to_date_p = 0;
15451 int used_current_matrix_p = 0;
15452 /* This is less strict than current_matrix_up_to_date_p.
15453 It indicates that the buffer contents and narrowing are unchanged. */
15454 int buffer_unchanged_p = 0;
15455 int temp_scroll_step = 0;
15456 ptrdiff_t count = SPECPDL_INDEX ();
15457 int rc;
15458 int centering_position = -1;
15459 int last_line_misfit = 0;
15460 ptrdiff_t beg_unchanged, end_unchanged;
15461
15462 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15463 opoint = lpoint;
15464
15465 /* W must be a leaf window here. */
15466 eassert (!NILP (w->buffer));
15467 #ifdef GLYPH_DEBUG
15468 *w->desired_matrix->method = 0;
15469 #endif
15470
15471 restart:
15472 reconsider_clip_changes (w, buffer);
15473
15474 /* Has the mode line to be updated? */
15475 update_mode_line = (w->update_mode_line
15476 || update_mode_lines
15477 || buffer->clip_changed
15478 || buffer->prevent_redisplay_optimizations_p);
15479
15480 if (MINI_WINDOW_P (w))
15481 {
15482 if (w == XWINDOW (echo_area_window)
15483 && !NILP (echo_area_buffer[0]))
15484 {
15485 if (update_mode_line)
15486 /* We may have to update a tty frame's menu bar or a
15487 tool-bar. Example `M-x C-h C-h C-g'. */
15488 goto finish_menu_bars;
15489 else
15490 /* We've already displayed the echo area glyphs in this window. */
15491 goto finish_scroll_bars;
15492 }
15493 else if ((w != XWINDOW (minibuf_window)
15494 || minibuf_level == 0)
15495 /* When buffer is nonempty, redisplay window normally. */
15496 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15497 /* Quail displays non-mini buffers in minibuffer window.
15498 In that case, redisplay the window normally. */
15499 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15500 {
15501 /* W is a mini-buffer window, but it's not active, so clear
15502 it. */
15503 int yb = window_text_bottom_y (w);
15504 struct glyph_row *row;
15505 int y;
15506
15507 for (y = 0, row = w->desired_matrix->rows;
15508 y < yb;
15509 y += row->height, ++row)
15510 blank_row (w, row, y);
15511 goto finish_scroll_bars;
15512 }
15513
15514 clear_glyph_matrix (w->desired_matrix);
15515 }
15516
15517 /* Otherwise set up data on this window; select its buffer and point
15518 value. */
15519 /* Really select the buffer, for the sake of buffer-local
15520 variables. */
15521 set_buffer_internal_1 (XBUFFER (w->buffer));
15522
15523 current_matrix_up_to_date_p
15524 = (!NILP (w->window_end_valid)
15525 && !current_buffer->clip_changed
15526 && !current_buffer->prevent_redisplay_optimizations_p
15527 && !window_outdated (w));
15528
15529 /* Run the window-bottom-change-functions
15530 if it is possible that the text on the screen has changed
15531 (either due to modification of the text, or any other reason). */
15532 if (!current_matrix_up_to_date_p
15533 && !NILP (Vwindow_text_change_functions))
15534 {
15535 safe_run_hooks (Qwindow_text_change_functions);
15536 goto restart;
15537 }
15538
15539 beg_unchanged = BEG_UNCHANGED;
15540 end_unchanged = END_UNCHANGED;
15541
15542 SET_TEXT_POS (opoint, PT, PT_BYTE);
15543
15544 specbind (Qinhibit_point_motion_hooks, Qt);
15545
15546 buffer_unchanged_p
15547 = (!NILP (w->window_end_valid)
15548 && !current_buffer->clip_changed
15549 && !window_outdated (w));
15550
15551 /* When windows_or_buffers_changed is non-zero, we can't rely on
15552 the window end being valid, so set it to nil there. */
15553 if (windows_or_buffers_changed)
15554 {
15555 /* If window starts on a continuation line, maybe adjust the
15556 window start in case the window's width changed. */
15557 if (XMARKER (w->start)->buffer == current_buffer)
15558 compute_window_start_on_continuation_line (w);
15559
15560 wset_window_end_valid (w, Qnil);
15561 }
15562
15563 /* Some sanity checks. */
15564 CHECK_WINDOW_END (w);
15565 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15566 emacs_abort ();
15567 if (BYTEPOS (opoint) < CHARPOS (opoint))
15568 emacs_abort ();
15569
15570 if (mode_line_update_needed (w))
15571 update_mode_line = 1;
15572
15573 /* Point refers normally to the selected window. For any other
15574 window, set up appropriate value. */
15575 if (!EQ (window, selected_window))
15576 {
15577 ptrdiff_t new_pt = marker_position (w->pointm);
15578 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15579 if (new_pt < BEGV)
15580 {
15581 new_pt = BEGV;
15582 new_pt_byte = BEGV_BYTE;
15583 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15584 }
15585 else if (new_pt > (ZV - 1))
15586 {
15587 new_pt = ZV;
15588 new_pt_byte = ZV_BYTE;
15589 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15590 }
15591
15592 /* We don't use SET_PT so that the point-motion hooks don't run. */
15593 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15594 }
15595
15596 /* If any of the character widths specified in the display table
15597 have changed, invalidate the width run cache. It's true that
15598 this may be a bit late to catch such changes, but the rest of
15599 redisplay goes (non-fatally) haywire when the display table is
15600 changed, so why should we worry about doing any better? */
15601 if (current_buffer->width_run_cache)
15602 {
15603 struct Lisp_Char_Table *disptab = buffer_display_table ();
15604
15605 if (! disptab_matches_widthtab
15606 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15607 {
15608 invalidate_region_cache (current_buffer,
15609 current_buffer->width_run_cache,
15610 BEG, Z);
15611 recompute_width_table (current_buffer, disptab);
15612 }
15613 }
15614
15615 /* If window-start is screwed up, choose a new one. */
15616 if (XMARKER (w->start)->buffer != current_buffer)
15617 goto recenter;
15618
15619 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15620
15621 /* If someone specified a new starting point but did not insist,
15622 check whether it can be used. */
15623 if (w->optional_new_start
15624 && CHARPOS (startp) >= BEGV
15625 && CHARPOS (startp) <= ZV)
15626 {
15627 w->optional_new_start = 0;
15628 start_display (&it, w, startp);
15629 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15630 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15631 if (IT_CHARPOS (it) == PT)
15632 w->force_start = 1;
15633 /* IT may overshoot PT if text at PT is invisible. */
15634 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15635 w->force_start = 1;
15636 }
15637
15638 force_start:
15639
15640 /* Handle case where place to start displaying has been specified,
15641 unless the specified location is outside the accessible range. */
15642 if (w->force_start || w->frozen_window_start_p)
15643 {
15644 /* We set this later on if we have to adjust point. */
15645 int new_vpos = -1;
15646
15647 w->force_start = 0;
15648 w->vscroll = 0;
15649 wset_window_end_valid (w, Qnil);
15650
15651 /* Forget any recorded base line for line number display. */
15652 if (!buffer_unchanged_p)
15653 wset_base_line_number (w, Qnil);
15654
15655 /* Redisplay the mode line. Select the buffer properly for that.
15656 Also, run the hook window-scroll-functions
15657 because we have scrolled. */
15658 /* Note, we do this after clearing force_start because
15659 if there's an error, it is better to forget about force_start
15660 than to get into an infinite loop calling the hook functions
15661 and having them get more errors. */
15662 if (!update_mode_line
15663 || ! NILP (Vwindow_scroll_functions))
15664 {
15665 update_mode_line = 1;
15666 w->update_mode_line = 1;
15667 startp = run_window_scroll_functions (window, startp);
15668 }
15669
15670 w->last_modified = 0;
15671 w->last_overlay_modified = 0;
15672 if (CHARPOS (startp) < BEGV)
15673 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15674 else if (CHARPOS (startp) > ZV)
15675 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15676
15677 /* Redisplay, then check if cursor has been set during the
15678 redisplay. Give up if new fonts were loaded. */
15679 /* We used to issue a CHECK_MARGINS argument to try_window here,
15680 but this causes scrolling to fail when point begins inside
15681 the scroll margin (bug#148) -- cyd */
15682 if (!try_window (window, startp, 0))
15683 {
15684 w->force_start = 1;
15685 clear_glyph_matrix (w->desired_matrix);
15686 goto need_larger_matrices;
15687 }
15688
15689 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15690 {
15691 /* If point does not appear, try to move point so it does
15692 appear. The desired matrix has been built above, so we
15693 can use it here. */
15694 new_vpos = window_box_height (w) / 2;
15695 }
15696
15697 if (!cursor_row_fully_visible_p (w, 0, 0))
15698 {
15699 /* Point does appear, but on a line partly visible at end of window.
15700 Move it back to a fully-visible line. */
15701 new_vpos = window_box_height (w);
15702 }
15703 else if (w->cursor.vpos >=0)
15704 {
15705 /* Some people insist on not letting point enter the scroll
15706 margin, even though this part handles windows that didn't
15707 scroll at all. */
15708 struct frame *f = XFRAME (w->frame);
15709 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15710 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15711 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15712
15713 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15714 below, which finds the row to move point to, advances by
15715 the Y coordinate of the _next_ row, see the definition of
15716 MATRIX_ROW_BOTTOM_Y. */
15717 if (w->cursor.vpos < margin + header_line)
15718 new_vpos
15719 = pixel_margin + (header_line
15720 ? CURRENT_HEADER_LINE_HEIGHT (w)
15721 : 0) + FRAME_LINE_HEIGHT (f);
15722 else
15723 {
15724 int window_height = window_box_height (w);
15725
15726 if (header_line)
15727 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15728 if (w->cursor.y >= window_height - pixel_margin)
15729 new_vpos = window_height - pixel_margin;
15730 }
15731 }
15732
15733 /* If we need to move point for either of the above reasons,
15734 now actually do it. */
15735 if (new_vpos >= 0)
15736 {
15737 struct glyph_row *row;
15738
15739 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15740 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15741 ++row;
15742
15743 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15744 MATRIX_ROW_START_BYTEPOS (row));
15745
15746 if (w != XWINDOW (selected_window))
15747 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15748 else if (current_buffer == old)
15749 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15750
15751 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15752
15753 /* If we are highlighting the region, then we just changed
15754 the region, so redisplay to show it. */
15755 if (0 <= markpos_of_region ())
15756 {
15757 clear_glyph_matrix (w->desired_matrix);
15758 if (!try_window (window, startp, 0))
15759 goto need_larger_matrices;
15760 }
15761 }
15762
15763 #ifdef GLYPH_DEBUG
15764 debug_method_add (w, "forced window start");
15765 #endif
15766 goto done;
15767 }
15768
15769 /* Handle case where text has not changed, only point, and it has
15770 not moved off the frame, and we are not retrying after hscroll.
15771 (current_matrix_up_to_date_p is nonzero when retrying.) */
15772 if (current_matrix_up_to_date_p
15773 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15774 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15775 {
15776 switch (rc)
15777 {
15778 case CURSOR_MOVEMENT_SUCCESS:
15779 used_current_matrix_p = 1;
15780 goto done;
15781
15782 case CURSOR_MOVEMENT_MUST_SCROLL:
15783 goto try_to_scroll;
15784
15785 default:
15786 emacs_abort ();
15787 }
15788 }
15789 /* If current starting point was originally the beginning of a line
15790 but no longer is, find a new starting point. */
15791 else if (w->start_at_line_beg
15792 && !(CHARPOS (startp) <= BEGV
15793 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15794 {
15795 #ifdef GLYPH_DEBUG
15796 debug_method_add (w, "recenter 1");
15797 #endif
15798 goto recenter;
15799 }
15800
15801 /* Try scrolling with try_window_id. Value is > 0 if update has
15802 been done, it is -1 if we know that the same window start will
15803 not work. It is 0 if unsuccessful for some other reason. */
15804 else if ((tem = try_window_id (w)) != 0)
15805 {
15806 #ifdef GLYPH_DEBUG
15807 debug_method_add (w, "try_window_id %d", tem);
15808 #endif
15809
15810 if (fonts_changed_p)
15811 goto need_larger_matrices;
15812 if (tem > 0)
15813 goto done;
15814
15815 /* Otherwise try_window_id has returned -1 which means that we
15816 don't want the alternative below this comment to execute. */
15817 }
15818 else if (CHARPOS (startp) >= BEGV
15819 && CHARPOS (startp) <= ZV
15820 && PT >= CHARPOS (startp)
15821 && (CHARPOS (startp) < ZV
15822 /* Avoid starting at end of buffer. */
15823 || CHARPOS (startp) == BEGV
15824 || !window_outdated (w)))
15825 {
15826 int d1, d2, d3, d4, d5, d6;
15827
15828 /* If first window line is a continuation line, and window start
15829 is inside the modified region, but the first change is before
15830 current window start, we must select a new window start.
15831
15832 However, if this is the result of a down-mouse event (e.g. by
15833 extending the mouse-drag-overlay), we don't want to select a
15834 new window start, since that would change the position under
15835 the mouse, resulting in an unwanted mouse-movement rather
15836 than a simple mouse-click. */
15837 if (!w->start_at_line_beg
15838 && NILP (do_mouse_tracking)
15839 && CHARPOS (startp) > BEGV
15840 && CHARPOS (startp) > BEG + beg_unchanged
15841 && CHARPOS (startp) <= Z - end_unchanged
15842 /* Even if w->start_at_line_beg is nil, a new window may
15843 start at a line_beg, since that's how set_buffer_window
15844 sets it. So, we need to check the return value of
15845 compute_window_start_on_continuation_line. (See also
15846 bug#197). */
15847 && XMARKER (w->start)->buffer == current_buffer
15848 && compute_window_start_on_continuation_line (w)
15849 /* It doesn't make sense to force the window start like we
15850 do at label force_start if it is already known that point
15851 will not be visible in the resulting window, because
15852 doing so will move point from its correct position
15853 instead of scrolling the window to bring point into view.
15854 See bug#9324. */
15855 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15856 {
15857 w->force_start = 1;
15858 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15859 goto force_start;
15860 }
15861
15862 #ifdef GLYPH_DEBUG
15863 debug_method_add (w, "same window start");
15864 #endif
15865
15866 /* Try to redisplay starting at same place as before.
15867 If point has not moved off frame, accept the results. */
15868 if (!current_matrix_up_to_date_p
15869 /* Don't use try_window_reusing_current_matrix in this case
15870 because a window scroll function can have changed the
15871 buffer. */
15872 || !NILP (Vwindow_scroll_functions)
15873 || MINI_WINDOW_P (w)
15874 || !(used_current_matrix_p
15875 = try_window_reusing_current_matrix (w)))
15876 {
15877 IF_DEBUG (debug_method_add (w, "1"));
15878 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15879 /* -1 means we need to scroll.
15880 0 means we need new matrices, but fonts_changed_p
15881 is set in that case, so we will detect it below. */
15882 goto try_to_scroll;
15883 }
15884
15885 if (fonts_changed_p)
15886 goto need_larger_matrices;
15887
15888 if (w->cursor.vpos >= 0)
15889 {
15890 if (!just_this_one_p
15891 || current_buffer->clip_changed
15892 || BEG_UNCHANGED < CHARPOS (startp))
15893 /* Forget any recorded base line for line number display. */
15894 wset_base_line_number (w, Qnil);
15895
15896 if (!cursor_row_fully_visible_p (w, 1, 0))
15897 {
15898 clear_glyph_matrix (w->desired_matrix);
15899 last_line_misfit = 1;
15900 }
15901 /* Drop through and scroll. */
15902 else
15903 goto done;
15904 }
15905 else
15906 clear_glyph_matrix (w->desired_matrix);
15907 }
15908
15909 try_to_scroll:
15910
15911 w->last_modified = 0;
15912 w->last_overlay_modified = 0;
15913
15914 /* Redisplay the mode line. Select the buffer properly for that. */
15915 if (!update_mode_line)
15916 {
15917 update_mode_line = 1;
15918 w->update_mode_line = 1;
15919 }
15920
15921 /* Try to scroll by specified few lines. */
15922 if ((scroll_conservatively
15923 || emacs_scroll_step
15924 || temp_scroll_step
15925 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15926 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15927 && CHARPOS (startp) >= BEGV
15928 && CHARPOS (startp) <= ZV)
15929 {
15930 /* The function returns -1 if new fonts were loaded, 1 if
15931 successful, 0 if not successful. */
15932 int ss = try_scrolling (window, just_this_one_p,
15933 scroll_conservatively,
15934 emacs_scroll_step,
15935 temp_scroll_step, last_line_misfit);
15936 switch (ss)
15937 {
15938 case SCROLLING_SUCCESS:
15939 goto done;
15940
15941 case SCROLLING_NEED_LARGER_MATRICES:
15942 goto need_larger_matrices;
15943
15944 case SCROLLING_FAILED:
15945 break;
15946
15947 default:
15948 emacs_abort ();
15949 }
15950 }
15951
15952 /* Finally, just choose a place to start which positions point
15953 according to user preferences. */
15954
15955 recenter:
15956
15957 #ifdef GLYPH_DEBUG
15958 debug_method_add (w, "recenter");
15959 #endif
15960
15961 /* w->vscroll = 0; */
15962
15963 /* Forget any previously recorded base line for line number display. */
15964 if (!buffer_unchanged_p)
15965 wset_base_line_number (w, Qnil);
15966
15967 /* Determine the window start relative to point. */
15968 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15969 it.current_y = it.last_visible_y;
15970 if (centering_position < 0)
15971 {
15972 int margin =
15973 scroll_margin > 0
15974 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15975 : 0;
15976 ptrdiff_t margin_pos = CHARPOS (startp);
15977 Lisp_Object aggressive;
15978 int scrolling_up;
15979
15980 /* If there is a scroll margin at the top of the window, find
15981 its character position. */
15982 if (margin
15983 /* Cannot call start_display if startp is not in the
15984 accessible region of the buffer. This can happen when we
15985 have just switched to a different buffer and/or changed
15986 its restriction. In that case, startp is initialized to
15987 the character position 1 (BEGV) because we did not yet
15988 have chance to display the buffer even once. */
15989 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15990 {
15991 struct it it1;
15992 void *it1data = NULL;
15993
15994 SAVE_IT (it1, it, it1data);
15995 start_display (&it1, w, startp);
15996 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15997 margin_pos = IT_CHARPOS (it1);
15998 RESTORE_IT (&it, &it, it1data);
15999 }
16000 scrolling_up = PT > margin_pos;
16001 aggressive =
16002 scrolling_up
16003 ? BVAR (current_buffer, scroll_up_aggressively)
16004 : BVAR (current_buffer, scroll_down_aggressively);
16005
16006 if (!MINI_WINDOW_P (w)
16007 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16008 {
16009 int pt_offset = 0;
16010
16011 /* Setting scroll-conservatively overrides
16012 scroll-*-aggressively. */
16013 if (!scroll_conservatively && NUMBERP (aggressive))
16014 {
16015 double float_amount = XFLOATINT (aggressive);
16016
16017 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16018 if (pt_offset == 0 && float_amount > 0)
16019 pt_offset = 1;
16020 if (pt_offset && margin > 0)
16021 margin -= 1;
16022 }
16023 /* Compute how much to move the window start backward from
16024 point so that point will be displayed where the user
16025 wants it. */
16026 if (scrolling_up)
16027 {
16028 centering_position = it.last_visible_y;
16029 if (pt_offset)
16030 centering_position -= pt_offset;
16031 centering_position -=
16032 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16033 + WINDOW_HEADER_LINE_HEIGHT (w);
16034 /* Don't let point enter the scroll margin near top of
16035 the window. */
16036 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16037 centering_position = margin * FRAME_LINE_HEIGHT (f);
16038 }
16039 else
16040 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16041 }
16042 else
16043 /* Set the window start half the height of the window backward
16044 from point. */
16045 centering_position = window_box_height (w) / 2;
16046 }
16047 move_it_vertically_backward (&it, centering_position);
16048
16049 eassert (IT_CHARPOS (it) >= BEGV);
16050
16051 /* The function move_it_vertically_backward may move over more
16052 than the specified y-distance. If it->w is small, e.g. a
16053 mini-buffer window, we may end up in front of the window's
16054 display area. Start displaying at the start of the line
16055 containing PT in this case. */
16056 if (it.current_y <= 0)
16057 {
16058 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16059 move_it_vertically_backward (&it, 0);
16060 it.current_y = 0;
16061 }
16062
16063 it.current_x = it.hpos = 0;
16064
16065 /* Set the window start position here explicitly, to avoid an
16066 infinite loop in case the functions in window-scroll-functions
16067 get errors. */
16068 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16069
16070 /* Run scroll hooks. */
16071 startp = run_window_scroll_functions (window, it.current.pos);
16072
16073 /* Redisplay the window. */
16074 if (!current_matrix_up_to_date_p
16075 || windows_or_buffers_changed
16076 || cursor_type_changed
16077 /* Don't use try_window_reusing_current_matrix in this case
16078 because it can have changed the buffer. */
16079 || !NILP (Vwindow_scroll_functions)
16080 || !just_this_one_p
16081 || MINI_WINDOW_P (w)
16082 || !(used_current_matrix_p
16083 = try_window_reusing_current_matrix (w)))
16084 try_window (window, startp, 0);
16085
16086 /* If new fonts have been loaded (due to fontsets), give up. We
16087 have to start a new redisplay since we need to re-adjust glyph
16088 matrices. */
16089 if (fonts_changed_p)
16090 goto need_larger_matrices;
16091
16092 /* If cursor did not appear assume that the middle of the window is
16093 in the first line of the window. Do it again with the next line.
16094 (Imagine a window of height 100, displaying two lines of height
16095 60. Moving back 50 from it->last_visible_y will end in the first
16096 line.) */
16097 if (w->cursor.vpos < 0)
16098 {
16099 if (!NILP (w->window_end_valid)
16100 && PT >= Z - XFASTINT (w->window_end_pos))
16101 {
16102 clear_glyph_matrix (w->desired_matrix);
16103 move_it_by_lines (&it, 1);
16104 try_window (window, it.current.pos, 0);
16105 }
16106 else if (PT < IT_CHARPOS (it))
16107 {
16108 clear_glyph_matrix (w->desired_matrix);
16109 move_it_by_lines (&it, -1);
16110 try_window (window, it.current.pos, 0);
16111 }
16112 else
16113 {
16114 /* Not much we can do about it. */
16115 }
16116 }
16117
16118 /* Consider the following case: Window starts at BEGV, there is
16119 invisible, intangible text at BEGV, so that display starts at
16120 some point START > BEGV. It can happen that we are called with
16121 PT somewhere between BEGV and START. Try to handle that case. */
16122 if (w->cursor.vpos < 0)
16123 {
16124 struct glyph_row *row = w->current_matrix->rows;
16125 if (row->mode_line_p)
16126 ++row;
16127 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16128 }
16129
16130 if (!cursor_row_fully_visible_p (w, 0, 0))
16131 {
16132 /* If vscroll is enabled, disable it and try again. */
16133 if (w->vscroll)
16134 {
16135 w->vscroll = 0;
16136 clear_glyph_matrix (w->desired_matrix);
16137 goto recenter;
16138 }
16139
16140 /* Users who set scroll-conservatively to a large number want
16141 point just above/below the scroll margin. If we ended up
16142 with point's row partially visible, move the window start to
16143 make that row fully visible and out of the margin. */
16144 if (scroll_conservatively > SCROLL_LIMIT)
16145 {
16146 int margin =
16147 scroll_margin > 0
16148 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16149 : 0;
16150 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16151
16152 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16153 clear_glyph_matrix (w->desired_matrix);
16154 if (1 == try_window (window, it.current.pos,
16155 TRY_WINDOW_CHECK_MARGINS))
16156 goto done;
16157 }
16158
16159 /* If centering point failed to make the whole line visible,
16160 put point at the top instead. That has to make the whole line
16161 visible, if it can be done. */
16162 if (centering_position == 0)
16163 goto done;
16164
16165 clear_glyph_matrix (w->desired_matrix);
16166 centering_position = 0;
16167 goto recenter;
16168 }
16169
16170 done:
16171
16172 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16173 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16174 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16175
16176 /* Display the mode line, if we must. */
16177 if ((update_mode_line
16178 /* If window not full width, must redo its mode line
16179 if (a) the window to its side is being redone and
16180 (b) we do a frame-based redisplay. This is a consequence
16181 of how inverted lines are drawn in frame-based redisplay. */
16182 || (!just_this_one_p
16183 && !FRAME_WINDOW_P (f)
16184 && !WINDOW_FULL_WIDTH_P (w))
16185 /* Line number to display. */
16186 || INTEGERP (w->base_line_pos)
16187 /* Column number is displayed and different from the one displayed. */
16188 || (!NILP (w->column_number_displayed)
16189 && (XFASTINT (w->column_number_displayed) != current_column ())))
16190 /* This means that the window has a mode line. */
16191 && (WINDOW_WANTS_MODELINE_P (w)
16192 || WINDOW_WANTS_HEADER_LINE_P (w)))
16193 {
16194 display_mode_lines (w);
16195
16196 /* If mode line height has changed, arrange for a thorough
16197 immediate redisplay using the correct mode line height. */
16198 if (WINDOW_WANTS_MODELINE_P (w)
16199 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16200 {
16201 fonts_changed_p = 1;
16202 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16203 = DESIRED_MODE_LINE_HEIGHT (w);
16204 }
16205
16206 /* If header line height has changed, arrange for a thorough
16207 immediate redisplay using the correct header line height. */
16208 if (WINDOW_WANTS_HEADER_LINE_P (w)
16209 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16210 {
16211 fonts_changed_p = 1;
16212 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16213 = DESIRED_HEADER_LINE_HEIGHT (w);
16214 }
16215
16216 if (fonts_changed_p)
16217 goto need_larger_matrices;
16218 }
16219
16220 if (!line_number_displayed
16221 && !BUFFERP (w->base_line_pos))
16222 {
16223 wset_base_line_pos (w, Qnil);
16224 wset_base_line_number (w, Qnil);
16225 }
16226
16227 finish_menu_bars:
16228
16229 /* When we reach a frame's selected window, redo the frame's menu bar. */
16230 if (update_mode_line
16231 && EQ (FRAME_SELECTED_WINDOW (f), window))
16232 {
16233 int redisplay_menu_p = 0;
16234
16235 if (FRAME_WINDOW_P (f))
16236 {
16237 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16238 || defined (HAVE_NS) || defined (USE_GTK)
16239 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16240 #else
16241 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16242 #endif
16243 }
16244 else
16245 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16246
16247 if (redisplay_menu_p)
16248 display_menu_bar (w);
16249
16250 #ifdef HAVE_WINDOW_SYSTEM
16251 if (FRAME_WINDOW_P (f))
16252 {
16253 #if defined (USE_GTK) || defined (HAVE_NS)
16254 if (FRAME_EXTERNAL_TOOL_BAR (f))
16255 redisplay_tool_bar (f);
16256 #else
16257 if (WINDOWP (f->tool_bar_window)
16258 && (FRAME_TOOL_BAR_LINES (f) > 0
16259 || !NILP (Vauto_resize_tool_bars))
16260 && redisplay_tool_bar (f))
16261 ignore_mouse_drag_p = 1;
16262 #endif
16263 }
16264 #endif
16265 }
16266
16267 #ifdef HAVE_WINDOW_SYSTEM
16268 if (FRAME_WINDOW_P (f)
16269 && update_window_fringes (w, (just_this_one_p
16270 || (!used_current_matrix_p && !overlay_arrow_seen)
16271 || w->pseudo_window_p)))
16272 {
16273 update_begin (f);
16274 block_input ();
16275 if (draw_window_fringes (w, 1))
16276 x_draw_vertical_border (w);
16277 unblock_input ();
16278 update_end (f);
16279 }
16280 #endif /* HAVE_WINDOW_SYSTEM */
16281
16282 /* We go to this label, with fonts_changed_p set,
16283 if it is necessary to try again using larger glyph matrices.
16284 We have to redeem the scroll bar even in this case,
16285 because the loop in redisplay_internal expects that. */
16286 need_larger_matrices:
16287 ;
16288 finish_scroll_bars:
16289
16290 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16291 {
16292 /* Set the thumb's position and size. */
16293 set_vertical_scroll_bar (w);
16294
16295 /* Note that we actually used the scroll bar attached to this
16296 window, so it shouldn't be deleted at the end of redisplay. */
16297 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16298 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16299 }
16300
16301 /* Restore current_buffer and value of point in it. The window
16302 update may have changed the buffer, so first make sure `opoint'
16303 is still valid (Bug#6177). */
16304 if (CHARPOS (opoint) < BEGV)
16305 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16306 else if (CHARPOS (opoint) > ZV)
16307 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16308 else
16309 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16310
16311 set_buffer_internal_1 (old);
16312 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16313 shorter. This can be caused by log truncation in *Messages*. */
16314 if (CHARPOS (lpoint) <= ZV)
16315 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16316
16317 unbind_to (count, Qnil);
16318 }
16319
16320
16321 /* Build the complete desired matrix of WINDOW with a window start
16322 buffer position POS.
16323
16324 Value is 1 if successful. It is zero if fonts were loaded during
16325 redisplay which makes re-adjusting glyph matrices necessary, and -1
16326 if point would appear in the scroll margins.
16327 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16328 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16329 set in FLAGS.) */
16330
16331 int
16332 try_window (Lisp_Object window, struct text_pos pos, int flags)
16333 {
16334 struct window *w = XWINDOW (window);
16335 struct it it;
16336 struct glyph_row *last_text_row = NULL;
16337 struct frame *f = XFRAME (w->frame);
16338
16339 /* Make POS the new window start. */
16340 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16341
16342 /* Mark cursor position as unknown. No overlay arrow seen. */
16343 w->cursor.vpos = -1;
16344 overlay_arrow_seen = 0;
16345
16346 /* Initialize iterator and info to start at POS. */
16347 start_display (&it, w, pos);
16348
16349 /* Display all lines of W. */
16350 while (it.current_y < it.last_visible_y)
16351 {
16352 if (display_line (&it))
16353 last_text_row = it.glyph_row - 1;
16354 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16355 return 0;
16356 }
16357
16358 /* Don't let the cursor end in the scroll margins. */
16359 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16360 && !MINI_WINDOW_P (w))
16361 {
16362 int this_scroll_margin;
16363
16364 if (scroll_margin > 0)
16365 {
16366 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16367 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16368 }
16369 else
16370 this_scroll_margin = 0;
16371
16372 if ((w->cursor.y >= 0 /* not vscrolled */
16373 && w->cursor.y < this_scroll_margin
16374 && CHARPOS (pos) > BEGV
16375 && IT_CHARPOS (it) < ZV)
16376 /* rms: considering make_cursor_line_fully_visible_p here
16377 seems to give wrong results. We don't want to recenter
16378 when the last line is partly visible, we want to allow
16379 that case to be handled in the usual way. */
16380 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16381 {
16382 w->cursor.vpos = -1;
16383 clear_glyph_matrix (w->desired_matrix);
16384 return -1;
16385 }
16386 }
16387
16388 /* If bottom moved off end of frame, change mode line percentage. */
16389 if (XFASTINT (w->window_end_pos) <= 0
16390 && Z != IT_CHARPOS (it))
16391 w->update_mode_line = 1;
16392
16393 /* Set window_end_pos to the offset of the last character displayed
16394 on the window from the end of current_buffer. Set
16395 window_end_vpos to its row number. */
16396 if (last_text_row)
16397 {
16398 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16399 w->window_end_bytepos
16400 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16401 wset_window_end_pos
16402 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16403 wset_window_end_vpos
16404 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16405 eassert
16406 (MATRIX_ROW (w->desired_matrix,
16407 XFASTINT (w->window_end_vpos))->displays_text_p);
16408 }
16409 else
16410 {
16411 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16412 wset_window_end_pos (w, make_number (Z - ZV));
16413 wset_window_end_vpos (w, make_number (0));
16414 }
16415
16416 /* But that is not valid info until redisplay finishes. */
16417 wset_window_end_valid (w, Qnil);
16418 return 1;
16419 }
16420
16421
16422 \f
16423 /************************************************************************
16424 Window redisplay reusing current matrix when buffer has not changed
16425 ************************************************************************/
16426
16427 /* Try redisplay of window W showing an unchanged buffer with a
16428 different window start than the last time it was displayed by
16429 reusing its current matrix. Value is non-zero if successful.
16430 W->start is the new window start. */
16431
16432 static int
16433 try_window_reusing_current_matrix (struct window *w)
16434 {
16435 struct frame *f = XFRAME (w->frame);
16436 struct glyph_row *bottom_row;
16437 struct it it;
16438 struct run run;
16439 struct text_pos start, new_start;
16440 int nrows_scrolled, i;
16441 struct glyph_row *last_text_row;
16442 struct glyph_row *last_reused_text_row;
16443 struct glyph_row *start_row;
16444 int start_vpos, min_y, max_y;
16445
16446 #ifdef GLYPH_DEBUG
16447 if (inhibit_try_window_reusing)
16448 return 0;
16449 #endif
16450
16451 if (/* This function doesn't handle terminal frames. */
16452 !FRAME_WINDOW_P (f)
16453 /* Don't try to reuse the display if windows have been split
16454 or such. */
16455 || windows_or_buffers_changed
16456 || cursor_type_changed)
16457 return 0;
16458
16459 /* Can't do this if region may have changed. */
16460 if (0 <= markpos_of_region ()
16461 || !NILP (w->region_showing)
16462 || !NILP (Vshow_trailing_whitespace))
16463 return 0;
16464
16465 /* If top-line visibility has changed, give up. */
16466 if (WINDOW_WANTS_HEADER_LINE_P (w)
16467 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16468 return 0;
16469
16470 /* Give up if old or new display is scrolled vertically. We could
16471 make this function handle this, but right now it doesn't. */
16472 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16473 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16474 return 0;
16475
16476 /* The variable new_start now holds the new window start. The old
16477 start `start' can be determined from the current matrix. */
16478 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16479 start = start_row->minpos;
16480 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16481
16482 /* Clear the desired matrix for the display below. */
16483 clear_glyph_matrix (w->desired_matrix);
16484
16485 if (CHARPOS (new_start) <= CHARPOS (start))
16486 {
16487 /* Don't use this method if the display starts with an ellipsis
16488 displayed for invisible text. It's not easy to handle that case
16489 below, and it's certainly not worth the effort since this is
16490 not a frequent case. */
16491 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16492 return 0;
16493
16494 IF_DEBUG (debug_method_add (w, "twu1"));
16495
16496 /* Display up to a row that can be reused. The variable
16497 last_text_row is set to the last row displayed that displays
16498 text. Note that it.vpos == 0 if or if not there is a
16499 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16500 start_display (&it, w, new_start);
16501 w->cursor.vpos = -1;
16502 last_text_row = last_reused_text_row = NULL;
16503
16504 while (it.current_y < it.last_visible_y
16505 && !fonts_changed_p)
16506 {
16507 /* If we have reached into the characters in the START row,
16508 that means the line boundaries have changed. So we
16509 can't start copying with the row START. Maybe it will
16510 work to start copying with the following row. */
16511 while (IT_CHARPOS (it) > CHARPOS (start))
16512 {
16513 /* Advance to the next row as the "start". */
16514 start_row++;
16515 start = start_row->minpos;
16516 /* If there are no more rows to try, or just one, give up. */
16517 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16518 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16519 || CHARPOS (start) == ZV)
16520 {
16521 clear_glyph_matrix (w->desired_matrix);
16522 return 0;
16523 }
16524
16525 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16526 }
16527 /* If we have reached alignment, we can copy the rest of the
16528 rows. */
16529 if (IT_CHARPOS (it) == CHARPOS (start)
16530 /* Don't accept "alignment" inside a display vector,
16531 since start_row could have started in the middle of
16532 that same display vector (thus their character
16533 positions match), and we have no way of telling if
16534 that is the case. */
16535 && it.current.dpvec_index < 0)
16536 break;
16537
16538 if (display_line (&it))
16539 last_text_row = it.glyph_row - 1;
16540
16541 }
16542
16543 /* A value of current_y < last_visible_y means that we stopped
16544 at the previous window start, which in turn means that we
16545 have at least one reusable row. */
16546 if (it.current_y < it.last_visible_y)
16547 {
16548 struct glyph_row *row;
16549
16550 /* IT.vpos always starts from 0; it counts text lines. */
16551 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16552
16553 /* Find PT if not already found in the lines displayed. */
16554 if (w->cursor.vpos < 0)
16555 {
16556 int dy = it.current_y - start_row->y;
16557
16558 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16559 row = row_containing_pos (w, PT, row, NULL, dy);
16560 if (row)
16561 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16562 dy, nrows_scrolled);
16563 else
16564 {
16565 clear_glyph_matrix (w->desired_matrix);
16566 return 0;
16567 }
16568 }
16569
16570 /* Scroll the display. Do it before the current matrix is
16571 changed. The problem here is that update has not yet
16572 run, i.e. part of the current matrix is not up to date.
16573 scroll_run_hook will clear the cursor, and use the
16574 current matrix to get the height of the row the cursor is
16575 in. */
16576 run.current_y = start_row->y;
16577 run.desired_y = it.current_y;
16578 run.height = it.last_visible_y - it.current_y;
16579
16580 if (run.height > 0 && run.current_y != run.desired_y)
16581 {
16582 update_begin (f);
16583 FRAME_RIF (f)->update_window_begin_hook (w);
16584 FRAME_RIF (f)->clear_window_mouse_face (w);
16585 FRAME_RIF (f)->scroll_run_hook (w, &run);
16586 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16587 update_end (f);
16588 }
16589
16590 /* Shift current matrix down by nrows_scrolled lines. */
16591 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16592 rotate_matrix (w->current_matrix,
16593 start_vpos,
16594 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16595 nrows_scrolled);
16596
16597 /* Disable lines that must be updated. */
16598 for (i = 0; i < nrows_scrolled; ++i)
16599 (start_row + i)->enabled_p = 0;
16600
16601 /* Re-compute Y positions. */
16602 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16603 max_y = it.last_visible_y;
16604 for (row = start_row + nrows_scrolled;
16605 row < bottom_row;
16606 ++row)
16607 {
16608 row->y = it.current_y;
16609 row->visible_height = row->height;
16610
16611 if (row->y < min_y)
16612 row->visible_height -= min_y - row->y;
16613 if (row->y + row->height > max_y)
16614 row->visible_height -= row->y + row->height - max_y;
16615 if (row->fringe_bitmap_periodic_p)
16616 row->redraw_fringe_bitmaps_p = 1;
16617
16618 it.current_y += row->height;
16619
16620 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16621 last_reused_text_row = row;
16622 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16623 break;
16624 }
16625
16626 /* Disable lines in the current matrix which are now
16627 below the window. */
16628 for (++row; row < bottom_row; ++row)
16629 row->enabled_p = row->mode_line_p = 0;
16630 }
16631
16632 /* Update window_end_pos etc.; last_reused_text_row is the last
16633 reused row from the current matrix containing text, if any.
16634 The value of last_text_row is the last displayed line
16635 containing text. */
16636 if (last_reused_text_row)
16637 {
16638 w->window_end_bytepos
16639 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16640 wset_window_end_pos
16641 (w, make_number (Z
16642 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16643 wset_window_end_vpos
16644 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16645 w->current_matrix)));
16646 }
16647 else if (last_text_row)
16648 {
16649 w->window_end_bytepos
16650 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16651 wset_window_end_pos
16652 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16653 wset_window_end_vpos
16654 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16655 w->desired_matrix)));
16656 }
16657 else
16658 {
16659 /* This window must be completely empty. */
16660 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16661 wset_window_end_pos (w, make_number (Z - ZV));
16662 wset_window_end_vpos (w, make_number (0));
16663 }
16664 wset_window_end_valid (w, Qnil);
16665
16666 /* Update hint: don't try scrolling again in update_window. */
16667 w->desired_matrix->no_scrolling_p = 1;
16668
16669 #ifdef GLYPH_DEBUG
16670 debug_method_add (w, "try_window_reusing_current_matrix 1");
16671 #endif
16672 return 1;
16673 }
16674 else if (CHARPOS (new_start) > CHARPOS (start))
16675 {
16676 struct glyph_row *pt_row, *row;
16677 struct glyph_row *first_reusable_row;
16678 struct glyph_row *first_row_to_display;
16679 int dy;
16680 int yb = window_text_bottom_y (w);
16681
16682 /* Find the row starting at new_start, if there is one. Don't
16683 reuse a partially visible line at the end. */
16684 first_reusable_row = start_row;
16685 while (first_reusable_row->enabled_p
16686 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16687 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16688 < CHARPOS (new_start)))
16689 ++first_reusable_row;
16690
16691 /* Give up if there is no row to reuse. */
16692 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16693 || !first_reusable_row->enabled_p
16694 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16695 != CHARPOS (new_start)))
16696 return 0;
16697
16698 /* We can reuse fully visible rows beginning with
16699 first_reusable_row to the end of the window. Set
16700 first_row_to_display to the first row that cannot be reused.
16701 Set pt_row to the row containing point, if there is any. */
16702 pt_row = NULL;
16703 for (first_row_to_display = first_reusable_row;
16704 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16705 ++first_row_to_display)
16706 {
16707 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16708 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16709 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16710 && first_row_to_display->ends_at_zv_p
16711 && pt_row == NULL)))
16712 pt_row = first_row_to_display;
16713 }
16714
16715 /* Start displaying at the start of first_row_to_display. */
16716 eassert (first_row_to_display->y < yb);
16717 init_to_row_start (&it, w, first_row_to_display);
16718
16719 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16720 - start_vpos);
16721 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16722 - nrows_scrolled);
16723 it.current_y = (first_row_to_display->y - first_reusable_row->y
16724 + WINDOW_HEADER_LINE_HEIGHT (w));
16725
16726 /* Display lines beginning with first_row_to_display in the
16727 desired matrix. Set last_text_row to the last row displayed
16728 that displays text. */
16729 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16730 if (pt_row == NULL)
16731 w->cursor.vpos = -1;
16732 last_text_row = NULL;
16733 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16734 if (display_line (&it))
16735 last_text_row = it.glyph_row - 1;
16736
16737 /* If point is in a reused row, adjust y and vpos of the cursor
16738 position. */
16739 if (pt_row)
16740 {
16741 w->cursor.vpos -= nrows_scrolled;
16742 w->cursor.y -= first_reusable_row->y - start_row->y;
16743 }
16744
16745 /* Give up if point isn't in a row displayed or reused. (This
16746 also handles the case where w->cursor.vpos < nrows_scrolled
16747 after the calls to display_line, which can happen with scroll
16748 margins. See bug#1295.) */
16749 if (w->cursor.vpos < 0)
16750 {
16751 clear_glyph_matrix (w->desired_matrix);
16752 return 0;
16753 }
16754
16755 /* Scroll the display. */
16756 run.current_y = first_reusable_row->y;
16757 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16758 run.height = it.last_visible_y - run.current_y;
16759 dy = run.current_y - run.desired_y;
16760
16761 if (run.height)
16762 {
16763 update_begin (f);
16764 FRAME_RIF (f)->update_window_begin_hook (w);
16765 FRAME_RIF (f)->clear_window_mouse_face (w);
16766 FRAME_RIF (f)->scroll_run_hook (w, &run);
16767 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16768 update_end (f);
16769 }
16770
16771 /* Adjust Y positions of reused rows. */
16772 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16773 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16774 max_y = it.last_visible_y;
16775 for (row = first_reusable_row; row < first_row_to_display; ++row)
16776 {
16777 row->y -= dy;
16778 row->visible_height = row->height;
16779 if (row->y < min_y)
16780 row->visible_height -= min_y - row->y;
16781 if (row->y + row->height > max_y)
16782 row->visible_height -= row->y + row->height - max_y;
16783 if (row->fringe_bitmap_periodic_p)
16784 row->redraw_fringe_bitmaps_p = 1;
16785 }
16786
16787 /* Scroll the current matrix. */
16788 eassert (nrows_scrolled > 0);
16789 rotate_matrix (w->current_matrix,
16790 start_vpos,
16791 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16792 -nrows_scrolled);
16793
16794 /* Disable rows not reused. */
16795 for (row -= nrows_scrolled; row < bottom_row; ++row)
16796 row->enabled_p = 0;
16797
16798 /* Point may have moved to a different line, so we cannot assume that
16799 the previous cursor position is valid; locate the correct row. */
16800 if (pt_row)
16801 {
16802 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16803 row < bottom_row
16804 && PT >= MATRIX_ROW_END_CHARPOS (row)
16805 && !row->ends_at_zv_p;
16806 row++)
16807 {
16808 w->cursor.vpos++;
16809 w->cursor.y = row->y;
16810 }
16811 if (row < bottom_row)
16812 {
16813 /* Can't simply scan the row for point with
16814 bidi-reordered glyph rows. Let set_cursor_from_row
16815 figure out where to put the cursor, and if it fails,
16816 give up. */
16817 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16818 {
16819 if (!set_cursor_from_row (w, row, w->current_matrix,
16820 0, 0, 0, 0))
16821 {
16822 clear_glyph_matrix (w->desired_matrix);
16823 return 0;
16824 }
16825 }
16826 else
16827 {
16828 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16829 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16830
16831 for (; glyph < end
16832 && (!BUFFERP (glyph->object)
16833 || glyph->charpos < PT);
16834 glyph++)
16835 {
16836 w->cursor.hpos++;
16837 w->cursor.x += glyph->pixel_width;
16838 }
16839 }
16840 }
16841 }
16842
16843 /* Adjust window end. A null value of last_text_row means that
16844 the window end is in reused rows which in turn means that
16845 only its vpos can have changed. */
16846 if (last_text_row)
16847 {
16848 w->window_end_bytepos
16849 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16850 wset_window_end_pos
16851 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16852 wset_window_end_vpos
16853 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16854 w->desired_matrix)));
16855 }
16856 else
16857 {
16858 wset_window_end_vpos
16859 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16860 }
16861
16862 wset_window_end_valid (w, Qnil);
16863 w->desired_matrix->no_scrolling_p = 1;
16864
16865 #ifdef GLYPH_DEBUG
16866 debug_method_add (w, "try_window_reusing_current_matrix 2");
16867 #endif
16868 return 1;
16869 }
16870
16871 return 0;
16872 }
16873
16874
16875 \f
16876 /************************************************************************
16877 Window redisplay reusing current matrix when buffer has changed
16878 ************************************************************************/
16879
16880 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16881 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16882 ptrdiff_t *, ptrdiff_t *);
16883 static struct glyph_row *
16884 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16885 struct glyph_row *);
16886
16887
16888 /* Return the last row in MATRIX displaying text. If row START is
16889 non-null, start searching with that row. IT gives the dimensions
16890 of the display. Value is null if matrix is empty; otherwise it is
16891 a pointer to the row found. */
16892
16893 static struct glyph_row *
16894 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16895 struct glyph_row *start)
16896 {
16897 struct glyph_row *row, *row_found;
16898
16899 /* Set row_found to the last row in IT->w's current matrix
16900 displaying text. The loop looks funny but think of partially
16901 visible lines. */
16902 row_found = NULL;
16903 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16904 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16905 {
16906 eassert (row->enabled_p);
16907 row_found = row;
16908 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16909 break;
16910 ++row;
16911 }
16912
16913 return row_found;
16914 }
16915
16916
16917 /* Return the last row in the current matrix of W that is not affected
16918 by changes at the start of current_buffer that occurred since W's
16919 current matrix was built. Value is null if no such row exists.
16920
16921 BEG_UNCHANGED us the number of characters unchanged at the start of
16922 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16923 first changed character in current_buffer. Characters at positions <
16924 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16925 when the current matrix was built. */
16926
16927 static struct glyph_row *
16928 find_last_unchanged_at_beg_row (struct window *w)
16929 {
16930 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16931 struct glyph_row *row;
16932 struct glyph_row *row_found = NULL;
16933 int yb = window_text_bottom_y (w);
16934
16935 /* Find the last row displaying unchanged text. */
16936 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16937 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16938 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16939 ++row)
16940 {
16941 if (/* If row ends before first_changed_pos, it is unchanged,
16942 except in some case. */
16943 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16944 /* When row ends in ZV and we write at ZV it is not
16945 unchanged. */
16946 && !row->ends_at_zv_p
16947 /* When first_changed_pos is the end of a continued line,
16948 row is not unchanged because it may be no longer
16949 continued. */
16950 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16951 && (row->continued_p
16952 || row->exact_window_width_line_p))
16953 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16954 needs to be recomputed, so don't consider this row as
16955 unchanged. This happens when the last line was
16956 bidi-reordered and was killed immediately before this
16957 redisplay cycle. In that case, ROW->end stores the
16958 buffer position of the first visual-order character of
16959 the killed text, which is now beyond ZV. */
16960 && CHARPOS (row->end.pos) <= ZV)
16961 row_found = row;
16962
16963 /* Stop if last visible row. */
16964 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16965 break;
16966 }
16967
16968 return row_found;
16969 }
16970
16971
16972 /* Find the first glyph row in the current matrix of W that is not
16973 affected by changes at the end of current_buffer since the
16974 time W's current matrix was built.
16975
16976 Return in *DELTA the number of chars by which buffer positions in
16977 unchanged text at the end of current_buffer must be adjusted.
16978
16979 Return in *DELTA_BYTES the corresponding number of bytes.
16980
16981 Value is null if no such row exists, i.e. all rows are affected by
16982 changes. */
16983
16984 static struct glyph_row *
16985 find_first_unchanged_at_end_row (struct window *w,
16986 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16987 {
16988 struct glyph_row *row;
16989 struct glyph_row *row_found = NULL;
16990
16991 *delta = *delta_bytes = 0;
16992
16993 /* Display must not have been paused, otherwise the current matrix
16994 is not up to date. */
16995 eassert (!NILP (w->window_end_valid));
16996
16997 /* A value of window_end_pos >= END_UNCHANGED means that the window
16998 end is in the range of changed text. If so, there is no
16999 unchanged row at the end of W's current matrix. */
17000 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
17001 return NULL;
17002
17003 /* Set row to the last row in W's current matrix displaying text. */
17004 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17005
17006 /* If matrix is entirely empty, no unchanged row exists. */
17007 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17008 {
17009 /* The value of row is the last glyph row in the matrix having a
17010 meaningful buffer position in it. The end position of row
17011 corresponds to window_end_pos. This allows us to translate
17012 buffer positions in the current matrix to current buffer
17013 positions for characters not in changed text. */
17014 ptrdiff_t Z_old =
17015 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17016 ptrdiff_t Z_BYTE_old =
17017 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17018 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17019 struct glyph_row *first_text_row
17020 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17021
17022 *delta = Z - Z_old;
17023 *delta_bytes = Z_BYTE - Z_BYTE_old;
17024
17025 /* Set last_unchanged_pos to the buffer position of the last
17026 character in the buffer that has not been changed. Z is the
17027 index + 1 of the last character in current_buffer, i.e. by
17028 subtracting END_UNCHANGED we get the index of the last
17029 unchanged character, and we have to add BEG to get its buffer
17030 position. */
17031 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17032 last_unchanged_pos_old = last_unchanged_pos - *delta;
17033
17034 /* Search backward from ROW for a row displaying a line that
17035 starts at a minimum position >= last_unchanged_pos_old. */
17036 for (; row > first_text_row; --row)
17037 {
17038 /* This used to abort, but it can happen.
17039 It is ok to just stop the search instead here. KFS. */
17040 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17041 break;
17042
17043 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17044 row_found = row;
17045 }
17046 }
17047
17048 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17049
17050 return row_found;
17051 }
17052
17053
17054 /* Make sure that glyph rows in the current matrix of window W
17055 reference the same glyph memory as corresponding rows in the
17056 frame's frame matrix. This function is called after scrolling W's
17057 current matrix on a terminal frame in try_window_id and
17058 try_window_reusing_current_matrix. */
17059
17060 static void
17061 sync_frame_with_window_matrix_rows (struct window *w)
17062 {
17063 struct frame *f = XFRAME (w->frame);
17064 struct glyph_row *window_row, *window_row_end, *frame_row;
17065
17066 /* Preconditions: W must be a leaf window and full-width. Its frame
17067 must have a frame matrix. */
17068 eassert (NILP (w->hchild) && NILP (w->vchild));
17069 eassert (WINDOW_FULL_WIDTH_P (w));
17070 eassert (!FRAME_WINDOW_P (f));
17071
17072 /* If W is a full-width window, glyph pointers in W's current matrix
17073 have, by definition, to be the same as glyph pointers in the
17074 corresponding frame matrix. Note that frame matrices have no
17075 marginal areas (see build_frame_matrix). */
17076 window_row = w->current_matrix->rows;
17077 window_row_end = window_row + w->current_matrix->nrows;
17078 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17079 while (window_row < window_row_end)
17080 {
17081 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17082 struct glyph *end = window_row->glyphs[LAST_AREA];
17083
17084 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17085 frame_row->glyphs[TEXT_AREA] = start;
17086 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17087 frame_row->glyphs[LAST_AREA] = end;
17088
17089 /* Disable frame rows whose corresponding window rows have
17090 been disabled in try_window_id. */
17091 if (!window_row->enabled_p)
17092 frame_row->enabled_p = 0;
17093
17094 ++window_row, ++frame_row;
17095 }
17096 }
17097
17098
17099 /* Find the glyph row in window W containing CHARPOS. Consider all
17100 rows between START and END (not inclusive). END null means search
17101 all rows to the end of the display area of W. Value is the row
17102 containing CHARPOS or null. */
17103
17104 struct glyph_row *
17105 row_containing_pos (struct window *w, ptrdiff_t charpos,
17106 struct glyph_row *start, struct glyph_row *end, int dy)
17107 {
17108 struct glyph_row *row = start;
17109 struct glyph_row *best_row = NULL;
17110 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17111 int last_y;
17112
17113 /* If we happen to start on a header-line, skip that. */
17114 if (row->mode_line_p)
17115 ++row;
17116
17117 if ((end && row >= end) || !row->enabled_p)
17118 return NULL;
17119
17120 last_y = window_text_bottom_y (w) - dy;
17121
17122 while (1)
17123 {
17124 /* Give up if we have gone too far. */
17125 if (end && row >= end)
17126 return NULL;
17127 /* This formerly returned if they were equal.
17128 I think that both quantities are of a "last plus one" type;
17129 if so, when they are equal, the row is within the screen. -- rms. */
17130 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17131 return NULL;
17132
17133 /* If it is in this row, return this row. */
17134 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17135 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17136 /* The end position of a row equals the start
17137 position of the next row. If CHARPOS is there, we
17138 would rather display it in the next line, except
17139 when this line ends in ZV. */
17140 && !row->ends_at_zv_p
17141 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17142 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17143 {
17144 struct glyph *g;
17145
17146 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17147 || (!best_row && !row->continued_p))
17148 return row;
17149 /* In bidi-reordered rows, there could be several rows
17150 occluding point, all of them belonging to the same
17151 continued line. We need to find the row which fits
17152 CHARPOS the best. */
17153 for (g = row->glyphs[TEXT_AREA];
17154 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17155 g++)
17156 {
17157 if (!STRINGP (g->object))
17158 {
17159 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17160 {
17161 mindif = eabs (g->charpos - charpos);
17162 best_row = row;
17163 /* Exact match always wins. */
17164 if (mindif == 0)
17165 return best_row;
17166 }
17167 }
17168 }
17169 }
17170 else if (best_row && !row->continued_p)
17171 return best_row;
17172 ++row;
17173 }
17174 }
17175
17176
17177 /* Try to redisplay window W by reusing its existing display. W's
17178 current matrix must be up to date when this function is called,
17179 i.e. window_end_valid must not be nil.
17180
17181 Value is
17182
17183 1 if display has been updated
17184 0 if otherwise unsuccessful
17185 -1 if redisplay with same window start is known not to succeed
17186
17187 The following steps are performed:
17188
17189 1. Find the last row in the current matrix of W that is not
17190 affected by changes at the start of current_buffer. If no such row
17191 is found, give up.
17192
17193 2. Find the first row in W's current matrix that is not affected by
17194 changes at the end of current_buffer. Maybe there is no such row.
17195
17196 3. Display lines beginning with the row + 1 found in step 1 to the
17197 row found in step 2 or, if step 2 didn't find a row, to the end of
17198 the window.
17199
17200 4. If cursor is not known to appear on the window, give up.
17201
17202 5. If display stopped at the row found in step 2, scroll the
17203 display and current matrix as needed.
17204
17205 6. Maybe display some lines at the end of W, if we must. This can
17206 happen under various circumstances, like a partially visible line
17207 becoming fully visible, or because newly displayed lines are displayed
17208 in smaller font sizes.
17209
17210 7. Update W's window end information. */
17211
17212 static int
17213 try_window_id (struct window *w)
17214 {
17215 struct frame *f = XFRAME (w->frame);
17216 struct glyph_matrix *current_matrix = w->current_matrix;
17217 struct glyph_matrix *desired_matrix = w->desired_matrix;
17218 struct glyph_row *last_unchanged_at_beg_row;
17219 struct glyph_row *first_unchanged_at_end_row;
17220 struct glyph_row *row;
17221 struct glyph_row *bottom_row;
17222 int bottom_vpos;
17223 struct it it;
17224 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17225 int dvpos, dy;
17226 struct text_pos start_pos;
17227 struct run run;
17228 int first_unchanged_at_end_vpos = 0;
17229 struct glyph_row *last_text_row, *last_text_row_at_end;
17230 struct text_pos start;
17231 ptrdiff_t first_changed_charpos, last_changed_charpos;
17232
17233 #ifdef GLYPH_DEBUG
17234 if (inhibit_try_window_id)
17235 return 0;
17236 #endif
17237
17238 /* This is handy for debugging. */
17239 #if 0
17240 #define GIVE_UP(X) \
17241 do { \
17242 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17243 return 0; \
17244 } while (0)
17245 #else
17246 #define GIVE_UP(X) return 0
17247 #endif
17248
17249 SET_TEXT_POS_FROM_MARKER (start, w->start);
17250
17251 /* Don't use this for mini-windows because these can show
17252 messages and mini-buffers, and we don't handle that here. */
17253 if (MINI_WINDOW_P (w))
17254 GIVE_UP (1);
17255
17256 /* This flag is used to prevent redisplay optimizations. */
17257 if (windows_or_buffers_changed || cursor_type_changed)
17258 GIVE_UP (2);
17259
17260 /* Verify that narrowing has not changed.
17261 Also verify that we were not told to prevent redisplay optimizations.
17262 It would be nice to further
17263 reduce the number of cases where this prevents try_window_id. */
17264 if (current_buffer->clip_changed
17265 || current_buffer->prevent_redisplay_optimizations_p)
17266 GIVE_UP (3);
17267
17268 /* Window must either use window-based redisplay or be full width. */
17269 if (!FRAME_WINDOW_P (f)
17270 && (!FRAME_LINE_INS_DEL_OK (f)
17271 || !WINDOW_FULL_WIDTH_P (w)))
17272 GIVE_UP (4);
17273
17274 /* Give up if point is known NOT to appear in W. */
17275 if (PT < CHARPOS (start))
17276 GIVE_UP (5);
17277
17278 /* Another way to prevent redisplay optimizations. */
17279 if (w->last_modified == 0)
17280 GIVE_UP (6);
17281
17282 /* Verify that window is not hscrolled. */
17283 if (w->hscroll != 0)
17284 GIVE_UP (7);
17285
17286 /* Verify that display wasn't paused. */
17287 if (NILP (w->window_end_valid))
17288 GIVE_UP (8);
17289
17290 /* Can't use this if highlighting a region because a cursor movement
17291 will do more than just set the cursor. */
17292 if (0 <= markpos_of_region ())
17293 GIVE_UP (9);
17294
17295 /* Likewise if highlighting trailing whitespace. */
17296 if (!NILP (Vshow_trailing_whitespace))
17297 GIVE_UP (11);
17298
17299 /* Likewise if showing a region. */
17300 if (!NILP (w->region_showing))
17301 GIVE_UP (10);
17302
17303 /* Can't use this if overlay arrow position and/or string have
17304 changed. */
17305 if (overlay_arrows_changed_p ())
17306 GIVE_UP (12);
17307
17308 /* When word-wrap is on, adding a space to the first word of a
17309 wrapped line can change the wrap position, altering the line
17310 above it. It might be worthwhile to handle this more
17311 intelligently, but for now just redisplay from scratch. */
17312 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17313 GIVE_UP (21);
17314
17315 /* Under bidi reordering, adding or deleting a character in the
17316 beginning of a paragraph, before the first strong directional
17317 character, can change the base direction of the paragraph (unless
17318 the buffer specifies a fixed paragraph direction), which will
17319 require to redisplay the whole paragraph. It might be worthwhile
17320 to find the paragraph limits and widen the range of redisplayed
17321 lines to that, but for now just give up this optimization and
17322 redisplay from scratch. */
17323 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17324 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17325 GIVE_UP (22);
17326
17327 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17328 only if buffer has really changed. The reason is that the gap is
17329 initially at Z for freshly visited files. The code below would
17330 set end_unchanged to 0 in that case. */
17331 if (MODIFF > SAVE_MODIFF
17332 /* This seems to happen sometimes after saving a buffer. */
17333 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17334 {
17335 if (GPT - BEG < BEG_UNCHANGED)
17336 BEG_UNCHANGED = GPT - BEG;
17337 if (Z - GPT < END_UNCHANGED)
17338 END_UNCHANGED = Z - GPT;
17339 }
17340
17341 /* The position of the first and last character that has been changed. */
17342 first_changed_charpos = BEG + BEG_UNCHANGED;
17343 last_changed_charpos = Z - END_UNCHANGED;
17344
17345 /* If window starts after a line end, and the last change is in
17346 front of that newline, then changes don't affect the display.
17347 This case happens with stealth-fontification. Note that although
17348 the display is unchanged, glyph positions in the matrix have to
17349 be adjusted, of course. */
17350 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17351 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17352 && ((last_changed_charpos < CHARPOS (start)
17353 && CHARPOS (start) == BEGV)
17354 || (last_changed_charpos < CHARPOS (start) - 1
17355 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17356 {
17357 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17358 struct glyph_row *r0;
17359
17360 /* Compute how many chars/bytes have been added to or removed
17361 from the buffer. */
17362 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17363 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17364 Z_delta = Z - Z_old;
17365 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17366
17367 /* Give up if PT is not in the window. Note that it already has
17368 been checked at the start of try_window_id that PT is not in
17369 front of the window start. */
17370 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17371 GIVE_UP (13);
17372
17373 /* If window start is unchanged, we can reuse the whole matrix
17374 as is, after adjusting glyph positions. No need to compute
17375 the window end again, since its offset from Z hasn't changed. */
17376 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17377 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17378 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17379 /* PT must not be in a partially visible line. */
17380 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17381 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17382 {
17383 /* Adjust positions in the glyph matrix. */
17384 if (Z_delta || Z_delta_bytes)
17385 {
17386 struct glyph_row *r1
17387 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17388 increment_matrix_positions (w->current_matrix,
17389 MATRIX_ROW_VPOS (r0, current_matrix),
17390 MATRIX_ROW_VPOS (r1, current_matrix),
17391 Z_delta, Z_delta_bytes);
17392 }
17393
17394 /* Set the cursor. */
17395 row = row_containing_pos (w, PT, r0, NULL, 0);
17396 if (row)
17397 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17398 else
17399 emacs_abort ();
17400 return 1;
17401 }
17402 }
17403
17404 /* Handle the case that changes are all below what is displayed in
17405 the window, and that PT is in the window. This shortcut cannot
17406 be taken if ZV is visible in the window, and text has been added
17407 there that is visible in the window. */
17408 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17409 /* ZV is not visible in the window, or there are no
17410 changes at ZV, actually. */
17411 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17412 || first_changed_charpos == last_changed_charpos))
17413 {
17414 struct glyph_row *r0;
17415
17416 /* Give up if PT is not in the window. Note that it already has
17417 been checked at the start of try_window_id that PT is not in
17418 front of the window start. */
17419 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17420 GIVE_UP (14);
17421
17422 /* If window start is unchanged, we can reuse the whole matrix
17423 as is, without changing glyph positions since no text has
17424 been added/removed in front of the window end. */
17425 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17426 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17427 /* PT must not be in a partially visible line. */
17428 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17429 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17430 {
17431 /* We have to compute the window end anew since text
17432 could have been added/removed after it. */
17433 wset_window_end_pos
17434 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17435 w->window_end_bytepos
17436 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17437
17438 /* Set the cursor. */
17439 row = row_containing_pos (w, PT, r0, NULL, 0);
17440 if (row)
17441 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17442 else
17443 emacs_abort ();
17444 return 2;
17445 }
17446 }
17447
17448 /* Give up if window start is in the changed area.
17449
17450 The condition used to read
17451
17452 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17453
17454 but why that was tested escapes me at the moment. */
17455 if (CHARPOS (start) >= first_changed_charpos
17456 && CHARPOS (start) <= last_changed_charpos)
17457 GIVE_UP (15);
17458
17459 /* Check that window start agrees with the start of the first glyph
17460 row in its current matrix. Check this after we know the window
17461 start is not in changed text, otherwise positions would not be
17462 comparable. */
17463 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17464 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17465 GIVE_UP (16);
17466
17467 /* Give up if the window ends in strings. Overlay strings
17468 at the end are difficult to handle, so don't try. */
17469 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17470 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17471 GIVE_UP (20);
17472
17473 /* Compute the position at which we have to start displaying new
17474 lines. Some of the lines at the top of the window might be
17475 reusable because they are not displaying changed text. Find the
17476 last row in W's current matrix not affected by changes at the
17477 start of current_buffer. Value is null if changes start in the
17478 first line of window. */
17479 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17480 if (last_unchanged_at_beg_row)
17481 {
17482 /* Avoid starting to display in the middle of a character, a TAB
17483 for instance. This is easier than to set up the iterator
17484 exactly, and it's not a frequent case, so the additional
17485 effort wouldn't really pay off. */
17486 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17487 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17488 && last_unchanged_at_beg_row > w->current_matrix->rows)
17489 --last_unchanged_at_beg_row;
17490
17491 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17492 GIVE_UP (17);
17493
17494 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17495 GIVE_UP (18);
17496 start_pos = it.current.pos;
17497
17498 /* Start displaying new lines in the desired matrix at the same
17499 vpos we would use in the current matrix, i.e. below
17500 last_unchanged_at_beg_row. */
17501 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17502 current_matrix);
17503 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17504 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17505
17506 eassert (it.hpos == 0 && it.current_x == 0);
17507 }
17508 else
17509 {
17510 /* There are no reusable lines at the start of the window.
17511 Start displaying in the first text line. */
17512 start_display (&it, w, start);
17513 it.vpos = it.first_vpos;
17514 start_pos = it.current.pos;
17515 }
17516
17517 /* Find the first row that is not affected by changes at the end of
17518 the buffer. Value will be null if there is no unchanged row, in
17519 which case we must redisplay to the end of the window. delta
17520 will be set to the value by which buffer positions beginning with
17521 first_unchanged_at_end_row have to be adjusted due to text
17522 changes. */
17523 first_unchanged_at_end_row
17524 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17525 IF_DEBUG (debug_delta = delta);
17526 IF_DEBUG (debug_delta_bytes = delta_bytes);
17527
17528 /* Set stop_pos to the buffer position up to which we will have to
17529 display new lines. If first_unchanged_at_end_row != NULL, this
17530 is the buffer position of the start of the line displayed in that
17531 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17532 that we don't stop at a buffer position. */
17533 stop_pos = 0;
17534 if (first_unchanged_at_end_row)
17535 {
17536 eassert (last_unchanged_at_beg_row == NULL
17537 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17538
17539 /* If this is a continuation line, move forward to the next one
17540 that isn't. Changes in lines above affect this line.
17541 Caution: this may move first_unchanged_at_end_row to a row
17542 not displaying text. */
17543 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17544 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17545 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17546 < it.last_visible_y))
17547 ++first_unchanged_at_end_row;
17548
17549 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17550 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17551 >= it.last_visible_y))
17552 first_unchanged_at_end_row = NULL;
17553 else
17554 {
17555 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17556 + delta);
17557 first_unchanged_at_end_vpos
17558 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17559 eassert (stop_pos >= Z - END_UNCHANGED);
17560 }
17561 }
17562 else if (last_unchanged_at_beg_row == NULL)
17563 GIVE_UP (19);
17564
17565
17566 #ifdef GLYPH_DEBUG
17567
17568 /* Either there is no unchanged row at the end, or the one we have
17569 now displays text. This is a necessary condition for the window
17570 end pos calculation at the end of this function. */
17571 eassert (first_unchanged_at_end_row == NULL
17572 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17573
17574 debug_last_unchanged_at_beg_vpos
17575 = (last_unchanged_at_beg_row
17576 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17577 : -1);
17578 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17579
17580 #endif /* GLYPH_DEBUG */
17581
17582
17583 /* Display new lines. Set last_text_row to the last new line
17584 displayed which has text on it, i.e. might end up as being the
17585 line where the window_end_vpos is. */
17586 w->cursor.vpos = -1;
17587 last_text_row = NULL;
17588 overlay_arrow_seen = 0;
17589 while (it.current_y < it.last_visible_y
17590 && !fonts_changed_p
17591 && (first_unchanged_at_end_row == NULL
17592 || IT_CHARPOS (it) < stop_pos))
17593 {
17594 if (display_line (&it))
17595 last_text_row = it.glyph_row - 1;
17596 }
17597
17598 if (fonts_changed_p)
17599 return -1;
17600
17601
17602 /* Compute differences in buffer positions, y-positions etc. for
17603 lines reused at the bottom of the window. Compute what we can
17604 scroll. */
17605 if (first_unchanged_at_end_row
17606 /* No lines reused because we displayed everything up to the
17607 bottom of the window. */
17608 && it.current_y < it.last_visible_y)
17609 {
17610 dvpos = (it.vpos
17611 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17612 current_matrix));
17613 dy = it.current_y - first_unchanged_at_end_row->y;
17614 run.current_y = first_unchanged_at_end_row->y;
17615 run.desired_y = run.current_y + dy;
17616 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17617 }
17618 else
17619 {
17620 delta = delta_bytes = dvpos = dy
17621 = run.current_y = run.desired_y = run.height = 0;
17622 first_unchanged_at_end_row = NULL;
17623 }
17624 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17625
17626
17627 /* Find the cursor if not already found. We have to decide whether
17628 PT will appear on this window (it sometimes doesn't, but this is
17629 not a very frequent case.) This decision has to be made before
17630 the current matrix is altered. A value of cursor.vpos < 0 means
17631 that PT is either in one of the lines beginning at
17632 first_unchanged_at_end_row or below the window. Don't care for
17633 lines that might be displayed later at the window end; as
17634 mentioned, this is not a frequent case. */
17635 if (w->cursor.vpos < 0)
17636 {
17637 /* Cursor in unchanged rows at the top? */
17638 if (PT < CHARPOS (start_pos)
17639 && last_unchanged_at_beg_row)
17640 {
17641 row = row_containing_pos (w, PT,
17642 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17643 last_unchanged_at_beg_row + 1, 0);
17644 if (row)
17645 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17646 }
17647
17648 /* Start from first_unchanged_at_end_row looking for PT. */
17649 else if (first_unchanged_at_end_row)
17650 {
17651 row = row_containing_pos (w, PT - delta,
17652 first_unchanged_at_end_row, NULL, 0);
17653 if (row)
17654 set_cursor_from_row (w, row, w->current_matrix, delta,
17655 delta_bytes, dy, dvpos);
17656 }
17657
17658 /* Give up if cursor was not found. */
17659 if (w->cursor.vpos < 0)
17660 {
17661 clear_glyph_matrix (w->desired_matrix);
17662 return -1;
17663 }
17664 }
17665
17666 /* Don't let the cursor end in the scroll margins. */
17667 {
17668 int this_scroll_margin, cursor_height;
17669
17670 this_scroll_margin =
17671 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17672 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17673 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17674
17675 if ((w->cursor.y < this_scroll_margin
17676 && CHARPOS (start) > BEGV)
17677 /* Old redisplay didn't take scroll margin into account at the bottom,
17678 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17679 || (w->cursor.y + (make_cursor_line_fully_visible_p
17680 ? cursor_height + this_scroll_margin
17681 : 1)) > it.last_visible_y)
17682 {
17683 w->cursor.vpos = -1;
17684 clear_glyph_matrix (w->desired_matrix);
17685 return -1;
17686 }
17687 }
17688
17689 /* Scroll the display. Do it before changing the current matrix so
17690 that xterm.c doesn't get confused about where the cursor glyph is
17691 found. */
17692 if (dy && run.height)
17693 {
17694 update_begin (f);
17695
17696 if (FRAME_WINDOW_P (f))
17697 {
17698 FRAME_RIF (f)->update_window_begin_hook (w);
17699 FRAME_RIF (f)->clear_window_mouse_face (w);
17700 FRAME_RIF (f)->scroll_run_hook (w, &run);
17701 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17702 }
17703 else
17704 {
17705 /* Terminal frame. In this case, dvpos gives the number of
17706 lines to scroll by; dvpos < 0 means scroll up. */
17707 int from_vpos
17708 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17709 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17710 int end = (WINDOW_TOP_EDGE_LINE (w)
17711 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17712 + window_internal_height (w));
17713
17714 #if defined (HAVE_GPM) || defined (MSDOS)
17715 x_clear_window_mouse_face (w);
17716 #endif
17717 /* Perform the operation on the screen. */
17718 if (dvpos > 0)
17719 {
17720 /* Scroll last_unchanged_at_beg_row to the end of the
17721 window down dvpos lines. */
17722 set_terminal_window (f, end);
17723
17724 /* On dumb terminals delete dvpos lines at the end
17725 before inserting dvpos empty lines. */
17726 if (!FRAME_SCROLL_REGION_OK (f))
17727 ins_del_lines (f, end - dvpos, -dvpos);
17728
17729 /* Insert dvpos empty lines in front of
17730 last_unchanged_at_beg_row. */
17731 ins_del_lines (f, from, dvpos);
17732 }
17733 else if (dvpos < 0)
17734 {
17735 /* Scroll up last_unchanged_at_beg_vpos to the end of
17736 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17737 set_terminal_window (f, end);
17738
17739 /* Delete dvpos lines in front of
17740 last_unchanged_at_beg_vpos. ins_del_lines will set
17741 the cursor to the given vpos and emit |dvpos| delete
17742 line sequences. */
17743 ins_del_lines (f, from + dvpos, dvpos);
17744
17745 /* On a dumb terminal insert dvpos empty lines at the
17746 end. */
17747 if (!FRAME_SCROLL_REGION_OK (f))
17748 ins_del_lines (f, end + dvpos, -dvpos);
17749 }
17750
17751 set_terminal_window (f, 0);
17752 }
17753
17754 update_end (f);
17755 }
17756
17757 /* Shift reused rows of the current matrix to the right position.
17758 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17759 text. */
17760 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17761 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17762 if (dvpos < 0)
17763 {
17764 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17765 bottom_vpos, dvpos);
17766 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17767 bottom_vpos);
17768 }
17769 else if (dvpos > 0)
17770 {
17771 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17772 bottom_vpos, dvpos);
17773 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17774 first_unchanged_at_end_vpos + dvpos);
17775 }
17776
17777 /* For frame-based redisplay, make sure that current frame and window
17778 matrix are in sync with respect to glyph memory. */
17779 if (!FRAME_WINDOW_P (f))
17780 sync_frame_with_window_matrix_rows (w);
17781
17782 /* Adjust buffer positions in reused rows. */
17783 if (delta || delta_bytes)
17784 increment_matrix_positions (current_matrix,
17785 first_unchanged_at_end_vpos + dvpos,
17786 bottom_vpos, delta, delta_bytes);
17787
17788 /* Adjust Y positions. */
17789 if (dy)
17790 shift_glyph_matrix (w, current_matrix,
17791 first_unchanged_at_end_vpos + dvpos,
17792 bottom_vpos, dy);
17793
17794 if (first_unchanged_at_end_row)
17795 {
17796 first_unchanged_at_end_row += dvpos;
17797 if (first_unchanged_at_end_row->y >= it.last_visible_y
17798 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17799 first_unchanged_at_end_row = NULL;
17800 }
17801
17802 /* If scrolling up, there may be some lines to display at the end of
17803 the window. */
17804 last_text_row_at_end = NULL;
17805 if (dy < 0)
17806 {
17807 /* Scrolling up can leave for example a partially visible line
17808 at the end of the window to be redisplayed. */
17809 /* Set last_row to the glyph row in the current matrix where the
17810 window end line is found. It has been moved up or down in
17811 the matrix by dvpos. */
17812 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17813 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17814
17815 /* If last_row is the window end line, it should display text. */
17816 eassert (last_row->displays_text_p);
17817
17818 /* If window end line was partially visible before, begin
17819 displaying at that line. Otherwise begin displaying with the
17820 line following it. */
17821 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17822 {
17823 init_to_row_start (&it, w, last_row);
17824 it.vpos = last_vpos;
17825 it.current_y = last_row->y;
17826 }
17827 else
17828 {
17829 init_to_row_end (&it, w, last_row);
17830 it.vpos = 1 + last_vpos;
17831 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17832 ++last_row;
17833 }
17834
17835 /* We may start in a continuation line. If so, we have to
17836 get the right continuation_lines_width and current_x. */
17837 it.continuation_lines_width = last_row->continuation_lines_width;
17838 it.hpos = it.current_x = 0;
17839
17840 /* Display the rest of the lines at the window end. */
17841 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17842 while (it.current_y < it.last_visible_y
17843 && !fonts_changed_p)
17844 {
17845 /* Is it always sure that the display agrees with lines in
17846 the current matrix? I don't think so, so we mark rows
17847 displayed invalid in the current matrix by setting their
17848 enabled_p flag to zero. */
17849 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17850 if (display_line (&it))
17851 last_text_row_at_end = it.glyph_row - 1;
17852 }
17853 }
17854
17855 /* Update window_end_pos and window_end_vpos. */
17856 if (first_unchanged_at_end_row
17857 && !last_text_row_at_end)
17858 {
17859 /* Window end line if one of the preserved rows from the current
17860 matrix. Set row to the last row displaying text in current
17861 matrix starting at first_unchanged_at_end_row, after
17862 scrolling. */
17863 eassert (first_unchanged_at_end_row->displays_text_p);
17864 row = find_last_row_displaying_text (w->current_matrix, &it,
17865 first_unchanged_at_end_row);
17866 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17867
17868 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17869 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17870 wset_window_end_vpos
17871 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17872 eassert (w->window_end_bytepos >= 0);
17873 IF_DEBUG (debug_method_add (w, "A"));
17874 }
17875 else if (last_text_row_at_end)
17876 {
17877 wset_window_end_pos
17878 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17879 w->window_end_bytepos
17880 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17881 wset_window_end_vpos
17882 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17883 desired_matrix)));
17884 eassert (w->window_end_bytepos >= 0);
17885 IF_DEBUG (debug_method_add (w, "B"));
17886 }
17887 else if (last_text_row)
17888 {
17889 /* We have displayed either to the end of the window or at the
17890 end of the window, i.e. the last row with text is to be found
17891 in the desired matrix. */
17892 wset_window_end_pos
17893 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17894 w->window_end_bytepos
17895 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17896 wset_window_end_vpos
17897 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17898 eassert (w->window_end_bytepos >= 0);
17899 }
17900 else if (first_unchanged_at_end_row == NULL
17901 && last_text_row == NULL
17902 && last_text_row_at_end == NULL)
17903 {
17904 /* Displayed to end of window, but no line containing text was
17905 displayed. Lines were deleted at the end of the window. */
17906 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17907 int vpos = XFASTINT (w->window_end_vpos);
17908 struct glyph_row *current_row = current_matrix->rows + vpos;
17909 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17910
17911 for (row = NULL;
17912 row == NULL && vpos >= first_vpos;
17913 --vpos, --current_row, --desired_row)
17914 {
17915 if (desired_row->enabled_p)
17916 {
17917 if (desired_row->displays_text_p)
17918 row = desired_row;
17919 }
17920 else if (current_row->displays_text_p)
17921 row = current_row;
17922 }
17923
17924 eassert (row != NULL);
17925 wset_window_end_vpos (w, make_number (vpos + 1));
17926 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17927 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17928 eassert (w->window_end_bytepos >= 0);
17929 IF_DEBUG (debug_method_add (w, "C"));
17930 }
17931 else
17932 emacs_abort ();
17933
17934 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17935 debug_end_vpos = XFASTINT (w->window_end_vpos));
17936
17937 /* Record that display has not been completed. */
17938 wset_window_end_valid (w, Qnil);
17939 w->desired_matrix->no_scrolling_p = 1;
17940 return 3;
17941
17942 #undef GIVE_UP
17943 }
17944
17945
17946 \f
17947 /***********************************************************************
17948 More debugging support
17949 ***********************************************************************/
17950
17951 #ifdef GLYPH_DEBUG
17952
17953 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17954 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17955 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17956
17957
17958 /* Dump the contents of glyph matrix MATRIX on stderr.
17959
17960 GLYPHS 0 means don't show glyph contents.
17961 GLYPHS 1 means show glyphs in short form
17962 GLYPHS > 1 means show glyphs in long form. */
17963
17964 void
17965 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17966 {
17967 int i;
17968 for (i = 0; i < matrix->nrows; ++i)
17969 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17970 }
17971
17972
17973 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17974 the glyph row and area where the glyph comes from. */
17975
17976 void
17977 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17978 {
17979 if (glyph->type == CHAR_GLYPH)
17980 {
17981 fprintf (stderr,
17982 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17983 glyph - row->glyphs[TEXT_AREA],
17984 'C',
17985 glyph->charpos,
17986 (BUFFERP (glyph->object)
17987 ? 'B'
17988 : (STRINGP (glyph->object)
17989 ? 'S'
17990 : '-')),
17991 glyph->pixel_width,
17992 glyph->u.ch,
17993 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17994 ? glyph->u.ch
17995 : '.'),
17996 glyph->face_id,
17997 glyph->left_box_line_p,
17998 glyph->right_box_line_p);
17999 }
18000 else if (glyph->type == STRETCH_GLYPH)
18001 {
18002 fprintf (stderr,
18003 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18004 glyph - row->glyphs[TEXT_AREA],
18005 'S',
18006 glyph->charpos,
18007 (BUFFERP (glyph->object)
18008 ? 'B'
18009 : (STRINGP (glyph->object)
18010 ? 'S'
18011 : '-')),
18012 glyph->pixel_width,
18013 0,
18014 '.',
18015 glyph->face_id,
18016 glyph->left_box_line_p,
18017 glyph->right_box_line_p);
18018 }
18019 else if (glyph->type == IMAGE_GLYPH)
18020 {
18021 fprintf (stderr,
18022 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18023 glyph - row->glyphs[TEXT_AREA],
18024 'I',
18025 glyph->charpos,
18026 (BUFFERP (glyph->object)
18027 ? 'B'
18028 : (STRINGP (glyph->object)
18029 ? 'S'
18030 : '-')),
18031 glyph->pixel_width,
18032 glyph->u.img_id,
18033 '.',
18034 glyph->face_id,
18035 glyph->left_box_line_p,
18036 glyph->right_box_line_p);
18037 }
18038 else if (glyph->type == COMPOSITE_GLYPH)
18039 {
18040 fprintf (stderr,
18041 " %5td %4c %6"pI"d %c %3d 0x%05x",
18042 glyph - row->glyphs[TEXT_AREA],
18043 '+',
18044 glyph->charpos,
18045 (BUFFERP (glyph->object)
18046 ? 'B'
18047 : (STRINGP (glyph->object)
18048 ? 'S'
18049 : '-')),
18050 glyph->pixel_width,
18051 glyph->u.cmp.id);
18052 if (glyph->u.cmp.automatic)
18053 fprintf (stderr,
18054 "[%d-%d]",
18055 glyph->slice.cmp.from, glyph->slice.cmp.to);
18056 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18057 glyph->face_id,
18058 glyph->left_box_line_p,
18059 glyph->right_box_line_p);
18060 }
18061 }
18062
18063
18064 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18065 GLYPHS 0 means don't show glyph contents.
18066 GLYPHS 1 means show glyphs in short form
18067 GLYPHS > 1 means show glyphs in long form. */
18068
18069 void
18070 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18071 {
18072 if (glyphs != 1)
18073 {
18074 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18075 fprintf (stderr, "======================================================================\n");
18076
18077 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18078 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18079 vpos,
18080 MATRIX_ROW_START_CHARPOS (row),
18081 MATRIX_ROW_END_CHARPOS (row),
18082 row->used[TEXT_AREA],
18083 row->contains_overlapping_glyphs_p,
18084 row->enabled_p,
18085 row->truncated_on_left_p,
18086 row->truncated_on_right_p,
18087 row->continued_p,
18088 MATRIX_ROW_CONTINUATION_LINE_P (row),
18089 row->displays_text_p,
18090 row->ends_at_zv_p,
18091 row->fill_line_p,
18092 row->ends_in_middle_of_char_p,
18093 row->starts_in_middle_of_char_p,
18094 row->mouse_face_p,
18095 row->x,
18096 row->y,
18097 row->pixel_width,
18098 row->height,
18099 row->visible_height,
18100 row->ascent,
18101 row->phys_ascent);
18102 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18103 row->end.overlay_string_index,
18104 row->continuation_lines_width);
18105 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18106 CHARPOS (row->start.string_pos),
18107 CHARPOS (row->end.string_pos));
18108 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18109 row->end.dpvec_index);
18110 }
18111
18112 if (glyphs > 1)
18113 {
18114 int area;
18115
18116 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18117 {
18118 struct glyph *glyph = row->glyphs[area];
18119 struct glyph *glyph_end = glyph + row->used[area];
18120
18121 /* Glyph for a line end in text. */
18122 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18123 ++glyph_end;
18124
18125 if (glyph < glyph_end)
18126 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18127
18128 for (; glyph < glyph_end; ++glyph)
18129 dump_glyph (row, glyph, area);
18130 }
18131 }
18132 else if (glyphs == 1)
18133 {
18134 int area;
18135
18136 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18137 {
18138 char *s = alloca (row->used[area] + 1);
18139 int i;
18140
18141 for (i = 0; i < row->used[area]; ++i)
18142 {
18143 struct glyph *glyph = row->glyphs[area] + i;
18144 if (glyph->type == CHAR_GLYPH
18145 && glyph->u.ch < 0x80
18146 && glyph->u.ch >= ' ')
18147 s[i] = glyph->u.ch;
18148 else
18149 s[i] = '.';
18150 }
18151
18152 s[i] = '\0';
18153 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18154 }
18155 }
18156 }
18157
18158
18159 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18160 Sdump_glyph_matrix, 0, 1, "p",
18161 doc: /* Dump the current matrix of the selected window to stderr.
18162 Shows contents of glyph row structures. With non-nil
18163 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18164 glyphs in short form, otherwise show glyphs in long form. */)
18165 (Lisp_Object glyphs)
18166 {
18167 struct window *w = XWINDOW (selected_window);
18168 struct buffer *buffer = XBUFFER (w->buffer);
18169
18170 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18171 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18172 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18173 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18174 fprintf (stderr, "=============================================\n");
18175 dump_glyph_matrix (w->current_matrix,
18176 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18177 return Qnil;
18178 }
18179
18180
18181 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18182 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18183 (void)
18184 {
18185 struct frame *f = XFRAME (selected_frame);
18186 dump_glyph_matrix (f->current_matrix, 1);
18187 return Qnil;
18188 }
18189
18190
18191 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18192 doc: /* Dump glyph row ROW to stderr.
18193 GLYPH 0 means don't dump glyphs.
18194 GLYPH 1 means dump glyphs in short form.
18195 GLYPH > 1 or omitted means dump glyphs in long form. */)
18196 (Lisp_Object row, Lisp_Object glyphs)
18197 {
18198 struct glyph_matrix *matrix;
18199 EMACS_INT vpos;
18200
18201 CHECK_NUMBER (row);
18202 matrix = XWINDOW (selected_window)->current_matrix;
18203 vpos = XINT (row);
18204 if (vpos >= 0 && vpos < matrix->nrows)
18205 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18206 vpos,
18207 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18208 return Qnil;
18209 }
18210
18211
18212 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18213 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18214 GLYPH 0 means don't dump glyphs.
18215 GLYPH 1 means dump glyphs in short form.
18216 GLYPH > 1 or omitted means dump glyphs in long form. */)
18217 (Lisp_Object row, Lisp_Object glyphs)
18218 {
18219 struct frame *sf = SELECTED_FRAME ();
18220 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18221 EMACS_INT vpos;
18222
18223 CHECK_NUMBER (row);
18224 vpos = XINT (row);
18225 if (vpos >= 0 && vpos < m->nrows)
18226 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18227 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18228 return Qnil;
18229 }
18230
18231
18232 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18233 doc: /* Toggle tracing of redisplay.
18234 With ARG, turn tracing on if and only if ARG is positive. */)
18235 (Lisp_Object arg)
18236 {
18237 if (NILP (arg))
18238 trace_redisplay_p = !trace_redisplay_p;
18239 else
18240 {
18241 arg = Fprefix_numeric_value (arg);
18242 trace_redisplay_p = XINT (arg) > 0;
18243 }
18244
18245 return Qnil;
18246 }
18247
18248
18249 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18250 doc: /* Like `format', but print result to stderr.
18251 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18252 (ptrdiff_t nargs, Lisp_Object *args)
18253 {
18254 Lisp_Object s = Fformat (nargs, args);
18255 fprintf (stderr, "%s", SDATA (s));
18256 return Qnil;
18257 }
18258
18259 #endif /* GLYPH_DEBUG */
18260
18261
18262 \f
18263 /***********************************************************************
18264 Building Desired Matrix Rows
18265 ***********************************************************************/
18266
18267 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18268 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18269
18270 static struct glyph_row *
18271 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18272 {
18273 struct frame *f = XFRAME (WINDOW_FRAME (w));
18274 struct buffer *buffer = XBUFFER (w->buffer);
18275 struct buffer *old = current_buffer;
18276 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18277 int arrow_len = SCHARS (overlay_arrow_string);
18278 const unsigned char *arrow_end = arrow_string + arrow_len;
18279 const unsigned char *p;
18280 struct it it;
18281 int multibyte_p;
18282 int n_glyphs_before;
18283
18284 set_buffer_temp (buffer);
18285 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18286 it.glyph_row->used[TEXT_AREA] = 0;
18287 SET_TEXT_POS (it.position, 0, 0);
18288
18289 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18290 p = arrow_string;
18291 while (p < arrow_end)
18292 {
18293 Lisp_Object face, ilisp;
18294
18295 /* Get the next character. */
18296 if (multibyte_p)
18297 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18298 else
18299 {
18300 it.c = it.char_to_display = *p, it.len = 1;
18301 if (! ASCII_CHAR_P (it.c))
18302 it.char_to_display = BYTE8_TO_CHAR (it.c);
18303 }
18304 p += it.len;
18305
18306 /* Get its face. */
18307 ilisp = make_number (p - arrow_string);
18308 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18309 it.face_id = compute_char_face (f, it.char_to_display, face);
18310
18311 /* Compute its width, get its glyphs. */
18312 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18313 SET_TEXT_POS (it.position, -1, -1);
18314 PRODUCE_GLYPHS (&it);
18315
18316 /* If this character doesn't fit any more in the line, we have
18317 to remove some glyphs. */
18318 if (it.current_x > it.last_visible_x)
18319 {
18320 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18321 break;
18322 }
18323 }
18324
18325 set_buffer_temp (old);
18326 return it.glyph_row;
18327 }
18328
18329
18330 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18331 glyphs to insert is determined by produce_special_glyphs. */
18332
18333 static void
18334 insert_left_trunc_glyphs (struct it *it)
18335 {
18336 struct it truncate_it;
18337 struct glyph *from, *end, *to, *toend;
18338
18339 eassert (!FRAME_WINDOW_P (it->f)
18340 || (!it->glyph_row->reversed_p
18341 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18342 || (it->glyph_row->reversed_p
18343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18344
18345 /* Get the truncation glyphs. */
18346 truncate_it = *it;
18347 truncate_it.current_x = 0;
18348 truncate_it.face_id = DEFAULT_FACE_ID;
18349 truncate_it.glyph_row = &scratch_glyph_row;
18350 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18351 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18352 truncate_it.object = make_number (0);
18353 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18354
18355 /* Overwrite glyphs from IT with truncation glyphs. */
18356 if (!it->glyph_row->reversed_p)
18357 {
18358 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18359
18360 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18361 end = from + tused;
18362 to = it->glyph_row->glyphs[TEXT_AREA];
18363 toend = to + it->glyph_row->used[TEXT_AREA];
18364 if (FRAME_WINDOW_P (it->f))
18365 {
18366 /* On GUI frames, when variable-size fonts are displayed,
18367 the truncation glyphs may need more pixels than the row's
18368 glyphs they overwrite. We overwrite more glyphs to free
18369 enough screen real estate, and enlarge the stretch glyph
18370 on the right (see display_line), if there is one, to
18371 preserve the screen position of the truncation glyphs on
18372 the right. */
18373 int w = 0;
18374 struct glyph *g = to;
18375 short used;
18376
18377 /* The first glyph could be partially visible, in which case
18378 it->glyph_row->x will be negative. But we want the left
18379 truncation glyphs to be aligned at the left margin of the
18380 window, so we override the x coordinate at which the row
18381 will begin. */
18382 it->glyph_row->x = 0;
18383 while (g < toend && w < it->truncation_pixel_width)
18384 {
18385 w += g->pixel_width;
18386 ++g;
18387 }
18388 if (g - to - tused > 0)
18389 {
18390 memmove (to + tused, g, (toend - g) * sizeof(*g));
18391 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18392 }
18393 used = it->glyph_row->used[TEXT_AREA];
18394 if (it->glyph_row->truncated_on_right_p
18395 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18396 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18397 == STRETCH_GLYPH)
18398 {
18399 int extra = w - it->truncation_pixel_width;
18400
18401 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18402 }
18403 }
18404
18405 while (from < end)
18406 *to++ = *from++;
18407
18408 /* There may be padding glyphs left over. Overwrite them too. */
18409 if (!FRAME_WINDOW_P (it->f))
18410 {
18411 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18412 {
18413 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18414 while (from < end)
18415 *to++ = *from++;
18416 }
18417 }
18418
18419 if (to > toend)
18420 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18421 }
18422 else
18423 {
18424 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18425
18426 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18427 that back to front. */
18428 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18429 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18430 toend = it->glyph_row->glyphs[TEXT_AREA];
18431 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18432 if (FRAME_WINDOW_P (it->f))
18433 {
18434 int w = 0;
18435 struct glyph *g = to;
18436
18437 while (g >= toend && w < it->truncation_pixel_width)
18438 {
18439 w += g->pixel_width;
18440 --g;
18441 }
18442 if (to - g - tused > 0)
18443 to = g + tused;
18444 if (it->glyph_row->truncated_on_right_p
18445 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18446 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18447 {
18448 int extra = w - it->truncation_pixel_width;
18449
18450 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18451 }
18452 }
18453
18454 while (from >= end && to >= toend)
18455 *to-- = *from--;
18456 if (!FRAME_WINDOW_P (it->f))
18457 {
18458 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18459 {
18460 from =
18461 truncate_it.glyph_row->glyphs[TEXT_AREA]
18462 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18463 while (from >= end && to >= toend)
18464 *to-- = *from--;
18465 }
18466 }
18467 if (from >= end)
18468 {
18469 /* Need to free some room before prepending additional
18470 glyphs. */
18471 int move_by = from - end + 1;
18472 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18473 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18474
18475 for ( ; g >= g0; g--)
18476 g[move_by] = *g;
18477 while (from >= end)
18478 *to-- = *from--;
18479 it->glyph_row->used[TEXT_AREA] += move_by;
18480 }
18481 }
18482 }
18483
18484 /* Compute the hash code for ROW. */
18485 unsigned
18486 row_hash (struct glyph_row *row)
18487 {
18488 int area, k;
18489 unsigned hashval = 0;
18490
18491 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18492 for (k = 0; k < row->used[area]; ++k)
18493 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18494 + row->glyphs[area][k].u.val
18495 + row->glyphs[area][k].face_id
18496 + row->glyphs[area][k].padding_p
18497 + (row->glyphs[area][k].type << 2));
18498
18499 return hashval;
18500 }
18501
18502 /* Compute the pixel height and width of IT->glyph_row.
18503
18504 Most of the time, ascent and height of a display line will be equal
18505 to the max_ascent and max_height values of the display iterator
18506 structure. This is not the case if
18507
18508 1. We hit ZV without displaying anything. In this case, max_ascent
18509 and max_height will be zero.
18510
18511 2. We have some glyphs that don't contribute to the line height.
18512 (The glyph row flag contributes_to_line_height_p is for future
18513 pixmap extensions).
18514
18515 The first case is easily covered by using default values because in
18516 these cases, the line height does not really matter, except that it
18517 must not be zero. */
18518
18519 static void
18520 compute_line_metrics (struct it *it)
18521 {
18522 struct glyph_row *row = it->glyph_row;
18523
18524 if (FRAME_WINDOW_P (it->f))
18525 {
18526 int i, min_y, max_y;
18527
18528 /* The line may consist of one space only, that was added to
18529 place the cursor on it. If so, the row's height hasn't been
18530 computed yet. */
18531 if (row->height == 0)
18532 {
18533 if (it->max_ascent + it->max_descent == 0)
18534 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18535 row->ascent = it->max_ascent;
18536 row->height = it->max_ascent + it->max_descent;
18537 row->phys_ascent = it->max_phys_ascent;
18538 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18539 row->extra_line_spacing = it->max_extra_line_spacing;
18540 }
18541
18542 /* Compute the width of this line. */
18543 row->pixel_width = row->x;
18544 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18545 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18546
18547 eassert (row->pixel_width >= 0);
18548 eassert (row->ascent >= 0 && row->height > 0);
18549
18550 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18551 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18552
18553 /* If first line's physical ascent is larger than its logical
18554 ascent, use the physical ascent, and make the row taller.
18555 This makes accented characters fully visible. */
18556 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18557 && row->phys_ascent > row->ascent)
18558 {
18559 row->height += row->phys_ascent - row->ascent;
18560 row->ascent = row->phys_ascent;
18561 }
18562
18563 /* Compute how much of the line is visible. */
18564 row->visible_height = row->height;
18565
18566 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18567 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18568
18569 if (row->y < min_y)
18570 row->visible_height -= min_y - row->y;
18571 if (row->y + row->height > max_y)
18572 row->visible_height -= row->y + row->height - max_y;
18573 }
18574 else
18575 {
18576 row->pixel_width = row->used[TEXT_AREA];
18577 if (row->continued_p)
18578 row->pixel_width -= it->continuation_pixel_width;
18579 else if (row->truncated_on_right_p)
18580 row->pixel_width -= it->truncation_pixel_width;
18581 row->ascent = row->phys_ascent = 0;
18582 row->height = row->phys_height = row->visible_height = 1;
18583 row->extra_line_spacing = 0;
18584 }
18585
18586 /* Compute a hash code for this row. */
18587 row->hash = row_hash (row);
18588
18589 it->max_ascent = it->max_descent = 0;
18590 it->max_phys_ascent = it->max_phys_descent = 0;
18591 }
18592
18593
18594 /* Append one space to the glyph row of iterator IT if doing a
18595 window-based redisplay. The space has the same face as
18596 IT->face_id. Value is non-zero if a space was added.
18597
18598 This function is called to make sure that there is always one glyph
18599 at the end of a glyph row that the cursor can be set on under
18600 window-systems. (If there weren't such a glyph we would not know
18601 how wide and tall a box cursor should be displayed).
18602
18603 At the same time this space let's a nicely handle clearing to the
18604 end of the line if the row ends in italic text. */
18605
18606 static int
18607 append_space_for_newline (struct it *it, int default_face_p)
18608 {
18609 if (FRAME_WINDOW_P (it->f))
18610 {
18611 int n = it->glyph_row->used[TEXT_AREA];
18612
18613 if (it->glyph_row->glyphs[TEXT_AREA] + n
18614 < it->glyph_row->glyphs[1 + TEXT_AREA])
18615 {
18616 /* Save some values that must not be changed.
18617 Must save IT->c and IT->len because otherwise
18618 ITERATOR_AT_END_P wouldn't work anymore after
18619 append_space_for_newline has been called. */
18620 enum display_element_type saved_what = it->what;
18621 int saved_c = it->c, saved_len = it->len;
18622 int saved_char_to_display = it->char_to_display;
18623 int saved_x = it->current_x;
18624 int saved_face_id = it->face_id;
18625 int saved_box_end = it->end_of_box_run_p;
18626 struct text_pos saved_pos;
18627 Lisp_Object saved_object;
18628 struct face *face;
18629
18630 saved_object = it->object;
18631 saved_pos = it->position;
18632
18633 it->what = IT_CHARACTER;
18634 memset (&it->position, 0, sizeof it->position);
18635 it->object = make_number (0);
18636 it->c = it->char_to_display = ' ';
18637 it->len = 1;
18638
18639 /* If the default face was remapped, be sure to use the
18640 remapped face for the appended newline. */
18641 if (default_face_p)
18642 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18643 else if (it->face_before_selective_p)
18644 it->face_id = it->saved_face_id;
18645 face = FACE_FROM_ID (it->f, it->face_id);
18646 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18647 /* In R2L rows, we will prepend a stretch glyph that will
18648 have the end_of_box_run_p flag set for it, so there's no
18649 need for the appended newline glyph to have that flag
18650 set. */
18651 if (it->glyph_row->reversed_p
18652 /* But if the appended newline glyph goes all the way to
18653 the end of the row, there will be no stretch glyph,
18654 so leave the box flag set. */
18655 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18656 it->end_of_box_run_p = 0;
18657
18658 PRODUCE_GLYPHS (it);
18659
18660 it->override_ascent = -1;
18661 it->constrain_row_ascent_descent_p = 0;
18662 it->current_x = saved_x;
18663 it->object = saved_object;
18664 it->position = saved_pos;
18665 it->what = saved_what;
18666 it->face_id = saved_face_id;
18667 it->len = saved_len;
18668 it->c = saved_c;
18669 it->char_to_display = saved_char_to_display;
18670 it->end_of_box_run_p = saved_box_end;
18671 return 1;
18672 }
18673 }
18674
18675 return 0;
18676 }
18677
18678
18679 /* Extend the face of the last glyph in the text area of IT->glyph_row
18680 to the end of the display line. Called from display_line. If the
18681 glyph row is empty, add a space glyph to it so that we know the
18682 face to draw. Set the glyph row flag fill_line_p. If the glyph
18683 row is R2L, prepend a stretch glyph to cover the empty space to the
18684 left of the leftmost glyph. */
18685
18686 static void
18687 extend_face_to_end_of_line (struct it *it)
18688 {
18689 struct face *face, *default_face;
18690 struct frame *f = it->f;
18691
18692 /* If line is already filled, do nothing. Non window-system frames
18693 get a grace of one more ``pixel'' because their characters are
18694 1-``pixel'' wide, so they hit the equality too early. This grace
18695 is needed only for R2L rows that are not continued, to produce
18696 one extra blank where we could display the cursor. */
18697 if (it->current_x >= it->last_visible_x
18698 + (!FRAME_WINDOW_P (f)
18699 && it->glyph_row->reversed_p
18700 && !it->glyph_row->continued_p))
18701 return;
18702
18703 /* The default face, possibly remapped. */
18704 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18705
18706 /* Face extension extends the background and box of IT->face_id
18707 to the end of the line. If the background equals the background
18708 of the frame, we don't have to do anything. */
18709 if (it->face_before_selective_p)
18710 face = FACE_FROM_ID (f, it->saved_face_id);
18711 else
18712 face = FACE_FROM_ID (f, it->face_id);
18713
18714 if (FRAME_WINDOW_P (f)
18715 && it->glyph_row->displays_text_p
18716 && face->box == FACE_NO_BOX
18717 && face->background == FRAME_BACKGROUND_PIXEL (f)
18718 && !face->stipple
18719 && !it->glyph_row->reversed_p)
18720 return;
18721
18722 /* Set the glyph row flag indicating that the face of the last glyph
18723 in the text area has to be drawn to the end of the text area. */
18724 it->glyph_row->fill_line_p = 1;
18725
18726 /* If current character of IT is not ASCII, make sure we have the
18727 ASCII face. This will be automatically undone the next time
18728 get_next_display_element returns a multibyte character. Note
18729 that the character will always be single byte in unibyte
18730 text. */
18731 if (!ASCII_CHAR_P (it->c))
18732 {
18733 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18734 }
18735
18736 if (FRAME_WINDOW_P (f))
18737 {
18738 /* If the row is empty, add a space with the current face of IT,
18739 so that we know which face to draw. */
18740 if (it->glyph_row->used[TEXT_AREA] == 0)
18741 {
18742 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18743 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18744 it->glyph_row->used[TEXT_AREA] = 1;
18745 }
18746 #ifdef HAVE_WINDOW_SYSTEM
18747 if (it->glyph_row->reversed_p)
18748 {
18749 /* Prepend a stretch glyph to the row, such that the
18750 rightmost glyph will be drawn flushed all the way to the
18751 right margin of the window. The stretch glyph that will
18752 occupy the empty space, if any, to the left of the
18753 glyphs. */
18754 struct font *font = face->font ? face->font : FRAME_FONT (f);
18755 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18756 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18757 struct glyph *g;
18758 int row_width, stretch_ascent, stretch_width;
18759 struct text_pos saved_pos;
18760 int saved_face_id, saved_avoid_cursor, saved_box_start;
18761
18762 for (row_width = 0, g = row_start; g < row_end; g++)
18763 row_width += g->pixel_width;
18764 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18765 if (stretch_width > 0)
18766 {
18767 stretch_ascent =
18768 (((it->ascent + it->descent)
18769 * FONT_BASE (font)) / FONT_HEIGHT (font));
18770 saved_pos = it->position;
18771 memset (&it->position, 0, sizeof it->position);
18772 saved_avoid_cursor = it->avoid_cursor_p;
18773 it->avoid_cursor_p = 1;
18774 saved_face_id = it->face_id;
18775 saved_box_start = it->start_of_box_run_p;
18776 /* The last row's stretch glyph should get the default
18777 face, to avoid painting the rest of the window with
18778 the region face, if the region ends at ZV. */
18779 if (it->glyph_row->ends_at_zv_p)
18780 it->face_id = default_face->id;
18781 else
18782 it->face_id = face->id;
18783 it->start_of_box_run_p = 0;
18784 append_stretch_glyph (it, make_number (0), stretch_width,
18785 it->ascent + it->descent, stretch_ascent);
18786 it->position = saved_pos;
18787 it->avoid_cursor_p = saved_avoid_cursor;
18788 it->face_id = saved_face_id;
18789 it->start_of_box_run_p = saved_box_start;
18790 }
18791 }
18792 #endif /* HAVE_WINDOW_SYSTEM */
18793 }
18794 else
18795 {
18796 /* Save some values that must not be changed. */
18797 int saved_x = it->current_x;
18798 struct text_pos saved_pos;
18799 Lisp_Object saved_object;
18800 enum display_element_type saved_what = it->what;
18801 int saved_face_id = it->face_id;
18802
18803 saved_object = it->object;
18804 saved_pos = it->position;
18805
18806 it->what = IT_CHARACTER;
18807 memset (&it->position, 0, sizeof it->position);
18808 it->object = make_number (0);
18809 it->c = it->char_to_display = ' ';
18810 it->len = 1;
18811 /* The last row's blank glyphs should get the default face, to
18812 avoid painting the rest of the window with the region face,
18813 if the region ends at ZV. */
18814 if (it->glyph_row->ends_at_zv_p)
18815 it->face_id = default_face->id;
18816 else
18817 it->face_id = face->id;
18818
18819 PRODUCE_GLYPHS (it);
18820
18821 while (it->current_x <= it->last_visible_x)
18822 PRODUCE_GLYPHS (it);
18823
18824 /* Don't count these blanks really. It would let us insert a left
18825 truncation glyph below and make us set the cursor on them, maybe. */
18826 it->current_x = saved_x;
18827 it->object = saved_object;
18828 it->position = saved_pos;
18829 it->what = saved_what;
18830 it->face_id = saved_face_id;
18831 }
18832 }
18833
18834
18835 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18836 trailing whitespace. */
18837
18838 static int
18839 trailing_whitespace_p (ptrdiff_t charpos)
18840 {
18841 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18842 int c = 0;
18843
18844 while (bytepos < ZV_BYTE
18845 && (c = FETCH_CHAR (bytepos),
18846 c == ' ' || c == '\t'))
18847 ++bytepos;
18848
18849 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18850 {
18851 if (bytepos != PT_BYTE)
18852 return 1;
18853 }
18854 return 0;
18855 }
18856
18857
18858 /* Highlight trailing whitespace, if any, in ROW. */
18859
18860 static void
18861 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18862 {
18863 int used = row->used[TEXT_AREA];
18864
18865 if (used)
18866 {
18867 struct glyph *start = row->glyphs[TEXT_AREA];
18868 struct glyph *glyph = start + used - 1;
18869
18870 if (row->reversed_p)
18871 {
18872 /* Right-to-left rows need to be processed in the opposite
18873 direction, so swap the edge pointers. */
18874 glyph = start;
18875 start = row->glyphs[TEXT_AREA] + used - 1;
18876 }
18877
18878 /* Skip over glyphs inserted to display the cursor at the
18879 end of a line, for extending the face of the last glyph
18880 to the end of the line on terminals, and for truncation
18881 and continuation glyphs. */
18882 if (!row->reversed_p)
18883 {
18884 while (glyph >= start
18885 && glyph->type == CHAR_GLYPH
18886 && INTEGERP (glyph->object))
18887 --glyph;
18888 }
18889 else
18890 {
18891 while (glyph <= start
18892 && glyph->type == CHAR_GLYPH
18893 && INTEGERP (glyph->object))
18894 ++glyph;
18895 }
18896
18897 /* If last glyph is a space or stretch, and it's trailing
18898 whitespace, set the face of all trailing whitespace glyphs in
18899 IT->glyph_row to `trailing-whitespace'. */
18900 if ((row->reversed_p ? glyph <= start : glyph >= start)
18901 && BUFFERP (glyph->object)
18902 && (glyph->type == STRETCH_GLYPH
18903 || (glyph->type == CHAR_GLYPH
18904 && glyph->u.ch == ' '))
18905 && trailing_whitespace_p (glyph->charpos))
18906 {
18907 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18908 if (face_id < 0)
18909 return;
18910
18911 if (!row->reversed_p)
18912 {
18913 while (glyph >= start
18914 && BUFFERP (glyph->object)
18915 && (glyph->type == STRETCH_GLYPH
18916 || (glyph->type == CHAR_GLYPH
18917 && glyph->u.ch == ' ')))
18918 (glyph--)->face_id = face_id;
18919 }
18920 else
18921 {
18922 while (glyph <= start
18923 && BUFFERP (glyph->object)
18924 && (glyph->type == STRETCH_GLYPH
18925 || (glyph->type == CHAR_GLYPH
18926 && glyph->u.ch == ' ')))
18927 (glyph++)->face_id = face_id;
18928 }
18929 }
18930 }
18931 }
18932
18933
18934 /* Value is non-zero if glyph row ROW should be
18935 used to hold the cursor. */
18936
18937 static int
18938 cursor_row_p (struct glyph_row *row)
18939 {
18940 int result = 1;
18941
18942 if (PT == CHARPOS (row->end.pos)
18943 || PT == MATRIX_ROW_END_CHARPOS (row))
18944 {
18945 /* Suppose the row ends on a string.
18946 Unless the row is continued, that means it ends on a newline
18947 in the string. If it's anything other than a display string
18948 (e.g., a before-string from an overlay), we don't want the
18949 cursor there. (This heuristic seems to give the optimal
18950 behavior for the various types of multi-line strings.)
18951 One exception: if the string has `cursor' property on one of
18952 its characters, we _do_ want the cursor there. */
18953 if (CHARPOS (row->end.string_pos) >= 0)
18954 {
18955 if (row->continued_p)
18956 result = 1;
18957 else
18958 {
18959 /* Check for `display' property. */
18960 struct glyph *beg = row->glyphs[TEXT_AREA];
18961 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18962 struct glyph *glyph;
18963
18964 result = 0;
18965 for (glyph = end; glyph >= beg; --glyph)
18966 if (STRINGP (glyph->object))
18967 {
18968 Lisp_Object prop
18969 = Fget_char_property (make_number (PT),
18970 Qdisplay, Qnil);
18971 result =
18972 (!NILP (prop)
18973 && display_prop_string_p (prop, glyph->object));
18974 /* If there's a `cursor' property on one of the
18975 string's characters, this row is a cursor row,
18976 even though this is not a display string. */
18977 if (!result)
18978 {
18979 Lisp_Object s = glyph->object;
18980
18981 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18982 {
18983 ptrdiff_t gpos = glyph->charpos;
18984
18985 if (!NILP (Fget_char_property (make_number (gpos),
18986 Qcursor, s)))
18987 {
18988 result = 1;
18989 break;
18990 }
18991 }
18992 }
18993 break;
18994 }
18995 }
18996 }
18997 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18998 {
18999 /* If the row ends in middle of a real character,
19000 and the line is continued, we want the cursor here.
19001 That's because CHARPOS (ROW->end.pos) would equal
19002 PT if PT is before the character. */
19003 if (!row->ends_in_ellipsis_p)
19004 result = row->continued_p;
19005 else
19006 /* If the row ends in an ellipsis, then
19007 CHARPOS (ROW->end.pos) will equal point after the
19008 invisible text. We want that position to be displayed
19009 after the ellipsis. */
19010 result = 0;
19011 }
19012 /* If the row ends at ZV, display the cursor at the end of that
19013 row instead of at the start of the row below. */
19014 else if (row->ends_at_zv_p)
19015 result = 1;
19016 else
19017 result = 0;
19018 }
19019
19020 return result;
19021 }
19022
19023 \f
19024
19025 /* Push the property PROP so that it will be rendered at the current
19026 position in IT. Return 1 if PROP was successfully pushed, 0
19027 otherwise. Called from handle_line_prefix to handle the
19028 `line-prefix' and `wrap-prefix' properties. */
19029
19030 static int
19031 push_prefix_prop (struct it *it, Lisp_Object prop)
19032 {
19033 struct text_pos pos =
19034 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19035
19036 eassert (it->method == GET_FROM_BUFFER
19037 || it->method == GET_FROM_DISPLAY_VECTOR
19038 || it->method == GET_FROM_STRING);
19039
19040 /* We need to save the current buffer/string position, so it will be
19041 restored by pop_it, because iterate_out_of_display_property
19042 depends on that being set correctly, but some situations leave
19043 it->position not yet set when this function is called. */
19044 push_it (it, &pos);
19045
19046 if (STRINGP (prop))
19047 {
19048 if (SCHARS (prop) == 0)
19049 {
19050 pop_it (it);
19051 return 0;
19052 }
19053
19054 it->string = prop;
19055 it->string_from_prefix_prop_p = 1;
19056 it->multibyte_p = STRING_MULTIBYTE (it->string);
19057 it->current.overlay_string_index = -1;
19058 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19059 it->end_charpos = it->string_nchars = SCHARS (it->string);
19060 it->method = GET_FROM_STRING;
19061 it->stop_charpos = 0;
19062 it->prev_stop = 0;
19063 it->base_level_stop = 0;
19064
19065 /* Force paragraph direction to be that of the parent
19066 buffer/string. */
19067 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19068 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19069 else
19070 it->paragraph_embedding = L2R;
19071
19072 /* Set up the bidi iterator for this display string. */
19073 if (it->bidi_p)
19074 {
19075 it->bidi_it.string.lstring = it->string;
19076 it->bidi_it.string.s = NULL;
19077 it->bidi_it.string.schars = it->end_charpos;
19078 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19079 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19080 it->bidi_it.string.unibyte = !it->multibyte_p;
19081 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19082 }
19083 }
19084 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19085 {
19086 it->method = GET_FROM_STRETCH;
19087 it->object = prop;
19088 }
19089 #ifdef HAVE_WINDOW_SYSTEM
19090 else if (IMAGEP (prop))
19091 {
19092 it->what = IT_IMAGE;
19093 it->image_id = lookup_image (it->f, prop);
19094 it->method = GET_FROM_IMAGE;
19095 }
19096 #endif /* HAVE_WINDOW_SYSTEM */
19097 else
19098 {
19099 pop_it (it); /* bogus display property, give up */
19100 return 0;
19101 }
19102
19103 return 1;
19104 }
19105
19106 /* Return the character-property PROP at the current position in IT. */
19107
19108 static Lisp_Object
19109 get_it_property (struct it *it, Lisp_Object prop)
19110 {
19111 Lisp_Object position;
19112
19113 if (STRINGP (it->object))
19114 position = make_number (IT_STRING_CHARPOS (*it));
19115 else if (BUFFERP (it->object))
19116 position = make_number (IT_CHARPOS (*it));
19117 else
19118 return Qnil;
19119
19120 return Fget_char_property (position, prop, it->object);
19121 }
19122
19123 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19124
19125 static void
19126 handle_line_prefix (struct it *it)
19127 {
19128 Lisp_Object prefix;
19129
19130 if (it->continuation_lines_width > 0)
19131 {
19132 prefix = get_it_property (it, Qwrap_prefix);
19133 if (NILP (prefix))
19134 prefix = Vwrap_prefix;
19135 }
19136 else
19137 {
19138 prefix = get_it_property (it, Qline_prefix);
19139 if (NILP (prefix))
19140 prefix = Vline_prefix;
19141 }
19142 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19143 {
19144 /* If the prefix is wider than the window, and we try to wrap
19145 it, it would acquire its own wrap prefix, and so on till the
19146 iterator stack overflows. So, don't wrap the prefix. */
19147 it->line_wrap = TRUNCATE;
19148 it->avoid_cursor_p = 1;
19149 }
19150 }
19151
19152 \f
19153
19154 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19155 only for R2L lines from display_line and display_string, when they
19156 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19157 the line/string needs to be continued on the next glyph row. */
19158 static void
19159 unproduce_glyphs (struct it *it, int n)
19160 {
19161 struct glyph *glyph, *end;
19162
19163 eassert (it->glyph_row);
19164 eassert (it->glyph_row->reversed_p);
19165 eassert (it->area == TEXT_AREA);
19166 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19167
19168 if (n > it->glyph_row->used[TEXT_AREA])
19169 n = it->glyph_row->used[TEXT_AREA];
19170 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19171 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19172 for ( ; glyph < end; glyph++)
19173 glyph[-n] = *glyph;
19174 }
19175
19176 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19177 and ROW->maxpos. */
19178 static void
19179 find_row_edges (struct it *it, struct glyph_row *row,
19180 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19181 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19182 {
19183 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19184 lines' rows is implemented for bidi-reordered rows. */
19185
19186 /* ROW->minpos is the value of min_pos, the minimal buffer position
19187 we have in ROW, or ROW->start.pos if that is smaller. */
19188 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19189 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19190 else
19191 /* We didn't find buffer positions smaller than ROW->start, or
19192 didn't find _any_ valid buffer positions in any of the glyphs,
19193 so we must trust the iterator's computed positions. */
19194 row->minpos = row->start.pos;
19195 if (max_pos <= 0)
19196 {
19197 max_pos = CHARPOS (it->current.pos);
19198 max_bpos = BYTEPOS (it->current.pos);
19199 }
19200
19201 /* Here are the various use-cases for ending the row, and the
19202 corresponding values for ROW->maxpos:
19203
19204 Line ends in a newline from buffer eol_pos + 1
19205 Line is continued from buffer max_pos + 1
19206 Line is truncated on right it->current.pos
19207 Line ends in a newline from string max_pos + 1(*)
19208 (*) + 1 only when line ends in a forward scan
19209 Line is continued from string max_pos
19210 Line is continued from display vector max_pos
19211 Line is entirely from a string min_pos == max_pos
19212 Line is entirely from a display vector min_pos == max_pos
19213 Line that ends at ZV ZV
19214
19215 If you discover other use-cases, please add them here as
19216 appropriate. */
19217 if (row->ends_at_zv_p)
19218 row->maxpos = it->current.pos;
19219 else if (row->used[TEXT_AREA])
19220 {
19221 int seen_this_string = 0;
19222 struct glyph_row *r1 = row - 1;
19223
19224 /* Did we see the same display string on the previous row? */
19225 if (STRINGP (it->object)
19226 /* this is not the first row */
19227 && row > it->w->desired_matrix->rows
19228 /* previous row is not the header line */
19229 && !r1->mode_line_p
19230 /* previous row also ends in a newline from a string */
19231 && r1->ends_in_newline_from_string_p)
19232 {
19233 struct glyph *start, *end;
19234
19235 /* Search for the last glyph of the previous row that came
19236 from buffer or string. Depending on whether the row is
19237 L2R or R2L, we need to process it front to back or the
19238 other way round. */
19239 if (!r1->reversed_p)
19240 {
19241 start = r1->glyphs[TEXT_AREA];
19242 end = start + r1->used[TEXT_AREA];
19243 /* Glyphs inserted by redisplay have an integer (zero)
19244 as their object. */
19245 while (end > start
19246 && INTEGERP ((end - 1)->object)
19247 && (end - 1)->charpos <= 0)
19248 --end;
19249 if (end > start)
19250 {
19251 if (EQ ((end - 1)->object, it->object))
19252 seen_this_string = 1;
19253 }
19254 else
19255 /* If all the glyphs of the previous row were inserted
19256 by redisplay, it means the previous row was
19257 produced from a single newline, which is only
19258 possible if that newline came from the same string
19259 as the one which produced this ROW. */
19260 seen_this_string = 1;
19261 }
19262 else
19263 {
19264 end = r1->glyphs[TEXT_AREA] - 1;
19265 start = end + r1->used[TEXT_AREA];
19266 while (end < start
19267 && INTEGERP ((end + 1)->object)
19268 && (end + 1)->charpos <= 0)
19269 ++end;
19270 if (end < start)
19271 {
19272 if (EQ ((end + 1)->object, it->object))
19273 seen_this_string = 1;
19274 }
19275 else
19276 seen_this_string = 1;
19277 }
19278 }
19279 /* Take note of each display string that covers a newline only
19280 once, the first time we see it. This is for when a display
19281 string includes more than one newline in it. */
19282 if (row->ends_in_newline_from_string_p && !seen_this_string)
19283 {
19284 /* If we were scanning the buffer forward when we displayed
19285 the string, we want to account for at least one buffer
19286 position that belongs to this row (position covered by
19287 the display string), so that cursor positioning will
19288 consider this row as a candidate when point is at the end
19289 of the visual line represented by this row. This is not
19290 required when scanning back, because max_pos will already
19291 have a much larger value. */
19292 if (CHARPOS (row->end.pos) > max_pos)
19293 INC_BOTH (max_pos, max_bpos);
19294 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19295 }
19296 else if (CHARPOS (it->eol_pos) > 0)
19297 SET_TEXT_POS (row->maxpos,
19298 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19299 else if (row->continued_p)
19300 {
19301 /* If max_pos is different from IT's current position, it
19302 means IT->method does not belong to the display element
19303 at max_pos. However, it also means that the display
19304 element at max_pos was displayed in its entirety on this
19305 line, which is equivalent to saying that the next line
19306 starts at the next buffer position. */
19307 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19308 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19309 else
19310 {
19311 INC_BOTH (max_pos, max_bpos);
19312 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19313 }
19314 }
19315 else if (row->truncated_on_right_p)
19316 /* display_line already called reseat_at_next_visible_line_start,
19317 which puts the iterator at the beginning of the next line, in
19318 the logical order. */
19319 row->maxpos = it->current.pos;
19320 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19321 /* A line that is entirely from a string/image/stretch... */
19322 row->maxpos = row->minpos;
19323 else
19324 emacs_abort ();
19325 }
19326 else
19327 row->maxpos = it->current.pos;
19328 }
19329
19330 /* Construct the glyph row IT->glyph_row in the desired matrix of
19331 IT->w from text at the current position of IT. See dispextern.h
19332 for an overview of struct it. Value is non-zero if
19333 IT->glyph_row displays text, as opposed to a line displaying ZV
19334 only. */
19335
19336 static int
19337 display_line (struct it *it)
19338 {
19339 struct glyph_row *row = it->glyph_row;
19340 Lisp_Object overlay_arrow_string;
19341 struct it wrap_it;
19342 void *wrap_data = NULL;
19343 int may_wrap = 0, wrap_x IF_LINT (= 0);
19344 int wrap_row_used = -1;
19345 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19346 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19347 int wrap_row_extra_line_spacing IF_LINT (= 0);
19348 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19349 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19350 int cvpos;
19351 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19352 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19353
19354 /* We always start displaying at hpos zero even if hscrolled. */
19355 eassert (it->hpos == 0 && it->current_x == 0);
19356
19357 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19358 >= it->w->desired_matrix->nrows)
19359 {
19360 it->w->nrows_scale_factor++;
19361 fonts_changed_p = 1;
19362 return 0;
19363 }
19364
19365 /* Is IT->w showing the region? */
19366 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19367
19368 /* Clear the result glyph row and enable it. */
19369 prepare_desired_row (row);
19370
19371 row->y = it->current_y;
19372 row->start = it->start;
19373 row->continuation_lines_width = it->continuation_lines_width;
19374 row->displays_text_p = 1;
19375 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19376 it->starts_in_middle_of_char_p = 0;
19377
19378 /* Arrange the overlays nicely for our purposes. Usually, we call
19379 display_line on only one line at a time, in which case this
19380 can't really hurt too much, or we call it on lines which appear
19381 one after another in the buffer, in which case all calls to
19382 recenter_overlay_lists but the first will be pretty cheap. */
19383 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19384
19385 /* Move over display elements that are not visible because we are
19386 hscrolled. This may stop at an x-position < IT->first_visible_x
19387 if the first glyph is partially visible or if we hit a line end. */
19388 if (it->current_x < it->first_visible_x)
19389 {
19390 enum move_it_result move_result;
19391
19392 this_line_min_pos = row->start.pos;
19393 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19394 MOVE_TO_POS | MOVE_TO_X);
19395 /* If we are under a large hscroll, move_it_in_display_line_to
19396 could hit the end of the line without reaching
19397 it->first_visible_x. Pretend that we did reach it. This is
19398 especially important on a TTY, where we will call
19399 extend_face_to_end_of_line, which needs to know how many
19400 blank glyphs to produce. */
19401 if (it->current_x < it->first_visible_x
19402 && (move_result == MOVE_NEWLINE_OR_CR
19403 || move_result == MOVE_POS_MATCH_OR_ZV))
19404 it->current_x = it->first_visible_x;
19405
19406 /* Record the smallest positions seen while we moved over
19407 display elements that are not visible. This is needed by
19408 redisplay_internal for optimizing the case where the cursor
19409 stays inside the same line. The rest of this function only
19410 considers positions that are actually displayed, so
19411 RECORD_MAX_MIN_POS will not otherwise record positions that
19412 are hscrolled to the left of the left edge of the window. */
19413 min_pos = CHARPOS (this_line_min_pos);
19414 min_bpos = BYTEPOS (this_line_min_pos);
19415 }
19416 else
19417 {
19418 /* We only do this when not calling `move_it_in_display_line_to'
19419 above, because move_it_in_display_line_to calls
19420 handle_line_prefix itself. */
19421 handle_line_prefix (it);
19422 }
19423
19424 /* Get the initial row height. This is either the height of the
19425 text hscrolled, if there is any, or zero. */
19426 row->ascent = it->max_ascent;
19427 row->height = it->max_ascent + it->max_descent;
19428 row->phys_ascent = it->max_phys_ascent;
19429 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19430 row->extra_line_spacing = it->max_extra_line_spacing;
19431
19432 /* Utility macro to record max and min buffer positions seen until now. */
19433 #define RECORD_MAX_MIN_POS(IT) \
19434 do \
19435 { \
19436 int composition_p = !STRINGP ((IT)->string) \
19437 && ((IT)->what == IT_COMPOSITION); \
19438 ptrdiff_t current_pos = \
19439 composition_p ? (IT)->cmp_it.charpos \
19440 : IT_CHARPOS (*(IT)); \
19441 ptrdiff_t current_bpos = \
19442 composition_p ? CHAR_TO_BYTE (current_pos) \
19443 : IT_BYTEPOS (*(IT)); \
19444 if (current_pos < min_pos) \
19445 { \
19446 min_pos = current_pos; \
19447 min_bpos = current_bpos; \
19448 } \
19449 if (IT_CHARPOS (*it) > max_pos) \
19450 { \
19451 max_pos = IT_CHARPOS (*it); \
19452 max_bpos = IT_BYTEPOS (*it); \
19453 } \
19454 } \
19455 while (0)
19456
19457 /* Loop generating characters. The loop is left with IT on the next
19458 character to display. */
19459 while (1)
19460 {
19461 int n_glyphs_before, hpos_before, x_before;
19462 int x, nglyphs;
19463 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19464
19465 /* Retrieve the next thing to display. Value is zero if end of
19466 buffer reached. */
19467 if (!get_next_display_element (it))
19468 {
19469 /* Maybe add a space at the end of this line that is used to
19470 display the cursor there under X. Set the charpos of the
19471 first glyph of blank lines not corresponding to any text
19472 to -1. */
19473 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19474 row->exact_window_width_line_p = 1;
19475 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19476 || row->used[TEXT_AREA] == 0)
19477 {
19478 row->glyphs[TEXT_AREA]->charpos = -1;
19479 row->displays_text_p = 0;
19480
19481 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19482 && (!MINI_WINDOW_P (it->w)
19483 || (minibuf_level && EQ (it->window, minibuf_window))))
19484 row->indicate_empty_line_p = 1;
19485 }
19486
19487 it->continuation_lines_width = 0;
19488 row->ends_at_zv_p = 1;
19489 /* A row that displays right-to-left text must always have
19490 its last face extended all the way to the end of line,
19491 even if this row ends in ZV, because we still write to
19492 the screen left to right. We also need to extend the
19493 last face if the default face is remapped to some
19494 different face, otherwise the functions that clear
19495 portions of the screen will clear with the default face's
19496 background color. */
19497 if (row->reversed_p
19498 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19499 extend_face_to_end_of_line (it);
19500 break;
19501 }
19502
19503 /* Now, get the metrics of what we want to display. This also
19504 generates glyphs in `row' (which is IT->glyph_row). */
19505 n_glyphs_before = row->used[TEXT_AREA];
19506 x = it->current_x;
19507
19508 /* Remember the line height so far in case the next element doesn't
19509 fit on the line. */
19510 if (it->line_wrap != TRUNCATE)
19511 {
19512 ascent = it->max_ascent;
19513 descent = it->max_descent;
19514 phys_ascent = it->max_phys_ascent;
19515 phys_descent = it->max_phys_descent;
19516
19517 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19518 {
19519 if (IT_DISPLAYING_WHITESPACE (it))
19520 may_wrap = 1;
19521 else if (may_wrap)
19522 {
19523 SAVE_IT (wrap_it, *it, wrap_data);
19524 wrap_x = x;
19525 wrap_row_used = row->used[TEXT_AREA];
19526 wrap_row_ascent = row->ascent;
19527 wrap_row_height = row->height;
19528 wrap_row_phys_ascent = row->phys_ascent;
19529 wrap_row_phys_height = row->phys_height;
19530 wrap_row_extra_line_spacing = row->extra_line_spacing;
19531 wrap_row_min_pos = min_pos;
19532 wrap_row_min_bpos = min_bpos;
19533 wrap_row_max_pos = max_pos;
19534 wrap_row_max_bpos = max_bpos;
19535 may_wrap = 0;
19536 }
19537 }
19538 }
19539
19540 PRODUCE_GLYPHS (it);
19541
19542 /* If this display element was in marginal areas, continue with
19543 the next one. */
19544 if (it->area != TEXT_AREA)
19545 {
19546 row->ascent = max (row->ascent, it->max_ascent);
19547 row->height = max (row->height, it->max_ascent + it->max_descent);
19548 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19549 row->phys_height = max (row->phys_height,
19550 it->max_phys_ascent + it->max_phys_descent);
19551 row->extra_line_spacing = max (row->extra_line_spacing,
19552 it->max_extra_line_spacing);
19553 set_iterator_to_next (it, 1);
19554 continue;
19555 }
19556
19557 /* Does the display element fit on the line? If we truncate
19558 lines, we should draw past the right edge of the window. If
19559 we don't truncate, we want to stop so that we can display the
19560 continuation glyph before the right margin. If lines are
19561 continued, there are two possible strategies for characters
19562 resulting in more than 1 glyph (e.g. tabs): Display as many
19563 glyphs as possible in this line and leave the rest for the
19564 continuation line, or display the whole element in the next
19565 line. Original redisplay did the former, so we do it also. */
19566 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19567 hpos_before = it->hpos;
19568 x_before = x;
19569
19570 if (/* Not a newline. */
19571 nglyphs > 0
19572 /* Glyphs produced fit entirely in the line. */
19573 && it->current_x < it->last_visible_x)
19574 {
19575 it->hpos += nglyphs;
19576 row->ascent = max (row->ascent, it->max_ascent);
19577 row->height = max (row->height, it->max_ascent + it->max_descent);
19578 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19579 row->phys_height = max (row->phys_height,
19580 it->max_phys_ascent + it->max_phys_descent);
19581 row->extra_line_spacing = max (row->extra_line_spacing,
19582 it->max_extra_line_spacing);
19583 if (it->current_x - it->pixel_width < it->first_visible_x)
19584 row->x = x - it->first_visible_x;
19585 /* Record the maximum and minimum buffer positions seen so
19586 far in glyphs that will be displayed by this row. */
19587 if (it->bidi_p)
19588 RECORD_MAX_MIN_POS (it);
19589 }
19590 else
19591 {
19592 int i, new_x;
19593 struct glyph *glyph;
19594
19595 for (i = 0; i < nglyphs; ++i, x = new_x)
19596 {
19597 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19598 new_x = x + glyph->pixel_width;
19599
19600 if (/* Lines are continued. */
19601 it->line_wrap != TRUNCATE
19602 && (/* Glyph doesn't fit on the line. */
19603 new_x > it->last_visible_x
19604 /* Or it fits exactly on a window system frame. */
19605 || (new_x == it->last_visible_x
19606 && FRAME_WINDOW_P (it->f)
19607 && (row->reversed_p
19608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19610 {
19611 /* End of a continued line. */
19612
19613 if (it->hpos == 0
19614 || (new_x == it->last_visible_x
19615 && FRAME_WINDOW_P (it->f)
19616 && (row->reversed_p
19617 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19618 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19619 {
19620 /* Current glyph is the only one on the line or
19621 fits exactly on the line. We must continue
19622 the line because we can't draw the cursor
19623 after the glyph. */
19624 row->continued_p = 1;
19625 it->current_x = new_x;
19626 it->continuation_lines_width += new_x;
19627 ++it->hpos;
19628 if (i == nglyphs - 1)
19629 {
19630 /* If line-wrap is on, check if a previous
19631 wrap point was found. */
19632 if (wrap_row_used > 0
19633 /* Even if there is a previous wrap
19634 point, continue the line here as
19635 usual, if (i) the previous character
19636 was a space or tab AND (ii) the
19637 current character is not. */
19638 && (!may_wrap
19639 || IT_DISPLAYING_WHITESPACE (it)))
19640 goto back_to_wrap;
19641
19642 /* Record the maximum and minimum buffer
19643 positions seen so far in glyphs that will be
19644 displayed by this row. */
19645 if (it->bidi_p)
19646 RECORD_MAX_MIN_POS (it);
19647 set_iterator_to_next (it, 1);
19648 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19649 {
19650 if (!get_next_display_element (it))
19651 {
19652 row->exact_window_width_line_p = 1;
19653 it->continuation_lines_width = 0;
19654 row->continued_p = 0;
19655 row->ends_at_zv_p = 1;
19656 }
19657 else if (ITERATOR_AT_END_OF_LINE_P (it))
19658 {
19659 row->continued_p = 0;
19660 row->exact_window_width_line_p = 1;
19661 }
19662 }
19663 }
19664 else if (it->bidi_p)
19665 RECORD_MAX_MIN_POS (it);
19666 }
19667 else if (CHAR_GLYPH_PADDING_P (*glyph)
19668 && !FRAME_WINDOW_P (it->f))
19669 {
19670 /* A padding glyph that doesn't fit on this line.
19671 This means the whole character doesn't fit
19672 on the line. */
19673 if (row->reversed_p)
19674 unproduce_glyphs (it, row->used[TEXT_AREA]
19675 - n_glyphs_before);
19676 row->used[TEXT_AREA] = n_glyphs_before;
19677
19678 /* Fill the rest of the row with continuation
19679 glyphs like in 20.x. */
19680 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19681 < row->glyphs[1 + TEXT_AREA])
19682 produce_special_glyphs (it, IT_CONTINUATION);
19683
19684 row->continued_p = 1;
19685 it->current_x = x_before;
19686 it->continuation_lines_width += x_before;
19687
19688 /* Restore the height to what it was before the
19689 element not fitting on the line. */
19690 it->max_ascent = ascent;
19691 it->max_descent = descent;
19692 it->max_phys_ascent = phys_ascent;
19693 it->max_phys_descent = phys_descent;
19694 }
19695 else if (wrap_row_used > 0)
19696 {
19697 back_to_wrap:
19698 if (row->reversed_p)
19699 unproduce_glyphs (it,
19700 row->used[TEXT_AREA] - wrap_row_used);
19701 RESTORE_IT (it, &wrap_it, wrap_data);
19702 it->continuation_lines_width += wrap_x;
19703 row->used[TEXT_AREA] = wrap_row_used;
19704 row->ascent = wrap_row_ascent;
19705 row->height = wrap_row_height;
19706 row->phys_ascent = wrap_row_phys_ascent;
19707 row->phys_height = wrap_row_phys_height;
19708 row->extra_line_spacing = wrap_row_extra_line_spacing;
19709 min_pos = wrap_row_min_pos;
19710 min_bpos = wrap_row_min_bpos;
19711 max_pos = wrap_row_max_pos;
19712 max_bpos = wrap_row_max_bpos;
19713 row->continued_p = 1;
19714 row->ends_at_zv_p = 0;
19715 row->exact_window_width_line_p = 0;
19716 it->continuation_lines_width += x;
19717
19718 /* Make sure that a non-default face is extended
19719 up to the right margin of the window. */
19720 extend_face_to_end_of_line (it);
19721 }
19722 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19723 {
19724 /* A TAB that extends past the right edge of the
19725 window. This produces a single glyph on
19726 window system frames. We leave the glyph in
19727 this row and let it fill the row, but don't
19728 consume the TAB. */
19729 if ((row->reversed_p
19730 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19731 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19732 produce_special_glyphs (it, IT_CONTINUATION);
19733 it->continuation_lines_width += it->last_visible_x;
19734 row->ends_in_middle_of_char_p = 1;
19735 row->continued_p = 1;
19736 glyph->pixel_width = it->last_visible_x - x;
19737 it->starts_in_middle_of_char_p = 1;
19738 }
19739 else
19740 {
19741 /* Something other than a TAB that draws past
19742 the right edge of the window. Restore
19743 positions to values before the element. */
19744 if (row->reversed_p)
19745 unproduce_glyphs (it, row->used[TEXT_AREA]
19746 - (n_glyphs_before + i));
19747 row->used[TEXT_AREA] = n_glyphs_before + i;
19748
19749 /* Display continuation glyphs. */
19750 it->current_x = x_before;
19751 it->continuation_lines_width += x;
19752 if (!FRAME_WINDOW_P (it->f)
19753 || (row->reversed_p
19754 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19755 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19756 produce_special_glyphs (it, IT_CONTINUATION);
19757 row->continued_p = 1;
19758
19759 extend_face_to_end_of_line (it);
19760
19761 if (nglyphs > 1 && i > 0)
19762 {
19763 row->ends_in_middle_of_char_p = 1;
19764 it->starts_in_middle_of_char_p = 1;
19765 }
19766
19767 /* Restore the height to what it was before the
19768 element not fitting on the line. */
19769 it->max_ascent = ascent;
19770 it->max_descent = descent;
19771 it->max_phys_ascent = phys_ascent;
19772 it->max_phys_descent = phys_descent;
19773 }
19774
19775 break;
19776 }
19777 else if (new_x > it->first_visible_x)
19778 {
19779 /* Increment number of glyphs actually displayed. */
19780 ++it->hpos;
19781
19782 /* Record the maximum and minimum buffer positions
19783 seen so far in glyphs that will be displayed by
19784 this row. */
19785 if (it->bidi_p)
19786 RECORD_MAX_MIN_POS (it);
19787
19788 if (x < it->first_visible_x)
19789 /* Glyph is partially visible, i.e. row starts at
19790 negative X position. */
19791 row->x = x - it->first_visible_x;
19792 }
19793 else
19794 {
19795 /* Glyph is completely off the left margin of the
19796 window. This should not happen because of the
19797 move_it_in_display_line at the start of this
19798 function, unless the text display area of the
19799 window is empty. */
19800 eassert (it->first_visible_x <= it->last_visible_x);
19801 }
19802 }
19803 /* Even if this display element produced no glyphs at all,
19804 we want to record its position. */
19805 if (it->bidi_p && nglyphs == 0)
19806 RECORD_MAX_MIN_POS (it);
19807
19808 row->ascent = max (row->ascent, it->max_ascent);
19809 row->height = max (row->height, it->max_ascent + it->max_descent);
19810 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19811 row->phys_height = max (row->phys_height,
19812 it->max_phys_ascent + it->max_phys_descent);
19813 row->extra_line_spacing = max (row->extra_line_spacing,
19814 it->max_extra_line_spacing);
19815
19816 /* End of this display line if row is continued. */
19817 if (row->continued_p || row->ends_at_zv_p)
19818 break;
19819 }
19820
19821 at_end_of_line:
19822 /* Is this a line end? If yes, we're also done, after making
19823 sure that a non-default face is extended up to the right
19824 margin of the window. */
19825 if (ITERATOR_AT_END_OF_LINE_P (it))
19826 {
19827 int used_before = row->used[TEXT_AREA];
19828
19829 row->ends_in_newline_from_string_p = STRINGP (it->object);
19830
19831 /* Add a space at the end of the line that is used to
19832 display the cursor there. */
19833 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19834 append_space_for_newline (it, 0);
19835
19836 /* Extend the face to the end of the line. */
19837 extend_face_to_end_of_line (it);
19838
19839 /* Make sure we have the position. */
19840 if (used_before == 0)
19841 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19842
19843 /* Record the position of the newline, for use in
19844 find_row_edges. */
19845 it->eol_pos = it->current.pos;
19846
19847 /* Consume the line end. This skips over invisible lines. */
19848 set_iterator_to_next (it, 1);
19849 it->continuation_lines_width = 0;
19850 break;
19851 }
19852
19853 /* Proceed with next display element. Note that this skips
19854 over lines invisible because of selective display. */
19855 set_iterator_to_next (it, 1);
19856
19857 /* If we truncate lines, we are done when the last displayed
19858 glyphs reach past the right margin of the window. */
19859 if (it->line_wrap == TRUNCATE
19860 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19861 ? (it->current_x >= it->last_visible_x)
19862 : (it->current_x > it->last_visible_x)))
19863 {
19864 /* Maybe add truncation glyphs. */
19865 if (!FRAME_WINDOW_P (it->f)
19866 || (row->reversed_p
19867 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19868 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19869 {
19870 int i, n;
19871
19872 if (!row->reversed_p)
19873 {
19874 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19875 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19876 break;
19877 }
19878 else
19879 {
19880 for (i = 0; i < row->used[TEXT_AREA]; i++)
19881 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19882 break;
19883 /* Remove any padding glyphs at the front of ROW, to
19884 make room for the truncation glyphs we will be
19885 adding below. The loop below always inserts at
19886 least one truncation glyph, so also remove the
19887 last glyph added to ROW. */
19888 unproduce_glyphs (it, i + 1);
19889 /* Adjust i for the loop below. */
19890 i = row->used[TEXT_AREA] - (i + 1);
19891 }
19892
19893 it->current_x = x_before;
19894 if (!FRAME_WINDOW_P (it->f))
19895 {
19896 for (n = row->used[TEXT_AREA]; i < n; ++i)
19897 {
19898 row->used[TEXT_AREA] = i;
19899 produce_special_glyphs (it, IT_TRUNCATION);
19900 }
19901 }
19902 else
19903 {
19904 row->used[TEXT_AREA] = i;
19905 produce_special_glyphs (it, IT_TRUNCATION);
19906 }
19907 }
19908 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19909 {
19910 /* Don't truncate if we can overflow newline into fringe. */
19911 if (!get_next_display_element (it))
19912 {
19913 it->continuation_lines_width = 0;
19914 row->ends_at_zv_p = 1;
19915 row->exact_window_width_line_p = 1;
19916 break;
19917 }
19918 if (ITERATOR_AT_END_OF_LINE_P (it))
19919 {
19920 row->exact_window_width_line_p = 1;
19921 goto at_end_of_line;
19922 }
19923 it->current_x = x_before;
19924 }
19925
19926 row->truncated_on_right_p = 1;
19927 it->continuation_lines_width = 0;
19928 reseat_at_next_visible_line_start (it, 0);
19929 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19930 it->hpos = hpos_before;
19931 break;
19932 }
19933 }
19934
19935 if (wrap_data)
19936 bidi_unshelve_cache (wrap_data, 1);
19937
19938 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19939 at the left window margin. */
19940 if (it->first_visible_x
19941 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19942 {
19943 if (!FRAME_WINDOW_P (it->f)
19944 || (row->reversed_p
19945 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19946 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19947 insert_left_trunc_glyphs (it);
19948 row->truncated_on_left_p = 1;
19949 }
19950
19951 /* Remember the position at which this line ends.
19952
19953 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19954 cannot be before the call to find_row_edges below, since that is
19955 where these positions are determined. */
19956 row->end = it->current;
19957 if (!it->bidi_p)
19958 {
19959 row->minpos = row->start.pos;
19960 row->maxpos = row->end.pos;
19961 }
19962 else
19963 {
19964 /* ROW->minpos and ROW->maxpos must be the smallest and
19965 `1 + the largest' buffer positions in ROW. But if ROW was
19966 bidi-reordered, these two positions can be anywhere in the
19967 row, so we must determine them now. */
19968 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19969 }
19970
19971 /* If the start of this line is the overlay arrow-position, then
19972 mark this glyph row as the one containing the overlay arrow.
19973 This is clearly a mess with variable size fonts. It would be
19974 better to let it be displayed like cursors under X. */
19975 if ((row->displays_text_p || !overlay_arrow_seen)
19976 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19977 !NILP (overlay_arrow_string)))
19978 {
19979 /* Overlay arrow in window redisplay is a fringe bitmap. */
19980 if (STRINGP (overlay_arrow_string))
19981 {
19982 struct glyph_row *arrow_row
19983 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19984 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19985 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19986 struct glyph *p = row->glyphs[TEXT_AREA];
19987 struct glyph *p2, *end;
19988
19989 /* Copy the arrow glyphs. */
19990 while (glyph < arrow_end)
19991 *p++ = *glyph++;
19992
19993 /* Throw away padding glyphs. */
19994 p2 = p;
19995 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19996 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19997 ++p2;
19998 if (p2 > p)
19999 {
20000 while (p2 < end)
20001 *p++ = *p2++;
20002 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20003 }
20004 }
20005 else
20006 {
20007 eassert (INTEGERP (overlay_arrow_string));
20008 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20009 }
20010 overlay_arrow_seen = 1;
20011 }
20012
20013 /* Highlight trailing whitespace. */
20014 if (!NILP (Vshow_trailing_whitespace))
20015 highlight_trailing_whitespace (it->f, it->glyph_row);
20016
20017 /* Compute pixel dimensions of this line. */
20018 compute_line_metrics (it);
20019
20020 /* Implementation note: No changes in the glyphs of ROW or in their
20021 faces can be done past this point, because compute_line_metrics
20022 computes ROW's hash value and stores it within the glyph_row
20023 structure. */
20024
20025 /* Record whether this row ends inside an ellipsis. */
20026 row->ends_in_ellipsis_p
20027 = (it->method == GET_FROM_DISPLAY_VECTOR
20028 && it->ellipsis_p);
20029
20030 /* Save fringe bitmaps in this row. */
20031 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20032 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20033 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20034 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20035
20036 it->left_user_fringe_bitmap = 0;
20037 it->left_user_fringe_face_id = 0;
20038 it->right_user_fringe_bitmap = 0;
20039 it->right_user_fringe_face_id = 0;
20040
20041 /* Maybe set the cursor. */
20042 cvpos = it->w->cursor.vpos;
20043 if ((cvpos < 0
20044 /* In bidi-reordered rows, keep checking for proper cursor
20045 position even if one has been found already, because buffer
20046 positions in such rows change non-linearly with ROW->VPOS,
20047 when a line is continued. One exception: when we are at ZV,
20048 display cursor on the first suitable glyph row, since all
20049 the empty rows after that also have their position set to ZV. */
20050 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20051 lines' rows is implemented for bidi-reordered rows. */
20052 || (it->bidi_p
20053 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20054 && PT >= MATRIX_ROW_START_CHARPOS (row)
20055 && PT <= MATRIX_ROW_END_CHARPOS (row)
20056 && cursor_row_p (row))
20057 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20058
20059 /* Prepare for the next line. This line starts horizontally at (X
20060 HPOS) = (0 0). Vertical positions are incremented. As a
20061 convenience for the caller, IT->glyph_row is set to the next
20062 row to be used. */
20063 it->current_x = it->hpos = 0;
20064 it->current_y += row->height;
20065 SET_TEXT_POS (it->eol_pos, 0, 0);
20066 ++it->vpos;
20067 ++it->glyph_row;
20068 /* The next row should by default use the same value of the
20069 reversed_p flag as this one. set_iterator_to_next decides when
20070 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20071 the flag accordingly. */
20072 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20073 it->glyph_row->reversed_p = row->reversed_p;
20074 it->start = row->end;
20075 return row->displays_text_p;
20076
20077 #undef RECORD_MAX_MIN_POS
20078 }
20079
20080 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20081 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20082 doc: /* Return paragraph direction at point in BUFFER.
20083 Value is either `left-to-right' or `right-to-left'.
20084 If BUFFER is omitted or nil, it defaults to the current buffer.
20085
20086 Paragraph direction determines how the text in the paragraph is displayed.
20087 In left-to-right paragraphs, text begins at the left margin of the window
20088 and the reading direction is generally left to right. In right-to-left
20089 paragraphs, text begins at the right margin and is read from right to left.
20090
20091 See also `bidi-paragraph-direction'. */)
20092 (Lisp_Object buffer)
20093 {
20094 struct buffer *buf = current_buffer;
20095 struct buffer *old = buf;
20096
20097 if (! NILP (buffer))
20098 {
20099 CHECK_BUFFER (buffer);
20100 buf = XBUFFER (buffer);
20101 }
20102
20103 if (NILP (BVAR (buf, bidi_display_reordering))
20104 || NILP (BVAR (buf, enable_multibyte_characters))
20105 /* When we are loading loadup.el, the character property tables
20106 needed for bidi iteration are not yet available. */
20107 || !NILP (Vpurify_flag))
20108 return Qleft_to_right;
20109 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20110 return BVAR (buf, bidi_paragraph_direction);
20111 else
20112 {
20113 /* Determine the direction from buffer text. We could try to
20114 use current_matrix if it is up to date, but this seems fast
20115 enough as it is. */
20116 struct bidi_it itb;
20117 ptrdiff_t pos = BUF_PT (buf);
20118 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20119 int c;
20120 void *itb_data = bidi_shelve_cache ();
20121
20122 set_buffer_temp (buf);
20123 /* bidi_paragraph_init finds the base direction of the paragraph
20124 by searching forward from paragraph start. We need the base
20125 direction of the current or _previous_ paragraph, so we need
20126 to make sure we are within that paragraph. To that end, find
20127 the previous non-empty line. */
20128 if (pos >= ZV && pos > BEGV)
20129 {
20130 pos--;
20131 bytepos = CHAR_TO_BYTE (pos);
20132 }
20133 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20134 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20135 {
20136 while ((c = FETCH_BYTE (bytepos)) == '\n'
20137 || c == ' ' || c == '\t' || c == '\f')
20138 {
20139 if (bytepos <= BEGV_BYTE)
20140 break;
20141 bytepos--;
20142 pos--;
20143 }
20144 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20145 bytepos--;
20146 }
20147 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20148 itb.paragraph_dir = NEUTRAL_DIR;
20149 itb.string.s = NULL;
20150 itb.string.lstring = Qnil;
20151 itb.string.bufpos = 0;
20152 itb.string.unibyte = 0;
20153 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20154 bidi_unshelve_cache (itb_data, 0);
20155 set_buffer_temp (old);
20156 switch (itb.paragraph_dir)
20157 {
20158 case L2R:
20159 return Qleft_to_right;
20160 break;
20161 case R2L:
20162 return Qright_to_left;
20163 break;
20164 default:
20165 emacs_abort ();
20166 }
20167 }
20168 }
20169
20170
20171 \f
20172 /***********************************************************************
20173 Menu Bar
20174 ***********************************************************************/
20175
20176 /* Redisplay the menu bar in the frame for window W.
20177
20178 The menu bar of X frames that don't have X toolkit support is
20179 displayed in a special window W->frame->menu_bar_window.
20180
20181 The menu bar of terminal frames is treated specially as far as
20182 glyph matrices are concerned. Menu bar lines are not part of
20183 windows, so the update is done directly on the frame matrix rows
20184 for the menu bar. */
20185
20186 static void
20187 display_menu_bar (struct window *w)
20188 {
20189 struct frame *f = XFRAME (WINDOW_FRAME (w));
20190 struct it it;
20191 Lisp_Object items;
20192 int i;
20193
20194 /* Don't do all this for graphical frames. */
20195 #ifdef HAVE_NTGUI
20196 if (FRAME_W32_P (f))
20197 return;
20198 #endif
20199 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20200 if (FRAME_X_P (f))
20201 return;
20202 #endif
20203
20204 #ifdef HAVE_NS
20205 if (FRAME_NS_P (f))
20206 return;
20207 #endif /* HAVE_NS */
20208
20209 #ifdef USE_X_TOOLKIT
20210 eassert (!FRAME_WINDOW_P (f));
20211 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20212 it.first_visible_x = 0;
20213 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20214 #else /* not USE_X_TOOLKIT */
20215 if (FRAME_WINDOW_P (f))
20216 {
20217 /* Menu bar lines are displayed in the desired matrix of the
20218 dummy window menu_bar_window. */
20219 struct window *menu_w;
20220 eassert (WINDOWP (f->menu_bar_window));
20221 menu_w = XWINDOW (f->menu_bar_window);
20222 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20223 MENU_FACE_ID);
20224 it.first_visible_x = 0;
20225 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20226 }
20227 else
20228 {
20229 /* This is a TTY frame, i.e. character hpos/vpos are used as
20230 pixel x/y. */
20231 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20232 MENU_FACE_ID);
20233 it.first_visible_x = 0;
20234 it.last_visible_x = FRAME_COLS (f);
20235 }
20236 #endif /* not USE_X_TOOLKIT */
20237
20238 /* FIXME: This should be controlled by a user option. See the
20239 comments in redisplay_tool_bar and display_mode_line about
20240 this. */
20241 it.paragraph_embedding = L2R;
20242
20243 /* Clear all rows of the menu bar. */
20244 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20245 {
20246 struct glyph_row *row = it.glyph_row + i;
20247 clear_glyph_row (row);
20248 row->enabled_p = 1;
20249 row->full_width_p = 1;
20250 }
20251
20252 /* Display all items of the menu bar. */
20253 items = FRAME_MENU_BAR_ITEMS (it.f);
20254 for (i = 0; i < ASIZE (items); i += 4)
20255 {
20256 Lisp_Object string;
20257
20258 /* Stop at nil string. */
20259 string = AREF (items, i + 1);
20260 if (NILP (string))
20261 break;
20262
20263 /* Remember where item was displayed. */
20264 ASET (items, i + 3, make_number (it.hpos));
20265
20266 /* Display the item, pad with one space. */
20267 if (it.current_x < it.last_visible_x)
20268 display_string (NULL, string, Qnil, 0, 0, &it,
20269 SCHARS (string) + 1, 0, 0, -1);
20270 }
20271
20272 /* Fill out the line with spaces. */
20273 if (it.current_x < it.last_visible_x)
20274 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20275
20276 /* Compute the total height of the lines. */
20277 compute_line_metrics (&it);
20278 }
20279
20280
20281 \f
20282 /***********************************************************************
20283 Mode Line
20284 ***********************************************************************/
20285
20286 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20287 FORCE is non-zero, redisplay mode lines unconditionally.
20288 Otherwise, redisplay only mode lines that are garbaged. Value is
20289 the number of windows whose mode lines were redisplayed. */
20290
20291 static int
20292 redisplay_mode_lines (Lisp_Object window, int force)
20293 {
20294 int nwindows = 0;
20295
20296 while (!NILP (window))
20297 {
20298 struct window *w = XWINDOW (window);
20299
20300 if (WINDOWP (w->hchild))
20301 nwindows += redisplay_mode_lines (w->hchild, force);
20302 else if (WINDOWP (w->vchild))
20303 nwindows += redisplay_mode_lines (w->vchild, force);
20304 else if (force
20305 || FRAME_GARBAGED_P (XFRAME (w->frame))
20306 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20307 {
20308 struct text_pos lpoint;
20309 struct buffer *old = current_buffer;
20310
20311 /* Set the window's buffer for the mode line display. */
20312 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20313 set_buffer_internal_1 (XBUFFER (w->buffer));
20314
20315 /* Point refers normally to the selected window. For any
20316 other window, set up appropriate value. */
20317 if (!EQ (window, selected_window))
20318 {
20319 struct text_pos pt;
20320
20321 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20322 if (CHARPOS (pt) < BEGV)
20323 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20324 else if (CHARPOS (pt) > (ZV - 1))
20325 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20326 else
20327 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20328 }
20329
20330 /* Display mode lines. */
20331 clear_glyph_matrix (w->desired_matrix);
20332 if (display_mode_lines (w))
20333 {
20334 ++nwindows;
20335 w->must_be_updated_p = 1;
20336 }
20337
20338 /* Restore old settings. */
20339 set_buffer_internal_1 (old);
20340 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20341 }
20342
20343 window = w->next;
20344 }
20345
20346 return nwindows;
20347 }
20348
20349
20350 /* Display the mode and/or header line of window W. Value is the
20351 sum number of mode lines and header lines displayed. */
20352
20353 static int
20354 display_mode_lines (struct window *w)
20355 {
20356 Lisp_Object old_selected_window, old_selected_frame;
20357 int n = 0;
20358
20359 old_selected_frame = selected_frame;
20360 selected_frame = w->frame;
20361 old_selected_window = selected_window;
20362 XSETWINDOW (selected_window, w);
20363
20364 /* These will be set while the mode line specs are processed. */
20365 line_number_displayed = 0;
20366 wset_column_number_displayed (w, Qnil);
20367
20368 if (WINDOW_WANTS_MODELINE_P (w))
20369 {
20370 struct window *sel_w = XWINDOW (old_selected_window);
20371
20372 /* Select mode line face based on the real selected window. */
20373 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20374 BVAR (current_buffer, mode_line_format));
20375 ++n;
20376 }
20377
20378 if (WINDOW_WANTS_HEADER_LINE_P (w))
20379 {
20380 display_mode_line (w, HEADER_LINE_FACE_ID,
20381 BVAR (current_buffer, header_line_format));
20382 ++n;
20383 }
20384
20385 selected_frame = old_selected_frame;
20386 selected_window = old_selected_window;
20387 return n;
20388 }
20389
20390
20391 /* Display mode or header line of window W. FACE_ID specifies which
20392 line to display; it is either MODE_LINE_FACE_ID or
20393 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20394 display. Value is the pixel height of the mode/header line
20395 displayed. */
20396
20397 static int
20398 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20399 {
20400 struct it it;
20401 struct face *face;
20402 ptrdiff_t count = SPECPDL_INDEX ();
20403
20404 init_iterator (&it, w, -1, -1, NULL, face_id);
20405 /* Don't extend on a previously drawn mode-line.
20406 This may happen if called from pos_visible_p. */
20407 it.glyph_row->enabled_p = 0;
20408 prepare_desired_row (it.glyph_row);
20409
20410 it.glyph_row->mode_line_p = 1;
20411
20412 /* FIXME: This should be controlled by a user option. But
20413 supporting such an option is not trivial, since the mode line is
20414 made up of many separate strings. */
20415 it.paragraph_embedding = L2R;
20416
20417 record_unwind_protect (unwind_format_mode_line,
20418 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20419
20420 mode_line_target = MODE_LINE_DISPLAY;
20421
20422 /* Temporarily make frame's keyboard the current kboard so that
20423 kboard-local variables in the mode_line_format will get the right
20424 values. */
20425 push_kboard (FRAME_KBOARD (it.f));
20426 record_unwind_save_match_data ();
20427 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20428 pop_kboard ();
20429
20430 unbind_to (count, Qnil);
20431
20432 /* Fill up with spaces. */
20433 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20434
20435 compute_line_metrics (&it);
20436 it.glyph_row->full_width_p = 1;
20437 it.glyph_row->continued_p = 0;
20438 it.glyph_row->truncated_on_left_p = 0;
20439 it.glyph_row->truncated_on_right_p = 0;
20440
20441 /* Make a 3D mode-line have a shadow at its right end. */
20442 face = FACE_FROM_ID (it.f, face_id);
20443 extend_face_to_end_of_line (&it);
20444 if (face->box != FACE_NO_BOX)
20445 {
20446 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20447 + it.glyph_row->used[TEXT_AREA] - 1);
20448 last->right_box_line_p = 1;
20449 }
20450
20451 return it.glyph_row->height;
20452 }
20453
20454 /* Move element ELT in LIST to the front of LIST.
20455 Return the updated list. */
20456
20457 static Lisp_Object
20458 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20459 {
20460 register Lisp_Object tail, prev;
20461 register Lisp_Object tem;
20462
20463 tail = list;
20464 prev = Qnil;
20465 while (CONSP (tail))
20466 {
20467 tem = XCAR (tail);
20468
20469 if (EQ (elt, tem))
20470 {
20471 /* Splice out the link TAIL. */
20472 if (NILP (prev))
20473 list = XCDR (tail);
20474 else
20475 Fsetcdr (prev, XCDR (tail));
20476
20477 /* Now make it the first. */
20478 Fsetcdr (tail, list);
20479 return tail;
20480 }
20481 else
20482 prev = tail;
20483 tail = XCDR (tail);
20484 QUIT;
20485 }
20486
20487 /* Not found--return unchanged LIST. */
20488 return list;
20489 }
20490
20491 /* Contribute ELT to the mode line for window IT->w. How it
20492 translates into text depends on its data type.
20493
20494 IT describes the display environment in which we display, as usual.
20495
20496 DEPTH is the depth in recursion. It is used to prevent
20497 infinite recursion here.
20498
20499 FIELD_WIDTH is the number of characters the display of ELT should
20500 occupy in the mode line, and PRECISION is the maximum number of
20501 characters to display from ELT's representation. See
20502 display_string for details.
20503
20504 Returns the hpos of the end of the text generated by ELT.
20505
20506 PROPS is a property list to add to any string we encounter.
20507
20508 If RISKY is nonzero, remove (disregard) any properties in any string
20509 we encounter, and ignore :eval and :propertize.
20510
20511 The global variable `mode_line_target' determines whether the
20512 output is passed to `store_mode_line_noprop',
20513 `store_mode_line_string', or `display_string'. */
20514
20515 static int
20516 display_mode_element (struct it *it, int depth, int field_width, int precision,
20517 Lisp_Object elt, Lisp_Object props, int risky)
20518 {
20519 int n = 0, field, prec;
20520 int literal = 0;
20521
20522 tail_recurse:
20523 if (depth > 100)
20524 elt = build_string ("*too-deep*");
20525
20526 depth++;
20527
20528 switch (XTYPE (elt))
20529 {
20530 case Lisp_String:
20531 {
20532 /* A string: output it and check for %-constructs within it. */
20533 unsigned char c;
20534 ptrdiff_t offset = 0;
20535
20536 if (SCHARS (elt) > 0
20537 && (!NILP (props) || risky))
20538 {
20539 Lisp_Object oprops, aelt;
20540 oprops = Ftext_properties_at (make_number (0), elt);
20541
20542 /* If the starting string's properties are not what
20543 we want, translate the string. Also, if the string
20544 is risky, do that anyway. */
20545
20546 if (NILP (Fequal (props, oprops)) || risky)
20547 {
20548 /* If the starting string has properties,
20549 merge the specified ones onto the existing ones. */
20550 if (! NILP (oprops) && !risky)
20551 {
20552 Lisp_Object tem;
20553
20554 oprops = Fcopy_sequence (oprops);
20555 tem = props;
20556 while (CONSP (tem))
20557 {
20558 oprops = Fplist_put (oprops, XCAR (tem),
20559 XCAR (XCDR (tem)));
20560 tem = XCDR (XCDR (tem));
20561 }
20562 props = oprops;
20563 }
20564
20565 aelt = Fassoc (elt, mode_line_proptrans_alist);
20566 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20567 {
20568 /* AELT is what we want. Move it to the front
20569 without consing. */
20570 elt = XCAR (aelt);
20571 mode_line_proptrans_alist
20572 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20573 }
20574 else
20575 {
20576 Lisp_Object tem;
20577
20578 /* If AELT has the wrong props, it is useless.
20579 so get rid of it. */
20580 if (! NILP (aelt))
20581 mode_line_proptrans_alist
20582 = Fdelq (aelt, mode_line_proptrans_alist);
20583
20584 elt = Fcopy_sequence (elt);
20585 Fset_text_properties (make_number (0), Flength (elt),
20586 props, elt);
20587 /* Add this item to mode_line_proptrans_alist. */
20588 mode_line_proptrans_alist
20589 = Fcons (Fcons (elt, props),
20590 mode_line_proptrans_alist);
20591 /* Truncate mode_line_proptrans_alist
20592 to at most 50 elements. */
20593 tem = Fnthcdr (make_number (50),
20594 mode_line_proptrans_alist);
20595 if (! NILP (tem))
20596 XSETCDR (tem, Qnil);
20597 }
20598 }
20599 }
20600
20601 offset = 0;
20602
20603 if (literal)
20604 {
20605 prec = precision - n;
20606 switch (mode_line_target)
20607 {
20608 case MODE_LINE_NOPROP:
20609 case MODE_LINE_TITLE:
20610 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20611 break;
20612 case MODE_LINE_STRING:
20613 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20614 break;
20615 case MODE_LINE_DISPLAY:
20616 n += display_string (NULL, elt, Qnil, 0, 0, it,
20617 0, prec, 0, STRING_MULTIBYTE (elt));
20618 break;
20619 }
20620
20621 break;
20622 }
20623
20624 /* Handle the non-literal case. */
20625
20626 while ((precision <= 0 || n < precision)
20627 && SREF (elt, offset) != 0
20628 && (mode_line_target != MODE_LINE_DISPLAY
20629 || it->current_x < it->last_visible_x))
20630 {
20631 ptrdiff_t last_offset = offset;
20632
20633 /* Advance to end of string or next format specifier. */
20634 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20635 ;
20636
20637 if (offset - 1 != last_offset)
20638 {
20639 ptrdiff_t nchars, nbytes;
20640
20641 /* Output to end of string or up to '%'. Field width
20642 is length of string. Don't output more than
20643 PRECISION allows us. */
20644 offset--;
20645
20646 prec = c_string_width (SDATA (elt) + last_offset,
20647 offset - last_offset, precision - n,
20648 &nchars, &nbytes);
20649
20650 switch (mode_line_target)
20651 {
20652 case MODE_LINE_NOPROP:
20653 case MODE_LINE_TITLE:
20654 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20655 break;
20656 case MODE_LINE_STRING:
20657 {
20658 ptrdiff_t bytepos = last_offset;
20659 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20660 ptrdiff_t endpos = (precision <= 0
20661 ? string_byte_to_char (elt, offset)
20662 : charpos + nchars);
20663
20664 n += store_mode_line_string (NULL,
20665 Fsubstring (elt, make_number (charpos),
20666 make_number (endpos)),
20667 0, 0, 0, Qnil);
20668 }
20669 break;
20670 case MODE_LINE_DISPLAY:
20671 {
20672 ptrdiff_t bytepos = last_offset;
20673 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20674
20675 if (precision <= 0)
20676 nchars = string_byte_to_char (elt, offset) - charpos;
20677 n += display_string (NULL, elt, Qnil, 0, charpos,
20678 it, 0, nchars, 0,
20679 STRING_MULTIBYTE (elt));
20680 }
20681 break;
20682 }
20683 }
20684 else /* c == '%' */
20685 {
20686 ptrdiff_t percent_position = offset;
20687
20688 /* Get the specified minimum width. Zero means
20689 don't pad. */
20690 field = 0;
20691 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20692 field = field * 10 + c - '0';
20693
20694 /* Don't pad beyond the total padding allowed. */
20695 if (field_width - n > 0 && field > field_width - n)
20696 field = field_width - n;
20697
20698 /* Note that either PRECISION <= 0 or N < PRECISION. */
20699 prec = precision - n;
20700
20701 if (c == 'M')
20702 n += display_mode_element (it, depth, field, prec,
20703 Vglobal_mode_string, props,
20704 risky);
20705 else if (c != 0)
20706 {
20707 int multibyte;
20708 ptrdiff_t bytepos, charpos;
20709 const char *spec;
20710 Lisp_Object string;
20711
20712 bytepos = percent_position;
20713 charpos = (STRING_MULTIBYTE (elt)
20714 ? string_byte_to_char (elt, bytepos)
20715 : bytepos);
20716 spec = decode_mode_spec (it->w, c, field, &string);
20717 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20718
20719 switch (mode_line_target)
20720 {
20721 case MODE_LINE_NOPROP:
20722 case MODE_LINE_TITLE:
20723 n += store_mode_line_noprop (spec, field, prec);
20724 break;
20725 case MODE_LINE_STRING:
20726 {
20727 Lisp_Object tem = build_string (spec);
20728 props = Ftext_properties_at (make_number (charpos), elt);
20729 /* Should only keep face property in props */
20730 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20731 }
20732 break;
20733 case MODE_LINE_DISPLAY:
20734 {
20735 int nglyphs_before, nwritten;
20736
20737 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20738 nwritten = display_string (spec, string, elt,
20739 charpos, 0, it,
20740 field, prec, 0,
20741 multibyte);
20742
20743 /* Assign to the glyphs written above the
20744 string where the `%x' came from, position
20745 of the `%'. */
20746 if (nwritten > 0)
20747 {
20748 struct glyph *glyph
20749 = (it->glyph_row->glyphs[TEXT_AREA]
20750 + nglyphs_before);
20751 int i;
20752
20753 for (i = 0; i < nwritten; ++i)
20754 {
20755 glyph[i].object = elt;
20756 glyph[i].charpos = charpos;
20757 }
20758
20759 n += nwritten;
20760 }
20761 }
20762 break;
20763 }
20764 }
20765 else /* c == 0 */
20766 break;
20767 }
20768 }
20769 }
20770 break;
20771
20772 case Lisp_Symbol:
20773 /* A symbol: process the value of the symbol recursively
20774 as if it appeared here directly. Avoid error if symbol void.
20775 Special case: if value of symbol is a string, output the string
20776 literally. */
20777 {
20778 register Lisp_Object tem;
20779
20780 /* If the variable is not marked as risky to set
20781 then its contents are risky to use. */
20782 if (NILP (Fget (elt, Qrisky_local_variable)))
20783 risky = 1;
20784
20785 tem = Fboundp (elt);
20786 if (!NILP (tem))
20787 {
20788 tem = Fsymbol_value (elt);
20789 /* If value is a string, output that string literally:
20790 don't check for % within it. */
20791 if (STRINGP (tem))
20792 literal = 1;
20793
20794 if (!EQ (tem, elt))
20795 {
20796 /* Give up right away for nil or t. */
20797 elt = tem;
20798 goto tail_recurse;
20799 }
20800 }
20801 }
20802 break;
20803
20804 case Lisp_Cons:
20805 {
20806 register Lisp_Object car, tem;
20807
20808 /* A cons cell: five distinct cases.
20809 If first element is :eval or :propertize, do something special.
20810 If first element is a string or a cons, process all the elements
20811 and effectively concatenate them.
20812 If first element is a negative number, truncate displaying cdr to
20813 at most that many characters. If positive, pad (with spaces)
20814 to at least that many characters.
20815 If first element is a symbol, process the cadr or caddr recursively
20816 according to whether the symbol's value is non-nil or nil. */
20817 car = XCAR (elt);
20818 if (EQ (car, QCeval))
20819 {
20820 /* An element of the form (:eval FORM) means evaluate FORM
20821 and use the result as mode line elements. */
20822
20823 if (risky)
20824 break;
20825
20826 if (CONSP (XCDR (elt)))
20827 {
20828 Lisp_Object spec;
20829 spec = safe_eval (XCAR (XCDR (elt)));
20830 n += display_mode_element (it, depth, field_width - n,
20831 precision - n, spec, props,
20832 risky);
20833 }
20834 }
20835 else if (EQ (car, QCpropertize))
20836 {
20837 /* An element of the form (:propertize ELT PROPS...)
20838 means display ELT but applying properties PROPS. */
20839
20840 if (risky)
20841 break;
20842
20843 if (CONSP (XCDR (elt)))
20844 n += display_mode_element (it, depth, field_width - n,
20845 precision - n, XCAR (XCDR (elt)),
20846 XCDR (XCDR (elt)), risky);
20847 }
20848 else if (SYMBOLP (car))
20849 {
20850 tem = Fboundp (car);
20851 elt = XCDR (elt);
20852 if (!CONSP (elt))
20853 goto invalid;
20854 /* elt is now the cdr, and we know it is a cons cell.
20855 Use its car if CAR has a non-nil value. */
20856 if (!NILP (tem))
20857 {
20858 tem = Fsymbol_value (car);
20859 if (!NILP (tem))
20860 {
20861 elt = XCAR (elt);
20862 goto tail_recurse;
20863 }
20864 }
20865 /* Symbol's value is nil (or symbol is unbound)
20866 Get the cddr of the original list
20867 and if possible find the caddr and use that. */
20868 elt = XCDR (elt);
20869 if (NILP (elt))
20870 break;
20871 else if (!CONSP (elt))
20872 goto invalid;
20873 elt = XCAR (elt);
20874 goto tail_recurse;
20875 }
20876 else if (INTEGERP (car))
20877 {
20878 register int lim = XINT (car);
20879 elt = XCDR (elt);
20880 if (lim < 0)
20881 {
20882 /* Negative int means reduce maximum width. */
20883 if (precision <= 0)
20884 precision = -lim;
20885 else
20886 precision = min (precision, -lim);
20887 }
20888 else if (lim > 0)
20889 {
20890 /* Padding specified. Don't let it be more than
20891 current maximum. */
20892 if (precision > 0)
20893 lim = min (precision, lim);
20894
20895 /* If that's more padding than already wanted, queue it.
20896 But don't reduce padding already specified even if
20897 that is beyond the current truncation point. */
20898 field_width = max (lim, field_width);
20899 }
20900 goto tail_recurse;
20901 }
20902 else if (STRINGP (car) || CONSP (car))
20903 {
20904 Lisp_Object halftail = elt;
20905 int len = 0;
20906
20907 while (CONSP (elt)
20908 && (precision <= 0 || n < precision))
20909 {
20910 n += display_mode_element (it, depth,
20911 /* Do padding only after the last
20912 element in the list. */
20913 (! CONSP (XCDR (elt))
20914 ? field_width - n
20915 : 0),
20916 precision - n, XCAR (elt),
20917 props, risky);
20918 elt = XCDR (elt);
20919 len++;
20920 if ((len & 1) == 0)
20921 halftail = XCDR (halftail);
20922 /* Check for cycle. */
20923 if (EQ (halftail, elt))
20924 break;
20925 }
20926 }
20927 }
20928 break;
20929
20930 default:
20931 invalid:
20932 elt = build_string ("*invalid*");
20933 goto tail_recurse;
20934 }
20935
20936 /* Pad to FIELD_WIDTH. */
20937 if (field_width > 0 && n < field_width)
20938 {
20939 switch (mode_line_target)
20940 {
20941 case MODE_LINE_NOPROP:
20942 case MODE_LINE_TITLE:
20943 n += store_mode_line_noprop ("", field_width - n, 0);
20944 break;
20945 case MODE_LINE_STRING:
20946 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20947 break;
20948 case MODE_LINE_DISPLAY:
20949 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20950 0, 0, 0);
20951 break;
20952 }
20953 }
20954
20955 return n;
20956 }
20957
20958 /* Store a mode-line string element in mode_line_string_list.
20959
20960 If STRING is non-null, display that C string. Otherwise, the Lisp
20961 string LISP_STRING is displayed.
20962
20963 FIELD_WIDTH is the minimum number of output glyphs to produce.
20964 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20965 with spaces. FIELD_WIDTH <= 0 means don't pad.
20966
20967 PRECISION is the maximum number of characters to output from
20968 STRING. PRECISION <= 0 means don't truncate the string.
20969
20970 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20971 properties to the string.
20972
20973 PROPS are the properties to add to the string.
20974 The mode_line_string_face face property is always added to the string.
20975 */
20976
20977 static int
20978 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20979 int field_width, int precision, Lisp_Object props)
20980 {
20981 ptrdiff_t len;
20982 int n = 0;
20983
20984 if (string != NULL)
20985 {
20986 len = strlen (string);
20987 if (precision > 0 && len > precision)
20988 len = precision;
20989 lisp_string = make_string (string, len);
20990 if (NILP (props))
20991 props = mode_line_string_face_prop;
20992 else if (!NILP (mode_line_string_face))
20993 {
20994 Lisp_Object face = Fplist_get (props, Qface);
20995 props = Fcopy_sequence (props);
20996 if (NILP (face))
20997 face = mode_line_string_face;
20998 else
20999 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21000 props = Fplist_put (props, Qface, face);
21001 }
21002 Fadd_text_properties (make_number (0), make_number (len),
21003 props, lisp_string);
21004 }
21005 else
21006 {
21007 len = XFASTINT (Flength (lisp_string));
21008 if (precision > 0 && len > precision)
21009 {
21010 len = precision;
21011 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21012 precision = -1;
21013 }
21014 if (!NILP (mode_line_string_face))
21015 {
21016 Lisp_Object face;
21017 if (NILP (props))
21018 props = Ftext_properties_at (make_number (0), lisp_string);
21019 face = Fplist_get (props, Qface);
21020 if (NILP (face))
21021 face = mode_line_string_face;
21022 else
21023 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21024 props = Fcons (Qface, Fcons (face, Qnil));
21025 if (copy_string)
21026 lisp_string = Fcopy_sequence (lisp_string);
21027 }
21028 if (!NILP (props))
21029 Fadd_text_properties (make_number (0), make_number (len),
21030 props, lisp_string);
21031 }
21032
21033 if (len > 0)
21034 {
21035 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21036 n += len;
21037 }
21038
21039 if (field_width > len)
21040 {
21041 field_width -= len;
21042 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21043 if (!NILP (props))
21044 Fadd_text_properties (make_number (0), make_number (field_width),
21045 props, lisp_string);
21046 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21047 n += field_width;
21048 }
21049
21050 return n;
21051 }
21052
21053
21054 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21055 1, 4, 0,
21056 doc: /* Format a string out of a mode line format specification.
21057 First arg FORMAT specifies the mode line format (see `mode-line-format'
21058 for details) to use.
21059
21060 By default, the format is evaluated for the currently selected window.
21061
21062 Optional second arg FACE specifies the face property to put on all
21063 characters for which no face is specified. The value nil means the
21064 default face. The value t means whatever face the window's mode line
21065 currently uses (either `mode-line' or `mode-line-inactive',
21066 depending on whether the window is the selected window or not).
21067 An integer value means the value string has no text
21068 properties.
21069
21070 Optional third and fourth args WINDOW and BUFFER specify the window
21071 and buffer to use as the context for the formatting (defaults
21072 are the selected window and the WINDOW's buffer). */)
21073 (Lisp_Object format, Lisp_Object face,
21074 Lisp_Object window, Lisp_Object buffer)
21075 {
21076 struct it it;
21077 int len;
21078 struct window *w;
21079 struct buffer *old_buffer = NULL;
21080 int face_id;
21081 int no_props = INTEGERP (face);
21082 ptrdiff_t count = SPECPDL_INDEX ();
21083 Lisp_Object str;
21084 int string_start = 0;
21085
21086 w = decode_any_window (window);
21087 XSETWINDOW (window, w);
21088
21089 if (NILP (buffer))
21090 buffer = w->buffer;
21091 CHECK_BUFFER (buffer);
21092
21093 /* Make formatting the modeline a non-op when noninteractive, otherwise
21094 there will be problems later caused by a partially initialized frame. */
21095 if (NILP (format) || noninteractive)
21096 return empty_unibyte_string;
21097
21098 if (no_props)
21099 face = Qnil;
21100
21101 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21102 : EQ (face, Qt) ? (EQ (window, selected_window)
21103 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21104 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21105 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21106 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21107 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21108 : DEFAULT_FACE_ID;
21109
21110 old_buffer = current_buffer;
21111
21112 /* Save things including mode_line_proptrans_alist,
21113 and set that to nil so that we don't alter the outer value. */
21114 record_unwind_protect (unwind_format_mode_line,
21115 format_mode_line_unwind_data
21116 (XFRAME (WINDOW_FRAME (w)),
21117 old_buffer, selected_window, 1));
21118 mode_line_proptrans_alist = Qnil;
21119
21120 Fselect_window (window, Qt);
21121 set_buffer_internal_1 (XBUFFER (buffer));
21122
21123 init_iterator (&it, w, -1, -1, NULL, face_id);
21124
21125 if (no_props)
21126 {
21127 mode_line_target = MODE_LINE_NOPROP;
21128 mode_line_string_face_prop = Qnil;
21129 mode_line_string_list = Qnil;
21130 string_start = MODE_LINE_NOPROP_LEN (0);
21131 }
21132 else
21133 {
21134 mode_line_target = MODE_LINE_STRING;
21135 mode_line_string_list = Qnil;
21136 mode_line_string_face = face;
21137 mode_line_string_face_prop
21138 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21139 }
21140
21141 push_kboard (FRAME_KBOARD (it.f));
21142 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21143 pop_kboard ();
21144
21145 if (no_props)
21146 {
21147 len = MODE_LINE_NOPROP_LEN (string_start);
21148 str = make_string (mode_line_noprop_buf + string_start, len);
21149 }
21150 else
21151 {
21152 mode_line_string_list = Fnreverse (mode_line_string_list);
21153 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21154 empty_unibyte_string);
21155 }
21156
21157 unbind_to (count, Qnil);
21158 return str;
21159 }
21160
21161 /* Write a null-terminated, right justified decimal representation of
21162 the positive integer D to BUF using a minimal field width WIDTH. */
21163
21164 static void
21165 pint2str (register char *buf, register int width, register ptrdiff_t d)
21166 {
21167 register char *p = buf;
21168
21169 if (d <= 0)
21170 *p++ = '0';
21171 else
21172 {
21173 while (d > 0)
21174 {
21175 *p++ = d % 10 + '0';
21176 d /= 10;
21177 }
21178 }
21179
21180 for (width -= (int) (p - buf); width > 0; --width)
21181 *p++ = ' ';
21182 *p-- = '\0';
21183 while (p > buf)
21184 {
21185 d = *buf;
21186 *buf++ = *p;
21187 *p-- = d;
21188 }
21189 }
21190
21191 /* Write a null-terminated, right justified decimal and "human
21192 readable" representation of the nonnegative integer D to BUF using
21193 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21194
21195 static const char power_letter[] =
21196 {
21197 0, /* no letter */
21198 'k', /* kilo */
21199 'M', /* mega */
21200 'G', /* giga */
21201 'T', /* tera */
21202 'P', /* peta */
21203 'E', /* exa */
21204 'Z', /* zetta */
21205 'Y' /* yotta */
21206 };
21207
21208 static void
21209 pint2hrstr (char *buf, int width, ptrdiff_t d)
21210 {
21211 /* We aim to represent the nonnegative integer D as
21212 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21213 ptrdiff_t quotient = d;
21214 int remainder = 0;
21215 /* -1 means: do not use TENTHS. */
21216 int tenths = -1;
21217 int exponent = 0;
21218
21219 /* Length of QUOTIENT.TENTHS as a string. */
21220 int length;
21221
21222 char * psuffix;
21223 char * p;
21224
21225 if (1000 <= quotient)
21226 {
21227 /* Scale to the appropriate EXPONENT. */
21228 do
21229 {
21230 remainder = quotient % 1000;
21231 quotient /= 1000;
21232 exponent++;
21233 }
21234 while (1000 <= quotient);
21235
21236 /* Round to nearest and decide whether to use TENTHS or not. */
21237 if (quotient <= 9)
21238 {
21239 tenths = remainder / 100;
21240 if (50 <= remainder % 100)
21241 {
21242 if (tenths < 9)
21243 tenths++;
21244 else
21245 {
21246 quotient++;
21247 if (quotient == 10)
21248 tenths = -1;
21249 else
21250 tenths = 0;
21251 }
21252 }
21253 }
21254 else
21255 if (500 <= remainder)
21256 {
21257 if (quotient < 999)
21258 quotient++;
21259 else
21260 {
21261 quotient = 1;
21262 exponent++;
21263 tenths = 0;
21264 }
21265 }
21266 }
21267
21268 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21269 if (tenths == -1 && quotient <= 99)
21270 if (quotient <= 9)
21271 length = 1;
21272 else
21273 length = 2;
21274 else
21275 length = 3;
21276 p = psuffix = buf + max (width, length);
21277
21278 /* Print EXPONENT. */
21279 *psuffix++ = power_letter[exponent];
21280 *psuffix = '\0';
21281
21282 /* Print TENTHS. */
21283 if (tenths >= 0)
21284 {
21285 *--p = '0' + tenths;
21286 *--p = '.';
21287 }
21288
21289 /* Print QUOTIENT. */
21290 do
21291 {
21292 int digit = quotient % 10;
21293 *--p = '0' + digit;
21294 }
21295 while ((quotient /= 10) != 0);
21296
21297 /* Print leading spaces. */
21298 while (buf < p)
21299 *--p = ' ';
21300 }
21301
21302 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21303 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21304 type of CODING_SYSTEM. Return updated pointer into BUF. */
21305
21306 static unsigned char invalid_eol_type[] = "(*invalid*)";
21307
21308 static char *
21309 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21310 {
21311 Lisp_Object val;
21312 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21313 const unsigned char *eol_str;
21314 int eol_str_len;
21315 /* The EOL conversion we are using. */
21316 Lisp_Object eoltype;
21317
21318 val = CODING_SYSTEM_SPEC (coding_system);
21319 eoltype = Qnil;
21320
21321 if (!VECTORP (val)) /* Not yet decided. */
21322 {
21323 *buf++ = multibyte ? '-' : ' ';
21324 if (eol_flag)
21325 eoltype = eol_mnemonic_undecided;
21326 /* Don't mention EOL conversion if it isn't decided. */
21327 }
21328 else
21329 {
21330 Lisp_Object attrs;
21331 Lisp_Object eolvalue;
21332
21333 attrs = AREF (val, 0);
21334 eolvalue = AREF (val, 2);
21335
21336 *buf++ = multibyte
21337 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21338 : ' ';
21339
21340 if (eol_flag)
21341 {
21342 /* The EOL conversion that is normal on this system. */
21343
21344 if (NILP (eolvalue)) /* Not yet decided. */
21345 eoltype = eol_mnemonic_undecided;
21346 else if (VECTORP (eolvalue)) /* Not yet decided. */
21347 eoltype = eol_mnemonic_undecided;
21348 else /* eolvalue is Qunix, Qdos, or Qmac. */
21349 eoltype = (EQ (eolvalue, Qunix)
21350 ? eol_mnemonic_unix
21351 : (EQ (eolvalue, Qdos) == 1
21352 ? eol_mnemonic_dos : eol_mnemonic_mac));
21353 }
21354 }
21355
21356 if (eol_flag)
21357 {
21358 /* Mention the EOL conversion if it is not the usual one. */
21359 if (STRINGP (eoltype))
21360 {
21361 eol_str = SDATA (eoltype);
21362 eol_str_len = SBYTES (eoltype);
21363 }
21364 else if (CHARACTERP (eoltype))
21365 {
21366 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21367 int c = XFASTINT (eoltype);
21368 eol_str_len = CHAR_STRING (c, tmp);
21369 eol_str = tmp;
21370 }
21371 else
21372 {
21373 eol_str = invalid_eol_type;
21374 eol_str_len = sizeof (invalid_eol_type) - 1;
21375 }
21376 memcpy (buf, eol_str, eol_str_len);
21377 buf += eol_str_len;
21378 }
21379
21380 return buf;
21381 }
21382
21383 /* Return a string for the output of a mode line %-spec for window W,
21384 generated by character C. FIELD_WIDTH > 0 means pad the string
21385 returned with spaces to that value. Return a Lisp string in
21386 *STRING if the resulting string is taken from that Lisp string.
21387
21388 Note we operate on the current buffer for most purposes,
21389 the exception being w->base_line_pos. */
21390
21391 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21392
21393 static const char *
21394 decode_mode_spec (struct window *w, register int c, int field_width,
21395 Lisp_Object *string)
21396 {
21397 Lisp_Object obj;
21398 struct frame *f = XFRAME (WINDOW_FRAME (w));
21399 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21400 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21401 produce strings from numerical values, so limit preposterously
21402 large values of FIELD_WIDTH to avoid overrunning the buffer's
21403 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21404 bytes plus the terminating null. */
21405 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21406 struct buffer *b = current_buffer;
21407
21408 obj = Qnil;
21409 *string = Qnil;
21410
21411 switch (c)
21412 {
21413 case '*':
21414 if (!NILP (BVAR (b, read_only)))
21415 return "%";
21416 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21417 return "*";
21418 return "-";
21419
21420 case '+':
21421 /* This differs from %* only for a modified read-only buffer. */
21422 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21423 return "*";
21424 if (!NILP (BVAR (b, read_only)))
21425 return "%";
21426 return "-";
21427
21428 case '&':
21429 /* This differs from %* in ignoring read-only-ness. */
21430 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21431 return "*";
21432 return "-";
21433
21434 case '%':
21435 return "%";
21436
21437 case '[':
21438 {
21439 int i;
21440 char *p;
21441
21442 if (command_loop_level > 5)
21443 return "[[[... ";
21444 p = decode_mode_spec_buf;
21445 for (i = 0; i < command_loop_level; i++)
21446 *p++ = '[';
21447 *p = 0;
21448 return decode_mode_spec_buf;
21449 }
21450
21451 case ']':
21452 {
21453 int i;
21454 char *p;
21455
21456 if (command_loop_level > 5)
21457 return " ...]]]";
21458 p = decode_mode_spec_buf;
21459 for (i = 0; i < command_loop_level; i++)
21460 *p++ = ']';
21461 *p = 0;
21462 return decode_mode_spec_buf;
21463 }
21464
21465 case '-':
21466 {
21467 register int i;
21468
21469 /* Let lots_of_dashes be a string of infinite length. */
21470 if (mode_line_target == MODE_LINE_NOPROP
21471 || mode_line_target == MODE_LINE_STRING)
21472 return "--";
21473 if (field_width <= 0
21474 || field_width > sizeof (lots_of_dashes))
21475 {
21476 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21477 decode_mode_spec_buf[i] = '-';
21478 decode_mode_spec_buf[i] = '\0';
21479 return decode_mode_spec_buf;
21480 }
21481 else
21482 return lots_of_dashes;
21483 }
21484
21485 case 'b':
21486 obj = BVAR (b, name);
21487 break;
21488
21489 case 'c':
21490 /* %c and %l are ignored in `frame-title-format'.
21491 (In redisplay_internal, the frame title is drawn _before_ the
21492 windows are updated, so the stuff which depends on actual
21493 window contents (such as %l) may fail to render properly, or
21494 even crash emacs.) */
21495 if (mode_line_target == MODE_LINE_TITLE)
21496 return "";
21497 else
21498 {
21499 ptrdiff_t col = current_column ();
21500 wset_column_number_displayed (w, make_number (col));
21501 pint2str (decode_mode_spec_buf, width, col);
21502 return decode_mode_spec_buf;
21503 }
21504
21505 case 'e':
21506 #ifndef SYSTEM_MALLOC
21507 {
21508 if (NILP (Vmemory_full))
21509 return "";
21510 else
21511 return "!MEM FULL! ";
21512 }
21513 #else
21514 return "";
21515 #endif
21516
21517 case 'F':
21518 /* %F displays the frame name. */
21519 if (!NILP (f->title))
21520 return SSDATA (f->title);
21521 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21522 return SSDATA (f->name);
21523 return "Emacs";
21524
21525 case 'f':
21526 obj = BVAR (b, filename);
21527 break;
21528
21529 case 'i':
21530 {
21531 ptrdiff_t size = ZV - BEGV;
21532 pint2str (decode_mode_spec_buf, width, size);
21533 return decode_mode_spec_buf;
21534 }
21535
21536 case 'I':
21537 {
21538 ptrdiff_t size = ZV - BEGV;
21539 pint2hrstr (decode_mode_spec_buf, width, size);
21540 return decode_mode_spec_buf;
21541 }
21542
21543 case 'l':
21544 {
21545 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21546 ptrdiff_t topline, nlines, height;
21547 ptrdiff_t junk;
21548
21549 /* %c and %l are ignored in `frame-title-format'. */
21550 if (mode_line_target == MODE_LINE_TITLE)
21551 return "";
21552
21553 startpos = marker_position (w->start);
21554 startpos_byte = marker_byte_position (w->start);
21555 height = WINDOW_TOTAL_LINES (w);
21556
21557 /* If we decided that this buffer isn't suitable for line numbers,
21558 don't forget that too fast. */
21559 if (EQ (w->base_line_pos, w->buffer))
21560 goto no_value;
21561 /* But do forget it, if the window shows a different buffer now. */
21562 else if (BUFFERP (w->base_line_pos))
21563 wset_base_line_pos (w, Qnil);
21564
21565 /* If the buffer is very big, don't waste time. */
21566 if (INTEGERP (Vline_number_display_limit)
21567 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21568 {
21569 wset_base_line_pos (w, Qnil);
21570 wset_base_line_number (w, Qnil);
21571 goto no_value;
21572 }
21573
21574 if (INTEGERP (w->base_line_number)
21575 && INTEGERP (w->base_line_pos)
21576 && XFASTINT (w->base_line_pos) <= startpos)
21577 {
21578 line = XFASTINT (w->base_line_number);
21579 linepos = XFASTINT (w->base_line_pos);
21580 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21581 }
21582 else
21583 {
21584 line = 1;
21585 linepos = BUF_BEGV (b);
21586 linepos_byte = BUF_BEGV_BYTE (b);
21587 }
21588
21589 /* Count lines from base line to window start position. */
21590 nlines = display_count_lines (linepos_byte,
21591 startpos_byte,
21592 startpos, &junk);
21593
21594 topline = nlines + line;
21595
21596 /* Determine a new base line, if the old one is too close
21597 or too far away, or if we did not have one.
21598 "Too close" means it's plausible a scroll-down would
21599 go back past it. */
21600 if (startpos == BUF_BEGV (b))
21601 {
21602 wset_base_line_number (w, make_number (topline));
21603 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21604 }
21605 else if (nlines < height + 25 || nlines > height * 3 + 50
21606 || linepos == BUF_BEGV (b))
21607 {
21608 ptrdiff_t limit = BUF_BEGV (b);
21609 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21610 ptrdiff_t position;
21611 ptrdiff_t distance =
21612 (height * 2 + 30) * line_number_display_limit_width;
21613
21614 if (startpos - distance > limit)
21615 {
21616 limit = startpos - distance;
21617 limit_byte = CHAR_TO_BYTE (limit);
21618 }
21619
21620 nlines = display_count_lines (startpos_byte,
21621 limit_byte,
21622 - (height * 2 + 30),
21623 &position);
21624 /* If we couldn't find the lines we wanted within
21625 line_number_display_limit_width chars per line,
21626 give up on line numbers for this window. */
21627 if (position == limit_byte && limit == startpos - distance)
21628 {
21629 wset_base_line_pos (w, w->buffer);
21630 wset_base_line_number (w, Qnil);
21631 goto no_value;
21632 }
21633
21634 wset_base_line_number (w, make_number (topline - nlines));
21635 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21636 }
21637
21638 /* Now count lines from the start pos to point. */
21639 nlines = display_count_lines (startpos_byte,
21640 PT_BYTE, PT, &junk);
21641
21642 /* Record that we did display the line number. */
21643 line_number_displayed = 1;
21644
21645 /* Make the string to show. */
21646 pint2str (decode_mode_spec_buf, width, topline + nlines);
21647 return decode_mode_spec_buf;
21648 no_value:
21649 {
21650 char* p = decode_mode_spec_buf;
21651 int pad = width - 2;
21652 while (pad-- > 0)
21653 *p++ = ' ';
21654 *p++ = '?';
21655 *p++ = '?';
21656 *p = '\0';
21657 return decode_mode_spec_buf;
21658 }
21659 }
21660 break;
21661
21662 case 'm':
21663 obj = BVAR (b, mode_name);
21664 break;
21665
21666 case 'n':
21667 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21668 return " Narrow";
21669 break;
21670
21671 case 'p':
21672 {
21673 ptrdiff_t pos = marker_position (w->start);
21674 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21675
21676 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21677 {
21678 if (pos <= BUF_BEGV (b))
21679 return "All";
21680 else
21681 return "Bottom";
21682 }
21683 else if (pos <= BUF_BEGV (b))
21684 return "Top";
21685 else
21686 {
21687 if (total > 1000000)
21688 /* Do it differently for a large value, to avoid overflow. */
21689 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21690 else
21691 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21692 /* We can't normally display a 3-digit number,
21693 so get us a 2-digit number that is close. */
21694 if (total == 100)
21695 total = 99;
21696 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21697 return decode_mode_spec_buf;
21698 }
21699 }
21700
21701 /* Display percentage of size above the bottom of the screen. */
21702 case 'P':
21703 {
21704 ptrdiff_t toppos = marker_position (w->start);
21705 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21706 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21707
21708 if (botpos >= BUF_ZV (b))
21709 {
21710 if (toppos <= BUF_BEGV (b))
21711 return "All";
21712 else
21713 return "Bottom";
21714 }
21715 else
21716 {
21717 if (total > 1000000)
21718 /* Do it differently for a large value, to avoid overflow. */
21719 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21720 else
21721 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21722 /* We can't normally display a 3-digit number,
21723 so get us a 2-digit number that is close. */
21724 if (total == 100)
21725 total = 99;
21726 if (toppos <= BUF_BEGV (b))
21727 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21728 else
21729 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21730 return decode_mode_spec_buf;
21731 }
21732 }
21733
21734 case 's':
21735 /* status of process */
21736 obj = Fget_buffer_process (Fcurrent_buffer ());
21737 if (NILP (obj))
21738 return "no process";
21739 #ifndef MSDOS
21740 obj = Fsymbol_name (Fprocess_status (obj));
21741 #endif
21742 break;
21743
21744 case '@':
21745 {
21746 ptrdiff_t count = inhibit_garbage_collection ();
21747 Lisp_Object val = call1 (intern ("file-remote-p"),
21748 BVAR (current_buffer, directory));
21749 unbind_to (count, Qnil);
21750
21751 if (NILP (val))
21752 return "-";
21753 else
21754 return "@";
21755 }
21756
21757 case 't': /* indicate TEXT or BINARY */
21758 return "T";
21759
21760 case 'z':
21761 /* coding-system (not including end-of-line format) */
21762 case 'Z':
21763 /* coding-system (including end-of-line type) */
21764 {
21765 int eol_flag = (c == 'Z');
21766 char *p = decode_mode_spec_buf;
21767
21768 if (! FRAME_WINDOW_P (f))
21769 {
21770 /* No need to mention EOL here--the terminal never needs
21771 to do EOL conversion. */
21772 p = decode_mode_spec_coding (CODING_ID_NAME
21773 (FRAME_KEYBOARD_CODING (f)->id),
21774 p, 0);
21775 p = decode_mode_spec_coding (CODING_ID_NAME
21776 (FRAME_TERMINAL_CODING (f)->id),
21777 p, 0);
21778 }
21779 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21780 p, eol_flag);
21781
21782 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21783 #ifdef subprocesses
21784 obj = Fget_buffer_process (Fcurrent_buffer ());
21785 if (PROCESSP (obj))
21786 {
21787 p = decode_mode_spec_coding
21788 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21789 p = decode_mode_spec_coding
21790 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21791 }
21792 #endif /* subprocesses */
21793 #endif /* 0 */
21794 *p = 0;
21795 return decode_mode_spec_buf;
21796 }
21797 }
21798
21799 if (STRINGP (obj))
21800 {
21801 *string = obj;
21802 return SSDATA (obj);
21803 }
21804 else
21805 return "";
21806 }
21807
21808
21809 /* Count up to COUNT lines starting from START_BYTE.
21810 But don't go beyond LIMIT_BYTE.
21811 Return the number of lines thus found (always nonnegative).
21812
21813 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21814
21815 static ptrdiff_t
21816 display_count_lines (ptrdiff_t start_byte,
21817 ptrdiff_t limit_byte, ptrdiff_t count,
21818 ptrdiff_t *byte_pos_ptr)
21819 {
21820 register unsigned char *cursor;
21821 unsigned char *base;
21822
21823 register ptrdiff_t ceiling;
21824 register unsigned char *ceiling_addr;
21825 ptrdiff_t orig_count = count;
21826
21827 /* If we are not in selective display mode,
21828 check only for newlines. */
21829 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21830 && !INTEGERP (BVAR (current_buffer, selective_display)));
21831
21832 if (count > 0)
21833 {
21834 while (start_byte < limit_byte)
21835 {
21836 ceiling = BUFFER_CEILING_OF (start_byte);
21837 ceiling = min (limit_byte - 1, ceiling);
21838 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21839 base = (cursor = BYTE_POS_ADDR (start_byte));
21840 while (1)
21841 {
21842 if (selective_display)
21843 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21844 ;
21845 else
21846 while (*cursor != '\n' && ++cursor != ceiling_addr)
21847 ;
21848
21849 if (cursor != ceiling_addr)
21850 {
21851 if (--count == 0)
21852 {
21853 start_byte += cursor - base + 1;
21854 *byte_pos_ptr = start_byte;
21855 return orig_count;
21856 }
21857 else
21858 if (++cursor == ceiling_addr)
21859 break;
21860 }
21861 else
21862 break;
21863 }
21864 start_byte += cursor - base;
21865 }
21866 }
21867 else
21868 {
21869 while (start_byte > limit_byte)
21870 {
21871 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21872 ceiling = max (limit_byte, ceiling);
21873 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21874 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21875 while (1)
21876 {
21877 if (selective_display)
21878 while (--cursor != ceiling_addr
21879 && *cursor != '\n' && *cursor != 015)
21880 ;
21881 else
21882 while (--cursor != ceiling_addr && *cursor != '\n')
21883 ;
21884
21885 if (cursor != ceiling_addr)
21886 {
21887 if (++count == 0)
21888 {
21889 start_byte += cursor - base + 1;
21890 *byte_pos_ptr = start_byte;
21891 /* When scanning backwards, we should
21892 not count the newline posterior to which we stop. */
21893 return - orig_count - 1;
21894 }
21895 }
21896 else
21897 break;
21898 }
21899 /* Here we add 1 to compensate for the last decrement
21900 of CURSOR, which took it past the valid range. */
21901 start_byte += cursor - base + 1;
21902 }
21903 }
21904
21905 *byte_pos_ptr = limit_byte;
21906
21907 if (count < 0)
21908 return - orig_count + count;
21909 return orig_count - count;
21910
21911 }
21912
21913
21914 \f
21915 /***********************************************************************
21916 Displaying strings
21917 ***********************************************************************/
21918
21919 /* Display a NUL-terminated string, starting with index START.
21920
21921 If STRING is non-null, display that C string. Otherwise, the Lisp
21922 string LISP_STRING is displayed. There's a case that STRING is
21923 non-null and LISP_STRING is not nil. It means STRING is a string
21924 data of LISP_STRING. In that case, we display LISP_STRING while
21925 ignoring its text properties.
21926
21927 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21928 FACE_STRING. Display STRING or LISP_STRING with the face at
21929 FACE_STRING_POS in FACE_STRING:
21930
21931 Display the string in the environment given by IT, but use the
21932 standard display table, temporarily.
21933
21934 FIELD_WIDTH is the minimum number of output glyphs to produce.
21935 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21936 with spaces. If STRING has more characters, more than FIELD_WIDTH
21937 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21938
21939 PRECISION is the maximum number of characters to output from
21940 STRING. PRECISION < 0 means don't truncate the string.
21941
21942 This is roughly equivalent to printf format specifiers:
21943
21944 FIELD_WIDTH PRECISION PRINTF
21945 ----------------------------------------
21946 -1 -1 %s
21947 -1 10 %.10s
21948 10 -1 %10s
21949 20 10 %20.10s
21950
21951 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21952 display them, and < 0 means obey the current buffer's value of
21953 enable_multibyte_characters.
21954
21955 Value is the number of columns displayed. */
21956
21957 static int
21958 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21959 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21960 int field_width, int precision, int max_x, int multibyte)
21961 {
21962 int hpos_at_start = it->hpos;
21963 int saved_face_id = it->face_id;
21964 struct glyph_row *row = it->glyph_row;
21965 ptrdiff_t it_charpos;
21966
21967 /* Initialize the iterator IT for iteration over STRING beginning
21968 with index START. */
21969 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21970 precision, field_width, multibyte);
21971 if (string && STRINGP (lisp_string))
21972 /* LISP_STRING is the one returned by decode_mode_spec. We should
21973 ignore its text properties. */
21974 it->stop_charpos = it->end_charpos;
21975
21976 /* If displaying STRING, set up the face of the iterator from
21977 FACE_STRING, if that's given. */
21978 if (STRINGP (face_string))
21979 {
21980 ptrdiff_t endptr;
21981 struct face *face;
21982
21983 it->face_id
21984 = face_at_string_position (it->w, face_string, face_string_pos,
21985 0, it->region_beg_charpos,
21986 it->region_end_charpos,
21987 &endptr, it->base_face_id, 0);
21988 face = FACE_FROM_ID (it->f, it->face_id);
21989 it->face_box_p = face->box != FACE_NO_BOX;
21990 }
21991
21992 /* Set max_x to the maximum allowed X position. Don't let it go
21993 beyond the right edge of the window. */
21994 if (max_x <= 0)
21995 max_x = it->last_visible_x;
21996 else
21997 max_x = min (max_x, it->last_visible_x);
21998
21999 /* Skip over display elements that are not visible. because IT->w is
22000 hscrolled. */
22001 if (it->current_x < it->first_visible_x)
22002 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22003 MOVE_TO_POS | MOVE_TO_X);
22004
22005 row->ascent = it->max_ascent;
22006 row->height = it->max_ascent + it->max_descent;
22007 row->phys_ascent = it->max_phys_ascent;
22008 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22009 row->extra_line_spacing = it->max_extra_line_spacing;
22010
22011 if (STRINGP (it->string))
22012 it_charpos = IT_STRING_CHARPOS (*it);
22013 else
22014 it_charpos = IT_CHARPOS (*it);
22015
22016 /* This condition is for the case that we are called with current_x
22017 past last_visible_x. */
22018 while (it->current_x < max_x)
22019 {
22020 int x_before, x, n_glyphs_before, i, nglyphs;
22021
22022 /* Get the next display element. */
22023 if (!get_next_display_element (it))
22024 break;
22025
22026 /* Produce glyphs. */
22027 x_before = it->current_x;
22028 n_glyphs_before = row->used[TEXT_AREA];
22029 PRODUCE_GLYPHS (it);
22030
22031 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22032 i = 0;
22033 x = x_before;
22034 while (i < nglyphs)
22035 {
22036 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22037
22038 if (it->line_wrap != TRUNCATE
22039 && x + glyph->pixel_width > max_x)
22040 {
22041 /* End of continued line or max_x reached. */
22042 if (CHAR_GLYPH_PADDING_P (*glyph))
22043 {
22044 /* A wide character is unbreakable. */
22045 if (row->reversed_p)
22046 unproduce_glyphs (it, row->used[TEXT_AREA]
22047 - n_glyphs_before);
22048 row->used[TEXT_AREA] = n_glyphs_before;
22049 it->current_x = x_before;
22050 }
22051 else
22052 {
22053 if (row->reversed_p)
22054 unproduce_glyphs (it, row->used[TEXT_AREA]
22055 - (n_glyphs_before + i));
22056 row->used[TEXT_AREA] = n_glyphs_before + i;
22057 it->current_x = x;
22058 }
22059 break;
22060 }
22061 else if (x + glyph->pixel_width >= it->first_visible_x)
22062 {
22063 /* Glyph is at least partially visible. */
22064 ++it->hpos;
22065 if (x < it->first_visible_x)
22066 row->x = x - it->first_visible_x;
22067 }
22068 else
22069 {
22070 /* Glyph is off the left margin of the display area.
22071 Should not happen. */
22072 emacs_abort ();
22073 }
22074
22075 row->ascent = max (row->ascent, it->max_ascent);
22076 row->height = max (row->height, it->max_ascent + it->max_descent);
22077 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22078 row->phys_height = max (row->phys_height,
22079 it->max_phys_ascent + it->max_phys_descent);
22080 row->extra_line_spacing = max (row->extra_line_spacing,
22081 it->max_extra_line_spacing);
22082 x += glyph->pixel_width;
22083 ++i;
22084 }
22085
22086 /* Stop if max_x reached. */
22087 if (i < nglyphs)
22088 break;
22089
22090 /* Stop at line ends. */
22091 if (ITERATOR_AT_END_OF_LINE_P (it))
22092 {
22093 it->continuation_lines_width = 0;
22094 break;
22095 }
22096
22097 set_iterator_to_next (it, 1);
22098 if (STRINGP (it->string))
22099 it_charpos = IT_STRING_CHARPOS (*it);
22100 else
22101 it_charpos = IT_CHARPOS (*it);
22102
22103 /* Stop if truncating at the right edge. */
22104 if (it->line_wrap == TRUNCATE
22105 && it->current_x >= it->last_visible_x)
22106 {
22107 /* Add truncation mark, but don't do it if the line is
22108 truncated at a padding space. */
22109 if (it_charpos < it->string_nchars)
22110 {
22111 if (!FRAME_WINDOW_P (it->f))
22112 {
22113 int ii, n;
22114
22115 if (it->current_x > it->last_visible_x)
22116 {
22117 if (!row->reversed_p)
22118 {
22119 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22120 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22121 break;
22122 }
22123 else
22124 {
22125 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22126 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22127 break;
22128 unproduce_glyphs (it, ii + 1);
22129 ii = row->used[TEXT_AREA] - (ii + 1);
22130 }
22131 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22132 {
22133 row->used[TEXT_AREA] = ii;
22134 produce_special_glyphs (it, IT_TRUNCATION);
22135 }
22136 }
22137 produce_special_glyphs (it, IT_TRUNCATION);
22138 }
22139 row->truncated_on_right_p = 1;
22140 }
22141 break;
22142 }
22143 }
22144
22145 /* Maybe insert a truncation at the left. */
22146 if (it->first_visible_x
22147 && it_charpos > 0)
22148 {
22149 if (!FRAME_WINDOW_P (it->f)
22150 || (row->reversed_p
22151 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22152 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22153 insert_left_trunc_glyphs (it);
22154 row->truncated_on_left_p = 1;
22155 }
22156
22157 it->face_id = saved_face_id;
22158
22159 /* Value is number of columns displayed. */
22160 return it->hpos - hpos_at_start;
22161 }
22162
22163
22164 \f
22165 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22166 appears as an element of LIST or as the car of an element of LIST.
22167 If PROPVAL is a list, compare each element against LIST in that
22168 way, and return 1/2 if any element of PROPVAL is found in LIST.
22169 Otherwise return 0. This function cannot quit.
22170 The return value is 2 if the text is invisible but with an ellipsis
22171 and 1 if it's invisible and without an ellipsis. */
22172
22173 int
22174 invisible_p (register Lisp_Object propval, Lisp_Object list)
22175 {
22176 register Lisp_Object tail, proptail;
22177
22178 for (tail = list; CONSP (tail); tail = XCDR (tail))
22179 {
22180 register Lisp_Object tem;
22181 tem = XCAR (tail);
22182 if (EQ (propval, tem))
22183 return 1;
22184 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22185 return NILP (XCDR (tem)) ? 1 : 2;
22186 }
22187
22188 if (CONSP (propval))
22189 {
22190 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22191 {
22192 Lisp_Object propelt;
22193 propelt = XCAR (proptail);
22194 for (tail = list; CONSP (tail); tail = XCDR (tail))
22195 {
22196 register Lisp_Object tem;
22197 tem = XCAR (tail);
22198 if (EQ (propelt, tem))
22199 return 1;
22200 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22201 return NILP (XCDR (tem)) ? 1 : 2;
22202 }
22203 }
22204 }
22205
22206 return 0;
22207 }
22208
22209 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22210 doc: /* Non-nil if the property makes the text invisible.
22211 POS-OR-PROP can be a marker or number, in which case it is taken to be
22212 a position in the current buffer and the value of the `invisible' property
22213 is checked; or it can be some other value, which is then presumed to be the
22214 value of the `invisible' property of the text of interest.
22215 The non-nil value returned can be t for truly invisible text or something
22216 else if the text is replaced by an ellipsis. */)
22217 (Lisp_Object pos_or_prop)
22218 {
22219 Lisp_Object prop
22220 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22221 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22222 : pos_or_prop);
22223 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22224 return (invis == 0 ? Qnil
22225 : invis == 1 ? Qt
22226 : make_number (invis));
22227 }
22228
22229 /* Calculate a width or height in pixels from a specification using
22230 the following elements:
22231
22232 SPEC ::=
22233 NUM - a (fractional) multiple of the default font width/height
22234 (NUM) - specifies exactly NUM pixels
22235 UNIT - a fixed number of pixels, see below.
22236 ELEMENT - size of a display element in pixels, see below.
22237 (NUM . SPEC) - equals NUM * SPEC
22238 (+ SPEC SPEC ...) - add pixel values
22239 (- SPEC SPEC ...) - subtract pixel values
22240 (- SPEC) - negate pixel value
22241
22242 NUM ::=
22243 INT or FLOAT - a number constant
22244 SYMBOL - use symbol's (buffer local) variable binding.
22245
22246 UNIT ::=
22247 in - pixels per inch *)
22248 mm - pixels per 1/1000 meter *)
22249 cm - pixels per 1/100 meter *)
22250 width - width of current font in pixels.
22251 height - height of current font in pixels.
22252
22253 *) using the ratio(s) defined in display-pixels-per-inch.
22254
22255 ELEMENT ::=
22256
22257 left-fringe - left fringe width in pixels
22258 right-fringe - right fringe width in pixels
22259
22260 left-margin - left margin width in pixels
22261 right-margin - right margin width in pixels
22262
22263 scroll-bar - scroll-bar area width in pixels
22264
22265 Examples:
22266
22267 Pixels corresponding to 5 inches:
22268 (5 . in)
22269
22270 Total width of non-text areas on left side of window (if scroll-bar is on left):
22271 '(space :width (+ left-fringe left-margin scroll-bar))
22272
22273 Align to first text column (in header line):
22274 '(space :align-to 0)
22275
22276 Align to middle of text area minus half the width of variable `my-image'
22277 containing a loaded image:
22278 '(space :align-to (0.5 . (- text my-image)))
22279
22280 Width of left margin minus width of 1 character in the default font:
22281 '(space :width (- left-margin 1))
22282
22283 Width of left margin minus width of 2 characters in the current font:
22284 '(space :width (- left-margin (2 . width)))
22285
22286 Center 1 character over left-margin (in header line):
22287 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22288
22289 Different ways to express width of left fringe plus left margin minus one pixel:
22290 '(space :width (- (+ left-fringe left-margin) (1)))
22291 '(space :width (+ left-fringe left-margin (- (1))))
22292 '(space :width (+ left-fringe left-margin (-1)))
22293
22294 */
22295
22296 #define NUMVAL(X) \
22297 ((INTEGERP (X) || FLOATP (X)) \
22298 ? XFLOATINT (X) \
22299 : - 1)
22300
22301 static int
22302 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22303 struct font *font, int width_p, int *align_to)
22304 {
22305 double pixels;
22306
22307 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22308 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22309
22310 if (NILP (prop))
22311 return OK_PIXELS (0);
22312
22313 eassert (FRAME_LIVE_P (it->f));
22314
22315 if (SYMBOLP (prop))
22316 {
22317 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22318 {
22319 char *unit = SSDATA (SYMBOL_NAME (prop));
22320
22321 if (unit[0] == 'i' && unit[1] == 'n')
22322 pixels = 1.0;
22323 else if (unit[0] == 'm' && unit[1] == 'm')
22324 pixels = 25.4;
22325 else if (unit[0] == 'c' && unit[1] == 'm')
22326 pixels = 2.54;
22327 else
22328 pixels = 0;
22329 if (pixels > 0)
22330 {
22331 double ppi;
22332 #ifdef HAVE_WINDOW_SYSTEM
22333 if (FRAME_WINDOW_P (it->f)
22334 && (ppi = (width_p
22335 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22336 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22337 ppi > 0))
22338 return OK_PIXELS (ppi / pixels);
22339 #endif
22340
22341 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22342 || (CONSP (Vdisplay_pixels_per_inch)
22343 && (ppi = (width_p
22344 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22345 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22346 ppi > 0)))
22347 return OK_PIXELS (ppi / pixels);
22348
22349 return 0;
22350 }
22351 }
22352
22353 #ifdef HAVE_WINDOW_SYSTEM
22354 if (EQ (prop, Qheight))
22355 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22356 if (EQ (prop, Qwidth))
22357 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22358 #else
22359 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22360 return OK_PIXELS (1);
22361 #endif
22362
22363 if (EQ (prop, Qtext))
22364 return OK_PIXELS (width_p
22365 ? window_box_width (it->w, TEXT_AREA)
22366 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22367
22368 if (align_to && *align_to < 0)
22369 {
22370 *res = 0;
22371 if (EQ (prop, Qleft))
22372 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22373 if (EQ (prop, Qright))
22374 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22375 if (EQ (prop, Qcenter))
22376 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22377 + window_box_width (it->w, TEXT_AREA) / 2);
22378 if (EQ (prop, Qleft_fringe))
22379 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22380 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22381 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22382 if (EQ (prop, Qright_fringe))
22383 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22384 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22385 : window_box_right_offset (it->w, TEXT_AREA));
22386 if (EQ (prop, Qleft_margin))
22387 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22388 if (EQ (prop, Qright_margin))
22389 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22390 if (EQ (prop, Qscroll_bar))
22391 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22392 ? 0
22393 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22394 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22395 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22396 : 0)));
22397 }
22398 else
22399 {
22400 if (EQ (prop, Qleft_fringe))
22401 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22402 if (EQ (prop, Qright_fringe))
22403 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22404 if (EQ (prop, Qleft_margin))
22405 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22406 if (EQ (prop, Qright_margin))
22407 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22408 if (EQ (prop, Qscroll_bar))
22409 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22410 }
22411
22412 prop = buffer_local_value_1 (prop, it->w->buffer);
22413 if (EQ (prop, Qunbound))
22414 prop = Qnil;
22415 }
22416
22417 if (INTEGERP (prop) || FLOATP (prop))
22418 {
22419 int base_unit = (width_p
22420 ? FRAME_COLUMN_WIDTH (it->f)
22421 : FRAME_LINE_HEIGHT (it->f));
22422 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22423 }
22424
22425 if (CONSP (prop))
22426 {
22427 Lisp_Object car = XCAR (prop);
22428 Lisp_Object cdr = XCDR (prop);
22429
22430 if (SYMBOLP (car))
22431 {
22432 #ifdef HAVE_WINDOW_SYSTEM
22433 if (FRAME_WINDOW_P (it->f)
22434 && valid_image_p (prop))
22435 {
22436 ptrdiff_t id = lookup_image (it->f, prop);
22437 struct image *img = IMAGE_FROM_ID (it->f, id);
22438
22439 return OK_PIXELS (width_p ? img->width : img->height);
22440 }
22441 #endif
22442 if (EQ (car, Qplus) || EQ (car, Qminus))
22443 {
22444 int first = 1;
22445 double px;
22446
22447 pixels = 0;
22448 while (CONSP (cdr))
22449 {
22450 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22451 font, width_p, align_to))
22452 return 0;
22453 if (first)
22454 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22455 else
22456 pixels += px;
22457 cdr = XCDR (cdr);
22458 }
22459 if (EQ (car, Qminus))
22460 pixels = -pixels;
22461 return OK_PIXELS (pixels);
22462 }
22463
22464 car = buffer_local_value_1 (car, it->w->buffer);
22465 if (EQ (car, Qunbound))
22466 car = Qnil;
22467 }
22468
22469 if (INTEGERP (car) || FLOATP (car))
22470 {
22471 double fact;
22472 pixels = XFLOATINT (car);
22473 if (NILP (cdr))
22474 return OK_PIXELS (pixels);
22475 if (calc_pixel_width_or_height (&fact, it, cdr,
22476 font, width_p, align_to))
22477 return OK_PIXELS (pixels * fact);
22478 return 0;
22479 }
22480
22481 return 0;
22482 }
22483
22484 return 0;
22485 }
22486
22487 \f
22488 /***********************************************************************
22489 Glyph Display
22490 ***********************************************************************/
22491
22492 #ifdef HAVE_WINDOW_SYSTEM
22493
22494 #ifdef GLYPH_DEBUG
22495
22496 void
22497 dump_glyph_string (struct glyph_string *s)
22498 {
22499 fprintf (stderr, "glyph string\n");
22500 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22501 s->x, s->y, s->width, s->height);
22502 fprintf (stderr, " ybase = %d\n", s->ybase);
22503 fprintf (stderr, " hl = %d\n", s->hl);
22504 fprintf (stderr, " left overhang = %d, right = %d\n",
22505 s->left_overhang, s->right_overhang);
22506 fprintf (stderr, " nchars = %d\n", s->nchars);
22507 fprintf (stderr, " extends to end of line = %d\n",
22508 s->extends_to_end_of_line_p);
22509 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22510 fprintf (stderr, " bg width = %d\n", s->background_width);
22511 }
22512
22513 #endif /* GLYPH_DEBUG */
22514
22515 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22516 of XChar2b structures for S; it can't be allocated in
22517 init_glyph_string because it must be allocated via `alloca'. W
22518 is the window on which S is drawn. ROW and AREA are the glyph row
22519 and area within the row from which S is constructed. START is the
22520 index of the first glyph structure covered by S. HL is a
22521 face-override for drawing S. */
22522
22523 #ifdef HAVE_NTGUI
22524 #define OPTIONAL_HDC(hdc) HDC hdc,
22525 #define DECLARE_HDC(hdc) HDC hdc;
22526 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22527 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22528 #endif
22529
22530 #ifndef OPTIONAL_HDC
22531 #define OPTIONAL_HDC(hdc)
22532 #define DECLARE_HDC(hdc)
22533 #define ALLOCATE_HDC(hdc, f)
22534 #define RELEASE_HDC(hdc, f)
22535 #endif
22536
22537 static void
22538 init_glyph_string (struct glyph_string *s,
22539 OPTIONAL_HDC (hdc)
22540 XChar2b *char2b, struct window *w, struct glyph_row *row,
22541 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22542 {
22543 memset (s, 0, sizeof *s);
22544 s->w = w;
22545 s->f = XFRAME (w->frame);
22546 #ifdef HAVE_NTGUI
22547 s->hdc = hdc;
22548 #endif
22549 s->display = FRAME_X_DISPLAY (s->f);
22550 s->window = FRAME_X_WINDOW (s->f);
22551 s->char2b = char2b;
22552 s->hl = hl;
22553 s->row = row;
22554 s->area = area;
22555 s->first_glyph = row->glyphs[area] + start;
22556 s->height = row->height;
22557 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22558 s->ybase = s->y + row->ascent;
22559 }
22560
22561
22562 /* Append the list of glyph strings with head H and tail T to the list
22563 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22564
22565 static void
22566 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22567 struct glyph_string *h, struct glyph_string *t)
22568 {
22569 if (h)
22570 {
22571 if (*head)
22572 (*tail)->next = h;
22573 else
22574 *head = h;
22575 h->prev = *tail;
22576 *tail = t;
22577 }
22578 }
22579
22580
22581 /* Prepend the list of glyph strings with head H and tail T to the
22582 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22583 result. */
22584
22585 static void
22586 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22587 struct glyph_string *h, struct glyph_string *t)
22588 {
22589 if (h)
22590 {
22591 if (*head)
22592 (*head)->prev = t;
22593 else
22594 *tail = t;
22595 t->next = *head;
22596 *head = h;
22597 }
22598 }
22599
22600
22601 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22602 Set *HEAD and *TAIL to the resulting list. */
22603
22604 static void
22605 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22606 struct glyph_string *s)
22607 {
22608 s->next = s->prev = NULL;
22609 append_glyph_string_lists (head, tail, s, s);
22610 }
22611
22612
22613 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22614 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22615 make sure that X resources for the face returned are allocated.
22616 Value is a pointer to a realized face that is ready for display if
22617 DISPLAY_P is non-zero. */
22618
22619 static struct face *
22620 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22621 XChar2b *char2b, int display_p)
22622 {
22623 struct face *face = FACE_FROM_ID (f, face_id);
22624
22625 if (face->font)
22626 {
22627 unsigned code = face->font->driver->encode_char (face->font, c);
22628
22629 if (code != FONT_INVALID_CODE)
22630 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22631 else
22632 STORE_XCHAR2B (char2b, 0, 0);
22633 }
22634
22635 /* Make sure X resources of the face are allocated. */
22636 #ifdef HAVE_X_WINDOWS
22637 if (display_p)
22638 #endif
22639 {
22640 eassert (face != NULL);
22641 PREPARE_FACE_FOR_DISPLAY (f, face);
22642 }
22643
22644 return face;
22645 }
22646
22647
22648 /* Get face and two-byte form of character glyph GLYPH on frame F.
22649 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22650 a pointer to a realized face that is ready for display. */
22651
22652 static struct face *
22653 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22654 XChar2b *char2b, int *two_byte_p)
22655 {
22656 struct face *face;
22657
22658 eassert (glyph->type == CHAR_GLYPH);
22659 face = FACE_FROM_ID (f, glyph->face_id);
22660
22661 if (two_byte_p)
22662 *two_byte_p = 0;
22663
22664 if (face->font)
22665 {
22666 unsigned code;
22667
22668 if (CHAR_BYTE8_P (glyph->u.ch))
22669 code = CHAR_TO_BYTE8 (glyph->u.ch);
22670 else
22671 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22672
22673 if (code != FONT_INVALID_CODE)
22674 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22675 else
22676 STORE_XCHAR2B (char2b, 0, 0);
22677 }
22678
22679 /* Make sure X resources of the face are allocated. */
22680 eassert (face != NULL);
22681 PREPARE_FACE_FOR_DISPLAY (f, face);
22682 return face;
22683 }
22684
22685
22686 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22687 Return 1 if FONT has a glyph for C, otherwise return 0. */
22688
22689 static int
22690 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22691 {
22692 unsigned code;
22693
22694 if (CHAR_BYTE8_P (c))
22695 code = CHAR_TO_BYTE8 (c);
22696 else
22697 code = font->driver->encode_char (font, c);
22698
22699 if (code == FONT_INVALID_CODE)
22700 return 0;
22701 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22702 return 1;
22703 }
22704
22705
22706 /* Fill glyph string S with composition components specified by S->cmp.
22707
22708 BASE_FACE is the base face of the composition.
22709 S->cmp_from is the index of the first component for S.
22710
22711 OVERLAPS non-zero means S should draw the foreground only, and use
22712 its physical height for clipping. See also draw_glyphs.
22713
22714 Value is the index of a component not in S. */
22715
22716 static int
22717 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22718 int overlaps)
22719 {
22720 int i;
22721 /* For all glyphs of this composition, starting at the offset
22722 S->cmp_from, until we reach the end of the definition or encounter a
22723 glyph that requires the different face, add it to S. */
22724 struct face *face;
22725
22726 eassert (s);
22727
22728 s->for_overlaps = overlaps;
22729 s->face = NULL;
22730 s->font = NULL;
22731 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22732 {
22733 int c = COMPOSITION_GLYPH (s->cmp, i);
22734
22735 /* TAB in a composition means display glyphs with padding space
22736 on the left or right. */
22737 if (c != '\t')
22738 {
22739 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22740 -1, Qnil);
22741
22742 face = get_char_face_and_encoding (s->f, c, face_id,
22743 s->char2b + i, 1);
22744 if (face)
22745 {
22746 if (! s->face)
22747 {
22748 s->face = face;
22749 s->font = s->face->font;
22750 }
22751 else if (s->face != face)
22752 break;
22753 }
22754 }
22755 ++s->nchars;
22756 }
22757 s->cmp_to = i;
22758
22759 if (s->face == NULL)
22760 {
22761 s->face = base_face->ascii_face;
22762 s->font = s->face->font;
22763 }
22764
22765 /* All glyph strings for the same composition has the same width,
22766 i.e. the width set for the first component of the composition. */
22767 s->width = s->first_glyph->pixel_width;
22768
22769 /* If the specified font could not be loaded, use the frame's
22770 default font, but record the fact that we couldn't load it in
22771 the glyph string so that we can draw rectangles for the
22772 characters of the glyph string. */
22773 if (s->font == NULL)
22774 {
22775 s->font_not_found_p = 1;
22776 s->font = FRAME_FONT (s->f);
22777 }
22778
22779 /* Adjust base line for subscript/superscript text. */
22780 s->ybase += s->first_glyph->voffset;
22781
22782 /* This glyph string must always be drawn with 16-bit functions. */
22783 s->two_byte_p = 1;
22784
22785 return s->cmp_to;
22786 }
22787
22788 static int
22789 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22790 int start, int end, int overlaps)
22791 {
22792 struct glyph *glyph, *last;
22793 Lisp_Object lgstring;
22794 int i;
22795
22796 s->for_overlaps = overlaps;
22797 glyph = s->row->glyphs[s->area] + start;
22798 last = s->row->glyphs[s->area] + end;
22799 s->cmp_id = glyph->u.cmp.id;
22800 s->cmp_from = glyph->slice.cmp.from;
22801 s->cmp_to = glyph->slice.cmp.to + 1;
22802 s->face = FACE_FROM_ID (s->f, face_id);
22803 lgstring = composition_gstring_from_id (s->cmp_id);
22804 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22805 glyph++;
22806 while (glyph < last
22807 && glyph->u.cmp.automatic
22808 && glyph->u.cmp.id == s->cmp_id
22809 && s->cmp_to == glyph->slice.cmp.from)
22810 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22811
22812 for (i = s->cmp_from; i < s->cmp_to; i++)
22813 {
22814 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22815 unsigned code = LGLYPH_CODE (lglyph);
22816
22817 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22818 }
22819 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22820 return glyph - s->row->glyphs[s->area];
22821 }
22822
22823
22824 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22825 See the comment of fill_glyph_string for arguments.
22826 Value is the index of the first glyph not in S. */
22827
22828
22829 static int
22830 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22831 int start, int end, int overlaps)
22832 {
22833 struct glyph *glyph, *last;
22834 int voffset;
22835
22836 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22837 s->for_overlaps = overlaps;
22838 glyph = s->row->glyphs[s->area] + start;
22839 last = s->row->glyphs[s->area] + end;
22840 voffset = glyph->voffset;
22841 s->face = FACE_FROM_ID (s->f, face_id);
22842 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22843 s->nchars = 1;
22844 s->width = glyph->pixel_width;
22845 glyph++;
22846 while (glyph < last
22847 && glyph->type == GLYPHLESS_GLYPH
22848 && glyph->voffset == voffset
22849 && glyph->face_id == face_id)
22850 {
22851 s->nchars++;
22852 s->width += glyph->pixel_width;
22853 glyph++;
22854 }
22855 s->ybase += voffset;
22856 return glyph - s->row->glyphs[s->area];
22857 }
22858
22859
22860 /* Fill glyph string S from a sequence of character glyphs.
22861
22862 FACE_ID is the face id of the string. START is the index of the
22863 first glyph to consider, END is the index of the last + 1.
22864 OVERLAPS non-zero means S should draw the foreground only, and use
22865 its physical height for clipping. See also draw_glyphs.
22866
22867 Value is the index of the first glyph not in S. */
22868
22869 static int
22870 fill_glyph_string (struct glyph_string *s, int face_id,
22871 int start, int end, int overlaps)
22872 {
22873 struct glyph *glyph, *last;
22874 int voffset;
22875 int glyph_not_available_p;
22876
22877 eassert (s->f == XFRAME (s->w->frame));
22878 eassert (s->nchars == 0);
22879 eassert (start >= 0 && end > start);
22880
22881 s->for_overlaps = overlaps;
22882 glyph = s->row->glyphs[s->area] + start;
22883 last = s->row->glyphs[s->area] + end;
22884 voffset = glyph->voffset;
22885 s->padding_p = glyph->padding_p;
22886 glyph_not_available_p = glyph->glyph_not_available_p;
22887
22888 while (glyph < last
22889 && glyph->type == CHAR_GLYPH
22890 && glyph->voffset == voffset
22891 /* Same face id implies same font, nowadays. */
22892 && glyph->face_id == face_id
22893 && glyph->glyph_not_available_p == glyph_not_available_p)
22894 {
22895 int two_byte_p;
22896
22897 s->face = get_glyph_face_and_encoding (s->f, glyph,
22898 s->char2b + s->nchars,
22899 &two_byte_p);
22900 s->two_byte_p = two_byte_p;
22901 ++s->nchars;
22902 eassert (s->nchars <= end - start);
22903 s->width += glyph->pixel_width;
22904 if (glyph++->padding_p != s->padding_p)
22905 break;
22906 }
22907
22908 s->font = s->face->font;
22909
22910 /* If the specified font could not be loaded, use the frame's font,
22911 but record the fact that we couldn't load it in
22912 S->font_not_found_p so that we can draw rectangles for the
22913 characters of the glyph string. */
22914 if (s->font == NULL || glyph_not_available_p)
22915 {
22916 s->font_not_found_p = 1;
22917 s->font = FRAME_FONT (s->f);
22918 }
22919
22920 /* Adjust base line for subscript/superscript text. */
22921 s->ybase += voffset;
22922
22923 eassert (s->face && s->face->gc);
22924 return glyph - s->row->glyphs[s->area];
22925 }
22926
22927
22928 /* Fill glyph string S from image glyph S->first_glyph. */
22929
22930 static void
22931 fill_image_glyph_string (struct glyph_string *s)
22932 {
22933 eassert (s->first_glyph->type == IMAGE_GLYPH);
22934 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22935 eassert (s->img);
22936 s->slice = s->first_glyph->slice.img;
22937 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22938 s->font = s->face->font;
22939 s->width = s->first_glyph->pixel_width;
22940
22941 /* Adjust base line for subscript/superscript text. */
22942 s->ybase += s->first_glyph->voffset;
22943 }
22944
22945
22946 /* Fill glyph string S from a sequence of stretch glyphs.
22947
22948 START is the index of the first glyph to consider,
22949 END is the index of the last + 1.
22950
22951 Value is the index of the first glyph not in S. */
22952
22953 static int
22954 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22955 {
22956 struct glyph *glyph, *last;
22957 int voffset, face_id;
22958
22959 eassert (s->first_glyph->type == STRETCH_GLYPH);
22960
22961 glyph = s->row->glyphs[s->area] + start;
22962 last = s->row->glyphs[s->area] + end;
22963 face_id = glyph->face_id;
22964 s->face = FACE_FROM_ID (s->f, face_id);
22965 s->font = s->face->font;
22966 s->width = glyph->pixel_width;
22967 s->nchars = 1;
22968 voffset = glyph->voffset;
22969
22970 for (++glyph;
22971 (glyph < last
22972 && glyph->type == STRETCH_GLYPH
22973 && glyph->voffset == voffset
22974 && glyph->face_id == face_id);
22975 ++glyph)
22976 s->width += glyph->pixel_width;
22977
22978 /* Adjust base line for subscript/superscript text. */
22979 s->ybase += voffset;
22980
22981 /* The case that face->gc == 0 is handled when drawing the glyph
22982 string by calling PREPARE_FACE_FOR_DISPLAY. */
22983 eassert (s->face);
22984 return glyph - s->row->glyphs[s->area];
22985 }
22986
22987 static struct font_metrics *
22988 get_per_char_metric (struct font *font, XChar2b *char2b)
22989 {
22990 static struct font_metrics metrics;
22991 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22992
22993 if (! font || code == FONT_INVALID_CODE)
22994 return NULL;
22995 font->driver->text_extents (font, &code, 1, &metrics);
22996 return &metrics;
22997 }
22998
22999 /* EXPORT for RIF:
23000 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23001 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23002 assumed to be zero. */
23003
23004 void
23005 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23006 {
23007 *left = *right = 0;
23008
23009 if (glyph->type == CHAR_GLYPH)
23010 {
23011 struct face *face;
23012 XChar2b char2b;
23013 struct font_metrics *pcm;
23014
23015 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23016 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23017 {
23018 if (pcm->rbearing > pcm->width)
23019 *right = pcm->rbearing - pcm->width;
23020 if (pcm->lbearing < 0)
23021 *left = -pcm->lbearing;
23022 }
23023 }
23024 else if (glyph->type == COMPOSITE_GLYPH)
23025 {
23026 if (! glyph->u.cmp.automatic)
23027 {
23028 struct composition *cmp = composition_table[glyph->u.cmp.id];
23029
23030 if (cmp->rbearing > cmp->pixel_width)
23031 *right = cmp->rbearing - cmp->pixel_width;
23032 if (cmp->lbearing < 0)
23033 *left = - cmp->lbearing;
23034 }
23035 else
23036 {
23037 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23038 struct font_metrics metrics;
23039
23040 composition_gstring_width (gstring, glyph->slice.cmp.from,
23041 glyph->slice.cmp.to + 1, &metrics);
23042 if (metrics.rbearing > metrics.width)
23043 *right = metrics.rbearing - metrics.width;
23044 if (metrics.lbearing < 0)
23045 *left = - metrics.lbearing;
23046 }
23047 }
23048 }
23049
23050
23051 /* Return the index of the first glyph preceding glyph string S that
23052 is overwritten by S because of S's left overhang. Value is -1
23053 if no glyphs are overwritten. */
23054
23055 static int
23056 left_overwritten (struct glyph_string *s)
23057 {
23058 int k;
23059
23060 if (s->left_overhang)
23061 {
23062 int x = 0, i;
23063 struct glyph *glyphs = s->row->glyphs[s->area];
23064 int first = s->first_glyph - glyphs;
23065
23066 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23067 x -= glyphs[i].pixel_width;
23068
23069 k = i + 1;
23070 }
23071 else
23072 k = -1;
23073
23074 return k;
23075 }
23076
23077
23078 /* Return the index of the first glyph preceding glyph string S that
23079 is overwriting S because of its right overhang. Value is -1 if no
23080 glyph in front of S overwrites S. */
23081
23082 static int
23083 left_overwriting (struct glyph_string *s)
23084 {
23085 int i, k, x;
23086 struct glyph *glyphs = s->row->glyphs[s->area];
23087 int first = s->first_glyph - glyphs;
23088
23089 k = -1;
23090 x = 0;
23091 for (i = first - 1; i >= 0; --i)
23092 {
23093 int left, right;
23094 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23095 if (x + right > 0)
23096 k = i;
23097 x -= glyphs[i].pixel_width;
23098 }
23099
23100 return k;
23101 }
23102
23103
23104 /* Return the index of the last glyph following glyph string S that is
23105 overwritten by S because of S's right overhang. Value is -1 if
23106 no such glyph is found. */
23107
23108 static int
23109 right_overwritten (struct glyph_string *s)
23110 {
23111 int k = -1;
23112
23113 if (s->right_overhang)
23114 {
23115 int x = 0, i;
23116 struct glyph *glyphs = s->row->glyphs[s->area];
23117 int first = (s->first_glyph - glyphs
23118 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23119 int end = s->row->used[s->area];
23120
23121 for (i = first; i < end && s->right_overhang > x; ++i)
23122 x += glyphs[i].pixel_width;
23123
23124 k = i;
23125 }
23126
23127 return k;
23128 }
23129
23130
23131 /* Return the index of the last glyph following glyph string S that
23132 overwrites S because of its left overhang. Value is negative
23133 if no such glyph is found. */
23134
23135 static int
23136 right_overwriting (struct glyph_string *s)
23137 {
23138 int i, k, x;
23139 int end = s->row->used[s->area];
23140 struct glyph *glyphs = s->row->glyphs[s->area];
23141 int first = (s->first_glyph - glyphs
23142 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23143
23144 k = -1;
23145 x = 0;
23146 for (i = first; i < end; ++i)
23147 {
23148 int left, right;
23149 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23150 if (x - left < 0)
23151 k = i;
23152 x += glyphs[i].pixel_width;
23153 }
23154
23155 return k;
23156 }
23157
23158
23159 /* Set background width of glyph string S. START is the index of the
23160 first glyph following S. LAST_X is the right-most x-position + 1
23161 in the drawing area. */
23162
23163 static void
23164 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23165 {
23166 /* If the face of this glyph string has to be drawn to the end of
23167 the drawing area, set S->extends_to_end_of_line_p. */
23168
23169 if (start == s->row->used[s->area]
23170 && s->area == TEXT_AREA
23171 && ((s->row->fill_line_p
23172 && (s->hl == DRAW_NORMAL_TEXT
23173 || s->hl == DRAW_IMAGE_RAISED
23174 || s->hl == DRAW_IMAGE_SUNKEN))
23175 || s->hl == DRAW_MOUSE_FACE))
23176 s->extends_to_end_of_line_p = 1;
23177
23178 /* If S extends its face to the end of the line, set its
23179 background_width to the distance to the right edge of the drawing
23180 area. */
23181 if (s->extends_to_end_of_line_p)
23182 s->background_width = last_x - s->x + 1;
23183 else
23184 s->background_width = s->width;
23185 }
23186
23187
23188 /* Compute overhangs and x-positions for glyph string S and its
23189 predecessors, or successors. X is the starting x-position for S.
23190 BACKWARD_P non-zero means process predecessors. */
23191
23192 static void
23193 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23194 {
23195 if (backward_p)
23196 {
23197 while (s)
23198 {
23199 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23200 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23201 x -= s->width;
23202 s->x = x;
23203 s = s->prev;
23204 }
23205 }
23206 else
23207 {
23208 while (s)
23209 {
23210 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23211 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23212 s->x = x;
23213 x += s->width;
23214 s = s->next;
23215 }
23216 }
23217 }
23218
23219
23220
23221 /* The following macros are only called from draw_glyphs below.
23222 They reference the following parameters of that function directly:
23223 `w', `row', `area', and `overlap_p'
23224 as well as the following local variables:
23225 `s', `f', and `hdc' (in W32) */
23226
23227 #ifdef HAVE_NTGUI
23228 /* On W32, silently add local `hdc' variable to argument list of
23229 init_glyph_string. */
23230 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23231 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23232 #else
23233 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23234 init_glyph_string (s, char2b, w, row, area, start, hl)
23235 #endif
23236
23237 /* Add a glyph string for a stretch glyph to the list of strings
23238 between HEAD and TAIL. START is the index of the stretch glyph in
23239 row area AREA of glyph row ROW. END is the index of the last glyph
23240 in that glyph row area. X is the current output position assigned
23241 to the new glyph string constructed. HL overrides that face of the
23242 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23243 is the right-most x-position of the drawing area. */
23244
23245 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23246 and below -- keep them on one line. */
23247 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23248 do \
23249 { \
23250 s = alloca (sizeof *s); \
23251 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23252 START = fill_stretch_glyph_string (s, START, END); \
23253 append_glyph_string (&HEAD, &TAIL, s); \
23254 s->x = (X); \
23255 } \
23256 while (0)
23257
23258
23259 /* Add a glyph string for an image glyph to the list of strings
23260 between HEAD and TAIL. START is the index of the image glyph in
23261 row area AREA of glyph row ROW. END is the index of the last glyph
23262 in that glyph row area. X is the current output position assigned
23263 to the new glyph string constructed. HL overrides that face of the
23264 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23265 is the right-most x-position of the drawing area. */
23266
23267 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23268 do \
23269 { \
23270 s = alloca (sizeof *s); \
23271 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23272 fill_image_glyph_string (s); \
23273 append_glyph_string (&HEAD, &TAIL, s); \
23274 ++START; \
23275 s->x = (X); \
23276 } \
23277 while (0)
23278
23279
23280 /* Add a glyph string for a sequence of character glyphs to the list
23281 of strings between HEAD and TAIL. START is the index of the first
23282 glyph in row area AREA of glyph row ROW that is part of the new
23283 glyph string. END is the index of the last glyph in that glyph row
23284 area. X is the current output position assigned to the new glyph
23285 string constructed. HL overrides that face of the glyph; e.g. it
23286 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23287 right-most x-position of the drawing area. */
23288
23289 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23290 do \
23291 { \
23292 int face_id; \
23293 XChar2b *char2b; \
23294 \
23295 face_id = (row)->glyphs[area][START].face_id; \
23296 \
23297 s = alloca (sizeof *s); \
23298 char2b = alloca ((END - START) * sizeof *char2b); \
23299 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23300 append_glyph_string (&HEAD, &TAIL, s); \
23301 s->x = (X); \
23302 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23303 } \
23304 while (0)
23305
23306
23307 /* Add a glyph string for a composite sequence to the list of strings
23308 between HEAD and TAIL. START is the index of the first glyph in
23309 row area AREA of glyph row ROW that is part of the new glyph
23310 string. END is the index of the last glyph in that glyph row area.
23311 X is the current output position assigned to the new glyph string
23312 constructed. HL overrides that face of the glyph; e.g. it is
23313 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23314 x-position of the drawing area. */
23315
23316 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23317 do { \
23318 int face_id = (row)->glyphs[area][START].face_id; \
23319 struct face *base_face = FACE_FROM_ID (f, face_id); \
23320 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23321 struct composition *cmp = composition_table[cmp_id]; \
23322 XChar2b *char2b; \
23323 struct glyph_string *first_s = NULL; \
23324 int n; \
23325 \
23326 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23327 \
23328 /* Make glyph_strings for each glyph sequence that is drawable by \
23329 the same face, and append them to HEAD/TAIL. */ \
23330 for (n = 0; n < cmp->glyph_len;) \
23331 { \
23332 s = alloca (sizeof *s); \
23333 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23334 append_glyph_string (&(HEAD), &(TAIL), s); \
23335 s->cmp = cmp; \
23336 s->cmp_from = n; \
23337 s->x = (X); \
23338 if (n == 0) \
23339 first_s = s; \
23340 n = fill_composite_glyph_string (s, base_face, overlaps); \
23341 } \
23342 \
23343 ++START; \
23344 s = first_s; \
23345 } while (0)
23346
23347
23348 /* Add a glyph string for a glyph-string sequence to the list of strings
23349 between HEAD and TAIL. */
23350
23351 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23352 do { \
23353 int face_id; \
23354 XChar2b *char2b; \
23355 Lisp_Object gstring; \
23356 \
23357 face_id = (row)->glyphs[area][START].face_id; \
23358 gstring = (composition_gstring_from_id \
23359 ((row)->glyphs[area][START].u.cmp.id)); \
23360 s = alloca (sizeof *s); \
23361 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23362 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23363 append_glyph_string (&(HEAD), &(TAIL), s); \
23364 s->x = (X); \
23365 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23366 } while (0)
23367
23368
23369 /* Add a glyph string for a sequence of glyphless character's glyphs
23370 to the list of strings between HEAD and TAIL. The meanings of
23371 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23372
23373 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23374 do \
23375 { \
23376 int face_id; \
23377 \
23378 face_id = (row)->glyphs[area][START].face_id; \
23379 \
23380 s = alloca (sizeof *s); \
23381 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23382 append_glyph_string (&HEAD, &TAIL, s); \
23383 s->x = (X); \
23384 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23385 overlaps); \
23386 } \
23387 while (0)
23388
23389
23390 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23391 of AREA of glyph row ROW on window W between indices START and END.
23392 HL overrides the face for drawing glyph strings, e.g. it is
23393 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23394 x-positions of the drawing area.
23395
23396 This is an ugly monster macro construct because we must use alloca
23397 to allocate glyph strings (because draw_glyphs can be called
23398 asynchronously). */
23399
23400 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23401 do \
23402 { \
23403 HEAD = TAIL = NULL; \
23404 while (START < END) \
23405 { \
23406 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23407 switch (first_glyph->type) \
23408 { \
23409 case CHAR_GLYPH: \
23410 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23411 HL, X, LAST_X); \
23412 break; \
23413 \
23414 case COMPOSITE_GLYPH: \
23415 if (first_glyph->u.cmp.automatic) \
23416 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23417 HL, X, LAST_X); \
23418 else \
23419 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23420 HL, X, LAST_X); \
23421 break; \
23422 \
23423 case STRETCH_GLYPH: \
23424 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23425 HL, X, LAST_X); \
23426 break; \
23427 \
23428 case IMAGE_GLYPH: \
23429 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23430 HL, X, LAST_X); \
23431 break; \
23432 \
23433 case GLYPHLESS_GLYPH: \
23434 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23435 HL, X, LAST_X); \
23436 break; \
23437 \
23438 default: \
23439 emacs_abort (); \
23440 } \
23441 \
23442 if (s) \
23443 { \
23444 set_glyph_string_background_width (s, START, LAST_X); \
23445 (X) += s->width; \
23446 } \
23447 } \
23448 } while (0)
23449
23450
23451 /* Draw glyphs between START and END in AREA of ROW on window W,
23452 starting at x-position X. X is relative to AREA in W. HL is a
23453 face-override with the following meaning:
23454
23455 DRAW_NORMAL_TEXT draw normally
23456 DRAW_CURSOR draw in cursor face
23457 DRAW_MOUSE_FACE draw in mouse face.
23458 DRAW_INVERSE_VIDEO draw in mode line face
23459 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23460 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23461
23462 If OVERLAPS is non-zero, draw only the foreground of characters and
23463 clip to the physical height of ROW. Non-zero value also defines
23464 the overlapping part to be drawn:
23465
23466 OVERLAPS_PRED overlap with preceding rows
23467 OVERLAPS_SUCC overlap with succeeding rows
23468 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23469 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23470
23471 Value is the x-position reached, relative to AREA of W. */
23472
23473 static int
23474 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23475 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23476 enum draw_glyphs_face hl, int overlaps)
23477 {
23478 struct glyph_string *head, *tail;
23479 struct glyph_string *s;
23480 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23481 int i, j, x_reached, last_x, area_left = 0;
23482 struct frame *f = XFRAME (WINDOW_FRAME (w));
23483 DECLARE_HDC (hdc);
23484
23485 ALLOCATE_HDC (hdc, f);
23486
23487 /* Let's rather be paranoid than getting a SEGV. */
23488 end = min (end, row->used[area]);
23489 start = clip_to_bounds (0, start, end);
23490
23491 /* Translate X to frame coordinates. Set last_x to the right
23492 end of the drawing area. */
23493 if (row->full_width_p)
23494 {
23495 /* X is relative to the left edge of W, without scroll bars
23496 or fringes. */
23497 area_left = WINDOW_LEFT_EDGE_X (w);
23498 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23499 }
23500 else
23501 {
23502 area_left = window_box_left (w, area);
23503 last_x = area_left + window_box_width (w, area);
23504 }
23505 x += area_left;
23506
23507 /* Build a doubly-linked list of glyph_string structures between
23508 head and tail from what we have to draw. Note that the macro
23509 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23510 the reason we use a separate variable `i'. */
23511 i = start;
23512 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23513 if (tail)
23514 x_reached = tail->x + tail->background_width;
23515 else
23516 x_reached = x;
23517
23518 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23519 the row, redraw some glyphs in front or following the glyph
23520 strings built above. */
23521 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23522 {
23523 struct glyph_string *h, *t;
23524 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23525 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23526 int check_mouse_face = 0;
23527 int dummy_x = 0;
23528
23529 /* If mouse highlighting is on, we may need to draw adjacent
23530 glyphs using mouse-face highlighting. */
23531 if (area == TEXT_AREA && row->mouse_face_p
23532 && hlinfo->mouse_face_beg_row >= 0
23533 && hlinfo->mouse_face_end_row >= 0)
23534 {
23535 struct glyph_row *mouse_beg_row, *mouse_end_row;
23536
23537 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23538 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23539
23540 if (row >= mouse_beg_row && row <= mouse_end_row)
23541 {
23542 check_mouse_face = 1;
23543 mouse_beg_col = (row == mouse_beg_row)
23544 ? hlinfo->mouse_face_beg_col : 0;
23545 mouse_end_col = (row == mouse_end_row)
23546 ? hlinfo->mouse_face_end_col
23547 : row->used[TEXT_AREA];
23548 }
23549 }
23550
23551 /* Compute overhangs for all glyph strings. */
23552 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23553 for (s = head; s; s = s->next)
23554 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23555
23556 /* Prepend glyph strings for glyphs in front of the first glyph
23557 string that are overwritten because of the first glyph
23558 string's left overhang. The background of all strings
23559 prepended must be drawn because the first glyph string
23560 draws over it. */
23561 i = left_overwritten (head);
23562 if (i >= 0)
23563 {
23564 enum draw_glyphs_face overlap_hl;
23565
23566 /* If this row contains mouse highlighting, attempt to draw
23567 the overlapped glyphs with the correct highlight. This
23568 code fails if the overlap encompasses more than one glyph
23569 and mouse-highlight spans only some of these glyphs.
23570 However, making it work perfectly involves a lot more
23571 code, and I don't know if the pathological case occurs in
23572 practice, so we'll stick to this for now. --- cyd */
23573 if (check_mouse_face
23574 && mouse_beg_col < start && mouse_end_col > i)
23575 overlap_hl = DRAW_MOUSE_FACE;
23576 else
23577 overlap_hl = DRAW_NORMAL_TEXT;
23578
23579 j = i;
23580 BUILD_GLYPH_STRINGS (j, start, h, t,
23581 overlap_hl, dummy_x, last_x);
23582 start = i;
23583 compute_overhangs_and_x (t, head->x, 1);
23584 prepend_glyph_string_lists (&head, &tail, h, t);
23585 clip_head = head;
23586 }
23587
23588 /* Prepend glyph strings for glyphs in front of the first glyph
23589 string that overwrite that glyph string because of their
23590 right overhang. For these strings, only the foreground must
23591 be drawn, because it draws over the glyph string at `head'.
23592 The background must not be drawn because this would overwrite
23593 right overhangs of preceding glyphs for which no glyph
23594 strings exist. */
23595 i = left_overwriting (head);
23596 if (i >= 0)
23597 {
23598 enum draw_glyphs_face overlap_hl;
23599
23600 if (check_mouse_face
23601 && mouse_beg_col < start && mouse_end_col > i)
23602 overlap_hl = DRAW_MOUSE_FACE;
23603 else
23604 overlap_hl = DRAW_NORMAL_TEXT;
23605
23606 clip_head = head;
23607 BUILD_GLYPH_STRINGS (i, start, h, t,
23608 overlap_hl, dummy_x, last_x);
23609 for (s = h; s; s = s->next)
23610 s->background_filled_p = 1;
23611 compute_overhangs_and_x (t, head->x, 1);
23612 prepend_glyph_string_lists (&head, &tail, h, t);
23613 }
23614
23615 /* Append glyphs strings for glyphs following the last glyph
23616 string tail that are overwritten by tail. The background of
23617 these strings has to be drawn because tail's foreground draws
23618 over it. */
23619 i = right_overwritten (tail);
23620 if (i >= 0)
23621 {
23622 enum draw_glyphs_face overlap_hl;
23623
23624 if (check_mouse_face
23625 && mouse_beg_col < i && mouse_end_col > end)
23626 overlap_hl = DRAW_MOUSE_FACE;
23627 else
23628 overlap_hl = DRAW_NORMAL_TEXT;
23629
23630 BUILD_GLYPH_STRINGS (end, i, h, t,
23631 overlap_hl, x, last_x);
23632 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23633 we don't have `end = i;' here. */
23634 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23635 append_glyph_string_lists (&head, &tail, h, t);
23636 clip_tail = tail;
23637 }
23638
23639 /* Append glyph strings for glyphs following the last glyph
23640 string tail that overwrite tail. The foreground of such
23641 glyphs has to be drawn because it writes into the background
23642 of tail. The background must not be drawn because it could
23643 paint over the foreground of following glyphs. */
23644 i = right_overwriting (tail);
23645 if (i >= 0)
23646 {
23647 enum draw_glyphs_face overlap_hl;
23648 if (check_mouse_face
23649 && mouse_beg_col < i && mouse_end_col > end)
23650 overlap_hl = DRAW_MOUSE_FACE;
23651 else
23652 overlap_hl = DRAW_NORMAL_TEXT;
23653
23654 clip_tail = tail;
23655 i++; /* We must include the Ith glyph. */
23656 BUILD_GLYPH_STRINGS (end, i, h, t,
23657 overlap_hl, x, last_x);
23658 for (s = h; s; s = s->next)
23659 s->background_filled_p = 1;
23660 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23661 append_glyph_string_lists (&head, &tail, h, t);
23662 }
23663 if (clip_head || clip_tail)
23664 for (s = head; s; s = s->next)
23665 {
23666 s->clip_head = clip_head;
23667 s->clip_tail = clip_tail;
23668 }
23669 }
23670
23671 /* Draw all strings. */
23672 for (s = head; s; s = s->next)
23673 FRAME_RIF (f)->draw_glyph_string (s);
23674
23675 #ifndef HAVE_NS
23676 /* When focus a sole frame and move horizontally, this sets on_p to 0
23677 causing a failure to erase prev cursor position. */
23678 if (area == TEXT_AREA
23679 && !row->full_width_p
23680 /* When drawing overlapping rows, only the glyph strings'
23681 foreground is drawn, which doesn't erase a cursor
23682 completely. */
23683 && !overlaps)
23684 {
23685 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23686 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23687 : (tail ? tail->x + tail->background_width : x));
23688 x0 -= area_left;
23689 x1 -= area_left;
23690
23691 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23692 row->y, MATRIX_ROW_BOTTOM_Y (row));
23693 }
23694 #endif
23695
23696 /* Value is the x-position up to which drawn, relative to AREA of W.
23697 This doesn't include parts drawn because of overhangs. */
23698 if (row->full_width_p)
23699 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23700 else
23701 x_reached -= area_left;
23702
23703 RELEASE_HDC (hdc, f);
23704
23705 return x_reached;
23706 }
23707
23708 /* Expand row matrix if too narrow. Don't expand if area
23709 is not present. */
23710
23711 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23712 { \
23713 if (!fonts_changed_p \
23714 && (it->glyph_row->glyphs[area] \
23715 < it->glyph_row->glyphs[area + 1])) \
23716 { \
23717 it->w->ncols_scale_factor++; \
23718 fonts_changed_p = 1; \
23719 } \
23720 }
23721
23722 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23723 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23724
23725 static void
23726 append_glyph (struct it *it)
23727 {
23728 struct glyph *glyph;
23729 enum glyph_row_area area = it->area;
23730
23731 eassert (it->glyph_row);
23732 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23733
23734 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23735 if (glyph < it->glyph_row->glyphs[area + 1])
23736 {
23737 /* If the glyph row is reversed, we need to prepend the glyph
23738 rather than append it. */
23739 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23740 {
23741 struct glyph *g;
23742
23743 /* Make room for the additional glyph. */
23744 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23745 g[1] = *g;
23746 glyph = it->glyph_row->glyphs[area];
23747 }
23748 glyph->charpos = CHARPOS (it->position);
23749 glyph->object = it->object;
23750 if (it->pixel_width > 0)
23751 {
23752 glyph->pixel_width = it->pixel_width;
23753 glyph->padding_p = 0;
23754 }
23755 else
23756 {
23757 /* Assure at least 1-pixel width. Otherwise, cursor can't
23758 be displayed correctly. */
23759 glyph->pixel_width = 1;
23760 glyph->padding_p = 1;
23761 }
23762 glyph->ascent = it->ascent;
23763 glyph->descent = it->descent;
23764 glyph->voffset = it->voffset;
23765 glyph->type = CHAR_GLYPH;
23766 glyph->avoid_cursor_p = it->avoid_cursor_p;
23767 glyph->multibyte_p = it->multibyte_p;
23768 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23769 {
23770 /* In R2L rows, the left and the right box edges need to be
23771 drawn in reverse direction. */
23772 glyph->right_box_line_p = it->start_of_box_run_p;
23773 glyph->left_box_line_p = it->end_of_box_run_p;
23774 }
23775 else
23776 {
23777 glyph->left_box_line_p = it->start_of_box_run_p;
23778 glyph->right_box_line_p = it->end_of_box_run_p;
23779 }
23780 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23781 || it->phys_descent > it->descent);
23782 glyph->glyph_not_available_p = it->glyph_not_available_p;
23783 glyph->face_id = it->face_id;
23784 glyph->u.ch = it->char_to_display;
23785 glyph->slice.img = null_glyph_slice;
23786 glyph->font_type = FONT_TYPE_UNKNOWN;
23787 if (it->bidi_p)
23788 {
23789 glyph->resolved_level = it->bidi_it.resolved_level;
23790 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23791 emacs_abort ();
23792 glyph->bidi_type = it->bidi_it.type;
23793 }
23794 else
23795 {
23796 glyph->resolved_level = 0;
23797 glyph->bidi_type = UNKNOWN_BT;
23798 }
23799 ++it->glyph_row->used[area];
23800 }
23801 else
23802 IT_EXPAND_MATRIX_WIDTH (it, area);
23803 }
23804
23805 /* Store one glyph for the composition IT->cmp_it.id in
23806 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23807 non-null. */
23808
23809 static void
23810 append_composite_glyph (struct it *it)
23811 {
23812 struct glyph *glyph;
23813 enum glyph_row_area area = it->area;
23814
23815 eassert (it->glyph_row);
23816
23817 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23818 if (glyph < it->glyph_row->glyphs[area + 1])
23819 {
23820 /* If the glyph row is reversed, we need to prepend the glyph
23821 rather than append it. */
23822 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23823 {
23824 struct glyph *g;
23825
23826 /* Make room for the new glyph. */
23827 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23828 g[1] = *g;
23829 glyph = it->glyph_row->glyphs[it->area];
23830 }
23831 glyph->charpos = it->cmp_it.charpos;
23832 glyph->object = it->object;
23833 glyph->pixel_width = it->pixel_width;
23834 glyph->ascent = it->ascent;
23835 glyph->descent = it->descent;
23836 glyph->voffset = it->voffset;
23837 glyph->type = COMPOSITE_GLYPH;
23838 if (it->cmp_it.ch < 0)
23839 {
23840 glyph->u.cmp.automatic = 0;
23841 glyph->u.cmp.id = it->cmp_it.id;
23842 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23843 }
23844 else
23845 {
23846 glyph->u.cmp.automatic = 1;
23847 glyph->u.cmp.id = it->cmp_it.id;
23848 glyph->slice.cmp.from = it->cmp_it.from;
23849 glyph->slice.cmp.to = it->cmp_it.to - 1;
23850 }
23851 glyph->avoid_cursor_p = it->avoid_cursor_p;
23852 glyph->multibyte_p = it->multibyte_p;
23853 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23854 {
23855 /* In R2L rows, the left and the right box edges need to be
23856 drawn in reverse direction. */
23857 glyph->right_box_line_p = it->start_of_box_run_p;
23858 glyph->left_box_line_p = it->end_of_box_run_p;
23859 }
23860 else
23861 {
23862 glyph->left_box_line_p = it->start_of_box_run_p;
23863 glyph->right_box_line_p = it->end_of_box_run_p;
23864 }
23865 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23866 || it->phys_descent > it->descent);
23867 glyph->padding_p = 0;
23868 glyph->glyph_not_available_p = 0;
23869 glyph->face_id = it->face_id;
23870 glyph->font_type = FONT_TYPE_UNKNOWN;
23871 if (it->bidi_p)
23872 {
23873 glyph->resolved_level = it->bidi_it.resolved_level;
23874 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23875 emacs_abort ();
23876 glyph->bidi_type = it->bidi_it.type;
23877 }
23878 ++it->glyph_row->used[area];
23879 }
23880 else
23881 IT_EXPAND_MATRIX_WIDTH (it, area);
23882 }
23883
23884
23885 /* Change IT->ascent and IT->height according to the setting of
23886 IT->voffset. */
23887
23888 static void
23889 take_vertical_position_into_account (struct it *it)
23890 {
23891 if (it->voffset)
23892 {
23893 if (it->voffset < 0)
23894 /* Increase the ascent so that we can display the text higher
23895 in the line. */
23896 it->ascent -= it->voffset;
23897 else
23898 /* Increase the descent so that we can display the text lower
23899 in the line. */
23900 it->descent += it->voffset;
23901 }
23902 }
23903
23904
23905 /* Produce glyphs/get display metrics for the image IT is loaded with.
23906 See the description of struct display_iterator in dispextern.h for
23907 an overview of struct display_iterator. */
23908
23909 static void
23910 produce_image_glyph (struct it *it)
23911 {
23912 struct image *img;
23913 struct face *face;
23914 int glyph_ascent, crop;
23915 struct glyph_slice slice;
23916
23917 eassert (it->what == IT_IMAGE);
23918
23919 face = FACE_FROM_ID (it->f, it->face_id);
23920 eassert (face);
23921 /* Make sure X resources of the face is loaded. */
23922 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23923
23924 if (it->image_id < 0)
23925 {
23926 /* Fringe bitmap. */
23927 it->ascent = it->phys_ascent = 0;
23928 it->descent = it->phys_descent = 0;
23929 it->pixel_width = 0;
23930 it->nglyphs = 0;
23931 return;
23932 }
23933
23934 img = IMAGE_FROM_ID (it->f, it->image_id);
23935 eassert (img);
23936 /* Make sure X resources of the image is loaded. */
23937 prepare_image_for_display (it->f, img);
23938
23939 slice.x = slice.y = 0;
23940 slice.width = img->width;
23941 slice.height = img->height;
23942
23943 if (INTEGERP (it->slice.x))
23944 slice.x = XINT (it->slice.x);
23945 else if (FLOATP (it->slice.x))
23946 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23947
23948 if (INTEGERP (it->slice.y))
23949 slice.y = XINT (it->slice.y);
23950 else if (FLOATP (it->slice.y))
23951 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23952
23953 if (INTEGERP (it->slice.width))
23954 slice.width = XINT (it->slice.width);
23955 else if (FLOATP (it->slice.width))
23956 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23957
23958 if (INTEGERP (it->slice.height))
23959 slice.height = XINT (it->slice.height);
23960 else if (FLOATP (it->slice.height))
23961 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23962
23963 if (slice.x >= img->width)
23964 slice.x = img->width;
23965 if (slice.y >= img->height)
23966 slice.y = img->height;
23967 if (slice.x + slice.width >= img->width)
23968 slice.width = img->width - slice.x;
23969 if (slice.y + slice.height > img->height)
23970 slice.height = img->height - slice.y;
23971
23972 if (slice.width == 0 || slice.height == 0)
23973 return;
23974
23975 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23976
23977 it->descent = slice.height - glyph_ascent;
23978 if (slice.y == 0)
23979 it->descent += img->vmargin;
23980 if (slice.y + slice.height == img->height)
23981 it->descent += img->vmargin;
23982 it->phys_descent = it->descent;
23983
23984 it->pixel_width = slice.width;
23985 if (slice.x == 0)
23986 it->pixel_width += img->hmargin;
23987 if (slice.x + slice.width == img->width)
23988 it->pixel_width += img->hmargin;
23989
23990 /* It's quite possible for images to have an ascent greater than
23991 their height, so don't get confused in that case. */
23992 if (it->descent < 0)
23993 it->descent = 0;
23994
23995 it->nglyphs = 1;
23996
23997 if (face->box != FACE_NO_BOX)
23998 {
23999 if (face->box_line_width > 0)
24000 {
24001 if (slice.y == 0)
24002 it->ascent += face->box_line_width;
24003 if (slice.y + slice.height == img->height)
24004 it->descent += face->box_line_width;
24005 }
24006
24007 if (it->start_of_box_run_p && slice.x == 0)
24008 it->pixel_width += eabs (face->box_line_width);
24009 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24010 it->pixel_width += eabs (face->box_line_width);
24011 }
24012
24013 take_vertical_position_into_account (it);
24014
24015 /* Automatically crop wide image glyphs at right edge so we can
24016 draw the cursor on same display row. */
24017 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24018 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24019 {
24020 it->pixel_width -= crop;
24021 slice.width -= crop;
24022 }
24023
24024 if (it->glyph_row)
24025 {
24026 struct glyph *glyph;
24027 enum glyph_row_area area = it->area;
24028
24029 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24030 if (glyph < it->glyph_row->glyphs[area + 1])
24031 {
24032 glyph->charpos = CHARPOS (it->position);
24033 glyph->object = it->object;
24034 glyph->pixel_width = it->pixel_width;
24035 glyph->ascent = glyph_ascent;
24036 glyph->descent = it->descent;
24037 glyph->voffset = it->voffset;
24038 glyph->type = IMAGE_GLYPH;
24039 glyph->avoid_cursor_p = it->avoid_cursor_p;
24040 glyph->multibyte_p = it->multibyte_p;
24041 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24042 {
24043 /* In R2L rows, the left and the right box edges need to be
24044 drawn in reverse direction. */
24045 glyph->right_box_line_p = it->start_of_box_run_p;
24046 glyph->left_box_line_p = it->end_of_box_run_p;
24047 }
24048 else
24049 {
24050 glyph->left_box_line_p = it->start_of_box_run_p;
24051 glyph->right_box_line_p = it->end_of_box_run_p;
24052 }
24053 glyph->overlaps_vertically_p = 0;
24054 glyph->padding_p = 0;
24055 glyph->glyph_not_available_p = 0;
24056 glyph->face_id = it->face_id;
24057 glyph->u.img_id = img->id;
24058 glyph->slice.img = slice;
24059 glyph->font_type = FONT_TYPE_UNKNOWN;
24060 if (it->bidi_p)
24061 {
24062 glyph->resolved_level = it->bidi_it.resolved_level;
24063 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24064 emacs_abort ();
24065 glyph->bidi_type = it->bidi_it.type;
24066 }
24067 ++it->glyph_row->used[area];
24068 }
24069 else
24070 IT_EXPAND_MATRIX_WIDTH (it, area);
24071 }
24072 }
24073
24074
24075 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24076 of the glyph, WIDTH and HEIGHT are the width and height of the
24077 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24078
24079 static void
24080 append_stretch_glyph (struct it *it, Lisp_Object object,
24081 int width, int height, int ascent)
24082 {
24083 struct glyph *glyph;
24084 enum glyph_row_area area = it->area;
24085
24086 eassert (ascent >= 0 && ascent <= height);
24087
24088 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24089 if (glyph < it->glyph_row->glyphs[area + 1])
24090 {
24091 /* If the glyph row is reversed, we need to prepend the glyph
24092 rather than append it. */
24093 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24094 {
24095 struct glyph *g;
24096
24097 /* Make room for the additional glyph. */
24098 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24099 g[1] = *g;
24100 glyph = it->glyph_row->glyphs[area];
24101 }
24102 glyph->charpos = CHARPOS (it->position);
24103 glyph->object = object;
24104 glyph->pixel_width = width;
24105 glyph->ascent = ascent;
24106 glyph->descent = height - ascent;
24107 glyph->voffset = it->voffset;
24108 glyph->type = STRETCH_GLYPH;
24109 glyph->avoid_cursor_p = it->avoid_cursor_p;
24110 glyph->multibyte_p = it->multibyte_p;
24111 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24112 {
24113 /* In R2L rows, the left and the right box edges need to be
24114 drawn in reverse direction. */
24115 glyph->right_box_line_p = it->start_of_box_run_p;
24116 glyph->left_box_line_p = it->end_of_box_run_p;
24117 }
24118 else
24119 {
24120 glyph->left_box_line_p = it->start_of_box_run_p;
24121 glyph->right_box_line_p = it->end_of_box_run_p;
24122 }
24123 glyph->overlaps_vertically_p = 0;
24124 glyph->padding_p = 0;
24125 glyph->glyph_not_available_p = 0;
24126 glyph->face_id = it->face_id;
24127 glyph->u.stretch.ascent = ascent;
24128 glyph->u.stretch.height = height;
24129 glyph->slice.img = null_glyph_slice;
24130 glyph->font_type = FONT_TYPE_UNKNOWN;
24131 if (it->bidi_p)
24132 {
24133 glyph->resolved_level = it->bidi_it.resolved_level;
24134 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24135 emacs_abort ();
24136 glyph->bidi_type = it->bidi_it.type;
24137 }
24138 else
24139 {
24140 glyph->resolved_level = 0;
24141 glyph->bidi_type = UNKNOWN_BT;
24142 }
24143 ++it->glyph_row->used[area];
24144 }
24145 else
24146 IT_EXPAND_MATRIX_WIDTH (it, area);
24147 }
24148
24149 #endif /* HAVE_WINDOW_SYSTEM */
24150
24151 /* Produce a stretch glyph for iterator IT. IT->object is the value
24152 of the glyph property displayed. The value must be a list
24153 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24154 being recognized:
24155
24156 1. `:width WIDTH' specifies that the space should be WIDTH *
24157 canonical char width wide. WIDTH may be an integer or floating
24158 point number.
24159
24160 2. `:relative-width FACTOR' specifies that the width of the stretch
24161 should be computed from the width of the first character having the
24162 `glyph' property, and should be FACTOR times that width.
24163
24164 3. `:align-to HPOS' specifies that the space should be wide enough
24165 to reach HPOS, a value in canonical character units.
24166
24167 Exactly one of the above pairs must be present.
24168
24169 4. `:height HEIGHT' specifies that the height of the stretch produced
24170 should be HEIGHT, measured in canonical character units.
24171
24172 5. `:relative-height FACTOR' specifies that the height of the
24173 stretch should be FACTOR times the height of the characters having
24174 the glyph property.
24175
24176 Either none or exactly one of 4 or 5 must be present.
24177
24178 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24179 of the stretch should be used for the ascent of the stretch.
24180 ASCENT must be in the range 0 <= ASCENT <= 100. */
24181
24182 void
24183 produce_stretch_glyph (struct it *it)
24184 {
24185 /* (space :width WIDTH :height HEIGHT ...) */
24186 Lisp_Object prop, plist;
24187 int width = 0, height = 0, align_to = -1;
24188 int zero_width_ok_p = 0;
24189 double tem;
24190 struct font *font = NULL;
24191
24192 #ifdef HAVE_WINDOW_SYSTEM
24193 int ascent = 0;
24194 int zero_height_ok_p = 0;
24195
24196 if (FRAME_WINDOW_P (it->f))
24197 {
24198 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24199 font = face->font ? face->font : FRAME_FONT (it->f);
24200 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24201 }
24202 #endif
24203
24204 /* List should start with `space'. */
24205 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24206 plist = XCDR (it->object);
24207
24208 /* Compute the width of the stretch. */
24209 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24210 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24211 {
24212 /* Absolute width `:width WIDTH' specified and valid. */
24213 zero_width_ok_p = 1;
24214 width = (int)tem;
24215 }
24216 #ifdef HAVE_WINDOW_SYSTEM
24217 else if (FRAME_WINDOW_P (it->f)
24218 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24219 {
24220 /* Relative width `:relative-width FACTOR' specified and valid.
24221 Compute the width of the characters having the `glyph'
24222 property. */
24223 struct it it2;
24224 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24225
24226 it2 = *it;
24227 if (it->multibyte_p)
24228 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24229 else
24230 {
24231 it2.c = it2.char_to_display = *p, it2.len = 1;
24232 if (! ASCII_CHAR_P (it2.c))
24233 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24234 }
24235
24236 it2.glyph_row = NULL;
24237 it2.what = IT_CHARACTER;
24238 x_produce_glyphs (&it2);
24239 width = NUMVAL (prop) * it2.pixel_width;
24240 }
24241 #endif /* HAVE_WINDOW_SYSTEM */
24242 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24243 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24244 {
24245 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24246 align_to = (align_to < 0
24247 ? 0
24248 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24249 else if (align_to < 0)
24250 align_to = window_box_left_offset (it->w, TEXT_AREA);
24251 width = max (0, (int)tem + align_to - it->current_x);
24252 zero_width_ok_p = 1;
24253 }
24254 else
24255 /* Nothing specified -> width defaults to canonical char width. */
24256 width = FRAME_COLUMN_WIDTH (it->f);
24257
24258 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24259 width = 1;
24260
24261 #ifdef HAVE_WINDOW_SYSTEM
24262 /* Compute height. */
24263 if (FRAME_WINDOW_P (it->f))
24264 {
24265 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24266 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24267 {
24268 height = (int)tem;
24269 zero_height_ok_p = 1;
24270 }
24271 else if (prop = Fplist_get (plist, QCrelative_height),
24272 NUMVAL (prop) > 0)
24273 height = FONT_HEIGHT (font) * NUMVAL (prop);
24274 else
24275 height = FONT_HEIGHT (font);
24276
24277 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24278 height = 1;
24279
24280 /* Compute percentage of height used for ascent. If
24281 `:ascent ASCENT' is present and valid, use that. Otherwise,
24282 derive the ascent from the font in use. */
24283 if (prop = Fplist_get (plist, QCascent),
24284 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24285 ascent = height * NUMVAL (prop) / 100.0;
24286 else if (!NILP (prop)
24287 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24288 ascent = min (max (0, (int)tem), height);
24289 else
24290 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24291 }
24292 else
24293 #endif /* HAVE_WINDOW_SYSTEM */
24294 height = 1;
24295
24296 if (width > 0 && it->line_wrap != TRUNCATE
24297 && it->current_x + width > it->last_visible_x)
24298 {
24299 width = it->last_visible_x - it->current_x;
24300 #ifdef HAVE_WINDOW_SYSTEM
24301 /* Subtract one more pixel from the stretch width, but only on
24302 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24303 width -= FRAME_WINDOW_P (it->f);
24304 #endif
24305 }
24306
24307 if (width > 0 && height > 0 && it->glyph_row)
24308 {
24309 Lisp_Object o_object = it->object;
24310 Lisp_Object object = it->stack[it->sp - 1].string;
24311 int n = width;
24312
24313 if (!STRINGP (object))
24314 object = it->w->buffer;
24315 #ifdef HAVE_WINDOW_SYSTEM
24316 if (FRAME_WINDOW_P (it->f))
24317 append_stretch_glyph (it, object, width, height, ascent);
24318 else
24319 #endif
24320 {
24321 it->object = object;
24322 it->char_to_display = ' ';
24323 it->pixel_width = it->len = 1;
24324 while (n--)
24325 tty_append_glyph (it);
24326 it->object = o_object;
24327 }
24328 }
24329
24330 it->pixel_width = width;
24331 #ifdef HAVE_WINDOW_SYSTEM
24332 if (FRAME_WINDOW_P (it->f))
24333 {
24334 it->ascent = it->phys_ascent = ascent;
24335 it->descent = it->phys_descent = height - it->ascent;
24336 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24337 take_vertical_position_into_account (it);
24338 }
24339 else
24340 #endif
24341 it->nglyphs = width;
24342 }
24343
24344 /* Get information about special display element WHAT in an
24345 environment described by IT. WHAT is one of IT_TRUNCATION or
24346 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24347 non-null glyph_row member. This function ensures that fields like
24348 face_id, c, len of IT are left untouched. */
24349
24350 static void
24351 produce_special_glyphs (struct it *it, enum display_element_type what)
24352 {
24353 struct it temp_it;
24354 Lisp_Object gc;
24355 GLYPH glyph;
24356
24357 temp_it = *it;
24358 temp_it.object = make_number (0);
24359 memset (&temp_it.current, 0, sizeof temp_it.current);
24360
24361 if (what == IT_CONTINUATION)
24362 {
24363 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24364 if (it->bidi_it.paragraph_dir == R2L)
24365 SET_GLYPH_FROM_CHAR (glyph, '/');
24366 else
24367 SET_GLYPH_FROM_CHAR (glyph, '\\');
24368 if (it->dp
24369 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24370 {
24371 /* FIXME: Should we mirror GC for R2L lines? */
24372 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24373 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24374 }
24375 }
24376 else if (what == IT_TRUNCATION)
24377 {
24378 /* Truncation glyph. */
24379 SET_GLYPH_FROM_CHAR (glyph, '$');
24380 if (it->dp
24381 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24382 {
24383 /* FIXME: Should we mirror GC for R2L lines? */
24384 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24385 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24386 }
24387 }
24388 else
24389 emacs_abort ();
24390
24391 #ifdef HAVE_WINDOW_SYSTEM
24392 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24393 is turned off, we precede the truncation/continuation glyphs by a
24394 stretch glyph whose width is computed such that these special
24395 glyphs are aligned at the window margin, even when very different
24396 fonts are used in different glyph rows. */
24397 if (FRAME_WINDOW_P (temp_it.f)
24398 /* init_iterator calls this with it->glyph_row == NULL, and it
24399 wants only the pixel width of the truncation/continuation
24400 glyphs. */
24401 && temp_it.glyph_row
24402 /* insert_left_trunc_glyphs calls us at the beginning of the
24403 row, and it has its own calculation of the stretch glyph
24404 width. */
24405 && temp_it.glyph_row->used[TEXT_AREA] > 0
24406 && (temp_it.glyph_row->reversed_p
24407 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24408 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24409 {
24410 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24411
24412 if (stretch_width > 0)
24413 {
24414 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24415 struct font *font =
24416 face->font ? face->font : FRAME_FONT (temp_it.f);
24417 int stretch_ascent =
24418 (((temp_it.ascent + temp_it.descent)
24419 * FONT_BASE (font)) / FONT_HEIGHT (font));
24420
24421 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24422 temp_it.ascent + temp_it.descent,
24423 stretch_ascent);
24424 }
24425 }
24426 #endif
24427
24428 temp_it.dp = NULL;
24429 temp_it.what = IT_CHARACTER;
24430 temp_it.len = 1;
24431 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24432 temp_it.face_id = GLYPH_FACE (glyph);
24433 temp_it.len = CHAR_BYTES (temp_it.c);
24434
24435 PRODUCE_GLYPHS (&temp_it);
24436 it->pixel_width = temp_it.pixel_width;
24437 it->nglyphs = temp_it.pixel_width;
24438 }
24439
24440 #ifdef HAVE_WINDOW_SYSTEM
24441
24442 /* Calculate line-height and line-spacing properties.
24443 An integer value specifies explicit pixel value.
24444 A float value specifies relative value to current face height.
24445 A cons (float . face-name) specifies relative value to
24446 height of specified face font.
24447
24448 Returns height in pixels, or nil. */
24449
24450
24451 static Lisp_Object
24452 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24453 int boff, int override)
24454 {
24455 Lisp_Object face_name = Qnil;
24456 int ascent, descent, height;
24457
24458 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24459 return val;
24460
24461 if (CONSP (val))
24462 {
24463 face_name = XCAR (val);
24464 val = XCDR (val);
24465 if (!NUMBERP (val))
24466 val = make_number (1);
24467 if (NILP (face_name))
24468 {
24469 height = it->ascent + it->descent;
24470 goto scale;
24471 }
24472 }
24473
24474 if (NILP (face_name))
24475 {
24476 font = FRAME_FONT (it->f);
24477 boff = FRAME_BASELINE_OFFSET (it->f);
24478 }
24479 else if (EQ (face_name, Qt))
24480 {
24481 override = 0;
24482 }
24483 else
24484 {
24485 int face_id;
24486 struct face *face;
24487
24488 face_id = lookup_named_face (it->f, face_name, 0);
24489 if (face_id < 0)
24490 return make_number (-1);
24491
24492 face = FACE_FROM_ID (it->f, face_id);
24493 font = face->font;
24494 if (font == NULL)
24495 return make_number (-1);
24496 boff = font->baseline_offset;
24497 if (font->vertical_centering)
24498 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24499 }
24500
24501 ascent = FONT_BASE (font) + boff;
24502 descent = FONT_DESCENT (font) - boff;
24503
24504 if (override)
24505 {
24506 it->override_ascent = ascent;
24507 it->override_descent = descent;
24508 it->override_boff = boff;
24509 }
24510
24511 height = ascent + descent;
24512
24513 scale:
24514 if (FLOATP (val))
24515 height = (int)(XFLOAT_DATA (val) * height);
24516 else if (INTEGERP (val))
24517 height *= XINT (val);
24518
24519 return make_number (height);
24520 }
24521
24522
24523 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24524 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24525 and only if this is for a character for which no font was found.
24526
24527 If the display method (it->glyphless_method) is
24528 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24529 length of the acronym or the hexadecimal string, UPPER_XOFF and
24530 UPPER_YOFF are pixel offsets for the upper part of the string,
24531 LOWER_XOFF and LOWER_YOFF are for the lower part.
24532
24533 For the other display methods, LEN through LOWER_YOFF are zero. */
24534
24535 static void
24536 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24537 short upper_xoff, short upper_yoff,
24538 short lower_xoff, short lower_yoff)
24539 {
24540 struct glyph *glyph;
24541 enum glyph_row_area area = it->area;
24542
24543 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24544 if (glyph < it->glyph_row->glyphs[area + 1])
24545 {
24546 /* If the glyph row is reversed, we need to prepend the glyph
24547 rather than append it. */
24548 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24549 {
24550 struct glyph *g;
24551
24552 /* Make room for the additional glyph. */
24553 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24554 g[1] = *g;
24555 glyph = it->glyph_row->glyphs[area];
24556 }
24557 glyph->charpos = CHARPOS (it->position);
24558 glyph->object = it->object;
24559 glyph->pixel_width = it->pixel_width;
24560 glyph->ascent = it->ascent;
24561 glyph->descent = it->descent;
24562 glyph->voffset = it->voffset;
24563 glyph->type = GLYPHLESS_GLYPH;
24564 glyph->u.glyphless.method = it->glyphless_method;
24565 glyph->u.glyphless.for_no_font = for_no_font;
24566 glyph->u.glyphless.len = len;
24567 glyph->u.glyphless.ch = it->c;
24568 glyph->slice.glyphless.upper_xoff = upper_xoff;
24569 glyph->slice.glyphless.upper_yoff = upper_yoff;
24570 glyph->slice.glyphless.lower_xoff = lower_xoff;
24571 glyph->slice.glyphless.lower_yoff = lower_yoff;
24572 glyph->avoid_cursor_p = it->avoid_cursor_p;
24573 glyph->multibyte_p = it->multibyte_p;
24574 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24575 {
24576 /* In R2L rows, the left and the right box edges need to be
24577 drawn in reverse direction. */
24578 glyph->right_box_line_p = it->start_of_box_run_p;
24579 glyph->left_box_line_p = it->end_of_box_run_p;
24580 }
24581 else
24582 {
24583 glyph->left_box_line_p = it->start_of_box_run_p;
24584 glyph->right_box_line_p = it->end_of_box_run_p;
24585 }
24586 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24587 || it->phys_descent > it->descent);
24588 glyph->padding_p = 0;
24589 glyph->glyph_not_available_p = 0;
24590 glyph->face_id = face_id;
24591 glyph->font_type = FONT_TYPE_UNKNOWN;
24592 if (it->bidi_p)
24593 {
24594 glyph->resolved_level = it->bidi_it.resolved_level;
24595 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24596 emacs_abort ();
24597 glyph->bidi_type = it->bidi_it.type;
24598 }
24599 ++it->glyph_row->used[area];
24600 }
24601 else
24602 IT_EXPAND_MATRIX_WIDTH (it, area);
24603 }
24604
24605
24606 /* Produce a glyph for a glyphless character for iterator IT.
24607 IT->glyphless_method specifies which method to use for displaying
24608 the character. See the description of enum
24609 glyphless_display_method in dispextern.h for the detail.
24610
24611 FOR_NO_FONT is nonzero if and only if this is for a character for
24612 which no font was found. ACRONYM, if non-nil, is an acronym string
24613 for the character. */
24614
24615 static void
24616 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24617 {
24618 int face_id;
24619 struct face *face;
24620 struct font *font;
24621 int base_width, base_height, width, height;
24622 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24623 int len;
24624
24625 /* Get the metrics of the base font. We always refer to the current
24626 ASCII face. */
24627 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24628 font = face->font ? face->font : FRAME_FONT (it->f);
24629 it->ascent = FONT_BASE (font) + font->baseline_offset;
24630 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24631 base_height = it->ascent + it->descent;
24632 base_width = font->average_width;
24633
24634 /* Get a face ID for the glyph by utilizing a cache (the same way as
24635 done for `escape-glyph' in get_next_display_element). */
24636 if (it->f == last_glyphless_glyph_frame
24637 && it->face_id == last_glyphless_glyph_face_id)
24638 {
24639 face_id = last_glyphless_glyph_merged_face_id;
24640 }
24641 else
24642 {
24643 /* Merge the `glyphless-char' face into the current face. */
24644 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24645 last_glyphless_glyph_frame = it->f;
24646 last_glyphless_glyph_face_id = it->face_id;
24647 last_glyphless_glyph_merged_face_id = face_id;
24648 }
24649
24650 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24651 {
24652 it->pixel_width = THIN_SPACE_WIDTH;
24653 len = 0;
24654 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24655 }
24656 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24657 {
24658 width = CHAR_WIDTH (it->c);
24659 if (width == 0)
24660 width = 1;
24661 else if (width > 4)
24662 width = 4;
24663 it->pixel_width = base_width * width;
24664 len = 0;
24665 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24666 }
24667 else
24668 {
24669 char buf[7];
24670 const char *str;
24671 unsigned int code[6];
24672 int upper_len;
24673 int ascent, descent;
24674 struct font_metrics metrics_upper, metrics_lower;
24675
24676 face = FACE_FROM_ID (it->f, face_id);
24677 font = face->font ? face->font : FRAME_FONT (it->f);
24678 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24679
24680 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24681 {
24682 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24683 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24684 if (CONSP (acronym))
24685 acronym = XCAR (acronym);
24686 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24687 }
24688 else
24689 {
24690 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24691 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24692 str = buf;
24693 }
24694 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24695 code[len] = font->driver->encode_char (font, str[len]);
24696 upper_len = (len + 1) / 2;
24697 font->driver->text_extents (font, code, upper_len,
24698 &metrics_upper);
24699 font->driver->text_extents (font, code + upper_len, len - upper_len,
24700 &metrics_lower);
24701
24702
24703
24704 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24705 width = max (metrics_upper.width, metrics_lower.width) + 4;
24706 upper_xoff = upper_yoff = 2; /* the typical case */
24707 if (base_width >= width)
24708 {
24709 /* Align the upper to the left, the lower to the right. */
24710 it->pixel_width = base_width;
24711 lower_xoff = base_width - 2 - metrics_lower.width;
24712 }
24713 else
24714 {
24715 /* Center the shorter one. */
24716 it->pixel_width = width;
24717 if (metrics_upper.width >= metrics_lower.width)
24718 lower_xoff = (width - metrics_lower.width) / 2;
24719 else
24720 {
24721 /* FIXME: This code doesn't look right. It formerly was
24722 missing the "lower_xoff = 0;", which couldn't have
24723 been right since it left lower_xoff uninitialized. */
24724 lower_xoff = 0;
24725 upper_xoff = (width - metrics_upper.width) / 2;
24726 }
24727 }
24728
24729 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24730 top, bottom, and between upper and lower strings. */
24731 height = (metrics_upper.ascent + metrics_upper.descent
24732 + metrics_lower.ascent + metrics_lower.descent) + 5;
24733 /* Center vertically.
24734 H:base_height, D:base_descent
24735 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24736
24737 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24738 descent = D - H/2 + h/2;
24739 lower_yoff = descent - 2 - ld;
24740 upper_yoff = lower_yoff - la - 1 - ud; */
24741 ascent = - (it->descent - (base_height + height + 1) / 2);
24742 descent = it->descent - (base_height - height) / 2;
24743 lower_yoff = descent - 2 - metrics_lower.descent;
24744 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24745 - metrics_upper.descent);
24746 /* Don't make the height shorter than the base height. */
24747 if (height > base_height)
24748 {
24749 it->ascent = ascent;
24750 it->descent = descent;
24751 }
24752 }
24753
24754 it->phys_ascent = it->ascent;
24755 it->phys_descent = it->descent;
24756 if (it->glyph_row)
24757 append_glyphless_glyph (it, face_id, for_no_font, len,
24758 upper_xoff, upper_yoff,
24759 lower_xoff, lower_yoff);
24760 it->nglyphs = 1;
24761 take_vertical_position_into_account (it);
24762 }
24763
24764
24765 /* RIF:
24766 Produce glyphs/get display metrics for the display element IT is
24767 loaded with. See the description of struct it in dispextern.h
24768 for an overview of struct it. */
24769
24770 void
24771 x_produce_glyphs (struct it *it)
24772 {
24773 int extra_line_spacing = it->extra_line_spacing;
24774
24775 it->glyph_not_available_p = 0;
24776
24777 if (it->what == IT_CHARACTER)
24778 {
24779 XChar2b char2b;
24780 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24781 struct font *font = face->font;
24782 struct font_metrics *pcm = NULL;
24783 int boff; /* baseline offset */
24784
24785 if (font == NULL)
24786 {
24787 /* When no suitable font is found, display this character by
24788 the method specified in the first extra slot of
24789 Vglyphless_char_display. */
24790 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24791
24792 eassert (it->what == IT_GLYPHLESS);
24793 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24794 goto done;
24795 }
24796
24797 boff = font->baseline_offset;
24798 if (font->vertical_centering)
24799 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24800
24801 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24802 {
24803 int stretched_p;
24804
24805 it->nglyphs = 1;
24806
24807 if (it->override_ascent >= 0)
24808 {
24809 it->ascent = it->override_ascent;
24810 it->descent = it->override_descent;
24811 boff = it->override_boff;
24812 }
24813 else
24814 {
24815 it->ascent = FONT_BASE (font) + boff;
24816 it->descent = FONT_DESCENT (font) - boff;
24817 }
24818
24819 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24820 {
24821 pcm = get_per_char_metric (font, &char2b);
24822 if (pcm->width == 0
24823 && pcm->rbearing == 0 && pcm->lbearing == 0)
24824 pcm = NULL;
24825 }
24826
24827 if (pcm)
24828 {
24829 it->phys_ascent = pcm->ascent + boff;
24830 it->phys_descent = pcm->descent - boff;
24831 it->pixel_width = pcm->width;
24832 }
24833 else
24834 {
24835 it->glyph_not_available_p = 1;
24836 it->phys_ascent = it->ascent;
24837 it->phys_descent = it->descent;
24838 it->pixel_width = font->space_width;
24839 }
24840
24841 if (it->constrain_row_ascent_descent_p)
24842 {
24843 if (it->descent > it->max_descent)
24844 {
24845 it->ascent += it->descent - it->max_descent;
24846 it->descent = it->max_descent;
24847 }
24848 if (it->ascent > it->max_ascent)
24849 {
24850 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24851 it->ascent = it->max_ascent;
24852 }
24853 it->phys_ascent = min (it->phys_ascent, it->ascent);
24854 it->phys_descent = min (it->phys_descent, it->descent);
24855 extra_line_spacing = 0;
24856 }
24857
24858 /* If this is a space inside a region of text with
24859 `space-width' property, change its width. */
24860 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24861 if (stretched_p)
24862 it->pixel_width *= XFLOATINT (it->space_width);
24863
24864 /* If face has a box, add the box thickness to the character
24865 height. If character has a box line to the left and/or
24866 right, add the box line width to the character's width. */
24867 if (face->box != FACE_NO_BOX)
24868 {
24869 int thick = face->box_line_width;
24870
24871 if (thick > 0)
24872 {
24873 it->ascent += thick;
24874 it->descent += thick;
24875 }
24876 else
24877 thick = -thick;
24878
24879 if (it->start_of_box_run_p)
24880 it->pixel_width += thick;
24881 if (it->end_of_box_run_p)
24882 it->pixel_width += thick;
24883 }
24884
24885 /* If face has an overline, add the height of the overline
24886 (1 pixel) and a 1 pixel margin to the character height. */
24887 if (face->overline_p)
24888 it->ascent += overline_margin;
24889
24890 if (it->constrain_row_ascent_descent_p)
24891 {
24892 if (it->ascent > it->max_ascent)
24893 it->ascent = it->max_ascent;
24894 if (it->descent > it->max_descent)
24895 it->descent = it->max_descent;
24896 }
24897
24898 take_vertical_position_into_account (it);
24899
24900 /* If we have to actually produce glyphs, do it. */
24901 if (it->glyph_row)
24902 {
24903 if (stretched_p)
24904 {
24905 /* Translate a space with a `space-width' property
24906 into a stretch glyph. */
24907 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24908 / FONT_HEIGHT (font));
24909 append_stretch_glyph (it, it->object, it->pixel_width,
24910 it->ascent + it->descent, ascent);
24911 }
24912 else
24913 append_glyph (it);
24914
24915 /* If characters with lbearing or rbearing are displayed
24916 in this line, record that fact in a flag of the
24917 glyph row. This is used to optimize X output code. */
24918 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24919 it->glyph_row->contains_overlapping_glyphs_p = 1;
24920 }
24921 if (! stretched_p && it->pixel_width == 0)
24922 /* We assure that all visible glyphs have at least 1-pixel
24923 width. */
24924 it->pixel_width = 1;
24925 }
24926 else if (it->char_to_display == '\n')
24927 {
24928 /* A newline has no width, but we need the height of the
24929 line. But if previous part of the line sets a height,
24930 don't increase that height */
24931
24932 Lisp_Object height;
24933 Lisp_Object total_height = Qnil;
24934
24935 it->override_ascent = -1;
24936 it->pixel_width = 0;
24937 it->nglyphs = 0;
24938
24939 height = get_it_property (it, Qline_height);
24940 /* Split (line-height total-height) list */
24941 if (CONSP (height)
24942 && CONSP (XCDR (height))
24943 && NILP (XCDR (XCDR (height))))
24944 {
24945 total_height = XCAR (XCDR (height));
24946 height = XCAR (height);
24947 }
24948 height = calc_line_height_property (it, height, font, boff, 1);
24949
24950 if (it->override_ascent >= 0)
24951 {
24952 it->ascent = it->override_ascent;
24953 it->descent = it->override_descent;
24954 boff = it->override_boff;
24955 }
24956 else
24957 {
24958 it->ascent = FONT_BASE (font) + boff;
24959 it->descent = FONT_DESCENT (font) - boff;
24960 }
24961
24962 if (EQ (height, Qt))
24963 {
24964 if (it->descent > it->max_descent)
24965 {
24966 it->ascent += it->descent - it->max_descent;
24967 it->descent = it->max_descent;
24968 }
24969 if (it->ascent > it->max_ascent)
24970 {
24971 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24972 it->ascent = it->max_ascent;
24973 }
24974 it->phys_ascent = min (it->phys_ascent, it->ascent);
24975 it->phys_descent = min (it->phys_descent, it->descent);
24976 it->constrain_row_ascent_descent_p = 1;
24977 extra_line_spacing = 0;
24978 }
24979 else
24980 {
24981 Lisp_Object spacing;
24982
24983 it->phys_ascent = it->ascent;
24984 it->phys_descent = it->descent;
24985
24986 if ((it->max_ascent > 0 || it->max_descent > 0)
24987 && face->box != FACE_NO_BOX
24988 && face->box_line_width > 0)
24989 {
24990 it->ascent += face->box_line_width;
24991 it->descent += face->box_line_width;
24992 }
24993 if (!NILP (height)
24994 && XINT (height) > it->ascent + it->descent)
24995 it->ascent = XINT (height) - it->descent;
24996
24997 if (!NILP (total_height))
24998 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24999 else
25000 {
25001 spacing = get_it_property (it, Qline_spacing);
25002 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25003 }
25004 if (INTEGERP (spacing))
25005 {
25006 extra_line_spacing = XINT (spacing);
25007 if (!NILP (total_height))
25008 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25009 }
25010 }
25011 }
25012 else /* i.e. (it->char_to_display == '\t') */
25013 {
25014 if (font->space_width > 0)
25015 {
25016 int tab_width = it->tab_width * font->space_width;
25017 int x = it->current_x + it->continuation_lines_width;
25018 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25019
25020 /* If the distance from the current position to the next tab
25021 stop is less than a space character width, use the
25022 tab stop after that. */
25023 if (next_tab_x - x < font->space_width)
25024 next_tab_x += tab_width;
25025
25026 it->pixel_width = next_tab_x - x;
25027 it->nglyphs = 1;
25028 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25029 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25030
25031 if (it->glyph_row)
25032 {
25033 append_stretch_glyph (it, it->object, it->pixel_width,
25034 it->ascent + it->descent, it->ascent);
25035 }
25036 }
25037 else
25038 {
25039 it->pixel_width = 0;
25040 it->nglyphs = 1;
25041 }
25042 }
25043 }
25044 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25045 {
25046 /* A static composition.
25047
25048 Note: A composition is represented as one glyph in the
25049 glyph matrix. There are no padding glyphs.
25050
25051 Important note: pixel_width, ascent, and descent are the
25052 values of what is drawn by draw_glyphs (i.e. the values of
25053 the overall glyphs composed). */
25054 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25055 int boff; /* baseline offset */
25056 struct composition *cmp = composition_table[it->cmp_it.id];
25057 int glyph_len = cmp->glyph_len;
25058 struct font *font = face->font;
25059
25060 it->nglyphs = 1;
25061
25062 /* If we have not yet calculated pixel size data of glyphs of
25063 the composition for the current face font, calculate them
25064 now. Theoretically, we have to check all fonts for the
25065 glyphs, but that requires much time and memory space. So,
25066 here we check only the font of the first glyph. This may
25067 lead to incorrect display, but it's very rare, and C-l
25068 (recenter-top-bottom) can correct the display anyway. */
25069 if (! cmp->font || cmp->font != font)
25070 {
25071 /* Ascent and descent of the font of the first character
25072 of this composition (adjusted by baseline offset).
25073 Ascent and descent of overall glyphs should not be less
25074 than these, respectively. */
25075 int font_ascent, font_descent, font_height;
25076 /* Bounding box of the overall glyphs. */
25077 int leftmost, rightmost, lowest, highest;
25078 int lbearing, rbearing;
25079 int i, width, ascent, descent;
25080 int left_padded = 0, right_padded = 0;
25081 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25082 XChar2b char2b;
25083 struct font_metrics *pcm;
25084 int font_not_found_p;
25085 ptrdiff_t pos;
25086
25087 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25088 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25089 break;
25090 if (glyph_len < cmp->glyph_len)
25091 right_padded = 1;
25092 for (i = 0; i < glyph_len; i++)
25093 {
25094 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25095 break;
25096 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25097 }
25098 if (i > 0)
25099 left_padded = 1;
25100
25101 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25102 : IT_CHARPOS (*it));
25103 /* If no suitable font is found, use the default font. */
25104 font_not_found_p = font == NULL;
25105 if (font_not_found_p)
25106 {
25107 face = face->ascii_face;
25108 font = face->font;
25109 }
25110 boff = font->baseline_offset;
25111 if (font->vertical_centering)
25112 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25113 font_ascent = FONT_BASE (font) + boff;
25114 font_descent = FONT_DESCENT (font) - boff;
25115 font_height = FONT_HEIGHT (font);
25116
25117 cmp->font = font;
25118
25119 pcm = NULL;
25120 if (! font_not_found_p)
25121 {
25122 get_char_face_and_encoding (it->f, c, it->face_id,
25123 &char2b, 0);
25124 pcm = get_per_char_metric (font, &char2b);
25125 }
25126
25127 /* Initialize the bounding box. */
25128 if (pcm)
25129 {
25130 width = cmp->glyph_len > 0 ? pcm->width : 0;
25131 ascent = pcm->ascent;
25132 descent = pcm->descent;
25133 lbearing = pcm->lbearing;
25134 rbearing = pcm->rbearing;
25135 }
25136 else
25137 {
25138 width = cmp->glyph_len > 0 ? font->space_width : 0;
25139 ascent = FONT_BASE (font);
25140 descent = FONT_DESCENT (font);
25141 lbearing = 0;
25142 rbearing = width;
25143 }
25144
25145 rightmost = width;
25146 leftmost = 0;
25147 lowest = - descent + boff;
25148 highest = ascent + boff;
25149
25150 if (! font_not_found_p
25151 && font->default_ascent
25152 && CHAR_TABLE_P (Vuse_default_ascent)
25153 && !NILP (Faref (Vuse_default_ascent,
25154 make_number (it->char_to_display))))
25155 highest = font->default_ascent + boff;
25156
25157 /* Draw the first glyph at the normal position. It may be
25158 shifted to right later if some other glyphs are drawn
25159 at the left. */
25160 cmp->offsets[i * 2] = 0;
25161 cmp->offsets[i * 2 + 1] = boff;
25162 cmp->lbearing = lbearing;
25163 cmp->rbearing = rbearing;
25164
25165 /* Set cmp->offsets for the remaining glyphs. */
25166 for (i++; i < glyph_len; i++)
25167 {
25168 int left, right, btm, top;
25169 int ch = COMPOSITION_GLYPH (cmp, i);
25170 int face_id;
25171 struct face *this_face;
25172
25173 if (ch == '\t')
25174 ch = ' ';
25175 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25176 this_face = FACE_FROM_ID (it->f, face_id);
25177 font = this_face->font;
25178
25179 if (font == NULL)
25180 pcm = NULL;
25181 else
25182 {
25183 get_char_face_and_encoding (it->f, ch, face_id,
25184 &char2b, 0);
25185 pcm = get_per_char_metric (font, &char2b);
25186 }
25187 if (! pcm)
25188 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25189 else
25190 {
25191 width = pcm->width;
25192 ascent = pcm->ascent;
25193 descent = pcm->descent;
25194 lbearing = pcm->lbearing;
25195 rbearing = pcm->rbearing;
25196 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25197 {
25198 /* Relative composition with or without
25199 alternate chars. */
25200 left = (leftmost + rightmost - width) / 2;
25201 btm = - descent + boff;
25202 if (font->relative_compose
25203 && (! CHAR_TABLE_P (Vignore_relative_composition)
25204 || NILP (Faref (Vignore_relative_composition,
25205 make_number (ch)))))
25206 {
25207
25208 if (- descent >= font->relative_compose)
25209 /* One extra pixel between two glyphs. */
25210 btm = highest + 1;
25211 else if (ascent <= 0)
25212 /* One extra pixel between two glyphs. */
25213 btm = lowest - 1 - ascent - descent;
25214 }
25215 }
25216 else
25217 {
25218 /* A composition rule is specified by an integer
25219 value that encodes global and new reference
25220 points (GREF and NREF). GREF and NREF are
25221 specified by numbers as below:
25222
25223 0---1---2 -- ascent
25224 | |
25225 | |
25226 | |
25227 9--10--11 -- center
25228 | |
25229 ---3---4---5--- baseline
25230 | |
25231 6---7---8 -- descent
25232 */
25233 int rule = COMPOSITION_RULE (cmp, i);
25234 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25235
25236 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25237 grefx = gref % 3, nrefx = nref % 3;
25238 grefy = gref / 3, nrefy = nref / 3;
25239 if (xoff)
25240 xoff = font_height * (xoff - 128) / 256;
25241 if (yoff)
25242 yoff = font_height * (yoff - 128) / 256;
25243
25244 left = (leftmost
25245 + grefx * (rightmost - leftmost) / 2
25246 - nrefx * width / 2
25247 + xoff);
25248
25249 btm = ((grefy == 0 ? highest
25250 : grefy == 1 ? 0
25251 : grefy == 2 ? lowest
25252 : (highest + lowest) / 2)
25253 - (nrefy == 0 ? ascent + descent
25254 : nrefy == 1 ? descent - boff
25255 : nrefy == 2 ? 0
25256 : (ascent + descent) / 2)
25257 + yoff);
25258 }
25259
25260 cmp->offsets[i * 2] = left;
25261 cmp->offsets[i * 2 + 1] = btm + descent;
25262
25263 /* Update the bounding box of the overall glyphs. */
25264 if (width > 0)
25265 {
25266 right = left + width;
25267 if (left < leftmost)
25268 leftmost = left;
25269 if (right > rightmost)
25270 rightmost = right;
25271 }
25272 top = btm + descent + ascent;
25273 if (top > highest)
25274 highest = top;
25275 if (btm < lowest)
25276 lowest = btm;
25277
25278 if (cmp->lbearing > left + lbearing)
25279 cmp->lbearing = left + lbearing;
25280 if (cmp->rbearing < left + rbearing)
25281 cmp->rbearing = left + rbearing;
25282 }
25283 }
25284
25285 /* If there are glyphs whose x-offsets are negative,
25286 shift all glyphs to the right and make all x-offsets
25287 non-negative. */
25288 if (leftmost < 0)
25289 {
25290 for (i = 0; i < cmp->glyph_len; i++)
25291 cmp->offsets[i * 2] -= leftmost;
25292 rightmost -= leftmost;
25293 cmp->lbearing -= leftmost;
25294 cmp->rbearing -= leftmost;
25295 }
25296
25297 if (left_padded && cmp->lbearing < 0)
25298 {
25299 for (i = 0; i < cmp->glyph_len; i++)
25300 cmp->offsets[i * 2] -= cmp->lbearing;
25301 rightmost -= cmp->lbearing;
25302 cmp->rbearing -= cmp->lbearing;
25303 cmp->lbearing = 0;
25304 }
25305 if (right_padded && rightmost < cmp->rbearing)
25306 {
25307 rightmost = cmp->rbearing;
25308 }
25309
25310 cmp->pixel_width = rightmost;
25311 cmp->ascent = highest;
25312 cmp->descent = - lowest;
25313 if (cmp->ascent < font_ascent)
25314 cmp->ascent = font_ascent;
25315 if (cmp->descent < font_descent)
25316 cmp->descent = font_descent;
25317 }
25318
25319 if (it->glyph_row
25320 && (cmp->lbearing < 0
25321 || cmp->rbearing > cmp->pixel_width))
25322 it->glyph_row->contains_overlapping_glyphs_p = 1;
25323
25324 it->pixel_width = cmp->pixel_width;
25325 it->ascent = it->phys_ascent = cmp->ascent;
25326 it->descent = it->phys_descent = cmp->descent;
25327 if (face->box != FACE_NO_BOX)
25328 {
25329 int thick = face->box_line_width;
25330
25331 if (thick > 0)
25332 {
25333 it->ascent += thick;
25334 it->descent += thick;
25335 }
25336 else
25337 thick = - thick;
25338
25339 if (it->start_of_box_run_p)
25340 it->pixel_width += thick;
25341 if (it->end_of_box_run_p)
25342 it->pixel_width += thick;
25343 }
25344
25345 /* If face has an overline, add the height of the overline
25346 (1 pixel) and a 1 pixel margin to the character height. */
25347 if (face->overline_p)
25348 it->ascent += overline_margin;
25349
25350 take_vertical_position_into_account (it);
25351 if (it->ascent < 0)
25352 it->ascent = 0;
25353 if (it->descent < 0)
25354 it->descent = 0;
25355
25356 if (it->glyph_row && cmp->glyph_len > 0)
25357 append_composite_glyph (it);
25358 }
25359 else if (it->what == IT_COMPOSITION)
25360 {
25361 /* A dynamic (automatic) composition. */
25362 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25363 Lisp_Object gstring;
25364 struct font_metrics metrics;
25365
25366 it->nglyphs = 1;
25367
25368 gstring = composition_gstring_from_id (it->cmp_it.id);
25369 it->pixel_width
25370 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25371 &metrics);
25372 if (it->glyph_row
25373 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25374 it->glyph_row->contains_overlapping_glyphs_p = 1;
25375 it->ascent = it->phys_ascent = metrics.ascent;
25376 it->descent = it->phys_descent = metrics.descent;
25377 if (face->box != FACE_NO_BOX)
25378 {
25379 int thick = face->box_line_width;
25380
25381 if (thick > 0)
25382 {
25383 it->ascent += thick;
25384 it->descent += thick;
25385 }
25386 else
25387 thick = - thick;
25388
25389 if (it->start_of_box_run_p)
25390 it->pixel_width += thick;
25391 if (it->end_of_box_run_p)
25392 it->pixel_width += thick;
25393 }
25394 /* If face has an overline, add the height of the overline
25395 (1 pixel) and a 1 pixel margin to the character height. */
25396 if (face->overline_p)
25397 it->ascent += overline_margin;
25398 take_vertical_position_into_account (it);
25399 if (it->ascent < 0)
25400 it->ascent = 0;
25401 if (it->descent < 0)
25402 it->descent = 0;
25403
25404 if (it->glyph_row)
25405 append_composite_glyph (it);
25406 }
25407 else if (it->what == IT_GLYPHLESS)
25408 produce_glyphless_glyph (it, 0, Qnil);
25409 else if (it->what == IT_IMAGE)
25410 produce_image_glyph (it);
25411 else if (it->what == IT_STRETCH)
25412 produce_stretch_glyph (it);
25413
25414 done:
25415 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25416 because this isn't true for images with `:ascent 100'. */
25417 eassert (it->ascent >= 0 && it->descent >= 0);
25418 if (it->area == TEXT_AREA)
25419 it->current_x += it->pixel_width;
25420
25421 if (extra_line_spacing > 0)
25422 {
25423 it->descent += extra_line_spacing;
25424 if (extra_line_spacing > it->max_extra_line_spacing)
25425 it->max_extra_line_spacing = extra_line_spacing;
25426 }
25427
25428 it->max_ascent = max (it->max_ascent, it->ascent);
25429 it->max_descent = max (it->max_descent, it->descent);
25430 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25431 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25432 }
25433
25434 /* EXPORT for RIF:
25435 Output LEN glyphs starting at START at the nominal cursor position.
25436 Advance the nominal cursor over the text. The global variable
25437 updated_window contains the window being updated, updated_row is
25438 the glyph row being updated, and updated_area is the area of that
25439 row being updated. */
25440
25441 void
25442 x_write_glyphs (struct glyph *start, int len)
25443 {
25444 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25445
25446 eassert (updated_window && updated_row);
25447 /* When the window is hscrolled, cursor hpos can legitimately be out
25448 of bounds, but we draw the cursor at the corresponding window
25449 margin in that case. */
25450 if (!updated_row->reversed_p && chpos < 0)
25451 chpos = 0;
25452 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25453 chpos = updated_row->used[TEXT_AREA] - 1;
25454
25455 block_input ();
25456
25457 /* Write glyphs. */
25458
25459 hpos = start - updated_row->glyphs[updated_area];
25460 x = draw_glyphs (updated_window, output_cursor.x,
25461 updated_row, updated_area,
25462 hpos, hpos + len,
25463 DRAW_NORMAL_TEXT, 0);
25464
25465 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25466 if (updated_area == TEXT_AREA
25467 && updated_window->phys_cursor_on_p
25468 && updated_window->phys_cursor.vpos == output_cursor.vpos
25469 && chpos >= hpos
25470 && chpos < hpos + len)
25471 updated_window->phys_cursor_on_p = 0;
25472
25473 unblock_input ();
25474
25475 /* Advance the output cursor. */
25476 output_cursor.hpos += len;
25477 output_cursor.x = x;
25478 }
25479
25480
25481 /* EXPORT for RIF:
25482 Insert LEN glyphs from START at the nominal cursor position. */
25483
25484 void
25485 x_insert_glyphs (struct glyph *start, int len)
25486 {
25487 struct frame *f;
25488 struct window *w;
25489 int line_height, shift_by_width, shifted_region_width;
25490 struct glyph_row *row;
25491 struct glyph *glyph;
25492 int frame_x, frame_y;
25493 ptrdiff_t hpos;
25494
25495 eassert (updated_window && updated_row);
25496 block_input ();
25497 w = updated_window;
25498 f = XFRAME (WINDOW_FRAME (w));
25499
25500 /* Get the height of the line we are in. */
25501 row = updated_row;
25502 line_height = row->height;
25503
25504 /* Get the width of the glyphs to insert. */
25505 shift_by_width = 0;
25506 for (glyph = start; glyph < start + len; ++glyph)
25507 shift_by_width += glyph->pixel_width;
25508
25509 /* Get the width of the region to shift right. */
25510 shifted_region_width = (window_box_width (w, updated_area)
25511 - output_cursor.x
25512 - shift_by_width);
25513
25514 /* Shift right. */
25515 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25516 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25517
25518 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25519 line_height, shift_by_width);
25520
25521 /* Write the glyphs. */
25522 hpos = start - row->glyphs[updated_area];
25523 draw_glyphs (w, output_cursor.x, row, updated_area,
25524 hpos, hpos + len,
25525 DRAW_NORMAL_TEXT, 0);
25526
25527 /* Advance the output cursor. */
25528 output_cursor.hpos += len;
25529 output_cursor.x += shift_by_width;
25530 unblock_input ();
25531 }
25532
25533
25534 /* EXPORT for RIF:
25535 Erase the current text line from the nominal cursor position
25536 (inclusive) to pixel column TO_X (exclusive). The idea is that
25537 everything from TO_X onward is already erased.
25538
25539 TO_X is a pixel position relative to updated_area of
25540 updated_window. TO_X == -1 means clear to the end of this area. */
25541
25542 void
25543 x_clear_end_of_line (int to_x)
25544 {
25545 struct frame *f;
25546 struct window *w = updated_window;
25547 int max_x, min_y, max_y;
25548 int from_x, from_y, to_y;
25549
25550 eassert (updated_window && updated_row);
25551 f = XFRAME (w->frame);
25552
25553 if (updated_row->full_width_p)
25554 max_x = WINDOW_TOTAL_WIDTH (w);
25555 else
25556 max_x = window_box_width (w, updated_area);
25557 max_y = window_text_bottom_y (w);
25558
25559 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25560 of window. For TO_X > 0, truncate to end of drawing area. */
25561 if (to_x == 0)
25562 return;
25563 else if (to_x < 0)
25564 to_x = max_x;
25565 else
25566 to_x = min (to_x, max_x);
25567
25568 to_y = min (max_y, output_cursor.y + updated_row->height);
25569
25570 /* Notice if the cursor will be cleared by this operation. */
25571 if (!updated_row->full_width_p)
25572 notice_overwritten_cursor (w, updated_area,
25573 output_cursor.x, -1,
25574 updated_row->y,
25575 MATRIX_ROW_BOTTOM_Y (updated_row));
25576
25577 from_x = output_cursor.x;
25578
25579 /* Translate to frame coordinates. */
25580 if (updated_row->full_width_p)
25581 {
25582 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25583 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25584 }
25585 else
25586 {
25587 int area_left = window_box_left (w, updated_area);
25588 from_x += area_left;
25589 to_x += area_left;
25590 }
25591
25592 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25593 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25594 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25595
25596 /* Prevent inadvertently clearing to end of the X window. */
25597 if (to_x > from_x && to_y > from_y)
25598 {
25599 block_input ();
25600 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25601 to_x - from_x, to_y - from_y);
25602 unblock_input ();
25603 }
25604 }
25605
25606 #endif /* HAVE_WINDOW_SYSTEM */
25607
25608
25609 \f
25610 /***********************************************************************
25611 Cursor types
25612 ***********************************************************************/
25613
25614 /* Value is the internal representation of the specified cursor type
25615 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25616 of the bar cursor. */
25617
25618 static enum text_cursor_kinds
25619 get_specified_cursor_type (Lisp_Object arg, int *width)
25620 {
25621 enum text_cursor_kinds type;
25622
25623 if (NILP (arg))
25624 return NO_CURSOR;
25625
25626 if (EQ (arg, Qbox))
25627 return FILLED_BOX_CURSOR;
25628
25629 if (EQ (arg, Qhollow))
25630 return HOLLOW_BOX_CURSOR;
25631
25632 if (EQ (arg, Qbar))
25633 {
25634 *width = 2;
25635 return BAR_CURSOR;
25636 }
25637
25638 if (CONSP (arg)
25639 && EQ (XCAR (arg), Qbar)
25640 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25641 {
25642 *width = XINT (XCDR (arg));
25643 return BAR_CURSOR;
25644 }
25645
25646 if (EQ (arg, Qhbar))
25647 {
25648 *width = 2;
25649 return HBAR_CURSOR;
25650 }
25651
25652 if (CONSP (arg)
25653 && EQ (XCAR (arg), Qhbar)
25654 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25655 {
25656 *width = XINT (XCDR (arg));
25657 return HBAR_CURSOR;
25658 }
25659
25660 /* Treat anything unknown as "hollow box cursor".
25661 It was bad to signal an error; people have trouble fixing
25662 .Xdefaults with Emacs, when it has something bad in it. */
25663 type = HOLLOW_BOX_CURSOR;
25664
25665 return type;
25666 }
25667
25668 /* Set the default cursor types for specified frame. */
25669 void
25670 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25671 {
25672 int width = 1;
25673 Lisp_Object tem;
25674
25675 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25676 FRAME_CURSOR_WIDTH (f) = width;
25677
25678 /* By default, set up the blink-off state depending on the on-state. */
25679
25680 tem = Fassoc (arg, Vblink_cursor_alist);
25681 if (!NILP (tem))
25682 {
25683 FRAME_BLINK_OFF_CURSOR (f)
25684 = get_specified_cursor_type (XCDR (tem), &width);
25685 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25686 }
25687 else
25688 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25689 }
25690
25691
25692 #ifdef HAVE_WINDOW_SYSTEM
25693
25694 /* Return the cursor we want to be displayed in window W. Return
25695 width of bar/hbar cursor through WIDTH arg. Return with
25696 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25697 (i.e. if the `system caret' should track this cursor).
25698
25699 In a mini-buffer window, we want the cursor only to appear if we
25700 are reading input from this window. For the selected window, we
25701 want the cursor type given by the frame parameter or buffer local
25702 setting of cursor-type. If explicitly marked off, draw no cursor.
25703 In all other cases, we want a hollow box cursor. */
25704
25705 static enum text_cursor_kinds
25706 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25707 int *active_cursor)
25708 {
25709 struct frame *f = XFRAME (w->frame);
25710 struct buffer *b = XBUFFER (w->buffer);
25711 int cursor_type = DEFAULT_CURSOR;
25712 Lisp_Object alt_cursor;
25713 int non_selected = 0;
25714
25715 *active_cursor = 1;
25716
25717 /* Echo area */
25718 if (cursor_in_echo_area
25719 && FRAME_HAS_MINIBUF_P (f)
25720 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25721 {
25722 if (w == XWINDOW (echo_area_window))
25723 {
25724 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25725 {
25726 *width = FRAME_CURSOR_WIDTH (f);
25727 return FRAME_DESIRED_CURSOR (f);
25728 }
25729 else
25730 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25731 }
25732
25733 *active_cursor = 0;
25734 non_selected = 1;
25735 }
25736
25737 /* Detect a nonselected window or nonselected frame. */
25738 else if (w != XWINDOW (f->selected_window)
25739 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25740 {
25741 *active_cursor = 0;
25742
25743 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25744 return NO_CURSOR;
25745
25746 non_selected = 1;
25747 }
25748
25749 /* Never display a cursor in a window in which cursor-type is nil. */
25750 if (NILP (BVAR (b, cursor_type)))
25751 return NO_CURSOR;
25752
25753 /* Get the normal cursor type for this window. */
25754 if (EQ (BVAR (b, cursor_type), Qt))
25755 {
25756 cursor_type = FRAME_DESIRED_CURSOR (f);
25757 *width = FRAME_CURSOR_WIDTH (f);
25758 }
25759 else
25760 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25761
25762 /* Use cursor-in-non-selected-windows instead
25763 for non-selected window or frame. */
25764 if (non_selected)
25765 {
25766 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25767 if (!EQ (Qt, alt_cursor))
25768 return get_specified_cursor_type (alt_cursor, width);
25769 /* t means modify the normal cursor type. */
25770 if (cursor_type == FILLED_BOX_CURSOR)
25771 cursor_type = HOLLOW_BOX_CURSOR;
25772 else if (cursor_type == BAR_CURSOR && *width > 1)
25773 --*width;
25774 return cursor_type;
25775 }
25776
25777 /* Use normal cursor if not blinked off. */
25778 if (!w->cursor_off_p)
25779 {
25780 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25781 {
25782 if (cursor_type == FILLED_BOX_CURSOR)
25783 {
25784 /* Using a block cursor on large images can be very annoying.
25785 So use a hollow cursor for "large" images.
25786 If image is not transparent (no mask), also use hollow cursor. */
25787 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25788 if (img != NULL && IMAGEP (img->spec))
25789 {
25790 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25791 where N = size of default frame font size.
25792 This should cover most of the "tiny" icons people may use. */
25793 if (!img->mask
25794 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25795 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25796 cursor_type = HOLLOW_BOX_CURSOR;
25797 }
25798 }
25799 else if (cursor_type != NO_CURSOR)
25800 {
25801 /* Display current only supports BOX and HOLLOW cursors for images.
25802 So for now, unconditionally use a HOLLOW cursor when cursor is
25803 not a solid box cursor. */
25804 cursor_type = HOLLOW_BOX_CURSOR;
25805 }
25806 }
25807 return cursor_type;
25808 }
25809
25810 /* Cursor is blinked off, so determine how to "toggle" it. */
25811
25812 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25813 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25814 return get_specified_cursor_type (XCDR (alt_cursor), width);
25815
25816 /* Then see if frame has specified a specific blink off cursor type. */
25817 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25818 {
25819 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25820 return FRAME_BLINK_OFF_CURSOR (f);
25821 }
25822
25823 #if 0
25824 /* Some people liked having a permanently visible blinking cursor,
25825 while others had very strong opinions against it. So it was
25826 decided to remove it. KFS 2003-09-03 */
25827
25828 /* Finally perform built-in cursor blinking:
25829 filled box <-> hollow box
25830 wide [h]bar <-> narrow [h]bar
25831 narrow [h]bar <-> no cursor
25832 other type <-> no cursor */
25833
25834 if (cursor_type == FILLED_BOX_CURSOR)
25835 return HOLLOW_BOX_CURSOR;
25836
25837 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25838 {
25839 *width = 1;
25840 return cursor_type;
25841 }
25842 #endif
25843
25844 return NO_CURSOR;
25845 }
25846
25847
25848 /* Notice when the text cursor of window W has been completely
25849 overwritten by a drawing operation that outputs glyphs in AREA
25850 starting at X0 and ending at X1 in the line starting at Y0 and
25851 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25852 the rest of the line after X0 has been written. Y coordinates
25853 are window-relative. */
25854
25855 static void
25856 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25857 int x0, int x1, int y0, int y1)
25858 {
25859 int cx0, cx1, cy0, cy1;
25860 struct glyph_row *row;
25861
25862 if (!w->phys_cursor_on_p)
25863 return;
25864 if (area != TEXT_AREA)
25865 return;
25866
25867 if (w->phys_cursor.vpos < 0
25868 || w->phys_cursor.vpos >= w->current_matrix->nrows
25869 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25870 !(row->enabled_p && row->displays_text_p)))
25871 return;
25872
25873 if (row->cursor_in_fringe_p)
25874 {
25875 row->cursor_in_fringe_p = 0;
25876 draw_fringe_bitmap (w, row, row->reversed_p);
25877 w->phys_cursor_on_p = 0;
25878 return;
25879 }
25880
25881 cx0 = w->phys_cursor.x;
25882 cx1 = cx0 + w->phys_cursor_width;
25883 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25884 return;
25885
25886 /* The cursor image will be completely removed from the
25887 screen if the output area intersects the cursor area in
25888 y-direction. When we draw in [y0 y1[, and some part of
25889 the cursor is at y < y0, that part must have been drawn
25890 before. When scrolling, the cursor is erased before
25891 actually scrolling, so we don't come here. When not
25892 scrolling, the rows above the old cursor row must have
25893 changed, and in this case these rows must have written
25894 over the cursor image.
25895
25896 Likewise if part of the cursor is below y1, with the
25897 exception of the cursor being in the first blank row at
25898 the buffer and window end because update_text_area
25899 doesn't draw that row. (Except when it does, but
25900 that's handled in update_text_area.) */
25901
25902 cy0 = w->phys_cursor.y;
25903 cy1 = cy0 + w->phys_cursor_height;
25904 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25905 return;
25906
25907 w->phys_cursor_on_p = 0;
25908 }
25909
25910 #endif /* HAVE_WINDOW_SYSTEM */
25911
25912 \f
25913 /************************************************************************
25914 Mouse Face
25915 ************************************************************************/
25916
25917 #ifdef HAVE_WINDOW_SYSTEM
25918
25919 /* EXPORT for RIF:
25920 Fix the display of area AREA of overlapping row ROW in window W
25921 with respect to the overlapping part OVERLAPS. */
25922
25923 void
25924 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25925 enum glyph_row_area area, int overlaps)
25926 {
25927 int i, x;
25928
25929 block_input ();
25930
25931 x = 0;
25932 for (i = 0; i < row->used[area];)
25933 {
25934 if (row->glyphs[area][i].overlaps_vertically_p)
25935 {
25936 int start = i, start_x = x;
25937
25938 do
25939 {
25940 x += row->glyphs[area][i].pixel_width;
25941 ++i;
25942 }
25943 while (i < row->used[area]
25944 && row->glyphs[area][i].overlaps_vertically_p);
25945
25946 draw_glyphs (w, start_x, row, area,
25947 start, i,
25948 DRAW_NORMAL_TEXT, overlaps);
25949 }
25950 else
25951 {
25952 x += row->glyphs[area][i].pixel_width;
25953 ++i;
25954 }
25955 }
25956
25957 unblock_input ();
25958 }
25959
25960
25961 /* EXPORT:
25962 Draw the cursor glyph of window W in glyph row ROW. See the
25963 comment of draw_glyphs for the meaning of HL. */
25964
25965 void
25966 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25967 enum draw_glyphs_face hl)
25968 {
25969 /* If cursor hpos is out of bounds, don't draw garbage. This can
25970 happen in mini-buffer windows when switching between echo area
25971 glyphs and mini-buffer. */
25972 if ((row->reversed_p
25973 ? (w->phys_cursor.hpos >= 0)
25974 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25975 {
25976 int on_p = w->phys_cursor_on_p;
25977 int x1;
25978 int hpos = w->phys_cursor.hpos;
25979
25980 /* When the window is hscrolled, cursor hpos can legitimately be
25981 out of bounds, but we draw the cursor at the corresponding
25982 window margin in that case. */
25983 if (!row->reversed_p && hpos < 0)
25984 hpos = 0;
25985 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25986 hpos = row->used[TEXT_AREA] - 1;
25987
25988 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25989 hl, 0);
25990 w->phys_cursor_on_p = on_p;
25991
25992 if (hl == DRAW_CURSOR)
25993 w->phys_cursor_width = x1 - w->phys_cursor.x;
25994 /* When we erase the cursor, and ROW is overlapped by other
25995 rows, make sure that these overlapping parts of other rows
25996 are redrawn. */
25997 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25998 {
25999 w->phys_cursor_width = x1 - w->phys_cursor.x;
26000
26001 if (row > w->current_matrix->rows
26002 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26003 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26004 OVERLAPS_ERASED_CURSOR);
26005
26006 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26007 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26008 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26009 OVERLAPS_ERASED_CURSOR);
26010 }
26011 }
26012 }
26013
26014
26015 /* EXPORT:
26016 Erase the image of a cursor of window W from the screen. */
26017
26018 void
26019 erase_phys_cursor (struct window *w)
26020 {
26021 struct frame *f = XFRAME (w->frame);
26022 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26023 int hpos = w->phys_cursor.hpos;
26024 int vpos = w->phys_cursor.vpos;
26025 int mouse_face_here_p = 0;
26026 struct glyph_matrix *active_glyphs = w->current_matrix;
26027 struct glyph_row *cursor_row;
26028 struct glyph *cursor_glyph;
26029 enum draw_glyphs_face hl;
26030
26031 /* No cursor displayed or row invalidated => nothing to do on the
26032 screen. */
26033 if (w->phys_cursor_type == NO_CURSOR)
26034 goto mark_cursor_off;
26035
26036 /* VPOS >= active_glyphs->nrows means that window has been resized.
26037 Don't bother to erase the cursor. */
26038 if (vpos >= active_glyphs->nrows)
26039 goto mark_cursor_off;
26040
26041 /* If row containing cursor is marked invalid, there is nothing we
26042 can do. */
26043 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26044 if (!cursor_row->enabled_p)
26045 goto mark_cursor_off;
26046
26047 /* If line spacing is > 0, old cursor may only be partially visible in
26048 window after split-window. So adjust visible height. */
26049 cursor_row->visible_height = min (cursor_row->visible_height,
26050 window_text_bottom_y (w) - cursor_row->y);
26051
26052 /* If row is completely invisible, don't attempt to delete a cursor which
26053 isn't there. This can happen if cursor is at top of a window, and
26054 we switch to a buffer with a header line in that window. */
26055 if (cursor_row->visible_height <= 0)
26056 goto mark_cursor_off;
26057
26058 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26059 if (cursor_row->cursor_in_fringe_p)
26060 {
26061 cursor_row->cursor_in_fringe_p = 0;
26062 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26063 goto mark_cursor_off;
26064 }
26065
26066 /* This can happen when the new row is shorter than the old one.
26067 In this case, either draw_glyphs or clear_end_of_line
26068 should have cleared the cursor. Note that we wouldn't be
26069 able to erase the cursor in this case because we don't have a
26070 cursor glyph at hand. */
26071 if ((cursor_row->reversed_p
26072 ? (w->phys_cursor.hpos < 0)
26073 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26074 goto mark_cursor_off;
26075
26076 /* When the window is hscrolled, cursor hpos can legitimately be out
26077 of bounds, but we draw the cursor at the corresponding window
26078 margin in that case. */
26079 if (!cursor_row->reversed_p && hpos < 0)
26080 hpos = 0;
26081 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26082 hpos = cursor_row->used[TEXT_AREA] - 1;
26083
26084 /* If the cursor is in the mouse face area, redisplay that when
26085 we clear the cursor. */
26086 if (! NILP (hlinfo->mouse_face_window)
26087 && coords_in_mouse_face_p (w, hpos, vpos)
26088 /* Don't redraw the cursor's spot in mouse face if it is at the
26089 end of a line (on a newline). The cursor appears there, but
26090 mouse highlighting does not. */
26091 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26092 mouse_face_here_p = 1;
26093
26094 /* Maybe clear the display under the cursor. */
26095 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26096 {
26097 int x, y, left_x;
26098 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26099 int width;
26100
26101 cursor_glyph = get_phys_cursor_glyph (w);
26102 if (cursor_glyph == NULL)
26103 goto mark_cursor_off;
26104
26105 width = cursor_glyph->pixel_width;
26106 left_x = window_box_left_offset (w, TEXT_AREA);
26107 x = w->phys_cursor.x;
26108 if (x < left_x)
26109 width -= left_x - x;
26110 width = min (width, window_box_width (w, TEXT_AREA) - x);
26111 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26112 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26113
26114 if (width > 0)
26115 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26116 }
26117
26118 /* Erase the cursor by redrawing the character underneath it. */
26119 if (mouse_face_here_p)
26120 hl = DRAW_MOUSE_FACE;
26121 else
26122 hl = DRAW_NORMAL_TEXT;
26123 draw_phys_cursor_glyph (w, cursor_row, hl);
26124
26125 mark_cursor_off:
26126 w->phys_cursor_on_p = 0;
26127 w->phys_cursor_type = NO_CURSOR;
26128 }
26129
26130
26131 /* EXPORT:
26132 Display or clear cursor of window W. If ON is zero, clear the
26133 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26134 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26135
26136 void
26137 display_and_set_cursor (struct window *w, int on,
26138 int hpos, int vpos, int x, int y)
26139 {
26140 struct frame *f = XFRAME (w->frame);
26141 int new_cursor_type;
26142 int new_cursor_width;
26143 int active_cursor;
26144 struct glyph_row *glyph_row;
26145 struct glyph *glyph;
26146
26147 /* This is pointless on invisible frames, and dangerous on garbaged
26148 windows and frames; in the latter case, the frame or window may
26149 be in the midst of changing its size, and x and y may be off the
26150 window. */
26151 if (! FRAME_VISIBLE_P (f)
26152 || FRAME_GARBAGED_P (f)
26153 || vpos >= w->current_matrix->nrows
26154 || hpos >= w->current_matrix->matrix_w)
26155 return;
26156
26157 /* If cursor is off and we want it off, return quickly. */
26158 if (!on && !w->phys_cursor_on_p)
26159 return;
26160
26161 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26162 /* If cursor row is not enabled, we don't really know where to
26163 display the cursor. */
26164 if (!glyph_row->enabled_p)
26165 {
26166 w->phys_cursor_on_p = 0;
26167 return;
26168 }
26169
26170 glyph = NULL;
26171 if (!glyph_row->exact_window_width_line_p
26172 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26173 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26174
26175 eassert (input_blocked_p ());
26176
26177 /* Set new_cursor_type to the cursor we want to be displayed. */
26178 new_cursor_type = get_window_cursor_type (w, glyph,
26179 &new_cursor_width, &active_cursor);
26180
26181 /* If cursor is currently being shown and we don't want it to be or
26182 it is in the wrong place, or the cursor type is not what we want,
26183 erase it. */
26184 if (w->phys_cursor_on_p
26185 && (!on
26186 || w->phys_cursor.x != x
26187 || w->phys_cursor.y != y
26188 || new_cursor_type != w->phys_cursor_type
26189 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26190 && new_cursor_width != w->phys_cursor_width)))
26191 erase_phys_cursor (w);
26192
26193 /* Don't check phys_cursor_on_p here because that flag is only set
26194 to zero in some cases where we know that the cursor has been
26195 completely erased, to avoid the extra work of erasing the cursor
26196 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26197 still not be visible, or it has only been partly erased. */
26198 if (on)
26199 {
26200 w->phys_cursor_ascent = glyph_row->ascent;
26201 w->phys_cursor_height = glyph_row->height;
26202
26203 /* Set phys_cursor_.* before x_draw_.* is called because some
26204 of them may need the information. */
26205 w->phys_cursor.x = x;
26206 w->phys_cursor.y = glyph_row->y;
26207 w->phys_cursor.hpos = hpos;
26208 w->phys_cursor.vpos = vpos;
26209 }
26210
26211 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26212 new_cursor_type, new_cursor_width,
26213 on, active_cursor);
26214 }
26215
26216
26217 /* Switch the display of W's cursor on or off, according to the value
26218 of ON. */
26219
26220 static void
26221 update_window_cursor (struct window *w, int on)
26222 {
26223 /* Don't update cursor in windows whose frame is in the process
26224 of being deleted. */
26225 if (w->current_matrix)
26226 {
26227 int hpos = w->phys_cursor.hpos;
26228 int vpos = w->phys_cursor.vpos;
26229 struct glyph_row *row;
26230
26231 if (vpos >= w->current_matrix->nrows
26232 || hpos >= w->current_matrix->matrix_w)
26233 return;
26234
26235 row = MATRIX_ROW (w->current_matrix, vpos);
26236
26237 /* When the window is hscrolled, cursor hpos can legitimately be
26238 out of bounds, but we draw the cursor at the corresponding
26239 window margin in that case. */
26240 if (!row->reversed_p && hpos < 0)
26241 hpos = 0;
26242 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26243 hpos = row->used[TEXT_AREA] - 1;
26244
26245 block_input ();
26246 display_and_set_cursor (w, on, hpos, vpos,
26247 w->phys_cursor.x, w->phys_cursor.y);
26248 unblock_input ();
26249 }
26250 }
26251
26252
26253 /* Call update_window_cursor with parameter ON_P on all leaf windows
26254 in the window tree rooted at W. */
26255
26256 static void
26257 update_cursor_in_window_tree (struct window *w, int on_p)
26258 {
26259 while (w)
26260 {
26261 if (!NILP (w->hchild))
26262 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26263 else if (!NILP (w->vchild))
26264 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26265 else
26266 update_window_cursor (w, on_p);
26267
26268 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26269 }
26270 }
26271
26272
26273 /* EXPORT:
26274 Display the cursor on window W, or clear it, according to ON_P.
26275 Don't change the cursor's position. */
26276
26277 void
26278 x_update_cursor (struct frame *f, int on_p)
26279 {
26280 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26281 }
26282
26283
26284 /* EXPORT:
26285 Clear the cursor of window W to background color, and mark the
26286 cursor as not shown. This is used when the text where the cursor
26287 is about to be rewritten. */
26288
26289 void
26290 x_clear_cursor (struct window *w)
26291 {
26292 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26293 update_window_cursor (w, 0);
26294 }
26295
26296 #endif /* HAVE_WINDOW_SYSTEM */
26297
26298 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26299 and MSDOS. */
26300 static void
26301 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26302 int start_hpos, int end_hpos,
26303 enum draw_glyphs_face draw)
26304 {
26305 #ifdef HAVE_WINDOW_SYSTEM
26306 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26307 {
26308 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26309 return;
26310 }
26311 #endif
26312 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26313 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26314 #endif
26315 }
26316
26317 /* Display the active region described by mouse_face_* according to DRAW. */
26318
26319 static void
26320 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26321 {
26322 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26323 struct frame *f = XFRAME (WINDOW_FRAME (w));
26324
26325 if (/* If window is in the process of being destroyed, don't bother
26326 to do anything. */
26327 w->current_matrix != NULL
26328 /* Don't update mouse highlight if hidden */
26329 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26330 /* Recognize when we are called to operate on rows that don't exist
26331 anymore. This can happen when a window is split. */
26332 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26333 {
26334 int phys_cursor_on_p = w->phys_cursor_on_p;
26335 struct glyph_row *row, *first, *last;
26336
26337 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26338 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26339
26340 for (row = first; row <= last && row->enabled_p; ++row)
26341 {
26342 int start_hpos, end_hpos, start_x;
26343
26344 /* For all but the first row, the highlight starts at column 0. */
26345 if (row == first)
26346 {
26347 /* R2L rows have BEG and END in reversed order, but the
26348 screen drawing geometry is always left to right. So
26349 we need to mirror the beginning and end of the
26350 highlighted area in R2L rows. */
26351 if (!row->reversed_p)
26352 {
26353 start_hpos = hlinfo->mouse_face_beg_col;
26354 start_x = hlinfo->mouse_face_beg_x;
26355 }
26356 else if (row == last)
26357 {
26358 start_hpos = hlinfo->mouse_face_end_col;
26359 start_x = hlinfo->mouse_face_end_x;
26360 }
26361 else
26362 {
26363 start_hpos = 0;
26364 start_x = 0;
26365 }
26366 }
26367 else if (row->reversed_p && row == last)
26368 {
26369 start_hpos = hlinfo->mouse_face_end_col;
26370 start_x = hlinfo->mouse_face_end_x;
26371 }
26372 else
26373 {
26374 start_hpos = 0;
26375 start_x = 0;
26376 }
26377
26378 if (row == last)
26379 {
26380 if (!row->reversed_p)
26381 end_hpos = hlinfo->mouse_face_end_col;
26382 else if (row == first)
26383 end_hpos = hlinfo->mouse_face_beg_col;
26384 else
26385 {
26386 end_hpos = row->used[TEXT_AREA];
26387 if (draw == DRAW_NORMAL_TEXT)
26388 row->fill_line_p = 1; /* Clear to end of line */
26389 }
26390 }
26391 else if (row->reversed_p && row == first)
26392 end_hpos = hlinfo->mouse_face_beg_col;
26393 else
26394 {
26395 end_hpos = row->used[TEXT_AREA];
26396 if (draw == DRAW_NORMAL_TEXT)
26397 row->fill_line_p = 1; /* Clear to end of line */
26398 }
26399
26400 if (end_hpos > start_hpos)
26401 {
26402 draw_row_with_mouse_face (w, start_x, row,
26403 start_hpos, end_hpos, draw);
26404
26405 row->mouse_face_p
26406 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26407 }
26408 }
26409
26410 #ifdef HAVE_WINDOW_SYSTEM
26411 /* When we've written over the cursor, arrange for it to
26412 be displayed again. */
26413 if (FRAME_WINDOW_P (f)
26414 && phys_cursor_on_p && !w->phys_cursor_on_p)
26415 {
26416 int hpos = w->phys_cursor.hpos;
26417
26418 /* When the window is hscrolled, cursor hpos can legitimately be
26419 out of bounds, but we draw the cursor at the corresponding
26420 window margin in that case. */
26421 if (!row->reversed_p && hpos < 0)
26422 hpos = 0;
26423 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26424 hpos = row->used[TEXT_AREA] - 1;
26425
26426 block_input ();
26427 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26428 w->phys_cursor.x, w->phys_cursor.y);
26429 unblock_input ();
26430 }
26431 #endif /* HAVE_WINDOW_SYSTEM */
26432 }
26433
26434 #ifdef HAVE_WINDOW_SYSTEM
26435 /* Change the mouse cursor. */
26436 if (FRAME_WINDOW_P (f))
26437 {
26438 if (draw == DRAW_NORMAL_TEXT
26439 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26440 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26441 else if (draw == DRAW_MOUSE_FACE)
26442 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26443 else
26444 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26445 }
26446 #endif /* HAVE_WINDOW_SYSTEM */
26447 }
26448
26449 /* EXPORT:
26450 Clear out the mouse-highlighted active region.
26451 Redraw it un-highlighted first. Value is non-zero if mouse
26452 face was actually drawn unhighlighted. */
26453
26454 int
26455 clear_mouse_face (Mouse_HLInfo *hlinfo)
26456 {
26457 int cleared = 0;
26458
26459 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26460 {
26461 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26462 cleared = 1;
26463 }
26464
26465 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26466 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26467 hlinfo->mouse_face_window = Qnil;
26468 hlinfo->mouse_face_overlay = Qnil;
26469 return cleared;
26470 }
26471
26472 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26473 within the mouse face on that window. */
26474 static int
26475 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26476 {
26477 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26478
26479 /* Quickly resolve the easy cases. */
26480 if (!(WINDOWP (hlinfo->mouse_face_window)
26481 && XWINDOW (hlinfo->mouse_face_window) == w))
26482 return 0;
26483 if (vpos < hlinfo->mouse_face_beg_row
26484 || vpos > hlinfo->mouse_face_end_row)
26485 return 0;
26486 if (vpos > hlinfo->mouse_face_beg_row
26487 && vpos < hlinfo->mouse_face_end_row)
26488 return 1;
26489
26490 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26491 {
26492 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26493 {
26494 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26495 return 1;
26496 }
26497 else if ((vpos == hlinfo->mouse_face_beg_row
26498 && hpos >= hlinfo->mouse_face_beg_col)
26499 || (vpos == hlinfo->mouse_face_end_row
26500 && hpos < hlinfo->mouse_face_end_col))
26501 return 1;
26502 }
26503 else
26504 {
26505 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26506 {
26507 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26508 return 1;
26509 }
26510 else if ((vpos == hlinfo->mouse_face_beg_row
26511 && hpos <= hlinfo->mouse_face_beg_col)
26512 || (vpos == hlinfo->mouse_face_end_row
26513 && hpos > hlinfo->mouse_face_end_col))
26514 return 1;
26515 }
26516 return 0;
26517 }
26518
26519
26520 /* EXPORT:
26521 Non-zero if physical cursor of window W is within mouse face. */
26522
26523 int
26524 cursor_in_mouse_face_p (struct window *w)
26525 {
26526 int hpos = w->phys_cursor.hpos;
26527 int vpos = w->phys_cursor.vpos;
26528 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26529
26530 /* When the window is hscrolled, cursor hpos can legitimately be out
26531 of bounds, but we draw the cursor at the corresponding window
26532 margin in that case. */
26533 if (!row->reversed_p && hpos < 0)
26534 hpos = 0;
26535 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26536 hpos = row->used[TEXT_AREA] - 1;
26537
26538 return coords_in_mouse_face_p (w, hpos, vpos);
26539 }
26540
26541
26542 \f
26543 /* Find the glyph rows START_ROW and END_ROW of window W that display
26544 characters between buffer positions START_CHARPOS and END_CHARPOS
26545 (excluding END_CHARPOS). DISP_STRING is a display string that
26546 covers these buffer positions. This is similar to
26547 row_containing_pos, but is more accurate when bidi reordering makes
26548 buffer positions change non-linearly with glyph rows. */
26549 static void
26550 rows_from_pos_range (struct window *w,
26551 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26552 Lisp_Object disp_string,
26553 struct glyph_row **start, struct glyph_row **end)
26554 {
26555 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26556 int last_y = window_text_bottom_y (w);
26557 struct glyph_row *row;
26558
26559 *start = NULL;
26560 *end = NULL;
26561
26562 while (!first->enabled_p
26563 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26564 first++;
26565
26566 /* Find the START row. */
26567 for (row = first;
26568 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26569 row++)
26570 {
26571 /* A row can potentially be the START row if the range of the
26572 characters it displays intersects the range
26573 [START_CHARPOS..END_CHARPOS). */
26574 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26575 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26576 /* See the commentary in row_containing_pos, for the
26577 explanation of the complicated way to check whether
26578 some position is beyond the end of the characters
26579 displayed by a row. */
26580 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26581 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26582 && !row->ends_at_zv_p
26583 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26584 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26585 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26586 && !row->ends_at_zv_p
26587 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26588 {
26589 /* Found a candidate row. Now make sure at least one of the
26590 glyphs it displays has a charpos from the range
26591 [START_CHARPOS..END_CHARPOS).
26592
26593 This is not obvious because bidi reordering could make
26594 buffer positions of a row be 1,2,3,102,101,100, and if we
26595 want to highlight characters in [50..60), we don't want
26596 this row, even though [50..60) does intersect [1..103),
26597 the range of character positions given by the row's start
26598 and end positions. */
26599 struct glyph *g = row->glyphs[TEXT_AREA];
26600 struct glyph *e = g + row->used[TEXT_AREA];
26601
26602 while (g < e)
26603 {
26604 if (((BUFFERP (g->object) || INTEGERP (g->object))
26605 && start_charpos <= g->charpos && g->charpos < end_charpos)
26606 /* A glyph that comes from DISP_STRING is by
26607 definition to be highlighted. */
26608 || EQ (g->object, disp_string))
26609 *start = row;
26610 g++;
26611 }
26612 if (*start)
26613 break;
26614 }
26615 }
26616
26617 /* Find the END row. */
26618 if (!*start
26619 /* If the last row is partially visible, start looking for END
26620 from that row, instead of starting from FIRST. */
26621 && !(row->enabled_p
26622 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26623 row = first;
26624 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26625 {
26626 struct glyph_row *next = row + 1;
26627 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26628
26629 if (!next->enabled_p
26630 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26631 /* The first row >= START whose range of displayed characters
26632 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26633 is the row END + 1. */
26634 || (start_charpos < next_start
26635 && end_charpos < next_start)
26636 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26637 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26638 && !next->ends_at_zv_p
26639 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26640 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26641 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26642 && !next->ends_at_zv_p
26643 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26644 {
26645 *end = row;
26646 break;
26647 }
26648 else
26649 {
26650 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26651 but none of the characters it displays are in the range, it is
26652 also END + 1. */
26653 struct glyph *g = next->glyphs[TEXT_AREA];
26654 struct glyph *s = g;
26655 struct glyph *e = g + next->used[TEXT_AREA];
26656
26657 while (g < e)
26658 {
26659 if (((BUFFERP (g->object) || INTEGERP (g->object))
26660 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26661 /* If the buffer position of the first glyph in
26662 the row is equal to END_CHARPOS, it means
26663 the last character to be highlighted is the
26664 newline of ROW, and we must consider NEXT as
26665 END, not END+1. */
26666 || (((!next->reversed_p && g == s)
26667 || (next->reversed_p && g == e - 1))
26668 && (g->charpos == end_charpos
26669 /* Special case for when NEXT is an
26670 empty line at ZV. */
26671 || (g->charpos == -1
26672 && !row->ends_at_zv_p
26673 && next_start == end_charpos)))))
26674 /* A glyph that comes from DISP_STRING is by
26675 definition to be highlighted. */
26676 || EQ (g->object, disp_string))
26677 break;
26678 g++;
26679 }
26680 if (g == e)
26681 {
26682 *end = row;
26683 break;
26684 }
26685 /* The first row that ends at ZV must be the last to be
26686 highlighted. */
26687 else if (next->ends_at_zv_p)
26688 {
26689 *end = next;
26690 break;
26691 }
26692 }
26693 }
26694 }
26695
26696 /* This function sets the mouse_face_* elements of HLINFO, assuming
26697 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26698 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26699 for the overlay or run of text properties specifying the mouse
26700 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26701 before-string and after-string that must also be highlighted.
26702 DISP_STRING, if non-nil, is a display string that may cover some
26703 or all of the highlighted text. */
26704
26705 static void
26706 mouse_face_from_buffer_pos (Lisp_Object window,
26707 Mouse_HLInfo *hlinfo,
26708 ptrdiff_t mouse_charpos,
26709 ptrdiff_t start_charpos,
26710 ptrdiff_t end_charpos,
26711 Lisp_Object before_string,
26712 Lisp_Object after_string,
26713 Lisp_Object disp_string)
26714 {
26715 struct window *w = XWINDOW (window);
26716 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26717 struct glyph_row *r1, *r2;
26718 struct glyph *glyph, *end;
26719 ptrdiff_t ignore, pos;
26720 int x;
26721
26722 eassert (NILP (disp_string) || STRINGP (disp_string));
26723 eassert (NILP (before_string) || STRINGP (before_string));
26724 eassert (NILP (after_string) || STRINGP (after_string));
26725
26726 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26727 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26728 if (r1 == NULL)
26729 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26730 /* If the before-string or display-string contains newlines,
26731 rows_from_pos_range skips to its last row. Move back. */
26732 if (!NILP (before_string) || !NILP (disp_string))
26733 {
26734 struct glyph_row *prev;
26735 while ((prev = r1 - 1, prev >= first)
26736 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26737 && prev->used[TEXT_AREA] > 0)
26738 {
26739 struct glyph *beg = prev->glyphs[TEXT_AREA];
26740 glyph = beg + prev->used[TEXT_AREA];
26741 while (--glyph >= beg && INTEGERP (glyph->object));
26742 if (glyph < beg
26743 || !(EQ (glyph->object, before_string)
26744 || EQ (glyph->object, disp_string)))
26745 break;
26746 r1 = prev;
26747 }
26748 }
26749 if (r2 == NULL)
26750 {
26751 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26752 hlinfo->mouse_face_past_end = 1;
26753 }
26754 else if (!NILP (after_string))
26755 {
26756 /* If the after-string has newlines, advance to its last row. */
26757 struct glyph_row *next;
26758 struct glyph_row *last
26759 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26760
26761 for (next = r2 + 1;
26762 next <= last
26763 && next->used[TEXT_AREA] > 0
26764 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26765 ++next)
26766 r2 = next;
26767 }
26768 /* The rest of the display engine assumes that mouse_face_beg_row is
26769 either above mouse_face_end_row or identical to it. But with
26770 bidi-reordered continued lines, the row for START_CHARPOS could
26771 be below the row for END_CHARPOS. If so, swap the rows and store
26772 them in correct order. */
26773 if (r1->y > r2->y)
26774 {
26775 struct glyph_row *tem = r2;
26776
26777 r2 = r1;
26778 r1 = tem;
26779 }
26780
26781 hlinfo->mouse_face_beg_y = r1->y;
26782 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26783 hlinfo->mouse_face_end_y = r2->y;
26784 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26785
26786 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26787 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26788 could be anywhere in the row and in any order. The strategy
26789 below is to find the leftmost and the rightmost glyph that
26790 belongs to either of these 3 strings, or whose position is
26791 between START_CHARPOS and END_CHARPOS, and highlight all the
26792 glyphs between those two. This may cover more than just the text
26793 between START_CHARPOS and END_CHARPOS if the range of characters
26794 strides the bidi level boundary, e.g. if the beginning is in R2L
26795 text while the end is in L2R text or vice versa. */
26796 if (!r1->reversed_p)
26797 {
26798 /* This row is in a left to right paragraph. Scan it left to
26799 right. */
26800 glyph = r1->glyphs[TEXT_AREA];
26801 end = glyph + r1->used[TEXT_AREA];
26802 x = r1->x;
26803
26804 /* Skip truncation glyphs at the start of the glyph row. */
26805 if (r1->displays_text_p)
26806 for (; glyph < end
26807 && INTEGERP (glyph->object)
26808 && glyph->charpos < 0;
26809 ++glyph)
26810 x += glyph->pixel_width;
26811
26812 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26813 or DISP_STRING, and the first glyph from buffer whose
26814 position is between START_CHARPOS and END_CHARPOS. */
26815 for (; glyph < end
26816 && !INTEGERP (glyph->object)
26817 && !EQ (glyph->object, disp_string)
26818 && !(BUFFERP (glyph->object)
26819 && (glyph->charpos >= start_charpos
26820 && glyph->charpos < end_charpos));
26821 ++glyph)
26822 {
26823 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26824 are present at buffer positions between START_CHARPOS and
26825 END_CHARPOS, or if they come from an overlay. */
26826 if (EQ (glyph->object, before_string))
26827 {
26828 pos = string_buffer_position (before_string,
26829 start_charpos);
26830 /* If pos == 0, it means before_string came from an
26831 overlay, not from a buffer position. */
26832 if (!pos || (pos >= start_charpos && pos < end_charpos))
26833 break;
26834 }
26835 else if (EQ (glyph->object, after_string))
26836 {
26837 pos = string_buffer_position (after_string, end_charpos);
26838 if (!pos || (pos >= start_charpos && pos < end_charpos))
26839 break;
26840 }
26841 x += glyph->pixel_width;
26842 }
26843 hlinfo->mouse_face_beg_x = x;
26844 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26845 }
26846 else
26847 {
26848 /* This row is in a right to left paragraph. Scan it right to
26849 left. */
26850 struct glyph *g;
26851
26852 end = r1->glyphs[TEXT_AREA] - 1;
26853 glyph = end + r1->used[TEXT_AREA];
26854
26855 /* Skip truncation glyphs at the start of the glyph row. */
26856 if (r1->displays_text_p)
26857 for (; glyph > end
26858 && INTEGERP (glyph->object)
26859 && glyph->charpos < 0;
26860 --glyph)
26861 ;
26862
26863 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26864 or DISP_STRING, and the first glyph from buffer whose
26865 position is between START_CHARPOS and END_CHARPOS. */
26866 for (; glyph > end
26867 && !INTEGERP (glyph->object)
26868 && !EQ (glyph->object, disp_string)
26869 && !(BUFFERP (glyph->object)
26870 && (glyph->charpos >= start_charpos
26871 && glyph->charpos < end_charpos));
26872 --glyph)
26873 {
26874 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26875 are present at buffer positions between START_CHARPOS and
26876 END_CHARPOS, or if they come from an overlay. */
26877 if (EQ (glyph->object, before_string))
26878 {
26879 pos = string_buffer_position (before_string, start_charpos);
26880 /* If pos == 0, it means before_string came from an
26881 overlay, not from a buffer position. */
26882 if (!pos || (pos >= start_charpos && pos < end_charpos))
26883 break;
26884 }
26885 else if (EQ (glyph->object, after_string))
26886 {
26887 pos = string_buffer_position (after_string, end_charpos);
26888 if (!pos || (pos >= start_charpos && pos < end_charpos))
26889 break;
26890 }
26891 }
26892
26893 glyph++; /* first glyph to the right of the highlighted area */
26894 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26895 x += g->pixel_width;
26896 hlinfo->mouse_face_beg_x = x;
26897 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26898 }
26899
26900 /* If the highlight ends in a different row, compute GLYPH and END
26901 for the end row. Otherwise, reuse the values computed above for
26902 the row where the highlight begins. */
26903 if (r2 != r1)
26904 {
26905 if (!r2->reversed_p)
26906 {
26907 glyph = r2->glyphs[TEXT_AREA];
26908 end = glyph + r2->used[TEXT_AREA];
26909 x = r2->x;
26910 }
26911 else
26912 {
26913 end = r2->glyphs[TEXT_AREA] - 1;
26914 glyph = end + r2->used[TEXT_AREA];
26915 }
26916 }
26917
26918 if (!r2->reversed_p)
26919 {
26920 /* Skip truncation and continuation glyphs near the end of the
26921 row, and also blanks and stretch glyphs inserted by
26922 extend_face_to_end_of_line. */
26923 while (end > glyph
26924 && INTEGERP ((end - 1)->object))
26925 --end;
26926 /* Scan the rest of the glyph row from the end, looking for the
26927 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26928 DISP_STRING, or whose position is between START_CHARPOS
26929 and END_CHARPOS */
26930 for (--end;
26931 end > glyph
26932 && !INTEGERP (end->object)
26933 && !EQ (end->object, disp_string)
26934 && !(BUFFERP (end->object)
26935 && (end->charpos >= start_charpos
26936 && end->charpos < end_charpos));
26937 --end)
26938 {
26939 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26940 are present at buffer positions between START_CHARPOS and
26941 END_CHARPOS, or if they come from an overlay. */
26942 if (EQ (end->object, before_string))
26943 {
26944 pos = string_buffer_position (before_string, start_charpos);
26945 if (!pos || (pos >= start_charpos && pos < end_charpos))
26946 break;
26947 }
26948 else if (EQ (end->object, after_string))
26949 {
26950 pos = string_buffer_position (after_string, end_charpos);
26951 if (!pos || (pos >= start_charpos && pos < end_charpos))
26952 break;
26953 }
26954 }
26955 /* Find the X coordinate of the last glyph to be highlighted. */
26956 for (; glyph <= end; ++glyph)
26957 x += glyph->pixel_width;
26958
26959 hlinfo->mouse_face_end_x = x;
26960 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26961 }
26962 else
26963 {
26964 /* Skip truncation and continuation glyphs near the end of the
26965 row, and also blanks and stretch glyphs inserted by
26966 extend_face_to_end_of_line. */
26967 x = r2->x;
26968 end++;
26969 while (end < glyph
26970 && INTEGERP (end->object))
26971 {
26972 x += end->pixel_width;
26973 ++end;
26974 }
26975 /* Scan the rest of the glyph row from the end, looking for the
26976 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26977 DISP_STRING, or whose position is between START_CHARPOS
26978 and END_CHARPOS */
26979 for ( ;
26980 end < glyph
26981 && !INTEGERP (end->object)
26982 && !EQ (end->object, disp_string)
26983 && !(BUFFERP (end->object)
26984 && (end->charpos >= start_charpos
26985 && end->charpos < end_charpos));
26986 ++end)
26987 {
26988 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26989 are present at buffer positions between START_CHARPOS and
26990 END_CHARPOS, or if they come from an overlay. */
26991 if (EQ (end->object, before_string))
26992 {
26993 pos = string_buffer_position (before_string, start_charpos);
26994 if (!pos || (pos >= start_charpos && pos < end_charpos))
26995 break;
26996 }
26997 else if (EQ (end->object, after_string))
26998 {
26999 pos = string_buffer_position (after_string, end_charpos);
27000 if (!pos || (pos >= start_charpos && pos < end_charpos))
27001 break;
27002 }
27003 x += end->pixel_width;
27004 }
27005 /* If we exited the above loop because we arrived at the last
27006 glyph of the row, and its buffer position is still not in
27007 range, it means the last character in range is the preceding
27008 newline. Bump the end column and x values to get past the
27009 last glyph. */
27010 if (end == glyph
27011 && BUFFERP (end->object)
27012 && (end->charpos < start_charpos
27013 || end->charpos >= end_charpos))
27014 {
27015 x += end->pixel_width;
27016 ++end;
27017 }
27018 hlinfo->mouse_face_end_x = x;
27019 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27020 }
27021
27022 hlinfo->mouse_face_window = window;
27023 hlinfo->mouse_face_face_id
27024 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27025 mouse_charpos + 1,
27026 !hlinfo->mouse_face_hidden, -1);
27027 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27028 }
27029
27030 /* The following function is not used anymore (replaced with
27031 mouse_face_from_string_pos), but I leave it here for the time
27032 being, in case someone would. */
27033
27034 #if 0 /* not used */
27035
27036 /* Find the position of the glyph for position POS in OBJECT in
27037 window W's current matrix, and return in *X, *Y the pixel
27038 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27039
27040 RIGHT_P non-zero means return the position of the right edge of the
27041 glyph, RIGHT_P zero means return the left edge position.
27042
27043 If no glyph for POS exists in the matrix, return the position of
27044 the glyph with the next smaller position that is in the matrix, if
27045 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27046 exists in the matrix, return the position of the glyph with the
27047 next larger position in OBJECT.
27048
27049 Value is non-zero if a glyph was found. */
27050
27051 static int
27052 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27053 int *hpos, int *vpos, int *x, int *y, int right_p)
27054 {
27055 int yb = window_text_bottom_y (w);
27056 struct glyph_row *r;
27057 struct glyph *best_glyph = NULL;
27058 struct glyph_row *best_row = NULL;
27059 int best_x = 0;
27060
27061 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27062 r->enabled_p && r->y < yb;
27063 ++r)
27064 {
27065 struct glyph *g = r->glyphs[TEXT_AREA];
27066 struct glyph *e = g + r->used[TEXT_AREA];
27067 int gx;
27068
27069 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27070 if (EQ (g->object, object))
27071 {
27072 if (g->charpos == pos)
27073 {
27074 best_glyph = g;
27075 best_x = gx;
27076 best_row = r;
27077 goto found;
27078 }
27079 else if (best_glyph == NULL
27080 || ((eabs (g->charpos - pos)
27081 < eabs (best_glyph->charpos - pos))
27082 && (right_p
27083 ? g->charpos < pos
27084 : g->charpos > pos)))
27085 {
27086 best_glyph = g;
27087 best_x = gx;
27088 best_row = r;
27089 }
27090 }
27091 }
27092
27093 found:
27094
27095 if (best_glyph)
27096 {
27097 *x = best_x;
27098 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27099
27100 if (right_p)
27101 {
27102 *x += best_glyph->pixel_width;
27103 ++*hpos;
27104 }
27105
27106 *y = best_row->y;
27107 *vpos = best_row - w->current_matrix->rows;
27108 }
27109
27110 return best_glyph != NULL;
27111 }
27112 #endif /* not used */
27113
27114 /* Find the positions of the first and the last glyphs in window W's
27115 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27116 (assumed to be a string), and return in HLINFO's mouse_face_*
27117 members the pixel and column/row coordinates of those glyphs. */
27118
27119 static void
27120 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27121 Lisp_Object object,
27122 ptrdiff_t startpos, ptrdiff_t endpos)
27123 {
27124 int yb = window_text_bottom_y (w);
27125 struct glyph_row *r;
27126 struct glyph *g, *e;
27127 int gx;
27128 int found = 0;
27129
27130 /* Find the glyph row with at least one position in the range
27131 [STARTPOS..ENDPOS], and the first glyph in that row whose
27132 position belongs to that range. */
27133 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27134 r->enabled_p && r->y < yb;
27135 ++r)
27136 {
27137 if (!r->reversed_p)
27138 {
27139 g = r->glyphs[TEXT_AREA];
27140 e = g + r->used[TEXT_AREA];
27141 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27142 if (EQ (g->object, object)
27143 && startpos <= g->charpos && g->charpos <= endpos)
27144 {
27145 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27146 hlinfo->mouse_face_beg_y = r->y;
27147 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27148 hlinfo->mouse_face_beg_x = gx;
27149 found = 1;
27150 break;
27151 }
27152 }
27153 else
27154 {
27155 struct glyph *g1;
27156
27157 e = r->glyphs[TEXT_AREA];
27158 g = e + r->used[TEXT_AREA];
27159 for ( ; g > e; --g)
27160 if (EQ ((g-1)->object, object)
27161 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27162 {
27163 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27164 hlinfo->mouse_face_beg_y = r->y;
27165 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27166 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27167 gx += g1->pixel_width;
27168 hlinfo->mouse_face_beg_x = gx;
27169 found = 1;
27170 break;
27171 }
27172 }
27173 if (found)
27174 break;
27175 }
27176
27177 if (!found)
27178 return;
27179
27180 /* Starting with the next row, look for the first row which does NOT
27181 include any glyphs whose positions are in the range. */
27182 for (++r; r->enabled_p && r->y < yb; ++r)
27183 {
27184 g = r->glyphs[TEXT_AREA];
27185 e = g + r->used[TEXT_AREA];
27186 found = 0;
27187 for ( ; g < e; ++g)
27188 if (EQ (g->object, object)
27189 && startpos <= g->charpos && g->charpos <= endpos)
27190 {
27191 found = 1;
27192 break;
27193 }
27194 if (!found)
27195 break;
27196 }
27197
27198 /* The highlighted region ends on the previous row. */
27199 r--;
27200
27201 /* Set the end row and its vertical pixel coordinate. */
27202 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27203 hlinfo->mouse_face_end_y = r->y;
27204
27205 /* Compute and set the end column and the end column's horizontal
27206 pixel coordinate. */
27207 if (!r->reversed_p)
27208 {
27209 g = r->glyphs[TEXT_AREA];
27210 e = g + r->used[TEXT_AREA];
27211 for ( ; e > g; --e)
27212 if (EQ ((e-1)->object, object)
27213 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27214 break;
27215 hlinfo->mouse_face_end_col = e - g;
27216
27217 for (gx = r->x; g < e; ++g)
27218 gx += g->pixel_width;
27219 hlinfo->mouse_face_end_x = gx;
27220 }
27221 else
27222 {
27223 e = r->glyphs[TEXT_AREA];
27224 g = e + r->used[TEXT_AREA];
27225 for (gx = r->x ; e < g; ++e)
27226 {
27227 if (EQ (e->object, object)
27228 && startpos <= e->charpos && e->charpos <= endpos)
27229 break;
27230 gx += e->pixel_width;
27231 }
27232 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27233 hlinfo->mouse_face_end_x = gx;
27234 }
27235 }
27236
27237 #ifdef HAVE_WINDOW_SYSTEM
27238
27239 /* See if position X, Y is within a hot-spot of an image. */
27240
27241 static int
27242 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27243 {
27244 if (!CONSP (hot_spot))
27245 return 0;
27246
27247 if (EQ (XCAR (hot_spot), Qrect))
27248 {
27249 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27250 Lisp_Object rect = XCDR (hot_spot);
27251 Lisp_Object tem;
27252 if (!CONSP (rect))
27253 return 0;
27254 if (!CONSP (XCAR (rect)))
27255 return 0;
27256 if (!CONSP (XCDR (rect)))
27257 return 0;
27258 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27259 return 0;
27260 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27261 return 0;
27262 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27263 return 0;
27264 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27265 return 0;
27266 return 1;
27267 }
27268 else if (EQ (XCAR (hot_spot), Qcircle))
27269 {
27270 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27271 Lisp_Object circ = XCDR (hot_spot);
27272 Lisp_Object lr, lx0, ly0;
27273 if (CONSP (circ)
27274 && CONSP (XCAR (circ))
27275 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27276 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27277 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27278 {
27279 double r = XFLOATINT (lr);
27280 double dx = XINT (lx0) - x;
27281 double dy = XINT (ly0) - y;
27282 return (dx * dx + dy * dy <= r * r);
27283 }
27284 }
27285 else if (EQ (XCAR (hot_spot), Qpoly))
27286 {
27287 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27288 if (VECTORP (XCDR (hot_spot)))
27289 {
27290 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27291 Lisp_Object *poly = v->contents;
27292 ptrdiff_t n = v->header.size;
27293 ptrdiff_t i;
27294 int inside = 0;
27295 Lisp_Object lx, ly;
27296 int x0, y0;
27297
27298 /* Need an even number of coordinates, and at least 3 edges. */
27299 if (n < 6 || n & 1)
27300 return 0;
27301
27302 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27303 If count is odd, we are inside polygon. Pixels on edges
27304 may or may not be included depending on actual geometry of the
27305 polygon. */
27306 if ((lx = poly[n-2], !INTEGERP (lx))
27307 || (ly = poly[n-1], !INTEGERP (lx)))
27308 return 0;
27309 x0 = XINT (lx), y0 = XINT (ly);
27310 for (i = 0; i < n; i += 2)
27311 {
27312 int x1 = x0, y1 = y0;
27313 if ((lx = poly[i], !INTEGERP (lx))
27314 || (ly = poly[i+1], !INTEGERP (ly)))
27315 return 0;
27316 x0 = XINT (lx), y0 = XINT (ly);
27317
27318 /* Does this segment cross the X line? */
27319 if (x0 >= x)
27320 {
27321 if (x1 >= x)
27322 continue;
27323 }
27324 else if (x1 < x)
27325 continue;
27326 if (y > y0 && y > y1)
27327 continue;
27328 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27329 inside = !inside;
27330 }
27331 return inside;
27332 }
27333 }
27334 return 0;
27335 }
27336
27337 Lisp_Object
27338 find_hot_spot (Lisp_Object map, int x, int y)
27339 {
27340 while (CONSP (map))
27341 {
27342 if (CONSP (XCAR (map))
27343 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27344 return XCAR (map);
27345 map = XCDR (map);
27346 }
27347
27348 return Qnil;
27349 }
27350
27351 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27352 3, 3, 0,
27353 doc: /* Lookup in image map MAP coordinates X and Y.
27354 An image map is an alist where each element has the format (AREA ID PLIST).
27355 An AREA is specified as either a rectangle, a circle, or a polygon:
27356 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27357 pixel coordinates of the upper left and bottom right corners.
27358 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27359 and the radius of the circle; r may be a float or integer.
27360 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27361 vector describes one corner in the polygon.
27362 Returns the alist element for the first matching AREA in MAP. */)
27363 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27364 {
27365 if (NILP (map))
27366 return Qnil;
27367
27368 CHECK_NUMBER (x);
27369 CHECK_NUMBER (y);
27370
27371 return find_hot_spot (map,
27372 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27373 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27374 }
27375
27376
27377 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27378 static void
27379 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27380 {
27381 /* Do not change cursor shape while dragging mouse. */
27382 if (!NILP (do_mouse_tracking))
27383 return;
27384
27385 if (!NILP (pointer))
27386 {
27387 if (EQ (pointer, Qarrow))
27388 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27389 else if (EQ (pointer, Qhand))
27390 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27391 else if (EQ (pointer, Qtext))
27392 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27393 else if (EQ (pointer, intern ("hdrag")))
27394 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27395 #ifdef HAVE_X_WINDOWS
27396 else if (EQ (pointer, intern ("vdrag")))
27397 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27398 #endif
27399 else if (EQ (pointer, intern ("hourglass")))
27400 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27401 else if (EQ (pointer, Qmodeline))
27402 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27403 else
27404 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27405 }
27406
27407 if (cursor != No_Cursor)
27408 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27409 }
27410
27411 #endif /* HAVE_WINDOW_SYSTEM */
27412
27413 /* Take proper action when mouse has moved to the mode or header line
27414 or marginal area AREA of window W, x-position X and y-position Y.
27415 X is relative to the start of the text display area of W, so the
27416 width of bitmap areas and scroll bars must be subtracted to get a
27417 position relative to the start of the mode line. */
27418
27419 static void
27420 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27421 enum window_part area)
27422 {
27423 struct window *w = XWINDOW (window);
27424 struct frame *f = XFRAME (w->frame);
27425 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27426 #ifdef HAVE_WINDOW_SYSTEM
27427 Display_Info *dpyinfo;
27428 #endif
27429 Cursor cursor = No_Cursor;
27430 Lisp_Object pointer = Qnil;
27431 int dx, dy, width, height;
27432 ptrdiff_t charpos;
27433 Lisp_Object string, object = Qnil;
27434 Lisp_Object pos IF_LINT (= Qnil), help;
27435
27436 Lisp_Object mouse_face;
27437 int original_x_pixel = x;
27438 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27439 struct glyph_row *row IF_LINT (= 0);
27440
27441 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27442 {
27443 int x0;
27444 struct glyph *end;
27445
27446 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27447 returns them in row/column units! */
27448 string = mode_line_string (w, area, &x, &y, &charpos,
27449 &object, &dx, &dy, &width, &height);
27450
27451 row = (area == ON_MODE_LINE
27452 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27453 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27454
27455 /* Find the glyph under the mouse pointer. */
27456 if (row->mode_line_p && row->enabled_p)
27457 {
27458 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27459 end = glyph + row->used[TEXT_AREA];
27460
27461 for (x0 = original_x_pixel;
27462 glyph < end && x0 >= glyph->pixel_width;
27463 ++glyph)
27464 x0 -= glyph->pixel_width;
27465
27466 if (glyph >= end)
27467 glyph = NULL;
27468 }
27469 }
27470 else
27471 {
27472 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27473 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27474 returns them in row/column units! */
27475 string = marginal_area_string (w, area, &x, &y, &charpos,
27476 &object, &dx, &dy, &width, &height);
27477 }
27478
27479 help = Qnil;
27480
27481 #ifdef HAVE_WINDOW_SYSTEM
27482 if (IMAGEP (object))
27483 {
27484 Lisp_Object image_map, hotspot;
27485 if ((image_map = Fplist_get (XCDR (object), QCmap),
27486 !NILP (image_map))
27487 && (hotspot = find_hot_spot (image_map, dx, dy),
27488 CONSP (hotspot))
27489 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27490 {
27491 Lisp_Object plist;
27492
27493 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27494 If so, we could look for mouse-enter, mouse-leave
27495 properties in PLIST (and do something...). */
27496 hotspot = XCDR (hotspot);
27497 if (CONSP (hotspot)
27498 && (plist = XCAR (hotspot), CONSP (plist)))
27499 {
27500 pointer = Fplist_get (plist, Qpointer);
27501 if (NILP (pointer))
27502 pointer = Qhand;
27503 help = Fplist_get (plist, Qhelp_echo);
27504 if (!NILP (help))
27505 {
27506 help_echo_string = help;
27507 XSETWINDOW (help_echo_window, w);
27508 help_echo_object = w->buffer;
27509 help_echo_pos = charpos;
27510 }
27511 }
27512 }
27513 if (NILP (pointer))
27514 pointer = Fplist_get (XCDR (object), QCpointer);
27515 }
27516 #endif /* HAVE_WINDOW_SYSTEM */
27517
27518 if (STRINGP (string))
27519 pos = make_number (charpos);
27520
27521 /* Set the help text and mouse pointer. If the mouse is on a part
27522 of the mode line without any text (e.g. past the right edge of
27523 the mode line text), use the default help text and pointer. */
27524 if (STRINGP (string) || area == ON_MODE_LINE)
27525 {
27526 /* Arrange to display the help by setting the global variables
27527 help_echo_string, help_echo_object, and help_echo_pos. */
27528 if (NILP (help))
27529 {
27530 if (STRINGP (string))
27531 help = Fget_text_property (pos, Qhelp_echo, string);
27532
27533 if (!NILP (help))
27534 {
27535 help_echo_string = help;
27536 XSETWINDOW (help_echo_window, w);
27537 help_echo_object = string;
27538 help_echo_pos = charpos;
27539 }
27540 else if (area == ON_MODE_LINE)
27541 {
27542 Lisp_Object default_help
27543 = buffer_local_value_1 (Qmode_line_default_help_echo,
27544 w->buffer);
27545
27546 if (STRINGP (default_help))
27547 {
27548 help_echo_string = default_help;
27549 XSETWINDOW (help_echo_window, w);
27550 help_echo_object = Qnil;
27551 help_echo_pos = -1;
27552 }
27553 }
27554 }
27555
27556 #ifdef HAVE_WINDOW_SYSTEM
27557 /* Change the mouse pointer according to what is under it. */
27558 if (FRAME_WINDOW_P (f))
27559 {
27560 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27561 if (STRINGP (string))
27562 {
27563 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27564
27565 if (NILP (pointer))
27566 pointer = Fget_text_property (pos, Qpointer, string);
27567
27568 /* Change the mouse pointer according to what is under X/Y. */
27569 if (NILP (pointer)
27570 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27571 {
27572 Lisp_Object map;
27573 map = Fget_text_property (pos, Qlocal_map, string);
27574 if (!KEYMAPP (map))
27575 map = Fget_text_property (pos, Qkeymap, string);
27576 if (!KEYMAPP (map))
27577 cursor = dpyinfo->vertical_scroll_bar_cursor;
27578 }
27579 }
27580 else
27581 /* Default mode-line pointer. */
27582 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27583 }
27584 #endif
27585 }
27586
27587 /* Change the mouse face according to what is under X/Y. */
27588 if (STRINGP (string))
27589 {
27590 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27591 if (!NILP (mouse_face)
27592 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27593 && glyph)
27594 {
27595 Lisp_Object b, e;
27596
27597 struct glyph * tmp_glyph;
27598
27599 int gpos;
27600 int gseq_length;
27601 int total_pixel_width;
27602 ptrdiff_t begpos, endpos, ignore;
27603
27604 int vpos, hpos;
27605
27606 b = Fprevious_single_property_change (make_number (charpos + 1),
27607 Qmouse_face, string, Qnil);
27608 if (NILP (b))
27609 begpos = 0;
27610 else
27611 begpos = XINT (b);
27612
27613 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27614 if (NILP (e))
27615 endpos = SCHARS (string);
27616 else
27617 endpos = XINT (e);
27618
27619 /* Calculate the glyph position GPOS of GLYPH in the
27620 displayed string, relative to the beginning of the
27621 highlighted part of the string.
27622
27623 Note: GPOS is different from CHARPOS. CHARPOS is the
27624 position of GLYPH in the internal string object. A mode
27625 line string format has structures which are converted to
27626 a flattened string by the Emacs Lisp interpreter. The
27627 internal string is an element of those structures. The
27628 displayed string is the flattened string. */
27629 tmp_glyph = row_start_glyph;
27630 while (tmp_glyph < glyph
27631 && (!(EQ (tmp_glyph->object, glyph->object)
27632 && begpos <= tmp_glyph->charpos
27633 && tmp_glyph->charpos < endpos)))
27634 tmp_glyph++;
27635 gpos = glyph - tmp_glyph;
27636
27637 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27638 the highlighted part of the displayed string to which
27639 GLYPH belongs. Note: GSEQ_LENGTH is different from
27640 SCHARS (STRING), because the latter returns the length of
27641 the internal string. */
27642 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27643 tmp_glyph > glyph
27644 && (!(EQ (tmp_glyph->object, glyph->object)
27645 && begpos <= tmp_glyph->charpos
27646 && tmp_glyph->charpos < endpos));
27647 tmp_glyph--)
27648 ;
27649 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27650
27651 /* Calculate the total pixel width of all the glyphs between
27652 the beginning of the highlighted area and GLYPH. */
27653 total_pixel_width = 0;
27654 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27655 total_pixel_width += tmp_glyph->pixel_width;
27656
27657 /* Pre calculation of re-rendering position. Note: X is in
27658 column units here, after the call to mode_line_string or
27659 marginal_area_string. */
27660 hpos = x - gpos;
27661 vpos = (area == ON_MODE_LINE
27662 ? (w->current_matrix)->nrows - 1
27663 : 0);
27664
27665 /* If GLYPH's position is included in the region that is
27666 already drawn in mouse face, we have nothing to do. */
27667 if ( EQ (window, hlinfo->mouse_face_window)
27668 && (!row->reversed_p
27669 ? (hlinfo->mouse_face_beg_col <= hpos
27670 && hpos < hlinfo->mouse_face_end_col)
27671 /* In R2L rows we swap BEG and END, see below. */
27672 : (hlinfo->mouse_face_end_col <= hpos
27673 && hpos < hlinfo->mouse_face_beg_col))
27674 && hlinfo->mouse_face_beg_row == vpos )
27675 return;
27676
27677 if (clear_mouse_face (hlinfo))
27678 cursor = No_Cursor;
27679
27680 if (!row->reversed_p)
27681 {
27682 hlinfo->mouse_face_beg_col = hpos;
27683 hlinfo->mouse_face_beg_x = original_x_pixel
27684 - (total_pixel_width + dx);
27685 hlinfo->mouse_face_end_col = hpos + gseq_length;
27686 hlinfo->mouse_face_end_x = 0;
27687 }
27688 else
27689 {
27690 /* In R2L rows, show_mouse_face expects BEG and END
27691 coordinates to be swapped. */
27692 hlinfo->mouse_face_end_col = hpos;
27693 hlinfo->mouse_face_end_x = original_x_pixel
27694 - (total_pixel_width + dx);
27695 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27696 hlinfo->mouse_face_beg_x = 0;
27697 }
27698
27699 hlinfo->mouse_face_beg_row = vpos;
27700 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27701 hlinfo->mouse_face_beg_y = 0;
27702 hlinfo->mouse_face_end_y = 0;
27703 hlinfo->mouse_face_past_end = 0;
27704 hlinfo->mouse_face_window = window;
27705
27706 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27707 charpos,
27708 0, 0, 0,
27709 &ignore,
27710 glyph->face_id,
27711 1);
27712 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27713
27714 if (NILP (pointer))
27715 pointer = Qhand;
27716 }
27717 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27718 clear_mouse_face (hlinfo);
27719 }
27720 #ifdef HAVE_WINDOW_SYSTEM
27721 if (FRAME_WINDOW_P (f))
27722 define_frame_cursor1 (f, cursor, pointer);
27723 #endif
27724 }
27725
27726
27727 /* EXPORT:
27728 Take proper action when the mouse has moved to position X, Y on
27729 frame F as regards highlighting characters that have mouse-face
27730 properties. Also de-highlighting chars where the mouse was before.
27731 X and Y can be negative or out of range. */
27732
27733 void
27734 note_mouse_highlight (struct frame *f, int x, int y)
27735 {
27736 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27737 enum window_part part = ON_NOTHING;
27738 Lisp_Object window;
27739 struct window *w;
27740 Cursor cursor = No_Cursor;
27741 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27742 struct buffer *b;
27743
27744 /* When a menu is active, don't highlight because this looks odd. */
27745 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27746 if (popup_activated ())
27747 return;
27748 #endif
27749
27750 if (NILP (Vmouse_highlight)
27751 || !f->glyphs_initialized_p
27752 || f->pointer_invisible)
27753 return;
27754
27755 hlinfo->mouse_face_mouse_x = x;
27756 hlinfo->mouse_face_mouse_y = y;
27757 hlinfo->mouse_face_mouse_frame = f;
27758
27759 if (hlinfo->mouse_face_defer)
27760 return;
27761
27762 /* Which window is that in? */
27763 window = window_from_coordinates (f, x, y, &part, 1);
27764
27765 /* If displaying active text in another window, clear that. */
27766 if (! EQ (window, hlinfo->mouse_face_window)
27767 /* Also clear if we move out of text area in same window. */
27768 || (!NILP (hlinfo->mouse_face_window)
27769 && !NILP (window)
27770 && part != ON_TEXT
27771 && part != ON_MODE_LINE
27772 && part != ON_HEADER_LINE))
27773 clear_mouse_face (hlinfo);
27774
27775 /* Not on a window -> return. */
27776 if (!WINDOWP (window))
27777 return;
27778
27779 /* Reset help_echo_string. It will get recomputed below. */
27780 help_echo_string = Qnil;
27781
27782 /* Convert to window-relative pixel coordinates. */
27783 w = XWINDOW (window);
27784 frame_to_window_pixel_xy (w, &x, &y);
27785
27786 #ifdef HAVE_WINDOW_SYSTEM
27787 /* Handle tool-bar window differently since it doesn't display a
27788 buffer. */
27789 if (EQ (window, f->tool_bar_window))
27790 {
27791 note_tool_bar_highlight (f, x, y);
27792 return;
27793 }
27794 #endif
27795
27796 /* Mouse is on the mode, header line or margin? */
27797 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27798 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27799 {
27800 note_mode_line_or_margin_highlight (window, x, y, part);
27801 return;
27802 }
27803
27804 #ifdef HAVE_WINDOW_SYSTEM
27805 if (part == ON_VERTICAL_BORDER)
27806 {
27807 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27808 help_echo_string = build_string ("drag-mouse-1: resize");
27809 }
27810 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27811 || part == ON_SCROLL_BAR)
27812 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27813 else
27814 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27815 #endif
27816
27817 /* Are we in a window whose display is up to date?
27818 And verify the buffer's text has not changed. */
27819 b = XBUFFER (w->buffer);
27820 if (part == ON_TEXT
27821 && EQ (w->window_end_valid, w->buffer)
27822 && w->last_modified == BUF_MODIFF (b)
27823 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27824 {
27825 int hpos, vpos, dx, dy, area = LAST_AREA;
27826 ptrdiff_t pos;
27827 struct glyph *glyph;
27828 Lisp_Object object;
27829 Lisp_Object mouse_face = Qnil, position;
27830 Lisp_Object *overlay_vec = NULL;
27831 ptrdiff_t i, noverlays;
27832 struct buffer *obuf;
27833 ptrdiff_t obegv, ozv;
27834 int same_region;
27835
27836 /* Find the glyph under X/Y. */
27837 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27838
27839 #ifdef HAVE_WINDOW_SYSTEM
27840 /* Look for :pointer property on image. */
27841 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27842 {
27843 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27844 if (img != NULL && IMAGEP (img->spec))
27845 {
27846 Lisp_Object image_map, hotspot;
27847 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27848 !NILP (image_map))
27849 && (hotspot = find_hot_spot (image_map,
27850 glyph->slice.img.x + dx,
27851 glyph->slice.img.y + dy),
27852 CONSP (hotspot))
27853 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27854 {
27855 Lisp_Object plist;
27856
27857 /* Could check XCAR (hotspot) to see if we enter/leave
27858 this hot-spot.
27859 If so, we could look for mouse-enter, mouse-leave
27860 properties in PLIST (and do something...). */
27861 hotspot = XCDR (hotspot);
27862 if (CONSP (hotspot)
27863 && (plist = XCAR (hotspot), CONSP (plist)))
27864 {
27865 pointer = Fplist_get (plist, Qpointer);
27866 if (NILP (pointer))
27867 pointer = Qhand;
27868 help_echo_string = Fplist_get (plist, Qhelp_echo);
27869 if (!NILP (help_echo_string))
27870 {
27871 help_echo_window = window;
27872 help_echo_object = glyph->object;
27873 help_echo_pos = glyph->charpos;
27874 }
27875 }
27876 }
27877 if (NILP (pointer))
27878 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27879 }
27880 }
27881 #endif /* HAVE_WINDOW_SYSTEM */
27882
27883 /* Clear mouse face if X/Y not over text. */
27884 if (glyph == NULL
27885 || area != TEXT_AREA
27886 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27887 /* Glyph's OBJECT is an integer for glyphs inserted by the
27888 display engine for its internal purposes, like truncation
27889 and continuation glyphs and blanks beyond the end of
27890 line's text on text terminals. If we are over such a
27891 glyph, we are not over any text. */
27892 || INTEGERP (glyph->object)
27893 /* R2L rows have a stretch glyph at their front, which
27894 stands for no text, whereas L2R rows have no glyphs at
27895 all beyond the end of text. Treat such stretch glyphs
27896 like we do with NULL glyphs in L2R rows. */
27897 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27898 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27899 && glyph->type == STRETCH_GLYPH
27900 && glyph->avoid_cursor_p))
27901 {
27902 if (clear_mouse_face (hlinfo))
27903 cursor = No_Cursor;
27904 #ifdef HAVE_WINDOW_SYSTEM
27905 if (FRAME_WINDOW_P (f) && NILP (pointer))
27906 {
27907 if (area != TEXT_AREA)
27908 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27909 else
27910 pointer = Vvoid_text_area_pointer;
27911 }
27912 #endif
27913 goto set_cursor;
27914 }
27915
27916 pos = glyph->charpos;
27917 object = glyph->object;
27918 if (!STRINGP (object) && !BUFFERP (object))
27919 goto set_cursor;
27920
27921 /* If we get an out-of-range value, return now; avoid an error. */
27922 if (BUFFERP (object) && pos > BUF_Z (b))
27923 goto set_cursor;
27924
27925 /* Make the window's buffer temporarily current for
27926 overlays_at and compute_char_face. */
27927 obuf = current_buffer;
27928 current_buffer = b;
27929 obegv = BEGV;
27930 ozv = ZV;
27931 BEGV = BEG;
27932 ZV = Z;
27933
27934 /* Is this char mouse-active or does it have help-echo? */
27935 position = make_number (pos);
27936
27937 if (BUFFERP (object))
27938 {
27939 /* Put all the overlays we want in a vector in overlay_vec. */
27940 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27941 /* Sort overlays into increasing priority order. */
27942 noverlays = sort_overlays (overlay_vec, noverlays, w);
27943 }
27944 else
27945 noverlays = 0;
27946
27947 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27948
27949 if (same_region)
27950 cursor = No_Cursor;
27951
27952 /* Check mouse-face highlighting. */
27953 if (! same_region
27954 /* If there exists an overlay with mouse-face overlapping
27955 the one we are currently highlighting, we have to
27956 check if we enter the overlapping overlay, and then
27957 highlight only that. */
27958 || (OVERLAYP (hlinfo->mouse_face_overlay)
27959 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27960 {
27961 /* Find the highest priority overlay with a mouse-face. */
27962 Lisp_Object overlay = Qnil;
27963 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27964 {
27965 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27966 if (!NILP (mouse_face))
27967 overlay = overlay_vec[i];
27968 }
27969
27970 /* If we're highlighting the same overlay as before, there's
27971 no need to do that again. */
27972 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27973 goto check_help_echo;
27974 hlinfo->mouse_face_overlay = overlay;
27975
27976 /* Clear the display of the old active region, if any. */
27977 if (clear_mouse_face (hlinfo))
27978 cursor = No_Cursor;
27979
27980 /* If no overlay applies, get a text property. */
27981 if (NILP (overlay))
27982 mouse_face = Fget_text_property (position, Qmouse_face, object);
27983
27984 /* Next, compute the bounds of the mouse highlighting and
27985 display it. */
27986 if (!NILP (mouse_face) && STRINGP (object))
27987 {
27988 /* The mouse-highlighting comes from a display string
27989 with a mouse-face. */
27990 Lisp_Object s, e;
27991 ptrdiff_t ignore;
27992
27993 s = Fprevious_single_property_change
27994 (make_number (pos + 1), Qmouse_face, object, Qnil);
27995 e = Fnext_single_property_change
27996 (position, Qmouse_face, object, Qnil);
27997 if (NILP (s))
27998 s = make_number (0);
27999 if (NILP (e))
28000 e = make_number (SCHARS (object) - 1);
28001 mouse_face_from_string_pos (w, hlinfo, object,
28002 XINT (s), XINT (e));
28003 hlinfo->mouse_face_past_end = 0;
28004 hlinfo->mouse_face_window = window;
28005 hlinfo->mouse_face_face_id
28006 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28007 glyph->face_id, 1);
28008 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28009 cursor = No_Cursor;
28010 }
28011 else
28012 {
28013 /* The mouse-highlighting, if any, comes from an overlay
28014 or text property in the buffer. */
28015 Lisp_Object buffer IF_LINT (= Qnil);
28016 Lisp_Object disp_string IF_LINT (= Qnil);
28017
28018 if (STRINGP (object))
28019 {
28020 /* If we are on a display string with no mouse-face,
28021 check if the text under it has one. */
28022 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28023 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28024 pos = string_buffer_position (object, start);
28025 if (pos > 0)
28026 {
28027 mouse_face = get_char_property_and_overlay
28028 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28029 buffer = w->buffer;
28030 disp_string = object;
28031 }
28032 }
28033 else
28034 {
28035 buffer = object;
28036 disp_string = Qnil;
28037 }
28038
28039 if (!NILP (mouse_face))
28040 {
28041 Lisp_Object before, after;
28042 Lisp_Object before_string, after_string;
28043 /* To correctly find the limits of mouse highlight
28044 in a bidi-reordered buffer, we must not use the
28045 optimization of limiting the search in
28046 previous-single-property-change and
28047 next-single-property-change, because
28048 rows_from_pos_range needs the real start and end
28049 positions to DTRT in this case. That's because
28050 the first row visible in a window does not
28051 necessarily display the character whose position
28052 is the smallest. */
28053 Lisp_Object lim1 =
28054 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28055 ? Fmarker_position (w->start)
28056 : Qnil;
28057 Lisp_Object lim2 =
28058 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28059 ? make_number (BUF_Z (XBUFFER (buffer))
28060 - XFASTINT (w->window_end_pos))
28061 : Qnil;
28062
28063 if (NILP (overlay))
28064 {
28065 /* Handle the text property case. */
28066 before = Fprevious_single_property_change
28067 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28068 after = Fnext_single_property_change
28069 (make_number (pos), Qmouse_face, buffer, lim2);
28070 before_string = after_string = Qnil;
28071 }
28072 else
28073 {
28074 /* Handle the overlay case. */
28075 before = Foverlay_start (overlay);
28076 after = Foverlay_end (overlay);
28077 before_string = Foverlay_get (overlay, Qbefore_string);
28078 after_string = Foverlay_get (overlay, Qafter_string);
28079
28080 if (!STRINGP (before_string)) before_string = Qnil;
28081 if (!STRINGP (after_string)) after_string = Qnil;
28082 }
28083
28084 mouse_face_from_buffer_pos (window, hlinfo, pos,
28085 NILP (before)
28086 ? 1
28087 : XFASTINT (before),
28088 NILP (after)
28089 ? BUF_Z (XBUFFER (buffer))
28090 : XFASTINT (after),
28091 before_string, after_string,
28092 disp_string);
28093 cursor = No_Cursor;
28094 }
28095 }
28096 }
28097
28098 check_help_echo:
28099
28100 /* Look for a `help-echo' property. */
28101 if (NILP (help_echo_string)) {
28102 Lisp_Object help, overlay;
28103
28104 /* Check overlays first. */
28105 help = overlay = Qnil;
28106 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28107 {
28108 overlay = overlay_vec[i];
28109 help = Foverlay_get (overlay, Qhelp_echo);
28110 }
28111
28112 if (!NILP (help))
28113 {
28114 help_echo_string = help;
28115 help_echo_window = window;
28116 help_echo_object = overlay;
28117 help_echo_pos = pos;
28118 }
28119 else
28120 {
28121 Lisp_Object obj = glyph->object;
28122 ptrdiff_t charpos = glyph->charpos;
28123
28124 /* Try text properties. */
28125 if (STRINGP (obj)
28126 && charpos >= 0
28127 && charpos < SCHARS (obj))
28128 {
28129 help = Fget_text_property (make_number (charpos),
28130 Qhelp_echo, obj);
28131 if (NILP (help))
28132 {
28133 /* If the string itself doesn't specify a help-echo,
28134 see if the buffer text ``under'' it does. */
28135 struct glyph_row *r
28136 = MATRIX_ROW (w->current_matrix, vpos);
28137 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28138 ptrdiff_t p = string_buffer_position (obj, start);
28139 if (p > 0)
28140 {
28141 help = Fget_char_property (make_number (p),
28142 Qhelp_echo, w->buffer);
28143 if (!NILP (help))
28144 {
28145 charpos = p;
28146 obj = w->buffer;
28147 }
28148 }
28149 }
28150 }
28151 else if (BUFFERP (obj)
28152 && charpos >= BEGV
28153 && charpos < ZV)
28154 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28155 obj);
28156
28157 if (!NILP (help))
28158 {
28159 help_echo_string = help;
28160 help_echo_window = window;
28161 help_echo_object = obj;
28162 help_echo_pos = charpos;
28163 }
28164 }
28165 }
28166
28167 #ifdef HAVE_WINDOW_SYSTEM
28168 /* Look for a `pointer' property. */
28169 if (FRAME_WINDOW_P (f) && NILP (pointer))
28170 {
28171 /* Check overlays first. */
28172 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28173 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28174
28175 if (NILP (pointer))
28176 {
28177 Lisp_Object obj = glyph->object;
28178 ptrdiff_t charpos = glyph->charpos;
28179
28180 /* Try text properties. */
28181 if (STRINGP (obj)
28182 && charpos >= 0
28183 && charpos < SCHARS (obj))
28184 {
28185 pointer = Fget_text_property (make_number (charpos),
28186 Qpointer, obj);
28187 if (NILP (pointer))
28188 {
28189 /* If the string itself doesn't specify a pointer,
28190 see if the buffer text ``under'' it does. */
28191 struct glyph_row *r
28192 = MATRIX_ROW (w->current_matrix, vpos);
28193 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28194 ptrdiff_t p = string_buffer_position (obj, start);
28195 if (p > 0)
28196 pointer = Fget_char_property (make_number (p),
28197 Qpointer, w->buffer);
28198 }
28199 }
28200 else if (BUFFERP (obj)
28201 && charpos >= BEGV
28202 && charpos < ZV)
28203 pointer = Fget_text_property (make_number (charpos),
28204 Qpointer, obj);
28205 }
28206 }
28207 #endif /* HAVE_WINDOW_SYSTEM */
28208
28209 BEGV = obegv;
28210 ZV = ozv;
28211 current_buffer = obuf;
28212 }
28213
28214 set_cursor:
28215
28216 #ifdef HAVE_WINDOW_SYSTEM
28217 if (FRAME_WINDOW_P (f))
28218 define_frame_cursor1 (f, cursor, pointer);
28219 #else
28220 /* This is here to prevent a compiler error, about "label at end of
28221 compound statement". */
28222 return;
28223 #endif
28224 }
28225
28226
28227 /* EXPORT for RIF:
28228 Clear any mouse-face on window W. This function is part of the
28229 redisplay interface, and is called from try_window_id and similar
28230 functions to ensure the mouse-highlight is off. */
28231
28232 void
28233 x_clear_window_mouse_face (struct window *w)
28234 {
28235 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28236 Lisp_Object window;
28237
28238 block_input ();
28239 XSETWINDOW (window, w);
28240 if (EQ (window, hlinfo->mouse_face_window))
28241 clear_mouse_face (hlinfo);
28242 unblock_input ();
28243 }
28244
28245
28246 /* EXPORT:
28247 Just discard the mouse face information for frame F, if any.
28248 This is used when the size of F is changed. */
28249
28250 void
28251 cancel_mouse_face (struct frame *f)
28252 {
28253 Lisp_Object window;
28254 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28255
28256 window = hlinfo->mouse_face_window;
28257 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28258 {
28259 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28260 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28261 hlinfo->mouse_face_window = Qnil;
28262 }
28263 }
28264
28265
28266 \f
28267 /***********************************************************************
28268 Exposure Events
28269 ***********************************************************************/
28270
28271 #ifdef HAVE_WINDOW_SYSTEM
28272
28273 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28274 which intersects rectangle R. R is in window-relative coordinates. */
28275
28276 static void
28277 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28278 enum glyph_row_area area)
28279 {
28280 struct glyph *first = row->glyphs[area];
28281 struct glyph *end = row->glyphs[area] + row->used[area];
28282 struct glyph *last;
28283 int first_x, start_x, x;
28284
28285 if (area == TEXT_AREA && row->fill_line_p)
28286 /* If row extends face to end of line write the whole line. */
28287 draw_glyphs (w, 0, row, area,
28288 0, row->used[area],
28289 DRAW_NORMAL_TEXT, 0);
28290 else
28291 {
28292 /* Set START_X to the window-relative start position for drawing glyphs of
28293 AREA. The first glyph of the text area can be partially visible.
28294 The first glyphs of other areas cannot. */
28295 start_x = window_box_left_offset (w, area);
28296 x = start_x;
28297 if (area == TEXT_AREA)
28298 x += row->x;
28299
28300 /* Find the first glyph that must be redrawn. */
28301 while (first < end
28302 && x + first->pixel_width < r->x)
28303 {
28304 x += first->pixel_width;
28305 ++first;
28306 }
28307
28308 /* Find the last one. */
28309 last = first;
28310 first_x = x;
28311 while (last < end
28312 && x < r->x + r->width)
28313 {
28314 x += last->pixel_width;
28315 ++last;
28316 }
28317
28318 /* Repaint. */
28319 if (last > first)
28320 draw_glyphs (w, first_x - start_x, row, area,
28321 first - row->glyphs[area], last - row->glyphs[area],
28322 DRAW_NORMAL_TEXT, 0);
28323 }
28324 }
28325
28326
28327 /* Redraw the parts of the glyph row ROW on window W intersecting
28328 rectangle R. R is in window-relative coordinates. Value is
28329 non-zero if mouse-face was overwritten. */
28330
28331 static int
28332 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28333 {
28334 eassert (row->enabled_p);
28335
28336 if (row->mode_line_p || w->pseudo_window_p)
28337 draw_glyphs (w, 0, row, TEXT_AREA,
28338 0, row->used[TEXT_AREA],
28339 DRAW_NORMAL_TEXT, 0);
28340 else
28341 {
28342 if (row->used[LEFT_MARGIN_AREA])
28343 expose_area (w, row, r, LEFT_MARGIN_AREA);
28344 if (row->used[TEXT_AREA])
28345 expose_area (w, row, r, TEXT_AREA);
28346 if (row->used[RIGHT_MARGIN_AREA])
28347 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28348 draw_row_fringe_bitmaps (w, row);
28349 }
28350
28351 return row->mouse_face_p;
28352 }
28353
28354
28355 /* Redraw those parts of glyphs rows during expose event handling that
28356 overlap other rows. Redrawing of an exposed line writes over parts
28357 of lines overlapping that exposed line; this function fixes that.
28358
28359 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28360 row in W's current matrix that is exposed and overlaps other rows.
28361 LAST_OVERLAPPING_ROW is the last such row. */
28362
28363 static void
28364 expose_overlaps (struct window *w,
28365 struct glyph_row *first_overlapping_row,
28366 struct glyph_row *last_overlapping_row,
28367 XRectangle *r)
28368 {
28369 struct glyph_row *row;
28370
28371 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28372 if (row->overlapping_p)
28373 {
28374 eassert (row->enabled_p && !row->mode_line_p);
28375
28376 row->clip = r;
28377 if (row->used[LEFT_MARGIN_AREA])
28378 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28379
28380 if (row->used[TEXT_AREA])
28381 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28382
28383 if (row->used[RIGHT_MARGIN_AREA])
28384 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28385 row->clip = NULL;
28386 }
28387 }
28388
28389
28390 /* Return non-zero if W's cursor intersects rectangle R. */
28391
28392 static int
28393 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28394 {
28395 XRectangle cr, result;
28396 struct glyph *cursor_glyph;
28397 struct glyph_row *row;
28398
28399 if (w->phys_cursor.vpos >= 0
28400 && w->phys_cursor.vpos < w->current_matrix->nrows
28401 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28402 row->enabled_p)
28403 && row->cursor_in_fringe_p)
28404 {
28405 /* Cursor is in the fringe. */
28406 cr.x = window_box_right_offset (w,
28407 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28408 ? RIGHT_MARGIN_AREA
28409 : TEXT_AREA));
28410 cr.y = row->y;
28411 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28412 cr.height = row->height;
28413 return x_intersect_rectangles (&cr, r, &result);
28414 }
28415
28416 cursor_glyph = get_phys_cursor_glyph (w);
28417 if (cursor_glyph)
28418 {
28419 /* r is relative to W's box, but w->phys_cursor.x is relative
28420 to left edge of W's TEXT area. Adjust it. */
28421 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28422 cr.y = w->phys_cursor.y;
28423 cr.width = cursor_glyph->pixel_width;
28424 cr.height = w->phys_cursor_height;
28425 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28426 I assume the effect is the same -- and this is portable. */
28427 return x_intersect_rectangles (&cr, r, &result);
28428 }
28429 /* If we don't understand the format, pretend we're not in the hot-spot. */
28430 return 0;
28431 }
28432
28433
28434 /* EXPORT:
28435 Draw a vertical window border to the right of window W if W doesn't
28436 have vertical scroll bars. */
28437
28438 void
28439 x_draw_vertical_border (struct window *w)
28440 {
28441 struct frame *f = XFRAME (WINDOW_FRAME (w));
28442
28443 /* We could do better, if we knew what type of scroll-bar the adjacent
28444 windows (on either side) have... But we don't :-(
28445 However, I think this works ok. ++KFS 2003-04-25 */
28446
28447 /* Redraw borders between horizontally adjacent windows. Don't
28448 do it for frames with vertical scroll bars because either the
28449 right scroll bar of a window, or the left scroll bar of its
28450 neighbor will suffice as a border. */
28451 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28452 return;
28453
28454 if (!WINDOW_RIGHTMOST_P (w)
28455 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28456 {
28457 int x0, x1, y0, y1;
28458
28459 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28460 y1 -= 1;
28461
28462 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28463 x1 -= 1;
28464
28465 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28466 }
28467 else if (!WINDOW_LEFTMOST_P (w)
28468 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28469 {
28470 int x0, x1, y0, y1;
28471
28472 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28473 y1 -= 1;
28474
28475 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28476 x0 -= 1;
28477
28478 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28479 }
28480 }
28481
28482
28483 /* Redraw the part of window W intersection rectangle FR. Pixel
28484 coordinates in FR are frame-relative. Call this function with
28485 input blocked. Value is non-zero if the exposure overwrites
28486 mouse-face. */
28487
28488 static int
28489 expose_window (struct window *w, XRectangle *fr)
28490 {
28491 struct frame *f = XFRAME (w->frame);
28492 XRectangle wr, r;
28493 int mouse_face_overwritten_p = 0;
28494
28495 /* If window is not yet fully initialized, do nothing. This can
28496 happen when toolkit scroll bars are used and a window is split.
28497 Reconfiguring the scroll bar will generate an expose for a newly
28498 created window. */
28499 if (w->current_matrix == NULL)
28500 return 0;
28501
28502 /* When we're currently updating the window, display and current
28503 matrix usually don't agree. Arrange for a thorough display
28504 later. */
28505 if (w == updated_window)
28506 {
28507 SET_FRAME_GARBAGED (f);
28508 return 0;
28509 }
28510
28511 /* Frame-relative pixel rectangle of W. */
28512 wr.x = WINDOW_LEFT_EDGE_X (w);
28513 wr.y = WINDOW_TOP_EDGE_Y (w);
28514 wr.width = WINDOW_TOTAL_WIDTH (w);
28515 wr.height = WINDOW_TOTAL_HEIGHT (w);
28516
28517 if (x_intersect_rectangles (fr, &wr, &r))
28518 {
28519 int yb = window_text_bottom_y (w);
28520 struct glyph_row *row;
28521 int cursor_cleared_p, phys_cursor_on_p;
28522 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28523
28524 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28525 r.x, r.y, r.width, r.height));
28526
28527 /* Convert to window coordinates. */
28528 r.x -= WINDOW_LEFT_EDGE_X (w);
28529 r.y -= WINDOW_TOP_EDGE_Y (w);
28530
28531 /* Turn off the cursor. */
28532 if (!w->pseudo_window_p
28533 && phys_cursor_in_rect_p (w, &r))
28534 {
28535 x_clear_cursor (w);
28536 cursor_cleared_p = 1;
28537 }
28538 else
28539 cursor_cleared_p = 0;
28540
28541 /* If the row containing the cursor extends face to end of line,
28542 then expose_area might overwrite the cursor outside the
28543 rectangle and thus notice_overwritten_cursor might clear
28544 w->phys_cursor_on_p. We remember the original value and
28545 check later if it is changed. */
28546 phys_cursor_on_p = w->phys_cursor_on_p;
28547
28548 /* Update lines intersecting rectangle R. */
28549 first_overlapping_row = last_overlapping_row = NULL;
28550 for (row = w->current_matrix->rows;
28551 row->enabled_p;
28552 ++row)
28553 {
28554 int y0 = row->y;
28555 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28556
28557 if ((y0 >= r.y && y0 < r.y + r.height)
28558 || (y1 > r.y && y1 < r.y + r.height)
28559 || (r.y >= y0 && r.y < y1)
28560 || (r.y + r.height > y0 && r.y + r.height < y1))
28561 {
28562 /* A header line may be overlapping, but there is no need
28563 to fix overlapping areas for them. KFS 2005-02-12 */
28564 if (row->overlapping_p && !row->mode_line_p)
28565 {
28566 if (first_overlapping_row == NULL)
28567 first_overlapping_row = row;
28568 last_overlapping_row = row;
28569 }
28570
28571 row->clip = fr;
28572 if (expose_line (w, row, &r))
28573 mouse_face_overwritten_p = 1;
28574 row->clip = NULL;
28575 }
28576 else if (row->overlapping_p)
28577 {
28578 /* We must redraw a row overlapping the exposed area. */
28579 if (y0 < r.y
28580 ? y0 + row->phys_height > r.y
28581 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28582 {
28583 if (first_overlapping_row == NULL)
28584 first_overlapping_row = row;
28585 last_overlapping_row = row;
28586 }
28587 }
28588
28589 if (y1 >= yb)
28590 break;
28591 }
28592
28593 /* Display the mode line if there is one. */
28594 if (WINDOW_WANTS_MODELINE_P (w)
28595 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28596 row->enabled_p)
28597 && row->y < r.y + r.height)
28598 {
28599 if (expose_line (w, row, &r))
28600 mouse_face_overwritten_p = 1;
28601 }
28602
28603 if (!w->pseudo_window_p)
28604 {
28605 /* Fix the display of overlapping rows. */
28606 if (first_overlapping_row)
28607 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28608 fr);
28609
28610 /* Draw border between windows. */
28611 x_draw_vertical_border (w);
28612
28613 /* Turn the cursor on again. */
28614 if (cursor_cleared_p
28615 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28616 update_window_cursor (w, 1);
28617 }
28618 }
28619
28620 return mouse_face_overwritten_p;
28621 }
28622
28623
28624
28625 /* Redraw (parts) of all windows in the window tree rooted at W that
28626 intersect R. R contains frame pixel coordinates. Value is
28627 non-zero if the exposure overwrites mouse-face. */
28628
28629 static int
28630 expose_window_tree (struct window *w, XRectangle *r)
28631 {
28632 struct frame *f = XFRAME (w->frame);
28633 int mouse_face_overwritten_p = 0;
28634
28635 while (w && !FRAME_GARBAGED_P (f))
28636 {
28637 if (!NILP (w->hchild))
28638 mouse_face_overwritten_p
28639 |= expose_window_tree (XWINDOW (w->hchild), r);
28640 else if (!NILP (w->vchild))
28641 mouse_face_overwritten_p
28642 |= expose_window_tree (XWINDOW (w->vchild), r);
28643 else
28644 mouse_face_overwritten_p |= expose_window (w, r);
28645
28646 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28647 }
28648
28649 return mouse_face_overwritten_p;
28650 }
28651
28652
28653 /* EXPORT:
28654 Redisplay an exposed area of frame F. X and Y are the upper-left
28655 corner of the exposed rectangle. W and H are width and height of
28656 the exposed area. All are pixel values. W or H zero means redraw
28657 the entire frame. */
28658
28659 void
28660 expose_frame (struct frame *f, int x, int y, int w, int h)
28661 {
28662 XRectangle r;
28663 int mouse_face_overwritten_p = 0;
28664
28665 TRACE ((stderr, "expose_frame "));
28666
28667 /* No need to redraw if frame will be redrawn soon. */
28668 if (FRAME_GARBAGED_P (f))
28669 {
28670 TRACE ((stderr, " garbaged\n"));
28671 return;
28672 }
28673
28674 /* If basic faces haven't been realized yet, there is no point in
28675 trying to redraw anything. This can happen when we get an expose
28676 event while Emacs is starting, e.g. by moving another window. */
28677 if (FRAME_FACE_CACHE (f) == NULL
28678 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28679 {
28680 TRACE ((stderr, " no faces\n"));
28681 return;
28682 }
28683
28684 if (w == 0 || h == 0)
28685 {
28686 r.x = r.y = 0;
28687 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28688 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28689 }
28690 else
28691 {
28692 r.x = x;
28693 r.y = y;
28694 r.width = w;
28695 r.height = h;
28696 }
28697
28698 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28699 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28700
28701 if (WINDOWP (f->tool_bar_window))
28702 mouse_face_overwritten_p
28703 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28704
28705 #ifdef HAVE_X_WINDOWS
28706 #ifndef MSDOS
28707 #ifndef USE_X_TOOLKIT
28708 if (WINDOWP (f->menu_bar_window))
28709 mouse_face_overwritten_p
28710 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28711 #endif /* not USE_X_TOOLKIT */
28712 #endif
28713 #endif
28714
28715 /* Some window managers support a focus-follows-mouse style with
28716 delayed raising of frames. Imagine a partially obscured frame,
28717 and moving the mouse into partially obscured mouse-face on that
28718 frame. The visible part of the mouse-face will be highlighted,
28719 then the WM raises the obscured frame. With at least one WM, KDE
28720 2.1, Emacs is not getting any event for the raising of the frame
28721 (even tried with SubstructureRedirectMask), only Expose events.
28722 These expose events will draw text normally, i.e. not
28723 highlighted. Which means we must redo the highlight here.
28724 Subsume it under ``we love X''. --gerd 2001-08-15 */
28725 /* Included in Windows version because Windows most likely does not
28726 do the right thing if any third party tool offers
28727 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28728 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28729 {
28730 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28731 if (f == hlinfo->mouse_face_mouse_frame)
28732 {
28733 int mouse_x = hlinfo->mouse_face_mouse_x;
28734 int mouse_y = hlinfo->mouse_face_mouse_y;
28735 clear_mouse_face (hlinfo);
28736 note_mouse_highlight (f, mouse_x, mouse_y);
28737 }
28738 }
28739 }
28740
28741
28742 /* EXPORT:
28743 Determine the intersection of two rectangles R1 and R2. Return
28744 the intersection in *RESULT. Value is non-zero if RESULT is not
28745 empty. */
28746
28747 int
28748 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28749 {
28750 XRectangle *left, *right;
28751 XRectangle *upper, *lower;
28752 int intersection_p = 0;
28753
28754 /* Rearrange so that R1 is the left-most rectangle. */
28755 if (r1->x < r2->x)
28756 left = r1, right = r2;
28757 else
28758 left = r2, right = r1;
28759
28760 /* X0 of the intersection is right.x0, if this is inside R1,
28761 otherwise there is no intersection. */
28762 if (right->x <= left->x + left->width)
28763 {
28764 result->x = right->x;
28765
28766 /* The right end of the intersection is the minimum of
28767 the right ends of left and right. */
28768 result->width = (min (left->x + left->width, right->x + right->width)
28769 - result->x);
28770
28771 /* Same game for Y. */
28772 if (r1->y < r2->y)
28773 upper = r1, lower = r2;
28774 else
28775 upper = r2, lower = r1;
28776
28777 /* The upper end of the intersection is lower.y0, if this is inside
28778 of upper. Otherwise, there is no intersection. */
28779 if (lower->y <= upper->y + upper->height)
28780 {
28781 result->y = lower->y;
28782
28783 /* The lower end of the intersection is the minimum of the lower
28784 ends of upper and lower. */
28785 result->height = (min (lower->y + lower->height,
28786 upper->y + upper->height)
28787 - result->y);
28788 intersection_p = 1;
28789 }
28790 }
28791
28792 return intersection_p;
28793 }
28794
28795 #endif /* HAVE_WINDOW_SYSTEM */
28796
28797 \f
28798 /***********************************************************************
28799 Initialization
28800 ***********************************************************************/
28801
28802 void
28803 syms_of_xdisp (void)
28804 {
28805 Vwith_echo_area_save_vector = Qnil;
28806 staticpro (&Vwith_echo_area_save_vector);
28807
28808 Vmessage_stack = Qnil;
28809 staticpro (&Vmessage_stack);
28810
28811 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28812 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28813
28814 message_dolog_marker1 = Fmake_marker ();
28815 staticpro (&message_dolog_marker1);
28816 message_dolog_marker2 = Fmake_marker ();
28817 staticpro (&message_dolog_marker2);
28818 message_dolog_marker3 = Fmake_marker ();
28819 staticpro (&message_dolog_marker3);
28820
28821 #ifdef GLYPH_DEBUG
28822 defsubr (&Sdump_frame_glyph_matrix);
28823 defsubr (&Sdump_glyph_matrix);
28824 defsubr (&Sdump_glyph_row);
28825 defsubr (&Sdump_tool_bar_row);
28826 defsubr (&Strace_redisplay);
28827 defsubr (&Strace_to_stderr);
28828 #endif
28829 #ifdef HAVE_WINDOW_SYSTEM
28830 defsubr (&Stool_bar_lines_needed);
28831 defsubr (&Slookup_image_map);
28832 #endif
28833 defsubr (&Sformat_mode_line);
28834 defsubr (&Sinvisible_p);
28835 defsubr (&Scurrent_bidi_paragraph_direction);
28836
28837 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28838 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28839 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28840 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28841 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28842 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28843 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28844 DEFSYM (Qeval, "eval");
28845 DEFSYM (QCdata, ":data");
28846 DEFSYM (Qdisplay, "display");
28847 DEFSYM (Qspace_width, "space-width");
28848 DEFSYM (Qraise, "raise");
28849 DEFSYM (Qslice, "slice");
28850 DEFSYM (Qspace, "space");
28851 DEFSYM (Qmargin, "margin");
28852 DEFSYM (Qpointer, "pointer");
28853 DEFSYM (Qleft_margin, "left-margin");
28854 DEFSYM (Qright_margin, "right-margin");
28855 DEFSYM (Qcenter, "center");
28856 DEFSYM (Qline_height, "line-height");
28857 DEFSYM (QCalign_to, ":align-to");
28858 DEFSYM (QCrelative_width, ":relative-width");
28859 DEFSYM (QCrelative_height, ":relative-height");
28860 DEFSYM (QCeval, ":eval");
28861 DEFSYM (QCpropertize, ":propertize");
28862 DEFSYM (QCfile, ":file");
28863 DEFSYM (Qfontified, "fontified");
28864 DEFSYM (Qfontification_functions, "fontification-functions");
28865 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28866 DEFSYM (Qescape_glyph, "escape-glyph");
28867 DEFSYM (Qnobreak_space, "nobreak-space");
28868 DEFSYM (Qimage, "image");
28869 DEFSYM (Qtext, "text");
28870 DEFSYM (Qboth, "both");
28871 DEFSYM (Qboth_horiz, "both-horiz");
28872 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28873 DEFSYM (QCmap, ":map");
28874 DEFSYM (QCpointer, ":pointer");
28875 DEFSYM (Qrect, "rect");
28876 DEFSYM (Qcircle, "circle");
28877 DEFSYM (Qpoly, "poly");
28878 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28879 DEFSYM (Qgrow_only, "grow-only");
28880 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28881 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28882 DEFSYM (Qposition, "position");
28883 DEFSYM (Qbuffer_position, "buffer-position");
28884 DEFSYM (Qobject, "object");
28885 DEFSYM (Qbar, "bar");
28886 DEFSYM (Qhbar, "hbar");
28887 DEFSYM (Qbox, "box");
28888 DEFSYM (Qhollow, "hollow");
28889 DEFSYM (Qhand, "hand");
28890 DEFSYM (Qarrow, "arrow");
28891 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28892
28893 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28894 Fcons (intern_c_string ("void-variable"), Qnil)),
28895 Qnil);
28896 staticpro (&list_of_error);
28897
28898 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28899 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28900 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28901 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28902
28903 echo_buffer[0] = echo_buffer[1] = Qnil;
28904 staticpro (&echo_buffer[0]);
28905 staticpro (&echo_buffer[1]);
28906
28907 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28908 staticpro (&echo_area_buffer[0]);
28909 staticpro (&echo_area_buffer[1]);
28910
28911 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28912 staticpro (&Vmessages_buffer_name);
28913
28914 mode_line_proptrans_alist = Qnil;
28915 staticpro (&mode_line_proptrans_alist);
28916 mode_line_string_list = Qnil;
28917 staticpro (&mode_line_string_list);
28918 mode_line_string_face = Qnil;
28919 staticpro (&mode_line_string_face);
28920 mode_line_string_face_prop = Qnil;
28921 staticpro (&mode_line_string_face_prop);
28922 Vmode_line_unwind_vector = Qnil;
28923 staticpro (&Vmode_line_unwind_vector);
28924
28925 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28926
28927 help_echo_string = Qnil;
28928 staticpro (&help_echo_string);
28929 help_echo_object = Qnil;
28930 staticpro (&help_echo_object);
28931 help_echo_window = Qnil;
28932 staticpro (&help_echo_window);
28933 previous_help_echo_string = Qnil;
28934 staticpro (&previous_help_echo_string);
28935 help_echo_pos = -1;
28936
28937 DEFSYM (Qright_to_left, "right-to-left");
28938 DEFSYM (Qleft_to_right, "left-to-right");
28939
28940 #ifdef HAVE_WINDOW_SYSTEM
28941 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28942 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28943 For example, if a block cursor is over a tab, it will be drawn as
28944 wide as that tab on the display. */);
28945 x_stretch_cursor_p = 0;
28946 #endif
28947
28948 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28949 doc: /* Non-nil means highlight trailing whitespace.
28950 The face used for trailing whitespace is `trailing-whitespace'. */);
28951 Vshow_trailing_whitespace = Qnil;
28952
28953 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28954 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28955 If the value is t, Emacs highlights non-ASCII chars which have the
28956 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28957 or `escape-glyph' face respectively.
28958
28959 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28960 U+2011 (non-breaking hyphen) are affected.
28961
28962 Any other non-nil value means to display these characters as a escape
28963 glyph followed by an ordinary space or hyphen.
28964
28965 A value of nil means no special handling of these characters. */);
28966 Vnobreak_char_display = Qt;
28967
28968 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28969 doc: /* The pointer shape to show in void text areas.
28970 A value of nil means to show the text pointer. Other options are `arrow',
28971 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28972 Vvoid_text_area_pointer = Qarrow;
28973
28974 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28975 doc: /* Non-nil means don't actually do any redisplay.
28976 This is used for internal purposes. */);
28977 Vinhibit_redisplay = Qnil;
28978
28979 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28980 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28981 Vglobal_mode_string = Qnil;
28982
28983 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28984 doc: /* Marker for where to display an arrow on top of the buffer text.
28985 This must be the beginning of a line in order to work.
28986 See also `overlay-arrow-string'. */);
28987 Voverlay_arrow_position = Qnil;
28988
28989 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28990 doc: /* String to display as an arrow in non-window frames.
28991 See also `overlay-arrow-position'. */);
28992 Voverlay_arrow_string = build_pure_c_string ("=>");
28993
28994 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28995 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28996 The symbols on this list are examined during redisplay to determine
28997 where to display overlay arrows. */);
28998 Voverlay_arrow_variable_list
28999 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29000
29001 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29002 doc: /* The number of lines to try scrolling a window by when point moves out.
29003 If that fails to bring point back on frame, point is centered instead.
29004 If this is zero, point is always centered after it moves off frame.
29005 If you want scrolling to always be a line at a time, you should set
29006 `scroll-conservatively' to a large value rather than set this to 1. */);
29007
29008 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29009 doc: /* Scroll up to this many lines, to bring point back on screen.
29010 If point moves off-screen, redisplay will scroll by up to
29011 `scroll-conservatively' lines in order to bring point just barely
29012 onto the screen again. If that cannot be done, then redisplay
29013 recenters point as usual.
29014
29015 If the value is greater than 100, redisplay will never recenter point,
29016 but will always scroll just enough text to bring point into view, even
29017 if you move far away.
29018
29019 A value of zero means always recenter point if it moves off screen. */);
29020 scroll_conservatively = 0;
29021
29022 DEFVAR_INT ("scroll-margin", scroll_margin,
29023 doc: /* Number of lines of margin at the top and bottom of a window.
29024 Recenter the window whenever point gets within this many lines
29025 of the top or bottom of the window. */);
29026 scroll_margin = 0;
29027
29028 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29029 doc: /* Pixels per inch value for non-window system displays.
29030 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29031 Vdisplay_pixels_per_inch = make_float (72.0);
29032
29033 #ifdef GLYPH_DEBUG
29034 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29035 #endif
29036
29037 DEFVAR_LISP ("truncate-partial-width-windows",
29038 Vtruncate_partial_width_windows,
29039 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29040 For an integer value, truncate lines in each window narrower than the
29041 full frame width, provided the window width is less than that integer;
29042 otherwise, respect the value of `truncate-lines'.
29043
29044 For any other non-nil value, truncate lines in all windows that do
29045 not span the full frame width.
29046
29047 A value of nil means to respect the value of `truncate-lines'.
29048
29049 If `word-wrap' is enabled, you might want to reduce this. */);
29050 Vtruncate_partial_width_windows = make_number (50);
29051
29052 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29053 doc: /* Maximum buffer size for which line number should be displayed.
29054 If the buffer is bigger than this, the line number does not appear
29055 in the mode line. A value of nil means no limit. */);
29056 Vline_number_display_limit = Qnil;
29057
29058 DEFVAR_INT ("line-number-display-limit-width",
29059 line_number_display_limit_width,
29060 doc: /* Maximum line width (in characters) for line number display.
29061 If the average length of the lines near point is bigger than this, then the
29062 line number may be omitted from the mode line. */);
29063 line_number_display_limit_width = 200;
29064
29065 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29066 doc: /* Non-nil means highlight region even in nonselected windows. */);
29067 highlight_nonselected_windows = 0;
29068
29069 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29070 doc: /* Non-nil if more than one frame is visible on this display.
29071 Minibuffer-only frames don't count, but iconified frames do.
29072 This variable is not guaranteed to be accurate except while processing
29073 `frame-title-format' and `icon-title-format'. */);
29074
29075 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29076 doc: /* Template for displaying the title bar of visible frames.
29077 \(Assuming the window manager supports this feature.)
29078
29079 This variable has the same structure as `mode-line-format', except that
29080 the %c and %l constructs are ignored. It is used only on frames for
29081 which no explicit name has been set \(see `modify-frame-parameters'). */);
29082
29083 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29084 doc: /* Template for displaying the title bar of an iconified frame.
29085 \(Assuming the window manager supports this feature.)
29086 This variable has the same structure as `mode-line-format' (which see),
29087 and is used only on frames for which no explicit name has been set
29088 \(see `modify-frame-parameters'). */);
29089 Vicon_title_format
29090 = Vframe_title_format
29091 = listn (CONSTYPE_PURE, 3,
29092 intern_c_string ("multiple-frames"),
29093 build_pure_c_string ("%b"),
29094 listn (CONSTYPE_PURE, 4,
29095 empty_unibyte_string,
29096 intern_c_string ("invocation-name"),
29097 build_pure_c_string ("@"),
29098 intern_c_string ("system-name")));
29099
29100 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29101 doc: /* Maximum number of lines to keep in the message log buffer.
29102 If nil, disable message logging. If t, log messages but don't truncate
29103 the buffer when it becomes large. */);
29104 Vmessage_log_max = make_number (1000);
29105
29106 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29107 doc: /* Functions called before redisplay, if window sizes have changed.
29108 The value should be a list of functions that take one argument.
29109 Just before redisplay, for each frame, if any of its windows have changed
29110 size since the last redisplay, or have been split or deleted,
29111 all the functions in the list are called, with the frame as argument. */);
29112 Vwindow_size_change_functions = Qnil;
29113
29114 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29115 doc: /* List of functions to call before redisplaying a window with scrolling.
29116 Each function is called with two arguments, the window and its new
29117 display-start position. Note that these functions are also called by
29118 `set-window-buffer'. Also note that the value of `window-end' is not
29119 valid when these functions are called.
29120
29121 Warning: Do not use this feature to alter the way the window
29122 is scrolled. It is not designed for that, and such use probably won't
29123 work. */);
29124 Vwindow_scroll_functions = Qnil;
29125
29126 DEFVAR_LISP ("window-text-change-functions",
29127 Vwindow_text_change_functions,
29128 doc: /* Functions to call in redisplay when text in the window might change. */);
29129 Vwindow_text_change_functions = Qnil;
29130
29131 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29132 doc: /* Functions called when redisplay of a window reaches the end trigger.
29133 Each function is called with two arguments, the window and the end trigger value.
29134 See `set-window-redisplay-end-trigger'. */);
29135 Vredisplay_end_trigger_functions = Qnil;
29136
29137 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29138 doc: /* Non-nil means autoselect window with mouse pointer.
29139 If nil, do not autoselect windows.
29140 A positive number means delay autoselection by that many seconds: a
29141 window is autoselected only after the mouse has remained in that
29142 window for the duration of the delay.
29143 A negative number has a similar effect, but causes windows to be
29144 autoselected only after the mouse has stopped moving. \(Because of
29145 the way Emacs compares mouse events, you will occasionally wait twice
29146 that time before the window gets selected.\)
29147 Any other value means to autoselect window instantaneously when the
29148 mouse pointer enters it.
29149
29150 Autoselection selects the minibuffer only if it is active, and never
29151 unselects the minibuffer if it is active.
29152
29153 When customizing this variable make sure that the actual value of
29154 `focus-follows-mouse' matches the behavior of your window manager. */);
29155 Vmouse_autoselect_window = Qnil;
29156
29157 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29158 doc: /* Non-nil means automatically resize tool-bars.
29159 This dynamically changes the tool-bar's height to the minimum height
29160 that is needed to make all tool-bar items visible.
29161 If value is `grow-only', the tool-bar's height is only increased
29162 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29163 Vauto_resize_tool_bars = Qt;
29164
29165 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29166 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29167 auto_raise_tool_bar_buttons_p = 1;
29168
29169 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29170 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29171 make_cursor_line_fully_visible_p = 1;
29172
29173 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29174 doc: /* Border below tool-bar in pixels.
29175 If an integer, use it as the height of the border.
29176 If it is one of `internal-border-width' or `border-width', use the
29177 value of the corresponding frame parameter.
29178 Otherwise, no border is added below the tool-bar. */);
29179 Vtool_bar_border = Qinternal_border_width;
29180
29181 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29182 doc: /* Margin around tool-bar buttons in pixels.
29183 If an integer, use that for both horizontal and vertical margins.
29184 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29185 HORZ specifying the horizontal margin, and VERT specifying the
29186 vertical margin. */);
29187 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29188
29189 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29190 doc: /* Relief thickness of tool-bar buttons. */);
29191 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29192
29193 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29194 doc: /* Tool bar style to use.
29195 It can be one of
29196 image - show images only
29197 text - show text only
29198 both - show both, text below image
29199 both-horiz - show text to the right of the image
29200 text-image-horiz - show text to the left of the image
29201 any other - use system default or image if no system default.
29202
29203 This variable only affects the GTK+ toolkit version of Emacs. */);
29204 Vtool_bar_style = Qnil;
29205
29206 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29207 doc: /* Maximum number of characters a label can have to be shown.
29208 The tool bar style must also show labels for this to have any effect, see
29209 `tool-bar-style'. */);
29210 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29211
29212 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29213 doc: /* List of functions to call to fontify regions of text.
29214 Each function is called with one argument POS. Functions must
29215 fontify a region starting at POS in the current buffer, and give
29216 fontified regions the property `fontified'. */);
29217 Vfontification_functions = Qnil;
29218 Fmake_variable_buffer_local (Qfontification_functions);
29219
29220 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29221 unibyte_display_via_language_environment,
29222 doc: /* Non-nil means display unibyte text according to language environment.
29223 Specifically, this means that raw bytes in the range 160-255 decimal
29224 are displayed by converting them to the equivalent multibyte characters
29225 according to the current language environment. As a result, they are
29226 displayed according to the current fontset.
29227
29228 Note that this variable affects only how these bytes are displayed,
29229 but does not change the fact they are interpreted as raw bytes. */);
29230 unibyte_display_via_language_environment = 0;
29231
29232 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29233 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29234 If a float, it specifies a fraction of the mini-window frame's height.
29235 If an integer, it specifies a number of lines. */);
29236 Vmax_mini_window_height = make_float (0.25);
29237
29238 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29239 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29240 A value of nil means don't automatically resize mini-windows.
29241 A value of t means resize them to fit the text displayed in them.
29242 A value of `grow-only', the default, means let mini-windows grow only;
29243 they return to their normal size when the minibuffer is closed, or the
29244 echo area becomes empty. */);
29245 Vresize_mini_windows = Qgrow_only;
29246
29247 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29248 doc: /* Alist specifying how to blink the cursor off.
29249 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29250 `cursor-type' frame-parameter or variable equals ON-STATE,
29251 comparing using `equal', Emacs uses OFF-STATE to specify
29252 how to blink it off. ON-STATE and OFF-STATE are values for
29253 the `cursor-type' frame parameter.
29254
29255 If a frame's ON-STATE has no entry in this list,
29256 the frame's other specifications determine how to blink the cursor off. */);
29257 Vblink_cursor_alist = Qnil;
29258
29259 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29260 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29261 If non-nil, windows are automatically scrolled horizontally to make
29262 point visible. */);
29263 automatic_hscrolling_p = 1;
29264 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29265
29266 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29267 doc: /* How many columns away from the window edge point is allowed to get
29268 before automatic hscrolling will horizontally scroll the window. */);
29269 hscroll_margin = 5;
29270
29271 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29272 doc: /* How many columns to scroll the window when point gets too close to the edge.
29273 When point is less than `hscroll-margin' columns from the window
29274 edge, automatic hscrolling will scroll the window by the amount of columns
29275 determined by this variable. If its value is a positive integer, scroll that
29276 many columns. If it's a positive floating-point number, it specifies the
29277 fraction of the window's width to scroll. If it's nil or zero, point will be
29278 centered horizontally after the scroll. Any other value, including negative
29279 numbers, are treated as if the value were zero.
29280
29281 Automatic hscrolling always moves point outside the scroll margin, so if
29282 point was more than scroll step columns inside the margin, the window will
29283 scroll more than the value given by the scroll step.
29284
29285 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29286 and `scroll-right' overrides this variable's effect. */);
29287 Vhscroll_step = make_number (0);
29288
29289 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29290 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29291 Bind this around calls to `message' to let it take effect. */);
29292 message_truncate_lines = 0;
29293
29294 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29295 doc: /* Normal hook run to update the menu bar definitions.
29296 Redisplay runs this hook before it redisplays the menu bar.
29297 This is used to update submenus such as Buffers,
29298 whose contents depend on various data. */);
29299 Vmenu_bar_update_hook = Qnil;
29300
29301 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29302 doc: /* Frame for which we are updating a menu.
29303 The enable predicate for a menu binding should check this variable. */);
29304 Vmenu_updating_frame = Qnil;
29305
29306 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29307 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29308 inhibit_menubar_update = 0;
29309
29310 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29311 doc: /* Prefix prepended to all continuation lines at display time.
29312 The value may be a string, an image, or a stretch-glyph; it is
29313 interpreted in the same way as the value of a `display' text property.
29314
29315 This variable is overridden by any `wrap-prefix' text or overlay
29316 property.
29317
29318 To add a prefix to non-continuation lines, use `line-prefix'. */);
29319 Vwrap_prefix = Qnil;
29320 DEFSYM (Qwrap_prefix, "wrap-prefix");
29321 Fmake_variable_buffer_local (Qwrap_prefix);
29322
29323 DEFVAR_LISP ("line-prefix", Vline_prefix,
29324 doc: /* Prefix prepended to all non-continuation lines at display time.
29325 The value may be a string, an image, or a stretch-glyph; it is
29326 interpreted in the same way as the value of a `display' text property.
29327
29328 This variable is overridden by any `line-prefix' text or overlay
29329 property.
29330
29331 To add a prefix to continuation lines, use `wrap-prefix'. */);
29332 Vline_prefix = Qnil;
29333 DEFSYM (Qline_prefix, "line-prefix");
29334 Fmake_variable_buffer_local (Qline_prefix);
29335
29336 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29337 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29338 inhibit_eval_during_redisplay = 0;
29339
29340 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29341 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29342 inhibit_free_realized_faces = 0;
29343
29344 #ifdef GLYPH_DEBUG
29345 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29346 doc: /* Inhibit try_window_id display optimization. */);
29347 inhibit_try_window_id = 0;
29348
29349 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29350 doc: /* Inhibit try_window_reusing display optimization. */);
29351 inhibit_try_window_reusing = 0;
29352
29353 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29354 doc: /* Inhibit try_cursor_movement display optimization. */);
29355 inhibit_try_cursor_movement = 0;
29356 #endif /* GLYPH_DEBUG */
29357
29358 DEFVAR_INT ("overline-margin", overline_margin,
29359 doc: /* Space between overline and text, in pixels.
29360 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29361 margin to the character height. */);
29362 overline_margin = 2;
29363
29364 DEFVAR_INT ("underline-minimum-offset",
29365 underline_minimum_offset,
29366 doc: /* Minimum distance between baseline and underline.
29367 This can improve legibility of underlined text at small font sizes,
29368 particularly when using variable `x-use-underline-position-properties'
29369 with fonts that specify an UNDERLINE_POSITION relatively close to the
29370 baseline. The default value is 1. */);
29371 underline_minimum_offset = 1;
29372
29373 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29374 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29375 This feature only works when on a window system that can change
29376 cursor shapes. */);
29377 display_hourglass_p = 1;
29378
29379 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29380 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29381 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29382
29383 hourglass_atimer = NULL;
29384 hourglass_shown_p = 0;
29385
29386 DEFSYM (Qglyphless_char, "glyphless-char");
29387 DEFSYM (Qhex_code, "hex-code");
29388 DEFSYM (Qempty_box, "empty-box");
29389 DEFSYM (Qthin_space, "thin-space");
29390 DEFSYM (Qzero_width, "zero-width");
29391
29392 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29393 /* Intern this now in case it isn't already done.
29394 Setting this variable twice is harmless.
29395 But don't staticpro it here--that is done in alloc.c. */
29396 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29397 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29398
29399 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29400 doc: /* Char-table defining glyphless characters.
29401 Each element, if non-nil, should be one of the following:
29402 an ASCII acronym string: display this string in a box
29403 `hex-code': display the hexadecimal code of a character in a box
29404 `empty-box': display as an empty box
29405 `thin-space': display as 1-pixel width space
29406 `zero-width': don't display
29407 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29408 display method for graphical terminals and text terminals respectively.
29409 GRAPHICAL and TEXT should each have one of the values listed above.
29410
29411 The char-table has one extra slot to control the display of a character for
29412 which no font is found. This slot only takes effect on graphical terminals.
29413 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29414 `thin-space'. The default is `empty-box'. */);
29415 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29416 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29417 Qempty_box);
29418
29419 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29420 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29421 Vdebug_on_message = Qnil;
29422 }
29423
29424
29425 /* Initialize this module when Emacs starts. */
29426
29427 void
29428 init_xdisp (void)
29429 {
29430 current_header_line_height = current_mode_line_height = -1;
29431
29432 CHARPOS (this_line_start_pos) = 0;
29433
29434 if (!noninteractive)
29435 {
29436 struct window *m = XWINDOW (minibuf_window);
29437 Lisp_Object frame = m->frame;
29438 struct frame *f = XFRAME (frame);
29439 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29440 struct window *r = XWINDOW (root);
29441 int i;
29442
29443 echo_area_window = minibuf_window;
29444
29445 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29446 wset_total_lines
29447 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29448 wset_total_cols (r, make_number (FRAME_COLS (f)));
29449 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29450 wset_total_lines (m, make_number (1));
29451 wset_total_cols (m, make_number (FRAME_COLS (f)));
29452
29453 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29454 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29455 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29456
29457 /* The default ellipsis glyphs `...'. */
29458 for (i = 0; i < 3; ++i)
29459 default_invis_vector[i] = make_number ('.');
29460 }
29461
29462 {
29463 /* Allocate the buffer for frame titles.
29464 Also used for `format-mode-line'. */
29465 int size = 100;
29466 mode_line_noprop_buf = xmalloc (size);
29467 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29468 mode_line_noprop_ptr = mode_line_noprop_buf;
29469 mode_line_target = MODE_LINE_DISPLAY;
29470 }
29471
29472 help_echo_showing_p = 0;
29473 }
29474
29475 /* Platform-independent portion of hourglass implementation. */
29476
29477 /* Cancel a currently active hourglass timer, and start a new one. */
29478 void
29479 start_hourglass (void)
29480 {
29481 #if defined (HAVE_WINDOW_SYSTEM)
29482 EMACS_TIME delay;
29483
29484 cancel_hourglass ();
29485
29486 if (INTEGERP (Vhourglass_delay)
29487 && XINT (Vhourglass_delay) > 0)
29488 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29489 TYPE_MAXIMUM (time_t)),
29490 0);
29491 else if (FLOATP (Vhourglass_delay)
29492 && XFLOAT_DATA (Vhourglass_delay) > 0)
29493 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29494 else
29495 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29496
29497 #ifdef HAVE_NTGUI
29498 {
29499 extern void w32_note_current_window (void);
29500 w32_note_current_window ();
29501 }
29502 #endif /* HAVE_NTGUI */
29503
29504 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29505 show_hourglass, NULL);
29506 #endif
29507 }
29508
29509
29510 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29511 shown. */
29512 void
29513 cancel_hourglass (void)
29514 {
29515 #if defined (HAVE_WINDOW_SYSTEM)
29516 if (hourglass_atimer)
29517 {
29518 cancel_atimer (hourglass_atimer);
29519 hourglass_atimer = NULL;
29520 }
29521
29522 if (hourglass_shown_p)
29523 hide_hourglass ();
29524 #endif
29525 }