]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs-24; up to 2012-04-22T13:58:00Z!cyd@gnu.org
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #if GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754
755 /* If a string, XTread_socket generates an event to display that string.
756 (The display is done in read_char.) */
757
758 Lisp_Object help_echo_string;
759 Lisp_Object help_echo_window;
760 Lisp_Object help_echo_object;
761 EMACS_INT help_echo_pos;
762
763 /* Temporary variable for XTread_socket. */
764
765 Lisp_Object previous_help_echo_string;
766
767 /* Platform-independent portion of hourglass implementation. */
768
769 /* Non-zero means an hourglass cursor is currently shown. */
770 int hourglass_shown_p;
771
772 /* If non-null, an asynchronous timer that, when it expires, displays
773 an hourglass cursor on all frames. */
774 struct atimer *hourglass_atimer;
775
776 /* Name of the face used to display glyphless characters. */
777 Lisp_Object Qglyphless_char;
778
779 /* Symbol for the purpose of Vglyphless_char_display. */
780 static Lisp_Object Qglyphless_char_display;
781
782 /* Method symbols for Vglyphless_char_display. */
783 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
784
785 /* Default pixel width of `thin-space' display method. */
786 #define THIN_SPACE_WIDTH 1
787
788 /* Default number of seconds to wait before displaying an hourglass
789 cursor. */
790 #define DEFAULT_HOURGLASS_DELAY 1
791
792 \f
793 /* Function prototypes. */
794
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, int);
797 static void mark_window_display_accurate_1 (struct window *, int);
798 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
799 static int display_prop_string_p (Lisp_Object, Lisp_Object);
800 static int cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, int);
802 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
803
804 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
805
806 static void handle_line_prefix (struct it *);
807
808 static void pint2str (char *, int, EMACS_INT);
809 static void pint2hrstr (char *, int, EMACS_INT);
810 static struct text_pos run_window_scroll_functions (Lisp_Object,
811 struct text_pos);
812 static void reconsider_clip_changes (struct window *, struct buffer *);
813 static int text_outside_line_unchanged_p (struct window *,
814 EMACS_INT, EMACS_INT);
815 static void store_mode_line_noprop_char (char);
816 static int store_mode_line_noprop (const char *, int, int);
817 static void handle_stop (struct it *);
818 static void handle_stop_backwards (struct it *, EMACS_INT);
819 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
820 static void ensure_echo_area_buffers (void);
821 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
822 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
823 static int with_echo_area_buffer (struct window *, int,
824 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
825 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
826 static void clear_garbaged_frames (void);
827 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
828 static void pop_message (void);
829 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
830 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
831 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
832 static int display_echo_area (struct window *);
833 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
834 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
835 static Lisp_Object unwind_redisplay (Lisp_Object);
836 static int string_char_and_length (const unsigned char *, int *);
837 static struct text_pos display_prop_end (struct it *, Lisp_Object,
838 struct text_pos);
839 static int compute_window_start_on_continuation_line (struct window *);
840 static Lisp_Object safe_eval_handler (Lisp_Object);
841 static void insert_left_trunc_glyphs (struct it *);
842 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
843 Lisp_Object);
844 static void extend_face_to_end_of_line (struct it *);
845 static int append_space_for_newline (struct it *, int);
846 static int cursor_row_fully_visible_p (struct window *, int, int);
847 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
848 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
849 static int trailing_whitespace_p (EMACS_INT);
850 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
851 static void push_it (struct it *, struct text_pos *);
852 static void iterate_out_of_display_property (struct it *);
853 static void pop_it (struct it *);
854 static void sync_frame_with_window_matrix_rows (struct window *);
855 static void select_frame_for_redisplay (Lisp_Object);
856 static void redisplay_internal (void);
857 static int echo_area_display (int);
858 static void redisplay_windows (Lisp_Object);
859 static void redisplay_window (Lisp_Object, int);
860 static Lisp_Object redisplay_window_error (Lisp_Object);
861 static Lisp_Object redisplay_window_0 (Lisp_Object);
862 static Lisp_Object redisplay_window_1 (Lisp_Object);
863 static int set_cursor_from_row (struct window *, struct glyph_row *,
864 struct glyph_matrix *, EMACS_INT, EMACS_INT,
865 int, int);
866 static int update_menu_bar (struct frame *, int, int);
867 static int try_window_reusing_current_matrix (struct window *);
868 static int try_window_id (struct window *);
869 static int display_line (struct it *);
870 static int display_mode_lines (struct window *);
871 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
872 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
873 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
874 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
875 static void display_menu_bar (struct window *);
876 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
877 EMACS_INT *);
878 static int display_string (const char *, Lisp_Object, Lisp_Object,
879 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
880 static void compute_line_metrics (struct it *);
881 static void run_redisplay_end_trigger_hook (struct it *);
882 static int get_overlay_strings (struct it *, EMACS_INT);
883 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
884 static void next_overlay_string (struct it *);
885 static void reseat (struct it *, struct text_pos, int);
886 static void reseat_1 (struct it *, struct text_pos, int);
887 static void back_to_previous_visible_line_start (struct it *);
888 void reseat_at_previous_visible_line_start (struct it *);
889 static void reseat_at_next_visible_line_start (struct it *, int);
890 static int next_element_from_ellipsis (struct it *);
891 static int next_element_from_display_vector (struct it *);
892 static int next_element_from_string (struct it *);
893 static int next_element_from_c_string (struct it *);
894 static int next_element_from_buffer (struct it *);
895 static int next_element_from_composition (struct it *);
896 static int next_element_from_image (struct it *);
897 static int next_element_from_stretch (struct it *);
898 static void load_overlay_strings (struct it *, EMACS_INT);
899 static int init_from_display_pos (struct it *, struct window *,
900 struct display_pos *);
901 static void reseat_to_string (struct it *, const char *,
902 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
903 static int get_next_display_element (struct it *);
904 static enum move_it_result
905 move_it_in_display_line_to (struct it *, EMACS_INT, int,
906 enum move_operation_enum);
907 void move_it_vertically_backward (struct it *, int);
908 static void init_to_row_start (struct it *, struct window *,
909 struct glyph_row *);
910 static int init_to_row_end (struct it *, struct window *,
911 struct glyph_row *);
912 static void back_to_previous_line_start (struct it *);
913 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
914 static struct text_pos string_pos_nchars_ahead (struct text_pos,
915 Lisp_Object, EMACS_INT);
916 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
917 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
918 static EMACS_INT number_of_chars (const char *, int);
919 static void compute_stop_pos (struct it *);
920 static void compute_string_pos (struct text_pos *, struct text_pos,
921 Lisp_Object);
922 static int face_before_or_after_it_pos (struct it *, int);
923 static EMACS_INT next_overlay_change (EMACS_INT);
924 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
925 Lisp_Object, struct text_pos *, EMACS_INT, int);
926 static int handle_single_display_spec (struct it *, Lisp_Object,
927 Lisp_Object, Lisp_Object,
928 struct text_pos *, EMACS_INT, int, int);
929 static int underlying_face_id (struct it *);
930 static int in_ellipses_for_invisible_text_p (struct display_pos *,
931 struct window *);
932
933 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
934 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
935
936 #ifdef HAVE_WINDOW_SYSTEM
937
938 static void x_consider_frame_title (Lisp_Object);
939 static int tool_bar_lines_needed (struct frame *, int *);
940 static void update_tool_bar (struct frame *, int);
941 static void build_desired_tool_bar_string (struct frame *f);
942 static int redisplay_tool_bar (struct frame *);
943 static void display_tool_bar_line (struct it *, int);
944 static void notice_overwritten_cursor (struct window *,
945 enum glyph_row_area,
946 int, int, int, int);
947 static void append_stretch_glyph (struct it *, Lisp_Object,
948 int, int, int);
949
950
951 #endif /* HAVE_WINDOW_SYSTEM */
952
953 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
954 static int coords_in_mouse_face_p (struct window *, int, int);
955
956
957 \f
958 /***********************************************************************
959 Window display dimensions
960 ***********************************************************************/
961
962 /* Return the bottom boundary y-position for text lines in window W.
963 This is the first y position at which a line cannot start.
964 It is relative to the top of the window.
965
966 This is the height of W minus the height of a mode line, if any. */
967
968 int
969 window_text_bottom_y (struct window *w)
970 {
971 int height = WINDOW_TOTAL_HEIGHT (w);
972
973 if (WINDOW_WANTS_MODELINE_P (w))
974 height -= CURRENT_MODE_LINE_HEIGHT (w);
975 return height;
976 }
977
978 /* Return the pixel width of display area AREA of window W. AREA < 0
979 means return the total width of W, not including fringes to
980 the left and right of the window. */
981
982 int
983 window_box_width (struct window *w, int area)
984 {
985 int cols = XFASTINT (w->total_cols);
986 int pixels = 0;
987
988 if (!w->pseudo_window_p)
989 {
990 cols -= WINDOW_SCROLL_BAR_COLS (w);
991
992 if (area == TEXT_AREA)
993 {
994 if (INTEGERP (w->left_margin_cols))
995 cols -= XFASTINT (w->left_margin_cols);
996 if (INTEGERP (w->right_margin_cols))
997 cols -= XFASTINT (w->right_margin_cols);
998 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
999 }
1000 else if (area == LEFT_MARGIN_AREA)
1001 {
1002 cols = (INTEGERP (w->left_margin_cols)
1003 ? XFASTINT (w->left_margin_cols) : 0);
1004 pixels = 0;
1005 }
1006 else if (area == RIGHT_MARGIN_AREA)
1007 {
1008 cols = (INTEGERP (w->right_margin_cols)
1009 ? XFASTINT (w->right_margin_cols) : 0);
1010 pixels = 0;
1011 }
1012 }
1013
1014 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1015 }
1016
1017
1018 /* Return the pixel height of the display area of window W, not
1019 including mode lines of W, if any. */
1020
1021 int
1022 window_box_height (struct window *w)
1023 {
1024 struct frame *f = XFRAME (w->frame);
1025 int height = WINDOW_TOTAL_HEIGHT (w);
1026
1027 xassert (height >= 0);
1028
1029 /* Note: the code below that determines the mode-line/header-line
1030 height is essentially the same as that contained in the macro
1031 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1032 the appropriate glyph row has its `mode_line_p' flag set,
1033 and if it doesn't, uses estimate_mode_line_height instead. */
1034
1035 if (WINDOW_WANTS_MODELINE_P (w))
1036 {
1037 struct glyph_row *ml_row
1038 = (w->current_matrix && w->current_matrix->rows
1039 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1040 : 0);
1041 if (ml_row && ml_row->mode_line_p)
1042 height -= ml_row->height;
1043 else
1044 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1045 }
1046
1047 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 {
1049 struct glyph_row *hl_row
1050 = (w->current_matrix && w->current_matrix->rows
1051 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1052 : 0);
1053 if (hl_row && hl_row->mode_line_p)
1054 height -= hl_row->height;
1055 else
1056 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1057 }
1058
1059 /* With a very small font and a mode-line that's taller than
1060 default, we might end up with a negative height. */
1061 return max (0, height);
1062 }
1063
1064 /* Return the window-relative coordinate of the left edge of display
1065 area AREA of window W. AREA < 0 means return the left edge of the
1066 whole window, to the right of the left fringe of W. */
1067
1068 int
1069 window_box_left_offset (struct window *w, int area)
1070 {
1071 int x;
1072
1073 if (w->pseudo_window_p)
1074 return 0;
1075
1076 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1077
1078 if (area == TEXT_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA));
1081 else if (area == RIGHT_MARGIN_AREA)
1082 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1083 + window_box_width (w, LEFT_MARGIN_AREA)
1084 + window_box_width (w, TEXT_AREA)
1085 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1086 ? 0
1087 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1088 else if (area == LEFT_MARGIN_AREA
1089 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1090 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1091
1092 return x;
1093 }
1094
1095
1096 /* Return the window-relative coordinate of the right edge of display
1097 area AREA of window W. AREA < 0 means return the right edge of the
1098 whole window, to the left of the right fringe of W. */
1099
1100 int
1101 window_box_right_offset (struct window *w, int area)
1102 {
1103 return window_box_left_offset (w, area) + window_box_width (w, area);
1104 }
1105
1106 /* Return the frame-relative coordinate of the left edge of display
1107 area AREA of window W. AREA < 0 means return the left edge of the
1108 whole window, to the right of the left fringe of W. */
1109
1110 int
1111 window_box_left (struct window *w, int area)
1112 {
1113 struct frame *f = XFRAME (w->frame);
1114 int x;
1115
1116 if (w->pseudo_window_p)
1117 return FRAME_INTERNAL_BORDER_WIDTH (f);
1118
1119 x = (WINDOW_LEFT_EDGE_X (w)
1120 + window_box_left_offset (w, area));
1121
1122 return x;
1123 }
1124
1125
1126 /* Return the frame-relative coordinate of the right edge of display
1127 area AREA of window W. AREA < 0 means return the right edge of the
1128 whole window, to the left of the right fringe of W. */
1129
1130 int
1131 window_box_right (struct window *w, int area)
1132 {
1133 return window_box_left (w, area) + window_box_width (w, area);
1134 }
1135
1136 /* Get the bounding box of the display area AREA of window W, without
1137 mode lines, in frame-relative coordinates. AREA < 0 means the
1138 whole window, not including the left and right fringes of
1139 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1140 coordinates of the upper-left corner of the box. Return in
1141 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1142
1143 void
1144 window_box (struct window *w, int area, int *box_x, int *box_y,
1145 int *box_width, int *box_height)
1146 {
1147 if (box_width)
1148 *box_width = window_box_width (w, area);
1149 if (box_height)
1150 *box_height = window_box_height (w);
1151 if (box_x)
1152 *box_x = window_box_left (w, area);
1153 if (box_y)
1154 {
1155 *box_y = WINDOW_TOP_EDGE_Y (w);
1156 if (WINDOW_WANTS_HEADER_LINE_P (w))
1157 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1158 }
1159 }
1160
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines. AREA < 0 means the whole window, not including the
1164 left and right fringe of the window. Return in *TOP_LEFT_X
1165 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1166 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1167 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1168 box. */
1169
1170 static inline void
1171 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1172 int *bottom_right_x, int *bottom_right_y)
1173 {
1174 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1175 bottom_right_y);
1176 *bottom_right_x += *top_left_x;
1177 *bottom_right_y += *top_left_y;
1178 }
1179
1180
1181 \f
1182 /***********************************************************************
1183 Utilities
1184 ***********************************************************************/
1185
1186 /* Return the bottom y-position of the line the iterator IT is in.
1187 This can modify IT's settings. */
1188
1189 int
1190 line_bottom_y (struct it *it)
1191 {
1192 int line_height = it->max_ascent + it->max_descent;
1193 int line_top_y = it->current_y;
1194
1195 if (line_height == 0)
1196 {
1197 if (last_height)
1198 line_height = last_height;
1199 else if (IT_CHARPOS (*it) < ZV)
1200 {
1201 move_it_by_lines (it, 1);
1202 line_height = (it->max_ascent || it->max_descent
1203 ? it->max_ascent + it->max_descent
1204 : last_height);
1205 }
1206 else
1207 {
1208 struct glyph_row *row = it->glyph_row;
1209
1210 /* Use the default character height. */
1211 it->glyph_row = NULL;
1212 it->what = IT_CHARACTER;
1213 it->c = ' ';
1214 it->len = 1;
1215 PRODUCE_GLYPHS (it);
1216 line_height = it->ascent + it->descent;
1217 it->glyph_row = row;
1218 }
1219 }
1220
1221 return line_top_y + line_height;
1222 }
1223
1224 /* Subroutine of pos_visible_p below. Extracts a display string, if
1225 any, from the display spec given as its argument. */
1226 static Lisp_Object
1227 string_from_display_spec (Lisp_Object spec)
1228 {
1229 if (CONSP (spec))
1230 {
1231 while (CONSP (spec))
1232 {
1233 if (STRINGP (XCAR (spec)))
1234 return XCAR (spec);
1235 spec = XCDR (spec);
1236 }
1237 }
1238 else if (VECTORP (spec))
1239 {
1240 ptrdiff_t i;
1241
1242 for (i = 0; i < ASIZE (spec); i++)
1243 {
1244 if (STRINGP (AREF (spec, i)))
1245 return AREF (spec, i);
1246 }
1247 return Qnil;
1248 }
1249
1250 return spec;
1251 }
1252
1253 /* Return 1 if position CHARPOS is visible in window W.
1254 CHARPOS < 0 means return info about WINDOW_END position.
1255 If visible, set *X and *Y to pixel coordinates of top left corner.
1256 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1257 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1258
1259 int
1260 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1261 int *rtop, int *rbot, int *rowh, int *vpos)
1262 {
1263 struct it it;
1264 void *itdata = bidi_shelve_cache ();
1265 struct text_pos top;
1266 int visible_p = 0;
1267 struct buffer *old_buffer = NULL;
1268
1269 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1270 return visible_p;
1271
1272 if (XBUFFER (w->buffer) != current_buffer)
1273 {
1274 old_buffer = current_buffer;
1275 set_buffer_internal_1 (XBUFFER (w->buffer));
1276 }
1277
1278 SET_TEXT_POS_FROM_MARKER (top, w->start);
1279 /* Scrolling a minibuffer window via scroll bar when the echo area
1280 shows long text sometimes resets the minibuffer contents behind
1281 our backs. */
1282 if (CHARPOS (top) > ZV)
1283 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1284
1285 /* Compute exact mode line heights. */
1286 if (WINDOW_WANTS_MODELINE_P (w))
1287 current_mode_line_height
1288 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1289 BVAR (current_buffer, mode_line_format));
1290
1291 if (WINDOW_WANTS_HEADER_LINE_P (w))
1292 current_header_line_height
1293 = display_mode_line (w, HEADER_LINE_FACE_ID,
1294 BVAR (current_buffer, header_line_format));
1295
1296 start_display (&it, w, top);
1297 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1298 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1299
1300 if (charpos >= 0
1301 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1302 && IT_CHARPOS (it) >= charpos)
1303 /* When scanning backwards under bidi iteration, move_it_to
1304 stops at or _before_ CHARPOS, because it stops at or to
1305 the _right_ of the character at CHARPOS. */
1306 || (it.bidi_p && it.bidi_it.scan_dir == -1
1307 && IT_CHARPOS (it) <= charpos)))
1308 {
1309 /* We have reached CHARPOS, or passed it. How the call to
1310 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1311 or covered by a display property, move_it_to stops at the end
1312 of the invisible text, to the right of CHARPOS. (ii) If
1313 CHARPOS is in a display vector, move_it_to stops on its last
1314 glyph. */
1315 int top_x = it.current_x;
1316 int top_y = it.current_y;
1317 /* Calling line_bottom_y may change it.method, it.position, etc. */
1318 enum it_method it_method = it.method;
1319 int bottom_y = (last_height = 0, line_bottom_y (&it));
1320 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1321
1322 if (top_y < window_top_y)
1323 visible_p = bottom_y > window_top_y;
1324 else if (top_y < it.last_visible_y)
1325 visible_p = 1;
1326 if (bottom_y >= it.last_visible_y
1327 && it.bidi_p && it.bidi_it.scan_dir == -1
1328 && IT_CHARPOS (it) < charpos)
1329 {
1330 /* When the last line of the window is scanned backwards
1331 under bidi iteration, we could be duped into thinking
1332 that we have passed CHARPOS, when in fact move_it_to
1333 simply stopped short of CHARPOS because it reached
1334 last_visible_y. To see if that's what happened, we call
1335 move_it_to again with a slightly larger vertical limit,
1336 and see if it actually moved vertically; if it did, we
1337 didn't really reach CHARPOS, which is beyond window end. */
1338 struct it save_it = it;
1339 /* Why 10? because we don't know how many canonical lines
1340 will the height of the next line(s) be. So we guess. */
1341 int ten_more_lines =
1342 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1343
1344 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1345 MOVE_TO_POS | MOVE_TO_Y);
1346 if (it.current_y > top_y)
1347 visible_p = 0;
1348
1349 it = save_it;
1350 }
1351 if (visible_p)
1352 {
1353 if (it_method == GET_FROM_DISPLAY_VECTOR)
1354 {
1355 /* We stopped on the last glyph of a display vector.
1356 Try and recompute. Hack alert! */
1357 if (charpos < 2 || top.charpos >= charpos)
1358 top_x = it.glyph_row->x;
1359 else
1360 {
1361 struct it it2;
1362 start_display (&it2, w, top);
1363 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1364 get_next_display_element (&it2);
1365 PRODUCE_GLYPHS (&it2);
1366 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1367 || it2.current_x > it2.last_visible_x)
1368 top_x = it.glyph_row->x;
1369 else
1370 {
1371 top_x = it2.current_x;
1372 top_y = it2.current_y;
1373 }
1374 }
1375 }
1376 else if (IT_CHARPOS (it) != charpos)
1377 {
1378 Lisp_Object cpos = make_number (charpos);
1379 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1380 Lisp_Object string = string_from_display_spec (spec);
1381 int newline_in_string = 0;
1382
1383 if (STRINGP (string))
1384 {
1385 const char *s = SSDATA (string);
1386 const char *e = s + SBYTES (string);
1387 while (s < e)
1388 {
1389 if (*s++ == '\n')
1390 {
1391 newline_in_string = 1;
1392 break;
1393 }
1394 }
1395 }
1396 /* The tricky code below is needed because there's a
1397 discrepancy between move_it_to and how we set cursor
1398 when the display line ends in a newline from a
1399 display string. move_it_to will stop _after_ such
1400 display strings, whereas set_cursor_from_row
1401 conspires with cursor_row_p to place the cursor on
1402 the first glyph produced from the display string. */
1403
1404 /* We have overshoot PT because it is covered by a
1405 display property whose value is a string. If the
1406 string includes embedded newlines, we are also in the
1407 wrong display line. Backtrack to the correct line,
1408 where the display string begins. */
1409 if (newline_in_string)
1410 {
1411 Lisp_Object startpos, endpos;
1412 EMACS_INT start, end;
1413 struct it it3;
1414 int it3_moved;
1415
1416 /* Find the first and the last buffer positions
1417 covered by the display string. */
1418 endpos =
1419 Fnext_single_char_property_change (cpos, Qdisplay,
1420 Qnil, Qnil);
1421 startpos =
1422 Fprevious_single_char_property_change (endpos, Qdisplay,
1423 Qnil, Qnil);
1424 start = XFASTINT (startpos);
1425 end = XFASTINT (endpos);
1426 /* Move to the last buffer position before the
1427 display property. */
1428 start_display (&it3, w, top);
1429 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1430 /* Move forward one more line if the position before
1431 the display string is a newline or if it is the
1432 rightmost character on a line that is
1433 continued or word-wrapped. */
1434 if (it3.method == GET_FROM_BUFFER
1435 && it3.c == '\n')
1436 move_it_by_lines (&it3, 1);
1437 else if (move_it_in_display_line_to (&it3, -1,
1438 it3.current_x
1439 + it3.pixel_width,
1440 MOVE_TO_X)
1441 == MOVE_LINE_CONTINUED)
1442 {
1443 move_it_by_lines (&it3, 1);
1444 /* When we are under word-wrap, the #$@%!
1445 move_it_by_lines moves 2 lines, so we need to
1446 fix that up. */
1447 if (it3.line_wrap == WORD_WRAP)
1448 move_it_by_lines (&it3, -1);
1449 }
1450
1451 /* Record the vertical coordinate of the display
1452 line where we wound up. */
1453 top_y = it3.current_y;
1454 if (it3.bidi_p)
1455 {
1456 /* When characters are reordered for display,
1457 the character displayed to the left of the
1458 display string could be _after_ the display
1459 property in the logical order. Use the
1460 smallest vertical position of these two. */
1461 start_display (&it3, w, top);
1462 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1463 if (it3.current_y < top_y)
1464 top_y = it3.current_y;
1465 }
1466 /* Move from the top of the window to the beginning
1467 of the display line where the display string
1468 begins. */
1469 start_display (&it3, w, top);
1470 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1471 /* If it3_moved stays zero after the 'while' loop
1472 below, that means we already were at a newline
1473 before the loop (e.g., the display string begins
1474 with a newline), so we don't need to (and cannot)
1475 inspect the glyphs of it3.glyph_row, because
1476 PRODUCE_GLYPHS will not produce anything for a
1477 newline, and thus it3.glyph_row stays at its
1478 stale content it got at top of the window. */
1479 it3_moved = 0;
1480 /* Finally, advance the iterator until we hit the
1481 first display element whose character position is
1482 CHARPOS, or until the first newline from the
1483 display string, which signals the end of the
1484 display line. */
1485 while (get_next_display_element (&it3))
1486 {
1487 PRODUCE_GLYPHS (&it3);
1488 if (IT_CHARPOS (it3) == charpos
1489 || ITERATOR_AT_END_OF_LINE_P (&it3))
1490 break;
1491 it3_moved = 1;
1492 set_iterator_to_next (&it3, 0);
1493 }
1494 top_x = it3.current_x - it3.pixel_width;
1495 /* Normally, we would exit the above loop because we
1496 found the display element whose character
1497 position is CHARPOS. For the contingency that we
1498 didn't, and stopped at the first newline from the
1499 display string, move back over the glyphs
1500 produced from the string, until we find the
1501 rightmost glyph not from the string. */
1502 if (it3_moved
1503 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1504 {
1505 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1506 + it3.glyph_row->used[TEXT_AREA];
1507
1508 while (EQ ((g - 1)->object, string))
1509 {
1510 --g;
1511 top_x -= g->pixel_width;
1512 }
1513 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1514 + it3.glyph_row->used[TEXT_AREA]);
1515 }
1516 }
1517 }
1518
1519 *x = top_x;
1520 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1521 *rtop = max (0, window_top_y - top_y);
1522 *rbot = max (0, bottom_y - it.last_visible_y);
1523 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1524 - max (top_y, window_top_y)));
1525 *vpos = it.vpos;
1526 }
1527 }
1528 else
1529 {
1530 /* We were asked to provide info about WINDOW_END. */
1531 struct it it2;
1532 void *it2data = NULL;
1533
1534 SAVE_IT (it2, it, it2data);
1535 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1536 move_it_by_lines (&it, 1);
1537 if (charpos < IT_CHARPOS (it)
1538 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1539 {
1540 visible_p = 1;
1541 RESTORE_IT (&it2, &it2, it2data);
1542 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1543 *x = it2.current_x;
1544 *y = it2.current_y + it2.max_ascent - it2.ascent;
1545 *rtop = max (0, -it2.current_y);
1546 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1547 - it.last_visible_y));
1548 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1549 it.last_visible_y)
1550 - max (it2.current_y,
1551 WINDOW_HEADER_LINE_HEIGHT (w))));
1552 *vpos = it2.vpos;
1553 }
1554 else
1555 bidi_unshelve_cache (it2data, 1);
1556 }
1557 bidi_unshelve_cache (itdata, 0);
1558
1559 if (old_buffer)
1560 set_buffer_internal_1 (old_buffer);
1561
1562 current_header_line_height = current_mode_line_height = -1;
1563
1564 if (visible_p && XFASTINT (w->hscroll) > 0)
1565 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1566
1567 #if 0
1568 /* Debugging code. */
1569 if (visible_p)
1570 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1571 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1572 else
1573 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1574 #endif
1575
1576 return visible_p;
1577 }
1578
1579
1580 /* Return the next character from STR. Return in *LEN the length of
1581 the character. This is like STRING_CHAR_AND_LENGTH but never
1582 returns an invalid character. If we find one, we return a `?', but
1583 with the length of the invalid character. */
1584
1585 static inline int
1586 string_char_and_length (const unsigned char *str, int *len)
1587 {
1588 int c;
1589
1590 c = STRING_CHAR_AND_LENGTH (str, *len);
1591 if (!CHAR_VALID_P (c))
1592 /* We may not change the length here because other places in Emacs
1593 don't use this function, i.e. they silently accept invalid
1594 characters. */
1595 c = '?';
1596
1597 return c;
1598 }
1599
1600
1601
1602 /* Given a position POS containing a valid character and byte position
1603 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1604
1605 static struct text_pos
1606 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1607 {
1608 xassert (STRINGP (string) && nchars >= 0);
1609
1610 if (STRING_MULTIBYTE (string))
1611 {
1612 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1613 int len;
1614
1615 while (nchars--)
1616 {
1617 string_char_and_length (p, &len);
1618 p += len;
1619 CHARPOS (pos) += 1;
1620 BYTEPOS (pos) += len;
1621 }
1622 }
1623 else
1624 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1625
1626 return pos;
1627 }
1628
1629
1630 /* Value is the text position, i.e. character and byte position,
1631 for character position CHARPOS in STRING. */
1632
1633 static inline struct text_pos
1634 string_pos (EMACS_INT charpos, Lisp_Object string)
1635 {
1636 struct text_pos pos;
1637 xassert (STRINGP (string));
1638 xassert (charpos >= 0);
1639 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1640 return pos;
1641 }
1642
1643
1644 /* Value is a text position, i.e. character and byte position, for
1645 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1646 means recognize multibyte characters. */
1647
1648 static struct text_pos
1649 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1650 {
1651 struct text_pos pos;
1652
1653 xassert (s != NULL);
1654 xassert (charpos >= 0);
1655
1656 if (multibyte_p)
1657 {
1658 int len;
1659
1660 SET_TEXT_POS (pos, 0, 0);
1661 while (charpos--)
1662 {
1663 string_char_and_length ((const unsigned char *) s, &len);
1664 s += len;
1665 CHARPOS (pos) += 1;
1666 BYTEPOS (pos) += len;
1667 }
1668 }
1669 else
1670 SET_TEXT_POS (pos, charpos, charpos);
1671
1672 return pos;
1673 }
1674
1675
1676 /* Value is the number of characters in C string S. MULTIBYTE_P
1677 non-zero means recognize multibyte characters. */
1678
1679 static EMACS_INT
1680 number_of_chars (const char *s, int multibyte_p)
1681 {
1682 EMACS_INT nchars;
1683
1684 if (multibyte_p)
1685 {
1686 EMACS_INT rest = strlen (s);
1687 int len;
1688 const unsigned char *p = (const unsigned char *) s;
1689
1690 for (nchars = 0; rest > 0; ++nchars)
1691 {
1692 string_char_and_length (p, &len);
1693 rest -= len, p += len;
1694 }
1695 }
1696 else
1697 nchars = strlen (s);
1698
1699 return nchars;
1700 }
1701
1702
1703 /* Compute byte position NEWPOS->bytepos corresponding to
1704 NEWPOS->charpos. POS is a known position in string STRING.
1705 NEWPOS->charpos must be >= POS.charpos. */
1706
1707 static void
1708 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1709 {
1710 xassert (STRINGP (string));
1711 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1712
1713 if (STRING_MULTIBYTE (string))
1714 *newpos = string_pos_nchars_ahead (pos, string,
1715 CHARPOS (*newpos) - CHARPOS (pos));
1716 else
1717 BYTEPOS (*newpos) = CHARPOS (*newpos);
1718 }
1719
1720 /* EXPORT:
1721 Return an estimation of the pixel height of mode or header lines on
1722 frame F. FACE_ID specifies what line's height to estimate. */
1723
1724 int
1725 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1726 {
1727 #ifdef HAVE_WINDOW_SYSTEM
1728 if (FRAME_WINDOW_P (f))
1729 {
1730 int height = FONT_HEIGHT (FRAME_FONT (f));
1731
1732 /* This function is called so early when Emacs starts that the face
1733 cache and mode line face are not yet initialized. */
1734 if (FRAME_FACE_CACHE (f))
1735 {
1736 struct face *face = FACE_FROM_ID (f, face_id);
1737 if (face)
1738 {
1739 if (face->font)
1740 height = FONT_HEIGHT (face->font);
1741 if (face->box_line_width > 0)
1742 height += 2 * face->box_line_width;
1743 }
1744 }
1745
1746 return height;
1747 }
1748 #endif
1749
1750 return 1;
1751 }
1752
1753 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1754 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1755 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1756 not force the value into range. */
1757
1758 void
1759 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1760 int *x, int *y, NativeRectangle *bounds, int noclip)
1761 {
1762
1763 #ifdef HAVE_WINDOW_SYSTEM
1764 if (FRAME_WINDOW_P (f))
1765 {
1766 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1767 even for negative values. */
1768 if (pix_x < 0)
1769 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1770 if (pix_y < 0)
1771 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1772
1773 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1774 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1775
1776 if (bounds)
1777 STORE_NATIVE_RECT (*bounds,
1778 FRAME_COL_TO_PIXEL_X (f, pix_x),
1779 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1780 FRAME_COLUMN_WIDTH (f) - 1,
1781 FRAME_LINE_HEIGHT (f) - 1);
1782
1783 if (!noclip)
1784 {
1785 if (pix_x < 0)
1786 pix_x = 0;
1787 else if (pix_x > FRAME_TOTAL_COLS (f))
1788 pix_x = FRAME_TOTAL_COLS (f);
1789
1790 if (pix_y < 0)
1791 pix_y = 0;
1792 else if (pix_y > FRAME_LINES (f))
1793 pix_y = FRAME_LINES (f);
1794 }
1795 }
1796 #endif
1797
1798 *x = pix_x;
1799 *y = pix_y;
1800 }
1801
1802
1803 /* Find the glyph under window-relative coordinates X/Y in window W.
1804 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1805 strings. Return in *HPOS and *VPOS the row and column number of
1806 the glyph found. Return in *AREA the glyph area containing X.
1807 Value is a pointer to the glyph found or null if X/Y is not on
1808 text, or we can't tell because W's current matrix is not up to
1809 date. */
1810
1811 static
1812 struct glyph *
1813 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1814 int *dx, int *dy, int *area)
1815 {
1816 struct glyph *glyph, *end;
1817 struct glyph_row *row = NULL;
1818 int x0, i;
1819
1820 /* Find row containing Y. Give up if some row is not enabled. */
1821 for (i = 0; i < w->current_matrix->nrows; ++i)
1822 {
1823 row = MATRIX_ROW (w->current_matrix, i);
1824 if (!row->enabled_p)
1825 return NULL;
1826 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1827 break;
1828 }
1829
1830 *vpos = i;
1831 *hpos = 0;
1832
1833 /* Give up if Y is not in the window. */
1834 if (i == w->current_matrix->nrows)
1835 return NULL;
1836
1837 /* Get the glyph area containing X. */
1838 if (w->pseudo_window_p)
1839 {
1840 *area = TEXT_AREA;
1841 x0 = 0;
1842 }
1843 else
1844 {
1845 if (x < window_box_left_offset (w, TEXT_AREA))
1846 {
1847 *area = LEFT_MARGIN_AREA;
1848 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1849 }
1850 else if (x < window_box_right_offset (w, TEXT_AREA))
1851 {
1852 *area = TEXT_AREA;
1853 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1854 }
1855 else
1856 {
1857 *area = RIGHT_MARGIN_AREA;
1858 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1859 }
1860 }
1861
1862 /* Find glyph containing X. */
1863 glyph = row->glyphs[*area];
1864 end = glyph + row->used[*area];
1865 x -= x0;
1866 while (glyph < end && x >= glyph->pixel_width)
1867 {
1868 x -= glyph->pixel_width;
1869 ++glyph;
1870 }
1871
1872 if (glyph == end)
1873 return NULL;
1874
1875 if (dx)
1876 {
1877 *dx = x;
1878 *dy = y - (row->y + row->ascent - glyph->ascent);
1879 }
1880
1881 *hpos = glyph - row->glyphs[*area];
1882 return glyph;
1883 }
1884
1885 /* Convert frame-relative x/y to coordinates relative to window W.
1886 Takes pseudo-windows into account. */
1887
1888 static void
1889 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1890 {
1891 if (w->pseudo_window_p)
1892 {
1893 /* A pseudo-window is always full-width, and starts at the
1894 left edge of the frame, plus a frame border. */
1895 struct frame *f = XFRAME (w->frame);
1896 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1897 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1898 }
1899 else
1900 {
1901 *x -= WINDOW_LEFT_EDGE_X (w);
1902 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1903 }
1904 }
1905
1906 #ifdef HAVE_WINDOW_SYSTEM
1907
1908 /* EXPORT:
1909 Return in RECTS[] at most N clipping rectangles for glyph string S.
1910 Return the number of stored rectangles. */
1911
1912 int
1913 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1914 {
1915 XRectangle r;
1916
1917 if (n <= 0)
1918 return 0;
1919
1920 if (s->row->full_width_p)
1921 {
1922 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1923 r.x = WINDOW_LEFT_EDGE_X (s->w);
1924 r.width = WINDOW_TOTAL_WIDTH (s->w);
1925
1926 /* Unless displaying a mode or menu bar line, which are always
1927 fully visible, clip to the visible part of the row. */
1928 if (s->w->pseudo_window_p)
1929 r.height = s->row->visible_height;
1930 else
1931 r.height = s->height;
1932 }
1933 else
1934 {
1935 /* This is a text line that may be partially visible. */
1936 r.x = window_box_left (s->w, s->area);
1937 r.width = window_box_width (s->w, s->area);
1938 r.height = s->row->visible_height;
1939 }
1940
1941 if (s->clip_head)
1942 if (r.x < s->clip_head->x)
1943 {
1944 if (r.width >= s->clip_head->x - r.x)
1945 r.width -= s->clip_head->x - r.x;
1946 else
1947 r.width = 0;
1948 r.x = s->clip_head->x;
1949 }
1950 if (s->clip_tail)
1951 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1952 {
1953 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1954 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1955 else
1956 r.width = 0;
1957 }
1958
1959 /* If S draws overlapping rows, it's sufficient to use the top and
1960 bottom of the window for clipping because this glyph string
1961 intentionally draws over other lines. */
1962 if (s->for_overlaps)
1963 {
1964 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1965 r.height = window_text_bottom_y (s->w) - r.y;
1966
1967 /* Alas, the above simple strategy does not work for the
1968 environments with anti-aliased text: if the same text is
1969 drawn onto the same place multiple times, it gets thicker.
1970 If the overlap we are processing is for the erased cursor, we
1971 take the intersection with the rectangle of the cursor. */
1972 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1973 {
1974 XRectangle rc, r_save = r;
1975
1976 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1977 rc.y = s->w->phys_cursor.y;
1978 rc.width = s->w->phys_cursor_width;
1979 rc.height = s->w->phys_cursor_height;
1980
1981 x_intersect_rectangles (&r_save, &rc, &r);
1982 }
1983 }
1984 else
1985 {
1986 /* Don't use S->y for clipping because it doesn't take partially
1987 visible lines into account. For example, it can be negative for
1988 partially visible lines at the top of a window. */
1989 if (!s->row->full_width_p
1990 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1991 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1992 else
1993 r.y = max (0, s->row->y);
1994 }
1995
1996 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1997
1998 /* If drawing the cursor, don't let glyph draw outside its
1999 advertised boundaries. Cleartype does this under some circumstances. */
2000 if (s->hl == DRAW_CURSOR)
2001 {
2002 struct glyph *glyph = s->first_glyph;
2003 int height, max_y;
2004
2005 if (s->x > r.x)
2006 {
2007 r.width -= s->x - r.x;
2008 r.x = s->x;
2009 }
2010 r.width = min (r.width, glyph->pixel_width);
2011
2012 /* If r.y is below window bottom, ensure that we still see a cursor. */
2013 height = min (glyph->ascent + glyph->descent,
2014 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2015 max_y = window_text_bottom_y (s->w) - height;
2016 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2017 if (s->ybase - glyph->ascent > max_y)
2018 {
2019 r.y = max_y;
2020 r.height = height;
2021 }
2022 else
2023 {
2024 /* Don't draw cursor glyph taller than our actual glyph. */
2025 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2026 if (height < r.height)
2027 {
2028 max_y = r.y + r.height;
2029 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2030 r.height = min (max_y - r.y, height);
2031 }
2032 }
2033 }
2034
2035 if (s->row->clip)
2036 {
2037 XRectangle r_save = r;
2038
2039 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2040 r.width = 0;
2041 }
2042
2043 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2044 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2045 {
2046 #ifdef CONVERT_FROM_XRECT
2047 CONVERT_FROM_XRECT (r, *rects);
2048 #else
2049 *rects = r;
2050 #endif
2051 return 1;
2052 }
2053 else
2054 {
2055 /* If we are processing overlapping and allowed to return
2056 multiple clipping rectangles, we exclude the row of the glyph
2057 string from the clipping rectangle. This is to avoid drawing
2058 the same text on the environment with anti-aliasing. */
2059 #ifdef CONVERT_FROM_XRECT
2060 XRectangle rs[2];
2061 #else
2062 XRectangle *rs = rects;
2063 #endif
2064 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2065
2066 if (s->for_overlaps & OVERLAPS_PRED)
2067 {
2068 rs[i] = r;
2069 if (r.y + r.height > row_y)
2070 {
2071 if (r.y < row_y)
2072 rs[i].height = row_y - r.y;
2073 else
2074 rs[i].height = 0;
2075 }
2076 i++;
2077 }
2078 if (s->for_overlaps & OVERLAPS_SUCC)
2079 {
2080 rs[i] = r;
2081 if (r.y < row_y + s->row->visible_height)
2082 {
2083 if (r.y + r.height > row_y + s->row->visible_height)
2084 {
2085 rs[i].y = row_y + s->row->visible_height;
2086 rs[i].height = r.y + r.height - rs[i].y;
2087 }
2088 else
2089 rs[i].height = 0;
2090 }
2091 i++;
2092 }
2093
2094 n = i;
2095 #ifdef CONVERT_FROM_XRECT
2096 for (i = 0; i < n; i++)
2097 CONVERT_FROM_XRECT (rs[i], rects[i]);
2098 #endif
2099 return n;
2100 }
2101 }
2102
2103 /* EXPORT:
2104 Return in *NR the clipping rectangle for glyph string S. */
2105
2106 void
2107 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2108 {
2109 get_glyph_string_clip_rects (s, nr, 1);
2110 }
2111
2112
2113 /* EXPORT:
2114 Return the position and height of the phys cursor in window W.
2115 Set w->phys_cursor_width to width of phys cursor.
2116 */
2117
2118 void
2119 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2120 struct glyph *glyph, int *xp, int *yp, int *heightp)
2121 {
2122 struct frame *f = XFRAME (WINDOW_FRAME (w));
2123 int x, y, wd, h, h0, y0;
2124
2125 /* Compute the width of the rectangle to draw. If on a stretch
2126 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2127 rectangle as wide as the glyph, but use a canonical character
2128 width instead. */
2129 wd = glyph->pixel_width - 1;
2130 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2131 wd++; /* Why? */
2132 #endif
2133
2134 x = w->phys_cursor.x;
2135 if (x < 0)
2136 {
2137 wd += x;
2138 x = 0;
2139 }
2140
2141 if (glyph->type == STRETCH_GLYPH
2142 && !x_stretch_cursor_p)
2143 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2144 w->phys_cursor_width = wd;
2145
2146 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2147
2148 /* If y is below window bottom, ensure that we still see a cursor. */
2149 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2150
2151 h = max (h0, glyph->ascent + glyph->descent);
2152 h0 = min (h0, glyph->ascent + glyph->descent);
2153
2154 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2155 if (y < y0)
2156 {
2157 h = max (h - (y0 - y) + 1, h0);
2158 y = y0 - 1;
2159 }
2160 else
2161 {
2162 y0 = window_text_bottom_y (w) - h0;
2163 if (y > y0)
2164 {
2165 h += y - y0;
2166 y = y0;
2167 }
2168 }
2169
2170 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2171 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2172 *heightp = h;
2173 }
2174
2175 /*
2176 * Remember which glyph the mouse is over.
2177 */
2178
2179 void
2180 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2181 {
2182 Lisp_Object window;
2183 struct window *w;
2184 struct glyph_row *r, *gr, *end_row;
2185 enum window_part part;
2186 enum glyph_row_area area;
2187 int x, y, width, height;
2188
2189 /* Try to determine frame pixel position and size of the glyph under
2190 frame pixel coordinates X/Y on frame F. */
2191
2192 if (!f->glyphs_initialized_p
2193 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2194 NILP (window)))
2195 {
2196 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2197 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2198 goto virtual_glyph;
2199 }
2200
2201 w = XWINDOW (window);
2202 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2203 height = WINDOW_FRAME_LINE_HEIGHT (w);
2204
2205 x = window_relative_x_coord (w, part, gx);
2206 y = gy - WINDOW_TOP_EDGE_Y (w);
2207
2208 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2209 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2210
2211 if (w->pseudo_window_p)
2212 {
2213 area = TEXT_AREA;
2214 part = ON_MODE_LINE; /* Don't adjust margin. */
2215 goto text_glyph;
2216 }
2217
2218 switch (part)
2219 {
2220 case ON_LEFT_MARGIN:
2221 area = LEFT_MARGIN_AREA;
2222 goto text_glyph;
2223
2224 case ON_RIGHT_MARGIN:
2225 area = RIGHT_MARGIN_AREA;
2226 goto text_glyph;
2227
2228 case ON_HEADER_LINE:
2229 case ON_MODE_LINE:
2230 gr = (part == ON_HEADER_LINE
2231 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2232 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2233 gy = gr->y;
2234 area = TEXT_AREA;
2235 goto text_glyph_row_found;
2236
2237 case ON_TEXT:
2238 area = TEXT_AREA;
2239
2240 text_glyph:
2241 gr = 0; gy = 0;
2242 for (; r <= end_row && r->enabled_p; ++r)
2243 if (r->y + r->height > y)
2244 {
2245 gr = r; gy = r->y;
2246 break;
2247 }
2248
2249 text_glyph_row_found:
2250 if (gr && gy <= y)
2251 {
2252 struct glyph *g = gr->glyphs[area];
2253 struct glyph *end = g + gr->used[area];
2254
2255 height = gr->height;
2256 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2257 if (gx + g->pixel_width > x)
2258 break;
2259
2260 if (g < end)
2261 {
2262 if (g->type == IMAGE_GLYPH)
2263 {
2264 /* Don't remember when mouse is over image, as
2265 image may have hot-spots. */
2266 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2267 return;
2268 }
2269 width = g->pixel_width;
2270 }
2271 else
2272 {
2273 /* Use nominal char spacing at end of line. */
2274 x -= gx;
2275 gx += (x / width) * width;
2276 }
2277
2278 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2279 gx += window_box_left_offset (w, area);
2280 }
2281 else
2282 {
2283 /* Use nominal line height at end of window. */
2284 gx = (x / width) * width;
2285 y -= gy;
2286 gy += (y / height) * height;
2287 }
2288 break;
2289
2290 case ON_LEFT_FRINGE:
2291 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2293 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2294 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2295 goto row_glyph;
2296
2297 case ON_RIGHT_FRINGE:
2298 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2300 : window_box_right_offset (w, TEXT_AREA));
2301 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2302 goto row_glyph;
2303
2304 case ON_SCROLL_BAR:
2305 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2306 ? 0
2307 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2310 : 0)));
2311 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2312
2313 row_glyph:
2314 gr = 0, gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2317 {
2318 gr = r; gy = r->y;
2319 break;
2320 }
2321
2322 if (gr && gy <= y)
2323 height = gr->height;
2324 else
2325 {
2326 /* Use nominal line height at end of window. */
2327 y -= gy;
2328 gy += (y / height) * height;
2329 }
2330 break;
2331
2332 default:
2333 ;
2334 virtual_glyph:
2335 /* If there is no glyph under the mouse, then we divide the screen
2336 into a grid of the smallest glyph in the frame, and use that
2337 as our "glyph". */
2338
2339 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2340 round down even for negative values. */
2341 if (gx < 0)
2342 gx -= width - 1;
2343 if (gy < 0)
2344 gy -= height - 1;
2345
2346 gx = (gx / width) * width;
2347 gy = (gy / height) * height;
2348
2349 goto store_rect;
2350 }
2351
2352 gx += WINDOW_LEFT_EDGE_X (w);
2353 gy += WINDOW_TOP_EDGE_Y (w);
2354
2355 store_rect:
2356 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2357
2358 /* Visible feedback for debugging. */
2359 #if 0
2360 #if HAVE_X_WINDOWS
2361 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2362 f->output_data.x->normal_gc,
2363 gx, gy, width, height);
2364 #endif
2365 #endif
2366 }
2367
2368
2369 #endif /* HAVE_WINDOW_SYSTEM */
2370
2371 \f
2372 /***********************************************************************
2373 Lisp form evaluation
2374 ***********************************************************************/
2375
2376 /* Error handler for safe_eval and safe_call. */
2377
2378 static Lisp_Object
2379 safe_eval_handler (Lisp_Object arg)
2380 {
2381 add_to_log ("Error during redisplay: %S", arg, Qnil);
2382 return Qnil;
2383 }
2384
2385
2386 /* Evaluate SEXPR and return the result, or nil if something went
2387 wrong. Prevent redisplay during the evaluation. */
2388
2389 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2390 Return the result, or nil if something went wrong. Prevent
2391 redisplay during the evaluation. */
2392
2393 Lisp_Object
2394 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2395 {
2396 Lisp_Object val;
2397
2398 if (inhibit_eval_during_redisplay)
2399 val = Qnil;
2400 else
2401 {
2402 int count = SPECPDL_INDEX ();
2403 struct gcpro gcpro1;
2404
2405 GCPRO1 (args[0]);
2406 gcpro1.nvars = nargs;
2407 specbind (Qinhibit_redisplay, Qt);
2408 /* Use Qt to ensure debugger does not run,
2409 so there is no possibility of wanting to redisplay. */
2410 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2411 safe_eval_handler);
2412 UNGCPRO;
2413 val = unbind_to (count, val);
2414 }
2415
2416 return val;
2417 }
2418
2419
2420 /* Call function FN with one argument ARG.
2421 Return the result, or nil if something went wrong. */
2422
2423 Lisp_Object
2424 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2425 {
2426 Lisp_Object args[2];
2427 args[0] = fn;
2428 args[1] = arg;
2429 return safe_call (2, args);
2430 }
2431
2432 static Lisp_Object Qeval;
2433
2434 Lisp_Object
2435 safe_eval (Lisp_Object sexpr)
2436 {
2437 return safe_call1 (Qeval, sexpr);
2438 }
2439
2440 /* Call function FN with one argument ARG.
2441 Return the result, or nil if something went wrong. */
2442
2443 Lisp_Object
2444 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2445 {
2446 Lisp_Object args[3];
2447 args[0] = fn;
2448 args[1] = arg1;
2449 args[2] = arg2;
2450 return safe_call (3, args);
2451 }
2452
2453
2454 \f
2455 /***********************************************************************
2456 Debugging
2457 ***********************************************************************/
2458
2459 #if 0
2460
2461 /* Define CHECK_IT to perform sanity checks on iterators.
2462 This is for debugging. It is too slow to do unconditionally. */
2463
2464 static void
2465 check_it (struct it *it)
2466 {
2467 if (it->method == GET_FROM_STRING)
2468 {
2469 xassert (STRINGP (it->string));
2470 xassert (IT_STRING_CHARPOS (*it) >= 0);
2471 }
2472 else
2473 {
2474 xassert (IT_STRING_CHARPOS (*it) < 0);
2475 if (it->method == GET_FROM_BUFFER)
2476 {
2477 /* Check that character and byte positions agree. */
2478 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2479 }
2480 }
2481
2482 if (it->dpvec)
2483 xassert (it->current.dpvec_index >= 0);
2484 else
2485 xassert (it->current.dpvec_index < 0);
2486 }
2487
2488 #define CHECK_IT(IT) check_it ((IT))
2489
2490 #else /* not 0 */
2491
2492 #define CHECK_IT(IT) (void) 0
2493
2494 #endif /* not 0 */
2495
2496
2497 #if GLYPH_DEBUG && XASSERTS
2498
2499 /* Check that the window end of window W is what we expect it
2500 to be---the last row in the current matrix displaying text. */
2501
2502 static void
2503 check_window_end (struct window *w)
2504 {
2505 if (!MINI_WINDOW_P (w)
2506 && !NILP (w->window_end_valid))
2507 {
2508 struct glyph_row *row;
2509 xassert ((row = MATRIX_ROW (w->current_matrix,
2510 XFASTINT (w->window_end_vpos)),
2511 !row->enabled_p
2512 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2513 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2514 }
2515 }
2516
2517 #define CHECK_WINDOW_END(W) check_window_end ((W))
2518
2519 #else
2520
2521 #define CHECK_WINDOW_END(W) (void) 0
2522
2523 #endif
2524
2525
2526 \f
2527 /***********************************************************************
2528 Iterator initialization
2529 ***********************************************************************/
2530
2531 /* Initialize IT for displaying current_buffer in window W, starting
2532 at character position CHARPOS. CHARPOS < 0 means that no buffer
2533 position is specified which is useful when the iterator is assigned
2534 a position later. BYTEPOS is the byte position corresponding to
2535 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2536
2537 If ROW is not null, calls to produce_glyphs with IT as parameter
2538 will produce glyphs in that row.
2539
2540 BASE_FACE_ID is the id of a base face to use. It must be one of
2541 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2542 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2543 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2544
2545 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2546 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2547 will be initialized to use the corresponding mode line glyph row of
2548 the desired matrix of W. */
2549
2550 void
2551 init_iterator (struct it *it, struct window *w,
2552 EMACS_INT charpos, EMACS_INT bytepos,
2553 struct glyph_row *row, enum face_id base_face_id)
2554 {
2555 int highlight_region_p;
2556 enum face_id remapped_base_face_id = base_face_id;
2557
2558 /* Some precondition checks. */
2559 xassert (w != NULL && it != NULL);
2560 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2561 && charpos <= ZV));
2562
2563 /* If face attributes have been changed since the last redisplay,
2564 free realized faces now because they depend on face definitions
2565 that might have changed. Don't free faces while there might be
2566 desired matrices pending which reference these faces. */
2567 if (face_change_count && !inhibit_free_realized_faces)
2568 {
2569 face_change_count = 0;
2570 free_all_realized_faces (Qnil);
2571 }
2572
2573 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2574 if (! NILP (Vface_remapping_alist))
2575 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2576
2577 /* Use one of the mode line rows of W's desired matrix if
2578 appropriate. */
2579 if (row == NULL)
2580 {
2581 if (base_face_id == MODE_LINE_FACE_ID
2582 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2583 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2584 else if (base_face_id == HEADER_LINE_FACE_ID)
2585 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2586 }
2587
2588 /* Clear IT. */
2589 memset (it, 0, sizeof *it);
2590 it->current.overlay_string_index = -1;
2591 it->current.dpvec_index = -1;
2592 it->base_face_id = remapped_base_face_id;
2593 it->string = Qnil;
2594 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2595 it->paragraph_embedding = L2R;
2596 it->bidi_it.string.lstring = Qnil;
2597 it->bidi_it.string.s = NULL;
2598 it->bidi_it.string.bufpos = 0;
2599
2600 /* The window in which we iterate over current_buffer: */
2601 XSETWINDOW (it->window, w);
2602 it->w = w;
2603 it->f = XFRAME (w->frame);
2604
2605 it->cmp_it.id = -1;
2606
2607 /* Extra space between lines (on window systems only). */
2608 if (base_face_id == DEFAULT_FACE_ID
2609 && FRAME_WINDOW_P (it->f))
2610 {
2611 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2612 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2613 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2614 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2615 * FRAME_LINE_HEIGHT (it->f));
2616 else if (it->f->extra_line_spacing > 0)
2617 it->extra_line_spacing = it->f->extra_line_spacing;
2618 it->max_extra_line_spacing = 0;
2619 }
2620
2621 /* If realized faces have been removed, e.g. because of face
2622 attribute changes of named faces, recompute them. When running
2623 in batch mode, the face cache of the initial frame is null. If
2624 we happen to get called, make a dummy face cache. */
2625 if (FRAME_FACE_CACHE (it->f) == NULL)
2626 init_frame_faces (it->f);
2627 if (FRAME_FACE_CACHE (it->f)->used == 0)
2628 recompute_basic_faces (it->f);
2629
2630 /* Current value of the `slice', `space-width', and 'height' properties. */
2631 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2632 it->space_width = Qnil;
2633 it->font_height = Qnil;
2634 it->override_ascent = -1;
2635
2636 /* Are control characters displayed as `^C'? */
2637 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2638
2639 /* -1 means everything between a CR and the following line end
2640 is invisible. >0 means lines indented more than this value are
2641 invisible. */
2642 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2643 ? XINT (BVAR (current_buffer, selective_display))
2644 : (!NILP (BVAR (current_buffer, selective_display))
2645 ? -1 : 0));
2646 it->selective_display_ellipsis_p
2647 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2648
2649 /* Display table to use. */
2650 it->dp = window_display_table (w);
2651
2652 /* Are multibyte characters enabled in current_buffer? */
2653 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2654
2655 /* Non-zero if we should highlight the region. */
2656 highlight_region_p
2657 = (!NILP (Vtransient_mark_mode)
2658 && !NILP (BVAR (current_buffer, mark_active))
2659 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2660
2661 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2662 start and end of a visible region in window IT->w. Set both to
2663 -1 to indicate no region. */
2664 if (highlight_region_p
2665 /* Maybe highlight only in selected window. */
2666 && (/* Either show region everywhere. */
2667 highlight_nonselected_windows
2668 /* Or show region in the selected window. */
2669 || w == XWINDOW (selected_window)
2670 /* Or show the region if we are in the mini-buffer and W is
2671 the window the mini-buffer refers to. */
2672 || (MINI_WINDOW_P (XWINDOW (selected_window))
2673 && WINDOWP (minibuf_selected_window)
2674 && w == XWINDOW (minibuf_selected_window))))
2675 {
2676 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2677 it->region_beg_charpos = min (PT, markpos);
2678 it->region_end_charpos = max (PT, markpos);
2679 }
2680 else
2681 it->region_beg_charpos = it->region_end_charpos = -1;
2682
2683 /* Get the position at which the redisplay_end_trigger hook should
2684 be run, if it is to be run at all. */
2685 if (MARKERP (w->redisplay_end_trigger)
2686 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2687 it->redisplay_end_trigger_charpos
2688 = marker_position (w->redisplay_end_trigger);
2689 else if (INTEGERP (w->redisplay_end_trigger))
2690 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2691
2692 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2693
2694 /* Are lines in the display truncated? */
2695 if (base_face_id != DEFAULT_FACE_ID
2696 || XINT (it->w->hscroll)
2697 || (! WINDOW_FULL_WIDTH_P (it->w)
2698 && ((!NILP (Vtruncate_partial_width_windows)
2699 && !INTEGERP (Vtruncate_partial_width_windows))
2700 || (INTEGERP (Vtruncate_partial_width_windows)
2701 && (WINDOW_TOTAL_COLS (it->w)
2702 < XINT (Vtruncate_partial_width_windows))))))
2703 it->line_wrap = TRUNCATE;
2704 else if (NILP (BVAR (current_buffer, truncate_lines)))
2705 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2706 ? WINDOW_WRAP : WORD_WRAP;
2707 else
2708 it->line_wrap = TRUNCATE;
2709
2710 /* Get dimensions of truncation and continuation glyphs. These are
2711 displayed as fringe bitmaps under X, so we don't need them for such
2712 frames. */
2713 if (!FRAME_WINDOW_P (it->f))
2714 {
2715 if (it->line_wrap == TRUNCATE)
2716 {
2717 /* We will need the truncation glyph. */
2718 xassert (it->glyph_row == NULL);
2719 produce_special_glyphs (it, IT_TRUNCATION);
2720 it->truncation_pixel_width = it->pixel_width;
2721 }
2722 else
2723 {
2724 /* We will need the continuation glyph. */
2725 xassert (it->glyph_row == NULL);
2726 produce_special_glyphs (it, IT_CONTINUATION);
2727 it->continuation_pixel_width = it->pixel_width;
2728 }
2729
2730 /* Reset these values to zero because the produce_special_glyphs
2731 above has changed them. */
2732 it->pixel_width = it->ascent = it->descent = 0;
2733 it->phys_ascent = it->phys_descent = 0;
2734 }
2735
2736 /* Set this after getting the dimensions of truncation and
2737 continuation glyphs, so that we don't produce glyphs when calling
2738 produce_special_glyphs, above. */
2739 it->glyph_row = row;
2740 it->area = TEXT_AREA;
2741
2742 /* Forget any previous info about this row being reversed. */
2743 if (it->glyph_row)
2744 it->glyph_row->reversed_p = 0;
2745
2746 /* Get the dimensions of the display area. The display area
2747 consists of the visible window area plus a horizontally scrolled
2748 part to the left of the window. All x-values are relative to the
2749 start of this total display area. */
2750 if (base_face_id != DEFAULT_FACE_ID)
2751 {
2752 /* Mode lines, menu bar in terminal frames. */
2753 it->first_visible_x = 0;
2754 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2755 }
2756 else
2757 {
2758 it->first_visible_x
2759 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2760 it->last_visible_x = (it->first_visible_x
2761 + window_box_width (w, TEXT_AREA));
2762
2763 /* If we truncate lines, leave room for the truncator glyph(s) at
2764 the right margin. Otherwise, leave room for the continuation
2765 glyph(s). Truncation and continuation glyphs are not inserted
2766 for window-based redisplay. */
2767 if (!FRAME_WINDOW_P (it->f))
2768 {
2769 if (it->line_wrap == TRUNCATE)
2770 it->last_visible_x -= it->truncation_pixel_width;
2771 else
2772 it->last_visible_x -= it->continuation_pixel_width;
2773 }
2774
2775 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2776 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2777 }
2778
2779 /* Leave room for a border glyph. */
2780 if (!FRAME_WINDOW_P (it->f)
2781 && !WINDOW_RIGHTMOST_P (it->w))
2782 it->last_visible_x -= 1;
2783
2784 it->last_visible_y = window_text_bottom_y (w);
2785
2786 /* For mode lines and alike, arrange for the first glyph having a
2787 left box line if the face specifies a box. */
2788 if (base_face_id != DEFAULT_FACE_ID)
2789 {
2790 struct face *face;
2791
2792 it->face_id = remapped_base_face_id;
2793
2794 /* If we have a boxed mode line, make the first character appear
2795 with a left box line. */
2796 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2797 if (face->box != FACE_NO_BOX)
2798 it->start_of_box_run_p = 1;
2799 }
2800
2801 /* If a buffer position was specified, set the iterator there,
2802 getting overlays and face properties from that position. */
2803 if (charpos >= BUF_BEG (current_buffer))
2804 {
2805 it->end_charpos = ZV;
2806 IT_CHARPOS (*it) = charpos;
2807
2808 /* We will rely on `reseat' to set this up properly, via
2809 handle_face_prop. */
2810 it->face_id = it->base_face_id;
2811
2812 /* Compute byte position if not specified. */
2813 if (bytepos < charpos)
2814 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2815 else
2816 IT_BYTEPOS (*it) = bytepos;
2817
2818 it->start = it->current;
2819 /* Do we need to reorder bidirectional text? Not if this is a
2820 unibyte buffer: by definition, none of the single-byte
2821 characters are strong R2L, so no reordering is needed. And
2822 bidi.c doesn't support unibyte buffers anyway. Also, don't
2823 reorder while we are loading loadup.el, since the tables of
2824 character properties needed for reordering are not yet
2825 available. */
2826 it->bidi_p =
2827 NILP (Vpurify_flag)
2828 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2829 && it->multibyte_p;
2830
2831 /* If we are to reorder bidirectional text, init the bidi
2832 iterator. */
2833 if (it->bidi_p)
2834 {
2835 /* Note the paragraph direction that this buffer wants to
2836 use. */
2837 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2838 Qleft_to_right))
2839 it->paragraph_embedding = L2R;
2840 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2841 Qright_to_left))
2842 it->paragraph_embedding = R2L;
2843 else
2844 it->paragraph_embedding = NEUTRAL_DIR;
2845 bidi_unshelve_cache (NULL, 0);
2846 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2847 &it->bidi_it);
2848 }
2849
2850 /* Compute faces etc. */
2851 reseat (it, it->current.pos, 1);
2852 }
2853
2854 CHECK_IT (it);
2855 }
2856
2857
2858 /* Initialize IT for the display of window W with window start POS. */
2859
2860 void
2861 start_display (struct it *it, struct window *w, struct text_pos pos)
2862 {
2863 struct glyph_row *row;
2864 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2865
2866 row = w->desired_matrix->rows + first_vpos;
2867 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2868 it->first_vpos = first_vpos;
2869
2870 /* Don't reseat to previous visible line start if current start
2871 position is in a string or image. */
2872 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2873 {
2874 int start_at_line_beg_p;
2875 int first_y = it->current_y;
2876
2877 /* If window start is not at a line start, skip forward to POS to
2878 get the correct continuation lines width. */
2879 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2880 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2881 if (!start_at_line_beg_p)
2882 {
2883 int new_x;
2884
2885 reseat_at_previous_visible_line_start (it);
2886 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2887
2888 new_x = it->current_x + it->pixel_width;
2889
2890 /* If lines are continued, this line may end in the middle
2891 of a multi-glyph character (e.g. a control character
2892 displayed as \003, or in the middle of an overlay
2893 string). In this case move_it_to above will not have
2894 taken us to the start of the continuation line but to the
2895 end of the continued line. */
2896 if (it->current_x > 0
2897 && it->line_wrap != TRUNCATE /* Lines are continued. */
2898 && (/* And glyph doesn't fit on the line. */
2899 new_x > it->last_visible_x
2900 /* Or it fits exactly and we're on a window
2901 system frame. */
2902 || (new_x == it->last_visible_x
2903 && FRAME_WINDOW_P (it->f))))
2904 {
2905 if ((it->current.dpvec_index >= 0
2906 || it->current.overlay_string_index >= 0)
2907 /* If we are on a newline from a display vector or
2908 overlay string, then we are already at the end of
2909 a screen line; no need to go to the next line in
2910 that case, as this line is not really continued.
2911 (If we do go to the next line, C-e will not DTRT.) */
2912 && it->c != '\n')
2913 {
2914 set_iterator_to_next (it, 1);
2915 move_it_in_display_line_to (it, -1, -1, 0);
2916 }
2917
2918 it->continuation_lines_width += it->current_x;
2919 }
2920 /* If the character at POS is displayed via a display
2921 vector, move_it_to above stops at the final glyph of
2922 IT->dpvec. To make the caller redisplay that character
2923 again (a.k.a. start at POS), we need to reset the
2924 dpvec_index to the beginning of IT->dpvec. */
2925 else if (it->current.dpvec_index >= 0)
2926 it->current.dpvec_index = 0;
2927
2928 /* We're starting a new display line, not affected by the
2929 height of the continued line, so clear the appropriate
2930 fields in the iterator structure. */
2931 it->max_ascent = it->max_descent = 0;
2932 it->max_phys_ascent = it->max_phys_descent = 0;
2933
2934 it->current_y = first_y;
2935 it->vpos = 0;
2936 it->current_x = it->hpos = 0;
2937 }
2938 }
2939 }
2940
2941
2942 /* Return 1 if POS is a position in ellipses displayed for invisible
2943 text. W is the window we display, for text property lookup. */
2944
2945 static int
2946 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2947 {
2948 Lisp_Object prop, window;
2949 int ellipses_p = 0;
2950 EMACS_INT charpos = CHARPOS (pos->pos);
2951
2952 /* If POS specifies a position in a display vector, this might
2953 be for an ellipsis displayed for invisible text. We won't
2954 get the iterator set up for delivering that ellipsis unless
2955 we make sure that it gets aware of the invisible text. */
2956 if (pos->dpvec_index >= 0
2957 && pos->overlay_string_index < 0
2958 && CHARPOS (pos->string_pos) < 0
2959 && charpos > BEGV
2960 && (XSETWINDOW (window, w),
2961 prop = Fget_char_property (make_number (charpos),
2962 Qinvisible, window),
2963 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2964 {
2965 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2966 window);
2967 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2968 }
2969
2970 return ellipses_p;
2971 }
2972
2973
2974 /* Initialize IT for stepping through current_buffer in window W,
2975 starting at position POS that includes overlay string and display
2976 vector/ control character translation position information. Value
2977 is zero if there are overlay strings with newlines at POS. */
2978
2979 static int
2980 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2981 {
2982 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2983 int i, overlay_strings_with_newlines = 0;
2984
2985 /* If POS specifies a position in a display vector, this might
2986 be for an ellipsis displayed for invisible text. We won't
2987 get the iterator set up for delivering that ellipsis unless
2988 we make sure that it gets aware of the invisible text. */
2989 if (in_ellipses_for_invisible_text_p (pos, w))
2990 {
2991 --charpos;
2992 bytepos = 0;
2993 }
2994
2995 /* Keep in mind: the call to reseat in init_iterator skips invisible
2996 text, so we might end up at a position different from POS. This
2997 is only a problem when POS is a row start after a newline and an
2998 overlay starts there with an after-string, and the overlay has an
2999 invisible property. Since we don't skip invisible text in
3000 display_line and elsewhere immediately after consuming the
3001 newline before the row start, such a POS will not be in a string,
3002 but the call to init_iterator below will move us to the
3003 after-string. */
3004 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3005
3006 /* This only scans the current chunk -- it should scan all chunks.
3007 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3008 to 16 in 22.1 to make this a lesser problem. */
3009 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3010 {
3011 const char *s = SSDATA (it->overlay_strings[i]);
3012 const char *e = s + SBYTES (it->overlay_strings[i]);
3013
3014 while (s < e && *s != '\n')
3015 ++s;
3016
3017 if (s < e)
3018 {
3019 overlay_strings_with_newlines = 1;
3020 break;
3021 }
3022 }
3023
3024 /* If position is within an overlay string, set up IT to the right
3025 overlay string. */
3026 if (pos->overlay_string_index >= 0)
3027 {
3028 int relative_index;
3029
3030 /* If the first overlay string happens to have a `display'
3031 property for an image, the iterator will be set up for that
3032 image, and we have to undo that setup first before we can
3033 correct the overlay string index. */
3034 if (it->method == GET_FROM_IMAGE)
3035 pop_it (it);
3036
3037 /* We already have the first chunk of overlay strings in
3038 IT->overlay_strings. Load more until the one for
3039 pos->overlay_string_index is in IT->overlay_strings. */
3040 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3041 {
3042 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3043 it->current.overlay_string_index = 0;
3044 while (n--)
3045 {
3046 load_overlay_strings (it, 0);
3047 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3048 }
3049 }
3050
3051 it->current.overlay_string_index = pos->overlay_string_index;
3052 relative_index = (it->current.overlay_string_index
3053 % OVERLAY_STRING_CHUNK_SIZE);
3054 it->string = it->overlay_strings[relative_index];
3055 xassert (STRINGP (it->string));
3056 it->current.string_pos = pos->string_pos;
3057 it->method = GET_FROM_STRING;
3058 }
3059
3060 if (CHARPOS (pos->string_pos) >= 0)
3061 {
3062 /* Recorded position is not in an overlay string, but in another
3063 string. This can only be a string from a `display' property.
3064 IT should already be filled with that string. */
3065 it->current.string_pos = pos->string_pos;
3066 xassert (STRINGP (it->string));
3067 }
3068
3069 /* Restore position in display vector translations, control
3070 character translations or ellipses. */
3071 if (pos->dpvec_index >= 0)
3072 {
3073 if (it->dpvec == NULL)
3074 get_next_display_element (it);
3075 xassert (it->dpvec && it->current.dpvec_index == 0);
3076 it->current.dpvec_index = pos->dpvec_index;
3077 }
3078
3079 CHECK_IT (it);
3080 return !overlay_strings_with_newlines;
3081 }
3082
3083
3084 /* Initialize IT for stepping through current_buffer in window W
3085 starting at ROW->start. */
3086
3087 static void
3088 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3089 {
3090 init_from_display_pos (it, w, &row->start);
3091 it->start = row->start;
3092 it->continuation_lines_width = row->continuation_lines_width;
3093 CHECK_IT (it);
3094 }
3095
3096
3097 /* Initialize IT for stepping through current_buffer in window W
3098 starting in the line following ROW, i.e. starting at ROW->end.
3099 Value is zero if there are overlay strings with newlines at ROW's
3100 end position. */
3101
3102 static int
3103 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3104 {
3105 int success = 0;
3106
3107 if (init_from_display_pos (it, w, &row->end))
3108 {
3109 if (row->continued_p)
3110 it->continuation_lines_width
3111 = row->continuation_lines_width + row->pixel_width;
3112 CHECK_IT (it);
3113 success = 1;
3114 }
3115
3116 return success;
3117 }
3118
3119
3120
3121 \f
3122 /***********************************************************************
3123 Text properties
3124 ***********************************************************************/
3125
3126 /* Called when IT reaches IT->stop_charpos. Handle text property and
3127 overlay changes. Set IT->stop_charpos to the next position where
3128 to stop. */
3129
3130 static void
3131 handle_stop (struct it *it)
3132 {
3133 enum prop_handled handled;
3134 int handle_overlay_change_p;
3135 struct props *p;
3136
3137 it->dpvec = NULL;
3138 it->current.dpvec_index = -1;
3139 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3140 it->ignore_overlay_strings_at_pos_p = 0;
3141 it->ellipsis_p = 0;
3142
3143 /* Use face of preceding text for ellipsis (if invisible) */
3144 if (it->selective_display_ellipsis_p)
3145 it->saved_face_id = it->face_id;
3146
3147 do
3148 {
3149 handled = HANDLED_NORMALLY;
3150
3151 /* Call text property handlers. */
3152 for (p = it_props; p->handler; ++p)
3153 {
3154 handled = p->handler (it);
3155
3156 if (handled == HANDLED_RECOMPUTE_PROPS)
3157 break;
3158 else if (handled == HANDLED_RETURN)
3159 {
3160 /* We still want to show before and after strings from
3161 overlays even if the actual buffer text is replaced. */
3162 if (!handle_overlay_change_p
3163 || it->sp > 1
3164 /* Don't call get_overlay_strings_1 if we already
3165 have overlay strings loaded, because doing so
3166 will load them again and push the iterator state
3167 onto the stack one more time, which is not
3168 expected by the rest of the code that processes
3169 overlay strings. */
3170 || (it->n_overlay_strings <= 0
3171 ? !get_overlay_strings_1 (it, 0, 0)
3172 : 0))
3173 {
3174 if (it->ellipsis_p)
3175 setup_for_ellipsis (it, 0);
3176 /* When handling a display spec, we might load an
3177 empty string. In that case, discard it here. We
3178 used to discard it in handle_single_display_spec,
3179 but that causes get_overlay_strings_1, above, to
3180 ignore overlay strings that we must check. */
3181 if (STRINGP (it->string) && !SCHARS (it->string))
3182 pop_it (it);
3183 return;
3184 }
3185 else if (STRINGP (it->string) && !SCHARS (it->string))
3186 pop_it (it);
3187 else
3188 {
3189 it->ignore_overlay_strings_at_pos_p = 1;
3190 it->string_from_display_prop_p = 0;
3191 it->from_disp_prop_p = 0;
3192 handle_overlay_change_p = 0;
3193 }
3194 handled = HANDLED_RECOMPUTE_PROPS;
3195 break;
3196 }
3197 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3198 handle_overlay_change_p = 0;
3199 }
3200
3201 if (handled != HANDLED_RECOMPUTE_PROPS)
3202 {
3203 /* Don't check for overlay strings below when set to deliver
3204 characters from a display vector. */
3205 if (it->method == GET_FROM_DISPLAY_VECTOR)
3206 handle_overlay_change_p = 0;
3207
3208 /* Handle overlay changes.
3209 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3210 if it finds overlays. */
3211 if (handle_overlay_change_p)
3212 handled = handle_overlay_change (it);
3213 }
3214
3215 if (it->ellipsis_p)
3216 {
3217 setup_for_ellipsis (it, 0);
3218 break;
3219 }
3220 }
3221 while (handled == HANDLED_RECOMPUTE_PROPS);
3222
3223 /* Determine where to stop next. */
3224 if (handled == HANDLED_NORMALLY)
3225 compute_stop_pos (it);
3226 }
3227
3228
3229 /* Compute IT->stop_charpos from text property and overlay change
3230 information for IT's current position. */
3231
3232 static void
3233 compute_stop_pos (struct it *it)
3234 {
3235 register INTERVAL iv, next_iv;
3236 Lisp_Object object, limit, position;
3237 EMACS_INT charpos, bytepos;
3238
3239 if (STRINGP (it->string))
3240 {
3241 /* Strings are usually short, so don't limit the search for
3242 properties. */
3243 it->stop_charpos = it->end_charpos;
3244 object = it->string;
3245 limit = Qnil;
3246 charpos = IT_STRING_CHARPOS (*it);
3247 bytepos = IT_STRING_BYTEPOS (*it);
3248 }
3249 else
3250 {
3251 EMACS_INT pos;
3252
3253 /* If end_charpos is out of range for some reason, such as a
3254 misbehaving display function, rationalize it (Bug#5984). */
3255 if (it->end_charpos > ZV)
3256 it->end_charpos = ZV;
3257 it->stop_charpos = it->end_charpos;
3258
3259 /* If next overlay change is in front of the current stop pos
3260 (which is IT->end_charpos), stop there. Note: value of
3261 next_overlay_change is point-max if no overlay change
3262 follows. */
3263 charpos = IT_CHARPOS (*it);
3264 bytepos = IT_BYTEPOS (*it);
3265 pos = next_overlay_change (charpos);
3266 if (pos < it->stop_charpos)
3267 it->stop_charpos = pos;
3268
3269 /* If showing the region, we have to stop at the region
3270 start or end because the face might change there. */
3271 if (it->region_beg_charpos > 0)
3272 {
3273 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3274 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3275 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3276 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3277 }
3278
3279 /* Set up variables for computing the stop position from text
3280 property changes. */
3281 XSETBUFFER (object, current_buffer);
3282 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3283 }
3284
3285 /* Get the interval containing IT's position. Value is a null
3286 interval if there isn't such an interval. */
3287 position = make_number (charpos);
3288 iv = validate_interval_range (object, &position, &position, 0);
3289 if (!NULL_INTERVAL_P (iv))
3290 {
3291 Lisp_Object values_here[LAST_PROP_IDX];
3292 struct props *p;
3293
3294 /* Get properties here. */
3295 for (p = it_props; p->handler; ++p)
3296 values_here[p->idx] = textget (iv->plist, *p->name);
3297
3298 /* Look for an interval following iv that has different
3299 properties. */
3300 for (next_iv = next_interval (iv);
3301 (!NULL_INTERVAL_P (next_iv)
3302 && (NILP (limit)
3303 || XFASTINT (limit) > next_iv->position));
3304 next_iv = next_interval (next_iv))
3305 {
3306 for (p = it_props; p->handler; ++p)
3307 {
3308 Lisp_Object new_value;
3309
3310 new_value = textget (next_iv->plist, *p->name);
3311 if (!EQ (values_here[p->idx], new_value))
3312 break;
3313 }
3314
3315 if (p->handler)
3316 break;
3317 }
3318
3319 if (!NULL_INTERVAL_P (next_iv))
3320 {
3321 if (INTEGERP (limit)
3322 && next_iv->position >= XFASTINT (limit))
3323 /* No text property change up to limit. */
3324 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3325 else
3326 /* Text properties change in next_iv. */
3327 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3328 }
3329 }
3330
3331 if (it->cmp_it.id < 0)
3332 {
3333 EMACS_INT stoppos = it->end_charpos;
3334
3335 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3336 stoppos = -1;
3337 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3338 stoppos, it->string);
3339 }
3340
3341 xassert (STRINGP (it->string)
3342 || (it->stop_charpos >= BEGV
3343 && it->stop_charpos >= IT_CHARPOS (*it)));
3344 }
3345
3346
3347 /* Return the position of the next overlay change after POS in
3348 current_buffer. Value is point-max if no overlay change
3349 follows. This is like `next-overlay-change' but doesn't use
3350 xmalloc. */
3351
3352 static EMACS_INT
3353 next_overlay_change (EMACS_INT pos)
3354 {
3355 ptrdiff_t i, noverlays;
3356 EMACS_INT endpos;
3357 Lisp_Object *overlays;
3358
3359 /* Get all overlays at the given position. */
3360 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3361
3362 /* If any of these overlays ends before endpos,
3363 use its ending point instead. */
3364 for (i = 0; i < noverlays; ++i)
3365 {
3366 Lisp_Object oend;
3367 EMACS_INT oendpos;
3368
3369 oend = OVERLAY_END (overlays[i]);
3370 oendpos = OVERLAY_POSITION (oend);
3371 endpos = min (endpos, oendpos);
3372 }
3373
3374 return endpos;
3375 }
3376
3377 /* How many characters forward to search for a display property or
3378 display string. Searching too far forward makes the bidi display
3379 sluggish, especially in small windows. */
3380 #define MAX_DISP_SCAN 250
3381
3382 /* Return the character position of a display string at or after
3383 position specified by POSITION. If no display string exists at or
3384 after POSITION, return ZV. A display string is either an overlay
3385 with `display' property whose value is a string, or a `display'
3386 text property whose value is a string. STRING is data about the
3387 string to iterate; if STRING->lstring is nil, we are iterating a
3388 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3389 on a GUI frame. DISP_PROP is set to zero if we searched
3390 MAX_DISP_SCAN characters forward without finding any display
3391 strings, non-zero otherwise. It is set to 2 if the display string
3392 uses any kind of `(space ...)' spec that will produce a stretch of
3393 white space in the text area. */
3394 EMACS_INT
3395 compute_display_string_pos (struct text_pos *position,
3396 struct bidi_string_data *string,
3397 int frame_window_p, int *disp_prop)
3398 {
3399 /* OBJECT = nil means current buffer. */
3400 Lisp_Object object =
3401 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3402 Lisp_Object pos, spec, limpos;
3403 int string_p = (string && (STRINGP (string->lstring) || string->s));
3404 EMACS_INT eob = string_p ? string->schars : ZV;
3405 EMACS_INT begb = string_p ? 0 : BEGV;
3406 EMACS_INT bufpos, charpos = CHARPOS (*position);
3407 EMACS_INT lim =
3408 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3409 struct text_pos tpos;
3410 int rv = 0;
3411
3412 *disp_prop = 1;
3413
3414 if (charpos >= eob
3415 /* We don't support display properties whose values are strings
3416 that have display string properties. */
3417 || string->from_disp_str
3418 /* C strings cannot have display properties. */
3419 || (string->s && !STRINGP (object)))
3420 {
3421 *disp_prop = 0;
3422 return eob;
3423 }
3424
3425 /* If the character at CHARPOS is where the display string begins,
3426 return CHARPOS. */
3427 pos = make_number (charpos);
3428 if (STRINGP (object))
3429 bufpos = string->bufpos;
3430 else
3431 bufpos = charpos;
3432 tpos = *position;
3433 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3434 && (charpos <= begb
3435 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3436 object),
3437 spec))
3438 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3439 frame_window_p)))
3440 {
3441 if (rv == 2)
3442 *disp_prop = 2;
3443 return charpos;
3444 }
3445
3446 /* Look forward for the first character with a `display' property
3447 that will replace the underlying text when displayed. */
3448 limpos = make_number (lim);
3449 do {
3450 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3451 CHARPOS (tpos) = XFASTINT (pos);
3452 if (CHARPOS (tpos) >= lim)
3453 {
3454 *disp_prop = 0;
3455 break;
3456 }
3457 if (STRINGP (object))
3458 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3459 else
3460 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3461 spec = Fget_char_property (pos, Qdisplay, object);
3462 if (!STRINGP (object))
3463 bufpos = CHARPOS (tpos);
3464 } while (NILP (spec)
3465 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3466 bufpos, frame_window_p)));
3467 if (rv == 2)
3468 *disp_prop = 2;
3469
3470 return CHARPOS (tpos);
3471 }
3472
3473 /* Return the character position of the end of the display string that
3474 started at CHARPOS. If there's no display string at CHARPOS,
3475 return -1. A display string is either an overlay with `display'
3476 property whose value is a string or a `display' text property whose
3477 value is a string. */
3478 EMACS_INT
3479 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3480 {
3481 /* OBJECT = nil means current buffer. */
3482 Lisp_Object object =
3483 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3484 Lisp_Object pos = make_number (charpos);
3485 EMACS_INT eob =
3486 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3487
3488 if (charpos >= eob || (string->s && !STRINGP (object)))
3489 return eob;
3490
3491 /* It could happen that the display property or overlay was removed
3492 since we found it in compute_display_string_pos above. One way
3493 this can happen is if JIT font-lock was called (through
3494 handle_fontified_prop), and jit-lock-functions remove text
3495 properties or overlays from the portion of buffer that includes
3496 CHARPOS. Muse mode is known to do that, for example. In this
3497 case, we return -1 to the caller, to signal that no display
3498 string is actually present at CHARPOS. See bidi_fetch_char for
3499 how this is handled.
3500
3501 An alternative would be to never look for display properties past
3502 it->stop_charpos. But neither compute_display_string_pos nor
3503 bidi_fetch_char that calls it know or care where the next
3504 stop_charpos is. */
3505 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3506 return -1;
3507
3508 /* Look forward for the first character where the `display' property
3509 changes. */
3510 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3511
3512 return XFASTINT (pos);
3513 }
3514
3515
3516 \f
3517 /***********************************************************************
3518 Fontification
3519 ***********************************************************************/
3520
3521 /* Handle changes in the `fontified' property of the current buffer by
3522 calling hook functions from Qfontification_functions to fontify
3523 regions of text. */
3524
3525 static enum prop_handled
3526 handle_fontified_prop (struct it *it)
3527 {
3528 Lisp_Object prop, pos;
3529 enum prop_handled handled = HANDLED_NORMALLY;
3530
3531 if (!NILP (Vmemory_full))
3532 return handled;
3533
3534 /* Get the value of the `fontified' property at IT's current buffer
3535 position. (The `fontified' property doesn't have a special
3536 meaning in strings.) If the value is nil, call functions from
3537 Qfontification_functions. */
3538 if (!STRINGP (it->string)
3539 && it->s == NULL
3540 && !NILP (Vfontification_functions)
3541 && !NILP (Vrun_hooks)
3542 && (pos = make_number (IT_CHARPOS (*it)),
3543 prop = Fget_char_property (pos, Qfontified, Qnil),
3544 /* Ignore the special cased nil value always present at EOB since
3545 no amount of fontifying will be able to change it. */
3546 NILP (prop) && IT_CHARPOS (*it) < Z))
3547 {
3548 int count = SPECPDL_INDEX ();
3549 Lisp_Object val;
3550 struct buffer *obuf = current_buffer;
3551 int begv = BEGV, zv = ZV;
3552 int old_clip_changed = current_buffer->clip_changed;
3553
3554 val = Vfontification_functions;
3555 specbind (Qfontification_functions, Qnil);
3556
3557 xassert (it->end_charpos == ZV);
3558
3559 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3560 safe_call1 (val, pos);
3561 else
3562 {
3563 Lisp_Object fns, fn;
3564 struct gcpro gcpro1, gcpro2;
3565
3566 fns = Qnil;
3567 GCPRO2 (val, fns);
3568
3569 for (; CONSP (val); val = XCDR (val))
3570 {
3571 fn = XCAR (val);
3572
3573 if (EQ (fn, Qt))
3574 {
3575 /* A value of t indicates this hook has a local
3576 binding; it means to run the global binding too.
3577 In a global value, t should not occur. If it
3578 does, we must ignore it to avoid an endless
3579 loop. */
3580 for (fns = Fdefault_value (Qfontification_functions);
3581 CONSP (fns);
3582 fns = XCDR (fns))
3583 {
3584 fn = XCAR (fns);
3585 if (!EQ (fn, Qt))
3586 safe_call1 (fn, pos);
3587 }
3588 }
3589 else
3590 safe_call1 (fn, pos);
3591 }
3592
3593 UNGCPRO;
3594 }
3595
3596 unbind_to (count, Qnil);
3597
3598 /* Fontification functions routinely call `save-restriction'.
3599 Normally, this tags clip_changed, which can confuse redisplay
3600 (see discussion in Bug#6671). Since we don't perform any
3601 special handling of fontification changes in the case where
3602 `save-restriction' isn't called, there's no point doing so in
3603 this case either. So, if the buffer's restrictions are
3604 actually left unchanged, reset clip_changed. */
3605 if (obuf == current_buffer)
3606 {
3607 if (begv == BEGV && zv == ZV)
3608 current_buffer->clip_changed = old_clip_changed;
3609 }
3610 /* There isn't much we can reasonably do to protect against
3611 misbehaving fontification, but here's a fig leaf. */
3612 else if (!NILP (BVAR (obuf, name)))
3613 set_buffer_internal_1 (obuf);
3614
3615 /* The fontification code may have added/removed text.
3616 It could do even a lot worse, but let's at least protect against
3617 the most obvious case where only the text past `pos' gets changed',
3618 as is/was done in grep.el where some escapes sequences are turned
3619 into face properties (bug#7876). */
3620 it->end_charpos = ZV;
3621
3622 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3623 something. This avoids an endless loop if they failed to
3624 fontify the text for which reason ever. */
3625 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3626 handled = HANDLED_RECOMPUTE_PROPS;
3627 }
3628
3629 return handled;
3630 }
3631
3632
3633 \f
3634 /***********************************************************************
3635 Faces
3636 ***********************************************************************/
3637
3638 /* Set up iterator IT from face properties at its current position.
3639 Called from handle_stop. */
3640
3641 static enum prop_handled
3642 handle_face_prop (struct it *it)
3643 {
3644 int new_face_id;
3645 EMACS_INT next_stop;
3646
3647 if (!STRINGP (it->string))
3648 {
3649 new_face_id
3650 = face_at_buffer_position (it->w,
3651 IT_CHARPOS (*it),
3652 it->region_beg_charpos,
3653 it->region_end_charpos,
3654 &next_stop,
3655 (IT_CHARPOS (*it)
3656 + TEXT_PROP_DISTANCE_LIMIT),
3657 0, it->base_face_id);
3658
3659 /* Is this a start of a run of characters with box face?
3660 Caveat: this can be called for a freshly initialized
3661 iterator; face_id is -1 in this case. We know that the new
3662 face will not change until limit, i.e. if the new face has a
3663 box, all characters up to limit will have one. But, as
3664 usual, we don't know whether limit is really the end. */
3665 if (new_face_id != it->face_id)
3666 {
3667 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3668
3669 /* If new face has a box but old face has not, this is
3670 the start of a run of characters with box, i.e. it has
3671 a shadow on the left side. The value of face_id of the
3672 iterator will be -1 if this is the initial call that gets
3673 the face. In this case, we have to look in front of IT's
3674 position and see whether there is a face != new_face_id. */
3675 it->start_of_box_run_p
3676 = (new_face->box != FACE_NO_BOX
3677 && (it->face_id >= 0
3678 || IT_CHARPOS (*it) == BEG
3679 || new_face_id != face_before_it_pos (it)));
3680 it->face_box_p = new_face->box != FACE_NO_BOX;
3681 }
3682 }
3683 else
3684 {
3685 int base_face_id;
3686 EMACS_INT bufpos;
3687 int i;
3688 Lisp_Object from_overlay
3689 = (it->current.overlay_string_index >= 0
3690 ? it->string_overlays[it->current.overlay_string_index]
3691 : Qnil);
3692
3693 /* See if we got to this string directly or indirectly from
3694 an overlay property. That includes the before-string or
3695 after-string of an overlay, strings in display properties
3696 provided by an overlay, their text properties, etc.
3697
3698 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3699 if (! NILP (from_overlay))
3700 for (i = it->sp - 1; i >= 0; i--)
3701 {
3702 if (it->stack[i].current.overlay_string_index >= 0)
3703 from_overlay
3704 = it->string_overlays[it->stack[i].current.overlay_string_index];
3705 else if (! NILP (it->stack[i].from_overlay))
3706 from_overlay = it->stack[i].from_overlay;
3707
3708 if (!NILP (from_overlay))
3709 break;
3710 }
3711
3712 if (! NILP (from_overlay))
3713 {
3714 bufpos = IT_CHARPOS (*it);
3715 /* For a string from an overlay, the base face depends
3716 only on text properties and ignores overlays. */
3717 base_face_id
3718 = face_for_overlay_string (it->w,
3719 IT_CHARPOS (*it),
3720 it->region_beg_charpos,
3721 it->region_end_charpos,
3722 &next_stop,
3723 (IT_CHARPOS (*it)
3724 + TEXT_PROP_DISTANCE_LIMIT),
3725 0,
3726 from_overlay);
3727 }
3728 else
3729 {
3730 bufpos = 0;
3731
3732 /* For strings from a `display' property, use the face at
3733 IT's current buffer position as the base face to merge
3734 with, so that overlay strings appear in the same face as
3735 surrounding text, unless they specify their own
3736 faces. */
3737 base_face_id = it->string_from_prefix_prop_p
3738 ? DEFAULT_FACE_ID
3739 : underlying_face_id (it);
3740 }
3741
3742 new_face_id = face_at_string_position (it->w,
3743 it->string,
3744 IT_STRING_CHARPOS (*it),
3745 bufpos,
3746 it->region_beg_charpos,
3747 it->region_end_charpos,
3748 &next_stop,
3749 base_face_id, 0);
3750
3751 /* Is this a start of a run of characters with box? Caveat:
3752 this can be called for a freshly allocated iterator; face_id
3753 is -1 is this case. We know that the new face will not
3754 change until the next check pos, i.e. if the new face has a
3755 box, all characters up to that position will have a
3756 box. But, as usual, we don't know whether that position
3757 is really the end. */
3758 if (new_face_id != it->face_id)
3759 {
3760 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3761 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3762
3763 /* If new face has a box but old face hasn't, this is the
3764 start of a run of characters with box, i.e. it has a
3765 shadow on the left side. */
3766 it->start_of_box_run_p
3767 = new_face->box && (old_face == NULL || !old_face->box);
3768 it->face_box_p = new_face->box != FACE_NO_BOX;
3769 }
3770 }
3771
3772 it->face_id = new_face_id;
3773 return HANDLED_NORMALLY;
3774 }
3775
3776
3777 /* Return the ID of the face ``underlying'' IT's current position,
3778 which is in a string. If the iterator is associated with a
3779 buffer, return the face at IT's current buffer position.
3780 Otherwise, use the iterator's base_face_id. */
3781
3782 static int
3783 underlying_face_id (struct it *it)
3784 {
3785 int face_id = it->base_face_id, i;
3786
3787 xassert (STRINGP (it->string));
3788
3789 for (i = it->sp - 1; i >= 0; --i)
3790 if (NILP (it->stack[i].string))
3791 face_id = it->stack[i].face_id;
3792
3793 return face_id;
3794 }
3795
3796
3797 /* Compute the face one character before or after the current position
3798 of IT, in the visual order. BEFORE_P non-zero means get the face
3799 in front (to the left in L2R paragraphs, to the right in R2L
3800 paragraphs) of IT's screen position. Value is the ID of the face. */
3801
3802 static int
3803 face_before_or_after_it_pos (struct it *it, int before_p)
3804 {
3805 int face_id, limit;
3806 EMACS_INT next_check_charpos;
3807 struct it it_copy;
3808 void *it_copy_data = NULL;
3809
3810 xassert (it->s == NULL);
3811
3812 if (STRINGP (it->string))
3813 {
3814 EMACS_INT bufpos, charpos;
3815 int base_face_id;
3816
3817 /* No face change past the end of the string (for the case
3818 we are padding with spaces). No face change before the
3819 string start. */
3820 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3821 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3822 return it->face_id;
3823
3824 if (!it->bidi_p)
3825 {
3826 /* Set charpos to the position before or after IT's current
3827 position, in the logical order, which in the non-bidi
3828 case is the same as the visual order. */
3829 if (before_p)
3830 charpos = IT_STRING_CHARPOS (*it) - 1;
3831 else if (it->what == IT_COMPOSITION)
3832 /* For composition, we must check the character after the
3833 composition. */
3834 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3835 else
3836 charpos = IT_STRING_CHARPOS (*it) + 1;
3837 }
3838 else
3839 {
3840 if (before_p)
3841 {
3842 /* With bidi iteration, the character before the current
3843 in the visual order cannot be found by simple
3844 iteration, because "reverse" reordering is not
3845 supported. Instead, we need to use the move_it_*
3846 family of functions. */
3847 /* Ignore face changes before the first visible
3848 character on this display line. */
3849 if (it->current_x <= it->first_visible_x)
3850 return it->face_id;
3851 SAVE_IT (it_copy, *it, it_copy_data);
3852 /* Implementation note: Since move_it_in_display_line
3853 works in the iterator geometry, and thinks the first
3854 character is always the leftmost, even in R2L lines,
3855 we don't need to distinguish between the R2L and L2R
3856 cases here. */
3857 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3858 it_copy.current_x - 1, MOVE_TO_X);
3859 charpos = IT_STRING_CHARPOS (it_copy);
3860 RESTORE_IT (it, it, it_copy_data);
3861 }
3862 else
3863 {
3864 /* Set charpos to the string position of the character
3865 that comes after IT's current position in the visual
3866 order. */
3867 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3868
3869 it_copy = *it;
3870 while (n--)
3871 bidi_move_to_visually_next (&it_copy.bidi_it);
3872
3873 charpos = it_copy.bidi_it.charpos;
3874 }
3875 }
3876 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3877
3878 if (it->current.overlay_string_index >= 0)
3879 bufpos = IT_CHARPOS (*it);
3880 else
3881 bufpos = 0;
3882
3883 base_face_id = underlying_face_id (it);
3884
3885 /* Get the face for ASCII, or unibyte. */
3886 face_id = face_at_string_position (it->w,
3887 it->string,
3888 charpos,
3889 bufpos,
3890 it->region_beg_charpos,
3891 it->region_end_charpos,
3892 &next_check_charpos,
3893 base_face_id, 0);
3894
3895 /* Correct the face for charsets different from ASCII. Do it
3896 for the multibyte case only. The face returned above is
3897 suitable for unibyte text if IT->string is unibyte. */
3898 if (STRING_MULTIBYTE (it->string))
3899 {
3900 struct text_pos pos1 = string_pos (charpos, it->string);
3901 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3902 int c, len;
3903 struct face *face = FACE_FROM_ID (it->f, face_id);
3904
3905 c = string_char_and_length (p, &len);
3906 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3907 }
3908 }
3909 else
3910 {
3911 struct text_pos pos;
3912
3913 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3914 || (IT_CHARPOS (*it) <= BEGV && before_p))
3915 return it->face_id;
3916
3917 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3918 pos = it->current.pos;
3919
3920 if (!it->bidi_p)
3921 {
3922 if (before_p)
3923 DEC_TEXT_POS (pos, it->multibyte_p);
3924 else
3925 {
3926 if (it->what == IT_COMPOSITION)
3927 {
3928 /* For composition, we must check the position after
3929 the composition. */
3930 pos.charpos += it->cmp_it.nchars;
3931 pos.bytepos += it->len;
3932 }
3933 else
3934 INC_TEXT_POS (pos, it->multibyte_p);
3935 }
3936 }
3937 else
3938 {
3939 if (before_p)
3940 {
3941 /* With bidi iteration, the character before the current
3942 in the visual order cannot be found by simple
3943 iteration, because "reverse" reordering is not
3944 supported. Instead, we need to use the move_it_*
3945 family of functions. */
3946 /* Ignore face changes before the first visible
3947 character on this display line. */
3948 if (it->current_x <= it->first_visible_x)
3949 return it->face_id;
3950 SAVE_IT (it_copy, *it, it_copy_data);
3951 /* Implementation note: Since move_it_in_display_line
3952 works in the iterator geometry, and thinks the first
3953 character is always the leftmost, even in R2L lines,
3954 we don't need to distinguish between the R2L and L2R
3955 cases here. */
3956 move_it_in_display_line (&it_copy, ZV,
3957 it_copy.current_x - 1, MOVE_TO_X);
3958 pos = it_copy.current.pos;
3959 RESTORE_IT (it, it, it_copy_data);
3960 }
3961 else
3962 {
3963 /* Set charpos to the buffer position of the character
3964 that comes after IT's current position in the visual
3965 order. */
3966 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3967
3968 it_copy = *it;
3969 while (n--)
3970 bidi_move_to_visually_next (&it_copy.bidi_it);
3971
3972 SET_TEXT_POS (pos,
3973 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3974 }
3975 }
3976 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3977
3978 /* Determine face for CHARSET_ASCII, or unibyte. */
3979 face_id = face_at_buffer_position (it->w,
3980 CHARPOS (pos),
3981 it->region_beg_charpos,
3982 it->region_end_charpos,
3983 &next_check_charpos,
3984 limit, 0, -1);
3985
3986 /* Correct the face for charsets different from ASCII. Do it
3987 for the multibyte case only. The face returned above is
3988 suitable for unibyte text if current_buffer is unibyte. */
3989 if (it->multibyte_p)
3990 {
3991 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3992 struct face *face = FACE_FROM_ID (it->f, face_id);
3993 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3994 }
3995 }
3996
3997 return face_id;
3998 }
3999
4000
4001 \f
4002 /***********************************************************************
4003 Invisible text
4004 ***********************************************************************/
4005
4006 /* Set up iterator IT from invisible properties at its current
4007 position. Called from handle_stop. */
4008
4009 static enum prop_handled
4010 handle_invisible_prop (struct it *it)
4011 {
4012 enum prop_handled handled = HANDLED_NORMALLY;
4013
4014 if (STRINGP (it->string))
4015 {
4016 Lisp_Object prop, end_charpos, limit, charpos;
4017
4018 /* Get the value of the invisible text property at the
4019 current position. Value will be nil if there is no such
4020 property. */
4021 charpos = make_number (IT_STRING_CHARPOS (*it));
4022 prop = Fget_text_property (charpos, Qinvisible, it->string);
4023
4024 if (!NILP (prop)
4025 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4026 {
4027 EMACS_INT endpos;
4028
4029 handled = HANDLED_RECOMPUTE_PROPS;
4030
4031 /* Get the position at which the next change of the
4032 invisible text property can be found in IT->string.
4033 Value will be nil if the property value is the same for
4034 all the rest of IT->string. */
4035 XSETINT (limit, SCHARS (it->string));
4036 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4037 it->string, limit);
4038
4039 /* Text at current position is invisible. The next
4040 change in the property is at position end_charpos.
4041 Move IT's current position to that position. */
4042 if (INTEGERP (end_charpos)
4043 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4044 {
4045 struct text_pos old;
4046 EMACS_INT oldpos;
4047
4048 old = it->current.string_pos;
4049 oldpos = CHARPOS (old);
4050 if (it->bidi_p)
4051 {
4052 if (it->bidi_it.first_elt
4053 && it->bidi_it.charpos < SCHARS (it->string))
4054 bidi_paragraph_init (it->paragraph_embedding,
4055 &it->bidi_it, 1);
4056 /* Bidi-iterate out of the invisible text. */
4057 do
4058 {
4059 bidi_move_to_visually_next (&it->bidi_it);
4060 }
4061 while (oldpos <= it->bidi_it.charpos
4062 && it->bidi_it.charpos < endpos);
4063
4064 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4065 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4066 if (IT_CHARPOS (*it) >= endpos)
4067 it->prev_stop = endpos;
4068 }
4069 else
4070 {
4071 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4072 compute_string_pos (&it->current.string_pos, old, it->string);
4073 }
4074 }
4075 else
4076 {
4077 /* The rest of the string is invisible. If this is an
4078 overlay string, proceed with the next overlay string
4079 or whatever comes and return a character from there. */
4080 if (it->current.overlay_string_index >= 0)
4081 {
4082 next_overlay_string (it);
4083 /* Don't check for overlay strings when we just
4084 finished processing them. */
4085 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4086 }
4087 else
4088 {
4089 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4090 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4091 }
4092 }
4093 }
4094 }
4095 else
4096 {
4097 int invis_p;
4098 EMACS_INT newpos, next_stop, start_charpos, tem;
4099 Lisp_Object pos, prop, overlay;
4100
4101 /* First of all, is there invisible text at this position? */
4102 tem = start_charpos = IT_CHARPOS (*it);
4103 pos = make_number (tem);
4104 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4105 &overlay);
4106 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4107
4108 /* If we are on invisible text, skip over it. */
4109 if (invis_p && start_charpos < it->end_charpos)
4110 {
4111 /* Record whether we have to display an ellipsis for the
4112 invisible text. */
4113 int display_ellipsis_p = invis_p == 2;
4114
4115 handled = HANDLED_RECOMPUTE_PROPS;
4116
4117 /* Loop skipping over invisible text. The loop is left at
4118 ZV or with IT on the first char being visible again. */
4119 do
4120 {
4121 /* Try to skip some invisible text. Return value is the
4122 position reached which can be equal to where we start
4123 if there is nothing invisible there. This skips both
4124 over invisible text properties and overlays with
4125 invisible property. */
4126 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4127
4128 /* If we skipped nothing at all we weren't at invisible
4129 text in the first place. If everything to the end of
4130 the buffer was skipped, end the loop. */
4131 if (newpos == tem || newpos >= ZV)
4132 invis_p = 0;
4133 else
4134 {
4135 /* We skipped some characters but not necessarily
4136 all there are. Check if we ended up on visible
4137 text. Fget_char_property returns the property of
4138 the char before the given position, i.e. if we
4139 get invis_p = 0, this means that the char at
4140 newpos is visible. */
4141 pos = make_number (newpos);
4142 prop = Fget_char_property (pos, Qinvisible, it->window);
4143 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4144 }
4145
4146 /* If we ended up on invisible text, proceed to
4147 skip starting with next_stop. */
4148 if (invis_p)
4149 tem = next_stop;
4150
4151 /* If there are adjacent invisible texts, don't lose the
4152 second one's ellipsis. */
4153 if (invis_p == 2)
4154 display_ellipsis_p = 1;
4155 }
4156 while (invis_p);
4157
4158 /* The position newpos is now either ZV or on visible text. */
4159 if (it->bidi_p)
4160 {
4161 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4162 int on_newline =
4163 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4164 int after_newline =
4165 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4166
4167 /* If the invisible text ends on a newline or on a
4168 character after a newline, we can avoid the costly,
4169 character by character, bidi iteration to NEWPOS, and
4170 instead simply reseat the iterator there. That's
4171 because all bidi reordering information is tossed at
4172 the newline. This is a big win for modes that hide
4173 complete lines, like Outline, Org, etc. */
4174 if (on_newline || after_newline)
4175 {
4176 struct text_pos tpos;
4177 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4178
4179 SET_TEXT_POS (tpos, newpos, bpos);
4180 reseat_1 (it, tpos, 0);
4181 /* If we reseat on a newline/ZV, we need to prep the
4182 bidi iterator for advancing to the next character
4183 after the newline/EOB, keeping the current paragraph
4184 direction (so that PRODUCE_GLYPHS does TRT wrt
4185 prepending/appending glyphs to a glyph row). */
4186 if (on_newline)
4187 {
4188 it->bidi_it.first_elt = 0;
4189 it->bidi_it.paragraph_dir = pdir;
4190 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4191 it->bidi_it.nchars = 1;
4192 it->bidi_it.ch_len = 1;
4193 }
4194 }
4195 else /* Must use the slow method. */
4196 {
4197 /* With bidi iteration, the region of invisible text
4198 could start and/or end in the middle of a
4199 non-base embedding level. Therefore, we need to
4200 skip invisible text using the bidi iterator,
4201 starting at IT's current position, until we find
4202 ourselves outside of the invisible text.
4203 Skipping invisible text _after_ bidi iteration
4204 avoids affecting the visual order of the
4205 displayed text when invisible properties are
4206 added or removed. */
4207 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4208 {
4209 /* If we were `reseat'ed to a new paragraph,
4210 determine the paragraph base direction. We
4211 need to do it now because
4212 next_element_from_buffer may not have a
4213 chance to do it, if we are going to skip any
4214 text at the beginning, which resets the
4215 FIRST_ELT flag. */
4216 bidi_paragraph_init (it->paragraph_embedding,
4217 &it->bidi_it, 1);
4218 }
4219 do
4220 {
4221 bidi_move_to_visually_next (&it->bidi_it);
4222 }
4223 while (it->stop_charpos <= it->bidi_it.charpos
4224 && it->bidi_it.charpos < newpos);
4225 IT_CHARPOS (*it) = it->bidi_it.charpos;
4226 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4227 /* If we overstepped NEWPOS, record its position in
4228 the iterator, so that we skip invisible text if
4229 later the bidi iteration lands us in the
4230 invisible region again. */
4231 if (IT_CHARPOS (*it) >= newpos)
4232 it->prev_stop = newpos;
4233 }
4234 }
4235 else
4236 {
4237 IT_CHARPOS (*it) = newpos;
4238 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4239 }
4240
4241 /* If there are before-strings at the start of invisible
4242 text, and the text is invisible because of a text
4243 property, arrange to show before-strings because 20.x did
4244 it that way. (If the text is invisible because of an
4245 overlay property instead of a text property, this is
4246 already handled in the overlay code.) */
4247 if (NILP (overlay)
4248 && get_overlay_strings (it, it->stop_charpos))
4249 {
4250 handled = HANDLED_RECOMPUTE_PROPS;
4251 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4252 }
4253 else if (display_ellipsis_p)
4254 {
4255 /* Make sure that the glyphs of the ellipsis will get
4256 correct `charpos' values. If we would not update
4257 it->position here, the glyphs would belong to the
4258 last visible character _before_ the invisible
4259 text, which confuses `set_cursor_from_row'.
4260
4261 We use the last invisible position instead of the
4262 first because this way the cursor is always drawn on
4263 the first "." of the ellipsis, whenever PT is inside
4264 the invisible text. Otherwise the cursor would be
4265 placed _after_ the ellipsis when the point is after the
4266 first invisible character. */
4267 if (!STRINGP (it->object))
4268 {
4269 it->position.charpos = newpos - 1;
4270 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4271 }
4272 it->ellipsis_p = 1;
4273 /* Let the ellipsis display before
4274 considering any properties of the following char.
4275 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4276 handled = HANDLED_RETURN;
4277 }
4278 }
4279 }
4280
4281 return handled;
4282 }
4283
4284
4285 /* Make iterator IT return `...' next.
4286 Replaces LEN characters from buffer. */
4287
4288 static void
4289 setup_for_ellipsis (struct it *it, int len)
4290 {
4291 /* Use the display table definition for `...'. Invalid glyphs
4292 will be handled by the method returning elements from dpvec. */
4293 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4294 {
4295 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4296 it->dpvec = v->contents;
4297 it->dpend = v->contents + v->header.size;
4298 }
4299 else
4300 {
4301 /* Default `...'. */
4302 it->dpvec = default_invis_vector;
4303 it->dpend = default_invis_vector + 3;
4304 }
4305
4306 it->dpvec_char_len = len;
4307 it->current.dpvec_index = 0;
4308 it->dpvec_face_id = -1;
4309
4310 /* Remember the current face id in case glyphs specify faces.
4311 IT's face is restored in set_iterator_to_next.
4312 saved_face_id was set to preceding char's face in handle_stop. */
4313 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4314 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4315
4316 it->method = GET_FROM_DISPLAY_VECTOR;
4317 it->ellipsis_p = 1;
4318 }
4319
4320
4321 \f
4322 /***********************************************************************
4323 'display' property
4324 ***********************************************************************/
4325
4326 /* Set up iterator IT from `display' property at its current position.
4327 Called from handle_stop.
4328 We return HANDLED_RETURN if some part of the display property
4329 overrides the display of the buffer text itself.
4330 Otherwise we return HANDLED_NORMALLY. */
4331
4332 static enum prop_handled
4333 handle_display_prop (struct it *it)
4334 {
4335 Lisp_Object propval, object, overlay;
4336 struct text_pos *position;
4337 EMACS_INT bufpos;
4338 /* Nonzero if some property replaces the display of the text itself. */
4339 int display_replaced_p = 0;
4340
4341 if (STRINGP (it->string))
4342 {
4343 object = it->string;
4344 position = &it->current.string_pos;
4345 bufpos = CHARPOS (it->current.pos);
4346 }
4347 else
4348 {
4349 XSETWINDOW (object, it->w);
4350 position = &it->current.pos;
4351 bufpos = CHARPOS (*position);
4352 }
4353
4354 /* Reset those iterator values set from display property values. */
4355 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4356 it->space_width = Qnil;
4357 it->font_height = Qnil;
4358 it->voffset = 0;
4359
4360 /* We don't support recursive `display' properties, i.e. string
4361 values that have a string `display' property, that have a string
4362 `display' property etc. */
4363 if (!it->string_from_display_prop_p)
4364 it->area = TEXT_AREA;
4365
4366 propval = get_char_property_and_overlay (make_number (position->charpos),
4367 Qdisplay, object, &overlay);
4368 if (NILP (propval))
4369 return HANDLED_NORMALLY;
4370 /* Now OVERLAY is the overlay that gave us this property, or nil
4371 if it was a text property. */
4372
4373 if (!STRINGP (it->string))
4374 object = it->w->buffer;
4375
4376 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4377 position, bufpos,
4378 FRAME_WINDOW_P (it->f));
4379
4380 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4381 }
4382
4383 /* Subroutine of handle_display_prop. Returns non-zero if the display
4384 specification in SPEC is a replacing specification, i.e. it would
4385 replace the text covered by `display' property with something else,
4386 such as an image or a display string. If SPEC includes any kind or
4387 `(space ...) specification, the value is 2; this is used by
4388 compute_display_string_pos, which see.
4389
4390 See handle_single_display_spec for documentation of arguments.
4391 frame_window_p is non-zero if the window being redisplayed is on a
4392 GUI frame; this argument is used only if IT is NULL, see below.
4393
4394 IT can be NULL, if this is called by the bidi reordering code
4395 through compute_display_string_pos, which see. In that case, this
4396 function only examines SPEC, but does not otherwise "handle" it, in
4397 the sense that it doesn't set up members of IT from the display
4398 spec. */
4399 static int
4400 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4401 Lisp_Object overlay, struct text_pos *position,
4402 EMACS_INT bufpos, int frame_window_p)
4403 {
4404 int replacing_p = 0;
4405 int rv;
4406
4407 if (CONSP (spec)
4408 /* Simple specifications. */
4409 && !EQ (XCAR (spec), Qimage)
4410 && !EQ (XCAR (spec), Qspace)
4411 && !EQ (XCAR (spec), Qwhen)
4412 && !EQ (XCAR (spec), Qslice)
4413 && !EQ (XCAR (spec), Qspace_width)
4414 && !EQ (XCAR (spec), Qheight)
4415 && !EQ (XCAR (spec), Qraise)
4416 /* Marginal area specifications. */
4417 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4418 && !EQ (XCAR (spec), Qleft_fringe)
4419 && !EQ (XCAR (spec), Qright_fringe)
4420 && !NILP (XCAR (spec)))
4421 {
4422 for (; CONSP (spec); spec = XCDR (spec))
4423 {
4424 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4425 overlay, position, bufpos,
4426 replacing_p, frame_window_p)))
4427 {
4428 replacing_p = rv;
4429 /* If some text in a string is replaced, `position' no
4430 longer points to the position of `object'. */
4431 if (!it || STRINGP (object))
4432 break;
4433 }
4434 }
4435 }
4436 else if (VECTORP (spec))
4437 {
4438 int i;
4439 for (i = 0; i < ASIZE (spec); ++i)
4440 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4441 overlay, position, bufpos,
4442 replacing_p, frame_window_p)))
4443 {
4444 replacing_p = rv;
4445 /* If some text in a string is replaced, `position' no
4446 longer points to the position of `object'. */
4447 if (!it || STRINGP (object))
4448 break;
4449 }
4450 }
4451 else
4452 {
4453 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4454 position, bufpos, 0,
4455 frame_window_p)))
4456 replacing_p = rv;
4457 }
4458
4459 return replacing_p;
4460 }
4461
4462 /* Value is the position of the end of the `display' property starting
4463 at START_POS in OBJECT. */
4464
4465 static struct text_pos
4466 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4467 {
4468 Lisp_Object end;
4469 struct text_pos end_pos;
4470
4471 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4472 Qdisplay, object, Qnil);
4473 CHARPOS (end_pos) = XFASTINT (end);
4474 if (STRINGP (object))
4475 compute_string_pos (&end_pos, start_pos, it->string);
4476 else
4477 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4478
4479 return end_pos;
4480 }
4481
4482
4483 /* Set up IT from a single `display' property specification SPEC. OBJECT
4484 is the object in which the `display' property was found. *POSITION
4485 is the position in OBJECT at which the `display' property was found.
4486 BUFPOS is the buffer position of OBJECT (different from POSITION if
4487 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4488 previously saw a display specification which already replaced text
4489 display with something else, for example an image; we ignore such
4490 properties after the first one has been processed.
4491
4492 OVERLAY is the overlay this `display' property came from,
4493 or nil if it was a text property.
4494
4495 If SPEC is a `space' or `image' specification, and in some other
4496 cases too, set *POSITION to the position where the `display'
4497 property ends.
4498
4499 If IT is NULL, only examine the property specification in SPEC, but
4500 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4501 is intended to be displayed in a window on a GUI frame.
4502
4503 Value is non-zero if something was found which replaces the display
4504 of buffer or string text. */
4505
4506 static int
4507 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4508 Lisp_Object overlay, struct text_pos *position,
4509 EMACS_INT bufpos, int display_replaced_p,
4510 int frame_window_p)
4511 {
4512 Lisp_Object form;
4513 Lisp_Object location, value;
4514 struct text_pos start_pos = *position;
4515 int valid_p;
4516
4517 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4518 If the result is non-nil, use VALUE instead of SPEC. */
4519 form = Qt;
4520 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4521 {
4522 spec = XCDR (spec);
4523 if (!CONSP (spec))
4524 return 0;
4525 form = XCAR (spec);
4526 spec = XCDR (spec);
4527 }
4528
4529 if (!NILP (form) && !EQ (form, Qt))
4530 {
4531 int count = SPECPDL_INDEX ();
4532 struct gcpro gcpro1;
4533
4534 /* Bind `object' to the object having the `display' property, a
4535 buffer or string. Bind `position' to the position in the
4536 object where the property was found, and `buffer-position'
4537 to the current position in the buffer. */
4538
4539 if (NILP (object))
4540 XSETBUFFER (object, current_buffer);
4541 specbind (Qobject, object);
4542 specbind (Qposition, make_number (CHARPOS (*position)));
4543 specbind (Qbuffer_position, make_number (bufpos));
4544 GCPRO1 (form);
4545 form = safe_eval (form);
4546 UNGCPRO;
4547 unbind_to (count, Qnil);
4548 }
4549
4550 if (NILP (form))
4551 return 0;
4552
4553 /* Handle `(height HEIGHT)' specifications. */
4554 if (CONSP (spec)
4555 && EQ (XCAR (spec), Qheight)
4556 && CONSP (XCDR (spec)))
4557 {
4558 if (it)
4559 {
4560 if (!FRAME_WINDOW_P (it->f))
4561 return 0;
4562
4563 it->font_height = XCAR (XCDR (spec));
4564 if (!NILP (it->font_height))
4565 {
4566 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4567 int new_height = -1;
4568
4569 if (CONSP (it->font_height)
4570 && (EQ (XCAR (it->font_height), Qplus)
4571 || EQ (XCAR (it->font_height), Qminus))
4572 && CONSP (XCDR (it->font_height))
4573 && INTEGERP (XCAR (XCDR (it->font_height))))
4574 {
4575 /* `(+ N)' or `(- N)' where N is an integer. */
4576 int steps = XINT (XCAR (XCDR (it->font_height)));
4577 if (EQ (XCAR (it->font_height), Qplus))
4578 steps = - steps;
4579 it->face_id = smaller_face (it->f, it->face_id, steps);
4580 }
4581 else if (FUNCTIONP (it->font_height))
4582 {
4583 /* Call function with current height as argument.
4584 Value is the new height. */
4585 Lisp_Object height;
4586 height = safe_call1 (it->font_height,
4587 face->lface[LFACE_HEIGHT_INDEX]);
4588 if (NUMBERP (height))
4589 new_height = XFLOATINT (height);
4590 }
4591 else if (NUMBERP (it->font_height))
4592 {
4593 /* Value is a multiple of the canonical char height. */
4594 struct face *f;
4595
4596 f = FACE_FROM_ID (it->f,
4597 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4598 new_height = (XFLOATINT (it->font_height)
4599 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4600 }
4601 else
4602 {
4603 /* Evaluate IT->font_height with `height' bound to the
4604 current specified height to get the new height. */
4605 int count = SPECPDL_INDEX ();
4606
4607 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4608 value = safe_eval (it->font_height);
4609 unbind_to (count, Qnil);
4610
4611 if (NUMBERP (value))
4612 new_height = XFLOATINT (value);
4613 }
4614
4615 if (new_height > 0)
4616 it->face_id = face_with_height (it->f, it->face_id, new_height);
4617 }
4618 }
4619
4620 return 0;
4621 }
4622
4623 /* Handle `(space-width WIDTH)'. */
4624 if (CONSP (spec)
4625 && EQ (XCAR (spec), Qspace_width)
4626 && CONSP (XCDR (spec)))
4627 {
4628 if (it)
4629 {
4630 if (!FRAME_WINDOW_P (it->f))
4631 return 0;
4632
4633 value = XCAR (XCDR (spec));
4634 if (NUMBERP (value) && XFLOATINT (value) > 0)
4635 it->space_width = value;
4636 }
4637
4638 return 0;
4639 }
4640
4641 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4642 if (CONSP (spec)
4643 && EQ (XCAR (spec), Qslice))
4644 {
4645 Lisp_Object tem;
4646
4647 if (it)
4648 {
4649 if (!FRAME_WINDOW_P (it->f))
4650 return 0;
4651
4652 if (tem = XCDR (spec), CONSP (tem))
4653 {
4654 it->slice.x = XCAR (tem);
4655 if (tem = XCDR (tem), CONSP (tem))
4656 {
4657 it->slice.y = XCAR (tem);
4658 if (tem = XCDR (tem), CONSP (tem))
4659 {
4660 it->slice.width = XCAR (tem);
4661 if (tem = XCDR (tem), CONSP (tem))
4662 it->slice.height = XCAR (tem);
4663 }
4664 }
4665 }
4666 }
4667
4668 return 0;
4669 }
4670
4671 /* Handle `(raise FACTOR)'. */
4672 if (CONSP (spec)
4673 && EQ (XCAR (spec), Qraise)
4674 && CONSP (XCDR (spec)))
4675 {
4676 if (it)
4677 {
4678 if (!FRAME_WINDOW_P (it->f))
4679 return 0;
4680
4681 #ifdef HAVE_WINDOW_SYSTEM
4682 value = XCAR (XCDR (spec));
4683 if (NUMBERP (value))
4684 {
4685 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4686 it->voffset = - (XFLOATINT (value)
4687 * (FONT_HEIGHT (face->font)));
4688 }
4689 #endif /* HAVE_WINDOW_SYSTEM */
4690 }
4691
4692 return 0;
4693 }
4694
4695 /* Don't handle the other kinds of display specifications
4696 inside a string that we got from a `display' property. */
4697 if (it && it->string_from_display_prop_p)
4698 return 0;
4699
4700 /* Characters having this form of property are not displayed, so
4701 we have to find the end of the property. */
4702 if (it)
4703 {
4704 start_pos = *position;
4705 *position = display_prop_end (it, object, start_pos);
4706 }
4707 value = Qnil;
4708
4709 /* Stop the scan at that end position--we assume that all
4710 text properties change there. */
4711 if (it)
4712 it->stop_charpos = position->charpos;
4713
4714 /* Handle `(left-fringe BITMAP [FACE])'
4715 and `(right-fringe BITMAP [FACE])'. */
4716 if (CONSP (spec)
4717 && (EQ (XCAR (spec), Qleft_fringe)
4718 || EQ (XCAR (spec), Qright_fringe))
4719 && CONSP (XCDR (spec)))
4720 {
4721 int fringe_bitmap;
4722
4723 if (it)
4724 {
4725 if (!FRAME_WINDOW_P (it->f))
4726 /* If we return here, POSITION has been advanced
4727 across the text with this property. */
4728 {
4729 /* Synchronize the bidi iterator with POSITION. This is
4730 needed because we are not going to push the iterator
4731 on behalf of this display property, so there will be
4732 no pop_it call to do this synchronization for us. */
4733 if (it->bidi_p)
4734 {
4735 it->position = *position;
4736 iterate_out_of_display_property (it);
4737 *position = it->position;
4738 }
4739 return 1;
4740 }
4741 }
4742 else if (!frame_window_p)
4743 return 1;
4744
4745 #ifdef HAVE_WINDOW_SYSTEM
4746 value = XCAR (XCDR (spec));
4747 if (!SYMBOLP (value)
4748 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4749 /* If we return here, POSITION has been advanced
4750 across the text with this property. */
4751 {
4752 if (it && it->bidi_p)
4753 {
4754 it->position = *position;
4755 iterate_out_of_display_property (it);
4756 *position = it->position;
4757 }
4758 return 1;
4759 }
4760
4761 if (it)
4762 {
4763 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4764
4765 if (CONSP (XCDR (XCDR (spec))))
4766 {
4767 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4768 int face_id2 = lookup_derived_face (it->f, face_name,
4769 FRINGE_FACE_ID, 0);
4770 if (face_id2 >= 0)
4771 face_id = face_id2;
4772 }
4773
4774 /* Save current settings of IT so that we can restore them
4775 when we are finished with the glyph property value. */
4776 push_it (it, position);
4777
4778 it->area = TEXT_AREA;
4779 it->what = IT_IMAGE;
4780 it->image_id = -1; /* no image */
4781 it->position = start_pos;
4782 it->object = NILP (object) ? it->w->buffer : object;
4783 it->method = GET_FROM_IMAGE;
4784 it->from_overlay = Qnil;
4785 it->face_id = face_id;
4786 it->from_disp_prop_p = 1;
4787
4788 /* Say that we haven't consumed the characters with
4789 `display' property yet. The call to pop_it in
4790 set_iterator_to_next will clean this up. */
4791 *position = start_pos;
4792
4793 if (EQ (XCAR (spec), Qleft_fringe))
4794 {
4795 it->left_user_fringe_bitmap = fringe_bitmap;
4796 it->left_user_fringe_face_id = face_id;
4797 }
4798 else
4799 {
4800 it->right_user_fringe_bitmap = fringe_bitmap;
4801 it->right_user_fringe_face_id = face_id;
4802 }
4803 }
4804 #endif /* HAVE_WINDOW_SYSTEM */
4805 return 1;
4806 }
4807
4808 /* Prepare to handle `((margin left-margin) ...)',
4809 `((margin right-margin) ...)' and `((margin nil) ...)'
4810 prefixes for display specifications. */
4811 location = Qunbound;
4812 if (CONSP (spec) && CONSP (XCAR (spec)))
4813 {
4814 Lisp_Object tem;
4815
4816 value = XCDR (spec);
4817 if (CONSP (value))
4818 value = XCAR (value);
4819
4820 tem = XCAR (spec);
4821 if (EQ (XCAR (tem), Qmargin)
4822 && (tem = XCDR (tem),
4823 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4824 (NILP (tem)
4825 || EQ (tem, Qleft_margin)
4826 || EQ (tem, Qright_margin))))
4827 location = tem;
4828 }
4829
4830 if (EQ (location, Qunbound))
4831 {
4832 location = Qnil;
4833 value = spec;
4834 }
4835
4836 /* After this point, VALUE is the property after any
4837 margin prefix has been stripped. It must be a string,
4838 an image specification, or `(space ...)'.
4839
4840 LOCATION specifies where to display: `left-margin',
4841 `right-margin' or nil. */
4842
4843 valid_p = (STRINGP (value)
4844 #ifdef HAVE_WINDOW_SYSTEM
4845 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4846 && valid_image_p (value))
4847 #endif /* not HAVE_WINDOW_SYSTEM */
4848 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4849
4850 if (valid_p && !display_replaced_p)
4851 {
4852 int retval = 1;
4853
4854 if (!it)
4855 {
4856 /* Callers need to know whether the display spec is any kind
4857 of `(space ...)' spec that is about to affect text-area
4858 display. */
4859 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4860 retval = 2;
4861 return retval;
4862 }
4863
4864 /* Save current settings of IT so that we can restore them
4865 when we are finished with the glyph property value. */
4866 push_it (it, position);
4867 it->from_overlay = overlay;
4868 it->from_disp_prop_p = 1;
4869
4870 if (NILP (location))
4871 it->area = TEXT_AREA;
4872 else if (EQ (location, Qleft_margin))
4873 it->area = LEFT_MARGIN_AREA;
4874 else
4875 it->area = RIGHT_MARGIN_AREA;
4876
4877 if (STRINGP (value))
4878 {
4879 it->string = value;
4880 it->multibyte_p = STRING_MULTIBYTE (it->string);
4881 it->current.overlay_string_index = -1;
4882 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4883 it->end_charpos = it->string_nchars = SCHARS (it->string);
4884 it->method = GET_FROM_STRING;
4885 it->stop_charpos = 0;
4886 it->prev_stop = 0;
4887 it->base_level_stop = 0;
4888 it->string_from_display_prop_p = 1;
4889 /* Say that we haven't consumed the characters with
4890 `display' property yet. The call to pop_it in
4891 set_iterator_to_next will clean this up. */
4892 if (BUFFERP (object))
4893 *position = start_pos;
4894
4895 /* Force paragraph direction to be that of the parent
4896 object. If the parent object's paragraph direction is
4897 not yet determined, default to L2R. */
4898 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4899 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4900 else
4901 it->paragraph_embedding = L2R;
4902
4903 /* Set up the bidi iterator for this display string. */
4904 if (it->bidi_p)
4905 {
4906 it->bidi_it.string.lstring = it->string;
4907 it->bidi_it.string.s = NULL;
4908 it->bidi_it.string.schars = it->end_charpos;
4909 it->bidi_it.string.bufpos = bufpos;
4910 it->bidi_it.string.from_disp_str = 1;
4911 it->bidi_it.string.unibyte = !it->multibyte_p;
4912 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4913 }
4914 }
4915 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4916 {
4917 it->method = GET_FROM_STRETCH;
4918 it->object = value;
4919 *position = it->position = start_pos;
4920 retval = 1 + (it->area == TEXT_AREA);
4921 }
4922 #ifdef HAVE_WINDOW_SYSTEM
4923 else
4924 {
4925 it->what = IT_IMAGE;
4926 it->image_id = lookup_image (it->f, value);
4927 it->position = start_pos;
4928 it->object = NILP (object) ? it->w->buffer : object;
4929 it->method = GET_FROM_IMAGE;
4930
4931 /* Say that we haven't consumed the characters with
4932 `display' property yet. The call to pop_it in
4933 set_iterator_to_next will clean this up. */
4934 *position = start_pos;
4935 }
4936 #endif /* HAVE_WINDOW_SYSTEM */
4937
4938 return retval;
4939 }
4940
4941 /* Invalid property or property not supported. Restore
4942 POSITION to what it was before. */
4943 *position = start_pos;
4944 return 0;
4945 }
4946
4947 /* Check if PROP is a display property value whose text should be
4948 treated as intangible. OVERLAY is the overlay from which PROP
4949 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4950 specify the buffer position covered by PROP. */
4951
4952 int
4953 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4954 EMACS_INT charpos, EMACS_INT bytepos)
4955 {
4956 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4957 struct text_pos position;
4958
4959 SET_TEXT_POS (position, charpos, bytepos);
4960 return handle_display_spec (NULL, prop, Qnil, overlay,
4961 &position, charpos, frame_window_p);
4962 }
4963
4964
4965 /* Return 1 if PROP is a display sub-property value containing STRING.
4966
4967 Implementation note: this and the following function are really
4968 special cases of handle_display_spec and
4969 handle_single_display_spec, and should ideally use the same code.
4970 Until they do, these two pairs must be consistent and must be
4971 modified in sync. */
4972
4973 static int
4974 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4975 {
4976 if (EQ (string, prop))
4977 return 1;
4978
4979 /* Skip over `when FORM'. */
4980 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4981 {
4982 prop = XCDR (prop);
4983 if (!CONSP (prop))
4984 return 0;
4985 /* Actually, the condition following `when' should be eval'ed,
4986 like handle_single_display_spec does, and we should return
4987 zero if it evaluates to nil. However, this function is
4988 called only when the buffer was already displayed and some
4989 glyph in the glyph matrix was found to come from a display
4990 string. Therefore, the condition was already evaluated, and
4991 the result was non-nil, otherwise the display string wouldn't
4992 have been displayed and we would have never been called for
4993 this property. Thus, we can skip the evaluation and assume
4994 its result is non-nil. */
4995 prop = XCDR (prop);
4996 }
4997
4998 if (CONSP (prop))
4999 /* Skip over `margin LOCATION'. */
5000 if (EQ (XCAR (prop), Qmargin))
5001 {
5002 prop = XCDR (prop);
5003 if (!CONSP (prop))
5004 return 0;
5005
5006 prop = XCDR (prop);
5007 if (!CONSP (prop))
5008 return 0;
5009 }
5010
5011 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5012 }
5013
5014
5015 /* Return 1 if STRING appears in the `display' property PROP. */
5016
5017 static int
5018 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5019 {
5020 if (CONSP (prop)
5021 && !EQ (XCAR (prop), Qwhen)
5022 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5023 {
5024 /* A list of sub-properties. */
5025 while (CONSP (prop))
5026 {
5027 if (single_display_spec_string_p (XCAR (prop), string))
5028 return 1;
5029 prop = XCDR (prop);
5030 }
5031 }
5032 else if (VECTORP (prop))
5033 {
5034 /* A vector of sub-properties. */
5035 int i;
5036 for (i = 0; i < ASIZE (prop); ++i)
5037 if (single_display_spec_string_p (AREF (prop, i), string))
5038 return 1;
5039 }
5040 else
5041 return single_display_spec_string_p (prop, string);
5042
5043 return 0;
5044 }
5045
5046 /* Look for STRING in overlays and text properties in the current
5047 buffer, between character positions FROM and TO (excluding TO).
5048 BACK_P non-zero means look back (in this case, TO is supposed to be
5049 less than FROM).
5050 Value is the first character position where STRING was found, or
5051 zero if it wasn't found before hitting TO.
5052
5053 This function may only use code that doesn't eval because it is
5054 called asynchronously from note_mouse_highlight. */
5055
5056 static EMACS_INT
5057 string_buffer_position_lim (Lisp_Object string,
5058 EMACS_INT from, EMACS_INT to, int back_p)
5059 {
5060 Lisp_Object limit, prop, pos;
5061 int found = 0;
5062
5063 pos = make_number (max (from, BEGV));
5064
5065 if (!back_p) /* looking forward */
5066 {
5067 limit = make_number (min (to, ZV));
5068 while (!found && !EQ (pos, limit))
5069 {
5070 prop = Fget_char_property (pos, Qdisplay, Qnil);
5071 if (!NILP (prop) && display_prop_string_p (prop, string))
5072 found = 1;
5073 else
5074 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5075 limit);
5076 }
5077 }
5078 else /* looking back */
5079 {
5080 limit = make_number (max (to, BEGV));
5081 while (!found && !EQ (pos, limit))
5082 {
5083 prop = Fget_char_property (pos, Qdisplay, Qnil);
5084 if (!NILP (prop) && display_prop_string_p (prop, string))
5085 found = 1;
5086 else
5087 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5088 limit);
5089 }
5090 }
5091
5092 return found ? XINT (pos) : 0;
5093 }
5094
5095 /* Determine which buffer position in current buffer STRING comes from.
5096 AROUND_CHARPOS is an approximate position where it could come from.
5097 Value is the buffer position or 0 if it couldn't be determined.
5098
5099 This function is necessary because we don't record buffer positions
5100 in glyphs generated from strings (to keep struct glyph small).
5101 This function may only use code that doesn't eval because it is
5102 called asynchronously from note_mouse_highlight. */
5103
5104 static EMACS_INT
5105 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5106 {
5107 const int MAX_DISTANCE = 1000;
5108 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5109 around_charpos + MAX_DISTANCE,
5110 0);
5111
5112 if (!found)
5113 found = string_buffer_position_lim (string, around_charpos,
5114 around_charpos - MAX_DISTANCE, 1);
5115 return found;
5116 }
5117
5118
5119 \f
5120 /***********************************************************************
5121 `composition' property
5122 ***********************************************************************/
5123
5124 /* Set up iterator IT from `composition' property at its current
5125 position. Called from handle_stop. */
5126
5127 static enum prop_handled
5128 handle_composition_prop (struct it *it)
5129 {
5130 Lisp_Object prop, string;
5131 EMACS_INT pos, pos_byte, start, end;
5132
5133 if (STRINGP (it->string))
5134 {
5135 unsigned char *s;
5136
5137 pos = IT_STRING_CHARPOS (*it);
5138 pos_byte = IT_STRING_BYTEPOS (*it);
5139 string = it->string;
5140 s = SDATA (string) + pos_byte;
5141 it->c = STRING_CHAR (s);
5142 }
5143 else
5144 {
5145 pos = IT_CHARPOS (*it);
5146 pos_byte = IT_BYTEPOS (*it);
5147 string = Qnil;
5148 it->c = FETCH_CHAR (pos_byte);
5149 }
5150
5151 /* If there's a valid composition and point is not inside of the
5152 composition (in the case that the composition is from the current
5153 buffer), draw a glyph composed from the composition components. */
5154 if (find_composition (pos, -1, &start, &end, &prop, string)
5155 && COMPOSITION_VALID_P (start, end, prop)
5156 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5157 {
5158 if (start < pos)
5159 /* As we can't handle this situation (perhaps font-lock added
5160 a new composition), we just return here hoping that next
5161 redisplay will detect this composition much earlier. */
5162 return HANDLED_NORMALLY;
5163 if (start != pos)
5164 {
5165 if (STRINGP (it->string))
5166 pos_byte = string_char_to_byte (it->string, start);
5167 else
5168 pos_byte = CHAR_TO_BYTE (start);
5169 }
5170 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5171 prop, string);
5172
5173 if (it->cmp_it.id >= 0)
5174 {
5175 it->cmp_it.ch = -1;
5176 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5177 it->cmp_it.nglyphs = -1;
5178 }
5179 }
5180
5181 return HANDLED_NORMALLY;
5182 }
5183
5184
5185 \f
5186 /***********************************************************************
5187 Overlay strings
5188 ***********************************************************************/
5189
5190 /* The following structure is used to record overlay strings for
5191 later sorting in load_overlay_strings. */
5192
5193 struct overlay_entry
5194 {
5195 Lisp_Object overlay;
5196 Lisp_Object string;
5197 int priority;
5198 int after_string_p;
5199 };
5200
5201
5202 /* Set up iterator IT from overlay strings at its current position.
5203 Called from handle_stop. */
5204
5205 static enum prop_handled
5206 handle_overlay_change (struct it *it)
5207 {
5208 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5209 return HANDLED_RECOMPUTE_PROPS;
5210 else
5211 return HANDLED_NORMALLY;
5212 }
5213
5214
5215 /* Set up the next overlay string for delivery by IT, if there is an
5216 overlay string to deliver. Called by set_iterator_to_next when the
5217 end of the current overlay string is reached. If there are more
5218 overlay strings to display, IT->string and
5219 IT->current.overlay_string_index are set appropriately here.
5220 Otherwise IT->string is set to nil. */
5221
5222 static void
5223 next_overlay_string (struct it *it)
5224 {
5225 ++it->current.overlay_string_index;
5226 if (it->current.overlay_string_index == it->n_overlay_strings)
5227 {
5228 /* No more overlay strings. Restore IT's settings to what
5229 they were before overlay strings were processed, and
5230 continue to deliver from current_buffer. */
5231
5232 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5233 pop_it (it);
5234 xassert (it->sp > 0
5235 || (NILP (it->string)
5236 && it->method == GET_FROM_BUFFER
5237 && it->stop_charpos >= BEGV
5238 && it->stop_charpos <= it->end_charpos));
5239 it->current.overlay_string_index = -1;
5240 it->n_overlay_strings = 0;
5241 it->overlay_strings_charpos = -1;
5242 /* If there's an empty display string on the stack, pop the
5243 stack, to resync the bidi iterator with IT's position. Such
5244 empty strings are pushed onto the stack in
5245 get_overlay_strings_1. */
5246 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5247 pop_it (it);
5248
5249 /* If we're at the end of the buffer, record that we have
5250 processed the overlay strings there already, so that
5251 next_element_from_buffer doesn't try it again. */
5252 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5253 it->overlay_strings_at_end_processed_p = 1;
5254 }
5255 else
5256 {
5257 /* There are more overlay strings to process. If
5258 IT->current.overlay_string_index has advanced to a position
5259 where we must load IT->overlay_strings with more strings, do
5260 it. We must load at the IT->overlay_strings_charpos where
5261 IT->n_overlay_strings was originally computed; when invisible
5262 text is present, this might not be IT_CHARPOS (Bug#7016). */
5263 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5264
5265 if (it->current.overlay_string_index && i == 0)
5266 load_overlay_strings (it, it->overlay_strings_charpos);
5267
5268 /* Initialize IT to deliver display elements from the overlay
5269 string. */
5270 it->string = it->overlay_strings[i];
5271 it->multibyte_p = STRING_MULTIBYTE (it->string);
5272 SET_TEXT_POS (it->current.string_pos, 0, 0);
5273 it->method = GET_FROM_STRING;
5274 it->stop_charpos = 0;
5275 if (it->cmp_it.stop_pos >= 0)
5276 it->cmp_it.stop_pos = 0;
5277 it->prev_stop = 0;
5278 it->base_level_stop = 0;
5279
5280 /* Set up the bidi iterator for this overlay string. */
5281 if (it->bidi_p)
5282 {
5283 it->bidi_it.string.lstring = it->string;
5284 it->bidi_it.string.s = NULL;
5285 it->bidi_it.string.schars = SCHARS (it->string);
5286 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5287 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5288 it->bidi_it.string.unibyte = !it->multibyte_p;
5289 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5290 }
5291 }
5292
5293 CHECK_IT (it);
5294 }
5295
5296
5297 /* Compare two overlay_entry structures E1 and E2. Used as a
5298 comparison function for qsort in load_overlay_strings. Overlay
5299 strings for the same position are sorted so that
5300
5301 1. All after-strings come in front of before-strings, except
5302 when they come from the same overlay.
5303
5304 2. Within after-strings, strings are sorted so that overlay strings
5305 from overlays with higher priorities come first.
5306
5307 2. Within before-strings, strings are sorted so that overlay
5308 strings from overlays with higher priorities come last.
5309
5310 Value is analogous to strcmp. */
5311
5312
5313 static int
5314 compare_overlay_entries (const void *e1, const void *e2)
5315 {
5316 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5317 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5318 int result;
5319
5320 if (entry1->after_string_p != entry2->after_string_p)
5321 {
5322 /* Let after-strings appear in front of before-strings if
5323 they come from different overlays. */
5324 if (EQ (entry1->overlay, entry2->overlay))
5325 result = entry1->after_string_p ? 1 : -1;
5326 else
5327 result = entry1->after_string_p ? -1 : 1;
5328 }
5329 else if (entry1->after_string_p)
5330 /* After-strings sorted in order of decreasing priority. */
5331 result = entry2->priority - entry1->priority;
5332 else
5333 /* Before-strings sorted in order of increasing priority. */
5334 result = entry1->priority - entry2->priority;
5335
5336 return result;
5337 }
5338
5339
5340 /* Load the vector IT->overlay_strings with overlay strings from IT's
5341 current buffer position, or from CHARPOS if that is > 0. Set
5342 IT->n_overlays to the total number of overlay strings found.
5343
5344 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5345 a time. On entry into load_overlay_strings,
5346 IT->current.overlay_string_index gives the number of overlay
5347 strings that have already been loaded by previous calls to this
5348 function.
5349
5350 IT->add_overlay_start contains an additional overlay start
5351 position to consider for taking overlay strings from, if non-zero.
5352 This position comes into play when the overlay has an `invisible'
5353 property, and both before and after-strings. When we've skipped to
5354 the end of the overlay, because of its `invisible' property, we
5355 nevertheless want its before-string to appear.
5356 IT->add_overlay_start will contain the overlay start position
5357 in this case.
5358
5359 Overlay strings are sorted so that after-string strings come in
5360 front of before-string strings. Within before and after-strings,
5361 strings are sorted by overlay priority. See also function
5362 compare_overlay_entries. */
5363
5364 static void
5365 load_overlay_strings (struct it *it, EMACS_INT charpos)
5366 {
5367 Lisp_Object overlay, window, str, invisible;
5368 struct Lisp_Overlay *ov;
5369 EMACS_INT start, end;
5370 int size = 20;
5371 int n = 0, i, j, invis_p;
5372 struct overlay_entry *entries
5373 = (struct overlay_entry *) alloca (size * sizeof *entries);
5374
5375 if (charpos <= 0)
5376 charpos = IT_CHARPOS (*it);
5377
5378 /* Append the overlay string STRING of overlay OVERLAY to vector
5379 `entries' which has size `size' and currently contains `n'
5380 elements. AFTER_P non-zero means STRING is an after-string of
5381 OVERLAY. */
5382 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5383 do \
5384 { \
5385 Lisp_Object priority; \
5386 \
5387 if (n == size) \
5388 { \
5389 int new_size = 2 * size; \
5390 struct overlay_entry *old = entries; \
5391 entries = \
5392 (struct overlay_entry *) alloca (new_size \
5393 * sizeof *entries); \
5394 memcpy (entries, old, size * sizeof *entries); \
5395 size = new_size; \
5396 } \
5397 \
5398 entries[n].string = (STRING); \
5399 entries[n].overlay = (OVERLAY); \
5400 priority = Foverlay_get ((OVERLAY), Qpriority); \
5401 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5402 entries[n].after_string_p = (AFTER_P); \
5403 ++n; \
5404 } \
5405 while (0)
5406
5407 /* Process overlay before the overlay center. */
5408 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5409 {
5410 XSETMISC (overlay, ov);
5411 xassert (OVERLAYP (overlay));
5412 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5413 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5414
5415 if (end < charpos)
5416 break;
5417
5418 /* Skip this overlay if it doesn't start or end at IT's current
5419 position. */
5420 if (end != charpos && start != charpos)
5421 continue;
5422
5423 /* Skip this overlay if it doesn't apply to IT->w. */
5424 window = Foverlay_get (overlay, Qwindow);
5425 if (WINDOWP (window) && XWINDOW (window) != it->w)
5426 continue;
5427
5428 /* If the text ``under'' the overlay is invisible, both before-
5429 and after-strings from this overlay are visible; start and
5430 end position are indistinguishable. */
5431 invisible = Foverlay_get (overlay, Qinvisible);
5432 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5433
5434 /* If overlay has a non-empty before-string, record it. */
5435 if ((start == charpos || (end == charpos && invis_p))
5436 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5437 && SCHARS (str))
5438 RECORD_OVERLAY_STRING (overlay, str, 0);
5439
5440 /* If overlay has a non-empty after-string, record it. */
5441 if ((end == charpos || (start == charpos && invis_p))
5442 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5443 && SCHARS (str))
5444 RECORD_OVERLAY_STRING (overlay, str, 1);
5445 }
5446
5447 /* Process overlays after the overlay center. */
5448 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5449 {
5450 XSETMISC (overlay, ov);
5451 xassert (OVERLAYP (overlay));
5452 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5453 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5454
5455 if (start > charpos)
5456 break;
5457
5458 /* Skip this overlay if it doesn't start or end at IT's current
5459 position. */
5460 if (end != charpos && start != charpos)
5461 continue;
5462
5463 /* Skip this overlay if it doesn't apply to IT->w. */
5464 window = Foverlay_get (overlay, Qwindow);
5465 if (WINDOWP (window) && XWINDOW (window) != it->w)
5466 continue;
5467
5468 /* If the text ``under'' the overlay is invisible, it has a zero
5469 dimension, and both before- and after-strings apply. */
5470 invisible = Foverlay_get (overlay, Qinvisible);
5471 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5472
5473 /* If overlay has a non-empty before-string, record it. */
5474 if ((start == charpos || (end == charpos && invis_p))
5475 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5476 && SCHARS (str))
5477 RECORD_OVERLAY_STRING (overlay, str, 0);
5478
5479 /* If overlay has a non-empty after-string, record it. */
5480 if ((end == charpos || (start == charpos && invis_p))
5481 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5482 && SCHARS (str))
5483 RECORD_OVERLAY_STRING (overlay, str, 1);
5484 }
5485
5486 #undef RECORD_OVERLAY_STRING
5487
5488 /* Sort entries. */
5489 if (n > 1)
5490 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5491
5492 /* Record number of overlay strings, and where we computed it. */
5493 it->n_overlay_strings = n;
5494 it->overlay_strings_charpos = charpos;
5495
5496 /* IT->current.overlay_string_index is the number of overlay strings
5497 that have already been consumed by IT. Copy some of the
5498 remaining overlay strings to IT->overlay_strings. */
5499 i = 0;
5500 j = it->current.overlay_string_index;
5501 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5502 {
5503 it->overlay_strings[i] = entries[j].string;
5504 it->string_overlays[i++] = entries[j++].overlay;
5505 }
5506
5507 CHECK_IT (it);
5508 }
5509
5510
5511 /* Get the first chunk of overlay strings at IT's current buffer
5512 position, or at CHARPOS if that is > 0. Value is non-zero if at
5513 least one overlay string was found. */
5514
5515 static int
5516 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5517 {
5518 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5519 process. This fills IT->overlay_strings with strings, and sets
5520 IT->n_overlay_strings to the total number of strings to process.
5521 IT->pos.overlay_string_index has to be set temporarily to zero
5522 because load_overlay_strings needs this; it must be set to -1
5523 when no overlay strings are found because a zero value would
5524 indicate a position in the first overlay string. */
5525 it->current.overlay_string_index = 0;
5526 load_overlay_strings (it, charpos);
5527
5528 /* If we found overlay strings, set up IT to deliver display
5529 elements from the first one. Otherwise set up IT to deliver
5530 from current_buffer. */
5531 if (it->n_overlay_strings)
5532 {
5533 /* Make sure we know settings in current_buffer, so that we can
5534 restore meaningful values when we're done with the overlay
5535 strings. */
5536 if (compute_stop_p)
5537 compute_stop_pos (it);
5538 xassert (it->face_id >= 0);
5539
5540 /* Save IT's settings. They are restored after all overlay
5541 strings have been processed. */
5542 xassert (!compute_stop_p || it->sp == 0);
5543
5544 /* When called from handle_stop, there might be an empty display
5545 string loaded. In that case, don't bother saving it. But
5546 don't use this optimization with the bidi iterator, since we
5547 need the corresponding pop_it call to resync the bidi
5548 iterator's position with IT's position, after we are done
5549 with the overlay strings. (The corresponding call to pop_it
5550 in case of an empty display string is in
5551 next_overlay_string.) */
5552 if (!(!it->bidi_p
5553 && STRINGP (it->string) && !SCHARS (it->string)))
5554 push_it (it, NULL);
5555
5556 /* Set up IT to deliver display elements from the first overlay
5557 string. */
5558 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5559 it->string = it->overlay_strings[0];
5560 it->from_overlay = Qnil;
5561 it->stop_charpos = 0;
5562 xassert (STRINGP (it->string));
5563 it->end_charpos = SCHARS (it->string);
5564 it->prev_stop = 0;
5565 it->base_level_stop = 0;
5566 it->multibyte_p = STRING_MULTIBYTE (it->string);
5567 it->method = GET_FROM_STRING;
5568 it->from_disp_prop_p = 0;
5569
5570 /* Force paragraph direction to be that of the parent
5571 buffer. */
5572 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5573 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5574 else
5575 it->paragraph_embedding = L2R;
5576
5577 /* Set up the bidi iterator for this overlay string. */
5578 if (it->bidi_p)
5579 {
5580 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5581
5582 it->bidi_it.string.lstring = it->string;
5583 it->bidi_it.string.s = NULL;
5584 it->bidi_it.string.schars = SCHARS (it->string);
5585 it->bidi_it.string.bufpos = pos;
5586 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5587 it->bidi_it.string.unibyte = !it->multibyte_p;
5588 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5589 }
5590 return 1;
5591 }
5592
5593 it->current.overlay_string_index = -1;
5594 return 0;
5595 }
5596
5597 static int
5598 get_overlay_strings (struct it *it, EMACS_INT charpos)
5599 {
5600 it->string = Qnil;
5601 it->method = GET_FROM_BUFFER;
5602
5603 (void) get_overlay_strings_1 (it, charpos, 1);
5604
5605 CHECK_IT (it);
5606
5607 /* Value is non-zero if we found at least one overlay string. */
5608 return STRINGP (it->string);
5609 }
5610
5611
5612 \f
5613 /***********************************************************************
5614 Saving and restoring state
5615 ***********************************************************************/
5616
5617 /* Save current settings of IT on IT->stack. Called, for example,
5618 before setting up IT for an overlay string, to be able to restore
5619 IT's settings to what they were after the overlay string has been
5620 processed. If POSITION is non-NULL, it is the position to save on
5621 the stack instead of IT->position. */
5622
5623 static void
5624 push_it (struct it *it, struct text_pos *position)
5625 {
5626 struct iterator_stack_entry *p;
5627
5628 xassert (it->sp < IT_STACK_SIZE);
5629 p = it->stack + it->sp;
5630
5631 p->stop_charpos = it->stop_charpos;
5632 p->prev_stop = it->prev_stop;
5633 p->base_level_stop = it->base_level_stop;
5634 p->cmp_it = it->cmp_it;
5635 xassert (it->face_id >= 0);
5636 p->face_id = it->face_id;
5637 p->string = it->string;
5638 p->method = it->method;
5639 p->from_overlay = it->from_overlay;
5640 switch (p->method)
5641 {
5642 case GET_FROM_IMAGE:
5643 p->u.image.object = it->object;
5644 p->u.image.image_id = it->image_id;
5645 p->u.image.slice = it->slice;
5646 break;
5647 case GET_FROM_STRETCH:
5648 p->u.stretch.object = it->object;
5649 break;
5650 }
5651 p->position = position ? *position : it->position;
5652 p->current = it->current;
5653 p->end_charpos = it->end_charpos;
5654 p->string_nchars = it->string_nchars;
5655 p->area = it->area;
5656 p->multibyte_p = it->multibyte_p;
5657 p->avoid_cursor_p = it->avoid_cursor_p;
5658 p->space_width = it->space_width;
5659 p->font_height = it->font_height;
5660 p->voffset = it->voffset;
5661 p->string_from_display_prop_p = it->string_from_display_prop_p;
5662 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5663 p->display_ellipsis_p = 0;
5664 p->line_wrap = it->line_wrap;
5665 p->bidi_p = it->bidi_p;
5666 p->paragraph_embedding = it->paragraph_embedding;
5667 p->from_disp_prop_p = it->from_disp_prop_p;
5668 ++it->sp;
5669
5670 /* Save the state of the bidi iterator as well. */
5671 if (it->bidi_p)
5672 bidi_push_it (&it->bidi_it);
5673 }
5674
5675 static void
5676 iterate_out_of_display_property (struct it *it)
5677 {
5678 int buffer_p = !STRINGP (it->string);
5679 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5680 EMACS_INT bob = (buffer_p ? BEGV : 0);
5681
5682 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5683
5684 /* Maybe initialize paragraph direction. If we are at the beginning
5685 of a new paragraph, next_element_from_buffer may not have a
5686 chance to do that. */
5687 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5688 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5689 /* prev_stop can be zero, so check against BEGV as well. */
5690 while (it->bidi_it.charpos >= bob
5691 && it->prev_stop <= it->bidi_it.charpos
5692 && it->bidi_it.charpos < CHARPOS (it->position)
5693 && it->bidi_it.charpos < eob)
5694 bidi_move_to_visually_next (&it->bidi_it);
5695 /* Record the stop_pos we just crossed, for when we cross it
5696 back, maybe. */
5697 if (it->bidi_it.charpos > CHARPOS (it->position))
5698 it->prev_stop = CHARPOS (it->position);
5699 /* If we ended up not where pop_it put us, resync IT's
5700 positional members with the bidi iterator. */
5701 if (it->bidi_it.charpos != CHARPOS (it->position))
5702 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5703 if (buffer_p)
5704 it->current.pos = it->position;
5705 else
5706 it->current.string_pos = it->position;
5707 }
5708
5709 /* Restore IT's settings from IT->stack. Called, for example, when no
5710 more overlay strings must be processed, and we return to delivering
5711 display elements from a buffer, or when the end of a string from a
5712 `display' property is reached and we return to delivering display
5713 elements from an overlay string, or from a buffer. */
5714
5715 static void
5716 pop_it (struct it *it)
5717 {
5718 struct iterator_stack_entry *p;
5719 int from_display_prop = it->from_disp_prop_p;
5720
5721 xassert (it->sp > 0);
5722 --it->sp;
5723 p = it->stack + it->sp;
5724 it->stop_charpos = p->stop_charpos;
5725 it->prev_stop = p->prev_stop;
5726 it->base_level_stop = p->base_level_stop;
5727 it->cmp_it = p->cmp_it;
5728 it->face_id = p->face_id;
5729 it->current = p->current;
5730 it->position = p->position;
5731 it->string = p->string;
5732 it->from_overlay = p->from_overlay;
5733 if (NILP (it->string))
5734 SET_TEXT_POS (it->current.string_pos, -1, -1);
5735 it->method = p->method;
5736 switch (it->method)
5737 {
5738 case GET_FROM_IMAGE:
5739 it->image_id = p->u.image.image_id;
5740 it->object = p->u.image.object;
5741 it->slice = p->u.image.slice;
5742 break;
5743 case GET_FROM_STRETCH:
5744 it->object = p->u.stretch.object;
5745 break;
5746 case GET_FROM_BUFFER:
5747 it->object = it->w->buffer;
5748 break;
5749 case GET_FROM_STRING:
5750 it->object = it->string;
5751 break;
5752 case GET_FROM_DISPLAY_VECTOR:
5753 if (it->s)
5754 it->method = GET_FROM_C_STRING;
5755 else if (STRINGP (it->string))
5756 it->method = GET_FROM_STRING;
5757 else
5758 {
5759 it->method = GET_FROM_BUFFER;
5760 it->object = it->w->buffer;
5761 }
5762 }
5763 it->end_charpos = p->end_charpos;
5764 it->string_nchars = p->string_nchars;
5765 it->area = p->area;
5766 it->multibyte_p = p->multibyte_p;
5767 it->avoid_cursor_p = p->avoid_cursor_p;
5768 it->space_width = p->space_width;
5769 it->font_height = p->font_height;
5770 it->voffset = p->voffset;
5771 it->string_from_display_prop_p = p->string_from_display_prop_p;
5772 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5773 it->line_wrap = p->line_wrap;
5774 it->bidi_p = p->bidi_p;
5775 it->paragraph_embedding = p->paragraph_embedding;
5776 it->from_disp_prop_p = p->from_disp_prop_p;
5777 if (it->bidi_p)
5778 {
5779 bidi_pop_it (&it->bidi_it);
5780 /* Bidi-iterate until we get out of the portion of text, if any,
5781 covered by a `display' text property or by an overlay with
5782 `display' property. (We cannot just jump there, because the
5783 internal coherency of the bidi iterator state can not be
5784 preserved across such jumps.) We also must determine the
5785 paragraph base direction if the overlay we just processed is
5786 at the beginning of a new paragraph. */
5787 if (from_display_prop
5788 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5789 iterate_out_of_display_property (it);
5790
5791 xassert ((BUFFERP (it->object)
5792 && IT_CHARPOS (*it) == it->bidi_it.charpos
5793 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5794 || (STRINGP (it->object)
5795 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5796 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5797 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5798 }
5799 }
5800
5801
5802 \f
5803 /***********************************************************************
5804 Moving over lines
5805 ***********************************************************************/
5806
5807 /* Set IT's current position to the previous line start. */
5808
5809 static void
5810 back_to_previous_line_start (struct it *it)
5811 {
5812 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5813 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5814 }
5815
5816
5817 /* Move IT to the next line start.
5818
5819 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5820 we skipped over part of the text (as opposed to moving the iterator
5821 continuously over the text). Otherwise, don't change the value
5822 of *SKIPPED_P.
5823
5824 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5825 iterator on the newline, if it was found.
5826
5827 Newlines may come from buffer text, overlay strings, or strings
5828 displayed via the `display' property. That's the reason we can't
5829 simply use find_next_newline_no_quit.
5830
5831 Note that this function may not skip over invisible text that is so
5832 because of text properties and immediately follows a newline. If
5833 it would, function reseat_at_next_visible_line_start, when called
5834 from set_iterator_to_next, would effectively make invisible
5835 characters following a newline part of the wrong glyph row, which
5836 leads to wrong cursor motion. */
5837
5838 static int
5839 forward_to_next_line_start (struct it *it, int *skipped_p,
5840 struct bidi_it *bidi_it_prev)
5841 {
5842 EMACS_INT old_selective;
5843 int newline_found_p, n;
5844 const int MAX_NEWLINE_DISTANCE = 500;
5845
5846 /* If already on a newline, just consume it to avoid unintended
5847 skipping over invisible text below. */
5848 if (it->what == IT_CHARACTER
5849 && it->c == '\n'
5850 && CHARPOS (it->position) == IT_CHARPOS (*it))
5851 {
5852 if (it->bidi_p && bidi_it_prev)
5853 *bidi_it_prev = it->bidi_it;
5854 set_iterator_to_next (it, 0);
5855 it->c = 0;
5856 return 1;
5857 }
5858
5859 /* Don't handle selective display in the following. It's (a)
5860 unnecessary because it's done by the caller, and (b) leads to an
5861 infinite recursion because next_element_from_ellipsis indirectly
5862 calls this function. */
5863 old_selective = it->selective;
5864 it->selective = 0;
5865
5866 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5867 from buffer text. */
5868 for (n = newline_found_p = 0;
5869 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5870 n += STRINGP (it->string) ? 0 : 1)
5871 {
5872 if (!get_next_display_element (it))
5873 return 0;
5874 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5875 if (newline_found_p && it->bidi_p && bidi_it_prev)
5876 *bidi_it_prev = it->bidi_it;
5877 set_iterator_to_next (it, 0);
5878 }
5879
5880 /* If we didn't find a newline near enough, see if we can use a
5881 short-cut. */
5882 if (!newline_found_p)
5883 {
5884 EMACS_INT start = IT_CHARPOS (*it);
5885 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5886 Lisp_Object pos;
5887
5888 xassert (!STRINGP (it->string));
5889
5890 /* If there isn't any `display' property in sight, and no
5891 overlays, we can just use the position of the newline in
5892 buffer text. */
5893 if (it->stop_charpos >= limit
5894 || ((pos = Fnext_single_property_change (make_number (start),
5895 Qdisplay, Qnil,
5896 make_number (limit)),
5897 NILP (pos))
5898 && next_overlay_change (start) == ZV))
5899 {
5900 if (!it->bidi_p)
5901 {
5902 IT_CHARPOS (*it) = limit;
5903 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5904 }
5905 else
5906 {
5907 struct bidi_it bprev;
5908
5909 /* Help bidi.c avoid expensive searches for display
5910 properties and overlays, by telling it that there are
5911 none up to `limit'. */
5912 if (it->bidi_it.disp_pos < limit)
5913 {
5914 it->bidi_it.disp_pos = limit;
5915 it->bidi_it.disp_prop = 0;
5916 }
5917 do {
5918 bprev = it->bidi_it;
5919 bidi_move_to_visually_next (&it->bidi_it);
5920 } while (it->bidi_it.charpos != limit);
5921 IT_CHARPOS (*it) = limit;
5922 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5923 if (bidi_it_prev)
5924 *bidi_it_prev = bprev;
5925 }
5926 *skipped_p = newline_found_p = 1;
5927 }
5928 else
5929 {
5930 while (get_next_display_element (it)
5931 && !newline_found_p)
5932 {
5933 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5934 if (newline_found_p && it->bidi_p && bidi_it_prev)
5935 *bidi_it_prev = it->bidi_it;
5936 set_iterator_to_next (it, 0);
5937 }
5938 }
5939 }
5940
5941 it->selective = old_selective;
5942 return newline_found_p;
5943 }
5944
5945
5946 /* Set IT's current position to the previous visible line start. Skip
5947 invisible text that is so either due to text properties or due to
5948 selective display. Caution: this does not change IT->current_x and
5949 IT->hpos. */
5950
5951 static void
5952 back_to_previous_visible_line_start (struct it *it)
5953 {
5954 while (IT_CHARPOS (*it) > BEGV)
5955 {
5956 back_to_previous_line_start (it);
5957
5958 if (IT_CHARPOS (*it) <= BEGV)
5959 break;
5960
5961 /* If selective > 0, then lines indented more than its value are
5962 invisible. */
5963 if (it->selective > 0
5964 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5965 it->selective))
5966 continue;
5967
5968 /* Check the newline before point for invisibility. */
5969 {
5970 Lisp_Object prop;
5971 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5972 Qinvisible, it->window);
5973 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5974 continue;
5975 }
5976
5977 if (IT_CHARPOS (*it) <= BEGV)
5978 break;
5979
5980 {
5981 struct it it2;
5982 void *it2data = NULL;
5983 EMACS_INT pos;
5984 EMACS_INT beg, end;
5985 Lisp_Object val, overlay;
5986
5987 SAVE_IT (it2, *it, it2data);
5988
5989 /* If newline is part of a composition, continue from start of composition */
5990 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5991 && beg < IT_CHARPOS (*it))
5992 goto replaced;
5993
5994 /* If newline is replaced by a display property, find start of overlay
5995 or interval and continue search from that point. */
5996 pos = --IT_CHARPOS (it2);
5997 --IT_BYTEPOS (it2);
5998 it2.sp = 0;
5999 bidi_unshelve_cache (NULL, 0);
6000 it2.string_from_display_prop_p = 0;
6001 it2.from_disp_prop_p = 0;
6002 if (handle_display_prop (&it2) == HANDLED_RETURN
6003 && !NILP (val = get_char_property_and_overlay
6004 (make_number (pos), Qdisplay, Qnil, &overlay))
6005 && (OVERLAYP (overlay)
6006 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6007 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6008 {
6009 RESTORE_IT (it, it, it2data);
6010 goto replaced;
6011 }
6012
6013 /* Newline is not replaced by anything -- so we are done. */
6014 RESTORE_IT (it, it, it2data);
6015 break;
6016
6017 replaced:
6018 if (beg < BEGV)
6019 beg = BEGV;
6020 IT_CHARPOS (*it) = beg;
6021 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6022 }
6023 }
6024
6025 it->continuation_lines_width = 0;
6026
6027 xassert (IT_CHARPOS (*it) >= BEGV);
6028 xassert (IT_CHARPOS (*it) == BEGV
6029 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6030 CHECK_IT (it);
6031 }
6032
6033
6034 /* Reseat iterator IT at the previous visible line start. Skip
6035 invisible text that is so either due to text properties or due to
6036 selective display. At the end, update IT's overlay information,
6037 face information etc. */
6038
6039 void
6040 reseat_at_previous_visible_line_start (struct it *it)
6041 {
6042 back_to_previous_visible_line_start (it);
6043 reseat (it, it->current.pos, 1);
6044 CHECK_IT (it);
6045 }
6046
6047
6048 /* Reseat iterator IT on the next visible line start in the current
6049 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6050 preceding the line start. Skip over invisible text that is so
6051 because of selective display. Compute faces, overlays etc at the
6052 new position. Note that this function does not skip over text that
6053 is invisible because of text properties. */
6054
6055 static void
6056 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6057 {
6058 int newline_found_p, skipped_p = 0;
6059 struct bidi_it bidi_it_prev;
6060
6061 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6062
6063 /* Skip over lines that are invisible because they are indented
6064 more than the value of IT->selective. */
6065 if (it->selective > 0)
6066 while (IT_CHARPOS (*it) < ZV
6067 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6068 it->selective))
6069 {
6070 xassert (IT_BYTEPOS (*it) == BEGV
6071 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6072 newline_found_p =
6073 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6074 }
6075
6076 /* Position on the newline if that's what's requested. */
6077 if (on_newline_p && newline_found_p)
6078 {
6079 if (STRINGP (it->string))
6080 {
6081 if (IT_STRING_CHARPOS (*it) > 0)
6082 {
6083 if (!it->bidi_p)
6084 {
6085 --IT_STRING_CHARPOS (*it);
6086 --IT_STRING_BYTEPOS (*it);
6087 }
6088 else
6089 {
6090 /* We need to restore the bidi iterator to the state
6091 it had on the newline, and resync the IT's
6092 position with that. */
6093 it->bidi_it = bidi_it_prev;
6094 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6095 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6096 }
6097 }
6098 }
6099 else if (IT_CHARPOS (*it) > BEGV)
6100 {
6101 if (!it->bidi_p)
6102 {
6103 --IT_CHARPOS (*it);
6104 --IT_BYTEPOS (*it);
6105 }
6106 else
6107 {
6108 /* We need to restore the bidi iterator to the state it
6109 had on the newline and resync IT with that. */
6110 it->bidi_it = bidi_it_prev;
6111 IT_CHARPOS (*it) = it->bidi_it.charpos;
6112 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6113 }
6114 reseat (it, it->current.pos, 0);
6115 }
6116 }
6117 else if (skipped_p)
6118 reseat (it, it->current.pos, 0);
6119
6120 CHECK_IT (it);
6121 }
6122
6123
6124 \f
6125 /***********************************************************************
6126 Changing an iterator's position
6127 ***********************************************************************/
6128
6129 /* Change IT's current position to POS in current_buffer. If FORCE_P
6130 is non-zero, always check for text properties at the new position.
6131 Otherwise, text properties are only looked up if POS >=
6132 IT->check_charpos of a property. */
6133
6134 static void
6135 reseat (struct it *it, struct text_pos pos, int force_p)
6136 {
6137 EMACS_INT original_pos = IT_CHARPOS (*it);
6138
6139 reseat_1 (it, pos, 0);
6140
6141 /* Determine where to check text properties. Avoid doing it
6142 where possible because text property lookup is very expensive. */
6143 if (force_p
6144 || CHARPOS (pos) > it->stop_charpos
6145 || CHARPOS (pos) < original_pos)
6146 {
6147 if (it->bidi_p)
6148 {
6149 /* For bidi iteration, we need to prime prev_stop and
6150 base_level_stop with our best estimations. */
6151 /* Implementation note: Of course, POS is not necessarily a
6152 stop position, so assigning prev_pos to it is a lie; we
6153 should have called compute_stop_backwards. However, if
6154 the current buffer does not include any R2L characters,
6155 that call would be a waste of cycles, because the
6156 iterator will never move back, and thus never cross this
6157 "fake" stop position. So we delay that backward search
6158 until the time we really need it, in next_element_from_buffer. */
6159 if (CHARPOS (pos) != it->prev_stop)
6160 it->prev_stop = CHARPOS (pos);
6161 if (CHARPOS (pos) < it->base_level_stop)
6162 it->base_level_stop = 0; /* meaning it's unknown */
6163 handle_stop (it);
6164 }
6165 else
6166 {
6167 handle_stop (it);
6168 it->prev_stop = it->base_level_stop = 0;
6169 }
6170
6171 }
6172
6173 CHECK_IT (it);
6174 }
6175
6176
6177 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6178 IT->stop_pos to POS, also. */
6179
6180 static void
6181 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6182 {
6183 /* Don't call this function when scanning a C string. */
6184 xassert (it->s == NULL);
6185
6186 /* POS must be a reasonable value. */
6187 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6188
6189 it->current.pos = it->position = pos;
6190 it->end_charpos = ZV;
6191 it->dpvec = NULL;
6192 it->current.dpvec_index = -1;
6193 it->current.overlay_string_index = -1;
6194 IT_STRING_CHARPOS (*it) = -1;
6195 IT_STRING_BYTEPOS (*it) = -1;
6196 it->string = Qnil;
6197 it->method = GET_FROM_BUFFER;
6198 it->object = it->w->buffer;
6199 it->area = TEXT_AREA;
6200 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6201 it->sp = 0;
6202 it->string_from_display_prop_p = 0;
6203 it->string_from_prefix_prop_p = 0;
6204
6205 it->from_disp_prop_p = 0;
6206 it->face_before_selective_p = 0;
6207 if (it->bidi_p)
6208 {
6209 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6210 &it->bidi_it);
6211 bidi_unshelve_cache (NULL, 0);
6212 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6213 it->bidi_it.string.s = NULL;
6214 it->bidi_it.string.lstring = Qnil;
6215 it->bidi_it.string.bufpos = 0;
6216 it->bidi_it.string.unibyte = 0;
6217 }
6218
6219 if (set_stop_p)
6220 {
6221 it->stop_charpos = CHARPOS (pos);
6222 it->base_level_stop = CHARPOS (pos);
6223 }
6224 }
6225
6226
6227 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6228 If S is non-null, it is a C string to iterate over. Otherwise,
6229 STRING gives a Lisp string to iterate over.
6230
6231 If PRECISION > 0, don't return more then PRECISION number of
6232 characters from the string.
6233
6234 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6235 characters have been returned. FIELD_WIDTH < 0 means an infinite
6236 field width.
6237
6238 MULTIBYTE = 0 means disable processing of multibyte characters,
6239 MULTIBYTE > 0 means enable it,
6240 MULTIBYTE < 0 means use IT->multibyte_p.
6241
6242 IT must be initialized via a prior call to init_iterator before
6243 calling this function. */
6244
6245 static void
6246 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6247 EMACS_INT charpos, EMACS_INT precision, int field_width,
6248 int multibyte)
6249 {
6250 /* No region in strings. */
6251 it->region_beg_charpos = it->region_end_charpos = -1;
6252
6253 /* No text property checks performed by default, but see below. */
6254 it->stop_charpos = -1;
6255
6256 /* Set iterator position and end position. */
6257 memset (&it->current, 0, sizeof it->current);
6258 it->current.overlay_string_index = -1;
6259 it->current.dpvec_index = -1;
6260 xassert (charpos >= 0);
6261
6262 /* If STRING is specified, use its multibyteness, otherwise use the
6263 setting of MULTIBYTE, if specified. */
6264 if (multibyte >= 0)
6265 it->multibyte_p = multibyte > 0;
6266
6267 /* Bidirectional reordering of strings is controlled by the default
6268 value of bidi-display-reordering. Don't try to reorder while
6269 loading loadup.el, as the necessary character property tables are
6270 not yet available. */
6271 it->bidi_p =
6272 NILP (Vpurify_flag)
6273 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6274
6275 if (s == NULL)
6276 {
6277 xassert (STRINGP (string));
6278 it->string = string;
6279 it->s = NULL;
6280 it->end_charpos = it->string_nchars = SCHARS (string);
6281 it->method = GET_FROM_STRING;
6282 it->current.string_pos = string_pos (charpos, string);
6283
6284 if (it->bidi_p)
6285 {
6286 it->bidi_it.string.lstring = string;
6287 it->bidi_it.string.s = NULL;
6288 it->bidi_it.string.schars = it->end_charpos;
6289 it->bidi_it.string.bufpos = 0;
6290 it->bidi_it.string.from_disp_str = 0;
6291 it->bidi_it.string.unibyte = !it->multibyte_p;
6292 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6293 FRAME_WINDOW_P (it->f), &it->bidi_it);
6294 }
6295 }
6296 else
6297 {
6298 it->s = (const unsigned char *) s;
6299 it->string = Qnil;
6300
6301 /* Note that we use IT->current.pos, not it->current.string_pos,
6302 for displaying C strings. */
6303 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6304 if (it->multibyte_p)
6305 {
6306 it->current.pos = c_string_pos (charpos, s, 1);
6307 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6308 }
6309 else
6310 {
6311 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6312 it->end_charpos = it->string_nchars = strlen (s);
6313 }
6314
6315 if (it->bidi_p)
6316 {
6317 it->bidi_it.string.lstring = Qnil;
6318 it->bidi_it.string.s = (const unsigned char *) s;
6319 it->bidi_it.string.schars = it->end_charpos;
6320 it->bidi_it.string.bufpos = 0;
6321 it->bidi_it.string.from_disp_str = 0;
6322 it->bidi_it.string.unibyte = !it->multibyte_p;
6323 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6324 &it->bidi_it);
6325 }
6326 it->method = GET_FROM_C_STRING;
6327 }
6328
6329 /* PRECISION > 0 means don't return more than PRECISION characters
6330 from the string. */
6331 if (precision > 0 && it->end_charpos - charpos > precision)
6332 {
6333 it->end_charpos = it->string_nchars = charpos + precision;
6334 if (it->bidi_p)
6335 it->bidi_it.string.schars = it->end_charpos;
6336 }
6337
6338 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6339 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6340 FIELD_WIDTH < 0 means infinite field width. This is useful for
6341 padding with `-' at the end of a mode line. */
6342 if (field_width < 0)
6343 field_width = INFINITY;
6344 /* Implementation note: We deliberately don't enlarge
6345 it->bidi_it.string.schars here to fit it->end_charpos, because
6346 the bidi iterator cannot produce characters out of thin air. */
6347 if (field_width > it->end_charpos - charpos)
6348 it->end_charpos = charpos + field_width;
6349
6350 /* Use the standard display table for displaying strings. */
6351 if (DISP_TABLE_P (Vstandard_display_table))
6352 it->dp = XCHAR_TABLE (Vstandard_display_table);
6353
6354 it->stop_charpos = charpos;
6355 it->prev_stop = charpos;
6356 it->base_level_stop = 0;
6357 if (it->bidi_p)
6358 {
6359 it->bidi_it.first_elt = 1;
6360 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6361 it->bidi_it.disp_pos = -1;
6362 }
6363 if (s == NULL && it->multibyte_p)
6364 {
6365 EMACS_INT endpos = SCHARS (it->string);
6366 if (endpos > it->end_charpos)
6367 endpos = it->end_charpos;
6368 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6369 it->string);
6370 }
6371 CHECK_IT (it);
6372 }
6373
6374
6375 \f
6376 /***********************************************************************
6377 Iteration
6378 ***********************************************************************/
6379
6380 /* Map enum it_method value to corresponding next_element_from_* function. */
6381
6382 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6383 {
6384 next_element_from_buffer,
6385 next_element_from_display_vector,
6386 next_element_from_string,
6387 next_element_from_c_string,
6388 next_element_from_image,
6389 next_element_from_stretch
6390 };
6391
6392 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6393
6394
6395 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6396 (possibly with the following characters). */
6397
6398 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6399 ((IT)->cmp_it.id >= 0 \
6400 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6401 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6402 END_CHARPOS, (IT)->w, \
6403 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6404 (IT)->string)))
6405
6406
6407 /* Lookup the char-table Vglyphless_char_display for character C (-1
6408 if we want information for no-font case), and return the display
6409 method symbol. By side-effect, update it->what and
6410 it->glyphless_method. This function is called from
6411 get_next_display_element for each character element, and from
6412 x_produce_glyphs when no suitable font was found. */
6413
6414 Lisp_Object
6415 lookup_glyphless_char_display (int c, struct it *it)
6416 {
6417 Lisp_Object glyphless_method = Qnil;
6418
6419 if (CHAR_TABLE_P (Vglyphless_char_display)
6420 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6421 {
6422 if (c >= 0)
6423 {
6424 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6425 if (CONSP (glyphless_method))
6426 glyphless_method = FRAME_WINDOW_P (it->f)
6427 ? XCAR (glyphless_method)
6428 : XCDR (glyphless_method);
6429 }
6430 else
6431 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6432 }
6433
6434 retry:
6435 if (NILP (glyphless_method))
6436 {
6437 if (c >= 0)
6438 /* The default is to display the character by a proper font. */
6439 return Qnil;
6440 /* The default for the no-font case is to display an empty box. */
6441 glyphless_method = Qempty_box;
6442 }
6443 if (EQ (glyphless_method, Qzero_width))
6444 {
6445 if (c >= 0)
6446 return glyphless_method;
6447 /* This method can't be used for the no-font case. */
6448 glyphless_method = Qempty_box;
6449 }
6450 if (EQ (glyphless_method, Qthin_space))
6451 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6452 else if (EQ (glyphless_method, Qempty_box))
6453 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6454 else if (EQ (glyphless_method, Qhex_code))
6455 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6456 else if (STRINGP (glyphless_method))
6457 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6458 else
6459 {
6460 /* Invalid value. We use the default method. */
6461 glyphless_method = Qnil;
6462 goto retry;
6463 }
6464 it->what = IT_GLYPHLESS;
6465 return glyphless_method;
6466 }
6467
6468 /* Load IT's display element fields with information about the next
6469 display element from the current position of IT. Value is zero if
6470 end of buffer (or C string) is reached. */
6471
6472 static struct frame *last_escape_glyph_frame = NULL;
6473 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6474 static int last_escape_glyph_merged_face_id = 0;
6475
6476 struct frame *last_glyphless_glyph_frame = NULL;
6477 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6478 int last_glyphless_glyph_merged_face_id = 0;
6479
6480 static int
6481 get_next_display_element (struct it *it)
6482 {
6483 /* Non-zero means that we found a display element. Zero means that
6484 we hit the end of what we iterate over. Performance note: the
6485 function pointer `method' used here turns out to be faster than
6486 using a sequence of if-statements. */
6487 int success_p;
6488
6489 get_next:
6490 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6491
6492 if (it->what == IT_CHARACTER)
6493 {
6494 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6495 and only if (a) the resolved directionality of that character
6496 is R..." */
6497 /* FIXME: Do we need an exception for characters from display
6498 tables? */
6499 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6500 it->c = bidi_mirror_char (it->c);
6501 /* Map via display table or translate control characters.
6502 IT->c, IT->len etc. have been set to the next character by
6503 the function call above. If we have a display table, and it
6504 contains an entry for IT->c, translate it. Don't do this if
6505 IT->c itself comes from a display table, otherwise we could
6506 end up in an infinite recursion. (An alternative could be to
6507 count the recursion depth of this function and signal an
6508 error when a certain maximum depth is reached.) Is it worth
6509 it? */
6510 if (success_p && it->dpvec == NULL)
6511 {
6512 Lisp_Object dv;
6513 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6514 int nonascii_space_p = 0;
6515 int nonascii_hyphen_p = 0;
6516 int c = it->c; /* This is the character to display. */
6517
6518 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6519 {
6520 xassert (SINGLE_BYTE_CHAR_P (c));
6521 if (unibyte_display_via_language_environment)
6522 {
6523 c = DECODE_CHAR (unibyte, c);
6524 if (c < 0)
6525 c = BYTE8_TO_CHAR (it->c);
6526 }
6527 else
6528 c = BYTE8_TO_CHAR (it->c);
6529 }
6530
6531 if (it->dp
6532 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6533 VECTORP (dv)))
6534 {
6535 struct Lisp_Vector *v = XVECTOR (dv);
6536
6537 /* Return the first character from the display table
6538 entry, if not empty. If empty, don't display the
6539 current character. */
6540 if (v->header.size)
6541 {
6542 it->dpvec_char_len = it->len;
6543 it->dpvec = v->contents;
6544 it->dpend = v->contents + v->header.size;
6545 it->current.dpvec_index = 0;
6546 it->dpvec_face_id = -1;
6547 it->saved_face_id = it->face_id;
6548 it->method = GET_FROM_DISPLAY_VECTOR;
6549 it->ellipsis_p = 0;
6550 }
6551 else
6552 {
6553 set_iterator_to_next (it, 0);
6554 }
6555 goto get_next;
6556 }
6557
6558 if (! NILP (lookup_glyphless_char_display (c, it)))
6559 {
6560 if (it->what == IT_GLYPHLESS)
6561 goto done;
6562 /* Don't display this character. */
6563 set_iterator_to_next (it, 0);
6564 goto get_next;
6565 }
6566
6567 /* If `nobreak-char-display' is non-nil, we display
6568 non-ASCII spaces and hyphens specially. */
6569 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6570 {
6571 if (c == 0xA0)
6572 nonascii_space_p = 1;
6573 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6574 nonascii_hyphen_p = 1;
6575 }
6576
6577 /* Translate control characters into `\003' or `^C' form.
6578 Control characters coming from a display table entry are
6579 currently not translated because we use IT->dpvec to hold
6580 the translation. This could easily be changed but I
6581 don't believe that it is worth doing.
6582
6583 The characters handled by `nobreak-char-display' must be
6584 translated too.
6585
6586 Non-printable characters and raw-byte characters are also
6587 translated to octal form. */
6588 if (((c < ' ' || c == 127) /* ASCII control chars */
6589 ? (it->area != TEXT_AREA
6590 /* In mode line, treat \n, \t like other crl chars. */
6591 || (c != '\t'
6592 && it->glyph_row
6593 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6594 || (c != '\n' && c != '\t'))
6595 : (nonascii_space_p
6596 || nonascii_hyphen_p
6597 || CHAR_BYTE8_P (c)
6598 || ! CHAR_PRINTABLE_P (c))))
6599 {
6600 /* C is a control character, non-ASCII space/hyphen,
6601 raw-byte, or a non-printable character which must be
6602 displayed either as '\003' or as `^C' where the '\\'
6603 and '^' can be defined in the display table. Fill
6604 IT->ctl_chars with glyphs for what we have to
6605 display. Then, set IT->dpvec to these glyphs. */
6606 Lisp_Object gc;
6607 int ctl_len;
6608 int face_id;
6609 EMACS_INT lface_id = 0;
6610 int escape_glyph;
6611
6612 /* Handle control characters with ^. */
6613
6614 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6615 {
6616 int g;
6617
6618 g = '^'; /* default glyph for Control */
6619 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6620 if (it->dp
6621 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6622 && GLYPH_CODE_CHAR_VALID_P (gc))
6623 {
6624 g = GLYPH_CODE_CHAR (gc);
6625 lface_id = GLYPH_CODE_FACE (gc);
6626 }
6627 if (lface_id)
6628 {
6629 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6630 }
6631 else if (it->f == last_escape_glyph_frame
6632 && it->face_id == last_escape_glyph_face_id)
6633 {
6634 face_id = last_escape_glyph_merged_face_id;
6635 }
6636 else
6637 {
6638 /* Merge the escape-glyph face into the current face. */
6639 face_id = merge_faces (it->f, Qescape_glyph, 0,
6640 it->face_id);
6641 last_escape_glyph_frame = it->f;
6642 last_escape_glyph_face_id = it->face_id;
6643 last_escape_glyph_merged_face_id = face_id;
6644 }
6645
6646 XSETINT (it->ctl_chars[0], g);
6647 XSETINT (it->ctl_chars[1], c ^ 0100);
6648 ctl_len = 2;
6649 goto display_control;
6650 }
6651
6652 /* Handle non-ascii space in the mode where it only gets
6653 highlighting. */
6654
6655 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6656 {
6657 /* Merge `nobreak-space' into the current face. */
6658 face_id = merge_faces (it->f, Qnobreak_space, 0,
6659 it->face_id);
6660 XSETINT (it->ctl_chars[0], ' ');
6661 ctl_len = 1;
6662 goto display_control;
6663 }
6664
6665 /* Handle sequences that start with the "escape glyph". */
6666
6667 /* the default escape glyph is \. */
6668 escape_glyph = '\\';
6669
6670 if (it->dp
6671 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6672 && GLYPH_CODE_CHAR_VALID_P (gc))
6673 {
6674 escape_glyph = GLYPH_CODE_CHAR (gc);
6675 lface_id = GLYPH_CODE_FACE (gc);
6676 }
6677 if (lface_id)
6678 {
6679 /* The display table specified a face.
6680 Merge it into face_id and also into escape_glyph. */
6681 face_id = merge_faces (it->f, Qt, lface_id,
6682 it->face_id);
6683 }
6684 else if (it->f == last_escape_glyph_frame
6685 && it->face_id == last_escape_glyph_face_id)
6686 {
6687 face_id = last_escape_glyph_merged_face_id;
6688 }
6689 else
6690 {
6691 /* Merge the escape-glyph face into the current face. */
6692 face_id = merge_faces (it->f, Qescape_glyph, 0,
6693 it->face_id);
6694 last_escape_glyph_frame = it->f;
6695 last_escape_glyph_face_id = it->face_id;
6696 last_escape_glyph_merged_face_id = face_id;
6697 }
6698
6699 /* Draw non-ASCII hyphen with just highlighting: */
6700
6701 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6702 {
6703 XSETINT (it->ctl_chars[0], '-');
6704 ctl_len = 1;
6705 goto display_control;
6706 }
6707
6708 /* Draw non-ASCII space/hyphen with escape glyph: */
6709
6710 if (nonascii_space_p || nonascii_hyphen_p)
6711 {
6712 XSETINT (it->ctl_chars[0], escape_glyph);
6713 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6714 ctl_len = 2;
6715 goto display_control;
6716 }
6717
6718 {
6719 char str[10];
6720 int len, i;
6721
6722 if (CHAR_BYTE8_P (c))
6723 /* Display \200 instead of \17777600. */
6724 c = CHAR_TO_BYTE8 (c);
6725 len = sprintf (str, "%03o", c);
6726
6727 XSETINT (it->ctl_chars[0], escape_glyph);
6728 for (i = 0; i < len; i++)
6729 XSETINT (it->ctl_chars[i + 1], str[i]);
6730 ctl_len = len + 1;
6731 }
6732
6733 display_control:
6734 /* Set up IT->dpvec and return first character from it. */
6735 it->dpvec_char_len = it->len;
6736 it->dpvec = it->ctl_chars;
6737 it->dpend = it->dpvec + ctl_len;
6738 it->current.dpvec_index = 0;
6739 it->dpvec_face_id = face_id;
6740 it->saved_face_id = it->face_id;
6741 it->method = GET_FROM_DISPLAY_VECTOR;
6742 it->ellipsis_p = 0;
6743 goto get_next;
6744 }
6745 it->char_to_display = c;
6746 }
6747 else if (success_p)
6748 {
6749 it->char_to_display = it->c;
6750 }
6751 }
6752
6753 /* Adjust face id for a multibyte character. There are no multibyte
6754 character in unibyte text. */
6755 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6756 && it->multibyte_p
6757 && success_p
6758 && FRAME_WINDOW_P (it->f))
6759 {
6760 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6761
6762 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6763 {
6764 /* Automatic composition with glyph-string. */
6765 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6766
6767 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6768 }
6769 else
6770 {
6771 EMACS_INT pos = (it->s ? -1
6772 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6773 : IT_CHARPOS (*it));
6774 int c;
6775
6776 if (it->what == IT_CHARACTER)
6777 c = it->char_to_display;
6778 else
6779 {
6780 struct composition *cmp = composition_table[it->cmp_it.id];
6781 int i;
6782
6783 c = ' ';
6784 for (i = 0; i < cmp->glyph_len; i++)
6785 /* TAB in a composition means display glyphs with
6786 padding space on the left or right. */
6787 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6788 break;
6789 }
6790 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6791 }
6792 }
6793
6794 done:
6795 /* Is this character the last one of a run of characters with
6796 box? If yes, set IT->end_of_box_run_p to 1. */
6797 if (it->face_box_p
6798 && it->s == NULL)
6799 {
6800 if (it->method == GET_FROM_STRING && it->sp)
6801 {
6802 int face_id = underlying_face_id (it);
6803 struct face *face = FACE_FROM_ID (it->f, face_id);
6804
6805 if (face)
6806 {
6807 if (face->box == FACE_NO_BOX)
6808 {
6809 /* If the box comes from face properties in a
6810 display string, check faces in that string. */
6811 int string_face_id = face_after_it_pos (it);
6812 it->end_of_box_run_p
6813 = (FACE_FROM_ID (it->f, string_face_id)->box
6814 == FACE_NO_BOX);
6815 }
6816 /* Otherwise, the box comes from the underlying face.
6817 If this is the last string character displayed, check
6818 the next buffer location. */
6819 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6820 && (it->current.overlay_string_index
6821 == it->n_overlay_strings - 1))
6822 {
6823 EMACS_INT ignore;
6824 int next_face_id;
6825 struct text_pos pos = it->current.pos;
6826 INC_TEXT_POS (pos, it->multibyte_p);
6827
6828 next_face_id = face_at_buffer_position
6829 (it->w, CHARPOS (pos), it->region_beg_charpos,
6830 it->region_end_charpos, &ignore,
6831 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6832 -1);
6833 it->end_of_box_run_p
6834 = (FACE_FROM_ID (it->f, next_face_id)->box
6835 == FACE_NO_BOX);
6836 }
6837 }
6838 }
6839 else
6840 {
6841 int face_id = face_after_it_pos (it);
6842 it->end_of_box_run_p
6843 = (face_id != it->face_id
6844 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6845 }
6846 }
6847 /* If we reached the end of the object we've been iterating (e.g., a
6848 display string or an overlay string), and there's something on
6849 IT->stack, proceed with what's on the stack. It doesn't make
6850 sense to return zero if there's unprocessed stuff on the stack,
6851 because otherwise that stuff will never be displayed. */
6852 if (!success_p && it->sp > 0)
6853 {
6854 set_iterator_to_next (it, 0);
6855 success_p = get_next_display_element (it);
6856 }
6857
6858 /* Value is 0 if end of buffer or string reached. */
6859 return success_p;
6860 }
6861
6862
6863 /* Move IT to the next display element.
6864
6865 RESEAT_P non-zero means if called on a newline in buffer text,
6866 skip to the next visible line start.
6867
6868 Functions get_next_display_element and set_iterator_to_next are
6869 separate because I find this arrangement easier to handle than a
6870 get_next_display_element function that also increments IT's
6871 position. The way it is we can first look at an iterator's current
6872 display element, decide whether it fits on a line, and if it does,
6873 increment the iterator position. The other way around we probably
6874 would either need a flag indicating whether the iterator has to be
6875 incremented the next time, or we would have to implement a
6876 decrement position function which would not be easy to write. */
6877
6878 void
6879 set_iterator_to_next (struct it *it, int reseat_p)
6880 {
6881 /* Reset flags indicating start and end of a sequence of characters
6882 with box. Reset them at the start of this function because
6883 moving the iterator to a new position might set them. */
6884 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6885
6886 switch (it->method)
6887 {
6888 case GET_FROM_BUFFER:
6889 /* The current display element of IT is a character from
6890 current_buffer. Advance in the buffer, and maybe skip over
6891 invisible lines that are so because of selective display. */
6892 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6893 reseat_at_next_visible_line_start (it, 0);
6894 else if (it->cmp_it.id >= 0)
6895 {
6896 /* We are currently getting glyphs from a composition. */
6897 int i;
6898
6899 if (! it->bidi_p)
6900 {
6901 IT_CHARPOS (*it) += it->cmp_it.nchars;
6902 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6903 if (it->cmp_it.to < it->cmp_it.nglyphs)
6904 {
6905 it->cmp_it.from = it->cmp_it.to;
6906 }
6907 else
6908 {
6909 it->cmp_it.id = -1;
6910 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6911 IT_BYTEPOS (*it),
6912 it->end_charpos, Qnil);
6913 }
6914 }
6915 else if (! it->cmp_it.reversed_p)
6916 {
6917 /* Composition created while scanning forward. */
6918 /* Update IT's char/byte positions to point to the first
6919 character of the next grapheme cluster, or to the
6920 character visually after the current composition. */
6921 for (i = 0; i < it->cmp_it.nchars; i++)
6922 bidi_move_to_visually_next (&it->bidi_it);
6923 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6924 IT_CHARPOS (*it) = it->bidi_it.charpos;
6925
6926 if (it->cmp_it.to < it->cmp_it.nglyphs)
6927 {
6928 /* Proceed to the next grapheme cluster. */
6929 it->cmp_it.from = it->cmp_it.to;
6930 }
6931 else
6932 {
6933 /* No more grapheme clusters in this composition.
6934 Find the next stop position. */
6935 EMACS_INT stop = it->end_charpos;
6936 if (it->bidi_it.scan_dir < 0)
6937 /* Now we are scanning backward and don't know
6938 where to stop. */
6939 stop = -1;
6940 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6941 IT_BYTEPOS (*it), stop, Qnil);
6942 }
6943 }
6944 else
6945 {
6946 /* Composition created while scanning backward. */
6947 /* Update IT's char/byte positions to point to the last
6948 character of the previous grapheme cluster, or the
6949 character visually after the current composition. */
6950 for (i = 0; i < it->cmp_it.nchars; i++)
6951 bidi_move_to_visually_next (&it->bidi_it);
6952 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6953 IT_CHARPOS (*it) = it->bidi_it.charpos;
6954 if (it->cmp_it.from > 0)
6955 {
6956 /* Proceed to the previous grapheme cluster. */
6957 it->cmp_it.to = it->cmp_it.from;
6958 }
6959 else
6960 {
6961 /* No more grapheme clusters in this composition.
6962 Find the next stop position. */
6963 EMACS_INT stop = it->end_charpos;
6964 if (it->bidi_it.scan_dir < 0)
6965 /* Now we are scanning backward and don't know
6966 where to stop. */
6967 stop = -1;
6968 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6969 IT_BYTEPOS (*it), stop, Qnil);
6970 }
6971 }
6972 }
6973 else
6974 {
6975 xassert (it->len != 0);
6976
6977 if (!it->bidi_p)
6978 {
6979 IT_BYTEPOS (*it) += it->len;
6980 IT_CHARPOS (*it) += 1;
6981 }
6982 else
6983 {
6984 int prev_scan_dir = it->bidi_it.scan_dir;
6985 /* If this is a new paragraph, determine its base
6986 direction (a.k.a. its base embedding level). */
6987 if (it->bidi_it.new_paragraph)
6988 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6989 bidi_move_to_visually_next (&it->bidi_it);
6990 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6991 IT_CHARPOS (*it) = it->bidi_it.charpos;
6992 if (prev_scan_dir != it->bidi_it.scan_dir)
6993 {
6994 /* As the scan direction was changed, we must
6995 re-compute the stop position for composition. */
6996 EMACS_INT stop = it->end_charpos;
6997 if (it->bidi_it.scan_dir < 0)
6998 stop = -1;
6999 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7000 IT_BYTEPOS (*it), stop, Qnil);
7001 }
7002 }
7003 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7004 }
7005 break;
7006
7007 case GET_FROM_C_STRING:
7008 /* Current display element of IT is from a C string. */
7009 if (!it->bidi_p
7010 /* If the string position is beyond string's end, it means
7011 next_element_from_c_string is padding the string with
7012 blanks, in which case we bypass the bidi iterator,
7013 because it cannot deal with such virtual characters. */
7014 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7015 {
7016 IT_BYTEPOS (*it) += it->len;
7017 IT_CHARPOS (*it) += 1;
7018 }
7019 else
7020 {
7021 bidi_move_to_visually_next (&it->bidi_it);
7022 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7023 IT_CHARPOS (*it) = it->bidi_it.charpos;
7024 }
7025 break;
7026
7027 case GET_FROM_DISPLAY_VECTOR:
7028 /* Current display element of IT is from a display table entry.
7029 Advance in the display table definition. Reset it to null if
7030 end reached, and continue with characters from buffers/
7031 strings. */
7032 ++it->current.dpvec_index;
7033
7034 /* Restore face of the iterator to what they were before the
7035 display vector entry (these entries may contain faces). */
7036 it->face_id = it->saved_face_id;
7037
7038 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7039 {
7040 int recheck_faces = it->ellipsis_p;
7041
7042 if (it->s)
7043 it->method = GET_FROM_C_STRING;
7044 else if (STRINGP (it->string))
7045 it->method = GET_FROM_STRING;
7046 else
7047 {
7048 it->method = GET_FROM_BUFFER;
7049 it->object = it->w->buffer;
7050 }
7051
7052 it->dpvec = NULL;
7053 it->current.dpvec_index = -1;
7054
7055 /* Skip over characters which were displayed via IT->dpvec. */
7056 if (it->dpvec_char_len < 0)
7057 reseat_at_next_visible_line_start (it, 1);
7058 else if (it->dpvec_char_len > 0)
7059 {
7060 if (it->method == GET_FROM_STRING
7061 && it->n_overlay_strings > 0)
7062 it->ignore_overlay_strings_at_pos_p = 1;
7063 it->len = it->dpvec_char_len;
7064 set_iterator_to_next (it, reseat_p);
7065 }
7066
7067 /* Maybe recheck faces after display vector */
7068 if (recheck_faces)
7069 it->stop_charpos = IT_CHARPOS (*it);
7070 }
7071 break;
7072
7073 case GET_FROM_STRING:
7074 /* Current display element is a character from a Lisp string. */
7075 xassert (it->s == NULL && STRINGP (it->string));
7076 /* Don't advance past string end. These conditions are true
7077 when set_iterator_to_next is called at the end of
7078 get_next_display_element, in which case the Lisp string is
7079 already exhausted, and all we want is pop the iterator
7080 stack. */
7081 if (it->current.overlay_string_index >= 0)
7082 {
7083 /* This is an overlay string, so there's no padding with
7084 spaces, and the number of characters in the string is
7085 where the string ends. */
7086 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7087 goto consider_string_end;
7088 }
7089 else
7090 {
7091 /* Not an overlay string. There could be padding, so test
7092 against it->end_charpos . */
7093 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7094 goto consider_string_end;
7095 }
7096 if (it->cmp_it.id >= 0)
7097 {
7098 int i;
7099
7100 if (! it->bidi_p)
7101 {
7102 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7103 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7104 if (it->cmp_it.to < it->cmp_it.nglyphs)
7105 it->cmp_it.from = it->cmp_it.to;
7106 else
7107 {
7108 it->cmp_it.id = -1;
7109 composition_compute_stop_pos (&it->cmp_it,
7110 IT_STRING_CHARPOS (*it),
7111 IT_STRING_BYTEPOS (*it),
7112 it->end_charpos, it->string);
7113 }
7114 }
7115 else if (! it->cmp_it.reversed_p)
7116 {
7117 for (i = 0; i < it->cmp_it.nchars; i++)
7118 bidi_move_to_visually_next (&it->bidi_it);
7119 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7120 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7121
7122 if (it->cmp_it.to < it->cmp_it.nglyphs)
7123 it->cmp_it.from = it->cmp_it.to;
7124 else
7125 {
7126 EMACS_INT stop = it->end_charpos;
7127 if (it->bidi_it.scan_dir < 0)
7128 stop = -1;
7129 composition_compute_stop_pos (&it->cmp_it,
7130 IT_STRING_CHARPOS (*it),
7131 IT_STRING_BYTEPOS (*it), stop,
7132 it->string);
7133 }
7134 }
7135 else
7136 {
7137 for (i = 0; i < it->cmp_it.nchars; i++)
7138 bidi_move_to_visually_next (&it->bidi_it);
7139 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7140 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7141 if (it->cmp_it.from > 0)
7142 it->cmp_it.to = it->cmp_it.from;
7143 else
7144 {
7145 EMACS_INT stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it,
7149 IT_STRING_CHARPOS (*it),
7150 IT_STRING_BYTEPOS (*it), stop,
7151 it->string);
7152 }
7153 }
7154 }
7155 else
7156 {
7157 if (!it->bidi_p
7158 /* If the string position is beyond string's end, it
7159 means next_element_from_string is padding the string
7160 with blanks, in which case we bypass the bidi
7161 iterator, because it cannot deal with such virtual
7162 characters. */
7163 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7164 {
7165 IT_STRING_BYTEPOS (*it) += it->len;
7166 IT_STRING_CHARPOS (*it) += 1;
7167 }
7168 else
7169 {
7170 int prev_scan_dir = it->bidi_it.scan_dir;
7171
7172 bidi_move_to_visually_next (&it->bidi_it);
7173 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7174 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7175 if (prev_scan_dir != it->bidi_it.scan_dir)
7176 {
7177 EMACS_INT stop = it->end_charpos;
7178
7179 if (it->bidi_it.scan_dir < 0)
7180 stop = -1;
7181 composition_compute_stop_pos (&it->cmp_it,
7182 IT_STRING_CHARPOS (*it),
7183 IT_STRING_BYTEPOS (*it), stop,
7184 it->string);
7185 }
7186 }
7187 }
7188
7189 consider_string_end:
7190
7191 if (it->current.overlay_string_index >= 0)
7192 {
7193 /* IT->string is an overlay string. Advance to the
7194 next, if there is one. */
7195 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7196 {
7197 it->ellipsis_p = 0;
7198 next_overlay_string (it);
7199 if (it->ellipsis_p)
7200 setup_for_ellipsis (it, 0);
7201 }
7202 }
7203 else
7204 {
7205 /* IT->string is not an overlay string. If we reached
7206 its end, and there is something on IT->stack, proceed
7207 with what is on the stack. This can be either another
7208 string, this time an overlay string, or a buffer. */
7209 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7210 && it->sp > 0)
7211 {
7212 pop_it (it);
7213 if (it->method == GET_FROM_STRING)
7214 goto consider_string_end;
7215 }
7216 }
7217 break;
7218
7219 case GET_FROM_IMAGE:
7220 case GET_FROM_STRETCH:
7221 /* The position etc with which we have to proceed are on
7222 the stack. The position may be at the end of a string,
7223 if the `display' property takes up the whole string. */
7224 xassert (it->sp > 0);
7225 pop_it (it);
7226 if (it->method == GET_FROM_STRING)
7227 goto consider_string_end;
7228 break;
7229
7230 default:
7231 /* There are no other methods defined, so this should be a bug. */
7232 abort ();
7233 }
7234
7235 xassert (it->method != GET_FROM_STRING
7236 || (STRINGP (it->string)
7237 && IT_STRING_CHARPOS (*it) >= 0));
7238 }
7239
7240 /* Load IT's display element fields with information about the next
7241 display element which comes from a display table entry or from the
7242 result of translating a control character to one of the forms `^C'
7243 or `\003'.
7244
7245 IT->dpvec holds the glyphs to return as characters.
7246 IT->saved_face_id holds the face id before the display vector--it
7247 is restored into IT->face_id in set_iterator_to_next. */
7248
7249 static int
7250 next_element_from_display_vector (struct it *it)
7251 {
7252 Lisp_Object gc;
7253
7254 /* Precondition. */
7255 xassert (it->dpvec && it->current.dpvec_index >= 0);
7256
7257 it->face_id = it->saved_face_id;
7258
7259 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7260 That seemed totally bogus - so I changed it... */
7261 gc = it->dpvec[it->current.dpvec_index];
7262
7263 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7264 {
7265 it->c = GLYPH_CODE_CHAR (gc);
7266 it->len = CHAR_BYTES (it->c);
7267
7268 /* The entry may contain a face id to use. Such a face id is
7269 the id of a Lisp face, not a realized face. A face id of
7270 zero means no face is specified. */
7271 if (it->dpvec_face_id >= 0)
7272 it->face_id = it->dpvec_face_id;
7273 else
7274 {
7275 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7276 if (lface_id > 0)
7277 it->face_id = merge_faces (it->f, Qt, lface_id,
7278 it->saved_face_id);
7279 }
7280 }
7281 else
7282 /* Display table entry is invalid. Return a space. */
7283 it->c = ' ', it->len = 1;
7284
7285 /* Don't change position and object of the iterator here. They are
7286 still the values of the character that had this display table
7287 entry or was translated, and that's what we want. */
7288 it->what = IT_CHARACTER;
7289 return 1;
7290 }
7291
7292 /* Get the first element of string/buffer in the visual order, after
7293 being reseated to a new position in a string or a buffer. */
7294 static void
7295 get_visually_first_element (struct it *it)
7296 {
7297 int string_p = STRINGP (it->string) || it->s;
7298 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7299 EMACS_INT bob = (string_p ? 0 : BEGV);
7300
7301 if (STRINGP (it->string))
7302 {
7303 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7304 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7305 }
7306 else
7307 {
7308 it->bidi_it.charpos = IT_CHARPOS (*it);
7309 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7310 }
7311
7312 if (it->bidi_it.charpos == eob)
7313 {
7314 /* Nothing to do, but reset the FIRST_ELT flag, like
7315 bidi_paragraph_init does, because we are not going to
7316 call it. */
7317 it->bidi_it.first_elt = 0;
7318 }
7319 else if (it->bidi_it.charpos == bob
7320 || (!string_p
7321 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7322 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7323 {
7324 /* If we are at the beginning of a line/string, we can produce
7325 the next element right away. */
7326 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 }
7329 else
7330 {
7331 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7332
7333 /* We need to prime the bidi iterator starting at the line's or
7334 string's beginning, before we will be able to produce the
7335 next element. */
7336 if (string_p)
7337 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7338 else
7339 {
7340 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7341 -1);
7342 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7343 }
7344 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7345 do
7346 {
7347 /* Now return to buffer/string position where we were asked
7348 to get the next display element, and produce that. */
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 }
7351 while (it->bidi_it.bytepos != orig_bytepos
7352 && it->bidi_it.charpos < eob);
7353 }
7354
7355 /* Adjust IT's position information to where we ended up. */
7356 if (STRINGP (it->string))
7357 {
7358 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7359 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7360 }
7361 else
7362 {
7363 IT_CHARPOS (*it) = it->bidi_it.charpos;
7364 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7365 }
7366
7367 if (STRINGP (it->string) || !it->s)
7368 {
7369 EMACS_INT stop, charpos, bytepos;
7370
7371 if (STRINGP (it->string))
7372 {
7373 xassert (!it->s);
7374 stop = SCHARS (it->string);
7375 if (stop > it->end_charpos)
7376 stop = it->end_charpos;
7377 charpos = IT_STRING_CHARPOS (*it);
7378 bytepos = IT_STRING_BYTEPOS (*it);
7379 }
7380 else
7381 {
7382 stop = it->end_charpos;
7383 charpos = IT_CHARPOS (*it);
7384 bytepos = IT_BYTEPOS (*it);
7385 }
7386 if (it->bidi_it.scan_dir < 0)
7387 stop = -1;
7388 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7389 it->string);
7390 }
7391 }
7392
7393 /* Load IT with the next display element from Lisp string IT->string.
7394 IT->current.string_pos is the current position within the string.
7395 If IT->current.overlay_string_index >= 0, the Lisp string is an
7396 overlay string. */
7397
7398 static int
7399 next_element_from_string (struct it *it)
7400 {
7401 struct text_pos position;
7402
7403 xassert (STRINGP (it->string));
7404 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7405 xassert (IT_STRING_CHARPOS (*it) >= 0);
7406 position = it->current.string_pos;
7407
7408 /* With bidi reordering, the character to display might not be the
7409 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7410 that we were reseat()ed to a new string, whose paragraph
7411 direction is not known. */
7412 if (it->bidi_p && it->bidi_it.first_elt)
7413 {
7414 get_visually_first_element (it);
7415 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7416 }
7417
7418 /* Time to check for invisible text? */
7419 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7420 {
7421 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7422 {
7423 if (!(!it->bidi_p
7424 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7425 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7426 {
7427 /* With bidi non-linear iteration, we could find
7428 ourselves far beyond the last computed stop_charpos,
7429 with several other stop positions in between that we
7430 missed. Scan them all now, in buffer's logical
7431 order, until we find and handle the last stop_charpos
7432 that precedes our current position. */
7433 handle_stop_backwards (it, it->stop_charpos);
7434 return GET_NEXT_DISPLAY_ELEMENT (it);
7435 }
7436 else
7437 {
7438 if (it->bidi_p)
7439 {
7440 /* Take note of the stop position we just moved
7441 across, for when we will move back across it. */
7442 it->prev_stop = it->stop_charpos;
7443 /* If we are at base paragraph embedding level, take
7444 note of the last stop position seen at this
7445 level. */
7446 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7447 it->base_level_stop = it->stop_charpos;
7448 }
7449 handle_stop (it);
7450
7451 /* Since a handler may have changed IT->method, we must
7452 recurse here. */
7453 return GET_NEXT_DISPLAY_ELEMENT (it);
7454 }
7455 }
7456 else if (it->bidi_p
7457 /* If we are before prev_stop, we may have overstepped
7458 on our way backwards a stop_pos, and if so, we need
7459 to handle that stop_pos. */
7460 && IT_STRING_CHARPOS (*it) < it->prev_stop
7461 /* We can sometimes back up for reasons that have nothing
7462 to do with bidi reordering. E.g., compositions. The
7463 code below is only needed when we are above the base
7464 embedding level, so test for that explicitly. */
7465 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7466 {
7467 /* If we lost track of base_level_stop, we have no better
7468 place for handle_stop_backwards to start from than string
7469 beginning. This happens, e.g., when we were reseated to
7470 the previous screenful of text by vertical-motion. */
7471 if (it->base_level_stop <= 0
7472 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7473 it->base_level_stop = 0;
7474 handle_stop_backwards (it, it->base_level_stop);
7475 return GET_NEXT_DISPLAY_ELEMENT (it);
7476 }
7477 }
7478
7479 if (it->current.overlay_string_index >= 0)
7480 {
7481 /* Get the next character from an overlay string. In overlay
7482 strings, there is no field width or padding with spaces to
7483 do. */
7484 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7485 {
7486 it->what = IT_EOB;
7487 return 0;
7488 }
7489 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7490 IT_STRING_BYTEPOS (*it),
7491 it->bidi_it.scan_dir < 0
7492 ? -1
7493 : SCHARS (it->string))
7494 && next_element_from_composition (it))
7495 {
7496 return 1;
7497 }
7498 else if (STRING_MULTIBYTE (it->string))
7499 {
7500 const unsigned char *s = (SDATA (it->string)
7501 + IT_STRING_BYTEPOS (*it));
7502 it->c = string_char_and_length (s, &it->len);
7503 }
7504 else
7505 {
7506 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7507 it->len = 1;
7508 }
7509 }
7510 else
7511 {
7512 /* Get the next character from a Lisp string that is not an
7513 overlay string. Such strings come from the mode line, for
7514 example. We may have to pad with spaces, or truncate the
7515 string. See also next_element_from_c_string. */
7516 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7517 {
7518 it->what = IT_EOB;
7519 return 0;
7520 }
7521 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7522 {
7523 /* Pad with spaces. */
7524 it->c = ' ', it->len = 1;
7525 CHARPOS (position) = BYTEPOS (position) = -1;
7526 }
7527 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7528 IT_STRING_BYTEPOS (*it),
7529 it->bidi_it.scan_dir < 0
7530 ? -1
7531 : it->string_nchars)
7532 && next_element_from_composition (it))
7533 {
7534 return 1;
7535 }
7536 else if (STRING_MULTIBYTE (it->string))
7537 {
7538 const unsigned char *s = (SDATA (it->string)
7539 + IT_STRING_BYTEPOS (*it));
7540 it->c = string_char_and_length (s, &it->len);
7541 }
7542 else
7543 {
7544 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7545 it->len = 1;
7546 }
7547 }
7548
7549 /* Record what we have and where it came from. */
7550 it->what = IT_CHARACTER;
7551 it->object = it->string;
7552 it->position = position;
7553 return 1;
7554 }
7555
7556
7557 /* Load IT with next display element from C string IT->s.
7558 IT->string_nchars is the maximum number of characters to return
7559 from the string. IT->end_charpos may be greater than
7560 IT->string_nchars when this function is called, in which case we
7561 may have to return padding spaces. Value is zero if end of string
7562 reached, including padding spaces. */
7563
7564 static int
7565 next_element_from_c_string (struct it *it)
7566 {
7567 int success_p = 1;
7568
7569 xassert (it->s);
7570 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7571 it->what = IT_CHARACTER;
7572 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7573 it->object = Qnil;
7574
7575 /* With bidi reordering, the character to display might not be the
7576 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7577 we were reseated to a new string, whose paragraph direction is
7578 not known. */
7579 if (it->bidi_p && it->bidi_it.first_elt)
7580 get_visually_first_element (it);
7581
7582 /* IT's position can be greater than IT->string_nchars in case a
7583 field width or precision has been specified when the iterator was
7584 initialized. */
7585 if (IT_CHARPOS (*it) >= it->end_charpos)
7586 {
7587 /* End of the game. */
7588 it->what = IT_EOB;
7589 success_p = 0;
7590 }
7591 else if (IT_CHARPOS (*it) >= it->string_nchars)
7592 {
7593 /* Pad with spaces. */
7594 it->c = ' ', it->len = 1;
7595 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7596 }
7597 else if (it->multibyte_p)
7598 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7599 else
7600 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7601
7602 return success_p;
7603 }
7604
7605
7606 /* Set up IT to return characters from an ellipsis, if appropriate.
7607 The definition of the ellipsis glyphs may come from a display table
7608 entry. This function fills IT with the first glyph from the
7609 ellipsis if an ellipsis is to be displayed. */
7610
7611 static int
7612 next_element_from_ellipsis (struct it *it)
7613 {
7614 if (it->selective_display_ellipsis_p)
7615 setup_for_ellipsis (it, it->len);
7616 else
7617 {
7618 /* The face at the current position may be different from the
7619 face we find after the invisible text. Remember what it
7620 was in IT->saved_face_id, and signal that it's there by
7621 setting face_before_selective_p. */
7622 it->saved_face_id = it->face_id;
7623 it->method = GET_FROM_BUFFER;
7624 it->object = it->w->buffer;
7625 reseat_at_next_visible_line_start (it, 1);
7626 it->face_before_selective_p = 1;
7627 }
7628
7629 return GET_NEXT_DISPLAY_ELEMENT (it);
7630 }
7631
7632
7633 /* Deliver an image display element. The iterator IT is already
7634 filled with image information (done in handle_display_prop). Value
7635 is always 1. */
7636
7637
7638 static int
7639 next_element_from_image (struct it *it)
7640 {
7641 it->what = IT_IMAGE;
7642 it->ignore_overlay_strings_at_pos_p = 0;
7643 return 1;
7644 }
7645
7646
7647 /* Fill iterator IT with next display element from a stretch glyph
7648 property. IT->object is the value of the text property. Value is
7649 always 1. */
7650
7651 static int
7652 next_element_from_stretch (struct it *it)
7653 {
7654 it->what = IT_STRETCH;
7655 return 1;
7656 }
7657
7658 /* Scan backwards from IT's current position until we find a stop
7659 position, or until BEGV. This is called when we find ourself
7660 before both the last known prev_stop and base_level_stop while
7661 reordering bidirectional text. */
7662
7663 static void
7664 compute_stop_pos_backwards (struct it *it)
7665 {
7666 const int SCAN_BACK_LIMIT = 1000;
7667 struct text_pos pos;
7668 struct display_pos save_current = it->current;
7669 struct text_pos save_position = it->position;
7670 EMACS_INT charpos = IT_CHARPOS (*it);
7671 EMACS_INT where_we_are = charpos;
7672 EMACS_INT save_stop_pos = it->stop_charpos;
7673 EMACS_INT save_end_pos = it->end_charpos;
7674
7675 xassert (NILP (it->string) && !it->s);
7676 xassert (it->bidi_p);
7677 it->bidi_p = 0;
7678 do
7679 {
7680 it->end_charpos = min (charpos + 1, ZV);
7681 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7682 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7683 reseat_1 (it, pos, 0);
7684 compute_stop_pos (it);
7685 /* We must advance forward, right? */
7686 if (it->stop_charpos <= charpos)
7687 abort ();
7688 }
7689 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7690
7691 if (it->stop_charpos <= where_we_are)
7692 it->prev_stop = it->stop_charpos;
7693 else
7694 it->prev_stop = BEGV;
7695 it->bidi_p = 1;
7696 it->current = save_current;
7697 it->position = save_position;
7698 it->stop_charpos = save_stop_pos;
7699 it->end_charpos = save_end_pos;
7700 }
7701
7702 /* Scan forward from CHARPOS in the current buffer/string, until we
7703 find a stop position > current IT's position. Then handle the stop
7704 position before that. This is called when we bump into a stop
7705 position while reordering bidirectional text. CHARPOS should be
7706 the last previously processed stop_pos (or BEGV/0, if none were
7707 processed yet) whose position is less that IT's current
7708 position. */
7709
7710 static void
7711 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7712 {
7713 int bufp = !STRINGP (it->string);
7714 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7715 struct display_pos save_current = it->current;
7716 struct text_pos save_position = it->position;
7717 struct text_pos pos1;
7718 EMACS_INT next_stop;
7719
7720 /* Scan in strict logical order. */
7721 xassert (it->bidi_p);
7722 it->bidi_p = 0;
7723 do
7724 {
7725 it->prev_stop = charpos;
7726 if (bufp)
7727 {
7728 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7729 reseat_1 (it, pos1, 0);
7730 }
7731 else
7732 it->current.string_pos = string_pos (charpos, it->string);
7733 compute_stop_pos (it);
7734 /* We must advance forward, right? */
7735 if (it->stop_charpos <= it->prev_stop)
7736 abort ();
7737 charpos = it->stop_charpos;
7738 }
7739 while (charpos <= where_we_are);
7740
7741 it->bidi_p = 1;
7742 it->current = save_current;
7743 it->position = save_position;
7744 next_stop = it->stop_charpos;
7745 it->stop_charpos = it->prev_stop;
7746 handle_stop (it);
7747 it->stop_charpos = next_stop;
7748 }
7749
7750 /* Load IT with the next display element from current_buffer. Value
7751 is zero if end of buffer reached. IT->stop_charpos is the next
7752 position at which to stop and check for text properties or buffer
7753 end. */
7754
7755 static int
7756 next_element_from_buffer (struct it *it)
7757 {
7758 int success_p = 1;
7759
7760 xassert (IT_CHARPOS (*it) >= BEGV);
7761 xassert (NILP (it->string) && !it->s);
7762 xassert (!it->bidi_p
7763 || (EQ (it->bidi_it.string.lstring, Qnil)
7764 && it->bidi_it.string.s == NULL));
7765
7766 /* With bidi reordering, the character to display might not be the
7767 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7768 we were reseat()ed to a new buffer position, which is potentially
7769 a different paragraph. */
7770 if (it->bidi_p && it->bidi_it.first_elt)
7771 {
7772 get_visually_first_element (it);
7773 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7774 }
7775
7776 if (IT_CHARPOS (*it) >= it->stop_charpos)
7777 {
7778 if (IT_CHARPOS (*it) >= it->end_charpos)
7779 {
7780 int overlay_strings_follow_p;
7781
7782 /* End of the game, except when overlay strings follow that
7783 haven't been returned yet. */
7784 if (it->overlay_strings_at_end_processed_p)
7785 overlay_strings_follow_p = 0;
7786 else
7787 {
7788 it->overlay_strings_at_end_processed_p = 1;
7789 overlay_strings_follow_p = get_overlay_strings (it, 0);
7790 }
7791
7792 if (overlay_strings_follow_p)
7793 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7794 else
7795 {
7796 it->what = IT_EOB;
7797 it->position = it->current.pos;
7798 success_p = 0;
7799 }
7800 }
7801 else if (!(!it->bidi_p
7802 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7803 || IT_CHARPOS (*it) == it->stop_charpos))
7804 {
7805 /* With bidi non-linear iteration, we could find ourselves
7806 far beyond the last computed stop_charpos, with several
7807 other stop positions in between that we missed. Scan
7808 them all now, in buffer's logical order, until we find
7809 and handle the last stop_charpos that precedes our
7810 current position. */
7811 handle_stop_backwards (it, it->stop_charpos);
7812 return GET_NEXT_DISPLAY_ELEMENT (it);
7813 }
7814 else
7815 {
7816 if (it->bidi_p)
7817 {
7818 /* Take note of the stop position we just moved across,
7819 for when we will move back across it. */
7820 it->prev_stop = it->stop_charpos;
7821 /* If we are at base paragraph embedding level, take
7822 note of the last stop position seen at this
7823 level. */
7824 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7825 it->base_level_stop = it->stop_charpos;
7826 }
7827 handle_stop (it);
7828 return GET_NEXT_DISPLAY_ELEMENT (it);
7829 }
7830 }
7831 else if (it->bidi_p
7832 /* If we are before prev_stop, we may have overstepped on
7833 our way backwards a stop_pos, and if so, we need to
7834 handle that stop_pos. */
7835 && IT_CHARPOS (*it) < it->prev_stop
7836 /* We can sometimes back up for reasons that have nothing
7837 to do with bidi reordering. E.g., compositions. The
7838 code below is only needed when we are above the base
7839 embedding level, so test for that explicitly. */
7840 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7841 {
7842 if (it->base_level_stop <= 0
7843 || IT_CHARPOS (*it) < it->base_level_stop)
7844 {
7845 /* If we lost track of base_level_stop, we need to find
7846 prev_stop by looking backwards. This happens, e.g., when
7847 we were reseated to the previous screenful of text by
7848 vertical-motion. */
7849 it->base_level_stop = BEGV;
7850 compute_stop_pos_backwards (it);
7851 handle_stop_backwards (it, it->prev_stop);
7852 }
7853 else
7854 handle_stop_backwards (it, it->base_level_stop);
7855 return GET_NEXT_DISPLAY_ELEMENT (it);
7856 }
7857 else
7858 {
7859 /* No face changes, overlays etc. in sight, so just return a
7860 character from current_buffer. */
7861 unsigned char *p;
7862 EMACS_INT stop;
7863
7864 /* Maybe run the redisplay end trigger hook. Performance note:
7865 This doesn't seem to cost measurable time. */
7866 if (it->redisplay_end_trigger_charpos
7867 && it->glyph_row
7868 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7869 run_redisplay_end_trigger_hook (it);
7870
7871 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7872 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7873 stop)
7874 && next_element_from_composition (it))
7875 {
7876 return 1;
7877 }
7878
7879 /* Get the next character, maybe multibyte. */
7880 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7881 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7882 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7883 else
7884 it->c = *p, it->len = 1;
7885
7886 /* Record what we have and where it came from. */
7887 it->what = IT_CHARACTER;
7888 it->object = it->w->buffer;
7889 it->position = it->current.pos;
7890
7891 /* Normally we return the character found above, except when we
7892 really want to return an ellipsis for selective display. */
7893 if (it->selective)
7894 {
7895 if (it->c == '\n')
7896 {
7897 /* A value of selective > 0 means hide lines indented more
7898 than that number of columns. */
7899 if (it->selective > 0
7900 && IT_CHARPOS (*it) + 1 < ZV
7901 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7902 IT_BYTEPOS (*it) + 1,
7903 it->selective))
7904 {
7905 success_p = next_element_from_ellipsis (it);
7906 it->dpvec_char_len = -1;
7907 }
7908 }
7909 else if (it->c == '\r' && it->selective == -1)
7910 {
7911 /* A value of selective == -1 means that everything from the
7912 CR to the end of the line is invisible, with maybe an
7913 ellipsis displayed for it. */
7914 success_p = next_element_from_ellipsis (it);
7915 it->dpvec_char_len = -1;
7916 }
7917 }
7918 }
7919
7920 /* Value is zero if end of buffer reached. */
7921 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7922 return success_p;
7923 }
7924
7925
7926 /* Run the redisplay end trigger hook for IT. */
7927
7928 static void
7929 run_redisplay_end_trigger_hook (struct it *it)
7930 {
7931 Lisp_Object args[3];
7932
7933 /* IT->glyph_row should be non-null, i.e. we should be actually
7934 displaying something, or otherwise we should not run the hook. */
7935 xassert (it->glyph_row);
7936
7937 /* Set up hook arguments. */
7938 args[0] = Qredisplay_end_trigger_functions;
7939 args[1] = it->window;
7940 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7941 it->redisplay_end_trigger_charpos = 0;
7942
7943 /* Since we are *trying* to run these functions, don't try to run
7944 them again, even if they get an error. */
7945 it->w->redisplay_end_trigger = Qnil;
7946 Frun_hook_with_args (3, args);
7947
7948 /* Notice if it changed the face of the character we are on. */
7949 handle_face_prop (it);
7950 }
7951
7952
7953 /* Deliver a composition display element. Unlike the other
7954 next_element_from_XXX, this function is not registered in the array
7955 get_next_element[]. It is called from next_element_from_buffer and
7956 next_element_from_string when necessary. */
7957
7958 static int
7959 next_element_from_composition (struct it *it)
7960 {
7961 it->what = IT_COMPOSITION;
7962 it->len = it->cmp_it.nbytes;
7963 if (STRINGP (it->string))
7964 {
7965 if (it->c < 0)
7966 {
7967 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7968 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7969 return 0;
7970 }
7971 it->position = it->current.string_pos;
7972 it->object = it->string;
7973 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7974 IT_STRING_BYTEPOS (*it), it->string);
7975 }
7976 else
7977 {
7978 if (it->c < 0)
7979 {
7980 IT_CHARPOS (*it) += it->cmp_it.nchars;
7981 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7982 if (it->bidi_p)
7983 {
7984 if (it->bidi_it.new_paragraph)
7985 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7986 /* Resync the bidi iterator with IT's new position.
7987 FIXME: this doesn't support bidirectional text. */
7988 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7989 bidi_move_to_visually_next (&it->bidi_it);
7990 }
7991 return 0;
7992 }
7993 it->position = it->current.pos;
7994 it->object = it->w->buffer;
7995 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7996 IT_BYTEPOS (*it), Qnil);
7997 }
7998 return 1;
7999 }
8000
8001
8002 \f
8003 /***********************************************************************
8004 Moving an iterator without producing glyphs
8005 ***********************************************************************/
8006
8007 /* Check if iterator is at a position corresponding to a valid buffer
8008 position after some move_it_ call. */
8009
8010 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8011 ((it)->method == GET_FROM_STRING \
8012 ? IT_STRING_CHARPOS (*it) == 0 \
8013 : 1)
8014
8015
8016 /* Move iterator IT to a specified buffer or X position within one
8017 line on the display without producing glyphs.
8018
8019 OP should be a bit mask including some or all of these bits:
8020 MOVE_TO_X: Stop upon reaching x-position TO_X.
8021 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8022 Regardless of OP's value, stop upon reaching the end of the display line.
8023
8024 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8025 This means, in particular, that TO_X includes window's horizontal
8026 scroll amount.
8027
8028 The return value has several possible values that
8029 say what condition caused the scan to stop:
8030
8031 MOVE_POS_MATCH_OR_ZV
8032 - when TO_POS or ZV was reached.
8033
8034 MOVE_X_REACHED
8035 -when TO_X was reached before TO_POS or ZV were reached.
8036
8037 MOVE_LINE_CONTINUED
8038 - when we reached the end of the display area and the line must
8039 be continued.
8040
8041 MOVE_LINE_TRUNCATED
8042 - when we reached the end of the display area and the line is
8043 truncated.
8044
8045 MOVE_NEWLINE_OR_CR
8046 - when we stopped at a line end, i.e. a newline or a CR and selective
8047 display is on. */
8048
8049 static enum move_it_result
8050 move_it_in_display_line_to (struct it *it,
8051 EMACS_INT to_charpos, int to_x,
8052 enum move_operation_enum op)
8053 {
8054 enum move_it_result result = MOVE_UNDEFINED;
8055 struct glyph_row *saved_glyph_row;
8056 struct it wrap_it, atpos_it, atx_it, ppos_it;
8057 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8058 void *ppos_data = NULL;
8059 int may_wrap = 0;
8060 enum it_method prev_method = it->method;
8061 EMACS_INT prev_pos = IT_CHARPOS (*it);
8062 int saw_smaller_pos = prev_pos < to_charpos;
8063
8064 /* Don't produce glyphs in produce_glyphs. */
8065 saved_glyph_row = it->glyph_row;
8066 it->glyph_row = NULL;
8067
8068 /* Use wrap_it to save a copy of IT wherever a word wrap could
8069 occur. Use atpos_it to save a copy of IT at the desired buffer
8070 position, if found, so that we can scan ahead and check if the
8071 word later overshoots the window edge. Use atx_it similarly, for
8072 pixel positions. */
8073 wrap_it.sp = -1;
8074 atpos_it.sp = -1;
8075 atx_it.sp = -1;
8076
8077 /* Use ppos_it under bidi reordering to save a copy of IT for the
8078 position > CHARPOS that is the closest to CHARPOS. We restore
8079 that position in IT when we have scanned the entire display line
8080 without finding a match for CHARPOS and all the character
8081 positions are greater than CHARPOS. */
8082 if (it->bidi_p)
8083 {
8084 SAVE_IT (ppos_it, *it, ppos_data);
8085 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8086 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8087 SAVE_IT (ppos_it, *it, ppos_data);
8088 }
8089
8090 #define BUFFER_POS_REACHED_P() \
8091 ((op & MOVE_TO_POS) != 0 \
8092 && BUFFERP (it->object) \
8093 && (IT_CHARPOS (*it) == to_charpos \
8094 || ((!it->bidi_p \
8095 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8096 && IT_CHARPOS (*it) > to_charpos) \
8097 || (it->what == IT_COMPOSITION \
8098 && ((IT_CHARPOS (*it) > to_charpos \
8099 && to_charpos >= it->cmp_it.charpos) \
8100 || (IT_CHARPOS (*it) < to_charpos \
8101 && to_charpos <= it->cmp_it.charpos)))) \
8102 && (it->method == GET_FROM_BUFFER \
8103 || (it->method == GET_FROM_DISPLAY_VECTOR \
8104 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8105
8106 /* If there's a line-/wrap-prefix, handle it. */
8107 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8108 && it->current_y < it->last_visible_y)
8109 handle_line_prefix (it);
8110
8111 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8112 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8113
8114 while (1)
8115 {
8116 int x, i, ascent = 0, descent = 0;
8117
8118 /* Utility macro to reset an iterator with x, ascent, and descent. */
8119 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8120 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8121 (IT)->max_descent = descent)
8122
8123 /* Stop if we move beyond TO_CHARPOS (after an image or a
8124 display string or stretch glyph). */
8125 if ((op & MOVE_TO_POS) != 0
8126 && BUFFERP (it->object)
8127 && it->method == GET_FROM_BUFFER
8128 && (((!it->bidi_p
8129 /* When the iterator is at base embedding level, we
8130 are guaranteed that characters are delivered for
8131 display in strictly increasing order of their
8132 buffer positions. */
8133 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8134 && IT_CHARPOS (*it) > to_charpos)
8135 || (it->bidi_p
8136 && (prev_method == GET_FROM_IMAGE
8137 || prev_method == GET_FROM_STRETCH
8138 || prev_method == GET_FROM_STRING)
8139 /* Passed TO_CHARPOS from left to right. */
8140 && ((prev_pos < to_charpos
8141 && IT_CHARPOS (*it) > to_charpos)
8142 /* Passed TO_CHARPOS from right to left. */
8143 || (prev_pos > to_charpos
8144 && IT_CHARPOS (*it) < to_charpos)))))
8145 {
8146 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8147 {
8148 result = MOVE_POS_MATCH_OR_ZV;
8149 break;
8150 }
8151 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8152 /* If wrap_it is valid, the current position might be in a
8153 word that is wrapped. So, save the iterator in
8154 atpos_it and continue to see if wrapping happens. */
8155 SAVE_IT (atpos_it, *it, atpos_data);
8156 }
8157
8158 /* Stop when ZV reached.
8159 We used to stop here when TO_CHARPOS reached as well, but that is
8160 too soon if this glyph does not fit on this line. So we handle it
8161 explicitly below. */
8162 if (!get_next_display_element (it))
8163 {
8164 result = MOVE_POS_MATCH_OR_ZV;
8165 break;
8166 }
8167
8168 if (it->line_wrap == TRUNCATE)
8169 {
8170 if (BUFFER_POS_REACHED_P ())
8171 {
8172 result = MOVE_POS_MATCH_OR_ZV;
8173 break;
8174 }
8175 }
8176 else
8177 {
8178 if (it->line_wrap == WORD_WRAP)
8179 {
8180 if (IT_DISPLAYING_WHITESPACE (it))
8181 may_wrap = 1;
8182 else if (may_wrap)
8183 {
8184 /* We have reached a glyph that follows one or more
8185 whitespace characters. If the position is
8186 already found, we are done. */
8187 if (atpos_it.sp >= 0)
8188 {
8189 RESTORE_IT (it, &atpos_it, atpos_data);
8190 result = MOVE_POS_MATCH_OR_ZV;
8191 goto done;
8192 }
8193 if (atx_it.sp >= 0)
8194 {
8195 RESTORE_IT (it, &atx_it, atx_data);
8196 result = MOVE_X_REACHED;
8197 goto done;
8198 }
8199 /* Otherwise, we can wrap here. */
8200 SAVE_IT (wrap_it, *it, wrap_data);
8201 may_wrap = 0;
8202 }
8203 }
8204 }
8205
8206 /* Remember the line height for the current line, in case
8207 the next element doesn't fit on the line. */
8208 ascent = it->max_ascent;
8209 descent = it->max_descent;
8210
8211 /* The call to produce_glyphs will get the metrics of the
8212 display element IT is loaded with. Record the x-position
8213 before this display element, in case it doesn't fit on the
8214 line. */
8215 x = it->current_x;
8216
8217 PRODUCE_GLYPHS (it);
8218
8219 if (it->area != TEXT_AREA)
8220 {
8221 prev_method = it->method;
8222 if (it->method == GET_FROM_BUFFER)
8223 prev_pos = IT_CHARPOS (*it);
8224 set_iterator_to_next (it, 1);
8225 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8226 SET_TEXT_POS (this_line_min_pos,
8227 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8228 if (it->bidi_p
8229 && (op & MOVE_TO_POS)
8230 && IT_CHARPOS (*it) > to_charpos
8231 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8232 SAVE_IT (ppos_it, *it, ppos_data);
8233 continue;
8234 }
8235
8236 /* The number of glyphs we get back in IT->nglyphs will normally
8237 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8238 character on a terminal frame, or (iii) a line end. For the
8239 second case, IT->nglyphs - 1 padding glyphs will be present.
8240 (On X frames, there is only one glyph produced for a
8241 composite character.)
8242
8243 The behavior implemented below means, for continuation lines,
8244 that as many spaces of a TAB as fit on the current line are
8245 displayed there. For terminal frames, as many glyphs of a
8246 multi-glyph character are displayed in the current line, too.
8247 This is what the old redisplay code did, and we keep it that
8248 way. Under X, the whole shape of a complex character must
8249 fit on the line or it will be completely displayed in the
8250 next line.
8251
8252 Note that both for tabs and padding glyphs, all glyphs have
8253 the same width. */
8254 if (it->nglyphs)
8255 {
8256 /* More than one glyph or glyph doesn't fit on line. All
8257 glyphs have the same width. */
8258 int single_glyph_width = it->pixel_width / it->nglyphs;
8259 int new_x;
8260 int x_before_this_char = x;
8261 int hpos_before_this_char = it->hpos;
8262
8263 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8264 {
8265 new_x = x + single_glyph_width;
8266
8267 /* We want to leave anything reaching TO_X to the caller. */
8268 if ((op & MOVE_TO_X) && new_x > to_x)
8269 {
8270 if (BUFFER_POS_REACHED_P ())
8271 {
8272 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8273 goto buffer_pos_reached;
8274 if (atpos_it.sp < 0)
8275 {
8276 SAVE_IT (atpos_it, *it, atpos_data);
8277 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8278 }
8279 }
8280 else
8281 {
8282 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8283 {
8284 it->current_x = x;
8285 result = MOVE_X_REACHED;
8286 break;
8287 }
8288 if (atx_it.sp < 0)
8289 {
8290 SAVE_IT (atx_it, *it, atx_data);
8291 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8292 }
8293 }
8294 }
8295
8296 if (/* Lines are continued. */
8297 it->line_wrap != TRUNCATE
8298 && (/* And glyph doesn't fit on the line. */
8299 new_x > it->last_visible_x
8300 /* Or it fits exactly and we're on a window
8301 system frame. */
8302 || (new_x == it->last_visible_x
8303 && FRAME_WINDOW_P (it->f))))
8304 {
8305 if (/* IT->hpos == 0 means the very first glyph
8306 doesn't fit on the line, e.g. a wide image. */
8307 it->hpos == 0
8308 || (new_x == it->last_visible_x
8309 && FRAME_WINDOW_P (it->f)))
8310 {
8311 ++it->hpos;
8312 it->current_x = new_x;
8313
8314 /* The character's last glyph just barely fits
8315 in this row. */
8316 if (i == it->nglyphs - 1)
8317 {
8318 /* If this is the destination position,
8319 return a position *before* it in this row,
8320 now that we know it fits in this row. */
8321 if (BUFFER_POS_REACHED_P ())
8322 {
8323 if (it->line_wrap != WORD_WRAP
8324 || wrap_it.sp < 0)
8325 {
8326 it->hpos = hpos_before_this_char;
8327 it->current_x = x_before_this_char;
8328 result = MOVE_POS_MATCH_OR_ZV;
8329 break;
8330 }
8331 if (it->line_wrap == WORD_WRAP
8332 && atpos_it.sp < 0)
8333 {
8334 SAVE_IT (atpos_it, *it, atpos_data);
8335 atpos_it.current_x = x_before_this_char;
8336 atpos_it.hpos = hpos_before_this_char;
8337 }
8338 }
8339
8340 prev_method = it->method;
8341 if (it->method == GET_FROM_BUFFER)
8342 prev_pos = IT_CHARPOS (*it);
8343 set_iterator_to_next (it, 1);
8344 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8345 SET_TEXT_POS (this_line_min_pos,
8346 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8347 /* On graphical terminals, newlines may
8348 "overflow" into the fringe if
8349 overflow-newline-into-fringe is non-nil.
8350 On text-only terminals, newlines may
8351 overflow into the last glyph on the
8352 display line.*/
8353 if (!FRAME_WINDOW_P (it->f)
8354 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8355 {
8356 if (!get_next_display_element (it))
8357 {
8358 result = MOVE_POS_MATCH_OR_ZV;
8359 break;
8360 }
8361 if (BUFFER_POS_REACHED_P ())
8362 {
8363 if (ITERATOR_AT_END_OF_LINE_P (it))
8364 result = MOVE_POS_MATCH_OR_ZV;
8365 else
8366 result = MOVE_LINE_CONTINUED;
8367 break;
8368 }
8369 if (ITERATOR_AT_END_OF_LINE_P (it))
8370 {
8371 result = MOVE_NEWLINE_OR_CR;
8372 break;
8373 }
8374 }
8375 }
8376 }
8377 else
8378 IT_RESET_X_ASCENT_DESCENT (it);
8379
8380 if (wrap_it.sp >= 0)
8381 {
8382 RESTORE_IT (it, &wrap_it, wrap_data);
8383 atpos_it.sp = -1;
8384 atx_it.sp = -1;
8385 }
8386
8387 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8388 IT_CHARPOS (*it)));
8389 result = MOVE_LINE_CONTINUED;
8390 break;
8391 }
8392
8393 if (BUFFER_POS_REACHED_P ())
8394 {
8395 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8396 goto buffer_pos_reached;
8397 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8398 {
8399 SAVE_IT (atpos_it, *it, atpos_data);
8400 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8401 }
8402 }
8403
8404 if (new_x > it->first_visible_x)
8405 {
8406 /* Glyph is visible. Increment number of glyphs that
8407 would be displayed. */
8408 ++it->hpos;
8409 }
8410 }
8411
8412 if (result != MOVE_UNDEFINED)
8413 break;
8414 }
8415 else if (BUFFER_POS_REACHED_P ())
8416 {
8417 buffer_pos_reached:
8418 IT_RESET_X_ASCENT_DESCENT (it);
8419 result = MOVE_POS_MATCH_OR_ZV;
8420 break;
8421 }
8422 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8423 {
8424 /* Stop when TO_X specified and reached. This check is
8425 necessary here because of lines consisting of a line end,
8426 only. The line end will not produce any glyphs and we
8427 would never get MOVE_X_REACHED. */
8428 xassert (it->nglyphs == 0);
8429 result = MOVE_X_REACHED;
8430 break;
8431 }
8432
8433 /* Is this a line end? If yes, we're done. */
8434 if (ITERATOR_AT_END_OF_LINE_P (it))
8435 {
8436 /* If we are past TO_CHARPOS, but never saw any character
8437 positions smaller than TO_CHARPOS, return
8438 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8439 did. */
8440 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8441 {
8442 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8443 {
8444 if (IT_CHARPOS (ppos_it) < ZV)
8445 {
8446 RESTORE_IT (it, &ppos_it, ppos_data);
8447 result = MOVE_POS_MATCH_OR_ZV;
8448 }
8449 else
8450 goto buffer_pos_reached;
8451 }
8452 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8453 && IT_CHARPOS (*it) > to_charpos)
8454 goto buffer_pos_reached;
8455 else
8456 result = MOVE_NEWLINE_OR_CR;
8457 }
8458 else
8459 result = MOVE_NEWLINE_OR_CR;
8460 break;
8461 }
8462
8463 prev_method = it->method;
8464 if (it->method == GET_FROM_BUFFER)
8465 prev_pos = IT_CHARPOS (*it);
8466 /* The current display element has been consumed. Advance
8467 to the next. */
8468 set_iterator_to_next (it, 1);
8469 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8470 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8471 if (IT_CHARPOS (*it) < to_charpos)
8472 saw_smaller_pos = 1;
8473 if (it->bidi_p
8474 && (op & MOVE_TO_POS)
8475 && IT_CHARPOS (*it) >= to_charpos
8476 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8477 SAVE_IT (ppos_it, *it, ppos_data);
8478
8479 /* Stop if lines are truncated and IT's current x-position is
8480 past the right edge of the window now. */
8481 if (it->line_wrap == TRUNCATE
8482 && it->current_x >= it->last_visible_x)
8483 {
8484 if (!FRAME_WINDOW_P (it->f)
8485 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8486 {
8487 int at_eob_p = 0;
8488
8489 if ((at_eob_p = !get_next_display_element (it))
8490 || BUFFER_POS_REACHED_P ()
8491 /* If we are past TO_CHARPOS, but never saw any
8492 character positions smaller than TO_CHARPOS,
8493 return MOVE_POS_MATCH_OR_ZV, like the
8494 unidirectional display did. */
8495 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8496 && !saw_smaller_pos
8497 && IT_CHARPOS (*it) > to_charpos))
8498 {
8499 if (it->bidi_p
8500 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8501 RESTORE_IT (it, &ppos_it, ppos_data);
8502 result = MOVE_POS_MATCH_OR_ZV;
8503 break;
8504 }
8505 if (ITERATOR_AT_END_OF_LINE_P (it))
8506 {
8507 result = MOVE_NEWLINE_OR_CR;
8508 break;
8509 }
8510 }
8511 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8512 && !saw_smaller_pos
8513 && IT_CHARPOS (*it) > to_charpos)
8514 {
8515 if (IT_CHARPOS (ppos_it) < ZV)
8516 RESTORE_IT (it, &ppos_it, ppos_data);
8517 result = MOVE_POS_MATCH_OR_ZV;
8518 break;
8519 }
8520 result = MOVE_LINE_TRUNCATED;
8521 break;
8522 }
8523 #undef IT_RESET_X_ASCENT_DESCENT
8524 }
8525
8526 #undef BUFFER_POS_REACHED_P
8527
8528 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8529 restore the saved iterator. */
8530 if (atpos_it.sp >= 0)
8531 RESTORE_IT (it, &atpos_it, atpos_data);
8532 else if (atx_it.sp >= 0)
8533 RESTORE_IT (it, &atx_it, atx_data);
8534
8535 done:
8536
8537 if (atpos_data)
8538 bidi_unshelve_cache (atpos_data, 1);
8539 if (atx_data)
8540 bidi_unshelve_cache (atx_data, 1);
8541 if (wrap_data)
8542 bidi_unshelve_cache (wrap_data, 1);
8543 if (ppos_data)
8544 bidi_unshelve_cache (ppos_data, 1);
8545
8546 /* Restore the iterator settings altered at the beginning of this
8547 function. */
8548 it->glyph_row = saved_glyph_row;
8549 return result;
8550 }
8551
8552 /* For external use. */
8553 void
8554 move_it_in_display_line (struct it *it,
8555 EMACS_INT to_charpos, int to_x,
8556 enum move_operation_enum op)
8557 {
8558 if (it->line_wrap == WORD_WRAP
8559 && (op & MOVE_TO_X))
8560 {
8561 struct it save_it;
8562 void *save_data = NULL;
8563 int skip;
8564
8565 SAVE_IT (save_it, *it, save_data);
8566 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8567 /* When word-wrap is on, TO_X may lie past the end
8568 of a wrapped line. Then it->current is the
8569 character on the next line, so backtrack to the
8570 space before the wrap point. */
8571 if (skip == MOVE_LINE_CONTINUED)
8572 {
8573 int prev_x = max (it->current_x - 1, 0);
8574 RESTORE_IT (it, &save_it, save_data);
8575 move_it_in_display_line_to
8576 (it, -1, prev_x, MOVE_TO_X);
8577 }
8578 else
8579 bidi_unshelve_cache (save_data, 1);
8580 }
8581 else
8582 move_it_in_display_line_to (it, to_charpos, to_x, op);
8583 }
8584
8585
8586 /* Move IT forward until it satisfies one or more of the criteria in
8587 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8588
8589 OP is a bit-mask that specifies where to stop, and in particular,
8590 which of those four position arguments makes a difference. See the
8591 description of enum move_operation_enum.
8592
8593 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8594 screen line, this function will set IT to the next position that is
8595 displayed to the right of TO_CHARPOS on the screen. */
8596
8597 void
8598 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8599 {
8600 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8601 int line_height, line_start_x = 0, reached = 0;
8602 void *backup_data = NULL;
8603
8604 for (;;)
8605 {
8606 if (op & MOVE_TO_VPOS)
8607 {
8608 /* If no TO_CHARPOS and no TO_X specified, stop at the
8609 start of the line TO_VPOS. */
8610 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8611 {
8612 if (it->vpos == to_vpos)
8613 {
8614 reached = 1;
8615 break;
8616 }
8617 else
8618 skip = move_it_in_display_line_to (it, -1, -1, 0);
8619 }
8620 else
8621 {
8622 /* TO_VPOS >= 0 means stop at TO_X in the line at
8623 TO_VPOS, or at TO_POS, whichever comes first. */
8624 if (it->vpos == to_vpos)
8625 {
8626 reached = 2;
8627 break;
8628 }
8629
8630 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8631
8632 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8633 {
8634 reached = 3;
8635 break;
8636 }
8637 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8638 {
8639 /* We have reached TO_X but not in the line we want. */
8640 skip = move_it_in_display_line_to (it, to_charpos,
8641 -1, MOVE_TO_POS);
8642 if (skip == MOVE_POS_MATCH_OR_ZV)
8643 {
8644 reached = 4;
8645 break;
8646 }
8647 }
8648 }
8649 }
8650 else if (op & MOVE_TO_Y)
8651 {
8652 struct it it_backup;
8653
8654 if (it->line_wrap == WORD_WRAP)
8655 SAVE_IT (it_backup, *it, backup_data);
8656
8657 /* TO_Y specified means stop at TO_X in the line containing
8658 TO_Y---or at TO_CHARPOS if this is reached first. The
8659 problem is that we can't really tell whether the line
8660 contains TO_Y before we have completely scanned it, and
8661 this may skip past TO_X. What we do is to first scan to
8662 TO_X.
8663
8664 If TO_X is not specified, use a TO_X of zero. The reason
8665 is to make the outcome of this function more predictable.
8666 If we didn't use TO_X == 0, we would stop at the end of
8667 the line which is probably not what a caller would expect
8668 to happen. */
8669 skip = move_it_in_display_line_to
8670 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8671 (MOVE_TO_X | (op & MOVE_TO_POS)));
8672
8673 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8674 if (skip == MOVE_POS_MATCH_OR_ZV)
8675 reached = 5;
8676 else if (skip == MOVE_X_REACHED)
8677 {
8678 /* If TO_X was reached, we want to know whether TO_Y is
8679 in the line. We know this is the case if the already
8680 scanned glyphs make the line tall enough. Otherwise,
8681 we must check by scanning the rest of the line. */
8682 line_height = it->max_ascent + it->max_descent;
8683 if (to_y >= it->current_y
8684 && to_y < it->current_y + line_height)
8685 {
8686 reached = 6;
8687 break;
8688 }
8689 SAVE_IT (it_backup, *it, backup_data);
8690 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8691 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8692 op & MOVE_TO_POS);
8693 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8694 line_height = it->max_ascent + it->max_descent;
8695 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8696
8697 if (to_y >= it->current_y
8698 && to_y < it->current_y + line_height)
8699 {
8700 /* If TO_Y is in this line and TO_X was reached
8701 above, we scanned too far. We have to restore
8702 IT's settings to the ones before skipping. */
8703 RESTORE_IT (it, &it_backup, backup_data);
8704 reached = 6;
8705 }
8706 else
8707 {
8708 skip = skip2;
8709 if (skip == MOVE_POS_MATCH_OR_ZV)
8710 reached = 7;
8711 }
8712 }
8713 else
8714 {
8715 /* Check whether TO_Y is in this line. */
8716 line_height = it->max_ascent + it->max_descent;
8717 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8718
8719 if (to_y >= it->current_y
8720 && to_y < it->current_y + line_height)
8721 {
8722 /* When word-wrap is on, TO_X may lie past the end
8723 of a wrapped line. Then it->current is the
8724 character on the next line, so backtrack to the
8725 space before the wrap point. */
8726 if (skip == MOVE_LINE_CONTINUED
8727 && it->line_wrap == WORD_WRAP)
8728 {
8729 int prev_x = max (it->current_x - 1, 0);
8730 RESTORE_IT (it, &it_backup, backup_data);
8731 skip = move_it_in_display_line_to
8732 (it, -1, prev_x, MOVE_TO_X);
8733 }
8734 reached = 6;
8735 }
8736 }
8737
8738 if (reached)
8739 break;
8740 }
8741 else if (BUFFERP (it->object)
8742 && (it->method == GET_FROM_BUFFER
8743 || it->method == GET_FROM_STRETCH)
8744 && IT_CHARPOS (*it) >= to_charpos
8745 /* Under bidi iteration, a call to set_iterator_to_next
8746 can scan far beyond to_charpos if the initial
8747 portion of the next line needs to be reordered. In
8748 that case, give move_it_in_display_line_to another
8749 chance below. */
8750 && !(it->bidi_p
8751 && it->bidi_it.scan_dir == -1))
8752 skip = MOVE_POS_MATCH_OR_ZV;
8753 else
8754 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8755
8756 switch (skip)
8757 {
8758 case MOVE_POS_MATCH_OR_ZV:
8759 reached = 8;
8760 goto out;
8761
8762 case MOVE_NEWLINE_OR_CR:
8763 set_iterator_to_next (it, 1);
8764 it->continuation_lines_width = 0;
8765 break;
8766
8767 case MOVE_LINE_TRUNCATED:
8768 it->continuation_lines_width = 0;
8769 reseat_at_next_visible_line_start (it, 0);
8770 if ((op & MOVE_TO_POS) != 0
8771 && IT_CHARPOS (*it) > to_charpos)
8772 {
8773 reached = 9;
8774 goto out;
8775 }
8776 break;
8777
8778 case MOVE_LINE_CONTINUED:
8779 /* For continued lines ending in a tab, some of the glyphs
8780 associated with the tab are displayed on the current
8781 line. Since it->current_x does not include these glyphs,
8782 we use it->last_visible_x instead. */
8783 if (it->c == '\t')
8784 {
8785 it->continuation_lines_width += it->last_visible_x;
8786 /* When moving by vpos, ensure that the iterator really
8787 advances to the next line (bug#847, bug#969). Fixme:
8788 do we need to do this in other circumstances? */
8789 if (it->current_x != it->last_visible_x
8790 && (op & MOVE_TO_VPOS)
8791 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8792 {
8793 line_start_x = it->current_x + it->pixel_width
8794 - it->last_visible_x;
8795 set_iterator_to_next (it, 0);
8796 }
8797 }
8798 else
8799 it->continuation_lines_width += it->current_x;
8800 break;
8801
8802 default:
8803 abort ();
8804 }
8805
8806 /* Reset/increment for the next run. */
8807 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8808 it->current_x = line_start_x;
8809 line_start_x = 0;
8810 it->hpos = 0;
8811 it->current_y += it->max_ascent + it->max_descent;
8812 ++it->vpos;
8813 last_height = it->max_ascent + it->max_descent;
8814 last_max_ascent = it->max_ascent;
8815 it->max_ascent = it->max_descent = 0;
8816 }
8817
8818 out:
8819
8820 /* On text terminals, we may stop at the end of a line in the middle
8821 of a multi-character glyph. If the glyph itself is continued,
8822 i.e. it is actually displayed on the next line, don't treat this
8823 stopping point as valid; move to the next line instead (unless
8824 that brings us offscreen). */
8825 if (!FRAME_WINDOW_P (it->f)
8826 && op & MOVE_TO_POS
8827 && IT_CHARPOS (*it) == to_charpos
8828 && it->what == IT_CHARACTER
8829 && it->nglyphs > 1
8830 && it->line_wrap == WINDOW_WRAP
8831 && it->current_x == it->last_visible_x - 1
8832 && it->c != '\n'
8833 && it->c != '\t'
8834 && it->vpos < XFASTINT (it->w->window_end_vpos))
8835 {
8836 it->continuation_lines_width += it->current_x;
8837 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8838 it->current_y += it->max_ascent + it->max_descent;
8839 ++it->vpos;
8840 last_height = it->max_ascent + it->max_descent;
8841 last_max_ascent = it->max_ascent;
8842 }
8843
8844 if (backup_data)
8845 bidi_unshelve_cache (backup_data, 1);
8846
8847 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8848 }
8849
8850
8851 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8852
8853 If DY > 0, move IT backward at least that many pixels. DY = 0
8854 means move IT backward to the preceding line start or BEGV. This
8855 function may move over more than DY pixels if IT->current_y - DY
8856 ends up in the middle of a line; in this case IT->current_y will be
8857 set to the top of the line moved to. */
8858
8859 void
8860 move_it_vertically_backward (struct it *it, int dy)
8861 {
8862 int nlines, h;
8863 struct it it2, it3;
8864 void *it2data = NULL, *it3data = NULL;
8865 EMACS_INT start_pos;
8866
8867 move_further_back:
8868 xassert (dy >= 0);
8869
8870 start_pos = IT_CHARPOS (*it);
8871
8872 /* Estimate how many newlines we must move back. */
8873 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8874
8875 /* Set the iterator's position that many lines back. */
8876 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8877 back_to_previous_visible_line_start (it);
8878
8879 /* Reseat the iterator here. When moving backward, we don't want
8880 reseat to skip forward over invisible text, set up the iterator
8881 to deliver from overlay strings at the new position etc. So,
8882 use reseat_1 here. */
8883 reseat_1 (it, it->current.pos, 1);
8884
8885 /* We are now surely at a line start. */
8886 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8887 reordering is in effect. */
8888 it->continuation_lines_width = 0;
8889
8890 /* Move forward and see what y-distance we moved. First move to the
8891 start of the next line so that we get its height. We need this
8892 height to be able to tell whether we reached the specified
8893 y-distance. */
8894 SAVE_IT (it2, *it, it2data);
8895 it2.max_ascent = it2.max_descent = 0;
8896 do
8897 {
8898 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8899 MOVE_TO_POS | MOVE_TO_VPOS);
8900 }
8901 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8902 /* If we are in a display string which starts at START_POS,
8903 and that display string includes a newline, and we are
8904 right after that newline (i.e. at the beginning of a
8905 display line), exit the loop, because otherwise we will
8906 infloop, since move_it_to will see that it is already at
8907 START_POS and will not move. */
8908 || (it2.method == GET_FROM_STRING
8909 && IT_CHARPOS (it2) == start_pos
8910 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8911 xassert (IT_CHARPOS (*it) >= BEGV);
8912 SAVE_IT (it3, it2, it3data);
8913
8914 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8915 xassert (IT_CHARPOS (*it) >= BEGV);
8916 /* H is the actual vertical distance from the position in *IT
8917 and the starting position. */
8918 h = it2.current_y - it->current_y;
8919 /* NLINES is the distance in number of lines. */
8920 nlines = it2.vpos - it->vpos;
8921
8922 /* Correct IT's y and vpos position
8923 so that they are relative to the starting point. */
8924 it->vpos -= nlines;
8925 it->current_y -= h;
8926
8927 if (dy == 0)
8928 {
8929 /* DY == 0 means move to the start of the screen line. The
8930 value of nlines is > 0 if continuation lines were involved,
8931 or if the original IT position was at start of a line. */
8932 RESTORE_IT (it, it, it2data);
8933 if (nlines > 0)
8934 move_it_by_lines (it, nlines);
8935 /* The above code moves us to some position NLINES down,
8936 usually to its first glyph (leftmost in an L2R line), but
8937 that's not necessarily the start of the line, under bidi
8938 reordering. We want to get to the character position
8939 that is immediately after the newline of the previous
8940 line. */
8941 if (it->bidi_p
8942 && !it->continuation_lines_width
8943 && !STRINGP (it->string)
8944 && IT_CHARPOS (*it) > BEGV
8945 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8946 {
8947 EMACS_INT nl_pos =
8948 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8949
8950 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8951 }
8952 bidi_unshelve_cache (it3data, 1);
8953 }
8954 else
8955 {
8956 /* The y-position we try to reach, relative to *IT.
8957 Note that H has been subtracted in front of the if-statement. */
8958 int target_y = it->current_y + h - dy;
8959 int y0 = it3.current_y;
8960 int y1;
8961 int line_height;
8962
8963 RESTORE_IT (&it3, &it3, it3data);
8964 y1 = line_bottom_y (&it3);
8965 line_height = y1 - y0;
8966 RESTORE_IT (it, it, it2data);
8967 /* If we did not reach target_y, try to move further backward if
8968 we can. If we moved too far backward, try to move forward. */
8969 if (target_y < it->current_y
8970 /* This is heuristic. In a window that's 3 lines high, with
8971 a line height of 13 pixels each, recentering with point
8972 on the bottom line will try to move -39/2 = 19 pixels
8973 backward. Try to avoid moving into the first line. */
8974 && (it->current_y - target_y
8975 > min (window_box_height (it->w), line_height * 2 / 3))
8976 && IT_CHARPOS (*it) > BEGV)
8977 {
8978 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8979 target_y - it->current_y));
8980 dy = it->current_y - target_y;
8981 goto move_further_back;
8982 }
8983 else if (target_y >= it->current_y + line_height
8984 && IT_CHARPOS (*it) < ZV)
8985 {
8986 /* Should move forward by at least one line, maybe more.
8987
8988 Note: Calling move_it_by_lines can be expensive on
8989 terminal frames, where compute_motion is used (via
8990 vmotion) to do the job, when there are very long lines
8991 and truncate-lines is nil. That's the reason for
8992 treating terminal frames specially here. */
8993
8994 if (!FRAME_WINDOW_P (it->f))
8995 move_it_vertically (it, target_y - (it->current_y + line_height));
8996 else
8997 {
8998 do
8999 {
9000 move_it_by_lines (it, 1);
9001 }
9002 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9003 }
9004 }
9005 }
9006 }
9007
9008
9009 /* Move IT by a specified amount of pixel lines DY. DY negative means
9010 move backwards. DY = 0 means move to start of screen line. At the
9011 end, IT will be on the start of a screen line. */
9012
9013 void
9014 move_it_vertically (struct it *it, int dy)
9015 {
9016 if (dy <= 0)
9017 move_it_vertically_backward (it, -dy);
9018 else
9019 {
9020 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9021 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9022 MOVE_TO_POS | MOVE_TO_Y);
9023 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9024
9025 /* If buffer ends in ZV without a newline, move to the start of
9026 the line to satisfy the post-condition. */
9027 if (IT_CHARPOS (*it) == ZV
9028 && ZV > BEGV
9029 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9030 move_it_by_lines (it, 0);
9031 }
9032 }
9033
9034
9035 /* Move iterator IT past the end of the text line it is in. */
9036
9037 void
9038 move_it_past_eol (struct it *it)
9039 {
9040 enum move_it_result rc;
9041
9042 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9043 if (rc == MOVE_NEWLINE_OR_CR)
9044 set_iterator_to_next (it, 0);
9045 }
9046
9047
9048 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9049 negative means move up. DVPOS == 0 means move to the start of the
9050 screen line.
9051
9052 Optimization idea: If we would know that IT->f doesn't use
9053 a face with proportional font, we could be faster for
9054 truncate-lines nil. */
9055
9056 void
9057 move_it_by_lines (struct it *it, int dvpos)
9058 {
9059
9060 /* The commented-out optimization uses vmotion on terminals. This
9061 gives bad results, because elements like it->what, on which
9062 callers such as pos_visible_p rely, aren't updated. */
9063 /* struct position pos;
9064 if (!FRAME_WINDOW_P (it->f))
9065 {
9066 struct text_pos textpos;
9067
9068 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9069 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9070 reseat (it, textpos, 1);
9071 it->vpos += pos.vpos;
9072 it->current_y += pos.vpos;
9073 }
9074 else */
9075
9076 if (dvpos == 0)
9077 {
9078 /* DVPOS == 0 means move to the start of the screen line. */
9079 move_it_vertically_backward (it, 0);
9080 /* Let next call to line_bottom_y calculate real line height */
9081 last_height = 0;
9082 }
9083 else if (dvpos > 0)
9084 {
9085 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9086 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9087 {
9088 /* Only move to the next buffer position if we ended up in a
9089 string from display property, not in an overlay string
9090 (before-string or after-string). That is because the
9091 latter don't conceal the underlying buffer position, so
9092 we can ask to move the iterator to the exact position we
9093 are interested in. Note that, even if we are already at
9094 IT_CHARPOS (*it), the call below is not a no-op, as it
9095 will detect that we are at the end of the string, pop the
9096 iterator, and compute it->current_x and it->hpos
9097 correctly. */
9098 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9099 -1, -1, -1, MOVE_TO_POS);
9100 }
9101 }
9102 else
9103 {
9104 struct it it2;
9105 void *it2data = NULL;
9106 EMACS_INT start_charpos, i;
9107
9108 /* Start at the beginning of the screen line containing IT's
9109 position. This may actually move vertically backwards,
9110 in case of overlays, so adjust dvpos accordingly. */
9111 dvpos += it->vpos;
9112 move_it_vertically_backward (it, 0);
9113 dvpos -= it->vpos;
9114
9115 /* Go back -DVPOS visible lines and reseat the iterator there. */
9116 start_charpos = IT_CHARPOS (*it);
9117 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9118 back_to_previous_visible_line_start (it);
9119 reseat (it, it->current.pos, 1);
9120
9121 /* Move further back if we end up in a string or an image. */
9122 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9123 {
9124 /* First try to move to start of display line. */
9125 dvpos += it->vpos;
9126 move_it_vertically_backward (it, 0);
9127 dvpos -= it->vpos;
9128 if (IT_POS_VALID_AFTER_MOVE_P (it))
9129 break;
9130 /* If start of line is still in string or image,
9131 move further back. */
9132 back_to_previous_visible_line_start (it);
9133 reseat (it, it->current.pos, 1);
9134 dvpos--;
9135 }
9136
9137 it->current_x = it->hpos = 0;
9138
9139 /* Above call may have moved too far if continuation lines
9140 are involved. Scan forward and see if it did. */
9141 SAVE_IT (it2, *it, it2data);
9142 it2.vpos = it2.current_y = 0;
9143 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9144 it->vpos -= it2.vpos;
9145 it->current_y -= it2.current_y;
9146 it->current_x = it->hpos = 0;
9147
9148 /* If we moved too far back, move IT some lines forward. */
9149 if (it2.vpos > -dvpos)
9150 {
9151 int delta = it2.vpos + dvpos;
9152
9153 RESTORE_IT (&it2, &it2, it2data);
9154 SAVE_IT (it2, *it, it2data);
9155 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9156 /* Move back again if we got too far ahead. */
9157 if (IT_CHARPOS (*it) >= start_charpos)
9158 RESTORE_IT (it, &it2, it2data);
9159 else
9160 bidi_unshelve_cache (it2data, 1);
9161 }
9162 else
9163 RESTORE_IT (it, it, it2data);
9164 }
9165 }
9166
9167 /* Return 1 if IT points into the middle of a display vector. */
9168
9169 int
9170 in_display_vector_p (struct it *it)
9171 {
9172 return (it->method == GET_FROM_DISPLAY_VECTOR
9173 && it->current.dpvec_index > 0
9174 && it->dpvec + it->current.dpvec_index != it->dpend);
9175 }
9176
9177 \f
9178 /***********************************************************************
9179 Messages
9180 ***********************************************************************/
9181
9182
9183 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9184 to *Messages*. */
9185
9186 void
9187 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9188 {
9189 Lisp_Object args[3];
9190 Lisp_Object msg, fmt;
9191 char *buffer;
9192 EMACS_INT len;
9193 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9194 USE_SAFE_ALLOCA;
9195
9196 /* Do nothing if called asynchronously. Inserting text into
9197 a buffer may call after-change-functions and alike and
9198 that would means running Lisp asynchronously. */
9199 if (handling_signal)
9200 return;
9201
9202 fmt = msg = Qnil;
9203 GCPRO4 (fmt, msg, arg1, arg2);
9204
9205 args[0] = fmt = build_string (format);
9206 args[1] = arg1;
9207 args[2] = arg2;
9208 msg = Fformat (3, args);
9209
9210 len = SBYTES (msg) + 1;
9211 SAFE_ALLOCA (buffer, char *, len);
9212 memcpy (buffer, SDATA (msg), len);
9213
9214 message_dolog (buffer, len - 1, 1, 0);
9215 SAFE_FREE ();
9216
9217 UNGCPRO;
9218 }
9219
9220
9221 /* Output a newline in the *Messages* buffer if "needs" one. */
9222
9223 void
9224 message_log_maybe_newline (void)
9225 {
9226 if (message_log_need_newline)
9227 message_dolog ("", 0, 1, 0);
9228 }
9229
9230
9231 /* Add a string M of length NBYTES to the message log, optionally
9232 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9233 nonzero, means interpret the contents of M as multibyte. This
9234 function calls low-level routines in order to bypass text property
9235 hooks, etc. which might not be safe to run.
9236
9237 This may GC (insert may run before/after change hooks),
9238 so the buffer M must NOT point to a Lisp string. */
9239
9240 void
9241 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9242 {
9243 const unsigned char *msg = (const unsigned char *) m;
9244
9245 if (!NILP (Vmemory_full))
9246 return;
9247
9248 if (!NILP (Vmessage_log_max))
9249 {
9250 struct buffer *oldbuf;
9251 Lisp_Object oldpoint, oldbegv, oldzv;
9252 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9253 EMACS_INT point_at_end = 0;
9254 EMACS_INT zv_at_end = 0;
9255 Lisp_Object old_deactivate_mark, tem;
9256 struct gcpro gcpro1;
9257
9258 old_deactivate_mark = Vdeactivate_mark;
9259 oldbuf = current_buffer;
9260 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9261 BVAR (current_buffer, undo_list) = Qt;
9262
9263 oldpoint = message_dolog_marker1;
9264 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9265 oldbegv = message_dolog_marker2;
9266 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9267 oldzv = message_dolog_marker3;
9268 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9269 GCPRO1 (old_deactivate_mark);
9270
9271 if (PT == Z)
9272 point_at_end = 1;
9273 if (ZV == Z)
9274 zv_at_end = 1;
9275
9276 BEGV = BEG;
9277 BEGV_BYTE = BEG_BYTE;
9278 ZV = Z;
9279 ZV_BYTE = Z_BYTE;
9280 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9281
9282 /* Insert the string--maybe converting multibyte to single byte
9283 or vice versa, so that all the text fits the buffer. */
9284 if (multibyte
9285 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9286 {
9287 EMACS_INT i;
9288 int c, char_bytes;
9289 char work[1];
9290
9291 /* Convert a multibyte string to single-byte
9292 for the *Message* buffer. */
9293 for (i = 0; i < nbytes; i += char_bytes)
9294 {
9295 c = string_char_and_length (msg + i, &char_bytes);
9296 work[0] = (ASCII_CHAR_P (c)
9297 ? c
9298 : multibyte_char_to_unibyte (c));
9299 insert_1_both (work, 1, 1, 1, 0, 0);
9300 }
9301 }
9302 else if (! multibyte
9303 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9304 {
9305 EMACS_INT i;
9306 int c, char_bytes;
9307 unsigned char str[MAX_MULTIBYTE_LENGTH];
9308 /* Convert a single-byte string to multibyte
9309 for the *Message* buffer. */
9310 for (i = 0; i < nbytes; i++)
9311 {
9312 c = msg[i];
9313 MAKE_CHAR_MULTIBYTE (c);
9314 char_bytes = CHAR_STRING (c, str);
9315 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9316 }
9317 }
9318 else if (nbytes)
9319 insert_1 (m, nbytes, 1, 0, 0);
9320
9321 if (nlflag)
9322 {
9323 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9324 printmax_t dups;
9325 insert_1 ("\n", 1, 1, 0, 0);
9326
9327 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9328 this_bol = PT;
9329 this_bol_byte = PT_BYTE;
9330
9331 /* See if this line duplicates the previous one.
9332 If so, combine duplicates. */
9333 if (this_bol > BEG)
9334 {
9335 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9336 prev_bol = PT;
9337 prev_bol_byte = PT_BYTE;
9338
9339 dups = message_log_check_duplicate (prev_bol_byte,
9340 this_bol_byte);
9341 if (dups)
9342 {
9343 del_range_both (prev_bol, prev_bol_byte,
9344 this_bol, this_bol_byte, 0);
9345 if (dups > 1)
9346 {
9347 char dupstr[sizeof " [ times]"
9348 + INT_STRLEN_BOUND (printmax_t)];
9349 int duplen;
9350
9351 /* If you change this format, don't forget to also
9352 change message_log_check_duplicate. */
9353 sprintf (dupstr, " [%"pMd" times]", dups);
9354 duplen = strlen (dupstr);
9355 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9356 insert_1 (dupstr, duplen, 1, 0, 1);
9357 }
9358 }
9359 }
9360
9361 /* If we have more than the desired maximum number of lines
9362 in the *Messages* buffer now, delete the oldest ones.
9363 This is safe because we don't have undo in this buffer. */
9364
9365 if (NATNUMP (Vmessage_log_max))
9366 {
9367 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9368 -XFASTINT (Vmessage_log_max) - 1, 0);
9369 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9370 }
9371 }
9372 BEGV = XMARKER (oldbegv)->charpos;
9373 BEGV_BYTE = marker_byte_position (oldbegv);
9374
9375 if (zv_at_end)
9376 {
9377 ZV = Z;
9378 ZV_BYTE = Z_BYTE;
9379 }
9380 else
9381 {
9382 ZV = XMARKER (oldzv)->charpos;
9383 ZV_BYTE = marker_byte_position (oldzv);
9384 }
9385
9386 if (point_at_end)
9387 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9388 else
9389 /* We can't do Fgoto_char (oldpoint) because it will run some
9390 Lisp code. */
9391 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9392 XMARKER (oldpoint)->bytepos);
9393
9394 UNGCPRO;
9395 unchain_marker (XMARKER (oldpoint));
9396 unchain_marker (XMARKER (oldbegv));
9397 unchain_marker (XMARKER (oldzv));
9398
9399 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9400 set_buffer_internal (oldbuf);
9401 if (NILP (tem))
9402 windows_or_buffers_changed = old_windows_or_buffers_changed;
9403 message_log_need_newline = !nlflag;
9404 Vdeactivate_mark = old_deactivate_mark;
9405 }
9406 }
9407
9408
9409 /* We are at the end of the buffer after just having inserted a newline.
9410 (Note: We depend on the fact we won't be crossing the gap.)
9411 Check to see if the most recent message looks a lot like the previous one.
9412 Return 0 if different, 1 if the new one should just replace it, or a
9413 value N > 1 if we should also append " [N times]". */
9414
9415 static intmax_t
9416 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9417 {
9418 EMACS_INT i;
9419 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9420 int seen_dots = 0;
9421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9423
9424 for (i = 0; i < len; i++)
9425 {
9426 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9427 seen_dots = 1;
9428 if (p1[i] != p2[i])
9429 return seen_dots;
9430 }
9431 p1 += len;
9432 if (*p1 == '\n')
9433 return 2;
9434 if (*p1++ == ' ' && *p1++ == '[')
9435 {
9436 char *pend;
9437 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9438 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9439 return n+1;
9440 }
9441 return 0;
9442 }
9443 \f
9444
9445 /* Display an echo area message M with a specified length of NBYTES
9446 bytes. The string may include null characters. If M is 0, clear
9447 out any existing message, and let the mini-buffer text show
9448 through.
9449
9450 This may GC, so the buffer M must NOT point to a Lisp string. */
9451
9452 void
9453 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9454 {
9455 /* First flush out any partial line written with print. */
9456 message_log_maybe_newline ();
9457 if (m)
9458 message_dolog (m, nbytes, 1, multibyte);
9459 message2_nolog (m, nbytes, multibyte);
9460 }
9461
9462
9463 /* The non-logging counterpart of message2. */
9464
9465 void
9466 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9467 {
9468 struct frame *sf = SELECTED_FRAME ();
9469 message_enable_multibyte = multibyte;
9470
9471 if (FRAME_INITIAL_P (sf))
9472 {
9473 if (noninteractive_need_newline)
9474 putc ('\n', stderr);
9475 noninteractive_need_newline = 0;
9476 if (m)
9477 fwrite (m, nbytes, 1, stderr);
9478 if (cursor_in_echo_area == 0)
9479 fprintf (stderr, "\n");
9480 fflush (stderr);
9481 }
9482 /* A null message buffer means that the frame hasn't really been
9483 initialized yet. Error messages get reported properly by
9484 cmd_error, so this must be just an informative message; toss it. */
9485 else if (INTERACTIVE
9486 && sf->glyphs_initialized_p
9487 && FRAME_MESSAGE_BUF (sf))
9488 {
9489 Lisp_Object mini_window;
9490 struct frame *f;
9491
9492 /* Get the frame containing the mini-buffer
9493 that the selected frame is using. */
9494 mini_window = FRAME_MINIBUF_WINDOW (sf);
9495 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9496
9497 FRAME_SAMPLE_VISIBILITY (f);
9498 if (FRAME_VISIBLE_P (sf)
9499 && ! FRAME_VISIBLE_P (f))
9500 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9501
9502 if (m)
9503 {
9504 set_message (m, Qnil, nbytes, multibyte);
9505 if (minibuffer_auto_raise)
9506 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9507 }
9508 else
9509 clear_message (1, 1);
9510
9511 do_pending_window_change (0);
9512 echo_area_display (1);
9513 do_pending_window_change (0);
9514 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9515 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9516 }
9517 }
9518
9519
9520 /* Display an echo area message M with a specified length of NBYTES
9521 bytes. The string may include null characters. If M is not a
9522 string, clear out any existing message, and let the mini-buffer
9523 text show through.
9524
9525 This function cancels echoing. */
9526
9527 void
9528 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9529 {
9530 struct gcpro gcpro1;
9531
9532 GCPRO1 (m);
9533 clear_message (1,1);
9534 cancel_echoing ();
9535
9536 /* First flush out any partial line written with print. */
9537 message_log_maybe_newline ();
9538 if (STRINGP (m))
9539 {
9540 char *buffer;
9541 USE_SAFE_ALLOCA;
9542
9543 SAFE_ALLOCA (buffer, char *, nbytes);
9544 memcpy (buffer, SDATA (m), nbytes);
9545 message_dolog (buffer, nbytes, 1, multibyte);
9546 SAFE_FREE ();
9547 }
9548 message3_nolog (m, nbytes, multibyte);
9549
9550 UNGCPRO;
9551 }
9552
9553
9554 /* The non-logging version of message3.
9555 This does not cancel echoing, because it is used for echoing.
9556 Perhaps we need to make a separate function for echoing
9557 and make this cancel echoing. */
9558
9559 void
9560 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9561 {
9562 struct frame *sf = SELECTED_FRAME ();
9563 message_enable_multibyte = multibyte;
9564
9565 if (FRAME_INITIAL_P (sf))
9566 {
9567 if (noninteractive_need_newline)
9568 putc ('\n', stderr);
9569 noninteractive_need_newline = 0;
9570 if (STRINGP (m))
9571 fwrite (SDATA (m), nbytes, 1, stderr);
9572 if (cursor_in_echo_area == 0)
9573 fprintf (stderr, "\n");
9574 fflush (stderr);
9575 }
9576 /* A null message buffer means that the frame hasn't really been
9577 initialized yet. Error messages get reported properly by
9578 cmd_error, so this must be just an informative message; toss it. */
9579 else if (INTERACTIVE
9580 && sf->glyphs_initialized_p
9581 && FRAME_MESSAGE_BUF (sf))
9582 {
9583 Lisp_Object mini_window;
9584 Lisp_Object frame;
9585 struct frame *f;
9586
9587 /* Get the frame containing the mini-buffer
9588 that the selected frame is using. */
9589 mini_window = FRAME_MINIBUF_WINDOW (sf);
9590 frame = XWINDOW (mini_window)->frame;
9591 f = XFRAME (frame);
9592
9593 FRAME_SAMPLE_VISIBILITY (f);
9594 if (FRAME_VISIBLE_P (sf)
9595 && !FRAME_VISIBLE_P (f))
9596 Fmake_frame_visible (frame);
9597
9598 if (STRINGP (m) && SCHARS (m) > 0)
9599 {
9600 set_message (NULL, m, nbytes, multibyte);
9601 if (minibuffer_auto_raise)
9602 Fraise_frame (frame);
9603 /* Assume we are not echoing.
9604 (If we are, echo_now will override this.) */
9605 echo_message_buffer = Qnil;
9606 }
9607 else
9608 clear_message (1, 1);
9609
9610 do_pending_window_change (0);
9611 echo_area_display (1);
9612 do_pending_window_change (0);
9613 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9614 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9615 }
9616 }
9617
9618
9619 /* Display a null-terminated echo area message M. If M is 0, clear
9620 out any existing message, and let the mini-buffer text show through.
9621
9622 The buffer M must continue to exist until after the echo area gets
9623 cleared or some other message gets displayed there. Do not pass
9624 text that is stored in a Lisp string. Do not pass text in a buffer
9625 that was alloca'd. */
9626
9627 void
9628 message1 (const char *m)
9629 {
9630 message2 (m, (m ? strlen (m) : 0), 0);
9631 }
9632
9633
9634 /* The non-logging counterpart of message1. */
9635
9636 void
9637 message1_nolog (const char *m)
9638 {
9639 message2_nolog (m, (m ? strlen (m) : 0), 0);
9640 }
9641
9642 /* Display a message M which contains a single %s
9643 which gets replaced with STRING. */
9644
9645 void
9646 message_with_string (const char *m, Lisp_Object string, int log)
9647 {
9648 CHECK_STRING (string);
9649
9650 if (noninteractive)
9651 {
9652 if (m)
9653 {
9654 if (noninteractive_need_newline)
9655 putc ('\n', stderr);
9656 noninteractive_need_newline = 0;
9657 fprintf (stderr, m, SDATA (string));
9658 if (!cursor_in_echo_area)
9659 fprintf (stderr, "\n");
9660 fflush (stderr);
9661 }
9662 }
9663 else if (INTERACTIVE)
9664 {
9665 /* The frame whose minibuffer we're going to display the message on.
9666 It may be larger than the selected frame, so we need
9667 to use its buffer, not the selected frame's buffer. */
9668 Lisp_Object mini_window;
9669 struct frame *f, *sf = SELECTED_FRAME ();
9670
9671 /* Get the frame containing the minibuffer
9672 that the selected frame is using. */
9673 mini_window = FRAME_MINIBUF_WINDOW (sf);
9674 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9675
9676 /* A null message buffer means that the frame hasn't really been
9677 initialized yet. Error messages get reported properly by
9678 cmd_error, so this must be just an informative message; toss it. */
9679 if (FRAME_MESSAGE_BUF (f))
9680 {
9681 Lisp_Object args[2], msg;
9682 struct gcpro gcpro1, gcpro2;
9683
9684 args[0] = build_string (m);
9685 args[1] = msg = string;
9686 GCPRO2 (args[0], msg);
9687 gcpro1.nvars = 2;
9688
9689 msg = Fformat (2, args);
9690
9691 if (log)
9692 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9693 else
9694 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9695
9696 UNGCPRO;
9697
9698 /* Print should start at the beginning of the message
9699 buffer next time. */
9700 message_buf_print = 0;
9701 }
9702 }
9703 }
9704
9705
9706 /* Dump an informative message to the minibuf. If M is 0, clear out
9707 any existing message, and let the mini-buffer text show through. */
9708
9709 static void
9710 vmessage (const char *m, va_list ap)
9711 {
9712 if (noninteractive)
9713 {
9714 if (m)
9715 {
9716 if (noninteractive_need_newline)
9717 putc ('\n', stderr);
9718 noninteractive_need_newline = 0;
9719 vfprintf (stderr, m, ap);
9720 if (cursor_in_echo_area == 0)
9721 fprintf (stderr, "\n");
9722 fflush (stderr);
9723 }
9724 }
9725 else if (INTERACTIVE)
9726 {
9727 /* The frame whose mini-buffer we're going to display the message
9728 on. It may be larger than the selected frame, so we need to
9729 use its buffer, not the selected frame's buffer. */
9730 Lisp_Object mini_window;
9731 struct frame *f, *sf = SELECTED_FRAME ();
9732
9733 /* Get the frame containing the mini-buffer
9734 that the selected frame is using. */
9735 mini_window = FRAME_MINIBUF_WINDOW (sf);
9736 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9737
9738 /* A null message buffer means that the frame hasn't really been
9739 initialized yet. Error messages get reported properly by
9740 cmd_error, so this must be just an informative message; toss
9741 it. */
9742 if (FRAME_MESSAGE_BUF (f))
9743 {
9744 if (m)
9745 {
9746 ptrdiff_t len;
9747
9748 len = doprnt (FRAME_MESSAGE_BUF (f),
9749 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9750
9751 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9752 }
9753 else
9754 message1 (0);
9755
9756 /* Print should start at the beginning of the message
9757 buffer next time. */
9758 message_buf_print = 0;
9759 }
9760 }
9761 }
9762
9763 void
9764 message (const char *m, ...)
9765 {
9766 va_list ap;
9767 va_start (ap, m);
9768 vmessage (m, ap);
9769 va_end (ap);
9770 }
9771
9772
9773 #if 0
9774 /* The non-logging version of message. */
9775
9776 void
9777 message_nolog (const char *m, ...)
9778 {
9779 Lisp_Object old_log_max;
9780 va_list ap;
9781 va_start (ap, m);
9782 old_log_max = Vmessage_log_max;
9783 Vmessage_log_max = Qnil;
9784 vmessage (m, ap);
9785 Vmessage_log_max = old_log_max;
9786 va_end (ap);
9787 }
9788 #endif
9789
9790
9791 /* Display the current message in the current mini-buffer. This is
9792 only called from error handlers in process.c, and is not time
9793 critical. */
9794
9795 void
9796 update_echo_area (void)
9797 {
9798 if (!NILP (echo_area_buffer[0]))
9799 {
9800 Lisp_Object string;
9801 string = Fcurrent_message ();
9802 message3 (string, SBYTES (string),
9803 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9804 }
9805 }
9806
9807
9808 /* Make sure echo area buffers in `echo_buffers' are live.
9809 If they aren't, make new ones. */
9810
9811 static void
9812 ensure_echo_area_buffers (void)
9813 {
9814 int i;
9815
9816 for (i = 0; i < 2; ++i)
9817 if (!BUFFERP (echo_buffer[i])
9818 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9819 {
9820 char name[30];
9821 Lisp_Object old_buffer;
9822 int j;
9823
9824 old_buffer = echo_buffer[i];
9825 sprintf (name, " *Echo Area %d*", i);
9826 echo_buffer[i] = Fget_buffer_create (build_string (name));
9827 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9828 /* to force word wrap in echo area -
9829 it was decided to postpone this*/
9830 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9831
9832 for (j = 0; j < 2; ++j)
9833 if (EQ (old_buffer, echo_area_buffer[j]))
9834 echo_area_buffer[j] = echo_buffer[i];
9835 }
9836 }
9837
9838
9839 /* Call FN with args A1..A4 with either the current or last displayed
9840 echo_area_buffer as current buffer.
9841
9842 WHICH zero means use the current message buffer
9843 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9844 from echo_buffer[] and clear it.
9845
9846 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9847 suitable buffer from echo_buffer[] and clear it.
9848
9849 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9850 that the current message becomes the last displayed one, make
9851 choose a suitable buffer for echo_area_buffer[0], and clear it.
9852
9853 Value is what FN returns. */
9854
9855 static int
9856 with_echo_area_buffer (struct window *w, int which,
9857 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9858 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9859 {
9860 Lisp_Object buffer;
9861 int this_one, the_other, clear_buffer_p, rc;
9862 int count = SPECPDL_INDEX ();
9863
9864 /* If buffers aren't live, make new ones. */
9865 ensure_echo_area_buffers ();
9866
9867 clear_buffer_p = 0;
9868
9869 if (which == 0)
9870 this_one = 0, the_other = 1;
9871 else if (which > 0)
9872 this_one = 1, the_other = 0;
9873 else
9874 {
9875 this_one = 0, the_other = 1;
9876 clear_buffer_p = 1;
9877
9878 /* We need a fresh one in case the current echo buffer equals
9879 the one containing the last displayed echo area message. */
9880 if (!NILP (echo_area_buffer[this_one])
9881 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9882 echo_area_buffer[this_one] = Qnil;
9883 }
9884
9885 /* Choose a suitable buffer from echo_buffer[] is we don't
9886 have one. */
9887 if (NILP (echo_area_buffer[this_one]))
9888 {
9889 echo_area_buffer[this_one]
9890 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9891 ? echo_buffer[the_other]
9892 : echo_buffer[this_one]);
9893 clear_buffer_p = 1;
9894 }
9895
9896 buffer = echo_area_buffer[this_one];
9897
9898 /* Don't get confused by reusing the buffer used for echoing
9899 for a different purpose. */
9900 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9901 cancel_echoing ();
9902
9903 record_unwind_protect (unwind_with_echo_area_buffer,
9904 with_echo_area_buffer_unwind_data (w));
9905
9906 /* Make the echo area buffer current. Note that for display
9907 purposes, it is not necessary that the displayed window's buffer
9908 == current_buffer, except for text property lookup. So, let's
9909 only set that buffer temporarily here without doing a full
9910 Fset_window_buffer. We must also change w->pointm, though,
9911 because otherwise an assertions in unshow_buffer fails, and Emacs
9912 aborts. */
9913 set_buffer_internal_1 (XBUFFER (buffer));
9914 if (w)
9915 {
9916 w->buffer = buffer;
9917 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9918 }
9919
9920 BVAR (current_buffer, undo_list) = Qt;
9921 BVAR (current_buffer, read_only) = Qnil;
9922 specbind (Qinhibit_read_only, Qt);
9923 specbind (Qinhibit_modification_hooks, Qt);
9924
9925 if (clear_buffer_p && Z > BEG)
9926 del_range (BEG, Z);
9927
9928 xassert (BEGV >= BEG);
9929 xassert (ZV <= Z && ZV >= BEGV);
9930
9931 rc = fn (a1, a2, a3, a4);
9932
9933 xassert (BEGV >= BEG);
9934 xassert (ZV <= Z && ZV >= BEGV);
9935
9936 unbind_to (count, Qnil);
9937 return rc;
9938 }
9939
9940
9941 /* Save state that should be preserved around the call to the function
9942 FN called in with_echo_area_buffer. */
9943
9944 static Lisp_Object
9945 with_echo_area_buffer_unwind_data (struct window *w)
9946 {
9947 int i = 0;
9948 Lisp_Object vector, tmp;
9949
9950 /* Reduce consing by keeping one vector in
9951 Vwith_echo_area_save_vector. */
9952 vector = Vwith_echo_area_save_vector;
9953 Vwith_echo_area_save_vector = Qnil;
9954
9955 if (NILP (vector))
9956 vector = Fmake_vector (make_number (7), Qnil);
9957
9958 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9959 ASET (vector, i, Vdeactivate_mark); ++i;
9960 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9961
9962 if (w)
9963 {
9964 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9965 ASET (vector, i, w->buffer); ++i;
9966 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9967 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9968 }
9969 else
9970 {
9971 int end = i + 4;
9972 for (; i < end; ++i)
9973 ASET (vector, i, Qnil);
9974 }
9975
9976 xassert (i == ASIZE (vector));
9977 return vector;
9978 }
9979
9980
9981 /* Restore global state from VECTOR which was created by
9982 with_echo_area_buffer_unwind_data. */
9983
9984 static Lisp_Object
9985 unwind_with_echo_area_buffer (Lisp_Object vector)
9986 {
9987 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9988 Vdeactivate_mark = AREF (vector, 1);
9989 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9990
9991 if (WINDOWP (AREF (vector, 3)))
9992 {
9993 struct window *w;
9994 Lisp_Object buffer, charpos, bytepos;
9995
9996 w = XWINDOW (AREF (vector, 3));
9997 buffer = AREF (vector, 4);
9998 charpos = AREF (vector, 5);
9999 bytepos = AREF (vector, 6);
10000
10001 w->buffer = buffer;
10002 set_marker_both (w->pointm, buffer,
10003 XFASTINT (charpos), XFASTINT (bytepos));
10004 }
10005
10006 Vwith_echo_area_save_vector = vector;
10007 return Qnil;
10008 }
10009
10010
10011 /* Set up the echo area for use by print functions. MULTIBYTE_P
10012 non-zero means we will print multibyte. */
10013
10014 void
10015 setup_echo_area_for_printing (int multibyte_p)
10016 {
10017 /* If we can't find an echo area any more, exit. */
10018 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10019 Fkill_emacs (Qnil);
10020
10021 ensure_echo_area_buffers ();
10022
10023 if (!message_buf_print)
10024 {
10025 /* A message has been output since the last time we printed.
10026 Choose a fresh echo area buffer. */
10027 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10028 echo_area_buffer[0] = echo_buffer[1];
10029 else
10030 echo_area_buffer[0] = echo_buffer[0];
10031
10032 /* Switch to that buffer and clear it. */
10033 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10034 BVAR (current_buffer, truncate_lines) = Qnil;
10035
10036 if (Z > BEG)
10037 {
10038 int count = SPECPDL_INDEX ();
10039 specbind (Qinhibit_read_only, Qt);
10040 /* Note that undo recording is always disabled. */
10041 del_range (BEG, Z);
10042 unbind_to (count, Qnil);
10043 }
10044 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10045
10046 /* Set up the buffer for the multibyteness we need. */
10047 if (multibyte_p
10048 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10049 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10050
10051 /* Raise the frame containing the echo area. */
10052 if (minibuffer_auto_raise)
10053 {
10054 struct frame *sf = SELECTED_FRAME ();
10055 Lisp_Object mini_window;
10056 mini_window = FRAME_MINIBUF_WINDOW (sf);
10057 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10058 }
10059
10060 message_log_maybe_newline ();
10061 message_buf_print = 1;
10062 }
10063 else
10064 {
10065 if (NILP (echo_area_buffer[0]))
10066 {
10067 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10068 echo_area_buffer[0] = echo_buffer[1];
10069 else
10070 echo_area_buffer[0] = echo_buffer[0];
10071 }
10072
10073 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10074 {
10075 /* Someone switched buffers between print requests. */
10076 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10077 BVAR (current_buffer, truncate_lines) = Qnil;
10078 }
10079 }
10080 }
10081
10082
10083 /* Display an echo area message in window W. Value is non-zero if W's
10084 height is changed. If display_last_displayed_message_p is
10085 non-zero, display the message that was last displayed, otherwise
10086 display the current message. */
10087
10088 static int
10089 display_echo_area (struct window *w)
10090 {
10091 int i, no_message_p, window_height_changed_p, count;
10092
10093 /* Temporarily disable garbage collections while displaying the echo
10094 area. This is done because a GC can print a message itself.
10095 That message would modify the echo area buffer's contents while a
10096 redisplay of the buffer is going on, and seriously confuse
10097 redisplay. */
10098 count = inhibit_garbage_collection ();
10099
10100 /* If there is no message, we must call display_echo_area_1
10101 nevertheless because it resizes the window. But we will have to
10102 reset the echo_area_buffer in question to nil at the end because
10103 with_echo_area_buffer will sets it to an empty buffer. */
10104 i = display_last_displayed_message_p ? 1 : 0;
10105 no_message_p = NILP (echo_area_buffer[i]);
10106
10107 window_height_changed_p
10108 = with_echo_area_buffer (w, display_last_displayed_message_p,
10109 display_echo_area_1,
10110 (intptr_t) w, Qnil, 0, 0);
10111
10112 if (no_message_p)
10113 echo_area_buffer[i] = Qnil;
10114
10115 unbind_to (count, Qnil);
10116 return window_height_changed_p;
10117 }
10118
10119
10120 /* Helper for display_echo_area. Display the current buffer which
10121 contains the current echo area message in window W, a mini-window,
10122 a pointer to which is passed in A1. A2..A4 are currently not used.
10123 Change the height of W so that all of the message is displayed.
10124 Value is non-zero if height of W was changed. */
10125
10126 static int
10127 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10128 {
10129 intptr_t i1 = a1;
10130 struct window *w = (struct window *) i1;
10131 Lisp_Object window;
10132 struct text_pos start;
10133 int window_height_changed_p = 0;
10134
10135 /* Do this before displaying, so that we have a large enough glyph
10136 matrix for the display. If we can't get enough space for the
10137 whole text, display the last N lines. That works by setting w->start. */
10138 window_height_changed_p = resize_mini_window (w, 0);
10139
10140 /* Use the starting position chosen by resize_mini_window. */
10141 SET_TEXT_POS_FROM_MARKER (start, w->start);
10142
10143 /* Display. */
10144 clear_glyph_matrix (w->desired_matrix);
10145 XSETWINDOW (window, w);
10146 try_window (window, start, 0);
10147
10148 return window_height_changed_p;
10149 }
10150
10151
10152 /* Resize the echo area window to exactly the size needed for the
10153 currently displayed message, if there is one. If a mini-buffer
10154 is active, don't shrink it. */
10155
10156 void
10157 resize_echo_area_exactly (void)
10158 {
10159 if (BUFFERP (echo_area_buffer[0])
10160 && WINDOWP (echo_area_window))
10161 {
10162 struct window *w = XWINDOW (echo_area_window);
10163 int resized_p;
10164 Lisp_Object resize_exactly;
10165
10166 if (minibuf_level == 0)
10167 resize_exactly = Qt;
10168 else
10169 resize_exactly = Qnil;
10170
10171 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10172 (intptr_t) w, resize_exactly,
10173 0, 0);
10174 if (resized_p)
10175 {
10176 ++windows_or_buffers_changed;
10177 ++update_mode_lines;
10178 redisplay_internal ();
10179 }
10180 }
10181 }
10182
10183
10184 /* Callback function for with_echo_area_buffer, when used from
10185 resize_echo_area_exactly. A1 contains a pointer to the window to
10186 resize, EXACTLY non-nil means resize the mini-window exactly to the
10187 size of the text displayed. A3 and A4 are not used. Value is what
10188 resize_mini_window returns. */
10189
10190 static int
10191 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10192 {
10193 intptr_t i1 = a1;
10194 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10195 }
10196
10197
10198 /* Resize mini-window W to fit the size of its contents. EXACT_P
10199 means size the window exactly to the size needed. Otherwise, it's
10200 only enlarged until W's buffer is empty.
10201
10202 Set W->start to the right place to begin display. If the whole
10203 contents fit, start at the beginning. Otherwise, start so as
10204 to make the end of the contents appear. This is particularly
10205 important for y-or-n-p, but seems desirable generally.
10206
10207 Value is non-zero if the window height has been changed. */
10208
10209 int
10210 resize_mini_window (struct window *w, int exact_p)
10211 {
10212 struct frame *f = XFRAME (w->frame);
10213 int window_height_changed_p = 0;
10214
10215 xassert (MINI_WINDOW_P (w));
10216
10217 /* By default, start display at the beginning. */
10218 set_marker_both (w->start, w->buffer,
10219 BUF_BEGV (XBUFFER (w->buffer)),
10220 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10221
10222 /* Don't resize windows while redisplaying a window; it would
10223 confuse redisplay functions when the size of the window they are
10224 displaying changes from under them. Such a resizing can happen,
10225 for instance, when which-func prints a long message while
10226 we are running fontification-functions. We're running these
10227 functions with safe_call which binds inhibit-redisplay to t. */
10228 if (!NILP (Vinhibit_redisplay))
10229 return 0;
10230
10231 /* Nil means don't try to resize. */
10232 if (NILP (Vresize_mini_windows)
10233 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10234 return 0;
10235
10236 if (!FRAME_MINIBUF_ONLY_P (f))
10237 {
10238 struct it it;
10239 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10240 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10241 int height, max_height;
10242 int unit = FRAME_LINE_HEIGHT (f);
10243 struct text_pos start;
10244 struct buffer *old_current_buffer = NULL;
10245
10246 if (current_buffer != XBUFFER (w->buffer))
10247 {
10248 old_current_buffer = current_buffer;
10249 set_buffer_internal (XBUFFER (w->buffer));
10250 }
10251
10252 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10253
10254 /* Compute the max. number of lines specified by the user. */
10255 if (FLOATP (Vmax_mini_window_height))
10256 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10257 else if (INTEGERP (Vmax_mini_window_height))
10258 max_height = XINT (Vmax_mini_window_height);
10259 else
10260 max_height = total_height / 4;
10261
10262 /* Correct that max. height if it's bogus. */
10263 max_height = max (1, max_height);
10264 max_height = min (total_height, max_height);
10265
10266 /* Find out the height of the text in the window. */
10267 if (it.line_wrap == TRUNCATE)
10268 height = 1;
10269 else
10270 {
10271 last_height = 0;
10272 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10273 if (it.max_ascent == 0 && it.max_descent == 0)
10274 height = it.current_y + last_height;
10275 else
10276 height = it.current_y + it.max_ascent + it.max_descent;
10277 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10278 height = (height + unit - 1) / unit;
10279 }
10280
10281 /* Compute a suitable window start. */
10282 if (height > max_height)
10283 {
10284 height = max_height;
10285 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10286 move_it_vertically_backward (&it, (height - 1) * unit);
10287 start = it.current.pos;
10288 }
10289 else
10290 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10291 SET_MARKER_FROM_TEXT_POS (w->start, start);
10292
10293 if (EQ (Vresize_mini_windows, Qgrow_only))
10294 {
10295 /* Let it grow only, until we display an empty message, in which
10296 case the window shrinks again. */
10297 if (height > WINDOW_TOTAL_LINES (w))
10298 {
10299 int old_height = WINDOW_TOTAL_LINES (w);
10300 freeze_window_starts (f, 1);
10301 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10302 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10303 }
10304 else if (height < WINDOW_TOTAL_LINES (w)
10305 && (exact_p || BEGV == ZV))
10306 {
10307 int old_height = WINDOW_TOTAL_LINES (w);
10308 freeze_window_starts (f, 0);
10309 shrink_mini_window (w);
10310 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10311 }
10312 }
10313 else
10314 {
10315 /* Always resize to exact size needed. */
10316 if (height > WINDOW_TOTAL_LINES (w))
10317 {
10318 int old_height = WINDOW_TOTAL_LINES (w);
10319 freeze_window_starts (f, 1);
10320 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10321 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10322 }
10323 else if (height < WINDOW_TOTAL_LINES (w))
10324 {
10325 int old_height = WINDOW_TOTAL_LINES (w);
10326 freeze_window_starts (f, 0);
10327 shrink_mini_window (w);
10328
10329 if (height)
10330 {
10331 freeze_window_starts (f, 1);
10332 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10333 }
10334
10335 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10336 }
10337 }
10338
10339 if (old_current_buffer)
10340 set_buffer_internal (old_current_buffer);
10341 }
10342
10343 return window_height_changed_p;
10344 }
10345
10346
10347 /* Value is the current message, a string, or nil if there is no
10348 current message. */
10349
10350 Lisp_Object
10351 current_message (void)
10352 {
10353 Lisp_Object msg;
10354
10355 if (!BUFFERP (echo_area_buffer[0]))
10356 msg = Qnil;
10357 else
10358 {
10359 with_echo_area_buffer (0, 0, current_message_1,
10360 (intptr_t) &msg, Qnil, 0, 0);
10361 if (NILP (msg))
10362 echo_area_buffer[0] = Qnil;
10363 }
10364
10365 return msg;
10366 }
10367
10368
10369 static int
10370 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10371 {
10372 intptr_t i1 = a1;
10373 Lisp_Object *msg = (Lisp_Object *) i1;
10374
10375 if (Z > BEG)
10376 *msg = make_buffer_string (BEG, Z, 1);
10377 else
10378 *msg = Qnil;
10379 return 0;
10380 }
10381
10382
10383 /* Push the current message on Vmessage_stack for later restoration
10384 by restore_message. Value is non-zero if the current message isn't
10385 empty. This is a relatively infrequent operation, so it's not
10386 worth optimizing. */
10387
10388 int
10389 push_message (void)
10390 {
10391 Lisp_Object msg;
10392 msg = current_message ();
10393 Vmessage_stack = Fcons (msg, Vmessage_stack);
10394 return STRINGP (msg);
10395 }
10396
10397
10398 /* Restore message display from the top of Vmessage_stack. */
10399
10400 void
10401 restore_message (void)
10402 {
10403 Lisp_Object msg;
10404
10405 xassert (CONSP (Vmessage_stack));
10406 msg = XCAR (Vmessage_stack);
10407 if (STRINGP (msg))
10408 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10409 else
10410 message3_nolog (msg, 0, 0);
10411 }
10412
10413
10414 /* Handler for record_unwind_protect calling pop_message. */
10415
10416 Lisp_Object
10417 pop_message_unwind (Lisp_Object dummy)
10418 {
10419 pop_message ();
10420 return Qnil;
10421 }
10422
10423 /* Pop the top-most entry off Vmessage_stack. */
10424
10425 static void
10426 pop_message (void)
10427 {
10428 xassert (CONSP (Vmessage_stack));
10429 Vmessage_stack = XCDR (Vmessage_stack);
10430 }
10431
10432
10433 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10434 exits. If the stack is not empty, we have a missing pop_message
10435 somewhere. */
10436
10437 void
10438 check_message_stack (void)
10439 {
10440 if (!NILP (Vmessage_stack))
10441 abort ();
10442 }
10443
10444
10445 /* Truncate to NCHARS what will be displayed in the echo area the next
10446 time we display it---but don't redisplay it now. */
10447
10448 void
10449 truncate_echo_area (EMACS_INT nchars)
10450 {
10451 if (nchars == 0)
10452 echo_area_buffer[0] = Qnil;
10453 /* A null message buffer means that the frame hasn't really been
10454 initialized yet. Error messages get reported properly by
10455 cmd_error, so this must be just an informative message; toss it. */
10456 else if (!noninteractive
10457 && INTERACTIVE
10458 && !NILP (echo_area_buffer[0]))
10459 {
10460 struct frame *sf = SELECTED_FRAME ();
10461 if (FRAME_MESSAGE_BUF (sf))
10462 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10463 }
10464 }
10465
10466
10467 /* Helper function for truncate_echo_area. Truncate the current
10468 message to at most NCHARS characters. */
10469
10470 static int
10471 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10472 {
10473 if (BEG + nchars < Z)
10474 del_range (BEG + nchars, Z);
10475 if (Z == BEG)
10476 echo_area_buffer[0] = Qnil;
10477 return 0;
10478 }
10479
10480
10481 /* Set the current message to a substring of S or STRING.
10482
10483 If STRING is a Lisp string, set the message to the first NBYTES
10484 bytes from STRING. NBYTES zero means use the whole string. If
10485 STRING is multibyte, the message will be displayed multibyte.
10486
10487 If S is not null, set the message to the first LEN bytes of S. LEN
10488 zero means use the whole string. MULTIBYTE_P non-zero means S is
10489 multibyte. Display the message multibyte in that case.
10490
10491 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10492 to t before calling set_message_1 (which calls insert).
10493 */
10494
10495 static void
10496 set_message (const char *s, Lisp_Object string,
10497 EMACS_INT nbytes, int multibyte_p)
10498 {
10499 message_enable_multibyte
10500 = ((s && multibyte_p)
10501 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10502
10503 with_echo_area_buffer (0, -1, set_message_1,
10504 (intptr_t) s, string, nbytes, multibyte_p);
10505 message_buf_print = 0;
10506 help_echo_showing_p = 0;
10507 }
10508
10509
10510 /* Helper function for set_message. Arguments have the same meaning
10511 as there, with A1 corresponding to S and A2 corresponding to STRING
10512 This function is called with the echo area buffer being
10513 current. */
10514
10515 static int
10516 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10517 {
10518 intptr_t i1 = a1;
10519 const char *s = (const char *) i1;
10520 const unsigned char *msg = (const unsigned char *) s;
10521 Lisp_Object string = a2;
10522
10523 /* Change multibyteness of the echo buffer appropriately. */
10524 if (message_enable_multibyte
10525 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10526 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10527
10528 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10529 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10530 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10531
10532 /* Insert new message at BEG. */
10533 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10534
10535 if (STRINGP (string))
10536 {
10537 EMACS_INT nchars;
10538
10539 if (nbytes == 0)
10540 nbytes = SBYTES (string);
10541 nchars = string_byte_to_char (string, nbytes);
10542
10543 /* This function takes care of single/multibyte conversion. We
10544 just have to ensure that the echo area buffer has the right
10545 setting of enable_multibyte_characters. */
10546 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10547 }
10548 else if (s)
10549 {
10550 if (nbytes == 0)
10551 nbytes = strlen (s);
10552
10553 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10554 {
10555 /* Convert from multi-byte to single-byte. */
10556 EMACS_INT i;
10557 int c, n;
10558 char work[1];
10559
10560 /* Convert a multibyte string to single-byte. */
10561 for (i = 0; i < nbytes; i += n)
10562 {
10563 c = string_char_and_length (msg + i, &n);
10564 work[0] = (ASCII_CHAR_P (c)
10565 ? c
10566 : multibyte_char_to_unibyte (c));
10567 insert_1_both (work, 1, 1, 1, 0, 0);
10568 }
10569 }
10570 else if (!multibyte_p
10571 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10572 {
10573 /* Convert from single-byte to multi-byte. */
10574 EMACS_INT i;
10575 int c, n;
10576 unsigned char str[MAX_MULTIBYTE_LENGTH];
10577
10578 /* Convert a single-byte string to multibyte. */
10579 for (i = 0; i < nbytes; i++)
10580 {
10581 c = msg[i];
10582 MAKE_CHAR_MULTIBYTE (c);
10583 n = CHAR_STRING (c, str);
10584 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10585 }
10586 }
10587 else
10588 insert_1 (s, nbytes, 1, 0, 0);
10589 }
10590
10591 return 0;
10592 }
10593
10594
10595 /* Clear messages. CURRENT_P non-zero means clear the current
10596 message. LAST_DISPLAYED_P non-zero means clear the message
10597 last displayed. */
10598
10599 void
10600 clear_message (int current_p, int last_displayed_p)
10601 {
10602 if (current_p)
10603 {
10604 echo_area_buffer[0] = Qnil;
10605 message_cleared_p = 1;
10606 }
10607
10608 if (last_displayed_p)
10609 echo_area_buffer[1] = Qnil;
10610
10611 message_buf_print = 0;
10612 }
10613
10614 /* Clear garbaged frames.
10615
10616 This function is used where the old redisplay called
10617 redraw_garbaged_frames which in turn called redraw_frame which in
10618 turn called clear_frame. The call to clear_frame was a source of
10619 flickering. I believe a clear_frame is not necessary. It should
10620 suffice in the new redisplay to invalidate all current matrices,
10621 and ensure a complete redisplay of all windows. */
10622
10623 static void
10624 clear_garbaged_frames (void)
10625 {
10626 if (frame_garbaged)
10627 {
10628 Lisp_Object tail, frame;
10629 int changed_count = 0;
10630
10631 FOR_EACH_FRAME (tail, frame)
10632 {
10633 struct frame *f = XFRAME (frame);
10634
10635 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10636 {
10637 if (f->resized_p)
10638 {
10639 Fredraw_frame (frame);
10640 f->force_flush_display_p = 1;
10641 }
10642 clear_current_matrices (f);
10643 changed_count++;
10644 f->garbaged = 0;
10645 f->resized_p = 0;
10646 }
10647 }
10648
10649 frame_garbaged = 0;
10650 if (changed_count)
10651 ++windows_or_buffers_changed;
10652 }
10653 }
10654
10655
10656 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10657 is non-zero update selected_frame. Value is non-zero if the
10658 mini-windows height has been changed. */
10659
10660 static int
10661 echo_area_display (int update_frame_p)
10662 {
10663 Lisp_Object mini_window;
10664 struct window *w;
10665 struct frame *f;
10666 int window_height_changed_p = 0;
10667 struct frame *sf = SELECTED_FRAME ();
10668
10669 mini_window = FRAME_MINIBUF_WINDOW (sf);
10670 w = XWINDOW (mini_window);
10671 f = XFRAME (WINDOW_FRAME (w));
10672
10673 /* Don't display if frame is invisible or not yet initialized. */
10674 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10675 return 0;
10676
10677 #ifdef HAVE_WINDOW_SYSTEM
10678 /* When Emacs starts, selected_frame may be the initial terminal
10679 frame. If we let this through, a message would be displayed on
10680 the terminal. */
10681 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10682 return 0;
10683 #endif /* HAVE_WINDOW_SYSTEM */
10684
10685 /* Redraw garbaged frames. */
10686 if (frame_garbaged)
10687 clear_garbaged_frames ();
10688
10689 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10690 {
10691 echo_area_window = mini_window;
10692 window_height_changed_p = display_echo_area (w);
10693 w->must_be_updated_p = 1;
10694
10695 /* Update the display, unless called from redisplay_internal.
10696 Also don't update the screen during redisplay itself. The
10697 update will happen at the end of redisplay, and an update
10698 here could cause confusion. */
10699 if (update_frame_p && !redisplaying_p)
10700 {
10701 int n = 0;
10702
10703 /* If the display update has been interrupted by pending
10704 input, update mode lines in the frame. Due to the
10705 pending input, it might have been that redisplay hasn't
10706 been called, so that mode lines above the echo area are
10707 garbaged. This looks odd, so we prevent it here. */
10708 if (!display_completed)
10709 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10710
10711 if (window_height_changed_p
10712 /* Don't do this if Emacs is shutting down. Redisplay
10713 needs to run hooks. */
10714 && !NILP (Vrun_hooks))
10715 {
10716 /* Must update other windows. Likewise as in other
10717 cases, don't let this update be interrupted by
10718 pending input. */
10719 int count = SPECPDL_INDEX ();
10720 specbind (Qredisplay_dont_pause, Qt);
10721 windows_or_buffers_changed = 1;
10722 redisplay_internal ();
10723 unbind_to (count, Qnil);
10724 }
10725 else if (FRAME_WINDOW_P (f) && n == 0)
10726 {
10727 /* Window configuration is the same as before.
10728 Can do with a display update of the echo area,
10729 unless we displayed some mode lines. */
10730 update_single_window (w, 1);
10731 FRAME_RIF (f)->flush_display (f);
10732 }
10733 else
10734 update_frame (f, 1, 1);
10735
10736 /* If cursor is in the echo area, make sure that the next
10737 redisplay displays the minibuffer, so that the cursor will
10738 be replaced with what the minibuffer wants. */
10739 if (cursor_in_echo_area)
10740 ++windows_or_buffers_changed;
10741 }
10742 }
10743 else if (!EQ (mini_window, selected_window))
10744 windows_or_buffers_changed++;
10745
10746 /* Last displayed message is now the current message. */
10747 echo_area_buffer[1] = echo_area_buffer[0];
10748 /* Inform read_char that we're not echoing. */
10749 echo_message_buffer = Qnil;
10750
10751 /* Prevent redisplay optimization in redisplay_internal by resetting
10752 this_line_start_pos. This is done because the mini-buffer now
10753 displays the message instead of its buffer text. */
10754 if (EQ (mini_window, selected_window))
10755 CHARPOS (this_line_start_pos) = 0;
10756
10757 return window_height_changed_p;
10758 }
10759
10760
10761 \f
10762 /***********************************************************************
10763 Mode Lines and Frame Titles
10764 ***********************************************************************/
10765
10766 /* A buffer for constructing non-propertized mode-line strings and
10767 frame titles in it; allocated from the heap in init_xdisp and
10768 resized as needed in store_mode_line_noprop_char. */
10769
10770 static char *mode_line_noprop_buf;
10771
10772 /* The buffer's end, and a current output position in it. */
10773
10774 static char *mode_line_noprop_buf_end;
10775 static char *mode_line_noprop_ptr;
10776
10777 #define MODE_LINE_NOPROP_LEN(start) \
10778 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10779
10780 static enum {
10781 MODE_LINE_DISPLAY = 0,
10782 MODE_LINE_TITLE,
10783 MODE_LINE_NOPROP,
10784 MODE_LINE_STRING
10785 } mode_line_target;
10786
10787 /* Alist that caches the results of :propertize.
10788 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10789 static Lisp_Object mode_line_proptrans_alist;
10790
10791 /* List of strings making up the mode-line. */
10792 static Lisp_Object mode_line_string_list;
10793
10794 /* Base face property when building propertized mode line string. */
10795 static Lisp_Object mode_line_string_face;
10796 static Lisp_Object mode_line_string_face_prop;
10797
10798
10799 /* Unwind data for mode line strings */
10800
10801 static Lisp_Object Vmode_line_unwind_vector;
10802
10803 static Lisp_Object
10804 format_mode_line_unwind_data (struct buffer *obuf,
10805 Lisp_Object owin,
10806 int save_proptrans)
10807 {
10808 Lisp_Object vector, tmp;
10809
10810 /* Reduce consing by keeping one vector in
10811 Vwith_echo_area_save_vector. */
10812 vector = Vmode_line_unwind_vector;
10813 Vmode_line_unwind_vector = Qnil;
10814
10815 if (NILP (vector))
10816 vector = Fmake_vector (make_number (8), Qnil);
10817
10818 ASET (vector, 0, make_number (mode_line_target));
10819 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10820 ASET (vector, 2, mode_line_string_list);
10821 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10822 ASET (vector, 4, mode_line_string_face);
10823 ASET (vector, 5, mode_line_string_face_prop);
10824
10825 if (obuf)
10826 XSETBUFFER (tmp, obuf);
10827 else
10828 tmp = Qnil;
10829 ASET (vector, 6, tmp);
10830 ASET (vector, 7, owin);
10831
10832 return vector;
10833 }
10834
10835 static Lisp_Object
10836 unwind_format_mode_line (Lisp_Object vector)
10837 {
10838 mode_line_target = XINT (AREF (vector, 0));
10839 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10840 mode_line_string_list = AREF (vector, 2);
10841 if (! EQ (AREF (vector, 3), Qt))
10842 mode_line_proptrans_alist = AREF (vector, 3);
10843 mode_line_string_face = AREF (vector, 4);
10844 mode_line_string_face_prop = AREF (vector, 5);
10845
10846 if (!NILP (AREF (vector, 7)))
10847 /* Select window before buffer, since it may change the buffer. */
10848 Fselect_window (AREF (vector, 7), Qt);
10849
10850 if (!NILP (AREF (vector, 6)))
10851 {
10852 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10853 ASET (vector, 6, Qnil);
10854 }
10855
10856 Vmode_line_unwind_vector = vector;
10857 return Qnil;
10858 }
10859
10860
10861 /* Store a single character C for the frame title in mode_line_noprop_buf.
10862 Re-allocate mode_line_noprop_buf if necessary. */
10863
10864 static void
10865 store_mode_line_noprop_char (char c)
10866 {
10867 /* If output position has reached the end of the allocated buffer,
10868 increase the buffer's size. */
10869 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10870 {
10871 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10872 ptrdiff_t size = len;
10873 mode_line_noprop_buf =
10874 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10875 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10876 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10877 }
10878
10879 *mode_line_noprop_ptr++ = c;
10880 }
10881
10882
10883 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10884 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10885 characters that yield more columns than PRECISION; PRECISION <= 0
10886 means copy the whole string. Pad with spaces until FIELD_WIDTH
10887 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10888 pad. Called from display_mode_element when it is used to build a
10889 frame title. */
10890
10891 static int
10892 store_mode_line_noprop (const char *string, int field_width, int precision)
10893 {
10894 const unsigned char *str = (const unsigned char *) string;
10895 int n = 0;
10896 EMACS_INT dummy, nbytes;
10897
10898 /* Copy at most PRECISION chars from STR. */
10899 nbytes = strlen (string);
10900 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10901 while (nbytes--)
10902 store_mode_line_noprop_char (*str++);
10903
10904 /* Fill up with spaces until FIELD_WIDTH reached. */
10905 while (field_width > 0
10906 && n < field_width)
10907 {
10908 store_mode_line_noprop_char (' ');
10909 ++n;
10910 }
10911
10912 return n;
10913 }
10914
10915 /***********************************************************************
10916 Frame Titles
10917 ***********************************************************************/
10918
10919 #ifdef HAVE_WINDOW_SYSTEM
10920
10921 /* Set the title of FRAME, if it has changed. The title format is
10922 Vicon_title_format if FRAME is iconified, otherwise it is
10923 frame_title_format. */
10924
10925 static void
10926 x_consider_frame_title (Lisp_Object frame)
10927 {
10928 struct frame *f = XFRAME (frame);
10929
10930 if (FRAME_WINDOW_P (f)
10931 || FRAME_MINIBUF_ONLY_P (f)
10932 || f->explicit_name)
10933 {
10934 /* Do we have more than one visible frame on this X display? */
10935 Lisp_Object tail;
10936 Lisp_Object fmt;
10937 ptrdiff_t title_start;
10938 char *title;
10939 ptrdiff_t len;
10940 struct it it;
10941 int count = SPECPDL_INDEX ();
10942
10943 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10944 {
10945 Lisp_Object other_frame = XCAR (tail);
10946 struct frame *tf = XFRAME (other_frame);
10947
10948 if (tf != f
10949 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10950 && !FRAME_MINIBUF_ONLY_P (tf)
10951 && !EQ (other_frame, tip_frame)
10952 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10953 break;
10954 }
10955
10956 /* Set global variable indicating that multiple frames exist. */
10957 multiple_frames = CONSP (tail);
10958
10959 /* Switch to the buffer of selected window of the frame. Set up
10960 mode_line_target so that display_mode_element will output into
10961 mode_line_noprop_buf; then display the title. */
10962 record_unwind_protect (unwind_format_mode_line,
10963 format_mode_line_unwind_data
10964 (current_buffer, selected_window, 0));
10965
10966 Fselect_window (f->selected_window, Qt);
10967 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10968 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10969
10970 mode_line_target = MODE_LINE_TITLE;
10971 title_start = MODE_LINE_NOPROP_LEN (0);
10972 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10973 NULL, DEFAULT_FACE_ID);
10974 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10975 len = MODE_LINE_NOPROP_LEN (title_start);
10976 title = mode_line_noprop_buf + title_start;
10977 unbind_to (count, Qnil);
10978
10979 /* Set the title only if it's changed. This avoids consing in
10980 the common case where it hasn't. (If it turns out that we've
10981 already wasted too much time by walking through the list with
10982 display_mode_element, then we might need to optimize at a
10983 higher level than this.) */
10984 if (! STRINGP (f->name)
10985 || SBYTES (f->name) != len
10986 || memcmp (title, SDATA (f->name), len) != 0)
10987 x_implicitly_set_name (f, make_string (title, len), Qnil);
10988 }
10989 }
10990
10991 #endif /* not HAVE_WINDOW_SYSTEM */
10992
10993
10994
10995 \f
10996 /***********************************************************************
10997 Menu Bars
10998 ***********************************************************************/
10999
11000
11001 /* Prepare for redisplay by updating menu-bar item lists when
11002 appropriate. This can call eval. */
11003
11004 void
11005 prepare_menu_bars (void)
11006 {
11007 int all_windows;
11008 struct gcpro gcpro1, gcpro2;
11009 struct frame *f;
11010 Lisp_Object tooltip_frame;
11011
11012 #ifdef HAVE_WINDOW_SYSTEM
11013 tooltip_frame = tip_frame;
11014 #else
11015 tooltip_frame = Qnil;
11016 #endif
11017
11018 /* Update all frame titles based on their buffer names, etc. We do
11019 this before the menu bars so that the buffer-menu will show the
11020 up-to-date frame titles. */
11021 #ifdef HAVE_WINDOW_SYSTEM
11022 if (windows_or_buffers_changed || update_mode_lines)
11023 {
11024 Lisp_Object tail, frame;
11025
11026 FOR_EACH_FRAME (tail, frame)
11027 {
11028 f = XFRAME (frame);
11029 if (!EQ (frame, tooltip_frame)
11030 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11031 x_consider_frame_title (frame);
11032 }
11033 }
11034 #endif /* HAVE_WINDOW_SYSTEM */
11035
11036 /* Update the menu bar item lists, if appropriate. This has to be
11037 done before any actual redisplay or generation of display lines. */
11038 all_windows = (update_mode_lines
11039 || buffer_shared > 1
11040 || windows_or_buffers_changed);
11041 if (all_windows)
11042 {
11043 Lisp_Object tail, frame;
11044 int count = SPECPDL_INDEX ();
11045 /* 1 means that update_menu_bar has run its hooks
11046 so any further calls to update_menu_bar shouldn't do so again. */
11047 int menu_bar_hooks_run = 0;
11048
11049 record_unwind_save_match_data ();
11050
11051 FOR_EACH_FRAME (tail, frame)
11052 {
11053 f = XFRAME (frame);
11054
11055 /* Ignore tooltip frame. */
11056 if (EQ (frame, tooltip_frame))
11057 continue;
11058
11059 /* If a window on this frame changed size, report that to
11060 the user and clear the size-change flag. */
11061 if (FRAME_WINDOW_SIZES_CHANGED (f))
11062 {
11063 Lisp_Object functions;
11064
11065 /* Clear flag first in case we get an error below. */
11066 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11067 functions = Vwindow_size_change_functions;
11068 GCPRO2 (tail, functions);
11069
11070 while (CONSP (functions))
11071 {
11072 if (!EQ (XCAR (functions), Qt))
11073 call1 (XCAR (functions), frame);
11074 functions = XCDR (functions);
11075 }
11076 UNGCPRO;
11077 }
11078
11079 GCPRO1 (tail);
11080 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11081 #ifdef HAVE_WINDOW_SYSTEM
11082 update_tool_bar (f, 0);
11083 #endif
11084 #ifdef HAVE_NS
11085 if (windows_or_buffers_changed
11086 && FRAME_NS_P (f))
11087 ns_set_doc_edited (f, Fbuffer_modified_p
11088 (XWINDOW (f->selected_window)->buffer));
11089 #endif
11090 UNGCPRO;
11091 }
11092
11093 unbind_to (count, Qnil);
11094 }
11095 else
11096 {
11097 struct frame *sf = SELECTED_FRAME ();
11098 update_menu_bar (sf, 1, 0);
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 update_tool_bar (sf, 1);
11101 #endif
11102 }
11103 }
11104
11105
11106 /* Update the menu bar item list for frame F. This has to be done
11107 before we start to fill in any display lines, because it can call
11108 eval.
11109
11110 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11111
11112 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11113 already ran the menu bar hooks for this redisplay, so there
11114 is no need to run them again. The return value is the
11115 updated value of this flag, to pass to the next call. */
11116
11117 static int
11118 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11119 {
11120 Lisp_Object window;
11121 register struct window *w;
11122
11123 /* If called recursively during a menu update, do nothing. This can
11124 happen when, for instance, an activate-menubar-hook causes a
11125 redisplay. */
11126 if (inhibit_menubar_update)
11127 return hooks_run;
11128
11129 window = FRAME_SELECTED_WINDOW (f);
11130 w = XWINDOW (window);
11131
11132 if (FRAME_WINDOW_P (f)
11133 ?
11134 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11135 || defined (HAVE_NS) || defined (USE_GTK)
11136 FRAME_EXTERNAL_MENU_BAR (f)
11137 #else
11138 FRAME_MENU_BAR_LINES (f) > 0
11139 #endif
11140 : FRAME_MENU_BAR_LINES (f) > 0)
11141 {
11142 /* If the user has switched buffers or windows, we need to
11143 recompute to reflect the new bindings. But we'll
11144 recompute when update_mode_lines is set too; that means
11145 that people can use force-mode-line-update to request
11146 that the menu bar be recomputed. The adverse effect on
11147 the rest of the redisplay algorithm is about the same as
11148 windows_or_buffers_changed anyway. */
11149 if (windows_or_buffers_changed
11150 /* This used to test w->update_mode_line, but we believe
11151 there is no need to recompute the menu in that case. */
11152 || update_mode_lines
11153 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11154 < BUF_MODIFF (XBUFFER (w->buffer)))
11155 != !NILP (w->last_had_star))
11156 || ((!NILP (Vtransient_mark_mode)
11157 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11158 != !NILP (w->region_showing)))
11159 {
11160 struct buffer *prev = current_buffer;
11161 int count = SPECPDL_INDEX ();
11162
11163 specbind (Qinhibit_menubar_update, Qt);
11164
11165 set_buffer_internal_1 (XBUFFER (w->buffer));
11166 if (save_match_data)
11167 record_unwind_save_match_data ();
11168 if (NILP (Voverriding_local_map_menu_flag))
11169 {
11170 specbind (Qoverriding_terminal_local_map, Qnil);
11171 specbind (Qoverriding_local_map, Qnil);
11172 }
11173
11174 if (!hooks_run)
11175 {
11176 /* Run the Lucid hook. */
11177 safe_run_hooks (Qactivate_menubar_hook);
11178
11179 /* If it has changed current-menubar from previous value,
11180 really recompute the menu-bar from the value. */
11181 if (! NILP (Vlucid_menu_bar_dirty_flag))
11182 call0 (Qrecompute_lucid_menubar);
11183
11184 safe_run_hooks (Qmenu_bar_update_hook);
11185
11186 hooks_run = 1;
11187 }
11188
11189 XSETFRAME (Vmenu_updating_frame, f);
11190 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11191
11192 /* Redisplay the menu bar in case we changed it. */
11193 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11194 || defined (HAVE_NS) || defined (USE_GTK)
11195 if (FRAME_WINDOW_P (f))
11196 {
11197 #if defined (HAVE_NS)
11198 /* All frames on Mac OS share the same menubar. So only
11199 the selected frame should be allowed to set it. */
11200 if (f == SELECTED_FRAME ())
11201 #endif
11202 set_frame_menubar (f, 0, 0);
11203 }
11204 else
11205 /* On a terminal screen, the menu bar is an ordinary screen
11206 line, and this makes it get updated. */
11207 w->update_mode_line = Qt;
11208 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11209 /* In the non-toolkit version, the menu bar is an ordinary screen
11210 line, and this makes it get updated. */
11211 w->update_mode_line = Qt;
11212 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11213
11214 unbind_to (count, Qnil);
11215 set_buffer_internal_1 (prev);
11216 }
11217 }
11218
11219 return hooks_run;
11220 }
11221
11222
11223 \f
11224 /***********************************************************************
11225 Output Cursor
11226 ***********************************************************************/
11227
11228 #ifdef HAVE_WINDOW_SYSTEM
11229
11230 /* EXPORT:
11231 Nominal cursor position -- where to draw output.
11232 HPOS and VPOS are window relative glyph matrix coordinates.
11233 X and Y are window relative pixel coordinates. */
11234
11235 struct cursor_pos output_cursor;
11236
11237
11238 /* EXPORT:
11239 Set the global variable output_cursor to CURSOR. All cursor
11240 positions are relative to updated_window. */
11241
11242 void
11243 set_output_cursor (struct cursor_pos *cursor)
11244 {
11245 output_cursor.hpos = cursor->hpos;
11246 output_cursor.vpos = cursor->vpos;
11247 output_cursor.x = cursor->x;
11248 output_cursor.y = cursor->y;
11249 }
11250
11251
11252 /* EXPORT for RIF:
11253 Set a nominal cursor position.
11254
11255 HPOS and VPOS are column/row positions in a window glyph matrix. X
11256 and Y are window text area relative pixel positions.
11257
11258 If this is done during an update, updated_window will contain the
11259 window that is being updated and the position is the future output
11260 cursor position for that window. If updated_window is null, use
11261 selected_window and display the cursor at the given position. */
11262
11263 void
11264 x_cursor_to (int vpos, int hpos, int y, int x)
11265 {
11266 struct window *w;
11267
11268 /* If updated_window is not set, work on selected_window. */
11269 if (updated_window)
11270 w = updated_window;
11271 else
11272 w = XWINDOW (selected_window);
11273
11274 /* Set the output cursor. */
11275 output_cursor.hpos = hpos;
11276 output_cursor.vpos = vpos;
11277 output_cursor.x = x;
11278 output_cursor.y = y;
11279
11280 /* If not called as part of an update, really display the cursor.
11281 This will also set the cursor position of W. */
11282 if (updated_window == NULL)
11283 {
11284 BLOCK_INPUT;
11285 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11286 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11287 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11288 UNBLOCK_INPUT;
11289 }
11290 }
11291
11292 #endif /* HAVE_WINDOW_SYSTEM */
11293
11294 \f
11295 /***********************************************************************
11296 Tool-bars
11297 ***********************************************************************/
11298
11299 #ifdef HAVE_WINDOW_SYSTEM
11300
11301 /* Where the mouse was last time we reported a mouse event. */
11302
11303 FRAME_PTR last_mouse_frame;
11304
11305 /* Tool-bar item index of the item on which a mouse button was pressed
11306 or -1. */
11307
11308 int last_tool_bar_item;
11309
11310
11311 static Lisp_Object
11312 update_tool_bar_unwind (Lisp_Object frame)
11313 {
11314 selected_frame = frame;
11315 return Qnil;
11316 }
11317
11318 /* Update the tool-bar item list for frame F. This has to be done
11319 before we start to fill in any display lines. Called from
11320 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11321 and restore it here. */
11322
11323 static void
11324 update_tool_bar (struct frame *f, int save_match_data)
11325 {
11326 #if defined (USE_GTK) || defined (HAVE_NS)
11327 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11328 #else
11329 int do_update = WINDOWP (f->tool_bar_window)
11330 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11331 #endif
11332
11333 if (do_update)
11334 {
11335 Lisp_Object window;
11336 struct window *w;
11337
11338 window = FRAME_SELECTED_WINDOW (f);
11339 w = XWINDOW (window);
11340
11341 /* If the user has switched buffers or windows, we need to
11342 recompute to reflect the new bindings. But we'll
11343 recompute when update_mode_lines is set too; that means
11344 that people can use force-mode-line-update to request
11345 that the menu bar be recomputed. The adverse effect on
11346 the rest of the redisplay algorithm is about the same as
11347 windows_or_buffers_changed anyway. */
11348 if (windows_or_buffers_changed
11349 || !NILP (w->update_mode_line)
11350 || update_mode_lines
11351 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11352 < BUF_MODIFF (XBUFFER (w->buffer)))
11353 != !NILP (w->last_had_star))
11354 || ((!NILP (Vtransient_mark_mode)
11355 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11356 != !NILP (w->region_showing)))
11357 {
11358 struct buffer *prev = current_buffer;
11359 int count = SPECPDL_INDEX ();
11360 Lisp_Object frame, new_tool_bar;
11361 int new_n_tool_bar;
11362 struct gcpro gcpro1;
11363
11364 /* Set current_buffer to the buffer of the selected
11365 window of the frame, so that we get the right local
11366 keymaps. */
11367 set_buffer_internal_1 (XBUFFER (w->buffer));
11368
11369 /* Save match data, if we must. */
11370 if (save_match_data)
11371 record_unwind_save_match_data ();
11372
11373 /* Make sure that we don't accidentally use bogus keymaps. */
11374 if (NILP (Voverriding_local_map_menu_flag))
11375 {
11376 specbind (Qoverriding_terminal_local_map, Qnil);
11377 specbind (Qoverriding_local_map, Qnil);
11378 }
11379
11380 GCPRO1 (new_tool_bar);
11381
11382 /* We must temporarily set the selected frame to this frame
11383 before calling tool_bar_items, because the calculation of
11384 the tool-bar keymap uses the selected frame (see
11385 `tool-bar-make-keymap' in tool-bar.el). */
11386 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11387 XSETFRAME (frame, f);
11388 selected_frame = frame;
11389
11390 /* Build desired tool-bar items from keymaps. */
11391 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11392 &new_n_tool_bar);
11393
11394 /* Redisplay the tool-bar if we changed it. */
11395 if (new_n_tool_bar != f->n_tool_bar_items
11396 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11397 {
11398 /* Redisplay that happens asynchronously due to an expose event
11399 may access f->tool_bar_items. Make sure we update both
11400 variables within BLOCK_INPUT so no such event interrupts. */
11401 BLOCK_INPUT;
11402 f->tool_bar_items = new_tool_bar;
11403 f->n_tool_bar_items = new_n_tool_bar;
11404 w->update_mode_line = Qt;
11405 UNBLOCK_INPUT;
11406 }
11407
11408 UNGCPRO;
11409
11410 unbind_to (count, Qnil);
11411 set_buffer_internal_1 (prev);
11412 }
11413 }
11414 }
11415
11416
11417 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11418 F's desired tool-bar contents. F->tool_bar_items must have
11419 been set up previously by calling prepare_menu_bars. */
11420
11421 static void
11422 build_desired_tool_bar_string (struct frame *f)
11423 {
11424 int i, size, size_needed;
11425 struct gcpro gcpro1, gcpro2, gcpro3;
11426 Lisp_Object image, plist, props;
11427
11428 image = plist = props = Qnil;
11429 GCPRO3 (image, plist, props);
11430
11431 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11432 Otherwise, make a new string. */
11433
11434 /* The size of the string we might be able to reuse. */
11435 size = (STRINGP (f->desired_tool_bar_string)
11436 ? SCHARS (f->desired_tool_bar_string)
11437 : 0);
11438
11439 /* We need one space in the string for each image. */
11440 size_needed = f->n_tool_bar_items;
11441
11442 /* Reuse f->desired_tool_bar_string, if possible. */
11443 if (size < size_needed || NILP (f->desired_tool_bar_string))
11444 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11445 make_number (' '));
11446 else
11447 {
11448 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11449 Fremove_text_properties (make_number (0), make_number (size),
11450 props, f->desired_tool_bar_string);
11451 }
11452
11453 /* Put a `display' property on the string for the images to display,
11454 put a `menu_item' property on tool-bar items with a value that
11455 is the index of the item in F's tool-bar item vector. */
11456 for (i = 0; i < f->n_tool_bar_items; ++i)
11457 {
11458 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11459
11460 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11461 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11462 int hmargin, vmargin, relief, idx, end;
11463
11464 /* If image is a vector, choose the image according to the
11465 button state. */
11466 image = PROP (TOOL_BAR_ITEM_IMAGES);
11467 if (VECTORP (image))
11468 {
11469 if (enabled_p)
11470 idx = (selected_p
11471 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11472 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11473 else
11474 idx = (selected_p
11475 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11476 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11477
11478 xassert (ASIZE (image) >= idx);
11479 image = AREF (image, idx);
11480 }
11481 else
11482 idx = -1;
11483
11484 /* Ignore invalid image specifications. */
11485 if (!valid_image_p (image))
11486 continue;
11487
11488 /* Display the tool-bar button pressed, or depressed. */
11489 plist = Fcopy_sequence (XCDR (image));
11490
11491 /* Compute margin and relief to draw. */
11492 relief = (tool_bar_button_relief >= 0
11493 ? tool_bar_button_relief
11494 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11495 hmargin = vmargin = relief;
11496
11497 if (INTEGERP (Vtool_bar_button_margin)
11498 && XINT (Vtool_bar_button_margin) > 0)
11499 {
11500 hmargin += XFASTINT (Vtool_bar_button_margin);
11501 vmargin += XFASTINT (Vtool_bar_button_margin);
11502 }
11503 else if (CONSP (Vtool_bar_button_margin))
11504 {
11505 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11506 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11507 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11508
11509 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11510 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11511 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11512 }
11513
11514 if (auto_raise_tool_bar_buttons_p)
11515 {
11516 /* Add a `:relief' property to the image spec if the item is
11517 selected. */
11518 if (selected_p)
11519 {
11520 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11521 hmargin -= relief;
11522 vmargin -= relief;
11523 }
11524 }
11525 else
11526 {
11527 /* If image is selected, display it pressed, i.e. with a
11528 negative relief. If it's not selected, display it with a
11529 raised relief. */
11530 plist = Fplist_put (plist, QCrelief,
11531 (selected_p
11532 ? make_number (-relief)
11533 : make_number (relief)));
11534 hmargin -= relief;
11535 vmargin -= relief;
11536 }
11537
11538 /* Put a margin around the image. */
11539 if (hmargin || vmargin)
11540 {
11541 if (hmargin == vmargin)
11542 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11543 else
11544 plist = Fplist_put (plist, QCmargin,
11545 Fcons (make_number (hmargin),
11546 make_number (vmargin)));
11547 }
11548
11549 /* If button is not enabled, and we don't have special images
11550 for the disabled state, make the image appear disabled by
11551 applying an appropriate algorithm to it. */
11552 if (!enabled_p && idx < 0)
11553 plist = Fplist_put (plist, QCconversion, Qdisabled);
11554
11555 /* Put a `display' text property on the string for the image to
11556 display. Put a `menu-item' property on the string that gives
11557 the start of this item's properties in the tool-bar items
11558 vector. */
11559 image = Fcons (Qimage, plist);
11560 props = list4 (Qdisplay, image,
11561 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11562
11563 /* Let the last image hide all remaining spaces in the tool bar
11564 string. The string can be longer than needed when we reuse a
11565 previous string. */
11566 if (i + 1 == f->n_tool_bar_items)
11567 end = SCHARS (f->desired_tool_bar_string);
11568 else
11569 end = i + 1;
11570 Fadd_text_properties (make_number (i), make_number (end),
11571 props, f->desired_tool_bar_string);
11572 #undef PROP
11573 }
11574
11575 UNGCPRO;
11576 }
11577
11578
11579 /* Display one line of the tool-bar of frame IT->f.
11580
11581 HEIGHT specifies the desired height of the tool-bar line.
11582 If the actual height of the glyph row is less than HEIGHT, the
11583 row's height is increased to HEIGHT, and the icons are centered
11584 vertically in the new height.
11585
11586 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11587 count a final empty row in case the tool-bar width exactly matches
11588 the window width.
11589 */
11590
11591 static void
11592 display_tool_bar_line (struct it *it, int height)
11593 {
11594 struct glyph_row *row = it->glyph_row;
11595 int max_x = it->last_visible_x;
11596 struct glyph *last;
11597
11598 prepare_desired_row (row);
11599 row->y = it->current_y;
11600
11601 /* Note that this isn't made use of if the face hasn't a box,
11602 so there's no need to check the face here. */
11603 it->start_of_box_run_p = 1;
11604
11605 while (it->current_x < max_x)
11606 {
11607 int x, n_glyphs_before, i, nglyphs;
11608 struct it it_before;
11609
11610 /* Get the next display element. */
11611 if (!get_next_display_element (it))
11612 {
11613 /* Don't count empty row if we are counting needed tool-bar lines. */
11614 if (height < 0 && !it->hpos)
11615 return;
11616 break;
11617 }
11618
11619 /* Produce glyphs. */
11620 n_glyphs_before = row->used[TEXT_AREA];
11621 it_before = *it;
11622
11623 PRODUCE_GLYPHS (it);
11624
11625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11626 i = 0;
11627 x = it_before.current_x;
11628 while (i < nglyphs)
11629 {
11630 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11631
11632 if (x + glyph->pixel_width > max_x)
11633 {
11634 /* Glyph doesn't fit on line. Backtrack. */
11635 row->used[TEXT_AREA] = n_glyphs_before;
11636 *it = it_before;
11637 /* If this is the only glyph on this line, it will never fit on the
11638 tool-bar, so skip it. But ensure there is at least one glyph,
11639 so we don't accidentally disable the tool-bar. */
11640 if (n_glyphs_before == 0
11641 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11642 break;
11643 goto out;
11644 }
11645
11646 ++it->hpos;
11647 x += glyph->pixel_width;
11648 ++i;
11649 }
11650
11651 /* Stop at line end. */
11652 if (ITERATOR_AT_END_OF_LINE_P (it))
11653 break;
11654
11655 set_iterator_to_next (it, 1);
11656 }
11657
11658 out:;
11659
11660 row->displays_text_p = row->used[TEXT_AREA] != 0;
11661
11662 /* Use default face for the border below the tool bar.
11663
11664 FIXME: When auto-resize-tool-bars is grow-only, there is
11665 no additional border below the possibly empty tool-bar lines.
11666 So to make the extra empty lines look "normal", we have to
11667 use the tool-bar face for the border too. */
11668 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11669 it->face_id = DEFAULT_FACE_ID;
11670
11671 extend_face_to_end_of_line (it);
11672 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11673 last->right_box_line_p = 1;
11674 if (last == row->glyphs[TEXT_AREA])
11675 last->left_box_line_p = 1;
11676
11677 /* Make line the desired height and center it vertically. */
11678 if ((height -= it->max_ascent + it->max_descent) > 0)
11679 {
11680 /* Don't add more than one line height. */
11681 height %= FRAME_LINE_HEIGHT (it->f);
11682 it->max_ascent += height / 2;
11683 it->max_descent += (height + 1) / 2;
11684 }
11685
11686 compute_line_metrics (it);
11687
11688 /* If line is empty, make it occupy the rest of the tool-bar. */
11689 if (!row->displays_text_p)
11690 {
11691 row->height = row->phys_height = it->last_visible_y - row->y;
11692 row->visible_height = row->height;
11693 row->ascent = row->phys_ascent = 0;
11694 row->extra_line_spacing = 0;
11695 }
11696
11697 row->full_width_p = 1;
11698 row->continued_p = 0;
11699 row->truncated_on_left_p = 0;
11700 row->truncated_on_right_p = 0;
11701
11702 it->current_x = it->hpos = 0;
11703 it->current_y += row->height;
11704 ++it->vpos;
11705 ++it->glyph_row;
11706 }
11707
11708
11709 /* Max tool-bar height. */
11710
11711 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11712 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11713
11714 /* Value is the number of screen lines needed to make all tool-bar
11715 items of frame F visible. The number of actual rows needed is
11716 returned in *N_ROWS if non-NULL. */
11717
11718 static int
11719 tool_bar_lines_needed (struct frame *f, int *n_rows)
11720 {
11721 struct window *w = XWINDOW (f->tool_bar_window);
11722 struct it it;
11723 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11724 the desired matrix, so use (unused) mode-line row as temporary row to
11725 avoid destroying the first tool-bar row. */
11726 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11727
11728 /* Initialize an iterator for iteration over
11729 F->desired_tool_bar_string in the tool-bar window of frame F. */
11730 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11731 it.first_visible_x = 0;
11732 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11733 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11734 it.paragraph_embedding = L2R;
11735
11736 while (!ITERATOR_AT_END_P (&it))
11737 {
11738 clear_glyph_row (temp_row);
11739 it.glyph_row = temp_row;
11740 display_tool_bar_line (&it, -1);
11741 }
11742 clear_glyph_row (temp_row);
11743
11744 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11745 if (n_rows)
11746 *n_rows = it.vpos > 0 ? it.vpos : -1;
11747
11748 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11749 }
11750
11751
11752 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11753 0, 1, 0,
11754 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11755 (Lisp_Object frame)
11756 {
11757 struct frame *f;
11758 struct window *w;
11759 int nlines = 0;
11760
11761 if (NILP (frame))
11762 frame = selected_frame;
11763 else
11764 CHECK_FRAME (frame);
11765 f = XFRAME (frame);
11766
11767 if (WINDOWP (f->tool_bar_window)
11768 && (w = XWINDOW (f->tool_bar_window),
11769 WINDOW_TOTAL_LINES (w) > 0))
11770 {
11771 update_tool_bar (f, 1);
11772 if (f->n_tool_bar_items)
11773 {
11774 build_desired_tool_bar_string (f);
11775 nlines = tool_bar_lines_needed (f, NULL);
11776 }
11777 }
11778
11779 return make_number (nlines);
11780 }
11781
11782
11783 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11784 height should be changed. */
11785
11786 static int
11787 redisplay_tool_bar (struct frame *f)
11788 {
11789 struct window *w;
11790 struct it it;
11791 struct glyph_row *row;
11792
11793 #if defined (USE_GTK) || defined (HAVE_NS)
11794 if (FRAME_EXTERNAL_TOOL_BAR (f))
11795 update_frame_tool_bar (f);
11796 return 0;
11797 #endif
11798
11799 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11800 do anything. This means you must start with tool-bar-lines
11801 non-zero to get the auto-sizing effect. Or in other words, you
11802 can turn off tool-bars by specifying tool-bar-lines zero. */
11803 if (!WINDOWP (f->tool_bar_window)
11804 || (w = XWINDOW (f->tool_bar_window),
11805 WINDOW_TOTAL_LINES (w) == 0))
11806 return 0;
11807
11808 /* Set up an iterator for the tool-bar window. */
11809 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11810 it.first_visible_x = 0;
11811 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11812 row = it.glyph_row;
11813
11814 /* Build a string that represents the contents of the tool-bar. */
11815 build_desired_tool_bar_string (f);
11816 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11817 /* FIXME: This should be controlled by a user option. But it
11818 doesn't make sense to have an R2L tool bar if the menu bar cannot
11819 be drawn also R2L, and making the menu bar R2L is tricky due
11820 toolkit-specific code that implements it. If an R2L tool bar is
11821 ever supported, display_tool_bar_line should also be augmented to
11822 call unproduce_glyphs like display_line and display_string
11823 do. */
11824 it.paragraph_embedding = L2R;
11825
11826 if (f->n_tool_bar_rows == 0)
11827 {
11828 int nlines;
11829
11830 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11831 nlines != WINDOW_TOTAL_LINES (w)))
11832 {
11833 Lisp_Object frame;
11834 int old_height = WINDOW_TOTAL_LINES (w);
11835
11836 XSETFRAME (frame, f);
11837 Fmodify_frame_parameters (frame,
11838 Fcons (Fcons (Qtool_bar_lines,
11839 make_number (nlines)),
11840 Qnil));
11841 if (WINDOW_TOTAL_LINES (w) != old_height)
11842 {
11843 clear_glyph_matrix (w->desired_matrix);
11844 fonts_changed_p = 1;
11845 return 1;
11846 }
11847 }
11848 }
11849
11850 /* Display as many lines as needed to display all tool-bar items. */
11851
11852 if (f->n_tool_bar_rows > 0)
11853 {
11854 int border, rows, height, extra;
11855
11856 if (INTEGERP (Vtool_bar_border))
11857 border = XINT (Vtool_bar_border);
11858 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11859 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11860 else if (EQ (Vtool_bar_border, Qborder_width))
11861 border = f->border_width;
11862 else
11863 border = 0;
11864 if (border < 0)
11865 border = 0;
11866
11867 rows = f->n_tool_bar_rows;
11868 height = max (1, (it.last_visible_y - border) / rows);
11869 extra = it.last_visible_y - border - height * rows;
11870
11871 while (it.current_y < it.last_visible_y)
11872 {
11873 int h = 0;
11874 if (extra > 0 && rows-- > 0)
11875 {
11876 h = (extra + rows - 1) / rows;
11877 extra -= h;
11878 }
11879 display_tool_bar_line (&it, height + h);
11880 }
11881 }
11882 else
11883 {
11884 while (it.current_y < it.last_visible_y)
11885 display_tool_bar_line (&it, 0);
11886 }
11887
11888 /* It doesn't make much sense to try scrolling in the tool-bar
11889 window, so don't do it. */
11890 w->desired_matrix->no_scrolling_p = 1;
11891 w->must_be_updated_p = 1;
11892
11893 if (!NILP (Vauto_resize_tool_bars))
11894 {
11895 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11896 int change_height_p = 0;
11897
11898 /* If we couldn't display everything, change the tool-bar's
11899 height if there is room for more. */
11900 if (IT_STRING_CHARPOS (it) < it.end_charpos
11901 && it.current_y < max_tool_bar_height)
11902 change_height_p = 1;
11903
11904 row = it.glyph_row - 1;
11905
11906 /* If there are blank lines at the end, except for a partially
11907 visible blank line at the end that is smaller than
11908 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11909 if (!row->displays_text_p
11910 && row->height >= FRAME_LINE_HEIGHT (f))
11911 change_height_p = 1;
11912
11913 /* If row displays tool-bar items, but is partially visible,
11914 change the tool-bar's height. */
11915 if (row->displays_text_p
11916 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11917 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11918 change_height_p = 1;
11919
11920 /* Resize windows as needed by changing the `tool-bar-lines'
11921 frame parameter. */
11922 if (change_height_p)
11923 {
11924 Lisp_Object frame;
11925 int old_height = WINDOW_TOTAL_LINES (w);
11926 int nrows;
11927 int nlines = tool_bar_lines_needed (f, &nrows);
11928
11929 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11930 && !f->minimize_tool_bar_window_p)
11931 ? (nlines > old_height)
11932 : (nlines != old_height));
11933 f->minimize_tool_bar_window_p = 0;
11934
11935 if (change_height_p)
11936 {
11937 XSETFRAME (frame, f);
11938 Fmodify_frame_parameters (frame,
11939 Fcons (Fcons (Qtool_bar_lines,
11940 make_number (nlines)),
11941 Qnil));
11942 if (WINDOW_TOTAL_LINES (w) != old_height)
11943 {
11944 clear_glyph_matrix (w->desired_matrix);
11945 f->n_tool_bar_rows = nrows;
11946 fonts_changed_p = 1;
11947 return 1;
11948 }
11949 }
11950 }
11951 }
11952
11953 f->minimize_tool_bar_window_p = 0;
11954 return 0;
11955 }
11956
11957
11958 /* Get information about the tool-bar item which is displayed in GLYPH
11959 on frame F. Return in *PROP_IDX the index where tool-bar item
11960 properties start in F->tool_bar_items. Value is zero if
11961 GLYPH doesn't display a tool-bar item. */
11962
11963 static int
11964 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11965 {
11966 Lisp_Object prop;
11967 int success_p;
11968 int charpos;
11969
11970 /* This function can be called asynchronously, which means we must
11971 exclude any possibility that Fget_text_property signals an
11972 error. */
11973 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11974 charpos = max (0, charpos);
11975
11976 /* Get the text property `menu-item' at pos. The value of that
11977 property is the start index of this item's properties in
11978 F->tool_bar_items. */
11979 prop = Fget_text_property (make_number (charpos),
11980 Qmenu_item, f->current_tool_bar_string);
11981 if (INTEGERP (prop))
11982 {
11983 *prop_idx = XINT (prop);
11984 success_p = 1;
11985 }
11986 else
11987 success_p = 0;
11988
11989 return success_p;
11990 }
11991
11992 \f
11993 /* Get information about the tool-bar item at position X/Y on frame F.
11994 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11995 the current matrix of the tool-bar window of F, or NULL if not
11996 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11997 item in F->tool_bar_items. Value is
11998
11999 -1 if X/Y is not on a tool-bar item
12000 0 if X/Y is on the same item that was highlighted before.
12001 1 otherwise. */
12002
12003 static int
12004 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12005 int *hpos, int *vpos, int *prop_idx)
12006 {
12007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12008 struct window *w = XWINDOW (f->tool_bar_window);
12009 int area;
12010
12011 /* Find the glyph under X/Y. */
12012 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12013 if (*glyph == NULL)
12014 return -1;
12015
12016 /* Get the start of this tool-bar item's properties in
12017 f->tool_bar_items. */
12018 if (!tool_bar_item_info (f, *glyph, prop_idx))
12019 return -1;
12020
12021 /* Is mouse on the highlighted item? */
12022 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12023 && *vpos >= hlinfo->mouse_face_beg_row
12024 && *vpos <= hlinfo->mouse_face_end_row
12025 && (*vpos > hlinfo->mouse_face_beg_row
12026 || *hpos >= hlinfo->mouse_face_beg_col)
12027 && (*vpos < hlinfo->mouse_face_end_row
12028 || *hpos < hlinfo->mouse_face_end_col
12029 || hlinfo->mouse_face_past_end))
12030 return 0;
12031
12032 return 1;
12033 }
12034
12035
12036 /* EXPORT:
12037 Handle mouse button event on the tool-bar of frame F, at
12038 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12039 0 for button release. MODIFIERS is event modifiers for button
12040 release. */
12041
12042 void
12043 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12044 unsigned int modifiers)
12045 {
12046 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12047 struct window *w = XWINDOW (f->tool_bar_window);
12048 int hpos, vpos, prop_idx;
12049 struct glyph *glyph;
12050 Lisp_Object enabled_p;
12051
12052 /* If not on the highlighted tool-bar item, return. */
12053 frame_to_window_pixel_xy (w, &x, &y);
12054 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12055 return;
12056
12057 /* If item is disabled, do nothing. */
12058 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12059 if (NILP (enabled_p))
12060 return;
12061
12062 if (down_p)
12063 {
12064 /* Show item in pressed state. */
12065 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12066 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12067 last_tool_bar_item = prop_idx;
12068 }
12069 else
12070 {
12071 Lisp_Object key, frame;
12072 struct input_event event;
12073 EVENT_INIT (event);
12074
12075 /* Show item in released state. */
12076 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12077 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12078
12079 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12080
12081 XSETFRAME (frame, f);
12082 event.kind = TOOL_BAR_EVENT;
12083 event.frame_or_window = frame;
12084 event.arg = frame;
12085 kbd_buffer_store_event (&event);
12086
12087 event.kind = TOOL_BAR_EVENT;
12088 event.frame_or_window = frame;
12089 event.arg = key;
12090 event.modifiers = modifiers;
12091 kbd_buffer_store_event (&event);
12092 last_tool_bar_item = -1;
12093 }
12094 }
12095
12096
12097 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12098 tool-bar window-relative coordinates X/Y. Called from
12099 note_mouse_highlight. */
12100
12101 static void
12102 note_tool_bar_highlight (struct frame *f, int x, int y)
12103 {
12104 Lisp_Object window = f->tool_bar_window;
12105 struct window *w = XWINDOW (window);
12106 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12107 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12108 int hpos, vpos;
12109 struct glyph *glyph;
12110 struct glyph_row *row;
12111 int i;
12112 Lisp_Object enabled_p;
12113 int prop_idx;
12114 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12115 int mouse_down_p, rc;
12116
12117 /* Function note_mouse_highlight is called with negative X/Y
12118 values when mouse moves outside of the frame. */
12119 if (x <= 0 || y <= 0)
12120 {
12121 clear_mouse_face (hlinfo);
12122 return;
12123 }
12124
12125 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12126 if (rc < 0)
12127 {
12128 /* Not on tool-bar item. */
12129 clear_mouse_face (hlinfo);
12130 return;
12131 }
12132 else if (rc == 0)
12133 /* On same tool-bar item as before. */
12134 goto set_help_echo;
12135
12136 clear_mouse_face (hlinfo);
12137
12138 /* Mouse is down, but on different tool-bar item? */
12139 mouse_down_p = (dpyinfo->grabbed
12140 && f == last_mouse_frame
12141 && FRAME_LIVE_P (f));
12142 if (mouse_down_p
12143 && last_tool_bar_item != prop_idx)
12144 return;
12145
12146 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12147 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12148
12149 /* If tool-bar item is not enabled, don't highlight it. */
12150 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12151 if (!NILP (enabled_p))
12152 {
12153 /* Compute the x-position of the glyph. In front and past the
12154 image is a space. We include this in the highlighted area. */
12155 row = MATRIX_ROW (w->current_matrix, vpos);
12156 for (i = x = 0; i < hpos; ++i)
12157 x += row->glyphs[TEXT_AREA][i].pixel_width;
12158
12159 /* Record this as the current active region. */
12160 hlinfo->mouse_face_beg_col = hpos;
12161 hlinfo->mouse_face_beg_row = vpos;
12162 hlinfo->mouse_face_beg_x = x;
12163 hlinfo->mouse_face_beg_y = row->y;
12164 hlinfo->mouse_face_past_end = 0;
12165
12166 hlinfo->mouse_face_end_col = hpos + 1;
12167 hlinfo->mouse_face_end_row = vpos;
12168 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12169 hlinfo->mouse_face_end_y = row->y;
12170 hlinfo->mouse_face_window = window;
12171 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12172
12173 /* Display it as active. */
12174 show_mouse_face (hlinfo, draw);
12175 hlinfo->mouse_face_image_state = draw;
12176 }
12177
12178 set_help_echo:
12179
12180 /* Set help_echo_string to a help string to display for this tool-bar item.
12181 XTread_socket does the rest. */
12182 help_echo_object = help_echo_window = Qnil;
12183 help_echo_pos = -1;
12184 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12185 if (NILP (help_echo_string))
12186 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12187 }
12188
12189 #endif /* HAVE_WINDOW_SYSTEM */
12190
12191
12192 \f
12193 /************************************************************************
12194 Horizontal scrolling
12195 ************************************************************************/
12196
12197 static int hscroll_window_tree (Lisp_Object);
12198 static int hscroll_windows (Lisp_Object);
12199
12200 /* For all leaf windows in the window tree rooted at WINDOW, set their
12201 hscroll value so that PT is (i) visible in the window, and (ii) so
12202 that it is not within a certain margin at the window's left and
12203 right border. Value is non-zero if any window's hscroll has been
12204 changed. */
12205
12206 static int
12207 hscroll_window_tree (Lisp_Object window)
12208 {
12209 int hscrolled_p = 0;
12210 int hscroll_relative_p = FLOATP (Vhscroll_step);
12211 int hscroll_step_abs = 0;
12212 double hscroll_step_rel = 0;
12213
12214 if (hscroll_relative_p)
12215 {
12216 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12217 if (hscroll_step_rel < 0)
12218 {
12219 hscroll_relative_p = 0;
12220 hscroll_step_abs = 0;
12221 }
12222 }
12223 else if (INTEGERP (Vhscroll_step))
12224 {
12225 hscroll_step_abs = XINT (Vhscroll_step);
12226 if (hscroll_step_abs < 0)
12227 hscroll_step_abs = 0;
12228 }
12229 else
12230 hscroll_step_abs = 0;
12231
12232 while (WINDOWP (window))
12233 {
12234 struct window *w = XWINDOW (window);
12235
12236 if (WINDOWP (w->hchild))
12237 hscrolled_p |= hscroll_window_tree (w->hchild);
12238 else if (WINDOWP (w->vchild))
12239 hscrolled_p |= hscroll_window_tree (w->vchild);
12240 else if (w->cursor.vpos >= 0)
12241 {
12242 int h_margin;
12243 int text_area_width;
12244 struct glyph_row *current_cursor_row
12245 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12246 struct glyph_row *desired_cursor_row
12247 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12248 struct glyph_row *cursor_row
12249 = (desired_cursor_row->enabled_p
12250 ? desired_cursor_row
12251 : current_cursor_row);
12252 int row_r2l_p = cursor_row->reversed_p;
12253
12254 text_area_width = window_box_width (w, TEXT_AREA);
12255
12256 /* Scroll when cursor is inside this scroll margin. */
12257 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12258
12259 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12260 /* For left-to-right rows, hscroll when cursor is either
12261 (i) inside the right hscroll margin, or (ii) if it is
12262 inside the left margin and the window is already
12263 hscrolled. */
12264 && ((!row_r2l_p
12265 && ((XFASTINT (w->hscroll)
12266 && w->cursor.x <= h_margin)
12267 || (cursor_row->enabled_p
12268 && cursor_row->truncated_on_right_p
12269 && (w->cursor.x >= text_area_width - h_margin))))
12270 /* For right-to-left rows, the logic is similar,
12271 except that rules for scrolling to left and right
12272 are reversed. E.g., if cursor.x <= h_margin, we
12273 need to hscroll "to the right" unconditionally,
12274 and that will scroll the screen to the left so as
12275 to reveal the next portion of the row. */
12276 || (row_r2l_p
12277 && ((cursor_row->enabled_p
12278 /* FIXME: It is confusing to set the
12279 truncated_on_right_p flag when R2L rows
12280 are actually truncated on the left. */
12281 && cursor_row->truncated_on_right_p
12282 && w->cursor.x <= h_margin)
12283 || (XFASTINT (w->hscroll)
12284 && (w->cursor.x >= text_area_width - h_margin))))))
12285 {
12286 struct it it;
12287 int hscroll;
12288 struct buffer *saved_current_buffer;
12289 EMACS_INT pt;
12290 int wanted_x;
12291
12292 /* Find point in a display of infinite width. */
12293 saved_current_buffer = current_buffer;
12294 current_buffer = XBUFFER (w->buffer);
12295
12296 if (w == XWINDOW (selected_window))
12297 pt = PT;
12298 else
12299 {
12300 pt = marker_position (w->pointm);
12301 pt = max (BEGV, pt);
12302 pt = min (ZV, pt);
12303 }
12304
12305 /* Move iterator to pt starting at cursor_row->start in
12306 a line with infinite width. */
12307 init_to_row_start (&it, w, cursor_row);
12308 it.last_visible_x = INFINITY;
12309 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12310 current_buffer = saved_current_buffer;
12311
12312 /* Position cursor in window. */
12313 if (!hscroll_relative_p && hscroll_step_abs == 0)
12314 hscroll = max (0, (it.current_x
12315 - (ITERATOR_AT_END_OF_LINE_P (&it)
12316 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12317 : (text_area_width / 2))))
12318 / FRAME_COLUMN_WIDTH (it.f);
12319 else if ((!row_r2l_p
12320 && w->cursor.x >= text_area_width - h_margin)
12321 || (row_r2l_p && w->cursor.x <= h_margin))
12322 {
12323 if (hscroll_relative_p)
12324 wanted_x = text_area_width * (1 - hscroll_step_rel)
12325 - h_margin;
12326 else
12327 wanted_x = text_area_width
12328 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12329 - h_margin;
12330 hscroll
12331 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12332 }
12333 else
12334 {
12335 if (hscroll_relative_p)
12336 wanted_x = text_area_width * hscroll_step_rel
12337 + h_margin;
12338 else
12339 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12340 + h_margin;
12341 hscroll
12342 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12343 }
12344 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12345
12346 /* Don't prevent redisplay optimizations if hscroll
12347 hasn't changed, as it will unnecessarily slow down
12348 redisplay. */
12349 if (XFASTINT (w->hscroll) != hscroll)
12350 {
12351 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12352 w->hscroll = make_number (hscroll);
12353 hscrolled_p = 1;
12354 }
12355 }
12356 }
12357
12358 window = w->next;
12359 }
12360
12361 /* Value is non-zero if hscroll of any leaf window has been changed. */
12362 return hscrolled_p;
12363 }
12364
12365
12366 /* Set hscroll so that cursor is visible and not inside horizontal
12367 scroll margins for all windows in the tree rooted at WINDOW. See
12368 also hscroll_window_tree above. Value is non-zero if any window's
12369 hscroll has been changed. If it has, desired matrices on the frame
12370 of WINDOW are cleared. */
12371
12372 static int
12373 hscroll_windows (Lisp_Object window)
12374 {
12375 int hscrolled_p = hscroll_window_tree (window);
12376 if (hscrolled_p)
12377 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12378 return hscrolled_p;
12379 }
12380
12381
12382 \f
12383 /************************************************************************
12384 Redisplay
12385 ************************************************************************/
12386
12387 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12388 to a non-zero value. This is sometimes handy to have in a debugger
12389 session. */
12390
12391 #if GLYPH_DEBUG
12392
12393 /* First and last unchanged row for try_window_id. */
12394
12395 static int debug_first_unchanged_at_end_vpos;
12396 static int debug_last_unchanged_at_beg_vpos;
12397
12398 /* Delta vpos and y. */
12399
12400 static int debug_dvpos, debug_dy;
12401
12402 /* Delta in characters and bytes for try_window_id. */
12403
12404 static EMACS_INT debug_delta, debug_delta_bytes;
12405
12406 /* Values of window_end_pos and window_end_vpos at the end of
12407 try_window_id. */
12408
12409 static EMACS_INT debug_end_vpos;
12410
12411 /* Append a string to W->desired_matrix->method. FMT is a printf
12412 format string. If trace_redisplay_p is non-zero also printf the
12413 resulting string to stderr. */
12414
12415 static void debug_method_add (struct window *, char const *, ...)
12416 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12417
12418 static void
12419 debug_method_add (struct window *w, char const *fmt, ...)
12420 {
12421 char buffer[512];
12422 char *method = w->desired_matrix->method;
12423 int len = strlen (method);
12424 int size = sizeof w->desired_matrix->method;
12425 int remaining = size - len - 1;
12426 va_list ap;
12427
12428 va_start (ap, fmt);
12429 vsprintf (buffer, fmt, ap);
12430 va_end (ap);
12431 if (len && remaining)
12432 {
12433 method[len] = '|';
12434 --remaining, ++len;
12435 }
12436
12437 strncpy (method + len, buffer, remaining);
12438
12439 if (trace_redisplay_p)
12440 fprintf (stderr, "%p (%s): %s\n",
12441 w,
12442 ((BUFFERP (w->buffer)
12443 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12444 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12445 : "no buffer"),
12446 buffer);
12447 }
12448
12449 #endif /* GLYPH_DEBUG */
12450
12451
12452 /* Value is non-zero if all changes in window W, which displays
12453 current_buffer, are in the text between START and END. START is a
12454 buffer position, END is given as a distance from Z. Used in
12455 redisplay_internal for display optimization. */
12456
12457 static inline int
12458 text_outside_line_unchanged_p (struct window *w,
12459 EMACS_INT start, EMACS_INT end)
12460 {
12461 int unchanged_p = 1;
12462
12463 /* If text or overlays have changed, see where. */
12464 if (XFASTINT (w->last_modified) < MODIFF
12465 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12466 {
12467 /* Gap in the line? */
12468 if (GPT < start || Z - GPT < end)
12469 unchanged_p = 0;
12470
12471 /* Changes start in front of the line, or end after it? */
12472 if (unchanged_p
12473 && (BEG_UNCHANGED < start - 1
12474 || END_UNCHANGED < end))
12475 unchanged_p = 0;
12476
12477 /* If selective display, can't optimize if changes start at the
12478 beginning of the line. */
12479 if (unchanged_p
12480 && INTEGERP (BVAR (current_buffer, selective_display))
12481 && XINT (BVAR (current_buffer, selective_display)) > 0
12482 && (BEG_UNCHANGED < start || GPT <= start))
12483 unchanged_p = 0;
12484
12485 /* If there are overlays at the start or end of the line, these
12486 may have overlay strings with newlines in them. A change at
12487 START, for instance, may actually concern the display of such
12488 overlay strings as well, and they are displayed on different
12489 lines. So, quickly rule out this case. (For the future, it
12490 might be desirable to implement something more telling than
12491 just BEG/END_UNCHANGED.) */
12492 if (unchanged_p)
12493 {
12494 if (BEG + BEG_UNCHANGED == start
12495 && overlay_touches_p (start))
12496 unchanged_p = 0;
12497 if (END_UNCHANGED == end
12498 && overlay_touches_p (Z - end))
12499 unchanged_p = 0;
12500 }
12501
12502 /* Under bidi reordering, adding or deleting a character in the
12503 beginning of a paragraph, before the first strong directional
12504 character, can change the base direction of the paragraph (unless
12505 the buffer specifies a fixed paragraph direction), which will
12506 require to redisplay the whole paragraph. It might be worthwhile
12507 to find the paragraph limits and widen the range of redisplayed
12508 lines to that, but for now just give up this optimization. */
12509 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12510 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12511 unchanged_p = 0;
12512 }
12513
12514 return unchanged_p;
12515 }
12516
12517
12518 /* Do a frame update, taking possible shortcuts into account. This is
12519 the main external entry point for redisplay.
12520
12521 If the last redisplay displayed an echo area message and that message
12522 is no longer requested, we clear the echo area or bring back the
12523 mini-buffer if that is in use. */
12524
12525 void
12526 redisplay (void)
12527 {
12528 redisplay_internal ();
12529 }
12530
12531
12532 static Lisp_Object
12533 overlay_arrow_string_or_property (Lisp_Object var)
12534 {
12535 Lisp_Object val;
12536
12537 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12538 return val;
12539
12540 return Voverlay_arrow_string;
12541 }
12542
12543 /* Return 1 if there are any overlay-arrows in current_buffer. */
12544 static int
12545 overlay_arrow_in_current_buffer_p (void)
12546 {
12547 Lisp_Object vlist;
12548
12549 for (vlist = Voverlay_arrow_variable_list;
12550 CONSP (vlist);
12551 vlist = XCDR (vlist))
12552 {
12553 Lisp_Object var = XCAR (vlist);
12554 Lisp_Object val;
12555
12556 if (!SYMBOLP (var))
12557 continue;
12558 val = find_symbol_value (var);
12559 if (MARKERP (val)
12560 && current_buffer == XMARKER (val)->buffer)
12561 return 1;
12562 }
12563 return 0;
12564 }
12565
12566
12567 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12568 has changed. */
12569
12570 static int
12571 overlay_arrows_changed_p (void)
12572 {
12573 Lisp_Object vlist;
12574
12575 for (vlist = Voverlay_arrow_variable_list;
12576 CONSP (vlist);
12577 vlist = XCDR (vlist))
12578 {
12579 Lisp_Object var = XCAR (vlist);
12580 Lisp_Object val, pstr;
12581
12582 if (!SYMBOLP (var))
12583 continue;
12584 val = find_symbol_value (var);
12585 if (!MARKERP (val))
12586 continue;
12587 if (! EQ (COERCE_MARKER (val),
12588 Fget (var, Qlast_arrow_position))
12589 || ! (pstr = overlay_arrow_string_or_property (var),
12590 EQ (pstr, Fget (var, Qlast_arrow_string))))
12591 return 1;
12592 }
12593 return 0;
12594 }
12595
12596 /* Mark overlay arrows to be updated on next redisplay. */
12597
12598 static void
12599 update_overlay_arrows (int up_to_date)
12600 {
12601 Lisp_Object vlist;
12602
12603 for (vlist = Voverlay_arrow_variable_list;
12604 CONSP (vlist);
12605 vlist = XCDR (vlist))
12606 {
12607 Lisp_Object var = XCAR (vlist);
12608
12609 if (!SYMBOLP (var))
12610 continue;
12611
12612 if (up_to_date > 0)
12613 {
12614 Lisp_Object val = find_symbol_value (var);
12615 Fput (var, Qlast_arrow_position,
12616 COERCE_MARKER (val));
12617 Fput (var, Qlast_arrow_string,
12618 overlay_arrow_string_or_property (var));
12619 }
12620 else if (up_to_date < 0
12621 || !NILP (Fget (var, Qlast_arrow_position)))
12622 {
12623 Fput (var, Qlast_arrow_position, Qt);
12624 Fput (var, Qlast_arrow_string, Qt);
12625 }
12626 }
12627 }
12628
12629
12630 /* Return overlay arrow string to display at row.
12631 Return integer (bitmap number) for arrow bitmap in left fringe.
12632 Return nil if no overlay arrow. */
12633
12634 static Lisp_Object
12635 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12636 {
12637 Lisp_Object vlist;
12638
12639 for (vlist = Voverlay_arrow_variable_list;
12640 CONSP (vlist);
12641 vlist = XCDR (vlist))
12642 {
12643 Lisp_Object var = XCAR (vlist);
12644 Lisp_Object val;
12645
12646 if (!SYMBOLP (var))
12647 continue;
12648
12649 val = find_symbol_value (var);
12650
12651 if (MARKERP (val)
12652 && current_buffer == XMARKER (val)->buffer
12653 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12654 {
12655 if (FRAME_WINDOW_P (it->f)
12656 /* FIXME: if ROW->reversed_p is set, this should test
12657 the right fringe, not the left one. */
12658 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12659 {
12660 #ifdef HAVE_WINDOW_SYSTEM
12661 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12662 {
12663 int fringe_bitmap;
12664 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12665 return make_number (fringe_bitmap);
12666 }
12667 #endif
12668 return make_number (-1); /* Use default arrow bitmap */
12669 }
12670 return overlay_arrow_string_or_property (var);
12671 }
12672 }
12673
12674 return Qnil;
12675 }
12676
12677 /* Return 1 if point moved out of or into a composition. Otherwise
12678 return 0. PREV_BUF and PREV_PT are the last point buffer and
12679 position. BUF and PT are the current point buffer and position. */
12680
12681 static int
12682 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12683 struct buffer *buf, EMACS_INT pt)
12684 {
12685 EMACS_INT start, end;
12686 Lisp_Object prop;
12687 Lisp_Object buffer;
12688
12689 XSETBUFFER (buffer, buf);
12690 /* Check a composition at the last point if point moved within the
12691 same buffer. */
12692 if (prev_buf == buf)
12693 {
12694 if (prev_pt == pt)
12695 /* Point didn't move. */
12696 return 0;
12697
12698 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12699 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12700 && COMPOSITION_VALID_P (start, end, prop)
12701 && start < prev_pt && end > prev_pt)
12702 /* The last point was within the composition. Return 1 iff
12703 point moved out of the composition. */
12704 return (pt <= start || pt >= end);
12705 }
12706
12707 /* Check a composition at the current point. */
12708 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12709 && find_composition (pt, -1, &start, &end, &prop, buffer)
12710 && COMPOSITION_VALID_P (start, end, prop)
12711 && start < pt && end > pt);
12712 }
12713
12714
12715 /* Reconsider the setting of B->clip_changed which is displayed
12716 in window W. */
12717
12718 static inline void
12719 reconsider_clip_changes (struct window *w, struct buffer *b)
12720 {
12721 if (b->clip_changed
12722 && !NILP (w->window_end_valid)
12723 && w->current_matrix->buffer == b
12724 && w->current_matrix->zv == BUF_ZV (b)
12725 && w->current_matrix->begv == BUF_BEGV (b))
12726 b->clip_changed = 0;
12727
12728 /* If display wasn't paused, and W is not a tool bar window, see if
12729 point has been moved into or out of a composition. In that case,
12730 we set b->clip_changed to 1 to force updating the screen. If
12731 b->clip_changed has already been set to 1, we can skip this
12732 check. */
12733 if (!b->clip_changed
12734 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12735 {
12736 EMACS_INT pt;
12737
12738 if (w == XWINDOW (selected_window))
12739 pt = PT;
12740 else
12741 pt = marker_position (w->pointm);
12742
12743 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12744 || pt != XINT (w->last_point))
12745 && check_point_in_composition (w->current_matrix->buffer,
12746 XINT (w->last_point),
12747 XBUFFER (w->buffer), pt))
12748 b->clip_changed = 1;
12749 }
12750 }
12751 \f
12752
12753 /* Select FRAME to forward the values of frame-local variables into C
12754 variables so that the redisplay routines can access those values
12755 directly. */
12756
12757 static void
12758 select_frame_for_redisplay (Lisp_Object frame)
12759 {
12760 Lisp_Object tail, tem;
12761 Lisp_Object old = selected_frame;
12762 struct Lisp_Symbol *sym;
12763
12764 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12765
12766 selected_frame = frame;
12767
12768 do {
12769 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12770 if (CONSP (XCAR (tail))
12771 && (tem = XCAR (XCAR (tail)),
12772 SYMBOLP (tem))
12773 && (sym = indirect_variable (XSYMBOL (tem)),
12774 sym->redirect == SYMBOL_LOCALIZED)
12775 && sym->val.blv->frame_local)
12776 /* Use find_symbol_value rather than Fsymbol_value
12777 to avoid an error if it is void. */
12778 find_symbol_value (tem);
12779 } while (!EQ (frame, old) && (frame = old, 1));
12780 }
12781
12782
12783 #define STOP_POLLING \
12784 do { if (! polling_stopped_here) stop_polling (); \
12785 polling_stopped_here = 1; } while (0)
12786
12787 #define RESUME_POLLING \
12788 do { if (polling_stopped_here) start_polling (); \
12789 polling_stopped_here = 0; } while (0)
12790
12791
12792 /* Perhaps in the future avoid recentering windows if it
12793 is not necessary; currently that causes some problems. */
12794
12795 static void
12796 redisplay_internal (void)
12797 {
12798 struct window *w = XWINDOW (selected_window);
12799 struct window *sw;
12800 struct frame *fr;
12801 int pending;
12802 int must_finish = 0;
12803 struct text_pos tlbufpos, tlendpos;
12804 int number_of_visible_frames;
12805 int count, count1;
12806 struct frame *sf;
12807 int polling_stopped_here = 0;
12808 Lisp_Object old_frame = selected_frame;
12809
12810 /* Non-zero means redisplay has to consider all windows on all
12811 frames. Zero means, only selected_window is considered. */
12812 int consider_all_windows_p;
12813
12814 /* Non-zero means redisplay has to redisplay the miniwindow */
12815 int update_miniwindow_p = 0;
12816
12817 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12818
12819 /* No redisplay if running in batch mode or frame is not yet fully
12820 initialized, or redisplay is explicitly turned off by setting
12821 Vinhibit_redisplay. */
12822 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12823 || !NILP (Vinhibit_redisplay))
12824 return;
12825
12826 /* Don't examine these until after testing Vinhibit_redisplay.
12827 When Emacs is shutting down, perhaps because its connection to
12828 X has dropped, we should not look at them at all. */
12829 fr = XFRAME (w->frame);
12830 sf = SELECTED_FRAME ();
12831
12832 if (!fr->glyphs_initialized_p)
12833 return;
12834
12835 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12836 if (popup_activated ())
12837 return;
12838 #endif
12839
12840 /* I don't think this happens but let's be paranoid. */
12841 if (redisplaying_p)
12842 return;
12843
12844 /* Record a function that resets redisplaying_p to its old value
12845 when we leave this function. */
12846 count = SPECPDL_INDEX ();
12847 record_unwind_protect (unwind_redisplay,
12848 Fcons (make_number (redisplaying_p), selected_frame));
12849 ++redisplaying_p;
12850 specbind (Qinhibit_free_realized_faces, Qnil);
12851
12852 {
12853 Lisp_Object tail, frame;
12854
12855 FOR_EACH_FRAME (tail, frame)
12856 {
12857 struct frame *f = XFRAME (frame);
12858 f->already_hscrolled_p = 0;
12859 }
12860 }
12861
12862 retry:
12863 /* Remember the currently selected window. */
12864 sw = w;
12865
12866 if (!EQ (old_frame, selected_frame)
12867 && FRAME_LIVE_P (XFRAME (old_frame)))
12868 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12869 selected_frame and selected_window to be temporarily out-of-sync so
12870 when we come back here via `goto retry', we need to resync because we
12871 may need to run Elisp code (via prepare_menu_bars). */
12872 select_frame_for_redisplay (old_frame);
12873
12874 pending = 0;
12875 reconsider_clip_changes (w, current_buffer);
12876 last_escape_glyph_frame = NULL;
12877 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12878 last_glyphless_glyph_frame = NULL;
12879 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12880
12881 /* If new fonts have been loaded that make a glyph matrix adjustment
12882 necessary, do it. */
12883 if (fonts_changed_p)
12884 {
12885 adjust_glyphs (NULL);
12886 ++windows_or_buffers_changed;
12887 fonts_changed_p = 0;
12888 }
12889
12890 /* If face_change_count is non-zero, init_iterator will free all
12891 realized faces, which includes the faces referenced from current
12892 matrices. So, we can't reuse current matrices in this case. */
12893 if (face_change_count)
12894 ++windows_or_buffers_changed;
12895
12896 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12897 && FRAME_TTY (sf)->previous_frame != sf)
12898 {
12899 /* Since frames on a single ASCII terminal share the same
12900 display area, displaying a different frame means redisplay
12901 the whole thing. */
12902 windows_or_buffers_changed++;
12903 SET_FRAME_GARBAGED (sf);
12904 #ifndef DOS_NT
12905 set_tty_color_mode (FRAME_TTY (sf), sf);
12906 #endif
12907 FRAME_TTY (sf)->previous_frame = sf;
12908 }
12909
12910 /* Set the visible flags for all frames. Do this before checking
12911 for resized or garbaged frames; they want to know if their frames
12912 are visible. See the comment in frame.h for
12913 FRAME_SAMPLE_VISIBILITY. */
12914 {
12915 Lisp_Object tail, frame;
12916
12917 number_of_visible_frames = 0;
12918
12919 FOR_EACH_FRAME (tail, frame)
12920 {
12921 struct frame *f = XFRAME (frame);
12922
12923 FRAME_SAMPLE_VISIBILITY (f);
12924 if (FRAME_VISIBLE_P (f))
12925 ++number_of_visible_frames;
12926 clear_desired_matrices (f);
12927 }
12928 }
12929
12930 /* Notice any pending interrupt request to change frame size. */
12931 do_pending_window_change (1);
12932
12933 /* do_pending_window_change could change the selected_window due to
12934 frame resizing which makes the selected window too small. */
12935 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12936 {
12937 sw = w;
12938 reconsider_clip_changes (w, current_buffer);
12939 }
12940
12941 /* Clear frames marked as garbaged. */
12942 if (frame_garbaged)
12943 clear_garbaged_frames ();
12944
12945 /* Build menubar and tool-bar items. */
12946 if (NILP (Vmemory_full))
12947 prepare_menu_bars ();
12948
12949 if (windows_or_buffers_changed)
12950 update_mode_lines++;
12951
12952 /* Detect case that we need to write or remove a star in the mode line. */
12953 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12954 {
12955 w->update_mode_line = Qt;
12956 if (buffer_shared > 1)
12957 update_mode_lines++;
12958 }
12959
12960 /* Avoid invocation of point motion hooks by `current_column' below. */
12961 count1 = SPECPDL_INDEX ();
12962 specbind (Qinhibit_point_motion_hooks, Qt);
12963
12964 /* If %c is in the mode line, update it if needed. */
12965 if (!NILP (w->column_number_displayed)
12966 /* This alternative quickly identifies a common case
12967 where no change is needed. */
12968 && !(PT == XFASTINT (w->last_point)
12969 && XFASTINT (w->last_modified) >= MODIFF
12970 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12971 && (XFASTINT (w->column_number_displayed) != current_column ()))
12972 w->update_mode_line = Qt;
12973
12974 unbind_to (count1, Qnil);
12975
12976 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12977
12978 /* The variable buffer_shared is set in redisplay_window and
12979 indicates that we redisplay a buffer in different windows. See
12980 there. */
12981 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12982 || cursor_type_changed);
12983
12984 /* If specs for an arrow have changed, do thorough redisplay
12985 to ensure we remove any arrow that should no longer exist. */
12986 if (overlay_arrows_changed_p ())
12987 consider_all_windows_p = windows_or_buffers_changed = 1;
12988
12989 /* Normally the message* functions will have already displayed and
12990 updated the echo area, but the frame may have been trashed, or
12991 the update may have been preempted, so display the echo area
12992 again here. Checking message_cleared_p captures the case that
12993 the echo area should be cleared. */
12994 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12995 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12996 || (message_cleared_p
12997 && minibuf_level == 0
12998 /* If the mini-window is currently selected, this means the
12999 echo-area doesn't show through. */
13000 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13001 {
13002 int window_height_changed_p = echo_area_display (0);
13003
13004 if (message_cleared_p)
13005 update_miniwindow_p = 1;
13006
13007 must_finish = 1;
13008
13009 /* If we don't display the current message, don't clear the
13010 message_cleared_p flag, because, if we did, we wouldn't clear
13011 the echo area in the next redisplay which doesn't preserve
13012 the echo area. */
13013 if (!display_last_displayed_message_p)
13014 message_cleared_p = 0;
13015
13016 if (fonts_changed_p)
13017 goto retry;
13018 else if (window_height_changed_p)
13019 {
13020 consider_all_windows_p = 1;
13021 ++update_mode_lines;
13022 ++windows_or_buffers_changed;
13023
13024 /* If window configuration was changed, frames may have been
13025 marked garbaged. Clear them or we will experience
13026 surprises wrt scrolling. */
13027 if (frame_garbaged)
13028 clear_garbaged_frames ();
13029 }
13030 }
13031 else if (EQ (selected_window, minibuf_window)
13032 && (current_buffer->clip_changed
13033 || XFASTINT (w->last_modified) < MODIFF
13034 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13035 && resize_mini_window (w, 0))
13036 {
13037 /* Resized active mini-window to fit the size of what it is
13038 showing if its contents might have changed. */
13039 must_finish = 1;
13040 /* FIXME: this causes all frames to be updated, which seems unnecessary
13041 since only the current frame needs to be considered. This function needs
13042 to be rewritten with two variables, consider_all_windows and
13043 consider_all_frames. */
13044 consider_all_windows_p = 1;
13045 ++windows_or_buffers_changed;
13046 ++update_mode_lines;
13047
13048 /* If window configuration was changed, frames may have been
13049 marked garbaged. Clear them or we will experience
13050 surprises wrt scrolling. */
13051 if (frame_garbaged)
13052 clear_garbaged_frames ();
13053 }
13054
13055
13056 /* If showing the region, and mark has changed, we must redisplay
13057 the whole window. The assignment to this_line_start_pos prevents
13058 the optimization directly below this if-statement. */
13059 if (((!NILP (Vtransient_mark_mode)
13060 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13061 != !NILP (w->region_showing))
13062 || (!NILP (w->region_showing)
13063 && !EQ (w->region_showing,
13064 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13065 CHARPOS (this_line_start_pos) = 0;
13066
13067 /* Optimize the case that only the line containing the cursor in the
13068 selected window has changed. Variables starting with this_ are
13069 set in display_line and record information about the line
13070 containing the cursor. */
13071 tlbufpos = this_line_start_pos;
13072 tlendpos = this_line_end_pos;
13073 if (!consider_all_windows_p
13074 && CHARPOS (tlbufpos) > 0
13075 && NILP (w->update_mode_line)
13076 && !current_buffer->clip_changed
13077 && !current_buffer->prevent_redisplay_optimizations_p
13078 && FRAME_VISIBLE_P (XFRAME (w->frame))
13079 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13080 /* Make sure recorded data applies to current buffer, etc. */
13081 && this_line_buffer == current_buffer
13082 && current_buffer == XBUFFER (w->buffer)
13083 && NILP (w->force_start)
13084 && NILP (w->optional_new_start)
13085 /* Point must be on the line that we have info recorded about. */
13086 && PT >= CHARPOS (tlbufpos)
13087 && PT <= Z - CHARPOS (tlendpos)
13088 /* All text outside that line, including its final newline,
13089 must be unchanged. */
13090 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13091 CHARPOS (tlendpos)))
13092 {
13093 if (CHARPOS (tlbufpos) > BEGV
13094 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13095 && (CHARPOS (tlbufpos) == ZV
13096 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13097 /* Former continuation line has disappeared by becoming empty. */
13098 goto cancel;
13099 else if (XFASTINT (w->last_modified) < MODIFF
13100 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13101 || MINI_WINDOW_P (w))
13102 {
13103 /* We have to handle the case of continuation around a
13104 wide-column character (see the comment in indent.c around
13105 line 1340).
13106
13107 For instance, in the following case:
13108
13109 -------- Insert --------
13110 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13111 J_I_ ==> J_I_ `^^' are cursors.
13112 ^^ ^^
13113 -------- --------
13114
13115 As we have to redraw the line above, we cannot use this
13116 optimization. */
13117
13118 struct it it;
13119 int line_height_before = this_line_pixel_height;
13120
13121 /* Note that start_display will handle the case that the
13122 line starting at tlbufpos is a continuation line. */
13123 start_display (&it, w, tlbufpos);
13124
13125 /* Implementation note: It this still necessary? */
13126 if (it.current_x != this_line_start_x)
13127 goto cancel;
13128
13129 TRACE ((stderr, "trying display optimization 1\n"));
13130 w->cursor.vpos = -1;
13131 overlay_arrow_seen = 0;
13132 it.vpos = this_line_vpos;
13133 it.current_y = this_line_y;
13134 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13135 display_line (&it);
13136
13137 /* If line contains point, is not continued,
13138 and ends at same distance from eob as before, we win. */
13139 if (w->cursor.vpos >= 0
13140 /* Line is not continued, otherwise this_line_start_pos
13141 would have been set to 0 in display_line. */
13142 && CHARPOS (this_line_start_pos)
13143 /* Line ends as before. */
13144 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13145 /* Line has same height as before. Otherwise other lines
13146 would have to be shifted up or down. */
13147 && this_line_pixel_height == line_height_before)
13148 {
13149 /* If this is not the window's last line, we must adjust
13150 the charstarts of the lines below. */
13151 if (it.current_y < it.last_visible_y)
13152 {
13153 struct glyph_row *row
13154 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13155 EMACS_INT delta, delta_bytes;
13156
13157 /* We used to distinguish between two cases here,
13158 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13159 when the line ends in a newline or the end of the
13160 buffer's accessible portion. But both cases did
13161 the same, so they were collapsed. */
13162 delta = (Z
13163 - CHARPOS (tlendpos)
13164 - MATRIX_ROW_START_CHARPOS (row));
13165 delta_bytes = (Z_BYTE
13166 - BYTEPOS (tlendpos)
13167 - MATRIX_ROW_START_BYTEPOS (row));
13168
13169 increment_matrix_positions (w->current_matrix,
13170 this_line_vpos + 1,
13171 w->current_matrix->nrows,
13172 delta, delta_bytes);
13173 }
13174
13175 /* If this row displays text now but previously didn't,
13176 or vice versa, w->window_end_vpos may have to be
13177 adjusted. */
13178 if ((it.glyph_row - 1)->displays_text_p)
13179 {
13180 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13181 XSETINT (w->window_end_vpos, this_line_vpos);
13182 }
13183 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13184 && this_line_vpos > 0)
13185 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13186 w->window_end_valid = Qnil;
13187
13188 /* Update hint: No need to try to scroll in update_window. */
13189 w->desired_matrix->no_scrolling_p = 1;
13190
13191 #if GLYPH_DEBUG
13192 *w->desired_matrix->method = 0;
13193 debug_method_add (w, "optimization 1");
13194 #endif
13195 #ifdef HAVE_WINDOW_SYSTEM
13196 update_window_fringes (w, 0);
13197 #endif
13198 goto update;
13199 }
13200 else
13201 goto cancel;
13202 }
13203 else if (/* Cursor position hasn't changed. */
13204 PT == XFASTINT (w->last_point)
13205 /* Make sure the cursor was last displayed
13206 in this window. Otherwise we have to reposition it. */
13207 && 0 <= w->cursor.vpos
13208 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13209 {
13210 if (!must_finish)
13211 {
13212 do_pending_window_change (1);
13213 /* If selected_window changed, redisplay again. */
13214 if (WINDOWP (selected_window)
13215 && (w = XWINDOW (selected_window)) != sw)
13216 goto retry;
13217
13218 /* We used to always goto end_of_redisplay here, but this
13219 isn't enough if we have a blinking cursor. */
13220 if (w->cursor_off_p == w->last_cursor_off_p)
13221 goto end_of_redisplay;
13222 }
13223 goto update;
13224 }
13225 /* If highlighting the region, or if the cursor is in the echo area,
13226 then we can't just move the cursor. */
13227 else if (! (!NILP (Vtransient_mark_mode)
13228 && !NILP (BVAR (current_buffer, mark_active)))
13229 && (EQ (selected_window,
13230 BVAR (current_buffer, last_selected_window))
13231 || highlight_nonselected_windows)
13232 && NILP (w->region_showing)
13233 && NILP (Vshow_trailing_whitespace)
13234 && !cursor_in_echo_area)
13235 {
13236 struct it it;
13237 struct glyph_row *row;
13238
13239 /* Skip from tlbufpos to PT and see where it is. Note that
13240 PT may be in invisible text. If so, we will end at the
13241 next visible position. */
13242 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13243 NULL, DEFAULT_FACE_ID);
13244 it.current_x = this_line_start_x;
13245 it.current_y = this_line_y;
13246 it.vpos = this_line_vpos;
13247
13248 /* The call to move_it_to stops in front of PT, but
13249 moves over before-strings. */
13250 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13251
13252 if (it.vpos == this_line_vpos
13253 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13254 row->enabled_p))
13255 {
13256 xassert (this_line_vpos == it.vpos);
13257 xassert (this_line_y == it.current_y);
13258 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13259 #if GLYPH_DEBUG
13260 *w->desired_matrix->method = 0;
13261 debug_method_add (w, "optimization 3");
13262 #endif
13263 goto update;
13264 }
13265 else
13266 goto cancel;
13267 }
13268
13269 cancel:
13270 /* Text changed drastically or point moved off of line. */
13271 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13272 }
13273
13274 CHARPOS (this_line_start_pos) = 0;
13275 consider_all_windows_p |= buffer_shared > 1;
13276 ++clear_face_cache_count;
13277 #ifdef HAVE_WINDOW_SYSTEM
13278 ++clear_image_cache_count;
13279 #endif
13280
13281 /* Build desired matrices, and update the display. If
13282 consider_all_windows_p is non-zero, do it for all windows on all
13283 frames. Otherwise do it for selected_window, only. */
13284
13285 if (consider_all_windows_p)
13286 {
13287 Lisp_Object tail, frame;
13288
13289 FOR_EACH_FRAME (tail, frame)
13290 XFRAME (frame)->updated_p = 0;
13291
13292 /* Recompute # windows showing selected buffer. This will be
13293 incremented each time such a window is displayed. */
13294 buffer_shared = 0;
13295
13296 FOR_EACH_FRAME (tail, frame)
13297 {
13298 struct frame *f = XFRAME (frame);
13299
13300 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13301 {
13302 if (! EQ (frame, selected_frame))
13303 /* Select the frame, for the sake of frame-local
13304 variables. */
13305 select_frame_for_redisplay (frame);
13306
13307 /* Mark all the scroll bars to be removed; we'll redeem
13308 the ones we want when we redisplay their windows. */
13309 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13310 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13311
13312 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13313 redisplay_windows (FRAME_ROOT_WINDOW (f));
13314
13315 /* The X error handler may have deleted that frame. */
13316 if (!FRAME_LIVE_P (f))
13317 continue;
13318
13319 /* Any scroll bars which redisplay_windows should have
13320 nuked should now go away. */
13321 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13322 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13323
13324 /* If fonts changed, display again. */
13325 /* ??? rms: I suspect it is a mistake to jump all the way
13326 back to retry here. It should just retry this frame. */
13327 if (fonts_changed_p)
13328 goto retry;
13329
13330 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13331 {
13332 /* See if we have to hscroll. */
13333 if (!f->already_hscrolled_p)
13334 {
13335 f->already_hscrolled_p = 1;
13336 if (hscroll_windows (f->root_window))
13337 goto retry;
13338 }
13339
13340 /* Prevent various kinds of signals during display
13341 update. stdio is not robust about handling
13342 signals, which can cause an apparent I/O
13343 error. */
13344 if (interrupt_input)
13345 unrequest_sigio ();
13346 STOP_POLLING;
13347
13348 /* Update the display. */
13349 set_window_update_flags (XWINDOW (f->root_window), 1);
13350 pending |= update_frame (f, 0, 0);
13351 f->updated_p = 1;
13352 }
13353 }
13354 }
13355
13356 if (!EQ (old_frame, selected_frame)
13357 && FRAME_LIVE_P (XFRAME (old_frame)))
13358 /* We played a bit fast-and-loose above and allowed selected_frame
13359 and selected_window to be temporarily out-of-sync but let's make
13360 sure this stays contained. */
13361 select_frame_for_redisplay (old_frame);
13362 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13363
13364 if (!pending)
13365 {
13366 /* Do the mark_window_display_accurate after all windows have
13367 been redisplayed because this call resets flags in buffers
13368 which are needed for proper redisplay. */
13369 FOR_EACH_FRAME (tail, frame)
13370 {
13371 struct frame *f = XFRAME (frame);
13372 if (f->updated_p)
13373 {
13374 mark_window_display_accurate (f->root_window, 1);
13375 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13376 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13377 }
13378 }
13379 }
13380 }
13381 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13382 {
13383 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13384 struct frame *mini_frame;
13385
13386 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13387 /* Use list_of_error, not Qerror, so that
13388 we catch only errors and don't run the debugger. */
13389 internal_condition_case_1 (redisplay_window_1, selected_window,
13390 list_of_error,
13391 redisplay_window_error);
13392 if (update_miniwindow_p)
13393 internal_condition_case_1 (redisplay_window_1, mini_window,
13394 list_of_error,
13395 redisplay_window_error);
13396
13397 /* Compare desired and current matrices, perform output. */
13398
13399 update:
13400 /* If fonts changed, display again. */
13401 if (fonts_changed_p)
13402 goto retry;
13403
13404 /* Prevent various kinds of signals during display update.
13405 stdio is not robust about handling signals,
13406 which can cause an apparent I/O error. */
13407 if (interrupt_input)
13408 unrequest_sigio ();
13409 STOP_POLLING;
13410
13411 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13412 {
13413 if (hscroll_windows (selected_window))
13414 goto retry;
13415
13416 XWINDOW (selected_window)->must_be_updated_p = 1;
13417 pending = update_frame (sf, 0, 0);
13418 }
13419
13420 /* We may have called echo_area_display at the top of this
13421 function. If the echo area is on another frame, that may
13422 have put text on a frame other than the selected one, so the
13423 above call to update_frame would not have caught it. Catch
13424 it here. */
13425 mini_window = FRAME_MINIBUF_WINDOW (sf);
13426 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13427
13428 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13429 {
13430 XWINDOW (mini_window)->must_be_updated_p = 1;
13431 pending |= update_frame (mini_frame, 0, 0);
13432 if (!pending && hscroll_windows (mini_window))
13433 goto retry;
13434 }
13435 }
13436
13437 /* If display was paused because of pending input, make sure we do a
13438 thorough update the next time. */
13439 if (pending)
13440 {
13441 /* Prevent the optimization at the beginning of
13442 redisplay_internal that tries a single-line update of the
13443 line containing the cursor in the selected window. */
13444 CHARPOS (this_line_start_pos) = 0;
13445
13446 /* Let the overlay arrow be updated the next time. */
13447 update_overlay_arrows (0);
13448
13449 /* If we pause after scrolling, some rows in the current
13450 matrices of some windows are not valid. */
13451 if (!WINDOW_FULL_WIDTH_P (w)
13452 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13453 update_mode_lines = 1;
13454 }
13455 else
13456 {
13457 if (!consider_all_windows_p)
13458 {
13459 /* This has already been done above if
13460 consider_all_windows_p is set. */
13461 mark_window_display_accurate_1 (w, 1);
13462
13463 /* Say overlay arrows are up to date. */
13464 update_overlay_arrows (1);
13465
13466 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13467 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13468 }
13469
13470 update_mode_lines = 0;
13471 windows_or_buffers_changed = 0;
13472 cursor_type_changed = 0;
13473 }
13474
13475 /* Start SIGIO interrupts coming again. Having them off during the
13476 code above makes it less likely one will discard output, but not
13477 impossible, since there might be stuff in the system buffer here.
13478 But it is much hairier to try to do anything about that. */
13479 if (interrupt_input)
13480 request_sigio ();
13481 RESUME_POLLING;
13482
13483 /* If a frame has become visible which was not before, redisplay
13484 again, so that we display it. Expose events for such a frame
13485 (which it gets when becoming visible) don't call the parts of
13486 redisplay constructing glyphs, so simply exposing a frame won't
13487 display anything in this case. So, we have to display these
13488 frames here explicitly. */
13489 if (!pending)
13490 {
13491 Lisp_Object tail, frame;
13492 int new_count = 0;
13493
13494 FOR_EACH_FRAME (tail, frame)
13495 {
13496 int this_is_visible = 0;
13497
13498 if (XFRAME (frame)->visible)
13499 this_is_visible = 1;
13500 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13501 if (XFRAME (frame)->visible)
13502 this_is_visible = 1;
13503
13504 if (this_is_visible)
13505 new_count++;
13506 }
13507
13508 if (new_count != number_of_visible_frames)
13509 windows_or_buffers_changed++;
13510 }
13511
13512 /* Change frame size now if a change is pending. */
13513 do_pending_window_change (1);
13514
13515 /* If we just did a pending size change, or have additional
13516 visible frames, or selected_window changed, redisplay again. */
13517 if ((windows_or_buffers_changed && !pending)
13518 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13519 goto retry;
13520
13521 /* Clear the face and image caches.
13522
13523 We used to do this only if consider_all_windows_p. But the cache
13524 needs to be cleared if a timer creates images in the current
13525 buffer (e.g. the test case in Bug#6230). */
13526
13527 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13528 {
13529 clear_face_cache (0);
13530 clear_face_cache_count = 0;
13531 }
13532
13533 #ifdef HAVE_WINDOW_SYSTEM
13534 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13535 {
13536 clear_image_caches (Qnil);
13537 clear_image_cache_count = 0;
13538 }
13539 #endif /* HAVE_WINDOW_SYSTEM */
13540
13541 end_of_redisplay:
13542 unbind_to (count, Qnil);
13543 RESUME_POLLING;
13544 }
13545
13546
13547 /* Redisplay, but leave alone any recent echo area message unless
13548 another message has been requested in its place.
13549
13550 This is useful in situations where you need to redisplay but no
13551 user action has occurred, making it inappropriate for the message
13552 area to be cleared. See tracking_off and
13553 wait_reading_process_output for examples of these situations.
13554
13555 FROM_WHERE is an integer saying from where this function was
13556 called. This is useful for debugging. */
13557
13558 void
13559 redisplay_preserve_echo_area (int from_where)
13560 {
13561 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13562
13563 if (!NILP (echo_area_buffer[1]))
13564 {
13565 /* We have a previously displayed message, but no current
13566 message. Redisplay the previous message. */
13567 display_last_displayed_message_p = 1;
13568 redisplay_internal ();
13569 display_last_displayed_message_p = 0;
13570 }
13571 else
13572 redisplay_internal ();
13573
13574 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13575 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13576 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13577 }
13578
13579
13580 /* Function registered with record_unwind_protect in
13581 redisplay_internal. Reset redisplaying_p to the value it had
13582 before redisplay_internal was called, and clear
13583 prevent_freeing_realized_faces_p. It also selects the previously
13584 selected frame, unless it has been deleted (by an X connection
13585 failure during redisplay, for example). */
13586
13587 static Lisp_Object
13588 unwind_redisplay (Lisp_Object val)
13589 {
13590 Lisp_Object old_redisplaying_p, old_frame;
13591
13592 old_redisplaying_p = XCAR (val);
13593 redisplaying_p = XFASTINT (old_redisplaying_p);
13594 old_frame = XCDR (val);
13595 if (! EQ (old_frame, selected_frame)
13596 && FRAME_LIVE_P (XFRAME (old_frame)))
13597 select_frame_for_redisplay (old_frame);
13598 return Qnil;
13599 }
13600
13601
13602 /* Mark the display of window W as accurate or inaccurate. If
13603 ACCURATE_P is non-zero mark display of W as accurate. If
13604 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13605 redisplay_internal is called. */
13606
13607 static void
13608 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13609 {
13610 if (BUFFERP (w->buffer))
13611 {
13612 struct buffer *b = XBUFFER (w->buffer);
13613
13614 w->last_modified
13615 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13616 w->last_overlay_modified
13617 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13618 w->last_had_star
13619 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13620
13621 if (accurate_p)
13622 {
13623 b->clip_changed = 0;
13624 b->prevent_redisplay_optimizations_p = 0;
13625
13626 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13627 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13628 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13629 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13630
13631 w->current_matrix->buffer = b;
13632 w->current_matrix->begv = BUF_BEGV (b);
13633 w->current_matrix->zv = BUF_ZV (b);
13634
13635 w->last_cursor = w->cursor;
13636 w->last_cursor_off_p = w->cursor_off_p;
13637
13638 if (w == XWINDOW (selected_window))
13639 w->last_point = make_number (BUF_PT (b));
13640 else
13641 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13642 }
13643 }
13644
13645 if (accurate_p)
13646 {
13647 w->window_end_valid = w->buffer;
13648 w->update_mode_line = Qnil;
13649 }
13650 }
13651
13652
13653 /* Mark the display of windows in the window tree rooted at WINDOW as
13654 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13655 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13656 be redisplayed the next time redisplay_internal is called. */
13657
13658 void
13659 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13660 {
13661 struct window *w;
13662
13663 for (; !NILP (window); window = w->next)
13664 {
13665 w = XWINDOW (window);
13666 mark_window_display_accurate_1 (w, accurate_p);
13667
13668 if (!NILP (w->vchild))
13669 mark_window_display_accurate (w->vchild, accurate_p);
13670 if (!NILP (w->hchild))
13671 mark_window_display_accurate (w->hchild, accurate_p);
13672 }
13673
13674 if (accurate_p)
13675 {
13676 update_overlay_arrows (1);
13677 }
13678 else
13679 {
13680 /* Force a thorough redisplay the next time by setting
13681 last_arrow_position and last_arrow_string to t, which is
13682 unequal to any useful value of Voverlay_arrow_... */
13683 update_overlay_arrows (-1);
13684 }
13685 }
13686
13687
13688 /* Return value in display table DP (Lisp_Char_Table *) for character
13689 C. Since a display table doesn't have any parent, we don't have to
13690 follow parent. Do not call this function directly but use the
13691 macro DISP_CHAR_VECTOR. */
13692
13693 Lisp_Object
13694 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13695 {
13696 Lisp_Object val;
13697
13698 if (ASCII_CHAR_P (c))
13699 {
13700 val = dp->ascii;
13701 if (SUB_CHAR_TABLE_P (val))
13702 val = XSUB_CHAR_TABLE (val)->contents[c];
13703 }
13704 else
13705 {
13706 Lisp_Object table;
13707
13708 XSETCHAR_TABLE (table, dp);
13709 val = char_table_ref (table, c);
13710 }
13711 if (NILP (val))
13712 val = dp->defalt;
13713 return val;
13714 }
13715
13716
13717 \f
13718 /***********************************************************************
13719 Window Redisplay
13720 ***********************************************************************/
13721
13722 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13723
13724 static void
13725 redisplay_windows (Lisp_Object window)
13726 {
13727 while (!NILP (window))
13728 {
13729 struct window *w = XWINDOW (window);
13730
13731 if (!NILP (w->hchild))
13732 redisplay_windows (w->hchild);
13733 else if (!NILP (w->vchild))
13734 redisplay_windows (w->vchild);
13735 else if (!NILP (w->buffer))
13736 {
13737 displayed_buffer = XBUFFER (w->buffer);
13738 /* Use list_of_error, not Qerror, so that
13739 we catch only errors and don't run the debugger. */
13740 internal_condition_case_1 (redisplay_window_0, window,
13741 list_of_error,
13742 redisplay_window_error);
13743 }
13744
13745 window = w->next;
13746 }
13747 }
13748
13749 static Lisp_Object
13750 redisplay_window_error (Lisp_Object ignore)
13751 {
13752 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13753 return Qnil;
13754 }
13755
13756 static Lisp_Object
13757 redisplay_window_0 (Lisp_Object window)
13758 {
13759 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13760 redisplay_window (window, 0);
13761 return Qnil;
13762 }
13763
13764 static Lisp_Object
13765 redisplay_window_1 (Lisp_Object window)
13766 {
13767 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13768 redisplay_window (window, 1);
13769 return Qnil;
13770 }
13771 \f
13772
13773 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13774 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13775 which positions recorded in ROW differ from current buffer
13776 positions.
13777
13778 Return 0 if cursor is not on this row, 1 otherwise. */
13779
13780 static int
13781 set_cursor_from_row (struct window *w, struct glyph_row *row,
13782 struct glyph_matrix *matrix,
13783 EMACS_INT delta, EMACS_INT delta_bytes,
13784 int dy, int dvpos)
13785 {
13786 struct glyph *glyph = row->glyphs[TEXT_AREA];
13787 struct glyph *end = glyph + row->used[TEXT_AREA];
13788 struct glyph *cursor = NULL;
13789 /* The last known character position in row. */
13790 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13791 int x = row->x;
13792 EMACS_INT pt_old = PT - delta;
13793 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13794 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13795 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13796 /* A glyph beyond the edge of TEXT_AREA which we should never
13797 touch. */
13798 struct glyph *glyphs_end = end;
13799 /* Non-zero means we've found a match for cursor position, but that
13800 glyph has the avoid_cursor_p flag set. */
13801 int match_with_avoid_cursor = 0;
13802 /* Non-zero means we've seen at least one glyph that came from a
13803 display string. */
13804 int string_seen = 0;
13805 /* Largest and smallest buffer positions seen so far during scan of
13806 glyph row. */
13807 EMACS_INT bpos_max = pos_before;
13808 EMACS_INT bpos_min = pos_after;
13809 /* Last buffer position covered by an overlay string with an integer
13810 `cursor' property. */
13811 EMACS_INT bpos_covered = 0;
13812 /* Non-zero means the display string on which to display the cursor
13813 comes from a text property, not from an overlay. */
13814 int string_from_text_prop = 0;
13815
13816 /* Don't even try doing anything if called for a mode-line or
13817 header-line row, since the rest of the code isn't prepared to
13818 deal with such calamities. */
13819 xassert (!row->mode_line_p);
13820 if (row->mode_line_p)
13821 return 0;
13822
13823 /* Skip over glyphs not having an object at the start and the end of
13824 the row. These are special glyphs like truncation marks on
13825 terminal frames. */
13826 if (row->displays_text_p)
13827 {
13828 if (!row->reversed_p)
13829 {
13830 while (glyph < end
13831 && INTEGERP (glyph->object)
13832 && glyph->charpos < 0)
13833 {
13834 x += glyph->pixel_width;
13835 ++glyph;
13836 }
13837 while (end > glyph
13838 && INTEGERP ((end - 1)->object)
13839 /* CHARPOS is zero for blanks and stretch glyphs
13840 inserted by extend_face_to_end_of_line. */
13841 && (end - 1)->charpos <= 0)
13842 --end;
13843 glyph_before = glyph - 1;
13844 glyph_after = end;
13845 }
13846 else
13847 {
13848 struct glyph *g;
13849
13850 /* If the glyph row is reversed, we need to process it from back
13851 to front, so swap the edge pointers. */
13852 glyphs_end = end = glyph - 1;
13853 glyph += row->used[TEXT_AREA] - 1;
13854
13855 while (glyph > end + 1
13856 && INTEGERP (glyph->object)
13857 && glyph->charpos < 0)
13858 {
13859 --glyph;
13860 x -= glyph->pixel_width;
13861 }
13862 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13863 --glyph;
13864 /* By default, in reversed rows we put the cursor on the
13865 rightmost (first in the reading order) glyph. */
13866 for (g = end + 1; g < glyph; g++)
13867 x += g->pixel_width;
13868 while (end < glyph
13869 && INTEGERP ((end + 1)->object)
13870 && (end + 1)->charpos <= 0)
13871 ++end;
13872 glyph_before = glyph + 1;
13873 glyph_after = end;
13874 }
13875 }
13876 else if (row->reversed_p)
13877 {
13878 /* In R2L rows that don't display text, put the cursor on the
13879 rightmost glyph. Case in point: an empty last line that is
13880 part of an R2L paragraph. */
13881 cursor = end - 1;
13882 /* Avoid placing the cursor on the last glyph of the row, where
13883 on terminal frames we hold the vertical border between
13884 adjacent windows. */
13885 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13886 && !WINDOW_RIGHTMOST_P (w)
13887 && cursor == row->glyphs[LAST_AREA] - 1)
13888 cursor--;
13889 x = -1; /* will be computed below, at label compute_x */
13890 }
13891
13892 /* Step 1: Try to find the glyph whose character position
13893 corresponds to point. If that's not possible, find 2 glyphs
13894 whose character positions are the closest to point, one before
13895 point, the other after it. */
13896 if (!row->reversed_p)
13897 while (/* not marched to end of glyph row */
13898 glyph < end
13899 /* glyph was not inserted by redisplay for internal purposes */
13900 && !INTEGERP (glyph->object))
13901 {
13902 if (BUFFERP (glyph->object))
13903 {
13904 EMACS_INT dpos = glyph->charpos - pt_old;
13905
13906 if (glyph->charpos > bpos_max)
13907 bpos_max = glyph->charpos;
13908 if (glyph->charpos < bpos_min)
13909 bpos_min = glyph->charpos;
13910 if (!glyph->avoid_cursor_p)
13911 {
13912 /* If we hit point, we've found the glyph on which to
13913 display the cursor. */
13914 if (dpos == 0)
13915 {
13916 match_with_avoid_cursor = 0;
13917 break;
13918 }
13919 /* See if we've found a better approximation to
13920 POS_BEFORE or to POS_AFTER. Note that we want the
13921 first (leftmost) glyph of all those that are the
13922 closest from below, and the last (rightmost) of all
13923 those from above. */
13924 if (0 > dpos && dpos > pos_before - pt_old)
13925 {
13926 pos_before = glyph->charpos;
13927 glyph_before = glyph;
13928 }
13929 else if (0 < dpos && dpos <= pos_after - pt_old)
13930 {
13931 pos_after = glyph->charpos;
13932 glyph_after = glyph;
13933 }
13934 }
13935 else if (dpos == 0)
13936 match_with_avoid_cursor = 1;
13937 }
13938 else if (STRINGP (glyph->object))
13939 {
13940 Lisp_Object chprop;
13941 EMACS_INT glyph_pos = glyph->charpos;
13942
13943 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13944 glyph->object);
13945 if (!NILP (chprop))
13946 {
13947 /* If the string came from a `display' text property,
13948 look up the buffer position of that property and
13949 use that position to update bpos_max, as if we
13950 actually saw such a position in one of the row's
13951 glyphs. This helps with supporting integer values
13952 of `cursor' property on the display string in
13953 situations where most or all of the row's buffer
13954 text is completely covered by display properties,
13955 so that no glyph with valid buffer positions is
13956 ever seen in the row. */
13957 EMACS_INT prop_pos =
13958 string_buffer_position_lim (glyph->object, pos_before,
13959 pos_after, 0);
13960
13961 if (prop_pos >= pos_before)
13962 bpos_max = prop_pos - 1;
13963 }
13964 if (INTEGERP (chprop))
13965 {
13966 bpos_covered = bpos_max + XINT (chprop);
13967 /* If the `cursor' property covers buffer positions up
13968 to and including point, we should display cursor on
13969 this glyph. Note that, if a `cursor' property on one
13970 of the string's characters has an integer value, we
13971 will break out of the loop below _before_ we get to
13972 the position match above. IOW, integer values of
13973 the `cursor' property override the "exact match for
13974 point" strategy of positioning the cursor. */
13975 /* Implementation note: bpos_max == pt_old when, e.g.,
13976 we are in an empty line, where bpos_max is set to
13977 MATRIX_ROW_START_CHARPOS, see above. */
13978 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13979 {
13980 cursor = glyph;
13981 break;
13982 }
13983 }
13984
13985 string_seen = 1;
13986 }
13987 x += glyph->pixel_width;
13988 ++glyph;
13989 }
13990 else if (glyph > end) /* row is reversed */
13991 while (!INTEGERP (glyph->object))
13992 {
13993 if (BUFFERP (glyph->object))
13994 {
13995 EMACS_INT dpos = glyph->charpos - pt_old;
13996
13997 if (glyph->charpos > bpos_max)
13998 bpos_max = glyph->charpos;
13999 if (glyph->charpos < bpos_min)
14000 bpos_min = glyph->charpos;
14001 if (!glyph->avoid_cursor_p)
14002 {
14003 if (dpos == 0)
14004 {
14005 match_with_avoid_cursor = 0;
14006 break;
14007 }
14008 if (0 > dpos && dpos > pos_before - pt_old)
14009 {
14010 pos_before = glyph->charpos;
14011 glyph_before = glyph;
14012 }
14013 else if (0 < dpos && dpos <= pos_after - pt_old)
14014 {
14015 pos_after = glyph->charpos;
14016 glyph_after = glyph;
14017 }
14018 }
14019 else if (dpos == 0)
14020 match_with_avoid_cursor = 1;
14021 }
14022 else if (STRINGP (glyph->object))
14023 {
14024 Lisp_Object chprop;
14025 EMACS_INT glyph_pos = glyph->charpos;
14026
14027 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14028 glyph->object);
14029 if (!NILP (chprop))
14030 {
14031 EMACS_INT prop_pos =
14032 string_buffer_position_lim (glyph->object, pos_before,
14033 pos_after, 0);
14034
14035 if (prop_pos >= pos_before)
14036 bpos_max = prop_pos - 1;
14037 }
14038 if (INTEGERP (chprop))
14039 {
14040 bpos_covered = bpos_max + XINT (chprop);
14041 /* If the `cursor' property covers buffer positions up
14042 to and including point, we should display cursor on
14043 this glyph. */
14044 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14045 {
14046 cursor = glyph;
14047 break;
14048 }
14049 }
14050 string_seen = 1;
14051 }
14052 --glyph;
14053 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14054 {
14055 x--; /* can't use any pixel_width */
14056 break;
14057 }
14058 x -= glyph->pixel_width;
14059 }
14060
14061 /* Step 2: If we didn't find an exact match for point, we need to
14062 look for a proper place to put the cursor among glyphs between
14063 GLYPH_BEFORE and GLYPH_AFTER. */
14064 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14065 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14066 && bpos_covered < pt_old)
14067 {
14068 /* An empty line has a single glyph whose OBJECT is zero and
14069 whose CHARPOS is the position of a newline on that line.
14070 Note that on a TTY, there are more glyphs after that, which
14071 were produced by extend_face_to_end_of_line, but their
14072 CHARPOS is zero or negative. */
14073 int empty_line_p =
14074 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14075 && INTEGERP (glyph->object) && glyph->charpos > 0;
14076
14077 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14078 {
14079 EMACS_INT ellipsis_pos;
14080
14081 /* Scan back over the ellipsis glyphs. */
14082 if (!row->reversed_p)
14083 {
14084 ellipsis_pos = (glyph - 1)->charpos;
14085 while (glyph > row->glyphs[TEXT_AREA]
14086 && (glyph - 1)->charpos == ellipsis_pos)
14087 glyph--, x -= glyph->pixel_width;
14088 /* That loop always goes one position too far, including
14089 the glyph before the ellipsis. So scan forward over
14090 that one. */
14091 x += glyph->pixel_width;
14092 glyph++;
14093 }
14094 else /* row is reversed */
14095 {
14096 ellipsis_pos = (glyph + 1)->charpos;
14097 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14098 && (glyph + 1)->charpos == ellipsis_pos)
14099 glyph++, x += glyph->pixel_width;
14100 x -= glyph->pixel_width;
14101 glyph--;
14102 }
14103 }
14104 else if (match_with_avoid_cursor)
14105 {
14106 cursor = glyph_after;
14107 x = -1;
14108 }
14109 else if (string_seen)
14110 {
14111 int incr = row->reversed_p ? -1 : +1;
14112
14113 /* Need to find the glyph that came out of a string which is
14114 present at point. That glyph is somewhere between
14115 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14116 positioned between POS_BEFORE and POS_AFTER in the
14117 buffer. */
14118 struct glyph *start, *stop;
14119 EMACS_INT pos = pos_before;
14120
14121 x = -1;
14122
14123 /* If the row ends in a newline from a display string,
14124 reordering could have moved the glyphs belonging to the
14125 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14126 in this case we extend the search to the last glyph in
14127 the row that was not inserted by redisplay. */
14128 if (row->ends_in_newline_from_string_p)
14129 {
14130 glyph_after = end;
14131 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14132 }
14133
14134 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14135 correspond to POS_BEFORE and POS_AFTER, respectively. We
14136 need START and STOP in the order that corresponds to the
14137 row's direction as given by its reversed_p flag. If the
14138 directionality of characters between POS_BEFORE and
14139 POS_AFTER is the opposite of the row's base direction,
14140 these characters will have been reordered for display,
14141 and we need to reverse START and STOP. */
14142 if (!row->reversed_p)
14143 {
14144 start = min (glyph_before, glyph_after);
14145 stop = max (glyph_before, glyph_after);
14146 }
14147 else
14148 {
14149 start = max (glyph_before, glyph_after);
14150 stop = min (glyph_before, glyph_after);
14151 }
14152 for (glyph = start + incr;
14153 row->reversed_p ? glyph > stop : glyph < stop; )
14154 {
14155
14156 /* Any glyphs that come from the buffer are here because
14157 of bidi reordering. Skip them, and only pay
14158 attention to glyphs that came from some string. */
14159 if (STRINGP (glyph->object))
14160 {
14161 Lisp_Object str;
14162 EMACS_INT tem;
14163 /* If the display property covers the newline, we
14164 need to search for it one position farther. */
14165 EMACS_INT lim = pos_after
14166 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14167
14168 string_from_text_prop = 0;
14169 str = glyph->object;
14170 tem = string_buffer_position_lim (str, pos, lim, 0);
14171 if (tem == 0 /* from overlay */
14172 || pos <= tem)
14173 {
14174 /* If the string from which this glyph came is
14175 found in the buffer at point, or at position
14176 that is closer to point than pos_after, then
14177 we've found the glyph we've been looking for.
14178 If it comes from an overlay (tem == 0), and
14179 it has the `cursor' property on one of its
14180 glyphs, record that glyph as a candidate for
14181 displaying the cursor. (As in the
14182 unidirectional version, we will display the
14183 cursor on the last candidate we find.) */
14184 if (tem == 0
14185 || tem == pt_old
14186 || (tem - pt_old > 0 && tem < pos_after))
14187 {
14188 /* The glyphs from this string could have
14189 been reordered. Find the one with the
14190 smallest string position. Or there could
14191 be a character in the string with the
14192 `cursor' property, which means display
14193 cursor on that character's glyph. */
14194 EMACS_INT strpos = glyph->charpos;
14195
14196 if (tem)
14197 {
14198 cursor = glyph;
14199 string_from_text_prop = 1;
14200 }
14201 for ( ;
14202 (row->reversed_p ? glyph > stop : glyph < stop)
14203 && EQ (glyph->object, str);
14204 glyph += incr)
14205 {
14206 Lisp_Object cprop;
14207 EMACS_INT gpos = glyph->charpos;
14208
14209 cprop = Fget_char_property (make_number (gpos),
14210 Qcursor,
14211 glyph->object);
14212 if (!NILP (cprop))
14213 {
14214 cursor = glyph;
14215 break;
14216 }
14217 if (tem && glyph->charpos < strpos)
14218 {
14219 strpos = glyph->charpos;
14220 cursor = glyph;
14221 }
14222 }
14223
14224 if (tem == pt_old
14225 || (tem - pt_old > 0 && tem < pos_after))
14226 goto compute_x;
14227 }
14228 if (tem)
14229 pos = tem + 1; /* don't find previous instances */
14230 }
14231 /* This string is not what we want; skip all of the
14232 glyphs that came from it. */
14233 while ((row->reversed_p ? glyph > stop : glyph < stop)
14234 && EQ (glyph->object, str))
14235 glyph += incr;
14236 }
14237 else
14238 glyph += incr;
14239 }
14240
14241 /* If we reached the end of the line, and END was from a string,
14242 the cursor is not on this line. */
14243 if (cursor == NULL
14244 && (row->reversed_p ? glyph <= end : glyph >= end)
14245 && STRINGP (end->object)
14246 && row->continued_p)
14247 return 0;
14248 }
14249 /* A truncated row may not include PT among its character positions.
14250 Setting the cursor inside the scroll margin will trigger
14251 recalculation of hscroll in hscroll_window_tree. But if a
14252 display string covers point, defer to the string-handling
14253 code below to figure this out. */
14254 else if (row->truncated_on_left_p && pt_old < bpos_min)
14255 {
14256 cursor = glyph_before;
14257 x = -1;
14258 }
14259 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14260 /* Zero-width characters produce no glyphs. */
14261 || (!empty_line_p
14262 && (row->reversed_p
14263 ? glyph_after > glyphs_end
14264 : glyph_after < glyphs_end)))
14265 {
14266 cursor = glyph_after;
14267 x = -1;
14268 }
14269 }
14270
14271 compute_x:
14272 if (cursor != NULL)
14273 glyph = cursor;
14274 if (x < 0)
14275 {
14276 struct glyph *g;
14277
14278 /* Need to compute x that corresponds to GLYPH. */
14279 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14280 {
14281 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14282 abort ();
14283 x += g->pixel_width;
14284 }
14285 }
14286
14287 /* ROW could be part of a continued line, which, under bidi
14288 reordering, might have other rows whose start and end charpos
14289 occlude point. Only set w->cursor if we found a better
14290 approximation to the cursor position than we have from previously
14291 examined candidate rows belonging to the same continued line. */
14292 if (/* we already have a candidate row */
14293 w->cursor.vpos >= 0
14294 /* that candidate is not the row we are processing */
14295 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14296 /* Make sure cursor.vpos specifies a row whose start and end
14297 charpos occlude point, and it is valid candidate for being a
14298 cursor-row. This is because some callers of this function
14299 leave cursor.vpos at the row where the cursor was displayed
14300 during the last redisplay cycle. */
14301 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14302 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14303 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14304 {
14305 struct glyph *g1 =
14306 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14307
14308 /* Don't consider glyphs that are outside TEXT_AREA. */
14309 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14310 return 0;
14311 /* Keep the candidate whose buffer position is the closest to
14312 point or has the `cursor' property. */
14313 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14314 w->cursor.hpos >= 0
14315 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14316 && ((BUFFERP (g1->object)
14317 && (g1->charpos == pt_old /* an exact match always wins */
14318 || (BUFFERP (glyph->object)
14319 && eabs (g1->charpos - pt_old)
14320 < eabs (glyph->charpos - pt_old))))
14321 /* previous candidate is a glyph from a string that has
14322 a non-nil `cursor' property */
14323 || (STRINGP (g1->object)
14324 && (!NILP (Fget_char_property (make_number (g1->charpos),
14325 Qcursor, g1->object))
14326 /* previous candidate is from the same display
14327 string as this one, and the display string
14328 came from a text property */
14329 || (EQ (g1->object, glyph->object)
14330 && string_from_text_prop)
14331 /* this candidate is from newline and its
14332 position is not an exact match */
14333 || (INTEGERP (glyph->object)
14334 && glyph->charpos != pt_old)))))
14335 return 0;
14336 /* If this candidate gives an exact match, use that. */
14337 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14338 /* If this candidate is a glyph created for the
14339 terminating newline of a line, and point is on that
14340 newline, it wins because it's an exact match. */
14341 || (!row->continued_p
14342 && INTEGERP (glyph->object)
14343 && glyph->charpos == 0
14344 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14345 /* Otherwise, keep the candidate that comes from a row
14346 spanning less buffer positions. This may win when one or
14347 both candidate positions are on glyphs that came from
14348 display strings, for which we cannot compare buffer
14349 positions. */
14350 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14351 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14352 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14353 return 0;
14354 }
14355 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14356 w->cursor.x = x;
14357 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14358 w->cursor.y = row->y + dy;
14359
14360 if (w == XWINDOW (selected_window))
14361 {
14362 if (!row->continued_p
14363 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14364 && row->x == 0)
14365 {
14366 this_line_buffer = XBUFFER (w->buffer);
14367
14368 CHARPOS (this_line_start_pos)
14369 = MATRIX_ROW_START_CHARPOS (row) + delta;
14370 BYTEPOS (this_line_start_pos)
14371 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14372
14373 CHARPOS (this_line_end_pos)
14374 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14375 BYTEPOS (this_line_end_pos)
14376 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14377
14378 this_line_y = w->cursor.y;
14379 this_line_pixel_height = row->height;
14380 this_line_vpos = w->cursor.vpos;
14381 this_line_start_x = row->x;
14382 }
14383 else
14384 CHARPOS (this_line_start_pos) = 0;
14385 }
14386
14387 return 1;
14388 }
14389
14390
14391 /* Run window scroll functions, if any, for WINDOW with new window
14392 start STARTP. Sets the window start of WINDOW to that position.
14393
14394 We assume that the window's buffer is really current. */
14395
14396 static inline struct text_pos
14397 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14398 {
14399 struct window *w = XWINDOW (window);
14400 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14401
14402 if (current_buffer != XBUFFER (w->buffer))
14403 abort ();
14404
14405 if (!NILP (Vwindow_scroll_functions))
14406 {
14407 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14408 make_number (CHARPOS (startp)));
14409 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14410 /* In case the hook functions switch buffers. */
14411 if (current_buffer != XBUFFER (w->buffer))
14412 set_buffer_internal_1 (XBUFFER (w->buffer));
14413 }
14414
14415 return startp;
14416 }
14417
14418
14419 /* Make sure the line containing the cursor is fully visible.
14420 A value of 1 means there is nothing to be done.
14421 (Either the line is fully visible, or it cannot be made so,
14422 or we cannot tell.)
14423
14424 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14425 is higher than window.
14426
14427 A value of 0 means the caller should do scrolling
14428 as if point had gone off the screen. */
14429
14430 static int
14431 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14432 {
14433 struct glyph_matrix *matrix;
14434 struct glyph_row *row;
14435 int window_height;
14436
14437 if (!make_cursor_line_fully_visible_p)
14438 return 1;
14439
14440 /* It's not always possible to find the cursor, e.g, when a window
14441 is full of overlay strings. Don't do anything in that case. */
14442 if (w->cursor.vpos < 0)
14443 return 1;
14444
14445 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14446 row = MATRIX_ROW (matrix, w->cursor.vpos);
14447
14448 /* If the cursor row is not partially visible, there's nothing to do. */
14449 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14450 return 1;
14451
14452 /* If the row the cursor is in is taller than the window's height,
14453 it's not clear what to do, so do nothing. */
14454 window_height = window_box_height (w);
14455 if (row->height >= window_height)
14456 {
14457 if (!force_p || MINI_WINDOW_P (w)
14458 || w->vscroll || w->cursor.vpos == 0)
14459 return 1;
14460 }
14461 return 0;
14462 }
14463
14464
14465 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14466 non-zero means only WINDOW is redisplayed in redisplay_internal.
14467 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14468 in redisplay_window to bring a partially visible line into view in
14469 the case that only the cursor has moved.
14470
14471 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14472 last screen line's vertical height extends past the end of the screen.
14473
14474 Value is
14475
14476 1 if scrolling succeeded
14477
14478 0 if scrolling didn't find point.
14479
14480 -1 if new fonts have been loaded so that we must interrupt
14481 redisplay, adjust glyph matrices, and try again. */
14482
14483 enum
14484 {
14485 SCROLLING_SUCCESS,
14486 SCROLLING_FAILED,
14487 SCROLLING_NEED_LARGER_MATRICES
14488 };
14489
14490 /* If scroll-conservatively is more than this, never recenter.
14491
14492 If you change this, don't forget to update the doc string of
14493 `scroll-conservatively' and the Emacs manual. */
14494 #define SCROLL_LIMIT 100
14495
14496 static int
14497 try_scrolling (Lisp_Object window, int just_this_one_p,
14498 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14499 int temp_scroll_step, int last_line_misfit)
14500 {
14501 struct window *w = XWINDOW (window);
14502 struct frame *f = XFRAME (w->frame);
14503 struct text_pos pos, startp;
14504 struct it it;
14505 int this_scroll_margin, scroll_max, rc, height;
14506 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14507 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14508 Lisp_Object aggressive;
14509 /* We will never try scrolling more than this number of lines. */
14510 int scroll_limit = SCROLL_LIMIT;
14511
14512 #if GLYPH_DEBUG
14513 debug_method_add (w, "try_scrolling");
14514 #endif
14515
14516 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14517
14518 /* Compute scroll margin height in pixels. We scroll when point is
14519 within this distance from the top or bottom of the window. */
14520 if (scroll_margin > 0)
14521 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14522 * FRAME_LINE_HEIGHT (f);
14523 else
14524 this_scroll_margin = 0;
14525
14526 /* Force arg_scroll_conservatively to have a reasonable value, to
14527 avoid scrolling too far away with slow move_it_* functions. Note
14528 that the user can supply scroll-conservatively equal to
14529 `most-positive-fixnum', which can be larger than INT_MAX. */
14530 if (arg_scroll_conservatively > scroll_limit)
14531 {
14532 arg_scroll_conservatively = scroll_limit + 1;
14533 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14534 }
14535 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14536 /* Compute how much we should try to scroll maximally to bring
14537 point into view. */
14538 scroll_max = (max (scroll_step,
14539 max (arg_scroll_conservatively, temp_scroll_step))
14540 * FRAME_LINE_HEIGHT (f));
14541 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14542 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14543 /* We're trying to scroll because of aggressive scrolling but no
14544 scroll_step is set. Choose an arbitrary one. */
14545 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14546 else
14547 scroll_max = 0;
14548
14549 too_near_end:
14550
14551 /* Decide whether to scroll down. */
14552 if (PT > CHARPOS (startp))
14553 {
14554 int scroll_margin_y;
14555
14556 /* Compute the pixel ypos of the scroll margin, then move IT to
14557 either that ypos or PT, whichever comes first. */
14558 start_display (&it, w, startp);
14559 scroll_margin_y = it.last_visible_y - this_scroll_margin
14560 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14561 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14562 (MOVE_TO_POS | MOVE_TO_Y));
14563
14564 if (PT > CHARPOS (it.current.pos))
14565 {
14566 int y0 = line_bottom_y (&it);
14567 /* Compute how many pixels below window bottom to stop searching
14568 for PT. This avoids costly search for PT that is far away if
14569 the user limited scrolling by a small number of lines, but
14570 always finds PT if scroll_conservatively is set to a large
14571 number, such as most-positive-fixnum. */
14572 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14573 int y_to_move = it.last_visible_y + slack;
14574
14575 /* Compute the distance from the scroll margin to PT or to
14576 the scroll limit, whichever comes first. This should
14577 include the height of the cursor line, to make that line
14578 fully visible. */
14579 move_it_to (&it, PT, -1, y_to_move,
14580 -1, MOVE_TO_POS | MOVE_TO_Y);
14581 dy = line_bottom_y (&it) - y0;
14582
14583 if (dy > scroll_max)
14584 return SCROLLING_FAILED;
14585
14586 if (dy > 0)
14587 scroll_down_p = 1;
14588 }
14589 }
14590
14591 if (scroll_down_p)
14592 {
14593 /* Point is in or below the bottom scroll margin, so move the
14594 window start down. If scrolling conservatively, move it just
14595 enough down to make point visible. If scroll_step is set,
14596 move it down by scroll_step. */
14597 if (arg_scroll_conservatively)
14598 amount_to_scroll
14599 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14600 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14601 else if (scroll_step || temp_scroll_step)
14602 amount_to_scroll = scroll_max;
14603 else
14604 {
14605 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14606 height = WINDOW_BOX_TEXT_HEIGHT (w);
14607 if (NUMBERP (aggressive))
14608 {
14609 double float_amount = XFLOATINT (aggressive) * height;
14610 amount_to_scroll = float_amount;
14611 if (amount_to_scroll == 0 && float_amount > 0)
14612 amount_to_scroll = 1;
14613 /* Don't let point enter the scroll margin near top of
14614 the window. */
14615 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14616 amount_to_scroll = height - 2*this_scroll_margin + dy;
14617 }
14618 }
14619
14620 if (amount_to_scroll <= 0)
14621 return SCROLLING_FAILED;
14622
14623 start_display (&it, w, startp);
14624 if (arg_scroll_conservatively <= scroll_limit)
14625 move_it_vertically (&it, amount_to_scroll);
14626 else
14627 {
14628 /* Extra precision for users who set scroll-conservatively
14629 to a large number: make sure the amount we scroll
14630 the window start is never less than amount_to_scroll,
14631 which was computed as distance from window bottom to
14632 point. This matters when lines at window top and lines
14633 below window bottom have different height. */
14634 struct it it1;
14635 void *it1data = NULL;
14636 /* We use a temporary it1 because line_bottom_y can modify
14637 its argument, if it moves one line down; see there. */
14638 int start_y;
14639
14640 SAVE_IT (it1, it, it1data);
14641 start_y = line_bottom_y (&it1);
14642 do {
14643 RESTORE_IT (&it, &it, it1data);
14644 move_it_by_lines (&it, 1);
14645 SAVE_IT (it1, it, it1data);
14646 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14647 }
14648
14649 /* If STARTP is unchanged, move it down another screen line. */
14650 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14651 move_it_by_lines (&it, 1);
14652 startp = it.current.pos;
14653 }
14654 else
14655 {
14656 struct text_pos scroll_margin_pos = startp;
14657
14658 /* See if point is inside the scroll margin at the top of the
14659 window. */
14660 if (this_scroll_margin)
14661 {
14662 start_display (&it, w, startp);
14663 move_it_vertically (&it, this_scroll_margin);
14664 scroll_margin_pos = it.current.pos;
14665 }
14666
14667 if (PT < CHARPOS (scroll_margin_pos))
14668 {
14669 /* Point is in the scroll margin at the top of the window or
14670 above what is displayed in the window. */
14671 int y0, y_to_move;
14672
14673 /* Compute the vertical distance from PT to the scroll
14674 margin position. Move as far as scroll_max allows, or
14675 one screenful, or 10 screen lines, whichever is largest.
14676 Give up if distance is greater than scroll_max. */
14677 SET_TEXT_POS (pos, PT, PT_BYTE);
14678 start_display (&it, w, pos);
14679 y0 = it.current_y;
14680 y_to_move = max (it.last_visible_y,
14681 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14682 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14683 y_to_move, -1,
14684 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14685 dy = it.current_y - y0;
14686 if (dy > scroll_max)
14687 return SCROLLING_FAILED;
14688
14689 /* Compute new window start. */
14690 start_display (&it, w, startp);
14691
14692 if (arg_scroll_conservatively)
14693 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14694 max (scroll_step, temp_scroll_step));
14695 else if (scroll_step || temp_scroll_step)
14696 amount_to_scroll = scroll_max;
14697 else
14698 {
14699 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14700 height = WINDOW_BOX_TEXT_HEIGHT (w);
14701 if (NUMBERP (aggressive))
14702 {
14703 double float_amount = XFLOATINT (aggressive) * height;
14704 amount_to_scroll = float_amount;
14705 if (amount_to_scroll == 0 && float_amount > 0)
14706 amount_to_scroll = 1;
14707 amount_to_scroll -=
14708 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14709 /* Don't let point enter the scroll margin near
14710 bottom of the window. */
14711 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14712 amount_to_scroll = height - 2*this_scroll_margin + dy;
14713 }
14714 }
14715
14716 if (amount_to_scroll <= 0)
14717 return SCROLLING_FAILED;
14718
14719 move_it_vertically_backward (&it, amount_to_scroll);
14720 startp = it.current.pos;
14721 }
14722 }
14723
14724 /* Run window scroll functions. */
14725 startp = run_window_scroll_functions (window, startp);
14726
14727 /* Display the window. Give up if new fonts are loaded, or if point
14728 doesn't appear. */
14729 if (!try_window (window, startp, 0))
14730 rc = SCROLLING_NEED_LARGER_MATRICES;
14731 else if (w->cursor.vpos < 0)
14732 {
14733 clear_glyph_matrix (w->desired_matrix);
14734 rc = SCROLLING_FAILED;
14735 }
14736 else
14737 {
14738 /* Maybe forget recorded base line for line number display. */
14739 if (!just_this_one_p
14740 || current_buffer->clip_changed
14741 || BEG_UNCHANGED < CHARPOS (startp))
14742 w->base_line_number = Qnil;
14743
14744 /* If cursor ends up on a partially visible line,
14745 treat that as being off the bottom of the screen. */
14746 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14747 /* It's possible that the cursor is on the first line of the
14748 buffer, which is partially obscured due to a vscroll
14749 (Bug#7537). In that case, avoid looping forever . */
14750 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14751 {
14752 clear_glyph_matrix (w->desired_matrix);
14753 ++extra_scroll_margin_lines;
14754 goto too_near_end;
14755 }
14756 rc = SCROLLING_SUCCESS;
14757 }
14758
14759 return rc;
14760 }
14761
14762
14763 /* Compute a suitable window start for window W if display of W starts
14764 on a continuation line. Value is non-zero if a new window start
14765 was computed.
14766
14767 The new window start will be computed, based on W's width, starting
14768 from the start of the continued line. It is the start of the
14769 screen line with the minimum distance from the old start W->start. */
14770
14771 static int
14772 compute_window_start_on_continuation_line (struct window *w)
14773 {
14774 struct text_pos pos, start_pos;
14775 int window_start_changed_p = 0;
14776
14777 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14778
14779 /* If window start is on a continuation line... Window start may be
14780 < BEGV in case there's invisible text at the start of the
14781 buffer (M-x rmail, for example). */
14782 if (CHARPOS (start_pos) > BEGV
14783 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14784 {
14785 struct it it;
14786 struct glyph_row *row;
14787
14788 /* Handle the case that the window start is out of range. */
14789 if (CHARPOS (start_pos) < BEGV)
14790 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14791 else if (CHARPOS (start_pos) > ZV)
14792 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14793
14794 /* Find the start of the continued line. This should be fast
14795 because scan_buffer is fast (newline cache). */
14796 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14797 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14798 row, DEFAULT_FACE_ID);
14799 reseat_at_previous_visible_line_start (&it);
14800
14801 /* If the line start is "too far" away from the window start,
14802 say it takes too much time to compute a new window start. */
14803 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14804 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14805 {
14806 int min_distance, distance;
14807
14808 /* Move forward by display lines to find the new window
14809 start. If window width was enlarged, the new start can
14810 be expected to be > the old start. If window width was
14811 decreased, the new window start will be < the old start.
14812 So, we're looking for the display line start with the
14813 minimum distance from the old window start. */
14814 pos = it.current.pos;
14815 min_distance = INFINITY;
14816 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14817 distance < min_distance)
14818 {
14819 min_distance = distance;
14820 pos = it.current.pos;
14821 move_it_by_lines (&it, 1);
14822 }
14823
14824 /* Set the window start there. */
14825 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14826 window_start_changed_p = 1;
14827 }
14828 }
14829
14830 return window_start_changed_p;
14831 }
14832
14833
14834 /* Try cursor movement in case text has not changed in window WINDOW,
14835 with window start STARTP. Value is
14836
14837 CURSOR_MOVEMENT_SUCCESS if successful
14838
14839 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14840
14841 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14842 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14843 we want to scroll as if scroll-step were set to 1. See the code.
14844
14845 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14846 which case we have to abort this redisplay, and adjust matrices
14847 first. */
14848
14849 enum
14850 {
14851 CURSOR_MOVEMENT_SUCCESS,
14852 CURSOR_MOVEMENT_CANNOT_BE_USED,
14853 CURSOR_MOVEMENT_MUST_SCROLL,
14854 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14855 };
14856
14857 static int
14858 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14859 {
14860 struct window *w = XWINDOW (window);
14861 struct frame *f = XFRAME (w->frame);
14862 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14863
14864 #if GLYPH_DEBUG
14865 if (inhibit_try_cursor_movement)
14866 return rc;
14867 #endif
14868
14869 /* Handle case where text has not changed, only point, and it has
14870 not moved off the frame. */
14871 if (/* Point may be in this window. */
14872 PT >= CHARPOS (startp)
14873 /* Selective display hasn't changed. */
14874 && !current_buffer->clip_changed
14875 /* Function force-mode-line-update is used to force a thorough
14876 redisplay. It sets either windows_or_buffers_changed or
14877 update_mode_lines. So don't take a shortcut here for these
14878 cases. */
14879 && !update_mode_lines
14880 && !windows_or_buffers_changed
14881 && !cursor_type_changed
14882 /* Can't use this case if highlighting a region. When a
14883 region exists, cursor movement has to do more than just
14884 set the cursor. */
14885 && !(!NILP (Vtransient_mark_mode)
14886 && !NILP (BVAR (current_buffer, mark_active)))
14887 && NILP (w->region_showing)
14888 && NILP (Vshow_trailing_whitespace)
14889 /* Right after splitting windows, last_point may be nil. */
14890 && INTEGERP (w->last_point)
14891 /* This code is not used for mini-buffer for the sake of the case
14892 of redisplaying to replace an echo area message; since in
14893 that case the mini-buffer contents per se are usually
14894 unchanged. This code is of no real use in the mini-buffer
14895 since the handling of this_line_start_pos, etc., in redisplay
14896 handles the same cases. */
14897 && !EQ (window, minibuf_window)
14898 /* When splitting windows or for new windows, it happens that
14899 redisplay is called with a nil window_end_vpos or one being
14900 larger than the window. This should really be fixed in
14901 window.c. I don't have this on my list, now, so we do
14902 approximately the same as the old redisplay code. --gerd. */
14903 && INTEGERP (w->window_end_vpos)
14904 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14905 && (FRAME_WINDOW_P (f)
14906 || !overlay_arrow_in_current_buffer_p ()))
14907 {
14908 int this_scroll_margin, top_scroll_margin;
14909 struct glyph_row *row = NULL;
14910
14911 #if GLYPH_DEBUG
14912 debug_method_add (w, "cursor movement");
14913 #endif
14914
14915 /* Scroll if point within this distance from the top or bottom
14916 of the window. This is a pixel value. */
14917 if (scroll_margin > 0)
14918 {
14919 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14920 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14921 }
14922 else
14923 this_scroll_margin = 0;
14924
14925 top_scroll_margin = this_scroll_margin;
14926 if (WINDOW_WANTS_HEADER_LINE_P (w))
14927 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14928
14929 /* Start with the row the cursor was displayed during the last
14930 not paused redisplay. Give up if that row is not valid. */
14931 if (w->last_cursor.vpos < 0
14932 || w->last_cursor.vpos >= w->current_matrix->nrows)
14933 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14934 else
14935 {
14936 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14937 if (row->mode_line_p)
14938 ++row;
14939 if (!row->enabled_p)
14940 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14941 }
14942
14943 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14944 {
14945 int scroll_p = 0, must_scroll = 0;
14946 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14947
14948 if (PT > XFASTINT (w->last_point))
14949 {
14950 /* Point has moved forward. */
14951 while (MATRIX_ROW_END_CHARPOS (row) < PT
14952 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14953 {
14954 xassert (row->enabled_p);
14955 ++row;
14956 }
14957
14958 /* If the end position of a row equals the start
14959 position of the next row, and PT is at that position,
14960 we would rather display cursor in the next line. */
14961 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14962 && MATRIX_ROW_END_CHARPOS (row) == PT
14963 && row < w->current_matrix->rows
14964 + w->current_matrix->nrows - 1
14965 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14966 && !cursor_row_p (row))
14967 ++row;
14968
14969 /* If within the scroll margin, scroll. Note that
14970 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14971 the next line would be drawn, and that
14972 this_scroll_margin can be zero. */
14973 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14974 || PT > MATRIX_ROW_END_CHARPOS (row)
14975 /* Line is completely visible last line in window
14976 and PT is to be set in the next line. */
14977 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14978 && PT == MATRIX_ROW_END_CHARPOS (row)
14979 && !row->ends_at_zv_p
14980 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14981 scroll_p = 1;
14982 }
14983 else if (PT < XFASTINT (w->last_point))
14984 {
14985 /* Cursor has to be moved backward. Note that PT >=
14986 CHARPOS (startp) because of the outer if-statement. */
14987 while (!row->mode_line_p
14988 && (MATRIX_ROW_START_CHARPOS (row) > PT
14989 || (MATRIX_ROW_START_CHARPOS (row) == PT
14990 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14991 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14992 row > w->current_matrix->rows
14993 && (row-1)->ends_in_newline_from_string_p))))
14994 && (row->y > top_scroll_margin
14995 || CHARPOS (startp) == BEGV))
14996 {
14997 xassert (row->enabled_p);
14998 --row;
14999 }
15000
15001 /* Consider the following case: Window starts at BEGV,
15002 there is invisible, intangible text at BEGV, so that
15003 display starts at some point START > BEGV. It can
15004 happen that we are called with PT somewhere between
15005 BEGV and START. Try to handle that case. */
15006 if (row < w->current_matrix->rows
15007 || row->mode_line_p)
15008 {
15009 row = w->current_matrix->rows;
15010 if (row->mode_line_p)
15011 ++row;
15012 }
15013
15014 /* Due to newlines in overlay strings, we may have to
15015 skip forward over overlay strings. */
15016 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15017 && MATRIX_ROW_END_CHARPOS (row) == PT
15018 && !cursor_row_p (row))
15019 ++row;
15020
15021 /* If within the scroll margin, scroll. */
15022 if (row->y < top_scroll_margin
15023 && CHARPOS (startp) != BEGV)
15024 scroll_p = 1;
15025 }
15026 else
15027 {
15028 /* Cursor did not move. So don't scroll even if cursor line
15029 is partially visible, as it was so before. */
15030 rc = CURSOR_MOVEMENT_SUCCESS;
15031 }
15032
15033 if (PT < MATRIX_ROW_START_CHARPOS (row)
15034 || PT > MATRIX_ROW_END_CHARPOS (row))
15035 {
15036 /* if PT is not in the glyph row, give up. */
15037 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15038 must_scroll = 1;
15039 }
15040 else if (rc != CURSOR_MOVEMENT_SUCCESS
15041 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15042 {
15043 struct glyph_row *row1;
15044
15045 /* If rows are bidi-reordered and point moved, back up
15046 until we find a row that does not belong to a
15047 continuation line. This is because we must consider
15048 all rows of a continued line as candidates for the
15049 new cursor positioning, since row start and end
15050 positions change non-linearly with vertical position
15051 in such rows. */
15052 /* FIXME: Revisit this when glyph ``spilling'' in
15053 continuation lines' rows is implemented for
15054 bidi-reordered rows. */
15055 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15056 MATRIX_ROW_CONTINUATION_LINE_P (row);
15057 --row)
15058 {
15059 /* If we hit the beginning of the displayed portion
15060 without finding the first row of a continued
15061 line, give up. */
15062 if (row <= row1)
15063 {
15064 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15065 break;
15066 }
15067 xassert (row->enabled_p);
15068 }
15069 }
15070 if (must_scroll)
15071 ;
15072 else if (rc != CURSOR_MOVEMENT_SUCCESS
15073 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15074 /* Make sure this isn't a header line by any chance, since
15075 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15076 && !row->mode_line_p
15077 && make_cursor_line_fully_visible_p)
15078 {
15079 if (PT == MATRIX_ROW_END_CHARPOS (row)
15080 && !row->ends_at_zv_p
15081 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15082 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15083 else if (row->height > window_box_height (w))
15084 {
15085 /* If we end up in a partially visible line, let's
15086 make it fully visible, except when it's taller
15087 than the window, in which case we can't do much
15088 about it. */
15089 *scroll_step = 1;
15090 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15091 }
15092 else
15093 {
15094 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15095 if (!cursor_row_fully_visible_p (w, 0, 1))
15096 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15097 else
15098 rc = CURSOR_MOVEMENT_SUCCESS;
15099 }
15100 }
15101 else if (scroll_p)
15102 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15103 else if (rc != CURSOR_MOVEMENT_SUCCESS
15104 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15105 {
15106 /* With bidi-reordered rows, there could be more than
15107 one candidate row whose start and end positions
15108 occlude point. We need to let set_cursor_from_row
15109 find the best candidate. */
15110 /* FIXME: Revisit this when glyph ``spilling'' in
15111 continuation lines' rows is implemented for
15112 bidi-reordered rows. */
15113 int rv = 0;
15114
15115 do
15116 {
15117 int at_zv_p = 0, exact_match_p = 0;
15118
15119 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15120 && PT <= MATRIX_ROW_END_CHARPOS (row)
15121 && cursor_row_p (row))
15122 rv |= set_cursor_from_row (w, row, w->current_matrix,
15123 0, 0, 0, 0);
15124 /* As soon as we've found the exact match for point,
15125 or the first suitable row whose ends_at_zv_p flag
15126 is set, we are done. */
15127 at_zv_p =
15128 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15129 if (rv && !at_zv_p
15130 && w->cursor.hpos >= 0
15131 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15132 w->cursor.vpos))
15133 {
15134 struct glyph_row *candidate =
15135 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15136 struct glyph *g =
15137 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15138 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15139
15140 exact_match_p =
15141 (BUFFERP (g->object) && g->charpos == PT)
15142 || (INTEGERP (g->object)
15143 && (g->charpos == PT
15144 || (g->charpos == 0 && endpos - 1 == PT)));
15145 }
15146 if (rv && (at_zv_p || exact_match_p))
15147 {
15148 rc = CURSOR_MOVEMENT_SUCCESS;
15149 break;
15150 }
15151 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15152 break;
15153 ++row;
15154 }
15155 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15156 || row->continued_p)
15157 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15158 || (MATRIX_ROW_START_CHARPOS (row) == PT
15159 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15160 /* If we didn't find any candidate rows, or exited the
15161 loop before all the candidates were examined, signal
15162 to the caller that this method failed. */
15163 if (rc != CURSOR_MOVEMENT_SUCCESS
15164 && !(rv
15165 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15166 && !row->continued_p))
15167 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15168 else if (rv)
15169 rc = CURSOR_MOVEMENT_SUCCESS;
15170 }
15171 else
15172 {
15173 do
15174 {
15175 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15176 {
15177 rc = CURSOR_MOVEMENT_SUCCESS;
15178 break;
15179 }
15180 ++row;
15181 }
15182 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15183 && MATRIX_ROW_START_CHARPOS (row) == PT
15184 && cursor_row_p (row));
15185 }
15186 }
15187 }
15188
15189 return rc;
15190 }
15191
15192 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15193 static
15194 #endif
15195 void
15196 set_vertical_scroll_bar (struct window *w)
15197 {
15198 EMACS_INT start, end, whole;
15199
15200 /* Calculate the start and end positions for the current window.
15201 At some point, it would be nice to choose between scrollbars
15202 which reflect the whole buffer size, with special markers
15203 indicating narrowing, and scrollbars which reflect only the
15204 visible region.
15205
15206 Note that mini-buffers sometimes aren't displaying any text. */
15207 if (!MINI_WINDOW_P (w)
15208 || (w == XWINDOW (minibuf_window)
15209 && NILP (echo_area_buffer[0])))
15210 {
15211 struct buffer *buf = XBUFFER (w->buffer);
15212 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15213 start = marker_position (w->start) - BUF_BEGV (buf);
15214 /* I don't think this is guaranteed to be right. For the
15215 moment, we'll pretend it is. */
15216 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15217
15218 if (end < start)
15219 end = start;
15220 if (whole < (end - start))
15221 whole = end - start;
15222 }
15223 else
15224 start = end = whole = 0;
15225
15226 /* Indicate what this scroll bar ought to be displaying now. */
15227 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15228 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15229 (w, end - start, whole, start);
15230 }
15231
15232
15233 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15234 selected_window is redisplayed.
15235
15236 We can return without actually redisplaying the window if
15237 fonts_changed_p is nonzero. In that case, redisplay_internal will
15238 retry. */
15239
15240 static void
15241 redisplay_window (Lisp_Object window, int just_this_one_p)
15242 {
15243 struct window *w = XWINDOW (window);
15244 struct frame *f = XFRAME (w->frame);
15245 struct buffer *buffer = XBUFFER (w->buffer);
15246 struct buffer *old = current_buffer;
15247 struct text_pos lpoint, opoint, startp;
15248 int update_mode_line;
15249 int tem;
15250 struct it it;
15251 /* Record it now because it's overwritten. */
15252 int current_matrix_up_to_date_p = 0;
15253 int used_current_matrix_p = 0;
15254 /* This is less strict than current_matrix_up_to_date_p.
15255 It indicates that the buffer contents and narrowing are unchanged. */
15256 int buffer_unchanged_p = 0;
15257 int temp_scroll_step = 0;
15258 int count = SPECPDL_INDEX ();
15259 int rc;
15260 int centering_position = -1;
15261 int last_line_misfit = 0;
15262 EMACS_INT beg_unchanged, end_unchanged;
15263
15264 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15265 opoint = lpoint;
15266
15267 /* W must be a leaf window here. */
15268 xassert (!NILP (w->buffer));
15269 #if GLYPH_DEBUG
15270 *w->desired_matrix->method = 0;
15271 #endif
15272
15273 restart:
15274 reconsider_clip_changes (w, buffer);
15275
15276 /* Has the mode line to be updated? */
15277 update_mode_line = (!NILP (w->update_mode_line)
15278 || update_mode_lines
15279 || buffer->clip_changed
15280 || buffer->prevent_redisplay_optimizations_p);
15281
15282 if (MINI_WINDOW_P (w))
15283 {
15284 if (w == XWINDOW (echo_area_window)
15285 && !NILP (echo_area_buffer[0]))
15286 {
15287 if (update_mode_line)
15288 /* We may have to update a tty frame's menu bar or a
15289 tool-bar. Example `M-x C-h C-h C-g'. */
15290 goto finish_menu_bars;
15291 else
15292 /* We've already displayed the echo area glyphs in this window. */
15293 goto finish_scroll_bars;
15294 }
15295 else if ((w != XWINDOW (minibuf_window)
15296 || minibuf_level == 0)
15297 /* When buffer is nonempty, redisplay window normally. */
15298 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15299 /* Quail displays non-mini buffers in minibuffer window.
15300 In that case, redisplay the window normally. */
15301 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15302 {
15303 /* W is a mini-buffer window, but it's not active, so clear
15304 it. */
15305 int yb = window_text_bottom_y (w);
15306 struct glyph_row *row;
15307 int y;
15308
15309 for (y = 0, row = w->desired_matrix->rows;
15310 y < yb;
15311 y += row->height, ++row)
15312 blank_row (w, row, y);
15313 goto finish_scroll_bars;
15314 }
15315
15316 clear_glyph_matrix (w->desired_matrix);
15317 }
15318
15319 /* Otherwise set up data on this window; select its buffer and point
15320 value. */
15321 /* Really select the buffer, for the sake of buffer-local
15322 variables. */
15323 set_buffer_internal_1 (XBUFFER (w->buffer));
15324
15325 current_matrix_up_to_date_p
15326 = (!NILP (w->window_end_valid)
15327 && !current_buffer->clip_changed
15328 && !current_buffer->prevent_redisplay_optimizations_p
15329 && XFASTINT (w->last_modified) >= MODIFF
15330 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15331
15332 /* Run the window-bottom-change-functions
15333 if it is possible that the text on the screen has changed
15334 (either due to modification of the text, or any other reason). */
15335 if (!current_matrix_up_to_date_p
15336 && !NILP (Vwindow_text_change_functions))
15337 {
15338 safe_run_hooks (Qwindow_text_change_functions);
15339 goto restart;
15340 }
15341
15342 beg_unchanged = BEG_UNCHANGED;
15343 end_unchanged = END_UNCHANGED;
15344
15345 SET_TEXT_POS (opoint, PT, PT_BYTE);
15346
15347 specbind (Qinhibit_point_motion_hooks, Qt);
15348
15349 buffer_unchanged_p
15350 = (!NILP (w->window_end_valid)
15351 && !current_buffer->clip_changed
15352 && XFASTINT (w->last_modified) >= MODIFF
15353 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15354
15355 /* When windows_or_buffers_changed is non-zero, we can't rely on
15356 the window end being valid, so set it to nil there. */
15357 if (windows_or_buffers_changed)
15358 {
15359 /* If window starts on a continuation line, maybe adjust the
15360 window start in case the window's width changed. */
15361 if (XMARKER (w->start)->buffer == current_buffer)
15362 compute_window_start_on_continuation_line (w);
15363
15364 w->window_end_valid = Qnil;
15365 }
15366
15367 /* Some sanity checks. */
15368 CHECK_WINDOW_END (w);
15369 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15370 abort ();
15371 if (BYTEPOS (opoint) < CHARPOS (opoint))
15372 abort ();
15373
15374 /* If %c is in mode line, update it if needed. */
15375 if (!NILP (w->column_number_displayed)
15376 /* This alternative quickly identifies a common case
15377 where no change is needed. */
15378 && !(PT == XFASTINT (w->last_point)
15379 && XFASTINT (w->last_modified) >= MODIFF
15380 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15381 && (XFASTINT (w->column_number_displayed) != current_column ()))
15382 update_mode_line = 1;
15383
15384 /* Count number of windows showing the selected buffer. An indirect
15385 buffer counts as its base buffer. */
15386 if (!just_this_one_p)
15387 {
15388 struct buffer *current_base, *window_base;
15389 current_base = current_buffer;
15390 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15391 if (current_base->base_buffer)
15392 current_base = current_base->base_buffer;
15393 if (window_base->base_buffer)
15394 window_base = window_base->base_buffer;
15395 if (current_base == window_base)
15396 buffer_shared++;
15397 }
15398
15399 /* Point refers normally to the selected window. For any other
15400 window, set up appropriate value. */
15401 if (!EQ (window, selected_window))
15402 {
15403 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15404 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15405 if (new_pt < BEGV)
15406 {
15407 new_pt = BEGV;
15408 new_pt_byte = BEGV_BYTE;
15409 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15410 }
15411 else if (new_pt > (ZV - 1))
15412 {
15413 new_pt = ZV;
15414 new_pt_byte = ZV_BYTE;
15415 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15416 }
15417
15418 /* We don't use SET_PT so that the point-motion hooks don't run. */
15419 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15420 }
15421
15422 /* If any of the character widths specified in the display table
15423 have changed, invalidate the width run cache. It's true that
15424 this may be a bit late to catch such changes, but the rest of
15425 redisplay goes (non-fatally) haywire when the display table is
15426 changed, so why should we worry about doing any better? */
15427 if (current_buffer->width_run_cache)
15428 {
15429 struct Lisp_Char_Table *disptab = buffer_display_table ();
15430
15431 if (! disptab_matches_widthtab (disptab,
15432 XVECTOR (BVAR (current_buffer, width_table))))
15433 {
15434 invalidate_region_cache (current_buffer,
15435 current_buffer->width_run_cache,
15436 BEG, Z);
15437 recompute_width_table (current_buffer, disptab);
15438 }
15439 }
15440
15441 /* If window-start is screwed up, choose a new one. */
15442 if (XMARKER (w->start)->buffer != current_buffer)
15443 goto recenter;
15444
15445 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15446
15447 /* If someone specified a new starting point but did not insist,
15448 check whether it can be used. */
15449 if (!NILP (w->optional_new_start)
15450 && CHARPOS (startp) >= BEGV
15451 && CHARPOS (startp) <= ZV)
15452 {
15453 w->optional_new_start = Qnil;
15454 start_display (&it, w, startp);
15455 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15456 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15457 if (IT_CHARPOS (it) == PT)
15458 w->force_start = Qt;
15459 /* IT may overshoot PT if text at PT is invisible. */
15460 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15461 w->force_start = Qt;
15462 }
15463
15464 force_start:
15465
15466 /* Handle case where place to start displaying has been specified,
15467 unless the specified location is outside the accessible range. */
15468 if (!NILP (w->force_start)
15469 || w->frozen_window_start_p)
15470 {
15471 /* We set this later on if we have to adjust point. */
15472 int new_vpos = -1;
15473
15474 w->force_start = Qnil;
15475 w->vscroll = 0;
15476 w->window_end_valid = Qnil;
15477
15478 /* Forget any recorded base line for line number display. */
15479 if (!buffer_unchanged_p)
15480 w->base_line_number = Qnil;
15481
15482 /* Redisplay the mode line. Select the buffer properly for that.
15483 Also, run the hook window-scroll-functions
15484 because we have scrolled. */
15485 /* Note, we do this after clearing force_start because
15486 if there's an error, it is better to forget about force_start
15487 than to get into an infinite loop calling the hook functions
15488 and having them get more errors. */
15489 if (!update_mode_line
15490 || ! NILP (Vwindow_scroll_functions))
15491 {
15492 update_mode_line = 1;
15493 w->update_mode_line = Qt;
15494 startp = run_window_scroll_functions (window, startp);
15495 }
15496
15497 w->last_modified = make_number (0);
15498 w->last_overlay_modified = make_number (0);
15499 if (CHARPOS (startp) < BEGV)
15500 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15501 else if (CHARPOS (startp) > ZV)
15502 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15503
15504 /* Redisplay, then check if cursor has been set during the
15505 redisplay. Give up if new fonts were loaded. */
15506 /* We used to issue a CHECK_MARGINS argument to try_window here,
15507 but this causes scrolling to fail when point begins inside
15508 the scroll margin (bug#148) -- cyd */
15509 if (!try_window (window, startp, 0))
15510 {
15511 w->force_start = Qt;
15512 clear_glyph_matrix (w->desired_matrix);
15513 goto need_larger_matrices;
15514 }
15515
15516 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15517 {
15518 /* If point does not appear, try to move point so it does
15519 appear. The desired matrix has been built above, so we
15520 can use it here. */
15521 new_vpos = window_box_height (w) / 2;
15522 }
15523
15524 if (!cursor_row_fully_visible_p (w, 0, 0))
15525 {
15526 /* Point does appear, but on a line partly visible at end of window.
15527 Move it back to a fully-visible line. */
15528 new_vpos = window_box_height (w);
15529 }
15530
15531 /* If we need to move point for either of the above reasons,
15532 now actually do it. */
15533 if (new_vpos >= 0)
15534 {
15535 struct glyph_row *row;
15536
15537 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15538 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15539 ++row;
15540
15541 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15542 MATRIX_ROW_START_BYTEPOS (row));
15543
15544 if (w != XWINDOW (selected_window))
15545 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15546 else if (current_buffer == old)
15547 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15548
15549 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15550
15551 /* If we are highlighting the region, then we just changed
15552 the region, so redisplay to show it. */
15553 if (!NILP (Vtransient_mark_mode)
15554 && !NILP (BVAR (current_buffer, mark_active)))
15555 {
15556 clear_glyph_matrix (w->desired_matrix);
15557 if (!try_window (window, startp, 0))
15558 goto need_larger_matrices;
15559 }
15560 }
15561
15562 #if GLYPH_DEBUG
15563 debug_method_add (w, "forced window start");
15564 #endif
15565 goto done;
15566 }
15567
15568 /* Handle case where text has not changed, only point, and it has
15569 not moved off the frame, and we are not retrying after hscroll.
15570 (current_matrix_up_to_date_p is nonzero when retrying.) */
15571 if (current_matrix_up_to_date_p
15572 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15573 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15574 {
15575 switch (rc)
15576 {
15577 case CURSOR_MOVEMENT_SUCCESS:
15578 used_current_matrix_p = 1;
15579 goto done;
15580
15581 case CURSOR_MOVEMENT_MUST_SCROLL:
15582 goto try_to_scroll;
15583
15584 default:
15585 abort ();
15586 }
15587 }
15588 /* If current starting point was originally the beginning of a line
15589 but no longer is, find a new starting point. */
15590 else if (!NILP (w->start_at_line_beg)
15591 && !(CHARPOS (startp) <= BEGV
15592 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15593 {
15594 #if GLYPH_DEBUG
15595 debug_method_add (w, "recenter 1");
15596 #endif
15597 goto recenter;
15598 }
15599
15600 /* Try scrolling with try_window_id. Value is > 0 if update has
15601 been done, it is -1 if we know that the same window start will
15602 not work. It is 0 if unsuccessful for some other reason. */
15603 else if ((tem = try_window_id (w)) != 0)
15604 {
15605 #if GLYPH_DEBUG
15606 debug_method_add (w, "try_window_id %d", tem);
15607 #endif
15608
15609 if (fonts_changed_p)
15610 goto need_larger_matrices;
15611 if (tem > 0)
15612 goto done;
15613
15614 /* Otherwise try_window_id has returned -1 which means that we
15615 don't want the alternative below this comment to execute. */
15616 }
15617 else if (CHARPOS (startp) >= BEGV
15618 && CHARPOS (startp) <= ZV
15619 && PT >= CHARPOS (startp)
15620 && (CHARPOS (startp) < ZV
15621 /* Avoid starting at end of buffer. */
15622 || CHARPOS (startp) == BEGV
15623 || (XFASTINT (w->last_modified) >= MODIFF
15624 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15625 {
15626 int d1, d2, d3, d4, d5, d6;
15627
15628 /* If first window line is a continuation line, and window start
15629 is inside the modified region, but the first change is before
15630 current window start, we must select a new window start.
15631
15632 However, if this is the result of a down-mouse event (e.g. by
15633 extending the mouse-drag-overlay), we don't want to select a
15634 new window start, since that would change the position under
15635 the mouse, resulting in an unwanted mouse-movement rather
15636 than a simple mouse-click. */
15637 if (NILP (w->start_at_line_beg)
15638 && NILP (do_mouse_tracking)
15639 && CHARPOS (startp) > BEGV
15640 && CHARPOS (startp) > BEG + beg_unchanged
15641 && CHARPOS (startp) <= Z - end_unchanged
15642 /* Even if w->start_at_line_beg is nil, a new window may
15643 start at a line_beg, since that's how set_buffer_window
15644 sets it. So, we need to check the return value of
15645 compute_window_start_on_continuation_line. (See also
15646 bug#197). */
15647 && XMARKER (w->start)->buffer == current_buffer
15648 && compute_window_start_on_continuation_line (w)
15649 /* It doesn't make sense to force the window start like we
15650 do at label force_start if it is already known that point
15651 will not be visible in the resulting window, because
15652 doing so will move point from its correct position
15653 instead of scrolling the window to bring point into view.
15654 See bug#9324. */
15655 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15656 {
15657 w->force_start = Qt;
15658 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15659 goto force_start;
15660 }
15661
15662 #if GLYPH_DEBUG
15663 debug_method_add (w, "same window start");
15664 #endif
15665
15666 /* Try to redisplay starting at same place as before.
15667 If point has not moved off frame, accept the results. */
15668 if (!current_matrix_up_to_date_p
15669 /* Don't use try_window_reusing_current_matrix in this case
15670 because a window scroll function can have changed the
15671 buffer. */
15672 || !NILP (Vwindow_scroll_functions)
15673 || MINI_WINDOW_P (w)
15674 || !(used_current_matrix_p
15675 = try_window_reusing_current_matrix (w)))
15676 {
15677 IF_DEBUG (debug_method_add (w, "1"));
15678 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15679 /* -1 means we need to scroll.
15680 0 means we need new matrices, but fonts_changed_p
15681 is set in that case, so we will detect it below. */
15682 goto try_to_scroll;
15683 }
15684
15685 if (fonts_changed_p)
15686 goto need_larger_matrices;
15687
15688 if (w->cursor.vpos >= 0)
15689 {
15690 if (!just_this_one_p
15691 || current_buffer->clip_changed
15692 || BEG_UNCHANGED < CHARPOS (startp))
15693 /* Forget any recorded base line for line number display. */
15694 w->base_line_number = Qnil;
15695
15696 if (!cursor_row_fully_visible_p (w, 1, 0))
15697 {
15698 clear_glyph_matrix (w->desired_matrix);
15699 last_line_misfit = 1;
15700 }
15701 /* Drop through and scroll. */
15702 else
15703 goto done;
15704 }
15705 else
15706 clear_glyph_matrix (w->desired_matrix);
15707 }
15708
15709 try_to_scroll:
15710
15711 w->last_modified = make_number (0);
15712 w->last_overlay_modified = make_number (0);
15713
15714 /* Redisplay the mode line. Select the buffer properly for that. */
15715 if (!update_mode_line)
15716 {
15717 update_mode_line = 1;
15718 w->update_mode_line = Qt;
15719 }
15720
15721 /* Try to scroll by specified few lines. */
15722 if ((scroll_conservatively
15723 || emacs_scroll_step
15724 || temp_scroll_step
15725 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15726 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15727 && CHARPOS (startp) >= BEGV
15728 && CHARPOS (startp) <= ZV)
15729 {
15730 /* The function returns -1 if new fonts were loaded, 1 if
15731 successful, 0 if not successful. */
15732 int ss = try_scrolling (window, just_this_one_p,
15733 scroll_conservatively,
15734 emacs_scroll_step,
15735 temp_scroll_step, last_line_misfit);
15736 switch (ss)
15737 {
15738 case SCROLLING_SUCCESS:
15739 goto done;
15740
15741 case SCROLLING_NEED_LARGER_MATRICES:
15742 goto need_larger_matrices;
15743
15744 case SCROLLING_FAILED:
15745 break;
15746
15747 default:
15748 abort ();
15749 }
15750 }
15751
15752 /* Finally, just choose a place to start which positions point
15753 according to user preferences. */
15754
15755 recenter:
15756
15757 #if GLYPH_DEBUG
15758 debug_method_add (w, "recenter");
15759 #endif
15760
15761 /* w->vscroll = 0; */
15762
15763 /* Forget any previously recorded base line for line number display. */
15764 if (!buffer_unchanged_p)
15765 w->base_line_number = Qnil;
15766
15767 /* Determine the window start relative to point. */
15768 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15769 it.current_y = it.last_visible_y;
15770 if (centering_position < 0)
15771 {
15772 int margin =
15773 scroll_margin > 0
15774 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15775 : 0;
15776 EMACS_INT margin_pos = CHARPOS (startp);
15777 Lisp_Object aggressive;
15778 int scrolling_up;
15779
15780 /* If there is a scroll margin at the top of the window, find
15781 its character position. */
15782 if (margin
15783 /* Cannot call start_display if startp is not in the
15784 accessible region of the buffer. This can happen when we
15785 have just switched to a different buffer and/or changed
15786 its restriction. In that case, startp is initialized to
15787 the character position 1 (BEGV) because we did not yet
15788 have chance to display the buffer even once. */
15789 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15790 {
15791 struct it it1;
15792 void *it1data = NULL;
15793
15794 SAVE_IT (it1, it, it1data);
15795 start_display (&it1, w, startp);
15796 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15797 margin_pos = IT_CHARPOS (it1);
15798 RESTORE_IT (&it, &it, it1data);
15799 }
15800 scrolling_up = PT > margin_pos;
15801 aggressive =
15802 scrolling_up
15803 ? BVAR (current_buffer, scroll_up_aggressively)
15804 : BVAR (current_buffer, scroll_down_aggressively);
15805
15806 if (!MINI_WINDOW_P (w)
15807 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15808 {
15809 int pt_offset = 0;
15810
15811 /* Setting scroll-conservatively overrides
15812 scroll-*-aggressively. */
15813 if (!scroll_conservatively && NUMBERP (aggressive))
15814 {
15815 double float_amount = XFLOATINT (aggressive);
15816
15817 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15818 if (pt_offset == 0 && float_amount > 0)
15819 pt_offset = 1;
15820 if (pt_offset && margin > 0)
15821 margin -= 1;
15822 }
15823 /* Compute how much to move the window start backward from
15824 point so that point will be displayed where the user
15825 wants it. */
15826 if (scrolling_up)
15827 {
15828 centering_position = it.last_visible_y;
15829 if (pt_offset)
15830 centering_position -= pt_offset;
15831 centering_position -=
15832 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15833 + WINDOW_HEADER_LINE_HEIGHT (w);
15834 /* Don't let point enter the scroll margin near top of
15835 the window. */
15836 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15837 centering_position = margin * FRAME_LINE_HEIGHT (f);
15838 }
15839 else
15840 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15841 }
15842 else
15843 /* Set the window start half the height of the window backward
15844 from point. */
15845 centering_position = window_box_height (w) / 2;
15846 }
15847 move_it_vertically_backward (&it, centering_position);
15848
15849 xassert (IT_CHARPOS (it) >= BEGV);
15850
15851 /* The function move_it_vertically_backward may move over more
15852 than the specified y-distance. If it->w is small, e.g. a
15853 mini-buffer window, we may end up in front of the window's
15854 display area. Start displaying at the start of the line
15855 containing PT in this case. */
15856 if (it.current_y <= 0)
15857 {
15858 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15859 move_it_vertically_backward (&it, 0);
15860 it.current_y = 0;
15861 }
15862
15863 it.current_x = it.hpos = 0;
15864
15865 /* Set the window start position here explicitly, to avoid an
15866 infinite loop in case the functions in window-scroll-functions
15867 get errors. */
15868 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15869
15870 /* Run scroll hooks. */
15871 startp = run_window_scroll_functions (window, it.current.pos);
15872
15873 /* Redisplay the window. */
15874 if (!current_matrix_up_to_date_p
15875 || windows_or_buffers_changed
15876 || cursor_type_changed
15877 /* Don't use try_window_reusing_current_matrix in this case
15878 because it can have changed the buffer. */
15879 || !NILP (Vwindow_scroll_functions)
15880 || !just_this_one_p
15881 || MINI_WINDOW_P (w)
15882 || !(used_current_matrix_p
15883 = try_window_reusing_current_matrix (w)))
15884 try_window (window, startp, 0);
15885
15886 /* If new fonts have been loaded (due to fontsets), give up. We
15887 have to start a new redisplay since we need to re-adjust glyph
15888 matrices. */
15889 if (fonts_changed_p)
15890 goto need_larger_matrices;
15891
15892 /* If cursor did not appear assume that the middle of the window is
15893 in the first line of the window. Do it again with the next line.
15894 (Imagine a window of height 100, displaying two lines of height
15895 60. Moving back 50 from it->last_visible_y will end in the first
15896 line.) */
15897 if (w->cursor.vpos < 0)
15898 {
15899 if (!NILP (w->window_end_valid)
15900 && PT >= Z - XFASTINT (w->window_end_pos))
15901 {
15902 clear_glyph_matrix (w->desired_matrix);
15903 move_it_by_lines (&it, 1);
15904 try_window (window, it.current.pos, 0);
15905 }
15906 else if (PT < IT_CHARPOS (it))
15907 {
15908 clear_glyph_matrix (w->desired_matrix);
15909 move_it_by_lines (&it, -1);
15910 try_window (window, it.current.pos, 0);
15911 }
15912 else
15913 {
15914 /* Not much we can do about it. */
15915 }
15916 }
15917
15918 /* Consider the following case: Window starts at BEGV, there is
15919 invisible, intangible text at BEGV, so that display starts at
15920 some point START > BEGV. It can happen that we are called with
15921 PT somewhere between BEGV and START. Try to handle that case. */
15922 if (w->cursor.vpos < 0)
15923 {
15924 struct glyph_row *row = w->current_matrix->rows;
15925 if (row->mode_line_p)
15926 ++row;
15927 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15928 }
15929
15930 if (!cursor_row_fully_visible_p (w, 0, 0))
15931 {
15932 /* If vscroll is enabled, disable it and try again. */
15933 if (w->vscroll)
15934 {
15935 w->vscroll = 0;
15936 clear_glyph_matrix (w->desired_matrix);
15937 goto recenter;
15938 }
15939
15940 /* Users who set scroll-conservatively to a large number want
15941 point just above/below the scroll margin. If we ended up
15942 with point's row partially visible, move the window start to
15943 make that row fully visible and out of the margin. */
15944 if (scroll_conservatively > SCROLL_LIMIT)
15945 {
15946 int margin =
15947 scroll_margin > 0
15948 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15949 : 0;
15950 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15951
15952 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15953 clear_glyph_matrix (w->desired_matrix);
15954 if (1 == try_window (window, it.current.pos,
15955 TRY_WINDOW_CHECK_MARGINS))
15956 goto done;
15957 }
15958
15959 /* If centering point failed to make the whole line visible,
15960 put point at the top instead. That has to make the whole line
15961 visible, if it can be done. */
15962 if (centering_position == 0)
15963 goto done;
15964
15965 clear_glyph_matrix (w->desired_matrix);
15966 centering_position = 0;
15967 goto recenter;
15968 }
15969
15970 done:
15971
15972 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15973 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15974 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15975 ? Qt : Qnil);
15976
15977 /* Display the mode line, if we must. */
15978 if ((update_mode_line
15979 /* If window not full width, must redo its mode line
15980 if (a) the window to its side is being redone and
15981 (b) we do a frame-based redisplay. This is a consequence
15982 of how inverted lines are drawn in frame-based redisplay. */
15983 || (!just_this_one_p
15984 && !FRAME_WINDOW_P (f)
15985 && !WINDOW_FULL_WIDTH_P (w))
15986 /* Line number to display. */
15987 || INTEGERP (w->base_line_pos)
15988 /* Column number is displayed and different from the one displayed. */
15989 || (!NILP (w->column_number_displayed)
15990 && (XFASTINT (w->column_number_displayed) != current_column ())))
15991 /* This means that the window has a mode line. */
15992 && (WINDOW_WANTS_MODELINE_P (w)
15993 || WINDOW_WANTS_HEADER_LINE_P (w)))
15994 {
15995 display_mode_lines (w);
15996
15997 /* If mode line height has changed, arrange for a thorough
15998 immediate redisplay using the correct mode line height. */
15999 if (WINDOW_WANTS_MODELINE_P (w)
16000 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16001 {
16002 fonts_changed_p = 1;
16003 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16004 = DESIRED_MODE_LINE_HEIGHT (w);
16005 }
16006
16007 /* If header line height has changed, arrange for a thorough
16008 immediate redisplay using the correct header line height. */
16009 if (WINDOW_WANTS_HEADER_LINE_P (w)
16010 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16011 {
16012 fonts_changed_p = 1;
16013 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16014 = DESIRED_HEADER_LINE_HEIGHT (w);
16015 }
16016
16017 if (fonts_changed_p)
16018 goto need_larger_matrices;
16019 }
16020
16021 if (!line_number_displayed
16022 && !BUFFERP (w->base_line_pos))
16023 {
16024 w->base_line_pos = Qnil;
16025 w->base_line_number = Qnil;
16026 }
16027
16028 finish_menu_bars:
16029
16030 /* When we reach a frame's selected window, redo the frame's menu bar. */
16031 if (update_mode_line
16032 && EQ (FRAME_SELECTED_WINDOW (f), window))
16033 {
16034 int redisplay_menu_p = 0;
16035
16036 if (FRAME_WINDOW_P (f))
16037 {
16038 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16039 || defined (HAVE_NS) || defined (USE_GTK)
16040 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16041 #else
16042 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16043 #endif
16044 }
16045 else
16046 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16047
16048 if (redisplay_menu_p)
16049 display_menu_bar (w);
16050
16051 #ifdef HAVE_WINDOW_SYSTEM
16052 if (FRAME_WINDOW_P (f))
16053 {
16054 #if defined (USE_GTK) || defined (HAVE_NS)
16055 if (FRAME_EXTERNAL_TOOL_BAR (f))
16056 redisplay_tool_bar (f);
16057 #else
16058 if (WINDOWP (f->tool_bar_window)
16059 && (FRAME_TOOL_BAR_LINES (f) > 0
16060 || !NILP (Vauto_resize_tool_bars))
16061 && redisplay_tool_bar (f))
16062 ignore_mouse_drag_p = 1;
16063 #endif
16064 }
16065 #endif
16066 }
16067
16068 #ifdef HAVE_WINDOW_SYSTEM
16069 if (FRAME_WINDOW_P (f)
16070 && update_window_fringes (w, (just_this_one_p
16071 || (!used_current_matrix_p && !overlay_arrow_seen)
16072 || w->pseudo_window_p)))
16073 {
16074 update_begin (f);
16075 BLOCK_INPUT;
16076 if (draw_window_fringes (w, 1))
16077 x_draw_vertical_border (w);
16078 UNBLOCK_INPUT;
16079 update_end (f);
16080 }
16081 #endif /* HAVE_WINDOW_SYSTEM */
16082
16083 /* We go to this label, with fonts_changed_p nonzero,
16084 if it is necessary to try again using larger glyph matrices.
16085 We have to redeem the scroll bar even in this case,
16086 because the loop in redisplay_internal expects that. */
16087 need_larger_matrices:
16088 ;
16089 finish_scroll_bars:
16090
16091 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16092 {
16093 /* Set the thumb's position and size. */
16094 set_vertical_scroll_bar (w);
16095
16096 /* Note that we actually used the scroll bar attached to this
16097 window, so it shouldn't be deleted at the end of redisplay. */
16098 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16099 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16100 }
16101
16102 /* Restore current_buffer and value of point in it. The window
16103 update may have changed the buffer, so first make sure `opoint'
16104 is still valid (Bug#6177). */
16105 if (CHARPOS (opoint) < BEGV)
16106 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16107 else if (CHARPOS (opoint) > ZV)
16108 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16109 else
16110 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16111
16112 set_buffer_internal_1 (old);
16113 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16114 shorter. This can be caused by log truncation in *Messages*. */
16115 if (CHARPOS (lpoint) <= ZV)
16116 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16117
16118 unbind_to (count, Qnil);
16119 }
16120
16121
16122 /* Build the complete desired matrix of WINDOW with a window start
16123 buffer position POS.
16124
16125 Value is 1 if successful. It is zero if fonts were loaded during
16126 redisplay which makes re-adjusting glyph matrices necessary, and -1
16127 if point would appear in the scroll margins.
16128 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16129 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16130 set in FLAGS.) */
16131
16132 int
16133 try_window (Lisp_Object window, struct text_pos pos, int flags)
16134 {
16135 struct window *w = XWINDOW (window);
16136 struct it it;
16137 struct glyph_row *last_text_row = NULL;
16138 struct frame *f = XFRAME (w->frame);
16139
16140 /* Make POS the new window start. */
16141 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16142
16143 /* Mark cursor position as unknown. No overlay arrow seen. */
16144 w->cursor.vpos = -1;
16145 overlay_arrow_seen = 0;
16146
16147 /* Initialize iterator and info to start at POS. */
16148 start_display (&it, w, pos);
16149
16150 /* Display all lines of W. */
16151 while (it.current_y < it.last_visible_y)
16152 {
16153 if (display_line (&it))
16154 last_text_row = it.glyph_row - 1;
16155 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16156 return 0;
16157 }
16158
16159 /* Don't let the cursor end in the scroll margins. */
16160 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16161 && !MINI_WINDOW_P (w))
16162 {
16163 int this_scroll_margin;
16164
16165 if (scroll_margin > 0)
16166 {
16167 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16168 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16169 }
16170 else
16171 this_scroll_margin = 0;
16172
16173 if ((w->cursor.y >= 0 /* not vscrolled */
16174 && w->cursor.y < this_scroll_margin
16175 && CHARPOS (pos) > BEGV
16176 && IT_CHARPOS (it) < ZV)
16177 /* rms: considering make_cursor_line_fully_visible_p here
16178 seems to give wrong results. We don't want to recenter
16179 when the last line is partly visible, we want to allow
16180 that case to be handled in the usual way. */
16181 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16182 {
16183 w->cursor.vpos = -1;
16184 clear_glyph_matrix (w->desired_matrix);
16185 return -1;
16186 }
16187 }
16188
16189 /* If bottom moved off end of frame, change mode line percentage. */
16190 if (XFASTINT (w->window_end_pos) <= 0
16191 && Z != IT_CHARPOS (it))
16192 w->update_mode_line = Qt;
16193
16194 /* Set window_end_pos to the offset of the last character displayed
16195 on the window from the end of current_buffer. Set
16196 window_end_vpos to its row number. */
16197 if (last_text_row)
16198 {
16199 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16200 w->window_end_bytepos
16201 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16202 w->window_end_pos
16203 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16204 w->window_end_vpos
16205 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16206 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16207 ->displays_text_p);
16208 }
16209 else
16210 {
16211 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16212 w->window_end_pos = make_number (Z - ZV);
16213 w->window_end_vpos = make_number (0);
16214 }
16215
16216 /* But that is not valid info until redisplay finishes. */
16217 w->window_end_valid = Qnil;
16218 return 1;
16219 }
16220
16221
16222 \f
16223 /************************************************************************
16224 Window redisplay reusing current matrix when buffer has not changed
16225 ************************************************************************/
16226
16227 /* Try redisplay of window W showing an unchanged buffer with a
16228 different window start than the last time it was displayed by
16229 reusing its current matrix. Value is non-zero if successful.
16230 W->start is the new window start. */
16231
16232 static int
16233 try_window_reusing_current_matrix (struct window *w)
16234 {
16235 struct frame *f = XFRAME (w->frame);
16236 struct glyph_row *bottom_row;
16237 struct it it;
16238 struct run run;
16239 struct text_pos start, new_start;
16240 int nrows_scrolled, i;
16241 struct glyph_row *last_text_row;
16242 struct glyph_row *last_reused_text_row;
16243 struct glyph_row *start_row;
16244 int start_vpos, min_y, max_y;
16245
16246 #if GLYPH_DEBUG
16247 if (inhibit_try_window_reusing)
16248 return 0;
16249 #endif
16250
16251 if (/* This function doesn't handle terminal frames. */
16252 !FRAME_WINDOW_P (f)
16253 /* Don't try to reuse the display if windows have been split
16254 or such. */
16255 || windows_or_buffers_changed
16256 || cursor_type_changed)
16257 return 0;
16258
16259 /* Can't do this if region may have changed. */
16260 if ((!NILP (Vtransient_mark_mode)
16261 && !NILP (BVAR (current_buffer, mark_active)))
16262 || !NILP (w->region_showing)
16263 || !NILP (Vshow_trailing_whitespace))
16264 return 0;
16265
16266 /* If top-line visibility has changed, give up. */
16267 if (WINDOW_WANTS_HEADER_LINE_P (w)
16268 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16269 return 0;
16270
16271 /* Give up if old or new display is scrolled vertically. We could
16272 make this function handle this, but right now it doesn't. */
16273 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16274 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16275 return 0;
16276
16277 /* The variable new_start now holds the new window start. The old
16278 start `start' can be determined from the current matrix. */
16279 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16280 start = start_row->minpos;
16281 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16282
16283 /* Clear the desired matrix for the display below. */
16284 clear_glyph_matrix (w->desired_matrix);
16285
16286 if (CHARPOS (new_start) <= CHARPOS (start))
16287 {
16288 /* Don't use this method if the display starts with an ellipsis
16289 displayed for invisible text. It's not easy to handle that case
16290 below, and it's certainly not worth the effort since this is
16291 not a frequent case. */
16292 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16293 return 0;
16294
16295 IF_DEBUG (debug_method_add (w, "twu1"));
16296
16297 /* Display up to a row that can be reused. The variable
16298 last_text_row is set to the last row displayed that displays
16299 text. Note that it.vpos == 0 if or if not there is a
16300 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16301 start_display (&it, w, new_start);
16302 w->cursor.vpos = -1;
16303 last_text_row = last_reused_text_row = NULL;
16304
16305 while (it.current_y < it.last_visible_y
16306 && !fonts_changed_p)
16307 {
16308 /* If we have reached into the characters in the START row,
16309 that means the line boundaries have changed. So we
16310 can't start copying with the row START. Maybe it will
16311 work to start copying with the following row. */
16312 while (IT_CHARPOS (it) > CHARPOS (start))
16313 {
16314 /* Advance to the next row as the "start". */
16315 start_row++;
16316 start = start_row->minpos;
16317 /* If there are no more rows to try, or just one, give up. */
16318 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16319 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16320 || CHARPOS (start) == ZV)
16321 {
16322 clear_glyph_matrix (w->desired_matrix);
16323 return 0;
16324 }
16325
16326 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16327 }
16328 /* If we have reached alignment, we can copy the rest of the
16329 rows. */
16330 if (IT_CHARPOS (it) == CHARPOS (start)
16331 /* Don't accept "alignment" inside a display vector,
16332 since start_row could have started in the middle of
16333 that same display vector (thus their character
16334 positions match), and we have no way of telling if
16335 that is the case. */
16336 && it.current.dpvec_index < 0)
16337 break;
16338
16339 if (display_line (&it))
16340 last_text_row = it.glyph_row - 1;
16341
16342 }
16343
16344 /* A value of current_y < last_visible_y means that we stopped
16345 at the previous window start, which in turn means that we
16346 have at least one reusable row. */
16347 if (it.current_y < it.last_visible_y)
16348 {
16349 struct glyph_row *row;
16350
16351 /* IT.vpos always starts from 0; it counts text lines. */
16352 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16353
16354 /* Find PT if not already found in the lines displayed. */
16355 if (w->cursor.vpos < 0)
16356 {
16357 int dy = it.current_y - start_row->y;
16358
16359 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16360 row = row_containing_pos (w, PT, row, NULL, dy);
16361 if (row)
16362 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16363 dy, nrows_scrolled);
16364 else
16365 {
16366 clear_glyph_matrix (w->desired_matrix);
16367 return 0;
16368 }
16369 }
16370
16371 /* Scroll the display. Do it before the current matrix is
16372 changed. The problem here is that update has not yet
16373 run, i.e. part of the current matrix is not up to date.
16374 scroll_run_hook will clear the cursor, and use the
16375 current matrix to get the height of the row the cursor is
16376 in. */
16377 run.current_y = start_row->y;
16378 run.desired_y = it.current_y;
16379 run.height = it.last_visible_y - it.current_y;
16380
16381 if (run.height > 0 && run.current_y != run.desired_y)
16382 {
16383 update_begin (f);
16384 FRAME_RIF (f)->update_window_begin_hook (w);
16385 FRAME_RIF (f)->clear_window_mouse_face (w);
16386 FRAME_RIF (f)->scroll_run_hook (w, &run);
16387 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16388 update_end (f);
16389 }
16390
16391 /* Shift current matrix down by nrows_scrolled lines. */
16392 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16393 rotate_matrix (w->current_matrix,
16394 start_vpos,
16395 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16396 nrows_scrolled);
16397
16398 /* Disable lines that must be updated. */
16399 for (i = 0; i < nrows_scrolled; ++i)
16400 (start_row + i)->enabled_p = 0;
16401
16402 /* Re-compute Y positions. */
16403 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16404 max_y = it.last_visible_y;
16405 for (row = start_row + nrows_scrolled;
16406 row < bottom_row;
16407 ++row)
16408 {
16409 row->y = it.current_y;
16410 row->visible_height = row->height;
16411
16412 if (row->y < min_y)
16413 row->visible_height -= min_y - row->y;
16414 if (row->y + row->height > max_y)
16415 row->visible_height -= row->y + row->height - max_y;
16416 if (row->fringe_bitmap_periodic_p)
16417 row->redraw_fringe_bitmaps_p = 1;
16418
16419 it.current_y += row->height;
16420
16421 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16422 last_reused_text_row = row;
16423 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16424 break;
16425 }
16426
16427 /* Disable lines in the current matrix which are now
16428 below the window. */
16429 for (++row; row < bottom_row; ++row)
16430 row->enabled_p = row->mode_line_p = 0;
16431 }
16432
16433 /* Update window_end_pos etc.; last_reused_text_row is the last
16434 reused row from the current matrix containing text, if any.
16435 The value of last_text_row is the last displayed line
16436 containing text. */
16437 if (last_reused_text_row)
16438 {
16439 w->window_end_bytepos
16440 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16441 w->window_end_pos
16442 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16443 w->window_end_vpos
16444 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16445 w->current_matrix));
16446 }
16447 else if (last_text_row)
16448 {
16449 w->window_end_bytepos
16450 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16451 w->window_end_pos
16452 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16453 w->window_end_vpos
16454 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16455 }
16456 else
16457 {
16458 /* This window must be completely empty. */
16459 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16460 w->window_end_pos = make_number (Z - ZV);
16461 w->window_end_vpos = make_number (0);
16462 }
16463 w->window_end_valid = Qnil;
16464
16465 /* Update hint: don't try scrolling again in update_window. */
16466 w->desired_matrix->no_scrolling_p = 1;
16467
16468 #if GLYPH_DEBUG
16469 debug_method_add (w, "try_window_reusing_current_matrix 1");
16470 #endif
16471 return 1;
16472 }
16473 else if (CHARPOS (new_start) > CHARPOS (start))
16474 {
16475 struct glyph_row *pt_row, *row;
16476 struct glyph_row *first_reusable_row;
16477 struct glyph_row *first_row_to_display;
16478 int dy;
16479 int yb = window_text_bottom_y (w);
16480
16481 /* Find the row starting at new_start, if there is one. Don't
16482 reuse a partially visible line at the end. */
16483 first_reusable_row = start_row;
16484 while (first_reusable_row->enabled_p
16485 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16486 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16487 < CHARPOS (new_start)))
16488 ++first_reusable_row;
16489
16490 /* Give up if there is no row to reuse. */
16491 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16492 || !first_reusable_row->enabled_p
16493 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16494 != CHARPOS (new_start)))
16495 return 0;
16496
16497 /* We can reuse fully visible rows beginning with
16498 first_reusable_row to the end of the window. Set
16499 first_row_to_display to the first row that cannot be reused.
16500 Set pt_row to the row containing point, if there is any. */
16501 pt_row = NULL;
16502 for (first_row_to_display = first_reusable_row;
16503 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16504 ++first_row_to_display)
16505 {
16506 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16507 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16508 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16509 && first_row_to_display->ends_at_zv_p
16510 && pt_row == NULL)))
16511 pt_row = first_row_to_display;
16512 }
16513
16514 /* Start displaying at the start of first_row_to_display. */
16515 xassert (first_row_to_display->y < yb);
16516 init_to_row_start (&it, w, first_row_to_display);
16517
16518 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16519 - start_vpos);
16520 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16521 - nrows_scrolled);
16522 it.current_y = (first_row_to_display->y - first_reusable_row->y
16523 + WINDOW_HEADER_LINE_HEIGHT (w));
16524
16525 /* Display lines beginning with first_row_to_display in the
16526 desired matrix. Set last_text_row to the last row displayed
16527 that displays text. */
16528 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16529 if (pt_row == NULL)
16530 w->cursor.vpos = -1;
16531 last_text_row = NULL;
16532 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16533 if (display_line (&it))
16534 last_text_row = it.glyph_row - 1;
16535
16536 /* If point is in a reused row, adjust y and vpos of the cursor
16537 position. */
16538 if (pt_row)
16539 {
16540 w->cursor.vpos -= nrows_scrolled;
16541 w->cursor.y -= first_reusable_row->y - start_row->y;
16542 }
16543
16544 /* Give up if point isn't in a row displayed or reused. (This
16545 also handles the case where w->cursor.vpos < nrows_scrolled
16546 after the calls to display_line, which can happen with scroll
16547 margins. See bug#1295.) */
16548 if (w->cursor.vpos < 0)
16549 {
16550 clear_glyph_matrix (w->desired_matrix);
16551 return 0;
16552 }
16553
16554 /* Scroll the display. */
16555 run.current_y = first_reusable_row->y;
16556 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16557 run.height = it.last_visible_y - run.current_y;
16558 dy = run.current_y - run.desired_y;
16559
16560 if (run.height)
16561 {
16562 update_begin (f);
16563 FRAME_RIF (f)->update_window_begin_hook (w);
16564 FRAME_RIF (f)->clear_window_mouse_face (w);
16565 FRAME_RIF (f)->scroll_run_hook (w, &run);
16566 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16567 update_end (f);
16568 }
16569
16570 /* Adjust Y positions of reused rows. */
16571 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16572 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16573 max_y = it.last_visible_y;
16574 for (row = first_reusable_row; row < first_row_to_display; ++row)
16575 {
16576 row->y -= dy;
16577 row->visible_height = row->height;
16578 if (row->y < min_y)
16579 row->visible_height -= min_y - row->y;
16580 if (row->y + row->height > max_y)
16581 row->visible_height -= row->y + row->height - max_y;
16582 if (row->fringe_bitmap_periodic_p)
16583 row->redraw_fringe_bitmaps_p = 1;
16584 }
16585
16586 /* Scroll the current matrix. */
16587 xassert (nrows_scrolled > 0);
16588 rotate_matrix (w->current_matrix,
16589 start_vpos,
16590 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16591 -nrows_scrolled);
16592
16593 /* Disable rows not reused. */
16594 for (row -= nrows_scrolled; row < bottom_row; ++row)
16595 row->enabled_p = 0;
16596
16597 /* Point may have moved to a different line, so we cannot assume that
16598 the previous cursor position is valid; locate the correct row. */
16599 if (pt_row)
16600 {
16601 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16602 row < bottom_row
16603 && PT >= MATRIX_ROW_END_CHARPOS (row)
16604 && !row->ends_at_zv_p;
16605 row++)
16606 {
16607 w->cursor.vpos++;
16608 w->cursor.y = row->y;
16609 }
16610 if (row < bottom_row)
16611 {
16612 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16613 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16614
16615 /* Can't use this optimization with bidi-reordered glyph
16616 rows, unless cursor is already at point. */
16617 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16618 {
16619 if (!(w->cursor.hpos >= 0
16620 && w->cursor.hpos < row->used[TEXT_AREA]
16621 && BUFFERP (glyph->object)
16622 && glyph->charpos == PT))
16623 return 0;
16624 }
16625 else
16626 for (; glyph < end
16627 && (!BUFFERP (glyph->object)
16628 || glyph->charpos < PT);
16629 glyph++)
16630 {
16631 w->cursor.hpos++;
16632 w->cursor.x += glyph->pixel_width;
16633 }
16634 }
16635 }
16636
16637 /* Adjust window end. A null value of last_text_row means that
16638 the window end is in reused rows which in turn means that
16639 only its vpos can have changed. */
16640 if (last_text_row)
16641 {
16642 w->window_end_bytepos
16643 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16644 w->window_end_pos
16645 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16646 w->window_end_vpos
16647 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16648 }
16649 else
16650 {
16651 w->window_end_vpos
16652 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16653 }
16654
16655 w->window_end_valid = Qnil;
16656 w->desired_matrix->no_scrolling_p = 1;
16657
16658 #if GLYPH_DEBUG
16659 debug_method_add (w, "try_window_reusing_current_matrix 2");
16660 #endif
16661 return 1;
16662 }
16663
16664 return 0;
16665 }
16666
16667
16668 \f
16669 /************************************************************************
16670 Window redisplay reusing current matrix when buffer has changed
16671 ************************************************************************/
16672
16673 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16674 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16675 EMACS_INT *, EMACS_INT *);
16676 static struct glyph_row *
16677 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16678 struct glyph_row *);
16679
16680
16681 /* Return the last row in MATRIX displaying text. If row START is
16682 non-null, start searching with that row. IT gives the dimensions
16683 of the display. Value is null if matrix is empty; otherwise it is
16684 a pointer to the row found. */
16685
16686 static struct glyph_row *
16687 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16688 struct glyph_row *start)
16689 {
16690 struct glyph_row *row, *row_found;
16691
16692 /* Set row_found to the last row in IT->w's current matrix
16693 displaying text. The loop looks funny but think of partially
16694 visible lines. */
16695 row_found = NULL;
16696 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16697 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16698 {
16699 xassert (row->enabled_p);
16700 row_found = row;
16701 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16702 break;
16703 ++row;
16704 }
16705
16706 return row_found;
16707 }
16708
16709
16710 /* Return the last row in the current matrix of W that is not affected
16711 by changes at the start of current_buffer that occurred since W's
16712 current matrix was built. Value is null if no such row exists.
16713
16714 BEG_UNCHANGED us the number of characters unchanged at the start of
16715 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16716 first changed character in current_buffer. Characters at positions <
16717 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16718 when the current matrix was built. */
16719
16720 static struct glyph_row *
16721 find_last_unchanged_at_beg_row (struct window *w)
16722 {
16723 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16724 struct glyph_row *row;
16725 struct glyph_row *row_found = NULL;
16726 int yb = window_text_bottom_y (w);
16727
16728 /* Find the last row displaying unchanged text. */
16729 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16730 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16731 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16732 ++row)
16733 {
16734 if (/* If row ends before first_changed_pos, it is unchanged,
16735 except in some case. */
16736 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16737 /* When row ends in ZV and we write at ZV it is not
16738 unchanged. */
16739 && !row->ends_at_zv_p
16740 /* When first_changed_pos is the end of a continued line,
16741 row is not unchanged because it may be no longer
16742 continued. */
16743 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16744 && (row->continued_p
16745 || row->exact_window_width_line_p))
16746 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16747 needs to be recomputed, so don't consider this row as
16748 unchanged. This happens when the last line was
16749 bidi-reordered and was killed immediately before this
16750 redisplay cycle. In that case, ROW->end stores the
16751 buffer position of the first visual-order character of
16752 the killed text, which is now beyond ZV. */
16753 && CHARPOS (row->end.pos) <= ZV)
16754 row_found = row;
16755
16756 /* Stop if last visible row. */
16757 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16758 break;
16759 }
16760
16761 return row_found;
16762 }
16763
16764
16765 /* Find the first glyph row in the current matrix of W that is not
16766 affected by changes at the end of current_buffer since the
16767 time W's current matrix was built.
16768
16769 Return in *DELTA the number of chars by which buffer positions in
16770 unchanged text at the end of current_buffer must be adjusted.
16771
16772 Return in *DELTA_BYTES the corresponding number of bytes.
16773
16774 Value is null if no such row exists, i.e. all rows are affected by
16775 changes. */
16776
16777 static struct glyph_row *
16778 find_first_unchanged_at_end_row (struct window *w,
16779 EMACS_INT *delta, EMACS_INT *delta_bytes)
16780 {
16781 struct glyph_row *row;
16782 struct glyph_row *row_found = NULL;
16783
16784 *delta = *delta_bytes = 0;
16785
16786 /* Display must not have been paused, otherwise the current matrix
16787 is not up to date. */
16788 eassert (!NILP (w->window_end_valid));
16789
16790 /* A value of window_end_pos >= END_UNCHANGED means that the window
16791 end is in the range of changed text. If so, there is no
16792 unchanged row at the end of W's current matrix. */
16793 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16794 return NULL;
16795
16796 /* Set row to the last row in W's current matrix displaying text. */
16797 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16798
16799 /* If matrix is entirely empty, no unchanged row exists. */
16800 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16801 {
16802 /* The value of row is the last glyph row in the matrix having a
16803 meaningful buffer position in it. The end position of row
16804 corresponds to window_end_pos. This allows us to translate
16805 buffer positions in the current matrix to current buffer
16806 positions for characters not in changed text. */
16807 EMACS_INT Z_old =
16808 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16809 EMACS_INT Z_BYTE_old =
16810 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16811 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16812 struct glyph_row *first_text_row
16813 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16814
16815 *delta = Z - Z_old;
16816 *delta_bytes = Z_BYTE - Z_BYTE_old;
16817
16818 /* Set last_unchanged_pos to the buffer position of the last
16819 character in the buffer that has not been changed. Z is the
16820 index + 1 of the last character in current_buffer, i.e. by
16821 subtracting END_UNCHANGED we get the index of the last
16822 unchanged character, and we have to add BEG to get its buffer
16823 position. */
16824 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16825 last_unchanged_pos_old = last_unchanged_pos - *delta;
16826
16827 /* Search backward from ROW for a row displaying a line that
16828 starts at a minimum position >= last_unchanged_pos_old. */
16829 for (; row > first_text_row; --row)
16830 {
16831 /* This used to abort, but it can happen.
16832 It is ok to just stop the search instead here. KFS. */
16833 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16834 break;
16835
16836 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16837 row_found = row;
16838 }
16839 }
16840
16841 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16842
16843 return row_found;
16844 }
16845
16846
16847 /* Make sure that glyph rows in the current matrix of window W
16848 reference the same glyph memory as corresponding rows in the
16849 frame's frame matrix. This function is called after scrolling W's
16850 current matrix on a terminal frame in try_window_id and
16851 try_window_reusing_current_matrix. */
16852
16853 static void
16854 sync_frame_with_window_matrix_rows (struct window *w)
16855 {
16856 struct frame *f = XFRAME (w->frame);
16857 struct glyph_row *window_row, *window_row_end, *frame_row;
16858
16859 /* Preconditions: W must be a leaf window and full-width. Its frame
16860 must have a frame matrix. */
16861 xassert (NILP (w->hchild) && NILP (w->vchild));
16862 xassert (WINDOW_FULL_WIDTH_P (w));
16863 xassert (!FRAME_WINDOW_P (f));
16864
16865 /* If W is a full-width window, glyph pointers in W's current matrix
16866 have, by definition, to be the same as glyph pointers in the
16867 corresponding frame matrix. Note that frame matrices have no
16868 marginal areas (see build_frame_matrix). */
16869 window_row = w->current_matrix->rows;
16870 window_row_end = window_row + w->current_matrix->nrows;
16871 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16872 while (window_row < window_row_end)
16873 {
16874 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16875 struct glyph *end = window_row->glyphs[LAST_AREA];
16876
16877 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16878 frame_row->glyphs[TEXT_AREA] = start;
16879 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16880 frame_row->glyphs[LAST_AREA] = end;
16881
16882 /* Disable frame rows whose corresponding window rows have
16883 been disabled in try_window_id. */
16884 if (!window_row->enabled_p)
16885 frame_row->enabled_p = 0;
16886
16887 ++window_row, ++frame_row;
16888 }
16889 }
16890
16891
16892 /* Find the glyph row in window W containing CHARPOS. Consider all
16893 rows between START and END (not inclusive). END null means search
16894 all rows to the end of the display area of W. Value is the row
16895 containing CHARPOS or null. */
16896
16897 struct glyph_row *
16898 row_containing_pos (struct window *w, EMACS_INT charpos,
16899 struct glyph_row *start, struct glyph_row *end, int dy)
16900 {
16901 struct glyph_row *row = start;
16902 struct glyph_row *best_row = NULL;
16903 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16904 int last_y;
16905
16906 /* If we happen to start on a header-line, skip that. */
16907 if (row->mode_line_p)
16908 ++row;
16909
16910 if ((end && row >= end) || !row->enabled_p)
16911 return NULL;
16912
16913 last_y = window_text_bottom_y (w) - dy;
16914
16915 while (1)
16916 {
16917 /* Give up if we have gone too far. */
16918 if (end && row >= end)
16919 return NULL;
16920 /* This formerly returned if they were equal.
16921 I think that both quantities are of a "last plus one" type;
16922 if so, when they are equal, the row is within the screen. -- rms. */
16923 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16924 return NULL;
16925
16926 /* If it is in this row, return this row. */
16927 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16928 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16929 /* The end position of a row equals the start
16930 position of the next row. If CHARPOS is there, we
16931 would rather display it in the next line, except
16932 when this line ends in ZV. */
16933 && !row->ends_at_zv_p
16934 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16935 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16936 {
16937 struct glyph *g;
16938
16939 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16940 || (!best_row && !row->continued_p))
16941 return row;
16942 /* In bidi-reordered rows, there could be several rows
16943 occluding point, all of them belonging to the same
16944 continued line. We need to find the row which fits
16945 CHARPOS the best. */
16946 for (g = row->glyphs[TEXT_AREA];
16947 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16948 g++)
16949 {
16950 if (!STRINGP (g->object))
16951 {
16952 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16953 {
16954 mindif = eabs (g->charpos - charpos);
16955 best_row = row;
16956 /* Exact match always wins. */
16957 if (mindif == 0)
16958 return best_row;
16959 }
16960 }
16961 }
16962 }
16963 else if (best_row && !row->continued_p)
16964 return best_row;
16965 ++row;
16966 }
16967 }
16968
16969
16970 /* Try to redisplay window W by reusing its existing display. W's
16971 current matrix must be up to date when this function is called,
16972 i.e. window_end_valid must not be nil.
16973
16974 Value is
16975
16976 1 if display has been updated
16977 0 if otherwise unsuccessful
16978 -1 if redisplay with same window start is known not to succeed
16979
16980 The following steps are performed:
16981
16982 1. Find the last row in the current matrix of W that is not
16983 affected by changes at the start of current_buffer. If no such row
16984 is found, give up.
16985
16986 2. Find the first row in W's current matrix that is not affected by
16987 changes at the end of current_buffer. Maybe there is no such row.
16988
16989 3. Display lines beginning with the row + 1 found in step 1 to the
16990 row found in step 2 or, if step 2 didn't find a row, to the end of
16991 the window.
16992
16993 4. If cursor is not known to appear on the window, give up.
16994
16995 5. If display stopped at the row found in step 2, scroll the
16996 display and current matrix as needed.
16997
16998 6. Maybe display some lines at the end of W, if we must. This can
16999 happen under various circumstances, like a partially visible line
17000 becoming fully visible, or because newly displayed lines are displayed
17001 in smaller font sizes.
17002
17003 7. Update W's window end information. */
17004
17005 static int
17006 try_window_id (struct window *w)
17007 {
17008 struct frame *f = XFRAME (w->frame);
17009 struct glyph_matrix *current_matrix = w->current_matrix;
17010 struct glyph_matrix *desired_matrix = w->desired_matrix;
17011 struct glyph_row *last_unchanged_at_beg_row;
17012 struct glyph_row *first_unchanged_at_end_row;
17013 struct glyph_row *row;
17014 struct glyph_row *bottom_row;
17015 int bottom_vpos;
17016 struct it it;
17017 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
17018 int dvpos, dy;
17019 struct text_pos start_pos;
17020 struct run run;
17021 int first_unchanged_at_end_vpos = 0;
17022 struct glyph_row *last_text_row, *last_text_row_at_end;
17023 struct text_pos start;
17024 EMACS_INT first_changed_charpos, last_changed_charpos;
17025
17026 #if GLYPH_DEBUG
17027 if (inhibit_try_window_id)
17028 return 0;
17029 #endif
17030
17031 /* This is handy for debugging. */
17032 #if 0
17033 #define GIVE_UP(X) \
17034 do { \
17035 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17036 return 0; \
17037 } while (0)
17038 #else
17039 #define GIVE_UP(X) return 0
17040 #endif
17041
17042 SET_TEXT_POS_FROM_MARKER (start, w->start);
17043
17044 /* Don't use this for mini-windows because these can show
17045 messages and mini-buffers, and we don't handle that here. */
17046 if (MINI_WINDOW_P (w))
17047 GIVE_UP (1);
17048
17049 /* This flag is used to prevent redisplay optimizations. */
17050 if (windows_or_buffers_changed || cursor_type_changed)
17051 GIVE_UP (2);
17052
17053 /* Verify that narrowing has not changed.
17054 Also verify that we were not told to prevent redisplay optimizations.
17055 It would be nice to further
17056 reduce the number of cases where this prevents try_window_id. */
17057 if (current_buffer->clip_changed
17058 || current_buffer->prevent_redisplay_optimizations_p)
17059 GIVE_UP (3);
17060
17061 /* Window must either use window-based redisplay or be full width. */
17062 if (!FRAME_WINDOW_P (f)
17063 && (!FRAME_LINE_INS_DEL_OK (f)
17064 || !WINDOW_FULL_WIDTH_P (w)))
17065 GIVE_UP (4);
17066
17067 /* Give up if point is known NOT to appear in W. */
17068 if (PT < CHARPOS (start))
17069 GIVE_UP (5);
17070
17071 /* Another way to prevent redisplay optimizations. */
17072 if (XFASTINT (w->last_modified) == 0)
17073 GIVE_UP (6);
17074
17075 /* Verify that window is not hscrolled. */
17076 if (XFASTINT (w->hscroll) != 0)
17077 GIVE_UP (7);
17078
17079 /* Verify that display wasn't paused. */
17080 if (NILP (w->window_end_valid))
17081 GIVE_UP (8);
17082
17083 /* Can't use this if highlighting a region because a cursor movement
17084 will do more than just set the cursor. */
17085 if (!NILP (Vtransient_mark_mode)
17086 && !NILP (BVAR (current_buffer, mark_active)))
17087 GIVE_UP (9);
17088
17089 /* Likewise if highlighting trailing whitespace. */
17090 if (!NILP (Vshow_trailing_whitespace))
17091 GIVE_UP (11);
17092
17093 /* Likewise if showing a region. */
17094 if (!NILP (w->region_showing))
17095 GIVE_UP (10);
17096
17097 /* Can't use this if overlay arrow position and/or string have
17098 changed. */
17099 if (overlay_arrows_changed_p ())
17100 GIVE_UP (12);
17101
17102 /* When word-wrap is on, adding a space to the first word of a
17103 wrapped line can change the wrap position, altering the line
17104 above it. It might be worthwhile to handle this more
17105 intelligently, but for now just redisplay from scratch. */
17106 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17107 GIVE_UP (21);
17108
17109 /* Under bidi reordering, adding or deleting a character in the
17110 beginning of a paragraph, before the first strong directional
17111 character, can change the base direction of the paragraph (unless
17112 the buffer specifies a fixed paragraph direction), which will
17113 require to redisplay the whole paragraph. It might be worthwhile
17114 to find the paragraph limits and widen the range of redisplayed
17115 lines to that, but for now just give up this optimization and
17116 redisplay from scratch. */
17117 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17118 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17119 GIVE_UP (22);
17120
17121 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17122 only if buffer has really changed. The reason is that the gap is
17123 initially at Z for freshly visited files. The code below would
17124 set end_unchanged to 0 in that case. */
17125 if (MODIFF > SAVE_MODIFF
17126 /* This seems to happen sometimes after saving a buffer. */
17127 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17128 {
17129 if (GPT - BEG < BEG_UNCHANGED)
17130 BEG_UNCHANGED = GPT - BEG;
17131 if (Z - GPT < END_UNCHANGED)
17132 END_UNCHANGED = Z - GPT;
17133 }
17134
17135 /* The position of the first and last character that has been changed. */
17136 first_changed_charpos = BEG + BEG_UNCHANGED;
17137 last_changed_charpos = Z - END_UNCHANGED;
17138
17139 /* If window starts after a line end, and the last change is in
17140 front of that newline, then changes don't affect the display.
17141 This case happens with stealth-fontification. Note that although
17142 the display is unchanged, glyph positions in the matrix have to
17143 be adjusted, of course. */
17144 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17145 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17146 && ((last_changed_charpos < CHARPOS (start)
17147 && CHARPOS (start) == BEGV)
17148 || (last_changed_charpos < CHARPOS (start) - 1
17149 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17150 {
17151 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17152 struct glyph_row *r0;
17153
17154 /* Compute how many chars/bytes have been added to or removed
17155 from the buffer. */
17156 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17157 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17158 Z_delta = Z - Z_old;
17159 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17160
17161 /* Give up if PT is not in the window. Note that it already has
17162 been checked at the start of try_window_id that PT is not in
17163 front of the window start. */
17164 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17165 GIVE_UP (13);
17166
17167 /* If window start is unchanged, we can reuse the whole matrix
17168 as is, after adjusting glyph positions. No need to compute
17169 the window end again, since its offset from Z hasn't changed. */
17170 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17171 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17172 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17173 /* PT must not be in a partially visible line. */
17174 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17175 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17176 {
17177 /* Adjust positions in the glyph matrix. */
17178 if (Z_delta || Z_delta_bytes)
17179 {
17180 struct glyph_row *r1
17181 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17182 increment_matrix_positions (w->current_matrix,
17183 MATRIX_ROW_VPOS (r0, current_matrix),
17184 MATRIX_ROW_VPOS (r1, current_matrix),
17185 Z_delta, Z_delta_bytes);
17186 }
17187
17188 /* Set the cursor. */
17189 row = row_containing_pos (w, PT, r0, NULL, 0);
17190 if (row)
17191 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17192 else
17193 abort ();
17194 return 1;
17195 }
17196 }
17197
17198 /* Handle the case that changes are all below what is displayed in
17199 the window, and that PT is in the window. This shortcut cannot
17200 be taken if ZV is visible in the window, and text has been added
17201 there that is visible in the window. */
17202 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17203 /* ZV is not visible in the window, or there are no
17204 changes at ZV, actually. */
17205 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17206 || first_changed_charpos == last_changed_charpos))
17207 {
17208 struct glyph_row *r0;
17209
17210 /* Give up if PT is not in the window. Note that it already has
17211 been checked at the start of try_window_id that PT is not in
17212 front of the window start. */
17213 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17214 GIVE_UP (14);
17215
17216 /* If window start is unchanged, we can reuse the whole matrix
17217 as is, without changing glyph positions since no text has
17218 been added/removed in front of the window end. */
17219 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17220 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17221 /* PT must not be in a partially visible line. */
17222 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17223 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17224 {
17225 /* We have to compute the window end anew since text
17226 could have been added/removed after it. */
17227 w->window_end_pos
17228 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17229 w->window_end_bytepos
17230 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17231
17232 /* Set the cursor. */
17233 row = row_containing_pos (w, PT, r0, NULL, 0);
17234 if (row)
17235 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17236 else
17237 abort ();
17238 return 2;
17239 }
17240 }
17241
17242 /* Give up if window start is in the changed area.
17243
17244 The condition used to read
17245
17246 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17247
17248 but why that was tested escapes me at the moment. */
17249 if (CHARPOS (start) >= first_changed_charpos
17250 && CHARPOS (start) <= last_changed_charpos)
17251 GIVE_UP (15);
17252
17253 /* Check that window start agrees with the start of the first glyph
17254 row in its current matrix. Check this after we know the window
17255 start is not in changed text, otherwise positions would not be
17256 comparable. */
17257 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17258 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17259 GIVE_UP (16);
17260
17261 /* Give up if the window ends in strings. Overlay strings
17262 at the end are difficult to handle, so don't try. */
17263 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17264 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17265 GIVE_UP (20);
17266
17267 /* Compute the position at which we have to start displaying new
17268 lines. Some of the lines at the top of the window might be
17269 reusable because they are not displaying changed text. Find the
17270 last row in W's current matrix not affected by changes at the
17271 start of current_buffer. Value is null if changes start in the
17272 first line of window. */
17273 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17274 if (last_unchanged_at_beg_row)
17275 {
17276 /* Avoid starting to display in the middle of a character, a TAB
17277 for instance. This is easier than to set up the iterator
17278 exactly, and it's not a frequent case, so the additional
17279 effort wouldn't really pay off. */
17280 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17281 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17282 && last_unchanged_at_beg_row > w->current_matrix->rows)
17283 --last_unchanged_at_beg_row;
17284
17285 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17286 GIVE_UP (17);
17287
17288 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17289 GIVE_UP (18);
17290 start_pos = it.current.pos;
17291
17292 /* Start displaying new lines in the desired matrix at the same
17293 vpos we would use in the current matrix, i.e. below
17294 last_unchanged_at_beg_row. */
17295 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17296 current_matrix);
17297 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17298 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17299
17300 xassert (it.hpos == 0 && it.current_x == 0);
17301 }
17302 else
17303 {
17304 /* There are no reusable lines at the start of the window.
17305 Start displaying in the first text line. */
17306 start_display (&it, w, start);
17307 it.vpos = it.first_vpos;
17308 start_pos = it.current.pos;
17309 }
17310
17311 /* Find the first row that is not affected by changes at the end of
17312 the buffer. Value will be null if there is no unchanged row, in
17313 which case we must redisplay to the end of the window. delta
17314 will be set to the value by which buffer positions beginning with
17315 first_unchanged_at_end_row have to be adjusted due to text
17316 changes. */
17317 first_unchanged_at_end_row
17318 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17319 IF_DEBUG (debug_delta = delta);
17320 IF_DEBUG (debug_delta_bytes = delta_bytes);
17321
17322 /* Set stop_pos to the buffer position up to which we will have to
17323 display new lines. If first_unchanged_at_end_row != NULL, this
17324 is the buffer position of the start of the line displayed in that
17325 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17326 that we don't stop at a buffer position. */
17327 stop_pos = 0;
17328 if (first_unchanged_at_end_row)
17329 {
17330 xassert (last_unchanged_at_beg_row == NULL
17331 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17332
17333 /* If this is a continuation line, move forward to the next one
17334 that isn't. Changes in lines above affect this line.
17335 Caution: this may move first_unchanged_at_end_row to a row
17336 not displaying text. */
17337 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17338 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17339 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17340 < it.last_visible_y))
17341 ++first_unchanged_at_end_row;
17342
17343 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17344 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17345 >= it.last_visible_y))
17346 first_unchanged_at_end_row = NULL;
17347 else
17348 {
17349 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17350 + delta);
17351 first_unchanged_at_end_vpos
17352 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17353 xassert (stop_pos >= Z - END_UNCHANGED);
17354 }
17355 }
17356 else if (last_unchanged_at_beg_row == NULL)
17357 GIVE_UP (19);
17358
17359
17360 #if GLYPH_DEBUG
17361
17362 /* Either there is no unchanged row at the end, or the one we have
17363 now displays text. This is a necessary condition for the window
17364 end pos calculation at the end of this function. */
17365 xassert (first_unchanged_at_end_row == NULL
17366 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17367
17368 debug_last_unchanged_at_beg_vpos
17369 = (last_unchanged_at_beg_row
17370 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17371 : -1);
17372 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17373
17374 #endif /* GLYPH_DEBUG != 0 */
17375
17376
17377 /* Display new lines. Set last_text_row to the last new line
17378 displayed which has text on it, i.e. might end up as being the
17379 line where the window_end_vpos is. */
17380 w->cursor.vpos = -1;
17381 last_text_row = NULL;
17382 overlay_arrow_seen = 0;
17383 while (it.current_y < it.last_visible_y
17384 && !fonts_changed_p
17385 && (first_unchanged_at_end_row == NULL
17386 || IT_CHARPOS (it) < stop_pos))
17387 {
17388 if (display_line (&it))
17389 last_text_row = it.glyph_row - 1;
17390 }
17391
17392 if (fonts_changed_p)
17393 return -1;
17394
17395
17396 /* Compute differences in buffer positions, y-positions etc. for
17397 lines reused at the bottom of the window. Compute what we can
17398 scroll. */
17399 if (first_unchanged_at_end_row
17400 /* No lines reused because we displayed everything up to the
17401 bottom of the window. */
17402 && it.current_y < it.last_visible_y)
17403 {
17404 dvpos = (it.vpos
17405 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17406 current_matrix));
17407 dy = it.current_y - first_unchanged_at_end_row->y;
17408 run.current_y = first_unchanged_at_end_row->y;
17409 run.desired_y = run.current_y + dy;
17410 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17411 }
17412 else
17413 {
17414 delta = delta_bytes = dvpos = dy
17415 = run.current_y = run.desired_y = run.height = 0;
17416 first_unchanged_at_end_row = NULL;
17417 }
17418 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17419
17420
17421 /* Find the cursor if not already found. We have to decide whether
17422 PT will appear on this window (it sometimes doesn't, but this is
17423 not a very frequent case.) This decision has to be made before
17424 the current matrix is altered. A value of cursor.vpos < 0 means
17425 that PT is either in one of the lines beginning at
17426 first_unchanged_at_end_row or below the window. Don't care for
17427 lines that might be displayed later at the window end; as
17428 mentioned, this is not a frequent case. */
17429 if (w->cursor.vpos < 0)
17430 {
17431 /* Cursor in unchanged rows at the top? */
17432 if (PT < CHARPOS (start_pos)
17433 && last_unchanged_at_beg_row)
17434 {
17435 row = row_containing_pos (w, PT,
17436 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17437 last_unchanged_at_beg_row + 1, 0);
17438 if (row)
17439 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17440 }
17441
17442 /* Start from first_unchanged_at_end_row looking for PT. */
17443 else if (first_unchanged_at_end_row)
17444 {
17445 row = row_containing_pos (w, PT - delta,
17446 first_unchanged_at_end_row, NULL, 0);
17447 if (row)
17448 set_cursor_from_row (w, row, w->current_matrix, delta,
17449 delta_bytes, dy, dvpos);
17450 }
17451
17452 /* Give up if cursor was not found. */
17453 if (w->cursor.vpos < 0)
17454 {
17455 clear_glyph_matrix (w->desired_matrix);
17456 return -1;
17457 }
17458 }
17459
17460 /* Don't let the cursor end in the scroll margins. */
17461 {
17462 int this_scroll_margin, cursor_height;
17463
17464 this_scroll_margin =
17465 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17466 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17467 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17468
17469 if ((w->cursor.y < this_scroll_margin
17470 && CHARPOS (start) > BEGV)
17471 /* Old redisplay didn't take scroll margin into account at the bottom,
17472 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17473 || (w->cursor.y + (make_cursor_line_fully_visible_p
17474 ? cursor_height + this_scroll_margin
17475 : 1)) > it.last_visible_y)
17476 {
17477 w->cursor.vpos = -1;
17478 clear_glyph_matrix (w->desired_matrix);
17479 return -1;
17480 }
17481 }
17482
17483 /* Scroll the display. Do it before changing the current matrix so
17484 that xterm.c doesn't get confused about where the cursor glyph is
17485 found. */
17486 if (dy && run.height)
17487 {
17488 update_begin (f);
17489
17490 if (FRAME_WINDOW_P (f))
17491 {
17492 FRAME_RIF (f)->update_window_begin_hook (w);
17493 FRAME_RIF (f)->clear_window_mouse_face (w);
17494 FRAME_RIF (f)->scroll_run_hook (w, &run);
17495 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17496 }
17497 else
17498 {
17499 /* Terminal frame. In this case, dvpos gives the number of
17500 lines to scroll by; dvpos < 0 means scroll up. */
17501 int from_vpos
17502 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17503 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17504 int end = (WINDOW_TOP_EDGE_LINE (w)
17505 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17506 + window_internal_height (w));
17507
17508 #if defined (HAVE_GPM) || defined (MSDOS)
17509 x_clear_window_mouse_face (w);
17510 #endif
17511 /* Perform the operation on the screen. */
17512 if (dvpos > 0)
17513 {
17514 /* Scroll last_unchanged_at_beg_row to the end of the
17515 window down dvpos lines. */
17516 set_terminal_window (f, end);
17517
17518 /* On dumb terminals delete dvpos lines at the end
17519 before inserting dvpos empty lines. */
17520 if (!FRAME_SCROLL_REGION_OK (f))
17521 ins_del_lines (f, end - dvpos, -dvpos);
17522
17523 /* Insert dvpos empty lines in front of
17524 last_unchanged_at_beg_row. */
17525 ins_del_lines (f, from, dvpos);
17526 }
17527 else if (dvpos < 0)
17528 {
17529 /* Scroll up last_unchanged_at_beg_vpos to the end of
17530 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17531 set_terminal_window (f, end);
17532
17533 /* Delete dvpos lines in front of
17534 last_unchanged_at_beg_vpos. ins_del_lines will set
17535 the cursor to the given vpos and emit |dvpos| delete
17536 line sequences. */
17537 ins_del_lines (f, from + dvpos, dvpos);
17538
17539 /* On a dumb terminal insert dvpos empty lines at the
17540 end. */
17541 if (!FRAME_SCROLL_REGION_OK (f))
17542 ins_del_lines (f, end + dvpos, -dvpos);
17543 }
17544
17545 set_terminal_window (f, 0);
17546 }
17547
17548 update_end (f);
17549 }
17550
17551 /* Shift reused rows of the current matrix to the right position.
17552 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17553 text. */
17554 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17555 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17556 if (dvpos < 0)
17557 {
17558 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17559 bottom_vpos, dvpos);
17560 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17561 bottom_vpos, 0);
17562 }
17563 else if (dvpos > 0)
17564 {
17565 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17566 bottom_vpos, dvpos);
17567 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17568 first_unchanged_at_end_vpos + dvpos, 0);
17569 }
17570
17571 /* For frame-based redisplay, make sure that current frame and window
17572 matrix are in sync with respect to glyph memory. */
17573 if (!FRAME_WINDOW_P (f))
17574 sync_frame_with_window_matrix_rows (w);
17575
17576 /* Adjust buffer positions in reused rows. */
17577 if (delta || delta_bytes)
17578 increment_matrix_positions (current_matrix,
17579 first_unchanged_at_end_vpos + dvpos,
17580 bottom_vpos, delta, delta_bytes);
17581
17582 /* Adjust Y positions. */
17583 if (dy)
17584 shift_glyph_matrix (w, current_matrix,
17585 first_unchanged_at_end_vpos + dvpos,
17586 bottom_vpos, dy);
17587
17588 if (first_unchanged_at_end_row)
17589 {
17590 first_unchanged_at_end_row += dvpos;
17591 if (first_unchanged_at_end_row->y >= it.last_visible_y
17592 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17593 first_unchanged_at_end_row = NULL;
17594 }
17595
17596 /* If scrolling up, there may be some lines to display at the end of
17597 the window. */
17598 last_text_row_at_end = NULL;
17599 if (dy < 0)
17600 {
17601 /* Scrolling up can leave for example a partially visible line
17602 at the end of the window to be redisplayed. */
17603 /* Set last_row to the glyph row in the current matrix where the
17604 window end line is found. It has been moved up or down in
17605 the matrix by dvpos. */
17606 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17607 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17608
17609 /* If last_row is the window end line, it should display text. */
17610 xassert (last_row->displays_text_p);
17611
17612 /* If window end line was partially visible before, begin
17613 displaying at that line. Otherwise begin displaying with the
17614 line following it. */
17615 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17616 {
17617 init_to_row_start (&it, w, last_row);
17618 it.vpos = last_vpos;
17619 it.current_y = last_row->y;
17620 }
17621 else
17622 {
17623 init_to_row_end (&it, w, last_row);
17624 it.vpos = 1 + last_vpos;
17625 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17626 ++last_row;
17627 }
17628
17629 /* We may start in a continuation line. If so, we have to
17630 get the right continuation_lines_width and current_x. */
17631 it.continuation_lines_width = last_row->continuation_lines_width;
17632 it.hpos = it.current_x = 0;
17633
17634 /* Display the rest of the lines at the window end. */
17635 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17636 while (it.current_y < it.last_visible_y
17637 && !fonts_changed_p)
17638 {
17639 /* Is it always sure that the display agrees with lines in
17640 the current matrix? I don't think so, so we mark rows
17641 displayed invalid in the current matrix by setting their
17642 enabled_p flag to zero. */
17643 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17644 if (display_line (&it))
17645 last_text_row_at_end = it.glyph_row - 1;
17646 }
17647 }
17648
17649 /* Update window_end_pos and window_end_vpos. */
17650 if (first_unchanged_at_end_row
17651 && !last_text_row_at_end)
17652 {
17653 /* Window end line if one of the preserved rows from the current
17654 matrix. Set row to the last row displaying text in current
17655 matrix starting at first_unchanged_at_end_row, after
17656 scrolling. */
17657 xassert (first_unchanged_at_end_row->displays_text_p);
17658 row = find_last_row_displaying_text (w->current_matrix, &it,
17659 first_unchanged_at_end_row);
17660 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17661
17662 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17663 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17664 w->window_end_vpos
17665 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17666 xassert (w->window_end_bytepos >= 0);
17667 IF_DEBUG (debug_method_add (w, "A"));
17668 }
17669 else if (last_text_row_at_end)
17670 {
17671 w->window_end_pos
17672 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17673 w->window_end_bytepos
17674 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17675 w->window_end_vpos
17676 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17677 xassert (w->window_end_bytepos >= 0);
17678 IF_DEBUG (debug_method_add (w, "B"));
17679 }
17680 else if (last_text_row)
17681 {
17682 /* We have displayed either to the end of the window or at the
17683 end of the window, i.e. the last row with text is to be found
17684 in the desired matrix. */
17685 w->window_end_pos
17686 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17687 w->window_end_bytepos
17688 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17689 w->window_end_vpos
17690 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17691 xassert (w->window_end_bytepos >= 0);
17692 }
17693 else if (first_unchanged_at_end_row == NULL
17694 && last_text_row == NULL
17695 && last_text_row_at_end == NULL)
17696 {
17697 /* Displayed to end of window, but no line containing text was
17698 displayed. Lines were deleted at the end of the window. */
17699 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17700 int vpos = XFASTINT (w->window_end_vpos);
17701 struct glyph_row *current_row = current_matrix->rows + vpos;
17702 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17703
17704 for (row = NULL;
17705 row == NULL && vpos >= first_vpos;
17706 --vpos, --current_row, --desired_row)
17707 {
17708 if (desired_row->enabled_p)
17709 {
17710 if (desired_row->displays_text_p)
17711 row = desired_row;
17712 }
17713 else if (current_row->displays_text_p)
17714 row = current_row;
17715 }
17716
17717 xassert (row != NULL);
17718 w->window_end_vpos = make_number (vpos + 1);
17719 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17720 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17721 xassert (w->window_end_bytepos >= 0);
17722 IF_DEBUG (debug_method_add (w, "C"));
17723 }
17724 else
17725 abort ();
17726
17727 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17728 debug_end_vpos = XFASTINT (w->window_end_vpos));
17729
17730 /* Record that display has not been completed. */
17731 w->window_end_valid = Qnil;
17732 w->desired_matrix->no_scrolling_p = 1;
17733 return 3;
17734
17735 #undef GIVE_UP
17736 }
17737
17738
17739 \f
17740 /***********************************************************************
17741 More debugging support
17742 ***********************************************************************/
17743
17744 #if GLYPH_DEBUG
17745
17746 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17747 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17748 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17749
17750
17751 /* Dump the contents of glyph matrix MATRIX on stderr.
17752
17753 GLYPHS 0 means don't show glyph contents.
17754 GLYPHS 1 means show glyphs in short form
17755 GLYPHS > 1 means show glyphs in long form. */
17756
17757 void
17758 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17759 {
17760 int i;
17761 for (i = 0; i < matrix->nrows; ++i)
17762 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17763 }
17764
17765
17766 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17767 the glyph row and area where the glyph comes from. */
17768
17769 void
17770 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17771 {
17772 if (glyph->type == CHAR_GLYPH)
17773 {
17774 fprintf (stderr,
17775 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17776 glyph - row->glyphs[TEXT_AREA],
17777 'C',
17778 glyph->charpos,
17779 (BUFFERP (glyph->object)
17780 ? 'B'
17781 : (STRINGP (glyph->object)
17782 ? 'S'
17783 : '-')),
17784 glyph->pixel_width,
17785 glyph->u.ch,
17786 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17787 ? glyph->u.ch
17788 : '.'),
17789 glyph->face_id,
17790 glyph->left_box_line_p,
17791 glyph->right_box_line_p);
17792 }
17793 else if (glyph->type == STRETCH_GLYPH)
17794 {
17795 fprintf (stderr,
17796 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17797 glyph - row->glyphs[TEXT_AREA],
17798 'S',
17799 glyph->charpos,
17800 (BUFFERP (glyph->object)
17801 ? 'B'
17802 : (STRINGP (glyph->object)
17803 ? 'S'
17804 : '-')),
17805 glyph->pixel_width,
17806 0,
17807 '.',
17808 glyph->face_id,
17809 glyph->left_box_line_p,
17810 glyph->right_box_line_p);
17811 }
17812 else if (glyph->type == IMAGE_GLYPH)
17813 {
17814 fprintf (stderr,
17815 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17816 glyph - row->glyphs[TEXT_AREA],
17817 'I',
17818 glyph->charpos,
17819 (BUFFERP (glyph->object)
17820 ? 'B'
17821 : (STRINGP (glyph->object)
17822 ? 'S'
17823 : '-')),
17824 glyph->pixel_width,
17825 glyph->u.img_id,
17826 '.',
17827 glyph->face_id,
17828 glyph->left_box_line_p,
17829 glyph->right_box_line_p);
17830 }
17831 else if (glyph->type == COMPOSITE_GLYPH)
17832 {
17833 fprintf (stderr,
17834 " %5td %4c %6"pI"d %c %3d 0x%05x",
17835 glyph - row->glyphs[TEXT_AREA],
17836 '+',
17837 glyph->charpos,
17838 (BUFFERP (glyph->object)
17839 ? 'B'
17840 : (STRINGP (glyph->object)
17841 ? 'S'
17842 : '-')),
17843 glyph->pixel_width,
17844 glyph->u.cmp.id);
17845 if (glyph->u.cmp.automatic)
17846 fprintf (stderr,
17847 "[%d-%d]",
17848 glyph->slice.cmp.from, glyph->slice.cmp.to);
17849 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17850 glyph->face_id,
17851 glyph->left_box_line_p,
17852 glyph->right_box_line_p);
17853 }
17854 }
17855
17856
17857 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17858 GLYPHS 0 means don't show glyph contents.
17859 GLYPHS 1 means show glyphs in short form
17860 GLYPHS > 1 means show glyphs in long form. */
17861
17862 void
17863 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17864 {
17865 if (glyphs != 1)
17866 {
17867 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17868 fprintf (stderr, "======================================================================\n");
17869
17870 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17871 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17872 vpos,
17873 MATRIX_ROW_START_CHARPOS (row),
17874 MATRIX_ROW_END_CHARPOS (row),
17875 row->used[TEXT_AREA],
17876 row->contains_overlapping_glyphs_p,
17877 row->enabled_p,
17878 row->truncated_on_left_p,
17879 row->truncated_on_right_p,
17880 row->continued_p,
17881 MATRIX_ROW_CONTINUATION_LINE_P (row),
17882 row->displays_text_p,
17883 row->ends_at_zv_p,
17884 row->fill_line_p,
17885 row->ends_in_middle_of_char_p,
17886 row->starts_in_middle_of_char_p,
17887 row->mouse_face_p,
17888 row->x,
17889 row->y,
17890 row->pixel_width,
17891 row->height,
17892 row->visible_height,
17893 row->ascent,
17894 row->phys_ascent);
17895 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17896 row->end.overlay_string_index,
17897 row->continuation_lines_width);
17898 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17899 CHARPOS (row->start.string_pos),
17900 CHARPOS (row->end.string_pos));
17901 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17902 row->end.dpvec_index);
17903 }
17904
17905 if (glyphs > 1)
17906 {
17907 int area;
17908
17909 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17910 {
17911 struct glyph *glyph = row->glyphs[area];
17912 struct glyph *glyph_end = glyph + row->used[area];
17913
17914 /* Glyph for a line end in text. */
17915 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17916 ++glyph_end;
17917
17918 if (glyph < glyph_end)
17919 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17920
17921 for (; glyph < glyph_end; ++glyph)
17922 dump_glyph (row, glyph, area);
17923 }
17924 }
17925 else if (glyphs == 1)
17926 {
17927 int area;
17928
17929 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17930 {
17931 char *s = (char *) alloca (row->used[area] + 1);
17932 int i;
17933
17934 for (i = 0; i < row->used[area]; ++i)
17935 {
17936 struct glyph *glyph = row->glyphs[area] + i;
17937 if (glyph->type == CHAR_GLYPH
17938 && glyph->u.ch < 0x80
17939 && glyph->u.ch >= ' ')
17940 s[i] = glyph->u.ch;
17941 else
17942 s[i] = '.';
17943 }
17944
17945 s[i] = '\0';
17946 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17947 }
17948 }
17949 }
17950
17951
17952 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17953 Sdump_glyph_matrix, 0, 1, "p",
17954 doc: /* Dump the current matrix of the selected window to stderr.
17955 Shows contents of glyph row structures. With non-nil
17956 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17957 glyphs in short form, otherwise show glyphs in long form. */)
17958 (Lisp_Object glyphs)
17959 {
17960 struct window *w = XWINDOW (selected_window);
17961 struct buffer *buffer = XBUFFER (w->buffer);
17962
17963 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17964 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17965 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17966 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17967 fprintf (stderr, "=============================================\n");
17968 dump_glyph_matrix (w->current_matrix,
17969 NILP (glyphs) ? 0 : XINT (glyphs));
17970 return Qnil;
17971 }
17972
17973
17974 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17975 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17976 (void)
17977 {
17978 struct frame *f = XFRAME (selected_frame);
17979 dump_glyph_matrix (f->current_matrix, 1);
17980 return Qnil;
17981 }
17982
17983
17984 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17985 doc: /* Dump glyph row ROW to stderr.
17986 GLYPH 0 means don't dump glyphs.
17987 GLYPH 1 means dump glyphs in short form.
17988 GLYPH > 1 or omitted means dump glyphs in long form. */)
17989 (Lisp_Object row, Lisp_Object glyphs)
17990 {
17991 struct glyph_matrix *matrix;
17992 int vpos;
17993
17994 CHECK_NUMBER (row);
17995 matrix = XWINDOW (selected_window)->current_matrix;
17996 vpos = XINT (row);
17997 if (vpos >= 0 && vpos < matrix->nrows)
17998 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17999 vpos,
18000 INTEGERP (glyphs) ? XINT (glyphs) : 2);
18001 return Qnil;
18002 }
18003
18004
18005 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18006 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18007 GLYPH 0 means don't dump glyphs.
18008 GLYPH 1 means dump glyphs in short form.
18009 GLYPH > 1 or omitted means dump glyphs in long form. */)
18010 (Lisp_Object row, Lisp_Object glyphs)
18011 {
18012 struct frame *sf = SELECTED_FRAME ();
18013 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18014 int vpos;
18015
18016 CHECK_NUMBER (row);
18017 vpos = XINT (row);
18018 if (vpos >= 0 && vpos < m->nrows)
18019 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18020 INTEGERP (glyphs) ? XINT (glyphs) : 2);
18021 return Qnil;
18022 }
18023
18024
18025 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18026 doc: /* Toggle tracing of redisplay.
18027 With ARG, turn tracing on if and only if ARG is positive. */)
18028 (Lisp_Object arg)
18029 {
18030 if (NILP (arg))
18031 trace_redisplay_p = !trace_redisplay_p;
18032 else
18033 {
18034 arg = Fprefix_numeric_value (arg);
18035 trace_redisplay_p = XINT (arg) > 0;
18036 }
18037
18038 return Qnil;
18039 }
18040
18041
18042 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18043 doc: /* Like `format', but print result to stderr.
18044 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18045 (ptrdiff_t nargs, Lisp_Object *args)
18046 {
18047 Lisp_Object s = Fformat (nargs, args);
18048 fprintf (stderr, "%s", SDATA (s));
18049 return Qnil;
18050 }
18051
18052 #endif /* GLYPH_DEBUG */
18053
18054
18055 \f
18056 /***********************************************************************
18057 Building Desired Matrix Rows
18058 ***********************************************************************/
18059
18060 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18061 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18062
18063 static struct glyph_row *
18064 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18065 {
18066 struct frame *f = XFRAME (WINDOW_FRAME (w));
18067 struct buffer *buffer = XBUFFER (w->buffer);
18068 struct buffer *old = current_buffer;
18069 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18070 int arrow_len = SCHARS (overlay_arrow_string);
18071 const unsigned char *arrow_end = arrow_string + arrow_len;
18072 const unsigned char *p;
18073 struct it it;
18074 int multibyte_p;
18075 int n_glyphs_before;
18076
18077 set_buffer_temp (buffer);
18078 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18079 it.glyph_row->used[TEXT_AREA] = 0;
18080 SET_TEXT_POS (it.position, 0, 0);
18081
18082 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18083 p = arrow_string;
18084 while (p < arrow_end)
18085 {
18086 Lisp_Object face, ilisp;
18087
18088 /* Get the next character. */
18089 if (multibyte_p)
18090 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18091 else
18092 {
18093 it.c = it.char_to_display = *p, it.len = 1;
18094 if (! ASCII_CHAR_P (it.c))
18095 it.char_to_display = BYTE8_TO_CHAR (it.c);
18096 }
18097 p += it.len;
18098
18099 /* Get its face. */
18100 ilisp = make_number (p - arrow_string);
18101 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18102 it.face_id = compute_char_face (f, it.char_to_display, face);
18103
18104 /* Compute its width, get its glyphs. */
18105 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18106 SET_TEXT_POS (it.position, -1, -1);
18107 PRODUCE_GLYPHS (&it);
18108
18109 /* If this character doesn't fit any more in the line, we have
18110 to remove some glyphs. */
18111 if (it.current_x > it.last_visible_x)
18112 {
18113 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18114 break;
18115 }
18116 }
18117
18118 set_buffer_temp (old);
18119 return it.glyph_row;
18120 }
18121
18122
18123 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18124 glyphs are only inserted for terminal frames since we can't really
18125 win with truncation glyphs when partially visible glyphs are
18126 involved. Which glyphs to insert is determined by
18127 produce_special_glyphs. */
18128
18129 static void
18130 insert_left_trunc_glyphs (struct it *it)
18131 {
18132 struct it truncate_it;
18133 struct glyph *from, *end, *to, *toend;
18134
18135 xassert (!FRAME_WINDOW_P (it->f));
18136
18137 /* Get the truncation glyphs. */
18138 truncate_it = *it;
18139 truncate_it.current_x = 0;
18140 truncate_it.face_id = DEFAULT_FACE_ID;
18141 truncate_it.glyph_row = &scratch_glyph_row;
18142 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18143 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18144 truncate_it.object = make_number (0);
18145 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18146
18147 /* Overwrite glyphs from IT with truncation glyphs. */
18148 if (!it->glyph_row->reversed_p)
18149 {
18150 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18151 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18152 to = it->glyph_row->glyphs[TEXT_AREA];
18153 toend = to + it->glyph_row->used[TEXT_AREA];
18154
18155 while (from < end)
18156 *to++ = *from++;
18157
18158 /* There may be padding glyphs left over. Overwrite them too. */
18159 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18160 {
18161 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18162 while (from < end)
18163 *to++ = *from++;
18164 }
18165
18166 if (to > toend)
18167 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18168 }
18169 else
18170 {
18171 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18172 that back to front. */
18173 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18174 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18175 toend = it->glyph_row->glyphs[TEXT_AREA];
18176 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18177
18178 while (from >= end && to >= toend)
18179 *to-- = *from--;
18180 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18181 {
18182 from =
18183 truncate_it.glyph_row->glyphs[TEXT_AREA]
18184 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18185 while (from >= end && to >= toend)
18186 *to-- = *from--;
18187 }
18188 if (from >= end)
18189 {
18190 /* Need to free some room before prepending additional
18191 glyphs. */
18192 int move_by = from - end + 1;
18193 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18194 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18195
18196 for ( ; g >= g0; g--)
18197 g[move_by] = *g;
18198 while (from >= end)
18199 *to-- = *from--;
18200 it->glyph_row->used[TEXT_AREA] += move_by;
18201 }
18202 }
18203 }
18204
18205 /* Compute the hash code for ROW. */
18206 unsigned
18207 row_hash (struct glyph_row *row)
18208 {
18209 int area, k;
18210 unsigned hashval = 0;
18211
18212 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18213 for (k = 0; k < row->used[area]; ++k)
18214 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18215 + row->glyphs[area][k].u.val
18216 + row->glyphs[area][k].face_id
18217 + row->glyphs[area][k].padding_p
18218 + (row->glyphs[area][k].type << 2));
18219
18220 return hashval;
18221 }
18222
18223 /* Compute the pixel height and width of IT->glyph_row.
18224
18225 Most of the time, ascent and height of a display line will be equal
18226 to the max_ascent and max_height values of the display iterator
18227 structure. This is not the case if
18228
18229 1. We hit ZV without displaying anything. In this case, max_ascent
18230 and max_height will be zero.
18231
18232 2. We have some glyphs that don't contribute to the line height.
18233 (The glyph row flag contributes_to_line_height_p is for future
18234 pixmap extensions).
18235
18236 The first case is easily covered by using default values because in
18237 these cases, the line height does not really matter, except that it
18238 must not be zero. */
18239
18240 static void
18241 compute_line_metrics (struct it *it)
18242 {
18243 struct glyph_row *row = it->glyph_row;
18244
18245 if (FRAME_WINDOW_P (it->f))
18246 {
18247 int i, min_y, max_y;
18248
18249 /* The line may consist of one space only, that was added to
18250 place the cursor on it. If so, the row's height hasn't been
18251 computed yet. */
18252 if (row->height == 0)
18253 {
18254 if (it->max_ascent + it->max_descent == 0)
18255 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18256 row->ascent = it->max_ascent;
18257 row->height = it->max_ascent + it->max_descent;
18258 row->phys_ascent = it->max_phys_ascent;
18259 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18260 row->extra_line_spacing = it->max_extra_line_spacing;
18261 }
18262
18263 /* Compute the width of this line. */
18264 row->pixel_width = row->x;
18265 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18266 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18267
18268 xassert (row->pixel_width >= 0);
18269 xassert (row->ascent >= 0 && row->height > 0);
18270
18271 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18272 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18273
18274 /* If first line's physical ascent is larger than its logical
18275 ascent, use the physical ascent, and make the row taller.
18276 This makes accented characters fully visible. */
18277 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18278 && row->phys_ascent > row->ascent)
18279 {
18280 row->height += row->phys_ascent - row->ascent;
18281 row->ascent = row->phys_ascent;
18282 }
18283
18284 /* Compute how much of the line is visible. */
18285 row->visible_height = row->height;
18286
18287 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18288 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18289
18290 if (row->y < min_y)
18291 row->visible_height -= min_y - row->y;
18292 if (row->y + row->height > max_y)
18293 row->visible_height -= row->y + row->height - max_y;
18294 }
18295 else
18296 {
18297 row->pixel_width = row->used[TEXT_AREA];
18298 if (row->continued_p)
18299 row->pixel_width -= it->continuation_pixel_width;
18300 else if (row->truncated_on_right_p)
18301 row->pixel_width -= it->truncation_pixel_width;
18302 row->ascent = row->phys_ascent = 0;
18303 row->height = row->phys_height = row->visible_height = 1;
18304 row->extra_line_spacing = 0;
18305 }
18306
18307 /* Compute a hash code for this row. */
18308 row->hash = row_hash (row);
18309
18310 it->max_ascent = it->max_descent = 0;
18311 it->max_phys_ascent = it->max_phys_descent = 0;
18312 }
18313
18314
18315 /* Append one space to the glyph row of iterator IT if doing a
18316 window-based redisplay. The space has the same face as
18317 IT->face_id. Value is non-zero if a space was added.
18318
18319 This function is called to make sure that there is always one glyph
18320 at the end of a glyph row that the cursor can be set on under
18321 window-systems. (If there weren't such a glyph we would not know
18322 how wide and tall a box cursor should be displayed).
18323
18324 At the same time this space let's a nicely handle clearing to the
18325 end of the line if the row ends in italic text. */
18326
18327 static int
18328 append_space_for_newline (struct it *it, int default_face_p)
18329 {
18330 if (FRAME_WINDOW_P (it->f))
18331 {
18332 int n = it->glyph_row->used[TEXT_AREA];
18333
18334 if (it->glyph_row->glyphs[TEXT_AREA] + n
18335 < it->glyph_row->glyphs[1 + TEXT_AREA])
18336 {
18337 /* Save some values that must not be changed.
18338 Must save IT->c and IT->len because otherwise
18339 ITERATOR_AT_END_P wouldn't work anymore after
18340 append_space_for_newline has been called. */
18341 enum display_element_type saved_what = it->what;
18342 int saved_c = it->c, saved_len = it->len;
18343 int saved_char_to_display = it->char_to_display;
18344 int saved_x = it->current_x;
18345 int saved_face_id = it->face_id;
18346 struct text_pos saved_pos;
18347 Lisp_Object saved_object;
18348 struct face *face;
18349
18350 saved_object = it->object;
18351 saved_pos = it->position;
18352
18353 it->what = IT_CHARACTER;
18354 memset (&it->position, 0, sizeof it->position);
18355 it->object = make_number (0);
18356 it->c = it->char_to_display = ' ';
18357 it->len = 1;
18358
18359 /* If the default face was remapped, be sure to use the
18360 remapped face for the appended newline. */
18361 if (default_face_p)
18362 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18363 else if (it->face_before_selective_p)
18364 it->face_id = it->saved_face_id;
18365 face = FACE_FROM_ID (it->f, it->face_id);
18366 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18367
18368 PRODUCE_GLYPHS (it);
18369
18370 it->override_ascent = -1;
18371 it->constrain_row_ascent_descent_p = 0;
18372 it->current_x = saved_x;
18373 it->object = saved_object;
18374 it->position = saved_pos;
18375 it->what = saved_what;
18376 it->face_id = saved_face_id;
18377 it->len = saved_len;
18378 it->c = saved_c;
18379 it->char_to_display = saved_char_to_display;
18380 return 1;
18381 }
18382 }
18383
18384 return 0;
18385 }
18386
18387
18388 /* Extend the face of the last glyph in the text area of IT->glyph_row
18389 to the end of the display line. Called from display_line. If the
18390 glyph row is empty, add a space glyph to it so that we know the
18391 face to draw. Set the glyph row flag fill_line_p. If the glyph
18392 row is R2L, prepend a stretch glyph to cover the empty space to the
18393 left of the leftmost glyph. */
18394
18395 static void
18396 extend_face_to_end_of_line (struct it *it)
18397 {
18398 struct face *face, *default_face;
18399 struct frame *f = it->f;
18400
18401 /* If line is already filled, do nothing. Non window-system frames
18402 get a grace of one more ``pixel'' because their characters are
18403 1-``pixel'' wide, so they hit the equality too early. This grace
18404 is needed only for R2L rows that are not continued, to produce
18405 one extra blank where we could display the cursor. */
18406 if (it->current_x >= it->last_visible_x
18407 + (!FRAME_WINDOW_P (f)
18408 && it->glyph_row->reversed_p
18409 && !it->glyph_row->continued_p))
18410 return;
18411
18412 /* The default face, possibly remapped. */
18413 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18414
18415 /* Face extension extends the background and box of IT->face_id
18416 to the end of the line. If the background equals the background
18417 of the frame, we don't have to do anything. */
18418 if (it->face_before_selective_p)
18419 face = FACE_FROM_ID (f, it->saved_face_id);
18420 else
18421 face = FACE_FROM_ID (f, it->face_id);
18422
18423 if (FRAME_WINDOW_P (f)
18424 && it->glyph_row->displays_text_p
18425 && face->box == FACE_NO_BOX
18426 && face->background == FRAME_BACKGROUND_PIXEL (f)
18427 && !face->stipple
18428 && !it->glyph_row->reversed_p)
18429 return;
18430
18431 /* Set the glyph row flag indicating that the face of the last glyph
18432 in the text area has to be drawn to the end of the text area. */
18433 it->glyph_row->fill_line_p = 1;
18434
18435 /* If current character of IT is not ASCII, make sure we have the
18436 ASCII face. This will be automatically undone the next time
18437 get_next_display_element returns a multibyte character. Note
18438 that the character will always be single byte in unibyte
18439 text. */
18440 if (!ASCII_CHAR_P (it->c))
18441 {
18442 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18443 }
18444
18445 if (FRAME_WINDOW_P (f))
18446 {
18447 /* If the row is empty, add a space with the current face of IT,
18448 so that we know which face to draw. */
18449 if (it->glyph_row->used[TEXT_AREA] == 0)
18450 {
18451 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18452 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18453 it->glyph_row->used[TEXT_AREA] = 1;
18454 }
18455 #ifdef HAVE_WINDOW_SYSTEM
18456 if (it->glyph_row->reversed_p)
18457 {
18458 /* Prepend a stretch glyph to the row, such that the
18459 rightmost glyph will be drawn flushed all the way to the
18460 right margin of the window. The stretch glyph that will
18461 occupy the empty space, if any, to the left of the
18462 glyphs. */
18463 struct font *font = face->font ? face->font : FRAME_FONT (f);
18464 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18465 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18466 struct glyph *g;
18467 int row_width, stretch_ascent, stretch_width;
18468 struct text_pos saved_pos;
18469 int saved_face_id, saved_avoid_cursor;
18470
18471 for (row_width = 0, g = row_start; g < row_end; g++)
18472 row_width += g->pixel_width;
18473 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18474 if (stretch_width > 0)
18475 {
18476 stretch_ascent =
18477 (((it->ascent + it->descent)
18478 * FONT_BASE (font)) / FONT_HEIGHT (font));
18479 saved_pos = it->position;
18480 memset (&it->position, 0, sizeof it->position);
18481 saved_avoid_cursor = it->avoid_cursor_p;
18482 it->avoid_cursor_p = 1;
18483 saved_face_id = it->face_id;
18484 /* The last row's stretch glyph should get the default
18485 face, to avoid painting the rest of the window with
18486 the region face, if the region ends at ZV. */
18487 if (it->glyph_row->ends_at_zv_p)
18488 it->face_id = default_face->id;
18489 else
18490 it->face_id = face->id;
18491 append_stretch_glyph (it, make_number (0), stretch_width,
18492 it->ascent + it->descent, stretch_ascent);
18493 it->position = saved_pos;
18494 it->avoid_cursor_p = saved_avoid_cursor;
18495 it->face_id = saved_face_id;
18496 }
18497 }
18498 #endif /* HAVE_WINDOW_SYSTEM */
18499 }
18500 else
18501 {
18502 /* Save some values that must not be changed. */
18503 int saved_x = it->current_x;
18504 struct text_pos saved_pos;
18505 Lisp_Object saved_object;
18506 enum display_element_type saved_what = it->what;
18507 int saved_face_id = it->face_id;
18508
18509 saved_object = it->object;
18510 saved_pos = it->position;
18511
18512 it->what = IT_CHARACTER;
18513 memset (&it->position, 0, sizeof it->position);
18514 it->object = make_number (0);
18515 it->c = it->char_to_display = ' ';
18516 it->len = 1;
18517 /* The last row's blank glyphs should get the default face, to
18518 avoid painting the rest of the window with the region face,
18519 if the region ends at ZV. */
18520 if (it->glyph_row->ends_at_zv_p)
18521 it->face_id = default_face->id;
18522 else
18523 it->face_id = face->id;
18524
18525 PRODUCE_GLYPHS (it);
18526
18527 while (it->current_x <= it->last_visible_x)
18528 PRODUCE_GLYPHS (it);
18529
18530 /* Don't count these blanks really. It would let us insert a left
18531 truncation glyph below and make us set the cursor on them, maybe. */
18532 it->current_x = saved_x;
18533 it->object = saved_object;
18534 it->position = saved_pos;
18535 it->what = saved_what;
18536 it->face_id = saved_face_id;
18537 }
18538 }
18539
18540
18541 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18542 trailing whitespace. */
18543
18544 static int
18545 trailing_whitespace_p (EMACS_INT charpos)
18546 {
18547 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18548 int c = 0;
18549
18550 while (bytepos < ZV_BYTE
18551 && (c = FETCH_CHAR (bytepos),
18552 c == ' ' || c == '\t'))
18553 ++bytepos;
18554
18555 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18556 {
18557 if (bytepos != PT_BYTE)
18558 return 1;
18559 }
18560 return 0;
18561 }
18562
18563
18564 /* Highlight trailing whitespace, if any, in ROW. */
18565
18566 static void
18567 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18568 {
18569 int used = row->used[TEXT_AREA];
18570
18571 if (used)
18572 {
18573 struct glyph *start = row->glyphs[TEXT_AREA];
18574 struct glyph *glyph = start + used - 1;
18575
18576 if (row->reversed_p)
18577 {
18578 /* Right-to-left rows need to be processed in the opposite
18579 direction, so swap the edge pointers. */
18580 glyph = start;
18581 start = row->glyphs[TEXT_AREA] + used - 1;
18582 }
18583
18584 /* Skip over glyphs inserted to display the cursor at the
18585 end of a line, for extending the face of the last glyph
18586 to the end of the line on terminals, and for truncation
18587 and continuation glyphs. */
18588 if (!row->reversed_p)
18589 {
18590 while (glyph >= start
18591 && glyph->type == CHAR_GLYPH
18592 && INTEGERP (glyph->object))
18593 --glyph;
18594 }
18595 else
18596 {
18597 while (glyph <= start
18598 && glyph->type == CHAR_GLYPH
18599 && INTEGERP (glyph->object))
18600 ++glyph;
18601 }
18602
18603 /* If last glyph is a space or stretch, and it's trailing
18604 whitespace, set the face of all trailing whitespace glyphs in
18605 IT->glyph_row to `trailing-whitespace'. */
18606 if ((row->reversed_p ? glyph <= start : glyph >= start)
18607 && BUFFERP (glyph->object)
18608 && (glyph->type == STRETCH_GLYPH
18609 || (glyph->type == CHAR_GLYPH
18610 && glyph->u.ch == ' '))
18611 && trailing_whitespace_p (glyph->charpos))
18612 {
18613 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18614 if (face_id < 0)
18615 return;
18616
18617 if (!row->reversed_p)
18618 {
18619 while (glyph >= start
18620 && BUFFERP (glyph->object)
18621 && (glyph->type == STRETCH_GLYPH
18622 || (glyph->type == CHAR_GLYPH
18623 && glyph->u.ch == ' ')))
18624 (glyph--)->face_id = face_id;
18625 }
18626 else
18627 {
18628 while (glyph <= start
18629 && BUFFERP (glyph->object)
18630 && (glyph->type == STRETCH_GLYPH
18631 || (glyph->type == CHAR_GLYPH
18632 && glyph->u.ch == ' ')))
18633 (glyph++)->face_id = face_id;
18634 }
18635 }
18636 }
18637 }
18638
18639
18640 /* Value is non-zero if glyph row ROW should be
18641 used to hold the cursor. */
18642
18643 static int
18644 cursor_row_p (struct glyph_row *row)
18645 {
18646 int result = 1;
18647
18648 if (PT == CHARPOS (row->end.pos)
18649 || PT == MATRIX_ROW_END_CHARPOS (row))
18650 {
18651 /* Suppose the row ends on a string.
18652 Unless the row is continued, that means it ends on a newline
18653 in the string. If it's anything other than a display string
18654 (e.g., a before-string from an overlay), we don't want the
18655 cursor there. (This heuristic seems to give the optimal
18656 behavior for the various types of multi-line strings.)
18657 One exception: if the string has `cursor' property on one of
18658 its characters, we _do_ want the cursor there. */
18659 if (CHARPOS (row->end.string_pos) >= 0)
18660 {
18661 if (row->continued_p)
18662 result = 1;
18663 else
18664 {
18665 /* Check for `display' property. */
18666 struct glyph *beg = row->glyphs[TEXT_AREA];
18667 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18668 struct glyph *glyph;
18669
18670 result = 0;
18671 for (glyph = end; glyph >= beg; --glyph)
18672 if (STRINGP (glyph->object))
18673 {
18674 Lisp_Object prop
18675 = Fget_char_property (make_number (PT),
18676 Qdisplay, Qnil);
18677 result =
18678 (!NILP (prop)
18679 && display_prop_string_p (prop, glyph->object));
18680 /* If there's a `cursor' property on one of the
18681 string's characters, this row is a cursor row,
18682 even though this is not a display string. */
18683 if (!result)
18684 {
18685 Lisp_Object s = glyph->object;
18686
18687 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18688 {
18689 EMACS_INT gpos = glyph->charpos;
18690
18691 if (!NILP (Fget_char_property (make_number (gpos),
18692 Qcursor, s)))
18693 {
18694 result = 1;
18695 break;
18696 }
18697 }
18698 }
18699 break;
18700 }
18701 }
18702 }
18703 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18704 {
18705 /* If the row ends in middle of a real character,
18706 and the line is continued, we want the cursor here.
18707 That's because CHARPOS (ROW->end.pos) would equal
18708 PT if PT is before the character. */
18709 if (!row->ends_in_ellipsis_p)
18710 result = row->continued_p;
18711 else
18712 /* If the row ends in an ellipsis, then
18713 CHARPOS (ROW->end.pos) will equal point after the
18714 invisible text. We want that position to be displayed
18715 after the ellipsis. */
18716 result = 0;
18717 }
18718 /* If the row ends at ZV, display the cursor at the end of that
18719 row instead of at the start of the row below. */
18720 else if (row->ends_at_zv_p)
18721 result = 1;
18722 else
18723 result = 0;
18724 }
18725
18726 return result;
18727 }
18728
18729 \f
18730
18731 /* Push the property PROP so that it will be rendered at the current
18732 position in IT. Return 1 if PROP was successfully pushed, 0
18733 otherwise. Called from handle_line_prefix to handle the
18734 `line-prefix' and `wrap-prefix' properties. */
18735
18736 static int
18737 push_prefix_prop (struct it *it, Lisp_Object prop)
18738 {
18739 struct text_pos pos =
18740 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18741
18742 xassert (it->method == GET_FROM_BUFFER
18743 || it->method == GET_FROM_DISPLAY_VECTOR
18744 || it->method == GET_FROM_STRING);
18745
18746 /* We need to save the current buffer/string position, so it will be
18747 restored by pop_it, because iterate_out_of_display_property
18748 depends on that being set correctly, but some situations leave
18749 it->position not yet set when this function is called. */
18750 push_it (it, &pos);
18751
18752 if (STRINGP (prop))
18753 {
18754 if (SCHARS (prop) == 0)
18755 {
18756 pop_it (it);
18757 return 0;
18758 }
18759
18760 it->string = prop;
18761 it->string_from_prefix_prop_p = 1;
18762 it->multibyte_p = STRING_MULTIBYTE (it->string);
18763 it->current.overlay_string_index = -1;
18764 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18765 it->end_charpos = it->string_nchars = SCHARS (it->string);
18766 it->method = GET_FROM_STRING;
18767 it->stop_charpos = 0;
18768 it->prev_stop = 0;
18769 it->base_level_stop = 0;
18770
18771 /* Force paragraph direction to be that of the parent
18772 buffer/string. */
18773 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18774 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18775 else
18776 it->paragraph_embedding = L2R;
18777
18778 /* Set up the bidi iterator for this display string. */
18779 if (it->bidi_p)
18780 {
18781 it->bidi_it.string.lstring = it->string;
18782 it->bidi_it.string.s = NULL;
18783 it->bidi_it.string.schars = it->end_charpos;
18784 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18785 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18786 it->bidi_it.string.unibyte = !it->multibyte_p;
18787 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18788 }
18789 }
18790 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18791 {
18792 it->method = GET_FROM_STRETCH;
18793 it->object = prop;
18794 }
18795 #ifdef HAVE_WINDOW_SYSTEM
18796 else if (IMAGEP (prop))
18797 {
18798 it->what = IT_IMAGE;
18799 it->image_id = lookup_image (it->f, prop);
18800 it->method = GET_FROM_IMAGE;
18801 }
18802 #endif /* HAVE_WINDOW_SYSTEM */
18803 else
18804 {
18805 pop_it (it); /* bogus display property, give up */
18806 return 0;
18807 }
18808
18809 return 1;
18810 }
18811
18812 /* Return the character-property PROP at the current position in IT. */
18813
18814 static Lisp_Object
18815 get_it_property (struct it *it, Lisp_Object prop)
18816 {
18817 Lisp_Object position;
18818
18819 if (STRINGP (it->object))
18820 position = make_number (IT_STRING_CHARPOS (*it));
18821 else if (BUFFERP (it->object))
18822 position = make_number (IT_CHARPOS (*it));
18823 else
18824 return Qnil;
18825
18826 return Fget_char_property (position, prop, it->object);
18827 }
18828
18829 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18830
18831 static void
18832 handle_line_prefix (struct it *it)
18833 {
18834 Lisp_Object prefix;
18835
18836 if (it->continuation_lines_width > 0)
18837 {
18838 prefix = get_it_property (it, Qwrap_prefix);
18839 if (NILP (prefix))
18840 prefix = Vwrap_prefix;
18841 }
18842 else
18843 {
18844 prefix = get_it_property (it, Qline_prefix);
18845 if (NILP (prefix))
18846 prefix = Vline_prefix;
18847 }
18848 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18849 {
18850 /* If the prefix is wider than the window, and we try to wrap
18851 it, it would acquire its own wrap prefix, and so on till the
18852 iterator stack overflows. So, don't wrap the prefix. */
18853 it->line_wrap = TRUNCATE;
18854 it->avoid_cursor_p = 1;
18855 }
18856 }
18857
18858 \f
18859
18860 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18861 only for R2L lines from display_line and display_string, when they
18862 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18863 the line/string needs to be continued on the next glyph row. */
18864 static void
18865 unproduce_glyphs (struct it *it, int n)
18866 {
18867 struct glyph *glyph, *end;
18868
18869 xassert (it->glyph_row);
18870 xassert (it->glyph_row->reversed_p);
18871 xassert (it->area == TEXT_AREA);
18872 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18873
18874 if (n > it->glyph_row->used[TEXT_AREA])
18875 n = it->glyph_row->used[TEXT_AREA];
18876 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18877 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18878 for ( ; glyph < end; glyph++)
18879 glyph[-n] = *glyph;
18880 }
18881
18882 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18883 and ROW->maxpos. */
18884 static void
18885 find_row_edges (struct it *it, struct glyph_row *row,
18886 EMACS_INT min_pos, EMACS_INT min_bpos,
18887 EMACS_INT max_pos, EMACS_INT max_bpos)
18888 {
18889 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18890 lines' rows is implemented for bidi-reordered rows. */
18891
18892 /* ROW->minpos is the value of min_pos, the minimal buffer position
18893 we have in ROW, or ROW->start.pos if that is smaller. */
18894 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18895 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18896 else
18897 /* We didn't find buffer positions smaller than ROW->start, or
18898 didn't find _any_ valid buffer positions in any of the glyphs,
18899 so we must trust the iterator's computed positions. */
18900 row->minpos = row->start.pos;
18901 if (max_pos <= 0)
18902 {
18903 max_pos = CHARPOS (it->current.pos);
18904 max_bpos = BYTEPOS (it->current.pos);
18905 }
18906
18907 /* Here are the various use-cases for ending the row, and the
18908 corresponding values for ROW->maxpos:
18909
18910 Line ends in a newline from buffer eol_pos + 1
18911 Line is continued from buffer max_pos + 1
18912 Line is truncated on right it->current.pos
18913 Line ends in a newline from string max_pos + 1(*)
18914 (*) + 1 only when line ends in a forward scan
18915 Line is continued from string max_pos
18916 Line is continued from display vector max_pos
18917 Line is entirely from a string min_pos == max_pos
18918 Line is entirely from a display vector min_pos == max_pos
18919 Line that ends at ZV ZV
18920
18921 If you discover other use-cases, please add them here as
18922 appropriate. */
18923 if (row->ends_at_zv_p)
18924 row->maxpos = it->current.pos;
18925 else if (row->used[TEXT_AREA])
18926 {
18927 int seen_this_string = 0;
18928 struct glyph_row *r1 = row - 1;
18929
18930 /* Did we see the same display string on the previous row? */
18931 if (STRINGP (it->object)
18932 /* this is not the first row */
18933 && row > it->w->desired_matrix->rows
18934 /* previous row is not the header line */
18935 && !r1->mode_line_p
18936 /* previous row also ends in a newline from a string */
18937 && r1->ends_in_newline_from_string_p)
18938 {
18939 struct glyph *start, *end;
18940
18941 /* Search for the last glyph of the previous row that came
18942 from buffer or string. Depending on whether the row is
18943 L2R or R2L, we need to process it front to back or the
18944 other way round. */
18945 if (!r1->reversed_p)
18946 {
18947 start = r1->glyphs[TEXT_AREA];
18948 end = start + r1->used[TEXT_AREA];
18949 /* Glyphs inserted by redisplay have an integer (zero)
18950 as their object. */
18951 while (end > start
18952 && INTEGERP ((end - 1)->object)
18953 && (end - 1)->charpos <= 0)
18954 --end;
18955 if (end > start)
18956 {
18957 if (EQ ((end - 1)->object, it->object))
18958 seen_this_string = 1;
18959 }
18960 else
18961 /* If all the glyphs of the previous row were inserted
18962 by redisplay, it means the previous row was
18963 produced from a single newline, which is only
18964 possible if that newline came from the same string
18965 as the one which produced this ROW. */
18966 seen_this_string = 1;
18967 }
18968 else
18969 {
18970 end = r1->glyphs[TEXT_AREA] - 1;
18971 start = end + r1->used[TEXT_AREA];
18972 while (end < start
18973 && INTEGERP ((end + 1)->object)
18974 && (end + 1)->charpos <= 0)
18975 ++end;
18976 if (end < start)
18977 {
18978 if (EQ ((end + 1)->object, it->object))
18979 seen_this_string = 1;
18980 }
18981 else
18982 seen_this_string = 1;
18983 }
18984 }
18985 /* Take note of each display string that covers a newline only
18986 once, the first time we see it. This is for when a display
18987 string includes more than one newline in it. */
18988 if (row->ends_in_newline_from_string_p && !seen_this_string)
18989 {
18990 /* If we were scanning the buffer forward when we displayed
18991 the string, we want to account for at least one buffer
18992 position that belongs to this row (position covered by
18993 the display string), so that cursor positioning will
18994 consider this row as a candidate when point is at the end
18995 of the visual line represented by this row. This is not
18996 required when scanning back, because max_pos will already
18997 have a much larger value. */
18998 if (CHARPOS (row->end.pos) > max_pos)
18999 INC_BOTH (max_pos, max_bpos);
19000 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19001 }
19002 else if (CHARPOS (it->eol_pos) > 0)
19003 SET_TEXT_POS (row->maxpos,
19004 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19005 else if (row->continued_p)
19006 {
19007 /* If max_pos is different from IT's current position, it
19008 means IT->method does not belong to the display element
19009 at max_pos. However, it also means that the display
19010 element at max_pos was displayed in its entirety on this
19011 line, which is equivalent to saying that the next line
19012 starts at the next buffer position. */
19013 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19014 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19015 else
19016 {
19017 INC_BOTH (max_pos, max_bpos);
19018 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19019 }
19020 }
19021 else if (row->truncated_on_right_p)
19022 /* display_line already called reseat_at_next_visible_line_start,
19023 which puts the iterator at the beginning of the next line, in
19024 the logical order. */
19025 row->maxpos = it->current.pos;
19026 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19027 /* A line that is entirely from a string/image/stretch... */
19028 row->maxpos = row->minpos;
19029 else
19030 abort ();
19031 }
19032 else
19033 row->maxpos = it->current.pos;
19034 }
19035
19036 /* Construct the glyph row IT->glyph_row in the desired matrix of
19037 IT->w from text at the current position of IT. See dispextern.h
19038 for an overview of struct it. Value is non-zero if
19039 IT->glyph_row displays text, as opposed to a line displaying ZV
19040 only. */
19041
19042 static int
19043 display_line (struct it *it)
19044 {
19045 struct glyph_row *row = it->glyph_row;
19046 Lisp_Object overlay_arrow_string;
19047 struct it wrap_it;
19048 void *wrap_data = NULL;
19049 int may_wrap = 0, wrap_x IF_LINT (= 0);
19050 int wrap_row_used = -1;
19051 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19052 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19053 int wrap_row_extra_line_spacing IF_LINT (= 0);
19054 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19055 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19056 int cvpos;
19057 EMACS_INT min_pos = ZV + 1, max_pos = 0;
19058 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19059
19060 /* We always start displaying at hpos zero even if hscrolled. */
19061 xassert (it->hpos == 0 && it->current_x == 0);
19062
19063 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19064 >= it->w->desired_matrix->nrows)
19065 {
19066 it->w->nrows_scale_factor++;
19067 fonts_changed_p = 1;
19068 return 0;
19069 }
19070
19071 /* Is IT->w showing the region? */
19072 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19073
19074 /* Clear the result glyph row and enable it. */
19075 prepare_desired_row (row);
19076
19077 row->y = it->current_y;
19078 row->start = it->start;
19079 row->continuation_lines_width = it->continuation_lines_width;
19080 row->displays_text_p = 1;
19081 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19082 it->starts_in_middle_of_char_p = 0;
19083
19084 /* Arrange the overlays nicely for our purposes. Usually, we call
19085 display_line on only one line at a time, in which case this
19086 can't really hurt too much, or we call it on lines which appear
19087 one after another in the buffer, in which case all calls to
19088 recenter_overlay_lists but the first will be pretty cheap. */
19089 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19090
19091 /* Move over display elements that are not visible because we are
19092 hscrolled. This may stop at an x-position < IT->first_visible_x
19093 if the first glyph is partially visible or if we hit a line end. */
19094 if (it->current_x < it->first_visible_x)
19095 {
19096 this_line_min_pos = row->start.pos;
19097 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19098 MOVE_TO_POS | MOVE_TO_X);
19099 /* Record the smallest positions seen while we moved over
19100 display elements that are not visible. This is needed by
19101 redisplay_internal for optimizing the case where the cursor
19102 stays inside the same line. The rest of this function only
19103 considers positions that are actually displayed, so
19104 RECORD_MAX_MIN_POS will not otherwise record positions that
19105 are hscrolled to the left of the left edge of the window. */
19106 min_pos = CHARPOS (this_line_min_pos);
19107 min_bpos = BYTEPOS (this_line_min_pos);
19108 }
19109 else
19110 {
19111 /* We only do this when not calling `move_it_in_display_line_to'
19112 above, because move_it_in_display_line_to calls
19113 handle_line_prefix itself. */
19114 handle_line_prefix (it);
19115 }
19116
19117 /* Get the initial row height. This is either the height of the
19118 text hscrolled, if there is any, or zero. */
19119 row->ascent = it->max_ascent;
19120 row->height = it->max_ascent + it->max_descent;
19121 row->phys_ascent = it->max_phys_ascent;
19122 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19123 row->extra_line_spacing = it->max_extra_line_spacing;
19124
19125 /* Utility macro to record max and min buffer positions seen until now. */
19126 #define RECORD_MAX_MIN_POS(IT) \
19127 do \
19128 { \
19129 int composition_p = !STRINGP ((IT)->string) \
19130 && ((IT)->what == IT_COMPOSITION); \
19131 EMACS_INT current_pos = \
19132 composition_p ? (IT)->cmp_it.charpos \
19133 : IT_CHARPOS (*(IT)); \
19134 EMACS_INT current_bpos = \
19135 composition_p ? CHAR_TO_BYTE (current_pos) \
19136 : IT_BYTEPOS (*(IT)); \
19137 if (current_pos < min_pos) \
19138 { \
19139 min_pos = current_pos; \
19140 min_bpos = current_bpos; \
19141 } \
19142 if (IT_CHARPOS (*it) > max_pos) \
19143 { \
19144 max_pos = IT_CHARPOS (*it); \
19145 max_bpos = IT_BYTEPOS (*it); \
19146 } \
19147 } \
19148 while (0)
19149
19150 /* Loop generating characters. The loop is left with IT on the next
19151 character to display. */
19152 while (1)
19153 {
19154 int n_glyphs_before, hpos_before, x_before;
19155 int x, nglyphs;
19156 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19157
19158 /* Retrieve the next thing to display. Value is zero if end of
19159 buffer reached. */
19160 if (!get_next_display_element (it))
19161 {
19162 /* Maybe add a space at the end of this line that is used to
19163 display the cursor there under X. Set the charpos of the
19164 first glyph of blank lines not corresponding to any text
19165 to -1. */
19166 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19167 row->exact_window_width_line_p = 1;
19168 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19169 || row->used[TEXT_AREA] == 0)
19170 {
19171 row->glyphs[TEXT_AREA]->charpos = -1;
19172 row->displays_text_p = 0;
19173
19174 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19175 && (!MINI_WINDOW_P (it->w)
19176 || (minibuf_level && EQ (it->window, minibuf_window))))
19177 row->indicate_empty_line_p = 1;
19178 }
19179
19180 it->continuation_lines_width = 0;
19181 row->ends_at_zv_p = 1;
19182 /* A row that displays right-to-left text must always have
19183 its last face extended all the way to the end of line,
19184 even if this row ends in ZV, because we still write to
19185 the screen left to right. We also need to extend the
19186 last face if the default face is remapped to some
19187 different face, otherwise the functions that clear
19188 portions of the screen will clear with the default face's
19189 background color. */
19190 if (row->reversed_p
19191 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19192 extend_face_to_end_of_line (it);
19193 break;
19194 }
19195
19196 /* Now, get the metrics of what we want to display. This also
19197 generates glyphs in `row' (which is IT->glyph_row). */
19198 n_glyphs_before = row->used[TEXT_AREA];
19199 x = it->current_x;
19200
19201 /* Remember the line height so far in case the next element doesn't
19202 fit on the line. */
19203 if (it->line_wrap != TRUNCATE)
19204 {
19205 ascent = it->max_ascent;
19206 descent = it->max_descent;
19207 phys_ascent = it->max_phys_ascent;
19208 phys_descent = it->max_phys_descent;
19209
19210 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19211 {
19212 if (IT_DISPLAYING_WHITESPACE (it))
19213 may_wrap = 1;
19214 else if (may_wrap)
19215 {
19216 SAVE_IT (wrap_it, *it, wrap_data);
19217 wrap_x = x;
19218 wrap_row_used = row->used[TEXT_AREA];
19219 wrap_row_ascent = row->ascent;
19220 wrap_row_height = row->height;
19221 wrap_row_phys_ascent = row->phys_ascent;
19222 wrap_row_phys_height = row->phys_height;
19223 wrap_row_extra_line_spacing = row->extra_line_spacing;
19224 wrap_row_min_pos = min_pos;
19225 wrap_row_min_bpos = min_bpos;
19226 wrap_row_max_pos = max_pos;
19227 wrap_row_max_bpos = max_bpos;
19228 may_wrap = 0;
19229 }
19230 }
19231 }
19232
19233 PRODUCE_GLYPHS (it);
19234
19235 /* If this display element was in marginal areas, continue with
19236 the next one. */
19237 if (it->area != TEXT_AREA)
19238 {
19239 row->ascent = max (row->ascent, it->max_ascent);
19240 row->height = max (row->height, it->max_ascent + it->max_descent);
19241 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19242 row->phys_height = max (row->phys_height,
19243 it->max_phys_ascent + it->max_phys_descent);
19244 row->extra_line_spacing = max (row->extra_line_spacing,
19245 it->max_extra_line_spacing);
19246 set_iterator_to_next (it, 1);
19247 continue;
19248 }
19249
19250 /* Does the display element fit on the line? If we truncate
19251 lines, we should draw past the right edge of the window. If
19252 we don't truncate, we want to stop so that we can display the
19253 continuation glyph before the right margin. If lines are
19254 continued, there are two possible strategies for characters
19255 resulting in more than 1 glyph (e.g. tabs): Display as many
19256 glyphs as possible in this line and leave the rest for the
19257 continuation line, or display the whole element in the next
19258 line. Original redisplay did the former, so we do it also. */
19259 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19260 hpos_before = it->hpos;
19261 x_before = x;
19262
19263 if (/* Not a newline. */
19264 nglyphs > 0
19265 /* Glyphs produced fit entirely in the line. */
19266 && it->current_x < it->last_visible_x)
19267 {
19268 it->hpos += nglyphs;
19269 row->ascent = max (row->ascent, it->max_ascent);
19270 row->height = max (row->height, it->max_ascent + it->max_descent);
19271 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19272 row->phys_height = max (row->phys_height,
19273 it->max_phys_ascent + it->max_phys_descent);
19274 row->extra_line_spacing = max (row->extra_line_spacing,
19275 it->max_extra_line_spacing);
19276 if (it->current_x - it->pixel_width < it->first_visible_x)
19277 row->x = x - it->first_visible_x;
19278 /* Record the maximum and minimum buffer positions seen so
19279 far in glyphs that will be displayed by this row. */
19280 if (it->bidi_p)
19281 RECORD_MAX_MIN_POS (it);
19282 }
19283 else
19284 {
19285 int i, new_x;
19286 struct glyph *glyph;
19287
19288 for (i = 0; i < nglyphs; ++i, x = new_x)
19289 {
19290 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19291 new_x = x + glyph->pixel_width;
19292
19293 if (/* Lines are continued. */
19294 it->line_wrap != TRUNCATE
19295 && (/* Glyph doesn't fit on the line. */
19296 new_x > it->last_visible_x
19297 /* Or it fits exactly on a window system frame. */
19298 || (new_x == it->last_visible_x
19299 && FRAME_WINDOW_P (it->f))))
19300 {
19301 /* End of a continued line. */
19302
19303 if (it->hpos == 0
19304 || (new_x == it->last_visible_x
19305 && FRAME_WINDOW_P (it->f)))
19306 {
19307 /* Current glyph is the only one on the line or
19308 fits exactly on the line. We must continue
19309 the line because we can't draw the cursor
19310 after the glyph. */
19311 row->continued_p = 1;
19312 it->current_x = new_x;
19313 it->continuation_lines_width += new_x;
19314 ++it->hpos;
19315 if (i == nglyphs - 1)
19316 {
19317 /* If line-wrap is on, check if a previous
19318 wrap point was found. */
19319 if (wrap_row_used > 0
19320 /* Even if there is a previous wrap
19321 point, continue the line here as
19322 usual, if (i) the previous character
19323 was a space or tab AND (ii) the
19324 current character is not. */
19325 && (!may_wrap
19326 || IT_DISPLAYING_WHITESPACE (it)))
19327 goto back_to_wrap;
19328
19329 /* Record the maximum and minimum buffer
19330 positions seen so far in glyphs that will be
19331 displayed by this row. */
19332 if (it->bidi_p)
19333 RECORD_MAX_MIN_POS (it);
19334 set_iterator_to_next (it, 1);
19335 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19336 {
19337 if (!get_next_display_element (it))
19338 {
19339 row->exact_window_width_line_p = 1;
19340 it->continuation_lines_width = 0;
19341 row->continued_p = 0;
19342 row->ends_at_zv_p = 1;
19343 }
19344 else if (ITERATOR_AT_END_OF_LINE_P (it))
19345 {
19346 row->continued_p = 0;
19347 row->exact_window_width_line_p = 1;
19348 }
19349 }
19350 }
19351 else if (it->bidi_p)
19352 RECORD_MAX_MIN_POS (it);
19353 }
19354 else if (CHAR_GLYPH_PADDING_P (*glyph)
19355 && !FRAME_WINDOW_P (it->f))
19356 {
19357 /* A padding glyph that doesn't fit on this line.
19358 This means the whole character doesn't fit
19359 on the line. */
19360 if (row->reversed_p)
19361 unproduce_glyphs (it, row->used[TEXT_AREA]
19362 - n_glyphs_before);
19363 row->used[TEXT_AREA] = n_glyphs_before;
19364
19365 /* Fill the rest of the row with continuation
19366 glyphs like in 20.x. */
19367 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19368 < row->glyphs[1 + TEXT_AREA])
19369 produce_special_glyphs (it, IT_CONTINUATION);
19370
19371 row->continued_p = 1;
19372 it->current_x = x_before;
19373 it->continuation_lines_width += x_before;
19374
19375 /* Restore the height to what it was before the
19376 element not fitting on the line. */
19377 it->max_ascent = ascent;
19378 it->max_descent = descent;
19379 it->max_phys_ascent = phys_ascent;
19380 it->max_phys_descent = phys_descent;
19381 }
19382 else if (wrap_row_used > 0)
19383 {
19384 back_to_wrap:
19385 if (row->reversed_p)
19386 unproduce_glyphs (it,
19387 row->used[TEXT_AREA] - wrap_row_used);
19388 RESTORE_IT (it, &wrap_it, wrap_data);
19389 it->continuation_lines_width += wrap_x;
19390 row->used[TEXT_AREA] = wrap_row_used;
19391 row->ascent = wrap_row_ascent;
19392 row->height = wrap_row_height;
19393 row->phys_ascent = wrap_row_phys_ascent;
19394 row->phys_height = wrap_row_phys_height;
19395 row->extra_line_spacing = wrap_row_extra_line_spacing;
19396 min_pos = wrap_row_min_pos;
19397 min_bpos = wrap_row_min_bpos;
19398 max_pos = wrap_row_max_pos;
19399 max_bpos = wrap_row_max_bpos;
19400 row->continued_p = 1;
19401 row->ends_at_zv_p = 0;
19402 row->exact_window_width_line_p = 0;
19403 it->continuation_lines_width += x;
19404
19405 /* Make sure that a non-default face is extended
19406 up to the right margin of the window. */
19407 extend_face_to_end_of_line (it);
19408 }
19409 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19410 {
19411 /* A TAB that extends past the right edge of the
19412 window. This produces a single glyph on
19413 window system frames. We leave the glyph in
19414 this row and let it fill the row, but don't
19415 consume the TAB. */
19416 it->continuation_lines_width += it->last_visible_x;
19417 row->ends_in_middle_of_char_p = 1;
19418 row->continued_p = 1;
19419 glyph->pixel_width = it->last_visible_x - x;
19420 it->starts_in_middle_of_char_p = 1;
19421 }
19422 else
19423 {
19424 /* Something other than a TAB that draws past
19425 the right edge of the window. Restore
19426 positions to values before the element. */
19427 if (row->reversed_p)
19428 unproduce_glyphs (it, row->used[TEXT_AREA]
19429 - (n_glyphs_before + i));
19430 row->used[TEXT_AREA] = n_glyphs_before + i;
19431
19432 /* Display continuation glyphs. */
19433 if (!FRAME_WINDOW_P (it->f))
19434 produce_special_glyphs (it, IT_CONTINUATION);
19435 row->continued_p = 1;
19436
19437 it->current_x = x_before;
19438 it->continuation_lines_width += x;
19439 extend_face_to_end_of_line (it);
19440
19441 if (nglyphs > 1 && i > 0)
19442 {
19443 row->ends_in_middle_of_char_p = 1;
19444 it->starts_in_middle_of_char_p = 1;
19445 }
19446
19447 /* Restore the height to what it was before the
19448 element not fitting on the line. */
19449 it->max_ascent = ascent;
19450 it->max_descent = descent;
19451 it->max_phys_ascent = phys_ascent;
19452 it->max_phys_descent = phys_descent;
19453 }
19454
19455 break;
19456 }
19457 else if (new_x > it->first_visible_x)
19458 {
19459 /* Increment number of glyphs actually displayed. */
19460 ++it->hpos;
19461
19462 /* Record the maximum and minimum buffer positions
19463 seen so far in glyphs that will be displayed by
19464 this row. */
19465 if (it->bidi_p)
19466 RECORD_MAX_MIN_POS (it);
19467
19468 if (x < it->first_visible_x)
19469 /* Glyph is partially visible, i.e. row starts at
19470 negative X position. */
19471 row->x = x - it->first_visible_x;
19472 }
19473 else
19474 {
19475 /* Glyph is completely off the left margin of the
19476 window. This should not happen because of the
19477 move_it_in_display_line at the start of this
19478 function, unless the text display area of the
19479 window is empty. */
19480 xassert (it->first_visible_x <= it->last_visible_x);
19481 }
19482 }
19483 /* Even if this display element produced no glyphs at all,
19484 we want to record its position. */
19485 if (it->bidi_p && nglyphs == 0)
19486 RECORD_MAX_MIN_POS (it);
19487
19488 row->ascent = max (row->ascent, it->max_ascent);
19489 row->height = max (row->height, it->max_ascent + it->max_descent);
19490 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19491 row->phys_height = max (row->phys_height,
19492 it->max_phys_ascent + it->max_phys_descent);
19493 row->extra_line_spacing = max (row->extra_line_spacing,
19494 it->max_extra_line_spacing);
19495
19496 /* End of this display line if row is continued. */
19497 if (row->continued_p || row->ends_at_zv_p)
19498 break;
19499 }
19500
19501 at_end_of_line:
19502 /* Is this a line end? If yes, we're also done, after making
19503 sure that a non-default face is extended up to the right
19504 margin of the window. */
19505 if (ITERATOR_AT_END_OF_LINE_P (it))
19506 {
19507 int used_before = row->used[TEXT_AREA];
19508
19509 row->ends_in_newline_from_string_p = STRINGP (it->object);
19510
19511 /* Add a space at the end of the line that is used to
19512 display the cursor there. */
19513 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19514 append_space_for_newline (it, 0);
19515
19516 /* Extend the face to the end of the line. */
19517 extend_face_to_end_of_line (it);
19518
19519 /* Make sure we have the position. */
19520 if (used_before == 0)
19521 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19522
19523 /* Record the position of the newline, for use in
19524 find_row_edges. */
19525 it->eol_pos = it->current.pos;
19526
19527 /* Consume the line end. This skips over invisible lines. */
19528 set_iterator_to_next (it, 1);
19529 it->continuation_lines_width = 0;
19530 break;
19531 }
19532
19533 /* Proceed with next display element. Note that this skips
19534 over lines invisible because of selective display. */
19535 set_iterator_to_next (it, 1);
19536
19537 /* If we truncate lines, we are done when the last displayed
19538 glyphs reach past the right margin of the window. */
19539 if (it->line_wrap == TRUNCATE
19540 && (FRAME_WINDOW_P (it->f)
19541 ? (it->current_x >= it->last_visible_x)
19542 : (it->current_x > it->last_visible_x)))
19543 {
19544 /* Maybe add truncation glyphs. */
19545 if (!FRAME_WINDOW_P (it->f))
19546 {
19547 int i, n;
19548
19549 if (!row->reversed_p)
19550 {
19551 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19552 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19553 break;
19554 }
19555 else
19556 {
19557 for (i = 0; i < row->used[TEXT_AREA]; i++)
19558 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19559 break;
19560 /* Remove any padding glyphs at the front of ROW, to
19561 make room for the truncation glyphs we will be
19562 adding below. The loop below always inserts at
19563 least one truncation glyph, so also remove the
19564 last glyph added to ROW. */
19565 unproduce_glyphs (it, i + 1);
19566 /* Adjust i for the loop below. */
19567 i = row->used[TEXT_AREA] - (i + 1);
19568 }
19569
19570 for (n = row->used[TEXT_AREA]; i < n; ++i)
19571 {
19572 row->used[TEXT_AREA] = i;
19573 produce_special_glyphs (it, IT_TRUNCATION);
19574 }
19575 }
19576 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19577 {
19578 /* Don't truncate if we can overflow newline into fringe. */
19579 if (!get_next_display_element (it))
19580 {
19581 it->continuation_lines_width = 0;
19582 row->ends_at_zv_p = 1;
19583 row->exact_window_width_line_p = 1;
19584 break;
19585 }
19586 if (ITERATOR_AT_END_OF_LINE_P (it))
19587 {
19588 row->exact_window_width_line_p = 1;
19589 goto at_end_of_line;
19590 }
19591 }
19592
19593 row->truncated_on_right_p = 1;
19594 it->continuation_lines_width = 0;
19595 reseat_at_next_visible_line_start (it, 0);
19596 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19597 it->hpos = hpos_before;
19598 it->current_x = x_before;
19599 break;
19600 }
19601 }
19602
19603 if (wrap_data)
19604 bidi_unshelve_cache (wrap_data, 1);
19605
19606 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19607 at the left window margin. */
19608 if (it->first_visible_x
19609 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19610 {
19611 if (!FRAME_WINDOW_P (it->f))
19612 insert_left_trunc_glyphs (it);
19613 row->truncated_on_left_p = 1;
19614 }
19615
19616 /* Remember the position at which this line ends.
19617
19618 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19619 cannot be before the call to find_row_edges below, since that is
19620 where these positions are determined. */
19621 row->end = it->current;
19622 if (!it->bidi_p)
19623 {
19624 row->minpos = row->start.pos;
19625 row->maxpos = row->end.pos;
19626 }
19627 else
19628 {
19629 /* ROW->minpos and ROW->maxpos must be the smallest and
19630 `1 + the largest' buffer positions in ROW. But if ROW was
19631 bidi-reordered, these two positions can be anywhere in the
19632 row, so we must determine them now. */
19633 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19634 }
19635
19636 /* If the start of this line is the overlay arrow-position, then
19637 mark this glyph row as the one containing the overlay arrow.
19638 This is clearly a mess with variable size fonts. It would be
19639 better to let it be displayed like cursors under X. */
19640 if ((row->displays_text_p || !overlay_arrow_seen)
19641 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19642 !NILP (overlay_arrow_string)))
19643 {
19644 /* Overlay arrow in window redisplay is a fringe bitmap. */
19645 if (STRINGP (overlay_arrow_string))
19646 {
19647 struct glyph_row *arrow_row
19648 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19649 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19650 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19651 struct glyph *p = row->glyphs[TEXT_AREA];
19652 struct glyph *p2, *end;
19653
19654 /* Copy the arrow glyphs. */
19655 while (glyph < arrow_end)
19656 *p++ = *glyph++;
19657
19658 /* Throw away padding glyphs. */
19659 p2 = p;
19660 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19661 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19662 ++p2;
19663 if (p2 > p)
19664 {
19665 while (p2 < end)
19666 *p++ = *p2++;
19667 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19668 }
19669 }
19670 else
19671 {
19672 xassert (INTEGERP (overlay_arrow_string));
19673 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19674 }
19675 overlay_arrow_seen = 1;
19676 }
19677
19678 /* Highlight trailing whitespace. */
19679 if (!NILP (Vshow_trailing_whitespace))
19680 highlight_trailing_whitespace (it->f, it->glyph_row);
19681
19682 /* Compute pixel dimensions of this line. */
19683 compute_line_metrics (it);
19684
19685 /* Implementation note: No changes in the glyphs of ROW or in their
19686 faces can be done past this point, because compute_line_metrics
19687 computes ROW's hash value and stores it within the glyph_row
19688 structure. */
19689
19690 /* Record whether this row ends inside an ellipsis. */
19691 row->ends_in_ellipsis_p
19692 = (it->method == GET_FROM_DISPLAY_VECTOR
19693 && it->ellipsis_p);
19694
19695 /* Save fringe bitmaps in this row. */
19696 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19697 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19698 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19699 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19700
19701 it->left_user_fringe_bitmap = 0;
19702 it->left_user_fringe_face_id = 0;
19703 it->right_user_fringe_bitmap = 0;
19704 it->right_user_fringe_face_id = 0;
19705
19706 /* Maybe set the cursor. */
19707 cvpos = it->w->cursor.vpos;
19708 if ((cvpos < 0
19709 /* In bidi-reordered rows, keep checking for proper cursor
19710 position even if one has been found already, because buffer
19711 positions in such rows change non-linearly with ROW->VPOS,
19712 when a line is continued. One exception: when we are at ZV,
19713 display cursor on the first suitable glyph row, since all
19714 the empty rows after that also have their position set to ZV. */
19715 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19716 lines' rows is implemented for bidi-reordered rows. */
19717 || (it->bidi_p
19718 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19719 && PT >= MATRIX_ROW_START_CHARPOS (row)
19720 && PT <= MATRIX_ROW_END_CHARPOS (row)
19721 && cursor_row_p (row))
19722 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19723
19724 /* Prepare for the next line. This line starts horizontally at (X
19725 HPOS) = (0 0). Vertical positions are incremented. As a
19726 convenience for the caller, IT->glyph_row is set to the next
19727 row to be used. */
19728 it->current_x = it->hpos = 0;
19729 it->current_y += row->height;
19730 SET_TEXT_POS (it->eol_pos, 0, 0);
19731 ++it->vpos;
19732 ++it->glyph_row;
19733 /* The next row should by default use the same value of the
19734 reversed_p flag as this one. set_iterator_to_next decides when
19735 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19736 the flag accordingly. */
19737 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19738 it->glyph_row->reversed_p = row->reversed_p;
19739 it->start = row->end;
19740 return row->displays_text_p;
19741
19742 #undef RECORD_MAX_MIN_POS
19743 }
19744
19745 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19746 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19747 doc: /* Return paragraph direction at point in BUFFER.
19748 Value is either `left-to-right' or `right-to-left'.
19749 If BUFFER is omitted or nil, it defaults to the current buffer.
19750
19751 Paragraph direction determines how the text in the paragraph is displayed.
19752 In left-to-right paragraphs, text begins at the left margin of the window
19753 and the reading direction is generally left to right. In right-to-left
19754 paragraphs, text begins at the right margin and is read from right to left.
19755
19756 See also `bidi-paragraph-direction'. */)
19757 (Lisp_Object buffer)
19758 {
19759 struct buffer *buf = current_buffer;
19760 struct buffer *old = buf;
19761
19762 if (! NILP (buffer))
19763 {
19764 CHECK_BUFFER (buffer);
19765 buf = XBUFFER (buffer);
19766 }
19767
19768 if (NILP (BVAR (buf, bidi_display_reordering))
19769 || NILP (BVAR (buf, enable_multibyte_characters))
19770 /* When we are loading loadup.el, the character property tables
19771 needed for bidi iteration are not yet available. */
19772 || !NILP (Vpurify_flag))
19773 return Qleft_to_right;
19774 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19775 return BVAR (buf, bidi_paragraph_direction);
19776 else
19777 {
19778 /* Determine the direction from buffer text. We could try to
19779 use current_matrix if it is up to date, but this seems fast
19780 enough as it is. */
19781 struct bidi_it itb;
19782 EMACS_INT pos = BUF_PT (buf);
19783 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19784 int c;
19785 void *itb_data = bidi_shelve_cache ();
19786
19787 set_buffer_temp (buf);
19788 /* bidi_paragraph_init finds the base direction of the paragraph
19789 by searching forward from paragraph start. We need the base
19790 direction of the current or _previous_ paragraph, so we need
19791 to make sure we are within that paragraph. To that end, find
19792 the previous non-empty line. */
19793 if (pos >= ZV && pos > BEGV)
19794 {
19795 pos--;
19796 bytepos = CHAR_TO_BYTE (pos);
19797 }
19798 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19799 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19800 {
19801 while ((c = FETCH_BYTE (bytepos)) == '\n'
19802 || c == ' ' || c == '\t' || c == '\f')
19803 {
19804 if (bytepos <= BEGV_BYTE)
19805 break;
19806 bytepos--;
19807 pos--;
19808 }
19809 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19810 bytepos--;
19811 }
19812 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19813 itb.paragraph_dir = NEUTRAL_DIR;
19814 itb.string.s = NULL;
19815 itb.string.lstring = Qnil;
19816 itb.string.bufpos = 0;
19817 itb.string.unibyte = 0;
19818 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19819 bidi_unshelve_cache (itb_data, 0);
19820 set_buffer_temp (old);
19821 switch (itb.paragraph_dir)
19822 {
19823 case L2R:
19824 return Qleft_to_right;
19825 break;
19826 case R2L:
19827 return Qright_to_left;
19828 break;
19829 default:
19830 abort ();
19831 }
19832 }
19833 }
19834
19835
19836 \f
19837 /***********************************************************************
19838 Menu Bar
19839 ***********************************************************************/
19840
19841 /* Redisplay the menu bar in the frame for window W.
19842
19843 The menu bar of X frames that don't have X toolkit support is
19844 displayed in a special window W->frame->menu_bar_window.
19845
19846 The menu bar of terminal frames is treated specially as far as
19847 glyph matrices are concerned. Menu bar lines are not part of
19848 windows, so the update is done directly on the frame matrix rows
19849 for the menu bar. */
19850
19851 static void
19852 display_menu_bar (struct window *w)
19853 {
19854 struct frame *f = XFRAME (WINDOW_FRAME (w));
19855 struct it it;
19856 Lisp_Object items;
19857 int i;
19858
19859 /* Don't do all this for graphical frames. */
19860 #ifdef HAVE_NTGUI
19861 if (FRAME_W32_P (f))
19862 return;
19863 #endif
19864 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19865 if (FRAME_X_P (f))
19866 return;
19867 #endif
19868
19869 #ifdef HAVE_NS
19870 if (FRAME_NS_P (f))
19871 return;
19872 #endif /* HAVE_NS */
19873
19874 #ifdef USE_X_TOOLKIT
19875 xassert (!FRAME_WINDOW_P (f));
19876 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19877 it.first_visible_x = 0;
19878 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19879 #else /* not USE_X_TOOLKIT */
19880 if (FRAME_WINDOW_P (f))
19881 {
19882 /* Menu bar lines are displayed in the desired matrix of the
19883 dummy window menu_bar_window. */
19884 struct window *menu_w;
19885 xassert (WINDOWP (f->menu_bar_window));
19886 menu_w = XWINDOW (f->menu_bar_window);
19887 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19888 MENU_FACE_ID);
19889 it.first_visible_x = 0;
19890 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19891 }
19892 else
19893 {
19894 /* This is a TTY frame, i.e. character hpos/vpos are used as
19895 pixel x/y. */
19896 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19897 MENU_FACE_ID);
19898 it.first_visible_x = 0;
19899 it.last_visible_x = FRAME_COLS (f);
19900 }
19901 #endif /* not USE_X_TOOLKIT */
19902
19903 /* FIXME: This should be controlled by a user option. See the
19904 comments in redisplay_tool_bar and display_mode_line about
19905 this. */
19906 it.paragraph_embedding = L2R;
19907
19908 if (! mode_line_inverse_video)
19909 /* Force the menu-bar to be displayed in the default face. */
19910 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19911
19912 /* Clear all rows of the menu bar. */
19913 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19914 {
19915 struct glyph_row *row = it.glyph_row + i;
19916 clear_glyph_row (row);
19917 row->enabled_p = 1;
19918 row->full_width_p = 1;
19919 }
19920
19921 /* Display all items of the menu bar. */
19922 items = FRAME_MENU_BAR_ITEMS (it.f);
19923 for (i = 0; i < ASIZE (items); i += 4)
19924 {
19925 Lisp_Object string;
19926
19927 /* Stop at nil string. */
19928 string = AREF (items, i + 1);
19929 if (NILP (string))
19930 break;
19931
19932 /* Remember where item was displayed. */
19933 ASET (items, i + 3, make_number (it.hpos));
19934
19935 /* Display the item, pad with one space. */
19936 if (it.current_x < it.last_visible_x)
19937 display_string (NULL, string, Qnil, 0, 0, &it,
19938 SCHARS (string) + 1, 0, 0, -1);
19939 }
19940
19941 /* Fill out the line with spaces. */
19942 if (it.current_x < it.last_visible_x)
19943 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19944
19945 /* Compute the total height of the lines. */
19946 compute_line_metrics (&it);
19947 }
19948
19949
19950 \f
19951 /***********************************************************************
19952 Mode Line
19953 ***********************************************************************/
19954
19955 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19956 FORCE is non-zero, redisplay mode lines unconditionally.
19957 Otherwise, redisplay only mode lines that are garbaged. Value is
19958 the number of windows whose mode lines were redisplayed. */
19959
19960 static int
19961 redisplay_mode_lines (Lisp_Object window, int force)
19962 {
19963 int nwindows = 0;
19964
19965 while (!NILP (window))
19966 {
19967 struct window *w = XWINDOW (window);
19968
19969 if (WINDOWP (w->hchild))
19970 nwindows += redisplay_mode_lines (w->hchild, force);
19971 else if (WINDOWP (w->vchild))
19972 nwindows += redisplay_mode_lines (w->vchild, force);
19973 else if (force
19974 || FRAME_GARBAGED_P (XFRAME (w->frame))
19975 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19976 {
19977 struct text_pos lpoint;
19978 struct buffer *old = current_buffer;
19979
19980 /* Set the window's buffer for the mode line display. */
19981 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19982 set_buffer_internal_1 (XBUFFER (w->buffer));
19983
19984 /* Point refers normally to the selected window. For any
19985 other window, set up appropriate value. */
19986 if (!EQ (window, selected_window))
19987 {
19988 struct text_pos pt;
19989
19990 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19991 if (CHARPOS (pt) < BEGV)
19992 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19993 else if (CHARPOS (pt) > (ZV - 1))
19994 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19995 else
19996 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19997 }
19998
19999 /* Display mode lines. */
20000 clear_glyph_matrix (w->desired_matrix);
20001 if (display_mode_lines (w))
20002 {
20003 ++nwindows;
20004 w->must_be_updated_p = 1;
20005 }
20006
20007 /* Restore old settings. */
20008 set_buffer_internal_1 (old);
20009 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20010 }
20011
20012 window = w->next;
20013 }
20014
20015 return nwindows;
20016 }
20017
20018
20019 /* Display the mode and/or header line of window W. Value is the
20020 sum number of mode lines and header lines displayed. */
20021
20022 static int
20023 display_mode_lines (struct window *w)
20024 {
20025 Lisp_Object old_selected_window, old_selected_frame;
20026 int n = 0;
20027
20028 old_selected_frame = selected_frame;
20029 selected_frame = w->frame;
20030 old_selected_window = selected_window;
20031 XSETWINDOW (selected_window, w);
20032
20033 /* These will be set while the mode line specs are processed. */
20034 line_number_displayed = 0;
20035 w->column_number_displayed = Qnil;
20036
20037 if (WINDOW_WANTS_MODELINE_P (w))
20038 {
20039 struct window *sel_w = XWINDOW (old_selected_window);
20040
20041 /* Select mode line face based on the real selected window. */
20042 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20043 BVAR (current_buffer, mode_line_format));
20044 ++n;
20045 }
20046
20047 if (WINDOW_WANTS_HEADER_LINE_P (w))
20048 {
20049 display_mode_line (w, HEADER_LINE_FACE_ID,
20050 BVAR (current_buffer, header_line_format));
20051 ++n;
20052 }
20053
20054 selected_frame = old_selected_frame;
20055 selected_window = old_selected_window;
20056 return n;
20057 }
20058
20059
20060 /* Display mode or header line of window W. FACE_ID specifies which
20061 line to display; it is either MODE_LINE_FACE_ID or
20062 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20063 display. Value is the pixel height of the mode/header line
20064 displayed. */
20065
20066 static int
20067 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20068 {
20069 struct it it;
20070 struct face *face;
20071 int count = SPECPDL_INDEX ();
20072
20073 init_iterator (&it, w, -1, -1, NULL, face_id);
20074 /* Don't extend on a previously drawn mode-line.
20075 This may happen if called from pos_visible_p. */
20076 it.glyph_row->enabled_p = 0;
20077 prepare_desired_row (it.glyph_row);
20078
20079 it.glyph_row->mode_line_p = 1;
20080
20081 if (! mode_line_inverse_video)
20082 /* Force the mode-line to be displayed in the default face. */
20083 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20084
20085 /* FIXME: This should be controlled by a user option. But
20086 supporting such an option is not trivial, since the mode line is
20087 made up of many separate strings. */
20088 it.paragraph_embedding = L2R;
20089
20090 record_unwind_protect (unwind_format_mode_line,
20091 format_mode_line_unwind_data (NULL, Qnil, 0));
20092
20093 mode_line_target = MODE_LINE_DISPLAY;
20094
20095 /* Temporarily make frame's keyboard the current kboard so that
20096 kboard-local variables in the mode_line_format will get the right
20097 values. */
20098 push_kboard (FRAME_KBOARD (it.f));
20099 record_unwind_save_match_data ();
20100 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20101 pop_kboard ();
20102
20103 unbind_to (count, Qnil);
20104
20105 /* Fill up with spaces. */
20106 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20107
20108 compute_line_metrics (&it);
20109 it.glyph_row->full_width_p = 1;
20110 it.glyph_row->continued_p = 0;
20111 it.glyph_row->truncated_on_left_p = 0;
20112 it.glyph_row->truncated_on_right_p = 0;
20113
20114 /* Make a 3D mode-line have a shadow at its right end. */
20115 face = FACE_FROM_ID (it.f, face_id);
20116 extend_face_to_end_of_line (&it);
20117 if (face->box != FACE_NO_BOX)
20118 {
20119 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20120 + it.glyph_row->used[TEXT_AREA] - 1);
20121 last->right_box_line_p = 1;
20122 }
20123
20124 return it.glyph_row->height;
20125 }
20126
20127 /* Move element ELT in LIST to the front of LIST.
20128 Return the updated list. */
20129
20130 static Lisp_Object
20131 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20132 {
20133 register Lisp_Object tail, prev;
20134 register Lisp_Object tem;
20135
20136 tail = list;
20137 prev = Qnil;
20138 while (CONSP (tail))
20139 {
20140 tem = XCAR (tail);
20141
20142 if (EQ (elt, tem))
20143 {
20144 /* Splice out the link TAIL. */
20145 if (NILP (prev))
20146 list = XCDR (tail);
20147 else
20148 Fsetcdr (prev, XCDR (tail));
20149
20150 /* Now make it the first. */
20151 Fsetcdr (tail, list);
20152 return tail;
20153 }
20154 else
20155 prev = tail;
20156 tail = XCDR (tail);
20157 QUIT;
20158 }
20159
20160 /* Not found--return unchanged LIST. */
20161 return list;
20162 }
20163
20164 /* Contribute ELT to the mode line for window IT->w. How it
20165 translates into text depends on its data type.
20166
20167 IT describes the display environment in which we display, as usual.
20168
20169 DEPTH is the depth in recursion. It is used to prevent
20170 infinite recursion here.
20171
20172 FIELD_WIDTH is the number of characters the display of ELT should
20173 occupy in the mode line, and PRECISION is the maximum number of
20174 characters to display from ELT's representation. See
20175 display_string for details.
20176
20177 Returns the hpos of the end of the text generated by ELT.
20178
20179 PROPS is a property list to add to any string we encounter.
20180
20181 If RISKY is nonzero, remove (disregard) any properties in any string
20182 we encounter, and ignore :eval and :propertize.
20183
20184 The global variable `mode_line_target' determines whether the
20185 output is passed to `store_mode_line_noprop',
20186 `store_mode_line_string', or `display_string'. */
20187
20188 static int
20189 display_mode_element (struct it *it, int depth, int field_width, int precision,
20190 Lisp_Object elt, Lisp_Object props, int risky)
20191 {
20192 int n = 0, field, prec;
20193 int literal = 0;
20194
20195 tail_recurse:
20196 if (depth > 100)
20197 elt = build_string ("*too-deep*");
20198
20199 depth++;
20200
20201 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20202 {
20203 case Lisp_String:
20204 {
20205 /* A string: output it and check for %-constructs within it. */
20206 unsigned char c;
20207 EMACS_INT offset = 0;
20208
20209 if (SCHARS (elt) > 0
20210 && (!NILP (props) || risky))
20211 {
20212 Lisp_Object oprops, aelt;
20213 oprops = Ftext_properties_at (make_number (0), elt);
20214
20215 /* If the starting string's properties are not what
20216 we want, translate the string. Also, if the string
20217 is risky, do that anyway. */
20218
20219 if (NILP (Fequal (props, oprops)) || risky)
20220 {
20221 /* If the starting string has properties,
20222 merge the specified ones onto the existing ones. */
20223 if (! NILP (oprops) && !risky)
20224 {
20225 Lisp_Object tem;
20226
20227 oprops = Fcopy_sequence (oprops);
20228 tem = props;
20229 while (CONSP (tem))
20230 {
20231 oprops = Fplist_put (oprops, XCAR (tem),
20232 XCAR (XCDR (tem)));
20233 tem = XCDR (XCDR (tem));
20234 }
20235 props = oprops;
20236 }
20237
20238 aelt = Fassoc (elt, mode_line_proptrans_alist);
20239 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20240 {
20241 /* AELT is what we want. Move it to the front
20242 without consing. */
20243 elt = XCAR (aelt);
20244 mode_line_proptrans_alist
20245 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20246 }
20247 else
20248 {
20249 Lisp_Object tem;
20250
20251 /* If AELT has the wrong props, it is useless.
20252 so get rid of it. */
20253 if (! NILP (aelt))
20254 mode_line_proptrans_alist
20255 = Fdelq (aelt, mode_line_proptrans_alist);
20256
20257 elt = Fcopy_sequence (elt);
20258 Fset_text_properties (make_number (0), Flength (elt),
20259 props, elt);
20260 /* Add this item to mode_line_proptrans_alist. */
20261 mode_line_proptrans_alist
20262 = Fcons (Fcons (elt, props),
20263 mode_line_proptrans_alist);
20264 /* Truncate mode_line_proptrans_alist
20265 to at most 50 elements. */
20266 tem = Fnthcdr (make_number (50),
20267 mode_line_proptrans_alist);
20268 if (! NILP (tem))
20269 XSETCDR (tem, Qnil);
20270 }
20271 }
20272 }
20273
20274 offset = 0;
20275
20276 if (literal)
20277 {
20278 prec = precision - n;
20279 switch (mode_line_target)
20280 {
20281 case MODE_LINE_NOPROP:
20282 case MODE_LINE_TITLE:
20283 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20284 break;
20285 case MODE_LINE_STRING:
20286 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20287 break;
20288 case MODE_LINE_DISPLAY:
20289 n += display_string (NULL, elt, Qnil, 0, 0, it,
20290 0, prec, 0, STRING_MULTIBYTE (elt));
20291 break;
20292 }
20293
20294 break;
20295 }
20296
20297 /* Handle the non-literal case. */
20298
20299 while ((precision <= 0 || n < precision)
20300 && SREF (elt, offset) != 0
20301 && (mode_line_target != MODE_LINE_DISPLAY
20302 || it->current_x < it->last_visible_x))
20303 {
20304 EMACS_INT last_offset = offset;
20305
20306 /* Advance to end of string or next format specifier. */
20307 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20308 ;
20309
20310 if (offset - 1 != last_offset)
20311 {
20312 EMACS_INT nchars, nbytes;
20313
20314 /* Output to end of string or up to '%'. Field width
20315 is length of string. Don't output more than
20316 PRECISION allows us. */
20317 offset--;
20318
20319 prec = c_string_width (SDATA (elt) + last_offset,
20320 offset - last_offset, precision - n,
20321 &nchars, &nbytes);
20322
20323 switch (mode_line_target)
20324 {
20325 case MODE_LINE_NOPROP:
20326 case MODE_LINE_TITLE:
20327 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20328 break;
20329 case MODE_LINE_STRING:
20330 {
20331 EMACS_INT bytepos = last_offset;
20332 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20333 EMACS_INT endpos = (precision <= 0
20334 ? string_byte_to_char (elt, offset)
20335 : charpos + nchars);
20336
20337 n += store_mode_line_string (NULL,
20338 Fsubstring (elt, make_number (charpos),
20339 make_number (endpos)),
20340 0, 0, 0, Qnil);
20341 }
20342 break;
20343 case MODE_LINE_DISPLAY:
20344 {
20345 EMACS_INT bytepos = last_offset;
20346 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20347
20348 if (precision <= 0)
20349 nchars = string_byte_to_char (elt, offset) - charpos;
20350 n += display_string (NULL, elt, Qnil, 0, charpos,
20351 it, 0, nchars, 0,
20352 STRING_MULTIBYTE (elt));
20353 }
20354 break;
20355 }
20356 }
20357 else /* c == '%' */
20358 {
20359 EMACS_INT percent_position = offset;
20360
20361 /* Get the specified minimum width. Zero means
20362 don't pad. */
20363 field = 0;
20364 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20365 field = field * 10 + c - '0';
20366
20367 /* Don't pad beyond the total padding allowed. */
20368 if (field_width - n > 0 && field > field_width - n)
20369 field = field_width - n;
20370
20371 /* Note that either PRECISION <= 0 or N < PRECISION. */
20372 prec = precision - n;
20373
20374 if (c == 'M')
20375 n += display_mode_element (it, depth, field, prec,
20376 Vglobal_mode_string, props,
20377 risky);
20378 else if (c != 0)
20379 {
20380 int multibyte;
20381 EMACS_INT bytepos, charpos;
20382 const char *spec;
20383 Lisp_Object string;
20384
20385 bytepos = percent_position;
20386 charpos = (STRING_MULTIBYTE (elt)
20387 ? string_byte_to_char (elt, bytepos)
20388 : bytepos);
20389 spec = decode_mode_spec (it->w, c, field, &string);
20390 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20391
20392 switch (mode_line_target)
20393 {
20394 case MODE_LINE_NOPROP:
20395 case MODE_LINE_TITLE:
20396 n += store_mode_line_noprop (spec, field, prec);
20397 break;
20398 case MODE_LINE_STRING:
20399 {
20400 Lisp_Object tem = build_string (spec);
20401 props = Ftext_properties_at (make_number (charpos), elt);
20402 /* Should only keep face property in props */
20403 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20404 }
20405 break;
20406 case MODE_LINE_DISPLAY:
20407 {
20408 int nglyphs_before, nwritten;
20409
20410 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20411 nwritten = display_string (spec, string, elt,
20412 charpos, 0, it,
20413 field, prec, 0,
20414 multibyte);
20415
20416 /* Assign to the glyphs written above the
20417 string where the `%x' came from, position
20418 of the `%'. */
20419 if (nwritten > 0)
20420 {
20421 struct glyph *glyph
20422 = (it->glyph_row->glyphs[TEXT_AREA]
20423 + nglyphs_before);
20424 int i;
20425
20426 for (i = 0; i < nwritten; ++i)
20427 {
20428 glyph[i].object = elt;
20429 glyph[i].charpos = charpos;
20430 }
20431
20432 n += nwritten;
20433 }
20434 }
20435 break;
20436 }
20437 }
20438 else /* c == 0 */
20439 break;
20440 }
20441 }
20442 }
20443 break;
20444
20445 case Lisp_Symbol:
20446 /* A symbol: process the value of the symbol recursively
20447 as if it appeared here directly. Avoid error if symbol void.
20448 Special case: if value of symbol is a string, output the string
20449 literally. */
20450 {
20451 register Lisp_Object tem;
20452
20453 /* If the variable is not marked as risky to set
20454 then its contents are risky to use. */
20455 if (NILP (Fget (elt, Qrisky_local_variable)))
20456 risky = 1;
20457
20458 tem = Fboundp (elt);
20459 if (!NILP (tem))
20460 {
20461 tem = Fsymbol_value (elt);
20462 /* If value is a string, output that string literally:
20463 don't check for % within it. */
20464 if (STRINGP (tem))
20465 literal = 1;
20466
20467 if (!EQ (tem, elt))
20468 {
20469 /* Give up right away for nil or t. */
20470 elt = tem;
20471 goto tail_recurse;
20472 }
20473 }
20474 }
20475 break;
20476
20477 case Lisp_Cons:
20478 {
20479 register Lisp_Object car, tem;
20480
20481 /* A cons cell: five distinct cases.
20482 If first element is :eval or :propertize, do something special.
20483 If first element is a string or a cons, process all the elements
20484 and effectively concatenate them.
20485 If first element is a negative number, truncate displaying cdr to
20486 at most that many characters. If positive, pad (with spaces)
20487 to at least that many characters.
20488 If first element is a symbol, process the cadr or caddr recursively
20489 according to whether the symbol's value is non-nil or nil. */
20490 car = XCAR (elt);
20491 if (EQ (car, QCeval))
20492 {
20493 /* An element of the form (:eval FORM) means evaluate FORM
20494 and use the result as mode line elements. */
20495
20496 if (risky)
20497 break;
20498
20499 if (CONSP (XCDR (elt)))
20500 {
20501 Lisp_Object spec;
20502 spec = safe_eval (XCAR (XCDR (elt)));
20503 n += display_mode_element (it, depth, field_width - n,
20504 precision - n, spec, props,
20505 risky);
20506 }
20507 }
20508 else if (EQ (car, QCpropertize))
20509 {
20510 /* An element of the form (:propertize ELT PROPS...)
20511 means display ELT but applying properties PROPS. */
20512
20513 if (risky)
20514 break;
20515
20516 if (CONSP (XCDR (elt)))
20517 n += display_mode_element (it, depth, field_width - n,
20518 precision - n, XCAR (XCDR (elt)),
20519 XCDR (XCDR (elt)), risky);
20520 }
20521 else if (SYMBOLP (car))
20522 {
20523 tem = Fboundp (car);
20524 elt = XCDR (elt);
20525 if (!CONSP (elt))
20526 goto invalid;
20527 /* elt is now the cdr, and we know it is a cons cell.
20528 Use its car if CAR has a non-nil value. */
20529 if (!NILP (tem))
20530 {
20531 tem = Fsymbol_value (car);
20532 if (!NILP (tem))
20533 {
20534 elt = XCAR (elt);
20535 goto tail_recurse;
20536 }
20537 }
20538 /* Symbol's value is nil (or symbol is unbound)
20539 Get the cddr of the original list
20540 and if possible find the caddr and use that. */
20541 elt = XCDR (elt);
20542 if (NILP (elt))
20543 break;
20544 else if (!CONSP (elt))
20545 goto invalid;
20546 elt = XCAR (elt);
20547 goto tail_recurse;
20548 }
20549 else if (INTEGERP (car))
20550 {
20551 register int lim = XINT (car);
20552 elt = XCDR (elt);
20553 if (lim < 0)
20554 {
20555 /* Negative int means reduce maximum width. */
20556 if (precision <= 0)
20557 precision = -lim;
20558 else
20559 precision = min (precision, -lim);
20560 }
20561 else if (lim > 0)
20562 {
20563 /* Padding specified. Don't let it be more than
20564 current maximum. */
20565 if (precision > 0)
20566 lim = min (precision, lim);
20567
20568 /* If that's more padding than already wanted, queue it.
20569 But don't reduce padding already specified even if
20570 that is beyond the current truncation point. */
20571 field_width = max (lim, field_width);
20572 }
20573 goto tail_recurse;
20574 }
20575 else if (STRINGP (car) || CONSP (car))
20576 {
20577 Lisp_Object halftail = elt;
20578 int len = 0;
20579
20580 while (CONSP (elt)
20581 && (precision <= 0 || n < precision))
20582 {
20583 n += display_mode_element (it, depth,
20584 /* Do padding only after the last
20585 element in the list. */
20586 (! CONSP (XCDR (elt))
20587 ? field_width - n
20588 : 0),
20589 precision - n, XCAR (elt),
20590 props, risky);
20591 elt = XCDR (elt);
20592 len++;
20593 if ((len & 1) == 0)
20594 halftail = XCDR (halftail);
20595 /* Check for cycle. */
20596 if (EQ (halftail, elt))
20597 break;
20598 }
20599 }
20600 }
20601 break;
20602
20603 default:
20604 invalid:
20605 elt = build_string ("*invalid*");
20606 goto tail_recurse;
20607 }
20608
20609 /* Pad to FIELD_WIDTH. */
20610 if (field_width > 0 && n < field_width)
20611 {
20612 switch (mode_line_target)
20613 {
20614 case MODE_LINE_NOPROP:
20615 case MODE_LINE_TITLE:
20616 n += store_mode_line_noprop ("", field_width - n, 0);
20617 break;
20618 case MODE_LINE_STRING:
20619 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20620 break;
20621 case MODE_LINE_DISPLAY:
20622 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20623 0, 0, 0);
20624 break;
20625 }
20626 }
20627
20628 return n;
20629 }
20630
20631 /* Store a mode-line string element in mode_line_string_list.
20632
20633 If STRING is non-null, display that C string. Otherwise, the Lisp
20634 string LISP_STRING is displayed.
20635
20636 FIELD_WIDTH is the minimum number of output glyphs to produce.
20637 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20638 with spaces. FIELD_WIDTH <= 0 means don't pad.
20639
20640 PRECISION is the maximum number of characters to output from
20641 STRING. PRECISION <= 0 means don't truncate the string.
20642
20643 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20644 properties to the string.
20645
20646 PROPS are the properties to add to the string.
20647 The mode_line_string_face face property is always added to the string.
20648 */
20649
20650 static int
20651 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20652 int field_width, int precision, Lisp_Object props)
20653 {
20654 EMACS_INT len;
20655 int n = 0;
20656
20657 if (string != NULL)
20658 {
20659 len = strlen (string);
20660 if (precision > 0 && len > precision)
20661 len = precision;
20662 lisp_string = make_string (string, len);
20663 if (NILP (props))
20664 props = mode_line_string_face_prop;
20665 else if (!NILP (mode_line_string_face))
20666 {
20667 Lisp_Object face = Fplist_get (props, Qface);
20668 props = Fcopy_sequence (props);
20669 if (NILP (face))
20670 face = mode_line_string_face;
20671 else
20672 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20673 props = Fplist_put (props, Qface, face);
20674 }
20675 Fadd_text_properties (make_number (0), make_number (len),
20676 props, lisp_string);
20677 }
20678 else
20679 {
20680 len = XFASTINT (Flength (lisp_string));
20681 if (precision > 0 && len > precision)
20682 {
20683 len = precision;
20684 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20685 precision = -1;
20686 }
20687 if (!NILP (mode_line_string_face))
20688 {
20689 Lisp_Object face;
20690 if (NILP (props))
20691 props = Ftext_properties_at (make_number (0), lisp_string);
20692 face = Fplist_get (props, Qface);
20693 if (NILP (face))
20694 face = mode_line_string_face;
20695 else
20696 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20697 props = Fcons (Qface, Fcons (face, Qnil));
20698 if (copy_string)
20699 lisp_string = Fcopy_sequence (lisp_string);
20700 }
20701 if (!NILP (props))
20702 Fadd_text_properties (make_number (0), make_number (len),
20703 props, lisp_string);
20704 }
20705
20706 if (len > 0)
20707 {
20708 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20709 n += len;
20710 }
20711
20712 if (field_width > len)
20713 {
20714 field_width -= len;
20715 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20716 if (!NILP (props))
20717 Fadd_text_properties (make_number (0), make_number (field_width),
20718 props, lisp_string);
20719 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20720 n += field_width;
20721 }
20722
20723 return n;
20724 }
20725
20726
20727 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20728 1, 4, 0,
20729 doc: /* Format a string out of a mode line format specification.
20730 First arg FORMAT specifies the mode line format (see `mode-line-format'
20731 for details) to use.
20732
20733 By default, the format is evaluated for the currently selected window.
20734
20735 Optional second arg FACE specifies the face property to put on all
20736 characters for which no face is specified. The value nil means the
20737 default face. The value t means whatever face the window's mode line
20738 currently uses (either `mode-line' or `mode-line-inactive',
20739 depending on whether the window is the selected window or not).
20740 An integer value means the value string has no text
20741 properties.
20742
20743 Optional third and fourth args WINDOW and BUFFER specify the window
20744 and buffer to use as the context for the formatting (defaults
20745 are the selected window and the WINDOW's buffer). */)
20746 (Lisp_Object format, Lisp_Object face,
20747 Lisp_Object window, Lisp_Object buffer)
20748 {
20749 struct it it;
20750 int len;
20751 struct window *w;
20752 struct buffer *old_buffer = NULL;
20753 int face_id;
20754 int no_props = INTEGERP (face);
20755 int count = SPECPDL_INDEX ();
20756 Lisp_Object str;
20757 int string_start = 0;
20758
20759 if (NILP (window))
20760 window = selected_window;
20761 CHECK_WINDOW (window);
20762 w = XWINDOW (window);
20763
20764 if (NILP (buffer))
20765 buffer = w->buffer;
20766 CHECK_BUFFER (buffer);
20767
20768 /* Make formatting the modeline a non-op when noninteractive, otherwise
20769 there will be problems later caused by a partially initialized frame. */
20770 if (NILP (format) || noninteractive)
20771 return empty_unibyte_string;
20772
20773 if (no_props)
20774 face = Qnil;
20775
20776 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20777 : EQ (face, Qt) ? (EQ (window, selected_window)
20778 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20779 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20780 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20781 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20782 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20783 : DEFAULT_FACE_ID;
20784
20785 if (XBUFFER (buffer) != current_buffer)
20786 old_buffer = current_buffer;
20787
20788 /* Save things including mode_line_proptrans_alist,
20789 and set that to nil so that we don't alter the outer value. */
20790 record_unwind_protect (unwind_format_mode_line,
20791 format_mode_line_unwind_data
20792 (old_buffer, selected_window, 1));
20793 mode_line_proptrans_alist = Qnil;
20794
20795 Fselect_window (window, Qt);
20796 if (old_buffer)
20797 set_buffer_internal_1 (XBUFFER (buffer));
20798
20799 init_iterator (&it, w, -1, -1, NULL, face_id);
20800
20801 if (no_props)
20802 {
20803 mode_line_target = MODE_LINE_NOPROP;
20804 mode_line_string_face_prop = Qnil;
20805 mode_line_string_list = Qnil;
20806 string_start = MODE_LINE_NOPROP_LEN (0);
20807 }
20808 else
20809 {
20810 mode_line_target = MODE_LINE_STRING;
20811 mode_line_string_list = Qnil;
20812 mode_line_string_face = face;
20813 mode_line_string_face_prop
20814 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20815 }
20816
20817 push_kboard (FRAME_KBOARD (it.f));
20818 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20819 pop_kboard ();
20820
20821 if (no_props)
20822 {
20823 len = MODE_LINE_NOPROP_LEN (string_start);
20824 str = make_string (mode_line_noprop_buf + string_start, len);
20825 }
20826 else
20827 {
20828 mode_line_string_list = Fnreverse (mode_line_string_list);
20829 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20830 empty_unibyte_string);
20831 }
20832
20833 unbind_to (count, Qnil);
20834 return str;
20835 }
20836
20837 /* Write a null-terminated, right justified decimal representation of
20838 the positive integer D to BUF using a minimal field width WIDTH. */
20839
20840 static void
20841 pint2str (register char *buf, register int width, register EMACS_INT d)
20842 {
20843 register char *p = buf;
20844
20845 if (d <= 0)
20846 *p++ = '0';
20847 else
20848 {
20849 while (d > 0)
20850 {
20851 *p++ = d % 10 + '0';
20852 d /= 10;
20853 }
20854 }
20855
20856 for (width -= (int) (p - buf); width > 0; --width)
20857 *p++ = ' ';
20858 *p-- = '\0';
20859 while (p > buf)
20860 {
20861 d = *buf;
20862 *buf++ = *p;
20863 *p-- = d;
20864 }
20865 }
20866
20867 /* Write a null-terminated, right justified decimal and "human
20868 readable" representation of the nonnegative integer D to BUF using
20869 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20870
20871 static const char power_letter[] =
20872 {
20873 0, /* no letter */
20874 'k', /* kilo */
20875 'M', /* mega */
20876 'G', /* giga */
20877 'T', /* tera */
20878 'P', /* peta */
20879 'E', /* exa */
20880 'Z', /* zetta */
20881 'Y' /* yotta */
20882 };
20883
20884 static void
20885 pint2hrstr (char *buf, int width, EMACS_INT d)
20886 {
20887 /* We aim to represent the nonnegative integer D as
20888 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20889 EMACS_INT quotient = d;
20890 int remainder = 0;
20891 /* -1 means: do not use TENTHS. */
20892 int tenths = -1;
20893 int exponent = 0;
20894
20895 /* Length of QUOTIENT.TENTHS as a string. */
20896 int length;
20897
20898 char * psuffix;
20899 char * p;
20900
20901 if (1000 <= quotient)
20902 {
20903 /* Scale to the appropriate EXPONENT. */
20904 do
20905 {
20906 remainder = quotient % 1000;
20907 quotient /= 1000;
20908 exponent++;
20909 }
20910 while (1000 <= quotient);
20911
20912 /* Round to nearest and decide whether to use TENTHS or not. */
20913 if (quotient <= 9)
20914 {
20915 tenths = remainder / 100;
20916 if (50 <= remainder % 100)
20917 {
20918 if (tenths < 9)
20919 tenths++;
20920 else
20921 {
20922 quotient++;
20923 if (quotient == 10)
20924 tenths = -1;
20925 else
20926 tenths = 0;
20927 }
20928 }
20929 }
20930 else
20931 if (500 <= remainder)
20932 {
20933 if (quotient < 999)
20934 quotient++;
20935 else
20936 {
20937 quotient = 1;
20938 exponent++;
20939 tenths = 0;
20940 }
20941 }
20942 }
20943
20944 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20945 if (tenths == -1 && quotient <= 99)
20946 if (quotient <= 9)
20947 length = 1;
20948 else
20949 length = 2;
20950 else
20951 length = 3;
20952 p = psuffix = buf + max (width, length);
20953
20954 /* Print EXPONENT. */
20955 *psuffix++ = power_letter[exponent];
20956 *psuffix = '\0';
20957
20958 /* Print TENTHS. */
20959 if (tenths >= 0)
20960 {
20961 *--p = '0' + tenths;
20962 *--p = '.';
20963 }
20964
20965 /* Print QUOTIENT. */
20966 do
20967 {
20968 int digit = quotient % 10;
20969 *--p = '0' + digit;
20970 }
20971 while ((quotient /= 10) != 0);
20972
20973 /* Print leading spaces. */
20974 while (buf < p)
20975 *--p = ' ';
20976 }
20977
20978 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20979 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20980 type of CODING_SYSTEM. Return updated pointer into BUF. */
20981
20982 static unsigned char invalid_eol_type[] = "(*invalid*)";
20983
20984 static char *
20985 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20986 {
20987 Lisp_Object val;
20988 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20989 const unsigned char *eol_str;
20990 int eol_str_len;
20991 /* The EOL conversion we are using. */
20992 Lisp_Object eoltype;
20993
20994 val = CODING_SYSTEM_SPEC (coding_system);
20995 eoltype = Qnil;
20996
20997 if (!VECTORP (val)) /* Not yet decided. */
20998 {
20999 if (multibyte)
21000 *buf++ = '-';
21001 if (eol_flag)
21002 eoltype = eol_mnemonic_undecided;
21003 /* Don't mention EOL conversion if it isn't decided. */
21004 }
21005 else
21006 {
21007 Lisp_Object attrs;
21008 Lisp_Object eolvalue;
21009
21010 attrs = AREF (val, 0);
21011 eolvalue = AREF (val, 2);
21012
21013 if (multibyte)
21014 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
21015
21016 if (eol_flag)
21017 {
21018 /* The EOL conversion that is normal on this system. */
21019
21020 if (NILP (eolvalue)) /* Not yet decided. */
21021 eoltype = eol_mnemonic_undecided;
21022 else if (VECTORP (eolvalue)) /* Not yet decided. */
21023 eoltype = eol_mnemonic_undecided;
21024 else /* eolvalue is Qunix, Qdos, or Qmac. */
21025 eoltype = (EQ (eolvalue, Qunix)
21026 ? eol_mnemonic_unix
21027 : (EQ (eolvalue, Qdos) == 1
21028 ? eol_mnemonic_dos : eol_mnemonic_mac));
21029 }
21030 }
21031
21032 if (eol_flag)
21033 {
21034 /* Mention the EOL conversion if it is not the usual one. */
21035 if (STRINGP (eoltype))
21036 {
21037 eol_str = SDATA (eoltype);
21038 eol_str_len = SBYTES (eoltype);
21039 }
21040 else if (CHARACTERP (eoltype))
21041 {
21042 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21043 int c = XFASTINT (eoltype);
21044 eol_str_len = CHAR_STRING (c, tmp);
21045 eol_str = tmp;
21046 }
21047 else
21048 {
21049 eol_str = invalid_eol_type;
21050 eol_str_len = sizeof (invalid_eol_type) - 1;
21051 }
21052 memcpy (buf, eol_str, eol_str_len);
21053 buf += eol_str_len;
21054 }
21055
21056 return buf;
21057 }
21058
21059 /* Return a string for the output of a mode line %-spec for window W,
21060 generated by character C. FIELD_WIDTH > 0 means pad the string
21061 returned with spaces to that value. Return a Lisp string in
21062 *STRING if the resulting string is taken from that Lisp string.
21063
21064 Note we operate on the current buffer for most purposes,
21065 the exception being w->base_line_pos. */
21066
21067 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21068
21069 static const char *
21070 decode_mode_spec (struct window *w, register int c, int field_width,
21071 Lisp_Object *string)
21072 {
21073 Lisp_Object obj;
21074 struct frame *f = XFRAME (WINDOW_FRAME (w));
21075 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21076 struct buffer *b = current_buffer;
21077
21078 obj = Qnil;
21079 *string = Qnil;
21080
21081 switch (c)
21082 {
21083 case '*':
21084 if (!NILP (BVAR (b, read_only)))
21085 return "%";
21086 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21087 return "*";
21088 return "-";
21089
21090 case '+':
21091 /* This differs from %* only for a modified read-only buffer. */
21092 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21093 return "*";
21094 if (!NILP (BVAR (b, read_only)))
21095 return "%";
21096 return "-";
21097
21098 case '&':
21099 /* This differs from %* in ignoring read-only-ness. */
21100 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21101 return "*";
21102 return "-";
21103
21104 case '%':
21105 return "%";
21106
21107 case '[':
21108 {
21109 int i;
21110 char *p;
21111
21112 if (command_loop_level > 5)
21113 return "[[[... ";
21114 p = decode_mode_spec_buf;
21115 for (i = 0; i < command_loop_level; i++)
21116 *p++ = '[';
21117 *p = 0;
21118 return decode_mode_spec_buf;
21119 }
21120
21121 case ']':
21122 {
21123 int i;
21124 char *p;
21125
21126 if (command_loop_level > 5)
21127 return " ...]]]";
21128 p = decode_mode_spec_buf;
21129 for (i = 0; i < command_loop_level; i++)
21130 *p++ = ']';
21131 *p = 0;
21132 return decode_mode_spec_buf;
21133 }
21134
21135 case '-':
21136 {
21137 register int i;
21138
21139 /* Let lots_of_dashes be a string of infinite length. */
21140 if (mode_line_target == MODE_LINE_NOPROP ||
21141 mode_line_target == MODE_LINE_STRING)
21142 return "--";
21143 if (field_width <= 0
21144 || field_width > sizeof (lots_of_dashes))
21145 {
21146 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21147 decode_mode_spec_buf[i] = '-';
21148 decode_mode_spec_buf[i] = '\0';
21149 return decode_mode_spec_buf;
21150 }
21151 else
21152 return lots_of_dashes;
21153 }
21154
21155 case 'b':
21156 obj = BVAR (b, name);
21157 break;
21158
21159 case 'c':
21160 /* %c and %l are ignored in `frame-title-format'.
21161 (In redisplay_internal, the frame title is drawn _before_ the
21162 windows are updated, so the stuff which depends on actual
21163 window contents (such as %l) may fail to render properly, or
21164 even crash emacs.) */
21165 if (mode_line_target == MODE_LINE_TITLE)
21166 return "";
21167 else
21168 {
21169 EMACS_INT col = current_column ();
21170 w->column_number_displayed = make_number (col);
21171 pint2str (decode_mode_spec_buf, field_width, col);
21172 return decode_mode_spec_buf;
21173 }
21174
21175 case 'e':
21176 #ifndef SYSTEM_MALLOC
21177 {
21178 if (NILP (Vmemory_full))
21179 return "";
21180 else
21181 return "!MEM FULL! ";
21182 }
21183 #else
21184 return "";
21185 #endif
21186
21187 case 'F':
21188 /* %F displays the frame name. */
21189 if (!NILP (f->title))
21190 return SSDATA (f->title);
21191 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21192 return SSDATA (f->name);
21193 return "Emacs";
21194
21195 case 'f':
21196 obj = BVAR (b, filename);
21197 break;
21198
21199 case 'i':
21200 {
21201 EMACS_INT size = ZV - BEGV;
21202 pint2str (decode_mode_spec_buf, field_width, size);
21203 return decode_mode_spec_buf;
21204 }
21205
21206 case 'I':
21207 {
21208 EMACS_INT size = ZV - BEGV;
21209 pint2hrstr (decode_mode_spec_buf, field_width, size);
21210 return decode_mode_spec_buf;
21211 }
21212
21213 case 'l':
21214 {
21215 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21216 EMACS_INT topline, nlines, height;
21217 EMACS_INT junk;
21218
21219 /* %c and %l are ignored in `frame-title-format'. */
21220 if (mode_line_target == MODE_LINE_TITLE)
21221 return "";
21222
21223 startpos = XMARKER (w->start)->charpos;
21224 startpos_byte = marker_byte_position (w->start);
21225 height = WINDOW_TOTAL_LINES (w);
21226
21227 /* If we decided that this buffer isn't suitable for line numbers,
21228 don't forget that too fast. */
21229 if (EQ (w->base_line_pos, w->buffer))
21230 goto no_value;
21231 /* But do forget it, if the window shows a different buffer now. */
21232 else if (BUFFERP (w->base_line_pos))
21233 w->base_line_pos = Qnil;
21234
21235 /* If the buffer is very big, don't waste time. */
21236 if (INTEGERP (Vline_number_display_limit)
21237 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21238 {
21239 w->base_line_pos = Qnil;
21240 w->base_line_number = Qnil;
21241 goto no_value;
21242 }
21243
21244 if (INTEGERP (w->base_line_number)
21245 && INTEGERP (w->base_line_pos)
21246 && XFASTINT (w->base_line_pos) <= startpos)
21247 {
21248 line = XFASTINT (w->base_line_number);
21249 linepos = XFASTINT (w->base_line_pos);
21250 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21251 }
21252 else
21253 {
21254 line = 1;
21255 linepos = BUF_BEGV (b);
21256 linepos_byte = BUF_BEGV_BYTE (b);
21257 }
21258
21259 /* Count lines from base line to window start position. */
21260 nlines = display_count_lines (linepos_byte,
21261 startpos_byte,
21262 startpos, &junk);
21263
21264 topline = nlines + line;
21265
21266 /* Determine a new base line, if the old one is too close
21267 or too far away, or if we did not have one.
21268 "Too close" means it's plausible a scroll-down would
21269 go back past it. */
21270 if (startpos == BUF_BEGV (b))
21271 {
21272 w->base_line_number = make_number (topline);
21273 w->base_line_pos = make_number (BUF_BEGV (b));
21274 }
21275 else if (nlines < height + 25 || nlines > height * 3 + 50
21276 || linepos == BUF_BEGV (b))
21277 {
21278 EMACS_INT limit = BUF_BEGV (b);
21279 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21280 EMACS_INT position;
21281 EMACS_INT distance =
21282 (height * 2 + 30) * line_number_display_limit_width;
21283
21284 if (startpos - distance > limit)
21285 {
21286 limit = startpos - distance;
21287 limit_byte = CHAR_TO_BYTE (limit);
21288 }
21289
21290 nlines = display_count_lines (startpos_byte,
21291 limit_byte,
21292 - (height * 2 + 30),
21293 &position);
21294 /* If we couldn't find the lines we wanted within
21295 line_number_display_limit_width chars per line,
21296 give up on line numbers for this window. */
21297 if (position == limit_byte && limit == startpos - distance)
21298 {
21299 w->base_line_pos = w->buffer;
21300 w->base_line_number = Qnil;
21301 goto no_value;
21302 }
21303
21304 w->base_line_number = make_number (topline - nlines);
21305 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21306 }
21307
21308 /* Now count lines from the start pos to point. */
21309 nlines = display_count_lines (startpos_byte,
21310 PT_BYTE, PT, &junk);
21311
21312 /* Record that we did display the line number. */
21313 line_number_displayed = 1;
21314
21315 /* Make the string to show. */
21316 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21317 return decode_mode_spec_buf;
21318 no_value:
21319 {
21320 char* p = decode_mode_spec_buf;
21321 int pad = field_width - 2;
21322 while (pad-- > 0)
21323 *p++ = ' ';
21324 *p++ = '?';
21325 *p++ = '?';
21326 *p = '\0';
21327 return decode_mode_spec_buf;
21328 }
21329 }
21330 break;
21331
21332 case 'm':
21333 obj = BVAR (b, mode_name);
21334 break;
21335
21336 case 'n':
21337 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21338 return " Narrow";
21339 break;
21340
21341 case 'p':
21342 {
21343 EMACS_INT pos = marker_position (w->start);
21344 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21345
21346 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21347 {
21348 if (pos <= BUF_BEGV (b))
21349 return "All";
21350 else
21351 return "Bottom";
21352 }
21353 else if (pos <= BUF_BEGV (b))
21354 return "Top";
21355 else
21356 {
21357 if (total > 1000000)
21358 /* Do it differently for a large value, to avoid overflow. */
21359 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21360 else
21361 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21362 /* We can't normally display a 3-digit number,
21363 so get us a 2-digit number that is close. */
21364 if (total == 100)
21365 total = 99;
21366 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21367 return decode_mode_spec_buf;
21368 }
21369 }
21370
21371 /* Display percentage of size above the bottom of the screen. */
21372 case 'P':
21373 {
21374 EMACS_INT toppos = marker_position (w->start);
21375 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21376 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21377
21378 if (botpos >= BUF_ZV (b))
21379 {
21380 if (toppos <= BUF_BEGV (b))
21381 return "All";
21382 else
21383 return "Bottom";
21384 }
21385 else
21386 {
21387 if (total > 1000000)
21388 /* Do it differently for a large value, to avoid overflow. */
21389 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21390 else
21391 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21392 /* We can't normally display a 3-digit number,
21393 so get us a 2-digit number that is close. */
21394 if (total == 100)
21395 total = 99;
21396 if (toppos <= BUF_BEGV (b))
21397 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21398 else
21399 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21400 return decode_mode_spec_buf;
21401 }
21402 }
21403
21404 case 's':
21405 /* status of process */
21406 obj = Fget_buffer_process (Fcurrent_buffer ());
21407 if (NILP (obj))
21408 return "no process";
21409 #ifndef MSDOS
21410 obj = Fsymbol_name (Fprocess_status (obj));
21411 #endif
21412 break;
21413
21414 case '@':
21415 {
21416 int count = inhibit_garbage_collection ();
21417 Lisp_Object val = call1 (intern ("file-remote-p"),
21418 BVAR (current_buffer, directory));
21419 unbind_to (count, Qnil);
21420
21421 if (NILP (val))
21422 return "-";
21423 else
21424 return "@";
21425 }
21426
21427 case 't': /* indicate TEXT or BINARY */
21428 return "T";
21429
21430 case 'z':
21431 /* coding-system (not including end-of-line format) */
21432 case 'Z':
21433 /* coding-system (including end-of-line type) */
21434 {
21435 int eol_flag = (c == 'Z');
21436 char *p = decode_mode_spec_buf;
21437
21438 if (! FRAME_WINDOW_P (f))
21439 {
21440 /* No need to mention EOL here--the terminal never needs
21441 to do EOL conversion. */
21442 p = decode_mode_spec_coding (CODING_ID_NAME
21443 (FRAME_KEYBOARD_CODING (f)->id),
21444 p, 0);
21445 p = decode_mode_spec_coding (CODING_ID_NAME
21446 (FRAME_TERMINAL_CODING (f)->id),
21447 p, 0);
21448 }
21449 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21450 p, eol_flag);
21451
21452 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21453 #ifdef subprocesses
21454 obj = Fget_buffer_process (Fcurrent_buffer ());
21455 if (PROCESSP (obj))
21456 {
21457 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21458 p, eol_flag);
21459 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21460 p, eol_flag);
21461 }
21462 #endif /* subprocesses */
21463 #endif /* 0 */
21464 *p = 0;
21465 return decode_mode_spec_buf;
21466 }
21467 }
21468
21469 if (STRINGP (obj))
21470 {
21471 *string = obj;
21472 return SSDATA (obj);
21473 }
21474 else
21475 return "";
21476 }
21477
21478
21479 /* Count up to COUNT lines starting from START_BYTE.
21480 But don't go beyond LIMIT_BYTE.
21481 Return the number of lines thus found (always nonnegative).
21482
21483 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21484
21485 static EMACS_INT
21486 display_count_lines (EMACS_INT start_byte,
21487 EMACS_INT limit_byte, EMACS_INT count,
21488 EMACS_INT *byte_pos_ptr)
21489 {
21490 register unsigned char *cursor;
21491 unsigned char *base;
21492
21493 register EMACS_INT ceiling;
21494 register unsigned char *ceiling_addr;
21495 EMACS_INT orig_count = count;
21496
21497 /* If we are not in selective display mode,
21498 check only for newlines. */
21499 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21500 && !INTEGERP (BVAR (current_buffer, selective_display)));
21501
21502 if (count > 0)
21503 {
21504 while (start_byte < limit_byte)
21505 {
21506 ceiling = BUFFER_CEILING_OF (start_byte);
21507 ceiling = min (limit_byte - 1, ceiling);
21508 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21509 base = (cursor = BYTE_POS_ADDR (start_byte));
21510 while (1)
21511 {
21512 if (selective_display)
21513 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21514 ;
21515 else
21516 while (*cursor != '\n' && ++cursor != ceiling_addr)
21517 ;
21518
21519 if (cursor != ceiling_addr)
21520 {
21521 if (--count == 0)
21522 {
21523 start_byte += cursor - base + 1;
21524 *byte_pos_ptr = start_byte;
21525 return orig_count;
21526 }
21527 else
21528 if (++cursor == ceiling_addr)
21529 break;
21530 }
21531 else
21532 break;
21533 }
21534 start_byte += cursor - base;
21535 }
21536 }
21537 else
21538 {
21539 while (start_byte > limit_byte)
21540 {
21541 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21542 ceiling = max (limit_byte, ceiling);
21543 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21544 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21545 while (1)
21546 {
21547 if (selective_display)
21548 while (--cursor != ceiling_addr
21549 && *cursor != '\n' && *cursor != 015)
21550 ;
21551 else
21552 while (--cursor != ceiling_addr && *cursor != '\n')
21553 ;
21554
21555 if (cursor != ceiling_addr)
21556 {
21557 if (++count == 0)
21558 {
21559 start_byte += cursor - base + 1;
21560 *byte_pos_ptr = start_byte;
21561 /* When scanning backwards, we should
21562 not count the newline posterior to which we stop. */
21563 return - orig_count - 1;
21564 }
21565 }
21566 else
21567 break;
21568 }
21569 /* Here we add 1 to compensate for the last decrement
21570 of CURSOR, which took it past the valid range. */
21571 start_byte += cursor - base + 1;
21572 }
21573 }
21574
21575 *byte_pos_ptr = limit_byte;
21576
21577 if (count < 0)
21578 return - orig_count + count;
21579 return orig_count - count;
21580
21581 }
21582
21583
21584 \f
21585 /***********************************************************************
21586 Displaying strings
21587 ***********************************************************************/
21588
21589 /* Display a NUL-terminated string, starting with index START.
21590
21591 If STRING is non-null, display that C string. Otherwise, the Lisp
21592 string LISP_STRING is displayed. There's a case that STRING is
21593 non-null and LISP_STRING is not nil. It means STRING is a string
21594 data of LISP_STRING. In that case, we display LISP_STRING while
21595 ignoring its text properties.
21596
21597 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21598 FACE_STRING. Display STRING or LISP_STRING with the face at
21599 FACE_STRING_POS in FACE_STRING:
21600
21601 Display the string in the environment given by IT, but use the
21602 standard display table, temporarily.
21603
21604 FIELD_WIDTH is the minimum number of output glyphs to produce.
21605 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21606 with spaces. If STRING has more characters, more than FIELD_WIDTH
21607 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21608
21609 PRECISION is the maximum number of characters to output from
21610 STRING. PRECISION < 0 means don't truncate the string.
21611
21612 This is roughly equivalent to printf format specifiers:
21613
21614 FIELD_WIDTH PRECISION PRINTF
21615 ----------------------------------------
21616 -1 -1 %s
21617 -1 10 %.10s
21618 10 -1 %10s
21619 20 10 %20.10s
21620
21621 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21622 display them, and < 0 means obey the current buffer's value of
21623 enable_multibyte_characters.
21624
21625 Value is the number of columns displayed. */
21626
21627 static int
21628 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21629 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21630 int field_width, int precision, int max_x, int multibyte)
21631 {
21632 int hpos_at_start = it->hpos;
21633 int saved_face_id = it->face_id;
21634 struct glyph_row *row = it->glyph_row;
21635 EMACS_INT it_charpos;
21636
21637 /* Initialize the iterator IT for iteration over STRING beginning
21638 with index START. */
21639 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21640 precision, field_width, multibyte);
21641 if (string && STRINGP (lisp_string))
21642 /* LISP_STRING is the one returned by decode_mode_spec. We should
21643 ignore its text properties. */
21644 it->stop_charpos = it->end_charpos;
21645
21646 /* If displaying STRING, set up the face of the iterator from
21647 FACE_STRING, if that's given. */
21648 if (STRINGP (face_string))
21649 {
21650 EMACS_INT endptr;
21651 struct face *face;
21652
21653 it->face_id
21654 = face_at_string_position (it->w, face_string, face_string_pos,
21655 0, it->region_beg_charpos,
21656 it->region_end_charpos,
21657 &endptr, it->base_face_id, 0);
21658 face = FACE_FROM_ID (it->f, it->face_id);
21659 it->face_box_p = face->box != FACE_NO_BOX;
21660 }
21661
21662 /* Set max_x to the maximum allowed X position. Don't let it go
21663 beyond the right edge of the window. */
21664 if (max_x <= 0)
21665 max_x = it->last_visible_x;
21666 else
21667 max_x = min (max_x, it->last_visible_x);
21668
21669 /* Skip over display elements that are not visible. because IT->w is
21670 hscrolled. */
21671 if (it->current_x < it->first_visible_x)
21672 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21673 MOVE_TO_POS | MOVE_TO_X);
21674
21675 row->ascent = it->max_ascent;
21676 row->height = it->max_ascent + it->max_descent;
21677 row->phys_ascent = it->max_phys_ascent;
21678 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21679 row->extra_line_spacing = it->max_extra_line_spacing;
21680
21681 if (STRINGP (it->string))
21682 it_charpos = IT_STRING_CHARPOS (*it);
21683 else
21684 it_charpos = IT_CHARPOS (*it);
21685
21686 /* This condition is for the case that we are called with current_x
21687 past last_visible_x. */
21688 while (it->current_x < max_x)
21689 {
21690 int x_before, x, n_glyphs_before, i, nglyphs;
21691
21692 /* Get the next display element. */
21693 if (!get_next_display_element (it))
21694 break;
21695
21696 /* Produce glyphs. */
21697 x_before = it->current_x;
21698 n_glyphs_before = row->used[TEXT_AREA];
21699 PRODUCE_GLYPHS (it);
21700
21701 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21702 i = 0;
21703 x = x_before;
21704 while (i < nglyphs)
21705 {
21706 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21707
21708 if (it->line_wrap != TRUNCATE
21709 && x + glyph->pixel_width > max_x)
21710 {
21711 /* End of continued line or max_x reached. */
21712 if (CHAR_GLYPH_PADDING_P (*glyph))
21713 {
21714 /* A wide character is unbreakable. */
21715 if (row->reversed_p)
21716 unproduce_glyphs (it, row->used[TEXT_AREA]
21717 - n_glyphs_before);
21718 row->used[TEXT_AREA] = n_glyphs_before;
21719 it->current_x = x_before;
21720 }
21721 else
21722 {
21723 if (row->reversed_p)
21724 unproduce_glyphs (it, row->used[TEXT_AREA]
21725 - (n_glyphs_before + i));
21726 row->used[TEXT_AREA] = n_glyphs_before + i;
21727 it->current_x = x;
21728 }
21729 break;
21730 }
21731 else if (x + glyph->pixel_width >= it->first_visible_x)
21732 {
21733 /* Glyph is at least partially visible. */
21734 ++it->hpos;
21735 if (x < it->first_visible_x)
21736 row->x = x - it->first_visible_x;
21737 }
21738 else
21739 {
21740 /* Glyph is off the left margin of the display area.
21741 Should not happen. */
21742 abort ();
21743 }
21744
21745 row->ascent = max (row->ascent, it->max_ascent);
21746 row->height = max (row->height, it->max_ascent + it->max_descent);
21747 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21748 row->phys_height = max (row->phys_height,
21749 it->max_phys_ascent + it->max_phys_descent);
21750 row->extra_line_spacing = max (row->extra_line_spacing,
21751 it->max_extra_line_spacing);
21752 x += glyph->pixel_width;
21753 ++i;
21754 }
21755
21756 /* Stop if max_x reached. */
21757 if (i < nglyphs)
21758 break;
21759
21760 /* Stop at line ends. */
21761 if (ITERATOR_AT_END_OF_LINE_P (it))
21762 {
21763 it->continuation_lines_width = 0;
21764 break;
21765 }
21766
21767 set_iterator_to_next (it, 1);
21768 if (STRINGP (it->string))
21769 it_charpos = IT_STRING_CHARPOS (*it);
21770 else
21771 it_charpos = IT_CHARPOS (*it);
21772
21773 /* Stop if truncating at the right edge. */
21774 if (it->line_wrap == TRUNCATE
21775 && it->current_x >= it->last_visible_x)
21776 {
21777 /* Add truncation mark, but don't do it if the line is
21778 truncated at a padding space. */
21779 if (it_charpos < it->string_nchars)
21780 {
21781 if (!FRAME_WINDOW_P (it->f))
21782 {
21783 int ii, n;
21784
21785 if (it->current_x > it->last_visible_x)
21786 {
21787 if (!row->reversed_p)
21788 {
21789 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21790 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21791 break;
21792 }
21793 else
21794 {
21795 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21796 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21797 break;
21798 unproduce_glyphs (it, ii + 1);
21799 ii = row->used[TEXT_AREA] - (ii + 1);
21800 }
21801 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21802 {
21803 row->used[TEXT_AREA] = ii;
21804 produce_special_glyphs (it, IT_TRUNCATION);
21805 }
21806 }
21807 produce_special_glyphs (it, IT_TRUNCATION);
21808 }
21809 row->truncated_on_right_p = 1;
21810 }
21811 break;
21812 }
21813 }
21814
21815 /* Maybe insert a truncation at the left. */
21816 if (it->first_visible_x
21817 && it_charpos > 0)
21818 {
21819 if (!FRAME_WINDOW_P (it->f))
21820 insert_left_trunc_glyphs (it);
21821 row->truncated_on_left_p = 1;
21822 }
21823
21824 it->face_id = saved_face_id;
21825
21826 /* Value is number of columns displayed. */
21827 return it->hpos - hpos_at_start;
21828 }
21829
21830
21831 \f
21832 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21833 appears as an element of LIST or as the car of an element of LIST.
21834 If PROPVAL is a list, compare each element against LIST in that
21835 way, and return 1/2 if any element of PROPVAL is found in LIST.
21836 Otherwise return 0. This function cannot quit.
21837 The return value is 2 if the text is invisible but with an ellipsis
21838 and 1 if it's invisible and without an ellipsis. */
21839
21840 int
21841 invisible_p (register Lisp_Object propval, Lisp_Object list)
21842 {
21843 register Lisp_Object tail, proptail;
21844
21845 for (tail = list; CONSP (tail); tail = XCDR (tail))
21846 {
21847 register Lisp_Object tem;
21848 tem = XCAR (tail);
21849 if (EQ (propval, tem))
21850 return 1;
21851 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21852 return NILP (XCDR (tem)) ? 1 : 2;
21853 }
21854
21855 if (CONSP (propval))
21856 {
21857 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21858 {
21859 Lisp_Object propelt;
21860 propelt = XCAR (proptail);
21861 for (tail = list; CONSP (tail); tail = XCDR (tail))
21862 {
21863 register Lisp_Object tem;
21864 tem = XCAR (tail);
21865 if (EQ (propelt, tem))
21866 return 1;
21867 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21868 return NILP (XCDR (tem)) ? 1 : 2;
21869 }
21870 }
21871 }
21872
21873 return 0;
21874 }
21875
21876 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21877 doc: /* Non-nil if the property makes the text invisible.
21878 POS-OR-PROP can be a marker or number, in which case it is taken to be
21879 a position in the current buffer and the value of the `invisible' property
21880 is checked; or it can be some other value, which is then presumed to be the
21881 value of the `invisible' property of the text of interest.
21882 The non-nil value returned can be t for truly invisible text or something
21883 else if the text is replaced by an ellipsis. */)
21884 (Lisp_Object pos_or_prop)
21885 {
21886 Lisp_Object prop
21887 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21888 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21889 : pos_or_prop);
21890 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21891 return (invis == 0 ? Qnil
21892 : invis == 1 ? Qt
21893 : make_number (invis));
21894 }
21895
21896 /* Calculate a width or height in pixels from a specification using
21897 the following elements:
21898
21899 SPEC ::=
21900 NUM - a (fractional) multiple of the default font width/height
21901 (NUM) - specifies exactly NUM pixels
21902 UNIT - a fixed number of pixels, see below.
21903 ELEMENT - size of a display element in pixels, see below.
21904 (NUM . SPEC) - equals NUM * SPEC
21905 (+ SPEC SPEC ...) - add pixel values
21906 (- SPEC SPEC ...) - subtract pixel values
21907 (- SPEC) - negate pixel value
21908
21909 NUM ::=
21910 INT or FLOAT - a number constant
21911 SYMBOL - use symbol's (buffer local) variable binding.
21912
21913 UNIT ::=
21914 in - pixels per inch *)
21915 mm - pixels per 1/1000 meter *)
21916 cm - pixels per 1/100 meter *)
21917 width - width of current font in pixels.
21918 height - height of current font in pixels.
21919
21920 *) using the ratio(s) defined in display-pixels-per-inch.
21921
21922 ELEMENT ::=
21923
21924 left-fringe - left fringe width in pixels
21925 right-fringe - right fringe width in pixels
21926
21927 left-margin - left margin width in pixels
21928 right-margin - right margin width in pixels
21929
21930 scroll-bar - scroll-bar area width in pixels
21931
21932 Examples:
21933
21934 Pixels corresponding to 5 inches:
21935 (5 . in)
21936
21937 Total width of non-text areas on left side of window (if scroll-bar is on left):
21938 '(space :width (+ left-fringe left-margin scroll-bar))
21939
21940 Align to first text column (in header line):
21941 '(space :align-to 0)
21942
21943 Align to middle of text area minus half the width of variable `my-image'
21944 containing a loaded image:
21945 '(space :align-to (0.5 . (- text my-image)))
21946
21947 Width of left margin minus width of 1 character in the default font:
21948 '(space :width (- left-margin 1))
21949
21950 Width of left margin minus width of 2 characters in the current font:
21951 '(space :width (- left-margin (2 . width)))
21952
21953 Center 1 character over left-margin (in header line):
21954 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21955
21956 Different ways to express width of left fringe plus left margin minus one pixel:
21957 '(space :width (- (+ left-fringe left-margin) (1)))
21958 '(space :width (+ left-fringe left-margin (- (1))))
21959 '(space :width (+ left-fringe left-margin (-1)))
21960
21961 */
21962
21963 #define NUMVAL(X) \
21964 ((INTEGERP (X) || FLOATP (X)) \
21965 ? XFLOATINT (X) \
21966 : - 1)
21967
21968 static int
21969 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21970 struct font *font, int width_p, int *align_to)
21971 {
21972 double pixels;
21973
21974 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21975 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21976
21977 if (NILP (prop))
21978 return OK_PIXELS (0);
21979
21980 xassert (FRAME_LIVE_P (it->f));
21981
21982 if (SYMBOLP (prop))
21983 {
21984 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21985 {
21986 char *unit = SSDATA (SYMBOL_NAME (prop));
21987
21988 if (unit[0] == 'i' && unit[1] == 'n')
21989 pixels = 1.0;
21990 else if (unit[0] == 'm' && unit[1] == 'm')
21991 pixels = 25.4;
21992 else if (unit[0] == 'c' && unit[1] == 'm')
21993 pixels = 2.54;
21994 else
21995 pixels = 0;
21996 if (pixels > 0)
21997 {
21998 double ppi;
21999 #ifdef HAVE_WINDOW_SYSTEM
22000 if (FRAME_WINDOW_P (it->f)
22001 && (ppi = (width_p
22002 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22003 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22004 ppi > 0))
22005 return OK_PIXELS (ppi / pixels);
22006 #endif
22007
22008 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22009 || (CONSP (Vdisplay_pixels_per_inch)
22010 && (ppi = (width_p
22011 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22012 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22013 ppi > 0)))
22014 return OK_PIXELS (ppi / pixels);
22015
22016 return 0;
22017 }
22018 }
22019
22020 #ifdef HAVE_WINDOW_SYSTEM
22021 if (EQ (prop, Qheight))
22022 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22023 if (EQ (prop, Qwidth))
22024 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22025 #else
22026 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22027 return OK_PIXELS (1);
22028 #endif
22029
22030 if (EQ (prop, Qtext))
22031 return OK_PIXELS (width_p
22032 ? window_box_width (it->w, TEXT_AREA)
22033 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22034
22035 if (align_to && *align_to < 0)
22036 {
22037 *res = 0;
22038 if (EQ (prop, Qleft))
22039 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22040 if (EQ (prop, Qright))
22041 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22042 if (EQ (prop, Qcenter))
22043 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22044 + window_box_width (it->w, TEXT_AREA) / 2);
22045 if (EQ (prop, Qleft_fringe))
22046 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22047 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22048 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22049 if (EQ (prop, Qright_fringe))
22050 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22051 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22052 : window_box_right_offset (it->w, TEXT_AREA));
22053 if (EQ (prop, Qleft_margin))
22054 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22055 if (EQ (prop, Qright_margin))
22056 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22057 if (EQ (prop, Qscroll_bar))
22058 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22059 ? 0
22060 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22061 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22062 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22063 : 0)));
22064 }
22065 else
22066 {
22067 if (EQ (prop, Qleft_fringe))
22068 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22069 if (EQ (prop, Qright_fringe))
22070 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22071 if (EQ (prop, Qleft_margin))
22072 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22073 if (EQ (prop, Qright_margin))
22074 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22075 if (EQ (prop, Qscroll_bar))
22076 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22077 }
22078
22079 prop = Fbuffer_local_value (prop, it->w->buffer);
22080 }
22081
22082 if (INTEGERP (prop) || FLOATP (prop))
22083 {
22084 int base_unit = (width_p
22085 ? FRAME_COLUMN_WIDTH (it->f)
22086 : FRAME_LINE_HEIGHT (it->f));
22087 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22088 }
22089
22090 if (CONSP (prop))
22091 {
22092 Lisp_Object car = XCAR (prop);
22093 Lisp_Object cdr = XCDR (prop);
22094
22095 if (SYMBOLP (car))
22096 {
22097 #ifdef HAVE_WINDOW_SYSTEM
22098 if (FRAME_WINDOW_P (it->f)
22099 && valid_image_p (prop))
22100 {
22101 ptrdiff_t id = lookup_image (it->f, prop);
22102 struct image *img = IMAGE_FROM_ID (it->f, id);
22103
22104 return OK_PIXELS (width_p ? img->width : img->height);
22105 }
22106 #endif
22107 if (EQ (car, Qplus) || EQ (car, Qminus))
22108 {
22109 int first = 1;
22110 double px;
22111
22112 pixels = 0;
22113 while (CONSP (cdr))
22114 {
22115 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22116 font, width_p, align_to))
22117 return 0;
22118 if (first)
22119 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22120 else
22121 pixels += px;
22122 cdr = XCDR (cdr);
22123 }
22124 if (EQ (car, Qminus))
22125 pixels = -pixels;
22126 return OK_PIXELS (pixels);
22127 }
22128
22129 car = Fbuffer_local_value (car, it->w->buffer);
22130 }
22131
22132 if (INTEGERP (car) || FLOATP (car))
22133 {
22134 double fact;
22135 pixels = XFLOATINT (car);
22136 if (NILP (cdr))
22137 return OK_PIXELS (pixels);
22138 if (calc_pixel_width_or_height (&fact, it, cdr,
22139 font, width_p, align_to))
22140 return OK_PIXELS (pixels * fact);
22141 return 0;
22142 }
22143
22144 return 0;
22145 }
22146
22147 return 0;
22148 }
22149
22150 \f
22151 /***********************************************************************
22152 Glyph Display
22153 ***********************************************************************/
22154
22155 #ifdef HAVE_WINDOW_SYSTEM
22156
22157 #if GLYPH_DEBUG
22158
22159 void
22160 dump_glyph_string (struct glyph_string *s)
22161 {
22162 fprintf (stderr, "glyph string\n");
22163 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22164 s->x, s->y, s->width, s->height);
22165 fprintf (stderr, " ybase = %d\n", s->ybase);
22166 fprintf (stderr, " hl = %d\n", s->hl);
22167 fprintf (stderr, " left overhang = %d, right = %d\n",
22168 s->left_overhang, s->right_overhang);
22169 fprintf (stderr, " nchars = %d\n", s->nchars);
22170 fprintf (stderr, " extends to end of line = %d\n",
22171 s->extends_to_end_of_line_p);
22172 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22173 fprintf (stderr, " bg width = %d\n", s->background_width);
22174 }
22175
22176 #endif /* GLYPH_DEBUG */
22177
22178 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22179 of XChar2b structures for S; it can't be allocated in
22180 init_glyph_string because it must be allocated via `alloca'. W
22181 is the window on which S is drawn. ROW and AREA are the glyph row
22182 and area within the row from which S is constructed. START is the
22183 index of the first glyph structure covered by S. HL is a
22184 face-override for drawing S. */
22185
22186 #ifdef HAVE_NTGUI
22187 #define OPTIONAL_HDC(hdc) HDC hdc,
22188 #define DECLARE_HDC(hdc) HDC hdc;
22189 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22190 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22191 #endif
22192
22193 #ifndef OPTIONAL_HDC
22194 #define OPTIONAL_HDC(hdc)
22195 #define DECLARE_HDC(hdc)
22196 #define ALLOCATE_HDC(hdc, f)
22197 #define RELEASE_HDC(hdc, f)
22198 #endif
22199
22200 static void
22201 init_glyph_string (struct glyph_string *s,
22202 OPTIONAL_HDC (hdc)
22203 XChar2b *char2b, struct window *w, struct glyph_row *row,
22204 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22205 {
22206 memset (s, 0, sizeof *s);
22207 s->w = w;
22208 s->f = XFRAME (w->frame);
22209 #ifdef HAVE_NTGUI
22210 s->hdc = hdc;
22211 #endif
22212 s->display = FRAME_X_DISPLAY (s->f);
22213 s->window = FRAME_X_WINDOW (s->f);
22214 s->char2b = char2b;
22215 s->hl = hl;
22216 s->row = row;
22217 s->area = area;
22218 s->first_glyph = row->glyphs[area] + start;
22219 s->height = row->height;
22220 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22221 s->ybase = s->y + row->ascent;
22222 }
22223
22224
22225 /* Append the list of glyph strings with head H and tail T to the list
22226 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22227
22228 static inline void
22229 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22230 struct glyph_string *h, struct glyph_string *t)
22231 {
22232 if (h)
22233 {
22234 if (*head)
22235 (*tail)->next = h;
22236 else
22237 *head = h;
22238 h->prev = *tail;
22239 *tail = t;
22240 }
22241 }
22242
22243
22244 /* Prepend the list of glyph strings with head H and tail T to the
22245 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22246 result. */
22247
22248 static inline void
22249 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22250 struct glyph_string *h, struct glyph_string *t)
22251 {
22252 if (h)
22253 {
22254 if (*head)
22255 (*head)->prev = t;
22256 else
22257 *tail = t;
22258 t->next = *head;
22259 *head = h;
22260 }
22261 }
22262
22263
22264 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22265 Set *HEAD and *TAIL to the resulting list. */
22266
22267 static inline void
22268 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22269 struct glyph_string *s)
22270 {
22271 s->next = s->prev = NULL;
22272 append_glyph_string_lists (head, tail, s, s);
22273 }
22274
22275
22276 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22277 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22278 make sure that X resources for the face returned are allocated.
22279 Value is a pointer to a realized face that is ready for display if
22280 DISPLAY_P is non-zero. */
22281
22282 static inline struct face *
22283 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22284 XChar2b *char2b, int display_p)
22285 {
22286 struct face *face = FACE_FROM_ID (f, face_id);
22287
22288 if (face->font)
22289 {
22290 unsigned code = face->font->driver->encode_char (face->font, c);
22291
22292 if (code != FONT_INVALID_CODE)
22293 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22294 else
22295 STORE_XCHAR2B (char2b, 0, 0);
22296 }
22297
22298 /* Make sure X resources of the face are allocated. */
22299 #ifdef HAVE_X_WINDOWS
22300 if (display_p)
22301 #endif
22302 {
22303 xassert (face != NULL);
22304 PREPARE_FACE_FOR_DISPLAY (f, face);
22305 }
22306
22307 return face;
22308 }
22309
22310
22311 /* Get face and two-byte form of character glyph GLYPH on frame F.
22312 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22313 a pointer to a realized face that is ready for display. */
22314
22315 static inline struct face *
22316 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22317 XChar2b *char2b, int *two_byte_p)
22318 {
22319 struct face *face;
22320
22321 xassert (glyph->type == CHAR_GLYPH);
22322 face = FACE_FROM_ID (f, glyph->face_id);
22323
22324 if (two_byte_p)
22325 *two_byte_p = 0;
22326
22327 if (face->font)
22328 {
22329 unsigned code;
22330
22331 if (CHAR_BYTE8_P (glyph->u.ch))
22332 code = CHAR_TO_BYTE8 (glyph->u.ch);
22333 else
22334 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22335
22336 if (code != FONT_INVALID_CODE)
22337 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22338 else
22339 STORE_XCHAR2B (char2b, 0, 0);
22340 }
22341
22342 /* Make sure X resources of the face are allocated. */
22343 xassert (face != NULL);
22344 PREPARE_FACE_FOR_DISPLAY (f, face);
22345 return face;
22346 }
22347
22348
22349 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22350 Return 1 if FONT has a glyph for C, otherwise return 0. */
22351
22352 static inline int
22353 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22354 {
22355 unsigned code;
22356
22357 if (CHAR_BYTE8_P (c))
22358 code = CHAR_TO_BYTE8 (c);
22359 else
22360 code = font->driver->encode_char (font, c);
22361
22362 if (code == FONT_INVALID_CODE)
22363 return 0;
22364 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22365 return 1;
22366 }
22367
22368
22369 /* Fill glyph string S with composition components specified by S->cmp.
22370
22371 BASE_FACE is the base face of the composition.
22372 S->cmp_from is the index of the first component for S.
22373
22374 OVERLAPS non-zero means S should draw the foreground only, and use
22375 its physical height for clipping. See also draw_glyphs.
22376
22377 Value is the index of a component not in S. */
22378
22379 static int
22380 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22381 int overlaps)
22382 {
22383 int i;
22384 /* For all glyphs of this composition, starting at the offset
22385 S->cmp_from, until we reach the end of the definition or encounter a
22386 glyph that requires the different face, add it to S. */
22387 struct face *face;
22388
22389 xassert (s);
22390
22391 s->for_overlaps = overlaps;
22392 s->face = NULL;
22393 s->font = NULL;
22394 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22395 {
22396 int c = COMPOSITION_GLYPH (s->cmp, i);
22397
22398 /* TAB in a composition means display glyphs with padding space
22399 on the left or right. */
22400 if (c != '\t')
22401 {
22402 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22403 -1, Qnil);
22404
22405 face = get_char_face_and_encoding (s->f, c, face_id,
22406 s->char2b + i, 1);
22407 if (face)
22408 {
22409 if (! s->face)
22410 {
22411 s->face = face;
22412 s->font = s->face->font;
22413 }
22414 else if (s->face != face)
22415 break;
22416 }
22417 }
22418 ++s->nchars;
22419 }
22420 s->cmp_to = i;
22421
22422 if (s->face == NULL)
22423 {
22424 s->face = base_face->ascii_face;
22425 s->font = s->face->font;
22426 }
22427
22428 /* All glyph strings for the same composition has the same width,
22429 i.e. the width set for the first component of the composition. */
22430 s->width = s->first_glyph->pixel_width;
22431
22432 /* If the specified font could not be loaded, use the frame's
22433 default font, but record the fact that we couldn't load it in
22434 the glyph string so that we can draw rectangles for the
22435 characters of the glyph string. */
22436 if (s->font == NULL)
22437 {
22438 s->font_not_found_p = 1;
22439 s->font = FRAME_FONT (s->f);
22440 }
22441
22442 /* Adjust base line for subscript/superscript text. */
22443 s->ybase += s->first_glyph->voffset;
22444
22445 /* This glyph string must always be drawn with 16-bit functions. */
22446 s->two_byte_p = 1;
22447
22448 return s->cmp_to;
22449 }
22450
22451 static int
22452 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22453 int start, int end, int overlaps)
22454 {
22455 struct glyph *glyph, *last;
22456 Lisp_Object lgstring;
22457 int i;
22458
22459 s->for_overlaps = overlaps;
22460 glyph = s->row->glyphs[s->area] + start;
22461 last = s->row->glyphs[s->area] + end;
22462 s->cmp_id = glyph->u.cmp.id;
22463 s->cmp_from = glyph->slice.cmp.from;
22464 s->cmp_to = glyph->slice.cmp.to + 1;
22465 s->face = FACE_FROM_ID (s->f, face_id);
22466 lgstring = composition_gstring_from_id (s->cmp_id);
22467 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22468 glyph++;
22469 while (glyph < last
22470 && glyph->u.cmp.automatic
22471 && glyph->u.cmp.id == s->cmp_id
22472 && s->cmp_to == glyph->slice.cmp.from)
22473 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22474
22475 for (i = s->cmp_from; i < s->cmp_to; i++)
22476 {
22477 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22478 unsigned code = LGLYPH_CODE (lglyph);
22479
22480 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22481 }
22482 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22483 return glyph - s->row->glyphs[s->area];
22484 }
22485
22486
22487 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22488 See the comment of fill_glyph_string for arguments.
22489 Value is the index of the first glyph not in S. */
22490
22491
22492 static int
22493 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22494 int start, int end, int overlaps)
22495 {
22496 struct glyph *glyph, *last;
22497 int voffset;
22498
22499 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22500 s->for_overlaps = overlaps;
22501 glyph = s->row->glyphs[s->area] + start;
22502 last = s->row->glyphs[s->area] + end;
22503 voffset = glyph->voffset;
22504 s->face = FACE_FROM_ID (s->f, face_id);
22505 s->font = s->face->font;
22506 s->nchars = 1;
22507 s->width = glyph->pixel_width;
22508 glyph++;
22509 while (glyph < last
22510 && glyph->type == GLYPHLESS_GLYPH
22511 && glyph->voffset == voffset
22512 && glyph->face_id == face_id)
22513 {
22514 s->nchars++;
22515 s->width += glyph->pixel_width;
22516 glyph++;
22517 }
22518 s->ybase += voffset;
22519 return glyph - s->row->glyphs[s->area];
22520 }
22521
22522
22523 /* Fill glyph string S from a sequence of character glyphs.
22524
22525 FACE_ID is the face id of the string. START is the index of the
22526 first glyph to consider, END is the index of the last + 1.
22527 OVERLAPS non-zero means S should draw the foreground only, and use
22528 its physical height for clipping. See also draw_glyphs.
22529
22530 Value is the index of the first glyph not in S. */
22531
22532 static int
22533 fill_glyph_string (struct glyph_string *s, int face_id,
22534 int start, int end, int overlaps)
22535 {
22536 struct glyph *glyph, *last;
22537 int voffset;
22538 int glyph_not_available_p;
22539
22540 xassert (s->f == XFRAME (s->w->frame));
22541 xassert (s->nchars == 0);
22542 xassert (start >= 0 && end > start);
22543
22544 s->for_overlaps = overlaps;
22545 glyph = s->row->glyphs[s->area] + start;
22546 last = s->row->glyphs[s->area] + end;
22547 voffset = glyph->voffset;
22548 s->padding_p = glyph->padding_p;
22549 glyph_not_available_p = glyph->glyph_not_available_p;
22550
22551 while (glyph < last
22552 && glyph->type == CHAR_GLYPH
22553 && glyph->voffset == voffset
22554 /* Same face id implies same font, nowadays. */
22555 && glyph->face_id == face_id
22556 && glyph->glyph_not_available_p == glyph_not_available_p)
22557 {
22558 int two_byte_p;
22559
22560 s->face = get_glyph_face_and_encoding (s->f, glyph,
22561 s->char2b + s->nchars,
22562 &two_byte_p);
22563 s->two_byte_p = two_byte_p;
22564 ++s->nchars;
22565 xassert (s->nchars <= end - start);
22566 s->width += glyph->pixel_width;
22567 if (glyph++->padding_p != s->padding_p)
22568 break;
22569 }
22570
22571 s->font = s->face->font;
22572
22573 /* If the specified font could not be loaded, use the frame's font,
22574 but record the fact that we couldn't load it in
22575 S->font_not_found_p so that we can draw rectangles for the
22576 characters of the glyph string. */
22577 if (s->font == NULL || glyph_not_available_p)
22578 {
22579 s->font_not_found_p = 1;
22580 s->font = FRAME_FONT (s->f);
22581 }
22582
22583 /* Adjust base line for subscript/superscript text. */
22584 s->ybase += voffset;
22585
22586 xassert (s->face && s->face->gc);
22587 return glyph - s->row->glyphs[s->area];
22588 }
22589
22590
22591 /* Fill glyph string S from image glyph S->first_glyph. */
22592
22593 static void
22594 fill_image_glyph_string (struct glyph_string *s)
22595 {
22596 xassert (s->first_glyph->type == IMAGE_GLYPH);
22597 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22598 xassert (s->img);
22599 s->slice = s->first_glyph->slice.img;
22600 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22601 s->font = s->face->font;
22602 s->width = s->first_glyph->pixel_width;
22603
22604 /* Adjust base line for subscript/superscript text. */
22605 s->ybase += s->first_glyph->voffset;
22606 }
22607
22608
22609 /* Fill glyph string S from a sequence of stretch glyphs.
22610
22611 START is the index of the first glyph to consider,
22612 END is the index of the last + 1.
22613
22614 Value is the index of the first glyph not in S. */
22615
22616 static int
22617 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22618 {
22619 struct glyph *glyph, *last;
22620 int voffset, face_id;
22621
22622 xassert (s->first_glyph->type == STRETCH_GLYPH);
22623
22624 glyph = s->row->glyphs[s->area] + start;
22625 last = s->row->glyphs[s->area] + end;
22626 face_id = glyph->face_id;
22627 s->face = FACE_FROM_ID (s->f, face_id);
22628 s->font = s->face->font;
22629 s->width = glyph->pixel_width;
22630 s->nchars = 1;
22631 voffset = glyph->voffset;
22632
22633 for (++glyph;
22634 (glyph < last
22635 && glyph->type == STRETCH_GLYPH
22636 && glyph->voffset == voffset
22637 && glyph->face_id == face_id);
22638 ++glyph)
22639 s->width += glyph->pixel_width;
22640
22641 /* Adjust base line for subscript/superscript text. */
22642 s->ybase += voffset;
22643
22644 /* The case that face->gc == 0 is handled when drawing the glyph
22645 string by calling PREPARE_FACE_FOR_DISPLAY. */
22646 xassert (s->face);
22647 return glyph - s->row->glyphs[s->area];
22648 }
22649
22650 static struct font_metrics *
22651 get_per_char_metric (struct font *font, XChar2b *char2b)
22652 {
22653 static struct font_metrics metrics;
22654 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22655
22656 if (! font || code == FONT_INVALID_CODE)
22657 return NULL;
22658 font->driver->text_extents (font, &code, 1, &metrics);
22659 return &metrics;
22660 }
22661
22662 /* EXPORT for RIF:
22663 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22664 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22665 assumed to be zero. */
22666
22667 void
22668 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22669 {
22670 *left = *right = 0;
22671
22672 if (glyph->type == CHAR_GLYPH)
22673 {
22674 struct face *face;
22675 XChar2b char2b;
22676 struct font_metrics *pcm;
22677
22678 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22679 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22680 {
22681 if (pcm->rbearing > pcm->width)
22682 *right = pcm->rbearing - pcm->width;
22683 if (pcm->lbearing < 0)
22684 *left = -pcm->lbearing;
22685 }
22686 }
22687 else if (glyph->type == COMPOSITE_GLYPH)
22688 {
22689 if (! glyph->u.cmp.automatic)
22690 {
22691 struct composition *cmp = composition_table[glyph->u.cmp.id];
22692
22693 if (cmp->rbearing > cmp->pixel_width)
22694 *right = cmp->rbearing - cmp->pixel_width;
22695 if (cmp->lbearing < 0)
22696 *left = - cmp->lbearing;
22697 }
22698 else
22699 {
22700 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22701 struct font_metrics metrics;
22702
22703 composition_gstring_width (gstring, glyph->slice.cmp.from,
22704 glyph->slice.cmp.to + 1, &metrics);
22705 if (metrics.rbearing > metrics.width)
22706 *right = metrics.rbearing - metrics.width;
22707 if (metrics.lbearing < 0)
22708 *left = - metrics.lbearing;
22709 }
22710 }
22711 }
22712
22713
22714 /* Return the index of the first glyph preceding glyph string S that
22715 is overwritten by S because of S's left overhang. Value is -1
22716 if no glyphs are overwritten. */
22717
22718 static int
22719 left_overwritten (struct glyph_string *s)
22720 {
22721 int k;
22722
22723 if (s->left_overhang)
22724 {
22725 int x = 0, i;
22726 struct glyph *glyphs = s->row->glyphs[s->area];
22727 int first = s->first_glyph - glyphs;
22728
22729 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22730 x -= glyphs[i].pixel_width;
22731
22732 k = i + 1;
22733 }
22734 else
22735 k = -1;
22736
22737 return k;
22738 }
22739
22740
22741 /* Return the index of the first glyph preceding glyph string S that
22742 is overwriting S because of its right overhang. Value is -1 if no
22743 glyph in front of S overwrites S. */
22744
22745 static int
22746 left_overwriting (struct glyph_string *s)
22747 {
22748 int i, k, x;
22749 struct glyph *glyphs = s->row->glyphs[s->area];
22750 int first = s->first_glyph - glyphs;
22751
22752 k = -1;
22753 x = 0;
22754 for (i = first - 1; i >= 0; --i)
22755 {
22756 int left, right;
22757 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22758 if (x + right > 0)
22759 k = i;
22760 x -= glyphs[i].pixel_width;
22761 }
22762
22763 return k;
22764 }
22765
22766
22767 /* Return the index of the last glyph following glyph string S that is
22768 overwritten by S because of S's right overhang. Value is -1 if
22769 no such glyph is found. */
22770
22771 static int
22772 right_overwritten (struct glyph_string *s)
22773 {
22774 int k = -1;
22775
22776 if (s->right_overhang)
22777 {
22778 int x = 0, i;
22779 struct glyph *glyphs = s->row->glyphs[s->area];
22780 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22781 int end = s->row->used[s->area];
22782
22783 for (i = first; i < end && s->right_overhang > x; ++i)
22784 x += glyphs[i].pixel_width;
22785
22786 k = i;
22787 }
22788
22789 return k;
22790 }
22791
22792
22793 /* Return the index of the last glyph following glyph string S that
22794 overwrites S because of its left overhang. Value is negative
22795 if no such glyph is found. */
22796
22797 static int
22798 right_overwriting (struct glyph_string *s)
22799 {
22800 int i, k, x;
22801 int end = s->row->used[s->area];
22802 struct glyph *glyphs = s->row->glyphs[s->area];
22803 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22804
22805 k = -1;
22806 x = 0;
22807 for (i = first; i < end; ++i)
22808 {
22809 int left, right;
22810 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22811 if (x - left < 0)
22812 k = i;
22813 x += glyphs[i].pixel_width;
22814 }
22815
22816 return k;
22817 }
22818
22819
22820 /* Set background width of glyph string S. START is the index of the
22821 first glyph following S. LAST_X is the right-most x-position + 1
22822 in the drawing area. */
22823
22824 static inline void
22825 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22826 {
22827 /* If the face of this glyph string has to be drawn to the end of
22828 the drawing area, set S->extends_to_end_of_line_p. */
22829
22830 if (start == s->row->used[s->area]
22831 && s->area == TEXT_AREA
22832 && ((s->row->fill_line_p
22833 && (s->hl == DRAW_NORMAL_TEXT
22834 || s->hl == DRAW_IMAGE_RAISED
22835 || s->hl == DRAW_IMAGE_SUNKEN))
22836 || s->hl == DRAW_MOUSE_FACE))
22837 s->extends_to_end_of_line_p = 1;
22838
22839 /* If S extends its face to the end of the line, set its
22840 background_width to the distance to the right edge of the drawing
22841 area. */
22842 if (s->extends_to_end_of_line_p)
22843 s->background_width = last_x - s->x + 1;
22844 else
22845 s->background_width = s->width;
22846 }
22847
22848
22849 /* Compute overhangs and x-positions for glyph string S and its
22850 predecessors, or successors. X is the starting x-position for S.
22851 BACKWARD_P non-zero means process predecessors. */
22852
22853 static void
22854 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22855 {
22856 if (backward_p)
22857 {
22858 while (s)
22859 {
22860 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22861 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22862 x -= s->width;
22863 s->x = x;
22864 s = s->prev;
22865 }
22866 }
22867 else
22868 {
22869 while (s)
22870 {
22871 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22872 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22873 s->x = x;
22874 x += s->width;
22875 s = s->next;
22876 }
22877 }
22878 }
22879
22880
22881
22882 /* The following macros are only called from draw_glyphs below.
22883 They reference the following parameters of that function directly:
22884 `w', `row', `area', and `overlap_p'
22885 as well as the following local variables:
22886 `s', `f', and `hdc' (in W32) */
22887
22888 #ifdef HAVE_NTGUI
22889 /* On W32, silently add local `hdc' variable to argument list of
22890 init_glyph_string. */
22891 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22892 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22893 #else
22894 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22895 init_glyph_string (s, char2b, w, row, area, start, hl)
22896 #endif
22897
22898 /* Add a glyph string for a stretch glyph to the list of strings
22899 between HEAD and TAIL. START is the index of the stretch glyph in
22900 row area AREA of glyph row ROW. END is the index of the last glyph
22901 in that glyph row area. X is the current output position assigned
22902 to the new glyph string constructed. HL overrides that face of the
22903 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22904 is the right-most x-position of the drawing area. */
22905
22906 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22907 and below -- keep them on one line. */
22908 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22909 do \
22910 { \
22911 s = (struct glyph_string *) alloca (sizeof *s); \
22912 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22913 START = fill_stretch_glyph_string (s, START, END); \
22914 append_glyph_string (&HEAD, &TAIL, s); \
22915 s->x = (X); \
22916 } \
22917 while (0)
22918
22919
22920 /* Add a glyph string for an image glyph to the list of strings
22921 between HEAD and TAIL. START is the index of the image glyph in
22922 row area AREA of glyph row ROW. END is the index of the last glyph
22923 in that glyph row area. X is the current output position assigned
22924 to the new glyph string constructed. HL overrides that face of the
22925 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22926 is the right-most x-position of the drawing area. */
22927
22928 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22929 do \
22930 { \
22931 s = (struct glyph_string *) alloca (sizeof *s); \
22932 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22933 fill_image_glyph_string (s); \
22934 append_glyph_string (&HEAD, &TAIL, s); \
22935 ++START; \
22936 s->x = (X); \
22937 } \
22938 while (0)
22939
22940
22941 /* Add a glyph string for a sequence of character glyphs to the list
22942 of strings between HEAD and TAIL. START is the index of the first
22943 glyph in row area AREA of glyph row ROW that is part of the new
22944 glyph string. END is the index of the last glyph in that glyph row
22945 area. X is the current output position assigned to the new glyph
22946 string constructed. HL overrides that face of the glyph; e.g. it
22947 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22948 right-most x-position of the drawing area. */
22949
22950 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22951 do \
22952 { \
22953 int face_id; \
22954 XChar2b *char2b; \
22955 \
22956 face_id = (row)->glyphs[area][START].face_id; \
22957 \
22958 s = (struct glyph_string *) alloca (sizeof *s); \
22959 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22960 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22961 append_glyph_string (&HEAD, &TAIL, s); \
22962 s->x = (X); \
22963 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22964 } \
22965 while (0)
22966
22967
22968 /* Add a glyph string for a composite sequence to the list of strings
22969 between HEAD and TAIL. START is the index of the first glyph in
22970 row area AREA of glyph row ROW that is part of the new glyph
22971 string. END is the index of the last glyph in that glyph row area.
22972 X is the current output position assigned to the new glyph string
22973 constructed. HL overrides that face of the glyph; e.g. it is
22974 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22975 x-position of the drawing area. */
22976
22977 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22978 do { \
22979 int face_id = (row)->glyphs[area][START].face_id; \
22980 struct face *base_face = FACE_FROM_ID (f, face_id); \
22981 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22982 struct composition *cmp = composition_table[cmp_id]; \
22983 XChar2b *char2b; \
22984 struct glyph_string *first_s = NULL; \
22985 int n; \
22986 \
22987 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22988 \
22989 /* Make glyph_strings for each glyph sequence that is drawable by \
22990 the same face, and append them to HEAD/TAIL. */ \
22991 for (n = 0; n < cmp->glyph_len;) \
22992 { \
22993 s = (struct glyph_string *) alloca (sizeof *s); \
22994 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22995 append_glyph_string (&(HEAD), &(TAIL), s); \
22996 s->cmp = cmp; \
22997 s->cmp_from = n; \
22998 s->x = (X); \
22999 if (n == 0) \
23000 first_s = s; \
23001 n = fill_composite_glyph_string (s, base_face, overlaps); \
23002 } \
23003 \
23004 ++START; \
23005 s = first_s; \
23006 } while (0)
23007
23008
23009 /* Add a glyph string for a glyph-string sequence to the list of strings
23010 between HEAD and TAIL. */
23011
23012 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23013 do { \
23014 int face_id; \
23015 XChar2b *char2b; \
23016 Lisp_Object gstring; \
23017 \
23018 face_id = (row)->glyphs[area][START].face_id; \
23019 gstring = (composition_gstring_from_id \
23020 ((row)->glyphs[area][START].u.cmp.id)); \
23021 s = (struct glyph_string *) alloca (sizeof *s); \
23022 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23023 * LGSTRING_GLYPH_LEN (gstring)); \
23024 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23025 append_glyph_string (&(HEAD), &(TAIL), s); \
23026 s->x = (X); \
23027 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23028 } while (0)
23029
23030
23031 /* Add a glyph string for a sequence of glyphless character's glyphs
23032 to the list of strings between HEAD and TAIL. The meanings of
23033 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23034
23035 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23036 do \
23037 { \
23038 int face_id; \
23039 \
23040 face_id = (row)->glyphs[area][START].face_id; \
23041 \
23042 s = (struct glyph_string *) alloca (sizeof *s); \
23043 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23044 append_glyph_string (&HEAD, &TAIL, s); \
23045 s->x = (X); \
23046 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23047 overlaps); \
23048 } \
23049 while (0)
23050
23051
23052 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23053 of AREA of glyph row ROW on window W between indices START and END.
23054 HL overrides the face for drawing glyph strings, e.g. it is
23055 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23056 x-positions of the drawing area.
23057
23058 This is an ugly monster macro construct because we must use alloca
23059 to allocate glyph strings (because draw_glyphs can be called
23060 asynchronously). */
23061
23062 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23063 do \
23064 { \
23065 HEAD = TAIL = NULL; \
23066 while (START < END) \
23067 { \
23068 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23069 switch (first_glyph->type) \
23070 { \
23071 case CHAR_GLYPH: \
23072 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23073 HL, X, LAST_X); \
23074 break; \
23075 \
23076 case COMPOSITE_GLYPH: \
23077 if (first_glyph->u.cmp.automatic) \
23078 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23079 HL, X, LAST_X); \
23080 else \
23081 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23082 HL, X, LAST_X); \
23083 break; \
23084 \
23085 case STRETCH_GLYPH: \
23086 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23087 HL, X, LAST_X); \
23088 break; \
23089 \
23090 case IMAGE_GLYPH: \
23091 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23092 HL, X, LAST_X); \
23093 break; \
23094 \
23095 case GLYPHLESS_GLYPH: \
23096 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23097 HL, X, LAST_X); \
23098 break; \
23099 \
23100 default: \
23101 abort (); \
23102 } \
23103 \
23104 if (s) \
23105 { \
23106 set_glyph_string_background_width (s, START, LAST_X); \
23107 (X) += s->width; \
23108 } \
23109 } \
23110 } while (0)
23111
23112
23113 /* Draw glyphs between START and END in AREA of ROW on window W,
23114 starting at x-position X. X is relative to AREA in W. HL is a
23115 face-override with the following meaning:
23116
23117 DRAW_NORMAL_TEXT draw normally
23118 DRAW_CURSOR draw in cursor face
23119 DRAW_MOUSE_FACE draw in mouse face.
23120 DRAW_INVERSE_VIDEO draw in mode line face
23121 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23122 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23123
23124 If OVERLAPS is non-zero, draw only the foreground of characters and
23125 clip to the physical height of ROW. Non-zero value also defines
23126 the overlapping part to be drawn:
23127
23128 OVERLAPS_PRED overlap with preceding rows
23129 OVERLAPS_SUCC overlap with succeeding rows
23130 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23131 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23132
23133 Value is the x-position reached, relative to AREA of W. */
23134
23135 static int
23136 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23137 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23138 enum draw_glyphs_face hl, int overlaps)
23139 {
23140 struct glyph_string *head, *tail;
23141 struct glyph_string *s;
23142 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23143 int i, j, x_reached, last_x, area_left = 0;
23144 struct frame *f = XFRAME (WINDOW_FRAME (w));
23145 DECLARE_HDC (hdc);
23146
23147 ALLOCATE_HDC (hdc, f);
23148
23149 /* Let's rather be paranoid than getting a SEGV. */
23150 end = min (end, row->used[area]);
23151 start = max (0, start);
23152 start = min (end, start);
23153
23154 /* Translate X to frame coordinates. Set last_x to the right
23155 end of the drawing area. */
23156 if (row->full_width_p)
23157 {
23158 /* X is relative to the left edge of W, without scroll bars
23159 or fringes. */
23160 area_left = WINDOW_LEFT_EDGE_X (w);
23161 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23162 }
23163 else
23164 {
23165 area_left = window_box_left (w, area);
23166 last_x = area_left + window_box_width (w, area);
23167 }
23168 x += area_left;
23169
23170 /* Build a doubly-linked list of glyph_string structures between
23171 head and tail from what we have to draw. Note that the macro
23172 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23173 the reason we use a separate variable `i'. */
23174 i = start;
23175 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23176 if (tail)
23177 x_reached = tail->x + tail->background_width;
23178 else
23179 x_reached = x;
23180
23181 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23182 the row, redraw some glyphs in front or following the glyph
23183 strings built above. */
23184 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23185 {
23186 struct glyph_string *h, *t;
23187 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23188 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23189 int check_mouse_face = 0;
23190 int dummy_x = 0;
23191
23192 /* If mouse highlighting is on, we may need to draw adjacent
23193 glyphs using mouse-face highlighting. */
23194 if (area == TEXT_AREA && row->mouse_face_p)
23195 {
23196 struct glyph_row *mouse_beg_row, *mouse_end_row;
23197
23198 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23199 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23200
23201 if (row >= mouse_beg_row && row <= mouse_end_row)
23202 {
23203 check_mouse_face = 1;
23204 mouse_beg_col = (row == mouse_beg_row)
23205 ? hlinfo->mouse_face_beg_col : 0;
23206 mouse_end_col = (row == mouse_end_row)
23207 ? hlinfo->mouse_face_end_col
23208 : row->used[TEXT_AREA];
23209 }
23210 }
23211
23212 /* Compute overhangs for all glyph strings. */
23213 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23214 for (s = head; s; s = s->next)
23215 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23216
23217 /* Prepend glyph strings for glyphs in front of the first glyph
23218 string that are overwritten because of the first glyph
23219 string's left overhang. The background of all strings
23220 prepended must be drawn because the first glyph string
23221 draws over it. */
23222 i = left_overwritten (head);
23223 if (i >= 0)
23224 {
23225 enum draw_glyphs_face overlap_hl;
23226
23227 /* If this row contains mouse highlighting, attempt to draw
23228 the overlapped glyphs with the correct highlight. This
23229 code fails if the overlap encompasses more than one glyph
23230 and mouse-highlight spans only some of these glyphs.
23231 However, making it work perfectly involves a lot more
23232 code, and I don't know if the pathological case occurs in
23233 practice, so we'll stick to this for now. --- cyd */
23234 if (check_mouse_face
23235 && mouse_beg_col < start && mouse_end_col > i)
23236 overlap_hl = DRAW_MOUSE_FACE;
23237 else
23238 overlap_hl = DRAW_NORMAL_TEXT;
23239
23240 j = i;
23241 BUILD_GLYPH_STRINGS (j, start, h, t,
23242 overlap_hl, dummy_x, last_x);
23243 start = i;
23244 compute_overhangs_and_x (t, head->x, 1);
23245 prepend_glyph_string_lists (&head, &tail, h, t);
23246 clip_head = head;
23247 }
23248
23249 /* Prepend glyph strings for glyphs in front of the first glyph
23250 string that overwrite that glyph string because of their
23251 right overhang. For these strings, only the foreground must
23252 be drawn, because it draws over the glyph string at `head'.
23253 The background must not be drawn because this would overwrite
23254 right overhangs of preceding glyphs for which no glyph
23255 strings exist. */
23256 i = left_overwriting (head);
23257 if (i >= 0)
23258 {
23259 enum draw_glyphs_face overlap_hl;
23260
23261 if (check_mouse_face
23262 && mouse_beg_col < start && mouse_end_col > i)
23263 overlap_hl = DRAW_MOUSE_FACE;
23264 else
23265 overlap_hl = DRAW_NORMAL_TEXT;
23266
23267 clip_head = head;
23268 BUILD_GLYPH_STRINGS (i, start, h, t,
23269 overlap_hl, dummy_x, last_x);
23270 for (s = h; s; s = s->next)
23271 s->background_filled_p = 1;
23272 compute_overhangs_and_x (t, head->x, 1);
23273 prepend_glyph_string_lists (&head, &tail, h, t);
23274 }
23275
23276 /* Append glyphs strings for glyphs following the last glyph
23277 string tail that are overwritten by tail. The background of
23278 these strings has to be drawn because tail's foreground draws
23279 over it. */
23280 i = right_overwritten (tail);
23281 if (i >= 0)
23282 {
23283 enum draw_glyphs_face overlap_hl;
23284
23285 if (check_mouse_face
23286 && mouse_beg_col < i && mouse_end_col > end)
23287 overlap_hl = DRAW_MOUSE_FACE;
23288 else
23289 overlap_hl = DRAW_NORMAL_TEXT;
23290
23291 BUILD_GLYPH_STRINGS (end, i, h, t,
23292 overlap_hl, x, last_x);
23293 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23294 we don't have `end = i;' here. */
23295 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23296 append_glyph_string_lists (&head, &tail, h, t);
23297 clip_tail = tail;
23298 }
23299
23300 /* Append glyph strings for glyphs following the last glyph
23301 string tail that overwrite tail. The foreground of such
23302 glyphs has to be drawn because it writes into the background
23303 of tail. The background must not be drawn because it could
23304 paint over the foreground of following glyphs. */
23305 i = right_overwriting (tail);
23306 if (i >= 0)
23307 {
23308 enum draw_glyphs_face overlap_hl;
23309 if (check_mouse_face
23310 && mouse_beg_col < i && mouse_end_col > end)
23311 overlap_hl = DRAW_MOUSE_FACE;
23312 else
23313 overlap_hl = DRAW_NORMAL_TEXT;
23314
23315 clip_tail = tail;
23316 i++; /* We must include the Ith glyph. */
23317 BUILD_GLYPH_STRINGS (end, i, h, t,
23318 overlap_hl, x, last_x);
23319 for (s = h; s; s = s->next)
23320 s->background_filled_p = 1;
23321 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23322 append_glyph_string_lists (&head, &tail, h, t);
23323 }
23324 if (clip_head || clip_tail)
23325 for (s = head; s; s = s->next)
23326 {
23327 s->clip_head = clip_head;
23328 s->clip_tail = clip_tail;
23329 }
23330 }
23331
23332 /* Draw all strings. */
23333 for (s = head; s; s = s->next)
23334 FRAME_RIF (f)->draw_glyph_string (s);
23335
23336 #ifndef HAVE_NS
23337 /* When focus a sole frame and move horizontally, this sets on_p to 0
23338 causing a failure to erase prev cursor position. */
23339 if (area == TEXT_AREA
23340 && !row->full_width_p
23341 /* When drawing overlapping rows, only the glyph strings'
23342 foreground is drawn, which doesn't erase a cursor
23343 completely. */
23344 && !overlaps)
23345 {
23346 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23347 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23348 : (tail ? tail->x + tail->background_width : x));
23349 x0 -= area_left;
23350 x1 -= area_left;
23351
23352 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23353 row->y, MATRIX_ROW_BOTTOM_Y (row));
23354 }
23355 #endif
23356
23357 /* Value is the x-position up to which drawn, relative to AREA of W.
23358 This doesn't include parts drawn because of overhangs. */
23359 if (row->full_width_p)
23360 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23361 else
23362 x_reached -= area_left;
23363
23364 RELEASE_HDC (hdc, f);
23365
23366 return x_reached;
23367 }
23368
23369 /* Expand row matrix if too narrow. Don't expand if area
23370 is not present. */
23371
23372 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23373 { \
23374 if (!fonts_changed_p \
23375 && (it->glyph_row->glyphs[area] \
23376 < it->glyph_row->glyphs[area + 1])) \
23377 { \
23378 it->w->ncols_scale_factor++; \
23379 fonts_changed_p = 1; \
23380 } \
23381 }
23382
23383 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23384 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23385
23386 static inline void
23387 append_glyph (struct it *it)
23388 {
23389 struct glyph *glyph;
23390 enum glyph_row_area area = it->area;
23391
23392 xassert (it->glyph_row);
23393 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23394
23395 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23396 if (glyph < it->glyph_row->glyphs[area + 1])
23397 {
23398 /* If the glyph row is reversed, we need to prepend the glyph
23399 rather than append it. */
23400 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23401 {
23402 struct glyph *g;
23403
23404 /* Make room for the additional glyph. */
23405 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23406 g[1] = *g;
23407 glyph = it->glyph_row->glyphs[area];
23408 }
23409 glyph->charpos = CHARPOS (it->position);
23410 glyph->object = it->object;
23411 if (it->pixel_width > 0)
23412 {
23413 glyph->pixel_width = it->pixel_width;
23414 glyph->padding_p = 0;
23415 }
23416 else
23417 {
23418 /* Assure at least 1-pixel width. Otherwise, cursor can't
23419 be displayed correctly. */
23420 glyph->pixel_width = 1;
23421 glyph->padding_p = 1;
23422 }
23423 glyph->ascent = it->ascent;
23424 glyph->descent = it->descent;
23425 glyph->voffset = it->voffset;
23426 glyph->type = CHAR_GLYPH;
23427 glyph->avoid_cursor_p = it->avoid_cursor_p;
23428 glyph->multibyte_p = it->multibyte_p;
23429 glyph->left_box_line_p = it->start_of_box_run_p;
23430 glyph->right_box_line_p = it->end_of_box_run_p;
23431 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23432 || it->phys_descent > it->descent);
23433 glyph->glyph_not_available_p = it->glyph_not_available_p;
23434 glyph->face_id = it->face_id;
23435 glyph->u.ch = it->char_to_display;
23436 glyph->slice.img = null_glyph_slice;
23437 glyph->font_type = FONT_TYPE_UNKNOWN;
23438 if (it->bidi_p)
23439 {
23440 glyph->resolved_level = it->bidi_it.resolved_level;
23441 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23442 abort ();
23443 glyph->bidi_type = it->bidi_it.type;
23444 }
23445 else
23446 {
23447 glyph->resolved_level = 0;
23448 glyph->bidi_type = UNKNOWN_BT;
23449 }
23450 ++it->glyph_row->used[area];
23451 }
23452 else
23453 IT_EXPAND_MATRIX_WIDTH (it, area);
23454 }
23455
23456 /* Store one glyph for the composition IT->cmp_it.id in
23457 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23458 non-null. */
23459
23460 static inline void
23461 append_composite_glyph (struct it *it)
23462 {
23463 struct glyph *glyph;
23464 enum glyph_row_area area = it->area;
23465
23466 xassert (it->glyph_row);
23467
23468 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23469 if (glyph < it->glyph_row->glyphs[area + 1])
23470 {
23471 /* If the glyph row is reversed, we need to prepend the glyph
23472 rather than append it. */
23473 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23474 {
23475 struct glyph *g;
23476
23477 /* Make room for the new glyph. */
23478 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23479 g[1] = *g;
23480 glyph = it->glyph_row->glyphs[it->area];
23481 }
23482 glyph->charpos = it->cmp_it.charpos;
23483 glyph->object = it->object;
23484 glyph->pixel_width = it->pixel_width;
23485 glyph->ascent = it->ascent;
23486 glyph->descent = it->descent;
23487 glyph->voffset = it->voffset;
23488 glyph->type = COMPOSITE_GLYPH;
23489 if (it->cmp_it.ch < 0)
23490 {
23491 glyph->u.cmp.automatic = 0;
23492 glyph->u.cmp.id = it->cmp_it.id;
23493 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23494 }
23495 else
23496 {
23497 glyph->u.cmp.automatic = 1;
23498 glyph->u.cmp.id = it->cmp_it.id;
23499 glyph->slice.cmp.from = it->cmp_it.from;
23500 glyph->slice.cmp.to = it->cmp_it.to - 1;
23501 }
23502 glyph->avoid_cursor_p = it->avoid_cursor_p;
23503 glyph->multibyte_p = it->multibyte_p;
23504 glyph->left_box_line_p = it->start_of_box_run_p;
23505 glyph->right_box_line_p = it->end_of_box_run_p;
23506 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23507 || it->phys_descent > it->descent);
23508 glyph->padding_p = 0;
23509 glyph->glyph_not_available_p = 0;
23510 glyph->face_id = it->face_id;
23511 glyph->font_type = FONT_TYPE_UNKNOWN;
23512 if (it->bidi_p)
23513 {
23514 glyph->resolved_level = it->bidi_it.resolved_level;
23515 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23516 abort ();
23517 glyph->bidi_type = it->bidi_it.type;
23518 }
23519 ++it->glyph_row->used[area];
23520 }
23521 else
23522 IT_EXPAND_MATRIX_WIDTH (it, area);
23523 }
23524
23525
23526 /* Change IT->ascent and IT->height according to the setting of
23527 IT->voffset. */
23528
23529 static inline void
23530 take_vertical_position_into_account (struct it *it)
23531 {
23532 if (it->voffset)
23533 {
23534 if (it->voffset < 0)
23535 /* Increase the ascent so that we can display the text higher
23536 in the line. */
23537 it->ascent -= it->voffset;
23538 else
23539 /* Increase the descent so that we can display the text lower
23540 in the line. */
23541 it->descent += it->voffset;
23542 }
23543 }
23544
23545
23546 /* Produce glyphs/get display metrics for the image IT is loaded with.
23547 See the description of struct display_iterator in dispextern.h for
23548 an overview of struct display_iterator. */
23549
23550 static void
23551 produce_image_glyph (struct it *it)
23552 {
23553 struct image *img;
23554 struct face *face;
23555 int glyph_ascent, crop;
23556 struct glyph_slice slice;
23557
23558 xassert (it->what == IT_IMAGE);
23559
23560 face = FACE_FROM_ID (it->f, it->face_id);
23561 xassert (face);
23562 /* Make sure X resources of the face is loaded. */
23563 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23564
23565 if (it->image_id < 0)
23566 {
23567 /* Fringe bitmap. */
23568 it->ascent = it->phys_ascent = 0;
23569 it->descent = it->phys_descent = 0;
23570 it->pixel_width = 0;
23571 it->nglyphs = 0;
23572 return;
23573 }
23574
23575 img = IMAGE_FROM_ID (it->f, it->image_id);
23576 xassert (img);
23577 /* Make sure X resources of the image is loaded. */
23578 prepare_image_for_display (it->f, img);
23579
23580 slice.x = slice.y = 0;
23581 slice.width = img->width;
23582 slice.height = img->height;
23583
23584 if (INTEGERP (it->slice.x))
23585 slice.x = XINT (it->slice.x);
23586 else if (FLOATP (it->slice.x))
23587 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23588
23589 if (INTEGERP (it->slice.y))
23590 slice.y = XINT (it->slice.y);
23591 else if (FLOATP (it->slice.y))
23592 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23593
23594 if (INTEGERP (it->slice.width))
23595 slice.width = XINT (it->slice.width);
23596 else if (FLOATP (it->slice.width))
23597 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23598
23599 if (INTEGERP (it->slice.height))
23600 slice.height = XINT (it->slice.height);
23601 else if (FLOATP (it->slice.height))
23602 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23603
23604 if (slice.x >= img->width)
23605 slice.x = img->width;
23606 if (slice.y >= img->height)
23607 slice.y = img->height;
23608 if (slice.x + slice.width >= img->width)
23609 slice.width = img->width - slice.x;
23610 if (slice.y + slice.height > img->height)
23611 slice.height = img->height - slice.y;
23612
23613 if (slice.width == 0 || slice.height == 0)
23614 return;
23615
23616 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23617
23618 it->descent = slice.height - glyph_ascent;
23619 if (slice.y == 0)
23620 it->descent += img->vmargin;
23621 if (slice.y + slice.height == img->height)
23622 it->descent += img->vmargin;
23623 it->phys_descent = it->descent;
23624
23625 it->pixel_width = slice.width;
23626 if (slice.x == 0)
23627 it->pixel_width += img->hmargin;
23628 if (slice.x + slice.width == img->width)
23629 it->pixel_width += img->hmargin;
23630
23631 /* It's quite possible for images to have an ascent greater than
23632 their height, so don't get confused in that case. */
23633 if (it->descent < 0)
23634 it->descent = 0;
23635
23636 it->nglyphs = 1;
23637
23638 if (face->box != FACE_NO_BOX)
23639 {
23640 if (face->box_line_width > 0)
23641 {
23642 if (slice.y == 0)
23643 it->ascent += face->box_line_width;
23644 if (slice.y + slice.height == img->height)
23645 it->descent += face->box_line_width;
23646 }
23647
23648 if (it->start_of_box_run_p && slice.x == 0)
23649 it->pixel_width += eabs (face->box_line_width);
23650 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23651 it->pixel_width += eabs (face->box_line_width);
23652 }
23653
23654 take_vertical_position_into_account (it);
23655
23656 /* Automatically crop wide image glyphs at right edge so we can
23657 draw the cursor on same display row. */
23658 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23659 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23660 {
23661 it->pixel_width -= crop;
23662 slice.width -= crop;
23663 }
23664
23665 if (it->glyph_row)
23666 {
23667 struct glyph *glyph;
23668 enum glyph_row_area area = it->area;
23669
23670 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23671 if (glyph < it->glyph_row->glyphs[area + 1])
23672 {
23673 glyph->charpos = CHARPOS (it->position);
23674 glyph->object = it->object;
23675 glyph->pixel_width = it->pixel_width;
23676 glyph->ascent = glyph_ascent;
23677 glyph->descent = it->descent;
23678 glyph->voffset = it->voffset;
23679 glyph->type = IMAGE_GLYPH;
23680 glyph->avoid_cursor_p = it->avoid_cursor_p;
23681 glyph->multibyte_p = it->multibyte_p;
23682 glyph->left_box_line_p = it->start_of_box_run_p;
23683 glyph->right_box_line_p = it->end_of_box_run_p;
23684 glyph->overlaps_vertically_p = 0;
23685 glyph->padding_p = 0;
23686 glyph->glyph_not_available_p = 0;
23687 glyph->face_id = it->face_id;
23688 glyph->u.img_id = img->id;
23689 glyph->slice.img = slice;
23690 glyph->font_type = FONT_TYPE_UNKNOWN;
23691 if (it->bidi_p)
23692 {
23693 glyph->resolved_level = it->bidi_it.resolved_level;
23694 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23695 abort ();
23696 glyph->bidi_type = it->bidi_it.type;
23697 }
23698 ++it->glyph_row->used[area];
23699 }
23700 else
23701 IT_EXPAND_MATRIX_WIDTH (it, area);
23702 }
23703 }
23704
23705
23706 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23707 of the glyph, WIDTH and HEIGHT are the width and height of the
23708 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23709
23710 static void
23711 append_stretch_glyph (struct it *it, Lisp_Object object,
23712 int width, int height, int ascent)
23713 {
23714 struct glyph *glyph;
23715 enum glyph_row_area area = it->area;
23716
23717 xassert (ascent >= 0 && ascent <= height);
23718
23719 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23720 if (glyph < it->glyph_row->glyphs[area + 1])
23721 {
23722 /* If the glyph row is reversed, we need to prepend the glyph
23723 rather than append it. */
23724 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23725 {
23726 struct glyph *g;
23727
23728 /* Make room for the additional glyph. */
23729 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23730 g[1] = *g;
23731 glyph = it->glyph_row->glyphs[area];
23732 }
23733 glyph->charpos = CHARPOS (it->position);
23734 glyph->object = object;
23735 glyph->pixel_width = width;
23736 glyph->ascent = ascent;
23737 glyph->descent = height - ascent;
23738 glyph->voffset = it->voffset;
23739 glyph->type = STRETCH_GLYPH;
23740 glyph->avoid_cursor_p = it->avoid_cursor_p;
23741 glyph->multibyte_p = it->multibyte_p;
23742 glyph->left_box_line_p = it->start_of_box_run_p;
23743 glyph->right_box_line_p = it->end_of_box_run_p;
23744 glyph->overlaps_vertically_p = 0;
23745 glyph->padding_p = 0;
23746 glyph->glyph_not_available_p = 0;
23747 glyph->face_id = it->face_id;
23748 glyph->u.stretch.ascent = ascent;
23749 glyph->u.stretch.height = height;
23750 glyph->slice.img = null_glyph_slice;
23751 glyph->font_type = FONT_TYPE_UNKNOWN;
23752 if (it->bidi_p)
23753 {
23754 glyph->resolved_level = it->bidi_it.resolved_level;
23755 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23756 abort ();
23757 glyph->bidi_type = it->bidi_it.type;
23758 }
23759 else
23760 {
23761 glyph->resolved_level = 0;
23762 glyph->bidi_type = UNKNOWN_BT;
23763 }
23764 ++it->glyph_row->used[area];
23765 }
23766 else
23767 IT_EXPAND_MATRIX_WIDTH (it, area);
23768 }
23769
23770 #endif /* HAVE_WINDOW_SYSTEM */
23771
23772 /* Produce a stretch glyph for iterator IT. IT->object is the value
23773 of the glyph property displayed. The value must be a list
23774 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23775 being recognized:
23776
23777 1. `:width WIDTH' specifies that the space should be WIDTH *
23778 canonical char width wide. WIDTH may be an integer or floating
23779 point number.
23780
23781 2. `:relative-width FACTOR' specifies that the width of the stretch
23782 should be computed from the width of the first character having the
23783 `glyph' property, and should be FACTOR times that width.
23784
23785 3. `:align-to HPOS' specifies that the space should be wide enough
23786 to reach HPOS, a value in canonical character units.
23787
23788 Exactly one of the above pairs must be present.
23789
23790 4. `:height HEIGHT' specifies that the height of the stretch produced
23791 should be HEIGHT, measured in canonical character units.
23792
23793 5. `:relative-height FACTOR' specifies that the height of the
23794 stretch should be FACTOR times the height of the characters having
23795 the glyph property.
23796
23797 Either none or exactly one of 4 or 5 must be present.
23798
23799 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23800 of the stretch should be used for the ascent of the stretch.
23801 ASCENT must be in the range 0 <= ASCENT <= 100. */
23802
23803 void
23804 produce_stretch_glyph (struct it *it)
23805 {
23806 /* (space :width WIDTH :height HEIGHT ...) */
23807 Lisp_Object prop, plist;
23808 int width = 0, height = 0, align_to = -1;
23809 int zero_width_ok_p = 0;
23810 int ascent = 0;
23811 double tem;
23812 struct face *face = NULL;
23813 struct font *font = NULL;
23814
23815 #ifdef HAVE_WINDOW_SYSTEM
23816 int zero_height_ok_p = 0;
23817
23818 if (FRAME_WINDOW_P (it->f))
23819 {
23820 face = FACE_FROM_ID (it->f, it->face_id);
23821 font = face->font ? face->font : FRAME_FONT (it->f);
23822 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23823 }
23824 #endif
23825
23826 /* List should start with `space'. */
23827 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23828 plist = XCDR (it->object);
23829
23830 /* Compute the width of the stretch. */
23831 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23832 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23833 {
23834 /* Absolute width `:width WIDTH' specified and valid. */
23835 zero_width_ok_p = 1;
23836 width = (int)tem;
23837 }
23838 #ifdef HAVE_WINDOW_SYSTEM
23839 else if (FRAME_WINDOW_P (it->f)
23840 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23841 {
23842 /* Relative width `:relative-width FACTOR' specified and valid.
23843 Compute the width of the characters having the `glyph'
23844 property. */
23845 struct it it2;
23846 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23847
23848 it2 = *it;
23849 if (it->multibyte_p)
23850 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23851 else
23852 {
23853 it2.c = it2.char_to_display = *p, it2.len = 1;
23854 if (! ASCII_CHAR_P (it2.c))
23855 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23856 }
23857
23858 it2.glyph_row = NULL;
23859 it2.what = IT_CHARACTER;
23860 x_produce_glyphs (&it2);
23861 width = NUMVAL (prop) * it2.pixel_width;
23862 }
23863 #endif /* HAVE_WINDOW_SYSTEM */
23864 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23865 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23866 {
23867 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23868 align_to = (align_to < 0
23869 ? 0
23870 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23871 else if (align_to < 0)
23872 align_to = window_box_left_offset (it->w, TEXT_AREA);
23873 width = max (0, (int)tem + align_to - it->current_x);
23874 zero_width_ok_p = 1;
23875 }
23876 else
23877 /* Nothing specified -> width defaults to canonical char width. */
23878 width = FRAME_COLUMN_WIDTH (it->f);
23879
23880 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23881 width = 1;
23882
23883 #ifdef HAVE_WINDOW_SYSTEM
23884 /* Compute height. */
23885 if (FRAME_WINDOW_P (it->f))
23886 {
23887 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23888 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23889 {
23890 height = (int)tem;
23891 zero_height_ok_p = 1;
23892 }
23893 else if (prop = Fplist_get (plist, QCrelative_height),
23894 NUMVAL (prop) > 0)
23895 height = FONT_HEIGHT (font) * NUMVAL (prop);
23896 else
23897 height = FONT_HEIGHT (font);
23898
23899 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23900 height = 1;
23901
23902 /* Compute percentage of height used for ascent. If
23903 `:ascent ASCENT' is present and valid, use that. Otherwise,
23904 derive the ascent from the font in use. */
23905 if (prop = Fplist_get (plist, QCascent),
23906 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23907 ascent = height * NUMVAL (prop) / 100.0;
23908 else if (!NILP (prop)
23909 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23910 ascent = min (max (0, (int)tem), height);
23911 else
23912 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23913 }
23914 else
23915 #endif /* HAVE_WINDOW_SYSTEM */
23916 height = 1;
23917
23918 if (width > 0 && it->line_wrap != TRUNCATE
23919 && it->current_x + width > it->last_visible_x)
23920 {
23921 width = it->last_visible_x - it->current_x;
23922 #ifdef HAVE_WINDOW_SYSTEM
23923 /* Subtract one more pixel from the stretch width, but only on
23924 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23925 width -= FRAME_WINDOW_P (it->f);
23926 #endif
23927 }
23928
23929 if (width > 0 && height > 0 && it->glyph_row)
23930 {
23931 Lisp_Object o_object = it->object;
23932 Lisp_Object object = it->stack[it->sp - 1].string;
23933 int n = width;
23934
23935 if (!STRINGP (object))
23936 object = it->w->buffer;
23937 #ifdef HAVE_WINDOW_SYSTEM
23938 if (FRAME_WINDOW_P (it->f))
23939 append_stretch_glyph (it, object, width, height, ascent);
23940 else
23941 #endif
23942 {
23943 it->object = object;
23944 it->char_to_display = ' ';
23945 it->pixel_width = it->len = 1;
23946 while (n--)
23947 tty_append_glyph (it);
23948 it->object = o_object;
23949 }
23950 }
23951
23952 it->pixel_width = width;
23953 #ifdef HAVE_WINDOW_SYSTEM
23954 if (FRAME_WINDOW_P (it->f))
23955 {
23956 it->ascent = it->phys_ascent = ascent;
23957 it->descent = it->phys_descent = height - it->ascent;
23958 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23959 take_vertical_position_into_account (it);
23960 }
23961 else
23962 #endif
23963 it->nglyphs = width;
23964 }
23965
23966 #ifdef HAVE_WINDOW_SYSTEM
23967
23968 /* Calculate line-height and line-spacing properties.
23969 An integer value specifies explicit pixel value.
23970 A float value specifies relative value to current face height.
23971 A cons (float . face-name) specifies relative value to
23972 height of specified face font.
23973
23974 Returns height in pixels, or nil. */
23975
23976
23977 static Lisp_Object
23978 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23979 int boff, int override)
23980 {
23981 Lisp_Object face_name = Qnil;
23982 int ascent, descent, height;
23983
23984 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23985 return val;
23986
23987 if (CONSP (val))
23988 {
23989 face_name = XCAR (val);
23990 val = XCDR (val);
23991 if (!NUMBERP (val))
23992 val = make_number (1);
23993 if (NILP (face_name))
23994 {
23995 height = it->ascent + it->descent;
23996 goto scale;
23997 }
23998 }
23999
24000 if (NILP (face_name))
24001 {
24002 font = FRAME_FONT (it->f);
24003 boff = FRAME_BASELINE_OFFSET (it->f);
24004 }
24005 else if (EQ (face_name, Qt))
24006 {
24007 override = 0;
24008 }
24009 else
24010 {
24011 int face_id;
24012 struct face *face;
24013
24014 face_id = lookup_named_face (it->f, face_name, 0);
24015 if (face_id < 0)
24016 return make_number (-1);
24017
24018 face = FACE_FROM_ID (it->f, face_id);
24019 font = face->font;
24020 if (font == NULL)
24021 return make_number (-1);
24022 boff = font->baseline_offset;
24023 if (font->vertical_centering)
24024 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24025 }
24026
24027 ascent = FONT_BASE (font) + boff;
24028 descent = FONT_DESCENT (font) - boff;
24029
24030 if (override)
24031 {
24032 it->override_ascent = ascent;
24033 it->override_descent = descent;
24034 it->override_boff = boff;
24035 }
24036
24037 height = ascent + descent;
24038
24039 scale:
24040 if (FLOATP (val))
24041 height = (int)(XFLOAT_DATA (val) * height);
24042 else if (INTEGERP (val))
24043 height *= XINT (val);
24044
24045 return make_number (height);
24046 }
24047
24048
24049 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24050 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24051 and only if this is for a character for which no font was found.
24052
24053 If the display method (it->glyphless_method) is
24054 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24055 length of the acronym or the hexadecimal string, UPPER_XOFF and
24056 UPPER_YOFF are pixel offsets for the upper part of the string,
24057 LOWER_XOFF and LOWER_YOFF are for the lower part.
24058
24059 For the other display methods, LEN through LOWER_YOFF are zero. */
24060
24061 static void
24062 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24063 short upper_xoff, short upper_yoff,
24064 short lower_xoff, short lower_yoff)
24065 {
24066 struct glyph *glyph;
24067 enum glyph_row_area area = it->area;
24068
24069 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24070 if (glyph < it->glyph_row->glyphs[area + 1])
24071 {
24072 /* If the glyph row is reversed, we need to prepend the glyph
24073 rather than append it. */
24074 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24075 {
24076 struct glyph *g;
24077
24078 /* Make room for the additional glyph. */
24079 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24080 g[1] = *g;
24081 glyph = it->glyph_row->glyphs[area];
24082 }
24083 glyph->charpos = CHARPOS (it->position);
24084 glyph->object = it->object;
24085 glyph->pixel_width = it->pixel_width;
24086 glyph->ascent = it->ascent;
24087 glyph->descent = it->descent;
24088 glyph->voffset = it->voffset;
24089 glyph->type = GLYPHLESS_GLYPH;
24090 glyph->u.glyphless.method = it->glyphless_method;
24091 glyph->u.glyphless.for_no_font = for_no_font;
24092 glyph->u.glyphless.len = len;
24093 glyph->u.glyphless.ch = it->c;
24094 glyph->slice.glyphless.upper_xoff = upper_xoff;
24095 glyph->slice.glyphless.upper_yoff = upper_yoff;
24096 glyph->slice.glyphless.lower_xoff = lower_xoff;
24097 glyph->slice.glyphless.lower_yoff = lower_yoff;
24098 glyph->avoid_cursor_p = it->avoid_cursor_p;
24099 glyph->multibyte_p = it->multibyte_p;
24100 glyph->left_box_line_p = it->start_of_box_run_p;
24101 glyph->right_box_line_p = it->end_of_box_run_p;
24102 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24103 || it->phys_descent > it->descent);
24104 glyph->padding_p = 0;
24105 glyph->glyph_not_available_p = 0;
24106 glyph->face_id = face_id;
24107 glyph->font_type = FONT_TYPE_UNKNOWN;
24108 if (it->bidi_p)
24109 {
24110 glyph->resolved_level = it->bidi_it.resolved_level;
24111 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24112 abort ();
24113 glyph->bidi_type = it->bidi_it.type;
24114 }
24115 ++it->glyph_row->used[area];
24116 }
24117 else
24118 IT_EXPAND_MATRIX_WIDTH (it, area);
24119 }
24120
24121
24122 /* Produce a glyph for a glyphless character for iterator IT.
24123 IT->glyphless_method specifies which method to use for displaying
24124 the character. See the description of enum
24125 glyphless_display_method in dispextern.h for the detail.
24126
24127 FOR_NO_FONT is nonzero if and only if this is for a character for
24128 which no font was found. ACRONYM, if non-nil, is an acronym string
24129 for the character. */
24130
24131 static void
24132 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24133 {
24134 int face_id;
24135 struct face *face;
24136 struct font *font;
24137 int base_width, base_height, width, height;
24138 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24139 int len;
24140
24141 /* Get the metrics of the base font. We always refer to the current
24142 ASCII face. */
24143 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24144 font = face->font ? face->font : FRAME_FONT (it->f);
24145 it->ascent = FONT_BASE (font) + font->baseline_offset;
24146 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24147 base_height = it->ascent + it->descent;
24148 base_width = font->average_width;
24149
24150 /* Get a face ID for the glyph by utilizing a cache (the same way as
24151 done for `escape-glyph' in get_next_display_element). */
24152 if (it->f == last_glyphless_glyph_frame
24153 && it->face_id == last_glyphless_glyph_face_id)
24154 {
24155 face_id = last_glyphless_glyph_merged_face_id;
24156 }
24157 else
24158 {
24159 /* Merge the `glyphless-char' face into the current face. */
24160 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24161 last_glyphless_glyph_frame = it->f;
24162 last_glyphless_glyph_face_id = it->face_id;
24163 last_glyphless_glyph_merged_face_id = face_id;
24164 }
24165
24166 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24167 {
24168 it->pixel_width = THIN_SPACE_WIDTH;
24169 len = 0;
24170 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24171 }
24172 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24173 {
24174 width = CHAR_WIDTH (it->c);
24175 if (width == 0)
24176 width = 1;
24177 else if (width > 4)
24178 width = 4;
24179 it->pixel_width = base_width * width;
24180 len = 0;
24181 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24182 }
24183 else
24184 {
24185 char buf[7];
24186 const char *str;
24187 unsigned int code[6];
24188 int upper_len;
24189 int ascent, descent;
24190 struct font_metrics metrics_upper, metrics_lower;
24191
24192 face = FACE_FROM_ID (it->f, face_id);
24193 font = face->font ? face->font : FRAME_FONT (it->f);
24194 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24195
24196 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24197 {
24198 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24199 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24200 if (CONSP (acronym))
24201 acronym = XCAR (acronym);
24202 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24203 }
24204 else
24205 {
24206 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24207 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24208 str = buf;
24209 }
24210 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24211 code[len] = font->driver->encode_char (font, str[len]);
24212 upper_len = (len + 1) / 2;
24213 font->driver->text_extents (font, code, upper_len,
24214 &metrics_upper);
24215 font->driver->text_extents (font, code + upper_len, len - upper_len,
24216 &metrics_lower);
24217
24218
24219
24220 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24221 width = max (metrics_upper.width, metrics_lower.width) + 4;
24222 upper_xoff = upper_yoff = 2; /* the typical case */
24223 if (base_width >= width)
24224 {
24225 /* Align the upper to the left, the lower to the right. */
24226 it->pixel_width = base_width;
24227 lower_xoff = base_width - 2 - metrics_lower.width;
24228 }
24229 else
24230 {
24231 /* Center the shorter one. */
24232 it->pixel_width = width;
24233 if (metrics_upper.width >= metrics_lower.width)
24234 lower_xoff = (width - metrics_lower.width) / 2;
24235 else
24236 {
24237 /* FIXME: This code doesn't look right. It formerly was
24238 missing the "lower_xoff = 0;", which couldn't have
24239 been right since it left lower_xoff uninitialized. */
24240 lower_xoff = 0;
24241 upper_xoff = (width - metrics_upper.width) / 2;
24242 }
24243 }
24244
24245 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24246 top, bottom, and between upper and lower strings. */
24247 height = (metrics_upper.ascent + metrics_upper.descent
24248 + metrics_lower.ascent + metrics_lower.descent) + 5;
24249 /* Center vertically.
24250 H:base_height, D:base_descent
24251 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24252
24253 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24254 descent = D - H/2 + h/2;
24255 lower_yoff = descent - 2 - ld;
24256 upper_yoff = lower_yoff - la - 1 - ud; */
24257 ascent = - (it->descent - (base_height + height + 1) / 2);
24258 descent = it->descent - (base_height - height) / 2;
24259 lower_yoff = descent - 2 - metrics_lower.descent;
24260 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24261 - metrics_upper.descent);
24262 /* Don't make the height shorter than the base height. */
24263 if (height > base_height)
24264 {
24265 it->ascent = ascent;
24266 it->descent = descent;
24267 }
24268 }
24269
24270 it->phys_ascent = it->ascent;
24271 it->phys_descent = it->descent;
24272 if (it->glyph_row)
24273 append_glyphless_glyph (it, face_id, for_no_font, len,
24274 upper_xoff, upper_yoff,
24275 lower_xoff, lower_yoff);
24276 it->nglyphs = 1;
24277 take_vertical_position_into_account (it);
24278 }
24279
24280
24281 /* RIF:
24282 Produce glyphs/get display metrics for the display element IT is
24283 loaded with. See the description of struct it in dispextern.h
24284 for an overview of struct it. */
24285
24286 void
24287 x_produce_glyphs (struct it *it)
24288 {
24289 int extra_line_spacing = it->extra_line_spacing;
24290
24291 it->glyph_not_available_p = 0;
24292
24293 if (it->what == IT_CHARACTER)
24294 {
24295 XChar2b char2b;
24296 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24297 struct font *font = face->font;
24298 struct font_metrics *pcm = NULL;
24299 int boff; /* baseline offset */
24300
24301 if (font == NULL)
24302 {
24303 /* When no suitable font is found, display this character by
24304 the method specified in the first extra slot of
24305 Vglyphless_char_display. */
24306 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24307
24308 xassert (it->what == IT_GLYPHLESS);
24309 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24310 goto done;
24311 }
24312
24313 boff = font->baseline_offset;
24314 if (font->vertical_centering)
24315 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24316
24317 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24318 {
24319 int stretched_p;
24320
24321 it->nglyphs = 1;
24322
24323 if (it->override_ascent >= 0)
24324 {
24325 it->ascent = it->override_ascent;
24326 it->descent = it->override_descent;
24327 boff = it->override_boff;
24328 }
24329 else
24330 {
24331 it->ascent = FONT_BASE (font) + boff;
24332 it->descent = FONT_DESCENT (font) - boff;
24333 }
24334
24335 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24336 {
24337 pcm = get_per_char_metric (font, &char2b);
24338 if (pcm->width == 0
24339 && pcm->rbearing == 0 && pcm->lbearing == 0)
24340 pcm = NULL;
24341 }
24342
24343 if (pcm)
24344 {
24345 it->phys_ascent = pcm->ascent + boff;
24346 it->phys_descent = pcm->descent - boff;
24347 it->pixel_width = pcm->width;
24348 }
24349 else
24350 {
24351 it->glyph_not_available_p = 1;
24352 it->phys_ascent = it->ascent;
24353 it->phys_descent = it->descent;
24354 it->pixel_width = font->space_width;
24355 }
24356
24357 if (it->constrain_row_ascent_descent_p)
24358 {
24359 if (it->descent > it->max_descent)
24360 {
24361 it->ascent += it->descent - it->max_descent;
24362 it->descent = it->max_descent;
24363 }
24364 if (it->ascent > it->max_ascent)
24365 {
24366 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24367 it->ascent = it->max_ascent;
24368 }
24369 it->phys_ascent = min (it->phys_ascent, it->ascent);
24370 it->phys_descent = min (it->phys_descent, it->descent);
24371 extra_line_spacing = 0;
24372 }
24373
24374 /* If this is a space inside a region of text with
24375 `space-width' property, change its width. */
24376 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24377 if (stretched_p)
24378 it->pixel_width *= XFLOATINT (it->space_width);
24379
24380 /* If face has a box, add the box thickness to the character
24381 height. If character has a box line to the left and/or
24382 right, add the box line width to the character's width. */
24383 if (face->box != FACE_NO_BOX)
24384 {
24385 int thick = face->box_line_width;
24386
24387 if (thick > 0)
24388 {
24389 it->ascent += thick;
24390 it->descent += thick;
24391 }
24392 else
24393 thick = -thick;
24394
24395 if (it->start_of_box_run_p)
24396 it->pixel_width += thick;
24397 if (it->end_of_box_run_p)
24398 it->pixel_width += thick;
24399 }
24400
24401 /* If face has an overline, add the height of the overline
24402 (1 pixel) and a 1 pixel margin to the character height. */
24403 if (face->overline_p)
24404 it->ascent += overline_margin;
24405
24406 if (it->constrain_row_ascent_descent_p)
24407 {
24408 if (it->ascent > it->max_ascent)
24409 it->ascent = it->max_ascent;
24410 if (it->descent > it->max_descent)
24411 it->descent = it->max_descent;
24412 }
24413
24414 take_vertical_position_into_account (it);
24415
24416 /* If we have to actually produce glyphs, do it. */
24417 if (it->glyph_row)
24418 {
24419 if (stretched_p)
24420 {
24421 /* Translate a space with a `space-width' property
24422 into a stretch glyph. */
24423 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24424 / FONT_HEIGHT (font));
24425 append_stretch_glyph (it, it->object, it->pixel_width,
24426 it->ascent + it->descent, ascent);
24427 }
24428 else
24429 append_glyph (it);
24430
24431 /* If characters with lbearing or rbearing are displayed
24432 in this line, record that fact in a flag of the
24433 glyph row. This is used to optimize X output code. */
24434 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24435 it->glyph_row->contains_overlapping_glyphs_p = 1;
24436 }
24437 if (! stretched_p && it->pixel_width == 0)
24438 /* We assure that all visible glyphs have at least 1-pixel
24439 width. */
24440 it->pixel_width = 1;
24441 }
24442 else if (it->char_to_display == '\n')
24443 {
24444 /* A newline has no width, but we need the height of the
24445 line. But if previous part of the line sets a height,
24446 don't increase that height */
24447
24448 Lisp_Object height;
24449 Lisp_Object total_height = Qnil;
24450
24451 it->override_ascent = -1;
24452 it->pixel_width = 0;
24453 it->nglyphs = 0;
24454
24455 height = get_it_property (it, Qline_height);
24456 /* Split (line-height total-height) list */
24457 if (CONSP (height)
24458 && CONSP (XCDR (height))
24459 && NILP (XCDR (XCDR (height))))
24460 {
24461 total_height = XCAR (XCDR (height));
24462 height = XCAR (height);
24463 }
24464 height = calc_line_height_property (it, height, font, boff, 1);
24465
24466 if (it->override_ascent >= 0)
24467 {
24468 it->ascent = it->override_ascent;
24469 it->descent = it->override_descent;
24470 boff = it->override_boff;
24471 }
24472 else
24473 {
24474 it->ascent = FONT_BASE (font) + boff;
24475 it->descent = FONT_DESCENT (font) - boff;
24476 }
24477
24478 if (EQ (height, Qt))
24479 {
24480 if (it->descent > it->max_descent)
24481 {
24482 it->ascent += it->descent - it->max_descent;
24483 it->descent = it->max_descent;
24484 }
24485 if (it->ascent > it->max_ascent)
24486 {
24487 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24488 it->ascent = it->max_ascent;
24489 }
24490 it->phys_ascent = min (it->phys_ascent, it->ascent);
24491 it->phys_descent = min (it->phys_descent, it->descent);
24492 it->constrain_row_ascent_descent_p = 1;
24493 extra_line_spacing = 0;
24494 }
24495 else
24496 {
24497 Lisp_Object spacing;
24498
24499 it->phys_ascent = it->ascent;
24500 it->phys_descent = it->descent;
24501
24502 if ((it->max_ascent > 0 || it->max_descent > 0)
24503 && face->box != FACE_NO_BOX
24504 && face->box_line_width > 0)
24505 {
24506 it->ascent += face->box_line_width;
24507 it->descent += face->box_line_width;
24508 }
24509 if (!NILP (height)
24510 && XINT (height) > it->ascent + it->descent)
24511 it->ascent = XINT (height) - it->descent;
24512
24513 if (!NILP (total_height))
24514 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24515 else
24516 {
24517 spacing = get_it_property (it, Qline_spacing);
24518 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24519 }
24520 if (INTEGERP (spacing))
24521 {
24522 extra_line_spacing = XINT (spacing);
24523 if (!NILP (total_height))
24524 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24525 }
24526 }
24527 }
24528 else /* i.e. (it->char_to_display == '\t') */
24529 {
24530 if (font->space_width > 0)
24531 {
24532 int tab_width = it->tab_width * font->space_width;
24533 int x = it->current_x + it->continuation_lines_width;
24534 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24535
24536 /* If the distance from the current position to the next tab
24537 stop is less than a space character width, use the
24538 tab stop after that. */
24539 if (next_tab_x - x < font->space_width)
24540 next_tab_x += tab_width;
24541
24542 it->pixel_width = next_tab_x - x;
24543 it->nglyphs = 1;
24544 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24545 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24546
24547 if (it->glyph_row)
24548 {
24549 append_stretch_glyph (it, it->object, it->pixel_width,
24550 it->ascent + it->descent, it->ascent);
24551 }
24552 }
24553 else
24554 {
24555 it->pixel_width = 0;
24556 it->nglyphs = 1;
24557 }
24558 }
24559 }
24560 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24561 {
24562 /* A static composition.
24563
24564 Note: A composition is represented as one glyph in the
24565 glyph matrix. There are no padding glyphs.
24566
24567 Important note: pixel_width, ascent, and descent are the
24568 values of what is drawn by draw_glyphs (i.e. the values of
24569 the overall glyphs composed). */
24570 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24571 int boff; /* baseline offset */
24572 struct composition *cmp = composition_table[it->cmp_it.id];
24573 int glyph_len = cmp->glyph_len;
24574 struct font *font = face->font;
24575
24576 it->nglyphs = 1;
24577
24578 /* If we have not yet calculated pixel size data of glyphs of
24579 the composition for the current face font, calculate them
24580 now. Theoretically, we have to check all fonts for the
24581 glyphs, but that requires much time and memory space. So,
24582 here we check only the font of the first glyph. This may
24583 lead to incorrect display, but it's very rare, and C-l
24584 (recenter-top-bottom) can correct the display anyway. */
24585 if (! cmp->font || cmp->font != font)
24586 {
24587 /* Ascent and descent of the font of the first character
24588 of this composition (adjusted by baseline offset).
24589 Ascent and descent of overall glyphs should not be less
24590 than these, respectively. */
24591 int font_ascent, font_descent, font_height;
24592 /* Bounding box of the overall glyphs. */
24593 int leftmost, rightmost, lowest, highest;
24594 int lbearing, rbearing;
24595 int i, width, ascent, descent;
24596 int left_padded = 0, right_padded = 0;
24597 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24598 XChar2b char2b;
24599 struct font_metrics *pcm;
24600 int font_not_found_p;
24601 EMACS_INT pos;
24602
24603 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24604 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24605 break;
24606 if (glyph_len < cmp->glyph_len)
24607 right_padded = 1;
24608 for (i = 0; i < glyph_len; i++)
24609 {
24610 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24611 break;
24612 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24613 }
24614 if (i > 0)
24615 left_padded = 1;
24616
24617 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24618 : IT_CHARPOS (*it));
24619 /* If no suitable font is found, use the default font. */
24620 font_not_found_p = font == NULL;
24621 if (font_not_found_p)
24622 {
24623 face = face->ascii_face;
24624 font = face->font;
24625 }
24626 boff = font->baseline_offset;
24627 if (font->vertical_centering)
24628 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24629 font_ascent = FONT_BASE (font) + boff;
24630 font_descent = FONT_DESCENT (font) - boff;
24631 font_height = FONT_HEIGHT (font);
24632
24633 cmp->font = (void *) font;
24634
24635 pcm = NULL;
24636 if (! font_not_found_p)
24637 {
24638 get_char_face_and_encoding (it->f, c, it->face_id,
24639 &char2b, 0);
24640 pcm = get_per_char_metric (font, &char2b);
24641 }
24642
24643 /* Initialize the bounding box. */
24644 if (pcm)
24645 {
24646 width = cmp->glyph_len > 0 ? pcm->width : 0;
24647 ascent = pcm->ascent;
24648 descent = pcm->descent;
24649 lbearing = pcm->lbearing;
24650 rbearing = pcm->rbearing;
24651 }
24652 else
24653 {
24654 width = cmp->glyph_len > 0 ? font->space_width : 0;
24655 ascent = FONT_BASE (font);
24656 descent = FONT_DESCENT (font);
24657 lbearing = 0;
24658 rbearing = width;
24659 }
24660
24661 rightmost = width;
24662 leftmost = 0;
24663 lowest = - descent + boff;
24664 highest = ascent + boff;
24665
24666 if (! font_not_found_p
24667 && font->default_ascent
24668 && CHAR_TABLE_P (Vuse_default_ascent)
24669 && !NILP (Faref (Vuse_default_ascent,
24670 make_number (it->char_to_display))))
24671 highest = font->default_ascent + boff;
24672
24673 /* Draw the first glyph at the normal position. It may be
24674 shifted to right later if some other glyphs are drawn
24675 at the left. */
24676 cmp->offsets[i * 2] = 0;
24677 cmp->offsets[i * 2 + 1] = boff;
24678 cmp->lbearing = lbearing;
24679 cmp->rbearing = rbearing;
24680
24681 /* Set cmp->offsets for the remaining glyphs. */
24682 for (i++; i < glyph_len; i++)
24683 {
24684 int left, right, btm, top;
24685 int ch = COMPOSITION_GLYPH (cmp, i);
24686 int face_id;
24687 struct face *this_face;
24688
24689 if (ch == '\t')
24690 ch = ' ';
24691 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24692 this_face = FACE_FROM_ID (it->f, face_id);
24693 font = this_face->font;
24694
24695 if (font == NULL)
24696 pcm = NULL;
24697 else
24698 {
24699 get_char_face_and_encoding (it->f, ch, face_id,
24700 &char2b, 0);
24701 pcm = get_per_char_metric (font, &char2b);
24702 }
24703 if (! pcm)
24704 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24705 else
24706 {
24707 width = pcm->width;
24708 ascent = pcm->ascent;
24709 descent = pcm->descent;
24710 lbearing = pcm->lbearing;
24711 rbearing = pcm->rbearing;
24712 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24713 {
24714 /* Relative composition with or without
24715 alternate chars. */
24716 left = (leftmost + rightmost - width) / 2;
24717 btm = - descent + boff;
24718 if (font->relative_compose
24719 && (! CHAR_TABLE_P (Vignore_relative_composition)
24720 || NILP (Faref (Vignore_relative_composition,
24721 make_number (ch)))))
24722 {
24723
24724 if (- descent >= font->relative_compose)
24725 /* One extra pixel between two glyphs. */
24726 btm = highest + 1;
24727 else if (ascent <= 0)
24728 /* One extra pixel between two glyphs. */
24729 btm = lowest - 1 - ascent - descent;
24730 }
24731 }
24732 else
24733 {
24734 /* A composition rule is specified by an integer
24735 value that encodes global and new reference
24736 points (GREF and NREF). GREF and NREF are
24737 specified by numbers as below:
24738
24739 0---1---2 -- ascent
24740 | |
24741 | |
24742 | |
24743 9--10--11 -- center
24744 | |
24745 ---3---4---5--- baseline
24746 | |
24747 6---7---8 -- descent
24748 */
24749 int rule = COMPOSITION_RULE (cmp, i);
24750 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24751
24752 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24753 grefx = gref % 3, nrefx = nref % 3;
24754 grefy = gref / 3, nrefy = nref / 3;
24755 if (xoff)
24756 xoff = font_height * (xoff - 128) / 256;
24757 if (yoff)
24758 yoff = font_height * (yoff - 128) / 256;
24759
24760 left = (leftmost
24761 + grefx * (rightmost - leftmost) / 2
24762 - nrefx * width / 2
24763 + xoff);
24764
24765 btm = ((grefy == 0 ? highest
24766 : grefy == 1 ? 0
24767 : grefy == 2 ? lowest
24768 : (highest + lowest) / 2)
24769 - (nrefy == 0 ? ascent + descent
24770 : nrefy == 1 ? descent - boff
24771 : nrefy == 2 ? 0
24772 : (ascent + descent) / 2)
24773 + yoff);
24774 }
24775
24776 cmp->offsets[i * 2] = left;
24777 cmp->offsets[i * 2 + 1] = btm + descent;
24778
24779 /* Update the bounding box of the overall glyphs. */
24780 if (width > 0)
24781 {
24782 right = left + width;
24783 if (left < leftmost)
24784 leftmost = left;
24785 if (right > rightmost)
24786 rightmost = right;
24787 }
24788 top = btm + descent + ascent;
24789 if (top > highest)
24790 highest = top;
24791 if (btm < lowest)
24792 lowest = btm;
24793
24794 if (cmp->lbearing > left + lbearing)
24795 cmp->lbearing = left + lbearing;
24796 if (cmp->rbearing < left + rbearing)
24797 cmp->rbearing = left + rbearing;
24798 }
24799 }
24800
24801 /* If there are glyphs whose x-offsets are negative,
24802 shift all glyphs to the right and make all x-offsets
24803 non-negative. */
24804 if (leftmost < 0)
24805 {
24806 for (i = 0; i < cmp->glyph_len; i++)
24807 cmp->offsets[i * 2] -= leftmost;
24808 rightmost -= leftmost;
24809 cmp->lbearing -= leftmost;
24810 cmp->rbearing -= leftmost;
24811 }
24812
24813 if (left_padded && cmp->lbearing < 0)
24814 {
24815 for (i = 0; i < cmp->glyph_len; i++)
24816 cmp->offsets[i * 2] -= cmp->lbearing;
24817 rightmost -= cmp->lbearing;
24818 cmp->rbearing -= cmp->lbearing;
24819 cmp->lbearing = 0;
24820 }
24821 if (right_padded && rightmost < cmp->rbearing)
24822 {
24823 rightmost = cmp->rbearing;
24824 }
24825
24826 cmp->pixel_width = rightmost;
24827 cmp->ascent = highest;
24828 cmp->descent = - lowest;
24829 if (cmp->ascent < font_ascent)
24830 cmp->ascent = font_ascent;
24831 if (cmp->descent < font_descent)
24832 cmp->descent = font_descent;
24833 }
24834
24835 if (it->glyph_row
24836 && (cmp->lbearing < 0
24837 || cmp->rbearing > cmp->pixel_width))
24838 it->glyph_row->contains_overlapping_glyphs_p = 1;
24839
24840 it->pixel_width = cmp->pixel_width;
24841 it->ascent = it->phys_ascent = cmp->ascent;
24842 it->descent = it->phys_descent = cmp->descent;
24843 if (face->box != FACE_NO_BOX)
24844 {
24845 int thick = face->box_line_width;
24846
24847 if (thick > 0)
24848 {
24849 it->ascent += thick;
24850 it->descent += thick;
24851 }
24852 else
24853 thick = - thick;
24854
24855 if (it->start_of_box_run_p)
24856 it->pixel_width += thick;
24857 if (it->end_of_box_run_p)
24858 it->pixel_width += thick;
24859 }
24860
24861 /* If face has an overline, add the height of the overline
24862 (1 pixel) and a 1 pixel margin to the character height. */
24863 if (face->overline_p)
24864 it->ascent += overline_margin;
24865
24866 take_vertical_position_into_account (it);
24867 if (it->ascent < 0)
24868 it->ascent = 0;
24869 if (it->descent < 0)
24870 it->descent = 0;
24871
24872 if (it->glyph_row && cmp->glyph_len > 0)
24873 append_composite_glyph (it);
24874 }
24875 else if (it->what == IT_COMPOSITION)
24876 {
24877 /* A dynamic (automatic) composition. */
24878 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24879 Lisp_Object gstring;
24880 struct font_metrics metrics;
24881
24882 it->nglyphs = 1;
24883
24884 gstring = composition_gstring_from_id (it->cmp_it.id);
24885 it->pixel_width
24886 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24887 &metrics);
24888 if (it->glyph_row
24889 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24890 it->glyph_row->contains_overlapping_glyphs_p = 1;
24891 it->ascent = it->phys_ascent = metrics.ascent;
24892 it->descent = it->phys_descent = metrics.descent;
24893 if (face->box != FACE_NO_BOX)
24894 {
24895 int thick = face->box_line_width;
24896
24897 if (thick > 0)
24898 {
24899 it->ascent += thick;
24900 it->descent += thick;
24901 }
24902 else
24903 thick = - thick;
24904
24905 if (it->start_of_box_run_p)
24906 it->pixel_width += thick;
24907 if (it->end_of_box_run_p)
24908 it->pixel_width += thick;
24909 }
24910 /* If face has an overline, add the height of the overline
24911 (1 pixel) and a 1 pixel margin to the character height. */
24912 if (face->overline_p)
24913 it->ascent += overline_margin;
24914 take_vertical_position_into_account (it);
24915 if (it->ascent < 0)
24916 it->ascent = 0;
24917 if (it->descent < 0)
24918 it->descent = 0;
24919
24920 if (it->glyph_row)
24921 append_composite_glyph (it);
24922 }
24923 else if (it->what == IT_GLYPHLESS)
24924 produce_glyphless_glyph (it, 0, Qnil);
24925 else if (it->what == IT_IMAGE)
24926 produce_image_glyph (it);
24927 else if (it->what == IT_STRETCH)
24928 produce_stretch_glyph (it);
24929
24930 done:
24931 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24932 because this isn't true for images with `:ascent 100'. */
24933 xassert (it->ascent >= 0 && it->descent >= 0);
24934 if (it->area == TEXT_AREA)
24935 it->current_x += it->pixel_width;
24936
24937 if (extra_line_spacing > 0)
24938 {
24939 it->descent += extra_line_spacing;
24940 if (extra_line_spacing > it->max_extra_line_spacing)
24941 it->max_extra_line_spacing = extra_line_spacing;
24942 }
24943
24944 it->max_ascent = max (it->max_ascent, it->ascent);
24945 it->max_descent = max (it->max_descent, it->descent);
24946 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24947 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24948 }
24949
24950 /* EXPORT for RIF:
24951 Output LEN glyphs starting at START at the nominal cursor position.
24952 Advance the nominal cursor over the text. The global variable
24953 updated_window contains the window being updated, updated_row is
24954 the glyph row being updated, and updated_area is the area of that
24955 row being updated. */
24956
24957 void
24958 x_write_glyphs (struct glyph *start, int len)
24959 {
24960 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24961
24962 xassert (updated_window && updated_row);
24963 /* When the window is hscrolled, cursor hpos can legitimately be out
24964 of bounds, but we draw the cursor at the corresponding window
24965 margin in that case. */
24966 if (!updated_row->reversed_p && chpos < 0)
24967 chpos = 0;
24968 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24969 chpos = updated_row->used[TEXT_AREA] - 1;
24970
24971 BLOCK_INPUT;
24972
24973 /* Write glyphs. */
24974
24975 hpos = start - updated_row->glyphs[updated_area];
24976 x = draw_glyphs (updated_window, output_cursor.x,
24977 updated_row, updated_area,
24978 hpos, hpos + len,
24979 DRAW_NORMAL_TEXT, 0);
24980
24981 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24982 if (updated_area == TEXT_AREA
24983 && updated_window->phys_cursor_on_p
24984 && updated_window->phys_cursor.vpos == output_cursor.vpos
24985 && chpos >= hpos
24986 && chpos < hpos + len)
24987 updated_window->phys_cursor_on_p = 0;
24988
24989 UNBLOCK_INPUT;
24990
24991 /* Advance the output cursor. */
24992 output_cursor.hpos += len;
24993 output_cursor.x = x;
24994 }
24995
24996
24997 /* EXPORT for RIF:
24998 Insert LEN glyphs from START at the nominal cursor position. */
24999
25000 void
25001 x_insert_glyphs (struct glyph *start, int len)
25002 {
25003 struct frame *f;
25004 struct window *w;
25005 int line_height, shift_by_width, shifted_region_width;
25006 struct glyph_row *row;
25007 struct glyph *glyph;
25008 int frame_x, frame_y;
25009 EMACS_INT hpos;
25010
25011 xassert (updated_window && updated_row);
25012 BLOCK_INPUT;
25013 w = updated_window;
25014 f = XFRAME (WINDOW_FRAME (w));
25015
25016 /* Get the height of the line we are in. */
25017 row = updated_row;
25018 line_height = row->height;
25019
25020 /* Get the width of the glyphs to insert. */
25021 shift_by_width = 0;
25022 for (glyph = start; glyph < start + len; ++glyph)
25023 shift_by_width += glyph->pixel_width;
25024
25025 /* Get the width of the region to shift right. */
25026 shifted_region_width = (window_box_width (w, updated_area)
25027 - output_cursor.x
25028 - shift_by_width);
25029
25030 /* Shift right. */
25031 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25032 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25033
25034 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25035 line_height, shift_by_width);
25036
25037 /* Write the glyphs. */
25038 hpos = start - row->glyphs[updated_area];
25039 draw_glyphs (w, output_cursor.x, row, updated_area,
25040 hpos, hpos + len,
25041 DRAW_NORMAL_TEXT, 0);
25042
25043 /* Advance the output cursor. */
25044 output_cursor.hpos += len;
25045 output_cursor.x += shift_by_width;
25046 UNBLOCK_INPUT;
25047 }
25048
25049
25050 /* EXPORT for RIF:
25051 Erase the current text line from the nominal cursor position
25052 (inclusive) to pixel column TO_X (exclusive). The idea is that
25053 everything from TO_X onward is already erased.
25054
25055 TO_X is a pixel position relative to updated_area of
25056 updated_window. TO_X == -1 means clear to the end of this area. */
25057
25058 void
25059 x_clear_end_of_line (int to_x)
25060 {
25061 struct frame *f;
25062 struct window *w = updated_window;
25063 int max_x, min_y, max_y;
25064 int from_x, from_y, to_y;
25065
25066 xassert (updated_window && updated_row);
25067 f = XFRAME (w->frame);
25068
25069 if (updated_row->full_width_p)
25070 max_x = WINDOW_TOTAL_WIDTH (w);
25071 else
25072 max_x = window_box_width (w, updated_area);
25073 max_y = window_text_bottom_y (w);
25074
25075 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25076 of window. For TO_X > 0, truncate to end of drawing area. */
25077 if (to_x == 0)
25078 return;
25079 else if (to_x < 0)
25080 to_x = max_x;
25081 else
25082 to_x = min (to_x, max_x);
25083
25084 to_y = min (max_y, output_cursor.y + updated_row->height);
25085
25086 /* Notice if the cursor will be cleared by this operation. */
25087 if (!updated_row->full_width_p)
25088 notice_overwritten_cursor (w, updated_area,
25089 output_cursor.x, -1,
25090 updated_row->y,
25091 MATRIX_ROW_BOTTOM_Y (updated_row));
25092
25093 from_x = output_cursor.x;
25094
25095 /* Translate to frame coordinates. */
25096 if (updated_row->full_width_p)
25097 {
25098 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25099 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25100 }
25101 else
25102 {
25103 int area_left = window_box_left (w, updated_area);
25104 from_x += area_left;
25105 to_x += area_left;
25106 }
25107
25108 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25109 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25110 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25111
25112 /* Prevent inadvertently clearing to end of the X window. */
25113 if (to_x > from_x && to_y > from_y)
25114 {
25115 BLOCK_INPUT;
25116 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25117 to_x - from_x, to_y - from_y);
25118 UNBLOCK_INPUT;
25119 }
25120 }
25121
25122 #endif /* HAVE_WINDOW_SYSTEM */
25123
25124
25125 \f
25126 /***********************************************************************
25127 Cursor types
25128 ***********************************************************************/
25129
25130 /* Value is the internal representation of the specified cursor type
25131 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25132 of the bar cursor. */
25133
25134 static enum text_cursor_kinds
25135 get_specified_cursor_type (Lisp_Object arg, int *width)
25136 {
25137 enum text_cursor_kinds type;
25138
25139 if (NILP (arg))
25140 return NO_CURSOR;
25141
25142 if (EQ (arg, Qbox))
25143 return FILLED_BOX_CURSOR;
25144
25145 if (EQ (arg, Qhollow))
25146 return HOLLOW_BOX_CURSOR;
25147
25148 if (EQ (arg, Qbar))
25149 {
25150 *width = 2;
25151 return BAR_CURSOR;
25152 }
25153
25154 if (CONSP (arg)
25155 && EQ (XCAR (arg), Qbar)
25156 && INTEGERP (XCDR (arg))
25157 && XINT (XCDR (arg)) >= 0)
25158 {
25159 *width = XINT (XCDR (arg));
25160 return BAR_CURSOR;
25161 }
25162
25163 if (EQ (arg, Qhbar))
25164 {
25165 *width = 2;
25166 return HBAR_CURSOR;
25167 }
25168
25169 if (CONSP (arg)
25170 && EQ (XCAR (arg), Qhbar)
25171 && INTEGERP (XCDR (arg))
25172 && XINT (XCDR (arg)) >= 0)
25173 {
25174 *width = XINT (XCDR (arg));
25175 return HBAR_CURSOR;
25176 }
25177
25178 /* Treat anything unknown as "hollow box cursor".
25179 It was bad to signal an error; people have trouble fixing
25180 .Xdefaults with Emacs, when it has something bad in it. */
25181 type = HOLLOW_BOX_CURSOR;
25182
25183 return type;
25184 }
25185
25186 /* Set the default cursor types for specified frame. */
25187 void
25188 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25189 {
25190 int width = 1;
25191 Lisp_Object tem;
25192
25193 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25194 FRAME_CURSOR_WIDTH (f) = width;
25195
25196 /* By default, set up the blink-off state depending on the on-state. */
25197
25198 tem = Fassoc (arg, Vblink_cursor_alist);
25199 if (!NILP (tem))
25200 {
25201 FRAME_BLINK_OFF_CURSOR (f)
25202 = get_specified_cursor_type (XCDR (tem), &width);
25203 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25204 }
25205 else
25206 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25207 }
25208
25209
25210 #ifdef HAVE_WINDOW_SYSTEM
25211
25212 /* Return the cursor we want to be displayed in window W. Return
25213 width of bar/hbar cursor through WIDTH arg. Return with
25214 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25215 (i.e. if the `system caret' should track this cursor).
25216
25217 In a mini-buffer window, we want the cursor only to appear if we
25218 are reading input from this window. For the selected window, we
25219 want the cursor type given by the frame parameter or buffer local
25220 setting of cursor-type. If explicitly marked off, draw no cursor.
25221 In all other cases, we want a hollow box cursor. */
25222
25223 static enum text_cursor_kinds
25224 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25225 int *active_cursor)
25226 {
25227 struct frame *f = XFRAME (w->frame);
25228 struct buffer *b = XBUFFER (w->buffer);
25229 int cursor_type = DEFAULT_CURSOR;
25230 Lisp_Object alt_cursor;
25231 int non_selected = 0;
25232
25233 *active_cursor = 1;
25234
25235 /* Echo area */
25236 if (cursor_in_echo_area
25237 && FRAME_HAS_MINIBUF_P (f)
25238 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25239 {
25240 if (w == XWINDOW (echo_area_window))
25241 {
25242 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25243 {
25244 *width = FRAME_CURSOR_WIDTH (f);
25245 return FRAME_DESIRED_CURSOR (f);
25246 }
25247 else
25248 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25249 }
25250
25251 *active_cursor = 0;
25252 non_selected = 1;
25253 }
25254
25255 /* Detect a nonselected window or nonselected frame. */
25256 else if (w != XWINDOW (f->selected_window)
25257 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25258 {
25259 *active_cursor = 0;
25260
25261 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25262 return NO_CURSOR;
25263
25264 non_selected = 1;
25265 }
25266
25267 /* Never display a cursor in a window in which cursor-type is nil. */
25268 if (NILP (BVAR (b, cursor_type)))
25269 return NO_CURSOR;
25270
25271 /* Get the normal cursor type for this window. */
25272 if (EQ (BVAR (b, cursor_type), Qt))
25273 {
25274 cursor_type = FRAME_DESIRED_CURSOR (f);
25275 *width = FRAME_CURSOR_WIDTH (f);
25276 }
25277 else
25278 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25279
25280 /* Use cursor-in-non-selected-windows instead
25281 for non-selected window or frame. */
25282 if (non_selected)
25283 {
25284 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25285 if (!EQ (Qt, alt_cursor))
25286 return get_specified_cursor_type (alt_cursor, width);
25287 /* t means modify the normal cursor type. */
25288 if (cursor_type == FILLED_BOX_CURSOR)
25289 cursor_type = HOLLOW_BOX_CURSOR;
25290 else if (cursor_type == BAR_CURSOR && *width > 1)
25291 --*width;
25292 return cursor_type;
25293 }
25294
25295 /* Use normal cursor if not blinked off. */
25296 if (!w->cursor_off_p)
25297 {
25298 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25299 {
25300 if (cursor_type == FILLED_BOX_CURSOR)
25301 {
25302 /* Using a block cursor on large images can be very annoying.
25303 So use a hollow cursor for "large" images.
25304 If image is not transparent (no mask), also use hollow cursor. */
25305 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25306 if (img != NULL && IMAGEP (img->spec))
25307 {
25308 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25309 where N = size of default frame font size.
25310 This should cover most of the "tiny" icons people may use. */
25311 if (!img->mask
25312 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25313 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25314 cursor_type = HOLLOW_BOX_CURSOR;
25315 }
25316 }
25317 else if (cursor_type != NO_CURSOR)
25318 {
25319 /* Display current only supports BOX and HOLLOW cursors for images.
25320 So for now, unconditionally use a HOLLOW cursor when cursor is
25321 not a solid box cursor. */
25322 cursor_type = HOLLOW_BOX_CURSOR;
25323 }
25324 }
25325 return cursor_type;
25326 }
25327
25328 /* Cursor is blinked off, so determine how to "toggle" it. */
25329
25330 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25331 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25332 return get_specified_cursor_type (XCDR (alt_cursor), width);
25333
25334 /* Then see if frame has specified a specific blink off cursor type. */
25335 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25336 {
25337 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25338 return FRAME_BLINK_OFF_CURSOR (f);
25339 }
25340
25341 #if 0
25342 /* Some people liked having a permanently visible blinking cursor,
25343 while others had very strong opinions against it. So it was
25344 decided to remove it. KFS 2003-09-03 */
25345
25346 /* Finally perform built-in cursor blinking:
25347 filled box <-> hollow box
25348 wide [h]bar <-> narrow [h]bar
25349 narrow [h]bar <-> no cursor
25350 other type <-> no cursor */
25351
25352 if (cursor_type == FILLED_BOX_CURSOR)
25353 return HOLLOW_BOX_CURSOR;
25354
25355 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25356 {
25357 *width = 1;
25358 return cursor_type;
25359 }
25360 #endif
25361
25362 return NO_CURSOR;
25363 }
25364
25365
25366 /* Notice when the text cursor of window W has been completely
25367 overwritten by a drawing operation that outputs glyphs in AREA
25368 starting at X0 and ending at X1 in the line starting at Y0 and
25369 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25370 the rest of the line after X0 has been written. Y coordinates
25371 are window-relative. */
25372
25373 static void
25374 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25375 int x0, int x1, int y0, int y1)
25376 {
25377 int cx0, cx1, cy0, cy1;
25378 struct glyph_row *row;
25379
25380 if (!w->phys_cursor_on_p)
25381 return;
25382 if (area != TEXT_AREA)
25383 return;
25384
25385 if (w->phys_cursor.vpos < 0
25386 || w->phys_cursor.vpos >= w->current_matrix->nrows
25387 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25388 !(row->enabled_p && row->displays_text_p)))
25389 return;
25390
25391 if (row->cursor_in_fringe_p)
25392 {
25393 row->cursor_in_fringe_p = 0;
25394 draw_fringe_bitmap (w, row, row->reversed_p);
25395 w->phys_cursor_on_p = 0;
25396 return;
25397 }
25398
25399 cx0 = w->phys_cursor.x;
25400 cx1 = cx0 + w->phys_cursor_width;
25401 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25402 return;
25403
25404 /* The cursor image will be completely removed from the
25405 screen if the output area intersects the cursor area in
25406 y-direction. When we draw in [y0 y1[, and some part of
25407 the cursor is at y < y0, that part must have been drawn
25408 before. When scrolling, the cursor is erased before
25409 actually scrolling, so we don't come here. When not
25410 scrolling, the rows above the old cursor row must have
25411 changed, and in this case these rows must have written
25412 over the cursor image.
25413
25414 Likewise if part of the cursor is below y1, with the
25415 exception of the cursor being in the first blank row at
25416 the buffer and window end because update_text_area
25417 doesn't draw that row. (Except when it does, but
25418 that's handled in update_text_area.) */
25419
25420 cy0 = w->phys_cursor.y;
25421 cy1 = cy0 + w->phys_cursor_height;
25422 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25423 return;
25424
25425 w->phys_cursor_on_p = 0;
25426 }
25427
25428 #endif /* HAVE_WINDOW_SYSTEM */
25429
25430 \f
25431 /************************************************************************
25432 Mouse Face
25433 ************************************************************************/
25434
25435 #ifdef HAVE_WINDOW_SYSTEM
25436
25437 /* EXPORT for RIF:
25438 Fix the display of area AREA of overlapping row ROW in window W
25439 with respect to the overlapping part OVERLAPS. */
25440
25441 void
25442 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25443 enum glyph_row_area area, int overlaps)
25444 {
25445 int i, x;
25446
25447 BLOCK_INPUT;
25448
25449 x = 0;
25450 for (i = 0; i < row->used[area];)
25451 {
25452 if (row->glyphs[area][i].overlaps_vertically_p)
25453 {
25454 int start = i, start_x = x;
25455
25456 do
25457 {
25458 x += row->glyphs[area][i].pixel_width;
25459 ++i;
25460 }
25461 while (i < row->used[area]
25462 && row->glyphs[area][i].overlaps_vertically_p);
25463
25464 draw_glyphs (w, start_x, row, area,
25465 start, i,
25466 DRAW_NORMAL_TEXT, overlaps);
25467 }
25468 else
25469 {
25470 x += row->glyphs[area][i].pixel_width;
25471 ++i;
25472 }
25473 }
25474
25475 UNBLOCK_INPUT;
25476 }
25477
25478
25479 /* EXPORT:
25480 Draw the cursor glyph of window W in glyph row ROW. See the
25481 comment of draw_glyphs for the meaning of HL. */
25482
25483 void
25484 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25485 enum draw_glyphs_face hl)
25486 {
25487 /* If cursor hpos is out of bounds, don't draw garbage. This can
25488 happen in mini-buffer windows when switching between echo area
25489 glyphs and mini-buffer. */
25490 if ((row->reversed_p
25491 ? (w->phys_cursor.hpos >= 0)
25492 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25493 {
25494 int on_p = w->phys_cursor_on_p;
25495 int x1;
25496 int hpos = w->phys_cursor.hpos;
25497
25498 /* When the window is hscrolled, cursor hpos can legitimately be
25499 out of bounds, but we draw the cursor at the corresponding
25500 window margin in that case. */
25501 if (!row->reversed_p && hpos < 0)
25502 hpos = 0;
25503 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25504 hpos = row->used[TEXT_AREA] - 1;
25505
25506 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25507 hl, 0);
25508 w->phys_cursor_on_p = on_p;
25509
25510 if (hl == DRAW_CURSOR)
25511 w->phys_cursor_width = x1 - w->phys_cursor.x;
25512 /* When we erase the cursor, and ROW is overlapped by other
25513 rows, make sure that these overlapping parts of other rows
25514 are redrawn. */
25515 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25516 {
25517 w->phys_cursor_width = x1 - w->phys_cursor.x;
25518
25519 if (row > w->current_matrix->rows
25520 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25521 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25522 OVERLAPS_ERASED_CURSOR);
25523
25524 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25525 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25526 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25527 OVERLAPS_ERASED_CURSOR);
25528 }
25529 }
25530 }
25531
25532
25533 /* EXPORT:
25534 Erase the image of a cursor of window W from the screen. */
25535
25536 void
25537 erase_phys_cursor (struct window *w)
25538 {
25539 struct frame *f = XFRAME (w->frame);
25540 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25541 int hpos = w->phys_cursor.hpos;
25542 int vpos = w->phys_cursor.vpos;
25543 int mouse_face_here_p = 0;
25544 struct glyph_matrix *active_glyphs = w->current_matrix;
25545 struct glyph_row *cursor_row;
25546 struct glyph *cursor_glyph;
25547 enum draw_glyphs_face hl;
25548
25549 /* No cursor displayed or row invalidated => nothing to do on the
25550 screen. */
25551 if (w->phys_cursor_type == NO_CURSOR)
25552 goto mark_cursor_off;
25553
25554 /* VPOS >= active_glyphs->nrows means that window has been resized.
25555 Don't bother to erase the cursor. */
25556 if (vpos >= active_glyphs->nrows)
25557 goto mark_cursor_off;
25558
25559 /* If row containing cursor is marked invalid, there is nothing we
25560 can do. */
25561 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25562 if (!cursor_row->enabled_p)
25563 goto mark_cursor_off;
25564
25565 /* If line spacing is > 0, old cursor may only be partially visible in
25566 window after split-window. So adjust visible height. */
25567 cursor_row->visible_height = min (cursor_row->visible_height,
25568 window_text_bottom_y (w) - cursor_row->y);
25569
25570 /* If row is completely invisible, don't attempt to delete a cursor which
25571 isn't there. This can happen if cursor is at top of a window, and
25572 we switch to a buffer with a header line in that window. */
25573 if (cursor_row->visible_height <= 0)
25574 goto mark_cursor_off;
25575
25576 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25577 if (cursor_row->cursor_in_fringe_p)
25578 {
25579 cursor_row->cursor_in_fringe_p = 0;
25580 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25581 goto mark_cursor_off;
25582 }
25583
25584 /* This can happen when the new row is shorter than the old one.
25585 In this case, either draw_glyphs or clear_end_of_line
25586 should have cleared the cursor. Note that we wouldn't be
25587 able to erase the cursor in this case because we don't have a
25588 cursor glyph at hand. */
25589 if ((cursor_row->reversed_p
25590 ? (w->phys_cursor.hpos < 0)
25591 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25592 goto mark_cursor_off;
25593
25594 /* When the window is hscrolled, cursor hpos can legitimately be out
25595 of bounds, but we draw the cursor at the corresponding window
25596 margin in that case. */
25597 if (!cursor_row->reversed_p && hpos < 0)
25598 hpos = 0;
25599 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25600 hpos = cursor_row->used[TEXT_AREA] - 1;
25601
25602 /* If the cursor is in the mouse face area, redisplay that when
25603 we clear the cursor. */
25604 if (! NILP (hlinfo->mouse_face_window)
25605 && coords_in_mouse_face_p (w, hpos, vpos)
25606 /* Don't redraw the cursor's spot in mouse face if it is at the
25607 end of a line (on a newline). The cursor appears there, but
25608 mouse highlighting does not. */
25609 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25610 mouse_face_here_p = 1;
25611
25612 /* Maybe clear the display under the cursor. */
25613 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25614 {
25615 int x, y, left_x;
25616 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25617 int width;
25618
25619 cursor_glyph = get_phys_cursor_glyph (w);
25620 if (cursor_glyph == NULL)
25621 goto mark_cursor_off;
25622
25623 width = cursor_glyph->pixel_width;
25624 left_x = window_box_left_offset (w, TEXT_AREA);
25625 x = w->phys_cursor.x;
25626 if (x < left_x)
25627 width -= left_x - x;
25628 width = min (width, window_box_width (w, TEXT_AREA) - x);
25629 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25630 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25631
25632 if (width > 0)
25633 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25634 }
25635
25636 /* Erase the cursor by redrawing the character underneath it. */
25637 if (mouse_face_here_p)
25638 hl = DRAW_MOUSE_FACE;
25639 else
25640 hl = DRAW_NORMAL_TEXT;
25641 draw_phys_cursor_glyph (w, cursor_row, hl);
25642
25643 mark_cursor_off:
25644 w->phys_cursor_on_p = 0;
25645 w->phys_cursor_type = NO_CURSOR;
25646 }
25647
25648
25649 /* EXPORT:
25650 Display or clear cursor of window W. If ON is zero, clear the
25651 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25652 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25653
25654 void
25655 display_and_set_cursor (struct window *w, int on,
25656 int hpos, int vpos, int x, int y)
25657 {
25658 struct frame *f = XFRAME (w->frame);
25659 int new_cursor_type;
25660 int new_cursor_width;
25661 int active_cursor;
25662 struct glyph_row *glyph_row;
25663 struct glyph *glyph;
25664
25665 /* This is pointless on invisible frames, and dangerous on garbaged
25666 windows and frames; in the latter case, the frame or window may
25667 be in the midst of changing its size, and x and y may be off the
25668 window. */
25669 if (! FRAME_VISIBLE_P (f)
25670 || FRAME_GARBAGED_P (f)
25671 || vpos >= w->current_matrix->nrows
25672 || hpos >= w->current_matrix->matrix_w)
25673 return;
25674
25675 /* If cursor is off and we want it off, return quickly. */
25676 if (!on && !w->phys_cursor_on_p)
25677 return;
25678
25679 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25680 /* If cursor row is not enabled, we don't really know where to
25681 display the cursor. */
25682 if (!glyph_row->enabled_p)
25683 {
25684 w->phys_cursor_on_p = 0;
25685 return;
25686 }
25687
25688 glyph = NULL;
25689 if (!glyph_row->exact_window_width_line_p
25690 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25691 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25692
25693 xassert (interrupt_input_blocked);
25694
25695 /* Set new_cursor_type to the cursor we want to be displayed. */
25696 new_cursor_type = get_window_cursor_type (w, glyph,
25697 &new_cursor_width, &active_cursor);
25698
25699 /* If cursor is currently being shown and we don't want it to be or
25700 it is in the wrong place, or the cursor type is not what we want,
25701 erase it. */
25702 if (w->phys_cursor_on_p
25703 && (!on
25704 || w->phys_cursor.x != x
25705 || w->phys_cursor.y != y
25706 || new_cursor_type != w->phys_cursor_type
25707 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25708 && new_cursor_width != w->phys_cursor_width)))
25709 erase_phys_cursor (w);
25710
25711 /* Don't check phys_cursor_on_p here because that flag is only set
25712 to zero in some cases where we know that the cursor has been
25713 completely erased, to avoid the extra work of erasing the cursor
25714 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25715 still not be visible, or it has only been partly erased. */
25716 if (on)
25717 {
25718 w->phys_cursor_ascent = glyph_row->ascent;
25719 w->phys_cursor_height = glyph_row->height;
25720
25721 /* Set phys_cursor_.* before x_draw_.* is called because some
25722 of them may need the information. */
25723 w->phys_cursor.x = x;
25724 w->phys_cursor.y = glyph_row->y;
25725 w->phys_cursor.hpos = hpos;
25726 w->phys_cursor.vpos = vpos;
25727 }
25728
25729 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25730 new_cursor_type, new_cursor_width,
25731 on, active_cursor);
25732 }
25733
25734
25735 /* Switch the display of W's cursor on or off, according to the value
25736 of ON. */
25737
25738 static void
25739 update_window_cursor (struct window *w, int on)
25740 {
25741 /* Don't update cursor in windows whose frame is in the process
25742 of being deleted. */
25743 if (w->current_matrix)
25744 {
25745 int hpos = w->phys_cursor.hpos;
25746 int vpos = w->phys_cursor.vpos;
25747 struct glyph_row *row;
25748
25749 if (vpos >= w->current_matrix->nrows
25750 || hpos >= w->current_matrix->matrix_w)
25751 return;
25752
25753 row = MATRIX_ROW (w->current_matrix, vpos);
25754
25755 /* When the window is hscrolled, cursor hpos can legitimately be
25756 out of bounds, but we draw the cursor at the corresponding
25757 window margin in that case. */
25758 if (!row->reversed_p && hpos < 0)
25759 hpos = 0;
25760 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25761 hpos = row->used[TEXT_AREA] - 1;
25762
25763 BLOCK_INPUT;
25764 display_and_set_cursor (w, on, hpos, vpos,
25765 w->phys_cursor.x, w->phys_cursor.y);
25766 UNBLOCK_INPUT;
25767 }
25768 }
25769
25770
25771 /* Call update_window_cursor with parameter ON_P on all leaf windows
25772 in the window tree rooted at W. */
25773
25774 static void
25775 update_cursor_in_window_tree (struct window *w, int on_p)
25776 {
25777 while (w)
25778 {
25779 if (!NILP (w->hchild))
25780 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25781 else if (!NILP (w->vchild))
25782 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25783 else
25784 update_window_cursor (w, on_p);
25785
25786 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25787 }
25788 }
25789
25790
25791 /* EXPORT:
25792 Display the cursor on window W, or clear it, according to ON_P.
25793 Don't change the cursor's position. */
25794
25795 void
25796 x_update_cursor (struct frame *f, int on_p)
25797 {
25798 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25799 }
25800
25801
25802 /* EXPORT:
25803 Clear the cursor of window W to background color, and mark the
25804 cursor as not shown. This is used when the text where the cursor
25805 is about to be rewritten. */
25806
25807 void
25808 x_clear_cursor (struct window *w)
25809 {
25810 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25811 update_window_cursor (w, 0);
25812 }
25813
25814 #endif /* HAVE_WINDOW_SYSTEM */
25815
25816 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25817 and MSDOS. */
25818 static void
25819 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25820 int start_hpos, int end_hpos,
25821 enum draw_glyphs_face draw)
25822 {
25823 #ifdef HAVE_WINDOW_SYSTEM
25824 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25825 {
25826 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25827 return;
25828 }
25829 #endif
25830 #if defined (HAVE_GPM) || defined (MSDOS)
25831 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25832 #endif
25833 }
25834
25835 /* Display the active region described by mouse_face_* according to DRAW. */
25836
25837 static void
25838 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25839 {
25840 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25841 struct frame *f = XFRAME (WINDOW_FRAME (w));
25842
25843 if (/* If window is in the process of being destroyed, don't bother
25844 to do anything. */
25845 w->current_matrix != NULL
25846 /* Don't update mouse highlight if hidden */
25847 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25848 /* Recognize when we are called to operate on rows that don't exist
25849 anymore. This can happen when a window is split. */
25850 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25851 {
25852 int phys_cursor_on_p = w->phys_cursor_on_p;
25853 struct glyph_row *row, *first, *last;
25854
25855 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25856 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25857
25858 for (row = first; row <= last && row->enabled_p; ++row)
25859 {
25860 int start_hpos, end_hpos, start_x;
25861
25862 /* For all but the first row, the highlight starts at column 0. */
25863 if (row == first)
25864 {
25865 /* R2L rows have BEG and END in reversed order, but the
25866 screen drawing geometry is always left to right. So
25867 we need to mirror the beginning and end of the
25868 highlighted area in R2L rows. */
25869 if (!row->reversed_p)
25870 {
25871 start_hpos = hlinfo->mouse_face_beg_col;
25872 start_x = hlinfo->mouse_face_beg_x;
25873 }
25874 else if (row == last)
25875 {
25876 start_hpos = hlinfo->mouse_face_end_col;
25877 start_x = hlinfo->mouse_face_end_x;
25878 }
25879 else
25880 {
25881 start_hpos = 0;
25882 start_x = 0;
25883 }
25884 }
25885 else if (row->reversed_p && row == last)
25886 {
25887 start_hpos = hlinfo->mouse_face_end_col;
25888 start_x = hlinfo->mouse_face_end_x;
25889 }
25890 else
25891 {
25892 start_hpos = 0;
25893 start_x = 0;
25894 }
25895
25896 if (row == last)
25897 {
25898 if (!row->reversed_p)
25899 end_hpos = hlinfo->mouse_face_end_col;
25900 else if (row == first)
25901 end_hpos = hlinfo->mouse_face_beg_col;
25902 else
25903 {
25904 end_hpos = row->used[TEXT_AREA];
25905 if (draw == DRAW_NORMAL_TEXT)
25906 row->fill_line_p = 1; /* Clear to end of line */
25907 }
25908 }
25909 else if (row->reversed_p && row == first)
25910 end_hpos = hlinfo->mouse_face_beg_col;
25911 else
25912 {
25913 end_hpos = row->used[TEXT_AREA];
25914 if (draw == DRAW_NORMAL_TEXT)
25915 row->fill_line_p = 1; /* Clear to end of line */
25916 }
25917
25918 if (end_hpos > start_hpos)
25919 {
25920 draw_row_with_mouse_face (w, start_x, row,
25921 start_hpos, end_hpos, draw);
25922
25923 row->mouse_face_p
25924 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25925 }
25926 }
25927
25928 #ifdef HAVE_WINDOW_SYSTEM
25929 /* When we've written over the cursor, arrange for it to
25930 be displayed again. */
25931 if (FRAME_WINDOW_P (f)
25932 && phys_cursor_on_p && !w->phys_cursor_on_p)
25933 {
25934 int hpos = w->phys_cursor.hpos;
25935
25936 /* When the window is hscrolled, cursor hpos can legitimately be
25937 out of bounds, but we draw the cursor at the corresponding
25938 window margin in that case. */
25939 if (!row->reversed_p && hpos < 0)
25940 hpos = 0;
25941 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25942 hpos = row->used[TEXT_AREA] - 1;
25943
25944 BLOCK_INPUT;
25945 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25946 w->phys_cursor.x, w->phys_cursor.y);
25947 UNBLOCK_INPUT;
25948 }
25949 #endif /* HAVE_WINDOW_SYSTEM */
25950 }
25951
25952 #ifdef HAVE_WINDOW_SYSTEM
25953 /* Change the mouse cursor. */
25954 if (FRAME_WINDOW_P (f))
25955 {
25956 if (draw == DRAW_NORMAL_TEXT
25957 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25958 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25959 else if (draw == DRAW_MOUSE_FACE)
25960 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25961 else
25962 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25963 }
25964 #endif /* HAVE_WINDOW_SYSTEM */
25965 }
25966
25967 /* EXPORT:
25968 Clear out the mouse-highlighted active region.
25969 Redraw it un-highlighted first. Value is non-zero if mouse
25970 face was actually drawn unhighlighted. */
25971
25972 int
25973 clear_mouse_face (Mouse_HLInfo *hlinfo)
25974 {
25975 int cleared = 0;
25976
25977 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25978 {
25979 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25980 cleared = 1;
25981 }
25982
25983 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25984 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25985 hlinfo->mouse_face_window = Qnil;
25986 hlinfo->mouse_face_overlay = Qnil;
25987 return cleared;
25988 }
25989
25990 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25991 within the mouse face on that window. */
25992 static int
25993 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25994 {
25995 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25996
25997 /* Quickly resolve the easy cases. */
25998 if (!(WINDOWP (hlinfo->mouse_face_window)
25999 && XWINDOW (hlinfo->mouse_face_window) == w))
26000 return 0;
26001 if (vpos < hlinfo->mouse_face_beg_row
26002 || vpos > hlinfo->mouse_face_end_row)
26003 return 0;
26004 if (vpos > hlinfo->mouse_face_beg_row
26005 && vpos < hlinfo->mouse_face_end_row)
26006 return 1;
26007
26008 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26009 {
26010 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26011 {
26012 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26013 return 1;
26014 }
26015 else if ((vpos == hlinfo->mouse_face_beg_row
26016 && hpos >= hlinfo->mouse_face_beg_col)
26017 || (vpos == hlinfo->mouse_face_end_row
26018 && hpos < hlinfo->mouse_face_end_col))
26019 return 1;
26020 }
26021 else
26022 {
26023 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26024 {
26025 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26026 return 1;
26027 }
26028 else if ((vpos == hlinfo->mouse_face_beg_row
26029 && hpos <= hlinfo->mouse_face_beg_col)
26030 || (vpos == hlinfo->mouse_face_end_row
26031 && hpos > hlinfo->mouse_face_end_col))
26032 return 1;
26033 }
26034 return 0;
26035 }
26036
26037
26038 /* EXPORT:
26039 Non-zero if physical cursor of window W is within mouse face. */
26040
26041 int
26042 cursor_in_mouse_face_p (struct window *w)
26043 {
26044 int hpos = w->phys_cursor.hpos;
26045 int vpos = w->phys_cursor.vpos;
26046 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26047
26048 /* When the window is hscrolled, cursor hpos can legitimately be out
26049 of bounds, but we draw the cursor at the corresponding window
26050 margin in that case. */
26051 if (!row->reversed_p && hpos < 0)
26052 hpos = 0;
26053 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26054 hpos = row->used[TEXT_AREA] - 1;
26055
26056 return coords_in_mouse_face_p (w, hpos, vpos);
26057 }
26058
26059
26060 \f
26061 /* Find the glyph rows START_ROW and END_ROW of window W that display
26062 characters between buffer positions START_CHARPOS and END_CHARPOS
26063 (excluding END_CHARPOS). DISP_STRING is a display string that
26064 covers these buffer positions. This is similar to
26065 row_containing_pos, but is more accurate when bidi reordering makes
26066 buffer positions change non-linearly with glyph rows. */
26067 static void
26068 rows_from_pos_range (struct window *w,
26069 EMACS_INT start_charpos, EMACS_INT end_charpos,
26070 Lisp_Object disp_string,
26071 struct glyph_row **start, struct glyph_row **end)
26072 {
26073 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26074 int last_y = window_text_bottom_y (w);
26075 struct glyph_row *row;
26076
26077 *start = NULL;
26078 *end = NULL;
26079
26080 while (!first->enabled_p
26081 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26082 first++;
26083
26084 /* Find the START row. */
26085 for (row = first;
26086 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26087 row++)
26088 {
26089 /* A row can potentially be the START row if the range of the
26090 characters it displays intersects the range
26091 [START_CHARPOS..END_CHARPOS). */
26092 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26093 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26094 /* See the commentary in row_containing_pos, for the
26095 explanation of the complicated way to check whether
26096 some position is beyond the end of the characters
26097 displayed by a row. */
26098 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26099 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26100 && !row->ends_at_zv_p
26101 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26102 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26103 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26104 && !row->ends_at_zv_p
26105 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26106 {
26107 /* Found a candidate row. Now make sure at least one of the
26108 glyphs it displays has a charpos from the range
26109 [START_CHARPOS..END_CHARPOS).
26110
26111 This is not obvious because bidi reordering could make
26112 buffer positions of a row be 1,2,3,102,101,100, and if we
26113 want to highlight characters in [50..60), we don't want
26114 this row, even though [50..60) does intersect [1..103),
26115 the range of character positions given by the row's start
26116 and end positions. */
26117 struct glyph *g = row->glyphs[TEXT_AREA];
26118 struct glyph *e = g + row->used[TEXT_AREA];
26119
26120 while (g < e)
26121 {
26122 if (((BUFFERP (g->object) || INTEGERP (g->object))
26123 && start_charpos <= g->charpos && g->charpos < end_charpos)
26124 /* A glyph that comes from DISP_STRING is by
26125 definition to be highlighted. */
26126 || EQ (g->object, disp_string))
26127 *start = row;
26128 g++;
26129 }
26130 if (*start)
26131 break;
26132 }
26133 }
26134
26135 /* Find the END row. */
26136 if (!*start
26137 /* If the last row is partially visible, start looking for END
26138 from that row, instead of starting from FIRST. */
26139 && !(row->enabled_p
26140 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26141 row = first;
26142 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26143 {
26144 struct glyph_row *next = row + 1;
26145 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26146
26147 if (!next->enabled_p
26148 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26149 /* The first row >= START whose range of displayed characters
26150 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26151 is the row END + 1. */
26152 || (start_charpos < next_start
26153 && end_charpos < next_start)
26154 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26155 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26156 && !next->ends_at_zv_p
26157 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26158 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26159 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26160 && !next->ends_at_zv_p
26161 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26162 {
26163 *end = row;
26164 break;
26165 }
26166 else
26167 {
26168 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26169 but none of the characters it displays are in the range, it is
26170 also END + 1. */
26171 struct glyph *g = next->glyphs[TEXT_AREA];
26172 struct glyph *s = g;
26173 struct glyph *e = g + next->used[TEXT_AREA];
26174
26175 while (g < e)
26176 {
26177 if (((BUFFERP (g->object) || INTEGERP (g->object))
26178 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26179 /* If the buffer position of the first glyph in
26180 the row is equal to END_CHARPOS, it means
26181 the last character to be highlighted is the
26182 newline of ROW, and we must consider NEXT as
26183 END, not END+1. */
26184 || (((!next->reversed_p && g == s)
26185 || (next->reversed_p && g == e - 1))
26186 && (g->charpos == end_charpos
26187 /* Special case for when NEXT is an
26188 empty line at ZV. */
26189 || (g->charpos == -1
26190 && !row->ends_at_zv_p
26191 && next_start == end_charpos)))))
26192 /* A glyph that comes from DISP_STRING is by
26193 definition to be highlighted. */
26194 || EQ (g->object, disp_string))
26195 break;
26196 g++;
26197 }
26198 if (g == e)
26199 {
26200 *end = row;
26201 break;
26202 }
26203 /* The first row that ends at ZV must be the last to be
26204 highlighted. */
26205 else if (next->ends_at_zv_p)
26206 {
26207 *end = next;
26208 break;
26209 }
26210 }
26211 }
26212 }
26213
26214 /* This function sets the mouse_face_* elements of HLINFO, assuming
26215 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26216 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26217 for the overlay or run of text properties specifying the mouse
26218 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26219 before-string and after-string that must also be highlighted.
26220 DISP_STRING, if non-nil, is a display string that may cover some
26221 or all of the highlighted text. */
26222
26223 static void
26224 mouse_face_from_buffer_pos (Lisp_Object window,
26225 Mouse_HLInfo *hlinfo,
26226 EMACS_INT mouse_charpos,
26227 EMACS_INT start_charpos,
26228 EMACS_INT end_charpos,
26229 Lisp_Object before_string,
26230 Lisp_Object after_string,
26231 Lisp_Object disp_string)
26232 {
26233 struct window *w = XWINDOW (window);
26234 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26235 struct glyph_row *r1, *r2;
26236 struct glyph *glyph, *end;
26237 EMACS_INT ignore, pos;
26238 int x;
26239
26240 xassert (NILP (disp_string) || STRINGP (disp_string));
26241 xassert (NILP (before_string) || STRINGP (before_string));
26242 xassert (NILP (after_string) || STRINGP (after_string));
26243
26244 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26245 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26246 if (r1 == NULL)
26247 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26248 /* If the before-string or display-string contains newlines,
26249 rows_from_pos_range skips to its last row. Move back. */
26250 if (!NILP (before_string) || !NILP (disp_string))
26251 {
26252 struct glyph_row *prev;
26253 while ((prev = r1 - 1, prev >= first)
26254 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26255 && prev->used[TEXT_AREA] > 0)
26256 {
26257 struct glyph *beg = prev->glyphs[TEXT_AREA];
26258 glyph = beg + prev->used[TEXT_AREA];
26259 while (--glyph >= beg && INTEGERP (glyph->object));
26260 if (glyph < beg
26261 || !(EQ (glyph->object, before_string)
26262 || EQ (glyph->object, disp_string)))
26263 break;
26264 r1 = prev;
26265 }
26266 }
26267 if (r2 == NULL)
26268 {
26269 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26270 hlinfo->mouse_face_past_end = 1;
26271 }
26272 else if (!NILP (after_string))
26273 {
26274 /* If the after-string has newlines, advance to its last row. */
26275 struct glyph_row *next;
26276 struct glyph_row *last
26277 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26278
26279 for (next = r2 + 1;
26280 next <= last
26281 && next->used[TEXT_AREA] > 0
26282 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26283 ++next)
26284 r2 = next;
26285 }
26286 /* The rest of the display engine assumes that mouse_face_beg_row is
26287 either above mouse_face_end_row or identical to it. But with
26288 bidi-reordered continued lines, the row for START_CHARPOS could
26289 be below the row for END_CHARPOS. If so, swap the rows and store
26290 them in correct order. */
26291 if (r1->y > r2->y)
26292 {
26293 struct glyph_row *tem = r2;
26294
26295 r2 = r1;
26296 r1 = tem;
26297 }
26298
26299 hlinfo->mouse_face_beg_y = r1->y;
26300 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26301 hlinfo->mouse_face_end_y = r2->y;
26302 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26303
26304 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26305 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26306 could be anywhere in the row and in any order. The strategy
26307 below is to find the leftmost and the rightmost glyph that
26308 belongs to either of these 3 strings, or whose position is
26309 between START_CHARPOS and END_CHARPOS, and highlight all the
26310 glyphs between those two. This may cover more than just the text
26311 between START_CHARPOS and END_CHARPOS if the range of characters
26312 strides the bidi level boundary, e.g. if the beginning is in R2L
26313 text while the end is in L2R text or vice versa. */
26314 if (!r1->reversed_p)
26315 {
26316 /* This row is in a left to right paragraph. Scan it left to
26317 right. */
26318 glyph = r1->glyphs[TEXT_AREA];
26319 end = glyph + r1->used[TEXT_AREA];
26320 x = r1->x;
26321
26322 /* Skip truncation glyphs at the start of the glyph row. */
26323 if (r1->displays_text_p)
26324 for (; glyph < end
26325 && INTEGERP (glyph->object)
26326 && glyph->charpos < 0;
26327 ++glyph)
26328 x += glyph->pixel_width;
26329
26330 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26331 or DISP_STRING, and the first glyph from buffer whose
26332 position is between START_CHARPOS and END_CHARPOS. */
26333 for (; glyph < end
26334 && !INTEGERP (glyph->object)
26335 && !EQ (glyph->object, disp_string)
26336 && !(BUFFERP (glyph->object)
26337 && (glyph->charpos >= start_charpos
26338 && glyph->charpos < end_charpos));
26339 ++glyph)
26340 {
26341 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26342 are present at buffer positions between START_CHARPOS and
26343 END_CHARPOS, or if they come from an overlay. */
26344 if (EQ (glyph->object, before_string))
26345 {
26346 pos = string_buffer_position (before_string,
26347 start_charpos);
26348 /* If pos == 0, it means before_string came from an
26349 overlay, not from a buffer position. */
26350 if (!pos || (pos >= start_charpos && pos < end_charpos))
26351 break;
26352 }
26353 else if (EQ (glyph->object, after_string))
26354 {
26355 pos = string_buffer_position (after_string, end_charpos);
26356 if (!pos || (pos >= start_charpos && pos < end_charpos))
26357 break;
26358 }
26359 x += glyph->pixel_width;
26360 }
26361 hlinfo->mouse_face_beg_x = x;
26362 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26363 }
26364 else
26365 {
26366 /* This row is in a right to left paragraph. Scan it right to
26367 left. */
26368 struct glyph *g;
26369
26370 end = r1->glyphs[TEXT_AREA] - 1;
26371 glyph = end + r1->used[TEXT_AREA];
26372
26373 /* Skip truncation glyphs at the start of the glyph row. */
26374 if (r1->displays_text_p)
26375 for (; glyph > end
26376 && INTEGERP (glyph->object)
26377 && glyph->charpos < 0;
26378 --glyph)
26379 ;
26380
26381 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26382 or DISP_STRING, and the first glyph from buffer whose
26383 position is between START_CHARPOS and END_CHARPOS. */
26384 for (; glyph > end
26385 && !INTEGERP (glyph->object)
26386 && !EQ (glyph->object, disp_string)
26387 && !(BUFFERP (glyph->object)
26388 && (glyph->charpos >= start_charpos
26389 && glyph->charpos < end_charpos));
26390 --glyph)
26391 {
26392 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26393 are present at buffer positions between START_CHARPOS and
26394 END_CHARPOS, or if they come from an overlay. */
26395 if (EQ (glyph->object, before_string))
26396 {
26397 pos = string_buffer_position (before_string, start_charpos);
26398 /* If pos == 0, it means before_string came from an
26399 overlay, not from a buffer position. */
26400 if (!pos || (pos >= start_charpos && pos < end_charpos))
26401 break;
26402 }
26403 else if (EQ (glyph->object, after_string))
26404 {
26405 pos = string_buffer_position (after_string, end_charpos);
26406 if (!pos || (pos >= start_charpos && pos < end_charpos))
26407 break;
26408 }
26409 }
26410
26411 glyph++; /* first glyph to the right of the highlighted area */
26412 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26413 x += g->pixel_width;
26414 hlinfo->mouse_face_beg_x = x;
26415 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26416 }
26417
26418 /* If the highlight ends in a different row, compute GLYPH and END
26419 for the end row. Otherwise, reuse the values computed above for
26420 the row where the highlight begins. */
26421 if (r2 != r1)
26422 {
26423 if (!r2->reversed_p)
26424 {
26425 glyph = r2->glyphs[TEXT_AREA];
26426 end = glyph + r2->used[TEXT_AREA];
26427 x = r2->x;
26428 }
26429 else
26430 {
26431 end = r2->glyphs[TEXT_AREA] - 1;
26432 glyph = end + r2->used[TEXT_AREA];
26433 }
26434 }
26435
26436 if (!r2->reversed_p)
26437 {
26438 /* Skip truncation and continuation glyphs near the end of the
26439 row, and also blanks and stretch glyphs inserted by
26440 extend_face_to_end_of_line. */
26441 while (end > glyph
26442 && INTEGERP ((end - 1)->object))
26443 --end;
26444 /* Scan the rest of the glyph row from the end, looking for the
26445 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26446 DISP_STRING, or whose position is between START_CHARPOS
26447 and END_CHARPOS */
26448 for (--end;
26449 end > glyph
26450 && !INTEGERP (end->object)
26451 && !EQ (end->object, disp_string)
26452 && !(BUFFERP (end->object)
26453 && (end->charpos >= start_charpos
26454 && end->charpos < end_charpos));
26455 --end)
26456 {
26457 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26458 are present at buffer positions between START_CHARPOS and
26459 END_CHARPOS, or if they come from an overlay. */
26460 if (EQ (end->object, before_string))
26461 {
26462 pos = string_buffer_position (before_string, start_charpos);
26463 if (!pos || (pos >= start_charpos && pos < end_charpos))
26464 break;
26465 }
26466 else if (EQ (end->object, after_string))
26467 {
26468 pos = string_buffer_position (after_string, end_charpos);
26469 if (!pos || (pos >= start_charpos && pos < end_charpos))
26470 break;
26471 }
26472 }
26473 /* Find the X coordinate of the last glyph to be highlighted. */
26474 for (; glyph <= end; ++glyph)
26475 x += glyph->pixel_width;
26476
26477 hlinfo->mouse_face_end_x = x;
26478 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26479 }
26480 else
26481 {
26482 /* Skip truncation and continuation glyphs near the end of the
26483 row, and also blanks and stretch glyphs inserted by
26484 extend_face_to_end_of_line. */
26485 x = r2->x;
26486 end++;
26487 while (end < glyph
26488 && INTEGERP (end->object))
26489 {
26490 x += end->pixel_width;
26491 ++end;
26492 }
26493 /* Scan the rest of the glyph row from the end, looking for the
26494 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26495 DISP_STRING, or whose position is between START_CHARPOS
26496 and END_CHARPOS */
26497 for ( ;
26498 end < glyph
26499 && !INTEGERP (end->object)
26500 && !EQ (end->object, disp_string)
26501 && !(BUFFERP (end->object)
26502 && (end->charpos >= start_charpos
26503 && end->charpos < end_charpos));
26504 ++end)
26505 {
26506 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26507 are present at buffer positions between START_CHARPOS and
26508 END_CHARPOS, or if they come from an overlay. */
26509 if (EQ (end->object, before_string))
26510 {
26511 pos = string_buffer_position (before_string, start_charpos);
26512 if (!pos || (pos >= start_charpos && pos < end_charpos))
26513 break;
26514 }
26515 else if (EQ (end->object, after_string))
26516 {
26517 pos = string_buffer_position (after_string, end_charpos);
26518 if (!pos || (pos >= start_charpos && pos < end_charpos))
26519 break;
26520 }
26521 x += end->pixel_width;
26522 }
26523 /* If we exited the above loop because we arrived at the last
26524 glyph of the row, and its buffer position is still not in
26525 range, it means the last character in range is the preceding
26526 newline. Bump the end column and x values to get past the
26527 last glyph. */
26528 if (end == glyph
26529 && BUFFERP (end->object)
26530 && (end->charpos < start_charpos
26531 || end->charpos >= end_charpos))
26532 {
26533 x += end->pixel_width;
26534 ++end;
26535 }
26536 hlinfo->mouse_face_end_x = x;
26537 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26538 }
26539
26540 hlinfo->mouse_face_window = window;
26541 hlinfo->mouse_face_face_id
26542 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26543 mouse_charpos + 1,
26544 !hlinfo->mouse_face_hidden, -1);
26545 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26546 }
26547
26548 /* The following function is not used anymore (replaced with
26549 mouse_face_from_string_pos), but I leave it here for the time
26550 being, in case someone would. */
26551
26552 #if 0 /* not used */
26553
26554 /* Find the position of the glyph for position POS in OBJECT in
26555 window W's current matrix, and return in *X, *Y the pixel
26556 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26557
26558 RIGHT_P non-zero means return the position of the right edge of the
26559 glyph, RIGHT_P zero means return the left edge position.
26560
26561 If no glyph for POS exists in the matrix, return the position of
26562 the glyph with the next smaller position that is in the matrix, if
26563 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26564 exists in the matrix, return the position of the glyph with the
26565 next larger position in OBJECT.
26566
26567 Value is non-zero if a glyph was found. */
26568
26569 static int
26570 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26571 int *hpos, int *vpos, int *x, int *y, int right_p)
26572 {
26573 int yb = window_text_bottom_y (w);
26574 struct glyph_row *r;
26575 struct glyph *best_glyph = NULL;
26576 struct glyph_row *best_row = NULL;
26577 int best_x = 0;
26578
26579 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26580 r->enabled_p && r->y < yb;
26581 ++r)
26582 {
26583 struct glyph *g = r->glyphs[TEXT_AREA];
26584 struct glyph *e = g + r->used[TEXT_AREA];
26585 int gx;
26586
26587 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26588 if (EQ (g->object, object))
26589 {
26590 if (g->charpos == pos)
26591 {
26592 best_glyph = g;
26593 best_x = gx;
26594 best_row = r;
26595 goto found;
26596 }
26597 else if (best_glyph == NULL
26598 || ((eabs (g->charpos - pos)
26599 < eabs (best_glyph->charpos - pos))
26600 && (right_p
26601 ? g->charpos < pos
26602 : g->charpos > pos)))
26603 {
26604 best_glyph = g;
26605 best_x = gx;
26606 best_row = r;
26607 }
26608 }
26609 }
26610
26611 found:
26612
26613 if (best_glyph)
26614 {
26615 *x = best_x;
26616 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26617
26618 if (right_p)
26619 {
26620 *x += best_glyph->pixel_width;
26621 ++*hpos;
26622 }
26623
26624 *y = best_row->y;
26625 *vpos = best_row - w->current_matrix->rows;
26626 }
26627
26628 return best_glyph != NULL;
26629 }
26630 #endif /* not used */
26631
26632 /* Find the positions of the first and the last glyphs in window W's
26633 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26634 (assumed to be a string), and return in HLINFO's mouse_face_*
26635 members the pixel and column/row coordinates of those glyphs. */
26636
26637 static void
26638 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26639 Lisp_Object object,
26640 EMACS_INT startpos, EMACS_INT endpos)
26641 {
26642 int yb = window_text_bottom_y (w);
26643 struct glyph_row *r;
26644 struct glyph *g, *e;
26645 int gx;
26646 int found = 0;
26647
26648 /* Find the glyph row with at least one position in the range
26649 [STARTPOS..ENDPOS], and the first glyph in that row whose
26650 position belongs to that range. */
26651 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26652 r->enabled_p && r->y < yb;
26653 ++r)
26654 {
26655 if (!r->reversed_p)
26656 {
26657 g = r->glyphs[TEXT_AREA];
26658 e = g + r->used[TEXT_AREA];
26659 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26660 if (EQ (g->object, object)
26661 && startpos <= g->charpos && g->charpos <= endpos)
26662 {
26663 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26664 hlinfo->mouse_face_beg_y = r->y;
26665 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26666 hlinfo->mouse_face_beg_x = gx;
26667 found = 1;
26668 break;
26669 }
26670 }
26671 else
26672 {
26673 struct glyph *g1;
26674
26675 e = r->glyphs[TEXT_AREA];
26676 g = e + r->used[TEXT_AREA];
26677 for ( ; g > e; --g)
26678 if (EQ ((g-1)->object, object)
26679 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26680 {
26681 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26682 hlinfo->mouse_face_beg_y = r->y;
26683 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26684 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26685 gx += g1->pixel_width;
26686 hlinfo->mouse_face_beg_x = gx;
26687 found = 1;
26688 break;
26689 }
26690 }
26691 if (found)
26692 break;
26693 }
26694
26695 if (!found)
26696 return;
26697
26698 /* Starting with the next row, look for the first row which does NOT
26699 include any glyphs whose positions are in the range. */
26700 for (++r; r->enabled_p && r->y < yb; ++r)
26701 {
26702 g = r->glyphs[TEXT_AREA];
26703 e = g + r->used[TEXT_AREA];
26704 found = 0;
26705 for ( ; g < e; ++g)
26706 if (EQ (g->object, object)
26707 && startpos <= g->charpos && g->charpos <= endpos)
26708 {
26709 found = 1;
26710 break;
26711 }
26712 if (!found)
26713 break;
26714 }
26715
26716 /* The highlighted region ends on the previous row. */
26717 r--;
26718
26719 /* Set the end row and its vertical pixel coordinate. */
26720 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26721 hlinfo->mouse_face_end_y = r->y;
26722
26723 /* Compute and set the end column and the end column's horizontal
26724 pixel coordinate. */
26725 if (!r->reversed_p)
26726 {
26727 g = r->glyphs[TEXT_AREA];
26728 e = g + r->used[TEXT_AREA];
26729 for ( ; e > g; --e)
26730 if (EQ ((e-1)->object, object)
26731 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26732 break;
26733 hlinfo->mouse_face_end_col = e - g;
26734
26735 for (gx = r->x; g < e; ++g)
26736 gx += g->pixel_width;
26737 hlinfo->mouse_face_end_x = gx;
26738 }
26739 else
26740 {
26741 e = r->glyphs[TEXT_AREA];
26742 g = e + r->used[TEXT_AREA];
26743 for (gx = r->x ; e < g; ++e)
26744 {
26745 if (EQ (e->object, object)
26746 && startpos <= e->charpos && e->charpos <= endpos)
26747 break;
26748 gx += e->pixel_width;
26749 }
26750 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26751 hlinfo->mouse_face_end_x = gx;
26752 }
26753 }
26754
26755 #ifdef HAVE_WINDOW_SYSTEM
26756
26757 /* See if position X, Y is within a hot-spot of an image. */
26758
26759 static int
26760 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26761 {
26762 if (!CONSP (hot_spot))
26763 return 0;
26764
26765 if (EQ (XCAR (hot_spot), Qrect))
26766 {
26767 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26768 Lisp_Object rect = XCDR (hot_spot);
26769 Lisp_Object tem;
26770 if (!CONSP (rect))
26771 return 0;
26772 if (!CONSP (XCAR (rect)))
26773 return 0;
26774 if (!CONSP (XCDR (rect)))
26775 return 0;
26776 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26777 return 0;
26778 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26779 return 0;
26780 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26781 return 0;
26782 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26783 return 0;
26784 return 1;
26785 }
26786 else if (EQ (XCAR (hot_spot), Qcircle))
26787 {
26788 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26789 Lisp_Object circ = XCDR (hot_spot);
26790 Lisp_Object lr, lx0, ly0;
26791 if (CONSP (circ)
26792 && CONSP (XCAR (circ))
26793 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26794 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26795 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26796 {
26797 double r = XFLOATINT (lr);
26798 double dx = XINT (lx0) - x;
26799 double dy = XINT (ly0) - y;
26800 return (dx * dx + dy * dy <= r * r);
26801 }
26802 }
26803 else if (EQ (XCAR (hot_spot), Qpoly))
26804 {
26805 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26806 if (VECTORP (XCDR (hot_spot)))
26807 {
26808 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26809 Lisp_Object *poly = v->contents;
26810 int n = v->header.size;
26811 int i;
26812 int inside = 0;
26813 Lisp_Object lx, ly;
26814 int x0, y0;
26815
26816 /* Need an even number of coordinates, and at least 3 edges. */
26817 if (n < 6 || n & 1)
26818 return 0;
26819
26820 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26821 If count is odd, we are inside polygon. Pixels on edges
26822 may or may not be included depending on actual geometry of the
26823 polygon. */
26824 if ((lx = poly[n-2], !INTEGERP (lx))
26825 || (ly = poly[n-1], !INTEGERP (lx)))
26826 return 0;
26827 x0 = XINT (lx), y0 = XINT (ly);
26828 for (i = 0; i < n; i += 2)
26829 {
26830 int x1 = x0, y1 = y0;
26831 if ((lx = poly[i], !INTEGERP (lx))
26832 || (ly = poly[i+1], !INTEGERP (ly)))
26833 return 0;
26834 x0 = XINT (lx), y0 = XINT (ly);
26835
26836 /* Does this segment cross the X line? */
26837 if (x0 >= x)
26838 {
26839 if (x1 >= x)
26840 continue;
26841 }
26842 else if (x1 < x)
26843 continue;
26844 if (y > y0 && y > y1)
26845 continue;
26846 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26847 inside = !inside;
26848 }
26849 return inside;
26850 }
26851 }
26852 return 0;
26853 }
26854
26855 Lisp_Object
26856 find_hot_spot (Lisp_Object map, int x, int y)
26857 {
26858 while (CONSP (map))
26859 {
26860 if (CONSP (XCAR (map))
26861 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26862 return XCAR (map);
26863 map = XCDR (map);
26864 }
26865
26866 return Qnil;
26867 }
26868
26869 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26870 3, 3, 0,
26871 doc: /* Lookup in image map MAP coordinates X and Y.
26872 An image map is an alist where each element has the format (AREA ID PLIST).
26873 An AREA is specified as either a rectangle, a circle, or a polygon:
26874 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26875 pixel coordinates of the upper left and bottom right corners.
26876 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26877 and the radius of the circle; r may be a float or integer.
26878 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26879 vector describes one corner in the polygon.
26880 Returns the alist element for the first matching AREA in MAP. */)
26881 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26882 {
26883 if (NILP (map))
26884 return Qnil;
26885
26886 CHECK_NUMBER (x);
26887 CHECK_NUMBER (y);
26888
26889 return find_hot_spot (map, XINT (x), XINT (y));
26890 }
26891
26892
26893 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26894 static void
26895 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26896 {
26897 /* Do not change cursor shape while dragging mouse. */
26898 if (!NILP (do_mouse_tracking))
26899 return;
26900
26901 if (!NILP (pointer))
26902 {
26903 if (EQ (pointer, Qarrow))
26904 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26905 else if (EQ (pointer, Qhand))
26906 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26907 else if (EQ (pointer, Qtext))
26908 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26909 else if (EQ (pointer, intern ("hdrag")))
26910 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26911 #ifdef HAVE_X_WINDOWS
26912 else if (EQ (pointer, intern ("vdrag")))
26913 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26914 #endif
26915 else if (EQ (pointer, intern ("hourglass")))
26916 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26917 else if (EQ (pointer, Qmodeline))
26918 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26919 else
26920 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26921 }
26922
26923 if (cursor != No_Cursor)
26924 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26925 }
26926
26927 #endif /* HAVE_WINDOW_SYSTEM */
26928
26929 /* Take proper action when mouse has moved to the mode or header line
26930 or marginal area AREA of window W, x-position X and y-position Y.
26931 X is relative to the start of the text display area of W, so the
26932 width of bitmap areas and scroll bars must be subtracted to get a
26933 position relative to the start of the mode line. */
26934
26935 static void
26936 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26937 enum window_part area)
26938 {
26939 struct window *w = XWINDOW (window);
26940 struct frame *f = XFRAME (w->frame);
26941 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26942 #ifdef HAVE_WINDOW_SYSTEM
26943 Display_Info *dpyinfo;
26944 #endif
26945 Cursor cursor = No_Cursor;
26946 Lisp_Object pointer = Qnil;
26947 int dx, dy, width, height;
26948 EMACS_INT charpos;
26949 Lisp_Object string, object = Qnil;
26950 Lisp_Object pos, help;
26951
26952 Lisp_Object mouse_face;
26953 int original_x_pixel = x;
26954 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26955 struct glyph_row *row;
26956
26957 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26958 {
26959 int x0;
26960 struct glyph *end;
26961
26962 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26963 returns them in row/column units! */
26964 string = mode_line_string (w, area, &x, &y, &charpos,
26965 &object, &dx, &dy, &width, &height);
26966
26967 row = (area == ON_MODE_LINE
26968 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26969 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26970
26971 /* Find the glyph under the mouse pointer. */
26972 if (row->mode_line_p && row->enabled_p)
26973 {
26974 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26975 end = glyph + row->used[TEXT_AREA];
26976
26977 for (x0 = original_x_pixel;
26978 glyph < end && x0 >= glyph->pixel_width;
26979 ++glyph)
26980 x0 -= glyph->pixel_width;
26981
26982 if (glyph >= end)
26983 glyph = NULL;
26984 }
26985 }
26986 else
26987 {
26988 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26989 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26990 returns them in row/column units! */
26991 string = marginal_area_string (w, area, &x, &y, &charpos,
26992 &object, &dx, &dy, &width, &height);
26993 }
26994
26995 help = Qnil;
26996
26997 #ifdef HAVE_WINDOW_SYSTEM
26998 if (IMAGEP (object))
26999 {
27000 Lisp_Object image_map, hotspot;
27001 if ((image_map = Fplist_get (XCDR (object), QCmap),
27002 !NILP (image_map))
27003 && (hotspot = find_hot_spot (image_map, dx, dy),
27004 CONSP (hotspot))
27005 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27006 {
27007 Lisp_Object plist;
27008
27009 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27010 If so, we could look for mouse-enter, mouse-leave
27011 properties in PLIST (and do something...). */
27012 hotspot = XCDR (hotspot);
27013 if (CONSP (hotspot)
27014 && (plist = XCAR (hotspot), CONSP (plist)))
27015 {
27016 pointer = Fplist_get (plist, Qpointer);
27017 if (NILP (pointer))
27018 pointer = Qhand;
27019 help = Fplist_get (plist, Qhelp_echo);
27020 if (!NILP (help))
27021 {
27022 help_echo_string = help;
27023 /* Is this correct? ++kfs */
27024 XSETWINDOW (help_echo_window, w);
27025 help_echo_object = w->buffer;
27026 help_echo_pos = charpos;
27027 }
27028 }
27029 }
27030 if (NILP (pointer))
27031 pointer = Fplist_get (XCDR (object), QCpointer);
27032 }
27033 #endif /* HAVE_WINDOW_SYSTEM */
27034
27035 if (STRINGP (string))
27036 {
27037 pos = make_number (charpos);
27038 /* If we're on a string with `help-echo' text property, arrange
27039 for the help to be displayed. This is done by setting the
27040 global variable help_echo_string to the help string. */
27041 if (NILP (help))
27042 {
27043 help = Fget_text_property (pos, Qhelp_echo, string);
27044 if (!NILP (help))
27045 {
27046 help_echo_string = help;
27047 XSETWINDOW (help_echo_window, w);
27048 help_echo_object = string;
27049 help_echo_pos = charpos;
27050 }
27051 }
27052
27053 #ifdef HAVE_WINDOW_SYSTEM
27054 if (FRAME_WINDOW_P (f))
27055 {
27056 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27057 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27058 if (NILP (pointer))
27059 pointer = Fget_text_property (pos, Qpointer, string);
27060
27061 /* Change the mouse pointer according to what is under X/Y. */
27062 if (NILP (pointer)
27063 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27064 {
27065 Lisp_Object map;
27066 map = Fget_text_property (pos, Qlocal_map, string);
27067 if (!KEYMAPP (map))
27068 map = Fget_text_property (pos, Qkeymap, string);
27069 if (!KEYMAPP (map))
27070 cursor = dpyinfo->vertical_scroll_bar_cursor;
27071 }
27072 }
27073 #endif
27074
27075 /* Change the mouse face according to what is under X/Y. */
27076 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27077 if (!NILP (mouse_face)
27078 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27079 && glyph)
27080 {
27081 Lisp_Object b, e;
27082
27083 struct glyph * tmp_glyph;
27084
27085 int gpos;
27086 int gseq_length;
27087 int total_pixel_width;
27088 EMACS_INT begpos, endpos, ignore;
27089
27090 int vpos, hpos;
27091
27092 b = Fprevious_single_property_change (make_number (charpos + 1),
27093 Qmouse_face, string, Qnil);
27094 if (NILP (b))
27095 begpos = 0;
27096 else
27097 begpos = XINT (b);
27098
27099 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27100 if (NILP (e))
27101 endpos = SCHARS (string);
27102 else
27103 endpos = XINT (e);
27104
27105 /* Calculate the glyph position GPOS of GLYPH in the
27106 displayed string, relative to the beginning of the
27107 highlighted part of the string.
27108
27109 Note: GPOS is different from CHARPOS. CHARPOS is the
27110 position of GLYPH in the internal string object. A mode
27111 line string format has structures which are converted to
27112 a flattened string by the Emacs Lisp interpreter. The
27113 internal string is an element of those structures. The
27114 displayed string is the flattened string. */
27115 tmp_glyph = row_start_glyph;
27116 while (tmp_glyph < glyph
27117 && (!(EQ (tmp_glyph->object, glyph->object)
27118 && begpos <= tmp_glyph->charpos
27119 && tmp_glyph->charpos < endpos)))
27120 tmp_glyph++;
27121 gpos = glyph - tmp_glyph;
27122
27123 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27124 the highlighted part of the displayed string to which
27125 GLYPH belongs. Note: GSEQ_LENGTH is different from
27126 SCHARS (STRING), because the latter returns the length of
27127 the internal string. */
27128 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27129 tmp_glyph > glyph
27130 && (!(EQ (tmp_glyph->object, glyph->object)
27131 && begpos <= tmp_glyph->charpos
27132 && tmp_glyph->charpos < endpos));
27133 tmp_glyph--)
27134 ;
27135 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27136
27137 /* Calculate the total pixel width of all the glyphs between
27138 the beginning of the highlighted area and GLYPH. */
27139 total_pixel_width = 0;
27140 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27141 total_pixel_width += tmp_glyph->pixel_width;
27142
27143 /* Pre calculation of re-rendering position. Note: X is in
27144 column units here, after the call to mode_line_string or
27145 marginal_area_string. */
27146 hpos = x - gpos;
27147 vpos = (area == ON_MODE_LINE
27148 ? (w->current_matrix)->nrows - 1
27149 : 0);
27150
27151 /* If GLYPH's position is included in the region that is
27152 already drawn in mouse face, we have nothing to do. */
27153 if ( EQ (window, hlinfo->mouse_face_window)
27154 && (!row->reversed_p
27155 ? (hlinfo->mouse_face_beg_col <= hpos
27156 && hpos < hlinfo->mouse_face_end_col)
27157 /* In R2L rows we swap BEG and END, see below. */
27158 : (hlinfo->mouse_face_end_col <= hpos
27159 && hpos < hlinfo->mouse_face_beg_col))
27160 && hlinfo->mouse_face_beg_row == vpos )
27161 return;
27162
27163 if (clear_mouse_face (hlinfo))
27164 cursor = No_Cursor;
27165
27166 if (!row->reversed_p)
27167 {
27168 hlinfo->mouse_face_beg_col = hpos;
27169 hlinfo->mouse_face_beg_x = original_x_pixel
27170 - (total_pixel_width + dx);
27171 hlinfo->mouse_face_end_col = hpos + gseq_length;
27172 hlinfo->mouse_face_end_x = 0;
27173 }
27174 else
27175 {
27176 /* In R2L rows, show_mouse_face expects BEG and END
27177 coordinates to be swapped. */
27178 hlinfo->mouse_face_end_col = hpos;
27179 hlinfo->mouse_face_end_x = original_x_pixel
27180 - (total_pixel_width + dx);
27181 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27182 hlinfo->mouse_face_beg_x = 0;
27183 }
27184
27185 hlinfo->mouse_face_beg_row = vpos;
27186 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27187 hlinfo->mouse_face_beg_y = 0;
27188 hlinfo->mouse_face_end_y = 0;
27189 hlinfo->mouse_face_past_end = 0;
27190 hlinfo->mouse_face_window = window;
27191
27192 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27193 charpos,
27194 0, 0, 0,
27195 &ignore,
27196 glyph->face_id,
27197 1);
27198 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27199
27200 if (NILP (pointer))
27201 pointer = Qhand;
27202 }
27203 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27204 clear_mouse_face (hlinfo);
27205 }
27206 #ifdef HAVE_WINDOW_SYSTEM
27207 if (FRAME_WINDOW_P (f))
27208 define_frame_cursor1 (f, cursor, pointer);
27209 #endif
27210 }
27211
27212
27213 /* EXPORT:
27214 Take proper action when the mouse has moved to position X, Y on
27215 frame F as regards highlighting characters that have mouse-face
27216 properties. Also de-highlighting chars where the mouse was before.
27217 X and Y can be negative or out of range. */
27218
27219 void
27220 note_mouse_highlight (struct frame *f, int x, int y)
27221 {
27222 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27223 enum window_part part = ON_NOTHING;
27224 Lisp_Object window;
27225 struct window *w;
27226 Cursor cursor = No_Cursor;
27227 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27228 struct buffer *b;
27229
27230 /* When a menu is active, don't highlight because this looks odd. */
27231 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27232 if (popup_activated ())
27233 return;
27234 #endif
27235
27236 if (NILP (Vmouse_highlight)
27237 || !f->glyphs_initialized_p
27238 || f->pointer_invisible)
27239 return;
27240
27241 hlinfo->mouse_face_mouse_x = x;
27242 hlinfo->mouse_face_mouse_y = y;
27243 hlinfo->mouse_face_mouse_frame = f;
27244
27245 if (hlinfo->mouse_face_defer)
27246 return;
27247
27248 if (gc_in_progress)
27249 {
27250 hlinfo->mouse_face_deferred_gc = 1;
27251 return;
27252 }
27253
27254 /* Which window is that in? */
27255 window = window_from_coordinates (f, x, y, &part, 1);
27256
27257 /* If displaying active text in another window, clear that. */
27258 if (! EQ (window, hlinfo->mouse_face_window)
27259 /* Also clear if we move out of text area in same window. */
27260 || (!NILP (hlinfo->mouse_face_window)
27261 && !NILP (window)
27262 && part != ON_TEXT
27263 && part != ON_MODE_LINE
27264 && part != ON_HEADER_LINE))
27265 clear_mouse_face (hlinfo);
27266
27267 /* Not on a window -> return. */
27268 if (!WINDOWP (window))
27269 return;
27270
27271 /* Reset help_echo_string. It will get recomputed below. */
27272 help_echo_string = Qnil;
27273
27274 /* Convert to window-relative pixel coordinates. */
27275 w = XWINDOW (window);
27276 frame_to_window_pixel_xy (w, &x, &y);
27277
27278 #ifdef HAVE_WINDOW_SYSTEM
27279 /* Handle tool-bar window differently since it doesn't display a
27280 buffer. */
27281 if (EQ (window, f->tool_bar_window))
27282 {
27283 note_tool_bar_highlight (f, x, y);
27284 return;
27285 }
27286 #endif
27287
27288 /* Mouse is on the mode, header line or margin? */
27289 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27290 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27291 {
27292 note_mode_line_or_margin_highlight (window, x, y, part);
27293 return;
27294 }
27295
27296 #ifdef HAVE_WINDOW_SYSTEM
27297 if (part == ON_VERTICAL_BORDER)
27298 {
27299 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27300 help_echo_string = build_string ("drag-mouse-1: resize");
27301 }
27302 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27303 || part == ON_SCROLL_BAR)
27304 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27305 else
27306 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27307 #endif
27308
27309 /* Are we in a window whose display is up to date?
27310 And verify the buffer's text has not changed. */
27311 b = XBUFFER (w->buffer);
27312 if (part == ON_TEXT
27313 && EQ (w->window_end_valid, w->buffer)
27314 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27315 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27316 {
27317 int hpos, vpos, dx, dy, area = LAST_AREA;
27318 EMACS_INT pos;
27319 struct glyph *glyph;
27320 Lisp_Object object;
27321 Lisp_Object mouse_face = Qnil, position;
27322 Lisp_Object *overlay_vec = NULL;
27323 ptrdiff_t i, noverlays;
27324 struct buffer *obuf;
27325 EMACS_INT obegv, ozv;
27326 int same_region;
27327
27328 /* Find the glyph under X/Y. */
27329 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27330
27331 #ifdef HAVE_WINDOW_SYSTEM
27332 /* Look for :pointer property on image. */
27333 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27334 {
27335 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27336 if (img != NULL && IMAGEP (img->spec))
27337 {
27338 Lisp_Object image_map, hotspot;
27339 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27340 !NILP (image_map))
27341 && (hotspot = find_hot_spot (image_map,
27342 glyph->slice.img.x + dx,
27343 glyph->slice.img.y + dy),
27344 CONSP (hotspot))
27345 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27346 {
27347 Lisp_Object plist;
27348
27349 /* Could check XCAR (hotspot) to see if we enter/leave
27350 this hot-spot.
27351 If so, we could look for mouse-enter, mouse-leave
27352 properties in PLIST (and do something...). */
27353 hotspot = XCDR (hotspot);
27354 if (CONSP (hotspot)
27355 && (plist = XCAR (hotspot), CONSP (plist)))
27356 {
27357 pointer = Fplist_get (plist, Qpointer);
27358 if (NILP (pointer))
27359 pointer = Qhand;
27360 help_echo_string = Fplist_get (plist, Qhelp_echo);
27361 if (!NILP (help_echo_string))
27362 {
27363 help_echo_window = window;
27364 help_echo_object = glyph->object;
27365 help_echo_pos = glyph->charpos;
27366 }
27367 }
27368 }
27369 if (NILP (pointer))
27370 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27371 }
27372 }
27373 #endif /* HAVE_WINDOW_SYSTEM */
27374
27375 /* Clear mouse face if X/Y not over text. */
27376 if (glyph == NULL
27377 || area != TEXT_AREA
27378 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27379 /* Glyph's OBJECT is an integer for glyphs inserted by the
27380 display engine for its internal purposes, like truncation
27381 and continuation glyphs and blanks beyond the end of
27382 line's text on text terminals. If we are over such a
27383 glyph, we are not over any text. */
27384 || INTEGERP (glyph->object)
27385 /* R2L rows have a stretch glyph at their front, which
27386 stands for no text, whereas L2R rows have no glyphs at
27387 all beyond the end of text. Treat such stretch glyphs
27388 like we do with NULL glyphs in L2R rows. */
27389 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27390 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27391 && glyph->type == STRETCH_GLYPH
27392 && glyph->avoid_cursor_p))
27393 {
27394 if (clear_mouse_face (hlinfo))
27395 cursor = No_Cursor;
27396 #ifdef HAVE_WINDOW_SYSTEM
27397 if (FRAME_WINDOW_P (f) && NILP (pointer))
27398 {
27399 if (area != TEXT_AREA)
27400 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27401 else
27402 pointer = Vvoid_text_area_pointer;
27403 }
27404 #endif
27405 goto set_cursor;
27406 }
27407
27408 pos = glyph->charpos;
27409 object = glyph->object;
27410 if (!STRINGP (object) && !BUFFERP (object))
27411 goto set_cursor;
27412
27413 /* If we get an out-of-range value, return now; avoid an error. */
27414 if (BUFFERP (object) && pos > BUF_Z (b))
27415 goto set_cursor;
27416
27417 /* Make the window's buffer temporarily current for
27418 overlays_at and compute_char_face. */
27419 obuf = current_buffer;
27420 current_buffer = b;
27421 obegv = BEGV;
27422 ozv = ZV;
27423 BEGV = BEG;
27424 ZV = Z;
27425
27426 /* Is this char mouse-active or does it have help-echo? */
27427 position = make_number (pos);
27428
27429 if (BUFFERP (object))
27430 {
27431 /* Put all the overlays we want in a vector in overlay_vec. */
27432 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27433 /* Sort overlays into increasing priority order. */
27434 noverlays = sort_overlays (overlay_vec, noverlays, w);
27435 }
27436 else
27437 noverlays = 0;
27438
27439 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27440
27441 if (same_region)
27442 cursor = No_Cursor;
27443
27444 /* Check mouse-face highlighting. */
27445 if (! same_region
27446 /* If there exists an overlay with mouse-face overlapping
27447 the one we are currently highlighting, we have to
27448 check if we enter the overlapping overlay, and then
27449 highlight only that. */
27450 || (OVERLAYP (hlinfo->mouse_face_overlay)
27451 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27452 {
27453 /* Find the highest priority overlay with a mouse-face. */
27454 Lisp_Object overlay = Qnil;
27455 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27456 {
27457 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27458 if (!NILP (mouse_face))
27459 overlay = overlay_vec[i];
27460 }
27461
27462 /* If we're highlighting the same overlay as before, there's
27463 no need to do that again. */
27464 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27465 goto check_help_echo;
27466 hlinfo->mouse_face_overlay = overlay;
27467
27468 /* Clear the display of the old active region, if any. */
27469 if (clear_mouse_face (hlinfo))
27470 cursor = No_Cursor;
27471
27472 /* If no overlay applies, get a text property. */
27473 if (NILP (overlay))
27474 mouse_face = Fget_text_property (position, Qmouse_face, object);
27475
27476 /* Next, compute the bounds of the mouse highlighting and
27477 display it. */
27478 if (!NILP (mouse_face) && STRINGP (object))
27479 {
27480 /* The mouse-highlighting comes from a display string
27481 with a mouse-face. */
27482 Lisp_Object s, e;
27483 EMACS_INT ignore;
27484
27485 s = Fprevious_single_property_change
27486 (make_number (pos + 1), Qmouse_face, object, Qnil);
27487 e = Fnext_single_property_change
27488 (position, Qmouse_face, object, Qnil);
27489 if (NILP (s))
27490 s = make_number (0);
27491 if (NILP (e))
27492 e = make_number (SCHARS (object) - 1);
27493 mouse_face_from_string_pos (w, hlinfo, object,
27494 XINT (s), XINT (e));
27495 hlinfo->mouse_face_past_end = 0;
27496 hlinfo->mouse_face_window = window;
27497 hlinfo->mouse_face_face_id
27498 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27499 glyph->face_id, 1);
27500 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27501 cursor = No_Cursor;
27502 }
27503 else
27504 {
27505 /* The mouse-highlighting, if any, comes from an overlay
27506 or text property in the buffer. */
27507 Lisp_Object buffer IF_LINT (= Qnil);
27508 Lisp_Object disp_string IF_LINT (= Qnil);
27509
27510 if (STRINGP (object))
27511 {
27512 /* If we are on a display string with no mouse-face,
27513 check if the text under it has one. */
27514 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27515 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27516 pos = string_buffer_position (object, start);
27517 if (pos > 0)
27518 {
27519 mouse_face = get_char_property_and_overlay
27520 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27521 buffer = w->buffer;
27522 disp_string = object;
27523 }
27524 }
27525 else
27526 {
27527 buffer = object;
27528 disp_string = Qnil;
27529 }
27530
27531 if (!NILP (mouse_face))
27532 {
27533 Lisp_Object before, after;
27534 Lisp_Object before_string, after_string;
27535 /* To correctly find the limits of mouse highlight
27536 in a bidi-reordered buffer, we must not use the
27537 optimization of limiting the search in
27538 previous-single-property-change and
27539 next-single-property-change, because
27540 rows_from_pos_range needs the real start and end
27541 positions to DTRT in this case. That's because
27542 the first row visible in a window does not
27543 necessarily display the character whose position
27544 is the smallest. */
27545 Lisp_Object lim1 =
27546 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27547 ? Fmarker_position (w->start)
27548 : Qnil;
27549 Lisp_Object lim2 =
27550 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27551 ? make_number (BUF_Z (XBUFFER (buffer))
27552 - XFASTINT (w->window_end_pos))
27553 : Qnil;
27554
27555 if (NILP (overlay))
27556 {
27557 /* Handle the text property case. */
27558 before = Fprevious_single_property_change
27559 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27560 after = Fnext_single_property_change
27561 (make_number (pos), Qmouse_face, buffer, lim2);
27562 before_string = after_string = Qnil;
27563 }
27564 else
27565 {
27566 /* Handle the overlay case. */
27567 before = Foverlay_start (overlay);
27568 after = Foverlay_end (overlay);
27569 before_string = Foverlay_get (overlay, Qbefore_string);
27570 after_string = Foverlay_get (overlay, Qafter_string);
27571
27572 if (!STRINGP (before_string)) before_string = Qnil;
27573 if (!STRINGP (after_string)) after_string = Qnil;
27574 }
27575
27576 mouse_face_from_buffer_pos (window, hlinfo, pos,
27577 NILP (before)
27578 ? 1
27579 : XFASTINT (before),
27580 NILP (after)
27581 ? BUF_Z (XBUFFER (buffer))
27582 : XFASTINT (after),
27583 before_string, after_string,
27584 disp_string);
27585 cursor = No_Cursor;
27586 }
27587 }
27588 }
27589
27590 check_help_echo:
27591
27592 /* Look for a `help-echo' property. */
27593 if (NILP (help_echo_string)) {
27594 Lisp_Object help, overlay;
27595
27596 /* Check overlays first. */
27597 help = overlay = Qnil;
27598 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27599 {
27600 overlay = overlay_vec[i];
27601 help = Foverlay_get (overlay, Qhelp_echo);
27602 }
27603
27604 if (!NILP (help))
27605 {
27606 help_echo_string = help;
27607 help_echo_window = window;
27608 help_echo_object = overlay;
27609 help_echo_pos = pos;
27610 }
27611 else
27612 {
27613 Lisp_Object obj = glyph->object;
27614 EMACS_INT charpos = glyph->charpos;
27615
27616 /* Try text properties. */
27617 if (STRINGP (obj)
27618 && charpos >= 0
27619 && charpos < SCHARS (obj))
27620 {
27621 help = Fget_text_property (make_number (charpos),
27622 Qhelp_echo, obj);
27623 if (NILP (help))
27624 {
27625 /* If the string itself doesn't specify a help-echo,
27626 see if the buffer text ``under'' it does. */
27627 struct glyph_row *r
27628 = MATRIX_ROW (w->current_matrix, vpos);
27629 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27630 EMACS_INT p = string_buffer_position (obj, start);
27631 if (p > 0)
27632 {
27633 help = Fget_char_property (make_number (p),
27634 Qhelp_echo, w->buffer);
27635 if (!NILP (help))
27636 {
27637 charpos = p;
27638 obj = w->buffer;
27639 }
27640 }
27641 }
27642 }
27643 else if (BUFFERP (obj)
27644 && charpos >= BEGV
27645 && charpos < ZV)
27646 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27647 obj);
27648
27649 if (!NILP (help))
27650 {
27651 help_echo_string = help;
27652 help_echo_window = window;
27653 help_echo_object = obj;
27654 help_echo_pos = charpos;
27655 }
27656 }
27657 }
27658
27659 #ifdef HAVE_WINDOW_SYSTEM
27660 /* Look for a `pointer' property. */
27661 if (FRAME_WINDOW_P (f) && NILP (pointer))
27662 {
27663 /* Check overlays first. */
27664 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27665 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27666
27667 if (NILP (pointer))
27668 {
27669 Lisp_Object obj = glyph->object;
27670 EMACS_INT charpos = glyph->charpos;
27671
27672 /* Try text properties. */
27673 if (STRINGP (obj)
27674 && charpos >= 0
27675 && charpos < SCHARS (obj))
27676 {
27677 pointer = Fget_text_property (make_number (charpos),
27678 Qpointer, obj);
27679 if (NILP (pointer))
27680 {
27681 /* If the string itself doesn't specify a pointer,
27682 see if the buffer text ``under'' it does. */
27683 struct glyph_row *r
27684 = MATRIX_ROW (w->current_matrix, vpos);
27685 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27686 EMACS_INT p = string_buffer_position (obj, start);
27687 if (p > 0)
27688 pointer = Fget_char_property (make_number (p),
27689 Qpointer, w->buffer);
27690 }
27691 }
27692 else if (BUFFERP (obj)
27693 && charpos >= BEGV
27694 && charpos < ZV)
27695 pointer = Fget_text_property (make_number (charpos),
27696 Qpointer, obj);
27697 }
27698 }
27699 #endif /* HAVE_WINDOW_SYSTEM */
27700
27701 BEGV = obegv;
27702 ZV = ozv;
27703 current_buffer = obuf;
27704 }
27705
27706 set_cursor:
27707
27708 #ifdef HAVE_WINDOW_SYSTEM
27709 if (FRAME_WINDOW_P (f))
27710 define_frame_cursor1 (f, cursor, pointer);
27711 #else
27712 /* This is here to prevent a compiler error, about "label at end of
27713 compound statement". */
27714 return;
27715 #endif
27716 }
27717
27718
27719 /* EXPORT for RIF:
27720 Clear any mouse-face on window W. This function is part of the
27721 redisplay interface, and is called from try_window_id and similar
27722 functions to ensure the mouse-highlight is off. */
27723
27724 void
27725 x_clear_window_mouse_face (struct window *w)
27726 {
27727 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27728 Lisp_Object window;
27729
27730 BLOCK_INPUT;
27731 XSETWINDOW (window, w);
27732 if (EQ (window, hlinfo->mouse_face_window))
27733 clear_mouse_face (hlinfo);
27734 UNBLOCK_INPUT;
27735 }
27736
27737
27738 /* EXPORT:
27739 Just discard the mouse face information for frame F, if any.
27740 This is used when the size of F is changed. */
27741
27742 void
27743 cancel_mouse_face (struct frame *f)
27744 {
27745 Lisp_Object window;
27746 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27747
27748 window = hlinfo->mouse_face_window;
27749 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27750 {
27751 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27752 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27753 hlinfo->mouse_face_window = Qnil;
27754 }
27755 }
27756
27757
27758 \f
27759 /***********************************************************************
27760 Exposure Events
27761 ***********************************************************************/
27762
27763 #ifdef HAVE_WINDOW_SYSTEM
27764
27765 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27766 which intersects rectangle R. R is in window-relative coordinates. */
27767
27768 static void
27769 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27770 enum glyph_row_area area)
27771 {
27772 struct glyph *first = row->glyphs[area];
27773 struct glyph *end = row->glyphs[area] + row->used[area];
27774 struct glyph *last;
27775 int first_x, start_x, x;
27776
27777 if (area == TEXT_AREA && row->fill_line_p)
27778 /* If row extends face to end of line write the whole line. */
27779 draw_glyphs (w, 0, row, area,
27780 0, row->used[area],
27781 DRAW_NORMAL_TEXT, 0);
27782 else
27783 {
27784 /* Set START_X to the window-relative start position for drawing glyphs of
27785 AREA. The first glyph of the text area can be partially visible.
27786 The first glyphs of other areas cannot. */
27787 start_x = window_box_left_offset (w, area);
27788 x = start_x;
27789 if (area == TEXT_AREA)
27790 x += row->x;
27791
27792 /* Find the first glyph that must be redrawn. */
27793 while (first < end
27794 && x + first->pixel_width < r->x)
27795 {
27796 x += first->pixel_width;
27797 ++first;
27798 }
27799
27800 /* Find the last one. */
27801 last = first;
27802 first_x = x;
27803 while (last < end
27804 && x < r->x + r->width)
27805 {
27806 x += last->pixel_width;
27807 ++last;
27808 }
27809
27810 /* Repaint. */
27811 if (last > first)
27812 draw_glyphs (w, first_x - start_x, row, area,
27813 first - row->glyphs[area], last - row->glyphs[area],
27814 DRAW_NORMAL_TEXT, 0);
27815 }
27816 }
27817
27818
27819 /* Redraw the parts of the glyph row ROW on window W intersecting
27820 rectangle R. R is in window-relative coordinates. Value is
27821 non-zero if mouse-face was overwritten. */
27822
27823 static int
27824 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27825 {
27826 xassert (row->enabled_p);
27827
27828 if (row->mode_line_p || w->pseudo_window_p)
27829 draw_glyphs (w, 0, row, TEXT_AREA,
27830 0, row->used[TEXT_AREA],
27831 DRAW_NORMAL_TEXT, 0);
27832 else
27833 {
27834 if (row->used[LEFT_MARGIN_AREA])
27835 expose_area (w, row, r, LEFT_MARGIN_AREA);
27836 if (row->used[TEXT_AREA])
27837 expose_area (w, row, r, TEXT_AREA);
27838 if (row->used[RIGHT_MARGIN_AREA])
27839 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27840 draw_row_fringe_bitmaps (w, row);
27841 }
27842
27843 return row->mouse_face_p;
27844 }
27845
27846
27847 /* Redraw those parts of glyphs rows during expose event handling that
27848 overlap other rows. Redrawing of an exposed line writes over parts
27849 of lines overlapping that exposed line; this function fixes that.
27850
27851 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27852 row in W's current matrix that is exposed and overlaps other rows.
27853 LAST_OVERLAPPING_ROW is the last such row. */
27854
27855 static void
27856 expose_overlaps (struct window *w,
27857 struct glyph_row *first_overlapping_row,
27858 struct glyph_row *last_overlapping_row,
27859 XRectangle *r)
27860 {
27861 struct glyph_row *row;
27862
27863 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27864 if (row->overlapping_p)
27865 {
27866 xassert (row->enabled_p && !row->mode_line_p);
27867
27868 row->clip = r;
27869 if (row->used[LEFT_MARGIN_AREA])
27870 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27871
27872 if (row->used[TEXT_AREA])
27873 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27874
27875 if (row->used[RIGHT_MARGIN_AREA])
27876 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27877 row->clip = NULL;
27878 }
27879 }
27880
27881
27882 /* Return non-zero if W's cursor intersects rectangle R. */
27883
27884 static int
27885 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27886 {
27887 XRectangle cr, result;
27888 struct glyph *cursor_glyph;
27889 struct glyph_row *row;
27890
27891 if (w->phys_cursor.vpos >= 0
27892 && w->phys_cursor.vpos < w->current_matrix->nrows
27893 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27894 row->enabled_p)
27895 && row->cursor_in_fringe_p)
27896 {
27897 /* Cursor is in the fringe. */
27898 cr.x = window_box_right_offset (w,
27899 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27900 ? RIGHT_MARGIN_AREA
27901 : TEXT_AREA));
27902 cr.y = row->y;
27903 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27904 cr.height = row->height;
27905 return x_intersect_rectangles (&cr, r, &result);
27906 }
27907
27908 cursor_glyph = get_phys_cursor_glyph (w);
27909 if (cursor_glyph)
27910 {
27911 /* r is relative to W's box, but w->phys_cursor.x is relative
27912 to left edge of W's TEXT area. Adjust it. */
27913 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27914 cr.y = w->phys_cursor.y;
27915 cr.width = cursor_glyph->pixel_width;
27916 cr.height = w->phys_cursor_height;
27917 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27918 I assume the effect is the same -- and this is portable. */
27919 return x_intersect_rectangles (&cr, r, &result);
27920 }
27921 /* If we don't understand the format, pretend we're not in the hot-spot. */
27922 return 0;
27923 }
27924
27925
27926 /* EXPORT:
27927 Draw a vertical window border to the right of window W if W doesn't
27928 have vertical scroll bars. */
27929
27930 void
27931 x_draw_vertical_border (struct window *w)
27932 {
27933 struct frame *f = XFRAME (WINDOW_FRAME (w));
27934
27935 /* We could do better, if we knew what type of scroll-bar the adjacent
27936 windows (on either side) have... But we don't :-(
27937 However, I think this works ok. ++KFS 2003-04-25 */
27938
27939 /* Redraw borders between horizontally adjacent windows. Don't
27940 do it for frames with vertical scroll bars because either the
27941 right scroll bar of a window, or the left scroll bar of its
27942 neighbor will suffice as a border. */
27943 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27944 return;
27945
27946 if (!WINDOW_RIGHTMOST_P (w)
27947 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27948 {
27949 int x0, x1, y0, y1;
27950
27951 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27952 y1 -= 1;
27953
27954 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27955 x1 -= 1;
27956
27957 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27958 }
27959 else if (!WINDOW_LEFTMOST_P (w)
27960 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27961 {
27962 int x0, x1, y0, y1;
27963
27964 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27965 y1 -= 1;
27966
27967 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27968 x0 -= 1;
27969
27970 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27971 }
27972 }
27973
27974
27975 /* Redraw the part of window W intersection rectangle FR. Pixel
27976 coordinates in FR are frame-relative. Call this function with
27977 input blocked. Value is non-zero if the exposure overwrites
27978 mouse-face. */
27979
27980 static int
27981 expose_window (struct window *w, XRectangle *fr)
27982 {
27983 struct frame *f = XFRAME (w->frame);
27984 XRectangle wr, r;
27985 int mouse_face_overwritten_p = 0;
27986
27987 /* If window is not yet fully initialized, do nothing. This can
27988 happen when toolkit scroll bars are used and a window is split.
27989 Reconfiguring the scroll bar will generate an expose for a newly
27990 created window. */
27991 if (w->current_matrix == NULL)
27992 return 0;
27993
27994 /* When we're currently updating the window, display and current
27995 matrix usually don't agree. Arrange for a thorough display
27996 later. */
27997 if (w == updated_window)
27998 {
27999 SET_FRAME_GARBAGED (f);
28000 return 0;
28001 }
28002
28003 /* Frame-relative pixel rectangle of W. */
28004 wr.x = WINDOW_LEFT_EDGE_X (w);
28005 wr.y = WINDOW_TOP_EDGE_Y (w);
28006 wr.width = WINDOW_TOTAL_WIDTH (w);
28007 wr.height = WINDOW_TOTAL_HEIGHT (w);
28008
28009 if (x_intersect_rectangles (fr, &wr, &r))
28010 {
28011 int yb = window_text_bottom_y (w);
28012 struct glyph_row *row;
28013 int cursor_cleared_p, phys_cursor_on_p;
28014 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28015
28016 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28017 r.x, r.y, r.width, r.height));
28018
28019 /* Convert to window coordinates. */
28020 r.x -= WINDOW_LEFT_EDGE_X (w);
28021 r.y -= WINDOW_TOP_EDGE_Y (w);
28022
28023 /* Turn off the cursor. */
28024 if (!w->pseudo_window_p
28025 && phys_cursor_in_rect_p (w, &r))
28026 {
28027 x_clear_cursor (w);
28028 cursor_cleared_p = 1;
28029 }
28030 else
28031 cursor_cleared_p = 0;
28032
28033 /* If the row containing the cursor extends face to end of line,
28034 then expose_area might overwrite the cursor outside the
28035 rectangle and thus notice_overwritten_cursor might clear
28036 w->phys_cursor_on_p. We remember the original value and
28037 check later if it is changed. */
28038 phys_cursor_on_p = w->phys_cursor_on_p;
28039
28040 /* Update lines intersecting rectangle R. */
28041 first_overlapping_row = last_overlapping_row = NULL;
28042 for (row = w->current_matrix->rows;
28043 row->enabled_p;
28044 ++row)
28045 {
28046 int y0 = row->y;
28047 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28048
28049 if ((y0 >= r.y && y0 < r.y + r.height)
28050 || (y1 > r.y && y1 < r.y + r.height)
28051 || (r.y >= y0 && r.y < y1)
28052 || (r.y + r.height > y0 && r.y + r.height < y1))
28053 {
28054 /* A header line may be overlapping, but there is no need
28055 to fix overlapping areas for them. KFS 2005-02-12 */
28056 if (row->overlapping_p && !row->mode_line_p)
28057 {
28058 if (first_overlapping_row == NULL)
28059 first_overlapping_row = row;
28060 last_overlapping_row = row;
28061 }
28062
28063 row->clip = fr;
28064 if (expose_line (w, row, &r))
28065 mouse_face_overwritten_p = 1;
28066 row->clip = NULL;
28067 }
28068 else if (row->overlapping_p)
28069 {
28070 /* We must redraw a row overlapping the exposed area. */
28071 if (y0 < r.y
28072 ? y0 + row->phys_height > r.y
28073 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28074 {
28075 if (first_overlapping_row == NULL)
28076 first_overlapping_row = row;
28077 last_overlapping_row = row;
28078 }
28079 }
28080
28081 if (y1 >= yb)
28082 break;
28083 }
28084
28085 /* Display the mode line if there is one. */
28086 if (WINDOW_WANTS_MODELINE_P (w)
28087 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28088 row->enabled_p)
28089 && row->y < r.y + r.height)
28090 {
28091 if (expose_line (w, row, &r))
28092 mouse_face_overwritten_p = 1;
28093 }
28094
28095 if (!w->pseudo_window_p)
28096 {
28097 /* Fix the display of overlapping rows. */
28098 if (first_overlapping_row)
28099 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28100 fr);
28101
28102 /* Draw border between windows. */
28103 x_draw_vertical_border (w);
28104
28105 /* Turn the cursor on again. */
28106 if (cursor_cleared_p
28107 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28108 update_window_cursor (w, 1);
28109 }
28110 }
28111
28112 return mouse_face_overwritten_p;
28113 }
28114
28115
28116
28117 /* Redraw (parts) of all windows in the window tree rooted at W that
28118 intersect R. R contains frame pixel coordinates. Value is
28119 non-zero if the exposure overwrites mouse-face. */
28120
28121 static int
28122 expose_window_tree (struct window *w, XRectangle *r)
28123 {
28124 struct frame *f = XFRAME (w->frame);
28125 int mouse_face_overwritten_p = 0;
28126
28127 while (w && !FRAME_GARBAGED_P (f))
28128 {
28129 if (!NILP (w->hchild))
28130 mouse_face_overwritten_p
28131 |= expose_window_tree (XWINDOW (w->hchild), r);
28132 else if (!NILP (w->vchild))
28133 mouse_face_overwritten_p
28134 |= expose_window_tree (XWINDOW (w->vchild), r);
28135 else
28136 mouse_face_overwritten_p |= expose_window (w, r);
28137
28138 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28139 }
28140
28141 return mouse_face_overwritten_p;
28142 }
28143
28144
28145 /* EXPORT:
28146 Redisplay an exposed area of frame F. X and Y are the upper-left
28147 corner of the exposed rectangle. W and H are width and height of
28148 the exposed area. All are pixel values. W or H zero means redraw
28149 the entire frame. */
28150
28151 void
28152 expose_frame (struct frame *f, int x, int y, int w, int h)
28153 {
28154 XRectangle r;
28155 int mouse_face_overwritten_p = 0;
28156
28157 TRACE ((stderr, "expose_frame "));
28158
28159 /* No need to redraw if frame will be redrawn soon. */
28160 if (FRAME_GARBAGED_P (f))
28161 {
28162 TRACE ((stderr, " garbaged\n"));
28163 return;
28164 }
28165
28166 /* If basic faces haven't been realized yet, there is no point in
28167 trying to redraw anything. This can happen when we get an expose
28168 event while Emacs is starting, e.g. by moving another window. */
28169 if (FRAME_FACE_CACHE (f) == NULL
28170 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28171 {
28172 TRACE ((stderr, " no faces\n"));
28173 return;
28174 }
28175
28176 if (w == 0 || h == 0)
28177 {
28178 r.x = r.y = 0;
28179 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28180 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28181 }
28182 else
28183 {
28184 r.x = x;
28185 r.y = y;
28186 r.width = w;
28187 r.height = h;
28188 }
28189
28190 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28191 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28192
28193 if (WINDOWP (f->tool_bar_window))
28194 mouse_face_overwritten_p
28195 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28196
28197 #ifdef HAVE_X_WINDOWS
28198 #ifndef MSDOS
28199 #ifndef USE_X_TOOLKIT
28200 if (WINDOWP (f->menu_bar_window))
28201 mouse_face_overwritten_p
28202 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28203 #endif /* not USE_X_TOOLKIT */
28204 #endif
28205 #endif
28206
28207 /* Some window managers support a focus-follows-mouse style with
28208 delayed raising of frames. Imagine a partially obscured frame,
28209 and moving the mouse into partially obscured mouse-face on that
28210 frame. The visible part of the mouse-face will be highlighted,
28211 then the WM raises the obscured frame. With at least one WM, KDE
28212 2.1, Emacs is not getting any event for the raising of the frame
28213 (even tried with SubstructureRedirectMask), only Expose events.
28214 These expose events will draw text normally, i.e. not
28215 highlighted. Which means we must redo the highlight here.
28216 Subsume it under ``we love X''. --gerd 2001-08-15 */
28217 /* Included in Windows version because Windows most likely does not
28218 do the right thing if any third party tool offers
28219 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28220 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28221 {
28222 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28223 if (f == hlinfo->mouse_face_mouse_frame)
28224 {
28225 int mouse_x = hlinfo->mouse_face_mouse_x;
28226 int mouse_y = hlinfo->mouse_face_mouse_y;
28227 clear_mouse_face (hlinfo);
28228 note_mouse_highlight (f, mouse_x, mouse_y);
28229 }
28230 }
28231 }
28232
28233
28234 /* EXPORT:
28235 Determine the intersection of two rectangles R1 and R2. Return
28236 the intersection in *RESULT. Value is non-zero if RESULT is not
28237 empty. */
28238
28239 int
28240 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28241 {
28242 XRectangle *left, *right;
28243 XRectangle *upper, *lower;
28244 int intersection_p = 0;
28245
28246 /* Rearrange so that R1 is the left-most rectangle. */
28247 if (r1->x < r2->x)
28248 left = r1, right = r2;
28249 else
28250 left = r2, right = r1;
28251
28252 /* X0 of the intersection is right.x0, if this is inside R1,
28253 otherwise there is no intersection. */
28254 if (right->x <= left->x + left->width)
28255 {
28256 result->x = right->x;
28257
28258 /* The right end of the intersection is the minimum of
28259 the right ends of left and right. */
28260 result->width = (min (left->x + left->width, right->x + right->width)
28261 - result->x);
28262
28263 /* Same game for Y. */
28264 if (r1->y < r2->y)
28265 upper = r1, lower = r2;
28266 else
28267 upper = r2, lower = r1;
28268
28269 /* The upper end of the intersection is lower.y0, if this is inside
28270 of upper. Otherwise, there is no intersection. */
28271 if (lower->y <= upper->y + upper->height)
28272 {
28273 result->y = lower->y;
28274
28275 /* The lower end of the intersection is the minimum of the lower
28276 ends of upper and lower. */
28277 result->height = (min (lower->y + lower->height,
28278 upper->y + upper->height)
28279 - result->y);
28280 intersection_p = 1;
28281 }
28282 }
28283
28284 return intersection_p;
28285 }
28286
28287 #endif /* HAVE_WINDOW_SYSTEM */
28288
28289 \f
28290 /***********************************************************************
28291 Initialization
28292 ***********************************************************************/
28293
28294 void
28295 syms_of_xdisp (void)
28296 {
28297 Vwith_echo_area_save_vector = Qnil;
28298 staticpro (&Vwith_echo_area_save_vector);
28299
28300 Vmessage_stack = Qnil;
28301 staticpro (&Vmessage_stack);
28302
28303 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28304
28305 message_dolog_marker1 = Fmake_marker ();
28306 staticpro (&message_dolog_marker1);
28307 message_dolog_marker2 = Fmake_marker ();
28308 staticpro (&message_dolog_marker2);
28309 message_dolog_marker3 = Fmake_marker ();
28310 staticpro (&message_dolog_marker3);
28311
28312 #if GLYPH_DEBUG
28313 defsubr (&Sdump_frame_glyph_matrix);
28314 defsubr (&Sdump_glyph_matrix);
28315 defsubr (&Sdump_glyph_row);
28316 defsubr (&Sdump_tool_bar_row);
28317 defsubr (&Strace_redisplay);
28318 defsubr (&Strace_to_stderr);
28319 #endif
28320 #ifdef HAVE_WINDOW_SYSTEM
28321 defsubr (&Stool_bar_lines_needed);
28322 defsubr (&Slookup_image_map);
28323 #endif
28324 defsubr (&Sformat_mode_line);
28325 defsubr (&Sinvisible_p);
28326 defsubr (&Scurrent_bidi_paragraph_direction);
28327
28328 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28329 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28330 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28331 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28332 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28333 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28334 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28335 DEFSYM (Qeval, "eval");
28336 DEFSYM (QCdata, ":data");
28337 DEFSYM (Qdisplay, "display");
28338 DEFSYM (Qspace_width, "space-width");
28339 DEFSYM (Qraise, "raise");
28340 DEFSYM (Qslice, "slice");
28341 DEFSYM (Qspace, "space");
28342 DEFSYM (Qmargin, "margin");
28343 DEFSYM (Qpointer, "pointer");
28344 DEFSYM (Qleft_margin, "left-margin");
28345 DEFSYM (Qright_margin, "right-margin");
28346 DEFSYM (Qcenter, "center");
28347 DEFSYM (Qline_height, "line-height");
28348 DEFSYM (QCalign_to, ":align-to");
28349 DEFSYM (QCrelative_width, ":relative-width");
28350 DEFSYM (QCrelative_height, ":relative-height");
28351 DEFSYM (QCeval, ":eval");
28352 DEFSYM (QCpropertize, ":propertize");
28353 DEFSYM (QCfile, ":file");
28354 DEFSYM (Qfontified, "fontified");
28355 DEFSYM (Qfontification_functions, "fontification-functions");
28356 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28357 DEFSYM (Qescape_glyph, "escape-glyph");
28358 DEFSYM (Qnobreak_space, "nobreak-space");
28359 DEFSYM (Qimage, "image");
28360 DEFSYM (Qtext, "text");
28361 DEFSYM (Qboth, "both");
28362 DEFSYM (Qboth_horiz, "both-horiz");
28363 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28364 DEFSYM (QCmap, ":map");
28365 DEFSYM (QCpointer, ":pointer");
28366 DEFSYM (Qrect, "rect");
28367 DEFSYM (Qcircle, "circle");
28368 DEFSYM (Qpoly, "poly");
28369 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28370 DEFSYM (Qgrow_only, "grow-only");
28371 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28372 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28373 DEFSYM (Qposition, "position");
28374 DEFSYM (Qbuffer_position, "buffer-position");
28375 DEFSYM (Qobject, "object");
28376 DEFSYM (Qbar, "bar");
28377 DEFSYM (Qhbar, "hbar");
28378 DEFSYM (Qbox, "box");
28379 DEFSYM (Qhollow, "hollow");
28380 DEFSYM (Qhand, "hand");
28381 DEFSYM (Qarrow, "arrow");
28382 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28383
28384 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28385 Fcons (intern_c_string ("void-variable"), Qnil)),
28386 Qnil);
28387 staticpro (&list_of_error);
28388
28389 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28390 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28391 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28392 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28393
28394 echo_buffer[0] = echo_buffer[1] = Qnil;
28395 staticpro (&echo_buffer[0]);
28396 staticpro (&echo_buffer[1]);
28397
28398 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28399 staticpro (&echo_area_buffer[0]);
28400 staticpro (&echo_area_buffer[1]);
28401
28402 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28403 staticpro (&Vmessages_buffer_name);
28404
28405 mode_line_proptrans_alist = Qnil;
28406 staticpro (&mode_line_proptrans_alist);
28407 mode_line_string_list = Qnil;
28408 staticpro (&mode_line_string_list);
28409 mode_line_string_face = Qnil;
28410 staticpro (&mode_line_string_face);
28411 mode_line_string_face_prop = Qnil;
28412 staticpro (&mode_line_string_face_prop);
28413 Vmode_line_unwind_vector = Qnil;
28414 staticpro (&Vmode_line_unwind_vector);
28415
28416 help_echo_string = Qnil;
28417 staticpro (&help_echo_string);
28418 help_echo_object = Qnil;
28419 staticpro (&help_echo_object);
28420 help_echo_window = Qnil;
28421 staticpro (&help_echo_window);
28422 previous_help_echo_string = Qnil;
28423 staticpro (&previous_help_echo_string);
28424 help_echo_pos = -1;
28425
28426 DEFSYM (Qright_to_left, "right-to-left");
28427 DEFSYM (Qleft_to_right, "left-to-right");
28428
28429 #ifdef HAVE_WINDOW_SYSTEM
28430 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28431 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28432 For example, if a block cursor is over a tab, it will be drawn as
28433 wide as that tab on the display. */);
28434 x_stretch_cursor_p = 0;
28435 #endif
28436
28437 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28438 doc: /* Non-nil means highlight trailing whitespace.
28439 The face used for trailing whitespace is `trailing-whitespace'. */);
28440 Vshow_trailing_whitespace = Qnil;
28441
28442 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28443 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28444 If the value is t, Emacs highlights non-ASCII chars which have the
28445 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28446 or `escape-glyph' face respectively.
28447
28448 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28449 U+2011 (non-breaking hyphen) are affected.
28450
28451 Any other non-nil value means to display these characters as a escape
28452 glyph followed by an ordinary space or hyphen.
28453
28454 A value of nil means no special handling of these characters. */);
28455 Vnobreak_char_display = Qt;
28456
28457 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28458 doc: /* The pointer shape to show in void text areas.
28459 A value of nil means to show the text pointer. Other options are `arrow',
28460 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28461 Vvoid_text_area_pointer = Qarrow;
28462
28463 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28464 doc: /* Non-nil means don't actually do any redisplay.
28465 This is used for internal purposes. */);
28466 Vinhibit_redisplay = Qnil;
28467
28468 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28469 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28470 Vglobal_mode_string = Qnil;
28471
28472 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28473 doc: /* Marker for where to display an arrow on top of the buffer text.
28474 This must be the beginning of a line in order to work.
28475 See also `overlay-arrow-string'. */);
28476 Voverlay_arrow_position = Qnil;
28477
28478 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28479 doc: /* String to display as an arrow in non-window frames.
28480 See also `overlay-arrow-position'. */);
28481 Voverlay_arrow_string = make_pure_c_string ("=>");
28482
28483 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28484 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28485 The symbols on this list are examined during redisplay to determine
28486 where to display overlay arrows. */);
28487 Voverlay_arrow_variable_list
28488 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28489
28490 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28491 doc: /* The number of lines to try scrolling a window by when point moves out.
28492 If that fails to bring point back on frame, point is centered instead.
28493 If this is zero, point is always centered after it moves off frame.
28494 If you want scrolling to always be a line at a time, you should set
28495 `scroll-conservatively' to a large value rather than set this to 1. */);
28496
28497 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28498 doc: /* Scroll up to this many lines, to bring point back on screen.
28499 If point moves off-screen, redisplay will scroll by up to
28500 `scroll-conservatively' lines in order to bring point just barely
28501 onto the screen again. If that cannot be done, then redisplay
28502 recenters point as usual.
28503
28504 If the value is greater than 100, redisplay will never recenter point,
28505 but will always scroll just enough text to bring point into view, even
28506 if you move far away.
28507
28508 A value of zero means always recenter point if it moves off screen. */);
28509 scroll_conservatively = 0;
28510
28511 DEFVAR_INT ("scroll-margin", scroll_margin,
28512 doc: /* Number of lines of margin at the top and bottom of a window.
28513 Recenter the window whenever point gets within this many lines
28514 of the top or bottom of the window. */);
28515 scroll_margin = 0;
28516
28517 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28518 doc: /* Pixels per inch value for non-window system displays.
28519 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28520 Vdisplay_pixels_per_inch = make_float (72.0);
28521
28522 #if GLYPH_DEBUG
28523 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28524 #endif
28525
28526 DEFVAR_LISP ("truncate-partial-width-windows",
28527 Vtruncate_partial_width_windows,
28528 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28529 For an integer value, truncate lines in each window narrower than the
28530 full frame width, provided the window width is less than that integer;
28531 otherwise, respect the value of `truncate-lines'.
28532
28533 For any other non-nil value, truncate lines in all windows that do
28534 not span the full frame width.
28535
28536 A value of nil means to respect the value of `truncate-lines'.
28537
28538 If `word-wrap' is enabled, you might want to reduce this. */);
28539 Vtruncate_partial_width_windows = make_number (50);
28540
28541 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28542 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28543 Any other value means to use the appropriate face, `mode-line',
28544 `header-line', or `menu' respectively. */);
28545 mode_line_inverse_video = 1;
28546
28547 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28548 doc: /* Maximum buffer size for which line number should be displayed.
28549 If the buffer is bigger than this, the line number does not appear
28550 in the mode line. A value of nil means no limit. */);
28551 Vline_number_display_limit = Qnil;
28552
28553 DEFVAR_INT ("line-number-display-limit-width",
28554 line_number_display_limit_width,
28555 doc: /* Maximum line width (in characters) for line number display.
28556 If the average length of the lines near point is bigger than this, then the
28557 line number may be omitted from the mode line. */);
28558 line_number_display_limit_width = 200;
28559
28560 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28561 doc: /* Non-nil means highlight region even in nonselected windows. */);
28562 highlight_nonselected_windows = 0;
28563
28564 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28565 doc: /* Non-nil if more than one frame is visible on this display.
28566 Minibuffer-only frames don't count, but iconified frames do.
28567 This variable is not guaranteed to be accurate except while processing
28568 `frame-title-format' and `icon-title-format'. */);
28569
28570 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28571 doc: /* Template for displaying the title bar of visible frames.
28572 \(Assuming the window manager supports this feature.)
28573
28574 This variable has the same structure as `mode-line-format', except that
28575 the %c and %l constructs are ignored. It is used only on frames for
28576 which no explicit name has been set \(see `modify-frame-parameters'). */);
28577
28578 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28579 doc: /* Template for displaying the title bar of an iconified frame.
28580 \(Assuming the window manager supports this feature.)
28581 This variable has the same structure as `mode-line-format' (which see),
28582 and is used only on frames for which no explicit name has been set
28583 \(see `modify-frame-parameters'). */);
28584 Vicon_title_format
28585 = Vframe_title_format
28586 = pure_cons (intern_c_string ("multiple-frames"),
28587 pure_cons (make_pure_c_string ("%b"),
28588 pure_cons (pure_cons (empty_unibyte_string,
28589 pure_cons (intern_c_string ("invocation-name"),
28590 pure_cons (make_pure_c_string ("@"),
28591 pure_cons (intern_c_string ("system-name"),
28592 Qnil)))),
28593 Qnil)));
28594
28595 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28596 doc: /* Maximum number of lines to keep in the message log buffer.
28597 If nil, disable message logging. If t, log messages but don't truncate
28598 the buffer when it becomes large. */);
28599 Vmessage_log_max = make_number (100);
28600
28601 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28602 doc: /* Functions called before redisplay, if window sizes have changed.
28603 The value should be a list of functions that take one argument.
28604 Just before redisplay, for each frame, if any of its windows have changed
28605 size since the last redisplay, or have been split or deleted,
28606 all the functions in the list are called, with the frame as argument. */);
28607 Vwindow_size_change_functions = Qnil;
28608
28609 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28610 doc: /* List of functions to call before redisplaying a window with scrolling.
28611 Each function is called with two arguments, the window and its new
28612 display-start position. Note that these functions are also called by
28613 `set-window-buffer'. Also note that the value of `window-end' is not
28614 valid when these functions are called.
28615
28616 Warning: Do not use this feature to alter the way the window
28617 is scrolled. It is not designed for that, and such use probably won't
28618 work. */);
28619 Vwindow_scroll_functions = Qnil;
28620
28621 DEFVAR_LISP ("window-text-change-functions",
28622 Vwindow_text_change_functions,
28623 doc: /* Functions to call in redisplay when text in the window might change. */);
28624 Vwindow_text_change_functions = Qnil;
28625
28626 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28627 doc: /* Functions called when redisplay of a window reaches the end trigger.
28628 Each function is called with two arguments, the window and the end trigger value.
28629 See `set-window-redisplay-end-trigger'. */);
28630 Vredisplay_end_trigger_functions = Qnil;
28631
28632 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28633 doc: /* Non-nil means autoselect window with mouse pointer.
28634 If nil, do not autoselect windows.
28635 A positive number means delay autoselection by that many seconds: a
28636 window is autoselected only after the mouse has remained in that
28637 window for the duration of the delay.
28638 A negative number has a similar effect, but causes windows to be
28639 autoselected only after the mouse has stopped moving. \(Because of
28640 the way Emacs compares mouse events, you will occasionally wait twice
28641 that time before the window gets selected.\)
28642 Any other value means to autoselect window instantaneously when the
28643 mouse pointer enters it.
28644
28645 Autoselection selects the minibuffer only if it is active, and never
28646 unselects the minibuffer if it is active.
28647
28648 When customizing this variable make sure that the actual value of
28649 `focus-follows-mouse' matches the behavior of your window manager. */);
28650 Vmouse_autoselect_window = Qnil;
28651
28652 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28653 doc: /* Non-nil means automatically resize tool-bars.
28654 This dynamically changes the tool-bar's height to the minimum height
28655 that is needed to make all tool-bar items visible.
28656 If value is `grow-only', the tool-bar's height is only increased
28657 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28658 Vauto_resize_tool_bars = Qt;
28659
28660 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28661 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28662 auto_raise_tool_bar_buttons_p = 1;
28663
28664 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28665 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28666 make_cursor_line_fully_visible_p = 1;
28667
28668 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28669 doc: /* Border below tool-bar in pixels.
28670 If an integer, use it as the height of the border.
28671 If it is one of `internal-border-width' or `border-width', use the
28672 value of the corresponding frame parameter.
28673 Otherwise, no border is added below the tool-bar. */);
28674 Vtool_bar_border = Qinternal_border_width;
28675
28676 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28677 doc: /* Margin around tool-bar buttons in pixels.
28678 If an integer, use that for both horizontal and vertical margins.
28679 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28680 HORZ specifying the horizontal margin, and VERT specifying the
28681 vertical margin. */);
28682 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28683
28684 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28685 doc: /* Relief thickness of tool-bar buttons. */);
28686 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28687
28688 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28689 doc: /* Tool bar style to use.
28690 It can be one of
28691 image - show images only
28692 text - show text only
28693 both - show both, text below image
28694 both-horiz - show text to the right of the image
28695 text-image-horiz - show text to the left of the image
28696 any other - use system default or image if no system default. */);
28697 Vtool_bar_style = Qnil;
28698
28699 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28700 doc: /* Maximum number of characters a label can have to be shown.
28701 The tool bar style must also show labels for this to have any effect, see
28702 `tool-bar-style'. */);
28703 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28704
28705 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28706 doc: /* List of functions to call to fontify regions of text.
28707 Each function is called with one argument POS. Functions must
28708 fontify a region starting at POS in the current buffer, and give
28709 fontified regions the property `fontified'. */);
28710 Vfontification_functions = Qnil;
28711 Fmake_variable_buffer_local (Qfontification_functions);
28712
28713 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28714 unibyte_display_via_language_environment,
28715 doc: /* Non-nil means display unibyte text according to language environment.
28716 Specifically, this means that raw bytes in the range 160-255 decimal
28717 are displayed by converting them to the equivalent multibyte characters
28718 according to the current language environment. As a result, they are
28719 displayed according to the current fontset.
28720
28721 Note that this variable affects only how these bytes are displayed,
28722 but does not change the fact they are interpreted as raw bytes. */);
28723 unibyte_display_via_language_environment = 0;
28724
28725 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28726 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28727 If a float, it specifies a fraction of the mini-window frame's height.
28728 If an integer, it specifies a number of lines. */);
28729 Vmax_mini_window_height = make_float (0.25);
28730
28731 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28732 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28733 A value of nil means don't automatically resize mini-windows.
28734 A value of t means resize them to fit the text displayed in them.
28735 A value of `grow-only', the default, means let mini-windows grow only;
28736 they return to their normal size when the minibuffer is closed, or the
28737 echo area becomes empty. */);
28738 Vresize_mini_windows = Qgrow_only;
28739
28740 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28741 doc: /* Alist specifying how to blink the cursor off.
28742 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28743 `cursor-type' frame-parameter or variable equals ON-STATE,
28744 comparing using `equal', Emacs uses OFF-STATE to specify
28745 how to blink it off. ON-STATE and OFF-STATE are values for
28746 the `cursor-type' frame parameter.
28747
28748 If a frame's ON-STATE has no entry in this list,
28749 the frame's other specifications determine how to blink the cursor off. */);
28750 Vblink_cursor_alist = Qnil;
28751
28752 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28753 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28754 If non-nil, windows are automatically scrolled horizontally to make
28755 point visible. */);
28756 automatic_hscrolling_p = 1;
28757 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28758
28759 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28760 doc: /* How many columns away from the window edge point is allowed to get
28761 before automatic hscrolling will horizontally scroll the window. */);
28762 hscroll_margin = 5;
28763
28764 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28765 doc: /* How many columns to scroll the window when point gets too close to the edge.
28766 When point is less than `hscroll-margin' columns from the window
28767 edge, automatic hscrolling will scroll the window by the amount of columns
28768 determined by this variable. If its value is a positive integer, scroll that
28769 many columns. If it's a positive floating-point number, it specifies the
28770 fraction of the window's width to scroll. If it's nil or zero, point will be
28771 centered horizontally after the scroll. Any other value, including negative
28772 numbers, are treated as if the value were zero.
28773
28774 Automatic hscrolling always moves point outside the scroll margin, so if
28775 point was more than scroll step columns inside the margin, the window will
28776 scroll more than the value given by the scroll step.
28777
28778 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28779 and `scroll-right' overrides this variable's effect. */);
28780 Vhscroll_step = make_number (0);
28781
28782 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28783 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28784 Bind this around calls to `message' to let it take effect. */);
28785 message_truncate_lines = 0;
28786
28787 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28788 doc: /* Normal hook run to update the menu bar definitions.
28789 Redisplay runs this hook before it redisplays the menu bar.
28790 This is used to update submenus such as Buffers,
28791 whose contents depend on various data. */);
28792 Vmenu_bar_update_hook = Qnil;
28793
28794 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28795 doc: /* Frame for which we are updating a menu.
28796 The enable predicate for a menu binding should check this variable. */);
28797 Vmenu_updating_frame = Qnil;
28798
28799 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28800 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28801 inhibit_menubar_update = 0;
28802
28803 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28804 doc: /* Prefix prepended to all continuation lines at display time.
28805 The value may be a string, an image, or a stretch-glyph; it is
28806 interpreted in the same way as the value of a `display' text property.
28807
28808 This variable is overridden by any `wrap-prefix' text or overlay
28809 property.
28810
28811 To add a prefix to non-continuation lines, use `line-prefix'. */);
28812 Vwrap_prefix = Qnil;
28813 DEFSYM (Qwrap_prefix, "wrap-prefix");
28814 Fmake_variable_buffer_local (Qwrap_prefix);
28815
28816 DEFVAR_LISP ("line-prefix", Vline_prefix,
28817 doc: /* Prefix prepended to all non-continuation lines at display time.
28818 The value may be a string, an image, or a stretch-glyph; it is
28819 interpreted in the same way as the value of a `display' text property.
28820
28821 This variable is overridden by any `line-prefix' text or overlay
28822 property.
28823
28824 To add a prefix to continuation lines, use `wrap-prefix'. */);
28825 Vline_prefix = Qnil;
28826 DEFSYM (Qline_prefix, "line-prefix");
28827 Fmake_variable_buffer_local (Qline_prefix);
28828
28829 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28830 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28831 inhibit_eval_during_redisplay = 0;
28832
28833 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28834 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28835 inhibit_free_realized_faces = 0;
28836
28837 #if GLYPH_DEBUG
28838 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28839 doc: /* Inhibit try_window_id display optimization. */);
28840 inhibit_try_window_id = 0;
28841
28842 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28843 doc: /* Inhibit try_window_reusing display optimization. */);
28844 inhibit_try_window_reusing = 0;
28845
28846 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28847 doc: /* Inhibit try_cursor_movement display optimization. */);
28848 inhibit_try_cursor_movement = 0;
28849 #endif /* GLYPH_DEBUG */
28850
28851 DEFVAR_INT ("overline-margin", overline_margin,
28852 doc: /* Space between overline and text, in pixels.
28853 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28854 margin to the character height. */);
28855 overline_margin = 2;
28856
28857 DEFVAR_INT ("underline-minimum-offset",
28858 underline_minimum_offset,
28859 doc: /* Minimum distance between baseline and underline.
28860 This can improve legibility of underlined text at small font sizes,
28861 particularly when using variable `x-use-underline-position-properties'
28862 with fonts that specify an UNDERLINE_POSITION relatively close to the
28863 baseline. The default value is 1. */);
28864 underline_minimum_offset = 1;
28865
28866 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28867 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28868 This feature only works when on a window system that can change
28869 cursor shapes. */);
28870 display_hourglass_p = 1;
28871
28872 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28873 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28874 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28875
28876 hourglass_atimer = NULL;
28877 hourglass_shown_p = 0;
28878
28879 DEFSYM (Qglyphless_char, "glyphless-char");
28880 DEFSYM (Qhex_code, "hex-code");
28881 DEFSYM (Qempty_box, "empty-box");
28882 DEFSYM (Qthin_space, "thin-space");
28883 DEFSYM (Qzero_width, "zero-width");
28884
28885 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28886 /* Intern this now in case it isn't already done.
28887 Setting this variable twice is harmless.
28888 But don't staticpro it here--that is done in alloc.c. */
28889 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28890 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28891
28892 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28893 doc: /* Char-table defining glyphless characters.
28894 Each element, if non-nil, should be one of the following:
28895 an ASCII acronym string: display this string in a box
28896 `hex-code': display the hexadecimal code of a character in a box
28897 `empty-box': display as an empty box
28898 `thin-space': display as 1-pixel width space
28899 `zero-width': don't display
28900 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28901 display method for graphical terminals and text terminals respectively.
28902 GRAPHICAL and TEXT should each have one of the values listed above.
28903
28904 The char-table has one extra slot to control the display of a character for
28905 which no font is found. This slot only takes effect on graphical terminals.
28906 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28907 `thin-space'. The default is `empty-box'. */);
28908 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28909 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28910 Qempty_box);
28911 }
28912
28913
28914 /* Initialize this module when Emacs starts. */
28915
28916 void
28917 init_xdisp (void)
28918 {
28919 current_header_line_height = current_mode_line_height = -1;
28920
28921 CHARPOS (this_line_start_pos) = 0;
28922
28923 if (!noninteractive)
28924 {
28925 struct window *m = XWINDOW (minibuf_window);
28926 Lisp_Object frame = m->frame;
28927 struct frame *f = XFRAME (frame);
28928 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28929 struct window *r = XWINDOW (root);
28930 int i;
28931
28932 echo_area_window = minibuf_window;
28933
28934 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28935 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28936 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28937 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28938 XSETFASTINT (m->total_lines, 1);
28939 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28940
28941 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28942 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28943 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28944
28945 /* The default ellipsis glyphs `...'. */
28946 for (i = 0; i < 3; ++i)
28947 default_invis_vector[i] = make_number ('.');
28948 }
28949
28950 {
28951 /* Allocate the buffer for frame titles.
28952 Also used for `format-mode-line'. */
28953 int size = 100;
28954 mode_line_noprop_buf = (char *) xmalloc (size);
28955 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28956 mode_line_noprop_ptr = mode_line_noprop_buf;
28957 mode_line_target = MODE_LINE_DISPLAY;
28958 }
28959
28960 help_echo_showing_p = 0;
28961 }
28962
28963 /* Since w32 does not support atimers, it defines its own implementation of
28964 the following three functions in w32fns.c. */
28965 #ifndef WINDOWSNT
28966
28967 /* Platform-independent portion of hourglass implementation. */
28968
28969 /* Cancel a currently active hourglass timer, and start a new one. */
28970 void
28971 start_hourglass (void)
28972 {
28973 #if defined (HAVE_WINDOW_SYSTEM)
28974 EMACS_TIME delay;
28975 int secs, usecs = 0;
28976
28977 cancel_hourglass ();
28978
28979 if (INTEGERP (Vhourglass_delay)
28980 && XINT (Vhourglass_delay) > 0)
28981 secs = XFASTINT (Vhourglass_delay);
28982 else if (FLOATP (Vhourglass_delay)
28983 && XFLOAT_DATA (Vhourglass_delay) > 0)
28984 {
28985 Lisp_Object tem;
28986 tem = Ftruncate (Vhourglass_delay, Qnil);
28987 secs = XFASTINT (tem);
28988 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28989 }
28990 else
28991 secs = DEFAULT_HOURGLASS_DELAY;
28992
28993 EMACS_SET_SECS_USECS (delay, secs, usecs);
28994 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28995 show_hourglass, NULL);
28996 #endif
28997 }
28998
28999
29000 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29001 shown. */
29002 void
29003 cancel_hourglass (void)
29004 {
29005 #if defined (HAVE_WINDOW_SYSTEM)
29006 if (hourglass_atimer)
29007 {
29008 cancel_atimer (hourglass_atimer);
29009 hourglass_atimer = NULL;
29010 }
29011
29012 if (hourglass_shown_p)
29013 hide_hourglass ();
29014 #endif
29015 }
29016 #endif /* ! WINDOWSNT */