Fix bugs #13623 and 13626 caused by changes in 2013-02-01T07:23:18Z!dmantipov@yandex.ru.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #include "font.h"
317
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
321
322 #define INFINITY 10000000
323
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
336
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
343
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
346
347 static Lisp_Object Qfontification_functions;
348
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
352
353 /* Non-nil means don't actually do any redisplay. */
354
355 Lisp_Object Qinhibit_redisplay;
356
357 /* Names of text properties relevant for redisplay. */
358
359 Lisp_Object Qdisplay;
360
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
369
370 #ifdef HAVE_WINDOW_SYSTEM
371
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
374
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x \
382 && (IT)->line_wrap != WORD_WRAP)
383
384 #else /* !HAVE_WINDOW_SYSTEM */
385 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
386 #endif /* HAVE_WINDOW_SYSTEM */
387
388 /* Test if the display element loaded in IT, or the underlying buffer
389 or string character, is a space or a TAB character. This is used
390 to determine where word wrapping can occur. */
391
392 #define IT_DISPLAYING_WHITESPACE(it) \
393 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
394 || ((STRINGP (it->string) \
395 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
396 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
397 || (it->s \
398 && (it->s[IT_BYTEPOS (*it)] == ' ' \
399 || it->s[IT_BYTEPOS (*it)] == '\t')) \
400 || (IT_BYTEPOS (*it) < ZV_BYTE \
401 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
402 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
403
404 /* Name of the face used to highlight trailing whitespace. */
405
406 static Lisp_Object Qtrailing_whitespace;
407
408 /* Name and number of the face used to highlight escape glyphs. */
409
410 static Lisp_Object Qescape_glyph;
411
412 /* Name and number of the face used to highlight non-breaking spaces. */
413
414 static Lisp_Object Qnobreak_space;
415
416 /* The symbol `image' which is the car of the lists used to represent
417 images in Lisp. Also a tool bar style. */
418
419 Lisp_Object Qimage;
420
421 /* The image map types. */
422 Lisp_Object QCmap;
423 static Lisp_Object QCpointer;
424 static Lisp_Object Qrect, Qcircle, Qpoly;
425
426 /* Tool bar styles */
427 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
428
429 /* Non-zero means print newline to stdout before next mini-buffer
430 message. */
431
432 int noninteractive_need_newline;
433
434 /* Non-zero means print newline to message log before next message. */
435
436 static int message_log_need_newline;
437
438 /* Three markers that message_dolog uses.
439 It could allocate them itself, but that causes trouble
440 in handling memory-full errors. */
441 static Lisp_Object message_dolog_marker1;
442 static Lisp_Object message_dolog_marker2;
443 static Lisp_Object message_dolog_marker3;
444 \f
445 /* The buffer position of the first character appearing entirely or
446 partially on the line of the selected window which contains the
447 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
448 redisplay optimization in redisplay_internal. */
449
450 static struct text_pos this_line_start_pos;
451
452 /* Number of characters past the end of the line above, including the
453 terminating newline. */
454
455 static struct text_pos this_line_end_pos;
456
457 /* The vertical positions and the height of this line. */
458
459 static int this_line_vpos;
460 static int this_line_y;
461 static int this_line_pixel_height;
462
463 /* X position at which this display line starts. Usually zero;
464 negative if first character is partially visible. */
465
466 static int this_line_start_x;
467
468 /* The smallest character position seen by move_it_* functions as they
469 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
470 hscrolled lines, see display_line. */
471
472 static struct text_pos this_line_min_pos;
473
474 /* Buffer that this_line_.* variables are referring to. */
475
476 static struct buffer *this_line_buffer;
477
478
479 /* Values of those variables at last redisplay are stored as
480 properties on `overlay-arrow-position' symbol. However, if
481 Voverlay_arrow_position is a marker, last-arrow-position is its
482 numerical position. */
483
484 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
485
486 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
487 properties on a symbol in overlay-arrow-variable-list. */
488
489 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
490
491 Lisp_Object Qmenu_bar_update_hook;
492
493 /* Nonzero if an overlay arrow has been displayed in this window. */
494
495 static int overlay_arrow_seen;
496
497 /* Vector containing glyphs for an ellipsis `...'. */
498
499 static Lisp_Object default_invis_vector[3];
500
501 /* This is the window where the echo area message was displayed. It
502 is always a mini-buffer window, but it may not be the same window
503 currently active as a mini-buffer. */
504
505 Lisp_Object echo_area_window;
506
507 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
508 pushes the current message and the value of
509 message_enable_multibyte on the stack, the function restore_message
510 pops the stack and displays MESSAGE again. */
511
512 static Lisp_Object Vmessage_stack;
513
514 /* Nonzero means multibyte characters were enabled when the echo area
515 message was specified. */
516
517 static int message_enable_multibyte;
518
519 /* Nonzero if we should redraw the mode lines on the next redisplay. */
520
521 int update_mode_lines;
522
523 /* Nonzero if window sizes or contents have changed since last
524 redisplay that finished. */
525
526 int windows_or_buffers_changed;
527
528 /* Nonzero means a frame's cursor type has been changed. */
529
530 int cursor_type_changed;
531
532 /* Nonzero after display_mode_line if %l was used and it displayed a
533 line number. */
534
535 static int line_number_displayed;
536
537 /* The name of the *Messages* buffer, a string. */
538
539 static Lisp_Object Vmessages_buffer_name;
540
541 /* Current, index 0, and last displayed echo area message. Either
542 buffers from echo_buffers, or nil to indicate no message. */
543
544 Lisp_Object echo_area_buffer[2];
545
546 /* The buffers referenced from echo_area_buffer. */
547
548 static Lisp_Object echo_buffer[2];
549
550 /* A vector saved used in with_area_buffer to reduce consing. */
551
552 static Lisp_Object Vwith_echo_area_save_vector;
553
554 /* Non-zero means display_echo_area should display the last echo area
555 message again. Set by redisplay_preserve_echo_area. */
556
557 static int display_last_displayed_message_p;
558
559 /* Nonzero if echo area is being used by print; zero if being used by
560 message. */
561
562 static int message_buf_print;
563
564 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
565
566 static Lisp_Object Qinhibit_menubar_update;
567 static Lisp_Object Qmessage_truncate_lines;
568
569 /* Set to 1 in clear_message to make redisplay_internal aware
570 of an emptied echo area. */
571
572 static int message_cleared_p;
573
574 /* A scratch glyph row with contents used for generating truncation
575 glyphs. Also used in direct_output_for_insert. */
576
577 #define MAX_SCRATCH_GLYPHS 100
578 static struct glyph_row scratch_glyph_row;
579 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
580
581 /* Ascent and height of the last line processed by move_it_to. */
582
583 static int last_max_ascent, last_height;
584
585 /* Non-zero if there's a help-echo in the echo area. */
586
587 int help_echo_showing_p;
588
589 /* If >= 0, computed, exact values of mode-line and header-line height
590 to use in the macros CURRENT_MODE_LINE_HEIGHT and
591 CURRENT_HEADER_LINE_HEIGHT. */
592
593 int current_mode_line_height, current_header_line_height;
594
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
600
601 #define TEXT_PROP_DISTANCE_LIMIT 100
602
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
617
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
625
626 #ifdef GLYPH_DEBUG
627
628 /* Non-zero means print traces of redisplay if compiled with
629 GLYPH_DEBUG defined. */
630
631 int trace_redisplay_p;
632
633 #endif /* GLYPH_DEBUG */
634
635 #ifdef DEBUG_TRACE_MOVE
636 /* Non-zero means trace with TRACE_MOVE to stderr. */
637 int trace_move;
638
639 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
640 #else
641 #define TRACE_MOVE(x) (void) 0
642 #endif
643
644 static Lisp_Object Qauto_hscroll_mode;
645
646 /* Buffer being redisplayed -- for redisplay_window_error. */
647
648 static struct buffer *displayed_buffer;
649
650 /* Value returned from text property handlers (see below). */
651
652 enum prop_handled
653 {
654 HANDLED_NORMALLY,
655 HANDLED_RECOMPUTE_PROPS,
656 HANDLED_OVERLAY_STRING_CONSUMED,
657 HANDLED_RETURN
658 };
659
660 /* A description of text properties that redisplay is interested
661 in. */
662
663 struct props
664 {
665 /* The name of the property. */
666 Lisp_Object *name;
667
668 /* A unique index for the property. */
669 enum prop_idx idx;
670
671 /* A handler function called to set up iterator IT from the property
672 at IT's current position. Value is used to steer handle_stop. */
673 enum prop_handled (*handler) (struct it *it);
674 };
675
676 static enum prop_handled handle_face_prop (struct it *);
677 static enum prop_handled handle_invisible_prop (struct it *);
678 static enum prop_handled handle_display_prop (struct it *);
679 static enum prop_handled handle_composition_prop (struct it *);
680 static enum prop_handled handle_overlay_change (struct it *);
681 static enum prop_handled handle_fontified_prop (struct it *);
682
683 /* Properties handled by iterators. */
684
685 static struct props it_props[] =
686 {
687 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
688 /* Handle `face' before `display' because some sub-properties of
689 `display' need to know the face. */
690 {&Qface, FACE_PROP_IDX, handle_face_prop},
691 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
692 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
693 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
694 {NULL, 0, NULL}
695 };
696
697 /* Value is the position described by X. If X is a marker, value is
698 the marker_position of X. Otherwise, value is X. */
699
700 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
701
702 /* Enumeration returned by some move_it_.* functions internally. */
703
704 enum move_it_result
705 {
706 /* Not used. Undefined value. */
707 MOVE_UNDEFINED,
708
709 /* Move ended at the requested buffer position or ZV. */
710 MOVE_POS_MATCH_OR_ZV,
711
712 /* Move ended at the requested X pixel position. */
713 MOVE_X_REACHED,
714
715 /* Move within a line ended at the end of a line that must be
716 continued. */
717 MOVE_LINE_CONTINUED,
718
719 /* Move within a line ended at the end of a line that would
720 be displayed truncated. */
721 MOVE_LINE_TRUNCATED,
722
723 /* Move within a line ended at a line end. */
724 MOVE_NEWLINE_OR_CR
725 };
726
727 /* This counter is used to clear the face cache every once in a while
728 in redisplay_internal. It is incremented for each redisplay.
729 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
730 cleared. */
731
732 #define CLEAR_FACE_CACHE_COUNT 500
733 static int clear_face_cache_count;
734
735 /* Similarly for the image cache. */
736
737 #ifdef HAVE_WINDOW_SYSTEM
738 #define CLEAR_IMAGE_CACHE_COUNT 101
739 static int clear_image_cache_count;
740
741 /* Null glyph slice */
742 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
743 #endif
744
745 /* True while redisplay_internal is in progress. */
746
747 bool redisplaying_p;
748
749 static Lisp_Object Qinhibit_free_realized_faces;
750 static Lisp_Object Qmode_line_default_help_echo;
751
752 /* If a string, XTread_socket generates an event to display that string.
753 (The display is done in read_char.) */
754
755 Lisp_Object help_echo_string;
756 Lisp_Object help_echo_window;
757 Lisp_Object help_echo_object;
758 ptrdiff_t help_echo_pos;
759
760 /* Temporary variable for XTread_socket. */
761
762 Lisp_Object previous_help_echo_string;
763
764 /* Platform-independent portion of hourglass implementation. */
765
766 /* Non-zero means an hourglass cursor is currently shown. */
767 int hourglass_shown_p;
768
769 /* If non-null, an asynchronous timer that, when it expires, displays
770 an hourglass cursor on all frames. */
771 struct atimer *hourglass_atimer;
772
773 /* Name of the face used to display glyphless characters. */
774 Lisp_Object Qglyphless_char;
775
776 /* Symbol for the purpose of Vglyphless_char_display. */
777 static Lisp_Object Qglyphless_char_display;
778
779 /* Method symbols for Vglyphless_char_display. */
780 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
781
782 /* Default pixel width of `thin-space' display method. */
783 #define THIN_SPACE_WIDTH 1
784
785 /* Default number of seconds to wait before displaying an hourglass
786 cursor. */
787 #define DEFAULT_HOURGLASS_DELAY 1
788
789 \f
790 /* Function prototypes. */
791
792 static void setup_for_ellipsis (struct it *, int);
793 static void set_iterator_to_next (struct it *, int);
794 static void mark_window_display_accurate_1 (struct window *, int);
795 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
796 static int display_prop_string_p (Lisp_Object, Lisp_Object);
797 static int cursor_row_p (struct glyph_row *);
798 static int redisplay_mode_lines (Lisp_Object, int);
799 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
800
801 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
802
803 static void handle_line_prefix (struct it *);
804
805 static void pint2str (char *, int, ptrdiff_t);
806 static void pint2hrstr (char *, int, ptrdiff_t);
807 static struct text_pos run_window_scroll_functions (Lisp_Object,
808 struct text_pos);
809 static void reconsider_clip_changes (struct window *, struct buffer *);
810 static int text_outside_line_unchanged_p (struct window *,
811 ptrdiff_t, ptrdiff_t);
812 static void store_mode_line_noprop_char (char);
813 static int store_mode_line_noprop (const char *, int, int);
814 static void handle_stop (struct it *);
815 static void handle_stop_backwards (struct it *, ptrdiff_t);
816 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
817 static void ensure_echo_area_buffers (void);
818 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
819 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
820 static int with_echo_area_buffer (struct window *, int,
821 int (*) (ptrdiff_t, Lisp_Object),
822 ptrdiff_t, Lisp_Object);
823 static void clear_garbaged_frames (void);
824 static int current_message_1 (ptrdiff_t, Lisp_Object);
825 static void pop_message (void);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static Lisp_Object unwind_redisplay (Lisp_Object);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 void reseat_at_previous_visible_line_start (struct it *);
884 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 void move_it_vertically_backward (struct it *, int);
903 static void get_visually_first_element (struct it *);
904 static void init_to_row_start (struct it *, struct window *,
905 struct glyph_row *);
906 static int init_to_row_end (struct it *, struct window *,
907 struct glyph_row *);
908 static void back_to_previous_line_start (struct it *);
909 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
910 static struct text_pos string_pos_nchars_ahead (struct text_pos,
911 Lisp_Object, ptrdiff_t);
912 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
913 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
914 static ptrdiff_t number_of_chars (const char *, int);
915 static void compute_stop_pos (struct it *);
916 static void compute_string_pos (struct text_pos *, struct text_pos,
917 Lisp_Object);
918 static int face_before_or_after_it_pos (struct it *, int);
919 static ptrdiff_t next_overlay_change (ptrdiff_t);
920 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
921 Lisp_Object, struct text_pos *, ptrdiff_t, int);
922 static int handle_single_display_spec (struct it *, Lisp_Object,
923 Lisp_Object, Lisp_Object,
924 struct text_pos *, ptrdiff_t, int, int);
925 static int underlying_face_id (struct it *);
926 static int in_ellipses_for_invisible_text_p (struct display_pos *,
927 struct window *);
928
929 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
930 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
931
932 #ifdef HAVE_WINDOW_SYSTEM
933
934 static void x_consider_frame_title (Lisp_Object);
935 static int tool_bar_lines_needed (struct frame *, int *);
936 static void update_tool_bar (struct frame *, int);
937 static void build_desired_tool_bar_string (struct frame *f);
938 static int redisplay_tool_bar (struct frame *);
939 static void display_tool_bar_line (struct it *, int);
940 static void notice_overwritten_cursor (struct window *,
941 enum glyph_row_area,
942 int, int, int, int);
943 static void append_stretch_glyph (struct it *, Lisp_Object,
944 int, int, int);
945
946
947 #endif /* HAVE_WINDOW_SYSTEM */
948
949 static void produce_special_glyphs (struct it *, enum display_element_type);
950 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
951 static int coords_in_mouse_face_p (struct window *, int, int);
952
953
954 \f
955 /***********************************************************************
956 Window display dimensions
957 ***********************************************************************/
958
959 /* Return the bottom boundary y-position for text lines in window W.
960 This is the first y position at which a line cannot start.
961 It is relative to the top of the window.
962
963 This is the height of W minus the height of a mode line, if any. */
964
965 int
966 window_text_bottom_y (struct window *w)
967 {
968 int height = WINDOW_TOTAL_HEIGHT (w);
969
970 if (WINDOW_WANTS_MODELINE_P (w))
971 height -= CURRENT_MODE_LINE_HEIGHT (w);
972 return height;
973 }
974
975 /* Return the pixel width of display area AREA of window W. AREA < 0
976 means return the total width of W, not including fringes to
977 the left and right of the window. */
978
979 int
980 window_box_width (struct window *w, int area)
981 {
982 int cols = XFASTINT (w->total_cols);
983 int pixels = 0;
984
985 if (!w->pseudo_window_p)
986 {
987 cols -= WINDOW_SCROLL_BAR_COLS (w);
988
989 if (area == TEXT_AREA)
990 {
991 if (INTEGERP (w->left_margin_cols))
992 cols -= XFASTINT (w->left_margin_cols);
993 if (INTEGERP (w->right_margin_cols))
994 cols -= XFASTINT (w->right_margin_cols);
995 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
996 }
997 else if (area == LEFT_MARGIN_AREA)
998 {
999 cols = (INTEGERP (w->left_margin_cols)
1000 ? XFASTINT (w->left_margin_cols) : 0);
1001 pixels = 0;
1002 }
1003 else if (area == RIGHT_MARGIN_AREA)
1004 {
1005 cols = (INTEGERP (w->right_margin_cols)
1006 ? XFASTINT (w->right_margin_cols) : 0);
1007 pixels = 0;
1008 }
1009 }
1010
1011 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1012 }
1013
1014
1015 /* Return the pixel height of the display area of window W, not
1016 including mode lines of W, if any. */
1017
1018 int
1019 window_box_height (struct window *w)
1020 {
1021 struct frame *f = XFRAME (w->frame);
1022 int height = WINDOW_TOTAL_HEIGHT (w);
1023
1024 eassert (height >= 0);
1025
1026 /* Note: the code below that determines the mode-line/header-line
1027 height is essentially the same as that contained in the macro
1028 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1029 the appropriate glyph row has its `mode_line_p' flag set,
1030 and if it doesn't, uses estimate_mode_line_height instead. */
1031
1032 if (WINDOW_WANTS_MODELINE_P (w))
1033 {
1034 struct glyph_row *ml_row
1035 = (w->current_matrix && w->current_matrix->rows
1036 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1037 : 0);
1038 if (ml_row && ml_row->mode_line_p)
1039 height -= ml_row->height;
1040 else
1041 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1042 }
1043
1044 if (WINDOW_WANTS_HEADER_LINE_P (w))
1045 {
1046 struct glyph_row *hl_row
1047 = (w->current_matrix && w->current_matrix->rows
1048 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1049 : 0);
1050 if (hl_row && hl_row->mode_line_p)
1051 height -= hl_row->height;
1052 else
1053 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1054 }
1055
1056 /* With a very small font and a mode-line that's taller than
1057 default, we might end up with a negative height. */
1058 return max (0, height);
1059 }
1060
1061 /* Return the window-relative coordinate of the left edge of display
1062 area AREA of window W. AREA < 0 means return the left edge of the
1063 whole window, to the right of the left fringe of W. */
1064
1065 int
1066 window_box_left_offset (struct window *w, int area)
1067 {
1068 int x;
1069
1070 if (w->pseudo_window_p)
1071 return 0;
1072
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1074
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1083 ? 0
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1088
1089 return x;
1090 }
1091
1092
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the right edge of the
1095 whole window, to the left of the right fringe of W. */
1096
1097 int
1098 window_box_right_offset (struct window *w, int area)
1099 {
1100 return window_box_left_offset (w, area) + window_box_width (w, area);
1101 }
1102
1103 /* Return the frame-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
1105 whole window, to the right of the left fringe of W. */
1106
1107 int
1108 window_box_left (struct window *w, int area)
1109 {
1110 struct frame *f = XFRAME (w->frame);
1111 int x;
1112
1113 if (w->pseudo_window_p)
1114 return FRAME_INTERNAL_BORDER_WIDTH (f);
1115
1116 x = (WINDOW_LEFT_EDGE_X (w)
1117 + window_box_left_offset (w, area));
1118
1119 return x;
1120 }
1121
1122
1123 /* Return the frame-relative coordinate of the right edge of display
1124 area AREA of window W. AREA < 0 means return the right edge of the
1125 whole window, to the left of the right fringe of W. */
1126
1127 int
1128 window_box_right (struct window *w, int area)
1129 {
1130 return window_box_left (w, area) + window_box_width (w, area);
1131 }
1132
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines, in frame-relative coordinates. AREA < 0 means the
1135 whole window, not including the left and right fringes of
1136 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1137 coordinates of the upper-left corner of the box. Return in
1138 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1139
1140 void
1141 window_box (struct window *w, int area, int *box_x, int *box_y,
1142 int *box_width, int *box_height)
1143 {
1144 if (box_width)
1145 *box_width = window_box_width (w, area);
1146 if (box_height)
1147 *box_height = window_box_height (w);
1148 if (box_x)
1149 *box_x = window_box_left (w, area);
1150 if (box_y)
1151 {
1152 *box_y = WINDOW_TOP_EDGE_Y (w);
1153 if (WINDOW_WANTS_HEADER_LINE_P (w))
1154 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1155 }
1156 }
1157
1158
1159 /* Get the bounding box of the display area AREA of window W, without
1160 mode lines. AREA < 0 means the whole window, not including the
1161 left and right fringe of the window. Return in *TOP_LEFT_X
1162 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1163 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1164 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1165 box. */
1166
1167 static void
1168 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1169 int *bottom_right_x, int *bottom_right_y)
1170 {
1171 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1172 bottom_right_y);
1173 *bottom_right_x += *top_left_x;
1174 *bottom_right_y += *top_left_y;
1175 }
1176
1177
1178 \f
1179 /***********************************************************************
1180 Utilities
1181 ***********************************************************************/
1182
1183 /* Return the bottom y-position of the line the iterator IT is in.
1184 This can modify IT's settings. */
1185
1186 int
1187 line_bottom_y (struct it *it)
1188 {
1189 int line_height = it->max_ascent + it->max_descent;
1190 int line_top_y = it->current_y;
1191
1192 if (line_height == 0)
1193 {
1194 if (last_height)
1195 line_height = last_height;
1196 else if (IT_CHARPOS (*it) < ZV)
1197 {
1198 move_it_by_lines (it, 1);
1199 line_height = (it->max_ascent || it->max_descent
1200 ? it->max_ascent + it->max_descent
1201 : last_height);
1202 }
1203 else
1204 {
1205 struct glyph_row *row = it->glyph_row;
1206
1207 /* Use the default character height. */
1208 it->glyph_row = NULL;
1209 it->what = IT_CHARACTER;
1210 it->c = ' ';
1211 it->len = 1;
1212 PRODUCE_GLYPHS (it);
1213 line_height = it->ascent + it->descent;
1214 it->glyph_row = row;
1215 }
1216 }
1217
1218 return line_top_y + line_height;
1219 }
1220
1221 /* Subroutine of pos_visible_p below. Extracts a display string, if
1222 any, from the display spec given as its argument. */
1223 static Lisp_Object
1224 string_from_display_spec (Lisp_Object spec)
1225 {
1226 if (CONSP (spec))
1227 {
1228 while (CONSP (spec))
1229 {
1230 if (STRINGP (XCAR (spec)))
1231 return XCAR (spec);
1232 spec = XCDR (spec);
1233 }
1234 }
1235 else if (VECTORP (spec))
1236 {
1237 ptrdiff_t i;
1238
1239 for (i = 0; i < ASIZE (spec); i++)
1240 {
1241 if (STRINGP (AREF (spec, i)))
1242 return AREF (spec, i);
1243 }
1244 return Qnil;
1245 }
1246
1247 return spec;
1248 }
1249
1250
1251 /* Limit insanely large values of W->hscroll on frame F to the largest
1252 value that will still prevent first_visible_x and last_visible_x of
1253 'struct it' from overflowing an int. */
1254 static int
1255 window_hscroll_limited (struct window *w, struct frame *f)
1256 {
1257 ptrdiff_t window_hscroll = w->hscroll;
1258 int window_text_width = window_box_width (w, TEXT_AREA);
1259 int colwidth = FRAME_COLUMN_WIDTH (f);
1260
1261 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1262 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1263
1264 return window_hscroll;
1265 }
1266
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 CHARPOS < 0 means return info about WINDOW_END position.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1272
1273 int
1274 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1275 int *rtop, int *rbot, int *rowh, int *vpos)
1276 {
1277 struct it it;
1278 void *itdata = bidi_shelve_cache ();
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1282
1283 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1284 return visible_p;
1285
1286 if (XBUFFER (w->buffer) != current_buffer)
1287 {
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1290 }
1291
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293 /* Scrolling a minibuffer window via scroll bar when the echo area
1294 shows long text sometimes resets the minibuffer contents behind
1295 our backs. */
1296 if (CHARPOS (top) > ZV)
1297 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1298
1299 /* Compute exact mode line heights. */
1300 if (WINDOW_WANTS_MODELINE_P (w))
1301 current_mode_line_height
1302 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1303 BVAR (current_buffer, mode_line_format));
1304
1305 if (WINDOW_WANTS_HEADER_LINE_P (w))
1306 current_header_line_height
1307 = display_mode_line (w, HEADER_LINE_FACE_ID,
1308 BVAR (current_buffer, header_line_format));
1309
1310 start_display (&it, w, top);
1311 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1312 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1313
1314 if (charpos >= 0
1315 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1316 && IT_CHARPOS (it) >= charpos)
1317 /* When scanning backwards under bidi iteration, move_it_to
1318 stops at or _before_ CHARPOS, because it stops at or to
1319 the _right_ of the character at CHARPOS. */
1320 || (it.bidi_p && it.bidi_it.scan_dir == -1
1321 && IT_CHARPOS (it) <= charpos)))
1322 {
1323 /* We have reached CHARPOS, or passed it. How the call to
1324 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1325 or covered by a display property, move_it_to stops at the end
1326 of the invisible text, to the right of CHARPOS. (ii) If
1327 CHARPOS is in a display vector, move_it_to stops on its last
1328 glyph. */
1329 int top_x = it.current_x;
1330 int top_y = it.current_y;
1331 /* Calling line_bottom_y may change it.method, it.position, etc. */
1332 enum it_method it_method = it.method;
1333 int bottom_y = (last_height = 0, line_bottom_y (&it));
1334 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1335
1336 if (top_y < window_top_y)
1337 visible_p = bottom_y > window_top_y;
1338 else if (top_y < it.last_visible_y)
1339 visible_p = 1;
1340 if (bottom_y >= it.last_visible_y
1341 && it.bidi_p && it.bidi_it.scan_dir == -1
1342 && IT_CHARPOS (it) < charpos)
1343 {
1344 /* When the last line of the window is scanned backwards
1345 under bidi iteration, we could be duped into thinking
1346 that we have passed CHARPOS, when in fact move_it_to
1347 simply stopped short of CHARPOS because it reached
1348 last_visible_y. To see if that's what happened, we call
1349 move_it_to again with a slightly larger vertical limit,
1350 and see if it actually moved vertically; if it did, we
1351 didn't really reach CHARPOS, which is beyond window end. */
1352 struct it save_it = it;
1353 /* Why 10? because we don't know how many canonical lines
1354 will the height of the next line(s) be. So we guess. */
1355 int ten_more_lines =
1356 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1357
1358 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1359 MOVE_TO_POS | MOVE_TO_Y);
1360 if (it.current_y > top_y)
1361 visible_p = 0;
1362
1363 it = save_it;
1364 }
1365 if (visible_p)
1366 {
1367 if (it_method == GET_FROM_DISPLAY_VECTOR)
1368 {
1369 /* We stopped on the last glyph of a display vector.
1370 Try and recompute. Hack alert! */
1371 if (charpos < 2 || top.charpos >= charpos)
1372 top_x = it.glyph_row->x;
1373 else
1374 {
1375 struct it it2;
1376 start_display (&it2, w, top);
1377 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1378 get_next_display_element (&it2);
1379 PRODUCE_GLYPHS (&it2);
1380 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1381 || it2.current_x > it2.last_visible_x)
1382 top_x = it.glyph_row->x;
1383 else
1384 {
1385 top_x = it2.current_x;
1386 top_y = it2.current_y;
1387 }
1388 }
1389 }
1390 else if (IT_CHARPOS (it) != charpos)
1391 {
1392 Lisp_Object cpos = make_number (charpos);
1393 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1394 Lisp_Object string = string_from_display_spec (spec);
1395 int newline_in_string = 0;
1396
1397 if (STRINGP (string))
1398 {
1399 const char *s = SSDATA (string);
1400 const char *e = s + SBYTES (string);
1401 while (s < e)
1402 {
1403 if (*s++ == '\n')
1404 {
1405 newline_in_string = 1;
1406 break;
1407 }
1408 }
1409 }
1410 /* The tricky code below is needed because there's a
1411 discrepancy between move_it_to and how we set cursor
1412 when the display line ends in a newline from a
1413 display string. move_it_to will stop _after_ such
1414 display strings, whereas set_cursor_from_row
1415 conspires with cursor_row_p to place the cursor on
1416 the first glyph produced from the display string. */
1417
1418 /* We have overshoot PT because it is covered by a
1419 display property whose value is a string. If the
1420 string includes embedded newlines, we are also in the
1421 wrong display line. Backtrack to the correct line,
1422 where the display string begins. */
1423 if (newline_in_string)
1424 {
1425 Lisp_Object startpos, endpos;
1426 EMACS_INT start, end;
1427 struct it it3;
1428 int it3_moved;
1429
1430 /* Find the first and the last buffer positions
1431 covered by the display string. */
1432 endpos =
1433 Fnext_single_char_property_change (cpos, Qdisplay,
1434 Qnil, Qnil);
1435 startpos =
1436 Fprevious_single_char_property_change (endpos, Qdisplay,
1437 Qnil, Qnil);
1438 start = XFASTINT (startpos);
1439 end = XFASTINT (endpos);
1440 /* Move to the last buffer position before the
1441 display property. */
1442 start_display (&it3, w, top);
1443 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* Move forward one more line if the position before
1445 the display string is a newline or if it is the
1446 rightmost character on a line that is
1447 continued or word-wrapped. */
1448 if (it3.method == GET_FROM_BUFFER
1449 && it3.c == '\n')
1450 move_it_by_lines (&it3, 1);
1451 else if (move_it_in_display_line_to (&it3, -1,
1452 it3.current_x
1453 + it3.pixel_width,
1454 MOVE_TO_X)
1455 == MOVE_LINE_CONTINUED)
1456 {
1457 move_it_by_lines (&it3, 1);
1458 /* When we are under word-wrap, the #$@%!
1459 move_it_by_lines moves 2 lines, so we need to
1460 fix that up. */
1461 if (it3.line_wrap == WORD_WRAP)
1462 move_it_by_lines (&it3, -1);
1463 }
1464
1465 /* Record the vertical coordinate of the display
1466 line where we wound up. */
1467 top_y = it3.current_y;
1468 if (it3.bidi_p)
1469 {
1470 /* When characters are reordered for display,
1471 the character displayed to the left of the
1472 display string could be _after_ the display
1473 property in the logical order. Use the
1474 smallest vertical position of these two. */
1475 start_display (&it3, w, top);
1476 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1477 if (it3.current_y < top_y)
1478 top_y = it3.current_y;
1479 }
1480 /* Move from the top of the window to the beginning
1481 of the display line where the display string
1482 begins. */
1483 start_display (&it3, w, top);
1484 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1485 /* If it3_moved stays zero after the 'while' loop
1486 below, that means we already were at a newline
1487 before the loop (e.g., the display string begins
1488 with a newline), so we don't need to (and cannot)
1489 inspect the glyphs of it3.glyph_row, because
1490 PRODUCE_GLYPHS will not produce anything for a
1491 newline, and thus it3.glyph_row stays at its
1492 stale content it got at top of the window. */
1493 it3_moved = 0;
1494 /* Finally, advance the iterator until we hit the
1495 first display element whose character position is
1496 CHARPOS, or until the first newline from the
1497 display string, which signals the end of the
1498 display line. */
1499 while (get_next_display_element (&it3))
1500 {
1501 PRODUCE_GLYPHS (&it3);
1502 if (IT_CHARPOS (it3) == charpos
1503 || ITERATOR_AT_END_OF_LINE_P (&it3))
1504 break;
1505 it3_moved = 1;
1506 set_iterator_to_next (&it3, 0);
1507 }
1508 top_x = it3.current_x - it3.pixel_width;
1509 /* Normally, we would exit the above loop because we
1510 found the display element whose character
1511 position is CHARPOS. For the contingency that we
1512 didn't, and stopped at the first newline from the
1513 display string, move back over the glyphs
1514 produced from the string, until we find the
1515 rightmost glyph not from the string. */
1516 if (it3_moved
1517 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1518 {
1519 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1520 + it3.glyph_row->used[TEXT_AREA];
1521
1522 while (EQ ((g - 1)->object, string))
1523 {
1524 --g;
1525 top_x -= g->pixel_width;
1526 }
1527 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1528 + it3.glyph_row->used[TEXT_AREA]);
1529 }
1530 }
1531 }
1532
1533 *x = top_x;
1534 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1535 *rtop = max (0, window_top_y - top_y);
1536 *rbot = max (0, bottom_y - it.last_visible_y);
1537 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1538 - max (top_y, window_top_y)));
1539 *vpos = it.vpos;
1540 }
1541 }
1542 else
1543 {
1544 /* We were asked to provide info about WINDOW_END. */
1545 struct it it2;
1546 void *it2data = NULL;
1547
1548 SAVE_IT (it2, it, it2data);
1549 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1550 move_it_by_lines (&it, 1);
1551 if (charpos < IT_CHARPOS (it)
1552 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1553 {
1554 visible_p = 1;
1555 RESTORE_IT (&it2, &it2, it2data);
1556 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1557 *x = it2.current_x;
1558 *y = it2.current_y + it2.max_ascent - it2.ascent;
1559 *rtop = max (0, -it2.current_y);
1560 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1561 - it.last_visible_y));
1562 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1563 it.last_visible_y)
1564 - max (it2.current_y,
1565 WINDOW_HEADER_LINE_HEIGHT (w))));
1566 *vpos = it2.vpos;
1567 }
1568 else
1569 bidi_unshelve_cache (it2data, 1);
1570 }
1571 bidi_unshelve_cache (itdata, 0);
1572
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1575
1576 current_header_line_height = current_mode_line_height = -1;
1577
1578 if (visible_p && w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1582
1583 #if 0
1584 /* Debugging code. */
1585 if (visible_p)
1586 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1587 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1588 else
1589 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1590 #endif
1591
1592 return visible_p;
1593 }
1594
1595
1596 /* Return the next character from STR. Return in *LEN the length of
1597 the character. This is like STRING_CHAR_AND_LENGTH but never
1598 returns an invalid character. If we find one, we return a `?', but
1599 with the length of the invalid character. */
1600
1601 static int
1602 string_char_and_length (const unsigned char *str, int *len)
1603 {
1604 int c;
1605
1606 c = STRING_CHAR_AND_LENGTH (str, *len);
1607 if (!CHAR_VALID_P (c))
1608 /* We may not change the length here because other places in Emacs
1609 don't use this function, i.e. they silently accept invalid
1610 characters. */
1611 c = '?';
1612
1613 return c;
1614 }
1615
1616
1617
1618 /* Given a position POS containing a valid character and byte position
1619 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1620
1621 static struct text_pos
1622 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1623 {
1624 eassert (STRINGP (string) && nchars >= 0);
1625
1626 if (STRING_MULTIBYTE (string))
1627 {
1628 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1629 int len;
1630
1631 while (nchars--)
1632 {
1633 string_char_and_length (p, &len);
1634 p += len;
1635 CHARPOS (pos) += 1;
1636 BYTEPOS (pos) += len;
1637 }
1638 }
1639 else
1640 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1641
1642 return pos;
1643 }
1644
1645
1646 /* Value is the text position, i.e. character and byte position,
1647 for character position CHARPOS in STRING. */
1648
1649 static struct text_pos
1650 string_pos (ptrdiff_t charpos, Lisp_Object string)
1651 {
1652 struct text_pos pos;
1653 eassert (STRINGP (string));
1654 eassert (charpos >= 0);
1655 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1656 return pos;
1657 }
1658
1659
1660 /* Value is a text position, i.e. character and byte position, for
1661 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1662 means recognize multibyte characters. */
1663
1664 static struct text_pos
1665 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1666 {
1667 struct text_pos pos;
1668
1669 eassert (s != NULL);
1670 eassert (charpos >= 0);
1671
1672 if (multibyte_p)
1673 {
1674 int len;
1675
1676 SET_TEXT_POS (pos, 0, 0);
1677 while (charpos--)
1678 {
1679 string_char_and_length ((const unsigned char *) s, &len);
1680 s += len;
1681 CHARPOS (pos) += 1;
1682 BYTEPOS (pos) += len;
1683 }
1684 }
1685 else
1686 SET_TEXT_POS (pos, charpos, charpos);
1687
1688 return pos;
1689 }
1690
1691
1692 /* Value is the number of characters in C string S. MULTIBYTE_P
1693 non-zero means recognize multibyte characters. */
1694
1695 static ptrdiff_t
1696 number_of_chars (const char *s, int multibyte_p)
1697 {
1698 ptrdiff_t nchars;
1699
1700 if (multibyte_p)
1701 {
1702 ptrdiff_t rest = strlen (s);
1703 int len;
1704 const unsigned char *p = (const unsigned char *) s;
1705
1706 for (nchars = 0; rest > 0; ++nchars)
1707 {
1708 string_char_and_length (p, &len);
1709 rest -= len, p += len;
1710 }
1711 }
1712 else
1713 nchars = strlen (s);
1714
1715 return nchars;
1716 }
1717
1718
1719 /* Compute byte position NEWPOS->bytepos corresponding to
1720 NEWPOS->charpos. POS is a known position in string STRING.
1721 NEWPOS->charpos must be >= POS.charpos. */
1722
1723 static void
1724 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1725 {
1726 eassert (STRINGP (string));
1727 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1728
1729 if (STRING_MULTIBYTE (string))
1730 *newpos = string_pos_nchars_ahead (pos, string,
1731 CHARPOS (*newpos) - CHARPOS (pos));
1732 else
1733 BYTEPOS (*newpos) = CHARPOS (*newpos);
1734 }
1735
1736 /* EXPORT:
1737 Return an estimation of the pixel height of mode or header lines on
1738 frame F. FACE_ID specifies what line's height to estimate. */
1739
1740 int
1741 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1742 {
1743 #ifdef HAVE_WINDOW_SYSTEM
1744 if (FRAME_WINDOW_P (f))
1745 {
1746 int height = FONT_HEIGHT (FRAME_FONT (f));
1747
1748 /* This function is called so early when Emacs starts that the face
1749 cache and mode line face are not yet initialized. */
1750 if (FRAME_FACE_CACHE (f))
1751 {
1752 struct face *face = FACE_FROM_ID (f, face_id);
1753 if (face)
1754 {
1755 if (face->font)
1756 height = FONT_HEIGHT (face->font);
1757 if (face->box_line_width > 0)
1758 height += 2 * face->box_line_width;
1759 }
1760 }
1761
1762 return height;
1763 }
1764 #endif
1765
1766 return 1;
1767 }
1768
1769 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1770 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1771 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1772 not force the value into range. */
1773
1774 void
1775 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1776 int *x, int *y, NativeRectangle *bounds, int noclip)
1777 {
1778
1779 #ifdef HAVE_WINDOW_SYSTEM
1780 if (FRAME_WINDOW_P (f))
1781 {
1782 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1783 even for negative values. */
1784 if (pix_x < 0)
1785 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1786 if (pix_y < 0)
1787 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1788
1789 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1790 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1791
1792 if (bounds)
1793 STORE_NATIVE_RECT (*bounds,
1794 FRAME_COL_TO_PIXEL_X (f, pix_x),
1795 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1796 FRAME_COLUMN_WIDTH (f) - 1,
1797 FRAME_LINE_HEIGHT (f) - 1);
1798
1799 if (!noclip)
1800 {
1801 if (pix_x < 0)
1802 pix_x = 0;
1803 else if (pix_x > FRAME_TOTAL_COLS (f))
1804 pix_x = FRAME_TOTAL_COLS (f);
1805
1806 if (pix_y < 0)
1807 pix_y = 0;
1808 else if (pix_y > FRAME_LINES (f))
1809 pix_y = FRAME_LINES (f);
1810 }
1811 }
1812 #endif
1813
1814 *x = pix_x;
1815 *y = pix_y;
1816 }
1817
1818
1819 /* Find the glyph under window-relative coordinates X/Y in window W.
1820 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1821 strings. Return in *HPOS and *VPOS the row and column number of
1822 the glyph found. Return in *AREA the glyph area containing X.
1823 Value is a pointer to the glyph found or null if X/Y is not on
1824 text, or we can't tell because W's current matrix is not up to
1825 date. */
1826
1827 static
1828 struct glyph *
1829 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1830 int *dx, int *dy, int *area)
1831 {
1832 struct glyph *glyph, *end;
1833 struct glyph_row *row = NULL;
1834 int x0, i;
1835
1836 /* Find row containing Y. Give up if some row is not enabled. */
1837 for (i = 0; i < w->current_matrix->nrows; ++i)
1838 {
1839 row = MATRIX_ROW (w->current_matrix, i);
1840 if (!row->enabled_p)
1841 return NULL;
1842 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1843 break;
1844 }
1845
1846 *vpos = i;
1847 *hpos = 0;
1848
1849 /* Give up if Y is not in the window. */
1850 if (i == w->current_matrix->nrows)
1851 return NULL;
1852
1853 /* Get the glyph area containing X. */
1854 if (w->pseudo_window_p)
1855 {
1856 *area = TEXT_AREA;
1857 x0 = 0;
1858 }
1859 else
1860 {
1861 if (x < window_box_left_offset (w, TEXT_AREA))
1862 {
1863 *area = LEFT_MARGIN_AREA;
1864 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1865 }
1866 else if (x < window_box_right_offset (w, TEXT_AREA))
1867 {
1868 *area = TEXT_AREA;
1869 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1870 }
1871 else
1872 {
1873 *area = RIGHT_MARGIN_AREA;
1874 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1875 }
1876 }
1877
1878 /* Find glyph containing X. */
1879 glyph = row->glyphs[*area];
1880 end = glyph + row->used[*area];
1881 x -= x0;
1882 while (glyph < end && x >= glyph->pixel_width)
1883 {
1884 x -= glyph->pixel_width;
1885 ++glyph;
1886 }
1887
1888 if (glyph == end)
1889 return NULL;
1890
1891 if (dx)
1892 {
1893 *dx = x;
1894 *dy = y - (row->y + row->ascent - glyph->ascent);
1895 }
1896
1897 *hpos = glyph - row->glyphs[*area];
1898 return glyph;
1899 }
1900
1901 /* Convert frame-relative x/y to coordinates relative to window W.
1902 Takes pseudo-windows into account. */
1903
1904 static void
1905 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1906 {
1907 if (w->pseudo_window_p)
1908 {
1909 /* A pseudo-window is always full-width, and starts at the
1910 left edge of the frame, plus a frame border. */
1911 struct frame *f = XFRAME (w->frame);
1912 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1913 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1914 }
1915 else
1916 {
1917 *x -= WINDOW_LEFT_EDGE_X (w);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 }
1920 }
1921
1922 #ifdef HAVE_WINDOW_SYSTEM
1923
1924 /* EXPORT:
1925 Return in RECTS[] at most N clipping rectangles for glyph string S.
1926 Return the number of stored rectangles. */
1927
1928 int
1929 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1930 {
1931 XRectangle r;
1932
1933 if (n <= 0)
1934 return 0;
1935
1936 if (s->row->full_width_p)
1937 {
1938 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1939 r.x = WINDOW_LEFT_EDGE_X (s->w);
1940 r.width = WINDOW_TOTAL_WIDTH (s->w);
1941
1942 /* Unless displaying a mode or menu bar line, which are always
1943 fully visible, clip to the visible part of the row. */
1944 if (s->w->pseudo_window_p)
1945 r.height = s->row->visible_height;
1946 else
1947 r.height = s->height;
1948 }
1949 else
1950 {
1951 /* This is a text line that may be partially visible. */
1952 r.x = window_box_left (s->w, s->area);
1953 r.width = window_box_width (s->w, s->area);
1954 r.height = s->row->visible_height;
1955 }
1956
1957 if (s->clip_head)
1958 if (r.x < s->clip_head->x)
1959 {
1960 if (r.width >= s->clip_head->x - r.x)
1961 r.width -= s->clip_head->x - r.x;
1962 else
1963 r.width = 0;
1964 r.x = s->clip_head->x;
1965 }
1966 if (s->clip_tail)
1967 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1968 {
1969 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1970 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1971 else
1972 r.width = 0;
1973 }
1974
1975 /* If S draws overlapping rows, it's sufficient to use the top and
1976 bottom of the window for clipping because this glyph string
1977 intentionally draws over other lines. */
1978 if (s->for_overlaps)
1979 {
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 r.height = window_text_bottom_y (s->w) - r.y;
1982
1983 /* Alas, the above simple strategy does not work for the
1984 environments with anti-aliased text: if the same text is
1985 drawn onto the same place multiple times, it gets thicker.
1986 If the overlap we are processing is for the erased cursor, we
1987 take the intersection with the rectangle of the cursor. */
1988 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1989 {
1990 XRectangle rc, r_save = r;
1991
1992 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1993 rc.y = s->w->phys_cursor.y;
1994 rc.width = s->w->phys_cursor_width;
1995 rc.height = s->w->phys_cursor_height;
1996
1997 x_intersect_rectangles (&r_save, &rc, &r);
1998 }
1999 }
2000 else
2001 {
2002 /* Don't use S->y for clipping because it doesn't take partially
2003 visible lines into account. For example, it can be negative for
2004 partially visible lines at the top of a window. */
2005 if (!s->row->full_width_p
2006 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2007 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2008 else
2009 r.y = max (0, s->row->y);
2010 }
2011
2012 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2013
2014 /* If drawing the cursor, don't let glyph draw outside its
2015 advertised boundaries. Cleartype does this under some circumstances. */
2016 if (s->hl == DRAW_CURSOR)
2017 {
2018 struct glyph *glyph = s->first_glyph;
2019 int height, max_y;
2020
2021 if (s->x > r.x)
2022 {
2023 r.width -= s->x - r.x;
2024 r.x = s->x;
2025 }
2026 r.width = min (r.width, glyph->pixel_width);
2027
2028 /* If r.y is below window bottom, ensure that we still see a cursor. */
2029 height = min (glyph->ascent + glyph->descent,
2030 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2031 max_y = window_text_bottom_y (s->w) - height;
2032 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2033 if (s->ybase - glyph->ascent > max_y)
2034 {
2035 r.y = max_y;
2036 r.height = height;
2037 }
2038 else
2039 {
2040 /* Don't draw cursor glyph taller than our actual glyph. */
2041 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2042 if (height < r.height)
2043 {
2044 max_y = r.y + r.height;
2045 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2046 r.height = min (max_y - r.y, height);
2047 }
2048 }
2049 }
2050
2051 if (s->row->clip)
2052 {
2053 XRectangle r_save = r;
2054
2055 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2056 r.width = 0;
2057 }
2058
2059 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2060 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2061 {
2062 #ifdef CONVERT_FROM_XRECT
2063 CONVERT_FROM_XRECT (r, *rects);
2064 #else
2065 *rects = r;
2066 #endif
2067 return 1;
2068 }
2069 else
2070 {
2071 /* If we are processing overlapping and allowed to return
2072 multiple clipping rectangles, we exclude the row of the glyph
2073 string from the clipping rectangle. This is to avoid drawing
2074 the same text on the environment with anti-aliasing. */
2075 #ifdef CONVERT_FROM_XRECT
2076 XRectangle rs[2];
2077 #else
2078 XRectangle *rs = rects;
2079 #endif
2080 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2081
2082 if (s->for_overlaps & OVERLAPS_PRED)
2083 {
2084 rs[i] = r;
2085 if (r.y + r.height > row_y)
2086 {
2087 if (r.y < row_y)
2088 rs[i].height = row_y - r.y;
2089 else
2090 rs[i].height = 0;
2091 }
2092 i++;
2093 }
2094 if (s->for_overlaps & OVERLAPS_SUCC)
2095 {
2096 rs[i] = r;
2097 if (r.y < row_y + s->row->visible_height)
2098 {
2099 if (r.y + r.height > row_y + s->row->visible_height)
2100 {
2101 rs[i].y = row_y + s->row->visible_height;
2102 rs[i].height = r.y + r.height - rs[i].y;
2103 }
2104 else
2105 rs[i].height = 0;
2106 }
2107 i++;
2108 }
2109
2110 n = i;
2111 #ifdef CONVERT_FROM_XRECT
2112 for (i = 0; i < n; i++)
2113 CONVERT_FROM_XRECT (rs[i], rects[i]);
2114 #endif
2115 return n;
2116 }
2117 }
2118
2119 /* EXPORT:
2120 Return in *NR the clipping rectangle for glyph string S. */
2121
2122 void
2123 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2124 {
2125 get_glyph_string_clip_rects (s, nr, 1);
2126 }
2127
2128
2129 /* EXPORT:
2130 Return the position and height of the phys cursor in window W.
2131 Set w->phys_cursor_width to width of phys cursor.
2132 */
2133
2134 void
2135 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2136 struct glyph *glyph, int *xp, int *yp, int *heightp)
2137 {
2138 struct frame *f = XFRAME (WINDOW_FRAME (w));
2139 int x, y, wd, h, h0, y0;
2140
2141 /* Compute the width of the rectangle to draw. If on a stretch
2142 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2143 rectangle as wide as the glyph, but use a canonical character
2144 width instead. */
2145 wd = glyph->pixel_width - 1;
2146 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2147 wd++; /* Why? */
2148 #endif
2149
2150 x = w->phys_cursor.x;
2151 if (x < 0)
2152 {
2153 wd += x;
2154 x = 0;
2155 }
2156
2157 if (glyph->type == STRETCH_GLYPH
2158 && !x_stretch_cursor_p)
2159 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2160 w->phys_cursor_width = wd;
2161
2162 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2163
2164 /* If y is below window bottom, ensure that we still see a cursor. */
2165 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2166
2167 h = max (h0, glyph->ascent + glyph->descent);
2168 h0 = min (h0, glyph->ascent + glyph->descent);
2169
2170 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2171 if (y < y0)
2172 {
2173 h = max (h - (y0 - y) + 1, h0);
2174 y = y0 - 1;
2175 }
2176 else
2177 {
2178 y0 = window_text_bottom_y (w) - h0;
2179 if (y > y0)
2180 {
2181 h += y - y0;
2182 y = y0;
2183 }
2184 }
2185
2186 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2187 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2188 *heightp = h;
2189 }
2190
2191 /*
2192 * Remember which glyph the mouse is over.
2193 */
2194
2195 void
2196 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2197 {
2198 Lisp_Object window;
2199 struct window *w;
2200 struct glyph_row *r, *gr, *end_row;
2201 enum window_part part;
2202 enum glyph_row_area area;
2203 int x, y, width, height;
2204
2205 /* Try to determine frame pixel position and size of the glyph under
2206 frame pixel coordinates X/Y on frame F. */
2207
2208 if (!f->glyphs_initialized_p
2209 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2210 NILP (window)))
2211 {
2212 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2213 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2214 goto virtual_glyph;
2215 }
2216
2217 w = XWINDOW (window);
2218 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2219 height = WINDOW_FRAME_LINE_HEIGHT (w);
2220
2221 x = window_relative_x_coord (w, part, gx);
2222 y = gy - WINDOW_TOP_EDGE_Y (w);
2223
2224 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2225 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2226
2227 if (w->pseudo_window_p)
2228 {
2229 area = TEXT_AREA;
2230 part = ON_MODE_LINE; /* Don't adjust margin. */
2231 goto text_glyph;
2232 }
2233
2234 switch (part)
2235 {
2236 case ON_LEFT_MARGIN:
2237 area = LEFT_MARGIN_AREA;
2238 goto text_glyph;
2239
2240 case ON_RIGHT_MARGIN:
2241 area = RIGHT_MARGIN_AREA;
2242 goto text_glyph;
2243
2244 case ON_HEADER_LINE:
2245 case ON_MODE_LINE:
2246 gr = (part == ON_HEADER_LINE
2247 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2248 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2249 gy = gr->y;
2250 area = TEXT_AREA;
2251 goto text_glyph_row_found;
2252
2253 case ON_TEXT:
2254 area = TEXT_AREA;
2255
2256 text_glyph:
2257 gr = 0; gy = 0;
2258 for (; r <= end_row && r->enabled_p; ++r)
2259 if (r->y + r->height > y)
2260 {
2261 gr = r; gy = r->y;
2262 break;
2263 }
2264
2265 text_glyph_row_found:
2266 if (gr && gy <= y)
2267 {
2268 struct glyph *g = gr->glyphs[area];
2269 struct glyph *end = g + gr->used[area];
2270
2271 height = gr->height;
2272 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2273 if (gx + g->pixel_width > x)
2274 break;
2275
2276 if (g < end)
2277 {
2278 if (g->type == IMAGE_GLYPH)
2279 {
2280 /* Don't remember when mouse is over image, as
2281 image may have hot-spots. */
2282 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2283 return;
2284 }
2285 width = g->pixel_width;
2286 }
2287 else
2288 {
2289 /* Use nominal char spacing at end of line. */
2290 x -= gx;
2291 gx += (x / width) * width;
2292 }
2293
2294 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2295 gx += window_box_left_offset (w, area);
2296 }
2297 else
2298 {
2299 /* Use nominal line height at end of window. */
2300 gx = (x / width) * width;
2301 y -= gy;
2302 gy += (y / height) * height;
2303 }
2304 break;
2305
2306 case ON_LEFT_FRINGE:
2307 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2309 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2310 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2311 goto row_glyph;
2312
2313 case ON_RIGHT_FRINGE:
2314 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2315 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2316 : window_box_right_offset (w, TEXT_AREA));
2317 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2318 goto row_glyph;
2319
2320 case ON_SCROLL_BAR:
2321 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2322 ? 0
2323 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2324 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2325 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2326 : 0)));
2327 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2328
2329 row_glyph:
2330 gr = 0, gy = 0;
2331 for (; r <= end_row && r->enabled_p; ++r)
2332 if (r->y + r->height > y)
2333 {
2334 gr = r; gy = r->y;
2335 break;
2336 }
2337
2338 if (gr && gy <= y)
2339 height = gr->height;
2340 else
2341 {
2342 /* Use nominal line height at end of window. */
2343 y -= gy;
2344 gy += (y / height) * height;
2345 }
2346 break;
2347
2348 default:
2349 ;
2350 virtual_glyph:
2351 /* If there is no glyph under the mouse, then we divide the screen
2352 into a grid of the smallest glyph in the frame, and use that
2353 as our "glyph". */
2354
2355 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2356 round down even for negative values. */
2357 if (gx < 0)
2358 gx -= width - 1;
2359 if (gy < 0)
2360 gy -= height - 1;
2361
2362 gx = (gx / width) * width;
2363 gy = (gy / height) * height;
2364
2365 goto store_rect;
2366 }
2367
2368 gx += WINDOW_LEFT_EDGE_X (w);
2369 gy += WINDOW_TOP_EDGE_Y (w);
2370
2371 store_rect:
2372 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2373
2374 /* Visible feedback for debugging. */
2375 #if 0
2376 #if HAVE_X_WINDOWS
2377 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2378 f->output_data.x->normal_gc,
2379 gx, gy, width, height);
2380 #endif
2381 #endif
2382 }
2383
2384
2385 #endif /* HAVE_WINDOW_SYSTEM */
2386
2387 \f
2388 /***********************************************************************
2389 Lisp form evaluation
2390 ***********************************************************************/
2391
2392 /* Error handler for safe_eval and safe_call. */
2393
2394 static Lisp_Object
2395 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2396 {
2397 add_to_log ("Error during redisplay: %S signaled %S",
2398 Flist (nargs, args), arg);
2399 return Qnil;
2400 }
2401
2402 /* Call function FUNC with the rest of NARGS - 1 arguments
2403 following. Return the result, or nil if something went
2404 wrong. Prevent redisplay during the evaluation. */
2405
2406 Lisp_Object
2407 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2408 {
2409 Lisp_Object val;
2410
2411 if (inhibit_eval_during_redisplay)
2412 val = Qnil;
2413 else
2414 {
2415 va_list ap;
2416 ptrdiff_t i;
2417 ptrdiff_t count = SPECPDL_INDEX ();
2418 struct gcpro gcpro1;
2419 Lisp_Object *args = alloca (nargs * word_size);
2420
2421 args[0] = func;
2422 va_start (ap, func);
2423 for (i = 1; i < nargs; i++)
2424 args[i] = va_arg (ap, Lisp_Object);
2425 va_end (ap);
2426
2427 GCPRO1 (args[0]);
2428 gcpro1.nvars = nargs;
2429 specbind (Qinhibit_redisplay, Qt);
2430 /* Use Qt to ensure debugger does not run,
2431 so there is no possibility of wanting to redisplay. */
2432 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2433 safe_eval_handler);
2434 UNGCPRO;
2435 val = unbind_to (count, val);
2436 }
2437
2438 return val;
2439 }
2440
2441
2442 /* Call function FN with one argument ARG.
2443 Return the result, or nil if something went wrong. */
2444
2445 Lisp_Object
2446 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2447 {
2448 return safe_call (2, fn, arg);
2449 }
2450
2451 static Lisp_Object Qeval;
2452
2453 Lisp_Object
2454 safe_eval (Lisp_Object sexpr)
2455 {
2456 return safe_call1 (Qeval, sexpr);
2457 }
2458
2459 /* Call function FN with two arguments ARG1 and ARG2.
2460 Return the result, or nil if something went wrong. */
2461
2462 Lisp_Object
2463 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2464 {
2465 return safe_call (3, fn, arg1, arg2);
2466 }
2467
2468
2469 \f
2470 /***********************************************************************
2471 Debugging
2472 ***********************************************************************/
2473
2474 #if 0
2475
2476 /* Define CHECK_IT to perform sanity checks on iterators.
2477 This is for debugging. It is too slow to do unconditionally. */
2478
2479 static void
2480 check_it (struct it *it)
2481 {
2482 if (it->method == GET_FROM_STRING)
2483 {
2484 eassert (STRINGP (it->string));
2485 eassert (IT_STRING_CHARPOS (*it) >= 0);
2486 }
2487 else
2488 {
2489 eassert (IT_STRING_CHARPOS (*it) < 0);
2490 if (it->method == GET_FROM_BUFFER)
2491 {
2492 /* Check that character and byte positions agree. */
2493 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2494 }
2495 }
2496
2497 if (it->dpvec)
2498 eassert (it->current.dpvec_index >= 0);
2499 else
2500 eassert (it->current.dpvec_index < 0);
2501 }
2502
2503 #define CHECK_IT(IT) check_it ((IT))
2504
2505 #else /* not 0 */
2506
2507 #define CHECK_IT(IT) (void) 0
2508
2509 #endif /* not 0 */
2510
2511
2512 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2513
2514 /* Check that the window end of window W is what we expect it
2515 to be---the last row in the current matrix displaying text. */
2516
2517 static void
2518 check_window_end (struct window *w)
2519 {
2520 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2521 {
2522 struct glyph_row *row;
2523 eassert ((row = MATRIX_ROW (w->current_matrix,
2524 XFASTINT (w->window_end_vpos)),
2525 !row->enabled_p
2526 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2527 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2528 }
2529 }
2530
2531 #define CHECK_WINDOW_END(W) check_window_end ((W))
2532
2533 #else
2534
2535 #define CHECK_WINDOW_END(W) (void) 0
2536
2537 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2538
2539 /* Return mark position if current buffer has the region of non-zero length,
2540 or -1 otherwise. */
2541
2542 static ptrdiff_t
2543 markpos_of_region (void)
2544 {
2545 if (!NILP (Vtransient_mark_mode)
2546 && !NILP (BVAR (current_buffer, mark_active))
2547 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2548 {
2549 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2550
2551 if (markpos != PT)
2552 return markpos;
2553 }
2554 return -1;
2555 }
2556
2557 /***********************************************************************
2558 Iterator initialization
2559 ***********************************************************************/
2560
2561 /* Initialize IT for displaying current_buffer in window W, starting
2562 at character position CHARPOS. CHARPOS < 0 means that no buffer
2563 position is specified which is useful when the iterator is assigned
2564 a position later. BYTEPOS is the byte position corresponding to
2565 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2566
2567 If ROW is not null, calls to produce_glyphs with IT as parameter
2568 will produce glyphs in that row.
2569
2570 BASE_FACE_ID is the id of a base face to use. It must be one of
2571 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2572 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2573 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2574
2575 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2576 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2577 will be initialized to use the corresponding mode line glyph row of
2578 the desired matrix of W. */
2579
2580 void
2581 init_iterator (struct it *it, struct window *w,
2582 ptrdiff_t charpos, ptrdiff_t bytepos,
2583 struct glyph_row *row, enum face_id base_face_id)
2584 {
2585 ptrdiff_t markpos;
2586 enum face_id remapped_base_face_id = base_face_id;
2587
2588 /* Some precondition checks. */
2589 eassert (w != NULL && it != NULL);
2590 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2591 && charpos <= ZV));
2592
2593 /* If face attributes have been changed since the last redisplay,
2594 free realized faces now because they depend on face definitions
2595 that might have changed. Don't free faces while there might be
2596 desired matrices pending which reference these faces. */
2597 if (face_change_count && !inhibit_free_realized_faces)
2598 {
2599 face_change_count = 0;
2600 free_all_realized_faces (Qnil);
2601 }
2602
2603 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2604 if (! NILP (Vface_remapping_alist))
2605 remapped_base_face_id
2606 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2607
2608 /* Use one of the mode line rows of W's desired matrix if
2609 appropriate. */
2610 if (row == NULL)
2611 {
2612 if (base_face_id == MODE_LINE_FACE_ID
2613 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2614 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2615 else if (base_face_id == HEADER_LINE_FACE_ID)
2616 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2617 }
2618
2619 /* Clear IT. */
2620 memset (it, 0, sizeof *it);
2621 it->current.overlay_string_index = -1;
2622 it->current.dpvec_index = -1;
2623 it->base_face_id = remapped_base_face_id;
2624 it->string = Qnil;
2625 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2626 it->paragraph_embedding = L2R;
2627 it->bidi_it.string.lstring = Qnil;
2628 it->bidi_it.string.s = NULL;
2629 it->bidi_it.string.bufpos = 0;
2630
2631 /* The window in which we iterate over current_buffer: */
2632 XSETWINDOW (it->window, w);
2633 it->w = w;
2634 it->f = XFRAME (w->frame);
2635
2636 it->cmp_it.id = -1;
2637
2638 /* Extra space between lines (on window systems only). */
2639 if (base_face_id == DEFAULT_FACE_ID
2640 && FRAME_WINDOW_P (it->f))
2641 {
2642 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2643 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2644 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2645 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2646 * FRAME_LINE_HEIGHT (it->f));
2647 else if (it->f->extra_line_spacing > 0)
2648 it->extra_line_spacing = it->f->extra_line_spacing;
2649 it->max_extra_line_spacing = 0;
2650 }
2651
2652 /* If realized faces have been removed, e.g. because of face
2653 attribute changes of named faces, recompute them. When running
2654 in batch mode, the face cache of the initial frame is null. If
2655 we happen to get called, make a dummy face cache. */
2656 if (FRAME_FACE_CACHE (it->f) == NULL)
2657 init_frame_faces (it->f);
2658 if (FRAME_FACE_CACHE (it->f)->used == 0)
2659 recompute_basic_faces (it->f);
2660
2661 /* Current value of the `slice', `space-width', and 'height' properties. */
2662 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2663 it->space_width = Qnil;
2664 it->font_height = Qnil;
2665 it->override_ascent = -1;
2666
2667 /* Are control characters displayed as `^C'? */
2668 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2669
2670 /* -1 means everything between a CR and the following line end
2671 is invisible. >0 means lines indented more than this value are
2672 invisible. */
2673 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2674 ? (clip_to_bounds
2675 (-1, XINT (BVAR (current_buffer, selective_display)),
2676 PTRDIFF_MAX))
2677 : (!NILP (BVAR (current_buffer, selective_display))
2678 ? -1 : 0));
2679 it->selective_display_ellipsis_p
2680 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2681
2682 /* Display table to use. */
2683 it->dp = window_display_table (w);
2684
2685 /* Are multibyte characters enabled in current_buffer? */
2686 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2687
2688 /* If visible region is of non-zero length, set IT->region_beg_charpos
2689 and IT->region_end_charpos to the start and end of a visible region
2690 in window IT->w. Set both to -1 to indicate no region. */
2691 markpos = markpos_of_region ();
2692 if (0 <= markpos
2693 /* Maybe highlight only in selected window. */
2694 && (/* Either show region everywhere. */
2695 highlight_nonselected_windows
2696 /* Or show region in the selected window. */
2697 || w == XWINDOW (selected_window)
2698 /* Or show the region if we are in the mini-buffer and W is
2699 the window the mini-buffer refers to. */
2700 || (MINI_WINDOW_P (XWINDOW (selected_window))
2701 && WINDOWP (minibuf_selected_window)
2702 && w == XWINDOW (minibuf_selected_window))))
2703 {
2704 it->region_beg_charpos = min (PT, markpos);
2705 it->region_end_charpos = max (PT, markpos);
2706 }
2707 else
2708 it->region_beg_charpos = it->region_end_charpos = -1;
2709
2710 /* Get the position at which the redisplay_end_trigger hook should
2711 be run, if it is to be run at all. */
2712 if (MARKERP (w->redisplay_end_trigger)
2713 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2714 it->redisplay_end_trigger_charpos
2715 = marker_position (w->redisplay_end_trigger);
2716 else if (INTEGERP (w->redisplay_end_trigger))
2717 it->redisplay_end_trigger_charpos =
2718 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2719
2720 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2721
2722 /* Are lines in the display truncated? */
2723 if (base_face_id != DEFAULT_FACE_ID
2724 || it->w->hscroll
2725 || (! WINDOW_FULL_WIDTH_P (it->w)
2726 && ((!NILP (Vtruncate_partial_width_windows)
2727 && !INTEGERP (Vtruncate_partial_width_windows))
2728 || (INTEGERP (Vtruncate_partial_width_windows)
2729 && (WINDOW_TOTAL_COLS (it->w)
2730 < XINT (Vtruncate_partial_width_windows))))))
2731 it->line_wrap = TRUNCATE;
2732 else if (NILP (BVAR (current_buffer, truncate_lines)))
2733 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2734 ? WINDOW_WRAP : WORD_WRAP;
2735 else
2736 it->line_wrap = TRUNCATE;
2737
2738 /* Get dimensions of truncation and continuation glyphs. These are
2739 displayed as fringe bitmaps under X, but we need them for such
2740 frames when the fringes are turned off. But leave the dimensions
2741 zero for tooltip frames, as these glyphs look ugly there and also
2742 sabotage calculations of tooltip dimensions in x-show-tip. */
2743 #ifdef HAVE_WINDOW_SYSTEM
2744 if (!(FRAME_WINDOW_P (it->f)
2745 && FRAMEP (tip_frame)
2746 && it->f == XFRAME (tip_frame)))
2747 #endif
2748 {
2749 if (it->line_wrap == TRUNCATE)
2750 {
2751 /* We will need the truncation glyph. */
2752 eassert (it->glyph_row == NULL);
2753 produce_special_glyphs (it, IT_TRUNCATION);
2754 it->truncation_pixel_width = it->pixel_width;
2755 }
2756 else
2757 {
2758 /* We will need the continuation glyph. */
2759 eassert (it->glyph_row == NULL);
2760 produce_special_glyphs (it, IT_CONTINUATION);
2761 it->continuation_pixel_width = it->pixel_width;
2762 }
2763 }
2764
2765 /* Reset these values to zero because the produce_special_glyphs
2766 above has changed them. */
2767 it->pixel_width = it->ascent = it->descent = 0;
2768 it->phys_ascent = it->phys_descent = 0;
2769
2770 /* Set this after getting the dimensions of truncation and
2771 continuation glyphs, so that we don't produce glyphs when calling
2772 produce_special_glyphs, above. */
2773 it->glyph_row = row;
2774 it->area = TEXT_AREA;
2775
2776 /* Forget any previous info about this row being reversed. */
2777 if (it->glyph_row)
2778 it->glyph_row->reversed_p = 0;
2779
2780 /* Get the dimensions of the display area. The display area
2781 consists of the visible window area plus a horizontally scrolled
2782 part to the left of the window. All x-values are relative to the
2783 start of this total display area. */
2784 if (base_face_id != DEFAULT_FACE_ID)
2785 {
2786 /* Mode lines, menu bar in terminal frames. */
2787 it->first_visible_x = 0;
2788 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2789 }
2790 else
2791 {
2792 it->first_visible_x =
2793 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2794 it->last_visible_x = (it->first_visible_x
2795 + window_box_width (w, TEXT_AREA));
2796
2797 /* If we truncate lines, leave room for the truncation glyph(s) at
2798 the right margin. Otherwise, leave room for the continuation
2799 glyph(s). Done only if the window has no fringes. Since we
2800 don't know at this point whether there will be any R2L lines in
2801 the window, we reserve space for truncation/continuation glyphs
2802 even if only one of the fringes is absent. */
2803 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2804 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2805 {
2806 if (it->line_wrap == TRUNCATE)
2807 it->last_visible_x -= it->truncation_pixel_width;
2808 else
2809 it->last_visible_x -= it->continuation_pixel_width;
2810 }
2811
2812 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2813 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2814 }
2815
2816 /* Leave room for a border glyph. */
2817 if (!FRAME_WINDOW_P (it->f)
2818 && !WINDOW_RIGHTMOST_P (it->w))
2819 it->last_visible_x -= 1;
2820
2821 it->last_visible_y = window_text_bottom_y (w);
2822
2823 /* For mode lines and alike, arrange for the first glyph having a
2824 left box line if the face specifies a box. */
2825 if (base_face_id != DEFAULT_FACE_ID)
2826 {
2827 struct face *face;
2828
2829 it->face_id = remapped_base_face_id;
2830
2831 /* If we have a boxed mode line, make the first character appear
2832 with a left box line. */
2833 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2834 if (face->box != FACE_NO_BOX)
2835 it->start_of_box_run_p = 1;
2836 }
2837
2838 /* If a buffer position was specified, set the iterator there,
2839 getting overlays and face properties from that position. */
2840 if (charpos >= BUF_BEG (current_buffer))
2841 {
2842 it->end_charpos = ZV;
2843 IT_CHARPOS (*it) = charpos;
2844
2845 /* We will rely on `reseat' to set this up properly, via
2846 handle_face_prop. */
2847 it->face_id = it->base_face_id;
2848
2849 /* Compute byte position if not specified. */
2850 if (bytepos < charpos)
2851 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2852 else
2853 IT_BYTEPOS (*it) = bytepos;
2854
2855 it->start = it->current;
2856 /* Do we need to reorder bidirectional text? Not if this is a
2857 unibyte buffer: by definition, none of the single-byte
2858 characters are strong R2L, so no reordering is needed. And
2859 bidi.c doesn't support unibyte buffers anyway. Also, don't
2860 reorder while we are loading loadup.el, since the tables of
2861 character properties needed for reordering are not yet
2862 available. */
2863 it->bidi_p =
2864 NILP (Vpurify_flag)
2865 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2866 && it->multibyte_p;
2867
2868 /* If we are to reorder bidirectional text, init the bidi
2869 iterator. */
2870 if (it->bidi_p)
2871 {
2872 /* Note the paragraph direction that this buffer wants to
2873 use. */
2874 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2875 Qleft_to_right))
2876 it->paragraph_embedding = L2R;
2877 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2878 Qright_to_left))
2879 it->paragraph_embedding = R2L;
2880 else
2881 it->paragraph_embedding = NEUTRAL_DIR;
2882 bidi_unshelve_cache (NULL, 0);
2883 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2884 &it->bidi_it);
2885 }
2886
2887 /* Compute faces etc. */
2888 reseat (it, it->current.pos, 1);
2889 }
2890
2891 CHECK_IT (it);
2892 }
2893
2894
2895 /* Initialize IT for the display of window W with window start POS. */
2896
2897 void
2898 start_display (struct it *it, struct window *w, struct text_pos pos)
2899 {
2900 struct glyph_row *row;
2901 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2902
2903 row = w->desired_matrix->rows + first_vpos;
2904 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2905 it->first_vpos = first_vpos;
2906
2907 /* Don't reseat to previous visible line start if current start
2908 position is in a string or image. */
2909 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2910 {
2911 int start_at_line_beg_p;
2912 int first_y = it->current_y;
2913
2914 /* If window start is not at a line start, skip forward to POS to
2915 get the correct continuation lines width. */
2916 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2917 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2918 if (!start_at_line_beg_p)
2919 {
2920 int new_x;
2921
2922 reseat_at_previous_visible_line_start (it);
2923 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2924
2925 new_x = it->current_x + it->pixel_width;
2926
2927 /* If lines are continued, this line may end in the middle
2928 of a multi-glyph character (e.g. a control character
2929 displayed as \003, or in the middle of an overlay
2930 string). In this case move_it_to above will not have
2931 taken us to the start of the continuation line but to the
2932 end of the continued line. */
2933 if (it->current_x > 0
2934 && it->line_wrap != TRUNCATE /* Lines are continued. */
2935 && (/* And glyph doesn't fit on the line. */
2936 new_x > it->last_visible_x
2937 /* Or it fits exactly and we're on a window
2938 system frame. */
2939 || (new_x == it->last_visible_x
2940 && FRAME_WINDOW_P (it->f)
2941 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2942 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2943 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2944 {
2945 if ((it->current.dpvec_index >= 0
2946 || it->current.overlay_string_index >= 0)
2947 /* If we are on a newline from a display vector or
2948 overlay string, then we are already at the end of
2949 a screen line; no need to go to the next line in
2950 that case, as this line is not really continued.
2951 (If we do go to the next line, C-e will not DTRT.) */
2952 && it->c != '\n')
2953 {
2954 set_iterator_to_next (it, 1);
2955 move_it_in_display_line_to (it, -1, -1, 0);
2956 }
2957
2958 it->continuation_lines_width += it->current_x;
2959 }
2960 /* If the character at POS is displayed via a display
2961 vector, move_it_to above stops at the final glyph of
2962 IT->dpvec. To make the caller redisplay that character
2963 again (a.k.a. start at POS), we need to reset the
2964 dpvec_index to the beginning of IT->dpvec. */
2965 else if (it->current.dpvec_index >= 0)
2966 it->current.dpvec_index = 0;
2967
2968 /* We're starting a new display line, not affected by the
2969 height of the continued line, so clear the appropriate
2970 fields in the iterator structure. */
2971 it->max_ascent = it->max_descent = 0;
2972 it->max_phys_ascent = it->max_phys_descent = 0;
2973
2974 it->current_y = first_y;
2975 it->vpos = 0;
2976 it->current_x = it->hpos = 0;
2977 }
2978 }
2979 }
2980
2981
2982 /* Return 1 if POS is a position in ellipses displayed for invisible
2983 text. W is the window we display, for text property lookup. */
2984
2985 static int
2986 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2987 {
2988 Lisp_Object prop, window;
2989 int ellipses_p = 0;
2990 ptrdiff_t charpos = CHARPOS (pos->pos);
2991
2992 /* If POS specifies a position in a display vector, this might
2993 be for an ellipsis displayed for invisible text. We won't
2994 get the iterator set up for delivering that ellipsis unless
2995 we make sure that it gets aware of the invisible text. */
2996 if (pos->dpvec_index >= 0
2997 && pos->overlay_string_index < 0
2998 && CHARPOS (pos->string_pos) < 0
2999 && charpos > BEGV
3000 && (XSETWINDOW (window, w),
3001 prop = Fget_char_property (make_number (charpos),
3002 Qinvisible, window),
3003 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3004 {
3005 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3006 window);
3007 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3008 }
3009
3010 return ellipses_p;
3011 }
3012
3013
3014 /* Initialize IT for stepping through current_buffer in window W,
3015 starting at position POS that includes overlay string and display
3016 vector/ control character translation position information. Value
3017 is zero if there are overlay strings with newlines at POS. */
3018
3019 static int
3020 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3021 {
3022 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3023 int i, overlay_strings_with_newlines = 0;
3024
3025 /* If POS specifies a position in a display vector, this might
3026 be for an ellipsis displayed for invisible text. We won't
3027 get the iterator set up for delivering that ellipsis unless
3028 we make sure that it gets aware of the invisible text. */
3029 if (in_ellipses_for_invisible_text_p (pos, w))
3030 {
3031 --charpos;
3032 bytepos = 0;
3033 }
3034
3035 /* Keep in mind: the call to reseat in init_iterator skips invisible
3036 text, so we might end up at a position different from POS. This
3037 is only a problem when POS is a row start after a newline and an
3038 overlay starts there with an after-string, and the overlay has an
3039 invisible property. Since we don't skip invisible text in
3040 display_line and elsewhere immediately after consuming the
3041 newline before the row start, such a POS will not be in a string,
3042 but the call to init_iterator below will move us to the
3043 after-string. */
3044 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3045
3046 /* This only scans the current chunk -- it should scan all chunks.
3047 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3048 to 16 in 22.1 to make this a lesser problem. */
3049 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3050 {
3051 const char *s = SSDATA (it->overlay_strings[i]);
3052 const char *e = s + SBYTES (it->overlay_strings[i]);
3053
3054 while (s < e && *s != '\n')
3055 ++s;
3056
3057 if (s < e)
3058 {
3059 overlay_strings_with_newlines = 1;
3060 break;
3061 }
3062 }
3063
3064 /* If position is within an overlay string, set up IT to the right
3065 overlay string. */
3066 if (pos->overlay_string_index >= 0)
3067 {
3068 int relative_index;
3069
3070 /* If the first overlay string happens to have a `display'
3071 property for an image, the iterator will be set up for that
3072 image, and we have to undo that setup first before we can
3073 correct the overlay string index. */
3074 if (it->method == GET_FROM_IMAGE)
3075 pop_it (it);
3076
3077 /* We already have the first chunk of overlay strings in
3078 IT->overlay_strings. Load more until the one for
3079 pos->overlay_string_index is in IT->overlay_strings. */
3080 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3081 {
3082 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3083 it->current.overlay_string_index = 0;
3084 while (n--)
3085 {
3086 load_overlay_strings (it, 0);
3087 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3088 }
3089 }
3090
3091 it->current.overlay_string_index = pos->overlay_string_index;
3092 relative_index = (it->current.overlay_string_index
3093 % OVERLAY_STRING_CHUNK_SIZE);
3094 it->string = it->overlay_strings[relative_index];
3095 eassert (STRINGP (it->string));
3096 it->current.string_pos = pos->string_pos;
3097 it->method = GET_FROM_STRING;
3098 it->end_charpos = SCHARS (it->string);
3099 /* Set up the bidi iterator for this overlay string. */
3100 if (it->bidi_p)
3101 {
3102 it->bidi_it.string.lstring = it->string;
3103 it->bidi_it.string.s = NULL;
3104 it->bidi_it.string.schars = SCHARS (it->string);
3105 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3106 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3107 it->bidi_it.string.unibyte = !it->multibyte_p;
3108 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3109 FRAME_WINDOW_P (it->f), &it->bidi_it);
3110
3111 /* Synchronize the state of the bidi iterator with
3112 pos->string_pos. For any string position other than
3113 zero, this will be done automagically when we resume
3114 iteration over the string and get_visually_first_element
3115 is called. But if string_pos is zero, and the string is
3116 to be reordered for display, we need to resync manually,
3117 since it could be that the iteration state recorded in
3118 pos ended at string_pos of 0 moving backwards in string. */
3119 if (CHARPOS (pos->string_pos) == 0)
3120 {
3121 get_visually_first_element (it);
3122 if (IT_STRING_CHARPOS (*it) != 0)
3123 do {
3124 /* Paranoia. */
3125 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3126 bidi_move_to_visually_next (&it->bidi_it);
3127 } while (it->bidi_it.charpos != 0);
3128 }
3129 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3130 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3131 }
3132 }
3133
3134 if (CHARPOS (pos->string_pos) >= 0)
3135 {
3136 /* Recorded position is not in an overlay string, but in another
3137 string. This can only be a string from a `display' property.
3138 IT should already be filled with that string. */
3139 it->current.string_pos = pos->string_pos;
3140 eassert (STRINGP (it->string));
3141 if (it->bidi_p)
3142 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3143 FRAME_WINDOW_P (it->f), &it->bidi_it);
3144 }
3145
3146 /* Restore position in display vector translations, control
3147 character translations or ellipses. */
3148 if (pos->dpvec_index >= 0)
3149 {
3150 if (it->dpvec == NULL)
3151 get_next_display_element (it);
3152 eassert (it->dpvec && it->current.dpvec_index == 0);
3153 it->current.dpvec_index = pos->dpvec_index;
3154 }
3155
3156 CHECK_IT (it);
3157 return !overlay_strings_with_newlines;
3158 }
3159
3160
3161 /* Initialize IT for stepping through current_buffer in window W
3162 starting at ROW->start. */
3163
3164 static void
3165 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3166 {
3167 init_from_display_pos (it, w, &row->start);
3168 it->start = row->start;
3169 it->continuation_lines_width = row->continuation_lines_width;
3170 CHECK_IT (it);
3171 }
3172
3173
3174 /* Initialize IT for stepping through current_buffer in window W
3175 starting in the line following ROW, i.e. starting at ROW->end.
3176 Value is zero if there are overlay strings with newlines at ROW's
3177 end position. */
3178
3179 static int
3180 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3181 {
3182 int success = 0;
3183
3184 if (init_from_display_pos (it, w, &row->end))
3185 {
3186 if (row->continued_p)
3187 it->continuation_lines_width
3188 = row->continuation_lines_width + row->pixel_width;
3189 CHECK_IT (it);
3190 success = 1;
3191 }
3192
3193 return success;
3194 }
3195
3196
3197
3198 \f
3199 /***********************************************************************
3200 Text properties
3201 ***********************************************************************/
3202
3203 /* Called when IT reaches IT->stop_charpos. Handle text property and
3204 overlay changes. Set IT->stop_charpos to the next position where
3205 to stop. */
3206
3207 static void
3208 handle_stop (struct it *it)
3209 {
3210 enum prop_handled handled;
3211 int handle_overlay_change_p;
3212 struct props *p;
3213
3214 it->dpvec = NULL;
3215 it->current.dpvec_index = -1;
3216 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3217 it->ignore_overlay_strings_at_pos_p = 0;
3218 it->ellipsis_p = 0;
3219
3220 /* Use face of preceding text for ellipsis (if invisible) */
3221 if (it->selective_display_ellipsis_p)
3222 it->saved_face_id = it->face_id;
3223
3224 do
3225 {
3226 handled = HANDLED_NORMALLY;
3227
3228 /* Call text property handlers. */
3229 for (p = it_props; p->handler; ++p)
3230 {
3231 handled = p->handler (it);
3232
3233 if (handled == HANDLED_RECOMPUTE_PROPS)
3234 break;
3235 else if (handled == HANDLED_RETURN)
3236 {
3237 /* We still want to show before and after strings from
3238 overlays even if the actual buffer text is replaced. */
3239 if (!handle_overlay_change_p
3240 || it->sp > 1
3241 /* Don't call get_overlay_strings_1 if we already
3242 have overlay strings loaded, because doing so
3243 will load them again and push the iterator state
3244 onto the stack one more time, which is not
3245 expected by the rest of the code that processes
3246 overlay strings. */
3247 || (it->current.overlay_string_index < 0
3248 ? !get_overlay_strings_1 (it, 0, 0)
3249 : 0))
3250 {
3251 if (it->ellipsis_p)
3252 setup_for_ellipsis (it, 0);
3253 /* When handling a display spec, we might load an
3254 empty string. In that case, discard it here. We
3255 used to discard it in handle_single_display_spec,
3256 but that causes get_overlay_strings_1, above, to
3257 ignore overlay strings that we must check. */
3258 if (STRINGP (it->string) && !SCHARS (it->string))
3259 pop_it (it);
3260 return;
3261 }
3262 else if (STRINGP (it->string) && !SCHARS (it->string))
3263 pop_it (it);
3264 else
3265 {
3266 it->ignore_overlay_strings_at_pos_p = 1;
3267 it->string_from_display_prop_p = 0;
3268 it->from_disp_prop_p = 0;
3269 handle_overlay_change_p = 0;
3270 }
3271 handled = HANDLED_RECOMPUTE_PROPS;
3272 break;
3273 }
3274 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3275 handle_overlay_change_p = 0;
3276 }
3277
3278 if (handled != HANDLED_RECOMPUTE_PROPS)
3279 {
3280 /* Don't check for overlay strings below when set to deliver
3281 characters from a display vector. */
3282 if (it->method == GET_FROM_DISPLAY_VECTOR)
3283 handle_overlay_change_p = 0;
3284
3285 /* Handle overlay changes.
3286 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3287 if it finds overlays. */
3288 if (handle_overlay_change_p)
3289 handled = handle_overlay_change (it);
3290 }
3291
3292 if (it->ellipsis_p)
3293 {
3294 setup_for_ellipsis (it, 0);
3295 break;
3296 }
3297 }
3298 while (handled == HANDLED_RECOMPUTE_PROPS);
3299
3300 /* Determine where to stop next. */
3301 if (handled == HANDLED_NORMALLY)
3302 compute_stop_pos (it);
3303 }
3304
3305
3306 /* Compute IT->stop_charpos from text property and overlay change
3307 information for IT's current position. */
3308
3309 static void
3310 compute_stop_pos (struct it *it)
3311 {
3312 register INTERVAL iv, next_iv;
3313 Lisp_Object object, limit, position;
3314 ptrdiff_t charpos, bytepos;
3315
3316 if (STRINGP (it->string))
3317 {
3318 /* Strings are usually short, so don't limit the search for
3319 properties. */
3320 it->stop_charpos = it->end_charpos;
3321 object = it->string;
3322 limit = Qnil;
3323 charpos = IT_STRING_CHARPOS (*it);
3324 bytepos = IT_STRING_BYTEPOS (*it);
3325 }
3326 else
3327 {
3328 ptrdiff_t pos;
3329
3330 /* If end_charpos is out of range for some reason, such as a
3331 misbehaving display function, rationalize it (Bug#5984). */
3332 if (it->end_charpos > ZV)
3333 it->end_charpos = ZV;
3334 it->stop_charpos = it->end_charpos;
3335
3336 /* If next overlay change is in front of the current stop pos
3337 (which is IT->end_charpos), stop there. Note: value of
3338 next_overlay_change is point-max if no overlay change
3339 follows. */
3340 charpos = IT_CHARPOS (*it);
3341 bytepos = IT_BYTEPOS (*it);
3342 pos = next_overlay_change (charpos);
3343 if (pos < it->stop_charpos)
3344 it->stop_charpos = pos;
3345
3346 /* If showing the region, we have to stop at the region
3347 start or end because the face might change there. */
3348 if (it->region_beg_charpos > 0)
3349 {
3350 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3351 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3352 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3353 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3354 }
3355
3356 /* Set up variables for computing the stop position from text
3357 property changes. */
3358 XSETBUFFER (object, current_buffer);
3359 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3360 }
3361
3362 /* Get the interval containing IT's position. Value is a null
3363 interval if there isn't such an interval. */
3364 position = make_number (charpos);
3365 iv = validate_interval_range (object, &position, &position, 0);
3366 if (iv)
3367 {
3368 Lisp_Object values_here[LAST_PROP_IDX];
3369 struct props *p;
3370
3371 /* Get properties here. */
3372 for (p = it_props; p->handler; ++p)
3373 values_here[p->idx] = textget (iv->plist, *p->name);
3374
3375 /* Look for an interval following iv that has different
3376 properties. */
3377 for (next_iv = next_interval (iv);
3378 (next_iv
3379 && (NILP (limit)
3380 || XFASTINT (limit) > next_iv->position));
3381 next_iv = next_interval (next_iv))
3382 {
3383 for (p = it_props; p->handler; ++p)
3384 {
3385 Lisp_Object new_value;
3386
3387 new_value = textget (next_iv->plist, *p->name);
3388 if (!EQ (values_here[p->idx], new_value))
3389 break;
3390 }
3391
3392 if (p->handler)
3393 break;
3394 }
3395
3396 if (next_iv)
3397 {
3398 if (INTEGERP (limit)
3399 && next_iv->position >= XFASTINT (limit))
3400 /* No text property change up to limit. */
3401 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3402 else
3403 /* Text properties change in next_iv. */
3404 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3405 }
3406 }
3407
3408 if (it->cmp_it.id < 0)
3409 {
3410 ptrdiff_t stoppos = it->end_charpos;
3411
3412 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3413 stoppos = -1;
3414 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3415 stoppos, it->string);
3416 }
3417
3418 eassert (STRINGP (it->string)
3419 || (it->stop_charpos >= BEGV
3420 && it->stop_charpos >= IT_CHARPOS (*it)));
3421 }
3422
3423
3424 /* Return the position of the next overlay change after POS in
3425 current_buffer. Value is point-max if no overlay change
3426 follows. This is like `next-overlay-change' but doesn't use
3427 xmalloc. */
3428
3429 static ptrdiff_t
3430 next_overlay_change (ptrdiff_t pos)
3431 {
3432 ptrdiff_t i, noverlays;
3433 ptrdiff_t endpos;
3434 Lisp_Object *overlays;
3435
3436 /* Get all overlays at the given position. */
3437 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3438
3439 /* If any of these overlays ends before endpos,
3440 use its ending point instead. */
3441 for (i = 0; i < noverlays; ++i)
3442 {
3443 Lisp_Object oend;
3444 ptrdiff_t oendpos;
3445
3446 oend = OVERLAY_END (overlays[i]);
3447 oendpos = OVERLAY_POSITION (oend);
3448 endpos = min (endpos, oendpos);
3449 }
3450
3451 return endpos;
3452 }
3453
3454 /* How many characters forward to search for a display property or
3455 display string. Searching too far forward makes the bidi display
3456 sluggish, especially in small windows. */
3457 #define MAX_DISP_SCAN 250
3458
3459 /* Return the character position of a display string at or after
3460 position specified by POSITION. If no display string exists at or
3461 after POSITION, return ZV. A display string is either an overlay
3462 with `display' property whose value is a string, or a `display'
3463 text property whose value is a string. STRING is data about the
3464 string to iterate; if STRING->lstring is nil, we are iterating a
3465 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3466 on a GUI frame. DISP_PROP is set to zero if we searched
3467 MAX_DISP_SCAN characters forward without finding any display
3468 strings, non-zero otherwise. It is set to 2 if the display string
3469 uses any kind of `(space ...)' spec that will produce a stretch of
3470 white space in the text area. */
3471 ptrdiff_t
3472 compute_display_string_pos (struct text_pos *position,
3473 struct bidi_string_data *string,
3474 int frame_window_p, int *disp_prop)
3475 {
3476 /* OBJECT = nil means current buffer. */
3477 Lisp_Object object =
3478 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3479 Lisp_Object pos, spec, limpos;
3480 int string_p = (string && (STRINGP (string->lstring) || string->s));
3481 ptrdiff_t eob = string_p ? string->schars : ZV;
3482 ptrdiff_t begb = string_p ? 0 : BEGV;
3483 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3484 ptrdiff_t lim =
3485 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3486 struct text_pos tpos;
3487 int rv = 0;
3488
3489 *disp_prop = 1;
3490
3491 if (charpos >= eob
3492 /* We don't support display properties whose values are strings
3493 that have display string properties. */
3494 || string->from_disp_str
3495 /* C strings cannot have display properties. */
3496 || (string->s && !STRINGP (object)))
3497 {
3498 *disp_prop = 0;
3499 return eob;
3500 }
3501
3502 /* If the character at CHARPOS is where the display string begins,
3503 return CHARPOS. */
3504 pos = make_number (charpos);
3505 if (STRINGP (object))
3506 bufpos = string->bufpos;
3507 else
3508 bufpos = charpos;
3509 tpos = *position;
3510 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3511 && (charpos <= begb
3512 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3513 object),
3514 spec))
3515 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3516 frame_window_p)))
3517 {
3518 if (rv == 2)
3519 *disp_prop = 2;
3520 return charpos;
3521 }
3522
3523 /* Look forward for the first character with a `display' property
3524 that will replace the underlying text when displayed. */
3525 limpos = make_number (lim);
3526 do {
3527 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3528 CHARPOS (tpos) = XFASTINT (pos);
3529 if (CHARPOS (tpos) >= lim)
3530 {
3531 *disp_prop = 0;
3532 break;
3533 }
3534 if (STRINGP (object))
3535 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3536 else
3537 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3538 spec = Fget_char_property (pos, Qdisplay, object);
3539 if (!STRINGP (object))
3540 bufpos = CHARPOS (tpos);
3541 } while (NILP (spec)
3542 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3543 bufpos, frame_window_p)));
3544 if (rv == 2)
3545 *disp_prop = 2;
3546
3547 return CHARPOS (tpos);
3548 }
3549
3550 /* Return the character position of the end of the display string that
3551 started at CHARPOS. If there's no display string at CHARPOS,
3552 return -1. A display string is either an overlay with `display'
3553 property whose value is a string or a `display' text property whose
3554 value is a string. */
3555 ptrdiff_t
3556 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3557 {
3558 /* OBJECT = nil means current buffer. */
3559 Lisp_Object object =
3560 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3561 Lisp_Object pos = make_number (charpos);
3562 ptrdiff_t eob =
3563 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3564
3565 if (charpos >= eob || (string->s && !STRINGP (object)))
3566 return eob;
3567
3568 /* It could happen that the display property or overlay was removed
3569 since we found it in compute_display_string_pos above. One way
3570 this can happen is if JIT font-lock was called (through
3571 handle_fontified_prop), and jit-lock-functions remove text
3572 properties or overlays from the portion of buffer that includes
3573 CHARPOS. Muse mode is known to do that, for example. In this
3574 case, we return -1 to the caller, to signal that no display
3575 string is actually present at CHARPOS. See bidi_fetch_char for
3576 how this is handled.
3577
3578 An alternative would be to never look for display properties past
3579 it->stop_charpos. But neither compute_display_string_pos nor
3580 bidi_fetch_char that calls it know or care where the next
3581 stop_charpos is. */
3582 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3583 return -1;
3584
3585 /* Look forward for the first character where the `display' property
3586 changes. */
3587 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3588
3589 return XFASTINT (pos);
3590 }
3591
3592
3593 \f
3594 /***********************************************************************
3595 Fontification
3596 ***********************************************************************/
3597
3598 /* Handle changes in the `fontified' property of the current buffer by
3599 calling hook functions from Qfontification_functions to fontify
3600 regions of text. */
3601
3602 static enum prop_handled
3603 handle_fontified_prop (struct it *it)
3604 {
3605 Lisp_Object prop, pos;
3606 enum prop_handled handled = HANDLED_NORMALLY;
3607
3608 if (!NILP (Vmemory_full))
3609 return handled;
3610
3611 /* Get the value of the `fontified' property at IT's current buffer
3612 position. (The `fontified' property doesn't have a special
3613 meaning in strings.) If the value is nil, call functions from
3614 Qfontification_functions. */
3615 if (!STRINGP (it->string)
3616 && it->s == NULL
3617 && !NILP (Vfontification_functions)
3618 && !NILP (Vrun_hooks)
3619 && (pos = make_number (IT_CHARPOS (*it)),
3620 prop = Fget_char_property (pos, Qfontified, Qnil),
3621 /* Ignore the special cased nil value always present at EOB since
3622 no amount of fontifying will be able to change it. */
3623 NILP (prop) && IT_CHARPOS (*it) < Z))
3624 {
3625 ptrdiff_t count = SPECPDL_INDEX ();
3626 Lisp_Object val;
3627 struct buffer *obuf = current_buffer;
3628 int begv = BEGV, zv = ZV;
3629 int old_clip_changed = current_buffer->clip_changed;
3630
3631 val = Vfontification_functions;
3632 specbind (Qfontification_functions, Qnil);
3633
3634 eassert (it->end_charpos == ZV);
3635
3636 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3637 safe_call1 (val, pos);
3638 else
3639 {
3640 Lisp_Object fns, fn;
3641 struct gcpro gcpro1, gcpro2;
3642
3643 fns = Qnil;
3644 GCPRO2 (val, fns);
3645
3646 for (; CONSP (val); val = XCDR (val))
3647 {
3648 fn = XCAR (val);
3649
3650 if (EQ (fn, Qt))
3651 {
3652 /* A value of t indicates this hook has a local
3653 binding; it means to run the global binding too.
3654 In a global value, t should not occur. If it
3655 does, we must ignore it to avoid an endless
3656 loop. */
3657 for (fns = Fdefault_value (Qfontification_functions);
3658 CONSP (fns);
3659 fns = XCDR (fns))
3660 {
3661 fn = XCAR (fns);
3662 if (!EQ (fn, Qt))
3663 safe_call1 (fn, pos);
3664 }
3665 }
3666 else
3667 safe_call1 (fn, pos);
3668 }
3669
3670 UNGCPRO;
3671 }
3672
3673 unbind_to (count, Qnil);
3674
3675 /* Fontification functions routinely call `save-restriction'.
3676 Normally, this tags clip_changed, which can confuse redisplay
3677 (see discussion in Bug#6671). Since we don't perform any
3678 special handling of fontification changes in the case where
3679 `save-restriction' isn't called, there's no point doing so in
3680 this case either. So, if the buffer's restrictions are
3681 actually left unchanged, reset clip_changed. */
3682 if (obuf == current_buffer)
3683 {
3684 if (begv == BEGV && zv == ZV)
3685 current_buffer->clip_changed = old_clip_changed;
3686 }
3687 /* There isn't much we can reasonably do to protect against
3688 misbehaving fontification, but here's a fig leaf. */
3689 else if (BUFFER_LIVE_P (obuf))
3690 set_buffer_internal_1 (obuf);
3691
3692 /* The fontification code may have added/removed text.
3693 It could do even a lot worse, but let's at least protect against
3694 the most obvious case where only the text past `pos' gets changed',
3695 as is/was done in grep.el where some escapes sequences are turned
3696 into face properties (bug#7876). */
3697 it->end_charpos = ZV;
3698
3699 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3700 something. This avoids an endless loop if they failed to
3701 fontify the text for which reason ever. */
3702 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3703 handled = HANDLED_RECOMPUTE_PROPS;
3704 }
3705
3706 return handled;
3707 }
3708
3709
3710 \f
3711 /***********************************************************************
3712 Faces
3713 ***********************************************************************/
3714
3715 /* Set up iterator IT from face properties at its current position.
3716 Called from handle_stop. */
3717
3718 static enum prop_handled
3719 handle_face_prop (struct it *it)
3720 {
3721 int new_face_id;
3722 ptrdiff_t next_stop;
3723
3724 if (!STRINGP (it->string))
3725 {
3726 new_face_id
3727 = face_at_buffer_position (it->w,
3728 IT_CHARPOS (*it),
3729 it->region_beg_charpos,
3730 it->region_end_charpos,
3731 &next_stop,
3732 (IT_CHARPOS (*it)
3733 + TEXT_PROP_DISTANCE_LIMIT),
3734 0, it->base_face_id);
3735
3736 /* Is this a start of a run of characters with box face?
3737 Caveat: this can be called for a freshly initialized
3738 iterator; face_id is -1 in this case. We know that the new
3739 face will not change until limit, i.e. if the new face has a
3740 box, all characters up to limit will have one. But, as
3741 usual, we don't know whether limit is really the end. */
3742 if (new_face_id != it->face_id)
3743 {
3744 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3745 /* If it->face_id is -1, old_face below will be NULL, see
3746 the definition of FACE_FROM_ID. This will happen if this
3747 is the initial call that gets the face. */
3748 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3749
3750 /* If the value of face_id of the iterator is -1, we have to
3751 look in front of IT's position and see whether there is a
3752 face there that's different from new_face_id. */
3753 if (!old_face && IT_CHARPOS (*it) > BEG)
3754 {
3755 int prev_face_id = face_before_it_pos (it);
3756
3757 old_face = FACE_FROM_ID (it->f, prev_face_id);
3758 }
3759
3760 /* If the new face has a box, but the old face does not,
3761 this is the start of a run of characters with box face,
3762 i.e. this character has a shadow on the left side. */
3763 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3764 && (old_face == NULL || !old_face->box));
3765 it->face_box_p = new_face->box != FACE_NO_BOX;
3766 }
3767 }
3768 else
3769 {
3770 int base_face_id;
3771 ptrdiff_t bufpos;
3772 int i;
3773 Lisp_Object from_overlay
3774 = (it->current.overlay_string_index >= 0
3775 ? it->string_overlays[it->current.overlay_string_index
3776 % OVERLAY_STRING_CHUNK_SIZE]
3777 : Qnil);
3778
3779 /* See if we got to this string directly or indirectly from
3780 an overlay property. That includes the before-string or
3781 after-string of an overlay, strings in display properties
3782 provided by an overlay, their text properties, etc.
3783
3784 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3785 if (! NILP (from_overlay))
3786 for (i = it->sp - 1; i >= 0; i--)
3787 {
3788 if (it->stack[i].current.overlay_string_index >= 0)
3789 from_overlay
3790 = it->string_overlays[it->stack[i].current.overlay_string_index
3791 % OVERLAY_STRING_CHUNK_SIZE];
3792 else if (! NILP (it->stack[i].from_overlay))
3793 from_overlay = it->stack[i].from_overlay;
3794
3795 if (!NILP (from_overlay))
3796 break;
3797 }
3798
3799 if (! NILP (from_overlay))
3800 {
3801 bufpos = IT_CHARPOS (*it);
3802 /* For a string from an overlay, the base face depends
3803 only on text properties and ignores overlays. */
3804 base_face_id
3805 = face_for_overlay_string (it->w,
3806 IT_CHARPOS (*it),
3807 it->region_beg_charpos,
3808 it->region_end_charpos,
3809 &next_stop,
3810 (IT_CHARPOS (*it)
3811 + TEXT_PROP_DISTANCE_LIMIT),
3812 0,
3813 from_overlay);
3814 }
3815 else
3816 {
3817 bufpos = 0;
3818
3819 /* For strings from a `display' property, use the face at
3820 IT's current buffer position as the base face to merge
3821 with, so that overlay strings appear in the same face as
3822 surrounding text, unless they specify their own
3823 faces. */
3824 base_face_id = it->string_from_prefix_prop_p
3825 ? DEFAULT_FACE_ID
3826 : underlying_face_id (it);
3827 }
3828
3829 new_face_id = face_at_string_position (it->w,
3830 it->string,
3831 IT_STRING_CHARPOS (*it),
3832 bufpos,
3833 it->region_beg_charpos,
3834 it->region_end_charpos,
3835 &next_stop,
3836 base_face_id, 0);
3837
3838 /* Is this a start of a run of characters with box? Caveat:
3839 this can be called for a freshly allocated iterator; face_id
3840 is -1 is this case. We know that the new face will not
3841 change until the next check pos, i.e. if the new face has a
3842 box, all characters up to that position will have a
3843 box. But, as usual, we don't know whether that position
3844 is really the end. */
3845 if (new_face_id != it->face_id)
3846 {
3847 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3848 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3849
3850 /* If new face has a box but old face hasn't, this is the
3851 start of a run of characters with box, i.e. it has a
3852 shadow on the left side. */
3853 it->start_of_box_run_p
3854 = new_face->box && (old_face == NULL || !old_face->box);
3855 it->face_box_p = new_face->box != FACE_NO_BOX;
3856 }
3857 }
3858
3859 it->face_id = new_face_id;
3860 return HANDLED_NORMALLY;
3861 }
3862
3863
3864 /* Return the ID of the face ``underlying'' IT's current position,
3865 which is in a string. If the iterator is associated with a
3866 buffer, return the face at IT's current buffer position.
3867 Otherwise, use the iterator's base_face_id. */
3868
3869 static int
3870 underlying_face_id (struct it *it)
3871 {
3872 int face_id = it->base_face_id, i;
3873
3874 eassert (STRINGP (it->string));
3875
3876 for (i = it->sp - 1; i >= 0; --i)
3877 if (NILP (it->stack[i].string))
3878 face_id = it->stack[i].face_id;
3879
3880 return face_id;
3881 }
3882
3883
3884 /* Compute the face one character before or after the current position
3885 of IT, in the visual order. BEFORE_P non-zero means get the face
3886 in front (to the left in L2R paragraphs, to the right in R2L
3887 paragraphs) of IT's screen position. Value is the ID of the face. */
3888
3889 static int
3890 face_before_or_after_it_pos (struct it *it, int before_p)
3891 {
3892 int face_id, limit;
3893 ptrdiff_t next_check_charpos;
3894 struct it it_copy;
3895 void *it_copy_data = NULL;
3896
3897 eassert (it->s == NULL);
3898
3899 if (STRINGP (it->string))
3900 {
3901 ptrdiff_t bufpos, charpos;
3902 int base_face_id;
3903
3904 /* No face change past the end of the string (for the case
3905 we are padding with spaces). No face change before the
3906 string start. */
3907 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3908 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3909 return it->face_id;
3910
3911 if (!it->bidi_p)
3912 {
3913 /* Set charpos to the position before or after IT's current
3914 position, in the logical order, which in the non-bidi
3915 case is the same as the visual order. */
3916 if (before_p)
3917 charpos = IT_STRING_CHARPOS (*it) - 1;
3918 else if (it->what == IT_COMPOSITION)
3919 /* For composition, we must check the character after the
3920 composition. */
3921 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3922 else
3923 charpos = IT_STRING_CHARPOS (*it) + 1;
3924 }
3925 else
3926 {
3927 if (before_p)
3928 {
3929 /* With bidi iteration, the character before the current
3930 in the visual order cannot be found by simple
3931 iteration, because "reverse" reordering is not
3932 supported. Instead, we need to use the move_it_*
3933 family of functions. */
3934 /* Ignore face changes before the first visible
3935 character on this display line. */
3936 if (it->current_x <= it->first_visible_x)
3937 return it->face_id;
3938 SAVE_IT (it_copy, *it, it_copy_data);
3939 /* Implementation note: Since move_it_in_display_line
3940 works in the iterator geometry, and thinks the first
3941 character is always the leftmost, even in R2L lines,
3942 we don't need to distinguish between the R2L and L2R
3943 cases here. */
3944 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3945 it_copy.current_x - 1, MOVE_TO_X);
3946 charpos = IT_STRING_CHARPOS (it_copy);
3947 RESTORE_IT (it, it, it_copy_data);
3948 }
3949 else
3950 {
3951 /* Set charpos to the string position of the character
3952 that comes after IT's current position in the visual
3953 order. */
3954 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3955
3956 it_copy = *it;
3957 while (n--)
3958 bidi_move_to_visually_next (&it_copy.bidi_it);
3959
3960 charpos = it_copy.bidi_it.charpos;
3961 }
3962 }
3963 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3964
3965 if (it->current.overlay_string_index >= 0)
3966 bufpos = IT_CHARPOS (*it);
3967 else
3968 bufpos = 0;
3969
3970 base_face_id = underlying_face_id (it);
3971
3972 /* Get the face for ASCII, or unibyte. */
3973 face_id = face_at_string_position (it->w,
3974 it->string,
3975 charpos,
3976 bufpos,
3977 it->region_beg_charpos,
3978 it->region_end_charpos,
3979 &next_check_charpos,
3980 base_face_id, 0);
3981
3982 /* Correct the face for charsets different from ASCII. Do it
3983 for the multibyte case only. The face returned above is
3984 suitable for unibyte text if IT->string is unibyte. */
3985 if (STRING_MULTIBYTE (it->string))
3986 {
3987 struct text_pos pos1 = string_pos (charpos, it->string);
3988 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3989 int c, len;
3990 struct face *face = FACE_FROM_ID (it->f, face_id);
3991
3992 c = string_char_and_length (p, &len);
3993 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3994 }
3995 }
3996 else
3997 {
3998 struct text_pos pos;
3999
4000 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4001 || (IT_CHARPOS (*it) <= BEGV && before_p))
4002 return it->face_id;
4003
4004 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4005 pos = it->current.pos;
4006
4007 if (!it->bidi_p)
4008 {
4009 if (before_p)
4010 DEC_TEXT_POS (pos, it->multibyte_p);
4011 else
4012 {
4013 if (it->what == IT_COMPOSITION)
4014 {
4015 /* For composition, we must check the position after
4016 the composition. */
4017 pos.charpos += it->cmp_it.nchars;
4018 pos.bytepos += it->len;
4019 }
4020 else
4021 INC_TEXT_POS (pos, it->multibyte_p);
4022 }
4023 }
4024 else
4025 {
4026 if (before_p)
4027 {
4028 /* With bidi iteration, the character before the current
4029 in the visual order cannot be found by simple
4030 iteration, because "reverse" reordering is not
4031 supported. Instead, we need to use the move_it_*
4032 family of functions. */
4033 /* Ignore face changes before the first visible
4034 character on this display line. */
4035 if (it->current_x <= it->first_visible_x)
4036 return it->face_id;
4037 SAVE_IT (it_copy, *it, it_copy_data);
4038 /* Implementation note: Since move_it_in_display_line
4039 works in the iterator geometry, and thinks the first
4040 character is always the leftmost, even in R2L lines,
4041 we don't need to distinguish between the R2L and L2R
4042 cases here. */
4043 move_it_in_display_line (&it_copy, ZV,
4044 it_copy.current_x - 1, MOVE_TO_X);
4045 pos = it_copy.current.pos;
4046 RESTORE_IT (it, it, it_copy_data);
4047 }
4048 else
4049 {
4050 /* Set charpos to the buffer position of the character
4051 that comes after IT's current position in the visual
4052 order. */
4053 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4054
4055 it_copy = *it;
4056 while (n--)
4057 bidi_move_to_visually_next (&it_copy.bidi_it);
4058
4059 SET_TEXT_POS (pos,
4060 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4061 }
4062 }
4063 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4064
4065 /* Determine face for CHARSET_ASCII, or unibyte. */
4066 face_id = face_at_buffer_position (it->w,
4067 CHARPOS (pos),
4068 it->region_beg_charpos,
4069 it->region_end_charpos,
4070 &next_check_charpos,
4071 limit, 0, -1);
4072
4073 /* Correct the face for charsets different from ASCII. Do it
4074 for the multibyte case only. The face returned above is
4075 suitable for unibyte text if current_buffer is unibyte. */
4076 if (it->multibyte_p)
4077 {
4078 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4079 struct face *face = FACE_FROM_ID (it->f, face_id);
4080 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4081 }
4082 }
4083
4084 return face_id;
4085 }
4086
4087
4088 \f
4089 /***********************************************************************
4090 Invisible text
4091 ***********************************************************************/
4092
4093 /* Set up iterator IT from invisible properties at its current
4094 position. Called from handle_stop. */
4095
4096 static enum prop_handled
4097 handle_invisible_prop (struct it *it)
4098 {
4099 enum prop_handled handled = HANDLED_NORMALLY;
4100 int invis_p;
4101 Lisp_Object prop;
4102
4103 if (STRINGP (it->string))
4104 {
4105 Lisp_Object end_charpos, limit, charpos;
4106
4107 /* Get the value of the invisible text property at the
4108 current position. Value will be nil if there is no such
4109 property. */
4110 charpos = make_number (IT_STRING_CHARPOS (*it));
4111 prop = Fget_text_property (charpos, Qinvisible, it->string);
4112 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4113
4114 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4115 {
4116 /* Record whether we have to display an ellipsis for the
4117 invisible text. */
4118 int display_ellipsis_p = (invis_p == 2);
4119 ptrdiff_t len, endpos;
4120
4121 handled = HANDLED_RECOMPUTE_PROPS;
4122
4123 /* Get the position at which the next visible text can be
4124 found in IT->string, if any. */
4125 endpos = len = SCHARS (it->string);
4126 XSETINT (limit, len);
4127 do
4128 {
4129 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4130 it->string, limit);
4131 if (INTEGERP (end_charpos))
4132 {
4133 endpos = XFASTINT (end_charpos);
4134 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4135 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 if (invis_p == 2)
4137 display_ellipsis_p = 1;
4138 }
4139 }
4140 while (invis_p && endpos < len);
4141
4142 if (display_ellipsis_p)
4143 it->ellipsis_p = 1;
4144
4145 if (endpos < len)
4146 {
4147 /* Text at END_CHARPOS is visible. Move IT there. */
4148 struct text_pos old;
4149 ptrdiff_t oldpos;
4150
4151 old = it->current.string_pos;
4152 oldpos = CHARPOS (old);
4153 if (it->bidi_p)
4154 {
4155 if (it->bidi_it.first_elt
4156 && it->bidi_it.charpos < SCHARS (it->string))
4157 bidi_paragraph_init (it->paragraph_embedding,
4158 &it->bidi_it, 1);
4159 /* Bidi-iterate out of the invisible text. */
4160 do
4161 {
4162 bidi_move_to_visually_next (&it->bidi_it);
4163 }
4164 while (oldpos <= it->bidi_it.charpos
4165 && it->bidi_it.charpos < endpos);
4166
4167 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4168 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4169 if (IT_CHARPOS (*it) >= endpos)
4170 it->prev_stop = endpos;
4171 }
4172 else
4173 {
4174 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4175 compute_string_pos (&it->current.string_pos, old, it->string);
4176 }
4177 }
4178 else
4179 {
4180 /* The rest of the string is invisible. If this is an
4181 overlay string, proceed with the next overlay string
4182 or whatever comes and return a character from there. */
4183 if (it->current.overlay_string_index >= 0
4184 && !display_ellipsis_p)
4185 {
4186 next_overlay_string (it);
4187 /* Don't check for overlay strings when we just
4188 finished processing them. */
4189 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4190 }
4191 else
4192 {
4193 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4194 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4195 }
4196 }
4197 }
4198 }
4199 else
4200 {
4201 ptrdiff_t newpos, next_stop, start_charpos, tem;
4202 Lisp_Object pos, overlay;
4203
4204 /* First of all, is there invisible text at this position? */
4205 tem = start_charpos = IT_CHARPOS (*it);
4206 pos = make_number (tem);
4207 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4208 &overlay);
4209 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4210
4211 /* If we are on invisible text, skip over it. */
4212 if (invis_p && start_charpos < it->end_charpos)
4213 {
4214 /* Record whether we have to display an ellipsis for the
4215 invisible text. */
4216 int display_ellipsis_p = invis_p == 2;
4217
4218 handled = HANDLED_RECOMPUTE_PROPS;
4219
4220 /* Loop skipping over invisible text. The loop is left at
4221 ZV or with IT on the first char being visible again. */
4222 do
4223 {
4224 /* Try to skip some invisible text. Return value is the
4225 position reached which can be equal to where we start
4226 if there is nothing invisible there. This skips both
4227 over invisible text properties and overlays with
4228 invisible property. */
4229 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4230
4231 /* If we skipped nothing at all we weren't at invisible
4232 text in the first place. If everything to the end of
4233 the buffer was skipped, end the loop. */
4234 if (newpos == tem || newpos >= ZV)
4235 invis_p = 0;
4236 else
4237 {
4238 /* We skipped some characters but not necessarily
4239 all there are. Check if we ended up on visible
4240 text. Fget_char_property returns the property of
4241 the char before the given position, i.e. if we
4242 get invis_p = 0, this means that the char at
4243 newpos is visible. */
4244 pos = make_number (newpos);
4245 prop = Fget_char_property (pos, Qinvisible, it->window);
4246 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4247 }
4248
4249 /* If we ended up on invisible text, proceed to
4250 skip starting with next_stop. */
4251 if (invis_p)
4252 tem = next_stop;
4253
4254 /* If there are adjacent invisible texts, don't lose the
4255 second one's ellipsis. */
4256 if (invis_p == 2)
4257 display_ellipsis_p = 1;
4258 }
4259 while (invis_p);
4260
4261 /* The position newpos is now either ZV or on visible text. */
4262 if (it->bidi_p)
4263 {
4264 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4265 int on_newline =
4266 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4267 int after_newline =
4268 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4269
4270 /* If the invisible text ends on a newline or on a
4271 character after a newline, we can avoid the costly,
4272 character by character, bidi iteration to NEWPOS, and
4273 instead simply reseat the iterator there. That's
4274 because all bidi reordering information is tossed at
4275 the newline. This is a big win for modes that hide
4276 complete lines, like Outline, Org, etc. */
4277 if (on_newline || after_newline)
4278 {
4279 struct text_pos tpos;
4280 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4281
4282 SET_TEXT_POS (tpos, newpos, bpos);
4283 reseat_1 (it, tpos, 0);
4284 /* If we reseat on a newline/ZV, we need to prep the
4285 bidi iterator for advancing to the next character
4286 after the newline/EOB, keeping the current paragraph
4287 direction (so that PRODUCE_GLYPHS does TRT wrt
4288 prepending/appending glyphs to a glyph row). */
4289 if (on_newline)
4290 {
4291 it->bidi_it.first_elt = 0;
4292 it->bidi_it.paragraph_dir = pdir;
4293 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4294 it->bidi_it.nchars = 1;
4295 it->bidi_it.ch_len = 1;
4296 }
4297 }
4298 else /* Must use the slow method. */
4299 {
4300 /* With bidi iteration, the region of invisible text
4301 could start and/or end in the middle of a
4302 non-base embedding level. Therefore, we need to
4303 skip invisible text using the bidi iterator,
4304 starting at IT's current position, until we find
4305 ourselves outside of the invisible text.
4306 Skipping invisible text _after_ bidi iteration
4307 avoids affecting the visual order of the
4308 displayed text when invisible properties are
4309 added or removed. */
4310 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4311 {
4312 /* If we were `reseat'ed to a new paragraph,
4313 determine the paragraph base direction. We
4314 need to do it now because
4315 next_element_from_buffer may not have a
4316 chance to do it, if we are going to skip any
4317 text at the beginning, which resets the
4318 FIRST_ELT flag. */
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, 1);
4321 }
4322 do
4323 {
4324 bidi_move_to_visually_next (&it->bidi_it);
4325 }
4326 while (it->stop_charpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < newpos);
4328 IT_CHARPOS (*it) = it->bidi_it.charpos;
4329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4330 /* If we overstepped NEWPOS, record its position in
4331 the iterator, so that we skip invisible text if
4332 later the bidi iteration lands us in the
4333 invisible region again. */
4334 if (IT_CHARPOS (*it) >= newpos)
4335 it->prev_stop = newpos;
4336 }
4337 }
4338 else
4339 {
4340 IT_CHARPOS (*it) = newpos;
4341 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4342 }
4343
4344 /* If there are before-strings at the start of invisible
4345 text, and the text is invisible because of a text
4346 property, arrange to show before-strings because 20.x did
4347 it that way. (If the text is invisible because of an
4348 overlay property instead of a text property, this is
4349 already handled in the overlay code.) */
4350 if (NILP (overlay)
4351 && get_overlay_strings (it, it->stop_charpos))
4352 {
4353 handled = HANDLED_RECOMPUTE_PROPS;
4354 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4355 }
4356 else if (display_ellipsis_p)
4357 {
4358 /* Make sure that the glyphs of the ellipsis will get
4359 correct `charpos' values. If we would not update
4360 it->position here, the glyphs would belong to the
4361 last visible character _before_ the invisible
4362 text, which confuses `set_cursor_from_row'.
4363
4364 We use the last invisible position instead of the
4365 first because this way the cursor is always drawn on
4366 the first "." of the ellipsis, whenever PT is inside
4367 the invisible text. Otherwise the cursor would be
4368 placed _after_ the ellipsis when the point is after the
4369 first invisible character. */
4370 if (!STRINGP (it->object))
4371 {
4372 it->position.charpos = newpos - 1;
4373 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4374 }
4375 it->ellipsis_p = 1;
4376 /* Let the ellipsis display before
4377 considering any properties of the following char.
4378 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4379 handled = HANDLED_RETURN;
4380 }
4381 }
4382 }
4383
4384 return handled;
4385 }
4386
4387
4388 /* Make iterator IT return `...' next.
4389 Replaces LEN characters from buffer. */
4390
4391 static void
4392 setup_for_ellipsis (struct it *it, int len)
4393 {
4394 /* Use the display table definition for `...'. Invalid glyphs
4395 will be handled by the method returning elements from dpvec. */
4396 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4397 {
4398 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4399 it->dpvec = v->contents;
4400 it->dpend = v->contents + v->header.size;
4401 }
4402 else
4403 {
4404 /* Default `...'. */
4405 it->dpvec = default_invis_vector;
4406 it->dpend = default_invis_vector + 3;
4407 }
4408
4409 it->dpvec_char_len = len;
4410 it->current.dpvec_index = 0;
4411 it->dpvec_face_id = -1;
4412
4413 /* Remember the current face id in case glyphs specify faces.
4414 IT's face is restored in set_iterator_to_next.
4415 saved_face_id was set to preceding char's face in handle_stop. */
4416 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4417 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4418
4419 it->method = GET_FROM_DISPLAY_VECTOR;
4420 it->ellipsis_p = 1;
4421 }
4422
4423
4424 \f
4425 /***********************************************************************
4426 'display' property
4427 ***********************************************************************/
4428
4429 /* Set up iterator IT from `display' property at its current position.
4430 Called from handle_stop.
4431 We return HANDLED_RETURN if some part of the display property
4432 overrides the display of the buffer text itself.
4433 Otherwise we return HANDLED_NORMALLY. */
4434
4435 static enum prop_handled
4436 handle_display_prop (struct it *it)
4437 {
4438 Lisp_Object propval, object, overlay;
4439 struct text_pos *position;
4440 ptrdiff_t bufpos;
4441 /* Nonzero if some property replaces the display of the text itself. */
4442 int display_replaced_p = 0;
4443
4444 if (STRINGP (it->string))
4445 {
4446 object = it->string;
4447 position = &it->current.string_pos;
4448 bufpos = CHARPOS (it->current.pos);
4449 }
4450 else
4451 {
4452 XSETWINDOW (object, it->w);
4453 position = &it->current.pos;
4454 bufpos = CHARPOS (*position);
4455 }
4456
4457 /* Reset those iterator values set from display property values. */
4458 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4459 it->space_width = Qnil;
4460 it->font_height = Qnil;
4461 it->voffset = 0;
4462
4463 /* We don't support recursive `display' properties, i.e. string
4464 values that have a string `display' property, that have a string
4465 `display' property etc. */
4466 if (!it->string_from_display_prop_p)
4467 it->area = TEXT_AREA;
4468
4469 propval = get_char_property_and_overlay (make_number (position->charpos),
4470 Qdisplay, object, &overlay);
4471 if (NILP (propval))
4472 return HANDLED_NORMALLY;
4473 /* Now OVERLAY is the overlay that gave us this property, or nil
4474 if it was a text property. */
4475
4476 if (!STRINGP (it->string))
4477 object = it->w->buffer;
4478
4479 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4480 position, bufpos,
4481 FRAME_WINDOW_P (it->f));
4482
4483 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4484 }
4485
4486 /* Subroutine of handle_display_prop. Returns non-zero if the display
4487 specification in SPEC is a replacing specification, i.e. it would
4488 replace the text covered by `display' property with something else,
4489 such as an image or a display string. If SPEC includes any kind or
4490 `(space ...) specification, the value is 2; this is used by
4491 compute_display_string_pos, which see.
4492
4493 See handle_single_display_spec for documentation of arguments.
4494 frame_window_p is non-zero if the window being redisplayed is on a
4495 GUI frame; this argument is used only if IT is NULL, see below.
4496
4497 IT can be NULL, if this is called by the bidi reordering code
4498 through compute_display_string_pos, which see. In that case, this
4499 function only examines SPEC, but does not otherwise "handle" it, in
4500 the sense that it doesn't set up members of IT from the display
4501 spec. */
4502 static int
4503 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4504 Lisp_Object overlay, struct text_pos *position,
4505 ptrdiff_t bufpos, int frame_window_p)
4506 {
4507 int replacing_p = 0;
4508 int rv;
4509
4510 if (CONSP (spec)
4511 /* Simple specifications. */
4512 && !EQ (XCAR (spec), Qimage)
4513 && !EQ (XCAR (spec), Qspace)
4514 && !EQ (XCAR (spec), Qwhen)
4515 && !EQ (XCAR (spec), Qslice)
4516 && !EQ (XCAR (spec), Qspace_width)
4517 && !EQ (XCAR (spec), Qheight)
4518 && !EQ (XCAR (spec), Qraise)
4519 /* Marginal area specifications. */
4520 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4521 && !EQ (XCAR (spec), Qleft_fringe)
4522 && !EQ (XCAR (spec), Qright_fringe)
4523 && !NILP (XCAR (spec)))
4524 {
4525 for (; CONSP (spec); spec = XCDR (spec))
4526 {
4527 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4528 overlay, position, bufpos,
4529 replacing_p, frame_window_p)))
4530 {
4531 replacing_p = rv;
4532 /* If some text in a string is replaced, `position' no
4533 longer points to the position of `object'. */
4534 if (!it || STRINGP (object))
4535 break;
4536 }
4537 }
4538 }
4539 else if (VECTORP (spec))
4540 {
4541 ptrdiff_t i;
4542 for (i = 0; i < ASIZE (spec); ++i)
4543 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4544 overlay, position, bufpos,
4545 replacing_p, frame_window_p)))
4546 {
4547 replacing_p = rv;
4548 /* If some text in a string is replaced, `position' no
4549 longer points to the position of `object'. */
4550 if (!it || STRINGP (object))
4551 break;
4552 }
4553 }
4554 else
4555 {
4556 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4557 position, bufpos, 0,
4558 frame_window_p)))
4559 replacing_p = rv;
4560 }
4561
4562 return replacing_p;
4563 }
4564
4565 /* Value is the position of the end of the `display' property starting
4566 at START_POS in OBJECT. */
4567
4568 static struct text_pos
4569 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4570 {
4571 Lisp_Object end;
4572 struct text_pos end_pos;
4573
4574 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4575 Qdisplay, object, Qnil);
4576 CHARPOS (end_pos) = XFASTINT (end);
4577 if (STRINGP (object))
4578 compute_string_pos (&end_pos, start_pos, it->string);
4579 else
4580 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4581
4582 return end_pos;
4583 }
4584
4585
4586 /* Set up IT from a single `display' property specification SPEC. OBJECT
4587 is the object in which the `display' property was found. *POSITION
4588 is the position in OBJECT at which the `display' property was found.
4589 BUFPOS is the buffer position of OBJECT (different from POSITION if
4590 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4591 previously saw a display specification which already replaced text
4592 display with something else, for example an image; we ignore such
4593 properties after the first one has been processed.
4594
4595 OVERLAY is the overlay this `display' property came from,
4596 or nil if it was a text property.
4597
4598 If SPEC is a `space' or `image' specification, and in some other
4599 cases too, set *POSITION to the position where the `display'
4600 property ends.
4601
4602 If IT is NULL, only examine the property specification in SPEC, but
4603 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4604 is intended to be displayed in a window on a GUI frame.
4605
4606 Value is non-zero if something was found which replaces the display
4607 of buffer or string text. */
4608
4609 static int
4610 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4611 Lisp_Object overlay, struct text_pos *position,
4612 ptrdiff_t bufpos, int display_replaced_p,
4613 int frame_window_p)
4614 {
4615 Lisp_Object form;
4616 Lisp_Object location, value;
4617 struct text_pos start_pos = *position;
4618 int valid_p;
4619
4620 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4621 If the result is non-nil, use VALUE instead of SPEC. */
4622 form = Qt;
4623 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4624 {
4625 spec = XCDR (spec);
4626 if (!CONSP (spec))
4627 return 0;
4628 form = XCAR (spec);
4629 spec = XCDR (spec);
4630 }
4631
4632 if (!NILP (form) && !EQ (form, Qt))
4633 {
4634 ptrdiff_t count = SPECPDL_INDEX ();
4635 struct gcpro gcpro1;
4636
4637 /* Bind `object' to the object having the `display' property, a
4638 buffer or string. Bind `position' to the position in the
4639 object where the property was found, and `buffer-position'
4640 to the current position in the buffer. */
4641
4642 if (NILP (object))
4643 XSETBUFFER (object, current_buffer);
4644 specbind (Qobject, object);
4645 specbind (Qposition, make_number (CHARPOS (*position)));
4646 specbind (Qbuffer_position, make_number (bufpos));
4647 GCPRO1 (form);
4648 form = safe_eval (form);
4649 UNGCPRO;
4650 unbind_to (count, Qnil);
4651 }
4652
4653 if (NILP (form))
4654 return 0;
4655
4656 /* Handle `(height HEIGHT)' specifications. */
4657 if (CONSP (spec)
4658 && EQ (XCAR (spec), Qheight)
4659 && CONSP (XCDR (spec)))
4660 {
4661 if (it)
4662 {
4663 if (!FRAME_WINDOW_P (it->f))
4664 return 0;
4665
4666 it->font_height = XCAR (XCDR (spec));
4667 if (!NILP (it->font_height))
4668 {
4669 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4670 int new_height = -1;
4671
4672 if (CONSP (it->font_height)
4673 && (EQ (XCAR (it->font_height), Qplus)
4674 || EQ (XCAR (it->font_height), Qminus))
4675 && CONSP (XCDR (it->font_height))
4676 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4677 {
4678 /* `(+ N)' or `(- N)' where N is an integer. */
4679 int steps = XINT (XCAR (XCDR (it->font_height)));
4680 if (EQ (XCAR (it->font_height), Qplus))
4681 steps = - steps;
4682 it->face_id = smaller_face (it->f, it->face_id, steps);
4683 }
4684 else if (FUNCTIONP (it->font_height))
4685 {
4686 /* Call function with current height as argument.
4687 Value is the new height. */
4688 Lisp_Object height;
4689 height = safe_call1 (it->font_height,
4690 face->lface[LFACE_HEIGHT_INDEX]);
4691 if (NUMBERP (height))
4692 new_height = XFLOATINT (height);
4693 }
4694 else if (NUMBERP (it->font_height))
4695 {
4696 /* Value is a multiple of the canonical char height. */
4697 struct face *f;
4698
4699 f = FACE_FROM_ID (it->f,
4700 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4701 new_height = (XFLOATINT (it->font_height)
4702 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4703 }
4704 else
4705 {
4706 /* Evaluate IT->font_height with `height' bound to the
4707 current specified height to get the new height. */
4708 ptrdiff_t count = SPECPDL_INDEX ();
4709
4710 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4711 value = safe_eval (it->font_height);
4712 unbind_to (count, Qnil);
4713
4714 if (NUMBERP (value))
4715 new_height = XFLOATINT (value);
4716 }
4717
4718 if (new_height > 0)
4719 it->face_id = face_with_height (it->f, it->face_id, new_height);
4720 }
4721 }
4722
4723 return 0;
4724 }
4725
4726 /* Handle `(space-width WIDTH)'. */
4727 if (CONSP (spec)
4728 && EQ (XCAR (spec), Qspace_width)
4729 && CONSP (XCDR (spec)))
4730 {
4731 if (it)
4732 {
4733 if (!FRAME_WINDOW_P (it->f))
4734 return 0;
4735
4736 value = XCAR (XCDR (spec));
4737 if (NUMBERP (value) && XFLOATINT (value) > 0)
4738 it->space_width = value;
4739 }
4740
4741 return 0;
4742 }
4743
4744 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qslice))
4747 {
4748 Lisp_Object tem;
4749
4750 if (it)
4751 {
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4754
4755 if (tem = XCDR (spec), CONSP (tem))
4756 {
4757 it->slice.x = XCAR (tem);
4758 if (tem = XCDR (tem), CONSP (tem))
4759 {
4760 it->slice.y = XCAR (tem);
4761 if (tem = XCDR (tem), CONSP (tem))
4762 {
4763 it->slice.width = XCAR (tem);
4764 if (tem = XCDR (tem), CONSP (tem))
4765 it->slice.height = XCAR (tem);
4766 }
4767 }
4768 }
4769 }
4770
4771 return 0;
4772 }
4773
4774 /* Handle `(raise FACTOR)'. */
4775 if (CONSP (spec)
4776 && EQ (XCAR (spec), Qraise)
4777 && CONSP (XCDR (spec)))
4778 {
4779 if (it)
4780 {
4781 if (!FRAME_WINDOW_P (it->f))
4782 return 0;
4783
4784 #ifdef HAVE_WINDOW_SYSTEM
4785 value = XCAR (XCDR (spec));
4786 if (NUMBERP (value))
4787 {
4788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4789 it->voffset = - (XFLOATINT (value)
4790 * (FONT_HEIGHT (face->font)));
4791 }
4792 #endif /* HAVE_WINDOW_SYSTEM */
4793 }
4794
4795 return 0;
4796 }
4797
4798 /* Don't handle the other kinds of display specifications
4799 inside a string that we got from a `display' property. */
4800 if (it && it->string_from_display_prop_p)
4801 return 0;
4802
4803 /* Characters having this form of property are not displayed, so
4804 we have to find the end of the property. */
4805 if (it)
4806 {
4807 start_pos = *position;
4808 *position = display_prop_end (it, object, start_pos);
4809 }
4810 value = Qnil;
4811
4812 /* Stop the scan at that end position--we assume that all
4813 text properties change there. */
4814 if (it)
4815 it->stop_charpos = position->charpos;
4816
4817 /* Handle `(left-fringe BITMAP [FACE])'
4818 and `(right-fringe BITMAP [FACE])'. */
4819 if (CONSP (spec)
4820 && (EQ (XCAR (spec), Qleft_fringe)
4821 || EQ (XCAR (spec), Qright_fringe))
4822 && CONSP (XCDR (spec)))
4823 {
4824 int fringe_bitmap;
4825
4826 if (it)
4827 {
4828 if (!FRAME_WINDOW_P (it->f))
4829 /* If we return here, POSITION has been advanced
4830 across the text with this property. */
4831 {
4832 /* Synchronize the bidi iterator with POSITION. This is
4833 needed because we are not going to push the iterator
4834 on behalf of this display property, so there will be
4835 no pop_it call to do this synchronization for us. */
4836 if (it->bidi_p)
4837 {
4838 it->position = *position;
4839 iterate_out_of_display_property (it);
4840 *position = it->position;
4841 }
4842 return 1;
4843 }
4844 }
4845 else if (!frame_window_p)
4846 return 1;
4847
4848 #ifdef HAVE_WINDOW_SYSTEM
4849 value = XCAR (XCDR (spec));
4850 if (!SYMBOLP (value)
4851 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4852 /* If we return here, POSITION has been advanced
4853 across the text with this property. */
4854 {
4855 if (it && it->bidi_p)
4856 {
4857 it->position = *position;
4858 iterate_out_of_display_property (it);
4859 *position = it->position;
4860 }
4861 return 1;
4862 }
4863
4864 if (it)
4865 {
4866 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4867
4868 if (CONSP (XCDR (XCDR (spec))))
4869 {
4870 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4871 int face_id2 = lookup_derived_face (it->f, face_name,
4872 FRINGE_FACE_ID, 0);
4873 if (face_id2 >= 0)
4874 face_id = face_id2;
4875 }
4876
4877 /* Save current settings of IT so that we can restore them
4878 when we are finished with the glyph property value. */
4879 push_it (it, position);
4880
4881 it->area = TEXT_AREA;
4882 it->what = IT_IMAGE;
4883 it->image_id = -1; /* no image */
4884 it->position = start_pos;
4885 it->object = NILP (object) ? it->w->buffer : object;
4886 it->method = GET_FROM_IMAGE;
4887 it->from_overlay = Qnil;
4888 it->face_id = face_id;
4889 it->from_disp_prop_p = 1;
4890
4891 /* Say that we haven't consumed the characters with
4892 `display' property yet. The call to pop_it in
4893 set_iterator_to_next will clean this up. */
4894 *position = start_pos;
4895
4896 if (EQ (XCAR (spec), Qleft_fringe))
4897 {
4898 it->left_user_fringe_bitmap = fringe_bitmap;
4899 it->left_user_fringe_face_id = face_id;
4900 }
4901 else
4902 {
4903 it->right_user_fringe_bitmap = fringe_bitmap;
4904 it->right_user_fringe_face_id = face_id;
4905 }
4906 }
4907 #endif /* HAVE_WINDOW_SYSTEM */
4908 return 1;
4909 }
4910
4911 /* Prepare to handle `((margin left-margin) ...)',
4912 `((margin right-margin) ...)' and `((margin nil) ...)'
4913 prefixes for display specifications. */
4914 location = Qunbound;
4915 if (CONSP (spec) && CONSP (XCAR (spec)))
4916 {
4917 Lisp_Object tem;
4918
4919 value = XCDR (spec);
4920 if (CONSP (value))
4921 value = XCAR (value);
4922
4923 tem = XCAR (spec);
4924 if (EQ (XCAR (tem), Qmargin)
4925 && (tem = XCDR (tem),
4926 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4927 (NILP (tem)
4928 || EQ (tem, Qleft_margin)
4929 || EQ (tem, Qright_margin))))
4930 location = tem;
4931 }
4932
4933 if (EQ (location, Qunbound))
4934 {
4935 location = Qnil;
4936 value = spec;
4937 }
4938
4939 /* After this point, VALUE is the property after any
4940 margin prefix has been stripped. It must be a string,
4941 an image specification, or `(space ...)'.
4942
4943 LOCATION specifies where to display: `left-margin',
4944 `right-margin' or nil. */
4945
4946 valid_p = (STRINGP (value)
4947 #ifdef HAVE_WINDOW_SYSTEM
4948 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4949 && valid_image_p (value))
4950 #endif /* not HAVE_WINDOW_SYSTEM */
4951 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4952
4953 if (valid_p && !display_replaced_p)
4954 {
4955 int retval = 1;
4956
4957 if (!it)
4958 {
4959 /* Callers need to know whether the display spec is any kind
4960 of `(space ...)' spec that is about to affect text-area
4961 display. */
4962 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4963 retval = 2;
4964 return retval;
4965 }
4966
4967 /* Save current settings of IT so that we can restore them
4968 when we are finished with the glyph property value. */
4969 push_it (it, position);
4970 it->from_overlay = overlay;
4971 it->from_disp_prop_p = 1;
4972
4973 if (NILP (location))
4974 it->area = TEXT_AREA;
4975 else if (EQ (location, Qleft_margin))
4976 it->area = LEFT_MARGIN_AREA;
4977 else
4978 it->area = RIGHT_MARGIN_AREA;
4979
4980 if (STRINGP (value))
4981 {
4982 it->string = value;
4983 it->multibyte_p = STRING_MULTIBYTE (it->string);
4984 it->current.overlay_string_index = -1;
4985 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4986 it->end_charpos = it->string_nchars = SCHARS (it->string);
4987 it->method = GET_FROM_STRING;
4988 it->stop_charpos = 0;
4989 it->prev_stop = 0;
4990 it->base_level_stop = 0;
4991 it->string_from_display_prop_p = 1;
4992 /* Say that we haven't consumed the characters with
4993 `display' property yet. The call to pop_it in
4994 set_iterator_to_next will clean this up. */
4995 if (BUFFERP (object))
4996 *position = start_pos;
4997
4998 /* Force paragraph direction to be that of the parent
4999 object. If the parent object's paragraph direction is
5000 not yet determined, default to L2R. */
5001 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5002 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5003 else
5004 it->paragraph_embedding = L2R;
5005
5006 /* Set up the bidi iterator for this display string. */
5007 if (it->bidi_p)
5008 {
5009 it->bidi_it.string.lstring = it->string;
5010 it->bidi_it.string.s = NULL;
5011 it->bidi_it.string.schars = it->end_charpos;
5012 it->bidi_it.string.bufpos = bufpos;
5013 it->bidi_it.string.from_disp_str = 1;
5014 it->bidi_it.string.unibyte = !it->multibyte_p;
5015 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5016 }
5017 }
5018 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5019 {
5020 it->method = GET_FROM_STRETCH;
5021 it->object = value;
5022 *position = it->position = start_pos;
5023 retval = 1 + (it->area == TEXT_AREA);
5024 }
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 else
5027 {
5028 it->what = IT_IMAGE;
5029 it->image_id = lookup_image (it->f, value);
5030 it->position = start_pos;
5031 it->object = NILP (object) ? it->w->buffer : object;
5032 it->method = GET_FROM_IMAGE;
5033
5034 /* Say that we haven't consumed the characters with
5035 `display' property yet. The call to pop_it in
5036 set_iterator_to_next will clean this up. */
5037 *position = start_pos;
5038 }
5039 #endif /* HAVE_WINDOW_SYSTEM */
5040
5041 return retval;
5042 }
5043
5044 /* Invalid property or property not supported. Restore
5045 POSITION to what it was before. */
5046 *position = start_pos;
5047 return 0;
5048 }
5049
5050 /* Check if PROP is a display property value whose text should be
5051 treated as intangible. OVERLAY is the overlay from which PROP
5052 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5053 specify the buffer position covered by PROP. */
5054
5055 int
5056 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5057 ptrdiff_t charpos, ptrdiff_t bytepos)
5058 {
5059 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5060 struct text_pos position;
5061
5062 SET_TEXT_POS (position, charpos, bytepos);
5063 return handle_display_spec (NULL, prop, Qnil, overlay,
5064 &position, charpos, frame_window_p);
5065 }
5066
5067
5068 /* Return 1 if PROP is a display sub-property value containing STRING.
5069
5070 Implementation note: this and the following function are really
5071 special cases of handle_display_spec and
5072 handle_single_display_spec, and should ideally use the same code.
5073 Until they do, these two pairs must be consistent and must be
5074 modified in sync. */
5075
5076 static int
5077 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5078 {
5079 if (EQ (string, prop))
5080 return 1;
5081
5082 /* Skip over `when FORM'. */
5083 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5084 {
5085 prop = XCDR (prop);
5086 if (!CONSP (prop))
5087 return 0;
5088 /* Actually, the condition following `when' should be eval'ed,
5089 like handle_single_display_spec does, and we should return
5090 zero if it evaluates to nil. However, this function is
5091 called only when the buffer was already displayed and some
5092 glyph in the glyph matrix was found to come from a display
5093 string. Therefore, the condition was already evaluated, and
5094 the result was non-nil, otherwise the display string wouldn't
5095 have been displayed and we would have never been called for
5096 this property. Thus, we can skip the evaluation and assume
5097 its result is non-nil. */
5098 prop = XCDR (prop);
5099 }
5100
5101 if (CONSP (prop))
5102 /* Skip over `margin LOCATION'. */
5103 if (EQ (XCAR (prop), Qmargin))
5104 {
5105 prop = XCDR (prop);
5106 if (!CONSP (prop))
5107 return 0;
5108
5109 prop = XCDR (prop);
5110 if (!CONSP (prop))
5111 return 0;
5112 }
5113
5114 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5115 }
5116
5117
5118 /* Return 1 if STRING appears in the `display' property PROP. */
5119
5120 static int
5121 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5122 {
5123 if (CONSP (prop)
5124 && !EQ (XCAR (prop), Qwhen)
5125 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5126 {
5127 /* A list of sub-properties. */
5128 while (CONSP (prop))
5129 {
5130 if (single_display_spec_string_p (XCAR (prop), string))
5131 return 1;
5132 prop = XCDR (prop);
5133 }
5134 }
5135 else if (VECTORP (prop))
5136 {
5137 /* A vector of sub-properties. */
5138 ptrdiff_t i;
5139 for (i = 0; i < ASIZE (prop); ++i)
5140 if (single_display_spec_string_p (AREF (prop, i), string))
5141 return 1;
5142 }
5143 else
5144 return single_display_spec_string_p (prop, string);
5145
5146 return 0;
5147 }
5148
5149 /* Look for STRING in overlays and text properties in the current
5150 buffer, between character positions FROM and TO (excluding TO).
5151 BACK_P non-zero means look back (in this case, TO is supposed to be
5152 less than FROM).
5153 Value is the first character position where STRING was found, or
5154 zero if it wasn't found before hitting TO.
5155
5156 This function may only use code that doesn't eval because it is
5157 called asynchronously from note_mouse_highlight. */
5158
5159 static ptrdiff_t
5160 string_buffer_position_lim (Lisp_Object string,
5161 ptrdiff_t from, ptrdiff_t to, int back_p)
5162 {
5163 Lisp_Object limit, prop, pos;
5164 int found = 0;
5165
5166 pos = make_number (max (from, BEGV));
5167
5168 if (!back_p) /* looking forward */
5169 {
5170 limit = make_number (min (to, ZV));
5171 while (!found && !EQ (pos, limit))
5172 {
5173 prop = Fget_char_property (pos, Qdisplay, Qnil);
5174 if (!NILP (prop) && display_prop_string_p (prop, string))
5175 found = 1;
5176 else
5177 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5178 limit);
5179 }
5180 }
5181 else /* looking back */
5182 {
5183 limit = make_number (max (to, BEGV));
5184 while (!found && !EQ (pos, limit))
5185 {
5186 prop = Fget_char_property (pos, Qdisplay, Qnil);
5187 if (!NILP (prop) && display_prop_string_p (prop, string))
5188 found = 1;
5189 else
5190 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5191 limit);
5192 }
5193 }
5194
5195 return found ? XINT (pos) : 0;
5196 }
5197
5198 /* Determine which buffer position in current buffer STRING comes from.
5199 AROUND_CHARPOS is an approximate position where it could come from.
5200 Value is the buffer position or 0 if it couldn't be determined.
5201
5202 This function is necessary because we don't record buffer positions
5203 in glyphs generated from strings (to keep struct glyph small).
5204 This function may only use code that doesn't eval because it is
5205 called asynchronously from note_mouse_highlight. */
5206
5207 static ptrdiff_t
5208 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5209 {
5210 const int MAX_DISTANCE = 1000;
5211 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5212 around_charpos + MAX_DISTANCE,
5213 0);
5214
5215 if (!found)
5216 found = string_buffer_position_lim (string, around_charpos,
5217 around_charpos - MAX_DISTANCE, 1);
5218 return found;
5219 }
5220
5221
5222 \f
5223 /***********************************************************************
5224 `composition' property
5225 ***********************************************************************/
5226
5227 /* Set up iterator IT from `composition' property at its current
5228 position. Called from handle_stop. */
5229
5230 static enum prop_handled
5231 handle_composition_prop (struct it *it)
5232 {
5233 Lisp_Object prop, string;
5234 ptrdiff_t pos, pos_byte, start, end;
5235
5236 if (STRINGP (it->string))
5237 {
5238 unsigned char *s;
5239
5240 pos = IT_STRING_CHARPOS (*it);
5241 pos_byte = IT_STRING_BYTEPOS (*it);
5242 string = it->string;
5243 s = SDATA (string) + pos_byte;
5244 it->c = STRING_CHAR (s);
5245 }
5246 else
5247 {
5248 pos = IT_CHARPOS (*it);
5249 pos_byte = IT_BYTEPOS (*it);
5250 string = Qnil;
5251 it->c = FETCH_CHAR (pos_byte);
5252 }
5253
5254 /* If there's a valid composition and point is not inside of the
5255 composition (in the case that the composition is from the current
5256 buffer), draw a glyph composed from the composition components. */
5257 if (find_composition (pos, -1, &start, &end, &prop, string)
5258 && COMPOSITION_VALID_P (start, end, prop)
5259 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5260 {
5261 if (start < pos)
5262 /* As we can't handle this situation (perhaps font-lock added
5263 a new composition), we just return here hoping that next
5264 redisplay will detect this composition much earlier. */
5265 return HANDLED_NORMALLY;
5266 if (start != pos)
5267 {
5268 if (STRINGP (it->string))
5269 pos_byte = string_char_to_byte (it->string, start);
5270 else
5271 pos_byte = CHAR_TO_BYTE (start);
5272 }
5273 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5274 prop, string);
5275
5276 if (it->cmp_it.id >= 0)
5277 {
5278 it->cmp_it.ch = -1;
5279 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5280 it->cmp_it.nglyphs = -1;
5281 }
5282 }
5283
5284 return HANDLED_NORMALLY;
5285 }
5286
5287
5288 \f
5289 /***********************************************************************
5290 Overlay strings
5291 ***********************************************************************/
5292
5293 /* The following structure is used to record overlay strings for
5294 later sorting in load_overlay_strings. */
5295
5296 struct overlay_entry
5297 {
5298 Lisp_Object overlay;
5299 Lisp_Object string;
5300 EMACS_INT priority;
5301 int after_string_p;
5302 };
5303
5304
5305 /* Set up iterator IT from overlay strings at its current position.
5306 Called from handle_stop. */
5307
5308 static enum prop_handled
5309 handle_overlay_change (struct it *it)
5310 {
5311 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5312 return HANDLED_RECOMPUTE_PROPS;
5313 else
5314 return HANDLED_NORMALLY;
5315 }
5316
5317
5318 /* Set up the next overlay string for delivery by IT, if there is an
5319 overlay string to deliver. Called by set_iterator_to_next when the
5320 end of the current overlay string is reached. If there are more
5321 overlay strings to display, IT->string and
5322 IT->current.overlay_string_index are set appropriately here.
5323 Otherwise IT->string is set to nil. */
5324
5325 static void
5326 next_overlay_string (struct it *it)
5327 {
5328 ++it->current.overlay_string_index;
5329 if (it->current.overlay_string_index == it->n_overlay_strings)
5330 {
5331 /* No more overlay strings. Restore IT's settings to what
5332 they were before overlay strings were processed, and
5333 continue to deliver from current_buffer. */
5334
5335 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5336 pop_it (it);
5337 eassert (it->sp > 0
5338 || (NILP (it->string)
5339 && it->method == GET_FROM_BUFFER
5340 && it->stop_charpos >= BEGV
5341 && it->stop_charpos <= it->end_charpos));
5342 it->current.overlay_string_index = -1;
5343 it->n_overlay_strings = 0;
5344 it->overlay_strings_charpos = -1;
5345 /* If there's an empty display string on the stack, pop the
5346 stack, to resync the bidi iterator with IT's position. Such
5347 empty strings are pushed onto the stack in
5348 get_overlay_strings_1. */
5349 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5350 pop_it (it);
5351
5352 /* If we're at the end of the buffer, record that we have
5353 processed the overlay strings there already, so that
5354 next_element_from_buffer doesn't try it again. */
5355 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5356 it->overlay_strings_at_end_processed_p = 1;
5357 }
5358 else
5359 {
5360 /* There are more overlay strings to process. If
5361 IT->current.overlay_string_index has advanced to a position
5362 where we must load IT->overlay_strings with more strings, do
5363 it. We must load at the IT->overlay_strings_charpos where
5364 IT->n_overlay_strings was originally computed; when invisible
5365 text is present, this might not be IT_CHARPOS (Bug#7016). */
5366 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5367
5368 if (it->current.overlay_string_index && i == 0)
5369 load_overlay_strings (it, it->overlay_strings_charpos);
5370
5371 /* Initialize IT to deliver display elements from the overlay
5372 string. */
5373 it->string = it->overlay_strings[i];
5374 it->multibyte_p = STRING_MULTIBYTE (it->string);
5375 SET_TEXT_POS (it->current.string_pos, 0, 0);
5376 it->method = GET_FROM_STRING;
5377 it->stop_charpos = 0;
5378 it->end_charpos = SCHARS (it->string);
5379 if (it->cmp_it.stop_pos >= 0)
5380 it->cmp_it.stop_pos = 0;
5381 it->prev_stop = 0;
5382 it->base_level_stop = 0;
5383
5384 /* Set up the bidi iterator for this overlay string. */
5385 if (it->bidi_p)
5386 {
5387 it->bidi_it.string.lstring = it->string;
5388 it->bidi_it.string.s = NULL;
5389 it->bidi_it.string.schars = SCHARS (it->string);
5390 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5391 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5392 it->bidi_it.string.unibyte = !it->multibyte_p;
5393 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5394 }
5395 }
5396
5397 CHECK_IT (it);
5398 }
5399
5400
5401 /* Compare two overlay_entry structures E1 and E2. Used as a
5402 comparison function for qsort in load_overlay_strings. Overlay
5403 strings for the same position are sorted so that
5404
5405 1. All after-strings come in front of before-strings, except
5406 when they come from the same overlay.
5407
5408 2. Within after-strings, strings are sorted so that overlay strings
5409 from overlays with higher priorities come first.
5410
5411 2. Within before-strings, strings are sorted so that overlay
5412 strings from overlays with higher priorities come last.
5413
5414 Value is analogous to strcmp. */
5415
5416
5417 static int
5418 compare_overlay_entries (const void *e1, const void *e2)
5419 {
5420 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5421 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5422 int result;
5423
5424 if (entry1->after_string_p != entry2->after_string_p)
5425 {
5426 /* Let after-strings appear in front of before-strings if
5427 they come from different overlays. */
5428 if (EQ (entry1->overlay, entry2->overlay))
5429 result = entry1->after_string_p ? 1 : -1;
5430 else
5431 result = entry1->after_string_p ? -1 : 1;
5432 }
5433 else if (entry1->priority != entry2->priority)
5434 {
5435 if (entry1->after_string_p)
5436 /* After-strings sorted in order of decreasing priority. */
5437 result = entry2->priority < entry1->priority ? -1 : 1;
5438 else
5439 /* Before-strings sorted in order of increasing priority. */
5440 result = entry1->priority < entry2->priority ? -1 : 1;
5441 }
5442 else
5443 result = 0;
5444
5445 return result;
5446 }
5447
5448
5449 /* Load the vector IT->overlay_strings with overlay strings from IT's
5450 current buffer position, or from CHARPOS if that is > 0. Set
5451 IT->n_overlays to the total number of overlay strings found.
5452
5453 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5454 a time. On entry into load_overlay_strings,
5455 IT->current.overlay_string_index gives the number of overlay
5456 strings that have already been loaded by previous calls to this
5457 function.
5458
5459 IT->add_overlay_start contains an additional overlay start
5460 position to consider for taking overlay strings from, if non-zero.
5461 This position comes into play when the overlay has an `invisible'
5462 property, and both before and after-strings. When we've skipped to
5463 the end of the overlay, because of its `invisible' property, we
5464 nevertheless want its before-string to appear.
5465 IT->add_overlay_start will contain the overlay start position
5466 in this case.
5467
5468 Overlay strings are sorted so that after-string strings come in
5469 front of before-string strings. Within before and after-strings,
5470 strings are sorted by overlay priority. See also function
5471 compare_overlay_entries. */
5472
5473 static void
5474 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5475 {
5476 Lisp_Object overlay, window, str, invisible;
5477 struct Lisp_Overlay *ov;
5478 ptrdiff_t start, end;
5479 ptrdiff_t size = 20;
5480 ptrdiff_t n = 0, i, j;
5481 int invis_p;
5482 struct overlay_entry *entries = alloca (size * sizeof *entries);
5483 USE_SAFE_ALLOCA;
5484
5485 if (charpos <= 0)
5486 charpos = IT_CHARPOS (*it);
5487
5488 /* Append the overlay string STRING of overlay OVERLAY to vector
5489 `entries' which has size `size' and currently contains `n'
5490 elements. AFTER_P non-zero means STRING is an after-string of
5491 OVERLAY. */
5492 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5493 do \
5494 { \
5495 Lisp_Object priority; \
5496 \
5497 if (n == size) \
5498 { \
5499 struct overlay_entry *old = entries; \
5500 SAFE_NALLOCA (entries, 2, size); \
5501 memcpy (entries, old, size * sizeof *entries); \
5502 size *= 2; \
5503 } \
5504 \
5505 entries[n].string = (STRING); \
5506 entries[n].overlay = (OVERLAY); \
5507 priority = Foverlay_get ((OVERLAY), Qpriority); \
5508 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5509 entries[n].after_string_p = (AFTER_P); \
5510 ++n; \
5511 } \
5512 while (0)
5513
5514 /* Process overlay before the overlay center. */
5515 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5516 {
5517 XSETMISC (overlay, ov);
5518 eassert (OVERLAYP (overlay));
5519 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5520 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5521
5522 if (end < charpos)
5523 break;
5524
5525 /* Skip this overlay if it doesn't start or end at IT's current
5526 position. */
5527 if (end != charpos && start != charpos)
5528 continue;
5529
5530 /* Skip this overlay if it doesn't apply to IT->w. */
5531 window = Foverlay_get (overlay, Qwindow);
5532 if (WINDOWP (window) && XWINDOW (window) != it->w)
5533 continue;
5534
5535 /* If the text ``under'' the overlay is invisible, both before-
5536 and after-strings from this overlay are visible; start and
5537 end position are indistinguishable. */
5538 invisible = Foverlay_get (overlay, Qinvisible);
5539 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5540
5541 /* If overlay has a non-empty before-string, record it. */
5542 if ((start == charpos || (end == charpos && invis_p))
5543 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5544 && SCHARS (str))
5545 RECORD_OVERLAY_STRING (overlay, str, 0);
5546
5547 /* If overlay has a non-empty after-string, record it. */
5548 if ((end == charpos || (start == charpos && invis_p))
5549 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5550 && SCHARS (str))
5551 RECORD_OVERLAY_STRING (overlay, str, 1);
5552 }
5553
5554 /* Process overlays after the overlay center. */
5555 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5556 {
5557 XSETMISC (overlay, ov);
5558 eassert (OVERLAYP (overlay));
5559 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5560 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5561
5562 if (start > charpos)
5563 break;
5564
5565 /* Skip this overlay if it doesn't start or end at IT's current
5566 position. */
5567 if (end != charpos && start != charpos)
5568 continue;
5569
5570 /* Skip this overlay if it doesn't apply to IT->w. */
5571 window = Foverlay_get (overlay, Qwindow);
5572 if (WINDOWP (window) && XWINDOW (window) != it->w)
5573 continue;
5574
5575 /* If the text ``under'' the overlay is invisible, it has a zero
5576 dimension, and both before- and after-strings apply. */
5577 invisible = Foverlay_get (overlay, Qinvisible);
5578 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5579
5580 /* If overlay has a non-empty before-string, record it. */
5581 if ((start == charpos || (end == charpos && invis_p))
5582 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5583 && SCHARS (str))
5584 RECORD_OVERLAY_STRING (overlay, str, 0);
5585
5586 /* If overlay has a non-empty after-string, record it. */
5587 if ((end == charpos || (start == charpos && invis_p))
5588 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5589 && SCHARS (str))
5590 RECORD_OVERLAY_STRING (overlay, str, 1);
5591 }
5592
5593 #undef RECORD_OVERLAY_STRING
5594
5595 /* Sort entries. */
5596 if (n > 1)
5597 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5598
5599 /* Record number of overlay strings, and where we computed it. */
5600 it->n_overlay_strings = n;
5601 it->overlay_strings_charpos = charpos;
5602
5603 /* IT->current.overlay_string_index is the number of overlay strings
5604 that have already been consumed by IT. Copy some of the
5605 remaining overlay strings to IT->overlay_strings. */
5606 i = 0;
5607 j = it->current.overlay_string_index;
5608 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5609 {
5610 it->overlay_strings[i] = entries[j].string;
5611 it->string_overlays[i++] = entries[j++].overlay;
5612 }
5613
5614 CHECK_IT (it);
5615 SAFE_FREE ();
5616 }
5617
5618
5619 /* Get the first chunk of overlay strings at IT's current buffer
5620 position, or at CHARPOS if that is > 0. Value is non-zero if at
5621 least one overlay string was found. */
5622
5623 static int
5624 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5625 {
5626 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5627 process. This fills IT->overlay_strings with strings, and sets
5628 IT->n_overlay_strings to the total number of strings to process.
5629 IT->pos.overlay_string_index has to be set temporarily to zero
5630 because load_overlay_strings needs this; it must be set to -1
5631 when no overlay strings are found because a zero value would
5632 indicate a position in the first overlay string. */
5633 it->current.overlay_string_index = 0;
5634 load_overlay_strings (it, charpos);
5635
5636 /* If we found overlay strings, set up IT to deliver display
5637 elements from the first one. Otherwise set up IT to deliver
5638 from current_buffer. */
5639 if (it->n_overlay_strings)
5640 {
5641 /* Make sure we know settings in current_buffer, so that we can
5642 restore meaningful values when we're done with the overlay
5643 strings. */
5644 if (compute_stop_p)
5645 compute_stop_pos (it);
5646 eassert (it->face_id >= 0);
5647
5648 /* Save IT's settings. They are restored after all overlay
5649 strings have been processed. */
5650 eassert (!compute_stop_p || it->sp == 0);
5651
5652 /* When called from handle_stop, there might be an empty display
5653 string loaded. In that case, don't bother saving it. But
5654 don't use this optimization with the bidi iterator, since we
5655 need the corresponding pop_it call to resync the bidi
5656 iterator's position with IT's position, after we are done
5657 with the overlay strings. (The corresponding call to pop_it
5658 in case of an empty display string is in
5659 next_overlay_string.) */
5660 if (!(!it->bidi_p
5661 && STRINGP (it->string) && !SCHARS (it->string)))
5662 push_it (it, NULL);
5663
5664 /* Set up IT to deliver display elements from the first overlay
5665 string. */
5666 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5667 it->string = it->overlay_strings[0];
5668 it->from_overlay = Qnil;
5669 it->stop_charpos = 0;
5670 eassert (STRINGP (it->string));
5671 it->end_charpos = SCHARS (it->string);
5672 it->prev_stop = 0;
5673 it->base_level_stop = 0;
5674 it->multibyte_p = STRING_MULTIBYTE (it->string);
5675 it->method = GET_FROM_STRING;
5676 it->from_disp_prop_p = 0;
5677
5678 /* Force paragraph direction to be that of the parent
5679 buffer. */
5680 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5681 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5682 else
5683 it->paragraph_embedding = L2R;
5684
5685 /* Set up the bidi iterator for this overlay string. */
5686 if (it->bidi_p)
5687 {
5688 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5689
5690 it->bidi_it.string.lstring = it->string;
5691 it->bidi_it.string.s = NULL;
5692 it->bidi_it.string.schars = SCHARS (it->string);
5693 it->bidi_it.string.bufpos = pos;
5694 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5695 it->bidi_it.string.unibyte = !it->multibyte_p;
5696 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5697 }
5698 return 1;
5699 }
5700
5701 it->current.overlay_string_index = -1;
5702 return 0;
5703 }
5704
5705 static int
5706 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5707 {
5708 it->string = Qnil;
5709 it->method = GET_FROM_BUFFER;
5710
5711 (void) get_overlay_strings_1 (it, charpos, 1);
5712
5713 CHECK_IT (it);
5714
5715 /* Value is non-zero if we found at least one overlay string. */
5716 return STRINGP (it->string);
5717 }
5718
5719
5720 \f
5721 /***********************************************************************
5722 Saving and restoring state
5723 ***********************************************************************/
5724
5725 /* Save current settings of IT on IT->stack. Called, for example,
5726 before setting up IT for an overlay string, to be able to restore
5727 IT's settings to what they were after the overlay string has been
5728 processed. If POSITION is non-NULL, it is the position to save on
5729 the stack instead of IT->position. */
5730
5731 static void
5732 push_it (struct it *it, struct text_pos *position)
5733 {
5734 struct iterator_stack_entry *p;
5735
5736 eassert (it->sp < IT_STACK_SIZE);
5737 p = it->stack + it->sp;
5738
5739 p->stop_charpos = it->stop_charpos;
5740 p->prev_stop = it->prev_stop;
5741 p->base_level_stop = it->base_level_stop;
5742 p->cmp_it = it->cmp_it;
5743 eassert (it->face_id >= 0);
5744 p->face_id = it->face_id;
5745 p->string = it->string;
5746 p->method = it->method;
5747 p->from_overlay = it->from_overlay;
5748 switch (p->method)
5749 {
5750 case GET_FROM_IMAGE:
5751 p->u.image.object = it->object;
5752 p->u.image.image_id = it->image_id;
5753 p->u.image.slice = it->slice;
5754 break;
5755 case GET_FROM_STRETCH:
5756 p->u.stretch.object = it->object;
5757 break;
5758 }
5759 p->position = position ? *position : it->position;
5760 p->current = it->current;
5761 p->end_charpos = it->end_charpos;
5762 p->string_nchars = it->string_nchars;
5763 p->area = it->area;
5764 p->multibyte_p = it->multibyte_p;
5765 p->avoid_cursor_p = it->avoid_cursor_p;
5766 p->space_width = it->space_width;
5767 p->font_height = it->font_height;
5768 p->voffset = it->voffset;
5769 p->string_from_display_prop_p = it->string_from_display_prop_p;
5770 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5771 p->display_ellipsis_p = 0;
5772 p->line_wrap = it->line_wrap;
5773 p->bidi_p = it->bidi_p;
5774 p->paragraph_embedding = it->paragraph_embedding;
5775 p->from_disp_prop_p = it->from_disp_prop_p;
5776 ++it->sp;
5777
5778 /* Save the state of the bidi iterator as well. */
5779 if (it->bidi_p)
5780 bidi_push_it (&it->bidi_it);
5781 }
5782
5783 static void
5784 iterate_out_of_display_property (struct it *it)
5785 {
5786 int buffer_p = !STRINGP (it->string);
5787 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5788 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5789
5790 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5791
5792 /* Maybe initialize paragraph direction. If we are at the beginning
5793 of a new paragraph, next_element_from_buffer may not have a
5794 chance to do that. */
5795 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5796 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5797 /* prev_stop can be zero, so check against BEGV as well. */
5798 while (it->bidi_it.charpos >= bob
5799 && it->prev_stop <= it->bidi_it.charpos
5800 && it->bidi_it.charpos < CHARPOS (it->position)
5801 && it->bidi_it.charpos < eob)
5802 bidi_move_to_visually_next (&it->bidi_it);
5803 /* Record the stop_pos we just crossed, for when we cross it
5804 back, maybe. */
5805 if (it->bidi_it.charpos > CHARPOS (it->position))
5806 it->prev_stop = CHARPOS (it->position);
5807 /* If we ended up not where pop_it put us, resync IT's
5808 positional members with the bidi iterator. */
5809 if (it->bidi_it.charpos != CHARPOS (it->position))
5810 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5811 if (buffer_p)
5812 it->current.pos = it->position;
5813 else
5814 it->current.string_pos = it->position;
5815 }
5816
5817 /* Restore IT's settings from IT->stack. Called, for example, when no
5818 more overlay strings must be processed, and we return to delivering
5819 display elements from a buffer, or when the end of a string from a
5820 `display' property is reached and we return to delivering display
5821 elements from an overlay string, or from a buffer. */
5822
5823 static void
5824 pop_it (struct it *it)
5825 {
5826 struct iterator_stack_entry *p;
5827 int from_display_prop = it->from_disp_prop_p;
5828
5829 eassert (it->sp > 0);
5830 --it->sp;
5831 p = it->stack + it->sp;
5832 it->stop_charpos = p->stop_charpos;
5833 it->prev_stop = p->prev_stop;
5834 it->base_level_stop = p->base_level_stop;
5835 it->cmp_it = p->cmp_it;
5836 it->face_id = p->face_id;
5837 it->current = p->current;
5838 it->position = p->position;
5839 it->string = p->string;
5840 it->from_overlay = p->from_overlay;
5841 if (NILP (it->string))
5842 SET_TEXT_POS (it->current.string_pos, -1, -1);
5843 it->method = p->method;
5844 switch (it->method)
5845 {
5846 case GET_FROM_IMAGE:
5847 it->image_id = p->u.image.image_id;
5848 it->object = p->u.image.object;
5849 it->slice = p->u.image.slice;
5850 break;
5851 case GET_FROM_STRETCH:
5852 it->object = p->u.stretch.object;
5853 break;
5854 case GET_FROM_BUFFER:
5855 it->object = it->w->buffer;
5856 break;
5857 case GET_FROM_STRING:
5858 it->object = it->string;
5859 break;
5860 case GET_FROM_DISPLAY_VECTOR:
5861 if (it->s)
5862 it->method = GET_FROM_C_STRING;
5863 else if (STRINGP (it->string))
5864 it->method = GET_FROM_STRING;
5865 else
5866 {
5867 it->method = GET_FROM_BUFFER;
5868 it->object = it->w->buffer;
5869 }
5870 }
5871 it->end_charpos = p->end_charpos;
5872 it->string_nchars = p->string_nchars;
5873 it->area = p->area;
5874 it->multibyte_p = p->multibyte_p;
5875 it->avoid_cursor_p = p->avoid_cursor_p;
5876 it->space_width = p->space_width;
5877 it->font_height = p->font_height;
5878 it->voffset = p->voffset;
5879 it->string_from_display_prop_p = p->string_from_display_prop_p;
5880 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5881 it->line_wrap = p->line_wrap;
5882 it->bidi_p = p->bidi_p;
5883 it->paragraph_embedding = p->paragraph_embedding;
5884 it->from_disp_prop_p = p->from_disp_prop_p;
5885 if (it->bidi_p)
5886 {
5887 bidi_pop_it (&it->bidi_it);
5888 /* Bidi-iterate until we get out of the portion of text, if any,
5889 covered by a `display' text property or by an overlay with
5890 `display' property. (We cannot just jump there, because the
5891 internal coherency of the bidi iterator state can not be
5892 preserved across such jumps.) We also must determine the
5893 paragraph base direction if the overlay we just processed is
5894 at the beginning of a new paragraph. */
5895 if (from_display_prop
5896 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5897 iterate_out_of_display_property (it);
5898
5899 eassert ((BUFFERP (it->object)
5900 && IT_CHARPOS (*it) == it->bidi_it.charpos
5901 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5902 || (STRINGP (it->object)
5903 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5904 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5905 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5906 }
5907 }
5908
5909
5910 \f
5911 /***********************************************************************
5912 Moving over lines
5913 ***********************************************************************/
5914
5915 /* Set IT's current position to the previous line start. */
5916
5917 static void
5918 back_to_previous_line_start (struct it *it)
5919 {
5920 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5921 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5922 }
5923
5924
5925 /* Move IT to the next line start.
5926
5927 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5928 we skipped over part of the text (as opposed to moving the iterator
5929 continuously over the text). Otherwise, don't change the value
5930 of *SKIPPED_P.
5931
5932 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5933 iterator on the newline, if it was found.
5934
5935 Newlines may come from buffer text, overlay strings, or strings
5936 displayed via the `display' property. That's the reason we can't
5937 simply use find_next_newline_no_quit.
5938
5939 Note that this function may not skip over invisible text that is so
5940 because of text properties and immediately follows a newline. If
5941 it would, function reseat_at_next_visible_line_start, when called
5942 from set_iterator_to_next, would effectively make invisible
5943 characters following a newline part of the wrong glyph row, which
5944 leads to wrong cursor motion. */
5945
5946 static int
5947 forward_to_next_line_start (struct it *it, int *skipped_p,
5948 struct bidi_it *bidi_it_prev)
5949 {
5950 ptrdiff_t old_selective;
5951 int newline_found_p, n;
5952 const int MAX_NEWLINE_DISTANCE = 500;
5953
5954 /* If already on a newline, just consume it to avoid unintended
5955 skipping over invisible text below. */
5956 if (it->what == IT_CHARACTER
5957 && it->c == '\n'
5958 && CHARPOS (it->position) == IT_CHARPOS (*it))
5959 {
5960 if (it->bidi_p && bidi_it_prev)
5961 *bidi_it_prev = it->bidi_it;
5962 set_iterator_to_next (it, 0);
5963 it->c = 0;
5964 return 1;
5965 }
5966
5967 /* Don't handle selective display in the following. It's (a)
5968 unnecessary because it's done by the caller, and (b) leads to an
5969 infinite recursion because next_element_from_ellipsis indirectly
5970 calls this function. */
5971 old_selective = it->selective;
5972 it->selective = 0;
5973
5974 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5975 from buffer text. */
5976 for (n = newline_found_p = 0;
5977 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5978 n += STRINGP (it->string) ? 0 : 1)
5979 {
5980 if (!get_next_display_element (it))
5981 return 0;
5982 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5983 if (newline_found_p && it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5986 }
5987
5988 /* If we didn't find a newline near enough, see if we can use a
5989 short-cut. */
5990 if (!newline_found_p)
5991 {
5992 ptrdiff_t start = IT_CHARPOS (*it);
5993 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5994 Lisp_Object pos;
5995
5996 eassert (!STRINGP (it->string));
5997
5998 /* If there isn't any `display' property in sight, and no
5999 overlays, we can just use the position of the newline in
6000 buffer text. */
6001 if (it->stop_charpos >= limit
6002 || ((pos = Fnext_single_property_change (make_number (start),
6003 Qdisplay, Qnil,
6004 make_number (limit)),
6005 NILP (pos))
6006 && next_overlay_change (start) == ZV))
6007 {
6008 if (!it->bidi_p)
6009 {
6010 IT_CHARPOS (*it) = limit;
6011 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6012 }
6013 else
6014 {
6015 struct bidi_it bprev;
6016
6017 /* Help bidi.c avoid expensive searches for display
6018 properties and overlays, by telling it that there are
6019 none up to `limit'. */
6020 if (it->bidi_it.disp_pos < limit)
6021 {
6022 it->bidi_it.disp_pos = limit;
6023 it->bidi_it.disp_prop = 0;
6024 }
6025 do {
6026 bprev = it->bidi_it;
6027 bidi_move_to_visually_next (&it->bidi_it);
6028 } while (it->bidi_it.charpos != limit);
6029 IT_CHARPOS (*it) = limit;
6030 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6031 if (bidi_it_prev)
6032 *bidi_it_prev = bprev;
6033 }
6034 *skipped_p = newline_found_p = 1;
6035 }
6036 else
6037 {
6038 while (get_next_display_element (it)
6039 && !newline_found_p)
6040 {
6041 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6042 if (newline_found_p && it->bidi_p && bidi_it_prev)
6043 *bidi_it_prev = it->bidi_it;
6044 set_iterator_to_next (it, 0);
6045 }
6046 }
6047 }
6048
6049 it->selective = old_selective;
6050 return newline_found_p;
6051 }
6052
6053
6054 /* Set IT's current position to the previous visible line start. Skip
6055 invisible text that is so either due to text properties or due to
6056 selective display. Caution: this does not change IT->current_x and
6057 IT->hpos. */
6058
6059 static void
6060 back_to_previous_visible_line_start (struct it *it)
6061 {
6062 while (IT_CHARPOS (*it) > BEGV)
6063 {
6064 back_to_previous_line_start (it);
6065
6066 if (IT_CHARPOS (*it) <= BEGV)
6067 break;
6068
6069 /* If selective > 0, then lines indented more than its value are
6070 invisible. */
6071 if (it->selective > 0
6072 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6073 it->selective))
6074 continue;
6075
6076 /* Check the newline before point for invisibility. */
6077 {
6078 Lisp_Object prop;
6079 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6080 Qinvisible, it->window);
6081 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6082 continue;
6083 }
6084
6085 if (IT_CHARPOS (*it) <= BEGV)
6086 break;
6087
6088 {
6089 struct it it2;
6090 void *it2data = NULL;
6091 ptrdiff_t pos;
6092 ptrdiff_t beg, end;
6093 Lisp_Object val, overlay;
6094
6095 SAVE_IT (it2, *it, it2data);
6096
6097 /* If newline is part of a composition, continue from start of composition */
6098 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6099 && beg < IT_CHARPOS (*it))
6100 goto replaced;
6101
6102 /* If newline is replaced by a display property, find start of overlay
6103 or interval and continue search from that point. */
6104 pos = --IT_CHARPOS (it2);
6105 --IT_BYTEPOS (it2);
6106 it2.sp = 0;
6107 bidi_unshelve_cache (NULL, 0);
6108 it2.string_from_display_prop_p = 0;
6109 it2.from_disp_prop_p = 0;
6110 if (handle_display_prop (&it2) == HANDLED_RETURN
6111 && !NILP (val = get_char_property_and_overlay
6112 (make_number (pos), Qdisplay, Qnil, &overlay))
6113 && (OVERLAYP (overlay)
6114 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6115 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6116 {
6117 RESTORE_IT (it, it, it2data);
6118 goto replaced;
6119 }
6120
6121 /* Newline is not replaced by anything -- so we are done. */
6122 RESTORE_IT (it, it, it2data);
6123 break;
6124
6125 replaced:
6126 if (beg < BEGV)
6127 beg = BEGV;
6128 IT_CHARPOS (*it) = beg;
6129 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6130 }
6131 }
6132
6133 it->continuation_lines_width = 0;
6134
6135 eassert (IT_CHARPOS (*it) >= BEGV);
6136 eassert (IT_CHARPOS (*it) == BEGV
6137 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6138 CHECK_IT (it);
6139 }
6140
6141
6142 /* Reseat iterator IT at the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. At the end, update IT's overlay information,
6145 face information etc. */
6146
6147 void
6148 reseat_at_previous_visible_line_start (struct it *it)
6149 {
6150 back_to_previous_visible_line_start (it);
6151 reseat (it, it->current.pos, 1);
6152 CHECK_IT (it);
6153 }
6154
6155
6156 /* Reseat iterator IT on the next visible line start in the current
6157 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6158 preceding the line start. Skip over invisible text that is so
6159 because of selective display. Compute faces, overlays etc at the
6160 new position. Note that this function does not skip over text that
6161 is invisible because of text properties. */
6162
6163 static void
6164 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6165 {
6166 int newline_found_p, skipped_p = 0;
6167 struct bidi_it bidi_it_prev;
6168
6169 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6170
6171 /* Skip over lines that are invisible because they are indented
6172 more than the value of IT->selective. */
6173 if (it->selective > 0)
6174 while (IT_CHARPOS (*it) < ZV
6175 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6176 it->selective))
6177 {
6178 eassert (IT_BYTEPOS (*it) == BEGV
6179 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6180 newline_found_p =
6181 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6182 }
6183
6184 /* Position on the newline if that's what's requested. */
6185 if (on_newline_p && newline_found_p)
6186 {
6187 if (STRINGP (it->string))
6188 {
6189 if (IT_STRING_CHARPOS (*it) > 0)
6190 {
6191 if (!it->bidi_p)
6192 {
6193 --IT_STRING_CHARPOS (*it);
6194 --IT_STRING_BYTEPOS (*it);
6195 }
6196 else
6197 {
6198 /* We need to restore the bidi iterator to the state
6199 it had on the newline, and resync the IT's
6200 position with that. */
6201 it->bidi_it = bidi_it_prev;
6202 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6204 }
6205 }
6206 }
6207 else if (IT_CHARPOS (*it) > BEGV)
6208 {
6209 if (!it->bidi_p)
6210 {
6211 --IT_CHARPOS (*it);
6212 --IT_BYTEPOS (*it);
6213 }
6214 else
6215 {
6216 /* We need to restore the bidi iterator to the state it
6217 had on the newline and resync IT with that. */
6218 it->bidi_it = bidi_it_prev;
6219 IT_CHARPOS (*it) = it->bidi_it.charpos;
6220 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6221 }
6222 reseat (it, it->current.pos, 0);
6223 }
6224 }
6225 else if (skipped_p)
6226 reseat (it, it->current.pos, 0);
6227
6228 CHECK_IT (it);
6229 }
6230
6231
6232 \f
6233 /***********************************************************************
6234 Changing an iterator's position
6235 ***********************************************************************/
6236
6237 /* Change IT's current position to POS in current_buffer. If FORCE_P
6238 is non-zero, always check for text properties at the new position.
6239 Otherwise, text properties are only looked up if POS >=
6240 IT->check_charpos of a property. */
6241
6242 static void
6243 reseat (struct it *it, struct text_pos pos, int force_p)
6244 {
6245 ptrdiff_t original_pos = IT_CHARPOS (*it);
6246
6247 reseat_1 (it, pos, 0);
6248
6249 /* Determine where to check text properties. Avoid doing it
6250 where possible because text property lookup is very expensive. */
6251 if (force_p
6252 || CHARPOS (pos) > it->stop_charpos
6253 || CHARPOS (pos) < original_pos)
6254 {
6255 if (it->bidi_p)
6256 {
6257 /* For bidi iteration, we need to prime prev_stop and
6258 base_level_stop with our best estimations. */
6259 /* Implementation note: Of course, POS is not necessarily a
6260 stop position, so assigning prev_pos to it is a lie; we
6261 should have called compute_stop_backwards. However, if
6262 the current buffer does not include any R2L characters,
6263 that call would be a waste of cycles, because the
6264 iterator will never move back, and thus never cross this
6265 "fake" stop position. So we delay that backward search
6266 until the time we really need it, in next_element_from_buffer. */
6267 if (CHARPOS (pos) != it->prev_stop)
6268 it->prev_stop = CHARPOS (pos);
6269 if (CHARPOS (pos) < it->base_level_stop)
6270 it->base_level_stop = 0; /* meaning it's unknown */
6271 handle_stop (it);
6272 }
6273 else
6274 {
6275 handle_stop (it);
6276 it->prev_stop = it->base_level_stop = 0;
6277 }
6278
6279 }
6280
6281 CHECK_IT (it);
6282 }
6283
6284
6285 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6286 IT->stop_pos to POS, also. */
6287
6288 static void
6289 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6290 {
6291 /* Don't call this function when scanning a C string. */
6292 eassert (it->s == NULL);
6293
6294 /* POS must be a reasonable value. */
6295 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6296
6297 it->current.pos = it->position = pos;
6298 it->end_charpos = ZV;
6299 it->dpvec = NULL;
6300 it->current.dpvec_index = -1;
6301 it->current.overlay_string_index = -1;
6302 IT_STRING_CHARPOS (*it) = -1;
6303 IT_STRING_BYTEPOS (*it) = -1;
6304 it->string = Qnil;
6305 it->method = GET_FROM_BUFFER;
6306 it->object = it->w->buffer;
6307 it->area = TEXT_AREA;
6308 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6309 it->sp = 0;
6310 it->string_from_display_prop_p = 0;
6311 it->string_from_prefix_prop_p = 0;
6312
6313 it->from_disp_prop_p = 0;
6314 it->face_before_selective_p = 0;
6315 if (it->bidi_p)
6316 {
6317 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6318 &it->bidi_it);
6319 bidi_unshelve_cache (NULL, 0);
6320 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6321 it->bidi_it.string.s = NULL;
6322 it->bidi_it.string.lstring = Qnil;
6323 it->bidi_it.string.bufpos = 0;
6324 it->bidi_it.string.unibyte = 0;
6325 }
6326
6327 if (set_stop_p)
6328 {
6329 it->stop_charpos = CHARPOS (pos);
6330 it->base_level_stop = CHARPOS (pos);
6331 }
6332 /* This make the information stored in it->cmp_it invalidate. */
6333 it->cmp_it.id = -1;
6334 }
6335
6336
6337 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6338 If S is non-null, it is a C string to iterate over. Otherwise,
6339 STRING gives a Lisp string to iterate over.
6340
6341 If PRECISION > 0, don't return more then PRECISION number of
6342 characters from the string.
6343
6344 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6345 characters have been returned. FIELD_WIDTH < 0 means an infinite
6346 field width.
6347
6348 MULTIBYTE = 0 means disable processing of multibyte characters,
6349 MULTIBYTE > 0 means enable it,
6350 MULTIBYTE < 0 means use IT->multibyte_p.
6351
6352 IT must be initialized via a prior call to init_iterator before
6353 calling this function. */
6354
6355 static void
6356 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6357 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6358 int multibyte)
6359 {
6360 /* No region in strings. */
6361 it->region_beg_charpos = it->region_end_charpos = -1;
6362
6363 /* No text property checks performed by default, but see below. */
6364 it->stop_charpos = -1;
6365
6366 /* Set iterator position and end position. */
6367 memset (&it->current, 0, sizeof it->current);
6368 it->current.overlay_string_index = -1;
6369 it->current.dpvec_index = -1;
6370 eassert (charpos >= 0);
6371
6372 /* If STRING is specified, use its multibyteness, otherwise use the
6373 setting of MULTIBYTE, if specified. */
6374 if (multibyte >= 0)
6375 it->multibyte_p = multibyte > 0;
6376
6377 /* Bidirectional reordering of strings is controlled by the default
6378 value of bidi-display-reordering. Don't try to reorder while
6379 loading loadup.el, as the necessary character property tables are
6380 not yet available. */
6381 it->bidi_p =
6382 NILP (Vpurify_flag)
6383 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6384
6385 if (s == NULL)
6386 {
6387 eassert (STRINGP (string));
6388 it->string = string;
6389 it->s = NULL;
6390 it->end_charpos = it->string_nchars = SCHARS (string);
6391 it->method = GET_FROM_STRING;
6392 it->current.string_pos = string_pos (charpos, string);
6393
6394 if (it->bidi_p)
6395 {
6396 it->bidi_it.string.lstring = string;
6397 it->bidi_it.string.s = NULL;
6398 it->bidi_it.string.schars = it->end_charpos;
6399 it->bidi_it.string.bufpos = 0;
6400 it->bidi_it.string.from_disp_str = 0;
6401 it->bidi_it.string.unibyte = !it->multibyte_p;
6402 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6403 FRAME_WINDOW_P (it->f), &it->bidi_it);
6404 }
6405 }
6406 else
6407 {
6408 it->s = (const unsigned char *) s;
6409 it->string = Qnil;
6410
6411 /* Note that we use IT->current.pos, not it->current.string_pos,
6412 for displaying C strings. */
6413 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6414 if (it->multibyte_p)
6415 {
6416 it->current.pos = c_string_pos (charpos, s, 1);
6417 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6418 }
6419 else
6420 {
6421 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6422 it->end_charpos = it->string_nchars = strlen (s);
6423 }
6424
6425 if (it->bidi_p)
6426 {
6427 it->bidi_it.string.lstring = Qnil;
6428 it->bidi_it.string.s = (const unsigned char *) s;
6429 it->bidi_it.string.schars = it->end_charpos;
6430 it->bidi_it.string.bufpos = 0;
6431 it->bidi_it.string.from_disp_str = 0;
6432 it->bidi_it.string.unibyte = !it->multibyte_p;
6433 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6434 &it->bidi_it);
6435 }
6436 it->method = GET_FROM_C_STRING;
6437 }
6438
6439 /* PRECISION > 0 means don't return more than PRECISION characters
6440 from the string. */
6441 if (precision > 0 && it->end_charpos - charpos > precision)
6442 {
6443 it->end_charpos = it->string_nchars = charpos + precision;
6444 if (it->bidi_p)
6445 it->bidi_it.string.schars = it->end_charpos;
6446 }
6447
6448 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6449 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6450 FIELD_WIDTH < 0 means infinite field width. This is useful for
6451 padding with `-' at the end of a mode line. */
6452 if (field_width < 0)
6453 field_width = INFINITY;
6454 /* Implementation note: We deliberately don't enlarge
6455 it->bidi_it.string.schars here to fit it->end_charpos, because
6456 the bidi iterator cannot produce characters out of thin air. */
6457 if (field_width > it->end_charpos - charpos)
6458 it->end_charpos = charpos + field_width;
6459
6460 /* Use the standard display table for displaying strings. */
6461 if (DISP_TABLE_P (Vstandard_display_table))
6462 it->dp = XCHAR_TABLE (Vstandard_display_table);
6463
6464 it->stop_charpos = charpos;
6465 it->prev_stop = charpos;
6466 it->base_level_stop = 0;
6467 if (it->bidi_p)
6468 {
6469 it->bidi_it.first_elt = 1;
6470 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6471 it->bidi_it.disp_pos = -1;
6472 }
6473 if (s == NULL && it->multibyte_p)
6474 {
6475 ptrdiff_t endpos = SCHARS (it->string);
6476 if (endpos > it->end_charpos)
6477 endpos = it->end_charpos;
6478 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6479 it->string);
6480 }
6481 CHECK_IT (it);
6482 }
6483
6484
6485 \f
6486 /***********************************************************************
6487 Iteration
6488 ***********************************************************************/
6489
6490 /* Map enum it_method value to corresponding next_element_from_* function. */
6491
6492 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6493 {
6494 next_element_from_buffer,
6495 next_element_from_display_vector,
6496 next_element_from_string,
6497 next_element_from_c_string,
6498 next_element_from_image,
6499 next_element_from_stretch
6500 };
6501
6502 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6503
6504
6505 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6506 (possibly with the following characters). */
6507
6508 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6509 ((IT)->cmp_it.id >= 0 \
6510 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6511 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6512 END_CHARPOS, (IT)->w, \
6513 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6514 (IT)->string)))
6515
6516
6517 /* Lookup the char-table Vglyphless_char_display for character C (-1
6518 if we want information for no-font case), and return the display
6519 method symbol. By side-effect, update it->what and
6520 it->glyphless_method. This function is called from
6521 get_next_display_element for each character element, and from
6522 x_produce_glyphs when no suitable font was found. */
6523
6524 Lisp_Object
6525 lookup_glyphless_char_display (int c, struct it *it)
6526 {
6527 Lisp_Object glyphless_method = Qnil;
6528
6529 if (CHAR_TABLE_P (Vglyphless_char_display)
6530 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6531 {
6532 if (c >= 0)
6533 {
6534 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6535 if (CONSP (glyphless_method))
6536 glyphless_method = FRAME_WINDOW_P (it->f)
6537 ? XCAR (glyphless_method)
6538 : XCDR (glyphless_method);
6539 }
6540 else
6541 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6542 }
6543
6544 retry:
6545 if (NILP (glyphless_method))
6546 {
6547 if (c >= 0)
6548 /* The default is to display the character by a proper font. */
6549 return Qnil;
6550 /* The default for the no-font case is to display an empty box. */
6551 glyphless_method = Qempty_box;
6552 }
6553 if (EQ (glyphless_method, Qzero_width))
6554 {
6555 if (c >= 0)
6556 return glyphless_method;
6557 /* This method can't be used for the no-font case. */
6558 glyphless_method = Qempty_box;
6559 }
6560 if (EQ (glyphless_method, Qthin_space))
6561 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6562 else if (EQ (glyphless_method, Qempty_box))
6563 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6564 else if (EQ (glyphless_method, Qhex_code))
6565 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6566 else if (STRINGP (glyphless_method))
6567 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6568 else
6569 {
6570 /* Invalid value. We use the default method. */
6571 glyphless_method = Qnil;
6572 goto retry;
6573 }
6574 it->what = IT_GLYPHLESS;
6575 return glyphless_method;
6576 }
6577
6578 /* Load IT's display element fields with information about the next
6579 display element from the current position of IT. Value is zero if
6580 end of buffer (or C string) is reached. */
6581
6582 static struct frame *last_escape_glyph_frame = NULL;
6583 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6584 static int last_escape_glyph_merged_face_id = 0;
6585
6586 struct frame *last_glyphless_glyph_frame = NULL;
6587 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6588 int last_glyphless_glyph_merged_face_id = 0;
6589
6590 static int
6591 get_next_display_element (struct it *it)
6592 {
6593 /* Non-zero means that we found a display element. Zero means that
6594 we hit the end of what we iterate over. Performance note: the
6595 function pointer `method' used here turns out to be faster than
6596 using a sequence of if-statements. */
6597 int success_p;
6598
6599 get_next:
6600 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6601
6602 if (it->what == IT_CHARACTER)
6603 {
6604 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6605 and only if (a) the resolved directionality of that character
6606 is R..." */
6607 /* FIXME: Do we need an exception for characters from display
6608 tables? */
6609 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6610 it->c = bidi_mirror_char (it->c);
6611 /* Map via display table or translate control characters.
6612 IT->c, IT->len etc. have been set to the next character by
6613 the function call above. If we have a display table, and it
6614 contains an entry for IT->c, translate it. Don't do this if
6615 IT->c itself comes from a display table, otherwise we could
6616 end up in an infinite recursion. (An alternative could be to
6617 count the recursion depth of this function and signal an
6618 error when a certain maximum depth is reached.) Is it worth
6619 it? */
6620 if (success_p && it->dpvec == NULL)
6621 {
6622 Lisp_Object dv;
6623 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6624 int nonascii_space_p = 0;
6625 int nonascii_hyphen_p = 0;
6626 int c = it->c; /* This is the character to display. */
6627
6628 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6629 {
6630 eassert (SINGLE_BYTE_CHAR_P (c));
6631 if (unibyte_display_via_language_environment)
6632 {
6633 c = DECODE_CHAR (unibyte, c);
6634 if (c < 0)
6635 c = BYTE8_TO_CHAR (it->c);
6636 }
6637 else
6638 c = BYTE8_TO_CHAR (it->c);
6639 }
6640
6641 if (it->dp
6642 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6643 VECTORP (dv)))
6644 {
6645 struct Lisp_Vector *v = XVECTOR (dv);
6646
6647 /* Return the first character from the display table
6648 entry, if not empty. If empty, don't display the
6649 current character. */
6650 if (v->header.size)
6651 {
6652 it->dpvec_char_len = it->len;
6653 it->dpvec = v->contents;
6654 it->dpend = v->contents + v->header.size;
6655 it->current.dpvec_index = 0;
6656 it->dpvec_face_id = -1;
6657 it->saved_face_id = it->face_id;
6658 it->method = GET_FROM_DISPLAY_VECTOR;
6659 it->ellipsis_p = 0;
6660 }
6661 else
6662 {
6663 set_iterator_to_next (it, 0);
6664 }
6665 goto get_next;
6666 }
6667
6668 if (! NILP (lookup_glyphless_char_display (c, it)))
6669 {
6670 if (it->what == IT_GLYPHLESS)
6671 goto done;
6672 /* Don't display this character. */
6673 set_iterator_to_next (it, 0);
6674 goto get_next;
6675 }
6676
6677 /* If `nobreak-char-display' is non-nil, we display
6678 non-ASCII spaces and hyphens specially. */
6679 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6680 {
6681 if (c == 0xA0)
6682 nonascii_space_p = 1;
6683 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6684 nonascii_hyphen_p = 1;
6685 }
6686
6687 /* Translate control characters into `\003' or `^C' form.
6688 Control characters coming from a display table entry are
6689 currently not translated because we use IT->dpvec to hold
6690 the translation. This could easily be changed but I
6691 don't believe that it is worth doing.
6692
6693 The characters handled by `nobreak-char-display' must be
6694 translated too.
6695
6696 Non-printable characters and raw-byte characters are also
6697 translated to octal form. */
6698 if (((c < ' ' || c == 127) /* ASCII control chars */
6699 ? (it->area != TEXT_AREA
6700 /* In mode line, treat \n, \t like other crl chars. */
6701 || (c != '\t'
6702 && it->glyph_row
6703 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6704 || (c != '\n' && c != '\t'))
6705 : (nonascii_space_p
6706 || nonascii_hyphen_p
6707 || CHAR_BYTE8_P (c)
6708 || ! CHAR_PRINTABLE_P (c))))
6709 {
6710 /* C is a control character, non-ASCII space/hyphen,
6711 raw-byte, or a non-printable character which must be
6712 displayed either as '\003' or as `^C' where the '\\'
6713 and '^' can be defined in the display table. Fill
6714 IT->ctl_chars with glyphs for what we have to
6715 display. Then, set IT->dpvec to these glyphs. */
6716 Lisp_Object gc;
6717 int ctl_len;
6718 int face_id;
6719 int lface_id = 0;
6720 int escape_glyph;
6721
6722 /* Handle control characters with ^. */
6723
6724 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6725 {
6726 int g;
6727
6728 g = '^'; /* default glyph for Control */
6729 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6730 if (it->dp
6731 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6732 {
6733 g = GLYPH_CODE_CHAR (gc);
6734 lface_id = GLYPH_CODE_FACE (gc);
6735 }
6736 if (lface_id)
6737 {
6738 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6739 }
6740 else if (it->f == last_escape_glyph_frame
6741 && it->face_id == last_escape_glyph_face_id)
6742 {
6743 face_id = last_escape_glyph_merged_face_id;
6744 }
6745 else
6746 {
6747 /* Merge the escape-glyph face into the current face. */
6748 face_id = merge_faces (it->f, Qescape_glyph, 0,
6749 it->face_id);
6750 last_escape_glyph_frame = it->f;
6751 last_escape_glyph_face_id = it->face_id;
6752 last_escape_glyph_merged_face_id = face_id;
6753 }
6754
6755 XSETINT (it->ctl_chars[0], g);
6756 XSETINT (it->ctl_chars[1], c ^ 0100);
6757 ctl_len = 2;
6758 goto display_control;
6759 }
6760
6761 /* Handle non-ascii space in the mode where it only gets
6762 highlighting. */
6763
6764 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6765 {
6766 /* Merge `nobreak-space' into the current face. */
6767 face_id = merge_faces (it->f, Qnobreak_space, 0,
6768 it->face_id);
6769 XSETINT (it->ctl_chars[0], ' ');
6770 ctl_len = 1;
6771 goto display_control;
6772 }
6773
6774 /* Handle sequences that start with the "escape glyph". */
6775
6776 /* the default escape glyph is \. */
6777 escape_glyph = '\\';
6778
6779 if (it->dp
6780 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6781 {
6782 escape_glyph = GLYPH_CODE_CHAR (gc);
6783 lface_id = GLYPH_CODE_FACE (gc);
6784 }
6785 if (lface_id)
6786 {
6787 /* The display table specified a face.
6788 Merge it into face_id and also into escape_glyph. */
6789 face_id = merge_faces (it->f, Qt, lface_id,
6790 it->face_id);
6791 }
6792 else if (it->f == last_escape_glyph_frame
6793 && it->face_id == last_escape_glyph_face_id)
6794 {
6795 face_id = last_escape_glyph_merged_face_id;
6796 }
6797 else
6798 {
6799 /* Merge the escape-glyph face into the current face. */
6800 face_id = merge_faces (it->f, Qescape_glyph, 0,
6801 it->face_id);
6802 last_escape_glyph_frame = it->f;
6803 last_escape_glyph_face_id = it->face_id;
6804 last_escape_glyph_merged_face_id = face_id;
6805 }
6806
6807 /* Draw non-ASCII hyphen with just highlighting: */
6808
6809 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6810 {
6811 XSETINT (it->ctl_chars[0], '-');
6812 ctl_len = 1;
6813 goto display_control;
6814 }
6815
6816 /* Draw non-ASCII space/hyphen with escape glyph: */
6817
6818 if (nonascii_space_p || nonascii_hyphen_p)
6819 {
6820 XSETINT (it->ctl_chars[0], escape_glyph);
6821 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6822 ctl_len = 2;
6823 goto display_control;
6824 }
6825
6826 {
6827 char str[10];
6828 int len, i;
6829
6830 if (CHAR_BYTE8_P (c))
6831 /* Display \200 instead of \17777600. */
6832 c = CHAR_TO_BYTE8 (c);
6833 len = sprintf (str, "%03o", c);
6834
6835 XSETINT (it->ctl_chars[0], escape_glyph);
6836 for (i = 0; i < len; i++)
6837 XSETINT (it->ctl_chars[i + 1], str[i]);
6838 ctl_len = len + 1;
6839 }
6840
6841 display_control:
6842 /* Set up IT->dpvec and return first character from it. */
6843 it->dpvec_char_len = it->len;
6844 it->dpvec = it->ctl_chars;
6845 it->dpend = it->dpvec + ctl_len;
6846 it->current.dpvec_index = 0;
6847 it->dpvec_face_id = face_id;
6848 it->saved_face_id = it->face_id;
6849 it->method = GET_FROM_DISPLAY_VECTOR;
6850 it->ellipsis_p = 0;
6851 goto get_next;
6852 }
6853 it->char_to_display = c;
6854 }
6855 else if (success_p)
6856 {
6857 it->char_to_display = it->c;
6858 }
6859 }
6860
6861 /* Adjust face id for a multibyte character. There are no multibyte
6862 character in unibyte text. */
6863 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6864 && it->multibyte_p
6865 && success_p
6866 && FRAME_WINDOW_P (it->f))
6867 {
6868 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6869
6870 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6871 {
6872 /* Automatic composition with glyph-string. */
6873 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6874
6875 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6876 }
6877 else
6878 {
6879 ptrdiff_t pos = (it->s ? -1
6880 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6881 : IT_CHARPOS (*it));
6882 int c;
6883
6884 if (it->what == IT_CHARACTER)
6885 c = it->char_to_display;
6886 else
6887 {
6888 struct composition *cmp = composition_table[it->cmp_it.id];
6889 int i;
6890
6891 c = ' ';
6892 for (i = 0; i < cmp->glyph_len; i++)
6893 /* TAB in a composition means display glyphs with
6894 padding space on the left or right. */
6895 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6896 break;
6897 }
6898 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6899 }
6900 }
6901
6902 done:
6903 /* Is this character the last one of a run of characters with
6904 box? If yes, set IT->end_of_box_run_p to 1. */
6905 if (it->face_box_p
6906 && it->s == NULL)
6907 {
6908 if (it->method == GET_FROM_STRING && it->sp)
6909 {
6910 int face_id = underlying_face_id (it);
6911 struct face *face = FACE_FROM_ID (it->f, face_id);
6912
6913 if (face)
6914 {
6915 if (face->box == FACE_NO_BOX)
6916 {
6917 /* If the box comes from face properties in a
6918 display string, check faces in that string. */
6919 int string_face_id = face_after_it_pos (it);
6920 it->end_of_box_run_p
6921 = (FACE_FROM_ID (it->f, string_face_id)->box
6922 == FACE_NO_BOX);
6923 }
6924 /* Otherwise, the box comes from the underlying face.
6925 If this is the last string character displayed, check
6926 the next buffer location. */
6927 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6928 && (it->current.overlay_string_index
6929 == it->n_overlay_strings - 1))
6930 {
6931 ptrdiff_t ignore;
6932 int next_face_id;
6933 struct text_pos pos = it->current.pos;
6934 INC_TEXT_POS (pos, it->multibyte_p);
6935
6936 next_face_id = face_at_buffer_position
6937 (it->w, CHARPOS (pos), it->region_beg_charpos,
6938 it->region_end_charpos, &ignore,
6939 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6940 -1);
6941 it->end_of_box_run_p
6942 = (FACE_FROM_ID (it->f, next_face_id)->box
6943 == FACE_NO_BOX);
6944 }
6945 }
6946 }
6947 else
6948 {
6949 int face_id = face_after_it_pos (it);
6950 it->end_of_box_run_p
6951 = (face_id != it->face_id
6952 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6953 }
6954 }
6955 /* If we reached the end of the object we've been iterating (e.g., a
6956 display string or an overlay string), and there's something on
6957 IT->stack, proceed with what's on the stack. It doesn't make
6958 sense to return zero if there's unprocessed stuff on the stack,
6959 because otherwise that stuff will never be displayed. */
6960 if (!success_p && it->sp > 0)
6961 {
6962 set_iterator_to_next (it, 0);
6963 success_p = get_next_display_element (it);
6964 }
6965
6966 /* Value is 0 if end of buffer or string reached. */
6967 return success_p;
6968 }
6969
6970
6971 /* Move IT to the next display element.
6972
6973 RESEAT_P non-zero means if called on a newline in buffer text,
6974 skip to the next visible line start.
6975
6976 Functions get_next_display_element and set_iterator_to_next are
6977 separate because I find this arrangement easier to handle than a
6978 get_next_display_element function that also increments IT's
6979 position. The way it is we can first look at an iterator's current
6980 display element, decide whether it fits on a line, and if it does,
6981 increment the iterator position. The other way around we probably
6982 would either need a flag indicating whether the iterator has to be
6983 incremented the next time, or we would have to implement a
6984 decrement position function which would not be easy to write. */
6985
6986 void
6987 set_iterator_to_next (struct it *it, int reseat_p)
6988 {
6989 /* Reset flags indicating start and end of a sequence of characters
6990 with box. Reset them at the start of this function because
6991 moving the iterator to a new position might set them. */
6992 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6993
6994 switch (it->method)
6995 {
6996 case GET_FROM_BUFFER:
6997 /* The current display element of IT is a character from
6998 current_buffer. Advance in the buffer, and maybe skip over
6999 invisible lines that are so because of selective display. */
7000 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7001 reseat_at_next_visible_line_start (it, 0);
7002 else if (it->cmp_it.id >= 0)
7003 {
7004 /* We are currently getting glyphs from a composition. */
7005 int i;
7006
7007 if (! it->bidi_p)
7008 {
7009 IT_CHARPOS (*it) += it->cmp_it.nchars;
7010 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7011 if (it->cmp_it.to < it->cmp_it.nglyphs)
7012 {
7013 it->cmp_it.from = it->cmp_it.to;
7014 }
7015 else
7016 {
7017 it->cmp_it.id = -1;
7018 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7019 IT_BYTEPOS (*it),
7020 it->end_charpos, Qnil);
7021 }
7022 }
7023 else if (! it->cmp_it.reversed_p)
7024 {
7025 /* Composition created while scanning forward. */
7026 /* Update IT's char/byte positions to point to the first
7027 character of the next grapheme cluster, or to the
7028 character visually after the current composition. */
7029 for (i = 0; i < it->cmp_it.nchars; i++)
7030 bidi_move_to_visually_next (&it->bidi_it);
7031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7033
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7035 {
7036 /* Proceed to the next grapheme cluster. */
7037 it->cmp_it.from = it->cmp_it.to;
7038 }
7039 else
7040 {
7041 /* No more grapheme clusters in this composition.
7042 Find the next stop position. */
7043 ptrdiff_t stop = it->end_charpos;
7044 if (it->bidi_it.scan_dir < 0)
7045 /* Now we are scanning backward and don't know
7046 where to stop. */
7047 stop = -1;
7048 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7049 IT_BYTEPOS (*it), stop, Qnil);
7050 }
7051 }
7052 else
7053 {
7054 /* Composition created while scanning backward. */
7055 /* Update IT's char/byte positions to point to the last
7056 character of the previous grapheme cluster, or the
7057 character visually after the current composition. */
7058 for (i = 0; i < it->cmp_it.nchars; i++)
7059 bidi_move_to_visually_next (&it->bidi_it);
7060 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7061 IT_CHARPOS (*it) = it->bidi_it.charpos;
7062 if (it->cmp_it.from > 0)
7063 {
7064 /* Proceed to the previous grapheme cluster. */
7065 it->cmp_it.to = it->cmp_it.from;
7066 }
7067 else
7068 {
7069 /* No more grapheme clusters in this composition.
7070 Find the next stop position. */
7071 ptrdiff_t stop = it->end_charpos;
7072 if (it->bidi_it.scan_dir < 0)
7073 /* Now we are scanning backward and don't know
7074 where to stop. */
7075 stop = -1;
7076 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7077 IT_BYTEPOS (*it), stop, Qnil);
7078 }
7079 }
7080 }
7081 else
7082 {
7083 eassert (it->len != 0);
7084
7085 if (!it->bidi_p)
7086 {
7087 IT_BYTEPOS (*it) += it->len;
7088 IT_CHARPOS (*it) += 1;
7089 }
7090 else
7091 {
7092 int prev_scan_dir = it->bidi_it.scan_dir;
7093 /* If this is a new paragraph, determine its base
7094 direction (a.k.a. its base embedding level). */
7095 if (it->bidi_it.new_paragraph)
7096 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7097 bidi_move_to_visually_next (&it->bidi_it);
7098 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7099 IT_CHARPOS (*it) = it->bidi_it.charpos;
7100 if (prev_scan_dir != it->bidi_it.scan_dir)
7101 {
7102 /* As the scan direction was changed, we must
7103 re-compute the stop position for composition. */
7104 ptrdiff_t stop = it->end_charpos;
7105 if (it->bidi_it.scan_dir < 0)
7106 stop = -1;
7107 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7108 IT_BYTEPOS (*it), stop, Qnil);
7109 }
7110 }
7111 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7112 }
7113 break;
7114
7115 case GET_FROM_C_STRING:
7116 /* Current display element of IT is from a C string. */
7117 if (!it->bidi_p
7118 /* If the string position is beyond string's end, it means
7119 next_element_from_c_string is padding the string with
7120 blanks, in which case we bypass the bidi iterator,
7121 because it cannot deal with such virtual characters. */
7122 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7123 {
7124 IT_BYTEPOS (*it) += it->len;
7125 IT_CHARPOS (*it) += 1;
7126 }
7127 else
7128 {
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7132 }
7133 break;
7134
7135 case GET_FROM_DISPLAY_VECTOR:
7136 /* Current display element of IT is from a display table entry.
7137 Advance in the display table definition. Reset it to null if
7138 end reached, and continue with characters from buffers/
7139 strings. */
7140 ++it->current.dpvec_index;
7141
7142 /* Restore face of the iterator to what they were before the
7143 display vector entry (these entries may contain faces). */
7144 it->face_id = it->saved_face_id;
7145
7146 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7147 {
7148 int recheck_faces = it->ellipsis_p;
7149
7150 if (it->s)
7151 it->method = GET_FROM_C_STRING;
7152 else if (STRINGP (it->string))
7153 it->method = GET_FROM_STRING;
7154 else
7155 {
7156 it->method = GET_FROM_BUFFER;
7157 it->object = it->w->buffer;
7158 }
7159
7160 it->dpvec = NULL;
7161 it->current.dpvec_index = -1;
7162
7163 /* Skip over characters which were displayed via IT->dpvec. */
7164 if (it->dpvec_char_len < 0)
7165 reseat_at_next_visible_line_start (it, 1);
7166 else if (it->dpvec_char_len > 0)
7167 {
7168 if (it->method == GET_FROM_STRING
7169 && it->n_overlay_strings > 0)
7170 it->ignore_overlay_strings_at_pos_p = 1;
7171 it->len = it->dpvec_char_len;
7172 set_iterator_to_next (it, reseat_p);
7173 }
7174
7175 /* Maybe recheck faces after display vector */
7176 if (recheck_faces)
7177 it->stop_charpos = IT_CHARPOS (*it);
7178 }
7179 break;
7180
7181 case GET_FROM_STRING:
7182 /* Current display element is a character from a Lisp string. */
7183 eassert (it->s == NULL && STRINGP (it->string));
7184 /* Don't advance past string end. These conditions are true
7185 when set_iterator_to_next is called at the end of
7186 get_next_display_element, in which case the Lisp string is
7187 already exhausted, and all we want is pop the iterator
7188 stack. */
7189 if (it->current.overlay_string_index >= 0)
7190 {
7191 /* This is an overlay string, so there's no padding with
7192 spaces, and the number of characters in the string is
7193 where the string ends. */
7194 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7195 goto consider_string_end;
7196 }
7197 else
7198 {
7199 /* Not an overlay string. There could be padding, so test
7200 against it->end_charpos . */
7201 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7202 goto consider_string_end;
7203 }
7204 if (it->cmp_it.id >= 0)
7205 {
7206 int i;
7207
7208 if (! it->bidi_p)
7209 {
7210 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7211 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7212 if (it->cmp_it.to < it->cmp_it.nglyphs)
7213 it->cmp_it.from = it->cmp_it.to;
7214 else
7215 {
7216 it->cmp_it.id = -1;
7217 composition_compute_stop_pos (&it->cmp_it,
7218 IT_STRING_CHARPOS (*it),
7219 IT_STRING_BYTEPOS (*it),
7220 it->end_charpos, it->string);
7221 }
7222 }
7223 else if (! it->cmp_it.reversed_p)
7224 {
7225 for (i = 0; i < it->cmp_it.nchars; i++)
7226 bidi_move_to_visually_next (&it->bidi_it);
7227 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7228 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7229
7230 if (it->cmp_it.to < it->cmp_it.nglyphs)
7231 it->cmp_it.from = it->cmp_it.to;
7232 else
7233 {
7234 ptrdiff_t stop = it->end_charpos;
7235 if (it->bidi_it.scan_dir < 0)
7236 stop = -1;
7237 composition_compute_stop_pos (&it->cmp_it,
7238 IT_STRING_CHARPOS (*it),
7239 IT_STRING_BYTEPOS (*it), stop,
7240 it->string);
7241 }
7242 }
7243 else
7244 {
7245 for (i = 0; i < it->cmp_it.nchars; i++)
7246 bidi_move_to_visually_next (&it->bidi_it);
7247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7248 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7249 if (it->cmp_it.from > 0)
7250 it->cmp_it.to = it->cmp_it.from;
7251 else
7252 {
7253 ptrdiff_t stop = it->end_charpos;
7254 if (it->bidi_it.scan_dir < 0)
7255 stop = -1;
7256 composition_compute_stop_pos (&it->cmp_it,
7257 IT_STRING_CHARPOS (*it),
7258 IT_STRING_BYTEPOS (*it), stop,
7259 it->string);
7260 }
7261 }
7262 }
7263 else
7264 {
7265 if (!it->bidi_p
7266 /* If the string position is beyond string's end, it
7267 means next_element_from_string is padding the string
7268 with blanks, in which case we bypass the bidi
7269 iterator, because it cannot deal with such virtual
7270 characters. */
7271 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7272 {
7273 IT_STRING_BYTEPOS (*it) += it->len;
7274 IT_STRING_CHARPOS (*it) += 1;
7275 }
7276 else
7277 {
7278 int prev_scan_dir = it->bidi_it.scan_dir;
7279
7280 bidi_move_to_visually_next (&it->bidi_it);
7281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7282 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7283 if (prev_scan_dir != it->bidi_it.scan_dir)
7284 {
7285 ptrdiff_t stop = it->end_charpos;
7286
7287 if (it->bidi_it.scan_dir < 0)
7288 stop = -1;
7289 composition_compute_stop_pos (&it->cmp_it,
7290 IT_STRING_CHARPOS (*it),
7291 IT_STRING_BYTEPOS (*it), stop,
7292 it->string);
7293 }
7294 }
7295 }
7296
7297 consider_string_end:
7298
7299 if (it->current.overlay_string_index >= 0)
7300 {
7301 /* IT->string is an overlay string. Advance to the
7302 next, if there is one. */
7303 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7304 {
7305 it->ellipsis_p = 0;
7306 next_overlay_string (it);
7307 if (it->ellipsis_p)
7308 setup_for_ellipsis (it, 0);
7309 }
7310 }
7311 else
7312 {
7313 /* IT->string is not an overlay string. If we reached
7314 its end, and there is something on IT->stack, proceed
7315 with what is on the stack. This can be either another
7316 string, this time an overlay string, or a buffer. */
7317 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7318 && it->sp > 0)
7319 {
7320 pop_it (it);
7321 if (it->method == GET_FROM_STRING)
7322 goto consider_string_end;
7323 }
7324 }
7325 break;
7326
7327 case GET_FROM_IMAGE:
7328 case GET_FROM_STRETCH:
7329 /* The position etc with which we have to proceed are on
7330 the stack. The position may be at the end of a string,
7331 if the `display' property takes up the whole string. */
7332 eassert (it->sp > 0);
7333 pop_it (it);
7334 if (it->method == GET_FROM_STRING)
7335 goto consider_string_end;
7336 break;
7337
7338 default:
7339 /* There are no other methods defined, so this should be a bug. */
7340 emacs_abort ();
7341 }
7342
7343 eassert (it->method != GET_FROM_STRING
7344 || (STRINGP (it->string)
7345 && IT_STRING_CHARPOS (*it) >= 0));
7346 }
7347
7348 /* Load IT's display element fields with information about the next
7349 display element which comes from a display table entry or from the
7350 result of translating a control character to one of the forms `^C'
7351 or `\003'.
7352
7353 IT->dpvec holds the glyphs to return as characters.
7354 IT->saved_face_id holds the face id before the display vector--it
7355 is restored into IT->face_id in set_iterator_to_next. */
7356
7357 static int
7358 next_element_from_display_vector (struct it *it)
7359 {
7360 Lisp_Object gc;
7361
7362 /* Precondition. */
7363 eassert (it->dpvec && it->current.dpvec_index >= 0);
7364
7365 it->face_id = it->saved_face_id;
7366
7367 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7368 That seemed totally bogus - so I changed it... */
7369 gc = it->dpvec[it->current.dpvec_index];
7370
7371 if (GLYPH_CODE_P (gc))
7372 {
7373 it->c = GLYPH_CODE_CHAR (gc);
7374 it->len = CHAR_BYTES (it->c);
7375
7376 /* The entry may contain a face id to use. Such a face id is
7377 the id of a Lisp face, not a realized face. A face id of
7378 zero means no face is specified. */
7379 if (it->dpvec_face_id >= 0)
7380 it->face_id = it->dpvec_face_id;
7381 else
7382 {
7383 int lface_id = GLYPH_CODE_FACE (gc);
7384 if (lface_id > 0)
7385 it->face_id = merge_faces (it->f, Qt, lface_id,
7386 it->saved_face_id);
7387 }
7388 }
7389 else
7390 /* Display table entry is invalid. Return a space. */
7391 it->c = ' ', it->len = 1;
7392
7393 /* Don't change position and object of the iterator here. They are
7394 still the values of the character that had this display table
7395 entry or was translated, and that's what we want. */
7396 it->what = IT_CHARACTER;
7397 return 1;
7398 }
7399
7400 /* Get the first element of string/buffer in the visual order, after
7401 being reseated to a new position in a string or a buffer. */
7402 static void
7403 get_visually_first_element (struct it *it)
7404 {
7405 int string_p = STRINGP (it->string) || it->s;
7406 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7407 ptrdiff_t bob = (string_p ? 0 : BEGV);
7408
7409 if (STRINGP (it->string))
7410 {
7411 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7412 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7413 }
7414 else
7415 {
7416 it->bidi_it.charpos = IT_CHARPOS (*it);
7417 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7418 }
7419
7420 if (it->bidi_it.charpos == eob)
7421 {
7422 /* Nothing to do, but reset the FIRST_ELT flag, like
7423 bidi_paragraph_init does, because we are not going to
7424 call it. */
7425 it->bidi_it.first_elt = 0;
7426 }
7427 else if (it->bidi_it.charpos == bob
7428 || (!string_p
7429 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7430 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7431 {
7432 /* If we are at the beginning of a line/string, we can produce
7433 the next element right away. */
7434 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7435 bidi_move_to_visually_next (&it->bidi_it);
7436 }
7437 else
7438 {
7439 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7440
7441 /* We need to prime the bidi iterator starting at the line's or
7442 string's beginning, before we will be able to produce the
7443 next element. */
7444 if (string_p)
7445 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7446 else
7447 {
7448 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7449 -1);
7450 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7451 }
7452 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7453 do
7454 {
7455 /* Now return to buffer/string position where we were asked
7456 to get the next display element, and produce that. */
7457 bidi_move_to_visually_next (&it->bidi_it);
7458 }
7459 while (it->bidi_it.bytepos != orig_bytepos
7460 && it->bidi_it.charpos < eob);
7461 }
7462
7463 /* Adjust IT's position information to where we ended up. */
7464 if (STRINGP (it->string))
7465 {
7466 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7467 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7468 }
7469 else
7470 {
7471 IT_CHARPOS (*it) = it->bidi_it.charpos;
7472 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7473 }
7474
7475 if (STRINGP (it->string) || !it->s)
7476 {
7477 ptrdiff_t stop, charpos, bytepos;
7478
7479 if (STRINGP (it->string))
7480 {
7481 eassert (!it->s);
7482 stop = SCHARS (it->string);
7483 if (stop > it->end_charpos)
7484 stop = it->end_charpos;
7485 charpos = IT_STRING_CHARPOS (*it);
7486 bytepos = IT_STRING_BYTEPOS (*it);
7487 }
7488 else
7489 {
7490 stop = it->end_charpos;
7491 charpos = IT_CHARPOS (*it);
7492 bytepos = IT_BYTEPOS (*it);
7493 }
7494 if (it->bidi_it.scan_dir < 0)
7495 stop = -1;
7496 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7497 it->string);
7498 }
7499 }
7500
7501 /* Load IT with the next display element from Lisp string IT->string.
7502 IT->current.string_pos is the current position within the string.
7503 If IT->current.overlay_string_index >= 0, the Lisp string is an
7504 overlay string. */
7505
7506 static int
7507 next_element_from_string (struct it *it)
7508 {
7509 struct text_pos position;
7510
7511 eassert (STRINGP (it->string));
7512 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7513 eassert (IT_STRING_CHARPOS (*it) >= 0);
7514 position = it->current.string_pos;
7515
7516 /* With bidi reordering, the character to display might not be the
7517 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7518 that we were reseat()ed to a new string, whose paragraph
7519 direction is not known. */
7520 if (it->bidi_p && it->bidi_it.first_elt)
7521 {
7522 get_visually_first_element (it);
7523 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7524 }
7525
7526 /* Time to check for invisible text? */
7527 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7528 {
7529 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7530 {
7531 if (!(!it->bidi_p
7532 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7533 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7534 {
7535 /* With bidi non-linear iteration, we could find
7536 ourselves far beyond the last computed stop_charpos,
7537 with several other stop positions in between that we
7538 missed. Scan them all now, in buffer's logical
7539 order, until we find and handle the last stop_charpos
7540 that precedes our current position. */
7541 handle_stop_backwards (it, it->stop_charpos);
7542 return GET_NEXT_DISPLAY_ELEMENT (it);
7543 }
7544 else
7545 {
7546 if (it->bidi_p)
7547 {
7548 /* Take note of the stop position we just moved
7549 across, for when we will move back across it. */
7550 it->prev_stop = it->stop_charpos;
7551 /* If we are at base paragraph embedding level, take
7552 note of the last stop position seen at this
7553 level. */
7554 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7555 it->base_level_stop = it->stop_charpos;
7556 }
7557 handle_stop (it);
7558
7559 /* Since a handler may have changed IT->method, we must
7560 recurse here. */
7561 return GET_NEXT_DISPLAY_ELEMENT (it);
7562 }
7563 }
7564 else if (it->bidi_p
7565 /* If we are before prev_stop, we may have overstepped
7566 on our way backwards a stop_pos, and if so, we need
7567 to handle that stop_pos. */
7568 && IT_STRING_CHARPOS (*it) < it->prev_stop
7569 /* We can sometimes back up for reasons that have nothing
7570 to do with bidi reordering. E.g., compositions. The
7571 code below is only needed when we are above the base
7572 embedding level, so test for that explicitly. */
7573 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7574 {
7575 /* If we lost track of base_level_stop, we have no better
7576 place for handle_stop_backwards to start from than string
7577 beginning. This happens, e.g., when we were reseated to
7578 the previous screenful of text by vertical-motion. */
7579 if (it->base_level_stop <= 0
7580 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7581 it->base_level_stop = 0;
7582 handle_stop_backwards (it, it->base_level_stop);
7583 return GET_NEXT_DISPLAY_ELEMENT (it);
7584 }
7585 }
7586
7587 if (it->current.overlay_string_index >= 0)
7588 {
7589 /* Get the next character from an overlay string. In overlay
7590 strings, there is no field width or padding with spaces to
7591 do. */
7592 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7593 {
7594 it->what = IT_EOB;
7595 return 0;
7596 }
7597 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7598 IT_STRING_BYTEPOS (*it),
7599 it->bidi_it.scan_dir < 0
7600 ? -1
7601 : SCHARS (it->string))
7602 && next_element_from_composition (it))
7603 {
7604 return 1;
7605 }
7606 else if (STRING_MULTIBYTE (it->string))
7607 {
7608 const unsigned char *s = (SDATA (it->string)
7609 + IT_STRING_BYTEPOS (*it));
7610 it->c = string_char_and_length (s, &it->len);
7611 }
7612 else
7613 {
7614 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7615 it->len = 1;
7616 }
7617 }
7618 else
7619 {
7620 /* Get the next character from a Lisp string that is not an
7621 overlay string. Such strings come from the mode line, for
7622 example. We may have to pad with spaces, or truncate the
7623 string. See also next_element_from_c_string. */
7624 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7625 {
7626 it->what = IT_EOB;
7627 return 0;
7628 }
7629 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7630 {
7631 /* Pad with spaces. */
7632 it->c = ' ', it->len = 1;
7633 CHARPOS (position) = BYTEPOS (position) = -1;
7634 }
7635 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7636 IT_STRING_BYTEPOS (*it),
7637 it->bidi_it.scan_dir < 0
7638 ? -1
7639 : it->string_nchars)
7640 && next_element_from_composition (it))
7641 {
7642 return 1;
7643 }
7644 else if (STRING_MULTIBYTE (it->string))
7645 {
7646 const unsigned char *s = (SDATA (it->string)
7647 + IT_STRING_BYTEPOS (*it));
7648 it->c = string_char_and_length (s, &it->len);
7649 }
7650 else
7651 {
7652 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7653 it->len = 1;
7654 }
7655 }
7656
7657 /* Record what we have and where it came from. */
7658 it->what = IT_CHARACTER;
7659 it->object = it->string;
7660 it->position = position;
7661 return 1;
7662 }
7663
7664
7665 /* Load IT with next display element from C string IT->s.
7666 IT->string_nchars is the maximum number of characters to return
7667 from the string. IT->end_charpos may be greater than
7668 IT->string_nchars when this function is called, in which case we
7669 may have to return padding spaces. Value is zero if end of string
7670 reached, including padding spaces. */
7671
7672 static int
7673 next_element_from_c_string (struct it *it)
7674 {
7675 int success_p = 1;
7676
7677 eassert (it->s);
7678 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7679 it->what = IT_CHARACTER;
7680 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7681 it->object = Qnil;
7682
7683 /* With bidi reordering, the character to display might not be the
7684 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7685 we were reseated to a new string, whose paragraph direction is
7686 not known. */
7687 if (it->bidi_p && it->bidi_it.first_elt)
7688 get_visually_first_element (it);
7689
7690 /* IT's position can be greater than IT->string_nchars in case a
7691 field width or precision has been specified when the iterator was
7692 initialized. */
7693 if (IT_CHARPOS (*it) >= it->end_charpos)
7694 {
7695 /* End of the game. */
7696 it->what = IT_EOB;
7697 success_p = 0;
7698 }
7699 else if (IT_CHARPOS (*it) >= it->string_nchars)
7700 {
7701 /* Pad with spaces. */
7702 it->c = ' ', it->len = 1;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7704 }
7705 else if (it->multibyte_p)
7706 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7707 else
7708 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7709
7710 return success_p;
7711 }
7712
7713
7714 /* Set up IT to return characters from an ellipsis, if appropriate.
7715 The definition of the ellipsis glyphs may come from a display table
7716 entry. This function fills IT with the first glyph from the
7717 ellipsis if an ellipsis is to be displayed. */
7718
7719 static int
7720 next_element_from_ellipsis (struct it *it)
7721 {
7722 if (it->selective_display_ellipsis_p)
7723 setup_for_ellipsis (it, it->len);
7724 else
7725 {
7726 /* The face at the current position may be different from the
7727 face we find after the invisible text. Remember what it
7728 was in IT->saved_face_id, and signal that it's there by
7729 setting face_before_selective_p. */
7730 it->saved_face_id = it->face_id;
7731 it->method = GET_FROM_BUFFER;
7732 it->object = it->w->buffer;
7733 reseat_at_next_visible_line_start (it, 1);
7734 it->face_before_selective_p = 1;
7735 }
7736
7737 return GET_NEXT_DISPLAY_ELEMENT (it);
7738 }
7739
7740
7741 /* Deliver an image display element. The iterator IT is already
7742 filled with image information (done in handle_display_prop). Value
7743 is always 1. */
7744
7745
7746 static int
7747 next_element_from_image (struct it *it)
7748 {
7749 it->what = IT_IMAGE;
7750 it->ignore_overlay_strings_at_pos_p = 0;
7751 return 1;
7752 }
7753
7754
7755 /* Fill iterator IT with next display element from a stretch glyph
7756 property. IT->object is the value of the text property. Value is
7757 always 1. */
7758
7759 static int
7760 next_element_from_stretch (struct it *it)
7761 {
7762 it->what = IT_STRETCH;
7763 return 1;
7764 }
7765
7766 /* Scan backwards from IT's current position until we find a stop
7767 position, or until BEGV. This is called when we find ourself
7768 before both the last known prev_stop and base_level_stop while
7769 reordering bidirectional text. */
7770
7771 static void
7772 compute_stop_pos_backwards (struct it *it)
7773 {
7774 const int SCAN_BACK_LIMIT = 1000;
7775 struct text_pos pos;
7776 struct display_pos save_current = it->current;
7777 struct text_pos save_position = it->position;
7778 ptrdiff_t charpos = IT_CHARPOS (*it);
7779 ptrdiff_t where_we_are = charpos;
7780 ptrdiff_t save_stop_pos = it->stop_charpos;
7781 ptrdiff_t save_end_pos = it->end_charpos;
7782
7783 eassert (NILP (it->string) && !it->s);
7784 eassert (it->bidi_p);
7785 it->bidi_p = 0;
7786 do
7787 {
7788 it->end_charpos = min (charpos + 1, ZV);
7789 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7790 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7791 reseat_1 (it, pos, 0);
7792 compute_stop_pos (it);
7793 /* We must advance forward, right? */
7794 if (it->stop_charpos <= charpos)
7795 emacs_abort ();
7796 }
7797 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7798
7799 if (it->stop_charpos <= where_we_are)
7800 it->prev_stop = it->stop_charpos;
7801 else
7802 it->prev_stop = BEGV;
7803 it->bidi_p = 1;
7804 it->current = save_current;
7805 it->position = save_position;
7806 it->stop_charpos = save_stop_pos;
7807 it->end_charpos = save_end_pos;
7808 }
7809
7810 /* Scan forward from CHARPOS in the current buffer/string, until we
7811 find a stop position > current IT's position. Then handle the stop
7812 position before that. This is called when we bump into a stop
7813 position while reordering bidirectional text. CHARPOS should be
7814 the last previously processed stop_pos (or BEGV/0, if none were
7815 processed yet) whose position is less that IT's current
7816 position. */
7817
7818 static void
7819 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7820 {
7821 int bufp = !STRINGP (it->string);
7822 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7823 struct display_pos save_current = it->current;
7824 struct text_pos save_position = it->position;
7825 struct text_pos pos1;
7826 ptrdiff_t next_stop;
7827
7828 /* Scan in strict logical order. */
7829 eassert (it->bidi_p);
7830 it->bidi_p = 0;
7831 do
7832 {
7833 it->prev_stop = charpos;
7834 if (bufp)
7835 {
7836 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7837 reseat_1 (it, pos1, 0);
7838 }
7839 else
7840 it->current.string_pos = string_pos (charpos, it->string);
7841 compute_stop_pos (it);
7842 /* We must advance forward, right? */
7843 if (it->stop_charpos <= it->prev_stop)
7844 emacs_abort ();
7845 charpos = it->stop_charpos;
7846 }
7847 while (charpos <= where_we_are);
7848
7849 it->bidi_p = 1;
7850 it->current = save_current;
7851 it->position = save_position;
7852 next_stop = it->stop_charpos;
7853 it->stop_charpos = it->prev_stop;
7854 handle_stop (it);
7855 it->stop_charpos = next_stop;
7856 }
7857
7858 /* Load IT with the next display element from current_buffer. Value
7859 is zero if end of buffer reached. IT->stop_charpos is the next
7860 position at which to stop and check for text properties or buffer
7861 end. */
7862
7863 static int
7864 next_element_from_buffer (struct it *it)
7865 {
7866 int success_p = 1;
7867
7868 eassert (IT_CHARPOS (*it) >= BEGV);
7869 eassert (NILP (it->string) && !it->s);
7870 eassert (!it->bidi_p
7871 || (EQ (it->bidi_it.string.lstring, Qnil)
7872 && it->bidi_it.string.s == NULL));
7873
7874 /* With bidi reordering, the character to display might not be the
7875 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7876 we were reseat()ed to a new buffer position, which is potentially
7877 a different paragraph. */
7878 if (it->bidi_p && it->bidi_it.first_elt)
7879 {
7880 get_visually_first_element (it);
7881 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7882 }
7883
7884 if (IT_CHARPOS (*it) >= it->stop_charpos)
7885 {
7886 if (IT_CHARPOS (*it) >= it->end_charpos)
7887 {
7888 int overlay_strings_follow_p;
7889
7890 /* End of the game, except when overlay strings follow that
7891 haven't been returned yet. */
7892 if (it->overlay_strings_at_end_processed_p)
7893 overlay_strings_follow_p = 0;
7894 else
7895 {
7896 it->overlay_strings_at_end_processed_p = 1;
7897 overlay_strings_follow_p = get_overlay_strings (it, 0);
7898 }
7899
7900 if (overlay_strings_follow_p)
7901 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7902 else
7903 {
7904 it->what = IT_EOB;
7905 it->position = it->current.pos;
7906 success_p = 0;
7907 }
7908 }
7909 else if (!(!it->bidi_p
7910 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7911 || IT_CHARPOS (*it) == it->stop_charpos))
7912 {
7913 /* With bidi non-linear iteration, we could find ourselves
7914 far beyond the last computed stop_charpos, with several
7915 other stop positions in between that we missed. Scan
7916 them all now, in buffer's logical order, until we find
7917 and handle the last stop_charpos that precedes our
7918 current position. */
7919 handle_stop_backwards (it, it->stop_charpos);
7920 return GET_NEXT_DISPLAY_ELEMENT (it);
7921 }
7922 else
7923 {
7924 if (it->bidi_p)
7925 {
7926 /* Take note of the stop position we just moved across,
7927 for when we will move back across it. */
7928 it->prev_stop = it->stop_charpos;
7929 /* If we are at base paragraph embedding level, take
7930 note of the last stop position seen at this
7931 level. */
7932 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7933 it->base_level_stop = it->stop_charpos;
7934 }
7935 handle_stop (it);
7936 return GET_NEXT_DISPLAY_ELEMENT (it);
7937 }
7938 }
7939 else if (it->bidi_p
7940 /* If we are before prev_stop, we may have overstepped on
7941 our way backwards a stop_pos, and if so, we need to
7942 handle that stop_pos. */
7943 && IT_CHARPOS (*it) < it->prev_stop
7944 /* We can sometimes back up for reasons that have nothing
7945 to do with bidi reordering. E.g., compositions. The
7946 code below is only needed when we are above the base
7947 embedding level, so test for that explicitly. */
7948 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7949 {
7950 if (it->base_level_stop <= 0
7951 || IT_CHARPOS (*it) < it->base_level_stop)
7952 {
7953 /* If we lost track of base_level_stop, we need to find
7954 prev_stop by looking backwards. This happens, e.g., when
7955 we were reseated to the previous screenful of text by
7956 vertical-motion. */
7957 it->base_level_stop = BEGV;
7958 compute_stop_pos_backwards (it);
7959 handle_stop_backwards (it, it->prev_stop);
7960 }
7961 else
7962 handle_stop_backwards (it, it->base_level_stop);
7963 return GET_NEXT_DISPLAY_ELEMENT (it);
7964 }
7965 else
7966 {
7967 /* No face changes, overlays etc. in sight, so just return a
7968 character from current_buffer. */
7969 unsigned char *p;
7970 ptrdiff_t stop;
7971
7972 /* Maybe run the redisplay end trigger hook. Performance note:
7973 This doesn't seem to cost measurable time. */
7974 if (it->redisplay_end_trigger_charpos
7975 && it->glyph_row
7976 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7977 run_redisplay_end_trigger_hook (it);
7978
7979 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7980 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7981 stop)
7982 && next_element_from_composition (it))
7983 {
7984 return 1;
7985 }
7986
7987 /* Get the next character, maybe multibyte. */
7988 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7989 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7990 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7991 else
7992 it->c = *p, it->len = 1;
7993
7994 /* Record what we have and where it came from. */
7995 it->what = IT_CHARACTER;
7996 it->object = it->w->buffer;
7997 it->position = it->current.pos;
7998
7999 /* Normally we return the character found above, except when we
8000 really want to return an ellipsis for selective display. */
8001 if (it->selective)
8002 {
8003 if (it->c == '\n')
8004 {
8005 /* A value of selective > 0 means hide lines indented more
8006 than that number of columns. */
8007 if (it->selective > 0
8008 && IT_CHARPOS (*it) + 1 < ZV
8009 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8010 IT_BYTEPOS (*it) + 1,
8011 it->selective))
8012 {
8013 success_p = next_element_from_ellipsis (it);
8014 it->dpvec_char_len = -1;
8015 }
8016 }
8017 else if (it->c == '\r' && it->selective == -1)
8018 {
8019 /* A value of selective == -1 means that everything from the
8020 CR to the end of the line is invisible, with maybe an
8021 ellipsis displayed for it. */
8022 success_p = next_element_from_ellipsis (it);
8023 it->dpvec_char_len = -1;
8024 }
8025 }
8026 }
8027
8028 /* Value is zero if end of buffer reached. */
8029 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8030 return success_p;
8031 }
8032
8033
8034 /* Run the redisplay end trigger hook for IT. */
8035
8036 static void
8037 run_redisplay_end_trigger_hook (struct it *it)
8038 {
8039 Lisp_Object args[3];
8040
8041 /* IT->glyph_row should be non-null, i.e. we should be actually
8042 displaying something, or otherwise we should not run the hook. */
8043 eassert (it->glyph_row);
8044
8045 /* Set up hook arguments. */
8046 args[0] = Qredisplay_end_trigger_functions;
8047 args[1] = it->window;
8048 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8049 it->redisplay_end_trigger_charpos = 0;
8050
8051 /* Since we are *trying* to run these functions, don't try to run
8052 them again, even if they get an error. */
8053 wset_redisplay_end_trigger (it->w, Qnil);
8054 Frun_hook_with_args (3, args);
8055
8056 /* Notice if it changed the face of the character we are on. */
8057 handle_face_prop (it);
8058 }
8059
8060
8061 /* Deliver a composition display element. Unlike the other
8062 next_element_from_XXX, this function is not registered in the array
8063 get_next_element[]. It is called from next_element_from_buffer and
8064 next_element_from_string when necessary. */
8065
8066 static int
8067 next_element_from_composition (struct it *it)
8068 {
8069 it->what = IT_COMPOSITION;
8070 it->len = it->cmp_it.nbytes;
8071 if (STRINGP (it->string))
8072 {
8073 if (it->c < 0)
8074 {
8075 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8076 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8077 return 0;
8078 }
8079 it->position = it->current.string_pos;
8080 it->object = it->string;
8081 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8082 IT_STRING_BYTEPOS (*it), it->string);
8083 }
8084 else
8085 {
8086 if (it->c < 0)
8087 {
8088 IT_CHARPOS (*it) += it->cmp_it.nchars;
8089 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8090 if (it->bidi_p)
8091 {
8092 if (it->bidi_it.new_paragraph)
8093 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8094 /* Resync the bidi iterator with IT's new position.
8095 FIXME: this doesn't support bidirectional text. */
8096 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8097 bidi_move_to_visually_next (&it->bidi_it);
8098 }
8099 return 0;
8100 }
8101 it->position = it->current.pos;
8102 it->object = it->w->buffer;
8103 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8104 IT_BYTEPOS (*it), Qnil);
8105 }
8106 return 1;
8107 }
8108
8109
8110 \f
8111 /***********************************************************************
8112 Moving an iterator without producing glyphs
8113 ***********************************************************************/
8114
8115 /* Check if iterator is at a position corresponding to a valid buffer
8116 position after some move_it_ call. */
8117
8118 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8119 ((it)->method == GET_FROM_STRING \
8120 ? IT_STRING_CHARPOS (*it) == 0 \
8121 : 1)
8122
8123
8124 /* Move iterator IT to a specified buffer or X position within one
8125 line on the display without producing glyphs.
8126
8127 OP should be a bit mask including some or all of these bits:
8128 MOVE_TO_X: Stop upon reaching x-position TO_X.
8129 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8130 Regardless of OP's value, stop upon reaching the end of the display line.
8131
8132 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8133 This means, in particular, that TO_X includes window's horizontal
8134 scroll amount.
8135
8136 The return value has several possible values that
8137 say what condition caused the scan to stop:
8138
8139 MOVE_POS_MATCH_OR_ZV
8140 - when TO_POS or ZV was reached.
8141
8142 MOVE_X_REACHED
8143 -when TO_X was reached before TO_POS or ZV were reached.
8144
8145 MOVE_LINE_CONTINUED
8146 - when we reached the end of the display area and the line must
8147 be continued.
8148
8149 MOVE_LINE_TRUNCATED
8150 - when we reached the end of the display area and the line is
8151 truncated.
8152
8153 MOVE_NEWLINE_OR_CR
8154 - when we stopped at a line end, i.e. a newline or a CR and selective
8155 display is on. */
8156
8157 static enum move_it_result
8158 move_it_in_display_line_to (struct it *it,
8159 ptrdiff_t to_charpos, int to_x,
8160 enum move_operation_enum op)
8161 {
8162 enum move_it_result result = MOVE_UNDEFINED;
8163 struct glyph_row *saved_glyph_row;
8164 struct it wrap_it, atpos_it, atx_it, ppos_it;
8165 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8166 void *ppos_data = NULL;
8167 int may_wrap = 0;
8168 enum it_method prev_method = it->method;
8169 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8170 int saw_smaller_pos = prev_pos < to_charpos;
8171
8172 /* Don't produce glyphs in produce_glyphs. */
8173 saved_glyph_row = it->glyph_row;
8174 it->glyph_row = NULL;
8175
8176 /* Use wrap_it to save a copy of IT wherever a word wrap could
8177 occur. Use atpos_it to save a copy of IT at the desired buffer
8178 position, if found, so that we can scan ahead and check if the
8179 word later overshoots the window edge. Use atx_it similarly, for
8180 pixel positions. */
8181 wrap_it.sp = -1;
8182 atpos_it.sp = -1;
8183 atx_it.sp = -1;
8184
8185 /* Use ppos_it under bidi reordering to save a copy of IT for the
8186 position > CHARPOS that is the closest to CHARPOS. We restore
8187 that position in IT when we have scanned the entire display line
8188 without finding a match for CHARPOS and all the character
8189 positions are greater than CHARPOS. */
8190 if (it->bidi_p)
8191 {
8192 SAVE_IT (ppos_it, *it, ppos_data);
8193 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8194 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8195 SAVE_IT (ppos_it, *it, ppos_data);
8196 }
8197
8198 #define BUFFER_POS_REACHED_P() \
8199 ((op & MOVE_TO_POS) != 0 \
8200 && BUFFERP (it->object) \
8201 && (IT_CHARPOS (*it) == to_charpos \
8202 || ((!it->bidi_p \
8203 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8204 && IT_CHARPOS (*it) > to_charpos) \
8205 || (it->what == IT_COMPOSITION \
8206 && ((IT_CHARPOS (*it) > to_charpos \
8207 && to_charpos >= it->cmp_it.charpos) \
8208 || (IT_CHARPOS (*it) < to_charpos \
8209 && to_charpos <= it->cmp_it.charpos)))) \
8210 && (it->method == GET_FROM_BUFFER \
8211 || (it->method == GET_FROM_DISPLAY_VECTOR \
8212 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8213
8214 /* If there's a line-/wrap-prefix, handle it. */
8215 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8216 && it->current_y < it->last_visible_y)
8217 handle_line_prefix (it);
8218
8219 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8220 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8221
8222 while (1)
8223 {
8224 int x, i, ascent = 0, descent = 0;
8225
8226 /* Utility macro to reset an iterator with x, ascent, and descent. */
8227 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8228 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8229 (IT)->max_descent = descent)
8230
8231 /* Stop if we move beyond TO_CHARPOS (after an image or a
8232 display string or stretch glyph). */
8233 if ((op & MOVE_TO_POS) != 0
8234 && BUFFERP (it->object)
8235 && it->method == GET_FROM_BUFFER
8236 && (((!it->bidi_p
8237 /* When the iterator is at base embedding level, we
8238 are guaranteed that characters are delivered for
8239 display in strictly increasing order of their
8240 buffer positions. */
8241 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8242 && IT_CHARPOS (*it) > to_charpos)
8243 || (it->bidi_p
8244 && (prev_method == GET_FROM_IMAGE
8245 || prev_method == GET_FROM_STRETCH
8246 || prev_method == GET_FROM_STRING)
8247 /* Passed TO_CHARPOS from left to right. */
8248 && ((prev_pos < to_charpos
8249 && IT_CHARPOS (*it) > to_charpos)
8250 /* Passed TO_CHARPOS from right to left. */
8251 || (prev_pos > to_charpos
8252 && IT_CHARPOS (*it) < to_charpos)))))
8253 {
8254 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8255 {
8256 result = MOVE_POS_MATCH_OR_ZV;
8257 break;
8258 }
8259 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8260 /* If wrap_it is valid, the current position might be in a
8261 word that is wrapped. So, save the iterator in
8262 atpos_it and continue to see if wrapping happens. */
8263 SAVE_IT (atpos_it, *it, atpos_data);
8264 }
8265
8266 /* Stop when ZV reached.
8267 We used to stop here when TO_CHARPOS reached as well, but that is
8268 too soon if this glyph does not fit on this line. So we handle it
8269 explicitly below. */
8270 if (!get_next_display_element (it))
8271 {
8272 result = MOVE_POS_MATCH_OR_ZV;
8273 break;
8274 }
8275
8276 if (it->line_wrap == TRUNCATE)
8277 {
8278 if (BUFFER_POS_REACHED_P ())
8279 {
8280 result = MOVE_POS_MATCH_OR_ZV;
8281 break;
8282 }
8283 }
8284 else
8285 {
8286 if (it->line_wrap == WORD_WRAP)
8287 {
8288 if (IT_DISPLAYING_WHITESPACE (it))
8289 may_wrap = 1;
8290 else if (may_wrap)
8291 {
8292 /* We have reached a glyph that follows one or more
8293 whitespace characters. If the position is
8294 already found, we are done. */
8295 if (atpos_it.sp >= 0)
8296 {
8297 RESTORE_IT (it, &atpos_it, atpos_data);
8298 result = MOVE_POS_MATCH_OR_ZV;
8299 goto done;
8300 }
8301 if (atx_it.sp >= 0)
8302 {
8303 RESTORE_IT (it, &atx_it, atx_data);
8304 result = MOVE_X_REACHED;
8305 goto done;
8306 }
8307 /* Otherwise, we can wrap here. */
8308 SAVE_IT (wrap_it, *it, wrap_data);
8309 may_wrap = 0;
8310 }
8311 }
8312 }
8313
8314 /* Remember the line height for the current line, in case
8315 the next element doesn't fit on the line. */
8316 ascent = it->max_ascent;
8317 descent = it->max_descent;
8318
8319 /* The call to produce_glyphs will get the metrics of the
8320 display element IT is loaded with. Record the x-position
8321 before this display element, in case it doesn't fit on the
8322 line. */
8323 x = it->current_x;
8324
8325 PRODUCE_GLYPHS (it);
8326
8327 if (it->area != TEXT_AREA)
8328 {
8329 prev_method = it->method;
8330 if (it->method == GET_FROM_BUFFER)
8331 prev_pos = IT_CHARPOS (*it);
8332 set_iterator_to_next (it, 1);
8333 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8334 SET_TEXT_POS (this_line_min_pos,
8335 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8336 if (it->bidi_p
8337 && (op & MOVE_TO_POS)
8338 && IT_CHARPOS (*it) > to_charpos
8339 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8340 SAVE_IT (ppos_it, *it, ppos_data);
8341 continue;
8342 }
8343
8344 /* The number of glyphs we get back in IT->nglyphs will normally
8345 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8346 character on a terminal frame, or (iii) a line end. For the
8347 second case, IT->nglyphs - 1 padding glyphs will be present.
8348 (On X frames, there is only one glyph produced for a
8349 composite character.)
8350
8351 The behavior implemented below means, for continuation lines,
8352 that as many spaces of a TAB as fit on the current line are
8353 displayed there. For terminal frames, as many glyphs of a
8354 multi-glyph character are displayed in the current line, too.
8355 This is what the old redisplay code did, and we keep it that
8356 way. Under X, the whole shape of a complex character must
8357 fit on the line or it will be completely displayed in the
8358 next line.
8359
8360 Note that both for tabs and padding glyphs, all glyphs have
8361 the same width. */
8362 if (it->nglyphs)
8363 {
8364 /* More than one glyph or glyph doesn't fit on line. All
8365 glyphs have the same width. */
8366 int single_glyph_width = it->pixel_width / it->nglyphs;
8367 int new_x;
8368 int x_before_this_char = x;
8369 int hpos_before_this_char = it->hpos;
8370
8371 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8372 {
8373 new_x = x + single_glyph_width;
8374
8375 /* We want to leave anything reaching TO_X to the caller. */
8376 if ((op & MOVE_TO_X) && new_x > to_x)
8377 {
8378 if (BUFFER_POS_REACHED_P ())
8379 {
8380 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8381 goto buffer_pos_reached;
8382 if (atpos_it.sp < 0)
8383 {
8384 SAVE_IT (atpos_it, *it, atpos_data);
8385 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8386 }
8387 }
8388 else
8389 {
8390 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8391 {
8392 it->current_x = x;
8393 result = MOVE_X_REACHED;
8394 break;
8395 }
8396 if (atx_it.sp < 0)
8397 {
8398 SAVE_IT (atx_it, *it, atx_data);
8399 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8400 }
8401 }
8402 }
8403
8404 if (/* Lines are continued. */
8405 it->line_wrap != TRUNCATE
8406 && (/* And glyph doesn't fit on the line. */
8407 new_x > it->last_visible_x
8408 /* Or it fits exactly and we're on a window
8409 system frame. */
8410 || (new_x == it->last_visible_x
8411 && FRAME_WINDOW_P (it->f)
8412 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8413 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8414 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8415 {
8416 if (/* IT->hpos == 0 means the very first glyph
8417 doesn't fit on the line, e.g. a wide image. */
8418 it->hpos == 0
8419 || (new_x == it->last_visible_x
8420 && FRAME_WINDOW_P (it->f)))
8421 {
8422 ++it->hpos;
8423 it->current_x = new_x;
8424
8425 /* The character's last glyph just barely fits
8426 in this row. */
8427 if (i == it->nglyphs - 1)
8428 {
8429 /* If this is the destination position,
8430 return a position *before* it in this row,
8431 now that we know it fits in this row. */
8432 if (BUFFER_POS_REACHED_P ())
8433 {
8434 if (it->line_wrap != WORD_WRAP
8435 || wrap_it.sp < 0)
8436 {
8437 it->hpos = hpos_before_this_char;
8438 it->current_x = x_before_this_char;
8439 result = MOVE_POS_MATCH_OR_ZV;
8440 break;
8441 }
8442 if (it->line_wrap == WORD_WRAP
8443 && atpos_it.sp < 0)
8444 {
8445 SAVE_IT (atpos_it, *it, atpos_data);
8446 atpos_it.current_x = x_before_this_char;
8447 atpos_it.hpos = hpos_before_this_char;
8448 }
8449 }
8450
8451 prev_method = it->method;
8452 if (it->method == GET_FROM_BUFFER)
8453 prev_pos = IT_CHARPOS (*it);
8454 set_iterator_to_next (it, 1);
8455 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8456 SET_TEXT_POS (this_line_min_pos,
8457 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8458 /* On graphical terminals, newlines may
8459 "overflow" into the fringe if
8460 overflow-newline-into-fringe is non-nil.
8461 On text terminals, and on graphical
8462 terminals with no right margin, newlines
8463 may overflow into the last glyph on the
8464 display line.*/
8465 if (!FRAME_WINDOW_P (it->f)
8466 || ((it->bidi_p
8467 && it->bidi_it.paragraph_dir == R2L)
8468 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8469 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8470 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8471 {
8472 if (!get_next_display_element (it))
8473 {
8474 result = MOVE_POS_MATCH_OR_ZV;
8475 break;
8476 }
8477 if (BUFFER_POS_REACHED_P ())
8478 {
8479 if (ITERATOR_AT_END_OF_LINE_P (it))
8480 result = MOVE_POS_MATCH_OR_ZV;
8481 else
8482 result = MOVE_LINE_CONTINUED;
8483 break;
8484 }
8485 if (ITERATOR_AT_END_OF_LINE_P (it))
8486 {
8487 result = MOVE_NEWLINE_OR_CR;
8488 break;
8489 }
8490 }
8491 }
8492 }
8493 else
8494 IT_RESET_X_ASCENT_DESCENT (it);
8495
8496 if (wrap_it.sp >= 0)
8497 {
8498 RESTORE_IT (it, &wrap_it, wrap_data);
8499 atpos_it.sp = -1;
8500 atx_it.sp = -1;
8501 }
8502
8503 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8504 IT_CHARPOS (*it)));
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8507 }
8508
8509 if (BUFFER_POS_REACHED_P ())
8510 {
8511 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8512 goto buffer_pos_reached;
8513 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8514 {
8515 SAVE_IT (atpos_it, *it, atpos_data);
8516 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8517 }
8518 }
8519
8520 if (new_x > it->first_visible_x)
8521 {
8522 /* Glyph is visible. Increment number of glyphs that
8523 would be displayed. */
8524 ++it->hpos;
8525 }
8526 }
8527
8528 if (result != MOVE_UNDEFINED)
8529 break;
8530 }
8531 else if (BUFFER_POS_REACHED_P ())
8532 {
8533 buffer_pos_reached:
8534 IT_RESET_X_ASCENT_DESCENT (it);
8535 result = MOVE_POS_MATCH_OR_ZV;
8536 break;
8537 }
8538 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8539 {
8540 /* Stop when TO_X specified and reached. This check is
8541 necessary here because of lines consisting of a line end,
8542 only. The line end will not produce any glyphs and we
8543 would never get MOVE_X_REACHED. */
8544 eassert (it->nglyphs == 0);
8545 result = MOVE_X_REACHED;
8546 break;
8547 }
8548
8549 /* Is this a line end? If yes, we're done. */
8550 if (ITERATOR_AT_END_OF_LINE_P (it))
8551 {
8552 /* If we are past TO_CHARPOS, but never saw any character
8553 positions smaller than TO_CHARPOS, return
8554 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8555 did. */
8556 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8557 {
8558 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8559 {
8560 if (IT_CHARPOS (ppos_it) < ZV)
8561 {
8562 RESTORE_IT (it, &ppos_it, ppos_data);
8563 result = MOVE_POS_MATCH_OR_ZV;
8564 }
8565 else
8566 goto buffer_pos_reached;
8567 }
8568 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8569 && IT_CHARPOS (*it) > to_charpos)
8570 goto buffer_pos_reached;
8571 else
8572 result = MOVE_NEWLINE_OR_CR;
8573 }
8574 else
8575 result = MOVE_NEWLINE_OR_CR;
8576 break;
8577 }
8578
8579 prev_method = it->method;
8580 if (it->method == GET_FROM_BUFFER)
8581 prev_pos = IT_CHARPOS (*it);
8582 /* The current display element has been consumed. Advance
8583 to the next. */
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8587 if (IT_CHARPOS (*it) < to_charpos)
8588 saw_smaller_pos = 1;
8589 if (it->bidi_p
8590 && (op & MOVE_TO_POS)
8591 && IT_CHARPOS (*it) >= to_charpos
8592 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8593 SAVE_IT (ppos_it, *it, ppos_data);
8594
8595 /* Stop if lines are truncated and IT's current x-position is
8596 past the right edge of the window now. */
8597 if (it->line_wrap == TRUNCATE
8598 && it->current_x >= it->last_visible_x)
8599 {
8600 if (!FRAME_WINDOW_P (it->f)
8601 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8602 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8603 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8604 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8605 {
8606 int at_eob_p = 0;
8607
8608 if ((at_eob_p = !get_next_display_element (it))
8609 || BUFFER_POS_REACHED_P ()
8610 /* If we are past TO_CHARPOS, but never saw any
8611 character positions smaller than TO_CHARPOS,
8612 return MOVE_POS_MATCH_OR_ZV, like the
8613 unidirectional display did. */
8614 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8615 && !saw_smaller_pos
8616 && IT_CHARPOS (*it) > to_charpos))
8617 {
8618 if (it->bidi_p
8619 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8620 RESTORE_IT (it, &ppos_it, ppos_data);
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8623 }
8624 if (ITERATOR_AT_END_OF_LINE_P (it))
8625 {
8626 result = MOVE_NEWLINE_OR_CR;
8627 break;
8628 }
8629 }
8630 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8631 && !saw_smaller_pos
8632 && IT_CHARPOS (*it) > to_charpos)
8633 {
8634 if (IT_CHARPOS (ppos_it) < ZV)
8635 RESTORE_IT (it, &ppos_it, ppos_data);
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8638 }
8639 result = MOVE_LINE_TRUNCATED;
8640 break;
8641 }
8642 #undef IT_RESET_X_ASCENT_DESCENT
8643 }
8644
8645 #undef BUFFER_POS_REACHED_P
8646
8647 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8648 restore the saved iterator. */
8649 if (atpos_it.sp >= 0)
8650 RESTORE_IT (it, &atpos_it, atpos_data);
8651 else if (atx_it.sp >= 0)
8652 RESTORE_IT (it, &atx_it, atx_data);
8653
8654 done:
8655
8656 if (atpos_data)
8657 bidi_unshelve_cache (atpos_data, 1);
8658 if (atx_data)
8659 bidi_unshelve_cache (atx_data, 1);
8660 if (wrap_data)
8661 bidi_unshelve_cache (wrap_data, 1);
8662 if (ppos_data)
8663 bidi_unshelve_cache (ppos_data, 1);
8664
8665 /* Restore the iterator settings altered at the beginning of this
8666 function. */
8667 it->glyph_row = saved_glyph_row;
8668 return result;
8669 }
8670
8671 /* For external use. */
8672 void
8673 move_it_in_display_line (struct it *it,
8674 ptrdiff_t to_charpos, int to_x,
8675 enum move_operation_enum op)
8676 {
8677 if (it->line_wrap == WORD_WRAP
8678 && (op & MOVE_TO_X))
8679 {
8680 struct it save_it;
8681 void *save_data = NULL;
8682 int skip;
8683
8684 SAVE_IT (save_it, *it, save_data);
8685 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8686 /* When word-wrap is on, TO_X may lie past the end
8687 of a wrapped line. Then it->current is the
8688 character on the next line, so backtrack to the
8689 space before the wrap point. */
8690 if (skip == MOVE_LINE_CONTINUED)
8691 {
8692 int prev_x = max (it->current_x - 1, 0);
8693 RESTORE_IT (it, &save_it, save_data);
8694 move_it_in_display_line_to
8695 (it, -1, prev_x, MOVE_TO_X);
8696 }
8697 else
8698 bidi_unshelve_cache (save_data, 1);
8699 }
8700 else
8701 move_it_in_display_line_to (it, to_charpos, to_x, op);
8702 }
8703
8704
8705 /* Move IT forward until it satisfies one or more of the criteria in
8706 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8707
8708 OP is a bit-mask that specifies where to stop, and in particular,
8709 which of those four position arguments makes a difference. See the
8710 description of enum move_operation_enum.
8711
8712 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8713 screen line, this function will set IT to the next position that is
8714 displayed to the right of TO_CHARPOS on the screen. */
8715
8716 void
8717 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8718 {
8719 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8720 int line_height, line_start_x = 0, reached = 0;
8721 void *backup_data = NULL;
8722
8723 for (;;)
8724 {
8725 if (op & MOVE_TO_VPOS)
8726 {
8727 /* If no TO_CHARPOS and no TO_X specified, stop at the
8728 start of the line TO_VPOS. */
8729 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8730 {
8731 if (it->vpos == to_vpos)
8732 {
8733 reached = 1;
8734 break;
8735 }
8736 else
8737 skip = move_it_in_display_line_to (it, -1, -1, 0);
8738 }
8739 else
8740 {
8741 /* TO_VPOS >= 0 means stop at TO_X in the line at
8742 TO_VPOS, or at TO_POS, whichever comes first. */
8743 if (it->vpos == to_vpos)
8744 {
8745 reached = 2;
8746 break;
8747 }
8748
8749 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8750
8751 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8752 {
8753 reached = 3;
8754 break;
8755 }
8756 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8757 {
8758 /* We have reached TO_X but not in the line we want. */
8759 skip = move_it_in_display_line_to (it, to_charpos,
8760 -1, MOVE_TO_POS);
8761 if (skip == MOVE_POS_MATCH_OR_ZV)
8762 {
8763 reached = 4;
8764 break;
8765 }
8766 }
8767 }
8768 }
8769 else if (op & MOVE_TO_Y)
8770 {
8771 struct it it_backup;
8772
8773 if (it->line_wrap == WORD_WRAP)
8774 SAVE_IT (it_backup, *it, backup_data);
8775
8776 /* TO_Y specified means stop at TO_X in the line containing
8777 TO_Y---or at TO_CHARPOS if this is reached first. The
8778 problem is that we can't really tell whether the line
8779 contains TO_Y before we have completely scanned it, and
8780 this may skip past TO_X. What we do is to first scan to
8781 TO_X.
8782
8783 If TO_X is not specified, use a TO_X of zero. The reason
8784 is to make the outcome of this function more predictable.
8785 If we didn't use TO_X == 0, we would stop at the end of
8786 the line which is probably not what a caller would expect
8787 to happen. */
8788 skip = move_it_in_display_line_to
8789 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8790 (MOVE_TO_X | (op & MOVE_TO_POS)));
8791
8792 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8793 if (skip == MOVE_POS_MATCH_OR_ZV)
8794 reached = 5;
8795 else if (skip == MOVE_X_REACHED)
8796 {
8797 /* If TO_X was reached, we want to know whether TO_Y is
8798 in the line. We know this is the case if the already
8799 scanned glyphs make the line tall enough. Otherwise,
8800 we must check by scanning the rest of the line. */
8801 line_height = it->max_ascent + it->max_descent;
8802 if (to_y >= it->current_y
8803 && to_y < it->current_y + line_height)
8804 {
8805 reached = 6;
8806 break;
8807 }
8808 SAVE_IT (it_backup, *it, backup_data);
8809 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8810 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8811 op & MOVE_TO_POS);
8812 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8813 line_height = it->max_ascent + it->max_descent;
8814 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8815
8816 if (to_y >= it->current_y
8817 && to_y < it->current_y + line_height)
8818 {
8819 /* If TO_Y is in this line and TO_X was reached
8820 above, we scanned too far. We have to restore
8821 IT's settings to the ones before skipping. But
8822 keep the more accurate values of max_ascent and
8823 max_descent we've found while skipping the rest
8824 of the line, for the sake of callers, such as
8825 pos_visible_p, that need to know the line
8826 height. */
8827 int max_ascent = it->max_ascent;
8828 int max_descent = it->max_descent;
8829
8830 RESTORE_IT (it, &it_backup, backup_data);
8831 it->max_ascent = max_ascent;
8832 it->max_descent = max_descent;
8833 reached = 6;
8834 }
8835 else
8836 {
8837 skip = skip2;
8838 if (skip == MOVE_POS_MATCH_OR_ZV)
8839 reached = 7;
8840 }
8841 }
8842 else
8843 {
8844 /* Check whether TO_Y is in this line. */
8845 line_height = it->max_ascent + it->max_descent;
8846 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8847
8848 if (to_y >= it->current_y
8849 && to_y < it->current_y + line_height)
8850 {
8851 /* When word-wrap is on, TO_X may lie past the end
8852 of a wrapped line. Then it->current is the
8853 character on the next line, so backtrack to the
8854 space before the wrap point. */
8855 if (skip == MOVE_LINE_CONTINUED
8856 && it->line_wrap == WORD_WRAP)
8857 {
8858 int prev_x = max (it->current_x - 1, 0);
8859 RESTORE_IT (it, &it_backup, backup_data);
8860 skip = move_it_in_display_line_to
8861 (it, -1, prev_x, MOVE_TO_X);
8862 }
8863 reached = 6;
8864 }
8865 }
8866
8867 if (reached)
8868 break;
8869 }
8870 else if (BUFFERP (it->object)
8871 && (it->method == GET_FROM_BUFFER
8872 || it->method == GET_FROM_STRETCH)
8873 && IT_CHARPOS (*it) >= to_charpos
8874 /* Under bidi iteration, a call to set_iterator_to_next
8875 can scan far beyond to_charpos if the initial
8876 portion of the next line needs to be reordered. In
8877 that case, give move_it_in_display_line_to another
8878 chance below. */
8879 && !(it->bidi_p
8880 && it->bidi_it.scan_dir == -1))
8881 skip = MOVE_POS_MATCH_OR_ZV;
8882 else
8883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8884
8885 switch (skip)
8886 {
8887 case MOVE_POS_MATCH_OR_ZV:
8888 reached = 8;
8889 goto out;
8890
8891 case MOVE_NEWLINE_OR_CR:
8892 set_iterator_to_next (it, 1);
8893 it->continuation_lines_width = 0;
8894 break;
8895
8896 case MOVE_LINE_TRUNCATED:
8897 it->continuation_lines_width = 0;
8898 reseat_at_next_visible_line_start (it, 0);
8899 if ((op & MOVE_TO_POS) != 0
8900 && IT_CHARPOS (*it) > to_charpos)
8901 {
8902 reached = 9;
8903 goto out;
8904 }
8905 break;
8906
8907 case MOVE_LINE_CONTINUED:
8908 /* For continued lines ending in a tab, some of the glyphs
8909 associated with the tab are displayed on the current
8910 line. Since it->current_x does not include these glyphs,
8911 we use it->last_visible_x instead. */
8912 if (it->c == '\t')
8913 {
8914 it->continuation_lines_width += it->last_visible_x;
8915 /* When moving by vpos, ensure that the iterator really
8916 advances to the next line (bug#847, bug#969). Fixme:
8917 do we need to do this in other circumstances? */
8918 if (it->current_x != it->last_visible_x
8919 && (op & MOVE_TO_VPOS)
8920 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8921 {
8922 line_start_x = it->current_x + it->pixel_width
8923 - it->last_visible_x;
8924 set_iterator_to_next (it, 0);
8925 }
8926 }
8927 else
8928 it->continuation_lines_width += it->current_x;
8929 break;
8930
8931 default:
8932 emacs_abort ();
8933 }
8934
8935 /* Reset/increment for the next run. */
8936 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8937 it->current_x = line_start_x;
8938 line_start_x = 0;
8939 it->hpos = 0;
8940 it->current_y += it->max_ascent + it->max_descent;
8941 ++it->vpos;
8942 last_height = it->max_ascent + it->max_descent;
8943 last_max_ascent = it->max_ascent;
8944 it->max_ascent = it->max_descent = 0;
8945 }
8946
8947 out:
8948
8949 /* On text terminals, we may stop at the end of a line in the middle
8950 of a multi-character glyph. If the glyph itself is continued,
8951 i.e. it is actually displayed on the next line, don't treat this
8952 stopping point as valid; move to the next line instead (unless
8953 that brings us offscreen). */
8954 if (!FRAME_WINDOW_P (it->f)
8955 && op & MOVE_TO_POS
8956 && IT_CHARPOS (*it) == to_charpos
8957 && it->what == IT_CHARACTER
8958 && it->nglyphs > 1
8959 && it->line_wrap == WINDOW_WRAP
8960 && it->current_x == it->last_visible_x - 1
8961 && it->c != '\n'
8962 && it->c != '\t'
8963 && it->vpos < XFASTINT (it->w->window_end_vpos))
8964 {
8965 it->continuation_lines_width += it->current_x;
8966 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8967 it->current_y += it->max_ascent + it->max_descent;
8968 ++it->vpos;
8969 last_height = it->max_ascent + it->max_descent;
8970 last_max_ascent = it->max_ascent;
8971 }
8972
8973 if (backup_data)
8974 bidi_unshelve_cache (backup_data, 1);
8975
8976 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8977 }
8978
8979
8980 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8981
8982 If DY > 0, move IT backward at least that many pixels. DY = 0
8983 means move IT backward to the preceding line start or BEGV. This
8984 function may move over more than DY pixels if IT->current_y - DY
8985 ends up in the middle of a line; in this case IT->current_y will be
8986 set to the top of the line moved to. */
8987
8988 void
8989 move_it_vertically_backward (struct it *it, int dy)
8990 {
8991 int nlines, h;
8992 struct it it2, it3;
8993 void *it2data = NULL, *it3data = NULL;
8994 ptrdiff_t start_pos;
8995
8996 move_further_back:
8997 eassert (dy >= 0);
8998
8999 start_pos = IT_CHARPOS (*it);
9000
9001 /* Estimate how many newlines we must move back. */
9002 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9003
9004 /* Set the iterator's position that many lines back. */
9005 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9006 back_to_previous_visible_line_start (it);
9007
9008 /* Reseat the iterator here. When moving backward, we don't want
9009 reseat to skip forward over invisible text, set up the iterator
9010 to deliver from overlay strings at the new position etc. So,
9011 use reseat_1 here. */
9012 reseat_1 (it, it->current.pos, 1);
9013
9014 /* We are now surely at a line start. */
9015 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9016 reordering is in effect. */
9017 it->continuation_lines_width = 0;
9018
9019 /* Move forward and see what y-distance we moved. First move to the
9020 start of the next line so that we get its height. We need this
9021 height to be able to tell whether we reached the specified
9022 y-distance. */
9023 SAVE_IT (it2, *it, it2data);
9024 it2.max_ascent = it2.max_descent = 0;
9025 do
9026 {
9027 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9028 MOVE_TO_POS | MOVE_TO_VPOS);
9029 }
9030 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9031 /* If we are in a display string which starts at START_POS,
9032 and that display string includes a newline, and we are
9033 right after that newline (i.e. at the beginning of a
9034 display line), exit the loop, because otherwise we will
9035 infloop, since move_it_to will see that it is already at
9036 START_POS and will not move. */
9037 || (it2.method == GET_FROM_STRING
9038 && IT_CHARPOS (it2) == start_pos
9039 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9040 eassert (IT_CHARPOS (*it) >= BEGV);
9041 SAVE_IT (it3, it2, it3data);
9042
9043 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9044 eassert (IT_CHARPOS (*it) >= BEGV);
9045 /* H is the actual vertical distance from the position in *IT
9046 and the starting position. */
9047 h = it2.current_y - it->current_y;
9048 /* NLINES is the distance in number of lines. */
9049 nlines = it2.vpos - it->vpos;
9050
9051 /* Correct IT's y and vpos position
9052 so that they are relative to the starting point. */
9053 it->vpos -= nlines;
9054 it->current_y -= h;
9055
9056 if (dy == 0)
9057 {
9058 /* DY == 0 means move to the start of the screen line. The
9059 value of nlines is > 0 if continuation lines were involved,
9060 or if the original IT position was at start of a line. */
9061 RESTORE_IT (it, it, it2data);
9062 if (nlines > 0)
9063 move_it_by_lines (it, nlines);
9064 /* The above code moves us to some position NLINES down,
9065 usually to its first glyph (leftmost in an L2R line), but
9066 that's not necessarily the start of the line, under bidi
9067 reordering. We want to get to the character position
9068 that is immediately after the newline of the previous
9069 line. */
9070 if (it->bidi_p
9071 && !it->continuation_lines_width
9072 && !STRINGP (it->string)
9073 && IT_CHARPOS (*it) > BEGV
9074 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9075 {
9076 ptrdiff_t nl_pos =
9077 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9078
9079 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9080 }
9081 bidi_unshelve_cache (it3data, 1);
9082 }
9083 else
9084 {
9085 /* The y-position we try to reach, relative to *IT.
9086 Note that H has been subtracted in front of the if-statement. */
9087 int target_y = it->current_y + h - dy;
9088 int y0 = it3.current_y;
9089 int y1;
9090 int line_height;
9091
9092 RESTORE_IT (&it3, &it3, it3data);
9093 y1 = line_bottom_y (&it3);
9094 line_height = y1 - y0;
9095 RESTORE_IT (it, it, it2data);
9096 /* If we did not reach target_y, try to move further backward if
9097 we can. If we moved too far backward, try to move forward. */
9098 if (target_y < it->current_y
9099 /* This is heuristic. In a window that's 3 lines high, with
9100 a line height of 13 pixels each, recentering with point
9101 on the bottom line will try to move -39/2 = 19 pixels
9102 backward. Try to avoid moving into the first line. */
9103 && (it->current_y - target_y
9104 > min (window_box_height (it->w), line_height * 2 / 3))
9105 && IT_CHARPOS (*it) > BEGV)
9106 {
9107 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9108 target_y - it->current_y));
9109 dy = it->current_y - target_y;
9110 goto move_further_back;
9111 }
9112 else if (target_y >= it->current_y + line_height
9113 && IT_CHARPOS (*it) < ZV)
9114 {
9115 /* Should move forward by at least one line, maybe more.
9116
9117 Note: Calling move_it_by_lines can be expensive on
9118 terminal frames, where compute_motion is used (via
9119 vmotion) to do the job, when there are very long lines
9120 and truncate-lines is nil. That's the reason for
9121 treating terminal frames specially here. */
9122
9123 if (!FRAME_WINDOW_P (it->f))
9124 move_it_vertically (it, target_y - (it->current_y + line_height));
9125 else
9126 {
9127 do
9128 {
9129 move_it_by_lines (it, 1);
9130 }
9131 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9132 }
9133 }
9134 }
9135 }
9136
9137
9138 /* Move IT by a specified amount of pixel lines DY. DY negative means
9139 move backwards. DY = 0 means move to start of screen line. At the
9140 end, IT will be on the start of a screen line. */
9141
9142 void
9143 move_it_vertically (struct it *it, int dy)
9144 {
9145 if (dy <= 0)
9146 move_it_vertically_backward (it, -dy);
9147 else
9148 {
9149 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9150 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9151 MOVE_TO_POS | MOVE_TO_Y);
9152 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9153
9154 /* If buffer ends in ZV without a newline, move to the start of
9155 the line to satisfy the post-condition. */
9156 if (IT_CHARPOS (*it) == ZV
9157 && ZV > BEGV
9158 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9159 move_it_by_lines (it, 0);
9160 }
9161 }
9162
9163
9164 /* Move iterator IT past the end of the text line it is in. */
9165
9166 void
9167 move_it_past_eol (struct it *it)
9168 {
9169 enum move_it_result rc;
9170
9171 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9172 if (rc == MOVE_NEWLINE_OR_CR)
9173 set_iterator_to_next (it, 0);
9174 }
9175
9176
9177 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9178 negative means move up. DVPOS == 0 means move to the start of the
9179 screen line.
9180
9181 Optimization idea: If we would know that IT->f doesn't use
9182 a face with proportional font, we could be faster for
9183 truncate-lines nil. */
9184
9185 void
9186 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9187 {
9188
9189 /* The commented-out optimization uses vmotion on terminals. This
9190 gives bad results, because elements like it->what, on which
9191 callers such as pos_visible_p rely, aren't updated. */
9192 /* struct position pos;
9193 if (!FRAME_WINDOW_P (it->f))
9194 {
9195 struct text_pos textpos;
9196
9197 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9198 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9199 reseat (it, textpos, 1);
9200 it->vpos += pos.vpos;
9201 it->current_y += pos.vpos;
9202 }
9203 else */
9204
9205 if (dvpos == 0)
9206 {
9207 /* DVPOS == 0 means move to the start of the screen line. */
9208 move_it_vertically_backward (it, 0);
9209 /* Let next call to line_bottom_y calculate real line height */
9210 last_height = 0;
9211 }
9212 else if (dvpos > 0)
9213 {
9214 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9215 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9216 {
9217 /* Only move to the next buffer position if we ended up in a
9218 string from display property, not in an overlay string
9219 (before-string or after-string). That is because the
9220 latter don't conceal the underlying buffer position, so
9221 we can ask to move the iterator to the exact position we
9222 are interested in. Note that, even if we are already at
9223 IT_CHARPOS (*it), the call below is not a no-op, as it
9224 will detect that we are at the end of the string, pop the
9225 iterator, and compute it->current_x and it->hpos
9226 correctly. */
9227 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9228 -1, -1, -1, MOVE_TO_POS);
9229 }
9230 }
9231 else
9232 {
9233 struct it it2;
9234 void *it2data = NULL;
9235 ptrdiff_t start_charpos, i;
9236
9237 /* Start at the beginning of the screen line containing IT's
9238 position. This may actually move vertically backwards,
9239 in case of overlays, so adjust dvpos accordingly. */
9240 dvpos += it->vpos;
9241 move_it_vertically_backward (it, 0);
9242 dvpos -= it->vpos;
9243
9244 /* Go back -DVPOS visible lines and reseat the iterator there. */
9245 start_charpos = IT_CHARPOS (*it);
9246 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9247 back_to_previous_visible_line_start (it);
9248 reseat (it, it->current.pos, 1);
9249
9250 /* Move further back if we end up in a string or an image. */
9251 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9252 {
9253 /* First try to move to start of display line. */
9254 dvpos += it->vpos;
9255 move_it_vertically_backward (it, 0);
9256 dvpos -= it->vpos;
9257 if (IT_POS_VALID_AFTER_MOVE_P (it))
9258 break;
9259 /* If start of line is still in string or image,
9260 move further back. */
9261 back_to_previous_visible_line_start (it);
9262 reseat (it, it->current.pos, 1);
9263 dvpos--;
9264 }
9265
9266 it->current_x = it->hpos = 0;
9267
9268 /* Above call may have moved too far if continuation lines
9269 are involved. Scan forward and see if it did. */
9270 SAVE_IT (it2, *it, it2data);
9271 it2.vpos = it2.current_y = 0;
9272 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9273 it->vpos -= it2.vpos;
9274 it->current_y -= it2.current_y;
9275 it->current_x = it->hpos = 0;
9276
9277 /* If we moved too far back, move IT some lines forward. */
9278 if (it2.vpos > -dvpos)
9279 {
9280 int delta = it2.vpos + dvpos;
9281
9282 RESTORE_IT (&it2, &it2, it2data);
9283 SAVE_IT (it2, *it, it2data);
9284 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9285 /* Move back again if we got too far ahead. */
9286 if (IT_CHARPOS (*it) >= start_charpos)
9287 RESTORE_IT (it, &it2, it2data);
9288 else
9289 bidi_unshelve_cache (it2data, 1);
9290 }
9291 else
9292 RESTORE_IT (it, it, it2data);
9293 }
9294 }
9295
9296 /* Return 1 if IT points into the middle of a display vector. */
9297
9298 int
9299 in_display_vector_p (struct it *it)
9300 {
9301 return (it->method == GET_FROM_DISPLAY_VECTOR
9302 && it->current.dpvec_index > 0
9303 && it->dpvec + it->current.dpvec_index != it->dpend);
9304 }
9305
9306 \f
9307 /***********************************************************************
9308 Messages
9309 ***********************************************************************/
9310
9311
9312 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9313 to *Messages*. */
9314
9315 void
9316 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9317 {
9318 Lisp_Object args[3];
9319 Lisp_Object msg, fmt;
9320 char *buffer;
9321 ptrdiff_t len;
9322 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9323 USE_SAFE_ALLOCA;
9324
9325 fmt = msg = Qnil;
9326 GCPRO4 (fmt, msg, arg1, arg2);
9327
9328 args[0] = fmt = build_string (format);
9329 args[1] = arg1;
9330 args[2] = arg2;
9331 msg = Fformat (3, args);
9332
9333 len = SBYTES (msg) + 1;
9334 buffer = SAFE_ALLOCA (len);
9335 memcpy (buffer, SDATA (msg), len);
9336
9337 message_dolog (buffer, len - 1, 1, 0);
9338 SAFE_FREE ();
9339
9340 UNGCPRO;
9341 }
9342
9343
9344 /* Output a newline in the *Messages* buffer if "needs" one. */
9345
9346 void
9347 message_log_maybe_newline (void)
9348 {
9349 if (message_log_need_newline)
9350 message_dolog ("", 0, 1, 0);
9351 }
9352
9353
9354 /* Add a string M of length NBYTES to the message log, optionally
9355 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9356 nonzero, means interpret the contents of M as multibyte. This
9357 function calls low-level routines in order to bypass text property
9358 hooks, etc. which might not be safe to run.
9359
9360 This may GC (insert may run before/after change hooks),
9361 so the buffer M must NOT point to a Lisp string. */
9362
9363 void
9364 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9365 {
9366 const unsigned char *msg = (const unsigned char *) m;
9367
9368 if (!NILP (Vmemory_full))
9369 return;
9370
9371 if (!NILP (Vmessage_log_max))
9372 {
9373 struct buffer *oldbuf;
9374 Lisp_Object oldpoint, oldbegv, oldzv;
9375 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9376 ptrdiff_t point_at_end = 0;
9377 ptrdiff_t zv_at_end = 0;
9378 Lisp_Object old_deactivate_mark;
9379 bool shown;
9380 struct gcpro gcpro1;
9381
9382 old_deactivate_mark = Vdeactivate_mark;
9383 oldbuf = current_buffer;
9384 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9385 bset_undo_list (current_buffer, Qt);
9386
9387 oldpoint = message_dolog_marker1;
9388 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9389 oldbegv = message_dolog_marker2;
9390 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9391 oldzv = message_dolog_marker3;
9392 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9393 GCPRO1 (old_deactivate_mark);
9394
9395 if (PT == Z)
9396 point_at_end = 1;
9397 if (ZV == Z)
9398 zv_at_end = 1;
9399
9400 BEGV = BEG;
9401 BEGV_BYTE = BEG_BYTE;
9402 ZV = Z;
9403 ZV_BYTE = Z_BYTE;
9404 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9405
9406 /* Insert the string--maybe converting multibyte to single byte
9407 or vice versa, so that all the text fits the buffer. */
9408 if (multibyte
9409 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9410 {
9411 ptrdiff_t i;
9412 int c, char_bytes;
9413 char work[1];
9414
9415 /* Convert a multibyte string to single-byte
9416 for the *Message* buffer. */
9417 for (i = 0; i < nbytes; i += char_bytes)
9418 {
9419 c = string_char_and_length (msg + i, &char_bytes);
9420 work[0] = (ASCII_CHAR_P (c)
9421 ? c
9422 : multibyte_char_to_unibyte (c));
9423 insert_1_both (work, 1, 1, 1, 0, 0);
9424 }
9425 }
9426 else if (! multibyte
9427 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9428 {
9429 ptrdiff_t i;
9430 int c, char_bytes;
9431 unsigned char str[MAX_MULTIBYTE_LENGTH];
9432 /* Convert a single-byte string to multibyte
9433 for the *Message* buffer. */
9434 for (i = 0; i < nbytes; i++)
9435 {
9436 c = msg[i];
9437 MAKE_CHAR_MULTIBYTE (c);
9438 char_bytes = CHAR_STRING (c, str);
9439 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9440 }
9441 }
9442 else if (nbytes)
9443 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9444
9445 if (nlflag)
9446 {
9447 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9448 printmax_t dups;
9449
9450 insert_1_both ("\n", 1, 1, 1, 0, 0);
9451
9452 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9453 this_bol = PT;
9454 this_bol_byte = PT_BYTE;
9455
9456 /* See if this line duplicates the previous one.
9457 If so, combine duplicates. */
9458 if (this_bol > BEG)
9459 {
9460 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9461 prev_bol = PT;
9462 prev_bol_byte = PT_BYTE;
9463
9464 dups = message_log_check_duplicate (prev_bol_byte,
9465 this_bol_byte);
9466 if (dups)
9467 {
9468 del_range_both (prev_bol, prev_bol_byte,
9469 this_bol, this_bol_byte, 0);
9470 if (dups > 1)
9471 {
9472 char dupstr[sizeof " [ times]"
9473 + INT_STRLEN_BOUND (printmax_t)];
9474
9475 /* If you change this format, don't forget to also
9476 change message_log_check_duplicate. */
9477 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9478 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9479 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9480 }
9481 }
9482 }
9483
9484 /* If we have more than the desired maximum number of lines
9485 in the *Messages* buffer now, delete the oldest ones.
9486 This is safe because we don't have undo in this buffer. */
9487
9488 if (NATNUMP (Vmessage_log_max))
9489 {
9490 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9491 -XFASTINT (Vmessage_log_max) - 1, 0);
9492 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9493 }
9494 }
9495 BEGV = marker_position (oldbegv);
9496 BEGV_BYTE = marker_byte_position (oldbegv);
9497
9498 if (zv_at_end)
9499 {
9500 ZV = Z;
9501 ZV_BYTE = Z_BYTE;
9502 }
9503 else
9504 {
9505 ZV = marker_position (oldzv);
9506 ZV_BYTE = marker_byte_position (oldzv);
9507 }
9508
9509 if (point_at_end)
9510 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9511 else
9512 /* We can't do Fgoto_char (oldpoint) because it will run some
9513 Lisp code. */
9514 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9515 marker_byte_position (oldpoint));
9516
9517 UNGCPRO;
9518 unchain_marker (XMARKER (oldpoint));
9519 unchain_marker (XMARKER (oldbegv));
9520 unchain_marker (XMARKER (oldzv));
9521
9522 shown = buffer_window_count (current_buffer) > 0;
9523 set_buffer_internal (oldbuf);
9524 if (!shown)
9525 windows_or_buffers_changed = old_windows_or_buffers_changed;
9526 message_log_need_newline = !nlflag;
9527 Vdeactivate_mark = old_deactivate_mark;
9528 }
9529 }
9530
9531
9532 /* We are at the end of the buffer after just having inserted a newline.
9533 (Note: We depend on the fact we won't be crossing the gap.)
9534 Check to see if the most recent message looks a lot like the previous one.
9535 Return 0 if different, 1 if the new one should just replace it, or a
9536 value N > 1 if we should also append " [N times]". */
9537
9538 static intmax_t
9539 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9540 {
9541 ptrdiff_t i;
9542 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9543 int seen_dots = 0;
9544 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9545 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9546
9547 for (i = 0; i < len; i++)
9548 {
9549 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9550 seen_dots = 1;
9551 if (p1[i] != p2[i])
9552 return seen_dots;
9553 }
9554 p1 += len;
9555 if (*p1 == '\n')
9556 return 2;
9557 if (*p1++ == ' ' && *p1++ == '[')
9558 {
9559 char *pend;
9560 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9561 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9562 return n + 1;
9563 }
9564 return 0;
9565 }
9566 \f
9567
9568 /* Display an echo area message M with a specified length of NBYTES
9569 bytes. The string may include null characters. If M is not a
9570 string, clear out any existing message, and let the mini-buffer
9571 text show through.
9572
9573 This function cancels echoing. */
9574
9575 void
9576 message3 (Lisp_Object m)
9577 {
9578 struct gcpro gcpro1;
9579
9580 GCPRO1 (m);
9581 clear_message (1,1);
9582 cancel_echoing ();
9583
9584 /* First flush out any partial line written with print. */
9585 message_log_maybe_newline ();
9586 if (STRINGP (m))
9587 {
9588 ptrdiff_t nbytes = SBYTES (m);
9589 int multibyte = STRING_MULTIBYTE (m);
9590 USE_SAFE_ALLOCA;
9591 char *buffer = SAFE_ALLOCA (nbytes);
9592 memcpy (buffer, SDATA (m), nbytes);
9593 message_dolog (buffer, nbytes, 1, multibyte);
9594 SAFE_FREE ();
9595 }
9596 message3_nolog (m);
9597
9598 UNGCPRO;
9599 }
9600
9601
9602 /* The non-logging version of message3.
9603 This does not cancel echoing, because it is used for echoing.
9604 Perhaps we need to make a separate function for echoing
9605 and make this cancel echoing. */
9606
9607 void
9608 message3_nolog (Lisp_Object m)
9609 {
9610 struct frame *sf = SELECTED_FRAME ();
9611
9612 if (FRAME_INITIAL_P (sf))
9613 {
9614 if (noninteractive_need_newline)
9615 putc ('\n', stderr);
9616 noninteractive_need_newline = 0;
9617 if (STRINGP (m))
9618 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9619 if (cursor_in_echo_area == 0)
9620 fprintf (stderr, "\n");
9621 fflush (stderr);
9622 }
9623 /* Error messages get reported properly by cmd_error, so this must be just an
9624 informative message; if the frame hasn't really been initialized yet, just
9625 toss it. */
9626 else if (INTERACTIVE && sf->glyphs_initialized_p)
9627 {
9628 /* Get the frame containing the mini-buffer
9629 that the selected frame is using. */
9630 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9631 Lisp_Object frame = XWINDOW (mini_window)->frame;
9632 struct frame *f = XFRAME (frame);
9633
9634 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9635 Fmake_frame_visible (frame);
9636
9637 if (STRINGP (m) && SCHARS (m) > 0)
9638 {
9639 set_message (m);
9640 if (minibuffer_auto_raise)
9641 Fraise_frame (frame);
9642 /* Assume we are not echoing.
9643 (If we are, echo_now will override this.) */
9644 echo_message_buffer = Qnil;
9645 }
9646 else
9647 clear_message (1, 1);
9648
9649 do_pending_window_change (0);
9650 echo_area_display (1);
9651 do_pending_window_change (0);
9652 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9653 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9654 }
9655 }
9656
9657
9658 /* Display a null-terminated echo area message M. If M is 0, clear
9659 out any existing message, and let the mini-buffer text show through.
9660
9661 The buffer M must continue to exist until after the echo area gets
9662 cleared or some other message gets displayed there. Do not pass
9663 text that is stored in a Lisp string. Do not pass text in a buffer
9664 that was alloca'd. */
9665
9666 void
9667 message1 (const char *m)
9668 {
9669 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9670 }
9671
9672
9673 /* The non-logging counterpart of message1. */
9674
9675 void
9676 message1_nolog (const char *m)
9677 {
9678 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9679 }
9680
9681 /* Display a message M which contains a single %s
9682 which gets replaced with STRING. */
9683
9684 void
9685 message_with_string (const char *m, Lisp_Object string, int log)
9686 {
9687 CHECK_STRING (string);
9688
9689 if (noninteractive)
9690 {
9691 if (m)
9692 {
9693 if (noninteractive_need_newline)
9694 putc ('\n', stderr);
9695 noninteractive_need_newline = 0;
9696 fprintf (stderr, m, SDATA (string));
9697 if (!cursor_in_echo_area)
9698 fprintf (stderr, "\n");
9699 fflush (stderr);
9700 }
9701 }
9702 else if (INTERACTIVE)
9703 {
9704 /* The frame whose minibuffer we're going to display the message on.
9705 It may be larger than the selected frame, so we need
9706 to use its buffer, not the selected frame's buffer. */
9707 Lisp_Object mini_window;
9708 struct frame *f, *sf = SELECTED_FRAME ();
9709
9710 /* Get the frame containing the minibuffer
9711 that the selected frame is using. */
9712 mini_window = FRAME_MINIBUF_WINDOW (sf);
9713 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9714
9715 /* Error messages get reported properly by cmd_error, so this must be
9716 just an informative message; if the frame hasn't really been
9717 initialized yet, just toss it. */
9718 if (f->glyphs_initialized_p)
9719 {
9720 Lisp_Object args[2], msg;
9721 struct gcpro gcpro1, gcpro2;
9722
9723 args[0] = build_string (m);
9724 args[1] = msg = string;
9725 GCPRO2 (args[0], msg);
9726 gcpro1.nvars = 2;
9727
9728 msg = Fformat (2, args);
9729
9730 if (log)
9731 message3 (msg);
9732 else
9733 message3_nolog (msg);
9734
9735 UNGCPRO;
9736
9737 /* Print should start at the beginning of the message
9738 buffer next time. */
9739 message_buf_print = 0;
9740 }
9741 }
9742 }
9743
9744
9745 /* Dump an informative message to the minibuf. If M is 0, clear out
9746 any existing message, and let the mini-buffer text show through. */
9747
9748 static void
9749 vmessage (const char *m, va_list ap)
9750 {
9751 if (noninteractive)
9752 {
9753 if (m)
9754 {
9755 if (noninteractive_need_newline)
9756 putc ('\n', stderr);
9757 noninteractive_need_newline = 0;
9758 vfprintf (stderr, m, ap);
9759 if (cursor_in_echo_area == 0)
9760 fprintf (stderr, "\n");
9761 fflush (stderr);
9762 }
9763 }
9764 else if (INTERACTIVE)
9765 {
9766 /* The frame whose mini-buffer we're going to display the message
9767 on. It may be larger than the selected frame, so we need to
9768 use its buffer, not the selected frame's buffer. */
9769 Lisp_Object mini_window;
9770 struct frame *f, *sf = SELECTED_FRAME ();
9771
9772 /* Get the frame containing the mini-buffer
9773 that the selected frame is using. */
9774 mini_window = FRAME_MINIBUF_WINDOW (sf);
9775 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9776
9777 /* Error messages get reported properly by cmd_error, so this must be
9778 just an informative message; if the frame hasn't really been
9779 initialized yet, just toss it. */
9780 if (f->glyphs_initialized_p)
9781 {
9782 if (m)
9783 {
9784 ptrdiff_t len;
9785 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9786 char *message_buf = alloca (maxsize + 1);
9787
9788 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9789
9790 message3 (make_string (message_buf, len));
9791 }
9792 else
9793 message1 (0);
9794
9795 /* Print should start at the beginning of the message
9796 buffer next time. */
9797 message_buf_print = 0;
9798 }
9799 }
9800 }
9801
9802 void
9803 message (const char *m, ...)
9804 {
9805 va_list ap;
9806 va_start (ap, m);
9807 vmessage (m, ap);
9808 va_end (ap);
9809 }
9810
9811
9812 #if 0
9813 /* The non-logging version of message. */
9814
9815 void
9816 message_nolog (const char *m, ...)
9817 {
9818 Lisp_Object old_log_max;
9819 va_list ap;
9820 va_start (ap, m);
9821 old_log_max = Vmessage_log_max;
9822 Vmessage_log_max = Qnil;
9823 vmessage (m, ap);
9824 Vmessage_log_max = old_log_max;
9825 va_end (ap);
9826 }
9827 #endif
9828
9829
9830 /* Display the current message in the current mini-buffer. This is
9831 only called from error handlers in process.c, and is not time
9832 critical. */
9833
9834 void
9835 update_echo_area (void)
9836 {
9837 if (!NILP (echo_area_buffer[0]))
9838 {
9839 Lisp_Object string;
9840 string = Fcurrent_message ();
9841 message3 (string);
9842 }
9843 }
9844
9845
9846 /* Make sure echo area buffers in `echo_buffers' are live.
9847 If they aren't, make new ones. */
9848
9849 static void
9850 ensure_echo_area_buffers (void)
9851 {
9852 int i;
9853
9854 for (i = 0; i < 2; ++i)
9855 if (!BUFFERP (echo_buffer[i])
9856 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9857 {
9858 char name[30];
9859 Lisp_Object old_buffer;
9860 int j;
9861
9862 old_buffer = echo_buffer[i];
9863 echo_buffer[i] = Fget_buffer_create
9864 (make_formatted_string (name, " *Echo Area %d*", i));
9865 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9866 /* to force word wrap in echo area -
9867 it was decided to postpone this*/
9868 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9869
9870 for (j = 0; j < 2; ++j)
9871 if (EQ (old_buffer, echo_area_buffer[j]))
9872 echo_area_buffer[j] = echo_buffer[i];
9873 }
9874 }
9875
9876
9877 /* Call FN with args A1..A2 with either the current or last displayed
9878 echo_area_buffer as current buffer.
9879
9880 WHICH zero means use the current message buffer
9881 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9882 from echo_buffer[] and clear it.
9883
9884 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9885 suitable buffer from echo_buffer[] and clear it.
9886
9887 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9888 that the current message becomes the last displayed one, make
9889 choose a suitable buffer for echo_area_buffer[0], and clear it.
9890
9891 Value is what FN returns. */
9892
9893 static int
9894 with_echo_area_buffer (struct window *w, int which,
9895 int (*fn) (ptrdiff_t, Lisp_Object),
9896 ptrdiff_t a1, Lisp_Object a2)
9897 {
9898 Lisp_Object buffer;
9899 int this_one, the_other, clear_buffer_p, rc;
9900 ptrdiff_t count = SPECPDL_INDEX ();
9901
9902 /* If buffers aren't live, make new ones. */
9903 ensure_echo_area_buffers ();
9904
9905 clear_buffer_p = 0;
9906
9907 if (which == 0)
9908 this_one = 0, the_other = 1;
9909 else if (which > 0)
9910 this_one = 1, the_other = 0;
9911 else
9912 {
9913 this_one = 0, the_other = 1;
9914 clear_buffer_p = 1;
9915
9916 /* We need a fresh one in case the current echo buffer equals
9917 the one containing the last displayed echo area message. */
9918 if (!NILP (echo_area_buffer[this_one])
9919 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9920 echo_area_buffer[this_one] = Qnil;
9921 }
9922
9923 /* Choose a suitable buffer from echo_buffer[] is we don't
9924 have one. */
9925 if (NILP (echo_area_buffer[this_one]))
9926 {
9927 echo_area_buffer[this_one]
9928 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9929 ? echo_buffer[the_other]
9930 : echo_buffer[this_one]);
9931 clear_buffer_p = 1;
9932 }
9933
9934 buffer = echo_area_buffer[this_one];
9935
9936 /* Don't get confused by reusing the buffer used for echoing
9937 for a different purpose. */
9938 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9939 cancel_echoing ();
9940
9941 record_unwind_protect (unwind_with_echo_area_buffer,
9942 with_echo_area_buffer_unwind_data (w));
9943
9944 /* Make the echo area buffer current. Note that for display
9945 purposes, it is not necessary that the displayed window's buffer
9946 == current_buffer, except for text property lookup. So, let's
9947 only set that buffer temporarily here without doing a full
9948 Fset_window_buffer. We must also change w->pointm, though,
9949 because otherwise an assertions in unshow_buffer fails, and Emacs
9950 aborts. */
9951 set_buffer_internal_1 (XBUFFER (buffer));
9952 if (w)
9953 {
9954 wset_buffer (w, buffer);
9955 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9956 }
9957
9958 bset_undo_list (current_buffer, Qt);
9959 bset_read_only (current_buffer, Qnil);
9960 specbind (Qinhibit_read_only, Qt);
9961 specbind (Qinhibit_modification_hooks, Qt);
9962
9963 if (clear_buffer_p && Z > BEG)
9964 del_range (BEG, Z);
9965
9966 eassert (BEGV >= BEG);
9967 eassert (ZV <= Z && ZV >= BEGV);
9968
9969 rc = fn (a1, a2);
9970
9971 eassert (BEGV >= BEG);
9972 eassert (ZV <= Z && ZV >= BEGV);
9973
9974 unbind_to (count, Qnil);
9975 return rc;
9976 }
9977
9978
9979 /* Save state that should be preserved around the call to the function
9980 FN called in with_echo_area_buffer. */
9981
9982 static Lisp_Object
9983 with_echo_area_buffer_unwind_data (struct window *w)
9984 {
9985 int i = 0;
9986 Lisp_Object vector, tmp;
9987
9988 /* Reduce consing by keeping one vector in
9989 Vwith_echo_area_save_vector. */
9990 vector = Vwith_echo_area_save_vector;
9991 Vwith_echo_area_save_vector = Qnil;
9992
9993 if (NILP (vector))
9994 vector = Fmake_vector (make_number (7), Qnil);
9995
9996 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9997 ASET (vector, i, Vdeactivate_mark); ++i;
9998 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9999
10000 if (w)
10001 {
10002 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10003 ASET (vector, i, w->buffer); ++i;
10004 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10005 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10006 }
10007 else
10008 {
10009 int end = i + 4;
10010 for (; i < end; ++i)
10011 ASET (vector, i, Qnil);
10012 }
10013
10014 eassert (i == ASIZE (vector));
10015 return vector;
10016 }
10017
10018
10019 /* Restore global state from VECTOR which was created by
10020 with_echo_area_buffer_unwind_data. */
10021
10022 static Lisp_Object
10023 unwind_with_echo_area_buffer (Lisp_Object vector)
10024 {
10025 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10026 Vdeactivate_mark = AREF (vector, 1);
10027 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10028
10029 if (WINDOWP (AREF (vector, 3)))
10030 {
10031 struct window *w;
10032 Lisp_Object buffer, charpos, bytepos;
10033
10034 w = XWINDOW (AREF (vector, 3));
10035 buffer = AREF (vector, 4);
10036 charpos = AREF (vector, 5);
10037 bytepos = AREF (vector, 6);
10038
10039 wset_buffer (w, buffer);
10040 set_marker_both (w->pointm, buffer,
10041 XFASTINT (charpos), XFASTINT (bytepos));
10042 }
10043
10044 Vwith_echo_area_save_vector = vector;
10045 return Qnil;
10046 }
10047
10048
10049 /* Set up the echo area for use by print functions. MULTIBYTE_P
10050 non-zero means we will print multibyte. */
10051
10052 void
10053 setup_echo_area_for_printing (int multibyte_p)
10054 {
10055 /* If we can't find an echo area any more, exit. */
10056 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10057 Fkill_emacs (Qnil);
10058
10059 ensure_echo_area_buffers ();
10060
10061 if (!message_buf_print)
10062 {
10063 /* A message has been output since the last time we printed.
10064 Choose a fresh echo area buffer. */
10065 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10066 echo_area_buffer[0] = echo_buffer[1];
10067 else
10068 echo_area_buffer[0] = echo_buffer[0];
10069
10070 /* Switch to that buffer and clear it. */
10071 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10072 bset_truncate_lines (current_buffer, Qnil);
10073
10074 if (Z > BEG)
10075 {
10076 ptrdiff_t count = SPECPDL_INDEX ();
10077 specbind (Qinhibit_read_only, Qt);
10078 /* Note that undo recording is always disabled. */
10079 del_range (BEG, Z);
10080 unbind_to (count, Qnil);
10081 }
10082 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10083
10084 /* Set up the buffer for the multibyteness we need. */
10085 if (multibyte_p
10086 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10087 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10088
10089 /* Raise the frame containing the echo area. */
10090 if (minibuffer_auto_raise)
10091 {
10092 struct frame *sf = SELECTED_FRAME ();
10093 Lisp_Object mini_window;
10094 mini_window = FRAME_MINIBUF_WINDOW (sf);
10095 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10096 }
10097
10098 message_log_maybe_newline ();
10099 message_buf_print = 1;
10100 }
10101 else
10102 {
10103 if (NILP (echo_area_buffer[0]))
10104 {
10105 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10106 echo_area_buffer[0] = echo_buffer[1];
10107 else
10108 echo_area_buffer[0] = echo_buffer[0];
10109 }
10110
10111 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10112 {
10113 /* Someone switched buffers between print requests. */
10114 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10115 bset_truncate_lines (current_buffer, Qnil);
10116 }
10117 }
10118 }
10119
10120
10121 /* Display an echo area message in window W. Value is non-zero if W's
10122 height is changed. If display_last_displayed_message_p is
10123 non-zero, display the message that was last displayed, otherwise
10124 display the current message. */
10125
10126 static int
10127 display_echo_area (struct window *w)
10128 {
10129 int i, no_message_p, window_height_changed_p;
10130
10131 /* Temporarily disable garbage collections while displaying the echo
10132 area. This is done because a GC can print a message itself.
10133 That message would modify the echo area buffer's contents while a
10134 redisplay of the buffer is going on, and seriously confuse
10135 redisplay. */
10136 ptrdiff_t count = inhibit_garbage_collection ();
10137
10138 /* If there is no message, we must call display_echo_area_1
10139 nevertheless because it resizes the window. But we will have to
10140 reset the echo_area_buffer in question to nil at the end because
10141 with_echo_area_buffer will sets it to an empty buffer. */
10142 i = display_last_displayed_message_p ? 1 : 0;
10143 no_message_p = NILP (echo_area_buffer[i]);
10144
10145 window_height_changed_p
10146 = with_echo_area_buffer (w, display_last_displayed_message_p,
10147 display_echo_area_1,
10148 (intptr_t) w, Qnil);
10149
10150 if (no_message_p)
10151 echo_area_buffer[i] = Qnil;
10152
10153 unbind_to (count, Qnil);
10154 return window_height_changed_p;
10155 }
10156
10157
10158 /* Helper for display_echo_area. Display the current buffer which
10159 contains the current echo area message in window W, a mini-window,
10160 a pointer to which is passed in A1. A2..A4 are currently not used.
10161 Change the height of W so that all of the message is displayed.
10162 Value is non-zero if height of W was changed. */
10163
10164 static int
10165 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10166 {
10167 intptr_t i1 = a1;
10168 struct window *w = (struct window *) i1;
10169 Lisp_Object window;
10170 struct text_pos start;
10171 int window_height_changed_p = 0;
10172
10173 /* Do this before displaying, so that we have a large enough glyph
10174 matrix for the display. If we can't get enough space for the
10175 whole text, display the last N lines. That works by setting w->start. */
10176 window_height_changed_p = resize_mini_window (w, 0);
10177
10178 /* Use the starting position chosen by resize_mini_window. */
10179 SET_TEXT_POS_FROM_MARKER (start, w->start);
10180
10181 /* Display. */
10182 clear_glyph_matrix (w->desired_matrix);
10183 XSETWINDOW (window, w);
10184 try_window (window, start, 0);
10185
10186 return window_height_changed_p;
10187 }
10188
10189
10190 /* Resize the echo area window to exactly the size needed for the
10191 currently displayed message, if there is one. If a mini-buffer
10192 is active, don't shrink it. */
10193
10194 void
10195 resize_echo_area_exactly (void)
10196 {
10197 if (BUFFERP (echo_area_buffer[0])
10198 && WINDOWP (echo_area_window))
10199 {
10200 struct window *w = XWINDOW (echo_area_window);
10201 int resized_p;
10202 Lisp_Object resize_exactly;
10203
10204 if (minibuf_level == 0)
10205 resize_exactly = Qt;
10206 else
10207 resize_exactly = Qnil;
10208
10209 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10210 (intptr_t) w, resize_exactly);
10211 if (resized_p)
10212 {
10213 ++windows_or_buffers_changed;
10214 ++update_mode_lines;
10215 redisplay_internal ();
10216 }
10217 }
10218 }
10219
10220
10221 /* Callback function for with_echo_area_buffer, when used from
10222 resize_echo_area_exactly. A1 contains a pointer to the window to
10223 resize, EXACTLY non-nil means resize the mini-window exactly to the
10224 size of the text displayed. A3 and A4 are not used. Value is what
10225 resize_mini_window returns. */
10226
10227 static int
10228 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10229 {
10230 intptr_t i1 = a1;
10231 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10232 }
10233
10234
10235 /* Resize mini-window W to fit the size of its contents. EXACT_P
10236 means size the window exactly to the size needed. Otherwise, it's
10237 only enlarged until W's buffer is empty.
10238
10239 Set W->start to the right place to begin display. If the whole
10240 contents fit, start at the beginning. Otherwise, start so as
10241 to make the end of the contents appear. This is particularly
10242 important for y-or-n-p, but seems desirable generally.
10243
10244 Value is non-zero if the window height has been changed. */
10245
10246 int
10247 resize_mini_window (struct window *w, int exact_p)
10248 {
10249 struct frame *f = XFRAME (w->frame);
10250 int window_height_changed_p = 0;
10251
10252 eassert (MINI_WINDOW_P (w));
10253
10254 /* By default, start display at the beginning. */
10255 set_marker_both (w->start, w->buffer,
10256 BUF_BEGV (XBUFFER (w->buffer)),
10257 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10258
10259 /* Don't resize windows while redisplaying a window; it would
10260 confuse redisplay functions when the size of the window they are
10261 displaying changes from under them. Such a resizing can happen,
10262 for instance, when which-func prints a long message while
10263 we are running fontification-functions. We're running these
10264 functions with safe_call which binds inhibit-redisplay to t. */
10265 if (!NILP (Vinhibit_redisplay))
10266 return 0;
10267
10268 /* Nil means don't try to resize. */
10269 if (NILP (Vresize_mini_windows)
10270 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10271 return 0;
10272
10273 if (!FRAME_MINIBUF_ONLY_P (f))
10274 {
10275 struct it it;
10276 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10277 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10278 int height;
10279 EMACS_INT max_height;
10280 int unit = FRAME_LINE_HEIGHT (f);
10281 struct text_pos start;
10282 struct buffer *old_current_buffer = NULL;
10283
10284 if (current_buffer != XBUFFER (w->buffer))
10285 {
10286 old_current_buffer = current_buffer;
10287 set_buffer_internal (XBUFFER (w->buffer));
10288 }
10289
10290 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10291
10292 /* Compute the max. number of lines specified by the user. */
10293 if (FLOATP (Vmax_mini_window_height))
10294 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10295 else if (INTEGERP (Vmax_mini_window_height))
10296 max_height = XINT (Vmax_mini_window_height);
10297 else
10298 max_height = total_height / 4;
10299
10300 /* Correct that max. height if it's bogus. */
10301 max_height = clip_to_bounds (1, max_height, total_height);
10302
10303 /* Find out the height of the text in the window. */
10304 if (it.line_wrap == TRUNCATE)
10305 height = 1;
10306 else
10307 {
10308 last_height = 0;
10309 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10310 if (it.max_ascent == 0 && it.max_descent == 0)
10311 height = it.current_y + last_height;
10312 else
10313 height = it.current_y + it.max_ascent + it.max_descent;
10314 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10315 height = (height + unit - 1) / unit;
10316 }
10317
10318 /* Compute a suitable window start. */
10319 if (height > max_height)
10320 {
10321 height = max_height;
10322 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10323 move_it_vertically_backward (&it, (height - 1) * unit);
10324 start = it.current.pos;
10325 }
10326 else
10327 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10328 SET_MARKER_FROM_TEXT_POS (w->start, start);
10329
10330 if (EQ (Vresize_mini_windows, Qgrow_only))
10331 {
10332 /* Let it grow only, until we display an empty message, in which
10333 case the window shrinks again. */
10334 if (height > WINDOW_TOTAL_LINES (w))
10335 {
10336 int old_height = WINDOW_TOTAL_LINES (w);
10337 freeze_window_starts (f, 1);
10338 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10339 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10340 }
10341 else if (height < WINDOW_TOTAL_LINES (w)
10342 && (exact_p || BEGV == ZV))
10343 {
10344 int old_height = WINDOW_TOTAL_LINES (w);
10345 freeze_window_starts (f, 0);
10346 shrink_mini_window (w);
10347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10348 }
10349 }
10350 else
10351 {
10352 /* Always resize to exact size needed. */
10353 if (height > WINDOW_TOTAL_LINES (w))
10354 {
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356 freeze_window_starts (f, 1);
10357 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10359 }
10360 else if (height < WINDOW_TOTAL_LINES (w))
10361 {
10362 int old_height = WINDOW_TOTAL_LINES (w);
10363 freeze_window_starts (f, 0);
10364 shrink_mini_window (w);
10365
10366 if (height)
10367 {
10368 freeze_window_starts (f, 1);
10369 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10370 }
10371
10372 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10373 }
10374 }
10375
10376 if (old_current_buffer)
10377 set_buffer_internal (old_current_buffer);
10378 }
10379
10380 return window_height_changed_p;
10381 }
10382
10383
10384 /* Value is the current message, a string, or nil if there is no
10385 current message. */
10386
10387 Lisp_Object
10388 current_message (void)
10389 {
10390 Lisp_Object msg;
10391
10392 if (!BUFFERP (echo_area_buffer[0]))
10393 msg = Qnil;
10394 else
10395 {
10396 with_echo_area_buffer (0, 0, current_message_1,
10397 (intptr_t) &msg, Qnil);
10398 if (NILP (msg))
10399 echo_area_buffer[0] = Qnil;
10400 }
10401
10402 return msg;
10403 }
10404
10405
10406 static int
10407 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10408 {
10409 intptr_t i1 = a1;
10410 Lisp_Object *msg = (Lisp_Object *) i1;
10411
10412 if (Z > BEG)
10413 *msg = make_buffer_string (BEG, Z, 1);
10414 else
10415 *msg = Qnil;
10416 return 0;
10417 }
10418
10419
10420 /* Push the current message on Vmessage_stack for later restoration
10421 by restore_message. Value is non-zero if the current message isn't
10422 empty. This is a relatively infrequent operation, so it's not
10423 worth optimizing. */
10424
10425 bool
10426 push_message (void)
10427 {
10428 Lisp_Object msg = current_message ();
10429 Vmessage_stack = Fcons (msg, Vmessage_stack);
10430 return STRINGP (msg);
10431 }
10432
10433
10434 /* Restore message display from the top of Vmessage_stack. */
10435
10436 void
10437 restore_message (void)
10438 {
10439 eassert (CONSP (Vmessage_stack));
10440 message3_nolog (XCAR (Vmessage_stack));
10441 }
10442
10443
10444 /* Handler for record_unwind_protect calling pop_message. */
10445
10446 Lisp_Object
10447 pop_message_unwind (Lisp_Object dummy)
10448 {
10449 pop_message ();
10450 return Qnil;
10451 }
10452
10453 /* Pop the top-most entry off Vmessage_stack. */
10454
10455 static void
10456 pop_message (void)
10457 {
10458 eassert (CONSP (Vmessage_stack));
10459 Vmessage_stack = XCDR (Vmessage_stack);
10460 }
10461
10462
10463 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10464 exits. If the stack is not empty, we have a missing pop_message
10465 somewhere. */
10466
10467 void
10468 check_message_stack (void)
10469 {
10470 if (!NILP (Vmessage_stack))
10471 emacs_abort ();
10472 }
10473
10474
10475 /* Truncate to NCHARS what will be displayed in the echo area the next
10476 time we display it---but don't redisplay it now. */
10477
10478 void
10479 truncate_echo_area (ptrdiff_t nchars)
10480 {
10481 if (nchars == 0)
10482 echo_area_buffer[0] = Qnil;
10483 else if (!noninteractive
10484 && INTERACTIVE
10485 && !NILP (echo_area_buffer[0]))
10486 {
10487 struct frame *sf = SELECTED_FRAME ();
10488 /* Error messages get reported properly by cmd_error, so this must be
10489 just an informative message; if the frame hasn't really been
10490 initialized yet, just toss it. */
10491 if (sf->glyphs_initialized_p)
10492 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10493 }
10494 }
10495
10496
10497 /* Helper function for truncate_echo_area. Truncate the current
10498 message to at most NCHARS characters. */
10499
10500 static int
10501 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10502 {
10503 if (BEG + nchars < Z)
10504 del_range (BEG + nchars, Z);
10505 if (Z == BEG)
10506 echo_area_buffer[0] = Qnil;
10507 return 0;
10508 }
10509
10510 /* Set the current message to STRING. */
10511
10512 static void
10513 set_message (Lisp_Object string)
10514 {
10515 eassert (STRINGP (string));
10516
10517 message_enable_multibyte = STRING_MULTIBYTE (string);
10518
10519 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10520 message_buf_print = 0;
10521 help_echo_showing_p = 0;
10522
10523 if (STRINGP (Vdebug_on_message)
10524 && fast_string_match (Vdebug_on_message, string) >= 0)
10525 call_debugger (list2 (Qerror, string));
10526 }
10527
10528
10529 /* Helper function for set_message. First argument is ignored and second
10530 argument has the same meaning as for set_message.
10531 This function is called with the echo area buffer being current. */
10532
10533 static int
10534 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10535 {
10536 eassert (STRINGP (string));
10537
10538 /* Change multibyteness of the echo buffer appropriately. */
10539 if (message_enable_multibyte
10540 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10541 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10542
10543 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10544 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10545 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10546
10547 /* Insert new message at BEG. */
10548 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10549
10550 /* This function takes care of single/multibyte conversion.
10551 We just have to ensure that the echo area buffer has the right
10552 setting of enable_multibyte_characters. */
10553 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10554
10555 return 0;
10556 }
10557
10558
10559 /* Clear messages. CURRENT_P non-zero means clear the current
10560 message. LAST_DISPLAYED_P non-zero means clear the message
10561 last displayed. */
10562
10563 void
10564 clear_message (int current_p, int last_displayed_p)
10565 {
10566 if (current_p)
10567 {
10568 echo_area_buffer[0] = Qnil;
10569 message_cleared_p = 1;
10570 }
10571
10572 if (last_displayed_p)
10573 echo_area_buffer[1] = Qnil;
10574
10575 message_buf_print = 0;
10576 }
10577
10578 /* Clear garbaged frames.
10579
10580 This function is used where the old redisplay called
10581 redraw_garbaged_frames which in turn called redraw_frame which in
10582 turn called clear_frame. The call to clear_frame was a source of
10583 flickering. I believe a clear_frame is not necessary. It should
10584 suffice in the new redisplay to invalidate all current matrices,
10585 and ensure a complete redisplay of all windows. */
10586
10587 static void
10588 clear_garbaged_frames (void)
10589 {
10590 if (frame_garbaged)
10591 {
10592 Lisp_Object tail, frame;
10593 int changed_count = 0;
10594
10595 FOR_EACH_FRAME (tail, frame)
10596 {
10597 struct frame *f = XFRAME (frame);
10598
10599 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10600 {
10601 if (f->resized_p)
10602 {
10603 redraw_frame (f);
10604 f->force_flush_display_p = 1;
10605 }
10606 clear_current_matrices (f);
10607 changed_count++;
10608 f->garbaged = 0;
10609 f->resized_p = 0;
10610 }
10611 }
10612
10613 frame_garbaged = 0;
10614 if (changed_count)
10615 ++windows_or_buffers_changed;
10616 }
10617 }
10618
10619
10620 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10621 is non-zero update selected_frame. Value is non-zero if the
10622 mini-windows height has been changed. */
10623
10624 static int
10625 echo_area_display (int update_frame_p)
10626 {
10627 Lisp_Object mini_window;
10628 struct window *w;
10629 struct frame *f;
10630 int window_height_changed_p = 0;
10631 struct frame *sf = SELECTED_FRAME ();
10632
10633 mini_window = FRAME_MINIBUF_WINDOW (sf);
10634 w = XWINDOW (mini_window);
10635 f = XFRAME (WINDOW_FRAME (w));
10636
10637 /* Don't display if frame is invisible or not yet initialized. */
10638 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10639 return 0;
10640
10641 #ifdef HAVE_WINDOW_SYSTEM
10642 /* When Emacs starts, selected_frame may be the initial terminal
10643 frame. If we let this through, a message would be displayed on
10644 the terminal. */
10645 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10646 return 0;
10647 #endif /* HAVE_WINDOW_SYSTEM */
10648
10649 /* Redraw garbaged frames. */
10650 clear_garbaged_frames ();
10651
10652 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10653 {
10654 echo_area_window = mini_window;
10655 window_height_changed_p = display_echo_area (w);
10656 w->must_be_updated_p = 1;
10657
10658 /* Update the display, unless called from redisplay_internal.
10659 Also don't update the screen during redisplay itself. The
10660 update will happen at the end of redisplay, and an update
10661 here could cause confusion. */
10662 if (update_frame_p && !redisplaying_p)
10663 {
10664 int n = 0;
10665
10666 /* If the display update has been interrupted by pending
10667 input, update mode lines in the frame. Due to the
10668 pending input, it might have been that redisplay hasn't
10669 been called, so that mode lines above the echo area are
10670 garbaged. This looks odd, so we prevent it here. */
10671 if (!display_completed)
10672 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10673
10674 if (window_height_changed_p
10675 /* Don't do this if Emacs is shutting down. Redisplay
10676 needs to run hooks. */
10677 && !NILP (Vrun_hooks))
10678 {
10679 /* Must update other windows. Likewise as in other
10680 cases, don't let this update be interrupted by
10681 pending input. */
10682 ptrdiff_t count = SPECPDL_INDEX ();
10683 specbind (Qredisplay_dont_pause, Qt);
10684 windows_or_buffers_changed = 1;
10685 redisplay_internal ();
10686 unbind_to (count, Qnil);
10687 }
10688 else if (FRAME_WINDOW_P (f) && n == 0)
10689 {
10690 /* Window configuration is the same as before.
10691 Can do with a display update of the echo area,
10692 unless we displayed some mode lines. */
10693 update_single_window (w, 1);
10694 FRAME_RIF (f)->flush_display (f);
10695 }
10696 else
10697 update_frame (f, 1, 1);
10698
10699 /* If cursor is in the echo area, make sure that the next
10700 redisplay displays the minibuffer, so that the cursor will
10701 be replaced with what the minibuffer wants. */
10702 if (cursor_in_echo_area)
10703 ++windows_or_buffers_changed;
10704 }
10705 }
10706 else if (!EQ (mini_window, selected_window))
10707 windows_or_buffers_changed++;
10708
10709 /* Last displayed message is now the current message. */
10710 echo_area_buffer[1] = echo_area_buffer[0];
10711 /* Inform read_char that we're not echoing. */
10712 echo_message_buffer = Qnil;
10713
10714 /* Prevent redisplay optimization in redisplay_internal by resetting
10715 this_line_start_pos. This is done because the mini-buffer now
10716 displays the message instead of its buffer text. */
10717 if (EQ (mini_window, selected_window))
10718 CHARPOS (this_line_start_pos) = 0;
10719
10720 return window_height_changed_p;
10721 }
10722
10723 /* Nonzero if the current window's buffer is shown in more than one
10724 window and was modified since last redisplay. */
10725
10726 static int
10727 buffer_shared_and_changed (void)
10728 {
10729 return (buffer_window_count (current_buffer) > 1
10730 && UNCHANGED_MODIFIED < MODIFF);
10731 }
10732
10733 /* Nonzero if W doesn't reflect the actual state of current buffer due
10734 to its text or overlays change. FIXME: this may be called when
10735 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10736
10737 static int
10738 window_outdated (struct window *w)
10739 {
10740 return (w->last_modified < MODIFF
10741 || w->last_overlay_modified < OVERLAY_MODIFF);
10742 }
10743
10744 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10745 is enabled and mark of W's buffer was changed since last W's update. */
10746
10747 static int
10748 window_buffer_changed (struct window *w)
10749 {
10750 struct buffer *b = XBUFFER (w->buffer);
10751
10752 eassert (BUFFER_LIVE_P (b));
10753
10754 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10755 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10756 != (w->region_showing != 0)));
10757 }
10758
10759 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10760
10761 static int
10762 mode_line_update_needed (struct window *w)
10763 {
10764 return (w->column_number_displayed != -1
10765 && !(PT == w->last_point && !window_outdated (w))
10766 && (w->column_number_displayed != current_column ()));
10767 }
10768
10769 /***********************************************************************
10770 Mode Lines and Frame Titles
10771 ***********************************************************************/
10772
10773 /* A buffer for constructing non-propertized mode-line strings and
10774 frame titles in it; allocated from the heap in init_xdisp and
10775 resized as needed in store_mode_line_noprop_char. */
10776
10777 static char *mode_line_noprop_buf;
10778
10779 /* The buffer's end, and a current output position in it. */
10780
10781 static char *mode_line_noprop_buf_end;
10782 static char *mode_line_noprop_ptr;
10783
10784 #define MODE_LINE_NOPROP_LEN(start) \
10785 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10786
10787 static enum {
10788 MODE_LINE_DISPLAY = 0,
10789 MODE_LINE_TITLE,
10790 MODE_LINE_NOPROP,
10791 MODE_LINE_STRING
10792 } mode_line_target;
10793
10794 /* Alist that caches the results of :propertize.
10795 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10796 static Lisp_Object mode_line_proptrans_alist;
10797
10798 /* List of strings making up the mode-line. */
10799 static Lisp_Object mode_line_string_list;
10800
10801 /* Base face property when building propertized mode line string. */
10802 static Lisp_Object mode_line_string_face;
10803 static Lisp_Object mode_line_string_face_prop;
10804
10805
10806 /* Unwind data for mode line strings */
10807
10808 static Lisp_Object Vmode_line_unwind_vector;
10809
10810 static Lisp_Object
10811 format_mode_line_unwind_data (struct frame *target_frame,
10812 struct buffer *obuf,
10813 Lisp_Object owin,
10814 int save_proptrans)
10815 {
10816 Lisp_Object vector, tmp;
10817
10818 /* Reduce consing by keeping one vector in
10819 Vwith_echo_area_save_vector. */
10820 vector = Vmode_line_unwind_vector;
10821 Vmode_line_unwind_vector = Qnil;
10822
10823 if (NILP (vector))
10824 vector = Fmake_vector (make_number (10), Qnil);
10825
10826 ASET (vector, 0, make_number (mode_line_target));
10827 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10828 ASET (vector, 2, mode_line_string_list);
10829 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10830 ASET (vector, 4, mode_line_string_face);
10831 ASET (vector, 5, mode_line_string_face_prop);
10832
10833 if (obuf)
10834 XSETBUFFER (tmp, obuf);
10835 else
10836 tmp = Qnil;
10837 ASET (vector, 6, tmp);
10838 ASET (vector, 7, owin);
10839 if (target_frame)
10840 {
10841 /* Similarly to `with-selected-window', if the operation selects
10842 a window on another frame, we must restore that frame's
10843 selected window, and (for a tty) the top-frame. */
10844 ASET (vector, 8, target_frame->selected_window);
10845 if (FRAME_TERMCAP_P (target_frame))
10846 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10847 }
10848
10849 return vector;
10850 }
10851
10852 static Lisp_Object
10853 unwind_format_mode_line (Lisp_Object vector)
10854 {
10855 Lisp_Object old_window = AREF (vector, 7);
10856 Lisp_Object target_frame_window = AREF (vector, 8);
10857 Lisp_Object old_top_frame = AREF (vector, 9);
10858
10859 mode_line_target = XINT (AREF (vector, 0));
10860 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10861 mode_line_string_list = AREF (vector, 2);
10862 if (! EQ (AREF (vector, 3), Qt))
10863 mode_line_proptrans_alist = AREF (vector, 3);
10864 mode_line_string_face = AREF (vector, 4);
10865 mode_line_string_face_prop = AREF (vector, 5);
10866
10867 /* Select window before buffer, since it may change the buffer. */
10868 if (!NILP (old_window))
10869 {
10870 /* If the operation that we are unwinding had selected a window
10871 on a different frame, reset its frame-selected-window. For a
10872 text terminal, reset its top-frame if necessary. */
10873 if (!NILP (target_frame_window))
10874 {
10875 Lisp_Object frame
10876 = WINDOW_FRAME (XWINDOW (target_frame_window));
10877
10878 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10879 Fselect_window (target_frame_window, Qt);
10880
10881 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10882 Fselect_frame (old_top_frame, Qt);
10883 }
10884
10885 Fselect_window (old_window, Qt);
10886 }
10887
10888 if (!NILP (AREF (vector, 6)))
10889 {
10890 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10891 ASET (vector, 6, Qnil);
10892 }
10893
10894 Vmode_line_unwind_vector = vector;
10895 return Qnil;
10896 }
10897
10898
10899 /* Store a single character C for the frame title in mode_line_noprop_buf.
10900 Re-allocate mode_line_noprop_buf if necessary. */
10901
10902 static void
10903 store_mode_line_noprop_char (char c)
10904 {
10905 /* If output position has reached the end of the allocated buffer,
10906 increase the buffer's size. */
10907 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10908 {
10909 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10910 ptrdiff_t size = len;
10911 mode_line_noprop_buf =
10912 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10913 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10914 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10915 }
10916
10917 *mode_line_noprop_ptr++ = c;
10918 }
10919
10920
10921 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10922 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10923 characters that yield more columns than PRECISION; PRECISION <= 0
10924 means copy the whole string. Pad with spaces until FIELD_WIDTH
10925 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10926 pad. Called from display_mode_element when it is used to build a
10927 frame title. */
10928
10929 static int
10930 store_mode_line_noprop (const char *string, int field_width, int precision)
10931 {
10932 const unsigned char *str = (const unsigned char *) string;
10933 int n = 0;
10934 ptrdiff_t dummy, nbytes;
10935
10936 /* Copy at most PRECISION chars from STR. */
10937 nbytes = strlen (string);
10938 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10939 while (nbytes--)
10940 store_mode_line_noprop_char (*str++);
10941
10942 /* Fill up with spaces until FIELD_WIDTH reached. */
10943 while (field_width > 0
10944 && n < field_width)
10945 {
10946 store_mode_line_noprop_char (' ');
10947 ++n;
10948 }
10949
10950 return n;
10951 }
10952
10953 /***********************************************************************
10954 Frame Titles
10955 ***********************************************************************/
10956
10957 #ifdef HAVE_WINDOW_SYSTEM
10958
10959 /* Set the title of FRAME, if it has changed. The title format is
10960 Vicon_title_format if FRAME is iconified, otherwise it is
10961 frame_title_format. */
10962
10963 static void
10964 x_consider_frame_title (Lisp_Object frame)
10965 {
10966 struct frame *f = XFRAME (frame);
10967
10968 if (FRAME_WINDOW_P (f)
10969 || FRAME_MINIBUF_ONLY_P (f)
10970 || f->explicit_name)
10971 {
10972 /* Do we have more than one visible frame on this X display? */
10973 Lisp_Object tail, other_frame, fmt;
10974 ptrdiff_t title_start;
10975 char *title;
10976 ptrdiff_t len;
10977 struct it it;
10978 ptrdiff_t count = SPECPDL_INDEX ();
10979
10980 FOR_EACH_FRAME (tail, other_frame)
10981 {
10982 struct frame *tf = XFRAME (other_frame);
10983
10984 if (tf != f
10985 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10986 && !FRAME_MINIBUF_ONLY_P (tf)
10987 && !EQ (other_frame, tip_frame)
10988 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10989 break;
10990 }
10991
10992 /* Set global variable indicating that multiple frames exist. */
10993 multiple_frames = CONSP (tail);
10994
10995 /* Switch to the buffer of selected window of the frame. Set up
10996 mode_line_target so that display_mode_element will output into
10997 mode_line_noprop_buf; then display the title. */
10998 record_unwind_protect (unwind_format_mode_line,
10999 format_mode_line_unwind_data
11000 (f, current_buffer, selected_window, 0));
11001
11002 Fselect_window (f->selected_window, Qt);
11003 set_buffer_internal_1
11004 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11005 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11006
11007 mode_line_target = MODE_LINE_TITLE;
11008 title_start = MODE_LINE_NOPROP_LEN (0);
11009 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11010 NULL, DEFAULT_FACE_ID);
11011 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11012 len = MODE_LINE_NOPROP_LEN (title_start);
11013 title = mode_line_noprop_buf + title_start;
11014 unbind_to (count, Qnil);
11015
11016 /* Set the title only if it's changed. This avoids consing in
11017 the common case where it hasn't. (If it turns out that we've
11018 already wasted too much time by walking through the list with
11019 display_mode_element, then we might need to optimize at a
11020 higher level than this.) */
11021 if (! STRINGP (f->name)
11022 || SBYTES (f->name) != len
11023 || memcmp (title, SDATA (f->name), len) != 0)
11024 x_implicitly_set_name (f, make_string (title, len), Qnil);
11025 }
11026 }
11027
11028 #endif /* not HAVE_WINDOW_SYSTEM */
11029
11030 \f
11031 /***********************************************************************
11032 Menu Bars
11033 ***********************************************************************/
11034
11035
11036 /* Prepare for redisplay by updating menu-bar item lists when
11037 appropriate. This can call eval. */
11038
11039 void
11040 prepare_menu_bars (void)
11041 {
11042 int all_windows;
11043 struct gcpro gcpro1, gcpro2;
11044 struct frame *f;
11045 Lisp_Object tooltip_frame;
11046
11047 #ifdef HAVE_WINDOW_SYSTEM
11048 tooltip_frame = tip_frame;
11049 #else
11050 tooltip_frame = Qnil;
11051 #endif
11052
11053 /* Update all frame titles based on their buffer names, etc. We do
11054 this before the menu bars so that the buffer-menu will show the
11055 up-to-date frame titles. */
11056 #ifdef HAVE_WINDOW_SYSTEM
11057 if (windows_or_buffers_changed || update_mode_lines)
11058 {
11059 Lisp_Object tail, frame;
11060
11061 FOR_EACH_FRAME (tail, frame)
11062 {
11063 f = XFRAME (frame);
11064 if (!EQ (frame, tooltip_frame)
11065 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11066 x_consider_frame_title (frame);
11067 }
11068 }
11069 #endif /* HAVE_WINDOW_SYSTEM */
11070
11071 /* Update the menu bar item lists, if appropriate. This has to be
11072 done before any actual redisplay or generation of display lines. */
11073 all_windows = (update_mode_lines
11074 || buffer_shared_and_changed ()
11075 || windows_or_buffers_changed);
11076 if (all_windows)
11077 {
11078 Lisp_Object tail, frame;
11079 ptrdiff_t count = SPECPDL_INDEX ();
11080 /* 1 means that update_menu_bar has run its hooks
11081 so any further calls to update_menu_bar shouldn't do so again. */
11082 int menu_bar_hooks_run = 0;
11083
11084 record_unwind_save_match_data ();
11085
11086 FOR_EACH_FRAME (tail, frame)
11087 {
11088 f = XFRAME (frame);
11089
11090 /* Ignore tooltip frame. */
11091 if (EQ (frame, tooltip_frame))
11092 continue;
11093
11094 /* If a window on this frame changed size, report that to
11095 the user and clear the size-change flag. */
11096 if (FRAME_WINDOW_SIZES_CHANGED (f))
11097 {
11098 Lisp_Object functions;
11099
11100 /* Clear flag first in case we get an error below. */
11101 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11102 functions = Vwindow_size_change_functions;
11103 GCPRO2 (tail, functions);
11104
11105 while (CONSP (functions))
11106 {
11107 if (!EQ (XCAR (functions), Qt))
11108 call1 (XCAR (functions), frame);
11109 functions = XCDR (functions);
11110 }
11111 UNGCPRO;
11112 }
11113
11114 GCPRO1 (tail);
11115 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11116 #ifdef HAVE_WINDOW_SYSTEM
11117 update_tool_bar (f, 0);
11118 #endif
11119 #ifdef HAVE_NS
11120 if (windows_or_buffers_changed
11121 && FRAME_NS_P (f))
11122 ns_set_doc_edited
11123 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11124 #endif
11125 UNGCPRO;
11126 }
11127
11128 unbind_to (count, Qnil);
11129 }
11130 else
11131 {
11132 struct frame *sf = SELECTED_FRAME ();
11133 update_menu_bar (sf, 1, 0);
11134 #ifdef HAVE_WINDOW_SYSTEM
11135 update_tool_bar (sf, 1);
11136 #endif
11137 }
11138 }
11139
11140
11141 /* Update the menu bar item list for frame F. This has to be done
11142 before we start to fill in any display lines, because it can call
11143 eval.
11144
11145 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11146
11147 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11148 already ran the menu bar hooks for this redisplay, so there
11149 is no need to run them again. The return value is the
11150 updated value of this flag, to pass to the next call. */
11151
11152 static int
11153 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11154 {
11155 Lisp_Object window;
11156 register struct window *w;
11157
11158 /* If called recursively during a menu update, do nothing. This can
11159 happen when, for instance, an activate-menubar-hook causes a
11160 redisplay. */
11161 if (inhibit_menubar_update)
11162 return hooks_run;
11163
11164 window = FRAME_SELECTED_WINDOW (f);
11165 w = XWINDOW (window);
11166
11167 if (FRAME_WINDOW_P (f)
11168 ?
11169 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11170 || defined (HAVE_NS) || defined (USE_GTK)
11171 FRAME_EXTERNAL_MENU_BAR (f)
11172 #else
11173 FRAME_MENU_BAR_LINES (f) > 0
11174 #endif
11175 : FRAME_MENU_BAR_LINES (f) > 0)
11176 {
11177 /* If the user has switched buffers or windows, we need to
11178 recompute to reflect the new bindings. But we'll
11179 recompute when update_mode_lines is set too; that means
11180 that people can use force-mode-line-update to request
11181 that the menu bar be recomputed. The adverse effect on
11182 the rest of the redisplay algorithm is about the same as
11183 windows_or_buffers_changed anyway. */
11184 if (windows_or_buffers_changed
11185 /* This used to test w->update_mode_line, but we believe
11186 there is no need to recompute the menu in that case. */
11187 || update_mode_lines
11188 || window_buffer_changed (w))
11189 {
11190 struct buffer *prev = current_buffer;
11191 ptrdiff_t count = SPECPDL_INDEX ();
11192
11193 specbind (Qinhibit_menubar_update, Qt);
11194
11195 set_buffer_internal_1 (XBUFFER (w->buffer));
11196 if (save_match_data)
11197 record_unwind_save_match_data ();
11198 if (NILP (Voverriding_local_map_menu_flag))
11199 {
11200 specbind (Qoverriding_terminal_local_map, Qnil);
11201 specbind (Qoverriding_local_map, Qnil);
11202 }
11203
11204 if (!hooks_run)
11205 {
11206 /* Run the Lucid hook. */
11207 safe_run_hooks (Qactivate_menubar_hook);
11208
11209 /* If it has changed current-menubar from previous value,
11210 really recompute the menu-bar from the value. */
11211 if (! NILP (Vlucid_menu_bar_dirty_flag))
11212 call0 (Qrecompute_lucid_menubar);
11213
11214 safe_run_hooks (Qmenu_bar_update_hook);
11215
11216 hooks_run = 1;
11217 }
11218
11219 XSETFRAME (Vmenu_updating_frame, f);
11220 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11221
11222 /* Redisplay the menu bar in case we changed it. */
11223 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11224 || defined (HAVE_NS) || defined (USE_GTK)
11225 if (FRAME_WINDOW_P (f))
11226 {
11227 #if defined (HAVE_NS)
11228 /* All frames on Mac OS share the same menubar. So only
11229 the selected frame should be allowed to set it. */
11230 if (f == SELECTED_FRAME ())
11231 #endif
11232 set_frame_menubar (f, 0, 0);
11233 }
11234 else
11235 /* On a terminal screen, the menu bar is an ordinary screen
11236 line, and this makes it get updated. */
11237 w->update_mode_line = 1;
11238 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11239 /* In the non-toolkit version, the menu bar is an ordinary screen
11240 line, and this makes it get updated. */
11241 w->update_mode_line = 1;
11242 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11243
11244 unbind_to (count, Qnil);
11245 set_buffer_internal_1 (prev);
11246 }
11247 }
11248
11249 return hooks_run;
11250 }
11251
11252
11253 \f
11254 /***********************************************************************
11255 Output Cursor
11256 ***********************************************************************/
11257
11258 #ifdef HAVE_WINDOW_SYSTEM
11259
11260 /* EXPORT:
11261 Nominal cursor position -- where to draw output.
11262 HPOS and VPOS are window relative glyph matrix coordinates.
11263 X and Y are window relative pixel coordinates. */
11264
11265 struct cursor_pos output_cursor;
11266
11267
11268 /* EXPORT:
11269 Set the global variable output_cursor to CURSOR. All cursor
11270 positions are relative to updated_window. */
11271
11272 void
11273 set_output_cursor (struct cursor_pos *cursor)
11274 {
11275 output_cursor.hpos = cursor->hpos;
11276 output_cursor.vpos = cursor->vpos;
11277 output_cursor.x = cursor->x;
11278 output_cursor.y = cursor->y;
11279 }
11280
11281
11282 /* EXPORT for RIF:
11283 Set a nominal cursor position.
11284
11285 HPOS and VPOS are column/row positions in a window glyph matrix. X
11286 and Y are window text area relative pixel positions.
11287
11288 If this is done during an update, updated_window will contain the
11289 window that is being updated and the position is the future output
11290 cursor position for that window. If updated_window is null, use
11291 selected_window and display the cursor at the given position. */
11292
11293 void
11294 x_cursor_to (int vpos, int hpos, int y, int x)
11295 {
11296 struct window *w;
11297
11298 /* If updated_window is not set, work on selected_window. */
11299 if (updated_window)
11300 w = updated_window;
11301 else
11302 w = XWINDOW (selected_window);
11303
11304 /* Set the output cursor. */
11305 output_cursor.hpos = hpos;
11306 output_cursor.vpos = vpos;
11307 output_cursor.x = x;
11308 output_cursor.y = y;
11309
11310 /* If not called as part of an update, really display the cursor.
11311 This will also set the cursor position of W. */
11312 if (updated_window == NULL)
11313 {
11314 block_input ();
11315 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11316 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11317 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11318 unblock_input ();
11319 }
11320 }
11321
11322 #endif /* HAVE_WINDOW_SYSTEM */
11323
11324 \f
11325 /***********************************************************************
11326 Tool-bars
11327 ***********************************************************************/
11328
11329 #ifdef HAVE_WINDOW_SYSTEM
11330
11331 /* Where the mouse was last time we reported a mouse event. */
11332
11333 FRAME_PTR last_mouse_frame;
11334
11335 /* Tool-bar item index of the item on which a mouse button was pressed
11336 or -1. */
11337
11338 int last_tool_bar_item;
11339
11340 /* Select `frame' temporarily without running all the code in
11341 do_switch_frame.
11342 FIXME: Maybe do_switch_frame should be trimmed down similarly
11343 when `norecord' is set. */
11344 static Lisp_Object
11345 fast_set_selected_frame (Lisp_Object frame)
11346 {
11347 if (!EQ (selected_frame, frame))
11348 {
11349 selected_frame = frame;
11350 selected_window = XFRAME (frame)->selected_window;
11351 }
11352 return Qnil;
11353 }
11354
11355 /* Update the tool-bar item list for frame F. This has to be done
11356 before we start to fill in any display lines. Called from
11357 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11358 and restore it here. */
11359
11360 static void
11361 update_tool_bar (struct frame *f, int save_match_data)
11362 {
11363 #if defined (USE_GTK) || defined (HAVE_NS)
11364 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11365 #else
11366 int do_update = WINDOWP (f->tool_bar_window)
11367 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11368 #endif
11369
11370 if (do_update)
11371 {
11372 Lisp_Object window;
11373 struct window *w;
11374
11375 window = FRAME_SELECTED_WINDOW (f);
11376 w = XWINDOW (window);
11377
11378 /* If the user has switched buffers or windows, we need to
11379 recompute to reflect the new bindings. But we'll
11380 recompute when update_mode_lines is set too; that means
11381 that people can use force-mode-line-update to request
11382 that the menu bar be recomputed. The adverse effect on
11383 the rest of the redisplay algorithm is about the same as
11384 windows_or_buffers_changed anyway. */
11385 if (windows_or_buffers_changed
11386 || w->update_mode_line
11387 || update_mode_lines
11388 || window_buffer_changed (w))
11389 {
11390 struct buffer *prev = current_buffer;
11391 ptrdiff_t count = SPECPDL_INDEX ();
11392 Lisp_Object frame, new_tool_bar;
11393 int new_n_tool_bar;
11394 struct gcpro gcpro1;
11395
11396 /* Set current_buffer to the buffer of the selected
11397 window of the frame, so that we get the right local
11398 keymaps. */
11399 set_buffer_internal_1 (XBUFFER (w->buffer));
11400
11401 /* Save match data, if we must. */
11402 if (save_match_data)
11403 record_unwind_save_match_data ();
11404
11405 /* Make sure that we don't accidentally use bogus keymaps. */
11406 if (NILP (Voverriding_local_map_menu_flag))
11407 {
11408 specbind (Qoverriding_terminal_local_map, Qnil);
11409 specbind (Qoverriding_local_map, Qnil);
11410 }
11411
11412 GCPRO1 (new_tool_bar);
11413
11414 /* We must temporarily set the selected frame to this frame
11415 before calling tool_bar_items, because the calculation of
11416 the tool-bar keymap uses the selected frame (see
11417 `tool-bar-make-keymap' in tool-bar.el). */
11418 eassert (EQ (selected_window,
11419 /* Since we only explicitly preserve selected_frame,
11420 check that selected_window would be redundant. */
11421 XFRAME (selected_frame)->selected_window));
11422 record_unwind_protect (fast_set_selected_frame, selected_frame);
11423 XSETFRAME (frame, f);
11424 fast_set_selected_frame (frame);
11425
11426 /* Build desired tool-bar items from keymaps. */
11427 new_tool_bar
11428 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11429 &new_n_tool_bar);
11430
11431 /* Redisplay the tool-bar if we changed it. */
11432 if (new_n_tool_bar != f->n_tool_bar_items
11433 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11434 {
11435 /* Redisplay that happens asynchronously due to an expose event
11436 may access f->tool_bar_items. Make sure we update both
11437 variables within BLOCK_INPUT so no such event interrupts. */
11438 block_input ();
11439 fset_tool_bar_items (f, new_tool_bar);
11440 f->n_tool_bar_items = new_n_tool_bar;
11441 w->update_mode_line = 1;
11442 unblock_input ();
11443 }
11444
11445 UNGCPRO;
11446
11447 unbind_to (count, Qnil);
11448 set_buffer_internal_1 (prev);
11449 }
11450 }
11451 }
11452
11453
11454 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11455 F's desired tool-bar contents. F->tool_bar_items must have
11456 been set up previously by calling prepare_menu_bars. */
11457
11458 static void
11459 build_desired_tool_bar_string (struct frame *f)
11460 {
11461 int i, size, size_needed;
11462 struct gcpro gcpro1, gcpro2, gcpro3;
11463 Lisp_Object image, plist, props;
11464
11465 image = plist = props = Qnil;
11466 GCPRO3 (image, plist, props);
11467
11468 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11469 Otherwise, make a new string. */
11470
11471 /* The size of the string we might be able to reuse. */
11472 size = (STRINGP (f->desired_tool_bar_string)
11473 ? SCHARS (f->desired_tool_bar_string)
11474 : 0);
11475
11476 /* We need one space in the string for each image. */
11477 size_needed = f->n_tool_bar_items;
11478
11479 /* Reuse f->desired_tool_bar_string, if possible. */
11480 if (size < size_needed || NILP (f->desired_tool_bar_string))
11481 fset_desired_tool_bar_string
11482 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11483 else
11484 {
11485 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11486 Fremove_text_properties (make_number (0), make_number (size),
11487 props, f->desired_tool_bar_string);
11488 }
11489
11490 /* Put a `display' property on the string for the images to display,
11491 put a `menu_item' property on tool-bar items with a value that
11492 is the index of the item in F's tool-bar item vector. */
11493 for (i = 0; i < f->n_tool_bar_items; ++i)
11494 {
11495 #define PROP(IDX) \
11496 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11497
11498 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11499 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11500 int hmargin, vmargin, relief, idx, end;
11501
11502 /* If image is a vector, choose the image according to the
11503 button state. */
11504 image = PROP (TOOL_BAR_ITEM_IMAGES);
11505 if (VECTORP (image))
11506 {
11507 if (enabled_p)
11508 idx = (selected_p
11509 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11510 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11511 else
11512 idx = (selected_p
11513 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11514 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11515
11516 eassert (ASIZE (image) >= idx);
11517 image = AREF (image, idx);
11518 }
11519 else
11520 idx = -1;
11521
11522 /* Ignore invalid image specifications. */
11523 if (!valid_image_p (image))
11524 continue;
11525
11526 /* Display the tool-bar button pressed, or depressed. */
11527 plist = Fcopy_sequence (XCDR (image));
11528
11529 /* Compute margin and relief to draw. */
11530 relief = (tool_bar_button_relief >= 0
11531 ? tool_bar_button_relief
11532 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11533 hmargin = vmargin = relief;
11534
11535 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11536 INT_MAX - max (hmargin, vmargin)))
11537 {
11538 hmargin += XFASTINT (Vtool_bar_button_margin);
11539 vmargin += XFASTINT (Vtool_bar_button_margin);
11540 }
11541 else if (CONSP (Vtool_bar_button_margin))
11542 {
11543 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11544 INT_MAX - hmargin))
11545 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11546
11547 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11548 INT_MAX - vmargin))
11549 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11550 }
11551
11552 if (auto_raise_tool_bar_buttons_p)
11553 {
11554 /* Add a `:relief' property to the image spec if the item is
11555 selected. */
11556 if (selected_p)
11557 {
11558 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11559 hmargin -= relief;
11560 vmargin -= relief;
11561 }
11562 }
11563 else
11564 {
11565 /* If image is selected, display it pressed, i.e. with a
11566 negative relief. If it's not selected, display it with a
11567 raised relief. */
11568 plist = Fplist_put (plist, QCrelief,
11569 (selected_p
11570 ? make_number (-relief)
11571 : make_number (relief)));
11572 hmargin -= relief;
11573 vmargin -= relief;
11574 }
11575
11576 /* Put a margin around the image. */
11577 if (hmargin || vmargin)
11578 {
11579 if (hmargin == vmargin)
11580 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11581 else
11582 plist = Fplist_put (plist, QCmargin,
11583 Fcons (make_number (hmargin),
11584 make_number (vmargin)));
11585 }
11586
11587 /* If button is not enabled, and we don't have special images
11588 for the disabled state, make the image appear disabled by
11589 applying an appropriate algorithm to it. */
11590 if (!enabled_p && idx < 0)
11591 plist = Fplist_put (plist, QCconversion, Qdisabled);
11592
11593 /* Put a `display' text property on the string for the image to
11594 display. Put a `menu-item' property on the string that gives
11595 the start of this item's properties in the tool-bar items
11596 vector. */
11597 image = Fcons (Qimage, plist);
11598 props = list4 (Qdisplay, image,
11599 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11600
11601 /* Let the last image hide all remaining spaces in the tool bar
11602 string. The string can be longer than needed when we reuse a
11603 previous string. */
11604 if (i + 1 == f->n_tool_bar_items)
11605 end = SCHARS (f->desired_tool_bar_string);
11606 else
11607 end = i + 1;
11608 Fadd_text_properties (make_number (i), make_number (end),
11609 props, f->desired_tool_bar_string);
11610 #undef PROP
11611 }
11612
11613 UNGCPRO;
11614 }
11615
11616
11617 /* Display one line of the tool-bar of frame IT->f.
11618
11619 HEIGHT specifies the desired height of the tool-bar line.
11620 If the actual height of the glyph row is less than HEIGHT, the
11621 row's height is increased to HEIGHT, and the icons are centered
11622 vertically in the new height.
11623
11624 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11625 count a final empty row in case the tool-bar width exactly matches
11626 the window width.
11627 */
11628
11629 static void
11630 display_tool_bar_line (struct it *it, int height)
11631 {
11632 struct glyph_row *row = it->glyph_row;
11633 int max_x = it->last_visible_x;
11634 struct glyph *last;
11635
11636 prepare_desired_row (row);
11637 row->y = it->current_y;
11638
11639 /* Note that this isn't made use of if the face hasn't a box,
11640 so there's no need to check the face here. */
11641 it->start_of_box_run_p = 1;
11642
11643 while (it->current_x < max_x)
11644 {
11645 int x, n_glyphs_before, i, nglyphs;
11646 struct it it_before;
11647
11648 /* Get the next display element. */
11649 if (!get_next_display_element (it))
11650 {
11651 /* Don't count empty row if we are counting needed tool-bar lines. */
11652 if (height < 0 && !it->hpos)
11653 return;
11654 break;
11655 }
11656
11657 /* Produce glyphs. */
11658 n_glyphs_before = row->used[TEXT_AREA];
11659 it_before = *it;
11660
11661 PRODUCE_GLYPHS (it);
11662
11663 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11664 i = 0;
11665 x = it_before.current_x;
11666 while (i < nglyphs)
11667 {
11668 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11669
11670 if (x + glyph->pixel_width > max_x)
11671 {
11672 /* Glyph doesn't fit on line. Backtrack. */
11673 row->used[TEXT_AREA] = n_glyphs_before;
11674 *it = it_before;
11675 /* If this is the only glyph on this line, it will never fit on the
11676 tool-bar, so skip it. But ensure there is at least one glyph,
11677 so we don't accidentally disable the tool-bar. */
11678 if (n_glyphs_before == 0
11679 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11680 break;
11681 goto out;
11682 }
11683
11684 ++it->hpos;
11685 x += glyph->pixel_width;
11686 ++i;
11687 }
11688
11689 /* Stop at line end. */
11690 if (ITERATOR_AT_END_OF_LINE_P (it))
11691 break;
11692
11693 set_iterator_to_next (it, 1);
11694 }
11695
11696 out:;
11697
11698 row->displays_text_p = row->used[TEXT_AREA] != 0;
11699
11700 /* Use default face for the border below the tool bar.
11701
11702 FIXME: When auto-resize-tool-bars is grow-only, there is
11703 no additional border below the possibly empty tool-bar lines.
11704 So to make the extra empty lines look "normal", we have to
11705 use the tool-bar face for the border too. */
11706 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11707 it->face_id = DEFAULT_FACE_ID;
11708
11709 extend_face_to_end_of_line (it);
11710 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11711 last->right_box_line_p = 1;
11712 if (last == row->glyphs[TEXT_AREA])
11713 last->left_box_line_p = 1;
11714
11715 /* Make line the desired height and center it vertically. */
11716 if ((height -= it->max_ascent + it->max_descent) > 0)
11717 {
11718 /* Don't add more than one line height. */
11719 height %= FRAME_LINE_HEIGHT (it->f);
11720 it->max_ascent += height / 2;
11721 it->max_descent += (height + 1) / 2;
11722 }
11723
11724 compute_line_metrics (it);
11725
11726 /* If line is empty, make it occupy the rest of the tool-bar. */
11727 if (!row->displays_text_p)
11728 {
11729 row->height = row->phys_height = it->last_visible_y - row->y;
11730 row->visible_height = row->height;
11731 row->ascent = row->phys_ascent = 0;
11732 row->extra_line_spacing = 0;
11733 }
11734
11735 row->full_width_p = 1;
11736 row->continued_p = 0;
11737 row->truncated_on_left_p = 0;
11738 row->truncated_on_right_p = 0;
11739
11740 it->current_x = it->hpos = 0;
11741 it->current_y += row->height;
11742 ++it->vpos;
11743 ++it->glyph_row;
11744 }
11745
11746
11747 /* Max tool-bar height. */
11748
11749 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11750 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11751
11752 /* Value is the number of screen lines needed to make all tool-bar
11753 items of frame F visible. The number of actual rows needed is
11754 returned in *N_ROWS if non-NULL. */
11755
11756 static int
11757 tool_bar_lines_needed (struct frame *f, int *n_rows)
11758 {
11759 struct window *w = XWINDOW (f->tool_bar_window);
11760 struct it it;
11761 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11762 the desired matrix, so use (unused) mode-line row as temporary row to
11763 avoid destroying the first tool-bar row. */
11764 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11765
11766 /* Initialize an iterator for iteration over
11767 F->desired_tool_bar_string in the tool-bar window of frame F. */
11768 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11769 it.first_visible_x = 0;
11770 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11771 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11772 it.paragraph_embedding = L2R;
11773
11774 while (!ITERATOR_AT_END_P (&it))
11775 {
11776 clear_glyph_row (temp_row);
11777 it.glyph_row = temp_row;
11778 display_tool_bar_line (&it, -1);
11779 }
11780 clear_glyph_row (temp_row);
11781
11782 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11783 if (n_rows)
11784 *n_rows = it.vpos > 0 ? it.vpos : -1;
11785
11786 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11787 }
11788
11789
11790 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11791 0, 1, 0,
11792 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11793 If FRAME is nil or omitted, use the selected frame. */)
11794 (Lisp_Object frame)
11795 {
11796 struct frame *f = decode_any_frame (frame);
11797 struct window *w;
11798 int nlines = 0;
11799
11800 if (WINDOWP (f->tool_bar_window)
11801 && (w = XWINDOW (f->tool_bar_window),
11802 WINDOW_TOTAL_LINES (w) > 0))
11803 {
11804 update_tool_bar (f, 1);
11805 if (f->n_tool_bar_items)
11806 {
11807 build_desired_tool_bar_string (f);
11808 nlines = tool_bar_lines_needed (f, NULL);
11809 }
11810 }
11811
11812 return make_number (nlines);
11813 }
11814
11815
11816 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11817 height should be changed. */
11818
11819 static int
11820 redisplay_tool_bar (struct frame *f)
11821 {
11822 struct window *w;
11823 struct it it;
11824 struct glyph_row *row;
11825
11826 #if defined (USE_GTK) || defined (HAVE_NS)
11827 if (FRAME_EXTERNAL_TOOL_BAR (f))
11828 update_frame_tool_bar (f);
11829 return 0;
11830 #endif
11831
11832 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11833 do anything. This means you must start with tool-bar-lines
11834 non-zero to get the auto-sizing effect. Or in other words, you
11835 can turn off tool-bars by specifying tool-bar-lines zero. */
11836 if (!WINDOWP (f->tool_bar_window)
11837 || (w = XWINDOW (f->tool_bar_window),
11838 WINDOW_TOTAL_LINES (w) == 0))
11839 return 0;
11840
11841 /* Set up an iterator for the tool-bar window. */
11842 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11843 it.first_visible_x = 0;
11844 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11845 row = it.glyph_row;
11846
11847 /* Build a string that represents the contents of the tool-bar. */
11848 build_desired_tool_bar_string (f);
11849 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11850 /* FIXME: This should be controlled by a user option. But it
11851 doesn't make sense to have an R2L tool bar if the menu bar cannot
11852 be drawn also R2L, and making the menu bar R2L is tricky due
11853 toolkit-specific code that implements it. If an R2L tool bar is
11854 ever supported, display_tool_bar_line should also be augmented to
11855 call unproduce_glyphs like display_line and display_string
11856 do. */
11857 it.paragraph_embedding = L2R;
11858
11859 if (f->n_tool_bar_rows == 0)
11860 {
11861 int nlines;
11862
11863 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11864 nlines != WINDOW_TOTAL_LINES (w)))
11865 {
11866 Lisp_Object frame;
11867 int old_height = WINDOW_TOTAL_LINES (w);
11868
11869 XSETFRAME (frame, f);
11870 Fmodify_frame_parameters (frame,
11871 Fcons (Fcons (Qtool_bar_lines,
11872 make_number (nlines)),
11873 Qnil));
11874 if (WINDOW_TOTAL_LINES (w) != old_height)
11875 {
11876 clear_glyph_matrix (w->desired_matrix);
11877 fonts_changed_p = 1;
11878 return 1;
11879 }
11880 }
11881 }
11882
11883 /* Display as many lines as needed to display all tool-bar items. */
11884
11885 if (f->n_tool_bar_rows > 0)
11886 {
11887 int border, rows, height, extra;
11888
11889 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11890 border = XINT (Vtool_bar_border);
11891 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11892 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11893 else if (EQ (Vtool_bar_border, Qborder_width))
11894 border = f->border_width;
11895 else
11896 border = 0;
11897 if (border < 0)
11898 border = 0;
11899
11900 rows = f->n_tool_bar_rows;
11901 height = max (1, (it.last_visible_y - border) / rows);
11902 extra = it.last_visible_y - border - height * rows;
11903
11904 while (it.current_y < it.last_visible_y)
11905 {
11906 int h = 0;
11907 if (extra > 0 && rows-- > 0)
11908 {
11909 h = (extra + rows - 1) / rows;
11910 extra -= h;
11911 }
11912 display_tool_bar_line (&it, height + h);
11913 }
11914 }
11915 else
11916 {
11917 while (it.current_y < it.last_visible_y)
11918 display_tool_bar_line (&it, 0);
11919 }
11920
11921 /* It doesn't make much sense to try scrolling in the tool-bar
11922 window, so don't do it. */
11923 w->desired_matrix->no_scrolling_p = 1;
11924 w->must_be_updated_p = 1;
11925
11926 if (!NILP (Vauto_resize_tool_bars))
11927 {
11928 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11929 int change_height_p = 0;
11930
11931 /* If we couldn't display everything, change the tool-bar's
11932 height if there is room for more. */
11933 if (IT_STRING_CHARPOS (it) < it.end_charpos
11934 && it.current_y < max_tool_bar_height)
11935 change_height_p = 1;
11936
11937 row = it.glyph_row - 1;
11938
11939 /* If there are blank lines at the end, except for a partially
11940 visible blank line at the end that is smaller than
11941 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11942 if (!row->displays_text_p
11943 && row->height >= FRAME_LINE_HEIGHT (f))
11944 change_height_p = 1;
11945
11946 /* If row displays tool-bar items, but is partially visible,
11947 change the tool-bar's height. */
11948 if (row->displays_text_p
11949 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11950 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11951 change_height_p = 1;
11952
11953 /* Resize windows as needed by changing the `tool-bar-lines'
11954 frame parameter. */
11955 if (change_height_p)
11956 {
11957 Lisp_Object frame;
11958 int old_height = WINDOW_TOTAL_LINES (w);
11959 int nrows;
11960 int nlines = tool_bar_lines_needed (f, &nrows);
11961
11962 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11963 && !f->minimize_tool_bar_window_p)
11964 ? (nlines > old_height)
11965 : (nlines != old_height));
11966 f->minimize_tool_bar_window_p = 0;
11967
11968 if (change_height_p)
11969 {
11970 XSETFRAME (frame, f);
11971 Fmodify_frame_parameters (frame,
11972 Fcons (Fcons (Qtool_bar_lines,
11973 make_number (nlines)),
11974 Qnil));
11975 if (WINDOW_TOTAL_LINES (w) != old_height)
11976 {
11977 clear_glyph_matrix (w->desired_matrix);
11978 f->n_tool_bar_rows = nrows;
11979 fonts_changed_p = 1;
11980 return 1;
11981 }
11982 }
11983 }
11984 }
11985
11986 f->minimize_tool_bar_window_p = 0;
11987 return 0;
11988 }
11989
11990
11991 /* Get information about the tool-bar item which is displayed in GLYPH
11992 on frame F. Return in *PROP_IDX the index where tool-bar item
11993 properties start in F->tool_bar_items. Value is zero if
11994 GLYPH doesn't display a tool-bar item. */
11995
11996 static int
11997 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11998 {
11999 Lisp_Object prop;
12000 int success_p;
12001 int charpos;
12002
12003 /* This function can be called asynchronously, which means we must
12004 exclude any possibility that Fget_text_property signals an
12005 error. */
12006 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12007 charpos = max (0, charpos);
12008
12009 /* Get the text property `menu-item' at pos. The value of that
12010 property is the start index of this item's properties in
12011 F->tool_bar_items. */
12012 prop = Fget_text_property (make_number (charpos),
12013 Qmenu_item, f->current_tool_bar_string);
12014 if (INTEGERP (prop))
12015 {
12016 *prop_idx = XINT (prop);
12017 success_p = 1;
12018 }
12019 else
12020 success_p = 0;
12021
12022 return success_p;
12023 }
12024
12025 \f
12026 /* Get information about the tool-bar item at position X/Y on frame F.
12027 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12028 the current matrix of the tool-bar window of F, or NULL if not
12029 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12030 item in F->tool_bar_items. Value is
12031
12032 -1 if X/Y is not on a tool-bar item
12033 0 if X/Y is on the same item that was highlighted before.
12034 1 otherwise. */
12035
12036 static int
12037 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12038 int *hpos, int *vpos, int *prop_idx)
12039 {
12040 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12041 struct window *w = XWINDOW (f->tool_bar_window);
12042 int area;
12043
12044 /* Find the glyph under X/Y. */
12045 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12046 if (*glyph == NULL)
12047 return -1;
12048
12049 /* Get the start of this tool-bar item's properties in
12050 f->tool_bar_items. */
12051 if (!tool_bar_item_info (f, *glyph, prop_idx))
12052 return -1;
12053
12054 /* Is mouse on the highlighted item? */
12055 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12056 && *vpos >= hlinfo->mouse_face_beg_row
12057 && *vpos <= hlinfo->mouse_face_end_row
12058 && (*vpos > hlinfo->mouse_face_beg_row
12059 || *hpos >= hlinfo->mouse_face_beg_col)
12060 && (*vpos < hlinfo->mouse_face_end_row
12061 || *hpos < hlinfo->mouse_face_end_col
12062 || hlinfo->mouse_face_past_end))
12063 return 0;
12064
12065 return 1;
12066 }
12067
12068
12069 /* EXPORT:
12070 Handle mouse button event on the tool-bar of frame F, at
12071 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12072 0 for button release. MODIFIERS is event modifiers for button
12073 release. */
12074
12075 void
12076 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12077 int modifiers)
12078 {
12079 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12080 struct window *w = XWINDOW (f->tool_bar_window);
12081 int hpos, vpos, prop_idx;
12082 struct glyph *glyph;
12083 Lisp_Object enabled_p;
12084
12085 /* If not on the highlighted tool-bar item, return. */
12086 frame_to_window_pixel_xy (w, &x, &y);
12087 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12088 return;
12089
12090 /* If item is disabled, do nothing. */
12091 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12092 if (NILP (enabled_p))
12093 return;
12094
12095 if (down_p)
12096 {
12097 /* Show item in pressed state. */
12098 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12099 last_tool_bar_item = prop_idx;
12100 }
12101 else
12102 {
12103 Lisp_Object key, frame;
12104 struct input_event event;
12105 EVENT_INIT (event);
12106
12107 /* Show item in released state. */
12108 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12109
12110 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12111
12112 XSETFRAME (frame, f);
12113 event.kind = TOOL_BAR_EVENT;
12114 event.frame_or_window = frame;
12115 event.arg = frame;
12116 kbd_buffer_store_event (&event);
12117
12118 event.kind = TOOL_BAR_EVENT;
12119 event.frame_or_window = frame;
12120 event.arg = key;
12121 event.modifiers = modifiers;
12122 kbd_buffer_store_event (&event);
12123 last_tool_bar_item = -1;
12124 }
12125 }
12126
12127
12128 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12129 tool-bar window-relative coordinates X/Y. Called from
12130 note_mouse_highlight. */
12131
12132 static void
12133 note_tool_bar_highlight (struct frame *f, int x, int y)
12134 {
12135 Lisp_Object window = f->tool_bar_window;
12136 struct window *w = XWINDOW (window);
12137 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12138 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12139 int hpos, vpos;
12140 struct glyph *glyph;
12141 struct glyph_row *row;
12142 int i;
12143 Lisp_Object enabled_p;
12144 int prop_idx;
12145 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12146 int mouse_down_p, rc;
12147
12148 /* Function note_mouse_highlight is called with negative X/Y
12149 values when mouse moves outside of the frame. */
12150 if (x <= 0 || y <= 0)
12151 {
12152 clear_mouse_face (hlinfo);
12153 return;
12154 }
12155
12156 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12157 if (rc < 0)
12158 {
12159 /* Not on tool-bar item. */
12160 clear_mouse_face (hlinfo);
12161 return;
12162 }
12163 else if (rc == 0)
12164 /* On same tool-bar item as before. */
12165 goto set_help_echo;
12166
12167 clear_mouse_face (hlinfo);
12168
12169 /* Mouse is down, but on different tool-bar item? */
12170 mouse_down_p = (dpyinfo->grabbed
12171 && f == last_mouse_frame
12172 && FRAME_LIVE_P (f));
12173 if (mouse_down_p
12174 && last_tool_bar_item != prop_idx)
12175 return;
12176
12177 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12178
12179 /* If tool-bar item is not enabled, don't highlight it. */
12180 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12181 if (!NILP (enabled_p))
12182 {
12183 /* Compute the x-position of the glyph. In front and past the
12184 image is a space. We include this in the highlighted area. */
12185 row = MATRIX_ROW (w->current_matrix, vpos);
12186 for (i = x = 0; i < hpos; ++i)
12187 x += row->glyphs[TEXT_AREA][i].pixel_width;
12188
12189 /* Record this as the current active region. */
12190 hlinfo->mouse_face_beg_col = hpos;
12191 hlinfo->mouse_face_beg_row = vpos;
12192 hlinfo->mouse_face_beg_x = x;
12193 hlinfo->mouse_face_beg_y = row->y;
12194 hlinfo->mouse_face_past_end = 0;
12195
12196 hlinfo->mouse_face_end_col = hpos + 1;
12197 hlinfo->mouse_face_end_row = vpos;
12198 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12199 hlinfo->mouse_face_end_y = row->y;
12200 hlinfo->mouse_face_window = window;
12201 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12202
12203 /* Display it as active. */
12204 show_mouse_face (hlinfo, draw);
12205 }
12206
12207 set_help_echo:
12208
12209 /* Set help_echo_string to a help string to display for this tool-bar item.
12210 XTread_socket does the rest. */
12211 help_echo_object = help_echo_window = Qnil;
12212 help_echo_pos = -1;
12213 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12214 if (NILP (help_echo_string))
12215 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12216 }
12217
12218 #endif /* HAVE_WINDOW_SYSTEM */
12219
12220
12221 \f
12222 /************************************************************************
12223 Horizontal scrolling
12224 ************************************************************************/
12225
12226 static int hscroll_window_tree (Lisp_Object);
12227 static int hscroll_windows (Lisp_Object);
12228
12229 /* For all leaf windows in the window tree rooted at WINDOW, set their
12230 hscroll value so that PT is (i) visible in the window, and (ii) so
12231 that it is not within a certain margin at the window's left and
12232 right border. Value is non-zero if any window's hscroll has been
12233 changed. */
12234
12235 static int
12236 hscroll_window_tree (Lisp_Object window)
12237 {
12238 int hscrolled_p = 0;
12239 int hscroll_relative_p = FLOATP (Vhscroll_step);
12240 int hscroll_step_abs = 0;
12241 double hscroll_step_rel = 0;
12242
12243 if (hscroll_relative_p)
12244 {
12245 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12246 if (hscroll_step_rel < 0)
12247 {
12248 hscroll_relative_p = 0;
12249 hscroll_step_abs = 0;
12250 }
12251 }
12252 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12253 {
12254 hscroll_step_abs = XINT (Vhscroll_step);
12255 if (hscroll_step_abs < 0)
12256 hscroll_step_abs = 0;
12257 }
12258 else
12259 hscroll_step_abs = 0;
12260
12261 while (WINDOWP (window))
12262 {
12263 struct window *w = XWINDOW (window);
12264
12265 if (WINDOWP (w->hchild))
12266 hscrolled_p |= hscroll_window_tree (w->hchild);
12267 else if (WINDOWP (w->vchild))
12268 hscrolled_p |= hscroll_window_tree (w->vchild);
12269 else if (w->cursor.vpos >= 0)
12270 {
12271 int h_margin;
12272 int text_area_width;
12273 struct glyph_row *current_cursor_row
12274 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12275 struct glyph_row *desired_cursor_row
12276 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12277 struct glyph_row *cursor_row
12278 = (desired_cursor_row->enabled_p
12279 ? desired_cursor_row
12280 : current_cursor_row);
12281 int row_r2l_p = cursor_row->reversed_p;
12282
12283 text_area_width = window_box_width (w, TEXT_AREA);
12284
12285 /* Scroll when cursor is inside this scroll margin. */
12286 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12287
12288 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12289 /* For left-to-right rows, hscroll when cursor is either
12290 (i) inside the right hscroll margin, or (ii) if it is
12291 inside the left margin and the window is already
12292 hscrolled. */
12293 && ((!row_r2l_p
12294 && ((w->hscroll
12295 && w->cursor.x <= h_margin)
12296 || (cursor_row->enabled_p
12297 && cursor_row->truncated_on_right_p
12298 && (w->cursor.x >= text_area_width - h_margin))))
12299 /* For right-to-left rows, the logic is similar,
12300 except that rules for scrolling to left and right
12301 are reversed. E.g., if cursor.x <= h_margin, we
12302 need to hscroll "to the right" unconditionally,
12303 and that will scroll the screen to the left so as
12304 to reveal the next portion of the row. */
12305 || (row_r2l_p
12306 && ((cursor_row->enabled_p
12307 /* FIXME: It is confusing to set the
12308 truncated_on_right_p flag when R2L rows
12309 are actually truncated on the left. */
12310 && cursor_row->truncated_on_right_p
12311 && w->cursor.x <= h_margin)
12312 || (w->hscroll
12313 && (w->cursor.x >= text_area_width - h_margin))))))
12314 {
12315 struct it it;
12316 ptrdiff_t hscroll;
12317 struct buffer *saved_current_buffer;
12318 ptrdiff_t pt;
12319 int wanted_x;
12320
12321 /* Find point in a display of infinite width. */
12322 saved_current_buffer = current_buffer;
12323 current_buffer = XBUFFER (w->buffer);
12324
12325 if (w == XWINDOW (selected_window))
12326 pt = PT;
12327 else
12328 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12329
12330 /* Move iterator to pt starting at cursor_row->start in
12331 a line with infinite width. */
12332 init_to_row_start (&it, w, cursor_row);
12333 it.last_visible_x = INFINITY;
12334 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12335 current_buffer = saved_current_buffer;
12336
12337 /* Position cursor in window. */
12338 if (!hscroll_relative_p && hscroll_step_abs == 0)
12339 hscroll = max (0, (it.current_x
12340 - (ITERATOR_AT_END_OF_LINE_P (&it)
12341 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12342 : (text_area_width / 2))))
12343 / FRAME_COLUMN_WIDTH (it.f);
12344 else if ((!row_r2l_p
12345 && w->cursor.x >= text_area_width - h_margin)
12346 || (row_r2l_p && w->cursor.x <= h_margin))
12347 {
12348 if (hscroll_relative_p)
12349 wanted_x = text_area_width * (1 - hscroll_step_rel)
12350 - h_margin;
12351 else
12352 wanted_x = text_area_width
12353 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12354 - h_margin;
12355 hscroll
12356 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12357 }
12358 else
12359 {
12360 if (hscroll_relative_p)
12361 wanted_x = text_area_width * hscroll_step_rel
12362 + h_margin;
12363 else
12364 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12365 + h_margin;
12366 hscroll
12367 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12368 }
12369 hscroll = max (hscroll, w->min_hscroll);
12370
12371 /* Don't prevent redisplay optimizations if hscroll
12372 hasn't changed, as it will unnecessarily slow down
12373 redisplay. */
12374 if (w->hscroll != hscroll)
12375 {
12376 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12377 w->hscroll = hscroll;
12378 hscrolled_p = 1;
12379 }
12380 }
12381 }
12382
12383 window = w->next;
12384 }
12385
12386 /* Value is non-zero if hscroll of any leaf window has been changed. */
12387 return hscrolled_p;
12388 }
12389
12390
12391 /* Set hscroll so that cursor is visible and not inside horizontal
12392 scroll margins for all windows in the tree rooted at WINDOW. See
12393 also hscroll_window_tree above. Value is non-zero if any window's
12394 hscroll has been changed. If it has, desired matrices on the frame
12395 of WINDOW are cleared. */
12396
12397 static int
12398 hscroll_windows (Lisp_Object window)
12399 {
12400 int hscrolled_p = hscroll_window_tree (window);
12401 if (hscrolled_p)
12402 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12403 return hscrolled_p;
12404 }
12405
12406
12407 \f
12408 /************************************************************************
12409 Redisplay
12410 ************************************************************************/
12411
12412 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12413 to a non-zero value. This is sometimes handy to have in a debugger
12414 session. */
12415
12416 #ifdef GLYPH_DEBUG
12417
12418 /* First and last unchanged row for try_window_id. */
12419
12420 static int debug_first_unchanged_at_end_vpos;
12421 static int debug_last_unchanged_at_beg_vpos;
12422
12423 /* Delta vpos and y. */
12424
12425 static int debug_dvpos, debug_dy;
12426
12427 /* Delta in characters and bytes for try_window_id. */
12428
12429 static ptrdiff_t debug_delta, debug_delta_bytes;
12430
12431 /* Values of window_end_pos and window_end_vpos at the end of
12432 try_window_id. */
12433
12434 static ptrdiff_t debug_end_vpos;
12435
12436 /* Append a string to W->desired_matrix->method. FMT is a printf
12437 format string. If trace_redisplay_p is non-zero also printf the
12438 resulting string to stderr. */
12439
12440 static void debug_method_add (struct window *, char const *, ...)
12441 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12442
12443 static void
12444 debug_method_add (struct window *w, char const *fmt, ...)
12445 {
12446 char *method = w->desired_matrix->method;
12447 int len = strlen (method);
12448 int size = sizeof w->desired_matrix->method;
12449 int remaining = size - len - 1;
12450 va_list ap;
12451
12452 if (len && remaining)
12453 {
12454 method[len] = '|';
12455 --remaining, ++len;
12456 }
12457
12458 va_start (ap, fmt);
12459 vsnprintf (method + len, remaining + 1, fmt, ap);
12460 va_end (ap);
12461
12462 if (trace_redisplay_p)
12463 fprintf (stderr, "%p (%s): %s\n",
12464 w,
12465 ((BUFFERP (w->buffer)
12466 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12467 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12468 : "no buffer"),
12469 method + len);
12470 }
12471
12472 #endif /* GLYPH_DEBUG */
12473
12474
12475 /* Value is non-zero if all changes in window W, which displays
12476 current_buffer, are in the text between START and END. START is a
12477 buffer position, END is given as a distance from Z. Used in
12478 redisplay_internal for display optimization. */
12479
12480 static int
12481 text_outside_line_unchanged_p (struct window *w,
12482 ptrdiff_t start, ptrdiff_t end)
12483 {
12484 int unchanged_p = 1;
12485
12486 /* If text or overlays have changed, see where. */
12487 if (window_outdated (w))
12488 {
12489 /* Gap in the line? */
12490 if (GPT < start || Z - GPT < end)
12491 unchanged_p = 0;
12492
12493 /* Changes start in front of the line, or end after it? */
12494 if (unchanged_p
12495 && (BEG_UNCHANGED < start - 1
12496 || END_UNCHANGED < end))
12497 unchanged_p = 0;
12498
12499 /* If selective display, can't optimize if changes start at the
12500 beginning of the line. */
12501 if (unchanged_p
12502 && INTEGERP (BVAR (current_buffer, selective_display))
12503 && XINT (BVAR (current_buffer, selective_display)) > 0
12504 && (BEG_UNCHANGED < start || GPT <= start))
12505 unchanged_p = 0;
12506
12507 /* If there are overlays at the start or end of the line, these
12508 may have overlay strings with newlines in them. A change at
12509 START, for instance, may actually concern the display of such
12510 overlay strings as well, and they are displayed on different
12511 lines. So, quickly rule out this case. (For the future, it
12512 might be desirable to implement something more telling than
12513 just BEG/END_UNCHANGED.) */
12514 if (unchanged_p)
12515 {
12516 if (BEG + BEG_UNCHANGED == start
12517 && overlay_touches_p (start))
12518 unchanged_p = 0;
12519 if (END_UNCHANGED == end
12520 && overlay_touches_p (Z - end))
12521 unchanged_p = 0;
12522 }
12523
12524 /* Under bidi reordering, adding or deleting a character in the
12525 beginning of a paragraph, before the first strong directional
12526 character, can change the base direction of the paragraph (unless
12527 the buffer specifies a fixed paragraph direction), which will
12528 require to redisplay the whole paragraph. It might be worthwhile
12529 to find the paragraph limits and widen the range of redisplayed
12530 lines to that, but for now just give up this optimization. */
12531 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12532 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12533 unchanged_p = 0;
12534 }
12535
12536 return unchanged_p;
12537 }
12538
12539
12540 /* Do a frame update, taking possible shortcuts into account. This is
12541 the main external entry point for redisplay.
12542
12543 If the last redisplay displayed an echo area message and that message
12544 is no longer requested, we clear the echo area or bring back the
12545 mini-buffer if that is in use. */
12546
12547 void
12548 redisplay (void)
12549 {
12550 redisplay_internal ();
12551 }
12552
12553
12554 static Lisp_Object
12555 overlay_arrow_string_or_property (Lisp_Object var)
12556 {
12557 Lisp_Object val;
12558
12559 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12560 return val;
12561
12562 return Voverlay_arrow_string;
12563 }
12564
12565 /* Return 1 if there are any overlay-arrows in current_buffer. */
12566 static int
12567 overlay_arrow_in_current_buffer_p (void)
12568 {
12569 Lisp_Object vlist;
12570
12571 for (vlist = Voverlay_arrow_variable_list;
12572 CONSP (vlist);
12573 vlist = XCDR (vlist))
12574 {
12575 Lisp_Object var = XCAR (vlist);
12576 Lisp_Object val;
12577
12578 if (!SYMBOLP (var))
12579 continue;
12580 val = find_symbol_value (var);
12581 if (MARKERP (val)
12582 && current_buffer == XMARKER (val)->buffer)
12583 return 1;
12584 }
12585 return 0;
12586 }
12587
12588
12589 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12590 has changed. */
12591
12592 static int
12593 overlay_arrows_changed_p (void)
12594 {
12595 Lisp_Object vlist;
12596
12597 for (vlist = Voverlay_arrow_variable_list;
12598 CONSP (vlist);
12599 vlist = XCDR (vlist))
12600 {
12601 Lisp_Object var = XCAR (vlist);
12602 Lisp_Object val, pstr;
12603
12604 if (!SYMBOLP (var))
12605 continue;
12606 val = find_symbol_value (var);
12607 if (!MARKERP (val))
12608 continue;
12609 if (! EQ (COERCE_MARKER (val),
12610 Fget (var, Qlast_arrow_position))
12611 || ! (pstr = overlay_arrow_string_or_property (var),
12612 EQ (pstr, Fget (var, Qlast_arrow_string))))
12613 return 1;
12614 }
12615 return 0;
12616 }
12617
12618 /* Mark overlay arrows to be updated on next redisplay. */
12619
12620 static void
12621 update_overlay_arrows (int up_to_date)
12622 {
12623 Lisp_Object vlist;
12624
12625 for (vlist = Voverlay_arrow_variable_list;
12626 CONSP (vlist);
12627 vlist = XCDR (vlist))
12628 {
12629 Lisp_Object var = XCAR (vlist);
12630
12631 if (!SYMBOLP (var))
12632 continue;
12633
12634 if (up_to_date > 0)
12635 {
12636 Lisp_Object val = find_symbol_value (var);
12637 Fput (var, Qlast_arrow_position,
12638 COERCE_MARKER (val));
12639 Fput (var, Qlast_arrow_string,
12640 overlay_arrow_string_or_property (var));
12641 }
12642 else if (up_to_date < 0
12643 || !NILP (Fget (var, Qlast_arrow_position)))
12644 {
12645 Fput (var, Qlast_arrow_position, Qt);
12646 Fput (var, Qlast_arrow_string, Qt);
12647 }
12648 }
12649 }
12650
12651
12652 /* Return overlay arrow string to display at row.
12653 Return integer (bitmap number) for arrow bitmap in left fringe.
12654 Return nil if no overlay arrow. */
12655
12656 static Lisp_Object
12657 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12658 {
12659 Lisp_Object vlist;
12660
12661 for (vlist = Voverlay_arrow_variable_list;
12662 CONSP (vlist);
12663 vlist = XCDR (vlist))
12664 {
12665 Lisp_Object var = XCAR (vlist);
12666 Lisp_Object val;
12667
12668 if (!SYMBOLP (var))
12669 continue;
12670
12671 val = find_symbol_value (var);
12672
12673 if (MARKERP (val)
12674 && current_buffer == XMARKER (val)->buffer
12675 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12676 {
12677 if (FRAME_WINDOW_P (it->f)
12678 /* FIXME: if ROW->reversed_p is set, this should test
12679 the right fringe, not the left one. */
12680 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12681 {
12682 #ifdef HAVE_WINDOW_SYSTEM
12683 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12684 {
12685 int fringe_bitmap;
12686 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12687 return make_number (fringe_bitmap);
12688 }
12689 #endif
12690 return make_number (-1); /* Use default arrow bitmap. */
12691 }
12692 return overlay_arrow_string_or_property (var);
12693 }
12694 }
12695
12696 return Qnil;
12697 }
12698
12699 /* Return 1 if point moved out of or into a composition. Otherwise
12700 return 0. PREV_BUF and PREV_PT are the last point buffer and
12701 position. BUF and PT are the current point buffer and position. */
12702
12703 static int
12704 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12705 struct buffer *buf, ptrdiff_t pt)
12706 {
12707 ptrdiff_t start, end;
12708 Lisp_Object prop;
12709 Lisp_Object buffer;
12710
12711 XSETBUFFER (buffer, buf);
12712 /* Check a composition at the last point if point moved within the
12713 same buffer. */
12714 if (prev_buf == buf)
12715 {
12716 if (prev_pt == pt)
12717 /* Point didn't move. */
12718 return 0;
12719
12720 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12721 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12722 && COMPOSITION_VALID_P (start, end, prop)
12723 && start < prev_pt && end > prev_pt)
12724 /* The last point was within the composition. Return 1 iff
12725 point moved out of the composition. */
12726 return (pt <= start || pt >= end);
12727 }
12728
12729 /* Check a composition at the current point. */
12730 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12731 && find_composition (pt, -1, &start, &end, &prop, buffer)
12732 && COMPOSITION_VALID_P (start, end, prop)
12733 && start < pt && end > pt);
12734 }
12735
12736
12737 /* Reconsider the setting of B->clip_changed which is displayed
12738 in window W. */
12739
12740 static void
12741 reconsider_clip_changes (struct window *w, struct buffer *b)
12742 {
12743 if (b->clip_changed
12744 && w->window_end_valid
12745 && w->current_matrix->buffer == b
12746 && w->current_matrix->zv == BUF_ZV (b)
12747 && w->current_matrix->begv == BUF_BEGV (b))
12748 b->clip_changed = 0;
12749
12750 /* If display wasn't paused, and W is not a tool bar window, see if
12751 point has been moved into or out of a composition. In that case,
12752 we set b->clip_changed to 1 to force updating the screen. If
12753 b->clip_changed has already been set to 1, we can skip this
12754 check. */
12755 if (!b->clip_changed && BUFFERP (w->buffer) && w->window_end_valid)
12756 {
12757 ptrdiff_t pt;
12758
12759 if (w == XWINDOW (selected_window))
12760 pt = PT;
12761 else
12762 pt = marker_position (w->pointm);
12763
12764 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12765 || pt != w->last_point)
12766 && check_point_in_composition (w->current_matrix->buffer,
12767 w->last_point,
12768 XBUFFER (w->buffer), pt))
12769 b->clip_changed = 1;
12770 }
12771 }
12772 \f
12773
12774 #define STOP_POLLING \
12775 do { if (! polling_stopped_here) stop_polling (); \
12776 polling_stopped_here = 1; } while (0)
12777
12778 #define RESUME_POLLING \
12779 do { if (polling_stopped_here) start_polling (); \
12780 polling_stopped_here = 0; } while (0)
12781
12782
12783 /* Perhaps in the future avoid recentering windows if it
12784 is not necessary; currently that causes some problems. */
12785
12786 static void
12787 redisplay_internal (void)
12788 {
12789 struct window *w = XWINDOW (selected_window);
12790 struct window *sw;
12791 struct frame *fr;
12792 int pending;
12793 int must_finish = 0;
12794 struct text_pos tlbufpos, tlendpos;
12795 int number_of_visible_frames;
12796 ptrdiff_t count, count1;
12797 struct frame *sf;
12798 int polling_stopped_here = 0;
12799 Lisp_Object tail, frame;
12800 struct backtrace backtrace;
12801
12802 /* Non-zero means redisplay has to consider all windows on all
12803 frames. Zero means, only selected_window is considered. */
12804 int consider_all_windows_p;
12805
12806 /* Non-zero means redisplay has to redisplay the miniwindow. */
12807 int update_miniwindow_p = 0;
12808
12809 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12810
12811 /* No redisplay if running in batch mode or frame is not yet fully
12812 initialized, or redisplay is explicitly turned off by setting
12813 Vinhibit_redisplay. */
12814 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12815 || !NILP (Vinhibit_redisplay))
12816 return;
12817
12818 /* Don't examine these until after testing Vinhibit_redisplay.
12819 When Emacs is shutting down, perhaps because its connection to
12820 X has dropped, we should not look at them at all. */
12821 fr = XFRAME (w->frame);
12822 sf = SELECTED_FRAME ();
12823
12824 if (!fr->glyphs_initialized_p)
12825 return;
12826
12827 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12828 if (popup_activated ())
12829 return;
12830 #endif
12831
12832 /* I don't think this happens but let's be paranoid. */
12833 if (redisplaying_p)
12834 return;
12835
12836 /* Record a function that clears redisplaying_p
12837 when we leave this function. */
12838 count = SPECPDL_INDEX ();
12839 record_unwind_protect (unwind_redisplay, selected_frame);
12840 redisplaying_p = 1;
12841 specbind (Qinhibit_free_realized_faces, Qnil);
12842
12843 /* Record this function, so it appears on the profiler's backtraces. */
12844 backtrace.next = backtrace_list;
12845 backtrace.function = Qredisplay_internal;
12846 backtrace.args = &Qnil;
12847 backtrace.nargs = 0;
12848 backtrace.debug_on_exit = 0;
12849 backtrace_list = &backtrace;
12850
12851 FOR_EACH_FRAME (tail, frame)
12852 XFRAME (frame)->already_hscrolled_p = 0;
12853
12854 retry:
12855 /* Remember the currently selected window. */
12856 sw = w;
12857
12858 pending = 0;
12859 reconsider_clip_changes (w, current_buffer);
12860 last_escape_glyph_frame = NULL;
12861 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12862 last_glyphless_glyph_frame = NULL;
12863 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12864
12865 /* If new fonts have been loaded that make a glyph matrix adjustment
12866 necessary, do it. */
12867 if (fonts_changed_p)
12868 {
12869 adjust_glyphs (NULL);
12870 ++windows_or_buffers_changed;
12871 fonts_changed_p = 0;
12872 }
12873
12874 /* If face_change_count is non-zero, init_iterator will free all
12875 realized faces, which includes the faces referenced from current
12876 matrices. So, we can't reuse current matrices in this case. */
12877 if (face_change_count)
12878 ++windows_or_buffers_changed;
12879
12880 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12881 && FRAME_TTY (sf)->previous_frame != sf)
12882 {
12883 /* Since frames on a single ASCII terminal share the same
12884 display area, displaying a different frame means redisplay
12885 the whole thing. */
12886 windows_or_buffers_changed++;
12887 SET_FRAME_GARBAGED (sf);
12888 #ifndef DOS_NT
12889 set_tty_color_mode (FRAME_TTY (sf), sf);
12890 #endif
12891 FRAME_TTY (sf)->previous_frame = sf;
12892 }
12893
12894 /* Set the visible flags for all frames. Do this before checking for
12895 resized or garbaged frames; they want to know if their frames are
12896 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12897 number_of_visible_frames = 0;
12898
12899 FOR_EACH_FRAME (tail, frame)
12900 {
12901 struct frame *f = XFRAME (frame);
12902
12903 if (FRAME_VISIBLE_P (f))
12904 ++number_of_visible_frames;
12905 clear_desired_matrices (f);
12906 }
12907
12908 /* Notice any pending interrupt request to change frame size. */
12909 do_pending_window_change (1);
12910
12911 /* do_pending_window_change could change the selected_window due to
12912 frame resizing which makes the selected window too small. */
12913 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12914 {
12915 sw = w;
12916 reconsider_clip_changes (w, current_buffer);
12917 }
12918
12919 /* Clear frames marked as garbaged. */
12920 clear_garbaged_frames ();
12921
12922 /* Build menubar and tool-bar items. */
12923 if (NILP (Vmemory_full))
12924 prepare_menu_bars ();
12925
12926 if (windows_or_buffers_changed)
12927 update_mode_lines++;
12928
12929 /* Detect case that we need to write or remove a star in the mode line. */
12930 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12931 {
12932 w->update_mode_line = 1;
12933 if (buffer_shared_and_changed ())
12934 update_mode_lines++;
12935 }
12936
12937 /* Avoid invocation of point motion hooks by `current_column' below. */
12938 count1 = SPECPDL_INDEX ();
12939 specbind (Qinhibit_point_motion_hooks, Qt);
12940
12941 if (mode_line_update_needed (w))
12942 w->update_mode_line = 1;
12943
12944 unbind_to (count1, Qnil);
12945
12946 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12947
12948 consider_all_windows_p = (update_mode_lines
12949 || buffer_shared_and_changed ()
12950 || cursor_type_changed);
12951
12952 /* If specs for an arrow have changed, do thorough redisplay
12953 to ensure we remove any arrow that should no longer exist. */
12954 if (overlay_arrows_changed_p ())
12955 consider_all_windows_p = windows_or_buffers_changed = 1;
12956
12957 /* Normally the message* functions will have already displayed and
12958 updated the echo area, but the frame may have been trashed, or
12959 the update may have been preempted, so display the echo area
12960 again here. Checking message_cleared_p captures the case that
12961 the echo area should be cleared. */
12962 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12963 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12964 || (message_cleared_p
12965 && minibuf_level == 0
12966 /* If the mini-window is currently selected, this means the
12967 echo-area doesn't show through. */
12968 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12969 {
12970 int window_height_changed_p = echo_area_display (0);
12971
12972 if (message_cleared_p)
12973 update_miniwindow_p = 1;
12974
12975 must_finish = 1;
12976
12977 /* If we don't display the current message, don't clear the
12978 message_cleared_p flag, because, if we did, we wouldn't clear
12979 the echo area in the next redisplay which doesn't preserve
12980 the echo area. */
12981 if (!display_last_displayed_message_p)
12982 message_cleared_p = 0;
12983
12984 if (fonts_changed_p)
12985 goto retry;
12986 else if (window_height_changed_p)
12987 {
12988 consider_all_windows_p = 1;
12989 ++update_mode_lines;
12990 ++windows_or_buffers_changed;
12991
12992 /* If window configuration was changed, frames may have been
12993 marked garbaged. Clear them or we will experience
12994 surprises wrt scrolling. */
12995 clear_garbaged_frames ();
12996 }
12997 }
12998 else if (EQ (selected_window, minibuf_window)
12999 && (current_buffer->clip_changed || window_outdated (w))
13000 && resize_mini_window (w, 0))
13001 {
13002 /* Resized active mini-window to fit the size of what it is
13003 showing if its contents might have changed. */
13004 must_finish = 1;
13005 /* FIXME: this causes all frames to be updated, which seems unnecessary
13006 since only the current frame needs to be considered. This function
13007 needs to be rewritten with two variables, consider_all_windows and
13008 consider_all_frames. */
13009 consider_all_windows_p = 1;
13010 ++windows_or_buffers_changed;
13011 ++update_mode_lines;
13012
13013 /* If window configuration was changed, frames may have been
13014 marked garbaged. Clear them or we will experience
13015 surprises wrt scrolling. */
13016 clear_garbaged_frames ();
13017 }
13018
13019 /* If showing the region, and mark has changed, we must redisplay
13020 the whole window. The assignment to this_line_start_pos prevents
13021 the optimization directly below this if-statement. */
13022 if (((!NILP (Vtransient_mark_mode)
13023 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13024 != (w->region_showing > 0))
13025 || (w->region_showing
13026 && w->region_showing
13027 != XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13028 CHARPOS (this_line_start_pos) = 0;
13029
13030 /* Optimize the case that only the line containing the cursor in the
13031 selected window has changed. Variables starting with this_ are
13032 set in display_line and record information about the line
13033 containing the cursor. */
13034 tlbufpos = this_line_start_pos;
13035 tlendpos = this_line_end_pos;
13036 if (!consider_all_windows_p
13037 && CHARPOS (tlbufpos) > 0
13038 && !w->update_mode_line
13039 && !current_buffer->clip_changed
13040 && !current_buffer->prevent_redisplay_optimizations_p
13041 && FRAME_VISIBLE_P (XFRAME (w->frame))
13042 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13043 /* Make sure recorded data applies to current buffer, etc. */
13044 && this_line_buffer == current_buffer
13045 && current_buffer == XBUFFER (w->buffer)
13046 && !w->force_start
13047 && !w->optional_new_start
13048 /* Point must be on the line that we have info recorded about. */
13049 && PT >= CHARPOS (tlbufpos)
13050 && PT <= Z - CHARPOS (tlendpos)
13051 /* All text outside that line, including its final newline,
13052 must be unchanged. */
13053 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13054 CHARPOS (tlendpos)))
13055 {
13056 if (CHARPOS (tlbufpos) > BEGV
13057 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13058 && (CHARPOS (tlbufpos) == ZV
13059 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13060 /* Former continuation line has disappeared by becoming empty. */
13061 goto cancel;
13062 else if (window_outdated (w) || MINI_WINDOW_P (w))
13063 {
13064 /* We have to handle the case of continuation around a
13065 wide-column character (see the comment in indent.c around
13066 line 1340).
13067
13068 For instance, in the following case:
13069
13070 -------- Insert --------
13071 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13072 J_I_ ==> J_I_ `^^' are cursors.
13073 ^^ ^^
13074 -------- --------
13075
13076 As we have to redraw the line above, we cannot use this
13077 optimization. */
13078
13079 struct it it;
13080 int line_height_before = this_line_pixel_height;
13081
13082 /* Note that start_display will handle the case that the
13083 line starting at tlbufpos is a continuation line. */
13084 start_display (&it, w, tlbufpos);
13085
13086 /* Implementation note: It this still necessary? */
13087 if (it.current_x != this_line_start_x)
13088 goto cancel;
13089
13090 TRACE ((stderr, "trying display optimization 1\n"));
13091 w->cursor.vpos = -1;
13092 overlay_arrow_seen = 0;
13093 it.vpos = this_line_vpos;
13094 it.current_y = this_line_y;
13095 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13096 display_line (&it);
13097
13098 /* If line contains point, is not continued,
13099 and ends at same distance from eob as before, we win. */
13100 if (w->cursor.vpos >= 0
13101 /* Line is not continued, otherwise this_line_start_pos
13102 would have been set to 0 in display_line. */
13103 && CHARPOS (this_line_start_pos)
13104 /* Line ends as before. */
13105 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13106 /* Line has same height as before. Otherwise other lines
13107 would have to be shifted up or down. */
13108 && this_line_pixel_height == line_height_before)
13109 {
13110 /* If this is not the window's last line, we must adjust
13111 the charstarts of the lines below. */
13112 if (it.current_y < it.last_visible_y)
13113 {
13114 struct glyph_row *row
13115 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13116 ptrdiff_t delta, delta_bytes;
13117
13118 /* We used to distinguish between two cases here,
13119 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13120 when the line ends in a newline or the end of the
13121 buffer's accessible portion. But both cases did
13122 the same, so they were collapsed. */
13123 delta = (Z
13124 - CHARPOS (tlendpos)
13125 - MATRIX_ROW_START_CHARPOS (row));
13126 delta_bytes = (Z_BYTE
13127 - BYTEPOS (tlendpos)
13128 - MATRIX_ROW_START_BYTEPOS (row));
13129
13130 increment_matrix_positions (w->current_matrix,
13131 this_line_vpos + 1,
13132 w->current_matrix->nrows,
13133 delta, delta_bytes);
13134 }
13135
13136 /* If this row displays text now but previously didn't,
13137 or vice versa, w->window_end_vpos may have to be
13138 adjusted. */
13139 if ((it.glyph_row - 1)->displays_text_p)
13140 {
13141 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13142 wset_window_end_vpos (w, make_number (this_line_vpos));
13143 }
13144 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13145 && this_line_vpos > 0)
13146 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13147 w->window_end_valid = 0;
13148
13149 /* Update hint: No need to try to scroll in update_window. */
13150 w->desired_matrix->no_scrolling_p = 1;
13151
13152 #ifdef GLYPH_DEBUG
13153 *w->desired_matrix->method = 0;
13154 debug_method_add (w, "optimization 1");
13155 #endif
13156 #ifdef HAVE_WINDOW_SYSTEM
13157 update_window_fringes (w, 0);
13158 #endif
13159 goto update;
13160 }
13161 else
13162 goto cancel;
13163 }
13164 else if (/* Cursor position hasn't changed. */
13165 PT == w->last_point
13166 /* Make sure the cursor was last displayed
13167 in this window. Otherwise we have to reposition it. */
13168 && 0 <= w->cursor.vpos
13169 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13170 {
13171 if (!must_finish)
13172 {
13173 do_pending_window_change (1);
13174 /* If selected_window changed, redisplay again. */
13175 if (WINDOWP (selected_window)
13176 && (w = XWINDOW (selected_window)) != sw)
13177 goto retry;
13178
13179 /* We used to always goto end_of_redisplay here, but this
13180 isn't enough if we have a blinking cursor. */
13181 if (w->cursor_off_p == w->last_cursor_off_p)
13182 goto end_of_redisplay;
13183 }
13184 goto update;
13185 }
13186 /* If highlighting the region, or if the cursor is in the echo area,
13187 then we can't just move the cursor. */
13188 else if (! (!NILP (Vtransient_mark_mode)
13189 && !NILP (BVAR (current_buffer, mark_active)))
13190 && (EQ (selected_window,
13191 BVAR (current_buffer, last_selected_window))
13192 || highlight_nonselected_windows)
13193 && !w->region_showing
13194 && NILP (Vshow_trailing_whitespace)
13195 && !cursor_in_echo_area)
13196 {
13197 struct it it;
13198 struct glyph_row *row;
13199
13200 /* Skip from tlbufpos to PT and see where it is. Note that
13201 PT may be in invisible text. If so, we will end at the
13202 next visible position. */
13203 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13204 NULL, DEFAULT_FACE_ID);
13205 it.current_x = this_line_start_x;
13206 it.current_y = this_line_y;
13207 it.vpos = this_line_vpos;
13208
13209 /* The call to move_it_to stops in front of PT, but
13210 moves over before-strings. */
13211 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13212
13213 if (it.vpos == this_line_vpos
13214 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13215 row->enabled_p))
13216 {
13217 eassert (this_line_vpos == it.vpos);
13218 eassert (this_line_y == it.current_y);
13219 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13220 #ifdef GLYPH_DEBUG
13221 *w->desired_matrix->method = 0;
13222 debug_method_add (w, "optimization 3");
13223 #endif
13224 goto update;
13225 }
13226 else
13227 goto cancel;
13228 }
13229
13230 cancel:
13231 /* Text changed drastically or point moved off of line. */
13232 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13233 }
13234
13235 CHARPOS (this_line_start_pos) = 0;
13236 consider_all_windows_p |= buffer_shared_and_changed ();
13237 ++clear_face_cache_count;
13238 #ifdef HAVE_WINDOW_SYSTEM
13239 ++clear_image_cache_count;
13240 #endif
13241
13242 w->region_showing = XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)));
13243
13244 /* Build desired matrices, and update the display. If
13245 consider_all_windows_p is non-zero, do it for all windows on all
13246 frames. Otherwise do it for selected_window, only. */
13247
13248 if (consider_all_windows_p)
13249 {
13250 FOR_EACH_FRAME (tail, frame)
13251 XFRAME (frame)->updated_p = 0;
13252
13253 FOR_EACH_FRAME (tail, frame)
13254 {
13255 struct frame *f = XFRAME (frame);
13256
13257 /* We don't have to do anything for unselected terminal
13258 frames. */
13259 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13260 && !EQ (FRAME_TTY (f)->top_frame, frame))
13261 continue;
13262
13263 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13264 {
13265 /* Mark all the scroll bars to be removed; we'll redeem
13266 the ones we want when we redisplay their windows. */
13267 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13268 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13269
13270 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13271 redisplay_windows (FRAME_ROOT_WINDOW (f));
13272
13273 /* The X error handler may have deleted that frame. */
13274 if (!FRAME_LIVE_P (f))
13275 continue;
13276
13277 /* Any scroll bars which redisplay_windows should have
13278 nuked should now go away. */
13279 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13280 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13281
13282 /* If fonts changed, display again. */
13283 /* ??? rms: I suspect it is a mistake to jump all the way
13284 back to retry here. It should just retry this frame. */
13285 if (fonts_changed_p)
13286 goto retry;
13287
13288 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13289 {
13290 /* See if we have to hscroll. */
13291 if (!f->already_hscrolled_p)
13292 {
13293 f->already_hscrolled_p = 1;
13294 if (hscroll_windows (f->root_window))
13295 goto retry;
13296 }
13297
13298 /* Prevent various kinds of signals during display
13299 update. stdio is not robust about handling
13300 signals, which can cause an apparent I/O
13301 error. */
13302 if (interrupt_input)
13303 unrequest_sigio ();
13304 STOP_POLLING;
13305
13306 /* Update the display. */
13307 set_window_update_flags (XWINDOW (f->root_window), 1);
13308 pending |= update_frame (f, 0, 0);
13309 f->updated_p = 1;
13310 }
13311 }
13312 }
13313
13314 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13315
13316 if (!pending)
13317 {
13318 /* Do the mark_window_display_accurate after all windows have
13319 been redisplayed because this call resets flags in buffers
13320 which are needed for proper redisplay. */
13321 FOR_EACH_FRAME (tail, frame)
13322 {
13323 struct frame *f = XFRAME (frame);
13324 if (f->updated_p)
13325 {
13326 mark_window_display_accurate (f->root_window, 1);
13327 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13328 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13329 }
13330 }
13331 }
13332 }
13333 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13334 {
13335 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13336 struct frame *mini_frame;
13337
13338 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13339 /* Use list_of_error, not Qerror, so that
13340 we catch only errors and don't run the debugger. */
13341 internal_condition_case_1 (redisplay_window_1, selected_window,
13342 list_of_error,
13343 redisplay_window_error);
13344 if (update_miniwindow_p)
13345 internal_condition_case_1 (redisplay_window_1, mini_window,
13346 list_of_error,
13347 redisplay_window_error);
13348
13349 /* Compare desired and current matrices, perform output. */
13350
13351 update:
13352 /* If fonts changed, display again. */
13353 if (fonts_changed_p)
13354 goto retry;
13355
13356 /* Prevent various kinds of signals during display update.
13357 stdio is not robust about handling signals,
13358 which can cause an apparent I/O error. */
13359 if (interrupt_input)
13360 unrequest_sigio ();
13361 STOP_POLLING;
13362
13363 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13364 {
13365 if (hscroll_windows (selected_window))
13366 goto retry;
13367
13368 XWINDOW (selected_window)->must_be_updated_p = 1;
13369 pending = update_frame (sf, 0, 0);
13370 }
13371
13372 /* We may have called echo_area_display at the top of this
13373 function. If the echo area is on another frame, that may
13374 have put text on a frame other than the selected one, so the
13375 above call to update_frame would not have caught it. Catch
13376 it here. */
13377 mini_window = FRAME_MINIBUF_WINDOW (sf);
13378 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13379
13380 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13381 {
13382 XWINDOW (mini_window)->must_be_updated_p = 1;
13383 pending |= update_frame (mini_frame, 0, 0);
13384 if (!pending && hscroll_windows (mini_window))
13385 goto retry;
13386 }
13387 }
13388
13389 /* If display was paused because of pending input, make sure we do a
13390 thorough update the next time. */
13391 if (pending)
13392 {
13393 /* Prevent the optimization at the beginning of
13394 redisplay_internal that tries a single-line update of the
13395 line containing the cursor in the selected window. */
13396 CHARPOS (this_line_start_pos) = 0;
13397
13398 /* Let the overlay arrow be updated the next time. */
13399 update_overlay_arrows (0);
13400
13401 /* If we pause after scrolling, some rows in the current
13402 matrices of some windows are not valid. */
13403 if (!WINDOW_FULL_WIDTH_P (w)
13404 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13405 update_mode_lines = 1;
13406 }
13407 else
13408 {
13409 if (!consider_all_windows_p)
13410 {
13411 /* This has already been done above if
13412 consider_all_windows_p is set. */
13413 mark_window_display_accurate_1 (w, 1);
13414
13415 /* Say overlay arrows are up to date. */
13416 update_overlay_arrows (1);
13417
13418 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13419 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13420 }
13421
13422 update_mode_lines = 0;
13423 windows_or_buffers_changed = 0;
13424 cursor_type_changed = 0;
13425 }
13426
13427 /* Start SIGIO interrupts coming again. Having them off during the
13428 code above makes it less likely one will discard output, but not
13429 impossible, since there might be stuff in the system buffer here.
13430 But it is much hairier to try to do anything about that. */
13431 if (interrupt_input)
13432 request_sigio ();
13433 RESUME_POLLING;
13434
13435 /* If a frame has become visible which was not before, redisplay
13436 again, so that we display it. Expose events for such a frame
13437 (which it gets when becoming visible) don't call the parts of
13438 redisplay constructing glyphs, so simply exposing a frame won't
13439 display anything in this case. So, we have to display these
13440 frames here explicitly. */
13441 if (!pending)
13442 {
13443 int new_count = 0;
13444
13445 FOR_EACH_FRAME (tail, frame)
13446 {
13447 int this_is_visible = 0;
13448
13449 if (XFRAME (frame)->visible)
13450 this_is_visible = 1;
13451
13452 if (this_is_visible)
13453 new_count++;
13454 }
13455
13456 if (new_count != number_of_visible_frames)
13457 windows_or_buffers_changed++;
13458 }
13459
13460 /* Change frame size now if a change is pending. */
13461 do_pending_window_change (1);
13462
13463 /* If we just did a pending size change, or have additional
13464 visible frames, or selected_window changed, redisplay again. */
13465 if ((windows_or_buffers_changed && !pending)
13466 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13467 goto retry;
13468
13469 /* Clear the face and image caches.
13470
13471 We used to do this only if consider_all_windows_p. But the cache
13472 needs to be cleared if a timer creates images in the current
13473 buffer (e.g. the test case in Bug#6230). */
13474
13475 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13476 {
13477 clear_face_cache (0);
13478 clear_face_cache_count = 0;
13479 }
13480
13481 #ifdef HAVE_WINDOW_SYSTEM
13482 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13483 {
13484 clear_image_caches (Qnil);
13485 clear_image_cache_count = 0;
13486 }
13487 #endif /* HAVE_WINDOW_SYSTEM */
13488
13489 end_of_redisplay:
13490 backtrace_list = backtrace.next;
13491 unbind_to (count, Qnil);
13492 RESUME_POLLING;
13493 }
13494
13495
13496 /* Redisplay, but leave alone any recent echo area message unless
13497 another message has been requested in its place.
13498
13499 This is useful in situations where you need to redisplay but no
13500 user action has occurred, making it inappropriate for the message
13501 area to be cleared. See tracking_off and
13502 wait_reading_process_output for examples of these situations.
13503
13504 FROM_WHERE is an integer saying from where this function was
13505 called. This is useful for debugging. */
13506
13507 void
13508 redisplay_preserve_echo_area (int from_where)
13509 {
13510 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13511
13512 if (!NILP (echo_area_buffer[1]))
13513 {
13514 /* We have a previously displayed message, but no current
13515 message. Redisplay the previous message. */
13516 display_last_displayed_message_p = 1;
13517 redisplay_internal ();
13518 display_last_displayed_message_p = 0;
13519 }
13520 else
13521 redisplay_internal ();
13522
13523 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13524 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13525 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13526 }
13527
13528
13529 /* Function registered with record_unwind_protect in redisplay_internal.
13530 Clear redisplaying_p. Also select the previously selected frame. */
13531
13532 static Lisp_Object
13533 unwind_redisplay (Lisp_Object old_frame)
13534 {
13535 redisplaying_p = 0;
13536 return Qnil;
13537 }
13538
13539
13540 /* Mark the display of leaf window W as accurate or inaccurate.
13541 If ACCURATE_P is non-zero mark display of W as accurate. If
13542 ACCURATE_P is zero, arrange for W to be redisplayed the next
13543 time redisplay_internal is called. */
13544
13545 static void
13546 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13547 {
13548 struct buffer *b = XBUFFER (w->buffer);
13549
13550 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13551 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13552 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13553
13554 if (accurate_p)
13555 {
13556 b->clip_changed = 0;
13557 b->prevent_redisplay_optimizations_p = 0;
13558
13559 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13560 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13561 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13562 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13563
13564 w->current_matrix->buffer = b;
13565 w->current_matrix->begv = BUF_BEGV (b);
13566 w->current_matrix->zv = BUF_ZV (b);
13567
13568 w->last_cursor = w->cursor;
13569 w->last_cursor_off_p = w->cursor_off_p;
13570
13571 if (w == XWINDOW (selected_window))
13572 w->last_point = BUF_PT (b);
13573 else
13574 w->last_point = marker_position (w->pointm);
13575
13576 w->window_end_valid = 1;
13577 w->update_mode_line = 0;
13578 }
13579 }
13580
13581
13582 /* Mark the display of windows in the window tree rooted at WINDOW as
13583 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13584 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13585 be redisplayed the next time redisplay_internal is called. */
13586
13587 void
13588 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13589 {
13590 struct window *w;
13591
13592 for (; !NILP (window); window = w->next)
13593 {
13594 w = XWINDOW (window);
13595 if (!NILP (w->vchild))
13596 mark_window_display_accurate (w->vchild, accurate_p);
13597 else if (!NILP (w->hchild))
13598 mark_window_display_accurate (w->hchild, accurate_p);
13599 else if (BUFFERP (w->buffer))
13600 mark_window_display_accurate_1 (w, accurate_p);
13601 }
13602
13603 if (accurate_p)
13604 update_overlay_arrows (1);
13605 else
13606 /* Force a thorough redisplay the next time by setting
13607 last_arrow_position and last_arrow_string to t, which is
13608 unequal to any useful value of Voverlay_arrow_... */
13609 update_overlay_arrows (-1);
13610 }
13611
13612
13613 /* Return value in display table DP (Lisp_Char_Table *) for character
13614 C. Since a display table doesn't have any parent, we don't have to
13615 follow parent. Do not call this function directly but use the
13616 macro DISP_CHAR_VECTOR. */
13617
13618 Lisp_Object
13619 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13620 {
13621 Lisp_Object val;
13622
13623 if (ASCII_CHAR_P (c))
13624 {
13625 val = dp->ascii;
13626 if (SUB_CHAR_TABLE_P (val))
13627 val = XSUB_CHAR_TABLE (val)->contents[c];
13628 }
13629 else
13630 {
13631 Lisp_Object table;
13632
13633 XSETCHAR_TABLE (table, dp);
13634 val = char_table_ref (table, c);
13635 }
13636 if (NILP (val))
13637 val = dp->defalt;
13638 return val;
13639 }
13640
13641
13642 \f
13643 /***********************************************************************
13644 Window Redisplay
13645 ***********************************************************************/
13646
13647 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13648
13649 static void
13650 redisplay_windows (Lisp_Object window)
13651 {
13652 while (!NILP (window))
13653 {
13654 struct window *w = XWINDOW (window);
13655
13656 if (!NILP (w->hchild))
13657 redisplay_windows (w->hchild);
13658 else if (!NILP (w->vchild))
13659 redisplay_windows (w->vchild);
13660 else if (!NILP (w->buffer))
13661 {
13662 displayed_buffer = XBUFFER (w->buffer);
13663 /* Use list_of_error, not Qerror, so that
13664 we catch only errors and don't run the debugger. */
13665 internal_condition_case_1 (redisplay_window_0, window,
13666 list_of_error,
13667 redisplay_window_error);
13668 }
13669
13670 window = w->next;
13671 }
13672 }
13673
13674 static Lisp_Object
13675 redisplay_window_error (Lisp_Object ignore)
13676 {
13677 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13678 return Qnil;
13679 }
13680
13681 static Lisp_Object
13682 redisplay_window_0 (Lisp_Object window)
13683 {
13684 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13685 redisplay_window (window, 0);
13686 return Qnil;
13687 }
13688
13689 static Lisp_Object
13690 redisplay_window_1 (Lisp_Object window)
13691 {
13692 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13693 redisplay_window (window, 1);
13694 return Qnil;
13695 }
13696 \f
13697
13698 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13699 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13700 which positions recorded in ROW differ from current buffer
13701 positions.
13702
13703 Return 0 if cursor is not on this row, 1 otherwise. */
13704
13705 static int
13706 set_cursor_from_row (struct window *w, struct glyph_row *row,
13707 struct glyph_matrix *matrix,
13708 ptrdiff_t delta, ptrdiff_t delta_bytes,
13709 int dy, int dvpos)
13710 {
13711 struct glyph *glyph = row->glyphs[TEXT_AREA];
13712 struct glyph *end = glyph + row->used[TEXT_AREA];
13713 struct glyph *cursor = NULL;
13714 /* The last known character position in row. */
13715 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13716 int x = row->x;
13717 ptrdiff_t pt_old = PT - delta;
13718 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13719 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13720 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13721 /* A glyph beyond the edge of TEXT_AREA which we should never
13722 touch. */
13723 struct glyph *glyphs_end = end;
13724 /* Non-zero means we've found a match for cursor position, but that
13725 glyph has the avoid_cursor_p flag set. */
13726 int match_with_avoid_cursor = 0;
13727 /* Non-zero means we've seen at least one glyph that came from a
13728 display string. */
13729 int string_seen = 0;
13730 /* Largest and smallest buffer positions seen so far during scan of
13731 glyph row. */
13732 ptrdiff_t bpos_max = pos_before;
13733 ptrdiff_t bpos_min = pos_after;
13734 /* Last buffer position covered by an overlay string with an integer
13735 `cursor' property. */
13736 ptrdiff_t bpos_covered = 0;
13737 /* Non-zero means the display string on which to display the cursor
13738 comes from a text property, not from an overlay. */
13739 int string_from_text_prop = 0;
13740
13741 /* Don't even try doing anything if called for a mode-line or
13742 header-line row, since the rest of the code isn't prepared to
13743 deal with such calamities. */
13744 eassert (!row->mode_line_p);
13745 if (row->mode_line_p)
13746 return 0;
13747
13748 /* Skip over glyphs not having an object at the start and the end of
13749 the row. These are special glyphs like truncation marks on
13750 terminal frames. */
13751 if (row->displays_text_p)
13752 {
13753 if (!row->reversed_p)
13754 {
13755 while (glyph < end
13756 && INTEGERP (glyph->object)
13757 && glyph->charpos < 0)
13758 {
13759 x += glyph->pixel_width;
13760 ++glyph;
13761 }
13762 while (end > glyph
13763 && INTEGERP ((end - 1)->object)
13764 /* CHARPOS is zero for blanks and stretch glyphs
13765 inserted by extend_face_to_end_of_line. */
13766 && (end - 1)->charpos <= 0)
13767 --end;
13768 glyph_before = glyph - 1;
13769 glyph_after = end;
13770 }
13771 else
13772 {
13773 struct glyph *g;
13774
13775 /* If the glyph row is reversed, we need to process it from back
13776 to front, so swap the edge pointers. */
13777 glyphs_end = end = glyph - 1;
13778 glyph += row->used[TEXT_AREA] - 1;
13779
13780 while (glyph > end + 1
13781 && INTEGERP (glyph->object)
13782 && glyph->charpos < 0)
13783 {
13784 --glyph;
13785 x -= glyph->pixel_width;
13786 }
13787 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13788 --glyph;
13789 /* By default, in reversed rows we put the cursor on the
13790 rightmost (first in the reading order) glyph. */
13791 for (g = end + 1; g < glyph; g++)
13792 x += g->pixel_width;
13793 while (end < glyph
13794 && INTEGERP ((end + 1)->object)
13795 && (end + 1)->charpos <= 0)
13796 ++end;
13797 glyph_before = glyph + 1;
13798 glyph_after = end;
13799 }
13800 }
13801 else if (row->reversed_p)
13802 {
13803 /* In R2L rows that don't display text, put the cursor on the
13804 rightmost glyph. Case in point: an empty last line that is
13805 part of an R2L paragraph. */
13806 cursor = end - 1;
13807 /* Avoid placing the cursor on the last glyph of the row, where
13808 on terminal frames we hold the vertical border between
13809 adjacent windows. */
13810 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13811 && !WINDOW_RIGHTMOST_P (w)
13812 && cursor == row->glyphs[LAST_AREA] - 1)
13813 cursor--;
13814 x = -1; /* will be computed below, at label compute_x */
13815 }
13816
13817 /* Step 1: Try to find the glyph whose character position
13818 corresponds to point. If that's not possible, find 2 glyphs
13819 whose character positions are the closest to point, one before
13820 point, the other after it. */
13821 if (!row->reversed_p)
13822 while (/* not marched to end of glyph row */
13823 glyph < end
13824 /* glyph was not inserted by redisplay for internal purposes */
13825 && !INTEGERP (glyph->object))
13826 {
13827 if (BUFFERP (glyph->object))
13828 {
13829 ptrdiff_t dpos = glyph->charpos - pt_old;
13830
13831 if (glyph->charpos > bpos_max)
13832 bpos_max = glyph->charpos;
13833 if (glyph->charpos < bpos_min)
13834 bpos_min = glyph->charpos;
13835 if (!glyph->avoid_cursor_p)
13836 {
13837 /* If we hit point, we've found the glyph on which to
13838 display the cursor. */
13839 if (dpos == 0)
13840 {
13841 match_with_avoid_cursor = 0;
13842 break;
13843 }
13844 /* See if we've found a better approximation to
13845 POS_BEFORE or to POS_AFTER. */
13846 if (0 > dpos && dpos > pos_before - pt_old)
13847 {
13848 pos_before = glyph->charpos;
13849 glyph_before = glyph;
13850 }
13851 else if (0 < dpos && dpos < pos_after - pt_old)
13852 {
13853 pos_after = glyph->charpos;
13854 glyph_after = glyph;
13855 }
13856 }
13857 else if (dpos == 0)
13858 match_with_avoid_cursor = 1;
13859 }
13860 else if (STRINGP (glyph->object))
13861 {
13862 Lisp_Object chprop;
13863 ptrdiff_t glyph_pos = glyph->charpos;
13864
13865 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13866 glyph->object);
13867 if (!NILP (chprop))
13868 {
13869 /* If the string came from a `display' text property,
13870 look up the buffer position of that property and
13871 use that position to update bpos_max, as if we
13872 actually saw such a position in one of the row's
13873 glyphs. This helps with supporting integer values
13874 of `cursor' property on the display string in
13875 situations where most or all of the row's buffer
13876 text is completely covered by display properties,
13877 so that no glyph with valid buffer positions is
13878 ever seen in the row. */
13879 ptrdiff_t prop_pos =
13880 string_buffer_position_lim (glyph->object, pos_before,
13881 pos_after, 0);
13882
13883 if (prop_pos >= pos_before)
13884 bpos_max = prop_pos - 1;
13885 }
13886 if (INTEGERP (chprop))
13887 {
13888 bpos_covered = bpos_max + XINT (chprop);
13889 /* If the `cursor' property covers buffer positions up
13890 to and including point, we should display cursor on
13891 this glyph. Note that, if a `cursor' property on one
13892 of the string's characters has an integer value, we
13893 will break out of the loop below _before_ we get to
13894 the position match above. IOW, integer values of
13895 the `cursor' property override the "exact match for
13896 point" strategy of positioning the cursor. */
13897 /* Implementation note: bpos_max == pt_old when, e.g.,
13898 we are in an empty line, where bpos_max is set to
13899 MATRIX_ROW_START_CHARPOS, see above. */
13900 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13901 {
13902 cursor = glyph;
13903 break;
13904 }
13905 }
13906
13907 string_seen = 1;
13908 }
13909 x += glyph->pixel_width;
13910 ++glyph;
13911 }
13912 else if (glyph > end) /* row is reversed */
13913 while (!INTEGERP (glyph->object))
13914 {
13915 if (BUFFERP (glyph->object))
13916 {
13917 ptrdiff_t dpos = glyph->charpos - pt_old;
13918
13919 if (glyph->charpos > bpos_max)
13920 bpos_max = glyph->charpos;
13921 if (glyph->charpos < bpos_min)
13922 bpos_min = glyph->charpos;
13923 if (!glyph->avoid_cursor_p)
13924 {
13925 if (dpos == 0)
13926 {
13927 match_with_avoid_cursor = 0;
13928 break;
13929 }
13930 if (0 > dpos && dpos > pos_before - pt_old)
13931 {
13932 pos_before = glyph->charpos;
13933 glyph_before = glyph;
13934 }
13935 else if (0 < dpos && dpos < pos_after - pt_old)
13936 {
13937 pos_after = glyph->charpos;
13938 glyph_after = glyph;
13939 }
13940 }
13941 else if (dpos == 0)
13942 match_with_avoid_cursor = 1;
13943 }
13944 else if (STRINGP (glyph->object))
13945 {
13946 Lisp_Object chprop;
13947 ptrdiff_t glyph_pos = glyph->charpos;
13948
13949 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13950 glyph->object);
13951 if (!NILP (chprop))
13952 {
13953 ptrdiff_t prop_pos =
13954 string_buffer_position_lim (glyph->object, pos_before,
13955 pos_after, 0);
13956
13957 if (prop_pos >= pos_before)
13958 bpos_max = prop_pos - 1;
13959 }
13960 if (INTEGERP (chprop))
13961 {
13962 bpos_covered = bpos_max + XINT (chprop);
13963 /* If the `cursor' property covers buffer positions up
13964 to and including point, we should display cursor on
13965 this glyph. */
13966 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13967 {
13968 cursor = glyph;
13969 break;
13970 }
13971 }
13972 string_seen = 1;
13973 }
13974 --glyph;
13975 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13976 {
13977 x--; /* can't use any pixel_width */
13978 break;
13979 }
13980 x -= glyph->pixel_width;
13981 }
13982
13983 /* Step 2: If we didn't find an exact match for point, we need to
13984 look for a proper place to put the cursor among glyphs between
13985 GLYPH_BEFORE and GLYPH_AFTER. */
13986 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13987 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13988 && !(bpos_max < pt_old && pt_old <= bpos_covered))
13989 {
13990 /* An empty line has a single glyph whose OBJECT is zero and
13991 whose CHARPOS is the position of a newline on that line.
13992 Note that on a TTY, there are more glyphs after that, which
13993 were produced by extend_face_to_end_of_line, but their
13994 CHARPOS is zero or negative. */
13995 int empty_line_p =
13996 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13997 && INTEGERP (glyph->object) && glyph->charpos > 0
13998 /* On a TTY, continued and truncated rows also have a glyph at
13999 their end whose OBJECT is zero and whose CHARPOS is
14000 positive (the continuation and truncation glyphs), but such
14001 rows are obviously not "empty". */
14002 && !(row->continued_p || row->truncated_on_right_p);
14003
14004 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14005 {
14006 ptrdiff_t ellipsis_pos;
14007
14008 /* Scan back over the ellipsis glyphs. */
14009 if (!row->reversed_p)
14010 {
14011 ellipsis_pos = (glyph - 1)->charpos;
14012 while (glyph > row->glyphs[TEXT_AREA]
14013 && (glyph - 1)->charpos == ellipsis_pos)
14014 glyph--, x -= glyph->pixel_width;
14015 /* That loop always goes one position too far, including
14016 the glyph before the ellipsis. So scan forward over
14017 that one. */
14018 x += glyph->pixel_width;
14019 glyph++;
14020 }
14021 else /* row is reversed */
14022 {
14023 ellipsis_pos = (glyph + 1)->charpos;
14024 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14025 && (glyph + 1)->charpos == ellipsis_pos)
14026 glyph++, x += glyph->pixel_width;
14027 x -= glyph->pixel_width;
14028 glyph--;
14029 }
14030 }
14031 else if (match_with_avoid_cursor)
14032 {
14033 cursor = glyph_after;
14034 x = -1;
14035 }
14036 else if (string_seen)
14037 {
14038 int incr = row->reversed_p ? -1 : +1;
14039
14040 /* Need to find the glyph that came out of a string which is
14041 present at point. That glyph is somewhere between
14042 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14043 positioned between POS_BEFORE and POS_AFTER in the
14044 buffer. */
14045 struct glyph *start, *stop;
14046 ptrdiff_t pos = pos_before;
14047
14048 x = -1;
14049
14050 /* If the row ends in a newline from a display string,
14051 reordering could have moved the glyphs belonging to the
14052 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14053 in this case we extend the search to the last glyph in
14054 the row that was not inserted by redisplay. */
14055 if (row->ends_in_newline_from_string_p)
14056 {
14057 glyph_after = end;
14058 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14059 }
14060
14061 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14062 correspond to POS_BEFORE and POS_AFTER, respectively. We
14063 need START and STOP in the order that corresponds to the
14064 row's direction as given by its reversed_p flag. If the
14065 directionality of characters between POS_BEFORE and
14066 POS_AFTER is the opposite of the row's base direction,
14067 these characters will have been reordered for display,
14068 and we need to reverse START and STOP. */
14069 if (!row->reversed_p)
14070 {
14071 start = min (glyph_before, glyph_after);
14072 stop = max (glyph_before, glyph_after);
14073 }
14074 else
14075 {
14076 start = max (glyph_before, glyph_after);
14077 stop = min (glyph_before, glyph_after);
14078 }
14079 for (glyph = start + incr;
14080 row->reversed_p ? glyph > stop : glyph < stop; )
14081 {
14082
14083 /* Any glyphs that come from the buffer are here because
14084 of bidi reordering. Skip them, and only pay
14085 attention to glyphs that came from some string. */
14086 if (STRINGP (glyph->object))
14087 {
14088 Lisp_Object str;
14089 ptrdiff_t tem;
14090 /* If the display property covers the newline, we
14091 need to search for it one position farther. */
14092 ptrdiff_t lim = pos_after
14093 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14094
14095 string_from_text_prop = 0;
14096 str = glyph->object;
14097 tem = string_buffer_position_lim (str, pos, lim, 0);
14098 if (tem == 0 /* from overlay */
14099 || pos <= tem)
14100 {
14101 /* If the string from which this glyph came is
14102 found in the buffer at point, or at position
14103 that is closer to point than pos_after, then
14104 we've found the glyph we've been looking for.
14105 If it comes from an overlay (tem == 0), and
14106 it has the `cursor' property on one of its
14107 glyphs, record that glyph as a candidate for
14108 displaying the cursor. (As in the
14109 unidirectional version, we will display the
14110 cursor on the last candidate we find.) */
14111 if (tem == 0
14112 || tem == pt_old
14113 || (tem - pt_old > 0 && tem < pos_after))
14114 {
14115 /* The glyphs from this string could have
14116 been reordered. Find the one with the
14117 smallest string position. Or there could
14118 be a character in the string with the
14119 `cursor' property, which means display
14120 cursor on that character's glyph. */
14121 ptrdiff_t strpos = glyph->charpos;
14122
14123 if (tem)
14124 {
14125 cursor = glyph;
14126 string_from_text_prop = 1;
14127 }
14128 for ( ;
14129 (row->reversed_p ? glyph > stop : glyph < stop)
14130 && EQ (glyph->object, str);
14131 glyph += incr)
14132 {
14133 Lisp_Object cprop;
14134 ptrdiff_t gpos = glyph->charpos;
14135
14136 cprop = Fget_char_property (make_number (gpos),
14137 Qcursor,
14138 glyph->object);
14139 if (!NILP (cprop))
14140 {
14141 cursor = glyph;
14142 break;
14143 }
14144 if (tem && glyph->charpos < strpos)
14145 {
14146 strpos = glyph->charpos;
14147 cursor = glyph;
14148 }
14149 }
14150
14151 if (tem == pt_old
14152 || (tem - pt_old > 0 && tem < pos_after))
14153 goto compute_x;
14154 }
14155 if (tem)
14156 pos = tem + 1; /* don't find previous instances */
14157 }
14158 /* This string is not what we want; skip all of the
14159 glyphs that came from it. */
14160 while ((row->reversed_p ? glyph > stop : glyph < stop)
14161 && EQ (glyph->object, str))
14162 glyph += incr;
14163 }
14164 else
14165 glyph += incr;
14166 }
14167
14168 /* If we reached the end of the line, and END was from a string,
14169 the cursor is not on this line. */
14170 if (cursor == NULL
14171 && (row->reversed_p ? glyph <= end : glyph >= end)
14172 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14173 && STRINGP (end->object)
14174 && row->continued_p)
14175 return 0;
14176 }
14177 /* A truncated row may not include PT among its character positions.
14178 Setting the cursor inside the scroll margin will trigger
14179 recalculation of hscroll in hscroll_window_tree. But if a
14180 display string covers point, defer to the string-handling
14181 code below to figure this out. */
14182 else if (row->truncated_on_left_p && pt_old < bpos_min)
14183 {
14184 cursor = glyph_before;
14185 x = -1;
14186 }
14187 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14188 /* Zero-width characters produce no glyphs. */
14189 || (!empty_line_p
14190 && (row->reversed_p
14191 ? glyph_after > glyphs_end
14192 : glyph_after < glyphs_end)))
14193 {
14194 cursor = glyph_after;
14195 x = -1;
14196 }
14197 }
14198
14199 compute_x:
14200 if (cursor != NULL)
14201 glyph = cursor;
14202 else if (glyph == glyphs_end
14203 && pos_before == pos_after
14204 && STRINGP ((row->reversed_p
14205 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14206 : row->glyphs[TEXT_AREA])->object))
14207 {
14208 /* If all the glyphs of this row came from strings, put the
14209 cursor on the first glyph of the row. This avoids having the
14210 cursor outside of the text area in this very rare and hard
14211 use case. */
14212 glyph =
14213 row->reversed_p
14214 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14215 : row->glyphs[TEXT_AREA];
14216 }
14217 if (x < 0)
14218 {
14219 struct glyph *g;
14220
14221 /* Need to compute x that corresponds to GLYPH. */
14222 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14223 {
14224 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14225 emacs_abort ();
14226 x += g->pixel_width;
14227 }
14228 }
14229
14230 /* ROW could be part of a continued line, which, under bidi
14231 reordering, might have other rows whose start and end charpos
14232 occlude point. Only set w->cursor if we found a better
14233 approximation to the cursor position than we have from previously
14234 examined candidate rows belonging to the same continued line. */
14235 if (/* we already have a candidate row */
14236 w->cursor.vpos >= 0
14237 /* that candidate is not the row we are processing */
14238 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14239 /* Make sure cursor.vpos specifies a row whose start and end
14240 charpos occlude point, and it is valid candidate for being a
14241 cursor-row. This is because some callers of this function
14242 leave cursor.vpos at the row where the cursor was displayed
14243 during the last redisplay cycle. */
14244 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14245 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14246 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14247 {
14248 struct glyph *g1 =
14249 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14250
14251 /* Don't consider glyphs that are outside TEXT_AREA. */
14252 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14253 return 0;
14254 /* Keep the candidate whose buffer position is the closest to
14255 point or has the `cursor' property. */
14256 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14257 w->cursor.hpos >= 0
14258 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14259 && ((BUFFERP (g1->object)
14260 && (g1->charpos == pt_old /* an exact match always wins */
14261 || (BUFFERP (glyph->object)
14262 && eabs (g1->charpos - pt_old)
14263 < eabs (glyph->charpos - pt_old))))
14264 /* previous candidate is a glyph from a string that has
14265 a non-nil `cursor' property */
14266 || (STRINGP (g1->object)
14267 && (!NILP (Fget_char_property (make_number (g1->charpos),
14268 Qcursor, g1->object))
14269 /* previous candidate is from the same display
14270 string as this one, and the display string
14271 came from a text property */
14272 || (EQ (g1->object, glyph->object)
14273 && string_from_text_prop)
14274 /* this candidate is from newline and its
14275 position is not an exact match */
14276 || (INTEGERP (glyph->object)
14277 && glyph->charpos != pt_old)))))
14278 return 0;
14279 /* If this candidate gives an exact match, use that. */
14280 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14281 /* If this candidate is a glyph created for the
14282 terminating newline of a line, and point is on that
14283 newline, it wins because it's an exact match. */
14284 || (!row->continued_p
14285 && INTEGERP (glyph->object)
14286 && glyph->charpos == 0
14287 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14288 /* Otherwise, keep the candidate that comes from a row
14289 spanning less buffer positions. This may win when one or
14290 both candidate positions are on glyphs that came from
14291 display strings, for which we cannot compare buffer
14292 positions. */
14293 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14294 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14295 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14296 return 0;
14297 }
14298 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14299 w->cursor.x = x;
14300 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14301 w->cursor.y = row->y + dy;
14302
14303 if (w == XWINDOW (selected_window))
14304 {
14305 if (!row->continued_p
14306 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14307 && row->x == 0)
14308 {
14309 this_line_buffer = XBUFFER (w->buffer);
14310
14311 CHARPOS (this_line_start_pos)
14312 = MATRIX_ROW_START_CHARPOS (row) + delta;
14313 BYTEPOS (this_line_start_pos)
14314 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14315
14316 CHARPOS (this_line_end_pos)
14317 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14318 BYTEPOS (this_line_end_pos)
14319 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14320
14321 this_line_y = w->cursor.y;
14322 this_line_pixel_height = row->height;
14323 this_line_vpos = w->cursor.vpos;
14324 this_line_start_x = row->x;
14325 }
14326 else
14327 CHARPOS (this_line_start_pos) = 0;
14328 }
14329
14330 return 1;
14331 }
14332
14333
14334 /* Run window scroll functions, if any, for WINDOW with new window
14335 start STARTP. Sets the window start of WINDOW to that position.
14336
14337 We assume that the window's buffer is really current. */
14338
14339 static struct text_pos
14340 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14341 {
14342 struct window *w = XWINDOW (window);
14343 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14344
14345 if (current_buffer != XBUFFER (w->buffer))
14346 emacs_abort ();
14347
14348 if (!NILP (Vwindow_scroll_functions))
14349 {
14350 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14351 make_number (CHARPOS (startp)));
14352 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14353 /* In case the hook functions switch buffers. */
14354 set_buffer_internal (XBUFFER (w->buffer));
14355 }
14356
14357 return startp;
14358 }
14359
14360
14361 /* Make sure the line containing the cursor is fully visible.
14362 A value of 1 means there is nothing to be done.
14363 (Either the line is fully visible, or it cannot be made so,
14364 or we cannot tell.)
14365
14366 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14367 is higher than window.
14368
14369 A value of 0 means the caller should do scrolling
14370 as if point had gone off the screen. */
14371
14372 static int
14373 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14374 {
14375 struct glyph_matrix *matrix;
14376 struct glyph_row *row;
14377 int window_height;
14378
14379 if (!make_cursor_line_fully_visible_p)
14380 return 1;
14381
14382 /* It's not always possible to find the cursor, e.g, when a window
14383 is full of overlay strings. Don't do anything in that case. */
14384 if (w->cursor.vpos < 0)
14385 return 1;
14386
14387 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14388 row = MATRIX_ROW (matrix, w->cursor.vpos);
14389
14390 /* If the cursor row is not partially visible, there's nothing to do. */
14391 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14392 return 1;
14393
14394 /* If the row the cursor is in is taller than the window's height,
14395 it's not clear what to do, so do nothing. */
14396 window_height = window_box_height (w);
14397 if (row->height >= window_height)
14398 {
14399 if (!force_p || MINI_WINDOW_P (w)
14400 || w->vscroll || w->cursor.vpos == 0)
14401 return 1;
14402 }
14403 return 0;
14404 }
14405
14406
14407 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14408 non-zero means only WINDOW is redisplayed in redisplay_internal.
14409 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14410 in redisplay_window to bring a partially visible line into view in
14411 the case that only the cursor has moved.
14412
14413 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14414 last screen line's vertical height extends past the end of the screen.
14415
14416 Value is
14417
14418 1 if scrolling succeeded
14419
14420 0 if scrolling didn't find point.
14421
14422 -1 if new fonts have been loaded so that we must interrupt
14423 redisplay, adjust glyph matrices, and try again. */
14424
14425 enum
14426 {
14427 SCROLLING_SUCCESS,
14428 SCROLLING_FAILED,
14429 SCROLLING_NEED_LARGER_MATRICES
14430 };
14431
14432 /* If scroll-conservatively is more than this, never recenter.
14433
14434 If you change this, don't forget to update the doc string of
14435 `scroll-conservatively' and the Emacs manual. */
14436 #define SCROLL_LIMIT 100
14437
14438 static int
14439 try_scrolling (Lisp_Object window, int just_this_one_p,
14440 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14441 int temp_scroll_step, int last_line_misfit)
14442 {
14443 struct window *w = XWINDOW (window);
14444 struct frame *f = XFRAME (w->frame);
14445 struct text_pos pos, startp;
14446 struct it it;
14447 int this_scroll_margin, scroll_max, rc, height;
14448 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14449 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14450 Lisp_Object aggressive;
14451 /* We will never try scrolling more than this number of lines. */
14452 int scroll_limit = SCROLL_LIMIT;
14453
14454 #ifdef GLYPH_DEBUG
14455 debug_method_add (w, "try_scrolling");
14456 #endif
14457
14458 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14459
14460 /* Compute scroll margin height in pixels. We scroll when point is
14461 within this distance from the top or bottom of the window. */
14462 if (scroll_margin > 0)
14463 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14464 * FRAME_LINE_HEIGHT (f);
14465 else
14466 this_scroll_margin = 0;
14467
14468 /* Force arg_scroll_conservatively to have a reasonable value, to
14469 avoid scrolling too far away with slow move_it_* functions. Note
14470 that the user can supply scroll-conservatively equal to
14471 `most-positive-fixnum', which can be larger than INT_MAX. */
14472 if (arg_scroll_conservatively > scroll_limit)
14473 {
14474 arg_scroll_conservatively = scroll_limit + 1;
14475 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14476 }
14477 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14478 /* Compute how much we should try to scroll maximally to bring
14479 point into view. */
14480 scroll_max = (max (scroll_step,
14481 max (arg_scroll_conservatively, temp_scroll_step))
14482 * FRAME_LINE_HEIGHT (f));
14483 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14484 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14485 /* We're trying to scroll because of aggressive scrolling but no
14486 scroll_step is set. Choose an arbitrary one. */
14487 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14488 else
14489 scroll_max = 0;
14490
14491 too_near_end:
14492
14493 /* Decide whether to scroll down. */
14494 if (PT > CHARPOS (startp))
14495 {
14496 int scroll_margin_y;
14497
14498 /* Compute the pixel ypos of the scroll margin, then move IT to
14499 either that ypos or PT, whichever comes first. */
14500 start_display (&it, w, startp);
14501 scroll_margin_y = it.last_visible_y - this_scroll_margin
14502 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14503 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14504 (MOVE_TO_POS | MOVE_TO_Y));
14505
14506 if (PT > CHARPOS (it.current.pos))
14507 {
14508 int y0 = line_bottom_y (&it);
14509 /* Compute how many pixels below window bottom to stop searching
14510 for PT. This avoids costly search for PT that is far away if
14511 the user limited scrolling by a small number of lines, but
14512 always finds PT if scroll_conservatively is set to a large
14513 number, such as most-positive-fixnum. */
14514 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14515 int y_to_move = it.last_visible_y + slack;
14516
14517 /* Compute the distance from the scroll margin to PT or to
14518 the scroll limit, whichever comes first. This should
14519 include the height of the cursor line, to make that line
14520 fully visible. */
14521 move_it_to (&it, PT, -1, y_to_move,
14522 -1, MOVE_TO_POS | MOVE_TO_Y);
14523 dy = line_bottom_y (&it) - y0;
14524
14525 if (dy > scroll_max)
14526 return SCROLLING_FAILED;
14527
14528 if (dy > 0)
14529 scroll_down_p = 1;
14530 }
14531 }
14532
14533 if (scroll_down_p)
14534 {
14535 /* Point is in or below the bottom scroll margin, so move the
14536 window start down. If scrolling conservatively, move it just
14537 enough down to make point visible. If scroll_step is set,
14538 move it down by scroll_step. */
14539 if (arg_scroll_conservatively)
14540 amount_to_scroll
14541 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14542 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14543 else if (scroll_step || temp_scroll_step)
14544 amount_to_scroll = scroll_max;
14545 else
14546 {
14547 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14548 height = WINDOW_BOX_TEXT_HEIGHT (w);
14549 if (NUMBERP (aggressive))
14550 {
14551 double float_amount = XFLOATINT (aggressive) * height;
14552 int aggressive_scroll = float_amount;
14553 if (aggressive_scroll == 0 && float_amount > 0)
14554 aggressive_scroll = 1;
14555 /* Don't let point enter the scroll margin near top of
14556 the window. This could happen if the value of
14557 scroll_up_aggressively is too large and there are
14558 non-zero margins, because scroll_up_aggressively
14559 means put point that fraction of window height
14560 _from_the_bottom_margin_. */
14561 if (aggressive_scroll + 2*this_scroll_margin > height)
14562 aggressive_scroll = height - 2*this_scroll_margin;
14563 amount_to_scroll = dy + aggressive_scroll;
14564 }
14565 }
14566
14567 if (amount_to_scroll <= 0)
14568 return SCROLLING_FAILED;
14569
14570 start_display (&it, w, startp);
14571 if (arg_scroll_conservatively <= scroll_limit)
14572 move_it_vertically (&it, amount_to_scroll);
14573 else
14574 {
14575 /* Extra precision for users who set scroll-conservatively
14576 to a large number: make sure the amount we scroll
14577 the window start is never less than amount_to_scroll,
14578 which was computed as distance from window bottom to
14579 point. This matters when lines at window top and lines
14580 below window bottom have different height. */
14581 struct it it1;
14582 void *it1data = NULL;
14583 /* We use a temporary it1 because line_bottom_y can modify
14584 its argument, if it moves one line down; see there. */
14585 int start_y;
14586
14587 SAVE_IT (it1, it, it1data);
14588 start_y = line_bottom_y (&it1);
14589 do {
14590 RESTORE_IT (&it, &it, it1data);
14591 move_it_by_lines (&it, 1);
14592 SAVE_IT (it1, it, it1data);
14593 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14594 }
14595
14596 /* If STARTP is unchanged, move it down another screen line. */
14597 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14598 move_it_by_lines (&it, 1);
14599 startp = it.current.pos;
14600 }
14601 else
14602 {
14603 struct text_pos scroll_margin_pos = startp;
14604
14605 /* See if point is inside the scroll margin at the top of the
14606 window. */
14607 if (this_scroll_margin)
14608 {
14609 start_display (&it, w, startp);
14610 move_it_vertically (&it, this_scroll_margin);
14611 scroll_margin_pos = it.current.pos;
14612 }
14613
14614 if (PT < CHARPOS (scroll_margin_pos))
14615 {
14616 /* Point is in the scroll margin at the top of the window or
14617 above what is displayed in the window. */
14618 int y0, y_to_move;
14619
14620 /* Compute the vertical distance from PT to the scroll
14621 margin position. Move as far as scroll_max allows, or
14622 one screenful, or 10 screen lines, whichever is largest.
14623 Give up if distance is greater than scroll_max or if we
14624 didn't reach the scroll margin position. */
14625 SET_TEXT_POS (pos, PT, PT_BYTE);
14626 start_display (&it, w, pos);
14627 y0 = it.current_y;
14628 y_to_move = max (it.last_visible_y,
14629 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14630 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14631 y_to_move, -1,
14632 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14633 dy = it.current_y - y0;
14634 if (dy > scroll_max
14635 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14636 return SCROLLING_FAILED;
14637
14638 /* Compute new window start. */
14639 start_display (&it, w, startp);
14640
14641 if (arg_scroll_conservatively)
14642 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14643 max (scroll_step, temp_scroll_step));
14644 else if (scroll_step || temp_scroll_step)
14645 amount_to_scroll = scroll_max;
14646 else
14647 {
14648 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14649 height = WINDOW_BOX_TEXT_HEIGHT (w);
14650 if (NUMBERP (aggressive))
14651 {
14652 double float_amount = XFLOATINT (aggressive) * height;
14653 int aggressive_scroll = float_amount;
14654 if (aggressive_scroll == 0 && float_amount > 0)
14655 aggressive_scroll = 1;
14656 /* Don't let point enter the scroll margin near
14657 bottom of the window, if the value of
14658 scroll_down_aggressively happens to be too
14659 large. */
14660 if (aggressive_scroll + 2*this_scroll_margin > height)
14661 aggressive_scroll = height - 2*this_scroll_margin;
14662 amount_to_scroll = dy + aggressive_scroll;
14663 }
14664 }
14665
14666 if (amount_to_scroll <= 0)
14667 return SCROLLING_FAILED;
14668
14669 move_it_vertically_backward (&it, amount_to_scroll);
14670 startp = it.current.pos;
14671 }
14672 }
14673
14674 /* Run window scroll functions. */
14675 startp = run_window_scroll_functions (window, startp);
14676
14677 /* Display the window. Give up if new fonts are loaded, or if point
14678 doesn't appear. */
14679 if (!try_window (window, startp, 0))
14680 rc = SCROLLING_NEED_LARGER_MATRICES;
14681 else if (w->cursor.vpos < 0)
14682 {
14683 clear_glyph_matrix (w->desired_matrix);
14684 rc = SCROLLING_FAILED;
14685 }
14686 else
14687 {
14688 /* Maybe forget recorded base line for line number display. */
14689 if (!just_this_one_p
14690 || current_buffer->clip_changed
14691 || BEG_UNCHANGED < CHARPOS (startp))
14692 w->base_line_number = 0;
14693
14694 /* If cursor ends up on a partially visible line,
14695 treat that as being off the bottom of the screen. */
14696 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14697 /* It's possible that the cursor is on the first line of the
14698 buffer, which is partially obscured due to a vscroll
14699 (Bug#7537). In that case, avoid looping forever . */
14700 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14701 {
14702 clear_glyph_matrix (w->desired_matrix);
14703 ++extra_scroll_margin_lines;
14704 goto too_near_end;
14705 }
14706 rc = SCROLLING_SUCCESS;
14707 }
14708
14709 return rc;
14710 }
14711
14712
14713 /* Compute a suitable window start for window W if display of W starts
14714 on a continuation line. Value is non-zero if a new window start
14715 was computed.
14716
14717 The new window start will be computed, based on W's width, starting
14718 from the start of the continued line. It is the start of the
14719 screen line with the minimum distance from the old start W->start. */
14720
14721 static int
14722 compute_window_start_on_continuation_line (struct window *w)
14723 {
14724 struct text_pos pos, start_pos;
14725 int window_start_changed_p = 0;
14726
14727 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14728
14729 /* If window start is on a continuation line... Window start may be
14730 < BEGV in case there's invisible text at the start of the
14731 buffer (M-x rmail, for example). */
14732 if (CHARPOS (start_pos) > BEGV
14733 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14734 {
14735 struct it it;
14736 struct glyph_row *row;
14737
14738 /* Handle the case that the window start is out of range. */
14739 if (CHARPOS (start_pos) < BEGV)
14740 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14741 else if (CHARPOS (start_pos) > ZV)
14742 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14743
14744 /* Find the start of the continued line. This should be fast
14745 because scan_buffer is fast (newline cache). */
14746 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14747 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14748 row, DEFAULT_FACE_ID);
14749 reseat_at_previous_visible_line_start (&it);
14750
14751 /* If the line start is "too far" away from the window start,
14752 say it takes too much time to compute a new window start. */
14753 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14754 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14755 {
14756 int min_distance, distance;
14757
14758 /* Move forward by display lines to find the new window
14759 start. If window width was enlarged, the new start can
14760 be expected to be > the old start. If window width was
14761 decreased, the new window start will be < the old start.
14762 So, we're looking for the display line start with the
14763 minimum distance from the old window start. */
14764 pos = it.current.pos;
14765 min_distance = INFINITY;
14766 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14767 distance < min_distance)
14768 {
14769 min_distance = distance;
14770 pos = it.current.pos;
14771 move_it_by_lines (&it, 1);
14772 }
14773
14774 /* Set the window start there. */
14775 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14776 window_start_changed_p = 1;
14777 }
14778 }
14779
14780 return window_start_changed_p;
14781 }
14782
14783
14784 /* Try cursor movement in case text has not changed in window WINDOW,
14785 with window start STARTP. Value is
14786
14787 CURSOR_MOVEMENT_SUCCESS if successful
14788
14789 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14790
14791 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14792 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14793 we want to scroll as if scroll-step were set to 1. See the code.
14794
14795 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14796 which case we have to abort this redisplay, and adjust matrices
14797 first. */
14798
14799 enum
14800 {
14801 CURSOR_MOVEMENT_SUCCESS,
14802 CURSOR_MOVEMENT_CANNOT_BE_USED,
14803 CURSOR_MOVEMENT_MUST_SCROLL,
14804 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14805 };
14806
14807 static int
14808 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14809 {
14810 struct window *w = XWINDOW (window);
14811 struct frame *f = XFRAME (w->frame);
14812 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14813
14814 #ifdef GLYPH_DEBUG
14815 if (inhibit_try_cursor_movement)
14816 return rc;
14817 #endif
14818
14819 /* Previously, there was a check for Lisp integer in the
14820 if-statement below. Now, this field is converted to
14821 ptrdiff_t, thus zero means invalid position in a buffer. */
14822 eassert (w->last_point > 0);
14823
14824 /* Handle case where text has not changed, only point, and it has
14825 not moved off the frame. */
14826 if (/* Point may be in this window. */
14827 PT >= CHARPOS (startp)
14828 /* Selective display hasn't changed. */
14829 && !current_buffer->clip_changed
14830 /* Function force-mode-line-update is used to force a thorough
14831 redisplay. It sets either windows_or_buffers_changed or
14832 update_mode_lines. So don't take a shortcut here for these
14833 cases. */
14834 && !update_mode_lines
14835 && !windows_or_buffers_changed
14836 && !cursor_type_changed
14837 /* Can't use this case if highlighting a region. When a
14838 region exists, cursor movement has to do more than just
14839 set the cursor. */
14840 && markpos_of_region () < 0
14841 && !w->region_showing
14842 && NILP (Vshow_trailing_whitespace)
14843 /* This code is not used for mini-buffer for the sake of the case
14844 of redisplaying to replace an echo area message; since in
14845 that case the mini-buffer contents per se are usually
14846 unchanged. This code is of no real use in the mini-buffer
14847 since the handling of this_line_start_pos, etc., in redisplay
14848 handles the same cases. */
14849 && !EQ (window, minibuf_window)
14850 /* When splitting windows or for new windows, it happens that
14851 redisplay is called with a nil window_end_vpos or one being
14852 larger than the window. This should really be fixed in
14853 window.c. I don't have this on my list, now, so we do
14854 approximately the same as the old redisplay code. --gerd. */
14855 && INTEGERP (w->window_end_vpos)
14856 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14857 && (FRAME_WINDOW_P (f)
14858 || !overlay_arrow_in_current_buffer_p ()))
14859 {
14860 int this_scroll_margin, top_scroll_margin;
14861 struct glyph_row *row = NULL;
14862
14863 #ifdef GLYPH_DEBUG
14864 debug_method_add (w, "cursor movement");
14865 #endif
14866
14867 /* Scroll if point within this distance from the top or bottom
14868 of the window. This is a pixel value. */
14869 if (scroll_margin > 0)
14870 {
14871 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14872 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14873 }
14874 else
14875 this_scroll_margin = 0;
14876
14877 top_scroll_margin = this_scroll_margin;
14878 if (WINDOW_WANTS_HEADER_LINE_P (w))
14879 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14880
14881 /* Start with the row the cursor was displayed during the last
14882 not paused redisplay. Give up if that row is not valid. */
14883 if (w->last_cursor.vpos < 0
14884 || w->last_cursor.vpos >= w->current_matrix->nrows)
14885 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14886 else
14887 {
14888 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14889 if (row->mode_line_p)
14890 ++row;
14891 if (!row->enabled_p)
14892 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14893 }
14894
14895 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14896 {
14897 int scroll_p = 0, must_scroll = 0;
14898 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14899
14900 if (PT > w->last_point)
14901 {
14902 /* Point has moved forward. */
14903 while (MATRIX_ROW_END_CHARPOS (row) < PT
14904 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14905 {
14906 eassert (row->enabled_p);
14907 ++row;
14908 }
14909
14910 /* If the end position of a row equals the start
14911 position of the next row, and PT is at that position,
14912 we would rather display cursor in the next line. */
14913 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14914 && MATRIX_ROW_END_CHARPOS (row) == PT
14915 && row < w->current_matrix->rows
14916 + w->current_matrix->nrows - 1
14917 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14918 && !cursor_row_p (row))
14919 ++row;
14920
14921 /* If within the scroll margin, scroll. Note that
14922 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14923 the next line would be drawn, and that
14924 this_scroll_margin can be zero. */
14925 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14926 || PT > MATRIX_ROW_END_CHARPOS (row)
14927 /* Line is completely visible last line in window
14928 and PT is to be set in the next line. */
14929 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14930 && PT == MATRIX_ROW_END_CHARPOS (row)
14931 && !row->ends_at_zv_p
14932 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14933 scroll_p = 1;
14934 }
14935 else if (PT < w->last_point)
14936 {
14937 /* Cursor has to be moved backward. Note that PT >=
14938 CHARPOS (startp) because of the outer if-statement. */
14939 while (!row->mode_line_p
14940 && (MATRIX_ROW_START_CHARPOS (row) > PT
14941 || (MATRIX_ROW_START_CHARPOS (row) == PT
14942 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14943 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14944 row > w->current_matrix->rows
14945 && (row-1)->ends_in_newline_from_string_p))))
14946 && (row->y > top_scroll_margin
14947 || CHARPOS (startp) == BEGV))
14948 {
14949 eassert (row->enabled_p);
14950 --row;
14951 }
14952
14953 /* Consider the following case: Window starts at BEGV,
14954 there is invisible, intangible text at BEGV, so that
14955 display starts at some point START > BEGV. It can
14956 happen that we are called with PT somewhere between
14957 BEGV and START. Try to handle that case. */
14958 if (row < w->current_matrix->rows
14959 || row->mode_line_p)
14960 {
14961 row = w->current_matrix->rows;
14962 if (row->mode_line_p)
14963 ++row;
14964 }
14965
14966 /* Due to newlines in overlay strings, we may have to
14967 skip forward over overlay strings. */
14968 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14969 && MATRIX_ROW_END_CHARPOS (row) == PT
14970 && !cursor_row_p (row))
14971 ++row;
14972
14973 /* If within the scroll margin, scroll. */
14974 if (row->y < top_scroll_margin
14975 && CHARPOS (startp) != BEGV)
14976 scroll_p = 1;
14977 }
14978 else
14979 {
14980 /* Cursor did not move. So don't scroll even if cursor line
14981 is partially visible, as it was so before. */
14982 rc = CURSOR_MOVEMENT_SUCCESS;
14983 }
14984
14985 if (PT < MATRIX_ROW_START_CHARPOS (row)
14986 || PT > MATRIX_ROW_END_CHARPOS (row))
14987 {
14988 /* if PT is not in the glyph row, give up. */
14989 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14990 must_scroll = 1;
14991 }
14992 else if (rc != CURSOR_MOVEMENT_SUCCESS
14993 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14994 {
14995 struct glyph_row *row1;
14996
14997 /* If rows are bidi-reordered and point moved, back up
14998 until we find a row that does not belong to a
14999 continuation line. This is because we must consider
15000 all rows of a continued line as candidates for the
15001 new cursor positioning, since row start and end
15002 positions change non-linearly with vertical position
15003 in such rows. */
15004 /* FIXME: Revisit this when glyph ``spilling'' in
15005 continuation lines' rows is implemented for
15006 bidi-reordered rows. */
15007 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15008 MATRIX_ROW_CONTINUATION_LINE_P (row);
15009 --row)
15010 {
15011 /* If we hit the beginning of the displayed portion
15012 without finding the first row of a continued
15013 line, give up. */
15014 if (row <= row1)
15015 {
15016 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15017 break;
15018 }
15019 eassert (row->enabled_p);
15020 }
15021 }
15022 if (must_scroll)
15023 ;
15024 else if (rc != CURSOR_MOVEMENT_SUCCESS
15025 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15026 /* Make sure this isn't a header line by any chance, since
15027 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15028 && !row->mode_line_p
15029 && make_cursor_line_fully_visible_p)
15030 {
15031 if (PT == MATRIX_ROW_END_CHARPOS (row)
15032 && !row->ends_at_zv_p
15033 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15034 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15035 else if (row->height > window_box_height (w))
15036 {
15037 /* If we end up in a partially visible line, let's
15038 make it fully visible, except when it's taller
15039 than the window, in which case we can't do much
15040 about it. */
15041 *scroll_step = 1;
15042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15043 }
15044 else
15045 {
15046 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15047 if (!cursor_row_fully_visible_p (w, 0, 1))
15048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15049 else
15050 rc = CURSOR_MOVEMENT_SUCCESS;
15051 }
15052 }
15053 else if (scroll_p)
15054 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15055 else if (rc != CURSOR_MOVEMENT_SUCCESS
15056 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15057 {
15058 /* With bidi-reordered rows, there could be more than
15059 one candidate row whose start and end positions
15060 occlude point. We need to let set_cursor_from_row
15061 find the best candidate. */
15062 /* FIXME: Revisit this when glyph ``spilling'' in
15063 continuation lines' rows is implemented for
15064 bidi-reordered rows. */
15065 int rv = 0;
15066
15067 do
15068 {
15069 int at_zv_p = 0, exact_match_p = 0;
15070
15071 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15072 && PT <= MATRIX_ROW_END_CHARPOS (row)
15073 && cursor_row_p (row))
15074 rv |= set_cursor_from_row (w, row, w->current_matrix,
15075 0, 0, 0, 0);
15076 /* As soon as we've found the exact match for point,
15077 or the first suitable row whose ends_at_zv_p flag
15078 is set, we are done. */
15079 at_zv_p =
15080 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15081 if (rv && !at_zv_p
15082 && w->cursor.hpos >= 0
15083 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15084 w->cursor.vpos))
15085 {
15086 struct glyph_row *candidate =
15087 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15088 struct glyph *g =
15089 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15090 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15091
15092 exact_match_p =
15093 (BUFFERP (g->object) && g->charpos == PT)
15094 || (INTEGERP (g->object)
15095 && (g->charpos == PT
15096 || (g->charpos == 0 && endpos - 1 == PT)));
15097 }
15098 if (rv && (at_zv_p || exact_match_p))
15099 {
15100 rc = CURSOR_MOVEMENT_SUCCESS;
15101 break;
15102 }
15103 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15104 break;
15105 ++row;
15106 }
15107 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15108 || row->continued_p)
15109 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15110 || (MATRIX_ROW_START_CHARPOS (row) == PT
15111 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15112 /* If we didn't find any candidate rows, or exited the
15113 loop before all the candidates were examined, signal
15114 to the caller that this method failed. */
15115 if (rc != CURSOR_MOVEMENT_SUCCESS
15116 && !(rv
15117 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15118 && !row->continued_p))
15119 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15120 else if (rv)
15121 rc = CURSOR_MOVEMENT_SUCCESS;
15122 }
15123 else
15124 {
15125 do
15126 {
15127 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15128 {
15129 rc = CURSOR_MOVEMENT_SUCCESS;
15130 break;
15131 }
15132 ++row;
15133 }
15134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15135 && MATRIX_ROW_START_CHARPOS (row) == PT
15136 && cursor_row_p (row));
15137 }
15138 }
15139 }
15140
15141 return rc;
15142 }
15143
15144 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15145 static
15146 #endif
15147 void
15148 set_vertical_scroll_bar (struct window *w)
15149 {
15150 ptrdiff_t start, end, whole;
15151
15152 /* Calculate the start and end positions for the current window.
15153 At some point, it would be nice to choose between scrollbars
15154 which reflect the whole buffer size, with special markers
15155 indicating narrowing, and scrollbars which reflect only the
15156 visible region.
15157
15158 Note that mini-buffers sometimes aren't displaying any text. */
15159 if (!MINI_WINDOW_P (w)
15160 || (w == XWINDOW (minibuf_window)
15161 && NILP (echo_area_buffer[0])))
15162 {
15163 struct buffer *buf = XBUFFER (w->buffer);
15164 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15165 start = marker_position (w->start) - BUF_BEGV (buf);
15166 /* I don't think this is guaranteed to be right. For the
15167 moment, we'll pretend it is. */
15168 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15169
15170 if (end < start)
15171 end = start;
15172 if (whole < (end - start))
15173 whole = end - start;
15174 }
15175 else
15176 start = end = whole = 0;
15177
15178 /* Indicate what this scroll bar ought to be displaying now. */
15179 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15180 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15181 (w, end - start, whole, start);
15182 }
15183
15184
15185 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15186 selected_window is redisplayed.
15187
15188 We can return without actually redisplaying the window if
15189 fonts_changed_p. In that case, redisplay_internal will
15190 retry. */
15191
15192 static void
15193 redisplay_window (Lisp_Object window, int just_this_one_p)
15194 {
15195 struct window *w = XWINDOW (window);
15196 struct frame *f = XFRAME (w->frame);
15197 struct buffer *buffer = XBUFFER (w->buffer);
15198 struct buffer *old = current_buffer;
15199 struct text_pos lpoint, opoint, startp;
15200 int update_mode_line;
15201 int tem;
15202 struct it it;
15203 /* Record it now because it's overwritten. */
15204 int current_matrix_up_to_date_p = 0;
15205 int used_current_matrix_p = 0;
15206 /* This is less strict than current_matrix_up_to_date_p.
15207 It indicates that the buffer contents and narrowing are unchanged. */
15208 int buffer_unchanged_p = 0;
15209 int temp_scroll_step = 0;
15210 ptrdiff_t count = SPECPDL_INDEX ();
15211 int rc;
15212 int centering_position = -1;
15213 int last_line_misfit = 0;
15214 ptrdiff_t beg_unchanged, end_unchanged;
15215
15216 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15217 opoint = lpoint;
15218
15219 /* W must be a leaf window here. */
15220 eassert (!NILP (w->buffer));
15221 #ifdef GLYPH_DEBUG
15222 *w->desired_matrix->method = 0;
15223 #endif
15224
15225 restart:
15226 reconsider_clip_changes (w, buffer);
15227
15228 /* Has the mode line to be updated? */
15229 update_mode_line = (w->update_mode_line
15230 || update_mode_lines
15231 || buffer->clip_changed
15232 || buffer->prevent_redisplay_optimizations_p);
15233
15234 if (MINI_WINDOW_P (w))
15235 {
15236 if (w == XWINDOW (echo_area_window)
15237 && !NILP (echo_area_buffer[0]))
15238 {
15239 if (update_mode_line)
15240 /* We may have to update a tty frame's menu bar or a
15241 tool-bar. Example `M-x C-h C-h C-g'. */
15242 goto finish_menu_bars;
15243 else
15244 /* We've already displayed the echo area glyphs in this window. */
15245 goto finish_scroll_bars;
15246 }
15247 else if ((w != XWINDOW (minibuf_window)
15248 || minibuf_level == 0)
15249 /* When buffer is nonempty, redisplay window normally. */
15250 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15251 /* Quail displays non-mini buffers in minibuffer window.
15252 In that case, redisplay the window normally. */
15253 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15254 {
15255 /* W is a mini-buffer window, but it's not active, so clear
15256 it. */
15257 int yb = window_text_bottom_y (w);
15258 struct glyph_row *row;
15259 int y;
15260
15261 for (y = 0, row = w->desired_matrix->rows;
15262 y < yb;
15263 y += row->height, ++row)
15264 blank_row (w, row, y);
15265 goto finish_scroll_bars;
15266 }
15267
15268 clear_glyph_matrix (w->desired_matrix);
15269 }
15270
15271 /* Otherwise set up data on this window; select its buffer and point
15272 value. */
15273 /* Really select the buffer, for the sake of buffer-local
15274 variables. */
15275 set_buffer_internal_1 (XBUFFER (w->buffer));
15276
15277 current_matrix_up_to_date_p
15278 = (w->window_end_valid
15279 && !current_buffer->clip_changed
15280 && !current_buffer->prevent_redisplay_optimizations_p
15281 && !window_outdated (w));
15282
15283 /* Run the window-bottom-change-functions
15284 if it is possible that the text on the screen has changed
15285 (either due to modification of the text, or any other reason). */
15286 if (!current_matrix_up_to_date_p
15287 && !NILP (Vwindow_text_change_functions))
15288 {
15289 safe_run_hooks (Qwindow_text_change_functions);
15290 goto restart;
15291 }
15292
15293 beg_unchanged = BEG_UNCHANGED;
15294 end_unchanged = END_UNCHANGED;
15295
15296 SET_TEXT_POS (opoint, PT, PT_BYTE);
15297
15298 specbind (Qinhibit_point_motion_hooks, Qt);
15299
15300 buffer_unchanged_p
15301 = (w->window_end_valid
15302 && !current_buffer->clip_changed
15303 && !window_outdated (w));
15304
15305 /* When windows_or_buffers_changed is non-zero, we can't rely on
15306 the window end being valid, so set it to nil there. */
15307 if (windows_or_buffers_changed)
15308 {
15309 /* If window starts on a continuation line, maybe adjust the
15310 window start in case the window's width changed. */
15311 if (XMARKER (w->start)->buffer == current_buffer)
15312 compute_window_start_on_continuation_line (w);
15313
15314 w->window_end_valid = 0;
15315 }
15316
15317 /* Some sanity checks. */
15318 CHECK_WINDOW_END (w);
15319 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15320 emacs_abort ();
15321 if (BYTEPOS (opoint) < CHARPOS (opoint))
15322 emacs_abort ();
15323
15324 if (mode_line_update_needed (w))
15325 update_mode_line = 1;
15326
15327 /* Point refers normally to the selected window. For any other
15328 window, set up appropriate value. */
15329 if (!EQ (window, selected_window))
15330 {
15331 ptrdiff_t new_pt = marker_position (w->pointm);
15332 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15333 if (new_pt < BEGV)
15334 {
15335 new_pt = BEGV;
15336 new_pt_byte = BEGV_BYTE;
15337 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15338 }
15339 else if (new_pt > (ZV - 1))
15340 {
15341 new_pt = ZV;
15342 new_pt_byte = ZV_BYTE;
15343 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15344 }
15345
15346 /* We don't use SET_PT so that the point-motion hooks don't run. */
15347 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15348 }
15349
15350 /* If any of the character widths specified in the display table
15351 have changed, invalidate the width run cache. It's true that
15352 this may be a bit late to catch such changes, but the rest of
15353 redisplay goes (non-fatally) haywire when the display table is
15354 changed, so why should we worry about doing any better? */
15355 if (current_buffer->width_run_cache)
15356 {
15357 struct Lisp_Char_Table *disptab = buffer_display_table ();
15358
15359 if (! disptab_matches_widthtab
15360 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15361 {
15362 invalidate_region_cache (current_buffer,
15363 current_buffer->width_run_cache,
15364 BEG, Z);
15365 recompute_width_table (current_buffer, disptab);
15366 }
15367 }
15368
15369 /* If window-start is screwed up, choose a new one. */
15370 if (XMARKER (w->start)->buffer != current_buffer)
15371 goto recenter;
15372
15373 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15374
15375 /* If someone specified a new starting point but did not insist,
15376 check whether it can be used. */
15377 if (w->optional_new_start
15378 && CHARPOS (startp) >= BEGV
15379 && CHARPOS (startp) <= ZV)
15380 {
15381 w->optional_new_start = 0;
15382 start_display (&it, w, startp);
15383 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15384 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15385 if (IT_CHARPOS (it) == PT)
15386 w->force_start = 1;
15387 /* IT may overshoot PT if text at PT is invisible. */
15388 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15389 w->force_start = 1;
15390 }
15391
15392 force_start:
15393
15394 /* Handle case where place to start displaying has been specified,
15395 unless the specified location is outside the accessible range. */
15396 if (w->force_start || w->frozen_window_start_p)
15397 {
15398 /* We set this later on if we have to adjust point. */
15399 int new_vpos = -1;
15400
15401 w->force_start = 0;
15402 w->vscroll = 0;
15403 w->window_end_valid = 0;
15404
15405 /* Forget any recorded base line for line number display. */
15406 if (!buffer_unchanged_p)
15407 w->base_line_number = 0;
15408
15409 /* Redisplay the mode line. Select the buffer properly for that.
15410 Also, run the hook window-scroll-functions
15411 because we have scrolled. */
15412 /* Note, we do this after clearing force_start because
15413 if there's an error, it is better to forget about force_start
15414 than to get into an infinite loop calling the hook functions
15415 and having them get more errors. */
15416 if (!update_mode_line
15417 || ! NILP (Vwindow_scroll_functions))
15418 {
15419 update_mode_line = 1;
15420 w->update_mode_line = 1;
15421 startp = run_window_scroll_functions (window, startp);
15422 }
15423
15424 w->last_modified = 0;
15425 w->last_overlay_modified = 0;
15426 if (CHARPOS (startp) < BEGV)
15427 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15428 else if (CHARPOS (startp) > ZV)
15429 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15430
15431 /* Redisplay, then check if cursor has been set during the
15432 redisplay. Give up if new fonts were loaded. */
15433 /* We used to issue a CHECK_MARGINS argument to try_window here,
15434 but this causes scrolling to fail when point begins inside
15435 the scroll margin (bug#148) -- cyd */
15436 if (!try_window (window, startp, 0))
15437 {
15438 w->force_start = 1;
15439 clear_glyph_matrix (w->desired_matrix);
15440 goto need_larger_matrices;
15441 }
15442
15443 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15444 {
15445 /* If point does not appear, try to move point so it does
15446 appear. The desired matrix has been built above, so we
15447 can use it here. */
15448 new_vpos = window_box_height (w) / 2;
15449 }
15450
15451 if (!cursor_row_fully_visible_p (w, 0, 0))
15452 {
15453 /* Point does appear, but on a line partly visible at end of window.
15454 Move it back to a fully-visible line. */
15455 new_vpos = window_box_height (w);
15456 }
15457 else if (w->cursor.vpos >=0)
15458 {
15459 /* Some people insist on not letting point enter the scroll
15460 margin, even though this part handles windows that didn't
15461 scroll at all. */
15462 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15463 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15464 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15465
15466 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15467 below, which finds the row to move point to, advances by
15468 the Y coordinate of the _next_ row, see the definition of
15469 MATRIX_ROW_BOTTOM_Y. */
15470 if (w->cursor.vpos < margin + header_line)
15471 new_vpos
15472 = pixel_margin + (header_line
15473 ? CURRENT_HEADER_LINE_HEIGHT (w)
15474 : 0) + FRAME_LINE_HEIGHT (f);
15475 else
15476 {
15477 int window_height = window_box_height (w);
15478
15479 if (header_line)
15480 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15481 if (w->cursor.y >= window_height - pixel_margin)
15482 new_vpos = window_height - pixel_margin;
15483 }
15484 }
15485
15486 /* If we need to move point for either of the above reasons,
15487 now actually do it. */
15488 if (new_vpos >= 0)
15489 {
15490 struct glyph_row *row;
15491
15492 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15493 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15494 ++row;
15495
15496 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15497 MATRIX_ROW_START_BYTEPOS (row));
15498
15499 if (w != XWINDOW (selected_window))
15500 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15501 else if (current_buffer == old)
15502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15503
15504 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15505
15506 /* If we are highlighting the region, then we just changed
15507 the region, so redisplay to show it. */
15508 if (0 <= markpos_of_region ())
15509 {
15510 clear_glyph_matrix (w->desired_matrix);
15511 if (!try_window (window, startp, 0))
15512 goto need_larger_matrices;
15513 }
15514 }
15515
15516 #ifdef GLYPH_DEBUG
15517 debug_method_add (w, "forced window start");
15518 #endif
15519 goto done;
15520 }
15521
15522 /* Handle case where text has not changed, only point, and it has
15523 not moved off the frame, and we are not retrying after hscroll.
15524 (current_matrix_up_to_date_p is nonzero when retrying.) */
15525 if (current_matrix_up_to_date_p
15526 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15527 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15528 {
15529 switch (rc)
15530 {
15531 case CURSOR_MOVEMENT_SUCCESS:
15532 used_current_matrix_p = 1;
15533 goto done;
15534
15535 case CURSOR_MOVEMENT_MUST_SCROLL:
15536 goto try_to_scroll;
15537
15538 default:
15539 emacs_abort ();
15540 }
15541 }
15542 /* If current starting point was originally the beginning of a line
15543 but no longer is, find a new starting point. */
15544 else if (w->start_at_line_beg
15545 && !(CHARPOS (startp) <= BEGV
15546 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15547 {
15548 #ifdef GLYPH_DEBUG
15549 debug_method_add (w, "recenter 1");
15550 #endif
15551 goto recenter;
15552 }
15553
15554 /* Try scrolling with try_window_id. Value is > 0 if update has
15555 been done, it is -1 if we know that the same window start will
15556 not work. It is 0 if unsuccessful for some other reason. */
15557 else if ((tem = try_window_id (w)) != 0)
15558 {
15559 #ifdef GLYPH_DEBUG
15560 debug_method_add (w, "try_window_id %d", tem);
15561 #endif
15562
15563 if (fonts_changed_p)
15564 goto need_larger_matrices;
15565 if (tem > 0)
15566 goto done;
15567
15568 /* Otherwise try_window_id has returned -1 which means that we
15569 don't want the alternative below this comment to execute. */
15570 }
15571 else if (CHARPOS (startp) >= BEGV
15572 && CHARPOS (startp) <= ZV
15573 && PT >= CHARPOS (startp)
15574 && (CHARPOS (startp) < ZV
15575 /* Avoid starting at end of buffer. */
15576 || CHARPOS (startp) == BEGV
15577 || !window_outdated (w)))
15578 {
15579 int d1, d2, d3, d4, d5, d6;
15580
15581 /* If first window line is a continuation line, and window start
15582 is inside the modified region, but the first change is before
15583 current window start, we must select a new window start.
15584
15585 However, if this is the result of a down-mouse event (e.g. by
15586 extending the mouse-drag-overlay), we don't want to select a
15587 new window start, since that would change the position under
15588 the mouse, resulting in an unwanted mouse-movement rather
15589 than a simple mouse-click. */
15590 if (!w->start_at_line_beg
15591 && NILP (do_mouse_tracking)
15592 && CHARPOS (startp) > BEGV
15593 && CHARPOS (startp) > BEG + beg_unchanged
15594 && CHARPOS (startp) <= Z - end_unchanged
15595 /* Even if w->start_at_line_beg is nil, a new window may
15596 start at a line_beg, since that's how set_buffer_window
15597 sets it. So, we need to check the return value of
15598 compute_window_start_on_continuation_line. (See also
15599 bug#197). */
15600 && XMARKER (w->start)->buffer == current_buffer
15601 && compute_window_start_on_continuation_line (w)
15602 /* It doesn't make sense to force the window start like we
15603 do at label force_start if it is already known that point
15604 will not be visible in the resulting window, because
15605 doing so will move point from its correct position
15606 instead of scrolling the window to bring point into view.
15607 See bug#9324. */
15608 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15609 {
15610 w->force_start = 1;
15611 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15612 goto force_start;
15613 }
15614
15615 #ifdef GLYPH_DEBUG
15616 debug_method_add (w, "same window start");
15617 #endif
15618
15619 /* Try to redisplay starting at same place as before.
15620 If point has not moved off frame, accept the results. */
15621 if (!current_matrix_up_to_date_p
15622 /* Don't use try_window_reusing_current_matrix in this case
15623 because a window scroll function can have changed the
15624 buffer. */
15625 || !NILP (Vwindow_scroll_functions)
15626 || MINI_WINDOW_P (w)
15627 || !(used_current_matrix_p
15628 = try_window_reusing_current_matrix (w)))
15629 {
15630 IF_DEBUG (debug_method_add (w, "1"));
15631 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15632 /* -1 means we need to scroll.
15633 0 means we need new matrices, but fonts_changed_p
15634 is set in that case, so we will detect it below. */
15635 goto try_to_scroll;
15636 }
15637
15638 if (fonts_changed_p)
15639 goto need_larger_matrices;
15640
15641 if (w->cursor.vpos >= 0)
15642 {
15643 if (!just_this_one_p
15644 || current_buffer->clip_changed
15645 || BEG_UNCHANGED < CHARPOS (startp))
15646 /* Forget any recorded base line for line number display. */
15647 w->base_line_number = 0;
15648
15649 if (!cursor_row_fully_visible_p (w, 1, 0))
15650 {
15651 clear_glyph_matrix (w->desired_matrix);
15652 last_line_misfit = 1;
15653 }
15654 /* Drop through and scroll. */
15655 else
15656 goto done;
15657 }
15658 else
15659 clear_glyph_matrix (w->desired_matrix);
15660 }
15661
15662 try_to_scroll:
15663
15664 w->last_modified = 0;
15665 w->last_overlay_modified = 0;
15666
15667 /* Redisplay the mode line. Select the buffer properly for that. */
15668 if (!update_mode_line)
15669 {
15670 update_mode_line = 1;
15671 w->update_mode_line = 1;
15672 }
15673
15674 /* Try to scroll by specified few lines. */
15675 if ((scroll_conservatively
15676 || emacs_scroll_step
15677 || temp_scroll_step
15678 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15679 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15680 && CHARPOS (startp) >= BEGV
15681 && CHARPOS (startp) <= ZV)
15682 {
15683 /* The function returns -1 if new fonts were loaded, 1 if
15684 successful, 0 if not successful. */
15685 int ss = try_scrolling (window, just_this_one_p,
15686 scroll_conservatively,
15687 emacs_scroll_step,
15688 temp_scroll_step, last_line_misfit);
15689 switch (ss)
15690 {
15691 case SCROLLING_SUCCESS:
15692 goto done;
15693
15694 case SCROLLING_NEED_LARGER_MATRICES:
15695 goto need_larger_matrices;
15696
15697 case SCROLLING_FAILED:
15698 break;
15699
15700 default:
15701 emacs_abort ();
15702 }
15703 }
15704
15705 /* Finally, just choose a place to start which positions point
15706 according to user preferences. */
15707
15708 recenter:
15709
15710 #ifdef GLYPH_DEBUG
15711 debug_method_add (w, "recenter");
15712 #endif
15713
15714 /* Forget any previously recorded base line for line number display. */
15715 if (!buffer_unchanged_p)
15716 w->base_line_number = 0;
15717
15718 /* Determine the window start relative to point. */
15719 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15720 it.current_y = it.last_visible_y;
15721 if (centering_position < 0)
15722 {
15723 int margin =
15724 scroll_margin > 0
15725 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15726 : 0;
15727 ptrdiff_t margin_pos = CHARPOS (startp);
15728 Lisp_Object aggressive;
15729 int scrolling_up;
15730
15731 /* If there is a scroll margin at the top of the window, find
15732 its character position. */
15733 if (margin
15734 /* Cannot call start_display if startp is not in the
15735 accessible region of the buffer. This can happen when we
15736 have just switched to a different buffer and/or changed
15737 its restriction. In that case, startp is initialized to
15738 the character position 1 (BEGV) because we did not yet
15739 have chance to display the buffer even once. */
15740 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15741 {
15742 struct it it1;
15743 void *it1data = NULL;
15744
15745 SAVE_IT (it1, it, it1data);
15746 start_display (&it1, w, startp);
15747 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15748 margin_pos = IT_CHARPOS (it1);
15749 RESTORE_IT (&it, &it, it1data);
15750 }
15751 scrolling_up = PT > margin_pos;
15752 aggressive =
15753 scrolling_up
15754 ? BVAR (current_buffer, scroll_up_aggressively)
15755 : BVAR (current_buffer, scroll_down_aggressively);
15756
15757 if (!MINI_WINDOW_P (w)
15758 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15759 {
15760 int pt_offset = 0;
15761
15762 /* Setting scroll-conservatively overrides
15763 scroll-*-aggressively. */
15764 if (!scroll_conservatively && NUMBERP (aggressive))
15765 {
15766 double float_amount = XFLOATINT (aggressive);
15767
15768 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15769 if (pt_offset == 0 && float_amount > 0)
15770 pt_offset = 1;
15771 if (pt_offset && margin > 0)
15772 margin -= 1;
15773 }
15774 /* Compute how much to move the window start backward from
15775 point so that point will be displayed where the user
15776 wants it. */
15777 if (scrolling_up)
15778 {
15779 centering_position = it.last_visible_y;
15780 if (pt_offset)
15781 centering_position -= pt_offset;
15782 centering_position -=
15783 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15784 + WINDOW_HEADER_LINE_HEIGHT (w);
15785 /* Don't let point enter the scroll margin near top of
15786 the window. */
15787 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15788 centering_position = margin * FRAME_LINE_HEIGHT (f);
15789 }
15790 else
15791 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15792 }
15793 else
15794 /* Set the window start half the height of the window backward
15795 from point. */
15796 centering_position = window_box_height (w) / 2;
15797 }
15798 move_it_vertically_backward (&it, centering_position);
15799
15800 eassert (IT_CHARPOS (it) >= BEGV);
15801
15802 /* The function move_it_vertically_backward may move over more
15803 than the specified y-distance. If it->w is small, e.g. a
15804 mini-buffer window, we may end up in front of the window's
15805 display area. Start displaying at the start of the line
15806 containing PT in this case. */
15807 if (it.current_y <= 0)
15808 {
15809 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15810 move_it_vertically_backward (&it, 0);
15811 it.current_y = 0;
15812 }
15813
15814 it.current_x = it.hpos = 0;
15815
15816 /* Set the window start position here explicitly, to avoid an
15817 infinite loop in case the functions in window-scroll-functions
15818 get errors. */
15819 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15820
15821 /* Run scroll hooks. */
15822 startp = run_window_scroll_functions (window, it.current.pos);
15823
15824 /* Redisplay the window. */
15825 if (!current_matrix_up_to_date_p
15826 || windows_or_buffers_changed
15827 || cursor_type_changed
15828 /* Don't use try_window_reusing_current_matrix in this case
15829 because it can have changed the buffer. */
15830 || !NILP (Vwindow_scroll_functions)
15831 || !just_this_one_p
15832 || MINI_WINDOW_P (w)
15833 || !(used_current_matrix_p
15834 = try_window_reusing_current_matrix (w)))
15835 try_window (window, startp, 0);
15836
15837 /* If new fonts have been loaded (due to fontsets), give up. We
15838 have to start a new redisplay since we need to re-adjust glyph
15839 matrices. */
15840 if (fonts_changed_p)
15841 goto need_larger_matrices;
15842
15843 /* If cursor did not appear assume that the middle of the window is
15844 in the first line of the window. Do it again with the next line.
15845 (Imagine a window of height 100, displaying two lines of height
15846 60. Moving back 50 from it->last_visible_y will end in the first
15847 line.) */
15848 if (w->cursor.vpos < 0)
15849 {
15850 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15851 {
15852 clear_glyph_matrix (w->desired_matrix);
15853 move_it_by_lines (&it, 1);
15854 try_window (window, it.current.pos, 0);
15855 }
15856 else if (PT < IT_CHARPOS (it))
15857 {
15858 clear_glyph_matrix (w->desired_matrix);
15859 move_it_by_lines (&it, -1);
15860 try_window (window, it.current.pos, 0);
15861 }
15862 else
15863 {
15864 /* Not much we can do about it. */
15865 }
15866 }
15867
15868 /* Consider the following case: Window starts at BEGV, there is
15869 invisible, intangible text at BEGV, so that display starts at
15870 some point START > BEGV. It can happen that we are called with
15871 PT somewhere between BEGV and START. Try to handle that case. */
15872 if (w->cursor.vpos < 0)
15873 {
15874 struct glyph_row *row = w->current_matrix->rows;
15875 if (row->mode_line_p)
15876 ++row;
15877 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15878 }
15879
15880 if (!cursor_row_fully_visible_p (w, 0, 0))
15881 {
15882 /* If vscroll is enabled, disable it and try again. */
15883 if (w->vscroll)
15884 {
15885 w->vscroll = 0;
15886 clear_glyph_matrix (w->desired_matrix);
15887 goto recenter;
15888 }
15889
15890 /* Users who set scroll-conservatively to a large number want
15891 point just above/below the scroll margin. If we ended up
15892 with point's row partially visible, move the window start to
15893 make that row fully visible and out of the margin. */
15894 if (scroll_conservatively > SCROLL_LIMIT)
15895 {
15896 int margin =
15897 scroll_margin > 0
15898 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15899 : 0;
15900 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15901
15902 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15903 clear_glyph_matrix (w->desired_matrix);
15904 if (1 == try_window (window, it.current.pos,
15905 TRY_WINDOW_CHECK_MARGINS))
15906 goto done;
15907 }
15908
15909 /* If centering point failed to make the whole line visible,
15910 put point at the top instead. That has to make the whole line
15911 visible, if it can be done. */
15912 if (centering_position == 0)
15913 goto done;
15914
15915 clear_glyph_matrix (w->desired_matrix);
15916 centering_position = 0;
15917 goto recenter;
15918 }
15919
15920 done:
15921
15922 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15923 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15924 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15925
15926 /* Display the mode line, if we must. */
15927 if ((update_mode_line
15928 /* If window not full width, must redo its mode line
15929 if (a) the window to its side is being redone and
15930 (b) we do a frame-based redisplay. This is a consequence
15931 of how inverted lines are drawn in frame-based redisplay. */
15932 || (!just_this_one_p
15933 && !FRAME_WINDOW_P (f)
15934 && !WINDOW_FULL_WIDTH_P (w))
15935 /* Line number to display. */
15936 || w->base_line_pos > 0
15937 /* Column number is displayed and different from the one displayed. */
15938 || (w->column_number_displayed != -1
15939 && (w->column_number_displayed != current_column ())))
15940 /* This means that the window has a mode line. */
15941 && (WINDOW_WANTS_MODELINE_P (w)
15942 || WINDOW_WANTS_HEADER_LINE_P (w)))
15943 {
15944 display_mode_lines (w);
15945
15946 /* If mode line height has changed, arrange for a thorough
15947 immediate redisplay using the correct mode line height. */
15948 if (WINDOW_WANTS_MODELINE_P (w)
15949 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15950 {
15951 fonts_changed_p = 1;
15952 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15953 = DESIRED_MODE_LINE_HEIGHT (w);
15954 }
15955
15956 /* If header line height has changed, arrange for a thorough
15957 immediate redisplay using the correct header line height. */
15958 if (WINDOW_WANTS_HEADER_LINE_P (w)
15959 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15960 {
15961 fonts_changed_p = 1;
15962 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15963 = DESIRED_HEADER_LINE_HEIGHT (w);
15964 }
15965
15966 if (fonts_changed_p)
15967 goto need_larger_matrices;
15968 }
15969
15970 if (!line_number_displayed && w->base_line_pos != -1)
15971 {
15972 w->base_line_pos = 0;
15973 w->base_line_number = 0;
15974 }
15975
15976 finish_menu_bars:
15977
15978 /* When we reach a frame's selected window, redo the frame's menu bar. */
15979 if (update_mode_line
15980 && EQ (FRAME_SELECTED_WINDOW (f), window))
15981 {
15982 int redisplay_menu_p = 0;
15983
15984 if (FRAME_WINDOW_P (f))
15985 {
15986 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15987 || defined (HAVE_NS) || defined (USE_GTK)
15988 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15989 #else
15990 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15991 #endif
15992 }
15993 else
15994 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15995
15996 if (redisplay_menu_p)
15997 display_menu_bar (w);
15998
15999 #ifdef HAVE_WINDOW_SYSTEM
16000 if (FRAME_WINDOW_P (f))
16001 {
16002 #if defined (USE_GTK) || defined (HAVE_NS)
16003 if (FRAME_EXTERNAL_TOOL_BAR (f))
16004 redisplay_tool_bar (f);
16005 #else
16006 if (WINDOWP (f->tool_bar_window)
16007 && (FRAME_TOOL_BAR_LINES (f) > 0
16008 || !NILP (Vauto_resize_tool_bars))
16009 && redisplay_tool_bar (f))
16010 ignore_mouse_drag_p = 1;
16011 #endif
16012 }
16013 #endif
16014 }
16015
16016 #ifdef HAVE_WINDOW_SYSTEM
16017 if (FRAME_WINDOW_P (f)
16018 && update_window_fringes (w, (just_this_one_p
16019 || (!used_current_matrix_p && !overlay_arrow_seen)
16020 || w->pseudo_window_p)))
16021 {
16022 update_begin (f);
16023 block_input ();
16024 if (draw_window_fringes (w, 1))
16025 x_draw_vertical_border (w);
16026 unblock_input ();
16027 update_end (f);
16028 }
16029 #endif /* HAVE_WINDOW_SYSTEM */
16030
16031 /* We go to this label, with fonts_changed_p set,
16032 if it is necessary to try again using larger glyph matrices.
16033 We have to redeem the scroll bar even in this case,
16034 because the loop in redisplay_internal expects that. */
16035 need_larger_matrices:
16036 ;
16037 finish_scroll_bars:
16038
16039 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16040 {
16041 /* Set the thumb's position and size. */
16042 set_vertical_scroll_bar (w);
16043
16044 /* Note that we actually used the scroll bar attached to this
16045 window, so it shouldn't be deleted at the end of redisplay. */
16046 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16047 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16048 }
16049
16050 /* Restore current_buffer and value of point in it. The window
16051 update may have changed the buffer, so first make sure `opoint'
16052 is still valid (Bug#6177). */
16053 if (CHARPOS (opoint) < BEGV)
16054 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16055 else if (CHARPOS (opoint) > ZV)
16056 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16057 else
16058 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16059
16060 set_buffer_internal_1 (old);
16061 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16062 shorter. This can be caused by log truncation in *Messages*. */
16063 if (CHARPOS (lpoint) <= ZV)
16064 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16065
16066 unbind_to (count, Qnil);
16067 }
16068
16069
16070 /* Build the complete desired matrix of WINDOW with a window start
16071 buffer position POS.
16072
16073 Value is 1 if successful. It is zero if fonts were loaded during
16074 redisplay which makes re-adjusting glyph matrices necessary, and -1
16075 if point would appear in the scroll margins.
16076 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16077 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16078 set in FLAGS.) */
16079
16080 int
16081 try_window (Lisp_Object window, struct text_pos pos, int flags)
16082 {
16083 struct window *w = XWINDOW (window);
16084 struct it it;
16085 struct glyph_row *last_text_row = NULL;
16086 struct frame *f = XFRAME (w->frame);
16087
16088 /* Make POS the new window start. */
16089 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16090
16091 /* Mark cursor position as unknown. No overlay arrow seen. */
16092 w->cursor.vpos = -1;
16093 overlay_arrow_seen = 0;
16094
16095 /* Initialize iterator and info to start at POS. */
16096 start_display (&it, w, pos);
16097
16098 /* Display all lines of W. */
16099 while (it.current_y < it.last_visible_y)
16100 {
16101 if (display_line (&it))
16102 last_text_row = it.glyph_row - 1;
16103 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16104 return 0;
16105 }
16106
16107 /* Don't let the cursor end in the scroll margins. */
16108 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16109 && !MINI_WINDOW_P (w))
16110 {
16111 int this_scroll_margin;
16112
16113 if (scroll_margin > 0)
16114 {
16115 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16116 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16117 }
16118 else
16119 this_scroll_margin = 0;
16120
16121 if ((w->cursor.y >= 0 /* not vscrolled */
16122 && w->cursor.y < this_scroll_margin
16123 && CHARPOS (pos) > BEGV
16124 && IT_CHARPOS (it) < ZV)
16125 /* rms: considering make_cursor_line_fully_visible_p here
16126 seems to give wrong results. We don't want to recenter
16127 when the last line is partly visible, we want to allow
16128 that case to be handled in the usual way. */
16129 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16130 {
16131 w->cursor.vpos = -1;
16132 clear_glyph_matrix (w->desired_matrix);
16133 return -1;
16134 }
16135 }
16136
16137 /* If bottom moved off end of frame, change mode line percentage. */
16138 if (XFASTINT (w->window_end_pos) <= 0
16139 && Z != IT_CHARPOS (it))
16140 w->update_mode_line = 1;
16141
16142 /* Set window_end_pos to the offset of the last character displayed
16143 on the window from the end of current_buffer. Set
16144 window_end_vpos to its row number. */
16145 if (last_text_row)
16146 {
16147 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16148 w->window_end_bytepos
16149 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16150 wset_window_end_pos
16151 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16152 wset_window_end_vpos
16153 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16154 eassert
16155 (MATRIX_ROW (w->desired_matrix,
16156 XFASTINT (w->window_end_vpos))->displays_text_p);
16157 }
16158 else
16159 {
16160 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16161 wset_window_end_pos (w, make_number (Z - ZV));
16162 wset_window_end_vpos (w, make_number (0));
16163 }
16164
16165 /* But that is not valid info until redisplay finishes. */
16166 w->window_end_valid = 0;
16167 return 1;
16168 }
16169
16170
16171 \f
16172 /************************************************************************
16173 Window redisplay reusing current matrix when buffer has not changed
16174 ************************************************************************/
16175
16176 /* Try redisplay of window W showing an unchanged buffer with a
16177 different window start than the last time it was displayed by
16178 reusing its current matrix. Value is non-zero if successful.
16179 W->start is the new window start. */
16180
16181 static int
16182 try_window_reusing_current_matrix (struct window *w)
16183 {
16184 struct frame *f = XFRAME (w->frame);
16185 struct glyph_row *bottom_row;
16186 struct it it;
16187 struct run run;
16188 struct text_pos start, new_start;
16189 int nrows_scrolled, i;
16190 struct glyph_row *last_text_row;
16191 struct glyph_row *last_reused_text_row;
16192 struct glyph_row *start_row;
16193 int start_vpos, min_y, max_y;
16194
16195 #ifdef GLYPH_DEBUG
16196 if (inhibit_try_window_reusing)
16197 return 0;
16198 #endif
16199
16200 if (/* This function doesn't handle terminal frames. */
16201 !FRAME_WINDOW_P (f)
16202 /* Don't try to reuse the display if windows have been split
16203 or such. */
16204 || windows_or_buffers_changed
16205 || cursor_type_changed)
16206 return 0;
16207
16208 /* Can't do this if region may have changed. */
16209 if (0 <= markpos_of_region ()
16210 || w->region_showing
16211 || !NILP (Vshow_trailing_whitespace))
16212 return 0;
16213
16214 /* If top-line visibility has changed, give up. */
16215 if (WINDOW_WANTS_HEADER_LINE_P (w)
16216 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16217 return 0;
16218
16219 /* Give up if old or new display is scrolled vertically. We could
16220 make this function handle this, but right now it doesn't. */
16221 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16222 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16223 return 0;
16224
16225 /* The variable new_start now holds the new window start. The old
16226 start `start' can be determined from the current matrix. */
16227 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16228 start = start_row->minpos;
16229 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16230
16231 /* Clear the desired matrix for the display below. */
16232 clear_glyph_matrix (w->desired_matrix);
16233
16234 if (CHARPOS (new_start) <= CHARPOS (start))
16235 {
16236 /* Don't use this method if the display starts with an ellipsis
16237 displayed for invisible text. It's not easy to handle that case
16238 below, and it's certainly not worth the effort since this is
16239 not a frequent case. */
16240 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16241 return 0;
16242
16243 IF_DEBUG (debug_method_add (w, "twu1"));
16244
16245 /* Display up to a row that can be reused. The variable
16246 last_text_row is set to the last row displayed that displays
16247 text. Note that it.vpos == 0 if or if not there is a
16248 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16249 start_display (&it, w, new_start);
16250 w->cursor.vpos = -1;
16251 last_text_row = last_reused_text_row = NULL;
16252
16253 while (it.current_y < it.last_visible_y
16254 && !fonts_changed_p)
16255 {
16256 /* If we have reached into the characters in the START row,
16257 that means the line boundaries have changed. So we
16258 can't start copying with the row START. Maybe it will
16259 work to start copying with the following row. */
16260 while (IT_CHARPOS (it) > CHARPOS (start))
16261 {
16262 /* Advance to the next row as the "start". */
16263 start_row++;
16264 start = start_row->minpos;
16265 /* If there are no more rows to try, or just one, give up. */
16266 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16267 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16268 || CHARPOS (start) == ZV)
16269 {
16270 clear_glyph_matrix (w->desired_matrix);
16271 return 0;
16272 }
16273
16274 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16275 }
16276 /* If we have reached alignment, we can copy the rest of the
16277 rows. */
16278 if (IT_CHARPOS (it) == CHARPOS (start)
16279 /* Don't accept "alignment" inside a display vector,
16280 since start_row could have started in the middle of
16281 that same display vector (thus their character
16282 positions match), and we have no way of telling if
16283 that is the case. */
16284 && it.current.dpvec_index < 0)
16285 break;
16286
16287 if (display_line (&it))
16288 last_text_row = it.glyph_row - 1;
16289
16290 }
16291
16292 /* A value of current_y < last_visible_y means that we stopped
16293 at the previous window start, which in turn means that we
16294 have at least one reusable row. */
16295 if (it.current_y < it.last_visible_y)
16296 {
16297 struct glyph_row *row;
16298
16299 /* IT.vpos always starts from 0; it counts text lines. */
16300 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16301
16302 /* Find PT if not already found in the lines displayed. */
16303 if (w->cursor.vpos < 0)
16304 {
16305 int dy = it.current_y - start_row->y;
16306
16307 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16308 row = row_containing_pos (w, PT, row, NULL, dy);
16309 if (row)
16310 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16311 dy, nrows_scrolled);
16312 else
16313 {
16314 clear_glyph_matrix (w->desired_matrix);
16315 return 0;
16316 }
16317 }
16318
16319 /* Scroll the display. Do it before the current matrix is
16320 changed. The problem here is that update has not yet
16321 run, i.e. part of the current matrix is not up to date.
16322 scroll_run_hook will clear the cursor, and use the
16323 current matrix to get the height of the row the cursor is
16324 in. */
16325 run.current_y = start_row->y;
16326 run.desired_y = it.current_y;
16327 run.height = it.last_visible_y - it.current_y;
16328
16329 if (run.height > 0 && run.current_y != run.desired_y)
16330 {
16331 update_begin (f);
16332 FRAME_RIF (f)->update_window_begin_hook (w);
16333 FRAME_RIF (f)->clear_window_mouse_face (w);
16334 FRAME_RIF (f)->scroll_run_hook (w, &run);
16335 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16336 update_end (f);
16337 }
16338
16339 /* Shift current matrix down by nrows_scrolled lines. */
16340 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16341 rotate_matrix (w->current_matrix,
16342 start_vpos,
16343 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16344 nrows_scrolled);
16345
16346 /* Disable lines that must be updated. */
16347 for (i = 0; i < nrows_scrolled; ++i)
16348 (start_row + i)->enabled_p = 0;
16349
16350 /* Re-compute Y positions. */
16351 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16352 max_y = it.last_visible_y;
16353 for (row = start_row + nrows_scrolled;
16354 row < bottom_row;
16355 ++row)
16356 {
16357 row->y = it.current_y;
16358 row->visible_height = row->height;
16359
16360 if (row->y < min_y)
16361 row->visible_height -= min_y - row->y;
16362 if (row->y + row->height > max_y)
16363 row->visible_height -= row->y + row->height - max_y;
16364 if (row->fringe_bitmap_periodic_p)
16365 row->redraw_fringe_bitmaps_p = 1;
16366
16367 it.current_y += row->height;
16368
16369 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16370 last_reused_text_row = row;
16371 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16372 break;
16373 }
16374
16375 /* Disable lines in the current matrix which are now
16376 below the window. */
16377 for (++row; row < bottom_row; ++row)
16378 row->enabled_p = row->mode_line_p = 0;
16379 }
16380
16381 /* Update window_end_pos etc.; last_reused_text_row is the last
16382 reused row from the current matrix containing text, if any.
16383 The value of last_text_row is the last displayed line
16384 containing text. */
16385 if (last_reused_text_row)
16386 {
16387 w->window_end_bytepos
16388 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16389 wset_window_end_pos
16390 (w, make_number (Z
16391 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16392 wset_window_end_vpos
16393 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16394 w->current_matrix)));
16395 }
16396 else if (last_text_row)
16397 {
16398 w->window_end_bytepos
16399 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16400 wset_window_end_pos
16401 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16402 wset_window_end_vpos
16403 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16404 w->desired_matrix)));
16405 }
16406 else
16407 {
16408 /* This window must be completely empty. */
16409 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16410 wset_window_end_pos (w, make_number (Z - ZV));
16411 wset_window_end_vpos (w, make_number (0));
16412 }
16413 w->window_end_valid = 0;
16414
16415 /* Update hint: don't try scrolling again in update_window. */
16416 w->desired_matrix->no_scrolling_p = 1;
16417
16418 #ifdef GLYPH_DEBUG
16419 debug_method_add (w, "try_window_reusing_current_matrix 1");
16420 #endif
16421 return 1;
16422 }
16423 else if (CHARPOS (new_start) > CHARPOS (start))
16424 {
16425 struct glyph_row *pt_row, *row;
16426 struct glyph_row *first_reusable_row;
16427 struct glyph_row *first_row_to_display;
16428 int dy;
16429 int yb = window_text_bottom_y (w);
16430
16431 /* Find the row starting at new_start, if there is one. Don't
16432 reuse a partially visible line at the end. */
16433 first_reusable_row = start_row;
16434 while (first_reusable_row->enabled_p
16435 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16436 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16437 < CHARPOS (new_start)))
16438 ++first_reusable_row;
16439
16440 /* Give up if there is no row to reuse. */
16441 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16442 || !first_reusable_row->enabled_p
16443 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16444 != CHARPOS (new_start)))
16445 return 0;
16446
16447 /* We can reuse fully visible rows beginning with
16448 first_reusable_row to the end of the window. Set
16449 first_row_to_display to the first row that cannot be reused.
16450 Set pt_row to the row containing point, if there is any. */
16451 pt_row = NULL;
16452 for (first_row_to_display = first_reusable_row;
16453 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16454 ++first_row_to_display)
16455 {
16456 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16457 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16458 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16459 && first_row_to_display->ends_at_zv_p
16460 && pt_row == NULL)))
16461 pt_row = first_row_to_display;
16462 }
16463
16464 /* Start displaying at the start of first_row_to_display. */
16465 eassert (first_row_to_display->y < yb);
16466 init_to_row_start (&it, w, first_row_to_display);
16467
16468 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16469 - start_vpos);
16470 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16471 - nrows_scrolled);
16472 it.current_y = (first_row_to_display->y - first_reusable_row->y
16473 + WINDOW_HEADER_LINE_HEIGHT (w));
16474
16475 /* Display lines beginning with first_row_to_display in the
16476 desired matrix. Set last_text_row to the last row displayed
16477 that displays text. */
16478 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16479 if (pt_row == NULL)
16480 w->cursor.vpos = -1;
16481 last_text_row = NULL;
16482 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16483 if (display_line (&it))
16484 last_text_row = it.glyph_row - 1;
16485
16486 /* If point is in a reused row, adjust y and vpos of the cursor
16487 position. */
16488 if (pt_row)
16489 {
16490 w->cursor.vpos -= nrows_scrolled;
16491 w->cursor.y -= first_reusable_row->y - start_row->y;
16492 }
16493
16494 /* Give up if point isn't in a row displayed or reused. (This
16495 also handles the case where w->cursor.vpos < nrows_scrolled
16496 after the calls to display_line, which can happen with scroll
16497 margins. See bug#1295.) */
16498 if (w->cursor.vpos < 0)
16499 {
16500 clear_glyph_matrix (w->desired_matrix);
16501 return 0;
16502 }
16503
16504 /* Scroll the display. */
16505 run.current_y = first_reusable_row->y;
16506 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16507 run.height = it.last_visible_y - run.current_y;
16508 dy = run.current_y - run.desired_y;
16509
16510 if (run.height)
16511 {
16512 update_begin (f);
16513 FRAME_RIF (f)->update_window_begin_hook (w);
16514 FRAME_RIF (f)->clear_window_mouse_face (w);
16515 FRAME_RIF (f)->scroll_run_hook (w, &run);
16516 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16517 update_end (f);
16518 }
16519
16520 /* Adjust Y positions of reused rows. */
16521 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16522 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16523 max_y = it.last_visible_y;
16524 for (row = first_reusable_row; row < first_row_to_display; ++row)
16525 {
16526 row->y -= dy;
16527 row->visible_height = row->height;
16528 if (row->y < min_y)
16529 row->visible_height -= min_y - row->y;
16530 if (row->y + row->height > max_y)
16531 row->visible_height -= row->y + row->height - max_y;
16532 if (row->fringe_bitmap_periodic_p)
16533 row->redraw_fringe_bitmaps_p = 1;
16534 }
16535
16536 /* Scroll the current matrix. */
16537 eassert (nrows_scrolled > 0);
16538 rotate_matrix (w->current_matrix,
16539 start_vpos,
16540 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16541 -nrows_scrolled);
16542
16543 /* Disable rows not reused. */
16544 for (row -= nrows_scrolled; row < bottom_row; ++row)
16545 row->enabled_p = 0;
16546
16547 /* Point may have moved to a different line, so we cannot assume that
16548 the previous cursor position is valid; locate the correct row. */
16549 if (pt_row)
16550 {
16551 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16552 row < bottom_row
16553 && PT >= MATRIX_ROW_END_CHARPOS (row)
16554 && !row->ends_at_zv_p;
16555 row++)
16556 {
16557 w->cursor.vpos++;
16558 w->cursor.y = row->y;
16559 }
16560 if (row < bottom_row)
16561 {
16562 /* Can't simply scan the row for point with
16563 bidi-reordered glyph rows. Let set_cursor_from_row
16564 figure out where to put the cursor, and if it fails,
16565 give up. */
16566 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16567 {
16568 if (!set_cursor_from_row (w, row, w->current_matrix,
16569 0, 0, 0, 0))
16570 {
16571 clear_glyph_matrix (w->desired_matrix);
16572 return 0;
16573 }
16574 }
16575 else
16576 {
16577 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16578 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16579
16580 for (; glyph < end
16581 && (!BUFFERP (glyph->object)
16582 || glyph->charpos < PT);
16583 glyph++)
16584 {
16585 w->cursor.hpos++;
16586 w->cursor.x += glyph->pixel_width;
16587 }
16588 }
16589 }
16590 }
16591
16592 /* Adjust window end. A null value of last_text_row means that
16593 the window end is in reused rows which in turn means that
16594 only its vpos can have changed. */
16595 if (last_text_row)
16596 {
16597 w->window_end_bytepos
16598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16599 wset_window_end_pos
16600 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16601 wset_window_end_vpos
16602 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16603 w->desired_matrix)));
16604 }
16605 else
16606 {
16607 wset_window_end_vpos
16608 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16609 }
16610
16611 w->window_end_valid = 0;
16612 w->desired_matrix->no_scrolling_p = 1;
16613
16614 #ifdef GLYPH_DEBUG
16615 debug_method_add (w, "try_window_reusing_current_matrix 2");
16616 #endif
16617 return 1;
16618 }
16619
16620 return 0;
16621 }
16622
16623
16624 \f
16625 /************************************************************************
16626 Window redisplay reusing current matrix when buffer has changed
16627 ************************************************************************/
16628
16629 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16630 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16631 ptrdiff_t *, ptrdiff_t *);
16632 static struct glyph_row *
16633 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16634 struct glyph_row *);
16635
16636
16637 /* Return the last row in MATRIX displaying text. If row START is
16638 non-null, start searching with that row. IT gives the dimensions
16639 of the display. Value is null if matrix is empty; otherwise it is
16640 a pointer to the row found. */
16641
16642 static struct glyph_row *
16643 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16644 struct glyph_row *start)
16645 {
16646 struct glyph_row *row, *row_found;
16647
16648 /* Set row_found to the last row in IT->w's current matrix
16649 displaying text. The loop looks funny but think of partially
16650 visible lines. */
16651 row_found = NULL;
16652 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16653 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16654 {
16655 eassert (row->enabled_p);
16656 row_found = row;
16657 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16658 break;
16659 ++row;
16660 }
16661
16662 return row_found;
16663 }
16664
16665
16666 /* Return the last row in the current matrix of W that is not affected
16667 by changes at the start of current_buffer that occurred since W's
16668 current matrix was built. Value is null if no such row exists.
16669
16670 BEG_UNCHANGED us the number of characters unchanged at the start of
16671 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16672 first changed character in current_buffer. Characters at positions <
16673 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16674 when the current matrix was built. */
16675
16676 static struct glyph_row *
16677 find_last_unchanged_at_beg_row (struct window *w)
16678 {
16679 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16680 struct glyph_row *row;
16681 struct glyph_row *row_found = NULL;
16682 int yb = window_text_bottom_y (w);
16683
16684 /* Find the last row displaying unchanged text. */
16685 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16686 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16687 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16688 ++row)
16689 {
16690 if (/* If row ends before first_changed_pos, it is unchanged,
16691 except in some case. */
16692 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16693 /* When row ends in ZV and we write at ZV it is not
16694 unchanged. */
16695 && !row->ends_at_zv_p
16696 /* When first_changed_pos is the end of a continued line,
16697 row is not unchanged because it may be no longer
16698 continued. */
16699 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16700 && (row->continued_p
16701 || row->exact_window_width_line_p))
16702 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16703 needs to be recomputed, so don't consider this row as
16704 unchanged. This happens when the last line was
16705 bidi-reordered and was killed immediately before this
16706 redisplay cycle. In that case, ROW->end stores the
16707 buffer position of the first visual-order character of
16708 the killed text, which is now beyond ZV. */
16709 && CHARPOS (row->end.pos) <= ZV)
16710 row_found = row;
16711
16712 /* Stop if last visible row. */
16713 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16714 break;
16715 }
16716
16717 return row_found;
16718 }
16719
16720
16721 /* Find the first glyph row in the current matrix of W that is not
16722 affected by changes at the end of current_buffer since the
16723 time W's current matrix was built.
16724
16725 Return in *DELTA the number of chars by which buffer positions in
16726 unchanged text at the end of current_buffer must be adjusted.
16727
16728 Return in *DELTA_BYTES the corresponding number of bytes.
16729
16730 Value is null if no such row exists, i.e. all rows are affected by
16731 changes. */
16732
16733 static struct glyph_row *
16734 find_first_unchanged_at_end_row (struct window *w,
16735 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16736 {
16737 struct glyph_row *row;
16738 struct glyph_row *row_found = NULL;
16739
16740 *delta = *delta_bytes = 0;
16741
16742 /* Display must not have been paused, otherwise the current matrix
16743 is not up to date. */
16744 eassert (w->window_end_valid);
16745
16746 /* A value of window_end_pos >= END_UNCHANGED means that the window
16747 end is in the range of changed text. If so, there is no
16748 unchanged row at the end of W's current matrix. */
16749 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16750 return NULL;
16751
16752 /* Set row to the last row in W's current matrix displaying text. */
16753 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16754
16755 /* If matrix is entirely empty, no unchanged row exists. */
16756 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16757 {
16758 /* The value of row is the last glyph row in the matrix having a
16759 meaningful buffer position in it. The end position of row
16760 corresponds to window_end_pos. This allows us to translate
16761 buffer positions in the current matrix to current buffer
16762 positions for characters not in changed text. */
16763 ptrdiff_t Z_old =
16764 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16765 ptrdiff_t Z_BYTE_old =
16766 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16767 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16768 struct glyph_row *first_text_row
16769 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16770
16771 *delta = Z - Z_old;
16772 *delta_bytes = Z_BYTE - Z_BYTE_old;
16773
16774 /* Set last_unchanged_pos to the buffer position of the last
16775 character in the buffer that has not been changed. Z is the
16776 index + 1 of the last character in current_buffer, i.e. by
16777 subtracting END_UNCHANGED we get the index of the last
16778 unchanged character, and we have to add BEG to get its buffer
16779 position. */
16780 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16781 last_unchanged_pos_old = last_unchanged_pos - *delta;
16782
16783 /* Search backward from ROW for a row displaying a line that
16784 starts at a minimum position >= last_unchanged_pos_old. */
16785 for (; row > first_text_row; --row)
16786 {
16787 /* This used to abort, but it can happen.
16788 It is ok to just stop the search instead here. KFS. */
16789 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16790 break;
16791
16792 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16793 row_found = row;
16794 }
16795 }
16796
16797 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16798
16799 return row_found;
16800 }
16801
16802
16803 /* Make sure that glyph rows in the current matrix of window W
16804 reference the same glyph memory as corresponding rows in the
16805 frame's frame matrix. This function is called after scrolling W's
16806 current matrix on a terminal frame in try_window_id and
16807 try_window_reusing_current_matrix. */
16808
16809 static void
16810 sync_frame_with_window_matrix_rows (struct window *w)
16811 {
16812 struct frame *f = XFRAME (w->frame);
16813 struct glyph_row *window_row, *window_row_end, *frame_row;
16814
16815 /* Preconditions: W must be a leaf window and full-width. Its frame
16816 must have a frame matrix. */
16817 eassert (NILP (w->hchild) && NILP (w->vchild));
16818 eassert (WINDOW_FULL_WIDTH_P (w));
16819 eassert (!FRAME_WINDOW_P (f));
16820
16821 /* If W is a full-width window, glyph pointers in W's current matrix
16822 have, by definition, to be the same as glyph pointers in the
16823 corresponding frame matrix. Note that frame matrices have no
16824 marginal areas (see build_frame_matrix). */
16825 window_row = w->current_matrix->rows;
16826 window_row_end = window_row + w->current_matrix->nrows;
16827 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16828 while (window_row < window_row_end)
16829 {
16830 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16831 struct glyph *end = window_row->glyphs[LAST_AREA];
16832
16833 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16834 frame_row->glyphs[TEXT_AREA] = start;
16835 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16836 frame_row->glyphs[LAST_AREA] = end;
16837
16838 /* Disable frame rows whose corresponding window rows have
16839 been disabled in try_window_id. */
16840 if (!window_row->enabled_p)
16841 frame_row->enabled_p = 0;
16842
16843 ++window_row, ++frame_row;
16844 }
16845 }
16846
16847
16848 /* Find the glyph row in window W containing CHARPOS. Consider all
16849 rows between START and END (not inclusive). END null means search
16850 all rows to the end of the display area of W. Value is the row
16851 containing CHARPOS or null. */
16852
16853 struct glyph_row *
16854 row_containing_pos (struct window *w, ptrdiff_t charpos,
16855 struct glyph_row *start, struct glyph_row *end, int dy)
16856 {
16857 struct glyph_row *row = start;
16858 struct glyph_row *best_row = NULL;
16859 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16860 int last_y;
16861
16862 /* If we happen to start on a header-line, skip that. */
16863 if (row->mode_line_p)
16864 ++row;
16865
16866 if ((end && row >= end) || !row->enabled_p)
16867 return NULL;
16868
16869 last_y = window_text_bottom_y (w) - dy;
16870
16871 while (1)
16872 {
16873 /* Give up if we have gone too far. */
16874 if (end && row >= end)
16875 return NULL;
16876 /* This formerly returned if they were equal.
16877 I think that both quantities are of a "last plus one" type;
16878 if so, when they are equal, the row is within the screen. -- rms. */
16879 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16880 return NULL;
16881
16882 /* If it is in this row, return this row. */
16883 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16884 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16885 /* The end position of a row equals the start
16886 position of the next row. If CHARPOS is there, we
16887 would rather display it in the next line, except
16888 when this line ends in ZV. */
16889 && !row->ends_at_zv_p
16890 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16891 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16892 {
16893 struct glyph *g;
16894
16895 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16896 || (!best_row && !row->continued_p))
16897 return row;
16898 /* In bidi-reordered rows, there could be several rows
16899 occluding point, all of them belonging to the same
16900 continued line. We need to find the row which fits
16901 CHARPOS the best. */
16902 for (g = row->glyphs[TEXT_AREA];
16903 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16904 g++)
16905 {
16906 if (!STRINGP (g->object))
16907 {
16908 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16909 {
16910 mindif = eabs (g->charpos - charpos);
16911 best_row = row;
16912 /* Exact match always wins. */
16913 if (mindif == 0)
16914 return best_row;
16915 }
16916 }
16917 }
16918 }
16919 else if (best_row && !row->continued_p)
16920 return best_row;
16921 ++row;
16922 }
16923 }
16924
16925
16926 /* Try to redisplay window W by reusing its existing display. W's
16927 current matrix must be up to date when this function is called,
16928 i.e. window_end_valid must be nonzero.
16929
16930 Value is
16931
16932 1 if display has been updated
16933 0 if otherwise unsuccessful
16934 -1 if redisplay with same window start is known not to succeed
16935
16936 The following steps are performed:
16937
16938 1. Find the last row in the current matrix of W that is not
16939 affected by changes at the start of current_buffer. If no such row
16940 is found, give up.
16941
16942 2. Find the first row in W's current matrix that is not affected by
16943 changes at the end of current_buffer. Maybe there is no such row.
16944
16945 3. Display lines beginning with the row + 1 found in step 1 to the
16946 row found in step 2 or, if step 2 didn't find a row, to the end of
16947 the window.
16948
16949 4. If cursor is not known to appear on the window, give up.
16950
16951 5. If display stopped at the row found in step 2, scroll the
16952 display and current matrix as needed.
16953
16954 6. Maybe display some lines at the end of W, if we must. This can
16955 happen under various circumstances, like a partially visible line
16956 becoming fully visible, or because newly displayed lines are displayed
16957 in smaller font sizes.
16958
16959 7. Update W's window end information. */
16960
16961 static int
16962 try_window_id (struct window *w)
16963 {
16964 struct frame *f = XFRAME (w->frame);
16965 struct glyph_matrix *current_matrix = w->current_matrix;
16966 struct glyph_matrix *desired_matrix = w->desired_matrix;
16967 struct glyph_row *last_unchanged_at_beg_row;
16968 struct glyph_row *first_unchanged_at_end_row;
16969 struct glyph_row *row;
16970 struct glyph_row *bottom_row;
16971 int bottom_vpos;
16972 struct it it;
16973 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16974 int dvpos, dy;
16975 struct text_pos start_pos;
16976 struct run run;
16977 int first_unchanged_at_end_vpos = 0;
16978 struct glyph_row *last_text_row, *last_text_row_at_end;
16979 struct text_pos start;
16980 ptrdiff_t first_changed_charpos, last_changed_charpos;
16981
16982 #ifdef GLYPH_DEBUG
16983 if (inhibit_try_window_id)
16984 return 0;
16985 #endif
16986
16987 /* This is handy for debugging. */
16988 #if 0
16989 #define GIVE_UP(X) \
16990 do { \
16991 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16992 return 0; \
16993 } while (0)
16994 #else
16995 #define GIVE_UP(X) return 0
16996 #endif
16997
16998 SET_TEXT_POS_FROM_MARKER (start, w->start);
16999
17000 /* Don't use this for mini-windows because these can show
17001 messages and mini-buffers, and we don't handle that here. */
17002 if (MINI_WINDOW_P (w))
17003 GIVE_UP (1);
17004
17005 /* This flag is used to prevent redisplay optimizations. */
17006 if (windows_or_buffers_changed || cursor_type_changed)
17007 GIVE_UP (2);
17008
17009 /* Verify that narrowing has not changed.
17010 Also verify that we were not told to prevent redisplay optimizations.
17011 It would be nice to further
17012 reduce the number of cases where this prevents try_window_id. */
17013 if (current_buffer->clip_changed
17014 || current_buffer->prevent_redisplay_optimizations_p)
17015 GIVE_UP (3);
17016
17017 /* Window must either use window-based redisplay or be full width. */
17018 if (!FRAME_WINDOW_P (f)
17019 && (!FRAME_LINE_INS_DEL_OK (f)
17020 || !WINDOW_FULL_WIDTH_P (w)))
17021 GIVE_UP (4);
17022
17023 /* Give up if point is known NOT to appear in W. */
17024 if (PT < CHARPOS (start))
17025 GIVE_UP (5);
17026
17027 /* Another way to prevent redisplay optimizations. */
17028 if (w->last_modified == 0)
17029 GIVE_UP (6);
17030
17031 /* Verify that window is not hscrolled. */
17032 if (w->hscroll != 0)
17033 GIVE_UP (7);
17034
17035 /* Verify that display wasn't paused. */
17036 if (!w->window_end_valid)
17037 GIVE_UP (8);
17038
17039 /* Can't use this if highlighting a region because a cursor movement
17040 will do more than just set the cursor. */
17041 if (0 <= markpos_of_region ())
17042 GIVE_UP (9);
17043
17044 /* Likewise if highlighting trailing whitespace. */
17045 if (!NILP (Vshow_trailing_whitespace))
17046 GIVE_UP (11);
17047
17048 /* Likewise if showing a region. */
17049 if (w->region_showing)
17050 GIVE_UP (10);
17051
17052 /* Can't use this if overlay arrow position and/or string have
17053 changed. */
17054 if (overlay_arrows_changed_p ())
17055 GIVE_UP (12);
17056
17057 /* When word-wrap is on, adding a space to the first word of a
17058 wrapped line can change the wrap position, altering the line
17059 above it. It might be worthwhile to handle this more
17060 intelligently, but for now just redisplay from scratch. */
17061 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17062 GIVE_UP (21);
17063
17064 /* Under bidi reordering, adding or deleting a character in the
17065 beginning of a paragraph, before the first strong directional
17066 character, can change the base direction of the paragraph (unless
17067 the buffer specifies a fixed paragraph direction), which will
17068 require to redisplay the whole paragraph. It might be worthwhile
17069 to find the paragraph limits and widen the range of redisplayed
17070 lines to that, but for now just give up this optimization and
17071 redisplay from scratch. */
17072 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17073 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17074 GIVE_UP (22);
17075
17076 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17077 only if buffer has really changed. The reason is that the gap is
17078 initially at Z for freshly visited files. The code below would
17079 set end_unchanged to 0 in that case. */
17080 if (MODIFF > SAVE_MODIFF
17081 /* This seems to happen sometimes after saving a buffer. */
17082 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17083 {
17084 if (GPT - BEG < BEG_UNCHANGED)
17085 BEG_UNCHANGED = GPT - BEG;
17086 if (Z - GPT < END_UNCHANGED)
17087 END_UNCHANGED = Z - GPT;
17088 }
17089
17090 /* The position of the first and last character that has been changed. */
17091 first_changed_charpos = BEG + BEG_UNCHANGED;
17092 last_changed_charpos = Z - END_UNCHANGED;
17093
17094 /* If window starts after a line end, and the last change is in
17095 front of that newline, then changes don't affect the display.
17096 This case happens with stealth-fontification. Note that although
17097 the display is unchanged, glyph positions in the matrix have to
17098 be adjusted, of course. */
17099 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17100 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17101 && ((last_changed_charpos < CHARPOS (start)
17102 && CHARPOS (start) == BEGV)
17103 || (last_changed_charpos < CHARPOS (start) - 1
17104 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17105 {
17106 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17107 struct glyph_row *r0;
17108
17109 /* Compute how many chars/bytes have been added to or removed
17110 from the buffer. */
17111 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17112 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17113 Z_delta = Z - Z_old;
17114 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17115
17116 /* Give up if PT is not in the window. Note that it already has
17117 been checked at the start of try_window_id that PT is not in
17118 front of the window start. */
17119 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17120 GIVE_UP (13);
17121
17122 /* If window start is unchanged, we can reuse the whole matrix
17123 as is, after adjusting glyph positions. No need to compute
17124 the window end again, since its offset from Z hasn't changed. */
17125 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17126 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17127 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17128 /* PT must not be in a partially visible line. */
17129 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17130 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17131 {
17132 /* Adjust positions in the glyph matrix. */
17133 if (Z_delta || Z_delta_bytes)
17134 {
17135 struct glyph_row *r1
17136 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17137 increment_matrix_positions (w->current_matrix,
17138 MATRIX_ROW_VPOS (r0, current_matrix),
17139 MATRIX_ROW_VPOS (r1, current_matrix),
17140 Z_delta, Z_delta_bytes);
17141 }
17142
17143 /* Set the cursor. */
17144 row = row_containing_pos (w, PT, r0, NULL, 0);
17145 if (row)
17146 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17147 else
17148 emacs_abort ();
17149 return 1;
17150 }
17151 }
17152
17153 /* Handle the case that changes are all below what is displayed in
17154 the window, and that PT is in the window. This shortcut cannot
17155 be taken if ZV is visible in the window, and text has been added
17156 there that is visible in the window. */
17157 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17158 /* ZV is not visible in the window, or there are no
17159 changes at ZV, actually. */
17160 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17161 || first_changed_charpos == last_changed_charpos))
17162 {
17163 struct glyph_row *r0;
17164
17165 /* Give up if PT is not in the window. Note that it already has
17166 been checked at the start of try_window_id that PT is not in
17167 front of the window start. */
17168 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17169 GIVE_UP (14);
17170
17171 /* If window start is unchanged, we can reuse the whole matrix
17172 as is, without changing glyph positions since no text has
17173 been added/removed in front of the window end. */
17174 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17175 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17176 /* PT must not be in a partially visible line. */
17177 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17178 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17179 {
17180 /* We have to compute the window end anew since text
17181 could have been added/removed after it. */
17182 wset_window_end_pos
17183 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17184 w->window_end_bytepos
17185 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17186
17187 /* Set the cursor. */
17188 row = row_containing_pos (w, PT, r0, NULL, 0);
17189 if (row)
17190 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17191 else
17192 emacs_abort ();
17193 return 2;
17194 }
17195 }
17196
17197 /* Give up if window start is in the changed area.
17198
17199 The condition used to read
17200
17201 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17202
17203 but why that was tested escapes me at the moment. */
17204 if (CHARPOS (start) >= first_changed_charpos
17205 && CHARPOS (start) <= last_changed_charpos)
17206 GIVE_UP (15);
17207
17208 /* Check that window start agrees with the start of the first glyph
17209 row in its current matrix. Check this after we know the window
17210 start is not in changed text, otherwise positions would not be
17211 comparable. */
17212 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17213 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17214 GIVE_UP (16);
17215
17216 /* Give up if the window ends in strings. Overlay strings
17217 at the end are difficult to handle, so don't try. */
17218 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17219 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17220 GIVE_UP (20);
17221
17222 /* Compute the position at which we have to start displaying new
17223 lines. Some of the lines at the top of the window might be
17224 reusable because they are not displaying changed text. Find the
17225 last row in W's current matrix not affected by changes at the
17226 start of current_buffer. Value is null if changes start in the
17227 first line of window. */
17228 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17229 if (last_unchanged_at_beg_row)
17230 {
17231 /* Avoid starting to display in the middle of a character, a TAB
17232 for instance. This is easier than to set up the iterator
17233 exactly, and it's not a frequent case, so the additional
17234 effort wouldn't really pay off. */
17235 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17236 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17237 && last_unchanged_at_beg_row > w->current_matrix->rows)
17238 --last_unchanged_at_beg_row;
17239
17240 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17241 GIVE_UP (17);
17242
17243 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17244 GIVE_UP (18);
17245 start_pos = it.current.pos;
17246
17247 /* Start displaying new lines in the desired matrix at the same
17248 vpos we would use in the current matrix, i.e. below
17249 last_unchanged_at_beg_row. */
17250 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17251 current_matrix);
17252 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17253 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17254
17255 eassert (it.hpos == 0 && it.current_x == 0);
17256 }
17257 else
17258 {
17259 /* There are no reusable lines at the start of the window.
17260 Start displaying in the first text line. */
17261 start_display (&it, w, start);
17262 it.vpos = it.first_vpos;
17263 start_pos = it.current.pos;
17264 }
17265
17266 /* Find the first row that is not affected by changes at the end of
17267 the buffer. Value will be null if there is no unchanged row, in
17268 which case we must redisplay to the end of the window. delta
17269 will be set to the value by which buffer positions beginning with
17270 first_unchanged_at_end_row have to be adjusted due to text
17271 changes. */
17272 first_unchanged_at_end_row
17273 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17274 IF_DEBUG (debug_delta = delta);
17275 IF_DEBUG (debug_delta_bytes = delta_bytes);
17276
17277 /* Set stop_pos to the buffer position up to which we will have to
17278 display new lines. If first_unchanged_at_end_row != NULL, this
17279 is the buffer position of the start of the line displayed in that
17280 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17281 that we don't stop at a buffer position. */
17282 stop_pos = 0;
17283 if (first_unchanged_at_end_row)
17284 {
17285 eassert (last_unchanged_at_beg_row == NULL
17286 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17287
17288 /* If this is a continuation line, move forward to the next one
17289 that isn't. Changes in lines above affect this line.
17290 Caution: this may move first_unchanged_at_end_row to a row
17291 not displaying text. */
17292 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17293 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17294 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17295 < it.last_visible_y))
17296 ++first_unchanged_at_end_row;
17297
17298 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17299 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17300 >= it.last_visible_y))
17301 first_unchanged_at_end_row = NULL;
17302 else
17303 {
17304 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17305 + delta);
17306 first_unchanged_at_end_vpos
17307 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17308 eassert (stop_pos >= Z - END_UNCHANGED);
17309 }
17310 }
17311 else if (last_unchanged_at_beg_row == NULL)
17312 GIVE_UP (19);
17313
17314
17315 #ifdef GLYPH_DEBUG
17316
17317 /* Either there is no unchanged row at the end, or the one we have
17318 now displays text. This is a necessary condition for the window
17319 end pos calculation at the end of this function. */
17320 eassert (first_unchanged_at_end_row == NULL
17321 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17322
17323 debug_last_unchanged_at_beg_vpos
17324 = (last_unchanged_at_beg_row
17325 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17326 : -1);
17327 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17328
17329 #endif /* GLYPH_DEBUG */
17330
17331
17332 /* Display new lines. Set last_text_row to the last new line
17333 displayed which has text on it, i.e. might end up as being the
17334 line where the window_end_vpos is. */
17335 w->cursor.vpos = -1;
17336 last_text_row = NULL;
17337 overlay_arrow_seen = 0;
17338 while (it.current_y < it.last_visible_y
17339 && !fonts_changed_p
17340 && (first_unchanged_at_end_row == NULL
17341 || IT_CHARPOS (it) < stop_pos))
17342 {
17343 if (display_line (&it))
17344 last_text_row = it.glyph_row - 1;
17345 }
17346
17347 if (fonts_changed_p)
17348 return -1;
17349
17350
17351 /* Compute differences in buffer positions, y-positions etc. for
17352 lines reused at the bottom of the window. Compute what we can
17353 scroll. */
17354 if (first_unchanged_at_end_row
17355 /* No lines reused because we displayed everything up to the
17356 bottom of the window. */
17357 && it.current_y < it.last_visible_y)
17358 {
17359 dvpos = (it.vpos
17360 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17361 current_matrix));
17362 dy = it.current_y - first_unchanged_at_end_row->y;
17363 run.current_y = first_unchanged_at_end_row->y;
17364 run.desired_y = run.current_y + dy;
17365 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17366 }
17367 else
17368 {
17369 delta = delta_bytes = dvpos = dy
17370 = run.current_y = run.desired_y = run.height = 0;
17371 first_unchanged_at_end_row = NULL;
17372 }
17373 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17374
17375
17376 /* Find the cursor if not already found. We have to decide whether
17377 PT will appear on this window (it sometimes doesn't, but this is
17378 not a very frequent case.) This decision has to be made before
17379 the current matrix is altered. A value of cursor.vpos < 0 means
17380 that PT is either in one of the lines beginning at
17381 first_unchanged_at_end_row or below the window. Don't care for
17382 lines that might be displayed later at the window end; as
17383 mentioned, this is not a frequent case. */
17384 if (w->cursor.vpos < 0)
17385 {
17386 /* Cursor in unchanged rows at the top? */
17387 if (PT < CHARPOS (start_pos)
17388 && last_unchanged_at_beg_row)
17389 {
17390 row = row_containing_pos (w, PT,
17391 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17392 last_unchanged_at_beg_row + 1, 0);
17393 if (row)
17394 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17395 }
17396
17397 /* Start from first_unchanged_at_end_row looking for PT. */
17398 else if (first_unchanged_at_end_row)
17399 {
17400 row = row_containing_pos (w, PT - delta,
17401 first_unchanged_at_end_row, NULL, 0);
17402 if (row)
17403 set_cursor_from_row (w, row, w->current_matrix, delta,
17404 delta_bytes, dy, dvpos);
17405 }
17406
17407 /* Give up if cursor was not found. */
17408 if (w->cursor.vpos < 0)
17409 {
17410 clear_glyph_matrix (w->desired_matrix);
17411 return -1;
17412 }
17413 }
17414
17415 /* Don't let the cursor end in the scroll margins. */
17416 {
17417 int this_scroll_margin, cursor_height;
17418
17419 this_scroll_margin =
17420 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17421 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17422 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17423
17424 if ((w->cursor.y < this_scroll_margin
17425 && CHARPOS (start) > BEGV)
17426 /* Old redisplay didn't take scroll margin into account at the bottom,
17427 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17428 || (w->cursor.y + (make_cursor_line_fully_visible_p
17429 ? cursor_height + this_scroll_margin
17430 : 1)) > it.last_visible_y)
17431 {
17432 w->cursor.vpos = -1;
17433 clear_glyph_matrix (w->desired_matrix);
17434 return -1;
17435 }
17436 }
17437
17438 /* Scroll the display. Do it before changing the current matrix so
17439 that xterm.c doesn't get confused about where the cursor glyph is
17440 found. */
17441 if (dy && run.height)
17442 {
17443 update_begin (f);
17444
17445 if (FRAME_WINDOW_P (f))
17446 {
17447 FRAME_RIF (f)->update_window_begin_hook (w);
17448 FRAME_RIF (f)->clear_window_mouse_face (w);
17449 FRAME_RIF (f)->scroll_run_hook (w, &run);
17450 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17451 }
17452 else
17453 {
17454 /* Terminal frame. In this case, dvpos gives the number of
17455 lines to scroll by; dvpos < 0 means scroll up. */
17456 int from_vpos
17457 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17458 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17459 int end = (WINDOW_TOP_EDGE_LINE (w)
17460 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17461 + window_internal_height (w));
17462
17463 #if defined (HAVE_GPM) || defined (MSDOS)
17464 x_clear_window_mouse_face (w);
17465 #endif
17466 /* Perform the operation on the screen. */
17467 if (dvpos > 0)
17468 {
17469 /* Scroll last_unchanged_at_beg_row to the end of the
17470 window down dvpos lines. */
17471 set_terminal_window (f, end);
17472
17473 /* On dumb terminals delete dvpos lines at the end
17474 before inserting dvpos empty lines. */
17475 if (!FRAME_SCROLL_REGION_OK (f))
17476 ins_del_lines (f, end - dvpos, -dvpos);
17477
17478 /* Insert dvpos empty lines in front of
17479 last_unchanged_at_beg_row. */
17480 ins_del_lines (f, from, dvpos);
17481 }
17482 else if (dvpos < 0)
17483 {
17484 /* Scroll up last_unchanged_at_beg_vpos to the end of
17485 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17486 set_terminal_window (f, end);
17487
17488 /* Delete dvpos lines in front of
17489 last_unchanged_at_beg_vpos. ins_del_lines will set
17490 the cursor to the given vpos and emit |dvpos| delete
17491 line sequences. */
17492 ins_del_lines (f, from + dvpos, dvpos);
17493
17494 /* On a dumb terminal insert dvpos empty lines at the
17495 end. */
17496 if (!FRAME_SCROLL_REGION_OK (f))
17497 ins_del_lines (f, end + dvpos, -dvpos);
17498 }
17499
17500 set_terminal_window (f, 0);
17501 }
17502
17503 update_end (f);
17504 }
17505
17506 /* Shift reused rows of the current matrix to the right position.
17507 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17508 text. */
17509 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17510 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17511 if (dvpos < 0)
17512 {
17513 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17514 bottom_vpos, dvpos);
17515 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17516 bottom_vpos);
17517 }
17518 else if (dvpos > 0)
17519 {
17520 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17521 bottom_vpos, dvpos);
17522 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17523 first_unchanged_at_end_vpos + dvpos);
17524 }
17525
17526 /* For frame-based redisplay, make sure that current frame and window
17527 matrix are in sync with respect to glyph memory. */
17528 if (!FRAME_WINDOW_P (f))
17529 sync_frame_with_window_matrix_rows (w);
17530
17531 /* Adjust buffer positions in reused rows. */
17532 if (delta || delta_bytes)
17533 increment_matrix_positions (current_matrix,
17534 first_unchanged_at_end_vpos + dvpos,
17535 bottom_vpos, delta, delta_bytes);
17536
17537 /* Adjust Y positions. */
17538 if (dy)
17539 shift_glyph_matrix (w, current_matrix,
17540 first_unchanged_at_end_vpos + dvpos,
17541 bottom_vpos, dy);
17542
17543 if (first_unchanged_at_end_row)
17544 {
17545 first_unchanged_at_end_row += dvpos;
17546 if (first_unchanged_at_end_row->y >= it.last_visible_y
17547 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17548 first_unchanged_at_end_row = NULL;
17549 }
17550
17551 /* If scrolling up, there may be some lines to display at the end of
17552 the window. */
17553 last_text_row_at_end = NULL;
17554 if (dy < 0)
17555 {
17556 /* Scrolling up can leave for example a partially visible line
17557 at the end of the window to be redisplayed. */
17558 /* Set last_row to the glyph row in the current matrix where the
17559 window end line is found. It has been moved up or down in
17560 the matrix by dvpos. */
17561 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17562 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17563
17564 /* If last_row is the window end line, it should display text. */
17565 eassert (last_row->displays_text_p);
17566
17567 /* If window end line was partially visible before, begin
17568 displaying at that line. Otherwise begin displaying with the
17569 line following it. */
17570 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17571 {
17572 init_to_row_start (&it, w, last_row);
17573 it.vpos = last_vpos;
17574 it.current_y = last_row->y;
17575 }
17576 else
17577 {
17578 init_to_row_end (&it, w, last_row);
17579 it.vpos = 1 + last_vpos;
17580 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17581 ++last_row;
17582 }
17583
17584 /* We may start in a continuation line. If so, we have to
17585 get the right continuation_lines_width and current_x. */
17586 it.continuation_lines_width = last_row->continuation_lines_width;
17587 it.hpos = it.current_x = 0;
17588
17589 /* Display the rest of the lines at the window end. */
17590 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17591 while (it.current_y < it.last_visible_y
17592 && !fonts_changed_p)
17593 {
17594 /* Is it always sure that the display agrees with lines in
17595 the current matrix? I don't think so, so we mark rows
17596 displayed invalid in the current matrix by setting their
17597 enabled_p flag to zero. */
17598 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17599 if (display_line (&it))
17600 last_text_row_at_end = it.glyph_row - 1;
17601 }
17602 }
17603
17604 /* Update window_end_pos and window_end_vpos. */
17605 if (first_unchanged_at_end_row
17606 && !last_text_row_at_end)
17607 {
17608 /* Window end line if one of the preserved rows from the current
17609 matrix. Set row to the last row displaying text in current
17610 matrix starting at first_unchanged_at_end_row, after
17611 scrolling. */
17612 eassert (first_unchanged_at_end_row->displays_text_p);
17613 row = find_last_row_displaying_text (w->current_matrix, &it,
17614 first_unchanged_at_end_row);
17615 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17616
17617 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17618 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17619 wset_window_end_vpos
17620 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17621 eassert (w->window_end_bytepos >= 0);
17622 IF_DEBUG (debug_method_add (w, "A"));
17623 }
17624 else if (last_text_row_at_end)
17625 {
17626 wset_window_end_pos
17627 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17628 w->window_end_bytepos
17629 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17630 wset_window_end_vpos
17631 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17632 desired_matrix)));
17633 eassert (w->window_end_bytepos >= 0);
17634 IF_DEBUG (debug_method_add (w, "B"));
17635 }
17636 else if (last_text_row)
17637 {
17638 /* We have displayed either to the end of the window or at the
17639 end of the window, i.e. the last row with text is to be found
17640 in the desired matrix. */
17641 wset_window_end_pos
17642 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17643 w->window_end_bytepos
17644 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17645 wset_window_end_vpos
17646 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17647 eassert (w->window_end_bytepos >= 0);
17648 }
17649 else if (first_unchanged_at_end_row == NULL
17650 && last_text_row == NULL
17651 && last_text_row_at_end == NULL)
17652 {
17653 /* Displayed to end of window, but no line containing text was
17654 displayed. Lines were deleted at the end of the window. */
17655 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17656 int vpos = XFASTINT (w->window_end_vpos);
17657 struct glyph_row *current_row = current_matrix->rows + vpos;
17658 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17659
17660 for (row = NULL;
17661 row == NULL && vpos >= first_vpos;
17662 --vpos, --current_row, --desired_row)
17663 {
17664 if (desired_row->enabled_p)
17665 {
17666 if (desired_row->displays_text_p)
17667 row = desired_row;
17668 }
17669 else if (current_row->displays_text_p)
17670 row = current_row;
17671 }
17672
17673 eassert (row != NULL);
17674 wset_window_end_vpos (w, make_number (vpos + 1));
17675 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17676 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17677 eassert (w->window_end_bytepos >= 0);
17678 IF_DEBUG (debug_method_add (w, "C"));
17679 }
17680 else
17681 emacs_abort ();
17682
17683 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17684 debug_end_vpos = XFASTINT (w->window_end_vpos));
17685
17686 /* Record that display has not been completed. */
17687 w->window_end_valid = 0;
17688 w->desired_matrix->no_scrolling_p = 1;
17689 return 3;
17690
17691 #undef GIVE_UP
17692 }
17693
17694
17695 \f
17696 /***********************************************************************
17697 More debugging support
17698 ***********************************************************************/
17699
17700 #ifdef GLYPH_DEBUG
17701
17702 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17703 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17704 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17705
17706
17707 /* Dump the contents of glyph matrix MATRIX on stderr.
17708
17709 GLYPHS 0 means don't show glyph contents.
17710 GLYPHS 1 means show glyphs in short form
17711 GLYPHS > 1 means show glyphs in long form. */
17712
17713 void
17714 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17715 {
17716 int i;
17717 for (i = 0; i < matrix->nrows; ++i)
17718 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17719 }
17720
17721
17722 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17723 the glyph row and area where the glyph comes from. */
17724
17725 void
17726 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17727 {
17728 if (glyph->type == CHAR_GLYPH
17729 || glyph->type == GLYPHLESS_GLYPH)
17730 {
17731 fprintf (stderr,
17732 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17733 glyph - row->glyphs[TEXT_AREA],
17734 (glyph->type == CHAR_GLYPH
17735 ? 'C'
17736 : 'G'),
17737 glyph->charpos,
17738 (BUFFERP (glyph->object)
17739 ? 'B'
17740 : (STRINGP (glyph->object)
17741 ? 'S'
17742 : (INTEGERP (glyph->object)
17743 ? '0'
17744 : '-'))),
17745 glyph->pixel_width,
17746 glyph->u.ch,
17747 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17748 ? glyph->u.ch
17749 : '.'),
17750 glyph->face_id,
17751 glyph->left_box_line_p,
17752 glyph->right_box_line_p);
17753 }
17754 else if (glyph->type == STRETCH_GLYPH)
17755 {
17756 fprintf (stderr,
17757 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17758 glyph - row->glyphs[TEXT_AREA],
17759 'S',
17760 glyph->charpos,
17761 (BUFFERP (glyph->object)
17762 ? 'B'
17763 : (STRINGP (glyph->object)
17764 ? 'S'
17765 : (INTEGERP (glyph->object)
17766 ? '0'
17767 : '-'))),
17768 glyph->pixel_width,
17769 0,
17770 ' ',
17771 glyph->face_id,
17772 glyph->left_box_line_p,
17773 glyph->right_box_line_p);
17774 }
17775 else if (glyph->type == IMAGE_GLYPH)
17776 {
17777 fprintf (stderr,
17778 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17779 glyph - row->glyphs[TEXT_AREA],
17780 'I',
17781 glyph->charpos,
17782 (BUFFERP (glyph->object)
17783 ? 'B'
17784 : (STRINGP (glyph->object)
17785 ? 'S'
17786 : (INTEGERP (glyph->object)
17787 ? '0'
17788 : '-'))),
17789 glyph->pixel_width,
17790 glyph->u.img_id,
17791 '.',
17792 glyph->face_id,
17793 glyph->left_box_line_p,
17794 glyph->right_box_line_p);
17795 }
17796 else if (glyph->type == COMPOSITE_GLYPH)
17797 {
17798 fprintf (stderr,
17799 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17800 glyph - row->glyphs[TEXT_AREA],
17801 '+',
17802 glyph->charpos,
17803 (BUFFERP (glyph->object)
17804 ? 'B'
17805 : (STRINGP (glyph->object)
17806 ? 'S'
17807 : (INTEGERP (glyph->object)
17808 ? '0'
17809 : '-'))),
17810 glyph->pixel_width,
17811 glyph->u.cmp.id);
17812 if (glyph->u.cmp.automatic)
17813 fprintf (stderr,
17814 "[%d-%d]",
17815 glyph->slice.cmp.from, glyph->slice.cmp.to);
17816 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17817 glyph->face_id,
17818 glyph->left_box_line_p,
17819 glyph->right_box_line_p);
17820 }
17821 }
17822
17823
17824 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17825 GLYPHS 0 means don't show glyph contents.
17826 GLYPHS 1 means show glyphs in short form
17827 GLYPHS > 1 means show glyphs in long form. */
17828
17829 void
17830 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17831 {
17832 if (glyphs != 1)
17833 {
17834 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17835 fprintf (stderr, "==============================================================================\n");
17836
17837 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17838 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17839 vpos,
17840 MATRIX_ROW_START_CHARPOS (row),
17841 MATRIX_ROW_END_CHARPOS (row),
17842 row->used[TEXT_AREA],
17843 row->contains_overlapping_glyphs_p,
17844 row->enabled_p,
17845 row->truncated_on_left_p,
17846 row->truncated_on_right_p,
17847 row->continued_p,
17848 MATRIX_ROW_CONTINUATION_LINE_P (row),
17849 row->displays_text_p,
17850 row->ends_at_zv_p,
17851 row->fill_line_p,
17852 row->ends_in_middle_of_char_p,
17853 row->starts_in_middle_of_char_p,
17854 row->mouse_face_p,
17855 row->x,
17856 row->y,
17857 row->pixel_width,
17858 row->height,
17859 row->visible_height,
17860 row->ascent,
17861 row->phys_ascent);
17862 /* The next 3 lines should align to "Start" in the header. */
17863 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17864 row->end.overlay_string_index,
17865 row->continuation_lines_width);
17866 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17867 CHARPOS (row->start.string_pos),
17868 CHARPOS (row->end.string_pos));
17869 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17870 row->end.dpvec_index);
17871 }
17872
17873 if (glyphs > 1)
17874 {
17875 int area;
17876
17877 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17878 {
17879 struct glyph *glyph = row->glyphs[area];
17880 struct glyph *glyph_end = glyph + row->used[area];
17881
17882 /* Glyph for a line end in text. */
17883 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17884 ++glyph_end;
17885
17886 if (glyph < glyph_end)
17887 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17888
17889 for (; glyph < glyph_end; ++glyph)
17890 dump_glyph (row, glyph, area);
17891 }
17892 }
17893 else if (glyphs == 1)
17894 {
17895 int area;
17896
17897 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17898 {
17899 char *s = alloca (row->used[area] + 4);
17900 int i;
17901
17902 for (i = 0; i < row->used[area]; ++i)
17903 {
17904 struct glyph *glyph = row->glyphs[area] + i;
17905 if (i == row->used[area] - 1
17906 && area == TEXT_AREA
17907 && INTEGERP (glyph->object)
17908 && glyph->type == CHAR_GLYPH
17909 && glyph->u.ch == ' ')
17910 {
17911 strcpy (&s[i], "[\\n]");
17912 i += 4;
17913 }
17914 else if (glyph->type == CHAR_GLYPH
17915 && glyph->u.ch < 0x80
17916 && glyph->u.ch >= ' ')
17917 s[i] = glyph->u.ch;
17918 else
17919 s[i] = '.';
17920 }
17921
17922 s[i] = '\0';
17923 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17924 }
17925 }
17926 }
17927
17928
17929 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17930 Sdump_glyph_matrix, 0, 1, "p",
17931 doc: /* Dump the current matrix of the selected window to stderr.
17932 Shows contents of glyph row structures. With non-nil
17933 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17934 glyphs in short form, otherwise show glyphs in long form. */)
17935 (Lisp_Object glyphs)
17936 {
17937 struct window *w = XWINDOW (selected_window);
17938 struct buffer *buffer = XBUFFER (w->buffer);
17939
17940 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17941 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17942 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17943 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17944 fprintf (stderr, "=============================================\n");
17945 dump_glyph_matrix (w->current_matrix,
17946 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17947 return Qnil;
17948 }
17949
17950
17951 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17952 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17953 (void)
17954 {
17955 struct frame *f = XFRAME (selected_frame);
17956 dump_glyph_matrix (f->current_matrix, 1);
17957 return Qnil;
17958 }
17959
17960
17961 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17962 doc: /* Dump glyph row ROW to stderr.
17963 GLYPH 0 means don't dump glyphs.
17964 GLYPH 1 means dump glyphs in short form.
17965 GLYPH > 1 or omitted means dump glyphs in long form. */)
17966 (Lisp_Object row, Lisp_Object glyphs)
17967 {
17968 struct glyph_matrix *matrix;
17969 EMACS_INT vpos;
17970
17971 CHECK_NUMBER (row);
17972 matrix = XWINDOW (selected_window)->current_matrix;
17973 vpos = XINT (row);
17974 if (vpos >= 0 && vpos < matrix->nrows)
17975 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17976 vpos,
17977 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17978 return Qnil;
17979 }
17980
17981
17982 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17983 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17984 GLYPH 0 means don't dump glyphs.
17985 GLYPH 1 means dump glyphs in short form.
17986 GLYPH > 1 or omitted means dump glyphs in long form. */)
17987 (Lisp_Object row, Lisp_Object glyphs)
17988 {
17989 struct frame *sf = SELECTED_FRAME ();
17990 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17991 EMACS_INT vpos;
17992
17993 CHECK_NUMBER (row);
17994 vpos = XINT (row);
17995 if (vpos >= 0 && vpos < m->nrows)
17996 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17997 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17998 return Qnil;
17999 }
18000
18001
18002 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18003 doc: /* Toggle tracing of redisplay.
18004 With ARG, turn tracing on if and only if ARG is positive. */)
18005 (Lisp_Object arg)
18006 {
18007 if (NILP (arg))
18008 trace_redisplay_p = !trace_redisplay_p;
18009 else
18010 {
18011 arg = Fprefix_numeric_value (arg);
18012 trace_redisplay_p = XINT (arg) > 0;
18013 }
18014
18015 return Qnil;
18016 }
18017
18018
18019 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18020 doc: /* Like `format', but print result to stderr.
18021 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18022 (ptrdiff_t nargs, Lisp_Object *args)
18023 {
18024 Lisp_Object s = Fformat (nargs, args);
18025 fprintf (stderr, "%s", SDATA (s));
18026 return Qnil;
18027 }
18028
18029 #endif /* GLYPH_DEBUG */
18030
18031
18032 \f
18033 /***********************************************************************
18034 Building Desired Matrix Rows
18035 ***********************************************************************/
18036
18037 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18038 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18039
18040 static struct glyph_row *
18041 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18042 {
18043 struct frame *f = XFRAME (WINDOW_FRAME (w));
18044 struct buffer *buffer = XBUFFER (w->buffer);
18045 struct buffer *old = current_buffer;
18046 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18047 int arrow_len = SCHARS (overlay_arrow_string);
18048 const unsigned char *arrow_end = arrow_string + arrow_len;
18049 const unsigned char *p;
18050 struct it it;
18051 int multibyte_p;
18052 int n_glyphs_before;
18053
18054 set_buffer_temp (buffer);
18055 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18056 it.glyph_row->used[TEXT_AREA] = 0;
18057 SET_TEXT_POS (it.position, 0, 0);
18058
18059 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18060 p = arrow_string;
18061 while (p < arrow_end)
18062 {
18063 Lisp_Object face, ilisp;
18064
18065 /* Get the next character. */
18066 if (multibyte_p)
18067 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18068 else
18069 {
18070 it.c = it.char_to_display = *p, it.len = 1;
18071 if (! ASCII_CHAR_P (it.c))
18072 it.char_to_display = BYTE8_TO_CHAR (it.c);
18073 }
18074 p += it.len;
18075
18076 /* Get its face. */
18077 ilisp = make_number (p - arrow_string);
18078 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18079 it.face_id = compute_char_face (f, it.char_to_display, face);
18080
18081 /* Compute its width, get its glyphs. */
18082 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18083 SET_TEXT_POS (it.position, -1, -1);
18084 PRODUCE_GLYPHS (&it);
18085
18086 /* If this character doesn't fit any more in the line, we have
18087 to remove some glyphs. */
18088 if (it.current_x > it.last_visible_x)
18089 {
18090 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18091 break;
18092 }
18093 }
18094
18095 set_buffer_temp (old);
18096 return it.glyph_row;
18097 }
18098
18099
18100 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18101 glyphs to insert is determined by produce_special_glyphs. */
18102
18103 static void
18104 insert_left_trunc_glyphs (struct it *it)
18105 {
18106 struct it truncate_it;
18107 struct glyph *from, *end, *to, *toend;
18108
18109 eassert (!FRAME_WINDOW_P (it->f)
18110 || (!it->glyph_row->reversed_p
18111 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18112 || (it->glyph_row->reversed_p
18113 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18114
18115 /* Get the truncation glyphs. */
18116 truncate_it = *it;
18117 truncate_it.current_x = 0;
18118 truncate_it.face_id = DEFAULT_FACE_ID;
18119 truncate_it.glyph_row = &scratch_glyph_row;
18120 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18121 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18122 truncate_it.object = make_number (0);
18123 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18124
18125 /* Overwrite glyphs from IT with truncation glyphs. */
18126 if (!it->glyph_row->reversed_p)
18127 {
18128 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18129
18130 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18131 end = from + tused;
18132 to = it->glyph_row->glyphs[TEXT_AREA];
18133 toend = to + it->glyph_row->used[TEXT_AREA];
18134 if (FRAME_WINDOW_P (it->f))
18135 {
18136 /* On GUI frames, when variable-size fonts are displayed,
18137 the truncation glyphs may need more pixels than the row's
18138 glyphs they overwrite. We overwrite more glyphs to free
18139 enough screen real estate, and enlarge the stretch glyph
18140 on the right (see display_line), if there is one, to
18141 preserve the screen position of the truncation glyphs on
18142 the right. */
18143 int w = 0;
18144 struct glyph *g = to;
18145 short used;
18146
18147 /* The first glyph could be partially visible, in which case
18148 it->glyph_row->x will be negative. But we want the left
18149 truncation glyphs to be aligned at the left margin of the
18150 window, so we override the x coordinate at which the row
18151 will begin. */
18152 it->glyph_row->x = 0;
18153 while (g < toend && w < it->truncation_pixel_width)
18154 {
18155 w += g->pixel_width;
18156 ++g;
18157 }
18158 if (g - to - tused > 0)
18159 {
18160 memmove (to + tused, g, (toend - g) * sizeof(*g));
18161 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18162 }
18163 used = it->glyph_row->used[TEXT_AREA];
18164 if (it->glyph_row->truncated_on_right_p
18165 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18166 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18167 == STRETCH_GLYPH)
18168 {
18169 int extra = w - it->truncation_pixel_width;
18170
18171 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18172 }
18173 }
18174
18175 while (from < end)
18176 *to++ = *from++;
18177
18178 /* There may be padding glyphs left over. Overwrite them too. */
18179 if (!FRAME_WINDOW_P (it->f))
18180 {
18181 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18182 {
18183 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18184 while (from < end)
18185 *to++ = *from++;
18186 }
18187 }
18188
18189 if (to > toend)
18190 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18191 }
18192 else
18193 {
18194 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18195
18196 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18197 that back to front. */
18198 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18199 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18200 toend = it->glyph_row->glyphs[TEXT_AREA];
18201 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18202 if (FRAME_WINDOW_P (it->f))
18203 {
18204 int w = 0;
18205 struct glyph *g = to;
18206
18207 while (g >= toend && w < it->truncation_pixel_width)
18208 {
18209 w += g->pixel_width;
18210 --g;
18211 }
18212 if (to - g - tused > 0)
18213 to = g + tused;
18214 if (it->glyph_row->truncated_on_right_p
18215 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18216 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18217 {
18218 int extra = w - it->truncation_pixel_width;
18219
18220 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18221 }
18222 }
18223
18224 while (from >= end && to >= toend)
18225 *to-- = *from--;
18226 if (!FRAME_WINDOW_P (it->f))
18227 {
18228 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18229 {
18230 from =
18231 truncate_it.glyph_row->glyphs[TEXT_AREA]
18232 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18233 while (from >= end && to >= toend)
18234 *to-- = *from--;
18235 }
18236 }
18237 if (from >= end)
18238 {
18239 /* Need to free some room before prepending additional
18240 glyphs. */
18241 int move_by = from - end + 1;
18242 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18243 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18244
18245 for ( ; g >= g0; g--)
18246 g[move_by] = *g;
18247 while (from >= end)
18248 *to-- = *from--;
18249 it->glyph_row->used[TEXT_AREA] += move_by;
18250 }
18251 }
18252 }
18253
18254 /* Compute the hash code for ROW. */
18255 unsigned
18256 row_hash (struct glyph_row *row)
18257 {
18258 int area, k;
18259 unsigned hashval = 0;
18260
18261 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18262 for (k = 0; k < row->used[area]; ++k)
18263 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18264 + row->glyphs[area][k].u.val
18265 + row->glyphs[area][k].face_id
18266 + row->glyphs[area][k].padding_p
18267 + (row->glyphs[area][k].type << 2));
18268
18269 return hashval;
18270 }
18271
18272 /* Compute the pixel height and width of IT->glyph_row.
18273
18274 Most of the time, ascent and height of a display line will be equal
18275 to the max_ascent and max_height values of the display iterator
18276 structure. This is not the case if
18277
18278 1. We hit ZV without displaying anything. In this case, max_ascent
18279 and max_height will be zero.
18280
18281 2. We have some glyphs that don't contribute to the line height.
18282 (The glyph row flag contributes_to_line_height_p is for future
18283 pixmap extensions).
18284
18285 The first case is easily covered by using default values because in
18286 these cases, the line height does not really matter, except that it
18287 must not be zero. */
18288
18289 static void
18290 compute_line_metrics (struct it *it)
18291 {
18292 struct glyph_row *row = it->glyph_row;
18293
18294 if (FRAME_WINDOW_P (it->f))
18295 {
18296 int i, min_y, max_y;
18297
18298 /* The line may consist of one space only, that was added to
18299 place the cursor on it. If so, the row's height hasn't been
18300 computed yet. */
18301 if (row->height == 0)
18302 {
18303 if (it->max_ascent + it->max_descent == 0)
18304 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18305 row->ascent = it->max_ascent;
18306 row->height = it->max_ascent + it->max_descent;
18307 row->phys_ascent = it->max_phys_ascent;
18308 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18309 row->extra_line_spacing = it->max_extra_line_spacing;
18310 }
18311
18312 /* Compute the width of this line. */
18313 row->pixel_width = row->x;
18314 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18315 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18316
18317 eassert (row->pixel_width >= 0);
18318 eassert (row->ascent >= 0 && row->height > 0);
18319
18320 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18321 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18322
18323 /* If first line's physical ascent is larger than its logical
18324 ascent, use the physical ascent, and make the row taller.
18325 This makes accented characters fully visible. */
18326 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18327 && row->phys_ascent > row->ascent)
18328 {
18329 row->height += row->phys_ascent - row->ascent;
18330 row->ascent = row->phys_ascent;
18331 }
18332
18333 /* Compute how much of the line is visible. */
18334 row->visible_height = row->height;
18335
18336 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18337 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18338
18339 if (row->y < min_y)
18340 row->visible_height -= min_y - row->y;
18341 if (row->y + row->height > max_y)
18342 row->visible_height -= row->y + row->height - max_y;
18343 }
18344 else
18345 {
18346 row->pixel_width = row->used[TEXT_AREA];
18347 if (row->continued_p)
18348 row->pixel_width -= it->continuation_pixel_width;
18349 else if (row->truncated_on_right_p)
18350 row->pixel_width -= it->truncation_pixel_width;
18351 row->ascent = row->phys_ascent = 0;
18352 row->height = row->phys_height = row->visible_height = 1;
18353 row->extra_line_spacing = 0;
18354 }
18355
18356 /* Compute a hash code for this row. */
18357 row->hash = row_hash (row);
18358
18359 it->max_ascent = it->max_descent = 0;
18360 it->max_phys_ascent = it->max_phys_descent = 0;
18361 }
18362
18363
18364 /* Append one space to the glyph row of iterator IT if doing a
18365 window-based redisplay. The space has the same face as
18366 IT->face_id. Value is non-zero if a space was added.
18367
18368 This function is called to make sure that there is always one glyph
18369 at the end of a glyph row that the cursor can be set on under
18370 window-systems. (If there weren't such a glyph we would not know
18371 how wide and tall a box cursor should be displayed).
18372
18373 At the same time this space let's a nicely handle clearing to the
18374 end of the line if the row ends in italic text. */
18375
18376 static int
18377 append_space_for_newline (struct it *it, int default_face_p)
18378 {
18379 if (FRAME_WINDOW_P (it->f))
18380 {
18381 int n = it->glyph_row->used[TEXT_AREA];
18382
18383 if (it->glyph_row->glyphs[TEXT_AREA] + n
18384 < it->glyph_row->glyphs[1 + TEXT_AREA])
18385 {
18386 /* Save some values that must not be changed.
18387 Must save IT->c and IT->len because otherwise
18388 ITERATOR_AT_END_P wouldn't work anymore after
18389 append_space_for_newline has been called. */
18390 enum display_element_type saved_what = it->what;
18391 int saved_c = it->c, saved_len = it->len;
18392 int saved_char_to_display = it->char_to_display;
18393 int saved_x = it->current_x;
18394 int saved_face_id = it->face_id;
18395 int saved_box_end = it->end_of_box_run_p;
18396 struct text_pos saved_pos;
18397 Lisp_Object saved_object;
18398 struct face *face;
18399
18400 saved_object = it->object;
18401 saved_pos = it->position;
18402
18403 it->what = IT_CHARACTER;
18404 memset (&it->position, 0, sizeof it->position);
18405 it->object = make_number (0);
18406 it->c = it->char_to_display = ' ';
18407 it->len = 1;
18408
18409 /* If the default face was remapped, be sure to use the
18410 remapped face for the appended newline. */
18411 if (default_face_p)
18412 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18413 else if (it->face_before_selective_p)
18414 it->face_id = it->saved_face_id;
18415 face = FACE_FROM_ID (it->f, it->face_id);
18416 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18417 /* In R2L rows, we will prepend a stretch glyph that will
18418 have the end_of_box_run_p flag set for it, so there's no
18419 need for the appended newline glyph to have that flag
18420 set. */
18421 if (it->glyph_row->reversed_p
18422 /* But if the appended newline glyph goes all the way to
18423 the end of the row, there will be no stretch glyph,
18424 so leave the box flag set. */
18425 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18426 it->end_of_box_run_p = 0;
18427
18428 PRODUCE_GLYPHS (it);
18429
18430 it->override_ascent = -1;
18431 it->constrain_row_ascent_descent_p = 0;
18432 it->current_x = saved_x;
18433 it->object = saved_object;
18434 it->position = saved_pos;
18435 it->what = saved_what;
18436 it->face_id = saved_face_id;
18437 it->len = saved_len;
18438 it->c = saved_c;
18439 it->char_to_display = saved_char_to_display;
18440 it->end_of_box_run_p = saved_box_end;
18441 return 1;
18442 }
18443 }
18444
18445 return 0;
18446 }
18447
18448
18449 /* Extend the face of the last glyph in the text area of IT->glyph_row
18450 to the end of the display line. Called from display_line. If the
18451 glyph row is empty, add a space glyph to it so that we know the
18452 face to draw. Set the glyph row flag fill_line_p. If the glyph
18453 row is R2L, prepend a stretch glyph to cover the empty space to the
18454 left of the leftmost glyph. */
18455
18456 static void
18457 extend_face_to_end_of_line (struct it *it)
18458 {
18459 struct face *face, *default_face;
18460 struct frame *f = it->f;
18461
18462 /* If line is already filled, do nothing. Non window-system frames
18463 get a grace of one more ``pixel'' because their characters are
18464 1-``pixel'' wide, so they hit the equality too early. This grace
18465 is needed only for R2L rows that are not continued, to produce
18466 one extra blank where we could display the cursor. */
18467 if (it->current_x >= it->last_visible_x
18468 + (!FRAME_WINDOW_P (f)
18469 && it->glyph_row->reversed_p
18470 && !it->glyph_row->continued_p))
18471 return;
18472
18473 /* The default face, possibly remapped. */
18474 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18475
18476 /* Face extension extends the background and box of IT->face_id
18477 to the end of the line. If the background equals the background
18478 of the frame, we don't have to do anything. */
18479 if (it->face_before_selective_p)
18480 face = FACE_FROM_ID (f, it->saved_face_id);
18481 else
18482 face = FACE_FROM_ID (f, it->face_id);
18483
18484 if (FRAME_WINDOW_P (f)
18485 && it->glyph_row->displays_text_p
18486 && face->box == FACE_NO_BOX
18487 && face->background == FRAME_BACKGROUND_PIXEL (f)
18488 && !face->stipple
18489 && !it->glyph_row->reversed_p)
18490 return;
18491
18492 /* Set the glyph row flag indicating that the face of the last glyph
18493 in the text area has to be drawn to the end of the text area. */
18494 it->glyph_row->fill_line_p = 1;
18495
18496 /* If current character of IT is not ASCII, make sure we have the
18497 ASCII face. This will be automatically undone the next time
18498 get_next_display_element returns a multibyte character. Note
18499 that the character will always be single byte in unibyte
18500 text. */
18501 if (!ASCII_CHAR_P (it->c))
18502 {
18503 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18504 }
18505
18506 if (FRAME_WINDOW_P (f))
18507 {
18508 /* If the row is empty, add a space with the current face of IT,
18509 so that we know which face to draw. */
18510 if (it->glyph_row->used[TEXT_AREA] == 0)
18511 {
18512 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18513 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18514 it->glyph_row->used[TEXT_AREA] = 1;
18515 }
18516 #ifdef HAVE_WINDOW_SYSTEM
18517 if (it->glyph_row->reversed_p)
18518 {
18519 /* Prepend a stretch glyph to the row, such that the
18520 rightmost glyph will be drawn flushed all the way to the
18521 right margin of the window. The stretch glyph that will
18522 occupy the empty space, if any, to the left of the
18523 glyphs. */
18524 struct font *font = face->font ? face->font : FRAME_FONT (f);
18525 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18526 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18527 struct glyph *g;
18528 int row_width, stretch_ascent, stretch_width;
18529 struct text_pos saved_pos;
18530 int saved_face_id, saved_avoid_cursor, saved_box_start;
18531
18532 for (row_width = 0, g = row_start; g < row_end; g++)
18533 row_width += g->pixel_width;
18534 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18535 if (stretch_width > 0)
18536 {
18537 stretch_ascent =
18538 (((it->ascent + it->descent)
18539 * FONT_BASE (font)) / FONT_HEIGHT (font));
18540 saved_pos = it->position;
18541 memset (&it->position, 0, sizeof it->position);
18542 saved_avoid_cursor = it->avoid_cursor_p;
18543 it->avoid_cursor_p = 1;
18544 saved_face_id = it->face_id;
18545 saved_box_start = it->start_of_box_run_p;
18546 /* The last row's stretch glyph should get the default
18547 face, to avoid painting the rest of the window with
18548 the region face, if the region ends at ZV. */
18549 if (it->glyph_row->ends_at_zv_p)
18550 it->face_id = default_face->id;
18551 else
18552 it->face_id = face->id;
18553 it->start_of_box_run_p = 0;
18554 append_stretch_glyph (it, make_number (0), stretch_width,
18555 it->ascent + it->descent, stretch_ascent);
18556 it->position = saved_pos;
18557 it->avoid_cursor_p = saved_avoid_cursor;
18558 it->face_id = saved_face_id;
18559 it->start_of_box_run_p = saved_box_start;
18560 }
18561 }
18562 #endif /* HAVE_WINDOW_SYSTEM */
18563 }
18564 else
18565 {
18566 /* Save some values that must not be changed. */
18567 int saved_x = it->current_x;
18568 struct text_pos saved_pos;
18569 Lisp_Object saved_object;
18570 enum display_element_type saved_what = it->what;
18571 int saved_face_id = it->face_id;
18572
18573 saved_object = it->object;
18574 saved_pos = it->position;
18575
18576 it->what = IT_CHARACTER;
18577 memset (&it->position, 0, sizeof it->position);
18578 it->object = make_number (0);
18579 it->c = it->char_to_display = ' ';
18580 it->len = 1;
18581 /* The last row's blank glyphs should get the default face, to
18582 avoid painting the rest of the window with the region face,
18583 if the region ends at ZV. */
18584 if (it->glyph_row->ends_at_zv_p)
18585 it->face_id = default_face->id;
18586 else
18587 it->face_id = face->id;
18588
18589 PRODUCE_GLYPHS (it);
18590
18591 while (it->current_x <= it->last_visible_x)
18592 PRODUCE_GLYPHS (it);
18593
18594 /* Don't count these blanks really. It would let us insert a left
18595 truncation glyph below and make us set the cursor on them, maybe. */
18596 it->current_x = saved_x;
18597 it->object = saved_object;
18598 it->position = saved_pos;
18599 it->what = saved_what;
18600 it->face_id = saved_face_id;
18601 }
18602 }
18603
18604
18605 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18606 trailing whitespace. */
18607
18608 static int
18609 trailing_whitespace_p (ptrdiff_t charpos)
18610 {
18611 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18612 int c = 0;
18613
18614 while (bytepos < ZV_BYTE
18615 && (c = FETCH_CHAR (bytepos),
18616 c == ' ' || c == '\t'))
18617 ++bytepos;
18618
18619 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18620 {
18621 if (bytepos != PT_BYTE)
18622 return 1;
18623 }
18624 return 0;
18625 }
18626
18627
18628 /* Highlight trailing whitespace, if any, in ROW. */
18629
18630 static void
18631 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18632 {
18633 int used = row->used[TEXT_AREA];
18634
18635 if (used)
18636 {
18637 struct glyph *start = row->glyphs[TEXT_AREA];
18638 struct glyph *glyph = start + used - 1;
18639
18640 if (row->reversed_p)
18641 {
18642 /* Right-to-left rows need to be processed in the opposite
18643 direction, so swap the edge pointers. */
18644 glyph = start;
18645 start = row->glyphs[TEXT_AREA] + used - 1;
18646 }
18647
18648 /* Skip over glyphs inserted to display the cursor at the
18649 end of a line, for extending the face of the last glyph
18650 to the end of the line on terminals, and for truncation
18651 and continuation glyphs. */
18652 if (!row->reversed_p)
18653 {
18654 while (glyph >= start
18655 && glyph->type == CHAR_GLYPH
18656 && INTEGERP (glyph->object))
18657 --glyph;
18658 }
18659 else
18660 {
18661 while (glyph <= start
18662 && glyph->type == CHAR_GLYPH
18663 && INTEGERP (glyph->object))
18664 ++glyph;
18665 }
18666
18667 /* If last glyph is a space or stretch, and it's trailing
18668 whitespace, set the face of all trailing whitespace glyphs in
18669 IT->glyph_row to `trailing-whitespace'. */
18670 if ((row->reversed_p ? glyph <= start : glyph >= start)
18671 && BUFFERP (glyph->object)
18672 && (glyph->type == STRETCH_GLYPH
18673 || (glyph->type == CHAR_GLYPH
18674 && glyph->u.ch == ' '))
18675 && trailing_whitespace_p (glyph->charpos))
18676 {
18677 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18678 if (face_id < 0)
18679 return;
18680
18681 if (!row->reversed_p)
18682 {
18683 while (glyph >= start
18684 && BUFFERP (glyph->object)
18685 && (glyph->type == STRETCH_GLYPH
18686 || (glyph->type == CHAR_GLYPH
18687 && glyph->u.ch == ' ')))
18688 (glyph--)->face_id = face_id;
18689 }
18690 else
18691 {
18692 while (glyph <= start
18693 && BUFFERP (glyph->object)
18694 && (glyph->type == STRETCH_GLYPH
18695 || (glyph->type == CHAR_GLYPH
18696 && glyph->u.ch == ' ')))
18697 (glyph++)->face_id = face_id;
18698 }
18699 }
18700 }
18701 }
18702
18703
18704 /* Value is non-zero if glyph row ROW should be
18705 used to hold the cursor. */
18706
18707 static int
18708 cursor_row_p (struct glyph_row *row)
18709 {
18710 int result = 1;
18711
18712 if (PT == CHARPOS (row->end.pos)
18713 || PT == MATRIX_ROW_END_CHARPOS (row))
18714 {
18715 /* Suppose the row ends on a string.
18716 Unless the row is continued, that means it ends on a newline
18717 in the string. If it's anything other than a display string
18718 (e.g., a before-string from an overlay), we don't want the
18719 cursor there. (This heuristic seems to give the optimal
18720 behavior for the various types of multi-line strings.)
18721 One exception: if the string has `cursor' property on one of
18722 its characters, we _do_ want the cursor there. */
18723 if (CHARPOS (row->end.string_pos) >= 0)
18724 {
18725 if (row->continued_p)
18726 result = 1;
18727 else
18728 {
18729 /* Check for `display' property. */
18730 struct glyph *beg = row->glyphs[TEXT_AREA];
18731 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18732 struct glyph *glyph;
18733
18734 result = 0;
18735 for (glyph = end; glyph >= beg; --glyph)
18736 if (STRINGP (glyph->object))
18737 {
18738 Lisp_Object prop
18739 = Fget_char_property (make_number (PT),
18740 Qdisplay, Qnil);
18741 result =
18742 (!NILP (prop)
18743 && display_prop_string_p (prop, glyph->object));
18744 /* If there's a `cursor' property on one of the
18745 string's characters, this row is a cursor row,
18746 even though this is not a display string. */
18747 if (!result)
18748 {
18749 Lisp_Object s = glyph->object;
18750
18751 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18752 {
18753 ptrdiff_t gpos = glyph->charpos;
18754
18755 if (!NILP (Fget_char_property (make_number (gpos),
18756 Qcursor, s)))
18757 {
18758 result = 1;
18759 break;
18760 }
18761 }
18762 }
18763 break;
18764 }
18765 }
18766 }
18767 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18768 {
18769 /* If the row ends in middle of a real character,
18770 and the line is continued, we want the cursor here.
18771 That's because CHARPOS (ROW->end.pos) would equal
18772 PT if PT is before the character. */
18773 if (!row->ends_in_ellipsis_p)
18774 result = row->continued_p;
18775 else
18776 /* If the row ends in an ellipsis, then
18777 CHARPOS (ROW->end.pos) will equal point after the
18778 invisible text. We want that position to be displayed
18779 after the ellipsis. */
18780 result = 0;
18781 }
18782 /* If the row ends at ZV, display the cursor at the end of that
18783 row instead of at the start of the row below. */
18784 else if (row->ends_at_zv_p)
18785 result = 1;
18786 else
18787 result = 0;
18788 }
18789
18790 return result;
18791 }
18792
18793 \f
18794
18795 /* Push the property PROP so that it will be rendered at the current
18796 position in IT. Return 1 if PROP was successfully pushed, 0
18797 otherwise. Called from handle_line_prefix to handle the
18798 `line-prefix' and `wrap-prefix' properties. */
18799
18800 static int
18801 push_prefix_prop (struct it *it, Lisp_Object prop)
18802 {
18803 struct text_pos pos =
18804 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18805
18806 eassert (it->method == GET_FROM_BUFFER
18807 || it->method == GET_FROM_DISPLAY_VECTOR
18808 || it->method == GET_FROM_STRING);
18809
18810 /* We need to save the current buffer/string position, so it will be
18811 restored by pop_it, because iterate_out_of_display_property
18812 depends on that being set correctly, but some situations leave
18813 it->position not yet set when this function is called. */
18814 push_it (it, &pos);
18815
18816 if (STRINGP (prop))
18817 {
18818 if (SCHARS (prop) == 0)
18819 {
18820 pop_it (it);
18821 return 0;
18822 }
18823
18824 it->string = prop;
18825 it->string_from_prefix_prop_p = 1;
18826 it->multibyte_p = STRING_MULTIBYTE (it->string);
18827 it->current.overlay_string_index = -1;
18828 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18829 it->end_charpos = it->string_nchars = SCHARS (it->string);
18830 it->method = GET_FROM_STRING;
18831 it->stop_charpos = 0;
18832 it->prev_stop = 0;
18833 it->base_level_stop = 0;
18834
18835 /* Force paragraph direction to be that of the parent
18836 buffer/string. */
18837 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18838 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18839 else
18840 it->paragraph_embedding = L2R;
18841
18842 /* Set up the bidi iterator for this display string. */
18843 if (it->bidi_p)
18844 {
18845 it->bidi_it.string.lstring = it->string;
18846 it->bidi_it.string.s = NULL;
18847 it->bidi_it.string.schars = it->end_charpos;
18848 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18849 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18850 it->bidi_it.string.unibyte = !it->multibyte_p;
18851 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18852 }
18853 }
18854 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18855 {
18856 it->method = GET_FROM_STRETCH;
18857 it->object = prop;
18858 }
18859 #ifdef HAVE_WINDOW_SYSTEM
18860 else if (IMAGEP (prop))
18861 {
18862 it->what = IT_IMAGE;
18863 it->image_id = lookup_image (it->f, prop);
18864 it->method = GET_FROM_IMAGE;
18865 }
18866 #endif /* HAVE_WINDOW_SYSTEM */
18867 else
18868 {
18869 pop_it (it); /* bogus display property, give up */
18870 return 0;
18871 }
18872
18873 return 1;
18874 }
18875
18876 /* Return the character-property PROP at the current position in IT. */
18877
18878 static Lisp_Object
18879 get_it_property (struct it *it, Lisp_Object prop)
18880 {
18881 Lisp_Object position;
18882
18883 if (STRINGP (it->object))
18884 position = make_number (IT_STRING_CHARPOS (*it));
18885 else if (BUFFERP (it->object))
18886 position = make_number (IT_CHARPOS (*it));
18887 else
18888 return Qnil;
18889
18890 return Fget_char_property (position, prop, it->object);
18891 }
18892
18893 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18894
18895 static void
18896 handle_line_prefix (struct it *it)
18897 {
18898 Lisp_Object prefix;
18899
18900 if (it->continuation_lines_width > 0)
18901 {
18902 prefix = get_it_property (it, Qwrap_prefix);
18903 if (NILP (prefix))
18904 prefix = Vwrap_prefix;
18905 }
18906 else
18907 {
18908 prefix = get_it_property (it, Qline_prefix);
18909 if (NILP (prefix))
18910 prefix = Vline_prefix;
18911 }
18912 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18913 {
18914 /* If the prefix is wider than the window, and we try to wrap
18915 it, it would acquire its own wrap prefix, and so on till the
18916 iterator stack overflows. So, don't wrap the prefix. */
18917 it->line_wrap = TRUNCATE;
18918 it->avoid_cursor_p = 1;
18919 }
18920 }
18921
18922 \f
18923
18924 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18925 only for R2L lines from display_line and display_string, when they
18926 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18927 the line/string needs to be continued on the next glyph row. */
18928 static void
18929 unproduce_glyphs (struct it *it, int n)
18930 {
18931 struct glyph *glyph, *end;
18932
18933 eassert (it->glyph_row);
18934 eassert (it->glyph_row->reversed_p);
18935 eassert (it->area == TEXT_AREA);
18936 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18937
18938 if (n > it->glyph_row->used[TEXT_AREA])
18939 n = it->glyph_row->used[TEXT_AREA];
18940 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18941 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18942 for ( ; glyph < end; glyph++)
18943 glyph[-n] = *glyph;
18944 }
18945
18946 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18947 and ROW->maxpos. */
18948 static void
18949 find_row_edges (struct it *it, struct glyph_row *row,
18950 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18951 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18952 {
18953 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18954 lines' rows is implemented for bidi-reordered rows. */
18955
18956 /* ROW->minpos is the value of min_pos, the minimal buffer position
18957 we have in ROW, or ROW->start.pos if that is smaller. */
18958 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18959 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18960 else
18961 /* We didn't find buffer positions smaller than ROW->start, or
18962 didn't find _any_ valid buffer positions in any of the glyphs,
18963 so we must trust the iterator's computed positions. */
18964 row->minpos = row->start.pos;
18965 if (max_pos <= 0)
18966 {
18967 max_pos = CHARPOS (it->current.pos);
18968 max_bpos = BYTEPOS (it->current.pos);
18969 }
18970
18971 /* Here are the various use-cases for ending the row, and the
18972 corresponding values for ROW->maxpos:
18973
18974 Line ends in a newline from buffer eol_pos + 1
18975 Line is continued from buffer max_pos + 1
18976 Line is truncated on right it->current.pos
18977 Line ends in a newline from string max_pos + 1(*)
18978 (*) + 1 only when line ends in a forward scan
18979 Line is continued from string max_pos
18980 Line is continued from display vector max_pos
18981 Line is entirely from a string min_pos == max_pos
18982 Line is entirely from a display vector min_pos == max_pos
18983 Line that ends at ZV ZV
18984
18985 If you discover other use-cases, please add them here as
18986 appropriate. */
18987 if (row->ends_at_zv_p)
18988 row->maxpos = it->current.pos;
18989 else if (row->used[TEXT_AREA])
18990 {
18991 int seen_this_string = 0;
18992 struct glyph_row *r1 = row - 1;
18993
18994 /* Did we see the same display string on the previous row? */
18995 if (STRINGP (it->object)
18996 /* this is not the first row */
18997 && row > it->w->desired_matrix->rows
18998 /* previous row is not the header line */
18999 && !r1->mode_line_p
19000 /* previous row also ends in a newline from a string */
19001 && r1->ends_in_newline_from_string_p)
19002 {
19003 struct glyph *start, *end;
19004
19005 /* Search for the last glyph of the previous row that came
19006 from buffer or string. Depending on whether the row is
19007 L2R or R2L, we need to process it front to back or the
19008 other way round. */
19009 if (!r1->reversed_p)
19010 {
19011 start = r1->glyphs[TEXT_AREA];
19012 end = start + r1->used[TEXT_AREA];
19013 /* Glyphs inserted by redisplay have an integer (zero)
19014 as their object. */
19015 while (end > start
19016 && INTEGERP ((end - 1)->object)
19017 && (end - 1)->charpos <= 0)
19018 --end;
19019 if (end > start)
19020 {
19021 if (EQ ((end - 1)->object, it->object))
19022 seen_this_string = 1;
19023 }
19024 else
19025 /* If all the glyphs of the previous row were inserted
19026 by redisplay, it means the previous row was
19027 produced from a single newline, which is only
19028 possible if that newline came from the same string
19029 as the one which produced this ROW. */
19030 seen_this_string = 1;
19031 }
19032 else
19033 {
19034 end = r1->glyphs[TEXT_AREA] - 1;
19035 start = end + r1->used[TEXT_AREA];
19036 while (end < start
19037 && INTEGERP ((end + 1)->object)
19038 && (end + 1)->charpos <= 0)
19039 ++end;
19040 if (end < start)
19041 {
19042 if (EQ ((end + 1)->object, it->object))
19043 seen_this_string = 1;
19044 }
19045 else
19046 seen_this_string = 1;
19047 }
19048 }
19049 /* Take note of each display string that covers a newline only
19050 once, the first time we see it. This is for when a display
19051 string includes more than one newline in it. */
19052 if (row->ends_in_newline_from_string_p && !seen_this_string)
19053 {
19054 /* If we were scanning the buffer forward when we displayed
19055 the string, we want to account for at least one buffer
19056 position that belongs to this row (position covered by
19057 the display string), so that cursor positioning will
19058 consider this row as a candidate when point is at the end
19059 of the visual line represented by this row. This is not
19060 required when scanning back, because max_pos will already
19061 have a much larger value. */
19062 if (CHARPOS (row->end.pos) > max_pos)
19063 INC_BOTH (max_pos, max_bpos);
19064 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19065 }
19066 else if (CHARPOS (it->eol_pos) > 0)
19067 SET_TEXT_POS (row->maxpos,
19068 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19069 else if (row->continued_p)
19070 {
19071 /* If max_pos is different from IT's current position, it
19072 means IT->method does not belong to the display element
19073 at max_pos. However, it also means that the display
19074 element at max_pos was displayed in its entirety on this
19075 line, which is equivalent to saying that the next line
19076 starts at the next buffer position. */
19077 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19078 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19079 else
19080 {
19081 INC_BOTH (max_pos, max_bpos);
19082 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19083 }
19084 }
19085 else if (row->truncated_on_right_p)
19086 /* display_line already called reseat_at_next_visible_line_start,
19087 which puts the iterator at the beginning of the next line, in
19088 the logical order. */
19089 row->maxpos = it->current.pos;
19090 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19091 /* A line that is entirely from a string/image/stretch... */
19092 row->maxpos = row->minpos;
19093 else
19094 emacs_abort ();
19095 }
19096 else
19097 row->maxpos = it->current.pos;
19098 }
19099
19100 /* Construct the glyph row IT->glyph_row in the desired matrix of
19101 IT->w from text at the current position of IT. See dispextern.h
19102 for an overview of struct it. Value is non-zero if
19103 IT->glyph_row displays text, as opposed to a line displaying ZV
19104 only. */
19105
19106 static int
19107 display_line (struct it *it)
19108 {
19109 struct glyph_row *row = it->glyph_row;
19110 Lisp_Object overlay_arrow_string;
19111 struct it wrap_it;
19112 void *wrap_data = NULL;
19113 int may_wrap = 0, wrap_x IF_LINT (= 0);
19114 int wrap_row_used = -1;
19115 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19116 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19117 int wrap_row_extra_line_spacing IF_LINT (= 0);
19118 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19119 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19120 int cvpos;
19121 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19122 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19123
19124 /* We always start displaying at hpos zero even if hscrolled. */
19125 eassert (it->hpos == 0 && it->current_x == 0);
19126
19127 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19128 >= it->w->desired_matrix->nrows)
19129 {
19130 it->w->nrows_scale_factor++;
19131 fonts_changed_p = 1;
19132 return 0;
19133 }
19134
19135 /* Is IT->w showing the region? */
19136 it->w->region_showing = it->region_beg_charpos > 0 ? -1 : 0;
19137
19138 /* Clear the result glyph row and enable it. */
19139 prepare_desired_row (row);
19140
19141 row->y = it->current_y;
19142 row->start = it->start;
19143 row->continuation_lines_width = it->continuation_lines_width;
19144 row->displays_text_p = 1;
19145 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19146 it->starts_in_middle_of_char_p = 0;
19147
19148 /* Arrange the overlays nicely for our purposes. Usually, we call
19149 display_line on only one line at a time, in which case this
19150 can't really hurt too much, or we call it on lines which appear
19151 one after another in the buffer, in which case all calls to
19152 recenter_overlay_lists but the first will be pretty cheap. */
19153 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19154
19155 /* Move over display elements that are not visible because we are
19156 hscrolled. This may stop at an x-position < IT->first_visible_x
19157 if the first glyph is partially visible or if we hit a line end. */
19158 if (it->current_x < it->first_visible_x)
19159 {
19160 enum move_it_result move_result;
19161
19162 this_line_min_pos = row->start.pos;
19163 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19164 MOVE_TO_POS | MOVE_TO_X);
19165 /* If we are under a large hscroll, move_it_in_display_line_to
19166 could hit the end of the line without reaching
19167 it->first_visible_x. Pretend that we did reach it. This is
19168 especially important on a TTY, where we will call
19169 extend_face_to_end_of_line, which needs to know how many
19170 blank glyphs to produce. */
19171 if (it->current_x < it->first_visible_x
19172 && (move_result == MOVE_NEWLINE_OR_CR
19173 || move_result == MOVE_POS_MATCH_OR_ZV))
19174 it->current_x = it->first_visible_x;
19175
19176 /* Record the smallest positions seen while we moved over
19177 display elements that are not visible. This is needed by
19178 redisplay_internal for optimizing the case where the cursor
19179 stays inside the same line. The rest of this function only
19180 considers positions that are actually displayed, so
19181 RECORD_MAX_MIN_POS will not otherwise record positions that
19182 are hscrolled to the left of the left edge of the window. */
19183 min_pos = CHARPOS (this_line_min_pos);
19184 min_bpos = BYTEPOS (this_line_min_pos);
19185 }
19186 else
19187 {
19188 /* We only do this when not calling `move_it_in_display_line_to'
19189 above, because move_it_in_display_line_to calls
19190 handle_line_prefix itself. */
19191 handle_line_prefix (it);
19192 }
19193
19194 /* Get the initial row height. This is either the height of the
19195 text hscrolled, if there is any, or zero. */
19196 row->ascent = it->max_ascent;
19197 row->height = it->max_ascent + it->max_descent;
19198 row->phys_ascent = it->max_phys_ascent;
19199 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19200 row->extra_line_spacing = it->max_extra_line_spacing;
19201
19202 /* Utility macro to record max and min buffer positions seen until now. */
19203 #define RECORD_MAX_MIN_POS(IT) \
19204 do \
19205 { \
19206 int composition_p = !STRINGP ((IT)->string) \
19207 && ((IT)->what == IT_COMPOSITION); \
19208 ptrdiff_t current_pos = \
19209 composition_p ? (IT)->cmp_it.charpos \
19210 : IT_CHARPOS (*(IT)); \
19211 ptrdiff_t current_bpos = \
19212 composition_p ? CHAR_TO_BYTE (current_pos) \
19213 : IT_BYTEPOS (*(IT)); \
19214 if (current_pos < min_pos) \
19215 { \
19216 min_pos = current_pos; \
19217 min_bpos = current_bpos; \
19218 } \
19219 if (IT_CHARPOS (*it) > max_pos) \
19220 { \
19221 max_pos = IT_CHARPOS (*it); \
19222 max_bpos = IT_BYTEPOS (*it); \
19223 } \
19224 } \
19225 while (0)
19226
19227 /* Loop generating characters. The loop is left with IT on the next
19228 character to display. */
19229 while (1)
19230 {
19231 int n_glyphs_before, hpos_before, x_before;
19232 int x, nglyphs;
19233 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19234
19235 /* Retrieve the next thing to display. Value is zero if end of
19236 buffer reached. */
19237 if (!get_next_display_element (it))
19238 {
19239 /* Maybe add a space at the end of this line that is used to
19240 display the cursor there under X. Set the charpos of the
19241 first glyph of blank lines not corresponding to any text
19242 to -1. */
19243 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19244 row->exact_window_width_line_p = 1;
19245 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19246 || row->used[TEXT_AREA] == 0)
19247 {
19248 row->glyphs[TEXT_AREA]->charpos = -1;
19249 row->displays_text_p = 0;
19250
19251 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19252 && (!MINI_WINDOW_P (it->w)
19253 || (minibuf_level && EQ (it->window, minibuf_window))))
19254 row->indicate_empty_line_p = 1;
19255 }
19256
19257 it->continuation_lines_width = 0;
19258 row->ends_at_zv_p = 1;
19259 /* A row that displays right-to-left text must always have
19260 its last face extended all the way to the end of line,
19261 even if this row ends in ZV, because we still write to
19262 the screen left to right. We also need to extend the
19263 last face if the default face is remapped to some
19264 different face, otherwise the functions that clear
19265 portions of the screen will clear with the default face's
19266 background color. */
19267 if (row->reversed_p
19268 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19269 extend_face_to_end_of_line (it);
19270 break;
19271 }
19272
19273 /* Now, get the metrics of what we want to display. This also
19274 generates glyphs in `row' (which is IT->glyph_row). */
19275 n_glyphs_before = row->used[TEXT_AREA];
19276 x = it->current_x;
19277
19278 /* Remember the line height so far in case the next element doesn't
19279 fit on the line. */
19280 if (it->line_wrap != TRUNCATE)
19281 {
19282 ascent = it->max_ascent;
19283 descent = it->max_descent;
19284 phys_ascent = it->max_phys_ascent;
19285 phys_descent = it->max_phys_descent;
19286
19287 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19288 {
19289 if (IT_DISPLAYING_WHITESPACE (it))
19290 may_wrap = 1;
19291 else if (may_wrap)
19292 {
19293 SAVE_IT (wrap_it, *it, wrap_data);
19294 wrap_x = x;
19295 wrap_row_used = row->used[TEXT_AREA];
19296 wrap_row_ascent = row->ascent;
19297 wrap_row_height = row->height;
19298 wrap_row_phys_ascent = row->phys_ascent;
19299 wrap_row_phys_height = row->phys_height;
19300 wrap_row_extra_line_spacing = row->extra_line_spacing;
19301 wrap_row_min_pos = min_pos;
19302 wrap_row_min_bpos = min_bpos;
19303 wrap_row_max_pos = max_pos;
19304 wrap_row_max_bpos = max_bpos;
19305 may_wrap = 0;
19306 }
19307 }
19308 }
19309
19310 PRODUCE_GLYPHS (it);
19311
19312 /* If this display element was in marginal areas, continue with
19313 the next one. */
19314 if (it->area != TEXT_AREA)
19315 {
19316 row->ascent = max (row->ascent, it->max_ascent);
19317 row->height = max (row->height, it->max_ascent + it->max_descent);
19318 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19319 row->phys_height = max (row->phys_height,
19320 it->max_phys_ascent + it->max_phys_descent);
19321 row->extra_line_spacing = max (row->extra_line_spacing,
19322 it->max_extra_line_spacing);
19323 set_iterator_to_next (it, 1);
19324 continue;
19325 }
19326
19327 /* Does the display element fit on the line? If we truncate
19328 lines, we should draw past the right edge of the window. If
19329 we don't truncate, we want to stop so that we can display the
19330 continuation glyph before the right margin. If lines are
19331 continued, there are two possible strategies for characters
19332 resulting in more than 1 glyph (e.g. tabs): Display as many
19333 glyphs as possible in this line and leave the rest for the
19334 continuation line, or display the whole element in the next
19335 line. Original redisplay did the former, so we do it also. */
19336 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19337 hpos_before = it->hpos;
19338 x_before = x;
19339
19340 if (/* Not a newline. */
19341 nglyphs > 0
19342 /* Glyphs produced fit entirely in the line. */
19343 && it->current_x < it->last_visible_x)
19344 {
19345 it->hpos += nglyphs;
19346 row->ascent = max (row->ascent, it->max_ascent);
19347 row->height = max (row->height, it->max_ascent + it->max_descent);
19348 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19349 row->phys_height = max (row->phys_height,
19350 it->max_phys_ascent + it->max_phys_descent);
19351 row->extra_line_spacing = max (row->extra_line_spacing,
19352 it->max_extra_line_spacing);
19353 if (it->current_x - it->pixel_width < it->first_visible_x)
19354 row->x = x - it->first_visible_x;
19355 /* Record the maximum and minimum buffer positions seen so
19356 far in glyphs that will be displayed by this row. */
19357 if (it->bidi_p)
19358 RECORD_MAX_MIN_POS (it);
19359 }
19360 else
19361 {
19362 int i, new_x;
19363 struct glyph *glyph;
19364
19365 for (i = 0; i < nglyphs; ++i, x = new_x)
19366 {
19367 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19368 new_x = x + glyph->pixel_width;
19369
19370 if (/* Lines are continued. */
19371 it->line_wrap != TRUNCATE
19372 && (/* Glyph doesn't fit on the line. */
19373 new_x > it->last_visible_x
19374 /* Or it fits exactly on a window system frame. */
19375 || (new_x == it->last_visible_x
19376 && FRAME_WINDOW_P (it->f)
19377 && (row->reversed_p
19378 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19379 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19380 {
19381 /* End of a continued line. */
19382
19383 if (it->hpos == 0
19384 || (new_x == it->last_visible_x
19385 && FRAME_WINDOW_P (it->f)
19386 && (row->reversed_p
19387 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19388 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19389 {
19390 /* Current glyph is the only one on the line or
19391 fits exactly on the line. We must continue
19392 the line because we can't draw the cursor
19393 after the glyph. */
19394 row->continued_p = 1;
19395 it->current_x = new_x;
19396 it->continuation_lines_width += new_x;
19397 ++it->hpos;
19398 if (i == nglyphs - 1)
19399 {
19400 /* If line-wrap is on, check if a previous
19401 wrap point was found. */
19402 if (wrap_row_used > 0
19403 /* Even if there is a previous wrap
19404 point, continue the line here as
19405 usual, if (i) the previous character
19406 was a space or tab AND (ii) the
19407 current character is not. */
19408 && (!may_wrap
19409 || IT_DISPLAYING_WHITESPACE (it)))
19410 goto back_to_wrap;
19411
19412 /* Record the maximum and minimum buffer
19413 positions seen so far in glyphs that will be
19414 displayed by this row. */
19415 if (it->bidi_p)
19416 RECORD_MAX_MIN_POS (it);
19417 set_iterator_to_next (it, 1);
19418 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19419 {
19420 if (!get_next_display_element (it))
19421 {
19422 row->exact_window_width_line_p = 1;
19423 it->continuation_lines_width = 0;
19424 row->continued_p = 0;
19425 row->ends_at_zv_p = 1;
19426 }
19427 else if (ITERATOR_AT_END_OF_LINE_P (it))
19428 {
19429 row->continued_p = 0;
19430 row->exact_window_width_line_p = 1;
19431 }
19432 }
19433 }
19434 else if (it->bidi_p)
19435 RECORD_MAX_MIN_POS (it);
19436 }
19437 else if (CHAR_GLYPH_PADDING_P (*glyph)
19438 && !FRAME_WINDOW_P (it->f))
19439 {
19440 /* A padding glyph that doesn't fit on this line.
19441 This means the whole character doesn't fit
19442 on the line. */
19443 if (row->reversed_p)
19444 unproduce_glyphs (it, row->used[TEXT_AREA]
19445 - n_glyphs_before);
19446 row->used[TEXT_AREA] = n_glyphs_before;
19447
19448 /* Fill the rest of the row with continuation
19449 glyphs like in 20.x. */
19450 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19451 < row->glyphs[1 + TEXT_AREA])
19452 produce_special_glyphs (it, IT_CONTINUATION);
19453
19454 row->continued_p = 1;
19455 it->current_x = x_before;
19456 it->continuation_lines_width += x_before;
19457
19458 /* Restore the height to what it was before the
19459 element not fitting on the line. */
19460 it->max_ascent = ascent;
19461 it->max_descent = descent;
19462 it->max_phys_ascent = phys_ascent;
19463 it->max_phys_descent = phys_descent;
19464 }
19465 else if (wrap_row_used > 0)
19466 {
19467 back_to_wrap:
19468 if (row->reversed_p)
19469 unproduce_glyphs (it,
19470 row->used[TEXT_AREA] - wrap_row_used);
19471 RESTORE_IT (it, &wrap_it, wrap_data);
19472 it->continuation_lines_width += wrap_x;
19473 row->used[TEXT_AREA] = wrap_row_used;
19474 row->ascent = wrap_row_ascent;
19475 row->height = wrap_row_height;
19476 row->phys_ascent = wrap_row_phys_ascent;
19477 row->phys_height = wrap_row_phys_height;
19478 row->extra_line_spacing = wrap_row_extra_line_spacing;
19479 min_pos = wrap_row_min_pos;
19480 min_bpos = wrap_row_min_bpos;
19481 max_pos = wrap_row_max_pos;
19482 max_bpos = wrap_row_max_bpos;
19483 row->continued_p = 1;
19484 row->ends_at_zv_p = 0;
19485 row->exact_window_width_line_p = 0;
19486 it->continuation_lines_width += x;
19487
19488 /* Make sure that a non-default face is extended
19489 up to the right margin of the window. */
19490 extend_face_to_end_of_line (it);
19491 }
19492 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19493 {
19494 /* A TAB that extends past the right edge of the
19495 window. This produces a single glyph on
19496 window system frames. We leave the glyph in
19497 this row and let it fill the row, but don't
19498 consume the TAB. */
19499 if ((row->reversed_p
19500 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19501 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19502 produce_special_glyphs (it, IT_CONTINUATION);
19503 it->continuation_lines_width += it->last_visible_x;
19504 row->ends_in_middle_of_char_p = 1;
19505 row->continued_p = 1;
19506 glyph->pixel_width = it->last_visible_x - x;
19507 it->starts_in_middle_of_char_p = 1;
19508 }
19509 else
19510 {
19511 /* Something other than a TAB that draws past
19512 the right edge of the window. Restore
19513 positions to values before the element. */
19514 if (row->reversed_p)
19515 unproduce_glyphs (it, row->used[TEXT_AREA]
19516 - (n_glyphs_before + i));
19517 row->used[TEXT_AREA] = n_glyphs_before + i;
19518
19519 /* Display continuation glyphs. */
19520 it->current_x = x_before;
19521 it->continuation_lines_width += x;
19522 if (!FRAME_WINDOW_P (it->f)
19523 || (row->reversed_p
19524 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19525 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19526 produce_special_glyphs (it, IT_CONTINUATION);
19527 row->continued_p = 1;
19528
19529 extend_face_to_end_of_line (it);
19530
19531 if (nglyphs > 1 && i > 0)
19532 {
19533 row->ends_in_middle_of_char_p = 1;
19534 it->starts_in_middle_of_char_p = 1;
19535 }
19536
19537 /* Restore the height to what it was before the
19538 element not fitting on the line. */
19539 it->max_ascent = ascent;
19540 it->max_descent = descent;
19541 it->max_phys_ascent = phys_ascent;
19542 it->max_phys_descent = phys_descent;
19543 }
19544
19545 break;
19546 }
19547 else if (new_x > it->first_visible_x)
19548 {
19549 /* Increment number of glyphs actually displayed. */
19550 ++it->hpos;
19551
19552 /* Record the maximum and minimum buffer positions
19553 seen so far in glyphs that will be displayed by
19554 this row. */
19555 if (it->bidi_p)
19556 RECORD_MAX_MIN_POS (it);
19557
19558 if (x < it->first_visible_x)
19559 /* Glyph is partially visible, i.e. row starts at
19560 negative X position. */
19561 row->x = x - it->first_visible_x;
19562 }
19563 else
19564 {
19565 /* Glyph is completely off the left margin of the
19566 window. This should not happen because of the
19567 move_it_in_display_line at the start of this
19568 function, unless the text display area of the
19569 window is empty. */
19570 eassert (it->first_visible_x <= it->last_visible_x);
19571 }
19572 }
19573 /* Even if this display element produced no glyphs at all,
19574 we want to record its position. */
19575 if (it->bidi_p && nglyphs == 0)
19576 RECORD_MAX_MIN_POS (it);
19577
19578 row->ascent = max (row->ascent, it->max_ascent);
19579 row->height = max (row->height, it->max_ascent + it->max_descent);
19580 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19581 row->phys_height = max (row->phys_height,
19582 it->max_phys_ascent + it->max_phys_descent);
19583 row->extra_line_spacing = max (row->extra_line_spacing,
19584 it->max_extra_line_spacing);
19585
19586 /* End of this display line if row is continued. */
19587 if (row->continued_p || row->ends_at_zv_p)
19588 break;
19589 }
19590
19591 at_end_of_line:
19592 /* Is this a line end? If yes, we're also done, after making
19593 sure that a non-default face is extended up to the right
19594 margin of the window. */
19595 if (ITERATOR_AT_END_OF_LINE_P (it))
19596 {
19597 int used_before = row->used[TEXT_AREA];
19598
19599 row->ends_in_newline_from_string_p = STRINGP (it->object);
19600
19601 /* Add a space at the end of the line that is used to
19602 display the cursor there. */
19603 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19604 append_space_for_newline (it, 0);
19605
19606 /* Extend the face to the end of the line. */
19607 extend_face_to_end_of_line (it);
19608
19609 /* Make sure we have the position. */
19610 if (used_before == 0)
19611 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19612
19613 /* Record the position of the newline, for use in
19614 find_row_edges. */
19615 it->eol_pos = it->current.pos;
19616
19617 /* Consume the line end. This skips over invisible lines. */
19618 set_iterator_to_next (it, 1);
19619 it->continuation_lines_width = 0;
19620 break;
19621 }
19622
19623 /* Proceed with next display element. Note that this skips
19624 over lines invisible because of selective display. */
19625 set_iterator_to_next (it, 1);
19626
19627 /* If we truncate lines, we are done when the last displayed
19628 glyphs reach past the right margin of the window. */
19629 if (it->line_wrap == TRUNCATE
19630 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19631 ? (it->current_x >= it->last_visible_x)
19632 : (it->current_x > it->last_visible_x)))
19633 {
19634 /* Maybe add truncation glyphs. */
19635 if (!FRAME_WINDOW_P (it->f)
19636 || (row->reversed_p
19637 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19638 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19639 {
19640 int i, n;
19641
19642 if (!row->reversed_p)
19643 {
19644 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19645 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19646 break;
19647 }
19648 else
19649 {
19650 for (i = 0; i < row->used[TEXT_AREA]; i++)
19651 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19652 break;
19653 /* Remove any padding glyphs at the front of ROW, to
19654 make room for the truncation glyphs we will be
19655 adding below. The loop below always inserts at
19656 least one truncation glyph, so also remove the
19657 last glyph added to ROW. */
19658 unproduce_glyphs (it, i + 1);
19659 /* Adjust i for the loop below. */
19660 i = row->used[TEXT_AREA] - (i + 1);
19661 }
19662
19663 it->current_x = x_before;
19664 if (!FRAME_WINDOW_P (it->f))
19665 {
19666 for (n = row->used[TEXT_AREA]; i < n; ++i)
19667 {
19668 row->used[TEXT_AREA] = i;
19669 produce_special_glyphs (it, IT_TRUNCATION);
19670 }
19671 }
19672 else
19673 {
19674 row->used[TEXT_AREA] = i;
19675 produce_special_glyphs (it, IT_TRUNCATION);
19676 }
19677 }
19678 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19679 {
19680 /* Don't truncate if we can overflow newline into fringe. */
19681 if (!get_next_display_element (it))
19682 {
19683 it->continuation_lines_width = 0;
19684 row->ends_at_zv_p = 1;
19685 row->exact_window_width_line_p = 1;
19686 break;
19687 }
19688 if (ITERATOR_AT_END_OF_LINE_P (it))
19689 {
19690 row->exact_window_width_line_p = 1;
19691 goto at_end_of_line;
19692 }
19693 it->current_x = x_before;
19694 }
19695
19696 row->truncated_on_right_p = 1;
19697 it->continuation_lines_width = 0;
19698 reseat_at_next_visible_line_start (it, 0);
19699 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19700 it->hpos = hpos_before;
19701 break;
19702 }
19703 }
19704
19705 if (wrap_data)
19706 bidi_unshelve_cache (wrap_data, 1);
19707
19708 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19709 at the left window margin. */
19710 if (it->first_visible_x
19711 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19712 {
19713 if (!FRAME_WINDOW_P (it->f)
19714 || (row->reversed_p
19715 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19716 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19717 insert_left_trunc_glyphs (it);
19718 row->truncated_on_left_p = 1;
19719 }
19720
19721 /* Remember the position at which this line ends.
19722
19723 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19724 cannot be before the call to find_row_edges below, since that is
19725 where these positions are determined. */
19726 row->end = it->current;
19727 if (!it->bidi_p)
19728 {
19729 row->minpos = row->start.pos;
19730 row->maxpos = row->end.pos;
19731 }
19732 else
19733 {
19734 /* ROW->minpos and ROW->maxpos must be the smallest and
19735 `1 + the largest' buffer positions in ROW. But if ROW was
19736 bidi-reordered, these two positions can be anywhere in the
19737 row, so we must determine them now. */
19738 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19739 }
19740
19741 /* If the start of this line is the overlay arrow-position, then
19742 mark this glyph row as the one containing the overlay arrow.
19743 This is clearly a mess with variable size fonts. It would be
19744 better to let it be displayed like cursors under X. */
19745 if ((row->displays_text_p || !overlay_arrow_seen)
19746 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19747 !NILP (overlay_arrow_string)))
19748 {
19749 /* Overlay arrow in window redisplay is a fringe bitmap. */
19750 if (STRINGP (overlay_arrow_string))
19751 {
19752 struct glyph_row *arrow_row
19753 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19754 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19755 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19756 struct glyph *p = row->glyphs[TEXT_AREA];
19757 struct glyph *p2, *end;
19758
19759 /* Copy the arrow glyphs. */
19760 while (glyph < arrow_end)
19761 *p++ = *glyph++;
19762
19763 /* Throw away padding glyphs. */
19764 p2 = p;
19765 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19766 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19767 ++p2;
19768 if (p2 > p)
19769 {
19770 while (p2 < end)
19771 *p++ = *p2++;
19772 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19773 }
19774 }
19775 else
19776 {
19777 eassert (INTEGERP (overlay_arrow_string));
19778 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19779 }
19780 overlay_arrow_seen = 1;
19781 }
19782
19783 /* Highlight trailing whitespace. */
19784 if (!NILP (Vshow_trailing_whitespace))
19785 highlight_trailing_whitespace (it->f, it->glyph_row);
19786
19787 /* Compute pixel dimensions of this line. */
19788 compute_line_metrics (it);
19789
19790 /* Implementation note: No changes in the glyphs of ROW or in their
19791 faces can be done past this point, because compute_line_metrics
19792 computes ROW's hash value and stores it within the glyph_row
19793 structure. */
19794
19795 /* Record whether this row ends inside an ellipsis. */
19796 row->ends_in_ellipsis_p
19797 = (it->method == GET_FROM_DISPLAY_VECTOR
19798 && it->ellipsis_p);
19799
19800 /* Save fringe bitmaps in this row. */
19801 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19802 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19803 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19804 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19805
19806 it->left_user_fringe_bitmap = 0;
19807 it->left_user_fringe_face_id = 0;
19808 it->right_user_fringe_bitmap = 0;
19809 it->right_user_fringe_face_id = 0;
19810
19811 /* Maybe set the cursor. */
19812 cvpos = it->w->cursor.vpos;
19813 if ((cvpos < 0
19814 /* In bidi-reordered rows, keep checking for proper cursor
19815 position even if one has been found already, because buffer
19816 positions in such rows change non-linearly with ROW->VPOS,
19817 when a line is continued. One exception: when we are at ZV,
19818 display cursor on the first suitable glyph row, since all
19819 the empty rows after that also have their position set to ZV. */
19820 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19821 lines' rows is implemented for bidi-reordered rows. */
19822 || (it->bidi_p
19823 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19824 && PT >= MATRIX_ROW_START_CHARPOS (row)
19825 && PT <= MATRIX_ROW_END_CHARPOS (row)
19826 && cursor_row_p (row))
19827 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19828
19829 /* Prepare for the next line. This line starts horizontally at (X
19830 HPOS) = (0 0). Vertical positions are incremented. As a
19831 convenience for the caller, IT->glyph_row is set to the next
19832 row to be used. */
19833 it->current_x = it->hpos = 0;
19834 it->current_y += row->height;
19835 SET_TEXT_POS (it->eol_pos, 0, 0);
19836 ++it->vpos;
19837 ++it->glyph_row;
19838 /* The next row should by default use the same value of the
19839 reversed_p flag as this one. set_iterator_to_next decides when
19840 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19841 the flag accordingly. */
19842 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19843 it->glyph_row->reversed_p = row->reversed_p;
19844 it->start = row->end;
19845 return row->displays_text_p;
19846
19847 #undef RECORD_MAX_MIN_POS
19848 }
19849
19850 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19851 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19852 doc: /* Return paragraph direction at point in BUFFER.
19853 Value is either `left-to-right' or `right-to-left'.
19854 If BUFFER is omitted or nil, it defaults to the current buffer.
19855
19856 Paragraph direction determines how the text in the paragraph is displayed.
19857 In left-to-right paragraphs, text begins at the left margin of the window
19858 and the reading direction is generally left to right. In right-to-left
19859 paragraphs, text begins at the right margin and is read from right to left.
19860
19861 See also `bidi-paragraph-direction'. */)
19862 (Lisp_Object buffer)
19863 {
19864 struct buffer *buf = current_buffer;
19865 struct buffer *old = buf;
19866
19867 if (! NILP (buffer))
19868 {
19869 CHECK_BUFFER (buffer);
19870 buf = XBUFFER (buffer);
19871 }
19872
19873 if (NILP (BVAR (buf, bidi_display_reordering))
19874 || NILP (BVAR (buf, enable_multibyte_characters))
19875 /* When we are loading loadup.el, the character property tables
19876 needed for bidi iteration are not yet available. */
19877 || !NILP (Vpurify_flag))
19878 return Qleft_to_right;
19879 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19880 return BVAR (buf, bidi_paragraph_direction);
19881 else
19882 {
19883 /* Determine the direction from buffer text. We could try to
19884 use current_matrix if it is up to date, but this seems fast
19885 enough as it is. */
19886 struct bidi_it itb;
19887 ptrdiff_t pos = BUF_PT (buf);
19888 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19889 int c;
19890 void *itb_data = bidi_shelve_cache ();
19891
19892 set_buffer_temp (buf);
19893 /* bidi_paragraph_init finds the base direction of the paragraph
19894 by searching forward from paragraph start. We need the base
19895 direction of the current or _previous_ paragraph, so we need
19896 to make sure we are within that paragraph. To that end, find
19897 the previous non-empty line. */
19898 if (pos >= ZV && pos > BEGV)
19899 {
19900 pos--;
19901 bytepos = CHAR_TO_BYTE (pos);
19902 }
19903 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19904 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19905 {
19906 while ((c = FETCH_BYTE (bytepos)) == '\n'
19907 || c == ' ' || c == '\t' || c == '\f')
19908 {
19909 if (bytepos <= BEGV_BYTE)
19910 break;
19911 bytepos--;
19912 pos--;
19913 }
19914 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19915 bytepos--;
19916 }
19917 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19918 itb.paragraph_dir = NEUTRAL_DIR;
19919 itb.string.s = NULL;
19920 itb.string.lstring = Qnil;
19921 itb.string.bufpos = 0;
19922 itb.string.unibyte = 0;
19923 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19924 bidi_unshelve_cache (itb_data, 0);
19925 set_buffer_temp (old);
19926 switch (itb.paragraph_dir)
19927 {
19928 case L2R:
19929 return Qleft_to_right;
19930 break;
19931 case R2L:
19932 return Qright_to_left;
19933 break;
19934 default:
19935 emacs_abort ();
19936 }
19937 }
19938 }
19939
19940
19941 \f
19942 /***********************************************************************
19943 Menu Bar
19944 ***********************************************************************/
19945
19946 /* Redisplay the menu bar in the frame for window W.
19947
19948 The menu bar of X frames that don't have X toolkit support is
19949 displayed in a special window W->frame->menu_bar_window.
19950
19951 The menu bar of terminal frames is treated specially as far as
19952 glyph matrices are concerned. Menu bar lines are not part of
19953 windows, so the update is done directly on the frame matrix rows
19954 for the menu bar. */
19955
19956 static void
19957 display_menu_bar (struct window *w)
19958 {
19959 struct frame *f = XFRAME (WINDOW_FRAME (w));
19960 struct it it;
19961 Lisp_Object items;
19962 int i;
19963
19964 /* Don't do all this for graphical frames. */
19965 #ifdef HAVE_NTGUI
19966 if (FRAME_W32_P (f))
19967 return;
19968 #endif
19969 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19970 if (FRAME_X_P (f))
19971 return;
19972 #endif
19973
19974 #ifdef HAVE_NS
19975 if (FRAME_NS_P (f))
19976 return;
19977 #endif /* HAVE_NS */
19978
19979 #ifdef USE_X_TOOLKIT
19980 eassert (!FRAME_WINDOW_P (f));
19981 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19982 it.first_visible_x = 0;
19983 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19984 #else /* not USE_X_TOOLKIT */
19985 if (FRAME_WINDOW_P (f))
19986 {
19987 /* Menu bar lines are displayed in the desired matrix of the
19988 dummy window menu_bar_window. */
19989 struct window *menu_w;
19990 eassert (WINDOWP (f->menu_bar_window));
19991 menu_w = XWINDOW (f->menu_bar_window);
19992 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19993 MENU_FACE_ID);
19994 it.first_visible_x = 0;
19995 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19996 }
19997 else
19998 {
19999 /* This is a TTY frame, i.e. character hpos/vpos are used as
20000 pixel x/y. */
20001 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20002 MENU_FACE_ID);
20003 it.first_visible_x = 0;
20004 it.last_visible_x = FRAME_COLS (f);
20005 }
20006 #endif /* not USE_X_TOOLKIT */
20007
20008 /* FIXME: This should be controlled by a user option. See the
20009 comments in redisplay_tool_bar and display_mode_line about
20010 this. */
20011 it.paragraph_embedding = L2R;
20012
20013 /* Clear all rows of the menu bar. */
20014 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20015 {
20016 struct glyph_row *row = it.glyph_row + i;
20017 clear_glyph_row (row);
20018 row->enabled_p = 1;
20019 row->full_width_p = 1;
20020 }
20021
20022 /* Display all items of the menu bar. */
20023 items = FRAME_MENU_BAR_ITEMS (it.f);
20024 for (i = 0; i < ASIZE (items); i += 4)
20025 {
20026 Lisp_Object string;
20027
20028 /* Stop at nil string. */
20029 string = AREF (items, i + 1);
20030 if (NILP (string))
20031 break;
20032
20033 /* Remember where item was displayed. */
20034 ASET (items, i + 3, make_number (it.hpos));
20035
20036 /* Display the item, pad with one space. */
20037 if (it.current_x < it.last_visible_x)
20038 display_string (NULL, string, Qnil, 0, 0, &it,
20039 SCHARS (string) + 1, 0, 0, -1);
20040 }
20041
20042 /* Fill out the line with spaces. */
20043 if (it.current_x < it.last_visible_x)
20044 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20045
20046 /* Compute the total height of the lines. */
20047 compute_line_metrics (&it);
20048 }
20049
20050
20051 \f
20052 /***********************************************************************
20053 Mode Line
20054 ***********************************************************************/
20055
20056 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20057 FORCE is non-zero, redisplay mode lines unconditionally.
20058 Otherwise, redisplay only mode lines that are garbaged. Value is
20059 the number of windows whose mode lines were redisplayed. */
20060
20061 static int
20062 redisplay_mode_lines (Lisp_Object window, int force)
20063 {
20064 int nwindows = 0;
20065
20066 while (!NILP (window))
20067 {
20068 struct window *w = XWINDOW (window);
20069
20070 if (WINDOWP (w->hchild))
20071 nwindows += redisplay_mode_lines (w->hchild, force);
20072 else if (WINDOWP (w->vchild))
20073 nwindows += redisplay_mode_lines (w->vchild, force);
20074 else if (force
20075 || FRAME_GARBAGED_P (XFRAME (w->frame))
20076 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20077 {
20078 struct text_pos lpoint;
20079 struct buffer *old = current_buffer;
20080
20081 /* Set the window's buffer for the mode line display. */
20082 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20083 set_buffer_internal_1 (XBUFFER (w->buffer));
20084
20085 /* Point refers normally to the selected window. For any
20086 other window, set up appropriate value. */
20087 if (!EQ (window, selected_window))
20088 {
20089 struct text_pos pt;
20090
20091 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20092 if (CHARPOS (pt) < BEGV)
20093 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20094 else if (CHARPOS (pt) > (ZV - 1))
20095 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20096 else
20097 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20098 }
20099
20100 /* Display mode lines. */
20101 clear_glyph_matrix (w->desired_matrix);
20102 if (display_mode_lines (w))
20103 {
20104 ++nwindows;
20105 w->must_be_updated_p = 1;
20106 }
20107
20108 /* Restore old settings. */
20109 set_buffer_internal_1 (old);
20110 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20111 }
20112
20113 window = w->next;
20114 }
20115
20116 return nwindows;
20117 }
20118
20119
20120 /* Display the mode and/or header line of window W. Value is the
20121 sum number of mode lines and header lines displayed. */
20122
20123 static int
20124 display_mode_lines (struct window *w)
20125 {
20126 Lisp_Object old_selected_window = selected_window;
20127 Lisp_Object old_selected_frame = selected_frame;
20128 Lisp_Object new_frame = w->frame;
20129 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20130 int n = 0;
20131
20132 selected_frame = new_frame;
20133 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20134 or window's point, then we'd need select_window_1 here as well. */
20135 XSETWINDOW (selected_window, w);
20136 XFRAME (new_frame)->selected_window = selected_window;
20137
20138 /* These will be set while the mode line specs are processed. */
20139 line_number_displayed = 0;
20140 w->column_number_displayed = -1;
20141
20142 if (WINDOW_WANTS_MODELINE_P (w))
20143 {
20144 struct window *sel_w = XWINDOW (old_selected_window);
20145
20146 /* Select mode line face based on the real selected window. */
20147 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20148 BVAR (current_buffer, mode_line_format));
20149 ++n;
20150 }
20151
20152 if (WINDOW_WANTS_HEADER_LINE_P (w))
20153 {
20154 display_mode_line (w, HEADER_LINE_FACE_ID,
20155 BVAR (current_buffer, header_line_format));
20156 ++n;
20157 }
20158
20159 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20160 selected_frame = old_selected_frame;
20161 selected_window = old_selected_window;
20162 return n;
20163 }
20164
20165
20166 /* Display mode or header line of window W. FACE_ID specifies which
20167 line to display; it is either MODE_LINE_FACE_ID or
20168 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20169 display. Value is the pixel height of the mode/header line
20170 displayed. */
20171
20172 static int
20173 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20174 {
20175 struct it it;
20176 struct face *face;
20177 ptrdiff_t count = SPECPDL_INDEX ();
20178
20179 init_iterator (&it, w, -1, -1, NULL, face_id);
20180 /* Don't extend on a previously drawn mode-line.
20181 This may happen if called from pos_visible_p. */
20182 it.glyph_row->enabled_p = 0;
20183 prepare_desired_row (it.glyph_row);
20184
20185 it.glyph_row->mode_line_p = 1;
20186
20187 /* FIXME: This should be controlled by a user option. But
20188 supporting such an option is not trivial, since the mode line is
20189 made up of many separate strings. */
20190 it.paragraph_embedding = L2R;
20191
20192 record_unwind_protect (unwind_format_mode_line,
20193 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20194
20195 mode_line_target = MODE_LINE_DISPLAY;
20196
20197 /* Temporarily make frame's keyboard the current kboard so that
20198 kboard-local variables in the mode_line_format will get the right
20199 values. */
20200 push_kboard (FRAME_KBOARD (it.f));
20201 record_unwind_save_match_data ();
20202 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20203 pop_kboard ();
20204
20205 unbind_to (count, Qnil);
20206
20207 /* Fill up with spaces. */
20208 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20209
20210 compute_line_metrics (&it);
20211 it.glyph_row->full_width_p = 1;
20212 it.glyph_row->continued_p = 0;
20213 it.glyph_row->truncated_on_left_p = 0;
20214 it.glyph_row->truncated_on_right_p = 0;
20215
20216 /* Make a 3D mode-line have a shadow at its right end. */
20217 face = FACE_FROM_ID (it.f, face_id);
20218 extend_face_to_end_of_line (&it);
20219 if (face->box != FACE_NO_BOX)
20220 {
20221 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20222 + it.glyph_row->used[TEXT_AREA] - 1);
20223 last->right_box_line_p = 1;
20224 }
20225
20226 return it.glyph_row->height;
20227 }
20228
20229 /* Move element ELT in LIST to the front of LIST.
20230 Return the updated list. */
20231
20232 static Lisp_Object
20233 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20234 {
20235 register Lisp_Object tail, prev;
20236 register Lisp_Object tem;
20237
20238 tail = list;
20239 prev = Qnil;
20240 while (CONSP (tail))
20241 {
20242 tem = XCAR (tail);
20243
20244 if (EQ (elt, tem))
20245 {
20246 /* Splice out the link TAIL. */
20247 if (NILP (prev))
20248 list = XCDR (tail);
20249 else
20250 Fsetcdr (prev, XCDR (tail));
20251
20252 /* Now make it the first. */
20253 Fsetcdr (tail, list);
20254 return tail;
20255 }
20256 else
20257 prev = tail;
20258 tail = XCDR (tail);
20259 QUIT;
20260 }
20261
20262 /* Not found--return unchanged LIST. */
20263 return list;
20264 }
20265
20266 /* Contribute ELT to the mode line for window IT->w. How it
20267 translates into text depends on its data type.
20268
20269 IT describes the display environment in which we display, as usual.
20270
20271 DEPTH is the depth in recursion. It is used to prevent
20272 infinite recursion here.
20273
20274 FIELD_WIDTH is the number of characters the display of ELT should
20275 occupy in the mode line, and PRECISION is the maximum number of
20276 characters to display from ELT's representation. See
20277 display_string for details.
20278
20279 Returns the hpos of the end of the text generated by ELT.
20280
20281 PROPS is a property list to add to any string we encounter.
20282
20283 If RISKY is nonzero, remove (disregard) any properties in any string
20284 we encounter, and ignore :eval and :propertize.
20285
20286 The global variable `mode_line_target' determines whether the
20287 output is passed to `store_mode_line_noprop',
20288 `store_mode_line_string', or `display_string'. */
20289
20290 static int
20291 display_mode_element (struct it *it, int depth, int field_width, int precision,
20292 Lisp_Object elt, Lisp_Object props, int risky)
20293 {
20294 int n = 0, field, prec;
20295 int literal = 0;
20296
20297 tail_recurse:
20298 if (depth > 100)
20299 elt = build_string ("*too-deep*");
20300
20301 depth++;
20302
20303 switch (XTYPE (elt))
20304 {
20305 case Lisp_String:
20306 {
20307 /* A string: output it and check for %-constructs within it. */
20308 unsigned char c;
20309 ptrdiff_t offset = 0;
20310
20311 if (SCHARS (elt) > 0
20312 && (!NILP (props) || risky))
20313 {
20314 Lisp_Object oprops, aelt;
20315 oprops = Ftext_properties_at (make_number (0), elt);
20316
20317 /* If the starting string's properties are not what
20318 we want, translate the string. Also, if the string
20319 is risky, do that anyway. */
20320
20321 if (NILP (Fequal (props, oprops)) || risky)
20322 {
20323 /* If the starting string has properties,
20324 merge the specified ones onto the existing ones. */
20325 if (! NILP (oprops) && !risky)
20326 {
20327 Lisp_Object tem;
20328
20329 oprops = Fcopy_sequence (oprops);
20330 tem = props;
20331 while (CONSP (tem))
20332 {
20333 oprops = Fplist_put (oprops, XCAR (tem),
20334 XCAR (XCDR (tem)));
20335 tem = XCDR (XCDR (tem));
20336 }
20337 props = oprops;
20338 }
20339
20340 aelt = Fassoc (elt, mode_line_proptrans_alist);
20341 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20342 {
20343 /* AELT is what we want. Move it to the front
20344 without consing. */
20345 elt = XCAR (aelt);
20346 mode_line_proptrans_alist
20347 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20348 }
20349 else
20350 {
20351 Lisp_Object tem;
20352
20353 /* If AELT has the wrong props, it is useless.
20354 so get rid of it. */
20355 if (! NILP (aelt))
20356 mode_line_proptrans_alist
20357 = Fdelq (aelt, mode_line_proptrans_alist);
20358
20359 elt = Fcopy_sequence (elt);
20360 Fset_text_properties (make_number (0), Flength (elt),
20361 props, elt);
20362 /* Add this item to mode_line_proptrans_alist. */
20363 mode_line_proptrans_alist
20364 = Fcons (Fcons (elt, props),
20365 mode_line_proptrans_alist);
20366 /* Truncate mode_line_proptrans_alist
20367 to at most 50 elements. */
20368 tem = Fnthcdr (make_number (50),
20369 mode_line_proptrans_alist);
20370 if (! NILP (tem))
20371 XSETCDR (tem, Qnil);
20372 }
20373 }
20374 }
20375
20376 offset = 0;
20377
20378 if (literal)
20379 {
20380 prec = precision - n;
20381 switch (mode_line_target)
20382 {
20383 case MODE_LINE_NOPROP:
20384 case MODE_LINE_TITLE:
20385 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20386 break;
20387 case MODE_LINE_STRING:
20388 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20389 break;
20390 case MODE_LINE_DISPLAY:
20391 n += display_string (NULL, elt, Qnil, 0, 0, it,
20392 0, prec, 0, STRING_MULTIBYTE (elt));
20393 break;
20394 }
20395
20396 break;
20397 }
20398
20399 /* Handle the non-literal case. */
20400
20401 while ((precision <= 0 || n < precision)
20402 && SREF (elt, offset) != 0
20403 && (mode_line_target != MODE_LINE_DISPLAY
20404 || it->current_x < it->last_visible_x))
20405 {
20406 ptrdiff_t last_offset = offset;
20407
20408 /* Advance to end of string or next format specifier. */
20409 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20410 ;
20411
20412 if (offset - 1 != last_offset)
20413 {
20414 ptrdiff_t nchars, nbytes;
20415
20416 /* Output to end of string or up to '%'. Field width
20417 is length of string. Don't output more than
20418 PRECISION allows us. */
20419 offset--;
20420
20421 prec = c_string_width (SDATA (elt) + last_offset,
20422 offset - last_offset, precision - n,
20423 &nchars, &nbytes);
20424
20425 switch (mode_line_target)
20426 {
20427 case MODE_LINE_NOPROP:
20428 case MODE_LINE_TITLE:
20429 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20430 break;
20431 case MODE_LINE_STRING:
20432 {
20433 ptrdiff_t bytepos = last_offset;
20434 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20435 ptrdiff_t endpos = (precision <= 0
20436 ? string_byte_to_char (elt, offset)
20437 : charpos + nchars);
20438
20439 n += store_mode_line_string (NULL,
20440 Fsubstring (elt, make_number (charpos),
20441 make_number (endpos)),
20442 0, 0, 0, Qnil);
20443 }
20444 break;
20445 case MODE_LINE_DISPLAY:
20446 {
20447 ptrdiff_t bytepos = last_offset;
20448 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20449
20450 if (precision <= 0)
20451 nchars = string_byte_to_char (elt, offset) - charpos;
20452 n += display_string (NULL, elt, Qnil, 0, charpos,
20453 it, 0, nchars, 0,
20454 STRING_MULTIBYTE (elt));
20455 }
20456 break;
20457 }
20458 }
20459 else /* c == '%' */
20460 {
20461 ptrdiff_t percent_position = offset;
20462
20463 /* Get the specified minimum width. Zero means
20464 don't pad. */
20465 field = 0;
20466 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20467 field = field * 10 + c - '0';
20468
20469 /* Don't pad beyond the total padding allowed. */
20470 if (field_width - n > 0 && field > field_width - n)
20471 field = field_width - n;
20472
20473 /* Note that either PRECISION <= 0 or N < PRECISION. */
20474 prec = precision - n;
20475
20476 if (c == 'M')
20477 n += display_mode_element (it, depth, field, prec,
20478 Vglobal_mode_string, props,
20479 risky);
20480 else if (c != 0)
20481 {
20482 int multibyte;
20483 ptrdiff_t bytepos, charpos;
20484 const char *spec;
20485 Lisp_Object string;
20486
20487 bytepos = percent_position;
20488 charpos = (STRING_MULTIBYTE (elt)
20489 ? string_byte_to_char (elt, bytepos)
20490 : bytepos);
20491 spec = decode_mode_spec (it->w, c, field, &string);
20492 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20493
20494 switch (mode_line_target)
20495 {
20496 case MODE_LINE_NOPROP:
20497 case MODE_LINE_TITLE:
20498 n += store_mode_line_noprop (spec, field, prec);
20499 break;
20500 case MODE_LINE_STRING:
20501 {
20502 Lisp_Object tem = build_string (spec);
20503 props = Ftext_properties_at (make_number (charpos), elt);
20504 /* Should only keep face property in props */
20505 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20506 }
20507 break;
20508 case MODE_LINE_DISPLAY:
20509 {
20510 int nglyphs_before, nwritten;
20511
20512 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20513 nwritten = display_string (spec, string, elt,
20514 charpos, 0, it,
20515 field, prec, 0,
20516 multibyte);
20517
20518 /* Assign to the glyphs written above the
20519 string where the `%x' came from, position
20520 of the `%'. */
20521 if (nwritten > 0)
20522 {
20523 struct glyph *glyph
20524 = (it->glyph_row->glyphs[TEXT_AREA]
20525 + nglyphs_before);
20526 int i;
20527
20528 for (i = 0; i < nwritten; ++i)
20529 {
20530 glyph[i].object = elt;
20531 glyph[i].charpos = charpos;
20532 }
20533
20534 n += nwritten;
20535 }
20536 }
20537 break;
20538 }
20539 }
20540 else /* c == 0 */
20541 break;
20542 }
20543 }
20544 }
20545 break;
20546
20547 case Lisp_Symbol:
20548 /* A symbol: process the value of the symbol recursively
20549 as if it appeared here directly. Avoid error if symbol void.
20550 Special case: if value of symbol is a string, output the string
20551 literally. */
20552 {
20553 register Lisp_Object tem;
20554
20555 /* If the variable is not marked as risky to set
20556 then its contents are risky to use. */
20557 if (NILP (Fget (elt, Qrisky_local_variable)))
20558 risky = 1;
20559
20560 tem = Fboundp (elt);
20561 if (!NILP (tem))
20562 {
20563 tem = Fsymbol_value (elt);
20564 /* If value is a string, output that string literally:
20565 don't check for % within it. */
20566 if (STRINGP (tem))
20567 literal = 1;
20568
20569 if (!EQ (tem, elt))
20570 {
20571 /* Give up right away for nil or t. */
20572 elt = tem;
20573 goto tail_recurse;
20574 }
20575 }
20576 }
20577 break;
20578
20579 case Lisp_Cons:
20580 {
20581 register Lisp_Object car, tem;
20582
20583 /* A cons cell: five distinct cases.
20584 If first element is :eval or :propertize, do something special.
20585 If first element is a string or a cons, process all the elements
20586 and effectively concatenate them.
20587 If first element is a negative number, truncate displaying cdr to
20588 at most that many characters. If positive, pad (with spaces)
20589 to at least that many characters.
20590 If first element is a symbol, process the cadr or caddr recursively
20591 according to whether the symbol's value is non-nil or nil. */
20592 car = XCAR (elt);
20593 if (EQ (car, QCeval))
20594 {
20595 /* An element of the form (:eval FORM) means evaluate FORM
20596 and use the result as mode line elements. */
20597
20598 if (risky)
20599 break;
20600
20601 if (CONSP (XCDR (elt)))
20602 {
20603 Lisp_Object spec;
20604 spec = safe_eval (XCAR (XCDR (elt)));
20605 n += display_mode_element (it, depth, field_width - n,
20606 precision - n, spec, props,
20607 risky);
20608 }
20609 }
20610 else if (EQ (car, QCpropertize))
20611 {
20612 /* An element of the form (:propertize ELT PROPS...)
20613 means display ELT but applying properties PROPS. */
20614
20615 if (risky)
20616 break;
20617
20618 if (CONSP (XCDR (elt)))
20619 n += display_mode_element (it, depth, field_width - n,
20620 precision - n, XCAR (XCDR (elt)),
20621 XCDR (XCDR (elt)), risky);
20622 }
20623 else if (SYMBOLP (car))
20624 {
20625 tem = Fboundp (car);
20626 elt = XCDR (elt);
20627 if (!CONSP (elt))
20628 goto invalid;
20629 /* elt is now the cdr, and we know it is a cons cell.
20630 Use its car if CAR has a non-nil value. */
20631 if (!NILP (tem))
20632 {
20633 tem = Fsymbol_value (car);
20634 if (!NILP (tem))
20635 {
20636 elt = XCAR (elt);
20637 goto tail_recurse;
20638 }
20639 }
20640 /* Symbol's value is nil (or symbol is unbound)
20641 Get the cddr of the original list
20642 and if possible find the caddr and use that. */
20643 elt = XCDR (elt);
20644 if (NILP (elt))
20645 break;
20646 else if (!CONSP (elt))
20647 goto invalid;
20648 elt = XCAR (elt);
20649 goto tail_recurse;
20650 }
20651 else if (INTEGERP (car))
20652 {
20653 register int lim = XINT (car);
20654 elt = XCDR (elt);
20655 if (lim < 0)
20656 {
20657 /* Negative int means reduce maximum width. */
20658 if (precision <= 0)
20659 precision = -lim;
20660 else
20661 precision = min (precision, -lim);
20662 }
20663 else if (lim > 0)
20664 {
20665 /* Padding specified. Don't let it be more than
20666 current maximum. */
20667 if (precision > 0)
20668 lim = min (precision, lim);
20669
20670 /* If that's more padding than already wanted, queue it.
20671 But don't reduce padding already specified even if
20672 that is beyond the current truncation point. */
20673 field_width = max (lim, field_width);
20674 }
20675 goto tail_recurse;
20676 }
20677 else if (STRINGP (car) || CONSP (car))
20678 {
20679 Lisp_Object halftail = elt;
20680 int len = 0;
20681
20682 while (CONSP (elt)
20683 && (precision <= 0 || n < precision))
20684 {
20685 n += display_mode_element (it, depth,
20686 /* Do padding only after the last
20687 element in the list. */
20688 (! CONSP (XCDR (elt))
20689 ? field_width - n
20690 : 0),
20691 precision - n, XCAR (elt),
20692 props, risky);
20693 elt = XCDR (elt);
20694 len++;
20695 if ((len & 1) == 0)
20696 halftail = XCDR (halftail);
20697 /* Check for cycle. */
20698 if (EQ (halftail, elt))
20699 break;
20700 }
20701 }
20702 }
20703 break;
20704
20705 default:
20706 invalid:
20707 elt = build_string ("*invalid*");
20708 goto tail_recurse;
20709 }
20710
20711 /* Pad to FIELD_WIDTH. */
20712 if (field_width > 0 && n < field_width)
20713 {
20714 switch (mode_line_target)
20715 {
20716 case MODE_LINE_NOPROP:
20717 case MODE_LINE_TITLE:
20718 n += store_mode_line_noprop ("", field_width - n, 0);
20719 break;
20720 case MODE_LINE_STRING:
20721 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20722 break;
20723 case MODE_LINE_DISPLAY:
20724 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20725 0, 0, 0);
20726 break;
20727 }
20728 }
20729
20730 return n;
20731 }
20732
20733 /* Store a mode-line string element in mode_line_string_list.
20734
20735 If STRING is non-null, display that C string. Otherwise, the Lisp
20736 string LISP_STRING is displayed.
20737
20738 FIELD_WIDTH is the minimum number of output glyphs to produce.
20739 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20740 with spaces. FIELD_WIDTH <= 0 means don't pad.
20741
20742 PRECISION is the maximum number of characters to output from
20743 STRING. PRECISION <= 0 means don't truncate the string.
20744
20745 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20746 properties to the string.
20747
20748 PROPS are the properties to add to the string.
20749 The mode_line_string_face face property is always added to the string.
20750 */
20751
20752 static int
20753 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20754 int field_width, int precision, Lisp_Object props)
20755 {
20756 ptrdiff_t len;
20757 int n = 0;
20758
20759 if (string != NULL)
20760 {
20761 len = strlen (string);
20762 if (precision > 0 && len > precision)
20763 len = precision;
20764 lisp_string = make_string (string, len);
20765 if (NILP (props))
20766 props = mode_line_string_face_prop;
20767 else if (!NILP (mode_line_string_face))
20768 {
20769 Lisp_Object face = Fplist_get (props, Qface);
20770 props = Fcopy_sequence (props);
20771 if (NILP (face))
20772 face = mode_line_string_face;
20773 else
20774 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20775 props = Fplist_put (props, Qface, face);
20776 }
20777 Fadd_text_properties (make_number (0), make_number (len),
20778 props, lisp_string);
20779 }
20780 else
20781 {
20782 len = XFASTINT (Flength (lisp_string));
20783 if (precision > 0 && len > precision)
20784 {
20785 len = precision;
20786 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20787 precision = -1;
20788 }
20789 if (!NILP (mode_line_string_face))
20790 {
20791 Lisp_Object face;
20792 if (NILP (props))
20793 props = Ftext_properties_at (make_number (0), lisp_string);
20794 face = Fplist_get (props, Qface);
20795 if (NILP (face))
20796 face = mode_line_string_face;
20797 else
20798 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20799 props = Fcons (Qface, Fcons (face, Qnil));
20800 if (copy_string)
20801 lisp_string = Fcopy_sequence (lisp_string);
20802 }
20803 if (!NILP (props))
20804 Fadd_text_properties (make_number (0), make_number (len),
20805 props, lisp_string);
20806 }
20807
20808 if (len > 0)
20809 {
20810 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20811 n += len;
20812 }
20813
20814 if (field_width > len)
20815 {
20816 field_width -= len;
20817 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20818 if (!NILP (props))
20819 Fadd_text_properties (make_number (0), make_number (field_width),
20820 props, lisp_string);
20821 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20822 n += field_width;
20823 }
20824
20825 return n;
20826 }
20827
20828
20829 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20830 1, 4, 0,
20831 doc: /* Format a string out of a mode line format specification.
20832 First arg FORMAT specifies the mode line format (see `mode-line-format'
20833 for details) to use.
20834
20835 By default, the format is evaluated for the currently selected window.
20836
20837 Optional second arg FACE specifies the face property to put on all
20838 characters for which no face is specified. The value nil means the
20839 default face. The value t means whatever face the window's mode line
20840 currently uses (either `mode-line' or `mode-line-inactive',
20841 depending on whether the window is the selected window or not).
20842 An integer value means the value string has no text
20843 properties.
20844
20845 Optional third and fourth args WINDOW and BUFFER specify the window
20846 and buffer to use as the context for the formatting (defaults
20847 are the selected window and the WINDOW's buffer). */)
20848 (Lisp_Object format, Lisp_Object face,
20849 Lisp_Object window, Lisp_Object buffer)
20850 {
20851 struct it it;
20852 int len;
20853 struct window *w;
20854 struct buffer *old_buffer = NULL;
20855 int face_id;
20856 int no_props = INTEGERP (face);
20857 ptrdiff_t count = SPECPDL_INDEX ();
20858 Lisp_Object str;
20859 int string_start = 0;
20860
20861 w = decode_any_window (window);
20862 XSETWINDOW (window, w);
20863
20864 if (NILP (buffer))
20865 buffer = w->buffer;
20866 CHECK_BUFFER (buffer);
20867
20868 /* Make formatting the modeline a non-op when noninteractive, otherwise
20869 there will be problems later caused by a partially initialized frame. */
20870 if (NILP (format) || noninteractive)
20871 return empty_unibyte_string;
20872
20873 if (no_props)
20874 face = Qnil;
20875
20876 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20877 : EQ (face, Qt) ? (EQ (window, selected_window)
20878 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20879 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20880 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20881 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20882 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20883 : DEFAULT_FACE_ID;
20884
20885 old_buffer = current_buffer;
20886
20887 /* Save things including mode_line_proptrans_alist,
20888 and set that to nil so that we don't alter the outer value. */
20889 record_unwind_protect (unwind_format_mode_line,
20890 format_mode_line_unwind_data
20891 (XFRAME (WINDOW_FRAME (w)),
20892 old_buffer, selected_window, 1));
20893 mode_line_proptrans_alist = Qnil;
20894
20895 Fselect_window (window, Qt);
20896 set_buffer_internal_1 (XBUFFER (buffer));
20897
20898 init_iterator (&it, w, -1, -1, NULL, face_id);
20899
20900 if (no_props)
20901 {
20902 mode_line_target = MODE_LINE_NOPROP;
20903 mode_line_string_face_prop = Qnil;
20904 mode_line_string_list = Qnil;
20905 string_start = MODE_LINE_NOPROP_LEN (0);
20906 }
20907 else
20908 {
20909 mode_line_target = MODE_LINE_STRING;
20910 mode_line_string_list = Qnil;
20911 mode_line_string_face = face;
20912 mode_line_string_face_prop
20913 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20914 }
20915
20916 push_kboard (FRAME_KBOARD (it.f));
20917 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20918 pop_kboard ();
20919
20920 if (no_props)
20921 {
20922 len = MODE_LINE_NOPROP_LEN (string_start);
20923 str = make_string (mode_line_noprop_buf + string_start, len);
20924 }
20925 else
20926 {
20927 mode_line_string_list = Fnreverse (mode_line_string_list);
20928 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20929 empty_unibyte_string);
20930 }
20931
20932 unbind_to (count, Qnil);
20933 return str;
20934 }
20935
20936 /* Write a null-terminated, right justified decimal representation of
20937 the positive integer D to BUF using a minimal field width WIDTH. */
20938
20939 static void
20940 pint2str (register char *buf, register int width, register ptrdiff_t d)
20941 {
20942 register char *p = buf;
20943
20944 if (d <= 0)
20945 *p++ = '0';
20946 else
20947 {
20948 while (d > 0)
20949 {
20950 *p++ = d % 10 + '0';
20951 d /= 10;
20952 }
20953 }
20954
20955 for (width -= (int) (p - buf); width > 0; --width)
20956 *p++ = ' ';
20957 *p-- = '\0';
20958 while (p > buf)
20959 {
20960 d = *buf;
20961 *buf++ = *p;
20962 *p-- = d;
20963 }
20964 }
20965
20966 /* Write a null-terminated, right justified decimal and "human
20967 readable" representation of the nonnegative integer D to BUF using
20968 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20969
20970 static const char power_letter[] =
20971 {
20972 0, /* no letter */
20973 'k', /* kilo */
20974 'M', /* mega */
20975 'G', /* giga */
20976 'T', /* tera */
20977 'P', /* peta */
20978 'E', /* exa */
20979 'Z', /* zetta */
20980 'Y' /* yotta */
20981 };
20982
20983 static void
20984 pint2hrstr (char *buf, int width, ptrdiff_t d)
20985 {
20986 /* We aim to represent the nonnegative integer D as
20987 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20988 ptrdiff_t quotient = d;
20989 int remainder = 0;
20990 /* -1 means: do not use TENTHS. */
20991 int tenths = -1;
20992 int exponent = 0;
20993
20994 /* Length of QUOTIENT.TENTHS as a string. */
20995 int length;
20996
20997 char * psuffix;
20998 char * p;
20999
21000 if (1000 <= quotient)
21001 {
21002 /* Scale to the appropriate EXPONENT. */
21003 do
21004 {
21005 remainder = quotient % 1000;
21006 quotient /= 1000;
21007 exponent++;
21008 }
21009 while (1000 <= quotient);
21010
21011 /* Round to nearest and decide whether to use TENTHS or not. */
21012 if (quotient <= 9)
21013 {
21014 tenths = remainder / 100;
21015 if (50 <= remainder % 100)
21016 {
21017 if (tenths < 9)
21018 tenths++;
21019 else
21020 {
21021 quotient++;
21022 if (quotient == 10)
21023 tenths = -1;
21024 else
21025 tenths = 0;
21026 }
21027 }
21028 }
21029 else
21030 if (500 <= remainder)
21031 {
21032 if (quotient < 999)
21033 quotient++;
21034 else
21035 {
21036 quotient = 1;
21037 exponent++;
21038 tenths = 0;
21039 }
21040 }
21041 }
21042
21043 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21044 if (tenths == -1 && quotient <= 99)
21045 if (quotient <= 9)
21046 length = 1;
21047 else
21048 length = 2;
21049 else
21050 length = 3;
21051 p = psuffix = buf + max (width, length);
21052
21053 /* Print EXPONENT. */
21054 *psuffix++ = power_letter[exponent];
21055 *psuffix = '\0';
21056
21057 /* Print TENTHS. */
21058 if (tenths >= 0)
21059 {
21060 *--p = '0' + tenths;
21061 *--p = '.';
21062 }
21063
21064 /* Print QUOTIENT. */
21065 do
21066 {
21067 int digit = quotient % 10;
21068 *--p = '0' + digit;
21069 }
21070 while ((quotient /= 10) != 0);
21071
21072 /* Print leading spaces. */
21073 while (buf < p)
21074 *--p = ' ';
21075 }
21076
21077 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21078 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21079 type of CODING_SYSTEM. Return updated pointer into BUF. */
21080
21081 static unsigned char invalid_eol_type[] = "(*invalid*)";
21082
21083 static char *
21084 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21085 {
21086 Lisp_Object val;
21087 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21088 const unsigned char *eol_str;
21089 int eol_str_len;
21090 /* The EOL conversion we are using. */
21091 Lisp_Object eoltype;
21092
21093 val = CODING_SYSTEM_SPEC (coding_system);
21094 eoltype = Qnil;
21095
21096 if (!VECTORP (val)) /* Not yet decided. */
21097 {
21098 *buf++ = multibyte ? '-' : ' ';
21099 if (eol_flag)
21100 eoltype = eol_mnemonic_undecided;
21101 /* Don't mention EOL conversion if it isn't decided. */
21102 }
21103 else
21104 {
21105 Lisp_Object attrs;
21106 Lisp_Object eolvalue;
21107
21108 attrs = AREF (val, 0);
21109 eolvalue = AREF (val, 2);
21110
21111 *buf++ = multibyte
21112 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21113 : ' ';
21114
21115 if (eol_flag)
21116 {
21117 /* The EOL conversion that is normal on this system. */
21118
21119 if (NILP (eolvalue)) /* Not yet decided. */
21120 eoltype = eol_mnemonic_undecided;
21121 else if (VECTORP (eolvalue)) /* Not yet decided. */
21122 eoltype = eol_mnemonic_undecided;
21123 else /* eolvalue is Qunix, Qdos, or Qmac. */
21124 eoltype = (EQ (eolvalue, Qunix)
21125 ? eol_mnemonic_unix
21126 : (EQ (eolvalue, Qdos) == 1
21127 ? eol_mnemonic_dos : eol_mnemonic_mac));
21128 }
21129 }
21130
21131 if (eol_flag)
21132 {
21133 /* Mention the EOL conversion if it is not the usual one. */
21134 if (STRINGP (eoltype))
21135 {
21136 eol_str = SDATA (eoltype);
21137 eol_str_len = SBYTES (eoltype);
21138 }
21139 else if (CHARACTERP (eoltype))
21140 {
21141 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21142 int c = XFASTINT (eoltype);
21143 eol_str_len = CHAR_STRING (c, tmp);
21144 eol_str = tmp;
21145 }
21146 else
21147 {
21148 eol_str = invalid_eol_type;
21149 eol_str_len = sizeof (invalid_eol_type) - 1;
21150 }
21151 memcpy (buf, eol_str, eol_str_len);
21152 buf += eol_str_len;
21153 }
21154
21155 return buf;
21156 }
21157
21158 /* Return a string for the output of a mode line %-spec for window W,
21159 generated by character C. FIELD_WIDTH > 0 means pad the string
21160 returned with spaces to that value. Return a Lisp string in
21161 *STRING if the resulting string is taken from that Lisp string.
21162
21163 Note we operate on the current buffer for most purposes. */
21164
21165 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21166
21167 static const char *
21168 decode_mode_spec (struct window *w, register int c, int field_width,
21169 Lisp_Object *string)
21170 {
21171 Lisp_Object obj;
21172 struct frame *f = XFRAME (WINDOW_FRAME (w));
21173 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21174 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21175 produce strings from numerical values, so limit preposterously
21176 large values of FIELD_WIDTH to avoid overrunning the buffer's
21177 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21178 bytes plus the terminating null. */
21179 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21180 struct buffer *b = current_buffer;
21181
21182 obj = Qnil;
21183 *string = Qnil;
21184
21185 switch (c)
21186 {
21187 case '*':
21188 if (!NILP (BVAR (b, read_only)))
21189 return "%";
21190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21191 return "*";
21192 return "-";
21193
21194 case '+':
21195 /* This differs from %* only for a modified read-only buffer. */
21196 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21197 return "*";
21198 if (!NILP (BVAR (b, read_only)))
21199 return "%";
21200 return "-";
21201
21202 case '&':
21203 /* This differs from %* in ignoring read-only-ness. */
21204 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21205 return "*";
21206 return "-";
21207
21208 case '%':
21209 return "%";
21210
21211 case '[':
21212 {
21213 int i;
21214 char *p;
21215
21216 if (command_loop_level > 5)
21217 return "[[[... ";
21218 p = decode_mode_spec_buf;
21219 for (i = 0; i < command_loop_level; i++)
21220 *p++ = '[';
21221 *p = 0;
21222 return decode_mode_spec_buf;
21223 }
21224
21225 case ']':
21226 {
21227 int i;
21228 char *p;
21229
21230 if (command_loop_level > 5)
21231 return " ...]]]";
21232 p = decode_mode_spec_buf;
21233 for (i = 0; i < command_loop_level; i++)
21234 *p++ = ']';
21235 *p = 0;
21236 return decode_mode_spec_buf;
21237 }
21238
21239 case '-':
21240 {
21241 register int i;
21242
21243 /* Let lots_of_dashes be a string of infinite length. */
21244 if (mode_line_target == MODE_LINE_NOPROP
21245 || mode_line_target == MODE_LINE_STRING)
21246 return "--";
21247 if (field_width <= 0
21248 || field_width > sizeof (lots_of_dashes))
21249 {
21250 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21251 decode_mode_spec_buf[i] = '-';
21252 decode_mode_spec_buf[i] = '\0';
21253 return decode_mode_spec_buf;
21254 }
21255 else
21256 return lots_of_dashes;
21257 }
21258
21259 case 'b':
21260 obj = BVAR (b, name);
21261 break;
21262
21263 case 'c':
21264 /* %c and %l are ignored in `frame-title-format'.
21265 (In redisplay_internal, the frame title is drawn _before_ the
21266 windows are updated, so the stuff which depends on actual
21267 window contents (such as %l) may fail to render properly, or
21268 even crash emacs.) */
21269 if (mode_line_target == MODE_LINE_TITLE)
21270 return "";
21271 else
21272 {
21273 ptrdiff_t col = current_column ();
21274 w->column_number_displayed = col;
21275 pint2str (decode_mode_spec_buf, width, col);
21276 return decode_mode_spec_buf;
21277 }
21278
21279 case 'e':
21280 #ifndef SYSTEM_MALLOC
21281 {
21282 if (NILP (Vmemory_full))
21283 return "";
21284 else
21285 return "!MEM FULL! ";
21286 }
21287 #else
21288 return "";
21289 #endif
21290
21291 case 'F':
21292 /* %F displays the frame name. */
21293 if (!NILP (f->title))
21294 return SSDATA (f->title);
21295 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21296 return SSDATA (f->name);
21297 return "Emacs";
21298
21299 case 'f':
21300 obj = BVAR (b, filename);
21301 break;
21302
21303 case 'i':
21304 {
21305 ptrdiff_t size = ZV - BEGV;
21306 pint2str (decode_mode_spec_buf, width, size);
21307 return decode_mode_spec_buf;
21308 }
21309
21310 case 'I':
21311 {
21312 ptrdiff_t size = ZV - BEGV;
21313 pint2hrstr (decode_mode_spec_buf, width, size);
21314 return decode_mode_spec_buf;
21315 }
21316
21317 case 'l':
21318 {
21319 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21320 ptrdiff_t topline, nlines, height;
21321 ptrdiff_t junk;
21322
21323 /* %c and %l are ignored in `frame-title-format'. */
21324 if (mode_line_target == MODE_LINE_TITLE)
21325 return "";
21326
21327 startpos = marker_position (w->start);
21328 startpos_byte = marker_byte_position (w->start);
21329 height = WINDOW_TOTAL_LINES (w);
21330
21331 /* If we decided that this buffer isn't suitable for line numbers,
21332 don't forget that too fast. */
21333 if (w->base_line_pos == -1)
21334 goto no_value;
21335
21336 /* If the buffer is very big, don't waste time. */
21337 if (INTEGERP (Vline_number_display_limit)
21338 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21339 {
21340 w->base_line_pos = 0;
21341 w->base_line_number = 0;
21342 goto no_value;
21343 }
21344
21345 if (w->base_line_number > 0
21346 && w->base_line_pos > 0
21347 && w->base_line_pos <= startpos)
21348 {
21349 line = w->base_line_number;
21350 linepos = w->base_line_pos;
21351 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21352 }
21353 else
21354 {
21355 line = 1;
21356 linepos = BUF_BEGV (b);
21357 linepos_byte = BUF_BEGV_BYTE (b);
21358 }
21359
21360 /* Count lines from base line to window start position. */
21361 nlines = display_count_lines (linepos_byte,
21362 startpos_byte,
21363 startpos, &junk);
21364
21365 topline = nlines + line;
21366
21367 /* Determine a new base line, if the old one is too close
21368 or too far away, or if we did not have one.
21369 "Too close" means it's plausible a scroll-down would
21370 go back past it. */
21371 if (startpos == BUF_BEGV (b))
21372 {
21373 w->base_line_number = topline;
21374 w->base_line_pos = BUF_BEGV (b);
21375 }
21376 else if (nlines < height + 25 || nlines > height * 3 + 50
21377 || linepos == BUF_BEGV (b))
21378 {
21379 ptrdiff_t limit = BUF_BEGV (b);
21380 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21381 ptrdiff_t position;
21382 ptrdiff_t distance =
21383 (height * 2 + 30) * line_number_display_limit_width;
21384
21385 if (startpos - distance > limit)
21386 {
21387 limit = startpos - distance;
21388 limit_byte = CHAR_TO_BYTE (limit);
21389 }
21390
21391 nlines = display_count_lines (startpos_byte,
21392 limit_byte,
21393 - (height * 2 + 30),
21394 &position);
21395 /* If we couldn't find the lines we wanted within
21396 line_number_display_limit_width chars per line,
21397 give up on line numbers for this window. */
21398 if (position == limit_byte && limit == startpos - distance)
21399 {
21400 w->base_line_pos = -1;
21401 w->base_line_number = 0;
21402 goto no_value;
21403 }
21404
21405 w->base_line_number = topline - nlines;
21406 w->base_line_pos = BYTE_TO_CHAR (position);
21407 }
21408
21409 /* Now count lines from the start pos to point. */
21410 nlines = display_count_lines (startpos_byte,
21411 PT_BYTE, PT, &junk);
21412
21413 /* Record that we did display the line number. */
21414 line_number_displayed = 1;
21415
21416 /* Make the string to show. */
21417 pint2str (decode_mode_spec_buf, width, topline + nlines);
21418 return decode_mode_spec_buf;
21419 no_value:
21420 {
21421 char* p = decode_mode_spec_buf;
21422 int pad = width - 2;
21423 while (pad-- > 0)
21424 *p++ = ' ';
21425 *p++ = '?';
21426 *p++ = '?';
21427 *p = '\0';
21428 return decode_mode_spec_buf;
21429 }
21430 }
21431 break;
21432
21433 case 'm':
21434 obj = BVAR (b, mode_name);
21435 break;
21436
21437 case 'n':
21438 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21439 return " Narrow";
21440 break;
21441
21442 case 'p':
21443 {
21444 ptrdiff_t pos = marker_position (w->start);
21445 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21446
21447 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21448 {
21449 if (pos <= BUF_BEGV (b))
21450 return "All";
21451 else
21452 return "Bottom";
21453 }
21454 else if (pos <= BUF_BEGV (b))
21455 return "Top";
21456 else
21457 {
21458 if (total > 1000000)
21459 /* Do it differently for a large value, to avoid overflow. */
21460 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21461 else
21462 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21463 /* We can't normally display a 3-digit number,
21464 so get us a 2-digit number that is close. */
21465 if (total == 100)
21466 total = 99;
21467 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21468 return decode_mode_spec_buf;
21469 }
21470 }
21471
21472 /* Display percentage of size above the bottom of the screen. */
21473 case 'P':
21474 {
21475 ptrdiff_t toppos = marker_position (w->start);
21476 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21477 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21478
21479 if (botpos >= BUF_ZV (b))
21480 {
21481 if (toppos <= BUF_BEGV (b))
21482 return "All";
21483 else
21484 return "Bottom";
21485 }
21486 else
21487 {
21488 if (total > 1000000)
21489 /* Do it differently for a large value, to avoid overflow. */
21490 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21491 else
21492 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21493 /* We can't normally display a 3-digit number,
21494 so get us a 2-digit number that is close. */
21495 if (total == 100)
21496 total = 99;
21497 if (toppos <= BUF_BEGV (b))
21498 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21499 else
21500 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21501 return decode_mode_spec_buf;
21502 }
21503 }
21504
21505 case 's':
21506 /* status of process */
21507 obj = Fget_buffer_process (Fcurrent_buffer ());
21508 if (NILP (obj))
21509 return "no process";
21510 #ifndef MSDOS
21511 obj = Fsymbol_name (Fprocess_status (obj));
21512 #endif
21513 break;
21514
21515 case '@':
21516 {
21517 ptrdiff_t count = inhibit_garbage_collection ();
21518 Lisp_Object val = call1 (intern ("file-remote-p"),
21519 BVAR (current_buffer, directory));
21520 unbind_to (count, Qnil);
21521
21522 if (NILP (val))
21523 return "-";
21524 else
21525 return "@";
21526 }
21527
21528 case 't': /* indicate TEXT or BINARY */
21529 return "T";
21530
21531 case 'z':
21532 /* coding-system (not including end-of-line format) */
21533 case 'Z':
21534 /* coding-system (including end-of-line type) */
21535 {
21536 int eol_flag = (c == 'Z');
21537 char *p = decode_mode_spec_buf;
21538
21539 if (! FRAME_WINDOW_P (f))
21540 {
21541 /* No need to mention EOL here--the terminal never needs
21542 to do EOL conversion. */
21543 p = decode_mode_spec_coding (CODING_ID_NAME
21544 (FRAME_KEYBOARD_CODING (f)->id),
21545 p, 0);
21546 p = decode_mode_spec_coding (CODING_ID_NAME
21547 (FRAME_TERMINAL_CODING (f)->id),
21548 p, 0);
21549 }
21550 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21551 p, eol_flag);
21552
21553 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21554 #ifdef subprocesses
21555 obj = Fget_buffer_process (Fcurrent_buffer ());
21556 if (PROCESSP (obj))
21557 {
21558 p = decode_mode_spec_coding
21559 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21560 p = decode_mode_spec_coding
21561 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21562 }
21563 #endif /* subprocesses */
21564 #endif /* 0 */
21565 *p = 0;
21566 return decode_mode_spec_buf;
21567 }
21568 }
21569
21570 if (STRINGP (obj))
21571 {
21572 *string = obj;
21573 return SSDATA (obj);
21574 }
21575 else
21576 return "";
21577 }
21578
21579
21580 /* Count up to COUNT lines starting from START_BYTE.
21581 But don't go beyond LIMIT_BYTE.
21582 Return the number of lines thus found (always nonnegative).
21583
21584 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21585
21586 static ptrdiff_t
21587 display_count_lines (ptrdiff_t start_byte,
21588 ptrdiff_t limit_byte, ptrdiff_t count,
21589 ptrdiff_t *byte_pos_ptr)
21590 {
21591 register unsigned char *cursor;
21592 unsigned char *base;
21593
21594 register ptrdiff_t ceiling;
21595 register unsigned char *ceiling_addr;
21596 ptrdiff_t orig_count = count;
21597
21598 /* If we are not in selective display mode,
21599 check only for newlines. */
21600 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21601 && !INTEGERP (BVAR (current_buffer, selective_display)));
21602
21603 if (count > 0)
21604 {
21605 while (start_byte < limit_byte)
21606 {
21607 ceiling = BUFFER_CEILING_OF (start_byte);
21608 ceiling = min (limit_byte - 1, ceiling);
21609 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21610 base = (cursor = BYTE_POS_ADDR (start_byte));
21611 while (1)
21612 {
21613 if (selective_display)
21614 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21615 ;
21616 else
21617 while (*cursor != '\n' && ++cursor != ceiling_addr)
21618 ;
21619
21620 if (cursor != ceiling_addr)
21621 {
21622 if (--count == 0)
21623 {
21624 start_byte += cursor - base + 1;
21625 *byte_pos_ptr = start_byte;
21626 return orig_count;
21627 }
21628 else
21629 if (++cursor == ceiling_addr)
21630 break;
21631 }
21632 else
21633 break;
21634 }
21635 start_byte += cursor - base;
21636 }
21637 }
21638 else
21639 {
21640 while (start_byte > limit_byte)
21641 {
21642 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21643 ceiling = max (limit_byte, ceiling);
21644 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21645 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21646 while (1)
21647 {
21648 if (selective_display)
21649 while (--cursor != ceiling_addr
21650 && *cursor != '\n' && *cursor != 015)
21651 ;
21652 else
21653 while (--cursor != ceiling_addr && *cursor != '\n')
21654 ;
21655
21656 if (cursor != ceiling_addr)
21657 {
21658 if (++count == 0)
21659 {
21660 start_byte += cursor - base + 1;
21661 *byte_pos_ptr = start_byte;
21662 /* When scanning backwards, we should
21663 not count the newline posterior to which we stop. */
21664 return - orig_count - 1;
21665 }
21666 }
21667 else
21668 break;
21669 }
21670 /* Here we add 1 to compensate for the last decrement
21671 of CURSOR, which took it past the valid range. */
21672 start_byte += cursor - base + 1;
21673 }
21674 }
21675
21676 *byte_pos_ptr = limit_byte;
21677
21678 if (count < 0)
21679 return - orig_count + count;
21680 return orig_count - count;
21681
21682 }
21683
21684
21685 \f
21686 /***********************************************************************
21687 Displaying strings
21688 ***********************************************************************/
21689
21690 /* Display a NUL-terminated string, starting with index START.
21691
21692 If STRING is non-null, display that C string. Otherwise, the Lisp
21693 string LISP_STRING is displayed. There's a case that STRING is
21694 non-null and LISP_STRING is not nil. It means STRING is a string
21695 data of LISP_STRING. In that case, we display LISP_STRING while
21696 ignoring its text properties.
21697
21698 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21699 FACE_STRING. Display STRING or LISP_STRING with the face at
21700 FACE_STRING_POS in FACE_STRING:
21701
21702 Display the string in the environment given by IT, but use the
21703 standard display table, temporarily.
21704
21705 FIELD_WIDTH is the minimum number of output glyphs to produce.
21706 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21707 with spaces. If STRING has more characters, more than FIELD_WIDTH
21708 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21709
21710 PRECISION is the maximum number of characters to output from
21711 STRING. PRECISION < 0 means don't truncate the string.
21712
21713 This is roughly equivalent to printf format specifiers:
21714
21715 FIELD_WIDTH PRECISION PRINTF
21716 ----------------------------------------
21717 -1 -1 %s
21718 -1 10 %.10s
21719 10 -1 %10s
21720 20 10 %20.10s
21721
21722 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21723 display them, and < 0 means obey the current buffer's value of
21724 enable_multibyte_characters.
21725
21726 Value is the number of columns displayed. */
21727
21728 static int
21729 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21730 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21731 int field_width, int precision, int max_x, int multibyte)
21732 {
21733 int hpos_at_start = it->hpos;
21734 int saved_face_id = it->face_id;
21735 struct glyph_row *row = it->glyph_row;
21736 ptrdiff_t it_charpos;
21737
21738 /* Initialize the iterator IT for iteration over STRING beginning
21739 with index START. */
21740 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21741 precision, field_width, multibyte);
21742 if (string && STRINGP (lisp_string))
21743 /* LISP_STRING is the one returned by decode_mode_spec. We should
21744 ignore its text properties. */
21745 it->stop_charpos = it->end_charpos;
21746
21747 /* If displaying STRING, set up the face of the iterator from
21748 FACE_STRING, if that's given. */
21749 if (STRINGP (face_string))
21750 {
21751 ptrdiff_t endptr;
21752 struct face *face;
21753
21754 it->face_id
21755 = face_at_string_position (it->w, face_string, face_string_pos,
21756 0, it->region_beg_charpos,
21757 it->region_end_charpos,
21758 &endptr, it->base_face_id, 0);
21759 face = FACE_FROM_ID (it->f, it->face_id);
21760 it->face_box_p = face->box != FACE_NO_BOX;
21761 }
21762
21763 /* Set max_x to the maximum allowed X position. Don't let it go
21764 beyond the right edge of the window. */
21765 if (max_x <= 0)
21766 max_x = it->last_visible_x;
21767 else
21768 max_x = min (max_x, it->last_visible_x);
21769
21770 /* Skip over display elements that are not visible. because IT->w is
21771 hscrolled. */
21772 if (it->current_x < it->first_visible_x)
21773 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21774 MOVE_TO_POS | MOVE_TO_X);
21775
21776 row->ascent = it->max_ascent;
21777 row->height = it->max_ascent + it->max_descent;
21778 row->phys_ascent = it->max_phys_ascent;
21779 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21780 row->extra_line_spacing = it->max_extra_line_spacing;
21781
21782 if (STRINGP (it->string))
21783 it_charpos = IT_STRING_CHARPOS (*it);
21784 else
21785 it_charpos = IT_CHARPOS (*it);
21786
21787 /* This condition is for the case that we are called with current_x
21788 past last_visible_x. */
21789 while (it->current_x < max_x)
21790 {
21791 int x_before, x, n_glyphs_before, i, nglyphs;
21792
21793 /* Get the next display element. */
21794 if (!get_next_display_element (it))
21795 break;
21796
21797 /* Produce glyphs. */
21798 x_before = it->current_x;
21799 n_glyphs_before = row->used[TEXT_AREA];
21800 PRODUCE_GLYPHS (it);
21801
21802 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21803 i = 0;
21804 x = x_before;
21805 while (i < nglyphs)
21806 {
21807 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21808
21809 if (it->line_wrap != TRUNCATE
21810 && x + glyph->pixel_width > max_x)
21811 {
21812 /* End of continued line or max_x reached. */
21813 if (CHAR_GLYPH_PADDING_P (*glyph))
21814 {
21815 /* A wide character is unbreakable. */
21816 if (row->reversed_p)
21817 unproduce_glyphs (it, row->used[TEXT_AREA]
21818 - n_glyphs_before);
21819 row->used[TEXT_AREA] = n_glyphs_before;
21820 it->current_x = x_before;
21821 }
21822 else
21823 {
21824 if (row->reversed_p)
21825 unproduce_glyphs (it, row->used[TEXT_AREA]
21826 - (n_glyphs_before + i));
21827 row->used[TEXT_AREA] = n_glyphs_before + i;
21828 it->current_x = x;
21829 }
21830 break;
21831 }
21832 else if (x + glyph->pixel_width >= it->first_visible_x)
21833 {
21834 /* Glyph is at least partially visible. */
21835 ++it->hpos;
21836 if (x < it->first_visible_x)
21837 row->x = x - it->first_visible_x;
21838 }
21839 else
21840 {
21841 /* Glyph is off the left margin of the display area.
21842 Should not happen. */
21843 emacs_abort ();
21844 }
21845
21846 row->ascent = max (row->ascent, it->max_ascent);
21847 row->height = max (row->height, it->max_ascent + it->max_descent);
21848 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21849 row->phys_height = max (row->phys_height,
21850 it->max_phys_ascent + it->max_phys_descent);
21851 row->extra_line_spacing = max (row->extra_line_spacing,
21852 it->max_extra_line_spacing);
21853 x += glyph->pixel_width;
21854 ++i;
21855 }
21856
21857 /* Stop if max_x reached. */
21858 if (i < nglyphs)
21859 break;
21860
21861 /* Stop at line ends. */
21862 if (ITERATOR_AT_END_OF_LINE_P (it))
21863 {
21864 it->continuation_lines_width = 0;
21865 break;
21866 }
21867
21868 set_iterator_to_next (it, 1);
21869 if (STRINGP (it->string))
21870 it_charpos = IT_STRING_CHARPOS (*it);
21871 else
21872 it_charpos = IT_CHARPOS (*it);
21873
21874 /* Stop if truncating at the right edge. */
21875 if (it->line_wrap == TRUNCATE
21876 && it->current_x >= it->last_visible_x)
21877 {
21878 /* Add truncation mark, but don't do it if the line is
21879 truncated at a padding space. */
21880 if (it_charpos < it->string_nchars)
21881 {
21882 if (!FRAME_WINDOW_P (it->f))
21883 {
21884 int ii, n;
21885
21886 if (it->current_x > it->last_visible_x)
21887 {
21888 if (!row->reversed_p)
21889 {
21890 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21891 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21892 break;
21893 }
21894 else
21895 {
21896 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21897 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21898 break;
21899 unproduce_glyphs (it, ii + 1);
21900 ii = row->used[TEXT_AREA] - (ii + 1);
21901 }
21902 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21903 {
21904 row->used[TEXT_AREA] = ii;
21905 produce_special_glyphs (it, IT_TRUNCATION);
21906 }
21907 }
21908 produce_special_glyphs (it, IT_TRUNCATION);
21909 }
21910 row->truncated_on_right_p = 1;
21911 }
21912 break;
21913 }
21914 }
21915
21916 /* Maybe insert a truncation at the left. */
21917 if (it->first_visible_x
21918 && it_charpos > 0)
21919 {
21920 if (!FRAME_WINDOW_P (it->f)
21921 || (row->reversed_p
21922 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21923 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21924 insert_left_trunc_glyphs (it);
21925 row->truncated_on_left_p = 1;
21926 }
21927
21928 it->face_id = saved_face_id;
21929
21930 /* Value is number of columns displayed. */
21931 return it->hpos - hpos_at_start;
21932 }
21933
21934
21935 \f
21936 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21937 appears as an element of LIST or as the car of an element of LIST.
21938 If PROPVAL is a list, compare each element against LIST in that
21939 way, and return 1/2 if any element of PROPVAL is found in LIST.
21940 Otherwise return 0. This function cannot quit.
21941 The return value is 2 if the text is invisible but with an ellipsis
21942 and 1 if it's invisible and without an ellipsis. */
21943
21944 int
21945 invisible_p (register Lisp_Object propval, Lisp_Object list)
21946 {
21947 register Lisp_Object tail, proptail;
21948
21949 for (tail = list; CONSP (tail); tail = XCDR (tail))
21950 {
21951 register Lisp_Object tem;
21952 tem = XCAR (tail);
21953 if (EQ (propval, tem))
21954 return 1;
21955 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21956 return NILP (XCDR (tem)) ? 1 : 2;
21957 }
21958
21959 if (CONSP (propval))
21960 {
21961 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21962 {
21963 Lisp_Object propelt;
21964 propelt = XCAR (proptail);
21965 for (tail = list; CONSP (tail); tail = XCDR (tail))
21966 {
21967 register Lisp_Object tem;
21968 tem = XCAR (tail);
21969 if (EQ (propelt, tem))
21970 return 1;
21971 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21972 return NILP (XCDR (tem)) ? 1 : 2;
21973 }
21974 }
21975 }
21976
21977 return 0;
21978 }
21979
21980 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21981 doc: /* Non-nil if the property makes the text invisible.
21982 POS-OR-PROP can be a marker or number, in which case it is taken to be
21983 a position in the current buffer and the value of the `invisible' property
21984 is checked; or it can be some other value, which is then presumed to be the
21985 value of the `invisible' property of the text of interest.
21986 The non-nil value returned can be t for truly invisible text or something
21987 else if the text is replaced by an ellipsis. */)
21988 (Lisp_Object pos_or_prop)
21989 {
21990 Lisp_Object prop
21991 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21992 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21993 : pos_or_prop);
21994 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21995 return (invis == 0 ? Qnil
21996 : invis == 1 ? Qt
21997 : make_number (invis));
21998 }
21999
22000 /* Calculate a width or height in pixels from a specification using
22001 the following elements:
22002
22003 SPEC ::=
22004 NUM - a (fractional) multiple of the default font width/height
22005 (NUM) - specifies exactly NUM pixels
22006 UNIT - a fixed number of pixels, see below.
22007 ELEMENT - size of a display element in pixels, see below.
22008 (NUM . SPEC) - equals NUM * SPEC
22009 (+ SPEC SPEC ...) - add pixel values
22010 (- SPEC SPEC ...) - subtract pixel values
22011 (- SPEC) - negate pixel value
22012
22013 NUM ::=
22014 INT or FLOAT - a number constant
22015 SYMBOL - use symbol's (buffer local) variable binding.
22016
22017 UNIT ::=
22018 in - pixels per inch *)
22019 mm - pixels per 1/1000 meter *)
22020 cm - pixels per 1/100 meter *)
22021 width - width of current font in pixels.
22022 height - height of current font in pixels.
22023
22024 *) using the ratio(s) defined in display-pixels-per-inch.
22025
22026 ELEMENT ::=
22027
22028 left-fringe - left fringe width in pixels
22029 right-fringe - right fringe width in pixels
22030
22031 left-margin - left margin width in pixels
22032 right-margin - right margin width in pixels
22033
22034 scroll-bar - scroll-bar area width in pixels
22035
22036 Examples:
22037
22038 Pixels corresponding to 5 inches:
22039 (5 . in)
22040
22041 Total width of non-text areas on left side of window (if scroll-bar is on left):
22042 '(space :width (+ left-fringe left-margin scroll-bar))
22043
22044 Align to first text column (in header line):
22045 '(space :align-to 0)
22046
22047 Align to middle of text area minus half the width of variable `my-image'
22048 containing a loaded image:
22049 '(space :align-to (0.5 . (- text my-image)))
22050
22051 Width of left margin minus width of 1 character in the default font:
22052 '(space :width (- left-margin 1))
22053
22054 Width of left margin minus width of 2 characters in the current font:
22055 '(space :width (- left-margin (2 . width)))
22056
22057 Center 1 character over left-margin (in header line):
22058 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22059
22060 Different ways to express width of left fringe plus left margin minus one pixel:
22061 '(space :width (- (+ left-fringe left-margin) (1)))
22062 '(space :width (+ left-fringe left-margin (- (1))))
22063 '(space :width (+ left-fringe left-margin (-1)))
22064
22065 */
22066
22067 #define NUMVAL(X) \
22068 ((INTEGERP (X) || FLOATP (X)) \
22069 ? XFLOATINT (X) \
22070 : - 1)
22071
22072 static int
22073 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22074 struct font *font, int width_p, int *align_to)
22075 {
22076 double pixels;
22077
22078 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22079 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22080
22081 if (NILP (prop))
22082 return OK_PIXELS (0);
22083
22084 eassert (FRAME_LIVE_P (it->f));
22085
22086 if (SYMBOLP (prop))
22087 {
22088 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22089 {
22090 char *unit = SSDATA (SYMBOL_NAME (prop));
22091
22092 if (unit[0] == 'i' && unit[1] == 'n')
22093 pixels = 1.0;
22094 else if (unit[0] == 'm' && unit[1] == 'm')
22095 pixels = 25.4;
22096 else if (unit[0] == 'c' && unit[1] == 'm')
22097 pixels = 2.54;
22098 else
22099 pixels = 0;
22100 if (pixels > 0)
22101 {
22102 double ppi;
22103 #ifdef HAVE_WINDOW_SYSTEM
22104 if (FRAME_WINDOW_P (it->f)
22105 && (ppi = (width_p
22106 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22107 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22108 ppi > 0))
22109 return OK_PIXELS (ppi / pixels);
22110 #endif
22111
22112 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22113 || (CONSP (Vdisplay_pixels_per_inch)
22114 && (ppi = (width_p
22115 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22116 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22117 ppi > 0)))
22118 return OK_PIXELS (ppi / pixels);
22119
22120 return 0;
22121 }
22122 }
22123
22124 #ifdef HAVE_WINDOW_SYSTEM
22125 if (EQ (prop, Qheight))
22126 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22127 if (EQ (prop, Qwidth))
22128 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22129 #else
22130 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22131 return OK_PIXELS (1);
22132 #endif
22133
22134 if (EQ (prop, Qtext))
22135 return OK_PIXELS (width_p
22136 ? window_box_width (it->w, TEXT_AREA)
22137 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22138
22139 if (align_to && *align_to < 0)
22140 {
22141 *res = 0;
22142 if (EQ (prop, Qleft))
22143 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22144 if (EQ (prop, Qright))
22145 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22146 if (EQ (prop, Qcenter))
22147 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22148 + window_box_width (it->w, TEXT_AREA) / 2);
22149 if (EQ (prop, Qleft_fringe))
22150 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22151 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22152 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22153 if (EQ (prop, Qright_fringe))
22154 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22155 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22156 : window_box_right_offset (it->w, TEXT_AREA));
22157 if (EQ (prop, Qleft_margin))
22158 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22159 if (EQ (prop, Qright_margin))
22160 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22161 if (EQ (prop, Qscroll_bar))
22162 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22163 ? 0
22164 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22165 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22166 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22167 : 0)));
22168 }
22169 else
22170 {
22171 if (EQ (prop, Qleft_fringe))
22172 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22173 if (EQ (prop, Qright_fringe))
22174 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22175 if (EQ (prop, Qleft_margin))
22176 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22177 if (EQ (prop, Qright_margin))
22178 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22179 if (EQ (prop, Qscroll_bar))
22180 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22181 }
22182
22183 prop = buffer_local_value_1 (prop, it->w->buffer);
22184 if (EQ (prop, Qunbound))
22185 prop = Qnil;
22186 }
22187
22188 if (INTEGERP (prop) || FLOATP (prop))
22189 {
22190 int base_unit = (width_p
22191 ? FRAME_COLUMN_WIDTH (it->f)
22192 : FRAME_LINE_HEIGHT (it->f));
22193 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22194 }
22195
22196 if (CONSP (prop))
22197 {
22198 Lisp_Object car = XCAR (prop);
22199 Lisp_Object cdr = XCDR (prop);
22200
22201 if (SYMBOLP (car))
22202 {
22203 #ifdef HAVE_WINDOW_SYSTEM
22204 if (FRAME_WINDOW_P (it->f)
22205 && valid_image_p (prop))
22206 {
22207 ptrdiff_t id = lookup_image (it->f, prop);
22208 struct image *img = IMAGE_FROM_ID (it->f, id);
22209
22210 return OK_PIXELS (width_p ? img->width : img->height);
22211 }
22212 #endif
22213 if (EQ (car, Qplus) || EQ (car, Qminus))
22214 {
22215 int first = 1;
22216 double px;
22217
22218 pixels = 0;
22219 while (CONSP (cdr))
22220 {
22221 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22222 font, width_p, align_to))
22223 return 0;
22224 if (first)
22225 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22226 else
22227 pixels += px;
22228 cdr = XCDR (cdr);
22229 }
22230 if (EQ (car, Qminus))
22231 pixels = -pixels;
22232 return OK_PIXELS (pixels);
22233 }
22234
22235 car = buffer_local_value_1 (car, it->w->buffer);
22236 if (EQ (car, Qunbound))
22237 car = Qnil;
22238 }
22239
22240 if (INTEGERP (car) || FLOATP (car))
22241 {
22242 double fact;
22243 pixels = XFLOATINT (car);
22244 if (NILP (cdr))
22245 return OK_PIXELS (pixels);
22246 if (calc_pixel_width_or_height (&fact, it, cdr,
22247 font, width_p, align_to))
22248 return OK_PIXELS (pixels * fact);
22249 return 0;
22250 }
22251
22252 return 0;
22253 }
22254
22255 return 0;
22256 }
22257
22258 \f
22259 /***********************************************************************
22260 Glyph Display
22261 ***********************************************************************/
22262
22263 #ifdef HAVE_WINDOW_SYSTEM
22264
22265 #ifdef GLYPH_DEBUG
22266
22267 void
22268 dump_glyph_string (struct glyph_string *s)
22269 {
22270 fprintf (stderr, "glyph string\n");
22271 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22272 s->x, s->y, s->width, s->height);
22273 fprintf (stderr, " ybase = %d\n", s->ybase);
22274 fprintf (stderr, " hl = %d\n", s->hl);
22275 fprintf (stderr, " left overhang = %d, right = %d\n",
22276 s->left_overhang, s->right_overhang);
22277 fprintf (stderr, " nchars = %d\n", s->nchars);
22278 fprintf (stderr, " extends to end of line = %d\n",
22279 s->extends_to_end_of_line_p);
22280 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22281 fprintf (stderr, " bg width = %d\n", s->background_width);
22282 }
22283
22284 #endif /* GLYPH_DEBUG */
22285
22286 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22287 of XChar2b structures for S; it can't be allocated in
22288 init_glyph_string because it must be allocated via `alloca'. W
22289 is the window on which S is drawn. ROW and AREA are the glyph row
22290 and area within the row from which S is constructed. START is the
22291 index of the first glyph structure covered by S. HL is a
22292 face-override for drawing S. */
22293
22294 #ifdef HAVE_NTGUI
22295 #define OPTIONAL_HDC(hdc) HDC hdc,
22296 #define DECLARE_HDC(hdc) HDC hdc;
22297 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22298 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22299 #endif
22300
22301 #ifndef OPTIONAL_HDC
22302 #define OPTIONAL_HDC(hdc)
22303 #define DECLARE_HDC(hdc)
22304 #define ALLOCATE_HDC(hdc, f)
22305 #define RELEASE_HDC(hdc, f)
22306 #endif
22307
22308 static void
22309 init_glyph_string (struct glyph_string *s,
22310 OPTIONAL_HDC (hdc)
22311 XChar2b *char2b, struct window *w, struct glyph_row *row,
22312 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22313 {
22314 memset (s, 0, sizeof *s);
22315 s->w = w;
22316 s->f = XFRAME (w->frame);
22317 #ifdef HAVE_NTGUI
22318 s->hdc = hdc;
22319 #endif
22320 s->display = FRAME_X_DISPLAY (s->f);
22321 s->window = FRAME_X_WINDOW (s->f);
22322 s->char2b = char2b;
22323 s->hl = hl;
22324 s->row = row;
22325 s->area = area;
22326 s->first_glyph = row->glyphs[area] + start;
22327 s->height = row->height;
22328 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22329 s->ybase = s->y + row->ascent;
22330 }
22331
22332
22333 /* Append the list of glyph strings with head H and tail T to the list
22334 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22335
22336 static void
22337 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22338 struct glyph_string *h, struct glyph_string *t)
22339 {
22340 if (h)
22341 {
22342 if (*head)
22343 (*tail)->next = h;
22344 else
22345 *head = h;
22346 h->prev = *tail;
22347 *tail = t;
22348 }
22349 }
22350
22351
22352 /* Prepend the list of glyph strings with head H and tail T to the
22353 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22354 result. */
22355
22356 static void
22357 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22358 struct glyph_string *h, struct glyph_string *t)
22359 {
22360 if (h)
22361 {
22362 if (*head)
22363 (*head)->prev = t;
22364 else
22365 *tail = t;
22366 t->next = *head;
22367 *head = h;
22368 }
22369 }
22370
22371
22372 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22373 Set *HEAD and *TAIL to the resulting list. */
22374
22375 static void
22376 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22377 struct glyph_string *s)
22378 {
22379 s->next = s->prev = NULL;
22380 append_glyph_string_lists (head, tail, s, s);
22381 }
22382
22383
22384 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22385 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22386 make sure that X resources for the face returned are allocated.
22387 Value is a pointer to a realized face that is ready for display if
22388 DISPLAY_P is non-zero. */
22389
22390 static struct face *
22391 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22392 XChar2b *char2b, int display_p)
22393 {
22394 struct face *face = FACE_FROM_ID (f, face_id);
22395
22396 if (face->font)
22397 {
22398 unsigned code = face->font->driver->encode_char (face->font, c);
22399
22400 if (code != FONT_INVALID_CODE)
22401 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22402 else
22403 STORE_XCHAR2B (char2b, 0, 0);
22404 }
22405
22406 /* Make sure X resources of the face are allocated. */
22407 #ifdef HAVE_X_WINDOWS
22408 if (display_p)
22409 #endif
22410 {
22411 eassert (face != NULL);
22412 PREPARE_FACE_FOR_DISPLAY (f, face);
22413 }
22414
22415 return face;
22416 }
22417
22418
22419 /* Get face and two-byte form of character glyph GLYPH on frame F.
22420 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22421 a pointer to a realized face that is ready for display. */
22422
22423 static struct face *
22424 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22425 XChar2b *char2b, int *two_byte_p)
22426 {
22427 struct face *face;
22428
22429 eassert (glyph->type == CHAR_GLYPH);
22430 face = FACE_FROM_ID (f, glyph->face_id);
22431
22432 if (two_byte_p)
22433 *two_byte_p = 0;
22434
22435 if (face->font)
22436 {
22437 unsigned code;
22438
22439 if (CHAR_BYTE8_P (glyph->u.ch))
22440 code = CHAR_TO_BYTE8 (glyph->u.ch);
22441 else
22442 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22443
22444 if (code != FONT_INVALID_CODE)
22445 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22446 else
22447 STORE_XCHAR2B (char2b, 0, 0);
22448 }
22449
22450 /* Make sure X resources of the face are allocated. */
22451 eassert (face != NULL);
22452 PREPARE_FACE_FOR_DISPLAY (f, face);
22453 return face;
22454 }
22455
22456
22457 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22458 Return 1 if FONT has a glyph for C, otherwise return 0. */
22459
22460 static int
22461 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22462 {
22463 unsigned code;
22464
22465 if (CHAR_BYTE8_P (c))
22466 code = CHAR_TO_BYTE8 (c);
22467 else
22468 code = font->driver->encode_char (font, c);
22469
22470 if (code == FONT_INVALID_CODE)
22471 return 0;
22472 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22473 return 1;
22474 }
22475
22476
22477 /* Fill glyph string S with composition components specified by S->cmp.
22478
22479 BASE_FACE is the base face of the composition.
22480 S->cmp_from is the index of the first component for S.
22481
22482 OVERLAPS non-zero means S should draw the foreground only, and use
22483 its physical height for clipping. See also draw_glyphs.
22484
22485 Value is the index of a component not in S. */
22486
22487 static int
22488 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22489 int overlaps)
22490 {
22491 int i;
22492 /* For all glyphs of this composition, starting at the offset
22493 S->cmp_from, until we reach the end of the definition or encounter a
22494 glyph that requires the different face, add it to S. */
22495 struct face *face;
22496
22497 eassert (s);
22498
22499 s->for_overlaps = overlaps;
22500 s->face = NULL;
22501 s->font = NULL;
22502 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22503 {
22504 int c = COMPOSITION_GLYPH (s->cmp, i);
22505
22506 /* TAB in a composition means display glyphs with padding space
22507 on the left or right. */
22508 if (c != '\t')
22509 {
22510 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22511 -1, Qnil);
22512
22513 face = get_char_face_and_encoding (s->f, c, face_id,
22514 s->char2b + i, 1);
22515 if (face)
22516 {
22517 if (! s->face)
22518 {
22519 s->face = face;
22520 s->font = s->face->font;
22521 }
22522 else if (s->face != face)
22523 break;
22524 }
22525 }
22526 ++s->nchars;
22527 }
22528 s->cmp_to = i;
22529
22530 if (s->face == NULL)
22531 {
22532 s->face = base_face->ascii_face;
22533 s->font = s->face->font;
22534 }
22535
22536 /* All glyph strings for the same composition has the same width,
22537 i.e. the width set for the first component of the composition. */
22538 s->width = s->first_glyph->pixel_width;
22539
22540 /* If the specified font could not be loaded, use the frame's
22541 default font, but record the fact that we couldn't load it in
22542 the glyph string so that we can draw rectangles for the
22543 characters of the glyph string. */
22544 if (s->font == NULL)
22545 {
22546 s->font_not_found_p = 1;
22547 s->font = FRAME_FONT (s->f);
22548 }
22549
22550 /* Adjust base line for subscript/superscript text. */
22551 s->ybase += s->first_glyph->voffset;
22552
22553 /* This glyph string must always be drawn with 16-bit functions. */
22554 s->two_byte_p = 1;
22555
22556 return s->cmp_to;
22557 }
22558
22559 static int
22560 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22561 int start, int end, int overlaps)
22562 {
22563 struct glyph *glyph, *last;
22564 Lisp_Object lgstring;
22565 int i;
22566
22567 s->for_overlaps = overlaps;
22568 glyph = s->row->glyphs[s->area] + start;
22569 last = s->row->glyphs[s->area] + end;
22570 s->cmp_id = glyph->u.cmp.id;
22571 s->cmp_from = glyph->slice.cmp.from;
22572 s->cmp_to = glyph->slice.cmp.to + 1;
22573 s->face = FACE_FROM_ID (s->f, face_id);
22574 lgstring = composition_gstring_from_id (s->cmp_id);
22575 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22576 glyph++;
22577 while (glyph < last
22578 && glyph->u.cmp.automatic
22579 && glyph->u.cmp.id == s->cmp_id
22580 && s->cmp_to == glyph->slice.cmp.from)
22581 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22582
22583 for (i = s->cmp_from; i < s->cmp_to; i++)
22584 {
22585 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22586 unsigned code = LGLYPH_CODE (lglyph);
22587
22588 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22589 }
22590 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22591 return glyph - s->row->glyphs[s->area];
22592 }
22593
22594
22595 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22596 See the comment of fill_glyph_string for arguments.
22597 Value is the index of the first glyph not in S. */
22598
22599
22600 static int
22601 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22602 int start, int end, int overlaps)
22603 {
22604 struct glyph *glyph, *last;
22605 int voffset;
22606
22607 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22608 s->for_overlaps = overlaps;
22609 glyph = s->row->glyphs[s->area] + start;
22610 last = s->row->glyphs[s->area] + end;
22611 voffset = glyph->voffset;
22612 s->face = FACE_FROM_ID (s->f, face_id);
22613 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22614 s->nchars = 1;
22615 s->width = glyph->pixel_width;
22616 glyph++;
22617 while (glyph < last
22618 && glyph->type == GLYPHLESS_GLYPH
22619 && glyph->voffset == voffset
22620 && glyph->face_id == face_id)
22621 {
22622 s->nchars++;
22623 s->width += glyph->pixel_width;
22624 glyph++;
22625 }
22626 s->ybase += voffset;
22627 return glyph - s->row->glyphs[s->area];
22628 }
22629
22630
22631 /* Fill glyph string S from a sequence of character glyphs.
22632
22633 FACE_ID is the face id of the string. START is the index of the
22634 first glyph to consider, END is the index of the last + 1.
22635 OVERLAPS non-zero means S should draw the foreground only, and use
22636 its physical height for clipping. See also draw_glyphs.
22637
22638 Value is the index of the first glyph not in S. */
22639
22640 static int
22641 fill_glyph_string (struct glyph_string *s, int face_id,
22642 int start, int end, int overlaps)
22643 {
22644 struct glyph *glyph, *last;
22645 int voffset;
22646 int glyph_not_available_p;
22647
22648 eassert (s->f == XFRAME (s->w->frame));
22649 eassert (s->nchars == 0);
22650 eassert (start >= 0 && end > start);
22651
22652 s->for_overlaps = overlaps;
22653 glyph = s->row->glyphs[s->area] + start;
22654 last = s->row->glyphs[s->area] + end;
22655 voffset = glyph->voffset;
22656 s->padding_p = glyph->padding_p;
22657 glyph_not_available_p = glyph->glyph_not_available_p;
22658
22659 while (glyph < last
22660 && glyph->type == CHAR_GLYPH
22661 && glyph->voffset == voffset
22662 /* Same face id implies same font, nowadays. */
22663 && glyph->face_id == face_id
22664 && glyph->glyph_not_available_p == glyph_not_available_p)
22665 {
22666 int two_byte_p;
22667
22668 s->face = get_glyph_face_and_encoding (s->f, glyph,
22669 s->char2b + s->nchars,
22670 &two_byte_p);
22671 s->two_byte_p = two_byte_p;
22672 ++s->nchars;
22673 eassert (s->nchars <= end - start);
22674 s->width += glyph->pixel_width;
22675 if (glyph++->padding_p != s->padding_p)
22676 break;
22677 }
22678
22679 s->font = s->face->font;
22680
22681 /* If the specified font could not be loaded, use the frame's font,
22682 but record the fact that we couldn't load it in
22683 S->font_not_found_p so that we can draw rectangles for the
22684 characters of the glyph string. */
22685 if (s->font == NULL || glyph_not_available_p)
22686 {
22687 s->font_not_found_p = 1;
22688 s->font = FRAME_FONT (s->f);
22689 }
22690
22691 /* Adjust base line for subscript/superscript text. */
22692 s->ybase += voffset;
22693
22694 eassert (s->face && s->face->gc);
22695 return glyph - s->row->glyphs[s->area];
22696 }
22697
22698
22699 /* Fill glyph string S from image glyph S->first_glyph. */
22700
22701 static void
22702 fill_image_glyph_string (struct glyph_string *s)
22703 {
22704 eassert (s->first_glyph->type == IMAGE_GLYPH);
22705 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22706 eassert (s->img);
22707 s->slice = s->first_glyph->slice.img;
22708 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22709 s->font = s->face->font;
22710 s->width = s->first_glyph->pixel_width;
22711
22712 /* Adjust base line for subscript/superscript text. */
22713 s->ybase += s->first_glyph->voffset;
22714 }
22715
22716
22717 /* Fill glyph string S from a sequence of stretch glyphs.
22718
22719 START is the index of the first glyph to consider,
22720 END is the index of the last + 1.
22721
22722 Value is the index of the first glyph not in S. */
22723
22724 static int
22725 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22726 {
22727 struct glyph *glyph, *last;
22728 int voffset, face_id;
22729
22730 eassert (s->first_glyph->type == STRETCH_GLYPH);
22731
22732 glyph = s->row->glyphs[s->area] + start;
22733 last = s->row->glyphs[s->area] + end;
22734 face_id = glyph->face_id;
22735 s->face = FACE_FROM_ID (s->f, face_id);
22736 s->font = s->face->font;
22737 s->width = glyph->pixel_width;
22738 s->nchars = 1;
22739 voffset = glyph->voffset;
22740
22741 for (++glyph;
22742 (glyph < last
22743 && glyph->type == STRETCH_GLYPH
22744 && glyph->voffset == voffset
22745 && glyph->face_id == face_id);
22746 ++glyph)
22747 s->width += glyph->pixel_width;
22748
22749 /* Adjust base line for subscript/superscript text. */
22750 s->ybase += voffset;
22751
22752 /* The case that face->gc == 0 is handled when drawing the glyph
22753 string by calling PREPARE_FACE_FOR_DISPLAY. */
22754 eassert (s->face);
22755 return glyph - s->row->glyphs[s->area];
22756 }
22757
22758 static struct font_metrics *
22759 get_per_char_metric (struct font *font, XChar2b *char2b)
22760 {
22761 static struct font_metrics metrics;
22762 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22763
22764 if (! font || code == FONT_INVALID_CODE)
22765 return NULL;
22766 font->driver->text_extents (font, &code, 1, &metrics);
22767 return &metrics;
22768 }
22769
22770 /* EXPORT for RIF:
22771 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22772 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22773 assumed to be zero. */
22774
22775 void
22776 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22777 {
22778 *left = *right = 0;
22779
22780 if (glyph->type == CHAR_GLYPH)
22781 {
22782 struct face *face;
22783 XChar2b char2b;
22784 struct font_metrics *pcm;
22785
22786 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22787 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22788 {
22789 if (pcm->rbearing > pcm->width)
22790 *right = pcm->rbearing - pcm->width;
22791 if (pcm->lbearing < 0)
22792 *left = -pcm->lbearing;
22793 }
22794 }
22795 else if (glyph->type == COMPOSITE_GLYPH)
22796 {
22797 if (! glyph->u.cmp.automatic)
22798 {
22799 struct composition *cmp = composition_table[glyph->u.cmp.id];
22800
22801 if (cmp->rbearing > cmp->pixel_width)
22802 *right = cmp->rbearing - cmp->pixel_width;
22803 if (cmp->lbearing < 0)
22804 *left = - cmp->lbearing;
22805 }
22806 else
22807 {
22808 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22809 struct font_metrics metrics;
22810
22811 composition_gstring_width (gstring, glyph->slice.cmp.from,
22812 glyph->slice.cmp.to + 1, &metrics);
22813 if (metrics.rbearing > metrics.width)
22814 *right = metrics.rbearing - metrics.width;
22815 if (metrics.lbearing < 0)
22816 *left = - metrics.lbearing;
22817 }
22818 }
22819 }
22820
22821
22822 /* Return the index of the first glyph preceding glyph string S that
22823 is overwritten by S because of S's left overhang. Value is -1
22824 if no glyphs are overwritten. */
22825
22826 static int
22827 left_overwritten (struct glyph_string *s)
22828 {
22829 int k;
22830
22831 if (s->left_overhang)
22832 {
22833 int x = 0, i;
22834 struct glyph *glyphs = s->row->glyphs[s->area];
22835 int first = s->first_glyph - glyphs;
22836
22837 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22838 x -= glyphs[i].pixel_width;
22839
22840 k = i + 1;
22841 }
22842 else
22843 k = -1;
22844
22845 return k;
22846 }
22847
22848
22849 /* Return the index of the first glyph preceding glyph string S that
22850 is overwriting S because of its right overhang. Value is -1 if no
22851 glyph in front of S overwrites S. */
22852
22853 static int
22854 left_overwriting (struct glyph_string *s)
22855 {
22856 int i, k, x;
22857 struct glyph *glyphs = s->row->glyphs[s->area];
22858 int first = s->first_glyph - glyphs;
22859
22860 k = -1;
22861 x = 0;
22862 for (i = first - 1; i >= 0; --i)
22863 {
22864 int left, right;
22865 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22866 if (x + right > 0)
22867 k = i;
22868 x -= glyphs[i].pixel_width;
22869 }
22870
22871 return k;
22872 }
22873
22874
22875 /* Return the index of the last glyph following glyph string S that is
22876 overwritten by S because of S's right overhang. Value is -1 if
22877 no such glyph is found. */
22878
22879 static int
22880 right_overwritten (struct glyph_string *s)
22881 {
22882 int k = -1;
22883
22884 if (s->right_overhang)
22885 {
22886 int x = 0, i;
22887 struct glyph *glyphs = s->row->glyphs[s->area];
22888 int first = (s->first_glyph - glyphs
22889 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22890 int end = s->row->used[s->area];
22891
22892 for (i = first; i < end && s->right_overhang > x; ++i)
22893 x += glyphs[i].pixel_width;
22894
22895 k = i;
22896 }
22897
22898 return k;
22899 }
22900
22901
22902 /* Return the index of the last glyph following glyph string S that
22903 overwrites S because of its left overhang. Value is negative
22904 if no such glyph is found. */
22905
22906 static int
22907 right_overwriting (struct glyph_string *s)
22908 {
22909 int i, k, x;
22910 int end = s->row->used[s->area];
22911 struct glyph *glyphs = s->row->glyphs[s->area];
22912 int first = (s->first_glyph - glyphs
22913 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22914
22915 k = -1;
22916 x = 0;
22917 for (i = first; i < end; ++i)
22918 {
22919 int left, right;
22920 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22921 if (x - left < 0)
22922 k = i;
22923 x += glyphs[i].pixel_width;
22924 }
22925
22926 return k;
22927 }
22928
22929
22930 /* Set background width of glyph string S. START is the index of the
22931 first glyph following S. LAST_X is the right-most x-position + 1
22932 in the drawing area. */
22933
22934 static void
22935 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22936 {
22937 /* If the face of this glyph string has to be drawn to the end of
22938 the drawing area, set S->extends_to_end_of_line_p. */
22939
22940 if (start == s->row->used[s->area]
22941 && s->area == TEXT_AREA
22942 && ((s->row->fill_line_p
22943 && (s->hl == DRAW_NORMAL_TEXT
22944 || s->hl == DRAW_IMAGE_RAISED
22945 || s->hl == DRAW_IMAGE_SUNKEN))
22946 || s->hl == DRAW_MOUSE_FACE))
22947 s->extends_to_end_of_line_p = 1;
22948
22949 /* If S extends its face to the end of the line, set its
22950 background_width to the distance to the right edge of the drawing
22951 area. */
22952 if (s->extends_to_end_of_line_p)
22953 s->background_width = last_x - s->x + 1;
22954 else
22955 s->background_width = s->width;
22956 }
22957
22958
22959 /* Compute overhangs and x-positions for glyph string S and its
22960 predecessors, or successors. X is the starting x-position for S.
22961 BACKWARD_P non-zero means process predecessors. */
22962
22963 static void
22964 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22965 {
22966 if (backward_p)
22967 {
22968 while (s)
22969 {
22970 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22971 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22972 x -= s->width;
22973 s->x = x;
22974 s = s->prev;
22975 }
22976 }
22977 else
22978 {
22979 while (s)
22980 {
22981 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22982 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22983 s->x = x;
22984 x += s->width;
22985 s = s->next;
22986 }
22987 }
22988 }
22989
22990
22991
22992 /* The following macros are only called from draw_glyphs below.
22993 They reference the following parameters of that function directly:
22994 `w', `row', `area', and `overlap_p'
22995 as well as the following local variables:
22996 `s', `f', and `hdc' (in W32) */
22997
22998 #ifdef HAVE_NTGUI
22999 /* On W32, silently add local `hdc' variable to argument list of
23000 init_glyph_string. */
23001 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23002 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23003 #else
23004 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23005 init_glyph_string (s, char2b, w, row, area, start, hl)
23006 #endif
23007
23008 /* Add a glyph string for a stretch glyph to the list of strings
23009 between HEAD and TAIL. START is the index of the stretch glyph in
23010 row area AREA of glyph row ROW. END is the index of the last glyph
23011 in that glyph row area. X is the current output position assigned
23012 to the new glyph string constructed. HL overrides that face of the
23013 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23014 is the right-most x-position of the drawing area. */
23015
23016 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23017 and below -- keep them on one line. */
23018 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23019 do \
23020 { \
23021 s = alloca (sizeof *s); \
23022 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23023 START = fill_stretch_glyph_string (s, START, END); \
23024 append_glyph_string (&HEAD, &TAIL, s); \
23025 s->x = (X); \
23026 } \
23027 while (0)
23028
23029
23030 /* Add a glyph string for an image glyph to the list of strings
23031 between HEAD and TAIL. START is the index of the image glyph in
23032 row area AREA of glyph row ROW. END is the index of the last glyph
23033 in that glyph row area. X is the current output position assigned
23034 to the new glyph string constructed. HL overrides that face of the
23035 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23036 is the right-most x-position of the drawing area. */
23037
23038 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23039 do \
23040 { \
23041 s = alloca (sizeof *s); \
23042 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23043 fill_image_glyph_string (s); \
23044 append_glyph_string (&HEAD, &TAIL, s); \
23045 ++START; \
23046 s->x = (X); \
23047 } \
23048 while (0)
23049
23050
23051 /* Add a glyph string for a sequence of character glyphs to the list
23052 of strings between HEAD and TAIL. START is the index of the first
23053 glyph in row area AREA of glyph row ROW that is part of the new
23054 glyph string. END is the index of the last glyph in that glyph row
23055 area. X is the current output position assigned to the new glyph
23056 string constructed. HL overrides that face of the glyph; e.g. it
23057 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23058 right-most x-position of the drawing area. */
23059
23060 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23061 do \
23062 { \
23063 int face_id; \
23064 XChar2b *char2b; \
23065 \
23066 face_id = (row)->glyphs[area][START].face_id; \
23067 \
23068 s = alloca (sizeof *s); \
23069 char2b = alloca ((END - START) * sizeof *char2b); \
23070 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23071 append_glyph_string (&HEAD, &TAIL, s); \
23072 s->x = (X); \
23073 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23074 } \
23075 while (0)
23076
23077
23078 /* Add a glyph string for a composite sequence to the list of strings
23079 between HEAD and TAIL. START is the index of the first glyph in
23080 row area AREA of glyph row ROW that is part of the new glyph
23081 string. END is the index of the last glyph in that glyph row area.
23082 X is the current output position assigned to the new glyph string
23083 constructed. HL overrides that face of the glyph; e.g. it is
23084 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23085 x-position of the drawing area. */
23086
23087 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23088 do { \
23089 int face_id = (row)->glyphs[area][START].face_id; \
23090 struct face *base_face = FACE_FROM_ID (f, face_id); \
23091 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23092 struct composition *cmp = composition_table[cmp_id]; \
23093 XChar2b *char2b; \
23094 struct glyph_string *first_s = NULL; \
23095 int n; \
23096 \
23097 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23098 \
23099 /* Make glyph_strings for each glyph sequence that is drawable by \
23100 the same face, and append them to HEAD/TAIL. */ \
23101 for (n = 0; n < cmp->glyph_len;) \
23102 { \
23103 s = alloca (sizeof *s); \
23104 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23105 append_glyph_string (&(HEAD), &(TAIL), s); \
23106 s->cmp = cmp; \
23107 s->cmp_from = n; \
23108 s->x = (X); \
23109 if (n == 0) \
23110 first_s = s; \
23111 n = fill_composite_glyph_string (s, base_face, overlaps); \
23112 } \
23113 \
23114 ++START; \
23115 s = first_s; \
23116 } while (0)
23117
23118
23119 /* Add a glyph string for a glyph-string sequence to the list of strings
23120 between HEAD and TAIL. */
23121
23122 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23123 do { \
23124 int face_id; \
23125 XChar2b *char2b; \
23126 Lisp_Object gstring; \
23127 \
23128 face_id = (row)->glyphs[area][START].face_id; \
23129 gstring = (composition_gstring_from_id \
23130 ((row)->glyphs[area][START].u.cmp.id)); \
23131 s = alloca (sizeof *s); \
23132 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23133 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23134 append_glyph_string (&(HEAD), &(TAIL), s); \
23135 s->x = (X); \
23136 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23137 } while (0)
23138
23139
23140 /* Add a glyph string for a sequence of glyphless character's glyphs
23141 to the list of strings between HEAD and TAIL. The meanings of
23142 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23143
23144 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23145 do \
23146 { \
23147 int face_id; \
23148 \
23149 face_id = (row)->glyphs[area][START].face_id; \
23150 \
23151 s = alloca (sizeof *s); \
23152 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23153 append_glyph_string (&HEAD, &TAIL, s); \
23154 s->x = (X); \
23155 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23156 overlaps); \
23157 } \
23158 while (0)
23159
23160
23161 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23162 of AREA of glyph row ROW on window W between indices START and END.
23163 HL overrides the face for drawing glyph strings, e.g. it is
23164 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23165 x-positions of the drawing area.
23166
23167 This is an ugly monster macro construct because we must use alloca
23168 to allocate glyph strings (because draw_glyphs can be called
23169 asynchronously). */
23170
23171 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23172 do \
23173 { \
23174 HEAD = TAIL = NULL; \
23175 while (START < END) \
23176 { \
23177 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23178 switch (first_glyph->type) \
23179 { \
23180 case CHAR_GLYPH: \
23181 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23182 HL, X, LAST_X); \
23183 break; \
23184 \
23185 case COMPOSITE_GLYPH: \
23186 if (first_glyph->u.cmp.automatic) \
23187 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23188 HL, X, LAST_X); \
23189 else \
23190 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23191 HL, X, LAST_X); \
23192 break; \
23193 \
23194 case STRETCH_GLYPH: \
23195 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23196 HL, X, LAST_X); \
23197 break; \
23198 \
23199 case IMAGE_GLYPH: \
23200 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23201 HL, X, LAST_X); \
23202 break; \
23203 \
23204 case GLYPHLESS_GLYPH: \
23205 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23206 HL, X, LAST_X); \
23207 break; \
23208 \
23209 default: \
23210 emacs_abort (); \
23211 } \
23212 \
23213 if (s) \
23214 { \
23215 set_glyph_string_background_width (s, START, LAST_X); \
23216 (X) += s->width; \
23217 } \
23218 } \
23219 } while (0)
23220
23221
23222 /* Draw glyphs between START and END in AREA of ROW on window W,
23223 starting at x-position X. X is relative to AREA in W. HL is a
23224 face-override with the following meaning:
23225
23226 DRAW_NORMAL_TEXT draw normally
23227 DRAW_CURSOR draw in cursor face
23228 DRAW_MOUSE_FACE draw in mouse face.
23229 DRAW_INVERSE_VIDEO draw in mode line face
23230 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23231 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23232
23233 If OVERLAPS is non-zero, draw only the foreground of characters and
23234 clip to the physical height of ROW. Non-zero value also defines
23235 the overlapping part to be drawn:
23236
23237 OVERLAPS_PRED overlap with preceding rows
23238 OVERLAPS_SUCC overlap with succeeding rows
23239 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23240 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23241
23242 Value is the x-position reached, relative to AREA of W. */
23243
23244 static int
23245 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23246 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23247 enum draw_glyphs_face hl, int overlaps)
23248 {
23249 struct glyph_string *head, *tail;
23250 struct glyph_string *s;
23251 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23252 int i, j, x_reached, last_x, area_left = 0;
23253 struct frame *f = XFRAME (WINDOW_FRAME (w));
23254 DECLARE_HDC (hdc);
23255
23256 ALLOCATE_HDC (hdc, f);
23257
23258 /* Let's rather be paranoid than getting a SEGV. */
23259 end = min (end, row->used[area]);
23260 start = clip_to_bounds (0, start, end);
23261
23262 /* Translate X to frame coordinates. Set last_x to the right
23263 end of the drawing area. */
23264 if (row->full_width_p)
23265 {
23266 /* X is relative to the left edge of W, without scroll bars
23267 or fringes. */
23268 area_left = WINDOW_LEFT_EDGE_X (w);
23269 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23270 }
23271 else
23272 {
23273 area_left = window_box_left (w, area);
23274 last_x = area_left + window_box_width (w, area);
23275 }
23276 x += area_left;
23277
23278 /* Build a doubly-linked list of glyph_string structures between
23279 head and tail from what we have to draw. Note that the macro
23280 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23281 the reason we use a separate variable `i'. */
23282 i = start;
23283 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23284 if (tail)
23285 x_reached = tail->x + tail->background_width;
23286 else
23287 x_reached = x;
23288
23289 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23290 the row, redraw some glyphs in front or following the glyph
23291 strings built above. */
23292 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23293 {
23294 struct glyph_string *h, *t;
23295 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23296 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23297 int check_mouse_face = 0;
23298 int dummy_x = 0;
23299
23300 /* If mouse highlighting is on, we may need to draw adjacent
23301 glyphs using mouse-face highlighting. */
23302 if (area == TEXT_AREA && row->mouse_face_p
23303 && hlinfo->mouse_face_beg_row >= 0
23304 && hlinfo->mouse_face_end_row >= 0)
23305 {
23306 struct glyph_row *mouse_beg_row, *mouse_end_row;
23307
23308 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23309 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23310
23311 if (row >= mouse_beg_row && row <= mouse_end_row)
23312 {
23313 check_mouse_face = 1;
23314 mouse_beg_col = (row == mouse_beg_row)
23315 ? hlinfo->mouse_face_beg_col : 0;
23316 mouse_end_col = (row == mouse_end_row)
23317 ? hlinfo->mouse_face_end_col
23318 : row->used[TEXT_AREA];
23319 }
23320 }
23321
23322 /* Compute overhangs for all glyph strings. */
23323 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23324 for (s = head; s; s = s->next)
23325 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23326
23327 /* Prepend glyph strings for glyphs in front of the first glyph
23328 string that are overwritten because of the first glyph
23329 string's left overhang. The background of all strings
23330 prepended must be drawn because the first glyph string
23331 draws over it. */
23332 i = left_overwritten (head);
23333 if (i >= 0)
23334 {
23335 enum draw_glyphs_face overlap_hl;
23336
23337 /* If this row contains mouse highlighting, attempt to draw
23338 the overlapped glyphs with the correct highlight. This
23339 code fails if the overlap encompasses more than one glyph
23340 and mouse-highlight spans only some of these glyphs.
23341 However, making it work perfectly involves a lot more
23342 code, and I don't know if the pathological case occurs in
23343 practice, so we'll stick to this for now. --- cyd */
23344 if (check_mouse_face
23345 && mouse_beg_col < start && mouse_end_col > i)
23346 overlap_hl = DRAW_MOUSE_FACE;
23347 else
23348 overlap_hl = DRAW_NORMAL_TEXT;
23349
23350 j = i;
23351 BUILD_GLYPH_STRINGS (j, start, h, t,
23352 overlap_hl, dummy_x, last_x);
23353 start = i;
23354 compute_overhangs_and_x (t, head->x, 1);
23355 prepend_glyph_string_lists (&head, &tail, h, t);
23356 clip_head = head;
23357 }
23358
23359 /* Prepend glyph strings for glyphs in front of the first glyph
23360 string that overwrite that glyph string because of their
23361 right overhang. For these strings, only the foreground must
23362 be drawn, because it draws over the glyph string at `head'.
23363 The background must not be drawn because this would overwrite
23364 right overhangs of preceding glyphs for which no glyph
23365 strings exist. */
23366 i = left_overwriting (head);
23367 if (i >= 0)
23368 {
23369 enum draw_glyphs_face overlap_hl;
23370
23371 if (check_mouse_face
23372 && mouse_beg_col < start && mouse_end_col > i)
23373 overlap_hl = DRAW_MOUSE_FACE;
23374 else
23375 overlap_hl = DRAW_NORMAL_TEXT;
23376
23377 clip_head = head;
23378 BUILD_GLYPH_STRINGS (i, start, h, t,
23379 overlap_hl, dummy_x, last_x);
23380 for (s = h; s; s = s->next)
23381 s->background_filled_p = 1;
23382 compute_overhangs_and_x (t, head->x, 1);
23383 prepend_glyph_string_lists (&head, &tail, h, t);
23384 }
23385
23386 /* Append glyphs strings for glyphs following the last glyph
23387 string tail that are overwritten by tail. The background of
23388 these strings has to be drawn because tail's foreground draws
23389 over it. */
23390 i = right_overwritten (tail);
23391 if (i >= 0)
23392 {
23393 enum draw_glyphs_face overlap_hl;
23394
23395 if (check_mouse_face
23396 && mouse_beg_col < i && mouse_end_col > end)
23397 overlap_hl = DRAW_MOUSE_FACE;
23398 else
23399 overlap_hl = DRAW_NORMAL_TEXT;
23400
23401 BUILD_GLYPH_STRINGS (end, i, h, t,
23402 overlap_hl, x, last_x);
23403 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23404 we don't have `end = i;' here. */
23405 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23406 append_glyph_string_lists (&head, &tail, h, t);
23407 clip_tail = tail;
23408 }
23409
23410 /* Append glyph strings for glyphs following the last glyph
23411 string tail that overwrite tail. The foreground of such
23412 glyphs has to be drawn because it writes into the background
23413 of tail. The background must not be drawn because it could
23414 paint over the foreground of following glyphs. */
23415 i = right_overwriting (tail);
23416 if (i >= 0)
23417 {
23418 enum draw_glyphs_face overlap_hl;
23419 if (check_mouse_face
23420 && mouse_beg_col < i && mouse_end_col > end)
23421 overlap_hl = DRAW_MOUSE_FACE;
23422 else
23423 overlap_hl = DRAW_NORMAL_TEXT;
23424
23425 clip_tail = tail;
23426 i++; /* We must include the Ith glyph. */
23427 BUILD_GLYPH_STRINGS (end, i, h, t,
23428 overlap_hl, x, last_x);
23429 for (s = h; s; s = s->next)
23430 s->background_filled_p = 1;
23431 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23432 append_glyph_string_lists (&head, &tail, h, t);
23433 }
23434 if (clip_head || clip_tail)
23435 for (s = head; s; s = s->next)
23436 {
23437 s->clip_head = clip_head;
23438 s->clip_tail = clip_tail;
23439 }
23440 }
23441
23442 /* Draw all strings. */
23443 for (s = head; s; s = s->next)
23444 FRAME_RIF (f)->draw_glyph_string (s);
23445
23446 #ifndef HAVE_NS
23447 /* When focus a sole frame and move horizontally, this sets on_p to 0
23448 causing a failure to erase prev cursor position. */
23449 if (area == TEXT_AREA
23450 && !row->full_width_p
23451 /* When drawing overlapping rows, only the glyph strings'
23452 foreground is drawn, which doesn't erase a cursor
23453 completely. */
23454 && !overlaps)
23455 {
23456 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23457 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23458 : (tail ? tail->x + tail->background_width : x));
23459 x0 -= area_left;
23460 x1 -= area_left;
23461
23462 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23463 row->y, MATRIX_ROW_BOTTOM_Y (row));
23464 }
23465 #endif
23466
23467 /* Value is the x-position up to which drawn, relative to AREA of W.
23468 This doesn't include parts drawn because of overhangs. */
23469 if (row->full_width_p)
23470 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23471 else
23472 x_reached -= area_left;
23473
23474 RELEASE_HDC (hdc, f);
23475
23476 return x_reached;
23477 }
23478
23479 /* Expand row matrix if too narrow. Don't expand if area
23480 is not present. */
23481
23482 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23483 { \
23484 if (!fonts_changed_p \
23485 && (it->glyph_row->glyphs[area] \
23486 < it->glyph_row->glyphs[area + 1])) \
23487 { \
23488 it->w->ncols_scale_factor++; \
23489 fonts_changed_p = 1; \
23490 } \
23491 }
23492
23493 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23494 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23495
23496 static void
23497 append_glyph (struct it *it)
23498 {
23499 struct glyph *glyph;
23500 enum glyph_row_area area = it->area;
23501
23502 eassert (it->glyph_row);
23503 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23504
23505 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23506 if (glyph < it->glyph_row->glyphs[area + 1])
23507 {
23508 /* If the glyph row is reversed, we need to prepend the glyph
23509 rather than append it. */
23510 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23511 {
23512 struct glyph *g;
23513
23514 /* Make room for the additional glyph. */
23515 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23516 g[1] = *g;
23517 glyph = it->glyph_row->glyphs[area];
23518 }
23519 glyph->charpos = CHARPOS (it->position);
23520 glyph->object = it->object;
23521 if (it->pixel_width > 0)
23522 {
23523 glyph->pixel_width = it->pixel_width;
23524 glyph->padding_p = 0;
23525 }
23526 else
23527 {
23528 /* Assure at least 1-pixel width. Otherwise, cursor can't
23529 be displayed correctly. */
23530 glyph->pixel_width = 1;
23531 glyph->padding_p = 1;
23532 }
23533 glyph->ascent = it->ascent;
23534 glyph->descent = it->descent;
23535 glyph->voffset = it->voffset;
23536 glyph->type = CHAR_GLYPH;
23537 glyph->avoid_cursor_p = it->avoid_cursor_p;
23538 glyph->multibyte_p = it->multibyte_p;
23539 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23540 {
23541 /* In R2L rows, the left and the right box edges need to be
23542 drawn in reverse direction. */
23543 glyph->right_box_line_p = it->start_of_box_run_p;
23544 glyph->left_box_line_p = it->end_of_box_run_p;
23545 }
23546 else
23547 {
23548 glyph->left_box_line_p = it->start_of_box_run_p;
23549 glyph->right_box_line_p = it->end_of_box_run_p;
23550 }
23551 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23552 || it->phys_descent > it->descent);
23553 glyph->glyph_not_available_p = it->glyph_not_available_p;
23554 glyph->face_id = it->face_id;
23555 glyph->u.ch = it->char_to_display;
23556 glyph->slice.img = null_glyph_slice;
23557 glyph->font_type = FONT_TYPE_UNKNOWN;
23558 if (it->bidi_p)
23559 {
23560 glyph->resolved_level = it->bidi_it.resolved_level;
23561 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23562 emacs_abort ();
23563 glyph->bidi_type = it->bidi_it.type;
23564 }
23565 else
23566 {
23567 glyph->resolved_level = 0;
23568 glyph->bidi_type = UNKNOWN_BT;
23569 }
23570 ++it->glyph_row->used[area];
23571 }
23572 else
23573 IT_EXPAND_MATRIX_WIDTH (it, area);
23574 }
23575
23576 /* Store one glyph for the composition IT->cmp_it.id in
23577 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23578 non-null. */
23579
23580 static void
23581 append_composite_glyph (struct it *it)
23582 {
23583 struct glyph *glyph;
23584 enum glyph_row_area area = it->area;
23585
23586 eassert (it->glyph_row);
23587
23588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23589 if (glyph < it->glyph_row->glyphs[area + 1])
23590 {
23591 /* If the glyph row is reversed, we need to prepend the glyph
23592 rather than append it. */
23593 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23594 {
23595 struct glyph *g;
23596
23597 /* Make room for the new glyph. */
23598 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23599 g[1] = *g;
23600 glyph = it->glyph_row->glyphs[it->area];
23601 }
23602 glyph->charpos = it->cmp_it.charpos;
23603 glyph->object = it->object;
23604 glyph->pixel_width = it->pixel_width;
23605 glyph->ascent = it->ascent;
23606 glyph->descent = it->descent;
23607 glyph->voffset = it->voffset;
23608 glyph->type = COMPOSITE_GLYPH;
23609 if (it->cmp_it.ch < 0)
23610 {
23611 glyph->u.cmp.automatic = 0;
23612 glyph->u.cmp.id = it->cmp_it.id;
23613 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23614 }
23615 else
23616 {
23617 glyph->u.cmp.automatic = 1;
23618 glyph->u.cmp.id = it->cmp_it.id;
23619 glyph->slice.cmp.from = it->cmp_it.from;
23620 glyph->slice.cmp.to = it->cmp_it.to - 1;
23621 }
23622 glyph->avoid_cursor_p = it->avoid_cursor_p;
23623 glyph->multibyte_p = it->multibyte_p;
23624 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23625 {
23626 /* In R2L rows, the left and the right box edges need to be
23627 drawn in reverse direction. */
23628 glyph->right_box_line_p = it->start_of_box_run_p;
23629 glyph->left_box_line_p = it->end_of_box_run_p;
23630 }
23631 else
23632 {
23633 glyph->left_box_line_p = it->start_of_box_run_p;
23634 glyph->right_box_line_p = it->end_of_box_run_p;
23635 }
23636 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23637 || it->phys_descent > it->descent);
23638 glyph->padding_p = 0;
23639 glyph->glyph_not_available_p = 0;
23640 glyph->face_id = it->face_id;
23641 glyph->font_type = FONT_TYPE_UNKNOWN;
23642 if (it->bidi_p)
23643 {
23644 glyph->resolved_level = it->bidi_it.resolved_level;
23645 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23646 emacs_abort ();
23647 glyph->bidi_type = it->bidi_it.type;
23648 }
23649 ++it->glyph_row->used[area];
23650 }
23651 else
23652 IT_EXPAND_MATRIX_WIDTH (it, area);
23653 }
23654
23655
23656 /* Change IT->ascent and IT->height according to the setting of
23657 IT->voffset. */
23658
23659 static void
23660 take_vertical_position_into_account (struct it *it)
23661 {
23662 if (it->voffset)
23663 {
23664 if (it->voffset < 0)
23665 /* Increase the ascent so that we can display the text higher
23666 in the line. */
23667 it->ascent -= it->voffset;
23668 else
23669 /* Increase the descent so that we can display the text lower
23670 in the line. */
23671 it->descent += it->voffset;
23672 }
23673 }
23674
23675
23676 /* Produce glyphs/get display metrics for the image IT is loaded with.
23677 See the description of struct display_iterator in dispextern.h for
23678 an overview of struct display_iterator. */
23679
23680 static void
23681 produce_image_glyph (struct it *it)
23682 {
23683 struct image *img;
23684 struct face *face;
23685 int glyph_ascent, crop;
23686 struct glyph_slice slice;
23687
23688 eassert (it->what == IT_IMAGE);
23689
23690 face = FACE_FROM_ID (it->f, it->face_id);
23691 eassert (face);
23692 /* Make sure X resources of the face is loaded. */
23693 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23694
23695 if (it->image_id < 0)
23696 {
23697 /* Fringe bitmap. */
23698 it->ascent = it->phys_ascent = 0;
23699 it->descent = it->phys_descent = 0;
23700 it->pixel_width = 0;
23701 it->nglyphs = 0;
23702 return;
23703 }
23704
23705 img = IMAGE_FROM_ID (it->f, it->image_id);
23706 eassert (img);
23707 /* Make sure X resources of the image is loaded. */
23708 prepare_image_for_display (it->f, img);
23709
23710 slice.x = slice.y = 0;
23711 slice.width = img->width;
23712 slice.height = img->height;
23713
23714 if (INTEGERP (it->slice.x))
23715 slice.x = XINT (it->slice.x);
23716 else if (FLOATP (it->slice.x))
23717 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23718
23719 if (INTEGERP (it->slice.y))
23720 slice.y = XINT (it->slice.y);
23721 else if (FLOATP (it->slice.y))
23722 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23723
23724 if (INTEGERP (it->slice.width))
23725 slice.width = XINT (it->slice.width);
23726 else if (FLOATP (it->slice.width))
23727 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23728
23729 if (INTEGERP (it->slice.height))
23730 slice.height = XINT (it->slice.height);
23731 else if (FLOATP (it->slice.height))
23732 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23733
23734 if (slice.x >= img->width)
23735 slice.x = img->width;
23736 if (slice.y >= img->height)
23737 slice.y = img->height;
23738 if (slice.x + slice.width >= img->width)
23739 slice.width = img->width - slice.x;
23740 if (slice.y + slice.height > img->height)
23741 slice.height = img->height - slice.y;
23742
23743 if (slice.width == 0 || slice.height == 0)
23744 return;
23745
23746 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23747
23748 it->descent = slice.height - glyph_ascent;
23749 if (slice.y == 0)
23750 it->descent += img->vmargin;
23751 if (slice.y + slice.height == img->height)
23752 it->descent += img->vmargin;
23753 it->phys_descent = it->descent;
23754
23755 it->pixel_width = slice.width;
23756 if (slice.x == 0)
23757 it->pixel_width += img->hmargin;
23758 if (slice.x + slice.width == img->width)
23759 it->pixel_width += img->hmargin;
23760
23761 /* It's quite possible for images to have an ascent greater than
23762 their height, so don't get confused in that case. */
23763 if (it->descent < 0)
23764 it->descent = 0;
23765
23766 it->nglyphs = 1;
23767
23768 if (face->box != FACE_NO_BOX)
23769 {
23770 if (face->box_line_width > 0)
23771 {
23772 if (slice.y == 0)
23773 it->ascent += face->box_line_width;
23774 if (slice.y + slice.height == img->height)
23775 it->descent += face->box_line_width;
23776 }
23777
23778 if (it->start_of_box_run_p && slice.x == 0)
23779 it->pixel_width += eabs (face->box_line_width);
23780 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23781 it->pixel_width += eabs (face->box_line_width);
23782 }
23783
23784 take_vertical_position_into_account (it);
23785
23786 /* Automatically crop wide image glyphs at right edge so we can
23787 draw the cursor on same display row. */
23788 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23789 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23790 {
23791 it->pixel_width -= crop;
23792 slice.width -= crop;
23793 }
23794
23795 if (it->glyph_row)
23796 {
23797 struct glyph *glyph;
23798 enum glyph_row_area area = it->area;
23799
23800 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23801 if (glyph < it->glyph_row->glyphs[area + 1])
23802 {
23803 glyph->charpos = CHARPOS (it->position);
23804 glyph->object = it->object;
23805 glyph->pixel_width = it->pixel_width;
23806 glyph->ascent = glyph_ascent;
23807 glyph->descent = it->descent;
23808 glyph->voffset = it->voffset;
23809 glyph->type = IMAGE_GLYPH;
23810 glyph->avoid_cursor_p = it->avoid_cursor_p;
23811 glyph->multibyte_p = it->multibyte_p;
23812 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23813 {
23814 /* In R2L rows, the left and the right box edges need to be
23815 drawn in reverse direction. */
23816 glyph->right_box_line_p = it->start_of_box_run_p;
23817 glyph->left_box_line_p = it->end_of_box_run_p;
23818 }
23819 else
23820 {
23821 glyph->left_box_line_p = it->start_of_box_run_p;
23822 glyph->right_box_line_p = it->end_of_box_run_p;
23823 }
23824 glyph->overlaps_vertically_p = 0;
23825 glyph->padding_p = 0;
23826 glyph->glyph_not_available_p = 0;
23827 glyph->face_id = it->face_id;
23828 glyph->u.img_id = img->id;
23829 glyph->slice.img = slice;
23830 glyph->font_type = FONT_TYPE_UNKNOWN;
23831 if (it->bidi_p)
23832 {
23833 glyph->resolved_level = it->bidi_it.resolved_level;
23834 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23835 emacs_abort ();
23836 glyph->bidi_type = it->bidi_it.type;
23837 }
23838 ++it->glyph_row->used[area];
23839 }
23840 else
23841 IT_EXPAND_MATRIX_WIDTH (it, area);
23842 }
23843 }
23844
23845
23846 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23847 of the glyph, WIDTH and HEIGHT are the width and height of the
23848 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23849
23850 static void
23851 append_stretch_glyph (struct it *it, Lisp_Object object,
23852 int width, int height, int ascent)
23853 {
23854 struct glyph *glyph;
23855 enum glyph_row_area area = it->area;
23856
23857 eassert (ascent >= 0 && ascent <= height);
23858
23859 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23860 if (glyph < it->glyph_row->glyphs[area + 1])
23861 {
23862 /* If the glyph row is reversed, we need to prepend the glyph
23863 rather than append it. */
23864 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23865 {
23866 struct glyph *g;
23867
23868 /* Make room for the additional glyph. */
23869 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23870 g[1] = *g;
23871 glyph = it->glyph_row->glyphs[area];
23872 }
23873 glyph->charpos = CHARPOS (it->position);
23874 glyph->object = object;
23875 glyph->pixel_width = width;
23876 glyph->ascent = ascent;
23877 glyph->descent = height - ascent;
23878 glyph->voffset = it->voffset;
23879 glyph->type = STRETCH_GLYPH;
23880 glyph->avoid_cursor_p = it->avoid_cursor_p;
23881 glyph->multibyte_p = it->multibyte_p;
23882 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23883 {
23884 /* In R2L rows, the left and the right box edges need to be
23885 drawn in reverse direction. */
23886 glyph->right_box_line_p = it->start_of_box_run_p;
23887 glyph->left_box_line_p = it->end_of_box_run_p;
23888 }
23889 else
23890 {
23891 glyph->left_box_line_p = it->start_of_box_run_p;
23892 glyph->right_box_line_p = it->end_of_box_run_p;
23893 }
23894 glyph->overlaps_vertically_p = 0;
23895 glyph->padding_p = 0;
23896 glyph->glyph_not_available_p = 0;
23897 glyph->face_id = it->face_id;
23898 glyph->u.stretch.ascent = ascent;
23899 glyph->u.stretch.height = height;
23900 glyph->slice.img = null_glyph_slice;
23901 glyph->font_type = FONT_TYPE_UNKNOWN;
23902 if (it->bidi_p)
23903 {
23904 glyph->resolved_level = it->bidi_it.resolved_level;
23905 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23906 emacs_abort ();
23907 glyph->bidi_type = it->bidi_it.type;
23908 }
23909 else
23910 {
23911 glyph->resolved_level = 0;
23912 glyph->bidi_type = UNKNOWN_BT;
23913 }
23914 ++it->glyph_row->used[area];
23915 }
23916 else
23917 IT_EXPAND_MATRIX_WIDTH (it, area);
23918 }
23919
23920 #endif /* HAVE_WINDOW_SYSTEM */
23921
23922 /* Produce a stretch glyph for iterator IT. IT->object is the value
23923 of the glyph property displayed. The value must be a list
23924 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23925 being recognized:
23926
23927 1. `:width WIDTH' specifies that the space should be WIDTH *
23928 canonical char width wide. WIDTH may be an integer or floating
23929 point number.
23930
23931 2. `:relative-width FACTOR' specifies that the width of the stretch
23932 should be computed from the width of the first character having the
23933 `glyph' property, and should be FACTOR times that width.
23934
23935 3. `:align-to HPOS' specifies that the space should be wide enough
23936 to reach HPOS, a value in canonical character units.
23937
23938 Exactly one of the above pairs must be present.
23939
23940 4. `:height HEIGHT' specifies that the height of the stretch produced
23941 should be HEIGHT, measured in canonical character units.
23942
23943 5. `:relative-height FACTOR' specifies that the height of the
23944 stretch should be FACTOR times the height of the characters having
23945 the glyph property.
23946
23947 Either none or exactly one of 4 or 5 must be present.
23948
23949 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23950 of the stretch should be used for the ascent of the stretch.
23951 ASCENT must be in the range 0 <= ASCENT <= 100. */
23952
23953 void
23954 produce_stretch_glyph (struct it *it)
23955 {
23956 /* (space :width WIDTH :height HEIGHT ...) */
23957 Lisp_Object prop, plist;
23958 int width = 0, height = 0, align_to = -1;
23959 int zero_width_ok_p = 0;
23960 double tem;
23961 struct font *font = NULL;
23962
23963 #ifdef HAVE_WINDOW_SYSTEM
23964 int ascent = 0;
23965 int zero_height_ok_p = 0;
23966
23967 if (FRAME_WINDOW_P (it->f))
23968 {
23969 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23970 font = face->font ? face->font : FRAME_FONT (it->f);
23971 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23972 }
23973 #endif
23974
23975 /* List should start with `space'. */
23976 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23977 plist = XCDR (it->object);
23978
23979 /* Compute the width of the stretch. */
23980 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23981 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23982 {
23983 /* Absolute width `:width WIDTH' specified and valid. */
23984 zero_width_ok_p = 1;
23985 width = (int)tem;
23986 }
23987 #ifdef HAVE_WINDOW_SYSTEM
23988 else if (FRAME_WINDOW_P (it->f)
23989 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23990 {
23991 /* Relative width `:relative-width FACTOR' specified and valid.
23992 Compute the width of the characters having the `glyph'
23993 property. */
23994 struct it it2;
23995 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23996
23997 it2 = *it;
23998 if (it->multibyte_p)
23999 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24000 else
24001 {
24002 it2.c = it2.char_to_display = *p, it2.len = 1;
24003 if (! ASCII_CHAR_P (it2.c))
24004 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24005 }
24006
24007 it2.glyph_row = NULL;
24008 it2.what = IT_CHARACTER;
24009 x_produce_glyphs (&it2);
24010 width = NUMVAL (prop) * it2.pixel_width;
24011 }
24012 #endif /* HAVE_WINDOW_SYSTEM */
24013 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24014 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24015 {
24016 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24017 align_to = (align_to < 0
24018 ? 0
24019 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24020 else if (align_to < 0)
24021 align_to = window_box_left_offset (it->w, TEXT_AREA);
24022 width = max (0, (int)tem + align_to - it->current_x);
24023 zero_width_ok_p = 1;
24024 }
24025 else
24026 /* Nothing specified -> width defaults to canonical char width. */
24027 width = FRAME_COLUMN_WIDTH (it->f);
24028
24029 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24030 width = 1;
24031
24032 #ifdef HAVE_WINDOW_SYSTEM
24033 /* Compute height. */
24034 if (FRAME_WINDOW_P (it->f))
24035 {
24036 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24037 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24038 {
24039 height = (int)tem;
24040 zero_height_ok_p = 1;
24041 }
24042 else if (prop = Fplist_get (plist, QCrelative_height),
24043 NUMVAL (prop) > 0)
24044 height = FONT_HEIGHT (font) * NUMVAL (prop);
24045 else
24046 height = FONT_HEIGHT (font);
24047
24048 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24049 height = 1;
24050
24051 /* Compute percentage of height used for ascent. If
24052 `:ascent ASCENT' is present and valid, use that. Otherwise,
24053 derive the ascent from the font in use. */
24054 if (prop = Fplist_get (plist, QCascent),
24055 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24056 ascent = height * NUMVAL (prop) / 100.0;
24057 else if (!NILP (prop)
24058 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24059 ascent = min (max (0, (int)tem), height);
24060 else
24061 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24062 }
24063 else
24064 #endif /* HAVE_WINDOW_SYSTEM */
24065 height = 1;
24066
24067 if (width > 0 && it->line_wrap != TRUNCATE
24068 && it->current_x + width > it->last_visible_x)
24069 {
24070 width = it->last_visible_x - it->current_x;
24071 #ifdef HAVE_WINDOW_SYSTEM
24072 /* Subtract one more pixel from the stretch width, but only on
24073 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24074 width -= FRAME_WINDOW_P (it->f);
24075 #endif
24076 }
24077
24078 if (width > 0 && height > 0 && it->glyph_row)
24079 {
24080 Lisp_Object o_object = it->object;
24081 Lisp_Object object = it->stack[it->sp - 1].string;
24082 int n = width;
24083
24084 if (!STRINGP (object))
24085 object = it->w->buffer;
24086 #ifdef HAVE_WINDOW_SYSTEM
24087 if (FRAME_WINDOW_P (it->f))
24088 append_stretch_glyph (it, object, width, height, ascent);
24089 else
24090 #endif
24091 {
24092 it->object = object;
24093 it->char_to_display = ' ';
24094 it->pixel_width = it->len = 1;
24095 while (n--)
24096 tty_append_glyph (it);
24097 it->object = o_object;
24098 }
24099 }
24100
24101 it->pixel_width = width;
24102 #ifdef HAVE_WINDOW_SYSTEM
24103 if (FRAME_WINDOW_P (it->f))
24104 {
24105 it->ascent = it->phys_ascent = ascent;
24106 it->descent = it->phys_descent = height - it->ascent;
24107 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24108 take_vertical_position_into_account (it);
24109 }
24110 else
24111 #endif
24112 it->nglyphs = width;
24113 }
24114
24115 /* Get information about special display element WHAT in an
24116 environment described by IT. WHAT is one of IT_TRUNCATION or
24117 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24118 non-null glyph_row member. This function ensures that fields like
24119 face_id, c, len of IT are left untouched. */
24120
24121 static void
24122 produce_special_glyphs (struct it *it, enum display_element_type what)
24123 {
24124 struct it temp_it;
24125 Lisp_Object gc;
24126 GLYPH glyph;
24127
24128 temp_it = *it;
24129 temp_it.object = make_number (0);
24130 memset (&temp_it.current, 0, sizeof temp_it.current);
24131
24132 if (what == IT_CONTINUATION)
24133 {
24134 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24135 if (it->bidi_it.paragraph_dir == R2L)
24136 SET_GLYPH_FROM_CHAR (glyph, '/');
24137 else
24138 SET_GLYPH_FROM_CHAR (glyph, '\\');
24139 if (it->dp
24140 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24141 {
24142 /* FIXME: Should we mirror GC for R2L lines? */
24143 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24144 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24145 }
24146 }
24147 else if (what == IT_TRUNCATION)
24148 {
24149 /* Truncation glyph. */
24150 SET_GLYPH_FROM_CHAR (glyph, '$');
24151 if (it->dp
24152 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24153 {
24154 /* FIXME: Should we mirror GC for R2L lines? */
24155 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24156 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24157 }
24158 }
24159 else
24160 emacs_abort ();
24161
24162 #ifdef HAVE_WINDOW_SYSTEM
24163 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24164 is turned off, we precede the truncation/continuation glyphs by a
24165 stretch glyph whose width is computed such that these special
24166 glyphs are aligned at the window margin, even when very different
24167 fonts are used in different glyph rows. */
24168 if (FRAME_WINDOW_P (temp_it.f)
24169 /* init_iterator calls this with it->glyph_row == NULL, and it
24170 wants only the pixel width of the truncation/continuation
24171 glyphs. */
24172 && temp_it.glyph_row
24173 /* insert_left_trunc_glyphs calls us at the beginning of the
24174 row, and it has its own calculation of the stretch glyph
24175 width. */
24176 && temp_it.glyph_row->used[TEXT_AREA] > 0
24177 && (temp_it.glyph_row->reversed_p
24178 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24179 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24180 {
24181 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24182
24183 if (stretch_width > 0)
24184 {
24185 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24186 struct font *font =
24187 face->font ? face->font : FRAME_FONT (temp_it.f);
24188 int stretch_ascent =
24189 (((temp_it.ascent + temp_it.descent)
24190 * FONT_BASE (font)) / FONT_HEIGHT (font));
24191
24192 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24193 temp_it.ascent + temp_it.descent,
24194 stretch_ascent);
24195 }
24196 }
24197 #endif
24198
24199 temp_it.dp = NULL;
24200 temp_it.what = IT_CHARACTER;
24201 temp_it.len = 1;
24202 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24203 temp_it.face_id = GLYPH_FACE (glyph);
24204 temp_it.len = CHAR_BYTES (temp_it.c);
24205
24206 PRODUCE_GLYPHS (&temp_it);
24207 it->pixel_width = temp_it.pixel_width;
24208 it->nglyphs = temp_it.pixel_width;
24209 }
24210
24211 #ifdef HAVE_WINDOW_SYSTEM
24212
24213 /* Calculate line-height and line-spacing properties.
24214 An integer value specifies explicit pixel value.
24215 A float value specifies relative value to current face height.
24216 A cons (float . face-name) specifies relative value to
24217 height of specified face font.
24218
24219 Returns height in pixels, or nil. */
24220
24221
24222 static Lisp_Object
24223 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24224 int boff, int override)
24225 {
24226 Lisp_Object face_name = Qnil;
24227 int ascent, descent, height;
24228
24229 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24230 return val;
24231
24232 if (CONSP (val))
24233 {
24234 face_name = XCAR (val);
24235 val = XCDR (val);
24236 if (!NUMBERP (val))
24237 val = make_number (1);
24238 if (NILP (face_name))
24239 {
24240 height = it->ascent + it->descent;
24241 goto scale;
24242 }
24243 }
24244
24245 if (NILP (face_name))
24246 {
24247 font = FRAME_FONT (it->f);
24248 boff = FRAME_BASELINE_OFFSET (it->f);
24249 }
24250 else if (EQ (face_name, Qt))
24251 {
24252 override = 0;
24253 }
24254 else
24255 {
24256 int face_id;
24257 struct face *face;
24258
24259 face_id = lookup_named_face (it->f, face_name, 0);
24260 if (face_id < 0)
24261 return make_number (-1);
24262
24263 face = FACE_FROM_ID (it->f, face_id);
24264 font = face->font;
24265 if (font == NULL)
24266 return make_number (-1);
24267 boff = font->baseline_offset;
24268 if (font->vertical_centering)
24269 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24270 }
24271
24272 ascent = FONT_BASE (font) + boff;
24273 descent = FONT_DESCENT (font) - boff;
24274
24275 if (override)
24276 {
24277 it->override_ascent = ascent;
24278 it->override_descent = descent;
24279 it->override_boff = boff;
24280 }
24281
24282 height = ascent + descent;
24283
24284 scale:
24285 if (FLOATP (val))
24286 height = (int)(XFLOAT_DATA (val) * height);
24287 else if (INTEGERP (val))
24288 height *= XINT (val);
24289
24290 return make_number (height);
24291 }
24292
24293
24294 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24295 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24296 and only if this is for a character for which no font was found.
24297
24298 If the display method (it->glyphless_method) is
24299 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24300 length of the acronym or the hexadecimal string, UPPER_XOFF and
24301 UPPER_YOFF are pixel offsets for the upper part of the string,
24302 LOWER_XOFF and LOWER_YOFF are for the lower part.
24303
24304 For the other display methods, LEN through LOWER_YOFF are zero. */
24305
24306 static void
24307 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24308 short upper_xoff, short upper_yoff,
24309 short lower_xoff, short lower_yoff)
24310 {
24311 struct glyph *glyph;
24312 enum glyph_row_area area = it->area;
24313
24314 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24315 if (glyph < it->glyph_row->glyphs[area + 1])
24316 {
24317 /* If the glyph row is reversed, we need to prepend the glyph
24318 rather than append it. */
24319 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24320 {
24321 struct glyph *g;
24322
24323 /* Make room for the additional glyph. */
24324 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24325 g[1] = *g;
24326 glyph = it->glyph_row->glyphs[area];
24327 }
24328 glyph->charpos = CHARPOS (it->position);
24329 glyph->object = it->object;
24330 glyph->pixel_width = it->pixel_width;
24331 glyph->ascent = it->ascent;
24332 glyph->descent = it->descent;
24333 glyph->voffset = it->voffset;
24334 glyph->type = GLYPHLESS_GLYPH;
24335 glyph->u.glyphless.method = it->glyphless_method;
24336 glyph->u.glyphless.for_no_font = for_no_font;
24337 glyph->u.glyphless.len = len;
24338 glyph->u.glyphless.ch = it->c;
24339 glyph->slice.glyphless.upper_xoff = upper_xoff;
24340 glyph->slice.glyphless.upper_yoff = upper_yoff;
24341 glyph->slice.glyphless.lower_xoff = lower_xoff;
24342 glyph->slice.glyphless.lower_yoff = lower_yoff;
24343 glyph->avoid_cursor_p = it->avoid_cursor_p;
24344 glyph->multibyte_p = it->multibyte_p;
24345 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24346 {
24347 /* In R2L rows, the left and the right box edges need to be
24348 drawn in reverse direction. */
24349 glyph->right_box_line_p = it->start_of_box_run_p;
24350 glyph->left_box_line_p = it->end_of_box_run_p;
24351 }
24352 else
24353 {
24354 glyph->left_box_line_p = it->start_of_box_run_p;
24355 glyph->right_box_line_p = it->end_of_box_run_p;
24356 }
24357 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24358 || it->phys_descent > it->descent);
24359 glyph->padding_p = 0;
24360 glyph->glyph_not_available_p = 0;
24361 glyph->face_id = face_id;
24362 glyph->font_type = FONT_TYPE_UNKNOWN;
24363 if (it->bidi_p)
24364 {
24365 glyph->resolved_level = it->bidi_it.resolved_level;
24366 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24367 emacs_abort ();
24368 glyph->bidi_type = it->bidi_it.type;
24369 }
24370 ++it->glyph_row->used[area];
24371 }
24372 else
24373 IT_EXPAND_MATRIX_WIDTH (it, area);
24374 }
24375
24376
24377 /* Produce a glyph for a glyphless character for iterator IT.
24378 IT->glyphless_method specifies which method to use for displaying
24379 the character. See the description of enum
24380 glyphless_display_method in dispextern.h for the detail.
24381
24382 FOR_NO_FONT is nonzero if and only if this is for a character for
24383 which no font was found. ACRONYM, if non-nil, is an acronym string
24384 for the character. */
24385
24386 static void
24387 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24388 {
24389 int face_id;
24390 struct face *face;
24391 struct font *font;
24392 int base_width, base_height, width, height;
24393 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24394 int len;
24395
24396 /* Get the metrics of the base font. We always refer to the current
24397 ASCII face. */
24398 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24399 font = face->font ? face->font : FRAME_FONT (it->f);
24400 it->ascent = FONT_BASE (font) + font->baseline_offset;
24401 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24402 base_height = it->ascent + it->descent;
24403 base_width = font->average_width;
24404
24405 /* Get a face ID for the glyph by utilizing a cache (the same way as
24406 done for `escape-glyph' in get_next_display_element). */
24407 if (it->f == last_glyphless_glyph_frame
24408 && it->face_id == last_glyphless_glyph_face_id)
24409 {
24410 face_id = last_glyphless_glyph_merged_face_id;
24411 }
24412 else
24413 {
24414 /* Merge the `glyphless-char' face into the current face. */
24415 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24416 last_glyphless_glyph_frame = it->f;
24417 last_glyphless_glyph_face_id = it->face_id;
24418 last_glyphless_glyph_merged_face_id = face_id;
24419 }
24420
24421 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24422 {
24423 it->pixel_width = THIN_SPACE_WIDTH;
24424 len = 0;
24425 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24426 }
24427 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24428 {
24429 width = CHAR_WIDTH (it->c);
24430 if (width == 0)
24431 width = 1;
24432 else if (width > 4)
24433 width = 4;
24434 it->pixel_width = base_width * width;
24435 len = 0;
24436 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24437 }
24438 else
24439 {
24440 char buf[7];
24441 const char *str;
24442 unsigned int code[6];
24443 int upper_len;
24444 int ascent, descent;
24445 struct font_metrics metrics_upper, metrics_lower;
24446
24447 face = FACE_FROM_ID (it->f, face_id);
24448 font = face->font ? face->font : FRAME_FONT (it->f);
24449 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24450
24451 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24452 {
24453 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24454 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24455 if (CONSP (acronym))
24456 acronym = XCAR (acronym);
24457 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24458 }
24459 else
24460 {
24461 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24462 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24463 str = buf;
24464 }
24465 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24466 code[len] = font->driver->encode_char (font, str[len]);
24467 upper_len = (len + 1) / 2;
24468 font->driver->text_extents (font, code, upper_len,
24469 &metrics_upper);
24470 font->driver->text_extents (font, code + upper_len, len - upper_len,
24471 &metrics_lower);
24472
24473
24474
24475 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24476 width = max (metrics_upper.width, metrics_lower.width) + 4;
24477 upper_xoff = upper_yoff = 2; /* the typical case */
24478 if (base_width >= width)
24479 {
24480 /* Align the upper to the left, the lower to the right. */
24481 it->pixel_width = base_width;
24482 lower_xoff = base_width - 2 - metrics_lower.width;
24483 }
24484 else
24485 {
24486 /* Center the shorter one. */
24487 it->pixel_width = width;
24488 if (metrics_upper.width >= metrics_lower.width)
24489 lower_xoff = (width - metrics_lower.width) / 2;
24490 else
24491 {
24492 /* FIXME: This code doesn't look right. It formerly was
24493 missing the "lower_xoff = 0;", which couldn't have
24494 been right since it left lower_xoff uninitialized. */
24495 lower_xoff = 0;
24496 upper_xoff = (width - metrics_upper.width) / 2;
24497 }
24498 }
24499
24500 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24501 top, bottom, and between upper and lower strings. */
24502 height = (metrics_upper.ascent + metrics_upper.descent
24503 + metrics_lower.ascent + metrics_lower.descent) + 5;
24504 /* Center vertically.
24505 H:base_height, D:base_descent
24506 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24507
24508 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24509 descent = D - H/2 + h/2;
24510 lower_yoff = descent - 2 - ld;
24511 upper_yoff = lower_yoff - la - 1 - ud; */
24512 ascent = - (it->descent - (base_height + height + 1) / 2);
24513 descent = it->descent - (base_height - height) / 2;
24514 lower_yoff = descent - 2 - metrics_lower.descent;
24515 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24516 - metrics_upper.descent);
24517 /* Don't make the height shorter than the base height. */
24518 if (height > base_height)
24519 {
24520 it->ascent = ascent;
24521 it->descent = descent;
24522 }
24523 }
24524
24525 it->phys_ascent = it->ascent;
24526 it->phys_descent = it->descent;
24527 if (it->glyph_row)
24528 append_glyphless_glyph (it, face_id, for_no_font, len,
24529 upper_xoff, upper_yoff,
24530 lower_xoff, lower_yoff);
24531 it->nglyphs = 1;
24532 take_vertical_position_into_account (it);
24533 }
24534
24535
24536 /* RIF:
24537 Produce glyphs/get display metrics for the display element IT is
24538 loaded with. See the description of struct it in dispextern.h
24539 for an overview of struct it. */
24540
24541 void
24542 x_produce_glyphs (struct it *it)
24543 {
24544 int extra_line_spacing = it->extra_line_spacing;
24545
24546 it->glyph_not_available_p = 0;
24547
24548 if (it->what == IT_CHARACTER)
24549 {
24550 XChar2b char2b;
24551 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24552 struct font *font = face->font;
24553 struct font_metrics *pcm = NULL;
24554 int boff; /* baseline offset */
24555
24556 if (font == NULL)
24557 {
24558 /* When no suitable font is found, display this character by
24559 the method specified in the first extra slot of
24560 Vglyphless_char_display. */
24561 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24562
24563 eassert (it->what == IT_GLYPHLESS);
24564 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24565 goto done;
24566 }
24567
24568 boff = font->baseline_offset;
24569 if (font->vertical_centering)
24570 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24571
24572 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24573 {
24574 int stretched_p;
24575
24576 it->nglyphs = 1;
24577
24578 if (it->override_ascent >= 0)
24579 {
24580 it->ascent = it->override_ascent;
24581 it->descent = it->override_descent;
24582 boff = it->override_boff;
24583 }
24584 else
24585 {
24586 it->ascent = FONT_BASE (font) + boff;
24587 it->descent = FONT_DESCENT (font) - boff;
24588 }
24589
24590 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24591 {
24592 pcm = get_per_char_metric (font, &char2b);
24593 if (pcm->width == 0
24594 && pcm->rbearing == 0 && pcm->lbearing == 0)
24595 pcm = NULL;
24596 }
24597
24598 if (pcm)
24599 {
24600 it->phys_ascent = pcm->ascent + boff;
24601 it->phys_descent = pcm->descent - boff;
24602 it->pixel_width = pcm->width;
24603 }
24604 else
24605 {
24606 it->glyph_not_available_p = 1;
24607 it->phys_ascent = it->ascent;
24608 it->phys_descent = it->descent;
24609 it->pixel_width = font->space_width;
24610 }
24611
24612 if (it->constrain_row_ascent_descent_p)
24613 {
24614 if (it->descent > it->max_descent)
24615 {
24616 it->ascent += it->descent - it->max_descent;
24617 it->descent = it->max_descent;
24618 }
24619 if (it->ascent > it->max_ascent)
24620 {
24621 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24622 it->ascent = it->max_ascent;
24623 }
24624 it->phys_ascent = min (it->phys_ascent, it->ascent);
24625 it->phys_descent = min (it->phys_descent, it->descent);
24626 extra_line_spacing = 0;
24627 }
24628
24629 /* If this is a space inside a region of text with
24630 `space-width' property, change its width. */
24631 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24632 if (stretched_p)
24633 it->pixel_width *= XFLOATINT (it->space_width);
24634
24635 /* If face has a box, add the box thickness to the character
24636 height. If character has a box line to the left and/or
24637 right, add the box line width to the character's width. */
24638 if (face->box != FACE_NO_BOX)
24639 {
24640 int thick = face->box_line_width;
24641
24642 if (thick > 0)
24643 {
24644 it->ascent += thick;
24645 it->descent += thick;
24646 }
24647 else
24648 thick = -thick;
24649
24650 if (it->start_of_box_run_p)
24651 it->pixel_width += thick;
24652 if (it->end_of_box_run_p)
24653 it->pixel_width += thick;
24654 }
24655
24656 /* If face has an overline, add the height of the overline
24657 (1 pixel) and a 1 pixel margin to the character height. */
24658 if (face->overline_p)
24659 it->ascent += overline_margin;
24660
24661 if (it->constrain_row_ascent_descent_p)
24662 {
24663 if (it->ascent > it->max_ascent)
24664 it->ascent = it->max_ascent;
24665 if (it->descent > it->max_descent)
24666 it->descent = it->max_descent;
24667 }
24668
24669 take_vertical_position_into_account (it);
24670
24671 /* If we have to actually produce glyphs, do it. */
24672 if (it->glyph_row)
24673 {
24674 if (stretched_p)
24675 {
24676 /* Translate a space with a `space-width' property
24677 into a stretch glyph. */
24678 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24679 / FONT_HEIGHT (font));
24680 append_stretch_glyph (it, it->object, it->pixel_width,
24681 it->ascent + it->descent, ascent);
24682 }
24683 else
24684 append_glyph (it);
24685
24686 /* If characters with lbearing or rbearing are displayed
24687 in this line, record that fact in a flag of the
24688 glyph row. This is used to optimize X output code. */
24689 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24690 it->glyph_row->contains_overlapping_glyphs_p = 1;
24691 }
24692 if (! stretched_p && it->pixel_width == 0)
24693 /* We assure that all visible glyphs have at least 1-pixel
24694 width. */
24695 it->pixel_width = 1;
24696 }
24697 else if (it->char_to_display == '\n')
24698 {
24699 /* A newline has no width, but we need the height of the
24700 line. But if previous part of the line sets a height,
24701 don't increase that height */
24702
24703 Lisp_Object height;
24704 Lisp_Object total_height = Qnil;
24705
24706 it->override_ascent = -1;
24707 it->pixel_width = 0;
24708 it->nglyphs = 0;
24709
24710 height = get_it_property (it, Qline_height);
24711 /* Split (line-height total-height) list */
24712 if (CONSP (height)
24713 && CONSP (XCDR (height))
24714 && NILP (XCDR (XCDR (height))))
24715 {
24716 total_height = XCAR (XCDR (height));
24717 height = XCAR (height);
24718 }
24719 height = calc_line_height_property (it, height, font, boff, 1);
24720
24721 if (it->override_ascent >= 0)
24722 {
24723 it->ascent = it->override_ascent;
24724 it->descent = it->override_descent;
24725 boff = it->override_boff;
24726 }
24727 else
24728 {
24729 it->ascent = FONT_BASE (font) + boff;
24730 it->descent = FONT_DESCENT (font) - boff;
24731 }
24732
24733 if (EQ (height, Qt))
24734 {
24735 if (it->descent > it->max_descent)
24736 {
24737 it->ascent += it->descent - it->max_descent;
24738 it->descent = it->max_descent;
24739 }
24740 if (it->ascent > it->max_ascent)
24741 {
24742 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24743 it->ascent = it->max_ascent;
24744 }
24745 it->phys_ascent = min (it->phys_ascent, it->ascent);
24746 it->phys_descent = min (it->phys_descent, it->descent);
24747 it->constrain_row_ascent_descent_p = 1;
24748 extra_line_spacing = 0;
24749 }
24750 else
24751 {
24752 Lisp_Object spacing;
24753
24754 it->phys_ascent = it->ascent;
24755 it->phys_descent = it->descent;
24756
24757 if ((it->max_ascent > 0 || it->max_descent > 0)
24758 && face->box != FACE_NO_BOX
24759 && face->box_line_width > 0)
24760 {
24761 it->ascent += face->box_line_width;
24762 it->descent += face->box_line_width;
24763 }
24764 if (!NILP (height)
24765 && XINT (height) > it->ascent + it->descent)
24766 it->ascent = XINT (height) - it->descent;
24767
24768 if (!NILP (total_height))
24769 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24770 else
24771 {
24772 spacing = get_it_property (it, Qline_spacing);
24773 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24774 }
24775 if (INTEGERP (spacing))
24776 {
24777 extra_line_spacing = XINT (spacing);
24778 if (!NILP (total_height))
24779 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24780 }
24781 }
24782 }
24783 else /* i.e. (it->char_to_display == '\t') */
24784 {
24785 if (font->space_width > 0)
24786 {
24787 int tab_width = it->tab_width * font->space_width;
24788 int x = it->current_x + it->continuation_lines_width;
24789 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24790
24791 /* If the distance from the current position to the next tab
24792 stop is less than a space character width, use the
24793 tab stop after that. */
24794 if (next_tab_x - x < font->space_width)
24795 next_tab_x += tab_width;
24796
24797 it->pixel_width = next_tab_x - x;
24798 it->nglyphs = 1;
24799 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24800 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24801
24802 if (it->glyph_row)
24803 {
24804 append_stretch_glyph (it, it->object, it->pixel_width,
24805 it->ascent + it->descent, it->ascent);
24806 }
24807 }
24808 else
24809 {
24810 it->pixel_width = 0;
24811 it->nglyphs = 1;
24812 }
24813 }
24814 }
24815 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24816 {
24817 /* A static composition.
24818
24819 Note: A composition is represented as one glyph in the
24820 glyph matrix. There are no padding glyphs.
24821
24822 Important note: pixel_width, ascent, and descent are the
24823 values of what is drawn by draw_glyphs (i.e. the values of
24824 the overall glyphs composed). */
24825 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24826 int boff; /* baseline offset */
24827 struct composition *cmp = composition_table[it->cmp_it.id];
24828 int glyph_len = cmp->glyph_len;
24829 struct font *font = face->font;
24830
24831 it->nglyphs = 1;
24832
24833 /* If we have not yet calculated pixel size data of glyphs of
24834 the composition for the current face font, calculate them
24835 now. Theoretically, we have to check all fonts for the
24836 glyphs, but that requires much time and memory space. So,
24837 here we check only the font of the first glyph. This may
24838 lead to incorrect display, but it's very rare, and C-l
24839 (recenter-top-bottom) can correct the display anyway. */
24840 if (! cmp->font || cmp->font != font)
24841 {
24842 /* Ascent and descent of the font of the first character
24843 of this composition (adjusted by baseline offset).
24844 Ascent and descent of overall glyphs should not be less
24845 than these, respectively. */
24846 int font_ascent, font_descent, font_height;
24847 /* Bounding box of the overall glyphs. */
24848 int leftmost, rightmost, lowest, highest;
24849 int lbearing, rbearing;
24850 int i, width, ascent, descent;
24851 int left_padded = 0, right_padded = 0;
24852 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24853 XChar2b char2b;
24854 struct font_metrics *pcm;
24855 int font_not_found_p;
24856 ptrdiff_t pos;
24857
24858 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24859 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24860 break;
24861 if (glyph_len < cmp->glyph_len)
24862 right_padded = 1;
24863 for (i = 0; i < glyph_len; i++)
24864 {
24865 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24866 break;
24867 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24868 }
24869 if (i > 0)
24870 left_padded = 1;
24871
24872 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24873 : IT_CHARPOS (*it));
24874 /* If no suitable font is found, use the default font. */
24875 font_not_found_p = font == NULL;
24876 if (font_not_found_p)
24877 {
24878 face = face->ascii_face;
24879 font = face->font;
24880 }
24881 boff = font->baseline_offset;
24882 if (font->vertical_centering)
24883 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24884 font_ascent = FONT_BASE (font) + boff;
24885 font_descent = FONT_DESCENT (font) - boff;
24886 font_height = FONT_HEIGHT (font);
24887
24888 cmp->font = font;
24889
24890 pcm = NULL;
24891 if (! font_not_found_p)
24892 {
24893 get_char_face_and_encoding (it->f, c, it->face_id,
24894 &char2b, 0);
24895 pcm = get_per_char_metric (font, &char2b);
24896 }
24897
24898 /* Initialize the bounding box. */
24899 if (pcm)
24900 {
24901 width = cmp->glyph_len > 0 ? pcm->width : 0;
24902 ascent = pcm->ascent;
24903 descent = pcm->descent;
24904 lbearing = pcm->lbearing;
24905 rbearing = pcm->rbearing;
24906 }
24907 else
24908 {
24909 width = cmp->glyph_len > 0 ? font->space_width : 0;
24910 ascent = FONT_BASE (font);
24911 descent = FONT_DESCENT (font);
24912 lbearing = 0;
24913 rbearing = width;
24914 }
24915
24916 rightmost = width;
24917 leftmost = 0;
24918 lowest = - descent + boff;
24919 highest = ascent + boff;
24920
24921 if (! font_not_found_p
24922 && font->default_ascent
24923 && CHAR_TABLE_P (Vuse_default_ascent)
24924 && !NILP (Faref (Vuse_default_ascent,
24925 make_number (it->char_to_display))))
24926 highest = font->default_ascent + boff;
24927
24928 /* Draw the first glyph at the normal position. It may be
24929 shifted to right later if some other glyphs are drawn
24930 at the left. */
24931 cmp->offsets[i * 2] = 0;
24932 cmp->offsets[i * 2 + 1] = boff;
24933 cmp->lbearing = lbearing;
24934 cmp->rbearing = rbearing;
24935
24936 /* Set cmp->offsets for the remaining glyphs. */
24937 for (i++; i < glyph_len; i++)
24938 {
24939 int left, right, btm, top;
24940 int ch = COMPOSITION_GLYPH (cmp, i);
24941 int face_id;
24942 struct face *this_face;
24943
24944 if (ch == '\t')
24945 ch = ' ';
24946 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24947 this_face = FACE_FROM_ID (it->f, face_id);
24948 font = this_face->font;
24949
24950 if (font == NULL)
24951 pcm = NULL;
24952 else
24953 {
24954 get_char_face_and_encoding (it->f, ch, face_id,
24955 &char2b, 0);
24956 pcm = get_per_char_metric (font, &char2b);
24957 }
24958 if (! pcm)
24959 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24960 else
24961 {
24962 width = pcm->width;
24963 ascent = pcm->ascent;
24964 descent = pcm->descent;
24965 lbearing = pcm->lbearing;
24966 rbearing = pcm->rbearing;
24967 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24968 {
24969 /* Relative composition with or without
24970 alternate chars. */
24971 left = (leftmost + rightmost - width) / 2;
24972 btm = - descent + boff;
24973 if (font->relative_compose
24974 && (! CHAR_TABLE_P (Vignore_relative_composition)
24975 || NILP (Faref (Vignore_relative_composition,
24976 make_number (ch)))))
24977 {
24978
24979 if (- descent >= font->relative_compose)
24980 /* One extra pixel between two glyphs. */
24981 btm = highest + 1;
24982 else if (ascent <= 0)
24983 /* One extra pixel between two glyphs. */
24984 btm = lowest - 1 - ascent - descent;
24985 }
24986 }
24987 else
24988 {
24989 /* A composition rule is specified by an integer
24990 value that encodes global and new reference
24991 points (GREF and NREF). GREF and NREF are
24992 specified by numbers as below:
24993
24994 0---1---2 -- ascent
24995 | |
24996 | |
24997 | |
24998 9--10--11 -- center
24999 | |
25000 ---3---4---5--- baseline
25001 | |
25002 6---7---8 -- descent
25003 */
25004 int rule = COMPOSITION_RULE (cmp, i);
25005 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25006
25007 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25008 grefx = gref % 3, nrefx = nref % 3;
25009 grefy = gref / 3, nrefy = nref / 3;
25010 if (xoff)
25011 xoff = font_height * (xoff - 128) / 256;
25012 if (yoff)
25013 yoff = font_height * (yoff - 128) / 256;
25014
25015 left = (leftmost
25016 + grefx * (rightmost - leftmost) / 2
25017 - nrefx * width / 2
25018 + xoff);
25019
25020 btm = ((grefy == 0 ? highest
25021 : grefy == 1 ? 0
25022 : grefy == 2 ? lowest
25023 : (highest + lowest) / 2)
25024 - (nrefy == 0 ? ascent + descent
25025 : nrefy == 1 ? descent - boff
25026 : nrefy == 2 ? 0
25027 : (ascent + descent) / 2)
25028 + yoff);
25029 }
25030
25031 cmp->offsets[i * 2] = left;
25032 cmp->offsets[i * 2 + 1] = btm + descent;
25033
25034 /* Update the bounding box of the overall glyphs. */
25035 if (width > 0)
25036 {
25037 right = left + width;
25038 if (left < leftmost)
25039 leftmost = left;
25040 if (right > rightmost)
25041 rightmost = right;
25042 }
25043 top = btm + descent + ascent;
25044 if (top > highest)
25045 highest = top;
25046 if (btm < lowest)
25047 lowest = btm;
25048
25049 if (cmp->lbearing > left + lbearing)
25050 cmp->lbearing = left + lbearing;
25051 if (cmp->rbearing < left + rbearing)
25052 cmp->rbearing = left + rbearing;
25053 }
25054 }
25055
25056 /* If there are glyphs whose x-offsets are negative,
25057 shift all glyphs to the right and make all x-offsets
25058 non-negative. */
25059 if (leftmost < 0)
25060 {
25061 for (i = 0; i < cmp->glyph_len; i++)
25062 cmp->offsets[i * 2] -= leftmost;
25063 rightmost -= leftmost;
25064 cmp->lbearing -= leftmost;
25065 cmp->rbearing -= leftmost;
25066 }
25067
25068 if (left_padded && cmp->lbearing < 0)
25069 {
25070 for (i = 0; i < cmp->glyph_len; i++)
25071 cmp->offsets[i * 2] -= cmp->lbearing;
25072 rightmost -= cmp->lbearing;
25073 cmp->rbearing -= cmp->lbearing;
25074 cmp->lbearing = 0;
25075 }
25076 if (right_padded && rightmost < cmp->rbearing)
25077 {
25078 rightmost = cmp->rbearing;
25079 }
25080
25081 cmp->pixel_width = rightmost;
25082 cmp->ascent = highest;
25083 cmp->descent = - lowest;
25084 if (cmp->ascent < font_ascent)
25085 cmp->ascent = font_ascent;
25086 if (cmp->descent < font_descent)
25087 cmp->descent = font_descent;
25088 }
25089
25090 if (it->glyph_row
25091 && (cmp->lbearing < 0
25092 || cmp->rbearing > cmp->pixel_width))
25093 it->glyph_row->contains_overlapping_glyphs_p = 1;
25094
25095 it->pixel_width = cmp->pixel_width;
25096 it->ascent = it->phys_ascent = cmp->ascent;
25097 it->descent = it->phys_descent = cmp->descent;
25098 if (face->box != FACE_NO_BOX)
25099 {
25100 int thick = face->box_line_width;
25101
25102 if (thick > 0)
25103 {
25104 it->ascent += thick;
25105 it->descent += thick;
25106 }
25107 else
25108 thick = - thick;
25109
25110 if (it->start_of_box_run_p)
25111 it->pixel_width += thick;
25112 if (it->end_of_box_run_p)
25113 it->pixel_width += thick;
25114 }
25115
25116 /* If face has an overline, add the height of the overline
25117 (1 pixel) and a 1 pixel margin to the character height. */
25118 if (face->overline_p)
25119 it->ascent += overline_margin;
25120
25121 take_vertical_position_into_account (it);
25122 if (it->ascent < 0)
25123 it->ascent = 0;
25124 if (it->descent < 0)
25125 it->descent = 0;
25126
25127 if (it->glyph_row && cmp->glyph_len > 0)
25128 append_composite_glyph (it);
25129 }
25130 else if (it->what == IT_COMPOSITION)
25131 {
25132 /* A dynamic (automatic) composition. */
25133 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25134 Lisp_Object gstring;
25135 struct font_metrics metrics;
25136
25137 it->nglyphs = 1;
25138
25139 gstring = composition_gstring_from_id (it->cmp_it.id);
25140 it->pixel_width
25141 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25142 &metrics);
25143 if (it->glyph_row
25144 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25145 it->glyph_row->contains_overlapping_glyphs_p = 1;
25146 it->ascent = it->phys_ascent = metrics.ascent;
25147 it->descent = it->phys_descent = metrics.descent;
25148 if (face->box != FACE_NO_BOX)
25149 {
25150 int thick = face->box_line_width;
25151
25152 if (thick > 0)
25153 {
25154 it->ascent += thick;
25155 it->descent += thick;
25156 }
25157 else
25158 thick = - thick;
25159
25160 if (it->start_of_box_run_p)
25161 it->pixel_width += thick;
25162 if (it->end_of_box_run_p)
25163 it->pixel_width += thick;
25164 }
25165 /* If face has an overline, add the height of the overline
25166 (1 pixel) and a 1 pixel margin to the character height. */
25167 if (face->overline_p)
25168 it->ascent += overline_margin;
25169 take_vertical_position_into_account (it);
25170 if (it->ascent < 0)
25171 it->ascent = 0;
25172 if (it->descent < 0)
25173 it->descent = 0;
25174
25175 if (it->glyph_row)
25176 append_composite_glyph (it);
25177 }
25178 else if (it->what == IT_GLYPHLESS)
25179 produce_glyphless_glyph (it, 0, Qnil);
25180 else if (it->what == IT_IMAGE)
25181 produce_image_glyph (it);
25182 else if (it->what == IT_STRETCH)
25183 produce_stretch_glyph (it);
25184
25185 done:
25186 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25187 because this isn't true for images with `:ascent 100'. */
25188 eassert (it->ascent >= 0 && it->descent >= 0);
25189 if (it->area == TEXT_AREA)
25190 it->current_x += it->pixel_width;
25191
25192 if (extra_line_spacing > 0)
25193 {
25194 it->descent += extra_line_spacing;
25195 if (extra_line_spacing > it->max_extra_line_spacing)
25196 it->max_extra_line_spacing = extra_line_spacing;
25197 }
25198
25199 it->max_ascent = max (it->max_ascent, it->ascent);
25200 it->max_descent = max (it->max_descent, it->descent);
25201 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25202 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25203 }
25204
25205 /* EXPORT for RIF:
25206 Output LEN glyphs starting at START at the nominal cursor position.
25207 Advance the nominal cursor over the text. The global variable
25208 updated_window contains the window being updated, updated_row is
25209 the glyph row being updated, and updated_area is the area of that
25210 row being updated. */
25211
25212 void
25213 x_write_glyphs (struct glyph *start, int len)
25214 {
25215 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25216
25217 eassert (updated_window && updated_row);
25218 /* When the window is hscrolled, cursor hpos can legitimately be out
25219 of bounds, but we draw the cursor at the corresponding window
25220 margin in that case. */
25221 if (!updated_row->reversed_p && chpos < 0)
25222 chpos = 0;
25223 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25224 chpos = updated_row->used[TEXT_AREA] - 1;
25225
25226 block_input ();
25227
25228 /* Write glyphs. */
25229
25230 hpos = start - updated_row->glyphs[updated_area];
25231 x = draw_glyphs (updated_window, output_cursor.x,
25232 updated_row, updated_area,
25233 hpos, hpos + len,
25234 DRAW_NORMAL_TEXT, 0);
25235
25236 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25237 if (updated_area == TEXT_AREA
25238 && updated_window->phys_cursor_on_p
25239 && updated_window->phys_cursor.vpos == output_cursor.vpos
25240 && chpos >= hpos
25241 && chpos < hpos + len)
25242 updated_window->phys_cursor_on_p = 0;
25243
25244 unblock_input ();
25245
25246 /* Advance the output cursor. */
25247 output_cursor.hpos += len;
25248 output_cursor.x = x;
25249 }
25250
25251
25252 /* EXPORT for RIF:
25253 Insert LEN glyphs from START at the nominal cursor position. */
25254
25255 void
25256 x_insert_glyphs (struct glyph *start, int len)
25257 {
25258 struct frame *f;
25259 struct window *w;
25260 int line_height, shift_by_width, shifted_region_width;
25261 struct glyph_row *row;
25262 struct glyph *glyph;
25263 int frame_x, frame_y;
25264 ptrdiff_t hpos;
25265
25266 eassert (updated_window && updated_row);
25267 block_input ();
25268 w = updated_window;
25269 f = XFRAME (WINDOW_FRAME (w));
25270
25271 /* Get the height of the line we are in. */
25272 row = updated_row;
25273 line_height = row->height;
25274
25275 /* Get the width of the glyphs to insert. */
25276 shift_by_width = 0;
25277 for (glyph = start; glyph < start + len; ++glyph)
25278 shift_by_width += glyph->pixel_width;
25279
25280 /* Get the width of the region to shift right. */
25281 shifted_region_width = (window_box_width (w, updated_area)
25282 - output_cursor.x
25283 - shift_by_width);
25284
25285 /* Shift right. */
25286 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25287 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25288
25289 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25290 line_height, shift_by_width);
25291
25292 /* Write the glyphs. */
25293 hpos = start - row->glyphs[updated_area];
25294 draw_glyphs (w, output_cursor.x, row, updated_area,
25295 hpos, hpos + len,
25296 DRAW_NORMAL_TEXT, 0);
25297
25298 /* Advance the output cursor. */
25299 output_cursor.hpos += len;
25300 output_cursor.x += shift_by_width;
25301 unblock_input ();
25302 }
25303
25304
25305 /* EXPORT for RIF:
25306 Erase the current text line from the nominal cursor position
25307 (inclusive) to pixel column TO_X (exclusive). The idea is that
25308 everything from TO_X onward is already erased.
25309
25310 TO_X is a pixel position relative to updated_area of
25311 updated_window. TO_X == -1 means clear to the end of this area. */
25312
25313 void
25314 x_clear_end_of_line (int to_x)
25315 {
25316 struct frame *f;
25317 struct window *w = updated_window;
25318 int max_x, min_y, max_y;
25319 int from_x, from_y, to_y;
25320
25321 eassert (updated_window && updated_row);
25322 f = XFRAME (w->frame);
25323
25324 if (updated_row->full_width_p)
25325 max_x = WINDOW_TOTAL_WIDTH (w);
25326 else
25327 max_x = window_box_width (w, updated_area);
25328 max_y = window_text_bottom_y (w);
25329
25330 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25331 of window. For TO_X > 0, truncate to end of drawing area. */
25332 if (to_x == 0)
25333 return;
25334 else if (to_x < 0)
25335 to_x = max_x;
25336 else
25337 to_x = min (to_x, max_x);
25338
25339 to_y = min (max_y, output_cursor.y + updated_row->height);
25340
25341 /* Notice if the cursor will be cleared by this operation. */
25342 if (!updated_row->full_width_p)
25343 notice_overwritten_cursor (w, updated_area,
25344 output_cursor.x, -1,
25345 updated_row->y,
25346 MATRIX_ROW_BOTTOM_Y (updated_row));
25347
25348 from_x = output_cursor.x;
25349
25350 /* Translate to frame coordinates. */
25351 if (updated_row->full_width_p)
25352 {
25353 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25354 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25355 }
25356 else
25357 {
25358 int area_left = window_box_left (w, updated_area);
25359 from_x += area_left;
25360 to_x += area_left;
25361 }
25362
25363 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25364 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25365 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25366
25367 /* Prevent inadvertently clearing to end of the X window. */
25368 if (to_x > from_x && to_y > from_y)
25369 {
25370 block_input ();
25371 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25372 to_x - from_x, to_y - from_y);
25373 unblock_input ();
25374 }
25375 }
25376
25377 #endif /* HAVE_WINDOW_SYSTEM */
25378
25379
25380 \f
25381 /***********************************************************************
25382 Cursor types
25383 ***********************************************************************/
25384
25385 /* Value is the internal representation of the specified cursor type
25386 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25387 of the bar cursor. */
25388
25389 static enum text_cursor_kinds
25390 get_specified_cursor_type (Lisp_Object arg, int *width)
25391 {
25392 enum text_cursor_kinds type;
25393
25394 if (NILP (arg))
25395 return NO_CURSOR;
25396
25397 if (EQ (arg, Qbox))
25398 return FILLED_BOX_CURSOR;
25399
25400 if (EQ (arg, Qhollow))
25401 return HOLLOW_BOX_CURSOR;
25402
25403 if (EQ (arg, Qbar))
25404 {
25405 *width = 2;
25406 return BAR_CURSOR;
25407 }
25408
25409 if (CONSP (arg)
25410 && EQ (XCAR (arg), Qbar)
25411 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25412 {
25413 *width = XINT (XCDR (arg));
25414 return BAR_CURSOR;
25415 }
25416
25417 if (EQ (arg, Qhbar))
25418 {
25419 *width = 2;
25420 return HBAR_CURSOR;
25421 }
25422
25423 if (CONSP (arg)
25424 && EQ (XCAR (arg), Qhbar)
25425 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25426 {
25427 *width = XINT (XCDR (arg));
25428 return HBAR_CURSOR;
25429 }
25430
25431 /* Treat anything unknown as "hollow box cursor".
25432 It was bad to signal an error; people have trouble fixing
25433 .Xdefaults with Emacs, when it has something bad in it. */
25434 type = HOLLOW_BOX_CURSOR;
25435
25436 return type;
25437 }
25438
25439 /* Set the default cursor types for specified frame. */
25440 void
25441 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25442 {
25443 int width = 1;
25444 Lisp_Object tem;
25445
25446 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25447 FRAME_CURSOR_WIDTH (f) = width;
25448
25449 /* By default, set up the blink-off state depending on the on-state. */
25450
25451 tem = Fassoc (arg, Vblink_cursor_alist);
25452 if (!NILP (tem))
25453 {
25454 FRAME_BLINK_OFF_CURSOR (f)
25455 = get_specified_cursor_type (XCDR (tem), &width);
25456 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25457 }
25458 else
25459 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25460 }
25461
25462
25463 #ifdef HAVE_WINDOW_SYSTEM
25464
25465 /* Return the cursor we want to be displayed in window W. Return
25466 width of bar/hbar cursor through WIDTH arg. Return with
25467 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25468 (i.e. if the `system caret' should track this cursor).
25469
25470 In a mini-buffer window, we want the cursor only to appear if we
25471 are reading input from this window. For the selected window, we
25472 want the cursor type given by the frame parameter or buffer local
25473 setting of cursor-type. If explicitly marked off, draw no cursor.
25474 In all other cases, we want a hollow box cursor. */
25475
25476 static enum text_cursor_kinds
25477 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25478 int *active_cursor)
25479 {
25480 struct frame *f = XFRAME (w->frame);
25481 struct buffer *b = XBUFFER (w->buffer);
25482 int cursor_type = DEFAULT_CURSOR;
25483 Lisp_Object alt_cursor;
25484 int non_selected = 0;
25485
25486 *active_cursor = 1;
25487
25488 /* Echo area */
25489 if (cursor_in_echo_area
25490 && FRAME_HAS_MINIBUF_P (f)
25491 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25492 {
25493 if (w == XWINDOW (echo_area_window))
25494 {
25495 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25496 {
25497 *width = FRAME_CURSOR_WIDTH (f);
25498 return FRAME_DESIRED_CURSOR (f);
25499 }
25500 else
25501 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25502 }
25503
25504 *active_cursor = 0;
25505 non_selected = 1;
25506 }
25507
25508 /* Detect a nonselected window or nonselected frame. */
25509 else if (w != XWINDOW (f->selected_window)
25510 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25511 {
25512 *active_cursor = 0;
25513
25514 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25515 return NO_CURSOR;
25516
25517 non_selected = 1;
25518 }
25519
25520 /* Never display a cursor in a window in which cursor-type is nil. */
25521 if (NILP (BVAR (b, cursor_type)))
25522 return NO_CURSOR;
25523
25524 /* Get the normal cursor type for this window. */
25525 if (EQ (BVAR (b, cursor_type), Qt))
25526 {
25527 cursor_type = FRAME_DESIRED_CURSOR (f);
25528 *width = FRAME_CURSOR_WIDTH (f);
25529 }
25530 else
25531 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25532
25533 /* Use cursor-in-non-selected-windows instead
25534 for non-selected window or frame. */
25535 if (non_selected)
25536 {
25537 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25538 if (!EQ (Qt, alt_cursor))
25539 return get_specified_cursor_type (alt_cursor, width);
25540 /* t means modify the normal cursor type. */
25541 if (cursor_type == FILLED_BOX_CURSOR)
25542 cursor_type = HOLLOW_BOX_CURSOR;
25543 else if (cursor_type == BAR_CURSOR && *width > 1)
25544 --*width;
25545 return cursor_type;
25546 }
25547
25548 /* Use normal cursor if not blinked off. */
25549 if (!w->cursor_off_p)
25550 {
25551 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25552 {
25553 if (cursor_type == FILLED_BOX_CURSOR)
25554 {
25555 /* Using a block cursor on large images can be very annoying.
25556 So use a hollow cursor for "large" images.
25557 If image is not transparent (no mask), also use hollow cursor. */
25558 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25559 if (img != NULL && IMAGEP (img->spec))
25560 {
25561 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25562 where N = size of default frame font size.
25563 This should cover most of the "tiny" icons people may use. */
25564 if (!img->mask
25565 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25566 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25567 cursor_type = HOLLOW_BOX_CURSOR;
25568 }
25569 }
25570 else if (cursor_type != NO_CURSOR)
25571 {
25572 /* Display current only supports BOX and HOLLOW cursors for images.
25573 So for now, unconditionally use a HOLLOW cursor when cursor is
25574 not a solid box cursor. */
25575 cursor_type = HOLLOW_BOX_CURSOR;
25576 }
25577 }
25578 return cursor_type;
25579 }
25580
25581 /* Cursor is blinked off, so determine how to "toggle" it. */
25582
25583 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25584 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25585 return get_specified_cursor_type (XCDR (alt_cursor), width);
25586
25587 /* Then see if frame has specified a specific blink off cursor type. */
25588 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25589 {
25590 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25591 return FRAME_BLINK_OFF_CURSOR (f);
25592 }
25593
25594 #if 0
25595 /* Some people liked having a permanently visible blinking cursor,
25596 while others had very strong opinions against it. So it was
25597 decided to remove it. KFS 2003-09-03 */
25598
25599 /* Finally perform built-in cursor blinking:
25600 filled box <-> hollow box
25601 wide [h]bar <-> narrow [h]bar
25602 narrow [h]bar <-> no cursor
25603 other type <-> no cursor */
25604
25605 if (cursor_type == FILLED_BOX_CURSOR)
25606 return HOLLOW_BOX_CURSOR;
25607
25608 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25609 {
25610 *width = 1;
25611 return cursor_type;
25612 }
25613 #endif
25614
25615 return NO_CURSOR;
25616 }
25617
25618
25619 /* Notice when the text cursor of window W has been completely
25620 overwritten by a drawing operation that outputs glyphs in AREA
25621 starting at X0 and ending at X1 in the line starting at Y0 and
25622 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25623 the rest of the line after X0 has been written. Y coordinates
25624 are window-relative. */
25625
25626 static void
25627 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25628 int x0, int x1, int y0, int y1)
25629 {
25630 int cx0, cx1, cy0, cy1;
25631 struct glyph_row *row;
25632
25633 if (!w->phys_cursor_on_p)
25634 return;
25635 if (area != TEXT_AREA)
25636 return;
25637
25638 if (w->phys_cursor.vpos < 0
25639 || w->phys_cursor.vpos >= w->current_matrix->nrows
25640 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25641 !(row->enabled_p && row->displays_text_p)))
25642 return;
25643
25644 if (row->cursor_in_fringe_p)
25645 {
25646 row->cursor_in_fringe_p = 0;
25647 draw_fringe_bitmap (w, row, row->reversed_p);
25648 w->phys_cursor_on_p = 0;
25649 return;
25650 }
25651
25652 cx0 = w->phys_cursor.x;
25653 cx1 = cx0 + w->phys_cursor_width;
25654 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25655 return;
25656
25657 /* The cursor image will be completely removed from the
25658 screen if the output area intersects the cursor area in
25659 y-direction. When we draw in [y0 y1[, and some part of
25660 the cursor is at y < y0, that part must have been drawn
25661 before. When scrolling, the cursor is erased before
25662 actually scrolling, so we don't come here. When not
25663 scrolling, the rows above the old cursor row must have
25664 changed, and in this case these rows must have written
25665 over the cursor image.
25666
25667 Likewise if part of the cursor is below y1, with the
25668 exception of the cursor being in the first blank row at
25669 the buffer and window end because update_text_area
25670 doesn't draw that row. (Except when it does, but
25671 that's handled in update_text_area.) */
25672
25673 cy0 = w->phys_cursor.y;
25674 cy1 = cy0 + w->phys_cursor_height;
25675 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25676 return;
25677
25678 w->phys_cursor_on_p = 0;
25679 }
25680
25681 #endif /* HAVE_WINDOW_SYSTEM */
25682
25683 \f
25684 /************************************************************************
25685 Mouse Face
25686 ************************************************************************/
25687
25688 #ifdef HAVE_WINDOW_SYSTEM
25689
25690 /* EXPORT for RIF:
25691 Fix the display of area AREA of overlapping row ROW in window W
25692 with respect to the overlapping part OVERLAPS. */
25693
25694 void
25695 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25696 enum glyph_row_area area, int overlaps)
25697 {
25698 int i, x;
25699
25700 block_input ();
25701
25702 x = 0;
25703 for (i = 0; i < row->used[area];)
25704 {
25705 if (row->glyphs[area][i].overlaps_vertically_p)
25706 {
25707 int start = i, start_x = x;
25708
25709 do
25710 {
25711 x += row->glyphs[area][i].pixel_width;
25712 ++i;
25713 }
25714 while (i < row->used[area]
25715 && row->glyphs[area][i].overlaps_vertically_p);
25716
25717 draw_glyphs (w, start_x, row, area,
25718 start, i,
25719 DRAW_NORMAL_TEXT, overlaps);
25720 }
25721 else
25722 {
25723 x += row->glyphs[area][i].pixel_width;
25724 ++i;
25725 }
25726 }
25727
25728 unblock_input ();
25729 }
25730
25731
25732 /* EXPORT:
25733 Draw the cursor glyph of window W in glyph row ROW. See the
25734 comment of draw_glyphs for the meaning of HL. */
25735
25736 void
25737 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25738 enum draw_glyphs_face hl)
25739 {
25740 /* If cursor hpos is out of bounds, don't draw garbage. This can
25741 happen in mini-buffer windows when switching between echo area
25742 glyphs and mini-buffer. */
25743 if ((row->reversed_p
25744 ? (w->phys_cursor.hpos >= 0)
25745 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25746 {
25747 int on_p = w->phys_cursor_on_p;
25748 int x1;
25749 int hpos = w->phys_cursor.hpos;
25750
25751 /* When the window is hscrolled, cursor hpos can legitimately be
25752 out of bounds, but we draw the cursor at the corresponding
25753 window margin in that case. */
25754 if (!row->reversed_p && hpos < 0)
25755 hpos = 0;
25756 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25757 hpos = row->used[TEXT_AREA] - 1;
25758
25759 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25760 hl, 0);
25761 w->phys_cursor_on_p = on_p;
25762
25763 if (hl == DRAW_CURSOR)
25764 w->phys_cursor_width = x1 - w->phys_cursor.x;
25765 /* When we erase the cursor, and ROW is overlapped by other
25766 rows, make sure that these overlapping parts of other rows
25767 are redrawn. */
25768 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25769 {
25770 w->phys_cursor_width = x1 - w->phys_cursor.x;
25771
25772 if (row > w->current_matrix->rows
25773 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25774 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25775 OVERLAPS_ERASED_CURSOR);
25776
25777 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25778 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25779 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25780 OVERLAPS_ERASED_CURSOR);
25781 }
25782 }
25783 }
25784
25785
25786 /* EXPORT:
25787 Erase the image of a cursor of window W from the screen. */
25788
25789 void
25790 erase_phys_cursor (struct window *w)
25791 {
25792 struct frame *f = XFRAME (w->frame);
25793 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25794 int hpos = w->phys_cursor.hpos;
25795 int vpos = w->phys_cursor.vpos;
25796 int mouse_face_here_p = 0;
25797 struct glyph_matrix *active_glyphs = w->current_matrix;
25798 struct glyph_row *cursor_row;
25799 struct glyph *cursor_glyph;
25800 enum draw_glyphs_face hl;
25801
25802 /* No cursor displayed or row invalidated => nothing to do on the
25803 screen. */
25804 if (w->phys_cursor_type == NO_CURSOR)
25805 goto mark_cursor_off;
25806
25807 /* VPOS >= active_glyphs->nrows means that window has been resized.
25808 Don't bother to erase the cursor. */
25809 if (vpos >= active_glyphs->nrows)
25810 goto mark_cursor_off;
25811
25812 /* If row containing cursor is marked invalid, there is nothing we
25813 can do. */
25814 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25815 if (!cursor_row->enabled_p)
25816 goto mark_cursor_off;
25817
25818 /* If line spacing is > 0, old cursor may only be partially visible in
25819 window after split-window. So adjust visible height. */
25820 cursor_row->visible_height = min (cursor_row->visible_height,
25821 window_text_bottom_y (w) - cursor_row->y);
25822
25823 /* If row is completely invisible, don't attempt to delete a cursor which
25824 isn't there. This can happen if cursor is at top of a window, and
25825 we switch to a buffer with a header line in that window. */
25826 if (cursor_row->visible_height <= 0)
25827 goto mark_cursor_off;
25828
25829 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25830 if (cursor_row->cursor_in_fringe_p)
25831 {
25832 cursor_row->cursor_in_fringe_p = 0;
25833 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25834 goto mark_cursor_off;
25835 }
25836
25837 /* This can happen when the new row is shorter than the old one.
25838 In this case, either draw_glyphs or clear_end_of_line
25839 should have cleared the cursor. Note that we wouldn't be
25840 able to erase the cursor in this case because we don't have a
25841 cursor glyph at hand. */
25842 if ((cursor_row->reversed_p
25843 ? (w->phys_cursor.hpos < 0)
25844 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25845 goto mark_cursor_off;
25846
25847 /* When the window is hscrolled, cursor hpos can legitimately be out
25848 of bounds, but we draw the cursor at the corresponding window
25849 margin in that case. */
25850 if (!cursor_row->reversed_p && hpos < 0)
25851 hpos = 0;
25852 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25853 hpos = cursor_row->used[TEXT_AREA] - 1;
25854
25855 /* If the cursor is in the mouse face area, redisplay that when
25856 we clear the cursor. */
25857 if (! NILP (hlinfo->mouse_face_window)
25858 && coords_in_mouse_face_p (w, hpos, vpos)
25859 /* Don't redraw the cursor's spot in mouse face if it is at the
25860 end of a line (on a newline). The cursor appears there, but
25861 mouse highlighting does not. */
25862 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25863 mouse_face_here_p = 1;
25864
25865 /* Maybe clear the display under the cursor. */
25866 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25867 {
25868 int x, y, left_x;
25869 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25870 int width;
25871
25872 cursor_glyph = get_phys_cursor_glyph (w);
25873 if (cursor_glyph == NULL)
25874 goto mark_cursor_off;
25875
25876 width = cursor_glyph->pixel_width;
25877 left_x = window_box_left_offset (w, TEXT_AREA);
25878 x = w->phys_cursor.x;
25879 if (x < left_x)
25880 width -= left_x - x;
25881 width = min (width, window_box_width (w, TEXT_AREA) - x);
25882 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25883 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25884
25885 if (width > 0)
25886 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25887 }
25888
25889 /* Erase the cursor by redrawing the character underneath it. */
25890 if (mouse_face_here_p)
25891 hl = DRAW_MOUSE_FACE;
25892 else
25893 hl = DRAW_NORMAL_TEXT;
25894 draw_phys_cursor_glyph (w, cursor_row, hl);
25895
25896 mark_cursor_off:
25897 w->phys_cursor_on_p = 0;
25898 w->phys_cursor_type = NO_CURSOR;
25899 }
25900
25901
25902 /* EXPORT:
25903 Display or clear cursor of window W. If ON is zero, clear the
25904 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25905 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25906
25907 void
25908 display_and_set_cursor (struct window *w, int on,
25909 int hpos, int vpos, int x, int y)
25910 {
25911 struct frame *f = XFRAME (w->frame);
25912 int new_cursor_type;
25913 int new_cursor_width;
25914 int active_cursor;
25915 struct glyph_row *glyph_row;
25916 struct glyph *glyph;
25917
25918 /* This is pointless on invisible frames, and dangerous on garbaged
25919 windows and frames; in the latter case, the frame or window may
25920 be in the midst of changing its size, and x and y may be off the
25921 window. */
25922 if (! FRAME_VISIBLE_P (f)
25923 || FRAME_GARBAGED_P (f)
25924 || vpos >= w->current_matrix->nrows
25925 || hpos >= w->current_matrix->matrix_w)
25926 return;
25927
25928 /* If cursor is off and we want it off, return quickly. */
25929 if (!on && !w->phys_cursor_on_p)
25930 return;
25931
25932 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25933 /* If cursor row is not enabled, we don't really know where to
25934 display the cursor. */
25935 if (!glyph_row->enabled_p)
25936 {
25937 w->phys_cursor_on_p = 0;
25938 return;
25939 }
25940
25941 glyph = NULL;
25942 if (!glyph_row->exact_window_width_line_p
25943 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25944 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25945
25946 eassert (input_blocked_p ());
25947
25948 /* Set new_cursor_type to the cursor we want to be displayed. */
25949 new_cursor_type = get_window_cursor_type (w, glyph,
25950 &new_cursor_width, &active_cursor);
25951
25952 /* If cursor is currently being shown and we don't want it to be or
25953 it is in the wrong place, or the cursor type is not what we want,
25954 erase it. */
25955 if (w->phys_cursor_on_p
25956 && (!on
25957 || w->phys_cursor.x != x
25958 || w->phys_cursor.y != y
25959 || new_cursor_type != w->phys_cursor_type
25960 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25961 && new_cursor_width != w->phys_cursor_width)))
25962 erase_phys_cursor (w);
25963
25964 /* Don't check phys_cursor_on_p here because that flag is only set
25965 to zero in some cases where we know that the cursor has been
25966 completely erased, to avoid the extra work of erasing the cursor
25967 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25968 still not be visible, or it has only been partly erased. */
25969 if (on)
25970 {
25971 w->phys_cursor_ascent = glyph_row->ascent;
25972 w->phys_cursor_height = glyph_row->height;
25973
25974 /* Set phys_cursor_.* before x_draw_.* is called because some
25975 of them may need the information. */
25976 w->phys_cursor.x = x;
25977 w->phys_cursor.y = glyph_row->y;
25978 w->phys_cursor.hpos = hpos;
25979 w->phys_cursor.vpos = vpos;
25980 }
25981
25982 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25983 new_cursor_type, new_cursor_width,
25984 on, active_cursor);
25985 }
25986
25987
25988 /* Switch the display of W's cursor on or off, according to the value
25989 of ON. */
25990
25991 static void
25992 update_window_cursor (struct window *w, int on)
25993 {
25994 /* Don't update cursor in windows whose frame is in the process
25995 of being deleted. */
25996 if (w->current_matrix)
25997 {
25998 int hpos = w->phys_cursor.hpos;
25999 int vpos = w->phys_cursor.vpos;
26000 struct glyph_row *row;
26001
26002 if (vpos >= w->current_matrix->nrows
26003 || hpos >= w->current_matrix->matrix_w)
26004 return;
26005
26006 row = MATRIX_ROW (w->current_matrix, vpos);
26007
26008 /* When the window is hscrolled, cursor hpos can legitimately be
26009 out of bounds, but we draw the cursor at the corresponding
26010 window margin in that case. */
26011 if (!row->reversed_p && hpos < 0)
26012 hpos = 0;
26013 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26014 hpos = row->used[TEXT_AREA] - 1;
26015
26016 block_input ();
26017 display_and_set_cursor (w, on, hpos, vpos,
26018 w->phys_cursor.x, w->phys_cursor.y);
26019 unblock_input ();
26020 }
26021 }
26022
26023
26024 /* Call update_window_cursor with parameter ON_P on all leaf windows
26025 in the window tree rooted at W. */
26026
26027 static void
26028 update_cursor_in_window_tree (struct window *w, int on_p)
26029 {
26030 while (w)
26031 {
26032 if (!NILP (w->hchild))
26033 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26034 else if (!NILP (w->vchild))
26035 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26036 else
26037 update_window_cursor (w, on_p);
26038
26039 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26040 }
26041 }
26042
26043
26044 /* EXPORT:
26045 Display the cursor on window W, or clear it, according to ON_P.
26046 Don't change the cursor's position. */
26047
26048 void
26049 x_update_cursor (struct frame *f, int on_p)
26050 {
26051 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26052 }
26053
26054
26055 /* EXPORT:
26056 Clear the cursor of window W to background color, and mark the
26057 cursor as not shown. This is used when the text where the cursor
26058 is about to be rewritten. */
26059
26060 void
26061 x_clear_cursor (struct window *w)
26062 {
26063 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26064 update_window_cursor (w, 0);
26065 }
26066
26067 #endif /* HAVE_WINDOW_SYSTEM */
26068
26069 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26070 and MSDOS. */
26071 static void
26072 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26073 int start_hpos, int end_hpos,
26074 enum draw_glyphs_face draw)
26075 {
26076 #ifdef HAVE_WINDOW_SYSTEM
26077 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26078 {
26079 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26080 return;
26081 }
26082 #endif
26083 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26084 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26085 #endif
26086 }
26087
26088 /* Display the active region described by mouse_face_* according to DRAW. */
26089
26090 static void
26091 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26092 {
26093 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26094 struct frame *f = XFRAME (WINDOW_FRAME (w));
26095
26096 if (/* If window is in the process of being destroyed, don't bother
26097 to do anything. */
26098 w->current_matrix != NULL
26099 /* Don't update mouse highlight if hidden */
26100 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26101 /* Recognize when we are called to operate on rows that don't exist
26102 anymore. This can happen when a window is split. */
26103 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26104 {
26105 int phys_cursor_on_p = w->phys_cursor_on_p;
26106 struct glyph_row *row, *first, *last;
26107
26108 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26109 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26110
26111 for (row = first; row <= last && row->enabled_p; ++row)
26112 {
26113 int start_hpos, end_hpos, start_x;
26114
26115 /* For all but the first row, the highlight starts at column 0. */
26116 if (row == first)
26117 {
26118 /* R2L rows have BEG and END in reversed order, but the
26119 screen drawing geometry is always left to right. So
26120 we need to mirror the beginning and end of the
26121 highlighted area in R2L rows. */
26122 if (!row->reversed_p)
26123 {
26124 start_hpos = hlinfo->mouse_face_beg_col;
26125 start_x = hlinfo->mouse_face_beg_x;
26126 }
26127 else if (row == last)
26128 {
26129 start_hpos = hlinfo->mouse_face_end_col;
26130 start_x = hlinfo->mouse_face_end_x;
26131 }
26132 else
26133 {
26134 start_hpos = 0;
26135 start_x = 0;
26136 }
26137 }
26138 else if (row->reversed_p && row == last)
26139 {
26140 start_hpos = hlinfo->mouse_face_end_col;
26141 start_x = hlinfo->mouse_face_end_x;
26142 }
26143 else
26144 {
26145 start_hpos = 0;
26146 start_x = 0;
26147 }
26148
26149 if (row == last)
26150 {
26151 if (!row->reversed_p)
26152 end_hpos = hlinfo->mouse_face_end_col;
26153 else if (row == first)
26154 end_hpos = hlinfo->mouse_face_beg_col;
26155 else
26156 {
26157 end_hpos = row->used[TEXT_AREA];
26158 if (draw == DRAW_NORMAL_TEXT)
26159 row->fill_line_p = 1; /* Clear to end of line */
26160 }
26161 }
26162 else if (row->reversed_p && row == first)
26163 end_hpos = hlinfo->mouse_face_beg_col;
26164 else
26165 {
26166 end_hpos = row->used[TEXT_AREA];
26167 if (draw == DRAW_NORMAL_TEXT)
26168 row->fill_line_p = 1; /* Clear to end of line */
26169 }
26170
26171 if (end_hpos > start_hpos)
26172 {
26173 draw_row_with_mouse_face (w, start_x, row,
26174 start_hpos, end_hpos, draw);
26175
26176 row->mouse_face_p
26177 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26178 }
26179 }
26180
26181 #ifdef HAVE_WINDOW_SYSTEM
26182 /* When we've written over the cursor, arrange for it to
26183 be displayed again. */
26184 if (FRAME_WINDOW_P (f)
26185 && phys_cursor_on_p && !w->phys_cursor_on_p)
26186 {
26187 int hpos = w->phys_cursor.hpos;
26188
26189 /* When the window is hscrolled, cursor hpos can legitimately be
26190 out of bounds, but we draw the cursor at the corresponding
26191 window margin in that case. */
26192 if (!row->reversed_p && hpos < 0)
26193 hpos = 0;
26194 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26195 hpos = row->used[TEXT_AREA] - 1;
26196
26197 block_input ();
26198 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26199 w->phys_cursor.x, w->phys_cursor.y);
26200 unblock_input ();
26201 }
26202 #endif /* HAVE_WINDOW_SYSTEM */
26203 }
26204
26205 #ifdef HAVE_WINDOW_SYSTEM
26206 /* Change the mouse cursor. */
26207 if (FRAME_WINDOW_P (f))
26208 {
26209 if (draw == DRAW_NORMAL_TEXT
26210 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26211 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26212 else if (draw == DRAW_MOUSE_FACE)
26213 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26214 else
26215 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26216 }
26217 #endif /* HAVE_WINDOW_SYSTEM */
26218 }
26219
26220 /* EXPORT:
26221 Clear out the mouse-highlighted active region.
26222 Redraw it un-highlighted first. Value is non-zero if mouse
26223 face was actually drawn unhighlighted. */
26224
26225 int
26226 clear_mouse_face (Mouse_HLInfo *hlinfo)
26227 {
26228 int cleared = 0;
26229
26230 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26231 {
26232 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26233 cleared = 1;
26234 }
26235
26236 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26237 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26238 hlinfo->mouse_face_window = Qnil;
26239 hlinfo->mouse_face_overlay = Qnil;
26240 return cleared;
26241 }
26242
26243 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26244 within the mouse face on that window. */
26245 static int
26246 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26247 {
26248 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26249
26250 /* Quickly resolve the easy cases. */
26251 if (!(WINDOWP (hlinfo->mouse_face_window)
26252 && XWINDOW (hlinfo->mouse_face_window) == w))
26253 return 0;
26254 if (vpos < hlinfo->mouse_face_beg_row
26255 || vpos > hlinfo->mouse_face_end_row)
26256 return 0;
26257 if (vpos > hlinfo->mouse_face_beg_row
26258 && vpos < hlinfo->mouse_face_end_row)
26259 return 1;
26260
26261 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26262 {
26263 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26264 {
26265 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26266 return 1;
26267 }
26268 else if ((vpos == hlinfo->mouse_face_beg_row
26269 && hpos >= hlinfo->mouse_face_beg_col)
26270 || (vpos == hlinfo->mouse_face_end_row
26271 && hpos < hlinfo->mouse_face_end_col))
26272 return 1;
26273 }
26274 else
26275 {
26276 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26277 {
26278 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26279 return 1;
26280 }
26281 else if ((vpos == hlinfo->mouse_face_beg_row
26282 && hpos <= hlinfo->mouse_face_beg_col)
26283 || (vpos == hlinfo->mouse_face_end_row
26284 && hpos > hlinfo->mouse_face_end_col))
26285 return 1;
26286 }
26287 return 0;
26288 }
26289
26290
26291 /* EXPORT:
26292 Non-zero if physical cursor of window W is within mouse face. */
26293
26294 int
26295 cursor_in_mouse_face_p (struct window *w)
26296 {
26297 int hpos = w->phys_cursor.hpos;
26298 int vpos = w->phys_cursor.vpos;
26299 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26300
26301 /* When the window is hscrolled, cursor hpos can legitimately be out
26302 of bounds, but we draw the cursor at the corresponding window
26303 margin in that case. */
26304 if (!row->reversed_p && hpos < 0)
26305 hpos = 0;
26306 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26307 hpos = row->used[TEXT_AREA] - 1;
26308
26309 return coords_in_mouse_face_p (w, hpos, vpos);
26310 }
26311
26312
26313 \f
26314 /* Find the glyph rows START_ROW and END_ROW of window W that display
26315 characters between buffer positions START_CHARPOS and END_CHARPOS
26316 (excluding END_CHARPOS). DISP_STRING is a display string that
26317 covers these buffer positions. This is similar to
26318 row_containing_pos, but is more accurate when bidi reordering makes
26319 buffer positions change non-linearly with glyph rows. */
26320 static void
26321 rows_from_pos_range (struct window *w,
26322 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26323 Lisp_Object disp_string,
26324 struct glyph_row **start, struct glyph_row **end)
26325 {
26326 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26327 int last_y = window_text_bottom_y (w);
26328 struct glyph_row *row;
26329
26330 *start = NULL;
26331 *end = NULL;
26332
26333 while (!first->enabled_p
26334 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26335 first++;
26336
26337 /* Find the START row. */
26338 for (row = first;
26339 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26340 row++)
26341 {
26342 /* A row can potentially be the START row if the range of the
26343 characters it displays intersects the range
26344 [START_CHARPOS..END_CHARPOS). */
26345 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26346 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26347 /* See the commentary in row_containing_pos, for the
26348 explanation of the complicated way to check whether
26349 some position is beyond the end of the characters
26350 displayed by a row. */
26351 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26352 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26353 && !row->ends_at_zv_p
26354 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26355 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26356 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26357 && !row->ends_at_zv_p
26358 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26359 {
26360 /* Found a candidate row. Now make sure at least one of the
26361 glyphs it displays has a charpos from the range
26362 [START_CHARPOS..END_CHARPOS).
26363
26364 This is not obvious because bidi reordering could make
26365 buffer positions of a row be 1,2,3,102,101,100, and if we
26366 want to highlight characters in [50..60), we don't want
26367 this row, even though [50..60) does intersect [1..103),
26368 the range of character positions given by the row's start
26369 and end positions. */
26370 struct glyph *g = row->glyphs[TEXT_AREA];
26371 struct glyph *e = g + row->used[TEXT_AREA];
26372
26373 while (g < e)
26374 {
26375 if (((BUFFERP (g->object) || INTEGERP (g->object))
26376 && start_charpos <= g->charpos && g->charpos < end_charpos)
26377 /* A glyph that comes from DISP_STRING is by
26378 definition to be highlighted. */
26379 || EQ (g->object, disp_string))
26380 *start = row;
26381 g++;
26382 }
26383 if (*start)
26384 break;
26385 }
26386 }
26387
26388 /* Find the END row. */
26389 if (!*start
26390 /* If the last row is partially visible, start looking for END
26391 from that row, instead of starting from FIRST. */
26392 && !(row->enabled_p
26393 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26394 row = first;
26395 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26396 {
26397 struct glyph_row *next = row + 1;
26398 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26399
26400 if (!next->enabled_p
26401 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26402 /* The first row >= START whose range of displayed characters
26403 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26404 is the row END + 1. */
26405 || (start_charpos < next_start
26406 && end_charpos < next_start)
26407 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26408 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26409 && !next->ends_at_zv_p
26410 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26411 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26412 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26413 && !next->ends_at_zv_p
26414 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26415 {
26416 *end = row;
26417 break;
26418 }
26419 else
26420 {
26421 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26422 but none of the characters it displays are in the range, it is
26423 also END + 1. */
26424 struct glyph *g = next->glyphs[TEXT_AREA];
26425 struct glyph *s = g;
26426 struct glyph *e = g + next->used[TEXT_AREA];
26427
26428 while (g < e)
26429 {
26430 if (((BUFFERP (g->object) || INTEGERP (g->object))
26431 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26432 /* If the buffer position of the first glyph in
26433 the row is equal to END_CHARPOS, it means
26434 the last character to be highlighted is the
26435 newline of ROW, and we must consider NEXT as
26436 END, not END+1. */
26437 || (((!next->reversed_p && g == s)
26438 || (next->reversed_p && g == e - 1))
26439 && (g->charpos == end_charpos
26440 /* Special case for when NEXT is an
26441 empty line at ZV. */
26442 || (g->charpos == -1
26443 && !row->ends_at_zv_p
26444 && next_start == end_charpos)))))
26445 /* A glyph that comes from DISP_STRING is by
26446 definition to be highlighted. */
26447 || EQ (g->object, disp_string))
26448 break;
26449 g++;
26450 }
26451 if (g == e)
26452 {
26453 *end = row;
26454 break;
26455 }
26456 /* The first row that ends at ZV must be the last to be
26457 highlighted. */
26458 else if (next->ends_at_zv_p)
26459 {
26460 *end = next;
26461 break;
26462 }
26463 }
26464 }
26465 }
26466
26467 /* This function sets the mouse_face_* elements of HLINFO, assuming
26468 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26469 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26470 for the overlay or run of text properties specifying the mouse
26471 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26472 before-string and after-string that must also be highlighted.
26473 DISP_STRING, if non-nil, is a display string that may cover some
26474 or all of the highlighted text. */
26475
26476 static void
26477 mouse_face_from_buffer_pos (Lisp_Object window,
26478 Mouse_HLInfo *hlinfo,
26479 ptrdiff_t mouse_charpos,
26480 ptrdiff_t start_charpos,
26481 ptrdiff_t end_charpos,
26482 Lisp_Object before_string,
26483 Lisp_Object after_string,
26484 Lisp_Object disp_string)
26485 {
26486 struct window *w = XWINDOW (window);
26487 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26488 struct glyph_row *r1, *r2;
26489 struct glyph *glyph, *end;
26490 ptrdiff_t ignore, pos;
26491 int x;
26492
26493 eassert (NILP (disp_string) || STRINGP (disp_string));
26494 eassert (NILP (before_string) || STRINGP (before_string));
26495 eassert (NILP (after_string) || STRINGP (after_string));
26496
26497 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26498 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26499 if (r1 == NULL)
26500 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26501 /* If the before-string or display-string contains newlines,
26502 rows_from_pos_range skips to its last row. Move back. */
26503 if (!NILP (before_string) || !NILP (disp_string))
26504 {
26505 struct glyph_row *prev;
26506 while ((prev = r1 - 1, prev >= first)
26507 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26508 && prev->used[TEXT_AREA] > 0)
26509 {
26510 struct glyph *beg = prev->glyphs[TEXT_AREA];
26511 glyph = beg + prev->used[TEXT_AREA];
26512 while (--glyph >= beg && INTEGERP (glyph->object));
26513 if (glyph < beg
26514 || !(EQ (glyph->object, before_string)
26515 || EQ (glyph->object, disp_string)))
26516 break;
26517 r1 = prev;
26518 }
26519 }
26520 if (r2 == NULL)
26521 {
26522 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26523 hlinfo->mouse_face_past_end = 1;
26524 }
26525 else if (!NILP (after_string))
26526 {
26527 /* If the after-string has newlines, advance to its last row. */
26528 struct glyph_row *next;
26529 struct glyph_row *last
26530 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26531
26532 for (next = r2 + 1;
26533 next <= last
26534 && next->used[TEXT_AREA] > 0
26535 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26536 ++next)
26537 r2 = next;
26538 }
26539 /* The rest of the display engine assumes that mouse_face_beg_row is
26540 either above mouse_face_end_row or identical to it. But with
26541 bidi-reordered continued lines, the row for START_CHARPOS could
26542 be below the row for END_CHARPOS. If so, swap the rows and store
26543 them in correct order. */
26544 if (r1->y > r2->y)
26545 {
26546 struct glyph_row *tem = r2;
26547
26548 r2 = r1;
26549 r1 = tem;
26550 }
26551
26552 hlinfo->mouse_face_beg_y = r1->y;
26553 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26554 hlinfo->mouse_face_end_y = r2->y;
26555 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26556
26557 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26558 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26559 could be anywhere in the row and in any order. The strategy
26560 below is to find the leftmost and the rightmost glyph that
26561 belongs to either of these 3 strings, or whose position is
26562 between START_CHARPOS and END_CHARPOS, and highlight all the
26563 glyphs between those two. This may cover more than just the text
26564 between START_CHARPOS and END_CHARPOS if the range of characters
26565 strides the bidi level boundary, e.g. if the beginning is in R2L
26566 text while the end is in L2R text or vice versa. */
26567 if (!r1->reversed_p)
26568 {
26569 /* This row is in a left to right paragraph. Scan it left to
26570 right. */
26571 glyph = r1->glyphs[TEXT_AREA];
26572 end = glyph + r1->used[TEXT_AREA];
26573 x = r1->x;
26574
26575 /* Skip truncation glyphs at the start of the glyph row. */
26576 if (r1->displays_text_p)
26577 for (; glyph < end
26578 && INTEGERP (glyph->object)
26579 && glyph->charpos < 0;
26580 ++glyph)
26581 x += glyph->pixel_width;
26582
26583 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26584 or DISP_STRING, and the first glyph from buffer whose
26585 position is between START_CHARPOS and END_CHARPOS. */
26586 for (; glyph < end
26587 && !INTEGERP (glyph->object)
26588 && !EQ (glyph->object, disp_string)
26589 && !(BUFFERP (glyph->object)
26590 && (glyph->charpos >= start_charpos
26591 && glyph->charpos < end_charpos));
26592 ++glyph)
26593 {
26594 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26595 are present at buffer positions between START_CHARPOS and
26596 END_CHARPOS, or if they come from an overlay. */
26597 if (EQ (glyph->object, before_string))
26598 {
26599 pos = string_buffer_position (before_string,
26600 start_charpos);
26601 /* If pos == 0, it means before_string came from an
26602 overlay, not from a buffer position. */
26603 if (!pos || (pos >= start_charpos && pos < end_charpos))
26604 break;
26605 }
26606 else if (EQ (glyph->object, after_string))
26607 {
26608 pos = string_buffer_position (after_string, end_charpos);
26609 if (!pos || (pos >= start_charpos && pos < end_charpos))
26610 break;
26611 }
26612 x += glyph->pixel_width;
26613 }
26614 hlinfo->mouse_face_beg_x = x;
26615 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26616 }
26617 else
26618 {
26619 /* This row is in a right to left paragraph. Scan it right to
26620 left. */
26621 struct glyph *g;
26622
26623 end = r1->glyphs[TEXT_AREA] - 1;
26624 glyph = end + r1->used[TEXT_AREA];
26625
26626 /* Skip truncation glyphs at the start of the glyph row. */
26627 if (r1->displays_text_p)
26628 for (; glyph > end
26629 && INTEGERP (glyph->object)
26630 && glyph->charpos < 0;
26631 --glyph)
26632 ;
26633
26634 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26635 or DISP_STRING, and the first glyph from buffer whose
26636 position is between START_CHARPOS and END_CHARPOS. */
26637 for (; glyph > end
26638 && !INTEGERP (glyph->object)
26639 && !EQ (glyph->object, disp_string)
26640 && !(BUFFERP (glyph->object)
26641 && (glyph->charpos >= start_charpos
26642 && glyph->charpos < end_charpos));
26643 --glyph)
26644 {
26645 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26646 are present at buffer positions between START_CHARPOS and
26647 END_CHARPOS, or if they come from an overlay. */
26648 if (EQ (glyph->object, before_string))
26649 {
26650 pos = string_buffer_position (before_string, start_charpos);
26651 /* If pos == 0, it means before_string came from an
26652 overlay, not from a buffer position. */
26653 if (!pos || (pos >= start_charpos && pos < end_charpos))
26654 break;
26655 }
26656 else if (EQ (glyph->object, after_string))
26657 {
26658 pos = string_buffer_position (after_string, end_charpos);
26659 if (!pos || (pos >= start_charpos && pos < end_charpos))
26660 break;
26661 }
26662 }
26663
26664 glyph++; /* first glyph to the right of the highlighted area */
26665 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26666 x += g->pixel_width;
26667 hlinfo->mouse_face_beg_x = x;
26668 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26669 }
26670
26671 /* If the highlight ends in a different row, compute GLYPH and END
26672 for the end row. Otherwise, reuse the values computed above for
26673 the row where the highlight begins. */
26674 if (r2 != r1)
26675 {
26676 if (!r2->reversed_p)
26677 {
26678 glyph = r2->glyphs[TEXT_AREA];
26679 end = glyph + r2->used[TEXT_AREA];
26680 x = r2->x;
26681 }
26682 else
26683 {
26684 end = r2->glyphs[TEXT_AREA] - 1;
26685 glyph = end + r2->used[TEXT_AREA];
26686 }
26687 }
26688
26689 if (!r2->reversed_p)
26690 {
26691 /* Skip truncation and continuation glyphs near the end of the
26692 row, and also blanks and stretch glyphs inserted by
26693 extend_face_to_end_of_line. */
26694 while (end > glyph
26695 && INTEGERP ((end - 1)->object))
26696 --end;
26697 /* Scan the rest of the glyph row from the end, looking for the
26698 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26699 DISP_STRING, or whose position is between START_CHARPOS
26700 and END_CHARPOS */
26701 for (--end;
26702 end > glyph
26703 && !INTEGERP (end->object)
26704 && !EQ (end->object, disp_string)
26705 && !(BUFFERP (end->object)
26706 && (end->charpos >= start_charpos
26707 && end->charpos < end_charpos));
26708 --end)
26709 {
26710 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26711 are present at buffer positions between START_CHARPOS and
26712 END_CHARPOS, or if they come from an overlay. */
26713 if (EQ (end->object, before_string))
26714 {
26715 pos = string_buffer_position (before_string, start_charpos);
26716 if (!pos || (pos >= start_charpos && pos < end_charpos))
26717 break;
26718 }
26719 else if (EQ (end->object, after_string))
26720 {
26721 pos = string_buffer_position (after_string, end_charpos);
26722 if (!pos || (pos >= start_charpos && pos < end_charpos))
26723 break;
26724 }
26725 }
26726 /* Find the X coordinate of the last glyph to be highlighted. */
26727 for (; glyph <= end; ++glyph)
26728 x += glyph->pixel_width;
26729
26730 hlinfo->mouse_face_end_x = x;
26731 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26732 }
26733 else
26734 {
26735 /* Skip truncation and continuation glyphs near the end of the
26736 row, and also blanks and stretch glyphs inserted by
26737 extend_face_to_end_of_line. */
26738 x = r2->x;
26739 end++;
26740 while (end < glyph
26741 && INTEGERP (end->object))
26742 {
26743 x += end->pixel_width;
26744 ++end;
26745 }
26746 /* Scan the rest of the glyph row from the end, looking for the
26747 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26748 DISP_STRING, or whose position is between START_CHARPOS
26749 and END_CHARPOS */
26750 for ( ;
26751 end < glyph
26752 && !INTEGERP (end->object)
26753 && !EQ (end->object, disp_string)
26754 && !(BUFFERP (end->object)
26755 && (end->charpos >= start_charpos
26756 && end->charpos < end_charpos));
26757 ++end)
26758 {
26759 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26760 are present at buffer positions between START_CHARPOS and
26761 END_CHARPOS, or if they come from an overlay. */
26762 if (EQ (end->object, before_string))
26763 {
26764 pos = string_buffer_position (before_string, start_charpos);
26765 if (!pos || (pos >= start_charpos && pos < end_charpos))
26766 break;
26767 }
26768 else if (EQ (end->object, after_string))
26769 {
26770 pos = string_buffer_position (after_string, end_charpos);
26771 if (!pos || (pos >= start_charpos && pos < end_charpos))
26772 break;
26773 }
26774 x += end->pixel_width;
26775 }
26776 /* If we exited the above loop because we arrived at the last
26777 glyph of the row, and its buffer position is still not in
26778 range, it means the last character in range is the preceding
26779 newline. Bump the end column and x values to get past the
26780 last glyph. */
26781 if (end == glyph
26782 && BUFFERP (end->object)
26783 && (end->charpos < start_charpos
26784 || end->charpos >= end_charpos))
26785 {
26786 x += end->pixel_width;
26787 ++end;
26788 }
26789 hlinfo->mouse_face_end_x = x;
26790 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26791 }
26792
26793 hlinfo->mouse_face_window = window;
26794 hlinfo->mouse_face_face_id
26795 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26796 mouse_charpos + 1,
26797 !hlinfo->mouse_face_hidden, -1);
26798 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26799 }
26800
26801 /* The following function is not used anymore (replaced with
26802 mouse_face_from_string_pos), but I leave it here for the time
26803 being, in case someone would. */
26804
26805 #if 0 /* not used */
26806
26807 /* Find the position of the glyph for position POS in OBJECT in
26808 window W's current matrix, and return in *X, *Y the pixel
26809 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26810
26811 RIGHT_P non-zero means return the position of the right edge of the
26812 glyph, RIGHT_P zero means return the left edge position.
26813
26814 If no glyph for POS exists in the matrix, return the position of
26815 the glyph with the next smaller position that is in the matrix, if
26816 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26817 exists in the matrix, return the position of the glyph with the
26818 next larger position in OBJECT.
26819
26820 Value is non-zero if a glyph was found. */
26821
26822 static int
26823 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26824 int *hpos, int *vpos, int *x, int *y, int right_p)
26825 {
26826 int yb = window_text_bottom_y (w);
26827 struct glyph_row *r;
26828 struct glyph *best_glyph = NULL;
26829 struct glyph_row *best_row = NULL;
26830 int best_x = 0;
26831
26832 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26833 r->enabled_p && r->y < yb;
26834 ++r)
26835 {
26836 struct glyph *g = r->glyphs[TEXT_AREA];
26837 struct glyph *e = g + r->used[TEXT_AREA];
26838 int gx;
26839
26840 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26841 if (EQ (g->object, object))
26842 {
26843 if (g->charpos == pos)
26844 {
26845 best_glyph = g;
26846 best_x = gx;
26847 best_row = r;
26848 goto found;
26849 }
26850 else if (best_glyph == NULL
26851 || ((eabs (g->charpos - pos)
26852 < eabs (best_glyph->charpos - pos))
26853 && (right_p
26854 ? g->charpos < pos
26855 : g->charpos > pos)))
26856 {
26857 best_glyph = g;
26858 best_x = gx;
26859 best_row = r;
26860 }
26861 }
26862 }
26863
26864 found:
26865
26866 if (best_glyph)
26867 {
26868 *x = best_x;
26869 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26870
26871 if (right_p)
26872 {
26873 *x += best_glyph->pixel_width;
26874 ++*hpos;
26875 }
26876
26877 *y = best_row->y;
26878 *vpos = best_row - w->current_matrix->rows;
26879 }
26880
26881 return best_glyph != NULL;
26882 }
26883 #endif /* not used */
26884
26885 /* Find the positions of the first and the last glyphs in window W's
26886 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26887 (assumed to be a string), and return in HLINFO's mouse_face_*
26888 members the pixel and column/row coordinates of those glyphs. */
26889
26890 static void
26891 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26892 Lisp_Object object,
26893 ptrdiff_t startpos, ptrdiff_t endpos)
26894 {
26895 int yb = window_text_bottom_y (w);
26896 struct glyph_row *r;
26897 struct glyph *g, *e;
26898 int gx;
26899 int found = 0;
26900
26901 /* Find the glyph row with at least one position in the range
26902 [STARTPOS..ENDPOS], and the first glyph in that row whose
26903 position belongs to that range. */
26904 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26905 r->enabled_p && r->y < yb;
26906 ++r)
26907 {
26908 if (!r->reversed_p)
26909 {
26910 g = r->glyphs[TEXT_AREA];
26911 e = g + r->used[TEXT_AREA];
26912 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26913 if (EQ (g->object, object)
26914 && startpos <= g->charpos && g->charpos <= endpos)
26915 {
26916 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26917 hlinfo->mouse_face_beg_y = r->y;
26918 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26919 hlinfo->mouse_face_beg_x = gx;
26920 found = 1;
26921 break;
26922 }
26923 }
26924 else
26925 {
26926 struct glyph *g1;
26927
26928 e = r->glyphs[TEXT_AREA];
26929 g = e + r->used[TEXT_AREA];
26930 for ( ; g > e; --g)
26931 if (EQ ((g-1)->object, object)
26932 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26933 {
26934 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26935 hlinfo->mouse_face_beg_y = r->y;
26936 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26937 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26938 gx += g1->pixel_width;
26939 hlinfo->mouse_face_beg_x = gx;
26940 found = 1;
26941 break;
26942 }
26943 }
26944 if (found)
26945 break;
26946 }
26947
26948 if (!found)
26949 return;
26950
26951 /* Starting with the next row, look for the first row which does NOT
26952 include any glyphs whose positions are in the range. */
26953 for (++r; r->enabled_p && r->y < yb; ++r)
26954 {
26955 g = r->glyphs[TEXT_AREA];
26956 e = g + r->used[TEXT_AREA];
26957 found = 0;
26958 for ( ; g < e; ++g)
26959 if (EQ (g->object, object)
26960 && startpos <= g->charpos && g->charpos <= endpos)
26961 {
26962 found = 1;
26963 break;
26964 }
26965 if (!found)
26966 break;
26967 }
26968
26969 /* The highlighted region ends on the previous row. */
26970 r--;
26971
26972 /* Set the end row and its vertical pixel coordinate. */
26973 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26974 hlinfo->mouse_face_end_y = r->y;
26975
26976 /* Compute and set the end column and the end column's horizontal
26977 pixel coordinate. */
26978 if (!r->reversed_p)
26979 {
26980 g = r->glyphs[TEXT_AREA];
26981 e = g + r->used[TEXT_AREA];
26982 for ( ; e > g; --e)
26983 if (EQ ((e-1)->object, object)
26984 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26985 break;
26986 hlinfo->mouse_face_end_col = e - g;
26987
26988 for (gx = r->x; g < e; ++g)
26989 gx += g->pixel_width;
26990 hlinfo->mouse_face_end_x = gx;
26991 }
26992 else
26993 {
26994 e = r->glyphs[TEXT_AREA];
26995 g = e + r->used[TEXT_AREA];
26996 for (gx = r->x ; e < g; ++e)
26997 {
26998 if (EQ (e->object, object)
26999 && startpos <= e->charpos && e->charpos <= endpos)
27000 break;
27001 gx += e->pixel_width;
27002 }
27003 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27004 hlinfo->mouse_face_end_x = gx;
27005 }
27006 }
27007
27008 #ifdef HAVE_WINDOW_SYSTEM
27009
27010 /* See if position X, Y is within a hot-spot of an image. */
27011
27012 static int
27013 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27014 {
27015 if (!CONSP (hot_spot))
27016 return 0;
27017
27018 if (EQ (XCAR (hot_spot), Qrect))
27019 {
27020 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27021 Lisp_Object rect = XCDR (hot_spot);
27022 Lisp_Object tem;
27023 if (!CONSP (rect))
27024 return 0;
27025 if (!CONSP (XCAR (rect)))
27026 return 0;
27027 if (!CONSP (XCDR (rect)))
27028 return 0;
27029 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27030 return 0;
27031 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27032 return 0;
27033 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27034 return 0;
27035 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27036 return 0;
27037 return 1;
27038 }
27039 else if (EQ (XCAR (hot_spot), Qcircle))
27040 {
27041 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27042 Lisp_Object circ = XCDR (hot_spot);
27043 Lisp_Object lr, lx0, ly0;
27044 if (CONSP (circ)
27045 && CONSP (XCAR (circ))
27046 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27047 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27048 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27049 {
27050 double r = XFLOATINT (lr);
27051 double dx = XINT (lx0) - x;
27052 double dy = XINT (ly0) - y;
27053 return (dx * dx + dy * dy <= r * r);
27054 }
27055 }
27056 else if (EQ (XCAR (hot_spot), Qpoly))
27057 {
27058 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27059 if (VECTORP (XCDR (hot_spot)))
27060 {
27061 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27062 Lisp_Object *poly = v->contents;
27063 ptrdiff_t n = v->header.size;
27064 ptrdiff_t i;
27065 int inside = 0;
27066 Lisp_Object lx, ly;
27067 int x0, y0;
27068
27069 /* Need an even number of coordinates, and at least 3 edges. */
27070 if (n < 6 || n & 1)
27071 return 0;
27072
27073 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27074 If count is odd, we are inside polygon. Pixels on edges
27075 may or may not be included depending on actual geometry of the
27076 polygon. */
27077 if ((lx = poly[n-2], !INTEGERP (lx))
27078 || (ly = poly[n-1], !INTEGERP (lx)))
27079 return 0;
27080 x0 = XINT (lx), y0 = XINT (ly);
27081 for (i = 0; i < n; i += 2)
27082 {
27083 int x1 = x0, y1 = y0;
27084 if ((lx = poly[i], !INTEGERP (lx))
27085 || (ly = poly[i+1], !INTEGERP (ly)))
27086 return 0;
27087 x0 = XINT (lx), y0 = XINT (ly);
27088
27089 /* Does this segment cross the X line? */
27090 if (x0 >= x)
27091 {
27092 if (x1 >= x)
27093 continue;
27094 }
27095 else if (x1 < x)
27096 continue;
27097 if (y > y0 && y > y1)
27098 continue;
27099 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27100 inside = !inside;
27101 }
27102 return inside;
27103 }
27104 }
27105 return 0;
27106 }
27107
27108 Lisp_Object
27109 find_hot_spot (Lisp_Object map, int x, int y)
27110 {
27111 while (CONSP (map))
27112 {
27113 if (CONSP (XCAR (map))
27114 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27115 return XCAR (map);
27116 map = XCDR (map);
27117 }
27118
27119 return Qnil;
27120 }
27121
27122 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27123 3, 3, 0,
27124 doc: /* Lookup in image map MAP coordinates X and Y.
27125 An image map is an alist where each element has the format (AREA ID PLIST).
27126 An AREA is specified as either a rectangle, a circle, or a polygon:
27127 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27128 pixel coordinates of the upper left and bottom right corners.
27129 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27130 and the radius of the circle; r may be a float or integer.
27131 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27132 vector describes one corner in the polygon.
27133 Returns the alist element for the first matching AREA in MAP. */)
27134 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27135 {
27136 if (NILP (map))
27137 return Qnil;
27138
27139 CHECK_NUMBER (x);
27140 CHECK_NUMBER (y);
27141
27142 return find_hot_spot (map,
27143 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27144 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27145 }
27146
27147
27148 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27149 static void
27150 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27151 {
27152 /* Do not change cursor shape while dragging mouse. */
27153 if (!NILP (do_mouse_tracking))
27154 return;
27155
27156 if (!NILP (pointer))
27157 {
27158 if (EQ (pointer, Qarrow))
27159 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27160 else if (EQ (pointer, Qhand))
27161 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27162 else if (EQ (pointer, Qtext))
27163 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27164 else if (EQ (pointer, intern ("hdrag")))
27165 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27166 #ifdef HAVE_X_WINDOWS
27167 else if (EQ (pointer, intern ("vdrag")))
27168 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27169 #endif
27170 else if (EQ (pointer, intern ("hourglass")))
27171 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27172 else if (EQ (pointer, Qmodeline))
27173 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27174 else
27175 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27176 }
27177
27178 if (cursor != No_Cursor)
27179 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27180 }
27181
27182 #endif /* HAVE_WINDOW_SYSTEM */
27183
27184 /* Take proper action when mouse has moved to the mode or header line
27185 or marginal area AREA of window W, x-position X and y-position Y.
27186 X is relative to the start of the text display area of W, so the
27187 width of bitmap areas and scroll bars must be subtracted to get a
27188 position relative to the start of the mode line. */
27189
27190 static void
27191 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27192 enum window_part area)
27193 {
27194 struct window *w = XWINDOW (window);
27195 struct frame *f = XFRAME (w->frame);
27196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27197 #ifdef HAVE_WINDOW_SYSTEM
27198 Display_Info *dpyinfo;
27199 #endif
27200 Cursor cursor = No_Cursor;
27201 Lisp_Object pointer = Qnil;
27202 int dx, dy, width, height;
27203 ptrdiff_t charpos;
27204 Lisp_Object string, object = Qnil;
27205 Lisp_Object pos IF_LINT (= Qnil), help;
27206
27207 Lisp_Object mouse_face;
27208 int original_x_pixel = x;
27209 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27210 struct glyph_row *row IF_LINT (= 0);
27211
27212 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27213 {
27214 int x0;
27215 struct glyph *end;
27216
27217 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27218 returns them in row/column units! */
27219 string = mode_line_string (w, area, &x, &y, &charpos,
27220 &object, &dx, &dy, &width, &height);
27221
27222 row = (area == ON_MODE_LINE
27223 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27224 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27225
27226 /* Find the glyph under the mouse pointer. */
27227 if (row->mode_line_p && row->enabled_p)
27228 {
27229 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27230 end = glyph + row->used[TEXT_AREA];
27231
27232 for (x0 = original_x_pixel;
27233 glyph < end && x0 >= glyph->pixel_width;
27234 ++glyph)
27235 x0 -= glyph->pixel_width;
27236
27237 if (glyph >= end)
27238 glyph = NULL;
27239 }
27240 }
27241 else
27242 {
27243 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27244 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27245 returns them in row/column units! */
27246 string = marginal_area_string (w, area, &x, &y, &charpos,
27247 &object, &dx, &dy, &width, &height);
27248 }
27249
27250 help = Qnil;
27251
27252 #ifdef HAVE_WINDOW_SYSTEM
27253 if (IMAGEP (object))
27254 {
27255 Lisp_Object image_map, hotspot;
27256 if ((image_map = Fplist_get (XCDR (object), QCmap),
27257 !NILP (image_map))
27258 && (hotspot = find_hot_spot (image_map, dx, dy),
27259 CONSP (hotspot))
27260 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27261 {
27262 Lisp_Object plist;
27263
27264 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27265 If so, we could look for mouse-enter, mouse-leave
27266 properties in PLIST (and do something...). */
27267 hotspot = XCDR (hotspot);
27268 if (CONSP (hotspot)
27269 && (plist = XCAR (hotspot), CONSP (plist)))
27270 {
27271 pointer = Fplist_get (plist, Qpointer);
27272 if (NILP (pointer))
27273 pointer = Qhand;
27274 help = Fplist_get (plist, Qhelp_echo);
27275 if (!NILP (help))
27276 {
27277 help_echo_string = help;
27278 XSETWINDOW (help_echo_window, w);
27279 help_echo_object = w->buffer;
27280 help_echo_pos = charpos;
27281 }
27282 }
27283 }
27284 if (NILP (pointer))
27285 pointer = Fplist_get (XCDR (object), QCpointer);
27286 }
27287 #endif /* HAVE_WINDOW_SYSTEM */
27288
27289 if (STRINGP (string))
27290 pos = make_number (charpos);
27291
27292 /* Set the help text and mouse pointer. If the mouse is on a part
27293 of the mode line without any text (e.g. past the right edge of
27294 the mode line text), use the default help text and pointer. */
27295 if (STRINGP (string) || area == ON_MODE_LINE)
27296 {
27297 /* Arrange to display the help by setting the global variables
27298 help_echo_string, help_echo_object, and help_echo_pos. */
27299 if (NILP (help))
27300 {
27301 if (STRINGP (string))
27302 help = Fget_text_property (pos, Qhelp_echo, string);
27303
27304 if (!NILP (help))
27305 {
27306 help_echo_string = help;
27307 XSETWINDOW (help_echo_window, w);
27308 help_echo_object = string;
27309 help_echo_pos = charpos;
27310 }
27311 else if (area == ON_MODE_LINE)
27312 {
27313 Lisp_Object default_help
27314 = buffer_local_value_1 (Qmode_line_default_help_echo,
27315 w->buffer);
27316
27317 if (STRINGP (default_help))
27318 {
27319 help_echo_string = default_help;
27320 XSETWINDOW (help_echo_window, w);
27321 help_echo_object = Qnil;
27322 help_echo_pos = -1;
27323 }
27324 }
27325 }
27326
27327 #ifdef HAVE_WINDOW_SYSTEM
27328 /* Change the mouse pointer according to what is under it. */
27329 if (FRAME_WINDOW_P (f))
27330 {
27331 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27332 if (STRINGP (string))
27333 {
27334 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27335
27336 if (NILP (pointer))
27337 pointer = Fget_text_property (pos, Qpointer, string);
27338
27339 /* Change the mouse pointer according to what is under X/Y. */
27340 if (NILP (pointer)
27341 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27342 {
27343 Lisp_Object map;
27344 map = Fget_text_property (pos, Qlocal_map, string);
27345 if (!KEYMAPP (map))
27346 map = Fget_text_property (pos, Qkeymap, string);
27347 if (!KEYMAPP (map))
27348 cursor = dpyinfo->vertical_scroll_bar_cursor;
27349 }
27350 }
27351 else
27352 /* Default mode-line pointer. */
27353 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27354 }
27355 #endif
27356 }
27357
27358 /* Change the mouse face according to what is under X/Y. */
27359 if (STRINGP (string))
27360 {
27361 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27362 if (!NILP (mouse_face)
27363 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27364 && glyph)
27365 {
27366 Lisp_Object b, e;
27367
27368 struct glyph * tmp_glyph;
27369
27370 int gpos;
27371 int gseq_length;
27372 int total_pixel_width;
27373 ptrdiff_t begpos, endpos, ignore;
27374
27375 int vpos, hpos;
27376
27377 b = Fprevious_single_property_change (make_number (charpos + 1),
27378 Qmouse_face, string, Qnil);
27379 if (NILP (b))
27380 begpos = 0;
27381 else
27382 begpos = XINT (b);
27383
27384 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27385 if (NILP (e))
27386 endpos = SCHARS (string);
27387 else
27388 endpos = XINT (e);
27389
27390 /* Calculate the glyph position GPOS of GLYPH in the
27391 displayed string, relative to the beginning of the
27392 highlighted part of the string.
27393
27394 Note: GPOS is different from CHARPOS. CHARPOS is the
27395 position of GLYPH in the internal string object. A mode
27396 line string format has structures which are converted to
27397 a flattened string by the Emacs Lisp interpreter. The
27398 internal string is an element of those structures. The
27399 displayed string is the flattened string. */
27400 tmp_glyph = row_start_glyph;
27401 while (tmp_glyph < glyph
27402 && (!(EQ (tmp_glyph->object, glyph->object)
27403 && begpos <= tmp_glyph->charpos
27404 && tmp_glyph->charpos < endpos)))
27405 tmp_glyph++;
27406 gpos = glyph - tmp_glyph;
27407
27408 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27409 the highlighted part of the displayed string to which
27410 GLYPH belongs. Note: GSEQ_LENGTH is different from
27411 SCHARS (STRING), because the latter returns the length of
27412 the internal string. */
27413 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27414 tmp_glyph > glyph
27415 && (!(EQ (tmp_glyph->object, glyph->object)
27416 && begpos <= tmp_glyph->charpos
27417 && tmp_glyph->charpos < endpos));
27418 tmp_glyph--)
27419 ;
27420 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27421
27422 /* Calculate the total pixel width of all the glyphs between
27423 the beginning of the highlighted area and GLYPH. */
27424 total_pixel_width = 0;
27425 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27426 total_pixel_width += tmp_glyph->pixel_width;
27427
27428 /* Pre calculation of re-rendering position. Note: X is in
27429 column units here, after the call to mode_line_string or
27430 marginal_area_string. */
27431 hpos = x - gpos;
27432 vpos = (area == ON_MODE_LINE
27433 ? (w->current_matrix)->nrows - 1
27434 : 0);
27435
27436 /* If GLYPH's position is included in the region that is
27437 already drawn in mouse face, we have nothing to do. */
27438 if ( EQ (window, hlinfo->mouse_face_window)
27439 && (!row->reversed_p
27440 ? (hlinfo->mouse_face_beg_col <= hpos
27441 && hpos < hlinfo->mouse_face_end_col)
27442 /* In R2L rows we swap BEG and END, see below. */
27443 : (hlinfo->mouse_face_end_col <= hpos
27444 && hpos < hlinfo->mouse_face_beg_col))
27445 && hlinfo->mouse_face_beg_row == vpos )
27446 return;
27447
27448 if (clear_mouse_face (hlinfo))
27449 cursor = No_Cursor;
27450
27451 if (!row->reversed_p)
27452 {
27453 hlinfo->mouse_face_beg_col = hpos;
27454 hlinfo->mouse_face_beg_x = original_x_pixel
27455 - (total_pixel_width + dx);
27456 hlinfo->mouse_face_end_col = hpos + gseq_length;
27457 hlinfo->mouse_face_end_x = 0;
27458 }
27459 else
27460 {
27461 /* In R2L rows, show_mouse_face expects BEG and END
27462 coordinates to be swapped. */
27463 hlinfo->mouse_face_end_col = hpos;
27464 hlinfo->mouse_face_end_x = original_x_pixel
27465 - (total_pixel_width + dx);
27466 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27467 hlinfo->mouse_face_beg_x = 0;
27468 }
27469
27470 hlinfo->mouse_face_beg_row = vpos;
27471 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27472 hlinfo->mouse_face_beg_y = 0;
27473 hlinfo->mouse_face_end_y = 0;
27474 hlinfo->mouse_face_past_end = 0;
27475 hlinfo->mouse_face_window = window;
27476
27477 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27478 charpos,
27479 0, 0, 0,
27480 &ignore,
27481 glyph->face_id,
27482 1);
27483 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27484
27485 if (NILP (pointer))
27486 pointer = Qhand;
27487 }
27488 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27489 clear_mouse_face (hlinfo);
27490 }
27491 #ifdef HAVE_WINDOW_SYSTEM
27492 if (FRAME_WINDOW_P (f))
27493 define_frame_cursor1 (f, cursor, pointer);
27494 #endif
27495 }
27496
27497
27498 /* EXPORT:
27499 Take proper action when the mouse has moved to position X, Y on
27500 frame F as regards highlighting characters that have mouse-face
27501 properties. Also de-highlighting chars where the mouse was before.
27502 X and Y can be negative or out of range. */
27503
27504 void
27505 note_mouse_highlight (struct frame *f, int x, int y)
27506 {
27507 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27508 enum window_part part = ON_NOTHING;
27509 Lisp_Object window;
27510 struct window *w;
27511 Cursor cursor = No_Cursor;
27512 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27513 struct buffer *b;
27514
27515 /* When a menu is active, don't highlight because this looks odd. */
27516 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27517 if (popup_activated ())
27518 return;
27519 #endif
27520
27521 if (NILP (Vmouse_highlight)
27522 || !f->glyphs_initialized_p
27523 || f->pointer_invisible)
27524 return;
27525
27526 hlinfo->mouse_face_mouse_x = x;
27527 hlinfo->mouse_face_mouse_y = y;
27528 hlinfo->mouse_face_mouse_frame = f;
27529
27530 if (hlinfo->mouse_face_defer)
27531 return;
27532
27533 /* Which window is that in? */
27534 window = window_from_coordinates (f, x, y, &part, 1);
27535
27536 /* If displaying active text in another window, clear that. */
27537 if (! EQ (window, hlinfo->mouse_face_window)
27538 /* Also clear if we move out of text area in same window. */
27539 || (!NILP (hlinfo->mouse_face_window)
27540 && !NILP (window)
27541 && part != ON_TEXT
27542 && part != ON_MODE_LINE
27543 && part != ON_HEADER_LINE))
27544 clear_mouse_face (hlinfo);
27545
27546 /* Not on a window -> return. */
27547 if (!WINDOWP (window))
27548 return;
27549
27550 /* Reset help_echo_string. It will get recomputed below. */
27551 help_echo_string = Qnil;
27552
27553 /* Convert to window-relative pixel coordinates. */
27554 w = XWINDOW (window);
27555 frame_to_window_pixel_xy (w, &x, &y);
27556
27557 #ifdef HAVE_WINDOW_SYSTEM
27558 /* Handle tool-bar window differently since it doesn't display a
27559 buffer. */
27560 if (EQ (window, f->tool_bar_window))
27561 {
27562 note_tool_bar_highlight (f, x, y);
27563 return;
27564 }
27565 #endif
27566
27567 /* Mouse is on the mode, header line or margin? */
27568 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27569 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27570 {
27571 note_mode_line_or_margin_highlight (window, x, y, part);
27572 return;
27573 }
27574
27575 #ifdef HAVE_WINDOW_SYSTEM
27576 if (part == ON_VERTICAL_BORDER)
27577 {
27578 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27579 help_echo_string = build_string ("drag-mouse-1: resize");
27580 }
27581 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27582 || part == ON_SCROLL_BAR)
27583 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27584 else
27585 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27586 #endif
27587
27588 /* Are we in a window whose display is up to date?
27589 And verify the buffer's text has not changed. */
27590 b = XBUFFER (w->buffer);
27591 if (part == ON_TEXT
27592 && w->window_end_valid
27593 && w->last_modified == BUF_MODIFF (b)
27594 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27595 {
27596 int hpos, vpos, dx, dy, area = LAST_AREA;
27597 ptrdiff_t pos;
27598 struct glyph *glyph;
27599 Lisp_Object object;
27600 Lisp_Object mouse_face = Qnil, position;
27601 Lisp_Object *overlay_vec = NULL;
27602 ptrdiff_t i, noverlays;
27603 struct buffer *obuf;
27604 ptrdiff_t obegv, ozv;
27605 int same_region;
27606
27607 /* Find the glyph under X/Y. */
27608 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27609
27610 #ifdef HAVE_WINDOW_SYSTEM
27611 /* Look for :pointer property on image. */
27612 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27613 {
27614 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27615 if (img != NULL && IMAGEP (img->spec))
27616 {
27617 Lisp_Object image_map, hotspot;
27618 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27619 !NILP (image_map))
27620 && (hotspot = find_hot_spot (image_map,
27621 glyph->slice.img.x + dx,
27622 glyph->slice.img.y + dy),
27623 CONSP (hotspot))
27624 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27625 {
27626 Lisp_Object plist;
27627
27628 /* Could check XCAR (hotspot) to see if we enter/leave
27629 this hot-spot.
27630 If so, we could look for mouse-enter, mouse-leave
27631 properties in PLIST (and do something...). */
27632 hotspot = XCDR (hotspot);
27633 if (CONSP (hotspot)
27634 && (plist = XCAR (hotspot), CONSP (plist)))
27635 {
27636 pointer = Fplist_get (plist, Qpointer);
27637 if (NILP (pointer))
27638 pointer = Qhand;
27639 help_echo_string = Fplist_get (plist, Qhelp_echo);
27640 if (!NILP (help_echo_string))
27641 {
27642 help_echo_window = window;
27643 help_echo_object = glyph->object;
27644 help_echo_pos = glyph->charpos;
27645 }
27646 }
27647 }
27648 if (NILP (pointer))
27649 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27650 }
27651 }
27652 #endif /* HAVE_WINDOW_SYSTEM */
27653
27654 /* Clear mouse face if X/Y not over text. */
27655 if (glyph == NULL
27656 || area != TEXT_AREA
27657 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27658 /* Glyph's OBJECT is an integer for glyphs inserted by the
27659 display engine for its internal purposes, like truncation
27660 and continuation glyphs and blanks beyond the end of
27661 line's text on text terminals. If we are over such a
27662 glyph, we are not over any text. */
27663 || INTEGERP (glyph->object)
27664 /* R2L rows have a stretch glyph at their front, which
27665 stands for no text, whereas L2R rows have no glyphs at
27666 all beyond the end of text. Treat such stretch glyphs
27667 like we do with NULL glyphs in L2R rows. */
27668 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27669 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27670 && glyph->type == STRETCH_GLYPH
27671 && glyph->avoid_cursor_p))
27672 {
27673 if (clear_mouse_face (hlinfo))
27674 cursor = No_Cursor;
27675 #ifdef HAVE_WINDOW_SYSTEM
27676 if (FRAME_WINDOW_P (f) && NILP (pointer))
27677 {
27678 if (area != TEXT_AREA)
27679 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27680 else
27681 pointer = Vvoid_text_area_pointer;
27682 }
27683 #endif
27684 goto set_cursor;
27685 }
27686
27687 pos = glyph->charpos;
27688 object = glyph->object;
27689 if (!STRINGP (object) && !BUFFERP (object))
27690 goto set_cursor;
27691
27692 /* If we get an out-of-range value, return now; avoid an error. */
27693 if (BUFFERP (object) && pos > BUF_Z (b))
27694 goto set_cursor;
27695
27696 /* Make the window's buffer temporarily current for
27697 overlays_at and compute_char_face. */
27698 obuf = current_buffer;
27699 current_buffer = b;
27700 obegv = BEGV;
27701 ozv = ZV;
27702 BEGV = BEG;
27703 ZV = Z;
27704
27705 /* Is this char mouse-active or does it have help-echo? */
27706 position = make_number (pos);
27707
27708 if (BUFFERP (object))
27709 {
27710 /* Put all the overlays we want in a vector in overlay_vec. */
27711 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27712 /* Sort overlays into increasing priority order. */
27713 noverlays = sort_overlays (overlay_vec, noverlays, w);
27714 }
27715 else
27716 noverlays = 0;
27717
27718 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27719
27720 if (same_region)
27721 cursor = No_Cursor;
27722
27723 /* Check mouse-face highlighting. */
27724 if (! same_region
27725 /* If there exists an overlay with mouse-face overlapping
27726 the one we are currently highlighting, we have to
27727 check if we enter the overlapping overlay, and then
27728 highlight only that. */
27729 || (OVERLAYP (hlinfo->mouse_face_overlay)
27730 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27731 {
27732 /* Find the highest priority overlay with a mouse-face. */
27733 Lisp_Object overlay = Qnil;
27734 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27735 {
27736 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27737 if (!NILP (mouse_face))
27738 overlay = overlay_vec[i];
27739 }
27740
27741 /* If we're highlighting the same overlay as before, there's
27742 no need to do that again. */
27743 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27744 goto check_help_echo;
27745 hlinfo->mouse_face_overlay = overlay;
27746
27747 /* Clear the display of the old active region, if any. */
27748 if (clear_mouse_face (hlinfo))
27749 cursor = No_Cursor;
27750
27751 /* If no overlay applies, get a text property. */
27752 if (NILP (overlay))
27753 mouse_face = Fget_text_property (position, Qmouse_face, object);
27754
27755 /* Next, compute the bounds of the mouse highlighting and
27756 display it. */
27757 if (!NILP (mouse_face) && STRINGP (object))
27758 {
27759 /* The mouse-highlighting comes from a display string
27760 with a mouse-face. */
27761 Lisp_Object s, e;
27762 ptrdiff_t ignore;
27763
27764 s = Fprevious_single_property_change
27765 (make_number (pos + 1), Qmouse_face, object, Qnil);
27766 e = Fnext_single_property_change
27767 (position, Qmouse_face, object, Qnil);
27768 if (NILP (s))
27769 s = make_number (0);
27770 if (NILP (e))
27771 e = make_number (SCHARS (object) - 1);
27772 mouse_face_from_string_pos (w, hlinfo, object,
27773 XINT (s), XINT (e));
27774 hlinfo->mouse_face_past_end = 0;
27775 hlinfo->mouse_face_window = window;
27776 hlinfo->mouse_face_face_id
27777 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27778 glyph->face_id, 1);
27779 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27780 cursor = No_Cursor;
27781 }
27782 else
27783 {
27784 /* The mouse-highlighting, if any, comes from an overlay
27785 or text property in the buffer. */
27786 Lisp_Object buffer IF_LINT (= Qnil);
27787 Lisp_Object disp_string IF_LINT (= Qnil);
27788
27789 if (STRINGP (object))
27790 {
27791 /* If we are on a display string with no mouse-face,
27792 check if the text under it has one. */
27793 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27794 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27795 pos = string_buffer_position (object, start);
27796 if (pos > 0)
27797 {
27798 mouse_face = get_char_property_and_overlay
27799 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27800 buffer = w->buffer;
27801 disp_string = object;
27802 }
27803 }
27804 else
27805 {
27806 buffer = object;
27807 disp_string = Qnil;
27808 }
27809
27810 if (!NILP (mouse_face))
27811 {
27812 Lisp_Object before, after;
27813 Lisp_Object before_string, after_string;
27814 /* To correctly find the limits of mouse highlight
27815 in a bidi-reordered buffer, we must not use the
27816 optimization of limiting the search in
27817 previous-single-property-change and
27818 next-single-property-change, because
27819 rows_from_pos_range needs the real start and end
27820 positions to DTRT in this case. That's because
27821 the first row visible in a window does not
27822 necessarily display the character whose position
27823 is the smallest. */
27824 Lisp_Object lim1 =
27825 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27826 ? Fmarker_position (w->start)
27827 : Qnil;
27828 Lisp_Object lim2 =
27829 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27830 ? make_number (BUF_Z (XBUFFER (buffer))
27831 - XFASTINT (w->window_end_pos))
27832 : Qnil;
27833
27834 if (NILP (overlay))
27835 {
27836 /* Handle the text property case. */
27837 before = Fprevious_single_property_change
27838 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27839 after = Fnext_single_property_change
27840 (make_number (pos), Qmouse_face, buffer, lim2);
27841 before_string = after_string = Qnil;
27842 }
27843 else
27844 {
27845 /* Handle the overlay case. */
27846 before = Foverlay_start (overlay);
27847 after = Foverlay_end (overlay);
27848 before_string = Foverlay_get (overlay, Qbefore_string);
27849 after_string = Foverlay_get (overlay, Qafter_string);
27850
27851 if (!STRINGP (before_string)) before_string = Qnil;
27852 if (!STRINGP (after_string)) after_string = Qnil;
27853 }
27854
27855 mouse_face_from_buffer_pos (window, hlinfo, pos,
27856 NILP (before)
27857 ? 1
27858 : XFASTINT (before),
27859 NILP (after)
27860 ? BUF_Z (XBUFFER (buffer))
27861 : XFASTINT (after),
27862 before_string, after_string,
27863 disp_string);
27864 cursor = No_Cursor;
27865 }
27866 }
27867 }
27868
27869 check_help_echo:
27870
27871 /* Look for a `help-echo' property. */
27872 if (NILP (help_echo_string)) {
27873 Lisp_Object help, overlay;
27874
27875 /* Check overlays first. */
27876 help = overlay = Qnil;
27877 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27878 {
27879 overlay = overlay_vec[i];
27880 help = Foverlay_get (overlay, Qhelp_echo);
27881 }
27882
27883 if (!NILP (help))
27884 {
27885 help_echo_string = help;
27886 help_echo_window = window;
27887 help_echo_object = overlay;
27888 help_echo_pos = pos;
27889 }
27890 else
27891 {
27892 Lisp_Object obj = glyph->object;
27893 ptrdiff_t charpos = glyph->charpos;
27894
27895 /* Try text properties. */
27896 if (STRINGP (obj)
27897 && charpos >= 0
27898 && charpos < SCHARS (obj))
27899 {
27900 help = Fget_text_property (make_number (charpos),
27901 Qhelp_echo, obj);
27902 if (NILP (help))
27903 {
27904 /* If the string itself doesn't specify a help-echo,
27905 see if the buffer text ``under'' it does. */
27906 struct glyph_row *r
27907 = MATRIX_ROW (w->current_matrix, vpos);
27908 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27909 ptrdiff_t p = string_buffer_position (obj, start);
27910 if (p > 0)
27911 {
27912 help = Fget_char_property (make_number (p),
27913 Qhelp_echo, w->buffer);
27914 if (!NILP (help))
27915 {
27916 charpos = p;
27917 obj = w->buffer;
27918 }
27919 }
27920 }
27921 }
27922 else if (BUFFERP (obj)
27923 && charpos >= BEGV
27924 && charpos < ZV)
27925 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27926 obj);
27927
27928 if (!NILP (help))
27929 {
27930 help_echo_string = help;
27931 help_echo_window = window;
27932 help_echo_object = obj;
27933 help_echo_pos = charpos;
27934 }
27935 }
27936 }
27937
27938 #ifdef HAVE_WINDOW_SYSTEM
27939 /* Look for a `pointer' property. */
27940 if (FRAME_WINDOW_P (f) && NILP (pointer))
27941 {
27942 /* Check overlays first. */
27943 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27944 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27945
27946 if (NILP (pointer))
27947 {
27948 Lisp_Object obj = glyph->object;
27949 ptrdiff_t charpos = glyph->charpos;
27950
27951 /* Try text properties. */
27952 if (STRINGP (obj)
27953 && charpos >= 0
27954 && charpos < SCHARS (obj))
27955 {
27956 pointer = Fget_text_property (make_number (charpos),
27957 Qpointer, obj);
27958 if (NILP (pointer))
27959 {
27960 /* If the string itself doesn't specify a pointer,
27961 see if the buffer text ``under'' it does. */
27962 struct glyph_row *r
27963 = MATRIX_ROW (w->current_matrix, vpos);
27964 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27965 ptrdiff_t p = string_buffer_position (obj, start);
27966 if (p > 0)
27967 pointer = Fget_char_property (make_number (p),
27968 Qpointer, w->buffer);
27969 }
27970 }
27971 else if (BUFFERP (obj)
27972 && charpos >= BEGV
27973 && charpos < ZV)
27974 pointer = Fget_text_property (make_number (charpos),
27975 Qpointer, obj);
27976 }
27977 }
27978 #endif /* HAVE_WINDOW_SYSTEM */
27979
27980 BEGV = obegv;
27981 ZV = ozv;
27982 current_buffer = obuf;
27983 }
27984
27985 set_cursor:
27986
27987 #ifdef HAVE_WINDOW_SYSTEM
27988 if (FRAME_WINDOW_P (f))
27989 define_frame_cursor1 (f, cursor, pointer);
27990 #else
27991 /* This is here to prevent a compiler error, about "label at end of
27992 compound statement". */
27993 return;
27994 #endif
27995 }
27996
27997
27998 /* EXPORT for RIF:
27999 Clear any mouse-face on window W. This function is part of the
28000 redisplay interface, and is called from try_window_id and similar
28001 functions to ensure the mouse-highlight is off. */
28002
28003 void
28004 x_clear_window_mouse_face (struct window *w)
28005 {
28006 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28007 Lisp_Object window;
28008
28009 block_input ();
28010 XSETWINDOW (window, w);
28011 if (EQ (window, hlinfo->mouse_face_window))
28012 clear_mouse_face (hlinfo);
28013 unblock_input ();
28014 }
28015
28016
28017 /* EXPORT:
28018 Just discard the mouse face information for frame F, if any.
28019 This is used when the size of F is changed. */
28020
28021 void
28022 cancel_mouse_face (struct frame *f)
28023 {
28024 Lisp_Object window;
28025 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28026
28027 window = hlinfo->mouse_face_window;
28028 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28029 {
28030 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28031 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28032 hlinfo->mouse_face_window = Qnil;
28033 }
28034 }
28035
28036
28037 \f
28038 /***********************************************************************
28039 Exposure Events
28040 ***********************************************************************/
28041
28042 #ifdef HAVE_WINDOW_SYSTEM
28043
28044 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28045 which intersects rectangle R. R is in window-relative coordinates. */
28046
28047 static void
28048 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28049 enum glyph_row_area area)
28050 {
28051 struct glyph *first = row->glyphs[area];
28052 struct glyph *end = row->glyphs[area] + row->used[area];
28053 struct glyph *last;
28054 int first_x, start_x, x;
28055
28056 if (area == TEXT_AREA && row->fill_line_p)
28057 /* If row extends face to end of line write the whole line. */
28058 draw_glyphs (w, 0, row, area,
28059 0, row->used[area],
28060 DRAW_NORMAL_TEXT, 0);
28061 else
28062 {
28063 /* Set START_X to the window-relative start position for drawing glyphs of
28064 AREA. The first glyph of the text area can be partially visible.
28065 The first glyphs of other areas cannot. */
28066 start_x = window_box_left_offset (w, area);
28067 x = start_x;
28068 if (area == TEXT_AREA)
28069 x += row->x;
28070
28071 /* Find the first glyph that must be redrawn. */
28072 while (first < end
28073 && x + first->pixel_width < r->x)
28074 {
28075 x += first->pixel_width;
28076 ++first;
28077 }
28078
28079 /* Find the last one. */
28080 last = first;
28081 first_x = x;
28082 while (last < end
28083 && x < r->x + r->width)
28084 {
28085 x += last->pixel_width;
28086 ++last;
28087 }
28088
28089 /* Repaint. */
28090 if (last > first)
28091 draw_glyphs (w, first_x - start_x, row, area,
28092 first - row->glyphs[area], last - row->glyphs[area],
28093 DRAW_NORMAL_TEXT, 0);
28094 }
28095 }
28096
28097
28098 /* Redraw the parts of the glyph row ROW on window W intersecting
28099 rectangle R. R is in window-relative coordinates. Value is
28100 non-zero if mouse-face was overwritten. */
28101
28102 static int
28103 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28104 {
28105 eassert (row->enabled_p);
28106
28107 if (row->mode_line_p || w->pseudo_window_p)
28108 draw_glyphs (w, 0, row, TEXT_AREA,
28109 0, row->used[TEXT_AREA],
28110 DRAW_NORMAL_TEXT, 0);
28111 else
28112 {
28113 if (row->used[LEFT_MARGIN_AREA])
28114 expose_area (w, row, r, LEFT_MARGIN_AREA);
28115 if (row->used[TEXT_AREA])
28116 expose_area (w, row, r, TEXT_AREA);
28117 if (row->used[RIGHT_MARGIN_AREA])
28118 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28119 draw_row_fringe_bitmaps (w, row);
28120 }
28121
28122 return row->mouse_face_p;
28123 }
28124
28125
28126 /* Redraw those parts of glyphs rows during expose event handling that
28127 overlap other rows. Redrawing of an exposed line writes over parts
28128 of lines overlapping that exposed line; this function fixes that.
28129
28130 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28131 row in W's current matrix that is exposed and overlaps other rows.
28132 LAST_OVERLAPPING_ROW is the last such row. */
28133
28134 static void
28135 expose_overlaps (struct window *w,
28136 struct glyph_row *first_overlapping_row,
28137 struct glyph_row *last_overlapping_row,
28138 XRectangle *r)
28139 {
28140 struct glyph_row *row;
28141
28142 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28143 if (row->overlapping_p)
28144 {
28145 eassert (row->enabled_p && !row->mode_line_p);
28146
28147 row->clip = r;
28148 if (row->used[LEFT_MARGIN_AREA])
28149 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28150
28151 if (row->used[TEXT_AREA])
28152 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28153
28154 if (row->used[RIGHT_MARGIN_AREA])
28155 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28156 row->clip = NULL;
28157 }
28158 }
28159
28160
28161 /* Return non-zero if W's cursor intersects rectangle R. */
28162
28163 static int
28164 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28165 {
28166 XRectangle cr, result;
28167 struct glyph *cursor_glyph;
28168 struct glyph_row *row;
28169
28170 if (w->phys_cursor.vpos >= 0
28171 && w->phys_cursor.vpos < w->current_matrix->nrows
28172 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28173 row->enabled_p)
28174 && row->cursor_in_fringe_p)
28175 {
28176 /* Cursor is in the fringe. */
28177 cr.x = window_box_right_offset (w,
28178 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28179 ? RIGHT_MARGIN_AREA
28180 : TEXT_AREA));
28181 cr.y = row->y;
28182 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28183 cr.height = row->height;
28184 return x_intersect_rectangles (&cr, r, &result);
28185 }
28186
28187 cursor_glyph = get_phys_cursor_glyph (w);
28188 if (cursor_glyph)
28189 {
28190 /* r is relative to W's box, but w->phys_cursor.x is relative
28191 to left edge of W's TEXT area. Adjust it. */
28192 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28193 cr.y = w->phys_cursor.y;
28194 cr.width = cursor_glyph->pixel_width;
28195 cr.height = w->phys_cursor_height;
28196 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28197 I assume the effect is the same -- and this is portable. */
28198 return x_intersect_rectangles (&cr, r, &result);
28199 }
28200 /* If we don't understand the format, pretend we're not in the hot-spot. */
28201 return 0;
28202 }
28203
28204
28205 /* EXPORT:
28206 Draw a vertical window border to the right of window W if W doesn't
28207 have vertical scroll bars. */
28208
28209 void
28210 x_draw_vertical_border (struct window *w)
28211 {
28212 struct frame *f = XFRAME (WINDOW_FRAME (w));
28213
28214 /* We could do better, if we knew what type of scroll-bar the adjacent
28215 windows (on either side) have... But we don't :-(
28216 However, I think this works ok. ++KFS 2003-04-25 */
28217
28218 /* Redraw borders between horizontally adjacent windows. Don't
28219 do it for frames with vertical scroll bars because either the
28220 right scroll bar of a window, or the left scroll bar of its
28221 neighbor will suffice as a border. */
28222 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28223 return;
28224
28225 if (!WINDOW_RIGHTMOST_P (w)
28226 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28227 {
28228 int x0, x1, y0, y1;
28229
28230 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28231 y1 -= 1;
28232
28233 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28234 x1 -= 1;
28235
28236 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28237 }
28238 else if (!WINDOW_LEFTMOST_P (w)
28239 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28240 {
28241 int x0, x1, y0, y1;
28242
28243 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28244 y1 -= 1;
28245
28246 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28247 x0 -= 1;
28248
28249 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28250 }
28251 }
28252
28253
28254 /* Redraw the part of window W intersection rectangle FR. Pixel
28255 coordinates in FR are frame-relative. Call this function with
28256 input blocked. Value is non-zero if the exposure overwrites
28257 mouse-face. */
28258
28259 static int
28260 expose_window (struct window *w, XRectangle *fr)
28261 {
28262 struct frame *f = XFRAME (w->frame);
28263 XRectangle wr, r;
28264 int mouse_face_overwritten_p = 0;
28265
28266 /* If window is not yet fully initialized, do nothing. This can
28267 happen when toolkit scroll bars are used and a window is split.
28268 Reconfiguring the scroll bar will generate an expose for a newly
28269 created window. */
28270 if (w->current_matrix == NULL)
28271 return 0;
28272
28273 /* When we're currently updating the window, display and current
28274 matrix usually don't agree. Arrange for a thorough display
28275 later. */
28276 if (w == updated_window)
28277 {
28278 SET_FRAME_GARBAGED (f);
28279 return 0;
28280 }
28281
28282 /* Frame-relative pixel rectangle of W. */
28283 wr.x = WINDOW_LEFT_EDGE_X (w);
28284 wr.y = WINDOW_TOP_EDGE_Y (w);
28285 wr.width = WINDOW_TOTAL_WIDTH (w);
28286 wr.height = WINDOW_TOTAL_HEIGHT (w);
28287
28288 if (x_intersect_rectangles (fr, &wr, &r))
28289 {
28290 int yb = window_text_bottom_y (w);
28291 struct glyph_row *row;
28292 int cursor_cleared_p, phys_cursor_on_p;
28293 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28294
28295 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28296 r.x, r.y, r.width, r.height));
28297
28298 /* Convert to window coordinates. */
28299 r.x -= WINDOW_LEFT_EDGE_X (w);
28300 r.y -= WINDOW_TOP_EDGE_Y (w);
28301
28302 /* Turn off the cursor. */
28303 if (!w->pseudo_window_p
28304 && phys_cursor_in_rect_p (w, &r))
28305 {
28306 x_clear_cursor (w);
28307 cursor_cleared_p = 1;
28308 }
28309 else
28310 cursor_cleared_p = 0;
28311
28312 /* If the row containing the cursor extends face to end of line,
28313 then expose_area might overwrite the cursor outside the
28314 rectangle and thus notice_overwritten_cursor might clear
28315 w->phys_cursor_on_p. We remember the original value and
28316 check later if it is changed. */
28317 phys_cursor_on_p = w->phys_cursor_on_p;
28318
28319 /* Update lines intersecting rectangle R. */
28320 first_overlapping_row = last_overlapping_row = NULL;
28321 for (row = w->current_matrix->rows;
28322 row->enabled_p;
28323 ++row)
28324 {
28325 int y0 = row->y;
28326 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28327
28328 if ((y0 >= r.y && y0 < r.y + r.height)
28329 || (y1 > r.y && y1 < r.y + r.height)
28330 || (r.y >= y0 && r.y < y1)
28331 || (r.y + r.height > y0 && r.y + r.height < y1))
28332 {
28333 /* A header line may be overlapping, but there is no need
28334 to fix overlapping areas for them. KFS 2005-02-12 */
28335 if (row->overlapping_p && !row->mode_line_p)
28336 {
28337 if (first_overlapping_row == NULL)
28338 first_overlapping_row = row;
28339 last_overlapping_row = row;
28340 }
28341
28342 row->clip = fr;
28343 if (expose_line (w, row, &r))
28344 mouse_face_overwritten_p = 1;
28345 row->clip = NULL;
28346 }
28347 else if (row->overlapping_p)
28348 {
28349 /* We must redraw a row overlapping the exposed area. */
28350 if (y0 < r.y
28351 ? y0 + row->phys_height > r.y
28352 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28353 {
28354 if (first_overlapping_row == NULL)
28355 first_overlapping_row = row;
28356 last_overlapping_row = row;
28357 }
28358 }
28359
28360 if (y1 >= yb)
28361 break;
28362 }
28363
28364 /* Display the mode line if there is one. */
28365 if (WINDOW_WANTS_MODELINE_P (w)
28366 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28367 row->enabled_p)
28368 && row->y < r.y + r.height)
28369 {
28370 if (expose_line (w, row, &r))
28371 mouse_face_overwritten_p = 1;
28372 }
28373
28374 if (!w->pseudo_window_p)
28375 {
28376 /* Fix the display of overlapping rows. */
28377 if (first_overlapping_row)
28378 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28379 fr);
28380
28381 /* Draw border between windows. */
28382 x_draw_vertical_border (w);
28383
28384 /* Turn the cursor on again. */
28385 if (cursor_cleared_p
28386 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28387 update_window_cursor (w, 1);
28388 }
28389 }
28390
28391 return mouse_face_overwritten_p;
28392 }
28393
28394
28395
28396 /* Redraw (parts) of all windows in the window tree rooted at W that
28397 intersect R. R contains frame pixel coordinates. Value is
28398 non-zero if the exposure overwrites mouse-face. */
28399
28400 static int
28401 expose_window_tree (struct window *w, XRectangle *r)
28402 {
28403 struct frame *f = XFRAME (w->frame);
28404 int mouse_face_overwritten_p = 0;
28405
28406 while (w && !FRAME_GARBAGED_P (f))
28407 {
28408 if (!NILP (w->hchild))
28409 mouse_face_overwritten_p
28410 |= expose_window_tree (XWINDOW (w->hchild), r);
28411 else if (!NILP (w->vchild))
28412 mouse_face_overwritten_p
28413 |= expose_window_tree (XWINDOW (w->vchild), r);
28414 else
28415 mouse_face_overwritten_p |= expose_window (w, r);
28416
28417 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28418 }
28419
28420 return mouse_face_overwritten_p;
28421 }
28422
28423
28424 /* EXPORT:
28425 Redisplay an exposed area of frame F. X and Y are the upper-left
28426 corner of the exposed rectangle. W and H are width and height of
28427 the exposed area. All are pixel values. W or H zero means redraw
28428 the entire frame. */
28429
28430 void
28431 expose_frame (struct frame *f, int x, int y, int w, int h)
28432 {
28433 XRectangle r;
28434 int mouse_face_overwritten_p = 0;
28435
28436 TRACE ((stderr, "expose_frame "));
28437
28438 /* No need to redraw if frame will be redrawn soon. */
28439 if (FRAME_GARBAGED_P (f))
28440 {
28441 TRACE ((stderr, " garbaged\n"));
28442 return;
28443 }
28444
28445 /* If basic faces haven't been realized yet, there is no point in
28446 trying to redraw anything. This can happen when we get an expose
28447 event while Emacs is starting, e.g. by moving another window. */
28448 if (FRAME_FACE_CACHE (f) == NULL
28449 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28450 {
28451 TRACE ((stderr, " no faces\n"));
28452 return;
28453 }
28454
28455 if (w == 0 || h == 0)
28456 {
28457 r.x = r.y = 0;
28458 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28459 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28460 }
28461 else
28462 {
28463 r.x = x;
28464 r.y = y;
28465 r.width = w;
28466 r.height = h;
28467 }
28468
28469 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28470 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28471
28472 if (WINDOWP (f->tool_bar_window))
28473 mouse_face_overwritten_p
28474 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28475
28476 #ifdef HAVE_X_WINDOWS
28477 #ifndef MSDOS
28478 #ifndef USE_X_TOOLKIT
28479 if (WINDOWP (f->menu_bar_window))
28480 mouse_face_overwritten_p
28481 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28482 #endif /* not USE_X_TOOLKIT */
28483 #endif
28484 #endif
28485
28486 /* Some window managers support a focus-follows-mouse style with
28487 delayed raising of frames. Imagine a partially obscured frame,
28488 and moving the mouse into partially obscured mouse-face on that
28489 frame. The visible part of the mouse-face will be highlighted,
28490 then the WM raises the obscured frame. With at least one WM, KDE
28491 2.1, Emacs is not getting any event for the raising of the frame
28492 (even tried with SubstructureRedirectMask), only Expose events.
28493 These expose events will draw text normally, i.e. not
28494 highlighted. Which means we must redo the highlight here.
28495 Subsume it under ``we love X''. --gerd 2001-08-15 */
28496 /* Included in Windows version because Windows most likely does not
28497 do the right thing if any third party tool offers
28498 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28499 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28500 {
28501 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28502 if (f == hlinfo->mouse_face_mouse_frame)
28503 {
28504 int mouse_x = hlinfo->mouse_face_mouse_x;
28505 int mouse_y = hlinfo->mouse_face_mouse_y;
28506 clear_mouse_face (hlinfo);
28507 note_mouse_highlight (f, mouse_x, mouse_y);
28508 }
28509 }
28510 }
28511
28512
28513 /* EXPORT:
28514 Determine the intersection of two rectangles R1 and R2. Return
28515 the intersection in *RESULT. Value is non-zero if RESULT is not
28516 empty. */
28517
28518 int
28519 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28520 {
28521 XRectangle *left, *right;
28522 XRectangle *upper, *lower;
28523 int intersection_p = 0;
28524
28525 /* Rearrange so that R1 is the left-most rectangle. */
28526 if (r1->x < r2->x)
28527 left = r1, right = r2;
28528 else
28529 left = r2, right = r1;
28530
28531 /* X0 of the intersection is right.x0, if this is inside R1,
28532 otherwise there is no intersection. */
28533 if (right->x <= left->x + left->width)
28534 {
28535 result->x = right->x;
28536
28537 /* The right end of the intersection is the minimum of
28538 the right ends of left and right. */
28539 result->width = (min (left->x + left->width, right->x + right->width)
28540 - result->x);
28541
28542 /* Same game for Y. */
28543 if (r1->y < r2->y)
28544 upper = r1, lower = r2;
28545 else
28546 upper = r2, lower = r1;
28547
28548 /* The upper end of the intersection is lower.y0, if this is inside
28549 of upper. Otherwise, there is no intersection. */
28550 if (lower->y <= upper->y + upper->height)
28551 {
28552 result->y = lower->y;
28553
28554 /* The lower end of the intersection is the minimum of the lower
28555 ends of upper and lower. */
28556 result->height = (min (lower->y + lower->height,
28557 upper->y + upper->height)
28558 - result->y);
28559 intersection_p = 1;
28560 }
28561 }
28562
28563 return intersection_p;
28564 }
28565
28566 #endif /* HAVE_WINDOW_SYSTEM */
28567
28568 \f
28569 /***********************************************************************
28570 Initialization
28571 ***********************************************************************/
28572
28573 void
28574 syms_of_xdisp (void)
28575 {
28576 Vwith_echo_area_save_vector = Qnil;
28577 staticpro (&Vwith_echo_area_save_vector);
28578
28579 Vmessage_stack = Qnil;
28580 staticpro (&Vmessage_stack);
28581
28582 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28583 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28584
28585 message_dolog_marker1 = Fmake_marker ();
28586 staticpro (&message_dolog_marker1);
28587 message_dolog_marker2 = Fmake_marker ();
28588 staticpro (&message_dolog_marker2);
28589 message_dolog_marker3 = Fmake_marker ();
28590 staticpro (&message_dolog_marker3);
28591
28592 #ifdef GLYPH_DEBUG
28593 defsubr (&Sdump_frame_glyph_matrix);
28594 defsubr (&Sdump_glyph_matrix);
28595 defsubr (&Sdump_glyph_row);
28596 defsubr (&Sdump_tool_bar_row);
28597 defsubr (&Strace_redisplay);
28598 defsubr (&Strace_to_stderr);
28599 #endif
28600 #ifdef HAVE_WINDOW_SYSTEM
28601 defsubr (&Stool_bar_lines_needed);
28602 defsubr (&Slookup_image_map);
28603 #endif
28604 defsubr (&Sformat_mode_line);
28605 defsubr (&Sinvisible_p);
28606 defsubr (&Scurrent_bidi_paragraph_direction);
28607
28608 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28609 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28610 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28611 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28612 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28613 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28614 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28615 DEFSYM (Qeval, "eval");
28616 DEFSYM (QCdata, ":data");
28617 DEFSYM (Qdisplay, "display");
28618 DEFSYM (Qspace_width, "space-width");
28619 DEFSYM (Qraise, "raise");
28620 DEFSYM (Qslice, "slice");
28621 DEFSYM (Qspace, "space");
28622 DEFSYM (Qmargin, "margin");
28623 DEFSYM (Qpointer, "pointer");
28624 DEFSYM (Qleft_margin, "left-margin");
28625 DEFSYM (Qright_margin, "right-margin");
28626 DEFSYM (Qcenter, "center");
28627 DEFSYM (Qline_height, "line-height");
28628 DEFSYM (QCalign_to, ":align-to");
28629 DEFSYM (QCrelative_width, ":relative-width");
28630 DEFSYM (QCrelative_height, ":relative-height");
28631 DEFSYM (QCeval, ":eval");
28632 DEFSYM (QCpropertize, ":propertize");
28633 DEFSYM (QCfile, ":file");
28634 DEFSYM (Qfontified, "fontified");
28635 DEFSYM (Qfontification_functions, "fontification-functions");
28636 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28637 DEFSYM (Qescape_glyph, "escape-glyph");
28638 DEFSYM (Qnobreak_space, "nobreak-space");
28639 DEFSYM (Qimage, "image");
28640 DEFSYM (Qtext, "text");
28641 DEFSYM (Qboth, "both");
28642 DEFSYM (Qboth_horiz, "both-horiz");
28643 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28644 DEFSYM (QCmap, ":map");
28645 DEFSYM (QCpointer, ":pointer");
28646 DEFSYM (Qrect, "rect");
28647 DEFSYM (Qcircle, "circle");
28648 DEFSYM (Qpoly, "poly");
28649 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28650 DEFSYM (Qgrow_only, "grow-only");
28651 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28652 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28653 DEFSYM (Qposition, "position");
28654 DEFSYM (Qbuffer_position, "buffer-position");
28655 DEFSYM (Qobject, "object");
28656 DEFSYM (Qbar, "bar");
28657 DEFSYM (Qhbar, "hbar");
28658 DEFSYM (Qbox, "box");
28659 DEFSYM (Qhollow, "hollow");
28660 DEFSYM (Qhand, "hand");
28661 DEFSYM (Qarrow, "arrow");
28662 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28663
28664 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28665 Fcons (intern_c_string ("void-variable"), Qnil)),
28666 Qnil);
28667 staticpro (&list_of_error);
28668
28669 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28670 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28671 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28672 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28673
28674 echo_buffer[0] = echo_buffer[1] = Qnil;
28675 staticpro (&echo_buffer[0]);
28676 staticpro (&echo_buffer[1]);
28677
28678 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28679 staticpro (&echo_area_buffer[0]);
28680 staticpro (&echo_area_buffer[1]);
28681
28682 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28683 staticpro (&Vmessages_buffer_name);
28684
28685 mode_line_proptrans_alist = Qnil;
28686 staticpro (&mode_line_proptrans_alist);
28687 mode_line_string_list = Qnil;
28688 staticpro (&mode_line_string_list);
28689 mode_line_string_face = Qnil;
28690 staticpro (&mode_line_string_face);
28691 mode_line_string_face_prop = Qnil;
28692 staticpro (&mode_line_string_face_prop);
28693 Vmode_line_unwind_vector = Qnil;
28694 staticpro (&Vmode_line_unwind_vector);
28695
28696 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28697
28698 help_echo_string = Qnil;
28699 staticpro (&help_echo_string);
28700 help_echo_object = Qnil;
28701 staticpro (&help_echo_object);
28702 help_echo_window = Qnil;
28703 staticpro (&help_echo_window);
28704 previous_help_echo_string = Qnil;
28705 staticpro (&previous_help_echo_string);
28706 help_echo_pos = -1;
28707
28708 DEFSYM (Qright_to_left, "right-to-left");
28709 DEFSYM (Qleft_to_right, "left-to-right");
28710
28711 #ifdef HAVE_WINDOW_SYSTEM
28712 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28713 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28714 For example, if a block cursor is over a tab, it will be drawn as
28715 wide as that tab on the display. */);
28716 x_stretch_cursor_p = 0;
28717 #endif
28718
28719 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28720 doc: /* Non-nil means highlight trailing whitespace.
28721 The face used for trailing whitespace is `trailing-whitespace'. */);
28722 Vshow_trailing_whitespace = Qnil;
28723
28724 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28725 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28726 If the value is t, Emacs highlights non-ASCII chars which have the
28727 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28728 or `escape-glyph' face respectively.
28729
28730 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28731 U+2011 (non-breaking hyphen) are affected.
28732
28733 Any other non-nil value means to display these characters as a escape
28734 glyph followed by an ordinary space or hyphen.
28735
28736 A value of nil means no special handling of these characters. */);
28737 Vnobreak_char_display = Qt;
28738
28739 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28740 doc: /* The pointer shape to show in void text areas.
28741 A value of nil means to show the text pointer. Other options are `arrow',
28742 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28743 Vvoid_text_area_pointer = Qarrow;
28744
28745 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28746 doc: /* Non-nil means don't actually do any redisplay.
28747 This is used for internal purposes. */);
28748 Vinhibit_redisplay = Qnil;
28749
28750 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28751 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28752 Vglobal_mode_string = Qnil;
28753
28754 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28755 doc: /* Marker for where to display an arrow on top of the buffer text.
28756 This must be the beginning of a line in order to work.
28757 See also `overlay-arrow-string'. */);
28758 Voverlay_arrow_position = Qnil;
28759
28760 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28761 doc: /* String to display as an arrow in non-window frames.
28762 See also `overlay-arrow-position'. */);
28763 Voverlay_arrow_string = build_pure_c_string ("=>");
28764
28765 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28766 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28767 The symbols on this list are examined during redisplay to determine
28768 where to display overlay arrows. */);
28769 Voverlay_arrow_variable_list
28770 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28771
28772 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28773 doc: /* The number of lines to try scrolling a window by when point moves out.
28774 If that fails to bring point back on frame, point is centered instead.
28775 If this is zero, point is always centered after it moves off frame.
28776 If you want scrolling to always be a line at a time, you should set
28777 `scroll-conservatively' to a large value rather than set this to 1. */);
28778
28779 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28780 doc: /* Scroll up to this many lines, to bring point back on screen.
28781 If point moves off-screen, redisplay will scroll by up to
28782 `scroll-conservatively' lines in order to bring point just barely
28783 onto the screen again. If that cannot be done, then redisplay
28784 recenters point as usual.
28785
28786 If the value is greater than 100, redisplay will never recenter point,
28787 but will always scroll just enough text to bring point into view, even
28788 if you move far away.
28789
28790 A value of zero means always recenter point if it moves off screen. */);
28791 scroll_conservatively = 0;
28792
28793 DEFVAR_INT ("scroll-margin", scroll_margin,
28794 doc: /* Number of lines of margin at the top and bottom of a window.
28795 Recenter the window whenever point gets within this many lines
28796 of the top or bottom of the window. */);
28797 scroll_margin = 0;
28798
28799 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28800 doc: /* Pixels per inch value for non-window system displays.
28801 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28802 Vdisplay_pixels_per_inch = make_float (72.0);
28803
28804 #ifdef GLYPH_DEBUG
28805 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28806 #endif
28807
28808 DEFVAR_LISP ("truncate-partial-width-windows",
28809 Vtruncate_partial_width_windows,
28810 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28811 For an integer value, truncate lines in each window narrower than the
28812 full frame width, provided the window width is less than that integer;
28813 otherwise, respect the value of `truncate-lines'.
28814
28815 For any other non-nil value, truncate lines in all windows that do
28816 not span the full frame width.
28817
28818 A value of nil means to respect the value of `truncate-lines'.
28819
28820 If `word-wrap' is enabled, you might want to reduce this. */);
28821 Vtruncate_partial_width_windows = make_number (50);
28822
28823 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28824 doc: /* Maximum buffer size for which line number should be displayed.
28825 If the buffer is bigger than this, the line number does not appear
28826 in the mode line. A value of nil means no limit. */);
28827 Vline_number_display_limit = Qnil;
28828
28829 DEFVAR_INT ("line-number-display-limit-width",
28830 line_number_display_limit_width,
28831 doc: /* Maximum line width (in characters) for line number display.
28832 If the average length of the lines near point is bigger than this, then the
28833 line number may be omitted from the mode line. */);
28834 line_number_display_limit_width = 200;
28835
28836 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28837 doc: /* Non-nil means highlight region even in nonselected windows. */);
28838 highlight_nonselected_windows = 0;
28839
28840 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28841 doc: /* Non-nil if more than one frame is visible on this display.
28842 Minibuffer-only frames don't count, but iconified frames do.
28843 This variable is not guaranteed to be accurate except while processing
28844 `frame-title-format' and `icon-title-format'. */);
28845
28846 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28847 doc: /* Template for displaying the title bar of visible frames.
28848 \(Assuming the window manager supports this feature.)
28849
28850 This variable has the same structure as `mode-line-format', except that
28851 the %c and %l constructs are ignored. It is used only on frames for
28852 which no explicit name has been set \(see `modify-frame-parameters'). */);
28853
28854 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28855 doc: /* Template for displaying the title bar of an iconified frame.
28856 \(Assuming the window manager supports this feature.)
28857 This variable has the same structure as `mode-line-format' (which see),
28858 and is used only on frames for which no explicit name has been set
28859 \(see `modify-frame-parameters'). */);
28860 Vicon_title_format
28861 = Vframe_title_format
28862 = listn (CONSTYPE_PURE, 3,
28863 intern_c_string ("multiple-frames"),
28864 build_pure_c_string ("%b"),
28865 listn (CONSTYPE_PURE, 4,
28866 empty_unibyte_string,
28867 intern_c_string ("invocation-name"),
28868 build_pure_c_string ("@"),
28869 intern_c_string ("system-name")));
28870
28871 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28872 doc: /* Maximum number of lines to keep in the message log buffer.
28873 If nil, disable message logging. If t, log messages but don't truncate
28874 the buffer when it becomes large. */);
28875 Vmessage_log_max = make_number (1000);
28876
28877 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28878 doc: /* Functions called before redisplay, if window sizes have changed.
28879 The value should be a list of functions that take one argument.
28880 Just before redisplay, for each frame, if any of its windows have changed
28881 size since the last redisplay, or have been split or deleted,
28882 all the functions in the list are called, with the frame as argument. */);
28883 Vwindow_size_change_functions = Qnil;
28884
28885 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28886 doc: /* List of functions to call before redisplaying a window with scrolling.
28887 Each function is called with two arguments, the window and its new
28888 display-start position. Note that these functions are also called by
28889 `set-window-buffer'. Also note that the value of `window-end' is not
28890 valid when these functions are called.
28891
28892 Warning: Do not use this feature to alter the way the window
28893 is scrolled. It is not designed for that, and such use probably won't
28894 work. */);
28895 Vwindow_scroll_functions = Qnil;
28896
28897 DEFVAR_LISP ("window-text-change-functions",
28898 Vwindow_text_change_functions,
28899 doc: /* Functions to call in redisplay when text in the window might change. */);
28900 Vwindow_text_change_functions = Qnil;
28901
28902 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28903 doc: /* Functions called when redisplay of a window reaches the end trigger.
28904 Each function is called with two arguments, the window and the end trigger value.
28905 See `set-window-redisplay-end-trigger'. */);
28906 Vredisplay_end_trigger_functions = Qnil;
28907
28908 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28909 doc: /* Non-nil means autoselect window with mouse pointer.
28910 If nil, do not autoselect windows.
28911 A positive number means delay autoselection by that many seconds: a
28912 window is autoselected only after the mouse has remained in that
28913 window for the duration of the delay.
28914 A negative number has a similar effect, but causes windows to be
28915 autoselected only after the mouse has stopped moving. \(Because of
28916 the way Emacs compares mouse events, you will occasionally wait twice
28917 that time before the window gets selected.\)
28918 Any other value means to autoselect window instantaneously when the
28919 mouse pointer enters it.
28920
28921 Autoselection selects the minibuffer only if it is active, and never
28922 unselects the minibuffer if it is active.
28923
28924 When customizing this variable make sure that the actual value of
28925 `focus-follows-mouse' matches the behavior of your window manager. */);
28926 Vmouse_autoselect_window = Qnil;
28927
28928 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28929 doc: /* Non-nil means automatically resize tool-bars.
28930 This dynamically changes the tool-bar's height to the minimum height
28931 that is needed to make all tool-bar items visible.
28932 If value is `grow-only', the tool-bar's height is only increased
28933 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28934 Vauto_resize_tool_bars = Qt;
28935
28936 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28937 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28938 auto_raise_tool_bar_buttons_p = 1;
28939
28940 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28941 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28942 make_cursor_line_fully_visible_p = 1;
28943
28944 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28945 doc: /* Border below tool-bar in pixels.
28946 If an integer, use it as the height of the border.
28947 If it is one of `internal-border-width' or `border-width', use the
28948 value of the corresponding frame parameter.
28949 Otherwise, no border is added below the tool-bar. */);
28950 Vtool_bar_border = Qinternal_border_width;
28951
28952 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28953 doc: /* Margin around tool-bar buttons in pixels.
28954 If an integer, use that for both horizontal and vertical margins.
28955 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28956 HORZ specifying the horizontal margin, and VERT specifying the
28957 vertical margin. */);
28958 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28959
28960 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28961 doc: /* Relief thickness of tool-bar buttons. */);
28962 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28963
28964 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28965 doc: /* Tool bar style to use.
28966 It can be one of
28967 image - show images only
28968 text - show text only
28969 both - show both, text below image
28970 both-horiz - show text to the right of the image
28971 text-image-horiz - show text to the left of the image
28972 any other - use system default or image if no system default.
28973
28974 This variable only affects the GTK+ toolkit version of Emacs. */);
28975 Vtool_bar_style = Qnil;
28976
28977 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28978 doc: /* Maximum number of characters a label can have to be shown.
28979 The tool bar style must also show labels for this to have any effect, see
28980 `tool-bar-style'. */);
28981 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28982
28983 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28984 doc: /* List of functions to call to fontify regions of text.
28985 Each function is called with one argument POS. Functions must
28986 fontify a region starting at POS in the current buffer, and give
28987 fontified regions the property `fontified'. */);
28988 Vfontification_functions = Qnil;
28989 Fmake_variable_buffer_local (Qfontification_functions);
28990
28991 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28992 unibyte_display_via_language_environment,
28993 doc: /* Non-nil means display unibyte text according to language environment.
28994 Specifically, this means that raw bytes in the range 160-255 decimal
28995 are displayed by converting them to the equivalent multibyte characters
28996 according to the current language environment. As a result, they are
28997 displayed according to the current fontset.
28998
28999 Note that this variable affects only how these bytes are displayed,
29000 but does not change the fact they are interpreted as raw bytes. */);
29001 unibyte_display_via_language_environment = 0;
29002
29003 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29004 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29005 If a float, it specifies a fraction of the mini-window frame's height.
29006 If an integer, it specifies a number of lines. */);
29007 Vmax_mini_window_height = make_float (0.25);
29008
29009 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29010 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29011 A value of nil means don't automatically resize mini-windows.
29012 A value of t means resize them to fit the text displayed in them.
29013 A value of `grow-only', the default, means let mini-windows grow only;
29014 they return to their normal size when the minibuffer is closed, or the
29015 echo area becomes empty. */);
29016 Vresize_mini_windows = Qgrow_only;
29017
29018 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29019 doc: /* Alist specifying how to blink the cursor off.
29020 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29021 `cursor-type' frame-parameter or variable equals ON-STATE,
29022 comparing using `equal', Emacs uses OFF-STATE to specify
29023 how to blink it off. ON-STATE and OFF-STATE are values for
29024 the `cursor-type' frame parameter.
29025
29026 If a frame's ON-STATE has no entry in this list,
29027 the frame's other specifications determine how to blink the cursor off. */);
29028 Vblink_cursor_alist = Qnil;
29029
29030 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29031 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29032 If non-nil, windows are automatically scrolled horizontally to make
29033 point visible. */);
29034 automatic_hscrolling_p = 1;
29035 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29036
29037 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29038 doc: /* How many columns away from the window edge point is allowed to get
29039 before automatic hscrolling will horizontally scroll the window. */);
29040 hscroll_margin = 5;
29041
29042 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29043 doc: /* How many columns to scroll the window when point gets too close to the edge.
29044 When point is less than `hscroll-margin' columns from the window
29045 edge, automatic hscrolling will scroll the window by the amount of columns
29046 determined by this variable. If its value is a positive integer, scroll that
29047 many columns. If it's a positive floating-point number, it specifies the
29048 fraction of the window's width to scroll. If it's nil or zero, point will be
29049 centered horizontally after the scroll. Any other value, including negative
29050 numbers, are treated as if the value were zero.
29051
29052 Automatic hscrolling always moves point outside the scroll margin, so if
29053 point was more than scroll step columns inside the margin, the window will
29054 scroll more than the value given by the scroll step.
29055
29056 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29057 and `scroll-right' overrides this variable's effect. */);
29058 Vhscroll_step = make_number (0);
29059
29060 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29061 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29062 Bind this around calls to `message' to let it take effect. */);
29063 message_truncate_lines = 0;
29064
29065 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29066 doc: /* Normal hook run to update the menu bar definitions.
29067 Redisplay runs this hook before it redisplays the menu bar.
29068 This is used to update submenus such as Buffers,
29069 whose contents depend on various data. */);
29070 Vmenu_bar_update_hook = Qnil;
29071
29072 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29073 doc: /* Frame for which we are updating a menu.
29074 The enable predicate for a menu binding should check this variable. */);
29075 Vmenu_updating_frame = Qnil;
29076
29077 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29078 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29079 inhibit_menubar_update = 0;
29080
29081 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29082 doc: /* Prefix prepended to all continuation lines at display time.
29083 The value may be a string, an image, or a stretch-glyph; it is
29084 interpreted in the same way as the value of a `display' text property.
29085
29086 This variable is overridden by any `wrap-prefix' text or overlay
29087 property.
29088
29089 To add a prefix to non-continuation lines, use `line-prefix'. */);
29090 Vwrap_prefix = Qnil;
29091 DEFSYM (Qwrap_prefix, "wrap-prefix");
29092 Fmake_variable_buffer_local (Qwrap_prefix);
29093
29094 DEFVAR_LISP ("line-prefix", Vline_prefix,
29095 doc: /* Prefix prepended to all non-continuation lines at display time.
29096 The value may be a string, an image, or a stretch-glyph; it is
29097 interpreted in the same way as the value of a `display' text property.
29098
29099 This variable is overridden by any `line-prefix' text or overlay
29100 property.
29101
29102 To add a prefix to continuation lines, use `wrap-prefix'. */);
29103 Vline_prefix = Qnil;
29104 DEFSYM (Qline_prefix, "line-prefix");
29105 Fmake_variable_buffer_local (Qline_prefix);
29106
29107 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29108 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29109 inhibit_eval_during_redisplay = 0;
29110
29111 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29112 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29113 inhibit_free_realized_faces = 0;
29114
29115 #ifdef GLYPH_DEBUG
29116 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29117 doc: /* Inhibit try_window_id display optimization. */);
29118 inhibit_try_window_id = 0;
29119
29120 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29121 doc: /* Inhibit try_window_reusing display optimization. */);
29122 inhibit_try_window_reusing = 0;
29123
29124 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29125 doc: /* Inhibit try_cursor_movement display optimization. */);
29126 inhibit_try_cursor_movement = 0;
29127 #endif /* GLYPH_DEBUG */
29128
29129 DEFVAR_INT ("overline-margin", overline_margin,
29130 doc: /* Space between overline and text, in pixels.
29131 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29132 margin to the character height. */);
29133 overline_margin = 2;
29134
29135 DEFVAR_INT ("underline-minimum-offset",
29136 underline_minimum_offset,
29137 doc: /* Minimum distance between baseline and underline.
29138 This can improve legibility of underlined text at small font sizes,
29139 particularly when using variable `x-use-underline-position-properties'
29140 with fonts that specify an UNDERLINE_POSITION relatively close to the
29141 baseline. The default value is 1. */);
29142 underline_minimum_offset = 1;
29143
29144 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29145 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29146 This feature only works when on a window system that can change
29147 cursor shapes. */);
29148 display_hourglass_p = 1;
29149
29150 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29151 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29152 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29153
29154 hourglass_atimer = NULL;
29155 hourglass_shown_p = 0;
29156
29157 DEFSYM (Qglyphless_char, "glyphless-char");
29158 DEFSYM (Qhex_code, "hex-code");
29159 DEFSYM (Qempty_box, "empty-box");
29160 DEFSYM (Qthin_space, "thin-space");
29161 DEFSYM (Qzero_width, "zero-width");
29162
29163 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29164 /* Intern this now in case it isn't already done.
29165 Setting this variable twice is harmless.
29166 But don't staticpro it here--that is done in alloc.c. */
29167 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29168 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29169
29170 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29171 doc: /* Char-table defining glyphless characters.
29172 Each element, if non-nil, should be one of the following:
29173 an ASCII acronym string: display this string in a box
29174 `hex-code': display the hexadecimal code of a character in a box
29175 `empty-box': display as an empty box
29176 `thin-space': display as 1-pixel width space
29177 `zero-width': don't display
29178 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29179 display method for graphical terminals and text terminals respectively.
29180 GRAPHICAL and TEXT should each have one of the values listed above.
29181
29182 The char-table has one extra slot to control the display of a character for
29183 which no font is found. This slot only takes effect on graphical terminals.
29184 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29185 `thin-space'. The default is `empty-box'. */);
29186 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29187 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29188 Qempty_box);
29189
29190 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29191 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29192 Vdebug_on_message = Qnil;
29193 }
29194
29195
29196 /* Initialize this module when Emacs starts. */
29197
29198 void
29199 init_xdisp (void)
29200 {
29201 current_header_line_height = current_mode_line_height = -1;
29202
29203 CHARPOS (this_line_start_pos) = 0;
29204
29205 if (!noninteractive)
29206 {
29207 struct window *m = XWINDOW (minibuf_window);
29208 Lisp_Object frame = m->frame;
29209 struct frame *f = XFRAME (frame);
29210 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29211 struct window *r = XWINDOW (root);
29212 int i;
29213
29214 echo_area_window = minibuf_window;
29215
29216 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29217 wset_total_lines
29218 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29219 wset_total_cols (r, make_number (FRAME_COLS (f)));
29220 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29221 wset_total_lines (m, make_number (1));
29222 wset_total_cols (m, make_number (FRAME_COLS (f)));
29223
29224 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29225 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29226 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29227
29228 /* The default ellipsis glyphs `...'. */
29229 for (i = 0; i < 3; ++i)
29230 default_invis_vector[i] = make_number ('.');
29231 }
29232
29233 {
29234 /* Allocate the buffer for frame titles.
29235 Also used for `format-mode-line'. */
29236 int size = 100;
29237 mode_line_noprop_buf = xmalloc (size);
29238 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29239 mode_line_noprop_ptr = mode_line_noprop_buf;
29240 mode_line_target = MODE_LINE_DISPLAY;
29241 }
29242
29243 help_echo_showing_p = 0;
29244 }
29245
29246 /* Platform-independent portion of hourglass implementation. */
29247
29248 /* Cancel a currently active hourglass timer, and start a new one. */
29249 void
29250 start_hourglass (void)
29251 {
29252 #if defined (HAVE_WINDOW_SYSTEM)
29253 EMACS_TIME delay;
29254
29255 cancel_hourglass ();
29256
29257 if (INTEGERP (Vhourglass_delay)
29258 && XINT (Vhourglass_delay) > 0)
29259 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29260 TYPE_MAXIMUM (time_t)),
29261 0);
29262 else if (FLOATP (Vhourglass_delay)
29263 && XFLOAT_DATA (Vhourglass_delay) > 0)
29264 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29265 else
29266 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29267
29268 #ifdef HAVE_NTGUI
29269 {
29270 extern void w32_note_current_window (void);
29271 w32_note_current_window ();
29272 }
29273 #endif /* HAVE_NTGUI */
29274
29275 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29276 show_hourglass, NULL);
29277 #endif
29278 }
29279
29280
29281 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29282 shown. */
29283 void
29284 cancel_hourglass (void)
29285 {
29286 #if defined (HAVE_WINDOW_SYSTEM)
29287 if (hourglass_atimer)
29288 {
29289 cancel_atimer (hourglass_atimer);
29290 hourglass_atimer = NULL;
29291 }
29292
29293 if (hourglass_shown_p)
29294 hide_hourglass ();
29295 #endif
29296 }