Merge from trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #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 is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
611
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
619
620 #if GLYPH_DEBUG
621
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
624
625 int trace_redisplay_p;
626
627 #endif /* GLYPH_DEBUG */
628
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
632
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
637
638 static Lisp_Object Qauto_hscroll_mode;
639
640 /* Buffer being redisplayed -- for redisplay_window_error. */
641
642 static struct buffer *displayed_buffer;
643
644 /* Value returned from text property handlers (see below). */
645
646 enum prop_handled
647 {
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
652 };
653
654 /* A description of text properties that redisplay is interested
655 in. */
656
657 struct props
658 {
659 /* The name of the property. */
660 Lisp_Object *name;
661
662 /* A unique index for the property. */
663 enum prop_idx idx;
664
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
668 };
669
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
676
677 /* Properties handled by iterators. */
678
679 static struct props it_props[] =
680 {
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
689 };
690
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
693
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
695
696 /* Enumeration returned by some move_it_.* functions internally. */
697
698 enum move_it_result
699 {
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
702
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
705
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
708
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
712
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
716
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
719 };
720
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
725
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
728
729 /* Similarly for the image cache. */
730
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
734
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
738
739 /* Non-zero while redisplay_internal is in progress. */
740
741 int redisplaying_p;
742
743 static Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 ptrdiff_t help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757 /* Platform-independent portion of hourglass implementation. */
758
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
761
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
765
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
768
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
771
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
781
782 \f
783 /* Function prototypes. */
784
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
793
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
795
796 static void handle_line_prefix (struct it *);
797
798 static void pint2str (char *, int, ptrdiff_t);
799 static void pint2hrstr (char *, int, ptrdiff_t);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 ptrdiff_t, ptrdiff_t);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, ptrdiff_t);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
815 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
818 static void pop_message (void);
819 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
820 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
821 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
824 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (ptrdiff_t);
840 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
866 ptrdiff_t *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, ptrdiff_t);
872 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, ptrdiff_t);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, ptrdiff_t);
905 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
906 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
907 static ptrdiff_t number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static ptrdiff_t next_overlay_change (ptrdiff_t);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, ptrdiff_t, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, ptrdiff_t, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
921
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
924
925 #ifdef HAVE_WINDOW_SYSTEM
926
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
944
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213 /* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215 static Lisp_Object
1216 string_from_display_spec (Lisp_Object spec)
1217 {
1218 if (CONSP (spec))
1219 {
1220 while (CONSP (spec))
1221 {
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1225 }
1226 }
1227 else if (VECTORP (spec))
1228 {
1229 ptrdiff_t i;
1230
1231 for (i = 0; i < ASIZE (spec); i++)
1232 {
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1235 }
1236 return Qnil;
1237 }
1238
1239 return spec;
1240 }
1241
1242 /* Return 1 if position CHARPOS is visible in window W.
1243 CHARPOS < 0 means return info about WINDOW_END position.
1244 If visible, set *X and *Y to pixel coordinates of top left corner.
1245 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1246 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1247
1248 int
1249 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1250 int *rtop, int *rbot, int *rowh, int *vpos)
1251 {
1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1254 struct text_pos top;
1255 int visible_p = 0;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1259 return visible_p;
1260
1261 if (XBUFFER (w->buffer) != current_buffer)
1262 {
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1265 }
1266
1267 SET_TEXT_POS_FROM_MARKER (top, w->start);
1268
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1279
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1283
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1292 {
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 enum it_method it_method = it.method;
1302 /* Calling line_bottom_y may change it.method, it.position, etc. */
1303 int bottom_y = (last_height = 0, line_bottom_y (&it));
1304 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1305
1306 if (top_y < window_top_y)
1307 visible_p = bottom_y > window_top_y;
1308 else if (top_y < it.last_visible_y)
1309 visible_p = 1;
1310 if (visible_p)
1311 {
1312 if (it_method == GET_FROM_DISPLAY_VECTOR)
1313 {
1314 /* We stopped on the last glyph of a display vector.
1315 Try and recompute. Hack alert! */
1316 if (charpos < 2 || top.charpos >= charpos)
1317 top_x = it.glyph_row->x;
1318 else
1319 {
1320 struct it it2;
1321 start_display (&it2, w, top);
1322 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1323 get_next_display_element (&it2);
1324 PRODUCE_GLYPHS (&it2);
1325 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1326 || it2.current_x > it2.last_visible_x)
1327 top_x = it.glyph_row->x;
1328 else
1329 {
1330 top_x = it2.current_x;
1331 top_y = it2.current_y;
1332 }
1333 }
1334 }
1335 else if (IT_CHARPOS (it) != charpos)
1336 {
1337 Lisp_Object cpos = make_number (charpos);
1338 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1339 Lisp_Object string = string_from_display_spec (spec);
1340 int newline_in_string = 0;
1341
1342 if (STRINGP (string))
1343 {
1344 const char *s = SSDATA (string);
1345 const char *e = s + SBYTES (string);
1346 while (s < e)
1347 {
1348 if (*s++ == '\n')
1349 {
1350 newline_in_string = 1;
1351 break;
1352 }
1353 }
1354 }
1355 /* The tricky code below is needed because there's a
1356 discrepancy between move_it_to and how we set cursor
1357 when the display line ends in a newline from a
1358 display string. move_it_to will stop _after_ such
1359 display strings, whereas set_cursor_from_row
1360 conspires with cursor_row_p to place the cursor on
1361 the first glyph produced from the display string. */
1362
1363 /* We have overshoot PT because it is covered by a
1364 display property whose value is a string. If the
1365 string includes embedded newlines, we are also in the
1366 wrong display line. Backtrack to the correct line,
1367 where the display string begins. */
1368 if (newline_in_string)
1369 {
1370 Lisp_Object startpos, endpos;
1371 EMACS_INT start, end;
1372 struct it it3;
1373
1374 /* Find the first and the last buffer positions
1375 covered by the display string. */
1376 endpos =
1377 Fnext_single_char_property_change (cpos, Qdisplay,
1378 Qnil, Qnil);
1379 startpos =
1380 Fprevious_single_char_property_change (endpos, Qdisplay,
1381 Qnil, Qnil);
1382 start = XFASTINT (startpos);
1383 end = XFASTINT (endpos);
1384 /* Move to the last buffer position before the
1385 display property. */
1386 start_display (&it3, w, top);
1387 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1388 /* Move forward one more line if the position before
1389 the display string is a newline or if it is the
1390 rightmost character on a line that is
1391 continued or word-wrapped. */
1392 if (it3.method == GET_FROM_BUFFER
1393 && it3.c == '\n')
1394 move_it_by_lines (&it3, 1);
1395 else if (move_it_in_display_line_to (&it3, -1,
1396 it3.current_x
1397 + it3.pixel_width,
1398 MOVE_TO_X)
1399 == MOVE_LINE_CONTINUED)
1400 {
1401 move_it_by_lines (&it3, 1);
1402 /* When we are under word-wrap, the #$@%!
1403 move_it_by_lines moves 2 lines, so we need to
1404 fix that up. */
1405 if (it3.line_wrap == WORD_WRAP)
1406 move_it_by_lines (&it3, -1);
1407 }
1408
1409 /* Record the vertical coordinate of the display
1410 line where we wound up. */
1411 top_y = it3.current_y;
1412 if (it3.bidi_p)
1413 {
1414 /* When characters are reordered for display,
1415 the character displayed to the left of the
1416 display string could be _after_ the display
1417 property in the logical order. Use the
1418 smallest vertical position of these two. */
1419 start_display (&it3, w, top);
1420 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1421 if (it3.current_y < top_y)
1422 top_y = it3.current_y;
1423 }
1424 /* Move from the top of the window to the beginning
1425 of the display line where the display string
1426 begins. */
1427 start_display (&it3, w, top);
1428 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1429 /* Finally, advance the iterator until we hit the
1430 first display element whose character position is
1431 CHARPOS, or until the first newline from the
1432 display string, which signals the end of the
1433 display line. */
1434 while (get_next_display_element (&it3))
1435 {
1436 PRODUCE_GLYPHS (&it3);
1437 if (IT_CHARPOS (it3) == charpos
1438 || ITERATOR_AT_END_OF_LINE_P (&it3))
1439 break;
1440 set_iterator_to_next (&it3, 0);
1441 }
1442 top_x = it3.current_x - it3.pixel_width;
1443 /* Normally, we would exit the above loop because we
1444 found the display element whose character
1445 position is CHARPOS. For the contingency that we
1446 didn't, and stopped at the first newline from the
1447 display string, move back over the glyphs
1448 produced from the string, until we find the
1449 rightmost glyph not from the string. */
1450 if (IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1451 {
1452 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1453 + it3.glyph_row->used[TEXT_AREA];
1454
1455 while (EQ ((g - 1)->object, string))
1456 {
1457 --g;
1458 top_x -= g->pixel_width;
1459 }
1460 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1461 + it3.glyph_row->used[TEXT_AREA]);
1462 }
1463 }
1464 }
1465
1466 *x = top_x;
1467 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1468 *rtop = max (0, window_top_y - top_y);
1469 *rbot = max (0, bottom_y - it.last_visible_y);
1470 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1471 - max (top_y, window_top_y)));
1472 *vpos = it.vpos;
1473 }
1474 }
1475 else
1476 {
1477 /* We were asked to provide info about WINDOW_END. */
1478 struct it it2;
1479 void *it2data = NULL;
1480
1481 SAVE_IT (it2, it, it2data);
1482 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1483 move_it_by_lines (&it, 1);
1484 if (charpos < IT_CHARPOS (it)
1485 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1486 {
1487 visible_p = 1;
1488 RESTORE_IT (&it2, &it2, it2data);
1489 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1490 *x = it2.current_x;
1491 *y = it2.current_y + it2.max_ascent - it2.ascent;
1492 *rtop = max (0, -it2.current_y);
1493 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1494 - it.last_visible_y));
1495 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1496 it.last_visible_y)
1497 - max (it2.current_y,
1498 WINDOW_HEADER_LINE_HEIGHT (w))));
1499 *vpos = it2.vpos;
1500 }
1501 else
1502 bidi_unshelve_cache (it2data, 1);
1503 }
1504 bidi_unshelve_cache (itdata, 0);
1505
1506 if (old_buffer)
1507 set_buffer_internal_1 (old_buffer);
1508
1509 current_header_line_height = current_mode_line_height = -1;
1510
1511 if (visible_p && XFASTINT (w->hscroll) > 0)
1512 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1513
1514 #if 0
1515 /* Debugging code. */
1516 if (visible_p)
1517 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1518 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1519 else
1520 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1521 #endif
1522
1523 return visible_p;
1524 }
1525
1526
1527 /* Return the next character from STR. Return in *LEN the length of
1528 the character. This is like STRING_CHAR_AND_LENGTH but never
1529 returns an invalid character. If we find one, we return a `?', but
1530 with the length of the invalid character. */
1531
1532 static inline int
1533 string_char_and_length (const unsigned char *str, int *len)
1534 {
1535 int c;
1536
1537 c = STRING_CHAR_AND_LENGTH (str, *len);
1538 if (!CHAR_VALID_P (c))
1539 /* We may not change the length here because other places in Emacs
1540 don't use this function, i.e. they silently accept invalid
1541 characters. */
1542 c = '?';
1543
1544 return c;
1545 }
1546
1547
1548
1549 /* Given a position POS containing a valid character and byte position
1550 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1551
1552 static struct text_pos
1553 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1554 {
1555 xassert (STRINGP (string) && nchars >= 0);
1556
1557 if (STRING_MULTIBYTE (string))
1558 {
1559 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1560 int len;
1561
1562 while (nchars--)
1563 {
1564 string_char_and_length (p, &len);
1565 p += len;
1566 CHARPOS (pos) += 1;
1567 BYTEPOS (pos) += len;
1568 }
1569 }
1570 else
1571 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1572
1573 return pos;
1574 }
1575
1576
1577 /* Value is the text position, i.e. character and byte position,
1578 for character position CHARPOS in STRING. */
1579
1580 static inline struct text_pos
1581 string_pos (ptrdiff_t charpos, Lisp_Object string)
1582 {
1583 struct text_pos pos;
1584 xassert (STRINGP (string));
1585 xassert (charpos >= 0);
1586 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1587 return pos;
1588 }
1589
1590
1591 /* Value is a text position, i.e. character and byte position, for
1592 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1593 means recognize multibyte characters. */
1594
1595 static struct text_pos
1596 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1597 {
1598 struct text_pos pos;
1599
1600 xassert (s != NULL);
1601 xassert (charpos >= 0);
1602
1603 if (multibyte_p)
1604 {
1605 int len;
1606
1607 SET_TEXT_POS (pos, 0, 0);
1608 while (charpos--)
1609 {
1610 string_char_and_length ((const unsigned char *) s, &len);
1611 s += len;
1612 CHARPOS (pos) += 1;
1613 BYTEPOS (pos) += len;
1614 }
1615 }
1616 else
1617 SET_TEXT_POS (pos, charpos, charpos);
1618
1619 return pos;
1620 }
1621
1622
1623 /* Value is the number of characters in C string S. MULTIBYTE_P
1624 non-zero means recognize multibyte characters. */
1625
1626 static ptrdiff_t
1627 number_of_chars (const char *s, int multibyte_p)
1628 {
1629 ptrdiff_t nchars;
1630
1631 if (multibyte_p)
1632 {
1633 ptrdiff_t rest = strlen (s);
1634 int len;
1635 const unsigned char *p = (const unsigned char *) s;
1636
1637 for (nchars = 0; rest > 0; ++nchars)
1638 {
1639 string_char_and_length (p, &len);
1640 rest -= len, p += len;
1641 }
1642 }
1643 else
1644 nchars = strlen (s);
1645
1646 return nchars;
1647 }
1648
1649
1650 /* Compute byte position NEWPOS->bytepos corresponding to
1651 NEWPOS->charpos. POS is a known position in string STRING.
1652 NEWPOS->charpos must be >= POS.charpos. */
1653
1654 static void
1655 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1656 {
1657 xassert (STRINGP (string));
1658 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1659
1660 if (STRING_MULTIBYTE (string))
1661 *newpos = string_pos_nchars_ahead (pos, string,
1662 CHARPOS (*newpos) - CHARPOS (pos));
1663 else
1664 BYTEPOS (*newpos) = CHARPOS (*newpos);
1665 }
1666
1667 /* EXPORT:
1668 Return an estimation of the pixel height of mode or header lines on
1669 frame F. FACE_ID specifies what line's height to estimate. */
1670
1671 int
1672 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1673 {
1674 #ifdef HAVE_WINDOW_SYSTEM
1675 if (FRAME_WINDOW_P (f))
1676 {
1677 int height = FONT_HEIGHT (FRAME_FONT (f));
1678
1679 /* This function is called so early when Emacs starts that the face
1680 cache and mode line face are not yet initialized. */
1681 if (FRAME_FACE_CACHE (f))
1682 {
1683 struct face *face = FACE_FROM_ID (f, face_id);
1684 if (face)
1685 {
1686 if (face->font)
1687 height = FONT_HEIGHT (face->font);
1688 if (face->box_line_width > 0)
1689 height += 2 * face->box_line_width;
1690 }
1691 }
1692
1693 return height;
1694 }
1695 #endif
1696
1697 return 1;
1698 }
1699
1700 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1701 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1702 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1703 not force the value into range. */
1704
1705 void
1706 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1707 int *x, int *y, NativeRectangle *bounds, int noclip)
1708 {
1709
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (f))
1712 {
1713 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1714 even for negative values. */
1715 if (pix_x < 0)
1716 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1717 if (pix_y < 0)
1718 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1719
1720 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1721 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1722
1723 if (bounds)
1724 STORE_NATIVE_RECT (*bounds,
1725 FRAME_COL_TO_PIXEL_X (f, pix_x),
1726 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1727 FRAME_COLUMN_WIDTH (f) - 1,
1728 FRAME_LINE_HEIGHT (f) - 1);
1729
1730 if (!noclip)
1731 {
1732 if (pix_x < 0)
1733 pix_x = 0;
1734 else if (pix_x > FRAME_TOTAL_COLS (f))
1735 pix_x = FRAME_TOTAL_COLS (f);
1736
1737 if (pix_y < 0)
1738 pix_y = 0;
1739 else if (pix_y > FRAME_LINES (f))
1740 pix_y = FRAME_LINES (f);
1741 }
1742 }
1743 #endif
1744
1745 *x = pix_x;
1746 *y = pix_y;
1747 }
1748
1749
1750 /* Find the glyph under window-relative coordinates X/Y in window W.
1751 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1752 strings. Return in *HPOS and *VPOS the row and column number of
1753 the glyph found. Return in *AREA the glyph area containing X.
1754 Value is a pointer to the glyph found or null if X/Y is not on
1755 text, or we can't tell because W's current matrix is not up to
1756 date. */
1757
1758 static
1759 struct glyph *
1760 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1761 int *dx, int *dy, int *area)
1762 {
1763 struct glyph *glyph, *end;
1764 struct glyph_row *row = NULL;
1765 int x0, i;
1766
1767 /* Find row containing Y. Give up if some row is not enabled. */
1768 for (i = 0; i < w->current_matrix->nrows; ++i)
1769 {
1770 row = MATRIX_ROW (w->current_matrix, i);
1771 if (!row->enabled_p)
1772 return NULL;
1773 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1774 break;
1775 }
1776
1777 *vpos = i;
1778 *hpos = 0;
1779
1780 /* Give up if Y is not in the window. */
1781 if (i == w->current_matrix->nrows)
1782 return NULL;
1783
1784 /* Get the glyph area containing X. */
1785 if (w->pseudo_window_p)
1786 {
1787 *area = TEXT_AREA;
1788 x0 = 0;
1789 }
1790 else
1791 {
1792 if (x < window_box_left_offset (w, TEXT_AREA))
1793 {
1794 *area = LEFT_MARGIN_AREA;
1795 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1796 }
1797 else if (x < window_box_right_offset (w, TEXT_AREA))
1798 {
1799 *area = TEXT_AREA;
1800 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1801 }
1802 else
1803 {
1804 *area = RIGHT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1806 }
1807 }
1808
1809 /* Find glyph containing X. */
1810 glyph = row->glyphs[*area];
1811 end = glyph + row->used[*area];
1812 x -= x0;
1813 while (glyph < end && x >= glyph->pixel_width)
1814 {
1815 x -= glyph->pixel_width;
1816 ++glyph;
1817 }
1818
1819 if (glyph == end)
1820 return NULL;
1821
1822 if (dx)
1823 {
1824 *dx = x;
1825 *dy = y - (row->y + row->ascent - glyph->ascent);
1826 }
1827
1828 *hpos = glyph - row->glyphs[*area];
1829 return glyph;
1830 }
1831
1832 /* Convert frame-relative x/y to coordinates relative to window W.
1833 Takes pseudo-windows into account. */
1834
1835 static void
1836 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1837 {
1838 if (w->pseudo_window_p)
1839 {
1840 /* A pseudo-window is always full-width, and starts at the
1841 left edge of the frame, plus a frame border. */
1842 struct frame *f = XFRAME (w->frame);
1843 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1844 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1845 }
1846 else
1847 {
1848 *x -= WINDOW_LEFT_EDGE_X (w);
1849 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1850 }
1851 }
1852
1853 #ifdef HAVE_WINDOW_SYSTEM
1854
1855 /* EXPORT:
1856 Return in RECTS[] at most N clipping rectangles for glyph string S.
1857 Return the number of stored rectangles. */
1858
1859 int
1860 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1861 {
1862 XRectangle r;
1863
1864 if (n <= 0)
1865 return 0;
1866
1867 if (s->row->full_width_p)
1868 {
1869 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1870 r.x = WINDOW_LEFT_EDGE_X (s->w);
1871 r.width = WINDOW_TOTAL_WIDTH (s->w);
1872
1873 /* Unless displaying a mode or menu bar line, which are always
1874 fully visible, clip to the visible part of the row. */
1875 if (s->w->pseudo_window_p)
1876 r.height = s->row->visible_height;
1877 else
1878 r.height = s->height;
1879 }
1880 else
1881 {
1882 /* This is a text line that may be partially visible. */
1883 r.x = window_box_left (s->w, s->area);
1884 r.width = window_box_width (s->w, s->area);
1885 r.height = s->row->visible_height;
1886 }
1887
1888 if (s->clip_head)
1889 if (r.x < s->clip_head->x)
1890 {
1891 if (r.width >= s->clip_head->x - r.x)
1892 r.width -= s->clip_head->x - r.x;
1893 else
1894 r.width = 0;
1895 r.x = s->clip_head->x;
1896 }
1897 if (s->clip_tail)
1898 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1899 {
1900 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1901 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1902 else
1903 r.width = 0;
1904 }
1905
1906 /* If S draws overlapping rows, it's sufficient to use the top and
1907 bottom of the window for clipping because this glyph string
1908 intentionally draws over other lines. */
1909 if (s->for_overlaps)
1910 {
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 r.height = window_text_bottom_y (s->w) - r.y;
1913
1914 /* Alas, the above simple strategy does not work for the
1915 environments with anti-aliased text: if the same text is
1916 drawn onto the same place multiple times, it gets thicker.
1917 If the overlap we are processing is for the erased cursor, we
1918 take the intersection with the rectangle of the cursor. */
1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1920 {
1921 XRectangle rc, r_save = r;
1922
1923 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1924 rc.y = s->w->phys_cursor.y;
1925 rc.width = s->w->phys_cursor_width;
1926 rc.height = s->w->phys_cursor_height;
1927
1928 x_intersect_rectangles (&r_save, &rc, &r);
1929 }
1930 }
1931 else
1932 {
1933 /* Don't use S->y for clipping because it doesn't take partially
1934 visible lines into account. For example, it can be negative for
1935 partially visible lines at the top of a window. */
1936 if (!s->row->full_width_p
1937 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 else
1940 r.y = max (0, s->row->y);
1941 }
1942
1943 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1944
1945 /* If drawing the cursor, don't let glyph draw outside its
1946 advertised boundaries. Cleartype does this under some circumstances. */
1947 if (s->hl == DRAW_CURSOR)
1948 {
1949 struct glyph *glyph = s->first_glyph;
1950 int height, max_y;
1951
1952 if (s->x > r.x)
1953 {
1954 r.width -= s->x - r.x;
1955 r.x = s->x;
1956 }
1957 r.width = min (r.width, glyph->pixel_width);
1958
1959 /* If r.y is below window bottom, ensure that we still see a cursor. */
1960 height = min (glyph->ascent + glyph->descent,
1961 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1962 max_y = window_text_bottom_y (s->w) - height;
1963 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1964 if (s->ybase - glyph->ascent > max_y)
1965 {
1966 r.y = max_y;
1967 r.height = height;
1968 }
1969 else
1970 {
1971 /* Don't draw cursor glyph taller than our actual glyph. */
1972 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1973 if (height < r.height)
1974 {
1975 max_y = r.y + r.height;
1976 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1977 r.height = min (max_y - r.y, height);
1978 }
1979 }
1980 }
1981
1982 if (s->row->clip)
1983 {
1984 XRectangle r_save = r;
1985
1986 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1987 r.width = 0;
1988 }
1989
1990 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1991 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1992 {
1993 #ifdef CONVERT_FROM_XRECT
1994 CONVERT_FROM_XRECT (r, *rects);
1995 #else
1996 *rects = r;
1997 #endif
1998 return 1;
1999 }
2000 else
2001 {
2002 /* If we are processing overlapping and allowed to return
2003 multiple clipping rectangles, we exclude the row of the glyph
2004 string from the clipping rectangle. This is to avoid drawing
2005 the same text on the environment with anti-aliasing. */
2006 #ifdef CONVERT_FROM_XRECT
2007 XRectangle rs[2];
2008 #else
2009 XRectangle *rs = rects;
2010 #endif
2011 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2012
2013 if (s->for_overlaps & OVERLAPS_PRED)
2014 {
2015 rs[i] = r;
2016 if (r.y + r.height > row_y)
2017 {
2018 if (r.y < row_y)
2019 rs[i].height = row_y - r.y;
2020 else
2021 rs[i].height = 0;
2022 }
2023 i++;
2024 }
2025 if (s->for_overlaps & OVERLAPS_SUCC)
2026 {
2027 rs[i] = r;
2028 if (r.y < row_y + s->row->visible_height)
2029 {
2030 if (r.y + r.height > row_y + s->row->visible_height)
2031 {
2032 rs[i].y = row_y + s->row->visible_height;
2033 rs[i].height = r.y + r.height - rs[i].y;
2034 }
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040
2041 n = i;
2042 #ifdef CONVERT_FROM_XRECT
2043 for (i = 0; i < n; i++)
2044 CONVERT_FROM_XRECT (rs[i], rects[i]);
2045 #endif
2046 return n;
2047 }
2048 }
2049
2050 /* EXPORT:
2051 Return in *NR the clipping rectangle for glyph string S. */
2052
2053 void
2054 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2055 {
2056 get_glyph_string_clip_rects (s, nr, 1);
2057 }
2058
2059
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2063 */
2064
2065 void
2066 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2067 struct glyph *glyph, int *xp, int *yp, int *heightp)
2068 {
2069 struct frame *f = XFRAME (WINDOW_FRAME (w));
2070 int x, y, wd, h, h0, y0;
2071
2072 /* Compute the width of the rectangle to draw. If on a stretch
2073 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2074 rectangle as wide as the glyph, but use a canonical character
2075 width instead. */
2076 wd = glyph->pixel_width - 1;
2077 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2078 wd++; /* Why? */
2079 #endif
2080
2081 x = w->phys_cursor.x;
2082 if (x < 0)
2083 {
2084 wd += x;
2085 x = 0;
2086 }
2087
2088 if (glyph->type == STRETCH_GLYPH
2089 && !x_stretch_cursor_p)
2090 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2091 w->phys_cursor_width = wd;
2092
2093 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2094
2095 /* If y is below window bottom, ensure that we still see a cursor. */
2096 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2097
2098 h = max (h0, glyph->ascent + glyph->descent);
2099 h0 = min (h0, glyph->ascent + glyph->descent);
2100
2101 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2102 if (y < y0)
2103 {
2104 h = max (h - (y0 - y) + 1, h0);
2105 y = y0 - 1;
2106 }
2107 else
2108 {
2109 y0 = window_text_bottom_y (w) - h0;
2110 if (y > y0)
2111 {
2112 h += y - y0;
2113 y = y0;
2114 }
2115 }
2116
2117 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2118 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2119 *heightp = h;
2120 }
2121
2122 /*
2123 * Remember which glyph the mouse is over.
2124 */
2125
2126 void
2127 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2128 {
2129 Lisp_Object window;
2130 struct window *w;
2131 struct glyph_row *r, *gr, *end_row;
2132 enum window_part part;
2133 enum glyph_row_area area;
2134 int x, y, width, height;
2135
2136 /* Try to determine frame pixel position and size of the glyph under
2137 frame pixel coordinates X/Y on frame F. */
2138
2139 if (!f->glyphs_initialized_p
2140 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2141 NILP (window)))
2142 {
2143 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2144 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2145 goto virtual_glyph;
2146 }
2147
2148 w = XWINDOW (window);
2149 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2150 height = WINDOW_FRAME_LINE_HEIGHT (w);
2151
2152 x = window_relative_x_coord (w, part, gx);
2153 y = gy - WINDOW_TOP_EDGE_Y (w);
2154
2155 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2156 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2157
2158 if (w->pseudo_window_p)
2159 {
2160 area = TEXT_AREA;
2161 part = ON_MODE_LINE; /* Don't adjust margin. */
2162 goto text_glyph;
2163 }
2164
2165 switch (part)
2166 {
2167 case ON_LEFT_MARGIN:
2168 area = LEFT_MARGIN_AREA;
2169 goto text_glyph;
2170
2171 case ON_RIGHT_MARGIN:
2172 area = RIGHT_MARGIN_AREA;
2173 goto text_glyph;
2174
2175 case ON_HEADER_LINE:
2176 case ON_MODE_LINE:
2177 gr = (part == ON_HEADER_LINE
2178 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2179 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2180 gy = gr->y;
2181 area = TEXT_AREA;
2182 goto text_glyph_row_found;
2183
2184 case ON_TEXT:
2185 area = TEXT_AREA;
2186
2187 text_glyph:
2188 gr = 0; gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2191 {
2192 gr = r; gy = r->y;
2193 break;
2194 }
2195
2196 text_glyph_row_found:
2197 if (gr && gy <= y)
2198 {
2199 struct glyph *g = gr->glyphs[area];
2200 struct glyph *end = g + gr->used[area];
2201
2202 height = gr->height;
2203 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2204 if (gx + g->pixel_width > x)
2205 break;
2206
2207 if (g < end)
2208 {
2209 if (g->type == IMAGE_GLYPH)
2210 {
2211 /* Don't remember when mouse is over image, as
2212 image may have hot-spots. */
2213 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2214 return;
2215 }
2216 width = g->pixel_width;
2217 }
2218 else
2219 {
2220 /* Use nominal char spacing at end of line. */
2221 x -= gx;
2222 gx += (x / width) * width;
2223 }
2224
2225 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2226 gx += window_box_left_offset (w, area);
2227 }
2228 else
2229 {
2230 /* Use nominal line height at end of window. */
2231 gx = (x / width) * width;
2232 y -= gy;
2233 gy += (y / height) * height;
2234 }
2235 break;
2236
2237 case ON_LEFT_FRINGE:
2238 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2239 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2240 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2241 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2242 goto row_glyph;
2243
2244 case ON_RIGHT_FRINGE:
2245 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2246 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2247 : window_box_right_offset (w, TEXT_AREA));
2248 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2249 goto row_glyph;
2250
2251 case ON_SCROLL_BAR:
2252 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2253 ? 0
2254 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2255 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2256 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2257 : 0)));
2258 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2259
2260 row_glyph:
2261 gr = 0, gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2264 {
2265 gr = r; gy = r->y;
2266 break;
2267 }
2268
2269 if (gr && gy <= y)
2270 height = gr->height;
2271 else
2272 {
2273 /* Use nominal line height at end of window. */
2274 y -= gy;
2275 gy += (y / height) * height;
2276 }
2277 break;
2278
2279 default:
2280 ;
2281 virtual_glyph:
2282 /* If there is no glyph under the mouse, then we divide the screen
2283 into a grid of the smallest glyph in the frame, and use that
2284 as our "glyph". */
2285
2286 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2287 round down even for negative values. */
2288 if (gx < 0)
2289 gx -= width - 1;
2290 if (gy < 0)
2291 gy -= height - 1;
2292
2293 gx = (gx / width) * width;
2294 gy = (gy / height) * height;
2295
2296 goto store_rect;
2297 }
2298
2299 gx += WINDOW_LEFT_EDGE_X (w);
2300 gy += WINDOW_TOP_EDGE_Y (w);
2301
2302 store_rect:
2303 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2304
2305 /* Visible feedback for debugging. */
2306 #if 0
2307 #if HAVE_X_WINDOWS
2308 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2309 f->output_data.x->normal_gc,
2310 gx, gy, width, height);
2311 #endif
2312 #endif
2313 }
2314
2315
2316 #endif /* HAVE_WINDOW_SYSTEM */
2317
2318 \f
2319 /***********************************************************************
2320 Lisp form evaluation
2321 ***********************************************************************/
2322
2323 /* Error handler for safe_eval and safe_call. */
2324
2325 static Lisp_Object
2326 safe_eval_handler (Lisp_Object arg)
2327 {
2328 add_to_log ("Error during redisplay: %S", arg, Qnil);
2329 return Qnil;
2330 }
2331
2332
2333 /* Evaluate SEXPR and return the result, or nil if something went
2334 wrong. Prevent redisplay during the evaluation. */
2335
2336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2337 Return the result, or nil if something went wrong. Prevent
2338 redisplay during the evaluation. */
2339
2340 Lisp_Object
2341 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2342 {
2343 Lisp_Object val;
2344
2345 if (inhibit_eval_during_redisplay)
2346 val = Qnil;
2347 else
2348 {
2349 ptrdiff_t count = SPECPDL_INDEX ();
2350 struct gcpro gcpro1;
2351
2352 GCPRO1 (args[0]);
2353 gcpro1.nvars = nargs;
2354 specbind (Qinhibit_redisplay, Qt);
2355 /* Use Qt to ensure debugger does not run,
2356 so there is no possibility of wanting to redisplay. */
2357 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2358 safe_eval_handler);
2359 UNGCPRO;
2360 val = unbind_to (count, val);
2361 }
2362
2363 return val;
2364 }
2365
2366
2367 /* Call function FN with one argument ARG.
2368 Return the result, or nil if something went wrong. */
2369
2370 Lisp_Object
2371 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2372 {
2373 Lisp_Object args[2];
2374 args[0] = fn;
2375 args[1] = arg;
2376 return safe_call (2, args);
2377 }
2378
2379 static Lisp_Object Qeval;
2380
2381 Lisp_Object
2382 safe_eval (Lisp_Object sexpr)
2383 {
2384 return safe_call1 (Qeval, sexpr);
2385 }
2386
2387 /* Call function FN with one argument ARG.
2388 Return the result, or nil if something went wrong. */
2389
2390 Lisp_Object
2391 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2392 {
2393 Lisp_Object args[3];
2394 args[0] = fn;
2395 args[1] = arg1;
2396 args[2] = arg2;
2397 return safe_call (3, args);
2398 }
2399
2400
2401 \f
2402 /***********************************************************************
2403 Debugging
2404 ***********************************************************************/
2405
2406 #if 0
2407
2408 /* Define CHECK_IT to perform sanity checks on iterators.
2409 This is for debugging. It is too slow to do unconditionally. */
2410
2411 static void
2412 check_it (struct it *it)
2413 {
2414 if (it->method == GET_FROM_STRING)
2415 {
2416 xassert (STRINGP (it->string));
2417 xassert (IT_STRING_CHARPOS (*it) >= 0);
2418 }
2419 else
2420 {
2421 xassert (IT_STRING_CHARPOS (*it) < 0);
2422 if (it->method == GET_FROM_BUFFER)
2423 {
2424 /* Check that character and byte positions agree. */
2425 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2426 }
2427 }
2428
2429 if (it->dpvec)
2430 xassert (it->current.dpvec_index >= 0);
2431 else
2432 xassert (it->current.dpvec_index < 0);
2433 }
2434
2435 #define CHECK_IT(IT) check_it ((IT))
2436
2437 #else /* not 0 */
2438
2439 #define CHECK_IT(IT) (void) 0
2440
2441 #endif /* not 0 */
2442
2443
2444 #if GLYPH_DEBUG && XASSERTS
2445
2446 /* Check that the window end of window W is what we expect it
2447 to be---the last row in the current matrix displaying text. */
2448
2449 static void
2450 check_window_end (struct window *w)
2451 {
2452 if (!MINI_WINDOW_P (w)
2453 && !NILP (w->window_end_valid))
2454 {
2455 struct glyph_row *row;
2456 xassert ((row = MATRIX_ROW (w->current_matrix,
2457 XFASTINT (w->window_end_vpos)),
2458 !row->enabled_p
2459 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2460 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2461 }
2462 }
2463
2464 #define CHECK_WINDOW_END(W) check_window_end ((W))
2465
2466 #else
2467
2468 #define CHECK_WINDOW_END(W) (void) 0
2469
2470 #endif
2471
2472
2473 \f
2474 /***********************************************************************
2475 Iterator initialization
2476 ***********************************************************************/
2477
2478 /* Initialize IT for displaying current_buffer in window W, starting
2479 at character position CHARPOS. CHARPOS < 0 means that no buffer
2480 position is specified which is useful when the iterator is assigned
2481 a position later. BYTEPOS is the byte position corresponding to
2482 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2483
2484 If ROW is not null, calls to produce_glyphs with IT as parameter
2485 will produce glyphs in that row.
2486
2487 BASE_FACE_ID is the id of a base face to use. It must be one of
2488 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2489 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2490 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2491
2492 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2493 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2494 will be initialized to use the corresponding mode line glyph row of
2495 the desired matrix of W. */
2496
2497 void
2498 init_iterator (struct it *it, struct window *w,
2499 ptrdiff_t charpos, ptrdiff_t bytepos,
2500 struct glyph_row *row, enum face_id base_face_id)
2501 {
2502 int highlight_region_p;
2503 enum face_id remapped_base_face_id = base_face_id;
2504
2505 /* Some precondition checks. */
2506 xassert (w != NULL && it != NULL);
2507 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2508 && charpos <= ZV));
2509
2510 /* If face attributes have been changed since the last redisplay,
2511 free realized faces now because they depend on face definitions
2512 that might have changed. Don't free faces while there might be
2513 desired matrices pending which reference these faces. */
2514 if (face_change_count && !inhibit_free_realized_faces)
2515 {
2516 face_change_count = 0;
2517 free_all_realized_faces (Qnil);
2518 }
2519
2520 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2521 if (! NILP (Vface_remapping_alist))
2522 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2523
2524 /* Use one of the mode line rows of W's desired matrix if
2525 appropriate. */
2526 if (row == NULL)
2527 {
2528 if (base_face_id == MODE_LINE_FACE_ID
2529 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2530 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2531 else if (base_face_id == HEADER_LINE_FACE_ID)
2532 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2533 }
2534
2535 /* Clear IT. */
2536 memset (it, 0, sizeof *it);
2537 it->current.overlay_string_index = -1;
2538 it->current.dpvec_index = -1;
2539 it->base_face_id = remapped_base_face_id;
2540 it->string = Qnil;
2541 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2542 it->paragraph_embedding = L2R;
2543 it->bidi_it.string.lstring = Qnil;
2544 it->bidi_it.string.s = NULL;
2545 it->bidi_it.string.bufpos = 0;
2546
2547 /* The window in which we iterate over current_buffer: */
2548 XSETWINDOW (it->window, w);
2549 it->w = w;
2550 it->f = XFRAME (w->frame);
2551
2552 it->cmp_it.id = -1;
2553
2554 /* Extra space between lines (on window systems only). */
2555 if (base_face_id == DEFAULT_FACE_ID
2556 && FRAME_WINDOW_P (it->f))
2557 {
2558 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2559 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2560 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2561 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2562 * FRAME_LINE_HEIGHT (it->f));
2563 else if (it->f->extra_line_spacing > 0)
2564 it->extra_line_spacing = it->f->extra_line_spacing;
2565 it->max_extra_line_spacing = 0;
2566 }
2567
2568 /* If realized faces have been removed, e.g. because of face
2569 attribute changes of named faces, recompute them. When running
2570 in batch mode, the face cache of the initial frame is null. If
2571 we happen to get called, make a dummy face cache. */
2572 if (FRAME_FACE_CACHE (it->f) == NULL)
2573 init_frame_faces (it->f);
2574 if (FRAME_FACE_CACHE (it->f)->used == 0)
2575 recompute_basic_faces (it->f);
2576
2577 /* Current value of the `slice', `space-width', and 'height' properties. */
2578 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2579 it->space_width = Qnil;
2580 it->font_height = Qnil;
2581 it->override_ascent = -1;
2582
2583 /* Are control characters displayed as `^C'? */
2584 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2585
2586 /* -1 means everything between a CR and the following line end
2587 is invisible. >0 means lines indented more than this value are
2588 invisible. */
2589 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2590 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2591 selective_display)),
2592 PTRDIFF_MAX)
2593 : (!NILP (BVAR (current_buffer, selective_display))
2594 ? -1 : 0));
2595 it->selective_display_ellipsis_p
2596 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2597
2598 /* Display table to use. */
2599 it->dp = window_display_table (w);
2600
2601 /* Are multibyte characters enabled in current_buffer? */
2602 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2603
2604 /* Non-zero if we should highlight the region. */
2605 highlight_region_p
2606 = (!NILP (Vtransient_mark_mode)
2607 && !NILP (BVAR (current_buffer, mark_active))
2608 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2609
2610 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2611 start and end of a visible region in window IT->w. Set both to
2612 -1 to indicate no region. */
2613 if (highlight_region_p
2614 /* Maybe highlight only in selected window. */
2615 && (/* Either show region everywhere. */
2616 highlight_nonselected_windows
2617 /* Or show region in the selected window. */
2618 || w == XWINDOW (selected_window)
2619 /* Or show the region if we are in the mini-buffer and W is
2620 the window the mini-buffer refers to. */
2621 || (MINI_WINDOW_P (XWINDOW (selected_window))
2622 && WINDOWP (minibuf_selected_window)
2623 && w == XWINDOW (minibuf_selected_window))))
2624 {
2625 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2626 it->region_beg_charpos = min (PT, markpos);
2627 it->region_end_charpos = max (PT, markpos);
2628 }
2629 else
2630 it->region_beg_charpos = it->region_end_charpos = -1;
2631
2632 /* Get the position at which the redisplay_end_trigger hook should
2633 be run, if it is to be run at all. */
2634 if (MARKERP (w->redisplay_end_trigger)
2635 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2636 it->redisplay_end_trigger_charpos
2637 = marker_position (w->redisplay_end_trigger);
2638 else if (INTEGERP (w->redisplay_end_trigger))
2639 it->redisplay_end_trigger_charpos =
2640 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2641
2642 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2643
2644 /* Are lines in the display truncated? */
2645 if (base_face_id != DEFAULT_FACE_ID
2646 || XINT (it->w->hscroll)
2647 || (! WINDOW_FULL_WIDTH_P (it->w)
2648 && ((!NILP (Vtruncate_partial_width_windows)
2649 && !INTEGERP (Vtruncate_partial_width_windows))
2650 || (INTEGERP (Vtruncate_partial_width_windows)
2651 && (WINDOW_TOTAL_COLS (it->w)
2652 < XINT (Vtruncate_partial_width_windows))))))
2653 it->line_wrap = TRUNCATE;
2654 else if (NILP (BVAR (current_buffer, truncate_lines)))
2655 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2656 ? WINDOW_WRAP : WORD_WRAP;
2657 else
2658 it->line_wrap = TRUNCATE;
2659
2660 /* Get dimensions of truncation and continuation glyphs. These are
2661 displayed as fringe bitmaps under X, so we don't need them for such
2662 frames. */
2663 if (!FRAME_WINDOW_P (it->f))
2664 {
2665 if (it->line_wrap == TRUNCATE)
2666 {
2667 /* We will need the truncation glyph. */
2668 xassert (it->glyph_row == NULL);
2669 produce_special_glyphs (it, IT_TRUNCATION);
2670 it->truncation_pixel_width = it->pixel_width;
2671 }
2672 else
2673 {
2674 /* We will need the continuation glyph. */
2675 xassert (it->glyph_row == NULL);
2676 produce_special_glyphs (it, IT_CONTINUATION);
2677 it->continuation_pixel_width = it->pixel_width;
2678 }
2679
2680 /* Reset these values to zero because the produce_special_glyphs
2681 above has changed them. */
2682 it->pixel_width = it->ascent = it->descent = 0;
2683 it->phys_ascent = it->phys_descent = 0;
2684 }
2685
2686 /* Set this after getting the dimensions of truncation and
2687 continuation glyphs, so that we don't produce glyphs when calling
2688 produce_special_glyphs, above. */
2689 it->glyph_row = row;
2690 it->area = TEXT_AREA;
2691
2692 /* Forget any previous info about this row being reversed. */
2693 if (it->glyph_row)
2694 it->glyph_row->reversed_p = 0;
2695
2696 /* Get the dimensions of the display area. The display area
2697 consists of the visible window area plus a horizontally scrolled
2698 part to the left of the window. All x-values are relative to the
2699 start of this total display area. */
2700 if (base_face_id != DEFAULT_FACE_ID)
2701 {
2702 /* Mode lines, menu bar in terminal frames. */
2703 it->first_visible_x = 0;
2704 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2705 }
2706 else
2707 {
2708 it->first_visible_x
2709 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2710 it->last_visible_x = (it->first_visible_x
2711 + window_box_width (w, TEXT_AREA));
2712
2713 /* If we truncate lines, leave room for the truncator glyph(s) at
2714 the right margin. Otherwise, leave room for the continuation
2715 glyph(s). Truncation and continuation glyphs are not inserted
2716 for window-based redisplay. */
2717 if (!FRAME_WINDOW_P (it->f))
2718 {
2719 if (it->line_wrap == TRUNCATE)
2720 it->last_visible_x -= it->truncation_pixel_width;
2721 else
2722 it->last_visible_x -= it->continuation_pixel_width;
2723 }
2724
2725 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2726 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2727 }
2728
2729 /* Leave room for a border glyph. */
2730 if (!FRAME_WINDOW_P (it->f)
2731 && !WINDOW_RIGHTMOST_P (it->w))
2732 it->last_visible_x -= 1;
2733
2734 it->last_visible_y = window_text_bottom_y (w);
2735
2736 /* For mode lines and alike, arrange for the first glyph having a
2737 left box line if the face specifies a box. */
2738 if (base_face_id != DEFAULT_FACE_ID)
2739 {
2740 struct face *face;
2741
2742 it->face_id = remapped_base_face_id;
2743
2744 /* If we have a boxed mode line, make the first character appear
2745 with a left box line. */
2746 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2747 if (face->box != FACE_NO_BOX)
2748 it->start_of_box_run_p = 1;
2749 }
2750
2751 /* If a buffer position was specified, set the iterator there,
2752 getting overlays and face properties from that position. */
2753 if (charpos >= BUF_BEG (current_buffer))
2754 {
2755 it->end_charpos = ZV;
2756 it->face_id = -1;
2757 IT_CHARPOS (*it) = charpos;
2758
2759 /* Compute byte position if not specified. */
2760 if (bytepos < charpos)
2761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2762 else
2763 IT_BYTEPOS (*it) = bytepos;
2764
2765 it->start = it->current;
2766 /* Do we need to reorder bidirectional text? Not if this is a
2767 unibyte buffer: by definition, none of the single-byte
2768 characters are strong R2L, so no reordering is needed. And
2769 bidi.c doesn't support unibyte buffers anyway. Also, don't
2770 reorder while we are loading loadup.el, since the tables of
2771 character properties needed for reordering are not yet
2772 available. */
2773 it->bidi_p =
2774 NILP (Vpurify_flag)
2775 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2776 && it->multibyte_p;
2777
2778 /* If we are to reorder bidirectional text, init the bidi
2779 iterator. */
2780 if (it->bidi_p)
2781 {
2782 /* Note the paragraph direction that this buffer wants to
2783 use. */
2784 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2785 Qleft_to_right))
2786 it->paragraph_embedding = L2R;
2787 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2788 Qright_to_left))
2789 it->paragraph_embedding = R2L;
2790 else
2791 it->paragraph_embedding = NEUTRAL_DIR;
2792 bidi_unshelve_cache (NULL, 0);
2793 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2794 &it->bidi_it);
2795 }
2796
2797 /* Compute faces etc. */
2798 reseat (it, it->current.pos, 1);
2799 }
2800
2801 CHECK_IT (it);
2802 }
2803
2804
2805 /* Initialize IT for the display of window W with window start POS. */
2806
2807 void
2808 start_display (struct it *it, struct window *w, struct text_pos pos)
2809 {
2810 struct glyph_row *row;
2811 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2812
2813 row = w->desired_matrix->rows + first_vpos;
2814 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2815 it->first_vpos = first_vpos;
2816
2817 /* Don't reseat to previous visible line start if current start
2818 position is in a string or image. */
2819 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2820 {
2821 int start_at_line_beg_p;
2822 int first_y = it->current_y;
2823
2824 /* If window start is not at a line start, skip forward to POS to
2825 get the correct continuation lines width. */
2826 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2827 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2828 if (!start_at_line_beg_p)
2829 {
2830 int new_x;
2831
2832 reseat_at_previous_visible_line_start (it);
2833 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2834
2835 new_x = it->current_x + it->pixel_width;
2836
2837 /* If lines are continued, this line may end in the middle
2838 of a multi-glyph character (e.g. a control character
2839 displayed as \003, or in the middle of an overlay
2840 string). In this case move_it_to above will not have
2841 taken us to the start of the continuation line but to the
2842 end of the continued line. */
2843 if (it->current_x > 0
2844 && it->line_wrap != TRUNCATE /* Lines are continued. */
2845 && (/* And glyph doesn't fit on the line. */
2846 new_x > it->last_visible_x
2847 /* Or it fits exactly and we're on a window
2848 system frame. */
2849 || (new_x == it->last_visible_x
2850 && FRAME_WINDOW_P (it->f))))
2851 {
2852 if ((it->current.dpvec_index >= 0
2853 || it->current.overlay_string_index >= 0)
2854 /* If we are on a newline from a display vector or
2855 overlay string, then we are already at the end of
2856 a screen line; no need to go to the next line in
2857 that case, as this line is not really continued.
2858 (If we do go to the next line, C-e will not DTRT.) */
2859 && it->c != '\n')
2860 {
2861 set_iterator_to_next (it, 1);
2862 move_it_in_display_line_to (it, -1, -1, 0);
2863 }
2864
2865 it->continuation_lines_width += it->current_x;
2866 }
2867 /* If the character at POS is displayed via a display
2868 vector, move_it_to above stops at the final glyph of
2869 IT->dpvec. To make the caller redisplay that character
2870 again (a.k.a. start at POS), we need to reset the
2871 dpvec_index to the beginning of IT->dpvec. */
2872 else if (it->current.dpvec_index >= 0)
2873 it->current.dpvec_index = 0;
2874
2875 /* We're starting a new display line, not affected by the
2876 height of the continued line, so clear the appropriate
2877 fields in the iterator structure. */
2878 it->max_ascent = it->max_descent = 0;
2879 it->max_phys_ascent = it->max_phys_descent = 0;
2880
2881 it->current_y = first_y;
2882 it->vpos = 0;
2883 it->current_x = it->hpos = 0;
2884 }
2885 }
2886 }
2887
2888
2889 /* Return 1 if POS is a position in ellipses displayed for invisible
2890 text. W is the window we display, for text property lookup. */
2891
2892 static int
2893 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2894 {
2895 Lisp_Object prop, window;
2896 int ellipses_p = 0;
2897 ptrdiff_t charpos = CHARPOS (pos->pos);
2898
2899 /* If POS specifies a position in a display vector, this might
2900 be for an ellipsis displayed for invisible text. We won't
2901 get the iterator set up for delivering that ellipsis unless
2902 we make sure that it gets aware of the invisible text. */
2903 if (pos->dpvec_index >= 0
2904 && pos->overlay_string_index < 0
2905 && CHARPOS (pos->string_pos) < 0
2906 && charpos > BEGV
2907 && (XSETWINDOW (window, w),
2908 prop = Fget_char_property (make_number (charpos),
2909 Qinvisible, window),
2910 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2911 {
2912 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2913 window);
2914 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2915 }
2916
2917 return ellipses_p;
2918 }
2919
2920
2921 /* Initialize IT for stepping through current_buffer in window W,
2922 starting at position POS that includes overlay string and display
2923 vector/ control character translation position information. Value
2924 is zero if there are overlay strings with newlines at POS. */
2925
2926 static int
2927 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2928 {
2929 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2930 int i, overlay_strings_with_newlines = 0;
2931
2932 /* If POS specifies a position in a display vector, this might
2933 be for an ellipsis displayed for invisible text. We won't
2934 get the iterator set up for delivering that ellipsis unless
2935 we make sure that it gets aware of the invisible text. */
2936 if (in_ellipses_for_invisible_text_p (pos, w))
2937 {
2938 --charpos;
2939 bytepos = 0;
2940 }
2941
2942 /* Keep in mind: the call to reseat in init_iterator skips invisible
2943 text, so we might end up at a position different from POS. This
2944 is only a problem when POS is a row start after a newline and an
2945 overlay starts there with an after-string, and the overlay has an
2946 invisible property. Since we don't skip invisible text in
2947 display_line and elsewhere immediately after consuming the
2948 newline before the row start, such a POS will not be in a string,
2949 but the call to init_iterator below will move us to the
2950 after-string. */
2951 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2952
2953 /* This only scans the current chunk -- it should scan all chunks.
2954 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2955 to 16 in 22.1 to make this a lesser problem. */
2956 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2957 {
2958 const char *s = SSDATA (it->overlay_strings[i]);
2959 const char *e = s + SBYTES (it->overlay_strings[i]);
2960
2961 while (s < e && *s != '\n')
2962 ++s;
2963
2964 if (s < e)
2965 {
2966 overlay_strings_with_newlines = 1;
2967 break;
2968 }
2969 }
2970
2971 /* If position is within an overlay string, set up IT to the right
2972 overlay string. */
2973 if (pos->overlay_string_index >= 0)
2974 {
2975 int relative_index;
2976
2977 /* If the first overlay string happens to have a `display'
2978 property for an image, the iterator will be set up for that
2979 image, and we have to undo that setup first before we can
2980 correct the overlay string index. */
2981 if (it->method == GET_FROM_IMAGE)
2982 pop_it (it);
2983
2984 /* We already have the first chunk of overlay strings in
2985 IT->overlay_strings. Load more until the one for
2986 pos->overlay_string_index is in IT->overlay_strings. */
2987 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2988 {
2989 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2990 it->current.overlay_string_index = 0;
2991 while (n--)
2992 {
2993 load_overlay_strings (it, 0);
2994 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2995 }
2996 }
2997
2998 it->current.overlay_string_index = pos->overlay_string_index;
2999 relative_index = (it->current.overlay_string_index
3000 % OVERLAY_STRING_CHUNK_SIZE);
3001 it->string = it->overlay_strings[relative_index];
3002 xassert (STRINGP (it->string));
3003 it->current.string_pos = pos->string_pos;
3004 it->method = GET_FROM_STRING;
3005 }
3006
3007 if (CHARPOS (pos->string_pos) >= 0)
3008 {
3009 /* Recorded position is not in an overlay string, but in another
3010 string. This can only be a string from a `display' property.
3011 IT should already be filled with that string. */
3012 it->current.string_pos = pos->string_pos;
3013 xassert (STRINGP (it->string));
3014 }
3015
3016 /* Restore position in display vector translations, control
3017 character translations or ellipses. */
3018 if (pos->dpvec_index >= 0)
3019 {
3020 if (it->dpvec == NULL)
3021 get_next_display_element (it);
3022 xassert (it->dpvec && it->current.dpvec_index == 0);
3023 it->current.dpvec_index = pos->dpvec_index;
3024 }
3025
3026 CHECK_IT (it);
3027 return !overlay_strings_with_newlines;
3028 }
3029
3030
3031 /* Initialize IT for stepping through current_buffer in window W
3032 starting at ROW->start. */
3033
3034 static void
3035 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3036 {
3037 init_from_display_pos (it, w, &row->start);
3038 it->start = row->start;
3039 it->continuation_lines_width = row->continuation_lines_width;
3040 CHECK_IT (it);
3041 }
3042
3043
3044 /* Initialize IT for stepping through current_buffer in window W
3045 starting in the line following ROW, i.e. starting at ROW->end.
3046 Value is zero if there are overlay strings with newlines at ROW's
3047 end position. */
3048
3049 static int
3050 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3051 {
3052 int success = 0;
3053
3054 if (init_from_display_pos (it, w, &row->end))
3055 {
3056 if (row->continued_p)
3057 it->continuation_lines_width
3058 = row->continuation_lines_width + row->pixel_width;
3059 CHECK_IT (it);
3060 success = 1;
3061 }
3062
3063 return success;
3064 }
3065
3066
3067
3068 \f
3069 /***********************************************************************
3070 Text properties
3071 ***********************************************************************/
3072
3073 /* Called when IT reaches IT->stop_charpos. Handle text property and
3074 overlay changes. Set IT->stop_charpos to the next position where
3075 to stop. */
3076
3077 static void
3078 handle_stop (struct it *it)
3079 {
3080 enum prop_handled handled;
3081 int handle_overlay_change_p;
3082 struct props *p;
3083
3084 it->dpvec = NULL;
3085 it->current.dpvec_index = -1;
3086 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3087 it->ignore_overlay_strings_at_pos_p = 0;
3088 it->ellipsis_p = 0;
3089
3090 /* Use face of preceding text for ellipsis (if invisible) */
3091 if (it->selective_display_ellipsis_p)
3092 it->saved_face_id = it->face_id;
3093
3094 do
3095 {
3096 handled = HANDLED_NORMALLY;
3097
3098 /* Call text property handlers. */
3099 for (p = it_props; p->handler; ++p)
3100 {
3101 handled = p->handler (it);
3102
3103 if (handled == HANDLED_RECOMPUTE_PROPS)
3104 break;
3105 else if (handled == HANDLED_RETURN)
3106 {
3107 /* We still want to show before and after strings from
3108 overlays even if the actual buffer text is replaced. */
3109 if (!handle_overlay_change_p
3110 || it->sp > 1
3111 || !get_overlay_strings_1 (it, 0, 0))
3112 {
3113 if (it->ellipsis_p)
3114 setup_for_ellipsis (it, 0);
3115 /* When handling a display spec, we might load an
3116 empty string. In that case, discard it here. We
3117 used to discard it in handle_single_display_spec,
3118 but that causes get_overlay_strings_1, above, to
3119 ignore overlay strings that we must check. */
3120 if (STRINGP (it->string) && !SCHARS (it->string))
3121 pop_it (it);
3122 return;
3123 }
3124 else if (STRINGP (it->string) && !SCHARS (it->string))
3125 pop_it (it);
3126 else
3127 {
3128 it->ignore_overlay_strings_at_pos_p = 1;
3129 it->string_from_display_prop_p = 0;
3130 it->from_disp_prop_p = 0;
3131 handle_overlay_change_p = 0;
3132 }
3133 handled = HANDLED_RECOMPUTE_PROPS;
3134 break;
3135 }
3136 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3137 handle_overlay_change_p = 0;
3138 }
3139
3140 if (handled != HANDLED_RECOMPUTE_PROPS)
3141 {
3142 /* Don't check for overlay strings below when set to deliver
3143 characters from a display vector. */
3144 if (it->method == GET_FROM_DISPLAY_VECTOR)
3145 handle_overlay_change_p = 0;
3146
3147 /* Handle overlay changes.
3148 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3149 if it finds overlays. */
3150 if (handle_overlay_change_p)
3151 handled = handle_overlay_change (it);
3152 }
3153
3154 if (it->ellipsis_p)
3155 {
3156 setup_for_ellipsis (it, 0);
3157 break;
3158 }
3159 }
3160 while (handled == HANDLED_RECOMPUTE_PROPS);
3161
3162 /* Determine where to stop next. */
3163 if (handled == HANDLED_NORMALLY)
3164 compute_stop_pos (it);
3165 }
3166
3167
3168 /* Compute IT->stop_charpos from text property and overlay change
3169 information for IT's current position. */
3170
3171 static void
3172 compute_stop_pos (struct it *it)
3173 {
3174 register INTERVAL iv, next_iv;
3175 Lisp_Object object, limit, position;
3176 ptrdiff_t charpos, bytepos;
3177
3178 if (STRINGP (it->string))
3179 {
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 it->stop_charpos = it->end_charpos;
3183 object = it->string;
3184 limit = Qnil;
3185 charpos = IT_STRING_CHARPOS (*it);
3186 bytepos = IT_STRING_BYTEPOS (*it);
3187 }
3188 else
3189 {
3190 ptrdiff_t pos;
3191
3192 /* If end_charpos is out of range for some reason, such as a
3193 misbehaving display function, rationalize it (Bug#5984). */
3194 if (it->end_charpos > ZV)
3195 it->end_charpos = ZV;
3196 it->stop_charpos = it->end_charpos;
3197
3198 /* If next overlay change is in front of the current stop pos
3199 (which is IT->end_charpos), stop there. Note: value of
3200 next_overlay_change is point-max if no overlay change
3201 follows. */
3202 charpos = IT_CHARPOS (*it);
3203 bytepos = IT_BYTEPOS (*it);
3204 pos = next_overlay_change (charpos);
3205 if (pos < it->stop_charpos)
3206 it->stop_charpos = pos;
3207
3208 /* If showing the region, we have to stop at the region
3209 start or end because the face might change there. */
3210 if (it->region_beg_charpos > 0)
3211 {
3212 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3213 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3214 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3216 }
3217
3218 /* Set up variables for computing the stop position from text
3219 property changes. */
3220 XSETBUFFER (object, current_buffer);
3221 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3222 }
3223
3224 /* Get the interval containing IT's position. Value is a null
3225 interval if there isn't such an interval. */
3226 position = make_number (charpos);
3227 iv = validate_interval_range (object, &position, &position, 0);
3228 if (!NULL_INTERVAL_P (iv))
3229 {
3230 Lisp_Object values_here[LAST_PROP_IDX];
3231 struct props *p;
3232
3233 /* Get properties here. */
3234 for (p = it_props; p->handler; ++p)
3235 values_here[p->idx] = textget (iv->plist, *p->name);
3236
3237 /* Look for an interval following iv that has different
3238 properties. */
3239 for (next_iv = next_interval (iv);
3240 (!NULL_INTERVAL_P (next_iv)
3241 && (NILP (limit)
3242 || XFASTINT (limit) > next_iv->position));
3243 next_iv = next_interval (next_iv))
3244 {
3245 for (p = it_props; p->handler; ++p)
3246 {
3247 Lisp_Object new_value;
3248
3249 new_value = textget (next_iv->plist, *p->name);
3250 if (!EQ (values_here[p->idx], new_value))
3251 break;
3252 }
3253
3254 if (p->handler)
3255 break;
3256 }
3257
3258 if (!NULL_INTERVAL_P (next_iv))
3259 {
3260 if (INTEGERP (limit)
3261 && next_iv->position >= XFASTINT (limit))
3262 /* No text property change up to limit. */
3263 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3264 else
3265 /* Text properties change in next_iv. */
3266 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3267 }
3268 }
3269
3270 if (it->cmp_it.id < 0)
3271 {
3272 ptrdiff_t stoppos = it->end_charpos;
3273
3274 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3275 stoppos = -1;
3276 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3277 stoppos, it->string);
3278 }
3279
3280 xassert (STRINGP (it->string)
3281 || (it->stop_charpos >= BEGV
3282 && it->stop_charpos >= IT_CHARPOS (*it)));
3283 }
3284
3285
3286 /* Return the position of the next overlay change after POS in
3287 current_buffer. Value is point-max if no overlay change
3288 follows. This is like `next-overlay-change' but doesn't use
3289 xmalloc. */
3290
3291 static ptrdiff_t
3292 next_overlay_change (ptrdiff_t pos)
3293 {
3294 ptrdiff_t i, noverlays;
3295 ptrdiff_t endpos;
3296 Lisp_Object *overlays;
3297
3298 /* Get all overlays at the given position. */
3299 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3300
3301 /* If any of these overlays ends before endpos,
3302 use its ending point instead. */
3303 for (i = 0; i < noverlays; ++i)
3304 {
3305 Lisp_Object oend;
3306 ptrdiff_t oendpos;
3307
3308 oend = OVERLAY_END (overlays[i]);
3309 oendpos = OVERLAY_POSITION (oend);
3310 endpos = min (endpos, oendpos);
3311 }
3312
3313 return endpos;
3314 }
3315
3316 /* How many characters forward to search for a display property or
3317 display string. Searching too far forward makes the bidi display
3318 sluggish, especially in small windows. */
3319 #define MAX_DISP_SCAN 250
3320
3321 /* Return the character position of a display string at or after
3322 position specified by POSITION. If no display string exists at or
3323 after POSITION, return ZV. A display string is either an overlay
3324 with `display' property whose value is a string, or a `display'
3325 text property whose value is a string. STRING is data about the
3326 string to iterate; if STRING->lstring is nil, we are iterating a
3327 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3328 on a GUI frame. DISP_PROP is set to zero if we searched
3329 MAX_DISP_SCAN characters forward without finding any display
3330 strings, non-zero otherwise. It is set to 2 if the display string
3331 uses any kind of `(space ...)' spec that will produce a stretch of
3332 white space in the text area. */
3333 ptrdiff_t
3334 compute_display_string_pos (struct text_pos *position,
3335 struct bidi_string_data *string,
3336 int frame_window_p, int *disp_prop)
3337 {
3338 /* OBJECT = nil means current buffer. */
3339 Lisp_Object object =
3340 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3341 Lisp_Object pos, spec, limpos;
3342 int string_p = (string && (STRINGP (string->lstring) || string->s));
3343 ptrdiff_t eob = string_p ? string->schars : ZV;
3344 ptrdiff_t begb = string_p ? 0 : BEGV;
3345 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3346 ptrdiff_t lim =
3347 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3348 struct text_pos tpos;
3349 int rv = 0;
3350
3351 *disp_prop = 1;
3352
3353 if (charpos >= eob
3354 /* We don't support display properties whose values are strings
3355 that have display string properties. */
3356 || string->from_disp_str
3357 /* C strings cannot have display properties. */
3358 || (string->s && !STRINGP (object)))
3359 {
3360 *disp_prop = 0;
3361 return eob;
3362 }
3363
3364 /* If the character at CHARPOS is where the display string begins,
3365 return CHARPOS. */
3366 pos = make_number (charpos);
3367 if (STRINGP (object))
3368 bufpos = string->bufpos;
3369 else
3370 bufpos = charpos;
3371 tpos = *position;
3372 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3373 && (charpos <= begb
3374 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3375 object),
3376 spec))
3377 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3378 frame_window_p)))
3379 {
3380 if (rv == 2)
3381 *disp_prop = 2;
3382 return charpos;
3383 }
3384
3385 /* Look forward for the first character with a `display' property
3386 that will replace the underlying text when displayed. */
3387 limpos = make_number (lim);
3388 do {
3389 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3390 CHARPOS (tpos) = XFASTINT (pos);
3391 if (CHARPOS (tpos) >= lim)
3392 {
3393 *disp_prop = 0;
3394 break;
3395 }
3396 if (STRINGP (object))
3397 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3398 else
3399 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3400 spec = Fget_char_property (pos, Qdisplay, object);
3401 if (!STRINGP (object))
3402 bufpos = CHARPOS (tpos);
3403 } while (NILP (spec)
3404 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3405 bufpos, frame_window_p)));
3406 if (rv == 2)
3407 *disp_prop = 2;
3408
3409 return CHARPOS (tpos);
3410 }
3411
3412 /* Return the character position of the end of the display string that
3413 started at CHARPOS. If there's no display string at CHARPOS,
3414 return -1. A display string is either an overlay with `display'
3415 property whose value is a string or a `display' text property whose
3416 value is a string. */
3417 ptrdiff_t
3418 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3419 {
3420 /* OBJECT = nil means current buffer. */
3421 Lisp_Object object =
3422 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3423 Lisp_Object pos = make_number (charpos);
3424 ptrdiff_t eob =
3425 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3426
3427 if (charpos >= eob || (string->s && !STRINGP (object)))
3428 return eob;
3429
3430 /* It could happen that the display property or overlay was removed
3431 since we found it in compute_display_string_pos above. One way
3432 this can happen is if JIT font-lock was called (through
3433 handle_fontified_prop), and jit-lock-functions remove text
3434 properties or overlays from the portion of buffer that includes
3435 CHARPOS. Muse mode is known to do that, for example. In this
3436 case, we return -1 to the caller, to signal that no display
3437 string is actually present at CHARPOS. See bidi_fetch_char for
3438 how this is handled.
3439
3440 An alternative would be to never look for display properties past
3441 it->stop_charpos. But neither compute_display_string_pos nor
3442 bidi_fetch_char that calls it know or care where the next
3443 stop_charpos is. */
3444 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3445 return -1;
3446
3447 /* Look forward for the first character where the `display' property
3448 changes. */
3449 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3450
3451 return XFASTINT (pos);
3452 }
3453
3454
3455 \f
3456 /***********************************************************************
3457 Fontification
3458 ***********************************************************************/
3459
3460 /* Handle changes in the `fontified' property of the current buffer by
3461 calling hook functions from Qfontification_functions to fontify
3462 regions of text. */
3463
3464 static enum prop_handled
3465 handle_fontified_prop (struct it *it)
3466 {
3467 Lisp_Object prop, pos;
3468 enum prop_handled handled = HANDLED_NORMALLY;
3469
3470 if (!NILP (Vmemory_full))
3471 return handled;
3472
3473 /* Get the value of the `fontified' property at IT's current buffer
3474 position. (The `fontified' property doesn't have a special
3475 meaning in strings.) If the value is nil, call functions from
3476 Qfontification_functions. */
3477 if (!STRINGP (it->string)
3478 && it->s == NULL
3479 && !NILP (Vfontification_functions)
3480 && !NILP (Vrun_hooks)
3481 && (pos = make_number (IT_CHARPOS (*it)),
3482 prop = Fget_char_property (pos, Qfontified, Qnil),
3483 /* Ignore the special cased nil value always present at EOB since
3484 no amount of fontifying will be able to change it. */
3485 NILP (prop) && IT_CHARPOS (*it) < Z))
3486 {
3487 ptrdiff_t count = SPECPDL_INDEX ();
3488 Lisp_Object val;
3489 struct buffer *obuf = current_buffer;
3490 int begv = BEGV, zv = ZV;
3491 int old_clip_changed = current_buffer->clip_changed;
3492
3493 val = Vfontification_functions;
3494 specbind (Qfontification_functions, Qnil);
3495
3496 xassert (it->end_charpos == ZV);
3497
3498 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3499 safe_call1 (val, pos);
3500 else
3501 {
3502 Lisp_Object fns, fn;
3503 struct gcpro gcpro1, gcpro2;
3504
3505 fns = Qnil;
3506 GCPRO2 (val, fns);
3507
3508 for (; CONSP (val); val = XCDR (val))
3509 {
3510 fn = XCAR (val);
3511
3512 if (EQ (fn, Qt))
3513 {
3514 /* A value of t indicates this hook has a local
3515 binding; it means to run the global binding too.
3516 In a global value, t should not occur. If it
3517 does, we must ignore it to avoid an endless
3518 loop. */
3519 for (fns = Fdefault_value (Qfontification_functions);
3520 CONSP (fns);
3521 fns = XCDR (fns))
3522 {
3523 fn = XCAR (fns);
3524 if (!EQ (fn, Qt))
3525 safe_call1 (fn, pos);
3526 }
3527 }
3528 else
3529 safe_call1 (fn, pos);
3530 }
3531
3532 UNGCPRO;
3533 }
3534
3535 unbind_to (count, Qnil);
3536
3537 /* Fontification functions routinely call `save-restriction'.
3538 Normally, this tags clip_changed, which can confuse redisplay
3539 (see discussion in Bug#6671). Since we don't perform any
3540 special handling of fontification changes in the case where
3541 `save-restriction' isn't called, there's no point doing so in
3542 this case either. So, if the buffer's restrictions are
3543 actually left unchanged, reset clip_changed. */
3544 if (obuf == current_buffer)
3545 {
3546 if (begv == BEGV && zv == ZV)
3547 current_buffer->clip_changed = old_clip_changed;
3548 }
3549 /* There isn't much we can reasonably do to protect against
3550 misbehaving fontification, but here's a fig leaf. */
3551 else if (!NILP (BVAR (obuf, name)))
3552 set_buffer_internal_1 (obuf);
3553
3554 /* The fontification code may have added/removed text.
3555 It could do even a lot worse, but let's at least protect against
3556 the most obvious case where only the text past `pos' gets changed',
3557 as is/was done in grep.el where some escapes sequences are turned
3558 into face properties (bug#7876). */
3559 it->end_charpos = ZV;
3560
3561 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3562 something. This avoids an endless loop if they failed to
3563 fontify the text for which reason ever. */
3564 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3565 handled = HANDLED_RECOMPUTE_PROPS;
3566 }
3567
3568 return handled;
3569 }
3570
3571
3572 \f
3573 /***********************************************************************
3574 Faces
3575 ***********************************************************************/
3576
3577 /* Set up iterator IT from face properties at its current position.
3578 Called from handle_stop. */
3579
3580 static enum prop_handled
3581 handle_face_prop (struct it *it)
3582 {
3583 int new_face_id;
3584 ptrdiff_t next_stop;
3585
3586 if (!STRINGP (it->string))
3587 {
3588 new_face_id
3589 = face_at_buffer_position (it->w,
3590 IT_CHARPOS (*it),
3591 it->region_beg_charpos,
3592 it->region_end_charpos,
3593 &next_stop,
3594 (IT_CHARPOS (*it)
3595 + TEXT_PROP_DISTANCE_LIMIT),
3596 0, it->base_face_id);
3597
3598 /* Is this a start of a run of characters with box face?
3599 Caveat: this can be called for a freshly initialized
3600 iterator; face_id is -1 in this case. We know that the new
3601 face will not change until limit, i.e. if the new face has a
3602 box, all characters up to limit will have one. But, as
3603 usual, we don't know whether limit is really the end. */
3604 if (new_face_id != it->face_id)
3605 {
3606 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3607
3608 /* If new face has a box but old face has not, this is
3609 the start of a run of characters with box, i.e. it has
3610 a shadow on the left side. The value of face_id of the
3611 iterator will be -1 if this is the initial call that gets
3612 the face. In this case, we have to look in front of IT's
3613 position and see whether there is a face != new_face_id. */
3614 it->start_of_box_run_p
3615 = (new_face->box != FACE_NO_BOX
3616 && (it->face_id >= 0
3617 || IT_CHARPOS (*it) == BEG
3618 || new_face_id != face_before_it_pos (it)));
3619 it->face_box_p = new_face->box != FACE_NO_BOX;
3620 }
3621 }
3622 else
3623 {
3624 int base_face_id;
3625 ptrdiff_t bufpos;
3626 int i;
3627 Lisp_Object from_overlay
3628 = (it->current.overlay_string_index >= 0
3629 ? it->string_overlays[it->current.overlay_string_index]
3630 : Qnil);
3631
3632 /* See if we got to this string directly or indirectly from
3633 an overlay property. That includes the before-string or
3634 after-string of an overlay, strings in display properties
3635 provided by an overlay, their text properties, etc.
3636
3637 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3638 if (! NILP (from_overlay))
3639 for (i = it->sp - 1; i >= 0; i--)
3640 {
3641 if (it->stack[i].current.overlay_string_index >= 0)
3642 from_overlay
3643 = it->string_overlays[it->stack[i].current.overlay_string_index];
3644 else if (! NILP (it->stack[i].from_overlay))
3645 from_overlay = it->stack[i].from_overlay;
3646
3647 if (!NILP (from_overlay))
3648 break;
3649 }
3650
3651 if (! NILP (from_overlay))
3652 {
3653 bufpos = IT_CHARPOS (*it);
3654 /* For a string from an overlay, the base face depends
3655 only on text properties and ignores overlays. */
3656 base_face_id
3657 = face_for_overlay_string (it->w,
3658 IT_CHARPOS (*it),
3659 it->region_beg_charpos,
3660 it->region_end_charpos,
3661 &next_stop,
3662 (IT_CHARPOS (*it)
3663 + TEXT_PROP_DISTANCE_LIMIT),
3664 0,
3665 from_overlay);
3666 }
3667 else
3668 {
3669 bufpos = 0;
3670
3671 /* For strings from a `display' property, use the face at
3672 IT's current buffer position as the base face to merge
3673 with, so that overlay strings appear in the same face as
3674 surrounding text, unless they specify their own
3675 faces. */
3676 base_face_id = underlying_face_id (it);
3677 }
3678
3679 new_face_id = face_at_string_position (it->w,
3680 it->string,
3681 IT_STRING_CHARPOS (*it),
3682 bufpos,
3683 it->region_beg_charpos,
3684 it->region_end_charpos,
3685 &next_stop,
3686 base_face_id, 0);
3687
3688 /* Is this a start of a run of characters with box? Caveat:
3689 this can be called for a freshly allocated iterator; face_id
3690 is -1 is this case. We know that the new face will not
3691 change until the next check pos, i.e. if the new face has a
3692 box, all characters up to that position will have a
3693 box. But, as usual, we don't know whether that position
3694 is really the end. */
3695 if (new_face_id != it->face_id)
3696 {
3697 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3698 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3699
3700 /* If new face has a box but old face hasn't, this is the
3701 start of a run of characters with box, i.e. it has a
3702 shadow on the left side. */
3703 it->start_of_box_run_p
3704 = new_face->box && (old_face == NULL || !old_face->box);
3705 it->face_box_p = new_face->box != FACE_NO_BOX;
3706 }
3707 }
3708
3709 it->face_id = new_face_id;
3710 return HANDLED_NORMALLY;
3711 }
3712
3713
3714 /* Return the ID of the face ``underlying'' IT's current position,
3715 which is in a string. If the iterator is associated with a
3716 buffer, return the face at IT's current buffer position.
3717 Otherwise, use the iterator's base_face_id. */
3718
3719 static int
3720 underlying_face_id (struct it *it)
3721 {
3722 int face_id = it->base_face_id, i;
3723
3724 xassert (STRINGP (it->string));
3725
3726 for (i = it->sp - 1; i >= 0; --i)
3727 if (NILP (it->stack[i].string))
3728 face_id = it->stack[i].face_id;
3729
3730 return face_id;
3731 }
3732
3733
3734 /* Compute the face one character before or after the current position
3735 of IT, in the visual order. BEFORE_P non-zero means get the face
3736 in front (to the left in L2R paragraphs, to the right in R2L
3737 paragraphs) of IT's screen position. Value is the ID of the face. */
3738
3739 static int
3740 face_before_or_after_it_pos (struct it *it, int before_p)
3741 {
3742 int face_id, limit;
3743 ptrdiff_t next_check_charpos;
3744 struct it it_copy;
3745 void *it_copy_data = NULL;
3746
3747 xassert (it->s == NULL);
3748
3749 if (STRINGP (it->string))
3750 {
3751 ptrdiff_t bufpos, charpos;
3752 int base_face_id;
3753
3754 /* No face change past the end of the string (for the case
3755 we are padding with spaces). No face change before the
3756 string start. */
3757 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3758 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3759 return it->face_id;
3760
3761 if (!it->bidi_p)
3762 {
3763 /* Set charpos to the position before or after IT's current
3764 position, in the logical order, which in the non-bidi
3765 case is the same as the visual order. */
3766 if (before_p)
3767 charpos = IT_STRING_CHARPOS (*it) - 1;
3768 else if (it->what == IT_COMPOSITION)
3769 /* For composition, we must check the character after the
3770 composition. */
3771 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3772 else
3773 charpos = IT_STRING_CHARPOS (*it) + 1;
3774 }
3775 else
3776 {
3777 if (before_p)
3778 {
3779 /* With bidi iteration, the character before the current
3780 in the visual order cannot be found by simple
3781 iteration, because "reverse" reordering is not
3782 supported. Instead, we need to use the move_it_*
3783 family of functions. */
3784 /* Ignore face changes before the first visible
3785 character on this display line. */
3786 if (it->current_x <= it->first_visible_x)
3787 return it->face_id;
3788 SAVE_IT (it_copy, *it, it_copy_data);
3789 /* Implementation note: Since move_it_in_display_line
3790 works in the iterator geometry, and thinks the first
3791 character is always the leftmost, even in R2L lines,
3792 we don't need to distinguish between the R2L and L2R
3793 cases here. */
3794 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3795 it_copy.current_x - 1, MOVE_TO_X);
3796 charpos = IT_STRING_CHARPOS (it_copy);
3797 RESTORE_IT (it, it, it_copy_data);
3798 }
3799 else
3800 {
3801 /* Set charpos to the string position of the character
3802 that comes after IT's current position in the visual
3803 order. */
3804 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3805
3806 it_copy = *it;
3807 while (n--)
3808 bidi_move_to_visually_next (&it_copy.bidi_it);
3809
3810 charpos = it_copy.bidi_it.charpos;
3811 }
3812 }
3813 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3814
3815 if (it->current.overlay_string_index >= 0)
3816 bufpos = IT_CHARPOS (*it);
3817 else
3818 bufpos = 0;
3819
3820 base_face_id = underlying_face_id (it);
3821
3822 /* Get the face for ASCII, or unibyte. */
3823 face_id = face_at_string_position (it->w,
3824 it->string,
3825 charpos,
3826 bufpos,
3827 it->region_beg_charpos,
3828 it->region_end_charpos,
3829 &next_check_charpos,
3830 base_face_id, 0);
3831
3832 /* Correct the face for charsets different from ASCII. Do it
3833 for the multibyte case only. The face returned above is
3834 suitable for unibyte text if IT->string is unibyte. */
3835 if (STRING_MULTIBYTE (it->string))
3836 {
3837 struct text_pos pos1 = string_pos (charpos, it->string);
3838 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3839 int c, len;
3840 struct face *face = FACE_FROM_ID (it->f, face_id);
3841
3842 c = string_char_and_length (p, &len);
3843 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3844 }
3845 }
3846 else
3847 {
3848 struct text_pos pos;
3849
3850 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3851 || (IT_CHARPOS (*it) <= BEGV && before_p))
3852 return it->face_id;
3853
3854 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3855 pos = it->current.pos;
3856
3857 if (!it->bidi_p)
3858 {
3859 if (before_p)
3860 DEC_TEXT_POS (pos, it->multibyte_p);
3861 else
3862 {
3863 if (it->what == IT_COMPOSITION)
3864 {
3865 /* For composition, we must check the position after
3866 the composition. */
3867 pos.charpos += it->cmp_it.nchars;
3868 pos.bytepos += it->len;
3869 }
3870 else
3871 INC_TEXT_POS (pos, it->multibyte_p);
3872 }
3873 }
3874 else
3875 {
3876 if (before_p)
3877 {
3878 /* With bidi iteration, the character before the current
3879 in the visual order cannot be found by simple
3880 iteration, because "reverse" reordering is not
3881 supported. Instead, we need to use the move_it_*
3882 family of functions. */
3883 /* Ignore face changes before the first visible
3884 character on this display line. */
3885 if (it->current_x <= it->first_visible_x)
3886 return it->face_id;
3887 SAVE_IT (it_copy, *it, it_copy_data);
3888 /* Implementation note: Since move_it_in_display_line
3889 works in the iterator geometry, and thinks the first
3890 character is always the leftmost, even in R2L lines,
3891 we don't need to distinguish between the R2L and L2R
3892 cases here. */
3893 move_it_in_display_line (&it_copy, ZV,
3894 it_copy.current_x - 1, MOVE_TO_X);
3895 pos = it_copy.current.pos;
3896 RESTORE_IT (it, it, it_copy_data);
3897 }
3898 else
3899 {
3900 /* Set charpos to the buffer position of the character
3901 that comes after IT's current position in the visual
3902 order. */
3903 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3904
3905 it_copy = *it;
3906 while (n--)
3907 bidi_move_to_visually_next (&it_copy.bidi_it);
3908
3909 SET_TEXT_POS (pos,
3910 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3911 }
3912 }
3913 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3914
3915 /* Determine face for CHARSET_ASCII, or unibyte. */
3916 face_id = face_at_buffer_position (it->w,
3917 CHARPOS (pos),
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 limit, 0, -1);
3922
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if current_buffer is unibyte. */
3926 if (it->multibyte_p)
3927 {
3928 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3929 struct face *face = FACE_FROM_ID (it->f, face_id);
3930 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3931 }
3932 }
3933
3934 return face_id;
3935 }
3936
3937
3938 \f
3939 /***********************************************************************
3940 Invisible text
3941 ***********************************************************************/
3942
3943 /* Set up iterator IT from invisible properties at its current
3944 position. Called from handle_stop. */
3945
3946 static enum prop_handled
3947 handle_invisible_prop (struct it *it)
3948 {
3949 enum prop_handled handled = HANDLED_NORMALLY;
3950
3951 if (STRINGP (it->string))
3952 {
3953 Lisp_Object prop, end_charpos, limit, charpos;
3954
3955 /* Get the value of the invisible text property at the
3956 current position. Value will be nil if there is no such
3957 property. */
3958 charpos = make_number (IT_STRING_CHARPOS (*it));
3959 prop = Fget_text_property (charpos, Qinvisible, it->string);
3960
3961 if (!NILP (prop)
3962 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3963 {
3964 ptrdiff_t endpos;
3965
3966 handled = HANDLED_RECOMPUTE_PROPS;
3967
3968 /* Get the position at which the next change of the
3969 invisible text property can be found in IT->string.
3970 Value will be nil if the property value is the same for
3971 all the rest of IT->string. */
3972 XSETINT (limit, SCHARS (it->string));
3973 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3974 it->string, limit);
3975
3976 /* Text at current position is invisible. The next
3977 change in the property is at position end_charpos.
3978 Move IT's current position to that position. */
3979 if (INTEGERP (end_charpos)
3980 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3981 {
3982 struct text_pos old;
3983 ptrdiff_t oldpos;
3984
3985 old = it->current.string_pos;
3986 oldpos = CHARPOS (old);
3987 if (it->bidi_p)
3988 {
3989 if (it->bidi_it.first_elt
3990 && it->bidi_it.charpos < SCHARS (it->string))
3991 bidi_paragraph_init (it->paragraph_embedding,
3992 &it->bidi_it, 1);
3993 /* Bidi-iterate out of the invisible text. */
3994 do
3995 {
3996 bidi_move_to_visually_next (&it->bidi_it);
3997 }
3998 while (oldpos <= it->bidi_it.charpos
3999 && it->bidi_it.charpos < endpos);
4000
4001 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4003 if (IT_CHARPOS (*it) >= endpos)
4004 it->prev_stop = endpos;
4005 }
4006 else
4007 {
4008 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4009 compute_string_pos (&it->current.string_pos, old, it->string);
4010 }
4011 }
4012 else
4013 {
4014 /* The rest of the string is invisible. If this is an
4015 overlay string, proceed with the next overlay string
4016 or whatever comes and return a character from there. */
4017 if (it->current.overlay_string_index >= 0)
4018 {
4019 next_overlay_string (it);
4020 /* Don't check for overlay strings when we just
4021 finished processing them. */
4022 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4023 }
4024 else
4025 {
4026 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4027 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4028 }
4029 }
4030 }
4031 }
4032 else
4033 {
4034 int invis_p;
4035 ptrdiff_t newpos, next_stop, start_charpos, tem;
4036 Lisp_Object pos, prop, overlay;
4037
4038 /* First of all, is there invisible text at this position? */
4039 tem = start_charpos = IT_CHARPOS (*it);
4040 pos = make_number (tem);
4041 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4042 &overlay);
4043 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4044
4045 /* If we are on invisible text, skip over it. */
4046 if (invis_p && start_charpos < it->end_charpos)
4047 {
4048 /* Record whether we have to display an ellipsis for the
4049 invisible text. */
4050 int display_ellipsis_p = invis_p == 2;
4051
4052 handled = HANDLED_RECOMPUTE_PROPS;
4053
4054 /* Loop skipping over invisible text. The loop is left at
4055 ZV or with IT on the first char being visible again. */
4056 do
4057 {
4058 /* Try to skip some invisible text. Return value is the
4059 position reached which can be equal to where we start
4060 if there is nothing invisible there. This skips both
4061 over invisible text properties and overlays with
4062 invisible property. */
4063 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4064
4065 /* If we skipped nothing at all we weren't at invisible
4066 text in the first place. If everything to the end of
4067 the buffer was skipped, end the loop. */
4068 if (newpos == tem || newpos >= ZV)
4069 invis_p = 0;
4070 else
4071 {
4072 /* We skipped some characters but not necessarily
4073 all there are. Check if we ended up on visible
4074 text. Fget_char_property returns the property of
4075 the char before the given position, i.e. if we
4076 get invis_p = 0, this means that the char at
4077 newpos is visible. */
4078 pos = make_number (newpos);
4079 prop = Fget_char_property (pos, Qinvisible, it->window);
4080 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4081 }
4082
4083 /* If we ended up on invisible text, proceed to
4084 skip starting with next_stop. */
4085 if (invis_p)
4086 tem = next_stop;
4087
4088 /* If there are adjacent invisible texts, don't lose the
4089 second one's ellipsis. */
4090 if (invis_p == 2)
4091 display_ellipsis_p = 1;
4092 }
4093 while (invis_p);
4094
4095 /* The position newpos is now either ZV or on visible text. */
4096 if (it->bidi_p)
4097 {
4098 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4099 int on_newline =
4100 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4101 int after_newline =
4102 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4103
4104 /* If the invisible text ends on a newline or on a
4105 character after a newline, we can avoid the costly,
4106 character by character, bidi iteration to NEWPOS, and
4107 instead simply reseat the iterator there. That's
4108 because all bidi reordering information is tossed at
4109 the newline. This is a big win for modes that hide
4110 complete lines, like Outline, Org, etc. */
4111 if (on_newline || after_newline)
4112 {
4113 struct text_pos tpos;
4114 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4115
4116 SET_TEXT_POS (tpos, newpos, bpos);
4117 reseat_1 (it, tpos, 0);
4118 /* If we reseat on a newline/ZV, we need to prep the
4119 bidi iterator for advancing to the next character
4120 after the newline/EOB, keeping the current paragraph
4121 direction (so that PRODUCE_GLYPHS does TRT wrt
4122 prepending/appending glyphs to a glyph row). */
4123 if (on_newline)
4124 {
4125 it->bidi_it.first_elt = 0;
4126 it->bidi_it.paragraph_dir = pdir;
4127 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4128 it->bidi_it.nchars = 1;
4129 it->bidi_it.ch_len = 1;
4130 }
4131 }
4132 else /* Must use the slow method. */
4133 {
4134 /* With bidi iteration, the region of invisible text
4135 could start and/or end in the middle of a
4136 non-base embedding level. Therefore, we need to
4137 skip invisible text using the bidi iterator,
4138 starting at IT's current position, until we find
4139 ourselves outside of the invisible text.
4140 Skipping invisible text _after_ bidi iteration
4141 avoids affecting the visual order of the
4142 displayed text when invisible properties are
4143 added or removed. */
4144 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4145 {
4146 /* If we were `reseat'ed to a new paragraph,
4147 determine the paragraph base direction. We
4148 need to do it now because
4149 next_element_from_buffer may not have a
4150 chance to do it, if we are going to skip any
4151 text at the beginning, which resets the
4152 FIRST_ELT flag. */
4153 bidi_paragraph_init (it->paragraph_embedding,
4154 &it->bidi_it, 1);
4155 }
4156 do
4157 {
4158 bidi_move_to_visually_next (&it->bidi_it);
4159 }
4160 while (it->stop_charpos <= it->bidi_it.charpos
4161 && it->bidi_it.charpos < newpos);
4162 IT_CHARPOS (*it) = it->bidi_it.charpos;
4163 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4164 /* If we overstepped NEWPOS, record its position in
4165 the iterator, so that we skip invisible text if
4166 later the bidi iteration lands us in the
4167 invisible region again. */
4168 if (IT_CHARPOS (*it) >= newpos)
4169 it->prev_stop = newpos;
4170 }
4171 }
4172 else
4173 {
4174 IT_CHARPOS (*it) = newpos;
4175 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4176 }
4177
4178 /* If there are before-strings at the start of invisible
4179 text, and the text is invisible because of a text
4180 property, arrange to show before-strings because 20.x did
4181 it that way. (If the text is invisible because of an
4182 overlay property instead of a text property, this is
4183 already handled in the overlay code.) */
4184 if (NILP (overlay)
4185 && get_overlay_strings (it, it->stop_charpos))
4186 {
4187 handled = HANDLED_RECOMPUTE_PROPS;
4188 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4189 }
4190 else if (display_ellipsis_p)
4191 {
4192 /* Make sure that the glyphs of the ellipsis will get
4193 correct `charpos' values. If we would not update
4194 it->position here, the glyphs would belong to the
4195 last visible character _before_ the invisible
4196 text, which confuses `set_cursor_from_row'.
4197
4198 We use the last invisible position instead of the
4199 first because this way the cursor is always drawn on
4200 the first "." of the ellipsis, whenever PT is inside
4201 the invisible text. Otherwise the cursor would be
4202 placed _after_ the ellipsis when the point is after the
4203 first invisible character. */
4204 if (!STRINGP (it->object))
4205 {
4206 it->position.charpos = newpos - 1;
4207 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4208 }
4209 it->ellipsis_p = 1;
4210 /* Let the ellipsis display before
4211 considering any properties of the following char.
4212 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4213 handled = HANDLED_RETURN;
4214 }
4215 }
4216 }
4217
4218 return handled;
4219 }
4220
4221
4222 /* Make iterator IT return `...' next.
4223 Replaces LEN characters from buffer. */
4224
4225 static void
4226 setup_for_ellipsis (struct it *it, int len)
4227 {
4228 /* Use the display table definition for `...'. Invalid glyphs
4229 will be handled by the method returning elements from dpvec. */
4230 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4231 {
4232 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4233 it->dpvec = v->contents;
4234 it->dpend = v->contents + v->header.size;
4235 }
4236 else
4237 {
4238 /* Default `...'. */
4239 it->dpvec = default_invis_vector;
4240 it->dpend = default_invis_vector + 3;
4241 }
4242
4243 it->dpvec_char_len = len;
4244 it->current.dpvec_index = 0;
4245 it->dpvec_face_id = -1;
4246
4247 /* Remember the current face id in case glyphs specify faces.
4248 IT's face is restored in set_iterator_to_next.
4249 saved_face_id was set to preceding char's face in handle_stop. */
4250 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4251 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4252
4253 it->method = GET_FROM_DISPLAY_VECTOR;
4254 it->ellipsis_p = 1;
4255 }
4256
4257
4258 \f
4259 /***********************************************************************
4260 'display' property
4261 ***********************************************************************/
4262
4263 /* Set up iterator IT from `display' property at its current position.
4264 Called from handle_stop.
4265 We return HANDLED_RETURN if some part of the display property
4266 overrides the display of the buffer text itself.
4267 Otherwise we return HANDLED_NORMALLY. */
4268
4269 static enum prop_handled
4270 handle_display_prop (struct it *it)
4271 {
4272 Lisp_Object propval, object, overlay;
4273 struct text_pos *position;
4274 ptrdiff_t bufpos;
4275 /* Nonzero if some property replaces the display of the text itself. */
4276 int display_replaced_p = 0;
4277
4278 if (STRINGP (it->string))
4279 {
4280 object = it->string;
4281 position = &it->current.string_pos;
4282 bufpos = CHARPOS (it->current.pos);
4283 }
4284 else
4285 {
4286 XSETWINDOW (object, it->w);
4287 position = &it->current.pos;
4288 bufpos = CHARPOS (*position);
4289 }
4290
4291 /* Reset those iterator values set from display property values. */
4292 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4293 it->space_width = Qnil;
4294 it->font_height = Qnil;
4295 it->voffset = 0;
4296
4297 /* We don't support recursive `display' properties, i.e. string
4298 values that have a string `display' property, that have a string
4299 `display' property etc. */
4300 if (!it->string_from_display_prop_p)
4301 it->area = TEXT_AREA;
4302
4303 propval = get_char_property_and_overlay (make_number (position->charpos),
4304 Qdisplay, object, &overlay);
4305 if (NILP (propval))
4306 return HANDLED_NORMALLY;
4307 /* Now OVERLAY is the overlay that gave us this property, or nil
4308 if it was a text property. */
4309
4310 if (!STRINGP (it->string))
4311 object = it->w->buffer;
4312
4313 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4314 position, bufpos,
4315 FRAME_WINDOW_P (it->f));
4316
4317 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4318 }
4319
4320 /* Subroutine of handle_display_prop. Returns non-zero if the display
4321 specification in SPEC is a replacing specification, i.e. it would
4322 replace the text covered by `display' property with something else,
4323 such as an image or a display string. If SPEC includes any kind or
4324 `(space ...) specification, the value is 2; this is used by
4325 compute_display_string_pos, which see.
4326
4327 See handle_single_display_spec for documentation of arguments.
4328 frame_window_p is non-zero if the window being redisplayed is on a
4329 GUI frame; this argument is used only if IT is NULL, see below.
4330
4331 IT can be NULL, if this is called by the bidi reordering code
4332 through compute_display_string_pos, which see. In that case, this
4333 function only examines SPEC, but does not otherwise "handle" it, in
4334 the sense that it doesn't set up members of IT from the display
4335 spec. */
4336 static int
4337 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4338 Lisp_Object overlay, struct text_pos *position,
4339 ptrdiff_t bufpos, int frame_window_p)
4340 {
4341 int replacing_p = 0;
4342 int rv;
4343
4344 if (CONSP (spec)
4345 /* Simple specifications. */
4346 && !EQ (XCAR (spec), Qimage)
4347 && !EQ (XCAR (spec), Qspace)
4348 && !EQ (XCAR (spec), Qwhen)
4349 && !EQ (XCAR (spec), Qslice)
4350 && !EQ (XCAR (spec), Qspace_width)
4351 && !EQ (XCAR (spec), Qheight)
4352 && !EQ (XCAR (spec), Qraise)
4353 /* Marginal area specifications. */
4354 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4355 && !EQ (XCAR (spec), Qleft_fringe)
4356 && !EQ (XCAR (spec), Qright_fringe)
4357 && !NILP (XCAR (spec)))
4358 {
4359 for (; CONSP (spec); spec = XCDR (spec))
4360 {
4361 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4362 overlay, position, bufpos,
4363 replacing_p, frame_window_p)))
4364 {
4365 replacing_p = rv;
4366 /* If some text in a string is replaced, `position' no
4367 longer points to the position of `object'. */
4368 if (!it || STRINGP (object))
4369 break;
4370 }
4371 }
4372 }
4373 else if (VECTORP (spec))
4374 {
4375 ptrdiff_t i;
4376 for (i = 0; i < ASIZE (spec); ++i)
4377 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4378 overlay, position, bufpos,
4379 replacing_p, frame_window_p)))
4380 {
4381 replacing_p = rv;
4382 /* If some text in a string is replaced, `position' no
4383 longer points to the position of `object'. */
4384 if (!it || STRINGP (object))
4385 break;
4386 }
4387 }
4388 else
4389 {
4390 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4391 position, bufpos, 0,
4392 frame_window_p)))
4393 replacing_p = rv;
4394 }
4395
4396 return replacing_p;
4397 }
4398
4399 /* Value is the position of the end of the `display' property starting
4400 at START_POS in OBJECT. */
4401
4402 static struct text_pos
4403 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4404 {
4405 Lisp_Object end;
4406 struct text_pos end_pos;
4407
4408 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4409 Qdisplay, object, Qnil);
4410 CHARPOS (end_pos) = XFASTINT (end);
4411 if (STRINGP (object))
4412 compute_string_pos (&end_pos, start_pos, it->string);
4413 else
4414 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4415
4416 return end_pos;
4417 }
4418
4419
4420 /* Set up IT from a single `display' property specification SPEC. OBJECT
4421 is the object in which the `display' property was found. *POSITION
4422 is the position in OBJECT at which the `display' property was found.
4423 BUFPOS is the buffer position of OBJECT (different from POSITION if
4424 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4425 previously saw a display specification which already replaced text
4426 display with something else, for example an image; we ignore such
4427 properties after the first one has been processed.
4428
4429 OVERLAY is the overlay this `display' property came from,
4430 or nil if it was a text property.
4431
4432 If SPEC is a `space' or `image' specification, and in some other
4433 cases too, set *POSITION to the position where the `display'
4434 property ends.
4435
4436 If IT is NULL, only examine the property specification in SPEC, but
4437 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4438 is intended to be displayed in a window on a GUI frame.
4439
4440 Value is non-zero if something was found which replaces the display
4441 of buffer or string text. */
4442
4443 static int
4444 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4445 Lisp_Object overlay, struct text_pos *position,
4446 ptrdiff_t bufpos, int display_replaced_p,
4447 int frame_window_p)
4448 {
4449 Lisp_Object form;
4450 Lisp_Object location, value;
4451 struct text_pos start_pos = *position;
4452 int valid_p;
4453
4454 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4455 If the result is non-nil, use VALUE instead of SPEC. */
4456 form = Qt;
4457 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4458 {
4459 spec = XCDR (spec);
4460 if (!CONSP (spec))
4461 return 0;
4462 form = XCAR (spec);
4463 spec = XCDR (spec);
4464 }
4465
4466 if (!NILP (form) && !EQ (form, Qt))
4467 {
4468 ptrdiff_t count = SPECPDL_INDEX ();
4469 struct gcpro gcpro1;
4470
4471 /* Bind `object' to the object having the `display' property, a
4472 buffer or string. Bind `position' to the position in the
4473 object where the property was found, and `buffer-position'
4474 to the current position in the buffer. */
4475
4476 if (NILP (object))
4477 XSETBUFFER (object, current_buffer);
4478 specbind (Qobject, object);
4479 specbind (Qposition, make_number (CHARPOS (*position)));
4480 specbind (Qbuffer_position, make_number (bufpos));
4481 GCPRO1 (form);
4482 form = safe_eval (form);
4483 UNGCPRO;
4484 unbind_to (count, Qnil);
4485 }
4486
4487 if (NILP (form))
4488 return 0;
4489
4490 /* Handle `(height HEIGHT)' specifications. */
4491 if (CONSP (spec)
4492 && EQ (XCAR (spec), Qheight)
4493 && CONSP (XCDR (spec)))
4494 {
4495 if (it)
4496 {
4497 if (!FRAME_WINDOW_P (it->f))
4498 return 0;
4499
4500 it->font_height = XCAR (XCDR (spec));
4501 if (!NILP (it->font_height))
4502 {
4503 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4504 int new_height = -1;
4505
4506 if (CONSP (it->font_height)
4507 && (EQ (XCAR (it->font_height), Qplus)
4508 || EQ (XCAR (it->font_height), Qminus))
4509 && CONSP (XCDR (it->font_height))
4510 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4511 {
4512 /* `(+ N)' or `(- N)' where N is an integer. */
4513 int steps = XINT (XCAR (XCDR (it->font_height)));
4514 if (EQ (XCAR (it->font_height), Qplus))
4515 steps = - steps;
4516 it->face_id = smaller_face (it->f, it->face_id, steps);
4517 }
4518 else if (FUNCTIONP (it->font_height))
4519 {
4520 /* Call function with current height as argument.
4521 Value is the new height. */
4522 Lisp_Object height;
4523 height = safe_call1 (it->font_height,
4524 face->lface[LFACE_HEIGHT_INDEX]);
4525 if (NUMBERP (height))
4526 new_height = XFLOATINT (height);
4527 }
4528 else if (NUMBERP (it->font_height))
4529 {
4530 /* Value is a multiple of the canonical char height. */
4531 struct face *f;
4532
4533 f = FACE_FROM_ID (it->f,
4534 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4535 new_height = (XFLOATINT (it->font_height)
4536 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4537 }
4538 else
4539 {
4540 /* Evaluate IT->font_height with `height' bound to the
4541 current specified height to get the new height. */
4542 ptrdiff_t count = SPECPDL_INDEX ();
4543
4544 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4545 value = safe_eval (it->font_height);
4546 unbind_to (count, Qnil);
4547
4548 if (NUMBERP (value))
4549 new_height = XFLOATINT (value);
4550 }
4551
4552 if (new_height > 0)
4553 it->face_id = face_with_height (it->f, it->face_id, new_height);
4554 }
4555 }
4556
4557 return 0;
4558 }
4559
4560 /* Handle `(space-width WIDTH)'. */
4561 if (CONSP (spec)
4562 && EQ (XCAR (spec), Qspace_width)
4563 && CONSP (XCDR (spec)))
4564 {
4565 if (it)
4566 {
4567 if (!FRAME_WINDOW_P (it->f))
4568 return 0;
4569
4570 value = XCAR (XCDR (spec));
4571 if (NUMBERP (value) && XFLOATINT (value) > 0)
4572 it->space_width = value;
4573 }
4574
4575 return 0;
4576 }
4577
4578 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4579 if (CONSP (spec)
4580 && EQ (XCAR (spec), Qslice))
4581 {
4582 Lisp_Object tem;
4583
4584 if (it)
4585 {
4586 if (!FRAME_WINDOW_P (it->f))
4587 return 0;
4588
4589 if (tem = XCDR (spec), CONSP (tem))
4590 {
4591 it->slice.x = XCAR (tem);
4592 if (tem = XCDR (tem), CONSP (tem))
4593 {
4594 it->slice.y = XCAR (tem);
4595 if (tem = XCDR (tem), CONSP (tem))
4596 {
4597 it->slice.width = XCAR (tem);
4598 if (tem = XCDR (tem), CONSP (tem))
4599 it->slice.height = XCAR (tem);
4600 }
4601 }
4602 }
4603 }
4604
4605 return 0;
4606 }
4607
4608 /* Handle `(raise FACTOR)'. */
4609 if (CONSP (spec)
4610 && EQ (XCAR (spec), Qraise)
4611 && CONSP (XCDR (spec)))
4612 {
4613 if (it)
4614 {
4615 if (!FRAME_WINDOW_P (it->f))
4616 return 0;
4617
4618 #ifdef HAVE_WINDOW_SYSTEM
4619 value = XCAR (XCDR (spec));
4620 if (NUMBERP (value))
4621 {
4622 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4623 it->voffset = - (XFLOATINT (value)
4624 * (FONT_HEIGHT (face->font)));
4625 }
4626 #endif /* HAVE_WINDOW_SYSTEM */
4627 }
4628
4629 return 0;
4630 }
4631
4632 /* Don't handle the other kinds of display specifications
4633 inside a string that we got from a `display' property. */
4634 if (it && it->string_from_display_prop_p)
4635 return 0;
4636
4637 /* Characters having this form of property are not displayed, so
4638 we have to find the end of the property. */
4639 if (it)
4640 {
4641 start_pos = *position;
4642 *position = display_prop_end (it, object, start_pos);
4643 }
4644 value = Qnil;
4645
4646 /* Stop the scan at that end position--we assume that all
4647 text properties change there. */
4648 if (it)
4649 it->stop_charpos = position->charpos;
4650
4651 /* Handle `(left-fringe BITMAP [FACE])'
4652 and `(right-fringe BITMAP [FACE])'. */
4653 if (CONSP (spec)
4654 && (EQ (XCAR (spec), Qleft_fringe)
4655 || EQ (XCAR (spec), Qright_fringe))
4656 && CONSP (XCDR (spec)))
4657 {
4658 int fringe_bitmap;
4659
4660 if (it)
4661 {
4662 if (!FRAME_WINDOW_P (it->f))
4663 /* If we return here, POSITION has been advanced
4664 across the text with this property. */
4665 return 0;
4666 }
4667 else if (!frame_window_p)
4668 return 0;
4669
4670 #ifdef HAVE_WINDOW_SYSTEM
4671 value = XCAR (XCDR (spec));
4672 if (!SYMBOLP (value)
4673 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4674 /* If we return here, POSITION has been advanced
4675 across the text with this property. */
4676 return 0;
4677
4678 if (it)
4679 {
4680 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4681
4682 if (CONSP (XCDR (XCDR (spec))))
4683 {
4684 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4685 int face_id2 = lookup_derived_face (it->f, face_name,
4686 FRINGE_FACE_ID, 0);
4687 if (face_id2 >= 0)
4688 face_id = face_id2;
4689 }
4690
4691 /* Save current settings of IT so that we can restore them
4692 when we are finished with the glyph property value. */
4693 push_it (it, position);
4694
4695 it->area = TEXT_AREA;
4696 it->what = IT_IMAGE;
4697 it->image_id = -1; /* no image */
4698 it->position = start_pos;
4699 it->object = NILP (object) ? it->w->buffer : object;
4700 it->method = GET_FROM_IMAGE;
4701 it->from_overlay = Qnil;
4702 it->face_id = face_id;
4703 it->from_disp_prop_p = 1;
4704
4705 /* Say that we haven't consumed the characters with
4706 `display' property yet. The call to pop_it in
4707 set_iterator_to_next will clean this up. */
4708 *position = start_pos;
4709
4710 if (EQ (XCAR (spec), Qleft_fringe))
4711 {
4712 it->left_user_fringe_bitmap = fringe_bitmap;
4713 it->left_user_fringe_face_id = face_id;
4714 }
4715 else
4716 {
4717 it->right_user_fringe_bitmap = fringe_bitmap;
4718 it->right_user_fringe_face_id = face_id;
4719 }
4720 }
4721 #endif /* HAVE_WINDOW_SYSTEM */
4722 return 1;
4723 }
4724
4725 /* Prepare to handle `((margin left-margin) ...)',
4726 `((margin right-margin) ...)' and `((margin nil) ...)'
4727 prefixes for display specifications. */
4728 location = Qunbound;
4729 if (CONSP (spec) && CONSP (XCAR (spec)))
4730 {
4731 Lisp_Object tem;
4732
4733 value = XCDR (spec);
4734 if (CONSP (value))
4735 value = XCAR (value);
4736
4737 tem = XCAR (spec);
4738 if (EQ (XCAR (tem), Qmargin)
4739 && (tem = XCDR (tem),
4740 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4741 (NILP (tem)
4742 || EQ (tem, Qleft_margin)
4743 || EQ (tem, Qright_margin))))
4744 location = tem;
4745 }
4746
4747 if (EQ (location, Qunbound))
4748 {
4749 location = Qnil;
4750 value = spec;
4751 }
4752
4753 /* After this point, VALUE is the property after any
4754 margin prefix has been stripped. It must be a string,
4755 an image specification, or `(space ...)'.
4756
4757 LOCATION specifies where to display: `left-margin',
4758 `right-margin' or nil. */
4759
4760 valid_p = (STRINGP (value)
4761 #ifdef HAVE_WINDOW_SYSTEM
4762 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4763 && valid_image_p (value))
4764 #endif /* not HAVE_WINDOW_SYSTEM */
4765 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4766
4767 if (valid_p && !display_replaced_p)
4768 {
4769 int retval = 1;
4770
4771 if (!it)
4772 {
4773 /* Callers need to know whether the display spec is any kind
4774 of `(space ...)' spec that is about to affect text-area
4775 display. */
4776 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4777 retval = 2;
4778 return retval;
4779 }
4780
4781 /* Save current settings of IT so that we can restore them
4782 when we are finished with the glyph property value. */
4783 push_it (it, position);
4784 it->from_overlay = overlay;
4785 it->from_disp_prop_p = 1;
4786
4787 if (NILP (location))
4788 it->area = TEXT_AREA;
4789 else if (EQ (location, Qleft_margin))
4790 it->area = LEFT_MARGIN_AREA;
4791 else
4792 it->area = RIGHT_MARGIN_AREA;
4793
4794 if (STRINGP (value))
4795 {
4796 it->string = value;
4797 it->multibyte_p = STRING_MULTIBYTE (it->string);
4798 it->current.overlay_string_index = -1;
4799 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4800 it->end_charpos = it->string_nchars = SCHARS (it->string);
4801 it->method = GET_FROM_STRING;
4802 it->stop_charpos = 0;
4803 it->prev_stop = 0;
4804 it->base_level_stop = 0;
4805 it->string_from_display_prop_p = 1;
4806 /* Say that we haven't consumed the characters with
4807 `display' property yet. The call to pop_it in
4808 set_iterator_to_next will clean this up. */
4809 if (BUFFERP (object))
4810 *position = start_pos;
4811
4812 /* Force paragraph direction to be that of the parent
4813 object. If the parent object's paragraph direction is
4814 not yet determined, default to L2R. */
4815 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4816 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4817 else
4818 it->paragraph_embedding = L2R;
4819
4820 /* Set up the bidi iterator for this display string. */
4821 if (it->bidi_p)
4822 {
4823 it->bidi_it.string.lstring = it->string;
4824 it->bidi_it.string.s = NULL;
4825 it->bidi_it.string.schars = it->end_charpos;
4826 it->bidi_it.string.bufpos = bufpos;
4827 it->bidi_it.string.from_disp_str = 1;
4828 it->bidi_it.string.unibyte = !it->multibyte_p;
4829 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4830 }
4831 }
4832 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4833 {
4834 it->method = GET_FROM_STRETCH;
4835 it->object = value;
4836 *position = it->position = start_pos;
4837 retval = 1 + (it->area == TEXT_AREA);
4838 }
4839 #ifdef HAVE_WINDOW_SYSTEM
4840 else
4841 {
4842 it->what = IT_IMAGE;
4843 it->image_id = lookup_image (it->f, value);
4844 it->position = start_pos;
4845 it->object = NILP (object) ? it->w->buffer : object;
4846 it->method = GET_FROM_IMAGE;
4847
4848 /* Say that we haven't consumed the characters with
4849 `display' property yet. The call to pop_it in
4850 set_iterator_to_next will clean this up. */
4851 *position = start_pos;
4852 }
4853 #endif /* HAVE_WINDOW_SYSTEM */
4854
4855 return retval;
4856 }
4857
4858 /* Invalid property or property not supported. Restore
4859 POSITION to what it was before. */
4860 *position = start_pos;
4861 return 0;
4862 }
4863
4864 /* Check if PROP is a display property value whose text should be
4865 treated as intangible. OVERLAY is the overlay from which PROP
4866 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4867 specify the buffer position covered by PROP. */
4868
4869 int
4870 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4871 ptrdiff_t charpos, ptrdiff_t bytepos)
4872 {
4873 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4874 struct text_pos position;
4875
4876 SET_TEXT_POS (position, charpos, bytepos);
4877 return handle_display_spec (NULL, prop, Qnil, overlay,
4878 &position, charpos, frame_window_p);
4879 }
4880
4881
4882 /* Return 1 if PROP is a display sub-property value containing STRING.
4883
4884 Implementation note: this and the following function are really
4885 special cases of handle_display_spec and
4886 handle_single_display_spec, and should ideally use the same code.
4887 Until they do, these two pairs must be consistent and must be
4888 modified in sync. */
4889
4890 static int
4891 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4892 {
4893 if (EQ (string, prop))
4894 return 1;
4895
4896 /* Skip over `when FORM'. */
4897 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4898 {
4899 prop = XCDR (prop);
4900 if (!CONSP (prop))
4901 return 0;
4902 /* Actually, the condition following `when' should be eval'ed,
4903 like handle_single_display_spec does, and we should return
4904 zero if it evaluates to nil. However, this function is
4905 called only when the buffer was already displayed and some
4906 glyph in the glyph matrix was found to come from a display
4907 string. Therefore, the condition was already evaluated, and
4908 the result was non-nil, otherwise the display string wouldn't
4909 have been displayed and we would have never been called for
4910 this property. Thus, we can skip the evaluation and assume
4911 its result is non-nil. */
4912 prop = XCDR (prop);
4913 }
4914
4915 if (CONSP (prop))
4916 /* Skip over `margin LOCATION'. */
4917 if (EQ (XCAR (prop), Qmargin))
4918 {
4919 prop = XCDR (prop);
4920 if (!CONSP (prop))
4921 return 0;
4922
4923 prop = XCDR (prop);
4924 if (!CONSP (prop))
4925 return 0;
4926 }
4927
4928 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4929 }
4930
4931
4932 /* Return 1 if STRING appears in the `display' property PROP. */
4933
4934 static int
4935 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4936 {
4937 if (CONSP (prop)
4938 && !EQ (XCAR (prop), Qwhen)
4939 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4940 {
4941 /* A list of sub-properties. */
4942 while (CONSP (prop))
4943 {
4944 if (single_display_spec_string_p (XCAR (prop), string))
4945 return 1;
4946 prop = XCDR (prop);
4947 }
4948 }
4949 else if (VECTORP (prop))
4950 {
4951 /* A vector of sub-properties. */
4952 ptrdiff_t i;
4953 for (i = 0; i < ASIZE (prop); ++i)
4954 if (single_display_spec_string_p (AREF (prop, i), string))
4955 return 1;
4956 }
4957 else
4958 return single_display_spec_string_p (prop, string);
4959
4960 return 0;
4961 }
4962
4963 /* Look for STRING in overlays and text properties in the current
4964 buffer, between character positions FROM and TO (excluding TO).
4965 BACK_P non-zero means look back (in this case, TO is supposed to be
4966 less than FROM).
4967 Value is the first character position where STRING was found, or
4968 zero if it wasn't found before hitting TO.
4969
4970 This function may only use code that doesn't eval because it is
4971 called asynchronously from note_mouse_highlight. */
4972
4973 static ptrdiff_t
4974 string_buffer_position_lim (Lisp_Object string,
4975 ptrdiff_t from, ptrdiff_t to, int back_p)
4976 {
4977 Lisp_Object limit, prop, pos;
4978 int found = 0;
4979
4980 pos = make_number (from);
4981
4982 if (!back_p) /* looking forward */
4983 {
4984 limit = make_number (min (to, ZV));
4985 while (!found && !EQ (pos, limit))
4986 {
4987 prop = Fget_char_property (pos, Qdisplay, Qnil);
4988 if (!NILP (prop) && display_prop_string_p (prop, string))
4989 found = 1;
4990 else
4991 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4992 limit);
4993 }
4994 }
4995 else /* looking back */
4996 {
4997 limit = make_number (max (to, BEGV));
4998 while (!found && !EQ (pos, limit))
4999 {
5000 prop = Fget_char_property (pos, Qdisplay, Qnil);
5001 if (!NILP (prop) && display_prop_string_p (prop, string))
5002 found = 1;
5003 else
5004 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5005 limit);
5006 }
5007 }
5008
5009 return found ? XINT (pos) : 0;
5010 }
5011
5012 /* Determine which buffer position in current buffer STRING comes from.
5013 AROUND_CHARPOS is an approximate position where it could come from.
5014 Value is the buffer position or 0 if it couldn't be determined.
5015
5016 This function is necessary because we don't record buffer positions
5017 in glyphs generated from strings (to keep struct glyph small).
5018 This function may only use code that doesn't eval because it is
5019 called asynchronously from note_mouse_highlight. */
5020
5021 static ptrdiff_t
5022 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5023 {
5024 const int MAX_DISTANCE = 1000;
5025 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5026 around_charpos + MAX_DISTANCE,
5027 0);
5028
5029 if (!found)
5030 found = string_buffer_position_lim (string, around_charpos,
5031 around_charpos - MAX_DISTANCE, 1);
5032 return found;
5033 }
5034
5035
5036 \f
5037 /***********************************************************************
5038 `composition' property
5039 ***********************************************************************/
5040
5041 /* Set up iterator IT from `composition' property at its current
5042 position. Called from handle_stop. */
5043
5044 static enum prop_handled
5045 handle_composition_prop (struct it *it)
5046 {
5047 Lisp_Object prop, string;
5048 ptrdiff_t pos, pos_byte, start, end;
5049
5050 if (STRINGP (it->string))
5051 {
5052 unsigned char *s;
5053
5054 pos = IT_STRING_CHARPOS (*it);
5055 pos_byte = IT_STRING_BYTEPOS (*it);
5056 string = it->string;
5057 s = SDATA (string) + pos_byte;
5058 it->c = STRING_CHAR (s);
5059 }
5060 else
5061 {
5062 pos = IT_CHARPOS (*it);
5063 pos_byte = IT_BYTEPOS (*it);
5064 string = Qnil;
5065 it->c = FETCH_CHAR (pos_byte);
5066 }
5067
5068 /* If there's a valid composition and point is not inside of the
5069 composition (in the case that the composition is from the current
5070 buffer), draw a glyph composed from the composition components. */
5071 if (find_composition (pos, -1, &start, &end, &prop, string)
5072 && COMPOSITION_VALID_P (start, end, prop)
5073 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5074 {
5075 if (start < pos)
5076 /* As we can't handle this situation (perhaps font-lock added
5077 a new composition), we just return here hoping that next
5078 redisplay will detect this composition much earlier. */
5079 return HANDLED_NORMALLY;
5080 if (start != pos)
5081 {
5082 if (STRINGP (it->string))
5083 pos_byte = string_char_to_byte (it->string, start);
5084 else
5085 pos_byte = CHAR_TO_BYTE (start);
5086 }
5087 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5088 prop, string);
5089
5090 if (it->cmp_it.id >= 0)
5091 {
5092 it->cmp_it.ch = -1;
5093 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5094 it->cmp_it.nglyphs = -1;
5095 }
5096 }
5097
5098 return HANDLED_NORMALLY;
5099 }
5100
5101
5102 \f
5103 /***********************************************************************
5104 Overlay strings
5105 ***********************************************************************/
5106
5107 /* The following structure is used to record overlay strings for
5108 later sorting in load_overlay_strings. */
5109
5110 struct overlay_entry
5111 {
5112 Lisp_Object overlay;
5113 Lisp_Object string;
5114 EMACS_INT priority;
5115 int after_string_p;
5116 };
5117
5118
5119 /* Set up iterator IT from overlay strings at its current position.
5120 Called from handle_stop. */
5121
5122 static enum prop_handled
5123 handle_overlay_change (struct it *it)
5124 {
5125 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5126 return HANDLED_RECOMPUTE_PROPS;
5127 else
5128 return HANDLED_NORMALLY;
5129 }
5130
5131
5132 /* Set up the next overlay string for delivery by IT, if there is an
5133 overlay string to deliver. Called by set_iterator_to_next when the
5134 end of the current overlay string is reached. If there are more
5135 overlay strings to display, IT->string and
5136 IT->current.overlay_string_index are set appropriately here.
5137 Otherwise IT->string is set to nil. */
5138
5139 static void
5140 next_overlay_string (struct it *it)
5141 {
5142 ++it->current.overlay_string_index;
5143 if (it->current.overlay_string_index == it->n_overlay_strings)
5144 {
5145 /* No more overlay strings. Restore IT's settings to what
5146 they were before overlay strings were processed, and
5147 continue to deliver from current_buffer. */
5148
5149 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5150 pop_it (it);
5151 xassert (it->sp > 0
5152 || (NILP (it->string)
5153 && it->method == GET_FROM_BUFFER
5154 && it->stop_charpos >= BEGV
5155 && it->stop_charpos <= it->end_charpos));
5156 it->current.overlay_string_index = -1;
5157 it->n_overlay_strings = 0;
5158 it->overlay_strings_charpos = -1;
5159
5160 /* If we're at the end of the buffer, record that we have
5161 processed the overlay strings there already, so that
5162 next_element_from_buffer doesn't try it again. */
5163 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5164 it->overlay_strings_at_end_processed_p = 1;
5165 }
5166 else
5167 {
5168 /* There are more overlay strings to process. If
5169 IT->current.overlay_string_index has advanced to a position
5170 where we must load IT->overlay_strings with more strings, do
5171 it. We must load at the IT->overlay_strings_charpos where
5172 IT->n_overlay_strings was originally computed; when invisible
5173 text is present, this might not be IT_CHARPOS (Bug#7016). */
5174 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5175
5176 if (it->current.overlay_string_index && i == 0)
5177 load_overlay_strings (it, it->overlay_strings_charpos);
5178
5179 /* Initialize IT to deliver display elements from the overlay
5180 string. */
5181 it->string = it->overlay_strings[i];
5182 it->multibyte_p = STRING_MULTIBYTE (it->string);
5183 SET_TEXT_POS (it->current.string_pos, 0, 0);
5184 it->method = GET_FROM_STRING;
5185 it->stop_charpos = 0;
5186 if (it->cmp_it.stop_pos >= 0)
5187 it->cmp_it.stop_pos = 0;
5188 it->prev_stop = 0;
5189 it->base_level_stop = 0;
5190
5191 /* Set up the bidi iterator for this overlay string. */
5192 if (it->bidi_p)
5193 {
5194 it->bidi_it.string.lstring = it->string;
5195 it->bidi_it.string.s = NULL;
5196 it->bidi_it.string.schars = SCHARS (it->string);
5197 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5198 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5199 it->bidi_it.string.unibyte = !it->multibyte_p;
5200 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5201 }
5202 }
5203
5204 CHECK_IT (it);
5205 }
5206
5207
5208 /* Compare two overlay_entry structures E1 and E2. Used as a
5209 comparison function for qsort in load_overlay_strings. Overlay
5210 strings for the same position are sorted so that
5211
5212 1. All after-strings come in front of before-strings, except
5213 when they come from the same overlay.
5214
5215 2. Within after-strings, strings are sorted so that overlay strings
5216 from overlays with higher priorities come first.
5217
5218 2. Within before-strings, strings are sorted so that overlay
5219 strings from overlays with higher priorities come last.
5220
5221 Value is analogous to strcmp. */
5222
5223
5224 static int
5225 compare_overlay_entries (const void *e1, const void *e2)
5226 {
5227 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5228 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5229 int result;
5230
5231 if (entry1->after_string_p != entry2->after_string_p)
5232 {
5233 /* Let after-strings appear in front of before-strings if
5234 they come from different overlays. */
5235 if (EQ (entry1->overlay, entry2->overlay))
5236 result = entry1->after_string_p ? 1 : -1;
5237 else
5238 result = entry1->after_string_p ? -1 : 1;
5239 }
5240 else if (entry1->priority != entry2->priority)
5241 {
5242 if (entry1->after_string_p)
5243 /* After-strings sorted in order of decreasing priority. */
5244 result = entry2->priority < entry1->priority ? -1 : 1;
5245 else
5246 /* Before-strings sorted in order of increasing priority. */
5247 result = entry1->priority < entry2->priority ? -1 : 1;
5248 }
5249 else
5250 result = 0;
5251
5252 return result;
5253 }
5254
5255
5256 /* Load the vector IT->overlay_strings with overlay strings from IT's
5257 current buffer position, or from CHARPOS if that is > 0. Set
5258 IT->n_overlays to the total number of overlay strings found.
5259
5260 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5261 a time. On entry into load_overlay_strings,
5262 IT->current.overlay_string_index gives the number of overlay
5263 strings that have already been loaded by previous calls to this
5264 function.
5265
5266 IT->add_overlay_start contains an additional overlay start
5267 position to consider for taking overlay strings from, if non-zero.
5268 This position comes into play when the overlay has an `invisible'
5269 property, and both before and after-strings. When we've skipped to
5270 the end of the overlay, because of its `invisible' property, we
5271 nevertheless want its before-string to appear.
5272 IT->add_overlay_start will contain the overlay start position
5273 in this case.
5274
5275 Overlay strings are sorted so that after-string strings come in
5276 front of before-string strings. Within before and after-strings,
5277 strings are sorted by overlay priority. See also function
5278 compare_overlay_entries. */
5279
5280 static void
5281 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5282 {
5283 Lisp_Object overlay, window, str, invisible;
5284 struct Lisp_Overlay *ov;
5285 ptrdiff_t start, end;
5286 ptrdiff_t size = 20;
5287 ptrdiff_t n = 0, i, j;
5288 int invis_p;
5289 struct overlay_entry *entries
5290 = (struct overlay_entry *) alloca (size * sizeof *entries);
5291 USE_SAFE_ALLOCA;
5292
5293 if (charpos <= 0)
5294 charpos = IT_CHARPOS (*it);
5295
5296 /* Append the overlay string STRING of overlay OVERLAY to vector
5297 `entries' which has size `size' and currently contains `n'
5298 elements. AFTER_P non-zero means STRING is an after-string of
5299 OVERLAY. */
5300 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5301 do \
5302 { \
5303 Lisp_Object priority; \
5304 \
5305 if (n == size) \
5306 { \
5307 struct overlay_entry *old = entries; \
5308 SAFE_NALLOCA (entries, 2, size); \
5309 memcpy (entries, old, size * sizeof *entries); \
5310 size *= 2; \
5311 } \
5312 \
5313 entries[n].string = (STRING); \
5314 entries[n].overlay = (OVERLAY); \
5315 priority = Foverlay_get ((OVERLAY), Qpriority); \
5316 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5317 entries[n].after_string_p = (AFTER_P); \
5318 ++n; \
5319 } \
5320 while (0)
5321
5322 /* Process overlay before the overlay center. */
5323 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5324 {
5325 XSETMISC (overlay, ov);
5326 xassert (OVERLAYP (overlay));
5327 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5328 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5329
5330 if (end < charpos)
5331 break;
5332
5333 /* Skip this overlay if it doesn't start or end at IT's current
5334 position. */
5335 if (end != charpos && start != charpos)
5336 continue;
5337
5338 /* Skip this overlay if it doesn't apply to IT->w. */
5339 window = Foverlay_get (overlay, Qwindow);
5340 if (WINDOWP (window) && XWINDOW (window) != it->w)
5341 continue;
5342
5343 /* If the text ``under'' the overlay is invisible, both before-
5344 and after-strings from this overlay are visible; start and
5345 end position are indistinguishable. */
5346 invisible = Foverlay_get (overlay, Qinvisible);
5347 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5348
5349 /* If overlay has a non-empty before-string, record it. */
5350 if ((start == charpos || (end == charpos && invis_p))
5351 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5352 && SCHARS (str))
5353 RECORD_OVERLAY_STRING (overlay, str, 0);
5354
5355 /* If overlay has a non-empty after-string, record it. */
5356 if ((end == charpos || (start == charpos && invis_p))
5357 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5358 && SCHARS (str))
5359 RECORD_OVERLAY_STRING (overlay, str, 1);
5360 }
5361
5362 /* Process overlays after the overlay center. */
5363 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5364 {
5365 XSETMISC (overlay, ov);
5366 xassert (OVERLAYP (overlay));
5367 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5368 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5369
5370 if (start > charpos)
5371 break;
5372
5373 /* Skip this overlay if it doesn't start or end at IT's current
5374 position. */
5375 if (end != charpos && start != charpos)
5376 continue;
5377
5378 /* Skip this overlay if it doesn't apply to IT->w. */
5379 window = Foverlay_get (overlay, Qwindow);
5380 if (WINDOWP (window) && XWINDOW (window) != it->w)
5381 continue;
5382
5383 /* If the text ``under'' the overlay is invisible, it has a zero
5384 dimension, and both before- and after-strings apply. */
5385 invisible = Foverlay_get (overlay, Qinvisible);
5386 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5387
5388 /* If overlay has a non-empty before-string, record it. */
5389 if ((start == charpos || (end == charpos && invis_p))
5390 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5391 && SCHARS (str))
5392 RECORD_OVERLAY_STRING (overlay, str, 0);
5393
5394 /* If overlay has a non-empty after-string, record it. */
5395 if ((end == charpos || (start == charpos && invis_p))
5396 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5397 && SCHARS (str))
5398 RECORD_OVERLAY_STRING (overlay, str, 1);
5399 }
5400
5401 #undef RECORD_OVERLAY_STRING
5402
5403 /* Sort entries. */
5404 if (n > 1)
5405 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5406
5407 /* Record number of overlay strings, and where we computed it. */
5408 it->n_overlay_strings = n;
5409 it->overlay_strings_charpos = charpos;
5410
5411 /* IT->current.overlay_string_index is the number of overlay strings
5412 that have already been consumed by IT. Copy some of the
5413 remaining overlay strings to IT->overlay_strings. */
5414 i = 0;
5415 j = it->current.overlay_string_index;
5416 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5417 {
5418 it->overlay_strings[i] = entries[j].string;
5419 it->string_overlays[i++] = entries[j++].overlay;
5420 }
5421
5422 CHECK_IT (it);
5423 SAFE_FREE ();
5424 }
5425
5426
5427 /* Get the first chunk of overlay strings at IT's current buffer
5428 position, or at CHARPOS if that is > 0. Value is non-zero if at
5429 least one overlay string was found. */
5430
5431 static int
5432 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5433 {
5434 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5435 process. This fills IT->overlay_strings with strings, and sets
5436 IT->n_overlay_strings to the total number of strings to process.
5437 IT->pos.overlay_string_index has to be set temporarily to zero
5438 because load_overlay_strings needs this; it must be set to -1
5439 when no overlay strings are found because a zero value would
5440 indicate a position in the first overlay string. */
5441 it->current.overlay_string_index = 0;
5442 load_overlay_strings (it, charpos);
5443
5444 /* If we found overlay strings, set up IT to deliver display
5445 elements from the first one. Otherwise set up IT to deliver
5446 from current_buffer. */
5447 if (it->n_overlay_strings)
5448 {
5449 /* Make sure we know settings in current_buffer, so that we can
5450 restore meaningful values when we're done with the overlay
5451 strings. */
5452 if (compute_stop_p)
5453 compute_stop_pos (it);
5454 xassert (it->face_id >= 0);
5455
5456 /* Save IT's settings. They are restored after all overlay
5457 strings have been processed. */
5458 xassert (!compute_stop_p || it->sp == 0);
5459
5460 /* When called from handle_stop, there might be an empty display
5461 string loaded. In that case, don't bother saving it. */
5462 if (!STRINGP (it->string) || SCHARS (it->string))
5463 push_it (it, NULL);
5464
5465 /* Set up IT to deliver display elements from the first overlay
5466 string. */
5467 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5468 it->string = it->overlay_strings[0];
5469 it->from_overlay = Qnil;
5470 it->stop_charpos = 0;
5471 xassert (STRINGP (it->string));
5472 it->end_charpos = SCHARS (it->string);
5473 it->prev_stop = 0;
5474 it->base_level_stop = 0;
5475 it->multibyte_p = STRING_MULTIBYTE (it->string);
5476 it->method = GET_FROM_STRING;
5477 it->from_disp_prop_p = 0;
5478
5479 /* Force paragraph direction to be that of the parent
5480 buffer. */
5481 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5482 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5483 else
5484 it->paragraph_embedding = L2R;
5485
5486 /* Set up the bidi iterator for this overlay string. */
5487 if (it->bidi_p)
5488 {
5489 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5490
5491 it->bidi_it.string.lstring = it->string;
5492 it->bidi_it.string.s = NULL;
5493 it->bidi_it.string.schars = SCHARS (it->string);
5494 it->bidi_it.string.bufpos = pos;
5495 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5496 it->bidi_it.string.unibyte = !it->multibyte_p;
5497 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5498 }
5499 return 1;
5500 }
5501
5502 it->current.overlay_string_index = -1;
5503 return 0;
5504 }
5505
5506 static int
5507 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5508 {
5509 it->string = Qnil;
5510 it->method = GET_FROM_BUFFER;
5511
5512 (void) get_overlay_strings_1 (it, charpos, 1);
5513
5514 CHECK_IT (it);
5515
5516 /* Value is non-zero if we found at least one overlay string. */
5517 return STRINGP (it->string);
5518 }
5519
5520
5521 \f
5522 /***********************************************************************
5523 Saving and restoring state
5524 ***********************************************************************/
5525
5526 /* Save current settings of IT on IT->stack. Called, for example,
5527 before setting up IT for an overlay string, to be able to restore
5528 IT's settings to what they were after the overlay string has been
5529 processed. If POSITION is non-NULL, it is the position to save on
5530 the stack instead of IT->position. */
5531
5532 static void
5533 push_it (struct it *it, struct text_pos *position)
5534 {
5535 struct iterator_stack_entry *p;
5536
5537 xassert (it->sp < IT_STACK_SIZE);
5538 p = it->stack + it->sp;
5539
5540 p->stop_charpos = it->stop_charpos;
5541 p->prev_stop = it->prev_stop;
5542 p->base_level_stop = it->base_level_stop;
5543 p->cmp_it = it->cmp_it;
5544 xassert (it->face_id >= 0);
5545 p->face_id = it->face_id;
5546 p->string = it->string;
5547 p->method = it->method;
5548 p->from_overlay = it->from_overlay;
5549 switch (p->method)
5550 {
5551 case GET_FROM_IMAGE:
5552 p->u.image.object = it->object;
5553 p->u.image.image_id = it->image_id;
5554 p->u.image.slice = it->slice;
5555 break;
5556 case GET_FROM_STRETCH:
5557 p->u.stretch.object = it->object;
5558 break;
5559 }
5560 p->position = position ? *position : it->position;
5561 p->current = it->current;
5562 p->end_charpos = it->end_charpos;
5563 p->string_nchars = it->string_nchars;
5564 p->area = it->area;
5565 p->multibyte_p = it->multibyte_p;
5566 p->avoid_cursor_p = it->avoid_cursor_p;
5567 p->space_width = it->space_width;
5568 p->font_height = it->font_height;
5569 p->voffset = it->voffset;
5570 p->string_from_display_prop_p = it->string_from_display_prop_p;
5571 p->display_ellipsis_p = 0;
5572 p->line_wrap = it->line_wrap;
5573 p->bidi_p = it->bidi_p;
5574 p->paragraph_embedding = it->paragraph_embedding;
5575 p->from_disp_prop_p = it->from_disp_prop_p;
5576 ++it->sp;
5577
5578 /* Save the state of the bidi iterator as well. */
5579 if (it->bidi_p)
5580 bidi_push_it (&it->bidi_it);
5581 }
5582
5583 static void
5584 iterate_out_of_display_property (struct it *it)
5585 {
5586 int buffer_p = BUFFERP (it->object);
5587 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5588 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5589
5590 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5591
5592 /* Maybe initialize paragraph direction. If we are at the beginning
5593 of a new paragraph, next_element_from_buffer may not have a
5594 chance to do that. */
5595 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5596 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5597 /* prev_stop can be zero, so check against BEGV as well. */
5598 while (it->bidi_it.charpos >= bob
5599 && it->prev_stop <= it->bidi_it.charpos
5600 && it->bidi_it.charpos < CHARPOS (it->position)
5601 && it->bidi_it.charpos < eob)
5602 bidi_move_to_visually_next (&it->bidi_it);
5603 /* Record the stop_pos we just crossed, for when we cross it
5604 back, maybe. */
5605 if (it->bidi_it.charpos > CHARPOS (it->position))
5606 it->prev_stop = CHARPOS (it->position);
5607 /* If we ended up not where pop_it put us, resync IT's
5608 positional members with the bidi iterator. */
5609 if (it->bidi_it.charpos != CHARPOS (it->position))
5610 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5611 if (buffer_p)
5612 it->current.pos = it->position;
5613 else
5614 it->current.string_pos = it->position;
5615 }
5616
5617 /* Restore IT's settings from IT->stack. Called, for example, when no
5618 more overlay strings must be processed, and we return to delivering
5619 display elements from a buffer, or when the end of a string from a
5620 `display' property is reached and we return to delivering display
5621 elements from an overlay string, or from a buffer. */
5622
5623 static void
5624 pop_it (struct it *it)
5625 {
5626 struct iterator_stack_entry *p;
5627 int from_display_prop = it->from_disp_prop_p;
5628
5629 xassert (it->sp > 0);
5630 --it->sp;
5631 p = it->stack + it->sp;
5632 it->stop_charpos = p->stop_charpos;
5633 it->prev_stop = p->prev_stop;
5634 it->base_level_stop = p->base_level_stop;
5635 it->cmp_it = p->cmp_it;
5636 it->face_id = p->face_id;
5637 it->current = p->current;
5638 it->position = p->position;
5639 it->string = p->string;
5640 it->from_overlay = p->from_overlay;
5641 if (NILP (it->string))
5642 SET_TEXT_POS (it->current.string_pos, -1, -1);
5643 it->method = p->method;
5644 switch (it->method)
5645 {
5646 case GET_FROM_IMAGE:
5647 it->image_id = p->u.image.image_id;
5648 it->object = p->u.image.object;
5649 it->slice = p->u.image.slice;
5650 break;
5651 case GET_FROM_STRETCH:
5652 it->object = p->u.stretch.object;
5653 break;
5654 case GET_FROM_BUFFER:
5655 it->object = it->w->buffer;
5656 break;
5657 case GET_FROM_STRING:
5658 it->object = it->string;
5659 break;
5660 case GET_FROM_DISPLAY_VECTOR:
5661 if (it->s)
5662 it->method = GET_FROM_C_STRING;
5663 else if (STRINGP (it->string))
5664 it->method = GET_FROM_STRING;
5665 else
5666 {
5667 it->method = GET_FROM_BUFFER;
5668 it->object = it->w->buffer;
5669 }
5670 }
5671 it->end_charpos = p->end_charpos;
5672 it->string_nchars = p->string_nchars;
5673 it->area = p->area;
5674 it->multibyte_p = p->multibyte_p;
5675 it->avoid_cursor_p = p->avoid_cursor_p;
5676 it->space_width = p->space_width;
5677 it->font_height = p->font_height;
5678 it->voffset = p->voffset;
5679 it->string_from_display_prop_p = p->string_from_display_prop_p;
5680 it->line_wrap = p->line_wrap;
5681 it->bidi_p = p->bidi_p;
5682 it->paragraph_embedding = p->paragraph_embedding;
5683 it->from_disp_prop_p = p->from_disp_prop_p;
5684 if (it->bidi_p)
5685 {
5686 bidi_pop_it (&it->bidi_it);
5687 /* Bidi-iterate until we get out of the portion of text, if any,
5688 covered by a `display' text property or by an overlay with
5689 `display' property. (We cannot just jump there, because the
5690 internal coherency of the bidi iterator state can not be
5691 preserved across such jumps.) We also must determine the
5692 paragraph base direction if the overlay we just processed is
5693 at the beginning of a new paragraph. */
5694 if (from_display_prop
5695 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5696 iterate_out_of_display_property (it);
5697
5698 xassert ((BUFFERP (it->object)
5699 && IT_CHARPOS (*it) == it->bidi_it.charpos
5700 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5701 || (STRINGP (it->object)
5702 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5703 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5704 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5705 }
5706 }
5707
5708
5709 \f
5710 /***********************************************************************
5711 Moving over lines
5712 ***********************************************************************/
5713
5714 /* Set IT's current position to the previous line start. */
5715
5716 static void
5717 back_to_previous_line_start (struct it *it)
5718 {
5719 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5720 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5721 }
5722
5723
5724 /* Move IT to the next line start.
5725
5726 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5727 we skipped over part of the text (as opposed to moving the iterator
5728 continuously over the text). Otherwise, don't change the value
5729 of *SKIPPED_P.
5730
5731 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5732 iterator on the newline, if it was found.
5733
5734 Newlines may come from buffer text, overlay strings, or strings
5735 displayed via the `display' property. That's the reason we can't
5736 simply use find_next_newline_no_quit.
5737
5738 Note that this function may not skip over invisible text that is so
5739 because of text properties and immediately follows a newline. If
5740 it would, function reseat_at_next_visible_line_start, when called
5741 from set_iterator_to_next, would effectively make invisible
5742 characters following a newline part of the wrong glyph row, which
5743 leads to wrong cursor motion. */
5744
5745 static int
5746 forward_to_next_line_start (struct it *it, int *skipped_p,
5747 struct bidi_it *bidi_it_prev)
5748 {
5749 ptrdiff_t old_selective;
5750 int newline_found_p, n;
5751 const int MAX_NEWLINE_DISTANCE = 500;
5752
5753 /* If already on a newline, just consume it to avoid unintended
5754 skipping over invisible text below. */
5755 if (it->what == IT_CHARACTER
5756 && it->c == '\n'
5757 && CHARPOS (it->position) == IT_CHARPOS (*it))
5758 {
5759 if (it->bidi_p && bidi_it_prev)
5760 *bidi_it_prev = it->bidi_it;
5761 set_iterator_to_next (it, 0);
5762 it->c = 0;
5763 return 1;
5764 }
5765
5766 /* Don't handle selective display in the following. It's (a)
5767 unnecessary because it's done by the caller, and (b) leads to an
5768 infinite recursion because next_element_from_ellipsis indirectly
5769 calls this function. */
5770 old_selective = it->selective;
5771 it->selective = 0;
5772
5773 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5774 from buffer text. */
5775 for (n = newline_found_p = 0;
5776 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5777 n += STRINGP (it->string) ? 0 : 1)
5778 {
5779 if (!get_next_display_element (it))
5780 return 0;
5781 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5782 if (newline_found_p && it->bidi_p && bidi_it_prev)
5783 *bidi_it_prev = it->bidi_it;
5784 set_iterator_to_next (it, 0);
5785 }
5786
5787 /* If we didn't find a newline near enough, see if we can use a
5788 short-cut. */
5789 if (!newline_found_p)
5790 {
5791 ptrdiff_t start = IT_CHARPOS (*it);
5792 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5793 Lisp_Object pos;
5794
5795 xassert (!STRINGP (it->string));
5796
5797 /* If there isn't any `display' property in sight, and no
5798 overlays, we can just use the position of the newline in
5799 buffer text. */
5800 if (it->stop_charpos >= limit
5801 || ((pos = Fnext_single_property_change (make_number (start),
5802 Qdisplay, Qnil,
5803 make_number (limit)),
5804 NILP (pos))
5805 && next_overlay_change (start) == ZV))
5806 {
5807 if (!it->bidi_p)
5808 {
5809 IT_CHARPOS (*it) = limit;
5810 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5811 }
5812 else
5813 {
5814 struct bidi_it bprev;
5815
5816 /* Help bidi.c avoid expensive searches for display
5817 properties and overlays, by telling it that there are
5818 none up to `limit'. */
5819 if (it->bidi_it.disp_pos < limit)
5820 {
5821 it->bidi_it.disp_pos = limit;
5822 it->bidi_it.disp_prop = 0;
5823 }
5824 do {
5825 bprev = it->bidi_it;
5826 bidi_move_to_visually_next (&it->bidi_it);
5827 } while (it->bidi_it.charpos != limit);
5828 IT_CHARPOS (*it) = limit;
5829 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5830 if (bidi_it_prev)
5831 *bidi_it_prev = bprev;
5832 }
5833 *skipped_p = newline_found_p = 1;
5834 }
5835 else
5836 {
5837 while (get_next_display_element (it)
5838 && !newline_found_p)
5839 {
5840 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5841 if (newline_found_p && it->bidi_p && bidi_it_prev)
5842 *bidi_it_prev = it->bidi_it;
5843 set_iterator_to_next (it, 0);
5844 }
5845 }
5846 }
5847
5848 it->selective = old_selective;
5849 return newline_found_p;
5850 }
5851
5852
5853 /* Set IT's current position to the previous visible line start. Skip
5854 invisible text that is so either due to text properties or due to
5855 selective display. Caution: this does not change IT->current_x and
5856 IT->hpos. */
5857
5858 static void
5859 back_to_previous_visible_line_start (struct it *it)
5860 {
5861 while (IT_CHARPOS (*it) > BEGV)
5862 {
5863 back_to_previous_line_start (it);
5864
5865 if (IT_CHARPOS (*it) <= BEGV)
5866 break;
5867
5868 /* If selective > 0, then lines indented more than its value are
5869 invisible. */
5870 if (it->selective > 0
5871 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5872 it->selective))
5873 continue;
5874
5875 /* Check the newline before point for invisibility. */
5876 {
5877 Lisp_Object prop;
5878 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5879 Qinvisible, it->window);
5880 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5881 continue;
5882 }
5883
5884 if (IT_CHARPOS (*it) <= BEGV)
5885 break;
5886
5887 {
5888 struct it it2;
5889 void *it2data = NULL;
5890 ptrdiff_t pos;
5891 ptrdiff_t beg, end;
5892 Lisp_Object val, overlay;
5893
5894 SAVE_IT (it2, *it, it2data);
5895
5896 /* If newline is part of a composition, continue from start of composition */
5897 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5898 && beg < IT_CHARPOS (*it))
5899 goto replaced;
5900
5901 /* If newline is replaced by a display property, find start of overlay
5902 or interval and continue search from that point. */
5903 pos = --IT_CHARPOS (it2);
5904 --IT_BYTEPOS (it2);
5905 it2.sp = 0;
5906 bidi_unshelve_cache (NULL, 0);
5907 it2.string_from_display_prop_p = 0;
5908 it2.from_disp_prop_p = 0;
5909 if (handle_display_prop (&it2) == HANDLED_RETURN
5910 && !NILP (val = get_char_property_and_overlay
5911 (make_number (pos), Qdisplay, Qnil, &overlay))
5912 && (OVERLAYP (overlay)
5913 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5914 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5915 {
5916 RESTORE_IT (it, it, it2data);
5917 goto replaced;
5918 }
5919
5920 /* Newline is not replaced by anything -- so we are done. */
5921 RESTORE_IT (it, it, it2data);
5922 break;
5923
5924 replaced:
5925 if (beg < BEGV)
5926 beg = BEGV;
5927 IT_CHARPOS (*it) = beg;
5928 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5929 }
5930 }
5931
5932 it->continuation_lines_width = 0;
5933
5934 xassert (IT_CHARPOS (*it) >= BEGV);
5935 xassert (IT_CHARPOS (*it) == BEGV
5936 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5937 CHECK_IT (it);
5938 }
5939
5940
5941 /* Reseat iterator IT at the previous visible line start. Skip
5942 invisible text that is so either due to text properties or due to
5943 selective display. At the end, update IT's overlay information,
5944 face information etc. */
5945
5946 void
5947 reseat_at_previous_visible_line_start (struct it *it)
5948 {
5949 back_to_previous_visible_line_start (it);
5950 reseat (it, it->current.pos, 1);
5951 CHECK_IT (it);
5952 }
5953
5954
5955 /* Reseat iterator IT on the next visible line start in the current
5956 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5957 preceding the line start. Skip over invisible text that is so
5958 because of selective display. Compute faces, overlays etc at the
5959 new position. Note that this function does not skip over text that
5960 is invisible because of text properties. */
5961
5962 static void
5963 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5964 {
5965 int newline_found_p, skipped_p = 0;
5966 struct bidi_it bidi_it_prev;
5967
5968 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5969
5970 /* Skip over lines that are invisible because they are indented
5971 more than the value of IT->selective. */
5972 if (it->selective > 0)
5973 while (IT_CHARPOS (*it) < ZV
5974 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5975 it->selective))
5976 {
5977 xassert (IT_BYTEPOS (*it) == BEGV
5978 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5979 newline_found_p =
5980 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5981 }
5982
5983 /* Position on the newline if that's what's requested. */
5984 if (on_newline_p && newline_found_p)
5985 {
5986 if (STRINGP (it->string))
5987 {
5988 if (IT_STRING_CHARPOS (*it) > 0)
5989 {
5990 if (!it->bidi_p)
5991 {
5992 --IT_STRING_CHARPOS (*it);
5993 --IT_STRING_BYTEPOS (*it);
5994 }
5995 else
5996 {
5997 /* We need to restore the bidi iterator to the state
5998 it had on the newline, and resync the IT's
5999 position with that. */
6000 it->bidi_it = bidi_it_prev;
6001 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6003 }
6004 }
6005 }
6006 else if (IT_CHARPOS (*it) > BEGV)
6007 {
6008 if (!it->bidi_p)
6009 {
6010 --IT_CHARPOS (*it);
6011 --IT_BYTEPOS (*it);
6012 }
6013 else
6014 {
6015 /* We need to restore the bidi iterator to the state it
6016 had on the newline and resync IT with that. */
6017 it->bidi_it = bidi_it_prev;
6018 IT_CHARPOS (*it) = it->bidi_it.charpos;
6019 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6020 }
6021 reseat (it, it->current.pos, 0);
6022 }
6023 }
6024 else if (skipped_p)
6025 reseat (it, it->current.pos, 0);
6026
6027 CHECK_IT (it);
6028 }
6029
6030
6031 \f
6032 /***********************************************************************
6033 Changing an iterator's position
6034 ***********************************************************************/
6035
6036 /* Change IT's current position to POS in current_buffer. If FORCE_P
6037 is non-zero, always check for text properties at the new position.
6038 Otherwise, text properties are only looked up if POS >=
6039 IT->check_charpos of a property. */
6040
6041 static void
6042 reseat (struct it *it, struct text_pos pos, int force_p)
6043 {
6044 ptrdiff_t original_pos = IT_CHARPOS (*it);
6045
6046 reseat_1 (it, pos, 0);
6047
6048 /* Determine where to check text properties. Avoid doing it
6049 where possible because text property lookup is very expensive. */
6050 if (force_p
6051 || CHARPOS (pos) > it->stop_charpos
6052 || CHARPOS (pos) < original_pos)
6053 {
6054 if (it->bidi_p)
6055 {
6056 /* For bidi iteration, we need to prime prev_stop and
6057 base_level_stop with our best estimations. */
6058 /* Implementation note: Of course, POS is not necessarily a
6059 stop position, so assigning prev_pos to it is a lie; we
6060 should have called compute_stop_backwards. However, if
6061 the current buffer does not include any R2L characters,
6062 that call would be a waste of cycles, because the
6063 iterator will never move back, and thus never cross this
6064 "fake" stop position. So we delay that backward search
6065 until the time we really need it, in next_element_from_buffer. */
6066 if (CHARPOS (pos) != it->prev_stop)
6067 it->prev_stop = CHARPOS (pos);
6068 if (CHARPOS (pos) < it->base_level_stop)
6069 it->base_level_stop = 0; /* meaning it's unknown */
6070 handle_stop (it);
6071 }
6072 else
6073 {
6074 handle_stop (it);
6075 it->prev_stop = it->base_level_stop = 0;
6076 }
6077
6078 }
6079
6080 CHECK_IT (it);
6081 }
6082
6083
6084 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6085 IT->stop_pos to POS, also. */
6086
6087 static void
6088 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6089 {
6090 /* Don't call this function when scanning a C string. */
6091 xassert (it->s == NULL);
6092
6093 /* POS must be a reasonable value. */
6094 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6095
6096 it->current.pos = it->position = pos;
6097 it->end_charpos = ZV;
6098 it->dpvec = NULL;
6099 it->current.dpvec_index = -1;
6100 it->current.overlay_string_index = -1;
6101 IT_STRING_CHARPOS (*it) = -1;
6102 IT_STRING_BYTEPOS (*it) = -1;
6103 it->string = Qnil;
6104 it->method = GET_FROM_BUFFER;
6105 it->object = it->w->buffer;
6106 it->area = TEXT_AREA;
6107 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6108 it->sp = 0;
6109 it->string_from_display_prop_p = 0;
6110 it->from_disp_prop_p = 0;
6111 it->face_before_selective_p = 0;
6112 if (it->bidi_p)
6113 {
6114 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6115 &it->bidi_it);
6116 bidi_unshelve_cache (NULL, 0);
6117 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6118 it->bidi_it.string.s = NULL;
6119 it->bidi_it.string.lstring = Qnil;
6120 it->bidi_it.string.bufpos = 0;
6121 it->bidi_it.string.unibyte = 0;
6122 }
6123
6124 if (set_stop_p)
6125 {
6126 it->stop_charpos = CHARPOS (pos);
6127 it->base_level_stop = CHARPOS (pos);
6128 }
6129 }
6130
6131
6132 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6133 If S is non-null, it is a C string to iterate over. Otherwise,
6134 STRING gives a Lisp string to iterate over.
6135
6136 If PRECISION > 0, don't return more then PRECISION number of
6137 characters from the string.
6138
6139 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6140 characters have been returned. FIELD_WIDTH < 0 means an infinite
6141 field width.
6142
6143 MULTIBYTE = 0 means disable processing of multibyte characters,
6144 MULTIBYTE > 0 means enable it,
6145 MULTIBYTE < 0 means use IT->multibyte_p.
6146
6147 IT must be initialized via a prior call to init_iterator before
6148 calling this function. */
6149
6150 static void
6151 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6152 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6153 int multibyte)
6154 {
6155 /* No region in strings. */
6156 it->region_beg_charpos = it->region_end_charpos = -1;
6157
6158 /* No text property checks performed by default, but see below. */
6159 it->stop_charpos = -1;
6160
6161 /* Set iterator position and end position. */
6162 memset (&it->current, 0, sizeof it->current);
6163 it->current.overlay_string_index = -1;
6164 it->current.dpvec_index = -1;
6165 xassert (charpos >= 0);
6166
6167 /* If STRING is specified, use its multibyteness, otherwise use the
6168 setting of MULTIBYTE, if specified. */
6169 if (multibyte >= 0)
6170 it->multibyte_p = multibyte > 0;
6171
6172 /* Bidirectional reordering of strings is controlled by the default
6173 value of bidi-display-reordering. Don't try to reorder while
6174 loading loadup.el, as the necessary character property tables are
6175 not yet available. */
6176 it->bidi_p =
6177 NILP (Vpurify_flag)
6178 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6179
6180 if (s == NULL)
6181 {
6182 xassert (STRINGP (string));
6183 it->string = string;
6184 it->s = NULL;
6185 it->end_charpos = it->string_nchars = SCHARS (string);
6186 it->method = GET_FROM_STRING;
6187 it->current.string_pos = string_pos (charpos, string);
6188
6189 if (it->bidi_p)
6190 {
6191 it->bidi_it.string.lstring = string;
6192 it->bidi_it.string.s = NULL;
6193 it->bidi_it.string.schars = it->end_charpos;
6194 it->bidi_it.string.bufpos = 0;
6195 it->bidi_it.string.from_disp_str = 0;
6196 it->bidi_it.string.unibyte = !it->multibyte_p;
6197 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6198 FRAME_WINDOW_P (it->f), &it->bidi_it);
6199 }
6200 }
6201 else
6202 {
6203 it->s = (const unsigned char *) s;
6204 it->string = Qnil;
6205
6206 /* Note that we use IT->current.pos, not it->current.string_pos,
6207 for displaying C strings. */
6208 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6209 if (it->multibyte_p)
6210 {
6211 it->current.pos = c_string_pos (charpos, s, 1);
6212 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6213 }
6214 else
6215 {
6216 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6217 it->end_charpos = it->string_nchars = strlen (s);
6218 }
6219
6220 if (it->bidi_p)
6221 {
6222 it->bidi_it.string.lstring = Qnil;
6223 it->bidi_it.string.s = (const unsigned char *) s;
6224 it->bidi_it.string.schars = it->end_charpos;
6225 it->bidi_it.string.bufpos = 0;
6226 it->bidi_it.string.from_disp_str = 0;
6227 it->bidi_it.string.unibyte = !it->multibyte_p;
6228 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6229 &it->bidi_it);
6230 }
6231 it->method = GET_FROM_C_STRING;
6232 }
6233
6234 /* PRECISION > 0 means don't return more than PRECISION characters
6235 from the string. */
6236 if (precision > 0 && it->end_charpos - charpos > precision)
6237 {
6238 it->end_charpos = it->string_nchars = charpos + precision;
6239 if (it->bidi_p)
6240 it->bidi_it.string.schars = it->end_charpos;
6241 }
6242
6243 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6244 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6245 FIELD_WIDTH < 0 means infinite field width. This is useful for
6246 padding with `-' at the end of a mode line. */
6247 if (field_width < 0)
6248 field_width = INFINITY;
6249 /* Implementation note: We deliberately don't enlarge
6250 it->bidi_it.string.schars here to fit it->end_charpos, because
6251 the bidi iterator cannot produce characters out of thin air. */
6252 if (field_width > it->end_charpos - charpos)
6253 it->end_charpos = charpos + field_width;
6254
6255 /* Use the standard display table for displaying strings. */
6256 if (DISP_TABLE_P (Vstandard_display_table))
6257 it->dp = XCHAR_TABLE (Vstandard_display_table);
6258
6259 it->stop_charpos = charpos;
6260 it->prev_stop = charpos;
6261 it->base_level_stop = 0;
6262 if (it->bidi_p)
6263 {
6264 it->bidi_it.first_elt = 1;
6265 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6266 it->bidi_it.disp_pos = -1;
6267 }
6268 if (s == NULL && it->multibyte_p)
6269 {
6270 ptrdiff_t endpos = SCHARS (it->string);
6271 if (endpos > it->end_charpos)
6272 endpos = it->end_charpos;
6273 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6274 it->string);
6275 }
6276 CHECK_IT (it);
6277 }
6278
6279
6280 \f
6281 /***********************************************************************
6282 Iteration
6283 ***********************************************************************/
6284
6285 /* Map enum it_method value to corresponding next_element_from_* function. */
6286
6287 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6288 {
6289 next_element_from_buffer,
6290 next_element_from_display_vector,
6291 next_element_from_string,
6292 next_element_from_c_string,
6293 next_element_from_image,
6294 next_element_from_stretch
6295 };
6296
6297 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6298
6299
6300 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6301 (possibly with the following characters). */
6302
6303 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6304 ((IT)->cmp_it.id >= 0 \
6305 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6306 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6307 END_CHARPOS, (IT)->w, \
6308 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6309 (IT)->string)))
6310
6311
6312 /* Lookup the char-table Vglyphless_char_display for character C (-1
6313 if we want information for no-font case), and return the display
6314 method symbol. By side-effect, update it->what and
6315 it->glyphless_method. This function is called from
6316 get_next_display_element for each character element, and from
6317 x_produce_glyphs when no suitable font was found. */
6318
6319 Lisp_Object
6320 lookup_glyphless_char_display (int c, struct it *it)
6321 {
6322 Lisp_Object glyphless_method = Qnil;
6323
6324 if (CHAR_TABLE_P (Vglyphless_char_display)
6325 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6326 {
6327 if (c >= 0)
6328 {
6329 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6330 if (CONSP (glyphless_method))
6331 glyphless_method = FRAME_WINDOW_P (it->f)
6332 ? XCAR (glyphless_method)
6333 : XCDR (glyphless_method);
6334 }
6335 else
6336 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6337 }
6338
6339 retry:
6340 if (NILP (glyphless_method))
6341 {
6342 if (c >= 0)
6343 /* The default is to display the character by a proper font. */
6344 return Qnil;
6345 /* The default for the no-font case is to display an empty box. */
6346 glyphless_method = Qempty_box;
6347 }
6348 if (EQ (glyphless_method, Qzero_width))
6349 {
6350 if (c >= 0)
6351 return glyphless_method;
6352 /* This method can't be used for the no-font case. */
6353 glyphless_method = Qempty_box;
6354 }
6355 if (EQ (glyphless_method, Qthin_space))
6356 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6357 else if (EQ (glyphless_method, Qempty_box))
6358 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6359 else if (EQ (glyphless_method, Qhex_code))
6360 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6361 else if (STRINGP (glyphless_method))
6362 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6363 else
6364 {
6365 /* Invalid value. We use the default method. */
6366 glyphless_method = Qnil;
6367 goto retry;
6368 }
6369 it->what = IT_GLYPHLESS;
6370 return glyphless_method;
6371 }
6372
6373 /* Load IT's display element fields with information about the next
6374 display element from the current position of IT. Value is zero if
6375 end of buffer (or C string) is reached. */
6376
6377 static struct frame *last_escape_glyph_frame = NULL;
6378 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6379 static int last_escape_glyph_merged_face_id = 0;
6380
6381 struct frame *last_glyphless_glyph_frame = NULL;
6382 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6383 int last_glyphless_glyph_merged_face_id = 0;
6384
6385 static int
6386 get_next_display_element (struct it *it)
6387 {
6388 /* Non-zero means that we found a display element. Zero means that
6389 we hit the end of what we iterate over. Performance note: the
6390 function pointer `method' used here turns out to be faster than
6391 using a sequence of if-statements. */
6392 int success_p;
6393
6394 get_next:
6395 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6396
6397 if (it->what == IT_CHARACTER)
6398 {
6399 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6400 and only if (a) the resolved directionality of that character
6401 is R..." */
6402 /* FIXME: Do we need an exception for characters from display
6403 tables? */
6404 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6405 it->c = bidi_mirror_char (it->c);
6406 /* Map via display table or translate control characters.
6407 IT->c, IT->len etc. have been set to the next character by
6408 the function call above. If we have a display table, and it
6409 contains an entry for IT->c, translate it. Don't do this if
6410 IT->c itself comes from a display table, otherwise we could
6411 end up in an infinite recursion. (An alternative could be to
6412 count the recursion depth of this function and signal an
6413 error when a certain maximum depth is reached.) Is it worth
6414 it? */
6415 if (success_p && it->dpvec == NULL)
6416 {
6417 Lisp_Object dv;
6418 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6419 int nonascii_space_p = 0;
6420 int nonascii_hyphen_p = 0;
6421 int c = it->c; /* This is the character to display. */
6422
6423 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6424 {
6425 xassert (SINGLE_BYTE_CHAR_P (c));
6426 if (unibyte_display_via_language_environment)
6427 {
6428 c = DECODE_CHAR (unibyte, c);
6429 if (c < 0)
6430 c = BYTE8_TO_CHAR (it->c);
6431 }
6432 else
6433 c = BYTE8_TO_CHAR (it->c);
6434 }
6435
6436 if (it->dp
6437 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6438 VECTORP (dv)))
6439 {
6440 struct Lisp_Vector *v = XVECTOR (dv);
6441
6442 /* Return the first character from the display table
6443 entry, if not empty. If empty, don't display the
6444 current character. */
6445 if (v->header.size)
6446 {
6447 it->dpvec_char_len = it->len;
6448 it->dpvec = v->contents;
6449 it->dpend = v->contents + v->header.size;
6450 it->current.dpvec_index = 0;
6451 it->dpvec_face_id = -1;
6452 it->saved_face_id = it->face_id;
6453 it->method = GET_FROM_DISPLAY_VECTOR;
6454 it->ellipsis_p = 0;
6455 }
6456 else
6457 {
6458 set_iterator_to_next (it, 0);
6459 }
6460 goto get_next;
6461 }
6462
6463 if (! NILP (lookup_glyphless_char_display (c, it)))
6464 {
6465 if (it->what == IT_GLYPHLESS)
6466 goto done;
6467 /* Don't display this character. */
6468 set_iterator_to_next (it, 0);
6469 goto get_next;
6470 }
6471
6472 /* If `nobreak-char-display' is non-nil, we display
6473 non-ASCII spaces and hyphens specially. */
6474 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6475 {
6476 if (c == 0xA0)
6477 nonascii_space_p = 1;
6478 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6479 nonascii_hyphen_p = 1;
6480 }
6481
6482 /* Translate control characters into `\003' or `^C' form.
6483 Control characters coming from a display table entry are
6484 currently not translated because we use IT->dpvec to hold
6485 the translation. This could easily be changed but I
6486 don't believe that it is worth doing.
6487
6488 The characters handled by `nobreak-char-display' must be
6489 translated too.
6490
6491 Non-printable characters and raw-byte characters are also
6492 translated to octal form. */
6493 if (((c < ' ' || c == 127) /* ASCII control chars */
6494 ? (it->area != TEXT_AREA
6495 /* In mode line, treat \n, \t like other crl chars. */
6496 || (c != '\t'
6497 && it->glyph_row
6498 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6499 || (c != '\n' && c != '\t'))
6500 : (nonascii_space_p
6501 || nonascii_hyphen_p
6502 || CHAR_BYTE8_P (c)
6503 || ! CHAR_PRINTABLE_P (c))))
6504 {
6505 /* C is a control character, non-ASCII space/hyphen,
6506 raw-byte, or a non-printable character which must be
6507 displayed either as '\003' or as `^C' where the '\\'
6508 and '^' can be defined in the display table. Fill
6509 IT->ctl_chars with glyphs for what we have to
6510 display. Then, set IT->dpvec to these glyphs. */
6511 Lisp_Object gc;
6512 int ctl_len;
6513 int face_id;
6514 int lface_id = 0;
6515 int escape_glyph;
6516
6517 /* Handle control characters with ^. */
6518
6519 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6520 {
6521 int g;
6522
6523 g = '^'; /* default glyph for Control */
6524 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6525 if (it->dp
6526 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6527 {
6528 g = GLYPH_CODE_CHAR (gc);
6529 lface_id = GLYPH_CODE_FACE (gc);
6530 }
6531 if (lface_id)
6532 {
6533 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6534 }
6535 else if (it->f == last_escape_glyph_frame
6536 && it->face_id == last_escape_glyph_face_id)
6537 {
6538 face_id = last_escape_glyph_merged_face_id;
6539 }
6540 else
6541 {
6542 /* Merge the escape-glyph face into the current face. */
6543 face_id = merge_faces (it->f, Qescape_glyph, 0,
6544 it->face_id);
6545 last_escape_glyph_frame = it->f;
6546 last_escape_glyph_face_id = it->face_id;
6547 last_escape_glyph_merged_face_id = face_id;
6548 }
6549
6550 XSETINT (it->ctl_chars[0], g);
6551 XSETINT (it->ctl_chars[1], c ^ 0100);
6552 ctl_len = 2;
6553 goto display_control;
6554 }
6555
6556 /* Handle non-ascii space in the mode where it only gets
6557 highlighting. */
6558
6559 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6560 {
6561 /* Merge `nobreak-space' into the current face. */
6562 face_id = merge_faces (it->f, Qnobreak_space, 0,
6563 it->face_id);
6564 XSETINT (it->ctl_chars[0], ' ');
6565 ctl_len = 1;
6566 goto display_control;
6567 }
6568
6569 /* Handle sequences that start with the "escape glyph". */
6570
6571 /* the default escape glyph is \. */
6572 escape_glyph = '\\';
6573
6574 if (it->dp
6575 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6576 {
6577 escape_glyph = GLYPH_CODE_CHAR (gc);
6578 lface_id = GLYPH_CODE_FACE (gc);
6579 }
6580 if (lface_id)
6581 {
6582 /* The display table specified a face.
6583 Merge it into face_id and also into escape_glyph. */
6584 face_id = merge_faces (it->f, Qt, lface_id,
6585 it->face_id);
6586 }
6587 else if (it->f == last_escape_glyph_frame
6588 && it->face_id == last_escape_glyph_face_id)
6589 {
6590 face_id = last_escape_glyph_merged_face_id;
6591 }
6592 else
6593 {
6594 /* Merge the escape-glyph face into the current face. */
6595 face_id = merge_faces (it->f, Qescape_glyph, 0,
6596 it->face_id);
6597 last_escape_glyph_frame = it->f;
6598 last_escape_glyph_face_id = it->face_id;
6599 last_escape_glyph_merged_face_id = face_id;
6600 }
6601
6602 /* Draw non-ASCII hyphen with just highlighting: */
6603
6604 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6605 {
6606 XSETINT (it->ctl_chars[0], '-');
6607 ctl_len = 1;
6608 goto display_control;
6609 }
6610
6611 /* Draw non-ASCII space/hyphen with escape glyph: */
6612
6613 if (nonascii_space_p || nonascii_hyphen_p)
6614 {
6615 XSETINT (it->ctl_chars[0], escape_glyph);
6616 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6617 ctl_len = 2;
6618 goto display_control;
6619 }
6620
6621 {
6622 char str[10];
6623 int len, i;
6624
6625 if (CHAR_BYTE8_P (c))
6626 /* Display \200 instead of \17777600. */
6627 c = CHAR_TO_BYTE8 (c);
6628 len = sprintf (str, "%03o", c);
6629
6630 XSETINT (it->ctl_chars[0], escape_glyph);
6631 for (i = 0; i < len; i++)
6632 XSETINT (it->ctl_chars[i + 1], str[i]);
6633 ctl_len = len + 1;
6634 }
6635
6636 display_control:
6637 /* Set up IT->dpvec and return first character from it. */
6638 it->dpvec_char_len = it->len;
6639 it->dpvec = it->ctl_chars;
6640 it->dpend = it->dpvec + ctl_len;
6641 it->current.dpvec_index = 0;
6642 it->dpvec_face_id = face_id;
6643 it->saved_face_id = it->face_id;
6644 it->method = GET_FROM_DISPLAY_VECTOR;
6645 it->ellipsis_p = 0;
6646 goto get_next;
6647 }
6648 it->char_to_display = c;
6649 }
6650 else if (success_p)
6651 {
6652 it->char_to_display = it->c;
6653 }
6654 }
6655
6656 /* Adjust face id for a multibyte character. There are no multibyte
6657 character in unibyte text. */
6658 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6659 && it->multibyte_p
6660 && success_p
6661 && FRAME_WINDOW_P (it->f))
6662 {
6663 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6664
6665 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6666 {
6667 /* Automatic composition with glyph-string. */
6668 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6669
6670 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6671 }
6672 else
6673 {
6674 ptrdiff_t pos = (it->s ? -1
6675 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6676 : IT_CHARPOS (*it));
6677 int c;
6678
6679 if (it->what == IT_CHARACTER)
6680 c = it->char_to_display;
6681 else
6682 {
6683 struct composition *cmp = composition_table[it->cmp_it.id];
6684 int i;
6685
6686 c = ' ';
6687 for (i = 0; i < cmp->glyph_len; i++)
6688 /* TAB in a composition means display glyphs with
6689 padding space on the left or right. */
6690 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6691 break;
6692 }
6693 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6694 }
6695 }
6696
6697 done:
6698 /* Is this character the last one of a run of characters with
6699 box? If yes, set IT->end_of_box_run_p to 1. */
6700 if (it->face_box_p
6701 && it->s == NULL)
6702 {
6703 if (it->method == GET_FROM_STRING && it->sp)
6704 {
6705 int face_id = underlying_face_id (it);
6706 struct face *face = FACE_FROM_ID (it->f, face_id);
6707
6708 if (face)
6709 {
6710 if (face->box == FACE_NO_BOX)
6711 {
6712 /* If the box comes from face properties in a
6713 display string, check faces in that string. */
6714 int string_face_id = face_after_it_pos (it);
6715 it->end_of_box_run_p
6716 = (FACE_FROM_ID (it->f, string_face_id)->box
6717 == FACE_NO_BOX);
6718 }
6719 /* Otherwise, the box comes from the underlying face.
6720 If this is the last string character displayed, check
6721 the next buffer location. */
6722 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6723 && (it->current.overlay_string_index
6724 == it->n_overlay_strings - 1))
6725 {
6726 ptrdiff_t ignore;
6727 int next_face_id;
6728 struct text_pos pos = it->current.pos;
6729 INC_TEXT_POS (pos, it->multibyte_p);
6730
6731 next_face_id = face_at_buffer_position
6732 (it->w, CHARPOS (pos), it->region_beg_charpos,
6733 it->region_end_charpos, &ignore,
6734 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6735 -1);
6736 it->end_of_box_run_p
6737 = (FACE_FROM_ID (it->f, next_face_id)->box
6738 == FACE_NO_BOX);
6739 }
6740 }
6741 }
6742 else
6743 {
6744 int face_id = face_after_it_pos (it);
6745 it->end_of_box_run_p
6746 = (face_id != it->face_id
6747 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6748 }
6749 }
6750
6751 /* Value is 0 if end of buffer or string reached. */
6752 return success_p;
6753 }
6754
6755
6756 /* Move IT to the next display element.
6757
6758 RESEAT_P non-zero means if called on a newline in buffer text,
6759 skip to the next visible line start.
6760
6761 Functions get_next_display_element and set_iterator_to_next are
6762 separate because I find this arrangement easier to handle than a
6763 get_next_display_element function that also increments IT's
6764 position. The way it is we can first look at an iterator's current
6765 display element, decide whether it fits on a line, and if it does,
6766 increment the iterator position. The other way around we probably
6767 would either need a flag indicating whether the iterator has to be
6768 incremented the next time, or we would have to implement a
6769 decrement position function which would not be easy to write. */
6770
6771 void
6772 set_iterator_to_next (struct it *it, int reseat_p)
6773 {
6774 /* Reset flags indicating start and end of a sequence of characters
6775 with box. Reset them at the start of this function because
6776 moving the iterator to a new position might set them. */
6777 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6778
6779 switch (it->method)
6780 {
6781 case GET_FROM_BUFFER:
6782 /* The current display element of IT is a character from
6783 current_buffer. Advance in the buffer, and maybe skip over
6784 invisible lines that are so because of selective display. */
6785 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6786 reseat_at_next_visible_line_start (it, 0);
6787 else if (it->cmp_it.id >= 0)
6788 {
6789 /* We are currently getting glyphs from a composition. */
6790 int i;
6791
6792 if (! it->bidi_p)
6793 {
6794 IT_CHARPOS (*it) += it->cmp_it.nchars;
6795 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6796 if (it->cmp_it.to < it->cmp_it.nglyphs)
6797 {
6798 it->cmp_it.from = it->cmp_it.to;
6799 }
6800 else
6801 {
6802 it->cmp_it.id = -1;
6803 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6804 IT_BYTEPOS (*it),
6805 it->end_charpos, Qnil);
6806 }
6807 }
6808 else if (! it->cmp_it.reversed_p)
6809 {
6810 /* Composition created while scanning forward. */
6811 /* Update IT's char/byte positions to point to the first
6812 character of the next grapheme cluster, or to the
6813 character visually after the current composition. */
6814 for (i = 0; i < it->cmp_it.nchars; i++)
6815 bidi_move_to_visually_next (&it->bidi_it);
6816 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6817 IT_CHARPOS (*it) = it->bidi_it.charpos;
6818
6819 if (it->cmp_it.to < it->cmp_it.nglyphs)
6820 {
6821 /* Proceed to the next grapheme cluster. */
6822 it->cmp_it.from = it->cmp_it.to;
6823 }
6824 else
6825 {
6826 /* No more grapheme clusters in this composition.
6827 Find the next stop position. */
6828 ptrdiff_t stop = it->end_charpos;
6829 if (it->bidi_it.scan_dir < 0)
6830 /* Now we are scanning backward and don't know
6831 where to stop. */
6832 stop = -1;
6833 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6834 IT_BYTEPOS (*it), stop, Qnil);
6835 }
6836 }
6837 else
6838 {
6839 /* Composition created while scanning backward. */
6840 /* Update IT's char/byte positions to point to the last
6841 character of the previous grapheme cluster, or the
6842 character visually after the current composition. */
6843 for (i = 0; i < it->cmp_it.nchars; i++)
6844 bidi_move_to_visually_next (&it->bidi_it);
6845 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6846 IT_CHARPOS (*it) = it->bidi_it.charpos;
6847 if (it->cmp_it.from > 0)
6848 {
6849 /* Proceed to the previous grapheme cluster. */
6850 it->cmp_it.to = it->cmp_it.from;
6851 }
6852 else
6853 {
6854 /* No more grapheme clusters in this composition.
6855 Find the next stop position. */
6856 ptrdiff_t stop = it->end_charpos;
6857 if (it->bidi_it.scan_dir < 0)
6858 /* Now we are scanning backward and don't know
6859 where to stop. */
6860 stop = -1;
6861 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6862 IT_BYTEPOS (*it), stop, Qnil);
6863 }
6864 }
6865 }
6866 else
6867 {
6868 xassert (it->len != 0);
6869
6870 if (!it->bidi_p)
6871 {
6872 IT_BYTEPOS (*it) += it->len;
6873 IT_CHARPOS (*it) += 1;
6874 }
6875 else
6876 {
6877 int prev_scan_dir = it->bidi_it.scan_dir;
6878 /* If this is a new paragraph, determine its base
6879 direction (a.k.a. its base embedding level). */
6880 if (it->bidi_it.new_paragraph)
6881 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6882 bidi_move_to_visually_next (&it->bidi_it);
6883 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6884 IT_CHARPOS (*it) = it->bidi_it.charpos;
6885 if (prev_scan_dir != it->bidi_it.scan_dir)
6886 {
6887 /* As the scan direction was changed, we must
6888 re-compute the stop position for composition. */
6889 ptrdiff_t stop = it->end_charpos;
6890 if (it->bidi_it.scan_dir < 0)
6891 stop = -1;
6892 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6893 IT_BYTEPOS (*it), stop, Qnil);
6894 }
6895 }
6896 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6897 }
6898 break;
6899
6900 case GET_FROM_C_STRING:
6901 /* Current display element of IT is from a C string. */
6902 if (!it->bidi_p
6903 /* If the string position is beyond string's end, it means
6904 next_element_from_c_string is padding the string with
6905 blanks, in which case we bypass the bidi iterator,
6906 because it cannot deal with such virtual characters. */
6907 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6908 {
6909 IT_BYTEPOS (*it) += it->len;
6910 IT_CHARPOS (*it) += 1;
6911 }
6912 else
6913 {
6914 bidi_move_to_visually_next (&it->bidi_it);
6915 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6916 IT_CHARPOS (*it) = it->bidi_it.charpos;
6917 }
6918 break;
6919
6920 case GET_FROM_DISPLAY_VECTOR:
6921 /* Current display element of IT is from a display table entry.
6922 Advance in the display table definition. Reset it to null if
6923 end reached, and continue with characters from buffers/
6924 strings. */
6925 ++it->current.dpvec_index;
6926
6927 /* Restore face of the iterator to what they were before the
6928 display vector entry (these entries may contain faces). */
6929 it->face_id = it->saved_face_id;
6930
6931 if (it->dpvec + it->current.dpvec_index == it->dpend)
6932 {
6933 int recheck_faces = it->ellipsis_p;
6934
6935 if (it->s)
6936 it->method = GET_FROM_C_STRING;
6937 else if (STRINGP (it->string))
6938 it->method = GET_FROM_STRING;
6939 else
6940 {
6941 it->method = GET_FROM_BUFFER;
6942 it->object = it->w->buffer;
6943 }
6944
6945 it->dpvec = NULL;
6946 it->current.dpvec_index = -1;
6947
6948 /* Skip over characters which were displayed via IT->dpvec. */
6949 if (it->dpvec_char_len < 0)
6950 reseat_at_next_visible_line_start (it, 1);
6951 else if (it->dpvec_char_len > 0)
6952 {
6953 if (it->method == GET_FROM_STRING
6954 && it->n_overlay_strings > 0)
6955 it->ignore_overlay_strings_at_pos_p = 1;
6956 it->len = it->dpvec_char_len;
6957 set_iterator_to_next (it, reseat_p);
6958 }
6959
6960 /* Maybe recheck faces after display vector */
6961 if (recheck_faces)
6962 it->stop_charpos = IT_CHARPOS (*it);
6963 }
6964 break;
6965
6966 case GET_FROM_STRING:
6967 /* Current display element is a character from a Lisp string. */
6968 xassert (it->s == NULL && STRINGP (it->string));
6969 if (it->cmp_it.id >= 0)
6970 {
6971 int i;
6972
6973 if (! it->bidi_p)
6974 {
6975 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6976 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6977 if (it->cmp_it.to < it->cmp_it.nglyphs)
6978 it->cmp_it.from = it->cmp_it.to;
6979 else
6980 {
6981 it->cmp_it.id = -1;
6982 composition_compute_stop_pos (&it->cmp_it,
6983 IT_STRING_CHARPOS (*it),
6984 IT_STRING_BYTEPOS (*it),
6985 it->end_charpos, it->string);
6986 }
6987 }
6988 else if (! it->cmp_it.reversed_p)
6989 {
6990 for (i = 0; i < it->cmp_it.nchars; i++)
6991 bidi_move_to_visually_next (&it->bidi_it);
6992 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6993 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6994
6995 if (it->cmp_it.to < it->cmp_it.nglyphs)
6996 it->cmp_it.from = it->cmp_it.to;
6997 else
6998 {
6999 ptrdiff_t stop = it->end_charpos;
7000 if (it->bidi_it.scan_dir < 0)
7001 stop = -1;
7002 composition_compute_stop_pos (&it->cmp_it,
7003 IT_STRING_CHARPOS (*it),
7004 IT_STRING_BYTEPOS (*it), stop,
7005 it->string);
7006 }
7007 }
7008 else
7009 {
7010 for (i = 0; i < it->cmp_it.nchars; i++)
7011 bidi_move_to_visually_next (&it->bidi_it);
7012 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7013 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7014 if (it->cmp_it.from > 0)
7015 it->cmp_it.to = it->cmp_it.from;
7016 else
7017 {
7018 ptrdiff_t stop = it->end_charpos;
7019 if (it->bidi_it.scan_dir < 0)
7020 stop = -1;
7021 composition_compute_stop_pos (&it->cmp_it,
7022 IT_STRING_CHARPOS (*it),
7023 IT_STRING_BYTEPOS (*it), stop,
7024 it->string);
7025 }
7026 }
7027 }
7028 else
7029 {
7030 if (!it->bidi_p
7031 /* If the string position is beyond string's end, it
7032 means next_element_from_string is padding the string
7033 with blanks, in which case we bypass the bidi
7034 iterator, because it cannot deal with such virtual
7035 characters. */
7036 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7037 {
7038 IT_STRING_BYTEPOS (*it) += it->len;
7039 IT_STRING_CHARPOS (*it) += 1;
7040 }
7041 else
7042 {
7043 int prev_scan_dir = it->bidi_it.scan_dir;
7044
7045 bidi_move_to_visually_next (&it->bidi_it);
7046 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7047 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7048 if (prev_scan_dir != it->bidi_it.scan_dir)
7049 {
7050 ptrdiff_t stop = it->end_charpos;
7051
7052 if (it->bidi_it.scan_dir < 0)
7053 stop = -1;
7054 composition_compute_stop_pos (&it->cmp_it,
7055 IT_STRING_CHARPOS (*it),
7056 IT_STRING_BYTEPOS (*it), stop,
7057 it->string);
7058 }
7059 }
7060 }
7061
7062 consider_string_end:
7063
7064 if (it->current.overlay_string_index >= 0)
7065 {
7066 /* IT->string is an overlay string. Advance to the
7067 next, if there is one. */
7068 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7069 {
7070 it->ellipsis_p = 0;
7071 next_overlay_string (it);
7072 if (it->ellipsis_p)
7073 setup_for_ellipsis (it, 0);
7074 }
7075 }
7076 else
7077 {
7078 /* IT->string is not an overlay string. If we reached
7079 its end, and there is something on IT->stack, proceed
7080 with what is on the stack. This can be either another
7081 string, this time an overlay string, or a buffer. */
7082 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7083 && it->sp > 0)
7084 {
7085 pop_it (it);
7086 if (it->method == GET_FROM_STRING)
7087 goto consider_string_end;
7088 }
7089 }
7090 break;
7091
7092 case GET_FROM_IMAGE:
7093 case GET_FROM_STRETCH:
7094 /* The position etc with which we have to proceed are on
7095 the stack. The position may be at the end of a string,
7096 if the `display' property takes up the whole string. */
7097 xassert (it->sp > 0);
7098 pop_it (it);
7099 if (it->method == GET_FROM_STRING)
7100 goto consider_string_end;
7101 break;
7102
7103 default:
7104 /* There are no other methods defined, so this should be a bug. */
7105 abort ();
7106 }
7107
7108 xassert (it->method != GET_FROM_STRING
7109 || (STRINGP (it->string)
7110 && IT_STRING_CHARPOS (*it) >= 0));
7111 }
7112
7113 /* Load IT's display element fields with information about the next
7114 display element which comes from a display table entry or from the
7115 result of translating a control character to one of the forms `^C'
7116 or `\003'.
7117
7118 IT->dpvec holds the glyphs to return as characters.
7119 IT->saved_face_id holds the face id before the display vector--it
7120 is restored into IT->face_id in set_iterator_to_next. */
7121
7122 static int
7123 next_element_from_display_vector (struct it *it)
7124 {
7125 Lisp_Object gc;
7126
7127 /* Precondition. */
7128 xassert (it->dpvec && it->current.dpvec_index >= 0);
7129
7130 it->face_id = it->saved_face_id;
7131
7132 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7133 That seemed totally bogus - so I changed it... */
7134 gc = it->dpvec[it->current.dpvec_index];
7135
7136 if (GLYPH_CODE_P (gc))
7137 {
7138 it->c = GLYPH_CODE_CHAR (gc);
7139 it->len = CHAR_BYTES (it->c);
7140
7141 /* The entry may contain a face id to use. Such a face id is
7142 the id of a Lisp face, not a realized face. A face id of
7143 zero means no face is specified. */
7144 if (it->dpvec_face_id >= 0)
7145 it->face_id = it->dpvec_face_id;
7146 else
7147 {
7148 int lface_id = GLYPH_CODE_FACE (gc);
7149 if (lface_id > 0)
7150 it->face_id = merge_faces (it->f, Qt, lface_id,
7151 it->saved_face_id);
7152 }
7153 }
7154 else
7155 /* Display table entry is invalid. Return a space. */
7156 it->c = ' ', it->len = 1;
7157
7158 /* Don't change position and object of the iterator here. They are
7159 still the values of the character that had this display table
7160 entry or was translated, and that's what we want. */
7161 it->what = IT_CHARACTER;
7162 return 1;
7163 }
7164
7165 /* Get the first element of string/buffer in the visual order, after
7166 being reseated to a new position in a string or a buffer. */
7167 static void
7168 get_visually_first_element (struct it *it)
7169 {
7170 int string_p = STRINGP (it->string) || it->s;
7171 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7172 ptrdiff_t bob = (string_p ? 0 : BEGV);
7173
7174 if (STRINGP (it->string))
7175 {
7176 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7177 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7178 }
7179 else
7180 {
7181 it->bidi_it.charpos = IT_CHARPOS (*it);
7182 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7183 }
7184
7185 if (it->bidi_it.charpos == eob)
7186 {
7187 /* Nothing to do, but reset the FIRST_ELT flag, like
7188 bidi_paragraph_init does, because we are not going to
7189 call it. */
7190 it->bidi_it.first_elt = 0;
7191 }
7192 else if (it->bidi_it.charpos == bob
7193 || (!string_p
7194 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7195 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7196 {
7197 /* If we are at the beginning of a line/string, we can produce
7198 the next element right away. */
7199 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7200 bidi_move_to_visually_next (&it->bidi_it);
7201 }
7202 else
7203 {
7204 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7205
7206 /* We need to prime the bidi iterator starting at the line's or
7207 string's beginning, before we will be able to produce the
7208 next element. */
7209 if (string_p)
7210 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7211 else
7212 {
7213 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7214 -1);
7215 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7216 }
7217 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7218 do
7219 {
7220 /* Now return to buffer/string position where we were asked
7221 to get the next display element, and produce that. */
7222 bidi_move_to_visually_next (&it->bidi_it);
7223 }
7224 while (it->bidi_it.bytepos != orig_bytepos
7225 && it->bidi_it.charpos < eob);
7226 }
7227
7228 /* Adjust IT's position information to where we ended up. */
7229 if (STRINGP (it->string))
7230 {
7231 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7232 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 }
7234 else
7235 {
7236 IT_CHARPOS (*it) = it->bidi_it.charpos;
7237 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7238 }
7239
7240 if (STRINGP (it->string) || !it->s)
7241 {
7242 ptrdiff_t stop, charpos, bytepos;
7243
7244 if (STRINGP (it->string))
7245 {
7246 xassert (!it->s);
7247 stop = SCHARS (it->string);
7248 if (stop > it->end_charpos)
7249 stop = it->end_charpos;
7250 charpos = IT_STRING_CHARPOS (*it);
7251 bytepos = IT_STRING_BYTEPOS (*it);
7252 }
7253 else
7254 {
7255 stop = it->end_charpos;
7256 charpos = IT_CHARPOS (*it);
7257 bytepos = IT_BYTEPOS (*it);
7258 }
7259 if (it->bidi_it.scan_dir < 0)
7260 stop = -1;
7261 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7262 it->string);
7263 }
7264 }
7265
7266 /* Load IT with the next display element from Lisp string IT->string.
7267 IT->current.string_pos is the current position within the string.
7268 If IT->current.overlay_string_index >= 0, the Lisp string is an
7269 overlay string. */
7270
7271 static int
7272 next_element_from_string (struct it *it)
7273 {
7274 struct text_pos position;
7275
7276 xassert (STRINGP (it->string));
7277 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7278 xassert (IT_STRING_CHARPOS (*it) >= 0);
7279 position = it->current.string_pos;
7280
7281 /* With bidi reordering, the character to display might not be the
7282 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7283 that we were reseat()ed to a new string, whose paragraph
7284 direction is not known. */
7285 if (it->bidi_p && it->bidi_it.first_elt)
7286 {
7287 get_visually_first_element (it);
7288 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7289 }
7290
7291 /* Time to check for invisible text? */
7292 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7293 {
7294 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7295 {
7296 if (!(!it->bidi_p
7297 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7298 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7299 {
7300 /* With bidi non-linear iteration, we could find
7301 ourselves far beyond the last computed stop_charpos,
7302 with several other stop positions in between that we
7303 missed. Scan them all now, in buffer's logical
7304 order, until we find and handle the last stop_charpos
7305 that precedes our current position. */
7306 handle_stop_backwards (it, it->stop_charpos);
7307 return GET_NEXT_DISPLAY_ELEMENT (it);
7308 }
7309 else
7310 {
7311 if (it->bidi_p)
7312 {
7313 /* Take note of the stop position we just moved
7314 across, for when we will move back across it. */
7315 it->prev_stop = it->stop_charpos;
7316 /* If we are at base paragraph embedding level, take
7317 note of the last stop position seen at this
7318 level. */
7319 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7320 it->base_level_stop = it->stop_charpos;
7321 }
7322 handle_stop (it);
7323
7324 /* Since a handler may have changed IT->method, we must
7325 recurse here. */
7326 return GET_NEXT_DISPLAY_ELEMENT (it);
7327 }
7328 }
7329 else if (it->bidi_p
7330 /* If we are before prev_stop, we may have overstepped
7331 on our way backwards a stop_pos, and if so, we need
7332 to handle that stop_pos. */
7333 && IT_STRING_CHARPOS (*it) < it->prev_stop
7334 /* We can sometimes back up for reasons that have nothing
7335 to do with bidi reordering. E.g., compositions. The
7336 code below is only needed when we are above the base
7337 embedding level, so test for that explicitly. */
7338 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7339 {
7340 /* If we lost track of base_level_stop, we have no better
7341 place for handle_stop_backwards to start from than string
7342 beginning. This happens, e.g., when we were reseated to
7343 the previous screenful of text by vertical-motion. */
7344 if (it->base_level_stop <= 0
7345 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7346 it->base_level_stop = 0;
7347 handle_stop_backwards (it, it->base_level_stop);
7348 return GET_NEXT_DISPLAY_ELEMENT (it);
7349 }
7350 }
7351
7352 if (it->current.overlay_string_index >= 0)
7353 {
7354 /* Get the next character from an overlay string. In overlay
7355 strings, There is no field width or padding with spaces to
7356 do. */
7357 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7358 {
7359 it->what = IT_EOB;
7360 return 0;
7361 }
7362 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7363 IT_STRING_BYTEPOS (*it),
7364 it->bidi_it.scan_dir < 0
7365 ? -1
7366 : SCHARS (it->string))
7367 && next_element_from_composition (it))
7368 {
7369 return 1;
7370 }
7371 else if (STRING_MULTIBYTE (it->string))
7372 {
7373 const unsigned char *s = (SDATA (it->string)
7374 + IT_STRING_BYTEPOS (*it));
7375 it->c = string_char_and_length (s, &it->len);
7376 }
7377 else
7378 {
7379 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7380 it->len = 1;
7381 }
7382 }
7383 else
7384 {
7385 /* Get the next character from a Lisp string that is not an
7386 overlay string. Such strings come from the mode line, for
7387 example. We may have to pad with spaces, or truncate the
7388 string. See also next_element_from_c_string. */
7389 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7390 {
7391 it->what = IT_EOB;
7392 return 0;
7393 }
7394 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7395 {
7396 /* Pad with spaces. */
7397 it->c = ' ', it->len = 1;
7398 CHARPOS (position) = BYTEPOS (position) = -1;
7399 }
7400 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it),
7402 it->bidi_it.scan_dir < 0
7403 ? -1
7404 : it->string_nchars)
7405 && next_element_from_composition (it))
7406 {
7407 return 1;
7408 }
7409 else if (STRING_MULTIBYTE (it->string))
7410 {
7411 const unsigned char *s = (SDATA (it->string)
7412 + IT_STRING_BYTEPOS (*it));
7413 it->c = string_char_and_length (s, &it->len);
7414 }
7415 else
7416 {
7417 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7418 it->len = 1;
7419 }
7420 }
7421
7422 /* Record what we have and where it came from. */
7423 it->what = IT_CHARACTER;
7424 it->object = it->string;
7425 it->position = position;
7426 return 1;
7427 }
7428
7429
7430 /* Load IT with next display element from C string IT->s.
7431 IT->string_nchars is the maximum number of characters to return
7432 from the string. IT->end_charpos may be greater than
7433 IT->string_nchars when this function is called, in which case we
7434 may have to return padding spaces. Value is zero if end of string
7435 reached, including padding spaces. */
7436
7437 static int
7438 next_element_from_c_string (struct it *it)
7439 {
7440 int success_p = 1;
7441
7442 xassert (it->s);
7443 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7444 it->what = IT_CHARACTER;
7445 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7446 it->object = Qnil;
7447
7448 /* With bidi reordering, the character to display might not be the
7449 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7450 we were reseated to a new string, whose paragraph direction is
7451 not known. */
7452 if (it->bidi_p && it->bidi_it.first_elt)
7453 get_visually_first_element (it);
7454
7455 /* IT's position can be greater than IT->string_nchars in case a
7456 field width or precision has been specified when the iterator was
7457 initialized. */
7458 if (IT_CHARPOS (*it) >= it->end_charpos)
7459 {
7460 /* End of the game. */
7461 it->what = IT_EOB;
7462 success_p = 0;
7463 }
7464 else if (IT_CHARPOS (*it) >= it->string_nchars)
7465 {
7466 /* Pad with spaces. */
7467 it->c = ' ', it->len = 1;
7468 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7469 }
7470 else if (it->multibyte_p)
7471 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7472 else
7473 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7474
7475 return success_p;
7476 }
7477
7478
7479 /* Set up IT to return characters from an ellipsis, if appropriate.
7480 The definition of the ellipsis glyphs may come from a display table
7481 entry. This function fills IT with the first glyph from the
7482 ellipsis if an ellipsis is to be displayed. */
7483
7484 static int
7485 next_element_from_ellipsis (struct it *it)
7486 {
7487 if (it->selective_display_ellipsis_p)
7488 setup_for_ellipsis (it, it->len);
7489 else
7490 {
7491 /* The face at the current position may be different from the
7492 face we find after the invisible text. Remember what it
7493 was in IT->saved_face_id, and signal that it's there by
7494 setting face_before_selective_p. */
7495 it->saved_face_id = it->face_id;
7496 it->method = GET_FROM_BUFFER;
7497 it->object = it->w->buffer;
7498 reseat_at_next_visible_line_start (it, 1);
7499 it->face_before_selective_p = 1;
7500 }
7501
7502 return GET_NEXT_DISPLAY_ELEMENT (it);
7503 }
7504
7505
7506 /* Deliver an image display element. The iterator IT is already
7507 filled with image information (done in handle_display_prop). Value
7508 is always 1. */
7509
7510
7511 static int
7512 next_element_from_image (struct it *it)
7513 {
7514 it->what = IT_IMAGE;
7515 it->ignore_overlay_strings_at_pos_p = 0;
7516 return 1;
7517 }
7518
7519
7520 /* Fill iterator IT with next display element from a stretch glyph
7521 property. IT->object is the value of the text property. Value is
7522 always 1. */
7523
7524 static int
7525 next_element_from_stretch (struct it *it)
7526 {
7527 it->what = IT_STRETCH;
7528 return 1;
7529 }
7530
7531 /* Scan backwards from IT's current position until we find a stop
7532 position, or until BEGV. This is called when we find ourself
7533 before both the last known prev_stop and base_level_stop while
7534 reordering bidirectional text. */
7535
7536 static void
7537 compute_stop_pos_backwards (struct it *it)
7538 {
7539 const int SCAN_BACK_LIMIT = 1000;
7540 struct text_pos pos;
7541 struct display_pos save_current = it->current;
7542 struct text_pos save_position = it->position;
7543 ptrdiff_t charpos = IT_CHARPOS (*it);
7544 ptrdiff_t where_we_are = charpos;
7545 ptrdiff_t save_stop_pos = it->stop_charpos;
7546 ptrdiff_t save_end_pos = it->end_charpos;
7547
7548 xassert (NILP (it->string) && !it->s);
7549 xassert (it->bidi_p);
7550 it->bidi_p = 0;
7551 do
7552 {
7553 it->end_charpos = min (charpos + 1, ZV);
7554 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7555 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7556 reseat_1 (it, pos, 0);
7557 compute_stop_pos (it);
7558 /* We must advance forward, right? */
7559 if (it->stop_charpos <= charpos)
7560 abort ();
7561 }
7562 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7563
7564 if (it->stop_charpos <= where_we_are)
7565 it->prev_stop = it->stop_charpos;
7566 else
7567 it->prev_stop = BEGV;
7568 it->bidi_p = 1;
7569 it->current = save_current;
7570 it->position = save_position;
7571 it->stop_charpos = save_stop_pos;
7572 it->end_charpos = save_end_pos;
7573 }
7574
7575 /* Scan forward from CHARPOS in the current buffer/string, until we
7576 find a stop position > current IT's position. Then handle the stop
7577 position before that. This is called when we bump into a stop
7578 position while reordering bidirectional text. CHARPOS should be
7579 the last previously processed stop_pos (or BEGV/0, if none were
7580 processed yet) whose position is less that IT's current
7581 position. */
7582
7583 static void
7584 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7585 {
7586 int bufp = !STRINGP (it->string);
7587 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7588 struct display_pos save_current = it->current;
7589 struct text_pos save_position = it->position;
7590 struct text_pos pos1;
7591 ptrdiff_t next_stop;
7592
7593 /* Scan in strict logical order. */
7594 xassert (it->bidi_p);
7595 it->bidi_p = 0;
7596 do
7597 {
7598 it->prev_stop = charpos;
7599 if (bufp)
7600 {
7601 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7602 reseat_1 (it, pos1, 0);
7603 }
7604 else
7605 it->current.string_pos = string_pos (charpos, it->string);
7606 compute_stop_pos (it);
7607 /* We must advance forward, right? */
7608 if (it->stop_charpos <= it->prev_stop)
7609 abort ();
7610 charpos = it->stop_charpos;
7611 }
7612 while (charpos <= where_we_are);
7613
7614 it->bidi_p = 1;
7615 it->current = save_current;
7616 it->position = save_position;
7617 next_stop = it->stop_charpos;
7618 it->stop_charpos = it->prev_stop;
7619 handle_stop (it);
7620 it->stop_charpos = next_stop;
7621 }
7622
7623 /* Load IT with the next display element from current_buffer. Value
7624 is zero if end of buffer reached. IT->stop_charpos is the next
7625 position at which to stop and check for text properties or buffer
7626 end. */
7627
7628 static int
7629 next_element_from_buffer (struct it *it)
7630 {
7631 int success_p = 1;
7632
7633 xassert (IT_CHARPOS (*it) >= BEGV);
7634 xassert (NILP (it->string) && !it->s);
7635 xassert (!it->bidi_p
7636 || (EQ (it->bidi_it.string.lstring, Qnil)
7637 && it->bidi_it.string.s == NULL));
7638
7639 /* With bidi reordering, the character to display might not be the
7640 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7641 we were reseat()ed to a new buffer position, which is potentially
7642 a different paragraph. */
7643 if (it->bidi_p && it->bidi_it.first_elt)
7644 {
7645 get_visually_first_element (it);
7646 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7647 }
7648
7649 if (IT_CHARPOS (*it) >= it->stop_charpos)
7650 {
7651 if (IT_CHARPOS (*it) >= it->end_charpos)
7652 {
7653 int overlay_strings_follow_p;
7654
7655 /* End of the game, except when overlay strings follow that
7656 haven't been returned yet. */
7657 if (it->overlay_strings_at_end_processed_p)
7658 overlay_strings_follow_p = 0;
7659 else
7660 {
7661 it->overlay_strings_at_end_processed_p = 1;
7662 overlay_strings_follow_p = get_overlay_strings (it, 0);
7663 }
7664
7665 if (overlay_strings_follow_p)
7666 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7667 else
7668 {
7669 it->what = IT_EOB;
7670 it->position = it->current.pos;
7671 success_p = 0;
7672 }
7673 }
7674 else if (!(!it->bidi_p
7675 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7676 || IT_CHARPOS (*it) == it->stop_charpos))
7677 {
7678 /* With bidi non-linear iteration, we could find ourselves
7679 far beyond the last computed stop_charpos, with several
7680 other stop positions in between that we missed. Scan
7681 them all now, in buffer's logical order, until we find
7682 and handle the last stop_charpos that precedes our
7683 current position. */
7684 handle_stop_backwards (it, it->stop_charpos);
7685 return GET_NEXT_DISPLAY_ELEMENT (it);
7686 }
7687 else
7688 {
7689 if (it->bidi_p)
7690 {
7691 /* Take note of the stop position we just moved across,
7692 for when we will move back across it. */
7693 it->prev_stop = it->stop_charpos;
7694 /* If we are at base paragraph embedding level, take
7695 note of the last stop position seen at this
7696 level. */
7697 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7698 it->base_level_stop = it->stop_charpos;
7699 }
7700 handle_stop (it);
7701 return GET_NEXT_DISPLAY_ELEMENT (it);
7702 }
7703 }
7704 else if (it->bidi_p
7705 /* If we are before prev_stop, we may have overstepped on
7706 our way backwards a stop_pos, and if so, we need to
7707 handle that stop_pos. */
7708 && IT_CHARPOS (*it) < it->prev_stop
7709 /* We can sometimes back up for reasons that have nothing
7710 to do with bidi reordering. E.g., compositions. The
7711 code below is only needed when we are above the base
7712 embedding level, so test for that explicitly. */
7713 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7714 {
7715 if (it->base_level_stop <= 0
7716 || IT_CHARPOS (*it) < it->base_level_stop)
7717 {
7718 /* If we lost track of base_level_stop, we need to find
7719 prev_stop by looking backwards. This happens, e.g., when
7720 we were reseated to the previous screenful of text by
7721 vertical-motion. */
7722 it->base_level_stop = BEGV;
7723 compute_stop_pos_backwards (it);
7724 handle_stop_backwards (it, it->prev_stop);
7725 }
7726 else
7727 handle_stop_backwards (it, it->base_level_stop);
7728 return GET_NEXT_DISPLAY_ELEMENT (it);
7729 }
7730 else
7731 {
7732 /* No face changes, overlays etc. in sight, so just return a
7733 character from current_buffer. */
7734 unsigned char *p;
7735 ptrdiff_t stop;
7736
7737 /* Maybe run the redisplay end trigger hook. Performance note:
7738 This doesn't seem to cost measurable time. */
7739 if (it->redisplay_end_trigger_charpos
7740 && it->glyph_row
7741 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7742 run_redisplay_end_trigger_hook (it);
7743
7744 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7745 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7746 stop)
7747 && next_element_from_composition (it))
7748 {
7749 return 1;
7750 }
7751
7752 /* Get the next character, maybe multibyte. */
7753 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7754 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7755 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7756 else
7757 it->c = *p, it->len = 1;
7758
7759 /* Record what we have and where it came from. */
7760 it->what = IT_CHARACTER;
7761 it->object = it->w->buffer;
7762 it->position = it->current.pos;
7763
7764 /* Normally we return the character found above, except when we
7765 really want to return an ellipsis for selective display. */
7766 if (it->selective)
7767 {
7768 if (it->c == '\n')
7769 {
7770 /* A value of selective > 0 means hide lines indented more
7771 than that number of columns. */
7772 if (it->selective > 0
7773 && IT_CHARPOS (*it) + 1 < ZV
7774 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7775 IT_BYTEPOS (*it) + 1,
7776 it->selective))
7777 {
7778 success_p = next_element_from_ellipsis (it);
7779 it->dpvec_char_len = -1;
7780 }
7781 }
7782 else if (it->c == '\r' && it->selective == -1)
7783 {
7784 /* A value of selective == -1 means that everything from the
7785 CR to the end of the line is invisible, with maybe an
7786 ellipsis displayed for it. */
7787 success_p = next_element_from_ellipsis (it);
7788 it->dpvec_char_len = -1;
7789 }
7790 }
7791 }
7792
7793 /* Value is zero if end of buffer reached. */
7794 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7795 return success_p;
7796 }
7797
7798
7799 /* Run the redisplay end trigger hook for IT. */
7800
7801 static void
7802 run_redisplay_end_trigger_hook (struct it *it)
7803 {
7804 Lisp_Object args[3];
7805
7806 /* IT->glyph_row should be non-null, i.e. we should be actually
7807 displaying something, or otherwise we should not run the hook. */
7808 xassert (it->glyph_row);
7809
7810 /* Set up hook arguments. */
7811 args[0] = Qredisplay_end_trigger_functions;
7812 args[1] = it->window;
7813 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7814 it->redisplay_end_trigger_charpos = 0;
7815
7816 /* Since we are *trying* to run these functions, don't try to run
7817 them again, even if they get an error. */
7818 it->w->redisplay_end_trigger = Qnil;
7819 Frun_hook_with_args (3, args);
7820
7821 /* Notice if it changed the face of the character we are on. */
7822 handle_face_prop (it);
7823 }
7824
7825
7826 /* Deliver a composition display element. Unlike the other
7827 next_element_from_XXX, this function is not registered in the array
7828 get_next_element[]. It is called from next_element_from_buffer and
7829 next_element_from_string when necessary. */
7830
7831 static int
7832 next_element_from_composition (struct it *it)
7833 {
7834 it->what = IT_COMPOSITION;
7835 it->len = it->cmp_it.nbytes;
7836 if (STRINGP (it->string))
7837 {
7838 if (it->c < 0)
7839 {
7840 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7841 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7842 return 0;
7843 }
7844 it->position = it->current.string_pos;
7845 it->object = it->string;
7846 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7847 IT_STRING_BYTEPOS (*it), it->string);
7848 }
7849 else
7850 {
7851 if (it->c < 0)
7852 {
7853 IT_CHARPOS (*it) += it->cmp_it.nchars;
7854 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7855 if (it->bidi_p)
7856 {
7857 if (it->bidi_it.new_paragraph)
7858 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7859 /* Resync the bidi iterator with IT's new position.
7860 FIXME: this doesn't support bidirectional text. */
7861 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7862 bidi_move_to_visually_next (&it->bidi_it);
7863 }
7864 return 0;
7865 }
7866 it->position = it->current.pos;
7867 it->object = it->w->buffer;
7868 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7869 IT_BYTEPOS (*it), Qnil);
7870 }
7871 return 1;
7872 }
7873
7874
7875 \f
7876 /***********************************************************************
7877 Moving an iterator without producing glyphs
7878 ***********************************************************************/
7879
7880 /* Check if iterator is at a position corresponding to a valid buffer
7881 position after some move_it_ call. */
7882
7883 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7884 ((it)->method == GET_FROM_STRING \
7885 ? IT_STRING_CHARPOS (*it) == 0 \
7886 : 1)
7887
7888
7889 /* Move iterator IT to a specified buffer or X position within one
7890 line on the display without producing glyphs.
7891
7892 OP should be a bit mask including some or all of these bits:
7893 MOVE_TO_X: Stop upon reaching x-position TO_X.
7894 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7895 Regardless of OP's value, stop upon reaching the end of the display line.
7896
7897 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7898 This means, in particular, that TO_X includes window's horizontal
7899 scroll amount.
7900
7901 The return value has several possible values that
7902 say what condition caused the scan to stop:
7903
7904 MOVE_POS_MATCH_OR_ZV
7905 - when TO_POS or ZV was reached.
7906
7907 MOVE_X_REACHED
7908 -when TO_X was reached before TO_POS or ZV were reached.
7909
7910 MOVE_LINE_CONTINUED
7911 - when we reached the end of the display area and the line must
7912 be continued.
7913
7914 MOVE_LINE_TRUNCATED
7915 - when we reached the end of the display area and the line is
7916 truncated.
7917
7918 MOVE_NEWLINE_OR_CR
7919 - when we stopped at a line end, i.e. a newline or a CR and selective
7920 display is on. */
7921
7922 static enum move_it_result
7923 move_it_in_display_line_to (struct it *it,
7924 ptrdiff_t to_charpos, int to_x,
7925 enum move_operation_enum op)
7926 {
7927 enum move_it_result result = MOVE_UNDEFINED;
7928 struct glyph_row *saved_glyph_row;
7929 struct it wrap_it, atpos_it, atx_it, ppos_it;
7930 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7931 void *ppos_data = NULL;
7932 int may_wrap = 0;
7933 enum it_method prev_method = it->method;
7934 ptrdiff_t prev_pos = IT_CHARPOS (*it);
7935 int saw_smaller_pos = prev_pos < to_charpos;
7936
7937 /* Don't produce glyphs in produce_glyphs. */
7938 saved_glyph_row = it->glyph_row;
7939 it->glyph_row = NULL;
7940
7941 /* Use wrap_it to save a copy of IT wherever a word wrap could
7942 occur. Use atpos_it to save a copy of IT at the desired buffer
7943 position, if found, so that we can scan ahead and check if the
7944 word later overshoots the window edge. Use atx_it similarly, for
7945 pixel positions. */
7946 wrap_it.sp = -1;
7947 atpos_it.sp = -1;
7948 atx_it.sp = -1;
7949
7950 /* Use ppos_it under bidi reordering to save a copy of IT for the
7951 position > CHARPOS that is the closest to CHARPOS. We restore
7952 that position in IT when we have scanned the entire display line
7953 without finding a match for CHARPOS and all the character
7954 positions are greater than CHARPOS. */
7955 if (it->bidi_p)
7956 {
7957 SAVE_IT (ppos_it, *it, ppos_data);
7958 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7959 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7960 SAVE_IT (ppos_it, *it, ppos_data);
7961 }
7962
7963 #define BUFFER_POS_REACHED_P() \
7964 ((op & MOVE_TO_POS) != 0 \
7965 && BUFFERP (it->object) \
7966 && (IT_CHARPOS (*it) == to_charpos \
7967 || ((!it->bidi_p \
7968 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7969 && IT_CHARPOS (*it) > to_charpos) \
7970 || (it->what == IT_COMPOSITION \
7971 && ((IT_CHARPOS (*it) > to_charpos \
7972 && to_charpos >= it->cmp_it.charpos) \
7973 || (IT_CHARPOS (*it) < to_charpos \
7974 && to_charpos <= it->cmp_it.charpos)))) \
7975 && (it->method == GET_FROM_BUFFER \
7976 || (it->method == GET_FROM_DISPLAY_VECTOR \
7977 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7978
7979 /* If there's a line-/wrap-prefix, handle it. */
7980 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7981 && it->current_y < it->last_visible_y)
7982 handle_line_prefix (it);
7983
7984 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7985 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7986
7987 while (1)
7988 {
7989 int x, i, ascent = 0, descent = 0;
7990
7991 /* Utility macro to reset an iterator with x, ascent, and descent. */
7992 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7993 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7994 (IT)->max_descent = descent)
7995
7996 /* Stop if we move beyond TO_CHARPOS (after an image or a
7997 display string or stretch glyph). */
7998 if ((op & MOVE_TO_POS) != 0
7999 && BUFFERP (it->object)
8000 && it->method == GET_FROM_BUFFER
8001 && (((!it->bidi_p
8002 /* When the iterator is at base embedding level, we
8003 are guaranteed that characters are delivered for
8004 display in strictly increasing order of their
8005 buffer positions. */
8006 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8007 && IT_CHARPOS (*it) > to_charpos)
8008 || (it->bidi_p
8009 && (prev_method == GET_FROM_IMAGE
8010 || prev_method == GET_FROM_STRETCH
8011 || prev_method == GET_FROM_STRING)
8012 /* Passed TO_CHARPOS from left to right. */
8013 && ((prev_pos < to_charpos
8014 && IT_CHARPOS (*it) > to_charpos)
8015 /* Passed TO_CHARPOS from right to left. */
8016 || (prev_pos > to_charpos
8017 && IT_CHARPOS (*it) < to_charpos)))))
8018 {
8019 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8020 {
8021 result = MOVE_POS_MATCH_OR_ZV;
8022 break;
8023 }
8024 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8025 /* If wrap_it is valid, the current position might be in a
8026 word that is wrapped. So, save the iterator in
8027 atpos_it and continue to see if wrapping happens. */
8028 SAVE_IT (atpos_it, *it, atpos_data);
8029 }
8030
8031 /* Stop when ZV reached.
8032 We used to stop here when TO_CHARPOS reached as well, but that is
8033 too soon if this glyph does not fit on this line. So we handle it
8034 explicitly below. */
8035 if (!get_next_display_element (it))
8036 {
8037 result = MOVE_POS_MATCH_OR_ZV;
8038 break;
8039 }
8040
8041 if (it->line_wrap == TRUNCATE)
8042 {
8043 if (BUFFER_POS_REACHED_P ())
8044 {
8045 result = MOVE_POS_MATCH_OR_ZV;
8046 break;
8047 }
8048 }
8049 else
8050 {
8051 if (it->line_wrap == WORD_WRAP)
8052 {
8053 if (IT_DISPLAYING_WHITESPACE (it))
8054 may_wrap = 1;
8055 else if (may_wrap)
8056 {
8057 /* We have reached a glyph that follows one or more
8058 whitespace characters. If the position is
8059 already found, we are done. */
8060 if (atpos_it.sp >= 0)
8061 {
8062 RESTORE_IT (it, &atpos_it, atpos_data);
8063 result = MOVE_POS_MATCH_OR_ZV;
8064 goto done;
8065 }
8066 if (atx_it.sp >= 0)
8067 {
8068 RESTORE_IT (it, &atx_it, atx_data);
8069 result = MOVE_X_REACHED;
8070 goto done;
8071 }
8072 /* Otherwise, we can wrap here. */
8073 SAVE_IT (wrap_it, *it, wrap_data);
8074 may_wrap = 0;
8075 }
8076 }
8077 }
8078
8079 /* Remember the line height for the current line, in case
8080 the next element doesn't fit on the line. */
8081 ascent = it->max_ascent;
8082 descent = it->max_descent;
8083
8084 /* The call to produce_glyphs will get the metrics of the
8085 display element IT is loaded with. Record the x-position
8086 before this display element, in case it doesn't fit on the
8087 line. */
8088 x = it->current_x;
8089
8090 PRODUCE_GLYPHS (it);
8091
8092 if (it->area != TEXT_AREA)
8093 {
8094 prev_method = it->method;
8095 if (it->method == GET_FROM_BUFFER)
8096 prev_pos = IT_CHARPOS (*it);
8097 set_iterator_to_next (it, 1);
8098 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8099 SET_TEXT_POS (this_line_min_pos,
8100 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8101 if (it->bidi_p
8102 && (op & MOVE_TO_POS)
8103 && IT_CHARPOS (*it) > to_charpos
8104 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8105 SAVE_IT (ppos_it, *it, ppos_data);
8106 continue;
8107 }
8108
8109 /* The number of glyphs we get back in IT->nglyphs will normally
8110 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8111 character on a terminal frame, or (iii) a line end. For the
8112 second case, IT->nglyphs - 1 padding glyphs will be present.
8113 (On X frames, there is only one glyph produced for a
8114 composite character.)
8115
8116 The behavior implemented below means, for continuation lines,
8117 that as many spaces of a TAB as fit on the current line are
8118 displayed there. For terminal frames, as many glyphs of a
8119 multi-glyph character are displayed in the current line, too.
8120 This is what the old redisplay code did, and we keep it that
8121 way. Under X, the whole shape of a complex character must
8122 fit on the line or it will be completely displayed in the
8123 next line.
8124
8125 Note that both for tabs and padding glyphs, all glyphs have
8126 the same width. */
8127 if (it->nglyphs)
8128 {
8129 /* More than one glyph or glyph doesn't fit on line. All
8130 glyphs have the same width. */
8131 int single_glyph_width = it->pixel_width / it->nglyphs;
8132 int new_x;
8133 int x_before_this_char = x;
8134 int hpos_before_this_char = it->hpos;
8135
8136 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8137 {
8138 new_x = x + single_glyph_width;
8139
8140 /* We want to leave anything reaching TO_X to the caller. */
8141 if ((op & MOVE_TO_X) && new_x > to_x)
8142 {
8143 if (BUFFER_POS_REACHED_P ())
8144 {
8145 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8146 goto buffer_pos_reached;
8147 if (atpos_it.sp < 0)
8148 {
8149 SAVE_IT (atpos_it, *it, atpos_data);
8150 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8151 }
8152 }
8153 else
8154 {
8155 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8156 {
8157 it->current_x = x;
8158 result = MOVE_X_REACHED;
8159 break;
8160 }
8161 if (atx_it.sp < 0)
8162 {
8163 SAVE_IT (atx_it, *it, atx_data);
8164 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8165 }
8166 }
8167 }
8168
8169 if (/* Lines are continued. */
8170 it->line_wrap != TRUNCATE
8171 && (/* And glyph doesn't fit on the line. */
8172 new_x > it->last_visible_x
8173 /* Or it fits exactly and we're on a window
8174 system frame. */
8175 || (new_x == it->last_visible_x
8176 && FRAME_WINDOW_P (it->f))))
8177 {
8178 if (/* IT->hpos == 0 means the very first glyph
8179 doesn't fit on the line, e.g. a wide image. */
8180 it->hpos == 0
8181 || (new_x == it->last_visible_x
8182 && FRAME_WINDOW_P (it->f)))
8183 {
8184 ++it->hpos;
8185 it->current_x = new_x;
8186
8187 /* The character's last glyph just barely fits
8188 in this row. */
8189 if (i == it->nglyphs - 1)
8190 {
8191 /* If this is the destination position,
8192 return a position *before* it in this row,
8193 now that we know it fits in this row. */
8194 if (BUFFER_POS_REACHED_P ())
8195 {
8196 if (it->line_wrap != WORD_WRAP
8197 || wrap_it.sp < 0)
8198 {
8199 it->hpos = hpos_before_this_char;
8200 it->current_x = x_before_this_char;
8201 result = MOVE_POS_MATCH_OR_ZV;
8202 break;
8203 }
8204 if (it->line_wrap == WORD_WRAP
8205 && atpos_it.sp < 0)
8206 {
8207 SAVE_IT (atpos_it, *it, atpos_data);
8208 atpos_it.current_x = x_before_this_char;
8209 atpos_it.hpos = hpos_before_this_char;
8210 }
8211 }
8212
8213 prev_method = it->method;
8214 if (it->method == GET_FROM_BUFFER)
8215 prev_pos = IT_CHARPOS (*it);
8216 set_iterator_to_next (it, 1);
8217 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8218 SET_TEXT_POS (this_line_min_pos,
8219 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8220 /* On graphical terminals, newlines may
8221 "overflow" into the fringe if
8222 overflow-newline-into-fringe is non-nil.
8223 On text-only terminals, newlines may
8224 overflow into the last glyph on the
8225 display line.*/
8226 if (!FRAME_WINDOW_P (it->f)
8227 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8228 {
8229 if (!get_next_display_element (it))
8230 {
8231 result = MOVE_POS_MATCH_OR_ZV;
8232 break;
8233 }
8234 if (BUFFER_POS_REACHED_P ())
8235 {
8236 if (ITERATOR_AT_END_OF_LINE_P (it))
8237 result = MOVE_POS_MATCH_OR_ZV;
8238 else
8239 result = MOVE_LINE_CONTINUED;
8240 break;
8241 }
8242 if (ITERATOR_AT_END_OF_LINE_P (it))
8243 {
8244 result = MOVE_NEWLINE_OR_CR;
8245 break;
8246 }
8247 }
8248 }
8249 }
8250 else
8251 IT_RESET_X_ASCENT_DESCENT (it);
8252
8253 if (wrap_it.sp >= 0)
8254 {
8255 RESTORE_IT (it, &wrap_it, wrap_data);
8256 atpos_it.sp = -1;
8257 atx_it.sp = -1;
8258 }
8259
8260 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8261 IT_CHARPOS (*it)));
8262 result = MOVE_LINE_CONTINUED;
8263 break;
8264 }
8265
8266 if (BUFFER_POS_REACHED_P ())
8267 {
8268 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8269 goto buffer_pos_reached;
8270 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8271 {
8272 SAVE_IT (atpos_it, *it, atpos_data);
8273 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8274 }
8275 }
8276
8277 if (new_x > it->first_visible_x)
8278 {
8279 /* Glyph is visible. Increment number of glyphs that
8280 would be displayed. */
8281 ++it->hpos;
8282 }
8283 }
8284
8285 if (result != MOVE_UNDEFINED)
8286 break;
8287 }
8288 else if (BUFFER_POS_REACHED_P ())
8289 {
8290 buffer_pos_reached:
8291 IT_RESET_X_ASCENT_DESCENT (it);
8292 result = MOVE_POS_MATCH_OR_ZV;
8293 break;
8294 }
8295 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8296 {
8297 /* Stop when TO_X specified and reached. This check is
8298 necessary here because of lines consisting of a line end,
8299 only. The line end will not produce any glyphs and we
8300 would never get MOVE_X_REACHED. */
8301 xassert (it->nglyphs == 0);
8302 result = MOVE_X_REACHED;
8303 break;
8304 }
8305
8306 /* Is this a line end? If yes, we're done. */
8307 if (ITERATOR_AT_END_OF_LINE_P (it))
8308 {
8309 /* If we are past TO_CHARPOS, but never saw any character
8310 positions smaller than TO_CHARPOS, return
8311 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8312 did. */
8313 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8314 {
8315 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8316 {
8317 if (IT_CHARPOS (ppos_it) < ZV)
8318 {
8319 RESTORE_IT (it, &ppos_it, ppos_data);
8320 result = MOVE_POS_MATCH_OR_ZV;
8321 }
8322 else
8323 goto buffer_pos_reached;
8324 }
8325 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8326 && IT_CHARPOS (*it) > to_charpos)
8327 goto buffer_pos_reached;
8328 else
8329 result = MOVE_NEWLINE_OR_CR;
8330 }
8331 else
8332 result = MOVE_NEWLINE_OR_CR;
8333 break;
8334 }
8335
8336 prev_method = it->method;
8337 if (it->method == GET_FROM_BUFFER)
8338 prev_pos = IT_CHARPOS (*it);
8339 /* The current display element has been consumed. Advance
8340 to the next. */
8341 set_iterator_to_next (it, 1);
8342 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8343 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8344 if (IT_CHARPOS (*it) < to_charpos)
8345 saw_smaller_pos = 1;
8346 if (it->bidi_p
8347 && (op & MOVE_TO_POS)
8348 && IT_CHARPOS (*it) >= to_charpos
8349 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8350 SAVE_IT (ppos_it, *it, ppos_data);
8351
8352 /* Stop if lines are truncated and IT's current x-position is
8353 past the right edge of the window now. */
8354 if (it->line_wrap == TRUNCATE
8355 && it->current_x >= it->last_visible_x)
8356 {
8357 if (!FRAME_WINDOW_P (it->f)
8358 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8359 {
8360 int at_eob_p = 0;
8361
8362 if ((at_eob_p = !get_next_display_element (it))
8363 || BUFFER_POS_REACHED_P ()
8364 /* If we are past TO_CHARPOS, but never saw any
8365 character positions smaller than TO_CHARPOS,
8366 return MOVE_POS_MATCH_OR_ZV, like the
8367 unidirectional display did. */
8368 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8369 && !saw_smaller_pos
8370 && IT_CHARPOS (*it) > to_charpos))
8371 {
8372 if (it->bidi_p
8373 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8374 RESTORE_IT (it, &ppos_it, ppos_data);
8375 result = MOVE_POS_MATCH_OR_ZV;
8376 break;
8377 }
8378 if (ITERATOR_AT_END_OF_LINE_P (it))
8379 {
8380 result = MOVE_NEWLINE_OR_CR;
8381 break;
8382 }
8383 }
8384 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8385 && !saw_smaller_pos
8386 && IT_CHARPOS (*it) > to_charpos)
8387 {
8388 if (IT_CHARPOS (ppos_it) < ZV)
8389 RESTORE_IT (it, &ppos_it, ppos_data);
8390 result = MOVE_POS_MATCH_OR_ZV;
8391 break;
8392 }
8393 result = MOVE_LINE_TRUNCATED;
8394 break;
8395 }
8396 #undef IT_RESET_X_ASCENT_DESCENT
8397 }
8398
8399 #undef BUFFER_POS_REACHED_P
8400
8401 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8402 restore the saved iterator. */
8403 if (atpos_it.sp >= 0)
8404 RESTORE_IT (it, &atpos_it, atpos_data);
8405 else if (atx_it.sp >= 0)
8406 RESTORE_IT (it, &atx_it, atx_data);
8407
8408 done:
8409
8410 if (atpos_data)
8411 bidi_unshelve_cache (atpos_data, 1);
8412 if (atx_data)
8413 bidi_unshelve_cache (atx_data, 1);
8414 if (wrap_data)
8415 bidi_unshelve_cache (wrap_data, 1);
8416 if (ppos_data)
8417 bidi_unshelve_cache (ppos_data, 1);
8418
8419 /* Restore the iterator settings altered at the beginning of this
8420 function. */
8421 it->glyph_row = saved_glyph_row;
8422 return result;
8423 }
8424
8425 /* For external use. */
8426 void
8427 move_it_in_display_line (struct it *it,
8428 ptrdiff_t to_charpos, int to_x,
8429 enum move_operation_enum op)
8430 {
8431 if (it->line_wrap == WORD_WRAP
8432 && (op & MOVE_TO_X))
8433 {
8434 struct it save_it;
8435 void *save_data = NULL;
8436 int skip;
8437
8438 SAVE_IT (save_it, *it, save_data);
8439 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8440 /* When word-wrap is on, TO_X may lie past the end
8441 of a wrapped line. Then it->current is the
8442 character on the next line, so backtrack to the
8443 space before the wrap point. */
8444 if (skip == MOVE_LINE_CONTINUED)
8445 {
8446 int prev_x = max (it->current_x - 1, 0);
8447 RESTORE_IT (it, &save_it, save_data);
8448 move_it_in_display_line_to
8449 (it, -1, prev_x, MOVE_TO_X);
8450 }
8451 else
8452 bidi_unshelve_cache (save_data, 1);
8453 }
8454 else
8455 move_it_in_display_line_to (it, to_charpos, to_x, op);
8456 }
8457
8458
8459 /* Move IT forward until it satisfies one or more of the criteria in
8460 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8461
8462 OP is a bit-mask that specifies where to stop, and in particular,
8463 which of those four position arguments makes a difference. See the
8464 description of enum move_operation_enum.
8465
8466 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8467 screen line, this function will set IT to the next position that is
8468 displayed to the right of TO_CHARPOS on the screen. */
8469
8470 void
8471 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8472 {
8473 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8474 int line_height, line_start_x = 0, reached = 0;
8475 void *backup_data = NULL;
8476
8477 for (;;)
8478 {
8479 if (op & MOVE_TO_VPOS)
8480 {
8481 /* If no TO_CHARPOS and no TO_X specified, stop at the
8482 start of the line TO_VPOS. */
8483 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8484 {
8485 if (it->vpos == to_vpos)
8486 {
8487 reached = 1;
8488 break;
8489 }
8490 else
8491 skip = move_it_in_display_line_to (it, -1, -1, 0);
8492 }
8493 else
8494 {
8495 /* TO_VPOS >= 0 means stop at TO_X in the line at
8496 TO_VPOS, or at TO_POS, whichever comes first. */
8497 if (it->vpos == to_vpos)
8498 {
8499 reached = 2;
8500 break;
8501 }
8502
8503 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8504
8505 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8506 {
8507 reached = 3;
8508 break;
8509 }
8510 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8511 {
8512 /* We have reached TO_X but not in the line we want. */
8513 skip = move_it_in_display_line_to (it, to_charpos,
8514 -1, MOVE_TO_POS);
8515 if (skip == MOVE_POS_MATCH_OR_ZV)
8516 {
8517 reached = 4;
8518 break;
8519 }
8520 }
8521 }
8522 }
8523 else if (op & MOVE_TO_Y)
8524 {
8525 struct it it_backup;
8526
8527 if (it->line_wrap == WORD_WRAP)
8528 SAVE_IT (it_backup, *it, backup_data);
8529
8530 /* TO_Y specified means stop at TO_X in the line containing
8531 TO_Y---or at TO_CHARPOS if this is reached first. The
8532 problem is that we can't really tell whether the line
8533 contains TO_Y before we have completely scanned it, and
8534 this may skip past TO_X. What we do is to first scan to
8535 TO_X.
8536
8537 If TO_X is not specified, use a TO_X of zero. The reason
8538 is to make the outcome of this function more predictable.
8539 If we didn't use TO_X == 0, we would stop at the end of
8540 the line which is probably not what a caller would expect
8541 to happen. */
8542 skip = move_it_in_display_line_to
8543 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8544 (MOVE_TO_X | (op & MOVE_TO_POS)));
8545
8546 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8547 if (skip == MOVE_POS_MATCH_OR_ZV)
8548 reached = 5;
8549 else if (skip == MOVE_X_REACHED)
8550 {
8551 /* If TO_X was reached, we want to know whether TO_Y is
8552 in the line. We know this is the case if the already
8553 scanned glyphs make the line tall enough. Otherwise,
8554 we must check by scanning the rest of the line. */
8555 line_height = it->max_ascent + it->max_descent;
8556 if (to_y >= it->current_y
8557 && to_y < it->current_y + line_height)
8558 {
8559 reached = 6;
8560 break;
8561 }
8562 SAVE_IT (it_backup, *it, backup_data);
8563 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8564 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8565 op & MOVE_TO_POS);
8566 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8567 line_height = it->max_ascent + it->max_descent;
8568 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8569
8570 if (to_y >= it->current_y
8571 && to_y < it->current_y + line_height)
8572 {
8573 /* If TO_Y is in this line and TO_X was reached
8574 above, we scanned too far. We have to restore
8575 IT's settings to the ones before skipping. */
8576 RESTORE_IT (it, &it_backup, backup_data);
8577 reached = 6;
8578 }
8579 else
8580 {
8581 skip = skip2;
8582 if (skip == MOVE_POS_MATCH_OR_ZV)
8583 reached = 7;
8584 }
8585 }
8586 else
8587 {
8588 /* Check whether TO_Y is in this line. */
8589 line_height = it->max_ascent + it->max_descent;
8590 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8591
8592 if (to_y >= it->current_y
8593 && to_y < it->current_y + line_height)
8594 {
8595 /* When word-wrap is on, TO_X may lie past the end
8596 of a wrapped line. Then it->current is the
8597 character on the next line, so backtrack to the
8598 space before the wrap point. */
8599 if (skip == MOVE_LINE_CONTINUED
8600 && it->line_wrap == WORD_WRAP)
8601 {
8602 int prev_x = max (it->current_x - 1, 0);
8603 RESTORE_IT (it, &it_backup, backup_data);
8604 skip = move_it_in_display_line_to
8605 (it, -1, prev_x, MOVE_TO_X);
8606 }
8607 reached = 6;
8608 }
8609 }
8610
8611 if (reached)
8612 break;
8613 }
8614 else if (BUFFERP (it->object)
8615 && (it->method == GET_FROM_BUFFER
8616 || it->method == GET_FROM_STRETCH)
8617 && IT_CHARPOS (*it) >= to_charpos
8618 /* Under bidi iteration, a call to set_iterator_to_next
8619 can scan far beyond to_charpos if the initial
8620 portion of the next line needs to be reordered. In
8621 that case, give move_it_in_display_line_to another
8622 chance below. */
8623 && !(it->bidi_p
8624 && it->bidi_it.scan_dir == -1))
8625 skip = MOVE_POS_MATCH_OR_ZV;
8626 else
8627 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8628
8629 switch (skip)
8630 {
8631 case MOVE_POS_MATCH_OR_ZV:
8632 reached = 8;
8633 goto out;
8634
8635 case MOVE_NEWLINE_OR_CR:
8636 set_iterator_to_next (it, 1);
8637 it->continuation_lines_width = 0;
8638 break;
8639
8640 case MOVE_LINE_TRUNCATED:
8641 it->continuation_lines_width = 0;
8642 reseat_at_next_visible_line_start (it, 0);
8643 if ((op & MOVE_TO_POS) != 0
8644 && IT_CHARPOS (*it) > to_charpos)
8645 {
8646 reached = 9;
8647 goto out;
8648 }
8649 break;
8650
8651 case MOVE_LINE_CONTINUED:
8652 /* For continued lines ending in a tab, some of the glyphs
8653 associated with the tab are displayed on the current
8654 line. Since it->current_x does not include these glyphs,
8655 we use it->last_visible_x instead. */
8656 if (it->c == '\t')
8657 {
8658 it->continuation_lines_width += it->last_visible_x;
8659 /* When moving by vpos, ensure that the iterator really
8660 advances to the next line (bug#847, bug#969). Fixme:
8661 do we need to do this in other circumstances? */
8662 if (it->current_x != it->last_visible_x
8663 && (op & MOVE_TO_VPOS)
8664 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8665 {
8666 line_start_x = it->current_x + it->pixel_width
8667 - it->last_visible_x;
8668 set_iterator_to_next (it, 0);
8669 }
8670 }
8671 else
8672 it->continuation_lines_width += it->current_x;
8673 break;
8674
8675 default:
8676 abort ();
8677 }
8678
8679 /* Reset/increment for the next run. */
8680 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8681 it->current_x = line_start_x;
8682 line_start_x = 0;
8683 it->hpos = 0;
8684 it->current_y += it->max_ascent + it->max_descent;
8685 ++it->vpos;
8686 last_height = it->max_ascent + it->max_descent;
8687 last_max_ascent = it->max_ascent;
8688 it->max_ascent = it->max_descent = 0;
8689 }
8690
8691 out:
8692
8693 /* On text terminals, we may stop at the end of a line in the middle
8694 of a multi-character glyph. If the glyph itself is continued,
8695 i.e. it is actually displayed on the next line, don't treat this
8696 stopping point as valid; move to the next line instead (unless
8697 that brings us offscreen). */
8698 if (!FRAME_WINDOW_P (it->f)
8699 && op & MOVE_TO_POS
8700 && IT_CHARPOS (*it) == to_charpos
8701 && it->what == IT_CHARACTER
8702 && it->nglyphs > 1
8703 && it->line_wrap == WINDOW_WRAP
8704 && it->current_x == it->last_visible_x - 1
8705 && it->c != '\n'
8706 && it->c != '\t'
8707 && it->vpos < XFASTINT (it->w->window_end_vpos))
8708 {
8709 it->continuation_lines_width += it->current_x;
8710 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8711 it->current_y += it->max_ascent + it->max_descent;
8712 ++it->vpos;
8713 last_height = it->max_ascent + it->max_descent;
8714 last_max_ascent = it->max_ascent;
8715 }
8716
8717 if (backup_data)
8718 bidi_unshelve_cache (backup_data, 1);
8719
8720 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8721 }
8722
8723
8724 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8725
8726 If DY > 0, move IT backward at least that many pixels. DY = 0
8727 means move IT backward to the preceding line start or BEGV. This
8728 function may move over more than DY pixels if IT->current_y - DY
8729 ends up in the middle of a line; in this case IT->current_y will be
8730 set to the top of the line moved to. */
8731
8732 void
8733 move_it_vertically_backward (struct it *it, int dy)
8734 {
8735 int nlines, h;
8736 struct it it2, it3;
8737 void *it2data = NULL, *it3data = NULL;
8738 ptrdiff_t start_pos;
8739
8740 move_further_back:
8741 xassert (dy >= 0);
8742
8743 start_pos = IT_CHARPOS (*it);
8744
8745 /* Estimate how many newlines we must move back. */
8746 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8747
8748 /* Set the iterator's position that many lines back. */
8749 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8750 back_to_previous_visible_line_start (it);
8751
8752 /* Reseat the iterator here. When moving backward, we don't want
8753 reseat to skip forward over invisible text, set up the iterator
8754 to deliver from overlay strings at the new position etc. So,
8755 use reseat_1 here. */
8756 reseat_1 (it, it->current.pos, 1);
8757
8758 /* We are now surely at a line start. */
8759 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8760 reordering is in effect. */
8761 it->continuation_lines_width = 0;
8762
8763 /* Move forward and see what y-distance we moved. First move to the
8764 start of the next line so that we get its height. We need this
8765 height to be able to tell whether we reached the specified
8766 y-distance. */
8767 SAVE_IT (it2, *it, it2data);
8768 it2.max_ascent = it2.max_descent = 0;
8769 do
8770 {
8771 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8772 MOVE_TO_POS | MOVE_TO_VPOS);
8773 }
8774 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8775 /* If we are in a display string which starts at START_POS,
8776 and that display string includes a newline, and we are
8777 right after that newline (i.e. at the beginning of a
8778 display line), exit the loop, because otherwise we will
8779 infloop, since move_it_to will see that it is already at
8780 START_POS and will not move. */
8781 || (it2.method == GET_FROM_STRING
8782 && IT_CHARPOS (it2) == start_pos
8783 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8784 xassert (IT_CHARPOS (*it) >= BEGV);
8785 SAVE_IT (it3, it2, it3data);
8786
8787 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8788 xassert (IT_CHARPOS (*it) >= BEGV);
8789 /* H is the actual vertical distance from the position in *IT
8790 and the starting position. */
8791 h = it2.current_y - it->current_y;
8792 /* NLINES is the distance in number of lines. */
8793 nlines = it2.vpos - it->vpos;
8794
8795 /* Correct IT's y and vpos position
8796 so that they are relative to the starting point. */
8797 it->vpos -= nlines;
8798 it->current_y -= h;
8799
8800 if (dy == 0)
8801 {
8802 /* DY == 0 means move to the start of the screen line. The
8803 value of nlines is > 0 if continuation lines were involved,
8804 or if the original IT position was at start of a line. */
8805 RESTORE_IT (it, it, it2data);
8806 if (nlines > 0)
8807 move_it_by_lines (it, nlines);
8808 /* The above code moves us to some position NLINES down,
8809 usually to its first glyph (leftmost in an L2R line), but
8810 that's not necessarily the start of the line, under bidi
8811 reordering. We want to get to the character position
8812 that is immediately after the newline of the previous
8813 line. */
8814 if (it->bidi_p
8815 && !it->continuation_lines_width
8816 && !STRINGP (it->string)
8817 && IT_CHARPOS (*it) > BEGV
8818 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8819 {
8820 ptrdiff_t nl_pos =
8821 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8822
8823 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8824 }
8825 bidi_unshelve_cache (it3data, 1);
8826 }
8827 else
8828 {
8829 /* The y-position we try to reach, relative to *IT.
8830 Note that H has been subtracted in front of the if-statement. */
8831 int target_y = it->current_y + h - dy;
8832 int y0 = it3.current_y;
8833 int y1;
8834 int line_height;
8835
8836 RESTORE_IT (&it3, &it3, it3data);
8837 y1 = line_bottom_y (&it3);
8838 line_height = y1 - y0;
8839 RESTORE_IT (it, it, it2data);
8840 /* If we did not reach target_y, try to move further backward if
8841 we can. If we moved too far backward, try to move forward. */
8842 if (target_y < it->current_y
8843 /* This is heuristic. In a window that's 3 lines high, with
8844 a line height of 13 pixels each, recentering with point
8845 on the bottom line will try to move -39/2 = 19 pixels
8846 backward. Try to avoid moving into the first line. */
8847 && (it->current_y - target_y
8848 > min (window_box_height (it->w), line_height * 2 / 3))
8849 && IT_CHARPOS (*it) > BEGV)
8850 {
8851 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8852 target_y - it->current_y));
8853 dy = it->current_y - target_y;
8854 goto move_further_back;
8855 }
8856 else if (target_y >= it->current_y + line_height
8857 && IT_CHARPOS (*it) < ZV)
8858 {
8859 /* Should move forward by at least one line, maybe more.
8860
8861 Note: Calling move_it_by_lines can be expensive on
8862 terminal frames, where compute_motion is used (via
8863 vmotion) to do the job, when there are very long lines
8864 and truncate-lines is nil. That's the reason for
8865 treating terminal frames specially here. */
8866
8867 if (!FRAME_WINDOW_P (it->f))
8868 move_it_vertically (it, target_y - (it->current_y + line_height));
8869 else
8870 {
8871 do
8872 {
8873 move_it_by_lines (it, 1);
8874 }
8875 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8876 }
8877 }
8878 }
8879 }
8880
8881
8882 /* Move IT by a specified amount of pixel lines DY. DY negative means
8883 move backwards. DY = 0 means move to start of screen line. At the
8884 end, IT will be on the start of a screen line. */
8885
8886 void
8887 move_it_vertically (struct it *it, int dy)
8888 {
8889 if (dy <= 0)
8890 move_it_vertically_backward (it, -dy);
8891 else
8892 {
8893 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8894 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8895 MOVE_TO_POS | MOVE_TO_Y);
8896 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8897
8898 /* If buffer ends in ZV without a newline, move to the start of
8899 the line to satisfy the post-condition. */
8900 if (IT_CHARPOS (*it) == ZV
8901 && ZV > BEGV
8902 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8903 move_it_by_lines (it, 0);
8904 }
8905 }
8906
8907
8908 /* Move iterator IT past the end of the text line it is in. */
8909
8910 void
8911 move_it_past_eol (struct it *it)
8912 {
8913 enum move_it_result rc;
8914
8915 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8916 if (rc == MOVE_NEWLINE_OR_CR)
8917 set_iterator_to_next (it, 0);
8918 }
8919
8920
8921 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8922 negative means move up. DVPOS == 0 means move to the start of the
8923 screen line.
8924
8925 Optimization idea: If we would know that IT->f doesn't use
8926 a face with proportional font, we could be faster for
8927 truncate-lines nil. */
8928
8929 void
8930 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
8931 {
8932
8933 /* The commented-out optimization uses vmotion on terminals. This
8934 gives bad results, because elements like it->what, on which
8935 callers such as pos_visible_p rely, aren't updated. */
8936 /* struct position pos;
8937 if (!FRAME_WINDOW_P (it->f))
8938 {
8939 struct text_pos textpos;
8940
8941 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8942 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8943 reseat (it, textpos, 1);
8944 it->vpos += pos.vpos;
8945 it->current_y += pos.vpos;
8946 }
8947 else */
8948
8949 if (dvpos == 0)
8950 {
8951 /* DVPOS == 0 means move to the start of the screen line. */
8952 move_it_vertically_backward (it, 0);
8953 xassert (it->current_x == 0 && it->hpos == 0);
8954 /* Let next call to line_bottom_y calculate real line height */
8955 last_height = 0;
8956 }
8957 else if (dvpos > 0)
8958 {
8959 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8960 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8961 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8962 }
8963 else
8964 {
8965 struct it it2;
8966 void *it2data = NULL;
8967 ptrdiff_t start_charpos, i;
8968
8969 /* Start at the beginning of the screen line containing IT's
8970 position. This may actually move vertically backwards,
8971 in case of overlays, so adjust dvpos accordingly. */
8972 dvpos += it->vpos;
8973 move_it_vertically_backward (it, 0);
8974 dvpos -= it->vpos;
8975
8976 /* Go back -DVPOS visible lines and reseat the iterator there. */
8977 start_charpos = IT_CHARPOS (*it);
8978 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8979 back_to_previous_visible_line_start (it);
8980 reseat (it, it->current.pos, 1);
8981
8982 /* Move further back if we end up in a string or an image. */
8983 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8984 {
8985 /* First try to move to start of display line. */
8986 dvpos += it->vpos;
8987 move_it_vertically_backward (it, 0);
8988 dvpos -= it->vpos;
8989 if (IT_POS_VALID_AFTER_MOVE_P (it))
8990 break;
8991 /* If start of line is still in string or image,
8992 move further back. */
8993 back_to_previous_visible_line_start (it);
8994 reseat (it, it->current.pos, 1);
8995 dvpos--;
8996 }
8997
8998 it->current_x = it->hpos = 0;
8999
9000 /* Above call may have moved too far if continuation lines
9001 are involved. Scan forward and see if it did. */
9002 SAVE_IT (it2, *it, it2data);
9003 it2.vpos = it2.current_y = 0;
9004 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9005 it->vpos -= it2.vpos;
9006 it->current_y -= it2.current_y;
9007 it->current_x = it->hpos = 0;
9008
9009 /* If we moved too far back, move IT some lines forward. */
9010 if (it2.vpos > -dvpos)
9011 {
9012 int delta = it2.vpos + dvpos;
9013
9014 RESTORE_IT (&it2, &it2, it2data);
9015 SAVE_IT (it2, *it, it2data);
9016 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9017 /* Move back again if we got too far ahead. */
9018 if (IT_CHARPOS (*it) >= start_charpos)
9019 RESTORE_IT (it, &it2, it2data);
9020 else
9021 bidi_unshelve_cache (it2data, 1);
9022 }
9023 else
9024 RESTORE_IT (it, it, it2data);
9025 }
9026 }
9027
9028 /* Return 1 if IT points into the middle of a display vector. */
9029
9030 int
9031 in_display_vector_p (struct it *it)
9032 {
9033 return (it->method == GET_FROM_DISPLAY_VECTOR
9034 && it->current.dpvec_index > 0
9035 && it->dpvec + it->current.dpvec_index != it->dpend);
9036 }
9037
9038 \f
9039 /***********************************************************************
9040 Messages
9041 ***********************************************************************/
9042
9043
9044 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9045 to *Messages*. */
9046
9047 void
9048 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9049 {
9050 Lisp_Object args[3];
9051 Lisp_Object msg, fmt;
9052 char *buffer;
9053 ptrdiff_t len;
9054 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9055 USE_SAFE_ALLOCA;
9056
9057 /* Do nothing if called asynchronously. Inserting text into
9058 a buffer may call after-change-functions and alike and
9059 that would means running Lisp asynchronously. */
9060 if (handling_signal)
9061 return;
9062
9063 fmt = msg = Qnil;
9064 GCPRO4 (fmt, msg, arg1, arg2);
9065
9066 args[0] = fmt = build_string (format);
9067 args[1] = arg1;
9068 args[2] = arg2;
9069 msg = Fformat (3, args);
9070
9071 len = SBYTES (msg) + 1;
9072 SAFE_ALLOCA (buffer, char *, len);
9073 memcpy (buffer, SDATA (msg), len);
9074
9075 message_dolog (buffer, len - 1, 1, 0);
9076 SAFE_FREE ();
9077
9078 UNGCPRO;
9079 }
9080
9081
9082 /* Output a newline in the *Messages* buffer if "needs" one. */
9083
9084 void
9085 message_log_maybe_newline (void)
9086 {
9087 if (message_log_need_newline)
9088 message_dolog ("", 0, 1, 0);
9089 }
9090
9091
9092 /* Add a string M of length NBYTES to the message log, optionally
9093 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9094 nonzero, means interpret the contents of M as multibyte. This
9095 function calls low-level routines in order to bypass text property
9096 hooks, etc. which might not be safe to run.
9097
9098 This may GC (insert may run before/after change hooks),
9099 so the buffer M must NOT point to a Lisp string. */
9100
9101 void
9102 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9103 {
9104 const unsigned char *msg = (const unsigned char *) m;
9105
9106 if (!NILP (Vmemory_full))
9107 return;
9108
9109 if (!NILP (Vmessage_log_max))
9110 {
9111 struct buffer *oldbuf;
9112 Lisp_Object oldpoint, oldbegv, oldzv;
9113 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9114 ptrdiff_t point_at_end = 0;
9115 ptrdiff_t zv_at_end = 0;
9116 Lisp_Object old_deactivate_mark, tem;
9117 struct gcpro gcpro1;
9118
9119 old_deactivate_mark = Vdeactivate_mark;
9120 oldbuf = current_buffer;
9121 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9122 BVAR (current_buffer, undo_list) = Qt;
9123
9124 oldpoint = message_dolog_marker1;
9125 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9126 oldbegv = message_dolog_marker2;
9127 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9128 oldzv = message_dolog_marker3;
9129 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9130 GCPRO1 (old_deactivate_mark);
9131
9132 if (PT == Z)
9133 point_at_end = 1;
9134 if (ZV == Z)
9135 zv_at_end = 1;
9136
9137 BEGV = BEG;
9138 BEGV_BYTE = BEG_BYTE;
9139 ZV = Z;
9140 ZV_BYTE = Z_BYTE;
9141 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9142
9143 /* Insert the string--maybe converting multibyte to single byte
9144 or vice versa, so that all the text fits the buffer. */
9145 if (multibyte
9146 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9147 {
9148 ptrdiff_t i;
9149 int c, char_bytes;
9150 char work[1];
9151
9152 /* Convert a multibyte string to single-byte
9153 for the *Message* buffer. */
9154 for (i = 0; i < nbytes; i += char_bytes)
9155 {
9156 c = string_char_and_length (msg + i, &char_bytes);
9157 work[0] = (ASCII_CHAR_P (c)
9158 ? c
9159 : multibyte_char_to_unibyte (c));
9160 insert_1_both (work, 1, 1, 1, 0, 0);
9161 }
9162 }
9163 else if (! multibyte
9164 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9165 {
9166 ptrdiff_t i;
9167 int c, char_bytes;
9168 unsigned char str[MAX_MULTIBYTE_LENGTH];
9169 /* Convert a single-byte string to multibyte
9170 for the *Message* buffer. */
9171 for (i = 0; i < nbytes; i++)
9172 {
9173 c = msg[i];
9174 MAKE_CHAR_MULTIBYTE (c);
9175 char_bytes = CHAR_STRING (c, str);
9176 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9177 }
9178 }
9179 else if (nbytes)
9180 insert_1 (m, nbytes, 1, 0, 0);
9181
9182 if (nlflag)
9183 {
9184 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9185 printmax_t dups;
9186 insert_1 ("\n", 1, 1, 0, 0);
9187
9188 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9189 this_bol = PT;
9190 this_bol_byte = PT_BYTE;
9191
9192 /* See if this line duplicates the previous one.
9193 If so, combine duplicates. */
9194 if (this_bol > BEG)
9195 {
9196 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9197 prev_bol = PT;
9198 prev_bol_byte = PT_BYTE;
9199
9200 dups = message_log_check_duplicate (prev_bol_byte,
9201 this_bol_byte);
9202 if (dups)
9203 {
9204 del_range_both (prev_bol, prev_bol_byte,
9205 this_bol, this_bol_byte, 0);
9206 if (dups > 1)
9207 {
9208 char dupstr[sizeof " [ times]"
9209 + INT_STRLEN_BOUND (printmax_t)];
9210 int duplen;
9211
9212 /* If you change this format, don't forget to also
9213 change message_log_check_duplicate. */
9214 sprintf (dupstr, " [%"pMd" times]", dups);
9215 duplen = strlen (dupstr);
9216 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9217 insert_1 (dupstr, duplen, 1, 0, 1);
9218 }
9219 }
9220 }
9221
9222 /* If we have more than the desired maximum number of lines
9223 in the *Messages* buffer now, delete the oldest ones.
9224 This is safe because we don't have undo in this buffer. */
9225
9226 if (NATNUMP (Vmessage_log_max))
9227 {
9228 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9229 -XFASTINT (Vmessage_log_max) - 1, 0);
9230 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9231 }
9232 }
9233 BEGV = XMARKER (oldbegv)->charpos;
9234 BEGV_BYTE = marker_byte_position (oldbegv);
9235
9236 if (zv_at_end)
9237 {
9238 ZV = Z;
9239 ZV_BYTE = Z_BYTE;
9240 }
9241 else
9242 {
9243 ZV = XMARKER (oldzv)->charpos;
9244 ZV_BYTE = marker_byte_position (oldzv);
9245 }
9246
9247 if (point_at_end)
9248 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9249 else
9250 /* We can't do Fgoto_char (oldpoint) because it will run some
9251 Lisp code. */
9252 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9253 XMARKER (oldpoint)->bytepos);
9254
9255 UNGCPRO;
9256 unchain_marker (XMARKER (oldpoint));
9257 unchain_marker (XMARKER (oldbegv));
9258 unchain_marker (XMARKER (oldzv));
9259
9260 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9261 set_buffer_internal (oldbuf);
9262 if (NILP (tem))
9263 windows_or_buffers_changed = old_windows_or_buffers_changed;
9264 message_log_need_newline = !nlflag;
9265 Vdeactivate_mark = old_deactivate_mark;
9266 }
9267 }
9268
9269
9270 /* We are at the end of the buffer after just having inserted a newline.
9271 (Note: We depend on the fact we won't be crossing the gap.)
9272 Check to see if the most recent message looks a lot like the previous one.
9273 Return 0 if different, 1 if the new one should just replace it, or a
9274 value N > 1 if we should also append " [N times]". */
9275
9276 static intmax_t
9277 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9278 {
9279 ptrdiff_t i;
9280 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9281 int seen_dots = 0;
9282 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9283 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9284
9285 for (i = 0; i < len; i++)
9286 {
9287 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9288 seen_dots = 1;
9289 if (p1[i] != p2[i])
9290 return seen_dots;
9291 }
9292 p1 += len;
9293 if (*p1 == '\n')
9294 return 2;
9295 if (*p1++ == ' ' && *p1++ == '[')
9296 {
9297 char *pend;
9298 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9299 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9300 return n+1;
9301 }
9302 return 0;
9303 }
9304 \f
9305
9306 /* Display an echo area message M with a specified length of NBYTES
9307 bytes. The string may include null characters. If M is 0, clear
9308 out any existing message, and let the mini-buffer text show
9309 through.
9310
9311 This may GC, so the buffer M must NOT point to a Lisp string. */
9312
9313 void
9314 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9315 {
9316 /* First flush out any partial line written with print. */
9317 message_log_maybe_newline ();
9318 if (m)
9319 message_dolog (m, nbytes, 1, multibyte);
9320 message2_nolog (m, nbytes, multibyte);
9321 }
9322
9323
9324 /* The non-logging counterpart of message2. */
9325
9326 void
9327 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9328 {
9329 struct frame *sf = SELECTED_FRAME ();
9330 message_enable_multibyte = multibyte;
9331
9332 if (FRAME_INITIAL_P (sf))
9333 {
9334 if (noninteractive_need_newline)
9335 putc ('\n', stderr);
9336 noninteractive_need_newline = 0;
9337 if (m)
9338 fwrite (m, nbytes, 1, stderr);
9339 if (cursor_in_echo_area == 0)
9340 fprintf (stderr, "\n");
9341 fflush (stderr);
9342 }
9343 /* A null message buffer means that the frame hasn't really been
9344 initialized yet. Error messages get reported properly by
9345 cmd_error, so this must be just an informative message; toss it. */
9346 else if (INTERACTIVE
9347 && sf->glyphs_initialized_p
9348 && FRAME_MESSAGE_BUF (sf))
9349 {
9350 Lisp_Object mini_window;
9351 struct frame *f;
9352
9353 /* Get the frame containing the mini-buffer
9354 that the selected frame is using. */
9355 mini_window = FRAME_MINIBUF_WINDOW (sf);
9356 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9357
9358 FRAME_SAMPLE_VISIBILITY (f);
9359 if (FRAME_VISIBLE_P (sf)
9360 && ! FRAME_VISIBLE_P (f))
9361 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9362
9363 if (m)
9364 {
9365 set_message (m, Qnil, nbytes, multibyte);
9366 if (minibuffer_auto_raise)
9367 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9368 }
9369 else
9370 clear_message (1, 1);
9371
9372 do_pending_window_change (0);
9373 echo_area_display (1);
9374 do_pending_window_change (0);
9375 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9376 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9377 }
9378 }
9379
9380
9381 /* Display an echo area message M with a specified length of NBYTES
9382 bytes. The string may include null characters. If M is not a
9383 string, clear out any existing message, and let the mini-buffer
9384 text show through.
9385
9386 This function cancels echoing. */
9387
9388 void
9389 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9390 {
9391 struct gcpro gcpro1;
9392
9393 GCPRO1 (m);
9394 clear_message (1,1);
9395 cancel_echoing ();
9396
9397 /* First flush out any partial line written with print. */
9398 message_log_maybe_newline ();
9399 if (STRINGP (m))
9400 {
9401 char *buffer;
9402 USE_SAFE_ALLOCA;
9403
9404 SAFE_ALLOCA (buffer, char *, nbytes);
9405 memcpy (buffer, SDATA (m), nbytes);
9406 message_dolog (buffer, nbytes, 1, multibyte);
9407 SAFE_FREE ();
9408 }
9409 message3_nolog (m, nbytes, multibyte);
9410
9411 UNGCPRO;
9412 }
9413
9414
9415 /* The non-logging version of message3.
9416 This does not cancel echoing, because it is used for echoing.
9417 Perhaps we need to make a separate function for echoing
9418 and make this cancel echoing. */
9419
9420 void
9421 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9422 {
9423 struct frame *sf = SELECTED_FRAME ();
9424 message_enable_multibyte = multibyte;
9425
9426 if (FRAME_INITIAL_P (sf))
9427 {
9428 if (noninteractive_need_newline)
9429 putc ('\n', stderr);
9430 noninteractive_need_newline = 0;
9431 if (STRINGP (m))
9432 fwrite (SDATA (m), nbytes, 1, stderr);
9433 if (cursor_in_echo_area == 0)
9434 fprintf (stderr, "\n");
9435 fflush (stderr);
9436 }
9437 /* A null message buffer means that the frame hasn't really been
9438 initialized yet. Error messages get reported properly by
9439 cmd_error, so this must be just an informative message; toss it. */
9440 else if (INTERACTIVE
9441 && sf->glyphs_initialized_p
9442 && FRAME_MESSAGE_BUF (sf))
9443 {
9444 Lisp_Object mini_window;
9445 Lisp_Object frame;
9446 struct frame *f;
9447
9448 /* Get the frame containing the mini-buffer
9449 that the selected frame is using. */
9450 mini_window = FRAME_MINIBUF_WINDOW (sf);
9451 frame = XWINDOW (mini_window)->frame;
9452 f = XFRAME (frame);
9453
9454 FRAME_SAMPLE_VISIBILITY (f);
9455 if (FRAME_VISIBLE_P (sf)
9456 && !FRAME_VISIBLE_P (f))
9457 Fmake_frame_visible (frame);
9458
9459 if (STRINGP (m) && SCHARS (m) > 0)
9460 {
9461 set_message (NULL, m, nbytes, multibyte);
9462 if (minibuffer_auto_raise)
9463 Fraise_frame (frame);
9464 /* Assume we are not echoing.
9465 (If we are, echo_now will override this.) */
9466 echo_message_buffer = Qnil;
9467 }
9468 else
9469 clear_message (1, 1);
9470
9471 do_pending_window_change (0);
9472 echo_area_display (1);
9473 do_pending_window_change (0);
9474 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9475 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9476 }
9477 }
9478
9479
9480 /* Display a null-terminated echo area message M. If M is 0, clear
9481 out any existing message, and let the mini-buffer text show through.
9482
9483 The buffer M must continue to exist until after the echo area gets
9484 cleared or some other message gets displayed there. Do not pass
9485 text that is stored in a Lisp string. Do not pass text in a buffer
9486 that was alloca'd. */
9487
9488 void
9489 message1 (const char *m)
9490 {
9491 message2 (m, (m ? strlen (m) : 0), 0);
9492 }
9493
9494
9495 /* The non-logging counterpart of message1. */
9496
9497 void
9498 message1_nolog (const char *m)
9499 {
9500 message2_nolog (m, (m ? strlen (m) : 0), 0);
9501 }
9502
9503 /* Display a message M which contains a single %s
9504 which gets replaced with STRING. */
9505
9506 void
9507 message_with_string (const char *m, Lisp_Object string, int log)
9508 {
9509 CHECK_STRING (string);
9510
9511 if (noninteractive)
9512 {
9513 if (m)
9514 {
9515 if (noninteractive_need_newline)
9516 putc ('\n', stderr);
9517 noninteractive_need_newline = 0;
9518 fprintf (stderr, m, SDATA (string));
9519 if (!cursor_in_echo_area)
9520 fprintf (stderr, "\n");
9521 fflush (stderr);
9522 }
9523 }
9524 else if (INTERACTIVE)
9525 {
9526 /* The frame whose minibuffer we're going to display the message on.
9527 It may be larger than the selected frame, so we need
9528 to use its buffer, not the selected frame's buffer. */
9529 Lisp_Object mini_window;
9530 struct frame *f, *sf = SELECTED_FRAME ();
9531
9532 /* Get the frame containing the minibuffer
9533 that the selected frame is using. */
9534 mini_window = FRAME_MINIBUF_WINDOW (sf);
9535 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9536
9537 /* A null message buffer means that the frame hasn't really been
9538 initialized yet. Error messages get reported properly by
9539 cmd_error, so this must be just an informative message; toss it. */
9540 if (FRAME_MESSAGE_BUF (f))
9541 {
9542 Lisp_Object args[2], msg;
9543 struct gcpro gcpro1, gcpro2;
9544
9545 args[0] = build_string (m);
9546 args[1] = msg = string;
9547 GCPRO2 (args[0], msg);
9548 gcpro1.nvars = 2;
9549
9550 msg = Fformat (2, args);
9551
9552 if (log)
9553 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9554 else
9555 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9556
9557 UNGCPRO;
9558
9559 /* Print should start at the beginning of the message
9560 buffer next time. */
9561 message_buf_print = 0;
9562 }
9563 }
9564 }
9565
9566
9567 /* Dump an informative message to the minibuf. If M is 0, clear out
9568 any existing message, and let the mini-buffer text show through. */
9569
9570 static void
9571 vmessage (const char *m, va_list ap)
9572 {
9573 if (noninteractive)
9574 {
9575 if (m)
9576 {
9577 if (noninteractive_need_newline)
9578 putc ('\n', stderr);
9579 noninteractive_need_newline = 0;
9580 vfprintf (stderr, m, ap);
9581 if (cursor_in_echo_area == 0)
9582 fprintf (stderr, "\n");
9583 fflush (stderr);
9584 }
9585 }
9586 else if (INTERACTIVE)
9587 {
9588 /* The frame whose mini-buffer we're going to display the message
9589 on. It may be larger than the selected frame, so we need to
9590 use its buffer, not the selected frame's buffer. */
9591 Lisp_Object mini_window;
9592 struct frame *f, *sf = SELECTED_FRAME ();
9593
9594 /* Get the frame containing the mini-buffer
9595 that the selected frame is using. */
9596 mini_window = FRAME_MINIBUF_WINDOW (sf);
9597 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9598
9599 /* A null message buffer means that the frame hasn't really been
9600 initialized yet. Error messages get reported properly by
9601 cmd_error, so this must be just an informative message; toss
9602 it. */
9603 if (FRAME_MESSAGE_BUF (f))
9604 {
9605 if (m)
9606 {
9607 ptrdiff_t len;
9608
9609 len = doprnt (FRAME_MESSAGE_BUF (f),
9610 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9611
9612 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9613 }
9614 else
9615 message1 (0);
9616
9617 /* Print should start at the beginning of the message
9618 buffer next time. */
9619 message_buf_print = 0;
9620 }
9621 }
9622 }
9623
9624 void
9625 message (const char *m, ...)
9626 {
9627 va_list ap;
9628 va_start (ap, m);
9629 vmessage (m, ap);
9630 va_end (ap);
9631 }
9632
9633
9634 #if 0
9635 /* The non-logging version of message. */
9636
9637 void
9638 message_nolog (const char *m, ...)
9639 {
9640 Lisp_Object old_log_max;
9641 va_list ap;
9642 va_start (ap, m);
9643 old_log_max = Vmessage_log_max;
9644 Vmessage_log_max = Qnil;
9645 vmessage (m, ap);
9646 Vmessage_log_max = old_log_max;
9647 va_end (ap);
9648 }
9649 #endif
9650
9651
9652 /* Display the current message in the current mini-buffer. This is
9653 only called from error handlers in process.c, and is not time
9654 critical. */
9655
9656 void
9657 update_echo_area (void)
9658 {
9659 if (!NILP (echo_area_buffer[0]))
9660 {
9661 Lisp_Object string;
9662 string = Fcurrent_message ();
9663 message3 (string, SBYTES (string),
9664 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9665 }
9666 }
9667
9668
9669 /* Make sure echo area buffers in `echo_buffers' are live.
9670 If they aren't, make new ones. */
9671
9672 static void
9673 ensure_echo_area_buffers (void)
9674 {
9675 int i;
9676
9677 for (i = 0; i < 2; ++i)
9678 if (!BUFFERP (echo_buffer[i])
9679 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9680 {
9681 char name[30];
9682 Lisp_Object old_buffer;
9683 int j;
9684
9685 old_buffer = echo_buffer[i];
9686 sprintf (name, " *Echo Area %d*", i);
9687 echo_buffer[i] = Fget_buffer_create (build_string (name));
9688 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9689 /* to force word wrap in echo area -
9690 it was decided to postpone this*/
9691 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9692
9693 for (j = 0; j < 2; ++j)
9694 if (EQ (old_buffer, echo_area_buffer[j]))
9695 echo_area_buffer[j] = echo_buffer[i];
9696 }
9697 }
9698
9699
9700 /* Call FN with args A1..A4 with either the current or last displayed
9701 echo_area_buffer as current buffer.
9702
9703 WHICH zero means use the current message buffer
9704 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9705 from echo_buffer[] and clear it.
9706
9707 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9708 suitable buffer from echo_buffer[] and clear it.
9709
9710 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9711 that the current message becomes the last displayed one, make
9712 choose a suitable buffer for echo_area_buffer[0], and clear it.
9713
9714 Value is what FN returns. */
9715
9716 static int
9717 with_echo_area_buffer (struct window *w, int which,
9718 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9719 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9720 {
9721 Lisp_Object buffer;
9722 int this_one, the_other, clear_buffer_p, rc;
9723 ptrdiff_t count = SPECPDL_INDEX ();
9724
9725 /* If buffers aren't live, make new ones. */
9726 ensure_echo_area_buffers ();
9727
9728 clear_buffer_p = 0;
9729
9730 if (which == 0)
9731 this_one = 0, the_other = 1;
9732 else if (which > 0)
9733 this_one = 1, the_other = 0;
9734 else
9735 {
9736 this_one = 0, the_other = 1;
9737 clear_buffer_p = 1;
9738
9739 /* We need a fresh one in case the current echo buffer equals
9740 the one containing the last displayed echo area message. */
9741 if (!NILP (echo_area_buffer[this_one])
9742 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9743 echo_area_buffer[this_one] = Qnil;
9744 }
9745
9746 /* Choose a suitable buffer from echo_buffer[] is we don't
9747 have one. */
9748 if (NILP (echo_area_buffer[this_one]))
9749 {
9750 echo_area_buffer[this_one]
9751 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9752 ? echo_buffer[the_other]
9753 : echo_buffer[this_one]);
9754 clear_buffer_p = 1;
9755 }
9756
9757 buffer = echo_area_buffer[this_one];
9758
9759 /* Don't get confused by reusing the buffer used for echoing
9760 for a different purpose. */
9761 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9762 cancel_echoing ();
9763
9764 record_unwind_protect (unwind_with_echo_area_buffer,
9765 with_echo_area_buffer_unwind_data (w));
9766
9767 /* Make the echo area buffer current. Note that for display
9768 purposes, it is not necessary that the displayed window's buffer
9769 == current_buffer, except for text property lookup. So, let's
9770 only set that buffer temporarily here without doing a full
9771 Fset_window_buffer. We must also change w->pointm, though,
9772 because otherwise an assertions in unshow_buffer fails, and Emacs
9773 aborts. */
9774 set_buffer_internal_1 (XBUFFER (buffer));
9775 if (w)
9776 {
9777 w->buffer = buffer;
9778 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9779 }
9780
9781 BVAR (current_buffer, undo_list) = Qt;
9782 BVAR (current_buffer, read_only) = Qnil;
9783 specbind (Qinhibit_read_only, Qt);
9784 specbind (Qinhibit_modification_hooks, Qt);
9785
9786 if (clear_buffer_p && Z > BEG)
9787 del_range (BEG, Z);
9788
9789 xassert (BEGV >= BEG);
9790 xassert (ZV <= Z && ZV >= BEGV);
9791
9792 rc = fn (a1, a2, a3, a4);
9793
9794 xassert (BEGV >= BEG);
9795 xassert (ZV <= Z && ZV >= BEGV);
9796
9797 unbind_to (count, Qnil);
9798 return rc;
9799 }
9800
9801
9802 /* Save state that should be preserved around the call to the function
9803 FN called in with_echo_area_buffer. */
9804
9805 static Lisp_Object
9806 with_echo_area_buffer_unwind_data (struct window *w)
9807 {
9808 int i = 0;
9809 Lisp_Object vector, tmp;
9810
9811 /* Reduce consing by keeping one vector in
9812 Vwith_echo_area_save_vector. */
9813 vector = Vwith_echo_area_save_vector;
9814 Vwith_echo_area_save_vector = Qnil;
9815
9816 if (NILP (vector))
9817 vector = Fmake_vector (make_number (7), Qnil);
9818
9819 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9820 ASET (vector, i, Vdeactivate_mark); ++i;
9821 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9822
9823 if (w)
9824 {
9825 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9826 ASET (vector, i, w->buffer); ++i;
9827 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9828 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9829 }
9830 else
9831 {
9832 int end = i + 4;
9833 for (; i < end; ++i)
9834 ASET (vector, i, Qnil);
9835 }
9836
9837 xassert (i == ASIZE (vector));
9838 return vector;
9839 }
9840
9841
9842 /* Restore global state from VECTOR which was created by
9843 with_echo_area_buffer_unwind_data. */
9844
9845 static Lisp_Object
9846 unwind_with_echo_area_buffer (Lisp_Object vector)
9847 {
9848 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9849 Vdeactivate_mark = AREF (vector, 1);
9850 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9851
9852 if (WINDOWP (AREF (vector, 3)))
9853 {
9854 struct window *w;
9855 Lisp_Object buffer, charpos, bytepos;
9856
9857 w = XWINDOW (AREF (vector, 3));
9858 buffer = AREF (vector, 4);
9859 charpos = AREF (vector, 5);
9860 bytepos = AREF (vector, 6);
9861
9862 w->buffer = buffer;
9863 set_marker_both (w->pointm, buffer,
9864 XFASTINT (charpos), XFASTINT (bytepos));
9865 }
9866
9867 Vwith_echo_area_save_vector = vector;
9868 return Qnil;
9869 }
9870
9871
9872 /* Set up the echo area for use by print functions. MULTIBYTE_P
9873 non-zero means we will print multibyte. */
9874
9875 void
9876 setup_echo_area_for_printing (int multibyte_p)
9877 {
9878 /* If we can't find an echo area any more, exit. */
9879 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9880 Fkill_emacs (Qnil);
9881
9882 ensure_echo_area_buffers ();
9883
9884 if (!message_buf_print)
9885 {
9886 /* A message has been output since the last time we printed.
9887 Choose a fresh echo area buffer. */
9888 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9889 echo_area_buffer[0] = echo_buffer[1];
9890 else
9891 echo_area_buffer[0] = echo_buffer[0];
9892
9893 /* Switch to that buffer and clear it. */
9894 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9895 BVAR (current_buffer, truncate_lines) = Qnil;
9896
9897 if (Z > BEG)
9898 {
9899 ptrdiff_t count = SPECPDL_INDEX ();
9900 specbind (Qinhibit_read_only, Qt);
9901 /* Note that undo recording is always disabled. */
9902 del_range (BEG, Z);
9903 unbind_to (count, Qnil);
9904 }
9905 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9906
9907 /* Set up the buffer for the multibyteness we need. */
9908 if (multibyte_p
9909 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9910 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9911
9912 /* Raise the frame containing the echo area. */
9913 if (minibuffer_auto_raise)
9914 {
9915 struct frame *sf = SELECTED_FRAME ();
9916 Lisp_Object mini_window;
9917 mini_window = FRAME_MINIBUF_WINDOW (sf);
9918 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9919 }
9920
9921 message_log_maybe_newline ();
9922 message_buf_print = 1;
9923 }
9924 else
9925 {
9926 if (NILP (echo_area_buffer[0]))
9927 {
9928 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9929 echo_area_buffer[0] = echo_buffer[1];
9930 else
9931 echo_area_buffer[0] = echo_buffer[0];
9932 }
9933
9934 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9935 {
9936 /* Someone switched buffers between print requests. */
9937 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9938 BVAR (current_buffer, truncate_lines) = Qnil;
9939 }
9940 }
9941 }
9942
9943
9944 /* Display an echo area message in window W. Value is non-zero if W's
9945 height is changed. If display_last_displayed_message_p is
9946 non-zero, display the message that was last displayed, otherwise
9947 display the current message. */
9948
9949 static int
9950 display_echo_area (struct window *w)
9951 {
9952 int i, no_message_p, window_height_changed_p;
9953
9954 /* Temporarily disable garbage collections while displaying the echo
9955 area. This is done because a GC can print a message itself.
9956 That message would modify the echo area buffer's contents while a
9957 redisplay of the buffer is going on, and seriously confuse
9958 redisplay. */
9959 ptrdiff_t count = inhibit_garbage_collection ();
9960
9961 /* If there is no message, we must call display_echo_area_1
9962 nevertheless because it resizes the window. But we will have to
9963 reset the echo_area_buffer in question to nil at the end because
9964 with_echo_area_buffer will sets it to an empty buffer. */
9965 i = display_last_displayed_message_p ? 1 : 0;
9966 no_message_p = NILP (echo_area_buffer[i]);
9967
9968 window_height_changed_p
9969 = with_echo_area_buffer (w, display_last_displayed_message_p,
9970 display_echo_area_1,
9971 (intptr_t) w, Qnil, 0, 0);
9972
9973 if (no_message_p)
9974 echo_area_buffer[i] = Qnil;
9975
9976 unbind_to (count, Qnil);
9977 return window_height_changed_p;
9978 }
9979
9980
9981 /* Helper for display_echo_area. Display the current buffer which
9982 contains the current echo area message in window W, a mini-window,
9983 a pointer to which is passed in A1. A2..A4 are currently not used.
9984 Change the height of W so that all of the message is displayed.
9985 Value is non-zero if height of W was changed. */
9986
9987 static int
9988 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9989 {
9990 intptr_t i1 = a1;
9991 struct window *w = (struct window *) i1;
9992 Lisp_Object window;
9993 struct text_pos start;
9994 int window_height_changed_p = 0;
9995
9996 /* Do this before displaying, so that we have a large enough glyph
9997 matrix for the display. If we can't get enough space for the
9998 whole text, display the last N lines. That works by setting w->start. */
9999 window_height_changed_p = resize_mini_window (w, 0);
10000
10001 /* Use the starting position chosen by resize_mini_window. */
10002 SET_TEXT_POS_FROM_MARKER (start, w->start);
10003
10004 /* Display. */
10005 clear_glyph_matrix (w->desired_matrix);
10006 XSETWINDOW (window, w);
10007 try_window (window, start, 0);
10008
10009 return window_height_changed_p;
10010 }
10011
10012
10013 /* Resize the echo area window to exactly the size needed for the
10014 currently displayed message, if there is one. If a mini-buffer
10015 is active, don't shrink it. */
10016
10017 void
10018 resize_echo_area_exactly (void)
10019 {
10020 if (BUFFERP (echo_area_buffer[0])
10021 && WINDOWP (echo_area_window))
10022 {
10023 struct window *w = XWINDOW (echo_area_window);
10024 int resized_p;
10025 Lisp_Object resize_exactly;
10026
10027 if (minibuf_level == 0)
10028 resize_exactly = Qt;
10029 else
10030 resize_exactly = Qnil;
10031
10032 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10033 (intptr_t) w, resize_exactly,
10034 0, 0);
10035 if (resized_p)
10036 {
10037 ++windows_or_buffers_changed;
10038 ++update_mode_lines;
10039 redisplay_internal ();
10040 }
10041 }
10042 }
10043
10044
10045 /* Callback function for with_echo_area_buffer, when used from
10046 resize_echo_area_exactly. A1 contains a pointer to the window to
10047 resize, EXACTLY non-nil means resize the mini-window exactly to the
10048 size of the text displayed. A3 and A4 are not used. Value is what
10049 resize_mini_window returns. */
10050
10051 static int
10052 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10053 {
10054 intptr_t i1 = a1;
10055 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10056 }
10057
10058
10059 /* Resize mini-window W to fit the size of its contents. EXACT_P
10060 means size the window exactly to the size needed. Otherwise, it's
10061 only enlarged until W's buffer is empty.
10062
10063 Set W->start to the right place to begin display. If the whole
10064 contents fit, start at the beginning. Otherwise, start so as
10065 to make the end of the contents appear. This is particularly
10066 important for y-or-n-p, but seems desirable generally.
10067
10068 Value is non-zero if the window height has been changed. */
10069
10070 int
10071 resize_mini_window (struct window *w, int exact_p)
10072 {
10073 struct frame *f = XFRAME (w->frame);
10074 int window_height_changed_p = 0;
10075
10076 xassert (MINI_WINDOW_P (w));
10077
10078 /* By default, start display at the beginning. */
10079 set_marker_both (w->start, w->buffer,
10080 BUF_BEGV (XBUFFER (w->buffer)),
10081 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10082
10083 /* Don't resize windows while redisplaying a window; it would
10084 confuse redisplay functions when the size of the window they are
10085 displaying changes from under them. Such a resizing can happen,
10086 for instance, when which-func prints a long message while
10087 we are running fontification-functions. We're running these
10088 functions with safe_call which binds inhibit-redisplay to t. */
10089 if (!NILP (Vinhibit_redisplay))
10090 return 0;
10091
10092 /* Nil means don't try to resize. */
10093 if (NILP (Vresize_mini_windows)
10094 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10095 return 0;
10096
10097 if (!FRAME_MINIBUF_ONLY_P (f))
10098 {
10099 struct it it;
10100 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10101 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10102 int height;
10103 EMACS_INT max_height;
10104 int unit = FRAME_LINE_HEIGHT (f);
10105 struct text_pos start;
10106 struct buffer *old_current_buffer = NULL;
10107
10108 if (current_buffer != XBUFFER (w->buffer))
10109 {
10110 old_current_buffer = current_buffer;
10111 set_buffer_internal (XBUFFER (w->buffer));
10112 }
10113
10114 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10115
10116 /* Compute the max. number of lines specified by the user. */
10117 if (FLOATP (Vmax_mini_window_height))
10118 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10119 else if (INTEGERP (Vmax_mini_window_height))
10120 max_height = XINT (Vmax_mini_window_height);
10121 else
10122 max_height = total_height / 4;
10123
10124 /* Correct that max. height if it's bogus. */
10125 max_height = max (1, max_height);
10126 max_height = min (total_height, max_height);
10127
10128 /* Find out the height of the text in the window. */
10129 if (it.line_wrap == TRUNCATE)
10130 height = 1;
10131 else
10132 {
10133 last_height = 0;
10134 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10135 if (it.max_ascent == 0 && it.max_descent == 0)
10136 height = it.current_y + last_height;
10137 else
10138 height = it.current_y + it.max_ascent + it.max_descent;
10139 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10140 height = (height + unit - 1) / unit;
10141 }
10142
10143 /* Compute a suitable window start. */
10144 if (height > max_height)
10145 {
10146 height = max_height;
10147 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10148 move_it_vertically_backward (&it, (height - 1) * unit);
10149 start = it.current.pos;
10150 }
10151 else
10152 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10153 SET_MARKER_FROM_TEXT_POS (w->start, start);
10154
10155 if (EQ (Vresize_mini_windows, Qgrow_only))
10156 {
10157 /* Let it grow only, until we display an empty message, in which
10158 case the window shrinks again. */
10159 if (height > WINDOW_TOTAL_LINES (w))
10160 {
10161 int old_height = WINDOW_TOTAL_LINES (w);
10162 freeze_window_starts (f, 1);
10163 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10164 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10165 }
10166 else if (height < WINDOW_TOTAL_LINES (w)
10167 && (exact_p || BEGV == ZV))
10168 {
10169 int old_height = WINDOW_TOTAL_LINES (w);
10170 freeze_window_starts (f, 0);
10171 shrink_mini_window (w);
10172 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10173 }
10174 }
10175 else
10176 {
10177 /* Always resize to exact size needed. */
10178 if (height > WINDOW_TOTAL_LINES (w))
10179 {
10180 int old_height = WINDOW_TOTAL_LINES (w);
10181 freeze_window_starts (f, 1);
10182 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10183 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10184 }
10185 else if (height < WINDOW_TOTAL_LINES (w))
10186 {
10187 int old_height = WINDOW_TOTAL_LINES (w);
10188 freeze_window_starts (f, 0);
10189 shrink_mini_window (w);
10190
10191 if (height)
10192 {
10193 freeze_window_starts (f, 1);
10194 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10195 }
10196
10197 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10198 }
10199 }
10200
10201 if (old_current_buffer)
10202 set_buffer_internal (old_current_buffer);
10203 }
10204
10205 return window_height_changed_p;
10206 }
10207
10208
10209 /* Value is the current message, a string, or nil if there is no
10210 current message. */
10211
10212 Lisp_Object
10213 current_message (void)
10214 {
10215 Lisp_Object msg;
10216
10217 if (!BUFFERP (echo_area_buffer[0]))
10218 msg = Qnil;
10219 else
10220 {
10221 with_echo_area_buffer (0, 0, current_message_1,
10222 (intptr_t) &msg, Qnil, 0, 0);
10223 if (NILP (msg))
10224 echo_area_buffer[0] = Qnil;
10225 }
10226
10227 return msg;
10228 }
10229
10230
10231 static int
10232 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10233 {
10234 intptr_t i1 = a1;
10235 Lisp_Object *msg = (Lisp_Object *) i1;
10236
10237 if (Z > BEG)
10238 *msg = make_buffer_string (BEG, Z, 1);
10239 else
10240 *msg = Qnil;
10241 return 0;
10242 }
10243
10244
10245 /* Push the current message on Vmessage_stack for later restoration
10246 by restore_message. Value is non-zero if the current message isn't
10247 empty. This is a relatively infrequent operation, so it's not
10248 worth optimizing. */
10249
10250 int
10251 push_message (void)
10252 {
10253 Lisp_Object msg;
10254 msg = current_message ();
10255 Vmessage_stack = Fcons (msg, Vmessage_stack);
10256 return STRINGP (msg);
10257 }
10258
10259
10260 /* Restore message display from the top of Vmessage_stack. */
10261
10262 void
10263 restore_message (void)
10264 {
10265 Lisp_Object msg;
10266
10267 xassert (CONSP (Vmessage_stack));
10268 msg = XCAR (Vmessage_stack);
10269 if (STRINGP (msg))
10270 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10271 else
10272 message3_nolog (msg, 0, 0);
10273 }
10274
10275
10276 /* Handler for record_unwind_protect calling pop_message. */
10277
10278 Lisp_Object
10279 pop_message_unwind (Lisp_Object dummy)
10280 {
10281 pop_message ();
10282 return Qnil;
10283 }
10284
10285 /* Pop the top-most entry off Vmessage_stack. */
10286
10287 static void
10288 pop_message (void)
10289 {
10290 xassert (CONSP (Vmessage_stack));
10291 Vmessage_stack = XCDR (Vmessage_stack);
10292 }
10293
10294
10295 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10296 exits. If the stack is not empty, we have a missing pop_message
10297 somewhere. */
10298
10299 void
10300 check_message_stack (void)
10301 {
10302 if (!NILP (Vmessage_stack))
10303 abort ();
10304 }
10305
10306
10307 /* Truncate to NCHARS what will be displayed in the echo area the next
10308 time we display it---but don't redisplay it now. */
10309
10310 void
10311 truncate_echo_area (ptrdiff_t nchars)
10312 {
10313 if (nchars == 0)
10314 echo_area_buffer[0] = Qnil;
10315 /* A null message buffer means that the frame hasn't really been
10316 initialized yet. Error messages get reported properly by
10317 cmd_error, so this must be just an informative message; toss it. */
10318 else if (!noninteractive
10319 && INTERACTIVE
10320 && !NILP (echo_area_buffer[0]))
10321 {
10322 struct frame *sf = SELECTED_FRAME ();
10323 if (FRAME_MESSAGE_BUF (sf))
10324 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10325 }
10326 }
10327
10328
10329 /* Helper function for truncate_echo_area. Truncate the current
10330 message to at most NCHARS characters. */
10331
10332 static int
10333 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10334 {
10335 if (BEG + nchars < Z)
10336 del_range (BEG + nchars, Z);
10337 if (Z == BEG)
10338 echo_area_buffer[0] = Qnil;
10339 return 0;
10340 }
10341
10342
10343 /* Set the current message to a substring of S or STRING.
10344
10345 If STRING is a Lisp string, set the message to the first NBYTES
10346 bytes from STRING. NBYTES zero means use the whole string. If
10347 STRING is multibyte, the message will be displayed multibyte.
10348
10349 If S is not null, set the message to the first LEN bytes of S. LEN
10350 zero means use the whole string. MULTIBYTE_P non-zero means S is
10351 multibyte. Display the message multibyte in that case.
10352
10353 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10354 to t before calling set_message_1 (which calls insert).
10355 */
10356
10357 static void
10358 set_message (const char *s, Lisp_Object string,
10359 ptrdiff_t nbytes, int multibyte_p)
10360 {
10361 message_enable_multibyte
10362 = ((s && multibyte_p)
10363 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10364
10365 with_echo_area_buffer (0, -1, set_message_1,
10366 (intptr_t) s, string, nbytes, multibyte_p);
10367 message_buf_print = 0;
10368 help_echo_showing_p = 0;
10369 }
10370
10371
10372 /* Helper function for set_message. Arguments have the same meaning
10373 as there, with A1 corresponding to S and A2 corresponding to STRING
10374 This function is called with the echo area buffer being
10375 current. */
10376
10377 static int
10378 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10379 {
10380 intptr_t i1 = a1;
10381 const char *s = (const char *) i1;
10382 const unsigned char *msg = (const unsigned char *) s;
10383 Lisp_Object string = a2;
10384
10385 /* Change multibyteness of the echo buffer appropriately. */
10386 if (message_enable_multibyte
10387 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10388 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10389
10390 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10391 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10392 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10393
10394 /* Insert new message at BEG. */
10395 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10396
10397 if (STRINGP (string))
10398 {
10399 ptrdiff_t nchars;
10400
10401 if (nbytes == 0)
10402 nbytes = SBYTES (string);
10403 nchars = string_byte_to_char (string, nbytes);
10404
10405 /* This function takes care of single/multibyte conversion. We
10406 just have to ensure that the echo area buffer has the right
10407 setting of enable_multibyte_characters. */
10408 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10409 }
10410 else if (s)
10411 {
10412 if (nbytes == 0)
10413 nbytes = strlen (s);
10414
10415 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10416 {
10417 /* Convert from multi-byte to single-byte. */
10418 ptrdiff_t i;
10419 int c, n;
10420 char work[1];
10421
10422 /* Convert a multibyte string to single-byte. */
10423 for (i = 0; i < nbytes; i += n)
10424 {
10425 c = string_char_and_length (msg + i, &n);
10426 work[0] = (ASCII_CHAR_P (c)
10427 ? c
10428 : multibyte_char_to_unibyte (c));
10429 insert_1_both (work, 1, 1, 1, 0, 0);
10430 }
10431 }
10432 else if (!multibyte_p
10433 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10434 {
10435 /* Convert from single-byte to multi-byte. */
10436 ptrdiff_t i;
10437 int c, n;
10438 unsigned char str[MAX_MULTIBYTE_LENGTH];
10439
10440 /* Convert a single-byte string to multibyte. */
10441 for (i = 0; i < nbytes; i++)
10442 {
10443 c = msg[i];
10444 MAKE_CHAR_MULTIBYTE (c);
10445 n = CHAR_STRING (c, str);
10446 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10447 }
10448 }
10449 else
10450 insert_1 (s, nbytes, 1, 0, 0);
10451 }
10452
10453 return 0;
10454 }
10455
10456
10457 /* Clear messages. CURRENT_P non-zero means clear the current
10458 message. LAST_DISPLAYED_P non-zero means clear the message
10459 last displayed. */
10460
10461 void
10462 clear_message (int current_p, int last_displayed_p)
10463 {
10464 if (current_p)
10465 {
10466 echo_area_buffer[0] = Qnil;
10467 message_cleared_p = 1;
10468 }
10469
10470 if (last_displayed_p)
10471 echo_area_buffer[1] = Qnil;
10472
10473 message_buf_print = 0;
10474 }
10475
10476 /* Clear garbaged frames.
10477
10478 This function is used where the old redisplay called
10479 redraw_garbaged_frames which in turn called redraw_frame which in
10480 turn called clear_frame. The call to clear_frame was a source of
10481 flickering. I believe a clear_frame is not necessary. It should
10482 suffice in the new redisplay to invalidate all current matrices,
10483 and ensure a complete redisplay of all windows. */
10484
10485 static void
10486 clear_garbaged_frames (void)
10487 {
10488 if (frame_garbaged)
10489 {
10490 Lisp_Object tail, frame;
10491 int changed_count = 0;
10492
10493 FOR_EACH_FRAME (tail, frame)
10494 {
10495 struct frame *f = XFRAME (frame);
10496
10497 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10498 {
10499 if (f->resized_p)
10500 {
10501 Fredraw_frame (frame);
10502 f->force_flush_display_p = 1;
10503 }
10504 clear_current_matrices (f);
10505 changed_count++;
10506 f->garbaged = 0;
10507 f->resized_p = 0;
10508 }
10509 }
10510
10511 frame_garbaged = 0;
10512 if (changed_count)
10513 ++windows_or_buffers_changed;
10514 }
10515 }
10516
10517
10518 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10519 is non-zero update selected_frame. Value is non-zero if the
10520 mini-windows height has been changed. */
10521
10522 static int
10523 echo_area_display (int update_frame_p)
10524 {
10525 Lisp_Object mini_window;
10526 struct window *w;
10527 struct frame *f;
10528 int window_height_changed_p = 0;
10529 struct frame *sf = SELECTED_FRAME ();
10530
10531 mini_window = FRAME_MINIBUF_WINDOW (sf);
10532 w = XWINDOW (mini_window);
10533 f = XFRAME (WINDOW_FRAME (w));
10534
10535 /* Don't display if frame is invisible or not yet initialized. */
10536 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10537 return 0;
10538
10539 #ifdef HAVE_WINDOW_SYSTEM
10540 /* When Emacs starts, selected_frame may be the initial terminal
10541 frame. If we let this through, a message would be displayed on
10542 the terminal. */
10543 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10544 return 0;
10545 #endif /* HAVE_WINDOW_SYSTEM */
10546
10547 /* Redraw garbaged frames. */
10548 if (frame_garbaged)
10549 clear_garbaged_frames ();
10550
10551 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10552 {
10553 echo_area_window = mini_window;
10554 window_height_changed_p = display_echo_area (w);
10555 w->must_be_updated_p = 1;
10556
10557 /* Update the display, unless called from redisplay_internal.
10558 Also don't update the screen during redisplay itself. The
10559 update will happen at the end of redisplay, and an update
10560 here could cause confusion. */
10561 if (update_frame_p && !redisplaying_p)
10562 {
10563 int n = 0;
10564
10565 /* If the display update has been interrupted by pending
10566 input, update mode lines in the frame. Due to the
10567 pending input, it might have been that redisplay hasn't
10568 been called, so that mode lines above the echo area are
10569 garbaged. This looks odd, so we prevent it here. */
10570 if (!display_completed)
10571 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10572
10573 if (window_height_changed_p
10574 /* Don't do this if Emacs is shutting down. Redisplay
10575 needs to run hooks. */
10576 && !NILP (Vrun_hooks))
10577 {
10578 /* Must update other windows. Likewise as in other
10579 cases, don't let this update be interrupted by
10580 pending input. */
10581 ptrdiff_t count = SPECPDL_INDEX ();
10582 specbind (Qredisplay_dont_pause, Qt);
10583 windows_or_buffers_changed = 1;
10584 redisplay_internal ();
10585 unbind_to (count, Qnil);
10586 }
10587 else if (FRAME_WINDOW_P (f) && n == 0)
10588 {
10589 /* Window configuration is the same as before.
10590 Can do with a display update of the echo area,
10591 unless we displayed some mode lines. */
10592 update_single_window (w, 1);
10593 FRAME_RIF (f)->flush_display (f);
10594 }
10595 else
10596 update_frame (f, 1, 1);
10597
10598 /* If cursor is in the echo area, make sure that the next
10599 redisplay displays the minibuffer, so that the cursor will
10600 be replaced with what the minibuffer wants. */
10601 if (cursor_in_echo_area)
10602 ++windows_or_buffers_changed;
10603 }
10604 }
10605 else if (!EQ (mini_window, selected_window))
10606 windows_or_buffers_changed++;
10607
10608 /* Last displayed message is now the current message. */
10609 echo_area_buffer[1] = echo_area_buffer[0];
10610 /* Inform read_char that we're not echoing. */
10611 echo_message_buffer = Qnil;
10612
10613 /* Prevent redisplay optimization in redisplay_internal by resetting
10614 this_line_start_pos. This is done because the mini-buffer now
10615 displays the message instead of its buffer text. */
10616 if (EQ (mini_window, selected_window))
10617 CHARPOS (this_line_start_pos) = 0;
10618
10619 return window_height_changed_p;
10620 }
10621
10622
10623 \f
10624 /***********************************************************************
10625 Mode Lines and Frame Titles
10626 ***********************************************************************/
10627
10628 /* A buffer for constructing non-propertized mode-line strings and
10629 frame titles in it; allocated from the heap in init_xdisp and
10630 resized as needed in store_mode_line_noprop_char. */
10631
10632 static char *mode_line_noprop_buf;
10633
10634 /* The buffer's end, and a current output position in it. */
10635
10636 static char *mode_line_noprop_buf_end;
10637 static char *mode_line_noprop_ptr;
10638
10639 #define MODE_LINE_NOPROP_LEN(start) \
10640 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10641
10642 static enum {
10643 MODE_LINE_DISPLAY = 0,
10644 MODE_LINE_TITLE,
10645 MODE_LINE_NOPROP,
10646 MODE_LINE_STRING
10647 } mode_line_target;
10648
10649 /* Alist that caches the results of :propertize.
10650 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10651 static Lisp_Object mode_line_proptrans_alist;
10652
10653 /* List of strings making up the mode-line. */
10654 static Lisp_Object mode_line_string_list;
10655
10656 /* Base face property when building propertized mode line string. */
10657 static Lisp_Object mode_line_string_face;
10658 static Lisp_Object mode_line_string_face_prop;
10659
10660
10661 /* Unwind data for mode line strings */
10662
10663 static Lisp_Object Vmode_line_unwind_vector;
10664
10665 static Lisp_Object
10666 format_mode_line_unwind_data (struct buffer *obuf,
10667 Lisp_Object owin,
10668 int save_proptrans)
10669 {
10670 Lisp_Object vector, tmp;
10671
10672 /* Reduce consing by keeping one vector in
10673 Vwith_echo_area_save_vector. */
10674 vector = Vmode_line_unwind_vector;
10675 Vmode_line_unwind_vector = Qnil;
10676
10677 if (NILP (vector))
10678 vector = Fmake_vector (make_number (8), Qnil);
10679
10680 ASET (vector, 0, make_number (mode_line_target));
10681 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10682 ASET (vector, 2, mode_line_string_list);
10683 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10684 ASET (vector, 4, mode_line_string_face);
10685 ASET (vector, 5, mode_line_string_face_prop);
10686
10687 if (obuf)
10688 XSETBUFFER (tmp, obuf);
10689 else
10690 tmp = Qnil;
10691 ASET (vector, 6, tmp);
10692 ASET (vector, 7, owin);
10693
10694 return vector;
10695 }
10696
10697 static Lisp_Object
10698 unwind_format_mode_line (Lisp_Object vector)
10699 {
10700 mode_line_target = XINT (AREF (vector, 0));
10701 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10702 mode_line_string_list = AREF (vector, 2);
10703 if (! EQ (AREF (vector, 3), Qt))
10704 mode_line_proptrans_alist = AREF (vector, 3);
10705 mode_line_string_face = AREF (vector, 4);
10706 mode_line_string_face_prop = AREF (vector, 5);
10707
10708 if (!NILP (AREF (vector, 7)))
10709 /* Select window before buffer, since it may change the buffer. */
10710 Fselect_window (AREF (vector, 7), Qt);
10711
10712 if (!NILP (AREF (vector, 6)))
10713 {
10714 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10715 ASET (vector, 6, Qnil);
10716 }
10717
10718 Vmode_line_unwind_vector = vector;
10719 return Qnil;
10720 }
10721
10722
10723 /* Store a single character C for the frame title in mode_line_noprop_buf.
10724 Re-allocate mode_line_noprop_buf if necessary. */
10725
10726 static void
10727 store_mode_line_noprop_char (char c)
10728 {
10729 /* If output position has reached the end of the allocated buffer,
10730 increase the buffer's size. */
10731 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10732 {
10733 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10734 ptrdiff_t size = len;
10735 mode_line_noprop_buf =
10736 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10737 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10738 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10739 }
10740
10741 *mode_line_noprop_ptr++ = c;
10742 }
10743
10744
10745 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10746 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10747 characters that yield more columns than PRECISION; PRECISION <= 0
10748 means copy the whole string. Pad with spaces until FIELD_WIDTH
10749 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10750 pad. Called from display_mode_element when it is used to build a
10751 frame title. */
10752
10753 static int
10754 store_mode_line_noprop (const char *string, int field_width, int precision)
10755 {
10756 const unsigned char *str = (const unsigned char *) string;
10757 int n = 0;
10758 ptrdiff_t dummy, nbytes;
10759
10760 /* Copy at most PRECISION chars from STR. */
10761 nbytes = strlen (string);
10762 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10763 while (nbytes--)
10764 store_mode_line_noprop_char (*str++);
10765
10766 /* Fill up with spaces until FIELD_WIDTH reached. */
10767 while (field_width > 0
10768 && n < field_width)
10769 {
10770 store_mode_line_noprop_char (' ');
10771 ++n;
10772 }
10773
10774 return n;
10775 }
10776
10777 /***********************************************************************
10778 Frame Titles
10779 ***********************************************************************/
10780
10781 #ifdef HAVE_WINDOW_SYSTEM
10782
10783 /* Set the title of FRAME, if it has changed. The title format is
10784 Vicon_title_format if FRAME is iconified, otherwise it is
10785 frame_title_format. */
10786
10787 static void
10788 x_consider_frame_title (Lisp_Object frame)
10789 {
10790 struct frame *f = XFRAME (frame);
10791
10792 if (FRAME_WINDOW_P (f)
10793 || FRAME_MINIBUF_ONLY_P (f)
10794 || f->explicit_name)
10795 {
10796 /* Do we have more than one visible frame on this X display? */
10797 Lisp_Object tail;
10798 Lisp_Object fmt;
10799 ptrdiff_t title_start;
10800 char *title;
10801 ptrdiff_t len;
10802 struct it it;
10803 ptrdiff_t count = SPECPDL_INDEX ();
10804
10805 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10806 {
10807 Lisp_Object other_frame = XCAR (tail);
10808 struct frame *tf = XFRAME (other_frame);
10809
10810 if (tf != f
10811 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10812 && !FRAME_MINIBUF_ONLY_P (tf)
10813 && !EQ (other_frame, tip_frame)
10814 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10815 break;
10816 }
10817
10818 /* Set global variable indicating that multiple frames exist. */
10819 multiple_frames = CONSP (tail);
10820
10821 /* Switch to the buffer of selected window of the frame. Set up
10822 mode_line_target so that display_mode_element will output into
10823 mode_line_noprop_buf; then display the title. */
10824 record_unwind_protect (unwind_format_mode_line,
10825 format_mode_line_unwind_data
10826 (current_buffer, selected_window, 0));
10827
10828 Fselect_window (f->selected_window, Qt);
10829 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10830 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10831
10832 mode_line_target = MODE_LINE_TITLE;
10833 title_start = MODE_LINE_NOPROP_LEN (0);
10834 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10835 NULL, DEFAULT_FACE_ID);
10836 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10837 len = MODE_LINE_NOPROP_LEN (title_start);
10838 title = mode_line_noprop_buf + title_start;
10839 unbind_to (count, Qnil);
10840
10841 /* Set the title only if it's changed. This avoids consing in
10842 the common case where it hasn't. (If it turns out that we've
10843 already wasted too much time by walking through the list with
10844 display_mode_element, then we might need to optimize at a
10845 higher level than this.) */
10846 if (! STRINGP (f->name)
10847 || SBYTES (f->name) != len
10848 || memcmp (title, SDATA (f->name), len) != 0)
10849 x_implicitly_set_name (f, make_string (title, len), Qnil);
10850 }
10851 }
10852
10853 #endif /* not HAVE_WINDOW_SYSTEM */
10854
10855
10856
10857 \f
10858 /***********************************************************************
10859 Menu Bars
10860 ***********************************************************************/
10861
10862
10863 /* Prepare for redisplay by updating menu-bar item lists when
10864 appropriate. This can call eval. */
10865
10866 void
10867 prepare_menu_bars (void)
10868 {
10869 int all_windows;
10870 struct gcpro gcpro1, gcpro2;
10871 struct frame *f;
10872 Lisp_Object tooltip_frame;
10873
10874 #ifdef HAVE_WINDOW_SYSTEM
10875 tooltip_frame = tip_frame;
10876 #else
10877 tooltip_frame = Qnil;
10878 #endif
10879
10880 /* Update all frame titles based on their buffer names, etc. We do
10881 this before the menu bars so that the buffer-menu will show the
10882 up-to-date frame titles. */
10883 #ifdef HAVE_WINDOW_SYSTEM
10884 if (windows_or_buffers_changed || update_mode_lines)
10885 {
10886 Lisp_Object tail, frame;
10887
10888 FOR_EACH_FRAME (tail, frame)
10889 {
10890 f = XFRAME (frame);
10891 if (!EQ (frame, tooltip_frame)
10892 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10893 x_consider_frame_title (frame);
10894 }
10895 }
10896 #endif /* HAVE_WINDOW_SYSTEM */
10897
10898 /* Update the menu bar item lists, if appropriate. This has to be
10899 done before any actual redisplay or generation of display lines. */
10900 all_windows = (update_mode_lines
10901 || buffer_shared > 1
10902 || windows_or_buffers_changed);
10903 if (all_windows)
10904 {
10905 Lisp_Object tail, frame;
10906 ptrdiff_t count = SPECPDL_INDEX ();
10907 /* 1 means that update_menu_bar has run its hooks
10908 so any further calls to update_menu_bar shouldn't do so again. */
10909 int menu_bar_hooks_run = 0;
10910
10911 record_unwind_save_match_data ();
10912
10913 FOR_EACH_FRAME (tail, frame)
10914 {
10915 f = XFRAME (frame);
10916
10917 /* Ignore tooltip frame. */
10918 if (EQ (frame, tooltip_frame))
10919 continue;
10920
10921 /* If a window on this frame changed size, report that to
10922 the user and clear the size-change flag. */
10923 if (FRAME_WINDOW_SIZES_CHANGED (f))
10924 {
10925 Lisp_Object functions;
10926
10927 /* Clear flag first in case we get an error below. */
10928 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10929 functions = Vwindow_size_change_functions;
10930 GCPRO2 (tail, functions);
10931
10932 while (CONSP (functions))
10933 {
10934 if (!EQ (XCAR (functions), Qt))
10935 call1 (XCAR (functions), frame);
10936 functions = XCDR (functions);
10937 }
10938 UNGCPRO;
10939 }
10940
10941 GCPRO1 (tail);
10942 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10943 #ifdef HAVE_WINDOW_SYSTEM
10944 update_tool_bar (f, 0);
10945 #endif
10946 #ifdef HAVE_NS
10947 if (windows_or_buffers_changed
10948 && FRAME_NS_P (f))
10949 ns_set_doc_edited (f, Fbuffer_modified_p
10950 (XWINDOW (f->selected_window)->buffer));
10951 #endif
10952 UNGCPRO;
10953 }
10954
10955 unbind_to (count, Qnil);
10956 }
10957 else
10958 {
10959 struct frame *sf = SELECTED_FRAME ();
10960 update_menu_bar (sf, 1, 0);
10961 #ifdef HAVE_WINDOW_SYSTEM
10962 update_tool_bar (sf, 1);
10963 #endif
10964 }
10965 }
10966
10967
10968 /* Update the menu bar item list for frame F. This has to be done
10969 before we start to fill in any display lines, because it can call
10970 eval.
10971
10972 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10973
10974 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10975 already ran the menu bar hooks for this redisplay, so there
10976 is no need to run them again. The return value is the
10977 updated value of this flag, to pass to the next call. */
10978
10979 static int
10980 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10981 {
10982 Lisp_Object window;
10983 register struct window *w;
10984
10985 /* If called recursively during a menu update, do nothing. This can
10986 happen when, for instance, an activate-menubar-hook causes a
10987 redisplay. */
10988 if (inhibit_menubar_update)
10989 return hooks_run;
10990
10991 window = FRAME_SELECTED_WINDOW (f);
10992 w = XWINDOW (window);
10993
10994 if (FRAME_WINDOW_P (f)
10995 ?
10996 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10997 || defined (HAVE_NS) || defined (USE_GTK)
10998 FRAME_EXTERNAL_MENU_BAR (f)
10999 #else
11000 FRAME_MENU_BAR_LINES (f) > 0
11001 #endif
11002 : FRAME_MENU_BAR_LINES (f) > 0)
11003 {
11004 /* If the user has switched buffers or windows, we need to
11005 recompute to reflect the new bindings. But we'll
11006 recompute when update_mode_lines is set too; that means
11007 that people can use force-mode-line-update to request
11008 that the menu bar be recomputed. The adverse effect on
11009 the rest of the redisplay algorithm is about the same as
11010 windows_or_buffers_changed anyway. */
11011 if (windows_or_buffers_changed
11012 /* This used to test w->update_mode_line, but we believe
11013 there is no need to recompute the menu in that case. */
11014 || update_mode_lines
11015 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11016 < BUF_MODIFF (XBUFFER (w->buffer)))
11017 != !NILP (w->last_had_star))
11018 || ((!NILP (Vtransient_mark_mode)
11019 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11020 != !NILP (w->region_showing)))
11021 {
11022 struct buffer *prev = current_buffer;
11023 ptrdiff_t count = SPECPDL_INDEX ();
11024
11025 specbind (Qinhibit_menubar_update, Qt);
11026
11027 set_buffer_internal_1 (XBUFFER (w->buffer));
11028 if (save_match_data)
11029 record_unwind_save_match_data ();
11030 if (NILP (Voverriding_local_map_menu_flag))
11031 {
11032 specbind (Qoverriding_terminal_local_map, Qnil);
11033 specbind (Qoverriding_local_map, Qnil);
11034 }
11035
11036 if (!hooks_run)
11037 {
11038 /* Run the Lucid hook. */
11039 safe_run_hooks (Qactivate_menubar_hook);
11040
11041 /* If it has changed current-menubar from previous value,
11042 really recompute the menu-bar from the value. */
11043 if (! NILP (Vlucid_menu_bar_dirty_flag))
11044 call0 (Qrecompute_lucid_menubar);
11045
11046 safe_run_hooks (Qmenu_bar_update_hook);
11047
11048 hooks_run = 1;
11049 }
11050
11051 XSETFRAME (Vmenu_updating_frame, f);
11052 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11053
11054 /* Redisplay the menu bar in case we changed it. */
11055 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11056 || defined (HAVE_NS) || defined (USE_GTK)
11057 if (FRAME_WINDOW_P (f))
11058 {
11059 #if defined (HAVE_NS)
11060 /* All frames on Mac OS share the same menubar. So only
11061 the selected frame should be allowed to set it. */
11062 if (f == SELECTED_FRAME ())
11063 #endif
11064 set_frame_menubar (f, 0, 0);
11065 }
11066 else
11067 /* On a terminal screen, the menu bar is an ordinary screen
11068 line, and this makes it get updated. */
11069 w->update_mode_line = Qt;
11070 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11071 /* In the non-toolkit version, the menu bar is an ordinary screen
11072 line, and this makes it get updated. */
11073 w->update_mode_line = Qt;
11074 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11075
11076 unbind_to (count, Qnil);
11077 set_buffer_internal_1 (prev);
11078 }
11079 }
11080
11081 return hooks_run;
11082 }
11083
11084
11085 \f
11086 /***********************************************************************
11087 Output Cursor
11088 ***********************************************************************/
11089
11090 #ifdef HAVE_WINDOW_SYSTEM
11091
11092 /* EXPORT:
11093 Nominal cursor position -- where to draw output.
11094 HPOS and VPOS are window relative glyph matrix coordinates.
11095 X and Y are window relative pixel coordinates. */
11096
11097 struct cursor_pos output_cursor;
11098
11099
11100 /* EXPORT:
11101 Set the global variable output_cursor to CURSOR. All cursor
11102 positions are relative to updated_window. */
11103
11104 void
11105 set_output_cursor (struct cursor_pos *cursor)
11106 {
11107 output_cursor.hpos = cursor->hpos;
11108 output_cursor.vpos = cursor->vpos;
11109 output_cursor.x = cursor->x;
11110 output_cursor.y = cursor->y;
11111 }
11112
11113
11114 /* EXPORT for RIF:
11115 Set a nominal cursor position.
11116
11117 HPOS and VPOS are column/row positions in a window glyph matrix. X
11118 and Y are window text area relative pixel positions.
11119
11120 If this is done during an update, updated_window will contain the
11121 window that is being updated and the position is the future output
11122 cursor position for that window. If updated_window is null, use
11123 selected_window and display the cursor at the given position. */
11124
11125 void
11126 x_cursor_to (int vpos, int hpos, int y, int x)
11127 {
11128 struct window *w;
11129
11130 /* If updated_window is not set, work on selected_window. */
11131 if (updated_window)
11132 w = updated_window;
11133 else
11134 w = XWINDOW (selected_window);
11135
11136 /* Set the output cursor. */
11137 output_cursor.hpos = hpos;
11138 output_cursor.vpos = vpos;
11139 output_cursor.x = x;
11140 output_cursor.y = y;
11141
11142 /* If not called as part of an update, really display the cursor.
11143 This will also set the cursor position of W. */
11144 if (updated_window == NULL)
11145 {
11146 BLOCK_INPUT;
11147 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11148 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11149 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11150 UNBLOCK_INPUT;
11151 }
11152 }
11153
11154 #endif /* HAVE_WINDOW_SYSTEM */
11155
11156 \f
11157 /***********************************************************************
11158 Tool-bars
11159 ***********************************************************************/
11160
11161 #ifdef HAVE_WINDOW_SYSTEM
11162
11163 /* Where the mouse was last time we reported a mouse event. */
11164
11165 FRAME_PTR last_mouse_frame;
11166
11167 /* Tool-bar item index of the item on which a mouse button was pressed
11168 or -1. */
11169
11170 int last_tool_bar_item;
11171
11172
11173 static Lisp_Object
11174 update_tool_bar_unwind (Lisp_Object frame)
11175 {
11176 selected_frame = frame;
11177 return Qnil;
11178 }
11179
11180 /* Update the tool-bar item list for frame F. This has to be done
11181 before we start to fill in any display lines. Called from
11182 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11183 and restore it here. */
11184
11185 static void
11186 update_tool_bar (struct frame *f, int save_match_data)
11187 {
11188 #if defined (USE_GTK) || defined (HAVE_NS)
11189 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11190 #else
11191 int do_update = WINDOWP (f->tool_bar_window)
11192 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11193 #endif
11194
11195 if (do_update)
11196 {
11197 Lisp_Object window;
11198 struct window *w;
11199
11200 window = FRAME_SELECTED_WINDOW (f);
11201 w = XWINDOW (window);
11202
11203 /* If the user has switched buffers or windows, we need to
11204 recompute to reflect the new bindings. But we'll
11205 recompute when update_mode_lines is set too; that means
11206 that people can use force-mode-line-update to request
11207 that the menu bar be recomputed. The adverse effect on
11208 the rest of the redisplay algorithm is about the same as
11209 windows_or_buffers_changed anyway. */
11210 if (windows_or_buffers_changed
11211 || !NILP (w->update_mode_line)
11212 || update_mode_lines
11213 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11214 < BUF_MODIFF (XBUFFER (w->buffer)))
11215 != !NILP (w->last_had_star))
11216 || ((!NILP (Vtransient_mark_mode)
11217 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11218 != !NILP (w->region_showing)))
11219 {
11220 struct buffer *prev = current_buffer;
11221 ptrdiff_t count = SPECPDL_INDEX ();
11222 Lisp_Object frame, new_tool_bar;
11223 int new_n_tool_bar;
11224 struct gcpro gcpro1;
11225
11226 /* Set current_buffer to the buffer of the selected
11227 window of the frame, so that we get the right local
11228 keymaps. */
11229 set_buffer_internal_1 (XBUFFER (w->buffer));
11230
11231 /* Save match data, if we must. */
11232 if (save_match_data)
11233 record_unwind_save_match_data ();
11234
11235 /* Make sure that we don't accidentally use bogus keymaps. */
11236 if (NILP (Voverriding_local_map_menu_flag))
11237 {
11238 specbind (Qoverriding_terminal_local_map, Qnil);
11239 specbind (Qoverriding_local_map, Qnil);
11240 }
11241
11242 GCPRO1 (new_tool_bar);
11243
11244 /* We must temporarily set the selected frame to this frame
11245 before calling tool_bar_items, because the calculation of
11246 the tool-bar keymap uses the selected frame (see
11247 `tool-bar-make-keymap' in tool-bar.el). */
11248 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11249 XSETFRAME (frame, f);
11250 selected_frame = frame;
11251
11252 /* Build desired tool-bar items from keymaps. */
11253 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11254 &new_n_tool_bar);
11255
11256 /* Redisplay the tool-bar if we changed it. */
11257 if (new_n_tool_bar != f->n_tool_bar_items
11258 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11259 {
11260 /* Redisplay that happens asynchronously due to an expose event
11261 may access f->tool_bar_items. Make sure we update both
11262 variables within BLOCK_INPUT so no such event interrupts. */
11263 BLOCK_INPUT;
11264 f->tool_bar_items = new_tool_bar;
11265 f->n_tool_bar_items = new_n_tool_bar;
11266 w->update_mode_line = Qt;
11267 UNBLOCK_INPUT;
11268 }
11269
11270 UNGCPRO;
11271
11272 unbind_to (count, Qnil);
11273 set_buffer_internal_1 (prev);
11274 }
11275 }
11276 }
11277
11278
11279 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11280 F's desired tool-bar contents. F->tool_bar_items must have
11281 been set up previously by calling prepare_menu_bars. */
11282
11283 static void
11284 build_desired_tool_bar_string (struct frame *f)
11285 {
11286 int i, size, size_needed;
11287 struct gcpro gcpro1, gcpro2, gcpro3;
11288 Lisp_Object image, plist, props;
11289
11290 image = plist = props = Qnil;
11291 GCPRO3 (image, plist, props);
11292
11293 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11294 Otherwise, make a new string. */
11295
11296 /* The size of the string we might be able to reuse. */
11297 size = (STRINGP (f->desired_tool_bar_string)
11298 ? SCHARS (f->desired_tool_bar_string)
11299 : 0);
11300
11301 /* We need one space in the string for each image. */
11302 size_needed = f->n_tool_bar_items;
11303
11304 /* Reuse f->desired_tool_bar_string, if possible. */
11305 if (size < size_needed || NILP (f->desired_tool_bar_string))
11306 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11307 make_number (' '));
11308 else
11309 {
11310 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11311 Fremove_text_properties (make_number (0), make_number (size),
11312 props, f->desired_tool_bar_string);
11313 }
11314
11315 /* Put a `display' property on the string for the images to display,
11316 put a `menu_item' property on tool-bar items with a value that
11317 is the index of the item in F's tool-bar item vector. */
11318 for (i = 0; i < f->n_tool_bar_items; ++i)
11319 {
11320 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11321
11322 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11323 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11324 int hmargin, vmargin, relief, idx, end;
11325
11326 /* If image is a vector, choose the image according to the
11327 button state. */
11328 image = PROP (TOOL_BAR_ITEM_IMAGES);
11329 if (VECTORP (image))
11330 {
11331 if (enabled_p)
11332 idx = (selected_p
11333 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11334 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11335 else
11336 idx = (selected_p
11337 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11338 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11339
11340 xassert (ASIZE (image) >= idx);
11341 image = AREF (image, idx);
11342 }
11343 else
11344 idx = -1;
11345
11346 /* Ignore invalid image specifications. */
11347 if (!valid_image_p (image))
11348 continue;
11349
11350 /* Display the tool-bar button pressed, or depressed. */
11351 plist = Fcopy_sequence (XCDR (image));
11352
11353 /* Compute margin and relief to draw. */
11354 relief = (tool_bar_button_relief >= 0
11355 ? tool_bar_button_relief
11356 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11357 hmargin = vmargin = relief;
11358
11359 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11360 INT_MAX - max (hmargin, vmargin)))
11361 {
11362 hmargin += XFASTINT (Vtool_bar_button_margin);
11363 vmargin += XFASTINT (Vtool_bar_button_margin);
11364 }
11365 else if (CONSP (Vtool_bar_button_margin))
11366 {
11367 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11368 INT_MAX - hmargin))
11369 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11370
11371 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11372 INT_MAX - vmargin))
11373 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11374 }
11375
11376 if (auto_raise_tool_bar_buttons_p)
11377 {
11378 /* Add a `:relief' property to the image spec if the item is
11379 selected. */
11380 if (selected_p)
11381 {
11382 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11383 hmargin -= relief;
11384 vmargin -= relief;
11385 }
11386 }
11387 else
11388 {
11389 /* If image is selected, display it pressed, i.e. with a
11390 negative relief. If it's not selected, display it with a
11391 raised relief. */
11392 plist = Fplist_put (plist, QCrelief,
11393 (selected_p
11394 ? make_number (-relief)
11395 : make_number (relief)));
11396 hmargin -= relief;
11397 vmargin -= relief;
11398 }
11399
11400 /* Put a margin around the image. */
11401 if (hmargin || vmargin)
11402 {
11403 if (hmargin == vmargin)
11404 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11405 else
11406 plist = Fplist_put (plist, QCmargin,
11407 Fcons (make_number (hmargin),
11408 make_number (vmargin)));
11409 }
11410
11411 /* If button is not enabled, and we don't have special images
11412 for the disabled state, make the image appear disabled by
11413 applying an appropriate algorithm to it. */
11414 if (!enabled_p && idx < 0)
11415 plist = Fplist_put (plist, QCconversion, Qdisabled);
11416
11417 /* Put a `display' text property on the string for the image to
11418 display. Put a `menu-item' property on the string that gives
11419 the start of this item's properties in the tool-bar items
11420 vector. */
11421 image = Fcons (Qimage, plist);
11422 props = list4 (Qdisplay, image,
11423 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11424
11425 /* Let the last image hide all remaining spaces in the tool bar
11426 string. The string can be longer than needed when we reuse a
11427 previous string. */
11428 if (i + 1 == f->n_tool_bar_items)
11429 end = SCHARS (f->desired_tool_bar_string);
11430 else
11431 end = i + 1;
11432 Fadd_text_properties (make_number (i), make_number (end),
11433 props, f->desired_tool_bar_string);
11434 #undef PROP
11435 }
11436
11437 UNGCPRO;
11438 }
11439
11440
11441 /* Display one line of the tool-bar of frame IT->f.
11442
11443 HEIGHT specifies the desired height of the tool-bar line.
11444 If the actual height of the glyph row is less than HEIGHT, the
11445 row's height is increased to HEIGHT, and the icons are centered
11446 vertically in the new height.
11447
11448 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11449 count a final empty row in case the tool-bar width exactly matches
11450 the window width.
11451 */
11452
11453 static void
11454 display_tool_bar_line (struct it *it, int height)
11455 {
11456 struct glyph_row *row = it->glyph_row;
11457 int max_x = it->last_visible_x;
11458 struct glyph *last;
11459
11460 prepare_desired_row (row);
11461 row->y = it->current_y;
11462
11463 /* Note that this isn't made use of if the face hasn't a box,
11464 so there's no need to check the face here. */
11465 it->start_of_box_run_p = 1;
11466
11467 while (it->current_x < max_x)
11468 {
11469 int x, n_glyphs_before, i, nglyphs;
11470 struct it it_before;
11471
11472 /* Get the next display element. */
11473 if (!get_next_display_element (it))
11474 {
11475 /* Don't count empty row if we are counting needed tool-bar lines. */
11476 if (height < 0 && !it->hpos)
11477 return;
11478 break;
11479 }
11480
11481 /* Produce glyphs. */
11482 n_glyphs_before = row->used[TEXT_AREA];
11483 it_before = *it;
11484
11485 PRODUCE_GLYPHS (it);
11486
11487 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11488 i = 0;
11489 x = it_before.current_x;
11490 while (i < nglyphs)
11491 {
11492 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11493
11494 if (x + glyph->pixel_width > max_x)
11495 {
11496 /* Glyph doesn't fit on line. Backtrack. */
11497 row->used[TEXT_AREA] = n_glyphs_before;
11498 *it = it_before;
11499 /* If this is the only glyph on this line, it will never fit on the
11500 tool-bar, so skip it. But ensure there is at least one glyph,
11501 so we don't accidentally disable the tool-bar. */
11502 if (n_glyphs_before == 0
11503 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11504 break;
11505 goto out;
11506 }
11507
11508 ++it->hpos;
11509 x += glyph->pixel_width;
11510 ++i;
11511 }
11512
11513 /* Stop at line end. */
11514 if (ITERATOR_AT_END_OF_LINE_P (it))
11515 break;
11516
11517 set_iterator_to_next (it, 1);
11518 }
11519
11520 out:;
11521
11522 row->displays_text_p = row->used[TEXT_AREA] != 0;
11523
11524 /* Use default face for the border below the tool bar.
11525
11526 FIXME: When auto-resize-tool-bars is grow-only, there is
11527 no additional border below the possibly empty tool-bar lines.
11528 So to make the extra empty lines look "normal", we have to
11529 use the tool-bar face for the border too. */
11530 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11531 it->face_id = DEFAULT_FACE_ID;
11532
11533 extend_face_to_end_of_line (it);
11534 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11535 last->right_box_line_p = 1;
11536 if (last == row->glyphs[TEXT_AREA])
11537 last->left_box_line_p = 1;
11538
11539 /* Make line the desired height and center it vertically. */
11540 if ((height -= it->max_ascent + it->max_descent) > 0)
11541 {
11542 /* Don't add more than one line height. */
11543 height %= FRAME_LINE_HEIGHT (it->f);
11544 it->max_ascent += height / 2;
11545 it->max_descent += (height + 1) / 2;
11546 }
11547
11548 compute_line_metrics (it);
11549
11550 /* If line is empty, make it occupy the rest of the tool-bar. */
11551 if (!row->displays_text_p)
11552 {
11553 row->height = row->phys_height = it->last_visible_y - row->y;
11554 row->visible_height = row->height;
11555 row->ascent = row->phys_ascent = 0;
11556 row->extra_line_spacing = 0;
11557 }
11558
11559 row->full_width_p = 1;
11560 row->continued_p = 0;
11561 row->truncated_on_left_p = 0;
11562 row->truncated_on_right_p = 0;
11563
11564 it->current_x = it->hpos = 0;
11565 it->current_y += row->height;
11566 ++it->vpos;
11567 ++it->glyph_row;
11568 }
11569
11570
11571 /* Max tool-bar height. */
11572
11573 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11574 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11575
11576 /* Value is the number of screen lines needed to make all tool-bar
11577 items of frame F visible. The number of actual rows needed is
11578 returned in *N_ROWS if non-NULL. */
11579
11580 static int
11581 tool_bar_lines_needed (struct frame *f, int *n_rows)
11582 {
11583 struct window *w = XWINDOW (f->tool_bar_window);
11584 struct it it;
11585 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11586 the desired matrix, so use (unused) mode-line row as temporary row to
11587 avoid destroying the first tool-bar row. */
11588 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11589
11590 /* Initialize an iterator for iteration over
11591 F->desired_tool_bar_string in the tool-bar window of frame F. */
11592 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11593 it.first_visible_x = 0;
11594 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11595 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11596 it.paragraph_embedding = L2R;
11597
11598 while (!ITERATOR_AT_END_P (&it))
11599 {
11600 clear_glyph_row (temp_row);
11601 it.glyph_row = temp_row;
11602 display_tool_bar_line (&it, -1);
11603 }
11604 clear_glyph_row (temp_row);
11605
11606 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11607 if (n_rows)
11608 *n_rows = it.vpos > 0 ? it.vpos : -1;
11609
11610 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11611 }
11612
11613
11614 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11615 0, 1, 0,
11616 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11617 (Lisp_Object frame)
11618 {
11619 struct frame *f;
11620 struct window *w;
11621 int nlines = 0;
11622
11623 if (NILP (frame))
11624 frame = selected_frame;
11625 else
11626 CHECK_FRAME (frame);
11627 f = XFRAME (frame);
11628
11629 if (WINDOWP (f->tool_bar_window)
11630 && (w = XWINDOW (f->tool_bar_window),
11631 WINDOW_TOTAL_LINES (w) > 0))
11632 {
11633 update_tool_bar (f, 1);
11634 if (f->n_tool_bar_items)
11635 {
11636 build_desired_tool_bar_string (f);
11637 nlines = tool_bar_lines_needed (f, NULL);
11638 }
11639 }
11640
11641 return make_number (nlines);
11642 }
11643
11644
11645 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11646 height should be changed. */
11647
11648 static int
11649 redisplay_tool_bar (struct frame *f)
11650 {
11651 struct window *w;
11652 struct it it;
11653 struct glyph_row *row;
11654
11655 #if defined (USE_GTK) || defined (HAVE_NS)
11656 if (FRAME_EXTERNAL_TOOL_BAR (f))
11657 update_frame_tool_bar (f);
11658 return 0;
11659 #endif
11660
11661 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11662 do anything. This means you must start with tool-bar-lines
11663 non-zero to get the auto-sizing effect. Or in other words, you
11664 can turn off tool-bars by specifying tool-bar-lines zero. */
11665 if (!WINDOWP (f->tool_bar_window)
11666 || (w = XWINDOW (f->tool_bar_window),
11667 WINDOW_TOTAL_LINES (w) == 0))
11668 return 0;
11669
11670 /* Set up an iterator for the tool-bar window. */
11671 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11672 it.first_visible_x = 0;
11673 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11674 row = it.glyph_row;
11675
11676 /* Build a string that represents the contents of the tool-bar. */
11677 build_desired_tool_bar_string (f);
11678 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11679 /* FIXME: This should be controlled by a user option. But it
11680 doesn't make sense to have an R2L tool bar if the menu bar cannot
11681 be drawn also R2L, and making the menu bar R2L is tricky due
11682 toolkit-specific code that implements it. If an R2L tool bar is
11683 ever supported, display_tool_bar_line should also be augmented to
11684 call unproduce_glyphs like display_line and display_string
11685 do. */
11686 it.paragraph_embedding = L2R;
11687
11688 if (f->n_tool_bar_rows == 0)
11689 {
11690 int nlines;
11691
11692 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11693 nlines != WINDOW_TOTAL_LINES (w)))
11694 {
11695 Lisp_Object frame;
11696 int old_height = WINDOW_TOTAL_LINES (w);
11697
11698 XSETFRAME (frame, f);
11699 Fmodify_frame_parameters (frame,
11700 Fcons (Fcons (Qtool_bar_lines,
11701 make_number (nlines)),
11702 Qnil));
11703 if (WINDOW_TOTAL_LINES (w) != old_height)
11704 {
11705 clear_glyph_matrix (w->desired_matrix);
11706 fonts_changed_p = 1;
11707 return 1;
11708 }
11709 }
11710 }
11711
11712 /* Display as many lines as needed to display all tool-bar items. */
11713
11714 if (f->n_tool_bar_rows > 0)
11715 {
11716 int border, rows, height, extra;
11717
11718 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11719 border = XINT (Vtool_bar_border);
11720 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11721 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11722 else if (EQ (Vtool_bar_border, Qborder_width))
11723 border = f->border_width;
11724 else
11725 border = 0;
11726 if (border < 0)
11727 border = 0;
11728
11729 rows = f->n_tool_bar_rows;
11730 height = max (1, (it.last_visible_y - border) / rows);
11731 extra = it.last_visible_y - border - height * rows;
11732
11733 while (it.current_y < it.last_visible_y)
11734 {
11735 int h = 0;
11736 if (extra > 0 && rows-- > 0)
11737 {
11738 h = (extra + rows - 1) / rows;
11739 extra -= h;
11740 }
11741 display_tool_bar_line (&it, height + h);
11742 }
11743 }
11744 else
11745 {
11746 while (it.current_y < it.last_visible_y)
11747 display_tool_bar_line (&it, 0);
11748 }
11749
11750 /* It doesn't make much sense to try scrolling in the tool-bar
11751 window, so don't do it. */
11752 w->desired_matrix->no_scrolling_p = 1;
11753 w->must_be_updated_p = 1;
11754
11755 if (!NILP (Vauto_resize_tool_bars))
11756 {
11757 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11758 int change_height_p = 0;
11759
11760 /* If we couldn't display everything, change the tool-bar's
11761 height if there is room for more. */
11762 if (IT_STRING_CHARPOS (it) < it.end_charpos
11763 && it.current_y < max_tool_bar_height)
11764 change_height_p = 1;
11765
11766 row = it.glyph_row - 1;
11767
11768 /* If there are blank lines at the end, except for a partially
11769 visible blank line at the end that is smaller than
11770 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11771 if (!row->displays_text_p
11772 && row->height >= FRAME_LINE_HEIGHT (f))
11773 change_height_p = 1;
11774
11775 /* If row displays tool-bar items, but is partially visible,
11776 change the tool-bar's height. */
11777 if (row->displays_text_p
11778 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11779 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11780 change_height_p = 1;
11781
11782 /* Resize windows as needed by changing the `tool-bar-lines'
11783 frame parameter. */
11784 if (change_height_p)
11785 {
11786 Lisp_Object frame;
11787 int old_height = WINDOW_TOTAL_LINES (w);
11788 int nrows;
11789 int nlines = tool_bar_lines_needed (f, &nrows);
11790
11791 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11792 && !f->minimize_tool_bar_window_p)
11793 ? (nlines > old_height)
11794 : (nlines != old_height));
11795 f->minimize_tool_bar_window_p = 0;
11796
11797 if (change_height_p)
11798 {
11799 XSETFRAME (frame, f);
11800 Fmodify_frame_parameters (frame,
11801 Fcons (Fcons (Qtool_bar_lines,
11802 make_number (nlines)),
11803 Qnil));
11804 if (WINDOW_TOTAL_LINES (w) != old_height)
11805 {
11806 clear_glyph_matrix (w->desired_matrix);
11807 f->n_tool_bar_rows = nrows;
11808 fonts_changed_p = 1;
11809 return 1;
11810 }
11811 }
11812 }
11813 }
11814
11815 f->minimize_tool_bar_window_p = 0;
11816 return 0;
11817 }
11818
11819
11820 /* Get information about the tool-bar item which is displayed in GLYPH
11821 on frame F. Return in *PROP_IDX the index where tool-bar item
11822 properties start in F->tool_bar_items. Value is zero if
11823 GLYPH doesn't display a tool-bar item. */
11824
11825 static int
11826 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11827 {
11828 Lisp_Object prop;
11829 int success_p;
11830 int charpos;
11831
11832 /* This function can be called asynchronously, which means we must
11833 exclude any possibility that Fget_text_property signals an
11834 error. */
11835 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11836 charpos = max (0, charpos);
11837
11838 /* Get the text property `menu-item' at pos. The value of that
11839 property is the start index of this item's properties in
11840 F->tool_bar_items. */
11841 prop = Fget_text_property (make_number (charpos),
11842 Qmenu_item, f->current_tool_bar_string);
11843 if (INTEGERP (prop))
11844 {
11845 *prop_idx = XINT (prop);
11846 success_p = 1;
11847 }
11848 else
11849 success_p = 0;
11850
11851 return success_p;
11852 }
11853
11854 \f
11855 /* Get information about the tool-bar item at position X/Y on frame F.
11856 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11857 the current matrix of the tool-bar window of F, or NULL if not
11858 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11859 item in F->tool_bar_items. Value is
11860
11861 -1 if X/Y is not on a tool-bar item
11862 0 if X/Y is on the same item that was highlighted before.
11863 1 otherwise. */
11864
11865 static int
11866 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11867 int *hpos, int *vpos, int *prop_idx)
11868 {
11869 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11870 struct window *w = XWINDOW (f->tool_bar_window);
11871 int area;
11872
11873 /* Find the glyph under X/Y. */
11874 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11875 if (*glyph == NULL)
11876 return -1;
11877
11878 /* Get the start of this tool-bar item's properties in
11879 f->tool_bar_items. */
11880 if (!tool_bar_item_info (f, *glyph, prop_idx))
11881 return -1;
11882
11883 /* Is mouse on the highlighted item? */
11884 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11885 && *vpos >= hlinfo->mouse_face_beg_row
11886 && *vpos <= hlinfo->mouse_face_end_row
11887 && (*vpos > hlinfo->mouse_face_beg_row
11888 || *hpos >= hlinfo->mouse_face_beg_col)
11889 && (*vpos < hlinfo->mouse_face_end_row
11890 || *hpos < hlinfo->mouse_face_end_col
11891 || hlinfo->mouse_face_past_end))
11892 return 0;
11893
11894 return 1;
11895 }
11896
11897
11898 /* EXPORT:
11899 Handle mouse button event on the tool-bar of frame F, at
11900 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11901 0 for button release. MODIFIERS is event modifiers for button
11902 release. */
11903
11904 void
11905 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11906 int modifiers)
11907 {
11908 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11909 struct window *w = XWINDOW (f->tool_bar_window);
11910 int hpos, vpos, prop_idx;
11911 struct glyph *glyph;
11912 Lisp_Object enabled_p;
11913
11914 /* If not on the highlighted tool-bar item, return. */
11915 frame_to_window_pixel_xy (w, &x, &y);
11916 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11917 return;
11918
11919 /* If item is disabled, do nothing. */
11920 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11921 if (NILP (enabled_p))
11922 return;
11923
11924 if (down_p)
11925 {
11926 /* Show item in pressed state. */
11927 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11928 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11929 last_tool_bar_item = prop_idx;
11930 }
11931 else
11932 {
11933 Lisp_Object key, frame;
11934 struct input_event event;
11935 EVENT_INIT (event);
11936
11937 /* Show item in released state. */
11938 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11939 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11940
11941 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11942
11943 XSETFRAME (frame, f);
11944 event.kind = TOOL_BAR_EVENT;
11945 event.frame_or_window = frame;
11946 event.arg = frame;
11947 kbd_buffer_store_event (&event);
11948
11949 event.kind = TOOL_BAR_EVENT;
11950 event.frame_or_window = frame;
11951 event.arg = key;
11952 event.modifiers = modifiers;
11953 kbd_buffer_store_event (&event);
11954 last_tool_bar_item = -1;
11955 }
11956 }
11957
11958
11959 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11960 tool-bar window-relative coordinates X/Y. Called from
11961 note_mouse_highlight. */
11962
11963 static void
11964 note_tool_bar_highlight (struct frame *f, int x, int y)
11965 {
11966 Lisp_Object window = f->tool_bar_window;
11967 struct window *w = XWINDOW (window);
11968 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11969 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11970 int hpos, vpos;
11971 struct glyph *glyph;
11972 struct glyph_row *row;
11973 int i;
11974 Lisp_Object enabled_p;
11975 int prop_idx;
11976 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11977 int mouse_down_p, rc;
11978
11979 /* Function note_mouse_highlight is called with negative X/Y
11980 values when mouse moves outside of the frame. */
11981 if (x <= 0 || y <= 0)
11982 {
11983 clear_mouse_face (hlinfo);
11984 return;
11985 }
11986
11987 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11988 if (rc < 0)
11989 {
11990 /* Not on tool-bar item. */
11991 clear_mouse_face (hlinfo);
11992 return;
11993 }
11994 else if (rc == 0)
11995 /* On same tool-bar item as before. */
11996 goto set_help_echo;
11997
11998 clear_mouse_face (hlinfo);
11999
12000 /* Mouse is down, but on different tool-bar item? */
12001 mouse_down_p = (dpyinfo->grabbed
12002 && f == last_mouse_frame
12003 && FRAME_LIVE_P (f));
12004 if (mouse_down_p
12005 && last_tool_bar_item != prop_idx)
12006 return;
12007
12008 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12009 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12010
12011 /* If tool-bar item is not enabled, don't highlight it. */
12012 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12013 if (!NILP (enabled_p))
12014 {
12015 /* Compute the x-position of the glyph. In front and past the
12016 image is a space. We include this in the highlighted area. */
12017 row = MATRIX_ROW (w->current_matrix, vpos);
12018 for (i = x = 0; i < hpos; ++i)
12019 x += row->glyphs[TEXT_AREA][i].pixel_width;
12020
12021 /* Record this as the current active region. */
12022 hlinfo->mouse_face_beg_col = hpos;
12023 hlinfo->mouse_face_beg_row = vpos;
12024 hlinfo->mouse_face_beg_x = x;
12025 hlinfo->mouse_face_beg_y = row->y;
12026 hlinfo->mouse_face_past_end = 0;
12027
12028 hlinfo->mouse_face_end_col = hpos + 1;
12029 hlinfo->mouse_face_end_row = vpos;
12030 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12031 hlinfo->mouse_face_end_y = row->y;
12032 hlinfo->mouse_face_window = window;
12033 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12034
12035 /* Display it as active. */
12036 show_mouse_face (hlinfo, draw);
12037 hlinfo->mouse_face_image_state = draw;
12038 }
12039
12040 set_help_echo:
12041
12042 /* Set help_echo_string to a help string to display for this tool-bar item.
12043 XTread_socket does the rest. */
12044 help_echo_object = help_echo_window = Qnil;
12045 help_echo_pos = -1;
12046 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12047 if (NILP (help_echo_string))
12048 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12049 }
12050
12051 #endif /* HAVE_WINDOW_SYSTEM */
12052
12053
12054 \f
12055 /************************************************************************
12056 Horizontal scrolling
12057 ************************************************************************/
12058
12059 static int hscroll_window_tree (Lisp_Object);
12060 static int hscroll_windows (Lisp_Object);
12061
12062 /* For all leaf windows in the window tree rooted at WINDOW, set their
12063 hscroll value so that PT is (i) visible in the window, and (ii) so
12064 that it is not within a certain margin at the window's left and
12065 right border. Value is non-zero if any window's hscroll has been
12066 changed. */
12067
12068 static int
12069 hscroll_window_tree (Lisp_Object window)
12070 {
12071 int hscrolled_p = 0;
12072 int hscroll_relative_p = FLOATP (Vhscroll_step);
12073 int hscroll_step_abs = 0;
12074 double hscroll_step_rel = 0;
12075
12076 if (hscroll_relative_p)
12077 {
12078 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12079 if (hscroll_step_rel < 0)
12080 {
12081 hscroll_relative_p = 0;
12082 hscroll_step_abs = 0;
12083 }
12084 }
12085 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12086 {
12087 hscroll_step_abs = XINT (Vhscroll_step);
12088 if (hscroll_step_abs < 0)
12089 hscroll_step_abs = 0;
12090 }
12091 else
12092 hscroll_step_abs = 0;
12093
12094 while (WINDOWP (window))
12095 {
12096 struct window *w = XWINDOW (window);
12097
12098 if (WINDOWP (w->hchild))
12099 hscrolled_p |= hscroll_window_tree (w->hchild);
12100 else if (WINDOWP (w->vchild))
12101 hscrolled_p |= hscroll_window_tree (w->vchild);
12102 else if (w->cursor.vpos >= 0)
12103 {
12104 int h_margin;
12105 int text_area_width;
12106 struct glyph_row *current_cursor_row
12107 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12108 struct glyph_row *desired_cursor_row
12109 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12110 struct glyph_row *cursor_row
12111 = (desired_cursor_row->enabled_p
12112 ? desired_cursor_row
12113 : current_cursor_row);
12114 int row_r2l_p = cursor_row->reversed_p;
12115
12116 text_area_width = window_box_width (w, TEXT_AREA);
12117
12118 /* Scroll when cursor is inside this scroll margin. */
12119 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12120
12121 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12122 /* For left-to-right rows, hscroll when cursor is either
12123 (i) inside the right hscroll margin, or (ii) if it is
12124 inside the left margin and the window is already
12125 hscrolled. */
12126 && ((!row_r2l_p
12127 && ((XFASTINT (w->hscroll)
12128 && w->cursor.x <= h_margin)
12129 || (cursor_row->enabled_p
12130 && cursor_row->truncated_on_right_p
12131 && (w->cursor.x >= text_area_width - h_margin))))
12132 /* For right-to-left rows, the logic is similar,
12133 except that rules for scrolling to left and right
12134 are reversed. E.g., if cursor.x <= h_margin, we
12135 need to hscroll "to the right" unconditionally,
12136 and that will scroll the screen to the left so as
12137 to reveal the next portion of the row. */
12138 || (row_r2l_p
12139 && ((cursor_row->enabled_p
12140 /* FIXME: It is confusing to set the
12141 truncated_on_right_p flag when R2L rows
12142 are actually truncated on the left. */
12143 && cursor_row->truncated_on_right_p
12144 && w->cursor.x <= h_margin)
12145 || (XFASTINT (w->hscroll)
12146 && (w->cursor.x >= text_area_width - h_margin))))))
12147 {
12148 struct it it;
12149 ptrdiff_t hscroll;
12150 struct buffer *saved_current_buffer;
12151 ptrdiff_t pt;
12152 int wanted_x;
12153
12154 /* Find point in a display of infinite width. */
12155 saved_current_buffer = current_buffer;
12156 current_buffer = XBUFFER (w->buffer);
12157
12158 if (w == XWINDOW (selected_window))
12159 pt = PT;
12160 else
12161 {
12162 pt = marker_position (w->pointm);
12163 pt = max (BEGV, pt);
12164 pt = min (ZV, pt);
12165 }
12166
12167 /* Move iterator to pt starting at cursor_row->start in
12168 a line with infinite width. */
12169 init_to_row_start (&it, w, cursor_row);
12170 it.last_visible_x = INFINITY;
12171 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12172 current_buffer = saved_current_buffer;
12173
12174 /* Position cursor in window. */
12175 if (!hscroll_relative_p && hscroll_step_abs == 0)
12176 hscroll = max (0, (it.current_x
12177 - (ITERATOR_AT_END_OF_LINE_P (&it)
12178 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12179 : (text_area_width / 2))))
12180 / FRAME_COLUMN_WIDTH (it.f);
12181 else if ((!row_r2l_p
12182 && w->cursor.x >= text_area_width - h_margin)
12183 || (row_r2l_p && w->cursor.x <= h_margin))
12184 {
12185 if (hscroll_relative_p)
12186 wanted_x = text_area_width * (1 - hscroll_step_rel)
12187 - h_margin;
12188 else
12189 wanted_x = text_area_width
12190 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12191 - h_margin;
12192 hscroll
12193 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12194 }
12195 else
12196 {
12197 if (hscroll_relative_p)
12198 wanted_x = text_area_width * hscroll_step_rel
12199 + h_margin;
12200 else
12201 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12202 + h_margin;
12203 hscroll
12204 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12205 }
12206 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12207
12208 /* Don't prevent redisplay optimizations if hscroll
12209 hasn't changed, as it will unnecessarily slow down
12210 redisplay. */
12211 if (XFASTINT (w->hscroll) != hscroll)
12212 {
12213 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12214 w->hscroll = make_number (hscroll);
12215 hscrolled_p = 1;
12216 }
12217 }
12218 }
12219
12220 window = w->next;
12221 }
12222
12223 /* Value is non-zero if hscroll of any leaf window has been changed. */
12224 return hscrolled_p;
12225 }
12226
12227
12228 /* Set hscroll so that cursor is visible and not inside horizontal
12229 scroll margins for all windows in the tree rooted at WINDOW. See
12230 also hscroll_window_tree above. Value is non-zero if any window's
12231 hscroll has been changed. If it has, desired matrices on the frame
12232 of WINDOW are cleared. */
12233
12234 static int
12235 hscroll_windows (Lisp_Object window)
12236 {
12237 int hscrolled_p = hscroll_window_tree (window);
12238 if (hscrolled_p)
12239 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12240 return hscrolled_p;
12241 }
12242
12243
12244 \f
12245 /************************************************************************
12246 Redisplay
12247 ************************************************************************/
12248
12249 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12250 to a non-zero value. This is sometimes handy to have in a debugger
12251 session. */
12252
12253 #if GLYPH_DEBUG
12254
12255 /* First and last unchanged row for try_window_id. */
12256
12257 static int debug_first_unchanged_at_end_vpos;
12258 static int debug_last_unchanged_at_beg_vpos;
12259
12260 /* Delta vpos and y. */
12261
12262 static int debug_dvpos, debug_dy;
12263
12264 /* Delta in characters and bytes for try_window_id. */
12265
12266 static ptrdiff_t debug_delta, debug_delta_bytes;
12267
12268 /* Values of window_end_pos and window_end_vpos at the end of
12269 try_window_id. */
12270
12271 static ptrdiff_t debug_end_vpos;
12272
12273 /* Append a string to W->desired_matrix->method. FMT is a printf
12274 format string. If trace_redisplay_p is non-zero also printf the
12275 resulting string to stderr. */
12276
12277 static void debug_method_add (struct window *, char const *, ...)
12278 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12279
12280 static void
12281 debug_method_add (struct window *w, char const *fmt, ...)
12282 {
12283 char buffer[512];
12284 char *method = w->desired_matrix->method;
12285 int len = strlen (method);
12286 int size = sizeof w->desired_matrix->method;
12287 int remaining = size - len - 1;
12288 va_list ap;
12289
12290 va_start (ap, fmt);
12291 vsprintf (buffer, fmt, ap);
12292 va_end (ap);
12293 if (len && remaining)
12294 {
12295 method[len] = '|';
12296 --remaining, ++len;
12297 }
12298
12299 strncpy (method + len, buffer, remaining);
12300
12301 if (trace_redisplay_p)
12302 fprintf (stderr, "%p (%s): %s\n",
12303 w,
12304 ((BUFFERP (w->buffer)
12305 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12306 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12307 : "no buffer"),
12308 buffer);
12309 }
12310
12311 #endif /* GLYPH_DEBUG */
12312
12313
12314 /* Value is non-zero if all changes in window W, which displays
12315 current_buffer, are in the text between START and END. START is a
12316 buffer position, END is given as a distance from Z. Used in
12317 redisplay_internal for display optimization. */
12318
12319 static inline int
12320 text_outside_line_unchanged_p (struct window *w,
12321 ptrdiff_t start, ptrdiff_t end)
12322 {
12323 int unchanged_p = 1;
12324
12325 /* If text or overlays have changed, see where. */
12326 if (XFASTINT (w->last_modified) < MODIFF
12327 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12328 {
12329 /* Gap in the line? */
12330 if (GPT < start || Z - GPT < end)
12331 unchanged_p = 0;
12332
12333 /* Changes start in front of the line, or end after it? */
12334 if (unchanged_p
12335 && (BEG_UNCHANGED < start - 1
12336 || END_UNCHANGED < end))
12337 unchanged_p = 0;
12338
12339 /* If selective display, can't optimize if changes start at the
12340 beginning of the line. */
12341 if (unchanged_p
12342 && INTEGERP (BVAR (current_buffer, selective_display))
12343 && XINT (BVAR (current_buffer, selective_display)) > 0
12344 && (BEG_UNCHANGED < start || GPT <= start))
12345 unchanged_p = 0;
12346
12347 /* If there are overlays at the start or end of the line, these
12348 may have overlay strings with newlines in them. A change at
12349 START, for instance, may actually concern the display of such
12350 overlay strings as well, and they are displayed on different
12351 lines. So, quickly rule out this case. (For the future, it
12352 might be desirable to implement something more telling than
12353 just BEG/END_UNCHANGED.) */
12354 if (unchanged_p)
12355 {
12356 if (BEG + BEG_UNCHANGED == start
12357 && overlay_touches_p (start))
12358 unchanged_p = 0;
12359 if (END_UNCHANGED == end
12360 && overlay_touches_p (Z - end))
12361 unchanged_p = 0;
12362 }
12363
12364 /* Under bidi reordering, adding or deleting a character in the
12365 beginning of a paragraph, before the first strong directional
12366 character, can change the base direction of the paragraph (unless
12367 the buffer specifies a fixed paragraph direction), which will
12368 require to redisplay the whole paragraph. It might be worthwhile
12369 to find the paragraph limits and widen the range of redisplayed
12370 lines to that, but for now just give up this optimization. */
12371 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12372 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12373 unchanged_p = 0;
12374 }
12375
12376 return unchanged_p;
12377 }
12378
12379
12380 /* Do a frame update, taking possible shortcuts into account. This is
12381 the main external entry point for redisplay.
12382
12383 If the last redisplay displayed an echo area message and that message
12384 is no longer requested, we clear the echo area or bring back the
12385 mini-buffer if that is in use. */
12386
12387 void
12388 redisplay (void)
12389 {
12390 redisplay_internal ();
12391 }
12392
12393
12394 static Lisp_Object
12395 overlay_arrow_string_or_property (Lisp_Object var)
12396 {
12397 Lisp_Object val;
12398
12399 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12400 return val;
12401
12402 return Voverlay_arrow_string;
12403 }
12404
12405 /* Return 1 if there are any overlay-arrows in current_buffer. */
12406 static int
12407 overlay_arrow_in_current_buffer_p (void)
12408 {
12409 Lisp_Object vlist;
12410
12411 for (vlist = Voverlay_arrow_variable_list;
12412 CONSP (vlist);
12413 vlist = XCDR (vlist))
12414 {
12415 Lisp_Object var = XCAR (vlist);
12416 Lisp_Object val;
12417
12418 if (!SYMBOLP (var))
12419 continue;
12420 val = find_symbol_value (var);
12421 if (MARKERP (val)
12422 && current_buffer == XMARKER (val)->buffer)
12423 return 1;
12424 }
12425 return 0;
12426 }
12427
12428
12429 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12430 has changed. */
12431
12432 static int
12433 overlay_arrows_changed_p (void)
12434 {
12435 Lisp_Object vlist;
12436
12437 for (vlist = Voverlay_arrow_variable_list;
12438 CONSP (vlist);
12439 vlist = XCDR (vlist))
12440 {
12441 Lisp_Object var = XCAR (vlist);
12442 Lisp_Object val, pstr;
12443
12444 if (!SYMBOLP (var))
12445 continue;
12446 val = find_symbol_value (var);
12447 if (!MARKERP (val))
12448 continue;
12449 if (! EQ (COERCE_MARKER (val),
12450 Fget (var, Qlast_arrow_position))
12451 || ! (pstr = overlay_arrow_string_or_property (var),
12452 EQ (pstr, Fget (var, Qlast_arrow_string))))
12453 return 1;
12454 }
12455 return 0;
12456 }
12457
12458 /* Mark overlay arrows to be updated on next redisplay. */
12459
12460 static void
12461 update_overlay_arrows (int up_to_date)
12462 {
12463 Lisp_Object vlist;
12464
12465 for (vlist = Voverlay_arrow_variable_list;
12466 CONSP (vlist);
12467 vlist = XCDR (vlist))
12468 {
12469 Lisp_Object var = XCAR (vlist);
12470
12471 if (!SYMBOLP (var))
12472 continue;
12473
12474 if (up_to_date > 0)
12475 {
12476 Lisp_Object val = find_symbol_value (var);
12477 Fput (var, Qlast_arrow_position,
12478 COERCE_MARKER (val));
12479 Fput (var, Qlast_arrow_string,
12480 overlay_arrow_string_or_property (var));
12481 }
12482 else if (up_to_date < 0
12483 || !NILP (Fget (var, Qlast_arrow_position)))
12484 {
12485 Fput (var, Qlast_arrow_position, Qt);
12486 Fput (var, Qlast_arrow_string, Qt);
12487 }
12488 }
12489 }
12490
12491
12492 /* Return overlay arrow string to display at row.
12493 Return integer (bitmap number) for arrow bitmap in left fringe.
12494 Return nil if no overlay arrow. */
12495
12496 static Lisp_Object
12497 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12498 {
12499 Lisp_Object vlist;
12500
12501 for (vlist = Voverlay_arrow_variable_list;
12502 CONSP (vlist);
12503 vlist = XCDR (vlist))
12504 {
12505 Lisp_Object var = XCAR (vlist);
12506 Lisp_Object val;
12507
12508 if (!SYMBOLP (var))
12509 continue;
12510
12511 val = find_symbol_value (var);
12512
12513 if (MARKERP (val)
12514 && current_buffer == XMARKER (val)->buffer
12515 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12516 {
12517 if (FRAME_WINDOW_P (it->f)
12518 /* FIXME: if ROW->reversed_p is set, this should test
12519 the right fringe, not the left one. */
12520 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12521 {
12522 #ifdef HAVE_WINDOW_SYSTEM
12523 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12524 {
12525 int fringe_bitmap;
12526 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12527 return make_number (fringe_bitmap);
12528 }
12529 #endif
12530 return make_number (-1); /* Use default arrow bitmap */
12531 }
12532 return overlay_arrow_string_or_property (var);
12533 }
12534 }
12535
12536 return Qnil;
12537 }
12538
12539 /* Return 1 if point moved out of or into a composition. Otherwise
12540 return 0. PREV_BUF and PREV_PT are the last point buffer and
12541 position. BUF and PT are the current point buffer and position. */
12542
12543 static int
12544 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12545 struct buffer *buf, ptrdiff_t pt)
12546 {
12547 ptrdiff_t start, end;
12548 Lisp_Object prop;
12549 Lisp_Object buffer;
12550
12551 XSETBUFFER (buffer, buf);
12552 /* Check a composition at the last point if point moved within the
12553 same buffer. */
12554 if (prev_buf == buf)
12555 {
12556 if (prev_pt == pt)
12557 /* Point didn't move. */
12558 return 0;
12559
12560 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12561 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12562 && COMPOSITION_VALID_P (start, end, prop)
12563 && start < prev_pt && end > prev_pt)
12564 /* The last point was within the composition. Return 1 iff
12565 point moved out of the composition. */
12566 return (pt <= start || pt >= end);
12567 }
12568
12569 /* Check a composition at the current point. */
12570 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12571 && find_composition (pt, -1, &start, &end, &prop, buffer)
12572 && COMPOSITION_VALID_P (start, end, prop)
12573 && start < pt && end > pt);
12574 }
12575
12576
12577 /* Reconsider the setting of B->clip_changed which is displayed
12578 in window W. */
12579
12580 static inline void
12581 reconsider_clip_changes (struct window *w, struct buffer *b)
12582 {
12583 if (b->clip_changed
12584 && !NILP (w->window_end_valid)
12585 && w->current_matrix->buffer == b
12586 && w->current_matrix->zv == BUF_ZV (b)
12587 && w->current_matrix->begv == BUF_BEGV (b))
12588 b->clip_changed = 0;
12589
12590 /* If display wasn't paused, and W is not a tool bar window, see if
12591 point has been moved into or out of a composition. In that case,
12592 we set b->clip_changed to 1 to force updating the screen. If
12593 b->clip_changed has already been set to 1, we can skip this
12594 check. */
12595 if (!b->clip_changed
12596 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12597 {
12598 ptrdiff_t pt;
12599
12600 if (w == XWINDOW (selected_window))
12601 pt = PT;
12602 else
12603 pt = marker_position (w->pointm);
12604
12605 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12606 || pt != XINT (w->last_point))
12607 && check_point_in_composition (w->current_matrix->buffer,
12608 XINT (w->last_point),
12609 XBUFFER (w->buffer), pt))
12610 b->clip_changed = 1;
12611 }
12612 }
12613 \f
12614
12615 /* Select FRAME to forward the values of frame-local variables into C
12616 variables so that the redisplay routines can access those values
12617 directly. */
12618
12619 static void
12620 select_frame_for_redisplay (Lisp_Object frame)
12621 {
12622 Lisp_Object tail, tem;
12623 Lisp_Object old = selected_frame;
12624 struct Lisp_Symbol *sym;
12625
12626 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12627
12628 selected_frame = frame;
12629
12630 do {
12631 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12632 if (CONSP (XCAR (tail))
12633 && (tem = XCAR (XCAR (tail)),
12634 SYMBOLP (tem))
12635 && (sym = indirect_variable (XSYMBOL (tem)),
12636 sym->redirect == SYMBOL_LOCALIZED)
12637 && sym->val.blv->frame_local)
12638 /* Use find_symbol_value rather than Fsymbol_value
12639 to avoid an error if it is void. */
12640 find_symbol_value (tem);
12641 } while (!EQ (frame, old) && (frame = old, 1));
12642 }
12643
12644
12645 #define STOP_POLLING \
12646 do { if (! polling_stopped_here) stop_polling (); \
12647 polling_stopped_here = 1; } while (0)
12648
12649 #define RESUME_POLLING \
12650 do { if (polling_stopped_here) start_polling (); \
12651 polling_stopped_here = 0; } while (0)
12652
12653
12654 /* Perhaps in the future avoid recentering windows if it
12655 is not necessary; currently that causes some problems. */
12656
12657 static void
12658 redisplay_internal (void)
12659 {
12660 struct window *w = XWINDOW (selected_window);
12661 struct window *sw;
12662 struct frame *fr;
12663 int pending;
12664 int must_finish = 0;
12665 struct text_pos tlbufpos, tlendpos;
12666 int number_of_visible_frames;
12667 ptrdiff_t count, count1;
12668 struct frame *sf;
12669 int polling_stopped_here = 0;
12670 Lisp_Object old_frame = selected_frame;
12671
12672 /* Non-zero means redisplay has to consider all windows on all
12673 frames. Zero means, only selected_window is considered. */
12674 int consider_all_windows_p;
12675
12676 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12677
12678 /* No redisplay if running in batch mode or frame is not yet fully
12679 initialized, or redisplay is explicitly turned off by setting
12680 Vinhibit_redisplay. */
12681 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12682 || !NILP (Vinhibit_redisplay))
12683 return;
12684
12685 /* Don't examine these until after testing Vinhibit_redisplay.
12686 When Emacs is shutting down, perhaps because its connection to
12687 X has dropped, we should not look at them at all. */
12688 fr = XFRAME (w->frame);
12689 sf = SELECTED_FRAME ();
12690
12691 if (!fr->glyphs_initialized_p)
12692 return;
12693
12694 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12695 if (popup_activated ())
12696 return;
12697 #endif
12698
12699 /* I don't think this happens but let's be paranoid. */
12700 if (redisplaying_p)
12701 return;
12702
12703 /* Record a function that resets redisplaying_p to its old value
12704 when we leave this function. */
12705 count = SPECPDL_INDEX ();
12706 record_unwind_protect (unwind_redisplay,
12707 Fcons (make_number (redisplaying_p), selected_frame));
12708 ++redisplaying_p;
12709 specbind (Qinhibit_free_realized_faces, Qnil);
12710
12711 {
12712 Lisp_Object tail, frame;
12713
12714 FOR_EACH_FRAME (tail, frame)
12715 {
12716 struct frame *f = XFRAME (frame);
12717 f->already_hscrolled_p = 0;
12718 }
12719 }
12720
12721 retry:
12722 /* Remember the currently selected window. */
12723 sw = w;
12724
12725 if (!EQ (old_frame, selected_frame)
12726 && FRAME_LIVE_P (XFRAME (old_frame)))
12727 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12728 selected_frame and selected_window to be temporarily out-of-sync so
12729 when we come back here via `goto retry', we need to resync because we
12730 may need to run Elisp code (via prepare_menu_bars). */
12731 select_frame_for_redisplay (old_frame);
12732
12733 pending = 0;
12734 reconsider_clip_changes (w, current_buffer);
12735 last_escape_glyph_frame = NULL;
12736 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12737 last_glyphless_glyph_frame = NULL;
12738 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12739
12740 /* If new fonts have been loaded that make a glyph matrix adjustment
12741 necessary, do it. */
12742 if (fonts_changed_p)
12743 {
12744 adjust_glyphs (NULL);
12745 ++windows_or_buffers_changed;
12746 fonts_changed_p = 0;
12747 }
12748
12749 /* If face_change_count is non-zero, init_iterator will free all
12750 realized faces, which includes the faces referenced from current
12751 matrices. So, we can't reuse current matrices in this case. */
12752 if (face_change_count)
12753 ++windows_or_buffers_changed;
12754
12755 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12756 && FRAME_TTY (sf)->previous_frame != sf)
12757 {
12758 /* Since frames on a single ASCII terminal share the same
12759 display area, displaying a different frame means redisplay
12760 the whole thing. */
12761 windows_or_buffers_changed++;
12762 SET_FRAME_GARBAGED (sf);
12763 #ifndef DOS_NT
12764 set_tty_color_mode (FRAME_TTY (sf), sf);
12765 #endif
12766 FRAME_TTY (sf)->previous_frame = sf;
12767 }
12768
12769 /* Set the visible flags for all frames. Do this before checking
12770 for resized or garbaged frames; they want to know if their frames
12771 are visible. See the comment in frame.h for
12772 FRAME_SAMPLE_VISIBILITY. */
12773 {
12774 Lisp_Object tail, frame;
12775
12776 number_of_visible_frames = 0;
12777
12778 FOR_EACH_FRAME (tail, frame)
12779 {
12780 struct frame *f = XFRAME (frame);
12781
12782 FRAME_SAMPLE_VISIBILITY (f);
12783 if (FRAME_VISIBLE_P (f))
12784 ++number_of_visible_frames;
12785 clear_desired_matrices (f);
12786 }
12787 }
12788
12789 /* Notice any pending interrupt request to change frame size. */
12790 do_pending_window_change (1);
12791
12792 /* do_pending_window_change could change the selected_window due to
12793 frame resizing which makes the selected window too small. */
12794 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12795 {
12796 sw = w;
12797 reconsider_clip_changes (w, current_buffer);
12798 }
12799
12800 /* Clear frames marked as garbaged. */
12801 if (frame_garbaged)
12802 clear_garbaged_frames ();
12803
12804 /* Build menubar and tool-bar items. */
12805 if (NILP (Vmemory_full))
12806 prepare_menu_bars ();
12807
12808 if (windows_or_buffers_changed)
12809 update_mode_lines++;
12810
12811 /* Detect case that we need to write or remove a star in the mode line. */
12812 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12813 {
12814 w->update_mode_line = Qt;
12815 if (buffer_shared > 1)
12816 update_mode_lines++;
12817 }
12818
12819 /* Avoid invocation of point motion hooks by `current_column' below. */
12820 count1 = SPECPDL_INDEX ();
12821 specbind (Qinhibit_point_motion_hooks, Qt);
12822
12823 /* If %c is in the mode line, update it if needed. */
12824 if (!NILP (w->column_number_displayed)
12825 /* This alternative quickly identifies a common case
12826 where no change is needed. */
12827 && !(PT == XFASTINT (w->last_point)
12828 && XFASTINT (w->last_modified) >= MODIFF
12829 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12830 && (XFASTINT (w->column_number_displayed) != current_column ()))
12831 w->update_mode_line = Qt;
12832
12833 unbind_to (count1, Qnil);
12834
12835 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12836
12837 /* The variable buffer_shared is set in redisplay_window and
12838 indicates that we redisplay a buffer in different windows. See
12839 there. */
12840 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12841 || cursor_type_changed);
12842
12843 /* If specs for an arrow have changed, do thorough redisplay
12844 to ensure we remove any arrow that should no longer exist. */
12845 if (overlay_arrows_changed_p ())
12846 consider_all_windows_p = windows_or_buffers_changed = 1;
12847
12848 /* Normally the message* functions will have already displayed and
12849 updated the echo area, but the frame may have been trashed, or
12850 the update may have been preempted, so display the echo area
12851 again here. Checking message_cleared_p captures the case that
12852 the echo area should be cleared. */
12853 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12854 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12855 || (message_cleared_p
12856 && minibuf_level == 0
12857 /* If the mini-window is currently selected, this means the
12858 echo-area doesn't show through. */
12859 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12860 {
12861 int window_height_changed_p = echo_area_display (0);
12862 must_finish = 1;
12863
12864 /* If we don't display the current message, don't clear the
12865 message_cleared_p flag, because, if we did, we wouldn't clear
12866 the echo area in the next redisplay which doesn't preserve
12867 the echo area. */
12868 if (!display_last_displayed_message_p)
12869 message_cleared_p = 0;
12870
12871 if (fonts_changed_p)
12872 goto retry;
12873 else if (window_height_changed_p)
12874 {
12875 consider_all_windows_p = 1;
12876 ++update_mode_lines;
12877 ++windows_or_buffers_changed;
12878
12879 /* If window configuration was changed, frames may have been
12880 marked garbaged. Clear them or we will experience
12881 surprises wrt scrolling. */
12882 if (frame_garbaged)
12883 clear_garbaged_frames ();
12884 }
12885 }
12886 else if (EQ (selected_window, minibuf_window)
12887 && (current_buffer->clip_changed
12888 || XFASTINT (w->last_modified) < MODIFF
12889 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12890 && resize_mini_window (w, 0))
12891 {
12892 /* Resized active mini-window to fit the size of what it is
12893 showing if its contents might have changed. */
12894 must_finish = 1;
12895 /* FIXME: this causes all frames to be updated, which seems unnecessary
12896 since only the current frame needs to be considered. This function needs
12897 to be rewritten with two variables, consider_all_windows and
12898 consider_all_frames. */
12899 consider_all_windows_p = 1;
12900 ++windows_or_buffers_changed;
12901 ++update_mode_lines;
12902
12903 /* If window configuration was changed, frames may have been
12904 marked garbaged. Clear them or we will experience
12905 surprises wrt scrolling. */
12906 if (frame_garbaged)
12907 clear_garbaged_frames ();
12908 }
12909
12910
12911 /* If showing the region, and mark has changed, we must redisplay
12912 the whole window. The assignment to this_line_start_pos prevents
12913 the optimization directly below this if-statement. */
12914 if (((!NILP (Vtransient_mark_mode)
12915 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12916 != !NILP (w->region_showing))
12917 || (!NILP (w->region_showing)
12918 && !EQ (w->region_showing,
12919 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12920 CHARPOS (this_line_start_pos) = 0;
12921
12922 /* Optimize the case that only the line containing the cursor in the
12923 selected window has changed. Variables starting with this_ are
12924 set in display_line and record information about the line
12925 containing the cursor. */
12926 tlbufpos = this_line_start_pos;
12927 tlendpos = this_line_end_pos;
12928 if (!consider_all_windows_p
12929 && CHARPOS (tlbufpos) > 0
12930 && NILP (w->update_mode_line)
12931 && !current_buffer->clip_changed
12932 && !current_buffer->prevent_redisplay_optimizations_p
12933 && FRAME_VISIBLE_P (XFRAME (w->frame))
12934 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12935 /* Make sure recorded data applies to current buffer, etc. */
12936 && this_line_buffer == current_buffer
12937 && current_buffer == XBUFFER (w->buffer)
12938 && NILP (w->force_start)
12939 && NILP (w->optional_new_start)
12940 /* Point must be on the line that we have info recorded about. */
12941 && PT >= CHARPOS (tlbufpos)
12942 && PT <= Z - CHARPOS (tlendpos)
12943 /* All text outside that line, including its final newline,
12944 must be unchanged. */
12945 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12946 CHARPOS (tlendpos)))
12947 {
12948 if (CHARPOS (tlbufpos) > BEGV
12949 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12950 && (CHARPOS (tlbufpos) == ZV
12951 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12952 /* Former continuation line has disappeared by becoming empty. */
12953 goto cancel;
12954 else if (XFASTINT (w->last_modified) < MODIFF
12955 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12956 || MINI_WINDOW_P (w))
12957 {
12958 /* We have to handle the case of continuation around a
12959 wide-column character (see the comment in indent.c around
12960 line 1340).
12961
12962 For instance, in the following case:
12963
12964 -------- Insert --------
12965 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12966 J_I_ ==> J_I_ `^^' are cursors.
12967 ^^ ^^
12968 -------- --------
12969
12970 As we have to redraw the line above, we cannot use this
12971 optimization. */
12972
12973 struct it it;
12974 int line_height_before = this_line_pixel_height;
12975
12976 /* Note that start_display will handle the case that the
12977 line starting at tlbufpos is a continuation line. */
12978 start_display (&it, w, tlbufpos);
12979
12980 /* Implementation note: It this still necessary? */
12981 if (it.current_x != this_line_start_x)
12982 goto cancel;
12983
12984 TRACE ((stderr, "trying display optimization 1\n"));
12985 w->cursor.vpos = -1;
12986 overlay_arrow_seen = 0;
12987 it.vpos = this_line_vpos;
12988 it.current_y = this_line_y;
12989 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12990 display_line (&it);
12991
12992 /* If line contains point, is not continued,
12993 and ends at same distance from eob as before, we win. */
12994 if (w->cursor.vpos >= 0
12995 /* Line is not continued, otherwise this_line_start_pos
12996 would have been set to 0 in display_line. */
12997 && CHARPOS (this_line_start_pos)
12998 /* Line ends as before. */
12999 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13000 /* Line has same height as before. Otherwise other lines
13001 would have to be shifted up or down. */
13002 && this_line_pixel_height == line_height_before)
13003 {
13004 /* If this is not the window's last line, we must adjust
13005 the charstarts of the lines below. */
13006 if (it.current_y < it.last_visible_y)
13007 {
13008 struct glyph_row *row
13009 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13010 ptrdiff_t delta, delta_bytes;
13011
13012 /* We used to distinguish between two cases here,
13013 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13014 when the line ends in a newline or the end of the
13015 buffer's accessible portion. But both cases did
13016 the same, so they were collapsed. */
13017 delta = (Z
13018 - CHARPOS (tlendpos)
13019 - MATRIX_ROW_START_CHARPOS (row));
13020 delta_bytes = (Z_BYTE
13021 - BYTEPOS (tlendpos)
13022 - MATRIX_ROW_START_BYTEPOS (row));
13023
13024 increment_matrix_positions (w->current_matrix,
13025 this_line_vpos + 1,
13026 w->current_matrix->nrows,
13027 delta, delta_bytes);
13028 }
13029
13030 /* If this row displays text now but previously didn't,
13031 or vice versa, w->window_end_vpos may have to be
13032 adjusted. */
13033 if ((it.glyph_row - 1)->displays_text_p)
13034 {
13035 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13036 XSETINT (w->window_end_vpos, this_line_vpos);
13037 }
13038 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13039 && this_line_vpos > 0)
13040 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13041 w->window_end_valid = Qnil;
13042
13043 /* Update hint: No need to try to scroll in update_window. */
13044 w->desired_matrix->no_scrolling_p = 1;
13045
13046 #if GLYPH_DEBUG
13047 *w->desired_matrix->method = 0;
13048 debug_method_add (w, "optimization 1");
13049 #endif
13050 #ifdef HAVE_WINDOW_SYSTEM
13051 update_window_fringes (w, 0);
13052 #endif
13053 goto update;
13054 }
13055 else
13056 goto cancel;
13057 }
13058 else if (/* Cursor position hasn't changed. */
13059 PT == XFASTINT (w->last_point)
13060 /* Make sure the cursor was last displayed
13061 in this window. Otherwise we have to reposition it. */
13062 && 0 <= w->cursor.vpos
13063 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13064 {
13065 if (!must_finish)
13066 {
13067 do_pending_window_change (1);
13068 /* If selected_window changed, redisplay again. */
13069 if (WINDOWP (selected_window)
13070 && (w = XWINDOW (selected_window)) != sw)
13071 goto retry;
13072
13073 /* We used to always goto end_of_redisplay here, but this
13074 isn't enough if we have a blinking cursor. */
13075 if (w->cursor_off_p == w->last_cursor_off_p)
13076 goto end_of_redisplay;
13077 }
13078 goto update;
13079 }
13080 /* If highlighting the region, or if the cursor is in the echo area,
13081 then we can't just move the cursor. */
13082 else if (! (!NILP (Vtransient_mark_mode)
13083 && !NILP (BVAR (current_buffer, mark_active)))
13084 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13085 || highlight_nonselected_windows)
13086 && NILP (w->region_showing)
13087 && NILP (Vshow_trailing_whitespace)
13088 && !cursor_in_echo_area)
13089 {
13090 struct it it;
13091 struct glyph_row *row;
13092
13093 /* Skip from tlbufpos to PT and see where it is. Note that
13094 PT may be in invisible text. If so, we will end at the
13095 next visible position. */
13096 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13097 NULL, DEFAULT_FACE_ID);
13098 it.current_x = this_line_start_x;
13099 it.current_y = this_line_y;
13100 it.vpos = this_line_vpos;
13101
13102 /* The call to move_it_to stops in front of PT, but
13103 moves over before-strings. */
13104 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13105
13106 if (it.vpos == this_line_vpos
13107 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13108 row->enabled_p))
13109 {
13110 xassert (this_line_vpos == it.vpos);
13111 xassert (this_line_y == it.current_y);
13112 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13113 #if GLYPH_DEBUG
13114 *w->desired_matrix->method = 0;
13115 debug_method_add (w, "optimization 3");
13116 #endif
13117 goto update;
13118 }
13119 else
13120 goto cancel;
13121 }
13122
13123 cancel:
13124 /* Text changed drastically or point moved off of line. */
13125 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13126 }
13127
13128 CHARPOS (this_line_start_pos) = 0;
13129 consider_all_windows_p |= buffer_shared > 1;
13130 ++clear_face_cache_count;
13131 #ifdef HAVE_WINDOW_SYSTEM
13132 ++clear_image_cache_count;
13133 #endif
13134
13135 /* Build desired matrices, and update the display. If
13136 consider_all_windows_p is non-zero, do it for all windows on all
13137 frames. Otherwise do it for selected_window, only. */
13138
13139 if (consider_all_windows_p)
13140 {
13141 Lisp_Object tail, frame;
13142
13143 FOR_EACH_FRAME (tail, frame)
13144 XFRAME (frame)->updated_p = 0;
13145
13146 /* Recompute # windows showing selected buffer. This will be
13147 incremented each time such a window is displayed. */
13148 buffer_shared = 0;
13149
13150 FOR_EACH_FRAME (tail, frame)
13151 {
13152 struct frame *f = XFRAME (frame);
13153
13154 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13155 {
13156 if (! EQ (frame, selected_frame))
13157 /* Select the frame, for the sake of frame-local
13158 variables. */
13159 select_frame_for_redisplay (frame);
13160
13161 /* Mark all the scroll bars to be removed; we'll redeem
13162 the ones we want when we redisplay their windows. */
13163 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13164 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13165
13166 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13167 redisplay_windows (FRAME_ROOT_WINDOW (f));
13168
13169 /* The X error handler may have deleted that frame. */
13170 if (!FRAME_LIVE_P (f))
13171 continue;
13172
13173 /* Any scroll bars which redisplay_windows should have
13174 nuked should now go away. */
13175 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13176 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13177
13178 /* If fonts changed, display again. */
13179 /* ??? rms: I suspect it is a mistake to jump all the way
13180 back to retry here. It should just retry this frame. */
13181 if (fonts_changed_p)
13182 goto retry;
13183
13184 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13185 {
13186 /* See if we have to hscroll. */
13187 if (!f->already_hscrolled_p)
13188 {
13189 f->already_hscrolled_p = 1;
13190 if (hscroll_windows (f->root_window))
13191 goto retry;
13192 }
13193
13194 /* Prevent various kinds of signals during display
13195 update. stdio is not robust about handling
13196 signals, which can cause an apparent I/O
13197 error. */
13198 if (interrupt_input)
13199 unrequest_sigio ();
13200 STOP_POLLING;
13201
13202 /* Update the display. */
13203 set_window_update_flags (XWINDOW (f->root_window), 1);
13204 pending |= update_frame (f, 0, 0);
13205 f->updated_p = 1;
13206 }
13207 }
13208 }
13209
13210 if (!EQ (old_frame, selected_frame)
13211 && FRAME_LIVE_P (XFRAME (old_frame)))
13212 /* We played a bit fast-and-loose above and allowed selected_frame
13213 and selected_window to be temporarily out-of-sync but let's make
13214 sure this stays contained. */
13215 select_frame_for_redisplay (old_frame);
13216 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13217
13218 if (!pending)
13219 {
13220 /* Do the mark_window_display_accurate after all windows have
13221 been redisplayed because this call resets flags in buffers
13222 which are needed for proper redisplay. */
13223 FOR_EACH_FRAME (tail, frame)
13224 {
13225 struct frame *f = XFRAME (frame);
13226 if (f->updated_p)
13227 {
13228 mark_window_display_accurate (f->root_window, 1);
13229 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13230 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13231 }
13232 }
13233 }
13234 }
13235 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13236 {
13237 Lisp_Object mini_window;
13238 struct frame *mini_frame;
13239
13240 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13241 /* Use list_of_error, not Qerror, so that
13242 we catch only errors and don't run the debugger. */
13243 internal_condition_case_1 (redisplay_window_1, selected_window,
13244 list_of_error,
13245 redisplay_window_error);
13246
13247 /* Compare desired and current matrices, perform output. */
13248
13249 update:
13250 /* If fonts changed, display again. */
13251 if (fonts_changed_p)
13252 goto retry;
13253
13254 /* Prevent various kinds of signals during display update.
13255 stdio is not robust about handling signals,
13256 which can cause an apparent I/O error. */
13257 if (interrupt_input)
13258 unrequest_sigio ();
13259 STOP_POLLING;
13260
13261 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13262 {
13263 if (hscroll_windows (selected_window))
13264 goto retry;
13265
13266 XWINDOW (selected_window)->must_be_updated_p = 1;
13267 pending = update_frame (sf, 0, 0);
13268 }
13269
13270 /* We may have called echo_area_display at the top of this
13271 function. If the echo area is on another frame, that may
13272 have put text on a frame other than the selected one, so the
13273 above call to update_frame would not have caught it. Catch
13274 it here. */
13275 mini_window = FRAME_MINIBUF_WINDOW (sf);
13276 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13277
13278 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13279 {
13280 XWINDOW (mini_window)->must_be_updated_p = 1;
13281 pending |= update_frame (mini_frame, 0, 0);
13282 if (!pending && hscroll_windows (mini_window))
13283 goto retry;
13284 }
13285 }
13286
13287 /* If display was paused because of pending input, make sure we do a
13288 thorough update the next time. */
13289 if (pending)
13290 {
13291 /* Prevent the optimization at the beginning of
13292 redisplay_internal that tries a single-line update of the
13293 line containing the cursor in the selected window. */
13294 CHARPOS (this_line_start_pos) = 0;
13295
13296 /* Let the overlay arrow be updated the next time. */
13297 update_overlay_arrows (0);
13298
13299 /* If we pause after scrolling, some rows in the current
13300 matrices of some windows are not valid. */
13301 if (!WINDOW_FULL_WIDTH_P (w)
13302 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13303 update_mode_lines = 1;
13304 }
13305 else
13306 {
13307 if (!consider_all_windows_p)
13308 {
13309 /* This has already been done above if
13310 consider_all_windows_p is set. */
13311 mark_window_display_accurate_1 (w, 1);
13312
13313 /* Say overlay arrows are up to date. */
13314 update_overlay_arrows (1);
13315
13316 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13317 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13318 }
13319
13320 update_mode_lines = 0;
13321 windows_or_buffers_changed = 0;
13322 cursor_type_changed = 0;
13323 }
13324
13325 /* Start SIGIO interrupts coming again. Having them off during the
13326 code above makes it less likely one will discard output, but not
13327 impossible, since there might be stuff in the system buffer here.
13328 But it is much hairier to try to do anything about that. */
13329 if (interrupt_input)
13330 request_sigio ();
13331 RESUME_POLLING;
13332
13333 /* If a frame has become visible which was not before, redisplay
13334 again, so that we display it. Expose events for such a frame
13335 (which it gets when becoming visible) don't call the parts of
13336 redisplay constructing glyphs, so simply exposing a frame won't
13337 display anything in this case. So, we have to display these
13338 frames here explicitly. */
13339 if (!pending)
13340 {
13341 Lisp_Object tail, frame;
13342 int new_count = 0;
13343
13344 FOR_EACH_FRAME (tail, frame)
13345 {
13346 int this_is_visible = 0;
13347
13348 if (XFRAME (frame)->visible)
13349 this_is_visible = 1;
13350 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13351 if (XFRAME (frame)->visible)
13352 this_is_visible = 1;
13353
13354 if (this_is_visible)
13355 new_count++;
13356 }
13357
13358 if (new_count != number_of_visible_frames)
13359 windows_or_buffers_changed++;
13360 }
13361
13362 /* Change frame size now if a change is pending. */
13363 do_pending_window_change (1);
13364
13365 /* If we just did a pending size change, or have additional
13366 visible frames, or selected_window changed, redisplay again. */
13367 if ((windows_or_buffers_changed && !pending)
13368 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13369 goto retry;
13370
13371 /* Clear the face and image caches.
13372
13373 We used to do this only if consider_all_windows_p. But the cache
13374 needs to be cleared if a timer creates images in the current
13375 buffer (e.g. the test case in Bug#6230). */
13376
13377 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13378 {
13379 clear_face_cache (0);
13380 clear_face_cache_count = 0;
13381 }
13382
13383 #ifdef HAVE_WINDOW_SYSTEM
13384 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13385 {
13386 clear_image_caches (Qnil);
13387 clear_image_cache_count = 0;
13388 }
13389 #endif /* HAVE_WINDOW_SYSTEM */
13390
13391 end_of_redisplay:
13392 unbind_to (count, Qnil);
13393 RESUME_POLLING;
13394 }
13395
13396
13397 /* Redisplay, but leave alone any recent echo area message unless
13398 another message has been requested in its place.
13399
13400 This is useful in situations where you need to redisplay but no
13401 user action has occurred, making it inappropriate for the message
13402 area to be cleared. See tracking_off and
13403 wait_reading_process_output for examples of these situations.
13404
13405 FROM_WHERE is an integer saying from where this function was
13406 called. This is useful for debugging. */
13407
13408 void
13409 redisplay_preserve_echo_area (int from_where)
13410 {
13411 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13412
13413 if (!NILP (echo_area_buffer[1]))
13414 {
13415 /* We have a previously displayed message, but no current
13416 message. Redisplay the previous message. */
13417 display_last_displayed_message_p = 1;
13418 redisplay_internal ();
13419 display_last_displayed_message_p = 0;
13420 }
13421 else
13422 redisplay_internal ();
13423
13424 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13425 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13426 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13427 }
13428
13429
13430 /* Function registered with record_unwind_protect in
13431 redisplay_internal. Reset redisplaying_p to the value it had
13432 before redisplay_internal was called, and clear
13433 prevent_freeing_realized_faces_p. It also selects the previously
13434 selected frame, unless it has been deleted (by an X connection
13435 failure during redisplay, for example). */
13436
13437 static Lisp_Object
13438 unwind_redisplay (Lisp_Object val)
13439 {
13440 Lisp_Object old_redisplaying_p, old_frame;
13441
13442 old_redisplaying_p = XCAR (val);
13443 redisplaying_p = XFASTINT (old_redisplaying_p);
13444 old_frame = XCDR (val);
13445 if (! EQ (old_frame, selected_frame)
13446 && FRAME_LIVE_P (XFRAME (old_frame)))
13447 select_frame_for_redisplay (old_frame);
13448 return Qnil;
13449 }
13450
13451
13452 /* Mark the display of window W as accurate or inaccurate. If
13453 ACCURATE_P is non-zero mark display of W as accurate. If
13454 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13455 redisplay_internal is called. */
13456
13457 static void
13458 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13459 {
13460 if (BUFFERP (w->buffer))
13461 {
13462 struct buffer *b = XBUFFER (w->buffer);
13463
13464 w->last_modified
13465 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13466 w->last_overlay_modified
13467 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13468 w->last_had_star
13469 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13470
13471 if (accurate_p)
13472 {
13473 b->clip_changed = 0;
13474 b->prevent_redisplay_optimizations_p = 0;
13475
13476 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13477 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13478 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13479 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13480
13481 w->current_matrix->buffer = b;
13482 w->current_matrix->begv = BUF_BEGV (b);
13483 w->current_matrix->zv = BUF_ZV (b);
13484
13485 w->last_cursor = w->cursor;
13486 w->last_cursor_off_p = w->cursor_off_p;
13487
13488 if (w == XWINDOW (selected_window))
13489 w->last_point = make_number (BUF_PT (b));
13490 else
13491 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13492 }
13493 }
13494
13495 if (accurate_p)
13496 {
13497 w->window_end_valid = w->buffer;
13498 w->update_mode_line = Qnil;
13499 }
13500 }
13501
13502
13503 /* Mark the display of windows in the window tree rooted at WINDOW as
13504 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13505 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13506 be redisplayed the next time redisplay_internal is called. */
13507
13508 void
13509 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13510 {
13511 struct window *w;
13512
13513 for (; !NILP (window); window = w->next)
13514 {
13515 w = XWINDOW (window);
13516 mark_window_display_accurate_1 (w, accurate_p);
13517
13518 if (!NILP (w->vchild))
13519 mark_window_display_accurate (w->vchild, accurate_p);
13520 if (!NILP (w->hchild))
13521 mark_window_display_accurate (w->hchild, accurate_p);
13522 }
13523
13524 if (accurate_p)
13525 {
13526 update_overlay_arrows (1);
13527 }
13528 else
13529 {
13530 /* Force a thorough redisplay the next time by setting
13531 last_arrow_position and last_arrow_string to t, which is
13532 unequal to any useful value of Voverlay_arrow_... */
13533 update_overlay_arrows (-1);
13534 }
13535 }
13536
13537
13538 /* Return value in display table DP (Lisp_Char_Table *) for character
13539 C. Since a display table doesn't have any parent, we don't have to
13540 follow parent. Do not call this function directly but use the
13541 macro DISP_CHAR_VECTOR. */
13542
13543 Lisp_Object
13544 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13545 {
13546 Lisp_Object val;
13547
13548 if (ASCII_CHAR_P (c))
13549 {
13550 val = dp->ascii;
13551 if (SUB_CHAR_TABLE_P (val))
13552 val = XSUB_CHAR_TABLE (val)->contents[c];
13553 }
13554 else
13555 {
13556 Lisp_Object table;
13557
13558 XSETCHAR_TABLE (table, dp);
13559 val = char_table_ref (table, c);
13560 }
13561 if (NILP (val))
13562 val = dp->defalt;
13563 return val;
13564 }
13565
13566
13567 \f
13568 /***********************************************************************
13569 Window Redisplay
13570 ***********************************************************************/
13571
13572 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13573
13574 static void
13575 redisplay_windows (Lisp_Object window)
13576 {
13577 while (!NILP (window))
13578 {
13579 struct window *w = XWINDOW (window);
13580
13581 if (!NILP (w->hchild))
13582 redisplay_windows (w->hchild);
13583 else if (!NILP (w->vchild))
13584 redisplay_windows (w->vchild);
13585 else if (!NILP (w->buffer))
13586 {
13587 displayed_buffer = XBUFFER (w->buffer);
13588 /* Use list_of_error, not Qerror, so that
13589 we catch only errors and don't run the debugger. */
13590 internal_condition_case_1 (redisplay_window_0, window,
13591 list_of_error,
13592 redisplay_window_error);
13593 }
13594
13595 window = w->next;
13596 }
13597 }
13598
13599 static Lisp_Object
13600 redisplay_window_error (Lisp_Object ignore)
13601 {
13602 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13603 return Qnil;
13604 }
13605
13606 static Lisp_Object
13607 redisplay_window_0 (Lisp_Object window)
13608 {
13609 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13610 redisplay_window (window, 0);
13611 return Qnil;
13612 }
13613
13614 static Lisp_Object
13615 redisplay_window_1 (Lisp_Object window)
13616 {
13617 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13618 redisplay_window (window, 1);
13619 return Qnil;
13620 }
13621 \f
13622
13623 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13624 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13625 which positions recorded in ROW differ from current buffer
13626 positions.
13627
13628 Return 0 if cursor is not on this row, 1 otherwise. */
13629
13630 static int
13631 set_cursor_from_row (struct window *w, struct glyph_row *row,
13632 struct glyph_matrix *matrix,
13633 ptrdiff_t delta, ptrdiff_t delta_bytes,
13634 int dy, int dvpos)
13635 {
13636 struct glyph *glyph = row->glyphs[TEXT_AREA];
13637 struct glyph *end = glyph + row->used[TEXT_AREA];
13638 struct glyph *cursor = NULL;
13639 /* The last known character position in row. */
13640 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13641 int x = row->x;
13642 ptrdiff_t pt_old = PT - delta;
13643 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13644 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13645 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13646 /* A glyph beyond the edge of TEXT_AREA which we should never
13647 touch. */
13648 struct glyph *glyphs_end = end;
13649 /* Non-zero means we've found a match for cursor position, but that
13650 glyph has the avoid_cursor_p flag set. */
13651 int match_with_avoid_cursor = 0;
13652 /* Non-zero means we've seen at least one glyph that came from a
13653 display string. */
13654 int string_seen = 0;
13655 /* Largest and smallest buffer positions seen so far during scan of
13656 glyph row. */
13657 ptrdiff_t bpos_max = pos_before;
13658 ptrdiff_t bpos_min = pos_after;
13659 /* Last buffer position covered by an overlay string with an integer
13660 `cursor' property. */
13661 ptrdiff_t bpos_covered = 0;
13662 /* Non-zero means the display string on which to display the cursor
13663 comes from a text property, not from an overlay. */
13664 int string_from_text_prop = 0;
13665
13666 /* Skip over glyphs not having an object at the start and the end of
13667 the row. These are special glyphs like truncation marks on
13668 terminal frames. */
13669 if (row->displays_text_p)
13670 {
13671 if (!row->reversed_p)
13672 {
13673 while (glyph < end
13674 && INTEGERP (glyph->object)
13675 && glyph->charpos < 0)
13676 {
13677 x += glyph->pixel_width;
13678 ++glyph;
13679 }
13680 while (end > glyph
13681 && INTEGERP ((end - 1)->object)
13682 /* CHARPOS is zero for blanks and stretch glyphs
13683 inserted by extend_face_to_end_of_line. */
13684 && (end - 1)->charpos <= 0)
13685 --end;
13686 glyph_before = glyph - 1;
13687 glyph_after = end;
13688 }
13689 else
13690 {
13691 struct glyph *g;
13692
13693 /* If the glyph row is reversed, we need to process it from back
13694 to front, so swap the edge pointers. */
13695 glyphs_end = end = glyph - 1;
13696 glyph += row->used[TEXT_AREA] - 1;
13697
13698 while (glyph > end + 1
13699 && INTEGERP (glyph->object)
13700 && glyph->charpos < 0)
13701 {
13702 --glyph;
13703 x -= glyph->pixel_width;
13704 }
13705 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13706 --glyph;
13707 /* By default, in reversed rows we put the cursor on the
13708 rightmost (first in the reading order) glyph. */
13709 for (g = end + 1; g < glyph; g++)
13710 x += g->pixel_width;
13711 while (end < glyph
13712 && INTEGERP ((end + 1)->object)
13713 && (end + 1)->charpos <= 0)
13714 ++end;
13715 glyph_before = glyph + 1;
13716 glyph_after = end;
13717 }
13718 }
13719 else if (row->reversed_p)
13720 {
13721 /* In R2L rows that don't display text, put the cursor on the
13722 rightmost glyph. Case in point: an empty last line that is
13723 part of an R2L paragraph. */
13724 cursor = end - 1;
13725 /* Avoid placing the cursor on the last glyph of the row, where
13726 on terminal frames we hold the vertical border between
13727 adjacent windows. */
13728 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13729 && !WINDOW_RIGHTMOST_P (w)
13730 && cursor == row->glyphs[LAST_AREA] - 1)
13731 cursor--;
13732 x = -1; /* will be computed below, at label compute_x */
13733 }
13734
13735 /* Step 1: Try to find the glyph whose character position
13736 corresponds to point. If that's not possible, find 2 glyphs
13737 whose character positions are the closest to point, one before
13738 point, the other after it. */
13739 if (!row->reversed_p)
13740 while (/* not marched to end of glyph row */
13741 glyph < end
13742 /* glyph was not inserted by redisplay for internal purposes */
13743 && !INTEGERP (glyph->object))
13744 {
13745 if (BUFFERP (glyph->object))
13746 {
13747 ptrdiff_t dpos = glyph->charpos - pt_old;
13748
13749 if (glyph->charpos > bpos_max)
13750 bpos_max = glyph->charpos;
13751 if (glyph->charpos < bpos_min)
13752 bpos_min = glyph->charpos;
13753 if (!glyph->avoid_cursor_p)
13754 {
13755 /* If we hit point, we've found the glyph on which to
13756 display the cursor. */
13757 if (dpos == 0)
13758 {
13759 match_with_avoid_cursor = 0;
13760 break;
13761 }
13762 /* See if we've found a better approximation to
13763 POS_BEFORE or to POS_AFTER. Note that we want the
13764 first (leftmost) glyph of all those that are the
13765 closest from below, and the last (rightmost) of all
13766 those from above. */
13767 if (0 > dpos && dpos > pos_before - pt_old)
13768 {
13769 pos_before = glyph->charpos;
13770 glyph_before = glyph;
13771 }
13772 else if (0 < dpos && dpos <= pos_after - pt_old)
13773 {
13774 pos_after = glyph->charpos;
13775 glyph_after = glyph;
13776 }
13777 }
13778 else if (dpos == 0)
13779 match_with_avoid_cursor = 1;
13780 }
13781 else if (STRINGP (glyph->object))
13782 {
13783 Lisp_Object chprop;
13784 ptrdiff_t glyph_pos = glyph->charpos;
13785
13786 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13787 glyph->object);
13788 if (INTEGERP (chprop))
13789 {
13790 bpos_covered = bpos_max + XINT (chprop);
13791 /* If the `cursor' property covers buffer positions up
13792 to and including point, we should display cursor on
13793 this glyph. Note that overlays and text properties
13794 with string values stop bidi reordering, so every
13795 buffer position to the left of the string is always
13796 smaller than any position to the right of the
13797 string. Therefore, if a `cursor' property on one
13798 of the string's characters has an integer value, we
13799 will break out of the loop below _before_ we get to
13800 the position match above. IOW, integer values of
13801 the `cursor' property override the "exact match for
13802 point" strategy of positioning the cursor. */
13803 /* Implementation note: bpos_max == pt_old when, e.g.,
13804 we are in an empty line, where bpos_max is set to
13805 MATRIX_ROW_START_CHARPOS, see above. */
13806 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13807 {
13808 cursor = glyph;
13809 break;
13810 }
13811 }
13812
13813 string_seen = 1;
13814 }
13815 x += glyph->pixel_width;
13816 ++glyph;
13817 }
13818 else if (glyph > end) /* row is reversed */
13819 while (!INTEGERP (glyph->object))
13820 {
13821 if (BUFFERP (glyph->object))
13822 {
13823 ptrdiff_t dpos = glyph->charpos - pt_old;
13824
13825 if (glyph->charpos > bpos_max)
13826 bpos_max = glyph->charpos;
13827 if (glyph->charpos < bpos_min)
13828 bpos_min = glyph->charpos;
13829 if (!glyph->avoid_cursor_p)
13830 {
13831 if (dpos == 0)
13832 {
13833 match_with_avoid_cursor = 0;
13834 break;
13835 }
13836 if (0 > dpos && dpos > pos_before - pt_old)
13837 {
13838 pos_before = glyph->charpos;
13839 glyph_before = glyph;
13840 }
13841 else if (0 < dpos && dpos <= pos_after - pt_old)
13842 {
13843 pos_after = glyph->charpos;
13844 glyph_after = glyph;
13845 }
13846 }
13847 else if (dpos == 0)
13848 match_with_avoid_cursor = 1;
13849 }
13850 else if (STRINGP (glyph->object))
13851 {
13852 Lisp_Object chprop;
13853 ptrdiff_t glyph_pos = glyph->charpos;
13854
13855 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13856 glyph->object);
13857 if (INTEGERP (chprop))
13858 {
13859 bpos_covered = bpos_max + XINT (chprop);
13860 /* If the `cursor' property covers buffer positions up
13861 to and including point, we should display cursor on
13862 this glyph. */
13863 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13864 {
13865 cursor = glyph;
13866 break;
13867 }
13868 }
13869 string_seen = 1;
13870 }
13871 --glyph;
13872 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13873 {
13874 x--; /* can't use any pixel_width */
13875 break;
13876 }
13877 x -= glyph->pixel_width;
13878 }
13879
13880 /* Step 2: If we didn't find an exact match for point, we need to
13881 look for a proper place to put the cursor among glyphs between
13882 GLYPH_BEFORE and GLYPH_AFTER. */
13883 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13884 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13885 && bpos_covered < pt_old)
13886 {
13887 /* An empty line has a single glyph whose OBJECT is zero and
13888 whose CHARPOS is the position of a newline on that line.
13889 Note that on a TTY, there are more glyphs after that, which
13890 were produced by extend_face_to_end_of_line, but their
13891 CHARPOS is zero or negative. */
13892 int empty_line_p =
13893 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13894 && INTEGERP (glyph->object) && glyph->charpos > 0;
13895
13896 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13897 {
13898 ptrdiff_t ellipsis_pos;
13899
13900 /* Scan back over the ellipsis glyphs. */
13901 if (!row->reversed_p)
13902 {
13903 ellipsis_pos = (glyph - 1)->charpos;
13904 while (glyph > row->glyphs[TEXT_AREA]
13905 && (glyph - 1)->charpos == ellipsis_pos)
13906 glyph--, x -= glyph->pixel_width;
13907 /* That loop always goes one position too far, including
13908 the glyph before the ellipsis. So scan forward over
13909 that one. */
13910 x += glyph->pixel_width;
13911 glyph++;
13912 }
13913 else /* row is reversed */
13914 {
13915 ellipsis_pos = (glyph + 1)->charpos;
13916 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13917 && (glyph + 1)->charpos == ellipsis_pos)
13918 glyph++, x += glyph->pixel_width;
13919 x -= glyph->pixel_width;
13920 glyph--;
13921 }
13922 }
13923 else if (match_with_avoid_cursor)
13924 {
13925 cursor = glyph_after;
13926 x = -1;
13927 }
13928 else if (string_seen)
13929 {
13930 int incr = row->reversed_p ? -1 : +1;
13931
13932 /* Need to find the glyph that came out of a string which is
13933 present at point. That glyph is somewhere between
13934 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13935 positioned between POS_BEFORE and POS_AFTER in the
13936 buffer. */
13937 struct glyph *start, *stop;
13938 ptrdiff_t pos = pos_before;
13939
13940 x = -1;
13941
13942 /* If the row ends in a newline from a display string,
13943 reordering could have moved the glyphs belonging to the
13944 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
13945 in this case we extend the search to the last glyph in
13946 the row that was not inserted by redisplay. */
13947 if (row->ends_in_newline_from_string_p)
13948 {
13949 glyph_after = end;
13950 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13951 }
13952
13953 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13954 correspond to POS_BEFORE and POS_AFTER, respectively. We
13955 need START and STOP in the order that corresponds to the
13956 row's direction as given by its reversed_p flag. If the
13957 directionality of characters between POS_BEFORE and
13958 POS_AFTER is the opposite of the row's base direction,
13959 these characters will have been reordered for display,
13960 and we need to reverse START and STOP. */
13961 if (!row->reversed_p)
13962 {
13963 start = min (glyph_before, glyph_after);
13964 stop = max (glyph_before, glyph_after);
13965 }
13966 else
13967 {
13968 start = max (glyph_before, glyph_after);
13969 stop = min (glyph_before, glyph_after);
13970 }
13971 for (glyph = start + incr;
13972 row->reversed_p ? glyph > stop : glyph < stop; )
13973 {
13974
13975 /* Any glyphs that come from the buffer are here because
13976 of bidi reordering. Skip them, and only pay
13977 attention to glyphs that came from some string. */
13978 if (STRINGP (glyph->object))
13979 {
13980 Lisp_Object str;
13981 ptrdiff_t tem;
13982 /* If the display property covers the newline, we
13983 need to search for it one position farther. */
13984 ptrdiff_t lim = pos_after
13985 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13986
13987 string_from_text_prop = 0;
13988 str = glyph->object;
13989 tem = string_buffer_position_lim (str, pos, lim, 0);
13990 if (tem == 0 /* from overlay */
13991 || pos <= tem)
13992 {
13993 /* If the string from which this glyph came is
13994 found in the buffer at point, then we've
13995 found the glyph we've been looking for. If
13996 it comes from an overlay (tem == 0), and it
13997 has the `cursor' property on one of its
13998 glyphs, record that glyph as a candidate for
13999 displaying the cursor. (As in the
14000 unidirectional version, we will display the
14001 cursor on the last candidate we find.) */
14002 if (tem == 0 || tem == pt_old)
14003 {
14004 /* The glyphs from this string could have
14005 been reordered. Find the one with the
14006 smallest string position. Or there could
14007 be a character in the string with the
14008 `cursor' property, which means display
14009 cursor on that character's glyph. */
14010 ptrdiff_t strpos = glyph->charpos;
14011
14012 if (tem)
14013 {
14014 cursor = glyph;
14015 string_from_text_prop = 1;
14016 }
14017 for ( ;
14018 (row->reversed_p ? glyph > stop : glyph < stop)
14019 && EQ (glyph->object, str);
14020 glyph += incr)
14021 {
14022 Lisp_Object cprop;
14023 ptrdiff_t gpos = glyph->charpos;
14024
14025 cprop = Fget_char_property (make_number (gpos),
14026 Qcursor,
14027 glyph->object);
14028 if (!NILP (cprop))
14029 {
14030 cursor = glyph;
14031 break;
14032 }
14033 if (tem && glyph->charpos < strpos)
14034 {
14035 strpos = glyph->charpos;
14036 cursor = glyph;
14037 }
14038 }
14039
14040 if (tem == pt_old)
14041 goto compute_x;
14042 }
14043 if (tem)
14044 pos = tem + 1; /* don't find previous instances */
14045 }
14046 /* This string is not what we want; skip all of the
14047 glyphs that came from it. */
14048 while ((row->reversed_p ? glyph > stop : glyph < stop)
14049 && EQ (glyph->object, str))
14050 glyph += incr;
14051 }
14052 else
14053 glyph += incr;
14054 }
14055
14056 /* If we reached the end of the line, and END was from a string,
14057 the cursor is not on this line. */
14058 if (cursor == NULL
14059 && (row->reversed_p ? glyph <= end : glyph >= end)
14060 && STRINGP (end->object)
14061 && row->continued_p)
14062 return 0;
14063 }
14064 /* A truncated row may not include PT among its character positions.
14065 Setting the cursor inside the scroll margin will trigger
14066 recalculation of hscroll in hscroll_window_tree. But if a
14067 display string covers point, defer to the string-handling
14068 code below to figure this out. */
14069 else if (row->truncated_on_left_p && pt_old < bpos_min)
14070 {
14071 cursor = glyph_before;
14072 x = -1;
14073 }
14074 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14075 /* Zero-width characters produce no glyphs. */
14076 || (!empty_line_p
14077 && (row->reversed_p
14078 ? glyph_after > glyphs_end
14079 : glyph_after < glyphs_end)))
14080 {
14081 cursor = glyph_after;
14082 x = -1;
14083 }
14084 }
14085
14086 compute_x:
14087 if (cursor != NULL)
14088 glyph = cursor;
14089 if (x < 0)
14090 {
14091 struct glyph *g;
14092
14093 /* Need to compute x that corresponds to GLYPH. */
14094 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14095 {
14096 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14097 abort ();
14098 x += g->pixel_width;
14099 }
14100 }
14101
14102 /* ROW could be part of a continued line, which, under bidi
14103 reordering, might have other rows whose start and end charpos
14104 occlude point. Only set w->cursor if we found a better
14105 approximation to the cursor position than we have from previously
14106 examined candidate rows belonging to the same continued line. */
14107 if (/* we already have a candidate row */
14108 w->cursor.vpos >= 0
14109 /* that candidate is not the row we are processing */
14110 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14111 /* Make sure cursor.vpos specifies a row whose start and end
14112 charpos occlude point, and it is valid candidate for being a
14113 cursor-row. This is because some callers of this function
14114 leave cursor.vpos at the row where the cursor was displayed
14115 during the last redisplay cycle. */
14116 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14117 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14118 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14119 {
14120 struct glyph *g1 =
14121 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14122
14123 /* Don't consider glyphs that are outside TEXT_AREA. */
14124 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14125 return 0;
14126 /* Keep the candidate whose buffer position is the closest to
14127 point or has the `cursor' property. */
14128 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14129 w->cursor.hpos >= 0
14130 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14131 && ((BUFFERP (g1->object)
14132 && (g1->charpos == pt_old /* an exact match always wins */
14133 || (BUFFERP (glyph->object)
14134 && eabs (g1->charpos - pt_old)
14135 < eabs (glyph->charpos - pt_old))))
14136 /* previous candidate is a glyph from a string that has
14137 a non-nil `cursor' property */
14138 || (STRINGP (g1->object)
14139 && (!NILP (Fget_char_property (make_number (g1->charpos),
14140 Qcursor, g1->object))
14141 /* previous candidate is from the same display
14142 string as this one, and the display string
14143 came from a text property */
14144 || (EQ (g1->object, glyph->object)
14145 && string_from_text_prop)
14146 /* this candidate is from newline and its
14147 position is not an exact match */
14148 || (INTEGERP (glyph->object)
14149 && glyph->charpos != pt_old)))))
14150 return 0;
14151 /* If this candidate gives an exact match, use that. */
14152 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14153 /* If this candidate is a glyph created for the
14154 terminating newline of a line, and point is on that
14155 newline, it wins because it's an exact match. */
14156 || (!row->continued_p
14157 && INTEGERP (glyph->object)
14158 && glyph->charpos == 0
14159 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14160 /* Otherwise, keep the candidate that comes from a row
14161 spanning less buffer positions. This may win when one or
14162 both candidate positions are on glyphs that came from
14163 display strings, for which we cannot compare buffer
14164 positions. */
14165 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14166 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14167 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14168 return 0;
14169 }
14170 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14171 w->cursor.x = x;
14172 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14173 w->cursor.y = row->y + dy;
14174
14175 if (w == XWINDOW (selected_window))
14176 {
14177 if (!row->continued_p
14178 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14179 && row->x == 0)
14180 {
14181 this_line_buffer = XBUFFER (w->buffer);
14182
14183 CHARPOS (this_line_start_pos)
14184 = MATRIX_ROW_START_CHARPOS (row) + delta;
14185 BYTEPOS (this_line_start_pos)
14186 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14187
14188 CHARPOS (this_line_end_pos)
14189 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14190 BYTEPOS (this_line_end_pos)
14191 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14192
14193 this_line_y = w->cursor.y;
14194 this_line_pixel_height = row->height;
14195 this_line_vpos = w->cursor.vpos;
14196 this_line_start_x = row->x;
14197 }
14198 else
14199 CHARPOS (this_line_start_pos) = 0;
14200 }
14201
14202 return 1;
14203 }
14204
14205
14206 /* Run window scroll functions, if any, for WINDOW with new window
14207 start STARTP. Sets the window start of WINDOW to that position.
14208
14209 We assume that the window's buffer is really current. */
14210
14211 static inline struct text_pos
14212 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14213 {
14214 struct window *w = XWINDOW (window);
14215 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14216
14217 if (current_buffer != XBUFFER (w->buffer))
14218 abort ();
14219
14220 if (!NILP (Vwindow_scroll_functions))
14221 {
14222 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14223 make_number (CHARPOS (startp)));
14224 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14225 /* In case the hook functions switch buffers. */
14226 if (current_buffer != XBUFFER (w->buffer))
14227 set_buffer_internal_1 (XBUFFER (w->buffer));
14228 }
14229
14230 return startp;
14231 }
14232
14233
14234 /* Make sure the line containing the cursor is fully visible.
14235 A value of 1 means there is nothing to be done.
14236 (Either the line is fully visible, or it cannot be made so,
14237 or we cannot tell.)
14238
14239 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14240 is higher than window.
14241
14242 A value of 0 means the caller should do scrolling
14243 as if point had gone off the screen. */
14244
14245 static int
14246 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14247 {
14248 struct glyph_matrix *matrix;
14249 struct glyph_row *row;
14250 int window_height;
14251
14252 if (!make_cursor_line_fully_visible_p)
14253 return 1;
14254
14255 /* It's not always possible to find the cursor, e.g, when a window
14256 is full of overlay strings. Don't do anything in that case. */
14257 if (w->cursor.vpos < 0)
14258 return 1;
14259
14260 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14261 row = MATRIX_ROW (matrix, w->cursor.vpos);
14262
14263 /* If the cursor row is not partially visible, there's nothing to do. */
14264 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14265 return 1;
14266
14267 /* If the row the cursor is in is taller than the window's height,
14268 it's not clear what to do, so do nothing. */
14269 window_height = window_box_height (w);
14270 if (row->height >= window_height)
14271 {
14272 if (!force_p || MINI_WINDOW_P (w)
14273 || w->vscroll || w->cursor.vpos == 0)
14274 return 1;
14275 }
14276 return 0;
14277 }
14278
14279
14280 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14281 non-zero means only WINDOW is redisplayed in redisplay_internal.
14282 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14283 in redisplay_window to bring a partially visible line into view in
14284 the case that only the cursor has moved.
14285
14286 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14287 last screen line's vertical height extends past the end of the screen.
14288
14289 Value is
14290
14291 1 if scrolling succeeded
14292
14293 0 if scrolling didn't find point.
14294
14295 -1 if new fonts have been loaded so that we must interrupt
14296 redisplay, adjust glyph matrices, and try again. */
14297
14298 enum
14299 {
14300 SCROLLING_SUCCESS,
14301 SCROLLING_FAILED,
14302 SCROLLING_NEED_LARGER_MATRICES
14303 };
14304
14305 /* If scroll-conservatively is more than this, never recenter.
14306
14307 If you change this, don't forget to update the doc string of
14308 `scroll-conservatively' and the Emacs manual. */
14309 #define SCROLL_LIMIT 100
14310
14311 static int
14312 try_scrolling (Lisp_Object window, int just_this_one_p,
14313 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14314 int temp_scroll_step, int last_line_misfit)
14315 {
14316 struct window *w = XWINDOW (window);
14317 struct frame *f = XFRAME (w->frame);
14318 struct text_pos pos, startp;
14319 struct it it;
14320 int this_scroll_margin, scroll_max, rc, height;
14321 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14322 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14323 Lisp_Object aggressive;
14324 /* We will never try scrolling more than this number of lines. */
14325 int scroll_limit = SCROLL_LIMIT;
14326
14327 #if GLYPH_DEBUG
14328 debug_method_add (w, "try_scrolling");
14329 #endif
14330
14331 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14332
14333 /* Compute scroll margin height in pixels. We scroll when point is
14334 within this distance from the top or bottom of the window. */
14335 if (scroll_margin > 0)
14336 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14337 * FRAME_LINE_HEIGHT (f);
14338 else
14339 this_scroll_margin = 0;
14340
14341 /* Force arg_scroll_conservatively to have a reasonable value, to
14342 avoid scrolling too far away with slow move_it_* functions. Note
14343 that the user can supply scroll-conservatively equal to
14344 `most-positive-fixnum', which can be larger than INT_MAX. */
14345 if (arg_scroll_conservatively > scroll_limit)
14346 {
14347 arg_scroll_conservatively = scroll_limit + 1;
14348 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14349 }
14350 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14351 /* Compute how much we should try to scroll maximally to bring
14352 point into view. */
14353 scroll_max = (max (scroll_step,
14354 max (arg_scroll_conservatively, temp_scroll_step))
14355 * FRAME_LINE_HEIGHT (f));
14356 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14357 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14358 /* We're trying to scroll because of aggressive scrolling but no
14359 scroll_step is set. Choose an arbitrary one. */
14360 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14361 else
14362 scroll_max = 0;
14363
14364 too_near_end:
14365
14366 /* Decide whether to scroll down. */
14367 if (PT > CHARPOS (startp))
14368 {
14369 int scroll_margin_y;
14370
14371 /* Compute the pixel ypos of the scroll margin, then move IT to
14372 either that ypos or PT, whichever comes first. */
14373 start_display (&it, w, startp);
14374 scroll_margin_y = it.last_visible_y - this_scroll_margin
14375 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14376 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14377 (MOVE_TO_POS | MOVE_TO_Y));
14378
14379 if (PT > CHARPOS (it.current.pos))
14380 {
14381 int y0 = line_bottom_y (&it);
14382 /* Compute how many pixels below window bottom to stop searching
14383 for PT. This avoids costly search for PT that is far away if
14384 the user limited scrolling by a small number of lines, but
14385 always finds PT if scroll_conservatively is set to a large
14386 number, such as most-positive-fixnum. */
14387 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14388 int y_to_move = it.last_visible_y + slack;
14389
14390 /* Compute the distance from the scroll margin to PT or to
14391 the scroll limit, whichever comes first. This should
14392 include the height of the cursor line, to make that line
14393 fully visible. */
14394 move_it_to (&it, PT, -1, y_to_move,
14395 -1, MOVE_TO_POS | MOVE_TO_Y);
14396 dy = line_bottom_y (&it) - y0;
14397
14398 if (dy > scroll_max)
14399 return SCROLLING_FAILED;
14400
14401 if (dy > 0)
14402 scroll_down_p = 1;
14403 }
14404 }
14405
14406 if (scroll_down_p)
14407 {
14408 /* Point is in or below the bottom scroll margin, so move the
14409 window start down. If scrolling conservatively, move it just
14410 enough down to make point visible. If scroll_step is set,
14411 move it down by scroll_step. */
14412 if (arg_scroll_conservatively)
14413 amount_to_scroll
14414 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14415 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14416 else if (scroll_step || temp_scroll_step)
14417 amount_to_scroll = scroll_max;
14418 else
14419 {
14420 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14421 height = WINDOW_BOX_TEXT_HEIGHT (w);
14422 if (NUMBERP (aggressive))
14423 {
14424 double float_amount = XFLOATINT (aggressive) * height;
14425 amount_to_scroll = float_amount;
14426 if (amount_to_scroll == 0 && float_amount > 0)
14427 amount_to_scroll = 1;
14428 /* Don't let point enter the scroll margin near top of
14429 the window. */
14430 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14431 amount_to_scroll = height - 2*this_scroll_margin + dy;
14432 }
14433 }
14434
14435 if (amount_to_scroll <= 0)
14436 return SCROLLING_FAILED;
14437
14438 start_display (&it, w, startp);
14439 if (arg_scroll_conservatively <= scroll_limit)
14440 move_it_vertically (&it, amount_to_scroll);
14441 else
14442 {
14443 /* Extra precision for users who set scroll-conservatively
14444 to a large number: make sure the amount we scroll
14445 the window start is never less than amount_to_scroll,
14446 which was computed as distance from window bottom to
14447 point. This matters when lines at window top and lines
14448 below window bottom have different height. */
14449 struct it it1;
14450 void *it1data = NULL;
14451 /* We use a temporary it1 because line_bottom_y can modify
14452 its argument, if it moves one line down; see there. */
14453 int start_y;
14454
14455 SAVE_IT (it1, it, it1data);
14456 start_y = line_bottom_y (&it1);
14457 do {
14458 RESTORE_IT (&it, &it, it1data);
14459 move_it_by_lines (&it, 1);
14460 SAVE_IT (it1, it, it1data);
14461 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14462 }
14463
14464 /* If STARTP is unchanged, move it down another screen line. */
14465 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14466 move_it_by_lines (&it, 1);
14467 startp = it.current.pos;
14468 }
14469 else
14470 {
14471 struct text_pos scroll_margin_pos = startp;
14472
14473 /* See if point is inside the scroll margin at the top of the
14474 window. */
14475 if (this_scroll_margin)
14476 {
14477 start_display (&it, w, startp);
14478 move_it_vertically (&it, this_scroll_margin);
14479 scroll_margin_pos = it.current.pos;
14480 }
14481
14482 if (PT < CHARPOS (scroll_margin_pos))
14483 {
14484 /* Point is in the scroll margin at the top of the window or
14485 above what is displayed in the window. */
14486 int y0, y_to_move;
14487
14488 /* Compute the vertical distance from PT to the scroll
14489 margin position. Move as far as scroll_max allows, or
14490 one screenful, or 10 screen lines, whichever is largest.
14491 Give up if distance is greater than scroll_max. */
14492 SET_TEXT_POS (pos, PT, PT_BYTE);
14493 start_display (&it, w, pos);
14494 y0 = it.current_y;
14495 y_to_move = max (it.last_visible_y,
14496 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14497 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14498 y_to_move, -1,
14499 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14500 dy = it.current_y - y0;
14501 if (dy > scroll_max)
14502 return SCROLLING_FAILED;
14503
14504 /* Compute new window start. */
14505 start_display (&it, w, startp);
14506
14507 if (arg_scroll_conservatively)
14508 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14509 max (scroll_step, temp_scroll_step));
14510 else if (scroll_step || temp_scroll_step)
14511 amount_to_scroll = scroll_max;
14512 else
14513 {
14514 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14515 height = WINDOW_BOX_TEXT_HEIGHT (w);
14516 if (NUMBERP (aggressive))
14517 {
14518 double float_amount = XFLOATINT (aggressive) * height;
14519 amount_to_scroll = float_amount;
14520 if (amount_to_scroll == 0 && float_amount > 0)
14521 amount_to_scroll = 1;
14522 amount_to_scroll -=
14523 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14524 /* Don't let point enter the scroll margin near
14525 bottom of the window. */
14526 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14527 amount_to_scroll = height - 2*this_scroll_margin + dy;
14528 }
14529 }
14530
14531 if (amount_to_scroll <= 0)
14532 return SCROLLING_FAILED;
14533
14534 move_it_vertically_backward (&it, amount_to_scroll);
14535 startp = it.current.pos;
14536 }
14537 }
14538
14539 /* Run window scroll functions. */
14540 startp = run_window_scroll_functions (window, startp);
14541
14542 /* Display the window. Give up if new fonts are loaded, or if point
14543 doesn't appear. */
14544 if (!try_window (window, startp, 0))
14545 rc = SCROLLING_NEED_LARGER_MATRICES;
14546 else if (w->cursor.vpos < 0)
14547 {
14548 clear_glyph_matrix (w->desired_matrix);
14549 rc = SCROLLING_FAILED;
14550 }
14551 else
14552 {
14553 /* Maybe forget recorded base line for line number display. */
14554 if (!just_this_one_p
14555 || current_buffer->clip_changed
14556 || BEG_UNCHANGED < CHARPOS (startp))
14557 w->base_line_number = Qnil;
14558
14559 /* If cursor ends up on a partially visible line,
14560 treat that as being off the bottom of the screen. */
14561 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14562 /* It's possible that the cursor is on the first line of the
14563 buffer, which is partially obscured due to a vscroll
14564 (Bug#7537). In that case, avoid looping forever . */
14565 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14566 {
14567 clear_glyph_matrix (w->desired_matrix);
14568 ++extra_scroll_margin_lines;
14569 goto too_near_end;
14570 }
14571 rc = SCROLLING_SUCCESS;
14572 }
14573
14574 return rc;
14575 }
14576
14577
14578 /* Compute a suitable window start for window W if display of W starts
14579 on a continuation line. Value is non-zero if a new window start
14580 was computed.
14581
14582 The new window start will be computed, based on W's width, starting
14583 from the start of the continued line. It is the start of the
14584 screen line with the minimum distance from the old start W->start. */
14585
14586 static int
14587 compute_window_start_on_continuation_line (struct window *w)
14588 {
14589 struct text_pos pos, start_pos;
14590 int window_start_changed_p = 0;
14591
14592 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14593
14594 /* If window start is on a continuation line... Window start may be
14595 < BEGV in case there's invisible text at the start of the
14596 buffer (M-x rmail, for example). */
14597 if (CHARPOS (start_pos) > BEGV
14598 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14599 {
14600 struct it it;
14601 struct glyph_row *row;
14602
14603 /* Handle the case that the window start is out of range. */
14604 if (CHARPOS (start_pos) < BEGV)
14605 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14606 else if (CHARPOS (start_pos) > ZV)
14607 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14608
14609 /* Find the start of the continued line. This should be fast
14610 because scan_buffer is fast (newline cache). */
14611 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14612 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14613 row, DEFAULT_FACE_ID);
14614 reseat_at_previous_visible_line_start (&it);
14615
14616 /* If the line start is "too far" away from the window start,
14617 say it takes too much time to compute a new window start. */
14618 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14619 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14620 {
14621 int min_distance, distance;
14622
14623 /* Move forward by display lines to find the new window
14624 start. If window width was enlarged, the new start can
14625 be expected to be > the old start. If window width was
14626 decreased, the new window start will be < the old start.
14627 So, we're looking for the display line start with the
14628 minimum distance from the old window start. */
14629 pos = it.current.pos;
14630 min_distance = INFINITY;
14631 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14632 distance < min_distance)
14633 {
14634 min_distance = distance;
14635 pos = it.current.pos;
14636 move_it_by_lines (&it, 1);
14637 }
14638
14639 /* Set the window start there. */
14640 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14641 window_start_changed_p = 1;
14642 }
14643 }
14644
14645 return window_start_changed_p;
14646 }
14647
14648
14649 /* Try cursor movement in case text has not changed in window WINDOW,
14650 with window start STARTP. Value is
14651
14652 CURSOR_MOVEMENT_SUCCESS if successful
14653
14654 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14655
14656 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14657 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14658 we want to scroll as if scroll-step were set to 1. See the code.
14659
14660 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14661 which case we have to abort this redisplay, and adjust matrices
14662 first. */
14663
14664 enum
14665 {
14666 CURSOR_MOVEMENT_SUCCESS,
14667 CURSOR_MOVEMENT_CANNOT_BE_USED,
14668 CURSOR_MOVEMENT_MUST_SCROLL,
14669 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14670 };
14671
14672 static int
14673 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14674 {
14675 struct window *w = XWINDOW (window);
14676 struct frame *f = XFRAME (w->frame);
14677 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14678
14679 #if GLYPH_DEBUG
14680 if (inhibit_try_cursor_movement)
14681 return rc;
14682 #endif
14683
14684 /* Handle case where text has not changed, only point, and it has
14685 not moved off the frame. */
14686 if (/* Point may be in this window. */
14687 PT >= CHARPOS (startp)
14688 /* Selective display hasn't changed. */
14689 && !current_buffer->clip_changed
14690 /* Function force-mode-line-update is used to force a thorough
14691 redisplay. It sets either windows_or_buffers_changed or
14692 update_mode_lines. So don't take a shortcut here for these
14693 cases. */
14694 && !update_mode_lines
14695 && !windows_or_buffers_changed
14696 && !cursor_type_changed
14697 /* Can't use this case if highlighting a region. When a
14698 region exists, cursor movement has to do more than just
14699 set the cursor. */
14700 && !(!NILP (Vtransient_mark_mode)
14701 && !NILP (BVAR (current_buffer, mark_active)))
14702 && NILP (w->region_showing)
14703 && NILP (Vshow_trailing_whitespace)
14704 /* Right after splitting windows, last_point may be nil. */
14705 && INTEGERP (w->last_point)
14706 /* This code is not used for mini-buffer for the sake of the case
14707 of redisplaying to replace an echo area message; since in
14708 that case the mini-buffer contents per se are usually
14709 unchanged. This code is of no real use in the mini-buffer
14710 since the handling of this_line_start_pos, etc., in redisplay
14711 handles the same cases. */
14712 && !EQ (window, minibuf_window)
14713 /* When splitting windows or for new windows, it happens that
14714 redisplay is called with a nil window_end_vpos or one being
14715 larger than the window. This should really be fixed in
14716 window.c. I don't have this on my list, now, so we do
14717 approximately the same as the old redisplay code. --gerd. */
14718 && INTEGERP (w->window_end_vpos)
14719 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14720 && (FRAME_WINDOW_P (f)
14721 || !overlay_arrow_in_current_buffer_p ()))
14722 {
14723 int this_scroll_margin, top_scroll_margin;
14724 struct glyph_row *row = NULL;
14725
14726 #if GLYPH_DEBUG
14727 debug_method_add (w, "cursor movement");
14728 #endif
14729
14730 /* Scroll if point within this distance from the top or bottom
14731 of the window. This is a pixel value. */
14732 if (scroll_margin > 0)
14733 {
14734 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14735 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14736 }
14737 else
14738 this_scroll_margin = 0;
14739
14740 top_scroll_margin = this_scroll_margin;
14741 if (WINDOW_WANTS_HEADER_LINE_P (w))
14742 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14743
14744 /* Start with the row the cursor was displayed during the last
14745 not paused redisplay. Give up if that row is not valid. */
14746 if (w->last_cursor.vpos < 0
14747 || w->last_cursor.vpos >= w->current_matrix->nrows)
14748 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14749 else
14750 {
14751 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14752 if (row->mode_line_p)
14753 ++row;
14754 if (!row->enabled_p)
14755 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14756 }
14757
14758 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14759 {
14760 int scroll_p = 0, must_scroll = 0;
14761 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14762
14763 if (PT > XFASTINT (w->last_point))
14764 {
14765 /* Point has moved forward. */
14766 while (MATRIX_ROW_END_CHARPOS (row) < PT
14767 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14768 {
14769 xassert (row->enabled_p);
14770 ++row;
14771 }
14772
14773 /* If the end position of a row equals the start
14774 position of the next row, and PT is at that position,
14775 we would rather display cursor in the next line. */
14776 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14777 && MATRIX_ROW_END_CHARPOS (row) == PT
14778 && row < w->current_matrix->rows
14779 + w->current_matrix->nrows - 1
14780 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14781 && !cursor_row_p (row))
14782 ++row;
14783
14784 /* If within the scroll margin, scroll. Note that
14785 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14786 the next line would be drawn, and that
14787 this_scroll_margin can be zero. */
14788 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14789 || PT > MATRIX_ROW_END_CHARPOS (row)
14790 /* Line is completely visible last line in window
14791 and PT is to be set in the next line. */
14792 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14793 && PT == MATRIX_ROW_END_CHARPOS (row)
14794 && !row->ends_at_zv_p
14795 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14796 scroll_p = 1;
14797 }
14798 else if (PT < XFASTINT (w->last_point))
14799 {
14800 /* Cursor has to be moved backward. Note that PT >=
14801 CHARPOS (startp) because of the outer if-statement. */
14802 while (!row->mode_line_p
14803 && (MATRIX_ROW_START_CHARPOS (row) > PT
14804 || (MATRIX_ROW_START_CHARPOS (row) == PT
14805 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14806 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14807 row > w->current_matrix->rows
14808 && (row-1)->ends_in_newline_from_string_p))))
14809 && (row->y > top_scroll_margin
14810 || CHARPOS (startp) == BEGV))
14811 {
14812 xassert (row->enabled_p);
14813 --row;
14814 }
14815
14816 /* Consider the following case: Window starts at BEGV,
14817 there is invisible, intangible text at BEGV, so that
14818 display starts at some point START > BEGV. It can
14819 happen that we are called with PT somewhere between
14820 BEGV and START. Try to handle that case. */
14821 if (row < w->current_matrix->rows
14822 || row->mode_line_p)
14823 {
14824 row = w->current_matrix->rows;
14825 if (row->mode_line_p)
14826 ++row;
14827 }
14828
14829 /* Due to newlines in overlay strings, we may have to
14830 skip forward over overlay strings. */
14831 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14832 && MATRIX_ROW_END_CHARPOS (row) == PT
14833 && !cursor_row_p (row))
14834 ++row;
14835
14836 /* If within the scroll margin, scroll. */
14837 if (row->y < top_scroll_margin
14838 && CHARPOS (startp) != BEGV)
14839 scroll_p = 1;
14840 }
14841 else
14842 {
14843 /* Cursor did not move. So don't scroll even if cursor line
14844 is partially visible, as it was so before. */
14845 rc = CURSOR_MOVEMENT_SUCCESS;
14846 }
14847
14848 if (PT < MATRIX_ROW_START_CHARPOS (row)
14849 || PT > MATRIX_ROW_END_CHARPOS (row))
14850 {
14851 /* if PT is not in the glyph row, give up. */
14852 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14853 must_scroll = 1;
14854 }
14855 else if (rc != CURSOR_MOVEMENT_SUCCESS
14856 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14857 {
14858 /* If rows are bidi-reordered and point moved, back up
14859 until we find a row that does not belong to a
14860 continuation line. This is because we must consider
14861 all rows of a continued line as candidates for the
14862 new cursor positioning, since row start and end
14863 positions change non-linearly with vertical position
14864 in such rows. */
14865 /* FIXME: Revisit this when glyph ``spilling'' in
14866 continuation lines' rows is implemented for
14867 bidi-reordered rows. */
14868 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14869 {
14870 /* If we hit the beginning of the displayed portion
14871 without finding the first row of a continued
14872 line, give up. */
14873 if (row <= w->current_matrix->rows)
14874 {
14875 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14876 break;
14877 }
14878 xassert (row->enabled_p);
14879 --row;
14880 }
14881 }
14882 if (must_scroll)
14883 ;
14884 else if (rc != CURSOR_MOVEMENT_SUCCESS
14885 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14886 && make_cursor_line_fully_visible_p)
14887 {
14888 if (PT == MATRIX_ROW_END_CHARPOS (row)
14889 && !row->ends_at_zv_p
14890 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14891 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14892 else if (row->height > window_box_height (w))
14893 {
14894 /* If we end up in a partially visible line, let's
14895 make it fully visible, except when it's taller
14896 than the window, in which case we can't do much
14897 about it. */
14898 *scroll_step = 1;
14899 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14900 }
14901 else
14902 {
14903 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14904 if (!cursor_row_fully_visible_p (w, 0, 1))
14905 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14906 else
14907 rc = CURSOR_MOVEMENT_SUCCESS;
14908 }
14909 }
14910 else if (scroll_p)
14911 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14912 else if (rc != CURSOR_MOVEMENT_SUCCESS
14913 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14914 {
14915 /* With bidi-reordered rows, there could be more than
14916 one candidate row whose start and end positions
14917 occlude point. We need to let set_cursor_from_row
14918 find the best candidate. */
14919 /* FIXME: Revisit this when glyph ``spilling'' in
14920 continuation lines' rows is implemented for
14921 bidi-reordered rows. */
14922 int rv = 0;
14923
14924 do
14925 {
14926 int at_zv_p = 0, exact_match_p = 0;
14927
14928 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14929 && PT <= MATRIX_ROW_END_CHARPOS (row)
14930 && cursor_row_p (row))
14931 rv |= set_cursor_from_row (w, row, w->current_matrix,
14932 0, 0, 0, 0);
14933 /* As soon as we've found the exact match for point,
14934 or the first suitable row whose ends_at_zv_p flag
14935 is set, we are done. */
14936 at_zv_p =
14937 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14938 if (rv && !at_zv_p
14939 && w->cursor.hpos >= 0
14940 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
14941 w->cursor.vpos))
14942 {
14943 struct glyph_row *candidate =
14944 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14945 struct glyph *g =
14946 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14947 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
14948
14949 exact_match_p =
14950 (BUFFERP (g->object) && g->charpos == PT)
14951 || (INTEGERP (g->object)
14952 && (g->charpos == PT
14953 || (g->charpos == 0 && endpos - 1 == PT)));
14954 }
14955 if (rv && (at_zv_p || exact_match_p))
14956 {
14957 rc = CURSOR_MOVEMENT_SUCCESS;
14958 break;
14959 }
14960 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14961 break;
14962 ++row;
14963 }
14964 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14965 || row->continued_p)
14966 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14967 || (MATRIX_ROW_START_CHARPOS (row) == PT
14968 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14969 /* If we didn't find any candidate rows, or exited the
14970 loop before all the candidates were examined, signal
14971 to the caller that this method failed. */
14972 if (rc != CURSOR_MOVEMENT_SUCCESS
14973 && !(rv
14974 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14975 && !row->continued_p))
14976 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14977 else if (rv)
14978 rc = CURSOR_MOVEMENT_SUCCESS;
14979 }
14980 else
14981 {
14982 do
14983 {
14984 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14985 {
14986 rc = CURSOR_MOVEMENT_SUCCESS;
14987 break;
14988 }
14989 ++row;
14990 }
14991 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14992 && MATRIX_ROW_START_CHARPOS (row) == PT
14993 && cursor_row_p (row));
14994 }
14995 }
14996 }
14997
14998 return rc;
14999 }
15000
15001 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15002 static
15003 #endif
15004 void
15005 set_vertical_scroll_bar (struct window *w)
15006 {
15007 ptrdiff_t start, end, whole;
15008
15009 /* Calculate the start and end positions for the current window.
15010 At some point, it would be nice to choose between scrollbars
15011 which reflect the whole buffer size, with special markers
15012 indicating narrowing, and scrollbars which reflect only the
15013 visible region.
15014
15015 Note that mini-buffers sometimes aren't displaying any text. */
15016 if (!MINI_WINDOW_P (w)
15017 || (w == XWINDOW (minibuf_window)
15018 && NILP (echo_area_buffer[0])))
15019 {
15020 struct buffer *buf = XBUFFER (w->buffer);
15021 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15022 start = marker_position (w->start) - BUF_BEGV (buf);
15023 /* I don't think this is guaranteed to be right. For the
15024 moment, we'll pretend it is. */
15025 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15026
15027 if (end < start)
15028 end = start;
15029 if (whole < (end - start))
15030 whole = end - start;
15031 }
15032 else
15033 start = end = whole = 0;
15034
15035 /* Indicate what this scroll bar ought to be displaying now. */
15036 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15037 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15038 (w, end - start, whole, start);
15039 }
15040
15041
15042 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15043 selected_window is redisplayed.
15044
15045 We can return without actually redisplaying the window if
15046 fonts_changed_p is nonzero. In that case, redisplay_internal will
15047 retry. */
15048
15049 static void
15050 redisplay_window (Lisp_Object window, int just_this_one_p)
15051 {
15052 struct window *w = XWINDOW (window);
15053 struct frame *f = XFRAME (w->frame);
15054 struct buffer *buffer = XBUFFER (w->buffer);
15055 struct buffer *old = current_buffer;
15056 struct text_pos lpoint, opoint, startp;
15057 int update_mode_line;
15058 int tem;
15059 struct it it;
15060 /* Record it now because it's overwritten. */
15061 int current_matrix_up_to_date_p = 0;
15062 int used_current_matrix_p = 0;
15063 /* This is less strict than current_matrix_up_to_date_p.
15064 It indicates that the buffer contents and narrowing are unchanged. */
15065 int buffer_unchanged_p = 0;
15066 int temp_scroll_step = 0;
15067 ptrdiff_t count = SPECPDL_INDEX ();
15068 int rc;
15069 int centering_position = -1;
15070 int last_line_misfit = 0;
15071 ptrdiff_t beg_unchanged, end_unchanged;
15072
15073 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15074 opoint = lpoint;
15075
15076 /* W must be a leaf window here. */
15077 xassert (!NILP (w->buffer));
15078 #if GLYPH_DEBUG
15079 *w->desired_matrix->method = 0;
15080 #endif
15081
15082 restart:
15083 reconsider_clip_changes (w, buffer);
15084
15085 /* Has the mode line to be updated? */
15086 update_mode_line = (!NILP (w->update_mode_line)
15087 || update_mode_lines
15088 || buffer->clip_changed
15089 || buffer->prevent_redisplay_optimizations_p);
15090
15091 if (MINI_WINDOW_P (w))
15092 {
15093 if (w == XWINDOW (echo_area_window)
15094 && !NILP (echo_area_buffer[0]))
15095 {
15096 if (update_mode_line)
15097 /* We may have to update a tty frame's menu bar or a
15098 tool-bar. Example `M-x C-h C-h C-g'. */
15099 goto finish_menu_bars;
15100 else
15101 /* We've already displayed the echo area glyphs in this window. */
15102 goto finish_scroll_bars;
15103 }
15104 else if ((w != XWINDOW (minibuf_window)
15105 || minibuf_level == 0)
15106 /* When buffer is nonempty, redisplay window normally. */
15107 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15108 /* Quail displays non-mini buffers in minibuffer window.
15109 In that case, redisplay the window normally. */
15110 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15111 {
15112 /* W is a mini-buffer window, but it's not active, so clear
15113 it. */
15114 int yb = window_text_bottom_y (w);
15115 struct glyph_row *row;
15116 int y;
15117
15118 for (y = 0, row = w->desired_matrix->rows;
15119 y < yb;
15120 y += row->height, ++row)
15121 blank_row (w, row, y);
15122 goto finish_scroll_bars;
15123 }
15124
15125 clear_glyph_matrix (w->desired_matrix);
15126 }
15127
15128 /* Otherwise set up data on this window; select its buffer and point
15129 value. */
15130 /* Really select the buffer, for the sake of buffer-local
15131 variables. */
15132 set_buffer_internal_1 (XBUFFER (w->buffer));
15133
15134 current_matrix_up_to_date_p
15135 = (!NILP (w->window_end_valid)
15136 && !current_buffer->clip_changed
15137 && !current_buffer->prevent_redisplay_optimizations_p
15138 && XFASTINT (w->last_modified) >= MODIFF
15139 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15140
15141 /* Run the window-bottom-change-functions
15142 if it is possible that the text on the screen has changed
15143 (either due to modification of the text, or any other reason). */
15144 if (!current_matrix_up_to_date_p
15145 && !NILP (Vwindow_text_change_functions))
15146 {
15147 safe_run_hooks (Qwindow_text_change_functions);
15148 goto restart;
15149 }
15150
15151 beg_unchanged = BEG_UNCHANGED;
15152 end_unchanged = END_UNCHANGED;
15153
15154 SET_TEXT_POS (opoint, PT, PT_BYTE);
15155
15156 specbind (Qinhibit_point_motion_hooks, Qt);
15157
15158 buffer_unchanged_p
15159 = (!NILP (w->window_end_valid)
15160 && !current_buffer->clip_changed
15161 && XFASTINT (w->last_modified) >= MODIFF
15162 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15163
15164 /* When windows_or_buffers_changed is non-zero, we can't rely on
15165 the window end being valid, so set it to nil there. */
15166 if (windows_or_buffers_changed)
15167 {
15168 /* If window starts on a continuation line, maybe adjust the
15169 window start in case the window's width changed. */
15170 if (XMARKER (w->start)->buffer == current_buffer)
15171 compute_window_start_on_continuation_line (w);
15172
15173 w->window_end_valid = Qnil;
15174 }
15175
15176 /* Some sanity checks. */
15177 CHECK_WINDOW_END (w);
15178 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15179 abort ();
15180 if (BYTEPOS (opoint) < CHARPOS (opoint))
15181 abort ();
15182
15183 /* If %c is in mode line, update it if needed. */
15184 if (!NILP (w->column_number_displayed)
15185 /* This alternative quickly identifies a common case
15186 where no change is needed. */
15187 && !(PT == XFASTINT (w->last_point)
15188 && XFASTINT (w->last_modified) >= MODIFF
15189 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15190 && (XFASTINT (w->column_number_displayed) != current_column ()))
15191 update_mode_line = 1;
15192
15193 /* Count number of windows showing the selected buffer. An indirect
15194 buffer counts as its base buffer. */
15195 if (!just_this_one_p)
15196 {
15197 struct buffer *current_base, *window_base;
15198 current_base = current_buffer;
15199 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15200 if (current_base->base_buffer)
15201 current_base = current_base->base_buffer;
15202 if (window_base->base_buffer)
15203 window_base = window_base->base_buffer;
15204 if (current_base == window_base)
15205 buffer_shared++;
15206 }
15207
15208 /* Point refers normally to the selected window. For any other
15209 window, set up appropriate value. */
15210 if (!EQ (window, selected_window))
15211 {
15212 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15213 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15214 if (new_pt < BEGV)
15215 {
15216 new_pt = BEGV;
15217 new_pt_byte = BEGV_BYTE;
15218 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15219 }
15220 else if (new_pt > (ZV - 1))
15221 {
15222 new_pt = ZV;
15223 new_pt_byte = ZV_BYTE;
15224 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15225 }
15226
15227 /* We don't use SET_PT so that the point-motion hooks don't run. */
15228 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15229 }
15230
15231 /* If any of the character widths specified in the display table
15232 have changed, invalidate the width run cache. It's true that
15233 this may be a bit late to catch such changes, but the rest of
15234 redisplay goes (non-fatally) haywire when the display table is
15235 changed, so why should we worry about doing any better? */
15236 if (current_buffer->width_run_cache)
15237 {
15238 struct Lisp_Char_Table *disptab = buffer_display_table ();
15239
15240 if (! disptab_matches_widthtab (disptab,
15241 XVECTOR (BVAR (current_buffer, width_table))))
15242 {
15243 invalidate_region_cache (current_buffer,
15244 current_buffer->width_run_cache,
15245 BEG, Z);
15246 recompute_width_table (current_buffer, disptab);
15247 }
15248 }
15249
15250 /* If window-start is screwed up, choose a new one. */
15251 if (XMARKER (w->start)->buffer != current_buffer)
15252 goto recenter;
15253
15254 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15255
15256 /* If someone specified a new starting point but did not insist,
15257 check whether it can be used. */
15258 if (!NILP (w->optional_new_start)
15259 && CHARPOS (startp) >= BEGV
15260 && CHARPOS (startp) <= ZV)
15261 {
15262 w->optional_new_start = Qnil;
15263 start_display (&it, w, startp);
15264 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15265 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15266 if (IT_CHARPOS (it) == PT)
15267 w->force_start = Qt;
15268 /* IT may overshoot PT if text at PT is invisible. */
15269 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15270 w->force_start = Qt;
15271 }
15272
15273 force_start:
15274
15275 /* Handle case where place to start displaying has been specified,
15276 unless the specified location is outside the accessible range. */
15277 if (!NILP (w->force_start)
15278 || w->frozen_window_start_p)
15279 {
15280 /* We set this later on if we have to adjust point. */
15281 int new_vpos = -1;
15282
15283 w->force_start = Qnil;
15284 w->vscroll = 0;
15285 w->window_end_valid = Qnil;
15286
15287 /* Forget any recorded base line for line number display. */
15288 if (!buffer_unchanged_p)
15289 w->base_line_number = Qnil;
15290
15291 /* Redisplay the mode line. Select the buffer properly for that.
15292 Also, run the hook window-scroll-functions
15293 because we have scrolled. */
15294 /* Note, we do this after clearing force_start because
15295 if there's an error, it is better to forget about force_start
15296 than to get into an infinite loop calling the hook functions
15297 and having them get more errors. */
15298 if (!update_mode_line
15299 || ! NILP (Vwindow_scroll_functions))
15300 {
15301 update_mode_line = 1;
15302 w->update_mode_line = Qt;
15303 startp = run_window_scroll_functions (window, startp);
15304 }
15305
15306 w->last_modified = make_number (0);
15307 w->last_overlay_modified = make_number (0);
15308 if (CHARPOS (startp) < BEGV)
15309 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15310 else if (CHARPOS (startp) > ZV)
15311 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15312
15313 /* Redisplay, then check if cursor has been set during the
15314 redisplay. Give up if new fonts were loaded. */
15315 /* We used to issue a CHECK_MARGINS argument to try_window here,
15316 but this causes scrolling to fail when point begins inside
15317 the scroll margin (bug#148) -- cyd */
15318 if (!try_window (window, startp, 0))
15319 {
15320 w->force_start = Qt;
15321 clear_glyph_matrix (w->desired_matrix);
15322 goto need_larger_matrices;
15323 }
15324
15325 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15326 {
15327 /* If point does not appear, try to move point so it does
15328 appear. The desired matrix has been built above, so we
15329 can use it here. */
15330 new_vpos = window_box_height (w) / 2;
15331 }
15332
15333 if (!cursor_row_fully_visible_p (w, 0, 0))
15334 {
15335 /* Point does appear, but on a line partly visible at end of window.
15336 Move it back to a fully-visible line. */
15337 new_vpos = window_box_height (w);
15338 }
15339
15340 /* If we need to move point for either of the above reasons,
15341 now actually do it. */
15342 if (new_vpos >= 0)
15343 {
15344 struct glyph_row *row;
15345
15346 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15347 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15348 ++row;
15349
15350 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15351 MATRIX_ROW_START_BYTEPOS (row));
15352
15353 if (w != XWINDOW (selected_window))
15354 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15355 else if (current_buffer == old)
15356 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15357
15358 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15359
15360 /* If we are highlighting the region, then we just changed
15361 the region, so redisplay to show it. */
15362 if (!NILP (Vtransient_mark_mode)
15363 && !NILP (BVAR (current_buffer, mark_active)))
15364 {
15365 clear_glyph_matrix (w->desired_matrix);
15366 if (!try_window (window, startp, 0))
15367 goto need_larger_matrices;
15368 }
15369 }
15370
15371 #if GLYPH_DEBUG
15372 debug_method_add (w, "forced window start");
15373 #endif
15374 goto done;
15375 }
15376
15377 /* Handle case where text has not changed, only point, and it has
15378 not moved off the frame, and we are not retrying after hscroll.
15379 (current_matrix_up_to_date_p is nonzero when retrying.) */
15380 if (current_matrix_up_to_date_p
15381 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15382 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15383 {
15384 switch (rc)
15385 {
15386 case CURSOR_MOVEMENT_SUCCESS:
15387 used_current_matrix_p = 1;
15388 goto done;
15389
15390 case CURSOR_MOVEMENT_MUST_SCROLL:
15391 goto try_to_scroll;
15392
15393 default:
15394 abort ();
15395 }
15396 }
15397 /* If current starting point was originally the beginning of a line
15398 but no longer is, find a new starting point. */
15399 else if (!NILP (w->start_at_line_beg)
15400 && !(CHARPOS (startp) <= BEGV
15401 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15402 {
15403 #if GLYPH_DEBUG
15404 debug_method_add (w, "recenter 1");
15405 #endif
15406 goto recenter;
15407 }
15408
15409 /* Try scrolling with try_window_id. Value is > 0 if update has
15410 been done, it is -1 if we know that the same window start will
15411 not work. It is 0 if unsuccessful for some other reason. */
15412 else if ((tem = try_window_id (w)) != 0)
15413 {
15414 #if GLYPH_DEBUG
15415 debug_method_add (w, "try_window_id %d", tem);
15416 #endif
15417
15418 if (fonts_changed_p)
15419 goto need_larger_matrices;
15420 if (tem > 0)
15421 goto done;
15422
15423 /* Otherwise try_window_id has returned -1 which means that we
15424 don't want the alternative below this comment to execute. */
15425 }
15426 else if (CHARPOS (startp) >= BEGV
15427 && CHARPOS (startp) <= ZV
15428 && PT >= CHARPOS (startp)
15429 && (CHARPOS (startp) < ZV
15430 /* Avoid starting at end of buffer. */
15431 || CHARPOS (startp) == BEGV
15432 || (XFASTINT (w->last_modified) >= MODIFF
15433 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15434 {
15435 int d1, d2, d3, d4, d5, d6;
15436
15437 /* If first window line is a continuation line, and window start
15438 is inside the modified region, but the first change is before
15439 current window start, we must select a new window start.
15440
15441 However, if this is the result of a down-mouse event (e.g. by
15442 extending the mouse-drag-overlay), we don't want to select a
15443 new window start, since that would change the position under
15444 the mouse, resulting in an unwanted mouse-movement rather
15445 than a simple mouse-click. */
15446 if (NILP (w->start_at_line_beg)
15447 && NILP (do_mouse_tracking)
15448 && CHARPOS (startp) > BEGV
15449 && CHARPOS (startp) > BEG + beg_unchanged
15450 && CHARPOS (startp) <= Z - end_unchanged
15451 /* Even if w->start_at_line_beg is nil, a new window may
15452 start at a line_beg, since that's how set_buffer_window
15453 sets it. So, we need to check the return value of
15454 compute_window_start_on_continuation_line. (See also
15455 bug#197). */
15456 && XMARKER (w->start)->buffer == current_buffer
15457 && compute_window_start_on_continuation_line (w)
15458 /* It doesn't make sense to force the window start like we
15459 do at label force_start if it is already known that point
15460 will not be visible in the resulting window, because
15461 doing so will move point from its correct position
15462 instead of scrolling the window to bring point into view.
15463 See bug#9324. */
15464 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15465 {
15466 w->force_start = Qt;
15467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15468 goto force_start;
15469 }
15470
15471 #if GLYPH_DEBUG
15472 debug_method_add (w, "same window start");
15473 #endif
15474
15475 /* Try to redisplay starting at same place as before.
15476 If point has not moved off frame, accept the results. */
15477 if (!current_matrix_up_to_date_p
15478 /* Don't use try_window_reusing_current_matrix in this case
15479 because a window scroll function can have changed the
15480 buffer. */
15481 || !NILP (Vwindow_scroll_functions)
15482 || MINI_WINDOW_P (w)
15483 || !(used_current_matrix_p
15484 = try_window_reusing_current_matrix (w)))
15485 {
15486 IF_DEBUG (debug_method_add (w, "1"));
15487 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15488 /* -1 means we need to scroll.
15489 0 means we need new matrices, but fonts_changed_p
15490 is set in that case, so we will detect it below. */
15491 goto try_to_scroll;
15492 }
15493
15494 if (fonts_changed_p)
15495 goto need_larger_matrices;
15496
15497 if (w->cursor.vpos >= 0)
15498 {
15499 if (!just_this_one_p
15500 || current_buffer->clip_changed
15501 || BEG_UNCHANGED < CHARPOS (startp))
15502 /* Forget any recorded base line for line number display. */
15503 w->base_line_number = Qnil;
15504
15505 if (!cursor_row_fully_visible_p (w, 1, 0))
15506 {
15507 clear_glyph_matrix (w->desired_matrix);
15508 last_line_misfit = 1;
15509 }
15510 /* Drop through and scroll. */
15511 else
15512 goto done;
15513 }
15514 else
15515 clear_glyph_matrix (w->desired_matrix);
15516 }
15517
15518 try_to_scroll:
15519
15520 w->last_modified = make_number (0);
15521 w->last_overlay_modified = make_number (0);
15522
15523 /* Redisplay the mode line. Select the buffer properly for that. */
15524 if (!update_mode_line)
15525 {
15526 update_mode_line = 1;
15527 w->update_mode_line = Qt;
15528 }
15529
15530 /* Try to scroll by specified few lines. */
15531 if ((scroll_conservatively
15532 || emacs_scroll_step
15533 || temp_scroll_step
15534 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15535 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15536 && CHARPOS (startp) >= BEGV
15537 && CHARPOS (startp) <= ZV)
15538 {
15539 /* The function returns -1 if new fonts were loaded, 1 if
15540 successful, 0 if not successful. */
15541 int ss = try_scrolling (window, just_this_one_p,
15542 scroll_conservatively,
15543 emacs_scroll_step,
15544 temp_scroll_step, last_line_misfit);
15545 switch (ss)
15546 {
15547 case SCROLLING_SUCCESS:
15548 goto done;
15549
15550 case SCROLLING_NEED_LARGER_MATRICES:
15551 goto need_larger_matrices;
15552
15553 case SCROLLING_FAILED:
15554 break;
15555
15556 default:
15557 abort ();
15558 }
15559 }
15560
15561 /* Finally, just choose a place to start which positions point
15562 according to user preferences. */
15563
15564 recenter:
15565
15566 #if GLYPH_DEBUG
15567 debug_method_add (w, "recenter");
15568 #endif
15569
15570 /* w->vscroll = 0; */
15571
15572 /* Forget any previously recorded base line for line number display. */
15573 if (!buffer_unchanged_p)
15574 w->base_line_number = Qnil;
15575
15576 /* Determine the window start relative to point. */
15577 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15578 it.current_y = it.last_visible_y;
15579 if (centering_position < 0)
15580 {
15581 int margin =
15582 scroll_margin > 0
15583 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15584 : 0;
15585 ptrdiff_t margin_pos = CHARPOS (startp);
15586 Lisp_Object aggressive;
15587 int scrolling_up;
15588
15589 /* If there is a scroll margin at the top of the window, find
15590 its character position. */
15591 if (margin
15592 /* Cannot call start_display if startp is not in the
15593 accessible region of the buffer. This can happen when we
15594 have just switched to a different buffer and/or changed
15595 its restriction. In that case, startp is initialized to
15596 the character position 1 (BEG) because we did not yet
15597 have chance to display the buffer even once. */
15598 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15599 {
15600 struct it it1;
15601 void *it1data = NULL;
15602
15603 SAVE_IT (it1, it, it1data);
15604 start_display (&it1, w, startp);
15605 move_it_vertically (&it1, margin);
15606 margin_pos = IT_CHARPOS (it1);
15607 RESTORE_IT (&it, &it, it1data);
15608 }
15609 scrolling_up = PT > margin_pos;
15610 aggressive =
15611 scrolling_up
15612 ? BVAR (current_buffer, scroll_up_aggressively)
15613 : BVAR (current_buffer, scroll_down_aggressively);
15614
15615 if (!MINI_WINDOW_P (w)
15616 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15617 {
15618 int pt_offset = 0;
15619
15620 /* Setting scroll-conservatively overrides
15621 scroll-*-aggressively. */
15622 if (!scroll_conservatively && NUMBERP (aggressive))
15623 {
15624 double float_amount = XFLOATINT (aggressive);
15625
15626 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15627 if (pt_offset == 0 && float_amount > 0)
15628 pt_offset = 1;
15629 if (pt_offset && margin > 0)
15630 margin -= 1;
15631 }
15632 /* Compute how much to move the window start backward from
15633 point so that point will be displayed where the user
15634 wants it. */
15635 if (scrolling_up)
15636 {
15637 centering_position = it.last_visible_y;
15638 if (pt_offset)
15639 centering_position -= pt_offset;
15640 centering_position -=
15641 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15642 + WINDOW_HEADER_LINE_HEIGHT (w);
15643 /* Don't let point enter the scroll margin near top of
15644 the window. */
15645 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15646 centering_position = margin * FRAME_LINE_HEIGHT (f);
15647 }
15648 else
15649 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15650 }
15651 else
15652 /* Set the window start half the height of the window backward
15653 from point. */
15654 centering_position = window_box_height (w) / 2;
15655 }
15656 move_it_vertically_backward (&it, centering_position);
15657
15658 xassert (IT_CHARPOS (it) >= BEGV);
15659
15660 /* The function move_it_vertically_backward may move over more
15661 than the specified y-distance. If it->w is small, e.g. a
15662 mini-buffer window, we may end up in front of the window's
15663 display area. Start displaying at the start of the line
15664 containing PT in this case. */
15665 if (it.current_y <= 0)
15666 {
15667 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15668 move_it_vertically_backward (&it, 0);
15669 it.current_y = 0;
15670 }
15671
15672 it.current_x = it.hpos = 0;
15673
15674 /* Set the window start position here explicitly, to avoid an
15675 infinite loop in case the functions in window-scroll-functions
15676 get errors. */
15677 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15678
15679 /* Run scroll hooks. */
15680 startp = run_window_scroll_functions (window, it.current.pos);
15681
15682 /* Redisplay the window. */
15683 if (!current_matrix_up_to_date_p
15684 || windows_or_buffers_changed
15685 || cursor_type_changed
15686 /* Don't use try_window_reusing_current_matrix in this case
15687 because it can have changed the buffer. */
15688 || !NILP (Vwindow_scroll_functions)
15689 || !just_this_one_p
15690 || MINI_WINDOW_P (w)
15691 || !(used_current_matrix_p
15692 = try_window_reusing_current_matrix (w)))
15693 try_window (window, startp, 0);
15694
15695 /* If new fonts have been loaded (due to fontsets), give up. We
15696 have to start a new redisplay since we need to re-adjust glyph
15697 matrices. */
15698 if (fonts_changed_p)
15699 goto need_larger_matrices;
15700
15701 /* If cursor did not appear assume that the middle of the window is
15702 in the first line of the window. Do it again with the next line.
15703 (Imagine a window of height 100, displaying two lines of height
15704 60. Moving back 50 from it->last_visible_y will end in the first
15705 line.) */
15706 if (w->cursor.vpos < 0)
15707 {
15708 if (!NILP (w->window_end_valid)
15709 && PT >= Z - XFASTINT (w->window_end_pos))
15710 {
15711 clear_glyph_matrix (w->desired_matrix);
15712 move_it_by_lines (&it, 1);
15713 try_window (window, it.current.pos, 0);
15714 }
15715 else if (PT < IT_CHARPOS (it))
15716 {
15717 clear_glyph_matrix (w->desired_matrix);
15718 move_it_by_lines (&it, -1);
15719 try_window (window, it.current.pos, 0);
15720 }
15721 else
15722 {
15723 /* Not much we can do about it. */
15724 }
15725 }
15726
15727 /* Consider the following case: Window starts at BEGV, there is
15728 invisible, intangible text at BEGV, so that display starts at
15729 some point START > BEGV. It can happen that we are called with
15730 PT somewhere between BEGV and START. Try to handle that case. */
15731 if (w->cursor.vpos < 0)
15732 {
15733 struct glyph_row *row = w->current_matrix->rows;
15734 if (row->mode_line_p)
15735 ++row;
15736 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15737 }
15738
15739 if (!cursor_row_fully_visible_p (w, 0, 0))
15740 {
15741 /* If vscroll is enabled, disable it and try again. */
15742 if (w->vscroll)
15743 {
15744 w->vscroll = 0;
15745 clear_glyph_matrix (w->desired_matrix);
15746 goto recenter;
15747 }
15748
15749 /* Users who set scroll-conservatively to a large number want
15750 point just above/below the scroll margin. If we ended up
15751 with point's row partially visible, move the window start to
15752 make that row fully visible and out of the margin. */
15753 if (scroll_conservatively > SCROLL_LIMIT)
15754 {
15755 int margin =
15756 scroll_margin > 0
15757 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15758 : 0;
15759 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15760
15761 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15762 clear_glyph_matrix (w->desired_matrix);
15763 if (1 == try_window (window, it.current.pos,
15764 TRY_WINDOW_CHECK_MARGINS))
15765 goto done;
15766 }
15767
15768 /* If centering point failed to make the whole line visible,
15769 put point at the top instead. That has to make the whole line
15770 visible, if it can be done. */
15771 if (centering_position == 0)
15772 goto done;
15773
15774 clear_glyph_matrix (w->desired_matrix);
15775 centering_position = 0;
15776 goto recenter;
15777 }
15778
15779 done:
15780
15781 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15782 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15783 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15784 ? Qt : Qnil);
15785
15786 /* Display the mode line, if we must. */
15787 if ((update_mode_line
15788 /* If window not full width, must redo its mode line
15789 if (a) the window to its side is being redone and
15790 (b) we do a frame-based redisplay. This is a consequence
15791 of how inverted lines are drawn in frame-based redisplay. */
15792 || (!just_this_one_p
15793 && !FRAME_WINDOW_P (f)
15794 && !WINDOW_FULL_WIDTH_P (w))
15795 /* Line number to display. */
15796 || INTEGERP (w->base_line_pos)
15797 /* Column number is displayed and different from the one displayed. */
15798 || (!NILP (w->column_number_displayed)
15799 && (XFASTINT (w->column_number_displayed) != current_column ())))
15800 /* This means that the window has a mode line. */
15801 && (WINDOW_WANTS_MODELINE_P (w)
15802 || WINDOW_WANTS_HEADER_LINE_P (w)))
15803 {
15804 display_mode_lines (w);
15805
15806 /* If mode line height has changed, arrange for a thorough
15807 immediate redisplay using the correct mode line height. */
15808 if (WINDOW_WANTS_MODELINE_P (w)
15809 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15810 {
15811 fonts_changed_p = 1;
15812 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15813 = DESIRED_MODE_LINE_HEIGHT (w);
15814 }
15815
15816 /* If header line height has changed, arrange for a thorough
15817 immediate redisplay using the correct header line height. */
15818 if (WINDOW_WANTS_HEADER_LINE_P (w)
15819 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15820 {
15821 fonts_changed_p = 1;
15822 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15823 = DESIRED_HEADER_LINE_HEIGHT (w);
15824 }
15825
15826 if (fonts_changed_p)
15827 goto need_larger_matrices;
15828 }
15829
15830 if (!line_number_displayed
15831 && !BUFFERP (w->base_line_pos))
15832 {
15833 w->base_line_pos = Qnil;
15834 w->base_line_number = Qnil;
15835 }
15836
15837 finish_menu_bars:
15838
15839 /* When we reach a frame's selected window, redo the frame's menu bar. */
15840 if (update_mode_line
15841 && EQ (FRAME_SELECTED_WINDOW (f), window))
15842 {
15843 int redisplay_menu_p = 0;
15844
15845 if (FRAME_WINDOW_P (f))
15846 {
15847 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15848 || defined (HAVE_NS) || defined (USE_GTK)
15849 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15850 #else
15851 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15852 #endif
15853 }
15854 else
15855 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15856
15857 if (redisplay_menu_p)
15858 display_menu_bar (w);
15859
15860 #ifdef HAVE_WINDOW_SYSTEM
15861 if (FRAME_WINDOW_P (f))
15862 {
15863 #if defined (USE_GTK) || defined (HAVE_NS)
15864 if (FRAME_EXTERNAL_TOOL_BAR (f))
15865 redisplay_tool_bar (f);
15866 #else
15867 if (WINDOWP (f->tool_bar_window)
15868 && (FRAME_TOOL_BAR_LINES (f) > 0
15869 || !NILP (Vauto_resize_tool_bars))
15870 && redisplay_tool_bar (f))
15871 ignore_mouse_drag_p = 1;
15872 #endif
15873 }
15874 #endif
15875 }
15876
15877 #ifdef HAVE_WINDOW_SYSTEM
15878 if (FRAME_WINDOW_P (f)
15879 && update_window_fringes (w, (just_this_one_p
15880 || (!used_current_matrix_p && !overlay_arrow_seen)
15881 || w->pseudo_window_p)))
15882 {
15883 update_begin (f);
15884 BLOCK_INPUT;
15885 if (draw_window_fringes (w, 1))
15886 x_draw_vertical_border (w);
15887 UNBLOCK_INPUT;
15888 update_end (f);
15889 }
15890 #endif /* HAVE_WINDOW_SYSTEM */
15891
15892 /* We go to this label, with fonts_changed_p nonzero,
15893 if it is necessary to try again using larger glyph matrices.
15894 We have to redeem the scroll bar even in this case,
15895 because the loop in redisplay_internal expects that. */
15896 need_larger_matrices:
15897 ;
15898 finish_scroll_bars:
15899
15900 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15901 {
15902 /* Set the thumb's position and size. */
15903 set_vertical_scroll_bar (w);
15904
15905 /* Note that we actually used the scroll bar attached to this
15906 window, so it shouldn't be deleted at the end of redisplay. */
15907 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15908 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15909 }
15910
15911 /* Restore current_buffer and value of point in it. The window
15912 update may have changed the buffer, so first make sure `opoint'
15913 is still valid (Bug#6177). */
15914 if (CHARPOS (opoint) < BEGV)
15915 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15916 else if (CHARPOS (opoint) > ZV)
15917 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15918 else
15919 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15920
15921 set_buffer_internal_1 (old);
15922 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15923 shorter. This can be caused by log truncation in *Messages*. */
15924 if (CHARPOS (lpoint) <= ZV)
15925 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15926
15927 unbind_to (count, Qnil);
15928 }
15929
15930
15931 /* Build the complete desired matrix of WINDOW with a window start
15932 buffer position POS.
15933
15934 Value is 1 if successful. It is zero if fonts were loaded during
15935 redisplay which makes re-adjusting glyph matrices necessary, and -1
15936 if point would appear in the scroll margins.
15937 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15938 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15939 set in FLAGS.) */
15940
15941 int
15942 try_window (Lisp_Object window, struct text_pos pos, int flags)
15943 {
15944 struct window *w = XWINDOW (window);
15945 struct it it;
15946 struct glyph_row *last_text_row = NULL;
15947 struct frame *f = XFRAME (w->frame);
15948
15949 /* Make POS the new window start. */
15950 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15951
15952 /* Mark cursor position as unknown. No overlay arrow seen. */
15953 w->cursor.vpos = -1;
15954 overlay_arrow_seen = 0;
15955
15956 /* Initialize iterator and info to start at POS. */
15957 start_display (&it, w, pos);
15958
15959 /* Display all lines of W. */
15960 while (it.current_y < it.last_visible_y)
15961 {
15962 if (display_line (&it))
15963 last_text_row = it.glyph_row - 1;
15964 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15965 return 0;
15966 }
15967
15968 /* Don't let the cursor end in the scroll margins. */
15969 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15970 && !MINI_WINDOW_P (w))
15971 {
15972 int this_scroll_margin;
15973
15974 if (scroll_margin > 0)
15975 {
15976 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15977 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15978 }
15979 else
15980 this_scroll_margin = 0;
15981
15982 if ((w->cursor.y >= 0 /* not vscrolled */
15983 && w->cursor.y < this_scroll_margin
15984 && CHARPOS (pos) > BEGV
15985 && IT_CHARPOS (it) < ZV)
15986 /* rms: considering make_cursor_line_fully_visible_p here
15987 seems to give wrong results. We don't want to recenter
15988 when the last line is partly visible, we want to allow
15989 that case to be handled in the usual way. */
15990 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15991 {
15992 w->cursor.vpos = -1;
15993 clear_glyph_matrix (w->desired_matrix);
15994 return -1;
15995 }
15996 }
15997
15998 /* If bottom moved off end of frame, change mode line percentage. */
15999 if (XFASTINT (w->window_end_pos) <= 0
16000 && Z != IT_CHARPOS (it))
16001 w->update_mode_line = Qt;
16002
16003 /* Set window_end_pos to the offset of the last character displayed
16004 on the window from the end of current_buffer. Set
16005 window_end_vpos to its row number. */
16006 if (last_text_row)
16007 {
16008 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16009 w->window_end_bytepos
16010 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16011 w->window_end_pos
16012 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16013 w->window_end_vpos
16014 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16015 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16016 ->displays_text_p);
16017 }
16018 else
16019 {
16020 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16021 w->window_end_pos = make_number (Z - ZV);
16022 w->window_end_vpos = make_number (0);
16023 }
16024
16025 /* But that is not valid info until redisplay finishes. */
16026 w->window_end_valid = Qnil;
16027 return 1;
16028 }
16029
16030
16031 \f
16032 /************************************************************************
16033 Window redisplay reusing current matrix when buffer has not changed
16034 ************************************************************************/
16035
16036 /* Try redisplay of window W showing an unchanged buffer with a
16037 different window start than the last time it was displayed by
16038 reusing its current matrix. Value is non-zero if successful.
16039 W->start is the new window start. */
16040
16041 static int
16042 try_window_reusing_current_matrix (struct window *w)
16043 {
16044 struct frame *f = XFRAME (w->frame);
16045 struct glyph_row *bottom_row;
16046 struct it it;
16047 struct run run;
16048 struct text_pos start, new_start;
16049 int nrows_scrolled, i;
16050 struct glyph_row *last_text_row;
16051 struct glyph_row *last_reused_text_row;
16052 struct glyph_row *start_row;
16053 int start_vpos, min_y, max_y;
16054
16055 #if GLYPH_DEBUG
16056 if (inhibit_try_window_reusing)
16057 return 0;
16058 #endif
16059
16060 if (/* This function doesn't handle terminal frames. */
16061 !FRAME_WINDOW_P (f)
16062 /* Don't try to reuse the display if windows have been split
16063 or such. */
16064 || windows_or_buffers_changed
16065 || cursor_type_changed)
16066 return 0;
16067
16068 /* Can't do this if region may have changed. */
16069 if ((!NILP (Vtransient_mark_mode)
16070 && !NILP (BVAR (current_buffer, mark_active)))
16071 || !NILP (w->region_showing)
16072 || !NILP (Vshow_trailing_whitespace))
16073 return 0;
16074
16075 /* If top-line visibility has changed, give up. */
16076 if (WINDOW_WANTS_HEADER_LINE_P (w)
16077 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16078 return 0;
16079
16080 /* Give up if old or new display is scrolled vertically. We could
16081 make this function handle this, but right now it doesn't. */
16082 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16083 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16084 return 0;
16085
16086 /* The variable new_start now holds the new window start. The old
16087 start `start' can be determined from the current matrix. */
16088 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16089 start = start_row->minpos;
16090 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16091
16092 /* Clear the desired matrix for the display below. */
16093 clear_glyph_matrix (w->desired_matrix);
16094
16095 if (CHARPOS (new_start) <= CHARPOS (start))
16096 {
16097 /* Don't use this method if the display starts with an ellipsis
16098 displayed for invisible text. It's not easy to handle that case
16099 below, and it's certainly not worth the effort since this is
16100 not a frequent case. */
16101 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16102 return 0;
16103
16104 IF_DEBUG (debug_method_add (w, "twu1"));
16105
16106 /* Display up to a row that can be reused. The variable
16107 last_text_row is set to the last row displayed that displays
16108 text. Note that it.vpos == 0 if or if not there is a
16109 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16110 start_display (&it, w, new_start);
16111 w->cursor.vpos = -1;
16112 last_text_row = last_reused_text_row = NULL;
16113
16114 while (it.current_y < it.last_visible_y
16115 && !fonts_changed_p)
16116 {
16117 /* If we have reached into the characters in the START row,
16118 that means the line boundaries have changed. So we
16119 can't start copying with the row START. Maybe it will
16120 work to start copying with the following row. */
16121 while (IT_CHARPOS (it) > CHARPOS (start))
16122 {
16123 /* Advance to the next row as the "start". */
16124 start_row++;
16125 start = start_row->minpos;
16126 /* If there are no more rows to try, or just one, give up. */
16127 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16128 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16129 || CHARPOS (start) == ZV)
16130 {
16131 clear_glyph_matrix (w->desired_matrix);
16132 return 0;
16133 }
16134
16135 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16136 }
16137 /* If we have reached alignment, we can copy the rest of the
16138 rows. */
16139 if (IT_CHARPOS (it) == CHARPOS (start)
16140 /* Don't accept "alignment" inside a display vector,
16141 since start_row could have started in the middle of
16142 that same display vector (thus their character
16143 positions match), and we have no way of telling if
16144 that is the case. */
16145 && it.current.dpvec_index < 0)
16146 break;
16147
16148 if (display_line (&it))
16149 last_text_row = it.glyph_row - 1;
16150
16151 }
16152
16153 /* A value of current_y < last_visible_y means that we stopped
16154 at the previous window start, which in turn means that we
16155 have at least one reusable row. */
16156 if (it.current_y < it.last_visible_y)
16157 {
16158 struct glyph_row *row;
16159
16160 /* IT.vpos always starts from 0; it counts text lines. */
16161 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16162
16163 /* Find PT if not already found in the lines displayed. */
16164 if (w->cursor.vpos < 0)
16165 {
16166 int dy = it.current_y - start_row->y;
16167
16168 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16169 row = row_containing_pos (w, PT, row, NULL, dy);
16170 if (row)
16171 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16172 dy, nrows_scrolled);
16173 else
16174 {
16175 clear_glyph_matrix (w->desired_matrix);
16176 return 0;
16177 }
16178 }
16179
16180 /* Scroll the display. Do it before the current matrix is
16181 changed. The problem here is that update has not yet
16182 run, i.e. part of the current matrix is not up to date.
16183 scroll_run_hook will clear the cursor, and use the
16184 current matrix to get the height of the row the cursor is
16185 in. */
16186 run.current_y = start_row->y;
16187 run.desired_y = it.current_y;
16188 run.height = it.last_visible_y - it.current_y;
16189
16190 if (run.height > 0 && run.current_y != run.desired_y)
16191 {
16192 update_begin (f);
16193 FRAME_RIF (f)->update_window_begin_hook (w);
16194 FRAME_RIF (f)->clear_window_mouse_face (w);
16195 FRAME_RIF (f)->scroll_run_hook (w, &run);
16196 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16197 update_end (f);
16198 }
16199
16200 /* Shift current matrix down by nrows_scrolled lines. */
16201 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16202 rotate_matrix (w->current_matrix,
16203 start_vpos,
16204 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16205 nrows_scrolled);
16206
16207 /* Disable lines that must be updated. */
16208 for (i = 0; i < nrows_scrolled; ++i)
16209 (start_row + i)->enabled_p = 0;
16210
16211 /* Re-compute Y positions. */
16212 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16213 max_y = it.last_visible_y;
16214 for (row = start_row + nrows_scrolled;
16215 row < bottom_row;
16216 ++row)
16217 {
16218 row->y = it.current_y;
16219 row->visible_height = row->height;
16220
16221 if (row->y < min_y)
16222 row->visible_height -= min_y - row->y;
16223 if (row->y + row->height > max_y)
16224 row->visible_height -= row->y + row->height - max_y;
16225 if (row->fringe_bitmap_periodic_p)
16226 row->redraw_fringe_bitmaps_p = 1;
16227
16228 it.current_y += row->height;
16229
16230 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16231 last_reused_text_row = row;
16232 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16233 break;
16234 }
16235
16236 /* Disable lines in the current matrix which are now
16237 below the window. */
16238 for (++row; row < bottom_row; ++row)
16239 row->enabled_p = row->mode_line_p = 0;
16240 }
16241
16242 /* Update window_end_pos etc.; last_reused_text_row is the last
16243 reused row from the current matrix containing text, if any.
16244 The value of last_text_row is the last displayed line
16245 containing text. */
16246 if (last_reused_text_row)
16247 {
16248 w->window_end_bytepos
16249 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16250 w->window_end_pos
16251 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16252 w->window_end_vpos
16253 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16254 w->current_matrix));
16255 }
16256 else if (last_text_row)
16257 {
16258 w->window_end_bytepos
16259 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16260 w->window_end_pos
16261 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16262 w->window_end_vpos
16263 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16264 }
16265 else
16266 {
16267 /* This window must be completely empty. */
16268 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16269 w->window_end_pos = make_number (Z - ZV);
16270 w->window_end_vpos = make_number (0);
16271 }
16272 w->window_end_valid = Qnil;
16273
16274 /* Update hint: don't try scrolling again in update_window. */
16275 w->desired_matrix->no_scrolling_p = 1;
16276
16277 #if GLYPH_DEBUG
16278 debug_method_add (w, "try_window_reusing_current_matrix 1");
16279 #endif
16280 return 1;
16281 }
16282 else if (CHARPOS (new_start) > CHARPOS (start))
16283 {
16284 struct glyph_row *pt_row, *row;
16285 struct glyph_row *first_reusable_row;
16286 struct glyph_row *first_row_to_display;
16287 int dy;
16288 int yb = window_text_bottom_y (w);
16289
16290 /* Find the row starting at new_start, if there is one. Don't
16291 reuse a partially visible line at the end. */
16292 first_reusable_row = start_row;
16293 while (first_reusable_row->enabled_p
16294 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16295 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16296 < CHARPOS (new_start)))
16297 ++first_reusable_row;
16298
16299 /* Give up if there is no row to reuse. */
16300 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16301 || !first_reusable_row->enabled_p
16302 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16303 != CHARPOS (new_start)))
16304 return 0;
16305
16306 /* We can reuse fully visible rows beginning with
16307 first_reusable_row to the end of the window. Set
16308 first_row_to_display to the first row that cannot be reused.
16309 Set pt_row to the row containing point, if there is any. */
16310 pt_row = NULL;
16311 for (first_row_to_display = first_reusable_row;
16312 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16313 ++first_row_to_display)
16314 {
16315 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16316 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
16317 pt_row = first_row_to_display;
16318 }
16319
16320 /* Start displaying at the start of first_row_to_display. */
16321 xassert (first_row_to_display->y < yb);
16322 init_to_row_start (&it, w, first_row_to_display);
16323
16324 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16325 - start_vpos);
16326 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16327 - nrows_scrolled);
16328 it.current_y = (first_row_to_display->y - first_reusable_row->y
16329 + WINDOW_HEADER_LINE_HEIGHT (w));
16330
16331 /* Display lines beginning with first_row_to_display in the
16332 desired matrix. Set last_text_row to the last row displayed
16333 that displays text. */
16334 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16335 if (pt_row == NULL)
16336 w->cursor.vpos = -1;
16337 last_text_row = NULL;
16338 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16339 if (display_line (&it))
16340 last_text_row = it.glyph_row - 1;
16341
16342 /* If point is in a reused row, adjust y and vpos of the cursor
16343 position. */
16344 if (pt_row)
16345 {
16346 w->cursor.vpos -= nrows_scrolled;
16347 w->cursor.y -= first_reusable_row->y - start_row->y;
16348 }
16349
16350 /* Give up if point isn't in a row displayed or reused. (This
16351 also handles the case where w->cursor.vpos < nrows_scrolled
16352 after the calls to display_line, which can happen with scroll
16353 margins. See bug#1295.) */
16354 if (w->cursor.vpos < 0)
16355 {
16356 clear_glyph_matrix (w->desired_matrix);
16357 return 0;
16358 }
16359
16360 /* Scroll the display. */
16361 run.current_y = first_reusable_row->y;
16362 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16363 run.height = it.last_visible_y - run.current_y;
16364 dy = run.current_y - run.desired_y;
16365
16366 if (run.height)
16367 {
16368 update_begin (f);
16369 FRAME_RIF (f)->update_window_begin_hook (w);
16370 FRAME_RIF (f)->clear_window_mouse_face (w);
16371 FRAME_RIF (f)->scroll_run_hook (w, &run);
16372 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16373 update_end (f);
16374 }
16375
16376 /* Adjust Y positions of reused rows. */
16377 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16378 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16379 max_y = it.last_visible_y;
16380 for (row = first_reusable_row; row < first_row_to_display; ++row)
16381 {
16382 row->y -= dy;
16383 row->visible_height = row->height;
16384 if (row->y < min_y)
16385 row->visible_height -= min_y - row->y;
16386 if (row->y + row->height > max_y)
16387 row->visible_height -= row->y + row->height - max_y;
16388 if (row->fringe_bitmap_periodic_p)
16389 row->redraw_fringe_bitmaps_p = 1;
16390 }
16391
16392 /* Scroll the current matrix. */
16393 xassert (nrows_scrolled > 0);
16394 rotate_matrix (w->current_matrix,
16395 start_vpos,
16396 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16397 -nrows_scrolled);
16398
16399 /* Disable rows not reused. */
16400 for (row -= nrows_scrolled; row < bottom_row; ++row)
16401 row->enabled_p = 0;
16402
16403 /* Point may have moved to a different line, so we cannot assume that
16404 the previous cursor position is valid; locate the correct row. */
16405 if (pt_row)
16406 {
16407 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16408 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
16409 row++)
16410 {
16411 w->cursor.vpos++;
16412 w->cursor.y = row->y;
16413 }
16414 if (row < bottom_row)
16415 {
16416 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16417 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16418
16419 /* Can't use this optimization with bidi-reordered glyph
16420 rows, unless cursor is already at point. */
16421 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16422 {
16423 if (!(w->cursor.hpos >= 0
16424 && w->cursor.hpos < row->used[TEXT_AREA]
16425 && BUFFERP (glyph->object)
16426 && glyph->charpos == PT))
16427 return 0;
16428 }
16429 else
16430 for (; glyph < end
16431 && (!BUFFERP (glyph->object)
16432 || glyph->charpos < PT);
16433 glyph++)
16434 {
16435 w->cursor.hpos++;
16436 w->cursor.x += glyph->pixel_width;
16437 }
16438 }
16439 }
16440
16441 /* Adjust window end. A null value of last_text_row means that
16442 the window end is in reused rows which in turn means that
16443 only its vpos can have changed. */
16444 if (last_text_row)
16445 {
16446 w->window_end_bytepos
16447 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16448 w->window_end_pos
16449 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16450 w->window_end_vpos
16451 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16452 }
16453 else
16454 {
16455 w->window_end_vpos
16456 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16457 }
16458
16459 w->window_end_valid = Qnil;
16460 w->desired_matrix->no_scrolling_p = 1;
16461
16462 #if GLYPH_DEBUG
16463 debug_method_add (w, "try_window_reusing_current_matrix 2");
16464 #endif
16465 return 1;
16466 }
16467
16468 return 0;
16469 }
16470
16471
16472 \f
16473 /************************************************************************
16474 Window redisplay reusing current matrix when buffer has changed
16475 ************************************************************************/
16476
16477 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16478 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16479 ptrdiff_t *, ptrdiff_t *);
16480 static struct glyph_row *
16481 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16482 struct glyph_row *);
16483
16484
16485 /* Return the last row in MATRIX displaying text. If row START is
16486 non-null, start searching with that row. IT gives the dimensions
16487 of the display. Value is null if matrix is empty; otherwise it is
16488 a pointer to the row found. */
16489
16490 static struct glyph_row *
16491 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16492 struct glyph_row *start)
16493 {
16494 struct glyph_row *row, *row_found;
16495
16496 /* Set row_found to the last row in IT->w's current matrix
16497 displaying text. The loop looks funny but think of partially
16498 visible lines. */
16499 row_found = NULL;
16500 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16501 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16502 {
16503 xassert (row->enabled_p);
16504 row_found = row;
16505 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16506 break;
16507 ++row;
16508 }
16509
16510 return row_found;
16511 }
16512
16513
16514 /* Return the last row in the current matrix of W that is not affected
16515 by changes at the start of current_buffer that occurred since W's
16516 current matrix was built. Value is null if no such row exists.
16517
16518 BEG_UNCHANGED us the number of characters unchanged at the start of
16519 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16520 first changed character in current_buffer. Characters at positions <
16521 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16522 when the current matrix was built. */
16523
16524 static struct glyph_row *
16525 find_last_unchanged_at_beg_row (struct window *w)
16526 {
16527 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16528 struct glyph_row *row;
16529 struct glyph_row *row_found = NULL;
16530 int yb = window_text_bottom_y (w);
16531
16532 /* Find the last row displaying unchanged text. */
16533 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16534 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16535 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16536 ++row)
16537 {
16538 if (/* If row ends before first_changed_pos, it is unchanged,
16539 except in some case. */
16540 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16541 /* When row ends in ZV and we write at ZV it is not
16542 unchanged. */
16543 && !row->ends_at_zv_p
16544 /* When first_changed_pos is the end of a continued line,
16545 row is not unchanged because it may be no longer
16546 continued. */
16547 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16548 && (row->continued_p
16549 || row->exact_window_width_line_p)))
16550 row_found = row;
16551
16552 /* Stop if last visible row. */
16553 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16554 break;
16555 }
16556
16557 return row_found;
16558 }
16559
16560
16561 /* Find the first glyph row in the current matrix of W that is not
16562 affected by changes at the end of current_buffer since the
16563 time W's current matrix was built.
16564
16565 Return in *DELTA the number of chars by which buffer positions in
16566 unchanged text at the end of current_buffer must be adjusted.
16567
16568 Return in *DELTA_BYTES the corresponding number of bytes.
16569
16570 Value is null if no such row exists, i.e. all rows are affected by
16571 changes. */
16572
16573 static struct glyph_row *
16574 find_first_unchanged_at_end_row (struct window *w,
16575 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16576 {
16577 struct glyph_row *row;
16578 struct glyph_row *row_found = NULL;
16579
16580 *delta = *delta_bytes = 0;
16581
16582 /* Display must not have been paused, otherwise the current matrix
16583 is not up to date. */
16584 eassert (!NILP (w->window_end_valid));
16585
16586 /* A value of window_end_pos >= END_UNCHANGED means that the window
16587 end is in the range of changed text. If so, there is no
16588 unchanged row at the end of W's current matrix. */
16589 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16590 return NULL;
16591
16592 /* Set row to the last row in W's current matrix displaying text. */
16593 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16594
16595 /* If matrix is entirely empty, no unchanged row exists. */
16596 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16597 {
16598 /* The value of row is the last glyph row in the matrix having a
16599 meaningful buffer position in it. The end position of row
16600 corresponds to window_end_pos. This allows us to translate
16601 buffer positions in the current matrix to current buffer
16602 positions for characters not in changed text. */
16603 ptrdiff_t Z_old =
16604 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16605 ptrdiff_t Z_BYTE_old =
16606 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16607 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16608 struct glyph_row *first_text_row
16609 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16610
16611 *delta = Z - Z_old;
16612 *delta_bytes = Z_BYTE - Z_BYTE_old;
16613
16614 /* Set last_unchanged_pos to the buffer position of the last
16615 character in the buffer that has not been changed. Z is the
16616 index + 1 of the last character in current_buffer, i.e. by
16617 subtracting END_UNCHANGED we get the index of the last
16618 unchanged character, and we have to add BEG to get its buffer
16619 position. */
16620 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16621 last_unchanged_pos_old = last_unchanged_pos - *delta;
16622
16623 /* Search backward from ROW for a row displaying a line that
16624 starts at a minimum position >= last_unchanged_pos_old. */
16625 for (; row > first_text_row; --row)
16626 {
16627 /* This used to abort, but it can happen.
16628 It is ok to just stop the search instead here. KFS. */
16629 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16630 break;
16631
16632 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16633 row_found = row;
16634 }
16635 }
16636
16637 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16638
16639 return row_found;
16640 }
16641
16642
16643 /* Make sure that glyph rows in the current matrix of window W
16644 reference the same glyph memory as corresponding rows in the
16645 frame's frame matrix. This function is called after scrolling W's
16646 current matrix on a terminal frame in try_window_id and
16647 try_window_reusing_current_matrix. */
16648
16649 static void
16650 sync_frame_with_window_matrix_rows (struct window *w)
16651 {
16652 struct frame *f = XFRAME (w->frame);
16653 struct glyph_row *window_row, *window_row_end, *frame_row;
16654
16655 /* Preconditions: W must be a leaf window and full-width. Its frame
16656 must have a frame matrix. */
16657 xassert (NILP (w->hchild) && NILP (w->vchild));
16658 xassert (WINDOW_FULL_WIDTH_P (w));
16659 xassert (!FRAME_WINDOW_P (f));
16660
16661 /* If W is a full-width window, glyph pointers in W's current matrix
16662 have, by definition, to be the same as glyph pointers in the
16663 corresponding frame matrix. Note that frame matrices have no
16664 marginal areas (see build_frame_matrix). */
16665 window_row = w->current_matrix->rows;
16666 window_row_end = window_row + w->current_matrix->nrows;
16667 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16668 while (window_row < window_row_end)
16669 {
16670 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16671 struct glyph *end = window_row->glyphs[LAST_AREA];
16672
16673 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16674 frame_row->glyphs[TEXT_AREA] = start;
16675 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16676 frame_row->glyphs[LAST_AREA] = end;
16677
16678 /* Disable frame rows whose corresponding window rows have
16679 been disabled in try_window_id. */
16680 if (!window_row->enabled_p)
16681 frame_row->enabled_p = 0;
16682
16683 ++window_row, ++frame_row;
16684 }
16685 }
16686
16687
16688 /* Find the glyph row in window W containing CHARPOS. Consider all
16689 rows between START and END (not inclusive). END null means search
16690 all rows to the end of the display area of W. Value is the row
16691 containing CHARPOS or null. */
16692
16693 struct glyph_row *
16694 row_containing_pos (struct window *w, ptrdiff_t charpos,
16695 struct glyph_row *start, struct glyph_row *end, int dy)
16696 {
16697 struct glyph_row *row = start;
16698 struct glyph_row *best_row = NULL;
16699 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16700 int last_y;
16701
16702 /* If we happen to start on a header-line, skip that. */
16703 if (row->mode_line_p)
16704 ++row;
16705
16706 if ((end && row >= end) || !row->enabled_p)
16707 return NULL;
16708
16709 last_y = window_text_bottom_y (w) - dy;
16710
16711 while (1)
16712 {
16713 /* Give up if we have gone too far. */
16714 if (end && row >= end)
16715 return NULL;
16716 /* This formerly returned if they were equal.
16717 I think that both quantities are of a "last plus one" type;
16718 if so, when they are equal, the row is within the screen. -- rms. */
16719 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16720 return NULL;
16721
16722 /* If it is in this row, return this row. */
16723 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16724 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16725 /* The end position of a row equals the start
16726 position of the next row. If CHARPOS is there, we
16727 would rather display it in the next line, except
16728 when this line ends in ZV. */
16729 && !row->ends_at_zv_p
16730 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16731 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16732 {
16733 struct glyph *g;
16734
16735 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16736 || (!best_row && !row->continued_p))
16737 return row;
16738 /* In bidi-reordered rows, there could be several rows
16739 occluding point, all of them belonging to the same
16740 continued line. We need to find the row which fits
16741 CHARPOS the best. */
16742 for (g = row->glyphs[TEXT_AREA];
16743 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16744 g++)
16745 {
16746 if (!STRINGP (g->object))
16747 {
16748 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16749 {
16750 mindif = eabs (g->charpos - charpos);
16751 best_row = row;
16752 /* Exact match always wins. */
16753 if (mindif == 0)
16754 return best_row;
16755 }
16756 }
16757 }
16758 }
16759 else if (best_row && !row->continued_p)
16760 return best_row;
16761 ++row;
16762 }
16763 }
16764
16765
16766 /* Try to redisplay window W by reusing its existing display. W's
16767 current matrix must be up to date when this function is called,
16768 i.e. window_end_valid must not be nil.
16769
16770 Value is
16771
16772 1 if display has been updated
16773 0 if otherwise unsuccessful
16774 -1 if redisplay with same window start is known not to succeed
16775
16776 The following steps are performed:
16777
16778 1. Find the last row in the current matrix of W that is not
16779 affected by changes at the start of current_buffer. If no such row
16780 is found, give up.
16781
16782 2. Find the first row in W's current matrix that is not affected by
16783 changes at the end of current_buffer. Maybe there is no such row.
16784
16785 3. Display lines beginning with the row + 1 found in step 1 to the
16786 row found in step 2 or, if step 2 didn't find a row, to the end of
16787 the window.
16788
16789 4. If cursor is not known to appear on the window, give up.
16790
16791 5. If display stopped at the row found in step 2, scroll the
16792 display and current matrix as needed.
16793
16794 6. Maybe display some lines at the end of W, if we must. This can
16795 happen under various circumstances, like a partially visible line
16796 becoming fully visible, or because newly displayed lines are displayed
16797 in smaller font sizes.
16798
16799 7. Update W's window end information. */
16800
16801 static int
16802 try_window_id (struct window *w)
16803 {
16804 struct frame *f = XFRAME (w->frame);
16805 struct glyph_matrix *current_matrix = w->current_matrix;
16806 struct glyph_matrix *desired_matrix = w->desired_matrix;
16807 struct glyph_row *last_unchanged_at_beg_row;
16808 struct glyph_row *first_unchanged_at_end_row;
16809 struct glyph_row *row;
16810 struct glyph_row *bottom_row;
16811 int bottom_vpos;
16812 struct it it;
16813 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16814 int dvpos, dy;
16815 struct text_pos start_pos;
16816 struct run run;
16817 int first_unchanged_at_end_vpos = 0;
16818 struct glyph_row *last_text_row, *last_text_row_at_end;
16819 struct text_pos start;
16820 ptrdiff_t first_changed_charpos, last_changed_charpos;
16821
16822 #if GLYPH_DEBUG
16823 if (inhibit_try_window_id)
16824 return 0;
16825 #endif
16826
16827 /* This is handy for debugging. */
16828 #if 0
16829 #define GIVE_UP(X) \
16830 do { \
16831 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16832 return 0; \
16833 } while (0)
16834 #else
16835 #define GIVE_UP(X) return 0
16836 #endif
16837
16838 SET_TEXT_POS_FROM_MARKER (start, w->start);
16839
16840 /* Don't use this for mini-windows because these can show
16841 messages and mini-buffers, and we don't handle that here. */
16842 if (MINI_WINDOW_P (w))
16843 GIVE_UP (1);
16844
16845 /* This flag is used to prevent redisplay optimizations. */
16846 if (windows_or_buffers_changed || cursor_type_changed)
16847 GIVE_UP (2);
16848
16849 /* Verify that narrowing has not changed.
16850 Also verify that we were not told to prevent redisplay optimizations.
16851 It would be nice to further
16852 reduce the number of cases where this prevents try_window_id. */
16853 if (current_buffer->clip_changed
16854 || current_buffer->prevent_redisplay_optimizations_p)
16855 GIVE_UP (3);
16856
16857 /* Window must either use window-based redisplay or be full width. */
16858 if (!FRAME_WINDOW_P (f)
16859 && (!FRAME_LINE_INS_DEL_OK (f)
16860 || !WINDOW_FULL_WIDTH_P (w)))
16861 GIVE_UP (4);
16862
16863 /* Give up if point is known NOT to appear in W. */
16864 if (PT < CHARPOS (start))
16865 GIVE_UP (5);
16866
16867 /* Another way to prevent redisplay optimizations. */
16868 if (XFASTINT (w->last_modified) == 0)
16869 GIVE_UP (6);
16870
16871 /* Verify that window is not hscrolled. */
16872 if (XFASTINT (w->hscroll) != 0)
16873 GIVE_UP (7);
16874
16875 /* Verify that display wasn't paused. */
16876 if (NILP (w->window_end_valid))
16877 GIVE_UP (8);
16878
16879 /* Can't use this if highlighting a region because a cursor movement
16880 will do more than just set the cursor. */
16881 if (!NILP (Vtransient_mark_mode)
16882 && !NILP (BVAR (current_buffer, mark_active)))
16883 GIVE_UP (9);
16884
16885 /* Likewise if highlighting trailing whitespace. */
16886 if (!NILP (Vshow_trailing_whitespace))
16887 GIVE_UP (11);
16888
16889 /* Likewise if showing a region. */
16890 if (!NILP (w->region_showing))
16891 GIVE_UP (10);
16892
16893 /* Can't use this if overlay arrow position and/or string have
16894 changed. */
16895 if (overlay_arrows_changed_p ())
16896 GIVE_UP (12);
16897
16898 /* When word-wrap is on, adding a space to the first word of a
16899 wrapped line can change the wrap position, altering the line
16900 above it. It might be worthwhile to handle this more
16901 intelligently, but for now just redisplay from scratch. */
16902 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16903 GIVE_UP (21);
16904
16905 /* Under bidi reordering, adding or deleting a character in the
16906 beginning of a paragraph, before the first strong directional
16907 character, can change the base direction of the paragraph (unless
16908 the buffer specifies a fixed paragraph direction), which will
16909 require to redisplay the whole paragraph. It might be worthwhile
16910 to find the paragraph limits and widen the range of redisplayed
16911 lines to that, but for now just give up this optimization and
16912 redisplay from scratch. */
16913 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16914 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16915 GIVE_UP (22);
16916
16917 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16918 only if buffer has really changed. The reason is that the gap is
16919 initially at Z for freshly visited files. The code below would
16920 set end_unchanged to 0 in that case. */
16921 if (MODIFF > SAVE_MODIFF
16922 /* This seems to happen sometimes after saving a buffer. */
16923 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16924 {
16925 if (GPT - BEG < BEG_UNCHANGED)
16926 BEG_UNCHANGED = GPT - BEG;
16927 if (Z - GPT < END_UNCHANGED)
16928 END_UNCHANGED = Z - GPT;
16929 }
16930
16931 /* The position of the first and last character that has been changed. */
16932 first_changed_charpos = BEG + BEG_UNCHANGED;
16933 last_changed_charpos = Z - END_UNCHANGED;
16934
16935 /* If window starts after a line end, and the last change is in
16936 front of that newline, then changes don't affect the display.
16937 This case happens with stealth-fontification. Note that although
16938 the display is unchanged, glyph positions in the matrix have to
16939 be adjusted, of course. */
16940 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16941 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16942 && ((last_changed_charpos < CHARPOS (start)
16943 && CHARPOS (start) == BEGV)
16944 || (last_changed_charpos < CHARPOS (start) - 1
16945 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16946 {
16947 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16948 struct glyph_row *r0;
16949
16950 /* Compute how many chars/bytes have been added to or removed
16951 from the buffer. */
16952 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16953 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16954 Z_delta = Z - Z_old;
16955 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16956
16957 /* Give up if PT is not in the window. Note that it already has
16958 been checked at the start of try_window_id that PT is not in
16959 front of the window start. */
16960 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16961 GIVE_UP (13);
16962
16963 /* If window start is unchanged, we can reuse the whole matrix
16964 as is, after adjusting glyph positions. No need to compute
16965 the window end again, since its offset from Z hasn't changed. */
16966 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16967 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16968 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16969 /* PT must not be in a partially visible line. */
16970 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16971 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16972 {
16973 /* Adjust positions in the glyph matrix. */
16974 if (Z_delta || Z_delta_bytes)
16975 {
16976 struct glyph_row *r1
16977 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16978 increment_matrix_positions (w->current_matrix,
16979 MATRIX_ROW_VPOS (r0, current_matrix),
16980 MATRIX_ROW_VPOS (r1, current_matrix),
16981 Z_delta, Z_delta_bytes);
16982 }
16983
16984 /* Set the cursor. */
16985 row = row_containing_pos (w, PT, r0, NULL, 0);
16986 if (row)
16987 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16988 else
16989 abort ();
16990 return 1;
16991 }
16992 }
16993
16994 /* Handle the case that changes are all below what is displayed in
16995 the window, and that PT is in the window. This shortcut cannot
16996 be taken if ZV is visible in the window, and text has been added
16997 there that is visible in the window. */
16998 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16999 /* ZV is not visible in the window, or there are no
17000 changes at ZV, actually. */
17001 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17002 || first_changed_charpos == last_changed_charpos))
17003 {
17004 struct glyph_row *r0;
17005
17006 /* Give up if PT is not in the window. Note that it already has
17007 been checked at the start of try_window_id that PT is not in
17008 front of the window start. */
17009 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17010 GIVE_UP (14);
17011
17012 /* If window start is unchanged, we can reuse the whole matrix
17013 as is, without changing glyph positions since no text has
17014 been added/removed in front of the window end. */
17015 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17016 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17017 /* PT must not be in a partially visible line. */
17018 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17019 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17020 {
17021 /* We have to compute the window end anew since text
17022 could have been added/removed after it. */
17023 w->window_end_pos
17024 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17025 w->window_end_bytepos
17026 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17027
17028 /* Set the cursor. */
17029 row = row_containing_pos (w, PT, r0, NULL, 0);
17030 if (row)
17031 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17032 else
17033 abort ();
17034 return 2;
17035 }
17036 }
17037
17038 /* Give up if window start is in the changed area.
17039
17040 The condition used to read
17041
17042 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17043
17044 but why that was tested escapes me at the moment. */
17045 if (CHARPOS (start) >= first_changed_charpos
17046 && CHARPOS (start) <= last_changed_charpos)
17047 GIVE_UP (15);
17048
17049 /* Check that window start agrees with the start of the first glyph
17050 row in its current matrix. Check this after we know the window
17051 start is not in changed text, otherwise positions would not be
17052 comparable. */
17053 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17054 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17055 GIVE_UP (16);
17056
17057 /* Give up if the window ends in strings. Overlay strings
17058 at the end are difficult to handle, so don't try. */
17059 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17060 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17061 GIVE_UP (20);
17062
17063 /* Compute the position at which we have to start displaying new
17064 lines. Some of the lines at the top of the window might be
17065 reusable because they are not displaying changed text. Find the
17066 last row in W's current matrix not affected by changes at the
17067 start of current_buffer. Value is null if changes start in the
17068 first line of window. */
17069 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17070 if (last_unchanged_at_beg_row)
17071 {
17072 /* Avoid starting to display in the middle of a character, a TAB
17073 for instance. This is easier than to set up the iterator
17074 exactly, and it's not a frequent case, so the additional
17075 effort wouldn't really pay off. */
17076 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17077 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17078 && last_unchanged_at_beg_row > w->current_matrix->rows)
17079 --last_unchanged_at_beg_row;
17080
17081 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17082 GIVE_UP (17);
17083
17084 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17085 GIVE_UP (18);
17086 start_pos = it.current.pos;
17087
17088 /* Start displaying new lines in the desired matrix at the same
17089 vpos we would use in the current matrix, i.e. below
17090 last_unchanged_at_beg_row. */
17091 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17092 current_matrix);
17093 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17094 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17095
17096 xassert (it.hpos == 0 && it.current_x == 0);
17097 }
17098 else
17099 {
17100 /* There are no reusable lines at the start of the window.
17101 Start displaying in the first text line. */
17102 start_display (&it, w, start);
17103 it.vpos = it.first_vpos;
17104 start_pos = it.current.pos;
17105 }
17106
17107 /* Find the first row that is not affected by changes at the end of
17108 the buffer. Value will be null if there is no unchanged row, in
17109 which case we must redisplay to the end of the window. delta
17110 will be set to the value by which buffer positions beginning with
17111 first_unchanged_at_end_row have to be adjusted due to text
17112 changes. */
17113 first_unchanged_at_end_row
17114 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17115 IF_DEBUG (debug_delta = delta);
17116 IF_DEBUG (debug_delta_bytes = delta_bytes);
17117
17118 /* Set stop_pos to the buffer position up to which we will have to
17119 display new lines. If first_unchanged_at_end_row != NULL, this
17120 is the buffer position of the start of the line displayed in that
17121 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17122 that we don't stop at a buffer position. */
17123 stop_pos = 0;
17124 if (first_unchanged_at_end_row)
17125 {
17126 xassert (last_unchanged_at_beg_row == NULL
17127 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17128
17129 /* If this is a continuation line, move forward to the next one
17130 that isn't. Changes in lines above affect this line.
17131 Caution: this may move first_unchanged_at_end_row to a row
17132 not displaying text. */
17133 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17134 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17135 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17136 < it.last_visible_y))
17137 ++first_unchanged_at_end_row;
17138
17139 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17140 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17141 >= it.last_visible_y))
17142 first_unchanged_at_end_row = NULL;
17143 else
17144 {
17145 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17146 + delta);
17147 first_unchanged_at_end_vpos
17148 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17149 xassert (stop_pos >= Z - END_UNCHANGED);
17150 }
17151 }
17152 else if (last_unchanged_at_beg_row == NULL)
17153 GIVE_UP (19);
17154
17155
17156 #if GLYPH_DEBUG
17157
17158 /* Either there is no unchanged row at the end, or the one we have
17159 now displays text. This is a necessary condition for the window
17160 end pos calculation at the end of this function. */
17161 xassert (first_unchanged_at_end_row == NULL
17162 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17163
17164 debug_last_unchanged_at_beg_vpos
17165 = (last_unchanged_at_beg_row
17166 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17167 : -1);
17168 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17169
17170 #endif /* GLYPH_DEBUG != 0 */
17171
17172
17173 /* Display new lines. Set last_text_row to the last new line
17174 displayed which has text on it, i.e. might end up as being the
17175 line where the window_end_vpos is. */
17176 w->cursor.vpos = -1;
17177 last_text_row = NULL;
17178 overlay_arrow_seen = 0;
17179 while (it.current_y < it.last_visible_y
17180 && !fonts_changed_p
17181 && (first_unchanged_at_end_row == NULL
17182 || IT_CHARPOS (it) < stop_pos))
17183 {
17184 if (display_line (&it))
17185 last_text_row = it.glyph_row - 1;
17186 }
17187
17188 if (fonts_changed_p)
17189 return -1;
17190
17191
17192 /* Compute differences in buffer positions, y-positions etc. for
17193 lines reused at the bottom of the window. Compute what we can
17194 scroll. */
17195 if (first_unchanged_at_end_row
17196 /* No lines reused because we displayed everything up to the
17197 bottom of the window. */
17198 && it.current_y < it.last_visible_y)
17199 {
17200 dvpos = (it.vpos
17201 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17202 current_matrix));
17203 dy = it.current_y - first_unchanged_at_end_row->y;
17204 run.current_y = first_unchanged_at_end_row->y;
17205 run.desired_y = run.current_y + dy;
17206 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17207 }
17208 else
17209 {
17210 delta = delta_bytes = dvpos = dy
17211 = run.current_y = run.desired_y = run.height = 0;
17212 first_unchanged_at_end_row = NULL;
17213 }
17214 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17215
17216
17217 /* Find the cursor if not already found. We have to decide whether
17218 PT will appear on this window (it sometimes doesn't, but this is
17219 not a very frequent case.) This decision has to be made before
17220 the current matrix is altered. A value of cursor.vpos < 0 means
17221 that PT is either in one of the lines beginning at
17222 first_unchanged_at_end_row or below the window. Don't care for
17223 lines that might be displayed later at the window end; as
17224 mentioned, this is not a frequent case. */
17225 if (w->cursor.vpos < 0)
17226 {
17227 /* Cursor in unchanged rows at the top? */
17228 if (PT < CHARPOS (start_pos)
17229 && last_unchanged_at_beg_row)
17230 {
17231 row = row_containing_pos (w, PT,
17232 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17233 last_unchanged_at_beg_row + 1, 0);
17234 if (row)
17235 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17236 }
17237
17238 /* Start from first_unchanged_at_end_row looking for PT. */
17239 else if (first_unchanged_at_end_row)
17240 {
17241 row = row_containing_pos (w, PT - delta,
17242 first_unchanged_at_end_row, NULL, 0);
17243 if (row)
17244 set_cursor_from_row (w, row, w->current_matrix, delta,
17245 delta_bytes, dy, dvpos);
17246 }
17247
17248 /* Give up if cursor was not found. */
17249 if (w->cursor.vpos < 0)
17250 {
17251 clear_glyph_matrix (w->desired_matrix);
17252 return -1;
17253 }
17254 }
17255
17256 /* Don't let the cursor end in the scroll margins. */
17257 {
17258 int this_scroll_margin, cursor_height;
17259
17260 this_scroll_margin =
17261 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17262 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17263 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17264
17265 if ((w->cursor.y < this_scroll_margin
17266 && CHARPOS (start) > BEGV)
17267 /* Old redisplay didn't take scroll margin into account at the bottom,
17268 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17269 || (w->cursor.y + (make_cursor_line_fully_visible_p
17270 ? cursor_height + this_scroll_margin
17271 : 1)) > it.last_visible_y)
17272 {
17273 w->cursor.vpos = -1;
17274 clear_glyph_matrix (w->desired_matrix);
17275 return -1;
17276 }
17277 }
17278
17279 /* Scroll the display. Do it before changing the current matrix so
17280 that xterm.c doesn't get confused about where the cursor glyph is
17281 found. */
17282 if (dy && run.height)
17283 {
17284 update_begin (f);
17285
17286 if (FRAME_WINDOW_P (f))
17287 {
17288 FRAME_RIF (f)->update_window_begin_hook (w);
17289 FRAME_RIF (f)->clear_window_mouse_face (w);
17290 FRAME_RIF (f)->scroll_run_hook (w, &run);
17291 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17292 }
17293 else
17294 {
17295 /* Terminal frame. In this case, dvpos gives the number of
17296 lines to scroll by; dvpos < 0 means scroll up. */
17297 int from_vpos
17298 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17299 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17300 int end = (WINDOW_TOP_EDGE_LINE (w)
17301 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17302 + window_internal_height (w));
17303
17304 #if defined (HAVE_GPM) || defined (MSDOS)
17305 x_clear_window_mouse_face (w);
17306 #endif
17307 /* Perform the operation on the screen. */
17308 if (dvpos > 0)
17309 {
17310 /* Scroll last_unchanged_at_beg_row to the end of the
17311 window down dvpos lines. */
17312 set_terminal_window (f, end);
17313
17314 /* On dumb terminals delete dvpos lines at the end
17315 before inserting dvpos empty lines. */
17316 if (!FRAME_SCROLL_REGION_OK (f))
17317 ins_del_lines (f, end - dvpos, -dvpos);
17318
17319 /* Insert dvpos empty lines in front of
17320 last_unchanged_at_beg_row. */
17321 ins_del_lines (f, from, dvpos);
17322 }
17323 else if (dvpos < 0)
17324 {
17325 /* Scroll up last_unchanged_at_beg_vpos to the end of
17326 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17327 set_terminal_window (f, end);
17328
17329 /* Delete dvpos lines in front of
17330 last_unchanged_at_beg_vpos. ins_del_lines will set
17331 the cursor to the given vpos and emit |dvpos| delete
17332 line sequences. */
17333 ins_del_lines (f, from + dvpos, dvpos);
17334
17335 /* On a dumb terminal insert dvpos empty lines at the
17336 end. */
17337 if (!FRAME_SCROLL_REGION_OK (f))
17338 ins_del_lines (f, end + dvpos, -dvpos);
17339 }
17340
17341 set_terminal_window (f, 0);
17342 }
17343
17344 update_end (f);
17345 }
17346
17347 /* Shift reused rows of the current matrix to the right position.
17348 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17349 text. */
17350 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17351 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17352 if (dvpos < 0)
17353 {
17354 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17355 bottom_vpos, dvpos);
17356 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17357 bottom_vpos, 0);
17358 }
17359 else if (dvpos > 0)
17360 {
17361 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17362 bottom_vpos, dvpos);
17363 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17364 first_unchanged_at_end_vpos + dvpos, 0);
17365 }
17366
17367 /* For frame-based redisplay, make sure that current frame and window
17368 matrix are in sync with respect to glyph memory. */
17369 if (!FRAME_WINDOW_P (f))
17370 sync_frame_with_window_matrix_rows (w);
17371
17372 /* Adjust buffer positions in reused rows. */
17373 if (delta || delta_bytes)
17374 increment_matrix_positions (current_matrix,
17375 first_unchanged_at_end_vpos + dvpos,
17376 bottom_vpos, delta, delta_bytes);
17377
17378 /* Adjust Y positions. */
17379 if (dy)
17380 shift_glyph_matrix (w, current_matrix,
17381 first_unchanged_at_end_vpos + dvpos,
17382 bottom_vpos, dy);
17383
17384 if (first_unchanged_at_end_row)
17385 {
17386 first_unchanged_at_end_row += dvpos;
17387 if (first_unchanged_at_end_row->y >= it.last_visible_y
17388 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17389 first_unchanged_at_end_row = NULL;
17390 }
17391
17392 /* If scrolling up, there may be some lines to display at the end of
17393 the window. */
17394 last_text_row_at_end = NULL;
17395 if (dy < 0)
17396 {
17397 /* Scrolling up can leave for example a partially visible line
17398 at the end of the window to be redisplayed. */
17399 /* Set last_row to the glyph row in the current matrix where the
17400 window end line is found. It has been moved up or down in
17401 the matrix by dvpos. */
17402 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17403 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17404
17405 /* If last_row is the window end line, it should display text. */
17406 xassert (last_row->displays_text_p);
17407
17408 /* If window end line was partially visible before, begin
17409 displaying at that line. Otherwise begin displaying with the
17410 line following it. */
17411 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17412 {
17413 init_to_row_start (&it, w, last_row);
17414 it.vpos = last_vpos;
17415 it.current_y = last_row->y;
17416 }
17417 else
17418 {
17419 init_to_row_end (&it, w, last_row);
17420 it.vpos = 1 + last_vpos;
17421 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17422 ++last_row;
17423 }
17424
17425 /* We may start in a continuation line. If so, we have to
17426 get the right continuation_lines_width and current_x. */
17427 it.continuation_lines_width = last_row->continuation_lines_width;
17428 it.hpos = it.current_x = 0;
17429
17430 /* Display the rest of the lines at the window end. */
17431 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17432 while (it.current_y < it.last_visible_y
17433 && !fonts_changed_p)
17434 {
17435 /* Is it always sure that the display agrees with lines in
17436 the current matrix? I don't think so, so we mark rows
17437 displayed invalid in the current matrix by setting their
17438 enabled_p flag to zero. */
17439 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17440 if (display_line (&it))
17441 last_text_row_at_end = it.glyph_row - 1;
17442 }
17443 }
17444
17445 /* Update window_end_pos and window_end_vpos. */
17446 if (first_unchanged_at_end_row
17447 && !last_text_row_at_end)
17448 {
17449 /* Window end line if one of the preserved rows from the current
17450 matrix. Set row to the last row displaying text in current
17451 matrix starting at first_unchanged_at_end_row, after
17452 scrolling. */
17453 xassert (first_unchanged_at_end_row->displays_text_p);
17454 row = find_last_row_displaying_text (w->current_matrix, &it,
17455 first_unchanged_at_end_row);
17456 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17457
17458 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17459 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17460 w->window_end_vpos
17461 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17462 xassert (w->window_end_bytepos >= 0);
17463 IF_DEBUG (debug_method_add (w, "A"));
17464 }
17465 else if (last_text_row_at_end)
17466 {
17467 w->window_end_pos
17468 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17469 w->window_end_bytepos
17470 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17471 w->window_end_vpos
17472 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17473 xassert (w->window_end_bytepos >= 0);
17474 IF_DEBUG (debug_method_add (w, "B"));
17475 }
17476 else if (last_text_row)
17477 {
17478 /* We have displayed either to the end of the window or at the
17479 end of the window, i.e. the last row with text is to be found
17480 in the desired matrix. */
17481 w->window_end_pos
17482 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17483 w->window_end_bytepos
17484 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17485 w->window_end_vpos
17486 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17487 xassert (w->window_end_bytepos >= 0);
17488 }
17489 else if (first_unchanged_at_end_row == NULL
17490 && last_text_row == NULL
17491 && last_text_row_at_end == NULL)
17492 {
17493 /* Displayed to end of window, but no line containing text was
17494 displayed. Lines were deleted at the end of the window. */
17495 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17496 int vpos = XFASTINT (w->window_end_vpos);
17497 struct glyph_row *current_row = current_matrix->rows + vpos;
17498 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17499
17500 for (row = NULL;
17501 row == NULL && vpos >= first_vpos;
17502 --vpos, --current_row, --desired_row)
17503 {
17504 if (desired_row->enabled_p)
17505 {
17506 if (desired_row->displays_text_p)
17507 row = desired_row;
17508 }
17509 else if (current_row->displays_text_p)
17510 row = current_row;
17511 }
17512
17513 xassert (row != NULL);
17514 w->window_end_vpos = make_number (vpos + 1);
17515 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17516 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17517 xassert (w->window_end_bytepos >= 0);
17518 IF_DEBUG (debug_method_add (w, "C"));
17519 }
17520 else
17521 abort ();
17522
17523 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17524 debug_end_vpos = XFASTINT (w->window_end_vpos));
17525
17526 /* Record that display has not been completed. */
17527 w->window_end_valid = Qnil;
17528 w->desired_matrix->no_scrolling_p = 1;
17529 return 3;
17530
17531 #undef GIVE_UP
17532 }
17533
17534
17535 \f
17536 /***********************************************************************
17537 More debugging support
17538 ***********************************************************************/
17539
17540 #if GLYPH_DEBUG
17541
17542 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17543 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17544 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17545
17546
17547 /* Dump the contents of glyph matrix MATRIX on stderr.
17548
17549 GLYPHS 0 means don't show glyph contents.
17550 GLYPHS 1 means show glyphs in short form
17551 GLYPHS > 1 means show glyphs in long form. */
17552
17553 void
17554 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17555 {
17556 int i;
17557 for (i = 0; i < matrix->nrows; ++i)
17558 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17559 }
17560
17561
17562 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17563 the glyph row and area where the glyph comes from. */
17564
17565 void
17566 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17567 {
17568 if (glyph->type == CHAR_GLYPH)
17569 {
17570 fprintf (stderr,
17571 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17572 glyph - row->glyphs[TEXT_AREA],
17573 'C',
17574 glyph->charpos,
17575 (BUFFERP (glyph->object)
17576 ? 'B'
17577 : (STRINGP (glyph->object)
17578 ? 'S'
17579 : '-')),
17580 glyph->pixel_width,
17581 glyph->u.ch,
17582 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17583 ? glyph->u.ch
17584 : '.'),
17585 glyph->face_id,
17586 glyph->left_box_line_p,
17587 glyph->right_box_line_p);
17588 }
17589 else if (glyph->type == STRETCH_GLYPH)
17590 {
17591 fprintf (stderr,
17592 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17593 glyph - row->glyphs[TEXT_AREA],
17594 'S',
17595 glyph->charpos,
17596 (BUFFERP (glyph->object)
17597 ? 'B'
17598 : (STRINGP (glyph->object)
17599 ? 'S'
17600 : '-')),
17601 glyph->pixel_width,
17602 0,
17603 '.',
17604 glyph->face_id,
17605 glyph->left_box_line_p,
17606 glyph->right_box_line_p);
17607 }
17608 else if (glyph->type == IMAGE_GLYPH)
17609 {
17610 fprintf (stderr,
17611 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17612 glyph - row->glyphs[TEXT_AREA],
17613 'I',
17614 glyph->charpos,
17615 (BUFFERP (glyph->object)
17616 ? 'B'
17617 : (STRINGP (glyph->object)
17618 ? 'S'
17619 : '-')),
17620 glyph->pixel_width,
17621 glyph->u.img_id,
17622 '.',
17623 glyph->face_id,
17624 glyph->left_box_line_p,
17625 glyph->right_box_line_p);
17626 }
17627 else if (glyph->type == COMPOSITE_GLYPH)
17628 {
17629 fprintf (stderr,
17630 " %5td %4c %6"pI"d %c %3d 0x%05x",
17631 glyph - row->glyphs[TEXT_AREA],
17632 '+',
17633 glyph->charpos,
17634 (BUFFERP (glyph->object)
17635 ? 'B'
17636 : (STRINGP (glyph->object)
17637 ? 'S'
17638 : '-')),
17639 glyph->pixel_width,
17640 glyph->u.cmp.id);
17641 if (glyph->u.cmp.automatic)
17642 fprintf (stderr,
17643 "[%d-%d]",
17644 glyph->slice.cmp.from, glyph->slice.cmp.to);
17645 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17646 glyph->face_id,
17647 glyph->left_box_line_p,
17648 glyph->right_box_line_p);
17649 }
17650 }
17651
17652
17653 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17654 GLYPHS 0 means don't show glyph contents.
17655 GLYPHS 1 means show glyphs in short form
17656 GLYPHS > 1 means show glyphs in long form. */
17657
17658 void
17659 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17660 {
17661 if (glyphs != 1)
17662 {
17663 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17664 fprintf (stderr, "======================================================================\n");
17665
17666 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17667 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17668 vpos,
17669 MATRIX_ROW_START_CHARPOS (row),
17670 MATRIX_ROW_END_CHARPOS (row),
17671 row->used[TEXT_AREA],
17672 row->contains_overlapping_glyphs_p,
17673 row->enabled_p,
17674 row->truncated_on_left_p,
17675 row->truncated_on_right_p,
17676 row->continued_p,
17677 MATRIX_ROW_CONTINUATION_LINE_P (row),
17678 row->displays_text_p,
17679 row->ends_at_zv_p,
17680 row->fill_line_p,
17681 row->ends_in_middle_of_char_p,
17682 row->starts_in_middle_of_char_p,
17683 row->mouse_face_p,
17684 row->x,
17685 row->y,
17686 row->pixel_width,
17687 row->height,
17688 row->visible_height,
17689 row->ascent,
17690 row->phys_ascent);
17691 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17692 row->end.overlay_string_index,
17693 row->continuation_lines_width);
17694 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17695 CHARPOS (row->start.string_pos),
17696 CHARPOS (row->end.string_pos));
17697 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17698 row->end.dpvec_index);
17699 }
17700
17701 if (glyphs > 1)
17702 {
17703 int area;
17704
17705 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17706 {
17707 struct glyph *glyph = row->glyphs[area];
17708 struct glyph *glyph_end = glyph + row->used[area];
17709
17710 /* Glyph for a line end in text. */
17711 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17712 ++glyph_end;
17713
17714 if (glyph < glyph_end)
17715 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17716
17717 for (; glyph < glyph_end; ++glyph)
17718 dump_glyph (row, glyph, area);
17719 }
17720 }
17721 else if (glyphs == 1)
17722 {
17723 int area;
17724
17725 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17726 {
17727 char *s = (char *) alloca (row->used[area] + 1);
17728 int i;
17729
17730 for (i = 0; i < row->used[area]; ++i)
17731 {
17732 struct glyph *glyph = row->glyphs[area] + i;
17733 if (glyph->type == CHAR_GLYPH
17734 && glyph->u.ch < 0x80
17735 && glyph->u.ch >= ' ')
17736 s[i] = glyph->u.ch;
17737 else
17738 s[i] = '.';
17739 }
17740
17741 s[i] = '\0';
17742 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17743 }
17744 }
17745 }
17746
17747
17748 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17749 Sdump_glyph_matrix, 0, 1, "p",
17750 doc: /* Dump the current matrix of the selected window to stderr.
17751 Shows contents of glyph row structures. With non-nil
17752 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17753 glyphs in short form, otherwise show glyphs in long form. */)
17754 (Lisp_Object glyphs)
17755 {
17756 struct window *w = XWINDOW (selected_window);
17757 struct buffer *buffer = XBUFFER (w->buffer);
17758
17759 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17760 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17761 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17762 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17763 fprintf (stderr, "=============================================\n");
17764 dump_glyph_matrix (w->current_matrix,
17765 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17766 return Qnil;
17767 }
17768
17769
17770 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17771 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17772 (void)
17773 {
17774 struct frame *f = XFRAME (selected_frame);
17775 dump_glyph_matrix (f->current_matrix, 1);
17776 return Qnil;
17777 }
17778
17779
17780 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17781 doc: /* Dump glyph row ROW to stderr.
17782 GLYPH 0 means don't dump glyphs.
17783 GLYPH 1 means dump glyphs in short form.
17784 GLYPH > 1 or omitted means dump glyphs in long form. */)
17785 (Lisp_Object row, Lisp_Object glyphs)
17786 {
17787 struct glyph_matrix *matrix;
17788 EMACS_INT vpos;
17789
17790 CHECK_NUMBER (row);
17791 matrix = XWINDOW (selected_window)->current_matrix;
17792 vpos = XINT (row);
17793 if (vpos >= 0 && vpos < matrix->nrows)
17794 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17795 vpos,
17796 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17797 return Qnil;
17798 }
17799
17800
17801 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17802 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17803 GLYPH 0 means don't dump glyphs.
17804 GLYPH 1 means dump glyphs in short form.
17805 GLYPH > 1 or omitted means dump glyphs in long form. */)
17806 (Lisp_Object row, Lisp_Object glyphs)
17807 {
17808 struct frame *sf = SELECTED_FRAME ();
17809 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17810 EMACS_INT vpos;
17811
17812 CHECK_NUMBER (row);
17813 vpos = XINT (row);
17814 if (vpos >= 0 && vpos < m->nrows)
17815 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17816 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17817 return Qnil;
17818 }
17819
17820
17821 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17822 doc: /* Toggle tracing of redisplay.
17823 With ARG, turn tracing on if and only if ARG is positive. */)
17824 (Lisp_Object arg)
17825 {
17826 if (NILP (arg))
17827 trace_redisplay_p = !trace_redisplay_p;
17828 else
17829 {
17830 arg = Fprefix_numeric_value (arg);
17831 trace_redisplay_p = XINT (arg) > 0;
17832 }
17833
17834 return Qnil;
17835 }
17836
17837
17838 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17839 doc: /* Like `format', but print result to stderr.
17840 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17841 (ptrdiff_t nargs, Lisp_Object *args)
17842 {
17843 Lisp_Object s = Fformat (nargs, args);
17844 fprintf (stderr, "%s", SDATA (s));
17845 return Qnil;
17846 }
17847
17848 #endif /* GLYPH_DEBUG */
17849
17850
17851 \f
17852 /***********************************************************************
17853 Building Desired Matrix Rows
17854 ***********************************************************************/
17855
17856 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17857 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17858
17859 static struct glyph_row *
17860 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17861 {
17862 struct frame *f = XFRAME (WINDOW_FRAME (w));
17863 struct buffer *buffer = XBUFFER (w->buffer);
17864 struct buffer *old = current_buffer;
17865 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17866 int arrow_len = SCHARS (overlay_arrow_string);
17867 const unsigned char *arrow_end = arrow_string + arrow_len;
17868 const unsigned char *p;
17869 struct it it;
17870 int multibyte_p;
17871 int n_glyphs_before;
17872
17873 set_buffer_temp (buffer);
17874 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17875 it.glyph_row->used[TEXT_AREA] = 0;
17876 SET_TEXT_POS (it.position, 0, 0);
17877
17878 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17879 p = arrow_string;
17880 while (p < arrow_end)
17881 {
17882 Lisp_Object face, ilisp;
17883
17884 /* Get the next character. */
17885 if (multibyte_p)
17886 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17887 else
17888 {
17889 it.c = it.char_to_display = *p, it.len = 1;
17890 if (! ASCII_CHAR_P (it.c))
17891 it.char_to_display = BYTE8_TO_CHAR (it.c);
17892 }
17893 p += it.len;
17894
17895 /* Get its face. */
17896 ilisp = make_number (p - arrow_string);
17897 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17898 it.face_id = compute_char_face (f, it.char_to_display, face);
17899
17900 /* Compute its width, get its glyphs. */
17901 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17902 SET_TEXT_POS (it.position, -1, -1);
17903 PRODUCE_GLYPHS (&it);
17904
17905 /* If this character doesn't fit any more in the line, we have
17906 to remove some glyphs. */
17907 if (it.current_x > it.last_visible_x)
17908 {
17909 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17910 break;
17911 }
17912 }
17913
17914 set_buffer_temp (old);
17915 return it.glyph_row;
17916 }
17917
17918
17919 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17920 glyphs are only inserted for terminal frames since we can't really
17921 win with truncation glyphs when partially visible glyphs are
17922 involved. Which glyphs to insert is determined by
17923 produce_special_glyphs. */
17924
17925 static void
17926 insert_left_trunc_glyphs (struct it *it)
17927 {
17928 struct it truncate_it;
17929 struct glyph *from, *end, *to, *toend;
17930
17931 xassert (!FRAME_WINDOW_P (it->f));
17932
17933 /* Get the truncation glyphs. */
17934 truncate_it = *it;
17935 truncate_it.current_x = 0;
17936 truncate_it.face_id = DEFAULT_FACE_ID;
17937 truncate_it.glyph_row = &scratch_glyph_row;
17938 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17939 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17940 truncate_it.object = make_number (0);
17941 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17942
17943 /* Overwrite glyphs from IT with truncation glyphs. */
17944 if (!it->glyph_row->reversed_p)
17945 {
17946 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17947 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17948 to = it->glyph_row->glyphs[TEXT_AREA];
17949 toend = to + it->glyph_row->used[TEXT_AREA];
17950
17951 while (from < end)
17952 *to++ = *from++;
17953
17954 /* There may be padding glyphs left over. Overwrite them too. */
17955 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17956 {
17957 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17958 while (from < end)
17959 *to++ = *from++;
17960 }
17961
17962 if (to > toend)
17963 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17964 }
17965 else
17966 {
17967 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17968 that back to front. */
17969 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17970 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17971 toend = it->glyph_row->glyphs[TEXT_AREA];
17972 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17973
17974 while (from >= end && to >= toend)
17975 *to-- = *from--;
17976 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17977 {
17978 from =
17979 truncate_it.glyph_row->glyphs[TEXT_AREA]
17980 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17981 while (from >= end && to >= toend)
17982 *to-- = *from--;
17983 }
17984 if (from >= end)
17985 {
17986 /* Need to free some room before prepending additional
17987 glyphs. */
17988 int move_by = from - end + 1;
17989 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17990 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17991
17992 for ( ; g >= g0; g--)
17993 g[move_by] = *g;
17994 while (from >= end)
17995 *to-- = *from--;
17996 it->glyph_row->used[TEXT_AREA] += move_by;
17997 }
17998 }
17999 }
18000
18001 /* Compute the hash code for ROW. */
18002 unsigned
18003 row_hash (struct glyph_row *row)
18004 {
18005 int area, k;
18006 unsigned hashval = 0;
18007
18008 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18009 for (k = 0; k < row->used[area]; ++k)
18010 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18011 + row->glyphs[area][k].u.val
18012 + row->glyphs[area][k].face_id
18013 + row->glyphs[area][k].padding_p
18014 + (row->glyphs[area][k].type << 2));
18015
18016 return hashval;
18017 }
18018
18019 /* Compute the pixel height and width of IT->glyph_row.
18020
18021 Most of the time, ascent and height of a display line will be equal
18022 to the max_ascent and max_height values of the display iterator
18023 structure. This is not the case if
18024
18025 1. We hit ZV without displaying anything. In this case, max_ascent
18026 and max_height will be zero.
18027
18028 2. We have some glyphs that don't contribute to the line height.
18029 (The glyph row flag contributes_to_line_height_p is for future
18030 pixmap extensions).
18031
18032 The first case is easily covered by using default values because in
18033 these cases, the line height does not really matter, except that it
18034 must not be zero. */
18035
18036 static void
18037 compute_line_metrics (struct it *it)
18038 {
18039 struct glyph_row *row = it->glyph_row;
18040
18041 if (FRAME_WINDOW_P (it->f))
18042 {
18043 int i, min_y, max_y;
18044
18045 /* The line may consist of one space only, that was added to
18046 place the cursor on it. If so, the row's height hasn't been
18047 computed yet. */
18048 if (row->height == 0)
18049 {
18050 if (it->max_ascent + it->max_descent == 0)
18051 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18052 row->ascent = it->max_ascent;
18053 row->height = it->max_ascent + it->max_descent;
18054 row->phys_ascent = it->max_phys_ascent;
18055 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18056 row->extra_line_spacing = it->max_extra_line_spacing;
18057 }
18058
18059 /* Compute the width of this line. */
18060 row->pixel_width = row->x;
18061 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18062 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18063
18064 xassert (row->pixel_width >= 0);
18065 xassert (row->ascent >= 0 && row->height > 0);
18066
18067 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18068 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18069
18070 /* If first line's physical ascent is larger than its logical
18071 ascent, use the physical ascent, and make the row taller.
18072 This makes accented characters fully visible. */
18073 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18074 && row->phys_ascent > row->ascent)
18075 {
18076 row->height += row->phys_ascent - row->ascent;
18077 row->ascent = row->phys_ascent;
18078 }
18079
18080 /* Compute how much of the line is visible. */
18081 row->visible_height = row->height;
18082
18083 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18084 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18085
18086 if (row->y < min_y)
18087 row->visible_height -= min_y - row->y;
18088 if (row->y + row->height > max_y)
18089 row->visible_height -= row->y + row->height - max_y;
18090 }
18091 else
18092 {
18093 row->pixel_width = row->used[TEXT_AREA];
18094 if (row->continued_p)
18095 row->pixel_width -= it->continuation_pixel_width;
18096 else if (row->truncated_on_right_p)
18097 row->pixel_width -= it->truncation_pixel_width;
18098 row->ascent = row->phys_ascent = 0;
18099 row->height = row->phys_height = row->visible_height = 1;
18100 row->extra_line_spacing = 0;
18101 }
18102
18103 /* Compute a hash code for this row. */
18104 row->hash = row_hash (row);
18105
18106 it->max_ascent = it->max_descent = 0;
18107 it->max_phys_ascent = it->max_phys_descent = 0;
18108 }
18109
18110
18111 /* Append one space to the glyph row of iterator IT if doing a
18112 window-based redisplay. The space has the same face as
18113 IT->face_id. Value is non-zero if a space was added.
18114
18115 This function is called to make sure that there is always one glyph
18116 at the end of a glyph row that the cursor can be set on under
18117 window-systems. (If there weren't such a glyph we would not know
18118 how wide and tall a box cursor should be displayed).
18119
18120 At the same time this space let's a nicely handle clearing to the
18121 end of the line if the row ends in italic text. */
18122
18123 static int
18124 append_space_for_newline (struct it *it, int default_face_p)
18125 {
18126 if (FRAME_WINDOW_P (it->f))
18127 {
18128 int n = it->glyph_row->used[TEXT_AREA];
18129
18130 if (it->glyph_row->glyphs[TEXT_AREA] + n
18131 < it->glyph_row->glyphs[1 + TEXT_AREA])
18132 {
18133 /* Save some values that must not be changed.
18134 Must save IT->c and IT->len because otherwise
18135 ITERATOR_AT_END_P wouldn't work anymore after
18136 append_space_for_newline has been called. */
18137 enum display_element_type saved_what = it->what;
18138 int saved_c = it->c, saved_len = it->len;
18139 int saved_char_to_display = it->char_to_display;
18140 int saved_x = it->current_x;
18141 int saved_face_id = it->face_id;
18142 struct text_pos saved_pos;
18143 Lisp_Object saved_object;
18144 struct face *face;
18145
18146 saved_object = it->object;
18147 saved_pos = it->position;
18148
18149 it->what = IT_CHARACTER;
18150 memset (&it->position, 0, sizeof it->position);
18151 it->object = make_number (0);
18152 it->c = it->char_to_display = ' ';
18153 it->len = 1;
18154
18155 if (default_face_p)
18156 it->face_id = DEFAULT_FACE_ID;
18157 else if (it->face_before_selective_p)
18158 it->face_id = it->saved_face_id;
18159 face = FACE_FROM_ID (it->f, it->face_id);
18160 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18161
18162 PRODUCE_GLYPHS (it);
18163
18164 it->override_ascent = -1;
18165 it->constrain_row_ascent_descent_p = 0;
18166 it->current_x = saved_x;
18167 it->object = saved_object;
18168 it->position = saved_pos;
18169 it->what = saved_what;
18170 it->face_id = saved_face_id;
18171 it->len = saved_len;
18172 it->c = saved_c;
18173 it->char_to_display = saved_char_to_display;
18174 return 1;
18175 }
18176 }
18177
18178 return 0;
18179 }
18180
18181
18182 /* Extend the face of the last glyph in the text area of IT->glyph_row
18183 to the end of the display line. Called from display_line. If the
18184 glyph row is empty, add a space glyph to it so that we know the
18185 face to draw. Set the glyph row flag fill_line_p. If the glyph
18186 row is R2L, prepend a stretch glyph to cover the empty space to the
18187 left of the leftmost glyph. */
18188
18189 static void
18190 extend_face_to_end_of_line (struct it *it)
18191 {
18192 struct face *face;
18193 struct frame *f = it->f;
18194
18195 /* If line is already filled, do nothing. Non window-system frames
18196 get a grace of one more ``pixel'' because their characters are
18197 1-``pixel'' wide, so they hit the equality too early. This grace
18198 is needed only for R2L rows that are not continued, to produce
18199 one extra blank where we could display the cursor. */
18200 if (it->current_x >= it->last_visible_x
18201 + (!FRAME_WINDOW_P (f)
18202 && it->glyph_row->reversed_p
18203 && !it->glyph_row->continued_p))
18204 return;
18205
18206 /* Face extension extends the background and box of IT->face_id
18207 to the end of the line. If the background equals the background
18208 of the frame, we don't have to do anything. */
18209 if (it->face_before_selective_p)
18210 face = FACE_FROM_ID (f, it->saved_face_id);
18211 else
18212 face = FACE_FROM_ID (f, it->face_id);
18213
18214 if (FRAME_WINDOW_P (f)
18215 && it->glyph_row->displays_text_p
18216 && face->box == FACE_NO_BOX
18217 && face->background == FRAME_BACKGROUND_PIXEL (f)
18218 && !face->stipple
18219 && !it->glyph_row->reversed_p)
18220 return;
18221
18222 /* Set the glyph row flag indicating that the face of the last glyph
18223 in the text area has to be drawn to the end of the text area. */
18224 it->glyph_row->fill_line_p = 1;
18225
18226 /* If current character of IT is not ASCII, make sure we have the
18227 ASCII face. This will be automatically undone the next time
18228 get_next_display_element returns a multibyte character. Note
18229 that the character will always be single byte in unibyte
18230 text. */
18231 if (!ASCII_CHAR_P (it->c))
18232 {
18233 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18234 }
18235
18236 if (FRAME_WINDOW_P (f))
18237 {
18238 /* If the row is empty, add a space with the current face of IT,
18239 so that we know which face to draw. */
18240 if (it->glyph_row->used[TEXT_AREA] == 0)
18241 {
18242 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18243 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
18244 it->glyph_row->used[TEXT_AREA] = 1;
18245 }
18246 #ifdef HAVE_WINDOW_SYSTEM
18247 if (it->glyph_row->reversed_p)
18248 {
18249 /* Prepend a stretch glyph to the row, such that the
18250 rightmost glyph will be drawn flushed all the way to the
18251 right margin of the window. The stretch glyph that will
18252 occupy the empty space, if any, to the left of the
18253 glyphs. */
18254 struct font *font = face->font ? face->font : FRAME_FONT (f);
18255 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18256 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18257 struct glyph *g;
18258 int row_width, stretch_ascent, stretch_width;
18259 struct text_pos saved_pos;
18260 int saved_face_id, saved_avoid_cursor;
18261
18262 for (row_width = 0, g = row_start; g < row_end; g++)
18263 row_width += g->pixel_width;
18264 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18265 if (stretch_width > 0)
18266 {
18267 stretch_ascent =
18268 (((it->ascent + it->descent)
18269 * FONT_BASE (font)) / FONT_HEIGHT (font));
18270 saved_pos = it->position;
18271 memset (&it->position, 0, sizeof it->position);
18272 saved_avoid_cursor = it->avoid_cursor_p;
18273 it->avoid_cursor_p = 1;
18274 saved_face_id = it->face_id;
18275 /* The last row's stretch glyph should get the default
18276 face, to avoid painting the rest of the window with
18277 the region face, if the region ends at ZV. */
18278 if (it->glyph_row->ends_at_zv_p)
18279 it->face_id = DEFAULT_FACE_ID;
18280 else
18281 it->face_id = face->id;
18282 append_stretch_glyph (it, make_number (0), stretch_width,
18283 it->ascent + it->descent, stretch_ascent);
18284 it->position = saved_pos;
18285 it->avoid_cursor_p = saved_avoid_cursor;
18286 it->face_id = saved_face_id;
18287 }
18288 }
18289 #endif /* HAVE_WINDOW_SYSTEM */
18290 }
18291 else
18292 {
18293 /* Save some values that must not be changed. */
18294 int saved_x = it->current_x;
18295 struct text_pos saved_pos;
18296 Lisp_Object saved_object;
18297 enum display_element_type saved_what = it->what;
18298 int saved_face_id = it->face_id;
18299
18300 saved_object = it->object;
18301 saved_pos = it->position;
18302
18303 it->what = IT_CHARACTER;
18304 memset (&it->position, 0, sizeof it->position);
18305 it->object = make_number (0);
18306 it->c = it->char_to_display = ' ';
18307 it->len = 1;
18308 /* The last row's blank glyphs should get the default face, to
18309 avoid painting the rest of the window with the region face,
18310 if the region ends at ZV. */
18311 if (it->glyph_row->ends_at_zv_p)
18312 it->face_id = DEFAULT_FACE_ID;
18313 else
18314 it->face_id = face->id;
18315
18316 PRODUCE_GLYPHS (it);
18317
18318 while (it->current_x <= it->last_visible_x)
18319 PRODUCE_GLYPHS (it);
18320
18321 /* Don't count these blanks really. It would let us insert a left
18322 truncation glyph below and make us set the cursor on them, maybe. */
18323 it->current_x = saved_x;
18324 it->object = saved_object;
18325 it->position = saved_pos;
18326 it->what = saved_what;
18327 it->face_id = saved_face_id;
18328 }
18329 }
18330
18331
18332 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18333 trailing whitespace. */
18334
18335 static int
18336 trailing_whitespace_p (ptrdiff_t charpos)
18337 {
18338 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18339 int c = 0;
18340
18341 while (bytepos < ZV_BYTE
18342 && (c = FETCH_CHAR (bytepos),
18343 c == ' ' || c == '\t'))
18344 ++bytepos;
18345
18346 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18347 {
18348 if (bytepos != PT_BYTE)
18349 return 1;
18350 }
18351 return 0;
18352 }
18353
18354
18355 /* Highlight trailing whitespace, if any, in ROW. */
18356
18357 static void
18358 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18359 {
18360 int used = row->used[TEXT_AREA];
18361
18362 if (used)
18363 {
18364 struct glyph *start = row->glyphs[TEXT_AREA];
18365 struct glyph *glyph = start + used - 1;
18366
18367 if (row->reversed_p)
18368 {
18369 /* Right-to-left rows need to be processed in the opposite
18370 direction, so swap the edge pointers. */
18371 glyph = start;
18372 start = row->glyphs[TEXT_AREA] + used - 1;
18373 }
18374
18375 /* Skip over glyphs inserted to display the cursor at the
18376 end of a line, for extending the face of the last glyph
18377 to the end of the line on terminals, and for truncation
18378 and continuation glyphs. */
18379 if (!row->reversed_p)
18380 {
18381 while (glyph >= start
18382 && glyph->type == CHAR_GLYPH
18383 && INTEGERP (glyph->object))
18384 --glyph;
18385 }
18386 else
18387 {
18388 while (glyph <= start
18389 && glyph->type == CHAR_GLYPH
18390 && INTEGERP (glyph->object))
18391 ++glyph;
18392 }
18393
18394 /* If last glyph is a space or stretch, and it's trailing
18395 whitespace, set the face of all trailing whitespace glyphs in
18396 IT->glyph_row to `trailing-whitespace'. */
18397 if ((row->reversed_p ? glyph <= start : glyph >= start)
18398 && BUFFERP (glyph->object)
18399 && (glyph->type == STRETCH_GLYPH
18400 || (glyph->type == CHAR_GLYPH
18401 && glyph->u.ch == ' '))
18402 && trailing_whitespace_p (glyph->charpos))
18403 {
18404 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18405 if (face_id < 0)
18406 return;
18407
18408 if (!row->reversed_p)
18409 {
18410 while (glyph >= start
18411 && BUFFERP (glyph->object)
18412 && (glyph->type == STRETCH_GLYPH
18413 || (glyph->type == CHAR_GLYPH
18414 && glyph->u.ch == ' ')))
18415 (glyph--)->face_id = face_id;
18416 }
18417 else
18418 {
18419 while (glyph <= start
18420 && BUFFERP (glyph->object)
18421 && (glyph->type == STRETCH_GLYPH
18422 || (glyph->type == CHAR_GLYPH
18423 && glyph->u.ch == ' ')))
18424 (glyph++)->face_id = face_id;
18425 }
18426 }
18427 }
18428 }
18429
18430
18431 /* Value is non-zero if glyph row ROW should be
18432 used to hold the cursor. */
18433
18434 static int
18435 cursor_row_p (struct glyph_row *row)
18436 {
18437 int result = 1;
18438
18439 if (PT == CHARPOS (row->end.pos)
18440 || PT == MATRIX_ROW_END_CHARPOS (row))
18441 {
18442 /* Suppose the row ends on a string.
18443 Unless the row is continued, that means it ends on a newline
18444 in the string. If it's anything other than a display string
18445 (e.g. a before-string from an overlay), we don't want the
18446 cursor there. (This heuristic seems to give the optimal
18447 behavior for the various types of multi-line strings.) */
18448 if (CHARPOS (row->end.string_pos) >= 0)
18449 {
18450 if (row->continued_p)
18451 result = 1;
18452 else
18453 {
18454 /* Check for `display' property. */
18455 struct glyph *beg = row->glyphs[TEXT_AREA];
18456 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18457 struct glyph *glyph;
18458
18459 result = 0;
18460 for (glyph = end; glyph >= beg; --glyph)
18461 if (STRINGP (glyph->object))
18462 {
18463 Lisp_Object prop
18464 = Fget_char_property (make_number (PT),
18465 Qdisplay, Qnil);
18466 result =
18467 (!NILP (prop)
18468 && display_prop_string_p (prop, glyph->object));
18469 break;
18470 }
18471 }
18472 }
18473 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18474 {
18475 /* If the row ends in middle of a real character,
18476 and the line is continued, we want the cursor here.
18477 That's because CHARPOS (ROW->end.pos) would equal
18478 PT if PT is before the character. */
18479 if (!row->ends_in_ellipsis_p)
18480 result = row->continued_p;
18481 else
18482 /* If the row ends in an ellipsis, then
18483 CHARPOS (ROW->end.pos) will equal point after the
18484 invisible text. We want that position to be displayed
18485 after the ellipsis. */
18486 result = 0;
18487 }
18488 /* If the row ends at ZV, display the cursor at the end of that
18489 row instead of at the start of the row below. */
18490 else if (row->ends_at_zv_p)
18491 result = 1;
18492 else
18493 result = 0;
18494 }
18495
18496 return result;
18497 }
18498
18499 \f
18500
18501 /* Push the property PROP so that it will be rendered at the current
18502 position in IT. Return 1 if PROP was successfully pushed, 0
18503 otherwise. Called from handle_line_prefix to handle the
18504 `line-prefix' and `wrap-prefix' properties. */
18505
18506 static int
18507 push_display_prop (struct it *it, Lisp_Object prop)
18508 {
18509 struct text_pos pos =
18510 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18511
18512 xassert (it->method == GET_FROM_BUFFER
18513 || it->method == GET_FROM_DISPLAY_VECTOR
18514 || it->method == GET_FROM_STRING);
18515
18516 /* We need to save the current buffer/string position, so it will be
18517 restored by pop_it, because iterate_out_of_display_property
18518 depends on that being set correctly, but some situations leave
18519 it->position not yet set when this function is called. */
18520 push_it (it, &pos);
18521
18522 if (STRINGP (prop))
18523 {
18524 if (SCHARS (prop) == 0)
18525 {
18526 pop_it (it);
18527 return 0;
18528 }
18529
18530 it->string = prop;
18531 it->multibyte_p = STRING_MULTIBYTE (it->string);
18532 it->current.overlay_string_index = -1;
18533 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18534 it->end_charpos = it->string_nchars = SCHARS (it->string);
18535 it->method = GET_FROM_STRING;
18536 it->stop_charpos = 0;
18537 it->prev_stop = 0;
18538 it->base_level_stop = 0;
18539
18540 /* Force paragraph direction to be that of the parent
18541 buffer/string. */
18542 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18543 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18544 else
18545 it->paragraph_embedding = L2R;
18546
18547 /* Set up the bidi iterator for this display string. */
18548 if (it->bidi_p)
18549 {
18550 it->bidi_it.string.lstring = it->string;
18551 it->bidi_it.string.s = NULL;
18552 it->bidi_it.string.schars = it->end_charpos;
18553 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18554 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18555 it->bidi_it.string.unibyte = !it->multibyte_p;
18556 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18557 }
18558 }
18559 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18560 {
18561 it->method = GET_FROM_STRETCH;
18562 it->object = prop;
18563 }
18564 #ifdef HAVE_WINDOW_SYSTEM
18565 else if (IMAGEP (prop))
18566 {
18567 it->what = IT_IMAGE;
18568 it->image_id = lookup_image (it->f, prop);
18569 it->method = GET_FROM_IMAGE;
18570 }
18571 #endif /* HAVE_WINDOW_SYSTEM */
18572 else
18573 {
18574 pop_it (it); /* bogus display property, give up */
18575 return 0;
18576 }
18577
18578 return 1;
18579 }
18580
18581 /* Return the character-property PROP at the current position in IT. */
18582
18583 static Lisp_Object
18584 get_it_property (struct it *it, Lisp_Object prop)
18585 {
18586 Lisp_Object position;
18587
18588 if (STRINGP (it->object))
18589 position = make_number (IT_STRING_CHARPOS (*it));
18590 else if (BUFFERP (it->object))
18591 position = make_number (IT_CHARPOS (*it));
18592 else
18593 return Qnil;
18594
18595 return Fget_char_property (position, prop, it->object);
18596 }
18597
18598 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18599
18600 static void
18601 handle_line_prefix (struct it *it)
18602 {
18603 Lisp_Object prefix;
18604
18605 if (it->continuation_lines_width > 0)
18606 {
18607 prefix = get_it_property (it, Qwrap_prefix);
18608 if (NILP (prefix))
18609 prefix = Vwrap_prefix;
18610 }
18611 else
18612 {
18613 prefix = get_it_property (it, Qline_prefix);
18614 if (NILP (prefix))
18615 prefix = Vline_prefix;
18616 }
18617 if (! NILP (prefix) && push_display_prop (it, prefix))
18618 {
18619 /* If the prefix is wider than the window, and we try to wrap
18620 it, it would acquire its own wrap prefix, and so on till the
18621 iterator stack overflows. So, don't wrap the prefix. */
18622 it->line_wrap = TRUNCATE;
18623 it->avoid_cursor_p = 1;
18624 }
18625 }
18626
18627 \f
18628
18629 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18630 only for R2L lines from display_line and display_string, when they
18631 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18632 the line/string needs to be continued on the next glyph row. */
18633 static void
18634 unproduce_glyphs (struct it *it, int n)
18635 {
18636 struct glyph *glyph, *end;
18637
18638 xassert (it->glyph_row);
18639 xassert (it->glyph_row->reversed_p);
18640 xassert (it->area == TEXT_AREA);
18641 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18642
18643 if (n > it->glyph_row->used[TEXT_AREA])
18644 n = it->glyph_row->used[TEXT_AREA];
18645 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18646 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18647 for ( ; glyph < end; glyph++)
18648 glyph[-n] = *glyph;
18649 }
18650
18651 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18652 and ROW->maxpos. */
18653 static void
18654 find_row_edges (struct it *it, struct glyph_row *row,
18655 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18656 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18657 {
18658 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18659 lines' rows is implemented for bidi-reordered rows. */
18660
18661 /* ROW->minpos is the value of min_pos, the minimal buffer position
18662 we have in ROW, or ROW->start.pos if that is smaller. */
18663 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18664 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18665 else
18666 /* We didn't find buffer positions smaller than ROW->start, or
18667 didn't find _any_ valid buffer positions in any of the glyphs,
18668 so we must trust the iterator's computed positions. */
18669 row->minpos = row->start.pos;
18670 if (max_pos <= 0)
18671 {
18672 max_pos = CHARPOS (it->current.pos);
18673 max_bpos = BYTEPOS (it->current.pos);
18674 }
18675
18676 /* Here are the various use-cases for ending the row, and the
18677 corresponding values for ROW->maxpos:
18678
18679 Line ends in a newline from buffer eol_pos + 1
18680 Line is continued from buffer max_pos + 1
18681 Line is truncated on right it->current.pos
18682 Line ends in a newline from string max_pos + 1(*)
18683 (*) + 1 only when line ends in a forward scan
18684 Line is continued from string max_pos
18685 Line is continued from display vector max_pos
18686 Line is entirely from a string min_pos == max_pos
18687 Line is entirely from a display vector min_pos == max_pos
18688 Line that ends at ZV ZV
18689
18690 If you discover other use-cases, please add them here as
18691 appropriate. */
18692 if (row->ends_at_zv_p)
18693 row->maxpos = it->current.pos;
18694 else if (row->used[TEXT_AREA])
18695 {
18696 int seen_this_string = 0;
18697 struct glyph_row *r1 = row - 1;
18698
18699 /* Did we see the same display string on the previous row? */
18700 if (STRINGP (it->object)
18701 /* this is not the first row */
18702 && row > it->w->desired_matrix->rows
18703 /* previous row is not the header line */
18704 && !r1->mode_line_p
18705 /* previous row also ends in a newline from a string */
18706 && r1->ends_in_newline_from_string_p)
18707 {
18708 struct glyph *start, *end;
18709
18710 /* Search for the last glyph of the previous row that came
18711 from buffer or string. Depending on whether the row is
18712 L2R or R2L, we need to process it front to back or the
18713 other way round. */
18714 if (!r1->reversed_p)
18715 {
18716 start = r1->glyphs[TEXT_AREA];
18717 end = start + r1->used[TEXT_AREA];
18718 /* Glyphs inserted by redisplay have an integer (zero)
18719 as their object. */
18720 while (end > start
18721 && INTEGERP ((end - 1)->object)
18722 && (end - 1)->charpos <= 0)
18723 --end;
18724 if (end > start)
18725 {
18726 if (EQ ((end - 1)->object, it->object))
18727 seen_this_string = 1;
18728 }
18729 else
18730 /* If all the glyphs of the previous row were inserted
18731 by redisplay, it means the previous row was
18732 produced from a single newline, which is only
18733 possible if that newline came from the same string
18734 as the one which produced this ROW. */
18735 seen_this_string = 1;
18736 }
18737 else
18738 {
18739 end = r1->glyphs[TEXT_AREA] - 1;
18740 start = end + r1->used[TEXT_AREA];
18741 while (end < start
18742 && INTEGERP ((end + 1)->object)
18743 && (end + 1)->charpos <= 0)
18744 ++end;
18745 if (end < start)
18746 {
18747 if (EQ ((end + 1)->object, it->object))
18748 seen_this_string = 1;
18749 }
18750 else
18751 seen_this_string = 1;
18752 }
18753 }
18754 /* Take note of each display string that covers a newline only
18755 once, the first time we see it. This is for when a display
18756 string includes more than one newline in it. */
18757 if (row->ends_in_newline_from_string_p && !seen_this_string)
18758 {
18759 /* If we were scanning the buffer forward when we displayed
18760 the string, we want to account for at least one buffer
18761 position that belongs to this row (position covered by
18762 the display string), so that cursor positioning will
18763 consider this row as a candidate when point is at the end
18764 of the visual line represented by this row. This is not
18765 required when scanning back, because max_pos will already
18766 have a much larger value. */
18767 if (CHARPOS (row->end.pos) > max_pos)
18768 INC_BOTH (max_pos, max_bpos);
18769 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18770 }
18771 else if (CHARPOS (it->eol_pos) > 0)
18772 SET_TEXT_POS (row->maxpos,
18773 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18774 else if (row->continued_p)
18775 {
18776 /* If max_pos is different from IT's current position, it
18777 means IT->method does not belong to the display element
18778 at max_pos. However, it also means that the display
18779 element at max_pos was displayed in its entirety on this
18780 line, which is equivalent to saying that the next line
18781 starts at the next buffer position. */
18782 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18783 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18784 else
18785 {
18786 INC_BOTH (max_pos, max_bpos);
18787 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18788 }
18789 }
18790 else if (row->truncated_on_right_p)
18791 /* display_line already called reseat_at_next_visible_line_start,
18792 which puts the iterator at the beginning of the next line, in
18793 the logical order. */
18794 row->maxpos = it->current.pos;
18795 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18796 /* A line that is entirely from a string/image/stretch... */
18797 row->maxpos = row->minpos;
18798 else
18799 abort ();
18800 }
18801 else
18802 row->maxpos = it->current.pos;
18803 }
18804
18805 /* Construct the glyph row IT->glyph_row in the desired matrix of
18806 IT->w from text at the current position of IT. See dispextern.h
18807 for an overview of struct it. Value is non-zero if
18808 IT->glyph_row displays text, as opposed to a line displaying ZV
18809 only. */
18810
18811 static int
18812 display_line (struct it *it)
18813 {
18814 struct glyph_row *row = it->glyph_row;
18815 Lisp_Object overlay_arrow_string;
18816 struct it wrap_it;
18817 void *wrap_data = NULL;
18818 int may_wrap = 0, wrap_x IF_LINT (= 0);
18819 int wrap_row_used = -1;
18820 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18821 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18822 int wrap_row_extra_line_spacing IF_LINT (= 0);
18823 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18824 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18825 int cvpos;
18826 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
18827 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18828
18829 /* We always start displaying at hpos zero even if hscrolled. */
18830 xassert (it->hpos == 0 && it->current_x == 0);
18831
18832 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18833 >= it->w->desired_matrix->nrows)
18834 {
18835 it->w->nrows_scale_factor++;
18836 fonts_changed_p = 1;
18837 return 0;
18838 }
18839
18840 /* Is IT->w showing the region? */
18841 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18842
18843 /* Clear the result glyph row and enable it. */
18844 prepare_desired_row (row);
18845
18846 row->y = it->current_y;
18847 row->start = it->start;
18848 row->continuation_lines_width = it->continuation_lines_width;
18849 row->displays_text_p = 1;
18850 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18851 it->starts_in_middle_of_char_p = 0;
18852
18853 /* Arrange the overlays nicely for our purposes. Usually, we call
18854 display_line on only one line at a time, in which case this
18855 can't really hurt too much, or we call it on lines which appear
18856 one after another in the buffer, in which case all calls to
18857 recenter_overlay_lists but the first will be pretty cheap. */
18858 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18859
18860 /* Move over display elements that are not visible because we are
18861 hscrolled. This may stop at an x-position < IT->first_visible_x
18862 if the first glyph is partially visible or if we hit a line end. */
18863 if (it->current_x < it->first_visible_x)
18864 {
18865 this_line_min_pos = row->start.pos;
18866 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18867 MOVE_TO_POS | MOVE_TO_X);
18868 /* Record the smallest positions seen while we moved over
18869 display elements that are not visible. This is needed by
18870 redisplay_internal for optimizing the case where the cursor
18871 stays inside the same line. The rest of this function only
18872 considers positions that are actually displayed, so
18873 RECORD_MAX_MIN_POS will not otherwise record positions that
18874 are hscrolled to the left of the left edge of the window. */
18875 min_pos = CHARPOS (this_line_min_pos);
18876 min_bpos = BYTEPOS (this_line_min_pos);
18877 }
18878 else
18879 {
18880 /* We only do this when not calling `move_it_in_display_line_to'
18881 above, because move_it_in_display_line_to calls
18882 handle_line_prefix itself. */
18883 handle_line_prefix (it);
18884 }
18885
18886 /* Get the initial row height. This is either the height of the
18887 text hscrolled, if there is any, or zero. */
18888 row->ascent = it->max_ascent;
18889 row->height = it->max_ascent + it->max_descent;
18890 row->phys_ascent = it->max_phys_ascent;
18891 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18892 row->extra_line_spacing = it->max_extra_line_spacing;
18893
18894 /* Utility macro to record max and min buffer positions seen until now. */
18895 #define RECORD_MAX_MIN_POS(IT) \
18896 do \
18897 { \
18898 int composition_p = !STRINGP ((IT)->string) \
18899 && ((IT)->what == IT_COMPOSITION); \
18900 ptrdiff_t current_pos = \
18901 composition_p ? (IT)->cmp_it.charpos \
18902 : IT_CHARPOS (*(IT)); \
18903 ptrdiff_t current_bpos = \
18904 composition_p ? CHAR_TO_BYTE (current_pos) \
18905 : IT_BYTEPOS (*(IT)); \
18906 if (current_pos < min_pos) \
18907 { \
18908 min_pos = current_pos; \
18909 min_bpos = current_bpos; \
18910 } \
18911 if (IT_CHARPOS (*it) > max_pos) \
18912 { \
18913 max_pos = IT_CHARPOS (*it); \
18914 max_bpos = IT_BYTEPOS (*it); \
18915 } \
18916 } \
18917 while (0)
18918
18919 /* Loop generating characters. The loop is left with IT on the next
18920 character to display. */
18921 while (1)
18922 {
18923 int n_glyphs_before, hpos_before, x_before;
18924 int x, nglyphs;
18925 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18926
18927 /* Retrieve the next thing to display. Value is zero if end of
18928 buffer reached. */
18929 if (!get_next_display_element (it))
18930 {
18931 /* Maybe add a space at the end of this line that is used to
18932 display the cursor there under X. Set the charpos of the
18933 first glyph of blank lines not corresponding to any text
18934 to -1. */
18935 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18936 row->exact_window_width_line_p = 1;
18937 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18938 || row->used[TEXT_AREA] == 0)
18939 {
18940 row->glyphs[TEXT_AREA]->charpos = -1;
18941 row->displays_text_p = 0;
18942
18943 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18944 && (!MINI_WINDOW_P (it->w)
18945 || (minibuf_level && EQ (it->window, minibuf_window))))
18946 row->indicate_empty_line_p = 1;
18947 }
18948
18949 it->continuation_lines_width = 0;
18950 row->ends_at_zv_p = 1;
18951 /* A row that displays right-to-left text must always have
18952 its last face extended all the way to the end of line,
18953 even if this row ends in ZV, because we still write to
18954 the screen left to right. */
18955 if (row->reversed_p)
18956 extend_face_to_end_of_line (it);
18957 break;
18958 }
18959
18960 /* Now, get the metrics of what we want to display. This also
18961 generates glyphs in `row' (which is IT->glyph_row). */
18962 n_glyphs_before = row->used[TEXT_AREA];
18963 x = it->current_x;
18964
18965 /* Remember the line height so far in case the next element doesn't
18966 fit on the line. */
18967 if (it->line_wrap != TRUNCATE)
18968 {
18969 ascent = it->max_ascent;
18970 descent = it->max_descent;
18971 phys_ascent = it->max_phys_ascent;
18972 phys_descent = it->max_phys_descent;
18973
18974 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18975 {
18976 if (IT_DISPLAYING_WHITESPACE (it))
18977 may_wrap = 1;
18978 else if (may_wrap)
18979 {
18980 SAVE_IT (wrap_it, *it, wrap_data);
18981 wrap_x = x;
18982 wrap_row_used = row->used[TEXT_AREA];
18983 wrap_row_ascent = row->ascent;
18984 wrap_row_height = row->height;
18985 wrap_row_phys_ascent = row->phys_ascent;
18986 wrap_row_phys_height = row->phys_height;
18987 wrap_row_extra_line_spacing = row->extra_line_spacing;
18988 wrap_row_min_pos = min_pos;
18989 wrap_row_min_bpos = min_bpos;
18990 wrap_row_max_pos = max_pos;
18991 wrap_row_max_bpos = max_bpos;
18992 may_wrap = 0;
18993 }
18994 }
18995 }
18996
18997 PRODUCE_GLYPHS (it);
18998
18999 /* If this display element was in marginal areas, continue with
19000 the next one. */
19001 if (it->area != TEXT_AREA)
19002 {
19003 row->ascent = max (row->ascent, it->max_ascent);
19004 row->height = max (row->height, it->max_ascent + it->max_descent);
19005 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19006 row->phys_height = max (row->phys_height,
19007 it->max_phys_ascent + it->max_phys_descent);
19008 row->extra_line_spacing = max (row->extra_line_spacing,
19009 it->max_extra_line_spacing);
19010 set_iterator_to_next (it, 1);
19011 continue;
19012 }
19013
19014 /* Does the display element fit on the line? If we truncate
19015 lines, we should draw past the right edge of the window. If
19016 we don't truncate, we want to stop so that we can display the
19017 continuation glyph before the right margin. If lines are
19018 continued, there are two possible strategies for characters
19019 resulting in more than 1 glyph (e.g. tabs): Display as many
19020 glyphs as possible in this line and leave the rest for the
19021 continuation line, or display the whole element in the next
19022 line. Original redisplay did the former, so we do it also. */
19023 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19024 hpos_before = it->hpos;
19025 x_before = x;
19026
19027 if (/* Not a newline. */
19028 nglyphs > 0
19029 /* Glyphs produced fit entirely in the line. */
19030 && it->current_x < it->last_visible_x)
19031 {
19032 it->hpos += nglyphs;
19033 row->ascent = max (row->ascent, it->max_ascent);
19034 row->height = max (row->height, it->max_ascent + it->max_descent);
19035 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19036 row->phys_height = max (row->phys_height,
19037 it->max_phys_ascent + it->max_phys_descent);
19038 row->extra_line_spacing = max (row->extra_line_spacing,
19039 it->max_extra_line_spacing);
19040 if (it->current_x - it->pixel_width < it->first_visible_x)
19041 row->x = x - it->first_visible_x;
19042 /* Record the maximum and minimum buffer positions seen so
19043 far in glyphs that will be displayed by this row. */
19044 if (it->bidi_p)
19045 RECORD_MAX_MIN_POS (it);
19046 }
19047 else
19048 {
19049 int i, new_x;
19050 struct glyph *glyph;
19051
19052 for (i = 0; i < nglyphs; ++i, x = new_x)
19053 {
19054 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19055 new_x = x + glyph->pixel_width;
19056
19057 if (/* Lines are continued. */
19058 it->line_wrap != TRUNCATE
19059 && (/* Glyph doesn't fit on the line. */
19060 new_x > it->last_visible_x
19061 /* Or it fits exactly on a window system frame. */
19062 || (new_x == it->last_visible_x
19063 && FRAME_WINDOW_P (it->f))))
19064 {
19065 /* End of a continued line. */
19066
19067 if (it->hpos == 0
19068 || (new_x == it->last_visible_x
19069 && FRAME_WINDOW_P (it->f)))
19070 {
19071 /* Current glyph is the only one on the line or
19072 fits exactly on the line. We must continue
19073 the line because we can't draw the cursor
19074 after the glyph. */
19075 row->continued_p = 1;
19076 it->current_x = new_x;
19077 it->continuation_lines_width += new_x;
19078 ++it->hpos;
19079 if (i == nglyphs - 1)
19080 {
19081 /* If line-wrap is on, check if a previous
19082 wrap point was found. */
19083 if (wrap_row_used > 0
19084 /* Even if there is a previous wrap
19085 point, continue the line here as
19086 usual, if (i) the previous character
19087 was a space or tab AND (ii) the
19088 current character is not. */
19089 && (!may_wrap
19090 || IT_DISPLAYING_WHITESPACE (it)))
19091 goto back_to_wrap;
19092
19093 /* Record the maximum and minimum buffer
19094 positions seen so far in glyphs that will be
19095 displayed by this row. */
19096 if (it->bidi_p)
19097 RECORD_MAX_MIN_POS (it);
19098 set_iterator_to_next (it, 1);
19099 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19100 {
19101 if (!get_next_display_element (it))
19102 {
19103 row->exact_window_width_line_p = 1;
19104 it->continuation_lines_width = 0;
19105 row->continued_p = 0;
19106 row->ends_at_zv_p = 1;
19107 }
19108 else if (ITERATOR_AT_END_OF_LINE_P (it))
19109 {
19110 row->continued_p = 0;
19111 row->exact_window_width_line_p = 1;
19112 }
19113 }
19114 }
19115 else if (it->bidi_p)
19116 RECORD_MAX_MIN_POS (it);
19117 }
19118 else if (CHAR_GLYPH_PADDING_P (*glyph)
19119 && !FRAME_WINDOW_P (it->f))
19120 {
19121 /* A padding glyph that doesn't fit on this line.
19122 This means the whole character doesn't fit
19123 on the line. */
19124 if (row->reversed_p)
19125 unproduce_glyphs (it, row->used[TEXT_AREA]
19126 - n_glyphs_before);
19127 row->used[TEXT_AREA] = n_glyphs_before;
19128
19129 /* Fill the rest of the row with continuation
19130 glyphs like in 20.x. */
19131 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19132 < row->glyphs[1 + TEXT_AREA])
19133 produce_special_glyphs (it, IT_CONTINUATION);
19134
19135 row->continued_p = 1;
19136 it->current_x = x_before;
19137 it->continuation_lines_width += x_before;
19138
19139 /* Restore the height to what it was before the
19140 element not fitting on the line. */
19141 it->max_ascent = ascent;
19142 it->max_descent = descent;
19143 it->max_phys_ascent = phys_ascent;
19144 it->max_phys_descent = phys_descent;
19145 }
19146 else if (wrap_row_used > 0)
19147 {
19148 back_to_wrap:
19149 if (row->reversed_p)
19150 unproduce_glyphs (it,
19151 row->used[TEXT_AREA] - wrap_row_used);
19152 RESTORE_IT (it, &wrap_it, wrap_data);
19153 it->continuation_lines_width += wrap_x;
19154 row->used[TEXT_AREA] = wrap_row_used;
19155 row->ascent = wrap_row_ascent;
19156 row->height = wrap_row_height;
19157 row->phys_ascent = wrap_row_phys_ascent;
19158 row->phys_height = wrap_row_phys_height;
19159 row->extra_line_spacing = wrap_row_extra_line_spacing;
19160 min_pos = wrap_row_min_pos;
19161 min_bpos = wrap_row_min_bpos;
19162 max_pos = wrap_row_max_pos;
19163 max_bpos = wrap_row_max_bpos;
19164 row->continued_p = 1;
19165 row->ends_at_zv_p = 0;
19166 row->exact_window_width_line_p = 0;
19167 it->continuation_lines_width += x;
19168
19169 /* Make sure that a non-default face is extended
19170 up to the right margin of the window. */
19171 extend_face_to_end_of_line (it);
19172 }
19173 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19174 {
19175 /* A TAB that extends past the right edge of the
19176 window. This produces a single glyph on
19177 window system frames. We leave the glyph in
19178 this row and let it fill the row, but don't
19179 consume the TAB. */
19180 it->continuation_lines_width += it->last_visible_x;
19181 row->ends_in_middle_of_char_p = 1;
19182 row->continued_p = 1;
19183 glyph->pixel_width = it->last_visible_x - x;
19184 it->starts_in_middle_of_char_p = 1;
19185 }
19186 else
19187 {
19188 /* Something other than a TAB that draws past
19189 the right edge of the window. Restore
19190 positions to values before the element. */
19191 if (row->reversed_p)
19192 unproduce_glyphs (it, row->used[TEXT_AREA]
19193 - (n_glyphs_before + i));
19194 row->used[TEXT_AREA] = n_glyphs_before + i;
19195
19196 /* Display continuation glyphs. */
19197 if (!FRAME_WINDOW_P (it->f))
19198 produce_special_glyphs (it, IT_CONTINUATION);
19199 row->continued_p = 1;
19200
19201 it->current_x = x_before;
19202 it->continuation_lines_width += x;
19203 extend_face_to_end_of_line (it);
19204
19205 if (nglyphs > 1 && i > 0)
19206 {
19207 row->ends_in_middle_of_char_p = 1;
19208 it->starts_in_middle_of_char_p = 1;
19209 }
19210
19211 /* Restore the height to what it was before the
19212 element not fitting on the line. */
19213 it->max_ascent = ascent;
19214 it->max_descent = descent;
19215 it->max_phys_ascent = phys_ascent;
19216 it->max_phys_descent = phys_descent;
19217 }
19218
19219 break;
19220 }
19221 else if (new_x > it->first_visible_x)
19222 {
19223 /* Increment number of glyphs actually displayed. */
19224 ++it->hpos;
19225
19226 /* Record the maximum and minimum buffer positions
19227 seen so far in glyphs that will be displayed by
19228 this row. */
19229 if (it->bidi_p)
19230 RECORD_MAX_MIN_POS (it);
19231
19232 if (x < it->first_visible_x)
19233 /* Glyph is partially visible, i.e. row starts at
19234 negative X position. */
19235 row->x = x - it->first_visible_x;
19236 }
19237 else
19238 {
19239 /* Glyph is completely off the left margin of the
19240 window. This should not happen because of the
19241 move_it_in_display_line at the start of this
19242 function, unless the text display area of the
19243 window is empty. */
19244 xassert (it->first_visible_x <= it->last_visible_x);
19245 }
19246 }
19247 /* Even if this display element produced no glyphs at all,
19248 we want to record its position. */
19249 if (it->bidi_p && nglyphs == 0)
19250 RECORD_MAX_MIN_POS (it);
19251
19252 row->ascent = max (row->ascent, it->max_ascent);
19253 row->height = max (row->height, it->max_ascent + it->max_descent);
19254 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19255 row->phys_height = max (row->phys_height,
19256 it->max_phys_ascent + it->max_phys_descent);
19257 row->extra_line_spacing = max (row->extra_line_spacing,
19258 it->max_extra_line_spacing);
19259
19260 /* End of this display line if row is continued. */
19261 if (row->continued_p || row->ends_at_zv_p)
19262 break;
19263 }
19264
19265 at_end_of_line:
19266 /* Is this a line end? If yes, we're also done, after making
19267 sure that a non-default face is extended up to the right
19268 margin of the window. */
19269 if (ITERATOR_AT_END_OF_LINE_P (it))
19270 {
19271 int used_before = row->used[TEXT_AREA];
19272
19273 row->ends_in_newline_from_string_p = STRINGP (it->object);
19274
19275 /* Add a space at the end of the line that is used to
19276 display the cursor there. */
19277 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19278 append_space_for_newline (it, 0);
19279
19280 /* Extend the face to the end of the line. */
19281 extend_face_to_end_of_line (it);
19282
19283 /* Make sure we have the position. */
19284 if (used_before == 0)
19285 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19286
19287 /* Record the position of the newline, for use in
19288 find_row_edges. */
19289 it->eol_pos = it->current.pos;
19290
19291 /* Consume the line end. This skips over invisible lines. */
19292 set_iterator_to_next (it, 1);
19293 it->continuation_lines_width = 0;
19294 break;
19295 }
19296
19297 /* Proceed with next display element. Note that this skips
19298 over lines invisible because of selective display. */
19299 set_iterator_to_next (it, 1);
19300
19301 /* If we truncate lines, we are done when the last displayed
19302 glyphs reach past the right margin of the window. */
19303 if (it->line_wrap == TRUNCATE
19304 && (FRAME_WINDOW_P (it->f)
19305 ? (it->current_x >= it->last_visible_x)
19306 : (it->current_x > it->last_visible_x)))
19307 {
19308 /* Maybe add truncation glyphs. */
19309 if (!FRAME_WINDOW_P (it->f))
19310 {
19311 int i, n;
19312
19313 if (!row->reversed_p)
19314 {
19315 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19316 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19317 break;
19318 }
19319 else
19320 {
19321 for (i = 0; i < row->used[TEXT_AREA]; i++)
19322 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19323 break;
19324 /* Remove any padding glyphs at the front of ROW, to
19325 make room for the truncation glyphs we will be
19326 adding below. The loop below always inserts at
19327 least one truncation glyph, so also remove the
19328 last glyph added to ROW. */
19329 unproduce_glyphs (it, i + 1);
19330 /* Adjust i for the loop below. */
19331 i = row->used[TEXT_AREA] - (i + 1);
19332 }
19333
19334 for (n = row->used[TEXT_AREA]; i < n; ++i)
19335 {
19336 row->used[TEXT_AREA] = i;
19337 produce_special_glyphs (it, IT_TRUNCATION);
19338 }
19339 }
19340 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19341 {
19342 /* Don't truncate if we can overflow newline into fringe. */
19343 if (!get_next_display_element (it))
19344 {
19345 it->continuation_lines_width = 0;
19346 row->ends_at_zv_p = 1;
19347 row->exact_window_width_line_p = 1;
19348 break;
19349 }
19350 if (ITERATOR_AT_END_OF_LINE_P (it))
19351 {
19352 row->exact_window_width_line_p = 1;
19353 goto at_end_of_line;
19354 }
19355 }
19356
19357 row->truncated_on_right_p = 1;
19358 it->continuation_lines_width = 0;
19359 reseat_at_next_visible_line_start (it, 0);
19360 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19361 it->hpos = hpos_before;
19362 it->current_x = x_before;
19363 break;
19364 }
19365 }
19366
19367 if (wrap_data)
19368 bidi_unshelve_cache (wrap_data, 1);
19369
19370 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19371 at the left window margin. */
19372 if (it->first_visible_x
19373 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19374 {
19375 if (!FRAME_WINDOW_P (it->f))
19376 insert_left_trunc_glyphs (it);
19377 row->truncated_on_left_p = 1;
19378 }
19379
19380 /* Remember the position at which this line ends.
19381
19382 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19383 cannot be before the call to find_row_edges below, since that is
19384 where these positions are determined. */
19385 row->end = it->current;
19386 if (!it->bidi_p)
19387 {
19388 row->minpos = row->start.pos;
19389 row->maxpos = row->end.pos;
19390 }
19391 else
19392 {
19393 /* ROW->minpos and ROW->maxpos must be the smallest and
19394 `1 + the largest' buffer positions in ROW. But if ROW was
19395 bidi-reordered, these two positions can be anywhere in the
19396 row, so we must determine them now. */
19397 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19398 }
19399
19400 /* If the start of this line is the overlay arrow-position, then
19401 mark this glyph row as the one containing the overlay arrow.
19402 This is clearly a mess with variable size fonts. It would be
19403 better to let it be displayed like cursors under X. */
19404 if ((row->displays_text_p || !overlay_arrow_seen)
19405 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19406 !NILP (overlay_arrow_string)))
19407 {
19408 /* Overlay arrow in window redisplay is a fringe bitmap. */
19409 if (STRINGP (overlay_arrow_string))
19410 {
19411 struct glyph_row *arrow_row
19412 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19413 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19414 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19415 struct glyph *p = row->glyphs[TEXT_AREA];
19416 struct glyph *p2, *end;
19417
19418 /* Copy the arrow glyphs. */
19419 while (glyph < arrow_end)
19420 *p++ = *glyph++;
19421
19422 /* Throw away padding glyphs. */
19423 p2 = p;
19424 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19425 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19426 ++p2;
19427 if (p2 > p)
19428 {
19429 while (p2 < end)
19430 *p++ = *p2++;
19431 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19432 }
19433 }
19434 else
19435 {
19436 xassert (INTEGERP (overlay_arrow_string));
19437 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19438 }
19439 overlay_arrow_seen = 1;
19440 }
19441
19442 /* Highlight trailing whitespace. */
19443 if (!NILP (Vshow_trailing_whitespace))
19444 highlight_trailing_whitespace (it->f, it->glyph_row);
19445
19446 /* Compute pixel dimensions of this line. */
19447 compute_line_metrics (it);
19448
19449 /* Implementation note: No changes in the glyphs of ROW or in their
19450 faces can be done past this point, because compute_line_metrics
19451 computes ROW's hash value and stores it within the glyph_row
19452 structure. */
19453
19454 /* Record whether this row ends inside an ellipsis. */
19455 row->ends_in_ellipsis_p
19456 = (it->method == GET_FROM_DISPLAY_VECTOR
19457 && it->ellipsis_p);
19458
19459 /* Save fringe bitmaps in this row. */
19460 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19461 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19462 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19463 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19464
19465 it->left_user_fringe_bitmap = 0;
19466 it->left_user_fringe_face_id = 0;
19467 it->right_user_fringe_bitmap = 0;
19468 it->right_user_fringe_face_id = 0;
19469
19470 /* Maybe set the cursor. */
19471 cvpos = it->w->cursor.vpos;
19472 if ((cvpos < 0
19473 /* In bidi-reordered rows, keep checking for proper cursor
19474 position even if one has been found already, because buffer
19475 positions in such rows change non-linearly with ROW->VPOS,
19476 when a line is continued. One exception: when we are at ZV,
19477 display cursor on the first suitable glyph row, since all
19478 the empty rows after that also have their position set to ZV. */
19479 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19480 lines' rows is implemented for bidi-reordered rows. */
19481 || (it->bidi_p
19482 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19483 && PT >= MATRIX_ROW_START_CHARPOS (row)
19484 && PT <= MATRIX_ROW_END_CHARPOS (row)
19485 && cursor_row_p (row))
19486 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19487
19488 /* Prepare for the next line. This line starts horizontally at (X
19489 HPOS) = (0 0). Vertical positions are incremented. As a
19490 convenience for the caller, IT->glyph_row is set to the next
19491 row to be used. */
19492 it->current_x = it->hpos = 0;
19493 it->current_y += row->height;
19494 SET_TEXT_POS (it->eol_pos, 0, 0);
19495 ++it->vpos;
19496 ++it->glyph_row;
19497 /* The next row should by default use the same value of the
19498 reversed_p flag as this one. set_iterator_to_next decides when
19499 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19500 the flag accordingly. */
19501 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19502 it->glyph_row->reversed_p = row->reversed_p;
19503 it->start = row->end;
19504 return row->displays_text_p;
19505
19506 #undef RECORD_MAX_MIN_POS
19507 }
19508
19509 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19510 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19511 doc: /* Return paragraph direction at point in BUFFER.
19512 Value is either `left-to-right' or `right-to-left'.
19513 If BUFFER is omitted or nil, it defaults to the current buffer.
19514
19515 Paragraph direction determines how the text in the paragraph is displayed.
19516 In left-to-right paragraphs, text begins at the left margin of the window
19517 and the reading direction is generally left to right. In right-to-left
19518 paragraphs, text begins at the right margin and is read from right to left.
19519
19520 See also `bidi-paragraph-direction'. */)
19521 (Lisp_Object buffer)
19522 {
19523 struct buffer *buf = current_buffer;
19524 struct buffer *old = buf;
19525
19526 if (! NILP (buffer))
19527 {
19528 CHECK_BUFFER (buffer);
19529 buf = XBUFFER (buffer);
19530 }
19531
19532 if (NILP (BVAR (buf, bidi_display_reordering))
19533 || NILP (BVAR (buf, enable_multibyte_characters))
19534 /* When we are loading loadup.el, the character property tables
19535 needed for bidi iteration are not yet available. */
19536 || !NILP (Vpurify_flag))
19537 return Qleft_to_right;
19538 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19539 return BVAR (buf, bidi_paragraph_direction);
19540 else
19541 {
19542 /* Determine the direction from buffer text. We could try to
19543 use current_matrix if it is up to date, but this seems fast
19544 enough as it is. */
19545 struct bidi_it itb;
19546 ptrdiff_t pos = BUF_PT (buf);
19547 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19548 int c;
19549 void *itb_data = bidi_shelve_cache ();
19550
19551 set_buffer_temp (buf);
19552 /* bidi_paragraph_init finds the base direction of the paragraph
19553 by searching forward from paragraph start. We need the base
19554 direction of the current or _previous_ paragraph, so we need
19555 to make sure we are within that paragraph. To that end, find
19556 the previous non-empty line. */
19557 if (pos >= ZV && pos > BEGV)
19558 {
19559 pos--;
19560 bytepos = CHAR_TO_BYTE (pos);
19561 }
19562 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19563 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19564 {
19565 while ((c = FETCH_BYTE (bytepos)) == '\n'
19566 || c == ' ' || c == '\t' || c == '\f')
19567 {
19568 if (bytepos <= BEGV_BYTE)
19569 break;
19570 bytepos--;
19571 pos--;
19572 }
19573 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19574 bytepos--;
19575 }
19576 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19577 itb.paragraph_dir = NEUTRAL_DIR;
19578 itb.string.s = NULL;
19579 itb.string.lstring = Qnil;
19580 itb.string.bufpos = 0;
19581 itb.string.unibyte = 0;
19582 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19583 bidi_unshelve_cache (itb_data, 0);
19584 set_buffer_temp (old);
19585 switch (itb.paragraph_dir)
19586 {
19587 case L2R:
19588 return Qleft_to_right;
19589 break;
19590 case R2L:
19591 return Qright_to_left;
19592 break;
19593 default:
19594 abort ();
19595 }
19596 }
19597 }
19598
19599
19600 \f
19601 /***********************************************************************
19602 Menu Bar
19603 ***********************************************************************/
19604
19605 /* Redisplay the menu bar in the frame for window W.
19606
19607 The menu bar of X frames that don't have X toolkit support is
19608 displayed in a special window W->frame->menu_bar_window.
19609
19610 The menu bar of terminal frames is treated specially as far as
19611 glyph matrices are concerned. Menu bar lines are not part of
19612 windows, so the update is done directly on the frame matrix rows
19613 for the menu bar. */
19614
19615 static void
19616 display_menu_bar (struct window *w)
19617 {
19618 struct frame *f = XFRAME (WINDOW_FRAME (w));
19619 struct it it;
19620 Lisp_Object items;
19621 int i;
19622
19623 /* Don't do all this for graphical frames. */
19624 #ifdef HAVE_NTGUI
19625 if (FRAME_W32_P (f))
19626 return;
19627 #endif
19628 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19629 if (FRAME_X_P (f))
19630 return;
19631 #endif
19632
19633 #ifdef HAVE_NS
19634 if (FRAME_NS_P (f))
19635 return;
19636 #endif /* HAVE_NS */
19637
19638 #ifdef USE_X_TOOLKIT
19639 xassert (!FRAME_WINDOW_P (f));
19640 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19641 it.first_visible_x = 0;
19642 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19643 #else /* not USE_X_TOOLKIT */
19644 if (FRAME_WINDOW_P (f))
19645 {
19646 /* Menu bar lines are displayed in the desired matrix of the
19647 dummy window menu_bar_window. */
19648 struct window *menu_w;
19649 xassert (WINDOWP (f->menu_bar_window));
19650 menu_w = XWINDOW (f->menu_bar_window);
19651 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19652 MENU_FACE_ID);
19653 it.first_visible_x = 0;
19654 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19655 }
19656 else
19657 {
19658 /* This is a TTY frame, i.e. character hpos/vpos are used as
19659 pixel x/y. */
19660 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19661 MENU_FACE_ID);
19662 it.first_visible_x = 0;
19663 it.last_visible_x = FRAME_COLS (f);
19664 }
19665 #endif /* not USE_X_TOOLKIT */
19666
19667 /* FIXME: This should be controlled by a user option. See the
19668 comments in redisplay_tool_bar and display_mode_line about
19669 this. */
19670 it.paragraph_embedding = L2R;
19671
19672 if (! mode_line_inverse_video)
19673 /* Force the menu-bar to be displayed in the default face. */
19674 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19675
19676 /* Clear all rows of the menu bar. */
19677 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19678 {
19679 struct glyph_row *row = it.glyph_row + i;
19680 clear_glyph_row (row);
19681 row->enabled_p = 1;
19682 row->full_width_p = 1;
19683 }
19684
19685 /* Display all items of the menu bar. */
19686 items = FRAME_MENU_BAR_ITEMS (it.f);
19687 for (i = 0; i < ASIZE (items); i += 4)
19688 {
19689 Lisp_Object string;
19690
19691 /* Stop at nil string. */
19692 string = AREF (items, i + 1);
19693 if (NILP (string))
19694 break;
19695
19696 /* Remember where item was displayed. */
19697 ASET (items, i + 3, make_number (it.hpos));
19698
19699 /* Display the item, pad with one space. */
19700 if (it.current_x < it.last_visible_x)
19701 display_string (NULL, string, Qnil, 0, 0, &it,
19702 SCHARS (string) + 1, 0, 0, -1);
19703 }
19704
19705 /* Fill out the line with spaces. */
19706 if (it.current_x < it.last_visible_x)
19707 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19708
19709 /* Compute the total height of the lines. */
19710 compute_line_metrics (&it);
19711 }
19712
19713
19714 \f
19715 /***********************************************************************
19716 Mode Line
19717 ***********************************************************************/
19718
19719 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19720 FORCE is non-zero, redisplay mode lines unconditionally.
19721 Otherwise, redisplay only mode lines that are garbaged. Value is
19722 the number of windows whose mode lines were redisplayed. */
19723
19724 static int
19725 redisplay_mode_lines (Lisp_Object window, int force)
19726 {
19727 int nwindows = 0;
19728
19729 while (!NILP (window))
19730 {
19731 struct window *w = XWINDOW (window);
19732
19733 if (WINDOWP (w->hchild))
19734 nwindows += redisplay_mode_lines (w->hchild, force);
19735 else if (WINDOWP (w->vchild))
19736 nwindows += redisplay_mode_lines (w->vchild, force);
19737 else if (force
19738 || FRAME_GARBAGED_P (XFRAME (w->frame))
19739 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19740 {
19741 struct text_pos lpoint;
19742 struct buffer *old = current_buffer;
19743
19744 /* Set the window's buffer for the mode line display. */
19745 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19746 set_buffer_internal_1 (XBUFFER (w->buffer));
19747
19748 /* Point refers normally to the selected window. For any
19749 other window, set up appropriate value. */
19750 if (!EQ (window, selected_window))
19751 {
19752 struct text_pos pt;
19753
19754 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19755 if (CHARPOS (pt) < BEGV)
19756 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19757 else if (CHARPOS (pt) > (ZV - 1))
19758 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19759 else
19760 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19761 }
19762
19763 /* Display mode lines. */
19764 clear_glyph_matrix (w->desired_matrix);
19765 if (display_mode_lines (w))
19766 {
19767 ++nwindows;
19768 w->must_be_updated_p = 1;
19769 }
19770
19771 /* Restore old settings. */
19772 set_buffer_internal_1 (old);
19773 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19774 }
19775
19776 window = w->next;
19777 }
19778
19779 return nwindows;
19780 }
19781
19782
19783 /* Display the mode and/or header line of window W. Value is the
19784 sum number of mode lines and header lines displayed. */
19785
19786 static int
19787 display_mode_lines (struct window *w)
19788 {
19789 Lisp_Object old_selected_window, old_selected_frame;
19790 int n = 0;
19791
19792 old_selected_frame = selected_frame;
19793 selected_frame = w->frame;
19794 old_selected_window = selected_window;
19795 XSETWINDOW (selected_window, w);
19796
19797 /* These will be set while the mode line specs are processed. */
19798 line_number_displayed = 0;
19799 w->column_number_displayed = Qnil;
19800
19801 if (WINDOW_WANTS_MODELINE_P (w))
19802 {
19803 struct window *sel_w = XWINDOW (old_selected_window);
19804
19805 /* Select mode line face based on the real selected window. */
19806 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19807 BVAR (current_buffer, mode_line_format));
19808 ++n;
19809 }
19810
19811 if (WINDOW_WANTS_HEADER_LINE_P (w))
19812 {
19813 display_mode_line (w, HEADER_LINE_FACE_ID,
19814 BVAR (current_buffer, header_line_format));
19815 ++n;
19816 }
19817
19818 selected_frame = old_selected_frame;
19819 selected_window = old_selected_window;
19820 return n;
19821 }
19822
19823
19824 /* Display mode or header line of window W. FACE_ID specifies which
19825 line to display; it is either MODE_LINE_FACE_ID or
19826 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19827 display. Value is the pixel height of the mode/header line
19828 displayed. */
19829
19830 static int
19831 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19832 {
19833 struct it it;
19834 struct face *face;
19835 ptrdiff_t count = SPECPDL_INDEX ();
19836
19837 init_iterator (&it, w, -1, -1, NULL, face_id);
19838 /* Don't extend on a previously drawn mode-line.
19839 This may happen if called from pos_visible_p. */
19840 it.glyph_row->enabled_p = 0;
19841 prepare_desired_row (it.glyph_row);
19842
19843 it.glyph_row->mode_line_p = 1;
19844
19845 if (! mode_line_inverse_video)
19846 /* Force the mode-line to be displayed in the default face. */
19847 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19848
19849 /* FIXME: This should be controlled by a user option. But
19850 supporting such an option is not trivial, since the mode line is
19851 made up of many separate strings. */
19852 it.paragraph_embedding = L2R;
19853
19854 record_unwind_protect (unwind_format_mode_line,
19855 format_mode_line_unwind_data (NULL, Qnil, 0));
19856
19857 mode_line_target = MODE_LINE_DISPLAY;
19858
19859 /* Temporarily make frame's keyboard the current kboard so that
19860 kboard-local variables in the mode_line_format will get the right
19861 values. */
19862 push_kboard (FRAME_KBOARD (it.f));
19863 record_unwind_save_match_data ();
19864 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19865 pop_kboard ();
19866
19867 unbind_to (count, Qnil);
19868
19869 /* Fill up with spaces. */
19870 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19871
19872 compute_line_metrics (&it);
19873 it.glyph_row->full_width_p = 1;
19874 it.glyph_row->continued_p = 0;
19875 it.glyph_row->truncated_on_left_p = 0;
19876 it.glyph_row->truncated_on_right_p = 0;
19877
19878 /* Make a 3D mode-line have a shadow at its right end. */
19879 face = FACE_FROM_ID (it.f, face_id);
19880 extend_face_to_end_of_line (&it);
19881 if (face->box != FACE_NO_BOX)
19882 {
19883 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19884 + it.glyph_row->used[TEXT_AREA] - 1);
19885 last->right_box_line_p = 1;
19886 }
19887
19888 return it.glyph_row->height;
19889 }
19890
19891 /* Move element ELT in LIST to the front of LIST.
19892 Return the updated list. */
19893
19894 static Lisp_Object
19895 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19896 {
19897 register Lisp_Object tail, prev;
19898 register Lisp_Object tem;
19899
19900 tail = list;
19901 prev = Qnil;
19902 while (CONSP (tail))
19903 {
19904 tem = XCAR (tail);
19905
19906 if (EQ (elt, tem))
19907 {
19908 /* Splice out the link TAIL. */
19909 if (NILP (prev))
19910 list = XCDR (tail);
19911 else
19912 Fsetcdr (prev, XCDR (tail));
19913
19914 /* Now make it the first. */
19915 Fsetcdr (tail, list);
19916 return tail;
19917 }
19918 else
19919 prev = tail;
19920 tail = XCDR (tail);
19921 QUIT;
19922 }
19923
19924 /* Not found--return unchanged LIST. */
19925 return list;
19926 }
19927
19928 /* Contribute ELT to the mode line for window IT->w. How it
19929 translates into text depends on its data type.
19930
19931 IT describes the display environment in which we display, as usual.
19932
19933 DEPTH is the depth in recursion. It is used to prevent
19934 infinite recursion here.
19935
19936 FIELD_WIDTH is the number of characters the display of ELT should
19937 occupy in the mode line, and PRECISION is the maximum number of
19938 characters to display from ELT's representation. See
19939 display_string for details.
19940
19941 Returns the hpos of the end of the text generated by ELT.
19942
19943 PROPS is a property list to add to any string we encounter.
19944
19945 If RISKY is nonzero, remove (disregard) any properties in any string
19946 we encounter, and ignore :eval and :propertize.
19947
19948 The global variable `mode_line_target' determines whether the
19949 output is passed to `store_mode_line_noprop',
19950 `store_mode_line_string', or `display_string'. */
19951
19952 static int
19953 display_mode_element (struct it *it, int depth, int field_width, int precision,
19954 Lisp_Object elt, Lisp_Object props, int risky)
19955 {
19956 int n = 0, field, prec;
19957 int literal = 0;
19958
19959 tail_recurse:
19960 if (depth > 100)
19961 elt = build_string ("*too-deep*");
19962
19963 depth++;
19964
19965 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19966 {
19967 case Lisp_String:
19968 {
19969 /* A string: output it and check for %-constructs within it. */
19970 unsigned char c;
19971 ptrdiff_t offset = 0;
19972
19973 if (SCHARS (elt) > 0
19974 && (!NILP (props) || risky))
19975 {
19976 Lisp_Object oprops, aelt;
19977 oprops = Ftext_properties_at (make_number (0), elt);
19978
19979 /* If the starting string's properties are not what
19980 we want, translate the string. Also, if the string
19981 is risky, do that anyway. */
19982
19983 if (NILP (Fequal (props, oprops)) || risky)
19984 {
19985 /* If the starting string has properties,
19986 merge the specified ones onto the existing ones. */
19987 if (! NILP (oprops) && !risky)
19988 {
19989 Lisp_Object tem;
19990
19991 oprops = Fcopy_sequence (oprops);
19992 tem = props;
19993 while (CONSP (tem))
19994 {
19995 oprops = Fplist_put (oprops, XCAR (tem),
19996 XCAR (XCDR (tem)));
19997 tem = XCDR (XCDR (tem));
19998 }
19999 props = oprops;
20000 }
20001
20002 aelt = Fassoc (elt, mode_line_proptrans_alist);
20003 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20004 {
20005 /* AELT is what we want. Move it to the front
20006 without consing. */
20007 elt = XCAR (aelt);
20008 mode_line_proptrans_alist
20009 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20010 }
20011 else
20012 {
20013 Lisp_Object tem;
20014
20015 /* If AELT has the wrong props, it is useless.
20016 so get rid of it. */
20017 if (! NILP (aelt))
20018 mode_line_proptrans_alist
20019 = Fdelq (aelt, mode_line_proptrans_alist);
20020
20021 elt = Fcopy_sequence (elt);
20022 Fset_text_properties (make_number (0), Flength (elt),
20023 props, elt);
20024 /* Add this item to mode_line_proptrans_alist. */
20025 mode_line_proptrans_alist
20026 = Fcons (Fcons (elt, props),
20027 mode_line_proptrans_alist);
20028 /* Truncate mode_line_proptrans_alist
20029 to at most 50 elements. */
20030 tem = Fnthcdr (make_number (50),
20031 mode_line_proptrans_alist);
20032 if (! NILP (tem))
20033 XSETCDR (tem, Qnil);
20034 }
20035 }
20036 }
20037
20038 offset = 0;
20039
20040 if (literal)
20041 {
20042 prec = precision - n;
20043 switch (mode_line_target)
20044 {
20045 case MODE_LINE_NOPROP:
20046 case MODE_LINE_TITLE:
20047 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20048 break;
20049 case MODE_LINE_STRING:
20050 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20051 break;
20052 case MODE_LINE_DISPLAY:
20053 n += display_string (NULL, elt, Qnil, 0, 0, it,
20054 0, prec, 0, STRING_MULTIBYTE (elt));
20055 break;
20056 }
20057
20058 break;
20059 }
20060
20061 /* Handle the non-literal case. */
20062
20063 while ((precision <= 0 || n < precision)
20064 && SREF (elt, offset) != 0
20065 && (mode_line_target != MODE_LINE_DISPLAY
20066 || it->current_x < it->last_visible_x))
20067 {
20068 ptrdiff_t last_offset = offset;
20069
20070 /* Advance to end of string or next format specifier. */
20071 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20072 ;
20073
20074 if (offset - 1 != last_offset)
20075 {
20076 ptrdiff_t nchars, nbytes;
20077
20078 /* Output to end of string or up to '%'. Field width
20079 is length of string. Don't output more than
20080 PRECISION allows us. */
20081 offset--;
20082
20083 prec = c_string_width (SDATA (elt) + last_offset,
20084 offset - last_offset, precision - n,
20085 &nchars, &nbytes);
20086
20087 switch (mode_line_target)
20088 {
20089 case MODE_LINE_NOPROP:
20090 case MODE_LINE_TITLE:
20091 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20092 break;
20093 case MODE_LINE_STRING:
20094 {
20095 ptrdiff_t bytepos = last_offset;
20096 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20097 ptrdiff_t endpos = (precision <= 0
20098 ? string_byte_to_char (elt, offset)
20099 : charpos + nchars);
20100
20101 n += store_mode_line_string (NULL,
20102 Fsubstring (elt, make_number (charpos),
20103 make_number (endpos)),
20104 0, 0, 0, Qnil);
20105 }
20106 break;
20107 case MODE_LINE_DISPLAY:
20108 {
20109 ptrdiff_t bytepos = last_offset;
20110 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20111
20112 if (precision <= 0)
20113 nchars = string_byte_to_char (elt, offset) - charpos;
20114 n += display_string (NULL, elt, Qnil, 0, charpos,
20115 it, 0, nchars, 0,
20116 STRING_MULTIBYTE (elt));
20117 }
20118 break;
20119 }
20120 }
20121 else /* c == '%' */
20122 {
20123 ptrdiff_t percent_position = offset;
20124
20125 /* Get the specified minimum width. Zero means
20126 don't pad. */
20127 field = 0;
20128 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20129 field = field * 10 + c - '0';
20130
20131 /* Don't pad beyond the total padding allowed. */
20132 if (field_width - n > 0 && field > field_width - n)
20133 field = field_width - n;
20134
20135 /* Note that either PRECISION <= 0 or N < PRECISION. */
20136 prec = precision - n;
20137
20138 if (c == 'M')
20139 n += display_mode_element (it, depth, field, prec,
20140 Vglobal_mode_string, props,
20141 risky);
20142 else if (c != 0)
20143 {
20144 int multibyte;
20145 ptrdiff_t bytepos, charpos;
20146 const char *spec;
20147 Lisp_Object string;
20148
20149 bytepos = percent_position;
20150 charpos = (STRING_MULTIBYTE (elt)
20151 ? string_byte_to_char (elt, bytepos)
20152 : bytepos);
20153 spec = decode_mode_spec (it->w, c, field, &string);
20154 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20155
20156 switch (mode_line_target)
20157 {
20158 case MODE_LINE_NOPROP:
20159 case MODE_LINE_TITLE:
20160 n += store_mode_line_noprop (spec, field, prec);
20161 break;
20162 case MODE_LINE_STRING:
20163 {
20164 Lisp_Object tem = build_string (spec);
20165 props = Ftext_properties_at (make_number (charpos), elt);
20166 /* Should only keep face property in props */
20167 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20168 }
20169 break;
20170 case MODE_LINE_DISPLAY:
20171 {
20172 int nglyphs_before, nwritten;
20173
20174 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20175 nwritten = display_string (spec, string, elt,
20176 charpos, 0, it,
20177 field, prec, 0,
20178 multibyte);
20179
20180 /* Assign to the glyphs written above the
20181 string where the `%x' came from, position
20182 of the `%'. */
20183 if (nwritten > 0)
20184 {
20185 struct glyph *glyph
20186 = (it->glyph_row->glyphs[TEXT_AREA]
20187 + nglyphs_before);
20188 int i;
20189
20190 for (i = 0; i < nwritten; ++i)
20191 {
20192 glyph[i].object = elt;
20193 glyph[i].charpos = charpos;
20194 }
20195
20196 n += nwritten;
20197 }
20198 }
20199 break;
20200 }
20201 }
20202 else /* c == 0 */
20203 break;
20204 }
20205 }
20206 }
20207 break;
20208
20209 case Lisp_Symbol:
20210 /* A symbol: process the value of the symbol recursively
20211 as if it appeared here directly. Avoid error if symbol void.
20212 Special case: if value of symbol is a string, output the string
20213 literally. */
20214 {
20215 register Lisp_Object tem;
20216
20217 /* If the variable is not marked as risky to set
20218 then its contents are risky to use. */
20219 if (NILP (Fget (elt, Qrisky_local_variable)))
20220 risky = 1;
20221
20222 tem = Fboundp (elt);
20223 if (!NILP (tem))
20224 {
20225 tem = Fsymbol_value (elt);
20226 /* If value is a string, output that string literally:
20227 don't check for % within it. */
20228 if (STRINGP (tem))
20229 literal = 1;
20230
20231 if (!EQ (tem, elt))
20232 {
20233 /* Give up right away for nil or t. */
20234 elt = tem;
20235 goto tail_recurse;
20236 }
20237 }
20238 }
20239 break;
20240
20241 case Lisp_Cons:
20242 {
20243 register Lisp_Object car, tem;
20244
20245 /* A cons cell: five distinct cases.
20246 If first element is :eval or :propertize, do something special.
20247 If first element is a string or a cons, process all the elements
20248 and effectively concatenate them.
20249 If first element is a negative number, truncate displaying cdr to
20250 at most that many characters. If positive, pad (with spaces)
20251 to at least that many characters.
20252 If first element is a symbol, process the cadr or caddr recursively
20253 according to whether the symbol's value is non-nil or nil. */
20254 car = XCAR (elt);
20255 if (EQ (car, QCeval))
20256 {
20257 /* An element of the form (:eval FORM) means evaluate FORM
20258 and use the result as mode line elements. */
20259
20260 if (risky)
20261 break;
20262
20263 if (CONSP (XCDR (elt)))
20264 {
20265 Lisp_Object spec;
20266 spec = safe_eval (XCAR (XCDR (elt)));
20267 n += display_mode_element (it, depth, field_width - n,
20268 precision - n, spec, props,
20269 risky);
20270 }
20271 }
20272 else if (EQ (car, QCpropertize))
20273 {
20274 /* An element of the form (:propertize ELT PROPS...)
20275 means display ELT but applying properties PROPS. */
20276
20277 if (risky)
20278 break;
20279
20280 if (CONSP (XCDR (elt)))
20281 n += display_mode_element (it, depth, field_width - n,
20282 precision - n, XCAR (XCDR (elt)),
20283 XCDR (XCDR (elt)), risky);
20284 }
20285 else if (SYMBOLP (car))
20286 {
20287 tem = Fboundp (car);
20288 elt = XCDR (elt);
20289 if (!CONSP (elt))
20290 goto invalid;
20291 /* elt is now the cdr, and we know it is a cons cell.
20292 Use its car if CAR has a non-nil value. */
20293 if (!NILP (tem))
20294 {
20295 tem = Fsymbol_value (car);
20296 if (!NILP (tem))
20297 {
20298 elt = XCAR (elt);
20299 goto tail_recurse;
20300 }
20301 }
20302 /* Symbol's value is nil (or symbol is unbound)
20303 Get the cddr of the original list
20304 and if possible find the caddr and use that. */
20305 elt = XCDR (elt);
20306 if (NILP (elt))
20307 break;
20308 else if (!CONSP (elt))
20309 goto invalid;
20310 elt = XCAR (elt);
20311 goto tail_recurse;
20312 }
20313 else if (INTEGERP (car))
20314 {
20315 register int lim = XINT (car);
20316 elt = XCDR (elt);
20317 if (lim < 0)
20318 {
20319 /* Negative int means reduce maximum width. */
20320 if (precision <= 0)
20321 precision = -lim;
20322 else
20323 precision = min (precision, -lim);
20324 }
20325 else if (lim > 0)
20326 {
20327 /* Padding specified. Don't let it be more than
20328 current maximum. */
20329 if (precision > 0)
20330 lim = min (precision, lim);
20331
20332 /* If that's more padding than already wanted, queue it.
20333 But don't reduce padding already specified even if
20334 that is beyond the current truncation point. */
20335 field_width = max (lim, field_width);
20336 }
20337 goto tail_recurse;
20338 }
20339 else if (STRINGP (car) || CONSP (car))
20340 {
20341 Lisp_Object halftail = elt;
20342 int len = 0;
20343
20344 while (CONSP (elt)
20345 && (precision <= 0 || n < precision))
20346 {
20347 n += display_mode_element (it, depth,
20348 /* Do padding only after the last
20349 element in the list. */
20350 (! CONSP (XCDR (elt))
20351 ? field_width - n
20352 : 0),
20353 precision - n, XCAR (elt),
20354 props, risky);
20355 elt = XCDR (elt);
20356 len++;
20357 if ((len & 1) == 0)
20358 halftail = XCDR (halftail);
20359 /* Check for cycle. */
20360 if (EQ (halftail, elt))
20361 break;
20362 }
20363 }
20364 }
20365 break;
20366
20367 default:
20368 invalid:
20369 elt = build_string ("*invalid*");
20370 goto tail_recurse;
20371 }
20372
20373 /* Pad to FIELD_WIDTH. */
20374 if (field_width > 0 && n < field_width)
20375 {
20376 switch (mode_line_target)
20377 {
20378 case MODE_LINE_NOPROP:
20379 case MODE_LINE_TITLE:
20380 n += store_mode_line_noprop ("", field_width - n, 0);
20381 break;
20382 case MODE_LINE_STRING:
20383 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20384 break;
20385 case MODE_LINE_DISPLAY:
20386 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20387 0, 0, 0);
20388 break;
20389 }
20390 }
20391
20392 return n;
20393 }
20394
20395 /* Store a mode-line string element in mode_line_string_list.
20396
20397 If STRING is non-null, display that C string. Otherwise, the Lisp
20398 string LISP_STRING is displayed.
20399
20400 FIELD_WIDTH is the minimum number of output glyphs to produce.
20401 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20402 with spaces. FIELD_WIDTH <= 0 means don't pad.
20403
20404 PRECISION is the maximum number of characters to output from
20405 STRING. PRECISION <= 0 means don't truncate the string.
20406
20407 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20408 properties to the string.
20409
20410 PROPS are the properties to add to the string.
20411 The mode_line_string_face face property is always added to the string.
20412 */
20413
20414 static int
20415 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20416 int field_width, int precision, Lisp_Object props)
20417 {
20418 ptrdiff_t len;
20419 int n = 0;
20420
20421 if (string != NULL)
20422 {
20423 len = strlen (string);
20424 if (precision > 0 && len > precision)
20425 len = precision;
20426 lisp_string = make_string (string, len);
20427 if (NILP (props))
20428 props = mode_line_string_face_prop;
20429 else if (!NILP (mode_line_string_face))
20430 {
20431 Lisp_Object face = Fplist_get (props, Qface);
20432 props = Fcopy_sequence (props);
20433 if (NILP (face))
20434 face = mode_line_string_face;
20435 else
20436 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20437 props = Fplist_put (props, Qface, face);
20438 }
20439 Fadd_text_properties (make_number (0), make_number (len),
20440 props, lisp_string);
20441 }
20442 else
20443 {
20444 len = XFASTINT (Flength (lisp_string));
20445 if (precision > 0 && len > precision)
20446 {
20447 len = precision;
20448 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20449 precision = -1;
20450 }
20451 if (!NILP (mode_line_string_face))
20452 {
20453 Lisp_Object face;
20454 if (NILP (props))
20455 props = Ftext_properties_at (make_number (0), lisp_string);
20456 face = Fplist_get (props, Qface);
20457 if (NILP (face))
20458 face = mode_line_string_face;
20459 else
20460 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20461 props = Fcons (Qface, Fcons (face, Qnil));
20462 if (copy_string)
20463 lisp_string = Fcopy_sequence (lisp_string);
20464 }
20465 if (!NILP (props))
20466 Fadd_text_properties (make_number (0), make_number (len),
20467 props, lisp_string);
20468 }
20469
20470 if (len > 0)
20471 {
20472 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20473 n += len;
20474 }
20475
20476 if (field_width > len)
20477 {
20478 field_width -= len;
20479 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20480 if (!NILP (props))
20481 Fadd_text_properties (make_number (0), make_number (field_width),
20482 props, lisp_string);
20483 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20484 n += field_width;
20485 }
20486
20487 return n;
20488 }
20489
20490
20491 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20492 1, 4, 0,
20493 doc: /* Format a string out of a mode line format specification.
20494 First arg FORMAT specifies the mode line format (see `mode-line-format'
20495 for details) to use.
20496
20497 By default, the format is evaluated for the currently selected window.
20498
20499 Optional second arg FACE specifies the face property to put on all
20500 characters for which no face is specified. The value nil means the
20501 default face. The value t means whatever face the window's mode line
20502 currently uses (either `mode-line' or `mode-line-inactive',
20503 depending on whether the window is the selected window or not).
20504 An integer value means the value string has no text
20505 properties.
20506
20507 Optional third and fourth args WINDOW and BUFFER specify the window
20508 and buffer to use as the context for the formatting (defaults
20509 are the selected window and the WINDOW's buffer). */)
20510 (Lisp_Object format, Lisp_Object face,
20511 Lisp_Object window, Lisp_Object buffer)
20512 {
20513 struct it it;
20514 int len;
20515 struct window *w;
20516 struct buffer *old_buffer = NULL;
20517 int face_id;
20518 int no_props = INTEGERP (face);
20519 ptrdiff_t count = SPECPDL_INDEX ();
20520 Lisp_Object str;
20521 int string_start = 0;
20522
20523 if (NILP (window))
20524 window = selected_window;
20525 CHECK_WINDOW (window);
20526 w = XWINDOW (window);
20527
20528 if (NILP (buffer))
20529 buffer = w->buffer;
20530 CHECK_BUFFER (buffer);
20531
20532 /* Make formatting the modeline a non-op when noninteractive, otherwise
20533 there will be problems later caused by a partially initialized frame. */
20534 if (NILP (format) || noninteractive)
20535 return empty_unibyte_string;
20536
20537 if (no_props)
20538 face = Qnil;
20539
20540 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20541 : EQ (face, Qt) ? (EQ (window, selected_window)
20542 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20543 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20544 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20545 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20546 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20547 : DEFAULT_FACE_ID;
20548
20549 if (XBUFFER (buffer) != current_buffer)
20550 old_buffer = current_buffer;
20551
20552 /* Save things including mode_line_proptrans_alist,
20553 and set that to nil so that we don't alter the outer value. */
20554 record_unwind_protect (unwind_format_mode_line,
20555 format_mode_line_unwind_data
20556 (old_buffer, selected_window, 1));
20557 mode_line_proptrans_alist = Qnil;
20558
20559 Fselect_window (window, Qt);
20560 if (old_buffer)
20561 set_buffer_internal_1 (XBUFFER (buffer));
20562
20563 init_iterator (&it, w, -1, -1, NULL, face_id);
20564
20565 if (no_props)
20566 {
20567 mode_line_target = MODE_LINE_NOPROP;
20568 mode_line_string_face_prop = Qnil;
20569 mode_line_string_list = Qnil;
20570 string_start = MODE_LINE_NOPROP_LEN (0);
20571 }
20572 else
20573 {
20574 mode_line_target = MODE_LINE_STRING;
20575 mode_line_string_list = Qnil;
20576 mode_line_string_face = face;
20577 mode_line_string_face_prop
20578 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20579 }
20580
20581 push_kboard (FRAME_KBOARD (it.f));
20582 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20583 pop_kboard ();
20584
20585 if (no_props)
20586 {
20587 len = MODE_LINE_NOPROP_LEN (string_start);
20588 str = make_string (mode_line_noprop_buf + string_start, len);
20589 }
20590 else
20591 {
20592 mode_line_string_list = Fnreverse (mode_line_string_list);
20593 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20594 empty_unibyte_string);
20595 }
20596
20597 unbind_to (count, Qnil);
20598 return str;
20599 }
20600
20601 /* Write a null-terminated, right justified decimal representation of
20602 the positive integer D to BUF using a minimal field width WIDTH. */
20603
20604 static void
20605 pint2str (register char *buf, register int width, register ptrdiff_t d)
20606 {
20607 register char *p = buf;
20608
20609 if (d <= 0)
20610 *p++ = '0';
20611 else
20612 {
20613 while (d > 0)
20614 {
20615 *p++ = d % 10 + '0';
20616 d /= 10;
20617 }
20618 }
20619
20620 for (width -= (int) (p - buf); width > 0; --width)
20621 *p++ = ' ';
20622 *p-- = '\0';
20623 while (p > buf)
20624 {
20625 d = *buf;
20626 *buf++ = *p;
20627 *p-- = d;
20628 }
20629 }
20630
20631 /* Write a null-terminated, right justified decimal and "human
20632 readable" representation of the nonnegative integer D to BUF using
20633 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20634
20635 static const char power_letter[] =
20636 {
20637 0, /* no letter */
20638 'k', /* kilo */
20639 'M', /* mega */
20640 'G', /* giga */
20641 'T', /* tera */
20642 'P', /* peta */
20643 'E', /* exa */
20644 'Z', /* zetta */
20645 'Y' /* yotta */
20646 };
20647
20648 static void
20649 pint2hrstr (char *buf, int width, ptrdiff_t d)
20650 {
20651 /* We aim to represent the nonnegative integer D as
20652 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20653 ptrdiff_t quotient = d;
20654 int remainder = 0;
20655 /* -1 means: do not use TENTHS. */
20656 int tenths = -1;
20657 int exponent = 0;
20658
20659 /* Length of QUOTIENT.TENTHS as a string. */
20660 int length;
20661
20662 char * psuffix;
20663 char * p;
20664
20665 if (1000 <= quotient)
20666 {
20667 /* Scale to the appropriate EXPONENT. */
20668 do
20669 {
20670 remainder = quotient % 1000;
20671 quotient /= 1000;
20672 exponent++;
20673 }
20674 while (1000 <= quotient);
20675
20676 /* Round to nearest and decide whether to use TENTHS or not. */
20677 if (quotient <= 9)
20678 {
20679 tenths = remainder / 100;
20680 if (50 <= remainder % 100)
20681 {
20682 if (tenths < 9)
20683 tenths++;
20684 else
20685 {
20686 quotient++;
20687 if (quotient == 10)
20688 tenths = -1;
20689 else
20690 tenths = 0;
20691 }
20692 }
20693 }
20694 else
20695 if (500 <= remainder)
20696 {
20697 if (quotient < 999)
20698 quotient++;
20699 else
20700 {
20701 quotient = 1;
20702 exponent++;
20703 tenths = 0;
20704 }
20705 }
20706 }
20707
20708 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20709 if (tenths == -1 && quotient <= 99)
20710 if (quotient <= 9)
20711 length = 1;
20712 else
20713 length = 2;
20714 else
20715 length = 3;
20716 p = psuffix = buf + max (width, length);
20717
20718 /* Print EXPONENT. */
20719 *psuffix++ = power_letter[exponent];
20720 *psuffix = '\0';
20721
20722 /* Print TENTHS. */
20723 if (tenths >= 0)
20724 {
20725 *--p = '0' + tenths;
20726 *--p = '.';
20727 }
20728
20729 /* Print QUOTIENT. */
20730 do
20731 {
20732 int digit = quotient % 10;
20733 *--p = '0' + digit;
20734 }
20735 while ((quotient /= 10) != 0);
20736
20737 /* Print leading spaces. */
20738 while (buf < p)
20739 *--p = ' ';
20740 }
20741
20742 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20743 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20744 type of CODING_SYSTEM. Return updated pointer into BUF. */
20745
20746 static unsigned char invalid_eol_type[] = "(*invalid*)";
20747
20748 static char *
20749 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20750 {
20751 Lisp_Object val;
20752 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20753 const unsigned char *eol_str;
20754 int eol_str_len;
20755 /* The EOL conversion we are using. */
20756 Lisp_Object eoltype;
20757
20758 val = CODING_SYSTEM_SPEC (coding_system);
20759 eoltype = Qnil;
20760
20761 if (!VECTORP (val)) /* Not yet decided. */
20762 {
20763 if (multibyte)
20764 *buf++ = '-';
20765 if (eol_flag)
20766 eoltype = eol_mnemonic_undecided;
20767 /* Don't mention EOL conversion if it isn't decided. */
20768 }
20769 else
20770 {
20771 Lisp_Object attrs;
20772 Lisp_Object eolvalue;
20773
20774 attrs = AREF (val, 0);
20775 eolvalue = AREF (val, 2);
20776
20777 if (multibyte)
20778 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20779
20780 if (eol_flag)
20781 {
20782 /* The EOL conversion that is normal on this system. */
20783
20784 if (NILP (eolvalue)) /* Not yet decided. */
20785 eoltype = eol_mnemonic_undecided;
20786 else if (VECTORP (eolvalue)) /* Not yet decided. */
20787 eoltype = eol_mnemonic_undecided;
20788 else /* eolvalue is Qunix, Qdos, or Qmac. */
20789 eoltype = (EQ (eolvalue, Qunix)
20790 ? eol_mnemonic_unix
20791 : (EQ (eolvalue, Qdos) == 1
20792 ? eol_mnemonic_dos : eol_mnemonic_mac));
20793 }
20794 }
20795
20796 if (eol_flag)
20797 {
20798 /* Mention the EOL conversion if it is not the usual one. */
20799 if (STRINGP (eoltype))
20800 {
20801 eol_str = SDATA (eoltype);
20802 eol_str_len = SBYTES (eoltype);
20803 }
20804 else if (CHARACTERP (eoltype))
20805 {
20806 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20807 int c = XFASTINT (eoltype);
20808 eol_str_len = CHAR_STRING (c, tmp);
20809 eol_str = tmp;
20810 }
20811 else
20812 {
20813 eol_str = invalid_eol_type;
20814 eol_str_len = sizeof (invalid_eol_type) - 1;
20815 }
20816 memcpy (buf, eol_str, eol_str_len);
20817 buf += eol_str_len;
20818 }
20819
20820 return buf;
20821 }
20822
20823 /* Return a string for the output of a mode line %-spec for window W,
20824 generated by character C. FIELD_WIDTH > 0 means pad the string
20825 returned with spaces to that value. Return a Lisp string in
20826 *STRING if the resulting string is taken from that Lisp string.
20827
20828 Note we operate on the current buffer for most purposes,
20829 the exception being w->base_line_pos. */
20830
20831 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20832
20833 static const char *
20834 decode_mode_spec (struct window *w, register int c, int field_width,
20835 Lisp_Object *string)
20836 {
20837 Lisp_Object obj;
20838 struct frame *f = XFRAME (WINDOW_FRAME (w));
20839 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20840 struct buffer *b = current_buffer;
20841
20842 obj = Qnil;
20843 *string = Qnil;
20844
20845 switch (c)
20846 {
20847 case '*':
20848 if (!NILP (BVAR (b, read_only)))
20849 return "%";
20850 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20851 return "*";
20852 return "-";
20853
20854 case '+':
20855 /* This differs from %* only for a modified read-only buffer. */
20856 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20857 return "*";
20858 if (!NILP (BVAR (b, read_only)))
20859 return "%";
20860 return "-";
20861
20862 case '&':
20863 /* This differs from %* in ignoring read-only-ness. */
20864 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20865 return "*";
20866 return "-";
20867
20868 case '%':
20869 return "%";
20870
20871 case '[':
20872 {
20873 int i;
20874 char *p;
20875
20876 if (command_loop_level > 5)
20877 return "[[[... ";
20878 p = decode_mode_spec_buf;
20879 for (i = 0; i < command_loop_level; i++)
20880 *p++ = '[';
20881 *p = 0;
20882 return decode_mode_spec_buf;
20883 }
20884
20885 case ']':
20886 {
20887 int i;
20888 char *p;
20889
20890 if (command_loop_level > 5)
20891 return " ...]]]";
20892 p = decode_mode_spec_buf;
20893 for (i = 0; i < command_loop_level; i++)
20894 *p++ = ']';
20895 *p = 0;
20896 return decode_mode_spec_buf;
20897 }
20898
20899 case '-':
20900 {
20901 register int i;
20902
20903 /* Let lots_of_dashes be a string of infinite length. */
20904 if (mode_line_target == MODE_LINE_NOPROP ||
20905 mode_line_target == MODE_LINE_STRING)
20906 return "--";
20907 if (field_width <= 0
20908 || field_width > sizeof (lots_of_dashes))
20909 {
20910 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20911 decode_mode_spec_buf[i] = '-';
20912 decode_mode_spec_buf[i] = '\0';
20913 return decode_mode_spec_buf;
20914 }
20915 else
20916 return lots_of_dashes;
20917 }
20918
20919 case 'b':
20920 obj = BVAR (b, name);
20921 break;
20922
20923 case 'c':
20924 /* %c and %l are ignored in `frame-title-format'.
20925 (In redisplay_internal, the frame title is drawn _before_ the
20926 windows are updated, so the stuff which depends on actual
20927 window contents (such as %l) may fail to render properly, or
20928 even crash emacs.) */
20929 if (mode_line_target == MODE_LINE_TITLE)
20930 return "";
20931 else
20932 {
20933 ptrdiff_t col = current_column ();
20934 w->column_number_displayed = make_number (col);
20935 pint2str (decode_mode_spec_buf, field_width, col);
20936 return decode_mode_spec_buf;
20937 }
20938
20939 case 'e':
20940 #ifndef SYSTEM_MALLOC
20941 {
20942 if (NILP (Vmemory_full))
20943 return "";
20944 else
20945 return "!MEM FULL! ";
20946 }
20947 #else
20948 return "";
20949 #endif
20950
20951 case 'F':
20952 /* %F displays the frame name. */
20953 if (!NILP (f->title))
20954 return SSDATA (f->title);
20955 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20956 return SSDATA (f->name);
20957 return "Emacs";
20958
20959 case 'f':
20960 obj = BVAR (b, filename);
20961 break;
20962
20963 case 'i':
20964 {
20965 ptrdiff_t size = ZV - BEGV;
20966 pint2str (decode_mode_spec_buf, field_width, size);
20967 return decode_mode_spec_buf;
20968 }
20969
20970 case 'I':
20971 {
20972 ptrdiff_t size = ZV - BEGV;
20973 pint2hrstr (decode_mode_spec_buf, field_width, size);
20974 return decode_mode_spec_buf;
20975 }
20976
20977 case 'l':
20978 {
20979 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
20980 ptrdiff_t topline, nlines, height;
20981 ptrdiff_t junk;
20982
20983 /* %c and %l are ignored in `frame-title-format'. */
20984 if (mode_line_target == MODE_LINE_TITLE)
20985 return "";
20986
20987 startpos = XMARKER (w->start)->charpos;
20988 startpos_byte = marker_byte_position (w->start);
20989 height = WINDOW_TOTAL_LINES (w);
20990
20991 /* If we decided that this buffer isn't suitable for line numbers,
20992 don't forget that too fast. */
20993 if (EQ (w->base_line_pos, w->buffer))
20994 goto no_value;
20995 /* But do forget it, if the window shows a different buffer now. */
20996 else if (BUFFERP (w->base_line_pos))
20997 w->base_line_pos = Qnil;
20998
20999 /* If the buffer is very big, don't waste time. */
21000 if (INTEGERP (Vline_number_display_limit)
21001 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21002 {
21003 w->base_line_pos = Qnil;
21004 w->base_line_number = Qnil;
21005 goto no_value;
21006 }
21007
21008 if (INTEGERP (w->base_line_number)
21009 && INTEGERP (w->base_line_pos)
21010 && XFASTINT (w->base_line_pos) <= startpos)
21011 {
21012 line = XFASTINT (w->base_line_number);
21013 linepos = XFASTINT (w->base_line_pos);
21014 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21015 }
21016 else
21017 {
21018 line = 1;
21019 linepos = BUF_BEGV (b);
21020 linepos_byte = BUF_BEGV_BYTE (b);
21021 }
21022
21023 /* Count lines from base line to window start position. */
21024 nlines = display_count_lines (linepos_byte,
21025 startpos_byte,
21026 startpos, &junk);
21027
21028 topline = nlines + line;
21029
21030 /* Determine a new base line, if the old one is too close
21031 or too far away, or if we did not have one.
21032 "Too close" means it's plausible a scroll-down would
21033 go back past it. */
21034 if (startpos == BUF_BEGV (b))
21035 {
21036 w->base_line_number = make_number (topline);
21037 w->base_line_pos = make_number (BUF_BEGV (b));
21038 }
21039 else if (nlines < height + 25 || nlines > height * 3 + 50
21040 || linepos == BUF_BEGV (b))
21041 {
21042 ptrdiff_t limit = BUF_BEGV (b);
21043 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21044 ptrdiff_t position;
21045 ptrdiff_t distance =
21046 (height * 2 + 30) * line_number_display_limit_width;
21047
21048 if (startpos - distance > limit)
21049 {
21050 limit = startpos - distance;
21051 limit_byte = CHAR_TO_BYTE (limit);
21052 }
21053
21054 nlines = display_count_lines (startpos_byte,
21055 limit_byte,
21056 - (height * 2 + 30),
21057 &position);
21058 /* If we couldn't find the lines we wanted within
21059 line_number_display_limit_width chars per line,
21060 give up on line numbers for this window. */
21061 if (position == limit_byte && limit == startpos - distance)
21062 {
21063 w->base_line_pos = w->buffer;
21064 w->base_line_number = Qnil;
21065 goto no_value;
21066 }
21067
21068 w->base_line_number = make_number (topline - nlines);
21069 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21070 }
21071
21072 /* Now count lines from the start pos to point. */
21073 nlines = display_count_lines (startpos_byte,
21074 PT_BYTE, PT, &junk);
21075
21076 /* Record that we did display the line number. */
21077 line_number_displayed = 1;
21078
21079 /* Make the string to show. */
21080 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21081 return decode_mode_spec_buf;
21082 no_value:
21083 {
21084 char* p = decode_mode_spec_buf;
21085 int pad = field_width - 2;
21086 while (pad-- > 0)
21087 *p++ = ' ';
21088 *p++ = '?';
21089 *p++ = '?';
21090 *p = '\0';
21091 return decode_mode_spec_buf;
21092 }
21093 }
21094 break;
21095
21096 case 'm':
21097 obj = BVAR (b, mode_name);
21098 break;
21099
21100 case 'n':
21101 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21102 return " Narrow";
21103 break;
21104
21105 case 'p':
21106 {
21107 ptrdiff_t pos = marker_position (w->start);
21108 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21109
21110 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21111 {
21112 if (pos <= BUF_BEGV (b))
21113 return "All";
21114 else
21115 return "Bottom";
21116 }
21117 else if (pos <= BUF_BEGV (b))
21118 return "Top";
21119 else
21120 {
21121 if (total > 1000000)
21122 /* Do it differently for a large value, to avoid overflow. */
21123 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21124 else
21125 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21126 /* We can't normally display a 3-digit number,
21127 so get us a 2-digit number that is close. */
21128 if (total == 100)
21129 total = 99;
21130 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21131 return decode_mode_spec_buf;
21132 }
21133 }
21134
21135 /* Display percentage of size above the bottom of the screen. */
21136 case 'P':
21137 {
21138 ptrdiff_t toppos = marker_position (w->start);
21139 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21140 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21141
21142 if (botpos >= BUF_ZV (b))
21143 {
21144 if (toppos <= BUF_BEGV (b))
21145 return "All";
21146 else
21147 return "Bottom";
21148 }
21149 else
21150 {
21151 if (total > 1000000)
21152 /* Do it differently for a large value, to avoid overflow. */
21153 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21154 else
21155 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21156 /* We can't normally display a 3-digit number,
21157 so get us a 2-digit number that is close. */
21158 if (total == 100)
21159 total = 99;
21160 if (toppos <= BUF_BEGV (b))
21161 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21162 else
21163 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21164 return decode_mode_spec_buf;
21165 }
21166 }
21167
21168 case 's':
21169 /* status of process */
21170 obj = Fget_buffer_process (Fcurrent_buffer ());
21171 if (NILP (obj))
21172 return "no process";
21173 #ifndef MSDOS
21174 obj = Fsymbol_name (Fprocess_status (obj));
21175 #endif
21176 break;
21177
21178 case '@':
21179 {
21180 ptrdiff_t count = inhibit_garbage_collection ();
21181 Lisp_Object val = call1 (intern ("file-remote-p"),
21182 BVAR (current_buffer, directory));
21183 unbind_to (count, Qnil);
21184
21185 if (NILP (val))
21186 return "-";
21187 else
21188 return "@";
21189 }
21190
21191 case 't': /* indicate TEXT or BINARY */
21192 return "T";
21193
21194 case 'z':
21195 /* coding-system (not including end-of-line format) */
21196 case 'Z':
21197 /* coding-system (including end-of-line type) */
21198 {
21199 int eol_flag = (c == 'Z');
21200 char *p = decode_mode_spec_buf;
21201
21202 if (! FRAME_WINDOW_P (f))
21203 {
21204 /* No need to mention EOL here--the terminal never needs
21205 to do EOL conversion. */
21206 p = decode_mode_spec_coding (CODING_ID_NAME
21207 (FRAME_KEYBOARD_CODING (f)->id),
21208 p, 0);
21209 p = decode_mode_spec_coding (CODING_ID_NAME
21210 (FRAME_TERMINAL_CODING (f)->id),
21211 p, 0);
21212 }
21213 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21214 p, eol_flag);
21215
21216 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21217 #ifdef subprocesses
21218 obj = Fget_buffer_process (Fcurrent_buffer ());
21219 if (PROCESSP (obj))
21220 {
21221 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21222 p, eol_flag);
21223 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21224 p, eol_flag);
21225 }
21226 #endif /* subprocesses */
21227 #endif /* 0 */
21228 *p = 0;
21229 return decode_mode_spec_buf;
21230 }
21231 }
21232
21233 if (STRINGP (obj))
21234 {
21235 *string = obj;
21236 return SSDATA (obj);
21237 }
21238 else
21239 return "";
21240 }
21241
21242
21243 /* Count up to COUNT lines starting from START_BYTE.
21244 But don't go beyond LIMIT_BYTE.
21245 Return the number of lines thus found (always nonnegative).
21246
21247 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21248
21249 static ptrdiff_t
21250 display_count_lines (ptrdiff_t start_byte,
21251 ptrdiff_t limit_byte, ptrdiff_t count,
21252 ptrdiff_t *byte_pos_ptr)
21253 {
21254 register unsigned char *cursor;
21255 unsigned char *base;
21256
21257 register ptrdiff_t ceiling;
21258 register unsigned char *ceiling_addr;
21259 ptrdiff_t orig_count = count;
21260
21261 /* If we are not in selective display mode,
21262 check only for newlines. */
21263 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21264 && !INTEGERP (BVAR (current_buffer, selective_display)));
21265
21266 if (count > 0)
21267 {
21268 while (start_byte < limit_byte)
21269 {
21270 ceiling = BUFFER_CEILING_OF (start_byte);
21271 ceiling = min (limit_byte - 1, ceiling);
21272 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21273 base = (cursor = BYTE_POS_ADDR (start_byte));
21274 while (1)
21275 {
21276 if (selective_display)
21277 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21278 ;
21279 else
21280 while (*cursor != '\n' && ++cursor != ceiling_addr)
21281 ;
21282
21283 if (cursor != ceiling_addr)
21284 {
21285 if (--count == 0)
21286 {
21287 start_byte += cursor - base + 1;
21288 *byte_pos_ptr = start_byte;
21289 return orig_count;
21290 }
21291 else
21292 if (++cursor == ceiling_addr)
21293 break;
21294 }
21295 else
21296 break;
21297 }
21298 start_byte += cursor - base;
21299 }
21300 }
21301 else
21302 {
21303 while (start_byte > limit_byte)
21304 {
21305 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21306 ceiling = max (limit_byte, ceiling);
21307 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21308 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21309 while (1)
21310 {
21311 if (selective_display)
21312 while (--cursor != ceiling_addr
21313 && *cursor != '\n' && *cursor != 015)
21314 ;
21315 else
21316 while (--cursor != ceiling_addr && *cursor != '\n')
21317 ;
21318
21319 if (cursor != ceiling_addr)
21320 {
21321 if (++count == 0)
21322 {
21323 start_byte += cursor - base + 1;
21324 *byte_pos_ptr = start_byte;
21325 /* When scanning backwards, we should
21326 not count the newline posterior to which we stop. */
21327 return - orig_count - 1;
21328 }
21329 }
21330 else
21331 break;
21332 }
21333 /* Here we add 1 to compensate for the last decrement
21334 of CURSOR, which took it past the valid range. */
21335 start_byte += cursor - base + 1;
21336 }
21337 }
21338
21339 *byte_pos_ptr = limit_byte;
21340
21341 if (count < 0)
21342 return - orig_count + count;
21343 return orig_count - count;
21344
21345 }
21346
21347
21348 \f
21349 /***********************************************************************
21350 Displaying strings
21351 ***********************************************************************/
21352
21353 /* Display a NUL-terminated string, starting with index START.
21354
21355 If STRING is non-null, display that C string. Otherwise, the Lisp
21356 string LISP_STRING is displayed. There's a case that STRING is
21357 non-null and LISP_STRING is not nil. It means STRING is a string
21358 data of LISP_STRING. In that case, we display LISP_STRING while
21359 ignoring its text properties.
21360
21361 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21362 FACE_STRING. Display STRING or LISP_STRING with the face at
21363 FACE_STRING_POS in FACE_STRING:
21364
21365 Display the string in the environment given by IT, but use the
21366 standard display table, temporarily.
21367
21368 FIELD_WIDTH is the minimum number of output glyphs to produce.
21369 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21370 with spaces. If STRING has more characters, more than FIELD_WIDTH
21371 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21372
21373 PRECISION is the maximum number of characters to output from
21374 STRING. PRECISION < 0 means don't truncate the string.
21375
21376 This is roughly equivalent to printf format specifiers:
21377
21378 FIELD_WIDTH PRECISION PRINTF
21379 ----------------------------------------
21380 -1 -1 %s
21381 -1 10 %.10s
21382 10 -1 %10s
21383 20 10 %20.10s
21384
21385 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21386 display them, and < 0 means obey the current buffer's value of
21387 enable_multibyte_characters.
21388
21389 Value is the number of columns displayed. */
21390
21391 static int
21392 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21393 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21394 int field_width, int precision, int max_x, int multibyte)
21395 {
21396 int hpos_at_start = it->hpos;
21397 int saved_face_id = it->face_id;
21398 struct glyph_row *row = it->glyph_row;
21399 ptrdiff_t it_charpos;
21400
21401 /* Initialize the iterator IT for iteration over STRING beginning
21402 with index START. */
21403 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21404 precision, field_width, multibyte);
21405 if (string && STRINGP (lisp_string))
21406 /* LISP_STRING is the one returned by decode_mode_spec. We should
21407 ignore its text properties. */
21408 it->stop_charpos = it->end_charpos;
21409
21410 /* If displaying STRING, set up the face of the iterator from
21411 FACE_STRING, if that's given. */
21412 if (STRINGP (face_string))
21413 {
21414 ptrdiff_t endptr;
21415 struct face *face;
21416
21417 it->face_id
21418 = face_at_string_position (it->w, face_string, face_string_pos,
21419 0, it->region_beg_charpos,
21420 it->region_end_charpos,
21421 &endptr, it->base_face_id, 0);
21422 face = FACE_FROM_ID (it->f, it->face_id);
21423 it->face_box_p = face->box != FACE_NO_BOX;
21424 }
21425
21426 /* Set max_x to the maximum allowed X position. Don't let it go
21427 beyond the right edge of the window. */
21428 if (max_x <= 0)
21429 max_x = it->last_visible_x;
21430 else
21431 max_x = min (max_x, it->last_visible_x);
21432
21433 /* Skip over display elements that are not visible. because IT->w is
21434 hscrolled. */
21435 if (it->current_x < it->first_visible_x)
21436 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21437 MOVE_TO_POS | MOVE_TO_X);
21438
21439 row->ascent = it->max_ascent;
21440 row->height = it->max_ascent + it->max_descent;
21441 row->phys_ascent = it->max_phys_ascent;
21442 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21443 row->extra_line_spacing = it->max_extra_line_spacing;
21444
21445 if (STRINGP (it->string))
21446 it_charpos = IT_STRING_CHARPOS (*it);
21447 else
21448 it_charpos = IT_CHARPOS (*it);
21449
21450 /* This condition is for the case that we are called with current_x
21451 past last_visible_x. */
21452 while (it->current_x < max_x)
21453 {
21454 int x_before, x, n_glyphs_before, i, nglyphs;
21455
21456 /* Get the next display element. */
21457 if (!get_next_display_element (it))
21458 break;
21459
21460 /* Produce glyphs. */
21461 x_before = it->current_x;
21462 n_glyphs_before = row->used[TEXT_AREA];
21463 PRODUCE_GLYPHS (it);
21464
21465 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21466 i = 0;
21467 x = x_before;
21468 while (i < nglyphs)
21469 {
21470 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21471
21472 if (it->line_wrap != TRUNCATE
21473 && x + glyph->pixel_width > max_x)
21474 {
21475 /* End of continued line or max_x reached. */
21476 if (CHAR_GLYPH_PADDING_P (*glyph))
21477 {
21478 /* A wide character is unbreakable. */
21479 if (row->reversed_p)
21480 unproduce_glyphs (it, row->used[TEXT_AREA]
21481 - n_glyphs_before);
21482 row->used[TEXT_AREA] = n_glyphs_before;
21483 it->current_x = x_before;
21484 }
21485 else
21486 {
21487 if (row->reversed_p)
21488 unproduce_glyphs (it, row->used[TEXT_AREA]
21489 - (n_glyphs_before + i));
21490 row->used[TEXT_AREA] = n_glyphs_before + i;
21491 it->current_x = x;
21492 }
21493 break;
21494 }
21495 else if (x + glyph->pixel_width >= it->first_visible_x)
21496 {
21497 /* Glyph is at least partially visible. */
21498 ++it->hpos;
21499 if (x < it->first_visible_x)
21500 row->x = x - it->first_visible_x;
21501 }
21502 else
21503 {
21504 /* Glyph is off the left margin of the display area.
21505 Should not happen. */
21506 abort ();
21507 }
21508
21509 row->ascent = max (row->ascent, it->max_ascent);
21510 row->height = max (row->height, it->max_ascent + it->max_descent);
21511 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21512 row->phys_height = max (row->phys_height,
21513 it->max_phys_ascent + it->max_phys_descent);
21514 row->extra_line_spacing = max (row->extra_line_spacing,
21515 it->max_extra_line_spacing);
21516 x += glyph->pixel_width;
21517 ++i;
21518 }
21519
21520 /* Stop if max_x reached. */
21521 if (i < nglyphs)
21522 break;
21523
21524 /* Stop at line ends. */
21525 if (ITERATOR_AT_END_OF_LINE_P (it))
21526 {
21527 it->continuation_lines_width = 0;
21528 break;
21529 }
21530
21531 set_iterator_to_next (it, 1);
21532 if (STRINGP (it->string))
21533 it_charpos = IT_STRING_CHARPOS (*it);
21534 else
21535 it_charpos = IT_CHARPOS (*it);
21536
21537 /* Stop if truncating at the right edge. */
21538 if (it->line_wrap == TRUNCATE
21539 && it->current_x >= it->last_visible_x)
21540 {
21541 /* Add truncation mark, but don't do it if the line is
21542 truncated at a padding space. */
21543 if (it_charpos < it->string_nchars)
21544 {
21545 if (!FRAME_WINDOW_P (it->f))
21546 {
21547 int ii, n;
21548
21549 if (it->current_x > it->last_visible_x)
21550 {
21551 if (!row->reversed_p)
21552 {
21553 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21554 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21555 break;
21556 }
21557 else
21558 {
21559 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21560 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21561 break;
21562 unproduce_glyphs (it, ii + 1);
21563 ii = row->used[TEXT_AREA] - (ii + 1);
21564 }
21565 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21566 {
21567 row->used[TEXT_AREA] = ii;
21568 produce_special_glyphs (it, IT_TRUNCATION);
21569 }
21570 }
21571 produce_special_glyphs (it, IT_TRUNCATION);
21572 }
21573 row->truncated_on_right_p = 1;
21574 }
21575 break;
21576 }
21577 }
21578
21579 /* Maybe insert a truncation at the left. */
21580 if (it->first_visible_x
21581 && it_charpos > 0)
21582 {
21583 if (!FRAME_WINDOW_P (it->f))
21584 insert_left_trunc_glyphs (it);
21585 row->truncated_on_left_p = 1;
21586 }
21587
21588 it->face_id = saved_face_id;
21589
21590 /* Value is number of columns displayed. */
21591 return it->hpos - hpos_at_start;
21592 }
21593
21594
21595 \f
21596 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21597 appears as an element of LIST or as the car of an element of LIST.
21598 If PROPVAL is a list, compare each element against LIST in that
21599 way, and return 1/2 if any element of PROPVAL is found in LIST.
21600 Otherwise return 0. This function cannot quit.
21601 The return value is 2 if the text is invisible but with an ellipsis
21602 and 1 if it's invisible and without an ellipsis. */
21603
21604 int
21605 invisible_p (register Lisp_Object propval, Lisp_Object list)
21606 {
21607 register Lisp_Object tail, proptail;
21608
21609 for (tail = list; CONSP (tail); tail = XCDR (tail))
21610 {
21611 register Lisp_Object tem;
21612 tem = XCAR (tail);
21613 if (EQ (propval, tem))
21614 return 1;
21615 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21616 return NILP (XCDR (tem)) ? 1 : 2;
21617 }
21618
21619 if (CONSP (propval))
21620 {
21621 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21622 {
21623 Lisp_Object propelt;
21624 propelt = XCAR (proptail);
21625 for (tail = list; CONSP (tail); tail = XCDR (tail))
21626 {
21627 register Lisp_Object tem;
21628 tem = XCAR (tail);
21629 if (EQ (propelt, tem))
21630 return 1;
21631 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21632 return NILP (XCDR (tem)) ? 1 : 2;
21633 }
21634 }
21635 }
21636
21637 return 0;
21638 }
21639
21640 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21641 doc: /* Non-nil if the property makes the text invisible.
21642 POS-OR-PROP can be a marker or number, in which case it is taken to be
21643 a position in the current buffer and the value of the `invisible' property
21644 is checked; or it can be some other value, which is then presumed to be the
21645 value of the `invisible' property of the text of interest.
21646 The non-nil value returned can be t for truly invisible text or something
21647 else if the text is replaced by an ellipsis. */)
21648 (Lisp_Object pos_or_prop)
21649 {
21650 Lisp_Object prop
21651 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21652 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21653 : pos_or_prop);
21654 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21655 return (invis == 0 ? Qnil
21656 : invis == 1 ? Qt
21657 : make_number (invis));
21658 }
21659
21660 /* Calculate a width or height in pixels from a specification using
21661 the following elements:
21662
21663 SPEC ::=
21664 NUM - a (fractional) multiple of the default font width/height
21665 (NUM) - specifies exactly NUM pixels
21666 UNIT - a fixed number of pixels, see below.
21667 ELEMENT - size of a display element in pixels, see below.
21668 (NUM . SPEC) - equals NUM * SPEC
21669 (+ SPEC SPEC ...) - add pixel values
21670 (- SPEC SPEC ...) - subtract pixel values
21671 (- SPEC) - negate pixel value
21672
21673 NUM ::=
21674 INT or FLOAT - a number constant
21675 SYMBOL - use symbol's (buffer local) variable binding.
21676
21677 UNIT ::=
21678 in - pixels per inch *)
21679 mm - pixels per 1/1000 meter *)
21680 cm - pixels per 1/100 meter *)
21681 width - width of current font in pixels.
21682 height - height of current font in pixels.
21683
21684 *) using the ratio(s) defined in display-pixels-per-inch.
21685
21686 ELEMENT ::=
21687
21688 left-fringe - left fringe width in pixels
21689 right-fringe - right fringe width in pixels
21690
21691 left-margin - left margin width in pixels
21692 right-margin - right margin width in pixels
21693
21694 scroll-bar - scroll-bar area width in pixels
21695
21696 Examples:
21697
21698 Pixels corresponding to 5 inches:
21699 (5 . in)
21700
21701 Total width of non-text areas on left side of window (if scroll-bar is on left):
21702 '(space :width (+ left-fringe left-margin scroll-bar))
21703
21704 Align to first text column (in header line):
21705 '(space :align-to 0)
21706
21707 Align to middle of text area minus half the width of variable `my-image'
21708 containing a loaded image:
21709 '(space :align-to (0.5 . (- text my-image)))
21710
21711 Width of left margin minus width of 1 character in the default font:
21712 '(space :width (- left-margin 1))
21713
21714 Width of left margin minus width of 2 characters in the current font:
21715 '(space :width (- left-margin (2 . width)))
21716
21717 Center 1 character over left-margin (in header line):
21718 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21719
21720 Different ways to express width of left fringe plus left margin minus one pixel:
21721 '(space :width (- (+ left-fringe left-margin) (1)))
21722 '(space :width (+ left-fringe left-margin (- (1))))
21723 '(space :width (+ left-fringe left-margin (-1)))
21724
21725 */
21726
21727 #define NUMVAL(X) \
21728 ((INTEGERP (X) || FLOATP (X)) \
21729 ? XFLOATINT (X) \
21730 : - 1)
21731
21732 static int
21733 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21734 struct font *font, int width_p, int *align_to)
21735 {
21736 double pixels;
21737
21738 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21739 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21740
21741 if (NILP (prop))
21742 return OK_PIXELS (0);
21743
21744 xassert (FRAME_LIVE_P (it->f));
21745
21746 if (SYMBOLP (prop))
21747 {
21748 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21749 {
21750 char *unit = SSDATA (SYMBOL_NAME (prop));
21751
21752 if (unit[0] == 'i' && unit[1] == 'n')
21753 pixels = 1.0;
21754 else if (unit[0] == 'm' && unit[1] == 'm')
21755 pixels = 25.4;
21756 else if (unit[0] == 'c' && unit[1] == 'm')
21757 pixels = 2.54;
21758 else
21759 pixels = 0;
21760 if (pixels > 0)
21761 {
21762 double ppi;
21763 #ifdef HAVE_WINDOW_SYSTEM
21764 if (FRAME_WINDOW_P (it->f)
21765 && (ppi = (width_p
21766 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21767 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21768 ppi > 0))
21769 return OK_PIXELS (ppi / pixels);
21770 #endif
21771
21772 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21773 || (CONSP (Vdisplay_pixels_per_inch)
21774 && (ppi = (width_p
21775 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21776 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21777 ppi > 0)))
21778 return OK_PIXELS (ppi / pixels);
21779
21780 return 0;
21781 }
21782 }
21783
21784 #ifdef HAVE_WINDOW_SYSTEM
21785 if (EQ (prop, Qheight))
21786 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21787 if (EQ (prop, Qwidth))
21788 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21789 #else
21790 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21791 return OK_PIXELS (1);
21792 #endif
21793
21794 if (EQ (prop, Qtext))
21795 return OK_PIXELS (width_p
21796 ? window_box_width (it->w, TEXT_AREA)
21797 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21798
21799 if (align_to && *align_to < 0)
21800 {
21801 *res = 0;
21802 if (EQ (prop, Qleft))
21803 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21804 if (EQ (prop, Qright))
21805 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21806 if (EQ (prop, Qcenter))
21807 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21808 + window_box_width (it->w, TEXT_AREA) / 2);
21809 if (EQ (prop, Qleft_fringe))
21810 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21811 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21812 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21813 if (EQ (prop, Qright_fringe))
21814 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21815 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21816 : window_box_right_offset (it->w, TEXT_AREA));
21817 if (EQ (prop, Qleft_margin))
21818 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21819 if (EQ (prop, Qright_margin))
21820 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21821 if (EQ (prop, Qscroll_bar))
21822 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21823 ? 0
21824 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21825 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21826 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21827 : 0)));
21828 }
21829 else
21830 {
21831 if (EQ (prop, Qleft_fringe))
21832 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21833 if (EQ (prop, Qright_fringe))
21834 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21835 if (EQ (prop, Qleft_margin))
21836 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21837 if (EQ (prop, Qright_margin))
21838 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21839 if (EQ (prop, Qscroll_bar))
21840 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21841 }
21842
21843 prop = Fbuffer_local_value (prop, it->w->buffer);
21844 }
21845
21846 if (INTEGERP (prop) || FLOATP (prop))
21847 {
21848 int base_unit = (width_p
21849 ? FRAME_COLUMN_WIDTH (it->f)
21850 : FRAME_LINE_HEIGHT (it->f));
21851 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21852 }
21853
21854 if (CONSP (prop))
21855 {
21856 Lisp_Object car = XCAR (prop);
21857 Lisp_Object cdr = XCDR (prop);
21858
21859 if (SYMBOLP (car))
21860 {
21861 #ifdef HAVE_WINDOW_SYSTEM
21862 if (FRAME_WINDOW_P (it->f)
21863 && valid_image_p (prop))
21864 {
21865 ptrdiff_t id = lookup_image (it->f, prop);
21866 struct image *img = IMAGE_FROM_ID (it->f, id);
21867
21868 return OK_PIXELS (width_p ? img->width : img->height);
21869 }
21870 #endif
21871 if (EQ (car, Qplus) || EQ (car, Qminus))
21872 {
21873 int first = 1;
21874 double px;
21875
21876 pixels = 0;
21877 while (CONSP (cdr))
21878 {
21879 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21880 font, width_p, align_to))
21881 return 0;
21882 if (first)
21883 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21884 else
21885 pixels += px;
21886 cdr = XCDR (cdr);
21887 }
21888 if (EQ (car, Qminus))
21889 pixels = -pixels;
21890 return OK_PIXELS (pixels);
21891 }
21892
21893 car = Fbuffer_local_value (car, it->w->buffer);
21894 }
21895
21896 if (INTEGERP (car) || FLOATP (car))
21897 {
21898 double fact;
21899 pixels = XFLOATINT (car);
21900 if (NILP (cdr))
21901 return OK_PIXELS (pixels);
21902 if (calc_pixel_width_or_height (&fact, it, cdr,
21903 font, width_p, align_to))
21904 return OK_PIXELS (pixels * fact);
21905 return 0;
21906 }
21907
21908 return 0;
21909 }
21910
21911 return 0;
21912 }
21913
21914 \f
21915 /***********************************************************************
21916 Glyph Display
21917 ***********************************************************************/
21918
21919 #ifdef HAVE_WINDOW_SYSTEM
21920
21921 #if GLYPH_DEBUG
21922
21923 void
21924 dump_glyph_string (struct glyph_string *s)
21925 {
21926 fprintf (stderr, "glyph string\n");
21927 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21928 s->x, s->y, s->width, s->height);
21929 fprintf (stderr, " ybase = %d\n", s->ybase);
21930 fprintf (stderr, " hl = %d\n", s->hl);
21931 fprintf (stderr, " left overhang = %d, right = %d\n",
21932 s->left_overhang, s->right_overhang);
21933 fprintf (stderr, " nchars = %d\n", s->nchars);
21934 fprintf (stderr, " extends to end of line = %d\n",
21935 s->extends_to_end_of_line_p);
21936 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21937 fprintf (stderr, " bg width = %d\n", s->background_width);
21938 }
21939
21940 #endif /* GLYPH_DEBUG */
21941
21942 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21943 of XChar2b structures for S; it can't be allocated in
21944 init_glyph_string because it must be allocated via `alloca'. W
21945 is the window on which S is drawn. ROW and AREA are the glyph row
21946 and area within the row from which S is constructed. START is the
21947 index of the first glyph structure covered by S. HL is a
21948 face-override for drawing S. */
21949
21950 #ifdef HAVE_NTGUI
21951 #define OPTIONAL_HDC(hdc) HDC hdc,
21952 #define DECLARE_HDC(hdc) HDC hdc;
21953 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21954 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21955 #endif
21956
21957 #ifndef OPTIONAL_HDC
21958 #define OPTIONAL_HDC(hdc)
21959 #define DECLARE_HDC(hdc)
21960 #define ALLOCATE_HDC(hdc, f)
21961 #define RELEASE_HDC(hdc, f)
21962 #endif
21963
21964 static void
21965 init_glyph_string (struct glyph_string *s,
21966 OPTIONAL_HDC (hdc)
21967 XChar2b *char2b, struct window *w, struct glyph_row *row,
21968 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21969 {
21970 memset (s, 0, sizeof *s);
21971 s->w = w;
21972 s->f = XFRAME (w->frame);
21973 #ifdef HAVE_NTGUI
21974 s->hdc = hdc;
21975 #endif
21976 s->display = FRAME_X_DISPLAY (s->f);
21977 s->window = FRAME_X_WINDOW (s->f);
21978 s->char2b = char2b;
21979 s->hl = hl;
21980 s->row = row;
21981 s->area = area;
21982 s->first_glyph = row->glyphs[area] + start;
21983 s->height = row->height;
21984 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21985 s->ybase = s->y + row->ascent;
21986 }
21987
21988
21989 /* Append the list of glyph strings with head H and tail T to the list
21990 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21991
21992 static inline void
21993 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21994 struct glyph_string *h, struct glyph_string *t)
21995 {
21996 if (h)
21997 {
21998 if (*head)
21999 (*tail)->next = h;
22000 else
22001 *head = h;
22002 h->prev = *tail;
22003 *tail = t;
22004 }
22005 }
22006
22007
22008 /* Prepend the list of glyph strings with head H and tail T to the
22009 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22010 result. */
22011
22012 static inline void
22013 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22014 struct glyph_string *h, struct glyph_string *t)
22015 {
22016 if (h)
22017 {
22018 if (*head)
22019 (*head)->prev = t;
22020 else
22021 *tail = t;
22022 t->next = *head;
22023 *head = h;
22024 }
22025 }
22026
22027
22028 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22029 Set *HEAD and *TAIL to the resulting list. */
22030
22031 static inline void
22032 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22033 struct glyph_string *s)
22034 {
22035 s->next = s->prev = NULL;
22036 append_glyph_string_lists (head, tail, s, s);
22037 }
22038
22039
22040 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22041 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22042 make sure that X resources for the face returned are allocated.
22043 Value is a pointer to a realized face that is ready for display if
22044 DISPLAY_P is non-zero. */
22045
22046 static inline struct face *
22047 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22048 XChar2b *char2b, int display_p)
22049 {
22050 struct face *face = FACE_FROM_ID (f, face_id);
22051
22052 if (face->font)
22053 {
22054 unsigned code = face->font->driver->encode_char (face->font, c);
22055
22056 if (code != FONT_INVALID_CODE)
22057 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22058 else
22059 STORE_XCHAR2B (char2b, 0, 0);
22060 }
22061
22062 /* Make sure X resources of the face are allocated. */
22063 #ifdef HAVE_X_WINDOWS
22064 if (display_p)
22065 #endif
22066 {
22067 xassert (face != NULL);
22068 PREPARE_FACE_FOR_DISPLAY (f, face);
22069 }
22070
22071 return face;
22072 }
22073
22074
22075 /* Get face and two-byte form of character glyph GLYPH on frame F.
22076 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22077 a pointer to a realized face that is ready for display. */
22078
22079 static inline struct face *
22080 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22081 XChar2b *char2b, int *two_byte_p)
22082 {
22083 struct face *face;
22084
22085 xassert (glyph->type == CHAR_GLYPH);
22086 face = FACE_FROM_ID (f, glyph->face_id);
22087
22088 if (two_byte_p)
22089 *two_byte_p = 0;
22090
22091 if (face->font)
22092 {
22093 unsigned code;
22094
22095 if (CHAR_BYTE8_P (glyph->u.ch))
22096 code = CHAR_TO_BYTE8 (glyph->u.ch);
22097 else
22098 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22099
22100 if (code != FONT_INVALID_CODE)
22101 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22102 else
22103 STORE_XCHAR2B (char2b, 0, 0);
22104 }
22105
22106 /* Make sure X resources of the face are allocated. */
22107 xassert (face != NULL);
22108 PREPARE_FACE_FOR_DISPLAY (f, face);
22109 return face;
22110 }
22111
22112
22113 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22114 Return 1 if FONT has a glyph for C, otherwise return 0. */
22115
22116 static inline int
22117 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22118 {
22119 unsigned code;
22120
22121 if (CHAR_BYTE8_P (c))
22122 code = CHAR_TO_BYTE8 (c);
22123 else
22124 code = font->driver->encode_char (font, c);
22125
22126 if (code == FONT_INVALID_CODE)
22127 return 0;
22128 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22129 return 1;
22130 }
22131
22132
22133 /* Fill glyph string S with composition components specified by S->cmp.
22134
22135 BASE_FACE is the base face of the composition.
22136 S->cmp_from is the index of the first component for S.
22137
22138 OVERLAPS non-zero means S should draw the foreground only, and use
22139 its physical height for clipping. See also draw_glyphs.
22140
22141 Value is the index of a component not in S. */
22142
22143 static int
22144 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22145 int overlaps)
22146 {
22147 int i;
22148 /* For all glyphs of this composition, starting at the offset
22149 S->cmp_from, until we reach the end of the definition or encounter a
22150 glyph that requires the different face, add it to S. */
22151 struct face *face;
22152
22153 xassert (s);
22154
22155 s->for_overlaps = overlaps;
22156 s->face = NULL;
22157 s->font = NULL;
22158 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22159 {
22160 int c = COMPOSITION_GLYPH (s->cmp, i);
22161
22162 /* TAB in a composition means display glyphs with padding space
22163 on the left or right. */
22164 if (c != '\t')
22165 {
22166 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22167 -1, Qnil);
22168
22169 face = get_char_face_and_encoding (s->f, c, face_id,
22170 s->char2b + i, 1);
22171 if (face)
22172 {
22173 if (! s->face)
22174 {
22175 s->face = face;
22176 s->font = s->face->font;
22177 }
22178 else if (s->face != face)
22179 break;
22180 }
22181 }
22182 ++s->nchars;
22183 }
22184 s->cmp_to = i;
22185
22186 if (s->face == NULL)
22187 {
22188 s->face = base_face->ascii_face;
22189 s->font = s->face->font;
22190 }
22191
22192 /* All glyph strings for the same composition has the same width,
22193 i.e. the width set for the first component of the composition. */
22194 s->width = s->first_glyph->pixel_width;
22195
22196 /* If the specified font could not be loaded, use the frame's
22197 default font, but record the fact that we couldn't load it in
22198 the glyph string so that we can draw rectangles for the
22199 characters of the glyph string. */
22200 if (s->font == NULL)
22201 {
22202 s->font_not_found_p = 1;
22203 s->font = FRAME_FONT (s->f);
22204 }
22205
22206 /* Adjust base line for subscript/superscript text. */
22207 s->ybase += s->first_glyph->voffset;
22208
22209 /* This glyph string must always be drawn with 16-bit functions. */
22210 s->two_byte_p = 1;
22211
22212 return s->cmp_to;
22213 }
22214
22215 static int
22216 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22217 int start, int end, int overlaps)
22218 {
22219 struct glyph *glyph, *last;
22220 Lisp_Object lgstring;
22221 int i;
22222
22223 s->for_overlaps = overlaps;
22224 glyph = s->row->glyphs[s->area] + start;
22225 last = s->row->glyphs[s->area] + end;
22226 s->cmp_id = glyph->u.cmp.id;
22227 s->cmp_from = glyph->slice.cmp.from;
22228 s->cmp_to = glyph->slice.cmp.to + 1;
22229 s->face = FACE_FROM_ID (s->f, face_id);
22230 lgstring = composition_gstring_from_id (s->cmp_id);
22231 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22232 glyph++;
22233 while (glyph < last
22234 && glyph->u.cmp.automatic
22235 && glyph->u.cmp.id == s->cmp_id
22236 && s->cmp_to == glyph->slice.cmp.from)
22237 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22238
22239 for (i = s->cmp_from; i < s->cmp_to; i++)
22240 {
22241 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22242 unsigned code = LGLYPH_CODE (lglyph);
22243
22244 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22245 }
22246 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22247 return glyph - s->row->glyphs[s->area];
22248 }
22249
22250
22251 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22252 See the comment of fill_glyph_string for arguments.
22253 Value is the index of the first glyph not in S. */
22254
22255
22256 static int
22257 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22258 int start, int end, int overlaps)
22259 {
22260 struct glyph *glyph, *last;
22261 int voffset;
22262
22263 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22264 s->for_overlaps = overlaps;
22265 glyph = s->row->glyphs[s->area] + start;
22266 last = s->row->glyphs[s->area] + end;
22267 voffset = glyph->voffset;
22268 s->face = FACE_FROM_ID (s->f, face_id);
22269 s->font = s->face->font;
22270 s->nchars = 1;
22271 s->width = glyph->pixel_width;
22272 glyph++;
22273 while (glyph < last
22274 && glyph->type == GLYPHLESS_GLYPH
22275 && glyph->voffset == voffset
22276 && glyph->face_id == face_id)
22277 {
22278 s->nchars++;
22279 s->width += glyph->pixel_width;
22280 glyph++;
22281 }
22282 s->ybase += voffset;
22283 return glyph - s->row->glyphs[s->area];
22284 }
22285
22286
22287 /* Fill glyph string S from a sequence of character glyphs.
22288
22289 FACE_ID is the face id of the string. START is the index of the
22290 first glyph to consider, END is the index of the last + 1.
22291 OVERLAPS non-zero means S should draw the foreground only, and use
22292 its physical height for clipping. See also draw_glyphs.
22293
22294 Value is the index of the first glyph not in S. */
22295
22296 static int
22297 fill_glyph_string (struct glyph_string *s, int face_id,
22298 int start, int end, int overlaps)
22299 {
22300 struct glyph *glyph, *last;
22301 int voffset;
22302 int glyph_not_available_p;
22303
22304 xassert (s->f == XFRAME (s->w->frame));
22305 xassert (s->nchars == 0);
22306 xassert (start >= 0 && end > start);
22307
22308 s->for_overlaps = overlaps;
22309 glyph = s->row->glyphs[s->area] + start;
22310 last = s->row->glyphs[s->area] + end;
22311 voffset = glyph->voffset;
22312 s->padding_p = glyph->padding_p;
22313 glyph_not_available_p = glyph->glyph_not_available_p;
22314
22315 while (glyph < last
22316 && glyph->type == CHAR_GLYPH
22317 && glyph->voffset == voffset
22318 /* Same face id implies same font, nowadays. */
22319 && glyph->face_id == face_id
22320 && glyph->glyph_not_available_p == glyph_not_available_p)
22321 {
22322 int two_byte_p;
22323
22324 s->face = get_glyph_face_and_encoding (s->f, glyph,
22325 s->char2b + s->nchars,
22326 &two_byte_p);
22327 s->two_byte_p = two_byte_p;
22328 ++s->nchars;
22329 xassert (s->nchars <= end - start);
22330 s->width += glyph->pixel_width;
22331 if (glyph++->padding_p != s->padding_p)
22332 break;
22333 }
22334
22335 s->font = s->face->font;
22336
22337 /* If the specified font could not be loaded, use the frame's font,
22338 but record the fact that we couldn't load it in
22339 S->font_not_found_p so that we can draw rectangles for the
22340 characters of the glyph string. */
22341 if (s->font == NULL || glyph_not_available_p)
22342 {
22343 s->font_not_found_p = 1;
22344 s->font = FRAME_FONT (s->f);
22345 }
22346
22347 /* Adjust base line for subscript/superscript text. */
22348 s->ybase += voffset;
22349
22350 xassert (s->face && s->face->gc);
22351 return glyph - s->row->glyphs[s->area];
22352 }
22353
22354
22355 /* Fill glyph string S from image glyph S->first_glyph. */
22356
22357 static void
22358 fill_image_glyph_string (struct glyph_string *s)
22359 {
22360 xassert (s->first_glyph->type == IMAGE_GLYPH);
22361 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22362 xassert (s->img);
22363 s->slice = s->first_glyph->slice.img;
22364 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22365 s->font = s->face->font;
22366 s->width = s->first_glyph->pixel_width;
22367
22368 /* Adjust base line for subscript/superscript text. */
22369 s->ybase += s->first_glyph->voffset;
22370 }
22371
22372
22373 /* Fill glyph string S from a sequence of stretch glyphs.
22374
22375 START is the index of the first glyph to consider,
22376 END is the index of the last + 1.
22377
22378 Value is the index of the first glyph not in S. */
22379
22380 static int
22381 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22382 {
22383 struct glyph *glyph, *last;
22384 int voffset, face_id;
22385
22386 xassert (s->first_glyph->type == STRETCH_GLYPH);
22387
22388 glyph = s->row->glyphs[s->area] + start;
22389 last = s->row->glyphs[s->area] + end;
22390 face_id = glyph->face_id;
22391 s->face = FACE_FROM_ID (s->f, face_id);
22392 s->font = s->face->font;
22393 s->width = glyph->pixel_width;
22394 s->nchars = 1;
22395 voffset = glyph->voffset;
22396
22397 for (++glyph;
22398 (glyph < last
22399 && glyph->type == STRETCH_GLYPH
22400 && glyph->voffset == voffset
22401 && glyph->face_id == face_id);
22402 ++glyph)
22403 s->width += glyph->pixel_width;
22404
22405 /* Adjust base line for subscript/superscript text. */
22406 s->ybase += voffset;
22407
22408 /* The case that face->gc == 0 is handled when drawing the glyph
22409 string by calling PREPARE_FACE_FOR_DISPLAY. */
22410 xassert (s->face);
22411 return glyph - s->row->glyphs[s->area];
22412 }
22413
22414 static struct font_metrics *
22415 get_per_char_metric (struct font *font, XChar2b *char2b)
22416 {
22417 static struct font_metrics metrics;
22418 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22419
22420 if (! font || code == FONT_INVALID_CODE)
22421 return NULL;
22422 font->driver->text_extents (font, &code, 1, &metrics);
22423 return &metrics;
22424 }
22425
22426 /* EXPORT for RIF:
22427 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22428 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22429 assumed to be zero. */
22430
22431 void
22432 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22433 {
22434 *left = *right = 0;
22435
22436 if (glyph->type == CHAR_GLYPH)
22437 {
22438 struct face *face;
22439 XChar2b char2b;
22440 struct font_metrics *pcm;
22441
22442 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22443 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22444 {
22445 if (pcm->rbearing > pcm->width)
22446 *right = pcm->rbearing - pcm->width;
22447 if (pcm->lbearing < 0)
22448 *left = -pcm->lbearing;
22449 }
22450 }
22451 else if (glyph->type == COMPOSITE_GLYPH)
22452 {
22453 if (! glyph->u.cmp.automatic)
22454 {
22455 struct composition *cmp = composition_table[glyph->u.cmp.id];
22456
22457 if (cmp->rbearing > cmp->pixel_width)
22458 *right = cmp->rbearing - cmp->pixel_width;
22459 if (cmp->lbearing < 0)
22460 *left = - cmp->lbearing;
22461 }
22462 else
22463 {
22464 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22465 struct font_metrics metrics;
22466
22467 composition_gstring_width (gstring, glyph->slice.cmp.from,
22468 glyph->slice.cmp.to + 1, &metrics);
22469 if (metrics.rbearing > metrics.width)
22470 *right = metrics.rbearing - metrics.width;
22471 if (metrics.lbearing < 0)
22472 *left = - metrics.lbearing;
22473 }
22474 }
22475 }
22476
22477
22478 /* Return the index of the first glyph preceding glyph string S that
22479 is overwritten by S because of S's left overhang. Value is -1
22480 if no glyphs are overwritten. */
22481
22482 static int
22483 left_overwritten (struct glyph_string *s)
22484 {
22485 int k;
22486
22487 if (s->left_overhang)
22488 {
22489 int x = 0, i;
22490 struct glyph *glyphs = s->row->glyphs[s->area];
22491 int first = s->first_glyph - glyphs;
22492
22493 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22494 x -= glyphs[i].pixel_width;
22495
22496 k = i + 1;
22497 }
22498 else
22499 k = -1;
22500
22501 return k;
22502 }
22503
22504
22505 /* Return the index of the first glyph preceding glyph string S that
22506 is overwriting S because of its right overhang. Value is -1 if no
22507 glyph in front of S overwrites S. */
22508
22509 static int
22510 left_overwriting (struct glyph_string *s)
22511 {
22512 int i, k, x;
22513 struct glyph *glyphs = s->row->glyphs[s->area];
22514 int first = s->first_glyph - glyphs;
22515
22516 k = -1;
22517 x = 0;
22518 for (i = first - 1; i >= 0; --i)
22519 {
22520 int left, right;
22521 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22522 if (x + right > 0)
22523 k = i;
22524 x -= glyphs[i].pixel_width;
22525 }
22526
22527 return k;
22528 }
22529
22530
22531 /* Return the index of the last glyph following glyph string S that is
22532 overwritten by S because of S's right overhang. Value is -1 if
22533 no such glyph is found. */
22534
22535 static int
22536 right_overwritten (struct glyph_string *s)
22537 {
22538 int k = -1;
22539
22540 if (s->right_overhang)
22541 {
22542 int x = 0, i;
22543 struct glyph *glyphs = s->row->glyphs[s->area];
22544 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22545 int end = s->row->used[s->area];
22546
22547 for (i = first; i < end && s->right_overhang > x; ++i)
22548 x += glyphs[i].pixel_width;
22549
22550 k = i;
22551 }
22552
22553 return k;
22554 }
22555
22556
22557 /* Return the index of the last glyph following glyph string S that
22558 overwrites S because of its left overhang. Value is negative
22559 if no such glyph is found. */
22560
22561 static int
22562 right_overwriting (struct glyph_string *s)
22563 {
22564 int i, k, x;
22565 int end = s->row->used[s->area];
22566 struct glyph *glyphs = s->row->glyphs[s->area];
22567 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22568
22569 k = -1;
22570 x = 0;
22571 for (i = first; i < end; ++i)
22572 {
22573 int left, right;
22574 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22575 if (x - left < 0)
22576 k = i;
22577 x += glyphs[i].pixel_width;
22578 }
22579
22580 return k;
22581 }
22582
22583
22584 /* Set background width of glyph string S. START is the index of the
22585 first glyph following S. LAST_X is the right-most x-position + 1
22586 in the drawing area. */
22587
22588 static inline void
22589 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22590 {
22591 /* If the face of this glyph string has to be drawn to the end of
22592 the drawing area, set S->extends_to_end_of_line_p. */
22593
22594 if (start == s->row->used[s->area]
22595 && s->area == TEXT_AREA
22596 && ((s->row->fill_line_p
22597 && (s->hl == DRAW_NORMAL_TEXT
22598 || s->hl == DRAW_IMAGE_RAISED
22599 || s->hl == DRAW_IMAGE_SUNKEN))
22600 || s->hl == DRAW_MOUSE_FACE))
22601 s->extends_to_end_of_line_p = 1;
22602
22603 /* If S extends its face to the end of the line, set its
22604 background_width to the distance to the right edge of the drawing
22605 area. */
22606 if (s->extends_to_end_of_line_p)
22607 s->background_width = last_x - s->x + 1;
22608 else
22609 s->background_width = s->width;
22610 }
22611
22612
22613 /* Compute overhangs and x-positions for glyph string S and its
22614 predecessors, or successors. X is the starting x-position for S.
22615 BACKWARD_P non-zero means process predecessors. */
22616
22617 static void
22618 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22619 {
22620 if (backward_p)
22621 {
22622 while (s)
22623 {
22624 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22625 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22626 x -= s->width;
22627 s->x = x;
22628 s = s->prev;
22629 }
22630 }
22631 else
22632 {
22633 while (s)
22634 {
22635 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22636 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22637 s->x = x;
22638 x += s->width;
22639 s = s->next;
22640 }
22641 }
22642 }
22643
22644
22645
22646 /* The following macros are only called from draw_glyphs below.
22647 They reference the following parameters of that function directly:
22648 `w', `row', `area', and `overlap_p'
22649 as well as the following local variables:
22650 `s', `f', and `hdc' (in W32) */
22651
22652 #ifdef HAVE_NTGUI
22653 /* On W32, silently add local `hdc' variable to argument list of
22654 init_glyph_string. */
22655 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22656 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22657 #else
22658 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22659 init_glyph_string (s, char2b, w, row, area, start, hl)
22660 #endif
22661
22662 /* Add a glyph string for a stretch glyph to the list of strings
22663 between HEAD and TAIL. START is the index of the stretch glyph in
22664 row area AREA of glyph row ROW. END is the index of the last glyph
22665 in that glyph row area. X is the current output position assigned
22666 to the new glyph string constructed. HL overrides that face of the
22667 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22668 is the right-most x-position of the drawing area. */
22669
22670 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22671 and below -- keep them on one line. */
22672 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22673 do \
22674 { \
22675 s = (struct glyph_string *) alloca (sizeof *s); \
22676 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22677 START = fill_stretch_glyph_string (s, START, END); \
22678 append_glyph_string (&HEAD, &TAIL, s); \
22679 s->x = (X); \
22680 } \
22681 while (0)
22682
22683
22684 /* Add a glyph string for an image glyph to the list of strings
22685 between HEAD and TAIL. START is the index of the image glyph in
22686 row area AREA of glyph row ROW. END is the index of the last glyph
22687 in that glyph row area. X is the current output position assigned
22688 to the new glyph string constructed. HL overrides that face of the
22689 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22690 is the right-most x-position of the drawing area. */
22691
22692 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22693 do \
22694 { \
22695 s = (struct glyph_string *) alloca (sizeof *s); \
22696 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22697 fill_image_glyph_string (s); \
22698 append_glyph_string (&HEAD, &TAIL, s); \
22699 ++START; \
22700 s->x = (X); \
22701 } \
22702 while (0)
22703
22704
22705 /* Add a glyph string for a sequence of character glyphs to the list
22706 of strings between HEAD and TAIL. START is the index of the first
22707 glyph in row area AREA of glyph row ROW that is part of the new
22708 glyph string. END is the index of the last glyph in that glyph row
22709 area. X is the current output position assigned to the new glyph
22710 string constructed. HL overrides that face of the glyph; e.g. it
22711 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22712 right-most x-position of the drawing area. */
22713
22714 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22715 do \
22716 { \
22717 int face_id; \
22718 XChar2b *char2b; \
22719 \
22720 face_id = (row)->glyphs[area][START].face_id; \
22721 \
22722 s = (struct glyph_string *) alloca (sizeof *s); \
22723 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22724 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22725 append_glyph_string (&HEAD, &TAIL, s); \
22726 s->x = (X); \
22727 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22728 } \
22729 while (0)
22730
22731
22732 /* Add a glyph string for a composite sequence to the list of strings
22733 between HEAD and TAIL. START is the index of the first glyph in
22734 row area AREA of glyph row ROW that is part of the new glyph
22735 string. END is the index of the last glyph in that glyph row area.
22736 X is the current output position assigned to the new glyph string
22737 constructed. HL overrides that face of the glyph; e.g. it is
22738 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22739 x-position of the drawing area. */
22740
22741 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22742 do { \
22743 int face_id = (row)->glyphs[area][START].face_id; \
22744 struct face *base_face = FACE_FROM_ID (f, face_id); \
22745 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22746 struct composition *cmp = composition_table[cmp_id]; \
22747 XChar2b *char2b; \
22748 struct glyph_string *first_s = NULL; \
22749 int n; \
22750 \
22751 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22752 \
22753 /* Make glyph_strings for each glyph sequence that is drawable by \
22754 the same face, and append them to HEAD/TAIL. */ \
22755 for (n = 0; n < cmp->glyph_len;) \
22756 { \
22757 s = (struct glyph_string *) alloca (sizeof *s); \
22758 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22759 append_glyph_string (&(HEAD), &(TAIL), s); \
22760 s->cmp = cmp; \
22761 s->cmp_from = n; \
22762 s->x = (X); \
22763 if (n == 0) \
22764 first_s = s; \
22765 n = fill_composite_glyph_string (s, base_face, overlaps); \
22766 } \
22767 \
22768 ++START; \
22769 s = first_s; \
22770 } while (0)
22771
22772
22773 /* Add a glyph string for a glyph-string sequence to the list of strings
22774 between HEAD and TAIL. */
22775
22776 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22777 do { \
22778 int face_id; \
22779 XChar2b *char2b; \
22780 Lisp_Object gstring; \
22781 \
22782 face_id = (row)->glyphs[area][START].face_id; \
22783 gstring = (composition_gstring_from_id \
22784 ((row)->glyphs[area][START].u.cmp.id)); \
22785 s = (struct glyph_string *) alloca (sizeof *s); \
22786 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22787 * LGSTRING_GLYPH_LEN (gstring)); \
22788 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22789 append_glyph_string (&(HEAD), &(TAIL), s); \
22790 s->x = (X); \
22791 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22792 } while (0)
22793
22794
22795 /* Add a glyph string for a sequence of glyphless character's glyphs
22796 to the list of strings between HEAD and TAIL. The meanings of
22797 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22798
22799 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22800 do \
22801 { \
22802 int face_id; \
22803 \
22804 face_id = (row)->glyphs[area][START].face_id; \
22805 \
22806 s = (struct glyph_string *) alloca (sizeof *s); \
22807 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22808 append_glyph_string (&HEAD, &TAIL, s); \
22809 s->x = (X); \
22810 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22811 overlaps); \
22812 } \
22813 while (0)
22814
22815
22816 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22817 of AREA of glyph row ROW on window W between indices START and END.
22818 HL overrides the face for drawing glyph strings, e.g. it is
22819 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22820 x-positions of the drawing area.
22821
22822 This is an ugly monster macro construct because we must use alloca
22823 to allocate glyph strings (because draw_glyphs can be called
22824 asynchronously). */
22825
22826 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22827 do \
22828 { \
22829 HEAD = TAIL = NULL; \
22830 while (START < END) \
22831 { \
22832 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22833 switch (first_glyph->type) \
22834 { \
22835 case CHAR_GLYPH: \
22836 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22837 HL, X, LAST_X); \
22838 break; \
22839 \
22840 case COMPOSITE_GLYPH: \
22841 if (first_glyph->u.cmp.automatic) \
22842 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22843 HL, X, LAST_X); \
22844 else \
22845 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22846 HL, X, LAST_X); \
22847 break; \
22848 \
22849 case STRETCH_GLYPH: \
22850 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22851 HL, X, LAST_X); \
22852 break; \
22853 \
22854 case IMAGE_GLYPH: \
22855 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22856 HL, X, LAST_X); \
22857 break; \
22858 \
22859 case GLYPHLESS_GLYPH: \
22860 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22861 HL, X, LAST_X); \
22862 break; \
22863 \
22864 default: \
22865 abort (); \
22866 } \
22867 \
22868 if (s) \
22869 { \
22870 set_glyph_string_background_width (s, START, LAST_X); \
22871 (X) += s->width; \
22872 } \
22873 } \
22874 } while (0)
22875
22876
22877 /* Draw glyphs between START and END in AREA of ROW on window W,
22878 starting at x-position X. X is relative to AREA in W. HL is a
22879 face-override with the following meaning:
22880
22881 DRAW_NORMAL_TEXT draw normally
22882 DRAW_CURSOR draw in cursor face
22883 DRAW_MOUSE_FACE draw in mouse face.
22884 DRAW_INVERSE_VIDEO draw in mode line face
22885 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22886 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22887
22888 If OVERLAPS is non-zero, draw only the foreground of characters and
22889 clip to the physical height of ROW. Non-zero value also defines
22890 the overlapping part to be drawn:
22891
22892 OVERLAPS_PRED overlap with preceding rows
22893 OVERLAPS_SUCC overlap with succeeding rows
22894 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22895 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22896
22897 Value is the x-position reached, relative to AREA of W. */
22898
22899 static int
22900 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22901 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
22902 enum draw_glyphs_face hl, int overlaps)
22903 {
22904 struct glyph_string *head, *tail;
22905 struct glyph_string *s;
22906 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22907 int i, j, x_reached, last_x, area_left = 0;
22908 struct frame *f = XFRAME (WINDOW_FRAME (w));
22909 DECLARE_HDC (hdc);
22910
22911 ALLOCATE_HDC (hdc, f);
22912
22913 /* Let's rather be paranoid than getting a SEGV. */
22914 end = min (end, row->used[area]);
22915 start = max (0, start);
22916 start = min (end, start);
22917
22918 /* Translate X to frame coordinates. Set last_x to the right
22919 end of the drawing area. */
22920 if (row->full_width_p)
22921 {
22922 /* X is relative to the left edge of W, without scroll bars
22923 or fringes. */
22924 area_left = WINDOW_LEFT_EDGE_X (w);
22925 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22926 }
22927 else
22928 {
22929 area_left = window_box_left (w, area);
22930 last_x = area_left + window_box_width (w, area);
22931 }
22932 x += area_left;
22933
22934 /* Build a doubly-linked list of glyph_string structures between
22935 head and tail from what we have to draw. Note that the macro
22936 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22937 the reason we use a separate variable `i'. */
22938 i = start;
22939 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22940 if (tail)
22941 x_reached = tail->x + tail->background_width;
22942 else
22943 x_reached = x;
22944
22945 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22946 the row, redraw some glyphs in front or following the glyph
22947 strings built above. */
22948 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22949 {
22950 struct glyph_string *h, *t;
22951 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22952 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22953 int check_mouse_face = 0;
22954 int dummy_x = 0;
22955
22956 /* If mouse highlighting is on, we may need to draw adjacent
22957 glyphs using mouse-face highlighting. */
22958 if (area == TEXT_AREA && row->mouse_face_p)
22959 {
22960 struct glyph_row *mouse_beg_row, *mouse_end_row;
22961
22962 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22963 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22964
22965 if (row >= mouse_beg_row && row <= mouse_end_row)
22966 {
22967 check_mouse_face = 1;
22968 mouse_beg_col = (row == mouse_beg_row)
22969 ? hlinfo->mouse_face_beg_col : 0;
22970 mouse_end_col = (row == mouse_end_row)
22971 ? hlinfo->mouse_face_end_col
22972 : row->used[TEXT_AREA];
22973 }
22974 }
22975
22976 /* Compute overhangs for all glyph strings. */
22977 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22978 for (s = head; s; s = s->next)
22979 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22980
22981 /* Prepend glyph strings for glyphs in front of the first glyph
22982 string that are overwritten because of the first glyph
22983 string's left overhang. The background of all strings
22984 prepended must be drawn because the first glyph string
22985 draws over it. */
22986 i = left_overwritten (head);
22987 if (i >= 0)
22988 {
22989 enum draw_glyphs_face overlap_hl;
22990
22991 /* If this row contains mouse highlighting, attempt to draw
22992 the overlapped glyphs with the correct highlight. This
22993 code fails if the overlap encompasses more than one glyph
22994 and mouse-highlight spans only some of these glyphs.
22995 However, making it work perfectly involves a lot more
22996 code, and I don't know if the pathological case occurs in
22997 practice, so we'll stick to this for now. --- cyd */
22998 if (check_mouse_face
22999 && mouse_beg_col < start && mouse_end_col > i)
23000 overlap_hl = DRAW_MOUSE_FACE;
23001 else
23002 overlap_hl = DRAW_NORMAL_TEXT;
23003
23004 j = i;
23005 BUILD_GLYPH_STRINGS (j, start, h, t,
23006 overlap_hl, dummy_x, last_x);
23007 start = i;
23008 compute_overhangs_and_x (t, head->x, 1);
23009 prepend_glyph_string_lists (&head, &tail, h, t);
23010 clip_head = head;
23011 }
23012
23013 /* Prepend glyph strings for glyphs in front of the first glyph
23014 string that overwrite that glyph string because of their
23015 right overhang. For these strings, only the foreground must
23016 be drawn, because it draws over the glyph string at `head'.
23017 The background must not be drawn because this would overwrite
23018 right overhangs of preceding glyphs for which no glyph
23019 strings exist. */
23020 i = left_overwriting (head);
23021 if (i >= 0)
23022 {
23023 enum draw_glyphs_face overlap_hl;
23024
23025 if (check_mouse_face
23026 && mouse_beg_col < start && mouse_end_col > i)
23027 overlap_hl = DRAW_MOUSE_FACE;
23028 else
23029 overlap_hl = DRAW_NORMAL_TEXT;
23030
23031 clip_head = head;
23032 BUILD_GLYPH_STRINGS (i, start, h, t,
23033 overlap_hl, dummy_x, last_x);
23034 for (s = h; s; s = s->next)
23035 s->background_filled_p = 1;
23036 compute_overhangs_and_x (t, head->x, 1);
23037 prepend_glyph_string_lists (&head, &tail, h, t);
23038 }
23039
23040 /* Append glyphs strings for glyphs following the last glyph
23041 string tail that are overwritten by tail. The background of
23042 these strings has to be drawn because tail's foreground draws
23043 over it. */
23044 i = right_overwritten (tail);
23045 if (i >= 0)
23046 {
23047 enum draw_glyphs_face overlap_hl;
23048
23049 if (check_mouse_face
23050 && mouse_beg_col < i && mouse_end_col > end)
23051 overlap_hl = DRAW_MOUSE_FACE;
23052 else
23053 overlap_hl = DRAW_NORMAL_TEXT;
23054
23055 BUILD_GLYPH_STRINGS (end, i, h, t,
23056 overlap_hl, x, last_x);
23057 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23058 we don't have `end = i;' here. */
23059 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23060 append_glyph_string_lists (&head, &tail, h, t);
23061 clip_tail = tail;
23062 }
23063
23064 /* Append glyph strings for glyphs following the last glyph
23065 string tail that overwrite tail. The foreground of such
23066 glyphs has to be drawn because it writes into the background
23067 of tail. The background must not be drawn because it could
23068 paint over the foreground of following glyphs. */
23069 i = right_overwriting (tail);
23070 if (i >= 0)
23071 {
23072 enum draw_glyphs_face overlap_hl;
23073 if (check_mouse_face
23074 && mouse_beg_col < i && mouse_end_col > end)
23075 overlap_hl = DRAW_MOUSE_FACE;
23076 else
23077 overlap_hl = DRAW_NORMAL_TEXT;
23078
23079 clip_tail = tail;
23080 i++; /* We must include the Ith glyph. */
23081 BUILD_GLYPH_STRINGS (end, i, h, t,
23082 overlap_hl, x, last_x);
23083 for (s = h; s; s = s->next)
23084 s->background_filled_p = 1;
23085 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23086 append_glyph_string_lists (&head, &tail, h, t);
23087 }
23088 if (clip_head || clip_tail)
23089 for (s = head; s; s = s->next)
23090 {
23091 s->clip_head = clip_head;
23092 s->clip_tail = clip_tail;
23093 }
23094 }
23095
23096 /* Draw all strings. */
23097 for (s = head; s; s = s->next)
23098 FRAME_RIF (f)->draw_glyph_string (s);
23099
23100 #ifndef HAVE_NS
23101 /* When focus a sole frame and move horizontally, this sets on_p to 0
23102 causing a failure to erase prev cursor position. */
23103 if (area == TEXT_AREA
23104 && !row->full_width_p
23105 /* When drawing overlapping rows, only the glyph strings'
23106 foreground is drawn, which doesn't erase a cursor
23107 completely. */
23108 && !overlaps)
23109 {
23110 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23111 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23112 : (tail ? tail->x + tail->background_width : x));
23113 x0 -= area_left;
23114 x1 -= area_left;
23115
23116 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23117 row->y, MATRIX_ROW_BOTTOM_Y (row));
23118 }
23119 #endif
23120
23121 /* Value is the x-position up to which drawn, relative to AREA of W.
23122 This doesn't include parts drawn because of overhangs. */
23123 if (row->full_width_p)
23124 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23125 else
23126 x_reached -= area_left;
23127
23128 RELEASE_HDC (hdc, f);
23129
23130 return x_reached;
23131 }
23132
23133 /* Expand row matrix if too narrow. Don't expand if area
23134 is not present. */
23135
23136 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23137 { \
23138 if (!fonts_changed_p \
23139 && (it->glyph_row->glyphs[area] \
23140 < it->glyph_row->glyphs[area + 1])) \
23141 { \
23142 it->w->ncols_scale_factor++; \
23143 fonts_changed_p = 1; \
23144 } \
23145 }
23146
23147 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23148 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23149
23150 static inline void
23151 append_glyph (struct it *it)
23152 {
23153 struct glyph *glyph;
23154 enum glyph_row_area area = it->area;
23155
23156 xassert (it->glyph_row);
23157 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23158
23159 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23160 if (glyph < it->glyph_row->glyphs[area + 1])
23161 {
23162 /* If the glyph row is reversed, we need to prepend the glyph
23163 rather than append it. */
23164 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23165 {
23166 struct glyph *g;
23167
23168 /* Make room for the additional glyph. */
23169 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23170 g[1] = *g;
23171 glyph = it->glyph_row->glyphs[area];
23172 }
23173 glyph->charpos = CHARPOS (it->position);
23174 glyph->object = it->object;
23175 if (it->pixel_width > 0)
23176 {
23177 glyph->pixel_width = it->pixel_width;
23178 glyph->padding_p = 0;
23179 }
23180 else
23181 {
23182 /* Assure at least 1-pixel width. Otherwise, cursor can't
23183 be displayed correctly. */
23184 glyph->pixel_width = 1;
23185 glyph->padding_p = 1;
23186 }
23187 glyph->ascent = it->ascent;
23188 glyph->descent = it->descent;
23189 glyph->voffset = it->voffset;
23190 glyph->type = CHAR_GLYPH;
23191 glyph->avoid_cursor_p = it->avoid_cursor_p;
23192 glyph->multibyte_p = it->multibyte_p;
23193 glyph->left_box_line_p = it->start_of_box_run_p;
23194 glyph->right_box_line_p = it->end_of_box_run_p;
23195 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23196 || it->phys_descent > it->descent);
23197 glyph->glyph_not_available_p = it->glyph_not_available_p;
23198 glyph->face_id = it->face_id;
23199 glyph->u.ch = it->char_to_display;
23200 glyph->slice.img = null_glyph_slice;
23201 glyph->font_type = FONT_TYPE_UNKNOWN;
23202 if (it->bidi_p)
23203 {
23204 glyph->resolved_level = it->bidi_it.resolved_level;
23205 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23206 abort ();
23207 glyph->bidi_type = it->bidi_it.type;
23208 }
23209 else
23210 {
23211 glyph->resolved_level = 0;
23212 glyph->bidi_type = UNKNOWN_BT;
23213 }
23214 ++it->glyph_row->used[area];
23215 }
23216 else
23217 IT_EXPAND_MATRIX_WIDTH (it, area);
23218 }
23219
23220 /* Store one glyph for the composition IT->cmp_it.id in
23221 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23222 non-null. */
23223
23224 static inline void
23225 append_composite_glyph (struct it *it)
23226 {
23227 struct glyph *glyph;
23228 enum glyph_row_area area = it->area;
23229
23230 xassert (it->glyph_row);
23231
23232 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23233 if (glyph < it->glyph_row->glyphs[area + 1])
23234 {
23235 /* If the glyph row is reversed, we need to prepend the glyph
23236 rather than append it. */
23237 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23238 {
23239 struct glyph *g;
23240
23241 /* Make room for the new glyph. */
23242 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23243 g[1] = *g;
23244 glyph = it->glyph_row->glyphs[it->area];
23245 }
23246 glyph->charpos = it->cmp_it.charpos;
23247 glyph->object = it->object;
23248 glyph->pixel_width = it->pixel_width;
23249 glyph->ascent = it->ascent;
23250 glyph->descent = it->descent;
23251 glyph->voffset = it->voffset;
23252 glyph->type = COMPOSITE_GLYPH;
23253 if (it->cmp_it.ch < 0)
23254 {
23255 glyph->u.cmp.automatic = 0;
23256 glyph->u.cmp.id = it->cmp_it.id;
23257 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23258 }
23259 else
23260 {
23261 glyph->u.cmp.automatic = 1;
23262 glyph->u.cmp.id = it->cmp_it.id;
23263 glyph->slice.cmp.from = it->cmp_it.from;
23264 glyph->slice.cmp.to = it->cmp_it.to - 1;
23265 }
23266 glyph->avoid_cursor_p = it->avoid_cursor_p;
23267 glyph->multibyte_p = it->multibyte_p;
23268 glyph->left_box_line_p = it->start_of_box_run_p;
23269 glyph->right_box_line_p = it->end_of_box_run_p;
23270 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23271 || it->phys_descent > it->descent);
23272 glyph->padding_p = 0;
23273 glyph->glyph_not_available_p = 0;
23274 glyph->face_id = it->face_id;
23275 glyph->font_type = FONT_TYPE_UNKNOWN;
23276 if (it->bidi_p)
23277 {
23278 glyph->resolved_level = it->bidi_it.resolved_level;
23279 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23280 abort ();
23281 glyph->bidi_type = it->bidi_it.type;
23282 }
23283 ++it->glyph_row->used[area];
23284 }
23285 else
23286 IT_EXPAND_MATRIX_WIDTH (it, area);
23287 }
23288
23289
23290 /* Change IT->ascent and IT->height according to the setting of
23291 IT->voffset. */
23292
23293 static inline void
23294 take_vertical_position_into_account (struct it *it)
23295 {
23296 if (it->voffset)
23297 {
23298 if (it->voffset < 0)
23299 /* Increase the ascent so that we can display the text higher
23300 in the line. */
23301 it->ascent -= it->voffset;
23302 else
23303 /* Increase the descent so that we can display the text lower
23304 in the line. */
23305 it->descent += it->voffset;
23306 }
23307 }
23308
23309
23310 /* Produce glyphs/get display metrics for the image IT is loaded with.
23311 See the description of struct display_iterator in dispextern.h for
23312 an overview of struct display_iterator. */
23313
23314 static void
23315 produce_image_glyph (struct it *it)
23316 {
23317 struct image *img;
23318 struct face *face;
23319 int glyph_ascent, crop;
23320 struct glyph_slice slice;
23321
23322 xassert (it->what == IT_IMAGE);
23323
23324 face = FACE_FROM_ID (it->f, it->face_id);
23325 xassert (face);
23326 /* Make sure X resources of the face is loaded. */
23327 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23328
23329 if (it->image_id < 0)
23330 {
23331 /* Fringe bitmap. */
23332 it->ascent = it->phys_ascent = 0;
23333 it->descent = it->phys_descent = 0;
23334 it->pixel_width = 0;
23335 it->nglyphs = 0;
23336 return;
23337 }
23338
23339 img = IMAGE_FROM_ID (it->f, it->image_id);
23340 xassert (img);
23341 /* Make sure X resources of the image is loaded. */
23342 prepare_image_for_display (it->f, img);
23343
23344 slice.x = slice.y = 0;
23345 slice.width = img->width;
23346 slice.height = img->height;
23347
23348 if (INTEGERP (it->slice.x))
23349 slice.x = XINT (it->slice.x);
23350 else if (FLOATP (it->slice.x))
23351 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23352
23353 if (INTEGERP (it->slice.y))
23354 slice.y = XINT (it->slice.y);
23355 else if (FLOATP (it->slice.y))
23356 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23357
23358 if (INTEGERP (it->slice.width))
23359 slice.width = XINT (it->slice.width);
23360 else if (FLOATP (it->slice.width))
23361 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23362
23363 if (INTEGERP (it->slice.height))
23364 slice.height = XINT (it->slice.height);
23365 else if (FLOATP (it->slice.height))
23366 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23367
23368 if (slice.x >= img->width)
23369 slice.x = img->width;
23370 if (slice.y >= img->height)
23371 slice.y = img->height;
23372 if (slice.x + slice.width >= img->width)
23373 slice.width = img->width - slice.x;
23374 if (slice.y + slice.height > img->height)
23375 slice.height = img->height - slice.y;
23376
23377 if (slice.width == 0 || slice.height == 0)
23378 return;
23379
23380 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23381
23382 it->descent = slice.height - glyph_ascent;
23383 if (slice.y == 0)
23384 it->descent += img->vmargin;
23385 if (slice.y + slice.height == img->height)
23386 it->descent += img->vmargin;
23387 it->phys_descent = it->descent;
23388
23389 it->pixel_width = slice.width;
23390 if (slice.x == 0)
23391 it->pixel_width += img->hmargin;
23392 if (slice.x + slice.width == img->width)
23393 it->pixel_width += img->hmargin;
23394
23395 /* It's quite possible for images to have an ascent greater than
23396 their height, so don't get confused in that case. */
23397 if (it->descent < 0)
23398 it->descent = 0;
23399
23400 it->nglyphs = 1;
23401
23402 if (face->box != FACE_NO_BOX)
23403 {
23404 if (face->box_line_width > 0)
23405 {
23406 if (slice.y == 0)
23407 it->ascent += face->box_line_width;
23408 if (slice.y + slice.height == img->height)
23409 it->descent += face->box_line_width;
23410 }
23411
23412 if (it->start_of_box_run_p && slice.x == 0)
23413 it->pixel_width += eabs (face->box_line_width);
23414 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23415 it->pixel_width += eabs (face->box_line_width);
23416 }
23417
23418 take_vertical_position_into_account (it);
23419
23420 /* Automatically crop wide image glyphs at right edge so we can
23421 draw the cursor on same display row. */
23422 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23423 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23424 {
23425 it->pixel_width -= crop;
23426 slice.width -= crop;
23427 }
23428
23429 if (it->glyph_row)
23430 {
23431 struct glyph *glyph;
23432 enum glyph_row_area area = it->area;
23433
23434 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23435 if (glyph < it->glyph_row->glyphs[area + 1])
23436 {
23437 glyph->charpos = CHARPOS (it->position);
23438 glyph->object = it->object;
23439 glyph->pixel_width = it->pixel_width;
23440 glyph->ascent = glyph_ascent;
23441 glyph->descent = it->descent;
23442 glyph->voffset = it->voffset;
23443 glyph->type = IMAGE_GLYPH;
23444 glyph->avoid_cursor_p = it->avoid_cursor_p;
23445 glyph->multibyte_p = it->multibyte_p;
23446 glyph->left_box_line_p = it->start_of_box_run_p;
23447 glyph->right_box_line_p = it->end_of_box_run_p;
23448 glyph->overlaps_vertically_p = 0;
23449 glyph->padding_p = 0;
23450 glyph->glyph_not_available_p = 0;
23451 glyph->face_id = it->face_id;
23452 glyph->u.img_id = img->id;
23453 glyph->slice.img = slice;
23454 glyph->font_type = FONT_TYPE_UNKNOWN;
23455 if (it->bidi_p)
23456 {
23457 glyph->resolved_level = it->bidi_it.resolved_level;
23458 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23459 abort ();
23460 glyph->bidi_type = it->bidi_it.type;
23461 }
23462 ++it->glyph_row->used[area];
23463 }
23464 else
23465 IT_EXPAND_MATRIX_WIDTH (it, area);
23466 }
23467 }
23468
23469
23470 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23471 of the glyph, WIDTH and HEIGHT are the width and height of the
23472 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23473
23474 static void
23475 append_stretch_glyph (struct it *it, Lisp_Object object,
23476 int width, int height, int ascent)
23477 {
23478 struct glyph *glyph;
23479 enum glyph_row_area area = it->area;
23480
23481 xassert (ascent >= 0 && ascent <= height);
23482
23483 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23484 if (glyph < it->glyph_row->glyphs[area + 1])
23485 {
23486 /* If the glyph row is reversed, we need to prepend the glyph
23487 rather than append it. */
23488 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23489 {
23490 struct glyph *g;
23491
23492 /* Make room for the additional glyph. */
23493 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23494 g[1] = *g;
23495 glyph = it->glyph_row->glyphs[area];
23496 }
23497 glyph->charpos = CHARPOS (it->position);
23498 glyph->object = object;
23499 glyph->pixel_width = width;
23500 glyph->ascent = ascent;
23501 glyph->descent = height - ascent;
23502 glyph->voffset = it->voffset;
23503 glyph->type = STRETCH_GLYPH;
23504 glyph->avoid_cursor_p = it->avoid_cursor_p;
23505 glyph->multibyte_p = it->multibyte_p;
23506 glyph->left_box_line_p = it->start_of_box_run_p;
23507 glyph->right_box_line_p = it->end_of_box_run_p;
23508 glyph->overlaps_vertically_p = 0;
23509 glyph->padding_p = 0;
23510 glyph->glyph_not_available_p = 0;
23511 glyph->face_id = it->face_id;
23512 glyph->u.stretch.ascent = ascent;
23513 glyph->u.stretch.height = height;
23514 glyph->slice.img = null_glyph_slice;
23515 glyph->font_type = FONT_TYPE_UNKNOWN;
23516 if (it->bidi_p)
23517 {
23518 glyph->resolved_level = it->bidi_it.resolved_level;
23519 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23520 abort ();
23521 glyph->bidi_type = it->bidi_it.type;
23522 }
23523 else
23524 {
23525 glyph->resolved_level = 0;
23526 glyph->bidi_type = UNKNOWN_BT;
23527 }
23528 ++it->glyph_row->used[area];
23529 }
23530 else
23531 IT_EXPAND_MATRIX_WIDTH (it, area);
23532 }
23533
23534 #endif /* HAVE_WINDOW_SYSTEM */
23535
23536 /* Produce a stretch glyph for iterator IT. IT->object is the value
23537 of the glyph property displayed. The value must be a list
23538 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23539 being recognized:
23540
23541 1. `:width WIDTH' specifies that the space should be WIDTH *
23542 canonical char width wide. WIDTH may be an integer or floating
23543 point number.
23544
23545 2. `:relative-width FACTOR' specifies that the width of the stretch
23546 should be computed from the width of the first character having the
23547 `glyph' property, and should be FACTOR times that width.
23548
23549 3. `:align-to HPOS' specifies that the space should be wide enough
23550 to reach HPOS, a value in canonical character units.
23551
23552 Exactly one of the above pairs must be present.
23553
23554 4. `:height HEIGHT' specifies that the height of the stretch produced
23555 should be HEIGHT, measured in canonical character units.
23556
23557 5. `:relative-height FACTOR' specifies that the height of the
23558 stretch should be FACTOR times the height of the characters having
23559 the glyph property.
23560
23561 Either none or exactly one of 4 or 5 must be present.
23562
23563 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23564 of the stretch should be used for the ascent of the stretch.
23565 ASCENT must be in the range 0 <= ASCENT <= 100. */
23566
23567 void
23568 produce_stretch_glyph (struct it *it)
23569 {
23570 /* (space :width WIDTH :height HEIGHT ...) */
23571 Lisp_Object prop, plist;
23572 int width = 0, height = 0, align_to = -1;
23573 int zero_width_ok_p = 0;
23574 int ascent = 0;
23575 double tem;
23576 struct face *face = NULL;
23577 struct font *font = NULL;
23578
23579 #ifdef HAVE_WINDOW_SYSTEM
23580 int zero_height_ok_p = 0;
23581
23582 if (FRAME_WINDOW_P (it->f))
23583 {
23584 face = FACE_FROM_ID (it->f, it->face_id);
23585 font = face->font ? face->font : FRAME_FONT (it->f);
23586 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23587 }
23588 #endif
23589
23590 /* List should start with `space'. */
23591 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23592 plist = XCDR (it->object);
23593
23594 /* Compute the width of the stretch. */
23595 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23596 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23597 {
23598 /* Absolute width `:width WIDTH' specified and valid. */
23599 zero_width_ok_p = 1;
23600 width = (int)tem;
23601 }
23602 #ifdef HAVE_WINDOW_SYSTEM
23603 else if (FRAME_WINDOW_P (it->f)
23604 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23605 {
23606 /* Relative width `:relative-width FACTOR' specified and valid.
23607 Compute the width of the characters having the `glyph'
23608 property. */
23609 struct it it2;
23610 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23611
23612 it2 = *it;
23613 if (it->multibyte_p)
23614 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23615 else
23616 {
23617 it2.c = it2.char_to_display = *p, it2.len = 1;
23618 if (! ASCII_CHAR_P (it2.c))
23619 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23620 }
23621
23622 it2.glyph_row = NULL;
23623 it2.what = IT_CHARACTER;
23624 x_produce_glyphs (&it2);
23625 width = NUMVAL (prop) * it2.pixel_width;
23626 }
23627 #endif /* HAVE_WINDOW_SYSTEM */
23628 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23629 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23630 {
23631 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23632 align_to = (align_to < 0
23633 ? 0
23634 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23635 else if (align_to < 0)
23636 align_to = window_box_left_offset (it->w, TEXT_AREA);
23637 width = max (0, (int)tem + align_to - it->current_x);
23638 zero_width_ok_p = 1;
23639 }
23640 else
23641 /* Nothing specified -> width defaults to canonical char width. */
23642 width = FRAME_COLUMN_WIDTH (it->f);
23643
23644 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23645 width = 1;
23646
23647 #ifdef HAVE_WINDOW_SYSTEM
23648 /* Compute height. */
23649 if (FRAME_WINDOW_P (it->f))
23650 {
23651 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23652 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23653 {
23654 height = (int)tem;
23655 zero_height_ok_p = 1;
23656 }
23657 else if (prop = Fplist_get (plist, QCrelative_height),
23658 NUMVAL (prop) > 0)
23659 height = FONT_HEIGHT (font) * NUMVAL (prop);
23660 else
23661 height = FONT_HEIGHT (font);
23662
23663 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23664 height = 1;
23665
23666 /* Compute percentage of height used for ascent. If
23667 `:ascent ASCENT' is present and valid, use that. Otherwise,
23668 derive the ascent from the font in use. */
23669 if (prop = Fplist_get (plist, QCascent),
23670 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23671 ascent = height * NUMVAL (prop) / 100.0;
23672 else if (!NILP (prop)
23673 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23674 ascent = min (max (0, (int)tem), height);
23675 else
23676 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23677 }
23678 else
23679 #endif /* HAVE_WINDOW_SYSTEM */
23680 height = 1;
23681
23682 if (width > 0 && it->line_wrap != TRUNCATE
23683 && it->current_x + width > it->last_visible_x)
23684 {
23685 width = it->last_visible_x - it->current_x;
23686 #ifdef HAVE_WINDOW_SYSTEM
23687 /* Subtract one more pixel from the stretch width, but only on
23688 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23689 width -= FRAME_WINDOW_P (it->f);
23690 #endif
23691 }
23692
23693 if (width > 0 && height > 0 && it->glyph_row)
23694 {
23695 Lisp_Object o_object = it->object;
23696 Lisp_Object object = it->stack[it->sp - 1].string;
23697 int n = width;
23698
23699 if (!STRINGP (object))
23700 object = it->w->buffer;
23701 #ifdef HAVE_WINDOW_SYSTEM
23702 if (FRAME_WINDOW_P (it->f))
23703 append_stretch_glyph (it, object, width, height, ascent);
23704 else
23705 #endif
23706 {
23707 it->object = object;
23708 it->char_to_display = ' ';
23709 it->pixel_width = it->len = 1;
23710 while (n--)
23711 tty_append_glyph (it);
23712 it->object = o_object;
23713 }
23714 }
23715
23716 it->pixel_width = width;
23717 #ifdef HAVE_WINDOW_SYSTEM
23718 if (FRAME_WINDOW_P (it->f))
23719 {
23720 it->ascent = it->phys_ascent = ascent;
23721 it->descent = it->phys_descent = height - it->ascent;
23722 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23723 take_vertical_position_into_account (it);
23724 }
23725 else
23726 #endif
23727 it->nglyphs = width;
23728 }
23729
23730 #ifdef HAVE_WINDOW_SYSTEM
23731
23732 /* Calculate line-height and line-spacing properties.
23733 An integer value specifies explicit pixel value.
23734 A float value specifies relative value to current face height.
23735 A cons (float . face-name) specifies relative value to
23736 height of specified face font.
23737
23738 Returns height in pixels, or nil. */
23739
23740
23741 static Lisp_Object
23742 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23743 int boff, int override)
23744 {
23745 Lisp_Object face_name = Qnil;
23746 int ascent, descent, height;
23747
23748 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23749 return val;
23750
23751 if (CONSP (val))
23752 {
23753 face_name = XCAR (val);
23754 val = XCDR (val);
23755 if (!NUMBERP (val))
23756 val = make_number (1);
23757 if (NILP (face_name))
23758 {
23759 height = it->ascent + it->descent;
23760 goto scale;
23761 }
23762 }
23763
23764 if (NILP (face_name))
23765 {
23766 font = FRAME_FONT (it->f);
23767 boff = FRAME_BASELINE_OFFSET (it->f);
23768 }
23769 else if (EQ (face_name, Qt))
23770 {
23771 override = 0;
23772 }
23773 else
23774 {
23775 int face_id;
23776 struct face *face;
23777
23778 face_id = lookup_named_face (it->f, face_name, 0);
23779 if (face_id < 0)
23780 return make_number (-1);
23781
23782 face = FACE_FROM_ID (it->f, face_id);
23783 font = face->font;
23784 if (font == NULL)
23785 return make_number (-1);
23786 boff = font->baseline_offset;
23787 if (font->vertical_centering)
23788 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23789 }
23790
23791 ascent = FONT_BASE (font) + boff;
23792 descent = FONT_DESCENT (font) - boff;
23793
23794 if (override)
23795 {
23796 it->override_ascent = ascent;
23797 it->override_descent = descent;
23798 it->override_boff = boff;
23799 }
23800
23801 height = ascent + descent;
23802
23803 scale:
23804 if (FLOATP (val))
23805 height = (int)(XFLOAT_DATA (val) * height);
23806 else if (INTEGERP (val))
23807 height *= XINT (val);
23808
23809 return make_number (height);
23810 }
23811
23812
23813 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23814 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23815 and only if this is for a character for which no font was found.
23816
23817 If the display method (it->glyphless_method) is
23818 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23819 length of the acronym or the hexadecimal string, UPPER_XOFF and
23820 UPPER_YOFF are pixel offsets for the upper part of the string,
23821 LOWER_XOFF and LOWER_YOFF are for the lower part.
23822
23823 For the other display methods, LEN through LOWER_YOFF are zero. */
23824
23825 static void
23826 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23827 short upper_xoff, short upper_yoff,
23828 short lower_xoff, short lower_yoff)
23829 {
23830 struct glyph *glyph;
23831 enum glyph_row_area area = it->area;
23832
23833 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23834 if (glyph < it->glyph_row->glyphs[area + 1])
23835 {
23836 /* If the glyph row is reversed, we need to prepend the glyph
23837 rather than append it. */
23838 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23839 {
23840 struct glyph *g;
23841
23842 /* Make room for the additional glyph. */
23843 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23844 g[1] = *g;
23845 glyph = it->glyph_row->glyphs[area];
23846 }
23847 glyph->charpos = CHARPOS (it->position);
23848 glyph->object = it->object;
23849 glyph->pixel_width = it->pixel_width;
23850 glyph->ascent = it->ascent;
23851 glyph->descent = it->descent;
23852 glyph->voffset = it->voffset;
23853 glyph->type = GLYPHLESS_GLYPH;
23854 glyph->u.glyphless.method = it->glyphless_method;
23855 glyph->u.glyphless.for_no_font = for_no_font;
23856 glyph->u.glyphless.len = len;
23857 glyph->u.glyphless.ch = it->c;
23858 glyph->slice.glyphless.upper_xoff = upper_xoff;
23859 glyph->slice.glyphless.upper_yoff = upper_yoff;
23860 glyph->slice.glyphless.lower_xoff = lower_xoff;
23861 glyph->slice.glyphless.lower_yoff = lower_yoff;
23862 glyph->avoid_cursor_p = it->avoid_cursor_p;
23863 glyph->multibyte_p = it->multibyte_p;
23864 glyph->left_box_line_p = it->start_of_box_run_p;
23865 glyph->right_box_line_p = it->end_of_box_run_p;
23866 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23867 || it->phys_descent > it->descent);
23868 glyph->padding_p = 0;
23869 glyph->glyph_not_available_p = 0;
23870 glyph->face_id = face_id;
23871 glyph->font_type = FONT_TYPE_UNKNOWN;
23872 if (it->bidi_p)
23873 {
23874 glyph->resolved_level = it->bidi_it.resolved_level;
23875 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23876 abort ();
23877 glyph->bidi_type = it->bidi_it.type;
23878 }
23879 ++it->glyph_row->used[area];
23880 }
23881 else
23882 IT_EXPAND_MATRIX_WIDTH (it, area);
23883 }
23884
23885
23886 /* Produce a glyph for a glyphless character for iterator IT.
23887 IT->glyphless_method specifies which method to use for displaying
23888 the character. See the description of enum
23889 glyphless_display_method in dispextern.h for the detail.
23890
23891 FOR_NO_FONT is nonzero if and only if this is for a character for
23892 which no font was found. ACRONYM, if non-nil, is an acronym string
23893 for the character. */
23894
23895 static void
23896 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23897 {
23898 int face_id;
23899 struct face *face;
23900 struct font *font;
23901 int base_width, base_height, width, height;
23902 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23903 int len;
23904
23905 /* Get the metrics of the base font. We always refer to the current
23906 ASCII face. */
23907 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23908 font = face->font ? face->font : FRAME_FONT (it->f);
23909 it->ascent = FONT_BASE (font) + font->baseline_offset;
23910 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23911 base_height = it->ascent + it->descent;
23912 base_width = font->average_width;
23913
23914 /* Get a face ID for the glyph by utilizing a cache (the same way as
23915 done for `escape-glyph' in get_next_display_element). */
23916 if (it->f == last_glyphless_glyph_frame
23917 && it->face_id == last_glyphless_glyph_face_id)
23918 {
23919 face_id = last_glyphless_glyph_merged_face_id;
23920 }
23921 else
23922 {
23923 /* Merge the `glyphless-char' face into the current face. */
23924 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23925 last_glyphless_glyph_frame = it->f;
23926 last_glyphless_glyph_face_id = it->face_id;
23927 last_glyphless_glyph_merged_face_id = face_id;
23928 }
23929
23930 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23931 {
23932 it->pixel_width = THIN_SPACE_WIDTH;
23933 len = 0;
23934 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23935 }
23936 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23937 {
23938 width = CHAR_WIDTH (it->c);
23939 if (width == 0)
23940 width = 1;
23941 else if (width > 4)
23942 width = 4;
23943 it->pixel_width = base_width * width;
23944 len = 0;
23945 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23946 }
23947 else
23948 {
23949 char buf[7];
23950 const char *str;
23951 unsigned int code[6];
23952 int upper_len;
23953 int ascent, descent;
23954 struct font_metrics metrics_upper, metrics_lower;
23955
23956 face = FACE_FROM_ID (it->f, face_id);
23957 font = face->font ? face->font : FRAME_FONT (it->f);
23958 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23959
23960 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23961 {
23962 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23963 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23964 if (CONSP (acronym))
23965 acronym = XCAR (acronym);
23966 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23967 }
23968 else
23969 {
23970 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23971 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23972 str = buf;
23973 }
23974 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23975 code[len] = font->driver->encode_char (font, str[len]);
23976 upper_len = (len + 1) / 2;
23977 font->driver->text_extents (font, code, upper_len,
23978 &metrics_upper);
23979 font->driver->text_extents (font, code + upper_len, len - upper_len,
23980 &metrics_lower);
23981
23982
23983
23984 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23985 width = max (metrics_upper.width, metrics_lower.width) + 4;
23986 upper_xoff = upper_yoff = 2; /* the typical case */
23987 if (base_width >= width)
23988 {
23989 /* Align the upper to the left, the lower to the right. */
23990 it->pixel_width = base_width;
23991 lower_xoff = base_width - 2 - metrics_lower.width;
23992 }
23993 else
23994 {
23995 /* Center the shorter one. */
23996 it->pixel_width = width;
23997 if (metrics_upper.width >= metrics_lower.width)
23998 lower_xoff = (width - metrics_lower.width) / 2;
23999 else
24000 {
24001 /* FIXME: This code doesn't look right. It formerly was
24002 missing the "lower_xoff = 0;", which couldn't have
24003 been right since it left lower_xoff uninitialized. */
24004 lower_xoff = 0;
24005 upper_xoff = (width - metrics_upper.width) / 2;
24006 }
24007 }
24008
24009 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24010 top, bottom, and between upper and lower strings. */
24011 height = (metrics_upper.ascent + metrics_upper.descent
24012 + metrics_lower.ascent + metrics_lower.descent) + 5;
24013 /* Center vertically.
24014 H:base_height, D:base_descent
24015 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24016
24017 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24018 descent = D - H/2 + h/2;
24019 lower_yoff = descent - 2 - ld;
24020 upper_yoff = lower_yoff - la - 1 - ud; */
24021 ascent = - (it->descent - (base_height + height + 1) / 2);
24022 descent = it->descent - (base_height - height) / 2;
24023 lower_yoff = descent - 2 - metrics_lower.descent;
24024 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24025 - metrics_upper.descent);
24026 /* Don't make the height shorter than the base height. */
24027 if (height > base_height)
24028 {
24029 it->ascent = ascent;
24030 it->descent = descent;
24031 }
24032 }
24033
24034 it->phys_ascent = it->ascent;
24035 it->phys_descent = it->descent;
24036 if (it->glyph_row)
24037 append_glyphless_glyph (it, face_id, for_no_font, len,
24038 upper_xoff, upper_yoff,
24039 lower_xoff, lower_yoff);
24040 it->nglyphs = 1;
24041 take_vertical_position_into_account (it);
24042 }
24043
24044
24045 /* RIF:
24046 Produce glyphs/get display metrics for the display element IT is
24047 loaded with. See the description of struct it in dispextern.h
24048 for an overview of struct it. */
24049
24050 void
24051 x_produce_glyphs (struct it *it)
24052 {
24053 int extra_line_spacing = it->extra_line_spacing;
24054
24055 it->glyph_not_available_p = 0;
24056
24057 if (it->what == IT_CHARACTER)
24058 {
24059 XChar2b char2b;
24060 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24061 struct font *font = face->font;
24062 struct font_metrics *pcm = NULL;
24063 int boff; /* baseline offset */
24064
24065 if (font == NULL)
24066 {
24067 /* When no suitable font is found, display this character by
24068 the method specified in the first extra slot of
24069 Vglyphless_char_display. */
24070 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24071
24072 xassert (it->what == IT_GLYPHLESS);
24073 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24074 goto done;
24075 }
24076
24077 boff = font->baseline_offset;
24078 if (font->vertical_centering)
24079 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24080
24081 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24082 {
24083 int stretched_p;
24084
24085 it->nglyphs = 1;
24086
24087 if (it->override_ascent >= 0)
24088 {
24089 it->ascent = it->override_ascent;
24090 it->descent = it->override_descent;
24091 boff = it->override_boff;
24092 }
24093 else
24094 {
24095 it->ascent = FONT_BASE (font) + boff;
24096 it->descent = FONT_DESCENT (font) - boff;
24097 }
24098
24099 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24100 {
24101 pcm = get_per_char_metric (font, &char2b);
24102 if (pcm->width == 0
24103 && pcm->rbearing == 0 && pcm->lbearing == 0)
24104 pcm = NULL;
24105 }
24106
24107 if (pcm)
24108 {
24109 it->phys_ascent = pcm->ascent + boff;
24110 it->phys_descent = pcm->descent - boff;
24111 it->pixel_width = pcm->width;
24112 }
24113 else
24114 {
24115 it->glyph_not_available_p = 1;
24116 it->phys_ascent = it->ascent;
24117 it->phys_descent = it->descent;
24118 it->pixel_width = font->space_width;
24119 }
24120
24121 if (it->constrain_row_ascent_descent_p)
24122 {
24123 if (it->descent > it->max_descent)
24124 {
24125 it->ascent += it->descent - it->max_descent;
24126 it->descent = it->max_descent;
24127 }
24128 if (it->ascent > it->max_ascent)
24129 {
24130 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24131 it->ascent = it->max_ascent;
24132 }
24133 it->phys_ascent = min (it->phys_ascent, it->ascent);
24134 it->phys_descent = min (it->phys_descent, it->descent);
24135 extra_line_spacing = 0;
24136 }
24137
24138 /* If this is a space inside a region of text with
24139 `space-width' property, change its width. */
24140 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24141 if (stretched_p)
24142 it->pixel_width *= XFLOATINT (it->space_width);
24143
24144 /* If face has a box, add the box thickness to the character
24145 height. If character has a box line to the left and/or
24146 right, add the box line width to the character's width. */
24147 if (face->box != FACE_NO_BOX)
24148 {
24149 int thick = face->box_line_width;
24150
24151 if (thick > 0)
24152 {
24153 it->ascent += thick;
24154 it->descent += thick;
24155 }
24156 else
24157 thick = -thick;
24158
24159 if (it->start_of_box_run_p)
24160 it->pixel_width += thick;
24161 if (it->end_of_box_run_p)
24162 it->pixel_width += thick;
24163 }
24164
24165 /* If face has an overline, add the height of the overline
24166 (1 pixel) and a 1 pixel margin to the character height. */
24167 if (face->overline_p)
24168 it->ascent += overline_margin;
24169
24170 if (it->constrain_row_ascent_descent_p)
24171 {
24172 if (it->ascent > it->max_ascent)
24173 it->ascent = it->max_ascent;
24174 if (it->descent > it->max_descent)
24175 it->descent = it->max_descent;
24176 }
24177
24178 take_vertical_position_into_account (it);
24179
24180 /* If we have to actually produce glyphs, do it. */
24181 if (it->glyph_row)
24182 {
24183 if (stretched_p)
24184 {
24185 /* Translate a space with a `space-width' property
24186 into a stretch glyph. */
24187 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24188 / FONT_HEIGHT (font));
24189 append_stretch_glyph (it, it->object, it->pixel_width,
24190 it->ascent + it->descent, ascent);
24191 }
24192 else
24193 append_glyph (it);
24194
24195 /* If characters with lbearing or rbearing are displayed
24196 in this line, record that fact in a flag of the
24197 glyph row. This is used to optimize X output code. */
24198 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24199 it->glyph_row->contains_overlapping_glyphs_p = 1;
24200 }
24201 if (! stretched_p && it->pixel_width == 0)
24202 /* We assure that all visible glyphs have at least 1-pixel
24203 width. */
24204 it->pixel_width = 1;
24205 }
24206 else if (it->char_to_display == '\n')
24207 {
24208 /* A newline has no width, but we need the height of the
24209 line. But if previous part of the line sets a height,
24210 don't increase that height */
24211
24212 Lisp_Object height;
24213 Lisp_Object total_height = Qnil;
24214
24215 it->override_ascent = -1;
24216 it->pixel_width = 0;
24217 it->nglyphs = 0;
24218
24219 height = get_it_property (it, Qline_height);
24220 /* Split (line-height total-height) list */
24221 if (CONSP (height)
24222 && CONSP (XCDR (height))
24223 && NILP (XCDR (XCDR (height))))
24224 {
24225 total_height = XCAR (XCDR (height));
24226 height = XCAR (height);
24227 }
24228 height = calc_line_height_property (it, height, font, boff, 1);
24229
24230 if (it->override_ascent >= 0)
24231 {
24232 it->ascent = it->override_ascent;
24233 it->descent = it->override_descent;
24234 boff = it->override_boff;
24235 }
24236 else
24237 {
24238 it->ascent = FONT_BASE (font) + boff;
24239 it->descent = FONT_DESCENT (font) - boff;
24240 }
24241
24242 if (EQ (height, Qt))
24243 {
24244 if (it->descent > it->max_descent)
24245 {
24246 it->ascent += it->descent - it->max_descent;
24247 it->descent = it->max_descent;
24248 }
24249 if (it->ascent > it->max_ascent)
24250 {
24251 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24252 it->ascent = it->max_ascent;
24253 }
24254 it->phys_ascent = min (it->phys_ascent, it->ascent);
24255 it->phys_descent = min (it->phys_descent, it->descent);
24256 it->constrain_row_ascent_descent_p = 1;
24257 extra_line_spacing = 0;
24258 }
24259 else
24260 {
24261 Lisp_Object spacing;
24262
24263 it->phys_ascent = it->ascent;
24264 it->phys_descent = it->descent;
24265
24266 if ((it->max_ascent > 0 || it->max_descent > 0)
24267 && face->box != FACE_NO_BOX
24268 && face->box_line_width > 0)
24269 {
24270 it->ascent += face->box_line_width;
24271 it->descent += face->box_line_width;
24272 }
24273 if (!NILP (height)
24274 && XINT (height) > it->ascent + it->descent)
24275 it->ascent = XINT (height) - it->descent;
24276
24277 if (!NILP (total_height))
24278 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24279 else
24280 {
24281 spacing = get_it_property (it, Qline_spacing);
24282 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24283 }
24284 if (INTEGERP (spacing))
24285 {
24286 extra_line_spacing = XINT (spacing);
24287 if (!NILP (total_height))
24288 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24289 }
24290 }
24291 }
24292 else /* i.e. (it->char_to_display == '\t') */
24293 {
24294 if (font->space_width > 0)
24295 {
24296 int tab_width = it->tab_width * font->space_width;
24297 int x = it->current_x + it->continuation_lines_width;
24298 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24299
24300 /* If the distance from the current position to the next tab
24301 stop is less than a space character width, use the
24302 tab stop after that. */
24303 if (next_tab_x - x < font->space_width)
24304 next_tab_x += tab_width;
24305
24306 it->pixel_width = next_tab_x - x;
24307 it->nglyphs = 1;
24308 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24309 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24310
24311 if (it->glyph_row)
24312 {
24313 append_stretch_glyph (it, it->object, it->pixel_width,
24314 it->ascent + it->descent, it->ascent);
24315 }
24316 }
24317 else
24318 {
24319 it->pixel_width = 0;
24320 it->nglyphs = 1;
24321 }
24322 }
24323 }
24324 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24325 {
24326 /* A static composition.
24327
24328 Note: A composition is represented as one glyph in the
24329 glyph matrix. There are no padding glyphs.
24330
24331 Important note: pixel_width, ascent, and descent are the
24332 values of what is drawn by draw_glyphs (i.e. the values of
24333 the overall glyphs composed). */
24334 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24335 int boff; /* baseline offset */
24336 struct composition *cmp = composition_table[it->cmp_it.id];
24337 int glyph_len = cmp->glyph_len;
24338 struct font *font = face->font;
24339
24340 it->nglyphs = 1;
24341
24342 /* If we have not yet calculated pixel size data of glyphs of
24343 the composition for the current face font, calculate them
24344 now. Theoretically, we have to check all fonts for the
24345 glyphs, but that requires much time and memory space. So,
24346 here we check only the font of the first glyph. This may
24347 lead to incorrect display, but it's very rare, and C-l
24348 (recenter-top-bottom) can correct the display anyway. */
24349 if (! cmp->font || cmp->font != font)
24350 {
24351 /* Ascent and descent of the font of the first character
24352 of this composition (adjusted by baseline offset).
24353 Ascent and descent of overall glyphs should not be less
24354 than these, respectively. */
24355 int font_ascent, font_descent, font_height;
24356 /* Bounding box of the overall glyphs. */
24357 int leftmost, rightmost, lowest, highest;
24358 int lbearing, rbearing;
24359 int i, width, ascent, descent;
24360 int left_padded = 0, right_padded = 0;
24361 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24362 XChar2b char2b;
24363 struct font_metrics *pcm;
24364 int font_not_found_p;
24365 ptrdiff_t pos;
24366
24367 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24368 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24369 break;
24370 if (glyph_len < cmp->glyph_len)
24371 right_padded = 1;
24372 for (i = 0; i < glyph_len; i++)
24373 {
24374 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24375 break;
24376 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24377 }
24378 if (i > 0)
24379 left_padded = 1;
24380
24381 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24382 : IT_CHARPOS (*it));
24383 /* If no suitable font is found, use the default font. */
24384 font_not_found_p = font == NULL;
24385 if (font_not_found_p)
24386 {
24387 face = face->ascii_face;
24388 font = face->font;
24389 }
24390 boff = font->baseline_offset;
24391 if (font->vertical_centering)
24392 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24393 font_ascent = FONT_BASE (font) + boff;
24394 font_descent = FONT_DESCENT (font) - boff;
24395 font_height = FONT_HEIGHT (font);
24396
24397 cmp->font = (void *) font;
24398
24399 pcm = NULL;
24400 if (! font_not_found_p)
24401 {
24402 get_char_face_and_encoding (it->f, c, it->face_id,
24403 &char2b, 0);
24404 pcm = get_per_char_metric (font, &char2b);
24405 }
24406
24407 /* Initialize the bounding box. */
24408 if (pcm)
24409 {
24410 width = cmp->glyph_len > 0 ? pcm->width : 0;
24411 ascent = pcm->ascent;
24412 descent = pcm->descent;
24413 lbearing = pcm->lbearing;
24414 rbearing = pcm->rbearing;
24415 }
24416 else
24417 {
24418 width = cmp->glyph_len > 0 ? font->space_width : 0;
24419 ascent = FONT_BASE (font);
24420 descent = FONT_DESCENT (font);
24421 lbearing = 0;
24422 rbearing = width;
24423 }
24424
24425 rightmost = width;
24426 leftmost = 0;
24427 lowest = - descent + boff;
24428 highest = ascent + boff;
24429
24430 if (! font_not_found_p
24431 && font->default_ascent
24432 && CHAR_TABLE_P (Vuse_default_ascent)
24433 && !NILP (Faref (Vuse_default_ascent,
24434 make_number (it->char_to_display))))
24435 highest = font->default_ascent + boff;
24436
24437 /* Draw the first glyph at the normal position. It may be
24438 shifted to right later if some other glyphs are drawn
24439 at the left. */
24440 cmp->offsets[i * 2] = 0;
24441 cmp->offsets[i * 2 + 1] = boff;
24442 cmp->lbearing = lbearing;
24443 cmp->rbearing = rbearing;
24444
24445 /* Set cmp->offsets for the remaining glyphs. */
24446 for (i++; i < glyph_len; i++)
24447 {
24448 int left, right, btm, top;
24449 int ch = COMPOSITION_GLYPH (cmp, i);
24450 int face_id;
24451 struct face *this_face;
24452
24453 if (ch == '\t')
24454 ch = ' ';
24455 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24456 this_face = FACE_FROM_ID (it->f, face_id);
24457 font = this_face->font;
24458
24459 if (font == NULL)
24460 pcm = NULL;
24461 else
24462 {
24463 get_char_face_and_encoding (it->f, ch, face_id,
24464 &char2b, 0);
24465 pcm = get_per_char_metric (font, &char2b);
24466 }
24467 if (! pcm)
24468 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24469 else
24470 {
24471 width = pcm->width;
24472 ascent = pcm->ascent;
24473 descent = pcm->descent;
24474 lbearing = pcm->lbearing;
24475 rbearing = pcm->rbearing;
24476 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24477 {
24478 /* Relative composition with or without
24479 alternate chars. */
24480 left = (leftmost + rightmost - width) / 2;
24481 btm = - descent + boff;
24482 if (font->relative_compose
24483 && (! CHAR_TABLE_P (Vignore_relative_composition)
24484 || NILP (Faref (Vignore_relative_composition,
24485 make_number (ch)))))
24486 {
24487
24488 if (- descent >= font->relative_compose)
24489 /* One extra pixel between two glyphs. */
24490 btm = highest + 1;
24491 else if (ascent <= 0)
24492 /* One extra pixel between two glyphs. */
24493 btm = lowest - 1 - ascent - descent;
24494 }
24495 }
24496 else
24497 {
24498 /* A composition rule is specified by an integer
24499 value that encodes global and new reference
24500 points (GREF and NREF). GREF and NREF are
24501 specified by numbers as below:
24502
24503 0---1---2 -- ascent
24504 | |
24505 | |
24506 | |
24507 9--10--11 -- center
24508 | |
24509 ---3---4---5--- baseline
24510 | |
24511 6---7---8 -- descent
24512 */
24513 int rule = COMPOSITION_RULE (cmp, i);
24514 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24515
24516 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24517 grefx = gref % 3, nrefx = nref % 3;
24518 grefy = gref / 3, nrefy = nref / 3;
24519 if (xoff)
24520 xoff = font_height * (xoff - 128) / 256;
24521 if (yoff)
24522 yoff = font_height * (yoff - 128) / 256;
24523
24524 left = (leftmost
24525 + grefx * (rightmost - leftmost) / 2
24526 - nrefx * width / 2
24527 + xoff);
24528
24529 btm = ((grefy == 0 ? highest
24530 : grefy == 1 ? 0
24531 : grefy == 2 ? lowest
24532 : (highest + lowest) / 2)
24533 - (nrefy == 0 ? ascent + descent
24534 : nrefy == 1 ? descent - boff
24535 : nrefy == 2 ? 0
24536 : (ascent + descent) / 2)
24537 + yoff);
24538 }
24539
24540 cmp->offsets[i * 2] = left;
24541 cmp->offsets[i * 2 + 1] = btm + descent;
24542
24543 /* Update the bounding box of the overall glyphs. */
24544 if (width > 0)
24545 {
24546 right = left + width;
24547 if (left < leftmost)
24548 leftmost = left;
24549 if (right > rightmost)
24550 rightmost = right;
24551 }
24552 top = btm + descent + ascent;
24553 if (top > highest)
24554 highest = top;
24555 if (btm < lowest)
24556 lowest = btm;
24557
24558 if (cmp->lbearing > left + lbearing)
24559 cmp->lbearing = left + lbearing;
24560 if (cmp->rbearing < left + rbearing)
24561 cmp->rbearing = left + rbearing;
24562 }
24563 }
24564
24565 /* If there are glyphs whose x-offsets are negative,
24566 shift all glyphs to the right and make all x-offsets
24567 non-negative. */
24568 if (leftmost < 0)
24569 {
24570 for (i = 0; i < cmp->glyph_len; i++)
24571 cmp->offsets[i * 2] -= leftmost;
24572 rightmost -= leftmost;
24573 cmp->lbearing -= leftmost;
24574 cmp->rbearing -= leftmost;
24575 }
24576
24577 if (left_padded && cmp->lbearing < 0)
24578 {
24579 for (i = 0; i < cmp->glyph_len; i++)
24580 cmp->offsets[i * 2] -= cmp->lbearing;
24581 rightmost -= cmp->lbearing;
24582 cmp->rbearing -= cmp->lbearing;
24583 cmp->lbearing = 0;
24584 }
24585 if (right_padded && rightmost < cmp->rbearing)
24586 {
24587 rightmost = cmp->rbearing;
24588 }
24589
24590 cmp->pixel_width = rightmost;
24591 cmp->ascent = highest;
24592 cmp->descent = - lowest;
24593 if (cmp->ascent < font_ascent)
24594 cmp->ascent = font_ascent;
24595 if (cmp->descent < font_descent)
24596 cmp->descent = font_descent;
24597 }
24598
24599 if (it->glyph_row
24600 && (cmp->lbearing < 0
24601 || cmp->rbearing > cmp->pixel_width))
24602 it->glyph_row->contains_overlapping_glyphs_p = 1;
24603
24604 it->pixel_width = cmp->pixel_width;
24605 it->ascent = it->phys_ascent = cmp->ascent;
24606 it->descent = it->phys_descent = cmp->descent;
24607 if (face->box != FACE_NO_BOX)
24608 {
24609 int thick = face->box_line_width;
24610
24611 if (thick > 0)
24612 {
24613 it->ascent += thick;
24614 it->descent += thick;
24615 }
24616 else
24617 thick = - thick;
24618
24619 if (it->start_of_box_run_p)
24620 it->pixel_width += thick;
24621 if (it->end_of_box_run_p)
24622 it->pixel_width += thick;
24623 }
24624
24625 /* If face has an overline, add the height of the overline
24626 (1 pixel) and a 1 pixel margin to the character height. */
24627 if (face->overline_p)
24628 it->ascent += overline_margin;
24629
24630 take_vertical_position_into_account (it);
24631 if (it->ascent < 0)
24632 it->ascent = 0;
24633 if (it->descent < 0)
24634 it->descent = 0;
24635
24636 if (it->glyph_row && cmp->glyph_len > 0)
24637 append_composite_glyph (it);
24638 }
24639 else if (it->what == IT_COMPOSITION)
24640 {
24641 /* A dynamic (automatic) composition. */
24642 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24643 Lisp_Object gstring;
24644 struct font_metrics metrics;
24645
24646 it->nglyphs = 1;
24647
24648 gstring = composition_gstring_from_id (it->cmp_it.id);
24649 it->pixel_width
24650 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24651 &metrics);
24652 if (it->glyph_row
24653 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24654 it->glyph_row->contains_overlapping_glyphs_p = 1;
24655 it->ascent = it->phys_ascent = metrics.ascent;
24656 it->descent = it->phys_descent = metrics.descent;
24657 if (face->box != FACE_NO_BOX)
24658 {
24659 int thick = face->box_line_width;
24660
24661 if (thick > 0)
24662 {
24663 it->ascent += thick;
24664 it->descent += thick;
24665 }
24666 else
24667 thick = - thick;
24668
24669 if (it->start_of_box_run_p)
24670 it->pixel_width += thick;
24671 if (it->end_of_box_run_p)
24672 it->pixel_width += thick;
24673 }
24674 /* If face has an overline, add the height of the overline
24675 (1 pixel) and a 1 pixel margin to the character height. */
24676 if (face->overline_p)
24677 it->ascent += overline_margin;
24678 take_vertical_position_into_account (it);
24679 if (it->ascent < 0)
24680 it->ascent = 0;
24681 if (it->descent < 0)
24682 it->descent = 0;
24683
24684 if (it->glyph_row)
24685 append_composite_glyph (it);
24686 }
24687 else if (it->what == IT_GLYPHLESS)
24688 produce_glyphless_glyph (it, 0, Qnil);
24689 else if (it->what == IT_IMAGE)
24690 produce_image_glyph (it);
24691 else if (it->what == IT_STRETCH)
24692 produce_stretch_glyph (it);
24693
24694 done:
24695 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24696 because this isn't true for images with `:ascent 100'. */
24697 xassert (it->ascent >= 0 && it->descent >= 0);
24698 if (it->area == TEXT_AREA)
24699 it->current_x += it->pixel_width;
24700
24701 if (extra_line_spacing > 0)
24702 {
24703 it->descent += extra_line_spacing;
24704 if (extra_line_spacing > it->max_extra_line_spacing)
24705 it->max_extra_line_spacing = extra_line_spacing;
24706 }
24707
24708 it->max_ascent = max (it->max_ascent, it->ascent);
24709 it->max_descent = max (it->max_descent, it->descent);
24710 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24711 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24712 }
24713
24714 /* EXPORT for RIF:
24715 Output LEN glyphs starting at START at the nominal cursor position.
24716 Advance the nominal cursor over the text. The global variable
24717 updated_window contains the window being updated, updated_row is
24718 the glyph row being updated, and updated_area is the area of that
24719 row being updated. */
24720
24721 void
24722 x_write_glyphs (struct glyph *start, int len)
24723 {
24724 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24725
24726 xassert (updated_window && updated_row);
24727 /* When the window is hscrolled, cursor hpos can legitimately be out
24728 of bounds, but we draw the cursor at the corresponding window
24729 margin in that case. */
24730 if (!updated_row->reversed_p && chpos < 0)
24731 chpos = 0;
24732 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24733 chpos = updated_row->used[TEXT_AREA] - 1;
24734
24735 BLOCK_INPUT;
24736
24737 /* Write glyphs. */
24738
24739 hpos = start - updated_row->glyphs[updated_area];
24740 x = draw_glyphs (updated_window, output_cursor.x,
24741 updated_row, updated_area,
24742 hpos, hpos + len,
24743 DRAW_NORMAL_TEXT, 0);
24744
24745 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24746 if (updated_area == TEXT_AREA
24747 && updated_window->phys_cursor_on_p
24748 && updated_window->phys_cursor.vpos == output_cursor.vpos
24749 && chpos >= hpos
24750 && chpos < hpos + len)
24751 updated_window->phys_cursor_on_p = 0;
24752
24753 UNBLOCK_INPUT;
24754
24755 /* Advance the output cursor. */
24756 output_cursor.hpos += len;
24757 output_cursor.x = x;
24758 }
24759
24760
24761 /* EXPORT for RIF:
24762 Insert LEN glyphs from START at the nominal cursor position. */
24763
24764 void
24765 x_insert_glyphs (struct glyph *start, int len)
24766 {
24767 struct frame *f;
24768 struct window *w;
24769 int line_height, shift_by_width, shifted_region_width;
24770 struct glyph_row *row;
24771 struct glyph *glyph;
24772 int frame_x, frame_y;
24773 ptrdiff_t hpos;
24774
24775 xassert (updated_window && updated_row);
24776 BLOCK_INPUT;
24777 w = updated_window;
24778 f = XFRAME (WINDOW_FRAME (w));
24779
24780 /* Get the height of the line we are in. */
24781 row = updated_row;
24782 line_height = row->height;
24783
24784 /* Get the width of the glyphs to insert. */
24785 shift_by_width = 0;
24786 for (glyph = start; glyph < start + len; ++glyph)
24787 shift_by_width += glyph->pixel_width;
24788
24789 /* Get the width of the region to shift right. */
24790 shifted_region_width = (window_box_width (w, updated_area)
24791 - output_cursor.x
24792 - shift_by_width);
24793
24794 /* Shift right. */
24795 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24796 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24797
24798 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24799 line_height, shift_by_width);
24800
24801 /* Write the glyphs. */
24802 hpos = start - row->glyphs[updated_area];
24803 draw_glyphs (w, output_cursor.x, row, updated_area,
24804 hpos, hpos + len,
24805 DRAW_NORMAL_TEXT, 0);
24806
24807 /* Advance the output cursor. */
24808 output_cursor.hpos += len;
24809 output_cursor.x += shift_by_width;
24810 UNBLOCK_INPUT;
24811 }
24812
24813
24814 /* EXPORT for RIF:
24815 Erase the current text line from the nominal cursor position
24816 (inclusive) to pixel column TO_X (exclusive). The idea is that
24817 everything from TO_X onward is already erased.
24818
24819 TO_X is a pixel position relative to updated_area of
24820 updated_window. TO_X == -1 means clear to the end of this area. */
24821
24822 void
24823 x_clear_end_of_line (int to_x)
24824 {
24825 struct frame *f;
24826 struct window *w = updated_window;
24827 int max_x, min_y, max_y;
24828 int from_x, from_y, to_y;
24829
24830 xassert (updated_window && updated_row);
24831 f = XFRAME (w->frame);
24832
24833 if (updated_row->full_width_p)
24834 max_x = WINDOW_TOTAL_WIDTH (w);
24835 else
24836 max_x = window_box_width (w, updated_area);
24837 max_y = window_text_bottom_y (w);
24838
24839 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24840 of window. For TO_X > 0, truncate to end of drawing area. */
24841 if (to_x == 0)
24842 return;
24843 else if (to_x < 0)
24844 to_x = max_x;
24845 else
24846 to_x = min (to_x, max_x);
24847
24848 to_y = min (max_y, output_cursor.y + updated_row->height);
24849
24850 /* Notice if the cursor will be cleared by this operation. */
24851 if (!updated_row->full_width_p)
24852 notice_overwritten_cursor (w, updated_area,
24853 output_cursor.x, -1,
24854 updated_row->y,
24855 MATRIX_ROW_BOTTOM_Y (updated_row));
24856
24857 from_x = output_cursor.x;
24858
24859 /* Translate to frame coordinates. */
24860 if (updated_row->full_width_p)
24861 {
24862 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24863 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24864 }
24865 else
24866 {
24867 int area_left = window_box_left (w, updated_area);
24868 from_x += area_left;
24869 to_x += area_left;
24870 }
24871
24872 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24873 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24874 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24875
24876 /* Prevent inadvertently clearing to end of the X window. */
24877 if (to_x > from_x && to_y > from_y)
24878 {
24879 BLOCK_INPUT;
24880 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24881 to_x - from_x, to_y - from_y);
24882 UNBLOCK_INPUT;
24883 }
24884 }
24885
24886 #endif /* HAVE_WINDOW_SYSTEM */
24887
24888
24889 \f
24890 /***********************************************************************
24891 Cursor types
24892 ***********************************************************************/
24893
24894 /* Value is the internal representation of the specified cursor type
24895 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24896 of the bar cursor. */
24897
24898 static enum text_cursor_kinds
24899 get_specified_cursor_type (Lisp_Object arg, int *width)
24900 {
24901 enum text_cursor_kinds type;
24902
24903 if (NILP (arg))
24904 return NO_CURSOR;
24905
24906 if (EQ (arg, Qbox))
24907 return FILLED_BOX_CURSOR;
24908
24909 if (EQ (arg, Qhollow))
24910 return HOLLOW_BOX_CURSOR;
24911
24912 if (EQ (arg, Qbar))
24913 {
24914 *width = 2;
24915 return BAR_CURSOR;
24916 }
24917
24918 if (CONSP (arg)
24919 && EQ (XCAR (arg), Qbar)
24920 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
24921 {
24922 *width = XINT (XCDR (arg));
24923 return BAR_CURSOR;
24924 }
24925
24926 if (EQ (arg, Qhbar))
24927 {
24928 *width = 2;
24929 return HBAR_CURSOR;
24930 }
24931
24932 if (CONSP (arg)
24933 && EQ (XCAR (arg), Qhbar)
24934 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
24935 {
24936 *width = XINT (XCDR (arg));
24937 return HBAR_CURSOR;
24938 }
24939
24940 /* Treat anything unknown as "hollow box cursor".
24941 It was bad to signal an error; people have trouble fixing
24942 .Xdefaults with Emacs, when it has something bad in it. */
24943 type = HOLLOW_BOX_CURSOR;
24944
24945 return type;
24946 }
24947
24948 /* Set the default cursor types for specified frame. */
24949 void
24950 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24951 {
24952 int width = 1;
24953 Lisp_Object tem;
24954
24955 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24956 FRAME_CURSOR_WIDTH (f) = width;
24957
24958 /* By default, set up the blink-off state depending on the on-state. */
24959
24960 tem = Fassoc (arg, Vblink_cursor_alist);
24961 if (!NILP (tem))
24962 {
24963 FRAME_BLINK_OFF_CURSOR (f)
24964 = get_specified_cursor_type (XCDR (tem), &width);
24965 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24966 }
24967 else
24968 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24969 }
24970
24971
24972 #ifdef HAVE_WINDOW_SYSTEM
24973
24974 /* Return the cursor we want to be displayed in window W. Return
24975 width of bar/hbar cursor through WIDTH arg. Return with
24976 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24977 (i.e. if the `system caret' should track this cursor).
24978
24979 In a mini-buffer window, we want the cursor only to appear if we
24980 are reading input from this window. For the selected window, we
24981 want the cursor type given by the frame parameter or buffer local
24982 setting of cursor-type. If explicitly marked off, draw no cursor.
24983 In all other cases, we want a hollow box cursor. */
24984
24985 static enum text_cursor_kinds
24986 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24987 int *active_cursor)
24988 {
24989 struct frame *f = XFRAME (w->frame);
24990 struct buffer *b = XBUFFER (w->buffer);
24991 int cursor_type = DEFAULT_CURSOR;
24992 Lisp_Object alt_cursor;
24993 int non_selected = 0;
24994
24995 *active_cursor = 1;
24996
24997 /* Echo area */
24998 if (cursor_in_echo_area
24999 && FRAME_HAS_MINIBUF_P (f)
25000 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25001 {
25002 if (w == XWINDOW (echo_area_window))
25003 {
25004 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25005 {
25006 *width = FRAME_CURSOR_WIDTH (f);
25007 return FRAME_DESIRED_CURSOR (f);
25008 }
25009 else
25010 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25011 }
25012
25013 *active_cursor = 0;
25014 non_selected = 1;
25015 }
25016
25017 /* Detect a nonselected window or nonselected frame. */
25018 else if (w != XWINDOW (f->selected_window)
25019 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25020 {
25021 *active_cursor = 0;
25022
25023 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25024 return NO_CURSOR;
25025
25026 non_selected = 1;
25027 }
25028
25029 /* Never display a cursor in a window in which cursor-type is nil. */
25030 if (NILP (BVAR (b, cursor_type)))
25031 return NO_CURSOR;
25032
25033 /* Get the normal cursor type for this window. */
25034 if (EQ (BVAR (b, cursor_type), Qt))
25035 {
25036 cursor_type = FRAME_DESIRED_CURSOR (f);
25037 *width = FRAME_CURSOR_WIDTH (f);
25038 }
25039 else
25040 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25041
25042 /* Use cursor-in-non-selected-windows instead
25043 for non-selected window or frame. */
25044 if (non_selected)
25045 {
25046 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25047 if (!EQ (Qt, alt_cursor))
25048 return get_specified_cursor_type (alt_cursor, width);
25049 /* t means modify the normal cursor type. */
25050 if (cursor_type == FILLED_BOX_CURSOR)
25051 cursor_type = HOLLOW_BOX_CURSOR;
25052 else if (cursor_type == BAR_CURSOR && *width > 1)
25053 --*width;
25054 return cursor_type;
25055 }
25056
25057 /* Use normal cursor if not blinked off. */
25058 if (!w->cursor_off_p)
25059 {
25060 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25061 {
25062 if (cursor_type == FILLED_BOX_CURSOR)
25063 {
25064 /* Using a block cursor on large images can be very annoying.
25065 So use a hollow cursor for "large" images.
25066 If image is not transparent (no mask), also use hollow cursor. */
25067 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25068 if (img != NULL && IMAGEP (img->spec))
25069 {
25070 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25071 where N = size of default frame font size.
25072 This should cover most of the "tiny" icons people may use. */
25073 if (!img->mask
25074 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25075 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25076 cursor_type = HOLLOW_BOX_CURSOR;
25077 }
25078 }
25079 else if (cursor_type != NO_CURSOR)
25080 {
25081 /* Display current only supports BOX and HOLLOW cursors for images.
25082 So for now, unconditionally use a HOLLOW cursor when cursor is
25083 not a solid box cursor. */
25084 cursor_type = HOLLOW_BOX_CURSOR;
25085 }
25086 }
25087 return cursor_type;
25088 }
25089
25090 /* Cursor is blinked off, so determine how to "toggle" it. */
25091
25092 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25093 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25094 return get_specified_cursor_type (XCDR (alt_cursor), width);
25095
25096 /* Then see if frame has specified a specific blink off cursor type. */
25097 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25098 {
25099 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25100 return FRAME_BLINK_OFF_CURSOR (f);
25101 }
25102
25103 #if 0
25104 /* Some people liked having a permanently visible blinking cursor,
25105 while others had very strong opinions against it. So it was
25106 decided to remove it. KFS 2003-09-03 */
25107
25108 /* Finally perform built-in cursor blinking:
25109 filled box <-> hollow box
25110 wide [h]bar <-> narrow [h]bar
25111 narrow [h]bar <-> no cursor
25112 other type <-> no cursor */
25113
25114 if (cursor_type == FILLED_BOX_CURSOR)
25115 return HOLLOW_BOX_CURSOR;
25116
25117 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25118 {
25119 *width = 1;
25120 return cursor_type;
25121 }
25122 #endif
25123
25124 return NO_CURSOR;
25125 }
25126
25127
25128 /* Notice when the text cursor of window W has been completely
25129 overwritten by a drawing operation that outputs glyphs in AREA
25130 starting at X0 and ending at X1 in the line starting at Y0 and
25131 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25132 the rest of the line after X0 has been written. Y coordinates
25133 are window-relative. */
25134
25135 static void
25136 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25137 int x0, int x1, int y0, int y1)
25138 {
25139 int cx0, cx1, cy0, cy1;
25140 struct glyph_row *row;
25141
25142 if (!w->phys_cursor_on_p)
25143 return;
25144 if (area != TEXT_AREA)
25145 return;
25146
25147 if (w->phys_cursor.vpos < 0
25148 || w->phys_cursor.vpos >= w->current_matrix->nrows
25149 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25150 !(row->enabled_p && row->displays_text_p)))
25151 return;
25152
25153 if (row->cursor_in_fringe_p)
25154 {
25155 row->cursor_in_fringe_p = 0;
25156 draw_fringe_bitmap (w, row, row->reversed_p);
25157 w->phys_cursor_on_p = 0;
25158 return;
25159 }
25160
25161 cx0 = w->phys_cursor.x;
25162 cx1 = cx0 + w->phys_cursor_width;
25163 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25164 return;
25165
25166 /* The cursor image will be completely removed from the
25167 screen if the output area intersects the cursor area in
25168 y-direction. When we draw in [y0 y1[, and some part of
25169 the cursor is at y < y0, that part must have been drawn
25170 before. When scrolling, the cursor is erased before
25171 actually scrolling, so we don't come here. When not
25172 scrolling, the rows above the old cursor row must have
25173 changed, and in this case these rows must have written
25174 over the cursor image.
25175
25176 Likewise if part of the cursor is below y1, with the
25177 exception of the cursor being in the first blank row at
25178 the buffer and window end because update_text_area
25179 doesn't draw that row. (Except when it does, but
25180 that's handled in update_text_area.) */
25181
25182 cy0 = w->phys_cursor.y;
25183 cy1 = cy0 + w->phys_cursor_height;
25184 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25185 return;
25186
25187 w->phys_cursor_on_p = 0;
25188 }
25189
25190 #endif /* HAVE_WINDOW_SYSTEM */
25191
25192 \f
25193 /************************************************************************
25194 Mouse Face
25195 ************************************************************************/
25196
25197 #ifdef HAVE_WINDOW_SYSTEM
25198
25199 /* EXPORT for RIF:
25200 Fix the display of area AREA of overlapping row ROW in window W
25201 with respect to the overlapping part OVERLAPS. */
25202
25203 void
25204 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25205 enum glyph_row_area area, int overlaps)
25206 {
25207 int i, x;
25208
25209 BLOCK_INPUT;
25210
25211 x = 0;
25212 for (i = 0; i < row->used[area];)
25213 {
25214 if (row->glyphs[area][i].overlaps_vertically_p)
25215 {
25216 int start = i, start_x = x;
25217
25218 do
25219 {
25220 x += row->glyphs[area][i].pixel_width;
25221 ++i;
25222 }
25223 while (i < row->used[area]
25224 && row->glyphs[area][i].overlaps_vertically_p);
25225
25226 draw_glyphs (w, start_x, row, area,
25227 start, i,
25228 DRAW_NORMAL_TEXT, overlaps);
25229 }
25230 else
25231 {
25232 x += row->glyphs[area][i].pixel_width;
25233 ++i;
25234 }
25235 }
25236
25237 UNBLOCK_INPUT;
25238 }
25239
25240
25241 /* EXPORT:
25242 Draw the cursor glyph of window W in glyph row ROW. See the
25243 comment of draw_glyphs for the meaning of HL. */
25244
25245 void
25246 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25247 enum draw_glyphs_face hl)
25248 {
25249 /* If cursor hpos is out of bounds, don't draw garbage. This can
25250 happen in mini-buffer windows when switching between echo area
25251 glyphs and mini-buffer. */
25252 if ((row->reversed_p
25253 ? (w->phys_cursor.hpos >= 0)
25254 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25255 {
25256 int on_p = w->phys_cursor_on_p;
25257 int x1;
25258 int hpos = w->phys_cursor.hpos;
25259
25260 /* When the window is hscrolled, cursor hpos can legitimately be
25261 out of bounds, but we draw the cursor at the corresponding
25262 window margin in that case. */
25263 if (!row->reversed_p && hpos < 0)
25264 hpos = 0;
25265 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25266 hpos = row->used[TEXT_AREA] - 1;
25267
25268 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25269 hl, 0);
25270 w->phys_cursor_on_p = on_p;
25271
25272 if (hl == DRAW_CURSOR)
25273 w->phys_cursor_width = x1 - w->phys_cursor.x;
25274 /* When we erase the cursor, and ROW is overlapped by other
25275 rows, make sure that these overlapping parts of other rows
25276 are redrawn. */
25277 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25278 {
25279 w->phys_cursor_width = x1 - w->phys_cursor.x;
25280
25281 if (row > w->current_matrix->rows
25282 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25283 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25284 OVERLAPS_ERASED_CURSOR);
25285
25286 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25287 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25288 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25289 OVERLAPS_ERASED_CURSOR);
25290 }
25291 }
25292 }
25293
25294
25295 /* EXPORT:
25296 Erase the image of a cursor of window W from the screen. */
25297
25298 void
25299 erase_phys_cursor (struct window *w)
25300 {
25301 struct frame *f = XFRAME (w->frame);
25302 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25303 int hpos = w->phys_cursor.hpos;
25304 int vpos = w->phys_cursor.vpos;
25305 int mouse_face_here_p = 0;
25306 struct glyph_matrix *active_glyphs = w->current_matrix;
25307 struct glyph_row *cursor_row;
25308 struct glyph *cursor_glyph;
25309 enum draw_glyphs_face hl;
25310
25311 /* No cursor displayed or row invalidated => nothing to do on the
25312 screen. */
25313 if (w->phys_cursor_type == NO_CURSOR)
25314 goto mark_cursor_off;
25315
25316 /* VPOS >= active_glyphs->nrows means that window has been resized.
25317 Don't bother to erase the cursor. */
25318 if (vpos >= active_glyphs->nrows)
25319 goto mark_cursor_off;
25320
25321 /* If row containing cursor is marked invalid, there is nothing we
25322 can do. */
25323 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25324 if (!cursor_row->enabled_p)
25325 goto mark_cursor_off;
25326
25327 /* If line spacing is > 0, old cursor may only be partially visible in
25328 window after split-window. So adjust visible height. */
25329 cursor_row->visible_height = min (cursor_row->visible_height,
25330 window_text_bottom_y (w) - cursor_row->y);
25331
25332 /* If row is completely invisible, don't attempt to delete a cursor which
25333 isn't there. This can happen if cursor is at top of a window, and
25334 we switch to a buffer with a header line in that window. */
25335 if (cursor_row->visible_height <= 0)
25336 goto mark_cursor_off;
25337
25338 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25339 if (cursor_row->cursor_in_fringe_p)
25340 {
25341 cursor_row->cursor_in_fringe_p = 0;
25342 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25343 goto mark_cursor_off;
25344 }
25345
25346 /* This can happen when the new row is shorter than the old one.
25347 In this case, either draw_glyphs or clear_end_of_line
25348 should have cleared the cursor. Note that we wouldn't be
25349 able to erase the cursor in this case because we don't have a
25350 cursor glyph at hand. */
25351 if ((cursor_row->reversed_p
25352 ? (w->phys_cursor.hpos < 0)
25353 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25354 goto mark_cursor_off;
25355
25356 /* When the window is hscrolled, cursor hpos can legitimately be out
25357 of bounds, but we draw the cursor at the corresponding window
25358 margin in that case. */
25359 if (!cursor_row->reversed_p && hpos < 0)
25360 hpos = 0;
25361 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25362 hpos = cursor_row->used[TEXT_AREA] - 1;
25363
25364 /* If the cursor is in the mouse face area, redisplay that when
25365 we clear the cursor. */
25366 if (! NILP (hlinfo->mouse_face_window)
25367 && coords_in_mouse_face_p (w, hpos, vpos)
25368 /* Don't redraw the cursor's spot in mouse face if it is at the
25369 end of a line (on a newline). The cursor appears there, but
25370 mouse highlighting does not. */
25371 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25372 mouse_face_here_p = 1;
25373
25374 /* Maybe clear the display under the cursor. */
25375 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25376 {
25377 int x, y, left_x;
25378 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25379 int width;
25380
25381 cursor_glyph = get_phys_cursor_glyph (w);
25382 if (cursor_glyph == NULL)
25383 goto mark_cursor_off;
25384
25385 width = cursor_glyph->pixel_width;
25386 left_x = window_box_left_offset (w, TEXT_AREA);
25387 x = w->phys_cursor.x;
25388 if (x < left_x)
25389 width -= left_x - x;
25390 width = min (width, window_box_width (w, TEXT_AREA) - x);
25391 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25392 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25393
25394 if (width > 0)
25395 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25396 }
25397
25398 /* Erase the cursor by redrawing the character underneath it. */
25399 if (mouse_face_here_p)
25400 hl = DRAW_MOUSE_FACE;
25401 else
25402 hl = DRAW_NORMAL_TEXT;
25403 draw_phys_cursor_glyph (w, cursor_row, hl);
25404
25405 mark_cursor_off:
25406 w->phys_cursor_on_p = 0;
25407 w->phys_cursor_type = NO_CURSOR;
25408 }
25409
25410
25411 /* EXPORT:
25412 Display or clear cursor of window W. If ON is zero, clear the
25413 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25414 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25415
25416 void
25417 display_and_set_cursor (struct window *w, int on,
25418 int hpos, int vpos, int x, int y)
25419 {
25420 struct frame *f = XFRAME (w->frame);
25421 int new_cursor_type;
25422 int new_cursor_width;
25423 int active_cursor;
25424 struct glyph_row *glyph_row;
25425 struct glyph *glyph;
25426
25427 /* This is pointless on invisible frames, and dangerous on garbaged
25428 windows and frames; in the latter case, the frame or window may
25429 be in the midst of changing its size, and x and y may be off the
25430 window. */
25431 if (! FRAME_VISIBLE_P (f)
25432 || FRAME_GARBAGED_P (f)
25433 || vpos >= w->current_matrix->nrows
25434 || hpos >= w->current_matrix->matrix_w)
25435 return;
25436
25437 /* If cursor is off and we want it off, return quickly. */
25438 if (!on && !w->phys_cursor_on_p)
25439 return;
25440
25441 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25442 /* If cursor row is not enabled, we don't really know where to
25443 display the cursor. */
25444 if (!glyph_row->enabled_p)
25445 {
25446 w->phys_cursor_on_p = 0;
25447 return;
25448 }
25449
25450 glyph = NULL;
25451 if (!glyph_row->exact_window_width_line_p
25452 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25453 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25454
25455 xassert (interrupt_input_blocked);
25456
25457 /* Set new_cursor_type to the cursor we want to be displayed. */
25458 new_cursor_type = get_window_cursor_type (w, glyph,
25459 &new_cursor_width, &active_cursor);
25460
25461 /* If cursor is currently being shown and we don't want it to be or
25462 it is in the wrong place, or the cursor type is not what we want,
25463 erase it. */
25464 if (w->phys_cursor_on_p
25465 && (!on
25466 || w->phys_cursor.x != x
25467 || w->phys_cursor.y != y
25468 || new_cursor_type != w->phys_cursor_type
25469 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25470 && new_cursor_width != w->phys_cursor_width)))
25471 erase_phys_cursor (w);
25472
25473 /* Don't check phys_cursor_on_p here because that flag is only set
25474 to zero in some cases where we know that the cursor has been
25475 completely erased, to avoid the extra work of erasing the cursor
25476 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25477 still not be visible, or it has only been partly erased. */
25478 if (on)
25479 {
25480 w->phys_cursor_ascent = glyph_row->ascent;
25481 w->phys_cursor_height = glyph_row->height;
25482
25483 /* Set phys_cursor_.* before x_draw_.* is called because some
25484 of them may need the information. */
25485 w->phys_cursor.x = x;
25486 w->phys_cursor.y = glyph_row->y;
25487 w->phys_cursor.hpos = hpos;
25488 w->phys_cursor.vpos = vpos;
25489 }
25490
25491 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25492 new_cursor_type, new_cursor_width,
25493 on, active_cursor);
25494 }
25495
25496
25497 /* Switch the display of W's cursor on or off, according to the value
25498 of ON. */
25499
25500 static void
25501 update_window_cursor (struct window *w, int on)
25502 {
25503 /* Don't update cursor in windows whose frame is in the process
25504 of being deleted. */
25505 if (w->current_matrix)
25506 {
25507 int hpos = w->phys_cursor.hpos;
25508 int vpos = w->phys_cursor.vpos;
25509 struct glyph_row *row;
25510
25511 if (vpos >= w->current_matrix->nrows
25512 || hpos >= w->current_matrix->matrix_w)
25513 return;
25514
25515 row = MATRIX_ROW (w->current_matrix, vpos);
25516
25517 /* When the window is hscrolled, cursor hpos can legitimately be
25518 out of bounds, but we draw the cursor at the corresponding
25519 window margin in that case. */
25520 if (!row->reversed_p && hpos < 0)
25521 hpos = 0;
25522 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25523 hpos = row->used[TEXT_AREA] - 1;
25524
25525 BLOCK_INPUT;
25526 display_and_set_cursor (w, on, hpos, vpos,
25527 w->phys_cursor.x, w->phys_cursor.y);
25528 UNBLOCK_INPUT;
25529 }
25530 }
25531
25532
25533 /* Call update_window_cursor with parameter ON_P on all leaf windows
25534 in the window tree rooted at W. */
25535
25536 static void
25537 update_cursor_in_window_tree (struct window *w, int on_p)
25538 {
25539 while (w)
25540 {
25541 if (!NILP (w->hchild))
25542 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25543 else if (!NILP (w->vchild))
25544 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25545 else
25546 update_window_cursor (w, on_p);
25547
25548 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25549 }
25550 }
25551
25552
25553 /* EXPORT:
25554 Display the cursor on window W, or clear it, according to ON_P.
25555 Don't change the cursor's position. */
25556
25557 void
25558 x_update_cursor (struct frame *f, int on_p)
25559 {
25560 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25561 }
25562
25563
25564 /* EXPORT:
25565 Clear the cursor of window W to background color, and mark the
25566 cursor as not shown. This is used when the text where the cursor
25567 is about to be rewritten. */
25568
25569 void
25570 x_clear_cursor (struct window *w)
25571 {
25572 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25573 update_window_cursor (w, 0);
25574 }
25575
25576 #endif /* HAVE_WINDOW_SYSTEM */
25577
25578 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25579 and MSDOS. */
25580 static void
25581 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25582 int start_hpos, int end_hpos,
25583 enum draw_glyphs_face draw)
25584 {
25585 #ifdef HAVE_WINDOW_SYSTEM
25586 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25587 {
25588 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25589 return;
25590 }
25591 #endif
25592 #if defined (HAVE_GPM) || defined (MSDOS)
25593 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25594 #endif
25595 }
25596
25597 /* Display the active region described by mouse_face_* according to DRAW. */
25598
25599 static void
25600 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25601 {
25602 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25603 struct frame *f = XFRAME (WINDOW_FRAME (w));
25604
25605 if (/* If window is in the process of being destroyed, don't bother
25606 to do anything. */
25607 w->current_matrix != NULL
25608 /* Don't update mouse highlight if hidden */
25609 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25610 /* Recognize when we are called to operate on rows that don't exist
25611 anymore. This can happen when a window is split. */
25612 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25613 {
25614 int phys_cursor_on_p = w->phys_cursor_on_p;
25615 struct glyph_row *row, *first, *last;
25616
25617 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25618 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25619
25620 for (row = first; row <= last && row->enabled_p; ++row)
25621 {
25622 int start_hpos, end_hpos, start_x;
25623
25624 /* For all but the first row, the highlight starts at column 0. */
25625 if (row == first)
25626 {
25627 /* R2L rows have BEG and END in reversed order, but the
25628 screen drawing geometry is always left to right. So
25629 we need to mirror the beginning and end of the
25630 highlighted area in R2L rows. */
25631 if (!row->reversed_p)
25632 {
25633 start_hpos = hlinfo->mouse_face_beg_col;
25634 start_x = hlinfo->mouse_face_beg_x;
25635 }
25636 else if (row == last)
25637 {
25638 start_hpos = hlinfo->mouse_face_end_col;
25639 start_x = hlinfo->mouse_face_end_x;
25640 }
25641 else
25642 {
25643 start_hpos = 0;
25644 start_x = 0;
25645 }
25646 }
25647 else if (row->reversed_p && row == last)
25648 {
25649 start_hpos = hlinfo->mouse_face_end_col;
25650 start_x = hlinfo->mouse_face_end_x;
25651 }
25652 else
25653 {
25654 start_hpos = 0;
25655 start_x = 0;
25656 }
25657
25658 if (row == last)
25659 {
25660 if (!row->reversed_p)
25661 end_hpos = hlinfo->mouse_face_end_col;
25662 else if (row == first)
25663 end_hpos = hlinfo->mouse_face_beg_col;
25664 else
25665 {
25666 end_hpos = row->used[TEXT_AREA];
25667 if (draw == DRAW_NORMAL_TEXT)
25668 row->fill_line_p = 1; /* Clear to end of line */
25669 }
25670 }
25671 else if (row->reversed_p && row == first)
25672 end_hpos = hlinfo->mouse_face_beg_col;
25673 else
25674 {
25675 end_hpos = row->used[TEXT_AREA];
25676 if (draw == DRAW_NORMAL_TEXT)
25677 row->fill_line_p = 1; /* Clear to end of line */
25678 }
25679
25680 if (end_hpos > start_hpos)
25681 {
25682 draw_row_with_mouse_face (w, start_x, row,
25683 start_hpos, end_hpos, draw);
25684
25685 row->mouse_face_p
25686 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25687 }
25688 }
25689
25690 #ifdef HAVE_WINDOW_SYSTEM
25691 /* When we've written over the cursor, arrange for it to
25692 be displayed again. */
25693 if (FRAME_WINDOW_P (f)
25694 && phys_cursor_on_p && !w->phys_cursor_on_p)
25695 {
25696 int hpos = w->phys_cursor.hpos;
25697
25698 /* When the window is hscrolled, cursor hpos can legitimately be
25699 out of bounds, but we draw the cursor at the corresponding
25700 window margin in that case. */
25701 if (!row->reversed_p && hpos < 0)
25702 hpos = 0;
25703 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25704 hpos = row->used[TEXT_AREA] - 1;
25705
25706 BLOCK_INPUT;
25707 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25708 w->phys_cursor.x, w->phys_cursor.y);
25709 UNBLOCK_INPUT;
25710 }
25711 #endif /* HAVE_WINDOW_SYSTEM */
25712 }
25713
25714 #ifdef HAVE_WINDOW_SYSTEM
25715 /* Change the mouse cursor. */
25716 if (FRAME_WINDOW_P (f))
25717 {
25718 if (draw == DRAW_NORMAL_TEXT
25719 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25720 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25721 else if (draw == DRAW_MOUSE_FACE)
25722 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25723 else
25724 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25725 }
25726 #endif /* HAVE_WINDOW_SYSTEM */
25727 }
25728
25729 /* EXPORT:
25730 Clear out the mouse-highlighted active region.
25731 Redraw it un-highlighted first. Value is non-zero if mouse
25732 face was actually drawn unhighlighted. */
25733
25734 int
25735 clear_mouse_face (Mouse_HLInfo *hlinfo)
25736 {
25737 int cleared = 0;
25738
25739 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25740 {
25741 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25742 cleared = 1;
25743 }
25744
25745 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25746 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25747 hlinfo->mouse_face_window = Qnil;
25748 hlinfo->mouse_face_overlay = Qnil;
25749 return cleared;
25750 }
25751
25752 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25753 within the mouse face on that window. */
25754 static int
25755 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25756 {
25757 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25758
25759 /* Quickly resolve the easy cases. */
25760 if (!(WINDOWP (hlinfo->mouse_face_window)
25761 && XWINDOW (hlinfo->mouse_face_window) == w))
25762 return 0;
25763 if (vpos < hlinfo->mouse_face_beg_row
25764 || vpos > hlinfo->mouse_face_end_row)
25765 return 0;
25766 if (vpos > hlinfo->mouse_face_beg_row
25767 && vpos < hlinfo->mouse_face_end_row)
25768 return 1;
25769
25770 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25771 {
25772 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25773 {
25774 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25775 return 1;
25776 }
25777 else if ((vpos == hlinfo->mouse_face_beg_row
25778 && hpos >= hlinfo->mouse_face_beg_col)
25779 || (vpos == hlinfo->mouse_face_end_row
25780 && hpos < hlinfo->mouse_face_end_col))
25781 return 1;
25782 }
25783 else
25784 {
25785 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25786 {
25787 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25788 return 1;
25789 }
25790 else if ((vpos == hlinfo->mouse_face_beg_row
25791 && hpos <= hlinfo->mouse_face_beg_col)
25792 || (vpos == hlinfo->mouse_face_end_row
25793 && hpos > hlinfo->mouse_face_end_col))
25794 return 1;
25795 }
25796 return 0;
25797 }
25798
25799
25800 /* EXPORT:
25801 Non-zero if physical cursor of window W is within mouse face. */
25802
25803 int
25804 cursor_in_mouse_face_p (struct window *w)
25805 {
25806 int hpos = w->phys_cursor.hpos;
25807 int vpos = w->phys_cursor.vpos;
25808 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25809
25810 /* When the window is hscrolled, cursor hpos can legitimately be out
25811 of bounds, but we draw the cursor at the corresponding window
25812 margin in that case. */
25813 if (!row->reversed_p && hpos < 0)
25814 hpos = 0;
25815 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25816 hpos = row->used[TEXT_AREA] - 1;
25817
25818 return coords_in_mouse_face_p (w, hpos, vpos);
25819 }
25820
25821
25822 \f
25823 /* Find the glyph rows START_ROW and END_ROW of window W that display
25824 characters between buffer positions START_CHARPOS and END_CHARPOS
25825 (excluding END_CHARPOS). DISP_STRING is a display string that
25826 covers these buffer positions. This is similar to
25827 row_containing_pos, but is more accurate when bidi reordering makes
25828 buffer positions change non-linearly with glyph rows. */
25829 static void
25830 rows_from_pos_range (struct window *w,
25831 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
25832 Lisp_Object disp_string,
25833 struct glyph_row **start, struct glyph_row **end)
25834 {
25835 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25836 int last_y = window_text_bottom_y (w);
25837 struct glyph_row *row;
25838
25839 *start = NULL;
25840 *end = NULL;
25841
25842 while (!first->enabled_p
25843 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25844 first++;
25845
25846 /* Find the START row. */
25847 for (row = first;
25848 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25849 row++)
25850 {
25851 /* A row can potentially be the START row if the range of the
25852 characters it displays intersects the range
25853 [START_CHARPOS..END_CHARPOS). */
25854 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25855 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25856 /* See the commentary in row_containing_pos, for the
25857 explanation of the complicated way to check whether
25858 some position is beyond the end of the characters
25859 displayed by a row. */
25860 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25861 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25862 && !row->ends_at_zv_p
25863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25864 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25865 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25866 && !row->ends_at_zv_p
25867 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25868 {
25869 /* Found a candidate row. Now make sure at least one of the
25870 glyphs it displays has a charpos from the range
25871 [START_CHARPOS..END_CHARPOS).
25872
25873 This is not obvious because bidi reordering could make
25874 buffer positions of a row be 1,2,3,102,101,100, and if we
25875 want to highlight characters in [50..60), we don't want
25876 this row, even though [50..60) does intersect [1..103),
25877 the range of character positions given by the row's start
25878 and end positions. */
25879 struct glyph *g = row->glyphs[TEXT_AREA];
25880 struct glyph *e = g + row->used[TEXT_AREA];
25881
25882 while (g < e)
25883 {
25884 if (((BUFFERP (g->object) || INTEGERP (g->object))
25885 && start_charpos <= g->charpos && g->charpos < end_charpos)
25886 /* A glyph that comes from DISP_STRING is by
25887 definition to be highlighted. */
25888 || EQ (g->object, disp_string))
25889 *start = row;
25890 g++;
25891 }
25892 if (*start)
25893 break;
25894 }
25895 }
25896
25897 /* Find the END row. */
25898 if (!*start
25899 /* If the last row is partially visible, start looking for END
25900 from that row, instead of starting from FIRST. */
25901 && !(row->enabled_p
25902 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25903 row = first;
25904 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25905 {
25906 struct glyph_row *next = row + 1;
25907 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
25908
25909 if (!next->enabled_p
25910 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25911 /* The first row >= START whose range of displayed characters
25912 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25913 is the row END + 1. */
25914 || (start_charpos < next_start
25915 && end_charpos < next_start)
25916 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25917 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25918 && !next->ends_at_zv_p
25919 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25920 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25921 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25922 && !next->ends_at_zv_p
25923 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25924 {
25925 *end = row;
25926 break;
25927 }
25928 else
25929 {
25930 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25931 but none of the characters it displays are in the range, it is
25932 also END + 1. */
25933 struct glyph *g = next->glyphs[TEXT_AREA];
25934 struct glyph *s = g;
25935 struct glyph *e = g + next->used[TEXT_AREA];
25936
25937 while (g < e)
25938 {
25939 if (((BUFFERP (g->object) || INTEGERP (g->object))
25940 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
25941 /* If the buffer position of the first glyph in
25942 the row is equal to END_CHARPOS, it means
25943 the last character to be highlighted is the
25944 newline of ROW, and we must consider NEXT as
25945 END, not END+1. */
25946 || (((!next->reversed_p && g == s)
25947 || (next->reversed_p && g == e - 1))
25948 && (g->charpos == end_charpos
25949 /* Special case for when NEXT is an
25950 empty line at ZV. */
25951 || (g->charpos == -1
25952 && !row->ends_at_zv_p
25953 && next_start == end_charpos)))))
25954 /* A glyph that comes from DISP_STRING is by
25955 definition to be highlighted. */
25956 || EQ (g->object, disp_string))
25957 break;
25958 g++;
25959 }
25960 if (g == e)
25961 {
25962 *end = row;
25963 break;
25964 }
25965 /* The first row that ends at ZV must be the last to be
25966 highlighted. */
25967 else if (next->ends_at_zv_p)
25968 {
25969 *end = next;
25970 break;
25971 }
25972 }
25973 }
25974 }
25975
25976 /* This function sets the mouse_face_* elements of HLINFO, assuming
25977 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25978 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25979 for the overlay or run of text properties specifying the mouse
25980 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25981 before-string and after-string that must also be highlighted.
25982 DISP_STRING, if non-nil, is a display string that may cover some
25983 or all of the highlighted text. */
25984
25985 static void
25986 mouse_face_from_buffer_pos (Lisp_Object window,
25987 Mouse_HLInfo *hlinfo,
25988 ptrdiff_t mouse_charpos,
25989 ptrdiff_t start_charpos,
25990 ptrdiff_t end_charpos,
25991 Lisp_Object before_string,
25992 Lisp_Object after_string,
25993 Lisp_Object disp_string)
25994 {
25995 struct window *w = XWINDOW (window);
25996 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25997 struct glyph_row *r1, *r2;
25998 struct glyph *glyph, *end;
25999 ptrdiff_t ignore, pos;
26000 int x;
26001
26002 xassert (NILP (disp_string) || STRINGP (disp_string));
26003 xassert (NILP (before_string) || STRINGP (before_string));
26004 xassert (NILP (after_string) || STRINGP (after_string));
26005
26006 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26007 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26008 if (r1 == NULL)
26009 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26010 /* If the before-string or display-string contains newlines,
26011 rows_from_pos_range skips to its last row. Move back. */
26012 if (!NILP (before_string) || !NILP (disp_string))
26013 {
26014 struct glyph_row *prev;
26015 while ((prev = r1 - 1, prev >= first)
26016 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26017 && prev->used[TEXT_AREA] > 0)
26018 {
26019 struct glyph *beg = prev->glyphs[TEXT_AREA];
26020 glyph = beg + prev->used[TEXT_AREA];
26021 while (--glyph >= beg && INTEGERP (glyph->object));
26022 if (glyph < beg
26023 || !(EQ (glyph->object, before_string)
26024 || EQ (glyph->object, disp_string)))
26025 break;
26026 r1 = prev;
26027 }
26028 }
26029 if (r2 == NULL)
26030 {
26031 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26032 hlinfo->mouse_face_past_end = 1;
26033 }
26034 else if (!NILP (after_string))
26035 {
26036 /* If the after-string has newlines, advance to its last row. */
26037 struct glyph_row *next;
26038 struct glyph_row *last
26039 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26040
26041 for (next = r2 + 1;
26042 next <= last
26043 && next->used[TEXT_AREA] > 0
26044 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26045 ++next)
26046 r2 = next;
26047 }
26048 /* The rest of the display engine assumes that mouse_face_beg_row is
26049 either above mouse_face_end_row or identical to it. But with
26050 bidi-reordered continued lines, the row for START_CHARPOS could
26051 be below the row for END_CHARPOS. If so, swap the rows and store
26052 them in correct order. */
26053 if (r1->y > r2->y)
26054 {
26055 struct glyph_row *tem = r2;
26056
26057 r2 = r1;
26058 r1 = tem;
26059 }
26060
26061 hlinfo->mouse_face_beg_y = r1->y;
26062 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26063 hlinfo->mouse_face_end_y = r2->y;
26064 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26065
26066 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26067 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26068 could be anywhere in the row and in any order. The strategy
26069 below is to find the leftmost and the rightmost glyph that
26070 belongs to either of these 3 strings, or whose position is
26071 between START_CHARPOS and END_CHARPOS, and highlight all the
26072 glyphs between those two. This may cover more than just the text
26073 between START_CHARPOS and END_CHARPOS if the range of characters
26074 strides the bidi level boundary, e.g. if the beginning is in R2L
26075 text while the end is in L2R text or vice versa. */
26076 if (!r1->reversed_p)
26077 {
26078 /* This row is in a left to right paragraph. Scan it left to
26079 right. */
26080 glyph = r1->glyphs[TEXT_AREA];
26081 end = glyph + r1->used[TEXT_AREA];
26082 x = r1->x;
26083
26084 /* Skip truncation glyphs at the start of the glyph row. */
26085 if (r1->displays_text_p)
26086 for (; glyph < end
26087 && INTEGERP (glyph->object)
26088 && glyph->charpos < 0;
26089 ++glyph)
26090 x += glyph->pixel_width;
26091
26092 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26093 or DISP_STRING, and the first glyph from buffer whose
26094 position is between START_CHARPOS and END_CHARPOS. */
26095 for (; glyph < end
26096 && !INTEGERP (glyph->object)
26097 && !EQ (glyph->object, disp_string)
26098 && !(BUFFERP (glyph->object)
26099 && (glyph->charpos >= start_charpos
26100 && glyph->charpos < end_charpos));
26101 ++glyph)
26102 {
26103 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26104 are present at buffer positions between START_CHARPOS and
26105 END_CHARPOS, or if they come from an overlay. */
26106 if (EQ (glyph->object, before_string))
26107 {
26108 pos = string_buffer_position (before_string,
26109 start_charpos);
26110 /* If pos == 0, it means before_string came from an
26111 overlay, not from a buffer position. */
26112 if (!pos || (pos >= start_charpos && pos < end_charpos))
26113 break;
26114 }
26115 else if (EQ (glyph->object, after_string))
26116 {
26117 pos = string_buffer_position (after_string, end_charpos);
26118 if (!pos || (pos >= start_charpos && pos < end_charpos))
26119 break;
26120 }
26121 x += glyph->pixel_width;
26122 }
26123 hlinfo->mouse_face_beg_x = x;
26124 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26125 }
26126 else
26127 {
26128 /* This row is in a right to left paragraph. Scan it right to
26129 left. */
26130 struct glyph *g;
26131
26132 end = r1->glyphs[TEXT_AREA] - 1;
26133 glyph = end + r1->used[TEXT_AREA];
26134
26135 /* Skip truncation glyphs at the start of the glyph row. */
26136 if (r1->displays_text_p)
26137 for (; glyph > end
26138 && INTEGERP (glyph->object)
26139 && glyph->charpos < 0;
26140 --glyph)
26141 ;
26142
26143 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26144 or DISP_STRING, and the first glyph from buffer whose
26145 position is between START_CHARPOS and END_CHARPOS. */
26146 for (; glyph > end
26147 && !INTEGERP (glyph->object)
26148 && !EQ (glyph->object, disp_string)
26149 && !(BUFFERP (glyph->object)
26150 && (glyph->charpos >= start_charpos
26151 && glyph->charpos < end_charpos));
26152 --glyph)
26153 {
26154 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26155 are present at buffer positions between START_CHARPOS and
26156 END_CHARPOS, or if they come from an overlay. */
26157 if (EQ (glyph->object, before_string))
26158 {
26159 pos = string_buffer_position (before_string, start_charpos);
26160 /* If pos == 0, it means before_string came from an
26161 overlay, not from a buffer position. */
26162 if (!pos || (pos >= start_charpos && pos < end_charpos))
26163 break;
26164 }
26165 else if (EQ (glyph->object, after_string))
26166 {
26167 pos = string_buffer_position (after_string, end_charpos);
26168 if (!pos || (pos >= start_charpos && pos < end_charpos))
26169 break;
26170 }
26171 }
26172
26173 glyph++; /* first glyph to the right of the highlighted area */
26174 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26175 x += g->pixel_width;
26176 hlinfo->mouse_face_beg_x = x;
26177 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26178 }
26179
26180 /* If the highlight ends in a different row, compute GLYPH and END
26181 for the end row. Otherwise, reuse the values computed above for
26182 the row where the highlight begins. */
26183 if (r2 != r1)
26184 {
26185 if (!r2->reversed_p)
26186 {
26187 glyph = r2->glyphs[TEXT_AREA];
26188 end = glyph + r2->used[TEXT_AREA];
26189 x = r2->x;
26190 }
26191 else
26192 {
26193 end = r2->glyphs[TEXT_AREA] - 1;
26194 glyph = end + r2->used[TEXT_AREA];
26195 }
26196 }
26197
26198 if (!r2->reversed_p)
26199 {
26200 /* Skip truncation and continuation glyphs near the end of the
26201 row, and also blanks and stretch glyphs inserted by
26202 extend_face_to_end_of_line. */
26203 while (end > glyph
26204 && INTEGERP ((end - 1)->object))
26205 --end;
26206 /* Scan the rest of the glyph row from the end, looking for the
26207 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26208 DISP_STRING, or whose position is between START_CHARPOS
26209 and END_CHARPOS */
26210 for (--end;
26211 end > glyph
26212 && !INTEGERP (end->object)
26213 && !EQ (end->object, disp_string)
26214 && !(BUFFERP (end->object)
26215 && (end->charpos >= start_charpos
26216 && end->charpos < end_charpos));
26217 --end)
26218 {
26219 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26220 are present at buffer positions between START_CHARPOS and
26221 END_CHARPOS, or if they come from an overlay. */
26222 if (EQ (end->object, before_string))
26223 {
26224 pos = string_buffer_position (before_string, start_charpos);
26225 if (!pos || (pos >= start_charpos && pos < end_charpos))
26226 break;
26227 }
26228 else if (EQ (end->object, after_string))
26229 {
26230 pos = string_buffer_position (after_string, end_charpos);
26231 if (!pos || (pos >= start_charpos && pos < end_charpos))
26232 break;
26233 }
26234 }
26235 /* Find the X coordinate of the last glyph to be highlighted. */
26236 for (; glyph <= end; ++glyph)
26237 x += glyph->pixel_width;
26238
26239 hlinfo->mouse_face_end_x = x;
26240 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26241 }
26242 else
26243 {
26244 /* Skip truncation and continuation glyphs near the end of the
26245 row, and also blanks and stretch glyphs inserted by
26246 extend_face_to_end_of_line. */
26247 x = r2->x;
26248 end++;
26249 while (end < glyph
26250 && INTEGERP (end->object))
26251 {
26252 x += end->pixel_width;
26253 ++end;
26254 }
26255 /* Scan the rest of the glyph row from the end, looking for the
26256 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26257 DISP_STRING, or whose position is between START_CHARPOS
26258 and END_CHARPOS */
26259 for ( ;
26260 end < glyph
26261 && !INTEGERP (end->object)
26262 && !EQ (end->object, disp_string)
26263 && !(BUFFERP (end->object)
26264 && (end->charpos >= start_charpos
26265 && end->charpos < end_charpos));
26266 ++end)
26267 {
26268 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26269 are present at buffer positions between START_CHARPOS and
26270 END_CHARPOS, or if they come from an overlay. */
26271 if (EQ (end->object, before_string))
26272 {
26273 pos = string_buffer_position (before_string, start_charpos);
26274 if (!pos || (pos >= start_charpos && pos < end_charpos))
26275 break;
26276 }
26277 else if (EQ (end->object, after_string))
26278 {
26279 pos = string_buffer_position (after_string, end_charpos);
26280 if (!pos || (pos >= start_charpos && pos < end_charpos))
26281 break;
26282 }
26283 x += end->pixel_width;
26284 }
26285 /* If we exited the above loop because we arrived at the last
26286 glyph of the row, and its buffer position is still not in
26287 range, it means the last character in range is the preceding
26288 newline. Bump the end column and x values to get past the
26289 last glyph. */
26290 if (end == glyph
26291 && BUFFERP (end->object)
26292 && (end->charpos < start_charpos
26293 || end->charpos >= end_charpos))
26294 {
26295 x += end->pixel_width;
26296 ++end;
26297 }
26298 hlinfo->mouse_face_end_x = x;
26299 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26300 }
26301
26302 hlinfo->mouse_face_window = window;
26303 hlinfo->mouse_face_face_id
26304 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26305 mouse_charpos + 1,
26306 !hlinfo->mouse_face_hidden, -1);
26307 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26308 }
26309
26310 /* The following function is not used anymore (replaced with
26311 mouse_face_from_string_pos), but I leave it here for the time
26312 being, in case someone would. */
26313
26314 #if 0 /* not used */
26315
26316 /* Find the position of the glyph for position POS in OBJECT in
26317 window W's current matrix, and return in *X, *Y the pixel
26318 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26319
26320 RIGHT_P non-zero means return the position of the right edge of the
26321 glyph, RIGHT_P zero means return the left edge position.
26322
26323 If no glyph for POS exists in the matrix, return the position of
26324 the glyph with the next smaller position that is in the matrix, if
26325 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26326 exists in the matrix, return the position of the glyph with the
26327 next larger position in OBJECT.
26328
26329 Value is non-zero if a glyph was found. */
26330
26331 static int
26332 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26333 int *hpos, int *vpos, int *x, int *y, int right_p)
26334 {
26335 int yb = window_text_bottom_y (w);
26336 struct glyph_row *r;
26337 struct glyph *best_glyph = NULL;
26338 struct glyph_row *best_row = NULL;
26339 int best_x = 0;
26340
26341 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26342 r->enabled_p && r->y < yb;
26343 ++r)
26344 {
26345 struct glyph *g = r->glyphs[TEXT_AREA];
26346 struct glyph *e = g + r->used[TEXT_AREA];
26347 int gx;
26348
26349 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26350 if (EQ (g->object, object))
26351 {
26352 if (g->charpos == pos)
26353 {
26354 best_glyph = g;
26355 best_x = gx;
26356 best_row = r;
26357 goto found;
26358 }
26359 else if (best_glyph == NULL
26360 || ((eabs (g->charpos - pos)
26361 < eabs (best_glyph->charpos - pos))
26362 && (right_p
26363 ? g->charpos < pos
26364 : g->charpos > pos)))
26365 {
26366 best_glyph = g;
26367 best_x = gx;
26368 best_row = r;
26369 }
26370 }
26371 }
26372
26373 found:
26374
26375 if (best_glyph)
26376 {
26377 *x = best_x;
26378 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26379
26380 if (right_p)
26381 {
26382 *x += best_glyph->pixel_width;
26383 ++*hpos;
26384 }
26385
26386 *y = best_row->y;
26387 *vpos = best_row - w->current_matrix->rows;
26388 }
26389
26390 return best_glyph != NULL;
26391 }
26392 #endif /* not used */
26393
26394 /* Find the positions of the first and the last glyphs in window W's
26395 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26396 (assumed to be a string), and return in HLINFO's mouse_face_*
26397 members the pixel and column/row coordinates of those glyphs. */
26398
26399 static void
26400 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26401 Lisp_Object object,
26402 ptrdiff_t startpos, ptrdiff_t endpos)
26403 {
26404 int yb = window_text_bottom_y (w);
26405 struct glyph_row *r;
26406 struct glyph *g, *e;
26407 int gx;
26408 int found = 0;
26409
26410 /* Find the glyph row with at least one position in the range
26411 [STARTPOS..ENDPOS], and the first glyph in that row whose
26412 position belongs to that range. */
26413 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26414 r->enabled_p && r->y < yb;
26415 ++r)
26416 {
26417 if (!r->reversed_p)
26418 {
26419 g = r->glyphs[TEXT_AREA];
26420 e = g + r->used[TEXT_AREA];
26421 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26422 if (EQ (g->object, object)
26423 && startpos <= g->charpos && g->charpos <= endpos)
26424 {
26425 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26426 hlinfo->mouse_face_beg_y = r->y;
26427 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26428 hlinfo->mouse_face_beg_x = gx;
26429 found = 1;
26430 break;
26431 }
26432 }
26433 else
26434 {
26435 struct glyph *g1;
26436
26437 e = r->glyphs[TEXT_AREA];
26438 g = e + r->used[TEXT_AREA];
26439 for ( ; g > e; --g)
26440 if (EQ ((g-1)->object, object)
26441 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26442 {
26443 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26444 hlinfo->mouse_face_beg_y = r->y;
26445 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26446 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26447 gx += g1->pixel_width;
26448 hlinfo->mouse_face_beg_x = gx;
26449 found = 1;
26450 break;
26451 }
26452 }
26453 if (found)
26454 break;
26455 }
26456
26457 if (!found)
26458 return;
26459
26460 /* Starting with the next row, look for the first row which does NOT
26461 include any glyphs whose positions are in the range. */
26462 for (++r; r->enabled_p && r->y < yb; ++r)
26463 {
26464 g = r->glyphs[TEXT_AREA];
26465 e = g + r->used[TEXT_AREA];
26466 found = 0;
26467 for ( ; g < e; ++g)
26468 if (EQ (g->object, object)
26469 && startpos <= g->charpos && g->charpos <= endpos)
26470 {
26471 found = 1;
26472 break;
26473 }
26474 if (!found)
26475 break;
26476 }
26477
26478 /* The highlighted region ends on the previous row. */
26479 r--;
26480
26481 /* Set the end row and its vertical pixel coordinate. */
26482 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26483 hlinfo->mouse_face_end_y = r->y;
26484
26485 /* Compute and set the end column and the end column's horizontal
26486 pixel coordinate. */
26487 if (!r->reversed_p)
26488 {
26489 g = r->glyphs[TEXT_AREA];
26490 e = g + r->used[TEXT_AREA];
26491 for ( ; e > g; --e)
26492 if (EQ ((e-1)->object, object)
26493 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26494 break;
26495 hlinfo->mouse_face_end_col = e - g;
26496
26497 for (gx = r->x; g < e; ++g)
26498 gx += g->pixel_width;
26499 hlinfo->mouse_face_end_x = gx;
26500 }
26501 else
26502 {
26503 e = r->glyphs[TEXT_AREA];
26504 g = e + r->used[TEXT_AREA];
26505 for (gx = r->x ; e < g; ++e)
26506 {
26507 if (EQ (e->object, object)
26508 && startpos <= e->charpos && e->charpos <= endpos)
26509 break;
26510 gx += e->pixel_width;
26511 }
26512 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26513 hlinfo->mouse_face_end_x = gx;
26514 }
26515 }
26516
26517 #ifdef HAVE_WINDOW_SYSTEM
26518
26519 /* See if position X, Y is within a hot-spot of an image. */
26520
26521 static int
26522 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26523 {
26524 if (!CONSP (hot_spot))
26525 return 0;
26526
26527 if (EQ (XCAR (hot_spot), Qrect))
26528 {
26529 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26530 Lisp_Object rect = XCDR (hot_spot);
26531 Lisp_Object tem;
26532 if (!CONSP (rect))
26533 return 0;
26534 if (!CONSP (XCAR (rect)))
26535 return 0;
26536 if (!CONSP (XCDR (rect)))
26537 return 0;
26538 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26539 return 0;
26540 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26541 return 0;
26542 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26543 return 0;
26544 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26545 return 0;
26546 return 1;
26547 }
26548 else if (EQ (XCAR (hot_spot), Qcircle))
26549 {
26550 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26551 Lisp_Object circ = XCDR (hot_spot);
26552 Lisp_Object lr, lx0, ly0;
26553 if (CONSP (circ)
26554 && CONSP (XCAR (circ))
26555 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26556 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26557 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26558 {
26559 double r = XFLOATINT (lr);
26560 double dx = XINT (lx0) - x;
26561 double dy = XINT (ly0) - y;
26562 return (dx * dx + dy * dy <= r * r);
26563 }
26564 }
26565 else if (EQ (XCAR (hot_spot), Qpoly))
26566 {
26567 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26568 if (VECTORP (XCDR (hot_spot)))
26569 {
26570 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26571 Lisp_Object *poly = v->contents;
26572 ptrdiff_t n = v->header.size;
26573 ptrdiff_t i;
26574 int inside = 0;
26575 Lisp_Object lx, ly;
26576 int x0, y0;
26577
26578 /* Need an even number of coordinates, and at least 3 edges. */
26579 if (n < 6 || n & 1)
26580 return 0;
26581
26582 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26583 If count is odd, we are inside polygon. Pixels on edges
26584 may or may not be included depending on actual geometry of the
26585 polygon. */
26586 if ((lx = poly[n-2], !INTEGERP (lx))
26587 || (ly = poly[n-1], !INTEGERP (lx)))
26588 return 0;
26589 x0 = XINT (lx), y0 = XINT (ly);
26590 for (i = 0; i < n; i += 2)
26591 {
26592 int x1 = x0, y1 = y0;
26593 if ((lx = poly[i], !INTEGERP (lx))
26594 || (ly = poly[i+1], !INTEGERP (ly)))
26595 return 0;
26596 x0 = XINT (lx), y0 = XINT (ly);
26597
26598 /* Does this segment cross the X line? */
26599 if (x0 >= x)
26600 {
26601 if (x1 >= x)
26602 continue;
26603 }
26604 else if (x1 < x)
26605 continue;
26606 if (y > y0 && y > y1)
26607 continue;
26608 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26609 inside = !inside;
26610 }
26611 return inside;
26612 }
26613 }
26614 return 0;
26615 }
26616
26617 Lisp_Object
26618 find_hot_spot (Lisp_Object map, int x, int y)
26619 {
26620 while (CONSP (map))
26621 {
26622 if (CONSP (XCAR (map))
26623 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26624 return XCAR (map);
26625 map = XCDR (map);
26626 }
26627
26628 return Qnil;
26629 }
26630
26631 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26632 3, 3, 0,
26633 doc: /* Lookup in image map MAP coordinates X and Y.
26634 An image map is an alist where each element has the format (AREA ID PLIST).
26635 An AREA is specified as either a rectangle, a circle, or a polygon:
26636 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26637 pixel coordinates of the upper left and bottom right corners.
26638 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26639 and the radius of the circle; r may be a float or integer.
26640 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26641 vector describes one corner in the polygon.
26642 Returns the alist element for the first matching AREA in MAP. */)
26643 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26644 {
26645 if (NILP (map))
26646 return Qnil;
26647
26648 CHECK_NUMBER (x);
26649 CHECK_NUMBER (y);
26650
26651 return find_hot_spot (map,
26652 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26653 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26654 }
26655
26656
26657 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26658 static void
26659 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26660 {
26661 /* Do not change cursor shape while dragging mouse. */
26662 if (!NILP (do_mouse_tracking))
26663 return;
26664
26665 if (!NILP (pointer))
26666 {
26667 if (EQ (pointer, Qarrow))
26668 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26669 else if (EQ (pointer, Qhand))
26670 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26671 else if (EQ (pointer, Qtext))
26672 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26673 else if (EQ (pointer, intern ("hdrag")))
26674 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26675 #ifdef HAVE_X_WINDOWS
26676 else if (EQ (pointer, intern ("vdrag")))
26677 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26678 #endif
26679 else if (EQ (pointer, intern ("hourglass")))
26680 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26681 else if (EQ (pointer, Qmodeline))
26682 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26683 else
26684 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26685 }
26686
26687 if (cursor != No_Cursor)
26688 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26689 }
26690
26691 #endif /* HAVE_WINDOW_SYSTEM */
26692
26693 /* Take proper action when mouse has moved to the mode or header line
26694 or marginal area AREA of window W, x-position X and y-position Y.
26695 X is relative to the start of the text display area of W, so the
26696 width of bitmap areas and scroll bars must be subtracted to get a
26697 position relative to the start of the mode line. */
26698
26699 static void
26700 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26701 enum window_part area)
26702 {
26703 struct window *w = XWINDOW (window);
26704 struct frame *f = XFRAME (w->frame);
26705 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26706 #ifdef HAVE_WINDOW_SYSTEM
26707 Display_Info *dpyinfo;
26708 #endif
26709 Cursor cursor = No_Cursor;
26710 Lisp_Object pointer = Qnil;
26711 int dx, dy, width, height;
26712 ptrdiff_t charpos;
26713 Lisp_Object string, object = Qnil;
26714 Lisp_Object pos, help;
26715
26716 Lisp_Object mouse_face;
26717 int original_x_pixel = x;
26718 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26719 struct glyph_row *row;
26720
26721 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26722 {
26723 int x0;
26724 struct glyph *end;
26725
26726 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26727 returns them in row/column units! */
26728 string = mode_line_string (w, area, &x, &y, &charpos,
26729 &object, &dx, &dy, &width, &height);
26730
26731 row = (area == ON_MODE_LINE
26732 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26733 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26734
26735 /* Find the glyph under the mouse pointer. */
26736 if (row->mode_line_p && row->enabled_p)
26737 {
26738 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26739 end = glyph + row->used[TEXT_AREA];
26740
26741 for (x0 = original_x_pixel;
26742 glyph < end && x0 >= glyph->pixel_width;
26743 ++glyph)
26744 x0 -= glyph->pixel_width;
26745
26746 if (glyph >= end)
26747 glyph = NULL;
26748 }
26749 }
26750 else
26751 {
26752 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26753 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26754 returns them in row/column units! */
26755 string = marginal_area_string (w, area, &x, &y, &charpos,
26756 &object, &dx, &dy, &width, &height);
26757 }
26758
26759 help = Qnil;
26760
26761 #ifdef HAVE_WINDOW_SYSTEM
26762 if (IMAGEP (object))
26763 {
26764 Lisp_Object image_map, hotspot;
26765 if ((image_map = Fplist_get (XCDR (object), QCmap),
26766 !NILP (image_map))
26767 && (hotspot = find_hot_spot (image_map, dx, dy),
26768 CONSP (hotspot))
26769 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26770 {
26771 Lisp_Object plist;
26772
26773 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26774 If so, we could look for mouse-enter, mouse-leave
26775 properties in PLIST (and do something...). */
26776 hotspot = XCDR (hotspot);
26777 if (CONSP (hotspot)
26778 && (plist = XCAR (hotspot), CONSP (plist)))
26779 {
26780 pointer = Fplist_get (plist, Qpointer);
26781 if (NILP (pointer))
26782 pointer = Qhand;
26783 help = Fplist_get (plist, Qhelp_echo);
26784 if (!NILP (help))
26785 {
26786 help_echo_string = help;
26787 /* Is this correct? ++kfs */
26788 XSETWINDOW (help_echo_window, w);
26789 help_echo_object = w->buffer;
26790 help_echo_pos = charpos;
26791 }
26792 }
26793 }
26794 if (NILP (pointer))
26795 pointer = Fplist_get (XCDR (object), QCpointer);
26796 }
26797 #endif /* HAVE_WINDOW_SYSTEM */
26798
26799 if (STRINGP (string))
26800 {
26801 pos = make_number (charpos);
26802 /* If we're on a string with `help-echo' text property, arrange
26803 for the help to be displayed. This is done by setting the
26804 global variable help_echo_string to the help string. */
26805 if (NILP (help))
26806 {
26807 help = Fget_text_property (pos, Qhelp_echo, string);
26808 if (!NILP (help))
26809 {
26810 help_echo_string = help;
26811 XSETWINDOW (help_echo_window, w);
26812 help_echo_object = string;
26813 help_echo_pos = charpos;
26814 }
26815 }
26816
26817 #ifdef HAVE_WINDOW_SYSTEM
26818 if (FRAME_WINDOW_P (f))
26819 {
26820 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26821 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26822 if (NILP (pointer))
26823 pointer = Fget_text_property (pos, Qpointer, string);
26824
26825 /* Change the mouse pointer according to what is under X/Y. */
26826 if (NILP (pointer)
26827 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26828 {
26829 Lisp_Object map;
26830 map = Fget_text_property (pos, Qlocal_map, string);
26831 if (!KEYMAPP (map))
26832 map = Fget_text_property (pos, Qkeymap, string);
26833 if (!KEYMAPP (map))
26834 cursor = dpyinfo->vertical_scroll_bar_cursor;
26835 }
26836 }
26837 #endif
26838
26839 /* Change the mouse face according to what is under X/Y. */
26840 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26841 if (!NILP (mouse_face)
26842 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26843 && glyph)
26844 {
26845 Lisp_Object b, e;
26846
26847 struct glyph * tmp_glyph;
26848
26849 int gpos;
26850 int gseq_length;
26851 int total_pixel_width;
26852 ptrdiff_t begpos, endpos, ignore;
26853
26854 int vpos, hpos;
26855
26856 b = Fprevious_single_property_change (make_number (charpos + 1),
26857 Qmouse_face, string, Qnil);
26858 if (NILP (b))
26859 begpos = 0;
26860 else
26861 begpos = XINT (b);
26862
26863 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26864 if (NILP (e))
26865 endpos = SCHARS (string);
26866 else
26867 endpos = XINT (e);
26868
26869 /* Calculate the glyph position GPOS of GLYPH in the
26870 displayed string, relative to the beginning of the
26871 highlighted part of the string.
26872
26873 Note: GPOS is different from CHARPOS. CHARPOS is the
26874 position of GLYPH in the internal string object. A mode
26875 line string format has structures which are converted to
26876 a flattened string by the Emacs Lisp interpreter. The
26877 internal string is an element of those structures. The
26878 displayed string is the flattened string. */
26879 tmp_glyph = row_start_glyph;
26880 while (tmp_glyph < glyph
26881 && (!(EQ (tmp_glyph->object, glyph->object)
26882 && begpos <= tmp_glyph->charpos
26883 && tmp_glyph->charpos < endpos)))
26884 tmp_glyph++;
26885 gpos = glyph - tmp_glyph;
26886
26887 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26888 the highlighted part of the displayed string to which
26889 GLYPH belongs. Note: GSEQ_LENGTH is different from
26890 SCHARS (STRING), because the latter returns the length of
26891 the internal string. */
26892 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26893 tmp_glyph > glyph
26894 && (!(EQ (tmp_glyph->object, glyph->object)
26895 && begpos <= tmp_glyph->charpos
26896 && tmp_glyph->charpos < endpos));
26897 tmp_glyph--)
26898 ;
26899 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26900
26901 /* Calculate the total pixel width of all the glyphs between
26902 the beginning of the highlighted area and GLYPH. */
26903 total_pixel_width = 0;
26904 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26905 total_pixel_width += tmp_glyph->pixel_width;
26906
26907 /* Pre calculation of re-rendering position. Note: X is in
26908 column units here, after the call to mode_line_string or
26909 marginal_area_string. */
26910 hpos = x - gpos;
26911 vpos = (area == ON_MODE_LINE
26912 ? (w->current_matrix)->nrows - 1
26913 : 0);
26914
26915 /* If GLYPH's position is included in the region that is
26916 already drawn in mouse face, we have nothing to do. */
26917 if ( EQ (window, hlinfo->mouse_face_window)
26918 && (!row->reversed_p
26919 ? (hlinfo->mouse_face_beg_col <= hpos
26920 && hpos < hlinfo->mouse_face_end_col)
26921 /* In R2L rows we swap BEG and END, see below. */
26922 : (hlinfo->mouse_face_end_col <= hpos
26923 && hpos < hlinfo->mouse_face_beg_col))
26924 && hlinfo->mouse_face_beg_row == vpos )
26925 return;
26926
26927 if (clear_mouse_face (hlinfo))
26928 cursor = No_Cursor;
26929
26930 if (!row->reversed_p)
26931 {
26932 hlinfo->mouse_face_beg_col = hpos;
26933 hlinfo->mouse_face_beg_x = original_x_pixel
26934 - (total_pixel_width + dx);
26935 hlinfo->mouse_face_end_col = hpos + gseq_length;
26936 hlinfo->mouse_face_end_x = 0;
26937 }
26938 else
26939 {
26940 /* In R2L rows, show_mouse_face expects BEG and END
26941 coordinates to be swapped. */
26942 hlinfo->mouse_face_end_col = hpos;
26943 hlinfo->mouse_face_end_x = original_x_pixel
26944 - (total_pixel_width + dx);
26945 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26946 hlinfo->mouse_face_beg_x = 0;
26947 }
26948
26949 hlinfo->mouse_face_beg_row = vpos;
26950 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26951 hlinfo->mouse_face_beg_y = 0;
26952 hlinfo->mouse_face_end_y = 0;
26953 hlinfo->mouse_face_past_end = 0;
26954 hlinfo->mouse_face_window = window;
26955
26956 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26957 charpos,
26958 0, 0, 0,
26959 &ignore,
26960 glyph->face_id,
26961 1);
26962 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26963
26964 if (NILP (pointer))
26965 pointer = Qhand;
26966 }
26967 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26968 clear_mouse_face (hlinfo);
26969 }
26970 #ifdef HAVE_WINDOW_SYSTEM
26971 if (FRAME_WINDOW_P (f))
26972 define_frame_cursor1 (f, cursor, pointer);
26973 #endif
26974 }
26975
26976
26977 /* EXPORT:
26978 Take proper action when the mouse has moved to position X, Y on
26979 frame F as regards highlighting characters that have mouse-face
26980 properties. Also de-highlighting chars where the mouse was before.
26981 X and Y can be negative or out of range. */
26982
26983 void
26984 note_mouse_highlight (struct frame *f, int x, int y)
26985 {
26986 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26987 enum window_part part = ON_NOTHING;
26988 Lisp_Object window;
26989 struct window *w;
26990 Cursor cursor = No_Cursor;
26991 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26992 struct buffer *b;
26993
26994 /* When a menu is active, don't highlight because this looks odd. */
26995 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26996 if (popup_activated ())
26997 return;
26998 #endif
26999
27000 if (NILP (Vmouse_highlight)
27001 || !f->glyphs_initialized_p
27002 || f->pointer_invisible)
27003 return;
27004
27005 hlinfo->mouse_face_mouse_x = x;
27006 hlinfo->mouse_face_mouse_y = y;
27007 hlinfo->mouse_face_mouse_frame = f;
27008
27009 if (hlinfo->mouse_face_defer)
27010 return;
27011
27012 if (gc_in_progress)
27013 {
27014 hlinfo->mouse_face_deferred_gc = 1;
27015 return;
27016 }
27017
27018 /* Which window is that in? */
27019 window = window_from_coordinates (f, x, y, &part, 1);
27020
27021 /* If displaying active text in another window, clear that. */
27022 if (! EQ (window, hlinfo->mouse_face_window)
27023 /* Also clear if we move out of text area in same window. */
27024 || (!NILP (hlinfo->mouse_face_window)
27025 && !NILP (window)
27026 && part != ON_TEXT
27027 && part != ON_MODE_LINE
27028 && part != ON_HEADER_LINE))
27029 clear_mouse_face (hlinfo);
27030
27031 /* Not on a window -> return. */
27032 if (!WINDOWP (window))
27033 return;
27034
27035 /* Reset help_echo_string. It will get recomputed below. */
27036 help_echo_string = Qnil;
27037
27038 /* Convert to window-relative pixel coordinates. */
27039 w = XWINDOW (window);
27040 frame_to_window_pixel_xy (w, &x, &y);
27041
27042 #ifdef HAVE_WINDOW_SYSTEM
27043 /* Handle tool-bar window differently since it doesn't display a
27044 buffer. */
27045 if (EQ (window, f->tool_bar_window))
27046 {
27047 note_tool_bar_highlight (f, x, y);
27048 return;
27049 }
27050 #endif
27051
27052 /* Mouse is on the mode, header line or margin? */
27053 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27054 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27055 {
27056 note_mode_line_or_margin_highlight (window, x, y, part);
27057 return;
27058 }
27059
27060 #ifdef HAVE_WINDOW_SYSTEM
27061 if (part == ON_VERTICAL_BORDER)
27062 {
27063 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27064 help_echo_string = build_string ("drag-mouse-1: resize");
27065 }
27066 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27067 || part == ON_SCROLL_BAR)
27068 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27069 else
27070 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27071 #endif
27072
27073 /* Are we in a window whose display is up to date?
27074 And verify the buffer's text has not changed. */
27075 b = XBUFFER (w->buffer);
27076 if (part == ON_TEXT
27077 && EQ (w->window_end_valid, w->buffer)
27078 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27079 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27080 {
27081 int hpos, vpos, dx, dy, area = LAST_AREA;
27082 ptrdiff_t pos;
27083 struct glyph *glyph;
27084 Lisp_Object object;
27085 Lisp_Object mouse_face = Qnil, position;
27086 Lisp_Object *overlay_vec = NULL;
27087 ptrdiff_t i, noverlays;
27088 struct buffer *obuf;
27089 ptrdiff_t obegv, ozv;
27090 int same_region;
27091
27092 /* Find the glyph under X/Y. */
27093 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27094
27095 #ifdef HAVE_WINDOW_SYSTEM
27096 /* Look for :pointer property on image. */
27097 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27098 {
27099 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27100 if (img != NULL && IMAGEP (img->spec))
27101 {
27102 Lisp_Object image_map, hotspot;
27103 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27104 !NILP (image_map))
27105 && (hotspot = find_hot_spot (image_map,
27106 glyph->slice.img.x + dx,
27107 glyph->slice.img.y + dy),
27108 CONSP (hotspot))
27109 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27110 {
27111 Lisp_Object plist;
27112
27113 /* Could check XCAR (hotspot) to see if we enter/leave
27114 this hot-spot.
27115 If so, we could look for mouse-enter, mouse-leave
27116 properties in PLIST (and do something...). */
27117 hotspot = XCDR (hotspot);
27118 if (CONSP (hotspot)
27119 && (plist = XCAR (hotspot), CONSP (plist)))
27120 {
27121 pointer = Fplist_get (plist, Qpointer);
27122 if (NILP (pointer))
27123 pointer = Qhand;
27124 help_echo_string = Fplist_get (plist, Qhelp_echo);
27125 if (!NILP (help_echo_string))
27126 {
27127 help_echo_window = window;
27128 help_echo_object = glyph->object;
27129 help_echo_pos = glyph->charpos;
27130 }
27131 }
27132 }
27133 if (NILP (pointer))
27134 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27135 }
27136 }
27137 #endif /* HAVE_WINDOW_SYSTEM */
27138
27139 /* Clear mouse face if X/Y not over text. */
27140 if (glyph == NULL
27141 || area != TEXT_AREA
27142 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27143 /* Glyph's OBJECT is an integer for glyphs inserted by the
27144 display engine for its internal purposes, like truncation
27145 and continuation glyphs and blanks beyond the end of
27146 line's text on text terminals. If we are over such a
27147 glyph, we are not over any text. */
27148 || INTEGERP (glyph->object)
27149 /* R2L rows have a stretch glyph at their front, which
27150 stands for no text, whereas L2R rows have no glyphs at
27151 all beyond the end of text. Treat such stretch glyphs
27152 like we do with NULL glyphs in L2R rows. */
27153 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27154 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27155 && glyph->type == STRETCH_GLYPH
27156 && glyph->avoid_cursor_p))
27157 {
27158 if (clear_mouse_face (hlinfo))
27159 cursor = No_Cursor;
27160 #ifdef HAVE_WINDOW_SYSTEM
27161 if (FRAME_WINDOW_P (f) && NILP (pointer))
27162 {
27163 if (area != TEXT_AREA)
27164 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27165 else
27166 pointer = Vvoid_text_area_pointer;
27167 }
27168 #endif
27169 goto set_cursor;
27170 }
27171
27172 pos = glyph->charpos;
27173 object = glyph->object;
27174 if (!STRINGP (object) && !BUFFERP (object))
27175 goto set_cursor;
27176
27177 /* If we get an out-of-range value, return now; avoid an error. */
27178 if (BUFFERP (object) && pos > BUF_Z (b))
27179 goto set_cursor;
27180
27181 /* Make the window's buffer temporarily current for
27182 overlays_at and compute_char_face. */
27183 obuf = current_buffer;
27184 current_buffer = b;
27185 obegv = BEGV;
27186 ozv = ZV;
27187 BEGV = BEG;
27188 ZV = Z;
27189
27190 /* Is this char mouse-active or does it have help-echo? */
27191 position = make_number (pos);
27192
27193 if (BUFFERP (object))
27194 {
27195 /* Put all the overlays we want in a vector in overlay_vec. */
27196 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27197 /* Sort overlays into increasing priority order. */
27198 noverlays = sort_overlays (overlay_vec, noverlays, w);
27199 }
27200 else
27201 noverlays = 0;
27202
27203 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27204
27205 if (same_region)
27206 cursor = No_Cursor;
27207
27208 /* Check mouse-face highlighting. */
27209 if (! same_region
27210 /* If there exists an overlay with mouse-face overlapping
27211 the one we are currently highlighting, we have to
27212 check if we enter the overlapping overlay, and then
27213 highlight only that. */
27214 || (OVERLAYP (hlinfo->mouse_face_overlay)
27215 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27216 {
27217 /* Find the highest priority overlay with a mouse-face. */
27218 Lisp_Object overlay = Qnil;
27219 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27220 {
27221 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27222 if (!NILP (mouse_face))
27223 overlay = overlay_vec[i];
27224 }
27225
27226 /* If we're highlighting the same overlay as before, there's
27227 no need to do that again. */
27228 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27229 goto check_help_echo;
27230 hlinfo->mouse_face_overlay = overlay;
27231
27232 /* Clear the display of the old active region, if any. */
27233 if (clear_mouse_face (hlinfo))
27234 cursor = No_Cursor;
27235
27236 /* If no overlay applies, get a text property. */
27237 if (NILP (overlay))
27238 mouse_face = Fget_text_property (position, Qmouse_face, object);
27239
27240 /* Next, compute the bounds of the mouse highlighting and
27241 display it. */
27242 if (!NILP (mouse_face) && STRINGP (object))
27243 {
27244 /* The mouse-highlighting comes from a display string
27245 with a mouse-face. */
27246 Lisp_Object s, e;
27247 ptrdiff_t ignore;
27248
27249 s = Fprevious_single_property_change
27250 (make_number (pos + 1), Qmouse_face, object, Qnil);
27251 e = Fnext_single_property_change
27252 (position, Qmouse_face, object, Qnil);
27253 if (NILP (s))
27254 s = make_number (0);
27255 if (NILP (e))
27256 e = make_number (SCHARS (object) - 1);
27257 mouse_face_from_string_pos (w, hlinfo, object,
27258 XINT (s), XINT (e));
27259 hlinfo->mouse_face_past_end = 0;
27260 hlinfo->mouse_face_window = window;
27261 hlinfo->mouse_face_face_id
27262 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27263 glyph->face_id, 1);
27264 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27265 cursor = No_Cursor;
27266 }
27267 else
27268 {
27269 /* The mouse-highlighting, if any, comes from an overlay
27270 or text property in the buffer. */
27271 Lisp_Object buffer IF_LINT (= Qnil);
27272 Lisp_Object disp_string IF_LINT (= Qnil);
27273
27274 if (STRINGP (object))
27275 {
27276 /* If we are on a display string with no mouse-face,
27277 check if the text under it has one. */
27278 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27279 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27280 pos = string_buffer_position (object, start);
27281 if (pos > 0)
27282 {
27283 mouse_face = get_char_property_and_overlay
27284 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27285 buffer = w->buffer;
27286 disp_string = object;
27287 }
27288 }
27289 else
27290 {
27291 buffer = object;
27292 disp_string = Qnil;
27293 }
27294
27295 if (!NILP (mouse_face))
27296 {
27297 Lisp_Object before, after;
27298 Lisp_Object before_string, after_string;
27299 /* To correctly find the limits of mouse highlight
27300 in a bidi-reordered buffer, we must not use the
27301 optimization of limiting the search in
27302 previous-single-property-change and
27303 next-single-property-change, because
27304 rows_from_pos_range needs the real start and end
27305 positions to DTRT in this case. That's because
27306 the first row visible in a window does not
27307 necessarily display the character whose position
27308 is the smallest. */
27309 Lisp_Object lim1 =
27310 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27311 ? Fmarker_position (w->start)
27312 : Qnil;
27313 Lisp_Object lim2 =
27314 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27315 ? make_number (BUF_Z (XBUFFER (buffer))
27316 - XFASTINT (w->window_end_pos))
27317 : Qnil;
27318
27319 if (NILP (overlay))
27320 {
27321 /* Handle the text property case. */
27322 before = Fprevious_single_property_change
27323 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27324 after = Fnext_single_property_change
27325 (make_number (pos), Qmouse_face, buffer, lim2);
27326 before_string = after_string = Qnil;
27327 }
27328 else
27329 {
27330 /* Handle the overlay case. */
27331 before = Foverlay_start (overlay);
27332 after = Foverlay_end (overlay);
27333 before_string = Foverlay_get (overlay, Qbefore_string);
27334 after_string = Foverlay_get (overlay, Qafter_string);
27335
27336 if (!STRINGP (before_string)) before_string = Qnil;
27337 if (!STRINGP (after_string)) after_string = Qnil;
27338 }
27339
27340 mouse_face_from_buffer_pos (window, hlinfo, pos,
27341 NILP (before)
27342 ? 1
27343 : XFASTINT (before),
27344 NILP (after)
27345 ? BUF_Z (XBUFFER (buffer))
27346 : XFASTINT (after),
27347 before_string, after_string,
27348 disp_string);
27349 cursor = No_Cursor;
27350 }
27351 }
27352 }
27353
27354 check_help_echo:
27355
27356 /* Look for a `help-echo' property. */
27357 if (NILP (help_echo_string)) {
27358 Lisp_Object help, overlay;
27359
27360 /* Check overlays first. */
27361 help = overlay = Qnil;
27362 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27363 {
27364 overlay = overlay_vec[i];
27365 help = Foverlay_get (overlay, Qhelp_echo);
27366 }
27367
27368 if (!NILP (help))
27369 {
27370 help_echo_string = help;
27371 help_echo_window = window;
27372 help_echo_object = overlay;
27373 help_echo_pos = pos;
27374 }
27375 else
27376 {
27377 Lisp_Object obj = glyph->object;
27378 ptrdiff_t charpos = glyph->charpos;
27379
27380 /* Try text properties. */
27381 if (STRINGP (obj)
27382 && charpos >= 0
27383 && charpos < SCHARS (obj))
27384 {
27385 help = Fget_text_property (make_number (charpos),
27386 Qhelp_echo, obj);
27387 if (NILP (help))
27388 {
27389 /* If the string itself doesn't specify a help-echo,
27390 see if the buffer text ``under'' it does. */
27391 struct glyph_row *r
27392 = MATRIX_ROW (w->current_matrix, vpos);
27393 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27394 ptrdiff_t p = string_buffer_position (obj, start);
27395 if (p > 0)
27396 {
27397 help = Fget_char_property (make_number (p),
27398 Qhelp_echo, w->buffer);
27399 if (!NILP (help))
27400 {
27401 charpos = p;
27402 obj = w->buffer;
27403 }
27404 }
27405 }
27406 }
27407 else if (BUFFERP (obj)
27408 && charpos >= BEGV
27409 && charpos < ZV)
27410 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27411 obj);
27412
27413 if (!NILP (help))
27414 {
27415 help_echo_string = help;
27416 help_echo_window = window;
27417 help_echo_object = obj;
27418 help_echo_pos = charpos;
27419 }
27420 }
27421 }
27422
27423 #ifdef HAVE_WINDOW_SYSTEM
27424 /* Look for a `pointer' property. */
27425 if (FRAME_WINDOW_P (f) && NILP (pointer))
27426 {
27427 /* Check overlays first. */
27428 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27429 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27430
27431 if (NILP (pointer))
27432 {
27433 Lisp_Object obj = glyph->object;
27434 ptrdiff_t charpos = glyph->charpos;
27435
27436 /* Try text properties. */
27437 if (STRINGP (obj)
27438 && charpos >= 0
27439 && charpos < SCHARS (obj))
27440 {
27441 pointer = Fget_text_property (make_number (charpos),
27442 Qpointer, obj);
27443 if (NILP (pointer))
27444 {
27445 /* If the string itself doesn't specify a pointer,
27446 see if the buffer text ``under'' it does. */
27447 struct glyph_row *r
27448 = MATRIX_ROW (w->current_matrix, vpos);
27449 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27450 ptrdiff_t p = string_buffer_position (obj, start);
27451 if (p > 0)
27452 pointer = Fget_char_property (make_number (p),
27453 Qpointer, w->buffer);
27454 }
27455 }
27456 else if (BUFFERP (obj)
27457 && charpos >= BEGV
27458 && charpos < ZV)
27459 pointer = Fget_text_property (make_number (charpos),
27460 Qpointer, obj);
27461 }
27462 }
27463 #endif /* HAVE_WINDOW_SYSTEM */
27464
27465 BEGV = obegv;
27466 ZV = ozv;
27467 current_buffer = obuf;
27468 }
27469
27470 set_cursor:
27471
27472 #ifdef HAVE_WINDOW_SYSTEM
27473 if (FRAME_WINDOW_P (f))
27474 define_frame_cursor1 (f, cursor, pointer);
27475 #else
27476 /* This is here to prevent a compiler error, about "label at end of
27477 compound statement". */
27478 return;
27479 #endif
27480 }
27481
27482
27483 /* EXPORT for RIF:
27484 Clear any mouse-face on window W. This function is part of the
27485 redisplay interface, and is called from try_window_id and similar
27486 functions to ensure the mouse-highlight is off. */
27487
27488 void
27489 x_clear_window_mouse_face (struct window *w)
27490 {
27491 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27492 Lisp_Object window;
27493
27494 BLOCK_INPUT;
27495 XSETWINDOW (window, w);
27496 if (EQ (window, hlinfo->mouse_face_window))
27497 clear_mouse_face (hlinfo);
27498 UNBLOCK_INPUT;
27499 }
27500
27501
27502 /* EXPORT:
27503 Just discard the mouse face information for frame F, if any.
27504 This is used when the size of F is changed. */
27505
27506 void
27507 cancel_mouse_face (struct frame *f)
27508 {
27509 Lisp_Object window;
27510 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27511
27512 window = hlinfo->mouse_face_window;
27513 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27514 {
27515 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27516 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27517 hlinfo->mouse_face_window = Qnil;
27518 }
27519 }
27520
27521
27522 \f
27523 /***********************************************************************
27524 Exposure Events
27525 ***********************************************************************/
27526
27527 #ifdef HAVE_WINDOW_SYSTEM
27528
27529 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27530 which intersects rectangle R. R is in window-relative coordinates. */
27531
27532 static void
27533 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27534 enum glyph_row_area area)
27535 {
27536 struct glyph *first = row->glyphs[area];
27537 struct glyph *end = row->glyphs[area] + row->used[area];
27538 struct glyph *last;
27539 int first_x, start_x, x;
27540
27541 if (area == TEXT_AREA && row->fill_line_p)
27542 /* If row extends face to end of line write the whole line. */
27543 draw_glyphs (w, 0, row, area,
27544 0, row->used[area],
27545 DRAW_NORMAL_TEXT, 0);
27546 else
27547 {
27548 /* Set START_X to the window-relative start position for drawing glyphs of
27549 AREA. The first glyph of the text area can be partially visible.
27550 The first glyphs of other areas cannot. */
27551 start_x = window_box_left_offset (w, area);
27552 x = start_x;
27553 if (area == TEXT_AREA)
27554 x += row->x;
27555
27556 /* Find the first glyph that must be redrawn. */
27557 while (first < end
27558 && x + first->pixel_width < r->x)
27559 {
27560 x += first->pixel_width;
27561 ++first;
27562 }
27563
27564 /* Find the last one. */
27565 last = first;
27566 first_x = x;
27567 while (last < end
27568 && x < r->x + r->width)
27569 {
27570 x += last->pixel_width;
27571 ++last;
27572 }
27573
27574 /* Repaint. */
27575 if (last > first)
27576 draw_glyphs (w, first_x - start_x, row, area,
27577 first - row->glyphs[area], last - row->glyphs[area],
27578 DRAW_NORMAL_TEXT, 0);
27579 }
27580 }
27581
27582
27583 /* Redraw the parts of the glyph row ROW on window W intersecting
27584 rectangle R. R is in window-relative coordinates. Value is
27585 non-zero if mouse-face was overwritten. */
27586
27587 static int
27588 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27589 {
27590 xassert (row->enabled_p);
27591
27592 if (row->mode_line_p || w->pseudo_window_p)
27593 draw_glyphs (w, 0, row, TEXT_AREA,
27594 0, row->used[TEXT_AREA],
27595 DRAW_NORMAL_TEXT, 0);
27596 else
27597 {
27598 if (row->used[LEFT_MARGIN_AREA])
27599 expose_area (w, row, r, LEFT_MARGIN_AREA);
27600 if (row->used[TEXT_AREA])
27601 expose_area (w, row, r, TEXT_AREA);
27602 if (row->used[RIGHT_MARGIN_AREA])
27603 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27604 draw_row_fringe_bitmaps (w, row);
27605 }
27606
27607 return row->mouse_face_p;
27608 }
27609
27610
27611 /* Redraw those parts of glyphs rows during expose event handling that
27612 overlap other rows. Redrawing of an exposed line writes over parts
27613 of lines overlapping that exposed line; this function fixes that.
27614
27615 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27616 row in W's current matrix that is exposed and overlaps other rows.
27617 LAST_OVERLAPPING_ROW is the last such row. */
27618
27619 static void
27620 expose_overlaps (struct window *w,
27621 struct glyph_row *first_overlapping_row,
27622 struct glyph_row *last_overlapping_row,
27623 XRectangle *r)
27624 {
27625 struct glyph_row *row;
27626
27627 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27628 if (row->overlapping_p)
27629 {
27630 xassert (row->enabled_p && !row->mode_line_p);
27631
27632 row->clip = r;
27633 if (row->used[LEFT_MARGIN_AREA])
27634 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27635
27636 if (row->used[TEXT_AREA])
27637 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27638
27639 if (row->used[RIGHT_MARGIN_AREA])
27640 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27641 row->clip = NULL;
27642 }
27643 }
27644
27645
27646 /* Return non-zero if W's cursor intersects rectangle R. */
27647
27648 static int
27649 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27650 {
27651 XRectangle cr, result;
27652 struct glyph *cursor_glyph;
27653 struct glyph_row *row;
27654
27655 if (w->phys_cursor.vpos >= 0
27656 && w->phys_cursor.vpos < w->current_matrix->nrows
27657 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27658 row->enabled_p)
27659 && row->cursor_in_fringe_p)
27660 {
27661 /* Cursor is in the fringe. */
27662 cr.x = window_box_right_offset (w,
27663 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27664 ? RIGHT_MARGIN_AREA
27665 : TEXT_AREA));
27666 cr.y = row->y;
27667 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27668 cr.height = row->height;
27669 return x_intersect_rectangles (&cr, r, &result);
27670 }
27671
27672 cursor_glyph = get_phys_cursor_glyph (w);
27673 if (cursor_glyph)
27674 {
27675 /* r is relative to W's box, but w->phys_cursor.x is relative
27676 to left edge of W's TEXT area. Adjust it. */
27677 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27678 cr.y = w->phys_cursor.y;
27679 cr.width = cursor_glyph->pixel_width;
27680 cr.height = w->phys_cursor_height;
27681 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27682 I assume the effect is the same -- and this is portable. */
27683 return x_intersect_rectangles (&cr, r, &result);
27684 }
27685 /* If we don't understand the format, pretend we're not in the hot-spot. */
27686 return 0;
27687 }
27688
27689
27690 /* EXPORT:
27691 Draw a vertical window border to the right of window W if W doesn't
27692 have vertical scroll bars. */
27693
27694 void
27695 x_draw_vertical_border (struct window *w)
27696 {
27697 struct frame *f = XFRAME (WINDOW_FRAME (w));
27698
27699 /* We could do better, if we knew what type of scroll-bar the adjacent
27700 windows (on either side) have... But we don't :-(
27701 However, I think this works ok. ++KFS 2003-04-25 */
27702
27703 /* Redraw borders between horizontally adjacent windows. Don't
27704 do it for frames with vertical scroll bars because either the
27705 right scroll bar of a window, or the left scroll bar of its
27706 neighbor will suffice as a border. */
27707 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27708 return;
27709
27710 if (!WINDOW_RIGHTMOST_P (w)
27711 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27712 {
27713 int x0, x1, y0, y1;
27714
27715 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27716 y1 -= 1;
27717
27718 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27719 x1 -= 1;
27720
27721 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27722 }
27723 else if (!WINDOW_LEFTMOST_P (w)
27724 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27725 {
27726 int x0, x1, y0, y1;
27727
27728 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27729 y1 -= 1;
27730
27731 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27732 x0 -= 1;
27733
27734 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27735 }
27736 }
27737
27738
27739 /* Redraw the part of window W intersection rectangle FR. Pixel
27740 coordinates in FR are frame-relative. Call this function with
27741 input blocked. Value is non-zero if the exposure overwrites
27742 mouse-face. */
27743
27744 static int
27745 expose_window (struct window *w, XRectangle *fr)
27746 {
27747 struct frame *f = XFRAME (w->frame);
27748 XRectangle wr, r;
27749 int mouse_face_overwritten_p = 0;
27750
27751 /* If window is not yet fully initialized, do nothing. This can
27752 happen when toolkit scroll bars are used and a window is split.
27753 Reconfiguring the scroll bar will generate an expose for a newly
27754 created window. */
27755 if (w->current_matrix == NULL)
27756 return 0;
27757
27758 /* When we're currently updating the window, display and current
27759 matrix usually don't agree. Arrange for a thorough display
27760 later. */
27761 if (w == updated_window)
27762 {
27763 SET_FRAME_GARBAGED (f);
27764 return 0;
27765 }
27766
27767 /* Frame-relative pixel rectangle of W. */
27768 wr.x = WINDOW_LEFT_EDGE_X (w);
27769 wr.y = WINDOW_TOP_EDGE_Y (w);
27770 wr.width = WINDOW_TOTAL_WIDTH (w);
27771 wr.height = WINDOW_TOTAL_HEIGHT (w);
27772
27773 if (x_intersect_rectangles (fr, &wr, &r))
27774 {
27775 int yb = window_text_bottom_y (w);
27776 struct glyph_row *row;
27777 int cursor_cleared_p, phys_cursor_on_p;
27778 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27779
27780 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27781 r.x, r.y, r.width, r.height));
27782
27783 /* Convert to window coordinates. */
27784 r.x -= WINDOW_LEFT_EDGE_X (w);
27785 r.y -= WINDOW_TOP_EDGE_Y (w);
27786
27787 /* Turn off the cursor. */
27788 if (!w->pseudo_window_p
27789 && phys_cursor_in_rect_p (w, &r))
27790 {
27791 x_clear_cursor (w);
27792 cursor_cleared_p = 1;
27793 }
27794 else
27795 cursor_cleared_p = 0;
27796
27797 /* If the row containing the cursor extends face to end of line,
27798 then expose_area might overwrite the cursor outside the
27799 rectangle and thus notice_overwritten_cursor might clear
27800 w->phys_cursor_on_p. We remember the original value and
27801 check later if it is changed. */
27802 phys_cursor_on_p = w->phys_cursor_on_p;
27803
27804 /* Update lines intersecting rectangle R. */
27805 first_overlapping_row = last_overlapping_row = NULL;
27806 for (row = w->current_matrix->rows;
27807 row->enabled_p;
27808 ++row)
27809 {
27810 int y0 = row->y;
27811 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27812
27813 if ((y0 >= r.y && y0 < r.y + r.height)
27814 || (y1 > r.y && y1 < r.y + r.height)
27815 || (r.y >= y0 && r.y < y1)
27816 || (r.y + r.height > y0 && r.y + r.height < y1))
27817 {
27818 /* A header line may be overlapping, but there is no need
27819 to fix overlapping areas for them. KFS 2005-02-12 */
27820 if (row->overlapping_p && !row->mode_line_p)
27821 {
27822 if (first_overlapping_row == NULL)
27823 first_overlapping_row = row;
27824 last_overlapping_row = row;
27825 }
27826
27827 row->clip = fr;
27828 if (expose_line (w, row, &r))
27829 mouse_face_overwritten_p = 1;
27830 row->clip = NULL;
27831 }
27832 else if (row->overlapping_p)
27833 {
27834 /* We must redraw a row overlapping the exposed area. */
27835 if (y0 < r.y
27836 ? y0 + row->phys_height > r.y
27837 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27838 {
27839 if (first_overlapping_row == NULL)
27840 first_overlapping_row = row;
27841 last_overlapping_row = row;
27842 }
27843 }
27844
27845 if (y1 >= yb)
27846 break;
27847 }
27848
27849 /* Display the mode line if there is one. */
27850 if (WINDOW_WANTS_MODELINE_P (w)
27851 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27852 row->enabled_p)
27853 && row->y < r.y + r.height)
27854 {
27855 if (expose_line (w, row, &r))
27856 mouse_face_overwritten_p = 1;
27857 }
27858
27859 if (!w->pseudo_window_p)
27860 {
27861 /* Fix the display of overlapping rows. */
27862 if (first_overlapping_row)
27863 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27864 fr);
27865
27866 /* Draw border between windows. */
27867 x_draw_vertical_border (w);
27868
27869 /* Turn the cursor on again. */
27870 if (cursor_cleared_p
27871 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27872 update_window_cursor (w, 1);
27873 }
27874 }
27875
27876 return mouse_face_overwritten_p;
27877 }
27878
27879
27880
27881 /* Redraw (parts) of all windows in the window tree rooted at W that
27882 intersect R. R contains frame pixel coordinates. Value is
27883 non-zero if the exposure overwrites mouse-face. */
27884
27885 static int
27886 expose_window_tree (struct window *w, XRectangle *r)
27887 {
27888 struct frame *f = XFRAME (w->frame);
27889 int mouse_face_overwritten_p = 0;
27890
27891 while (w && !FRAME_GARBAGED_P (f))
27892 {
27893 if (!NILP (w->hchild))
27894 mouse_face_overwritten_p
27895 |= expose_window_tree (XWINDOW (w->hchild), r);
27896 else if (!NILP (w->vchild))
27897 mouse_face_overwritten_p
27898 |= expose_window_tree (XWINDOW (w->vchild), r);
27899 else
27900 mouse_face_overwritten_p |= expose_window (w, r);
27901
27902 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27903 }
27904
27905 return mouse_face_overwritten_p;
27906 }
27907
27908
27909 /* EXPORT:
27910 Redisplay an exposed area of frame F. X and Y are the upper-left
27911 corner of the exposed rectangle. W and H are width and height of
27912 the exposed area. All are pixel values. W or H zero means redraw
27913 the entire frame. */
27914
27915 void
27916 expose_frame (struct frame *f, int x, int y, int w, int h)
27917 {
27918 XRectangle r;
27919 int mouse_face_overwritten_p = 0;
27920
27921 TRACE ((stderr, "expose_frame "));
27922
27923 /* No need to redraw if frame will be redrawn soon. */
27924 if (FRAME_GARBAGED_P (f))
27925 {
27926 TRACE ((stderr, " garbaged\n"));
27927 return;
27928 }
27929
27930 /* If basic faces haven't been realized yet, there is no point in
27931 trying to redraw anything. This can happen when we get an expose
27932 event while Emacs is starting, e.g. by moving another window. */
27933 if (FRAME_FACE_CACHE (f) == NULL
27934 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27935 {
27936 TRACE ((stderr, " no faces\n"));
27937 return;
27938 }
27939
27940 if (w == 0 || h == 0)
27941 {
27942 r.x = r.y = 0;
27943 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27944 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27945 }
27946 else
27947 {
27948 r.x = x;
27949 r.y = y;
27950 r.width = w;
27951 r.height = h;
27952 }
27953
27954 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27955 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27956
27957 if (WINDOWP (f->tool_bar_window))
27958 mouse_face_overwritten_p
27959 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27960
27961 #ifdef HAVE_X_WINDOWS
27962 #ifndef MSDOS
27963 #ifndef USE_X_TOOLKIT
27964 if (WINDOWP (f->menu_bar_window))
27965 mouse_face_overwritten_p
27966 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27967 #endif /* not USE_X_TOOLKIT */
27968 #endif
27969 #endif
27970
27971 /* Some window managers support a focus-follows-mouse style with
27972 delayed raising of frames. Imagine a partially obscured frame,
27973 and moving the mouse into partially obscured mouse-face on that
27974 frame. The visible part of the mouse-face will be highlighted,
27975 then the WM raises the obscured frame. With at least one WM, KDE
27976 2.1, Emacs is not getting any event for the raising of the frame
27977 (even tried with SubstructureRedirectMask), only Expose events.
27978 These expose events will draw text normally, i.e. not
27979 highlighted. Which means we must redo the highlight here.
27980 Subsume it under ``we love X''. --gerd 2001-08-15 */
27981 /* Included in Windows version because Windows most likely does not
27982 do the right thing if any third party tool offers
27983 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27984 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27985 {
27986 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27987 if (f == hlinfo->mouse_face_mouse_frame)
27988 {
27989 int mouse_x = hlinfo->mouse_face_mouse_x;
27990 int mouse_y = hlinfo->mouse_face_mouse_y;
27991 clear_mouse_face (hlinfo);
27992 note_mouse_highlight (f, mouse_x, mouse_y);
27993 }
27994 }
27995 }
27996
27997
27998 /* EXPORT:
27999 Determine the intersection of two rectangles R1 and R2. Return
28000 the intersection in *RESULT. Value is non-zero if RESULT is not
28001 empty. */
28002
28003 int
28004 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28005 {
28006 XRectangle *left, *right;
28007 XRectangle *upper, *lower;
28008 int intersection_p = 0;
28009
28010 /* Rearrange so that R1 is the left-most rectangle. */
28011 if (r1->x < r2->x)
28012 left = r1, right = r2;
28013 else
28014 left = r2, right = r1;
28015
28016 /* X0 of the intersection is right.x0, if this is inside R1,
28017 otherwise there is no intersection. */
28018 if (right->x <= left->x + left->width)
28019 {
28020 result->x = right->x;
28021
28022 /* The right end of the intersection is the minimum of
28023 the right ends of left and right. */
28024 result->width = (min (left->x + left->width, right->x + right->width)
28025 - result->x);
28026
28027 /* Same game for Y. */
28028 if (r1->y < r2->y)
28029 upper = r1, lower = r2;
28030 else
28031 upper = r2, lower = r1;
28032
28033 /* The upper end of the intersection is lower.y0, if this is inside
28034 of upper. Otherwise, there is no intersection. */
28035 if (lower->y <= upper->y + upper->height)
28036 {
28037 result->y = lower->y;
28038
28039 /* The lower end of the intersection is the minimum of the lower
28040 ends of upper and lower. */
28041 result->height = (min (lower->y + lower->height,
28042 upper->y + upper->height)
28043 - result->y);
28044 intersection_p = 1;
28045 }
28046 }
28047
28048 return intersection_p;
28049 }
28050
28051 #endif /* HAVE_WINDOW_SYSTEM */
28052
28053 \f
28054 /***********************************************************************
28055 Initialization
28056 ***********************************************************************/
28057
28058 void
28059 syms_of_xdisp (void)
28060 {
28061 Vwith_echo_area_save_vector = Qnil;
28062 staticpro (&Vwith_echo_area_save_vector);
28063
28064 Vmessage_stack = Qnil;
28065 staticpro (&Vmessage_stack);
28066
28067 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28068
28069 message_dolog_marker1 = Fmake_marker ();
28070 staticpro (&message_dolog_marker1);
28071 message_dolog_marker2 = Fmake_marker ();
28072 staticpro (&message_dolog_marker2);
28073 message_dolog_marker3 = Fmake_marker ();
28074 staticpro (&message_dolog_marker3);
28075
28076 #if GLYPH_DEBUG
28077 defsubr (&Sdump_frame_glyph_matrix);
28078 defsubr (&Sdump_glyph_matrix);
28079 defsubr (&Sdump_glyph_row);
28080 defsubr (&Sdump_tool_bar_row);
28081 defsubr (&Strace_redisplay);
28082 defsubr (&Strace_to_stderr);
28083 #endif
28084 #ifdef HAVE_WINDOW_SYSTEM
28085 defsubr (&Stool_bar_lines_needed);
28086 defsubr (&Slookup_image_map);
28087 #endif
28088 defsubr (&Sformat_mode_line);
28089 defsubr (&Sinvisible_p);
28090 defsubr (&Scurrent_bidi_paragraph_direction);
28091
28092 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28093 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28094 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28095 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28096 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28097 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28098 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28099 DEFSYM (Qeval, "eval");
28100 DEFSYM (QCdata, ":data");
28101 DEFSYM (Qdisplay, "display");
28102 DEFSYM (Qspace_width, "space-width");
28103 DEFSYM (Qraise, "raise");
28104 DEFSYM (Qslice, "slice");
28105 DEFSYM (Qspace, "space");
28106 DEFSYM (Qmargin, "margin");
28107 DEFSYM (Qpointer, "pointer");
28108 DEFSYM (Qleft_margin, "left-margin");
28109 DEFSYM (Qright_margin, "right-margin");
28110 DEFSYM (Qcenter, "center");
28111 DEFSYM (Qline_height, "line-height");
28112 DEFSYM (QCalign_to, ":align-to");
28113 DEFSYM (QCrelative_width, ":relative-width");
28114 DEFSYM (QCrelative_height, ":relative-height");
28115 DEFSYM (QCeval, ":eval");
28116 DEFSYM (QCpropertize, ":propertize");
28117 DEFSYM (QCfile, ":file");
28118 DEFSYM (Qfontified, "fontified");
28119 DEFSYM (Qfontification_functions, "fontification-functions");
28120 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28121 DEFSYM (Qescape_glyph, "escape-glyph");
28122 DEFSYM (Qnobreak_space, "nobreak-space");
28123 DEFSYM (Qimage, "image");
28124 DEFSYM (Qtext, "text");
28125 DEFSYM (Qboth, "both");
28126 DEFSYM (Qboth_horiz, "both-horiz");
28127 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28128 DEFSYM (QCmap, ":map");
28129 DEFSYM (QCpointer, ":pointer");
28130 DEFSYM (Qrect, "rect");
28131 DEFSYM (Qcircle, "circle");
28132 DEFSYM (Qpoly, "poly");
28133 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28134 DEFSYM (Qgrow_only, "grow-only");
28135 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28136 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28137 DEFSYM (Qposition, "position");
28138 DEFSYM (Qbuffer_position, "buffer-position");
28139 DEFSYM (Qobject, "object");
28140 DEFSYM (Qbar, "bar");
28141 DEFSYM (Qhbar, "hbar");
28142 DEFSYM (Qbox, "box");
28143 DEFSYM (Qhollow, "hollow");
28144 DEFSYM (Qhand, "hand");
28145 DEFSYM (Qarrow, "arrow");
28146 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28147
28148 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28149 Fcons (intern_c_string ("void-variable"), Qnil)),
28150 Qnil);
28151 staticpro (&list_of_error);
28152
28153 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28154 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28155 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28156 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28157
28158 echo_buffer[0] = echo_buffer[1] = Qnil;
28159 staticpro (&echo_buffer[0]);
28160 staticpro (&echo_buffer[1]);
28161
28162 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28163 staticpro (&echo_area_buffer[0]);
28164 staticpro (&echo_area_buffer[1]);
28165
28166 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28167 staticpro (&Vmessages_buffer_name);
28168
28169 mode_line_proptrans_alist = Qnil;
28170 staticpro (&mode_line_proptrans_alist);
28171 mode_line_string_list = Qnil;
28172 staticpro (&mode_line_string_list);
28173 mode_line_string_face = Qnil;
28174 staticpro (&mode_line_string_face);
28175 mode_line_string_face_prop = Qnil;
28176 staticpro (&mode_line_string_face_prop);
28177 Vmode_line_unwind_vector = Qnil;
28178 staticpro (&Vmode_line_unwind_vector);
28179
28180 help_echo_string = Qnil;
28181 staticpro (&help_echo_string);
28182 help_echo_object = Qnil;
28183 staticpro (&help_echo_object);
28184 help_echo_window = Qnil;
28185 staticpro (&help_echo_window);
28186 previous_help_echo_string = Qnil;
28187 staticpro (&previous_help_echo_string);
28188 help_echo_pos = -1;
28189
28190 DEFSYM (Qright_to_left, "right-to-left");
28191 DEFSYM (Qleft_to_right, "left-to-right");
28192
28193 #ifdef HAVE_WINDOW_SYSTEM
28194 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28195 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28196 For example, if a block cursor is over a tab, it will be drawn as
28197 wide as that tab on the display. */);
28198 x_stretch_cursor_p = 0;
28199 #endif
28200
28201 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28202 doc: /* *Non-nil means highlight trailing whitespace.
28203 The face used for trailing whitespace is `trailing-whitespace'. */);
28204 Vshow_trailing_whitespace = Qnil;
28205
28206 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28207 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28208 If the value is t, Emacs highlights non-ASCII chars which have the
28209 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28210 or `escape-glyph' face respectively.
28211
28212 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28213 U+2011 (non-breaking hyphen) are affected.
28214
28215 Any other non-nil value means to display these characters as a escape
28216 glyph followed by an ordinary space or hyphen.
28217
28218 A value of nil means no special handling of these characters. */);
28219 Vnobreak_char_display = Qt;
28220
28221 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28222 doc: /* *The pointer shape to show in void text areas.
28223 A value of nil means to show the text pointer. Other options are `arrow',
28224 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28225 Vvoid_text_area_pointer = Qarrow;
28226
28227 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28228 doc: /* Non-nil means don't actually do any redisplay.
28229 This is used for internal purposes. */);
28230 Vinhibit_redisplay = Qnil;
28231
28232 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28233 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28234 Vglobal_mode_string = Qnil;
28235
28236 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28237 doc: /* Marker for where to display an arrow on top of the buffer text.
28238 This must be the beginning of a line in order to work.
28239 See also `overlay-arrow-string'. */);
28240 Voverlay_arrow_position = Qnil;
28241
28242 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28243 doc: /* String to display as an arrow in non-window frames.
28244 See also `overlay-arrow-position'. */);
28245 Voverlay_arrow_string = make_pure_c_string ("=>");
28246
28247 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28248 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28249 The symbols on this list are examined during redisplay to determine
28250 where to display overlay arrows. */);
28251 Voverlay_arrow_variable_list
28252 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28253
28254 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28255 doc: /* *The number of lines to try scrolling a window by when point moves out.
28256 If that fails to bring point back on frame, point is centered instead.
28257 If this is zero, point is always centered after it moves off frame.
28258 If you want scrolling to always be a line at a time, you should set
28259 `scroll-conservatively' to a large value rather than set this to 1. */);
28260
28261 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28262 doc: /* *Scroll up to this many lines, to bring point back on screen.
28263 If point moves off-screen, redisplay will scroll by up to
28264 `scroll-conservatively' lines in order to bring point just barely
28265 onto the screen again. If that cannot be done, then redisplay
28266 recenters point as usual.
28267
28268 If the value is greater than 100, redisplay will never recenter point,
28269 but will always scroll just enough text to bring point into view, even
28270 if you move far away.
28271
28272 A value of zero means always recenter point if it moves off screen. */);
28273 scroll_conservatively = 0;
28274
28275 DEFVAR_INT ("scroll-margin", scroll_margin,
28276 doc: /* *Number of lines of margin at the top and bottom of a window.
28277 Recenter the window whenever point gets within this many lines
28278 of the top or bottom of the window. */);
28279 scroll_margin = 0;
28280
28281 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28282 doc: /* Pixels per inch value for non-window system displays.
28283 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28284 Vdisplay_pixels_per_inch = make_float (72.0);
28285
28286 #if GLYPH_DEBUG
28287 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28288 #endif
28289
28290 DEFVAR_LISP ("truncate-partial-width-windows",
28291 Vtruncate_partial_width_windows,
28292 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28293 For an integer value, truncate lines in each window narrower than the
28294 full frame width, provided the window width is less than that integer;
28295 otherwise, respect the value of `truncate-lines'.
28296
28297 For any other non-nil value, truncate lines in all windows that do
28298 not span the full frame width.
28299
28300 A value of nil means to respect the value of `truncate-lines'.
28301
28302 If `word-wrap' is enabled, you might want to reduce this. */);
28303 Vtruncate_partial_width_windows = make_number (50);
28304
28305 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28306 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28307 Any other value means to use the appropriate face, `mode-line',
28308 `header-line', or `menu' respectively. */);
28309 mode_line_inverse_video = 1;
28310
28311 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28312 doc: /* *Maximum buffer size for which line number should be displayed.
28313 If the buffer is bigger than this, the line number does not appear
28314 in the mode line. A value of nil means no limit. */);
28315 Vline_number_display_limit = Qnil;
28316
28317 DEFVAR_INT ("line-number-display-limit-width",
28318 line_number_display_limit_width,
28319 doc: /* *Maximum line width (in characters) for line number display.
28320 If the average length of the lines near point is bigger than this, then the
28321 line number may be omitted from the mode line. */);
28322 line_number_display_limit_width = 200;
28323
28324 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28325 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28326 highlight_nonselected_windows = 0;
28327
28328 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28329 doc: /* Non-nil if more than one frame is visible on this display.
28330 Minibuffer-only frames don't count, but iconified frames do.
28331 This variable is not guaranteed to be accurate except while processing
28332 `frame-title-format' and `icon-title-format'. */);
28333
28334 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28335 doc: /* Template for displaying the title bar of visible frames.
28336 \(Assuming the window manager supports this feature.)
28337
28338 This variable has the same structure as `mode-line-format', except that
28339 the %c and %l constructs are ignored. It is used only on frames for
28340 which no explicit name has been set \(see `modify-frame-parameters'). */);
28341
28342 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28343 doc: /* Template for displaying the title bar of an iconified frame.
28344 \(Assuming the window manager supports this feature.)
28345 This variable has the same structure as `mode-line-format' (which see),
28346 and is used only on frames for which no explicit name has been set
28347 \(see `modify-frame-parameters'). */);
28348 Vicon_title_format
28349 = Vframe_title_format
28350 = pure_cons (intern_c_string ("multiple-frames"),
28351 pure_cons (make_pure_c_string ("%b"),
28352 pure_cons (pure_cons (empty_unibyte_string,
28353 pure_cons (intern_c_string ("invocation-name"),
28354 pure_cons (make_pure_c_string ("@"),
28355 pure_cons (intern_c_string ("system-name"),
28356 Qnil)))),
28357 Qnil)));
28358
28359 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28360 doc: /* Maximum number of lines to keep in the message log buffer.
28361 If nil, disable message logging. If t, log messages but don't truncate
28362 the buffer when it becomes large. */);
28363 Vmessage_log_max = make_number (100);
28364
28365 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28366 doc: /* Functions called before redisplay, if window sizes have changed.
28367 The value should be a list of functions that take one argument.
28368 Just before redisplay, for each frame, if any of its windows have changed
28369 size since the last redisplay, or have been split or deleted,
28370 all the functions in the list are called, with the frame as argument. */);
28371 Vwindow_size_change_functions = Qnil;
28372
28373 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28374 doc: /* List of functions to call before redisplaying a window with scrolling.
28375 Each function is called with two arguments, the window and its new
28376 display-start position. Note that these functions are also called by
28377 `set-window-buffer'. Also note that the value of `window-end' is not
28378 valid when these functions are called.
28379
28380 Warning: Do not use this feature to alter the way the window
28381 is scrolled. It is not designed for that, and such use probably won't
28382 work. */);
28383 Vwindow_scroll_functions = Qnil;
28384
28385 DEFVAR_LISP ("window-text-change-functions",
28386 Vwindow_text_change_functions,
28387 doc: /* Functions to call in redisplay when text in the window might change. */);
28388 Vwindow_text_change_functions = Qnil;
28389
28390 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28391 doc: /* Functions called when redisplay of a window reaches the end trigger.
28392 Each function is called with two arguments, the window and the end trigger value.
28393 See `set-window-redisplay-end-trigger'. */);
28394 Vredisplay_end_trigger_functions = Qnil;
28395
28396 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28397 doc: /* *Non-nil means autoselect window with mouse pointer.
28398 If nil, do not autoselect windows.
28399 A positive number means delay autoselection by that many seconds: a
28400 window is autoselected only after the mouse has remained in that
28401 window for the duration of the delay.
28402 A negative number has a similar effect, but causes windows to be
28403 autoselected only after the mouse has stopped moving. \(Because of
28404 the way Emacs compares mouse events, you will occasionally wait twice
28405 that time before the window gets selected.\)
28406 Any other value means to autoselect window instantaneously when the
28407 mouse pointer enters it.
28408
28409 Autoselection selects the minibuffer only if it is active, and never
28410 unselects the minibuffer if it is active.
28411
28412 When customizing this variable make sure that the actual value of
28413 `focus-follows-mouse' matches the behavior of your window manager. */);
28414 Vmouse_autoselect_window = Qnil;
28415
28416 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28417 doc: /* *Non-nil means automatically resize tool-bars.
28418 This dynamically changes the tool-bar's height to the minimum height
28419 that is needed to make all tool-bar items visible.
28420 If value is `grow-only', the tool-bar's height is only increased
28421 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28422 Vauto_resize_tool_bars = Qt;
28423
28424 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28425 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28426 auto_raise_tool_bar_buttons_p = 1;
28427
28428 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28429 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28430 make_cursor_line_fully_visible_p = 1;
28431
28432 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28433 doc: /* *Border below tool-bar in pixels.
28434 If an integer, use it as the height of the border.
28435 If it is one of `internal-border-width' or `border-width', use the
28436 value of the corresponding frame parameter.
28437 Otherwise, no border is added below the tool-bar. */);
28438 Vtool_bar_border = Qinternal_border_width;
28439
28440 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28441 doc: /* *Margin around tool-bar buttons in pixels.
28442 If an integer, use that for both horizontal and vertical margins.
28443 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28444 HORZ specifying the horizontal margin, and VERT specifying the
28445 vertical margin. */);
28446 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28447
28448 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28449 doc: /* *Relief thickness of tool-bar buttons. */);
28450 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28451
28452 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28453 doc: /* Tool bar style to use.
28454 It can be one of
28455 image - show images only
28456 text - show text only
28457 both - show both, text below image
28458 both-horiz - show text to the right of the image
28459 text-image-horiz - show text to the left of the image
28460 any other - use system default or image if no system default. */);
28461 Vtool_bar_style = Qnil;
28462
28463 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28464 doc: /* *Maximum number of characters a label can have to be shown.
28465 The tool bar style must also show labels for this to have any effect, see
28466 `tool-bar-style'. */);
28467 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28468
28469 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28470 doc: /* List of functions to call to fontify regions of text.
28471 Each function is called with one argument POS. Functions must
28472 fontify a region starting at POS in the current buffer, and give
28473 fontified regions the property `fontified'. */);
28474 Vfontification_functions = Qnil;
28475 Fmake_variable_buffer_local (Qfontification_functions);
28476
28477 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28478 unibyte_display_via_language_environment,
28479 doc: /* *Non-nil means display unibyte text according to language environment.
28480 Specifically, this means that raw bytes in the range 160-255 decimal
28481 are displayed by converting them to the equivalent multibyte characters
28482 according to the current language environment. As a result, they are
28483 displayed according to the current fontset.
28484
28485 Note that this variable affects only how these bytes are displayed,
28486 but does not change the fact they are interpreted as raw bytes. */);
28487 unibyte_display_via_language_environment = 0;
28488
28489 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28490 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28491 If a float, it specifies a fraction of the mini-window frame's height.
28492 If an integer, it specifies a number of lines. */);
28493 Vmax_mini_window_height = make_float (0.25);
28494
28495 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28496 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28497 A value of nil means don't automatically resize mini-windows.
28498 A value of t means resize them to fit the text displayed in them.
28499 A value of `grow-only', the default, means let mini-windows grow only;
28500 they return to their normal size when the minibuffer is closed, or the
28501 echo area becomes empty. */);
28502 Vresize_mini_windows = Qgrow_only;
28503
28504 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28505 doc: /* Alist specifying how to blink the cursor off.
28506 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28507 `cursor-type' frame-parameter or variable equals ON-STATE,
28508 comparing using `equal', Emacs uses OFF-STATE to specify
28509 how to blink it off. ON-STATE and OFF-STATE are values for
28510 the `cursor-type' frame parameter.
28511
28512 If a frame's ON-STATE has no entry in this list,
28513 the frame's other specifications determine how to blink the cursor off. */);
28514 Vblink_cursor_alist = Qnil;
28515
28516 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28517 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28518 If non-nil, windows are automatically scrolled horizontally to make
28519 point visible. */);
28520 automatic_hscrolling_p = 1;
28521 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28522
28523 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28524 doc: /* *How many columns away from the window edge point is allowed to get
28525 before automatic hscrolling will horizontally scroll the window. */);
28526 hscroll_margin = 5;
28527
28528 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28529 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28530 When point is less than `hscroll-margin' columns from the window
28531 edge, automatic hscrolling will scroll the window by the amount of columns
28532 determined by this variable. If its value is a positive integer, scroll that
28533 many columns. If it's a positive floating-point number, it specifies the
28534 fraction of the window's width to scroll. If it's nil or zero, point will be
28535 centered horizontally after the scroll. Any other value, including negative
28536 numbers, are treated as if the value were zero.
28537
28538 Automatic hscrolling always moves point outside the scroll margin, so if
28539 point was more than scroll step columns inside the margin, the window will
28540 scroll more than the value given by the scroll step.
28541
28542 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28543 and `scroll-right' overrides this variable's effect. */);
28544 Vhscroll_step = make_number (0);
28545
28546 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28547 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28548 Bind this around calls to `message' to let it take effect. */);
28549 message_truncate_lines = 0;
28550
28551 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28552 doc: /* Normal hook run to update the menu bar definitions.
28553 Redisplay runs this hook before it redisplays the menu bar.
28554 This is used to update submenus such as Buffers,
28555 whose contents depend on various data. */);
28556 Vmenu_bar_update_hook = Qnil;
28557
28558 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28559 doc: /* Frame for which we are updating a menu.
28560 The enable predicate for a menu binding should check this variable. */);
28561 Vmenu_updating_frame = Qnil;
28562
28563 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28564 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28565 inhibit_menubar_update = 0;
28566
28567 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28568 doc: /* Prefix prepended to all continuation lines at display time.
28569 The value may be a string, an image, or a stretch-glyph; it is
28570 interpreted in the same way as the value of a `display' text property.
28571
28572 This variable is overridden by any `wrap-prefix' text or overlay
28573 property.
28574
28575 To add a prefix to non-continuation lines, use `line-prefix'. */);
28576 Vwrap_prefix = Qnil;
28577 DEFSYM (Qwrap_prefix, "wrap-prefix");
28578 Fmake_variable_buffer_local (Qwrap_prefix);
28579
28580 DEFVAR_LISP ("line-prefix", Vline_prefix,
28581 doc: /* Prefix prepended to all non-continuation lines at display time.
28582 The value may be a string, an image, or a stretch-glyph; it is
28583 interpreted in the same way as the value of a `display' text property.
28584
28585 This variable is overridden by any `line-prefix' text or overlay
28586 property.
28587
28588 To add a prefix to continuation lines, use `wrap-prefix'. */);
28589 Vline_prefix = Qnil;
28590 DEFSYM (Qline_prefix, "line-prefix");
28591 Fmake_variable_buffer_local (Qline_prefix);
28592
28593 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28594 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28595 inhibit_eval_during_redisplay = 0;
28596
28597 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28598 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28599 inhibit_free_realized_faces = 0;
28600
28601 #if GLYPH_DEBUG
28602 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28603 doc: /* Inhibit try_window_id display optimization. */);
28604 inhibit_try_window_id = 0;
28605
28606 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28607 doc: /* Inhibit try_window_reusing display optimization. */);
28608 inhibit_try_window_reusing = 0;
28609
28610 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28611 doc: /* Inhibit try_cursor_movement display optimization. */);
28612 inhibit_try_cursor_movement = 0;
28613 #endif /* GLYPH_DEBUG */
28614
28615 DEFVAR_INT ("overline-margin", overline_margin,
28616 doc: /* *Space between overline and text, in pixels.
28617 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28618 margin to the character height. */);
28619 overline_margin = 2;
28620
28621 DEFVAR_INT ("underline-minimum-offset",
28622 underline_minimum_offset,
28623 doc: /* Minimum distance between baseline and underline.
28624 This can improve legibility of underlined text at small font sizes,
28625 particularly when using variable `x-use-underline-position-properties'
28626 with fonts that specify an UNDERLINE_POSITION relatively close to the
28627 baseline. The default value is 1. */);
28628 underline_minimum_offset = 1;
28629
28630 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28631 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28632 This feature only works when on a window system that can change
28633 cursor shapes. */);
28634 display_hourglass_p = 1;
28635
28636 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28637 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28638 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28639
28640 hourglass_atimer = NULL;
28641 hourglass_shown_p = 0;
28642
28643 DEFSYM (Qglyphless_char, "glyphless-char");
28644 DEFSYM (Qhex_code, "hex-code");
28645 DEFSYM (Qempty_box, "empty-box");
28646 DEFSYM (Qthin_space, "thin-space");
28647 DEFSYM (Qzero_width, "zero-width");
28648
28649 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28650 /* Intern this now in case it isn't already done.
28651 Setting this variable twice is harmless.
28652 But don't staticpro it here--that is done in alloc.c. */
28653 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28654 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28655
28656 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28657 doc: /* Char-table defining glyphless characters.
28658 Each element, if non-nil, should be one of the following:
28659 an ASCII acronym string: display this string in a box
28660 `hex-code': display the hexadecimal code of a character in a box
28661 `empty-box': display as an empty box
28662 `thin-space': display as 1-pixel width space
28663 `zero-width': don't display
28664 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28665 display method for graphical terminals and text terminals respectively.
28666 GRAPHICAL and TEXT should each have one of the values listed above.
28667
28668 The char-table has one extra slot to control the display of a character for
28669 which no font is found. This slot only takes effect on graphical terminals.
28670 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28671 `thin-space'. The default is `empty-box'. */);
28672 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28673 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28674 Qempty_box);
28675 }
28676
28677
28678 /* Initialize this module when Emacs starts. */
28679
28680 void
28681 init_xdisp (void)
28682 {
28683 current_header_line_height = current_mode_line_height = -1;
28684
28685 CHARPOS (this_line_start_pos) = 0;
28686
28687 if (!noninteractive)
28688 {
28689 struct window *m = XWINDOW (minibuf_window);
28690 Lisp_Object frame = m->frame;
28691 struct frame *f = XFRAME (frame);
28692 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28693 struct window *r = XWINDOW (root);
28694 int i;
28695
28696 echo_area_window = minibuf_window;
28697
28698 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28699 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28700 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28701 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28702 XSETFASTINT (m->total_lines, 1);
28703 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28704
28705 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28706 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28707 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28708
28709 /* The default ellipsis glyphs `...'. */
28710 for (i = 0; i < 3; ++i)
28711 default_invis_vector[i] = make_number ('.');
28712 }
28713
28714 {
28715 /* Allocate the buffer for frame titles.
28716 Also used for `format-mode-line'. */
28717 int size = 100;
28718 mode_line_noprop_buf = (char *) xmalloc (size);
28719 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28720 mode_line_noprop_ptr = mode_line_noprop_buf;
28721 mode_line_target = MODE_LINE_DISPLAY;
28722 }
28723
28724 help_echo_showing_p = 0;
28725 }
28726
28727 /* Since w32 does not support atimers, it defines its own implementation of
28728 the following three functions in w32fns.c. */
28729 #ifndef WINDOWSNT
28730
28731 /* Platform-independent portion of hourglass implementation. */
28732
28733 /* Return non-zero if hourglass timer has been started or hourglass is
28734 shown. */
28735 int
28736 hourglass_started (void)
28737 {
28738 return hourglass_shown_p || hourglass_atimer != NULL;
28739 }
28740
28741 /* Cancel a currently active hourglass timer, and start a new one. */
28742 void
28743 start_hourglass (void)
28744 {
28745 #if defined (HAVE_WINDOW_SYSTEM)
28746 EMACS_TIME delay;
28747 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
28748
28749 cancel_hourglass ();
28750
28751 if (NUMBERP (Vhourglass_delay))
28752 {
28753 double duration = extract_float (Vhourglass_delay);
28754 if (0 < duration)
28755 duration_to_sec_usec (duration, &secs, &usecs);
28756 }
28757
28758 EMACS_SET_SECS_USECS (delay, secs, usecs);
28759 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28760 show_hourglass, NULL);
28761 #endif
28762 }
28763
28764
28765 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28766 shown. */
28767 void
28768 cancel_hourglass (void)
28769 {
28770 #if defined (HAVE_WINDOW_SYSTEM)
28771 if (hourglass_atimer)
28772 {
28773 cancel_atimer (hourglass_atimer);
28774 hourglass_atimer = NULL;
28775 }
28776
28777 if (hourglass_shown_p)
28778 hide_hourglass ();
28779 #endif
28780 }
28781 #endif /* ! WINDOWSNT */