]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from trunk.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 prfoduced 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 rectagle 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. */
2770 it->bidi_p =
2771 !NILP (BVAR (current_buffer, bidi_display_reordering))
2772 && it->multibyte_p;
2773
2774 /* If we are to reorder bidirectional text, init the bidi
2775 iterator. */
2776 if (it->bidi_p)
2777 {
2778 /* Note the paragraph direction that this buffer wants to
2779 use. */
2780 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2781 Qleft_to_right))
2782 it->paragraph_embedding = L2R;
2783 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2784 Qright_to_left))
2785 it->paragraph_embedding = R2L;
2786 else
2787 it->paragraph_embedding = NEUTRAL_DIR;
2788 bidi_unshelve_cache (NULL, 0);
2789 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2790 &it->bidi_it);
2791 }
2792
2793 /* Compute faces etc. */
2794 reseat (it, it->current.pos, 1);
2795 }
2796
2797 CHECK_IT (it);
2798 }
2799
2800
2801 /* Initialize IT for the display of window W with window start POS. */
2802
2803 void
2804 start_display (struct it *it, struct window *w, struct text_pos pos)
2805 {
2806 struct glyph_row *row;
2807 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2808
2809 row = w->desired_matrix->rows + first_vpos;
2810 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2811 it->first_vpos = first_vpos;
2812
2813 /* Don't reseat to previous visible line start if current start
2814 position is in a string or image. */
2815 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2816 {
2817 int start_at_line_beg_p;
2818 int first_y = it->current_y;
2819
2820 /* If window start is not at a line start, skip forward to POS to
2821 get the correct continuation lines width. */
2822 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2823 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2824 if (!start_at_line_beg_p)
2825 {
2826 int new_x;
2827
2828 reseat_at_previous_visible_line_start (it);
2829 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2830
2831 new_x = it->current_x + it->pixel_width;
2832
2833 /* If lines are continued, this line may end in the middle
2834 of a multi-glyph character (e.g. a control character
2835 displayed as \003, or in the middle of an overlay
2836 string). In this case move_it_to above will not have
2837 taken us to the start of the continuation line but to the
2838 end of the continued line. */
2839 if (it->current_x > 0
2840 && it->line_wrap != TRUNCATE /* Lines are continued. */
2841 && (/* And glyph doesn't fit on the line. */
2842 new_x > it->last_visible_x
2843 /* Or it fits exactly and we're on a window
2844 system frame. */
2845 || (new_x == it->last_visible_x
2846 && FRAME_WINDOW_P (it->f))))
2847 {
2848 if (it->current.dpvec_index >= 0
2849 || it->current.overlay_string_index >= 0)
2850 {
2851 set_iterator_to_next (it, 1);
2852 move_it_in_display_line_to (it, -1, -1, 0);
2853 }
2854
2855 it->continuation_lines_width += it->current_x;
2856 }
2857
2858 /* We're starting a new display line, not affected by the
2859 height of the continued line, so clear the appropriate
2860 fields in the iterator structure. */
2861 it->max_ascent = it->max_descent = 0;
2862 it->max_phys_ascent = it->max_phys_descent = 0;
2863
2864 it->current_y = first_y;
2865 it->vpos = 0;
2866 it->current_x = it->hpos = 0;
2867 }
2868 }
2869 }
2870
2871
2872 /* Return 1 if POS is a position in ellipses displayed for invisible
2873 text. W is the window we display, for text property lookup. */
2874
2875 static int
2876 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2877 {
2878 Lisp_Object prop, window;
2879 int ellipses_p = 0;
2880 ptrdiff_t charpos = CHARPOS (pos->pos);
2881
2882 /* If POS specifies a position in a display vector, this might
2883 be for an ellipsis displayed for invisible text. We won't
2884 get the iterator set up for delivering that ellipsis unless
2885 we make sure that it gets aware of the invisible text. */
2886 if (pos->dpvec_index >= 0
2887 && pos->overlay_string_index < 0
2888 && CHARPOS (pos->string_pos) < 0
2889 && charpos > BEGV
2890 && (XSETWINDOW (window, w),
2891 prop = Fget_char_property (make_number (charpos),
2892 Qinvisible, window),
2893 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2894 {
2895 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2896 window);
2897 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2898 }
2899
2900 return ellipses_p;
2901 }
2902
2903
2904 /* Initialize IT for stepping through current_buffer in window W,
2905 starting at position POS that includes overlay string and display
2906 vector/ control character translation position information. Value
2907 is zero if there are overlay strings with newlines at POS. */
2908
2909 static int
2910 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2911 {
2912 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2913 int i, overlay_strings_with_newlines = 0;
2914
2915 /* If POS specifies a position in a display vector, this might
2916 be for an ellipsis displayed for invisible text. We won't
2917 get the iterator set up for delivering that ellipsis unless
2918 we make sure that it gets aware of the invisible text. */
2919 if (in_ellipses_for_invisible_text_p (pos, w))
2920 {
2921 --charpos;
2922 bytepos = 0;
2923 }
2924
2925 /* Keep in mind: the call to reseat in init_iterator skips invisible
2926 text, so we might end up at a position different from POS. This
2927 is only a problem when POS is a row start after a newline and an
2928 overlay starts there with an after-string, and the overlay has an
2929 invisible property. Since we don't skip invisible text in
2930 display_line and elsewhere immediately after consuming the
2931 newline before the row start, such a POS will not be in a string,
2932 but the call to init_iterator below will move us to the
2933 after-string. */
2934 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2935
2936 /* This only scans the current chunk -- it should scan all chunks.
2937 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2938 to 16 in 22.1 to make this a lesser problem. */
2939 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2940 {
2941 const char *s = SSDATA (it->overlay_strings[i]);
2942 const char *e = s + SBYTES (it->overlay_strings[i]);
2943
2944 while (s < e && *s != '\n')
2945 ++s;
2946
2947 if (s < e)
2948 {
2949 overlay_strings_with_newlines = 1;
2950 break;
2951 }
2952 }
2953
2954 /* If position is within an overlay string, set up IT to the right
2955 overlay string. */
2956 if (pos->overlay_string_index >= 0)
2957 {
2958 int relative_index;
2959
2960 /* If the first overlay string happens to have a `display'
2961 property for an image, the iterator will be set up for that
2962 image, and we have to undo that setup first before we can
2963 correct the overlay string index. */
2964 if (it->method == GET_FROM_IMAGE)
2965 pop_it (it);
2966
2967 /* We already have the first chunk of overlay strings in
2968 IT->overlay_strings. Load more until the one for
2969 pos->overlay_string_index is in IT->overlay_strings. */
2970 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2971 {
2972 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2973 it->current.overlay_string_index = 0;
2974 while (n--)
2975 {
2976 load_overlay_strings (it, 0);
2977 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2978 }
2979 }
2980
2981 it->current.overlay_string_index = pos->overlay_string_index;
2982 relative_index = (it->current.overlay_string_index
2983 % OVERLAY_STRING_CHUNK_SIZE);
2984 it->string = it->overlay_strings[relative_index];
2985 xassert (STRINGP (it->string));
2986 it->current.string_pos = pos->string_pos;
2987 it->method = GET_FROM_STRING;
2988 }
2989
2990 if (CHARPOS (pos->string_pos) >= 0)
2991 {
2992 /* Recorded position is not in an overlay string, but in another
2993 string. This can only be a string from a `display' property.
2994 IT should already be filled with that string. */
2995 it->current.string_pos = pos->string_pos;
2996 xassert (STRINGP (it->string));
2997 }
2998
2999 /* Restore position in display vector translations, control
3000 character translations or ellipses. */
3001 if (pos->dpvec_index >= 0)
3002 {
3003 if (it->dpvec == NULL)
3004 get_next_display_element (it);
3005 xassert (it->dpvec && it->current.dpvec_index == 0);
3006 it->current.dpvec_index = pos->dpvec_index;
3007 }
3008
3009 CHECK_IT (it);
3010 return !overlay_strings_with_newlines;
3011 }
3012
3013
3014 /* Initialize IT for stepping through current_buffer in window W
3015 starting at ROW->start. */
3016
3017 static void
3018 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3019 {
3020 init_from_display_pos (it, w, &row->start);
3021 it->start = row->start;
3022 it->continuation_lines_width = row->continuation_lines_width;
3023 CHECK_IT (it);
3024 }
3025
3026
3027 /* Initialize IT for stepping through current_buffer in window W
3028 starting in the line following ROW, i.e. starting at ROW->end.
3029 Value is zero if there are overlay strings with newlines at ROW's
3030 end position. */
3031
3032 static int
3033 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3034 {
3035 int success = 0;
3036
3037 if (init_from_display_pos (it, w, &row->end))
3038 {
3039 if (row->continued_p)
3040 it->continuation_lines_width
3041 = row->continuation_lines_width + row->pixel_width;
3042 CHECK_IT (it);
3043 success = 1;
3044 }
3045
3046 return success;
3047 }
3048
3049
3050
3051 \f
3052 /***********************************************************************
3053 Text properties
3054 ***********************************************************************/
3055
3056 /* Called when IT reaches IT->stop_charpos. Handle text property and
3057 overlay changes. Set IT->stop_charpos to the next position where
3058 to stop. */
3059
3060 static void
3061 handle_stop (struct it *it)
3062 {
3063 enum prop_handled handled;
3064 int handle_overlay_change_p;
3065 struct props *p;
3066
3067 it->dpvec = NULL;
3068 it->current.dpvec_index = -1;
3069 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3070 it->ignore_overlay_strings_at_pos_p = 0;
3071 it->ellipsis_p = 0;
3072
3073 /* Use face of preceding text for ellipsis (if invisible) */
3074 if (it->selective_display_ellipsis_p)
3075 it->saved_face_id = it->face_id;
3076
3077 do
3078 {
3079 handled = HANDLED_NORMALLY;
3080
3081 /* Call text property handlers. */
3082 for (p = it_props; p->handler; ++p)
3083 {
3084 handled = p->handler (it);
3085
3086 if (handled == HANDLED_RECOMPUTE_PROPS)
3087 break;
3088 else if (handled == HANDLED_RETURN)
3089 {
3090 /* We still want to show before and after strings from
3091 overlays even if the actual buffer text is replaced. */
3092 if (!handle_overlay_change_p
3093 || it->sp > 1
3094 || !get_overlay_strings_1 (it, 0, 0))
3095 {
3096 if (it->ellipsis_p)
3097 setup_for_ellipsis (it, 0);
3098 /* When handling a display spec, we might load an
3099 empty string. In that case, discard it here. We
3100 used to discard it in handle_single_display_spec,
3101 but that causes get_overlay_strings_1, above, to
3102 ignore overlay strings that we must check. */
3103 if (STRINGP (it->string) && !SCHARS (it->string))
3104 pop_it (it);
3105 return;
3106 }
3107 else if (STRINGP (it->string) && !SCHARS (it->string))
3108 pop_it (it);
3109 else
3110 {
3111 it->ignore_overlay_strings_at_pos_p = 1;
3112 it->string_from_display_prop_p = 0;
3113 it->from_disp_prop_p = 0;
3114 handle_overlay_change_p = 0;
3115 }
3116 handled = HANDLED_RECOMPUTE_PROPS;
3117 break;
3118 }
3119 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3120 handle_overlay_change_p = 0;
3121 }
3122
3123 if (handled != HANDLED_RECOMPUTE_PROPS)
3124 {
3125 /* Don't check for overlay strings below when set to deliver
3126 characters from a display vector. */
3127 if (it->method == GET_FROM_DISPLAY_VECTOR)
3128 handle_overlay_change_p = 0;
3129
3130 /* Handle overlay changes.
3131 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3132 if it finds overlays. */
3133 if (handle_overlay_change_p)
3134 handled = handle_overlay_change (it);
3135 }
3136
3137 if (it->ellipsis_p)
3138 {
3139 setup_for_ellipsis (it, 0);
3140 break;
3141 }
3142 }
3143 while (handled == HANDLED_RECOMPUTE_PROPS);
3144
3145 /* Determine where to stop next. */
3146 if (handled == HANDLED_NORMALLY)
3147 compute_stop_pos (it);
3148 }
3149
3150
3151 /* Compute IT->stop_charpos from text property and overlay change
3152 information for IT's current position. */
3153
3154 static void
3155 compute_stop_pos (struct it *it)
3156 {
3157 register INTERVAL iv, next_iv;
3158 Lisp_Object object, limit, position;
3159 ptrdiff_t charpos, bytepos;
3160
3161 /* If nowhere else, stop at the end. */
3162 it->stop_charpos = it->end_charpos;
3163
3164 if (STRINGP (it->string))
3165 {
3166 /* Strings are usually short, so don't limit the search for
3167 properties. */
3168 object = it->string;
3169 limit = Qnil;
3170 charpos = IT_STRING_CHARPOS (*it);
3171 bytepos = IT_STRING_BYTEPOS (*it);
3172 }
3173 else
3174 {
3175 ptrdiff_t pos;
3176
3177 /* If next overlay change is in front of the current stop pos
3178 (which is IT->end_charpos), stop there. Note: value of
3179 next_overlay_change is point-max if no overlay change
3180 follows. */
3181 charpos = IT_CHARPOS (*it);
3182 bytepos = IT_BYTEPOS (*it);
3183 pos = next_overlay_change (charpos);
3184 if (pos < it->stop_charpos)
3185 it->stop_charpos = pos;
3186
3187 /* If showing the region, we have to stop at the region
3188 start or end because the face might change there. */
3189 if (it->region_beg_charpos > 0)
3190 {
3191 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3192 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3193 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3194 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3195 }
3196
3197 /* Set up variables for computing the stop position from text
3198 property changes. */
3199 XSETBUFFER (object, current_buffer);
3200 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3201 }
3202
3203 /* Get the interval containing IT's position. Value is a null
3204 interval if there isn't such an interval. */
3205 position = make_number (charpos);
3206 iv = validate_interval_range (object, &position, &position, 0);
3207 if (!NULL_INTERVAL_P (iv))
3208 {
3209 Lisp_Object values_here[LAST_PROP_IDX];
3210 struct props *p;
3211
3212 /* Get properties here. */
3213 for (p = it_props; p->handler; ++p)
3214 values_here[p->idx] = textget (iv->plist, *p->name);
3215
3216 /* Look for an interval following iv that has different
3217 properties. */
3218 for (next_iv = next_interval (iv);
3219 (!NULL_INTERVAL_P (next_iv)
3220 && (NILP (limit)
3221 || XFASTINT (limit) > next_iv->position));
3222 next_iv = next_interval (next_iv))
3223 {
3224 for (p = it_props; p->handler; ++p)
3225 {
3226 Lisp_Object new_value;
3227
3228 new_value = textget (next_iv->plist, *p->name);
3229 if (!EQ (values_here[p->idx], new_value))
3230 break;
3231 }
3232
3233 if (p->handler)
3234 break;
3235 }
3236
3237 if (!NULL_INTERVAL_P (next_iv))
3238 {
3239 if (INTEGERP (limit)
3240 && next_iv->position >= XFASTINT (limit))
3241 /* No text property change up to limit. */
3242 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3243 else
3244 /* Text properties change in next_iv. */
3245 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3246 }
3247 }
3248
3249 if (it->cmp_it.id < 0)
3250 {
3251 ptrdiff_t stoppos = it->end_charpos;
3252
3253 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3254 stoppos = -1;
3255 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3256 stoppos, it->string);
3257 }
3258
3259 xassert (STRINGP (it->string)
3260 || (it->stop_charpos >= BEGV
3261 && it->stop_charpos >= IT_CHARPOS (*it)));
3262 }
3263
3264
3265 /* Return the position of the next overlay change after POS in
3266 current_buffer. Value is point-max if no overlay change
3267 follows. This is like `next-overlay-change' but doesn't use
3268 xmalloc. */
3269
3270 static ptrdiff_t
3271 next_overlay_change (ptrdiff_t pos)
3272 {
3273 ptrdiff_t i, noverlays;
3274 ptrdiff_t endpos;
3275 Lisp_Object *overlays;
3276
3277 /* Get all overlays at the given position. */
3278 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3279
3280 /* If any of these overlays ends before endpos,
3281 use its ending point instead. */
3282 for (i = 0; i < noverlays; ++i)
3283 {
3284 Lisp_Object oend;
3285 ptrdiff_t oendpos;
3286
3287 oend = OVERLAY_END (overlays[i]);
3288 oendpos = OVERLAY_POSITION (oend);
3289 endpos = min (endpos, oendpos);
3290 }
3291
3292 return endpos;
3293 }
3294
3295 /* How many characters forward to search for a display property or
3296 display string. Searching too far forward makes the bidi display
3297 sluggish, especially in small windows. */
3298 #define MAX_DISP_SCAN 250
3299
3300 /* Return the character position of a display string at or after
3301 position specified by POSITION. If no display string exists at or
3302 after POSITION, return ZV. A display string is either an overlay
3303 with `display' property whose value is a string, or a `display'
3304 text property whose value is a string. STRING is data about the
3305 string to iterate; if STRING->lstring is nil, we are iterating a
3306 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3307 on a GUI frame. DISP_PROP is set to zero if we searched
3308 MAX_DISP_SCAN characters forward without finding any display
3309 strings, non-zero otherwise. It is set to 2 if the display string
3310 uses any kind of `(space ...)' spec that will produce a stretch of
3311 white space in the text area. */
3312 ptrdiff_t
3313 compute_display_string_pos (struct text_pos *position,
3314 struct bidi_string_data *string,
3315 int frame_window_p, int *disp_prop)
3316 {
3317 /* OBJECT = nil means current buffer. */
3318 Lisp_Object object =
3319 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3320 Lisp_Object pos, spec, limpos;
3321 int string_p = (string && (STRINGP (string->lstring) || string->s));
3322 ptrdiff_t eob = string_p ? string->schars : ZV;
3323 ptrdiff_t begb = string_p ? 0 : BEGV;
3324 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3325 ptrdiff_t lim =
3326 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3327 struct text_pos tpos;
3328 int rv = 0;
3329
3330 *disp_prop = 1;
3331
3332 if (charpos >= eob
3333 /* We don't support display properties whose values are strings
3334 that have display string properties. */
3335 || string->from_disp_str
3336 /* C strings cannot have display properties. */
3337 || (string->s && !STRINGP (object)))
3338 {
3339 *disp_prop = 0;
3340 return eob;
3341 }
3342
3343 /* If the character at CHARPOS is where the display string begins,
3344 return CHARPOS. */
3345 pos = make_number (charpos);
3346 if (STRINGP (object))
3347 bufpos = string->bufpos;
3348 else
3349 bufpos = charpos;
3350 tpos = *position;
3351 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3352 && (charpos <= begb
3353 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3354 object),
3355 spec))
3356 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3357 frame_window_p)))
3358 {
3359 if (rv == 2)
3360 *disp_prop = 2;
3361 return charpos;
3362 }
3363
3364 /* Look forward for the first character with a `display' property
3365 that will replace the underlying text when displayed. */
3366 limpos = make_number (lim);
3367 do {
3368 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3369 CHARPOS (tpos) = XFASTINT (pos);
3370 if (CHARPOS (tpos) >= lim)
3371 {
3372 *disp_prop = 0;
3373 break;
3374 }
3375 if (STRINGP (object))
3376 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3377 else
3378 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3379 spec = Fget_char_property (pos, Qdisplay, object);
3380 if (!STRINGP (object))
3381 bufpos = CHARPOS (tpos);
3382 } while (NILP (spec)
3383 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3384 bufpos, frame_window_p)));
3385 if (rv == 2)
3386 *disp_prop = 2;
3387
3388 return CHARPOS (tpos);
3389 }
3390
3391 /* Return the character position of the end of the display string that
3392 started at CHARPOS. If there's no display string at CHARPOS,
3393 return -1. A display string is either an overlay with `display'
3394 property whose value is a string or a `display' text property whose
3395 value is a string. */
3396 ptrdiff_t
3397 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3398 {
3399 /* OBJECT = nil means current buffer. */
3400 Lisp_Object object =
3401 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3402 Lisp_Object pos = make_number (charpos);
3403 ptrdiff_t eob =
3404 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3405
3406 if (charpos >= eob || (string->s && !STRINGP (object)))
3407 return eob;
3408
3409 /* It could happen that the display property or overlay was removed
3410 since we found it in compute_display_string_pos above. One way
3411 this can happen is if JIT font-lock was called (through
3412 handle_fontified_prop), and jit-lock-functions remove text
3413 properties or overlays from the portion of buffer that includes
3414 CHARPOS. Muse mode is known to do that, for example. In this
3415 case, we return -1 to the caller, to signal that no display
3416 string is actually present at CHARPOS. See bidi_fetch_char for
3417 how this is handled.
3418
3419 An alternative would be to never look for display properties past
3420 it->stop_charpos. But neither compute_display_string_pos nor
3421 bidi_fetch_char that calls it know or care where the next
3422 stop_charpos is. */
3423 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3424 return -1;
3425
3426 /* Look forward for the first character where the `display' property
3427 changes. */
3428 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3429
3430 return XFASTINT (pos);
3431 }
3432
3433
3434 \f
3435 /***********************************************************************
3436 Fontification
3437 ***********************************************************************/
3438
3439 /* Handle changes in the `fontified' property of the current buffer by
3440 calling hook functions from Qfontification_functions to fontify
3441 regions of text. */
3442
3443 static enum prop_handled
3444 handle_fontified_prop (struct it *it)
3445 {
3446 Lisp_Object prop, pos;
3447 enum prop_handled handled = HANDLED_NORMALLY;
3448
3449 if (!NILP (Vmemory_full))
3450 return handled;
3451
3452 /* Get the value of the `fontified' property at IT's current buffer
3453 position. (The `fontified' property doesn't have a special
3454 meaning in strings.) If the value is nil, call functions from
3455 Qfontification_functions. */
3456 if (!STRINGP (it->string)
3457 && it->s == NULL
3458 && !NILP (Vfontification_functions)
3459 && !NILP (Vrun_hooks)
3460 && (pos = make_number (IT_CHARPOS (*it)),
3461 prop = Fget_char_property (pos, Qfontified, Qnil),
3462 /* Ignore the special cased nil value always present at EOB since
3463 no amount of fontifying will be able to change it. */
3464 NILP (prop) && IT_CHARPOS (*it) < Z))
3465 {
3466 ptrdiff_t count = SPECPDL_INDEX ();
3467 Lisp_Object val;
3468 struct buffer *obuf = current_buffer;
3469 int begv = BEGV, zv = ZV;
3470 int old_clip_changed = current_buffer->clip_changed;
3471
3472 val = Vfontification_functions;
3473 specbind (Qfontification_functions, Qnil);
3474
3475 xassert (it->end_charpos == ZV);
3476
3477 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3478 safe_call1 (val, pos);
3479 else
3480 {
3481 Lisp_Object fns, fn;
3482 struct gcpro gcpro1, gcpro2;
3483
3484 fns = Qnil;
3485 GCPRO2 (val, fns);
3486
3487 for (; CONSP (val); val = XCDR (val))
3488 {
3489 fn = XCAR (val);
3490
3491 if (EQ (fn, Qt))
3492 {
3493 /* A value of t indicates this hook has a local
3494 binding; it means to run the global binding too.
3495 In a global value, t should not occur. If it
3496 does, we must ignore it to avoid an endless
3497 loop. */
3498 for (fns = Fdefault_value (Qfontification_functions);
3499 CONSP (fns);
3500 fns = XCDR (fns))
3501 {
3502 fn = XCAR (fns);
3503 if (!EQ (fn, Qt))
3504 safe_call1 (fn, pos);
3505 }
3506 }
3507 else
3508 safe_call1 (fn, pos);
3509 }
3510
3511 UNGCPRO;
3512 }
3513
3514 unbind_to (count, Qnil);
3515
3516 /* Fontification functions routinely call `save-restriction'.
3517 Normally, this tags clip_changed, which can confuse redisplay
3518 (see discussion in Bug#6671). Since we don't perform any
3519 special handling of fontification changes in the case where
3520 `save-restriction' isn't called, there's no point doing so in
3521 this case either. So, if the buffer's restrictions are
3522 actually left unchanged, reset clip_changed. */
3523 if (obuf == current_buffer)
3524 {
3525 if (begv == BEGV && zv == ZV)
3526 current_buffer->clip_changed = old_clip_changed;
3527 }
3528 /* There isn't much we can reasonably do to protect against
3529 misbehaving fontification, but here's a fig leaf. */
3530 else if (!NILP (BVAR (obuf, name)))
3531 set_buffer_internal_1 (obuf);
3532
3533 /* The fontification code may have added/removed text.
3534 It could do even a lot worse, but let's at least protect against
3535 the most obvious case where only the text past `pos' gets changed',
3536 as is/was done in grep.el where some escapes sequences are turned
3537 into face properties (bug#7876). */
3538 it->end_charpos = ZV;
3539
3540 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3541 something. This avoids an endless loop if they failed to
3542 fontify the text for which reason ever. */
3543 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3544 handled = HANDLED_RECOMPUTE_PROPS;
3545 }
3546
3547 return handled;
3548 }
3549
3550
3551 \f
3552 /***********************************************************************
3553 Faces
3554 ***********************************************************************/
3555
3556 /* Set up iterator IT from face properties at its current position.
3557 Called from handle_stop. */
3558
3559 static enum prop_handled
3560 handle_face_prop (struct it *it)
3561 {
3562 int new_face_id;
3563 ptrdiff_t next_stop;
3564
3565 if (!STRINGP (it->string))
3566 {
3567 new_face_id
3568 = face_at_buffer_position (it->w,
3569 IT_CHARPOS (*it),
3570 it->region_beg_charpos,
3571 it->region_end_charpos,
3572 &next_stop,
3573 (IT_CHARPOS (*it)
3574 + TEXT_PROP_DISTANCE_LIMIT),
3575 0, it->base_face_id);
3576
3577 /* Is this a start of a run of characters with box face?
3578 Caveat: this can be called for a freshly initialized
3579 iterator; face_id is -1 in this case. We know that the new
3580 face will not change until limit, i.e. if the new face has a
3581 box, all characters up to limit will have one. But, as
3582 usual, we don't know whether limit is really the end. */
3583 if (new_face_id != it->face_id)
3584 {
3585 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3586
3587 /* If new face has a box but old face has not, this is
3588 the start of a run of characters with box, i.e. it has
3589 a shadow on the left side. The value of face_id of the
3590 iterator will be -1 if this is the initial call that gets
3591 the face. In this case, we have to look in front of IT's
3592 position and see whether there is a face != new_face_id. */
3593 it->start_of_box_run_p
3594 = (new_face->box != FACE_NO_BOX
3595 && (it->face_id >= 0
3596 || IT_CHARPOS (*it) == BEG
3597 || new_face_id != face_before_it_pos (it)));
3598 it->face_box_p = new_face->box != FACE_NO_BOX;
3599 }
3600 }
3601 else
3602 {
3603 int base_face_id;
3604 ptrdiff_t bufpos;
3605 int i;
3606 Lisp_Object from_overlay
3607 = (it->current.overlay_string_index >= 0
3608 ? it->string_overlays[it->current.overlay_string_index]
3609 : Qnil);
3610
3611 /* See if we got to this string directly or indirectly from
3612 an overlay property. That includes the before-string or
3613 after-string of an overlay, strings in display properties
3614 provided by an overlay, their text properties, etc.
3615
3616 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3617 if (! NILP (from_overlay))
3618 for (i = it->sp - 1; i >= 0; i--)
3619 {
3620 if (it->stack[i].current.overlay_string_index >= 0)
3621 from_overlay
3622 = it->string_overlays[it->stack[i].current.overlay_string_index];
3623 else if (! NILP (it->stack[i].from_overlay))
3624 from_overlay = it->stack[i].from_overlay;
3625
3626 if (!NILP (from_overlay))
3627 break;
3628 }
3629
3630 if (! NILP (from_overlay))
3631 {
3632 bufpos = IT_CHARPOS (*it);
3633 /* For a string from an overlay, the base face depends
3634 only on text properties and ignores overlays. */
3635 base_face_id
3636 = face_for_overlay_string (it->w,
3637 IT_CHARPOS (*it),
3638 it->region_beg_charpos,
3639 it->region_end_charpos,
3640 &next_stop,
3641 (IT_CHARPOS (*it)
3642 + TEXT_PROP_DISTANCE_LIMIT),
3643 0,
3644 from_overlay);
3645 }
3646 else
3647 {
3648 bufpos = 0;
3649
3650 /* For strings from a `display' property, use the face at
3651 IT's current buffer position as the base face to merge
3652 with, so that overlay strings appear in the same face as
3653 surrounding text, unless they specify their own
3654 faces. */
3655 base_face_id = underlying_face_id (it);
3656 }
3657
3658 new_face_id = face_at_string_position (it->w,
3659 it->string,
3660 IT_STRING_CHARPOS (*it),
3661 bufpos,
3662 it->region_beg_charpos,
3663 it->region_end_charpos,
3664 &next_stop,
3665 base_face_id, 0);
3666
3667 /* Is this a start of a run of characters with box? Caveat:
3668 this can be called for a freshly allocated iterator; face_id
3669 is -1 is this case. We know that the new face will not
3670 change until the next check pos, i.e. if the new face has a
3671 box, all characters up to that position will have a
3672 box. But, as usual, we don't know whether that position
3673 is really the end. */
3674 if (new_face_id != it->face_id)
3675 {
3676 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3677 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3678
3679 /* If new face has a box but old face hasn't, this is the
3680 start of a run of characters with box, i.e. it has a
3681 shadow on the left side. */
3682 it->start_of_box_run_p
3683 = new_face->box && (old_face == NULL || !old_face->box);
3684 it->face_box_p = new_face->box != FACE_NO_BOX;
3685 }
3686 }
3687
3688 it->face_id = new_face_id;
3689 return HANDLED_NORMALLY;
3690 }
3691
3692
3693 /* Return the ID of the face ``underlying'' IT's current position,
3694 which is in a string. If the iterator is associated with a
3695 buffer, return the face at IT's current buffer position.
3696 Otherwise, use the iterator's base_face_id. */
3697
3698 static int
3699 underlying_face_id (struct it *it)
3700 {
3701 int face_id = it->base_face_id, i;
3702
3703 xassert (STRINGP (it->string));
3704
3705 for (i = it->sp - 1; i >= 0; --i)
3706 if (NILP (it->stack[i].string))
3707 face_id = it->stack[i].face_id;
3708
3709 return face_id;
3710 }
3711
3712
3713 /* Compute the face one character before or after the current position
3714 of IT, in the visual order. BEFORE_P non-zero means get the face
3715 in front (to the left in L2R paragraphs, to the right in R2L
3716 paragraphs) of IT's screen position. Value is the ID of the face. */
3717
3718 static int
3719 face_before_or_after_it_pos (struct it *it, int before_p)
3720 {
3721 int face_id, limit;
3722 ptrdiff_t next_check_charpos;
3723 struct it it_copy;
3724 void *it_copy_data = NULL;
3725
3726 xassert (it->s == NULL);
3727
3728 if (STRINGP (it->string))
3729 {
3730 ptrdiff_t bufpos, charpos;
3731 int base_face_id;
3732
3733 /* No face change past the end of the string (for the case
3734 we are padding with spaces). No face change before the
3735 string start. */
3736 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3737 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3738 return it->face_id;
3739
3740 if (!it->bidi_p)
3741 {
3742 /* Set charpos to the position before or after IT's current
3743 position, in the logical order, which in the non-bidi
3744 case is the same as the visual order. */
3745 if (before_p)
3746 charpos = IT_STRING_CHARPOS (*it) - 1;
3747 else if (it->what == IT_COMPOSITION)
3748 /* For composition, we must check the character after the
3749 composition. */
3750 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3751 else
3752 charpos = IT_STRING_CHARPOS (*it) + 1;
3753 }
3754 else
3755 {
3756 if (before_p)
3757 {
3758 /* With bidi iteration, the character before the current
3759 in the visual order cannot be found by simple
3760 iteration, because "reverse" reordering is not
3761 supported. Instead, we need to use the move_it_*
3762 family of functions. */
3763 /* Ignore face changes before the first visible
3764 character on this display line. */
3765 if (it->current_x <= it->first_visible_x)
3766 return it->face_id;
3767 SAVE_IT (it_copy, *it, it_copy_data);
3768 /* Implementation note: Since move_it_in_display_line
3769 works in the iterator geometry, and thinks the first
3770 character is always the leftmost, even in R2L lines,
3771 we don't need to distinguish between the R2L and L2R
3772 cases here. */
3773 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3774 it_copy.current_x - 1, MOVE_TO_X);
3775 charpos = IT_STRING_CHARPOS (it_copy);
3776 RESTORE_IT (it, it, it_copy_data);
3777 }
3778 else
3779 {
3780 /* Set charpos to the string position of the character
3781 that comes after IT's current position in the visual
3782 order. */
3783 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3784
3785 it_copy = *it;
3786 while (n--)
3787 bidi_move_to_visually_next (&it_copy.bidi_it);
3788
3789 charpos = it_copy.bidi_it.charpos;
3790 }
3791 }
3792 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3793
3794 if (it->current.overlay_string_index >= 0)
3795 bufpos = IT_CHARPOS (*it);
3796 else
3797 bufpos = 0;
3798
3799 base_face_id = underlying_face_id (it);
3800
3801 /* Get the face for ASCII, or unibyte. */
3802 face_id = face_at_string_position (it->w,
3803 it->string,
3804 charpos,
3805 bufpos,
3806 it->region_beg_charpos,
3807 it->region_end_charpos,
3808 &next_check_charpos,
3809 base_face_id, 0);
3810
3811 /* Correct the face for charsets different from ASCII. Do it
3812 for the multibyte case only. The face returned above is
3813 suitable for unibyte text if IT->string is unibyte. */
3814 if (STRING_MULTIBYTE (it->string))
3815 {
3816 struct text_pos pos1 = string_pos (charpos, it->string);
3817 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3818 int c, len;
3819 struct face *face = FACE_FROM_ID (it->f, face_id);
3820
3821 c = string_char_and_length (p, &len);
3822 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3823 }
3824 }
3825 else
3826 {
3827 struct text_pos pos;
3828
3829 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3830 || (IT_CHARPOS (*it) <= BEGV && before_p))
3831 return it->face_id;
3832
3833 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3834 pos = it->current.pos;
3835
3836 if (!it->bidi_p)
3837 {
3838 if (before_p)
3839 DEC_TEXT_POS (pos, it->multibyte_p);
3840 else
3841 {
3842 if (it->what == IT_COMPOSITION)
3843 {
3844 /* For composition, we must check the position after
3845 the composition. */
3846 pos.charpos += it->cmp_it.nchars;
3847 pos.bytepos += it->len;
3848 }
3849 else
3850 INC_TEXT_POS (pos, it->multibyte_p);
3851 }
3852 }
3853 else
3854 {
3855 if (before_p)
3856 {
3857 /* With bidi iteration, the character before the current
3858 in the visual order cannot be found by simple
3859 iteration, because "reverse" reordering is not
3860 supported. Instead, we need to use the move_it_*
3861 family of functions. */
3862 /* Ignore face changes before the first visible
3863 character on this display line. */
3864 if (it->current_x <= it->first_visible_x)
3865 return it->face_id;
3866 SAVE_IT (it_copy, *it, it_copy_data);
3867 /* Implementation note: Since move_it_in_display_line
3868 works in the iterator geometry, and thinks the first
3869 character is always the leftmost, even in R2L lines,
3870 we don't need to distinguish between the R2L and L2R
3871 cases here. */
3872 move_it_in_display_line (&it_copy, ZV,
3873 it_copy.current_x - 1, MOVE_TO_X);
3874 pos = it_copy.current.pos;
3875 RESTORE_IT (it, it, it_copy_data);
3876 }
3877 else
3878 {
3879 /* Set charpos to the buffer position of the character
3880 that comes after IT's current position in the visual
3881 order. */
3882 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3883
3884 it_copy = *it;
3885 while (n--)
3886 bidi_move_to_visually_next (&it_copy.bidi_it);
3887
3888 SET_TEXT_POS (pos,
3889 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3890 }
3891 }
3892 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3893
3894 /* Determine face for CHARSET_ASCII, or unibyte. */
3895 face_id = face_at_buffer_position (it->w,
3896 CHARPOS (pos),
3897 it->region_beg_charpos,
3898 it->region_end_charpos,
3899 &next_check_charpos,
3900 limit, 0, -1);
3901
3902 /* Correct the face for charsets different from ASCII. Do it
3903 for the multibyte case only. The face returned above is
3904 suitable for unibyte text if current_buffer is unibyte. */
3905 if (it->multibyte_p)
3906 {
3907 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3908 struct face *face = FACE_FROM_ID (it->f, face_id);
3909 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3910 }
3911 }
3912
3913 return face_id;
3914 }
3915
3916
3917 \f
3918 /***********************************************************************
3919 Invisible text
3920 ***********************************************************************/
3921
3922 /* Set up iterator IT from invisible properties at its current
3923 position. Called from handle_stop. */
3924
3925 static enum prop_handled
3926 handle_invisible_prop (struct it *it)
3927 {
3928 enum prop_handled handled = HANDLED_NORMALLY;
3929
3930 if (STRINGP (it->string))
3931 {
3932 Lisp_Object prop, end_charpos, limit, charpos;
3933
3934 /* Get the value of the invisible text property at the
3935 current position. Value will be nil if there is no such
3936 property. */
3937 charpos = make_number (IT_STRING_CHARPOS (*it));
3938 prop = Fget_text_property (charpos, Qinvisible, it->string);
3939
3940 if (!NILP (prop)
3941 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3942 {
3943 ptrdiff_t endpos;
3944
3945 handled = HANDLED_RECOMPUTE_PROPS;
3946
3947 /* Get the position at which the next change of the
3948 invisible text property can be found in IT->string.
3949 Value will be nil if the property value is the same for
3950 all the rest of IT->string. */
3951 XSETINT (limit, SCHARS (it->string));
3952 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3953 it->string, limit);
3954
3955 /* Text at current position is invisible. The next
3956 change in the property is at position end_charpos.
3957 Move IT's current position to that position. */
3958 if (INTEGERP (end_charpos)
3959 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3960 {
3961 struct text_pos old;
3962 ptrdiff_t oldpos;
3963
3964 old = it->current.string_pos;
3965 oldpos = CHARPOS (old);
3966 if (it->bidi_p)
3967 {
3968 if (it->bidi_it.first_elt
3969 && it->bidi_it.charpos < SCHARS (it->string))
3970 bidi_paragraph_init (it->paragraph_embedding,
3971 &it->bidi_it, 1);
3972 /* Bidi-iterate out of the invisible text. */
3973 do
3974 {
3975 bidi_move_to_visually_next (&it->bidi_it);
3976 }
3977 while (oldpos <= it->bidi_it.charpos
3978 && it->bidi_it.charpos < endpos);
3979
3980 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3981 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3982 if (IT_CHARPOS (*it) >= endpos)
3983 it->prev_stop = endpos;
3984 }
3985 else
3986 {
3987 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3988 compute_string_pos (&it->current.string_pos, old, it->string);
3989 }
3990 }
3991 else
3992 {
3993 /* The rest of the string is invisible. If this is an
3994 overlay string, proceed with the next overlay string
3995 or whatever comes and return a character from there. */
3996 if (it->current.overlay_string_index >= 0)
3997 {
3998 next_overlay_string (it);
3999 /* Don't check for overlay strings when we just
4000 finished processing them. */
4001 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4002 }
4003 else
4004 {
4005 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4006 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4007 }
4008 }
4009 }
4010 }
4011 else
4012 {
4013 int invis_p;
4014 ptrdiff_t newpos, next_stop, start_charpos, tem;
4015 Lisp_Object pos, prop, overlay;
4016
4017 /* First of all, is there invisible text at this position? */
4018 tem = start_charpos = IT_CHARPOS (*it);
4019 pos = make_number (tem);
4020 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4021 &overlay);
4022 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4023
4024 /* If we are on invisible text, skip over it. */
4025 if (invis_p && start_charpos < it->end_charpos)
4026 {
4027 /* Record whether we have to display an ellipsis for the
4028 invisible text. */
4029 int display_ellipsis_p = invis_p == 2;
4030
4031 handled = HANDLED_RECOMPUTE_PROPS;
4032
4033 /* Loop skipping over invisible text. The loop is left at
4034 ZV or with IT on the first char being visible again. */
4035 do
4036 {
4037 /* Try to skip some invisible text. Return value is the
4038 position reached which can be equal to where we start
4039 if there is nothing invisible there. This skips both
4040 over invisible text properties and overlays with
4041 invisible property. */
4042 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4043
4044 /* If we skipped nothing at all we weren't at invisible
4045 text in the first place. If everything to the end of
4046 the buffer was skipped, end the loop. */
4047 if (newpos == tem || newpos >= ZV)
4048 invis_p = 0;
4049 else
4050 {
4051 /* We skipped some characters but not necessarily
4052 all there are. Check if we ended up on visible
4053 text. Fget_char_property returns the property of
4054 the char before the given position, i.e. if we
4055 get invis_p = 0, this means that the char at
4056 newpos is visible. */
4057 pos = make_number (newpos);
4058 prop = Fget_char_property (pos, Qinvisible, it->window);
4059 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4060 }
4061
4062 /* If we ended up on invisible text, proceed to
4063 skip starting with next_stop. */
4064 if (invis_p)
4065 tem = next_stop;
4066
4067 /* If there are adjacent invisible texts, don't lose the
4068 second one's ellipsis. */
4069 if (invis_p == 2)
4070 display_ellipsis_p = 1;
4071 }
4072 while (invis_p);
4073
4074 /* The position newpos is now either ZV or on visible text. */
4075 if (it->bidi_p && newpos < ZV)
4076 {
4077 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4078
4079 if (FETCH_BYTE (bpos) == '\n'
4080 || (newpos > BEGV && FETCH_BYTE (bpos - 1) == '\n'))
4081 {
4082 /* If the invisible text ends on a newline or the
4083 character after a newline, we can avoid the
4084 costly, character by character, bidi iteration to
4085 newpos, and instead simply reseat the iterator
4086 there. That's because all bidi reordering
4087 information is tossed at the newline. This is a
4088 big win for modes that hide complete lines, like
4089 Outline, Org, etc. (Implementation note: the
4090 call to reseat_1 is necessary, because it signals
4091 to the bidi iterator that it needs to reinit its
4092 internal information when the next element for
4093 display is requested. */
4094 struct text_pos tpos;
4095
4096 SET_TEXT_POS (tpos, newpos, bpos);
4097 reseat_1 (it, tpos, 0);
4098 }
4099 else /* Must use the slow method. */
4100 {
4101 /* With bidi iteration, the region of invisible text
4102 could start and/or end in the middle of a
4103 non-base embedding level. Therefore, we need to
4104 skip invisible text using the bidi iterator,
4105 starting at IT's current position, until we find
4106 ourselves outside the invisible text. Skipping
4107 invisible text _after_ bidi iteration avoids
4108 affecting the visual order of the displayed text
4109 when invisible properties are added or
4110 removed. */
4111 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4112 {
4113 /* If we were `reseat'ed to a new paragraph,
4114 determine the paragraph base direction. We
4115 need to do it now because
4116 next_element_from_buffer may not have a
4117 chance to do it, if we are going to skip any
4118 text at the beginning, which resets the
4119 FIRST_ELT flag. */
4120 bidi_paragraph_init (it->paragraph_embedding,
4121 &it->bidi_it, 1);
4122 }
4123 do
4124 {
4125 bidi_move_to_visually_next (&it->bidi_it);
4126 }
4127 while (it->stop_charpos <= it->bidi_it.charpos
4128 && it->bidi_it.charpos < newpos);
4129 IT_CHARPOS (*it) = it->bidi_it.charpos;
4130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4131 /* If we overstepped NEWPOS, record its position in
4132 the iterator, so that we skip invisible text if
4133 later the bidi iteration lands us in the
4134 invisible region again. */
4135 if (IT_CHARPOS (*it) >= newpos)
4136 it->prev_stop = newpos;
4137 }
4138 }
4139 else
4140 {
4141 IT_CHARPOS (*it) = newpos;
4142 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4143 }
4144
4145 /* If there are before-strings at the start of invisible
4146 text, and the text is invisible because of a text
4147 property, arrange to show before-strings because 20.x did
4148 it that way. (If the text is invisible because of an
4149 overlay property instead of a text property, this is
4150 already handled in the overlay code.) */
4151 if (NILP (overlay)
4152 && get_overlay_strings (it, it->stop_charpos))
4153 {
4154 handled = HANDLED_RECOMPUTE_PROPS;
4155 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4156 }
4157 else if (display_ellipsis_p)
4158 {
4159 /* Make sure that the glyphs of the ellipsis will get
4160 correct `charpos' values. If we would not update
4161 it->position here, the glyphs would belong to the
4162 last visible character _before_ the invisible
4163 text, which confuses `set_cursor_from_row'.
4164
4165 We use the last invisible position instead of the
4166 first because this way the cursor is always drawn on
4167 the first "." of the ellipsis, whenever PT is inside
4168 the invisible text. Otherwise the cursor would be
4169 placed _after_ the ellipsis when the point is after the
4170 first invisible character. */
4171 if (!STRINGP (it->object))
4172 {
4173 it->position.charpos = newpos - 1;
4174 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4175 }
4176 it->ellipsis_p = 1;
4177 /* Let the ellipsis display before
4178 considering any properties of the following char.
4179 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4180 handled = HANDLED_RETURN;
4181 }
4182 }
4183 }
4184
4185 return handled;
4186 }
4187
4188
4189 /* Make iterator IT return `...' next.
4190 Replaces LEN characters from buffer. */
4191
4192 static void
4193 setup_for_ellipsis (struct it *it, int len)
4194 {
4195 /* Use the display table definition for `...'. Invalid glyphs
4196 will be handled by the method returning elements from dpvec. */
4197 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4198 {
4199 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4200 it->dpvec = v->contents;
4201 it->dpend = v->contents + v->header.size;
4202 }
4203 else
4204 {
4205 /* Default `...'. */
4206 it->dpvec = default_invis_vector;
4207 it->dpend = default_invis_vector + 3;
4208 }
4209
4210 it->dpvec_char_len = len;
4211 it->current.dpvec_index = 0;
4212 it->dpvec_face_id = -1;
4213
4214 /* Remember the current face id in case glyphs specify faces.
4215 IT's face is restored in set_iterator_to_next.
4216 saved_face_id was set to preceding char's face in handle_stop. */
4217 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4218 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4219
4220 it->method = GET_FROM_DISPLAY_VECTOR;
4221 it->ellipsis_p = 1;
4222 }
4223
4224
4225 \f
4226 /***********************************************************************
4227 'display' property
4228 ***********************************************************************/
4229
4230 /* Set up iterator IT from `display' property at its current position.
4231 Called from handle_stop.
4232 We return HANDLED_RETURN if some part of the display property
4233 overrides the display of the buffer text itself.
4234 Otherwise we return HANDLED_NORMALLY. */
4235
4236 static enum prop_handled
4237 handle_display_prop (struct it *it)
4238 {
4239 Lisp_Object propval, object, overlay;
4240 struct text_pos *position;
4241 ptrdiff_t bufpos;
4242 /* Nonzero if some property replaces the display of the text itself. */
4243 int display_replaced_p = 0;
4244
4245 if (STRINGP (it->string))
4246 {
4247 object = it->string;
4248 position = &it->current.string_pos;
4249 bufpos = CHARPOS (it->current.pos);
4250 }
4251 else
4252 {
4253 XSETWINDOW (object, it->w);
4254 position = &it->current.pos;
4255 bufpos = CHARPOS (*position);
4256 }
4257
4258 /* Reset those iterator values set from display property values. */
4259 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4260 it->space_width = Qnil;
4261 it->font_height = Qnil;
4262 it->voffset = 0;
4263
4264 /* We don't support recursive `display' properties, i.e. string
4265 values that have a string `display' property, that have a string
4266 `display' property etc. */
4267 if (!it->string_from_display_prop_p)
4268 it->area = TEXT_AREA;
4269
4270 propval = get_char_property_and_overlay (make_number (position->charpos),
4271 Qdisplay, object, &overlay);
4272 if (NILP (propval))
4273 return HANDLED_NORMALLY;
4274 /* Now OVERLAY is the overlay that gave us this property, or nil
4275 if it was a text property. */
4276
4277 if (!STRINGP (it->string))
4278 object = it->w->buffer;
4279
4280 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4281 position, bufpos,
4282 FRAME_WINDOW_P (it->f));
4283
4284 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4285 }
4286
4287 /* Subroutine of handle_display_prop. Returns non-zero if the display
4288 specification in SPEC is a replacing specification, i.e. it would
4289 replace the text covered by `display' property with something else,
4290 such as an image or a display string. If SPEC includes any kind or
4291 `(space ...) specification, the value is 2; this is used by
4292 compute_display_string_pos, which see.
4293
4294 See handle_single_display_spec for documentation of arguments.
4295 frame_window_p is non-zero if the window being redisplayed is on a
4296 GUI frame; this argument is used only if IT is NULL, see below.
4297
4298 IT can be NULL, if this is called by the bidi reordering code
4299 through compute_display_string_pos, which see. In that case, this
4300 function only examines SPEC, but does not otherwise "handle" it, in
4301 the sense that it doesn't set up members of IT from the display
4302 spec. */
4303 static int
4304 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4305 Lisp_Object overlay, struct text_pos *position,
4306 ptrdiff_t bufpos, int frame_window_p)
4307 {
4308 int replacing_p = 0;
4309 int rv;
4310
4311 if (CONSP (spec)
4312 /* Simple specerties. */
4313 && !EQ (XCAR (spec), Qimage)
4314 && !EQ (XCAR (spec), Qspace)
4315 && !EQ (XCAR (spec), Qwhen)
4316 && !EQ (XCAR (spec), Qslice)
4317 && !EQ (XCAR (spec), Qspace_width)
4318 && !EQ (XCAR (spec), Qheight)
4319 && !EQ (XCAR (spec), Qraise)
4320 /* Marginal area specifications. */
4321 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4322 && !EQ (XCAR (spec), Qleft_fringe)
4323 && !EQ (XCAR (spec), Qright_fringe)
4324 && !NILP (XCAR (spec)))
4325 {
4326 for (; CONSP (spec); spec = XCDR (spec))
4327 {
4328 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4329 overlay, position, bufpos,
4330 replacing_p, frame_window_p)))
4331 {
4332 replacing_p = rv;
4333 /* If some text in a string is replaced, `position' no
4334 longer points to the position of `object'. */
4335 if (!it || STRINGP (object))
4336 break;
4337 }
4338 }
4339 }
4340 else if (VECTORP (spec))
4341 {
4342 ptrdiff_t i;
4343 for (i = 0; i < ASIZE (spec); ++i)
4344 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4345 overlay, position, bufpos,
4346 replacing_p, frame_window_p)))
4347 {
4348 replacing_p = rv;
4349 /* If some text in a string is replaced, `position' no
4350 longer points to the position of `object'. */
4351 if (!it || STRINGP (object))
4352 break;
4353 }
4354 }
4355 else
4356 {
4357 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4358 position, bufpos, 0,
4359 frame_window_p)))
4360 replacing_p = rv;
4361 }
4362
4363 return replacing_p;
4364 }
4365
4366 /* Value is the position of the end of the `display' property starting
4367 at START_POS in OBJECT. */
4368
4369 static struct text_pos
4370 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4371 {
4372 Lisp_Object end;
4373 struct text_pos end_pos;
4374
4375 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4376 Qdisplay, object, Qnil);
4377 CHARPOS (end_pos) = XFASTINT (end);
4378 if (STRINGP (object))
4379 compute_string_pos (&end_pos, start_pos, it->string);
4380 else
4381 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4382
4383 return end_pos;
4384 }
4385
4386
4387 /* Set up IT from a single `display' property specification SPEC. OBJECT
4388 is the object in which the `display' property was found. *POSITION
4389 is the position in OBJECT at which the `display' property was found.
4390 BUFPOS is the buffer position of OBJECT (different from POSITION if
4391 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4392 previously saw a display specification which already replaced text
4393 display with something else, for example an image; we ignore such
4394 properties after the first one has been processed.
4395
4396 OVERLAY is the overlay this `display' property came from,
4397 or nil if it was a text property.
4398
4399 If SPEC is a `space' or `image' specification, and in some other
4400 cases too, set *POSITION to the position where the `display'
4401 property ends.
4402
4403 If IT is NULL, only examine the property specification in SPEC, but
4404 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4405 is intended to be displayed in a window on a GUI frame.
4406
4407 Value is non-zero if something was found which replaces the display
4408 of buffer or string text. */
4409
4410 static int
4411 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4412 Lisp_Object overlay, struct text_pos *position,
4413 ptrdiff_t bufpos, int display_replaced_p,
4414 int frame_window_p)
4415 {
4416 Lisp_Object form;
4417 Lisp_Object location, value;
4418 struct text_pos start_pos = *position;
4419 int valid_p;
4420
4421 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4422 If the result is non-nil, use VALUE instead of SPEC. */
4423 form = Qt;
4424 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4425 {
4426 spec = XCDR (spec);
4427 if (!CONSP (spec))
4428 return 0;
4429 form = XCAR (spec);
4430 spec = XCDR (spec);
4431 }
4432
4433 if (!NILP (form) && !EQ (form, Qt))
4434 {
4435 ptrdiff_t count = SPECPDL_INDEX ();
4436 struct gcpro gcpro1;
4437
4438 /* Bind `object' to the object having the `display' property, a
4439 buffer or string. Bind `position' to the position in the
4440 object where the property was found, and `buffer-position'
4441 to the current position in the buffer. */
4442
4443 if (NILP (object))
4444 XSETBUFFER (object, current_buffer);
4445 specbind (Qobject, object);
4446 specbind (Qposition, make_number (CHARPOS (*position)));
4447 specbind (Qbuffer_position, make_number (bufpos));
4448 GCPRO1 (form);
4449 form = safe_eval (form);
4450 UNGCPRO;
4451 unbind_to (count, Qnil);
4452 }
4453
4454 if (NILP (form))
4455 return 0;
4456
4457 /* Handle `(height HEIGHT)' specifications. */
4458 if (CONSP (spec)
4459 && EQ (XCAR (spec), Qheight)
4460 && CONSP (XCDR (spec)))
4461 {
4462 if (it)
4463 {
4464 if (!FRAME_WINDOW_P (it->f))
4465 return 0;
4466
4467 it->font_height = XCAR (XCDR (spec));
4468 if (!NILP (it->font_height))
4469 {
4470 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4471 int new_height = -1;
4472
4473 if (CONSP (it->font_height)
4474 && (EQ (XCAR (it->font_height), Qplus)
4475 || EQ (XCAR (it->font_height), Qminus))
4476 && CONSP (XCDR (it->font_height))
4477 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4478 {
4479 /* `(+ N)' or `(- N)' where N is an integer. */
4480 int steps = XINT (XCAR (XCDR (it->font_height)));
4481 if (EQ (XCAR (it->font_height), Qplus))
4482 steps = - steps;
4483 it->face_id = smaller_face (it->f, it->face_id, steps);
4484 }
4485 else if (FUNCTIONP (it->font_height))
4486 {
4487 /* Call function with current height as argument.
4488 Value is the new height. */
4489 Lisp_Object height;
4490 height = safe_call1 (it->font_height,
4491 face->lface[LFACE_HEIGHT_INDEX]);
4492 if (NUMBERP (height))
4493 new_height = XFLOATINT (height);
4494 }
4495 else if (NUMBERP (it->font_height))
4496 {
4497 /* Value is a multiple of the canonical char height. */
4498 struct face *f;
4499
4500 f = FACE_FROM_ID (it->f,
4501 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4502 new_height = (XFLOATINT (it->font_height)
4503 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4504 }
4505 else
4506 {
4507 /* Evaluate IT->font_height with `height' bound to the
4508 current specified height to get the new height. */
4509 ptrdiff_t count = SPECPDL_INDEX ();
4510
4511 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4512 value = safe_eval (it->font_height);
4513 unbind_to (count, Qnil);
4514
4515 if (NUMBERP (value))
4516 new_height = XFLOATINT (value);
4517 }
4518
4519 if (new_height > 0)
4520 it->face_id = face_with_height (it->f, it->face_id, new_height);
4521 }
4522 }
4523
4524 return 0;
4525 }
4526
4527 /* Handle `(space-width WIDTH)'. */
4528 if (CONSP (spec)
4529 && EQ (XCAR (spec), Qspace_width)
4530 && CONSP (XCDR (spec)))
4531 {
4532 if (it)
4533 {
4534 if (!FRAME_WINDOW_P (it->f))
4535 return 0;
4536
4537 value = XCAR (XCDR (spec));
4538 if (NUMBERP (value) && XFLOATINT (value) > 0)
4539 it->space_width = value;
4540 }
4541
4542 return 0;
4543 }
4544
4545 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4546 if (CONSP (spec)
4547 && EQ (XCAR (spec), Qslice))
4548 {
4549 Lisp_Object tem;
4550
4551 if (it)
4552 {
4553 if (!FRAME_WINDOW_P (it->f))
4554 return 0;
4555
4556 if (tem = XCDR (spec), CONSP (tem))
4557 {
4558 it->slice.x = XCAR (tem);
4559 if (tem = XCDR (tem), CONSP (tem))
4560 {
4561 it->slice.y = XCAR (tem);
4562 if (tem = XCDR (tem), CONSP (tem))
4563 {
4564 it->slice.width = XCAR (tem);
4565 if (tem = XCDR (tem), CONSP (tem))
4566 it->slice.height = XCAR (tem);
4567 }
4568 }
4569 }
4570 }
4571
4572 return 0;
4573 }
4574
4575 /* Handle `(raise FACTOR)'. */
4576 if (CONSP (spec)
4577 && EQ (XCAR (spec), Qraise)
4578 && CONSP (XCDR (spec)))
4579 {
4580 if (it)
4581 {
4582 if (!FRAME_WINDOW_P (it->f))
4583 return 0;
4584
4585 #ifdef HAVE_WINDOW_SYSTEM
4586 value = XCAR (XCDR (spec));
4587 if (NUMBERP (value))
4588 {
4589 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4590 it->voffset = - (XFLOATINT (value)
4591 * (FONT_HEIGHT (face->font)));
4592 }
4593 #endif /* HAVE_WINDOW_SYSTEM */
4594 }
4595
4596 return 0;
4597 }
4598
4599 /* Don't handle the other kinds of display specifications
4600 inside a string that we got from a `display' property. */
4601 if (it && it->string_from_display_prop_p)
4602 return 0;
4603
4604 /* Characters having this form of property are not displayed, so
4605 we have to find the end of the property. */
4606 if (it)
4607 {
4608 start_pos = *position;
4609 *position = display_prop_end (it, object, start_pos);
4610 }
4611 value = Qnil;
4612
4613 /* Stop the scan at that end position--we assume that all
4614 text properties change there. */
4615 if (it)
4616 it->stop_charpos = position->charpos;
4617
4618 /* Handle `(left-fringe BITMAP [FACE])'
4619 and `(right-fringe BITMAP [FACE])'. */
4620 if (CONSP (spec)
4621 && (EQ (XCAR (spec), Qleft_fringe)
4622 || EQ (XCAR (spec), Qright_fringe))
4623 && CONSP (XCDR (spec)))
4624 {
4625 int fringe_bitmap;
4626
4627 if (it)
4628 {
4629 if (!FRAME_WINDOW_P (it->f))
4630 /* If we return here, POSITION has been advanced
4631 across the text with this property. */
4632 return 0;
4633 }
4634 else if (!frame_window_p)
4635 return 0;
4636
4637 #ifdef HAVE_WINDOW_SYSTEM
4638 value = XCAR (XCDR (spec));
4639 if (!SYMBOLP (value)
4640 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4641 /* If we return here, POSITION has been advanced
4642 across the text with this property. */
4643 return 0;
4644
4645 if (it)
4646 {
4647 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4648
4649 if (CONSP (XCDR (XCDR (spec))))
4650 {
4651 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4652 int face_id2 = lookup_derived_face (it->f, face_name,
4653 FRINGE_FACE_ID, 0);
4654 if (face_id2 >= 0)
4655 face_id = face_id2;
4656 }
4657
4658 /* Save current settings of IT so that we can restore them
4659 when we are finished with the glyph property value. */
4660 push_it (it, position);
4661
4662 it->area = TEXT_AREA;
4663 it->what = IT_IMAGE;
4664 it->image_id = -1; /* no image */
4665 it->position = start_pos;
4666 it->object = NILP (object) ? it->w->buffer : object;
4667 it->method = GET_FROM_IMAGE;
4668 it->from_overlay = Qnil;
4669 it->face_id = face_id;
4670 it->from_disp_prop_p = 1;
4671
4672 /* Say that we haven't consumed the characters with
4673 `display' property yet. The call to pop_it in
4674 set_iterator_to_next will clean this up. */
4675 *position = start_pos;
4676
4677 if (EQ (XCAR (spec), Qleft_fringe))
4678 {
4679 it->left_user_fringe_bitmap = fringe_bitmap;
4680 it->left_user_fringe_face_id = face_id;
4681 }
4682 else
4683 {
4684 it->right_user_fringe_bitmap = fringe_bitmap;
4685 it->right_user_fringe_face_id = face_id;
4686 }
4687 }
4688 #endif /* HAVE_WINDOW_SYSTEM */
4689 return 1;
4690 }
4691
4692 /* Prepare to handle `((margin left-margin) ...)',
4693 `((margin right-margin) ...)' and `((margin nil) ...)'
4694 prefixes for display specifications. */
4695 location = Qunbound;
4696 if (CONSP (spec) && CONSP (XCAR (spec)))
4697 {
4698 Lisp_Object tem;
4699
4700 value = XCDR (spec);
4701 if (CONSP (value))
4702 value = XCAR (value);
4703
4704 tem = XCAR (spec);
4705 if (EQ (XCAR (tem), Qmargin)
4706 && (tem = XCDR (tem),
4707 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4708 (NILP (tem)
4709 || EQ (tem, Qleft_margin)
4710 || EQ (tem, Qright_margin))))
4711 location = tem;
4712 }
4713
4714 if (EQ (location, Qunbound))
4715 {
4716 location = Qnil;
4717 value = spec;
4718 }
4719
4720 /* After this point, VALUE is the property after any
4721 margin prefix has been stripped. It must be a string,
4722 an image specification, or `(space ...)'.
4723
4724 LOCATION specifies where to display: `left-margin',
4725 `right-margin' or nil. */
4726
4727 valid_p = (STRINGP (value)
4728 #ifdef HAVE_WINDOW_SYSTEM
4729 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4730 && valid_image_p (value))
4731 #endif /* not HAVE_WINDOW_SYSTEM */
4732 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4733
4734 if (valid_p && !display_replaced_p)
4735 {
4736 int retval = 1;
4737
4738 if (!it)
4739 {
4740 /* Callers need to know whether the display spec is any kind
4741 of `(space ...)' spec that is about to affect text-area
4742 display. */
4743 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4744 retval = 2;
4745 return retval;
4746 }
4747
4748 /* Save current settings of IT so that we can restore them
4749 when we are finished with the glyph property value. */
4750 push_it (it, position);
4751 it->from_overlay = overlay;
4752 it->from_disp_prop_p = 1;
4753
4754 if (NILP (location))
4755 it->area = TEXT_AREA;
4756 else if (EQ (location, Qleft_margin))
4757 it->area = LEFT_MARGIN_AREA;
4758 else
4759 it->area = RIGHT_MARGIN_AREA;
4760
4761 if (STRINGP (value))
4762 {
4763 it->string = value;
4764 it->multibyte_p = STRING_MULTIBYTE (it->string);
4765 it->current.overlay_string_index = -1;
4766 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4767 it->end_charpos = it->string_nchars = SCHARS (it->string);
4768 it->method = GET_FROM_STRING;
4769 it->stop_charpos = 0;
4770 it->prev_stop = 0;
4771 it->base_level_stop = 0;
4772 it->string_from_display_prop_p = 1;
4773 /* Say that we haven't consumed the characters with
4774 `display' property yet. The call to pop_it in
4775 set_iterator_to_next will clean this up. */
4776 if (BUFFERP (object))
4777 *position = start_pos;
4778
4779 /* Force paragraph direction to be that of the parent
4780 object. If the parent object's paragraph direction is
4781 not yet determined, default to L2R. */
4782 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4783 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4784 else
4785 it->paragraph_embedding = L2R;
4786
4787 /* Set up the bidi iterator for this display string. */
4788 if (it->bidi_p)
4789 {
4790 it->bidi_it.string.lstring = it->string;
4791 it->bidi_it.string.s = NULL;
4792 it->bidi_it.string.schars = it->end_charpos;
4793 it->bidi_it.string.bufpos = bufpos;
4794 it->bidi_it.string.from_disp_str = 1;
4795 it->bidi_it.string.unibyte = !it->multibyte_p;
4796 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4797 }
4798 }
4799 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4800 {
4801 it->method = GET_FROM_STRETCH;
4802 it->object = value;
4803 *position = it->position = start_pos;
4804 retval = 1 + (it->area == TEXT_AREA);
4805 }
4806 #ifdef HAVE_WINDOW_SYSTEM
4807 else
4808 {
4809 it->what = IT_IMAGE;
4810 it->image_id = lookup_image (it->f, value);
4811 it->position = start_pos;
4812 it->object = NILP (object) ? it->w->buffer : object;
4813 it->method = GET_FROM_IMAGE;
4814
4815 /* Say that we haven't consumed the characters with
4816 `display' property yet. The call to pop_it in
4817 set_iterator_to_next will clean this up. */
4818 *position = start_pos;
4819 }
4820 #endif /* HAVE_WINDOW_SYSTEM */
4821
4822 return retval;
4823 }
4824
4825 /* Invalid property or property not supported. Restore
4826 POSITION to what it was before. */
4827 *position = start_pos;
4828 return 0;
4829 }
4830
4831 /* Check if PROP is a display property value whose text should be
4832 treated as intangible. OVERLAY is the overlay from which PROP
4833 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4834 specify the buffer position covered by PROP. */
4835
4836 int
4837 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4838 ptrdiff_t charpos, ptrdiff_t bytepos)
4839 {
4840 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4841 struct text_pos position;
4842
4843 SET_TEXT_POS (position, charpos, bytepos);
4844 return handle_display_spec (NULL, prop, Qnil, overlay,
4845 &position, charpos, frame_window_p);
4846 }
4847
4848
4849 /* Return 1 if PROP is a display sub-property value containing STRING.
4850
4851 Implementation note: this and the following function are really
4852 special cases of handle_display_spec and
4853 handle_single_display_spec, and should ideally use the same code.
4854 Until they do, these two pairs must be consistent and must be
4855 modified in sync. */
4856
4857 static int
4858 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4859 {
4860 if (EQ (string, prop))
4861 return 1;
4862
4863 /* Skip over `when FORM'. */
4864 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4865 {
4866 prop = XCDR (prop);
4867 if (!CONSP (prop))
4868 return 0;
4869 /* Actually, the condition following `when' should be eval'ed,
4870 like handle_single_display_spec does, and we should return
4871 zero if it evaluates to nil. However, this function is
4872 called only when the buffer was already displayed and some
4873 glyph in the glyph matrix was found to come from a display
4874 string. Therefore, the condition was already evaluated, and
4875 the result was non-nil, otherwise the display string wouldn't
4876 have been displayed and we would have never been called for
4877 this property. Thus, we can skip the evaluation and assume
4878 its result is non-nil. */
4879 prop = XCDR (prop);
4880 }
4881
4882 if (CONSP (prop))
4883 /* Skip over `margin LOCATION'. */
4884 if (EQ (XCAR (prop), Qmargin))
4885 {
4886 prop = XCDR (prop);
4887 if (!CONSP (prop))
4888 return 0;
4889
4890 prop = XCDR (prop);
4891 if (!CONSP (prop))
4892 return 0;
4893 }
4894
4895 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4896 }
4897
4898
4899 /* Return 1 if STRING appears in the `display' property PROP. */
4900
4901 static int
4902 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4903 {
4904 if (CONSP (prop)
4905 && !EQ (XCAR (prop), Qwhen)
4906 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4907 {
4908 /* A list of sub-properties. */
4909 while (CONSP (prop))
4910 {
4911 if (single_display_spec_string_p (XCAR (prop), string))
4912 return 1;
4913 prop = XCDR (prop);
4914 }
4915 }
4916 else if (VECTORP (prop))
4917 {
4918 /* A vector of sub-properties. */
4919 ptrdiff_t i;
4920 for (i = 0; i < ASIZE (prop); ++i)
4921 if (single_display_spec_string_p (AREF (prop, i), string))
4922 return 1;
4923 }
4924 else
4925 return single_display_spec_string_p (prop, string);
4926
4927 return 0;
4928 }
4929
4930 /* Look for STRING in overlays and text properties in the current
4931 buffer, between character positions FROM and TO (excluding TO).
4932 BACK_P non-zero means look back (in this case, TO is supposed to be
4933 less than FROM).
4934 Value is the first character position where STRING was found, or
4935 zero if it wasn't found before hitting TO.
4936
4937 This function may only use code that doesn't eval because it is
4938 called asynchronously from note_mouse_highlight. */
4939
4940 static ptrdiff_t
4941 string_buffer_position_lim (Lisp_Object string,
4942 ptrdiff_t from, ptrdiff_t to, int back_p)
4943 {
4944 Lisp_Object limit, prop, pos;
4945 int found = 0;
4946
4947 pos = make_number (from);
4948
4949 if (!back_p) /* looking forward */
4950 {
4951 limit = make_number (min (to, ZV));
4952 while (!found && !EQ (pos, limit))
4953 {
4954 prop = Fget_char_property (pos, Qdisplay, Qnil);
4955 if (!NILP (prop) && display_prop_string_p (prop, string))
4956 found = 1;
4957 else
4958 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4959 limit);
4960 }
4961 }
4962 else /* looking back */
4963 {
4964 limit = make_number (max (to, BEGV));
4965 while (!found && !EQ (pos, limit))
4966 {
4967 prop = Fget_char_property (pos, Qdisplay, Qnil);
4968 if (!NILP (prop) && display_prop_string_p (prop, string))
4969 found = 1;
4970 else
4971 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4972 limit);
4973 }
4974 }
4975
4976 return found ? XINT (pos) : 0;
4977 }
4978
4979 /* Determine which buffer position in current buffer STRING comes from.
4980 AROUND_CHARPOS is an approximate position where it could come from.
4981 Value is the buffer position or 0 if it couldn't be determined.
4982
4983 This function is necessary because we don't record buffer positions
4984 in glyphs generated from strings (to keep struct glyph small).
4985 This function may only use code that doesn't eval because it is
4986 called asynchronously from note_mouse_highlight. */
4987
4988 static ptrdiff_t
4989 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
4990 {
4991 const int MAX_DISTANCE = 1000;
4992 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
4993 around_charpos + MAX_DISTANCE,
4994 0);
4995
4996 if (!found)
4997 found = string_buffer_position_lim (string, around_charpos,
4998 around_charpos - MAX_DISTANCE, 1);
4999 return found;
5000 }
5001
5002
5003 \f
5004 /***********************************************************************
5005 `composition' property
5006 ***********************************************************************/
5007
5008 /* Set up iterator IT from `composition' property at its current
5009 position. Called from handle_stop. */
5010
5011 static enum prop_handled
5012 handle_composition_prop (struct it *it)
5013 {
5014 Lisp_Object prop, string;
5015 ptrdiff_t pos, pos_byte, start, end;
5016
5017 if (STRINGP (it->string))
5018 {
5019 unsigned char *s;
5020
5021 pos = IT_STRING_CHARPOS (*it);
5022 pos_byte = IT_STRING_BYTEPOS (*it);
5023 string = it->string;
5024 s = SDATA (string) + pos_byte;
5025 it->c = STRING_CHAR (s);
5026 }
5027 else
5028 {
5029 pos = IT_CHARPOS (*it);
5030 pos_byte = IT_BYTEPOS (*it);
5031 string = Qnil;
5032 it->c = FETCH_CHAR (pos_byte);
5033 }
5034
5035 /* If there's a valid composition and point is not inside of the
5036 composition (in the case that the composition is from the current
5037 buffer), draw a glyph composed from the composition components. */
5038 if (find_composition (pos, -1, &start, &end, &prop, string)
5039 && COMPOSITION_VALID_P (start, end, prop)
5040 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5041 {
5042 if (start < pos)
5043 /* As we can't handle this situation (perhaps font-lock added
5044 a new composition), we just return here hoping that next
5045 redisplay will detect this composition much earlier. */
5046 return HANDLED_NORMALLY;
5047 if (start != pos)
5048 {
5049 if (STRINGP (it->string))
5050 pos_byte = string_char_to_byte (it->string, start);
5051 else
5052 pos_byte = CHAR_TO_BYTE (start);
5053 }
5054 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5055 prop, string);
5056
5057 if (it->cmp_it.id >= 0)
5058 {
5059 it->cmp_it.ch = -1;
5060 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5061 it->cmp_it.nglyphs = -1;
5062 }
5063 }
5064
5065 return HANDLED_NORMALLY;
5066 }
5067
5068
5069 \f
5070 /***********************************************************************
5071 Overlay strings
5072 ***********************************************************************/
5073
5074 /* The following structure is used to record overlay strings for
5075 later sorting in load_overlay_strings. */
5076
5077 struct overlay_entry
5078 {
5079 Lisp_Object overlay;
5080 Lisp_Object string;
5081 EMACS_INT priority;
5082 int after_string_p;
5083 };
5084
5085
5086 /* Set up iterator IT from overlay strings at its current position.
5087 Called from handle_stop. */
5088
5089 static enum prop_handled
5090 handle_overlay_change (struct it *it)
5091 {
5092 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5093 return HANDLED_RECOMPUTE_PROPS;
5094 else
5095 return HANDLED_NORMALLY;
5096 }
5097
5098
5099 /* Set up the next overlay string for delivery by IT, if there is an
5100 overlay string to deliver. Called by set_iterator_to_next when the
5101 end of the current overlay string is reached. If there are more
5102 overlay strings to display, IT->string and
5103 IT->current.overlay_string_index are set appropriately here.
5104 Otherwise IT->string is set to nil. */
5105
5106 static void
5107 next_overlay_string (struct it *it)
5108 {
5109 ++it->current.overlay_string_index;
5110 if (it->current.overlay_string_index == it->n_overlay_strings)
5111 {
5112 /* No more overlay strings. Restore IT's settings to what
5113 they were before overlay strings were processed, and
5114 continue to deliver from current_buffer. */
5115
5116 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5117 pop_it (it);
5118 xassert (it->sp > 0
5119 || (NILP (it->string)
5120 && it->method == GET_FROM_BUFFER
5121 && it->stop_charpos >= BEGV
5122 && it->stop_charpos <= it->end_charpos));
5123 it->current.overlay_string_index = -1;
5124 it->n_overlay_strings = 0;
5125 it->overlay_strings_charpos = -1;
5126
5127 /* If we're at the end of the buffer, record that we have
5128 processed the overlay strings there already, so that
5129 next_element_from_buffer doesn't try it again. */
5130 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5131 it->overlay_strings_at_end_processed_p = 1;
5132 }
5133 else
5134 {
5135 /* There are more overlay strings to process. If
5136 IT->current.overlay_string_index has advanced to a position
5137 where we must load IT->overlay_strings with more strings, do
5138 it. We must load at the IT->overlay_strings_charpos where
5139 IT->n_overlay_strings was originally computed; when invisible
5140 text is present, this might not be IT_CHARPOS (Bug#7016). */
5141 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5142
5143 if (it->current.overlay_string_index && i == 0)
5144 load_overlay_strings (it, it->overlay_strings_charpos);
5145
5146 /* Initialize IT to deliver display elements from the overlay
5147 string. */
5148 it->string = it->overlay_strings[i];
5149 it->multibyte_p = STRING_MULTIBYTE (it->string);
5150 SET_TEXT_POS (it->current.string_pos, 0, 0);
5151 it->method = GET_FROM_STRING;
5152 it->stop_charpos = 0;
5153 if (it->cmp_it.stop_pos >= 0)
5154 it->cmp_it.stop_pos = 0;
5155 it->prev_stop = 0;
5156 it->base_level_stop = 0;
5157
5158 /* Set up the bidi iterator for this overlay string. */
5159 if (it->bidi_p)
5160 {
5161 it->bidi_it.string.lstring = it->string;
5162 it->bidi_it.string.s = NULL;
5163 it->bidi_it.string.schars = SCHARS (it->string);
5164 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5165 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5166 it->bidi_it.string.unibyte = !it->multibyte_p;
5167 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5168 }
5169 }
5170
5171 CHECK_IT (it);
5172 }
5173
5174
5175 /* Compare two overlay_entry structures E1 and E2. Used as a
5176 comparison function for qsort in load_overlay_strings. Overlay
5177 strings for the same position are sorted so that
5178
5179 1. All after-strings come in front of before-strings, except
5180 when they come from the same overlay.
5181
5182 2. Within after-strings, strings are sorted so that overlay strings
5183 from overlays with higher priorities come first.
5184
5185 2. Within before-strings, strings are sorted so that overlay
5186 strings from overlays with higher priorities come last.
5187
5188 Value is analogous to strcmp. */
5189
5190
5191 static int
5192 compare_overlay_entries (const void *e1, const void *e2)
5193 {
5194 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5195 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5196 int result;
5197
5198 if (entry1->after_string_p != entry2->after_string_p)
5199 {
5200 /* Let after-strings appear in front of before-strings if
5201 they come from different overlays. */
5202 if (EQ (entry1->overlay, entry2->overlay))
5203 result = entry1->after_string_p ? 1 : -1;
5204 else
5205 result = entry1->after_string_p ? -1 : 1;
5206 }
5207 else if (entry1->priority != entry2->priority)
5208 {
5209 if (entry1->after_string_p)
5210 /* After-strings sorted in order of decreasing priority. */
5211 result = entry2->priority < entry1->priority ? -1 : 1;
5212 else
5213 /* Before-strings sorted in order of increasing priority. */
5214 result = entry1->priority < entry2->priority ? -1 : 1;
5215 }
5216 else
5217 result = 0;
5218
5219 return result;
5220 }
5221
5222
5223 /* Load the vector IT->overlay_strings with overlay strings from IT's
5224 current buffer position, or from CHARPOS if that is > 0. Set
5225 IT->n_overlays to the total number of overlay strings found.
5226
5227 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5228 a time. On entry into load_overlay_strings,
5229 IT->current.overlay_string_index gives the number of overlay
5230 strings that have already been loaded by previous calls to this
5231 function.
5232
5233 IT->add_overlay_start contains an additional overlay start
5234 position to consider for taking overlay strings from, if non-zero.
5235 This position comes into play when the overlay has an `invisible'
5236 property, and both before and after-strings. When we've skipped to
5237 the end of the overlay, because of its `invisible' property, we
5238 nevertheless want its before-string to appear.
5239 IT->add_overlay_start will contain the overlay start position
5240 in this case.
5241
5242 Overlay strings are sorted so that after-string strings come in
5243 front of before-string strings. Within before and after-strings,
5244 strings are sorted by overlay priority. See also function
5245 compare_overlay_entries. */
5246
5247 static void
5248 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5249 {
5250 Lisp_Object overlay, window, str, invisible;
5251 struct Lisp_Overlay *ov;
5252 ptrdiff_t start, end;
5253 ptrdiff_t size = 20;
5254 ptrdiff_t n = 0, i, j;
5255 int invis_p;
5256 struct overlay_entry *entries
5257 = (struct overlay_entry *) alloca (size * sizeof *entries);
5258 USE_SAFE_ALLOCA;
5259
5260 if (charpos <= 0)
5261 charpos = IT_CHARPOS (*it);
5262
5263 /* Append the overlay string STRING of overlay OVERLAY to vector
5264 `entries' which has size `size' and currently contains `n'
5265 elements. AFTER_P non-zero means STRING is an after-string of
5266 OVERLAY. */
5267 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5268 do \
5269 { \
5270 Lisp_Object priority; \
5271 \
5272 if (n == size) \
5273 { \
5274 struct overlay_entry *old = entries; \
5275 SAFE_NALLOCA (entries, 2, size); \
5276 memcpy (entries, old, size * sizeof *entries); \
5277 size *= 2; \
5278 } \
5279 \
5280 entries[n].string = (STRING); \
5281 entries[n].overlay = (OVERLAY); \
5282 priority = Foverlay_get ((OVERLAY), Qpriority); \
5283 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5284 entries[n].after_string_p = (AFTER_P); \
5285 ++n; \
5286 } \
5287 while (0)
5288
5289 /* Process overlay before the overlay center. */
5290 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5291 {
5292 XSETMISC (overlay, ov);
5293 xassert (OVERLAYP (overlay));
5294 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5295 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5296
5297 if (end < charpos)
5298 break;
5299
5300 /* Skip this overlay if it doesn't start or end at IT's current
5301 position. */
5302 if (end != charpos && start != charpos)
5303 continue;
5304
5305 /* Skip this overlay if it doesn't apply to IT->w. */
5306 window = Foverlay_get (overlay, Qwindow);
5307 if (WINDOWP (window) && XWINDOW (window) != it->w)
5308 continue;
5309
5310 /* If the text ``under'' the overlay is invisible, both before-
5311 and after-strings from this overlay are visible; start and
5312 end position are indistinguishable. */
5313 invisible = Foverlay_get (overlay, Qinvisible);
5314 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5315
5316 /* If overlay has a non-empty before-string, record it. */
5317 if ((start == charpos || (end == charpos && invis_p))
5318 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5319 && SCHARS (str))
5320 RECORD_OVERLAY_STRING (overlay, str, 0);
5321
5322 /* If overlay has a non-empty after-string, record it. */
5323 if ((end == charpos || (start == charpos && invis_p))
5324 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5325 && SCHARS (str))
5326 RECORD_OVERLAY_STRING (overlay, str, 1);
5327 }
5328
5329 /* Process overlays after the overlay center. */
5330 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5331 {
5332 XSETMISC (overlay, ov);
5333 xassert (OVERLAYP (overlay));
5334 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5335 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5336
5337 if (start > charpos)
5338 break;
5339
5340 /* Skip this overlay if it doesn't start or end at IT's current
5341 position. */
5342 if (end != charpos && start != charpos)
5343 continue;
5344
5345 /* Skip this overlay if it doesn't apply to IT->w. */
5346 window = Foverlay_get (overlay, Qwindow);
5347 if (WINDOWP (window) && XWINDOW (window) != it->w)
5348 continue;
5349
5350 /* If the text ``under'' the overlay is invisible, it has a zero
5351 dimension, and both before- and after-strings apply. */
5352 invisible = Foverlay_get (overlay, Qinvisible);
5353 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5354
5355 /* If overlay has a non-empty before-string, record it. */
5356 if ((start == charpos || (end == charpos && invis_p))
5357 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5358 && SCHARS (str))
5359 RECORD_OVERLAY_STRING (overlay, str, 0);
5360
5361 /* If overlay has a non-empty after-string, record it. */
5362 if ((end == charpos || (start == charpos && invis_p))
5363 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5364 && SCHARS (str))
5365 RECORD_OVERLAY_STRING (overlay, str, 1);
5366 }
5367
5368 #undef RECORD_OVERLAY_STRING
5369
5370 /* Sort entries. */
5371 if (n > 1)
5372 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5373
5374 /* Record number of overlay strings, and where we computed it. */
5375 it->n_overlay_strings = n;
5376 it->overlay_strings_charpos = charpos;
5377
5378 /* IT->current.overlay_string_index is the number of overlay strings
5379 that have already been consumed by IT. Copy some of the
5380 remaining overlay strings to IT->overlay_strings. */
5381 i = 0;
5382 j = it->current.overlay_string_index;
5383 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5384 {
5385 it->overlay_strings[i] = entries[j].string;
5386 it->string_overlays[i++] = entries[j++].overlay;
5387 }
5388
5389 CHECK_IT (it);
5390 SAFE_FREE ();
5391 }
5392
5393
5394 /* Get the first chunk of overlay strings at IT's current buffer
5395 position, or at CHARPOS if that is > 0. Value is non-zero if at
5396 least one overlay string was found. */
5397
5398 static int
5399 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5400 {
5401 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5402 process. This fills IT->overlay_strings with strings, and sets
5403 IT->n_overlay_strings to the total number of strings to process.
5404 IT->pos.overlay_string_index has to be set temporarily to zero
5405 because load_overlay_strings needs this; it must be set to -1
5406 when no overlay strings are found because a zero value would
5407 indicate a position in the first overlay string. */
5408 it->current.overlay_string_index = 0;
5409 load_overlay_strings (it, charpos);
5410
5411 /* If we found overlay strings, set up IT to deliver display
5412 elements from the first one. Otherwise set up IT to deliver
5413 from current_buffer. */
5414 if (it->n_overlay_strings)
5415 {
5416 /* Make sure we know settings in current_buffer, so that we can
5417 restore meaningful values when we're done with the overlay
5418 strings. */
5419 if (compute_stop_p)
5420 compute_stop_pos (it);
5421 xassert (it->face_id >= 0);
5422
5423 /* Save IT's settings. They are restored after all overlay
5424 strings have been processed. */
5425 xassert (!compute_stop_p || it->sp == 0);
5426
5427 /* When called from handle_stop, there might be an empty display
5428 string loaded. In that case, don't bother saving it. */
5429 if (!STRINGP (it->string) || SCHARS (it->string))
5430 push_it (it, NULL);
5431
5432 /* Set up IT to deliver display elements from the first overlay
5433 string. */
5434 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5435 it->string = it->overlay_strings[0];
5436 it->from_overlay = Qnil;
5437 it->stop_charpos = 0;
5438 xassert (STRINGP (it->string));
5439 it->end_charpos = SCHARS (it->string);
5440 it->prev_stop = 0;
5441 it->base_level_stop = 0;
5442 it->multibyte_p = STRING_MULTIBYTE (it->string);
5443 it->method = GET_FROM_STRING;
5444 it->from_disp_prop_p = 0;
5445
5446 /* Force paragraph direction to be that of the parent
5447 buffer. */
5448 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5449 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5450 else
5451 it->paragraph_embedding = L2R;
5452
5453 /* Set up the bidi iterator for this overlay string. */
5454 if (it->bidi_p)
5455 {
5456 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5457
5458 it->bidi_it.string.lstring = it->string;
5459 it->bidi_it.string.s = NULL;
5460 it->bidi_it.string.schars = SCHARS (it->string);
5461 it->bidi_it.string.bufpos = pos;
5462 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5463 it->bidi_it.string.unibyte = !it->multibyte_p;
5464 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5465 }
5466 return 1;
5467 }
5468
5469 it->current.overlay_string_index = -1;
5470 return 0;
5471 }
5472
5473 static int
5474 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5475 {
5476 it->string = Qnil;
5477 it->method = GET_FROM_BUFFER;
5478
5479 (void) get_overlay_strings_1 (it, charpos, 1);
5480
5481 CHECK_IT (it);
5482
5483 /* Value is non-zero if we found at least one overlay string. */
5484 return STRINGP (it->string);
5485 }
5486
5487
5488 \f
5489 /***********************************************************************
5490 Saving and restoring state
5491 ***********************************************************************/
5492
5493 /* Save current settings of IT on IT->stack. Called, for example,
5494 before setting up IT for an overlay string, to be able to restore
5495 IT's settings to what they were after the overlay string has been
5496 processed. If POSITION is non-NULL, it is the position to save on
5497 the stack instead of IT->position. */
5498
5499 static void
5500 push_it (struct it *it, struct text_pos *position)
5501 {
5502 struct iterator_stack_entry *p;
5503
5504 xassert (it->sp < IT_STACK_SIZE);
5505 p = it->stack + it->sp;
5506
5507 p->stop_charpos = it->stop_charpos;
5508 p->prev_stop = it->prev_stop;
5509 p->base_level_stop = it->base_level_stop;
5510 p->cmp_it = it->cmp_it;
5511 xassert (it->face_id >= 0);
5512 p->face_id = it->face_id;
5513 p->string = it->string;
5514 p->method = it->method;
5515 p->from_overlay = it->from_overlay;
5516 switch (p->method)
5517 {
5518 case GET_FROM_IMAGE:
5519 p->u.image.object = it->object;
5520 p->u.image.image_id = it->image_id;
5521 p->u.image.slice = it->slice;
5522 break;
5523 case GET_FROM_STRETCH:
5524 p->u.stretch.object = it->object;
5525 break;
5526 }
5527 p->position = position ? *position : it->position;
5528 p->current = it->current;
5529 p->end_charpos = it->end_charpos;
5530 p->string_nchars = it->string_nchars;
5531 p->area = it->area;
5532 p->multibyte_p = it->multibyte_p;
5533 p->avoid_cursor_p = it->avoid_cursor_p;
5534 p->space_width = it->space_width;
5535 p->font_height = it->font_height;
5536 p->voffset = it->voffset;
5537 p->string_from_display_prop_p = it->string_from_display_prop_p;
5538 p->display_ellipsis_p = 0;
5539 p->line_wrap = it->line_wrap;
5540 p->bidi_p = it->bidi_p;
5541 p->paragraph_embedding = it->paragraph_embedding;
5542 p->from_disp_prop_p = it->from_disp_prop_p;
5543 ++it->sp;
5544
5545 /* Save the state of the bidi iterator as well. */
5546 if (it->bidi_p)
5547 bidi_push_it (&it->bidi_it);
5548 }
5549
5550 static void
5551 iterate_out_of_display_property (struct it *it)
5552 {
5553 int buffer_p = BUFFERP (it->object);
5554 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5555 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5556
5557 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5558
5559 /* Maybe initialize paragraph direction. If we are at the beginning
5560 of a new paragraph, next_element_from_buffer may not have a
5561 chance to do that. */
5562 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5563 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5564 /* prev_stop can be zero, so check against BEGV as well. */
5565 while (it->bidi_it.charpos >= bob
5566 && it->prev_stop <= it->bidi_it.charpos
5567 && it->bidi_it.charpos < CHARPOS (it->position)
5568 && it->bidi_it.charpos < eob)
5569 bidi_move_to_visually_next (&it->bidi_it);
5570 /* Record the stop_pos we just crossed, for when we cross it
5571 back, maybe. */
5572 if (it->bidi_it.charpos > CHARPOS (it->position))
5573 it->prev_stop = CHARPOS (it->position);
5574 /* If we ended up not where pop_it put us, resync IT's
5575 positional members with the bidi iterator. */
5576 if (it->bidi_it.charpos != CHARPOS (it->position))
5577 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5578 if (buffer_p)
5579 it->current.pos = it->position;
5580 else
5581 it->current.string_pos = it->position;
5582 }
5583
5584 /* Restore IT's settings from IT->stack. Called, for example, when no
5585 more overlay strings must be processed, and we return to delivering
5586 display elements from a buffer, or when the end of a string from a
5587 `display' property is reached and we return to delivering display
5588 elements from an overlay string, or from a buffer. */
5589
5590 static void
5591 pop_it (struct it *it)
5592 {
5593 struct iterator_stack_entry *p;
5594 int from_display_prop = it->from_disp_prop_p;
5595
5596 xassert (it->sp > 0);
5597 --it->sp;
5598 p = it->stack + it->sp;
5599 it->stop_charpos = p->stop_charpos;
5600 it->prev_stop = p->prev_stop;
5601 it->base_level_stop = p->base_level_stop;
5602 it->cmp_it = p->cmp_it;
5603 it->face_id = p->face_id;
5604 it->current = p->current;
5605 it->position = p->position;
5606 it->string = p->string;
5607 it->from_overlay = p->from_overlay;
5608 if (NILP (it->string))
5609 SET_TEXT_POS (it->current.string_pos, -1, -1);
5610 it->method = p->method;
5611 switch (it->method)
5612 {
5613 case GET_FROM_IMAGE:
5614 it->image_id = p->u.image.image_id;
5615 it->object = p->u.image.object;
5616 it->slice = p->u.image.slice;
5617 break;
5618 case GET_FROM_STRETCH:
5619 it->object = p->u.stretch.object;
5620 break;
5621 case GET_FROM_BUFFER:
5622 it->object = it->w->buffer;
5623 break;
5624 case GET_FROM_STRING:
5625 it->object = it->string;
5626 break;
5627 case GET_FROM_DISPLAY_VECTOR:
5628 if (it->s)
5629 it->method = GET_FROM_C_STRING;
5630 else if (STRINGP (it->string))
5631 it->method = GET_FROM_STRING;
5632 else
5633 {
5634 it->method = GET_FROM_BUFFER;
5635 it->object = it->w->buffer;
5636 }
5637 }
5638 it->end_charpos = p->end_charpos;
5639 it->string_nchars = p->string_nchars;
5640 it->area = p->area;
5641 it->multibyte_p = p->multibyte_p;
5642 it->avoid_cursor_p = p->avoid_cursor_p;
5643 it->space_width = p->space_width;
5644 it->font_height = p->font_height;
5645 it->voffset = p->voffset;
5646 it->string_from_display_prop_p = p->string_from_display_prop_p;
5647 it->line_wrap = p->line_wrap;
5648 it->bidi_p = p->bidi_p;
5649 it->paragraph_embedding = p->paragraph_embedding;
5650 it->from_disp_prop_p = p->from_disp_prop_p;
5651 if (it->bidi_p)
5652 {
5653 bidi_pop_it (&it->bidi_it);
5654 /* Bidi-iterate until we get out of the portion of text, if any,
5655 covered by a `display' text property or by an overlay with
5656 `display' property. (We cannot just jump there, because the
5657 internal coherency of the bidi iterator state can not be
5658 preserved across such jumps.) We also must determine the
5659 paragraph base direction if the overlay we just processed is
5660 at the beginning of a new paragraph. */
5661 if (from_display_prop
5662 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5663 iterate_out_of_display_property (it);
5664
5665 xassert ((BUFFERP (it->object)
5666 && IT_CHARPOS (*it) == it->bidi_it.charpos
5667 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5668 || (STRINGP (it->object)
5669 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5670 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5671 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5672 }
5673 }
5674
5675
5676 \f
5677 /***********************************************************************
5678 Moving over lines
5679 ***********************************************************************/
5680
5681 /* Set IT's current position to the previous line start. */
5682
5683 static void
5684 back_to_previous_line_start (struct it *it)
5685 {
5686 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5687 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5688 }
5689
5690
5691 /* Move IT to the next line start.
5692
5693 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5694 we skipped over part of the text (as opposed to moving the iterator
5695 continuously over the text). Otherwise, don't change the value
5696 of *SKIPPED_P.
5697
5698 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5699 iterator on the newline, if it was found.
5700
5701 Newlines may come from buffer text, overlay strings, or strings
5702 displayed via the `display' property. That's the reason we can't
5703 simply use find_next_newline_no_quit.
5704
5705 Note that this function may not skip over invisible text that is so
5706 because of text properties and immediately follows a newline. If
5707 it would, function reseat_at_next_visible_line_start, when called
5708 from set_iterator_to_next, would effectively make invisible
5709 characters following a newline part of the wrong glyph row, which
5710 leads to wrong cursor motion. */
5711
5712 static int
5713 forward_to_next_line_start (struct it *it, int *skipped_p,
5714 struct bidi_it *bidi_it_prev)
5715 {
5716 ptrdiff_t old_selective;
5717 int newline_found_p, n;
5718 const int MAX_NEWLINE_DISTANCE = 500;
5719
5720 /* If already on a newline, just consume it to avoid unintended
5721 skipping over invisible text below. */
5722 if (it->what == IT_CHARACTER
5723 && it->c == '\n'
5724 && CHARPOS (it->position) == IT_CHARPOS (*it))
5725 {
5726 if (it->bidi_p && bidi_it_prev)
5727 *bidi_it_prev = it->bidi_it;
5728 set_iterator_to_next (it, 0);
5729 it->c = 0;
5730 return 1;
5731 }
5732
5733 /* Don't handle selective display in the following. It's (a)
5734 unnecessary because it's done by the caller, and (b) leads to an
5735 infinite recursion because next_element_from_ellipsis indirectly
5736 calls this function. */
5737 old_selective = it->selective;
5738 it->selective = 0;
5739
5740 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5741 from buffer text. */
5742 for (n = newline_found_p = 0;
5743 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5744 n += STRINGP (it->string) ? 0 : 1)
5745 {
5746 if (!get_next_display_element (it))
5747 return 0;
5748 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5749 if (newline_found_p && it->bidi_p && bidi_it_prev)
5750 *bidi_it_prev = it->bidi_it;
5751 set_iterator_to_next (it, 0);
5752 }
5753
5754 /* If we didn't find a newline near enough, see if we can use a
5755 short-cut. */
5756 if (!newline_found_p)
5757 {
5758 ptrdiff_t start = IT_CHARPOS (*it);
5759 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5760 Lisp_Object pos;
5761
5762 xassert (!STRINGP (it->string));
5763
5764 /* If there isn't any `display' property in sight, and no
5765 overlays, we can just use the position of the newline in
5766 buffer text. */
5767 if (it->stop_charpos >= limit
5768 || ((pos = Fnext_single_property_change (make_number (start),
5769 Qdisplay, Qnil,
5770 make_number (limit)),
5771 NILP (pos))
5772 && next_overlay_change (start) == ZV))
5773 {
5774 if (!it->bidi_p)
5775 {
5776 IT_CHARPOS (*it) = limit;
5777 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5778 }
5779 else
5780 {
5781 struct bidi_it bprev;
5782
5783 /* Help bidi.c avoid expensive searches for display
5784 properties and overlays, by telling it that there are
5785 none up to `limit'. */
5786 if (it->bidi_it.disp_pos < limit)
5787 {
5788 it->bidi_it.disp_pos = limit;
5789 it->bidi_it.disp_prop = 0;
5790 }
5791 do {
5792 bprev = it->bidi_it;
5793 bidi_move_to_visually_next (&it->bidi_it);
5794 } while (it->bidi_it.charpos != limit);
5795 IT_CHARPOS (*it) = limit;
5796 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5797 if (bidi_it_prev)
5798 *bidi_it_prev = bprev;
5799 }
5800 *skipped_p = newline_found_p = 1;
5801 }
5802 else
5803 {
5804 while (get_next_display_element (it)
5805 && !newline_found_p)
5806 {
5807 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5808 if (newline_found_p && it->bidi_p && bidi_it_prev)
5809 *bidi_it_prev = it->bidi_it;
5810 set_iterator_to_next (it, 0);
5811 }
5812 }
5813 }
5814
5815 it->selective = old_selective;
5816 return newline_found_p;
5817 }
5818
5819
5820 /* Set IT's current position to the previous visible line start. Skip
5821 invisible text that is so either due to text properties or due to
5822 selective display. Caution: this does not change IT->current_x and
5823 IT->hpos. */
5824
5825 static void
5826 back_to_previous_visible_line_start (struct it *it)
5827 {
5828 while (IT_CHARPOS (*it) > BEGV)
5829 {
5830 back_to_previous_line_start (it);
5831
5832 if (IT_CHARPOS (*it) <= BEGV)
5833 break;
5834
5835 /* If selective > 0, then lines indented more than its value are
5836 invisible. */
5837 if (it->selective > 0
5838 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5839 it->selective))
5840 continue;
5841
5842 /* Check the newline before point for invisibility. */
5843 {
5844 Lisp_Object prop;
5845 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5846 Qinvisible, it->window);
5847 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5848 continue;
5849 }
5850
5851 if (IT_CHARPOS (*it) <= BEGV)
5852 break;
5853
5854 {
5855 struct it it2;
5856 void *it2data = NULL;
5857 ptrdiff_t pos;
5858 ptrdiff_t beg, end;
5859 Lisp_Object val, overlay;
5860
5861 SAVE_IT (it2, *it, it2data);
5862
5863 /* If newline is part of a composition, continue from start of composition */
5864 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5865 && beg < IT_CHARPOS (*it))
5866 goto replaced;
5867
5868 /* If newline is replaced by a display property, find start of overlay
5869 or interval and continue search from that point. */
5870 pos = --IT_CHARPOS (it2);
5871 --IT_BYTEPOS (it2);
5872 it2.sp = 0;
5873 bidi_unshelve_cache (NULL, 0);
5874 it2.string_from_display_prop_p = 0;
5875 it2.from_disp_prop_p = 0;
5876 if (handle_display_prop (&it2) == HANDLED_RETURN
5877 && !NILP (val = get_char_property_and_overlay
5878 (make_number (pos), Qdisplay, Qnil, &overlay))
5879 && (OVERLAYP (overlay)
5880 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5881 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5882 {
5883 RESTORE_IT (it, it, it2data);
5884 goto replaced;
5885 }
5886
5887 /* Newline is not replaced by anything -- so we are done. */
5888 RESTORE_IT (it, it, it2data);
5889 break;
5890
5891 replaced:
5892 if (beg < BEGV)
5893 beg = BEGV;
5894 IT_CHARPOS (*it) = beg;
5895 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5896 }
5897 }
5898
5899 it->continuation_lines_width = 0;
5900
5901 xassert (IT_CHARPOS (*it) >= BEGV);
5902 xassert (IT_CHARPOS (*it) == BEGV
5903 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5904 CHECK_IT (it);
5905 }
5906
5907
5908 /* Reseat iterator IT at the previous visible line start. Skip
5909 invisible text that is so either due to text properties or due to
5910 selective display. At the end, update IT's overlay information,
5911 face information etc. */
5912
5913 void
5914 reseat_at_previous_visible_line_start (struct it *it)
5915 {
5916 back_to_previous_visible_line_start (it);
5917 reseat (it, it->current.pos, 1);
5918 CHECK_IT (it);
5919 }
5920
5921
5922 /* Reseat iterator IT on the next visible line start in the current
5923 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5924 preceding the line start. Skip over invisible text that is so
5925 because of selective display. Compute faces, overlays etc at the
5926 new position. Note that this function does not skip over text that
5927 is invisible because of text properties. */
5928
5929 static void
5930 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5931 {
5932 int newline_found_p, skipped_p = 0;
5933 struct bidi_it bidi_it_prev;
5934
5935 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5936
5937 /* Skip over lines that are invisible because they are indented
5938 more than the value of IT->selective. */
5939 if (it->selective > 0)
5940 while (IT_CHARPOS (*it) < ZV
5941 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5942 it->selective))
5943 {
5944 xassert (IT_BYTEPOS (*it) == BEGV
5945 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5946 newline_found_p =
5947 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5948 }
5949
5950 /* Position on the newline if that's what's requested. */
5951 if (on_newline_p && newline_found_p)
5952 {
5953 if (STRINGP (it->string))
5954 {
5955 if (IT_STRING_CHARPOS (*it) > 0)
5956 {
5957 if (!it->bidi_p)
5958 {
5959 --IT_STRING_CHARPOS (*it);
5960 --IT_STRING_BYTEPOS (*it);
5961 }
5962 else
5963 {
5964 /* We need to restore the bidi iterator to the state
5965 it had on the newline, and resync the IT's
5966 position with that. */
5967 it->bidi_it = bidi_it_prev;
5968 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
5969 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
5970 }
5971 }
5972 }
5973 else if (IT_CHARPOS (*it) > BEGV)
5974 {
5975 if (!it->bidi_p)
5976 {
5977 --IT_CHARPOS (*it);
5978 --IT_BYTEPOS (*it);
5979 }
5980 else
5981 {
5982 /* We need to restore the bidi iterator to the state it
5983 had on the newline and resync IT with that. */
5984 it->bidi_it = bidi_it_prev;
5985 IT_CHARPOS (*it) = it->bidi_it.charpos;
5986 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5987 }
5988 reseat (it, it->current.pos, 0);
5989 }
5990 }
5991 else if (skipped_p)
5992 reseat (it, it->current.pos, 0);
5993
5994 CHECK_IT (it);
5995 }
5996
5997
5998 \f
5999 /***********************************************************************
6000 Changing an iterator's position
6001 ***********************************************************************/
6002
6003 /* Change IT's current position to POS in current_buffer. If FORCE_P
6004 is non-zero, always check for text properties at the new position.
6005 Otherwise, text properties are only looked up if POS >=
6006 IT->check_charpos of a property. */
6007
6008 static void
6009 reseat (struct it *it, struct text_pos pos, int force_p)
6010 {
6011 ptrdiff_t original_pos = IT_CHARPOS (*it);
6012
6013 reseat_1 (it, pos, 0);
6014
6015 /* Determine where to check text properties. Avoid doing it
6016 where possible because text property lookup is very expensive. */
6017 if (force_p
6018 || CHARPOS (pos) > it->stop_charpos
6019 || CHARPOS (pos) < original_pos)
6020 {
6021 if (it->bidi_p)
6022 {
6023 /* For bidi iteration, we need to prime prev_stop and
6024 base_level_stop with our best estimations. */
6025 /* Implementation note: Of course, POS is not necessarily a
6026 stop position, so assigning prev_pos to it is a lie; we
6027 should have called compute_stop_backwards. However, if
6028 the current buffer does not include any R2L characters,
6029 that call would be a waste of cycles, because the
6030 iterator will never move back, and thus never cross this
6031 "fake" stop position. So we delay that backward search
6032 until the time we really need it, in next_element_from_buffer. */
6033 if (CHARPOS (pos) != it->prev_stop)
6034 it->prev_stop = CHARPOS (pos);
6035 if (CHARPOS (pos) < it->base_level_stop)
6036 it->base_level_stop = 0; /* meaning it's unknown */
6037 handle_stop (it);
6038 }
6039 else
6040 {
6041 handle_stop (it);
6042 it->prev_stop = it->base_level_stop = 0;
6043 }
6044
6045 }
6046
6047 CHECK_IT (it);
6048 }
6049
6050
6051 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6052 IT->stop_pos to POS, also. */
6053
6054 static void
6055 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6056 {
6057 /* Don't call this function when scanning a C string. */
6058 xassert (it->s == NULL);
6059
6060 /* POS must be a reasonable value. */
6061 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6062
6063 it->current.pos = it->position = pos;
6064 it->end_charpos = ZV;
6065 it->dpvec = NULL;
6066 it->current.dpvec_index = -1;
6067 it->current.overlay_string_index = -1;
6068 IT_STRING_CHARPOS (*it) = -1;
6069 IT_STRING_BYTEPOS (*it) = -1;
6070 it->string = Qnil;
6071 it->method = GET_FROM_BUFFER;
6072 it->object = it->w->buffer;
6073 it->area = TEXT_AREA;
6074 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6075 it->sp = 0;
6076 it->string_from_display_prop_p = 0;
6077 it->from_disp_prop_p = 0;
6078 it->face_before_selective_p = 0;
6079 if (it->bidi_p)
6080 {
6081 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6082 &it->bidi_it);
6083 bidi_unshelve_cache (NULL, 0);
6084 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6085 it->bidi_it.string.s = NULL;
6086 it->bidi_it.string.lstring = Qnil;
6087 it->bidi_it.string.bufpos = 0;
6088 it->bidi_it.string.unibyte = 0;
6089 }
6090
6091 if (set_stop_p)
6092 {
6093 it->stop_charpos = CHARPOS (pos);
6094 it->base_level_stop = CHARPOS (pos);
6095 }
6096 }
6097
6098
6099 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6100 If S is non-null, it is a C string to iterate over. Otherwise,
6101 STRING gives a Lisp string to iterate over.
6102
6103 If PRECISION > 0, don't return more then PRECISION number of
6104 characters from the string.
6105
6106 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6107 characters have been returned. FIELD_WIDTH < 0 means an infinite
6108 field width.
6109
6110 MULTIBYTE = 0 means disable processing of multibyte characters,
6111 MULTIBYTE > 0 means enable it,
6112 MULTIBYTE < 0 means use IT->multibyte_p.
6113
6114 IT must be initialized via a prior call to init_iterator before
6115 calling this function. */
6116
6117 static void
6118 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6119 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6120 int multibyte)
6121 {
6122 /* No region in strings. */
6123 it->region_beg_charpos = it->region_end_charpos = -1;
6124
6125 /* No text property checks performed by default, but see below. */
6126 it->stop_charpos = -1;
6127
6128 /* Set iterator position and end position. */
6129 memset (&it->current, 0, sizeof it->current);
6130 it->current.overlay_string_index = -1;
6131 it->current.dpvec_index = -1;
6132 xassert (charpos >= 0);
6133
6134 /* If STRING is specified, use its multibyteness, otherwise use the
6135 setting of MULTIBYTE, if specified. */
6136 if (multibyte >= 0)
6137 it->multibyte_p = multibyte > 0;
6138
6139 /* Bidirectional reordering of strings is controlled by the default
6140 value of bidi-display-reordering. */
6141 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6142
6143 if (s == NULL)
6144 {
6145 xassert (STRINGP (string));
6146 it->string = string;
6147 it->s = NULL;
6148 it->end_charpos = it->string_nchars = SCHARS (string);
6149 it->method = GET_FROM_STRING;
6150 it->current.string_pos = string_pos (charpos, string);
6151
6152 if (it->bidi_p)
6153 {
6154 it->bidi_it.string.lstring = string;
6155 it->bidi_it.string.s = NULL;
6156 it->bidi_it.string.schars = it->end_charpos;
6157 it->bidi_it.string.bufpos = 0;
6158 it->bidi_it.string.from_disp_str = 0;
6159 it->bidi_it.string.unibyte = !it->multibyte_p;
6160 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6161 FRAME_WINDOW_P (it->f), &it->bidi_it);
6162 }
6163 }
6164 else
6165 {
6166 it->s = (const unsigned char *) s;
6167 it->string = Qnil;
6168
6169 /* Note that we use IT->current.pos, not it->current.string_pos,
6170 for displaying C strings. */
6171 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6172 if (it->multibyte_p)
6173 {
6174 it->current.pos = c_string_pos (charpos, s, 1);
6175 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6176 }
6177 else
6178 {
6179 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6180 it->end_charpos = it->string_nchars = strlen (s);
6181 }
6182
6183 if (it->bidi_p)
6184 {
6185 it->bidi_it.string.lstring = Qnil;
6186 it->bidi_it.string.s = (const unsigned char *) s;
6187 it->bidi_it.string.schars = it->end_charpos;
6188 it->bidi_it.string.bufpos = 0;
6189 it->bidi_it.string.from_disp_str = 0;
6190 it->bidi_it.string.unibyte = !it->multibyte_p;
6191 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6192 &it->bidi_it);
6193 }
6194 it->method = GET_FROM_C_STRING;
6195 }
6196
6197 /* PRECISION > 0 means don't return more than PRECISION characters
6198 from the string. */
6199 if (precision > 0 && it->end_charpos - charpos > precision)
6200 {
6201 it->end_charpos = it->string_nchars = charpos + precision;
6202 if (it->bidi_p)
6203 it->bidi_it.string.schars = it->end_charpos;
6204 }
6205
6206 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6207 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6208 FIELD_WIDTH < 0 means infinite field width. This is useful for
6209 padding with `-' at the end of a mode line. */
6210 if (field_width < 0)
6211 field_width = INFINITY;
6212 /* Implementation note: We deliberately don't enlarge
6213 it->bidi_it.string.schars here to fit it->end_charpos, because
6214 the bidi iterator cannot produce characters out of thin air. */
6215 if (field_width > it->end_charpos - charpos)
6216 it->end_charpos = charpos + field_width;
6217
6218 /* Use the standard display table for displaying strings. */
6219 if (DISP_TABLE_P (Vstandard_display_table))
6220 it->dp = XCHAR_TABLE (Vstandard_display_table);
6221
6222 it->stop_charpos = charpos;
6223 it->prev_stop = charpos;
6224 it->base_level_stop = 0;
6225 if (it->bidi_p)
6226 {
6227 it->bidi_it.first_elt = 1;
6228 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6229 it->bidi_it.disp_pos = -1;
6230 }
6231 if (s == NULL && it->multibyte_p)
6232 {
6233 ptrdiff_t endpos = SCHARS (it->string);
6234 if (endpos > it->end_charpos)
6235 endpos = it->end_charpos;
6236 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6237 it->string);
6238 }
6239 CHECK_IT (it);
6240 }
6241
6242
6243 \f
6244 /***********************************************************************
6245 Iteration
6246 ***********************************************************************/
6247
6248 /* Map enum it_method value to corresponding next_element_from_* function. */
6249
6250 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6251 {
6252 next_element_from_buffer,
6253 next_element_from_display_vector,
6254 next_element_from_string,
6255 next_element_from_c_string,
6256 next_element_from_image,
6257 next_element_from_stretch
6258 };
6259
6260 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6261
6262
6263 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6264 (possibly with the following characters). */
6265
6266 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6267 ((IT)->cmp_it.id >= 0 \
6268 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6269 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6270 END_CHARPOS, (IT)->w, \
6271 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6272 (IT)->string)))
6273
6274
6275 /* Lookup the char-table Vglyphless_char_display for character C (-1
6276 if we want information for no-font case), and return the display
6277 method symbol. By side-effect, update it->what and
6278 it->glyphless_method. This function is called from
6279 get_next_display_element for each character element, and from
6280 x_produce_glyphs when no suitable font was found. */
6281
6282 Lisp_Object
6283 lookup_glyphless_char_display (int c, struct it *it)
6284 {
6285 Lisp_Object glyphless_method = Qnil;
6286
6287 if (CHAR_TABLE_P (Vglyphless_char_display)
6288 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6289 {
6290 if (c >= 0)
6291 {
6292 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6293 if (CONSP (glyphless_method))
6294 glyphless_method = FRAME_WINDOW_P (it->f)
6295 ? XCAR (glyphless_method)
6296 : XCDR (glyphless_method);
6297 }
6298 else
6299 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6300 }
6301
6302 retry:
6303 if (NILP (glyphless_method))
6304 {
6305 if (c >= 0)
6306 /* The default is to display the character by a proper font. */
6307 return Qnil;
6308 /* The default for the no-font case is to display an empty box. */
6309 glyphless_method = Qempty_box;
6310 }
6311 if (EQ (glyphless_method, Qzero_width))
6312 {
6313 if (c >= 0)
6314 return glyphless_method;
6315 /* This method can't be used for the no-font case. */
6316 glyphless_method = Qempty_box;
6317 }
6318 if (EQ (glyphless_method, Qthin_space))
6319 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6320 else if (EQ (glyphless_method, Qempty_box))
6321 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6322 else if (EQ (glyphless_method, Qhex_code))
6323 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6324 else if (STRINGP (glyphless_method))
6325 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6326 else
6327 {
6328 /* Invalid value. We use the default method. */
6329 glyphless_method = Qnil;
6330 goto retry;
6331 }
6332 it->what = IT_GLYPHLESS;
6333 return glyphless_method;
6334 }
6335
6336 /* Load IT's display element fields with information about the next
6337 display element from the current position of IT. Value is zero if
6338 end of buffer (or C string) is reached. */
6339
6340 static struct frame *last_escape_glyph_frame = NULL;
6341 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6342 static int last_escape_glyph_merged_face_id = 0;
6343
6344 struct frame *last_glyphless_glyph_frame = NULL;
6345 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6346 int last_glyphless_glyph_merged_face_id = 0;
6347
6348 static int
6349 get_next_display_element (struct it *it)
6350 {
6351 /* Non-zero means that we found a display element. Zero means that
6352 we hit the end of what we iterate over. Performance note: the
6353 function pointer `method' used here turns out to be faster than
6354 using a sequence of if-statements. */
6355 int success_p;
6356
6357 get_next:
6358 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6359
6360 if (it->what == IT_CHARACTER)
6361 {
6362 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6363 and only if (a) the resolved directionality of that character
6364 is R..." */
6365 /* FIXME: Do we need an exception for characters from display
6366 tables? */
6367 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6368 it->c = bidi_mirror_char (it->c);
6369 /* Map via display table or translate control characters.
6370 IT->c, IT->len etc. have been set to the next character by
6371 the function call above. If we have a display table, and it
6372 contains an entry for IT->c, translate it. Don't do this if
6373 IT->c itself comes from a display table, otherwise we could
6374 end up in an infinite recursion. (An alternative could be to
6375 count the recursion depth of this function and signal an
6376 error when a certain maximum depth is reached.) Is it worth
6377 it? */
6378 if (success_p && it->dpvec == NULL)
6379 {
6380 Lisp_Object dv;
6381 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6382 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6383 nbsp_or_shy = char_is_other;
6384 int c = it->c; /* This is the character to display. */
6385
6386 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6387 {
6388 xassert (SINGLE_BYTE_CHAR_P (c));
6389 if (unibyte_display_via_language_environment)
6390 {
6391 c = DECODE_CHAR (unibyte, c);
6392 if (c < 0)
6393 c = BYTE8_TO_CHAR (it->c);
6394 }
6395 else
6396 c = BYTE8_TO_CHAR (it->c);
6397 }
6398
6399 if (it->dp
6400 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6401 VECTORP (dv)))
6402 {
6403 struct Lisp_Vector *v = XVECTOR (dv);
6404
6405 /* Return the first character from the display table
6406 entry, if not empty. If empty, don't display the
6407 current character. */
6408 if (v->header.size)
6409 {
6410 it->dpvec_char_len = it->len;
6411 it->dpvec = v->contents;
6412 it->dpend = v->contents + v->header.size;
6413 it->current.dpvec_index = 0;
6414 it->dpvec_face_id = -1;
6415 it->saved_face_id = it->face_id;
6416 it->method = GET_FROM_DISPLAY_VECTOR;
6417 it->ellipsis_p = 0;
6418 }
6419 else
6420 {
6421 set_iterator_to_next (it, 0);
6422 }
6423 goto get_next;
6424 }
6425
6426 if (! NILP (lookup_glyphless_char_display (c, it)))
6427 {
6428 if (it->what == IT_GLYPHLESS)
6429 goto done;
6430 /* Don't display this character. */
6431 set_iterator_to_next (it, 0);
6432 goto get_next;
6433 }
6434
6435 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6436 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6437 : c == 0xAD ? char_is_soft_hyphen
6438 : char_is_other);
6439
6440 /* Translate control characters into `\003' or `^C' form.
6441 Control characters coming from a display table entry are
6442 currently not translated because we use IT->dpvec to hold
6443 the translation. This could easily be changed but I
6444 don't believe that it is worth doing.
6445
6446 NBSP and SOFT-HYPEN are property translated too.
6447
6448 Non-printable characters and raw-byte characters are also
6449 translated to octal form. */
6450 if (((c < ' ' || c == 127) /* ASCII control chars */
6451 ? (it->area != TEXT_AREA
6452 /* In mode line, treat \n, \t like other crl chars. */
6453 || (c != '\t'
6454 && it->glyph_row
6455 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6456 || (c != '\n' && c != '\t'))
6457 : (nbsp_or_shy
6458 || CHAR_BYTE8_P (c)
6459 || ! CHAR_PRINTABLE_P (c))))
6460 {
6461 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6462 or a non-printable character which must be displayed
6463 either as '\003' or as `^C' where the '\\' and '^'
6464 can be defined in the display table. Fill
6465 IT->ctl_chars with glyphs for what we have to
6466 display. Then, set IT->dpvec to these glyphs. */
6467 Lisp_Object gc;
6468 int ctl_len;
6469 int face_id;
6470 int lface_id = 0;
6471 int escape_glyph;
6472
6473 /* Handle control characters with ^. */
6474
6475 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6476 {
6477 int g;
6478
6479 g = '^'; /* default glyph for Control */
6480 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6481 if (it->dp
6482 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6483 {
6484 g = GLYPH_CODE_CHAR (gc);
6485 lface_id = GLYPH_CODE_FACE (gc);
6486 }
6487 if (lface_id)
6488 {
6489 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6490 }
6491 else if (it->f == last_escape_glyph_frame
6492 && it->face_id == last_escape_glyph_face_id)
6493 {
6494 face_id = last_escape_glyph_merged_face_id;
6495 }
6496 else
6497 {
6498 /* Merge the escape-glyph face into the current face. */
6499 face_id = merge_faces (it->f, Qescape_glyph, 0,
6500 it->face_id);
6501 last_escape_glyph_frame = it->f;
6502 last_escape_glyph_face_id = it->face_id;
6503 last_escape_glyph_merged_face_id = face_id;
6504 }
6505
6506 XSETINT (it->ctl_chars[0], g);
6507 XSETINT (it->ctl_chars[1], c ^ 0100);
6508 ctl_len = 2;
6509 goto display_control;
6510 }
6511
6512 /* Handle non-break space in the mode where it only gets
6513 highlighting. */
6514
6515 if (EQ (Vnobreak_char_display, Qt)
6516 && nbsp_or_shy == char_is_nbsp)
6517 {
6518 /* Merge the no-break-space face into the current face. */
6519 face_id = merge_faces (it->f, Qnobreak_space, 0,
6520 it->face_id);
6521
6522 c = ' ';
6523 XSETINT (it->ctl_chars[0], ' ');
6524 ctl_len = 1;
6525 goto display_control;
6526 }
6527
6528 /* Handle sequences that start with the "escape glyph". */
6529
6530 /* the default escape glyph is \. */
6531 escape_glyph = '\\';
6532
6533 if (it->dp
6534 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6535 {
6536 escape_glyph = GLYPH_CODE_CHAR (gc);
6537 lface_id = GLYPH_CODE_FACE (gc);
6538 }
6539 if (lface_id)
6540 {
6541 /* The display table specified a face.
6542 Merge it into face_id and also into escape_glyph. */
6543 face_id = merge_faces (it->f, Qt, lface_id,
6544 it->face_id);
6545 }
6546 else if (it->f == last_escape_glyph_frame
6547 && it->face_id == last_escape_glyph_face_id)
6548 {
6549 face_id = last_escape_glyph_merged_face_id;
6550 }
6551 else
6552 {
6553 /* Merge the escape-glyph face into the current face. */
6554 face_id = merge_faces (it->f, Qescape_glyph, 0,
6555 it->face_id);
6556 last_escape_glyph_frame = it->f;
6557 last_escape_glyph_face_id = it->face_id;
6558 last_escape_glyph_merged_face_id = face_id;
6559 }
6560
6561 /* Handle soft hyphens in the mode where they only get
6562 highlighting. */
6563
6564 if (EQ (Vnobreak_char_display, Qt)
6565 && nbsp_or_shy == char_is_soft_hyphen)
6566 {
6567 XSETINT (it->ctl_chars[0], '-');
6568 ctl_len = 1;
6569 goto display_control;
6570 }
6571
6572 /* Handle non-break space and soft hyphen
6573 with the escape glyph. */
6574
6575 if (nbsp_or_shy)
6576 {
6577 XSETINT (it->ctl_chars[0], escape_glyph);
6578 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6579 XSETINT (it->ctl_chars[1], c);
6580 ctl_len = 2;
6581 goto display_control;
6582 }
6583
6584 {
6585 char str[10];
6586 int len, i;
6587
6588 if (CHAR_BYTE8_P (c))
6589 /* Display \200 instead of \17777600. */
6590 c = CHAR_TO_BYTE8 (c);
6591 len = sprintf (str, "%03o", c);
6592
6593 XSETINT (it->ctl_chars[0], escape_glyph);
6594 for (i = 0; i < len; i++)
6595 XSETINT (it->ctl_chars[i + 1], str[i]);
6596 ctl_len = len + 1;
6597 }
6598
6599 display_control:
6600 /* Set up IT->dpvec and return first character from it. */
6601 it->dpvec_char_len = it->len;
6602 it->dpvec = it->ctl_chars;
6603 it->dpend = it->dpvec + ctl_len;
6604 it->current.dpvec_index = 0;
6605 it->dpvec_face_id = face_id;
6606 it->saved_face_id = it->face_id;
6607 it->method = GET_FROM_DISPLAY_VECTOR;
6608 it->ellipsis_p = 0;
6609 goto get_next;
6610 }
6611 it->char_to_display = c;
6612 }
6613 else if (success_p)
6614 {
6615 it->char_to_display = it->c;
6616 }
6617 }
6618
6619 /* Adjust face id for a multibyte character. There are no multibyte
6620 character in unibyte text. */
6621 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6622 && it->multibyte_p
6623 && success_p
6624 && FRAME_WINDOW_P (it->f))
6625 {
6626 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6627
6628 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6629 {
6630 /* Automatic composition with glyph-string. */
6631 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6632
6633 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6634 }
6635 else
6636 {
6637 ptrdiff_t pos = (it->s ? -1
6638 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6639 : IT_CHARPOS (*it));
6640 int c;
6641
6642 if (it->what == IT_CHARACTER)
6643 c = it->char_to_display;
6644 else
6645 {
6646 struct composition *cmp = composition_table[it->cmp_it.id];
6647 int i;
6648
6649 c = ' ';
6650 for (i = 0; i < cmp->glyph_len; i++)
6651 /* TAB in a composition means display glyphs with
6652 padding space on the left or right. */
6653 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6654 break;
6655 }
6656 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6657 }
6658 }
6659
6660 done:
6661 /* Is this character the last one of a run of characters with
6662 box? If yes, set IT->end_of_box_run_p to 1. */
6663 if (it->face_box_p
6664 && it->s == NULL)
6665 {
6666 if (it->method == GET_FROM_STRING && it->sp)
6667 {
6668 int face_id = underlying_face_id (it);
6669 struct face *face = FACE_FROM_ID (it->f, face_id);
6670
6671 if (face)
6672 {
6673 if (face->box == FACE_NO_BOX)
6674 {
6675 /* If the box comes from face properties in a
6676 display string, check faces in that string. */
6677 int string_face_id = face_after_it_pos (it);
6678 it->end_of_box_run_p
6679 = (FACE_FROM_ID (it->f, string_face_id)->box
6680 == FACE_NO_BOX);
6681 }
6682 /* Otherwise, the box comes from the underlying face.
6683 If this is the last string character displayed, check
6684 the next buffer location. */
6685 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6686 && (it->current.overlay_string_index
6687 == it->n_overlay_strings - 1))
6688 {
6689 ptrdiff_t ignore;
6690 int next_face_id;
6691 struct text_pos pos = it->current.pos;
6692 INC_TEXT_POS (pos, it->multibyte_p);
6693
6694 next_face_id = face_at_buffer_position
6695 (it->w, CHARPOS (pos), it->region_beg_charpos,
6696 it->region_end_charpos, &ignore,
6697 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6698 -1);
6699 it->end_of_box_run_p
6700 = (FACE_FROM_ID (it->f, next_face_id)->box
6701 == FACE_NO_BOX);
6702 }
6703 }
6704 }
6705 else
6706 {
6707 int face_id = face_after_it_pos (it);
6708 it->end_of_box_run_p
6709 = (face_id != it->face_id
6710 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6711 }
6712 }
6713
6714 /* Value is 0 if end of buffer or string reached. */
6715 return success_p;
6716 }
6717
6718
6719 /* Move IT to the next display element.
6720
6721 RESEAT_P non-zero means if called on a newline in buffer text,
6722 skip to the next visible line start.
6723
6724 Functions get_next_display_element and set_iterator_to_next are
6725 separate because I find this arrangement easier to handle than a
6726 get_next_display_element function that also increments IT's
6727 position. The way it is we can first look at an iterator's current
6728 display element, decide whether it fits on a line, and if it does,
6729 increment the iterator position. The other way around we probably
6730 would either need a flag indicating whether the iterator has to be
6731 incremented the next time, or we would have to implement a
6732 decrement position function which would not be easy to write. */
6733
6734 void
6735 set_iterator_to_next (struct it *it, int reseat_p)
6736 {
6737 /* Reset flags indicating start and end of a sequence of characters
6738 with box. Reset them at the start of this function because
6739 moving the iterator to a new position might set them. */
6740 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6741
6742 switch (it->method)
6743 {
6744 case GET_FROM_BUFFER:
6745 /* The current display element of IT is a character from
6746 current_buffer. Advance in the buffer, and maybe skip over
6747 invisible lines that are so because of selective display. */
6748 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6749 reseat_at_next_visible_line_start (it, 0);
6750 else if (it->cmp_it.id >= 0)
6751 {
6752 /* We are currently getting glyphs from a composition. */
6753 int i;
6754
6755 if (! it->bidi_p)
6756 {
6757 IT_CHARPOS (*it) += it->cmp_it.nchars;
6758 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6759 if (it->cmp_it.to < it->cmp_it.nglyphs)
6760 {
6761 it->cmp_it.from = it->cmp_it.to;
6762 }
6763 else
6764 {
6765 it->cmp_it.id = -1;
6766 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6767 IT_BYTEPOS (*it),
6768 it->end_charpos, Qnil);
6769 }
6770 }
6771 else if (! it->cmp_it.reversed_p)
6772 {
6773 /* Composition created while scanning forward. */
6774 /* Update IT's char/byte positions to point to the first
6775 character of the next grapheme cluster, or to the
6776 character visually after the current composition. */
6777 for (i = 0; i < it->cmp_it.nchars; i++)
6778 bidi_move_to_visually_next (&it->bidi_it);
6779 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6780 IT_CHARPOS (*it) = it->bidi_it.charpos;
6781
6782 if (it->cmp_it.to < it->cmp_it.nglyphs)
6783 {
6784 /* Proceed to the next grapheme cluster. */
6785 it->cmp_it.from = it->cmp_it.to;
6786 }
6787 else
6788 {
6789 /* No more grapheme clusters in this composition.
6790 Find the next stop position. */
6791 ptrdiff_t stop = it->end_charpos;
6792 if (it->bidi_it.scan_dir < 0)
6793 /* Now we are scanning backward and don't know
6794 where to stop. */
6795 stop = -1;
6796 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6797 IT_BYTEPOS (*it), stop, Qnil);
6798 }
6799 }
6800 else
6801 {
6802 /* Composition created while scanning backward. */
6803 /* Update IT's char/byte positions to point to the last
6804 character of the previous grapheme cluster, or the
6805 character visually after the current composition. */
6806 for (i = 0; i < it->cmp_it.nchars; i++)
6807 bidi_move_to_visually_next (&it->bidi_it);
6808 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6809 IT_CHARPOS (*it) = it->bidi_it.charpos;
6810 if (it->cmp_it.from > 0)
6811 {
6812 /* Proceed to the previous grapheme cluster. */
6813 it->cmp_it.to = it->cmp_it.from;
6814 }
6815 else
6816 {
6817 /* No more grapheme clusters in this composition.
6818 Find the next stop position. */
6819 ptrdiff_t stop = it->end_charpos;
6820 if (it->bidi_it.scan_dir < 0)
6821 /* Now we are scanning backward and don't know
6822 where to stop. */
6823 stop = -1;
6824 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6825 IT_BYTEPOS (*it), stop, Qnil);
6826 }
6827 }
6828 }
6829 else
6830 {
6831 xassert (it->len != 0);
6832
6833 if (!it->bidi_p)
6834 {
6835 IT_BYTEPOS (*it) += it->len;
6836 IT_CHARPOS (*it) += 1;
6837 }
6838 else
6839 {
6840 int prev_scan_dir = it->bidi_it.scan_dir;
6841 /* If this is a new paragraph, determine its base
6842 direction (a.k.a. its base embedding level). */
6843 if (it->bidi_it.new_paragraph)
6844 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6845 bidi_move_to_visually_next (&it->bidi_it);
6846 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6847 IT_CHARPOS (*it) = it->bidi_it.charpos;
6848 if (prev_scan_dir != it->bidi_it.scan_dir)
6849 {
6850 /* As the scan direction was changed, we must
6851 re-compute the stop position for composition. */
6852 ptrdiff_t stop = it->end_charpos;
6853 if (it->bidi_it.scan_dir < 0)
6854 stop = -1;
6855 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6856 IT_BYTEPOS (*it), stop, Qnil);
6857 }
6858 }
6859 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6860 }
6861 break;
6862
6863 case GET_FROM_C_STRING:
6864 /* Current display element of IT is from a C string. */
6865 if (!it->bidi_p
6866 /* If the string position is beyond string's end, it means
6867 next_element_from_c_string is padding the string with
6868 blanks, in which case we bypass the bidi iterator,
6869 because it cannot deal with such virtual characters. */
6870 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6871 {
6872 IT_BYTEPOS (*it) += it->len;
6873 IT_CHARPOS (*it) += 1;
6874 }
6875 else
6876 {
6877 bidi_move_to_visually_next (&it->bidi_it);
6878 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6879 IT_CHARPOS (*it) = it->bidi_it.charpos;
6880 }
6881 break;
6882
6883 case GET_FROM_DISPLAY_VECTOR:
6884 /* Current display element of IT is from a display table entry.
6885 Advance in the display table definition. Reset it to null if
6886 end reached, and continue with characters from buffers/
6887 strings. */
6888 ++it->current.dpvec_index;
6889
6890 /* Restore face of the iterator to what they were before the
6891 display vector entry (these entries may contain faces). */
6892 it->face_id = it->saved_face_id;
6893
6894 if (it->dpvec + it->current.dpvec_index == it->dpend)
6895 {
6896 int recheck_faces = it->ellipsis_p;
6897
6898 if (it->s)
6899 it->method = GET_FROM_C_STRING;
6900 else if (STRINGP (it->string))
6901 it->method = GET_FROM_STRING;
6902 else
6903 {
6904 it->method = GET_FROM_BUFFER;
6905 it->object = it->w->buffer;
6906 }
6907
6908 it->dpvec = NULL;
6909 it->current.dpvec_index = -1;
6910
6911 /* Skip over characters which were displayed via IT->dpvec. */
6912 if (it->dpvec_char_len < 0)
6913 reseat_at_next_visible_line_start (it, 1);
6914 else if (it->dpvec_char_len > 0)
6915 {
6916 if (it->method == GET_FROM_STRING
6917 && it->n_overlay_strings > 0)
6918 it->ignore_overlay_strings_at_pos_p = 1;
6919 it->len = it->dpvec_char_len;
6920 set_iterator_to_next (it, reseat_p);
6921 }
6922
6923 /* Maybe recheck faces after display vector */
6924 if (recheck_faces)
6925 it->stop_charpos = IT_CHARPOS (*it);
6926 }
6927 break;
6928
6929 case GET_FROM_STRING:
6930 /* Current display element is a character from a Lisp string. */
6931 xassert (it->s == NULL && STRINGP (it->string));
6932 if (it->cmp_it.id >= 0)
6933 {
6934 int i;
6935
6936 if (! it->bidi_p)
6937 {
6938 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6939 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6940 if (it->cmp_it.to < it->cmp_it.nglyphs)
6941 it->cmp_it.from = it->cmp_it.to;
6942 else
6943 {
6944 it->cmp_it.id = -1;
6945 composition_compute_stop_pos (&it->cmp_it,
6946 IT_STRING_CHARPOS (*it),
6947 IT_STRING_BYTEPOS (*it),
6948 it->end_charpos, it->string);
6949 }
6950 }
6951 else if (! it->cmp_it.reversed_p)
6952 {
6953 for (i = 0; i < it->cmp_it.nchars; i++)
6954 bidi_move_to_visually_next (&it->bidi_it);
6955 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6956 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6957
6958 if (it->cmp_it.to < it->cmp_it.nglyphs)
6959 it->cmp_it.from = it->cmp_it.to;
6960 else
6961 {
6962 ptrdiff_t stop = it->end_charpos;
6963 if (it->bidi_it.scan_dir < 0)
6964 stop = -1;
6965 composition_compute_stop_pos (&it->cmp_it,
6966 IT_STRING_CHARPOS (*it),
6967 IT_STRING_BYTEPOS (*it), stop,
6968 it->string);
6969 }
6970 }
6971 else
6972 {
6973 for (i = 0; i < it->cmp_it.nchars; i++)
6974 bidi_move_to_visually_next (&it->bidi_it);
6975 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6976 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6977 if (it->cmp_it.from > 0)
6978 it->cmp_it.to = it->cmp_it.from;
6979 else
6980 {
6981 ptrdiff_t stop = it->end_charpos;
6982 if (it->bidi_it.scan_dir < 0)
6983 stop = -1;
6984 composition_compute_stop_pos (&it->cmp_it,
6985 IT_STRING_CHARPOS (*it),
6986 IT_STRING_BYTEPOS (*it), stop,
6987 it->string);
6988 }
6989 }
6990 }
6991 else
6992 {
6993 if (!it->bidi_p
6994 /* If the string position is beyond string's end, it
6995 means next_element_from_string is padding the string
6996 with blanks, in which case we bypass the bidi
6997 iterator, because it cannot deal with such virtual
6998 characters. */
6999 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7000 {
7001 IT_STRING_BYTEPOS (*it) += it->len;
7002 IT_STRING_CHARPOS (*it) += 1;
7003 }
7004 else
7005 {
7006 int prev_scan_dir = it->bidi_it.scan_dir;
7007
7008 bidi_move_to_visually_next (&it->bidi_it);
7009 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7010 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7011 if (prev_scan_dir != it->bidi_it.scan_dir)
7012 {
7013 ptrdiff_t stop = it->end_charpos;
7014
7015 if (it->bidi_it.scan_dir < 0)
7016 stop = -1;
7017 composition_compute_stop_pos (&it->cmp_it,
7018 IT_STRING_CHARPOS (*it),
7019 IT_STRING_BYTEPOS (*it), stop,
7020 it->string);
7021 }
7022 }
7023 }
7024
7025 consider_string_end:
7026
7027 if (it->current.overlay_string_index >= 0)
7028 {
7029 /* IT->string is an overlay string. Advance to the
7030 next, if there is one. */
7031 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7032 {
7033 it->ellipsis_p = 0;
7034 next_overlay_string (it);
7035 if (it->ellipsis_p)
7036 setup_for_ellipsis (it, 0);
7037 }
7038 }
7039 else
7040 {
7041 /* IT->string is not an overlay string. If we reached
7042 its end, and there is something on IT->stack, proceed
7043 with what is on the stack. This can be either another
7044 string, this time an overlay string, or a buffer. */
7045 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7046 && it->sp > 0)
7047 {
7048 pop_it (it);
7049 if (it->method == GET_FROM_STRING)
7050 goto consider_string_end;
7051 }
7052 }
7053 break;
7054
7055 case GET_FROM_IMAGE:
7056 case GET_FROM_STRETCH:
7057 /* The position etc with which we have to proceed are on
7058 the stack. The position may be at the end of a string,
7059 if the `display' property takes up the whole string. */
7060 xassert (it->sp > 0);
7061 pop_it (it);
7062 if (it->method == GET_FROM_STRING)
7063 goto consider_string_end;
7064 break;
7065
7066 default:
7067 /* There are no other methods defined, so this should be a bug. */
7068 abort ();
7069 }
7070
7071 xassert (it->method != GET_FROM_STRING
7072 || (STRINGP (it->string)
7073 && IT_STRING_CHARPOS (*it) >= 0));
7074 }
7075
7076 /* Load IT's display element fields with information about the next
7077 display element which comes from a display table entry or from the
7078 result of translating a control character to one of the forms `^C'
7079 or `\003'.
7080
7081 IT->dpvec holds the glyphs to return as characters.
7082 IT->saved_face_id holds the face id before the display vector--it
7083 is restored into IT->face_id in set_iterator_to_next. */
7084
7085 static int
7086 next_element_from_display_vector (struct it *it)
7087 {
7088 Lisp_Object gc;
7089
7090 /* Precondition. */
7091 xassert (it->dpvec && it->current.dpvec_index >= 0);
7092
7093 it->face_id = it->saved_face_id;
7094
7095 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7096 That seemed totally bogus - so I changed it... */
7097 gc = it->dpvec[it->current.dpvec_index];
7098
7099 if (GLYPH_CODE_P (gc))
7100 {
7101 it->c = GLYPH_CODE_CHAR (gc);
7102 it->len = CHAR_BYTES (it->c);
7103
7104 /* The entry may contain a face id to use. Such a face id is
7105 the id of a Lisp face, not a realized face. A face id of
7106 zero means no face is specified. */
7107 if (it->dpvec_face_id >= 0)
7108 it->face_id = it->dpvec_face_id;
7109 else
7110 {
7111 int lface_id = GLYPH_CODE_FACE (gc);
7112 if (lface_id > 0)
7113 it->face_id = merge_faces (it->f, Qt, lface_id,
7114 it->saved_face_id);
7115 }
7116 }
7117 else
7118 /* Display table entry is invalid. Return a space. */
7119 it->c = ' ', it->len = 1;
7120
7121 /* Don't change position and object of the iterator here. They are
7122 still the values of the character that had this display table
7123 entry or was translated, and that's what we want. */
7124 it->what = IT_CHARACTER;
7125 return 1;
7126 }
7127
7128 /* Get the first element of string/buffer in the visual order, after
7129 being reseated to a new position in a string or a buffer. */
7130 static void
7131 get_visually_first_element (struct it *it)
7132 {
7133 int string_p = STRINGP (it->string) || it->s;
7134 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7135 ptrdiff_t bob = (string_p ? 0 : BEGV);
7136
7137 if (STRINGP (it->string))
7138 {
7139 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7140 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7141 }
7142 else
7143 {
7144 it->bidi_it.charpos = IT_CHARPOS (*it);
7145 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7146 }
7147
7148 if (it->bidi_it.charpos == eob)
7149 {
7150 /* Nothing to do, but reset the FIRST_ELT flag, like
7151 bidi_paragraph_init does, because we are not going to
7152 call it. */
7153 it->bidi_it.first_elt = 0;
7154 }
7155 else if (it->bidi_it.charpos == bob
7156 || (!string_p
7157 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7158 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7159 {
7160 /* If we are at the beginning of a line/string, we can produce
7161 the next element right away. */
7162 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7163 bidi_move_to_visually_next (&it->bidi_it);
7164 }
7165 else
7166 {
7167 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7168
7169 /* We need to prime the bidi iterator starting at the line's or
7170 string's beginning, before we will be able to produce the
7171 next element. */
7172 if (string_p)
7173 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7174 else
7175 {
7176 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7177 -1);
7178 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7179 }
7180 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7181 do
7182 {
7183 /* Now return to buffer/string position where we were asked
7184 to get the next display element, and produce that. */
7185 bidi_move_to_visually_next (&it->bidi_it);
7186 }
7187 while (it->bidi_it.bytepos != orig_bytepos
7188 && it->bidi_it.charpos < eob);
7189 }
7190
7191 /* Adjust IT's position information to where we ended up. */
7192 if (STRINGP (it->string))
7193 {
7194 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7195 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7196 }
7197 else
7198 {
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 }
7202
7203 if (STRINGP (it->string) || !it->s)
7204 {
7205 ptrdiff_t stop, charpos, bytepos;
7206
7207 if (STRINGP (it->string))
7208 {
7209 xassert (!it->s);
7210 stop = SCHARS (it->string);
7211 if (stop > it->end_charpos)
7212 stop = it->end_charpos;
7213 charpos = IT_STRING_CHARPOS (*it);
7214 bytepos = IT_STRING_BYTEPOS (*it);
7215 }
7216 else
7217 {
7218 stop = it->end_charpos;
7219 charpos = IT_CHARPOS (*it);
7220 bytepos = IT_BYTEPOS (*it);
7221 }
7222 if (it->bidi_it.scan_dir < 0)
7223 stop = -1;
7224 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7225 it->string);
7226 }
7227 }
7228
7229 /* Load IT with the next display element from Lisp string IT->string.
7230 IT->current.string_pos is the current position within the string.
7231 If IT->current.overlay_string_index >= 0, the Lisp string is an
7232 overlay string. */
7233
7234 static int
7235 next_element_from_string (struct it *it)
7236 {
7237 struct text_pos position;
7238
7239 xassert (STRINGP (it->string));
7240 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7241 xassert (IT_STRING_CHARPOS (*it) >= 0);
7242 position = it->current.string_pos;
7243
7244 /* With bidi reordering, the character to display might not be the
7245 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7246 that we were reseat()ed to a new string, whose paragraph
7247 direction is not known. */
7248 if (it->bidi_p && it->bidi_it.first_elt)
7249 {
7250 get_visually_first_element (it);
7251 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7252 }
7253
7254 /* Time to check for invisible text? */
7255 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7256 {
7257 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7258 {
7259 if (!(!it->bidi_p
7260 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7261 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7262 {
7263 /* With bidi non-linear iteration, we could find
7264 ourselves far beyond the last computed stop_charpos,
7265 with several other stop positions in between that we
7266 missed. Scan them all now, in buffer's logical
7267 order, until we find and handle the last stop_charpos
7268 that precedes our current position. */
7269 handle_stop_backwards (it, it->stop_charpos);
7270 return GET_NEXT_DISPLAY_ELEMENT (it);
7271 }
7272 else
7273 {
7274 if (it->bidi_p)
7275 {
7276 /* Take note of the stop position we just moved
7277 across, for when we will move back across it. */
7278 it->prev_stop = it->stop_charpos;
7279 /* If we are at base paragraph embedding level, take
7280 note of the last stop position seen at this
7281 level. */
7282 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7283 it->base_level_stop = it->stop_charpos;
7284 }
7285 handle_stop (it);
7286
7287 /* Since a handler may have changed IT->method, we must
7288 recurse here. */
7289 return GET_NEXT_DISPLAY_ELEMENT (it);
7290 }
7291 }
7292 else if (it->bidi_p
7293 /* If we are before prev_stop, we may have overstepped
7294 on our way backwards a stop_pos, and if so, we need
7295 to handle that stop_pos. */
7296 && IT_STRING_CHARPOS (*it) < it->prev_stop
7297 /* We can sometimes back up for reasons that have nothing
7298 to do with bidi reordering. E.g., compositions. The
7299 code below is only needed when we are above the base
7300 embedding level, so test for that explicitly. */
7301 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7302 {
7303 /* If we lost track of base_level_stop, we have no better
7304 place for handle_stop_backwards to start from than string
7305 beginning. This happens, e.g., when we were reseated to
7306 the previous screenful of text by vertical-motion. */
7307 if (it->base_level_stop <= 0
7308 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7309 it->base_level_stop = 0;
7310 handle_stop_backwards (it, it->base_level_stop);
7311 return GET_NEXT_DISPLAY_ELEMENT (it);
7312 }
7313 }
7314
7315 if (it->current.overlay_string_index >= 0)
7316 {
7317 /* Get the next character from an overlay string. In overlay
7318 strings, There is no field width or padding with spaces to
7319 do. */
7320 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7321 {
7322 it->what = IT_EOB;
7323 return 0;
7324 }
7325 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7326 IT_STRING_BYTEPOS (*it),
7327 it->bidi_it.scan_dir < 0
7328 ? -1
7329 : SCHARS (it->string))
7330 && next_element_from_composition (it))
7331 {
7332 return 1;
7333 }
7334 else if (STRING_MULTIBYTE (it->string))
7335 {
7336 const unsigned char *s = (SDATA (it->string)
7337 + IT_STRING_BYTEPOS (*it));
7338 it->c = string_char_and_length (s, &it->len);
7339 }
7340 else
7341 {
7342 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7343 it->len = 1;
7344 }
7345 }
7346 else
7347 {
7348 /* Get the next character from a Lisp string that is not an
7349 overlay string. Such strings come from the mode line, for
7350 example. We may have to pad with spaces, or truncate the
7351 string. See also next_element_from_c_string. */
7352 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7353 {
7354 it->what = IT_EOB;
7355 return 0;
7356 }
7357 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7358 {
7359 /* Pad with spaces. */
7360 it->c = ' ', it->len = 1;
7361 CHARPOS (position) = BYTEPOS (position) = -1;
7362 }
7363 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7364 IT_STRING_BYTEPOS (*it),
7365 it->bidi_it.scan_dir < 0
7366 ? -1
7367 : it->string_nchars)
7368 && next_element_from_composition (it))
7369 {
7370 return 1;
7371 }
7372 else if (STRING_MULTIBYTE (it->string))
7373 {
7374 const unsigned char *s = (SDATA (it->string)
7375 + IT_STRING_BYTEPOS (*it));
7376 it->c = string_char_and_length (s, &it->len);
7377 }
7378 else
7379 {
7380 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7381 it->len = 1;
7382 }
7383 }
7384
7385 /* Record what we have and where it came from. */
7386 it->what = IT_CHARACTER;
7387 it->object = it->string;
7388 it->position = position;
7389 return 1;
7390 }
7391
7392
7393 /* Load IT with next display element from C string IT->s.
7394 IT->string_nchars is the maximum number of characters to return
7395 from the string. IT->end_charpos may be greater than
7396 IT->string_nchars when this function is called, in which case we
7397 may have to return padding spaces. Value is zero if end of string
7398 reached, including padding spaces. */
7399
7400 static int
7401 next_element_from_c_string (struct it *it)
7402 {
7403 int success_p = 1;
7404
7405 xassert (it->s);
7406 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7407 it->what = IT_CHARACTER;
7408 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7409 it->object = Qnil;
7410
7411 /* With bidi reordering, the character to display might not be the
7412 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7413 we were reseated to a new string, whose paragraph direction is
7414 not known. */
7415 if (it->bidi_p && it->bidi_it.first_elt)
7416 get_visually_first_element (it);
7417
7418 /* IT's position can be greater than IT->string_nchars in case a
7419 field width or precision has been specified when the iterator was
7420 initialized. */
7421 if (IT_CHARPOS (*it) >= it->end_charpos)
7422 {
7423 /* End of the game. */
7424 it->what = IT_EOB;
7425 success_p = 0;
7426 }
7427 else if (IT_CHARPOS (*it) >= it->string_nchars)
7428 {
7429 /* Pad with spaces. */
7430 it->c = ' ', it->len = 1;
7431 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7432 }
7433 else if (it->multibyte_p)
7434 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7435 else
7436 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7437
7438 return success_p;
7439 }
7440
7441
7442 /* Set up IT to return characters from an ellipsis, if appropriate.
7443 The definition of the ellipsis glyphs may come from a display table
7444 entry. This function fills IT with the first glyph from the
7445 ellipsis if an ellipsis is to be displayed. */
7446
7447 static int
7448 next_element_from_ellipsis (struct it *it)
7449 {
7450 if (it->selective_display_ellipsis_p)
7451 setup_for_ellipsis (it, it->len);
7452 else
7453 {
7454 /* The face at the current position may be different from the
7455 face we find after the invisible text. Remember what it
7456 was in IT->saved_face_id, and signal that it's there by
7457 setting face_before_selective_p. */
7458 it->saved_face_id = it->face_id;
7459 it->method = GET_FROM_BUFFER;
7460 it->object = it->w->buffer;
7461 reseat_at_next_visible_line_start (it, 1);
7462 it->face_before_selective_p = 1;
7463 }
7464
7465 return GET_NEXT_DISPLAY_ELEMENT (it);
7466 }
7467
7468
7469 /* Deliver an image display element. The iterator IT is already
7470 filled with image information (done in handle_display_prop). Value
7471 is always 1. */
7472
7473
7474 static int
7475 next_element_from_image (struct it *it)
7476 {
7477 it->what = IT_IMAGE;
7478 it->ignore_overlay_strings_at_pos_p = 0;
7479 return 1;
7480 }
7481
7482
7483 /* Fill iterator IT with next display element from a stretch glyph
7484 property. IT->object is the value of the text property. Value is
7485 always 1. */
7486
7487 static int
7488 next_element_from_stretch (struct it *it)
7489 {
7490 it->what = IT_STRETCH;
7491 return 1;
7492 }
7493
7494 /* Scan backwards from IT's current position until we find a stop
7495 position, or until BEGV. This is called when we find ourself
7496 before both the last known prev_stop and base_level_stop while
7497 reordering bidirectional text. */
7498
7499 static void
7500 compute_stop_pos_backwards (struct it *it)
7501 {
7502 const int SCAN_BACK_LIMIT = 1000;
7503 struct text_pos pos;
7504 struct display_pos save_current = it->current;
7505 struct text_pos save_position = it->position;
7506 ptrdiff_t charpos = IT_CHARPOS (*it);
7507 ptrdiff_t where_we_are = charpos;
7508 ptrdiff_t save_stop_pos = it->stop_charpos;
7509 ptrdiff_t save_end_pos = it->end_charpos;
7510
7511 xassert (NILP (it->string) && !it->s);
7512 xassert (it->bidi_p);
7513 it->bidi_p = 0;
7514 do
7515 {
7516 it->end_charpos = min (charpos + 1, ZV);
7517 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7518 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7519 reseat_1 (it, pos, 0);
7520 compute_stop_pos (it);
7521 /* We must advance forward, right? */
7522 if (it->stop_charpos <= charpos)
7523 abort ();
7524 }
7525 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7526
7527 if (it->stop_charpos <= where_we_are)
7528 it->prev_stop = it->stop_charpos;
7529 else
7530 it->prev_stop = BEGV;
7531 it->bidi_p = 1;
7532 it->current = save_current;
7533 it->position = save_position;
7534 it->stop_charpos = save_stop_pos;
7535 it->end_charpos = save_end_pos;
7536 }
7537
7538 /* Scan forward from CHARPOS in the current buffer/string, until we
7539 find a stop position > current IT's position. Then handle the stop
7540 position before that. This is called when we bump into a stop
7541 position while reordering bidirectional text. CHARPOS should be
7542 the last previously processed stop_pos (or BEGV/0, if none were
7543 processed yet) whose position is less that IT's current
7544 position. */
7545
7546 static void
7547 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7548 {
7549 int bufp = !STRINGP (it->string);
7550 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7551 struct display_pos save_current = it->current;
7552 struct text_pos save_position = it->position;
7553 struct text_pos pos1;
7554 ptrdiff_t next_stop;
7555
7556 /* Scan in strict logical order. */
7557 xassert (it->bidi_p);
7558 it->bidi_p = 0;
7559 do
7560 {
7561 it->prev_stop = charpos;
7562 if (bufp)
7563 {
7564 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7565 reseat_1 (it, pos1, 0);
7566 }
7567 else
7568 it->current.string_pos = string_pos (charpos, it->string);
7569 compute_stop_pos (it);
7570 /* We must advance forward, right? */
7571 if (it->stop_charpos <= it->prev_stop)
7572 abort ();
7573 charpos = it->stop_charpos;
7574 }
7575 while (charpos <= where_we_are);
7576
7577 it->bidi_p = 1;
7578 it->current = save_current;
7579 it->position = save_position;
7580 next_stop = it->stop_charpos;
7581 it->stop_charpos = it->prev_stop;
7582 handle_stop (it);
7583 it->stop_charpos = next_stop;
7584 }
7585
7586 /* Load IT with the next display element from current_buffer. Value
7587 is zero if end of buffer reached. IT->stop_charpos is the next
7588 position at which to stop and check for text properties or buffer
7589 end. */
7590
7591 static int
7592 next_element_from_buffer (struct it *it)
7593 {
7594 int success_p = 1;
7595
7596 xassert (IT_CHARPOS (*it) >= BEGV);
7597 xassert (NILP (it->string) && !it->s);
7598 xassert (!it->bidi_p
7599 || (EQ (it->bidi_it.string.lstring, Qnil)
7600 && it->bidi_it.string.s == NULL));
7601
7602 /* With bidi reordering, the character to display might not be the
7603 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7604 we were reseat()ed to a new buffer position, which is potentially
7605 a different paragraph. */
7606 if (it->bidi_p && it->bidi_it.first_elt)
7607 {
7608 get_visually_first_element (it);
7609 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7610 }
7611
7612 if (IT_CHARPOS (*it) >= it->stop_charpos)
7613 {
7614 if (IT_CHARPOS (*it) >= it->end_charpos)
7615 {
7616 int overlay_strings_follow_p;
7617
7618 /* End of the game, except when overlay strings follow that
7619 haven't been returned yet. */
7620 if (it->overlay_strings_at_end_processed_p)
7621 overlay_strings_follow_p = 0;
7622 else
7623 {
7624 it->overlay_strings_at_end_processed_p = 1;
7625 overlay_strings_follow_p = get_overlay_strings (it, 0);
7626 }
7627
7628 if (overlay_strings_follow_p)
7629 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7630 else
7631 {
7632 it->what = IT_EOB;
7633 it->position = it->current.pos;
7634 success_p = 0;
7635 }
7636 }
7637 else if (!(!it->bidi_p
7638 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7639 || IT_CHARPOS (*it) == it->stop_charpos))
7640 {
7641 /* With bidi non-linear iteration, we could find ourselves
7642 far beyond the last computed stop_charpos, with several
7643 other stop positions in between that we missed. Scan
7644 them all now, in buffer's logical order, until we find
7645 and handle the last stop_charpos that precedes our
7646 current position. */
7647 handle_stop_backwards (it, it->stop_charpos);
7648 return GET_NEXT_DISPLAY_ELEMENT (it);
7649 }
7650 else
7651 {
7652 if (it->bidi_p)
7653 {
7654 /* Take note of the stop position we just moved across,
7655 for when we will move back across it. */
7656 it->prev_stop = it->stop_charpos;
7657 /* If we are at base paragraph embedding level, take
7658 note of the last stop position seen at this
7659 level. */
7660 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7661 it->base_level_stop = it->stop_charpos;
7662 }
7663 handle_stop (it);
7664 return GET_NEXT_DISPLAY_ELEMENT (it);
7665 }
7666 }
7667 else if (it->bidi_p
7668 /* If we are before prev_stop, we may have overstepped on
7669 our way backwards a stop_pos, and if so, we need to
7670 handle that stop_pos. */
7671 && IT_CHARPOS (*it) < it->prev_stop
7672 /* We can sometimes back up for reasons that have nothing
7673 to do with bidi reordering. E.g., compositions. The
7674 code below is only needed when we are above the base
7675 embedding level, so test for that explicitly. */
7676 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7677 {
7678 if (it->base_level_stop <= 0
7679 || IT_CHARPOS (*it) < it->base_level_stop)
7680 {
7681 /* If we lost track of base_level_stop, we need to find
7682 prev_stop by looking backwards. This happens, e.g., when
7683 we were reseated to the previous screenful of text by
7684 vertical-motion. */
7685 it->base_level_stop = BEGV;
7686 compute_stop_pos_backwards (it);
7687 handle_stop_backwards (it, it->prev_stop);
7688 }
7689 else
7690 handle_stop_backwards (it, it->base_level_stop);
7691 return GET_NEXT_DISPLAY_ELEMENT (it);
7692 }
7693 else
7694 {
7695 /* No face changes, overlays etc. in sight, so just return a
7696 character from current_buffer. */
7697 unsigned char *p;
7698 ptrdiff_t stop;
7699
7700 /* Maybe run the redisplay end trigger hook. Performance note:
7701 This doesn't seem to cost measurable time. */
7702 if (it->redisplay_end_trigger_charpos
7703 && it->glyph_row
7704 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7705 run_redisplay_end_trigger_hook (it);
7706
7707 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7708 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7709 stop)
7710 && next_element_from_composition (it))
7711 {
7712 return 1;
7713 }
7714
7715 /* Get the next character, maybe multibyte. */
7716 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7717 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7718 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7719 else
7720 it->c = *p, it->len = 1;
7721
7722 /* Record what we have and where it came from. */
7723 it->what = IT_CHARACTER;
7724 it->object = it->w->buffer;
7725 it->position = it->current.pos;
7726
7727 /* Normally we return the character found above, except when we
7728 really want to return an ellipsis for selective display. */
7729 if (it->selective)
7730 {
7731 if (it->c == '\n')
7732 {
7733 /* A value of selective > 0 means hide lines indented more
7734 than that number of columns. */
7735 if (it->selective > 0
7736 && IT_CHARPOS (*it) + 1 < ZV
7737 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7738 IT_BYTEPOS (*it) + 1,
7739 it->selective))
7740 {
7741 success_p = next_element_from_ellipsis (it);
7742 it->dpvec_char_len = -1;
7743 }
7744 }
7745 else if (it->c == '\r' && it->selective == -1)
7746 {
7747 /* A value of selective == -1 means that everything from the
7748 CR to the end of the line is invisible, with maybe an
7749 ellipsis displayed for it. */
7750 success_p = next_element_from_ellipsis (it);
7751 it->dpvec_char_len = -1;
7752 }
7753 }
7754 }
7755
7756 /* Value is zero if end of buffer reached. */
7757 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7758 return success_p;
7759 }
7760
7761
7762 /* Run the redisplay end trigger hook for IT. */
7763
7764 static void
7765 run_redisplay_end_trigger_hook (struct it *it)
7766 {
7767 Lisp_Object args[3];
7768
7769 /* IT->glyph_row should be non-null, i.e. we should be actually
7770 displaying something, or otherwise we should not run the hook. */
7771 xassert (it->glyph_row);
7772
7773 /* Set up hook arguments. */
7774 args[0] = Qredisplay_end_trigger_functions;
7775 args[1] = it->window;
7776 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7777 it->redisplay_end_trigger_charpos = 0;
7778
7779 /* Since we are *trying* to run these functions, don't try to run
7780 them again, even if they get an error. */
7781 it->w->redisplay_end_trigger = Qnil;
7782 Frun_hook_with_args (3, args);
7783
7784 /* Notice if it changed the face of the character we are on. */
7785 handle_face_prop (it);
7786 }
7787
7788
7789 /* Deliver a composition display element. Unlike the other
7790 next_element_from_XXX, this function is not registered in the array
7791 get_next_element[]. It is called from next_element_from_buffer and
7792 next_element_from_string when necessary. */
7793
7794 static int
7795 next_element_from_composition (struct it *it)
7796 {
7797 it->what = IT_COMPOSITION;
7798 it->len = it->cmp_it.nbytes;
7799 if (STRINGP (it->string))
7800 {
7801 if (it->c < 0)
7802 {
7803 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7804 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7805 return 0;
7806 }
7807 it->position = it->current.string_pos;
7808 it->object = it->string;
7809 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7810 IT_STRING_BYTEPOS (*it), it->string);
7811 }
7812 else
7813 {
7814 if (it->c < 0)
7815 {
7816 IT_CHARPOS (*it) += it->cmp_it.nchars;
7817 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7818 if (it->bidi_p)
7819 {
7820 if (it->bidi_it.new_paragraph)
7821 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7822 /* Resync the bidi iterator with IT's new position.
7823 FIXME: this doesn't support bidirectional text. */
7824 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7825 bidi_move_to_visually_next (&it->bidi_it);
7826 }
7827 return 0;
7828 }
7829 it->position = it->current.pos;
7830 it->object = it->w->buffer;
7831 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7832 IT_BYTEPOS (*it), Qnil);
7833 }
7834 return 1;
7835 }
7836
7837
7838 \f
7839 /***********************************************************************
7840 Moving an iterator without producing glyphs
7841 ***********************************************************************/
7842
7843 /* Check if iterator is at a position corresponding to a valid buffer
7844 position after some move_it_ call. */
7845
7846 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7847 ((it)->method == GET_FROM_STRING \
7848 ? IT_STRING_CHARPOS (*it) == 0 \
7849 : 1)
7850
7851
7852 /* Move iterator IT to a specified buffer or X position within one
7853 line on the display without producing glyphs.
7854
7855 OP should be a bit mask including some or all of these bits:
7856 MOVE_TO_X: Stop upon reaching x-position TO_X.
7857 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7858 Regardless of OP's value, stop upon reaching the end of the display line.
7859
7860 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7861 This means, in particular, that TO_X includes window's horizontal
7862 scroll amount.
7863
7864 The return value has several possible values that
7865 say what condition caused the scan to stop:
7866
7867 MOVE_POS_MATCH_OR_ZV
7868 - when TO_POS or ZV was reached.
7869
7870 MOVE_X_REACHED
7871 -when TO_X was reached before TO_POS or ZV were reached.
7872
7873 MOVE_LINE_CONTINUED
7874 - when we reached the end of the display area and the line must
7875 be continued.
7876
7877 MOVE_LINE_TRUNCATED
7878 - when we reached the end of the display area and the line is
7879 truncated.
7880
7881 MOVE_NEWLINE_OR_CR
7882 - when we stopped at a line end, i.e. a newline or a CR and selective
7883 display is on. */
7884
7885 static enum move_it_result
7886 move_it_in_display_line_to (struct it *it,
7887 ptrdiff_t to_charpos, int to_x,
7888 enum move_operation_enum op)
7889 {
7890 enum move_it_result result = MOVE_UNDEFINED;
7891 struct glyph_row *saved_glyph_row;
7892 struct it wrap_it, atpos_it, atx_it, ppos_it;
7893 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7894 void *ppos_data = NULL;
7895 int may_wrap = 0;
7896 enum it_method prev_method = it->method;
7897 ptrdiff_t prev_pos = IT_CHARPOS (*it);
7898 int saw_smaller_pos = prev_pos < to_charpos;
7899
7900 /* Don't produce glyphs in produce_glyphs. */
7901 saved_glyph_row = it->glyph_row;
7902 it->glyph_row = NULL;
7903
7904 /* Use wrap_it to save a copy of IT wherever a word wrap could
7905 occur. Use atpos_it to save a copy of IT at the desired buffer
7906 position, if found, so that we can scan ahead and check if the
7907 word later overshoots the window edge. Use atx_it similarly, for
7908 pixel positions. */
7909 wrap_it.sp = -1;
7910 atpos_it.sp = -1;
7911 atx_it.sp = -1;
7912
7913 /* Use ppos_it under bidi reordering to save a copy of IT for the
7914 position > CHARPOS that is the closest to CHARPOS. We restore
7915 that position in IT when we have scanned the entire display line
7916 without finding a match for CHARPOS and all the character
7917 positions are greater than CHARPOS. */
7918 if (it->bidi_p)
7919 {
7920 SAVE_IT (ppos_it, *it, ppos_data);
7921 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7922 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7923 SAVE_IT (ppos_it, *it, ppos_data);
7924 }
7925
7926 #define BUFFER_POS_REACHED_P() \
7927 ((op & MOVE_TO_POS) != 0 \
7928 && BUFFERP (it->object) \
7929 && (IT_CHARPOS (*it) == to_charpos \
7930 || ((!it->bidi_p \
7931 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7932 && IT_CHARPOS (*it) > to_charpos) \
7933 || (it->what == IT_COMPOSITION \
7934 && ((IT_CHARPOS (*it) > to_charpos \
7935 && to_charpos >= it->cmp_it.charpos) \
7936 || (IT_CHARPOS (*it) < to_charpos \
7937 && to_charpos <= it->cmp_it.charpos)))) \
7938 && (it->method == GET_FROM_BUFFER \
7939 || (it->method == GET_FROM_DISPLAY_VECTOR \
7940 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7941
7942 /* If there's a line-/wrap-prefix, handle it. */
7943 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7944 && it->current_y < it->last_visible_y)
7945 handle_line_prefix (it);
7946
7947 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7948 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7949
7950 while (1)
7951 {
7952 int x, i, ascent = 0, descent = 0;
7953
7954 /* Utility macro to reset an iterator with x, ascent, and descent. */
7955 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7956 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7957 (IT)->max_descent = descent)
7958
7959 /* Stop if we move beyond TO_CHARPOS (after an image or a
7960 display string or stretch glyph). */
7961 if ((op & MOVE_TO_POS) != 0
7962 && BUFFERP (it->object)
7963 && it->method == GET_FROM_BUFFER
7964 && (((!it->bidi_p
7965 /* When the iterator is at base embedding level, we
7966 are guaranteed that characters are delivered for
7967 display in strictly increasing order of their
7968 buffer positions. */
7969 || BIDI_AT_BASE_LEVEL (it->bidi_it))
7970 && IT_CHARPOS (*it) > to_charpos)
7971 || (it->bidi_p
7972 && (prev_method == GET_FROM_IMAGE
7973 || prev_method == GET_FROM_STRETCH
7974 || prev_method == GET_FROM_STRING)
7975 /* Passed TO_CHARPOS from left to right. */
7976 && ((prev_pos < to_charpos
7977 && IT_CHARPOS (*it) > to_charpos)
7978 /* Passed TO_CHARPOS from right to left. */
7979 || (prev_pos > to_charpos
7980 && IT_CHARPOS (*it) < to_charpos)))))
7981 {
7982 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7983 {
7984 result = MOVE_POS_MATCH_OR_ZV;
7985 break;
7986 }
7987 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7988 /* If wrap_it is valid, the current position might be in a
7989 word that is wrapped. So, save the iterator in
7990 atpos_it and continue to see if wrapping happens. */
7991 SAVE_IT (atpos_it, *it, atpos_data);
7992 }
7993
7994 /* Stop when ZV reached.
7995 We used to stop here when TO_CHARPOS reached as well, but that is
7996 too soon if this glyph does not fit on this line. So we handle it
7997 explicitly below. */
7998 if (!get_next_display_element (it))
7999 {
8000 result = MOVE_POS_MATCH_OR_ZV;
8001 break;
8002 }
8003
8004 if (it->line_wrap == TRUNCATE)
8005 {
8006 if (BUFFER_POS_REACHED_P ())
8007 {
8008 result = MOVE_POS_MATCH_OR_ZV;
8009 break;
8010 }
8011 }
8012 else
8013 {
8014 if (it->line_wrap == WORD_WRAP)
8015 {
8016 if (IT_DISPLAYING_WHITESPACE (it))
8017 may_wrap = 1;
8018 else if (may_wrap)
8019 {
8020 /* We have reached a glyph that follows one or more
8021 whitespace characters. If the position is
8022 already found, we are done. */
8023 if (atpos_it.sp >= 0)
8024 {
8025 RESTORE_IT (it, &atpos_it, atpos_data);
8026 result = MOVE_POS_MATCH_OR_ZV;
8027 goto done;
8028 }
8029 if (atx_it.sp >= 0)
8030 {
8031 RESTORE_IT (it, &atx_it, atx_data);
8032 result = MOVE_X_REACHED;
8033 goto done;
8034 }
8035 /* Otherwise, we can wrap here. */
8036 SAVE_IT (wrap_it, *it, wrap_data);
8037 may_wrap = 0;
8038 }
8039 }
8040 }
8041
8042 /* Remember the line height for the current line, in case
8043 the next element doesn't fit on the line. */
8044 ascent = it->max_ascent;
8045 descent = it->max_descent;
8046
8047 /* The call to produce_glyphs will get the metrics of the
8048 display element IT is loaded with. Record the x-position
8049 before this display element, in case it doesn't fit on the
8050 line. */
8051 x = it->current_x;
8052
8053 PRODUCE_GLYPHS (it);
8054
8055 if (it->area != TEXT_AREA)
8056 {
8057 prev_method = it->method;
8058 if (it->method == GET_FROM_BUFFER)
8059 prev_pos = IT_CHARPOS (*it);
8060 set_iterator_to_next (it, 1);
8061 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8062 SET_TEXT_POS (this_line_min_pos,
8063 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8064 if (it->bidi_p
8065 && (op & MOVE_TO_POS)
8066 && IT_CHARPOS (*it) > to_charpos
8067 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8068 SAVE_IT (ppos_it, *it, ppos_data);
8069 continue;
8070 }
8071
8072 /* The number of glyphs we get back in IT->nglyphs will normally
8073 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8074 character on a terminal frame, or (iii) a line end. For the
8075 second case, IT->nglyphs - 1 padding glyphs will be present.
8076 (On X frames, there is only one glyph produced for a
8077 composite character.)
8078
8079 The behavior implemented below means, for continuation lines,
8080 that as many spaces of a TAB as fit on the current line are
8081 displayed there. For terminal frames, as many glyphs of a
8082 multi-glyph character are displayed in the current line, too.
8083 This is what the old redisplay code did, and we keep it that
8084 way. Under X, the whole shape of a complex character must
8085 fit on the line or it will be completely displayed in the
8086 next line.
8087
8088 Note that both for tabs and padding glyphs, all glyphs have
8089 the same width. */
8090 if (it->nglyphs)
8091 {
8092 /* More than one glyph or glyph doesn't fit on line. All
8093 glyphs have the same width. */
8094 int single_glyph_width = it->pixel_width / it->nglyphs;
8095 int new_x;
8096 int x_before_this_char = x;
8097 int hpos_before_this_char = it->hpos;
8098
8099 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8100 {
8101 new_x = x + single_glyph_width;
8102
8103 /* We want to leave anything reaching TO_X to the caller. */
8104 if ((op & MOVE_TO_X) && new_x > to_x)
8105 {
8106 if (BUFFER_POS_REACHED_P ())
8107 {
8108 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8109 goto buffer_pos_reached;
8110 if (atpos_it.sp < 0)
8111 {
8112 SAVE_IT (atpos_it, *it, atpos_data);
8113 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8114 }
8115 }
8116 else
8117 {
8118 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8119 {
8120 it->current_x = x;
8121 result = MOVE_X_REACHED;
8122 break;
8123 }
8124 if (atx_it.sp < 0)
8125 {
8126 SAVE_IT (atx_it, *it, atx_data);
8127 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8128 }
8129 }
8130 }
8131
8132 if (/* Lines are continued. */
8133 it->line_wrap != TRUNCATE
8134 && (/* And glyph doesn't fit on the line. */
8135 new_x > it->last_visible_x
8136 /* Or it fits exactly and we're on a window
8137 system frame. */
8138 || (new_x == it->last_visible_x
8139 && FRAME_WINDOW_P (it->f))))
8140 {
8141 if (/* IT->hpos == 0 means the very first glyph
8142 doesn't fit on the line, e.g. a wide image. */
8143 it->hpos == 0
8144 || (new_x == it->last_visible_x
8145 && FRAME_WINDOW_P (it->f)))
8146 {
8147 ++it->hpos;
8148 it->current_x = new_x;
8149
8150 /* The character's last glyph just barely fits
8151 in this row. */
8152 if (i == it->nglyphs - 1)
8153 {
8154 /* If this is the destination position,
8155 return a position *before* it in this row,
8156 now that we know it fits in this row. */
8157 if (BUFFER_POS_REACHED_P ())
8158 {
8159 if (it->line_wrap != WORD_WRAP
8160 || wrap_it.sp < 0)
8161 {
8162 it->hpos = hpos_before_this_char;
8163 it->current_x = x_before_this_char;
8164 result = MOVE_POS_MATCH_OR_ZV;
8165 break;
8166 }
8167 if (it->line_wrap == WORD_WRAP
8168 && atpos_it.sp < 0)
8169 {
8170 SAVE_IT (atpos_it, *it, atpos_data);
8171 atpos_it.current_x = x_before_this_char;
8172 atpos_it.hpos = hpos_before_this_char;
8173 }
8174 }
8175
8176 prev_method = it->method;
8177 if (it->method == GET_FROM_BUFFER)
8178 prev_pos = IT_CHARPOS (*it);
8179 set_iterator_to_next (it, 1);
8180 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8181 SET_TEXT_POS (this_line_min_pos,
8182 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8183 /* On graphical terminals, newlines may
8184 "overflow" into the fringe if
8185 overflow-newline-into-fringe is non-nil.
8186 On text-only terminals, newlines may
8187 overflow into the last glyph on the
8188 display line.*/
8189 if (!FRAME_WINDOW_P (it->f)
8190 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8191 {
8192 if (!get_next_display_element (it))
8193 {
8194 result = MOVE_POS_MATCH_OR_ZV;
8195 break;
8196 }
8197 if (BUFFER_POS_REACHED_P ())
8198 {
8199 if (ITERATOR_AT_END_OF_LINE_P (it))
8200 result = MOVE_POS_MATCH_OR_ZV;
8201 else
8202 result = MOVE_LINE_CONTINUED;
8203 break;
8204 }
8205 if (ITERATOR_AT_END_OF_LINE_P (it))
8206 {
8207 result = MOVE_NEWLINE_OR_CR;
8208 break;
8209 }
8210 }
8211 }
8212 }
8213 else
8214 IT_RESET_X_ASCENT_DESCENT (it);
8215
8216 if (wrap_it.sp >= 0)
8217 {
8218 RESTORE_IT (it, &wrap_it, wrap_data);
8219 atpos_it.sp = -1;
8220 atx_it.sp = -1;
8221 }
8222
8223 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8224 IT_CHARPOS (*it)));
8225 result = MOVE_LINE_CONTINUED;
8226 break;
8227 }
8228
8229 if (BUFFER_POS_REACHED_P ())
8230 {
8231 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8232 goto buffer_pos_reached;
8233 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8234 {
8235 SAVE_IT (atpos_it, *it, atpos_data);
8236 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8237 }
8238 }
8239
8240 if (new_x > it->first_visible_x)
8241 {
8242 /* Glyph is visible. Increment number of glyphs that
8243 would be displayed. */
8244 ++it->hpos;
8245 }
8246 }
8247
8248 if (result != MOVE_UNDEFINED)
8249 break;
8250 }
8251 else if (BUFFER_POS_REACHED_P ())
8252 {
8253 buffer_pos_reached:
8254 IT_RESET_X_ASCENT_DESCENT (it);
8255 result = MOVE_POS_MATCH_OR_ZV;
8256 break;
8257 }
8258 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8259 {
8260 /* Stop when TO_X specified and reached. This check is
8261 necessary here because of lines consisting of a line end,
8262 only. The line end will not produce any glyphs and we
8263 would never get MOVE_X_REACHED. */
8264 xassert (it->nglyphs == 0);
8265 result = MOVE_X_REACHED;
8266 break;
8267 }
8268
8269 /* Is this a line end? If yes, we're done. */
8270 if (ITERATOR_AT_END_OF_LINE_P (it))
8271 {
8272 /* If we are past TO_CHARPOS, but never saw any character
8273 positions smaller than TO_CHARPOS, return
8274 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8275 did. */
8276 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8277 {
8278 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8279 {
8280 if (IT_CHARPOS (ppos_it) < ZV)
8281 {
8282 RESTORE_IT (it, &ppos_it, ppos_data);
8283 result = MOVE_POS_MATCH_OR_ZV;
8284 }
8285 else
8286 goto buffer_pos_reached;
8287 }
8288 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8289 && IT_CHARPOS (*it) > to_charpos)
8290 goto buffer_pos_reached;
8291 else
8292 result = MOVE_NEWLINE_OR_CR;
8293 }
8294 else
8295 result = MOVE_NEWLINE_OR_CR;
8296 break;
8297 }
8298
8299 prev_method = it->method;
8300 if (it->method == GET_FROM_BUFFER)
8301 prev_pos = IT_CHARPOS (*it);
8302 /* The current display element has been consumed. Advance
8303 to the next. */
8304 set_iterator_to_next (it, 1);
8305 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8306 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8307 if (IT_CHARPOS (*it) < to_charpos)
8308 saw_smaller_pos = 1;
8309 if (it->bidi_p
8310 && (op & MOVE_TO_POS)
8311 && IT_CHARPOS (*it) >= to_charpos
8312 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8313 SAVE_IT (ppos_it, *it, ppos_data);
8314
8315 /* Stop if lines are truncated and IT's current x-position is
8316 past the right edge of the window now. */
8317 if (it->line_wrap == TRUNCATE
8318 && it->current_x >= it->last_visible_x)
8319 {
8320 if (!FRAME_WINDOW_P (it->f)
8321 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8322 {
8323 int at_eob_p = 0;
8324
8325 if ((at_eob_p = !get_next_display_element (it))
8326 || BUFFER_POS_REACHED_P ()
8327 /* If we are past TO_CHARPOS, but never saw any
8328 character positions smaller than TO_CHARPOS,
8329 return MOVE_POS_MATCH_OR_ZV, like the
8330 unidirectional display did. */
8331 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8332 && !saw_smaller_pos
8333 && IT_CHARPOS (*it) > to_charpos))
8334 {
8335 if (it->bidi_p
8336 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8337 RESTORE_IT (it, &ppos_it, ppos_data);
8338 result = MOVE_POS_MATCH_OR_ZV;
8339 break;
8340 }
8341 if (ITERATOR_AT_END_OF_LINE_P (it))
8342 {
8343 result = MOVE_NEWLINE_OR_CR;
8344 break;
8345 }
8346 }
8347 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8348 && !saw_smaller_pos
8349 && IT_CHARPOS (*it) > to_charpos)
8350 {
8351 if (IT_CHARPOS (ppos_it) < ZV)
8352 RESTORE_IT (it, &ppos_it, ppos_data);
8353 result = MOVE_POS_MATCH_OR_ZV;
8354 break;
8355 }
8356 result = MOVE_LINE_TRUNCATED;
8357 break;
8358 }
8359 #undef IT_RESET_X_ASCENT_DESCENT
8360 }
8361
8362 #undef BUFFER_POS_REACHED_P
8363
8364 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8365 restore the saved iterator. */
8366 if (atpos_it.sp >= 0)
8367 RESTORE_IT (it, &atpos_it, atpos_data);
8368 else if (atx_it.sp >= 0)
8369 RESTORE_IT (it, &atx_it, atx_data);
8370
8371 done:
8372
8373 if (atpos_data)
8374 bidi_unshelve_cache (atpos_data, 1);
8375 if (atx_data)
8376 bidi_unshelve_cache (atx_data, 1);
8377 if (wrap_data)
8378 bidi_unshelve_cache (wrap_data, 1);
8379 if (ppos_data)
8380 bidi_unshelve_cache (ppos_data, 1);
8381
8382 /* Restore the iterator settings altered at the beginning of this
8383 function. */
8384 it->glyph_row = saved_glyph_row;
8385 return result;
8386 }
8387
8388 /* For external use. */
8389 void
8390 move_it_in_display_line (struct it *it,
8391 ptrdiff_t to_charpos, int to_x,
8392 enum move_operation_enum op)
8393 {
8394 if (it->line_wrap == WORD_WRAP
8395 && (op & MOVE_TO_X))
8396 {
8397 struct it save_it;
8398 void *save_data = NULL;
8399 int skip;
8400
8401 SAVE_IT (save_it, *it, save_data);
8402 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8403 /* When word-wrap is on, TO_X may lie past the end
8404 of a wrapped line. Then it->current is the
8405 character on the next line, so backtrack to the
8406 space before the wrap point. */
8407 if (skip == MOVE_LINE_CONTINUED)
8408 {
8409 int prev_x = max (it->current_x - 1, 0);
8410 RESTORE_IT (it, &save_it, save_data);
8411 move_it_in_display_line_to
8412 (it, -1, prev_x, MOVE_TO_X);
8413 }
8414 else
8415 bidi_unshelve_cache (save_data, 1);
8416 }
8417 else
8418 move_it_in_display_line_to (it, to_charpos, to_x, op);
8419 }
8420
8421
8422 /* Move IT forward until it satisfies one or more of the criteria in
8423 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8424
8425 OP is a bit-mask that specifies where to stop, and in particular,
8426 which of those four position arguments makes a difference. See the
8427 description of enum move_operation_enum.
8428
8429 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8430 screen line, this function will set IT to the next position that is
8431 displayed to the right of TO_CHARPOS on the screen. */
8432
8433 void
8434 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8435 {
8436 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8437 int line_height, line_start_x = 0, reached = 0;
8438 void *backup_data = NULL;
8439
8440 for (;;)
8441 {
8442 if (op & MOVE_TO_VPOS)
8443 {
8444 /* If no TO_CHARPOS and no TO_X specified, stop at the
8445 start of the line TO_VPOS. */
8446 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8447 {
8448 if (it->vpos == to_vpos)
8449 {
8450 reached = 1;
8451 break;
8452 }
8453 else
8454 skip = move_it_in_display_line_to (it, -1, -1, 0);
8455 }
8456 else
8457 {
8458 /* TO_VPOS >= 0 means stop at TO_X in the line at
8459 TO_VPOS, or at TO_POS, whichever comes first. */
8460 if (it->vpos == to_vpos)
8461 {
8462 reached = 2;
8463 break;
8464 }
8465
8466 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8467
8468 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8469 {
8470 reached = 3;
8471 break;
8472 }
8473 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8474 {
8475 /* We have reached TO_X but not in the line we want. */
8476 skip = move_it_in_display_line_to (it, to_charpos,
8477 -1, MOVE_TO_POS);
8478 if (skip == MOVE_POS_MATCH_OR_ZV)
8479 {
8480 reached = 4;
8481 break;
8482 }
8483 }
8484 }
8485 }
8486 else if (op & MOVE_TO_Y)
8487 {
8488 struct it it_backup;
8489
8490 if (it->line_wrap == WORD_WRAP)
8491 SAVE_IT (it_backup, *it, backup_data);
8492
8493 /* TO_Y specified means stop at TO_X in the line containing
8494 TO_Y---or at TO_CHARPOS if this is reached first. The
8495 problem is that we can't really tell whether the line
8496 contains TO_Y before we have completely scanned it, and
8497 this may skip past TO_X. What we do is to first scan to
8498 TO_X.
8499
8500 If TO_X is not specified, use a TO_X of zero. The reason
8501 is to make the outcome of this function more predictable.
8502 If we didn't use TO_X == 0, we would stop at the end of
8503 the line which is probably not what a caller would expect
8504 to happen. */
8505 skip = move_it_in_display_line_to
8506 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8507 (MOVE_TO_X | (op & MOVE_TO_POS)));
8508
8509 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8510 if (skip == MOVE_POS_MATCH_OR_ZV)
8511 reached = 5;
8512 else if (skip == MOVE_X_REACHED)
8513 {
8514 /* If TO_X was reached, we want to know whether TO_Y is
8515 in the line. We know this is the case if the already
8516 scanned glyphs make the line tall enough. Otherwise,
8517 we must check by scanning the rest of the line. */
8518 line_height = it->max_ascent + it->max_descent;
8519 if (to_y >= it->current_y
8520 && to_y < it->current_y + line_height)
8521 {
8522 reached = 6;
8523 break;
8524 }
8525 SAVE_IT (it_backup, *it, backup_data);
8526 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8527 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8528 op & MOVE_TO_POS);
8529 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8530 line_height = it->max_ascent + it->max_descent;
8531 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8532
8533 if (to_y >= it->current_y
8534 && to_y < it->current_y + line_height)
8535 {
8536 /* If TO_Y is in this line and TO_X was reached
8537 above, we scanned too far. We have to restore
8538 IT's settings to the ones before skipping. */
8539 RESTORE_IT (it, &it_backup, backup_data);
8540 reached = 6;
8541 }
8542 else
8543 {
8544 skip = skip2;
8545 if (skip == MOVE_POS_MATCH_OR_ZV)
8546 reached = 7;
8547 }
8548 }
8549 else
8550 {
8551 /* Check whether TO_Y is in this line. */
8552 line_height = it->max_ascent + it->max_descent;
8553 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8554
8555 if (to_y >= it->current_y
8556 && to_y < it->current_y + line_height)
8557 {
8558 /* When word-wrap is on, TO_X may lie past the end
8559 of a wrapped line. Then it->current is the
8560 character on the next line, so backtrack to the
8561 space before the wrap point. */
8562 if (skip == MOVE_LINE_CONTINUED
8563 && it->line_wrap == WORD_WRAP)
8564 {
8565 int prev_x = max (it->current_x - 1, 0);
8566 RESTORE_IT (it, &it_backup, backup_data);
8567 skip = move_it_in_display_line_to
8568 (it, -1, prev_x, MOVE_TO_X);
8569 }
8570 reached = 6;
8571 }
8572 }
8573
8574 if (reached)
8575 break;
8576 }
8577 else if (BUFFERP (it->object)
8578 && (it->method == GET_FROM_BUFFER
8579 || it->method == GET_FROM_STRETCH)
8580 && IT_CHARPOS (*it) >= to_charpos
8581 /* Under bidi iteration, a call to set_iterator_to_next
8582 can scan far beyond to_charpos if the initial
8583 portion of the next line needs to be reordered. In
8584 that case, give move_it_in_display_line_to another
8585 chance below. */
8586 && !(it->bidi_p
8587 && it->bidi_it.scan_dir == -1))
8588 skip = MOVE_POS_MATCH_OR_ZV;
8589 else
8590 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8591
8592 switch (skip)
8593 {
8594 case MOVE_POS_MATCH_OR_ZV:
8595 reached = 8;
8596 goto out;
8597
8598 case MOVE_NEWLINE_OR_CR:
8599 set_iterator_to_next (it, 1);
8600 it->continuation_lines_width = 0;
8601 break;
8602
8603 case MOVE_LINE_TRUNCATED:
8604 it->continuation_lines_width = 0;
8605 reseat_at_next_visible_line_start (it, 0);
8606 if ((op & MOVE_TO_POS) != 0
8607 && IT_CHARPOS (*it) > to_charpos)
8608 {
8609 reached = 9;
8610 goto out;
8611 }
8612 break;
8613
8614 case MOVE_LINE_CONTINUED:
8615 /* For continued lines ending in a tab, some of the glyphs
8616 associated with the tab are displayed on the current
8617 line. Since it->current_x does not include these glyphs,
8618 we use it->last_visible_x instead. */
8619 if (it->c == '\t')
8620 {
8621 it->continuation_lines_width += it->last_visible_x;
8622 /* When moving by vpos, ensure that the iterator really
8623 advances to the next line (bug#847, bug#969). Fixme:
8624 do we need to do this in other circumstances? */
8625 if (it->current_x != it->last_visible_x
8626 && (op & MOVE_TO_VPOS)
8627 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8628 {
8629 line_start_x = it->current_x + it->pixel_width
8630 - it->last_visible_x;
8631 set_iterator_to_next (it, 0);
8632 }
8633 }
8634 else
8635 it->continuation_lines_width += it->current_x;
8636 break;
8637
8638 default:
8639 abort ();
8640 }
8641
8642 /* Reset/increment for the next run. */
8643 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8644 it->current_x = line_start_x;
8645 line_start_x = 0;
8646 it->hpos = 0;
8647 it->current_y += it->max_ascent + it->max_descent;
8648 ++it->vpos;
8649 last_height = it->max_ascent + it->max_descent;
8650 last_max_ascent = it->max_ascent;
8651 it->max_ascent = it->max_descent = 0;
8652 }
8653
8654 out:
8655
8656 /* On text terminals, we may stop at the end of a line in the middle
8657 of a multi-character glyph. If the glyph itself is continued,
8658 i.e. it is actually displayed on the next line, don't treat this
8659 stopping point as valid; move to the next line instead (unless
8660 that brings us offscreen). */
8661 if (!FRAME_WINDOW_P (it->f)
8662 && op & MOVE_TO_POS
8663 && IT_CHARPOS (*it) == to_charpos
8664 && it->what == IT_CHARACTER
8665 && it->nglyphs > 1
8666 && it->line_wrap == WINDOW_WRAP
8667 && it->current_x == it->last_visible_x - 1
8668 && it->c != '\n'
8669 && it->c != '\t'
8670 && it->vpos < XFASTINT (it->w->window_end_vpos))
8671 {
8672 it->continuation_lines_width += it->current_x;
8673 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8674 it->current_y += it->max_ascent + it->max_descent;
8675 ++it->vpos;
8676 last_height = it->max_ascent + it->max_descent;
8677 last_max_ascent = it->max_ascent;
8678 }
8679
8680 if (backup_data)
8681 bidi_unshelve_cache (backup_data, 1);
8682
8683 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8684 }
8685
8686
8687 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8688
8689 If DY > 0, move IT backward at least that many pixels. DY = 0
8690 means move IT backward to the preceding line start or BEGV. This
8691 function may move over more than DY pixels if IT->current_y - DY
8692 ends up in the middle of a line; in this case IT->current_y will be
8693 set to the top of the line moved to. */
8694
8695 void
8696 move_it_vertically_backward (struct it *it, int dy)
8697 {
8698 int nlines, h;
8699 struct it it2, it3;
8700 void *it2data = NULL, *it3data = NULL;
8701 ptrdiff_t start_pos;
8702
8703 move_further_back:
8704 xassert (dy >= 0);
8705
8706 start_pos = IT_CHARPOS (*it);
8707
8708 /* Estimate how many newlines we must move back. */
8709 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8710
8711 /* Set the iterator's position that many lines back. */
8712 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8713 back_to_previous_visible_line_start (it);
8714
8715 /* Reseat the iterator here. When moving backward, we don't want
8716 reseat to skip forward over invisible text, set up the iterator
8717 to deliver from overlay strings at the new position etc. So,
8718 use reseat_1 here. */
8719 reseat_1 (it, it->current.pos, 1);
8720
8721 /* We are now surely at a line start. */
8722 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8723 reordering is in effect. */
8724 it->continuation_lines_width = 0;
8725
8726 /* Move forward and see what y-distance we moved. First move to the
8727 start of the next line so that we get its height. We need this
8728 height to be able to tell whether we reached the specified
8729 y-distance. */
8730 SAVE_IT (it2, *it, it2data);
8731 it2.max_ascent = it2.max_descent = 0;
8732 do
8733 {
8734 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8735 MOVE_TO_POS | MOVE_TO_VPOS);
8736 }
8737 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8738 /* If we are in a display string which starts at START_POS,
8739 and that display string includes a newline, and we are
8740 right after that newline (i.e. at the beginning of a
8741 display line), exit the loop, because otherwise we will
8742 infloop, since move_it_to will see that it is already at
8743 START_POS and will not move. */
8744 || (it2.method == GET_FROM_STRING
8745 && IT_CHARPOS (it2) == start_pos
8746 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8747 xassert (IT_CHARPOS (*it) >= BEGV);
8748 SAVE_IT (it3, it2, it3data);
8749
8750 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8751 xassert (IT_CHARPOS (*it) >= BEGV);
8752 /* H is the actual vertical distance from the position in *IT
8753 and the starting position. */
8754 h = it2.current_y - it->current_y;
8755 /* NLINES is the distance in number of lines. */
8756 nlines = it2.vpos - it->vpos;
8757
8758 /* Correct IT's y and vpos position
8759 so that they are relative to the starting point. */
8760 it->vpos -= nlines;
8761 it->current_y -= h;
8762
8763 if (dy == 0)
8764 {
8765 /* DY == 0 means move to the start of the screen line. The
8766 value of nlines is > 0 if continuation lines were involved,
8767 or if the original IT position was at start of a line. */
8768 RESTORE_IT (it, it, it2data);
8769 if (nlines > 0)
8770 move_it_by_lines (it, nlines);
8771 /* The above code moves us to some position NLINES down,
8772 usually to its first glyph (leftmost in an L2R line), but
8773 that's not necessarily the start of the line, under bidi
8774 reordering. We want to get to the character position
8775 that is immediately after the newline of the previous
8776 line. */
8777 if (it->bidi_p && IT_CHARPOS (*it) > BEGV
8778 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8779 {
8780 ptrdiff_t nl_pos =
8781 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8782
8783 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8784 }
8785 bidi_unshelve_cache (it3data, 1);
8786 }
8787 else
8788 {
8789 /* The y-position we try to reach, relative to *IT.
8790 Note that H has been subtracted in front of the if-statement. */
8791 int target_y = it->current_y + h - dy;
8792 int y0 = it3.current_y;
8793 int y1;
8794 int line_height;
8795
8796 RESTORE_IT (&it3, &it3, it3data);
8797 y1 = line_bottom_y (&it3);
8798 line_height = y1 - y0;
8799 RESTORE_IT (it, it, it2data);
8800 /* If we did not reach target_y, try to move further backward if
8801 we can. If we moved too far backward, try to move forward. */
8802 if (target_y < it->current_y
8803 /* This is heuristic. In a window that's 3 lines high, with
8804 a line height of 13 pixels each, recentering with point
8805 on the bottom line will try to move -39/2 = 19 pixels
8806 backward. Try to avoid moving into the first line. */
8807 && (it->current_y - target_y
8808 > min (window_box_height (it->w), line_height * 2 / 3))
8809 && IT_CHARPOS (*it) > BEGV)
8810 {
8811 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8812 target_y - it->current_y));
8813 dy = it->current_y - target_y;
8814 goto move_further_back;
8815 }
8816 else if (target_y >= it->current_y + line_height
8817 && IT_CHARPOS (*it) < ZV)
8818 {
8819 /* Should move forward by at least one line, maybe more.
8820
8821 Note: Calling move_it_by_lines can be expensive on
8822 terminal frames, where compute_motion is used (via
8823 vmotion) to do the job, when there are very long lines
8824 and truncate-lines is nil. That's the reason for
8825 treating terminal frames specially here. */
8826
8827 if (!FRAME_WINDOW_P (it->f))
8828 move_it_vertically (it, target_y - (it->current_y + line_height));
8829 else
8830 {
8831 do
8832 {
8833 move_it_by_lines (it, 1);
8834 }
8835 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8836 }
8837 }
8838 }
8839 }
8840
8841
8842 /* Move IT by a specified amount of pixel lines DY. DY negative means
8843 move backwards. DY = 0 means move to start of screen line. At the
8844 end, IT will be on the start of a screen line. */
8845
8846 void
8847 move_it_vertically (struct it *it, int dy)
8848 {
8849 if (dy <= 0)
8850 move_it_vertically_backward (it, -dy);
8851 else
8852 {
8853 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8854 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8855 MOVE_TO_POS | MOVE_TO_Y);
8856 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8857
8858 /* If buffer ends in ZV without a newline, move to the start of
8859 the line to satisfy the post-condition. */
8860 if (IT_CHARPOS (*it) == ZV
8861 && ZV > BEGV
8862 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8863 move_it_by_lines (it, 0);
8864 }
8865 }
8866
8867
8868 /* Move iterator IT past the end of the text line it is in. */
8869
8870 void
8871 move_it_past_eol (struct it *it)
8872 {
8873 enum move_it_result rc;
8874
8875 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8876 if (rc == MOVE_NEWLINE_OR_CR)
8877 set_iterator_to_next (it, 0);
8878 }
8879
8880
8881 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8882 negative means move up. DVPOS == 0 means move to the start of the
8883 screen line.
8884
8885 Optimization idea: If we would know that IT->f doesn't use
8886 a face with proportional font, we could be faster for
8887 truncate-lines nil. */
8888
8889 void
8890 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
8891 {
8892
8893 /* The commented-out optimization uses vmotion on terminals. This
8894 gives bad results, because elements like it->what, on which
8895 callers such as pos_visible_p rely, aren't updated. */
8896 /* struct position pos;
8897 if (!FRAME_WINDOW_P (it->f))
8898 {
8899 struct text_pos textpos;
8900
8901 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8902 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8903 reseat (it, textpos, 1);
8904 it->vpos += pos.vpos;
8905 it->current_y += pos.vpos;
8906 }
8907 else */
8908
8909 if (dvpos == 0)
8910 {
8911 /* DVPOS == 0 means move to the start of the screen line. */
8912 move_it_vertically_backward (it, 0);
8913 xassert (it->current_x == 0 && it->hpos == 0);
8914 /* Let next call to line_bottom_y calculate real line height */
8915 last_height = 0;
8916 }
8917 else if (dvpos > 0)
8918 {
8919 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8920 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8921 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8922 }
8923 else
8924 {
8925 struct it it2;
8926 void *it2data = NULL;
8927 ptrdiff_t start_charpos, i;
8928
8929 /* Start at the beginning of the screen line containing IT's
8930 position. This may actually move vertically backwards,
8931 in case of overlays, so adjust dvpos accordingly. */
8932 dvpos += it->vpos;
8933 move_it_vertically_backward (it, 0);
8934 dvpos -= it->vpos;
8935
8936 /* Go back -DVPOS visible lines and reseat the iterator there. */
8937 start_charpos = IT_CHARPOS (*it);
8938 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8939 back_to_previous_visible_line_start (it);
8940 reseat (it, it->current.pos, 1);
8941
8942 /* Move further back if we end up in a string or an image. */
8943 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8944 {
8945 /* First try to move to start of display line. */
8946 dvpos += it->vpos;
8947 move_it_vertically_backward (it, 0);
8948 dvpos -= it->vpos;
8949 if (IT_POS_VALID_AFTER_MOVE_P (it))
8950 break;
8951 /* If start of line is still in string or image,
8952 move further back. */
8953 back_to_previous_visible_line_start (it);
8954 reseat (it, it->current.pos, 1);
8955 dvpos--;
8956 }
8957
8958 it->current_x = it->hpos = 0;
8959
8960 /* Above call may have moved too far if continuation lines
8961 are involved. Scan forward and see if it did. */
8962 SAVE_IT (it2, *it, it2data);
8963 it2.vpos = it2.current_y = 0;
8964 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8965 it->vpos -= it2.vpos;
8966 it->current_y -= it2.current_y;
8967 it->current_x = it->hpos = 0;
8968
8969 /* If we moved too far back, move IT some lines forward. */
8970 if (it2.vpos > -dvpos)
8971 {
8972 int delta = it2.vpos + dvpos;
8973
8974 RESTORE_IT (&it2, &it2, it2data);
8975 SAVE_IT (it2, *it, it2data);
8976 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8977 /* Move back again if we got too far ahead. */
8978 if (IT_CHARPOS (*it) >= start_charpos)
8979 RESTORE_IT (it, &it2, it2data);
8980 else
8981 bidi_unshelve_cache (it2data, 1);
8982 }
8983 else
8984 RESTORE_IT (it, it, it2data);
8985 }
8986 }
8987
8988 /* Return 1 if IT points into the middle of a display vector. */
8989
8990 int
8991 in_display_vector_p (struct it *it)
8992 {
8993 return (it->method == GET_FROM_DISPLAY_VECTOR
8994 && it->current.dpvec_index > 0
8995 && it->dpvec + it->current.dpvec_index != it->dpend);
8996 }
8997
8998 \f
8999 /***********************************************************************
9000 Messages
9001 ***********************************************************************/
9002
9003
9004 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9005 to *Messages*. */
9006
9007 void
9008 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9009 {
9010 Lisp_Object args[3];
9011 Lisp_Object msg, fmt;
9012 char *buffer;
9013 ptrdiff_t len;
9014 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9015 USE_SAFE_ALLOCA;
9016
9017 /* Do nothing if called asynchronously. Inserting text into
9018 a buffer may call after-change-functions and alike and
9019 that would means running Lisp asynchronously. */
9020 if (handling_signal)
9021 return;
9022
9023 fmt = msg = Qnil;
9024 GCPRO4 (fmt, msg, arg1, arg2);
9025
9026 args[0] = fmt = build_string (format);
9027 args[1] = arg1;
9028 args[2] = arg2;
9029 msg = Fformat (3, args);
9030
9031 len = SBYTES (msg) + 1;
9032 SAFE_ALLOCA (buffer, char *, len);
9033 memcpy (buffer, SDATA (msg), len);
9034
9035 message_dolog (buffer, len - 1, 1, 0);
9036 SAFE_FREE ();
9037
9038 UNGCPRO;
9039 }
9040
9041
9042 /* Output a newline in the *Messages* buffer if "needs" one. */
9043
9044 void
9045 message_log_maybe_newline (void)
9046 {
9047 if (message_log_need_newline)
9048 message_dolog ("", 0, 1, 0);
9049 }
9050
9051
9052 /* Add a string M of length NBYTES to the message log, optionally
9053 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9054 nonzero, means interpret the contents of M as multibyte. This
9055 function calls low-level routines in order to bypass text property
9056 hooks, etc. which might not be safe to run.
9057
9058 This may GC (insert may run before/after change hooks),
9059 so the buffer M must NOT point to a Lisp string. */
9060
9061 void
9062 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9063 {
9064 const unsigned char *msg = (const unsigned char *) m;
9065
9066 if (!NILP (Vmemory_full))
9067 return;
9068
9069 if (!NILP (Vmessage_log_max))
9070 {
9071 struct buffer *oldbuf;
9072 Lisp_Object oldpoint, oldbegv, oldzv;
9073 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9074 ptrdiff_t point_at_end = 0;
9075 ptrdiff_t zv_at_end = 0;
9076 Lisp_Object old_deactivate_mark, tem;
9077 struct gcpro gcpro1;
9078
9079 old_deactivate_mark = Vdeactivate_mark;
9080 oldbuf = current_buffer;
9081 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9082 BVAR (current_buffer, undo_list) = Qt;
9083
9084 oldpoint = message_dolog_marker1;
9085 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9086 oldbegv = message_dolog_marker2;
9087 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9088 oldzv = message_dolog_marker3;
9089 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9090 GCPRO1 (old_deactivate_mark);
9091
9092 if (PT == Z)
9093 point_at_end = 1;
9094 if (ZV == Z)
9095 zv_at_end = 1;
9096
9097 BEGV = BEG;
9098 BEGV_BYTE = BEG_BYTE;
9099 ZV = Z;
9100 ZV_BYTE = Z_BYTE;
9101 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9102
9103 /* Insert the string--maybe converting multibyte to single byte
9104 or vice versa, so that all the text fits the buffer. */
9105 if (multibyte
9106 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9107 {
9108 ptrdiff_t i;
9109 int c, char_bytes;
9110 char work[1];
9111
9112 /* Convert a multibyte string to single-byte
9113 for the *Message* buffer. */
9114 for (i = 0; i < nbytes; i += char_bytes)
9115 {
9116 c = string_char_and_length (msg + i, &char_bytes);
9117 work[0] = (ASCII_CHAR_P (c)
9118 ? c
9119 : multibyte_char_to_unibyte (c));
9120 insert_1_both (work, 1, 1, 1, 0, 0);
9121 }
9122 }
9123 else if (! multibyte
9124 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9125 {
9126 ptrdiff_t i;
9127 int c, char_bytes;
9128 unsigned char str[MAX_MULTIBYTE_LENGTH];
9129 /* Convert a single-byte string to multibyte
9130 for the *Message* buffer. */
9131 for (i = 0; i < nbytes; i++)
9132 {
9133 c = msg[i];
9134 MAKE_CHAR_MULTIBYTE (c);
9135 char_bytes = CHAR_STRING (c, str);
9136 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9137 }
9138 }
9139 else if (nbytes)
9140 insert_1 (m, nbytes, 1, 0, 0);
9141
9142 if (nlflag)
9143 {
9144 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9145 printmax_t dups;
9146 insert_1 ("\n", 1, 1, 0, 0);
9147
9148 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9149 this_bol = PT;
9150 this_bol_byte = PT_BYTE;
9151
9152 /* See if this line duplicates the previous one.
9153 If so, combine duplicates. */
9154 if (this_bol > BEG)
9155 {
9156 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9157 prev_bol = PT;
9158 prev_bol_byte = PT_BYTE;
9159
9160 dups = message_log_check_duplicate (prev_bol_byte,
9161 this_bol_byte);
9162 if (dups)
9163 {
9164 del_range_both (prev_bol, prev_bol_byte,
9165 this_bol, this_bol_byte, 0);
9166 if (dups > 1)
9167 {
9168 char dupstr[sizeof " [ times]"
9169 + INT_STRLEN_BOUND (printmax_t)];
9170 int duplen;
9171
9172 /* If you change this format, don't forget to also
9173 change message_log_check_duplicate. */
9174 sprintf (dupstr, " [%"pMd" times]", dups);
9175 duplen = strlen (dupstr);
9176 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9177 insert_1 (dupstr, duplen, 1, 0, 1);
9178 }
9179 }
9180 }
9181
9182 /* If we have more than the desired maximum number of lines
9183 in the *Messages* buffer now, delete the oldest ones.
9184 This is safe because we don't have undo in this buffer. */
9185
9186 if (NATNUMP (Vmessage_log_max))
9187 {
9188 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9189 -XFASTINT (Vmessage_log_max) - 1, 0);
9190 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9191 }
9192 }
9193 BEGV = XMARKER (oldbegv)->charpos;
9194 BEGV_BYTE = marker_byte_position (oldbegv);
9195
9196 if (zv_at_end)
9197 {
9198 ZV = Z;
9199 ZV_BYTE = Z_BYTE;
9200 }
9201 else
9202 {
9203 ZV = XMARKER (oldzv)->charpos;
9204 ZV_BYTE = marker_byte_position (oldzv);
9205 }
9206
9207 if (point_at_end)
9208 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9209 else
9210 /* We can't do Fgoto_char (oldpoint) because it will run some
9211 Lisp code. */
9212 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9213 XMARKER (oldpoint)->bytepos);
9214
9215 UNGCPRO;
9216 unchain_marker (XMARKER (oldpoint));
9217 unchain_marker (XMARKER (oldbegv));
9218 unchain_marker (XMARKER (oldzv));
9219
9220 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9221 set_buffer_internal (oldbuf);
9222 if (NILP (tem))
9223 windows_or_buffers_changed = old_windows_or_buffers_changed;
9224 message_log_need_newline = !nlflag;
9225 Vdeactivate_mark = old_deactivate_mark;
9226 }
9227 }
9228
9229
9230 /* We are at the end of the buffer after just having inserted a newline.
9231 (Note: We depend on the fact we won't be crossing the gap.)
9232 Check to see if the most recent message looks a lot like the previous one.
9233 Return 0 if different, 1 if the new one should just replace it, or a
9234 value N > 1 if we should also append " [N times]". */
9235
9236 static intmax_t
9237 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9238 {
9239 ptrdiff_t i;
9240 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9241 int seen_dots = 0;
9242 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9243 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9244
9245 for (i = 0; i < len; i++)
9246 {
9247 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9248 seen_dots = 1;
9249 if (p1[i] != p2[i])
9250 return seen_dots;
9251 }
9252 p1 += len;
9253 if (*p1 == '\n')
9254 return 2;
9255 if (*p1++ == ' ' && *p1++ == '[')
9256 {
9257 char *pend;
9258 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9259 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9260 return n+1;
9261 }
9262 return 0;
9263 }
9264 \f
9265
9266 /* Display an echo area message M with a specified length of NBYTES
9267 bytes. The string may include null characters. If M is 0, clear
9268 out any existing message, and let the mini-buffer text show
9269 through.
9270
9271 This may GC, so the buffer M must NOT point to a Lisp string. */
9272
9273 void
9274 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9275 {
9276 /* First flush out any partial line written with print. */
9277 message_log_maybe_newline ();
9278 if (m)
9279 message_dolog (m, nbytes, 1, multibyte);
9280 message2_nolog (m, nbytes, multibyte);
9281 }
9282
9283
9284 /* The non-logging counterpart of message2. */
9285
9286 void
9287 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9288 {
9289 struct frame *sf = SELECTED_FRAME ();
9290 message_enable_multibyte = multibyte;
9291
9292 if (FRAME_INITIAL_P (sf))
9293 {
9294 if (noninteractive_need_newline)
9295 putc ('\n', stderr);
9296 noninteractive_need_newline = 0;
9297 if (m)
9298 fwrite (m, nbytes, 1, stderr);
9299 if (cursor_in_echo_area == 0)
9300 fprintf (stderr, "\n");
9301 fflush (stderr);
9302 }
9303 /* A null message buffer means that the frame hasn't really been
9304 initialized yet. Error messages get reported properly by
9305 cmd_error, so this must be just an informative message; toss it. */
9306 else if (INTERACTIVE
9307 && sf->glyphs_initialized_p
9308 && FRAME_MESSAGE_BUF (sf))
9309 {
9310 Lisp_Object mini_window;
9311 struct frame *f;
9312
9313 /* Get the frame containing the mini-buffer
9314 that the selected frame is using. */
9315 mini_window = FRAME_MINIBUF_WINDOW (sf);
9316 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9317
9318 FRAME_SAMPLE_VISIBILITY (f);
9319 if (FRAME_VISIBLE_P (sf)
9320 && ! FRAME_VISIBLE_P (f))
9321 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9322
9323 if (m)
9324 {
9325 set_message (m, Qnil, nbytes, multibyte);
9326 if (minibuffer_auto_raise)
9327 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9328 }
9329 else
9330 clear_message (1, 1);
9331
9332 do_pending_window_change (0);
9333 echo_area_display (1);
9334 do_pending_window_change (0);
9335 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9336 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9337 }
9338 }
9339
9340
9341 /* Display an echo area message M with a specified length of NBYTES
9342 bytes. The string may include null characters. If M is not a
9343 string, clear out any existing message, and let the mini-buffer
9344 text show through.
9345
9346 This function cancels echoing. */
9347
9348 void
9349 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9350 {
9351 struct gcpro gcpro1;
9352
9353 GCPRO1 (m);
9354 clear_message (1,1);
9355 cancel_echoing ();
9356
9357 /* First flush out any partial line written with print. */
9358 message_log_maybe_newline ();
9359 if (STRINGP (m))
9360 {
9361 char *buffer;
9362 USE_SAFE_ALLOCA;
9363
9364 SAFE_ALLOCA (buffer, char *, nbytes);
9365 memcpy (buffer, SDATA (m), nbytes);
9366 message_dolog (buffer, nbytes, 1, multibyte);
9367 SAFE_FREE ();
9368 }
9369 message3_nolog (m, nbytes, multibyte);
9370
9371 UNGCPRO;
9372 }
9373
9374
9375 /* The non-logging version of message3.
9376 This does not cancel echoing, because it is used for echoing.
9377 Perhaps we need to make a separate function for echoing
9378 and make this cancel echoing. */
9379
9380 void
9381 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9382 {
9383 struct frame *sf = SELECTED_FRAME ();
9384 message_enable_multibyte = multibyte;
9385
9386 if (FRAME_INITIAL_P (sf))
9387 {
9388 if (noninteractive_need_newline)
9389 putc ('\n', stderr);
9390 noninteractive_need_newline = 0;
9391 if (STRINGP (m))
9392 fwrite (SDATA (m), nbytes, 1, stderr);
9393 if (cursor_in_echo_area == 0)
9394 fprintf (stderr, "\n");
9395 fflush (stderr);
9396 }
9397 /* A null message buffer means that the frame hasn't really been
9398 initialized yet. Error messages get reported properly by
9399 cmd_error, so this must be just an informative message; toss it. */
9400 else if (INTERACTIVE
9401 && sf->glyphs_initialized_p
9402 && FRAME_MESSAGE_BUF (sf))
9403 {
9404 Lisp_Object mini_window;
9405 Lisp_Object frame;
9406 struct frame *f;
9407
9408 /* Get the frame containing the mini-buffer
9409 that the selected frame is using. */
9410 mini_window = FRAME_MINIBUF_WINDOW (sf);
9411 frame = XWINDOW (mini_window)->frame;
9412 f = XFRAME (frame);
9413
9414 FRAME_SAMPLE_VISIBILITY (f);
9415 if (FRAME_VISIBLE_P (sf)
9416 && !FRAME_VISIBLE_P (f))
9417 Fmake_frame_visible (frame);
9418
9419 if (STRINGP (m) && SCHARS (m) > 0)
9420 {
9421 set_message (NULL, m, nbytes, multibyte);
9422 if (minibuffer_auto_raise)
9423 Fraise_frame (frame);
9424 /* Assume we are not echoing.
9425 (If we are, echo_now will override this.) */
9426 echo_message_buffer = Qnil;
9427 }
9428 else
9429 clear_message (1, 1);
9430
9431 do_pending_window_change (0);
9432 echo_area_display (1);
9433 do_pending_window_change (0);
9434 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9435 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9436 }
9437 }
9438
9439
9440 /* Display a null-terminated echo area message M. If M is 0, clear
9441 out any existing message, and let the mini-buffer text show through.
9442
9443 The buffer M must continue to exist until after the echo area gets
9444 cleared or some other message gets displayed there. Do not pass
9445 text that is stored in a Lisp string. Do not pass text in a buffer
9446 that was alloca'd. */
9447
9448 void
9449 message1 (const char *m)
9450 {
9451 message2 (m, (m ? strlen (m) : 0), 0);
9452 }
9453
9454
9455 /* The non-logging counterpart of message1. */
9456
9457 void
9458 message1_nolog (const char *m)
9459 {
9460 message2_nolog (m, (m ? strlen (m) : 0), 0);
9461 }
9462
9463 /* Display a message M which contains a single %s
9464 which gets replaced with STRING. */
9465
9466 void
9467 message_with_string (const char *m, Lisp_Object string, int log)
9468 {
9469 CHECK_STRING (string);
9470
9471 if (noninteractive)
9472 {
9473 if (m)
9474 {
9475 if (noninteractive_need_newline)
9476 putc ('\n', stderr);
9477 noninteractive_need_newline = 0;
9478 fprintf (stderr, m, SDATA (string));
9479 if (!cursor_in_echo_area)
9480 fprintf (stderr, "\n");
9481 fflush (stderr);
9482 }
9483 }
9484 else if (INTERACTIVE)
9485 {
9486 /* The frame whose minibuffer we're going to display the message on.
9487 It may be larger than the selected frame, so we need
9488 to use its buffer, not the selected frame's buffer. */
9489 Lisp_Object mini_window;
9490 struct frame *f, *sf = SELECTED_FRAME ();
9491
9492 /* Get the frame containing the minibuffer
9493 that the selected frame is using. */
9494 mini_window = FRAME_MINIBUF_WINDOW (sf);
9495 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9496
9497 /* A null message buffer means that the frame hasn't really been
9498 initialized yet. Error messages get reported properly by
9499 cmd_error, so this must be just an informative message; toss it. */
9500 if (FRAME_MESSAGE_BUF (f))
9501 {
9502 Lisp_Object args[2], msg;
9503 struct gcpro gcpro1, gcpro2;
9504
9505 args[0] = build_string (m);
9506 args[1] = msg = string;
9507 GCPRO2 (args[0], msg);
9508 gcpro1.nvars = 2;
9509
9510 msg = Fformat (2, args);
9511
9512 if (log)
9513 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9514 else
9515 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9516
9517 UNGCPRO;
9518
9519 /* Print should start at the beginning of the message
9520 buffer next time. */
9521 message_buf_print = 0;
9522 }
9523 }
9524 }
9525
9526
9527 /* Dump an informative message to the minibuf. If M is 0, clear out
9528 any existing message, and let the mini-buffer text show through. */
9529
9530 static void
9531 vmessage (const char *m, va_list ap)
9532 {
9533 if (noninteractive)
9534 {
9535 if (m)
9536 {
9537 if (noninteractive_need_newline)
9538 putc ('\n', stderr);
9539 noninteractive_need_newline = 0;
9540 vfprintf (stderr, m, ap);
9541 if (cursor_in_echo_area == 0)
9542 fprintf (stderr, "\n");
9543 fflush (stderr);
9544 }
9545 }
9546 else if (INTERACTIVE)
9547 {
9548 /* The frame whose mini-buffer we're going to display the message
9549 on. It may be larger than the selected frame, so we need to
9550 use its buffer, not the selected frame's buffer. */
9551 Lisp_Object mini_window;
9552 struct frame *f, *sf = SELECTED_FRAME ();
9553
9554 /* Get the frame containing the mini-buffer
9555 that the selected frame is using. */
9556 mini_window = FRAME_MINIBUF_WINDOW (sf);
9557 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9558
9559 /* A null message buffer means that the frame hasn't really been
9560 initialized yet. Error messages get reported properly by
9561 cmd_error, so this must be just an informative message; toss
9562 it. */
9563 if (FRAME_MESSAGE_BUF (f))
9564 {
9565 if (m)
9566 {
9567 ptrdiff_t len;
9568
9569 len = doprnt (FRAME_MESSAGE_BUF (f),
9570 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9571
9572 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9573 }
9574 else
9575 message1 (0);
9576
9577 /* Print should start at the beginning of the message
9578 buffer next time. */
9579 message_buf_print = 0;
9580 }
9581 }
9582 }
9583
9584 void
9585 message (const char *m, ...)
9586 {
9587 va_list ap;
9588 va_start (ap, m);
9589 vmessage (m, ap);
9590 va_end (ap);
9591 }
9592
9593
9594 #if 0
9595 /* The non-logging version of message. */
9596
9597 void
9598 message_nolog (const char *m, ...)
9599 {
9600 Lisp_Object old_log_max;
9601 va_list ap;
9602 va_start (ap, m);
9603 old_log_max = Vmessage_log_max;
9604 Vmessage_log_max = Qnil;
9605 vmessage (m, ap);
9606 Vmessage_log_max = old_log_max;
9607 va_end (ap);
9608 }
9609 #endif
9610
9611
9612 /* Display the current message in the current mini-buffer. This is
9613 only called from error handlers in process.c, and is not time
9614 critical. */
9615
9616 void
9617 update_echo_area (void)
9618 {
9619 if (!NILP (echo_area_buffer[0]))
9620 {
9621 Lisp_Object string;
9622 string = Fcurrent_message ();
9623 message3 (string, SBYTES (string),
9624 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9625 }
9626 }
9627
9628
9629 /* Make sure echo area buffers in `echo_buffers' are live.
9630 If they aren't, make new ones. */
9631
9632 static void
9633 ensure_echo_area_buffers (void)
9634 {
9635 int i;
9636
9637 for (i = 0; i < 2; ++i)
9638 if (!BUFFERP (echo_buffer[i])
9639 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9640 {
9641 char name[30];
9642 Lisp_Object old_buffer;
9643 int j;
9644
9645 old_buffer = echo_buffer[i];
9646 sprintf (name, " *Echo Area %d*", i);
9647 echo_buffer[i] = Fget_buffer_create (build_string (name));
9648 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9649 /* to force word wrap in echo area -
9650 it was decided to postpone this*/
9651 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9652
9653 for (j = 0; j < 2; ++j)
9654 if (EQ (old_buffer, echo_area_buffer[j]))
9655 echo_area_buffer[j] = echo_buffer[i];
9656 }
9657 }
9658
9659
9660 /* Call FN with args A1..A4 with either the current or last displayed
9661 echo_area_buffer as current buffer.
9662
9663 WHICH zero means use the current message buffer
9664 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9665 from echo_buffer[] and clear it.
9666
9667 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9668 suitable buffer from echo_buffer[] and clear it.
9669
9670 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9671 that the current message becomes the last displayed one, make
9672 choose a suitable buffer for echo_area_buffer[0], and clear it.
9673
9674 Value is what FN returns. */
9675
9676 static int
9677 with_echo_area_buffer (struct window *w, int which,
9678 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9679 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9680 {
9681 Lisp_Object buffer;
9682 int this_one, the_other, clear_buffer_p, rc;
9683 ptrdiff_t count = SPECPDL_INDEX ();
9684
9685 /* If buffers aren't live, make new ones. */
9686 ensure_echo_area_buffers ();
9687
9688 clear_buffer_p = 0;
9689
9690 if (which == 0)
9691 this_one = 0, the_other = 1;
9692 else if (which > 0)
9693 this_one = 1, the_other = 0;
9694 else
9695 {
9696 this_one = 0, the_other = 1;
9697 clear_buffer_p = 1;
9698
9699 /* We need a fresh one in case the current echo buffer equals
9700 the one containing the last displayed echo area message. */
9701 if (!NILP (echo_area_buffer[this_one])
9702 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9703 echo_area_buffer[this_one] = Qnil;
9704 }
9705
9706 /* Choose a suitable buffer from echo_buffer[] is we don't
9707 have one. */
9708 if (NILP (echo_area_buffer[this_one]))
9709 {
9710 echo_area_buffer[this_one]
9711 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9712 ? echo_buffer[the_other]
9713 : echo_buffer[this_one]);
9714 clear_buffer_p = 1;
9715 }
9716
9717 buffer = echo_area_buffer[this_one];
9718
9719 /* Don't get confused by reusing the buffer used for echoing
9720 for a different purpose. */
9721 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9722 cancel_echoing ();
9723
9724 record_unwind_protect (unwind_with_echo_area_buffer,
9725 with_echo_area_buffer_unwind_data (w));
9726
9727 /* Make the echo area buffer current. Note that for display
9728 purposes, it is not necessary that the displayed window's buffer
9729 == current_buffer, except for text property lookup. So, let's
9730 only set that buffer temporarily here without doing a full
9731 Fset_window_buffer. We must also change w->pointm, though,
9732 because otherwise an assertions in unshow_buffer fails, and Emacs
9733 aborts. */
9734 set_buffer_internal_1 (XBUFFER (buffer));
9735 if (w)
9736 {
9737 w->buffer = buffer;
9738 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9739 }
9740
9741 BVAR (current_buffer, undo_list) = Qt;
9742 BVAR (current_buffer, read_only) = Qnil;
9743 specbind (Qinhibit_read_only, Qt);
9744 specbind (Qinhibit_modification_hooks, Qt);
9745
9746 if (clear_buffer_p && Z > BEG)
9747 del_range (BEG, Z);
9748
9749 xassert (BEGV >= BEG);
9750 xassert (ZV <= Z && ZV >= BEGV);
9751
9752 rc = fn (a1, a2, a3, a4);
9753
9754 xassert (BEGV >= BEG);
9755 xassert (ZV <= Z && ZV >= BEGV);
9756
9757 unbind_to (count, Qnil);
9758 return rc;
9759 }
9760
9761
9762 /* Save state that should be preserved around the call to the function
9763 FN called in with_echo_area_buffer. */
9764
9765 static Lisp_Object
9766 with_echo_area_buffer_unwind_data (struct window *w)
9767 {
9768 int i = 0;
9769 Lisp_Object vector, tmp;
9770
9771 /* Reduce consing by keeping one vector in
9772 Vwith_echo_area_save_vector. */
9773 vector = Vwith_echo_area_save_vector;
9774 Vwith_echo_area_save_vector = Qnil;
9775
9776 if (NILP (vector))
9777 vector = Fmake_vector (make_number (7), Qnil);
9778
9779 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9780 ASET (vector, i, Vdeactivate_mark); ++i;
9781 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9782
9783 if (w)
9784 {
9785 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9786 ASET (vector, i, w->buffer); ++i;
9787 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9788 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9789 }
9790 else
9791 {
9792 int end = i + 4;
9793 for (; i < end; ++i)
9794 ASET (vector, i, Qnil);
9795 }
9796
9797 xassert (i == ASIZE (vector));
9798 return vector;
9799 }
9800
9801
9802 /* Restore global state from VECTOR which was created by
9803 with_echo_area_buffer_unwind_data. */
9804
9805 static Lisp_Object
9806 unwind_with_echo_area_buffer (Lisp_Object vector)
9807 {
9808 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9809 Vdeactivate_mark = AREF (vector, 1);
9810 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9811
9812 if (WINDOWP (AREF (vector, 3)))
9813 {
9814 struct window *w;
9815 Lisp_Object buffer, charpos, bytepos;
9816
9817 w = XWINDOW (AREF (vector, 3));
9818 buffer = AREF (vector, 4);
9819 charpos = AREF (vector, 5);
9820 bytepos = AREF (vector, 6);
9821
9822 w->buffer = buffer;
9823 set_marker_both (w->pointm, buffer,
9824 XFASTINT (charpos), XFASTINT (bytepos));
9825 }
9826
9827 Vwith_echo_area_save_vector = vector;
9828 return Qnil;
9829 }
9830
9831
9832 /* Set up the echo area for use by print functions. MULTIBYTE_P
9833 non-zero means we will print multibyte. */
9834
9835 void
9836 setup_echo_area_for_printing (int multibyte_p)
9837 {
9838 /* If we can't find an echo area any more, exit. */
9839 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9840 Fkill_emacs (Qnil);
9841
9842 ensure_echo_area_buffers ();
9843
9844 if (!message_buf_print)
9845 {
9846 /* A message has been output since the last time we printed.
9847 Choose a fresh echo area buffer. */
9848 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9849 echo_area_buffer[0] = echo_buffer[1];
9850 else
9851 echo_area_buffer[0] = echo_buffer[0];
9852
9853 /* Switch to that buffer and clear it. */
9854 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9855 BVAR (current_buffer, truncate_lines) = Qnil;
9856
9857 if (Z > BEG)
9858 {
9859 ptrdiff_t count = SPECPDL_INDEX ();
9860 specbind (Qinhibit_read_only, Qt);
9861 /* Note that undo recording is always disabled. */
9862 del_range (BEG, Z);
9863 unbind_to (count, Qnil);
9864 }
9865 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9866
9867 /* Set up the buffer for the multibyteness we need. */
9868 if (multibyte_p
9869 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9870 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9871
9872 /* Raise the frame containing the echo area. */
9873 if (minibuffer_auto_raise)
9874 {
9875 struct frame *sf = SELECTED_FRAME ();
9876 Lisp_Object mini_window;
9877 mini_window = FRAME_MINIBUF_WINDOW (sf);
9878 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9879 }
9880
9881 message_log_maybe_newline ();
9882 message_buf_print = 1;
9883 }
9884 else
9885 {
9886 if (NILP (echo_area_buffer[0]))
9887 {
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
9894 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9895 {
9896 /* Someone switched buffers between print requests. */
9897 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9898 BVAR (current_buffer, truncate_lines) = Qnil;
9899 }
9900 }
9901 }
9902
9903
9904 /* Display an echo area message in window W. Value is non-zero if W's
9905 height is changed. If display_last_displayed_message_p is
9906 non-zero, display the message that was last displayed, otherwise
9907 display the current message. */
9908
9909 static int
9910 display_echo_area (struct window *w)
9911 {
9912 int i, no_message_p, window_height_changed_p;
9913
9914 /* Temporarily disable garbage collections while displaying the echo
9915 area. This is done because a GC can print a message itself.
9916 That message would modify the echo area buffer's contents while a
9917 redisplay of the buffer is going on, and seriously confuse
9918 redisplay. */
9919 ptrdiff_t count = inhibit_garbage_collection ();
9920
9921 /* If there is no message, we must call display_echo_area_1
9922 nevertheless because it resizes the window. But we will have to
9923 reset the echo_area_buffer in question to nil at the end because
9924 with_echo_area_buffer will sets it to an empty buffer. */
9925 i = display_last_displayed_message_p ? 1 : 0;
9926 no_message_p = NILP (echo_area_buffer[i]);
9927
9928 window_height_changed_p
9929 = with_echo_area_buffer (w, display_last_displayed_message_p,
9930 display_echo_area_1,
9931 (intptr_t) w, Qnil, 0, 0);
9932
9933 if (no_message_p)
9934 echo_area_buffer[i] = Qnil;
9935
9936 unbind_to (count, Qnil);
9937 return window_height_changed_p;
9938 }
9939
9940
9941 /* Helper for display_echo_area. Display the current buffer which
9942 contains the current echo area message in window W, a mini-window,
9943 a pointer to which is passed in A1. A2..A4 are currently not used.
9944 Change the height of W so that all of the message is displayed.
9945 Value is non-zero if height of W was changed. */
9946
9947 static int
9948 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9949 {
9950 intptr_t i1 = a1;
9951 struct window *w = (struct window *) i1;
9952 Lisp_Object window;
9953 struct text_pos start;
9954 int window_height_changed_p = 0;
9955
9956 /* Do this before displaying, so that we have a large enough glyph
9957 matrix for the display. If we can't get enough space for the
9958 whole text, display the last N lines. That works by setting w->start. */
9959 window_height_changed_p = resize_mini_window (w, 0);
9960
9961 /* Use the starting position chosen by resize_mini_window. */
9962 SET_TEXT_POS_FROM_MARKER (start, w->start);
9963
9964 /* Display. */
9965 clear_glyph_matrix (w->desired_matrix);
9966 XSETWINDOW (window, w);
9967 try_window (window, start, 0);
9968
9969 return window_height_changed_p;
9970 }
9971
9972
9973 /* Resize the echo area window to exactly the size needed for the
9974 currently displayed message, if there is one. If a mini-buffer
9975 is active, don't shrink it. */
9976
9977 void
9978 resize_echo_area_exactly (void)
9979 {
9980 if (BUFFERP (echo_area_buffer[0])
9981 && WINDOWP (echo_area_window))
9982 {
9983 struct window *w = XWINDOW (echo_area_window);
9984 int resized_p;
9985 Lisp_Object resize_exactly;
9986
9987 if (minibuf_level == 0)
9988 resize_exactly = Qt;
9989 else
9990 resize_exactly = Qnil;
9991
9992 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9993 (intptr_t) w, resize_exactly,
9994 0, 0);
9995 if (resized_p)
9996 {
9997 ++windows_or_buffers_changed;
9998 ++update_mode_lines;
9999 redisplay_internal ();
10000 }
10001 }
10002 }
10003
10004
10005 /* Callback function for with_echo_area_buffer, when used from
10006 resize_echo_area_exactly. A1 contains a pointer to the window to
10007 resize, EXACTLY non-nil means resize the mini-window exactly to the
10008 size of the text displayed. A3 and A4 are not used. Value is what
10009 resize_mini_window returns. */
10010
10011 static int
10012 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10013 {
10014 intptr_t i1 = a1;
10015 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10016 }
10017
10018
10019 /* Resize mini-window W to fit the size of its contents. EXACT_P
10020 means size the window exactly to the size needed. Otherwise, it's
10021 only enlarged until W's buffer is empty.
10022
10023 Set W->start to the right place to begin display. If the whole
10024 contents fit, start at the beginning. Otherwise, start so as
10025 to make the end of the contents appear. This is particularly
10026 important for y-or-n-p, but seems desirable generally.
10027
10028 Value is non-zero if the window height has been changed. */
10029
10030 int
10031 resize_mini_window (struct window *w, int exact_p)
10032 {
10033 struct frame *f = XFRAME (w->frame);
10034 int window_height_changed_p = 0;
10035
10036 xassert (MINI_WINDOW_P (w));
10037
10038 /* By default, start display at the beginning. */
10039 set_marker_both (w->start, w->buffer,
10040 BUF_BEGV (XBUFFER (w->buffer)),
10041 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10042
10043 /* Don't resize windows while redisplaying a window; it would
10044 confuse redisplay functions when the size of the window they are
10045 displaying changes from under them. Such a resizing can happen,
10046 for instance, when which-func prints a long message while
10047 we are running fontification-functions. We're running these
10048 functions with safe_call which binds inhibit-redisplay to t. */
10049 if (!NILP (Vinhibit_redisplay))
10050 return 0;
10051
10052 /* Nil means don't try to resize. */
10053 if (NILP (Vresize_mini_windows)
10054 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10055 return 0;
10056
10057 if (!FRAME_MINIBUF_ONLY_P (f))
10058 {
10059 struct it it;
10060 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10061 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10062 int height;
10063 EMACS_INT max_height;
10064 int unit = FRAME_LINE_HEIGHT (f);
10065 struct text_pos start;
10066 struct buffer *old_current_buffer = NULL;
10067
10068 if (current_buffer != XBUFFER (w->buffer))
10069 {
10070 old_current_buffer = current_buffer;
10071 set_buffer_internal (XBUFFER (w->buffer));
10072 }
10073
10074 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10075
10076 /* Compute the max. number of lines specified by the user. */
10077 if (FLOATP (Vmax_mini_window_height))
10078 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10079 else if (INTEGERP (Vmax_mini_window_height))
10080 max_height = XINT (Vmax_mini_window_height);
10081 else
10082 max_height = total_height / 4;
10083
10084 /* Correct that max. height if it's bogus. */
10085 max_height = max (1, max_height);
10086 max_height = min (total_height, max_height);
10087
10088 /* Find out the height of the text in the window. */
10089 if (it.line_wrap == TRUNCATE)
10090 height = 1;
10091 else
10092 {
10093 last_height = 0;
10094 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10095 if (it.max_ascent == 0 && it.max_descent == 0)
10096 height = it.current_y + last_height;
10097 else
10098 height = it.current_y + it.max_ascent + it.max_descent;
10099 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10100 height = (height + unit - 1) / unit;
10101 }
10102
10103 /* Compute a suitable window start. */
10104 if (height > max_height)
10105 {
10106 height = max_height;
10107 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10108 move_it_vertically_backward (&it, (height - 1) * unit);
10109 start = it.current.pos;
10110 }
10111 else
10112 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10113 SET_MARKER_FROM_TEXT_POS (w->start, start);
10114
10115 if (EQ (Vresize_mini_windows, Qgrow_only))
10116 {
10117 /* Let it grow only, until we display an empty message, in which
10118 case the window shrinks again. */
10119 if (height > WINDOW_TOTAL_LINES (w))
10120 {
10121 int old_height = WINDOW_TOTAL_LINES (w);
10122 freeze_window_starts (f, 1);
10123 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10124 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10125 }
10126 else if (height < WINDOW_TOTAL_LINES (w)
10127 && (exact_p || BEGV == ZV))
10128 {
10129 int old_height = WINDOW_TOTAL_LINES (w);
10130 freeze_window_starts (f, 0);
10131 shrink_mini_window (w);
10132 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10133 }
10134 }
10135 else
10136 {
10137 /* Always resize to exact size needed. */
10138 if (height > WINDOW_TOTAL_LINES (w))
10139 {
10140 int old_height = WINDOW_TOTAL_LINES (w);
10141 freeze_window_starts (f, 1);
10142 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10143 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10144 }
10145 else if (height < WINDOW_TOTAL_LINES (w))
10146 {
10147 int old_height = WINDOW_TOTAL_LINES (w);
10148 freeze_window_starts (f, 0);
10149 shrink_mini_window (w);
10150
10151 if (height)
10152 {
10153 freeze_window_starts (f, 1);
10154 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10155 }
10156
10157 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10158 }
10159 }
10160
10161 if (old_current_buffer)
10162 set_buffer_internal (old_current_buffer);
10163 }
10164
10165 return window_height_changed_p;
10166 }
10167
10168
10169 /* Value is the current message, a string, or nil if there is no
10170 current message. */
10171
10172 Lisp_Object
10173 current_message (void)
10174 {
10175 Lisp_Object msg;
10176
10177 if (!BUFFERP (echo_area_buffer[0]))
10178 msg = Qnil;
10179 else
10180 {
10181 with_echo_area_buffer (0, 0, current_message_1,
10182 (intptr_t) &msg, Qnil, 0, 0);
10183 if (NILP (msg))
10184 echo_area_buffer[0] = Qnil;
10185 }
10186
10187 return msg;
10188 }
10189
10190
10191 static int
10192 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10193 {
10194 intptr_t i1 = a1;
10195 Lisp_Object *msg = (Lisp_Object *) i1;
10196
10197 if (Z > BEG)
10198 *msg = make_buffer_string (BEG, Z, 1);
10199 else
10200 *msg = Qnil;
10201 return 0;
10202 }
10203
10204
10205 /* Push the current message on Vmessage_stack for later restauration
10206 by restore_message. Value is non-zero if the current message isn't
10207 empty. This is a relatively infrequent operation, so it's not
10208 worth optimizing. */
10209
10210 int
10211 push_message (void)
10212 {
10213 Lisp_Object msg;
10214 msg = current_message ();
10215 Vmessage_stack = Fcons (msg, Vmessage_stack);
10216 return STRINGP (msg);
10217 }
10218
10219
10220 /* Restore message display from the top of Vmessage_stack. */
10221
10222 void
10223 restore_message (void)
10224 {
10225 Lisp_Object msg;
10226
10227 xassert (CONSP (Vmessage_stack));
10228 msg = XCAR (Vmessage_stack);
10229 if (STRINGP (msg))
10230 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10231 else
10232 message3_nolog (msg, 0, 0);
10233 }
10234
10235
10236 /* Handler for record_unwind_protect calling pop_message. */
10237
10238 Lisp_Object
10239 pop_message_unwind (Lisp_Object dummy)
10240 {
10241 pop_message ();
10242 return Qnil;
10243 }
10244
10245 /* Pop the top-most entry off Vmessage_stack. */
10246
10247 static void
10248 pop_message (void)
10249 {
10250 xassert (CONSP (Vmessage_stack));
10251 Vmessage_stack = XCDR (Vmessage_stack);
10252 }
10253
10254
10255 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10256 exits. If the stack is not empty, we have a missing pop_message
10257 somewhere. */
10258
10259 void
10260 check_message_stack (void)
10261 {
10262 if (!NILP (Vmessage_stack))
10263 abort ();
10264 }
10265
10266
10267 /* Truncate to NCHARS what will be displayed in the echo area the next
10268 time we display it---but don't redisplay it now. */
10269
10270 void
10271 truncate_echo_area (ptrdiff_t nchars)
10272 {
10273 if (nchars == 0)
10274 echo_area_buffer[0] = Qnil;
10275 /* A null message buffer means that the frame hasn't really been
10276 initialized yet. Error messages get reported properly by
10277 cmd_error, so this must be just an informative message; toss it. */
10278 else if (!noninteractive
10279 && INTERACTIVE
10280 && !NILP (echo_area_buffer[0]))
10281 {
10282 struct frame *sf = SELECTED_FRAME ();
10283 if (FRAME_MESSAGE_BUF (sf))
10284 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10285 }
10286 }
10287
10288
10289 /* Helper function for truncate_echo_area. Truncate the current
10290 message to at most NCHARS characters. */
10291
10292 static int
10293 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10294 {
10295 if (BEG + nchars < Z)
10296 del_range (BEG + nchars, Z);
10297 if (Z == BEG)
10298 echo_area_buffer[0] = Qnil;
10299 return 0;
10300 }
10301
10302
10303 /* Set the current message to a substring of S or STRING.
10304
10305 If STRING is a Lisp string, set the message to the first NBYTES
10306 bytes from STRING. NBYTES zero means use the whole string. If
10307 STRING is multibyte, the message will be displayed multibyte.
10308
10309 If S is not null, set the message to the first LEN bytes of S. LEN
10310 zero means use the whole string. MULTIBYTE_P non-zero means S is
10311 multibyte. Display the message multibyte in that case.
10312
10313 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10314 to t before calling set_message_1 (which calls insert).
10315 */
10316
10317 static void
10318 set_message (const char *s, Lisp_Object string,
10319 ptrdiff_t nbytes, int multibyte_p)
10320 {
10321 message_enable_multibyte
10322 = ((s && multibyte_p)
10323 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10324
10325 with_echo_area_buffer (0, -1, set_message_1,
10326 (intptr_t) s, string, nbytes, multibyte_p);
10327 message_buf_print = 0;
10328 help_echo_showing_p = 0;
10329 }
10330
10331
10332 /* Helper function for set_message. Arguments have the same meaning
10333 as there, with A1 corresponding to S and A2 corresponding to STRING
10334 This function is called with the echo area buffer being
10335 current. */
10336
10337 static int
10338 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10339 {
10340 intptr_t i1 = a1;
10341 const char *s = (const char *) i1;
10342 const unsigned char *msg = (const unsigned char *) s;
10343 Lisp_Object string = a2;
10344
10345 /* Change multibyteness of the echo buffer appropriately. */
10346 if (message_enable_multibyte
10347 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10348 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10349
10350 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10351 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10352 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10353
10354 /* Insert new message at BEG. */
10355 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10356
10357 if (STRINGP (string))
10358 {
10359 ptrdiff_t nchars;
10360
10361 if (nbytes == 0)
10362 nbytes = SBYTES (string);
10363 nchars = string_byte_to_char (string, nbytes);
10364
10365 /* This function takes care of single/multibyte conversion. We
10366 just have to ensure that the echo area buffer has the right
10367 setting of enable_multibyte_characters. */
10368 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10369 }
10370 else if (s)
10371 {
10372 if (nbytes == 0)
10373 nbytes = strlen (s);
10374
10375 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10376 {
10377 /* Convert from multi-byte to single-byte. */
10378 ptrdiff_t i;
10379 int c, n;
10380 char work[1];
10381
10382 /* Convert a multibyte string to single-byte. */
10383 for (i = 0; i < nbytes; i += n)
10384 {
10385 c = string_char_and_length (msg + i, &n);
10386 work[0] = (ASCII_CHAR_P (c)
10387 ? c
10388 : multibyte_char_to_unibyte (c));
10389 insert_1_both (work, 1, 1, 1, 0, 0);
10390 }
10391 }
10392 else if (!multibyte_p
10393 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10394 {
10395 /* Convert from single-byte to multi-byte. */
10396 ptrdiff_t i;
10397 int c, n;
10398 unsigned char str[MAX_MULTIBYTE_LENGTH];
10399
10400 /* Convert a single-byte string to multibyte. */
10401 for (i = 0; i < nbytes; i++)
10402 {
10403 c = msg[i];
10404 MAKE_CHAR_MULTIBYTE (c);
10405 n = CHAR_STRING (c, str);
10406 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10407 }
10408 }
10409 else
10410 insert_1 (s, nbytes, 1, 0, 0);
10411 }
10412
10413 return 0;
10414 }
10415
10416
10417 /* Clear messages. CURRENT_P non-zero means clear the current
10418 message. LAST_DISPLAYED_P non-zero means clear the message
10419 last displayed. */
10420
10421 void
10422 clear_message (int current_p, int last_displayed_p)
10423 {
10424 if (current_p)
10425 {
10426 echo_area_buffer[0] = Qnil;
10427 message_cleared_p = 1;
10428 }
10429
10430 if (last_displayed_p)
10431 echo_area_buffer[1] = Qnil;
10432
10433 message_buf_print = 0;
10434 }
10435
10436 /* Clear garbaged frames.
10437
10438 This function is used where the old redisplay called
10439 redraw_garbaged_frames which in turn called redraw_frame which in
10440 turn called clear_frame. The call to clear_frame was a source of
10441 flickering. I believe a clear_frame is not necessary. It should
10442 suffice in the new redisplay to invalidate all current matrices,
10443 and ensure a complete redisplay of all windows. */
10444
10445 static void
10446 clear_garbaged_frames (void)
10447 {
10448 if (frame_garbaged)
10449 {
10450 Lisp_Object tail, frame;
10451 int changed_count = 0;
10452
10453 FOR_EACH_FRAME (tail, frame)
10454 {
10455 struct frame *f = XFRAME (frame);
10456
10457 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10458 {
10459 if (f->resized_p)
10460 {
10461 Fredraw_frame (frame);
10462 f->force_flush_display_p = 1;
10463 }
10464 clear_current_matrices (f);
10465 changed_count++;
10466 f->garbaged = 0;
10467 f->resized_p = 0;
10468 }
10469 }
10470
10471 frame_garbaged = 0;
10472 if (changed_count)
10473 ++windows_or_buffers_changed;
10474 }
10475 }
10476
10477
10478 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10479 is non-zero update selected_frame. Value is non-zero if the
10480 mini-windows height has been changed. */
10481
10482 static int
10483 echo_area_display (int update_frame_p)
10484 {
10485 Lisp_Object mini_window;
10486 struct window *w;
10487 struct frame *f;
10488 int window_height_changed_p = 0;
10489 struct frame *sf = SELECTED_FRAME ();
10490
10491 mini_window = FRAME_MINIBUF_WINDOW (sf);
10492 w = XWINDOW (mini_window);
10493 f = XFRAME (WINDOW_FRAME (w));
10494
10495 /* Don't display if frame is invisible or not yet initialized. */
10496 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10497 return 0;
10498
10499 #ifdef HAVE_WINDOW_SYSTEM
10500 /* When Emacs starts, selected_frame may be the initial terminal
10501 frame. If we let this through, a message would be displayed on
10502 the terminal. */
10503 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10504 return 0;
10505 #endif /* HAVE_WINDOW_SYSTEM */
10506
10507 /* Redraw garbaged frames. */
10508 if (frame_garbaged)
10509 clear_garbaged_frames ();
10510
10511 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10512 {
10513 echo_area_window = mini_window;
10514 window_height_changed_p = display_echo_area (w);
10515 w->must_be_updated_p = 1;
10516
10517 /* Update the display, unless called from redisplay_internal.
10518 Also don't update the screen during redisplay itself. The
10519 update will happen at the end of redisplay, and an update
10520 here could cause confusion. */
10521 if (update_frame_p && !redisplaying_p)
10522 {
10523 int n = 0;
10524
10525 /* If the display update has been interrupted by pending
10526 input, update mode lines in the frame. Due to the
10527 pending input, it might have been that redisplay hasn't
10528 been called, so that mode lines above the echo area are
10529 garbaged. This looks odd, so we prevent it here. */
10530 if (!display_completed)
10531 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10532
10533 if (window_height_changed_p
10534 /* Don't do this if Emacs is shutting down. Redisplay
10535 needs to run hooks. */
10536 && !NILP (Vrun_hooks))
10537 {
10538 /* Must update other windows. Likewise as in other
10539 cases, don't let this update be interrupted by
10540 pending input. */
10541 ptrdiff_t count = SPECPDL_INDEX ();
10542 specbind (Qredisplay_dont_pause, Qt);
10543 windows_or_buffers_changed = 1;
10544 redisplay_internal ();
10545 unbind_to (count, Qnil);
10546 }
10547 else if (FRAME_WINDOW_P (f) && n == 0)
10548 {
10549 /* Window configuration is the same as before.
10550 Can do with a display update of the echo area,
10551 unless we displayed some mode lines. */
10552 update_single_window (w, 1);
10553 FRAME_RIF (f)->flush_display (f);
10554 }
10555 else
10556 update_frame (f, 1, 1);
10557
10558 /* If cursor is in the echo area, make sure that the next
10559 redisplay displays the minibuffer, so that the cursor will
10560 be replaced with what the minibuffer wants. */
10561 if (cursor_in_echo_area)
10562 ++windows_or_buffers_changed;
10563 }
10564 }
10565 else if (!EQ (mini_window, selected_window))
10566 windows_or_buffers_changed++;
10567
10568 /* Last displayed message is now the current message. */
10569 echo_area_buffer[1] = echo_area_buffer[0];
10570 /* Inform read_char that we're not echoing. */
10571 echo_message_buffer = Qnil;
10572
10573 /* Prevent redisplay optimization in redisplay_internal by resetting
10574 this_line_start_pos. This is done because the mini-buffer now
10575 displays the message instead of its buffer text. */
10576 if (EQ (mini_window, selected_window))
10577 CHARPOS (this_line_start_pos) = 0;
10578
10579 return window_height_changed_p;
10580 }
10581
10582
10583 \f
10584 /***********************************************************************
10585 Mode Lines and Frame Titles
10586 ***********************************************************************/
10587
10588 /* A buffer for constructing non-propertized mode-line strings and
10589 frame titles in it; allocated from the heap in init_xdisp and
10590 resized as needed in store_mode_line_noprop_char. */
10591
10592 static char *mode_line_noprop_buf;
10593
10594 /* The buffer's end, and a current output position in it. */
10595
10596 static char *mode_line_noprop_buf_end;
10597 static char *mode_line_noprop_ptr;
10598
10599 #define MODE_LINE_NOPROP_LEN(start) \
10600 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10601
10602 static enum {
10603 MODE_LINE_DISPLAY = 0,
10604 MODE_LINE_TITLE,
10605 MODE_LINE_NOPROP,
10606 MODE_LINE_STRING
10607 } mode_line_target;
10608
10609 /* Alist that caches the results of :propertize.
10610 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10611 static Lisp_Object mode_line_proptrans_alist;
10612
10613 /* List of strings making up the mode-line. */
10614 static Lisp_Object mode_line_string_list;
10615
10616 /* Base face property when building propertized mode line string. */
10617 static Lisp_Object mode_line_string_face;
10618 static Lisp_Object mode_line_string_face_prop;
10619
10620
10621 /* Unwind data for mode line strings */
10622
10623 static Lisp_Object Vmode_line_unwind_vector;
10624
10625 static Lisp_Object
10626 format_mode_line_unwind_data (struct buffer *obuf,
10627 Lisp_Object owin,
10628 int save_proptrans)
10629 {
10630 Lisp_Object vector, tmp;
10631
10632 /* Reduce consing by keeping one vector in
10633 Vwith_echo_area_save_vector. */
10634 vector = Vmode_line_unwind_vector;
10635 Vmode_line_unwind_vector = Qnil;
10636
10637 if (NILP (vector))
10638 vector = Fmake_vector (make_number (8), Qnil);
10639
10640 ASET (vector, 0, make_number (mode_line_target));
10641 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10642 ASET (vector, 2, mode_line_string_list);
10643 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10644 ASET (vector, 4, mode_line_string_face);
10645 ASET (vector, 5, mode_line_string_face_prop);
10646
10647 if (obuf)
10648 XSETBUFFER (tmp, obuf);
10649 else
10650 tmp = Qnil;
10651 ASET (vector, 6, tmp);
10652 ASET (vector, 7, owin);
10653
10654 return vector;
10655 }
10656
10657 static Lisp_Object
10658 unwind_format_mode_line (Lisp_Object vector)
10659 {
10660 mode_line_target = XINT (AREF (vector, 0));
10661 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10662 mode_line_string_list = AREF (vector, 2);
10663 if (! EQ (AREF (vector, 3), Qt))
10664 mode_line_proptrans_alist = AREF (vector, 3);
10665 mode_line_string_face = AREF (vector, 4);
10666 mode_line_string_face_prop = AREF (vector, 5);
10667
10668 if (!NILP (AREF (vector, 7)))
10669 /* Select window before buffer, since it may change the buffer. */
10670 Fselect_window (AREF (vector, 7), Qt);
10671
10672 if (!NILP (AREF (vector, 6)))
10673 {
10674 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10675 ASET (vector, 6, Qnil);
10676 }
10677
10678 Vmode_line_unwind_vector = vector;
10679 return Qnil;
10680 }
10681
10682
10683 /* Store a single character C for the frame title in mode_line_noprop_buf.
10684 Re-allocate mode_line_noprop_buf if necessary. */
10685
10686 static void
10687 store_mode_line_noprop_char (char c)
10688 {
10689 /* If output position has reached the end of the allocated buffer,
10690 increase the buffer's size. */
10691 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10692 {
10693 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10694 ptrdiff_t size = len;
10695 mode_line_noprop_buf =
10696 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10697 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10698 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10699 }
10700
10701 *mode_line_noprop_ptr++ = c;
10702 }
10703
10704
10705 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10706 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10707 characters that yield more columns than PRECISION; PRECISION <= 0
10708 means copy the whole string. Pad with spaces until FIELD_WIDTH
10709 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10710 pad. Called from display_mode_element when it is used to build a
10711 frame title. */
10712
10713 static int
10714 store_mode_line_noprop (const char *string, int field_width, int precision)
10715 {
10716 const unsigned char *str = (const unsigned char *) string;
10717 int n = 0;
10718 ptrdiff_t dummy, nbytes;
10719
10720 /* Copy at most PRECISION chars from STR. */
10721 nbytes = strlen (string);
10722 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10723 while (nbytes--)
10724 store_mode_line_noprop_char (*str++);
10725
10726 /* Fill up with spaces until FIELD_WIDTH reached. */
10727 while (field_width > 0
10728 && n < field_width)
10729 {
10730 store_mode_line_noprop_char (' ');
10731 ++n;
10732 }
10733
10734 return n;
10735 }
10736
10737 /***********************************************************************
10738 Frame Titles
10739 ***********************************************************************/
10740
10741 #ifdef HAVE_WINDOW_SYSTEM
10742
10743 /* Set the title of FRAME, if it has changed. The title format is
10744 Vicon_title_format if FRAME is iconified, otherwise it is
10745 frame_title_format. */
10746
10747 static void
10748 x_consider_frame_title (Lisp_Object frame)
10749 {
10750 struct frame *f = XFRAME (frame);
10751
10752 if (FRAME_WINDOW_P (f)
10753 || FRAME_MINIBUF_ONLY_P (f)
10754 || f->explicit_name)
10755 {
10756 /* Do we have more than one visible frame on this X display? */
10757 Lisp_Object tail;
10758 Lisp_Object fmt;
10759 ptrdiff_t title_start;
10760 char *title;
10761 ptrdiff_t len;
10762 struct it it;
10763 ptrdiff_t count = SPECPDL_INDEX ();
10764
10765 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10766 {
10767 Lisp_Object other_frame = XCAR (tail);
10768 struct frame *tf = XFRAME (other_frame);
10769
10770 if (tf != f
10771 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10772 && !FRAME_MINIBUF_ONLY_P (tf)
10773 && !EQ (other_frame, tip_frame)
10774 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10775 break;
10776 }
10777
10778 /* Set global variable indicating that multiple frames exist. */
10779 multiple_frames = CONSP (tail);
10780
10781 /* Switch to the buffer of selected window of the frame. Set up
10782 mode_line_target so that display_mode_element will output into
10783 mode_line_noprop_buf; then display the title. */
10784 record_unwind_protect (unwind_format_mode_line,
10785 format_mode_line_unwind_data
10786 (current_buffer, selected_window, 0));
10787
10788 Fselect_window (f->selected_window, Qt);
10789 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10790 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10791
10792 mode_line_target = MODE_LINE_TITLE;
10793 title_start = MODE_LINE_NOPROP_LEN (0);
10794 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10795 NULL, DEFAULT_FACE_ID);
10796 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10797 len = MODE_LINE_NOPROP_LEN (title_start);
10798 title = mode_line_noprop_buf + title_start;
10799 unbind_to (count, Qnil);
10800
10801 /* Set the title only if it's changed. This avoids consing in
10802 the common case where it hasn't. (If it turns out that we've
10803 already wasted too much time by walking through the list with
10804 display_mode_element, then we might need to optimize at a
10805 higher level than this.) */
10806 if (! STRINGP (f->name)
10807 || SBYTES (f->name) != len
10808 || memcmp (title, SDATA (f->name), len) != 0)
10809 x_implicitly_set_name (f, make_string (title, len), Qnil);
10810 }
10811 }
10812
10813 #endif /* not HAVE_WINDOW_SYSTEM */
10814
10815
10816
10817 \f
10818 /***********************************************************************
10819 Menu Bars
10820 ***********************************************************************/
10821
10822
10823 /* Prepare for redisplay by updating menu-bar item lists when
10824 appropriate. This can call eval. */
10825
10826 void
10827 prepare_menu_bars (void)
10828 {
10829 int all_windows;
10830 struct gcpro gcpro1, gcpro2;
10831 struct frame *f;
10832 Lisp_Object tooltip_frame;
10833
10834 #ifdef HAVE_WINDOW_SYSTEM
10835 tooltip_frame = tip_frame;
10836 #else
10837 tooltip_frame = Qnil;
10838 #endif
10839
10840 /* Update all frame titles based on their buffer names, etc. We do
10841 this before the menu bars so that the buffer-menu will show the
10842 up-to-date frame titles. */
10843 #ifdef HAVE_WINDOW_SYSTEM
10844 if (windows_or_buffers_changed || update_mode_lines)
10845 {
10846 Lisp_Object tail, frame;
10847
10848 FOR_EACH_FRAME (tail, frame)
10849 {
10850 f = XFRAME (frame);
10851 if (!EQ (frame, tooltip_frame)
10852 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10853 x_consider_frame_title (frame);
10854 }
10855 }
10856 #endif /* HAVE_WINDOW_SYSTEM */
10857
10858 /* Update the menu bar item lists, if appropriate. This has to be
10859 done before any actual redisplay or generation of display lines. */
10860 all_windows = (update_mode_lines
10861 || buffer_shared > 1
10862 || windows_or_buffers_changed);
10863 if (all_windows)
10864 {
10865 Lisp_Object tail, frame;
10866 ptrdiff_t count = SPECPDL_INDEX ();
10867 /* 1 means that update_menu_bar has run its hooks
10868 so any further calls to update_menu_bar shouldn't do so again. */
10869 int menu_bar_hooks_run = 0;
10870
10871 record_unwind_save_match_data ();
10872
10873 FOR_EACH_FRAME (tail, frame)
10874 {
10875 f = XFRAME (frame);
10876
10877 /* Ignore tooltip frame. */
10878 if (EQ (frame, tooltip_frame))
10879 continue;
10880
10881 /* If a window on this frame changed size, report that to
10882 the user and clear the size-change flag. */
10883 if (FRAME_WINDOW_SIZES_CHANGED (f))
10884 {
10885 Lisp_Object functions;
10886
10887 /* Clear flag first in case we get an error below. */
10888 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10889 functions = Vwindow_size_change_functions;
10890 GCPRO2 (tail, functions);
10891
10892 while (CONSP (functions))
10893 {
10894 if (!EQ (XCAR (functions), Qt))
10895 call1 (XCAR (functions), frame);
10896 functions = XCDR (functions);
10897 }
10898 UNGCPRO;
10899 }
10900
10901 GCPRO1 (tail);
10902 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10903 #ifdef HAVE_WINDOW_SYSTEM
10904 update_tool_bar (f, 0);
10905 #endif
10906 #ifdef HAVE_NS
10907 if (windows_or_buffers_changed
10908 && FRAME_NS_P (f))
10909 ns_set_doc_edited (f, Fbuffer_modified_p
10910 (XWINDOW (f->selected_window)->buffer));
10911 #endif
10912 UNGCPRO;
10913 }
10914
10915 unbind_to (count, Qnil);
10916 }
10917 else
10918 {
10919 struct frame *sf = SELECTED_FRAME ();
10920 update_menu_bar (sf, 1, 0);
10921 #ifdef HAVE_WINDOW_SYSTEM
10922 update_tool_bar (sf, 1);
10923 #endif
10924 }
10925 }
10926
10927
10928 /* Update the menu bar item list for frame F. This has to be done
10929 before we start to fill in any display lines, because it can call
10930 eval.
10931
10932 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10933
10934 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10935 already ran the menu bar hooks for this redisplay, so there
10936 is no need to run them again. The return value is the
10937 updated value of this flag, to pass to the next call. */
10938
10939 static int
10940 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10941 {
10942 Lisp_Object window;
10943 register struct window *w;
10944
10945 /* If called recursively during a menu update, do nothing. This can
10946 happen when, for instance, an activate-menubar-hook causes a
10947 redisplay. */
10948 if (inhibit_menubar_update)
10949 return hooks_run;
10950
10951 window = FRAME_SELECTED_WINDOW (f);
10952 w = XWINDOW (window);
10953
10954 if (FRAME_WINDOW_P (f)
10955 ?
10956 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10957 || defined (HAVE_NS) || defined (USE_GTK)
10958 FRAME_EXTERNAL_MENU_BAR (f)
10959 #else
10960 FRAME_MENU_BAR_LINES (f) > 0
10961 #endif
10962 : FRAME_MENU_BAR_LINES (f) > 0)
10963 {
10964 /* If the user has switched buffers or windows, we need to
10965 recompute to reflect the new bindings. But we'll
10966 recompute when update_mode_lines is set too; that means
10967 that people can use force-mode-line-update to request
10968 that the menu bar be recomputed. The adverse effect on
10969 the rest of the redisplay algorithm is about the same as
10970 windows_or_buffers_changed anyway. */
10971 if (windows_or_buffers_changed
10972 /* This used to test w->update_mode_line, but we believe
10973 there is no need to recompute the menu in that case. */
10974 || update_mode_lines
10975 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10976 < BUF_MODIFF (XBUFFER (w->buffer)))
10977 != !NILP (w->last_had_star))
10978 || ((!NILP (Vtransient_mark_mode)
10979 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10980 != !NILP (w->region_showing)))
10981 {
10982 struct buffer *prev = current_buffer;
10983 ptrdiff_t count = SPECPDL_INDEX ();
10984
10985 specbind (Qinhibit_menubar_update, Qt);
10986
10987 set_buffer_internal_1 (XBUFFER (w->buffer));
10988 if (save_match_data)
10989 record_unwind_save_match_data ();
10990 if (NILP (Voverriding_local_map_menu_flag))
10991 {
10992 specbind (Qoverriding_terminal_local_map, Qnil);
10993 specbind (Qoverriding_local_map, Qnil);
10994 }
10995
10996 if (!hooks_run)
10997 {
10998 /* Run the Lucid hook. */
10999 safe_run_hooks (Qactivate_menubar_hook);
11000
11001 /* If it has changed current-menubar from previous value,
11002 really recompute the menu-bar from the value. */
11003 if (! NILP (Vlucid_menu_bar_dirty_flag))
11004 call0 (Qrecompute_lucid_menubar);
11005
11006 safe_run_hooks (Qmenu_bar_update_hook);
11007
11008 hooks_run = 1;
11009 }
11010
11011 XSETFRAME (Vmenu_updating_frame, f);
11012 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11013
11014 /* Redisplay the menu bar in case we changed it. */
11015 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11016 || defined (HAVE_NS) || defined (USE_GTK)
11017 if (FRAME_WINDOW_P (f))
11018 {
11019 #if defined (HAVE_NS)
11020 /* All frames on Mac OS share the same menubar. So only
11021 the selected frame should be allowed to set it. */
11022 if (f == SELECTED_FRAME ())
11023 #endif
11024 set_frame_menubar (f, 0, 0);
11025 }
11026 else
11027 /* On a terminal screen, the menu bar is an ordinary screen
11028 line, and this makes it get updated. */
11029 w->update_mode_line = Qt;
11030 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11031 /* In the non-toolkit version, the menu bar is an ordinary screen
11032 line, and this makes it get updated. */
11033 w->update_mode_line = Qt;
11034 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11035
11036 unbind_to (count, Qnil);
11037 set_buffer_internal_1 (prev);
11038 }
11039 }
11040
11041 return hooks_run;
11042 }
11043
11044
11045 \f
11046 /***********************************************************************
11047 Output Cursor
11048 ***********************************************************************/
11049
11050 #ifdef HAVE_WINDOW_SYSTEM
11051
11052 /* EXPORT:
11053 Nominal cursor position -- where to draw output.
11054 HPOS and VPOS are window relative glyph matrix coordinates.
11055 X and Y are window relative pixel coordinates. */
11056
11057 struct cursor_pos output_cursor;
11058
11059
11060 /* EXPORT:
11061 Set the global variable output_cursor to CURSOR. All cursor
11062 positions are relative to updated_window. */
11063
11064 void
11065 set_output_cursor (struct cursor_pos *cursor)
11066 {
11067 output_cursor.hpos = cursor->hpos;
11068 output_cursor.vpos = cursor->vpos;
11069 output_cursor.x = cursor->x;
11070 output_cursor.y = cursor->y;
11071 }
11072
11073
11074 /* EXPORT for RIF:
11075 Set a nominal cursor position.
11076
11077 HPOS and VPOS are column/row positions in a window glyph matrix. X
11078 and Y are window text area relative pixel positions.
11079
11080 If this is done during an update, updated_window will contain the
11081 window that is being updated and the position is the future output
11082 cursor position for that window. If updated_window is null, use
11083 selected_window and display the cursor at the given position. */
11084
11085 void
11086 x_cursor_to (int vpos, int hpos, int y, int x)
11087 {
11088 struct window *w;
11089
11090 /* If updated_window is not set, work on selected_window. */
11091 if (updated_window)
11092 w = updated_window;
11093 else
11094 w = XWINDOW (selected_window);
11095
11096 /* Set the output cursor. */
11097 output_cursor.hpos = hpos;
11098 output_cursor.vpos = vpos;
11099 output_cursor.x = x;
11100 output_cursor.y = y;
11101
11102 /* If not called as part of an update, really display the cursor.
11103 This will also set the cursor position of W. */
11104 if (updated_window == NULL)
11105 {
11106 BLOCK_INPUT;
11107 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11108 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11109 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11110 UNBLOCK_INPUT;
11111 }
11112 }
11113
11114 #endif /* HAVE_WINDOW_SYSTEM */
11115
11116 \f
11117 /***********************************************************************
11118 Tool-bars
11119 ***********************************************************************/
11120
11121 #ifdef HAVE_WINDOW_SYSTEM
11122
11123 /* Where the mouse was last time we reported a mouse event. */
11124
11125 FRAME_PTR last_mouse_frame;
11126
11127 /* Tool-bar item index of the item on which a mouse button was pressed
11128 or -1. */
11129
11130 int last_tool_bar_item;
11131
11132
11133 static Lisp_Object
11134 update_tool_bar_unwind (Lisp_Object frame)
11135 {
11136 selected_frame = frame;
11137 return Qnil;
11138 }
11139
11140 /* Update the tool-bar item list for frame F. This has to be done
11141 before we start to fill in any display lines. Called from
11142 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11143 and restore it here. */
11144
11145 static void
11146 update_tool_bar (struct frame *f, int save_match_data)
11147 {
11148 #if defined (USE_GTK) || defined (HAVE_NS)
11149 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11150 #else
11151 int do_update = WINDOWP (f->tool_bar_window)
11152 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11153 #endif
11154
11155 if (do_update)
11156 {
11157 Lisp_Object window;
11158 struct window *w;
11159
11160 window = FRAME_SELECTED_WINDOW (f);
11161 w = XWINDOW (window);
11162
11163 /* If the user has switched buffers or windows, we need to
11164 recompute to reflect the new bindings. But we'll
11165 recompute when update_mode_lines is set too; that means
11166 that people can use force-mode-line-update to request
11167 that the menu bar be recomputed. The adverse effect on
11168 the rest of the redisplay algorithm is about the same as
11169 windows_or_buffers_changed anyway. */
11170 if (windows_or_buffers_changed
11171 || !NILP (w->update_mode_line)
11172 || update_mode_lines
11173 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11174 < BUF_MODIFF (XBUFFER (w->buffer)))
11175 != !NILP (w->last_had_star))
11176 || ((!NILP (Vtransient_mark_mode)
11177 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11178 != !NILP (w->region_showing)))
11179 {
11180 struct buffer *prev = current_buffer;
11181 ptrdiff_t count = SPECPDL_INDEX ();
11182 Lisp_Object frame, new_tool_bar;
11183 int new_n_tool_bar;
11184 struct gcpro gcpro1;
11185
11186 /* Set current_buffer to the buffer of the selected
11187 window of the frame, so that we get the right local
11188 keymaps. */
11189 set_buffer_internal_1 (XBUFFER (w->buffer));
11190
11191 /* Save match data, if we must. */
11192 if (save_match_data)
11193 record_unwind_save_match_data ();
11194
11195 /* Make sure that we don't accidentally use bogus keymaps. */
11196 if (NILP (Voverriding_local_map_menu_flag))
11197 {
11198 specbind (Qoverriding_terminal_local_map, Qnil);
11199 specbind (Qoverriding_local_map, Qnil);
11200 }
11201
11202 GCPRO1 (new_tool_bar);
11203
11204 /* We must temporarily set the selected frame to this frame
11205 before calling tool_bar_items, because the calculation of
11206 the tool-bar keymap uses the selected frame (see
11207 `tool-bar-make-keymap' in tool-bar.el). */
11208 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11209 XSETFRAME (frame, f);
11210 selected_frame = frame;
11211
11212 /* Build desired tool-bar items from keymaps. */
11213 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11214 &new_n_tool_bar);
11215
11216 /* Redisplay the tool-bar if we changed it. */
11217 if (new_n_tool_bar != f->n_tool_bar_items
11218 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11219 {
11220 /* Redisplay that happens asynchronously due to an expose event
11221 may access f->tool_bar_items. Make sure we update both
11222 variables within BLOCK_INPUT so no such event interrupts. */
11223 BLOCK_INPUT;
11224 f->tool_bar_items = new_tool_bar;
11225 f->n_tool_bar_items = new_n_tool_bar;
11226 w->update_mode_line = Qt;
11227 UNBLOCK_INPUT;
11228 }
11229
11230 UNGCPRO;
11231
11232 unbind_to (count, Qnil);
11233 set_buffer_internal_1 (prev);
11234 }
11235 }
11236 }
11237
11238
11239 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11240 F's desired tool-bar contents. F->tool_bar_items must have
11241 been set up previously by calling prepare_menu_bars. */
11242
11243 static void
11244 build_desired_tool_bar_string (struct frame *f)
11245 {
11246 int i, size, size_needed;
11247 struct gcpro gcpro1, gcpro2, gcpro3;
11248 Lisp_Object image, plist, props;
11249
11250 image = plist = props = Qnil;
11251 GCPRO3 (image, plist, props);
11252
11253 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11254 Otherwise, make a new string. */
11255
11256 /* The size of the string we might be able to reuse. */
11257 size = (STRINGP (f->desired_tool_bar_string)
11258 ? SCHARS (f->desired_tool_bar_string)
11259 : 0);
11260
11261 /* We need one space in the string for each image. */
11262 size_needed = f->n_tool_bar_items;
11263
11264 /* Reuse f->desired_tool_bar_string, if possible. */
11265 if (size < size_needed || NILP (f->desired_tool_bar_string))
11266 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11267 make_number (' '));
11268 else
11269 {
11270 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11271 Fremove_text_properties (make_number (0), make_number (size),
11272 props, f->desired_tool_bar_string);
11273 }
11274
11275 /* Put a `display' property on the string for the images to display,
11276 put a `menu_item' property on tool-bar items with a value that
11277 is the index of the item in F's tool-bar item vector. */
11278 for (i = 0; i < f->n_tool_bar_items; ++i)
11279 {
11280 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11281
11282 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11283 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11284 int hmargin, vmargin, relief, idx, end;
11285
11286 /* If image is a vector, choose the image according to the
11287 button state. */
11288 image = PROP (TOOL_BAR_ITEM_IMAGES);
11289 if (VECTORP (image))
11290 {
11291 if (enabled_p)
11292 idx = (selected_p
11293 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11294 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11295 else
11296 idx = (selected_p
11297 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11298 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11299
11300 xassert (ASIZE (image) >= idx);
11301 image = AREF (image, idx);
11302 }
11303 else
11304 idx = -1;
11305
11306 /* Ignore invalid image specifications. */
11307 if (!valid_image_p (image))
11308 continue;
11309
11310 /* Display the tool-bar button pressed, or depressed. */
11311 plist = Fcopy_sequence (XCDR (image));
11312
11313 /* Compute margin and relief to draw. */
11314 relief = (tool_bar_button_relief >= 0
11315 ? tool_bar_button_relief
11316 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11317 hmargin = vmargin = relief;
11318
11319 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11320 INT_MAX - max (hmargin, vmargin)))
11321 {
11322 hmargin += XFASTINT (Vtool_bar_button_margin);
11323 vmargin += XFASTINT (Vtool_bar_button_margin);
11324 }
11325 else if (CONSP (Vtool_bar_button_margin))
11326 {
11327 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11328 INT_MAX - hmargin))
11329 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11330
11331 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11332 INT_MAX - vmargin))
11333 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11334 }
11335
11336 if (auto_raise_tool_bar_buttons_p)
11337 {
11338 /* Add a `:relief' property to the image spec if the item is
11339 selected. */
11340 if (selected_p)
11341 {
11342 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11343 hmargin -= relief;
11344 vmargin -= relief;
11345 }
11346 }
11347 else
11348 {
11349 /* If image is selected, display it pressed, i.e. with a
11350 negative relief. If it's not selected, display it with a
11351 raised relief. */
11352 plist = Fplist_put (plist, QCrelief,
11353 (selected_p
11354 ? make_number (-relief)
11355 : make_number (relief)));
11356 hmargin -= relief;
11357 vmargin -= relief;
11358 }
11359
11360 /* Put a margin around the image. */
11361 if (hmargin || vmargin)
11362 {
11363 if (hmargin == vmargin)
11364 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11365 else
11366 plist = Fplist_put (plist, QCmargin,
11367 Fcons (make_number (hmargin),
11368 make_number (vmargin)));
11369 }
11370
11371 /* If button is not enabled, and we don't have special images
11372 for the disabled state, make the image appear disabled by
11373 applying an appropriate algorithm to it. */
11374 if (!enabled_p && idx < 0)
11375 plist = Fplist_put (plist, QCconversion, Qdisabled);
11376
11377 /* Put a `display' text property on the string for the image to
11378 display. Put a `menu-item' property on the string that gives
11379 the start of this item's properties in the tool-bar items
11380 vector. */
11381 image = Fcons (Qimage, plist);
11382 props = list4 (Qdisplay, image,
11383 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11384
11385 /* Let the last image hide all remaining spaces in the tool bar
11386 string. The string can be longer than needed when we reuse a
11387 previous string. */
11388 if (i + 1 == f->n_tool_bar_items)
11389 end = SCHARS (f->desired_tool_bar_string);
11390 else
11391 end = i + 1;
11392 Fadd_text_properties (make_number (i), make_number (end),
11393 props, f->desired_tool_bar_string);
11394 #undef PROP
11395 }
11396
11397 UNGCPRO;
11398 }
11399
11400
11401 /* Display one line of the tool-bar of frame IT->f.
11402
11403 HEIGHT specifies the desired height of the tool-bar line.
11404 If the actual height of the glyph row is less than HEIGHT, the
11405 row's height is increased to HEIGHT, and the icons are centered
11406 vertically in the new height.
11407
11408 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11409 count a final empty row in case the tool-bar width exactly matches
11410 the window width.
11411 */
11412
11413 static void
11414 display_tool_bar_line (struct it *it, int height)
11415 {
11416 struct glyph_row *row = it->glyph_row;
11417 int max_x = it->last_visible_x;
11418 struct glyph *last;
11419
11420 prepare_desired_row (row);
11421 row->y = it->current_y;
11422
11423 /* Note that this isn't made use of if the face hasn't a box,
11424 so there's no need to check the face here. */
11425 it->start_of_box_run_p = 1;
11426
11427 while (it->current_x < max_x)
11428 {
11429 int x, n_glyphs_before, i, nglyphs;
11430 struct it it_before;
11431
11432 /* Get the next display element. */
11433 if (!get_next_display_element (it))
11434 {
11435 /* Don't count empty row if we are counting needed tool-bar lines. */
11436 if (height < 0 && !it->hpos)
11437 return;
11438 break;
11439 }
11440
11441 /* Produce glyphs. */
11442 n_glyphs_before = row->used[TEXT_AREA];
11443 it_before = *it;
11444
11445 PRODUCE_GLYPHS (it);
11446
11447 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11448 i = 0;
11449 x = it_before.current_x;
11450 while (i < nglyphs)
11451 {
11452 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11453
11454 if (x + glyph->pixel_width > max_x)
11455 {
11456 /* Glyph doesn't fit on line. Backtrack. */
11457 row->used[TEXT_AREA] = n_glyphs_before;
11458 *it = it_before;
11459 /* If this is the only glyph on this line, it will never fit on the
11460 tool-bar, so skip it. But ensure there is at least one glyph,
11461 so we don't accidentally disable the tool-bar. */
11462 if (n_glyphs_before == 0
11463 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11464 break;
11465 goto out;
11466 }
11467
11468 ++it->hpos;
11469 x += glyph->pixel_width;
11470 ++i;
11471 }
11472
11473 /* Stop at line end. */
11474 if (ITERATOR_AT_END_OF_LINE_P (it))
11475 break;
11476
11477 set_iterator_to_next (it, 1);
11478 }
11479
11480 out:;
11481
11482 row->displays_text_p = row->used[TEXT_AREA] != 0;
11483
11484 /* Use default face for the border below the tool bar.
11485
11486 FIXME: When auto-resize-tool-bars is grow-only, there is
11487 no additional border below the possibly empty tool-bar lines.
11488 So to make the extra empty lines look "normal", we have to
11489 use the tool-bar face for the border too. */
11490 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11491 it->face_id = DEFAULT_FACE_ID;
11492
11493 extend_face_to_end_of_line (it);
11494 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11495 last->right_box_line_p = 1;
11496 if (last == row->glyphs[TEXT_AREA])
11497 last->left_box_line_p = 1;
11498
11499 /* Make line the desired height and center it vertically. */
11500 if ((height -= it->max_ascent + it->max_descent) > 0)
11501 {
11502 /* Don't add more than one line height. */
11503 height %= FRAME_LINE_HEIGHT (it->f);
11504 it->max_ascent += height / 2;
11505 it->max_descent += (height + 1) / 2;
11506 }
11507
11508 compute_line_metrics (it);
11509
11510 /* If line is empty, make it occupy the rest of the tool-bar. */
11511 if (!row->displays_text_p)
11512 {
11513 row->height = row->phys_height = it->last_visible_y - row->y;
11514 row->visible_height = row->height;
11515 row->ascent = row->phys_ascent = 0;
11516 row->extra_line_spacing = 0;
11517 }
11518
11519 row->full_width_p = 1;
11520 row->continued_p = 0;
11521 row->truncated_on_left_p = 0;
11522 row->truncated_on_right_p = 0;
11523
11524 it->current_x = it->hpos = 0;
11525 it->current_y += row->height;
11526 ++it->vpos;
11527 ++it->glyph_row;
11528 }
11529
11530
11531 /* Max tool-bar height. */
11532
11533 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11534 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11535
11536 /* Value is the number of screen lines needed to make all tool-bar
11537 items of frame F visible. The number of actual rows needed is
11538 returned in *N_ROWS if non-NULL. */
11539
11540 static int
11541 tool_bar_lines_needed (struct frame *f, int *n_rows)
11542 {
11543 struct window *w = XWINDOW (f->tool_bar_window);
11544 struct it it;
11545 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11546 the desired matrix, so use (unused) mode-line row as temporary row to
11547 avoid destroying the first tool-bar row. */
11548 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11549
11550 /* Initialize an iterator for iteration over
11551 F->desired_tool_bar_string in the tool-bar window of frame F. */
11552 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11553 it.first_visible_x = 0;
11554 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11555 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11556 it.paragraph_embedding = L2R;
11557
11558 while (!ITERATOR_AT_END_P (&it))
11559 {
11560 clear_glyph_row (temp_row);
11561 it.glyph_row = temp_row;
11562 display_tool_bar_line (&it, -1);
11563 }
11564 clear_glyph_row (temp_row);
11565
11566 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11567 if (n_rows)
11568 *n_rows = it.vpos > 0 ? it.vpos : -1;
11569
11570 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11571 }
11572
11573
11574 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11575 0, 1, 0,
11576 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11577 (Lisp_Object frame)
11578 {
11579 struct frame *f;
11580 struct window *w;
11581 int nlines = 0;
11582
11583 if (NILP (frame))
11584 frame = selected_frame;
11585 else
11586 CHECK_FRAME (frame);
11587 f = XFRAME (frame);
11588
11589 if (WINDOWP (f->tool_bar_window)
11590 && (w = XWINDOW (f->tool_bar_window),
11591 WINDOW_TOTAL_LINES (w) > 0))
11592 {
11593 update_tool_bar (f, 1);
11594 if (f->n_tool_bar_items)
11595 {
11596 build_desired_tool_bar_string (f);
11597 nlines = tool_bar_lines_needed (f, NULL);
11598 }
11599 }
11600
11601 return make_number (nlines);
11602 }
11603
11604
11605 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11606 height should be changed. */
11607
11608 static int
11609 redisplay_tool_bar (struct frame *f)
11610 {
11611 struct window *w;
11612 struct it it;
11613 struct glyph_row *row;
11614
11615 #if defined (USE_GTK) || defined (HAVE_NS)
11616 if (FRAME_EXTERNAL_TOOL_BAR (f))
11617 update_frame_tool_bar (f);
11618 return 0;
11619 #endif
11620
11621 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11622 do anything. This means you must start with tool-bar-lines
11623 non-zero to get the auto-sizing effect. Or in other words, you
11624 can turn off tool-bars by specifying tool-bar-lines zero. */
11625 if (!WINDOWP (f->tool_bar_window)
11626 || (w = XWINDOW (f->tool_bar_window),
11627 WINDOW_TOTAL_LINES (w) == 0))
11628 return 0;
11629
11630 /* Set up an iterator for the tool-bar window. */
11631 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11632 it.first_visible_x = 0;
11633 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11634 row = it.glyph_row;
11635
11636 /* Build a string that represents the contents of the tool-bar. */
11637 build_desired_tool_bar_string (f);
11638 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11639 /* FIXME: This should be controlled by a user option. But it
11640 doesn't make sense to have an R2L tool bar if the menu bar cannot
11641 be drawn also R2L, and making the menu bar R2L is tricky due
11642 toolkit-specific code that implements it. If an R2L tool bar is
11643 ever supported, display_tool_bar_line should also be augmented to
11644 call unproduce_glyphs like display_line and display_string
11645 do. */
11646 it.paragraph_embedding = L2R;
11647
11648 if (f->n_tool_bar_rows == 0)
11649 {
11650 int nlines;
11651
11652 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11653 nlines != WINDOW_TOTAL_LINES (w)))
11654 {
11655 Lisp_Object frame;
11656 int old_height = WINDOW_TOTAL_LINES (w);
11657
11658 XSETFRAME (frame, f);
11659 Fmodify_frame_parameters (frame,
11660 Fcons (Fcons (Qtool_bar_lines,
11661 make_number (nlines)),
11662 Qnil));
11663 if (WINDOW_TOTAL_LINES (w) != old_height)
11664 {
11665 clear_glyph_matrix (w->desired_matrix);
11666 fonts_changed_p = 1;
11667 return 1;
11668 }
11669 }
11670 }
11671
11672 /* Display as many lines as needed to display all tool-bar items. */
11673
11674 if (f->n_tool_bar_rows > 0)
11675 {
11676 int border, rows, height, extra;
11677
11678 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11679 border = XINT (Vtool_bar_border);
11680 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11681 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11682 else if (EQ (Vtool_bar_border, Qborder_width))
11683 border = f->border_width;
11684 else
11685 border = 0;
11686 if (border < 0)
11687 border = 0;
11688
11689 rows = f->n_tool_bar_rows;
11690 height = max (1, (it.last_visible_y - border) / rows);
11691 extra = it.last_visible_y - border - height * rows;
11692
11693 while (it.current_y < it.last_visible_y)
11694 {
11695 int h = 0;
11696 if (extra > 0 && rows-- > 0)
11697 {
11698 h = (extra + rows - 1) / rows;
11699 extra -= h;
11700 }
11701 display_tool_bar_line (&it, height + h);
11702 }
11703 }
11704 else
11705 {
11706 while (it.current_y < it.last_visible_y)
11707 display_tool_bar_line (&it, 0);
11708 }
11709
11710 /* It doesn't make much sense to try scrolling in the tool-bar
11711 window, so don't do it. */
11712 w->desired_matrix->no_scrolling_p = 1;
11713 w->must_be_updated_p = 1;
11714
11715 if (!NILP (Vauto_resize_tool_bars))
11716 {
11717 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11718 int change_height_p = 0;
11719
11720 /* If we couldn't display everything, change the tool-bar's
11721 height if there is room for more. */
11722 if (IT_STRING_CHARPOS (it) < it.end_charpos
11723 && it.current_y < max_tool_bar_height)
11724 change_height_p = 1;
11725
11726 row = it.glyph_row - 1;
11727
11728 /* If there are blank lines at the end, except for a partially
11729 visible blank line at the end that is smaller than
11730 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11731 if (!row->displays_text_p
11732 && row->height >= FRAME_LINE_HEIGHT (f))
11733 change_height_p = 1;
11734
11735 /* If row displays tool-bar items, but is partially visible,
11736 change the tool-bar's height. */
11737 if (row->displays_text_p
11738 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11739 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11740 change_height_p = 1;
11741
11742 /* Resize windows as needed by changing the `tool-bar-lines'
11743 frame parameter. */
11744 if (change_height_p)
11745 {
11746 Lisp_Object frame;
11747 int old_height = WINDOW_TOTAL_LINES (w);
11748 int nrows;
11749 int nlines = tool_bar_lines_needed (f, &nrows);
11750
11751 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11752 && !f->minimize_tool_bar_window_p)
11753 ? (nlines > old_height)
11754 : (nlines != old_height));
11755 f->minimize_tool_bar_window_p = 0;
11756
11757 if (change_height_p)
11758 {
11759 XSETFRAME (frame, f);
11760 Fmodify_frame_parameters (frame,
11761 Fcons (Fcons (Qtool_bar_lines,
11762 make_number (nlines)),
11763 Qnil));
11764 if (WINDOW_TOTAL_LINES (w) != old_height)
11765 {
11766 clear_glyph_matrix (w->desired_matrix);
11767 f->n_tool_bar_rows = nrows;
11768 fonts_changed_p = 1;
11769 return 1;
11770 }
11771 }
11772 }
11773 }
11774
11775 f->minimize_tool_bar_window_p = 0;
11776 return 0;
11777 }
11778
11779
11780 /* Get information about the tool-bar item which is displayed in GLYPH
11781 on frame F. Return in *PROP_IDX the index where tool-bar item
11782 properties start in F->tool_bar_items. Value is zero if
11783 GLYPH doesn't display a tool-bar item. */
11784
11785 static int
11786 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11787 {
11788 Lisp_Object prop;
11789 int success_p;
11790 int charpos;
11791
11792 /* This function can be called asynchronously, which means we must
11793 exclude any possibility that Fget_text_property signals an
11794 error. */
11795 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11796 charpos = max (0, charpos);
11797
11798 /* Get the text property `menu-item' at pos. The value of that
11799 property is the start index of this item's properties in
11800 F->tool_bar_items. */
11801 prop = Fget_text_property (make_number (charpos),
11802 Qmenu_item, f->current_tool_bar_string);
11803 if (INTEGERP (prop))
11804 {
11805 *prop_idx = XINT (prop);
11806 success_p = 1;
11807 }
11808 else
11809 success_p = 0;
11810
11811 return success_p;
11812 }
11813
11814 \f
11815 /* Get information about the tool-bar item at position X/Y on frame F.
11816 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11817 the current matrix of the tool-bar window of F, or NULL if not
11818 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11819 item in F->tool_bar_items. Value is
11820
11821 -1 if X/Y is not on a tool-bar item
11822 0 if X/Y is on the same item that was highlighted before.
11823 1 otherwise. */
11824
11825 static int
11826 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11827 int *hpos, int *vpos, int *prop_idx)
11828 {
11829 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11830 struct window *w = XWINDOW (f->tool_bar_window);
11831 int area;
11832
11833 /* Find the glyph under X/Y. */
11834 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11835 if (*glyph == NULL)
11836 return -1;
11837
11838 /* Get the start of this tool-bar item's properties in
11839 f->tool_bar_items. */
11840 if (!tool_bar_item_info (f, *glyph, prop_idx))
11841 return -1;
11842
11843 /* Is mouse on the highlighted item? */
11844 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11845 && *vpos >= hlinfo->mouse_face_beg_row
11846 && *vpos <= hlinfo->mouse_face_end_row
11847 && (*vpos > hlinfo->mouse_face_beg_row
11848 || *hpos >= hlinfo->mouse_face_beg_col)
11849 && (*vpos < hlinfo->mouse_face_end_row
11850 || *hpos < hlinfo->mouse_face_end_col
11851 || hlinfo->mouse_face_past_end))
11852 return 0;
11853
11854 return 1;
11855 }
11856
11857
11858 /* EXPORT:
11859 Handle mouse button event on the tool-bar of frame F, at
11860 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11861 0 for button release. MODIFIERS is event modifiers for button
11862 release. */
11863
11864 void
11865 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11866 int modifiers)
11867 {
11868 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11869 struct window *w = XWINDOW (f->tool_bar_window);
11870 int hpos, vpos, prop_idx;
11871 struct glyph *glyph;
11872 Lisp_Object enabled_p;
11873
11874 /* If not on the highlighted tool-bar item, return. */
11875 frame_to_window_pixel_xy (w, &x, &y);
11876 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11877 return;
11878
11879 /* If item is disabled, do nothing. */
11880 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11881 if (NILP (enabled_p))
11882 return;
11883
11884 if (down_p)
11885 {
11886 /* Show item in pressed state. */
11887 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11888 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11889 last_tool_bar_item = prop_idx;
11890 }
11891 else
11892 {
11893 Lisp_Object key, frame;
11894 struct input_event event;
11895 EVENT_INIT (event);
11896
11897 /* Show item in released state. */
11898 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11899 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11900
11901 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11902
11903 XSETFRAME (frame, f);
11904 event.kind = TOOL_BAR_EVENT;
11905 event.frame_or_window = frame;
11906 event.arg = frame;
11907 kbd_buffer_store_event (&event);
11908
11909 event.kind = TOOL_BAR_EVENT;
11910 event.frame_or_window = frame;
11911 event.arg = key;
11912 event.modifiers = modifiers;
11913 kbd_buffer_store_event (&event);
11914 last_tool_bar_item = -1;
11915 }
11916 }
11917
11918
11919 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11920 tool-bar window-relative coordinates X/Y. Called from
11921 note_mouse_highlight. */
11922
11923 static void
11924 note_tool_bar_highlight (struct frame *f, int x, int y)
11925 {
11926 Lisp_Object window = f->tool_bar_window;
11927 struct window *w = XWINDOW (window);
11928 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11929 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11930 int hpos, vpos;
11931 struct glyph *glyph;
11932 struct glyph_row *row;
11933 int i;
11934 Lisp_Object enabled_p;
11935 int prop_idx;
11936 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11937 int mouse_down_p, rc;
11938
11939 /* Function note_mouse_highlight is called with negative X/Y
11940 values when mouse moves outside of the frame. */
11941 if (x <= 0 || y <= 0)
11942 {
11943 clear_mouse_face (hlinfo);
11944 return;
11945 }
11946
11947 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11948 if (rc < 0)
11949 {
11950 /* Not on tool-bar item. */
11951 clear_mouse_face (hlinfo);
11952 return;
11953 }
11954 else if (rc == 0)
11955 /* On same tool-bar item as before. */
11956 goto set_help_echo;
11957
11958 clear_mouse_face (hlinfo);
11959
11960 /* Mouse is down, but on different tool-bar item? */
11961 mouse_down_p = (dpyinfo->grabbed
11962 && f == last_mouse_frame
11963 && FRAME_LIVE_P (f));
11964 if (mouse_down_p
11965 && last_tool_bar_item != prop_idx)
11966 return;
11967
11968 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11969 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11970
11971 /* If tool-bar item is not enabled, don't highlight it. */
11972 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11973 if (!NILP (enabled_p))
11974 {
11975 /* Compute the x-position of the glyph. In front and past the
11976 image is a space. We include this in the highlighted area. */
11977 row = MATRIX_ROW (w->current_matrix, vpos);
11978 for (i = x = 0; i < hpos; ++i)
11979 x += row->glyphs[TEXT_AREA][i].pixel_width;
11980
11981 /* Record this as the current active region. */
11982 hlinfo->mouse_face_beg_col = hpos;
11983 hlinfo->mouse_face_beg_row = vpos;
11984 hlinfo->mouse_face_beg_x = x;
11985 hlinfo->mouse_face_beg_y = row->y;
11986 hlinfo->mouse_face_past_end = 0;
11987
11988 hlinfo->mouse_face_end_col = hpos + 1;
11989 hlinfo->mouse_face_end_row = vpos;
11990 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11991 hlinfo->mouse_face_end_y = row->y;
11992 hlinfo->mouse_face_window = window;
11993 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11994
11995 /* Display it as active. */
11996 show_mouse_face (hlinfo, draw);
11997 hlinfo->mouse_face_image_state = draw;
11998 }
11999
12000 set_help_echo:
12001
12002 /* Set help_echo_string to a help string to display for this tool-bar item.
12003 XTread_socket does the rest. */
12004 help_echo_object = help_echo_window = Qnil;
12005 help_echo_pos = -1;
12006 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12007 if (NILP (help_echo_string))
12008 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12009 }
12010
12011 #endif /* HAVE_WINDOW_SYSTEM */
12012
12013
12014 \f
12015 /************************************************************************
12016 Horizontal scrolling
12017 ************************************************************************/
12018
12019 static int hscroll_window_tree (Lisp_Object);
12020 static int hscroll_windows (Lisp_Object);
12021
12022 /* For all leaf windows in the window tree rooted at WINDOW, set their
12023 hscroll value so that PT is (i) visible in the window, and (ii) so
12024 that it is not within a certain margin at the window's left and
12025 right border. Value is non-zero if any window's hscroll has been
12026 changed. */
12027
12028 static int
12029 hscroll_window_tree (Lisp_Object window)
12030 {
12031 int hscrolled_p = 0;
12032 int hscroll_relative_p = FLOATP (Vhscroll_step);
12033 int hscroll_step_abs = 0;
12034 double hscroll_step_rel = 0;
12035
12036 if (hscroll_relative_p)
12037 {
12038 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12039 if (hscroll_step_rel < 0)
12040 {
12041 hscroll_relative_p = 0;
12042 hscroll_step_abs = 0;
12043 }
12044 }
12045 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12046 {
12047 hscroll_step_abs = XINT (Vhscroll_step);
12048 if (hscroll_step_abs < 0)
12049 hscroll_step_abs = 0;
12050 }
12051 else
12052 hscroll_step_abs = 0;
12053
12054 while (WINDOWP (window))
12055 {
12056 struct window *w = XWINDOW (window);
12057
12058 if (WINDOWP (w->hchild))
12059 hscrolled_p |= hscroll_window_tree (w->hchild);
12060 else if (WINDOWP (w->vchild))
12061 hscrolled_p |= hscroll_window_tree (w->vchild);
12062 else if (w->cursor.vpos >= 0)
12063 {
12064 int h_margin;
12065 int text_area_width;
12066 struct glyph_row *current_cursor_row
12067 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12068 struct glyph_row *desired_cursor_row
12069 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12070 struct glyph_row *cursor_row
12071 = (desired_cursor_row->enabled_p
12072 ? desired_cursor_row
12073 : current_cursor_row);
12074 int row_r2l_p = cursor_row->reversed_p;
12075
12076 text_area_width = window_box_width (w, TEXT_AREA);
12077
12078 /* Scroll when cursor is inside this scroll margin. */
12079 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12080
12081 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12082 /* For left-to-right rows, hscroll when cursor is either
12083 (i) inside the right hscroll margin, or (ii) if it is
12084 inside the left margin and the window is already
12085 hscrolled. */
12086 && ((!row_r2l_p
12087 && ((XFASTINT (w->hscroll)
12088 && w->cursor.x <= h_margin)
12089 || (cursor_row->enabled_p
12090 && cursor_row->truncated_on_right_p
12091 && (w->cursor.x >= text_area_width - h_margin))))
12092 /* For right-to-left rows, the logic is similar,
12093 except that rules for scrolling to left and right
12094 are reversed. E.g., if cursor.x <= h_margin, we
12095 need to hscroll "to the right" unconditionally,
12096 and that will scroll the screen to the left so as
12097 to reveal the next portion of the row. */
12098 || (row_r2l_p
12099 && ((cursor_row->enabled_p
12100 /* FIXME: It is confusing to set the
12101 truncated_on_right_p flag when R2L rows
12102 are actually truncated on the left. */
12103 && cursor_row->truncated_on_right_p
12104 && w->cursor.x <= h_margin)
12105 || (XFASTINT (w->hscroll)
12106 && (w->cursor.x >= text_area_width - h_margin))))))
12107 {
12108 struct it it;
12109 ptrdiff_t hscroll;
12110 struct buffer *saved_current_buffer;
12111 ptrdiff_t pt;
12112 int wanted_x;
12113
12114 /* Find point in a display of infinite width. */
12115 saved_current_buffer = current_buffer;
12116 current_buffer = XBUFFER (w->buffer);
12117
12118 if (w == XWINDOW (selected_window))
12119 pt = PT;
12120 else
12121 {
12122 pt = marker_position (w->pointm);
12123 pt = max (BEGV, pt);
12124 pt = min (ZV, pt);
12125 }
12126
12127 /* Move iterator to pt starting at cursor_row->start in
12128 a line with infinite width. */
12129 init_to_row_start (&it, w, cursor_row);
12130 it.last_visible_x = INFINITY;
12131 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12132 current_buffer = saved_current_buffer;
12133
12134 /* Position cursor in window. */
12135 if (!hscroll_relative_p && hscroll_step_abs == 0)
12136 hscroll = max (0, (it.current_x
12137 - (ITERATOR_AT_END_OF_LINE_P (&it)
12138 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12139 : (text_area_width / 2))))
12140 / FRAME_COLUMN_WIDTH (it.f);
12141 else if ((!row_r2l_p
12142 && w->cursor.x >= text_area_width - h_margin)
12143 || (row_r2l_p && w->cursor.x <= h_margin))
12144 {
12145 if (hscroll_relative_p)
12146 wanted_x = text_area_width * (1 - hscroll_step_rel)
12147 - h_margin;
12148 else
12149 wanted_x = text_area_width
12150 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12151 - h_margin;
12152 hscroll
12153 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12154 }
12155 else
12156 {
12157 if (hscroll_relative_p)
12158 wanted_x = text_area_width * hscroll_step_rel
12159 + h_margin;
12160 else
12161 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12162 + h_margin;
12163 hscroll
12164 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12165 }
12166 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12167
12168 /* Don't prevent redisplay optimizations if hscroll
12169 hasn't changed, as it will unnecessarily slow down
12170 redisplay. */
12171 if (XFASTINT (w->hscroll) != hscroll)
12172 {
12173 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12174 w->hscroll = make_number (hscroll);
12175 hscrolled_p = 1;
12176 }
12177 }
12178 }
12179
12180 window = w->next;
12181 }
12182
12183 /* Value is non-zero if hscroll of any leaf window has been changed. */
12184 return hscrolled_p;
12185 }
12186
12187
12188 /* Set hscroll so that cursor is visible and not inside horizontal
12189 scroll margins for all windows in the tree rooted at WINDOW. See
12190 also hscroll_window_tree above. Value is non-zero if any window's
12191 hscroll has been changed. If it has, desired matrices on the frame
12192 of WINDOW are cleared. */
12193
12194 static int
12195 hscroll_windows (Lisp_Object window)
12196 {
12197 int hscrolled_p = hscroll_window_tree (window);
12198 if (hscrolled_p)
12199 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12200 return hscrolled_p;
12201 }
12202
12203
12204 \f
12205 /************************************************************************
12206 Redisplay
12207 ************************************************************************/
12208
12209 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12210 to a non-zero value. This is sometimes handy to have in a debugger
12211 session. */
12212
12213 #if GLYPH_DEBUG
12214
12215 /* First and last unchanged row for try_window_id. */
12216
12217 static int debug_first_unchanged_at_end_vpos;
12218 static int debug_last_unchanged_at_beg_vpos;
12219
12220 /* Delta vpos and y. */
12221
12222 static int debug_dvpos, debug_dy;
12223
12224 /* Delta in characters and bytes for try_window_id. */
12225
12226 static ptrdiff_t debug_delta, debug_delta_bytes;
12227
12228 /* Values of window_end_pos and window_end_vpos at the end of
12229 try_window_id. */
12230
12231 static ptrdiff_t debug_end_vpos;
12232
12233 /* Append a string to W->desired_matrix->method. FMT is a printf
12234 format string. If trace_redisplay_p is non-zero also printf the
12235 resulting string to stderr. */
12236
12237 static void debug_method_add (struct window *, char const *, ...)
12238 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12239
12240 static void
12241 debug_method_add (struct window *w, char const *fmt, ...)
12242 {
12243 char buffer[512];
12244 char *method = w->desired_matrix->method;
12245 int len = strlen (method);
12246 int size = sizeof w->desired_matrix->method;
12247 int remaining = size - len - 1;
12248 va_list ap;
12249
12250 va_start (ap, fmt);
12251 vsprintf (buffer, fmt, ap);
12252 va_end (ap);
12253 if (len && remaining)
12254 {
12255 method[len] = '|';
12256 --remaining, ++len;
12257 }
12258
12259 strncpy (method + len, buffer, remaining);
12260
12261 if (trace_redisplay_p)
12262 fprintf (stderr, "%p (%s): %s\n",
12263 w,
12264 ((BUFFERP (w->buffer)
12265 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12266 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12267 : "no buffer"),
12268 buffer);
12269 }
12270
12271 #endif /* GLYPH_DEBUG */
12272
12273
12274 /* Value is non-zero if all changes in window W, which displays
12275 current_buffer, are in the text between START and END. START is a
12276 buffer position, END is given as a distance from Z. Used in
12277 redisplay_internal for display optimization. */
12278
12279 static inline int
12280 text_outside_line_unchanged_p (struct window *w,
12281 ptrdiff_t start, ptrdiff_t end)
12282 {
12283 int unchanged_p = 1;
12284
12285 /* If text or overlays have changed, see where. */
12286 if (XFASTINT (w->last_modified) < MODIFF
12287 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12288 {
12289 /* Gap in the line? */
12290 if (GPT < start || Z - GPT < end)
12291 unchanged_p = 0;
12292
12293 /* Changes start in front of the line, or end after it? */
12294 if (unchanged_p
12295 && (BEG_UNCHANGED < start - 1
12296 || END_UNCHANGED < end))
12297 unchanged_p = 0;
12298
12299 /* If selective display, can't optimize if changes start at the
12300 beginning of the line. */
12301 if (unchanged_p
12302 && INTEGERP (BVAR (current_buffer, selective_display))
12303 && XINT (BVAR (current_buffer, selective_display)) > 0
12304 && (BEG_UNCHANGED < start || GPT <= start))
12305 unchanged_p = 0;
12306
12307 /* If there are overlays at the start or end of the line, these
12308 may have overlay strings with newlines in them. A change at
12309 START, for instance, may actually concern the display of such
12310 overlay strings as well, and they are displayed on different
12311 lines. So, quickly rule out this case. (For the future, it
12312 might be desirable to implement something more telling than
12313 just BEG/END_UNCHANGED.) */
12314 if (unchanged_p)
12315 {
12316 if (BEG + BEG_UNCHANGED == start
12317 && overlay_touches_p (start))
12318 unchanged_p = 0;
12319 if (END_UNCHANGED == end
12320 && overlay_touches_p (Z - end))
12321 unchanged_p = 0;
12322 }
12323
12324 /* Under bidi reordering, adding or deleting a character in the
12325 beginning of a paragraph, before the first strong directional
12326 character, can change the base direction of the paragraph (unless
12327 the buffer specifies a fixed paragraph direction), which will
12328 require to redisplay the whole paragraph. It might be worthwhile
12329 to find the paragraph limits and widen the range of redisplayed
12330 lines to that, but for now just give up this optimization. */
12331 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12332 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12333 unchanged_p = 0;
12334 }
12335
12336 return unchanged_p;
12337 }
12338
12339
12340 /* Do a frame update, taking possible shortcuts into account. This is
12341 the main external entry point for redisplay.
12342
12343 If the last redisplay displayed an echo area message and that message
12344 is no longer requested, we clear the echo area or bring back the
12345 mini-buffer if that is in use. */
12346
12347 void
12348 redisplay (void)
12349 {
12350 redisplay_internal ();
12351 }
12352
12353
12354 static Lisp_Object
12355 overlay_arrow_string_or_property (Lisp_Object var)
12356 {
12357 Lisp_Object val;
12358
12359 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12360 return val;
12361
12362 return Voverlay_arrow_string;
12363 }
12364
12365 /* Return 1 if there are any overlay-arrows in current_buffer. */
12366 static int
12367 overlay_arrow_in_current_buffer_p (void)
12368 {
12369 Lisp_Object vlist;
12370
12371 for (vlist = Voverlay_arrow_variable_list;
12372 CONSP (vlist);
12373 vlist = XCDR (vlist))
12374 {
12375 Lisp_Object var = XCAR (vlist);
12376 Lisp_Object val;
12377
12378 if (!SYMBOLP (var))
12379 continue;
12380 val = find_symbol_value (var);
12381 if (MARKERP (val)
12382 && current_buffer == XMARKER (val)->buffer)
12383 return 1;
12384 }
12385 return 0;
12386 }
12387
12388
12389 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12390 has changed. */
12391
12392 static int
12393 overlay_arrows_changed_p (void)
12394 {
12395 Lisp_Object vlist;
12396
12397 for (vlist = Voverlay_arrow_variable_list;
12398 CONSP (vlist);
12399 vlist = XCDR (vlist))
12400 {
12401 Lisp_Object var = XCAR (vlist);
12402 Lisp_Object val, pstr;
12403
12404 if (!SYMBOLP (var))
12405 continue;
12406 val = find_symbol_value (var);
12407 if (!MARKERP (val))
12408 continue;
12409 if (! EQ (COERCE_MARKER (val),
12410 Fget (var, Qlast_arrow_position))
12411 || ! (pstr = overlay_arrow_string_or_property (var),
12412 EQ (pstr, Fget (var, Qlast_arrow_string))))
12413 return 1;
12414 }
12415 return 0;
12416 }
12417
12418 /* Mark overlay arrows to be updated on next redisplay. */
12419
12420 static void
12421 update_overlay_arrows (int up_to_date)
12422 {
12423 Lisp_Object vlist;
12424
12425 for (vlist = Voverlay_arrow_variable_list;
12426 CONSP (vlist);
12427 vlist = XCDR (vlist))
12428 {
12429 Lisp_Object var = XCAR (vlist);
12430
12431 if (!SYMBOLP (var))
12432 continue;
12433
12434 if (up_to_date > 0)
12435 {
12436 Lisp_Object val = find_symbol_value (var);
12437 Fput (var, Qlast_arrow_position,
12438 COERCE_MARKER (val));
12439 Fput (var, Qlast_arrow_string,
12440 overlay_arrow_string_or_property (var));
12441 }
12442 else if (up_to_date < 0
12443 || !NILP (Fget (var, Qlast_arrow_position)))
12444 {
12445 Fput (var, Qlast_arrow_position, Qt);
12446 Fput (var, Qlast_arrow_string, Qt);
12447 }
12448 }
12449 }
12450
12451
12452 /* Return overlay arrow string to display at row.
12453 Return integer (bitmap number) for arrow bitmap in left fringe.
12454 Return nil if no overlay arrow. */
12455
12456 static Lisp_Object
12457 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12458 {
12459 Lisp_Object vlist;
12460
12461 for (vlist = Voverlay_arrow_variable_list;
12462 CONSP (vlist);
12463 vlist = XCDR (vlist))
12464 {
12465 Lisp_Object var = XCAR (vlist);
12466 Lisp_Object val;
12467
12468 if (!SYMBOLP (var))
12469 continue;
12470
12471 val = find_symbol_value (var);
12472
12473 if (MARKERP (val)
12474 && current_buffer == XMARKER (val)->buffer
12475 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12476 {
12477 if (FRAME_WINDOW_P (it->f)
12478 /* FIXME: if ROW->reversed_p is set, this should test
12479 the right fringe, not the left one. */
12480 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12481 {
12482 #ifdef HAVE_WINDOW_SYSTEM
12483 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12484 {
12485 int fringe_bitmap;
12486 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12487 return make_number (fringe_bitmap);
12488 }
12489 #endif
12490 return make_number (-1); /* Use default arrow bitmap */
12491 }
12492 return overlay_arrow_string_or_property (var);
12493 }
12494 }
12495
12496 return Qnil;
12497 }
12498
12499 /* Return 1 if point moved out of or into a composition. Otherwise
12500 return 0. PREV_BUF and PREV_PT are the last point buffer and
12501 position. BUF and PT are the current point buffer and position. */
12502
12503 static int
12504 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12505 struct buffer *buf, ptrdiff_t pt)
12506 {
12507 ptrdiff_t start, end;
12508 Lisp_Object prop;
12509 Lisp_Object buffer;
12510
12511 XSETBUFFER (buffer, buf);
12512 /* Check a composition at the last point if point moved within the
12513 same buffer. */
12514 if (prev_buf == buf)
12515 {
12516 if (prev_pt == pt)
12517 /* Point didn't move. */
12518 return 0;
12519
12520 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12521 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12522 && COMPOSITION_VALID_P (start, end, prop)
12523 && start < prev_pt && end > prev_pt)
12524 /* The last point was within the composition. Return 1 iff
12525 point moved out of the composition. */
12526 return (pt <= start || pt >= end);
12527 }
12528
12529 /* Check a composition at the current point. */
12530 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12531 && find_composition (pt, -1, &start, &end, &prop, buffer)
12532 && COMPOSITION_VALID_P (start, end, prop)
12533 && start < pt && end > pt);
12534 }
12535
12536
12537 /* Reconsider the setting of B->clip_changed which is displayed
12538 in window W. */
12539
12540 static inline void
12541 reconsider_clip_changes (struct window *w, struct buffer *b)
12542 {
12543 if (b->clip_changed
12544 && !NILP (w->window_end_valid)
12545 && w->current_matrix->buffer == b
12546 && w->current_matrix->zv == BUF_ZV (b)
12547 && w->current_matrix->begv == BUF_BEGV (b))
12548 b->clip_changed = 0;
12549
12550 /* If display wasn't paused, and W is not a tool bar window, see if
12551 point has been moved into or out of a composition. In that case,
12552 we set b->clip_changed to 1 to force updating the screen. If
12553 b->clip_changed has already been set to 1, we can skip this
12554 check. */
12555 if (!b->clip_changed
12556 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12557 {
12558 ptrdiff_t pt;
12559
12560 if (w == XWINDOW (selected_window))
12561 pt = PT;
12562 else
12563 pt = marker_position (w->pointm);
12564
12565 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12566 || pt != XINT (w->last_point))
12567 && check_point_in_composition (w->current_matrix->buffer,
12568 XINT (w->last_point),
12569 XBUFFER (w->buffer), pt))
12570 b->clip_changed = 1;
12571 }
12572 }
12573 \f
12574
12575 /* Select FRAME to forward the values of frame-local variables into C
12576 variables so that the redisplay routines can access those values
12577 directly. */
12578
12579 static void
12580 select_frame_for_redisplay (Lisp_Object frame)
12581 {
12582 Lisp_Object tail, tem;
12583 Lisp_Object old = selected_frame;
12584 struct Lisp_Symbol *sym;
12585
12586 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12587
12588 selected_frame = frame;
12589
12590 do {
12591 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12592 if (CONSP (XCAR (tail))
12593 && (tem = XCAR (XCAR (tail)),
12594 SYMBOLP (tem))
12595 && (sym = indirect_variable (XSYMBOL (tem)),
12596 sym->redirect == SYMBOL_LOCALIZED)
12597 && sym->val.blv->frame_local)
12598 /* Use find_symbol_value rather than Fsymbol_value
12599 to avoid an error if it is void. */
12600 find_symbol_value (tem);
12601 } while (!EQ (frame, old) && (frame = old, 1));
12602 }
12603
12604
12605 #define STOP_POLLING \
12606 do { if (! polling_stopped_here) stop_polling (); \
12607 polling_stopped_here = 1; } while (0)
12608
12609 #define RESUME_POLLING \
12610 do { if (polling_stopped_here) start_polling (); \
12611 polling_stopped_here = 0; } while (0)
12612
12613
12614 /* Perhaps in the future avoid recentering windows if it
12615 is not necessary; currently that causes some problems. */
12616
12617 static void
12618 redisplay_internal (void)
12619 {
12620 struct window *w = XWINDOW (selected_window);
12621 struct window *sw;
12622 struct frame *fr;
12623 int pending;
12624 int must_finish = 0;
12625 struct text_pos tlbufpos, tlendpos;
12626 int number_of_visible_frames;
12627 ptrdiff_t count, count1;
12628 struct frame *sf;
12629 int polling_stopped_here = 0;
12630 Lisp_Object old_frame = selected_frame;
12631
12632 /* Non-zero means redisplay has to consider all windows on all
12633 frames. Zero means, only selected_window is considered. */
12634 int consider_all_windows_p;
12635
12636 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12637
12638 /* No redisplay if running in batch mode or frame is not yet fully
12639 initialized, or redisplay is explicitly turned off by setting
12640 Vinhibit_redisplay. */
12641 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12642 || !NILP (Vinhibit_redisplay))
12643 return;
12644
12645 /* Don't examine these until after testing Vinhibit_redisplay.
12646 When Emacs is shutting down, perhaps because its connection to
12647 X has dropped, we should not look at them at all. */
12648 fr = XFRAME (w->frame);
12649 sf = SELECTED_FRAME ();
12650
12651 if (!fr->glyphs_initialized_p)
12652 return;
12653
12654 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12655 if (popup_activated ())
12656 return;
12657 #endif
12658
12659 /* I don't think this happens but let's be paranoid. */
12660 if (redisplaying_p)
12661 return;
12662
12663 /* Record a function that resets redisplaying_p to its old value
12664 when we leave this function. */
12665 count = SPECPDL_INDEX ();
12666 record_unwind_protect (unwind_redisplay,
12667 Fcons (make_number (redisplaying_p), selected_frame));
12668 ++redisplaying_p;
12669 specbind (Qinhibit_free_realized_faces, Qnil);
12670
12671 {
12672 Lisp_Object tail, frame;
12673
12674 FOR_EACH_FRAME (tail, frame)
12675 {
12676 struct frame *f = XFRAME (frame);
12677 f->already_hscrolled_p = 0;
12678 }
12679 }
12680
12681 retry:
12682 /* Remember the currently selected window. */
12683 sw = w;
12684
12685 if (!EQ (old_frame, selected_frame)
12686 && FRAME_LIVE_P (XFRAME (old_frame)))
12687 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12688 selected_frame and selected_window to be temporarily out-of-sync so
12689 when we come back here via `goto retry', we need to resync because we
12690 may need to run Elisp code (via prepare_menu_bars). */
12691 select_frame_for_redisplay (old_frame);
12692
12693 pending = 0;
12694 reconsider_clip_changes (w, current_buffer);
12695 last_escape_glyph_frame = NULL;
12696 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12697 last_glyphless_glyph_frame = NULL;
12698 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12699
12700 /* If new fonts have been loaded that make a glyph matrix adjustment
12701 necessary, do it. */
12702 if (fonts_changed_p)
12703 {
12704 adjust_glyphs (NULL);
12705 ++windows_or_buffers_changed;
12706 fonts_changed_p = 0;
12707 }
12708
12709 /* If face_change_count is non-zero, init_iterator will free all
12710 realized faces, which includes the faces referenced from current
12711 matrices. So, we can't reuse current matrices in this case. */
12712 if (face_change_count)
12713 ++windows_or_buffers_changed;
12714
12715 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12716 && FRAME_TTY (sf)->previous_frame != sf)
12717 {
12718 /* Since frames on a single ASCII terminal share the same
12719 display area, displaying a different frame means redisplay
12720 the whole thing. */
12721 windows_or_buffers_changed++;
12722 SET_FRAME_GARBAGED (sf);
12723 #ifndef DOS_NT
12724 set_tty_color_mode (FRAME_TTY (sf), sf);
12725 #endif
12726 FRAME_TTY (sf)->previous_frame = sf;
12727 }
12728
12729 /* Set the visible flags for all frames. Do this before checking
12730 for resized or garbaged frames; they want to know if their frames
12731 are visible. See the comment in frame.h for
12732 FRAME_SAMPLE_VISIBILITY. */
12733 {
12734 Lisp_Object tail, frame;
12735
12736 number_of_visible_frames = 0;
12737
12738 FOR_EACH_FRAME (tail, frame)
12739 {
12740 struct frame *f = XFRAME (frame);
12741
12742 FRAME_SAMPLE_VISIBILITY (f);
12743 if (FRAME_VISIBLE_P (f))
12744 ++number_of_visible_frames;
12745 clear_desired_matrices (f);
12746 }
12747 }
12748
12749 /* Notice any pending interrupt request to change frame size. */
12750 do_pending_window_change (1);
12751
12752 /* do_pending_window_change could change the selected_window due to
12753 frame resizing which makes the selected window too small. */
12754 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12755 {
12756 sw = w;
12757 reconsider_clip_changes (w, current_buffer);
12758 }
12759
12760 /* Clear frames marked as garbaged. */
12761 if (frame_garbaged)
12762 clear_garbaged_frames ();
12763
12764 /* Build menubar and tool-bar items. */
12765 if (NILP (Vmemory_full))
12766 prepare_menu_bars ();
12767
12768 if (windows_or_buffers_changed)
12769 update_mode_lines++;
12770
12771 /* Detect case that we need to write or remove a star in the mode line. */
12772 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12773 {
12774 w->update_mode_line = Qt;
12775 if (buffer_shared > 1)
12776 update_mode_lines++;
12777 }
12778
12779 /* Avoid invocation of point motion hooks by `current_column' below. */
12780 count1 = SPECPDL_INDEX ();
12781 specbind (Qinhibit_point_motion_hooks, Qt);
12782
12783 /* If %c is in the mode line, update it if needed. */
12784 if (!NILP (w->column_number_displayed)
12785 /* This alternative quickly identifies a common case
12786 where no change is needed. */
12787 && !(PT == XFASTINT (w->last_point)
12788 && XFASTINT (w->last_modified) >= MODIFF
12789 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12790 && (XFASTINT (w->column_number_displayed) != current_column ()))
12791 w->update_mode_line = Qt;
12792
12793 unbind_to (count1, Qnil);
12794
12795 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12796
12797 /* The variable buffer_shared is set in redisplay_window and
12798 indicates that we redisplay a buffer in different windows. See
12799 there. */
12800 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12801 || cursor_type_changed);
12802
12803 /* If specs for an arrow have changed, do thorough redisplay
12804 to ensure we remove any arrow that should no longer exist. */
12805 if (overlay_arrows_changed_p ())
12806 consider_all_windows_p = windows_or_buffers_changed = 1;
12807
12808 /* Normally the message* functions will have already displayed and
12809 updated the echo area, but the frame may have been trashed, or
12810 the update may have been preempted, so display the echo area
12811 again here. Checking message_cleared_p captures the case that
12812 the echo area should be cleared. */
12813 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12814 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12815 || (message_cleared_p
12816 && minibuf_level == 0
12817 /* If the mini-window is currently selected, this means the
12818 echo-area doesn't show through. */
12819 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12820 {
12821 int window_height_changed_p = echo_area_display (0);
12822 must_finish = 1;
12823
12824 /* If we don't display the current message, don't clear the
12825 message_cleared_p flag, because, if we did, we wouldn't clear
12826 the echo area in the next redisplay which doesn't preserve
12827 the echo area. */
12828 if (!display_last_displayed_message_p)
12829 message_cleared_p = 0;
12830
12831 if (fonts_changed_p)
12832 goto retry;
12833 else if (window_height_changed_p)
12834 {
12835 consider_all_windows_p = 1;
12836 ++update_mode_lines;
12837 ++windows_or_buffers_changed;
12838
12839 /* If window configuration was changed, frames may have been
12840 marked garbaged. Clear them or we will experience
12841 surprises wrt scrolling. */
12842 if (frame_garbaged)
12843 clear_garbaged_frames ();
12844 }
12845 }
12846 else if (EQ (selected_window, minibuf_window)
12847 && (current_buffer->clip_changed
12848 || XFASTINT (w->last_modified) < MODIFF
12849 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12850 && resize_mini_window (w, 0))
12851 {
12852 /* Resized active mini-window to fit the size of what it is
12853 showing if its contents might have changed. */
12854 must_finish = 1;
12855 /* FIXME: this causes all frames to be updated, which seems unnecessary
12856 since only the current frame needs to be considered. This function needs
12857 to be rewritten with two variables, consider_all_windows and
12858 consider_all_frames. */
12859 consider_all_windows_p = 1;
12860 ++windows_or_buffers_changed;
12861 ++update_mode_lines;
12862
12863 /* If window configuration was changed, frames may have been
12864 marked garbaged. Clear them or we will experience
12865 surprises wrt scrolling. */
12866 if (frame_garbaged)
12867 clear_garbaged_frames ();
12868 }
12869
12870
12871 /* If showing the region, and mark has changed, we must redisplay
12872 the whole window. The assignment to this_line_start_pos prevents
12873 the optimization directly below this if-statement. */
12874 if (((!NILP (Vtransient_mark_mode)
12875 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12876 != !NILP (w->region_showing))
12877 || (!NILP (w->region_showing)
12878 && !EQ (w->region_showing,
12879 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12880 CHARPOS (this_line_start_pos) = 0;
12881
12882 /* Optimize the case that only the line containing the cursor in the
12883 selected window has changed. Variables starting with this_ are
12884 set in display_line and record information about the line
12885 containing the cursor. */
12886 tlbufpos = this_line_start_pos;
12887 tlendpos = this_line_end_pos;
12888 if (!consider_all_windows_p
12889 && CHARPOS (tlbufpos) > 0
12890 && NILP (w->update_mode_line)
12891 && !current_buffer->clip_changed
12892 && !current_buffer->prevent_redisplay_optimizations_p
12893 && FRAME_VISIBLE_P (XFRAME (w->frame))
12894 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12895 /* Make sure recorded data applies to current buffer, etc. */
12896 && this_line_buffer == current_buffer
12897 && current_buffer == XBUFFER (w->buffer)
12898 && NILP (w->force_start)
12899 && NILP (w->optional_new_start)
12900 /* Point must be on the line that we have info recorded about. */
12901 && PT >= CHARPOS (tlbufpos)
12902 && PT <= Z - CHARPOS (tlendpos)
12903 /* All text outside that line, including its final newline,
12904 must be unchanged. */
12905 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12906 CHARPOS (tlendpos)))
12907 {
12908 if (CHARPOS (tlbufpos) > BEGV
12909 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12910 && (CHARPOS (tlbufpos) == ZV
12911 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12912 /* Former continuation line has disappeared by becoming empty. */
12913 goto cancel;
12914 else if (XFASTINT (w->last_modified) < MODIFF
12915 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12916 || MINI_WINDOW_P (w))
12917 {
12918 /* We have to handle the case of continuation around a
12919 wide-column character (see the comment in indent.c around
12920 line 1340).
12921
12922 For instance, in the following case:
12923
12924 -------- Insert --------
12925 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12926 J_I_ ==> J_I_ `^^' are cursors.
12927 ^^ ^^
12928 -------- --------
12929
12930 As we have to redraw the line above, we cannot use this
12931 optimization. */
12932
12933 struct it it;
12934 int line_height_before = this_line_pixel_height;
12935
12936 /* Note that start_display will handle the case that the
12937 line starting at tlbufpos is a continuation line. */
12938 start_display (&it, w, tlbufpos);
12939
12940 /* Implementation note: It this still necessary? */
12941 if (it.current_x != this_line_start_x)
12942 goto cancel;
12943
12944 TRACE ((stderr, "trying display optimization 1\n"));
12945 w->cursor.vpos = -1;
12946 overlay_arrow_seen = 0;
12947 it.vpos = this_line_vpos;
12948 it.current_y = this_line_y;
12949 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12950 display_line (&it);
12951
12952 /* If line contains point, is not continued,
12953 and ends at same distance from eob as before, we win. */
12954 if (w->cursor.vpos >= 0
12955 /* Line is not continued, otherwise this_line_start_pos
12956 would have been set to 0 in display_line. */
12957 && CHARPOS (this_line_start_pos)
12958 /* Line ends as before. */
12959 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12960 /* Line has same height as before. Otherwise other lines
12961 would have to be shifted up or down. */
12962 && this_line_pixel_height == line_height_before)
12963 {
12964 /* If this is not the window's last line, we must adjust
12965 the charstarts of the lines below. */
12966 if (it.current_y < it.last_visible_y)
12967 {
12968 struct glyph_row *row
12969 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12970 ptrdiff_t delta, delta_bytes;
12971
12972 /* We used to distinguish between two cases here,
12973 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12974 when the line ends in a newline or the end of the
12975 buffer's accessible portion. But both cases did
12976 the same, so they were collapsed. */
12977 delta = (Z
12978 - CHARPOS (tlendpos)
12979 - MATRIX_ROW_START_CHARPOS (row));
12980 delta_bytes = (Z_BYTE
12981 - BYTEPOS (tlendpos)
12982 - MATRIX_ROW_START_BYTEPOS (row));
12983
12984 increment_matrix_positions (w->current_matrix,
12985 this_line_vpos + 1,
12986 w->current_matrix->nrows,
12987 delta, delta_bytes);
12988 }
12989
12990 /* If this row displays text now but previously didn't,
12991 or vice versa, w->window_end_vpos may have to be
12992 adjusted. */
12993 if ((it.glyph_row - 1)->displays_text_p)
12994 {
12995 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12996 XSETINT (w->window_end_vpos, this_line_vpos);
12997 }
12998 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12999 && this_line_vpos > 0)
13000 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13001 w->window_end_valid = Qnil;
13002
13003 /* Update hint: No need to try to scroll in update_window. */
13004 w->desired_matrix->no_scrolling_p = 1;
13005
13006 #if GLYPH_DEBUG
13007 *w->desired_matrix->method = 0;
13008 debug_method_add (w, "optimization 1");
13009 #endif
13010 #ifdef HAVE_WINDOW_SYSTEM
13011 update_window_fringes (w, 0);
13012 #endif
13013 goto update;
13014 }
13015 else
13016 goto cancel;
13017 }
13018 else if (/* Cursor position hasn't changed. */
13019 PT == XFASTINT (w->last_point)
13020 /* Make sure the cursor was last displayed
13021 in this window. Otherwise we have to reposition it. */
13022 && 0 <= w->cursor.vpos
13023 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13024 {
13025 if (!must_finish)
13026 {
13027 do_pending_window_change (1);
13028 /* If selected_window changed, redisplay again. */
13029 if (WINDOWP (selected_window)
13030 && (w = XWINDOW (selected_window)) != sw)
13031 goto retry;
13032
13033 /* We used to always goto end_of_redisplay here, but this
13034 isn't enough if we have a blinking cursor. */
13035 if (w->cursor_off_p == w->last_cursor_off_p)
13036 goto end_of_redisplay;
13037 }
13038 goto update;
13039 }
13040 /* If highlighting the region, or if the cursor is in the echo area,
13041 then we can't just move the cursor. */
13042 else if (! (!NILP (Vtransient_mark_mode)
13043 && !NILP (BVAR (current_buffer, mark_active)))
13044 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13045 || highlight_nonselected_windows)
13046 && NILP (w->region_showing)
13047 && NILP (Vshow_trailing_whitespace)
13048 && !cursor_in_echo_area)
13049 {
13050 struct it it;
13051 struct glyph_row *row;
13052
13053 /* Skip from tlbufpos to PT and see where it is. Note that
13054 PT may be in invisible text. If so, we will end at the
13055 next visible position. */
13056 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13057 NULL, DEFAULT_FACE_ID);
13058 it.current_x = this_line_start_x;
13059 it.current_y = this_line_y;
13060 it.vpos = this_line_vpos;
13061
13062 /* The call to move_it_to stops in front of PT, but
13063 moves over before-strings. */
13064 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13065
13066 if (it.vpos == this_line_vpos
13067 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13068 row->enabled_p))
13069 {
13070 xassert (this_line_vpos == it.vpos);
13071 xassert (this_line_y == it.current_y);
13072 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13073 #if GLYPH_DEBUG
13074 *w->desired_matrix->method = 0;
13075 debug_method_add (w, "optimization 3");
13076 #endif
13077 goto update;
13078 }
13079 else
13080 goto cancel;
13081 }
13082
13083 cancel:
13084 /* Text changed drastically or point moved off of line. */
13085 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13086 }
13087
13088 CHARPOS (this_line_start_pos) = 0;
13089 consider_all_windows_p |= buffer_shared > 1;
13090 ++clear_face_cache_count;
13091 #ifdef HAVE_WINDOW_SYSTEM
13092 ++clear_image_cache_count;
13093 #endif
13094
13095 /* Build desired matrices, and update the display. If
13096 consider_all_windows_p is non-zero, do it for all windows on all
13097 frames. Otherwise do it for selected_window, only. */
13098
13099 if (consider_all_windows_p)
13100 {
13101 Lisp_Object tail, frame;
13102
13103 FOR_EACH_FRAME (tail, frame)
13104 XFRAME (frame)->updated_p = 0;
13105
13106 /* Recompute # windows showing selected buffer. This will be
13107 incremented each time such a window is displayed. */
13108 buffer_shared = 0;
13109
13110 FOR_EACH_FRAME (tail, frame)
13111 {
13112 struct frame *f = XFRAME (frame);
13113
13114 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13115 {
13116 if (! EQ (frame, selected_frame))
13117 /* Select the frame, for the sake of frame-local
13118 variables. */
13119 select_frame_for_redisplay (frame);
13120
13121 /* Mark all the scroll bars to be removed; we'll redeem
13122 the ones we want when we redisplay their windows. */
13123 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13124 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13125
13126 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13127 redisplay_windows (FRAME_ROOT_WINDOW (f));
13128
13129 /* The X error handler may have deleted that frame. */
13130 if (!FRAME_LIVE_P (f))
13131 continue;
13132
13133 /* Any scroll bars which redisplay_windows should have
13134 nuked should now go away. */
13135 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13136 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13137
13138 /* If fonts changed, display again. */
13139 /* ??? rms: I suspect it is a mistake to jump all the way
13140 back to retry here. It should just retry this frame. */
13141 if (fonts_changed_p)
13142 goto retry;
13143
13144 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13145 {
13146 /* See if we have to hscroll. */
13147 if (!f->already_hscrolled_p)
13148 {
13149 f->already_hscrolled_p = 1;
13150 if (hscroll_windows (f->root_window))
13151 goto retry;
13152 }
13153
13154 /* Prevent various kinds of signals during display
13155 update. stdio is not robust about handling
13156 signals, which can cause an apparent I/O
13157 error. */
13158 if (interrupt_input)
13159 unrequest_sigio ();
13160 STOP_POLLING;
13161
13162 /* Update the display. */
13163 set_window_update_flags (XWINDOW (f->root_window), 1);
13164 pending |= update_frame (f, 0, 0);
13165 f->updated_p = 1;
13166 }
13167 }
13168 }
13169
13170 if (!EQ (old_frame, selected_frame)
13171 && FRAME_LIVE_P (XFRAME (old_frame)))
13172 /* We played a bit fast-and-loose above and allowed selected_frame
13173 and selected_window to be temporarily out-of-sync but let's make
13174 sure this stays contained. */
13175 select_frame_for_redisplay (old_frame);
13176 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13177
13178 if (!pending)
13179 {
13180 /* Do the mark_window_display_accurate after all windows have
13181 been redisplayed because this call resets flags in buffers
13182 which are needed for proper redisplay. */
13183 FOR_EACH_FRAME (tail, frame)
13184 {
13185 struct frame *f = XFRAME (frame);
13186 if (f->updated_p)
13187 {
13188 mark_window_display_accurate (f->root_window, 1);
13189 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13190 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13191 }
13192 }
13193 }
13194 }
13195 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13196 {
13197 Lisp_Object mini_window;
13198 struct frame *mini_frame;
13199
13200 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13201 /* Use list_of_error, not Qerror, so that
13202 we catch only errors and don't run the debugger. */
13203 internal_condition_case_1 (redisplay_window_1, selected_window,
13204 list_of_error,
13205 redisplay_window_error);
13206
13207 /* Compare desired and current matrices, perform output. */
13208
13209 update:
13210 /* If fonts changed, display again. */
13211 if (fonts_changed_p)
13212 goto retry;
13213
13214 /* Prevent various kinds of signals during display update.
13215 stdio is not robust about handling signals,
13216 which can cause an apparent I/O error. */
13217 if (interrupt_input)
13218 unrequest_sigio ();
13219 STOP_POLLING;
13220
13221 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13222 {
13223 if (hscroll_windows (selected_window))
13224 goto retry;
13225
13226 XWINDOW (selected_window)->must_be_updated_p = 1;
13227 pending = update_frame (sf, 0, 0);
13228 }
13229
13230 /* We may have called echo_area_display at the top of this
13231 function. If the echo area is on another frame, that may
13232 have put text on a frame other than the selected one, so the
13233 above call to update_frame would not have caught it. Catch
13234 it here. */
13235 mini_window = FRAME_MINIBUF_WINDOW (sf);
13236 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13237
13238 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13239 {
13240 XWINDOW (mini_window)->must_be_updated_p = 1;
13241 pending |= update_frame (mini_frame, 0, 0);
13242 if (!pending && hscroll_windows (mini_window))
13243 goto retry;
13244 }
13245 }
13246
13247 /* If display was paused because of pending input, make sure we do a
13248 thorough update the next time. */
13249 if (pending)
13250 {
13251 /* Prevent the optimization at the beginning of
13252 redisplay_internal that tries a single-line update of the
13253 line containing the cursor in the selected window. */
13254 CHARPOS (this_line_start_pos) = 0;
13255
13256 /* Let the overlay arrow be updated the next time. */
13257 update_overlay_arrows (0);
13258
13259 /* If we pause after scrolling, some rows in the current
13260 matrices of some windows are not valid. */
13261 if (!WINDOW_FULL_WIDTH_P (w)
13262 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13263 update_mode_lines = 1;
13264 }
13265 else
13266 {
13267 if (!consider_all_windows_p)
13268 {
13269 /* This has already been done above if
13270 consider_all_windows_p is set. */
13271 mark_window_display_accurate_1 (w, 1);
13272
13273 /* Say overlay arrows are up to date. */
13274 update_overlay_arrows (1);
13275
13276 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13277 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13278 }
13279
13280 update_mode_lines = 0;
13281 windows_or_buffers_changed = 0;
13282 cursor_type_changed = 0;
13283 }
13284
13285 /* Start SIGIO interrupts coming again. Having them off during the
13286 code above makes it less likely one will discard output, but not
13287 impossible, since there might be stuff in the system buffer here.
13288 But it is much hairier to try to do anything about that. */
13289 if (interrupt_input)
13290 request_sigio ();
13291 RESUME_POLLING;
13292
13293 /* If a frame has become visible which was not before, redisplay
13294 again, so that we display it. Expose events for such a frame
13295 (which it gets when becoming visible) don't call the parts of
13296 redisplay constructing glyphs, so simply exposing a frame won't
13297 display anything in this case. So, we have to display these
13298 frames here explicitly. */
13299 if (!pending)
13300 {
13301 Lisp_Object tail, frame;
13302 int new_count = 0;
13303
13304 FOR_EACH_FRAME (tail, frame)
13305 {
13306 int this_is_visible = 0;
13307
13308 if (XFRAME (frame)->visible)
13309 this_is_visible = 1;
13310 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13311 if (XFRAME (frame)->visible)
13312 this_is_visible = 1;
13313
13314 if (this_is_visible)
13315 new_count++;
13316 }
13317
13318 if (new_count != number_of_visible_frames)
13319 windows_or_buffers_changed++;
13320 }
13321
13322 /* Change frame size now if a change is pending. */
13323 do_pending_window_change (1);
13324
13325 /* If we just did a pending size change, or have additional
13326 visible frames, or selected_window changed, redisplay again. */
13327 if ((windows_or_buffers_changed && !pending)
13328 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13329 goto retry;
13330
13331 /* Clear the face and image caches.
13332
13333 We used to do this only if consider_all_windows_p. But the cache
13334 needs to be cleared if a timer creates images in the current
13335 buffer (e.g. the test case in Bug#6230). */
13336
13337 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13338 {
13339 clear_face_cache (0);
13340 clear_face_cache_count = 0;
13341 }
13342
13343 #ifdef HAVE_WINDOW_SYSTEM
13344 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13345 {
13346 clear_image_caches (Qnil);
13347 clear_image_cache_count = 0;
13348 }
13349 #endif /* HAVE_WINDOW_SYSTEM */
13350
13351 end_of_redisplay:
13352 unbind_to (count, Qnil);
13353 RESUME_POLLING;
13354 }
13355
13356
13357 /* Redisplay, but leave alone any recent echo area message unless
13358 another message has been requested in its place.
13359
13360 This is useful in situations where you need to redisplay but no
13361 user action has occurred, making it inappropriate for the message
13362 area to be cleared. See tracking_off and
13363 wait_reading_process_output for examples of these situations.
13364
13365 FROM_WHERE is an integer saying from where this function was
13366 called. This is useful for debugging. */
13367
13368 void
13369 redisplay_preserve_echo_area (int from_where)
13370 {
13371 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13372
13373 if (!NILP (echo_area_buffer[1]))
13374 {
13375 /* We have a previously displayed message, but no current
13376 message. Redisplay the previous message. */
13377 display_last_displayed_message_p = 1;
13378 redisplay_internal ();
13379 display_last_displayed_message_p = 0;
13380 }
13381 else
13382 redisplay_internal ();
13383
13384 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13385 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13386 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13387 }
13388
13389
13390 /* Function registered with record_unwind_protect in
13391 redisplay_internal. Reset redisplaying_p to the value it had
13392 before redisplay_internal was called, and clear
13393 prevent_freeing_realized_faces_p. It also selects the previously
13394 selected frame, unless it has been deleted (by an X connection
13395 failure during redisplay, for example). */
13396
13397 static Lisp_Object
13398 unwind_redisplay (Lisp_Object val)
13399 {
13400 Lisp_Object old_redisplaying_p, old_frame;
13401
13402 old_redisplaying_p = XCAR (val);
13403 redisplaying_p = XFASTINT (old_redisplaying_p);
13404 old_frame = XCDR (val);
13405 if (! EQ (old_frame, selected_frame)
13406 && FRAME_LIVE_P (XFRAME (old_frame)))
13407 select_frame_for_redisplay (old_frame);
13408 return Qnil;
13409 }
13410
13411
13412 /* Mark the display of window W as accurate or inaccurate. If
13413 ACCURATE_P is non-zero mark display of W as accurate. If
13414 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13415 redisplay_internal is called. */
13416
13417 static void
13418 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13419 {
13420 if (BUFFERP (w->buffer))
13421 {
13422 struct buffer *b = XBUFFER (w->buffer);
13423
13424 w->last_modified
13425 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13426 w->last_overlay_modified
13427 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13428 w->last_had_star
13429 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13430
13431 if (accurate_p)
13432 {
13433 b->clip_changed = 0;
13434 b->prevent_redisplay_optimizations_p = 0;
13435
13436 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13437 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13438 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13439 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13440
13441 w->current_matrix->buffer = b;
13442 w->current_matrix->begv = BUF_BEGV (b);
13443 w->current_matrix->zv = BUF_ZV (b);
13444
13445 w->last_cursor = w->cursor;
13446 w->last_cursor_off_p = w->cursor_off_p;
13447
13448 if (w == XWINDOW (selected_window))
13449 w->last_point = make_number (BUF_PT (b));
13450 else
13451 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13452 }
13453 }
13454
13455 if (accurate_p)
13456 {
13457 w->window_end_valid = w->buffer;
13458 w->update_mode_line = Qnil;
13459 }
13460 }
13461
13462
13463 /* Mark the display of windows in the window tree rooted at WINDOW as
13464 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13465 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13466 be redisplayed the next time redisplay_internal is called. */
13467
13468 void
13469 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13470 {
13471 struct window *w;
13472
13473 for (; !NILP (window); window = w->next)
13474 {
13475 w = XWINDOW (window);
13476 mark_window_display_accurate_1 (w, accurate_p);
13477
13478 if (!NILP (w->vchild))
13479 mark_window_display_accurate (w->vchild, accurate_p);
13480 if (!NILP (w->hchild))
13481 mark_window_display_accurate (w->hchild, accurate_p);
13482 }
13483
13484 if (accurate_p)
13485 {
13486 update_overlay_arrows (1);
13487 }
13488 else
13489 {
13490 /* Force a thorough redisplay the next time by setting
13491 last_arrow_position and last_arrow_string to t, which is
13492 unequal to any useful value of Voverlay_arrow_... */
13493 update_overlay_arrows (-1);
13494 }
13495 }
13496
13497
13498 /* Return value in display table DP (Lisp_Char_Table *) for character
13499 C. Since a display table doesn't have any parent, we don't have to
13500 follow parent. Do not call this function directly but use the
13501 macro DISP_CHAR_VECTOR. */
13502
13503 Lisp_Object
13504 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13505 {
13506 Lisp_Object val;
13507
13508 if (ASCII_CHAR_P (c))
13509 {
13510 val = dp->ascii;
13511 if (SUB_CHAR_TABLE_P (val))
13512 val = XSUB_CHAR_TABLE (val)->contents[c];
13513 }
13514 else
13515 {
13516 Lisp_Object table;
13517
13518 XSETCHAR_TABLE (table, dp);
13519 val = char_table_ref (table, c);
13520 }
13521 if (NILP (val))
13522 val = dp->defalt;
13523 return val;
13524 }
13525
13526
13527 \f
13528 /***********************************************************************
13529 Window Redisplay
13530 ***********************************************************************/
13531
13532 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13533
13534 static void
13535 redisplay_windows (Lisp_Object window)
13536 {
13537 while (!NILP (window))
13538 {
13539 struct window *w = XWINDOW (window);
13540
13541 if (!NILP (w->hchild))
13542 redisplay_windows (w->hchild);
13543 else if (!NILP (w->vchild))
13544 redisplay_windows (w->vchild);
13545 else if (!NILP (w->buffer))
13546 {
13547 displayed_buffer = XBUFFER (w->buffer);
13548 /* Use list_of_error, not Qerror, so that
13549 we catch only errors and don't run the debugger. */
13550 internal_condition_case_1 (redisplay_window_0, window,
13551 list_of_error,
13552 redisplay_window_error);
13553 }
13554
13555 window = w->next;
13556 }
13557 }
13558
13559 static Lisp_Object
13560 redisplay_window_error (Lisp_Object ignore)
13561 {
13562 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13563 return Qnil;
13564 }
13565
13566 static Lisp_Object
13567 redisplay_window_0 (Lisp_Object window)
13568 {
13569 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13570 redisplay_window (window, 0);
13571 return Qnil;
13572 }
13573
13574 static Lisp_Object
13575 redisplay_window_1 (Lisp_Object window)
13576 {
13577 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13578 redisplay_window (window, 1);
13579 return Qnil;
13580 }
13581 \f
13582
13583 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13584 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13585 which positions recorded in ROW differ from current buffer
13586 positions.
13587
13588 Return 0 if cursor is not on this row, 1 otherwise. */
13589
13590 static int
13591 set_cursor_from_row (struct window *w, struct glyph_row *row,
13592 struct glyph_matrix *matrix,
13593 ptrdiff_t delta, ptrdiff_t delta_bytes,
13594 int dy, int dvpos)
13595 {
13596 struct glyph *glyph = row->glyphs[TEXT_AREA];
13597 struct glyph *end = glyph + row->used[TEXT_AREA];
13598 struct glyph *cursor = NULL;
13599 /* The last known character position in row. */
13600 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13601 int x = row->x;
13602 ptrdiff_t pt_old = PT - delta;
13603 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13604 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13605 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13606 /* A glyph beyond the edge of TEXT_AREA which we should never
13607 touch. */
13608 struct glyph *glyphs_end = end;
13609 /* Non-zero means we've found a match for cursor position, but that
13610 glyph has the avoid_cursor_p flag set. */
13611 int match_with_avoid_cursor = 0;
13612 /* Non-zero means we've seen at least one glyph that came from a
13613 display string. */
13614 int string_seen = 0;
13615 /* Largest and smalles buffer positions seen so far during scan of
13616 glyph row. */
13617 ptrdiff_t bpos_max = pos_before;
13618 ptrdiff_t bpos_min = pos_after;
13619 /* Last buffer position covered by an overlay string with an integer
13620 `cursor' property. */
13621 ptrdiff_t bpos_covered = 0;
13622 /* Non-zero means the display string on which to display the cursor
13623 comes from a text property, not from an overlay. */
13624 int string_from_text_prop = 0;
13625
13626 /* Skip over glyphs not having an object at the start and the end of
13627 the row. These are special glyphs like truncation marks on
13628 terminal frames. */
13629 if (row->displays_text_p)
13630 {
13631 if (!row->reversed_p)
13632 {
13633 while (glyph < end
13634 && INTEGERP (glyph->object)
13635 && glyph->charpos < 0)
13636 {
13637 x += glyph->pixel_width;
13638 ++glyph;
13639 }
13640 while (end > glyph
13641 && INTEGERP ((end - 1)->object)
13642 /* CHARPOS is zero for blanks and stretch glyphs
13643 inserted by extend_face_to_end_of_line. */
13644 && (end - 1)->charpos <= 0)
13645 --end;
13646 glyph_before = glyph - 1;
13647 glyph_after = end;
13648 }
13649 else
13650 {
13651 struct glyph *g;
13652
13653 /* If the glyph row is reversed, we need to process it from back
13654 to front, so swap the edge pointers. */
13655 glyphs_end = end = glyph - 1;
13656 glyph += row->used[TEXT_AREA] - 1;
13657
13658 while (glyph > end + 1
13659 && INTEGERP (glyph->object)
13660 && glyph->charpos < 0)
13661 {
13662 --glyph;
13663 x -= glyph->pixel_width;
13664 }
13665 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13666 --glyph;
13667 /* By default, in reversed rows we put the cursor on the
13668 rightmost (first in the reading order) glyph. */
13669 for (g = end + 1; g < glyph; g++)
13670 x += g->pixel_width;
13671 while (end < glyph
13672 && INTEGERP ((end + 1)->object)
13673 && (end + 1)->charpos <= 0)
13674 ++end;
13675 glyph_before = glyph + 1;
13676 glyph_after = end;
13677 }
13678 }
13679 else if (row->reversed_p)
13680 {
13681 /* In R2L rows that don't display text, put the cursor on the
13682 rightmost glyph. Case in point: an empty last line that is
13683 part of an R2L paragraph. */
13684 cursor = end - 1;
13685 /* Avoid placing the cursor on the last glyph of the row, where
13686 on terminal frames we hold the vertical border between
13687 adjacent windows. */
13688 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13689 && !WINDOW_RIGHTMOST_P (w)
13690 && cursor == row->glyphs[LAST_AREA] - 1)
13691 cursor--;
13692 x = -1; /* will be computed below, at label compute_x */
13693 }
13694
13695 /* Step 1: Try to find the glyph whose character position
13696 corresponds to point. If that's not possible, find 2 glyphs
13697 whose character positions are the closest to point, one before
13698 point, the other after it. */
13699 if (!row->reversed_p)
13700 while (/* not marched to end of glyph row */
13701 glyph < end
13702 /* glyph was not inserted by redisplay for internal purposes */
13703 && !INTEGERP (glyph->object))
13704 {
13705 if (BUFFERP (glyph->object))
13706 {
13707 ptrdiff_t dpos = glyph->charpos - pt_old;
13708
13709 if (glyph->charpos > bpos_max)
13710 bpos_max = glyph->charpos;
13711 if (glyph->charpos < bpos_min)
13712 bpos_min = glyph->charpos;
13713 if (!glyph->avoid_cursor_p)
13714 {
13715 /* If we hit point, we've found the glyph on which to
13716 display the cursor. */
13717 if (dpos == 0)
13718 {
13719 match_with_avoid_cursor = 0;
13720 break;
13721 }
13722 /* See if we've found a better approximation to
13723 POS_BEFORE or to POS_AFTER. Note that we want the
13724 first (leftmost) glyph of all those that are the
13725 closest from below, and the last (rightmost) of all
13726 those from above. */
13727 if (0 > dpos && dpos > pos_before - pt_old)
13728 {
13729 pos_before = glyph->charpos;
13730 glyph_before = glyph;
13731 }
13732 else if (0 < dpos && dpos <= pos_after - pt_old)
13733 {
13734 pos_after = glyph->charpos;
13735 glyph_after = glyph;
13736 }
13737 }
13738 else if (dpos == 0)
13739 match_with_avoid_cursor = 1;
13740 }
13741 else if (STRINGP (glyph->object))
13742 {
13743 Lisp_Object chprop;
13744 ptrdiff_t glyph_pos = glyph->charpos;
13745
13746 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13747 glyph->object);
13748 if (INTEGERP (chprop))
13749 {
13750 bpos_covered = bpos_max + XINT (chprop);
13751 /* If the `cursor' property covers buffer positions up
13752 to and including point, we should display cursor on
13753 this glyph. Note that overlays and text properties
13754 with string values stop bidi reordering, so every
13755 buffer position to the left of the string is always
13756 smaller than any position to the right of the
13757 string. Therefore, if a `cursor' property on one
13758 of the string's characters has an integer value, we
13759 will break out of the loop below _before_ we get to
13760 the position match above. IOW, integer values of
13761 the `cursor' property override the "exact match for
13762 point" strategy of positioning the cursor. */
13763 /* Implementation note: bpos_max == pt_old when, e.g.,
13764 we are in an empty line, where bpos_max is set to
13765 MATRIX_ROW_START_CHARPOS, see above. */
13766 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13767 {
13768 cursor = glyph;
13769 break;
13770 }
13771 }
13772
13773 string_seen = 1;
13774 }
13775 x += glyph->pixel_width;
13776 ++glyph;
13777 }
13778 else if (glyph > end) /* row is reversed */
13779 while (!INTEGERP (glyph->object))
13780 {
13781 if (BUFFERP (glyph->object))
13782 {
13783 ptrdiff_t dpos = glyph->charpos - pt_old;
13784
13785 if (glyph->charpos > bpos_max)
13786 bpos_max = glyph->charpos;
13787 if (glyph->charpos < bpos_min)
13788 bpos_min = glyph->charpos;
13789 if (!glyph->avoid_cursor_p)
13790 {
13791 if (dpos == 0)
13792 {
13793 match_with_avoid_cursor = 0;
13794 break;
13795 }
13796 if (0 > dpos && dpos > pos_before - pt_old)
13797 {
13798 pos_before = glyph->charpos;
13799 glyph_before = glyph;
13800 }
13801 else if (0 < dpos && dpos <= pos_after - pt_old)
13802 {
13803 pos_after = glyph->charpos;
13804 glyph_after = glyph;
13805 }
13806 }
13807 else if (dpos == 0)
13808 match_with_avoid_cursor = 1;
13809 }
13810 else if (STRINGP (glyph->object))
13811 {
13812 Lisp_Object chprop;
13813 ptrdiff_t glyph_pos = glyph->charpos;
13814
13815 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13816 glyph->object);
13817 if (INTEGERP (chprop))
13818 {
13819 bpos_covered = bpos_max + XINT (chprop);
13820 /* If the `cursor' property covers buffer positions up
13821 to and including point, we should display cursor on
13822 this glyph. */
13823 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13824 {
13825 cursor = glyph;
13826 break;
13827 }
13828 }
13829 string_seen = 1;
13830 }
13831 --glyph;
13832 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13833 {
13834 x--; /* can't use any pixel_width */
13835 break;
13836 }
13837 x -= glyph->pixel_width;
13838 }
13839
13840 /* Step 2: If we didn't find an exact match for point, we need to
13841 look for a proper place to put the cursor among glyphs between
13842 GLYPH_BEFORE and GLYPH_AFTER. */
13843 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13844 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13845 && bpos_covered < pt_old)
13846 {
13847 /* An empty line has a single glyph whose OBJECT is zero and
13848 whose CHARPOS is the position of a newline on that line.
13849 Note that on a TTY, there are more glyphs after that, which
13850 were produced by extend_face_to_end_of_line, but their
13851 CHARPOS is zero or negative. */
13852 int empty_line_p =
13853 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13854 && INTEGERP (glyph->object) && glyph->charpos > 0;
13855
13856 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13857 {
13858 ptrdiff_t ellipsis_pos;
13859
13860 /* Scan back over the ellipsis glyphs. */
13861 if (!row->reversed_p)
13862 {
13863 ellipsis_pos = (glyph - 1)->charpos;
13864 while (glyph > row->glyphs[TEXT_AREA]
13865 && (glyph - 1)->charpos == ellipsis_pos)
13866 glyph--, x -= glyph->pixel_width;
13867 /* That loop always goes one position too far, including
13868 the glyph before the ellipsis. So scan forward over
13869 that one. */
13870 x += glyph->pixel_width;
13871 glyph++;
13872 }
13873 else /* row is reversed */
13874 {
13875 ellipsis_pos = (glyph + 1)->charpos;
13876 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13877 && (glyph + 1)->charpos == ellipsis_pos)
13878 glyph++, x += glyph->pixel_width;
13879 x -= glyph->pixel_width;
13880 glyph--;
13881 }
13882 }
13883 else if (match_with_avoid_cursor)
13884 {
13885 cursor = glyph_after;
13886 x = -1;
13887 }
13888 else if (string_seen)
13889 {
13890 int incr = row->reversed_p ? -1 : +1;
13891
13892 /* Need to find the glyph that came out of a string which is
13893 present at point. That glyph is somewhere between
13894 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13895 positioned between POS_BEFORE and POS_AFTER in the
13896 buffer. */
13897 struct glyph *start, *stop;
13898 ptrdiff_t pos = pos_before;
13899
13900 x = -1;
13901
13902 /* If the row ends in a newline from a display string,
13903 reordering could have moved the glyphs belonging to the
13904 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
13905 in this case we extend the search to the last glyph in
13906 the row that was not inserted by redisplay. */
13907 if (row->ends_in_newline_from_string_p)
13908 {
13909 glyph_after = end;
13910 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13911 }
13912
13913 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13914 correspond to POS_BEFORE and POS_AFTER, respectively. We
13915 need START and STOP in the order that corresponds to the
13916 row's direction as given by its reversed_p flag. If the
13917 directionality of characters between POS_BEFORE and
13918 POS_AFTER is the opposite of the row's base direction,
13919 these characters will have been reordered for display,
13920 and we need to reverse START and STOP. */
13921 if (!row->reversed_p)
13922 {
13923 start = min (glyph_before, glyph_after);
13924 stop = max (glyph_before, glyph_after);
13925 }
13926 else
13927 {
13928 start = max (glyph_before, glyph_after);
13929 stop = min (glyph_before, glyph_after);
13930 }
13931 for (glyph = start + incr;
13932 row->reversed_p ? glyph > stop : glyph < stop; )
13933 {
13934
13935 /* Any glyphs that come from the buffer are here because
13936 of bidi reordering. Skip them, and only pay
13937 attention to glyphs that came from some string. */
13938 if (STRINGP (glyph->object))
13939 {
13940 Lisp_Object str;
13941 ptrdiff_t tem;
13942 /* If the display property covers the newline, we
13943 need to search for it one position farther. */
13944 ptrdiff_t lim = pos_after
13945 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13946
13947 string_from_text_prop = 0;
13948 str = glyph->object;
13949 tem = string_buffer_position_lim (str, pos, lim, 0);
13950 if (tem == 0 /* from overlay */
13951 || pos <= tem)
13952 {
13953 /* If the string from which this glyph came is
13954 found in the buffer at point, then we've
13955 found the glyph we've been looking for. If
13956 it comes from an overlay (tem == 0), and it
13957 has the `cursor' property on one of its
13958 glyphs, record that glyph as a candidate for
13959 displaying the cursor. (As in the
13960 unidirectional version, we will display the
13961 cursor on the last candidate we find.) */
13962 if (tem == 0 || tem == pt_old)
13963 {
13964 /* The glyphs from this string could have
13965 been reordered. Find the one with the
13966 smallest string position. Or there could
13967 be a character in the string with the
13968 `cursor' property, which means display
13969 cursor on that character's glyph. */
13970 ptrdiff_t strpos = glyph->charpos;
13971
13972 if (tem)
13973 {
13974 cursor = glyph;
13975 string_from_text_prop = 1;
13976 }
13977 for ( ;
13978 (row->reversed_p ? glyph > stop : glyph < stop)
13979 && EQ (glyph->object, str);
13980 glyph += incr)
13981 {
13982 Lisp_Object cprop;
13983 ptrdiff_t gpos = glyph->charpos;
13984
13985 cprop = Fget_char_property (make_number (gpos),
13986 Qcursor,
13987 glyph->object);
13988 if (!NILP (cprop))
13989 {
13990 cursor = glyph;
13991 break;
13992 }
13993 if (tem && glyph->charpos < strpos)
13994 {
13995 strpos = glyph->charpos;
13996 cursor = glyph;
13997 }
13998 }
13999
14000 if (tem == pt_old)
14001 goto compute_x;
14002 }
14003 if (tem)
14004 pos = tem + 1; /* don't find previous instances */
14005 }
14006 /* This string is not what we want; skip all of the
14007 glyphs that came from it. */
14008 while ((row->reversed_p ? glyph > stop : glyph < stop)
14009 && EQ (glyph->object, str))
14010 glyph += incr;
14011 }
14012 else
14013 glyph += incr;
14014 }
14015
14016 /* If we reached the end of the line, and END was from a string,
14017 the cursor is not on this line. */
14018 if (cursor == NULL
14019 && (row->reversed_p ? glyph <= end : glyph >= end)
14020 && STRINGP (end->object)
14021 && row->continued_p)
14022 return 0;
14023 }
14024 /* A truncated row may not include PT among its character positions.
14025 Setting the cursor inside the scroll margin will trigger
14026 recalculation of hscroll in hscroll_window_tree. But if a
14027 display string covers point, defer to the string-handling
14028 code below to figure this out. */
14029 else if (row->truncated_on_left_p && pt_old < bpos_min)
14030 {
14031 cursor = glyph_before;
14032 x = -1;
14033 }
14034 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14035 /* Zero-width characters produce no glyphs. */
14036 || (!empty_line_p
14037 && (row->reversed_p
14038 ? glyph_after > glyphs_end
14039 : glyph_after < glyphs_end)))
14040 {
14041 cursor = glyph_after;
14042 x = -1;
14043 }
14044 }
14045
14046 compute_x:
14047 if (cursor != NULL)
14048 glyph = cursor;
14049 if (x < 0)
14050 {
14051 struct glyph *g;
14052
14053 /* Need to compute x that corresponds to GLYPH. */
14054 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14055 {
14056 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14057 abort ();
14058 x += g->pixel_width;
14059 }
14060 }
14061
14062 /* ROW could be part of a continued line, which, under bidi
14063 reordering, might have other rows whose start and end charpos
14064 occlude point. Only set w->cursor if we found a better
14065 approximation to the cursor position than we have from previously
14066 examined candidate rows belonging to the same continued line. */
14067 if (/* we already have a candidate row */
14068 w->cursor.vpos >= 0
14069 /* that candidate is not the row we are processing */
14070 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14071 /* Make sure cursor.vpos specifies a row whose start and end
14072 charpos occlude point, and it is valid candidate for being a
14073 cursor-row. This is because some callers of this function
14074 leave cursor.vpos at the row where the cursor was displayed
14075 during the last redisplay cycle. */
14076 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14077 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14078 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14079 {
14080 struct glyph *g1 =
14081 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14082
14083 /* Don't consider glyphs that are outside TEXT_AREA. */
14084 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14085 return 0;
14086 /* Keep the candidate whose buffer position is the closest to
14087 point or has the `cursor' property. */
14088 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14089 w->cursor.hpos >= 0
14090 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14091 && ((BUFFERP (g1->object)
14092 && (g1->charpos == pt_old /* an exact match always wins */
14093 || (BUFFERP (glyph->object)
14094 && eabs (g1->charpos - pt_old)
14095 < eabs (glyph->charpos - pt_old))))
14096 /* previous candidate is a glyph from a string that has
14097 a non-nil `cursor' property */
14098 || (STRINGP (g1->object)
14099 && (!NILP (Fget_char_property (make_number (g1->charpos),
14100 Qcursor, g1->object))
14101 /* pevious candidate is from the same display
14102 string as this one, and the display string
14103 came from a text property */
14104 || (EQ (g1->object, glyph->object)
14105 && string_from_text_prop)
14106 /* this candidate is from newline and its
14107 position is not an exact match */
14108 || (INTEGERP (glyph->object)
14109 && glyph->charpos != pt_old)))))
14110 return 0;
14111 /* If this candidate gives an exact match, use that. */
14112 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14113 /* If this candidate is a glyph created for the
14114 terminating newline of a line, and point is on that
14115 newline, it wins because it's an exact match. */
14116 || (!row->continued_p
14117 && INTEGERP (glyph->object)
14118 && glyph->charpos == 0
14119 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14120 /* Otherwise, keep the candidate that comes from a row
14121 spanning less buffer positions. This may win when one or
14122 both candidate positions are on glyphs that came from
14123 display strings, for which we cannot compare buffer
14124 positions. */
14125 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14126 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14127 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14128 return 0;
14129 }
14130 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14131 w->cursor.x = x;
14132 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14133 w->cursor.y = row->y + dy;
14134
14135 if (w == XWINDOW (selected_window))
14136 {
14137 if (!row->continued_p
14138 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14139 && row->x == 0)
14140 {
14141 this_line_buffer = XBUFFER (w->buffer);
14142
14143 CHARPOS (this_line_start_pos)
14144 = MATRIX_ROW_START_CHARPOS (row) + delta;
14145 BYTEPOS (this_line_start_pos)
14146 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14147
14148 CHARPOS (this_line_end_pos)
14149 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14150 BYTEPOS (this_line_end_pos)
14151 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14152
14153 this_line_y = w->cursor.y;
14154 this_line_pixel_height = row->height;
14155 this_line_vpos = w->cursor.vpos;
14156 this_line_start_x = row->x;
14157 }
14158 else
14159 CHARPOS (this_line_start_pos) = 0;
14160 }
14161
14162 return 1;
14163 }
14164
14165
14166 /* Run window scroll functions, if any, for WINDOW with new window
14167 start STARTP. Sets the window start of WINDOW to that position.
14168
14169 We assume that the window's buffer is really current. */
14170
14171 static inline struct text_pos
14172 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14173 {
14174 struct window *w = XWINDOW (window);
14175 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14176
14177 if (current_buffer != XBUFFER (w->buffer))
14178 abort ();
14179
14180 if (!NILP (Vwindow_scroll_functions))
14181 {
14182 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14183 make_number (CHARPOS (startp)));
14184 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14185 /* In case the hook functions switch buffers. */
14186 if (current_buffer != XBUFFER (w->buffer))
14187 set_buffer_internal_1 (XBUFFER (w->buffer));
14188 }
14189
14190 return startp;
14191 }
14192
14193
14194 /* Make sure the line containing the cursor is fully visible.
14195 A value of 1 means there is nothing to be done.
14196 (Either the line is fully visible, or it cannot be made so,
14197 or we cannot tell.)
14198
14199 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14200 is higher than window.
14201
14202 A value of 0 means the caller should do scrolling
14203 as if point had gone off the screen. */
14204
14205 static int
14206 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14207 {
14208 struct glyph_matrix *matrix;
14209 struct glyph_row *row;
14210 int window_height;
14211
14212 if (!make_cursor_line_fully_visible_p)
14213 return 1;
14214
14215 /* It's not always possible to find the cursor, e.g, when a window
14216 is full of overlay strings. Don't do anything in that case. */
14217 if (w->cursor.vpos < 0)
14218 return 1;
14219
14220 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14221 row = MATRIX_ROW (matrix, w->cursor.vpos);
14222
14223 /* If the cursor row is not partially visible, there's nothing to do. */
14224 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14225 return 1;
14226
14227 /* If the row the cursor is in is taller than the window's height,
14228 it's not clear what to do, so do nothing. */
14229 window_height = window_box_height (w);
14230 if (row->height >= window_height)
14231 {
14232 if (!force_p || MINI_WINDOW_P (w)
14233 || w->vscroll || w->cursor.vpos == 0)
14234 return 1;
14235 }
14236 return 0;
14237 }
14238
14239
14240 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14241 non-zero means only WINDOW is redisplayed in redisplay_internal.
14242 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14243 in redisplay_window to bring a partially visible line into view in
14244 the case that only the cursor has moved.
14245
14246 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14247 last screen line's vertical height extends past the end of the screen.
14248
14249 Value is
14250
14251 1 if scrolling succeeded
14252
14253 0 if scrolling didn't find point.
14254
14255 -1 if new fonts have been loaded so that we must interrupt
14256 redisplay, adjust glyph matrices, and try again. */
14257
14258 enum
14259 {
14260 SCROLLING_SUCCESS,
14261 SCROLLING_FAILED,
14262 SCROLLING_NEED_LARGER_MATRICES
14263 };
14264
14265 /* If scroll-conservatively is more than this, never recenter.
14266
14267 If you change this, don't forget to update the doc string of
14268 `scroll-conservatively' and the Emacs manual. */
14269 #define SCROLL_LIMIT 100
14270
14271 static int
14272 try_scrolling (Lisp_Object window, int just_this_one_p,
14273 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14274 int temp_scroll_step, int last_line_misfit)
14275 {
14276 struct window *w = XWINDOW (window);
14277 struct frame *f = XFRAME (w->frame);
14278 struct text_pos pos, startp;
14279 struct it it;
14280 int this_scroll_margin, scroll_max, rc, height;
14281 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14282 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14283 Lisp_Object aggressive;
14284 /* We will never try scrolling more than this number of lines. */
14285 int scroll_limit = SCROLL_LIMIT;
14286
14287 #if GLYPH_DEBUG
14288 debug_method_add (w, "try_scrolling");
14289 #endif
14290
14291 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14292
14293 /* Compute scroll margin height in pixels. We scroll when point is
14294 within this distance from the top or bottom of the window. */
14295 if (scroll_margin > 0)
14296 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14297 * FRAME_LINE_HEIGHT (f);
14298 else
14299 this_scroll_margin = 0;
14300
14301 /* Force arg_scroll_conservatively to have a reasonable value, to
14302 avoid scrolling too far away with slow move_it_* functions. Note
14303 that the user can supply scroll-conservatively equal to
14304 `most-positive-fixnum', which can be larger than INT_MAX. */
14305 if (arg_scroll_conservatively > scroll_limit)
14306 {
14307 arg_scroll_conservatively = scroll_limit + 1;
14308 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14309 }
14310 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14311 /* Compute how much we should try to scroll maximally to bring
14312 point into view. */
14313 scroll_max = (max (scroll_step,
14314 max (arg_scroll_conservatively, temp_scroll_step))
14315 * FRAME_LINE_HEIGHT (f));
14316 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14317 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14318 /* We're trying to scroll because of aggressive scrolling but no
14319 scroll_step is set. Choose an arbitrary one. */
14320 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14321 else
14322 scroll_max = 0;
14323
14324 too_near_end:
14325
14326 /* Decide whether to scroll down. */
14327 if (PT > CHARPOS (startp))
14328 {
14329 int scroll_margin_y;
14330
14331 /* Compute the pixel ypos of the scroll margin, then move it to
14332 either that ypos or PT, whichever comes first. */
14333 start_display (&it, w, startp);
14334 scroll_margin_y = it.last_visible_y - this_scroll_margin
14335 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14336 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14337 (MOVE_TO_POS | MOVE_TO_Y));
14338
14339 if (PT > CHARPOS (it.current.pos))
14340 {
14341 int y0 = line_bottom_y (&it);
14342 /* Compute how many pixels below window bottom to stop searching
14343 for PT. This avoids costly search for PT that is far away if
14344 the user limited scrolling by a small number of lines, but
14345 always finds PT if scroll_conservatively is set to a large
14346 number, such as most-positive-fixnum. */
14347 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14348 int y_to_move = it.last_visible_y + slack;
14349
14350 /* Compute the distance from the scroll margin to PT or to
14351 the scroll limit, whichever comes first. This should
14352 include the height of the cursor line, to make that line
14353 fully visible. */
14354 move_it_to (&it, PT, -1, y_to_move,
14355 -1, MOVE_TO_POS | MOVE_TO_Y);
14356 dy = line_bottom_y (&it) - y0;
14357
14358 if (dy > scroll_max)
14359 return SCROLLING_FAILED;
14360
14361 scroll_down_p = 1;
14362 }
14363 }
14364
14365 if (scroll_down_p)
14366 {
14367 /* Point is in or below the bottom scroll margin, so move the
14368 window start down. If scrolling conservatively, move it just
14369 enough down to make point visible. If scroll_step is set,
14370 move it down by scroll_step. */
14371 if (arg_scroll_conservatively)
14372 amount_to_scroll
14373 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14374 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14375 else if (scroll_step || temp_scroll_step)
14376 amount_to_scroll = scroll_max;
14377 else
14378 {
14379 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14380 height = WINDOW_BOX_TEXT_HEIGHT (w);
14381 if (NUMBERP (aggressive))
14382 {
14383 double float_amount = XFLOATINT (aggressive) * height;
14384 amount_to_scroll = float_amount;
14385 if (amount_to_scroll == 0 && float_amount > 0)
14386 amount_to_scroll = 1;
14387 /* Don't let point enter the scroll margin near top of
14388 the window. */
14389 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14390 amount_to_scroll = height - 2*this_scroll_margin + dy;
14391 }
14392 }
14393
14394 if (amount_to_scroll <= 0)
14395 return SCROLLING_FAILED;
14396
14397 start_display (&it, w, startp);
14398 if (arg_scroll_conservatively <= scroll_limit)
14399 move_it_vertically (&it, amount_to_scroll);
14400 else
14401 {
14402 /* Extra precision for users who set scroll-conservatively
14403 to a large number: make sure the amount we scroll
14404 the window start is never less than amount_to_scroll,
14405 which was computed as distance from window bottom to
14406 point. This matters when lines at window top and lines
14407 below window bottom have different height. */
14408 struct it it1;
14409 void *it1data = NULL;
14410 /* We use a temporary it1 because line_bottom_y can modify
14411 its argument, if it moves one line down; see there. */
14412 int start_y;
14413
14414 SAVE_IT (it1, it, it1data);
14415 start_y = line_bottom_y (&it1);
14416 do {
14417 RESTORE_IT (&it, &it, it1data);
14418 move_it_by_lines (&it, 1);
14419 SAVE_IT (it1, it, it1data);
14420 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14421 }
14422
14423 /* If STARTP is unchanged, move it down another screen line. */
14424 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14425 move_it_by_lines (&it, 1);
14426 startp = it.current.pos;
14427 }
14428 else
14429 {
14430 struct text_pos scroll_margin_pos = startp;
14431
14432 /* See if point is inside the scroll margin at the top of the
14433 window. */
14434 if (this_scroll_margin)
14435 {
14436 start_display (&it, w, startp);
14437 move_it_vertically (&it, this_scroll_margin);
14438 scroll_margin_pos = it.current.pos;
14439 }
14440
14441 if (PT < CHARPOS (scroll_margin_pos))
14442 {
14443 /* Point is in the scroll margin at the top of the window or
14444 above what is displayed in the window. */
14445 int y0, y_to_move;
14446
14447 /* Compute the vertical distance from PT to the scroll
14448 margin position. Move as far as scroll_max allows, or
14449 one screenful, or 10 screen lines, whichever is largest.
14450 Give up if distance is greater than scroll_max. */
14451 SET_TEXT_POS (pos, PT, PT_BYTE);
14452 start_display (&it, w, pos);
14453 y0 = it.current_y;
14454 y_to_move = max (it.last_visible_y,
14455 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14456 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14457 y_to_move, -1,
14458 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14459 dy = it.current_y - y0;
14460 if (dy > scroll_max)
14461 return SCROLLING_FAILED;
14462
14463 /* Compute new window start. */
14464 start_display (&it, w, startp);
14465
14466 if (arg_scroll_conservatively)
14467 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14468 max (scroll_step, temp_scroll_step));
14469 else if (scroll_step || temp_scroll_step)
14470 amount_to_scroll = scroll_max;
14471 else
14472 {
14473 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14474 height = WINDOW_BOX_TEXT_HEIGHT (w);
14475 if (NUMBERP (aggressive))
14476 {
14477 double float_amount = XFLOATINT (aggressive) * height;
14478 amount_to_scroll = float_amount;
14479 if (amount_to_scroll == 0 && float_amount > 0)
14480 amount_to_scroll = 1;
14481 amount_to_scroll -=
14482 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14483 /* Don't let point enter the scroll margin near
14484 bottom of the window. */
14485 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14486 amount_to_scroll = height - 2*this_scroll_margin + dy;
14487 }
14488 }
14489
14490 if (amount_to_scroll <= 0)
14491 return SCROLLING_FAILED;
14492
14493 move_it_vertically_backward (&it, amount_to_scroll);
14494 startp = it.current.pos;
14495 }
14496 }
14497
14498 /* Run window scroll functions. */
14499 startp = run_window_scroll_functions (window, startp);
14500
14501 /* Display the window. Give up if new fonts are loaded, or if point
14502 doesn't appear. */
14503 if (!try_window (window, startp, 0))
14504 rc = SCROLLING_NEED_LARGER_MATRICES;
14505 else if (w->cursor.vpos < 0)
14506 {
14507 clear_glyph_matrix (w->desired_matrix);
14508 rc = SCROLLING_FAILED;
14509 }
14510 else
14511 {
14512 /* Maybe forget recorded base line for line number display. */
14513 if (!just_this_one_p
14514 || current_buffer->clip_changed
14515 || BEG_UNCHANGED < CHARPOS (startp))
14516 w->base_line_number = Qnil;
14517
14518 /* If cursor ends up on a partially visible line,
14519 treat that as being off the bottom of the screen. */
14520 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14521 /* It's possible that the cursor is on the first line of the
14522 buffer, which is partially obscured due to a vscroll
14523 (Bug#7537). In that case, avoid looping forever . */
14524 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14525 {
14526 clear_glyph_matrix (w->desired_matrix);
14527 ++extra_scroll_margin_lines;
14528 goto too_near_end;
14529 }
14530 rc = SCROLLING_SUCCESS;
14531 }
14532
14533 return rc;
14534 }
14535
14536
14537 /* Compute a suitable window start for window W if display of W starts
14538 on a continuation line. Value is non-zero if a new window start
14539 was computed.
14540
14541 The new window start will be computed, based on W's width, starting
14542 from the start of the continued line. It is the start of the
14543 screen line with the minimum distance from the old start W->start. */
14544
14545 static int
14546 compute_window_start_on_continuation_line (struct window *w)
14547 {
14548 struct text_pos pos, start_pos;
14549 int window_start_changed_p = 0;
14550
14551 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14552
14553 /* If window start is on a continuation line... Window start may be
14554 < BEGV in case there's invisible text at the start of the
14555 buffer (M-x rmail, for example). */
14556 if (CHARPOS (start_pos) > BEGV
14557 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14558 {
14559 struct it it;
14560 struct glyph_row *row;
14561
14562 /* Handle the case that the window start is out of range. */
14563 if (CHARPOS (start_pos) < BEGV)
14564 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14565 else if (CHARPOS (start_pos) > ZV)
14566 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14567
14568 /* Find the start of the continued line. This should be fast
14569 because scan_buffer is fast (newline cache). */
14570 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14571 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14572 row, DEFAULT_FACE_ID);
14573 reseat_at_previous_visible_line_start (&it);
14574
14575 /* If the line start is "too far" away from the window start,
14576 say it takes too much time to compute a new window start. */
14577 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14578 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14579 {
14580 int min_distance, distance;
14581
14582 /* Move forward by display lines to find the new window
14583 start. If window width was enlarged, the new start can
14584 be expected to be > the old start. If window width was
14585 decreased, the new window start will be < the old start.
14586 So, we're looking for the display line start with the
14587 minimum distance from the old window start. */
14588 pos = it.current.pos;
14589 min_distance = INFINITY;
14590 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14591 distance < min_distance)
14592 {
14593 min_distance = distance;
14594 pos = it.current.pos;
14595 move_it_by_lines (&it, 1);
14596 }
14597
14598 /* Set the window start there. */
14599 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14600 window_start_changed_p = 1;
14601 }
14602 }
14603
14604 return window_start_changed_p;
14605 }
14606
14607
14608 /* Try cursor movement in case text has not changed in window WINDOW,
14609 with window start STARTP. Value is
14610
14611 CURSOR_MOVEMENT_SUCCESS if successful
14612
14613 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14614
14615 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14616 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14617 we want to scroll as if scroll-step were set to 1. See the code.
14618
14619 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14620 which case we have to abort this redisplay, and adjust matrices
14621 first. */
14622
14623 enum
14624 {
14625 CURSOR_MOVEMENT_SUCCESS,
14626 CURSOR_MOVEMENT_CANNOT_BE_USED,
14627 CURSOR_MOVEMENT_MUST_SCROLL,
14628 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14629 };
14630
14631 static int
14632 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14633 {
14634 struct window *w = XWINDOW (window);
14635 struct frame *f = XFRAME (w->frame);
14636 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14637
14638 #if GLYPH_DEBUG
14639 if (inhibit_try_cursor_movement)
14640 return rc;
14641 #endif
14642
14643 /* Handle case where text has not changed, only point, and it has
14644 not moved off the frame. */
14645 if (/* Point may be in this window. */
14646 PT >= CHARPOS (startp)
14647 /* Selective display hasn't changed. */
14648 && !current_buffer->clip_changed
14649 /* Function force-mode-line-update is used to force a thorough
14650 redisplay. It sets either windows_or_buffers_changed or
14651 update_mode_lines. So don't take a shortcut here for these
14652 cases. */
14653 && !update_mode_lines
14654 && !windows_or_buffers_changed
14655 && !cursor_type_changed
14656 /* Can't use this case if highlighting a region. When a
14657 region exists, cursor movement has to do more than just
14658 set the cursor. */
14659 && !(!NILP (Vtransient_mark_mode)
14660 && !NILP (BVAR (current_buffer, mark_active)))
14661 && NILP (w->region_showing)
14662 && NILP (Vshow_trailing_whitespace)
14663 /* Right after splitting windows, last_point may be nil. */
14664 && INTEGERP (w->last_point)
14665 /* This code is not used for mini-buffer for the sake of the case
14666 of redisplaying to replace an echo area message; since in
14667 that case the mini-buffer contents per se are usually
14668 unchanged. This code is of no real use in the mini-buffer
14669 since the handling of this_line_start_pos, etc., in redisplay
14670 handles the same cases. */
14671 && !EQ (window, minibuf_window)
14672 /* When splitting windows or for new windows, it happens that
14673 redisplay is called with a nil window_end_vpos or one being
14674 larger than the window. This should really be fixed in
14675 window.c. I don't have this on my list, now, so we do
14676 approximately the same as the old redisplay code. --gerd. */
14677 && INTEGERP (w->window_end_vpos)
14678 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14679 && (FRAME_WINDOW_P (f)
14680 || !overlay_arrow_in_current_buffer_p ()))
14681 {
14682 int this_scroll_margin, top_scroll_margin;
14683 struct glyph_row *row = NULL;
14684
14685 #if GLYPH_DEBUG
14686 debug_method_add (w, "cursor movement");
14687 #endif
14688
14689 /* Scroll if point within this distance from the top or bottom
14690 of the window. This is a pixel value. */
14691 if (scroll_margin > 0)
14692 {
14693 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14694 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14695 }
14696 else
14697 this_scroll_margin = 0;
14698
14699 top_scroll_margin = this_scroll_margin;
14700 if (WINDOW_WANTS_HEADER_LINE_P (w))
14701 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14702
14703 /* Start with the row the cursor was displayed during the last
14704 not paused redisplay. Give up if that row is not valid. */
14705 if (w->last_cursor.vpos < 0
14706 || w->last_cursor.vpos >= w->current_matrix->nrows)
14707 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14708 else
14709 {
14710 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14711 if (row->mode_line_p)
14712 ++row;
14713 if (!row->enabled_p)
14714 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14715 }
14716
14717 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14718 {
14719 int scroll_p = 0, must_scroll = 0;
14720 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14721
14722 if (PT > XFASTINT (w->last_point))
14723 {
14724 /* Point has moved forward. */
14725 while (MATRIX_ROW_END_CHARPOS (row) < PT
14726 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14727 {
14728 xassert (row->enabled_p);
14729 ++row;
14730 }
14731
14732 /* If the end position of a row equals the start
14733 position of the next row, and PT is at that position,
14734 we would rather display cursor in the next line. */
14735 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14736 && MATRIX_ROW_END_CHARPOS (row) == PT
14737 && row < w->current_matrix->rows
14738 + w->current_matrix->nrows - 1
14739 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14740 && !cursor_row_p (row))
14741 ++row;
14742
14743 /* If within the scroll margin, scroll. Note that
14744 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14745 the next line would be drawn, and that
14746 this_scroll_margin can be zero. */
14747 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14748 || PT > MATRIX_ROW_END_CHARPOS (row)
14749 /* Line is completely visible last line in window
14750 and PT is to be set in the next line. */
14751 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14752 && PT == MATRIX_ROW_END_CHARPOS (row)
14753 && !row->ends_at_zv_p
14754 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14755 scroll_p = 1;
14756 }
14757 else if (PT < XFASTINT (w->last_point))
14758 {
14759 /* Cursor has to be moved backward. Note that PT >=
14760 CHARPOS (startp) because of the outer if-statement. */
14761 while (!row->mode_line_p
14762 && (MATRIX_ROW_START_CHARPOS (row) > PT
14763 || (MATRIX_ROW_START_CHARPOS (row) == PT
14764 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14765 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14766 row > w->current_matrix->rows
14767 && (row-1)->ends_in_newline_from_string_p))))
14768 && (row->y > top_scroll_margin
14769 || CHARPOS (startp) == BEGV))
14770 {
14771 xassert (row->enabled_p);
14772 --row;
14773 }
14774
14775 /* Consider the following case: Window starts at BEGV,
14776 there is invisible, intangible text at BEGV, so that
14777 display starts at some point START > BEGV. It can
14778 happen that we are called with PT somewhere between
14779 BEGV and START. Try to handle that case. */
14780 if (row < w->current_matrix->rows
14781 || row->mode_line_p)
14782 {
14783 row = w->current_matrix->rows;
14784 if (row->mode_line_p)
14785 ++row;
14786 }
14787
14788 /* Due to newlines in overlay strings, we may have to
14789 skip forward over overlay strings. */
14790 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14791 && MATRIX_ROW_END_CHARPOS (row) == PT
14792 && !cursor_row_p (row))
14793 ++row;
14794
14795 /* If within the scroll margin, scroll. */
14796 if (row->y < top_scroll_margin
14797 && CHARPOS (startp) != BEGV)
14798 scroll_p = 1;
14799 }
14800 else
14801 {
14802 /* Cursor did not move. So don't scroll even if cursor line
14803 is partially visible, as it was so before. */
14804 rc = CURSOR_MOVEMENT_SUCCESS;
14805 }
14806
14807 if (PT < MATRIX_ROW_START_CHARPOS (row)
14808 || PT > MATRIX_ROW_END_CHARPOS (row))
14809 {
14810 /* if PT is not in the glyph row, give up. */
14811 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14812 must_scroll = 1;
14813 }
14814 else if (rc != CURSOR_MOVEMENT_SUCCESS
14815 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14816 {
14817 /* If rows are bidi-reordered and point moved, back up
14818 until we find a row that does not belong to a
14819 continuation line. This is because we must consider
14820 all rows of a continued line as candidates for the
14821 new cursor positioning, since row start and end
14822 positions change non-linearly with vertical position
14823 in such rows. */
14824 /* FIXME: Revisit this when glyph ``spilling'' in
14825 continuation lines' rows is implemented for
14826 bidi-reordered rows. */
14827 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14828 {
14829 xassert (row->enabled_p);
14830 --row;
14831 /* If we hit the beginning of the displayed portion
14832 without finding the first row of a continued
14833 line, give up. */
14834 if (row <= w->current_matrix->rows)
14835 {
14836 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14837 break;
14838 }
14839
14840 }
14841 }
14842 if (must_scroll)
14843 ;
14844 else if (rc != CURSOR_MOVEMENT_SUCCESS
14845 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14846 && make_cursor_line_fully_visible_p)
14847 {
14848 if (PT == MATRIX_ROW_END_CHARPOS (row)
14849 && !row->ends_at_zv_p
14850 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14851 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14852 else if (row->height > window_box_height (w))
14853 {
14854 /* If we end up in a partially visible line, let's
14855 make it fully visible, except when it's taller
14856 than the window, in which case we can't do much
14857 about it. */
14858 *scroll_step = 1;
14859 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14860 }
14861 else
14862 {
14863 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14864 if (!cursor_row_fully_visible_p (w, 0, 1))
14865 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14866 else
14867 rc = CURSOR_MOVEMENT_SUCCESS;
14868 }
14869 }
14870 else if (scroll_p)
14871 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14872 else if (rc != CURSOR_MOVEMENT_SUCCESS
14873 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14874 {
14875 /* With bidi-reordered rows, there could be more than
14876 one candidate row whose start and end positions
14877 occlude point. We need to let set_cursor_from_row
14878 find the best candidate. */
14879 /* FIXME: Revisit this when glyph ``spilling'' in
14880 continuation lines' rows is implemented for
14881 bidi-reordered rows. */
14882 int rv = 0;
14883
14884 do
14885 {
14886 int at_zv_p = 0, exact_match_p = 0;
14887
14888 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14889 && PT <= MATRIX_ROW_END_CHARPOS (row)
14890 && cursor_row_p (row))
14891 rv |= set_cursor_from_row (w, row, w->current_matrix,
14892 0, 0, 0, 0);
14893 /* As soon as we've found the exact match for point,
14894 or the first suitable row whose ends_at_zv_p flag
14895 is set, we are done. */
14896 at_zv_p =
14897 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14898 if (rv && !at_zv_p
14899 && w->cursor.hpos >= 0
14900 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
14901 w->cursor.vpos))
14902 {
14903 struct glyph_row *candidate =
14904 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14905 struct glyph *g =
14906 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14907 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
14908
14909 exact_match_p =
14910 (BUFFERP (g->object) && g->charpos == PT)
14911 || (INTEGERP (g->object)
14912 && (g->charpos == PT
14913 || (g->charpos == 0 && endpos - 1 == PT)));
14914 }
14915 if (rv && (at_zv_p || exact_match_p))
14916 {
14917 rc = CURSOR_MOVEMENT_SUCCESS;
14918 break;
14919 }
14920 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14921 break;
14922 ++row;
14923 }
14924 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14925 || row->continued_p)
14926 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14927 || (MATRIX_ROW_START_CHARPOS (row) == PT
14928 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14929 /* If we didn't find any candidate rows, or exited the
14930 loop before all the candidates were examined, signal
14931 to the caller that this method failed. */
14932 if (rc != CURSOR_MOVEMENT_SUCCESS
14933 && !(rv
14934 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14935 && !row->continued_p))
14936 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14937 else if (rv)
14938 rc = CURSOR_MOVEMENT_SUCCESS;
14939 }
14940 else
14941 {
14942 do
14943 {
14944 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14945 {
14946 rc = CURSOR_MOVEMENT_SUCCESS;
14947 break;
14948 }
14949 ++row;
14950 }
14951 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14952 && MATRIX_ROW_START_CHARPOS (row) == PT
14953 && cursor_row_p (row));
14954 }
14955 }
14956 }
14957
14958 return rc;
14959 }
14960
14961 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14962 static
14963 #endif
14964 void
14965 set_vertical_scroll_bar (struct window *w)
14966 {
14967 ptrdiff_t start, end, whole;
14968
14969 /* Calculate the start and end positions for the current window.
14970 At some point, it would be nice to choose between scrollbars
14971 which reflect the whole buffer size, with special markers
14972 indicating narrowing, and scrollbars which reflect only the
14973 visible region.
14974
14975 Note that mini-buffers sometimes aren't displaying any text. */
14976 if (!MINI_WINDOW_P (w)
14977 || (w == XWINDOW (minibuf_window)
14978 && NILP (echo_area_buffer[0])))
14979 {
14980 struct buffer *buf = XBUFFER (w->buffer);
14981 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14982 start = marker_position (w->start) - BUF_BEGV (buf);
14983 /* I don't think this is guaranteed to be right. For the
14984 moment, we'll pretend it is. */
14985 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14986
14987 if (end < start)
14988 end = start;
14989 if (whole < (end - start))
14990 whole = end - start;
14991 }
14992 else
14993 start = end = whole = 0;
14994
14995 /* Indicate what this scroll bar ought to be displaying now. */
14996 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14997 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14998 (w, end - start, whole, start);
14999 }
15000
15001
15002 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15003 selected_window is redisplayed.
15004
15005 We can return without actually redisplaying the window if
15006 fonts_changed_p is nonzero. In that case, redisplay_internal will
15007 retry. */
15008
15009 static void
15010 redisplay_window (Lisp_Object window, int just_this_one_p)
15011 {
15012 struct window *w = XWINDOW (window);
15013 struct frame *f = XFRAME (w->frame);
15014 struct buffer *buffer = XBUFFER (w->buffer);
15015 struct buffer *old = current_buffer;
15016 struct text_pos lpoint, opoint, startp;
15017 int update_mode_line;
15018 int tem;
15019 struct it it;
15020 /* Record it now because it's overwritten. */
15021 int current_matrix_up_to_date_p = 0;
15022 int used_current_matrix_p = 0;
15023 /* This is less strict than current_matrix_up_to_date_p.
15024 It indictes that the buffer contents and narrowing are unchanged. */
15025 int buffer_unchanged_p = 0;
15026 int temp_scroll_step = 0;
15027 ptrdiff_t count = SPECPDL_INDEX ();
15028 int rc;
15029 int centering_position = -1;
15030 int last_line_misfit = 0;
15031 ptrdiff_t beg_unchanged, end_unchanged;
15032
15033 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15034 opoint = lpoint;
15035
15036 /* W must be a leaf window here. */
15037 xassert (!NILP (w->buffer));
15038 #if GLYPH_DEBUG
15039 *w->desired_matrix->method = 0;
15040 #endif
15041
15042 restart:
15043 reconsider_clip_changes (w, buffer);
15044
15045 /* Has the mode line to be updated? */
15046 update_mode_line = (!NILP (w->update_mode_line)
15047 || update_mode_lines
15048 || buffer->clip_changed
15049 || buffer->prevent_redisplay_optimizations_p);
15050
15051 if (MINI_WINDOW_P (w))
15052 {
15053 if (w == XWINDOW (echo_area_window)
15054 && !NILP (echo_area_buffer[0]))
15055 {
15056 if (update_mode_line)
15057 /* We may have to update a tty frame's menu bar or a
15058 tool-bar. Example `M-x C-h C-h C-g'. */
15059 goto finish_menu_bars;
15060 else
15061 /* We've already displayed the echo area glyphs in this window. */
15062 goto finish_scroll_bars;
15063 }
15064 else if ((w != XWINDOW (minibuf_window)
15065 || minibuf_level == 0)
15066 /* When buffer is nonempty, redisplay window normally. */
15067 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15068 /* Quail displays non-mini buffers in minibuffer window.
15069 In that case, redisplay the window normally. */
15070 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15071 {
15072 /* W is a mini-buffer window, but it's not active, so clear
15073 it. */
15074 int yb = window_text_bottom_y (w);
15075 struct glyph_row *row;
15076 int y;
15077
15078 for (y = 0, row = w->desired_matrix->rows;
15079 y < yb;
15080 y += row->height, ++row)
15081 blank_row (w, row, y);
15082 goto finish_scroll_bars;
15083 }
15084
15085 clear_glyph_matrix (w->desired_matrix);
15086 }
15087
15088 /* Otherwise set up data on this window; select its buffer and point
15089 value. */
15090 /* Really select the buffer, for the sake of buffer-local
15091 variables. */
15092 set_buffer_internal_1 (XBUFFER (w->buffer));
15093
15094 current_matrix_up_to_date_p
15095 = (!NILP (w->window_end_valid)
15096 && !current_buffer->clip_changed
15097 && !current_buffer->prevent_redisplay_optimizations_p
15098 && XFASTINT (w->last_modified) >= MODIFF
15099 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15100
15101 /* Run the window-bottom-change-functions
15102 if it is possible that the text on the screen has changed
15103 (either due to modification of the text, or any other reason). */
15104 if (!current_matrix_up_to_date_p
15105 && !NILP (Vwindow_text_change_functions))
15106 {
15107 safe_run_hooks (Qwindow_text_change_functions);
15108 goto restart;
15109 }
15110
15111 beg_unchanged = BEG_UNCHANGED;
15112 end_unchanged = END_UNCHANGED;
15113
15114 SET_TEXT_POS (opoint, PT, PT_BYTE);
15115
15116 specbind (Qinhibit_point_motion_hooks, Qt);
15117
15118 buffer_unchanged_p
15119 = (!NILP (w->window_end_valid)
15120 && !current_buffer->clip_changed
15121 && XFASTINT (w->last_modified) >= MODIFF
15122 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15123
15124 /* When windows_or_buffers_changed is non-zero, we can't rely on
15125 the window end being valid, so set it to nil there. */
15126 if (windows_or_buffers_changed)
15127 {
15128 /* If window starts on a continuation line, maybe adjust the
15129 window start in case the window's width changed. */
15130 if (XMARKER (w->start)->buffer == current_buffer)
15131 compute_window_start_on_continuation_line (w);
15132
15133 w->window_end_valid = Qnil;
15134 }
15135
15136 /* Some sanity checks. */
15137 CHECK_WINDOW_END (w);
15138 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15139 abort ();
15140 if (BYTEPOS (opoint) < CHARPOS (opoint))
15141 abort ();
15142
15143 /* If %c is in mode line, update it if needed. */
15144 if (!NILP (w->column_number_displayed)
15145 /* This alternative quickly identifies a common case
15146 where no change is needed. */
15147 && !(PT == XFASTINT (w->last_point)
15148 && XFASTINT (w->last_modified) >= MODIFF
15149 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15150 && (XFASTINT (w->column_number_displayed) != current_column ()))
15151 update_mode_line = 1;
15152
15153 /* Count number of windows showing the selected buffer. An indirect
15154 buffer counts as its base buffer. */
15155 if (!just_this_one_p)
15156 {
15157 struct buffer *current_base, *window_base;
15158 current_base = current_buffer;
15159 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15160 if (current_base->base_buffer)
15161 current_base = current_base->base_buffer;
15162 if (window_base->base_buffer)
15163 window_base = window_base->base_buffer;
15164 if (current_base == window_base)
15165 buffer_shared++;
15166 }
15167
15168 /* Point refers normally to the selected window. For any other
15169 window, set up appropriate value. */
15170 if (!EQ (window, selected_window))
15171 {
15172 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15173 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15174 if (new_pt < BEGV)
15175 {
15176 new_pt = BEGV;
15177 new_pt_byte = BEGV_BYTE;
15178 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15179 }
15180 else if (new_pt > (ZV - 1))
15181 {
15182 new_pt = ZV;
15183 new_pt_byte = ZV_BYTE;
15184 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15185 }
15186
15187 /* We don't use SET_PT so that the point-motion hooks don't run. */
15188 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15189 }
15190
15191 /* If any of the character widths specified in the display table
15192 have changed, invalidate the width run cache. It's true that
15193 this may be a bit late to catch such changes, but the rest of
15194 redisplay goes (non-fatally) haywire when the display table is
15195 changed, so why should we worry about doing any better? */
15196 if (current_buffer->width_run_cache)
15197 {
15198 struct Lisp_Char_Table *disptab = buffer_display_table ();
15199
15200 if (! disptab_matches_widthtab (disptab,
15201 XVECTOR (BVAR (current_buffer, width_table))))
15202 {
15203 invalidate_region_cache (current_buffer,
15204 current_buffer->width_run_cache,
15205 BEG, Z);
15206 recompute_width_table (current_buffer, disptab);
15207 }
15208 }
15209
15210 /* If window-start is screwed up, choose a new one. */
15211 if (XMARKER (w->start)->buffer != current_buffer)
15212 goto recenter;
15213
15214 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15215
15216 /* If someone specified a new starting point but did not insist,
15217 check whether it can be used. */
15218 if (!NILP (w->optional_new_start)
15219 && CHARPOS (startp) >= BEGV
15220 && CHARPOS (startp) <= ZV)
15221 {
15222 w->optional_new_start = Qnil;
15223 start_display (&it, w, startp);
15224 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15225 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15226 if (IT_CHARPOS (it) == PT)
15227 w->force_start = Qt;
15228 /* IT may overshoot PT if text at PT is invisible. */
15229 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15230 w->force_start = Qt;
15231 }
15232
15233 force_start:
15234
15235 /* Handle case where place to start displaying has been specified,
15236 unless the specified location is outside the accessible range. */
15237 if (!NILP (w->force_start)
15238 || w->frozen_window_start_p)
15239 {
15240 /* We set this later on if we have to adjust point. */
15241 int new_vpos = -1;
15242
15243 w->force_start = Qnil;
15244 w->vscroll = 0;
15245 w->window_end_valid = Qnil;
15246
15247 /* Forget any recorded base line for line number display. */
15248 if (!buffer_unchanged_p)
15249 w->base_line_number = Qnil;
15250
15251 /* Redisplay the mode line. Select the buffer properly for that.
15252 Also, run the hook window-scroll-functions
15253 because we have scrolled. */
15254 /* Note, we do this after clearing force_start because
15255 if there's an error, it is better to forget about force_start
15256 than to get into an infinite loop calling the hook functions
15257 and having them get more errors. */
15258 if (!update_mode_line
15259 || ! NILP (Vwindow_scroll_functions))
15260 {
15261 update_mode_line = 1;
15262 w->update_mode_line = Qt;
15263 startp = run_window_scroll_functions (window, startp);
15264 }
15265
15266 w->last_modified = make_number (0);
15267 w->last_overlay_modified = make_number (0);
15268 if (CHARPOS (startp) < BEGV)
15269 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15270 else if (CHARPOS (startp) > ZV)
15271 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15272
15273 /* Redisplay, then check if cursor has been set during the
15274 redisplay. Give up if new fonts were loaded. */
15275 /* We used to issue a CHECK_MARGINS argument to try_window here,
15276 but this causes scrolling to fail when point begins inside
15277 the scroll margin (bug#148) -- cyd */
15278 if (!try_window (window, startp, 0))
15279 {
15280 w->force_start = Qt;
15281 clear_glyph_matrix (w->desired_matrix);
15282 goto need_larger_matrices;
15283 }
15284
15285 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15286 {
15287 /* If point does not appear, try to move point so it does
15288 appear. The desired matrix has been built above, so we
15289 can use it here. */
15290 new_vpos = window_box_height (w) / 2;
15291 }
15292
15293 if (!cursor_row_fully_visible_p (w, 0, 0))
15294 {
15295 /* Point does appear, but on a line partly visible at end of window.
15296 Move it back to a fully-visible line. */
15297 new_vpos = window_box_height (w);
15298 }
15299
15300 /* If we need to move point for either of the above reasons,
15301 now actually do it. */
15302 if (new_vpos >= 0)
15303 {
15304 struct glyph_row *row;
15305
15306 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15307 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15308 ++row;
15309
15310 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15311 MATRIX_ROW_START_BYTEPOS (row));
15312
15313 if (w != XWINDOW (selected_window))
15314 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15315 else if (current_buffer == old)
15316 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15317
15318 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15319
15320 /* If we are highlighting the region, then we just changed
15321 the region, so redisplay to show it. */
15322 if (!NILP (Vtransient_mark_mode)
15323 && !NILP (BVAR (current_buffer, mark_active)))
15324 {
15325 clear_glyph_matrix (w->desired_matrix);
15326 if (!try_window (window, startp, 0))
15327 goto need_larger_matrices;
15328 }
15329 }
15330
15331 #if GLYPH_DEBUG
15332 debug_method_add (w, "forced window start");
15333 #endif
15334 goto done;
15335 }
15336
15337 /* Handle case where text has not changed, only point, and it has
15338 not moved off the frame, and we are not retrying after hscroll.
15339 (current_matrix_up_to_date_p is nonzero when retrying.) */
15340 if (current_matrix_up_to_date_p
15341 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15342 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15343 {
15344 switch (rc)
15345 {
15346 case CURSOR_MOVEMENT_SUCCESS:
15347 used_current_matrix_p = 1;
15348 goto done;
15349
15350 case CURSOR_MOVEMENT_MUST_SCROLL:
15351 goto try_to_scroll;
15352
15353 default:
15354 abort ();
15355 }
15356 }
15357 /* If current starting point was originally the beginning of a line
15358 but no longer is, find a new starting point. */
15359 else if (!NILP (w->start_at_line_beg)
15360 && !(CHARPOS (startp) <= BEGV
15361 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15362 {
15363 #if GLYPH_DEBUG
15364 debug_method_add (w, "recenter 1");
15365 #endif
15366 goto recenter;
15367 }
15368
15369 /* Try scrolling with try_window_id. Value is > 0 if update has
15370 been done, it is -1 if we know that the same window start will
15371 not work. It is 0 if unsuccessful for some other reason. */
15372 else if ((tem = try_window_id (w)) != 0)
15373 {
15374 #if GLYPH_DEBUG
15375 debug_method_add (w, "try_window_id %d", tem);
15376 #endif
15377
15378 if (fonts_changed_p)
15379 goto need_larger_matrices;
15380 if (tem > 0)
15381 goto done;
15382
15383 /* Otherwise try_window_id has returned -1 which means that we
15384 don't want the alternative below this comment to execute. */
15385 }
15386 else if (CHARPOS (startp) >= BEGV
15387 && CHARPOS (startp) <= ZV
15388 && PT >= CHARPOS (startp)
15389 && (CHARPOS (startp) < ZV
15390 /* Avoid starting at end of buffer. */
15391 || CHARPOS (startp) == BEGV
15392 || (XFASTINT (w->last_modified) >= MODIFF
15393 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15394 {
15395 int d1, d2, d3, d4, d5, d6;
15396
15397 /* If first window line is a continuation line, and window start
15398 is inside the modified region, but the first change is before
15399 current window start, we must select a new window start.
15400
15401 However, if this is the result of a down-mouse event (e.g. by
15402 extending the mouse-drag-overlay), we don't want to select a
15403 new window start, since that would change the position under
15404 the mouse, resulting in an unwanted mouse-movement rather
15405 than a simple mouse-click. */
15406 if (NILP (w->start_at_line_beg)
15407 && NILP (do_mouse_tracking)
15408 && CHARPOS (startp) > BEGV
15409 && CHARPOS (startp) > BEG + beg_unchanged
15410 && CHARPOS (startp) <= Z - end_unchanged
15411 /* Even if w->start_at_line_beg is nil, a new window may
15412 start at a line_beg, since that's how set_buffer_window
15413 sets it. So, we need to check the return value of
15414 compute_window_start_on_continuation_line. (See also
15415 bug#197). */
15416 && XMARKER (w->start)->buffer == current_buffer
15417 && compute_window_start_on_continuation_line (w)
15418 /* It doesn't make sense to force the window start like we
15419 do at label force_start if it is already known that point
15420 will not be visible in the resulting window, because
15421 doing so will move point from its correct position
15422 instead of scrolling the window to bring point into view.
15423 See bug#9324. */
15424 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15425 {
15426 w->force_start = Qt;
15427 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15428 goto force_start;
15429 }
15430
15431 #if GLYPH_DEBUG
15432 debug_method_add (w, "same window start");
15433 #endif
15434
15435 /* Try to redisplay starting at same place as before.
15436 If point has not moved off frame, accept the results. */
15437 if (!current_matrix_up_to_date_p
15438 /* Don't use try_window_reusing_current_matrix in this case
15439 because a window scroll function can have changed the
15440 buffer. */
15441 || !NILP (Vwindow_scroll_functions)
15442 || MINI_WINDOW_P (w)
15443 || !(used_current_matrix_p
15444 = try_window_reusing_current_matrix (w)))
15445 {
15446 IF_DEBUG (debug_method_add (w, "1"));
15447 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15448 /* -1 means we need to scroll.
15449 0 means we need new matrices, but fonts_changed_p
15450 is set in that case, so we will detect it below. */
15451 goto try_to_scroll;
15452 }
15453
15454 if (fonts_changed_p)
15455 goto need_larger_matrices;
15456
15457 if (w->cursor.vpos >= 0)
15458 {
15459 if (!just_this_one_p
15460 || current_buffer->clip_changed
15461 || BEG_UNCHANGED < CHARPOS (startp))
15462 /* Forget any recorded base line for line number display. */
15463 w->base_line_number = Qnil;
15464
15465 if (!cursor_row_fully_visible_p (w, 1, 0))
15466 {
15467 clear_glyph_matrix (w->desired_matrix);
15468 last_line_misfit = 1;
15469 }
15470 /* Drop through and scroll. */
15471 else
15472 goto done;
15473 }
15474 else
15475 clear_glyph_matrix (w->desired_matrix);
15476 }
15477
15478 try_to_scroll:
15479
15480 w->last_modified = make_number (0);
15481 w->last_overlay_modified = make_number (0);
15482
15483 /* Redisplay the mode line. Select the buffer properly for that. */
15484 if (!update_mode_line)
15485 {
15486 update_mode_line = 1;
15487 w->update_mode_line = Qt;
15488 }
15489
15490 /* Try to scroll by specified few lines. */
15491 if ((scroll_conservatively
15492 || emacs_scroll_step
15493 || temp_scroll_step
15494 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15495 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15496 && CHARPOS (startp) >= BEGV
15497 && CHARPOS (startp) <= ZV)
15498 {
15499 /* The function returns -1 if new fonts were loaded, 1 if
15500 successful, 0 if not successful. */
15501 int ss = try_scrolling (window, just_this_one_p,
15502 scroll_conservatively,
15503 emacs_scroll_step,
15504 temp_scroll_step, last_line_misfit);
15505 switch (ss)
15506 {
15507 case SCROLLING_SUCCESS:
15508 goto done;
15509
15510 case SCROLLING_NEED_LARGER_MATRICES:
15511 goto need_larger_matrices;
15512
15513 case SCROLLING_FAILED:
15514 break;
15515
15516 default:
15517 abort ();
15518 }
15519 }
15520
15521 /* Finally, just choose a place to start which positions point
15522 according to user preferences. */
15523
15524 recenter:
15525
15526 #if GLYPH_DEBUG
15527 debug_method_add (w, "recenter");
15528 #endif
15529
15530 /* w->vscroll = 0; */
15531
15532 /* Forget any previously recorded base line for line number display. */
15533 if (!buffer_unchanged_p)
15534 w->base_line_number = Qnil;
15535
15536 /* Determine the window start relative to point. */
15537 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15538 it.current_y = it.last_visible_y;
15539 if (centering_position < 0)
15540 {
15541 int margin =
15542 scroll_margin > 0
15543 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15544 : 0;
15545 ptrdiff_t margin_pos = CHARPOS (startp);
15546 int scrolling_up;
15547 Lisp_Object aggressive;
15548
15549 /* If there is a scroll margin at the top of the window, find
15550 its character position. */
15551 if (margin
15552 /* Cannot call start_display if startp is not in the
15553 accessible region of the buffer. This can happen when we
15554 have just switched to a different buffer and/or changed
15555 its restriction. In that case, startp is initialized to
15556 the character position 1 (BEG) because we did not yet
15557 have chance to display the buffer even once. */
15558 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15559 {
15560 struct it it1;
15561 void *it1data = NULL;
15562
15563 SAVE_IT (it1, it, it1data);
15564 start_display (&it1, w, startp);
15565 move_it_vertically (&it1, margin);
15566 margin_pos = IT_CHARPOS (it1);
15567 RESTORE_IT (&it, &it, it1data);
15568 }
15569 scrolling_up = PT > margin_pos;
15570 aggressive =
15571 scrolling_up
15572 ? BVAR (current_buffer, scroll_up_aggressively)
15573 : BVAR (current_buffer, scroll_down_aggressively);
15574
15575 if (!MINI_WINDOW_P (w)
15576 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15577 {
15578 int pt_offset = 0;
15579
15580 /* Setting scroll-conservatively overrides
15581 scroll-*-aggressively. */
15582 if (!scroll_conservatively && NUMBERP (aggressive))
15583 {
15584 double float_amount = XFLOATINT (aggressive);
15585
15586 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15587 if (pt_offset == 0 && float_amount > 0)
15588 pt_offset = 1;
15589 if (pt_offset)
15590 margin -= 1;
15591 }
15592 /* Compute how much to move the window start backward from
15593 point so that point will be displayed where the user
15594 wants it. */
15595 if (scrolling_up)
15596 {
15597 centering_position = it.last_visible_y;
15598 if (pt_offset)
15599 centering_position -= pt_offset;
15600 centering_position -=
15601 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15602 + WINDOW_HEADER_LINE_HEIGHT (w);
15603 /* Don't let point enter the scroll margin near top of
15604 the window. */
15605 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15606 centering_position = margin * FRAME_LINE_HEIGHT (f);
15607 }
15608 else
15609 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15610 }
15611 else
15612 /* Set the window start half the height of the window backward
15613 from point. */
15614 centering_position = window_box_height (w) / 2;
15615 }
15616 move_it_vertically_backward (&it, centering_position);
15617
15618 xassert (IT_CHARPOS (it) >= BEGV);
15619
15620 /* The function move_it_vertically_backward may move over more
15621 than the specified y-distance. If it->w is small, e.g. a
15622 mini-buffer window, we may end up in front of the window's
15623 display area. Start displaying at the start of the line
15624 containing PT in this case. */
15625 if (it.current_y <= 0)
15626 {
15627 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15628 move_it_vertically_backward (&it, 0);
15629 it.current_y = 0;
15630 }
15631
15632 it.current_x = it.hpos = 0;
15633
15634 /* Set the window start position here explicitly, to avoid an
15635 infinite loop in case the functions in window-scroll-functions
15636 get errors. */
15637 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15638
15639 /* Run scroll hooks. */
15640 startp = run_window_scroll_functions (window, it.current.pos);
15641
15642 /* Redisplay the window. */
15643 if (!current_matrix_up_to_date_p
15644 || windows_or_buffers_changed
15645 || cursor_type_changed
15646 /* Don't use try_window_reusing_current_matrix in this case
15647 because it can have changed the buffer. */
15648 || !NILP (Vwindow_scroll_functions)
15649 || !just_this_one_p
15650 || MINI_WINDOW_P (w)
15651 || !(used_current_matrix_p
15652 = try_window_reusing_current_matrix (w)))
15653 try_window (window, startp, 0);
15654
15655 /* If new fonts have been loaded (due to fontsets), give up. We
15656 have to start a new redisplay since we need to re-adjust glyph
15657 matrices. */
15658 if (fonts_changed_p)
15659 goto need_larger_matrices;
15660
15661 /* If cursor did not appear assume that the middle of the window is
15662 in the first line of the window. Do it again with the next line.
15663 (Imagine a window of height 100, displaying two lines of height
15664 60. Moving back 50 from it->last_visible_y will end in the first
15665 line.) */
15666 if (w->cursor.vpos < 0)
15667 {
15668 if (!NILP (w->window_end_valid)
15669 && PT >= Z - XFASTINT (w->window_end_pos))
15670 {
15671 clear_glyph_matrix (w->desired_matrix);
15672 move_it_by_lines (&it, 1);
15673 try_window (window, it.current.pos, 0);
15674 }
15675 else if (PT < IT_CHARPOS (it))
15676 {
15677 clear_glyph_matrix (w->desired_matrix);
15678 move_it_by_lines (&it, -1);
15679 try_window (window, it.current.pos, 0);
15680 }
15681 else
15682 {
15683 /* Not much we can do about it. */
15684 }
15685 }
15686
15687 /* Consider the following case: Window starts at BEGV, there is
15688 invisible, intangible text at BEGV, so that display starts at
15689 some point START > BEGV. It can happen that we are called with
15690 PT somewhere between BEGV and START. Try to handle that case. */
15691 if (w->cursor.vpos < 0)
15692 {
15693 struct glyph_row *row = w->current_matrix->rows;
15694 if (row->mode_line_p)
15695 ++row;
15696 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15697 }
15698
15699 if (!cursor_row_fully_visible_p (w, 0, 0))
15700 {
15701 /* If vscroll is enabled, disable it and try again. */
15702 if (w->vscroll)
15703 {
15704 w->vscroll = 0;
15705 clear_glyph_matrix (w->desired_matrix);
15706 goto recenter;
15707 }
15708
15709 /* If centering point failed to make the whole line visible,
15710 put point at the top instead. That has to make the whole line
15711 visible, if it can be done. */
15712 if (centering_position == 0)
15713 goto done;
15714
15715 clear_glyph_matrix (w->desired_matrix);
15716 centering_position = 0;
15717 goto recenter;
15718 }
15719
15720 done:
15721
15722 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15723 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15724 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15725 ? Qt : Qnil);
15726
15727 /* Display the mode line, if we must. */
15728 if ((update_mode_line
15729 /* If window not full width, must redo its mode line
15730 if (a) the window to its side is being redone and
15731 (b) we do a frame-based redisplay. This is a consequence
15732 of how inverted lines are drawn in frame-based redisplay. */
15733 || (!just_this_one_p
15734 && !FRAME_WINDOW_P (f)
15735 && !WINDOW_FULL_WIDTH_P (w))
15736 /* Line number to display. */
15737 || INTEGERP (w->base_line_pos)
15738 /* Column number is displayed and different from the one displayed. */
15739 || (!NILP (w->column_number_displayed)
15740 && (XFASTINT (w->column_number_displayed) != current_column ())))
15741 /* This means that the window has a mode line. */
15742 && (WINDOW_WANTS_MODELINE_P (w)
15743 || WINDOW_WANTS_HEADER_LINE_P (w)))
15744 {
15745 display_mode_lines (w);
15746
15747 /* If mode line height has changed, arrange for a thorough
15748 immediate redisplay using the correct mode line height. */
15749 if (WINDOW_WANTS_MODELINE_P (w)
15750 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15751 {
15752 fonts_changed_p = 1;
15753 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15754 = DESIRED_MODE_LINE_HEIGHT (w);
15755 }
15756
15757 /* If header line height has changed, arrange for a thorough
15758 immediate redisplay using the correct header line height. */
15759 if (WINDOW_WANTS_HEADER_LINE_P (w)
15760 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15761 {
15762 fonts_changed_p = 1;
15763 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15764 = DESIRED_HEADER_LINE_HEIGHT (w);
15765 }
15766
15767 if (fonts_changed_p)
15768 goto need_larger_matrices;
15769 }
15770
15771 if (!line_number_displayed
15772 && !BUFFERP (w->base_line_pos))
15773 {
15774 w->base_line_pos = Qnil;
15775 w->base_line_number = Qnil;
15776 }
15777
15778 finish_menu_bars:
15779
15780 /* When we reach a frame's selected window, redo the frame's menu bar. */
15781 if (update_mode_line
15782 && EQ (FRAME_SELECTED_WINDOW (f), window))
15783 {
15784 int redisplay_menu_p = 0;
15785
15786 if (FRAME_WINDOW_P (f))
15787 {
15788 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15789 || defined (HAVE_NS) || defined (USE_GTK)
15790 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15791 #else
15792 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15793 #endif
15794 }
15795 else
15796 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15797
15798 if (redisplay_menu_p)
15799 display_menu_bar (w);
15800
15801 #ifdef HAVE_WINDOW_SYSTEM
15802 if (FRAME_WINDOW_P (f))
15803 {
15804 #if defined (USE_GTK) || defined (HAVE_NS)
15805 if (FRAME_EXTERNAL_TOOL_BAR (f))
15806 redisplay_tool_bar (f);
15807 #else
15808 if (WINDOWP (f->tool_bar_window)
15809 && (FRAME_TOOL_BAR_LINES (f) > 0
15810 || !NILP (Vauto_resize_tool_bars))
15811 && redisplay_tool_bar (f))
15812 ignore_mouse_drag_p = 1;
15813 #endif
15814 }
15815 #endif
15816 }
15817
15818 #ifdef HAVE_WINDOW_SYSTEM
15819 if (FRAME_WINDOW_P (f)
15820 && update_window_fringes (w, (just_this_one_p
15821 || (!used_current_matrix_p && !overlay_arrow_seen)
15822 || w->pseudo_window_p)))
15823 {
15824 update_begin (f);
15825 BLOCK_INPUT;
15826 if (draw_window_fringes (w, 1))
15827 x_draw_vertical_border (w);
15828 UNBLOCK_INPUT;
15829 update_end (f);
15830 }
15831 #endif /* HAVE_WINDOW_SYSTEM */
15832
15833 /* We go to this label, with fonts_changed_p nonzero,
15834 if it is necessary to try again using larger glyph matrices.
15835 We have to redeem the scroll bar even in this case,
15836 because the loop in redisplay_internal expects that. */
15837 need_larger_matrices:
15838 ;
15839 finish_scroll_bars:
15840
15841 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15842 {
15843 /* Set the thumb's position and size. */
15844 set_vertical_scroll_bar (w);
15845
15846 /* Note that we actually used the scroll bar attached to this
15847 window, so it shouldn't be deleted at the end of redisplay. */
15848 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15849 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15850 }
15851
15852 /* Restore current_buffer and value of point in it. The window
15853 update may have changed the buffer, so first make sure `opoint'
15854 is still valid (Bug#6177). */
15855 if (CHARPOS (opoint) < BEGV)
15856 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15857 else if (CHARPOS (opoint) > ZV)
15858 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15859 else
15860 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15861
15862 set_buffer_internal_1 (old);
15863 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15864 shorter. This can be caused by log truncation in *Messages*. */
15865 if (CHARPOS (lpoint) <= ZV)
15866 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15867
15868 unbind_to (count, Qnil);
15869 }
15870
15871
15872 /* Build the complete desired matrix of WINDOW with a window start
15873 buffer position POS.
15874
15875 Value is 1 if successful. It is zero if fonts were loaded during
15876 redisplay which makes re-adjusting glyph matrices necessary, and -1
15877 if point would appear in the scroll margins.
15878 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15879 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15880 set in FLAGS.) */
15881
15882 int
15883 try_window (Lisp_Object window, struct text_pos pos, int flags)
15884 {
15885 struct window *w = XWINDOW (window);
15886 struct it it;
15887 struct glyph_row *last_text_row = NULL;
15888 struct frame *f = XFRAME (w->frame);
15889
15890 /* Make POS the new window start. */
15891 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15892
15893 /* Mark cursor position as unknown. No overlay arrow seen. */
15894 w->cursor.vpos = -1;
15895 overlay_arrow_seen = 0;
15896
15897 /* Initialize iterator and info to start at POS. */
15898 start_display (&it, w, pos);
15899
15900 /* Display all lines of W. */
15901 while (it.current_y < it.last_visible_y)
15902 {
15903 if (display_line (&it))
15904 last_text_row = it.glyph_row - 1;
15905 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15906 return 0;
15907 }
15908
15909 /* Don't let the cursor end in the scroll margins. */
15910 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15911 && !MINI_WINDOW_P (w))
15912 {
15913 int this_scroll_margin;
15914
15915 if (scroll_margin > 0)
15916 {
15917 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15918 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15919 }
15920 else
15921 this_scroll_margin = 0;
15922
15923 if ((w->cursor.y >= 0 /* not vscrolled */
15924 && w->cursor.y < this_scroll_margin
15925 && CHARPOS (pos) > BEGV
15926 && IT_CHARPOS (it) < ZV)
15927 /* rms: considering make_cursor_line_fully_visible_p here
15928 seems to give wrong results. We don't want to recenter
15929 when the last line is partly visible, we want to allow
15930 that case to be handled in the usual way. */
15931 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15932 {
15933 w->cursor.vpos = -1;
15934 clear_glyph_matrix (w->desired_matrix);
15935 return -1;
15936 }
15937 }
15938
15939 /* If bottom moved off end of frame, change mode line percentage. */
15940 if (XFASTINT (w->window_end_pos) <= 0
15941 && Z != IT_CHARPOS (it))
15942 w->update_mode_line = Qt;
15943
15944 /* Set window_end_pos to the offset of the last character displayed
15945 on the window from the end of current_buffer. Set
15946 window_end_vpos to its row number. */
15947 if (last_text_row)
15948 {
15949 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15950 w->window_end_bytepos
15951 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15952 w->window_end_pos
15953 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15954 w->window_end_vpos
15955 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15956 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15957 ->displays_text_p);
15958 }
15959 else
15960 {
15961 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15962 w->window_end_pos = make_number (Z - ZV);
15963 w->window_end_vpos = make_number (0);
15964 }
15965
15966 /* But that is not valid info until redisplay finishes. */
15967 w->window_end_valid = Qnil;
15968 return 1;
15969 }
15970
15971
15972 \f
15973 /************************************************************************
15974 Window redisplay reusing current matrix when buffer has not changed
15975 ************************************************************************/
15976
15977 /* Try redisplay of window W showing an unchanged buffer with a
15978 different window start than the last time it was displayed by
15979 reusing its current matrix. Value is non-zero if successful.
15980 W->start is the new window start. */
15981
15982 static int
15983 try_window_reusing_current_matrix (struct window *w)
15984 {
15985 struct frame *f = XFRAME (w->frame);
15986 struct glyph_row *bottom_row;
15987 struct it it;
15988 struct run run;
15989 struct text_pos start, new_start;
15990 int nrows_scrolled, i;
15991 struct glyph_row *last_text_row;
15992 struct glyph_row *last_reused_text_row;
15993 struct glyph_row *start_row;
15994 int start_vpos, min_y, max_y;
15995
15996 #if GLYPH_DEBUG
15997 if (inhibit_try_window_reusing)
15998 return 0;
15999 #endif
16000
16001 if (/* This function doesn't handle terminal frames. */
16002 !FRAME_WINDOW_P (f)
16003 /* Don't try to reuse the display if windows have been split
16004 or such. */
16005 || windows_or_buffers_changed
16006 || cursor_type_changed)
16007 return 0;
16008
16009 /* Can't do this if region may have changed. */
16010 if ((!NILP (Vtransient_mark_mode)
16011 && !NILP (BVAR (current_buffer, mark_active)))
16012 || !NILP (w->region_showing)
16013 || !NILP (Vshow_trailing_whitespace))
16014 return 0;
16015
16016 /* If top-line visibility has changed, give up. */
16017 if (WINDOW_WANTS_HEADER_LINE_P (w)
16018 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16019 return 0;
16020
16021 /* Give up if old or new display is scrolled vertically. We could
16022 make this function handle this, but right now it doesn't. */
16023 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16024 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16025 return 0;
16026
16027 /* The variable new_start now holds the new window start. The old
16028 start `start' can be determined from the current matrix. */
16029 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16030 start = start_row->minpos;
16031 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16032
16033 /* Clear the desired matrix for the display below. */
16034 clear_glyph_matrix (w->desired_matrix);
16035
16036 if (CHARPOS (new_start) <= CHARPOS (start))
16037 {
16038 /* Don't use this method if the display starts with an ellipsis
16039 displayed for invisible text. It's not easy to handle that case
16040 below, and it's certainly not worth the effort since this is
16041 not a frequent case. */
16042 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16043 return 0;
16044
16045 IF_DEBUG (debug_method_add (w, "twu1"));
16046
16047 /* Display up to a row that can be reused. The variable
16048 last_text_row is set to the last row displayed that displays
16049 text. Note that it.vpos == 0 if or if not there is a
16050 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16051 start_display (&it, w, new_start);
16052 w->cursor.vpos = -1;
16053 last_text_row = last_reused_text_row = NULL;
16054
16055 while (it.current_y < it.last_visible_y
16056 && !fonts_changed_p)
16057 {
16058 /* If we have reached into the characters in the START row,
16059 that means the line boundaries have changed. So we
16060 can't start copying with the row START. Maybe it will
16061 work to start copying with the following row. */
16062 while (IT_CHARPOS (it) > CHARPOS (start))
16063 {
16064 /* Advance to the next row as the "start". */
16065 start_row++;
16066 start = start_row->minpos;
16067 /* If there are no more rows to try, or just one, give up. */
16068 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16069 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16070 || CHARPOS (start) == ZV)
16071 {
16072 clear_glyph_matrix (w->desired_matrix);
16073 return 0;
16074 }
16075
16076 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16077 }
16078 /* If we have reached alignment,
16079 we can copy the rest of the rows. */
16080 if (IT_CHARPOS (it) == CHARPOS (start))
16081 break;
16082
16083 if (display_line (&it))
16084 last_text_row = it.glyph_row - 1;
16085 }
16086
16087 /* A value of current_y < last_visible_y means that we stopped
16088 at the previous window start, which in turn means that we
16089 have at least one reusable row. */
16090 if (it.current_y < it.last_visible_y)
16091 {
16092 struct glyph_row *row;
16093
16094 /* IT.vpos always starts from 0; it counts text lines. */
16095 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16096
16097 /* Find PT if not already found in the lines displayed. */
16098 if (w->cursor.vpos < 0)
16099 {
16100 int dy = it.current_y - start_row->y;
16101
16102 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16103 row = row_containing_pos (w, PT, row, NULL, dy);
16104 if (row)
16105 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16106 dy, nrows_scrolled);
16107 else
16108 {
16109 clear_glyph_matrix (w->desired_matrix);
16110 return 0;
16111 }
16112 }
16113
16114 /* Scroll the display. Do it before the current matrix is
16115 changed. The problem here is that update has not yet
16116 run, i.e. part of the current matrix is not up to date.
16117 scroll_run_hook will clear the cursor, and use the
16118 current matrix to get the height of the row the cursor is
16119 in. */
16120 run.current_y = start_row->y;
16121 run.desired_y = it.current_y;
16122 run.height = it.last_visible_y - it.current_y;
16123
16124 if (run.height > 0 && run.current_y != run.desired_y)
16125 {
16126 update_begin (f);
16127 FRAME_RIF (f)->update_window_begin_hook (w);
16128 FRAME_RIF (f)->clear_window_mouse_face (w);
16129 FRAME_RIF (f)->scroll_run_hook (w, &run);
16130 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16131 update_end (f);
16132 }
16133
16134 /* Shift current matrix down by nrows_scrolled lines. */
16135 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16136 rotate_matrix (w->current_matrix,
16137 start_vpos,
16138 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16139 nrows_scrolled);
16140
16141 /* Disable lines that must be updated. */
16142 for (i = 0; i < nrows_scrolled; ++i)
16143 (start_row + i)->enabled_p = 0;
16144
16145 /* Re-compute Y positions. */
16146 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16147 max_y = it.last_visible_y;
16148 for (row = start_row + nrows_scrolled;
16149 row < bottom_row;
16150 ++row)
16151 {
16152 row->y = it.current_y;
16153 row->visible_height = row->height;
16154
16155 if (row->y < min_y)
16156 row->visible_height -= min_y - row->y;
16157 if (row->y + row->height > max_y)
16158 row->visible_height -= row->y + row->height - max_y;
16159 if (row->fringe_bitmap_periodic_p)
16160 row->redraw_fringe_bitmaps_p = 1;
16161
16162 it.current_y += row->height;
16163
16164 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16165 last_reused_text_row = row;
16166 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16167 break;
16168 }
16169
16170 /* Disable lines in the current matrix which are now
16171 below the window. */
16172 for (++row; row < bottom_row; ++row)
16173 row->enabled_p = row->mode_line_p = 0;
16174 }
16175
16176 /* Update window_end_pos etc.; last_reused_text_row is the last
16177 reused row from the current matrix containing text, if any.
16178 The value of last_text_row is the last displayed line
16179 containing text. */
16180 if (last_reused_text_row)
16181 {
16182 w->window_end_bytepos
16183 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16184 w->window_end_pos
16185 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16186 w->window_end_vpos
16187 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16188 w->current_matrix));
16189 }
16190 else if (last_text_row)
16191 {
16192 w->window_end_bytepos
16193 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16194 w->window_end_pos
16195 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16196 w->window_end_vpos
16197 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16198 }
16199 else
16200 {
16201 /* This window must be completely empty. */
16202 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16203 w->window_end_pos = make_number (Z - ZV);
16204 w->window_end_vpos = make_number (0);
16205 }
16206 w->window_end_valid = Qnil;
16207
16208 /* Update hint: don't try scrolling again in update_window. */
16209 w->desired_matrix->no_scrolling_p = 1;
16210
16211 #if GLYPH_DEBUG
16212 debug_method_add (w, "try_window_reusing_current_matrix 1");
16213 #endif
16214 return 1;
16215 }
16216 else if (CHARPOS (new_start) > CHARPOS (start))
16217 {
16218 struct glyph_row *pt_row, *row;
16219 struct glyph_row *first_reusable_row;
16220 struct glyph_row *first_row_to_display;
16221 int dy;
16222 int yb = window_text_bottom_y (w);
16223
16224 /* Find the row starting at new_start, if there is one. Don't
16225 reuse a partially visible line at the end. */
16226 first_reusable_row = start_row;
16227 while (first_reusable_row->enabled_p
16228 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16229 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16230 < CHARPOS (new_start)))
16231 ++first_reusable_row;
16232
16233 /* Give up if there is no row to reuse. */
16234 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16235 || !first_reusable_row->enabled_p
16236 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16237 != CHARPOS (new_start)))
16238 return 0;
16239
16240 /* We can reuse fully visible rows beginning with
16241 first_reusable_row to the end of the window. Set
16242 first_row_to_display to the first row that cannot be reused.
16243 Set pt_row to the row containing point, if there is any. */
16244 pt_row = NULL;
16245 for (first_row_to_display = first_reusable_row;
16246 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16247 ++first_row_to_display)
16248 {
16249 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16250 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
16251 pt_row = first_row_to_display;
16252 }
16253
16254 /* Start displaying at the start of first_row_to_display. */
16255 xassert (first_row_to_display->y < yb);
16256 init_to_row_start (&it, w, first_row_to_display);
16257
16258 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16259 - start_vpos);
16260 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16261 - nrows_scrolled);
16262 it.current_y = (first_row_to_display->y - first_reusable_row->y
16263 + WINDOW_HEADER_LINE_HEIGHT (w));
16264
16265 /* Display lines beginning with first_row_to_display in the
16266 desired matrix. Set last_text_row to the last row displayed
16267 that displays text. */
16268 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16269 if (pt_row == NULL)
16270 w->cursor.vpos = -1;
16271 last_text_row = NULL;
16272 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16273 if (display_line (&it))
16274 last_text_row = it.glyph_row - 1;
16275
16276 /* If point is in a reused row, adjust y and vpos of the cursor
16277 position. */
16278 if (pt_row)
16279 {
16280 w->cursor.vpos -= nrows_scrolled;
16281 w->cursor.y -= first_reusable_row->y - start_row->y;
16282 }
16283
16284 /* Give up if point isn't in a row displayed or reused. (This
16285 also handles the case where w->cursor.vpos < nrows_scrolled
16286 after the calls to display_line, which can happen with scroll
16287 margins. See bug#1295.) */
16288 if (w->cursor.vpos < 0)
16289 {
16290 clear_glyph_matrix (w->desired_matrix);
16291 return 0;
16292 }
16293
16294 /* Scroll the display. */
16295 run.current_y = first_reusable_row->y;
16296 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16297 run.height = it.last_visible_y - run.current_y;
16298 dy = run.current_y - run.desired_y;
16299
16300 if (run.height)
16301 {
16302 update_begin (f);
16303 FRAME_RIF (f)->update_window_begin_hook (w);
16304 FRAME_RIF (f)->clear_window_mouse_face (w);
16305 FRAME_RIF (f)->scroll_run_hook (w, &run);
16306 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16307 update_end (f);
16308 }
16309
16310 /* Adjust Y positions of reused rows. */
16311 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16312 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16313 max_y = it.last_visible_y;
16314 for (row = first_reusable_row; row < first_row_to_display; ++row)
16315 {
16316 row->y -= dy;
16317 row->visible_height = row->height;
16318 if (row->y < min_y)
16319 row->visible_height -= min_y - row->y;
16320 if (row->y + row->height > max_y)
16321 row->visible_height -= row->y + row->height - max_y;
16322 if (row->fringe_bitmap_periodic_p)
16323 row->redraw_fringe_bitmaps_p = 1;
16324 }
16325
16326 /* Scroll the current matrix. */
16327 xassert (nrows_scrolled > 0);
16328 rotate_matrix (w->current_matrix,
16329 start_vpos,
16330 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16331 -nrows_scrolled);
16332
16333 /* Disable rows not reused. */
16334 for (row -= nrows_scrolled; row < bottom_row; ++row)
16335 row->enabled_p = 0;
16336
16337 /* Point may have moved to a different line, so we cannot assume that
16338 the previous cursor position is valid; locate the correct row. */
16339 if (pt_row)
16340 {
16341 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16342 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
16343 row++)
16344 {
16345 w->cursor.vpos++;
16346 w->cursor.y = row->y;
16347 }
16348 if (row < bottom_row)
16349 {
16350 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16351 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16352
16353 /* Can't use this optimization with bidi-reordered glyph
16354 rows, unless cursor is already at point. */
16355 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16356 {
16357 if (!(w->cursor.hpos >= 0
16358 && w->cursor.hpos < row->used[TEXT_AREA]
16359 && BUFFERP (glyph->object)
16360 && glyph->charpos == PT))
16361 return 0;
16362 }
16363 else
16364 for (; glyph < end
16365 && (!BUFFERP (glyph->object)
16366 || glyph->charpos < PT);
16367 glyph++)
16368 {
16369 w->cursor.hpos++;
16370 w->cursor.x += glyph->pixel_width;
16371 }
16372 }
16373 }
16374
16375 /* Adjust window end. A null value of last_text_row means that
16376 the window end is in reused rows which in turn means that
16377 only its vpos can have changed. */
16378 if (last_text_row)
16379 {
16380 w->window_end_bytepos
16381 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16382 w->window_end_pos
16383 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16384 w->window_end_vpos
16385 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16386 }
16387 else
16388 {
16389 w->window_end_vpos
16390 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16391 }
16392
16393 w->window_end_valid = Qnil;
16394 w->desired_matrix->no_scrolling_p = 1;
16395
16396 #if GLYPH_DEBUG
16397 debug_method_add (w, "try_window_reusing_current_matrix 2");
16398 #endif
16399 return 1;
16400 }
16401
16402 return 0;
16403 }
16404
16405
16406 \f
16407 /************************************************************************
16408 Window redisplay reusing current matrix when buffer has changed
16409 ************************************************************************/
16410
16411 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16412 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16413 ptrdiff_t *, ptrdiff_t *);
16414 static struct glyph_row *
16415 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16416 struct glyph_row *);
16417
16418
16419 /* Return the last row in MATRIX displaying text. If row START is
16420 non-null, start searching with that row. IT gives the dimensions
16421 of the display. Value is null if matrix is empty; otherwise it is
16422 a pointer to the row found. */
16423
16424 static struct glyph_row *
16425 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16426 struct glyph_row *start)
16427 {
16428 struct glyph_row *row, *row_found;
16429
16430 /* Set row_found to the last row in IT->w's current matrix
16431 displaying text. The loop looks funny but think of partially
16432 visible lines. */
16433 row_found = NULL;
16434 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16435 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16436 {
16437 xassert (row->enabled_p);
16438 row_found = row;
16439 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16440 break;
16441 ++row;
16442 }
16443
16444 return row_found;
16445 }
16446
16447
16448 /* Return the last row in the current matrix of W that is not affected
16449 by changes at the start of current_buffer that occurred since W's
16450 current matrix was built. Value is null if no such row exists.
16451
16452 BEG_UNCHANGED us the number of characters unchanged at the start of
16453 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16454 first changed character in current_buffer. Characters at positions <
16455 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16456 when the current matrix was built. */
16457
16458 static struct glyph_row *
16459 find_last_unchanged_at_beg_row (struct window *w)
16460 {
16461 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16462 struct glyph_row *row;
16463 struct glyph_row *row_found = NULL;
16464 int yb = window_text_bottom_y (w);
16465
16466 /* Find the last row displaying unchanged text. */
16467 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16468 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16469 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16470 ++row)
16471 {
16472 if (/* If row ends before first_changed_pos, it is unchanged,
16473 except in some case. */
16474 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16475 /* When row ends in ZV and we write at ZV it is not
16476 unchanged. */
16477 && !row->ends_at_zv_p
16478 /* When first_changed_pos is the end of a continued line,
16479 row is not unchanged because it may be no longer
16480 continued. */
16481 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16482 && (row->continued_p
16483 || row->exact_window_width_line_p)))
16484 row_found = row;
16485
16486 /* Stop if last visible row. */
16487 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16488 break;
16489 }
16490
16491 return row_found;
16492 }
16493
16494
16495 /* Find the first glyph row in the current matrix of W that is not
16496 affected by changes at the end of current_buffer since the
16497 time W's current matrix was built.
16498
16499 Return in *DELTA the number of chars by which buffer positions in
16500 unchanged text at the end of current_buffer must be adjusted.
16501
16502 Return in *DELTA_BYTES the corresponding number of bytes.
16503
16504 Value is null if no such row exists, i.e. all rows are affected by
16505 changes. */
16506
16507 static struct glyph_row *
16508 find_first_unchanged_at_end_row (struct window *w,
16509 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16510 {
16511 struct glyph_row *row;
16512 struct glyph_row *row_found = NULL;
16513
16514 *delta = *delta_bytes = 0;
16515
16516 /* Display must not have been paused, otherwise the current matrix
16517 is not up to date. */
16518 eassert (!NILP (w->window_end_valid));
16519
16520 /* A value of window_end_pos >= END_UNCHANGED means that the window
16521 end is in the range of changed text. If so, there is no
16522 unchanged row at the end of W's current matrix. */
16523 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16524 return NULL;
16525
16526 /* Set row to the last row in W's current matrix displaying text. */
16527 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16528
16529 /* If matrix is entirely empty, no unchanged row exists. */
16530 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16531 {
16532 /* The value of row is the last glyph row in the matrix having a
16533 meaningful buffer position in it. The end position of row
16534 corresponds to window_end_pos. This allows us to translate
16535 buffer positions in the current matrix to current buffer
16536 positions for characters not in changed text. */
16537 ptrdiff_t Z_old =
16538 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16539 ptrdiff_t Z_BYTE_old =
16540 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16541 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16542 struct glyph_row *first_text_row
16543 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16544
16545 *delta = Z - Z_old;
16546 *delta_bytes = Z_BYTE - Z_BYTE_old;
16547
16548 /* Set last_unchanged_pos to the buffer position of the last
16549 character in the buffer that has not been changed. Z is the
16550 index + 1 of the last character in current_buffer, i.e. by
16551 subtracting END_UNCHANGED we get the index of the last
16552 unchanged character, and we have to add BEG to get its buffer
16553 position. */
16554 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16555 last_unchanged_pos_old = last_unchanged_pos - *delta;
16556
16557 /* Search backward from ROW for a row displaying a line that
16558 starts at a minimum position >= last_unchanged_pos_old. */
16559 for (; row > first_text_row; --row)
16560 {
16561 /* This used to abort, but it can happen.
16562 It is ok to just stop the search instead here. KFS. */
16563 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16564 break;
16565
16566 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16567 row_found = row;
16568 }
16569 }
16570
16571 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16572
16573 return row_found;
16574 }
16575
16576
16577 /* Make sure that glyph rows in the current matrix of window W
16578 reference the same glyph memory as corresponding rows in the
16579 frame's frame matrix. This function is called after scrolling W's
16580 current matrix on a terminal frame in try_window_id and
16581 try_window_reusing_current_matrix. */
16582
16583 static void
16584 sync_frame_with_window_matrix_rows (struct window *w)
16585 {
16586 struct frame *f = XFRAME (w->frame);
16587 struct glyph_row *window_row, *window_row_end, *frame_row;
16588
16589 /* Preconditions: W must be a leaf window and full-width. Its frame
16590 must have a frame matrix. */
16591 xassert (NILP (w->hchild) && NILP (w->vchild));
16592 xassert (WINDOW_FULL_WIDTH_P (w));
16593 xassert (!FRAME_WINDOW_P (f));
16594
16595 /* If W is a full-width window, glyph pointers in W's current matrix
16596 have, by definition, to be the same as glyph pointers in the
16597 corresponding frame matrix. Note that frame matrices have no
16598 marginal areas (see build_frame_matrix). */
16599 window_row = w->current_matrix->rows;
16600 window_row_end = window_row + w->current_matrix->nrows;
16601 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16602 while (window_row < window_row_end)
16603 {
16604 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16605 struct glyph *end = window_row->glyphs[LAST_AREA];
16606
16607 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16608 frame_row->glyphs[TEXT_AREA] = start;
16609 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16610 frame_row->glyphs[LAST_AREA] = end;
16611
16612 /* Disable frame rows whose corresponding window rows have
16613 been disabled in try_window_id. */
16614 if (!window_row->enabled_p)
16615 frame_row->enabled_p = 0;
16616
16617 ++window_row, ++frame_row;
16618 }
16619 }
16620
16621
16622 /* Find the glyph row in window W containing CHARPOS. Consider all
16623 rows between START and END (not inclusive). END null means search
16624 all rows to the end of the display area of W. Value is the row
16625 containing CHARPOS or null. */
16626
16627 struct glyph_row *
16628 row_containing_pos (struct window *w, ptrdiff_t charpos,
16629 struct glyph_row *start, struct glyph_row *end, int dy)
16630 {
16631 struct glyph_row *row = start;
16632 struct glyph_row *best_row = NULL;
16633 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16634 int last_y;
16635
16636 /* If we happen to start on a header-line, skip that. */
16637 if (row->mode_line_p)
16638 ++row;
16639
16640 if ((end && row >= end) || !row->enabled_p)
16641 return NULL;
16642
16643 last_y = window_text_bottom_y (w) - dy;
16644
16645 while (1)
16646 {
16647 /* Give up if we have gone too far. */
16648 if (end && row >= end)
16649 return NULL;
16650 /* This formerly returned if they were equal.
16651 I think that both quantities are of a "last plus one" type;
16652 if so, when they are equal, the row is within the screen. -- rms. */
16653 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16654 return NULL;
16655
16656 /* If it is in this row, return this row. */
16657 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16658 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16659 /* The end position of a row equals the start
16660 position of the next row. If CHARPOS is there, we
16661 would rather display it in the next line, except
16662 when this line ends in ZV. */
16663 && !row->ends_at_zv_p
16664 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16665 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16666 {
16667 struct glyph *g;
16668
16669 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16670 || (!best_row && !row->continued_p))
16671 return row;
16672 /* In bidi-reordered rows, there could be several rows
16673 occluding point, all of them belonging to the same
16674 continued line. We need to find the row which fits
16675 CHARPOS the best. */
16676 for (g = row->glyphs[TEXT_AREA];
16677 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16678 g++)
16679 {
16680 if (!STRINGP (g->object))
16681 {
16682 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16683 {
16684 mindif = eabs (g->charpos - charpos);
16685 best_row = row;
16686 /* Exact match always wins. */
16687 if (mindif == 0)
16688 return best_row;
16689 }
16690 }
16691 }
16692 }
16693 else if (best_row && !row->continued_p)
16694 return best_row;
16695 ++row;
16696 }
16697 }
16698
16699
16700 /* Try to redisplay window W by reusing its existing display. W's
16701 current matrix must be up to date when this function is called,
16702 i.e. window_end_valid must not be nil.
16703
16704 Value is
16705
16706 1 if display has been updated
16707 0 if otherwise unsuccessful
16708 -1 if redisplay with same window start is known not to succeed
16709
16710 The following steps are performed:
16711
16712 1. Find the last row in the current matrix of W that is not
16713 affected by changes at the start of current_buffer. If no such row
16714 is found, give up.
16715
16716 2. Find the first row in W's current matrix that is not affected by
16717 changes at the end of current_buffer. Maybe there is no such row.
16718
16719 3. Display lines beginning with the row + 1 found in step 1 to the
16720 row found in step 2 or, if step 2 didn't find a row, to the end of
16721 the window.
16722
16723 4. If cursor is not known to appear on the window, give up.
16724
16725 5. If display stopped at the row found in step 2, scroll the
16726 display and current matrix as needed.
16727
16728 6. Maybe display some lines at the end of W, if we must. This can
16729 happen under various circumstances, like a partially visible line
16730 becoming fully visible, or because newly displayed lines are displayed
16731 in smaller font sizes.
16732
16733 7. Update W's window end information. */
16734
16735 static int
16736 try_window_id (struct window *w)
16737 {
16738 struct frame *f = XFRAME (w->frame);
16739 struct glyph_matrix *current_matrix = w->current_matrix;
16740 struct glyph_matrix *desired_matrix = w->desired_matrix;
16741 struct glyph_row *last_unchanged_at_beg_row;
16742 struct glyph_row *first_unchanged_at_end_row;
16743 struct glyph_row *row;
16744 struct glyph_row *bottom_row;
16745 int bottom_vpos;
16746 struct it it;
16747 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16748 int dvpos, dy;
16749 struct text_pos start_pos;
16750 struct run run;
16751 int first_unchanged_at_end_vpos = 0;
16752 struct glyph_row *last_text_row, *last_text_row_at_end;
16753 struct text_pos start;
16754 ptrdiff_t first_changed_charpos, last_changed_charpos;
16755
16756 #if GLYPH_DEBUG
16757 if (inhibit_try_window_id)
16758 return 0;
16759 #endif
16760
16761 /* This is handy for debugging. */
16762 #if 0
16763 #define GIVE_UP(X) \
16764 do { \
16765 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16766 return 0; \
16767 } while (0)
16768 #else
16769 #define GIVE_UP(X) return 0
16770 #endif
16771
16772 SET_TEXT_POS_FROM_MARKER (start, w->start);
16773
16774 /* Don't use this for mini-windows because these can show
16775 messages and mini-buffers, and we don't handle that here. */
16776 if (MINI_WINDOW_P (w))
16777 GIVE_UP (1);
16778
16779 /* This flag is used to prevent redisplay optimizations. */
16780 if (windows_or_buffers_changed || cursor_type_changed)
16781 GIVE_UP (2);
16782
16783 /* Verify that narrowing has not changed.
16784 Also verify that we were not told to prevent redisplay optimizations.
16785 It would be nice to further
16786 reduce the number of cases where this prevents try_window_id. */
16787 if (current_buffer->clip_changed
16788 || current_buffer->prevent_redisplay_optimizations_p)
16789 GIVE_UP (3);
16790
16791 /* Window must either use window-based redisplay or be full width. */
16792 if (!FRAME_WINDOW_P (f)
16793 && (!FRAME_LINE_INS_DEL_OK (f)
16794 || !WINDOW_FULL_WIDTH_P (w)))
16795 GIVE_UP (4);
16796
16797 /* Give up if point is known NOT to appear in W. */
16798 if (PT < CHARPOS (start))
16799 GIVE_UP (5);
16800
16801 /* Another way to prevent redisplay optimizations. */
16802 if (XFASTINT (w->last_modified) == 0)
16803 GIVE_UP (6);
16804
16805 /* Verify that window is not hscrolled. */
16806 if (XFASTINT (w->hscroll) != 0)
16807 GIVE_UP (7);
16808
16809 /* Verify that display wasn't paused. */
16810 if (NILP (w->window_end_valid))
16811 GIVE_UP (8);
16812
16813 /* Can't use this if highlighting a region because a cursor movement
16814 will do more than just set the cursor. */
16815 if (!NILP (Vtransient_mark_mode)
16816 && !NILP (BVAR (current_buffer, mark_active)))
16817 GIVE_UP (9);
16818
16819 /* Likewise if highlighting trailing whitespace. */
16820 if (!NILP (Vshow_trailing_whitespace))
16821 GIVE_UP (11);
16822
16823 /* Likewise if showing a region. */
16824 if (!NILP (w->region_showing))
16825 GIVE_UP (10);
16826
16827 /* Can't use this if overlay arrow position and/or string have
16828 changed. */
16829 if (overlay_arrows_changed_p ())
16830 GIVE_UP (12);
16831
16832 /* When word-wrap is on, adding a space to the first word of a
16833 wrapped line can change the wrap position, altering the line
16834 above it. It might be worthwhile to handle this more
16835 intelligently, but for now just redisplay from scratch. */
16836 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16837 GIVE_UP (21);
16838
16839 /* Under bidi reordering, adding or deleting a character in the
16840 beginning of a paragraph, before the first strong directional
16841 character, can change the base direction of the paragraph (unless
16842 the buffer specifies a fixed paragraph direction), which will
16843 require to redisplay the whole paragraph. It might be worthwhile
16844 to find the paragraph limits and widen the range of redisplayed
16845 lines to that, but for now just give up this optimization and
16846 redisplay from scratch. */
16847 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16848 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16849 GIVE_UP (22);
16850
16851 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16852 only if buffer has really changed. The reason is that the gap is
16853 initially at Z for freshly visited files. The code below would
16854 set end_unchanged to 0 in that case. */
16855 if (MODIFF > SAVE_MODIFF
16856 /* This seems to happen sometimes after saving a buffer. */
16857 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16858 {
16859 if (GPT - BEG < BEG_UNCHANGED)
16860 BEG_UNCHANGED = GPT - BEG;
16861 if (Z - GPT < END_UNCHANGED)
16862 END_UNCHANGED = Z - GPT;
16863 }
16864
16865 /* The position of the first and last character that has been changed. */
16866 first_changed_charpos = BEG + BEG_UNCHANGED;
16867 last_changed_charpos = Z - END_UNCHANGED;
16868
16869 /* If window starts after a line end, and the last change is in
16870 front of that newline, then changes don't affect the display.
16871 This case happens with stealth-fontification. Note that although
16872 the display is unchanged, glyph positions in the matrix have to
16873 be adjusted, of course. */
16874 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16875 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16876 && ((last_changed_charpos < CHARPOS (start)
16877 && CHARPOS (start) == BEGV)
16878 || (last_changed_charpos < CHARPOS (start) - 1
16879 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16880 {
16881 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16882 struct glyph_row *r0;
16883
16884 /* Compute how many chars/bytes have been added to or removed
16885 from the buffer. */
16886 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16887 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16888 Z_delta = Z - Z_old;
16889 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16890
16891 /* Give up if PT is not in the window. Note that it already has
16892 been checked at the start of try_window_id that PT is not in
16893 front of the window start. */
16894 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16895 GIVE_UP (13);
16896
16897 /* If window start is unchanged, we can reuse the whole matrix
16898 as is, after adjusting glyph positions. No need to compute
16899 the window end again, since its offset from Z hasn't changed. */
16900 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16901 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16902 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16903 /* PT must not be in a partially visible line. */
16904 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16905 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16906 {
16907 /* Adjust positions in the glyph matrix. */
16908 if (Z_delta || Z_delta_bytes)
16909 {
16910 struct glyph_row *r1
16911 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16912 increment_matrix_positions (w->current_matrix,
16913 MATRIX_ROW_VPOS (r0, current_matrix),
16914 MATRIX_ROW_VPOS (r1, current_matrix),
16915 Z_delta, Z_delta_bytes);
16916 }
16917
16918 /* Set the cursor. */
16919 row = row_containing_pos (w, PT, r0, NULL, 0);
16920 if (row)
16921 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16922 else
16923 abort ();
16924 return 1;
16925 }
16926 }
16927
16928 /* Handle the case that changes are all below what is displayed in
16929 the window, and that PT is in the window. This shortcut cannot
16930 be taken if ZV is visible in the window, and text has been added
16931 there that is visible in the window. */
16932 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16933 /* ZV is not visible in the window, or there are no
16934 changes at ZV, actually. */
16935 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16936 || first_changed_charpos == last_changed_charpos))
16937 {
16938 struct glyph_row *r0;
16939
16940 /* Give up if PT is not in the window. Note that it already has
16941 been checked at the start of try_window_id that PT is not in
16942 front of the window start. */
16943 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16944 GIVE_UP (14);
16945
16946 /* If window start is unchanged, we can reuse the whole matrix
16947 as is, without changing glyph positions since no text has
16948 been added/removed in front of the window end. */
16949 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16950 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16951 /* PT must not be in a partially visible line. */
16952 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16953 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16954 {
16955 /* We have to compute the window end anew since text
16956 could have been added/removed after it. */
16957 w->window_end_pos
16958 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16959 w->window_end_bytepos
16960 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16961
16962 /* Set the cursor. */
16963 row = row_containing_pos (w, PT, r0, NULL, 0);
16964 if (row)
16965 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16966 else
16967 abort ();
16968 return 2;
16969 }
16970 }
16971
16972 /* Give up if window start is in the changed area.
16973
16974 The condition used to read
16975
16976 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16977
16978 but why that was tested escapes me at the moment. */
16979 if (CHARPOS (start) >= first_changed_charpos
16980 && CHARPOS (start) <= last_changed_charpos)
16981 GIVE_UP (15);
16982
16983 /* Check that window start agrees with the start of the first glyph
16984 row in its current matrix. Check this after we know the window
16985 start is not in changed text, otherwise positions would not be
16986 comparable. */
16987 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16988 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16989 GIVE_UP (16);
16990
16991 /* Give up if the window ends in strings. Overlay strings
16992 at the end are difficult to handle, so don't try. */
16993 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16994 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16995 GIVE_UP (20);
16996
16997 /* Compute the position at which we have to start displaying new
16998 lines. Some of the lines at the top of the window might be
16999 reusable because they are not displaying changed text. Find the
17000 last row in W's current matrix not affected by changes at the
17001 start of current_buffer. Value is null if changes start in the
17002 first line of window. */
17003 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17004 if (last_unchanged_at_beg_row)
17005 {
17006 /* Avoid starting to display in the moddle of a character, a TAB
17007 for instance. This is easier than to set up the iterator
17008 exactly, and it's not a frequent case, so the additional
17009 effort wouldn't really pay off. */
17010 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17011 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17012 && last_unchanged_at_beg_row > w->current_matrix->rows)
17013 --last_unchanged_at_beg_row;
17014
17015 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17016 GIVE_UP (17);
17017
17018 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17019 GIVE_UP (18);
17020 start_pos = it.current.pos;
17021
17022 /* Start displaying new lines in the desired matrix at the same
17023 vpos we would use in the current matrix, i.e. below
17024 last_unchanged_at_beg_row. */
17025 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17026 current_matrix);
17027 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17028 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17029
17030 xassert (it.hpos == 0 && it.current_x == 0);
17031 }
17032 else
17033 {
17034 /* There are no reusable lines at the start of the window.
17035 Start displaying in the first text line. */
17036 start_display (&it, w, start);
17037 it.vpos = it.first_vpos;
17038 start_pos = it.current.pos;
17039 }
17040
17041 /* Find the first row that is not affected by changes at the end of
17042 the buffer. Value will be null if there is no unchanged row, in
17043 which case we must redisplay to the end of the window. delta
17044 will be set to the value by which buffer positions beginning with
17045 first_unchanged_at_end_row have to be adjusted due to text
17046 changes. */
17047 first_unchanged_at_end_row
17048 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17049 IF_DEBUG (debug_delta = delta);
17050 IF_DEBUG (debug_delta_bytes = delta_bytes);
17051
17052 /* Set stop_pos to the buffer position up to which we will have to
17053 display new lines. If first_unchanged_at_end_row != NULL, this
17054 is the buffer position of the start of the line displayed in that
17055 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17056 that we don't stop at a buffer position. */
17057 stop_pos = 0;
17058 if (first_unchanged_at_end_row)
17059 {
17060 xassert (last_unchanged_at_beg_row == NULL
17061 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17062
17063 /* If this is a continuation line, move forward to the next one
17064 that isn't. Changes in lines above affect this line.
17065 Caution: this may move first_unchanged_at_end_row to a row
17066 not displaying text. */
17067 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17068 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17069 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17070 < it.last_visible_y))
17071 ++first_unchanged_at_end_row;
17072
17073 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17074 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17075 >= it.last_visible_y))
17076 first_unchanged_at_end_row = NULL;
17077 else
17078 {
17079 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17080 + delta);
17081 first_unchanged_at_end_vpos
17082 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17083 xassert (stop_pos >= Z - END_UNCHANGED);
17084 }
17085 }
17086 else if (last_unchanged_at_beg_row == NULL)
17087 GIVE_UP (19);
17088
17089
17090 #if GLYPH_DEBUG
17091
17092 /* Either there is no unchanged row at the end, or the one we have
17093 now displays text. This is a necessary condition for the window
17094 end pos calculation at the end of this function. */
17095 xassert (first_unchanged_at_end_row == NULL
17096 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17097
17098 debug_last_unchanged_at_beg_vpos
17099 = (last_unchanged_at_beg_row
17100 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17101 : -1);
17102 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17103
17104 #endif /* GLYPH_DEBUG != 0 */
17105
17106
17107 /* Display new lines. Set last_text_row to the last new line
17108 displayed which has text on it, i.e. might end up as being the
17109 line where the window_end_vpos is. */
17110 w->cursor.vpos = -1;
17111 last_text_row = NULL;
17112 overlay_arrow_seen = 0;
17113 while (it.current_y < it.last_visible_y
17114 && !fonts_changed_p
17115 && (first_unchanged_at_end_row == NULL
17116 || IT_CHARPOS (it) < stop_pos))
17117 {
17118 if (display_line (&it))
17119 last_text_row = it.glyph_row - 1;
17120 }
17121
17122 if (fonts_changed_p)
17123 return -1;
17124
17125
17126 /* Compute differences in buffer positions, y-positions etc. for
17127 lines reused at the bottom of the window. Compute what we can
17128 scroll. */
17129 if (first_unchanged_at_end_row
17130 /* No lines reused because we displayed everything up to the
17131 bottom of the window. */
17132 && it.current_y < it.last_visible_y)
17133 {
17134 dvpos = (it.vpos
17135 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17136 current_matrix));
17137 dy = it.current_y - first_unchanged_at_end_row->y;
17138 run.current_y = first_unchanged_at_end_row->y;
17139 run.desired_y = run.current_y + dy;
17140 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17141 }
17142 else
17143 {
17144 delta = delta_bytes = dvpos = dy
17145 = run.current_y = run.desired_y = run.height = 0;
17146 first_unchanged_at_end_row = NULL;
17147 }
17148 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17149
17150
17151 /* Find the cursor if not already found. We have to decide whether
17152 PT will appear on this window (it sometimes doesn't, but this is
17153 not a very frequent case.) This decision has to be made before
17154 the current matrix is altered. A value of cursor.vpos < 0 means
17155 that PT is either in one of the lines beginning at
17156 first_unchanged_at_end_row or below the window. Don't care for
17157 lines that might be displayed later at the window end; as
17158 mentioned, this is not a frequent case. */
17159 if (w->cursor.vpos < 0)
17160 {
17161 /* Cursor in unchanged rows at the top? */
17162 if (PT < CHARPOS (start_pos)
17163 && last_unchanged_at_beg_row)
17164 {
17165 row = row_containing_pos (w, PT,
17166 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17167 last_unchanged_at_beg_row + 1, 0);
17168 if (row)
17169 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17170 }
17171
17172 /* Start from first_unchanged_at_end_row looking for PT. */
17173 else if (first_unchanged_at_end_row)
17174 {
17175 row = row_containing_pos (w, PT - delta,
17176 first_unchanged_at_end_row, NULL, 0);
17177 if (row)
17178 set_cursor_from_row (w, row, w->current_matrix, delta,
17179 delta_bytes, dy, dvpos);
17180 }
17181
17182 /* Give up if cursor was not found. */
17183 if (w->cursor.vpos < 0)
17184 {
17185 clear_glyph_matrix (w->desired_matrix);
17186 return -1;
17187 }
17188 }
17189
17190 /* Don't let the cursor end in the scroll margins. */
17191 {
17192 int this_scroll_margin, cursor_height;
17193
17194 this_scroll_margin =
17195 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17196 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17197 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17198
17199 if ((w->cursor.y < this_scroll_margin
17200 && CHARPOS (start) > BEGV)
17201 /* Old redisplay didn't take scroll margin into account at the bottom,
17202 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17203 || (w->cursor.y + (make_cursor_line_fully_visible_p
17204 ? cursor_height + this_scroll_margin
17205 : 1)) > it.last_visible_y)
17206 {
17207 w->cursor.vpos = -1;
17208 clear_glyph_matrix (w->desired_matrix);
17209 return -1;
17210 }
17211 }
17212
17213 /* Scroll the display. Do it before changing the current matrix so
17214 that xterm.c doesn't get confused about where the cursor glyph is
17215 found. */
17216 if (dy && run.height)
17217 {
17218 update_begin (f);
17219
17220 if (FRAME_WINDOW_P (f))
17221 {
17222 FRAME_RIF (f)->update_window_begin_hook (w);
17223 FRAME_RIF (f)->clear_window_mouse_face (w);
17224 FRAME_RIF (f)->scroll_run_hook (w, &run);
17225 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17226 }
17227 else
17228 {
17229 /* Terminal frame. In this case, dvpos gives the number of
17230 lines to scroll by; dvpos < 0 means scroll up. */
17231 int from_vpos
17232 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17233 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17234 int end = (WINDOW_TOP_EDGE_LINE (w)
17235 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17236 + window_internal_height (w));
17237
17238 #if defined (HAVE_GPM) || defined (MSDOS)
17239 x_clear_window_mouse_face (w);
17240 #endif
17241 /* Perform the operation on the screen. */
17242 if (dvpos > 0)
17243 {
17244 /* Scroll last_unchanged_at_beg_row to the end of the
17245 window down dvpos lines. */
17246 set_terminal_window (f, end);
17247
17248 /* On dumb terminals delete dvpos lines at the end
17249 before inserting dvpos empty lines. */
17250 if (!FRAME_SCROLL_REGION_OK (f))
17251 ins_del_lines (f, end - dvpos, -dvpos);
17252
17253 /* Insert dvpos empty lines in front of
17254 last_unchanged_at_beg_row. */
17255 ins_del_lines (f, from, dvpos);
17256 }
17257 else if (dvpos < 0)
17258 {
17259 /* Scroll up last_unchanged_at_beg_vpos to the end of
17260 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17261 set_terminal_window (f, end);
17262
17263 /* Delete dvpos lines in front of
17264 last_unchanged_at_beg_vpos. ins_del_lines will set
17265 the cursor to the given vpos and emit |dvpos| delete
17266 line sequences. */
17267 ins_del_lines (f, from + dvpos, dvpos);
17268
17269 /* On a dumb terminal insert dvpos empty lines at the
17270 end. */
17271 if (!FRAME_SCROLL_REGION_OK (f))
17272 ins_del_lines (f, end + dvpos, -dvpos);
17273 }
17274
17275 set_terminal_window (f, 0);
17276 }
17277
17278 update_end (f);
17279 }
17280
17281 /* Shift reused rows of the current matrix to the right position.
17282 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17283 text. */
17284 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17285 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17286 if (dvpos < 0)
17287 {
17288 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17289 bottom_vpos, dvpos);
17290 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17291 bottom_vpos, 0);
17292 }
17293 else if (dvpos > 0)
17294 {
17295 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17296 bottom_vpos, dvpos);
17297 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17298 first_unchanged_at_end_vpos + dvpos, 0);
17299 }
17300
17301 /* For frame-based redisplay, make sure that current frame and window
17302 matrix are in sync with respect to glyph memory. */
17303 if (!FRAME_WINDOW_P (f))
17304 sync_frame_with_window_matrix_rows (w);
17305
17306 /* Adjust buffer positions in reused rows. */
17307 if (delta || delta_bytes)
17308 increment_matrix_positions (current_matrix,
17309 first_unchanged_at_end_vpos + dvpos,
17310 bottom_vpos, delta, delta_bytes);
17311
17312 /* Adjust Y positions. */
17313 if (dy)
17314 shift_glyph_matrix (w, current_matrix,
17315 first_unchanged_at_end_vpos + dvpos,
17316 bottom_vpos, dy);
17317
17318 if (first_unchanged_at_end_row)
17319 {
17320 first_unchanged_at_end_row += dvpos;
17321 if (first_unchanged_at_end_row->y >= it.last_visible_y
17322 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17323 first_unchanged_at_end_row = NULL;
17324 }
17325
17326 /* If scrolling up, there may be some lines to display at the end of
17327 the window. */
17328 last_text_row_at_end = NULL;
17329 if (dy < 0)
17330 {
17331 /* Scrolling up can leave for example a partially visible line
17332 at the end of the window to be redisplayed. */
17333 /* Set last_row to the glyph row in the current matrix where the
17334 window end line is found. It has been moved up or down in
17335 the matrix by dvpos. */
17336 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17337 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17338
17339 /* If last_row is the window end line, it should display text. */
17340 xassert (last_row->displays_text_p);
17341
17342 /* If window end line was partially visible before, begin
17343 displaying at that line. Otherwise begin displaying with the
17344 line following it. */
17345 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17346 {
17347 init_to_row_start (&it, w, last_row);
17348 it.vpos = last_vpos;
17349 it.current_y = last_row->y;
17350 }
17351 else
17352 {
17353 init_to_row_end (&it, w, last_row);
17354 it.vpos = 1 + last_vpos;
17355 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17356 ++last_row;
17357 }
17358
17359 /* We may start in a continuation line. If so, we have to
17360 get the right continuation_lines_width and current_x. */
17361 it.continuation_lines_width = last_row->continuation_lines_width;
17362 it.hpos = it.current_x = 0;
17363
17364 /* Display the rest of the lines at the window end. */
17365 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17366 while (it.current_y < it.last_visible_y
17367 && !fonts_changed_p)
17368 {
17369 /* Is it always sure that the display agrees with lines in
17370 the current matrix? I don't think so, so we mark rows
17371 displayed invalid in the current matrix by setting their
17372 enabled_p flag to zero. */
17373 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17374 if (display_line (&it))
17375 last_text_row_at_end = it.glyph_row - 1;
17376 }
17377 }
17378
17379 /* Update window_end_pos and window_end_vpos. */
17380 if (first_unchanged_at_end_row
17381 && !last_text_row_at_end)
17382 {
17383 /* Window end line if one of the preserved rows from the current
17384 matrix. Set row to the last row displaying text in current
17385 matrix starting at first_unchanged_at_end_row, after
17386 scrolling. */
17387 xassert (first_unchanged_at_end_row->displays_text_p);
17388 row = find_last_row_displaying_text (w->current_matrix, &it,
17389 first_unchanged_at_end_row);
17390 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17391
17392 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17393 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17394 w->window_end_vpos
17395 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17396 xassert (w->window_end_bytepos >= 0);
17397 IF_DEBUG (debug_method_add (w, "A"));
17398 }
17399 else if (last_text_row_at_end)
17400 {
17401 w->window_end_pos
17402 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17403 w->window_end_bytepos
17404 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17405 w->window_end_vpos
17406 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17407 xassert (w->window_end_bytepos >= 0);
17408 IF_DEBUG (debug_method_add (w, "B"));
17409 }
17410 else if (last_text_row)
17411 {
17412 /* We have displayed either to the end of the window or at the
17413 end of the window, i.e. the last row with text is to be found
17414 in the desired matrix. */
17415 w->window_end_pos
17416 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17417 w->window_end_bytepos
17418 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17419 w->window_end_vpos
17420 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17421 xassert (w->window_end_bytepos >= 0);
17422 }
17423 else if (first_unchanged_at_end_row == NULL
17424 && last_text_row == NULL
17425 && last_text_row_at_end == NULL)
17426 {
17427 /* Displayed to end of window, but no line containing text was
17428 displayed. Lines were deleted at the end of the window. */
17429 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17430 int vpos = XFASTINT (w->window_end_vpos);
17431 struct glyph_row *current_row = current_matrix->rows + vpos;
17432 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17433
17434 for (row = NULL;
17435 row == NULL && vpos >= first_vpos;
17436 --vpos, --current_row, --desired_row)
17437 {
17438 if (desired_row->enabled_p)
17439 {
17440 if (desired_row->displays_text_p)
17441 row = desired_row;
17442 }
17443 else if (current_row->displays_text_p)
17444 row = current_row;
17445 }
17446
17447 xassert (row != NULL);
17448 w->window_end_vpos = make_number (vpos + 1);
17449 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17450 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17451 xassert (w->window_end_bytepos >= 0);
17452 IF_DEBUG (debug_method_add (w, "C"));
17453 }
17454 else
17455 abort ();
17456
17457 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17458 debug_end_vpos = XFASTINT (w->window_end_vpos));
17459
17460 /* Record that display has not been completed. */
17461 w->window_end_valid = Qnil;
17462 w->desired_matrix->no_scrolling_p = 1;
17463 return 3;
17464
17465 #undef GIVE_UP
17466 }
17467
17468
17469 \f
17470 /***********************************************************************
17471 More debugging support
17472 ***********************************************************************/
17473
17474 #if GLYPH_DEBUG
17475
17476 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17477 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17478 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17479
17480
17481 /* Dump the contents of glyph matrix MATRIX on stderr.
17482
17483 GLYPHS 0 means don't show glyph contents.
17484 GLYPHS 1 means show glyphs in short form
17485 GLYPHS > 1 means show glyphs in long form. */
17486
17487 void
17488 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17489 {
17490 int i;
17491 for (i = 0; i < matrix->nrows; ++i)
17492 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17493 }
17494
17495
17496 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17497 the glyph row and area where the glyph comes from. */
17498
17499 void
17500 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17501 {
17502 if (glyph->type == CHAR_GLYPH)
17503 {
17504 fprintf (stderr,
17505 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17506 glyph - row->glyphs[TEXT_AREA],
17507 'C',
17508 glyph->charpos,
17509 (BUFFERP (glyph->object)
17510 ? 'B'
17511 : (STRINGP (glyph->object)
17512 ? 'S'
17513 : '-')),
17514 glyph->pixel_width,
17515 glyph->u.ch,
17516 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17517 ? glyph->u.ch
17518 : '.'),
17519 glyph->face_id,
17520 glyph->left_box_line_p,
17521 glyph->right_box_line_p);
17522 }
17523 else if (glyph->type == STRETCH_GLYPH)
17524 {
17525 fprintf (stderr,
17526 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17527 glyph - row->glyphs[TEXT_AREA],
17528 'S',
17529 glyph->charpos,
17530 (BUFFERP (glyph->object)
17531 ? 'B'
17532 : (STRINGP (glyph->object)
17533 ? 'S'
17534 : '-')),
17535 glyph->pixel_width,
17536 0,
17537 '.',
17538 glyph->face_id,
17539 glyph->left_box_line_p,
17540 glyph->right_box_line_p);
17541 }
17542 else if (glyph->type == IMAGE_GLYPH)
17543 {
17544 fprintf (stderr,
17545 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17546 glyph - row->glyphs[TEXT_AREA],
17547 'I',
17548 glyph->charpos,
17549 (BUFFERP (glyph->object)
17550 ? 'B'
17551 : (STRINGP (glyph->object)
17552 ? 'S'
17553 : '-')),
17554 glyph->pixel_width,
17555 glyph->u.img_id,
17556 '.',
17557 glyph->face_id,
17558 glyph->left_box_line_p,
17559 glyph->right_box_line_p);
17560 }
17561 else if (glyph->type == COMPOSITE_GLYPH)
17562 {
17563 fprintf (stderr,
17564 " %5td %4c %6"pI"d %c %3d 0x%05x",
17565 glyph - row->glyphs[TEXT_AREA],
17566 '+',
17567 glyph->charpos,
17568 (BUFFERP (glyph->object)
17569 ? 'B'
17570 : (STRINGP (glyph->object)
17571 ? 'S'
17572 : '-')),
17573 glyph->pixel_width,
17574 glyph->u.cmp.id);
17575 if (glyph->u.cmp.automatic)
17576 fprintf (stderr,
17577 "[%d-%d]",
17578 glyph->slice.cmp.from, glyph->slice.cmp.to);
17579 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17580 glyph->face_id,
17581 glyph->left_box_line_p,
17582 glyph->right_box_line_p);
17583 }
17584 }
17585
17586
17587 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17588 GLYPHS 0 means don't show glyph contents.
17589 GLYPHS 1 means show glyphs in short form
17590 GLYPHS > 1 means show glyphs in long form. */
17591
17592 void
17593 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17594 {
17595 if (glyphs != 1)
17596 {
17597 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17598 fprintf (stderr, "======================================================================\n");
17599
17600 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17601 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17602 vpos,
17603 MATRIX_ROW_START_CHARPOS (row),
17604 MATRIX_ROW_END_CHARPOS (row),
17605 row->used[TEXT_AREA],
17606 row->contains_overlapping_glyphs_p,
17607 row->enabled_p,
17608 row->truncated_on_left_p,
17609 row->truncated_on_right_p,
17610 row->continued_p,
17611 MATRIX_ROW_CONTINUATION_LINE_P (row),
17612 row->displays_text_p,
17613 row->ends_at_zv_p,
17614 row->fill_line_p,
17615 row->ends_in_middle_of_char_p,
17616 row->starts_in_middle_of_char_p,
17617 row->mouse_face_p,
17618 row->x,
17619 row->y,
17620 row->pixel_width,
17621 row->height,
17622 row->visible_height,
17623 row->ascent,
17624 row->phys_ascent);
17625 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17626 row->end.overlay_string_index,
17627 row->continuation_lines_width);
17628 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17629 CHARPOS (row->start.string_pos),
17630 CHARPOS (row->end.string_pos));
17631 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17632 row->end.dpvec_index);
17633 }
17634
17635 if (glyphs > 1)
17636 {
17637 int area;
17638
17639 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17640 {
17641 struct glyph *glyph = row->glyphs[area];
17642 struct glyph *glyph_end = glyph + row->used[area];
17643
17644 /* Glyph for a line end in text. */
17645 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17646 ++glyph_end;
17647
17648 if (glyph < glyph_end)
17649 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17650
17651 for (; glyph < glyph_end; ++glyph)
17652 dump_glyph (row, glyph, area);
17653 }
17654 }
17655 else if (glyphs == 1)
17656 {
17657 int area;
17658
17659 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17660 {
17661 char *s = (char *) alloca (row->used[area] + 1);
17662 int i;
17663
17664 for (i = 0; i < row->used[area]; ++i)
17665 {
17666 struct glyph *glyph = row->glyphs[area] + i;
17667 if (glyph->type == CHAR_GLYPH
17668 && glyph->u.ch < 0x80
17669 && glyph->u.ch >= ' ')
17670 s[i] = glyph->u.ch;
17671 else
17672 s[i] = '.';
17673 }
17674
17675 s[i] = '\0';
17676 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17677 }
17678 }
17679 }
17680
17681
17682 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17683 Sdump_glyph_matrix, 0, 1, "p",
17684 doc: /* Dump the current matrix of the selected window to stderr.
17685 Shows contents of glyph row structures. With non-nil
17686 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17687 glyphs in short form, otherwise show glyphs in long form. */)
17688 (Lisp_Object glyphs)
17689 {
17690 struct window *w = XWINDOW (selected_window);
17691 struct buffer *buffer = XBUFFER (w->buffer);
17692
17693 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17694 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17695 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17696 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17697 fprintf (stderr, "=============================================\n");
17698 dump_glyph_matrix (w->current_matrix,
17699 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17700 return Qnil;
17701 }
17702
17703
17704 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17705 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17706 (void)
17707 {
17708 struct frame *f = XFRAME (selected_frame);
17709 dump_glyph_matrix (f->current_matrix, 1);
17710 return Qnil;
17711 }
17712
17713
17714 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17715 doc: /* Dump glyph row ROW to stderr.
17716 GLYPH 0 means don't dump glyphs.
17717 GLYPH 1 means dump glyphs in short form.
17718 GLYPH > 1 or omitted means dump glyphs in long form. */)
17719 (Lisp_Object row, Lisp_Object glyphs)
17720 {
17721 struct glyph_matrix *matrix;
17722 EMACS_INT vpos;
17723
17724 CHECK_NUMBER (row);
17725 matrix = XWINDOW (selected_window)->current_matrix;
17726 vpos = XINT (row);
17727 if (vpos >= 0 && vpos < matrix->nrows)
17728 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17729 vpos,
17730 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17731 return Qnil;
17732 }
17733
17734
17735 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17736 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17737 GLYPH 0 means don't dump glyphs.
17738 GLYPH 1 means dump glyphs in short form.
17739 GLYPH > 1 or omitted means dump glyphs in long form. */)
17740 (Lisp_Object row, Lisp_Object glyphs)
17741 {
17742 struct frame *sf = SELECTED_FRAME ();
17743 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17744 EMACS_INT vpos;
17745
17746 CHECK_NUMBER (row);
17747 vpos = XINT (row);
17748 if (vpos >= 0 && vpos < m->nrows)
17749 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17750 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17751 return Qnil;
17752 }
17753
17754
17755 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17756 doc: /* Toggle tracing of redisplay.
17757 With ARG, turn tracing on if and only if ARG is positive. */)
17758 (Lisp_Object arg)
17759 {
17760 if (NILP (arg))
17761 trace_redisplay_p = !trace_redisplay_p;
17762 else
17763 {
17764 arg = Fprefix_numeric_value (arg);
17765 trace_redisplay_p = XINT (arg) > 0;
17766 }
17767
17768 return Qnil;
17769 }
17770
17771
17772 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17773 doc: /* Like `format', but print result to stderr.
17774 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17775 (ptrdiff_t nargs, Lisp_Object *args)
17776 {
17777 Lisp_Object s = Fformat (nargs, args);
17778 fprintf (stderr, "%s", SDATA (s));
17779 return Qnil;
17780 }
17781
17782 #endif /* GLYPH_DEBUG */
17783
17784
17785 \f
17786 /***********************************************************************
17787 Building Desired Matrix Rows
17788 ***********************************************************************/
17789
17790 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17791 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17792
17793 static struct glyph_row *
17794 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17795 {
17796 struct frame *f = XFRAME (WINDOW_FRAME (w));
17797 struct buffer *buffer = XBUFFER (w->buffer);
17798 struct buffer *old = current_buffer;
17799 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17800 int arrow_len = SCHARS (overlay_arrow_string);
17801 const unsigned char *arrow_end = arrow_string + arrow_len;
17802 const unsigned char *p;
17803 struct it it;
17804 int multibyte_p;
17805 int n_glyphs_before;
17806
17807 set_buffer_temp (buffer);
17808 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17809 it.glyph_row->used[TEXT_AREA] = 0;
17810 SET_TEXT_POS (it.position, 0, 0);
17811
17812 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17813 p = arrow_string;
17814 while (p < arrow_end)
17815 {
17816 Lisp_Object face, ilisp;
17817
17818 /* Get the next character. */
17819 if (multibyte_p)
17820 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17821 else
17822 {
17823 it.c = it.char_to_display = *p, it.len = 1;
17824 if (! ASCII_CHAR_P (it.c))
17825 it.char_to_display = BYTE8_TO_CHAR (it.c);
17826 }
17827 p += it.len;
17828
17829 /* Get its face. */
17830 ilisp = make_number (p - arrow_string);
17831 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17832 it.face_id = compute_char_face (f, it.char_to_display, face);
17833
17834 /* Compute its width, get its glyphs. */
17835 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17836 SET_TEXT_POS (it.position, -1, -1);
17837 PRODUCE_GLYPHS (&it);
17838
17839 /* If this character doesn't fit any more in the line, we have
17840 to remove some glyphs. */
17841 if (it.current_x > it.last_visible_x)
17842 {
17843 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17844 break;
17845 }
17846 }
17847
17848 set_buffer_temp (old);
17849 return it.glyph_row;
17850 }
17851
17852
17853 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17854 glyphs are only inserted for terminal frames since we can't really
17855 win with truncation glyphs when partially visible glyphs are
17856 involved. Which glyphs to insert is determined by
17857 produce_special_glyphs. */
17858
17859 static void
17860 insert_left_trunc_glyphs (struct it *it)
17861 {
17862 struct it truncate_it;
17863 struct glyph *from, *end, *to, *toend;
17864
17865 xassert (!FRAME_WINDOW_P (it->f));
17866
17867 /* Get the truncation glyphs. */
17868 truncate_it = *it;
17869 truncate_it.current_x = 0;
17870 truncate_it.face_id = DEFAULT_FACE_ID;
17871 truncate_it.glyph_row = &scratch_glyph_row;
17872 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17873 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17874 truncate_it.object = make_number (0);
17875 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17876
17877 /* Overwrite glyphs from IT with truncation glyphs. */
17878 if (!it->glyph_row->reversed_p)
17879 {
17880 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17881 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17882 to = it->glyph_row->glyphs[TEXT_AREA];
17883 toend = to + it->glyph_row->used[TEXT_AREA];
17884
17885 while (from < end)
17886 *to++ = *from++;
17887
17888 /* There may be padding glyphs left over. Overwrite them too. */
17889 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17890 {
17891 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17892 while (from < end)
17893 *to++ = *from++;
17894 }
17895
17896 if (to > toend)
17897 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17898 }
17899 else
17900 {
17901 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17902 that back to front. */
17903 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17904 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17905 toend = it->glyph_row->glyphs[TEXT_AREA];
17906 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17907
17908 while (from >= end && to >= toend)
17909 *to-- = *from--;
17910 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17911 {
17912 from =
17913 truncate_it.glyph_row->glyphs[TEXT_AREA]
17914 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17915 while (from >= end && to >= toend)
17916 *to-- = *from--;
17917 }
17918 if (from >= end)
17919 {
17920 /* Need to free some room before prepending additional
17921 glyphs. */
17922 int move_by = from - end + 1;
17923 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17924 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17925
17926 for ( ; g >= g0; g--)
17927 g[move_by] = *g;
17928 while (from >= end)
17929 *to-- = *from--;
17930 it->glyph_row->used[TEXT_AREA] += move_by;
17931 }
17932 }
17933 }
17934
17935
17936 /* Compute the pixel height and width of IT->glyph_row.
17937
17938 Most of the time, ascent and height of a display line will be equal
17939 to the max_ascent and max_height values of the display iterator
17940 structure. This is not the case if
17941
17942 1. We hit ZV without displaying anything. In this case, max_ascent
17943 and max_height will be zero.
17944
17945 2. We have some glyphs that don't contribute to the line height.
17946 (The glyph row flag contributes_to_line_height_p is for future
17947 pixmap extensions).
17948
17949 The first case is easily covered by using default values because in
17950 these cases, the line height does not really matter, except that it
17951 must not be zero. */
17952
17953 static void
17954 compute_line_metrics (struct it *it)
17955 {
17956 struct glyph_row *row = it->glyph_row;
17957
17958 if (FRAME_WINDOW_P (it->f))
17959 {
17960 int i, min_y, max_y;
17961
17962 /* The line may consist of one space only, that was added to
17963 place the cursor on it. If so, the row's height hasn't been
17964 computed yet. */
17965 if (row->height == 0)
17966 {
17967 if (it->max_ascent + it->max_descent == 0)
17968 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17969 row->ascent = it->max_ascent;
17970 row->height = it->max_ascent + it->max_descent;
17971 row->phys_ascent = it->max_phys_ascent;
17972 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17973 row->extra_line_spacing = it->max_extra_line_spacing;
17974 }
17975
17976 /* Compute the width of this line. */
17977 row->pixel_width = row->x;
17978 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17979 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17980
17981 xassert (row->pixel_width >= 0);
17982 xassert (row->ascent >= 0 && row->height > 0);
17983
17984 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17985 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17986
17987 /* If first line's physical ascent is larger than its logical
17988 ascent, use the physical ascent, and make the row taller.
17989 This makes accented characters fully visible. */
17990 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17991 && row->phys_ascent > row->ascent)
17992 {
17993 row->height += row->phys_ascent - row->ascent;
17994 row->ascent = row->phys_ascent;
17995 }
17996
17997 /* Compute how much of the line is visible. */
17998 row->visible_height = row->height;
17999
18000 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18001 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18002
18003 if (row->y < min_y)
18004 row->visible_height -= min_y - row->y;
18005 if (row->y + row->height > max_y)
18006 row->visible_height -= row->y + row->height - max_y;
18007 }
18008 else
18009 {
18010 row->pixel_width = row->used[TEXT_AREA];
18011 if (row->continued_p)
18012 row->pixel_width -= it->continuation_pixel_width;
18013 else if (row->truncated_on_right_p)
18014 row->pixel_width -= it->truncation_pixel_width;
18015 row->ascent = row->phys_ascent = 0;
18016 row->height = row->phys_height = row->visible_height = 1;
18017 row->extra_line_spacing = 0;
18018 }
18019
18020 /* Compute a hash code for this row. */
18021 {
18022 int area, i;
18023 row->hash = 0;
18024 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18025 for (i = 0; i < row->used[area]; ++i)
18026 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
18027 + row->glyphs[area][i].u.val
18028 + row->glyphs[area][i].face_id
18029 + row->glyphs[area][i].padding_p
18030 + (row->glyphs[area][i].type << 2));
18031 }
18032
18033 it->max_ascent = it->max_descent = 0;
18034 it->max_phys_ascent = it->max_phys_descent = 0;
18035 }
18036
18037
18038 /* Append one space to the glyph row of iterator IT if doing a
18039 window-based redisplay. The space has the same face as
18040 IT->face_id. Value is non-zero if a space was added.
18041
18042 This function is called to make sure that there is always one glyph
18043 at the end of a glyph row that the cursor can be set on under
18044 window-systems. (If there weren't such a glyph we would not know
18045 how wide and tall a box cursor should be displayed).
18046
18047 At the same time this space let's a nicely handle clearing to the
18048 end of the line if the row ends in italic text. */
18049
18050 static int
18051 append_space_for_newline (struct it *it, int default_face_p)
18052 {
18053 if (FRAME_WINDOW_P (it->f))
18054 {
18055 int n = it->glyph_row->used[TEXT_AREA];
18056
18057 if (it->glyph_row->glyphs[TEXT_AREA] + n
18058 < it->glyph_row->glyphs[1 + TEXT_AREA])
18059 {
18060 /* Save some values that must not be changed.
18061 Must save IT->c and IT->len because otherwise
18062 ITERATOR_AT_END_P wouldn't work anymore after
18063 append_space_for_newline has been called. */
18064 enum display_element_type saved_what = it->what;
18065 int saved_c = it->c, saved_len = it->len;
18066 int saved_char_to_display = it->char_to_display;
18067 int saved_x = it->current_x;
18068 int saved_face_id = it->face_id;
18069 struct text_pos saved_pos;
18070 Lisp_Object saved_object;
18071 struct face *face;
18072
18073 saved_object = it->object;
18074 saved_pos = it->position;
18075
18076 it->what = IT_CHARACTER;
18077 memset (&it->position, 0, sizeof it->position);
18078 it->object = make_number (0);
18079 it->c = it->char_to_display = ' ';
18080 it->len = 1;
18081
18082 if (default_face_p)
18083 it->face_id = DEFAULT_FACE_ID;
18084 else if (it->face_before_selective_p)
18085 it->face_id = it->saved_face_id;
18086 face = FACE_FROM_ID (it->f, it->face_id);
18087 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18088
18089 PRODUCE_GLYPHS (it);
18090
18091 it->override_ascent = -1;
18092 it->constrain_row_ascent_descent_p = 0;
18093 it->current_x = saved_x;
18094 it->object = saved_object;
18095 it->position = saved_pos;
18096 it->what = saved_what;
18097 it->face_id = saved_face_id;
18098 it->len = saved_len;
18099 it->c = saved_c;
18100 it->char_to_display = saved_char_to_display;
18101 return 1;
18102 }
18103 }
18104
18105 return 0;
18106 }
18107
18108
18109 /* Extend the face of the last glyph in the text area of IT->glyph_row
18110 to the end of the display line. Called from display_line. If the
18111 glyph row is empty, add a space glyph to it so that we know the
18112 face to draw. Set the glyph row flag fill_line_p. If the glyph
18113 row is R2L, prepend a stretch glyph to cover the empty space to the
18114 left of the leftmost glyph. */
18115
18116 static void
18117 extend_face_to_end_of_line (struct it *it)
18118 {
18119 struct face *face;
18120 struct frame *f = it->f;
18121
18122 /* If line is already filled, do nothing. Non window-system frames
18123 get a grace of one more ``pixel'' because their characters are
18124 1-``pixel'' wide, so they hit the equality too early. This grace
18125 is needed only for R2L rows that are not continued, to produce
18126 one extra blank where we could display the cursor. */
18127 if (it->current_x >= it->last_visible_x
18128 + (!FRAME_WINDOW_P (f)
18129 && it->glyph_row->reversed_p
18130 && !it->glyph_row->continued_p))
18131 return;
18132
18133 /* Face extension extends the background and box of IT->face_id
18134 to the end of the line. If the background equals the background
18135 of the frame, we don't have to do anything. */
18136 if (it->face_before_selective_p)
18137 face = FACE_FROM_ID (f, it->saved_face_id);
18138 else
18139 face = FACE_FROM_ID (f, it->face_id);
18140
18141 if (FRAME_WINDOW_P (f)
18142 && it->glyph_row->displays_text_p
18143 && face->box == FACE_NO_BOX
18144 && face->background == FRAME_BACKGROUND_PIXEL (f)
18145 && !face->stipple
18146 && !it->glyph_row->reversed_p)
18147 return;
18148
18149 /* Set the glyph row flag indicating that the face of the last glyph
18150 in the text area has to be drawn to the end of the text area. */
18151 it->glyph_row->fill_line_p = 1;
18152
18153 /* If current character of IT is not ASCII, make sure we have the
18154 ASCII face. This will be automatically undone the next time
18155 get_next_display_element returns a multibyte character. Note
18156 that the character will always be single byte in unibyte
18157 text. */
18158 if (!ASCII_CHAR_P (it->c))
18159 {
18160 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18161 }
18162
18163 if (FRAME_WINDOW_P (f))
18164 {
18165 /* If the row is empty, add a space with the current face of IT,
18166 so that we know which face to draw. */
18167 if (it->glyph_row->used[TEXT_AREA] == 0)
18168 {
18169 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18170 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
18171 it->glyph_row->used[TEXT_AREA] = 1;
18172 }
18173 #ifdef HAVE_WINDOW_SYSTEM
18174 if (it->glyph_row->reversed_p)
18175 {
18176 /* Prepend a stretch glyph to the row, such that the
18177 rightmost glyph will be drawn flushed all the way to the
18178 right margin of the window. The stretch glyph that will
18179 occupy the empty space, if any, to the left of the
18180 glyphs. */
18181 struct font *font = face->font ? face->font : FRAME_FONT (f);
18182 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18183 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18184 struct glyph *g;
18185 int row_width, stretch_ascent, stretch_width;
18186 struct text_pos saved_pos;
18187 int saved_face_id, saved_avoid_cursor;
18188
18189 for (row_width = 0, g = row_start; g < row_end; g++)
18190 row_width += g->pixel_width;
18191 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18192 if (stretch_width > 0)
18193 {
18194 stretch_ascent =
18195 (((it->ascent + it->descent)
18196 * FONT_BASE (font)) / FONT_HEIGHT (font));
18197 saved_pos = it->position;
18198 memset (&it->position, 0, sizeof it->position);
18199 saved_avoid_cursor = it->avoid_cursor_p;
18200 it->avoid_cursor_p = 1;
18201 saved_face_id = it->face_id;
18202 /* The last row's stretch glyph should get the default
18203 face, to avoid painting the rest of the window with
18204 the region face, if the region ends at ZV. */
18205 if (it->glyph_row->ends_at_zv_p)
18206 it->face_id = DEFAULT_FACE_ID;
18207 else
18208 it->face_id = face->id;
18209 append_stretch_glyph (it, make_number (0), stretch_width,
18210 it->ascent + it->descent, stretch_ascent);
18211 it->position = saved_pos;
18212 it->avoid_cursor_p = saved_avoid_cursor;
18213 it->face_id = saved_face_id;
18214 }
18215 }
18216 #endif /* HAVE_WINDOW_SYSTEM */
18217 }
18218 else
18219 {
18220 /* Save some values that must not be changed. */
18221 int saved_x = it->current_x;
18222 struct text_pos saved_pos;
18223 Lisp_Object saved_object;
18224 enum display_element_type saved_what = it->what;
18225 int saved_face_id = it->face_id;
18226
18227 saved_object = it->object;
18228 saved_pos = it->position;
18229
18230 it->what = IT_CHARACTER;
18231 memset (&it->position, 0, sizeof it->position);
18232 it->object = make_number (0);
18233 it->c = it->char_to_display = ' ';
18234 it->len = 1;
18235 /* The last row's blank glyphs should get the default face, to
18236 avoid painting the rest of the window with the region face,
18237 if the region ends at ZV. */
18238 if (it->glyph_row->ends_at_zv_p)
18239 it->face_id = DEFAULT_FACE_ID;
18240 else
18241 it->face_id = face->id;
18242
18243 PRODUCE_GLYPHS (it);
18244
18245 while (it->current_x <= it->last_visible_x)
18246 PRODUCE_GLYPHS (it);
18247
18248 /* Don't count these blanks really. It would let us insert a left
18249 truncation glyph below and make us set the cursor on them, maybe. */
18250 it->current_x = saved_x;
18251 it->object = saved_object;
18252 it->position = saved_pos;
18253 it->what = saved_what;
18254 it->face_id = saved_face_id;
18255 }
18256 }
18257
18258
18259 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18260 trailing whitespace. */
18261
18262 static int
18263 trailing_whitespace_p (ptrdiff_t charpos)
18264 {
18265 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18266 int c = 0;
18267
18268 while (bytepos < ZV_BYTE
18269 && (c = FETCH_CHAR (bytepos),
18270 c == ' ' || c == '\t'))
18271 ++bytepos;
18272
18273 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18274 {
18275 if (bytepos != PT_BYTE)
18276 return 1;
18277 }
18278 return 0;
18279 }
18280
18281
18282 /* Highlight trailing whitespace, if any, in ROW. */
18283
18284 static void
18285 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18286 {
18287 int used = row->used[TEXT_AREA];
18288
18289 if (used)
18290 {
18291 struct glyph *start = row->glyphs[TEXT_AREA];
18292 struct glyph *glyph = start + used - 1;
18293
18294 if (row->reversed_p)
18295 {
18296 /* Right-to-left rows need to be processed in the opposite
18297 direction, so swap the edge pointers. */
18298 glyph = start;
18299 start = row->glyphs[TEXT_AREA] + used - 1;
18300 }
18301
18302 /* Skip over glyphs inserted to display the cursor at the
18303 end of a line, for extending the face of the last glyph
18304 to the end of the line on terminals, and for truncation
18305 and continuation glyphs. */
18306 if (!row->reversed_p)
18307 {
18308 while (glyph >= start
18309 && glyph->type == CHAR_GLYPH
18310 && INTEGERP (glyph->object))
18311 --glyph;
18312 }
18313 else
18314 {
18315 while (glyph <= start
18316 && glyph->type == CHAR_GLYPH
18317 && INTEGERP (glyph->object))
18318 ++glyph;
18319 }
18320
18321 /* If last glyph is a space or stretch, and it's trailing
18322 whitespace, set the face of all trailing whitespace glyphs in
18323 IT->glyph_row to `trailing-whitespace'. */
18324 if ((row->reversed_p ? glyph <= start : glyph >= start)
18325 && BUFFERP (glyph->object)
18326 && (glyph->type == STRETCH_GLYPH
18327 || (glyph->type == CHAR_GLYPH
18328 && glyph->u.ch == ' '))
18329 && trailing_whitespace_p (glyph->charpos))
18330 {
18331 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18332 if (face_id < 0)
18333 return;
18334
18335 if (!row->reversed_p)
18336 {
18337 while (glyph >= start
18338 && BUFFERP (glyph->object)
18339 && (glyph->type == STRETCH_GLYPH
18340 || (glyph->type == CHAR_GLYPH
18341 && glyph->u.ch == ' ')))
18342 (glyph--)->face_id = face_id;
18343 }
18344 else
18345 {
18346 while (glyph <= start
18347 && BUFFERP (glyph->object)
18348 && (glyph->type == STRETCH_GLYPH
18349 || (glyph->type == CHAR_GLYPH
18350 && glyph->u.ch == ' ')))
18351 (glyph++)->face_id = face_id;
18352 }
18353 }
18354 }
18355 }
18356
18357
18358 /* Value is non-zero if glyph row ROW should be
18359 used to hold the cursor. */
18360
18361 static int
18362 cursor_row_p (struct glyph_row *row)
18363 {
18364 int result = 1;
18365
18366 if (PT == CHARPOS (row->end.pos)
18367 || PT == MATRIX_ROW_END_CHARPOS (row))
18368 {
18369 /* Suppose the row ends on a string.
18370 Unless the row is continued, that means it ends on a newline
18371 in the string. If it's anything other than a display string
18372 (e.g. a before-string from an overlay), we don't want the
18373 cursor there. (This heuristic seems to give the optimal
18374 behavior for the various types of multi-line strings.) */
18375 if (CHARPOS (row->end.string_pos) >= 0)
18376 {
18377 if (row->continued_p)
18378 result = 1;
18379 else
18380 {
18381 /* Check for `display' property. */
18382 struct glyph *beg = row->glyphs[TEXT_AREA];
18383 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18384 struct glyph *glyph;
18385
18386 result = 0;
18387 for (glyph = end; glyph >= beg; --glyph)
18388 if (STRINGP (glyph->object))
18389 {
18390 Lisp_Object prop
18391 = Fget_char_property (make_number (PT),
18392 Qdisplay, Qnil);
18393 result =
18394 (!NILP (prop)
18395 && display_prop_string_p (prop, glyph->object));
18396 break;
18397 }
18398 }
18399 }
18400 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18401 {
18402 /* If the row ends in middle of a real character,
18403 and the line is continued, we want the cursor here.
18404 That's because CHARPOS (ROW->end.pos) would equal
18405 PT if PT is before the character. */
18406 if (!row->ends_in_ellipsis_p)
18407 result = row->continued_p;
18408 else
18409 /* If the row ends in an ellipsis, then
18410 CHARPOS (ROW->end.pos) will equal point after the
18411 invisible text. We want that position to be displayed
18412 after the ellipsis. */
18413 result = 0;
18414 }
18415 /* If the row ends at ZV, display the cursor at the end of that
18416 row instead of at the start of the row below. */
18417 else if (row->ends_at_zv_p)
18418 result = 1;
18419 else
18420 result = 0;
18421 }
18422
18423 return result;
18424 }
18425
18426 \f
18427
18428 /* Push the property PROP so that it will be rendered at the current
18429 position in IT. Return 1 if PROP was successfully pushed, 0
18430 otherwise. Called from handle_line_prefix to handle the
18431 `line-prefix' and `wrap-prefix' properties. */
18432
18433 static int
18434 push_display_prop (struct it *it, Lisp_Object prop)
18435 {
18436 struct text_pos pos =
18437 (it->method == GET_FROM_STRING) ? it->current.string_pos : it->current.pos;
18438
18439 xassert (it->method == GET_FROM_BUFFER
18440 || it->method == GET_FROM_STRING);
18441
18442 /* We need to save the current buffer/string position, so it will be
18443 restored by pop_it, because iterate_out_of_display_property
18444 depends on that being set correctly, but some situations leave
18445 it->position not yet set when this function is called. */
18446 push_it (it, &pos);
18447
18448 if (STRINGP (prop))
18449 {
18450 if (SCHARS (prop) == 0)
18451 {
18452 pop_it (it);
18453 return 0;
18454 }
18455
18456 it->string = prop;
18457 it->multibyte_p = STRING_MULTIBYTE (it->string);
18458 it->current.overlay_string_index = -1;
18459 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18460 it->end_charpos = it->string_nchars = SCHARS (it->string);
18461 it->method = GET_FROM_STRING;
18462 it->stop_charpos = 0;
18463 it->prev_stop = 0;
18464 it->base_level_stop = 0;
18465
18466 /* Force paragraph direction to be that of the parent
18467 buffer/string. */
18468 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18469 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18470 else
18471 it->paragraph_embedding = L2R;
18472
18473 /* Set up the bidi iterator for this display string. */
18474 if (it->bidi_p)
18475 {
18476 it->bidi_it.string.lstring = it->string;
18477 it->bidi_it.string.s = NULL;
18478 it->bidi_it.string.schars = it->end_charpos;
18479 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18480 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18481 it->bidi_it.string.unibyte = !it->multibyte_p;
18482 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18483 }
18484 }
18485 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18486 {
18487 it->method = GET_FROM_STRETCH;
18488 it->object = prop;
18489 }
18490 #ifdef HAVE_WINDOW_SYSTEM
18491 else if (IMAGEP (prop))
18492 {
18493 it->what = IT_IMAGE;
18494 it->image_id = lookup_image (it->f, prop);
18495 it->method = GET_FROM_IMAGE;
18496 }
18497 #endif /* HAVE_WINDOW_SYSTEM */
18498 else
18499 {
18500 pop_it (it); /* bogus display property, give up */
18501 return 0;
18502 }
18503
18504 return 1;
18505 }
18506
18507 /* Return the character-property PROP at the current position in IT. */
18508
18509 static Lisp_Object
18510 get_it_property (struct it *it, Lisp_Object prop)
18511 {
18512 Lisp_Object position;
18513
18514 if (STRINGP (it->object))
18515 position = make_number (IT_STRING_CHARPOS (*it));
18516 else if (BUFFERP (it->object))
18517 position = make_number (IT_CHARPOS (*it));
18518 else
18519 return Qnil;
18520
18521 return Fget_char_property (position, prop, it->object);
18522 }
18523
18524 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18525
18526 static void
18527 handle_line_prefix (struct it *it)
18528 {
18529 Lisp_Object prefix;
18530
18531 if (it->continuation_lines_width > 0)
18532 {
18533 prefix = get_it_property (it, Qwrap_prefix);
18534 if (NILP (prefix))
18535 prefix = Vwrap_prefix;
18536 }
18537 else
18538 {
18539 prefix = get_it_property (it, Qline_prefix);
18540 if (NILP (prefix))
18541 prefix = Vline_prefix;
18542 }
18543 if (! NILP (prefix) && push_display_prop (it, prefix))
18544 {
18545 /* If the prefix is wider than the window, and we try to wrap
18546 it, it would acquire its own wrap prefix, and so on till the
18547 iterator stack overflows. So, don't wrap the prefix. */
18548 it->line_wrap = TRUNCATE;
18549 it->avoid_cursor_p = 1;
18550 }
18551 }
18552
18553 \f
18554
18555 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18556 only for R2L lines from display_line and display_string, when they
18557 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18558 the line/string needs to be continued on the next glyph row. */
18559 static void
18560 unproduce_glyphs (struct it *it, int n)
18561 {
18562 struct glyph *glyph, *end;
18563
18564 xassert (it->glyph_row);
18565 xassert (it->glyph_row->reversed_p);
18566 xassert (it->area == TEXT_AREA);
18567 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18568
18569 if (n > it->glyph_row->used[TEXT_AREA])
18570 n = it->glyph_row->used[TEXT_AREA];
18571 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18572 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18573 for ( ; glyph < end; glyph++)
18574 glyph[-n] = *glyph;
18575 }
18576
18577 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18578 and ROW->maxpos. */
18579 static void
18580 find_row_edges (struct it *it, struct glyph_row *row,
18581 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18582 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18583 {
18584 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18585 lines' rows is implemented for bidi-reordered rows. */
18586
18587 /* ROW->minpos is the value of min_pos, the minimal buffer position
18588 we have in ROW, or ROW->start.pos if that is smaller. */
18589 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18590 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18591 else
18592 /* We didn't find buffer positions smaller than ROW->start, or
18593 didn't find _any_ valid buffer positions in any of the glyphs,
18594 so we must trust the iterator's computed positions. */
18595 row->minpos = row->start.pos;
18596 if (max_pos <= 0)
18597 {
18598 max_pos = CHARPOS (it->current.pos);
18599 max_bpos = BYTEPOS (it->current.pos);
18600 }
18601
18602 /* Here are the various use-cases for ending the row, and the
18603 corresponding values for ROW->maxpos:
18604
18605 Line ends in a newline from buffer eol_pos + 1
18606 Line is continued from buffer max_pos + 1
18607 Line is truncated on right it->current.pos
18608 Line ends in a newline from string max_pos + 1(*)
18609 (*) + 1 only when line ends in a forward scan
18610 Line is continued from string max_pos
18611 Line is continued from display vector max_pos
18612 Line is entirely from a string min_pos == max_pos
18613 Line is entirely from a display vector min_pos == max_pos
18614 Line that ends at ZV ZV
18615
18616 If you discover other use-cases, please add them here as
18617 appropriate. */
18618 if (row->ends_at_zv_p)
18619 row->maxpos = it->current.pos;
18620 else if (row->used[TEXT_AREA])
18621 {
18622 int seen_this_string = 0;
18623 struct glyph_row *r1 = row - 1;
18624
18625 /* Did we see the same display string on the previous row? */
18626 if (STRINGP (it->object)
18627 /* this is not the first row */
18628 && row > it->w->desired_matrix->rows
18629 /* previous row is not the header line */
18630 && !r1->mode_line_p
18631 /* previous row also ends in a newline from a string */
18632 && r1->ends_in_newline_from_string_p)
18633 {
18634 struct glyph *start, *end;
18635
18636 /* Search for the last glyph of the previous row that came
18637 from buffer or string. Depending on whether the row is
18638 L2R or R2L, we need to process it front to back or the
18639 other way round. */
18640 if (!r1->reversed_p)
18641 {
18642 start = r1->glyphs[TEXT_AREA];
18643 end = start + r1->used[TEXT_AREA];
18644 /* Glyphs inserted by redisplay have an integer (zero)
18645 as their object. */
18646 while (end > start
18647 && INTEGERP ((end - 1)->object)
18648 && (end - 1)->charpos <= 0)
18649 --end;
18650 if (end > start)
18651 {
18652 if (EQ ((end - 1)->object, it->object))
18653 seen_this_string = 1;
18654 }
18655 else
18656 /* If all the glyphs of the previous row were inserted
18657 by redisplay, it means the previous row was
18658 produced from a single newline, which is only
18659 possible if that newline came from the same string
18660 as the one which produced this ROW. */
18661 seen_this_string = 1;
18662 }
18663 else
18664 {
18665 end = r1->glyphs[TEXT_AREA] - 1;
18666 start = end + r1->used[TEXT_AREA];
18667 while (end < start
18668 && INTEGERP ((end + 1)->object)
18669 && (end + 1)->charpos <= 0)
18670 ++end;
18671 if (end < start)
18672 {
18673 if (EQ ((end + 1)->object, it->object))
18674 seen_this_string = 1;
18675 }
18676 else
18677 seen_this_string = 1;
18678 }
18679 }
18680 /* Take note of each display string that covers a newline only
18681 once, the first time we see it. This is for when a display
18682 string includes more than one newline in it. */
18683 if (row->ends_in_newline_from_string_p && !seen_this_string)
18684 {
18685 /* If we were scanning the buffer forward when we displayed
18686 the string, we want to account for at least one buffer
18687 position that belongs to this row (position covered by
18688 the display string), so that cursor positioning will
18689 consider this row as a candidate when point is at the end
18690 of the visual line represented by this row. This is not
18691 required when scanning back, because max_pos will already
18692 have a much larger value. */
18693 if (CHARPOS (row->end.pos) > max_pos)
18694 INC_BOTH (max_pos, max_bpos);
18695 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18696 }
18697 else if (CHARPOS (it->eol_pos) > 0)
18698 SET_TEXT_POS (row->maxpos,
18699 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18700 else if (row->continued_p)
18701 {
18702 /* If max_pos is different from IT's current position, it
18703 means IT->method does not belong to the display element
18704 at max_pos. However, it also means that the display
18705 element at max_pos was displayed in its entirety on this
18706 line, which is equivalent to saying that the next line
18707 starts at the next buffer position. */
18708 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18709 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18710 else
18711 {
18712 INC_BOTH (max_pos, max_bpos);
18713 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18714 }
18715 }
18716 else if (row->truncated_on_right_p)
18717 /* display_line already called reseat_at_next_visible_line_start,
18718 which puts the iterator at the beginning of the next line, in
18719 the logical order. */
18720 row->maxpos = it->current.pos;
18721 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18722 /* A line that is entirely from a string/image/stretch... */
18723 row->maxpos = row->minpos;
18724 else
18725 abort ();
18726 }
18727 else
18728 row->maxpos = it->current.pos;
18729 }
18730
18731 /* Construct the glyph row IT->glyph_row in the desired matrix of
18732 IT->w from text at the current position of IT. See dispextern.h
18733 for an overview of struct it. Value is non-zero if
18734 IT->glyph_row displays text, as opposed to a line displaying ZV
18735 only. */
18736
18737 static int
18738 display_line (struct it *it)
18739 {
18740 struct glyph_row *row = it->glyph_row;
18741 Lisp_Object overlay_arrow_string;
18742 struct it wrap_it;
18743 void *wrap_data = NULL;
18744 int may_wrap = 0, wrap_x IF_LINT (= 0);
18745 int wrap_row_used = -1;
18746 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18747 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18748 int wrap_row_extra_line_spacing IF_LINT (= 0);
18749 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18750 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18751 int cvpos;
18752 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
18753 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18754
18755 /* We always start displaying at hpos zero even if hscrolled. */
18756 xassert (it->hpos == 0 && it->current_x == 0);
18757
18758 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18759 >= it->w->desired_matrix->nrows)
18760 {
18761 it->w->nrows_scale_factor++;
18762 fonts_changed_p = 1;
18763 return 0;
18764 }
18765
18766 /* Is IT->w showing the region? */
18767 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18768
18769 /* Clear the result glyph row and enable it. */
18770 prepare_desired_row (row);
18771
18772 row->y = it->current_y;
18773 row->start = it->start;
18774 row->continuation_lines_width = it->continuation_lines_width;
18775 row->displays_text_p = 1;
18776 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18777 it->starts_in_middle_of_char_p = 0;
18778
18779 /* Arrange the overlays nicely for our purposes. Usually, we call
18780 display_line on only one line at a time, in which case this
18781 can't really hurt too much, or we call it on lines which appear
18782 one after another in the buffer, in which case all calls to
18783 recenter_overlay_lists but the first will be pretty cheap. */
18784 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18785
18786 /* Move over display elements that are not visible because we are
18787 hscrolled. This may stop at an x-position < IT->first_visible_x
18788 if the first glyph is partially visible or if we hit a line end. */
18789 if (it->current_x < it->first_visible_x)
18790 {
18791 this_line_min_pos = row->start.pos;
18792 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18793 MOVE_TO_POS | MOVE_TO_X);
18794 /* Record the smallest positions seen while we moved over
18795 display elements that are not visible. This is needed by
18796 redisplay_internal for optimizing the case where the cursor
18797 stays inside the same line. The rest of this function only
18798 considers positions that are actually displayed, so
18799 RECORD_MAX_MIN_POS will not otherwise record positions that
18800 are hscrolled to the left of the left edge of the window. */
18801 min_pos = CHARPOS (this_line_min_pos);
18802 min_bpos = BYTEPOS (this_line_min_pos);
18803 }
18804 else
18805 {
18806 /* We only do this when not calling `move_it_in_display_line_to'
18807 above, because move_it_in_display_line_to calls
18808 handle_line_prefix itself. */
18809 handle_line_prefix (it);
18810 }
18811
18812 /* Get the initial row height. This is either the height of the
18813 text hscrolled, if there is any, or zero. */
18814 row->ascent = it->max_ascent;
18815 row->height = it->max_ascent + it->max_descent;
18816 row->phys_ascent = it->max_phys_ascent;
18817 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18818 row->extra_line_spacing = it->max_extra_line_spacing;
18819
18820 /* Utility macro to record max and min buffer positions seen until now. */
18821 #define RECORD_MAX_MIN_POS(IT) \
18822 do \
18823 { \
18824 int composition_p = (IT)->what == IT_COMPOSITION; \
18825 ptrdiff_t current_pos = \
18826 composition_p ? (IT)->cmp_it.charpos \
18827 : IT_CHARPOS (*(IT)); \
18828 ptrdiff_t current_bpos = \
18829 composition_p ? CHAR_TO_BYTE (current_pos) \
18830 : IT_BYTEPOS (*(IT)); \
18831 if (current_pos < min_pos) \
18832 { \
18833 min_pos = current_pos; \
18834 min_bpos = current_bpos; \
18835 } \
18836 if (IT_CHARPOS (*it) > max_pos) \
18837 { \
18838 max_pos = IT_CHARPOS (*it); \
18839 max_bpos = IT_BYTEPOS (*it); \
18840 } \
18841 } \
18842 while (0)
18843
18844 /* Loop generating characters. The loop is left with IT on the next
18845 character to display. */
18846 while (1)
18847 {
18848 int n_glyphs_before, hpos_before, x_before;
18849 int x, nglyphs;
18850 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18851
18852 /* Retrieve the next thing to display. Value is zero if end of
18853 buffer reached. */
18854 if (!get_next_display_element (it))
18855 {
18856 /* Maybe add a space at the end of this line that is used to
18857 display the cursor there under X. Set the charpos of the
18858 first glyph of blank lines not corresponding to any text
18859 to -1. */
18860 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18861 row->exact_window_width_line_p = 1;
18862 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18863 || row->used[TEXT_AREA] == 0)
18864 {
18865 row->glyphs[TEXT_AREA]->charpos = -1;
18866 row->displays_text_p = 0;
18867
18868 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18869 && (!MINI_WINDOW_P (it->w)
18870 || (minibuf_level && EQ (it->window, minibuf_window))))
18871 row->indicate_empty_line_p = 1;
18872 }
18873
18874 it->continuation_lines_width = 0;
18875 row->ends_at_zv_p = 1;
18876 /* A row that displays right-to-left text must always have
18877 its last face extended all the way to the end of line,
18878 even if this row ends in ZV, because we still write to
18879 the screen left to right. */
18880 if (row->reversed_p)
18881 extend_face_to_end_of_line (it);
18882 break;
18883 }
18884
18885 /* Now, get the metrics of what we want to display. This also
18886 generates glyphs in `row' (which is IT->glyph_row). */
18887 n_glyphs_before = row->used[TEXT_AREA];
18888 x = it->current_x;
18889
18890 /* Remember the line height so far in case the next element doesn't
18891 fit on the line. */
18892 if (it->line_wrap != TRUNCATE)
18893 {
18894 ascent = it->max_ascent;
18895 descent = it->max_descent;
18896 phys_ascent = it->max_phys_ascent;
18897 phys_descent = it->max_phys_descent;
18898
18899 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18900 {
18901 if (IT_DISPLAYING_WHITESPACE (it))
18902 may_wrap = 1;
18903 else if (may_wrap)
18904 {
18905 SAVE_IT (wrap_it, *it, wrap_data);
18906 wrap_x = x;
18907 wrap_row_used = row->used[TEXT_AREA];
18908 wrap_row_ascent = row->ascent;
18909 wrap_row_height = row->height;
18910 wrap_row_phys_ascent = row->phys_ascent;
18911 wrap_row_phys_height = row->phys_height;
18912 wrap_row_extra_line_spacing = row->extra_line_spacing;
18913 wrap_row_min_pos = min_pos;
18914 wrap_row_min_bpos = min_bpos;
18915 wrap_row_max_pos = max_pos;
18916 wrap_row_max_bpos = max_bpos;
18917 may_wrap = 0;
18918 }
18919 }
18920 }
18921
18922 PRODUCE_GLYPHS (it);
18923
18924 /* If this display element was in marginal areas, continue with
18925 the next one. */
18926 if (it->area != TEXT_AREA)
18927 {
18928 row->ascent = max (row->ascent, it->max_ascent);
18929 row->height = max (row->height, it->max_ascent + it->max_descent);
18930 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18931 row->phys_height = max (row->phys_height,
18932 it->max_phys_ascent + it->max_phys_descent);
18933 row->extra_line_spacing = max (row->extra_line_spacing,
18934 it->max_extra_line_spacing);
18935 set_iterator_to_next (it, 1);
18936 continue;
18937 }
18938
18939 /* Does the display element fit on the line? If we truncate
18940 lines, we should draw past the right edge of the window. If
18941 we don't truncate, we want to stop so that we can display the
18942 continuation glyph before the right margin. If lines are
18943 continued, there are two possible strategies for characters
18944 resulting in more than 1 glyph (e.g. tabs): Display as many
18945 glyphs as possible in this line and leave the rest for the
18946 continuation line, or display the whole element in the next
18947 line. Original redisplay did the former, so we do it also. */
18948 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18949 hpos_before = it->hpos;
18950 x_before = x;
18951
18952 if (/* Not a newline. */
18953 nglyphs > 0
18954 /* Glyphs produced fit entirely in the line. */
18955 && it->current_x < it->last_visible_x)
18956 {
18957 it->hpos += nglyphs;
18958 row->ascent = max (row->ascent, it->max_ascent);
18959 row->height = max (row->height, it->max_ascent + it->max_descent);
18960 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18961 row->phys_height = max (row->phys_height,
18962 it->max_phys_ascent + it->max_phys_descent);
18963 row->extra_line_spacing = max (row->extra_line_spacing,
18964 it->max_extra_line_spacing);
18965 if (it->current_x - it->pixel_width < it->first_visible_x)
18966 row->x = x - it->first_visible_x;
18967 /* Record the maximum and minimum buffer positions seen so
18968 far in glyphs that will be displayed by this row. */
18969 if (it->bidi_p)
18970 RECORD_MAX_MIN_POS (it);
18971 }
18972 else
18973 {
18974 int i, new_x;
18975 struct glyph *glyph;
18976
18977 for (i = 0; i < nglyphs; ++i, x = new_x)
18978 {
18979 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18980 new_x = x + glyph->pixel_width;
18981
18982 if (/* Lines are continued. */
18983 it->line_wrap != TRUNCATE
18984 && (/* Glyph doesn't fit on the line. */
18985 new_x > it->last_visible_x
18986 /* Or it fits exactly on a window system frame. */
18987 || (new_x == it->last_visible_x
18988 && FRAME_WINDOW_P (it->f))))
18989 {
18990 /* End of a continued line. */
18991
18992 if (it->hpos == 0
18993 || (new_x == it->last_visible_x
18994 && FRAME_WINDOW_P (it->f)))
18995 {
18996 /* Current glyph is the only one on the line or
18997 fits exactly on the line. We must continue
18998 the line because we can't draw the cursor
18999 after the glyph. */
19000 row->continued_p = 1;
19001 it->current_x = new_x;
19002 it->continuation_lines_width += new_x;
19003 ++it->hpos;
19004 if (i == nglyphs - 1)
19005 {
19006 /* If line-wrap is on, check if a previous
19007 wrap point was found. */
19008 if (wrap_row_used > 0
19009 /* Even if there is a previous wrap
19010 point, continue the line here as
19011 usual, if (i) the previous character
19012 was a space or tab AND (ii) the
19013 current character is not. */
19014 && (!may_wrap
19015 || IT_DISPLAYING_WHITESPACE (it)))
19016 goto back_to_wrap;
19017
19018 /* Record the maximum and minimum buffer
19019 positions seen so far in glyphs that will be
19020 displayed by this row. */
19021 if (it->bidi_p)
19022 RECORD_MAX_MIN_POS (it);
19023 set_iterator_to_next (it, 1);
19024 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19025 {
19026 if (!get_next_display_element (it))
19027 {
19028 row->exact_window_width_line_p = 1;
19029 it->continuation_lines_width = 0;
19030 row->continued_p = 0;
19031 row->ends_at_zv_p = 1;
19032 }
19033 else if (ITERATOR_AT_END_OF_LINE_P (it))
19034 {
19035 row->continued_p = 0;
19036 row->exact_window_width_line_p = 1;
19037 }
19038 }
19039 }
19040 else if (it->bidi_p)
19041 RECORD_MAX_MIN_POS (it);
19042 }
19043 else if (CHAR_GLYPH_PADDING_P (*glyph)
19044 && !FRAME_WINDOW_P (it->f))
19045 {
19046 /* A padding glyph that doesn't fit on this line.
19047 This means the whole character doesn't fit
19048 on the line. */
19049 if (row->reversed_p)
19050 unproduce_glyphs (it, row->used[TEXT_AREA]
19051 - n_glyphs_before);
19052 row->used[TEXT_AREA] = n_glyphs_before;
19053
19054 /* Fill the rest of the row with continuation
19055 glyphs like in 20.x. */
19056 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19057 < row->glyphs[1 + TEXT_AREA])
19058 produce_special_glyphs (it, IT_CONTINUATION);
19059
19060 row->continued_p = 1;
19061 it->current_x = x_before;
19062 it->continuation_lines_width += x_before;
19063
19064 /* Restore the height to what it was before the
19065 element not fitting on the line. */
19066 it->max_ascent = ascent;
19067 it->max_descent = descent;
19068 it->max_phys_ascent = phys_ascent;
19069 it->max_phys_descent = phys_descent;
19070 }
19071 else if (wrap_row_used > 0)
19072 {
19073 back_to_wrap:
19074 if (row->reversed_p)
19075 unproduce_glyphs (it,
19076 row->used[TEXT_AREA] - wrap_row_used);
19077 RESTORE_IT (it, &wrap_it, wrap_data);
19078 it->continuation_lines_width += wrap_x;
19079 row->used[TEXT_AREA] = wrap_row_used;
19080 row->ascent = wrap_row_ascent;
19081 row->height = wrap_row_height;
19082 row->phys_ascent = wrap_row_phys_ascent;
19083 row->phys_height = wrap_row_phys_height;
19084 row->extra_line_spacing = wrap_row_extra_line_spacing;
19085 min_pos = wrap_row_min_pos;
19086 min_bpos = wrap_row_min_bpos;
19087 max_pos = wrap_row_max_pos;
19088 max_bpos = wrap_row_max_bpos;
19089 row->continued_p = 1;
19090 row->ends_at_zv_p = 0;
19091 row->exact_window_width_line_p = 0;
19092 it->continuation_lines_width += x;
19093
19094 /* Make sure that a non-default face is extended
19095 up to the right margin of the window. */
19096 extend_face_to_end_of_line (it);
19097 }
19098 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19099 {
19100 /* A TAB that extends past the right edge of the
19101 window. This produces a single glyph on
19102 window system frames. We leave the glyph in
19103 this row and let it fill the row, but don't
19104 consume the TAB. */
19105 it->continuation_lines_width += it->last_visible_x;
19106 row->ends_in_middle_of_char_p = 1;
19107 row->continued_p = 1;
19108 glyph->pixel_width = it->last_visible_x - x;
19109 it->starts_in_middle_of_char_p = 1;
19110 }
19111 else
19112 {
19113 /* Something other than a TAB that draws past
19114 the right edge of the window. Restore
19115 positions to values before the element. */
19116 if (row->reversed_p)
19117 unproduce_glyphs (it, row->used[TEXT_AREA]
19118 - (n_glyphs_before + i));
19119 row->used[TEXT_AREA] = n_glyphs_before + i;
19120
19121 /* Display continuation glyphs. */
19122 if (!FRAME_WINDOW_P (it->f))
19123 produce_special_glyphs (it, IT_CONTINUATION);
19124 row->continued_p = 1;
19125
19126 it->current_x = x_before;
19127 it->continuation_lines_width += x;
19128 extend_face_to_end_of_line (it);
19129
19130 if (nglyphs > 1 && i > 0)
19131 {
19132 row->ends_in_middle_of_char_p = 1;
19133 it->starts_in_middle_of_char_p = 1;
19134 }
19135
19136 /* Restore the height to what it was before the
19137 element not fitting on the line. */
19138 it->max_ascent = ascent;
19139 it->max_descent = descent;
19140 it->max_phys_ascent = phys_ascent;
19141 it->max_phys_descent = phys_descent;
19142 }
19143
19144 break;
19145 }
19146 else if (new_x > it->first_visible_x)
19147 {
19148 /* Increment number of glyphs actually displayed. */
19149 ++it->hpos;
19150
19151 /* Record the maximum and minimum buffer positions
19152 seen so far in glyphs that will be displayed by
19153 this row. */
19154 if (it->bidi_p)
19155 RECORD_MAX_MIN_POS (it);
19156
19157 if (x < it->first_visible_x)
19158 /* Glyph is partially visible, i.e. row starts at
19159 negative X position. */
19160 row->x = x - it->first_visible_x;
19161 }
19162 else
19163 {
19164 /* Glyph is completely off the left margin of the
19165 window. This should not happen because of the
19166 move_it_in_display_line at the start of this
19167 function, unless the text display area of the
19168 window is empty. */
19169 xassert (it->first_visible_x <= it->last_visible_x);
19170 }
19171 }
19172 /* Even if this display element produced no glyphs at all,
19173 we want to record its position. */
19174 if (it->bidi_p && nglyphs == 0)
19175 RECORD_MAX_MIN_POS (it);
19176
19177 row->ascent = max (row->ascent, it->max_ascent);
19178 row->height = max (row->height, it->max_ascent + it->max_descent);
19179 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19180 row->phys_height = max (row->phys_height,
19181 it->max_phys_ascent + it->max_phys_descent);
19182 row->extra_line_spacing = max (row->extra_line_spacing,
19183 it->max_extra_line_spacing);
19184
19185 /* End of this display line if row is continued. */
19186 if (row->continued_p || row->ends_at_zv_p)
19187 break;
19188 }
19189
19190 at_end_of_line:
19191 /* Is this a line end? If yes, we're also done, after making
19192 sure that a non-default face is extended up to the right
19193 margin of the window. */
19194 if (ITERATOR_AT_END_OF_LINE_P (it))
19195 {
19196 int used_before = row->used[TEXT_AREA];
19197
19198 row->ends_in_newline_from_string_p = STRINGP (it->object);
19199
19200 /* Add a space at the end of the line that is used to
19201 display the cursor there. */
19202 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19203 append_space_for_newline (it, 0);
19204
19205 /* Extend the face to the end of the line. */
19206 extend_face_to_end_of_line (it);
19207
19208 /* Make sure we have the position. */
19209 if (used_before == 0)
19210 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19211
19212 /* Record the position of the newline, for use in
19213 find_row_edges. */
19214 it->eol_pos = it->current.pos;
19215
19216 /* Consume the line end. This skips over invisible lines. */
19217 set_iterator_to_next (it, 1);
19218 it->continuation_lines_width = 0;
19219 break;
19220 }
19221
19222 /* Proceed with next display element. Note that this skips
19223 over lines invisible because of selective display. */
19224 set_iterator_to_next (it, 1);
19225
19226 /* If we truncate lines, we are done when the last displayed
19227 glyphs reach past the right margin of the window. */
19228 if (it->line_wrap == TRUNCATE
19229 && (FRAME_WINDOW_P (it->f)
19230 ? (it->current_x >= it->last_visible_x)
19231 : (it->current_x > it->last_visible_x)))
19232 {
19233 /* Maybe add truncation glyphs. */
19234 if (!FRAME_WINDOW_P (it->f))
19235 {
19236 int i, n;
19237
19238 if (!row->reversed_p)
19239 {
19240 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19241 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19242 break;
19243 }
19244 else
19245 {
19246 for (i = 0; i < row->used[TEXT_AREA]; i++)
19247 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19248 break;
19249 /* Remove any padding glyphs at the front of ROW, to
19250 make room for the truncation glyphs we will be
19251 adding below. The loop below always inserts at
19252 least one truncation glyph, so also remove the
19253 last glyph added to ROW. */
19254 unproduce_glyphs (it, i + 1);
19255 /* Adjust i for the loop below. */
19256 i = row->used[TEXT_AREA] - (i + 1);
19257 }
19258
19259 for (n = row->used[TEXT_AREA]; i < n; ++i)
19260 {
19261 row->used[TEXT_AREA] = i;
19262 produce_special_glyphs (it, IT_TRUNCATION);
19263 }
19264 }
19265 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19266 {
19267 /* Don't truncate if we can overflow newline into fringe. */
19268 if (!get_next_display_element (it))
19269 {
19270 it->continuation_lines_width = 0;
19271 row->ends_at_zv_p = 1;
19272 row->exact_window_width_line_p = 1;
19273 break;
19274 }
19275 if (ITERATOR_AT_END_OF_LINE_P (it))
19276 {
19277 row->exact_window_width_line_p = 1;
19278 goto at_end_of_line;
19279 }
19280 }
19281
19282 row->truncated_on_right_p = 1;
19283 it->continuation_lines_width = 0;
19284 reseat_at_next_visible_line_start (it, 0);
19285 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19286 it->hpos = hpos_before;
19287 it->current_x = x_before;
19288 break;
19289 }
19290 }
19291
19292 if (wrap_data)
19293 bidi_unshelve_cache (wrap_data, 1);
19294
19295 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19296 at the left window margin. */
19297 if (it->first_visible_x
19298 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19299 {
19300 if (!FRAME_WINDOW_P (it->f))
19301 insert_left_trunc_glyphs (it);
19302 row->truncated_on_left_p = 1;
19303 }
19304
19305 /* Remember the position at which this line ends.
19306
19307 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19308 cannot be before the call to find_row_edges below, since that is
19309 where these positions are determined. */
19310 row->end = it->current;
19311 if (!it->bidi_p)
19312 {
19313 row->minpos = row->start.pos;
19314 row->maxpos = row->end.pos;
19315 }
19316 else
19317 {
19318 /* ROW->minpos and ROW->maxpos must be the smallest and
19319 `1 + the largest' buffer positions in ROW. But if ROW was
19320 bidi-reordered, these two positions can be anywhere in the
19321 row, so we must determine them now. */
19322 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19323 }
19324
19325 /* If the start of this line is the overlay arrow-position, then
19326 mark this glyph row as the one containing the overlay arrow.
19327 This is clearly a mess with variable size fonts. It would be
19328 better to let it be displayed like cursors under X. */
19329 if ((row->displays_text_p || !overlay_arrow_seen)
19330 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19331 !NILP (overlay_arrow_string)))
19332 {
19333 /* Overlay arrow in window redisplay is a fringe bitmap. */
19334 if (STRINGP (overlay_arrow_string))
19335 {
19336 struct glyph_row *arrow_row
19337 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19338 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19339 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19340 struct glyph *p = row->glyphs[TEXT_AREA];
19341 struct glyph *p2, *end;
19342
19343 /* Copy the arrow glyphs. */
19344 while (glyph < arrow_end)
19345 *p++ = *glyph++;
19346
19347 /* Throw away padding glyphs. */
19348 p2 = p;
19349 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19350 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19351 ++p2;
19352 if (p2 > p)
19353 {
19354 while (p2 < end)
19355 *p++ = *p2++;
19356 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19357 }
19358 }
19359 else
19360 {
19361 xassert (INTEGERP (overlay_arrow_string));
19362 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19363 }
19364 overlay_arrow_seen = 1;
19365 }
19366
19367 /* Compute pixel dimensions of this line. */
19368 compute_line_metrics (it);
19369
19370 /* Record whether this row ends inside an ellipsis. */
19371 row->ends_in_ellipsis_p
19372 = (it->method == GET_FROM_DISPLAY_VECTOR
19373 && it->ellipsis_p);
19374
19375 /* Save fringe bitmaps in this row. */
19376 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19377 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19378 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19379 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19380
19381 it->left_user_fringe_bitmap = 0;
19382 it->left_user_fringe_face_id = 0;
19383 it->right_user_fringe_bitmap = 0;
19384 it->right_user_fringe_face_id = 0;
19385
19386 /* Maybe set the cursor. */
19387 cvpos = it->w->cursor.vpos;
19388 if ((cvpos < 0
19389 /* In bidi-reordered rows, keep checking for proper cursor
19390 position even if one has been found already, because buffer
19391 positions in such rows change non-linearly with ROW->VPOS,
19392 when a line is continued. One exception: when we are at ZV,
19393 display cursor on the first suitable glyph row, since all
19394 the empty rows after that also have their position set to ZV. */
19395 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19396 lines' rows is implemented for bidi-reordered rows. */
19397 || (it->bidi_p
19398 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19399 && PT >= MATRIX_ROW_START_CHARPOS (row)
19400 && PT <= MATRIX_ROW_END_CHARPOS (row)
19401 && cursor_row_p (row))
19402 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19403
19404 /* Highlight trailing whitespace. */
19405 if (!NILP (Vshow_trailing_whitespace))
19406 highlight_trailing_whitespace (it->f, it->glyph_row);
19407
19408 /* Prepare for the next line. This line starts horizontally at (X
19409 HPOS) = (0 0). Vertical positions are incremented. As a
19410 convenience for the caller, IT->glyph_row is set to the next
19411 row to be used. */
19412 it->current_x = it->hpos = 0;
19413 it->current_y += row->height;
19414 SET_TEXT_POS (it->eol_pos, 0, 0);
19415 ++it->vpos;
19416 ++it->glyph_row;
19417 /* The next row should by default use the same value of the
19418 reversed_p flag as this one. set_iterator_to_next decides when
19419 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19420 the flag accordingly. */
19421 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19422 it->glyph_row->reversed_p = row->reversed_p;
19423 it->start = row->end;
19424 return row->displays_text_p;
19425
19426 #undef RECORD_MAX_MIN_POS
19427 }
19428
19429 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19430 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19431 doc: /* Return paragraph direction at point in BUFFER.
19432 Value is either `left-to-right' or `right-to-left'.
19433 If BUFFER is omitted or nil, it defaults to the current buffer.
19434
19435 Paragraph direction determines how the text in the paragraph is displayed.
19436 In left-to-right paragraphs, text begins at the left margin of the window
19437 and the reading direction is generally left to right. In right-to-left
19438 paragraphs, text begins at the right margin and is read from right to left.
19439
19440 See also `bidi-paragraph-direction'. */)
19441 (Lisp_Object buffer)
19442 {
19443 struct buffer *buf = current_buffer;
19444 struct buffer *old = buf;
19445
19446 if (! NILP (buffer))
19447 {
19448 CHECK_BUFFER (buffer);
19449 buf = XBUFFER (buffer);
19450 }
19451
19452 if (NILP (BVAR (buf, bidi_display_reordering))
19453 || NILP (BVAR (buf, enable_multibyte_characters)))
19454 return Qleft_to_right;
19455 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19456 return BVAR (buf, bidi_paragraph_direction);
19457 else
19458 {
19459 /* Determine the direction from buffer text. We could try to
19460 use current_matrix if it is up to date, but this seems fast
19461 enough as it is. */
19462 struct bidi_it itb;
19463 ptrdiff_t pos = BUF_PT (buf);
19464 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19465 int c;
19466 void *itb_data = bidi_shelve_cache ();
19467
19468 set_buffer_temp (buf);
19469 /* bidi_paragraph_init finds the base direction of the paragraph
19470 by searching forward from paragraph start. We need the base
19471 direction of the current or _previous_ paragraph, so we need
19472 to make sure we are within that paragraph. To that end, find
19473 the previous non-empty line. */
19474 if (pos >= ZV && pos > BEGV)
19475 {
19476 pos--;
19477 bytepos = CHAR_TO_BYTE (pos);
19478 }
19479 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19480 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19481 {
19482 while ((c = FETCH_BYTE (bytepos)) == '\n'
19483 || c == ' ' || c == '\t' || c == '\f')
19484 {
19485 if (bytepos <= BEGV_BYTE)
19486 break;
19487 bytepos--;
19488 pos--;
19489 }
19490 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19491 bytepos--;
19492 }
19493 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19494 itb.paragraph_dir = NEUTRAL_DIR;
19495 itb.string.s = NULL;
19496 itb.string.lstring = Qnil;
19497 itb.string.bufpos = 0;
19498 itb.string.unibyte = 0;
19499 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19500 bidi_unshelve_cache (itb_data, 0);
19501 set_buffer_temp (old);
19502 switch (itb.paragraph_dir)
19503 {
19504 case L2R:
19505 return Qleft_to_right;
19506 break;
19507 case R2L:
19508 return Qright_to_left;
19509 break;
19510 default:
19511 abort ();
19512 }
19513 }
19514 }
19515
19516
19517 \f
19518 /***********************************************************************
19519 Menu Bar
19520 ***********************************************************************/
19521
19522 /* Redisplay the menu bar in the frame for window W.
19523
19524 The menu bar of X frames that don't have X toolkit support is
19525 displayed in a special window W->frame->menu_bar_window.
19526
19527 The menu bar of terminal frames is treated specially as far as
19528 glyph matrices are concerned. Menu bar lines are not part of
19529 windows, so the update is done directly on the frame matrix rows
19530 for the menu bar. */
19531
19532 static void
19533 display_menu_bar (struct window *w)
19534 {
19535 struct frame *f = XFRAME (WINDOW_FRAME (w));
19536 struct it it;
19537 Lisp_Object items;
19538 int i;
19539
19540 /* Don't do all this for graphical frames. */
19541 #ifdef HAVE_NTGUI
19542 if (FRAME_W32_P (f))
19543 return;
19544 #endif
19545 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19546 if (FRAME_X_P (f))
19547 return;
19548 #endif
19549
19550 #ifdef HAVE_NS
19551 if (FRAME_NS_P (f))
19552 return;
19553 #endif /* HAVE_NS */
19554
19555 #ifdef USE_X_TOOLKIT
19556 xassert (!FRAME_WINDOW_P (f));
19557 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19558 it.first_visible_x = 0;
19559 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19560 #else /* not USE_X_TOOLKIT */
19561 if (FRAME_WINDOW_P (f))
19562 {
19563 /* Menu bar lines are displayed in the desired matrix of the
19564 dummy window menu_bar_window. */
19565 struct window *menu_w;
19566 xassert (WINDOWP (f->menu_bar_window));
19567 menu_w = XWINDOW (f->menu_bar_window);
19568 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19569 MENU_FACE_ID);
19570 it.first_visible_x = 0;
19571 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19572 }
19573 else
19574 {
19575 /* This is a TTY frame, i.e. character hpos/vpos are used as
19576 pixel x/y. */
19577 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19578 MENU_FACE_ID);
19579 it.first_visible_x = 0;
19580 it.last_visible_x = FRAME_COLS (f);
19581 }
19582 #endif /* not USE_X_TOOLKIT */
19583
19584 /* FIXME: This should be controlled by a user option. See the
19585 comments in redisplay_tool_bar and display_mode_line about
19586 this. */
19587 it.paragraph_embedding = L2R;
19588
19589 if (! mode_line_inverse_video)
19590 /* Force the menu-bar to be displayed in the default face. */
19591 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19592
19593 /* Clear all rows of the menu bar. */
19594 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19595 {
19596 struct glyph_row *row = it.glyph_row + i;
19597 clear_glyph_row (row);
19598 row->enabled_p = 1;
19599 row->full_width_p = 1;
19600 }
19601
19602 /* Display all items of the menu bar. */
19603 items = FRAME_MENU_BAR_ITEMS (it.f);
19604 for (i = 0; i < ASIZE (items); i += 4)
19605 {
19606 Lisp_Object string;
19607
19608 /* Stop at nil string. */
19609 string = AREF (items, i + 1);
19610 if (NILP (string))
19611 break;
19612
19613 /* Remember where item was displayed. */
19614 ASET (items, i + 3, make_number (it.hpos));
19615
19616 /* Display the item, pad with one space. */
19617 if (it.current_x < it.last_visible_x)
19618 display_string (NULL, string, Qnil, 0, 0, &it,
19619 SCHARS (string) + 1, 0, 0, -1);
19620 }
19621
19622 /* Fill out the line with spaces. */
19623 if (it.current_x < it.last_visible_x)
19624 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19625
19626 /* Compute the total height of the lines. */
19627 compute_line_metrics (&it);
19628 }
19629
19630
19631 \f
19632 /***********************************************************************
19633 Mode Line
19634 ***********************************************************************/
19635
19636 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19637 FORCE is non-zero, redisplay mode lines unconditionally.
19638 Otherwise, redisplay only mode lines that are garbaged. Value is
19639 the number of windows whose mode lines were redisplayed. */
19640
19641 static int
19642 redisplay_mode_lines (Lisp_Object window, int force)
19643 {
19644 int nwindows = 0;
19645
19646 while (!NILP (window))
19647 {
19648 struct window *w = XWINDOW (window);
19649
19650 if (WINDOWP (w->hchild))
19651 nwindows += redisplay_mode_lines (w->hchild, force);
19652 else if (WINDOWP (w->vchild))
19653 nwindows += redisplay_mode_lines (w->vchild, force);
19654 else if (force
19655 || FRAME_GARBAGED_P (XFRAME (w->frame))
19656 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19657 {
19658 struct text_pos lpoint;
19659 struct buffer *old = current_buffer;
19660
19661 /* Set the window's buffer for the mode line display. */
19662 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19663 set_buffer_internal_1 (XBUFFER (w->buffer));
19664
19665 /* Point refers normally to the selected window. For any
19666 other window, set up appropriate value. */
19667 if (!EQ (window, selected_window))
19668 {
19669 struct text_pos pt;
19670
19671 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19672 if (CHARPOS (pt) < BEGV)
19673 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19674 else if (CHARPOS (pt) > (ZV - 1))
19675 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19676 else
19677 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19678 }
19679
19680 /* Display mode lines. */
19681 clear_glyph_matrix (w->desired_matrix);
19682 if (display_mode_lines (w))
19683 {
19684 ++nwindows;
19685 w->must_be_updated_p = 1;
19686 }
19687
19688 /* Restore old settings. */
19689 set_buffer_internal_1 (old);
19690 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19691 }
19692
19693 window = w->next;
19694 }
19695
19696 return nwindows;
19697 }
19698
19699
19700 /* Display the mode and/or header line of window W. Value is the
19701 sum number of mode lines and header lines displayed. */
19702
19703 static int
19704 display_mode_lines (struct window *w)
19705 {
19706 Lisp_Object old_selected_window, old_selected_frame;
19707 int n = 0;
19708
19709 old_selected_frame = selected_frame;
19710 selected_frame = w->frame;
19711 old_selected_window = selected_window;
19712 XSETWINDOW (selected_window, w);
19713
19714 /* These will be set while the mode line specs are processed. */
19715 line_number_displayed = 0;
19716 w->column_number_displayed = Qnil;
19717
19718 if (WINDOW_WANTS_MODELINE_P (w))
19719 {
19720 struct window *sel_w = XWINDOW (old_selected_window);
19721
19722 /* Select mode line face based on the real selected window. */
19723 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19724 BVAR (current_buffer, mode_line_format));
19725 ++n;
19726 }
19727
19728 if (WINDOW_WANTS_HEADER_LINE_P (w))
19729 {
19730 display_mode_line (w, HEADER_LINE_FACE_ID,
19731 BVAR (current_buffer, header_line_format));
19732 ++n;
19733 }
19734
19735 selected_frame = old_selected_frame;
19736 selected_window = old_selected_window;
19737 return n;
19738 }
19739
19740
19741 /* Display mode or header line of window W. FACE_ID specifies which
19742 line to display; it is either MODE_LINE_FACE_ID or
19743 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19744 display. Value is the pixel height of the mode/header line
19745 displayed. */
19746
19747 static int
19748 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19749 {
19750 struct it it;
19751 struct face *face;
19752 ptrdiff_t count = SPECPDL_INDEX ();
19753
19754 init_iterator (&it, w, -1, -1, NULL, face_id);
19755 /* Don't extend on a previously drawn mode-line.
19756 This may happen if called from pos_visible_p. */
19757 it.glyph_row->enabled_p = 0;
19758 prepare_desired_row (it.glyph_row);
19759
19760 it.glyph_row->mode_line_p = 1;
19761
19762 if (! mode_line_inverse_video)
19763 /* Force the mode-line to be displayed in the default face. */
19764 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19765
19766 /* FIXME: This should be controlled by a user option. But
19767 supporting such an option is not trivial, since the mode line is
19768 made up of many separate strings. */
19769 it.paragraph_embedding = L2R;
19770
19771 record_unwind_protect (unwind_format_mode_line,
19772 format_mode_line_unwind_data (NULL, Qnil, 0));
19773
19774 mode_line_target = MODE_LINE_DISPLAY;
19775
19776 /* Temporarily make frame's keyboard the current kboard so that
19777 kboard-local variables in the mode_line_format will get the right
19778 values. */
19779 push_kboard (FRAME_KBOARD (it.f));
19780 record_unwind_save_match_data ();
19781 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19782 pop_kboard ();
19783
19784 unbind_to (count, Qnil);
19785
19786 /* Fill up with spaces. */
19787 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19788
19789 compute_line_metrics (&it);
19790 it.glyph_row->full_width_p = 1;
19791 it.glyph_row->continued_p = 0;
19792 it.glyph_row->truncated_on_left_p = 0;
19793 it.glyph_row->truncated_on_right_p = 0;
19794
19795 /* Make a 3D mode-line have a shadow at its right end. */
19796 face = FACE_FROM_ID (it.f, face_id);
19797 extend_face_to_end_of_line (&it);
19798 if (face->box != FACE_NO_BOX)
19799 {
19800 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19801 + it.glyph_row->used[TEXT_AREA] - 1);
19802 last->right_box_line_p = 1;
19803 }
19804
19805 return it.glyph_row->height;
19806 }
19807
19808 /* Move element ELT in LIST to the front of LIST.
19809 Return the updated list. */
19810
19811 static Lisp_Object
19812 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19813 {
19814 register Lisp_Object tail, prev;
19815 register Lisp_Object tem;
19816
19817 tail = list;
19818 prev = Qnil;
19819 while (CONSP (tail))
19820 {
19821 tem = XCAR (tail);
19822
19823 if (EQ (elt, tem))
19824 {
19825 /* Splice out the link TAIL. */
19826 if (NILP (prev))
19827 list = XCDR (tail);
19828 else
19829 Fsetcdr (prev, XCDR (tail));
19830
19831 /* Now make it the first. */
19832 Fsetcdr (tail, list);
19833 return tail;
19834 }
19835 else
19836 prev = tail;
19837 tail = XCDR (tail);
19838 QUIT;
19839 }
19840
19841 /* Not found--return unchanged LIST. */
19842 return list;
19843 }
19844
19845 /* Contribute ELT to the mode line for window IT->w. How it
19846 translates into text depends on its data type.
19847
19848 IT describes the display environment in which we display, as usual.
19849
19850 DEPTH is the depth in recursion. It is used to prevent
19851 infinite recursion here.
19852
19853 FIELD_WIDTH is the number of characters the display of ELT should
19854 occupy in the mode line, and PRECISION is the maximum number of
19855 characters to display from ELT's representation. See
19856 display_string for details.
19857
19858 Returns the hpos of the end of the text generated by ELT.
19859
19860 PROPS is a property list to add to any string we encounter.
19861
19862 If RISKY is nonzero, remove (disregard) any properties in any string
19863 we encounter, and ignore :eval and :propertize.
19864
19865 The global variable `mode_line_target' determines whether the
19866 output is passed to `store_mode_line_noprop',
19867 `store_mode_line_string', or `display_string'. */
19868
19869 static int
19870 display_mode_element (struct it *it, int depth, int field_width, int precision,
19871 Lisp_Object elt, Lisp_Object props, int risky)
19872 {
19873 int n = 0, field, prec;
19874 int literal = 0;
19875
19876 tail_recurse:
19877 if (depth > 100)
19878 elt = build_string ("*too-deep*");
19879
19880 depth++;
19881
19882 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19883 {
19884 case Lisp_String:
19885 {
19886 /* A string: output it and check for %-constructs within it. */
19887 unsigned char c;
19888 ptrdiff_t offset = 0;
19889
19890 if (SCHARS (elt) > 0
19891 && (!NILP (props) || risky))
19892 {
19893 Lisp_Object oprops, aelt;
19894 oprops = Ftext_properties_at (make_number (0), elt);
19895
19896 /* If the starting string's properties are not what
19897 we want, translate the string. Also, if the string
19898 is risky, do that anyway. */
19899
19900 if (NILP (Fequal (props, oprops)) || risky)
19901 {
19902 /* If the starting string has properties,
19903 merge the specified ones onto the existing ones. */
19904 if (! NILP (oprops) && !risky)
19905 {
19906 Lisp_Object tem;
19907
19908 oprops = Fcopy_sequence (oprops);
19909 tem = props;
19910 while (CONSP (tem))
19911 {
19912 oprops = Fplist_put (oprops, XCAR (tem),
19913 XCAR (XCDR (tem)));
19914 tem = XCDR (XCDR (tem));
19915 }
19916 props = oprops;
19917 }
19918
19919 aelt = Fassoc (elt, mode_line_proptrans_alist);
19920 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19921 {
19922 /* AELT is what we want. Move it to the front
19923 without consing. */
19924 elt = XCAR (aelt);
19925 mode_line_proptrans_alist
19926 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19927 }
19928 else
19929 {
19930 Lisp_Object tem;
19931
19932 /* If AELT has the wrong props, it is useless.
19933 so get rid of it. */
19934 if (! NILP (aelt))
19935 mode_line_proptrans_alist
19936 = Fdelq (aelt, mode_line_proptrans_alist);
19937
19938 elt = Fcopy_sequence (elt);
19939 Fset_text_properties (make_number (0), Flength (elt),
19940 props, elt);
19941 /* Add this item to mode_line_proptrans_alist. */
19942 mode_line_proptrans_alist
19943 = Fcons (Fcons (elt, props),
19944 mode_line_proptrans_alist);
19945 /* Truncate mode_line_proptrans_alist
19946 to at most 50 elements. */
19947 tem = Fnthcdr (make_number (50),
19948 mode_line_proptrans_alist);
19949 if (! NILP (tem))
19950 XSETCDR (tem, Qnil);
19951 }
19952 }
19953 }
19954
19955 offset = 0;
19956
19957 if (literal)
19958 {
19959 prec = precision - n;
19960 switch (mode_line_target)
19961 {
19962 case MODE_LINE_NOPROP:
19963 case MODE_LINE_TITLE:
19964 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19965 break;
19966 case MODE_LINE_STRING:
19967 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19968 break;
19969 case MODE_LINE_DISPLAY:
19970 n += display_string (NULL, elt, Qnil, 0, 0, it,
19971 0, prec, 0, STRING_MULTIBYTE (elt));
19972 break;
19973 }
19974
19975 break;
19976 }
19977
19978 /* Handle the non-literal case. */
19979
19980 while ((precision <= 0 || n < precision)
19981 && SREF (elt, offset) != 0
19982 && (mode_line_target != MODE_LINE_DISPLAY
19983 || it->current_x < it->last_visible_x))
19984 {
19985 ptrdiff_t last_offset = offset;
19986
19987 /* Advance to end of string or next format specifier. */
19988 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19989 ;
19990
19991 if (offset - 1 != last_offset)
19992 {
19993 ptrdiff_t nchars, nbytes;
19994
19995 /* Output to end of string or up to '%'. Field width
19996 is length of string. Don't output more than
19997 PRECISION allows us. */
19998 offset--;
19999
20000 prec = c_string_width (SDATA (elt) + last_offset,
20001 offset - last_offset, precision - n,
20002 &nchars, &nbytes);
20003
20004 switch (mode_line_target)
20005 {
20006 case MODE_LINE_NOPROP:
20007 case MODE_LINE_TITLE:
20008 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20009 break;
20010 case MODE_LINE_STRING:
20011 {
20012 ptrdiff_t bytepos = last_offset;
20013 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20014 ptrdiff_t endpos = (precision <= 0
20015 ? string_byte_to_char (elt, offset)
20016 : charpos + nchars);
20017
20018 n += store_mode_line_string (NULL,
20019 Fsubstring (elt, make_number (charpos),
20020 make_number (endpos)),
20021 0, 0, 0, Qnil);
20022 }
20023 break;
20024 case MODE_LINE_DISPLAY:
20025 {
20026 ptrdiff_t bytepos = last_offset;
20027 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20028
20029 if (precision <= 0)
20030 nchars = string_byte_to_char (elt, offset) - charpos;
20031 n += display_string (NULL, elt, Qnil, 0, charpos,
20032 it, 0, nchars, 0,
20033 STRING_MULTIBYTE (elt));
20034 }
20035 break;
20036 }
20037 }
20038 else /* c == '%' */
20039 {
20040 ptrdiff_t percent_position = offset;
20041
20042 /* Get the specified minimum width. Zero means
20043 don't pad. */
20044 field = 0;
20045 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20046 field = field * 10 + c - '0';
20047
20048 /* Don't pad beyond the total padding allowed. */
20049 if (field_width - n > 0 && field > field_width - n)
20050 field = field_width - n;
20051
20052 /* Note that either PRECISION <= 0 or N < PRECISION. */
20053 prec = precision - n;
20054
20055 if (c == 'M')
20056 n += display_mode_element (it, depth, field, prec,
20057 Vglobal_mode_string, props,
20058 risky);
20059 else if (c != 0)
20060 {
20061 int multibyte;
20062 ptrdiff_t bytepos, charpos;
20063 const char *spec;
20064 Lisp_Object string;
20065
20066 bytepos = percent_position;
20067 charpos = (STRING_MULTIBYTE (elt)
20068 ? string_byte_to_char (elt, bytepos)
20069 : bytepos);
20070 spec = decode_mode_spec (it->w, c, field, &string);
20071 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20072
20073 switch (mode_line_target)
20074 {
20075 case MODE_LINE_NOPROP:
20076 case MODE_LINE_TITLE:
20077 n += store_mode_line_noprop (spec, field, prec);
20078 break;
20079 case MODE_LINE_STRING:
20080 {
20081 Lisp_Object tem = build_string (spec);
20082 props = Ftext_properties_at (make_number (charpos), elt);
20083 /* Should only keep face property in props */
20084 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20085 }
20086 break;
20087 case MODE_LINE_DISPLAY:
20088 {
20089 int nglyphs_before, nwritten;
20090
20091 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20092 nwritten = display_string (spec, string, elt,
20093 charpos, 0, it,
20094 field, prec, 0,
20095 multibyte);
20096
20097 /* Assign to the glyphs written above the
20098 string where the `%x' came from, position
20099 of the `%'. */
20100 if (nwritten > 0)
20101 {
20102 struct glyph *glyph
20103 = (it->glyph_row->glyphs[TEXT_AREA]
20104 + nglyphs_before);
20105 int i;
20106
20107 for (i = 0; i < nwritten; ++i)
20108 {
20109 glyph[i].object = elt;
20110 glyph[i].charpos = charpos;
20111 }
20112
20113 n += nwritten;
20114 }
20115 }
20116 break;
20117 }
20118 }
20119 else /* c == 0 */
20120 break;
20121 }
20122 }
20123 }
20124 break;
20125
20126 case Lisp_Symbol:
20127 /* A symbol: process the value of the symbol recursively
20128 as if it appeared here directly. Avoid error if symbol void.
20129 Special case: if value of symbol is a string, output the string
20130 literally. */
20131 {
20132 register Lisp_Object tem;
20133
20134 /* If the variable is not marked as risky to set
20135 then its contents are risky to use. */
20136 if (NILP (Fget (elt, Qrisky_local_variable)))
20137 risky = 1;
20138
20139 tem = Fboundp (elt);
20140 if (!NILP (tem))
20141 {
20142 tem = Fsymbol_value (elt);
20143 /* If value is a string, output that string literally:
20144 don't check for % within it. */
20145 if (STRINGP (tem))
20146 literal = 1;
20147
20148 if (!EQ (tem, elt))
20149 {
20150 /* Give up right away for nil or t. */
20151 elt = tem;
20152 goto tail_recurse;
20153 }
20154 }
20155 }
20156 break;
20157
20158 case Lisp_Cons:
20159 {
20160 register Lisp_Object car, tem;
20161
20162 /* A cons cell: five distinct cases.
20163 If first element is :eval or :propertize, do something special.
20164 If first element is a string or a cons, process all the elements
20165 and effectively concatenate them.
20166 If first element is a negative number, truncate displaying cdr to
20167 at most that many characters. If positive, pad (with spaces)
20168 to at least that many characters.
20169 If first element is a symbol, process the cadr or caddr recursively
20170 according to whether the symbol's value is non-nil or nil. */
20171 car = XCAR (elt);
20172 if (EQ (car, QCeval))
20173 {
20174 /* An element of the form (:eval FORM) means evaluate FORM
20175 and use the result as mode line elements. */
20176
20177 if (risky)
20178 break;
20179
20180 if (CONSP (XCDR (elt)))
20181 {
20182 Lisp_Object spec;
20183 spec = safe_eval (XCAR (XCDR (elt)));
20184 n += display_mode_element (it, depth, field_width - n,
20185 precision - n, spec, props,
20186 risky);
20187 }
20188 }
20189 else if (EQ (car, QCpropertize))
20190 {
20191 /* An element of the form (:propertize ELT PROPS...)
20192 means display ELT but applying properties PROPS. */
20193
20194 if (risky)
20195 break;
20196
20197 if (CONSP (XCDR (elt)))
20198 n += display_mode_element (it, depth, field_width - n,
20199 precision - n, XCAR (XCDR (elt)),
20200 XCDR (XCDR (elt)), risky);
20201 }
20202 else if (SYMBOLP (car))
20203 {
20204 tem = Fboundp (car);
20205 elt = XCDR (elt);
20206 if (!CONSP (elt))
20207 goto invalid;
20208 /* elt is now the cdr, and we know it is a cons cell.
20209 Use its car if CAR has a non-nil value. */
20210 if (!NILP (tem))
20211 {
20212 tem = Fsymbol_value (car);
20213 if (!NILP (tem))
20214 {
20215 elt = XCAR (elt);
20216 goto tail_recurse;
20217 }
20218 }
20219 /* Symbol's value is nil (or symbol is unbound)
20220 Get the cddr of the original list
20221 and if possible find the caddr and use that. */
20222 elt = XCDR (elt);
20223 if (NILP (elt))
20224 break;
20225 else if (!CONSP (elt))
20226 goto invalid;
20227 elt = XCAR (elt);
20228 goto tail_recurse;
20229 }
20230 else if (INTEGERP (car))
20231 {
20232 register int lim = XINT (car);
20233 elt = XCDR (elt);
20234 if (lim < 0)
20235 {
20236 /* Negative int means reduce maximum width. */
20237 if (precision <= 0)
20238 precision = -lim;
20239 else
20240 precision = min (precision, -lim);
20241 }
20242 else if (lim > 0)
20243 {
20244 /* Padding specified. Don't let it be more than
20245 current maximum. */
20246 if (precision > 0)
20247 lim = min (precision, lim);
20248
20249 /* If that's more padding than already wanted, queue it.
20250 But don't reduce padding already specified even if
20251 that is beyond the current truncation point. */
20252 field_width = max (lim, field_width);
20253 }
20254 goto tail_recurse;
20255 }
20256 else if (STRINGP (car) || CONSP (car))
20257 {
20258 Lisp_Object halftail = elt;
20259 int len = 0;
20260
20261 while (CONSP (elt)
20262 && (precision <= 0 || n < precision))
20263 {
20264 n += display_mode_element (it, depth,
20265 /* Do padding only after the last
20266 element in the list. */
20267 (! CONSP (XCDR (elt))
20268 ? field_width - n
20269 : 0),
20270 precision - n, XCAR (elt),
20271 props, risky);
20272 elt = XCDR (elt);
20273 len++;
20274 if ((len & 1) == 0)
20275 halftail = XCDR (halftail);
20276 /* Check for cycle. */
20277 if (EQ (halftail, elt))
20278 break;
20279 }
20280 }
20281 }
20282 break;
20283
20284 default:
20285 invalid:
20286 elt = build_string ("*invalid*");
20287 goto tail_recurse;
20288 }
20289
20290 /* Pad to FIELD_WIDTH. */
20291 if (field_width > 0 && n < field_width)
20292 {
20293 switch (mode_line_target)
20294 {
20295 case MODE_LINE_NOPROP:
20296 case MODE_LINE_TITLE:
20297 n += store_mode_line_noprop ("", field_width - n, 0);
20298 break;
20299 case MODE_LINE_STRING:
20300 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20301 break;
20302 case MODE_LINE_DISPLAY:
20303 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20304 0, 0, 0);
20305 break;
20306 }
20307 }
20308
20309 return n;
20310 }
20311
20312 /* Store a mode-line string element in mode_line_string_list.
20313
20314 If STRING is non-null, display that C string. Otherwise, the Lisp
20315 string LISP_STRING is displayed.
20316
20317 FIELD_WIDTH is the minimum number of output glyphs to produce.
20318 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20319 with spaces. FIELD_WIDTH <= 0 means don't pad.
20320
20321 PRECISION is the maximum number of characters to output from
20322 STRING. PRECISION <= 0 means don't truncate the string.
20323
20324 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20325 properties to the string.
20326
20327 PROPS are the properties to add to the string.
20328 The mode_line_string_face face property is always added to the string.
20329 */
20330
20331 static int
20332 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20333 int field_width, int precision, Lisp_Object props)
20334 {
20335 ptrdiff_t len;
20336 int n = 0;
20337
20338 if (string != NULL)
20339 {
20340 len = strlen (string);
20341 if (precision > 0 && len > precision)
20342 len = precision;
20343 lisp_string = make_string (string, len);
20344 if (NILP (props))
20345 props = mode_line_string_face_prop;
20346 else if (!NILP (mode_line_string_face))
20347 {
20348 Lisp_Object face = Fplist_get (props, Qface);
20349 props = Fcopy_sequence (props);
20350 if (NILP (face))
20351 face = mode_line_string_face;
20352 else
20353 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20354 props = Fplist_put (props, Qface, face);
20355 }
20356 Fadd_text_properties (make_number (0), make_number (len),
20357 props, lisp_string);
20358 }
20359 else
20360 {
20361 len = XFASTINT (Flength (lisp_string));
20362 if (precision > 0 && len > precision)
20363 {
20364 len = precision;
20365 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20366 precision = -1;
20367 }
20368 if (!NILP (mode_line_string_face))
20369 {
20370 Lisp_Object face;
20371 if (NILP (props))
20372 props = Ftext_properties_at (make_number (0), lisp_string);
20373 face = Fplist_get (props, Qface);
20374 if (NILP (face))
20375 face = mode_line_string_face;
20376 else
20377 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20378 props = Fcons (Qface, Fcons (face, Qnil));
20379 if (copy_string)
20380 lisp_string = Fcopy_sequence (lisp_string);
20381 }
20382 if (!NILP (props))
20383 Fadd_text_properties (make_number (0), make_number (len),
20384 props, lisp_string);
20385 }
20386
20387 if (len > 0)
20388 {
20389 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20390 n += len;
20391 }
20392
20393 if (field_width > len)
20394 {
20395 field_width -= len;
20396 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20397 if (!NILP (props))
20398 Fadd_text_properties (make_number (0), make_number (field_width),
20399 props, lisp_string);
20400 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20401 n += field_width;
20402 }
20403
20404 return n;
20405 }
20406
20407
20408 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20409 1, 4, 0,
20410 doc: /* Format a string out of a mode line format specification.
20411 First arg FORMAT specifies the mode line format (see `mode-line-format'
20412 for details) to use.
20413
20414 By default, the format is evaluated for the currently selected window.
20415
20416 Optional second arg FACE specifies the face property to put on all
20417 characters for which no face is specified. The value nil means the
20418 default face. The value t means whatever face the window's mode line
20419 currently uses (either `mode-line' or `mode-line-inactive',
20420 depending on whether the window is the selected window or not).
20421 An integer value means the value string has no text
20422 properties.
20423
20424 Optional third and fourth args WINDOW and BUFFER specify the window
20425 and buffer to use as the context for the formatting (defaults
20426 are the selected window and the WINDOW's buffer). */)
20427 (Lisp_Object format, Lisp_Object face,
20428 Lisp_Object window, Lisp_Object buffer)
20429 {
20430 struct it it;
20431 int len;
20432 struct window *w;
20433 struct buffer *old_buffer = NULL;
20434 int face_id;
20435 int no_props = INTEGERP (face);
20436 ptrdiff_t count = SPECPDL_INDEX ();
20437 Lisp_Object str;
20438 int string_start = 0;
20439
20440 if (NILP (window))
20441 window = selected_window;
20442 CHECK_WINDOW (window);
20443 w = XWINDOW (window);
20444
20445 if (NILP (buffer))
20446 buffer = w->buffer;
20447 CHECK_BUFFER (buffer);
20448
20449 /* Make formatting the modeline a non-op when noninteractive, otherwise
20450 there will be problems later caused by a partially initialized frame. */
20451 if (NILP (format) || noninteractive)
20452 return empty_unibyte_string;
20453
20454 if (no_props)
20455 face = Qnil;
20456
20457 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20458 : EQ (face, Qt) ? (EQ (window, selected_window)
20459 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20460 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20461 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20462 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20463 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20464 : DEFAULT_FACE_ID;
20465
20466 if (XBUFFER (buffer) != current_buffer)
20467 old_buffer = current_buffer;
20468
20469 /* Save things including mode_line_proptrans_alist,
20470 and set that to nil so that we don't alter the outer value. */
20471 record_unwind_protect (unwind_format_mode_line,
20472 format_mode_line_unwind_data
20473 (old_buffer, selected_window, 1));
20474 mode_line_proptrans_alist = Qnil;
20475
20476 Fselect_window (window, Qt);
20477 if (old_buffer)
20478 set_buffer_internal_1 (XBUFFER (buffer));
20479
20480 init_iterator (&it, w, -1, -1, NULL, face_id);
20481
20482 if (no_props)
20483 {
20484 mode_line_target = MODE_LINE_NOPROP;
20485 mode_line_string_face_prop = Qnil;
20486 mode_line_string_list = Qnil;
20487 string_start = MODE_LINE_NOPROP_LEN (0);
20488 }
20489 else
20490 {
20491 mode_line_target = MODE_LINE_STRING;
20492 mode_line_string_list = Qnil;
20493 mode_line_string_face = face;
20494 mode_line_string_face_prop
20495 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20496 }
20497
20498 push_kboard (FRAME_KBOARD (it.f));
20499 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20500 pop_kboard ();
20501
20502 if (no_props)
20503 {
20504 len = MODE_LINE_NOPROP_LEN (string_start);
20505 str = make_string (mode_line_noprop_buf + string_start, len);
20506 }
20507 else
20508 {
20509 mode_line_string_list = Fnreverse (mode_line_string_list);
20510 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20511 empty_unibyte_string);
20512 }
20513
20514 unbind_to (count, Qnil);
20515 return str;
20516 }
20517
20518 /* Write a null-terminated, right justified decimal representation of
20519 the positive integer D to BUF using a minimal field width WIDTH. */
20520
20521 static void
20522 pint2str (register char *buf, register int width, register ptrdiff_t d)
20523 {
20524 register char *p = buf;
20525
20526 if (d <= 0)
20527 *p++ = '0';
20528 else
20529 {
20530 while (d > 0)
20531 {
20532 *p++ = d % 10 + '0';
20533 d /= 10;
20534 }
20535 }
20536
20537 for (width -= (int) (p - buf); width > 0; --width)
20538 *p++ = ' ';
20539 *p-- = '\0';
20540 while (p > buf)
20541 {
20542 d = *buf;
20543 *buf++ = *p;
20544 *p-- = d;
20545 }
20546 }
20547
20548 /* Write a null-terminated, right justified decimal and "human
20549 readable" representation of the nonnegative integer D to BUF using
20550 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20551
20552 static const char power_letter[] =
20553 {
20554 0, /* no letter */
20555 'k', /* kilo */
20556 'M', /* mega */
20557 'G', /* giga */
20558 'T', /* tera */
20559 'P', /* peta */
20560 'E', /* exa */
20561 'Z', /* zetta */
20562 'Y' /* yotta */
20563 };
20564
20565 static void
20566 pint2hrstr (char *buf, int width, ptrdiff_t d)
20567 {
20568 /* We aim to represent the nonnegative integer D as
20569 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20570 ptrdiff_t quotient = d;
20571 int remainder = 0;
20572 /* -1 means: do not use TENTHS. */
20573 int tenths = -1;
20574 int exponent = 0;
20575
20576 /* Length of QUOTIENT.TENTHS as a string. */
20577 int length;
20578
20579 char * psuffix;
20580 char * p;
20581
20582 if (1000 <= quotient)
20583 {
20584 /* Scale to the appropriate EXPONENT. */
20585 do
20586 {
20587 remainder = quotient % 1000;
20588 quotient /= 1000;
20589 exponent++;
20590 }
20591 while (1000 <= quotient);
20592
20593 /* Round to nearest and decide whether to use TENTHS or not. */
20594 if (quotient <= 9)
20595 {
20596 tenths = remainder / 100;
20597 if (50 <= remainder % 100)
20598 {
20599 if (tenths < 9)
20600 tenths++;
20601 else
20602 {
20603 quotient++;
20604 if (quotient == 10)
20605 tenths = -1;
20606 else
20607 tenths = 0;
20608 }
20609 }
20610 }
20611 else
20612 if (500 <= remainder)
20613 {
20614 if (quotient < 999)
20615 quotient++;
20616 else
20617 {
20618 quotient = 1;
20619 exponent++;
20620 tenths = 0;
20621 }
20622 }
20623 }
20624
20625 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20626 if (tenths == -1 && quotient <= 99)
20627 if (quotient <= 9)
20628 length = 1;
20629 else
20630 length = 2;
20631 else
20632 length = 3;
20633 p = psuffix = buf + max (width, length);
20634
20635 /* Print EXPONENT. */
20636 *psuffix++ = power_letter[exponent];
20637 *psuffix = '\0';
20638
20639 /* Print TENTHS. */
20640 if (tenths >= 0)
20641 {
20642 *--p = '0' + tenths;
20643 *--p = '.';
20644 }
20645
20646 /* Print QUOTIENT. */
20647 do
20648 {
20649 int digit = quotient % 10;
20650 *--p = '0' + digit;
20651 }
20652 while ((quotient /= 10) != 0);
20653
20654 /* Print leading spaces. */
20655 while (buf < p)
20656 *--p = ' ';
20657 }
20658
20659 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20660 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20661 type of CODING_SYSTEM. Return updated pointer into BUF. */
20662
20663 static unsigned char invalid_eol_type[] = "(*invalid*)";
20664
20665 static char *
20666 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20667 {
20668 Lisp_Object val;
20669 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20670 const unsigned char *eol_str;
20671 int eol_str_len;
20672 /* The EOL conversion we are using. */
20673 Lisp_Object eoltype;
20674
20675 val = CODING_SYSTEM_SPEC (coding_system);
20676 eoltype = Qnil;
20677
20678 if (!VECTORP (val)) /* Not yet decided. */
20679 {
20680 if (multibyte)
20681 *buf++ = '-';
20682 if (eol_flag)
20683 eoltype = eol_mnemonic_undecided;
20684 /* Don't mention EOL conversion if it isn't decided. */
20685 }
20686 else
20687 {
20688 Lisp_Object attrs;
20689 Lisp_Object eolvalue;
20690
20691 attrs = AREF (val, 0);
20692 eolvalue = AREF (val, 2);
20693
20694 if (multibyte)
20695 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20696
20697 if (eol_flag)
20698 {
20699 /* The EOL conversion that is normal on this system. */
20700
20701 if (NILP (eolvalue)) /* Not yet decided. */
20702 eoltype = eol_mnemonic_undecided;
20703 else if (VECTORP (eolvalue)) /* Not yet decided. */
20704 eoltype = eol_mnemonic_undecided;
20705 else /* eolvalue is Qunix, Qdos, or Qmac. */
20706 eoltype = (EQ (eolvalue, Qunix)
20707 ? eol_mnemonic_unix
20708 : (EQ (eolvalue, Qdos) == 1
20709 ? eol_mnemonic_dos : eol_mnemonic_mac));
20710 }
20711 }
20712
20713 if (eol_flag)
20714 {
20715 /* Mention the EOL conversion if it is not the usual one. */
20716 if (STRINGP (eoltype))
20717 {
20718 eol_str = SDATA (eoltype);
20719 eol_str_len = SBYTES (eoltype);
20720 }
20721 else if (CHARACTERP (eoltype))
20722 {
20723 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20724 int c = XFASTINT (eoltype);
20725 eol_str_len = CHAR_STRING (c, tmp);
20726 eol_str = tmp;
20727 }
20728 else
20729 {
20730 eol_str = invalid_eol_type;
20731 eol_str_len = sizeof (invalid_eol_type) - 1;
20732 }
20733 memcpy (buf, eol_str, eol_str_len);
20734 buf += eol_str_len;
20735 }
20736
20737 return buf;
20738 }
20739
20740 /* Return a string for the output of a mode line %-spec for window W,
20741 generated by character C. FIELD_WIDTH > 0 means pad the string
20742 returned with spaces to that value. Return a Lisp string in
20743 *STRING if the resulting string is taken from that Lisp string.
20744
20745 Note we operate on the current buffer for most purposes,
20746 the exception being w->base_line_pos. */
20747
20748 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20749
20750 static const char *
20751 decode_mode_spec (struct window *w, register int c, int field_width,
20752 Lisp_Object *string)
20753 {
20754 Lisp_Object obj;
20755 struct frame *f = XFRAME (WINDOW_FRAME (w));
20756 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20757 struct buffer *b = current_buffer;
20758
20759 obj = Qnil;
20760 *string = Qnil;
20761
20762 switch (c)
20763 {
20764 case '*':
20765 if (!NILP (BVAR (b, read_only)))
20766 return "%";
20767 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20768 return "*";
20769 return "-";
20770
20771 case '+':
20772 /* This differs from %* only for a modified read-only buffer. */
20773 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20774 return "*";
20775 if (!NILP (BVAR (b, read_only)))
20776 return "%";
20777 return "-";
20778
20779 case '&':
20780 /* This differs from %* in ignoring read-only-ness. */
20781 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20782 return "*";
20783 return "-";
20784
20785 case '%':
20786 return "%";
20787
20788 case '[':
20789 {
20790 int i;
20791 char *p;
20792
20793 if (command_loop_level > 5)
20794 return "[[[... ";
20795 p = decode_mode_spec_buf;
20796 for (i = 0; i < command_loop_level; i++)
20797 *p++ = '[';
20798 *p = 0;
20799 return decode_mode_spec_buf;
20800 }
20801
20802 case ']':
20803 {
20804 int i;
20805 char *p;
20806
20807 if (command_loop_level > 5)
20808 return " ...]]]";
20809 p = decode_mode_spec_buf;
20810 for (i = 0; i < command_loop_level; i++)
20811 *p++ = ']';
20812 *p = 0;
20813 return decode_mode_spec_buf;
20814 }
20815
20816 case '-':
20817 {
20818 register int i;
20819
20820 /* Let lots_of_dashes be a string of infinite length. */
20821 if (mode_line_target == MODE_LINE_NOPROP ||
20822 mode_line_target == MODE_LINE_STRING)
20823 return "--";
20824 if (field_width <= 0
20825 || field_width > sizeof (lots_of_dashes))
20826 {
20827 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20828 decode_mode_spec_buf[i] = '-';
20829 decode_mode_spec_buf[i] = '\0';
20830 return decode_mode_spec_buf;
20831 }
20832 else
20833 return lots_of_dashes;
20834 }
20835
20836 case 'b':
20837 obj = BVAR (b, name);
20838 break;
20839
20840 case 'c':
20841 /* %c and %l are ignored in `frame-title-format'.
20842 (In redisplay_internal, the frame title is drawn _before_ the
20843 windows are updated, so the stuff which depends on actual
20844 window contents (such as %l) may fail to render properly, or
20845 even crash emacs.) */
20846 if (mode_line_target == MODE_LINE_TITLE)
20847 return "";
20848 else
20849 {
20850 ptrdiff_t col = current_column ();
20851 w->column_number_displayed = make_number (col);
20852 pint2str (decode_mode_spec_buf, field_width, col);
20853 return decode_mode_spec_buf;
20854 }
20855
20856 case 'e':
20857 #ifndef SYSTEM_MALLOC
20858 {
20859 if (NILP (Vmemory_full))
20860 return "";
20861 else
20862 return "!MEM FULL! ";
20863 }
20864 #else
20865 return "";
20866 #endif
20867
20868 case 'F':
20869 /* %F displays the frame name. */
20870 if (!NILP (f->title))
20871 return SSDATA (f->title);
20872 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20873 return SSDATA (f->name);
20874 return "Emacs";
20875
20876 case 'f':
20877 obj = BVAR (b, filename);
20878 break;
20879
20880 case 'i':
20881 {
20882 ptrdiff_t size = ZV - BEGV;
20883 pint2str (decode_mode_spec_buf, field_width, size);
20884 return decode_mode_spec_buf;
20885 }
20886
20887 case 'I':
20888 {
20889 ptrdiff_t size = ZV - BEGV;
20890 pint2hrstr (decode_mode_spec_buf, field_width, size);
20891 return decode_mode_spec_buf;
20892 }
20893
20894 case 'l':
20895 {
20896 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
20897 ptrdiff_t topline, nlines, height;
20898 ptrdiff_t junk;
20899
20900 /* %c and %l are ignored in `frame-title-format'. */
20901 if (mode_line_target == MODE_LINE_TITLE)
20902 return "";
20903
20904 startpos = XMARKER (w->start)->charpos;
20905 startpos_byte = marker_byte_position (w->start);
20906 height = WINDOW_TOTAL_LINES (w);
20907
20908 /* If we decided that this buffer isn't suitable for line numbers,
20909 don't forget that too fast. */
20910 if (EQ (w->base_line_pos, w->buffer))
20911 goto no_value;
20912 /* But do forget it, if the window shows a different buffer now. */
20913 else if (BUFFERP (w->base_line_pos))
20914 w->base_line_pos = Qnil;
20915
20916 /* If the buffer is very big, don't waste time. */
20917 if (INTEGERP (Vline_number_display_limit)
20918 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20919 {
20920 w->base_line_pos = Qnil;
20921 w->base_line_number = Qnil;
20922 goto no_value;
20923 }
20924
20925 if (INTEGERP (w->base_line_number)
20926 && INTEGERP (w->base_line_pos)
20927 && XFASTINT (w->base_line_pos) <= startpos)
20928 {
20929 line = XFASTINT (w->base_line_number);
20930 linepos = XFASTINT (w->base_line_pos);
20931 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20932 }
20933 else
20934 {
20935 line = 1;
20936 linepos = BUF_BEGV (b);
20937 linepos_byte = BUF_BEGV_BYTE (b);
20938 }
20939
20940 /* Count lines from base line to window start position. */
20941 nlines = display_count_lines (linepos_byte,
20942 startpos_byte,
20943 startpos, &junk);
20944
20945 topline = nlines + line;
20946
20947 /* Determine a new base line, if the old one is too close
20948 or too far away, or if we did not have one.
20949 "Too close" means it's plausible a scroll-down would
20950 go back past it. */
20951 if (startpos == BUF_BEGV (b))
20952 {
20953 w->base_line_number = make_number (topline);
20954 w->base_line_pos = make_number (BUF_BEGV (b));
20955 }
20956 else if (nlines < height + 25 || nlines > height * 3 + 50
20957 || linepos == BUF_BEGV (b))
20958 {
20959 ptrdiff_t limit = BUF_BEGV (b);
20960 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
20961 ptrdiff_t position;
20962 ptrdiff_t distance =
20963 (height * 2 + 30) * line_number_display_limit_width;
20964
20965 if (startpos - distance > limit)
20966 {
20967 limit = startpos - distance;
20968 limit_byte = CHAR_TO_BYTE (limit);
20969 }
20970
20971 nlines = display_count_lines (startpos_byte,
20972 limit_byte,
20973 - (height * 2 + 30),
20974 &position);
20975 /* If we couldn't find the lines we wanted within
20976 line_number_display_limit_width chars per line,
20977 give up on line numbers for this window. */
20978 if (position == limit_byte && limit == startpos - distance)
20979 {
20980 w->base_line_pos = w->buffer;
20981 w->base_line_number = Qnil;
20982 goto no_value;
20983 }
20984
20985 w->base_line_number = make_number (topline - nlines);
20986 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20987 }
20988
20989 /* Now count lines from the start pos to point. */
20990 nlines = display_count_lines (startpos_byte,
20991 PT_BYTE, PT, &junk);
20992
20993 /* Record that we did display the line number. */
20994 line_number_displayed = 1;
20995
20996 /* Make the string to show. */
20997 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20998 return decode_mode_spec_buf;
20999 no_value:
21000 {
21001 char* p = decode_mode_spec_buf;
21002 int pad = field_width - 2;
21003 while (pad-- > 0)
21004 *p++ = ' ';
21005 *p++ = '?';
21006 *p++ = '?';
21007 *p = '\0';
21008 return decode_mode_spec_buf;
21009 }
21010 }
21011 break;
21012
21013 case 'm':
21014 obj = BVAR (b, mode_name);
21015 break;
21016
21017 case 'n':
21018 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21019 return " Narrow";
21020 break;
21021
21022 case 'p':
21023 {
21024 ptrdiff_t pos = marker_position (w->start);
21025 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21026
21027 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21028 {
21029 if (pos <= BUF_BEGV (b))
21030 return "All";
21031 else
21032 return "Bottom";
21033 }
21034 else if (pos <= BUF_BEGV (b))
21035 return "Top";
21036 else
21037 {
21038 if (total > 1000000)
21039 /* Do it differently for a large value, to avoid overflow. */
21040 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21041 else
21042 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21043 /* We can't normally display a 3-digit number,
21044 so get us a 2-digit number that is close. */
21045 if (total == 100)
21046 total = 99;
21047 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21048 return decode_mode_spec_buf;
21049 }
21050 }
21051
21052 /* Display percentage of size above the bottom of the screen. */
21053 case 'P':
21054 {
21055 ptrdiff_t toppos = marker_position (w->start);
21056 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21057 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21058
21059 if (botpos >= BUF_ZV (b))
21060 {
21061 if (toppos <= BUF_BEGV (b))
21062 return "All";
21063 else
21064 return "Bottom";
21065 }
21066 else
21067 {
21068 if (total > 1000000)
21069 /* Do it differently for a large value, to avoid overflow. */
21070 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21071 else
21072 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21073 /* We can't normally display a 3-digit number,
21074 so get us a 2-digit number that is close. */
21075 if (total == 100)
21076 total = 99;
21077 if (toppos <= BUF_BEGV (b))
21078 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21079 else
21080 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21081 return decode_mode_spec_buf;
21082 }
21083 }
21084
21085 case 's':
21086 /* status of process */
21087 obj = Fget_buffer_process (Fcurrent_buffer ());
21088 if (NILP (obj))
21089 return "no process";
21090 #ifndef MSDOS
21091 obj = Fsymbol_name (Fprocess_status (obj));
21092 #endif
21093 break;
21094
21095 case '@':
21096 {
21097 ptrdiff_t count = inhibit_garbage_collection ();
21098 Lisp_Object val = call1 (intern ("file-remote-p"),
21099 BVAR (current_buffer, directory));
21100 unbind_to (count, Qnil);
21101
21102 if (NILP (val))
21103 return "-";
21104 else
21105 return "@";
21106 }
21107
21108 case 't': /* indicate TEXT or BINARY */
21109 return "T";
21110
21111 case 'z':
21112 /* coding-system (not including end-of-line format) */
21113 case 'Z':
21114 /* coding-system (including end-of-line type) */
21115 {
21116 int eol_flag = (c == 'Z');
21117 char *p = decode_mode_spec_buf;
21118
21119 if (! FRAME_WINDOW_P (f))
21120 {
21121 /* No need to mention EOL here--the terminal never needs
21122 to do EOL conversion. */
21123 p = decode_mode_spec_coding (CODING_ID_NAME
21124 (FRAME_KEYBOARD_CODING (f)->id),
21125 p, 0);
21126 p = decode_mode_spec_coding (CODING_ID_NAME
21127 (FRAME_TERMINAL_CODING (f)->id),
21128 p, 0);
21129 }
21130 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21131 p, eol_flag);
21132
21133 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21134 #ifdef subprocesses
21135 obj = Fget_buffer_process (Fcurrent_buffer ());
21136 if (PROCESSP (obj))
21137 {
21138 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21139 p, eol_flag);
21140 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21141 p, eol_flag);
21142 }
21143 #endif /* subprocesses */
21144 #endif /* 0 */
21145 *p = 0;
21146 return decode_mode_spec_buf;
21147 }
21148 }
21149
21150 if (STRINGP (obj))
21151 {
21152 *string = obj;
21153 return SSDATA (obj);
21154 }
21155 else
21156 return "";
21157 }
21158
21159
21160 /* Count up to COUNT lines starting from START_BYTE.
21161 But don't go beyond LIMIT_BYTE.
21162 Return the number of lines thus found (always nonnegative).
21163
21164 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21165
21166 static ptrdiff_t
21167 display_count_lines (ptrdiff_t start_byte,
21168 ptrdiff_t limit_byte, ptrdiff_t count,
21169 ptrdiff_t *byte_pos_ptr)
21170 {
21171 register unsigned char *cursor;
21172 unsigned char *base;
21173
21174 register ptrdiff_t ceiling;
21175 register unsigned char *ceiling_addr;
21176 ptrdiff_t orig_count = count;
21177
21178 /* If we are not in selective display mode,
21179 check only for newlines. */
21180 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21181 && !INTEGERP (BVAR (current_buffer, selective_display)));
21182
21183 if (count > 0)
21184 {
21185 while (start_byte < limit_byte)
21186 {
21187 ceiling = BUFFER_CEILING_OF (start_byte);
21188 ceiling = min (limit_byte - 1, ceiling);
21189 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21190 base = (cursor = BYTE_POS_ADDR (start_byte));
21191 while (1)
21192 {
21193 if (selective_display)
21194 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21195 ;
21196 else
21197 while (*cursor != '\n' && ++cursor != ceiling_addr)
21198 ;
21199
21200 if (cursor != ceiling_addr)
21201 {
21202 if (--count == 0)
21203 {
21204 start_byte += cursor - base + 1;
21205 *byte_pos_ptr = start_byte;
21206 return orig_count;
21207 }
21208 else
21209 if (++cursor == ceiling_addr)
21210 break;
21211 }
21212 else
21213 break;
21214 }
21215 start_byte += cursor - base;
21216 }
21217 }
21218 else
21219 {
21220 while (start_byte > limit_byte)
21221 {
21222 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21223 ceiling = max (limit_byte, ceiling);
21224 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21225 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21226 while (1)
21227 {
21228 if (selective_display)
21229 while (--cursor != ceiling_addr
21230 && *cursor != '\n' && *cursor != 015)
21231 ;
21232 else
21233 while (--cursor != ceiling_addr && *cursor != '\n')
21234 ;
21235
21236 if (cursor != ceiling_addr)
21237 {
21238 if (++count == 0)
21239 {
21240 start_byte += cursor - base + 1;
21241 *byte_pos_ptr = start_byte;
21242 /* When scanning backwards, we should
21243 not count the newline posterior to which we stop. */
21244 return - orig_count - 1;
21245 }
21246 }
21247 else
21248 break;
21249 }
21250 /* Here we add 1 to compensate for the last decrement
21251 of CURSOR, which took it past the valid range. */
21252 start_byte += cursor - base + 1;
21253 }
21254 }
21255
21256 *byte_pos_ptr = limit_byte;
21257
21258 if (count < 0)
21259 return - orig_count + count;
21260 return orig_count - count;
21261
21262 }
21263
21264
21265 \f
21266 /***********************************************************************
21267 Displaying strings
21268 ***********************************************************************/
21269
21270 /* Display a NUL-terminated string, starting with index START.
21271
21272 If STRING is non-null, display that C string. Otherwise, the Lisp
21273 string LISP_STRING is displayed. There's a case that STRING is
21274 non-null and LISP_STRING is not nil. It means STRING is a string
21275 data of LISP_STRING. In that case, we display LISP_STRING while
21276 ignoring its text properties.
21277
21278 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21279 FACE_STRING. Display STRING or LISP_STRING with the face at
21280 FACE_STRING_POS in FACE_STRING:
21281
21282 Display the string in the environment given by IT, but use the
21283 standard display table, temporarily.
21284
21285 FIELD_WIDTH is the minimum number of output glyphs to produce.
21286 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21287 with spaces. If STRING has more characters, more than FIELD_WIDTH
21288 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21289
21290 PRECISION is the maximum number of characters to output from
21291 STRING. PRECISION < 0 means don't truncate the string.
21292
21293 This is roughly equivalent to printf format specifiers:
21294
21295 FIELD_WIDTH PRECISION PRINTF
21296 ----------------------------------------
21297 -1 -1 %s
21298 -1 10 %.10s
21299 10 -1 %10s
21300 20 10 %20.10s
21301
21302 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21303 display them, and < 0 means obey the current buffer's value of
21304 enable_multibyte_characters.
21305
21306 Value is the number of columns displayed. */
21307
21308 static int
21309 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21310 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21311 int field_width, int precision, int max_x, int multibyte)
21312 {
21313 int hpos_at_start = it->hpos;
21314 int saved_face_id = it->face_id;
21315 struct glyph_row *row = it->glyph_row;
21316 ptrdiff_t it_charpos;
21317
21318 /* Initialize the iterator IT for iteration over STRING beginning
21319 with index START. */
21320 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21321 precision, field_width, multibyte);
21322 if (string && STRINGP (lisp_string))
21323 /* LISP_STRING is the one returned by decode_mode_spec. We should
21324 ignore its text properties. */
21325 it->stop_charpos = it->end_charpos;
21326
21327 /* If displaying STRING, set up the face of the iterator from
21328 FACE_STRING, if that's given. */
21329 if (STRINGP (face_string))
21330 {
21331 ptrdiff_t endptr;
21332 struct face *face;
21333
21334 it->face_id
21335 = face_at_string_position (it->w, face_string, face_string_pos,
21336 0, it->region_beg_charpos,
21337 it->region_end_charpos,
21338 &endptr, it->base_face_id, 0);
21339 face = FACE_FROM_ID (it->f, it->face_id);
21340 it->face_box_p = face->box != FACE_NO_BOX;
21341 }
21342
21343 /* Set max_x to the maximum allowed X position. Don't let it go
21344 beyond the right edge of the window. */
21345 if (max_x <= 0)
21346 max_x = it->last_visible_x;
21347 else
21348 max_x = min (max_x, it->last_visible_x);
21349
21350 /* Skip over display elements that are not visible. because IT->w is
21351 hscrolled. */
21352 if (it->current_x < it->first_visible_x)
21353 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21354 MOVE_TO_POS | MOVE_TO_X);
21355
21356 row->ascent = it->max_ascent;
21357 row->height = it->max_ascent + it->max_descent;
21358 row->phys_ascent = it->max_phys_ascent;
21359 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21360 row->extra_line_spacing = it->max_extra_line_spacing;
21361
21362 if (STRINGP (it->string))
21363 it_charpos = IT_STRING_CHARPOS (*it);
21364 else
21365 it_charpos = IT_CHARPOS (*it);
21366
21367 /* This condition is for the case that we are called with current_x
21368 past last_visible_x. */
21369 while (it->current_x < max_x)
21370 {
21371 int x_before, x, n_glyphs_before, i, nglyphs;
21372
21373 /* Get the next display element. */
21374 if (!get_next_display_element (it))
21375 break;
21376
21377 /* Produce glyphs. */
21378 x_before = it->current_x;
21379 n_glyphs_before = row->used[TEXT_AREA];
21380 PRODUCE_GLYPHS (it);
21381
21382 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21383 i = 0;
21384 x = x_before;
21385 while (i < nglyphs)
21386 {
21387 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21388
21389 if (it->line_wrap != TRUNCATE
21390 && x + glyph->pixel_width > max_x)
21391 {
21392 /* End of continued line or max_x reached. */
21393 if (CHAR_GLYPH_PADDING_P (*glyph))
21394 {
21395 /* A wide character is unbreakable. */
21396 if (row->reversed_p)
21397 unproduce_glyphs (it, row->used[TEXT_AREA]
21398 - n_glyphs_before);
21399 row->used[TEXT_AREA] = n_glyphs_before;
21400 it->current_x = x_before;
21401 }
21402 else
21403 {
21404 if (row->reversed_p)
21405 unproduce_glyphs (it, row->used[TEXT_AREA]
21406 - (n_glyphs_before + i));
21407 row->used[TEXT_AREA] = n_glyphs_before + i;
21408 it->current_x = x;
21409 }
21410 break;
21411 }
21412 else if (x + glyph->pixel_width >= it->first_visible_x)
21413 {
21414 /* Glyph is at least partially visible. */
21415 ++it->hpos;
21416 if (x < it->first_visible_x)
21417 row->x = x - it->first_visible_x;
21418 }
21419 else
21420 {
21421 /* Glyph is off the left margin of the display area.
21422 Should not happen. */
21423 abort ();
21424 }
21425
21426 row->ascent = max (row->ascent, it->max_ascent);
21427 row->height = max (row->height, it->max_ascent + it->max_descent);
21428 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21429 row->phys_height = max (row->phys_height,
21430 it->max_phys_ascent + it->max_phys_descent);
21431 row->extra_line_spacing = max (row->extra_line_spacing,
21432 it->max_extra_line_spacing);
21433 x += glyph->pixel_width;
21434 ++i;
21435 }
21436
21437 /* Stop if max_x reached. */
21438 if (i < nglyphs)
21439 break;
21440
21441 /* Stop at line ends. */
21442 if (ITERATOR_AT_END_OF_LINE_P (it))
21443 {
21444 it->continuation_lines_width = 0;
21445 break;
21446 }
21447
21448 set_iterator_to_next (it, 1);
21449 if (STRINGP (it->string))
21450 it_charpos = IT_STRING_CHARPOS (*it);
21451 else
21452 it_charpos = IT_CHARPOS (*it);
21453
21454 /* Stop if truncating at the right edge. */
21455 if (it->line_wrap == TRUNCATE
21456 && it->current_x >= it->last_visible_x)
21457 {
21458 /* Add truncation mark, but don't do it if the line is
21459 truncated at a padding space. */
21460 if (it_charpos < it->string_nchars)
21461 {
21462 if (!FRAME_WINDOW_P (it->f))
21463 {
21464 int ii, n;
21465
21466 if (it->current_x > it->last_visible_x)
21467 {
21468 if (!row->reversed_p)
21469 {
21470 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21471 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21472 break;
21473 }
21474 else
21475 {
21476 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21477 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21478 break;
21479 unproduce_glyphs (it, ii + 1);
21480 ii = row->used[TEXT_AREA] - (ii + 1);
21481 }
21482 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21483 {
21484 row->used[TEXT_AREA] = ii;
21485 produce_special_glyphs (it, IT_TRUNCATION);
21486 }
21487 }
21488 produce_special_glyphs (it, IT_TRUNCATION);
21489 }
21490 row->truncated_on_right_p = 1;
21491 }
21492 break;
21493 }
21494 }
21495
21496 /* Maybe insert a truncation at the left. */
21497 if (it->first_visible_x
21498 && it_charpos > 0)
21499 {
21500 if (!FRAME_WINDOW_P (it->f))
21501 insert_left_trunc_glyphs (it);
21502 row->truncated_on_left_p = 1;
21503 }
21504
21505 it->face_id = saved_face_id;
21506
21507 /* Value is number of columns displayed. */
21508 return it->hpos - hpos_at_start;
21509 }
21510
21511
21512 \f
21513 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21514 appears as an element of LIST or as the car of an element of LIST.
21515 If PROPVAL is a list, compare each element against LIST in that
21516 way, and return 1/2 if any element of PROPVAL is found in LIST.
21517 Otherwise return 0. This function cannot quit.
21518 The return value is 2 if the text is invisible but with an ellipsis
21519 and 1 if it's invisible and without an ellipsis. */
21520
21521 int
21522 invisible_p (register Lisp_Object propval, Lisp_Object list)
21523 {
21524 register Lisp_Object tail, proptail;
21525
21526 for (tail = list; CONSP (tail); tail = XCDR (tail))
21527 {
21528 register Lisp_Object tem;
21529 tem = XCAR (tail);
21530 if (EQ (propval, tem))
21531 return 1;
21532 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21533 return NILP (XCDR (tem)) ? 1 : 2;
21534 }
21535
21536 if (CONSP (propval))
21537 {
21538 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21539 {
21540 Lisp_Object propelt;
21541 propelt = XCAR (proptail);
21542 for (tail = list; CONSP (tail); tail = XCDR (tail))
21543 {
21544 register Lisp_Object tem;
21545 tem = XCAR (tail);
21546 if (EQ (propelt, tem))
21547 return 1;
21548 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21549 return NILP (XCDR (tem)) ? 1 : 2;
21550 }
21551 }
21552 }
21553
21554 return 0;
21555 }
21556
21557 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21558 doc: /* Non-nil if the property makes the text invisible.
21559 POS-OR-PROP can be a marker or number, in which case it is taken to be
21560 a position in the current buffer and the value of the `invisible' property
21561 is checked; or it can be some other value, which is then presumed to be the
21562 value of the `invisible' property of the text of interest.
21563 The non-nil value returned can be t for truly invisible text or something
21564 else if the text is replaced by an ellipsis. */)
21565 (Lisp_Object pos_or_prop)
21566 {
21567 Lisp_Object prop
21568 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21569 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21570 : pos_or_prop);
21571 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21572 return (invis == 0 ? Qnil
21573 : invis == 1 ? Qt
21574 : make_number (invis));
21575 }
21576
21577 /* Calculate a width or height in pixels from a specification using
21578 the following elements:
21579
21580 SPEC ::=
21581 NUM - a (fractional) multiple of the default font width/height
21582 (NUM) - specifies exactly NUM pixels
21583 UNIT - a fixed number of pixels, see below.
21584 ELEMENT - size of a display element in pixels, see below.
21585 (NUM . SPEC) - equals NUM * SPEC
21586 (+ SPEC SPEC ...) - add pixel values
21587 (- SPEC SPEC ...) - subtract pixel values
21588 (- SPEC) - negate pixel value
21589
21590 NUM ::=
21591 INT or FLOAT - a number constant
21592 SYMBOL - use symbol's (buffer local) variable binding.
21593
21594 UNIT ::=
21595 in - pixels per inch *)
21596 mm - pixels per 1/1000 meter *)
21597 cm - pixels per 1/100 meter *)
21598 width - width of current font in pixels.
21599 height - height of current font in pixels.
21600
21601 *) using the ratio(s) defined in display-pixels-per-inch.
21602
21603 ELEMENT ::=
21604
21605 left-fringe - left fringe width in pixels
21606 right-fringe - right fringe width in pixels
21607
21608 left-margin - left margin width in pixels
21609 right-margin - right margin width in pixels
21610
21611 scroll-bar - scroll-bar area width in pixels
21612
21613 Examples:
21614
21615 Pixels corresponding to 5 inches:
21616 (5 . in)
21617
21618 Total width of non-text areas on left side of window (if scroll-bar is on left):
21619 '(space :width (+ left-fringe left-margin scroll-bar))
21620
21621 Align to first text column (in header line):
21622 '(space :align-to 0)
21623
21624 Align to middle of text area minus half the width of variable `my-image'
21625 containing a loaded image:
21626 '(space :align-to (0.5 . (- text my-image)))
21627
21628 Width of left margin minus width of 1 character in the default font:
21629 '(space :width (- left-margin 1))
21630
21631 Width of left margin minus width of 2 characters in the current font:
21632 '(space :width (- left-margin (2 . width)))
21633
21634 Center 1 character over left-margin (in header line):
21635 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21636
21637 Different ways to express width of left fringe plus left margin minus one pixel:
21638 '(space :width (- (+ left-fringe left-margin) (1)))
21639 '(space :width (+ left-fringe left-margin (- (1))))
21640 '(space :width (+ left-fringe left-margin (-1)))
21641
21642 */
21643
21644 #define NUMVAL(X) \
21645 ((INTEGERP (X) || FLOATP (X)) \
21646 ? XFLOATINT (X) \
21647 : - 1)
21648
21649 static int
21650 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21651 struct font *font, int width_p, int *align_to)
21652 {
21653 double pixels;
21654
21655 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21656 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21657
21658 if (NILP (prop))
21659 return OK_PIXELS (0);
21660
21661 xassert (FRAME_LIVE_P (it->f));
21662
21663 if (SYMBOLP (prop))
21664 {
21665 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21666 {
21667 char *unit = SSDATA (SYMBOL_NAME (prop));
21668
21669 if (unit[0] == 'i' && unit[1] == 'n')
21670 pixels = 1.0;
21671 else if (unit[0] == 'm' && unit[1] == 'm')
21672 pixels = 25.4;
21673 else if (unit[0] == 'c' && unit[1] == 'm')
21674 pixels = 2.54;
21675 else
21676 pixels = 0;
21677 if (pixels > 0)
21678 {
21679 double ppi;
21680 #ifdef HAVE_WINDOW_SYSTEM
21681 if (FRAME_WINDOW_P (it->f)
21682 && (ppi = (width_p
21683 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21684 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21685 ppi > 0))
21686 return OK_PIXELS (ppi / pixels);
21687 #endif
21688
21689 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21690 || (CONSP (Vdisplay_pixels_per_inch)
21691 && (ppi = (width_p
21692 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21693 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21694 ppi > 0)))
21695 return OK_PIXELS (ppi / pixels);
21696
21697 return 0;
21698 }
21699 }
21700
21701 #ifdef HAVE_WINDOW_SYSTEM
21702 if (EQ (prop, Qheight))
21703 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21704 if (EQ (prop, Qwidth))
21705 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21706 #else
21707 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21708 return OK_PIXELS (1);
21709 #endif
21710
21711 if (EQ (prop, Qtext))
21712 return OK_PIXELS (width_p
21713 ? window_box_width (it->w, TEXT_AREA)
21714 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21715
21716 if (align_to && *align_to < 0)
21717 {
21718 *res = 0;
21719 if (EQ (prop, Qleft))
21720 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21721 if (EQ (prop, Qright))
21722 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21723 if (EQ (prop, Qcenter))
21724 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21725 + window_box_width (it->w, TEXT_AREA) / 2);
21726 if (EQ (prop, Qleft_fringe))
21727 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21728 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21729 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21730 if (EQ (prop, Qright_fringe))
21731 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21732 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21733 : window_box_right_offset (it->w, TEXT_AREA));
21734 if (EQ (prop, Qleft_margin))
21735 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21736 if (EQ (prop, Qright_margin))
21737 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21738 if (EQ (prop, Qscroll_bar))
21739 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21740 ? 0
21741 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21742 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21743 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21744 : 0)));
21745 }
21746 else
21747 {
21748 if (EQ (prop, Qleft_fringe))
21749 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21750 if (EQ (prop, Qright_fringe))
21751 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21752 if (EQ (prop, Qleft_margin))
21753 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21754 if (EQ (prop, Qright_margin))
21755 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21756 if (EQ (prop, Qscroll_bar))
21757 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21758 }
21759
21760 prop = Fbuffer_local_value (prop, it->w->buffer);
21761 }
21762
21763 if (INTEGERP (prop) || FLOATP (prop))
21764 {
21765 int base_unit = (width_p
21766 ? FRAME_COLUMN_WIDTH (it->f)
21767 : FRAME_LINE_HEIGHT (it->f));
21768 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21769 }
21770
21771 if (CONSP (prop))
21772 {
21773 Lisp_Object car = XCAR (prop);
21774 Lisp_Object cdr = XCDR (prop);
21775
21776 if (SYMBOLP (car))
21777 {
21778 #ifdef HAVE_WINDOW_SYSTEM
21779 if (FRAME_WINDOW_P (it->f)
21780 && valid_image_p (prop))
21781 {
21782 ptrdiff_t id = lookup_image (it->f, prop);
21783 struct image *img = IMAGE_FROM_ID (it->f, id);
21784
21785 return OK_PIXELS (width_p ? img->width : img->height);
21786 }
21787 #endif
21788 if (EQ (car, Qplus) || EQ (car, Qminus))
21789 {
21790 int first = 1;
21791 double px;
21792
21793 pixels = 0;
21794 while (CONSP (cdr))
21795 {
21796 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21797 font, width_p, align_to))
21798 return 0;
21799 if (first)
21800 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21801 else
21802 pixels += px;
21803 cdr = XCDR (cdr);
21804 }
21805 if (EQ (car, Qminus))
21806 pixels = -pixels;
21807 return OK_PIXELS (pixels);
21808 }
21809
21810 car = Fbuffer_local_value (car, it->w->buffer);
21811 }
21812
21813 if (INTEGERP (car) || FLOATP (car))
21814 {
21815 double fact;
21816 pixels = XFLOATINT (car);
21817 if (NILP (cdr))
21818 return OK_PIXELS (pixels);
21819 if (calc_pixel_width_or_height (&fact, it, cdr,
21820 font, width_p, align_to))
21821 return OK_PIXELS (pixels * fact);
21822 return 0;
21823 }
21824
21825 return 0;
21826 }
21827
21828 return 0;
21829 }
21830
21831 \f
21832 /***********************************************************************
21833 Glyph Display
21834 ***********************************************************************/
21835
21836 #ifdef HAVE_WINDOW_SYSTEM
21837
21838 #if GLYPH_DEBUG
21839
21840 void
21841 dump_glyph_string (struct glyph_string *s)
21842 {
21843 fprintf (stderr, "glyph string\n");
21844 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21845 s->x, s->y, s->width, s->height);
21846 fprintf (stderr, " ybase = %d\n", s->ybase);
21847 fprintf (stderr, " hl = %d\n", s->hl);
21848 fprintf (stderr, " left overhang = %d, right = %d\n",
21849 s->left_overhang, s->right_overhang);
21850 fprintf (stderr, " nchars = %d\n", s->nchars);
21851 fprintf (stderr, " extends to end of line = %d\n",
21852 s->extends_to_end_of_line_p);
21853 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21854 fprintf (stderr, " bg width = %d\n", s->background_width);
21855 }
21856
21857 #endif /* GLYPH_DEBUG */
21858
21859 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21860 of XChar2b structures for S; it can't be allocated in
21861 init_glyph_string because it must be allocated via `alloca'. W
21862 is the window on which S is drawn. ROW and AREA are the glyph row
21863 and area within the row from which S is constructed. START is the
21864 index of the first glyph structure covered by S. HL is a
21865 face-override for drawing S. */
21866
21867 #ifdef HAVE_NTGUI
21868 #define OPTIONAL_HDC(hdc) HDC hdc,
21869 #define DECLARE_HDC(hdc) HDC hdc;
21870 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21871 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21872 #endif
21873
21874 #ifndef OPTIONAL_HDC
21875 #define OPTIONAL_HDC(hdc)
21876 #define DECLARE_HDC(hdc)
21877 #define ALLOCATE_HDC(hdc, f)
21878 #define RELEASE_HDC(hdc, f)
21879 #endif
21880
21881 static void
21882 init_glyph_string (struct glyph_string *s,
21883 OPTIONAL_HDC (hdc)
21884 XChar2b *char2b, struct window *w, struct glyph_row *row,
21885 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21886 {
21887 memset (s, 0, sizeof *s);
21888 s->w = w;
21889 s->f = XFRAME (w->frame);
21890 #ifdef HAVE_NTGUI
21891 s->hdc = hdc;
21892 #endif
21893 s->display = FRAME_X_DISPLAY (s->f);
21894 s->window = FRAME_X_WINDOW (s->f);
21895 s->char2b = char2b;
21896 s->hl = hl;
21897 s->row = row;
21898 s->area = area;
21899 s->first_glyph = row->glyphs[area] + start;
21900 s->height = row->height;
21901 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21902 s->ybase = s->y + row->ascent;
21903 }
21904
21905
21906 /* Append the list of glyph strings with head H and tail T to the list
21907 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21908
21909 static inline void
21910 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21911 struct glyph_string *h, struct glyph_string *t)
21912 {
21913 if (h)
21914 {
21915 if (*head)
21916 (*tail)->next = h;
21917 else
21918 *head = h;
21919 h->prev = *tail;
21920 *tail = t;
21921 }
21922 }
21923
21924
21925 /* Prepend the list of glyph strings with head H and tail T to the
21926 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21927 result. */
21928
21929 static inline void
21930 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21931 struct glyph_string *h, struct glyph_string *t)
21932 {
21933 if (h)
21934 {
21935 if (*head)
21936 (*head)->prev = t;
21937 else
21938 *tail = t;
21939 t->next = *head;
21940 *head = h;
21941 }
21942 }
21943
21944
21945 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21946 Set *HEAD and *TAIL to the resulting list. */
21947
21948 static inline void
21949 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21950 struct glyph_string *s)
21951 {
21952 s->next = s->prev = NULL;
21953 append_glyph_string_lists (head, tail, s, s);
21954 }
21955
21956
21957 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21958 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21959 make sure that X resources for the face returned are allocated.
21960 Value is a pointer to a realized face that is ready for display if
21961 DISPLAY_P is non-zero. */
21962
21963 static inline struct face *
21964 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21965 XChar2b *char2b, int display_p)
21966 {
21967 struct face *face = FACE_FROM_ID (f, face_id);
21968
21969 if (face->font)
21970 {
21971 unsigned code = face->font->driver->encode_char (face->font, c);
21972
21973 if (code != FONT_INVALID_CODE)
21974 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21975 else
21976 STORE_XCHAR2B (char2b, 0, 0);
21977 }
21978
21979 /* Make sure X resources of the face are allocated. */
21980 #ifdef HAVE_X_WINDOWS
21981 if (display_p)
21982 #endif
21983 {
21984 xassert (face != NULL);
21985 PREPARE_FACE_FOR_DISPLAY (f, face);
21986 }
21987
21988 return face;
21989 }
21990
21991
21992 /* Get face and two-byte form of character glyph GLYPH on frame F.
21993 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21994 a pointer to a realized face that is ready for display. */
21995
21996 static inline struct face *
21997 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21998 XChar2b *char2b, int *two_byte_p)
21999 {
22000 struct face *face;
22001
22002 xassert (glyph->type == CHAR_GLYPH);
22003 face = FACE_FROM_ID (f, glyph->face_id);
22004
22005 if (two_byte_p)
22006 *two_byte_p = 0;
22007
22008 if (face->font)
22009 {
22010 unsigned code;
22011
22012 if (CHAR_BYTE8_P (glyph->u.ch))
22013 code = CHAR_TO_BYTE8 (glyph->u.ch);
22014 else
22015 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22016
22017 if (code != FONT_INVALID_CODE)
22018 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22019 else
22020 STORE_XCHAR2B (char2b, 0, 0);
22021 }
22022
22023 /* Make sure X resources of the face are allocated. */
22024 xassert (face != NULL);
22025 PREPARE_FACE_FOR_DISPLAY (f, face);
22026 return face;
22027 }
22028
22029
22030 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22031 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
22032
22033 static inline int
22034 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22035 {
22036 unsigned code;
22037
22038 if (CHAR_BYTE8_P (c))
22039 code = CHAR_TO_BYTE8 (c);
22040 else
22041 code = font->driver->encode_char (font, c);
22042
22043 if (code == FONT_INVALID_CODE)
22044 return 0;
22045 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22046 return 1;
22047 }
22048
22049
22050 /* Fill glyph string S with composition components specified by S->cmp.
22051
22052 BASE_FACE is the base face of the composition.
22053 S->cmp_from is the index of the first component for S.
22054
22055 OVERLAPS non-zero means S should draw the foreground only, and use
22056 its physical height for clipping. See also draw_glyphs.
22057
22058 Value is the index of a component not in S. */
22059
22060 static int
22061 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22062 int overlaps)
22063 {
22064 int i;
22065 /* For all glyphs of this composition, starting at the offset
22066 S->cmp_from, until we reach the end of the definition or encounter a
22067 glyph that requires the different face, add it to S. */
22068 struct face *face;
22069
22070 xassert (s);
22071
22072 s->for_overlaps = overlaps;
22073 s->face = NULL;
22074 s->font = NULL;
22075 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22076 {
22077 int c = COMPOSITION_GLYPH (s->cmp, i);
22078
22079 /* TAB in a composition means display glyphs with padding space
22080 on the left or right. */
22081 if (c != '\t')
22082 {
22083 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22084 -1, Qnil);
22085
22086 face = get_char_face_and_encoding (s->f, c, face_id,
22087 s->char2b + i, 1);
22088 if (face)
22089 {
22090 if (! s->face)
22091 {
22092 s->face = face;
22093 s->font = s->face->font;
22094 }
22095 else if (s->face != face)
22096 break;
22097 }
22098 }
22099 ++s->nchars;
22100 }
22101 s->cmp_to = i;
22102
22103 /* All glyph strings for the same composition has the same width,
22104 i.e. the width set for the first component of the composition. */
22105 s->width = s->first_glyph->pixel_width;
22106
22107 /* If the specified font could not be loaded, use the frame's
22108 default font, but record the fact that we couldn't load it in
22109 the glyph string so that we can draw rectangles for the
22110 characters of the glyph string. */
22111 if (s->font == NULL)
22112 {
22113 s->font_not_found_p = 1;
22114 s->font = FRAME_FONT (s->f);
22115 }
22116
22117 /* Adjust base line for subscript/superscript text. */
22118 s->ybase += s->first_glyph->voffset;
22119
22120 /* This glyph string must always be drawn with 16-bit functions. */
22121 s->two_byte_p = 1;
22122
22123 return s->cmp_to;
22124 }
22125
22126 static int
22127 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22128 int start, int end, int overlaps)
22129 {
22130 struct glyph *glyph, *last;
22131 Lisp_Object lgstring;
22132 int i;
22133
22134 s->for_overlaps = overlaps;
22135 glyph = s->row->glyphs[s->area] + start;
22136 last = s->row->glyphs[s->area] + end;
22137 s->cmp_id = glyph->u.cmp.id;
22138 s->cmp_from = glyph->slice.cmp.from;
22139 s->cmp_to = glyph->slice.cmp.to + 1;
22140 s->face = FACE_FROM_ID (s->f, face_id);
22141 lgstring = composition_gstring_from_id (s->cmp_id);
22142 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22143 glyph++;
22144 while (glyph < last
22145 && glyph->u.cmp.automatic
22146 && glyph->u.cmp.id == s->cmp_id
22147 && s->cmp_to == glyph->slice.cmp.from)
22148 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22149
22150 for (i = s->cmp_from; i < s->cmp_to; i++)
22151 {
22152 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22153 unsigned code = LGLYPH_CODE (lglyph);
22154
22155 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22156 }
22157 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22158 return glyph - s->row->glyphs[s->area];
22159 }
22160
22161
22162 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22163 See the comment of fill_glyph_string for arguments.
22164 Value is the index of the first glyph not in S. */
22165
22166
22167 static int
22168 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22169 int start, int end, int overlaps)
22170 {
22171 struct glyph *glyph, *last;
22172 int voffset;
22173
22174 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22175 s->for_overlaps = overlaps;
22176 glyph = s->row->glyphs[s->area] + start;
22177 last = s->row->glyphs[s->area] + end;
22178 voffset = glyph->voffset;
22179 s->face = FACE_FROM_ID (s->f, face_id);
22180 s->font = s->face->font;
22181 s->nchars = 1;
22182 s->width = glyph->pixel_width;
22183 glyph++;
22184 while (glyph < last
22185 && glyph->type == GLYPHLESS_GLYPH
22186 && glyph->voffset == voffset
22187 && glyph->face_id == face_id)
22188 {
22189 s->nchars++;
22190 s->width += glyph->pixel_width;
22191 glyph++;
22192 }
22193 s->ybase += voffset;
22194 return glyph - s->row->glyphs[s->area];
22195 }
22196
22197
22198 /* Fill glyph string S from a sequence of character glyphs.
22199
22200 FACE_ID is the face id of the string. START is the index of the
22201 first glyph to consider, END is the index of the last + 1.
22202 OVERLAPS non-zero means S should draw the foreground only, and use
22203 its physical height for clipping. See also draw_glyphs.
22204
22205 Value is the index of the first glyph not in S. */
22206
22207 static int
22208 fill_glyph_string (struct glyph_string *s, int face_id,
22209 int start, int end, int overlaps)
22210 {
22211 struct glyph *glyph, *last;
22212 int voffset;
22213 int glyph_not_available_p;
22214
22215 xassert (s->f == XFRAME (s->w->frame));
22216 xassert (s->nchars == 0);
22217 xassert (start >= 0 && end > start);
22218
22219 s->for_overlaps = overlaps;
22220 glyph = s->row->glyphs[s->area] + start;
22221 last = s->row->glyphs[s->area] + end;
22222 voffset = glyph->voffset;
22223 s->padding_p = glyph->padding_p;
22224 glyph_not_available_p = glyph->glyph_not_available_p;
22225
22226 while (glyph < last
22227 && glyph->type == CHAR_GLYPH
22228 && glyph->voffset == voffset
22229 /* Same face id implies same font, nowadays. */
22230 && glyph->face_id == face_id
22231 && glyph->glyph_not_available_p == glyph_not_available_p)
22232 {
22233 int two_byte_p;
22234
22235 s->face = get_glyph_face_and_encoding (s->f, glyph,
22236 s->char2b + s->nchars,
22237 &two_byte_p);
22238 s->two_byte_p = two_byte_p;
22239 ++s->nchars;
22240 xassert (s->nchars <= end - start);
22241 s->width += glyph->pixel_width;
22242 if (glyph++->padding_p != s->padding_p)
22243 break;
22244 }
22245
22246 s->font = s->face->font;
22247
22248 /* If the specified font could not be loaded, use the frame's font,
22249 but record the fact that we couldn't load it in
22250 S->font_not_found_p so that we can draw rectangles for the
22251 characters of the glyph string. */
22252 if (s->font == NULL || glyph_not_available_p)
22253 {
22254 s->font_not_found_p = 1;
22255 s->font = FRAME_FONT (s->f);
22256 }
22257
22258 /* Adjust base line for subscript/superscript text. */
22259 s->ybase += voffset;
22260
22261 xassert (s->face && s->face->gc);
22262 return glyph - s->row->glyphs[s->area];
22263 }
22264
22265
22266 /* Fill glyph string S from image glyph S->first_glyph. */
22267
22268 static void
22269 fill_image_glyph_string (struct glyph_string *s)
22270 {
22271 xassert (s->first_glyph->type == IMAGE_GLYPH);
22272 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22273 xassert (s->img);
22274 s->slice = s->first_glyph->slice.img;
22275 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22276 s->font = s->face->font;
22277 s->width = s->first_glyph->pixel_width;
22278
22279 /* Adjust base line for subscript/superscript text. */
22280 s->ybase += s->first_glyph->voffset;
22281 }
22282
22283
22284 /* Fill glyph string S from a sequence of stretch glyphs.
22285
22286 START is the index of the first glyph to consider,
22287 END is the index of the last + 1.
22288
22289 Value is the index of the first glyph not in S. */
22290
22291 static int
22292 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22293 {
22294 struct glyph *glyph, *last;
22295 int voffset, face_id;
22296
22297 xassert (s->first_glyph->type == STRETCH_GLYPH);
22298
22299 glyph = s->row->glyphs[s->area] + start;
22300 last = s->row->glyphs[s->area] + end;
22301 face_id = glyph->face_id;
22302 s->face = FACE_FROM_ID (s->f, face_id);
22303 s->font = s->face->font;
22304 s->width = glyph->pixel_width;
22305 s->nchars = 1;
22306 voffset = glyph->voffset;
22307
22308 for (++glyph;
22309 (glyph < last
22310 && glyph->type == STRETCH_GLYPH
22311 && glyph->voffset == voffset
22312 && glyph->face_id == face_id);
22313 ++glyph)
22314 s->width += glyph->pixel_width;
22315
22316 /* Adjust base line for subscript/superscript text. */
22317 s->ybase += voffset;
22318
22319 /* The case that face->gc == 0 is handled when drawing the glyph
22320 string by calling PREPARE_FACE_FOR_DISPLAY. */
22321 xassert (s->face);
22322 return glyph - s->row->glyphs[s->area];
22323 }
22324
22325 static struct font_metrics *
22326 get_per_char_metric (struct font *font, XChar2b *char2b)
22327 {
22328 static struct font_metrics metrics;
22329 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22330
22331 if (! font || code == FONT_INVALID_CODE)
22332 return NULL;
22333 font->driver->text_extents (font, &code, 1, &metrics);
22334 return &metrics;
22335 }
22336
22337 /* EXPORT for RIF:
22338 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22339 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22340 assumed to be zero. */
22341
22342 void
22343 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22344 {
22345 *left = *right = 0;
22346
22347 if (glyph->type == CHAR_GLYPH)
22348 {
22349 struct face *face;
22350 XChar2b char2b;
22351 struct font_metrics *pcm;
22352
22353 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22354 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22355 {
22356 if (pcm->rbearing > pcm->width)
22357 *right = pcm->rbearing - pcm->width;
22358 if (pcm->lbearing < 0)
22359 *left = -pcm->lbearing;
22360 }
22361 }
22362 else if (glyph->type == COMPOSITE_GLYPH)
22363 {
22364 if (! glyph->u.cmp.automatic)
22365 {
22366 struct composition *cmp = composition_table[glyph->u.cmp.id];
22367
22368 if (cmp->rbearing > cmp->pixel_width)
22369 *right = cmp->rbearing - cmp->pixel_width;
22370 if (cmp->lbearing < 0)
22371 *left = - cmp->lbearing;
22372 }
22373 else
22374 {
22375 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22376 struct font_metrics metrics;
22377
22378 composition_gstring_width (gstring, glyph->slice.cmp.from,
22379 glyph->slice.cmp.to + 1, &metrics);
22380 if (metrics.rbearing > metrics.width)
22381 *right = metrics.rbearing - metrics.width;
22382 if (metrics.lbearing < 0)
22383 *left = - metrics.lbearing;
22384 }
22385 }
22386 }
22387
22388
22389 /* Return the index of the first glyph preceding glyph string S that
22390 is overwritten by S because of S's left overhang. Value is -1
22391 if no glyphs are overwritten. */
22392
22393 static int
22394 left_overwritten (struct glyph_string *s)
22395 {
22396 int k;
22397
22398 if (s->left_overhang)
22399 {
22400 int x = 0, i;
22401 struct glyph *glyphs = s->row->glyphs[s->area];
22402 int first = s->first_glyph - glyphs;
22403
22404 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22405 x -= glyphs[i].pixel_width;
22406
22407 k = i + 1;
22408 }
22409 else
22410 k = -1;
22411
22412 return k;
22413 }
22414
22415
22416 /* Return the index of the first glyph preceding glyph string S that
22417 is overwriting S because of its right overhang. Value is -1 if no
22418 glyph in front of S overwrites S. */
22419
22420 static int
22421 left_overwriting (struct glyph_string *s)
22422 {
22423 int i, k, x;
22424 struct glyph *glyphs = s->row->glyphs[s->area];
22425 int first = s->first_glyph - glyphs;
22426
22427 k = -1;
22428 x = 0;
22429 for (i = first - 1; i >= 0; --i)
22430 {
22431 int left, right;
22432 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22433 if (x + right > 0)
22434 k = i;
22435 x -= glyphs[i].pixel_width;
22436 }
22437
22438 return k;
22439 }
22440
22441
22442 /* Return the index of the last glyph following glyph string S that is
22443 overwritten by S because of S's right overhang. Value is -1 if
22444 no such glyph is found. */
22445
22446 static int
22447 right_overwritten (struct glyph_string *s)
22448 {
22449 int k = -1;
22450
22451 if (s->right_overhang)
22452 {
22453 int x = 0, i;
22454 struct glyph *glyphs = s->row->glyphs[s->area];
22455 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22456 int end = s->row->used[s->area];
22457
22458 for (i = first; i < end && s->right_overhang > x; ++i)
22459 x += glyphs[i].pixel_width;
22460
22461 k = i;
22462 }
22463
22464 return k;
22465 }
22466
22467
22468 /* Return the index of the last glyph following glyph string S that
22469 overwrites S because of its left overhang. Value is negative
22470 if no such glyph is found. */
22471
22472 static int
22473 right_overwriting (struct glyph_string *s)
22474 {
22475 int i, k, x;
22476 int end = s->row->used[s->area];
22477 struct glyph *glyphs = s->row->glyphs[s->area];
22478 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22479
22480 k = -1;
22481 x = 0;
22482 for (i = first; i < end; ++i)
22483 {
22484 int left, right;
22485 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22486 if (x - left < 0)
22487 k = i;
22488 x += glyphs[i].pixel_width;
22489 }
22490
22491 return k;
22492 }
22493
22494
22495 /* Set background width of glyph string S. START is the index of the
22496 first glyph following S. LAST_X is the right-most x-position + 1
22497 in the drawing area. */
22498
22499 static inline void
22500 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22501 {
22502 /* If the face of this glyph string has to be drawn to the end of
22503 the drawing area, set S->extends_to_end_of_line_p. */
22504
22505 if (start == s->row->used[s->area]
22506 && s->area == TEXT_AREA
22507 && ((s->row->fill_line_p
22508 && (s->hl == DRAW_NORMAL_TEXT
22509 || s->hl == DRAW_IMAGE_RAISED
22510 || s->hl == DRAW_IMAGE_SUNKEN))
22511 || s->hl == DRAW_MOUSE_FACE))
22512 s->extends_to_end_of_line_p = 1;
22513
22514 /* If S extends its face to the end of the line, set its
22515 background_width to the distance to the right edge of the drawing
22516 area. */
22517 if (s->extends_to_end_of_line_p)
22518 s->background_width = last_x - s->x + 1;
22519 else
22520 s->background_width = s->width;
22521 }
22522
22523
22524 /* Compute overhangs and x-positions for glyph string S and its
22525 predecessors, or successors. X is the starting x-position for S.
22526 BACKWARD_P non-zero means process predecessors. */
22527
22528 static void
22529 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22530 {
22531 if (backward_p)
22532 {
22533 while (s)
22534 {
22535 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22536 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22537 x -= s->width;
22538 s->x = x;
22539 s = s->prev;
22540 }
22541 }
22542 else
22543 {
22544 while (s)
22545 {
22546 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22547 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22548 s->x = x;
22549 x += s->width;
22550 s = s->next;
22551 }
22552 }
22553 }
22554
22555
22556
22557 /* The following macros are only called from draw_glyphs below.
22558 They reference the following parameters of that function directly:
22559 `w', `row', `area', and `overlap_p'
22560 as well as the following local variables:
22561 `s', `f', and `hdc' (in W32) */
22562
22563 #ifdef HAVE_NTGUI
22564 /* On W32, silently add local `hdc' variable to argument list of
22565 init_glyph_string. */
22566 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22567 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22568 #else
22569 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22570 init_glyph_string (s, char2b, w, row, area, start, hl)
22571 #endif
22572
22573 /* Add a glyph string for a stretch glyph to the list of strings
22574 between HEAD and TAIL. START is the index of the stretch glyph in
22575 row area AREA of glyph row ROW. END is the index of the last glyph
22576 in that glyph row area. X is the current output position assigned
22577 to the new glyph string constructed. HL overrides that face of the
22578 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22579 is the right-most x-position of the drawing area. */
22580
22581 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22582 and below -- keep them on one line. */
22583 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22584 do \
22585 { \
22586 s = (struct glyph_string *) alloca (sizeof *s); \
22587 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22588 START = fill_stretch_glyph_string (s, START, END); \
22589 append_glyph_string (&HEAD, &TAIL, s); \
22590 s->x = (X); \
22591 } \
22592 while (0)
22593
22594
22595 /* Add a glyph string for an image glyph to the list of strings
22596 between HEAD and TAIL. START is the index of the image glyph in
22597 row area AREA of glyph row ROW. END is the index of the last glyph
22598 in that glyph row area. X is the current output position assigned
22599 to the new glyph string constructed. HL overrides that face of the
22600 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22601 is the right-most x-position of the drawing area. */
22602
22603 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22604 do \
22605 { \
22606 s = (struct glyph_string *) alloca (sizeof *s); \
22607 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22608 fill_image_glyph_string (s); \
22609 append_glyph_string (&HEAD, &TAIL, s); \
22610 ++START; \
22611 s->x = (X); \
22612 } \
22613 while (0)
22614
22615
22616 /* Add a glyph string for a sequence of character glyphs to the list
22617 of strings between HEAD and TAIL. START is the index of the first
22618 glyph in row area AREA of glyph row ROW that is part of the new
22619 glyph string. END is the index of the last glyph in that glyph row
22620 area. X is the current output position assigned to the new glyph
22621 string constructed. HL overrides that face of the glyph; e.g. it
22622 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22623 right-most x-position of the drawing area. */
22624
22625 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22626 do \
22627 { \
22628 int face_id; \
22629 XChar2b *char2b; \
22630 \
22631 face_id = (row)->glyphs[area][START].face_id; \
22632 \
22633 s = (struct glyph_string *) alloca (sizeof *s); \
22634 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22635 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22636 append_glyph_string (&HEAD, &TAIL, s); \
22637 s->x = (X); \
22638 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22639 } \
22640 while (0)
22641
22642
22643 /* Add a glyph string for a composite sequence to the list of strings
22644 between HEAD and TAIL. START is the index of the first glyph in
22645 row area AREA of glyph row ROW that is part of the new glyph
22646 string. END is the index of the last glyph in that glyph row area.
22647 X is the current output position assigned to the new glyph string
22648 constructed. HL overrides that face of the glyph; e.g. it is
22649 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22650 x-position of the drawing area. */
22651
22652 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22653 do { \
22654 int face_id = (row)->glyphs[area][START].face_id; \
22655 struct face *base_face = FACE_FROM_ID (f, face_id); \
22656 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22657 struct composition *cmp = composition_table[cmp_id]; \
22658 XChar2b *char2b; \
22659 struct glyph_string *first_s IF_LINT (= NULL); \
22660 int n; \
22661 \
22662 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22663 \
22664 /* Make glyph_strings for each glyph sequence that is drawable by \
22665 the same face, and append them to HEAD/TAIL. */ \
22666 for (n = 0; n < cmp->glyph_len;) \
22667 { \
22668 s = (struct glyph_string *) alloca (sizeof *s); \
22669 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22670 append_glyph_string (&(HEAD), &(TAIL), s); \
22671 s->cmp = cmp; \
22672 s->cmp_from = n; \
22673 s->x = (X); \
22674 if (n == 0) \
22675 first_s = s; \
22676 n = fill_composite_glyph_string (s, base_face, overlaps); \
22677 } \
22678 \
22679 ++START; \
22680 s = first_s; \
22681 } while (0)
22682
22683
22684 /* Add a glyph string for a glyph-string sequence to the list of strings
22685 between HEAD and TAIL. */
22686
22687 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22688 do { \
22689 int face_id; \
22690 XChar2b *char2b; \
22691 Lisp_Object gstring; \
22692 \
22693 face_id = (row)->glyphs[area][START].face_id; \
22694 gstring = (composition_gstring_from_id \
22695 ((row)->glyphs[area][START].u.cmp.id)); \
22696 s = (struct glyph_string *) alloca (sizeof *s); \
22697 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22698 * LGSTRING_GLYPH_LEN (gstring)); \
22699 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22700 append_glyph_string (&(HEAD), &(TAIL), s); \
22701 s->x = (X); \
22702 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22703 } while (0)
22704
22705
22706 /* Add a glyph string for a sequence of glyphless character's glyphs
22707 to the list of strings between HEAD and TAIL. The meanings of
22708 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22709
22710 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22711 do \
22712 { \
22713 int face_id; \
22714 \
22715 face_id = (row)->glyphs[area][START].face_id; \
22716 \
22717 s = (struct glyph_string *) alloca (sizeof *s); \
22718 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22719 append_glyph_string (&HEAD, &TAIL, s); \
22720 s->x = (X); \
22721 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22722 overlaps); \
22723 } \
22724 while (0)
22725
22726
22727 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22728 of AREA of glyph row ROW on window W between indices START and END.
22729 HL overrides the face for drawing glyph strings, e.g. it is
22730 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22731 x-positions of the drawing area.
22732
22733 This is an ugly monster macro construct because we must use alloca
22734 to allocate glyph strings (because draw_glyphs can be called
22735 asynchronously). */
22736
22737 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22738 do \
22739 { \
22740 HEAD = TAIL = NULL; \
22741 while (START < END) \
22742 { \
22743 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22744 switch (first_glyph->type) \
22745 { \
22746 case CHAR_GLYPH: \
22747 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22748 HL, X, LAST_X); \
22749 break; \
22750 \
22751 case COMPOSITE_GLYPH: \
22752 if (first_glyph->u.cmp.automatic) \
22753 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22754 HL, X, LAST_X); \
22755 else \
22756 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22757 HL, X, LAST_X); \
22758 break; \
22759 \
22760 case STRETCH_GLYPH: \
22761 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22762 HL, X, LAST_X); \
22763 break; \
22764 \
22765 case IMAGE_GLYPH: \
22766 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22767 HL, X, LAST_X); \
22768 break; \
22769 \
22770 case GLYPHLESS_GLYPH: \
22771 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22772 HL, X, LAST_X); \
22773 break; \
22774 \
22775 default: \
22776 abort (); \
22777 } \
22778 \
22779 if (s) \
22780 { \
22781 set_glyph_string_background_width (s, START, LAST_X); \
22782 (X) += s->width; \
22783 } \
22784 } \
22785 } while (0)
22786
22787
22788 /* Draw glyphs between START and END in AREA of ROW on window W,
22789 starting at x-position X. X is relative to AREA in W. HL is a
22790 face-override with the following meaning:
22791
22792 DRAW_NORMAL_TEXT draw normally
22793 DRAW_CURSOR draw in cursor face
22794 DRAW_MOUSE_FACE draw in mouse face.
22795 DRAW_INVERSE_VIDEO draw in mode line face
22796 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22797 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22798
22799 If OVERLAPS is non-zero, draw only the foreground of characters and
22800 clip to the physical height of ROW. Non-zero value also defines
22801 the overlapping part to be drawn:
22802
22803 OVERLAPS_PRED overlap with preceding rows
22804 OVERLAPS_SUCC overlap with succeeding rows
22805 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22806 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22807
22808 Value is the x-position reached, relative to AREA of W. */
22809
22810 static int
22811 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22812 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
22813 enum draw_glyphs_face hl, int overlaps)
22814 {
22815 struct glyph_string *head, *tail;
22816 struct glyph_string *s;
22817 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22818 int i, j, x_reached, last_x, area_left = 0;
22819 struct frame *f = XFRAME (WINDOW_FRAME (w));
22820 DECLARE_HDC (hdc);
22821
22822 ALLOCATE_HDC (hdc, f);
22823
22824 /* Let's rather be paranoid than getting a SEGV. */
22825 end = min (end, row->used[area]);
22826 start = max (0, start);
22827 start = min (end, start);
22828
22829 /* Translate X to frame coordinates. Set last_x to the right
22830 end of the drawing area. */
22831 if (row->full_width_p)
22832 {
22833 /* X is relative to the left edge of W, without scroll bars
22834 or fringes. */
22835 area_left = WINDOW_LEFT_EDGE_X (w);
22836 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22837 }
22838 else
22839 {
22840 area_left = window_box_left (w, area);
22841 last_x = area_left + window_box_width (w, area);
22842 }
22843 x += area_left;
22844
22845 /* Build a doubly-linked list of glyph_string structures between
22846 head and tail from what we have to draw. Note that the macro
22847 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22848 the reason we use a separate variable `i'. */
22849 i = start;
22850 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22851 if (tail)
22852 x_reached = tail->x + tail->background_width;
22853 else
22854 x_reached = x;
22855
22856 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22857 the row, redraw some glyphs in front or following the glyph
22858 strings built above. */
22859 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22860 {
22861 struct glyph_string *h, *t;
22862 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22863 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22864 int check_mouse_face = 0;
22865 int dummy_x = 0;
22866
22867 /* If mouse highlighting is on, we may need to draw adjacent
22868 glyphs using mouse-face highlighting. */
22869 if (area == TEXT_AREA && row->mouse_face_p)
22870 {
22871 struct glyph_row *mouse_beg_row, *mouse_end_row;
22872
22873 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22874 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22875
22876 if (row >= mouse_beg_row && row <= mouse_end_row)
22877 {
22878 check_mouse_face = 1;
22879 mouse_beg_col = (row == mouse_beg_row)
22880 ? hlinfo->mouse_face_beg_col : 0;
22881 mouse_end_col = (row == mouse_end_row)
22882 ? hlinfo->mouse_face_end_col
22883 : row->used[TEXT_AREA];
22884 }
22885 }
22886
22887 /* Compute overhangs for all glyph strings. */
22888 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22889 for (s = head; s; s = s->next)
22890 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22891
22892 /* Prepend glyph strings for glyphs in front of the first glyph
22893 string that are overwritten because of the first glyph
22894 string's left overhang. The background of all strings
22895 prepended must be drawn because the first glyph string
22896 draws over it. */
22897 i = left_overwritten (head);
22898 if (i >= 0)
22899 {
22900 enum draw_glyphs_face overlap_hl;
22901
22902 /* If this row contains mouse highlighting, attempt to draw
22903 the overlapped glyphs with the correct highlight. This
22904 code fails if the overlap encompasses more than one glyph
22905 and mouse-highlight spans only some of these glyphs.
22906 However, making it work perfectly involves a lot more
22907 code, and I don't know if the pathological case occurs in
22908 practice, so we'll stick to this for now. --- cyd */
22909 if (check_mouse_face
22910 && mouse_beg_col < start && mouse_end_col > i)
22911 overlap_hl = DRAW_MOUSE_FACE;
22912 else
22913 overlap_hl = DRAW_NORMAL_TEXT;
22914
22915 j = i;
22916 BUILD_GLYPH_STRINGS (j, start, h, t,
22917 overlap_hl, dummy_x, last_x);
22918 start = i;
22919 compute_overhangs_and_x (t, head->x, 1);
22920 prepend_glyph_string_lists (&head, &tail, h, t);
22921 clip_head = head;
22922 }
22923
22924 /* Prepend glyph strings for glyphs in front of the first glyph
22925 string that overwrite that glyph string because of their
22926 right overhang. For these strings, only the foreground must
22927 be drawn, because it draws over the glyph string at `head'.
22928 The background must not be drawn because this would overwrite
22929 right overhangs of preceding glyphs for which no glyph
22930 strings exist. */
22931 i = left_overwriting (head);
22932 if (i >= 0)
22933 {
22934 enum draw_glyphs_face overlap_hl;
22935
22936 if (check_mouse_face
22937 && mouse_beg_col < start && mouse_end_col > i)
22938 overlap_hl = DRAW_MOUSE_FACE;
22939 else
22940 overlap_hl = DRAW_NORMAL_TEXT;
22941
22942 clip_head = head;
22943 BUILD_GLYPH_STRINGS (i, start, h, t,
22944 overlap_hl, dummy_x, last_x);
22945 for (s = h; s; s = s->next)
22946 s->background_filled_p = 1;
22947 compute_overhangs_and_x (t, head->x, 1);
22948 prepend_glyph_string_lists (&head, &tail, h, t);
22949 }
22950
22951 /* Append glyphs strings for glyphs following the last glyph
22952 string tail that are overwritten by tail. The background of
22953 these strings has to be drawn because tail's foreground draws
22954 over it. */
22955 i = right_overwritten (tail);
22956 if (i >= 0)
22957 {
22958 enum draw_glyphs_face overlap_hl;
22959
22960 if (check_mouse_face
22961 && mouse_beg_col < i && mouse_end_col > end)
22962 overlap_hl = DRAW_MOUSE_FACE;
22963 else
22964 overlap_hl = DRAW_NORMAL_TEXT;
22965
22966 BUILD_GLYPH_STRINGS (end, i, h, t,
22967 overlap_hl, x, last_x);
22968 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22969 we don't have `end = i;' here. */
22970 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22971 append_glyph_string_lists (&head, &tail, h, t);
22972 clip_tail = tail;
22973 }
22974
22975 /* Append glyph strings for glyphs following the last glyph
22976 string tail that overwrite tail. The foreground of such
22977 glyphs has to be drawn because it writes into the background
22978 of tail. The background must not be drawn because it could
22979 paint over the foreground of following glyphs. */
22980 i = right_overwriting (tail);
22981 if (i >= 0)
22982 {
22983 enum draw_glyphs_face overlap_hl;
22984 if (check_mouse_face
22985 && mouse_beg_col < i && mouse_end_col > end)
22986 overlap_hl = DRAW_MOUSE_FACE;
22987 else
22988 overlap_hl = DRAW_NORMAL_TEXT;
22989
22990 clip_tail = tail;
22991 i++; /* We must include the Ith glyph. */
22992 BUILD_GLYPH_STRINGS (end, i, h, t,
22993 overlap_hl, x, last_x);
22994 for (s = h; s; s = s->next)
22995 s->background_filled_p = 1;
22996 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22997 append_glyph_string_lists (&head, &tail, h, t);
22998 }
22999 if (clip_head || clip_tail)
23000 for (s = head; s; s = s->next)
23001 {
23002 s->clip_head = clip_head;
23003 s->clip_tail = clip_tail;
23004 }
23005 }
23006
23007 /* Draw all strings. */
23008 for (s = head; s; s = s->next)
23009 FRAME_RIF (f)->draw_glyph_string (s);
23010
23011 #ifndef HAVE_NS
23012 /* When focus a sole frame and move horizontally, this sets on_p to 0
23013 causing a failure to erase prev cursor position. */
23014 if (area == TEXT_AREA
23015 && !row->full_width_p
23016 /* When drawing overlapping rows, only the glyph strings'
23017 foreground is drawn, which doesn't erase a cursor
23018 completely. */
23019 && !overlaps)
23020 {
23021 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23022 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23023 : (tail ? tail->x + tail->background_width : x));
23024 x0 -= area_left;
23025 x1 -= area_left;
23026
23027 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23028 row->y, MATRIX_ROW_BOTTOM_Y (row));
23029 }
23030 #endif
23031
23032 /* Value is the x-position up to which drawn, relative to AREA of W.
23033 This doesn't include parts drawn because of overhangs. */
23034 if (row->full_width_p)
23035 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23036 else
23037 x_reached -= area_left;
23038
23039 RELEASE_HDC (hdc, f);
23040
23041 return x_reached;
23042 }
23043
23044 /* Expand row matrix if too narrow. Don't expand if area
23045 is not present. */
23046
23047 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23048 { \
23049 if (!fonts_changed_p \
23050 && (it->glyph_row->glyphs[area] \
23051 < it->glyph_row->glyphs[area + 1])) \
23052 { \
23053 it->w->ncols_scale_factor++; \
23054 fonts_changed_p = 1; \
23055 } \
23056 }
23057
23058 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23059 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23060
23061 static inline void
23062 append_glyph (struct it *it)
23063 {
23064 struct glyph *glyph;
23065 enum glyph_row_area area = it->area;
23066
23067 xassert (it->glyph_row);
23068 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23069
23070 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23071 if (glyph < it->glyph_row->glyphs[area + 1])
23072 {
23073 /* If the glyph row is reversed, we need to prepend the glyph
23074 rather than append it. */
23075 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23076 {
23077 struct glyph *g;
23078
23079 /* Make room for the additional glyph. */
23080 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23081 g[1] = *g;
23082 glyph = it->glyph_row->glyphs[area];
23083 }
23084 glyph->charpos = CHARPOS (it->position);
23085 glyph->object = it->object;
23086 if (it->pixel_width > 0)
23087 {
23088 glyph->pixel_width = it->pixel_width;
23089 glyph->padding_p = 0;
23090 }
23091 else
23092 {
23093 /* Assure at least 1-pixel width. Otherwise, cursor can't
23094 be displayed correctly. */
23095 glyph->pixel_width = 1;
23096 glyph->padding_p = 1;
23097 }
23098 glyph->ascent = it->ascent;
23099 glyph->descent = it->descent;
23100 glyph->voffset = it->voffset;
23101 glyph->type = CHAR_GLYPH;
23102 glyph->avoid_cursor_p = it->avoid_cursor_p;
23103 glyph->multibyte_p = it->multibyte_p;
23104 glyph->left_box_line_p = it->start_of_box_run_p;
23105 glyph->right_box_line_p = it->end_of_box_run_p;
23106 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23107 || it->phys_descent > it->descent);
23108 glyph->glyph_not_available_p = it->glyph_not_available_p;
23109 glyph->face_id = it->face_id;
23110 glyph->u.ch = it->char_to_display;
23111 glyph->slice.img = null_glyph_slice;
23112 glyph->font_type = FONT_TYPE_UNKNOWN;
23113 if (it->bidi_p)
23114 {
23115 glyph->resolved_level = it->bidi_it.resolved_level;
23116 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23117 abort ();
23118 glyph->bidi_type = it->bidi_it.type;
23119 }
23120 else
23121 {
23122 glyph->resolved_level = 0;
23123 glyph->bidi_type = UNKNOWN_BT;
23124 }
23125 ++it->glyph_row->used[area];
23126 }
23127 else
23128 IT_EXPAND_MATRIX_WIDTH (it, area);
23129 }
23130
23131 /* Store one glyph for the composition IT->cmp_it.id in
23132 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23133 non-null. */
23134
23135 static inline void
23136 append_composite_glyph (struct it *it)
23137 {
23138 struct glyph *glyph;
23139 enum glyph_row_area area = it->area;
23140
23141 xassert (it->glyph_row);
23142
23143 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23144 if (glyph < it->glyph_row->glyphs[area + 1])
23145 {
23146 /* If the glyph row is reversed, we need to prepend the glyph
23147 rather than append it. */
23148 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23149 {
23150 struct glyph *g;
23151
23152 /* Make room for the new glyph. */
23153 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23154 g[1] = *g;
23155 glyph = it->glyph_row->glyphs[it->area];
23156 }
23157 glyph->charpos = it->cmp_it.charpos;
23158 glyph->object = it->object;
23159 glyph->pixel_width = it->pixel_width;
23160 glyph->ascent = it->ascent;
23161 glyph->descent = it->descent;
23162 glyph->voffset = it->voffset;
23163 glyph->type = COMPOSITE_GLYPH;
23164 if (it->cmp_it.ch < 0)
23165 {
23166 glyph->u.cmp.automatic = 0;
23167 glyph->u.cmp.id = it->cmp_it.id;
23168 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23169 }
23170 else
23171 {
23172 glyph->u.cmp.automatic = 1;
23173 glyph->u.cmp.id = it->cmp_it.id;
23174 glyph->slice.cmp.from = it->cmp_it.from;
23175 glyph->slice.cmp.to = it->cmp_it.to - 1;
23176 }
23177 glyph->avoid_cursor_p = it->avoid_cursor_p;
23178 glyph->multibyte_p = it->multibyte_p;
23179 glyph->left_box_line_p = it->start_of_box_run_p;
23180 glyph->right_box_line_p = it->end_of_box_run_p;
23181 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23182 || it->phys_descent > it->descent);
23183 glyph->padding_p = 0;
23184 glyph->glyph_not_available_p = 0;
23185 glyph->face_id = it->face_id;
23186 glyph->font_type = FONT_TYPE_UNKNOWN;
23187 if (it->bidi_p)
23188 {
23189 glyph->resolved_level = it->bidi_it.resolved_level;
23190 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23191 abort ();
23192 glyph->bidi_type = it->bidi_it.type;
23193 }
23194 ++it->glyph_row->used[area];
23195 }
23196 else
23197 IT_EXPAND_MATRIX_WIDTH (it, area);
23198 }
23199
23200
23201 /* Change IT->ascent and IT->height according to the setting of
23202 IT->voffset. */
23203
23204 static inline void
23205 take_vertical_position_into_account (struct it *it)
23206 {
23207 if (it->voffset)
23208 {
23209 if (it->voffset < 0)
23210 /* Increase the ascent so that we can display the text higher
23211 in the line. */
23212 it->ascent -= it->voffset;
23213 else
23214 /* Increase the descent so that we can display the text lower
23215 in the line. */
23216 it->descent += it->voffset;
23217 }
23218 }
23219
23220
23221 /* Produce glyphs/get display metrics for the image IT is loaded with.
23222 See the description of struct display_iterator in dispextern.h for
23223 an overview of struct display_iterator. */
23224
23225 static void
23226 produce_image_glyph (struct it *it)
23227 {
23228 struct image *img;
23229 struct face *face;
23230 int glyph_ascent, crop;
23231 struct glyph_slice slice;
23232
23233 xassert (it->what == IT_IMAGE);
23234
23235 face = FACE_FROM_ID (it->f, it->face_id);
23236 xassert (face);
23237 /* Make sure X resources of the face is loaded. */
23238 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23239
23240 if (it->image_id < 0)
23241 {
23242 /* Fringe bitmap. */
23243 it->ascent = it->phys_ascent = 0;
23244 it->descent = it->phys_descent = 0;
23245 it->pixel_width = 0;
23246 it->nglyphs = 0;
23247 return;
23248 }
23249
23250 img = IMAGE_FROM_ID (it->f, it->image_id);
23251 xassert (img);
23252 /* Make sure X resources of the image is loaded. */
23253 prepare_image_for_display (it->f, img);
23254
23255 slice.x = slice.y = 0;
23256 slice.width = img->width;
23257 slice.height = img->height;
23258
23259 if (INTEGERP (it->slice.x))
23260 slice.x = XINT (it->slice.x);
23261 else if (FLOATP (it->slice.x))
23262 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23263
23264 if (INTEGERP (it->slice.y))
23265 slice.y = XINT (it->slice.y);
23266 else if (FLOATP (it->slice.y))
23267 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23268
23269 if (INTEGERP (it->slice.width))
23270 slice.width = XINT (it->slice.width);
23271 else if (FLOATP (it->slice.width))
23272 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23273
23274 if (INTEGERP (it->slice.height))
23275 slice.height = XINT (it->slice.height);
23276 else if (FLOATP (it->slice.height))
23277 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23278
23279 if (slice.x >= img->width)
23280 slice.x = img->width;
23281 if (slice.y >= img->height)
23282 slice.y = img->height;
23283 if (slice.x + slice.width >= img->width)
23284 slice.width = img->width - slice.x;
23285 if (slice.y + slice.height > img->height)
23286 slice.height = img->height - slice.y;
23287
23288 if (slice.width == 0 || slice.height == 0)
23289 return;
23290
23291 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23292
23293 it->descent = slice.height - glyph_ascent;
23294 if (slice.y == 0)
23295 it->descent += img->vmargin;
23296 if (slice.y + slice.height == img->height)
23297 it->descent += img->vmargin;
23298 it->phys_descent = it->descent;
23299
23300 it->pixel_width = slice.width;
23301 if (slice.x == 0)
23302 it->pixel_width += img->hmargin;
23303 if (slice.x + slice.width == img->width)
23304 it->pixel_width += img->hmargin;
23305
23306 /* It's quite possible for images to have an ascent greater than
23307 their height, so don't get confused in that case. */
23308 if (it->descent < 0)
23309 it->descent = 0;
23310
23311 it->nglyphs = 1;
23312
23313 if (face->box != FACE_NO_BOX)
23314 {
23315 if (face->box_line_width > 0)
23316 {
23317 if (slice.y == 0)
23318 it->ascent += face->box_line_width;
23319 if (slice.y + slice.height == img->height)
23320 it->descent += face->box_line_width;
23321 }
23322
23323 if (it->start_of_box_run_p && slice.x == 0)
23324 it->pixel_width += eabs (face->box_line_width);
23325 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23326 it->pixel_width += eabs (face->box_line_width);
23327 }
23328
23329 take_vertical_position_into_account (it);
23330
23331 /* Automatically crop wide image glyphs at right edge so we can
23332 draw the cursor on same display row. */
23333 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23334 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23335 {
23336 it->pixel_width -= crop;
23337 slice.width -= crop;
23338 }
23339
23340 if (it->glyph_row)
23341 {
23342 struct glyph *glyph;
23343 enum glyph_row_area area = it->area;
23344
23345 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23346 if (glyph < it->glyph_row->glyphs[area + 1])
23347 {
23348 glyph->charpos = CHARPOS (it->position);
23349 glyph->object = it->object;
23350 glyph->pixel_width = it->pixel_width;
23351 glyph->ascent = glyph_ascent;
23352 glyph->descent = it->descent;
23353 glyph->voffset = it->voffset;
23354 glyph->type = IMAGE_GLYPH;
23355 glyph->avoid_cursor_p = it->avoid_cursor_p;
23356 glyph->multibyte_p = it->multibyte_p;
23357 glyph->left_box_line_p = it->start_of_box_run_p;
23358 glyph->right_box_line_p = it->end_of_box_run_p;
23359 glyph->overlaps_vertically_p = 0;
23360 glyph->padding_p = 0;
23361 glyph->glyph_not_available_p = 0;
23362 glyph->face_id = it->face_id;
23363 glyph->u.img_id = img->id;
23364 glyph->slice.img = slice;
23365 glyph->font_type = FONT_TYPE_UNKNOWN;
23366 if (it->bidi_p)
23367 {
23368 glyph->resolved_level = it->bidi_it.resolved_level;
23369 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23370 abort ();
23371 glyph->bidi_type = it->bidi_it.type;
23372 }
23373 ++it->glyph_row->used[area];
23374 }
23375 else
23376 IT_EXPAND_MATRIX_WIDTH (it, area);
23377 }
23378 }
23379
23380
23381 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23382 of the glyph, WIDTH and HEIGHT are the width and height of the
23383 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23384
23385 static void
23386 append_stretch_glyph (struct it *it, Lisp_Object object,
23387 int width, int height, int ascent)
23388 {
23389 struct glyph *glyph;
23390 enum glyph_row_area area = it->area;
23391
23392 xassert (ascent >= 0 && ascent <= height);
23393
23394 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23395 if (glyph < it->glyph_row->glyphs[area + 1])
23396 {
23397 /* If the glyph row is reversed, we need to prepend the glyph
23398 rather than append it. */
23399 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23400 {
23401 struct glyph *g;
23402
23403 /* Make room for the additional glyph. */
23404 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23405 g[1] = *g;
23406 glyph = it->glyph_row->glyphs[area];
23407 }
23408 glyph->charpos = CHARPOS (it->position);
23409 glyph->object = object;
23410 glyph->pixel_width = width;
23411 glyph->ascent = ascent;
23412 glyph->descent = height - ascent;
23413 glyph->voffset = it->voffset;
23414 glyph->type = STRETCH_GLYPH;
23415 glyph->avoid_cursor_p = it->avoid_cursor_p;
23416 glyph->multibyte_p = it->multibyte_p;
23417 glyph->left_box_line_p = it->start_of_box_run_p;
23418 glyph->right_box_line_p = it->end_of_box_run_p;
23419 glyph->overlaps_vertically_p = 0;
23420 glyph->padding_p = 0;
23421 glyph->glyph_not_available_p = 0;
23422 glyph->face_id = it->face_id;
23423 glyph->u.stretch.ascent = ascent;
23424 glyph->u.stretch.height = height;
23425 glyph->slice.img = null_glyph_slice;
23426 glyph->font_type = FONT_TYPE_UNKNOWN;
23427 if (it->bidi_p)
23428 {
23429 glyph->resolved_level = it->bidi_it.resolved_level;
23430 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23431 abort ();
23432 glyph->bidi_type = it->bidi_it.type;
23433 }
23434 else
23435 {
23436 glyph->resolved_level = 0;
23437 glyph->bidi_type = UNKNOWN_BT;
23438 }
23439 ++it->glyph_row->used[area];
23440 }
23441 else
23442 IT_EXPAND_MATRIX_WIDTH (it, area);
23443 }
23444
23445 #endif /* HAVE_WINDOW_SYSTEM */
23446
23447 /* Produce a stretch glyph for iterator IT. IT->object is the value
23448 of the glyph property displayed. The value must be a list
23449 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23450 being recognized:
23451
23452 1. `:width WIDTH' specifies that the space should be WIDTH *
23453 canonical char width wide. WIDTH may be an integer or floating
23454 point number.
23455
23456 2. `:relative-width FACTOR' specifies that the width of the stretch
23457 should be computed from the width of the first character having the
23458 `glyph' property, and should be FACTOR times that width.
23459
23460 3. `:align-to HPOS' specifies that the space should be wide enough
23461 to reach HPOS, a value in canonical character units.
23462
23463 Exactly one of the above pairs must be present.
23464
23465 4. `:height HEIGHT' specifies that the height of the stretch produced
23466 should be HEIGHT, measured in canonical character units.
23467
23468 5. `:relative-height FACTOR' specifies that the height of the
23469 stretch should be FACTOR times the height of the characters having
23470 the glyph property.
23471
23472 Either none or exactly one of 4 or 5 must be present.
23473
23474 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23475 of the stretch should be used for the ascent of the stretch.
23476 ASCENT must be in the range 0 <= ASCENT <= 100. */
23477
23478 void
23479 produce_stretch_glyph (struct it *it)
23480 {
23481 /* (space :width WIDTH :height HEIGHT ...) */
23482 Lisp_Object prop, plist;
23483 int width = 0, height = 0, align_to = -1;
23484 int zero_width_ok_p = 0;
23485 int ascent = 0;
23486 double tem;
23487 struct face *face = NULL;
23488 struct font *font = NULL;
23489
23490 #ifdef HAVE_WINDOW_SYSTEM
23491 int zero_height_ok_p = 0;
23492
23493 if (FRAME_WINDOW_P (it->f))
23494 {
23495 face = FACE_FROM_ID (it->f, it->face_id);
23496 font = face->font ? face->font : FRAME_FONT (it->f);
23497 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23498 }
23499 #endif
23500
23501 /* List should start with `space'. */
23502 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23503 plist = XCDR (it->object);
23504
23505 /* Compute the width of the stretch. */
23506 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23507 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23508 {
23509 /* Absolute width `:width WIDTH' specified and valid. */
23510 zero_width_ok_p = 1;
23511 width = (int)tem;
23512 }
23513 #ifdef HAVE_WINDOW_SYSTEM
23514 else if (FRAME_WINDOW_P (it->f)
23515 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23516 {
23517 /* Relative width `:relative-width FACTOR' specified and valid.
23518 Compute the width of the characters having the `glyph'
23519 property. */
23520 struct it it2;
23521 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23522
23523 it2 = *it;
23524 if (it->multibyte_p)
23525 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23526 else
23527 {
23528 it2.c = it2.char_to_display = *p, it2.len = 1;
23529 if (! ASCII_CHAR_P (it2.c))
23530 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23531 }
23532
23533 it2.glyph_row = NULL;
23534 it2.what = IT_CHARACTER;
23535 x_produce_glyphs (&it2);
23536 width = NUMVAL (prop) * it2.pixel_width;
23537 }
23538 #endif /* HAVE_WINDOW_SYSTEM */
23539 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23540 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23541 {
23542 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23543 align_to = (align_to < 0
23544 ? 0
23545 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23546 else if (align_to < 0)
23547 align_to = window_box_left_offset (it->w, TEXT_AREA);
23548 width = max (0, (int)tem + align_to - it->current_x);
23549 zero_width_ok_p = 1;
23550 }
23551 else
23552 /* Nothing specified -> width defaults to canonical char width. */
23553 width = FRAME_COLUMN_WIDTH (it->f);
23554
23555 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23556 width = 1;
23557
23558 #ifdef HAVE_WINDOW_SYSTEM
23559 /* Compute height. */
23560 if (FRAME_WINDOW_P (it->f))
23561 {
23562 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23563 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23564 {
23565 height = (int)tem;
23566 zero_height_ok_p = 1;
23567 }
23568 else if (prop = Fplist_get (plist, QCrelative_height),
23569 NUMVAL (prop) > 0)
23570 height = FONT_HEIGHT (font) * NUMVAL (prop);
23571 else
23572 height = FONT_HEIGHT (font);
23573
23574 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23575 height = 1;
23576
23577 /* Compute percentage of height used for ascent. If
23578 `:ascent ASCENT' is present and valid, use that. Otherwise,
23579 derive the ascent from the font in use. */
23580 if (prop = Fplist_get (plist, QCascent),
23581 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23582 ascent = height * NUMVAL (prop) / 100.0;
23583 else if (!NILP (prop)
23584 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23585 ascent = min (max (0, (int)tem), height);
23586 else
23587 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23588 }
23589 else
23590 #endif /* HAVE_WINDOW_SYSTEM */
23591 height = 1;
23592
23593 if (width > 0 && it->line_wrap != TRUNCATE
23594 && it->current_x + width > it->last_visible_x)
23595 {
23596 width = it->last_visible_x - it->current_x;
23597 #ifdef HAVE_WINDOW_SYSTEM
23598 /* Subtact one more pixel from the stretch width, but only on
23599 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23600 width -= FRAME_WINDOW_P (it->f);
23601 #endif
23602 }
23603
23604 if (width > 0 && height > 0 && it->glyph_row)
23605 {
23606 Lisp_Object o_object = it->object;
23607 Lisp_Object object = it->stack[it->sp - 1].string;
23608 int n = width;
23609
23610 if (!STRINGP (object))
23611 object = it->w->buffer;
23612 #ifdef HAVE_WINDOW_SYSTEM
23613 if (FRAME_WINDOW_P (it->f))
23614 append_stretch_glyph (it, object, width, height, ascent);
23615 else
23616 #endif
23617 {
23618 it->object = object;
23619 it->char_to_display = ' ';
23620 it->pixel_width = it->len = 1;
23621 while (n--)
23622 tty_append_glyph (it);
23623 it->object = o_object;
23624 }
23625 }
23626
23627 it->pixel_width = width;
23628 #ifdef HAVE_WINDOW_SYSTEM
23629 if (FRAME_WINDOW_P (it->f))
23630 {
23631 it->ascent = it->phys_ascent = ascent;
23632 it->descent = it->phys_descent = height - it->ascent;
23633 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23634 take_vertical_position_into_account (it);
23635 }
23636 else
23637 #endif
23638 it->nglyphs = width;
23639 }
23640
23641 #ifdef HAVE_WINDOW_SYSTEM
23642
23643 /* Calculate line-height and line-spacing properties.
23644 An integer value specifies explicit pixel value.
23645 A float value specifies relative value to current face height.
23646 A cons (float . face-name) specifies relative value to
23647 height of specified face font.
23648
23649 Returns height in pixels, or nil. */
23650
23651
23652 static Lisp_Object
23653 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23654 int boff, int override)
23655 {
23656 Lisp_Object face_name = Qnil;
23657 int ascent, descent, height;
23658
23659 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23660 return val;
23661
23662 if (CONSP (val))
23663 {
23664 face_name = XCAR (val);
23665 val = XCDR (val);
23666 if (!NUMBERP (val))
23667 val = make_number (1);
23668 if (NILP (face_name))
23669 {
23670 height = it->ascent + it->descent;
23671 goto scale;
23672 }
23673 }
23674
23675 if (NILP (face_name))
23676 {
23677 font = FRAME_FONT (it->f);
23678 boff = FRAME_BASELINE_OFFSET (it->f);
23679 }
23680 else if (EQ (face_name, Qt))
23681 {
23682 override = 0;
23683 }
23684 else
23685 {
23686 int face_id;
23687 struct face *face;
23688
23689 face_id = lookup_named_face (it->f, face_name, 0);
23690 if (face_id < 0)
23691 return make_number (-1);
23692
23693 face = FACE_FROM_ID (it->f, face_id);
23694 font = face->font;
23695 if (font == NULL)
23696 return make_number (-1);
23697 boff = font->baseline_offset;
23698 if (font->vertical_centering)
23699 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23700 }
23701
23702 ascent = FONT_BASE (font) + boff;
23703 descent = FONT_DESCENT (font) - boff;
23704
23705 if (override)
23706 {
23707 it->override_ascent = ascent;
23708 it->override_descent = descent;
23709 it->override_boff = boff;
23710 }
23711
23712 height = ascent + descent;
23713
23714 scale:
23715 if (FLOATP (val))
23716 height = (int)(XFLOAT_DATA (val) * height);
23717 else if (INTEGERP (val))
23718 height *= XINT (val);
23719
23720 return make_number (height);
23721 }
23722
23723
23724 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23725 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23726 and only if this is for a character for which no font was found.
23727
23728 If the display method (it->glyphless_method) is
23729 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23730 length of the acronym or the hexadecimal string, UPPER_XOFF and
23731 UPPER_YOFF are pixel offsets for the upper part of the string,
23732 LOWER_XOFF and LOWER_YOFF are for the lower part.
23733
23734 For the other display methods, LEN through LOWER_YOFF are zero. */
23735
23736 static void
23737 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23738 short upper_xoff, short upper_yoff,
23739 short lower_xoff, short lower_yoff)
23740 {
23741 struct glyph *glyph;
23742 enum glyph_row_area area = it->area;
23743
23744 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23745 if (glyph < it->glyph_row->glyphs[area + 1])
23746 {
23747 /* If the glyph row is reversed, we need to prepend the glyph
23748 rather than append it. */
23749 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23750 {
23751 struct glyph *g;
23752
23753 /* Make room for the additional glyph. */
23754 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23755 g[1] = *g;
23756 glyph = it->glyph_row->glyphs[area];
23757 }
23758 glyph->charpos = CHARPOS (it->position);
23759 glyph->object = it->object;
23760 glyph->pixel_width = it->pixel_width;
23761 glyph->ascent = it->ascent;
23762 glyph->descent = it->descent;
23763 glyph->voffset = it->voffset;
23764 glyph->type = GLYPHLESS_GLYPH;
23765 glyph->u.glyphless.method = it->glyphless_method;
23766 glyph->u.glyphless.for_no_font = for_no_font;
23767 glyph->u.glyphless.len = len;
23768 glyph->u.glyphless.ch = it->c;
23769 glyph->slice.glyphless.upper_xoff = upper_xoff;
23770 glyph->slice.glyphless.upper_yoff = upper_yoff;
23771 glyph->slice.glyphless.lower_xoff = lower_xoff;
23772 glyph->slice.glyphless.lower_yoff = lower_yoff;
23773 glyph->avoid_cursor_p = it->avoid_cursor_p;
23774 glyph->multibyte_p = it->multibyte_p;
23775 glyph->left_box_line_p = it->start_of_box_run_p;
23776 glyph->right_box_line_p = it->end_of_box_run_p;
23777 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23778 || it->phys_descent > it->descent);
23779 glyph->padding_p = 0;
23780 glyph->glyph_not_available_p = 0;
23781 glyph->face_id = face_id;
23782 glyph->font_type = FONT_TYPE_UNKNOWN;
23783 if (it->bidi_p)
23784 {
23785 glyph->resolved_level = it->bidi_it.resolved_level;
23786 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23787 abort ();
23788 glyph->bidi_type = it->bidi_it.type;
23789 }
23790 ++it->glyph_row->used[area];
23791 }
23792 else
23793 IT_EXPAND_MATRIX_WIDTH (it, area);
23794 }
23795
23796
23797 /* Produce a glyph for a glyphless character for iterator IT.
23798 IT->glyphless_method specifies which method to use for displaying
23799 the character. See the description of enum
23800 glyphless_display_method in dispextern.h for the detail.
23801
23802 FOR_NO_FONT is nonzero if and only if this is for a character for
23803 which no font was found. ACRONYM, if non-nil, is an acronym string
23804 for the character. */
23805
23806 static void
23807 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23808 {
23809 int face_id;
23810 struct face *face;
23811 struct font *font;
23812 int base_width, base_height, width, height;
23813 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23814 int len;
23815
23816 /* Get the metrics of the base font. We always refer to the current
23817 ASCII face. */
23818 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23819 font = face->font ? face->font : FRAME_FONT (it->f);
23820 it->ascent = FONT_BASE (font) + font->baseline_offset;
23821 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23822 base_height = it->ascent + it->descent;
23823 base_width = font->average_width;
23824
23825 /* Get a face ID for the glyph by utilizing a cache (the same way as
23826 done for `escape-glyph' in get_next_display_element). */
23827 if (it->f == last_glyphless_glyph_frame
23828 && it->face_id == last_glyphless_glyph_face_id)
23829 {
23830 face_id = last_glyphless_glyph_merged_face_id;
23831 }
23832 else
23833 {
23834 /* Merge the `glyphless-char' face into the current face. */
23835 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23836 last_glyphless_glyph_frame = it->f;
23837 last_glyphless_glyph_face_id = it->face_id;
23838 last_glyphless_glyph_merged_face_id = face_id;
23839 }
23840
23841 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23842 {
23843 it->pixel_width = THIN_SPACE_WIDTH;
23844 len = 0;
23845 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23846 }
23847 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23848 {
23849 width = CHAR_WIDTH (it->c);
23850 if (width == 0)
23851 width = 1;
23852 else if (width > 4)
23853 width = 4;
23854 it->pixel_width = base_width * width;
23855 len = 0;
23856 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23857 }
23858 else
23859 {
23860 char buf[7];
23861 const char *str;
23862 unsigned int code[6];
23863 int upper_len;
23864 int ascent, descent;
23865 struct font_metrics metrics_upper, metrics_lower;
23866
23867 face = FACE_FROM_ID (it->f, face_id);
23868 font = face->font ? face->font : FRAME_FONT (it->f);
23869 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23870
23871 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23872 {
23873 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23874 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23875 if (CONSP (acronym))
23876 acronym = XCAR (acronym);
23877 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23878 }
23879 else
23880 {
23881 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23882 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23883 str = buf;
23884 }
23885 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23886 code[len] = font->driver->encode_char (font, str[len]);
23887 upper_len = (len + 1) / 2;
23888 font->driver->text_extents (font, code, upper_len,
23889 &metrics_upper);
23890 font->driver->text_extents (font, code + upper_len, len - upper_len,
23891 &metrics_lower);
23892
23893
23894
23895 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23896 width = max (metrics_upper.width, metrics_lower.width) + 4;
23897 upper_xoff = upper_yoff = 2; /* the typical case */
23898 if (base_width >= width)
23899 {
23900 /* Align the upper to the left, the lower to the right. */
23901 it->pixel_width = base_width;
23902 lower_xoff = base_width - 2 - metrics_lower.width;
23903 }
23904 else
23905 {
23906 /* Center the shorter one. */
23907 it->pixel_width = width;
23908 if (metrics_upper.width >= metrics_lower.width)
23909 lower_xoff = (width - metrics_lower.width) / 2;
23910 else
23911 {
23912 /* FIXME: This code doesn't look right. It formerly was
23913 missing the "lower_xoff = 0;", which couldn't have
23914 been right since it left lower_xoff uninitialized. */
23915 lower_xoff = 0;
23916 upper_xoff = (width - metrics_upper.width) / 2;
23917 }
23918 }
23919
23920 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23921 top, bottom, and between upper and lower strings. */
23922 height = (metrics_upper.ascent + metrics_upper.descent
23923 + metrics_lower.ascent + metrics_lower.descent) + 5;
23924 /* Center vertically.
23925 H:base_height, D:base_descent
23926 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23927
23928 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23929 descent = D - H/2 + h/2;
23930 lower_yoff = descent - 2 - ld;
23931 upper_yoff = lower_yoff - la - 1 - ud; */
23932 ascent = - (it->descent - (base_height + height + 1) / 2);
23933 descent = it->descent - (base_height - height) / 2;
23934 lower_yoff = descent - 2 - metrics_lower.descent;
23935 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23936 - metrics_upper.descent);
23937 /* Don't make the height shorter than the base height. */
23938 if (height > base_height)
23939 {
23940 it->ascent = ascent;
23941 it->descent = descent;
23942 }
23943 }
23944
23945 it->phys_ascent = it->ascent;
23946 it->phys_descent = it->descent;
23947 if (it->glyph_row)
23948 append_glyphless_glyph (it, face_id, for_no_font, len,
23949 upper_xoff, upper_yoff,
23950 lower_xoff, lower_yoff);
23951 it->nglyphs = 1;
23952 take_vertical_position_into_account (it);
23953 }
23954
23955
23956 /* RIF:
23957 Produce glyphs/get display metrics for the display element IT is
23958 loaded with. See the description of struct it in dispextern.h
23959 for an overview of struct it. */
23960
23961 void
23962 x_produce_glyphs (struct it *it)
23963 {
23964 int extra_line_spacing = it->extra_line_spacing;
23965
23966 it->glyph_not_available_p = 0;
23967
23968 if (it->what == IT_CHARACTER)
23969 {
23970 XChar2b char2b;
23971 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23972 struct font *font = face->font;
23973 struct font_metrics *pcm = NULL;
23974 int boff; /* baseline offset */
23975
23976 if (font == NULL)
23977 {
23978 /* When no suitable font is found, display this character by
23979 the method specified in the first extra slot of
23980 Vglyphless_char_display. */
23981 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23982
23983 xassert (it->what == IT_GLYPHLESS);
23984 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23985 goto done;
23986 }
23987
23988 boff = font->baseline_offset;
23989 if (font->vertical_centering)
23990 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23991
23992 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23993 {
23994 int stretched_p;
23995
23996 it->nglyphs = 1;
23997
23998 if (it->override_ascent >= 0)
23999 {
24000 it->ascent = it->override_ascent;
24001 it->descent = it->override_descent;
24002 boff = it->override_boff;
24003 }
24004 else
24005 {
24006 it->ascent = FONT_BASE (font) + boff;
24007 it->descent = FONT_DESCENT (font) - boff;
24008 }
24009
24010 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24011 {
24012 pcm = get_per_char_metric (font, &char2b);
24013 if (pcm->width == 0
24014 && pcm->rbearing == 0 && pcm->lbearing == 0)
24015 pcm = NULL;
24016 }
24017
24018 if (pcm)
24019 {
24020 it->phys_ascent = pcm->ascent + boff;
24021 it->phys_descent = pcm->descent - boff;
24022 it->pixel_width = pcm->width;
24023 }
24024 else
24025 {
24026 it->glyph_not_available_p = 1;
24027 it->phys_ascent = it->ascent;
24028 it->phys_descent = it->descent;
24029 it->pixel_width = font->space_width;
24030 }
24031
24032 if (it->constrain_row_ascent_descent_p)
24033 {
24034 if (it->descent > it->max_descent)
24035 {
24036 it->ascent += it->descent - it->max_descent;
24037 it->descent = it->max_descent;
24038 }
24039 if (it->ascent > it->max_ascent)
24040 {
24041 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24042 it->ascent = it->max_ascent;
24043 }
24044 it->phys_ascent = min (it->phys_ascent, it->ascent);
24045 it->phys_descent = min (it->phys_descent, it->descent);
24046 extra_line_spacing = 0;
24047 }
24048
24049 /* If this is a space inside a region of text with
24050 `space-width' property, change its width. */
24051 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24052 if (stretched_p)
24053 it->pixel_width *= XFLOATINT (it->space_width);
24054
24055 /* If face has a box, add the box thickness to the character
24056 height. If character has a box line to the left and/or
24057 right, add the box line width to the character's width. */
24058 if (face->box != FACE_NO_BOX)
24059 {
24060 int thick = face->box_line_width;
24061
24062 if (thick > 0)
24063 {
24064 it->ascent += thick;
24065 it->descent += thick;
24066 }
24067 else
24068 thick = -thick;
24069
24070 if (it->start_of_box_run_p)
24071 it->pixel_width += thick;
24072 if (it->end_of_box_run_p)
24073 it->pixel_width += thick;
24074 }
24075
24076 /* If face has an overline, add the height of the overline
24077 (1 pixel) and a 1 pixel margin to the character height. */
24078 if (face->overline_p)
24079 it->ascent += overline_margin;
24080
24081 if (it->constrain_row_ascent_descent_p)
24082 {
24083 if (it->ascent > it->max_ascent)
24084 it->ascent = it->max_ascent;
24085 if (it->descent > it->max_descent)
24086 it->descent = it->max_descent;
24087 }
24088
24089 take_vertical_position_into_account (it);
24090
24091 /* If we have to actually produce glyphs, do it. */
24092 if (it->glyph_row)
24093 {
24094 if (stretched_p)
24095 {
24096 /* Translate a space with a `space-width' property
24097 into a stretch glyph. */
24098 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24099 / FONT_HEIGHT (font));
24100 append_stretch_glyph (it, it->object, it->pixel_width,
24101 it->ascent + it->descent, ascent);
24102 }
24103 else
24104 append_glyph (it);
24105
24106 /* If characters with lbearing or rbearing are displayed
24107 in this line, record that fact in a flag of the
24108 glyph row. This is used to optimize X output code. */
24109 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24110 it->glyph_row->contains_overlapping_glyphs_p = 1;
24111 }
24112 if (! stretched_p && it->pixel_width == 0)
24113 /* We assure that all visible glyphs have at least 1-pixel
24114 width. */
24115 it->pixel_width = 1;
24116 }
24117 else if (it->char_to_display == '\n')
24118 {
24119 /* A newline has no width, but we need the height of the
24120 line. But if previous part of the line sets a height,
24121 don't increase that height */
24122
24123 Lisp_Object height;
24124 Lisp_Object total_height = Qnil;
24125
24126 it->override_ascent = -1;
24127 it->pixel_width = 0;
24128 it->nglyphs = 0;
24129
24130 height = get_it_property (it, Qline_height);
24131 /* Split (line-height total-height) list */
24132 if (CONSP (height)
24133 && CONSP (XCDR (height))
24134 && NILP (XCDR (XCDR (height))))
24135 {
24136 total_height = XCAR (XCDR (height));
24137 height = XCAR (height);
24138 }
24139 height = calc_line_height_property (it, height, font, boff, 1);
24140
24141 if (it->override_ascent >= 0)
24142 {
24143 it->ascent = it->override_ascent;
24144 it->descent = it->override_descent;
24145 boff = it->override_boff;
24146 }
24147 else
24148 {
24149 it->ascent = FONT_BASE (font) + boff;
24150 it->descent = FONT_DESCENT (font) - boff;
24151 }
24152
24153 if (EQ (height, Qt))
24154 {
24155 if (it->descent > it->max_descent)
24156 {
24157 it->ascent += it->descent - it->max_descent;
24158 it->descent = it->max_descent;
24159 }
24160 if (it->ascent > it->max_ascent)
24161 {
24162 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24163 it->ascent = it->max_ascent;
24164 }
24165 it->phys_ascent = min (it->phys_ascent, it->ascent);
24166 it->phys_descent = min (it->phys_descent, it->descent);
24167 it->constrain_row_ascent_descent_p = 1;
24168 extra_line_spacing = 0;
24169 }
24170 else
24171 {
24172 Lisp_Object spacing;
24173
24174 it->phys_ascent = it->ascent;
24175 it->phys_descent = it->descent;
24176
24177 if ((it->max_ascent > 0 || it->max_descent > 0)
24178 && face->box != FACE_NO_BOX
24179 && face->box_line_width > 0)
24180 {
24181 it->ascent += face->box_line_width;
24182 it->descent += face->box_line_width;
24183 }
24184 if (!NILP (height)
24185 && XINT (height) > it->ascent + it->descent)
24186 it->ascent = XINT (height) - it->descent;
24187
24188 if (!NILP (total_height))
24189 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24190 else
24191 {
24192 spacing = get_it_property (it, Qline_spacing);
24193 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24194 }
24195 if (INTEGERP (spacing))
24196 {
24197 extra_line_spacing = XINT (spacing);
24198 if (!NILP (total_height))
24199 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24200 }
24201 }
24202 }
24203 else /* i.e. (it->char_to_display == '\t') */
24204 {
24205 if (font->space_width > 0)
24206 {
24207 int tab_width = it->tab_width * font->space_width;
24208 int x = it->current_x + it->continuation_lines_width;
24209 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24210
24211 /* If the distance from the current position to the next tab
24212 stop is less than a space character width, use the
24213 tab stop after that. */
24214 if (next_tab_x - x < font->space_width)
24215 next_tab_x += tab_width;
24216
24217 it->pixel_width = next_tab_x - x;
24218 it->nglyphs = 1;
24219 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24220 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24221
24222 if (it->glyph_row)
24223 {
24224 append_stretch_glyph (it, it->object, it->pixel_width,
24225 it->ascent + it->descent, it->ascent);
24226 }
24227 }
24228 else
24229 {
24230 it->pixel_width = 0;
24231 it->nglyphs = 1;
24232 }
24233 }
24234 }
24235 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24236 {
24237 /* A static composition.
24238
24239 Note: A composition is represented as one glyph in the
24240 glyph matrix. There are no padding glyphs.
24241
24242 Important note: pixel_width, ascent, and descent are the
24243 values of what is drawn by draw_glyphs (i.e. the values of
24244 the overall glyphs composed). */
24245 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24246 int boff; /* baseline offset */
24247 struct composition *cmp = composition_table[it->cmp_it.id];
24248 int glyph_len = cmp->glyph_len;
24249 struct font *font = face->font;
24250
24251 it->nglyphs = 1;
24252
24253 /* If we have not yet calculated pixel size data of glyphs of
24254 the composition for the current face font, calculate them
24255 now. Theoretically, we have to check all fonts for the
24256 glyphs, but that requires much time and memory space. So,
24257 here we check only the font of the first glyph. This may
24258 lead to incorrect display, but it's very rare, and C-l
24259 (recenter-top-bottom) can correct the display anyway. */
24260 if (! cmp->font || cmp->font != font)
24261 {
24262 /* Ascent and descent of the font of the first character
24263 of this composition (adjusted by baseline offset).
24264 Ascent and descent of overall glyphs should not be less
24265 than these, respectively. */
24266 int font_ascent, font_descent, font_height;
24267 /* Bounding box of the overall glyphs. */
24268 int leftmost, rightmost, lowest, highest;
24269 int lbearing, rbearing;
24270 int i, width, ascent, descent;
24271 int left_padded = 0, right_padded = 0;
24272 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24273 XChar2b char2b;
24274 struct font_metrics *pcm;
24275 int font_not_found_p;
24276 ptrdiff_t pos;
24277
24278 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24279 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24280 break;
24281 if (glyph_len < cmp->glyph_len)
24282 right_padded = 1;
24283 for (i = 0; i < glyph_len; i++)
24284 {
24285 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24286 break;
24287 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24288 }
24289 if (i > 0)
24290 left_padded = 1;
24291
24292 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24293 : IT_CHARPOS (*it));
24294 /* If no suitable font is found, use the default font. */
24295 font_not_found_p = font == NULL;
24296 if (font_not_found_p)
24297 {
24298 face = face->ascii_face;
24299 font = face->font;
24300 }
24301 boff = font->baseline_offset;
24302 if (font->vertical_centering)
24303 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24304 font_ascent = FONT_BASE (font) + boff;
24305 font_descent = FONT_DESCENT (font) - boff;
24306 font_height = FONT_HEIGHT (font);
24307
24308 cmp->font = (void *) font;
24309
24310 pcm = NULL;
24311 if (! font_not_found_p)
24312 {
24313 get_char_face_and_encoding (it->f, c, it->face_id,
24314 &char2b, 0);
24315 pcm = get_per_char_metric (font, &char2b);
24316 }
24317
24318 /* Initialize the bounding box. */
24319 if (pcm)
24320 {
24321 width = pcm->width;
24322 ascent = pcm->ascent;
24323 descent = pcm->descent;
24324 lbearing = pcm->lbearing;
24325 rbearing = pcm->rbearing;
24326 }
24327 else
24328 {
24329 width = font->space_width;
24330 ascent = FONT_BASE (font);
24331 descent = FONT_DESCENT (font);
24332 lbearing = 0;
24333 rbearing = width;
24334 }
24335
24336 rightmost = width;
24337 leftmost = 0;
24338 lowest = - descent + boff;
24339 highest = ascent + boff;
24340
24341 if (! font_not_found_p
24342 && font->default_ascent
24343 && CHAR_TABLE_P (Vuse_default_ascent)
24344 && !NILP (Faref (Vuse_default_ascent,
24345 make_number (it->char_to_display))))
24346 highest = font->default_ascent + boff;
24347
24348 /* Draw the first glyph at the normal position. It may be
24349 shifted to right later if some other glyphs are drawn
24350 at the left. */
24351 cmp->offsets[i * 2] = 0;
24352 cmp->offsets[i * 2 + 1] = boff;
24353 cmp->lbearing = lbearing;
24354 cmp->rbearing = rbearing;
24355
24356 /* Set cmp->offsets for the remaining glyphs. */
24357 for (i++; i < glyph_len; i++)
24358 {
24359 int left, right, btm, top;
24360 int ch = COMPOSITION_GLYPH (cmp, i);
24361 int face_id;
24362 struct face *this_face;
24363
24364 if (ch == '\t')
24365 ch = ' ';
24366 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24367 this_face = FACE_FROM_ID (it->f, face_id);
24368 font = this_face->font;
24369
24370 if (font == NULL)
24371 pcm = NULL;
24372 else
24373 {
24374 get_char_face_and_encoding (it->f, ch, face_id,
24375 &char2b, 0);
24376 pcm = get_per_char_metric (font, &char2b);
24377 }
24378 if (! pcm)
24379 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24380 else
24381 {
24382 width = pcm->width;
24383 ascent = pcm->ascent;
24384 descent = pcm->descent;
24385 lbearing = pcm->lbearing;
24386 rbearing = pcm->rbearing;
24387 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24388 {
24389 /* Relative composition with or without
24390 alternate chars. */
24391 left = (leftmost + rightmost - width) / 2;
24392 btm = - descent + boff;
24393 if (font->relative_compose
24394 && (! CHAR_TABLE_P (Vignore_relative_composition)
24395 || NILP (Faref (Vignore_relative_composition,
24396 make_number (ch)))))
24397 {
24398
24399 if (- descent >= font->relative_compose)
24400 /* One extra pixel between two glyphs. */
24401 btm = highest + 1;
24402 else if (ascent <= 0)
24403 /* One extra pixel between two glyphs. */
24404 btm = lowest - 1 - ascent - descent;
24405 }
24406 }
24407 else
24408 {
24409 /* A composition rule is specified by an integer
24410 value that encodes global and new reference
24411 points (GREF and NREF). GREF and NREF are
24412 specified by numbers as below:
24413
24414 0---1---2 -- ascent
24415 | |
24416 | |
24417 | |
24418 9--10--11 -- center
24419 | |
24420 ---3---4---5--- baseline
24421 | |
24422 6---7---8 -- descent
24423 */
24424 int rule = COMPOSITION_RULE (cmp, i);
24425 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24426
24427 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24428 grefx = gref % 3, nrefx = nref % 3;
24429 grefy = gref / 3, nrefy = nref / 3;
24430 if (xoff)
24431 xoff = font_height * (xoff - 128) / 256;
24432 if (yoff)
24433 yoff = font_height * (yoff - 128) / 256;
24434
24435 left = (leftmost
24436 + grefx * (rightmost - leftmost) / 2
24437 - nrefx * width / 2
24438 + xoff);
24439
24440 btm = ((grefy == 0 ? highest
24441 : grefy == 1 ? 0
24442 : grefy == 2 ? lowest
24443 : (highest + lowest) / 2)
24444 - (nrefy == 0 ? ascent + descent
24445 : nrefy == 1 ? descent - boff
24446 : nrefy == 2 ? 0
24447 : (ascent + descent) / 2)
24448 + yoff);
24449 }
24450
24451 cmp->offsets[i * 2] = left;
24452 cmp->offsets[i * 2 + 1] = btm + descent;
24453
24454 /* Update the bounding box of the overall glyphs. */
24455 if (width > 0)
24456 {
24457 right = left + width;
24458 if (left < leftmost)
24459 leftmost = left;
24460 if (right > rightmost)
24461 rightmost = right;
24462 }
24463 top = btm + descent + ascent;
24464 if (top > highest)
24465 highest = top;
24466 if (btm < lowest)
24467 lowest = btm;
24468
24469 if (cmp->lbearing > left + lbearing)
24470 cmp->lbearing = left + lbearing;
24471 if (cmp->rbearing < left + rbearing)
24472 cmp->rbearing = left + rbearing;
24473 }
24474 }
24475
24476 /* If there are glyphs whose x-offsets are negative,
24477 shift all glyphs to the right and make all x-offsets
24478 non-negative. */
24479 if (leftmost < 0)
24480 {
24481 for (i = 0; i < cmp->glyph_len; i++)
24482 cmp->offsets[i * 2] -= leftmost;
24483 rightmost -= leftmost;
24484 cmp->lbearing -= leftmost;
24485 cmp->rbearing -= leftmost;
24486 }
24487
24488 if (left_padded && cmp->lbearing < 0)
24489 {
24490 for (i = 0; i < cmp->glyph_len; i++)
24491 cmp->offsets[i * 2] -= cmp->lbearing;
24492 rightmost -= cmp->lbearing;
24493 cmp->rbearing -= cmp->lbearing;
24494 cmp->lbearing = 0;
24495 }
24496 if (right_padded && rightmost < cmp->rbearing)
24497 {
24498 rightmost = cmp->rbearing;
24499 }
24500
24501 cmp->pixel_width = rightmost;
24502 cmp->ascent = highest;
24503 cmp->descent = - lowest;
24504 if (cmp->ascent < font_ascent)
24505 cmp->ascent = font_ascent;
24506 if (cmp->descent < font_descent)
24507 cmp->descent = font_descent;
24508 }
24509
24510 if (it->glyph_row
24511 && (cmp->lbearing < 0
24512 || cmp->rbearing > cmp->pixel_width))
24513 it->glyph_row->contains_overlapping_glyphs_p = 1;
24514
24515 it->pixel_width = cmp->pixel_width;
24516 it->ascent = it->phys_ascent = cmp->ascent;
24517 it->descent = it->phys_descent = cmp->descent;
24518 if (face->box != FACE_NO_BOX)
24519 {
24520 int thick = face->box_line_width;
24521
24522 if (thick > 0)
24523 {
24524 it->ascent += thick;
24525 it->descent += thick;
24526 }
24527 else
24528 thick = - thick;
24529
24530 if (it->start_of_box_run_p)
24531 it->pixel_width += thick;
24532 if (it->end_of_box_run_p)
24533 it->pixel_width += thick;
24534 }
24535
24536 /* If face has an overline, add the height of the overline
24537 (1 pixel) and a 1 pixel margin to the character height. */
24538 if (face->overline_p)
24539 it->ascent += overline_margin;
24540
24541 take_vertical_position_into_account (it);
24542 if (it->ascent < 0)
24543 it->ascent = 0;
24544 if (it->descent < 0)
24545 it->descent = 0;
24546
24547 if (it->glyph_row)
24548 append_composite_glyph (it);
24549 }
24550 else if (it->what == IT_COMPOSITION)
24551 {
24552 /* A dynamic (automatic) composition. */
24553 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24554 Lisp_Object gstring;
24555 struct font_metrics metrics;
24556
24557 it->nglyphs = 1;
24558
24559 gstring = composition_gstring_from_id (it->cmp_it.id);
24560 it->pixel_width
24561 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24562 &metrics);
24563 if (it->glyph_row
24564 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24565 it->glyph_row->contains_overlapping_glyphs_p = 1;
24566 it->ascent = it->phys_ascent = metrics.ascent;
24567 it->descent = it->phys_descent = metrics.descent;
24568 if (face->box != FACE_NO_BOX)
24569 {
24570 int thick = face->box_line_width;
24571
24572 if (thick > 0)
24573 {
24574 it->ascent += thick;
24575 it->descent += thick;
24576 }
24577 else
24578 thick = - thick;
24579
24580 if (it->start_of_box_run_p)
24581 it->pixel_width += thick;
24582 if (it->end_of_box_run_p)
24583 it->pixel_width += thick;
24584 }
24585 /* If face has an overline, add the height of the overline
24586 (1 pixel) and a 1 pixel margin to the character height. */
24587 if (face->overline_p)
24588 it->ascent += overline_margin;
24589 take_vertical_position_into_account (it);
24590 if (it->ascent < 0)
24591 it->ascent = 0;
24592 if (it->descent < 0)
24593 it->descent = 0;
24594
24595 if (it->glyph_row)
24596 append_composite_glyph (it);
24597 }
24598 else if (it->what == IT_GLYPHLESS)
24599 produce_glyphless_glyph (it, 0, Qnil);
24600 else if (it->what == IT_IMAGE)
24601 produce_image_glyph (it);
24602 else if (it->what == IT_STRETCH)
24603 produce_stretch_glyph (it);
24604
24605 done:
24606 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24607 because this isn't true for images with `:ascent 100'. */
24608 xassert (it->ascent >= 0 && it->descent >= 0);
24609 if (it->area == TEXT_AREA)
24610 it->current_x += it->pixel_width;
24611
24612 if (extra_line_spacing > 0)
24613 {
24614 it->descent += extra_line_spacing;
24615 if (extra_line_spacing > it->max_extra_line_spacing)
24616 it->max_extra_line_spacing = extra_line_spacing;
24617 }
24618
24619 it->max_ascent = max (it->max_ascent, it->ascent);
24620 it->max_descent = max (it->max_descent, it->descent);
24621 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24622 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24623 }
24624
24625 /* EXPORT for RIF:
24626 Output LEN glyphs starting at START at the nominal cursor position.
24627 Advance the nominal cursor over the text. The global variable
24628 updated_window contains the window being updated, updated_row is
24629 the glyph row being updated, and updated_area is the area of that
24630 row being updated. */
24631
24632 void
24633 x_write_glyphs (struct glyph *start, int len)
24634 {
24635 int x, hpos;
24636
24637 xassert (updated_window && updated_row);
24638 BLOCK_INPUT;
24639
24640 /* Write glyphs. */
24641
24642 hpos = start - updated_row->glyphs[updated_area];
24643 x = draw_glyphs (updated_window, output_cursor.x,
24644 updated_row, updated_area,
24645 hpos, hpos + len,
24646 DRAW_NORMAL_TEXT, 0);
24647
24648 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24649 if (updated_area == TEXT_AREA
24650 && updated_window->phys_cursor_on_p
24651 && updated_window->phys_cursor.vpos == output_cursor.vpos
24652 && updated_window->phys_cursor.hpos >= hpos
24653 && updated_window->phys_cursor.hpos < hpos + len)
24654 updated_window->phys_cursor_on_p = 0;
24655
24656 UNBLOCK_INPUT;
24657
24658 /* Advance the output cursor. */
24659 output_cursor.hpos += len;
24660 output_cursor.x = x;
24661 }
24662
24663
24664 /* EXPORT for RIF:
24665 Insert LEN glyphs from START at the nominal cursor position. */
24666
24667 void
24668 x_insert_glyphs (struct glyph *start, int len)
24669 {
24670 struct frame *f;
24671 struct window *w;
24672 int line_height, shift_by_width, shifted_region_width;
24673 struct glyph_row *row;
24674 struct glyph *glyph;
24675 int frame_x, frame_y;
24676 ptrdiff_t hpos;
24677
24678 xassert (updated_window && updated_row);
24679 BLOCK_INPUT;
24680 w = updated_window;
24681 f = XFRAME (WINDOW_FRAME (w));
24682
24683 /* Get the height of the line we are in. */
24684 row = updated_row;
24685 line_height = row->height;
24686
24687 /* Get the width of the glyphs to insert. */
24688 shift_by_width = 0;
24689 for (glyph = start; glyph < start + len; ++glyph)
24690 shift_by_width += glyph->pixel_width;
24691
24692 /* Get the width of the region to shift right. */
24693 shifted_region_width = (window_box_width (w, updated_area)
24694 - output_cursor.x
24695 - shift_by_width);
24696
24697 /* Shift right. */
24698 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24699 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24700
24701 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24702 line_height, shift_by_width);
24703
24704 /* Write the glyphs. */
24705 hpos = start - row->glyphs[updated_area];
24706 draw_glyphs (w, output_cursor.x, row, updated_area,
24707 hpos, hpos + len,
24708 DRAW_NORMAL_TEXT, 0);
24709
24710 /* Advance the output cursor. */
24711 output_cursor.hpos += len;
24712 output_cursor.x += shift_by_width;
24713 UNBLOCK_INPUT;
24714 }
24715
24716
24717 /* EXPORT for RIF:
24718 Erase the current text line from the nominal cursor position
24719 (inclusive) to pixel column TO_X (exclusive). The idea is that
24720 everything from TO_X onward is already erased.
24721
24722 TO_X is a pixel position relative to updated_area of
24723 updated_window. TO_X == -1 means clear to the end of this area. */
24724
24725 void
24726 x_clear_end_of_line (int to_x)
24727 {
24728 struct frame *f;
24729 struct window *w = updated_window;
24730 int max_x, min_y, max_y;
24731 int from_x, from_y, to_y;
24732
24733 xassert (updated_window && updated_row);
24734 f = XFRAME (w->frame);
24735
24736 if (updated_row->full_width_p)
24737 max_x = WINDOW_TOTAL_WIDTH (w);
24738 else
24739 max_x = window_box_width (w, updated_area);
24740 max_y = window_text_bottom_y (w);
24741
24742 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24743 of window. For TO_X > 0, truncate to end of drawing area. */
24744 if (to_x == 0)
24745 return;
24746 else if (to_x < 0)
24747 to_x = max_x;
24748 else
24749 to_x = min (to_x, max_x);
24750
24751 to_y = min (max_y, output_cursor.y + updated_row->height);
24752
24753 /* Notice if the cursor will be cleared by this operation. */
24754 if (!updated_row->full_width_p)
24755 notice_overwritten_cursor (w, updated_area,
24756 output_cursor.x, -1,
24757 updated_row->y,
24758 MATRIX_ROW_BOTTOM_Y (updated_row));
24759
24760 from_x = output_cursor.x;
24761
24762 /* Translate to frame coordinates. */
24763 if (updated_row->full_width_p)
24764 {
24765 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24766 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24767 }
24768 else
24769 {
24770 int area_left = window_box_left (w, updated_area);
24771 from_x += area_left;
24772 to_x += area_left;
24773 }
24774
24775 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24776 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24777 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24778
24779 /* Prevent inadvertently clearing to end of the X window. */
24780 if (to_x > from_x && to_y > from_y)
24781 {
24782 BLOCK_INPUT;
24783 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24784 to_x - from_x, to_y - from_y);
24785 UNBLOCK_INPUT;
24786 }
24787 }
24788
24789 #endif /* HAVE_WINDOW_SYSTEM */
24790
24791
24792 \f
24793 /***********************************************************************
24794 Cursor types
24795 ***********************************************************************/
24796
24797 /* Value is the internal representation of the specified cursor type
24798 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24799 of the bar cursor. */
24800
24801 static enum text_cursor_kinds
24802 get_specified_cursor_type (Lisp_Object arg, int *width)
24803 {
24804 enum text_cursor_kinds type;
24805
24806 if (NILP (arg))
24807 return NO_CURSOR;
24808
24809 if (EQ (arg, Qbox))
24810 return FILLED_BOX_CURSOR;
24811
24812 if (EQ (arg, Qhollow))
24813 return HOLLOW_BOX_CURSOR;
24814
24815 if (EQ (arg, Qbar))
24816 {
24817 *width = 2;
24818 return BAR_CURSOR;
24819 }
24820
24821 if (CONSP (arg)
24822 && EQ (XCAR (arg), Qbar)
24823 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
24824 {
24825 *width = XINT (XCDR (arg));
24826 return BAR_CURSOR;
24827 }
24828
24829 if (EQ (arg, Qhbar))
24830 {
24831 *width = 2;
24832 return HBAR_CURSOR;
24833 }
24834
24835 if (CONSP (arg)
24836 && EQ (XCAR (arg), Qhbar)
24837 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
24838 {
24839 *width = XINT (XCDR (arg));
24840 return HBAR_CURSOR;
24841 }
24842
24843 /* Treat anything unknown as "hollow box cursor".
24844 It was bad to signal an error; people have trouble fixing
24845 .Xdefaults with Emacs, when it has something bad in it. */
24846 type = HOLLOW_BOX_CURSOR;
24847
24848 return type;
24849 }
24850
24851 /* Set the default cursor types for specified frame. */
24852 void
24853 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24854 {
24855 int width = 1;
24856 Lisp_Object tem;
24857
24858 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24859 FRAME_CURSOR_WIDTH (f) = width;
24860
24861 /* By default, set up the blink-off state depending on the on-state. */
24862
24863 tem = Fassoc (arg, Vblink_cursor_alist);
24864 if (!NILP (tem))
24865 {
24866 FRAME_BLINK_OFF_CURSOR (f)
24867 = get_specified_cursor_type (XCDR (tem), &width);
24868 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24869 }
24870 else
24871 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24872 }
24873
24874
24875 #ifdef HAVE_WINDOW_SYSTEM
24876
24877 /* Return the cursor we want to be displayed in window W. Return
24878 width of bar/hbar cursor through WIDTH arg. Return with
24879 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24880 (i.e. if the `system caret' should track this cursor).
24881
24882 In a mini-buffer window, we want the cursor only to appear if we
24883 are reading input from this window. For the selected window, we
24884 want the cursor type given by the frame parameter or buffer local
24885 setting of cursor-type. If explicitly marked off, draw no cursor.
24886 In all other cases, we want a hollow box cursor. */
24887
24888 static enum text_cursor_kinds
24889 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24890 int *active_cursor)
24891 {
24892 struct frame *f = XFRAME (w->frame);
24893 struct buffer *b = XBUFFER (w->buffer);
24894 int cursor_type = DEFAULT_CURSOR;
24895 Lisp_Object alt_cursor;
24896 int non_selected = 0;
24897
24898 *active_cursor = 1;
24899
24900 /* Echo area */
24901 if (cursor_in_echo_area
24902 && FRAME_HAS_MINIBUF_P (f)
24903 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24904 {
24905 if (w == XWINDOW (echo_area_window))
24906 {
24907 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24908 {
24909 *width = FRAME_CURSOR_WIDTH (f);
24910 return FRAME_DESIRED_CURSOR (f);
24911 }
24912 else
24913 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24914 }
24915
24916 *active_cursor = 0;
24917 non_selected = 1;
24918 }
24919
24920 /* Detect a nonselected window or nonselected frame. */
24921 else if (w != XWINDOW (f->selected_window)
24922 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24923 {
24924 *active_cursor = 0;
24925
24926 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24927 return NO_CURSOR;
24928
24929 non_selected = 1;
24930 }
24931
24932 /* Never display a cursor in a window in which cursor-type is nil. */
24933 if (NILP (BVAR (b, cursor_type)))
24934 return NO_CURSOR;
24935
24936 /* Get the normal cursor type for this window. */
24937 if (EQ (BVAR (b, cursor_type), Qt))
24938 {
24939 cursor_type = FRAME_DESIRED_CURSOR (f);
24940 *width = FRAME_CURSOR_WIDTH (f);
24941 }
24942 else
24943 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24944
24945 /* Use cursor-in-non-selected-windows instead
24946 for non-selected window or frame. */
24947 if (non_selected)
24948 {
24949 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24950 if (!EQ (Qt, alt_cursor))
24951 return get_specified_cursor_type (alt_cursor, width);
24952 /* t means modify the normal cursor type. */
24953 if (cursor_type == FILLED_BOX_CURSOR)
24954 cursor_type = HOLLOW_BOX_CURSOR;
24955 else if (cursor_type == BAR_CURSOR && *width > 1)
24956 --*width;
24957 return cursor_type;
24958 }
24959
24960 /* Use normal cursor if not blinked off. */
24961 if (!w->cursor_off_p)
24962 {
24963 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24964 {
24965 if (cursor_type == FILLED_BOX_CURSOR)
24966 {
24967 /* Using a block cursor on large images can be very annoying.
24968 So use a hollow cursor for "large" images.
24969 If image is not transparent (no mask), also use hollow cursor. */
24970 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24971 if (img != NULL && IMAGEP (img->spec))
24972 {
24973 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24974 where N = size of default frame font size.
24975 This should cover most of the "tiny" icons people may use. */
24976 if (!img->mask
24977 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24978 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24979 cursor_type = HOLLOW_BOX_CURSOR;
24980 }
24981 }
24982 else if (cursor_type != NO_CURSOR)
24983 {
24984 /* Display current only supports BOX and HOLLOW cursors for images.
24985 So for now, unconditionally use a HOLLOW cursor when cursor is
24986 not a solid box cursor. */
24987 cursor_type = HOLLOW_BOX_CURSOR;
24988 }
24989 }
24990 return cursor_type;
24991 }
24992
24993 /* Cursor is blinked off, so determine how to "toggle" it. */
24994
24995 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24996 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24997 return get_specified_cursor_type (XCDR (alt_cursor), width);
24998
24999 /* Then see if frame has specified a specific blink off cursor type. */
25000 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25001 {
25002 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25003 return FRAME_BLINK_OFF_CURSOR (f);
25004 }
25005
25006 #if 0
25007 /* Some people liked having a permanently visible blinking cursor,
25008 while others had very strong opinions against it. So it was
25009 decided to remove it. KFS 2003-09-03 */
25010
25011 /* Finally perform built-in cursor blinking:
25012 filled box <-> hollow box
25013 wide [h]bar <-> narrow [h]bar
25014 narrow [h]bar <-> no cursor
25015 other type <-> no cursor */
25016
25017 if (cursor_type == FILLED_BOX_CURSOR)
25018 return HOLLOW_BOX_CURSOR;
25019
25020 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25021 {
25022 *width = 1;
25023 return cursor_type;
25024 }
25025 #endif
25026
25027 return NO_CURSOR;
25028 }
25029
25030
25031 /* Notice when the text cursor of window W has been completely
25032 overwritten by a drawing operation that outputs glyphs in AREA
25033 starting at X0 and ending at X1 in the line starting at Y0 and
25034 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25035 the rest of the line after X0 has been written. Y coordinates
25036 are window-relative. */
25037
25038 static void
25039 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25040 int x0, int x1, int y0, int y1)
25041 {
25042 int cx0, cx1, cy0, cy1;
25043 struct glyph_row *row;
25044
25045 if (!w->phys_cursor_on_p)
25046 return;
25047 if (area != TEXT_AREA)
25048 return;
25049
25050 if (w->phys_cursor.vpos < 0
25051 || w->phys_cursor.vpos >= w->current_matrix->nrows
25052 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25053 !(row->enabled_p && row->displays_text_p)))
25054 return;
25055
25056 if (row->cursor_in_fringe_p)
25057 {
25058 row->cursor_in_fringe_p = 0;
25059 draw_fringe_bitmap (w, row, row->reversed_p);
25060 w->phys_cursor_on_p = 0;
25061 return;
25062 }
25063
25064 cx0 = w->phys_cursor.x;
25065 cx1 = cx0 + w->phys_cursor_width;
25066 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25067 return;
25068
25069 /* The cursor image will be completely removed from the
25070 screen if the output area intersects the cursor area in
25071 y-direction. When we draw in [y0 y1[, and some part of
25072 the cursor is at y < y0, that part must have been drawn
25073 before. When scrolling, the cursor is erased before
25074 actually scrolling, so we don't come here. When not
25075 scrolling, the rows above the old cursor row must have
25076 changed, and in this case these rows must have written
25077 over the cursor image.
25078
25079 Likewise if part of the cursor is below y1, with the
25080 exception of the cursor being in the first blank row at
25081 the buffer and window end because update_text_area
25082 doesn't draw that row. (Except when it does, but
25083 that's handled in update_text_area.) */
25084
25085 cy0 = w->phys_cursor.y;
25086 cy1 = cy0 + w->phys_cursor_height;
25087 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25088 return;
25089
25090 w->phys_cursor_on_p = 0;
25091 }
25092
25093 #endif /* HAVE_WINDOW_SYSTEM */
25094
25095 \f
25096 /************************************************************************
25097 Mouse Face
25098 ************************************************************************/
25099
25100 #ifdef HAVE_WINDOW_SYSTEM
25101
25102 /* EXPORT for RIF:
25103 Fix the display of area AREA of overlapping row ROW in window W
25104 with respect to the overlapping part OVERLAPS. */
25105
25106 void
25107 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25108 enum glyph_row_area area, int overlaps)
25109 {
25110 int i, x;
25111
25112 BLOCK_INPUT;
25113
25114 x = 0;
25115 for (i = 0; i < row->used[area];)
25116 {
25117 if (row->glyphs[area][i].overlaps_vertically_p)
25118 {
25119 int start = i, start_x = x;
25120
25121 do
25122 {
25123 x += row->glyphs[area][i].pixel_width;
25124 ++i;
25125 }
25126 while (i < row->used[area]
25127 && row->glyphs[area][i].overlaps_vertically_p);
25128
25129 draw_glyphs (w, start_x, row, area,
25130 start, i,
25131 DRAW_NORMAL_TEXT, overlaps);
25132 }
25133 else
25134 {
25135 x += row->glyphs[area][i].pixel_width;
25136 ++i;
25137 }
25138 }
25139
25140 UNBLOCK_INPUT;
25141 }
25142
25143
25144 /* EXPORT:
25145 Draw the cursor glyph of window W in glyph row ROW. See the
25146 comment of draw_glyphs for the meaning of HL. */
25147
25148 void
25149 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25150 enum draw_glyphs_face hl)
25151 {
25152 /* If cursor hpos is out of bounds, don't draw garbage. This can
25153 happen in mini-buffer windows when switching between echo area
25154 glyphs and mini-buffer. */
25155 if ((row->reversed_p
25156 ? (w->phys_cursor.hpos >= 0)
25157 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25158 {
25159 int on_p = w->phys_cursor_on_p;
25160 int x1;
25161 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
25162 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
25163 hl, 0);
25164 w->phys_cursor_on_p = on_p;
25165
25166 if (hl == DRAW_CURSOR)
25167 w->phys_cursor_width = x1 - w->phys_cursor.x;
25168 /* When we erase the cursor, and ROW is overlapped by other
25169 rows, make sure that these overlapping parts of other rows
25170 are redrawn. */
25171 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25172 {
25173 w->phys_cursor_width = x1 - w->phys_cursor.x;
25174
25175 if (row > w->current_matrix->rows
25176 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25177 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25178 OVERLAPS_ERASED_CURSOR);
25179
25180 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25181 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25182 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25183 OVERLAPS_ERASED_CURSOR);
25184 }
25185 }
25186 }
25187
25188
25189 /* EXPORT:
25190 Erase the image of a cursor of window W from the screen. */
25191
25192 void
25193 erase_phys_cursor (struct window *w)
25194 {
25195 struct frame *f = XFRAME (w->frame);
25196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25197 int hpos = w->phys_cursor.hpos;
25198 int vpos = w->phys_cursor.vpos;
25199 int mouse_face_here_p = 0;
25200 struct glyph_matrix *active_glyphs = w->current_matrix;
25201 struct glyph_row *cursor_row;
25202 struct glyph *cursor_glyph;
25203 enum draw_glyphs_face hl;
25204
25205 /* No cursor displayed or row invalidated => nothing to do on the
25206 screen. */
25207 if (w->phys_cursor_type == NO_CURSOR)
25208 goto mark_cursor_off;
25209
25210 /* VPOS >= active_glyphs->nrows means that window has been resized.
25211 Don't bother to erase the cursor. */
25212 if (vpos >= active_glyphs->nrows)
25213 goto mark_cursor_off;
25214
25215 /* If row containing cursor is marked invalid, there is nothing we
25216 can do. */
25217 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25218 if (!cursor_row->enabled_p)
25219 goto mark_cursor_off;
25220
25221 /* If line spacing is > 0, old cursor may only be partially visible in
25222 window after split-window. So adjust visible height. */
25223 cursor_row->visible_height = min (cursor_row->visible_height,
25224 window_text_bottom_y (w) - cursor_row->y);
25225
25226 /* If row is completely invisible, don't attempt to delete a cursor which
25227 isn't there. This can happen if cursor is at top of a window, and
25228 we switch to a buffer with a header line in that window. */
25229 if (cursor_row->visible_height <= 0)
25230 goto mark_cursor_off;
25231
25232 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25233 if (cursor_row->cursor_in_fringe_p)
25234 {
25235 cursor_row->cursor_in_fringe_p = 0;
25236 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25237 goto mark_cursor_off;
25238 }
25239
25240 /* This can happen when the new row is shorter than the old one.
25241 In this case, either draw_glyphs or clear_end_of_line
25242 should have cleared the cursor. Note that we wouldn't be
25243 able to erase the cursor in this case because we don't have a
25244 cursor glyph at hand. */
25245 if ((cursor_row->reversed_p
25246 ? (w->phys_cursor.hpos < 0)
25247 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25248 goto mark_cursor_off;
25249
25250 /* If the cursor is in the mouse face area, redisplay that when
25251 we clear the cursor. */
25252 if (! NILP (hlinfo->mouse_face_window)
25253 && coords_in_mouse_face_p (w, hpos, vpos)
25254 /* Don't redraw the cursor's spot in mouse face if it is at the
25255 end of a line (on a newline). The cursor appears there, but
25256 mouse highlighting does not. */
25257 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25258 mouse_face_here_p = 1;
25259
25260 /* Maybe clear the display under the cursor. */
25261 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25262 {
25263 int x, y, left_x;
25264 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25265 int width;
25266
25267 cursor_glyph = get_phys_cursor_glyph (w);
25268 if (cursor_glyph == NULL)
25269 goto mark_cursor_off;
25270
25271 width = cursor_glyph->pixel_width;
25272 left_x = window_box_left_offset (w, TEXT_AREA);
25273 x = w->phys_cursor.x;
25274 if (x < left_x)
25275 width -= left_x - x;
25276 width = min (width, window_box_width (w, TEXT_AREA) - x);
25277 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25278 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25279
25280 if (width > 0)
25281 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25282 }
25283
25284 /* Erase the cursor by redrawing the character underneath it. */
25285 if (mouse_face_here_p)
25286 hl = DRAW_MOUSE_FACE;
25287 else
25288 hl = DRAW_NORMAL_TEXT;
25289 draw_phys_cursor_glyph (w, cursor_row, hl);
25290
25291 mark_cursor_off:
25292 w->phys_cursor_on_p = 0;
25293 w->phys_cursor_type = NO_CURSOR;
25294 }
25295
25296
25297 /* EXPORT:
25298 Display or clear cursor of window W. If ON is zero, clear the
25299 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25300 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25301
25302 void
25303 display_and_set_cursor (struct window *w, int on,
25304 int hpos, int vpos, int x, int y)
25305 {
25306 struct frame *f = XFRAME (w->frame);
25307 int new_cursor_type;
25308 int new_cursor_width;
25309 int active_cursor;
25310 struct glyph_row *glyph_row;
25311 struct glyph *glyph;
25312
25313 /* This is pointless on invisible frames, and dangerous on garbaged
25314 windows and frames; in the latter case, the frame or window may
25315 be in the midst of changing its size, and x and y may be off the
25316 window. */
25317 if (! FRAME_VISIBLE_P (f)
25318 || FRAME_GARBAGED_P (f)
25319 || vpos >= w->current_matrix->nrows
25320 || hpos >= w->current_matrix->matrix_w)
25321 return;
25322
25323 /* If cursor is off and we want it off, return quickly. */
25324 if (!on && !w->phys_cursor_on_p)
25325 return;
25326
25327 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25328 /* If cursor row is not enabled, we don't really know where to
25329 display the cursor. */
25330 if (!glyph_row->enabled_p)
25331 {
25332 w->phys_cursor_on_p = 0;
25333 return;
25334 }
25335
25336 glyph = NULL;
25337 if (!glyph_row->exact_window_width_line_p
25338 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25339 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25340
25341 xassert (interrupt_input_blocked);
25342
25343 /* Set new_cursor_type to the cursor we want to be displayed. */
25344 new_cursor_type = get_window_cursor_type (w, glyph,
25345 &new_cursor_width, &active_cursor);
25346
25347 /* If cursor is currently being shown and we don't want it to be or
25348 it is in the wrong place, or the cursor type is not what we want,
25349 erase it. */
25350 if (w->phys_cursor_on_p
25351 && (!on
25352 || w->phys_cursor.x != x
25353 || w->phys_cursor.y != y
25354 || new_cursor_type != w->phys_cursor_type
25355 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25356 && new_cursor_width != w->phys_cursor_width)))
25357 erase_phys_cursor (w);
25358
25359 /* Don't check phys_cursor_on_p here because that flag is only set
25360 to zero in some cases where we know that the cursor has been
25361 completely erased, to avoid the extra work of erasing the cursor
25362 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25363 still not be visible, or it has only been partly erased. */
25364 if (on)
25365 {
25366 w->phys_cursor_ascent = glyph_row->ascent;
25367 w->phys_cursor_height = glyph_row->height;
25368
25369 /* Set phys_cursor_.* before x_draw_.* is called because some
25370 of them may need the information. */
25371 w->phys_cursor.x = x;
25372 w->phys_cursor.y = glyph_row->y;
25373 w->phys_cursor.hpos = hpos;
25374 w->phys_cursor.vpos = vpos;
25375 }
25376
25377 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25378 new_cursor_type, new_cursor_width,
25379 on, active_cursor);
25380 }
25381
25382
25383 /* Switch the display of W's cursor on or off, according to the value
25384 of ON. */
25385
25386 static void
25387 update_window_cursor (struct window *w, int on)
25388 {
25389 /* Don't update cursor in windows whose frame is in the process
25390 of being deleted. */
25391 if (w->current_matrix)
25392 {
25393 BLOCK_INPUT;
25394 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
25395 w->phys_cursor.x, w->phys_cursor.y);
25396 UNBLOCK_INPUT;
25397 }
25398 }
25399
25400
25401 /* Call update_window_cursor with parameter ON_P on all leaf windows
25402 in the window tree rooted at W. */
25403
25404 static void
25405 update_cursor_in_window_tree (struct window *w, int on_p)
25406 {
25407 while (w)
25408 {
25409 if (!NILP (w->hchild))
25410 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25411 else if (!NILP (w->vchild))
25412 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25413 else
25414 update_window_cursor (w, on_p);
25415
25416 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25417 }
25418 }
25419
25420
25421 /* EXPORT:
25422 Display the cursor on window W, or clear it, according to ON_P.
25423 Don't change the cursor's position. */
25424
25425 void
25426 x_update_cursor (struct frame *f, int on_p)
25427 {
25428 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25429 }
25430
25431
25432 /* EXPORT:
25433 Clear the cursor of window W to background color, and mark the
25434 cursor as not shown. This is used when the text where the cursor
25435 is about to be rewritten. */
25436
25437 void
25438 x_clear_cursor (struct window *w)
25439 {
25440 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25441 update_window_cursor (w, 0);
25442 }
25443
25444 #endif /* HAVE_WINDOW_SYSTEM */
25445
25446 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25447 and MSDOS. */
25448 static void
25449 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25450 int start_hpos, int end_hpos,
25451 enum draw_glyphs_face draw)
25452 {
25453 #ifdef HAVE_WINDOW_SYSTEM
25454 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25455 {
25456 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25457 return;
25458 }
25459 #endif
25460 #if defined (HAVE_GPM) || defined (MSDOS)
25461 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25462 #endif
25463 }
25464
25465 /* Display the active region described by mouse_face_* according to DRAW. */
25466
25467 static void
25468 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25469 {
25470 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25471 struct frame *f = XFRAME (WINDOW_FRAME (w));
25472
25473 if (/* If window is in the process of being destroyed, don't bother
25474 to do anything. */
25475 w->current_matrix != NULL
25476 /* Don't update mouse highlight if hidden */
25477 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25478 /* Recognize when we are called to operate on rows that don't exist
25479 anymore. This can happen when a window is split. */
25480 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25481 {
25482 int phys_cursor_on_p = w->phys_cursor_on_p;
25483 struct glyph_row *row, *first, *last;
25484
25485 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25486 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25487
25488 for (row = first; row <= last && row->enabled_p; ++row)
25489 {
25490 int start_hpos, end_hpos, start_x;
25491
25492 /* For all but the first row, the highlight starts at column 0. */
25493 if (row == first)
25494 {
25495 /* R2L rows have BEG and END in reversed order, but the
25496 screen drawing geometry is always left to right. So
25497 we need to mirror the beginning and end of the
25498 highlighted area in R2L rows. */
25499 if (!row->reversed_p)
25500 {
25501 start_hpos = hlinfo->mouse_face_beg_col;
25502 start_x = hlinfo->mouse_face_beg_x;
25503 }
25504 else if (row == last)
25505 {
25506 start_hpos = hlinfo->mouse_face_end_col;
25507 start_x = hlinfo->mouse_face_end_x;
25508 }
25509 else
25510 {
25511 start_hpos = 0;
25512 start_x = 0;
25513 }
25514 }
25515 else if (row->reversed_p && row == last)
25516 {
25517 start_hpos = hlinfo->mouse_face_end_col;
25518 start_x = hlinfo->mouse_face_end_x;
25519 }
25520 else
25521 {
25522 start_hpos = 0;
25523 start_x = 0;
25524 }
25525
25526 if (row == last)
25527 {
25528 if (!row->reversed_p)
25529 end_hpos = hlinfo->mouse_face_end_col;
25530 else if (row == first)
25531 end_hpos = hlinfo->mouse_face_beg_col;
25532 else
25533 {
25534 end_hpos = row->used[TEXT_AREA];
25535 if (draw == DRAW_NORMAL_TEXT)
25536 row->fill_line_p = 1; /* Clear to end of line */
25537 }
25538 }
25539 else if (row->reversed_p && row == first)
25540 end_hpos = hlinfo->mouse_face_beg_col;
25541 else
25542 {
25543 end_hpos = row->used[TEXT_AREA];
25544 if (draw == DRAW_NORMAL_TEXT)
25545 row->fill_line_p = 1; /* Clear to end of line */
25546 }
25547
25548 if (end_hpos > start_hpos)
25549 {
25550 draw_row_with_mouse_face (w, start_x, row,
25551 start_hpos, end_hpos, draw);
25552
25553 row->mouse_face_p
25554 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25555 }
25556 }
25557
25558 #ifdef HAVE_WINDOW_SYSTEM
25559 /* When we've written over the cursor, arrange for it to
25560 be displayed again. */
25561 if (FRAME_WINDOW_P (f)
25562 && phys_cursor_on_p && !w->phys_cursor_on_p)
25563 {
25564 BLOCK_INPUT;
25565 display_and_set_cursor (w, 1,
25566 w->phys_cursor.hpos, w->phys_cursor.vpos,
25567 w->phys_cursor.x, w->phys_cursor.y);
25568 UNBLOCK_INPUT;
25569 }
25570 #endif /* HAVE_WINDOW_SYSTEM */
25571 }
25572
25573 #ifdef HAVE_WINDOW_SYSTEM
25574 /* Change the mouse cursor. */
25575 if (FRAME_WINDOW_P (f))
25576 {
25577 if (draw == DRAW_NORMAL_TEXT
25578 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25579 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25580 else if (draw == DRAW_MOUSE_FACE)
25581 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25582 else
25583 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25584 }
25585 #endif /* HAVE_WINDOW_SYSTEM */
25586 }
25587
25588 /* EXPORT:
25589 Clear out the mouse-highlighted active region.
25590 Redraw it un-highlighted first. Value is non-zero if mouse
25591 face was actually drawn unhighlighted. */
25592
25593 int
25594 clear_mouse_face (Mouse_HLInfo *hlinfo)
25595 {
25596 int cleared = 0;
25597
25598 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25599 {
25600 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25601 cleared = 1;
25602 }
25603
25604 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25605 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25606 hlinfo->mouse_face_window = Qnil;
25607 hlinfo->mouse_face_overlay = Qnil;
25608 return cleared;
25609 }
25610
25611 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25612 within the mouse face on that window. */
25613 static int
25614 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25615 {
25616 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25617
25618 /* Quickly resolve the easy cases. */
25619 if (!(WINDOWP (hlinfo->mouse_face_window)
25620 && XWINDOW (hlinfo->mouse_face_window) == w))
25621 return 0;
25622 if (vpos < hlinfo->mouse_face_beg_row
25623 || vpos > hlinfo->mouse_face_end_row)
25624 return 0;
25625 if (vpos > hlinfo->mouse_face_beg_row
25626 && vpos < hlinfo->mouse_face_end_row)
25627 return 1;
25628
25629 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25630 {
25631 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25632 {
25633 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25634 return 1;
25635 }
25636 else if ((vpos == hlinfo->mouse_face_beg_row
25637 && hpos >= hlinfo->mouse_face_beg_col)
25638 || (vpos == hlinfo->mouse_face_end_row
25639 && hpos < hlinfo->mouse_face_end_col))
25640 return 1;
25641 }
25642 else
25643 {
25644 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25645 {
25646 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25647 return 1;
25648 }
25649 else if ((vpos == hlinfo->mouse_face_beg_row
25650 && hpos <= hlinfo->mouse_face_beg_col)
25651 || (vpos == hlinfo->mouse_face_end_row
25652 && hpos > hlinfo->mouse_face_end_col))
25653 return 1;
25654 }
25655 return 0;
25656 }
25657
25658
25659 /* EXPORT:
25660 Non-zero if physical cursor of window W is within mouse face. */
25661
25662 int
25663 cursor_in_mouse_face_p (struct window *w)
25664 {
25665 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25666 }
25667
25668
25669 \f
25670 /* Find the glyph rows START_ROW and END_ROW of window W that display
25671 characters between buffer positions START_CHARPOS and END_CHARPOS
25672 (excluding END_CHARPOS). This is similar to row_containing_pos,
25673 but is more accurate when bidi reordering makes buffer positions
25674 change non-linearly with glyph rows. */
25675 static void
25676 rows_from_pos_range (struct window *w,
25677 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
25678 struct glyph_row **start, struct glyph_row **end)
25679 {
25680 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25681 int last_y = window_text_bottom_y (w);
25682 struct glyph_row *row;
25683
25684 *start = NULL;
25685 *end = NULL;
25686
25687 while (!first->enabled_p
25688 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25689 first++;
25690
25691 /* Find the START row. */
25692 for (row = first;
25693 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25694 row++)
25695 {
25696 /* A row can potentially be the START row if the range of the
25697 characters it displays intersects the range
25698 [START_CHARPOS..END_CHARPOS). */
25699 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25700 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25701 /* See the commentary in row_containing_pos, for the
25702 explanation of the complicated way to check whether
25703 some position is beyond the end of the characters
25704 displayed by a row. */
25705 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25706 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25707 && !row->ends_at_zv_p
25708 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25709 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25710 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25711 && !row->ends_at_zv_p
25712 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25713 {
25714 /* Found a candidate row. Now make sure at least one of the
25715 glyphs it displays has a charpos from the range
25716 [START_CHARPOS..END_CHARPOS).
25717
25718 This is not obvious because bidi reordering could make
25719 buffer positions of a row be 1,2,3,102,101,100, and if we
25720 want to highlight characters in [50..60), we don't want
25721 this row, even though [50..60) does intersect [1..103),
25722 the range of character positions given by the row's start
25723 and end positions. */
25724 struct glyph *g = row->glyphs[TEXT_AREA];
25725 struct glyph *e = g + row->used[TEXT_AREA];
25726
25727 while (g < e)
25728 {
25729 if ((BUFFERP (g->object) || INTEGERP (g->object))
25730 && start_charpos <= g->charpos && g->charpos < end_charpos)
25731 *start = row;
25732 g++;
25733 }
25734 if (*start)
25735 break;
25736 }
25737 }
25738
25739 /* Find the END row. */
25740 if (!*start
25741 /* If the last row is partially visible, start looking for END
25742 from that row, instead of starting from FIRST. */
25743 && !(row->enabled_p
25744 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25745 row = first;
25746 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25747 {
25748 struct glyph_row *next = row + 1;
25749
25750 if (!next->enabled_p
25751 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25752 /* The first row >= START whose range of displayed characters
25753 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25754 is the row END + 1. */
25755 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25756 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25757 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25758 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25759 && !next->ends_at_zv_p
25760 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25761 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25762 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25763 && !next->ends_at_zv_p
25764 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25765 {
25766 *end = row;
25767 break;
25768 }
25769 else
25770 {
25771 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25772 but none of the characters it displays are in the range, it is
25773 also END + 1. */
25774 struct glyph *g = next->glyphs[TEXT_AREA];
25775 struct glyph *e = g + next->used[TEXT_AREA];
25776
25777 while (g < e)
25778 {
25779 if ((BUFFERP (g->object) || INTEGERP (g->object))
25780 && start_charpos <= g->charpos && g->charpos < end_charpos)
25781 break;
25782 g++;
25783 }
25784 if (g == e)
25785 {
25786 *end = row;
25787 break;
25788 }
25789 }
25790 }
25791 }
25792
25793 /* This function sets the mouse_face_* elements of HLINFO, assuming
25794 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25795 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25796 for the overlay or run of text properties specifying the mouse
25797 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25798 before-string and after-string that must also be highlighted.
25799 COVER_STRING, if non-nil, is a display string that may cover some
25800 or all of the highlighted text. */
25801
25802 static void
25803 mouse_face_from_buffer_pos (Lisp_Object window,
25804 Mouse_HLInfo *hlinfo,
25805 ptrdiff_t mouse_charpos,
25806 ptrdiff_t start_charpos,
25807 ptrdiff_t end_charpos,
25808 Lisp_Object before_string,
25809 Lisp_Object after_string,
25810 Lisp_Object cover_string)
25811 {
25812 struct window *w = XWINDOW (window);
25813 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25814 struct glyph_row *r1, *r2;
25815 struct glyph *glyph, *end;
25816 ptrdiff_t ignore, pos;
25817 int x;
25818
25819 xassert (NILP (cover_string) || STRINGP (cover_string));
25820 xassert (NILP (before_string) || STRINGP (before_string));
25821 xassert (NILP (after_string) || STRINGP (after_string));
25822
25823 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25824 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25825 if (r1 == NULL)
25826 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25827 /* If the before-string or display-string contains newlines,
25828 rows_from_pos_range skips to its last row. Move back. */
25829 if (!NILP (before_string) || !NILP (cover_string))
25830 {
25831 struct glyph_row *prev;
25832 while ((prev = r1 - 1, prev >= first)
25833 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25834 && prev->used[TEXT_AREA] > 0)
25835 {
25836 struct glyph *beg = prev->glyphs[TEXT_AREA];
25837 glyph = beg + prev->used[TEXT_AREA];
25838 while (--glyph >= beg && INTEGERP (glyph->object));
25839 if (glyph < beg
25840 || !(EQ (glyph->object, before_string)
25841 || EQ (glyph->object, cover_string)))
25842 break;
25843 r1 = prev;
25844 }
25845 }
25846 if (r2 == NULL)
25847 {
25848 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25849 hlinfo->mouse_face_past_end = 1;
25850 }
25851 else if (!NILP (after_string))
25852 {
25853 /* If the after-string has newlines, advance to its last row. */
25854 struct glyph_row *next;
25855 struct glyph_row *last
25856 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25857
25858 for (next = r2 + 1;
25859 next <= last
25860 && next->used[TEXT_AREA] > 0
25861 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25862 ++next)
25863 r2 = next;
25864 }
25865 /* The rest of the display engine assumes that mouse_face_beg_row is
25866 either above below mouse_face_end_row or identical to it. But
25867 with bidi-reordered continued lines, the row for START_CHARPOS
25868 could be below the row for END_CHARPOS. If so, swap the rows and
25869 store them in correct order. */
25870 if (r1->y > r2->y)
25871 {
25872 struct glyph_row *tem = r2;
25873
25874 r2 = r1;
25875 r1 = tem;
25876 }
25877
25878 hlinfo->mouse_face_beg_y = r1->y;
25879 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25880 hlinfo->mouse_face_end_y = r2->y;
25881 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25882
25883 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25884 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25885 could be anywhere in the row and in any order. The strategy
25886 below is to find the leftmost and the rightmost glyph that
25887 belongs to either of these 3 strings, or whose position is
25888 between START_CHARPOS and END_CHARPOS, and highlight all the
25889 glyphs between those two. This may cover more than just the text
25890 between START_CHARPOS and END_CHARPOS if the range of characters
25891 strides the bidi level boundary, e.g. if the beginning is in R2L
25892 text while the end is in L2R text or vice versa. */
25893 if (!r1->reversed_p)
25894 {
25895 /* This row is in a left to right paragraph. Scan it left to
25896 right. */
25897 glyph = r1->glyphs[TEXT_AREA];
25898 end = glyph + r1->used[TEXT_AREA];
25899 x = r1->x;
25900
25901 /* Skip truncation glyphs at the start of the glyph row. */
25902 if (r1->displays_text_p)
25903 for (; glyph < end
25904 && INTEGERP (glyph->object)
25905 && glyph->charpos < 0;
25906 ++glyph)
25907 x += glyph->pixel_width;
25908
25909 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25910 or COVER_STRING, and the first glyph from buffer whose
25911 position is between START_CHARPOS and END_CHARPOS. */
25912 for (; glyph < end
25913 && !INTEGERP (glyph->object)
25914 && !EQ (glyph->object, cover_string)
25915 && !(BUFFERP (glyph->object)
25916 && (glyph->charpos >= start_charpos
25917 && glyph->charpos < end_charpos));
25918 ++glyph)
25919 {
25920 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25921 are present at buffer positions between START_CHARPOS and
25922 END_CHARPOS, or if they come from an overlay. */
25923 if (EQ (glyph->object, before_string))
25924 {
25925 pos = string_buffer_position (before_string,
25926 start_charpos);
25927 /* If pos == 0, it means before_string came from an
25928 overlay, not from a buffer position. */
25929 if (!pos || (pos >= start_charpos && pos < end_charpos))
25930 break;
25931 }
25932 else if (EQ (glyph->object, after_string))
25933 {
25934 pos = string_buffer_position (after_string, end_charpos);
25935 if (!pos || (pos >= start_charpos && pos < end_charpos))
25936 break;
25937 }
25938 x += glyph->pixel_width;
25939 }
25940 hlinfo->mouse_face_beg_x = x;
25941 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25942 }
25943 else
25944 {
25945 /* This row is in a right to left paragraph. Scan it right to
25946 left. */
25947 struct glyph *g;
25948
25949 end = r1->glyphs[TEXT_AREA] - 1;
25950 glyph = end + r1->used[TEXT_AREA];
25951
25952 /* Skip truncation glyphs at the start of the glyph row. */
25953 if (r1->displays_text_p)
25954 for (; glyph > end
25955 && INTEGERP (glyph->object)
25956 && glyph->charpos < 0;
25957 --glyph)
25958 ;
25959
25960 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25961 or COVER_STRING, and the first glyph from buffer whose
25962 position is between START_CHARPOS and END_CHARPOS. */
25963 for (; glyph > end
25964 && !INTEGERP (glyph->object)
25965 && !EQ (glyph->object, cover_string)
25966 && !(BUFFERP (glyph->object)
25967 && (glyph->charpos >= start_charpos
25968 && glyph->charpos < end_charpos));
25969 --glyph)
25970 {
25971 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25972 are present at buffer positions between START_CHARPOS and
25973 END_CHARPOS, or if they come from an overlay. */
25974 if (EQ (glyph->object, before_string))
25975 {
25976 pos = string_buffer_position (before_string, start_charpos);
25977 /* If pos == 0, it means before_string came from an
25978 overlay, not from a buffer position. */
25979 if (!pos || (pos >= start_charpos && pos < end_charpos))
25980 break;
25981 }
25982 else if (EQ (glyph->object, after_string))
25983 {
25984 pos = string_buffer_position (after_string, end_charpos);
25985 if (!pos || (pos >= start_charpos && pos < end_charpos))
25986 break;
25987 }
25988 }
25989
25990 glyph++; /* first glyph to the right of the highlighted area */
25991 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25992 x += g->pixel_width;
25993 hlinfo->mouse_face_beg_x = x;
25994 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25995 }
25996
25997 /* If the highlight ends in a different row, compute GLYPH and END
25998 for the end row. Otherwise, reuse the values computed above for
25999 the row where the highlight begins. */
26000 if (r2 != r1)
26001 {
26002 if (!r2->reversed_p)
26003 {
26004 glyph = r2->glyphs[TEXT_AREA];
26005 end = glyph + r2->used[TEXT_AREA];
26006 x = r2->x;
26007 }
26008 else
26009 {
26010 end = r2->glyphs[TEXT_AREA] - 1;
26011 glyph = end + r2->used[TEXT_AREA];
26012 }
26013 }
26014
26015 if (!r2->reversed_p)
26016 {
26017 /* Skip truncation and continuation glyphs near the end of the
26018 row, and also blanks and stretch glyphs inserted by
26019 extend_face_to_end_of_line. */
26020 while (end > glyph
26021 && INTEGERP ((end - 1)->object)
26022 && (end - 1)->charpos <= 0)
26023 --end;
26024 /* Scan the rest of the glyph row from the end, looking for the
26025 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26026 COVER_STRING, or whose position is between START_CHARPOS
26027 and END_CHARPOS */
26028 for (--end;
26029 end > glyph
26030 && !INTEGERP (end->object)
26031 && !EQ (end->object, cover_string)
26032 && !(BUFFERP (end->object)
26033 && (end->charpos >= start_charpos
26034 && end->charpos < end_charpos));
26035 --end)
26036 {
26037 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26038 are present at buffer positions between START_CHARPOS and
26039 END_CHARPOS, or if they come from an overlay. */
26040 if (EQ (end->object, before_string))
26041 {
26042 pos = string_buffer_position (before_string, start_charpos);
26043 if (!pos || (pos >= start_charpos && pos < end_charpos))
26044 break;
26045 }
26046 else if (EQ (end->object, after_string))
26047 {
26048 pos = string_buffer_position (after_string, end_charpos);
26049 if (!pos || (pos >= start_charpos && pos < end_charpos))
26050 break;
26051 }
26052 }
26053 /* Find the X coordinate of the last glyph to be highlighted. */
26054 for (; glyph <= end; ++glyph)
26055 x += glyph->pixel_width;
26056
26057 hlinfo->mouse_face_end_x = x;
26058 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26059 }
26060 else
26061 {
26062 /* Skip truncation and continuation glyphs near the end of the
26063 row, and also blanks and stretch glyphs inserted by
26064 extend_face_to_end_of_line. */
26065 x = r2->x;
26066 end++;
26067 while (end < glyph
26068 && INTEGERP (end->object)
26069 && end->charpos <= 0)
26070 {
26071 x += end->pixel_width;
26072 ++end;
26073 }
26074 /* Scan the rest of the glyph row from the end, looking for the
26075 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26076 COVER_STRING, or whose position is between START_CHARPOS
26077 and END_CHARPOS */
26078 for ( ;
26079 end < glyph
26080 && !INTEGERP (end->object)
26081 && !EQ (end->object, cover_string)
26082 && !(BUFFERP (end->object)
26083 && (end->charpos >= start_charpos
26084 && end->charpos < end_charpos));
26085 ++end)
26086 {
26087 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26088 are present at buffer positions between START_CHARPOS and
26089 END_CHARPOS, or if they come from an overlay. */
26090 if (EQ (end->object, before_string))
26091 {
26092 pos = string_buffer_position (before_string, start_charpos);
26093 if (!pos || (pos >= start_charpos && pos < end_charpos))
26094 break;
26095 }
26096 else if (EQ (end->object, after_string))
26097 {
26098 pos = string_buffer_position (after_string, end_charpos);
26099 if (!pos || (pos >= start_charpos && pos < end_charpos))
26100 break;
26101 }
26102 x += end->pixel_width;
26103 }
26104 hlinfo->mouse_face_end_x = x;
26105 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26106 }
26107
26108 hlinfo->mouse_face_window = window;
26109 hlinfo->mouse_face_face_id
26110 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26111 mouse_charpos + 1,
26112 !hlinfo->mouse_face_hidden, -1);
26113 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26114 }
26115
26116 /* The following function is not used anymore (replaced with
26117 mouse_face_from_string_pos), but I leave it here for the time
26118 being, in case someone would. */
26119
26120 #if 0 /* not used */
26121
26122 /* Find the position of the glyph for position POS in OBJECT in
26123 window W's current matrix, and return in *X, *Y the pixel
26124 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26125
26126 RIGHT_P non-zero means return the position of the right edge of the
26127 glyph, RIGHT_P zero means return the left edge position.
26128
26129 If no glyph for POS exists in the matrix, return the position of
26130 the glyph with the next smaller position that is in the matrix, if
26131 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26132 exists in the matrix, return the position of the glyph with the
26133 next larger position in OBJECT.
26134
26135 Value is non-zero if a glyph was found. */
26136
26137 static int
26138 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26139 int *hpos, int *vpos, int *x, int *y, int right_p)
26140 {
26141 int yb = window_text_bottom_y (w);
26142 struct glyph_row *r;
26143 struct glyph *best_glyph = NULL;
26144 struct glyph_row *best_row = NULL;
26145 int best_x = 0;
26146
26147 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26148 r->enabled_p && r->y < yb;
26149 ++r)
26150 {
26151 struct glyph *g = r->glyphs[TEXT_AREA];
26152 struct glyph *e = g + r->used[TEXT_AREA];
26153 int gx;
26154
26155 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26156 if (EQ (g->object, object))
26157 {
26158 if (g->charpos == pos)
26159 {
26160 best_glyph = g;
26161 best_x = gx;
26162 best_row = r;
26163 goto found;
26164 }
26165 else if (best_glyph == NULL
26166 || ((eabs (g->charpos - pos)
26167 < eabs (best_glyph->charpos - pos))
26168 && (right_p
26169 ? g->charpos < pos
26170 : g->charpos > pos)))
26171 {
26172 best_glyph = g;
26173 best_x = gx;
26174 best_row = r;
26175 }
26176 }
26177 }
26178
26179 found:
26180
26181 if (best_glyph)
26182 {
26183 *x = best_x;
26184 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26185
26186 if (right_p)
26187 {
26188 *x += best_glyph->pixel_width;
26189 ++*hpos;
26190 }
26191
26192 *y = best_row->y;
26193 *vpos = best_row - w->current_matrix->rows;
26194 }
26195
26196 return best_glyph != NULL;
26197 }
26198 #endif /* not used */
26199
26200 /* Find the positions of the first and the last glyphs in window W's
26201 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26202 (assumed to be a string), and return in HLINFO's mouse_face_*
26203 members the pixel and column/row coordinates of those glyphs. */
26204
26205 static void
26206 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26207 Lisp_Object object,
26208 ptrdiff_t startpos, ptrdiff_t endpos)
26209 {
26210 int yb = window_text_bottom_y (w);
26211 struct glyph_row *r;
26212 struct glyph *g, *e;
26213 int gx;
26214 int found = 0;
26215
26216 /* Find the glyph row with at least one position in the range
26217 [STARTPOS..ENDPOS], and the first glyph in that row whose
26218 position belongs to that range. */
26219 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26220 r->enabled_p && r->y < yb;
26221 ++r)
26222 {
26223 if (!r->reversed_p)
26224 {
26225 g = r->glyphs[TEXT_AREA];
26226 e = g + r->used[TEXT_AREA];
26227 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26228 if (EQ (g->object, object)
26229 && startpos <= g->charpos && g->charpos <= endpos)
26230 {
26231 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26232 hlinfo->mouse_face_beg_y = r->y;
26233 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26234 hlinfo->mouse_face_beg_x = gx;
26235 found = 1;
26236 break;
26237 }
26238 }
26239 else
26240 {
26241 struct glyph *g1;
26242
26243 e = r->glyphs[TEXT_AREA];
26244 g = e + r->used[TEXT_AREA];
26245 for ( ; g > e; --g)
26246 if (EQ ((g-1)->object, object)
26247 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26248 {
26249 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26250 hlinfo->mouse_face_beg_y = r->y;
26251 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26252 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26253 gx += g1->pixel_width;
26254 hlinfo->mouse_face_beg_x = gx;
26255 found = 1;
26256 break;
26257 }
26258 }
26259 if (found)
26260 break;
26261 }
26262
26263 if (!found)
26264 return;
26265
26266 /* Starting with the next row, look for the first row which does NOT
26267 include any glyphs whose positions are in the range. */
26268 for (++r; r->enabled_p && r->y < yb; ++r)
26269 {
26270 g = r->glyphs[TEXT_AREA];
26271 e = g + r->used[TEXT_AREA];
26272 found = 0;
26273 for ( ; g < e; ++g)
26274 if (EQ (g->object, object)
26275 && startpos <= g->charpos && g->charpos <= endpos)
26276 {
26277 found = 1;
26278 break;
26279 }
26280 if (!found)
26281 break;
26282 }
26283
26284 /* The highlighted region ends on the previous row. */
26285 r--;
26286
26287 /* Set the end row and its vertical pixel coordinate. */
26288 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26289 hlinfo->mouse_face_end_y = r->y;
26290
26291 /* Compute and set the end column and the end column's horizontal
26292 pixel coordinate. */
26293 if (!r->reversed_p)
26294 {
26295 g = r->glyphs[TEXT_AREA];
26296 e = g + r->used[TEXT_AREA];
26297 for ( ; e > g; --e)
26298 if (EQ ((e-1)->object, object)
26299 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26300 break;
26301 hlinfo->mouse_face_end_col = e - g;
26302
26303 for (gx = r->x; g < e; ++g)
26304 gx += g->pixel_width;
26305 hlinfo->mouse_face_end_x = gx;
26306 }
26307 else
26308 {
26309 e = r->glyphs[TEXT_AREA];
26310 g = e + r->used[TEXT_AREA];
26311 for (gx = r->x ; e < g; ++e)
26312 {
26313 if (EQ (e->object, object)
26314 && startpos <= e->charpos && e->charpos <= endpos)
26315 break;
26316 gx += e->pixel_width;
26317 }
26318 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26319 hlinfo->mouse_face_end_x = gx;
26320 }
26321 }
26322
26323 #ifdef HAVE_WINDOW_SYSTEM
26324
26325 /* See if position X, Y is within a hot-spot of an image. */
26326
26327 static int
26328 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26329 {
26330 if (!CONSP (hot_spot))
26331 return 0;
26332
26333 if (EQ (XCAR (hot_spot), Qrect))
26334 {
26335 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26336 Lisp_Object rect = XCDR (hot_spot);
26337 Lisp_Object tem;
26338 if (!CONSP (rect))
26339 return 0;
26340 if (!CONSP (XCAR (rect)))
26341 return 0;
26342 if (!CONSP (XCDR (rect)))
26343 return 0;
26344 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26345 return 0;
26346 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26347 return 0;
26348 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26349 return 0;
26350 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26351 return 0;
26352 return 1;
26353 }
26354 else if (EQ (XCAR (hot_spot), Qcircle))
26355 {
26356 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26357 Lisp_Object circ = XCDR (hot_spot);
26358 Lisp_Object lr, lx0, ly0;
26359 if (CONSP (circ)
26360 && CONSP (XCAR (circ))
26361 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26362 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26363 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26364 {
26365 double r = XFLOATINT (lr);
26366 double dx = XINT (lx0) - x;
26367 double dy = XINT (ly0) - y;
26368 return (dx * dx + dy * dy <= r * r);
26369 }
26370 }
26371 else if (EQ (XCAR (hot_spot), Qpoly))
26372 {
26373 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26374 if (VECTORP (XCDR (hot_spot)))
26375 {
26376 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26377 Lisp_Object *poly = v->contents;
26378 ptrdiff_t n = v->header.size;
26379 ptrdiff_t i;
26380 int inside = 0;
26381 Lisp_Object lx, ly;
26382 int x0, y0;
26383
26384 /* Need an even number of coordinates, and at least 3 edges. */
26385 if (n < 6 || n & 1)
26386 return 0;
26387
26388 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26389 If count is odd, we are inside polygon. Pixels on edges
26390 may or may not be included depending on actual geometry of the
26391 polygon. */
26392 if ((lx = poly[n-2], !INTEGERP (lx))
26393 || (ly = poly[n-1], !INTEGERP (lx)))
26394 return 0;
26395 x0 = XINT (lx), y0 = XINT (ly);
26396 for (i = 0; i < n; i += 2)
26397 {
26398 int x1 = x0, y1 = y0;
26399 if ((lx = poly[i], !INTEGERP (lx))
26400 || (ly = poly[i+1], !INTEGERP (ly)))
26401 return 0;
26402 x0 = XINT (lx), y0 = XINT (ly);
26403
26404 /* Does this segment cross the X line? */
26405 if (x0 >= x)
26406 {
26407 if (x1 >= x)
26408 continue;
26409 }
26410 else if (x1 < x)
26411 continue;
26412 if (y > y0 && y > y1)
26413 continue;
26414 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26415 inside = !inside;
26416 }
26417 return inside;
26418 }
26419 }
26420 return 0;
26421 }
26422
26423 Lisp_Object
26424 find_hot_spot (Lisp_Object map, int x, int y)
26425 {
26426 while (CONSP (map))
26427 {
26428 if (CONSP (XCAR (map))
26429 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26430 return XCAR (map);
26431 map = XCDR (map);
26432 }
26433
26434 return Qnil;
26435 }
26436
26437 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26438 3, 3, 0,
26439 doc: /* Lookup in image map MAP coordinates X and Y.
26440 An image map is an alist where each element has the format (AREA ID PLIST).
26441 An AREA is specified as either a rectangle, a circle, or a polygon:
26442 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26443 pixel coordinates of the upper left and bottom right corners.
26444 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26445 and the radius of the circle; r may be a float or integer.
26446 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26447 vector describes one corner in the polygon.
26448 Returns the alist element for the first matching AREA in MAP. */)
26449 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26450 {
26451 if (NILP (map))
26452 return Qnil;
26453
26454 CHECK_NUMBER (x);
26455 CHECK_NUMBER (y);
26456
26457 return find_hot_spot (map,
26458 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26459 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26460 }
26461
26462
26463 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26464 static void
26465 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26466 {
26467 /* Do not change cursor shape while dragging mouse. */
26468 if (!NILP (do_mouse_tracking))
26469 return;
26470
26471 if (!NILP (pointer))
26472 {
26473 if (EQ (pointer, Qarrow))
26474 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26475 else if (EQ (pointer, Qhand))
26476 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26477 else if (EQ (pointer, Qtext))
26478 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26479 else if (EQ (pointer, intern ("hdrag")))
26480 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26481 #ifdef HAVE_X_WINDOWS
26482 else if (EQ (pointer, intern ("vdrag")))
26483 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26484 #endif
26485 else if (EQ (pointer, intern ("hourglass")))
26486 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26487 else if (EQ (pointer, Qmodeline))
26488 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26489 else
26490 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26491 }
26492
26493 if (cursor != No_Cursor)
26494 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26495 }
26496
26497 #endif /* HAVE_WINDOW_SYSTEM */
26498
26499 /* Take proper action when mouse has moved to the mode or header line
26500 or marginal area AREA of window W, x-position X and y-position Y.
26501 X is relative to the start of the text display area of W, so the
26502 width of bitmap areas and scroll bars must be subtracted to get a
26503 position relative to the start of the mode line. */
26504
26505 static void
26506 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26507 enum window_part area)
26508 {
26509 struct window *w = XWINDOW (window);
26510 struct frame *f = XFRAME (w->frame);
26511 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26512 #ifdef HAVE_WINDOW_SYSTEM
26513 Display_Info *dpyinfo;
26514 #endif
26515 Cursor cursor = No_Cursor;
26516 Lisp_Object pointer = Qnil;
26517 int dx, dy, width, height;
26518 ptrdiff_t charpos;
26519 Lisp_Object string, object = Qnil;
26520 Lisp_Object pos, help;
26521
26522 Lisp_Object mouse_face;
26523 int original_x_pixel = x;
26524 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26525 struct glyph_row *row;
26526
26527 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26528 {
26529 int x0;
26530 struct glyph *end;
26531
26532 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26533 returns them in row/column units! */
26534 string = mode_line_string (w, area, &x, &y, &charpos,
26535 &object, &dx, &dy, &width, &height);
26536
26537 row = (area == ON_MODE_LINE
26538 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26539 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26540
26541 /* Find the glyph under the mouse pointer. */
26542 if (row->mode_line_p && row->enabled_p)
26543 {
26544 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26545 end = glyph + row->used[TEXT_AREA];
26546
26547 for (x0 = original_x_pixel;
26548 glyph < end && x0 >= glyph->pixel_width;
26549 ++glyph)
26550 x0 -= glyph->pixel_width;
26551
26552 if (glyph >= end)
26553 glyph = NULL;
26554 }
26555 }
26556 else
26557 {
26558 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26559 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26560 returns them in row/column units! */
26561 string = marginal_area_string (w, area, &x, &y, &charpos,
26562 &object, &dx, &dy, &width, &height);
26563 }
26564
26565 help = Qnil;
26566
26567 #ifdef HAVE_WINDOW_SYSTEM
26568 if (IMAGEP (object))
26569 {
26570 Lisp_Object image_map, hotspot;
26571 if ((image_map = Fplist_get (XCDR (object), QCmap),
26572 !NILP (image_map))
26573 && (hotspot = find_hot_spot (image_map, dx, dy),
26574 CONSP (hotspot))
26575 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26576 {
26577 Lisp_Object plist;
26578
26579 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26580 If so, we could look for mouse-enter, mouse-leave
26581 properties in PLIST (and do something...). */
26582 hotspot = XCDR (hotspot);
26583 if (CONSP (hotspot)
26584 && (plist = XCAR (hotspot), CONSP (plist)))
26585 {
26586 pointer = Fplist_get (plist, Qpointer);
26587 if (NILP (pointer))
26588 pointer = Qhand;
26589 help = Fplist_get (plist, Qhelp_echo);
26590 if (!NILP (help))
26591 {
26592 help_echo_string = help;
26593 /* Is this correct? ++kfs */
26594 XSETWINDOW (help_echo_window, w);
26595 help_echo_object = w->buffer;
26596 help_echo_pos = charpos;
26597 }
26598 }
26599 }
26600 if (NILP (pointer))
26601 pointer = Fplist_get (XCDR (object), QCpointer);
26602 }
26603 #endif /* HAVE_WINDOW_SYSTEM */
26604
26605 if (STRINGP (string))
26606 {
26607 pos = make_number (charpos);
26608 /* If we're on a string with `help-echo' text property, arrange
26609 for the help to be displayed. This is done by setting the
26610 global variable help_echo_string to the help string. */
26611 if (NILP (help))
26612 {
26613 help = Fget_text_property (pos, Qhelp_echo, string);
26614 if (!NILP (help))
26615 {
26616 help_echo_string = help;
26617 XSETWINDOW (help_echo_window, w);
26618 help_echo_object = string;
26619 help_echo_pos = charpos;
26620 }
26621 }
26622
26623 #ifdef HAVE_WINDOW_SYSTEM
26624 if (FRAME_WINDOW_P (f))
26625 {
26626 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26627 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26628 if (NILP (pointer))
26629 pointer = Fget_text_property (pos, Qpointer, string);
26630
26631 /* Change the mouse pointer according to what is under X/Y. */
26632 if (NILP (pointer)
26633 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26634 {
26635 Lisp_Object map;
26636 map = Fget_text_property (pos, Qlocal_map, string);
26637 if (!KEYMAPP (map))
26638 map = Fget_text_property (pos, Qkeymap, string);
26639 if (!KEYMAPP (map))
26640 cursor = dpyinfo->vertical_scroll_bar_cursor;
26641 }
26642 }
26643 #endif
26644
26645 /* Change the mouse face according to what is under X/Y. */
26646 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26647 if (!NILP (mouse_face)
26648 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26649 && glyph)
26650 {
26651 Lisp_Object b, e;
26652
26653 struct glyph * tmp_glyph;
26654
26655 int gpos;
26656 int gseq_length;
26657 int total_pixel_width;
26658 ptrdiff_t begpos, endpos, ignore;
26659
26660 int vpos, hpos;
26661
26662 b = Fprevious_single_property_change (make_number (charpos + 1),
26663 Qmouse_face, string, Qnil);
26664 if (NILP (b))
26665 begpos = 0;
26666 else
26667 begpos = XINT (b);
26668
26669 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26670 if (NILP (e))
26671 endpos = SCHARS (string);
26672 else
26673 endpos = XINT (e);
26674
26675 /* Calculate the glyph position GPOS of GLYPH in the
26676 displayed string, relative to the beginning of the
26677 highlighted part of the string.
26678
26679 Note: GPOS is different from CHARPOS. CHARPOS is the
26680 position of GLYPH in the internal string object. A mode
26681 line string format has structures which are converted to
26682 a flattened string by the Emacs Lisp interpreter. The
26683 internal string is an element of those structures. The
26684 displayed string is the flattened string. */
26685 tmp_glyph = row_start_glyph;
26686 while (tmp_glyph < glyph
26687 && (!(EQ (tmp_glyph->object, glyph->object)
26688 && begpos <= tmp_glyph->charpos
26689 && tmp_glyph->charpos < endpos)))
26690 tmp_glyph++;
26691 gpos = glyph - tmp_glyph;
26692
26693 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26694 the highlighted part of the displayed string to which
26695 GLYPH belongs. Note: GSEQ_LENGTH is different from
26696 SCHARS (STRING), because the latter returns the length of
26697 the internal string. */
26698 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26699 tmp_glyph > glyph
26700 && (!(EQ (tmp_glyph->object, glyph->object)
26701 && begpos <= tmp_glyph->charpos
26702 && tmp_glyph->charpos < endpos));
26703 tmp_glyph--)
26704 ;
26705 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26706
26707 /* Calculate the total pixel width of all the glyphs between
26708 the beginning of the highlighted area and GLYPH. */
26709 total_pixel_width = 0;
26710 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26711 total_pixel_width += tmp_glyph->pixel_width;
26712
26713 /* Pre calculation of re-rendering position. Note: X is in
26714 column units here, after the call to mode_line_string or
26715 marginal_area_string. */
26716 hpos = x - gpos;
26717 vpos = (area == ON_MODE_LINE
26718 ? (w->current_matrix)->nrows - 1
26719 : 0);
26720
26721 /* If GLYPH's position is included in the region that is
26722 already drawn in mouse face, we have nothing to do. */
26723 if ( EQ (window, hlinfo->mouse_face_window)
26724 && (!row->reversed_p
26725 ? (hlinfo->mouse_face_beg_col <= hpos
26726 && hpos < hlinfo->mouse_face_end_col)
26727 /* In R2L rows we swap BEG and END, see below. */
26728 : (hlinfo->mouse_face_end_col <= hpos
26729 && hpos < hlinfo->mouse_face_beg_col))
26730 && hlinfo->mouse_face_beg_row == vpos )
26731 return;
26732
26733 if (clear_mouse_face (hlinfo))
26734 cursor = No_Cursor;
26735
26736 if (!row->reversed_p)
26737 {
26738 hlinfo->mouse_face_beg_col = hpos;
26739 hlinfo->mouse_face_beg_x = original_x_pixel
26740 - (total_pixel_width + dx);
26741 hlinfo->mouse_face_end_col = hpos + gseq_length;
26742 hlinfo->mouse_face_end_x = 0;
26743 }
26744 else
26745 {
26746 /* In R2L rows, show_mouse_face expects BEG and END
26747 coordinates to be swapped. */
26748 hlinfo->mouse_face_end_col = hpos;
26749 hlinfo->mouse_face_end_x = original_x_pixel
26750 - (total_pixel_width + dx);
26751 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26752 hlinfo->mouse_face_beg_x = 0;
26753 }
26754
26755 hlinfo->mouse_face_beg_row = vpos;
26756 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26757 hlinfo->mouse_face_beg_y = 0;
26758 hlinfo->mouse_face_end_y = 0;
26759 hlinfo->mouse_face_past_end = 0;
26760 hlinfo->mouse_face_window = window;
26761
26762 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26763 charpos,
26764 0, 0, 0,
26765 &ignore,
26766 glyph->face_id,
26767 1);
26768 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26769
26770 if (NILP (pointer))
26771 pointer = Qhand;
26772 }
26773 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26774 clear_mouse_face (hlinfo);
26775 }
26776 #ifdef HAVE_WINDOW_SYSTEM
26777 if (FRAME_WINDOW_P (f))
26778 define_frame_cursor1 (f, cursor, pointer);
26779 #endif
26780 }
26781
26782
26783 /* EXPORT:
26784 Take proper action when the mouse has moved to position X, Y on
26785 frame F as regards highlighting characters that have mouse-face
26786 properties. Also de-highlighting chars where the mouse was before.
26787 X and Y can be negative or out of range. */
26788
26789 void
26790 note_mouse_highlight (struct frame *f, int x, int y)
26791 {
26792 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26793 enum window_part part;
26794 Lisp_Object window;
26795 struct window *w;
26796 Cursor cursor = No_Cursor;
26797 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26798 struct buffer *b;
26799
26800 /* When a menu is active, don't highlight because this looks odd. */
26801 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26802 if (popup_activated ())
26803 return;
26804 #endif
26805
26806 if (NILP (Vmouse_highlight)
26807 || !f->glyphs_initialized_p
26808 || f->pointer_invisible)
26809 return;
26810
26811 hlinfo->mouse_face_mouse_x = x;
26812 hlinfo->mouse_face_mouse_y = y;
26813 hlinfo->mouse_face_mouse_frame = f;
26814
26815 if (hlinfo->mouse_face_defer)
26816 return;
26817
26818 if (gc_in_progress)
26819 {
26820 hlinfo->mouse_face_deferred_gc = 1;
26821 return;
26822 }
26823
26824 /* Which window is that in? */
26825 window = window_from_coordinates (f, x, y, &part, 1);
26826
26827 /* If we were displaying active text in another window, clear that.
26828 Also clear if we move out of text area in same window. */
26829 if (! EQ (window, hlinfo->mouse_face_window)
26830 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26831 && !NILP (hlinfo->mouse_face_window)))
26832 clear_mouse_face (hlinfo);
26833
26834 /* Not on a window -> return. */
26835 if (!WINDOWP (window))
26836 return;
26837
26838 /* Reset help_echo_string. It will get recomputed below. */
26839 help_echo_string = Qnil;
26840
26841 /* Convert to window-relative pixel coordinates. */
26842 w = XWINDOW (window);
26843 frame_to_window_pixel_xy (w, &x, &y);
26844
26845 #ifdef HAVE_WINDOW_SYSTEM
26846 /* Handle tool-bar window differently since it doesn't display a
26847 buffer. */
26848 if (EQ (window, f->tool_bar_window))
26849 {
26850 note_tool_bar_highlight (f, x, y);
26851 return;
26852 }
26853 #endif
26854
26855 /* Mouse is on the mode, header line or margin? */
26856 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26857 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26858 {
26859 note_mode_line_or_margin_highlight (window, x, y, part);
26860 return;
26861 }
26862
26863 #ifdef HAVE_WINDOW_SYSTEM
26864 if (part == ON_VERTICAL_BORDER)
26865 {
26866 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26867 help_echo_string = build_string ("drag-mouse-1: resize");
26868 }
26869 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26870 || part == ON_SCROLL_BAR)
26871 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26872 else
26873 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26874 #endif
26875
26876 /* Are we in a window whose display is up to date?
26877 And verify the buffer's text has not changed. */
26878 b = XBUFFER (w->buffer);
26879 if (part == ON_TEXT
26880 && EQ (w->window_end_valid, w->buffer)
26881 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26882 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26883 {
26884 int hpos, vpos, dx, dy, area;
26885 ptrdiff_t pos;
26886 struct glyph *glyph;
26887 Lisp_Object object;
26888 Lisp_Object mouse_face = Qnil, position;
26889 Lisp_Object *overlay_vec = NULL;
26890 ptrdiff_t i, noverlays;
26891 struct buffer *obuf;
26892 ptrdiff_t obegv, ozv;
26893 int same_region;
26894
26895 /* Find the glyph under X/Y. */
26896 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26897
26898 #ifdef HAVE_WINDOW_SYSTEM
26899 /* Look for :pointer property on image. */
26900 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26901 {
26902 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26903 if (img != NULL && IMAGEP (img->spec))
26904 {
26905 Lisp_Object image_map, hotspot;
26906 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26907 !NILP (image_map))
26908 && (hotspot = find_hot_spot (image_map,
26909 glyph->slice.img.x + dx,
26910 glyph->slice.img.y + dy),
26911 CONSP (hotspot))
26912 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26913 {
26914 Lisp_Object plist;
26915
26916 /* Could check XCAR (hotspot) to see if we enter/leave
26917 this hot-spot.
26918 If so, we could look for mouse-enter, mouse-leave
26919 properties in PLIST (and do something...). */
26920 hotspot = XCDR (hotspot);
26921 if (CONSP (hotspot)
26922 && (plist = XCAR (hotspot), CONSP (plist)))
26923 {
26924 pointer = Fplist_get (plist, Qpointer);
26925 if (NILP (pointer))
26926 pointer = Qhand;
26927 help_echo_string = Fplist_get (plist, Qhelp_echo);
26928 if (!NILP (help_echo_string))
26929 {
26930 help_echo_window = window;
26931 help_echo_object = glyph->object;
26932 help_echo_pos = glyph->charpos;
26933 }
26934 }
26935 }
26936 if (NILP (pointer))
26937 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26938 }
26939 }
26940 #endif /* HAVE_WINDOW_SYSTEM */
26941
26942 /* Clear mouse face if X/Y not over text. */
26943 if (glyph == NULL
26944 || area != TEXT_AREA
26945 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26946 /* Glyph's OBJECT is an integer for glyphs inserted by the
26947 display engine for its internal purposes, like truncation
26948 and continuation glyphs and blanks beyond the end of
26949 line's text on text terminals. If we are over such a
26950 glyph, we are not over any text. */
26951 || INTEGERP (glyph->object)
26952 /* R2L rows have a stretch glyph at their front, which
26953 stands for no text, whereas L2R rows have no glyphs at
26954 all beyond the end of text. Treat such stretch glyphs
26955 like we do with NULL glyphs in L2R rows. */
26956 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26957 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26958 && glyph->type == STRETCH_GLYPH
26959 && glyph->avoid_cursor_p))
26960 {
26961 if (clear_mouse_face (hlinfo))
26962 cursor = No_Cursor;
26963 #ifdef HAVE_WINDOW_SYSTEM
26964 if (FRAME_WINDOW_P (f) && NILP (pointer))
26965 {
26966 if (area != TEXT_AREA)
26967 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26968 else
26969 pointer = Vvoid_text_area_pointer;
26970 }
26971 #endif
26972 goto set_cursor;
26973 }
26974
26975 pos = glyph->charpos;
26976 object = glyph->object;
26977 if (!STRINGP (object) && !BUFFERP (object))
26978 goto set_cursor;
26979
26980 /* If we get an out-of-range value, return now; avoid an error. */
26981 if (BUFFERP (object) && pos > BUF_Z (b))
26982 goto set_cursor;
26983
26984 /* Make the window's buffer temporarily current for
26985 overlays_at and compute_char_face. */
26986 obuf = current_buffer;
26987 current_buffer = b;
26988 obegv = BEGV;
26989 ozv = ZV;
26990 BEGV = BEG;
26991 ZV = Z;
26992
26993 /* Is this char mouse-active or does it have help-echo? */
26994 position = make_number (pos);
26995
26996 if (BUFFERP (object))
26997 {
26998 /* Put all the overlays we want in a vector in overlay_vec. */
26999 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27000 /* Sort overlays into increasing priority order. */
27001 noverlays = sort_overlays (overlay_vec, noverlays, w);
27002 }
27003 else
27004 noverlays = 0;
27005
27006 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27007
27008 if (same_region)
27009 cursor = No_Cursor;
27010
27011 /* Check mouse-face highlighting. */
27012 if (! same_region
27013 /* If there exists an overlay with mouse-face overlapping
27014 the one we are currently highlighting, we have to
27015 check if we enter the overlapping overlay, and then
27016 highlight only that. */
27017 || (OVERLAYP (hlinfo->mouse_face_overlay)
27018 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27019 {
27020 /* Find the highest priority overlay with a mouse-face. */
27021 Lisp_Object overlay = Qnil;
27022 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27023 {
27024 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27025 if (!NILP (mouse_face))
27026 overlay = overlay_vec[i];
27027 }
27028
27029 /* If we're highlighting the same overlay as before, there's
27030 no need to do that again. */
27031 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27032 goto check_help_echo;
27033 hlinfo->mouse_face_overlay = overlay;
27034
27035 /* Clear the display of the old active region, if any. */
27036 if (clear_mouse_face (hlinfo))
27037 cursor = No_Cursor;
27038
27039 /* If no overlay applies, get a text property. */
27040 if (NILP (overlay))
27041 mouse_face = Fget_text_property (position, Qmouse_face, object);
27042
27043 /* Next, compute the bounds of the mouse highlighting and
27044 display it. */
27045 if (!NILP (mouse_face) && STRINGP (object))
27046 {
27047 /* The mouse-highlighting comes from a display string
27048 with a mouse-face. */
27049 Lisp_Object s, e;
27050 ptrdiff_t ignore;
27051
27052 s = Fprevious_single_property_change
27053 (make_number (pos + 1), Qmouse_face, object, Qnil);
27054 e = Fnext_single_property_change
27055 (position, Qmouse_face, object, Qnil);
27056 if (NILP (s))
27057 s = make_number (0);
27058 if (NILP (e))
27059 e = make_number (SCHARS (object) - 1);
27060 mouse_face_from_string_pos (w, hlinfo, object,
27061 XINT (s), XINT (e));
27062 hlinfo->mouse_face_past_end = 0;
27063 hlinfo->mouse_face_window = window;
27064 hlinfo->mouse_face_face_id
27065 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27066 glyph->face_id, 1);
27067 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27068 cursor = No_Cursor;
27069 }
27070 else
27071 {
27072 /* The mouse-highlighting, if any, comes from an overlay
27073 or text property in the buffer. */
27074 Lisp_Object buffer IF_LINT (= Qnil);
27075 Lisp_Object cover_string IF_LINT (= Qnil);
27076
27077 if (STRINGP (object))
27078 {
27079 /* If we are on a display string with no mouse-face,
27080 check if the text under it has one. */
27081 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27082 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27083 pos = string_buffer_position (object, start);
27084 if (pos > 0)
27085 {
27086 mouse_face = get_char_property_and_overlay
27087 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27088 buffer = w->buffer;
27089 cover_string = object;
27090 }
27091 }
27092 else
27093 {
27094 buffer = object;
27095 cover_string = Qnil;
27096 }
27097
27098 if (!NILP (mouse_face))
27099 {
27100 Lisp_Object before, after;
27101 Lisp_Object before_string, after_string;
27102 /* To correctly find the limits of mouse highlight
27103 in a bidi-reordered buffer, we must not use the
27104 optimization of limiting the search in
27105 previous-single-property-change and
27106 next-single-property-change, because
27107 rows_from_pos_range needs the real start and end
27108 positions to DTRT in this case. That's because
27109 the first row visible in a window does not
27110 necessarily display the character whose position
27111 is the smallest. */
27112 Lisp_Object lim1 =
27113 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27114 ? Fmarker_position (w->start)
27115 : Qnil;
27116 Lisp_Object lim2 =
27117 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27118 ? make_number (BUF_Z (XBUFFER (buffer))
27119 - XFASTINT (w->window_end_pos))
27120 : Qnil;
27121
27122 if (NILP (overlay))
27123 {
27124 /* Handle the text property case. */
27125 before = Fprevious_single_property_change
27126 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27127 after = Fnext_single_property_change
27128 (make_number (pos), Qmouse_face, buffer, lim2);
27129 before_string = after_string = Qnil;
27130 }
27131 else
27132 {
27133 /* Handle the overlay case. */
27134 before = Foverlay_start (overlay);
27135 after = Foverlay_end (overlay);
27136 before_string = Foverlay_get (overlay, Qbefore_string);
27137 after_string = Foverlay_get (overlay, Qafter_string);
27138
27139 if (!STRINGP (before_string)) before_string = Qnil;
27140 if (!STRINGP (after_string)) after_string = Qnil;
27141 }
27142
27143 mouse_face_from_buffer_pos (window, hlinfo, pos,
27144 XFASTINT (before),
27145 XFASTINT (after),
27146 before_string, after_string,
27147 cover_string);
27148 cursor = No_Cursor;
27149 }
27150 }
27151 }
27152
27153 check_help_echo:
27154
27155 /* Look for a `help-echo' property. */
27156 if (NILP (help_echo_string)) {
27157 Lisp_Object help, overlay;
27158
27159 /* Check overlays first. */
27160 help = overlay = Qnil;
27161 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27162 {
27163 overlay = overlay_vec[i];
27164 help = Foverlay_get (overlay, Qhelp_echo);
27165 }
27166
27167 if (!NILP (help))
27168 {
27169 help_echo_string = help;
27170 help_echo_window = window;
27171 help_echo_object = overlay;
27172 help_echo_pos = pos;
27173 }
27174 else
27175 {
27176 Lisp_Object obj = glyph->object;
27177 ptrdiff_t charpos = glyph->charpos;
27178
27179 /* Try text properties. */
27180 if (STRINGP (obj)
27181 && charpos >= 0
27182 && charpos < SCHARS (obj))
27183 {
27184 help = Fget_text_property (make_number (charpos),
27185 Qhelp_echo, obj);
27186 if (NILP (help))
27187 {
27188 /* If the string itself doesn't specify a help-echo,
27189 see if the buffer text ``under'' it does. */
27190 struct glyph_row *r
27191 = MATRIX_ROW (w->current_matrix, vpos);
27192 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27193 ptrdiff_t p = string_buffer_position (obj, start);
27194 if (p > 0)
27195 {
27196 help = Fget_char_property (make_number (p),
27197 Qhelp_echo, w->buffer);
27198 if (!NILP (help))
27199 {
27200 charpos = p;
27201 obj = w->buffer;
27202 }
27203 }
27204 }
27205 }
27206 else if (BUFFERP (obj)
27207 && charpos >= BEGV
27208 && charpos < ZV)
27209 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27210 obj);
27211
27212 if (!NILP (help))
27213 {
27214 help_echo_string = help;
27215 help_echo_window = window;
27216 help_echo_object = obj;
27217 help_echo_pos = charpos;
27218 }
27219 }
27220 }
27221
27222 #ifdef HAVE_WINDOW_SYSTEM
27223 /* Look for a `pointer' property. */
27224 if (FRAME_WINDOW_P (f) && NILP (pointer))
27225 {
27226 /* Check overlays first. */
27227 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27228 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27229
27230 if (NILP (pointer))
27231 {
27232 Lisp_Object obj = glyph->object;
27233 ptrdiff_t charpos = glyph->charpos;
27234
27235 /* Try text properties. */
27236 if (STRINGP (obj)
27237 && charpos >= 0
27238 && charpos < SCHARS (obj))
27239 {
27240 pointer = Fget_text_property (make_number (charpos),
27241 Qpointer, obj);
27242 if (NILP (pointer))
27243 {
27244 /* If the string itself doesn't specify a pointer,
27245 see if the buffer text ``under'' it does. */
27246 struct glyph_row *r
27247 = MATRIX_ROW (w->current_matrix, vpos);
27248 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27249 ptrdiff_t p = string_buffer_position (obj, start);
27250 if (p > 0)
27251 pointer = Fget_char_property (make_number (p),
27252 Qpointer, w->buffer);
27253 }
27254 }
27255 else if (BUFFERP (obj)
27256 && charpos >= BEGV
27257 && charpos < ZV)
27258 pointer = Fget_text_property (make_number (charpos),
27259 Qpointer, obj);
27260 }
27261 }
27262 #endif /* HAVE_WINDOW_SYSTEM */
27263
27264 BEGV = obegv;
27265 ZV = ozv;
27266 current_buffer = obuf;
27267 }
27268
27269 set_cursor:
27270
27271 #ifdef HAVE_WINDOW_SYSTEM
27272 if (FRAME_WINDOW_P (f))
27273 define_frame_cursor1 (f, cursor, pointer);
27274 #else
27275 /* This is here to prevent a compiler error, about "label at end of
27276 compound statement". */
27277 return;
27278 #endif
27279 }
27280
27281
27282 /* EXPORT for RIF:
27283 Clear any mouse-face on window W. This function is part of the
27284 redisplay interface, and is called from try_window_id and similar
27285 functions to ensure the mouse-highlight is off. */
27286
27287 void
27288 x_clear_window_mouse_face (struct window *w)
27289 {
27290 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27291 Lisp_Object window;
27292
27293 BLOCK_INPUT;
27294 XSETWINDOW (window, w);
27295 if (EQ (window, hlinfo->mouse_face_window))
27296 clear_mouse_face (hlinfo);
27297 UNBLOCK_INPUT;
27298 }
27299
27300
27301 /* EXPORT:
27302 Just discard the mouse face information for frame F, if any.
27303 This is used when the size of F is changed. */
27304
27305 void
27306 cancel_mouse_face (struct frame *f)
27307 {
27308 Lisp_Object window;
27309 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27310
27311 window = hlinfo->mouse_face_window;
27312 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27313 {
27314 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27315 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27316 hlinfo->mouse_face_window = Qnil;
27317 }
27318 }
27319
27320
27321 \f
27322 /***********************************************************************
27323 Exposure Events
27324 ***********************************************************************/
27325
27326 #ifdef HAVE_WINDOW_SYSTEM
27327
27328 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27329 which intersects rectangle R. R is in window-relative coordinates. */
27330
27331 static void
27332 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27333 enum glyph_row_area area)
27334 {
27335 struct glyph *first = row->glyphs[area];
27336 struct glyph *end = row->glyphs[area] + row->used[area];
27337 struct glyph *last;
27338 int first_x, start_x, x;
27339
27340 if (area == TEXT_AREA && row->fill_line_p)
27341 /* If row extends face to end of line write the whole line. */
27342 draw_glyphs (w, 0, row, area,
27343 0, row->used[area],
27344 DRAW_NORMAL_TEXT, 0);
27345 else
27346 {
27347 /* Set START_X to the window-relative start position for drawing glyphs of
27348 AREA. The first glyph of the text area can be partially visible.
27349 The first glyphs of other areas cannot. */
27350 start_x = window_box_left_offset (w, area);
27351 x = start_x;
27352 if (area == TEXT_AREA)
27353 x += row->x;
27354
27355 /* Find the first glyph that must be redrawn. */
27356 while (first < end
27357 && x + first->pixel_width < r->x)
27358 {
27359 x += first->pixel_width;
27360 ++first;
27361 }
27362
27363 /* Find the last one. */
27364 last = first;
27365 first_x = x;
27366 while (last < end
27367 && x < r->x + r->width)
27368 {
27369 x += last->pixel_width;
27370 ++last;
27371 }
27372
27373 /* Repaint. */
27374 if (last > first)
27375 draw_glyphs (w, first_x - start_x, row, area,
27376 first - row->glyphs[area], last - row->glyphs[area],
27377 DRAW_NORMAL_TEXT, 0);
27378 }
27379 }
27380
27381
27382 /* Redraw the parts of the glyph row ROW on window W intersecting
27383 rectangle R. R is in window-relative coordinates. Value is
27384 non-zero if mouse-face was overwritten. */
27385
27386 static int
27387 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27388 {
27389 xassert (row->enabled_p);
27390
27391 if (row->mode_line_p || w->pseudo_window_p)
27392 draw_glyphs (w, 0, row, TEXT_AREA,
27393 0, row->used[TEXT_AREA],
27394 DRAW_NORMAL_TEXT, 0);
27395 else
27396 {
27397 if (row->used[LEFT_MARGIN_AREA])
27398 expose_area (w, row, r, LEFT_MARGIN_AREA);
27399 if (row->used[TEXT_AREA])
27400 expose_area (w, row, r, TEXT_AREA);
27401 if (row->used[RIGHT_MARGIN_AREA])
27402 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27403 draw_row_fringe_bitmaps (w, row);
27404 }
27405
27406 return row->mouse_face_p;
27407 }
27408
27409
27410 /* Redraw those parts of glyphs rows during expose event handling that
27411 overlap other rows. Redrawing of an exposed line writes over parts
27412 of lines overlapping that exposed line; this function fixes that.
27413
27414 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27415 row in W's current matrix that is exposed and overlaps other rows.
27416 LAST_OVERLAPPING_ROW is the last such row. */
27417
27418 static void
27419 expose_overlaps (struct window *w,
27420 struct glyph_row *first_overlapping_row,
27421 struct glyph_row *last_overlapping_row,
27422 XRectangle *r)
27423 {
27424 struct glyph_row *row;
27425
27426 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27427 if (row->overlapping_p)
27428 {
27429 xassert (row->enabled_p && !row->mode_line_p);
27430
27431 row->clip = r;
27432 if (row->used[LEFT_MARGIN_AREA])
27433 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27434
27435 if (row->used[TEXT_AREA])
27436 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27437
27438 if (row->used[RIGHT_MARGIN_AREA])
27439 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27440 row->clip = NULL;
27441 }
27442 }
27443
27444
27445 /* Return non-zero if W's cursor intersects rectangle R. */
27446
27447 static int
27448 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27449 {
27450 XRectangle cr, result;
27451 struct glyph *cursor_glyph;
27452 struct glyph_row *row;
27453
27454 if (w->phys_cursor.vpos >= 0
27455 && w->phys_cursor.vpos < w->current_matrix->nrows
27456 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27457 row->enabled_p)
27458 && row->cursor_in_fringe_p)
27459 {
27460 /* Cursor is in the fringe. */
27461 cr.x = window_box_right_offset (w,
27462 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27463 ? RIGHT_MARGIN_AREA
27464 : TEXT_AREA));
27465 cr.y = row->y;
27466 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27467 cr.height = row->height;
27468 return x_intersect_rectangles (&cr, r, &result);
27469 }
27470
27471 cursor_glyph = get_phys_cursor_glyph (w);
27472 if (cursor_glyph)
27473 {
27474 /* r is relative to W's box, but w->phys_cursor.x is relative
27475 to left edge of W's TEXT area. Adjust it. */
27476 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27477 cr.y = w->phys_cursor.y;
27478 cr.width = cursor_glyph->pixel_width;
27479 cr.height = w->phys_cursor_height;
27480 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27481 I assume the effect is the same -- and this is portable. */
27482 return x_intersect_rectangles (&cr, r, &result);
27483 }
27484 /* If we don't understand the format, pretend we're not in the hot-spot. */
27485 return 0;
27486 }
27487
27488
27489 /* EXPORT:
27490 Draw a vertical window border to the right of window W if W doesn't
27491 have vertical scroll bars. */
27492
27493 void
27494 x_draw_vertical_border (struct window *w)
27495 {
27496 struct frame *f = XFRAME (WINDOW_FRAME (w));
27497
27498 /* We could do better, if we knew what type of scroll-bar the adjacent
27499 windows (on either side) have... But we don't :-(
27500 However, I think this works ok. ++KFS 2003-04-25 */
27501
27502 /* Redraw borders between horizontally adjacent windows. Don't
27503 do it for frames with vertical scroll bars because either the
27504 right scroll bar of a window, or the left scroll bar of its
27505 neighbor will suffice as a border. */
27506 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27507 return;
27508
27509 if (!WINDOW_RIGHTMOST_P (w)
27510 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27511 {
27512 int x0, x1, y0, y1;
27513
27514 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27515 y1 -= 1;
27516
27517 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27518 x1 -= 1;
27519
27520 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27521 }
27522 else if (!WINDOW_LEFTMOST_P (w)
27523 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27524 {
27525 int x0, x1, y0, y1;
27526
27527 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27528 y1 -= 1;
27529
27530 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27531 x0 -= 1;
27532
27533 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27534 }
27535 }
27536
27537
27538 /* Redraw the part of window W intersection rectangle FR. Pixel
27539 coordinates in FR are frame-relative. Call this function with
27540 input blocked. Value is non-zero if the exposure overwrites
27541 mouse-face. */
27542
27543 static int
27544 expose_window (struct window *w, XRectangle *fr)
27545 {
27546 struct frame *f = XFRAME (w->frame);
27547 XRectangle wr, r;
27548 int mouse_face_overwritten_p = 0;
27549
27550 /* If window is not yet fully initialized, do nothing. This can
27551 happen when toolkit scroll bars are used and a window is split.
27552 Reconfiguring the scroll bar will generate an expose for a newly
27553 created window. */
27554 if (w->current_matrix == NULL)
27555 return 0;
27556
27557 /* When we're currently updating the window, display and current
27558 matrix usually don't agree. Arrange for a thorough display
27559 later. */
27560 if (w == updated_window)
27561 {
27562 SET_FRAME_GARBAGED (f);
27563 return 0;
27564 }
27565
27566 /* Frame-relative pixel rectangle of W. */
27567 wr.x = WINDOW_LEFT_EDGE_X (w);
27568 wr.y = WINDOW_TOP_EDGE_Y (w);
27569 wr.width = WINDOW_TOTAL_WIDTH (w);
27570 wr.height = WINDOW_TOTAL_HEIGHT (w);
27571
27572 if (x_intersect_rectangles (fr, &wr, &r))
27573 {
27574 int yb = window_text_bottom_y (w);
27575 struct glyph_row *row;
27576 int cursor_cleared_p, phys_cursor_on_p;
27577 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27578
27579 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27580 r.x, r.y, r.width, r.height));
27581
27582 /* Convert to window coordinates. */
27583 r.x -= WINDOW_LEFT_EDGE_X (w);
27584 r.y -= WINDOW_TOP_EDGE_Y (w);
27585
27586 /* Turn off the cursor. */
27587 if (!w->pseudo_window_p
27588 && phys_cursor_in_rect_p (w, &r))
27589 {
27590 x_clear_cursor (w);
27591 cursor_cleared_p = 1;
27592 }
27593 else
27594 cursor_cleared_p = 0;
27595
27596 /* If the row containing the cursor extends face to end of line,
27597 then expose_area might overwrite the cursor outside the
27598 rectangle and thus notice_overwritten_cursor might clear
27599 w->phys_cursor_on_p. We remember the original value and
27600 check later if it is changed. */
27601 phys_cursor_on_p = w->phys_cursor_on_p;
27602
27603 /* Update lines intersecting rectangle R. */
27604 first_overlapping_row = last_overlapping_row = NULL;
27605 for (row = w->current_matrix->rows;
27606 row->enabled_p;
27607 ++row)
27608 {
27609 int y0 = row->y;
27610 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27611
27612 if ((y0 >= r.y && y0 < r.y + r.height)
27613 || (y1 > r.y && y1 < r.y + r.height)
27614 || (r.y >= y0 && r.y < y1)
27615 || (r.y + r.height > y0 && r.y + r.height < y1))
27616 {
27617 /* A header line may be overlapping, but there is no need
27618 to fix overlapping areas for them. KFS 2005-02-12 */
27619 if (row->overlapping_p && !row->mode_line_p)
27620 {
27621 if (first_overlapping_row == NULL)
27622 first_overlapping_row = row;
27623 last_overlapping_row = row;
27624 }
27625
27626 row->clip = fr;
27627 if (expose_line (w, row, &r))
27628 mouse_face_overwritten_p = 1;
27629 row->clip = NULL;
27630 }
27631 else if (row->overlapping_p)
27632 {
27633 /* We must redraw a row overlapping the exposed area. */
27634 if (y0 < r.y
27635 ? y0 + row->phys_height > r.y
27636 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27637 {
27638 if (first_overlapping_row == NULL)
27639 first_overlapping_row = row;
27640 last_overlapping_row = row;
27641 }
27642 }
27643
27644 if (y1 >= yb)
27645 break;
27646 }
27647
27648 /* Display the mode line if there is one. */
27649 if (WINDOW_WANTS_MODELINE_P (w)
27650 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27651 row->enabled_p)
27652 && row->y < r.y + r.height)
27653 {
27654 if (expose_line (w, row, &r))
27655 mouse_face_overwritten_p = 1;
27656 }
27657
27658 if (!w->pseudo_window_p)
27659 {
27660 /* Fix the display of overlapping rows. */
27661 if (first_overlapping_row)
27662 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27663 fr);
27664
27665 /* Draw border between windows. */
27666 x_draw_vertical_border (w);
27667
27668 /* Turn the cursor on again. */
27669 if (cursor_cleared_p
27670 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27671 update_window_cursor (w, 1);
27672 }
27673 }
27674
27675 return mouse_face_overwritten_p;
27676 }
27677
27678
27679
27680 /* Redraw (parts) of all windows in the window tree rooted at W that
27681 intersect R. R contains frame pixel coordinates. Value is
27682 non-zero if the exposure overwrites mouse-face. */
27683
27684 static int
27685 expose_window_tree (struct window *w, XRectangle *r)
27686 {
27687 struct frame *f = XFRAME (w->frame);
27688 int mouse_face_overwritten_p = 0;
27689
27690 while (w && !FRAME_GARBAGED_P (f))
27691 {
27692 if (!NILP (w->hchild))
27693 mouse_face_overwritten_p
27694 |= expose_window_tree (XWINDOW (w->hchild), r);
27695 else if (!NILP (w->vchild))
27696 mouse_face_overwritten_p
27697 |= expose_window_tree (XWINDOW (w->vchild), r);
27698 else
27699 mouse_face_overwritten_p |= expose_window (w, r);
27700
27701 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27702 }
27703
27704 return mouse_face_overwritten_p;
27705 }
27706
27707
27708 /* EXPORT:
27709 Redisplay an exposed area of frame F. X and Y are the upper-left
27710 corner of the exposed rectangle. W and H are width and height of
27711 the exposed area. All are pixel values. W or H zero means redraw
27712 the entire frame. */
27713
27714 void
27715 expose_frame (struct frame *f, int x, int y, int w, int h)
27716 {
27717 XRectangle r;
27718 int mouse_face_overwritten_p = 0;
27719
27720 TRACE ((stderr, "expose_frame "));
27721
27722 /* No need to redraw if frame will be redrawn soon. */
27723 if (FRAME_GARBAGED_P (f))
27724 {
27725 TRACE ((stderr, " garbaged\n"));
27726 return;
27727 }
27728
27729 /* If basic faces haven't been realized yet, there is no point in
27730 trying to redraw anything. This can happen when we get an expose
27731 event while Emacs is starting, e.g. by moving another window. */
27732 if (FRAME_FACE_CACHE (f) == NULL
27733 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27734 {
27735 TRACE ((stderr, " no faces\n"));
27736 return;
27737 }
27738
27739 if (w == 0 || h == 0)
27740 {
27741 r.x = r.y = 0;
27742 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27743 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27744 }
27745 else
27746 {
27747 r.x = x;
27748 r.y = y;
27749 r.width = w;
27750 r.height = h;
27751 }
27752
27753 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27754 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27755
27756 if (WINDOWP (f->tool_bar_window))
27757 mouse_face_overwritten_p
27758 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27759
27760 #ifdef HAVE_X_WINDOWS
27761 #ifndef MSDOS
27762 #ifndef USE_X_TOOLKIT
27763 if (WINDOWP (f->menu_bar_window))
27764 mouse_face_overwritten_p
27765 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27766 #endif /* not USE_X_TOOLKIT */
27767 #endif
27768 #endif
27769
27770 /* Some window managers support a focus-follows-mouse style with
27771 delayed raising of frames. Imagine a partially obscured frame,
27772 and moving the mouse into partially obscured mouse-face on that
27773 frame. The visible part of the mouse-face will be highlighted,
27774 then the WM raises the obscured frame. With at least one WM, KDE
27775 2.1, Emacs is not getting any event for the raising of the frame
27776 (even tried with SubstructureRedirectMask), only Expose events.
27777 These expose events will draw text normally, i.e. not
27778 highlighted. Which means we must redo the highlight here.
27779 Subsume it under ``we love X''. --gerd 2001-08-15 */
27780 /* Included in Windows version because Windows most likely does not
27781 do the right thing if any third party tool offers
27782 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27783 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27784 {
27785 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27786 if (f == hlinfo->mouse_face_mouse_frame)
27787 {
27788 int mouse_x = hlinfo->mouse_face_mouse_x;
27789 int mouse_y = hlinfo->mouse_face_mouse_y;
27790 clear_mouse_face (hlinfo);
27791 note_mouse_highlight (f, mouse_x, mouse_y);
27792 }
27793 }
27794 }
27795
27796
27797 /* EXPORT:
27798 Determine the intersection of two rectangles R1 and R2. Return
27799 the intersection in *RESULT. Value is non-zero if RESULT is not
27800 empty. */
27801
27802 int
27803 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27804 {
27805 XRectangle *left, *right;
27806 XRectangle *upper, *lower;
27807 int intersection_p = 0;
27808
27809 /* Rearrange so that R1 is the left-most rectangle. */
27810 if (r1->x < r2->x)
27811 left = r1, right = r2;
27812 else
27813 left = r2, right = r1;
27814
27815 /* X0 of the intersection is right.x0, if this is inside R1,
27816 otherwise there is no intersection. */
27817 if (right->x <= left->x + left->width)
27818 {
27819 result->x = right->x;
27820
27821 /* The right end of the intersection is the minimum of
27822 the right ends of left and right. */
27823 result->width = (min (left->x + left->width, right->x + right->width)
27824 - result->x);
27825
27826 /* Same game for Y. */
27827 if (r1->y < r2->y)
27828 upper = r1, lower = r2;
27829 else
27830 upper = r2, lower = r1;
27831
27832 /* The upper end of the intersection is lower.y0, if this is inside
27833 of upper. Otherwise, there is no intersection. */
27834 if (lower->y <= upper->y + upper->height)
27835 {
27836 result->y = lower->y;
27837
27838 /* The lower end of the intersection is the minimum of the lower
27839 ends of upper and lower. */
27840 result->height = (min (lower->y + lower->height,
27841 upper->y + upper->height)
27842 - result->y);
27843 intersection_p = 1;
27844 }
27845 }
27846
27847 return intersection_p;
27848 }
27849
27850 #endif /* HAVE_WINDOW_SYSTEM */
27851
27852 \f
27853 /***********************************************************************
27854 Initialization
27855 ***********************************************************************/
27856
27857 void
27858 syms_of_xdisp (void)
27859 {
27860 Vwith_echo_area_save_vector = Qnil;
27861 staticpro (&Vwith_echo_area_save_vector);
27862
27863 Vmessage_stack = Qnil;
27864 staticpro (&Vmessage_stack);
27865
27866 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27867
27868 message_dolog_marker1 = Fmake_marker ();
27869 staticpro (&message_dolog_marker1);
27870 message_dolog_marker2 = Fmake_marker ();
27871 staticpro (&message_dolog_marker2);
27872 message_dolog_marker3 = Fmake_marker ();
27873 staticpro (&message_dolog_marker3);
27874
27875 #if GLYPH_DEBUG
27876 defsubr (&Sdump_frame_glyph_matrix);
27877 defsubr (&Sdump_glyph_matrix);
27878 defsubr (&Sdump_glyph_row);
27879 defsubr (&Sdump_tool_bar_row);
27880 defsubr (&Strace_redisplay);
27881 defsubr (&Strace_to_stderr);
27882 #endif
27883 #ifdef HAVE_WINDOW_SYSTEM
27884 defsubr (&Stool_bar_lines_needed);
27885 defsubr (&Slookup_image_map);
27886 #endif
27887 defsubr (&Sformat_mode_line);
27888 defsubr (&Sinvisible_p);
27889 defsubr (&Scurrent_bidi_paragraph_direction);
27890
27891 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27892 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27893 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27894 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27895 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27896 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27897 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27898 DEFSYM (Qeval, "eval");
27899 DEFSYM (QCdata, ":data");
27900 DEFSYM (Qdisplay, "display");
27901 DEFSYM (Qspace_width, "space-width");
27902 DEFSYM (Qraise, "raise");
27903 DEFSYM (Qslice, "slice");
27904 DEFSYM (Qspace, "space");
27905 DEFSYM (Qmargin, "margin");
27906 DEFSYM (Qpointer, "pointer");
27907 DEFSYM (Qleft_margin, "left-margin");
27908 DEFSYM (Qright_margin, "right-margin");
27909 DEFSYM (Qcenter, "center");
27910 DEFSYM (Qline_height, "line-height");
27911 DEFSYM (QCalign_to, ":align-to");
27912 DEFSYM (QCrelative_width, ":relative-width");
27913 DEFSYM (QCrelative_height, ":relative-height");
27914 DEFSYM (QCeval, ":eval");
27915 DEFSYM (QCpropertize, ":propertize");
27916 DEFSYM (QCfile, ":file");
27917 DEFSYM (Qfontified, "fontified");
27918 DEFSYM (Qfontification_functions, "fontification-functions");
27919 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27920 DEFSYM (Qescape_glyph, "escape-glyph");
27921 DEFSYM (Qnobreak_space, "nobreak-space");
27922 DEFSYM (Qimage, "image");
27923 DEFSYM (Qtext, "text");
27924 DEFSYM (Qboth, "both");
27925 DEFSYM (Qboth_horiz, "both-horiz");
27926 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27927 DEFSYM (QCmap, ":map");
27928 DEFSYM (QCpointer, ":pointer");
27929 DEFSYM (Qrect, "rect");
27930 DEFSYM (Qcircle, "circle");
27931 DEFSYM (Qpoly, "poly");
27932 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27933 DEFSYM (Qgrow_only, "grow-only");
27934 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27935 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27936 DEFSYM (Qposition, "position");
27937 DEFSYM (Qbuffer_position, "buffer-position");
27938 DEFSYM (Qobject, "object");
27939 DEFSYM (Qbar, "bar");
27940 DEFSYM (Qhbar, "hbar");
27941 DEFSYM (Qbox, "box");
27942 DEFSYM (Qhollow, "hollow");
27943 DEFSYM (Qhand, "hand");
27944 DEFSYM (Qarrow, "arrow");
27945 DEFSYM (Qtext, "text");
27946 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27947
27948 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27949 Fcons (intern_c_string ("void-variable"), Qnil)),
27950 Qnil);
27951 staticpro (&list_of_error);
27952
27953 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27954 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27955 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27956 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27957
27958 echo_buffer[0] = echo_buffer[1] = Qnil;
27959 staticpro (&echo_buffer[0]);
27960 staticpro (&echo_buffer[1]);
27961
27962 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27963 staticpro (&echo_area_buffer[0]);
27964 staticpro (&echo_area_buffer[1]);
27965
27966 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27967 staticpro (&Vmessages_buffer_name);
27968
27969 mode_line_proptrans_alist = Qnil;
27970 staticpro (&mode_line_proptrans_alist);
27971 mode_line_string_list = Qnil;
27972 staticpro (&mode_line_string_list);
27973 mode_line_string_face = Qnil;
27974 staticpro (&mode_line_string_face);
27975 mode_line_string_face_prop = Qnil;
27976 staticpro (&mode_line_string_face_prop);
27977 Vmode_line_unwind_vector = Qnil;
27978 staticpro (&Vmode_line_unwind_vector);
27979
27980 help_echo_string = Qnil;
27981 staticpro (&help_echo_string);
27982 help_echo_object = Qnil;
27983 staticpro (&help_echo_object);
27984 help_echo_window = Qnil;
27985 staticpro (&help_echo_window);
27986 previous_help_echo_string = Qnil;
27987 staticpro (&previous_help_echo_string);
27988 help_echo_pos = -1;
27989
27990 DEFSYM (Qright_to_left, "right-to-left");
27991 DEFSYM (Qleft_to_right, "left-to-right");
27992
27993 #ifdef HAVE_WINDOW_SYSTEM
27994 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27995 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27996 For example, if a block cursor is over a tab, it will be drawn as
27997 wide as that tab on the display. */);
27998 x_stretch_cursor_p = 0;
27999 #endif
28000
28001 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28002 doc: /* *Non-nil means highlight trailing whitespace.
28003 The face used for trailing whitespace is `trailing-whitespace'. */);
28004 Vshow_trailing_whitespace = Qnil;
28005
28006 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28007 doc: /* *Control highlighting of nobreak space and soft hyphen.
28008 A value of t means highlight the character itself (for nobreak space,
28009 use face `nobreak-space').
28010 A value of nil means no highlighting.
28011 Other values mean display the escape glyph followed by an ordinary
28012 space or ordinary hyphen. */);
28013 Vnobreak_char_display = Qt;
28014
28015 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28016 doc: /* *The pointer shape to show in void text areas.
28017 A value of nil means to show the text pointer. Other options are `arrow',
28018 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28019 Vvoid_text_area_pointer = Qarrow;
28020
28021 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28022 doc: /* Non-nil means don't actually do any redisplay.
28023 This is used for internal purposes. */);
28024 Vinhibit_redisplay = Qnil;
28025
28026 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28027 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28028 Vglobal_mode_string = Qnil;
28029
28030 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28031 doc: /* Marker for where to display an arrow on top of the buffer text.
28032 This must be the beginning of a line in order to work.
28033 See also `overlay-arrow-string'. */);
28034 Voverlay_arrow_position = Qnil;
28035
28036 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28037 doc: /* String to display as an arrow in non-window frames.
28038 See also `overlay-arrow-position'. */);
28039 Voverlay_arrow_string = make_pure_c_string ("=>");
28040
28041 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28042 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28043 The symbols on this list are examined during redisplay to determine
28044 where to display overlay arrows. */);
28045 Voverlay_arrow_variable_list
28046 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28047
28048 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28049 doc: /* *The number of lines to try scrolling a window by when point moves out.
28050 If that fails to bring point back on frame, point is centered instead.
28051 If this is zero, point is always centered after it moves off frame.
28052 If you want scrolling to always be a line at a time, you should set
28053 `scroll-conservatively' to a large value rather than set this to 1. */);
28054
28055 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28056 doc: /* *Scroll up to this many lines, to bring point back on screen.
28057 If point moves off-screen, redisplay will scroll by up to
28058 `scroll-conservatively' lines in order to bring point just barely
28059 onto the screen again. If that cannot be done, then redisplay
28060 recenters point as usual.
28061
28062 If the value is greater than 100, redisplay will never recenter point,
28063 but will always scroll just enough text to bring point into view, even
28064 if you move far away.
28065
28066 A value of zero means always recenter point if it moves off screen. */);
28067 scroll_conservatively = 0;
28068
28069 DEFVAR_INT ("scroll-margin", scroll_margin,
28070 doc: /* *Number of lines of margin at the top and bottom of a window.
28071 Recenter the window whenever point gets within this many lines
28072 of the top or bottom of the window. */);
28073 scroll_margin = 0;
28074
28075 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28076 doc: /* Pixels per inch value for non-window system displays.
28077 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28078 Vdisplay_pixels_per_inch = make_float (72.0);
28079
28080 #if GLYPH_DEBUG
28081 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28082 #endif
28083
28084 DEFVAR_LISP ("truncate-partial-width-windows",
28085 Vtruncate_partial_width_windows,
28086 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28087 For an integer value, truncate lines in each window narrower than the
28088 full frame width, provided the window width is less than that integer;
28089 otherwise, respect the value of `truncate-lines'.
28090
28091 For any other non-nil value, truncate lines in all windows that do
28092 not span the full frame width.
28093
28094 A value of nil means to respect the value of `truncate-lines'.
28095
28096 If `word-wrap' is enabled, you might want to reduce this. */);
28097 Vtruncate_partial_width_windows = make_number (50);
28098
28099 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28100 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28101 Any other value means to use the appropriate face, `mode-line',
28102 `header-line', or `menu' respectively. */);
28103 mode_line_inverse_video = 1;
28104
28105 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28106 doc: /* *Maximum buffer size for which line number should be displayed.
28107 If the buffer is bigger than this, the line number does not appear
28108 in the mode line. A value of nil means no limit. */);
28109 Vline_number_display_limit = Qnil;
28110
28111 DEFVAR_INT ("line-number-display-limit-width",
28112 line_number_display_limit_width,
28113 doc: /* *Maximum line width (in characters) for line number display.
28114 If the average length of the lines near point is bigger than this, then the
28115 line number may be omitted from the mode line. */);
28116 line_number_display_limit_width = 200;
28117
28118 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28119 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28120 highlight_nonselected_windows = 0;
28121
28122 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28123 doc: /* Non-nil if more than one frame is visible on this display.
28124 Minibuffer-only frames don't count, but iconified frames do.
28125 This variable is not guaranteed to be accurate except while processing
28126 `frame-title-format' and `icon-title-format'. */);
28127
28128 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28129 doc: /* Template for displaying the title bar of visible frames.
28130 \(Assuming the window manager supports this feature.)
28131
28132 This variable has the same structure as `mode-line-format', except that
28133 the %c and %l constructs are ignored. It is used only on frames for
28134 which no explicit name has been set \(see `modify-frame-parameters'). */);
28135
28136 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28137 doc: /* Template for displaying the title bar of an iconified frame.
28138 \(Assuming the window manager supports this feature.)
28139 This variable has the same structure as `mode-line-format' (which see),
28140 and is used only on frames for which no explicit name has been set
28141 \(see `modify-frame-parameters'). */);
28142 Vicon_title_format
28143 = Vframe_title_format
28144 = pure_cons (intern_c_string ("multiple-frames"),
28145 pure_cons (make_pure_c_string ("%b"),
28146 pure_cons (pure_cons (empty_unibyte_string,
28147 pure_cons (intern_c_string ("invocation-name"),
28148 pure_cons (make_pure_c_string ("@"),
28149 pure_cons (intern_c_string ("system-name"),
28150 Qnil)))),
28151 Qnil)));
28152
28153 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28154 doc: /* Maximum number of lines to keep in the message log buffer.
28155 If nil, disable message logging. If t, log messages but don't truncate
28156 the buffer when it becomes large. */);
28157 Vmessage_log_max = make_number (100);
28158
28159 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28160 doc: /* Functions called before redisplay, if window sizes have changed.
28161 The value should be a list of functions that take one argument.
28162 Just before redisplay, for each frame, if any of its windows have changed
28163 size since the last redisplay, or have been split or deleted,
28164 all the functions in the list are called, with the frame as argument. */);
28165 Vwindow_size_change_functions = Qnil;
28166
28167 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28168 doc: /* List of functions to call before redisplaying a window with scrolling.
28169 Each function is called with two arguments, the window and its new
28170 display-start position. Note that these functions are also called by
28171 `set-window-buffer'. Also note that the value of `window-end' is not
28172 valid when these functions are called. */);
28173 Vwindow_scroll_functions = Qnil;
28174
28175 DEFVAR_LISP ("window-text-change-functions",
28176 Vwindow_text_change_functions,
28177 doc: /* Functions to call in redisplay when text in the window might change. */);
28178 Vwindow_text_change_functions = Qnil;
28179
28180 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28181 doc: /* Functions called when redisplay of a window reaches the end trigger.
28182 Each function is called with two arguments, the window and the end trigger value.
28183 See `set-window-redisplay-end-trigger'. */);
28184 Vredisplay_end_trigger_functions = Qnil;
28185
28186 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28187 doc: /* *Non-nil means autoselect window with mouse pointer.
28188 If nil, do not autoselect windows.
28189 A positive number means delay autoselection by that many seconds: a
28190 window is autoselected only after the mouse has remained in that
28191 window for the duration of the delay.
28192 A negative number has a similar effect, but causes windows to be
28193 autoselected only after the mouse has stopped moving. \(Because of
28194 the way Emacs compares mouse events, you will occasionally wait twice
28195 that time before the window gets selected.\)
28196 Any other value means to autoselect window instantaneously when the
28197 mouse pointer enters it.
28198
28199 Autoselection selects the minibuffer only if it is active, and never
28200 unselects the minibuffer if it is active.
28201
28202 When customizing this variable make sure that the actual value of
28203 `focus-follows-mouse' matches the behavior of your window manager. */);
28204 Vmouse_autoselect_window = Qnil;
28205
28206 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28207 doc: /* *Non-nil means automatically resize tool-bars.
28208 This dynamically changes the tool-bar's height to the minimum height
28209 that is needed to make all tool-bar items visible.
28210 If value is `grow-only', the tool-bar's height is only increased
28211 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28212 Vauto_resize_tool_bars = Qt;
28213
28214 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28215 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28216 auto_raise_tool_bar_buttons_p = 1;
28217
28218 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28219 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28220 make_cursor_line_fully_visible_p = 1;
28221
28222 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28223 doc: /* *Border below tool-bar in pixels.
28224 If an integer, use it as the height of the border.
28225 If it is one of `internal-border-width' or `border-width', use the
28226 value of the corresponding frame parameter.
28227 Otherwise, no border is added below the tool-bar. */);
28228 Vtool_bar_border = Qinternal_border_width;
28229
28230 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28231 doc: /* *Margin around tool-bar buttons in pixels.
28232 If an integer, use that for both horizontal and vertical margins.
28233 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28234 HORZ specifying the horizontal margin, and VERT specifying the
28235 vertical margin. */);
28236 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28237
28238 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28239 doc: /* *Relief thickness of tool-bar buttons. */);
28240 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28241
28242 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28243 doc: /* Tool bar style to use.
28244 It can be one of
28245 image - show images only
28246 text - show text only
28247 both - show both, text below image
28248 both-horiz - show text to the right of the image
28249 text-image-horiz - show text to the left of the image
28250 any other - use system default or image if no system default. */);
28251 Vtool_bar_style = Qnil;
28252
28253 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28254 doc: /* *Maximum number of characters a label can have to be shown.
28255 The tool bar style must also show labels for this to have any effect, see
28256 `tool-bar-style'. */);
28257 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28258
28259 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28260 doc: /* List of functions to call to fontify regions of text.
28261 Each function is called with one argument POS. Functions must
28262 fontify a region starting at POS in the current buffer, and give
28263 fontified regions the property `fontified'. */);
28264 Vfontification_functions = Qnil;
28265 Fmake_variable_buffer_local (Qfontification_functions);
28266
28267 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28268 unibyte_display_via_language_environment,
28269 doc: /* *Non-nil means display unibyte text according to language environment.
28270 Specifically, this means that raw bytes in the range 160-255 decimal
28271 are displayed by converting them to the equivalent multibyte characters
28272 according to the current language environment. As a result, they are
28273 displayed according to the current fontset.
28274
28275 Note that this variable affects only how these bytes are displayed,
28276 but does not change the fact they are interpreted as raw bytes. */);
28277 unibyte_display_via_language_environment = 0;
28278
28279 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28280 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28281 If a float, it specifies a fraction of the mini-window frame's height.
28282 If an integer, it specifies a number of lines. */);
28283 Vmax_mini_window_height = make_float (0.25);
28284
28285 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28286 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28287 A value of nil means don't automatically resize mini-windows.
28288 A value of t means resize them to fit the text displayed in them.
28289 A value of `grow-only', the default, means let mini-windows grow only;
28290 they return to their normal size when the minibuffer is closed, or the
28291 echo area becomes empty. */);
28292 Vresize_mini_windows = Qgrow_only;
28293
28294 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28295 doc: /* Alist specifying how to blink the cursor off.
28296 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28297 `cursor-type' frame-parameter or variable equals ON-STATE,
28298 comparing using `equal', Emacs uses OFF-STATE to specify
28299 how to blink it off. ON-STATE and OFF-STATE are values for
28300 the `cursor-type' frame parameter.
28301
28302 If a frame's ON-STATE has no entry in this list,
28303 the frame's other specifications determine how to blink the cursor off. */);
28304 Vblink_cursor_alist = Qnil;
28305
28306 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28307 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28308 If non-nil, windows are automatically scrolled horizontally to make
28309 point visible. */);
28310 automatic_hscrolling_p = 1;
28311 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28312
28313 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28314 doc: /* *How many columns away from the window edge point is allowed to get
28315 before automatic hscrolling will horizontally scroll the window. */);
28316 hscroll_margin = 5;
28317
28318 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28319 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28320 When point is less than `hscroll-margin' columns from the window
28321 edge, automatic hscrolling will scroll the window by the amount of columns
28322 determined by this variable. If its value is a positive integer, scroll that
28323 many columns. If it's a positive floating-point number, it specifies the
28324 fraction of the window's width to scroll. If it's nil or zero, point will be
28325 centered horizontally after the scroll. Any other value, including negative
28326 numbers, are treated as if the value were zero.
28327
28328 Automatic hscrolling always moves point outside the scroll margin, so if
28329 point was more than scroll step columns inside the margin, the window will
28330 scroll more than the value given by the scroll step.
28331
28332 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28333 and `scroll-right' overrides this variable's effect. */);
28334 Vhscroll_step = make_number (0);
28335
28336 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28337 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28338 Bind this around calls to `message' to let it take effect. */);
28339 message_truncate_lines = 0;
28340
28341 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28342 doc: /* Normal hook run to update the menu bar definitions.
28343 Redisplay runs this hook before it redisplays the menu bar.
28344 This is used to update submenus such as Buffers,
28345 whose contents depend on various data. */);
28346 Vmenu_bar_update_hook = Qnil;
28347
28348 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28349 doc: /* Frame for which we are updating a menu.
28350 The enable predicate for a menu binding should check this variable. */);
28351 Vmenu_updating_frame = Qnil;
28352
28353 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28354 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28355 inhibit_menubar_update = 0;
28356
28357 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28358 doc: /* Prefix prepended to all continuation lines at display time.
28359 The value may be a string, an image, or a stretch-glyph; it is
28360 interpreted in the same way as the value of a `display' text property.
28361
28362 This variable is overridden by any `wrap-prefix' text or overlay
28363 property.
28364
28365 To add a prefix to non-continuation lines, use `line-prefix'. */);
28366 Vwrap_prefix = Qnil;
28367 DEFSYM (Qwrap_prefix, "wrap-prefix");
28368 Fmake_variable_buffer_local (Qwrap_prefix);
28369
28370 DEFVAR_LISP ("line-prefix", Vline_prefix,
28371 doc: /* Prefix prepended to all non-continuation lines at display time.
28372 The value may be a string, an image, or a stretch-glyph; it is
28373 interpreted in the same way as the value of a `display' text property.
28374
28375 This variable is overridden by any `line-prefix' text or overlay
28376 property.
28377
28378 To add a prefix to continuation lines, use `wrap-prefix'. */);
28379 Vline_prefix = Qnil;
28380 DEFSYM (Qline_prefix, "line-prefix");
28381 Fmake_variable_buffer_local (Qline_prefix);
28382
28383 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28384 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28385 inhibit_eval_during_redisplay = 0;
28386
28387 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28388 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28389 inhibit_free_realized_faces = 0;
28390
28391 #if GLYPH_DEBUG
28392 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28393 doc: /* Inhibit try_window_id display optimization. */);
28394 inhibit_try_window_id = 0;
28395
28396 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28397 doc: /* Inhibit try_window_reusing display optimization. */);
28398 inhibit_try_window_reusing = 0;
28399
28400 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28401 doc: /* Inhibit try_cursor_movement display optimization. */);
28402 inhibit_try_cursor_movement = 0;
28403 #endif /* GLYPH_DEBUG */
28404
28405 DEFVAR_INT ("overline-margin", overline_margin,
28406 doc: /* *Space between overline and text, in pixels.
28407 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28408 margin to the caracter height. */);
28409 overline_margin = 2;
28410
28411 DEFVAR_INT ("underline-minimum-offset",
28412 underline_minimum_offset,
28413 doc: /* Minimum distance between baseline and underline.
28414 This can improve legibility of underlined text at small font sizes,
28415 particularly when using variable `x-use-underline-position-properties'
28416 with fonts that specify an UNDERLINE_POSITION relatively close to the
28417 baseline. The default value is 1. */);
28418 underline_minimum_offset = 1;
28419
28420 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28421 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28422 This feature only works when on a window system that can change
28423 cursor shapes. */);
28424 display_hourglass_p = 1;
28425
28426 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28427 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28428 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28429
28430 hourglass_atimer = NULL;
28431 hourglass_shown_p = 0;
28432
28433 DEFSYM (Qglyphless_char, "glyphless-char");
28434 DEFSYM (Qhex_code, "hex-code");
28435 DEFSYM (Qempty_box, "empty-box");
28436 DEFSYM (Qthin_space, "thin-space");
28437 DEFSYM (Qzero_width, "zero-width");
28438
28439 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28440 /* Intern this now in case it isn't already done.
28441 Setting this variable twice is harmless.
28442 But don't staticpro it here--that is done in alloc.c. */
28443 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28444 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28445
28446 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28447 doc: /* Char-table defining glyphless characters.
28448 Each element, if non-nil, should be one of the following:
28449 an ASCII acronym string: display this string in a box
28450 `hex-code': display the hexadecimal code of a character in a box
28451 `empty-box': display as an empty box
28452 `thin-space': display as 1-pixel width space
28453 `zero-width': don't display
28454 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28455 display method for graphical terminals and text terminals respectively.
28456 GRAPHICAL and TEXT should each have one of the values listed above.
28457
28458 The char-table has one extra slot to control the display of a character for
28459 which no font is found. This slot only takes effect on graphical terminals.
28460 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28461 `thin-space'. The default is `empty-box'. */);
28462 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28463 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28464 Qempty_box);
28465 }
28466
28467
28468 /* Initialize this module when Emacs starts. */
28469
28470 void
28471 init_xdisp (void)
28472 {
28473 current_header_line_height = current_mode_line_height = -1;
28474
28475 CHARPOS (this_line_start_pos) = 0;
28476
28477 if (!noninteractive)
28478 {
28479 struct window *m = XWINDOW (minibuf_window);
28480 Lisp_Object frame = m->frame;
28481 struct frame *f = XFRAME (frame);
28482 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28483 struct window *r = XWINDOW (root);
28484 int i;
28485
28486 echo_area_window = minibuf_window;
28487
28488 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28489 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28490 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28491 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28492 XSETFASTINT (m->total_lines, 1);
28493 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28494
28495 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28496 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28497 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28498
28499 /* The default ellipsis glyphs `...'. */
28500 for (i = 0; i < 3; ++i)
28501 default_invis_vector[i] = make_number ('.');
28502 }
28503
28504 {
28505 /* Allocate the buffer for frame titles.
28506 Also used for `format-mode-line'. */
28507 int size = 100;
28508 mode_line_noprop_buf = (char *) xmalloc (size);
28509 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28510 mode_line_noprop_ptr = mode_line_noprop_buf;
28511 mode_line_target = MODE_LINE_DISPLAY;
28512 }
28513
28514 help_echo_showing_p = 0;
28515 }
28516
28517 /* Since w32 does not support atimers, it defines its own implementation of
28518 the following three functions in w32fns.c. */
28519 #ifndef WINDOWSNT
28520
28521 /* Platform-independent portion of hourglass implementation. */
28522
28523 /* Return non-zero if houglass timer has been started or hourglass is shown. */
28524 int
28525 hourglass_started (void)
28526 {
28527 return hourglass_shown_p || hourglass_atimer != NULL;
28528 }
28529
28530 /* Cancel a currently active hourglass timer, and start a new one. */
28531 void
28532 start_hourglass (void)
28533 {
28534 #if defined (HAVE_WINDOW_SYSTEM)
28535 EMACS_TIME delay;
28536 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
28537
28538 cancel_hourglass ();
28539
28540 if (NUMBERP (Vhourglass_delay))
28541 {
28542 double duration = extract_float (Vhourglass_delay);
28543 if (0 < duration)
28544 duration_to_sec_usec (duration, &secs, &usecs);
28545 }
28546
28547 EMACS_SET_SECS_USECS (delay, secs, usecs);
28548 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28549 show_hourglass, NULL);
28550 #endif
28551 }
28552
28553
28554 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28555 shown. */
28556 void
28557 cancel_hourglass (void)
28558 {
28559 #if defined (HAVE_WINDOW_SYSTEM)
28560 if (hourglass_atimer)
28561 {
28562 cancel_atimer (hourglass_atimer);
28563 hourglass_atimer = NULL;
28564 }
28565
28566 if (hourglass_shown_p)
28567 hide_hourglass ();
28568 #endif
28569 }
28570 #endif /* ! WINDOWSNT */