Merge from trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
611
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
619
620 #if GLYPH_DEBUG
621
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
624
625 int trace_redisplay_p;
626
627 #endif /* GLYPH_DEBUG */
628
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
632
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
637
638 static Lisp_Object Qauto_hscroll_mode;
639
640 /* Buffer being redisplayed -- for redisplay_window_error. */
641
642 static struct buffer *displayed_buffer;
643
644 /* Value returned from text property handlers (see below). */
645
646 enum prop_handled
647 {
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
652 };
653
654 /* A description of text properties that redisplay is interested
655 in. */
656
657 struct props
658 {
659 /* The name of the property. */
660 Lisp_Object *name;
661
662 /* A unique index for the property. */
663 enum prop_idx idx;
664
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
668 };
669
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
676
677 /* Properties handled by iterators. */
678
679 static struct props it_props[] =
680 {
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
689 };
690
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
693
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
695
696 /* Enumeration returned by some move_it_.* functions internally. */
697
698 enum move_it_result
699 {
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
702
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
705
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
708
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
712
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
716
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
719 };
720
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
725
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
728
729 /* Similarly for the image cache. */
730
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
734
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
738
739 /* Non-zero while redisplay_internal is in progress. */
740
741 int redisplaying_p;
742
743 static Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 ptrdiff_t help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757 /* Platform-independent portion of hourglass implementation. */
758
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
761
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
765
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
768
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
771
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
781
782 \f
783 /* Function prototypes. */
784
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
793
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
795
796 static void handle_line_prefix (struct it *);
797
798 static void pint2str (char *, int, ptrdiff_t);
799 static void pint2hrstr (char *, int, ptrdiff_t);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 ptrdiff_t, ptrdiff_t);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, ptrdiff_t);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
815 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
818 static void pop_message (void);
819 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
820 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
821 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
824 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (ptrdiff_t);
840 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
866 ptrdiff_t *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, ptrdiff_t);
872 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, ptrdiff_t);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, ptrdiff_t);
905 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
906 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
907 static ptrdiff_t number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static ptrdiff_t next_overlay_change (ptrdiff_t);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, ptrdiff_t, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, ptrdiff_t, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
921
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
924
925 #ifdef HAVE_WINDOW_SYSTEM
926
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
944
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213 /* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215 static Lisp_Object
1216 string_from_display_spec (Lisp_Object spec)
1217 {
1218 if (CONSP (spec))
1219 {
1220 while (CONSP (spec))
1221 {
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1225 }
1226 }
1227 else if (VECTORP (spec))
1228 {
1229 ptrdiff_t i;
1230
1231 for (i = 0; i < ASIZE (spec); i++)
1232 {
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1235 }
1236 return Qnil;
1237 }
1238
1239 return spec;
1240 }
1241
1242 /* Return 1 if position CHARPOS is visible in window W.
1243 CHARPOS < 0 means return info about WINDOW_END position.
1244 If visible, set *X and *Y to pixel coordinates of top left corner.
1245 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1246 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1247
1248 int
1249 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1250 int *rtop, int *rbot, int *rowh, int *vpos)
1251 {
1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1254 struct text_pos top;
1255 int visible_p = 0;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1259 return visible_p;
1260
1261 if (XBUFFER (w->buffer) != current_buffer)
1262 {
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1265 }
1266
1267 SET_TEXT_POS_FROM_MARKER (top, w->start);
1268
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1279
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1283
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1292 {
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 enum it_method it_method = it.method;
1302 /* Calling line_bottom_y may change it.method, it.position, etc. */
1303 int bottom_y = (last_height = 0, line_bottom_y (&it));
1304 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1305
1306 if (top_y < window_top_y)
1307 visible_p = bottom_y > window_top_y;
1308 else if (top_y < it.last_visible_y)
1309 visible_p = 1;
1310 if (visible_p)
1311 {
1312 if (it_method == GET_FROM_DISPLAY_VECTOR)
1313 {
1314 /* We stopped on the last glyph of a display vector.
1315 Try and recompute. Hack alert! */
1316 if (charpos < 2 || top.charpos >= charpos)
1317 top_x = it.glyph_row->x;
1318 else
1319 {
1320 struct it it2;
1321 start_display (&it2, w, top);
1322 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1323 get_next_display_element (&it2);
1324 PRODUCE_GLYPHS (&it2);
1325 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1326 || it2.current_x > it2.last_visible_x)
1327 top_x = it.glyph_row->x;
1328 else
1329 {
1330 top_x = it2.current_x;
1331 top_y = it2.current_y;
1332 }
1333 }
1334 }
1335 else if (IT_CHARPOS (it) != charpos)
1336 {
1337 Lisp_Object cpos = make_number (charpos);
1338 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1339 Lisp_Object string = string_from_display_spec (spec);
1340 int newline_in_string = 0;
1341
1342 if (STRINGP (string))
1343 {
1344 const char *s = SSDATA (string);
1345 const char *e = s + SBYTES (string);
1346 while (s < e)
1347 {
1348 if (*s++ == '\n')
1349 {
1350 newline_in_string = 1;
1351 break;
1352 }
1353 }
1354 }
1355 /* The tricky code below is needed because there's a
1356 discrepancy between move_it_to and how we set cursor
1357 when the display line ends in a newline from a
1358 display string. move_it_to will stop _after_ such
1359 display strings, whereas set_cursor_from_row
1360 conspires with cursor_row_p to place the cursor on
1361 the first glyph produced from the display string. */
1362
1363 /* We have overshoot PT because it is covered by a
1364 display property whose value is a string. If the
1365 string includes embedded newlines, we are also in the
1366 wrong display line. Backtrack to the correct line,
1367 where the display string begins. */
1368 if (newline_in_string)
1369 {
1370 Lisp_Object startpos, endpos;
1371 EMACS_INT start, end;
1372 struct it it3;
1373
1374 /* Find the first and the last buffer positions
1375 covered by the display string. */
1376 endpos =
1377 Fnext_single_char_property_change (cpos, Qdisplay,
1378 Qnil, Qnil);
1379 startpos =
1380 Fprevious_single_char_property_change (endpos, Qdisplay,
1381 Qnil, Qnil);
1382 start = XFASTINT (startpos);
1383 end = XFASTINT (endpos);
1384 /* Move to the last buffer position before the
1385 display property. */
1386 start_display (&it3, w, top);
1387 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1388 /* Move forward one more line if the position before
1389 the display string is a newline or if it is the
1390 rightmost character on a line that is
1391 continued or word-wrapped. */
1392 if (it3.method == GET_FROM_BUFFER
1393 && it3.c == '\n')
1394 move_it_by_lines (&it3, 1);
1395 else if (move_it_in_display_line_to (&it3, -1,
1396 it3.current_x
1397 + it3.pixel_width,
1398 MOVE_TO_X)
1399 == MOVE_LINE_CONTINUED)
1400 {
1401 move_it_by_lines (&it3, 1);
1402 /* When we are under word-wrap, the #$@%!
1403 move_it_by_lines moves 2 lines, so we need to
1404 fix that up. */
1405 if (it3.line_wrap == WORD_WRAP)
1406 move_it_by_lines (&it3, -1);
1407 }
1408
1409 /* Record the vertical coordinate of the display
1410 line where we wound up. */
1411 top_y = it3.current_y;
1412 if (it3.bidi_p)
1413 {
1414 /* When characters are reordered for display,
1415 the character displayed to the left of the
1416 display string could be _after_ the display
1417 property in the logical order. Use the
1418 smallest vertical position of these two. */
1419 start_display (&it3, w, top);
1420 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1421 if (it3.current_y < top_y)
1422 top_y = it3.current_y;
1423 }
1424 /* Move from the top of the window to the beginning
1425 of the display line where the display string
1426 begins. */
1427 start_display (&it3, w, top);
1428 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1429 /* Finally, advance the iterator until we hit the
1430 first display element whose character position is
1431 CHARPOS, or until the first newline from the
1432 display string, which signals the end of the
1433 display line. */
1434 while (get_next_display_element (&it3))
1435 {
1436 PRODUCE_GLYPHS (&it3);
1437 if (IT_CHARPOS (it3) == charpos
1438 || ITERATOR_AT_END_OF_LINE_P (&it3))
1439 break;
1440 set_iterator_to_next (&it3, 0);
1441 }
1442 top_x = it3.current_x - it3.pixel_width;
1443 /* Normally, we would exit the above loop because we
1444 found the display element whose character
1445 position is CHARPOS. For the contingency that we
1446 didn't, and stopped at the first newline from the
1447 display string, move back over the glyphs
1448 produced from the string, until we find the
1449 rightmost glyph not from the string. */
1450 if (IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1451 {
1452 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1453 + it3.glyph_row->used[TEXT_AREA];
1454
1455 while (EQ ((g - 1)->object, string))
1456 {
1457 --g;
1458 top_x -= g->pixel_width;
1459 }
1460 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1461 + it3.glyph_row->used[TEXT_AREA]);
1462 }
1463 }
1464 }
1465
1466 *x = top_x;
1467 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1468 *rtop = max (0, window_top_y - top_y);
1469 *rbot = max (0, bottom_y - it.last_visible_y);
1470 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1471 - max (top_y, window_top_y)));
1472 *vpos = it.vpos;
1473 }
1474 }
1475 else
1476 {
1477 /* We were asked to provide info about WINDOW_END. */
1478 struct it it2;
1479 void *it2data = NULL;
1480
1481 SAVE_IT (it2, it, it2data);
1482 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1483 move_it_by_lines (&it, 1);
1484 if (charpos < IT_CHARPOS (it)
1485 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1486 {
1487 visible_p = 1;
1488 RESTORE_IT (&it2, &it2, it2data);
1489 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1490 *x = it2.current_x;
1491 *y = it2.current_y + it2.max_ascent - it2.ascent;
1492 *rtop = max (0, -it2.current_y);
1493 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1494 - it.last_visible_y));
1495 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1496 it.last_visible_y)
1497 - max (it2.current_y,
1498 WINDOW_HEADER_LINE_HEIGHT (w))));
1499 *vpos = it2.vpos;
1500 }
1501 else
1502 bidi_unshelve_cache (it2data, 1);
1503 }
1504 bidi_unshelve_cache (itdata, 0);
1505
1506 if (old_buffer)
1507 set_buffer_internal_1 (old_buffer);
1508
1509 current_header_line_height = current_mode_line_height = -1;
1510
1511 if (visible_p && XFASTINT (w->hscroll) > 0)
1512 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1513
1514 #if 0
1515 /* Debugging code. */
1516 if (visible_p)
1517 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1518 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1519 else
1520 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1521 #endif
1522
1523 return visible_p;
1524 }
1525
1526
1527 /* Return the next character from STR. Return in *LEN the length of
1528 the character. This is like STRING_CHAR_AND_LENGTH but never
1529 returns an invalid character. If we find one, we return a `?', but
1530 with the length of the invalid character. */
1531
1532 static inline int
1533 string_char_and_length (const unsigned char *str, int *len)
1534 {
1535 int c;
1536
1537 c = STRING_CHAR_AND_LENGTH (str, *len);
1538 if (!CHAR_VALID_P (c))
1539 /* We may not change the length here because other places in Emacs
1540 don't use this function, i.e. they silently accept invalid
1541 characters. */
1542 c = '?';
1543
1544 return c;
1545 }
1546
1547
1548
1549 /* Given a position POS containing a valid character and byte position
1550 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1551
1552 static struct text_pos
1553 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1554 {
1555 xassert (STRINGP (string) && nchars >= 0);
1556
1557 if (STRING_MULTIBYTE (string))
1558 {
1559 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1560 int len;
1561
1562 while (nchars--)
1563 {
1564 string_char_and_length (p, &len);
1565 p += len;
1566 CHARPOS (pos) += 1;
1567 BYTEPOS (pos) += len;
1568 }
1569 }
1570 else
1571 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1572
1573 return pos;
1574 }
1575
1576
1577 /* Value is the text position, i.e. character and byte position,
1578 for character position CHARPOS in STRING. */
1579
1580 static inline struct text_pos
1581 string_pos (ptrdiff_t charpos, Lisp_Object string)
1582 {
1583 struct text_pos pos;
1584 xassert (STRINGP (string));
1585 xassert (charpos >= 0);
1586 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1587 return pos;
1588 }
1589
1590
1591 /* Value is a text position, i.e. character and byte position, for
1592 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1593 means recognize multibyte characters. */
1594
1595 static struct text_pos
1596 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1597 {
1598 struct text_pos pos;
1599
1600 xassert (s != NULL);
1601 xassert (charpos >= 0);
1602
1603 if (multibyte_p)
1604 {
1605 int len;
1606
1607 SET_TEXT_POS (pos, 0, 0);
1608 while (charpos--)
1609 {
1610 string_char_and_length ((const unsigned char *) s, &len);
1611 s += len;
1612 CHARPOS (pos) += 1;
1613 BYTEPOS (pos) += len;
1614 }
1615 }
1616 else
1617 SET_TEXT_POS (pos, charpos, charpos);
1618
1619 return pos;
1620 }
1621
1622
1623 /* Value is the number of characters in C string S. MULTIBYTE_P
1624 non-zero means recognize multibyte characters. */
1625
1626 static ptrdiff_t
1627 number_of_chars (const char *s, int multibyte_p)
1628 {
1629 ptrdiff_t nchars;
1630
1631 if (multibyte_p)
1632 {
1633 ptrdiff_t rest = strlen (s);
1634 int len;
1635 const unsigned char *p = (const unsigned char *) s;
1636
1637 for (nchars = 0; rest > 0; ++nchars)
1638 {
1639 string_char_and_length (p, &len);
1640 rest -= len, p += len;
1641 }
1642 }
1643 else
1644 nchars = strlen (s);
1645
1646 return nchars;
1647 }
1648
1649
1650 /* Compute byte position NEWPOS->bytepos corresponding to
1651 NEWPOS->charpos. POS is a known position in string STRING.
1652 NEWPOS->charpos must be >= POS.charpos. */
1653
1654 static void
1655 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1656 {
1657 xassert (STRINGP (string));
1658 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1659
1660 if (STRING_MULTIBYTE (string))
1661 *newpos = string_pos_nchars_ahead (pos, string,
1662 CHARPOS (*newpos) - CHARPOS (pos));
1663 else
1664 BYTEPOS (*newpos) = CHARPOS (*newpos);
1665 }
1666
1667 /* EXPORT:
1668 Return an estimation of the pixel height of mode or header lines on
1669 frame F. FACE_ID specifies what line's height to estimate. */
1670
1671 int
1672 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1673 {
1674 #ifdef HAVE_WINDOW_SYSTEM
1675 if (FRAME_WINDOW_P (f))
1676 {
1677 int height = FONT_HEIGHT (FRAME_FONT (f));
1678
1679 /* This function is called so early when Emacs starts that the face
1680 cache and mode line face are not yet initialized. */
1681 if (FRAME_FACE_CACHE (f))
1682 {
1683 struct face *face = FACE_FROM_ID (f, face_id);
1684 if (face)
1685 {
1686 if (face->font)
1687 height = FONT_HEIGHT (face->font);
1688 if (face->box_line_width > 0)
1689 height += 2 * face->box_line_width;
1690 }
1691 }
1692
1693 return height;
1694 }
1695 #endif
1696
1697 return 1;
1698 }
1699
1700 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1701 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1702 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1703 not force the value into range. */
1704
1705 void
1706 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1707 int *x, int *y, NativeRectangle *bounds, int noclip)
1708 {
1709
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (f))
1712 {
1713 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1714 even for negative values. */
1715 if (pix_x < 0)
1716 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1717 if (pix_y < 0)
1718 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1719
1720 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1721 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1722
1723 if (bounds)
1724 STORE_NATIVE_RECT (*bounds,
1725 FRAME_COL_TO_PIXEL_X (f, pix_x),
1726 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1727 FRAME_COLUMN_WIDTH (f) - 1,
1728 FRAME_LINE_HEIGHT (f) - 1);
1729
1730 if (!noclip)
1731 {
1732 if (pix_x < 0)
1733 pix_x = 0;
1734 else if (pix_x > FRAME_TOTAL_COLS (f))
1735 pix_x = FRAME_TOTAL_COLS (f);
1736
1737 if (pix_y < 0)
1738 pix_y = 0;
1739 else if (pix_y > FRAME_LINES (f))
1740 pix_y = FRAME_LINES (f);
1741 }
1742 }
1743 #endif
1744
1745 *x = pix_x;
1746 *y = pix_y;
1747 }
1748
1749
1750 /* Find the glyph under window-relative coordinates X/Y in window W.
1751 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1752 strings. Return in *HPOS and *VPOS the row and column number of
1753 the glyph found. Return in *AREA the glyph area containing X.
1754 Value is a pointer to the glyph found or null if X/Y is not on
1755 text, or we can't tell because W's current matrix is not up to
1756 date. */
1757
1758 static
1759 struct glyph *
1760 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1761 int *dx, int *dy, int *area)
1762 {
1763 struct glyph *glyph, *end;
1764 struct glyph_row *row = NULL;
1765 int x0, i;
1766
1767 /* Find row containing Y. Give up if some row is not enabled. */
1768 for (i = 0; i < w->current_matrix->nrows; ++i)
1769 {
1770 row = MATRIX_ROW (w->current_matrix, i);
1771 if (!row->enabled_p)
1772 return NULL;
1773 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1774 break;
1775 }
1776
1777 *vpos = i;
1778 *hpos = 0;
1779
1780 /* Give up if Y is not in the window. */
1781 if (i == w->current_matrix->nrows)
1782 return NULL;
1783
1784 /* Get the glyph area containing X. */
1785 if (w->pseudo_window_p)
1786 {
1787 *area = TEXT_AREA;
1788 x0 = 0;
1789 }
1790 else
1791 {
1792 if (x < window_box_left_offset (w, TEXT_AREA))
1793 {
1794 *area = LEFT_MARGIN_AREA;
1795 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1796 }
1797 else if (x < window_box_right_offset (w, TEXT_AREA))
1798 {
1799 *area = TEXT_AREA;
1800 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1801 }
1802 else
1803 {
1804 *area = RIGHT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1806 }
1807 }
1808
1809 /* Find glyph containing X. */
1810 glyph = row->glyphs[*area];
1811 end = glyph + row->used[*area];
1812 x -= x0;
1813 while (glyph < end && x >= glyph->pixel_width)
1814 {
1815 x -= glyph->pixel_width;
1816 ++glyph;
1817 }
1818
1819 if (glyph == end)
1820 return NULL;
1821
1822 if (dx)
1823 {
1824 *dx = x;
1825 *dy = y - (row->y + row->ascent - glyph->ascent);
1826 }
1827
1828 *hpos = glyph - row->glyphs[*area];
1829 return glyph;
1830 }
1831
1832 /* Convert frame-relative x/y to coordinates relative to window W.
1833 Takes pseudo-windows into account. */
1834
1835 static void
1836 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1837 {
1838 if (w->pseudo_window_p)
1839 {
1840 /* A pseudo-window is always full-width, and starts at the
1841 left edge of the frame, plus a frame border. */
1842 struct frame *f = XFRAME (w->frame);
1843 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1844 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1845 }
1846 else
1847 {
1848 *x -= WINDOW_LEFT_EDGE_X (w);
1849 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1850 }
1851 }
1852
1853 #ifdef HAVE_WINDOW_SYSTEM
1854
1855 /* EXPORT:
1856 Return in RECTS[] at most N clipping rectangles for glyph string S.
1857 Return the number of stored rectangles. */
1858
1859 int
1860 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1861 {
1862 XRectangle r;
1863
1864 if (n <= 0)
1865 return 0;
1866
1867 if (s->row->full_width_p)
1868 {
1869 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1870 r.x = WINDOW_LEFT_EDGE_X (s->w);
1871 r.width = WINDOW_TOTAL_WIDTH (s->w);
1872
1873 /* Unless displaying a mode or menu bar line, which are always
1874 fully visible, clip to the visible part of the row. */
1875 if (s->w->pseudo_window_p)
1876 r.height = s->row->visible_height;
1877 else
1878 r.height = s->height;
1879 }
1880 else
1881 {
1882 /* This is a text line that may be partially visible. */
1883 r.x = window_box_left (s->w, s->area);
1884 r.width = window_box_width (s->w, s->area);
1885 r.height = s->row->visible_height;
1886 }
1887
1888 if (s->clip_head)
1889 if (r.x < s->clip_head->x)
1890 {
1891 if (r.width >= s->clip_head->x - r.x)
1892 r.width -= s->clip_head->x - r.x;
1893 else
1894 r.width = 0;
1895 r.x = s->clip_head->x;
1896 }
1897 if (s->clip_tail)
1898 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1899 {
1900 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1901 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1902 else
1903 r.width = 0;
1904 }
1905
1906 /* If S draws overlapping rows, it's sufficient to use the top and
1907 bottom of the window for clipping because this glyph string
1908 intentionally draws over other lines. */
1909 if (s->for_overlaps)
1910 {
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 r.height = window_text_bottom_y (s->w) - r.y;
1913
1914 /* Alas, the above simple strategy does not work for the
1915 environments with anti-aliased text: if the same text is
1916 drawn onto the same place multiple times, it gets thicker.
1917 If the overlap we are processing is for the erased cursor, we
1918 take the intersection with the rectangle of the cursor. */
1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1920 {
1921 XRectangle rc, r_save = r;
1922
1923 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1924 rc.y = s->w->phys_cursor.y;
1925 rc.width = s->w->phys_cursor_width;
1926 rc.height = s->w->phys_cursor_height;
1927
1928 x_intersect_rectangles (&r_save, &rc, &r);
1929 }
1930 }
1931 else
1932 {
1933 /* Don't use S->y for clipping because it doesn't take partially
1934 visible lines into account. For example, it can be negative for
1935 partially visible lines at the top of a window. */
1936 if (!s->row->full_width_p
1937 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 else
1940 r.y = max (0, s->row->y);
1941 }
1942
1943 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1944
1945 /* If drawing the cursor, don't let glyph draw outside its
1946 advertised boundaries. Cleartype does this under some circumstances. */
1947 if (s->hl == DRAW_CURSOR)
1948 {
1949 struct glyph *glyph = s->first_glyph;
1950 int height, max_y;
1951
1952 if (s->x > r.x)
1953 {
1954 r.width -= s->x - r.x;
1955 r.x = s->x;
1956 }
1957 r.width = min (r.width, glyph->pixel_width);
1958
1959 /* If r.y is below window bottom, ensure that we still see a cursor. */
1960 height = min (glyph->ascent + glyph->descent,
1961 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1962 max_y = window_text_bottom_y (s->w) - height;
1963 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1964 if (s->ybase - glyph->ascent > max_y)
1965 {
1966 r.y = max_y;
1967 r.height = height;
1968 }
1969 else
1970 {
1971 /* Don't draw cursor glyph taller than our actual glyph. */
1972 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1973 if (height < r.height)
1974 {
1975 max_y = r.y + r.height;
1976 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1977 r.height = min (max_y - r.y, height);
1978 }
1979 }
1980 }
1981
1982 if (s->row->clip)
1983 {
1984 XRectangle r_save = r;
1985
1986 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1987 r.width = 0;
1988 }
1989
1990 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1991 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1992 {
1993 #ifdef CONVERT_FROM_XRECT
1994 CONVERT_FROM_XRECT (r, *rects);
1995 #else
1996 *rects = r;
1997 #endif
1998 return 1;
1999 }
2000 else
2001 {
2002 /* If we are processing overlapping and allowed to return
2003 multiple clipping rectangles, we exclude the row of the glyph
2004 string from the clipping rectangle. This is to avoid drawing
2005 the same text on the environment with anti-aliasing. */
2006 #ifdef CONVERT_FROM_XRECT
2007 XRectangle rs[2];
2008 #else
2009 XRectangle *rs = rects;
2010 #endif
2011 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2012
2013 if (s->for_overlaps & OVERLAPS_PRED)
2014 {
2015 rs[i] = r;
2016 if (r.y + r.height > row_y)
2017 {
2018 if (r.y < row_y)
2019 rs[i].height = row_y - r.y;
2020 else
2021 rs[i].height = 0;
2022 }
2023 i++;
2024 }
2025 if (s->for_overlaps & OVERLAPS_SUCC)
2026 {
2027 rs[i] = r;
2028 if (r.y < row_y + s->row->visible_height)
2029 {
2030 if (r.y + r.height > row_y + s->row->visible_height)
2031 {
2032 rs[i].y = row_y + s->row->visible_height;
2033 rs[i].height = r.y + r.height - rs[i].y;
2034 }
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040
2041 n = i;
2042 #ifdef CONVERT_FROM_XRECT
2043 for (i = 0; i < n; i++)
2044 CONVERT_FROM_XRECT (rs[i], rects[i]);
2045 #endif
2046 return n;
2047 }
2048 }
2049
2050 /* EXPORT:
2051 Return in *NR the clipping rectangle for glyph string S. */
2052
2053 void
2054 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2055 {
2056 get_glyph_string_clip_rects (s, nr, 1);
2057 }
2058
2059
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2063 */
2064
2065 void
2066 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2067 struct glyph *glyph, int *xp, int *yp, int *heightp)
2068 {
2069 struct frame *f = XFRAME (WINDOW_FRAME (w));
2070 int x, y, wd, h, h0, y0;
2071
2072 /* Compute the width of the rectangle to draw. If on a stretch
2073 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2074 rectangle as wide as the glyph, but use a canonical character
2075 width instead. */
2076 wd = glyph->pixel_width - 1;
2077 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2078 wd++; /* Why? */
2079 #endif
2080
2081 x = w->phys_cursor.x;
2082 if (x < 0)
2083 {
2084 wd += x;
2085 x = 0;
2086 }
2087
2088 if (glyph->type == STRETCH_GLYPH
2089 && !x_stretch_cursor_p)
2090 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2091 w->phys_cursor_width = wd;
2092
2093 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2094
2095 /* If y is below window bottom, ensure that we still see a cursor. */
2096 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2097
2098 h = max (h0, glyph->ascent + glyph->descent);
2099 h0 = min (h0, glyph->ascent + glyph->descent);
2100
2101 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2102 if (y < y0)
2103 {
2104 h = max (h - (y0 - y) + 1, h0);
2105 y = y0 - 1;
2106 }
2107 else
2108 {
2109 y0 = window_text_bottom_y (w) - h0;
2110 if (y > y0)
2111 {
2112 h += y - y0;
2113 y = y0;
2114 }
2115 }
2116
2117 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2118 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2119 *heightp = h;
2120 }
2121
2122 /*
2123 * Remember which glyph the mouse is over.
2124 */
2125
2126 void
2127 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2128 {
2129 Lisp_Object window;
2130 struct window *w;
2131 struct glyph_row *r, *gr, *end_row;
2132 enum window_part part;
2133 enum glyph_row_area area;
2134 int x, y, width, height;
2135
2136 /* Try to determine frame pixel position and size of the glyph under
2137 frame pixel coordinates X/Y on frame F. */
2138
2139 if (!f->glyphs_initialized_p
2140 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2141 NILP (window)))
2142 {
2143 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2144 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2145 goto virtual_glyph;
2146 }
2147
2148 w = XWINDOW (window);
2149 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2150 height = WINDOW_FRAME_LINE_HEIGHT (w);
2151
2152 x = window_relative_x_coord (w, part, gx);
2153 y = gy - WINDOW_TOP_EDGE_Y (w);
2154
2155 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2156 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2157
2158 if (w->pseudo_window_p)
2159 {
2160 area = TEXT_AREA;
2161 part = ON_MODE_LINE; /* Don't adjust margin. */
2162 goto text_glyph;
2163 }
2164
2165 switch (part)
2166 {
2167 case ON_LEFT_MARGIN:
2168 area = LEFT_MARGIN_AREA;
2169 goto text_glyph;
2170
2171 case ON_RIGHT_MARGIN:
2172 area = RIGHT_MARGIN_AREA;
2173 goto text_glyph;
2174
2175 case ON_HEADER_LINE:
2176 case ON_MODE_LINE:
2177 gr = (part == ON_HEADER_LINE
2178 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2179 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2180 gy = gr->y;
2181 area = TEXT_AREA;
2182 goto text_glyph_row_found;
2183
2184 case ON_TEXT:
2185 area = TEXT_AREA;
2186
2187 text_glyph:
2188 gr = 0; gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2191 {
2192 gr = r; gy = r->y;
2193 break;
2194 }
2195
2196 text_glyph_row_found:
2197 if (gr && gy <= y)
2198 {
2199 struct glyph *g = gr->glyphs[area];
2200 struct glyph *end = g + gr->used[area];
2201
2202 height = gr->height;
2203 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2204 if (gx + g->pixel_width > x)
2205 break;
2206
2207 if (g < end)
2208 {
2209 if (g->type == IMAGE_GLYPH)
2210 {
2211 /* Don't remember when mouse is over image, as
2212 image may have hot-spots. */
2213 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2214 return;
2215 }
2216 width = g->pixel_width;
2217 }
2218 else
2219 {
2220 /* Use nominal char spacing at end of line. */
2221 x -= gx;
2222 gx += (x / width) * width;
2223 }
2224
2225 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2226 gx += window_box_left_offset (w, area);
2227 }
2228 else
2229 {
2230 /* Use nominal line height at end of window. */
2231 gx = (x / width) * width;
2232 y -= gy;
2233 gy += (y / height) * height;
2234 }
2235 break;
2236
2237 case ON_LEFT_FRINGE:
2238 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2239 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2240 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2241 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2242 goto row_glyph;
2243
2244 case ON_RIGHT_FRINGE:
2245 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2246 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2247 : window_box_right_offset (w, TEXT_AREA));
2248 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2249 goto row_glyph;
2250
2251 case ON_SCROLL_BAR:
2252 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2253 ? 0
2254 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2255 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2256 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2257 : 0)));
2258 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2259
2260 row_glyph:
2261 gr = 0, gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2264 {
2265 gr = r; gy = r->y;
2266 break;
2267 }
2268
2269 if (gr && gy <= y)
2270 height = gr->height;
2271 else
2272 {
2273 /* Use nominal line height at end of window. */
2274 y -= gy;
2275 gy += (y / height) * height;
2276 }
2277 break;
2278
2279 default:
2280 ;
2281 virtual_glyph:
2282 /* If there is no glyph under the mouse, then we divide the screen
2283 into a grid of the smallest glyph in the frame, and use that
2284 as our "glyph". */
2285
2286 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2287 round down even for negative values. */
2288 if (gx < 0)
2289 gx -= width - 1;
2290 if (gy < 0)
2291 gy -= height - 1;
2292
2293 gx = (gx / width) * width;
2294 gy = (gy / height) * height;
2295
2296 goto store_rect;
2297 }
2298
2299 gx += WINDOW_LEFT_EDGE_X (w);
2300 gy += WINDOW_TOP_EDGE_Y (w);
2301
2302 store_rect:
2303 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2304
2305 /* Visible feedback for debugging. */
2306 #if 0
2307 #if HAVE_X_WINDOWS
2308 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2309 f->output_data.x->normal_gc,
2310 gx, gy, width, height);
2311 #endif
2312 #endif
2313 }
2314
2315
2316 #endif /* HAVE_WINDOW_SYSTEM */
2317
2318 \f
2319 /***********************************************************************
2320 Lisp form evaluation
2321 ***********************************************************************/
2322
2323 /* Error handler for safe_eval and safe_call. */
2324
2325 static Lisp_Object
2326 safe_eval_handler (Lisp_Object arg)
2327 {
2328 add_to_log ("Error during redisplay: %S", arg, Qnil);
2329 return Qnil;
2330 }
2331
2332
2333 /* Evaluate SEXPR and return the result, or nil if something went
2334 wrong. Prevent redisplay during the evaluation. */
2335
2336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2337 Return the result, or nil if something went wrong. Prevent
2338 redisplay during the evaluation. */
2339
2340 Lisp_Object
2341 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2342 {
2343 Lisp_Object val;
2344
2345 if (inhibit_eval_during_redisplay)
2346 val = Qnil;
2347 else
2348 {
2349 ptrdiff_t count = SPECPDL_INDEX ();
2350 struct gcpro gcpro1;
2351
2352 GCPRO1 (args[0]);
2353 gcpro1.nvars = nargs;
2354 specbind (Qinhibit_redisplay, Qt);
2355 /* Use Qt to ensure debugger does not run,
2356 so there is no possibility of wanting to redisplay. */
2357 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2358 safe_eval_handler);
2359 UNGCPRO;
2360 val = unbind_to (count, val);
2361 }
2362
2363 return val;
2364 }
2365
2366
2367 /* Call function FN with one argument ARG.
2368 Return the result, or nil if something went wrong. */
2369
2370 Lisp_Object
2371 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2372 {
2373 Lisp_Object args[2];
2374 args[0] = fn;
2375 args[1] = arg;
2376 return safe_call (2, args);
2377 }
2378
2379 static Lisp_Object Qeval;
2380
2381 Lisp_Object
2382 safe_eval (Lisp_Object sexpr)
2383 {
2384 return safe_call1 (Qeval, sexpr);
2385 }
2386
2387 /* Call function FN with one argument ARG.
2388 Return the result, or nil if something went wrong. */
2389
2390 Lisp_Object
2391 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2392 {
2393 Lisp_Object args[3];
2394 args[0] = fn;
2395 args[1] = arg1;
2396 args[2] = arg2;
2397 return safe_call (3, args);
2398 }
2399
2400
2401 \f
2402 /***********************************************************************
2403 Debugging
2404 ***********************************************************************/
2405
2406 #if 0
2407
2408 /* Define CHECK_IT to perform sanity checks on iterators.
2409 This is for debugging. It is too slow to do unconditionally. */
2410
2411 static void
2412 check_it (struct it *it)
2413 {
2414 if (it->method == GET_FROM_STRING)
2415 {
2416 xassert (STRINGP (it->string));
2417 xassert (IT_STRING_CHARPOS (*it) >= 0);
2418 }
2419 else
2420 {
2421 xassert (IT_STRING_CHARPOS (*it) < 0);
2422 if (it->method == GET_FROM_BUFFER)
2423 {
2424 /* Check that character and byte positions agree. */
2425 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2426 }
2427 }
2428
2429 if (it->dpvec)
2430 xassert (it->current.dpvec_index >= 0);
2431 else
2432 xassert (it->current.dpvec_index < 0);
2433 }
2434
2435 #define CHECK_IT(IT) check_it ((IT))
2436
2437 #else /* not 0 */
2438
2439 #define CHECK_IT(IT) (void) 0
2440
2441 #endif /* not 0 */
2442
2443
2444 #if GLYPH_DEBUG && XASSERTS
2445
2446 /* Check that the window end of window W is what we expect it
2447 to be---the last row in the current matrix displaying text. */
2448
2449 static void
2450 check_window_end (struct window *w)
2451 {
2452 if (!MINI_WINDOW_P (w)
2453 && !NILP (w->window_end_valid))
2454 {
2455 struct glyph_row *row;
2456 xassert ((row = MATRIX_ROW (w->current_matrix,
2457 XFASTINT (w->window_end_vpos)),
2458 !row->enabled_p
2459 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2460 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2461 }
2462 }
2463
2464 #define CHECK_WINDOW_END(W) check_window_end ((W))
2465
2466 #else
2467
2468 #define CHECK_WINDOW_END(W) (void) 0
2469
2470 #endif
2471
2472
2473 \f
2474 /***********************************************************************
2475 Iterator initialization
2476 ***********************************************************************/
2477
2478 /* Initialize IT for displaying current_buffer in window W, starting
2479 at character position CHARPOS. CHARPOS < 0 means that no buffer
2480 position is specified which is useful when the iterator is assigned
2481 a position later. BYTEPOS is the byte position corresponding to
2482 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2483
2484 If ROW is not null, calls to produce_glyphs with IT as parameter
2485 will produce glyphs in that row.
2486
2487 BASE_FACE_ID is the id of a base face to use. It must be one of
2488 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2489 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2490 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2491
2492 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2493 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2494 will be initialized to use the corresponding mode line glyph row of
2495 the desired matrix of W. */
2496
2497 void
2498 init_iterator (struct it *it, struct window *w,
2499 ptrdiff_t charpos, ptrdiff_t bytepos,
2500 struct glyph_row *row, enum face_id base_face_id)
2501 {
2502 int highlight_region_p;
2503 enum face_id remapped_base_face_id = base_face_id;
2504
2505 /* Some precondition checks. */
2506 xassert (w != NULL && it != NULL);
2507 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2508 && charpos <= ZV));
2509
2510 /* If face attributes have been changed since the last redisplay,
2511 free realized faces now because they depend on face definitions
2512 that might have changed. Don't free faces while there might be
2513 desired matrices pending which reference these faces. */
2514 if (face_change_count && !inhibit_free_realized_faces)
2515 {
2516 face_change_count = 0;
2517 free_all_realized_faces (Qnil);
2518 }
2519
2520 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2521 if (! NILP (Vface_remapping_alist))
2522 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2523
2524 /* Use one of the mode line rows of W's desired matrix if
2525 appropriate. */
2526 if (row == NULL)
2527 {
2528 if (base_face_id == MODE_LINE_FACE_ID
2529 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2530 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2531 else if (base_face_id == HEADER_LINE_FACE_ID)
2532 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2533 }
2534
2535 /* Clear IT. */
2536 memset (it, 0, sizeof *it);
2537 it->current.overlay_string_index = -1;
2538 it->current.dpvec_index = -1;
2539 it->base_face_id = remapped_base_face_id;
2540 it->string = Qnil;
2541 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2542 it->paragraph_embedding = L2R;
2543 it->bidi_it.string.lstring = Qnil;
2544 it->bidi_it.string.s = NULL;
2545 it->bidi_it.string.bufpos = 0;
2546
2547 /* The window in which we iterate over current_buffer: */
2548 XSETWINDOW (it->window, w);
2549 it->w = w;
2550 it->f = XFRAME (w->frame);
2551
2552 it->cmp_it.id = -1;
2553
2554 /* Extra space between lines (on window systems only). */
2555 if (base_face_id == DEFAULT_FACE_ID
2556 && FRAME_WINDOW_P (it->f))
2557 {
2558 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2559 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2560 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2561 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2562 * FRAME_LINE_HEIGHT (it->f));
2563 else if (it->f->extra_line_spacing > 0)
2564 it->extra_line_spacing = it->f->extra_line_spacing;
2565 it->max_extra_line_spacing = 0;
2566 }
2567
2568 /* If realized faces have been removed, e.g. because of face
2569 attribute changes of named faces, recompute them. When running
2570 in batch mode, the face cache of the initial frame is null. If
2571 we happen to get called, make a dummy face cache. */
2572 if (FRAME_FACE_CACHE (it->f) == NULL)
2573 init_frame_faces (it->f);
2574 if (FRAME_FACE_CACHE (it->f)->used == 0)
2575 recompute_basic_faces (it->f);
2576
2577 /* Current value of the `slice', `space-width', and 'height' properties. */
2578 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2579 it->space_width = Qnil;
2580 it->font_height = Qnil;
2581 it->override_ascent = -1;
2582
2583 /* Are control characters displayed as `^C'? */
2584 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2585
2586 /* -1 means everything between a CR and the following line end
2587 is invisible. >0 means lines indented more than this value are
2588 invisible. */
2589 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2590 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2591 selective_display)),
2592 PTRDIFF_MAX)
2593 : (!NILP (BVAR (current_buffer, selective_display))
2594 ? -1 : 0));
2595 it->selective_display_ellipsis_p
2596 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2597
2598 /* Display table to use. */
2599 it->dp = window_display_table (w);
2600
2601 /* Are multibyte characters enabled in current_buffer? */
2602 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2603
2604 /* Non-zero if we should highlight the region. */
2605 highlight_region_p
2606 = (!NILP (Vtransient_mark_mode)
2607 && !NILP (BVAR (current_buffer, mark_active))
2608 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2609
2610 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2611 start and end of a visible region in window IT->w. Set both to
2612 -1 to indicate no region. */
2613 if (highlight_region_p
2614 /* Maybe highlight only in selected window. */
2615 && (/* Either show region everywhere. */
2616 highlight_nonselected_windows
2617 /* Or show region in the selected window. */
2618 || w == XWINDOW (selected_window)
2619 /* Or show the region if we are in the mini-buffer and W is
2620 the window the mini-buffer refers to. */
2621 || (MINI_WINDOW_P (XWINDOW (selected_window))
2622 && WINDOWP (minibuf_selected_window)
2623 && w == XWINDOW (minibuf_selected_window))))
2624 {
2625 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2626 it->region_beg_charpos = min (PT, markpos);
2627 it->region_end_charpos = max (PT, markpos);
2628 }
2629 else
2630 it->region_beg_charpos = it->region_end_charpos = -1;
2631
2632 /* Get the position at which the redisplay_end_trigger hook should
2633 be run, if it is to be run at all. */
2634 if (MARKERP (w->redisplay_end_trigger)
2635 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2636 it->redisplay_end_trigger_charpos
2637 = marker_position (w->redisplay_end_trigger);
2638 else if (INTEGERP (w->redisplay_end_trigger))
2639 it->redisplay_end_trigger_charpos =
2640 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2641
2642 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2643
2644 /* Are lines in the display truncated? */
2645 if (base_face_id != DEFAULT_FACE_ID
2646 || XINT (it->w->hscroll)
2647 || (! WINDOW_FULL_WIDTH_P (it->w)
2648 && ((!NILP (Vtruncate_partial_width_windows)
2649 && !INTEGERP (Vtruncate_partial_width_windows))
2650 || (INTEGERP (Vtruncate_partial_width_windows)
2651 && (WINDOW_TOTAL_COLS (it->w)
2652 < XINT (Vtruncate_partial_width_windows))))))
2653 it->line_wrap = TRUNCATE;
2654 else if (NILP (BVAR (current_buffer, truncate_lines)))
2655 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2656 ? WINDOW_WRAP : WORD_WRAP;
2657 else
2658 it->line_wrap = TRUNCATE;
2659
2660 /* Get dimensions of truncation and continuation glyphs. These are
2661 displayed as fringe bitmaps under X, so we don't need them for such
2662 frames. */
2663 if (!FRAME_WINDOW_P (it->f))
2664 {
2665 if (it->line_wrap == TRUNCATE)
2666 {
2667 /* We will need the truncation glyph. */
2668 xassert (it->glyph_row == NULL);
2669 produce_special_glyphs (it, IT_TRUNCATION);
2670 it->truncation_pixel_width = it->pixel_width;
2671 }
2672 else
2673 {
2674 /* We will need the continuation glyph. */
2675 xassert (it->glyph_row == NULL);
2676 produce_special_glyphs (it, IT_CONTINUATION);
2677 it->continuation_pixel_width = it->pixel_width;
2678 }
2679
2680 /* Reset these values to zero because the produce_special_glyphs
2681 above has changed them. */
2682 it->pixel_width = it->ascent = it->descent = 0;
2683 it->phys_ascent = it->phys_descent = 0;
2684 }
2685
2686 /* Set this after getting the dimensions of truncation and
2687 continuation glyphs, so that we don't produce glyphs when calling
2688 produce_special_glyphs, above. */
2689 it->glyph_row = row;
2690 it->area = TEXT_AREA;
2691
2692 /* Forget any previous info about this row being reversed. */
2693 if (it->glyph_row)
2694 it->glyph_row->reversed_p = 0;
2695
2696 /* Get the dimensions of the display area. The display area
2697 consists of the visible window area plus a horizontally scrolled
2698 part to the left of the window. All x-values are relative to the
2699 start of this total display area. */
2700 if (base_face_id != DEFAULT_FACE_ID)
2701 {
2702 /* Mode lines, menu bar in terminal frames. */
2703 it->first_visible_x = 0;
2704 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2705 }
2706 else
2707 {
2708 it->first_visible_x
2709 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2710 it->last_visible_x = (it->first_visible_x
2711 + window_box_width (w, TEXT_AREA));
2712
2713 /* If we truncate lines, leave room for the truncator glyph(s) at
2714 the right margin. Otherwise, leave room for the continuation
2715 glyph(s). Truncation and continuation glyphs are not inserted
2716 for window-based redisplay. */
2717 if (!FRAME_WINDOW_P (it->f))
2718 {
2719 if (it->line_wrap == TRUNCATE)
2720 it->last_visible_x -= it->truncation_pixel_width;
2721 else
2722 it->last_visible_x -= it->continuation_pixel_width;
2723 }
2724
2725 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2726 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2727 }
2728
2729 /* Leave room for a border glyph. */
2730 if (!FRAME_WINDOW_P (it->f)
2731 && !WINDOW_RIGHTMOST_P (it->w))
2732 it->last_visible_x -= 1;
2733
2734 it->last_visible_y = window_text_bottom_y (w);
2735
2736 /* For mode lines and alike, arrange for the first glyph having a
2737 left box line if the face specifies a box. */
2738 if (base_face_id != DEFAULT_FACE_ID)
2739 {
2740 struct face *face;
2741
2742 it->face_id = remapped_base_face_id;
2743
2744 /* If we have a boxed mode line, make the first character appear
2745 with a left box line. */
2746 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2747 if (face->box != FACE_NO_BOX)
2748 it->start_of_box_run_p = 1;
2749 }
2750
2751 /* If a buffer position was specified, set the iterator there,
2752 getting overlays and face properties from that position. */
2753 if (charpos >= BUF_BEG (current_buffer))
2754 {
2755 it->end_charpos = ZV;
2756 IT_CHARPOS (*it) = charpos;
2757
2758 /* We will rely on `reseat' to set this up properly, via
2759 handle_face_prop. */
2760 it->face_id = it->base_face_id;
2761
2762 /* Compute byte position if not specified. */
2763 if (bytepos < charpos)
2764 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2765 else
2766 IT_BYTEPOS (*it) = bytepos;
2767
2768 it->start = it->current;
2769 /* Do we need to reorder bidirectional text? Not if this is a
2770 unibyte buffer: by definition, none of the single-byte
2771 characters are strong R2L, so no reordering is needed. And
2772 bidi.c doesn't support unibyte buffers anyway. Also, don't
2773 reorder while we are loading loadup.el, since the tables of
2774 character properties needed for reordering are not yet
2775 available. */
2776 it->bidi_p =
2777 NILP (Vpurify_flag)
2778 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2779 && it->multibyte_p;
2780
2781 /* If we are to reorder bidirectional text, init the bidi
2782 iterator. */
2783 if (it->bidi_p)
2784 {
2785 /* Note the paragraph direction that this buffer wants to
2786 use. */
2787 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2788 Qleft_to_right))
2789 it->paragraph_embedding = L2R;
2790 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2791 Qright_to_left))
2792 it->paragraph_embedding = R2L;
2793 else
2794 it->paragraph_embedding = NEUTRAL_DIR;
2795 bidi_unshelve_cache (NULL, 0);
2796 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2797 &it->bidi_it);
2798 }
2799
2800 /* Compute faces etc. */
2801 reseat (it, it->current.pos, 1);
2802 }
2803
2804 CHECK_IT (it);
2805 }
2806
2807
2808 /* Initialize IT for the display of window W with window start POS. */
2809
2810 void
2811 start_display (struct it *it, struct window *w, struct text_pos pos)
2812 {
2813 struct glyph_row *row;
2814 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2815
2816 row = w->desired_matrix->rows + first_vpos;
2817 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2818 it->first_vpos = first_vpos;
2819
2820 /* Don't reseat to previous visible line start if current start
2821 position is in a string or image. */
2822 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2823 {
2824 int start_at_line_beg_p;
2825 int first_y = it->current_y;
2826
2827 /* If window start is not at a line start, skip forward to POS to
2828 get the correct continuation lines width. */
2829 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2830 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2831 if (!start_at_line_beg_p)
2832 {
2833 int new_x;
2834
2835 reseat_at_previous_visible_line_start (it);
2836 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2837
2838 new_x = it->current_x + it->pixel_width;
2839
2840 /* If lines are continued, this line may end in the middle
2841 of a multi-glyph character (e.g. a control character
2842 displayed as \003, or in the middle of an overlay
2843 string). In this case move_it_to above will not have
2844 taken us to the start of the continuation line but to the
2845 end of the continued line. */
2846 if (it->current_x > 0
2847 && it->line_wrap != TRUNCATE /* Lines are continued. */
2848 && (/* And glyph doesn't fit on the line. */
2849 new_x > it->last_visible_x
2850 /* Or it fits exactly and we're on a window
2851 system frame. */
2852 || (new_x == it->last_visible_x
2853 && FRAME_WINDOW_P (it->f))))
2854 {
2855 if ((it->current.dpvec_index >= 0
2856 || it->current.overlay_string_index >= 0)
2857 /* If we are on a newline from a display vector or
2858 overlay string, then we are already at the end of
2859 a screen line; no need to go to the next line in
2860 that case, as this line is not really continued.
2861 (If we do go to the next line, C-e will not DTRT.) */
2862 && it->c != '\n')
2863 {
2864 set_iterator_to_next (it, 1);
2865 move_it_in_display_line_to (it, -1, -1, 0);
2866 }
2867
2868 it->continuation_lines_width += it->current_x;
2869 }
2870 /* If the character at POS is displayed via a display
2871 vector, move_it_to above stops at the final glyph of
2872 IT->dpvec. To make the caller redisplay that character
2873 again (a.k.a. start at POS), we need to reset the
2874 dpvec_index to the beginning of IT->dpvec. */
2875 else if (it->current.dpvec_index >= 0)
2876 it->current.dpvec_index = 0;
2877
2878 /* We're starting a new display line, not affected by the
2879 height of the continued line, so clear the appropriate
2880 fields in the iterator structure. */
2881 it->max_ascent = it->max_descent = 0;
2882 it->max_phys_ascent = it->max_phys_descent = 0;
2883
2884 it->current_y = first_y;
2885 it->vpos = 0;
2886 it->current_x = it->hpos = 0;
2887 }
2888 }
2889 }
2890
2891
2892 /* Return 1 if POS is a position in ellipses displayed for invisible
2893 text. W is the window we display, for text property lookup. */
2894
2895 static int
2896 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2897 {
2898 Lisp_Object prop, window;
2899 int ellipses_p = 0;
2900 ptrdiff_t charpos = CHARPOS (pos->pos);
2901
2902 /* If POS specifies a position in a display vector, this might
2903 be for an ellipsis displayed for invisible text. We won't
2904 get the iterator set up for delivering that ellipsis unless
2905 we make sure that it gets aware of the invisible text. */
2906 if (pos->dpvec_index >= 0
2907 && pos->overlay_string_index < 0
2908 && CHARPOS (pos->string_pos) < 0
2909 && charpos > BEGV
2910 && (XSETWINDOW (window, w),
2911 prop = Fget_char_property (make_number (charpos),
2912 Qinvisible, window),
2913 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2914 {
2915 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2916 window);
2917 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2918 }
2919
2920 return ellipses_p;
2921 }
2922
2923
2924 /* Initialize IT for stepping through current_buffer in window W,
2925 starting at position POS that includes overlay string and display
2926 vector/ control character translation position information. Value
2927 is zero if there are overlay strings with newlines at POS. */
2928
2929 static int
2930 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2931 {
2932 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2933 int i, overlay_strings_with_newlines = 0;
2934
2935 /* If POS specifies a position in a display vector, this might
2936 be for an ellipsis displayed for invisible text. We won't
2937 get the iterator set up for delivering that ellipsis unless
2938 we make sure that it gets aware of the invisible text. */
2939 if (in_ellipses_for_invisible_text_p (pos, w))
2940 {
2941 --charpos;
2942 bytepos = 0;
2943 }
2944
2945 /* Keep in mind: the call to reseat in init_iterator skips invisible
2946 text, so we might end up at a position different from POS. This
2947 is only a problem when POS is a row start after a newline and an
2948 overlay starts there with an after-string, and the overlay has an
2949 invisible property. Since we don't skip invisible text in
2950 display_line and elsewhere immediately after consuming the
2951 newline before the row start, such a POS will not be in a string,
2952 but the call to init_iterator below will move us to the
2953 after-string. */
2954 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2955
2956 /* This only scans the current chunk -- it should scan all chunks.
2957 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2958 to 16 in 22.1 to make this a lesser problem. */
2959 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2960 {
2961 const char *s = SSDATA (it->overlay_strings[i]);
2962 const char *e = s + SBYTES (it->overlay_strings[i]);
2963
2964 while (s < e && *s != '\n')
2965 ++s;
2966
2967 if (s < e)
2968 {
2969 overlay_strings_with_newlines = 1;
2970 break;
2971 }
2972 }
2973
2974 /* If position is within an overlay string, set up IT to the right
2975 overlay string. */
2976 if (pos->overlay_string_index >= 0)
2977 {
2978 int relative_index;
2979
2980 /* If the first overlay string happens to have a `display'
2981 property for an image, the iterator will be set up for that
2982 image, and we have to undo that setup first before we can
2983 correct the overlay string index. */
2984 if (it->method == GET_FROM_IMAGE)
2985 pop_it (it);
2986
2987 /* We already have the first chunk of overlay strings in
2988 IT->overlay_strings. Load more until the one for
2989 pos->overlay_string_index is in IT->overlay_strings. */
2990 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2991 {
2992 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2993 it->current.overlay_string_index = 0;
2994 while (n--)
2995 {
2996 load_overlay_strings (it, 0);
2997 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2998 }
2999 }
3000
3001 it->current.overlay_string_index = pos->overlay_string_index;
3002 relative_index = (it->current.overlay_string_index
3003 % OVERLAY_STRING_CHUNK_SIZE);
3004 it->string = it->overlay_strings[relative_index];
3005 xassert (STRINGP (it->string));
3006 it->current.string_pos = pos->string_pos;
3007 it->method = GET_FROM_STRING;
3008 }
3009
3010 if (CHARPOS (pos->string_pos) >= 0)
3011 {
3012 /* Recorded position is not in an overlay string, but in another
3013 string. This can only be a string from a `display' property.
3014 IT should already be filled with that string. */
3015 it->current.string_pos = pos->string_pos;
3016 xassert (STRINGP (it->string));
3017 }
3018
3019 /* Restore position in display vector translations, control
3020 character translations or ellipses. */
3021 if (pos->dpvec_index >= 0)
3022 {
3023 if (it->dpvec == NULL)
3024 get_next_display_element (it);
3025 xassert (it->dpvec && it->current.dpvec_index == 0);
3026 it->current.dpvec_index = pos->dpvec_index;
3027 }
3028
3029 CHECK_IT (it);
3030 return !overlay_strings_with_newlines;
3031 }
3032
3033
3034 /* Initialize IT for stepping through current_buffer in window W
3035 starting at ROW->start. */
3036
3037 static void
3038 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3039 {
3040 init_from_display_pos (it, w, &row->start);
3041 it->start = row->start;
3042 it->continuation_lines_width = row->continuation_lines_width;
3043 CHECK_IT (it);
3044 }
3045
3046
3047 /* Initialize IT for stepping through current_buffer in window W
3048 starting in the line following ROW, i.e. starting at ROW->end.
3049 Value is zero if there are overlay strings with newlines at ROW's
3050 end position. */
3051
3052 static int
3053 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3054 {
3055 int success = 0;
3056
3057 if (init_from_display_pos (it, w, &row->end))
3058 {
3059 if (row->continued_p)
3060 it->continuation_lines_width
3061 = row->continuation_lines_width + row->pixel_width;
3062 CHECK_IT (it);
3063 success = 1;
3064 }
3065
3066 return success;
3067 }
3068
3069
3070
3071 \f
3072 /***********************************************************************
3073 Text properties
3074 ***********************************************************************/
3075
3076 /* Called when IT reaches IT->stop_charpos. Handle text property and
3077 overlay changes. Set IT->stop_charpos to the next position where
3078 to stop. */
3079
3080 static void
3081 handle_stop (struct it *it)
3082 {
3083 enum prop_handled handled;
3084 int handle_overlay_change_p;
3085 struct props *p;
3086
3087 it->dpvec = NULL;
3088 it->current.dpvec_index = -1;
3089 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3090 it->ignore_overlay_strings_at_pos_p = 0;
3091 it->ellipsis_p = 0;
3092
3093 /* Use face of preceding text for ellipsis (if invisible) */
3094 if (it->selective_display_ellipsis_p)
3095 it->saved_face_id = it->face_id;
3096
3097 do
3098 {
3099 handled = HANDLED_NORMALLY;
3100
3101 /* Call text property handlers. */
3102 for (p = it_props; p->handler; ++p)
3103 {
3104 handled = p->handler (it);
3105
3106 if (handled == HANDLED_RECOMPUTE_PROPS)
3107 break;
3108 else if (handled == HANDLED_RETURN)
3109 {
3110 /* We still want to show before and after strings from
3111 overlays even if the actual buffer text is replaced. */
3112 if (!handle_overlay_change_p
3113 || it->sp > 1
3114 || !get_overlay_strings_1 (it, 0, 0))
3115 {
3116 if (it->ellipsis_p)
3117 setup_for_ellipsis (it, 0);
3118 /* When handling a display spec, we might load an
3119 empty string. In that case, discard it here. We
3120 used to discard it in handle_single_display_spec,
3121 but that causes get_overlay_strings_1, above, to
3122 ignore overlay strings that we must check. */
3123 if (STRINGP (it->string) && !SCHARS (it->string))
3124 pop_it (it);
3125 return;
3126 }
3127 else if (STRINGP (it->string) && !SCHARS (it->string))
3128 pop_it (it);
3129 else
3130 {
3131 it->ignore_overlay_strings_at_pos_p = 1;
3132 it->string_from_display_prop_p = 0;
3133 it->from_disp_prop_p = 0;
3134 handle_overlay_change_p = 0;
3135 }
3136 handled = HANDLED_RECOMPUTE_PROPS;
3137 break;
3138 }
3139 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3140 handle_overlay_change_p = 0;
3141 }
3142
3143 if (handled != HANDLED_RECOMPUTE_PROPS)
3144 {
3145 /* Don't check for overlay strings below when set to deliver
3146 characters from a display vector. */
3147 if (it->method == GET_FROM_DISPLAY_VECTOR)
3148 handle_overlay_change_p = 0;
3149
3150 /* Handle overlay changes.
3151 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3152 if it finds overlays. */
3153 if (handle_overlay_change_p)
3154 handled = handle_overlay_change (it);
3155 }
3156
3157 if (it->ellipsis_p)
3158 {
3159 setup_for_ellipsis (it, 0);
3160 break;
3161 }
3162 }
3163 while (handled == HANDLED_RECOMPUTE_PROPS);
3164
3165 /* Determine where to stop next. */
3166 if (handled == HANDLED_NORMALLY)
3167 compute_stop_pos (it);
3168 }
3169
3170
3171 /* Compute IT->stop_charpos from text property and overlay change
3172 information for IT's current position. */
3173
3174 static void
3175 compute_stop_pos (struct it *it)
3176 {
3177 register INTERVAL iv, next_iv;
3178 Lisp_Object object, limit, position;
3179 ptrdiff_t charpos, bytepos;
3180
3181 if (STRINGP (it->string))
3182 {
3183 /* Strings are usually short, so don't limit the search for
3184 properties. */
3185 it->stop_charpos = it->end_charpos;
3186 object = it->string;
3187 limit = Qnil;
3188 charpos = IT_STRING_CHARPOS (*it);
3189 bytepos = IT_STRING_BYTEPOS (*it);
3190 }
3191 else
3192 {
3193 ptrdiff_t pos;
3194
3195 /* If end_charpos is out of range for some reason, such as a
3196 misbehaving display function, rationalize it (Bug#5984). */
3197 if (it->end_charpos > ZV)
3198 it->end_charpos = ZV;
3199 it->stop_charpos = it->end_charpos;
3200
3201 /* If next overlay change is in front of the current stop pos
3202 (which is IT->end_charpos), stop there. Note: value of
3203 next_overlay_change is point-max if no overlay change
3204 follows. */
3205 charpos = IT_CHARPOS (*it);
3206 bytepos = IT_BYTEPOS (*it);
3207 pos = next_overlay_change (charpos);
3208 if (pos < it->stop_charpos)
3209 it->stop_charpos = pos;
3210
3211 /* If showing the region, we have to stop at the region
3212 start or end because the face might change there. */
3213 if (it->region_beg_charpos > 0)
3214 {
3215 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3216 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3217 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3218 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3219 }
3220
3221 /* Set up variables for computing the stop position from text
3222 property changes. */
3223 XSETBUFFER (object, current_buffer);
3224 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3225 }
3226
3227 /* Get the interval containing IT's position. Value is a null
3228 interval if there isn't such an interval. */
3229 position = make_number (charpos);
3230 iv = validate_interval_range (object, &position, &position, 0);
3231 if (!NULL_INTERVAL_P (iv))
3232 {
3233 Lisp_Object values_here[LAST_PROP_IDX];
3234 struct props *p;
3235
3236 /* Get properties here. */
3237 for (p = it_props; p->handler; ++p)
3238 values_here[p->idx] = textget (iv->plist, *p->name);
3239
3240 /* Look for an interval following iv that has different
3241 properties. */
3242 for (next_iv = next_interval (iv);
3243 (!NULL_INTERVAL_P (next_iv)
3244 && (NILP (limit)
3245 || XFASTINT (limit) > next_iv->position));
3246 next_iv = next_interval (next_iv))
3247 {
3248 for (p = it_props; p->handler; ++p)
3249 {
3250 Lisp_Object new_value;
3251
3252 new_value = textget (next_iv->plist, *p->name);
3253 if (!EQ (values_here[p->idx], new_value))
3254 break;
3255 }
3256
3257 if (p->handler)
3258 break;
3259 }
3260
3261 if (!NULL_INTERVAL_P (next_iv))
3262 {
3263 if (INTEGERP (limit)
3264 && next_iv->position >= XFASTINT (limit))
3265 /* No text property change up to limit. */
3266 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3267 else
3268 /* Text properties change in next_iv. */
3269 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3270 }
3271 }
3272
3273 if (it->cmp_it.id < 0)
3274 {
3275 ptrdiff_t stoppos = it->end_charpos;
3276
3277 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3278 stoppos = -1;
3279 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3280 stoppos, it->string);
3281 }
3282
3283 xassert (STRINGP (it->string)
3284 || (it->stop_charpos >= BEGV
3285 && it->stop_charpos >= IT_CHARPOS (*it)));
3286 }
3287
3288
3289 /* Return the position of the next overlay change after POS in
3290 current_buffer. Value is point-max if no overlay change
3291 follows. This is like `next-overlay-change' but doesn't use
3292 xmalloc. */
3293
3294 static ptrdiff_t
3295 next_overlay_change (ptrdiff_t pos)
3296 {
3297 ptrdiff_t i, noverlays;
3298 ptrdiff_t endpos;
3299 Lisp_Object *overlays;
3300
3301 /* Get all overlays at the given position. */
3302 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3303
3304 /* If any of these overlays ends before endpos,
3305 use its ending point instead. */
3306 for (i = 0; i < noverlays; ++i)
3307 {
3308 Lisp_Object oend;
3309 ptrdiff_t oendpos;
3310
3311 oend = OVERLAY_END (overlays[i]);
3312 oendpos = OVERLAY_POSITION (oend);
3313 endpos = min (endpos, oendpos);
3314 }
3315
3316 return endpos;
3317 }
3318
3319 /* How many characters forward to search for a display property or
3320 display string. Searching too far forward makes the bidi display
3321 sluggish, especially in small windows. */
3322 #define MAX_DISP_SCAN 250
3323
3324 /* Return the character position of a display string at or after
3325 position specified by POSITION. If no display string exists at or
3326 after POSITION, return ZV. A display string is either an overlay
3327 with `display' property whose value is a string, or a `display'
3328 text property whose value is a string. STRING is data about the
3329 string to iterate; if STRING->lstring is nil, we are iterating a
3330 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3331 on a GUI frame. DISP_PROP is set to zero if we searched
3332 MAX_DISP_SCAN characters forward without finding any display
3333 strings, non-zero otherwise. It is set to 2 if the display string
3334 uses any kind of `(space ...)' spec that will produce a stretch of
3335 white space in the text area. */
3336 ptrdiff_t
3337 compute_display_string_pos (struct text_pos *position,
3338 struct bidi_string_data *string,
3339 int frame_window_p, int *disp_prop)
3340 {
3341 /* OBJECT = nil means current buffer. */
3342 Lisp_Object object =
3343 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3344 Lisp_Object pos, spec, limpos;
3345 int string_p = (string && (STRINGP (string->lstring) || string->s));
3346 ptrdiff_t eob = string_p ? string->schars : ZV;
3347 ptrdiff_t begb = string_p ? 0 : BEGV;
3348 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3349 ptrdiff_t lim =
3350 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3351 struct text_pos tpos;
3352 int rv = 0;
3353
3354 *disp_prop = 1;
3355
3356 if (charpos >= eob
3357 /* We don't support display properties whose values are strings
3358 that have display string properties. */
3359 || string->from_disp_str
3360 /* C strings cannot have display properties. */
3361 || (string->s && !STRINGP (object)))
3362 {
3363 *disp_prop = 0;
3364 return eob;
3365 }
3366
3367 /* If the character at CHARPOS is where the display string begins,
3368 return CHARPOS. */
3369 pos = make_number (charpos);
3370 if (STRINGP (object))
3371 bufpos = string->bufpos;
3372 else
3373 bufpos = charpos;
3374 tpos = *position;
3375 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3376 && (charpos <= begb
3377 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3378 object),
3379 spec))
3380 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3381 frame_window_p)))
3382 {
3383 if (rv == 2)
3384 *disp_prop = 2;
3385 return charpos;
3386 }
3387
3388 /* Look forward for the first character with a `display' property
3389 that will replace the underlying text when displayed. */
3390 limpos = make_number (lim);
3391 do {
3392 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3393 CHARPOS (tpos) = XFASTINT (pos);
3394 if (CHARPOS (tpos) >= lim)
3395 {
3396 *disp_prop = 0;
3397 break;
3398 }
3399 if (STRINGP (object))
3400 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3401 else
3402 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3403 spec = Fget_char_property (pos, Qdisplay, object);
3404 if (!STRINGP (object))
3405 bufpos = CHARPOS (tpos);
3406 } while (NILP (spec)
3407 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3408 bufpos, frame_window_p)));
3409 if (rv == 2)
3410 *disp_prop = 2;
3411
3412 return CHARPOS (tpos);
3413 }
3414
3415 /* Return the character position of the end of the display string that
3416 started at CHARPOS. If there's no display string at CHARPOS,
3417 return -1. A display string is either an overlay with `display'
3418 property whose value is a string or a `display' text property whose
3419 value is a string. */
3420 ptrdiff_t
3421 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3422 {
3423 /* OBJECT = nil means current buffer. */
3424 Lisp_Object object =
3425 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3426 Lisp_Object pos = make_number (charpos);
3427 ptrdiff_t eob =
3428 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3429
3430 if (charpos >= eob || (string->s && !STRINGP (object)))
3431 return eob;
3432
3433 /* It could happen that the display property or overlay was removed
3434 since we found it in compute_display_string_pos above. One way
3435 this can happen is if JIT font-lock was called (through
3436 handle_fontified_prop), and jit-lock-functions remove text
3437 properties or overlays from the portion of buffer that includes
3438 CHARPOS. Muse mode is known to do that, for example. In this
3439 case, we return -1 to the caller, to signal that no display
3440 string is actually present at CHARPOS. See bidi_fetch_char for
3441 how this is handled.
3442
3443 An alternative would be to never look for display properties past
3444 it->stop_charpos. But neither compute_display_string_pos nor
3445 bidi_fetch_char that calls it know or care where the next
3446 stop_charpos is. */
3447 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3448 return -1;
3449
3450 /* Look forward for the first character where the `display' property
3451 changes. */
3452 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3453
3454 return XFASTINT (pos);
3455 }
3456
3457
3458 \f
3459 /***********************************************************************
3460 Fontification
3461 ***********************************************************************/
3462
3463 /* Handle changes in the `fontified' property of the current buffer by
3464 calling hook functions from Qfontification_functions to fontify
3465 regions of text. */
3466
3467 static enum prop_handled
3468 handle_fontified_prop (struct it *it)
3469 {
3470 Lisp_Object prop, pos;
3471 enum prop_handled handled = HANDLED_NORMALLY;
3472
3473 if (!NILP (Vmemory_full))
3474 return handled;
3475
3476 /* Get the value of the `fontified' property at IT's current buffer
3477 position. (The `fontified' property doesn't have a special
3478 meaning in strings.) If the value is nil, call functions from
3479 Qfontification_functions. */
3480 if (!STRINGP (it->string)
3481 && it->s == NULL
3482 && !NILP (Vfontification_functions)
3483 && !NILP (Vrun_hooks)
3484 && (pos = make_number (IT_CHARPOS (*it)),
3485 prop = Fget_char_property (pos, Qfontified, Qnil),
3486 /* Ignore the special cased nil value always present at EOB since
3487 no amount of fontifying will be able to change it. */
3488 NILP (prop) && IT_CHARPOS (*it) < Z))
3489 {
3490 ptrdiff_t count = SPECPDL_INDEX ();
3491 Lisp_Object val;
3492 struct buffer *obuf = current_buffer;
3493 int begv = BEGV, zv = ZV;
3494 int old_clip_changed = current_buffer->clip_changed;
3495
3496 val = Vfontification_functions;
3497 specbind (Qfontification_functions, Qnil);
3498
3499 xassert (it->end_charpos == ZV);
3500
3501 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3502 safe_call1 (val, pos);
3503 else
3504 {
3505 Lisp_Object fns, fn;
3506 struct gcpro gcpro1, gcpro2;
3507
3508 fns = Qnil;
3509 GCPRO2 (val, fns);
3510
3511 for (; CONSP (val); val = XCDR (val))
3512 {
3513 fn = XCAR (val);
3514
3515 if (EQ (fn, Qt))
3516 {
3517 /* A value of t indicates this hook has a local
3518 binding; it means to run the global binding too.
3519 In a global value, t should not occur. If it
3520 does, we must ignore it to avoid an endless
3521 loop. */
3522 for (fns = Fdefault_value (Qfontification_functions);
3523 CONSP (fns);
3524 fns = XCDR (fns))
3525 {
3526 fn = XCAR (fns);
3527 if (!EQ (fn, Qt))
3528 safe_call1 (fn, pos);
3529 }
3530 }
3531 else
3532 safe_call1 (fn, pos);
3533 }
3534
3535 UNGCPRO;
3536 }
3537
3538 unbind_to (count, Qnil);
3539
3540 /* Fontification functions routinely call `save-restriction'.
3541 Normally, this tags clip_changed, which can confuse redisplay
3542 (see discussion in Bug#6671). Since we don't perform any
3543 special handling of fontification changes in the case where
3544 `save-restriction' isn't called, there's no point doing so in
3545 this case either. So, if the buffer's restrictions are
3546 actually left unchanged, reset clip_changed. */
3547 if (obuf == current_buffer)
3548 {
3549 if (begv == BEGV && zv == ZV)
3550 current_buffer->clip_changed = old_clip_changed;
3551 }
3552 /* There isn't much we can reasonably do to protect against
3553 misbehaving fontification, but here's a fig leaf. */
3554 else if (!NILP (BVAR (obuf, name)))
3555 set_buffer_internal_1 (obuf);
3556
3557 /* The fontification code may have added/removed text.
3558 It could do even a lot worse, but let's at least protect against
3559 the most obvious case where only the text past `pos' gets changed',
3560 as is/was done in grep.el where some escapes sequences are turned
3561 into face properties (bug#7876). */
3562 it->end_charpos = ZV;
3563
3564 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3565 something. This avoids an endless loop if they failed to
3566 fontify the text for which reason ever. */
3567 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3568 handled = HANDLED_RECOMPUTE_PROPS;
3569 }
3570
3571 return handled;
3572 }
3573
3574
3575 \f
3576 /***********************************************************************
3577 Faces
3578 ***********************************************************************/
3579
3580 /* Set up iterator IT from face properties at its current position.
3581 Called from handle_stop. */
3582
3583 static enum prop_handled
3584 handle_face_prop (struct it *it)
3585 {
3586 int new_face_id;
3587 ptrdiff_t next_stop;
3588
3589 if (!STRINGP (it->string))
3590 {
3591 new_face_id
3592 = face_at_buffer_position (it->w,
3593 IT_CHARPOS (*it),
3594 it->region_beg_charpos,
3595 it->region_end_charpos,
3596 &next_stop,
3597 (IT_CHARPOS (*it)
3598 + TEXT_PROP_DISTANCE_LIMIT),
3599 0, it->base_face_id);
3600
3601 /* Is this a start of a run of characters with box face?
3602 Caveat: this can be called for a freshly initialized
3603 iterator; face_id is -1 in this case. We know that the new
3604 face will not change until limit, i.e. if the new face has a
3605 box, all characters up to limit will have one. But, as
3606 usual, we don't know whether limit is really the end. */
3607 if (new_face_id != it->face_id)
3608 {
3609 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3610
3611 /* If new face has a box but old face has not, this is
3612 the start of a run of characters with box, i.e. it has
3613 a shadow on the left side. The value of face_id of the
3614 iterator will be -1 if this is the initial call that gets
3615 the face. In this case, we have to look in front of IT's
3616 position and see whether there is a face != new_face_id. */
3617 it->start_of_box_run_p
3618 = (new_face->box != FACE_NO_BOX
3619 && (it->face_id >= 0
3620 || IT_CHARPOS (*it) == BEG
3621 || new_face_id != face_before_it_pos (it)));
3622 it->face_box_p = new_face->box != FACE_NO_BOX;
3623 }
3624 }
3625 else
3626 {
3627 int base_face_id;
3628 ptrdiff_t bufpos;
3629 int i;
3630 Lisp_Object from_overlay
3631 = (it->current.overlay_string_index >= 0
3632 ? it->string_overlays[it->current.overlay_string_index]
3633 : Qnil);
3634
3635 /* See if we got to this string directly or indirectly from
3636 an overlay property. That includes the before-string or
3637 after-string of an overlay, strings in display properties
3638 provided by an overlay, their text properties, etc.
3639
3640 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3641 if (! NILP (from_overlay))
3642 for (i = it->sp - 1; i >= 0; i--)
3643 {
3644 if (it->stack[i].current.overlay_string_index >= 0)
3645 from_overlay
3646 = it->string_overlays[it->stack[i].current.overlay_string_index];
3647 else if (! NILP (it->stack[i].from_overlay))
3648 from_overlay = it->stack[i].from_overlay;
3649
3650 if (!NILP (from_overlay))
3651 break;
3652 }
3653
3654 if (! NILP (from_overlay))
3655 {
3656 bufpos = IT_CHARPOS (*it);
3657 /* For a string from an overlay, the base face depends
3658 only on text properties and ignores overlays. */
3659 base_face_id
3660 = face_for_overlay_string (it->w,
3661 IT_CHARPOS (*it),
3662 it->region_beg_charpos,
3663 it->region_end_charpos,
3664 &next_stop,
3665 (IT_CHARPOS (*it)
3666 + TEXT_PROP_DISTANCE_LIMIT),
3667 0,
3668 from_overlay);
3669 }
3670 else
3671 {
3672 bufpos = 0;
3673
3674 /* For strings from a `display' property, use the face at
3675 IT's current buffer position as the base face to merge
3676 with, so that overlay strings appear in the same face as
3677 surrounding text, unless they specify their own
3678 faces. */
3679 base_face_id = it->string_from_prefix_prop_p
3680 ? DEFAULT_FACE_ID
3681 : underlying_face_id (it);
3682 }
3683
3684 new_face_id = face_at_string_position (it->w,
3685 it->string,
3686 IT_STRING_CHARPOS (*it),
3687 bufpos,
3688 it->region_beg_charpos,
3689 it->region_end_charpos,
3690 &next_stop,
3691 base_face_id, 0);
3692
3693 /* Is this a start of a run of characters with box? Caveat:
3694 this can be called for a freshly allocated iterator; face_id
3695 is -1 is this case. We know that the new face will not
3696 change until the next check pos, i.e. if the new face has a
3697 box, all characters up to that position will have a
3698 box. But, as usual, we don't know whether that position
3699 is really the end. */
3700 if (new_face_id != it->face_id)
3701 {
3702 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3703 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3704
3705 /* If new face has a box but old face hasn't, this is the
3706 start of a run of characters with box, i.e. it has a
3707 shadow on the left side. */
3708 it->start_of_box_run_p
3709 = new_face->box && (old_face == NULL || !old_face->box);
3710 it->face_box_p = new_face->box != FACE_NO_BOX;
3711 }
3712 }
3713
3714 it->face_id = new_face_id;
3715 return HANDLED_NORMALLY;
3716 }
3717
3718
3719 /* Return the ID of the face ``underlying'' IT's current position,
3720 which is in a string. If the iterator is associated with a
3721 buffer, return the face at IT's current buffer position.
3722 Otherwise, use the iterator's base_face_id. */
3723
3724 static int
3725 underlying_face_id (struct it *it)
3726 {
3727 int face_id = it->base_face_id, i;
3728
3729 xassert (STRINGP (it->string));
3730
3731 for (i = it->sp - 1; i >= 0; --i)
3732 if (NILP (it->stack[i].string))
3733 face_id = it->stack[i].face_id;
3734
3735 return face_id;
3736 }
3737
3738
3739 /* Compute the face one character before or after the current position
3740 of IT, in the visual order. BEFORE_P non-zero means get the face
3741 in front (to the left in L2R paragraphs, to the right in R2L
3742 paragraphs) of IT's screen position. Value is the ID of the face. */
3743
3744 static int
3745 face_before_or_after_it_pos (struct it *it, int before_p)
3746 {
3747 int face_id, limit;
3748 ptrdiff_t next_check_charpos;
3749 struct it it_copy;
3750 void *it_copy_data = NULL;
3751
3752 xassert (it->s == NULL);
3753
3754 if (STRINGP (it->string))
3755 {
3756 ptrdiff_t bufpos, charpos;
3757 int base_face_id;
3758
3759 /* No face change past the end of the string (for the case
3760 we are padding with spaces). No face change before the
3761 string start. */
3762 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3763 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3764 return it->face_id;
3765
3766 if (!it->bidi_p)
3767 {
3768 /* Set charpos to the position before or after IT's current
3769 position, in the logical order, which in the non-bidi
3770 case is the same as the visual order. */
3771 if (before_p)
3772 charpos = IT_STRING_CHARPOS (*it) - 1;
3773 else if (it->what == IT_COMPOSITION)
3774 /* For composition, we must check the character after the
3775 composition. */
3776 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3777 else
3778 charpos = IT_STRING_CHARPOS (*it) + 1;
3779 }
3780 else
3781 {
3782 if (before_p)
3783 {
3784 /* With bidi iteration, the character before the current
3785 in the visual order cannot be found by simple
3786 iteration, because "reverse" reordering is not
3787 supported. Instead, we need to use the move_it_*
3788 family of functions. */
3789 /* Ignore face changes before the first visible
3790 character on this display line. */
3791 if (it->current_x <= it->first_visible_x)
3792 return it->face_id;
3793 SAVE_IT (it_copy, *it, it_copy_data);
3794 /* Implementation note: Since move_it_in_display_line
3795 works in the iterator geometry, and thinks the first
3796 character is always the leftmost, even in R2L lines,
3797 we don't need to distinguish between the R2L and L2R
3798 cases here. */
3799 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3800 it_copy.current_x - 1, MOVE_TO_X);
3801 charpos = IT_STRING_CHARPOS (it_copy);
3802 RESTORE_IT (it, it, it_copy_data);
3803 }
3804 else
3805 {
3806 /* Set charpos to the string position of the character
3807 that comes after IT's current position in the visual
3808 order. */
3809 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3810
3811 it_copy = *it;
3812 while (n--)
3813 bidi_move_to_visually_next (&it_copy.bidi_it);
3814
3815 charpos = it_copy.bidi_it.charpos;
3816 }
3817 }
3818 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3819
3820 if (it->current.overlay_string_index >= 0)
3821 bufpos = IT_CHARPOS (*it);
3822 else
3823 bufpos = 0;
3824
3825 base_face_id = underlying_face_id (it);
3826
3827 /* Get the face for ASCII, or unibyte. */
3828 face_id = face_at_string_position (it->w,
3829 it->string,
3830 charpos,
3831 bufpos,
3832 it->region_beg_charpos,
3833 it->region_end_charpos,
3834 &next_check_charpos,
3835 base_face_id, 0);
3836
3837 /* Correct the face for charsets different from ASCII. Do it
3838 for the multibyte case only. The face returned above is
3839 suitable for unibyte text if IT->string is unibyte. */
3840 if (STRING_MULTIBYTE (it->string))
3841 {
3842 struct text_pos pos1 = string_pos (charpos, it->string);
3843 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3844 int c, len;
3845 struct face *face = FACE_FROM_ID (it->f, face_id);
3846
3847 c = string_char_and_length (p, &len);
3848 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3849 }
3850 }
3851 else
3852 {
3853 struct text_pos pos;
3854
3855 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3856 || (IT_CHARPOS (*it) <= BEGV && before_p))
3857 return it->face_id;
3858
3859 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3860 pos = it->current.pos;
3861
3862 if (!it->bidi_p)
3863 {
3864 if (before_p)
3865 DEC_TEXT_POS (pos, it->multibyte_p);
3866 else
3867 {
3868 if (it->what == IT_COMPOSITION)
3869 {
3870 /* For composition, we must check the position after
3871 the composition. */
3872 pos.charpos += it->cmp_it.nchars;
3873 pos.bytepos += it->len;
3874 }
3875 else
3876 INC_TEXT_POS (pos, it->multibyte_p);
3877 }
3878 }
3879 else
3880 {
3881 if (before_p)
3882 {
3883 /* With bidi iteration, the character before the current
3884 in the visual order cannot be found by simple
3885 iteration, because "reverse" reordering is not
3886 supported. Instead, we need to use the move_it_*
3887 family of functions. */
3888 /* Ignore face changes before the first visible
3889 character on this display line. */
3890 if (it->current_x <= it->first_visible_x)
3891 return it->face_id;
3892 SAVE_IT (it_copy, *it, it_copy_data);
3893 /* Implementation note: Since move_it_in_display_line
3894 works in the iterator geometry, and thinks the first
3895 character is always the leftmost, even in R2L lines,
3896 we don't need to distinguish between the R2L and L2R
3897 cases here. */
3898 move_it_in_display_line (&it_copy, ZV,
3899 it_copy.current_x - 1, MOVE_TO_X);
3900 pos = it_copy.current.pos;
3901 RESTORE_IT (it, it, it_copy_data);
3902 }
3903 else
3904 {
3905 /* Set charpos to the buffer position of the character
3906 that comes after IT's current position in the visual
3907 order. */
3908 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3909
3910 it_copy = *it;
3911 while (n--)
3912 bidi_move_to_visually_next (&it_copy.bidi_it);
3913
3914 SET_TEXT_POS (pos,
3915 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3916 }
3917 }
3918 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3919
3920 /* Determine face for CHARSET_ASCII, or unibyte. */
3921 face_id = face_at_buffer_position (it->w,
3922 CHARPOS (pos),
3923 it->region_beg_charpos,
3924 it->region_end_charpos,
3925 &next_check_charpos,
3926 limit, 0, -1);
3927
3928 /* Correct the face for charsets different from ASCII. Do it
3929 for the multibyte case only. The face returned above is
3930 suitable for unibyte text if current_buffer is unibyte. */
3931 if (it->multibyte_p)
3932 {
3933 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3934 struct face *face = FACE_FROM_ID (it->f, face_id);
3935 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3936 }
3937 }
3938
3939 return face_id;
3940 }
3941
3942
3943 \f
3944 /***********************************************************************
3945 Invisible text
3946 ***********************************************************************/
3947
3948 /* Set up iterator IT from invisible properties at its current
3949 position. Called from handle_stop. */
3950
3951 static enum prop_handled
3952 handle_invisible_prop (struct it *it)
3953 {
3954 enum prop_handled handled = HANDLED_NORMALLY;
3955
3956 if (STRINGP (it->string))
3957 {
3958 Lisp_Object prop, end_charpos, limit, charpos;
3959
3960 /* Get the value of the invisible text property at the
3961 current position. Value will be nil if there is no such
3962 property. */
3963 charpos = make_number (IT_STRING_CHARPOS (*it));
3964 prop = Fget_text_property (charpos, Qinvisible, it->string);
3965
3966 if (!NILP (prop)
3967 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3968 {
3969 ptrdiff_t endpos;
3970
3971 handled = HANDLED_RECOMPUTE_PROPS;
3972
3973 /* Get the position at which the next change of the
3974 invisible text property can be found in IT->string.
3975 Value will be nil if the property value is the same for
3976 all the rest of IT->string. */
3977 XSETINT (limit, SCHARS (it->string));
3978 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3979 it->string, limit);
3980
3981 /* Text at current position is invisible. The next
3982 change in the property is at position end_charpos.
3983 Move IT's current position to that position. */
3984 if (INTEGERP (end_charpos)
3985 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3986 {
3987 struct text_pos old;
3988 ptrdiff_t oldpos;
3989
3990 old = it->current.string_pos;
3991 oldpos = CHARPOS (old);
3992 if (it->bidi_p)
3993 {
3994 if (it->bidi_it.first_elt
3995 && it->bidi_it.charpos < SCHARS (it->string))
3996 bidi_paragraph_init (it->paragraph_embedding,
3997 &it->bidi_it, 1);
3998 /* Bidi-iterate out of the invisible text. */
3999 do
4000 {
4001 bidi_move_to_visually_next (&it->bidi_it);
4002 }
4003 while (oldpos <= it->bidi_it.charpos
4004 && it->bidi_it.charpos < endpos);
4005
4006 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4007 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4008 if (IT_CHARPOS (*it) >= endpos)
4009 it->prev_stop = endpos;
4010 }
4011 else
4012 {
4013 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4014 compute_string_pos (&it->current.string_pos, old, it->string);
4015 }
4016 }
4017 else
4018 {
4019 /* The rest of the string is invisible. If this is an
4020 overlay string, proceed with the next overlay string
4021 or whatever comes and return a character from there. */
4022 if (it->current.overlay_string_index >= 0)
4023 {
4024 next_overlay_string (it);
4025 /* Don't check for overlay strings when we just
4026 finished processing them. */
4027 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4028 }
4029 else
4030 {
4031 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4032 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4033 }
4034 }
4035 }
4036 }
4037 else
4038 {
4039 int invis_p;
4040 ptrdiff_t newpos, next_stop, start_charpos, tem;
4041 Lisp_Object pos, prop, overlay;
4042
4043 /* First of all, is there invisible text at this position? */
4044 tem = start_charpos = IT_CHARPOS (*it);
4045 pos = make_number (tem);
4046 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4047 &overlay);
4048 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4049
4050 /* If we are on invisible text, skip over it. */
4051 if (invis_p && start_charpos < it->end_charpos)
4052 {
4053 /* Record whether we have to display an ellipsis for the
4054 invisible text. */
4055 int display_ellipsis_p = invis_p == 2;
4056
4057 handled = HANDLED_RECOMPUTE_PROPS;
4058
4059 /* Loop skipping over invisible text. The loop is left at
4060 ZV or with IT on the first char being visible again. */
4061 do
4062 {
4063 /* Try to skip some invisible text. Return value is the
4064 position reached which can be equal to where we start
4065 if there is nothing invisible there. This skips both
4066 over invisible text properties and overlays with
4067 invisible property. */
4068 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4069
4070 /* If we skipped nothing at all we weren't at invisible
4071 text in the first place. If everything to the end of
4072 the buffer was skipped, end the loop. */
4073 if (newpos == tem || newpos >= ZV)
4074 invis_p = 0;
4075 else
4076 {
4077 /* We skipped some characters but not necessarily
4078 all there are. Check if we ended up on visible
4079 text. Fget_char_property returns the property of
4080 the char before the given position, i.e. if we
4081 get invis_p = 0, this means that the char at
4082 newpos is visible. */
4083 pos = make_number (newpos);
4084 prop = Fget_char_property (pos, Qinvisible, it->window);
4085 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4086 }
4087
4088 /* If we ended up on invisible text, proceed to
4089 skip starting with next_stop. */
4090 if (invis_p)
4091 tem = next_stop;
4092
4093 /* If there are adjacent invisible texts, don't lose the
4094 second one's ellipsis. */
4095 if (invis_p == 2)
4096 display_ellipsis_p = 1;
4097 }
4098 while (invis_p);
4099
4100 /* The position newpos is now either ZV or on visible text. */
4101 if (it->bidi_p)
4102 {
4103 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4104 int on_newline =
4105 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4106 int after_newline =
4107 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4108
4109 /* If the invisible text ends on a newline or on a
4110 character after a newline, we can avoid the costly,
4111 character by character, bidi iteration to NEWPOS, and
4112 instead simply reseat the iterator there. That's
4113 because all bidi reordering information is tossed at
4114 the newline. This is a big win for modes that hide
4115 complete lines, like Outline, Org, etc. */
4116 if (on_newline || after_newline)
4117 {
4118 struct text_pos tpos;
4119 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4120
4121 SET_TEXT_POS (tpos, newpos, bpos);
4122 reseat_1 (it, tpos, 0);
4123 /* If we reseat on a newline/ZV, we need to prep the
4124 bidi iterator for advancing to the next character
4125 after the newline/EOB, keeping the current paragraph
4126 direction (so that PRODUCE_GLYPHS does TRT wrt
4127 prepending/appending glyphs to a glyph row). */
4128 if (on_newline)
4129 {
4130 it->bidi_it.first_elt = 0;
4131 it->bidi_it.paragraph_dir = pdir;
4132 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4133 it->bidi_it.nchars = 1;
4134 it->bidi_it.ch_len = 1;
4135 }
4136 }
4137 else /* Must use the slow method. */
4138 {
4139 /* With bidi iteration, the region of invisible text
4140 could start and/or end in the middle of a
4141 non-base embedding level. Therefore, we need to
4142 skip invisible text using the bidi iterator,
4143 starting at IT's current position, until we find
4144 ourselves outside of the invisible text.
4145 Skipping invisible text _after_ bidi iteration
4146 avoids affecting the visual order of the
4147 displayed text when invisible properties are
4148 added or removed. */
4149 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4150 {
4151 /* If we were `reseat'ed to a new paragraph,
4152 determine the paragraph base direction. We
4153 need to do it now because
4154 next_element_from_buffer may not have a
4155 chance to do it, if we are going to skip any
4156 text at the beginning, which resets the
4157 FIRST_ELT flag. */
4158 bidi_paragraph_init (it->paragraph_embedding,
4159 &it->bidi_it, 1);
4160 }
4161 do
4162 {
4163 bidi_move_to_visually_next (&it->bidi_it);
4164 }
4165 while (it->stop_charpos <= it->bidi_it.charpos
4166 && it->bidi_it.charpos < newpos);
4167 IT_CHARPOS (*it) = it->bidi_it.charpos;
4168 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4169 /* If we overstepped NEWPOS, record its position in
4170 the iterator, so that we skip invisible text if
4171 later the bidi iteration lands us in the
4172 invisible region again. */
4173 if (IT_CHARPOS (*it) >= newpos)
4174 it->prev_stop = newpos;
4175 }
4176 }
4177 else
4178 {
4179 IT_CHARPOS (*it) = newpos;
4180 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4181 }
4182
4183 /* If there are before-strings at the start of invisible
4184 text, and the text is invisible because of a text
4185 property, arrange to show before-strings because 20.x did
4186 it that way. (If the text is invisible because of an
4187 overlay property instead of a text property, this is
4188 already handled in the overlay code.) */
4189 if (NILP (overlay)
4190 && get_overlay_strings (it, it->stop_charpos))
4191 {
4192 handled = HANDLED_RECOMPUTE_PROPS;
4193 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4194 }
4195 else if (display_ellipsis_p)
4196 {
4197 /* Make sure that the glyphs of the ellipsis will get
4198 correct `charpos' values. If we would not update
4199 it->position here, the glyphs would belong to the
4200 last visible character _before_ the invisible
4201 text, which confuses `set_cursor_from_row'.
4202
4203 We use the last invisible position instead of the
4204 first because this way the cursor is always drawn on
4205 the first "." of the ellipsis, whenever PT is inside
4206 the invisible text. Otherwise the cursor would be
4207 placed _after_ the ellipsis when the point is after the
4208 first invisible character. */
4209 if (!STRINGP (it->object))
4210 {
4211 it->position.charpos = newpos - 1;
4212 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4213 }
4214 it->ellipsis_p = 1;
4215 /* Let the ellipsis display before
4216 considering any properties of the following char.
4217 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4218 handled = HANDLED_RETURN;
4219 }
4220 }
4221 }
4222
4223 return handled;
4224 }
4225
4226
4227 /* Make iterator IT return `...' next.
4228 Replaces LEN characters from buffer. */
4229
4230 static void
4231 setup_for_ellipsis (struct it *it, int len)
4232 {
4233 /* Use the display table definition for `...'. Invalid glyphs
4234 will be handled by the method returning elements from dpvec. */
4235 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4236 {
4237 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4238 it->dpvec = v->contents;
4239 it->dpend = v->contents + v->header.size;
4240 }
4241 else
4242 {
4243 /* Default `...'. */
4244 it->dpvec = default_invis_vector;
4245 it->dpend = default_invis_vector + 3;
4246 }
4247
4248 it->dpvec_char_len = len;
4249 it->current.dpvec_index = 0;
4250 it->dpvec_face_id = -1;
4251
4252 /* Remember the current face id in case glyphs specify faces.
4253 IT's face is restored in set_iterator_to_next.
4254 saved_face_id was set to preceding char's face in handle_stop. */
4255 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4256 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4257
4258 it->method = GET_FROM_DISPLAY_VECTOR;
4259 it->ellipsis_p = 1;
4260 }
4261
4262
4263 \f
4264 /***********************************************************************
4265 'display' property
4266 ***********************************************************************/
4267
4268 /* Set up iterator IT from `display' property at its current position.
4269 Called from handle_stop.
4270 We return HANDLED_RETURN if some part of the display property
4271 overrides the display of the buffer text itself.
4272 Otherwise we return HANDLED_NORMALLY. */
4273
4274 static enum prop_handled
4275 handle_display_prop (struct it *it)
4276 {
4277 Lisp_Object propval, object, overlay;
4278 struct text_pos *position;
4279 ptrdiff_t bufpos;
4280 /* Nonzero if some property replaces the display of the text itself. */
4281 int display_replaced_p = 0;
4282
4283 if (STRINGP (it->string))
4284 {
4285 object = it->string;
4286 position = &it->current.string_pos;
4287 bufpos = CHARPOS (it->current.pos);
4288 }
4289 else
4290 {
4291 XSETWINDOW (object, it->w);
4292 position = &it->current.pos;
4293 bufpos = CHARPOS (*position);
4294 }
4295
4296 /* Reset those iterator values set from display property values. */
4297 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4298 it->space_width = Qnil;
4299 it->font_height = Qnil;
4300 it->voffset = 0;
4301
4302 /* We don't support recursive `display' properties, i.e. string
4303 values that have a string `display' property, that have a string
4304 `display' property etc. */
4305 if (!it->string_from_display_prop_p)
4306 it->area = TEXT_AREA;
4307
4308 propval = get_char_property_and_overlay (make_number (position->charpos),
4309 Qdisplay, object, &overlay);
4310 if (NILP (propval))
4311 return HANDLED_NORMALLY;
4312 /* Now OVERLAY is the overlay that gave us this property, or nil
4313 if it was a text property. */
4314
4315 if (!STRINGP (it->string))
4316 object = it->w->buffer;
4317
4318 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4319 position, bufpos,
4320 FRAME_WINDOW_P (it->f));
4321
4322 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4323 }
4324
4325 /* Subroutine of handle_display_prop. Returns non-zero if the display
4326 specification in SPEC is a replacing specification, i.e. it would
4327 replace the text covered by `display' property with something else,
4328 such as an image or a display string. If SPEC includes any kind or
4329 `(space ...) specification, the value is 2; this is used by
4330 compute_display_string_pos, which see.
4331
4332 See handle_single_display_spec for documentation of arguments.
4333 frame_window_p is non-zero if the window being redisplayed is on a
4334 GUI frame; this argument is used only if IT is NULL, see below.
4335
4336 IT can be NULL, if this is called by the bidi reordering code
4337 through compute_display_string_pos, which see. In that case, this
4338 function only examines SPEC, but does not otherwise "handle" it, in
4339 the sense that it doesn't set up members of IT from the display
4340 spec. */
4341 static int
4342 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4343 Lisp_Object overlay, struct text_pos *position,
4344 ptrdiff_t bufpos, int frame_window_p)
4345 {
4346 int replacing_p = 0;
4347 int rv;
4348
4349 if (CONSP (spec)
4350 /* Simple specifications. */
4351 && !EQ (XCAR (spec), Qimage)
4352 && !EQ (XCAR (spec), Qspace)
4353 && !EQ (XCAR (spec), Qwhen)
4354 && !EQ (XCAR (spec), Qslice)
4355 && !EQ (XCAR (spec), Qspace_width)
4356 && !EQ (XCAR (spec), Qheight)
4357 && !EQ (XCAR (spec), Qraise)
4358 /* Marginal area specifications. */
4359 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4360 && !EQ (XCAR (spec), Qleft_fringe)
4361 && !EQ (XCAR (spec), Qright_fringe)
4362 && !NILP (XCAR (spec)))
4363 {
4364 for (; CONSP (spec); spec = XCDR (spec))
4365 {
4366 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4367 overlay, position, bufpos,
4368 replacing_p, frame_window_p)))
4369 {
4370 replacing_p = rv;
4371 /* If some text in a string is replaced, `position' no
4372 longer points to the position of `object'. */
4373 if (!it || STRINGP (object))
4374 break;
4375 }
4376 }
4377 }
4378 else if (VECTORP (spec))
4379 {
4380 ptrdiff_t i;
4381 for (i = 0; i < ASIZE (spec); ++i)
4382 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4383 overlay, position, bufpos,
4384 replacing_p, frame_window_p)))
4385 {
4386 replacing_p = rv;
4387 /* If some text in a string is replaced, `position' no
4388 longer points to the position of `object'. */
4389 if (!it || STRINGP (object))
4390 break;
4391 }
4392 }
4393 else
4394 {
4395 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4396 position, bufpos, 0,
4397 frame_window_p)))
4398 replacing_p = rv;
4399 }
4400
4401 return replacing_p;
4402 }
4403
4404 /* Value is the position of the end of the `display' property starting
4405 at START_POS in OBJECT. */
4406
4407 static struct text_pos
4408 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4409 {
4410 Lisp_Object end;
4411 struct text_pos end_pos;
4412
4413 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4414 Qdisplay, object, Qnil);
4415 CHARPOS (end_pos) = XFASTINT (end);
4416 if (STRINGP (object))
4417 compute_string_pos (&end_pos, start_pos, it->string);
4418 else
4419 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4420
4421 return end_pos;
4422 }
4423
4424
4425 /* Set up IT from a single `display' property specification SPEC. OBJECT
4426 is the object in which the `display' property was found. *POSITION
4427 is the position in OBJECT at which the `display' property was found.
4428 BUFPOS is the buffer position of OBJECT (different from POSITION if
4429 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4430 previously saw a display specification which already replaced text
4431 display with something else, for example an image; we ignore such
4432 properties after the first one has been processed.
4433
4434 OVERLAY is the overlay this `display' property came from,
4435 or nil if it was a text property.
4436
4437 If SPEC is a `space' or `image' specification, and in some other
4438 cases too, set *POSITION to the position where the `display'
4439 property ends.
4440
4441 If IT is NULL, only examine the property specification in SPEC, but
4442 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4443 is intended to be displayed in a window on a GUI frame.
4444
4445 Value is non-zero if something was found which replaces the display
4446 of buffer or string text. */
4447
4448 static int
4449 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4450 Lisp_Object overlay, struct text_pos *position,
4451 ptrdiff_t bufpos, int display_replaced_p,
4452 int frame_window_p)
4453 {
4454 Lisp_Object form;
4455 Lisp_Object location, value;
4456 struct text_pos start_pos = *position;
4457 int valid_p;
4458
4459 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4460 If the result is non-nil, use VALUE instead of SPEC. */
4461 form = Qt;
4462 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4463 {
4464 spec = XCDR (spec);
4465 if (!CONSP (spec))
4466 return 0;
4467 form = XCAR (spec);
4468 spec = XCDR (spec);
4469 }
4470
4471 if (!NILP (form) && !EQ (form, Qt))
4472 {
4473 ptrdiff_t count = SPECPDL_INDEX ();
4474 struct gcpro gcpro1;
4475
4476 /* Bind `object' to the object having the `display' property, a
4477 buffer or string. Bind `position' to the position in the
4478 object where the property was found, and `buffer-position'
4479 to the current position in the buffer. */
4480
4481 if (NILP (object))
4482 XSETBUFFER (object, current_buffer);
4483 specbind (Qobject, object);
4484 specbind (Qposition, make_number (CHARPOS (*position)));
4485 specbind (Qbuffer_position, make_number (bufpos));
4486 GCPRO1 (form);
4487 form = safe_eval (form);
4488 UNGCPRO;
4489 unbind_to (count, Qnil);
4490 }
4491
4492 if (NILP (form))
4493 return 0;
4494
4495 /* Handle `(height HEIGHT)' specifications. */
4496 if (CONSP (spec)
4497 && EQ (XCAR (spec), Qheight)
4498 && CONSP (XCDR (spec)))
4499 {
4500 if (it)
4501 {
4502 if (!FRAME_WINDOW_P (it->f))
4503 return 0;
4504
4505 it->font_height = XCAR (XCDR (spec));
4506 if (!NILP (it->font_height))
4507 {
4508 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4509 int new_height = -1;
4510
4511 if (CONSP (it->font_height)
4512 && (EQ (XCAR (it->font_height), Qplus)
4513 || EQ (XCAR (it->font_height), Qminus))
4514 && CONSP (XCDR (it->font_height))
4515 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4516 {
4517 /* `(+ N)' or `(- N)' where N is an integer. */
4518 int steps = XINT (XCAR (XCDR (it->font_height)));
4519 if (EQ (XCAR (it->font_height), Qplus))
4520 steps = - steps;
4521 it->face_id = smaller_face (it->f, it->face_id, steps);
4522 }
4523 else if (FUNCTIONP (it->font_height))
4524 {
4525 /* Call function with current height as argument.
4526 Value is the new height. */
4527 Lisp_Object height;
4528 height = safe_call1 (it->font_height,
4529 face->lface[LFACE_HEIGHT_INDEX]);
4530 if (NUMBERP (height))
4531 new_height = XFLOATINT (height);
4532 }
4533 else if (NUMBERP (it->font_height))
4534 {
4535 /* Value is a multiple of the canonical char height. */
4536 struct face *f;
4537
4538 f = FACE_FROM_ID (it->f,
4539 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4540 new_height = (XFLOATINT (it->font_height)
4541 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4542 }
4543 else
4544 {
4545 /* Evaluate IT->font_height with `height' bound to the
4546 current specified height to get the new height. */
4547 ptrdiff_t count = SPECPDL_INDEX ();
4548
4549 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4550 value = safe_eval (it->font_height);
4551 unbind_to (count, Qnil);
4552
4553 if (NUMBERP (value))
4554 new_height = XFLOATINT (value);
4555 }
4556
4557 if (new_height > 0)
4558 it->face_id = face_with_height (it->f, it->face_id, new_height);
4559 }
4560 }
4561
4562 return 0;
4563 }
4564
4565 /* Handle `(space-width WIDTH)'. */
4566 if (CONSP (spec)
4567 && EQ (XCAR (spec), Qspace_width)
4568 && CONSP (XCDR (spec)))
4569 {
4570 if (it)
4571 {
4572 if (!FRAME_WINDOW_P (it->f))
4573 return 0;
4574
4575 value = XCAR (XCDR (spec));
4576 if (NUMBERP (value) && XFLOATINT (value) > 0)
4577 it->space_width = value;
4578 }
4579
4580 return 0;
4581 }
4582
4583 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4584 if (CONSP (spec)
4585 && EQ (XCAR (spec), Qslice))
4586 {
4587 Lisp_Object tem;
4588
4589 if (it)
4590 {
4591 if (!FRAME_WINDOW_P (it->f))
4592 return 0;
4593
4594 if (tem = XCDR (spec), CONSP (tem))
4595 {
4596 it->slice.x = XCAR (tem);
4597 if (tem = XCDR (tem), CONSP (tem))
4598 {
4599 it->slice.y = XCAR (tem);
4600 if (tem = XCDR (tem), CONSP (tem))
4601 {
4602 it->slice.width = XCAR (tem);
4603 if (tem = XCDR (tem), CONSP (tem))
4604 it->slice.height = XCAR (tem);
4605 }
4606 }
4607 }
4608 }
4609
4610 return 0;
4611 }
4612
4613 /* Handle `(raise FACTOR)'. */
4614 if (CONSP (spec)
4615 && EQ (XCAR (spec), Qraise)
4616 && CONSP (XCDR (spec)))
4617 {
4618 if (it)
4619 {
4620 if (!FRAME_WINDOW_P (it->f))
4621 return 0;
4622
4623 #ifdef HAVE_WINDOW_SYSTEM
4624 value = XCAR (XCDR (spec));
4625 if (NUMBERP (value))
4626 {
4627 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4628 it->voffset = - (XFLOATINT (value)
4629 * (FONT_HEIGHT (face->font)));
4630 }
4631 #endif /* HAVE_WINDOW_SYSTEM */
4632 }
4633
4634 return 0;
4635 }
4636
4637 /* Don't handle the other kinds of display specifications
4638 inside a string that we got from a `display' property. */
4639 if (it && it->string_from_display_prop_p)
4640 return 0;
4641
4642 /* Characters having this form of property are not displayed, so
4643 we have to find the end of the property. */
4644 if (it)
4645 {
4646 start_pos = *position;
4647 *position = display_prop_end (it, object, start_pos);
4648 }
4649 value = Qnil;
4650
4651 /* Stop the scan at that end position--we assume that all
4652 text properties change there. */
4653 if (it)
4654 it->stop_charpos = position->charpos;
4655
4656 /* Handle `(left-fringe BITMAP [FACE])'
4657 and `(right-fringe BITMAP [FACE])'. */
4658 if (CONSP (spec)
4659 && (EQ (XCAR (spec), Qleft_fringe)
4660 || EQ (XCAR (spec), Qright_fringe))
4661 && CONSP (XCDR (spec)))
4662 {
4663 int fringe_bitmap;
4664
4665 if (it)
4666 {
4667 if (!FRAME_WINDOW_P (it->f))
4668 /* If we return here, POSITION has been advanced
4669 across the text with this property. */
4670 return 0;
4671 }
4672 else if (!frame_window_p)
4673 return 0;
4674
4675 #ifdef HAVE_WINDOW_SYSTEM
4676 value = XCAR (XCDR (spec));
4677 if (!SYMBOLP (value)
4678 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4679 /* If we return here, POSITION has been advanced
4680 across the text with this property. */
4681 return 0;
4682
4683 if (it)
4684 {
4685 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4686
4687 if (CONSP (XCDR (XCDR (spec))))
4688 {
4689 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4690 int face_id2 = lookup_derived_face (it->f, face_name,
4691 FRINGE_FACE_ID, 0);
4692 if (face_id2 >= 0)
4693 face_id = face_id2;
4694 }
4695
4696 /* Save current settings of IT so that we can restore them
4697 when we are finished with the glyph property value. */
4698 push_it (it, position);
4699
4700 it->area = TEXT_AREA;
4701 it->what = IT_IMAGE;
4702 it->image_id = -1; /* no image */
4703 it->position = start_pos;
4704 it->object = NILP (object) ? it->w->buffer : object;
4705 it->method = GET_FROM_IMAGE;
4706 it->from_overlay = Qnil;
4707 it->face_id = face_id;
4708 it->from_disp_prop_p = 1;
4709
4710 /* Say that we haven't consumed the characters with
4711 `display' property yet. The call to pop_it in
4712 set_iterator_to_next will clean this up. */
4713 *position = start_pos;
4714
4715 if (EQ (XCAR (spec), Qleft_fringe))
4716 {
4717 it->left_user_fringe_bitmap = fringe_bitmap;
4718 it->left_user_fringe_face_id = face_id;
4719 }
4720 else
4721 {
4722 it->right_user_fringe_bitmap = fringe_bitmap;
4723 it->right_user_fringe_face_id = face_id;
4724 }
4725 }
4726 #endif /* HAVE_WINDOW_SYSTEM */
4727 return 1;
4728 }
4729
4730 /* Prepare to handle `((margin left-margin) ...)',
4731 `((margin right-margin) ...)' and `((margin nil) ...)'
4732 prefixes for display specifications. */
4733 location = Qunbound;
4734 if (CONSP (spec) && CONSP (XCAR (spec)))
4735 {
4736 Lisp_Object tem;
4737
4738 value = XCDR (spec);
4739 if (CONSP (value))
4740 value = XCAR (value);
4741
4742 tem = XCAR (spec);
4743 if (EQ (XCAR (tem), Qmargin)
4744 && (tem = XCDR (tem),
4745 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4746 (NILP (tem)
4747 || EQ (tem, Qleft_margin)
4748 || EQ (tem, Qright_margin))))
4749 location = tem;
4750 }
4751
4752 if (EQ (location, Qunbound))
4753 {
4754 location = Qnil;
4755 value = spec;
4756 }
4757
4758 /* After this point, VALUE is the property after any
4759 margin prefix has been stripped. It must be a string,
4760 an image specification, or `(space ...)'.
4761
4762 LOCATION specifies where to display: `left-margin',
4763 `right-margin' or nil. */
4764
4765 valid_p = (STRINGP (value)
4766 #ifdef HAVE_WINDOW_SYSTEM
4767 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4768 && valid_image_p (value))
4769 #endif /* not HAVE_WINDOW_SYSTEM */
4770 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4771
4772 if (valid_p && !display_replaced_p)
4773 {
4774 int retval = 1;
4775
4776 if (!it)
4777 {
4778 /* Callers need to know whether the display spec is any kind
4779 of `(space ...)' spec that is about to affect text-area
4780 display. */
4781 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4782 retval = 2;
4783 return retval;
4784 }
4785
4786 /* Save current settings of IT so that we can restore them
4787 when we are finished with the glyph property value. */
4788 push_it (it, position);
4789 it->from_overlay = overlay;
4790 it->from_disp_prop_p = 1;
4791
4792 if (NILP (location))
4793 it->area = TEXT_AREA;
4794 else if (EQ (location, Qleft_margin))
4795 it->area = LEFT_MARGIN_AREA;
4796 else
4797 it->area = RIGHT_MARGIN_AREA;
4798
4799 if (STRINGP (value))
4800 {
4801 it->string = value;
4802 it->multibyte_p = STRING_MULTIBYTE (it->string);
4803 it->current.overlay_string_index = -1;
4804 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4805 it->end_charpos = it->string_nchars = SCHARS (it->string);
4806 it->method = GET_FROM_STRING;
4807 it->stop_charpos = 0;
4808 it->prev_stop = 0;
4809 it->base_level_stop = 0;
4810 it->string_from_display_prop_p = 1;
4811 /* Say that we haven't consumed the characters with
4812 `display' property yet. The call to pop_it in
4813 set_iterator_to_next will clean this up. */
4814 if (BUFFERP (object))
4815 *position = start_pos;
4816
4817 /* Force paragraph direction to be that of the parent
4818 object. If the parent object's paragraph direction is
4819 not yet determined, default to L2R. */
4820 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4821 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4822 else
4823 it->paragraph_embedding = L2R;
4824
4825 /* Set up the bidi iterator for this display string. */
4826 if (it->bidi_p)
4827 {
4828 it->bidi_it.string.lstring = it->string;
4829 it->bidi_it.string.s = NULL;
4830 it->bidi_it.string.schars = it->end_charpos;
4831 it->bidi_it.string.bufpos = bufpos;
4832 it->bidi_it.string.from_disp_str = 1;
4833 it->bidi_it.string.unibyte = !it->multibyte_p;
4834 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4835 }
4836 }
4837 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4838 {
4839 it->method = GET_FROM_STRETCH;
4840 it->object = value;
4841 *position = it->position = start_pos;
4842 retval = 1 + (it->area == TEXT_AREA);
4843 }
4844 #ifdef HAVE_WINDOW_SYSTEM
4845 else
4846 {
4847 it->what = IT_IMAGE;
4848 it->image_id = lookup_image (it->f, value);
4849 it->position = start_pos;
4850 it->object = NILP (object) ? it->w->buffer : object;
4851 it->method = GET_FROM_IMAGE;
4852
4853 /* Say that we haven't consumed the characters with
4854 `display' property yet. The call to pop_it in
4855 set_iterator_to_next will clean this up. */
4856 *position = start_pos;
4857 }
4858 #endif /* HAVE_WINDOW_SYSTEM */
4859
4860 return retval;
4861 }
4862
4863 /* Invalid property or property not supported. Restore
4864 POSITION to what it was before. */
4865 *position = start_pos;
4866 return 0;
4867 }
4868
4869 /* Check if PROP is a display property value whose text should be
4870 treated as intangible. OVERLAY is the overlay from which PROP
4871 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4872 specify the buffer position covered by PROP. */
4873
4874 int
4875 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4876 ptrdiff_t charpos, ptrdiff_t bytepos)
4877 {
4878 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4879 struct text_pos position;
4880
4881 SET_TEXT_POS (position, charpos, bytepos);
4882 return handle_display_spec (NULL, prop, Qnil, overlay,
4883 &position, charpos, frame_window_p);
4884 }
4885
4886
4887 /* Return 1 if PROP is a display sub-property value containing STRING.
4888
4889 Implementation note: this and the following function are really
4890 special cases of handle_display_spec and
4891 handle_single_display_spec, and should ideally use the same code.
4892 Until they do, these two pairs must be consistent and must be
4893 modified in sync. */
4894
4895 static int
4896 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4897 {
4898 if (EQ (string, prop))
4899 return 1;
4900
4901 /* Skip over `when FORM'. */
4902 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4903 {
4904 prop = XCDR (prop);
4905 if (!CONSP (prop))
4906 return 0;
4907 /* Actually, the condition following `when' should be eval'ed,
4908 like handle_single_display_spec does, and we should return
4909 zero if it evaluates to nil. However, this function is
4910 called only when the buffer was already displayed and some
4911 glyph in the glyph matrix was found to come from a display
4912 string. Therefore, the condition was already evaluated, and
4913 the result was non-nil, otherwise the display string wouldn't
4914 have been displayed and we would have never been called for
4915 this property. Thus, we can skip the evaluation and assume
4916 its result is non-nil. */
4917 prop = XCDR (prop);
4918 }
4919
4920 if (CONSP (prop))
4921 /* Skip over `margin LOCATION'. */
4922 if (EQ (XCAR (prop), Qmargin))
4923 {
4924 prop = XCDR (prop);
4925 if (!CONSP (prop))
4926 return 0;
4927
4928 prop = XCDR (prop);
4929 if (!CONSP (prop))
4930 return 0;
4931 }
4932
4933 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4934 }
4935
4936
4937 /* Return 1 if STRING appears in the `display' property PROP. */
4938
4939 static int
4940 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4941 {
4942 if (CONSP (prop)
4943 && !EQ (XCAR (prop), Qwhen)
4944 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4945 {
4946 /* A list of sub-properties. */
4947 while (CONSP (prop))
4948 {
4949 if (single_display_spec_string_p (XCAR (prop), string))
4950 return 1;
4951 prop = XCDR (prop);
4952 }
4953 }
4954 else if (VECTORP (prop))
4955 {
4956 /* A vector of sub-properties. */
4957 ptrdiff_t i;
4958 for (i = 0; i < ASIZE (prop); ++i)
4959 if (single_display_spec_string_p (AREF (prop, i), string))
4960 return 1;
4961 }
4962 else
4963 return single_display_spec_string_p (prop, string);
4964
4965 return 0;
4966 }
4967
4968 /* Look for STRING in overlays and text properties in the current
4969 buffer, between character positions FROM and TO (excluding TO).
4970 BACK_P non-zero means look back (in this case, TO is supposed to be
4971 less than FROM).
4972 Value is the first character position where STRING was found, or
4973 zero if it wasn't found before hitting TO.
4974
4975 This function may only use code that doesn't eval because it is
4976 called asynchronously from note_mouse_highlight. */
4977
4978 static ptrdiff_t
4979 string_buffer_position_lim (Lisp_Object string,
4980 ptrdiff_t from, ptrdiff_t to, int back_p)
4981 {
4982 Lisp_Object limit, prop, pos;
4983 int found = 0;
4984
4985 pos = make_number (max (from, BEGV));
4986
4987 if (!back_p) /* looking forward */
4988 {
4989 limit = make_number (min (to, ZV));
4990 while (!found && !EQ (pos, limit))
4991 {
4992 prop = Fget_char_property (pos, Qdisplay, Qnil);
4993 if (!NILP (prop) && display_prop_string_p (prop, string))
4994 found = 1;
4995 else
4996 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4997 limit);
4998 }
4999 }
5000 else /* looking back */
5001 {
5002 limit = make_number (max (to, BEGV));
5003 while (!found && !EQ (pos, limit))
5004 {
5005 prop = Fget_char_property (pos, Qdisplay, Qnil);
5006 if (!NILP (prop) && display_prop_string_p (prop, string))
5007 found = 1;
5008 else
5009 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5010 limit);
5011 }
5012 }
5013
5014 return found ? XINT (pos) : 0;
5015 }
5016
5017 /* Determine which buffer position in current buffer STRING comes from.
5018 AROUND_CHARPOS is an approximate position where it could come from.
5019 Value is the buffer position or 0 if it couldn't be determined.
5020
5021 This function is necessary because we don't record buffer positions
5022 in glyphs generated from strings (to keep struct glyph small).
5023 This function may only use code that doesn't eval because it is
5024 called asynchronously from note_mouse_highlight. */
5025
5026 static ptrdiff_t
5027 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5028 {
5029 const int MAX_DISTANCE = 1000;
5030 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5031 around_charpos + MAX_DISTANCE,
5032 0);
5033
5034 if (!found)
5035 found = string_buffer_position_lim (string, around_charpos,
5036 around_charpos - MAX_DISTANCE, 1);
5037 return found;
5038 }
5039
5040
5041 \f
5042 /***********************************************************************
5043 `composition' property
5044 ***********************************************************************/
5045
5046 /* Set up iterator IT from `composition' property at its current
5047 position. Called from handle_stop. */
5048
5049 static enum prop_handled
5050 handle_composition_prop (struct it *it)
5051 {
5052 Lisp_Object prop, string;
5053 ptrdiff_t pos, pos_byte, start, end;
5054
5055 if (STRINGP (it->string))
5056 {
5057 unsigned char *s;
5058
5059 pos = IT_STRING_CHARPOS (*it);
5060 pos_byte = IT_STRING_BYTEPOS (*it);
5061 string = it->string;
5062 s = SDATA (string) + pos_byte;
5063 it->c = STRING_CHAR (s);
5064 }
5065 else
5066 {
5067 pos = IT_CHARPOS (*it);
5068 pos_byte = IT_BYTEPOS (*it);
5069 string = Qnil;
5070 it->c = FETCH_CHAR (pos_byte);
5071 }
5072
5073 /* If there's a valid composition and point is not inside of the
5074 composition (in the case that the composition is from the current
5075 buffer), draw a glyph composed from the composition components. */
5076 if (find_composition (pos, -1, &start, &end, &prop, string)
5077 && COMPOSITION_VALID_P (start, end, prop)
5078 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5079 {
5080 if (start < pos)
5081 /* As we can't handle this situation (perhaps font-lock added
5082 a new composition), we just return here hoping that next
5083 redisplay will detect this composition much earlier. */
5084 return HANDLED_NORMALLY;
5085 if (start != pos)
5086 {
5087 if (STRINGP (it->string))
5088 pos_byte = string_char_to_byte (it->string, start);
5089 else
5090 pos_byte = CHAR_TO_BYTE (start);
5091 }
5092 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5093 prop, string);
5094
5095 if (it->cmp_it.id >= 0)
5096 {
5097 it->cmp_it.ch = -1;
5098 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5099 it->cmp_it.nglyphs = -1;
5100 }
5101 }
5102
5103 return HANDLED_NORMALLY;
5104 }
5105
5106
5107 \f
5108 /***********************************************************************
5109 Overlay strings
5110 ***********************************************************************/
5111
5112 /* The following structure is used to record overlay strings for
5113 later sorting in load_overlay_strings. */
5114
5115 struct overlay_entry
5116 {
5117 Lisp_Object overlay;
5118 Lisp_Object string;
5119 EMACS_INT priority;
5120 int after_string_p;
5121 };
5122
5123
5124 /* Set up iterator IT from overlay strings at its current position.
5125 Called from handle_stop. */
5126
5127 static enum prop_handled
5128 handle_overlay_change (struct it *it)
5129 {
5130 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5131 return HANDLED_RECOMPUTE_PROPS;
5132 else
5133 return HANDLED_NORMALLY;
5134 }
5135
5136
5137 /* Set up the next overlay string for delivery by IT, if there is an
5138 overlay string to deliver. Called by set_iterator_to_next when the
5139 end of the current overlay string is reached. If there are more
5140 overlay strings to display, IT->string and
5141 IT->current.overlay_string_index are set appropriately here.
5142 Otherwise IT->string is set to nil. */
5143
5144 static void
5145 next_overlay_string (struct it *it)
5146 {
5147 ++it->current.overlay_string_index;
5148 if (it->current.overlay_string_index == it->n_overlay_strings)
5149 {
5150 /* No more overlay strings. Restore IT's settings to what
5151 they were before overlay strings were processed, and
5152 continue to deliver from current_buffer. */
5153
5154 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5155 pop_it (it);
5156 xassert (it->sp > 0
5157 || (NILP (it->string)
5158 && it->method == GET_FROM_BUFFER
5159 && it->stop_charpos >= BEGV
5160 && it->stop_charpos <= it->end_charpos));
5161 it->current.overlay_string_index = -1;
5162 it->n_overlay_strings = 0;
5163 it->overlay_strings_charpos = -1;
5164 /* If there's an empty display string on the stack, pop the
5165 stack, to resync the bidi iterator with IT's position. Such
5166 empty strings are pushed onto the stack in
5167 get_overlay_strings_1. */
5168 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5169 pop_it (it);
5170
5171 /* If we're at the end of the buffer, record that we have
5172 processed the overlay strings there already, so that
5173 next_element_from_buffer doesn't try it again. */
5174 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5175 it->overlay_strings_at_end_processed_p = 1;
5176 }
5177 else
5178 {
5179 /* There are more overlay strings to process. If
5180 IT->current.overlay_string_index has advanced to a position
5181 where we must load IT->overlay_strings with more strings, do
5182 it. We must load at the IT->overlay_strings_charpos where
5183 IT->n_overlay_strings was originally computed; when invisible
5184 text is present, this might not be IT_CHARPOS (Bug#7016). */
5185 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5186
5187 if (it->current.overlay_string_index && i == 0)
5188 load_overlay_strings (it, it->overlay_strings_charpos);
5189
5190 /* Initialize IT to deliver display elements from the overlay
5191 string. */
5192 it->string = it->overlay_strings[i];
5193 it->multibyte_p = STRING_MULTIBYTE (it->string);
5194 SET_TEXT_POS (it->current.string_pos, 0, 0);
5195 it->method = GET_FROM_STRING;
5196 it->stop_charpos = 0;
5197 if (it->cmp_it.stop_pos >= 0)
5198 it->cmp_it.stop_pos = 0;
5199 it->prev_stop = 0;
5200 it->base_level_stop = 0;
5201
5202 /* Set up the bidi iterator for this overlay string. */
5203 if (it->bidi_p)
5204 {
5205 it->bidi_it.string.lstring = it->string;
5206 it->bidi_it.string.s = NULL;
5207 it->bidi_it.string.schars = SCHARS (it->string);
5208 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5209 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5210 it->bidi_it.string.unibyte = !it->multibyte_p;
5211 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5212 }
5213 }
5214
5215 CHECK_IT (it);
5216 }
5217
5218
5219 /* Compare two overlay_entry structures E1 and E2. Used as a
5220 comparison function for qsort in load_overlay_strings. Overlay
5221 strings for the same position are sorted so that
5222
5223 1. All after-strings come in front of before-strings, except
5224 when they come from the same overlay.
5225
5226 2. Within after-strings, strings are sorted so that overlay strings
5227 from overlays with higher priorities come first.
5228
5229 2. Within before-strings, strings are sorted so that overlay
5230 strings from overlays with higher priorities come last.
5231
5232 Value is analogous to strcmp. */
5233
5234
5235 static int
5236 compare_overlay_entries (const void *e1, const void *e2)
5237 {
5238 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5239 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5240 int result;
5241
5242 if (entry1->after_string_p != entry2->after_string_p)
5243 {
5244 /* Let after-strings appear in front of before-strings if
5245 they come from different overlays. */
5246 if (EQ (entry1->overlay, entry2->overlay))
5247 result = entry1->after_string_p ? 1 : -1;
5248 else
5249 result = entry1->after_string_p ? -1 : 1;
5250 }
5251 else if (entry1->priority != entry2->priority)
5252 {
5253 if (entry1->after_string_p)
5254 /* After-strings sorted in order of decreasing priority. */
5255 result = entry2->priority < entry1->priority ? -1 : 1;
5256 else
5257 /* Before-strings sorted in order of increasing priority. */
5258 result = entry1->priority < entry2->priority ? -1 : 1;
5259 }
5260 else
5261 result = 0;
5262
5263 return result;
5264 }
5265
5266
5267 /* Load the vector IT->overlay_strings with overlay strings from IT's
5268 current buffer position, or from CHARPOS if that is > 0. Set
5269 IT->n_overlays to the total number of overlay strings found.
5270
5271 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5272 a time. On entry into load_overlay_strings,
5273 IT->current.overlay_string_index gives the number of overlay
5274 strings that have already been loaded by previous calls to this
5275 function.
5276
5277 IT->add_overlay_start contains an additional overlay start
5278 position to consider for taking overlay strings from, if non-zero.
5279 This position comes into play when the overlay has an `invisible'
5280 property, and both before and after-strings. When we've skipped to
5281 the end of the overlay, because of its `invisible' property, we
5282 nevertheless want its before-string to appear.
5283 IT->add_overlay_start will contain the overlay start position
5284 in this case.
5285
5286 Overlay strings are sorted so that after-string strings come in
5287 front of before-string strings. Within before and after-strings,
5288 strings are sorted by overlay priority. See also function
5289 compare_overlay_entries. */
5290
5291 static void
5292 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5293 {
5294 Lisp_Object overlay, window, str, invisible;
5295 struct Lisp_Overlay *ov;
5296 ptrdiff_t start, end;
5297 ptrdiff_t size = 20;
5298 ptrdiff_t n = 0, i, j;
5299 int invis_p;
5300 struct overlay_entry *entries
5301 = (struct overlay_entry *) alloca (size * sizeof *entries);
5302 USE_SAFE_ALLOCA;
5303
5304 if (charpos <= 0)
5305 charpos = IT_CHARPOS (*it);
5306
5307 /* Append the overlay string STRING of overlay OVERLAY to vector
5308 `entries' which has size `size' and currently contains `n'
5309 elements. AFTER_P non-zero means STRING is an after-string of
5310 OVERLAY. */
5311 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5312 do \
5313 { \
5314 Lisp_Object priority; \
5315 \
5316 if (n == size) \
5317 { \
5318 struct overlay_entry *old = entries; \
5319 SAFE_NALLOCA (entries, 2, size); \
5320 memcpy (entries, old, size * sizeof *entries); \
5321 size *= 2; \
5322 } \
5323 \
5324 entries[n].string = (STRING); \
5325 entries[n].overlay = (OVERLAY); \
5326 priority = Foverlay_get ((OVERLAY), Qpriority); \
5327 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5328 entries[n].after_string_p = (AFTER_P); \
5329 ++n; \
5330 } \
5331 while (0)
5332
5333 /* Process overlay before the overlay center. */
5334 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5335 {
5336 XSETMISC (overlay, ov);
5337 xassert (OVERLAYP (overlay));
5338 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5339 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5340
5341 if (end < charpos)
5342 break;
5343
5344 /* Skip this overlay if it doesn't start or end at IT's current
5345 position. */
5346 if (end != charpos && start != charpos)
5347 continue;
5348
5349 /* Skip this overlay if it doesn't apply to IT->w. */
5350 window = Foverlay_get (overlay, Qwindow);
5351 if (WINDOWP (window) && XWINDOW (window) != it->w)
5352 continue;
5353
5354 /* If the text ``under'' the overlay is invisible, both before-
5355 and after-strings from this overlay are visible; start and
5356 end position are indistinguishable. */
5357 invisible = Foverlay_get (overlay, Qinvisible);
5358 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5359
5360 /* If overlay has a non-empty before-string, record it. */
5361 if ((start == charpos || (end == charpos && invis_p))
5362 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5363 && SCHARS (str))
5364 RECORD_OVERLAY_STRING (overlay, str, 0);
5365
5366 /* If overlay has a non-empty after-string, record it. */
5367 if ((end == charpos || (start == charpos && invis_p))
5368 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5369 && SCHARS (str))
5370 RECORD_OVERLAY_STRING (overlay, str, 1);
5371 }
5372
5373 /* Process overlays after the overlay center. */
5374 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5375 {
5376 XSETMISC (overlay, ov);
5377 xassert (OVERLAYP (overlay));
5378 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5379 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5380
5381 if (start > charpos)
5382 break;
5383
5384 /* Skip this overlay if it doesn't start or end at IT's current
5385 position. */
5386 if (end != charpos && start != charpos)
5387 continue;
5388
5389 /* Skip this overlay if it doesn't apply to IT->w. */
5390 window = Foverlay_get (overlay, Qwindow);
5391 if (WINDOWP (window) && XWINDOW (window) != it->w)
5392 continue;
5393
5394 /* If the text ``under'' the overlay is invisible, it has a zero
5395 dimension, and both before- and after-strings apply. */
5396 invisible = Foverlay_get (overlay, Qinvisible);
5397 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5398
5399 /* If overlay has a non-empty before-string, record it. */
5400 if ((start == charpos || (end == charpos && invis_p))
5401 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5402 && SCHARS (str))
5403 RECORD_OVERLAY_STRING (overlay, str, 0);
5404
5405 /* If overlay has a non-empty after-string, record it. */
5406 if ((end == charpos || (start == charpos && invis_p))
5407 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5408 && SCHARS (str))
5409 RECORD_OVERLAY_STRING (overlay, str, 1);
5410 }
5411
5412 #undef RECORD_OVERLAY_STRING
5413
5414 /* Sort entries. */
5415 if (n > 1)
5416 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5417
5418 /* Record number of overlay strings, and where we computed it. */
5419 it->n_overlay_strings = n;
5420 it->overlay_strings_charpos = charpos;
5421
5422 /* IT->current.overlay_string_index is the number of overlay strings
5423 that have already been consumed by IT. Copy some of the
5424 remaining overlay strings to IT->overlay_strings. */
5425 i = 0;
5426 j = it->current.overlay_string_index;
5427 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5428 {
5429 it->overlay_strings[i] = entries[j].string;
5430 it->string_overlays[i++] = entries[j++].overlay;
5431 }
5432
5433 CHECK_IT (it);
5434 SAFE_FREE ();
5435 }
5436
5437
5438 /* Get the first chunk of overlay strings at IT's current buffer
5439 position, or at CHARPOS if that is > 0. Value is non-zero if at
5440 least one overlay string was found. */
5441
5442 static int
5443 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5444 {
5445 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5446 process. This fills IT->overlay_strings with strings, and sets
5447 IT->n_overlay_strings to the total number of strings to process.
5448 IT->pos.overlay_string_index has to be set temporarily to zero
5449 because load_overlay_strings needs this; it must be set to -1
5450 when no overlay strings are found because a zero value would
5451 indicate a position in the first overlay string. */
5452 it->current.overlay_string_index = 0;
5453 load_overlay_strings (it, charpos);
5454
5455 /* If we found overlay strings, set up IT to deliver display
5456 elements from the first one. Otherwise set up IT to deliver
5457 from current_buffer. */
5458 if (it->n_overlay_strings)
5459 {
5460 /* Make sure we know settings in current_buffer, so that we can
5461 restore meaningful values when we're done with the overlay
5462 strings. */
5463 if (compute_stop_p)
5464 compute_stop_pos (it);
5465 xassert (it->face_id >= 0);
5466
5467 /* Save IT's settings. They are restored after all overlay
5468 strings have been processed. */
5469 xassert (!compute_stop_p || it->sp == 0);
5470
5471 /* When called from handle_stop, there might be an empty display
5472 string loaded. In that case, don't bother saving it. But
5473 don't use this optimization with the bidi iterator, since we
5474 need the corresponding pop_it call to resync the bidi
5475 iterator's position with IT's position, after we are done
5476 with the overlay strings. (The corresponding call to pop_it
5477 in case of an empty display string is in
5478 next_overlay_string.) */
5479 if (!(!it->bidi_p
5480 && STRINGP (it->string) && !SCHARS (it->string)))
5481 push_it (it, NULL);
5482
5483 /* Set up IT to deliver display elements from the first overlay
5484 string. */
5485 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5486 it->string = it->overlay_strings[0];
5487 it->from_overlay = Qnil;
5488 it->stop_charpos = 0;
5489 xassert (STRINGP (it->string));
5490 it->end_charpos = SCHARS (it->string);
5491 it->prev_stop = 0;
5492 it->base_level_stop = 0;
5493 it->multibyte_p = STRING_MULTIBYTE (it->string);
5494 it->method = GET_FROM_STRING;
5495 it->from_disp_prop_p = 0;
5496
5497 /* Force paragraph direction to be that of the parent
5498 buffer. */
5499 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5500 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5501 else
5502 it->paragraph_embedding = L2R;
5503
5504 /* Set up the bidi iterator for this overlay string. */
5505 if (it->bidi_p)
5506 {
5507 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5508
5509 it->bidi_it.string.lstring = it->string;
5510 it->bidi_it.string.s = NULL;
5511 it->bidi_it.string.schars = SCHARS (it->string);
5512 it->bidi_it.string.bufpos = pos;
5513 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5514 it->bidi_it.string.unibyte = !it->multibyte_p;
5515 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5516 }
5517 return 1;
5518 }
5519
5520 it->current.overlay_string_index = -1;
5521 return 0;
5522 }
5523
5524 static int
5525 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5526 {
5527 it->string = Qnil;
5528 it->method = GET_FROM_BUFFER;
5529
5530 (void) get_overlay_strings_1 (it, charpos, 1);
5531
5532 CHECK_IT (it);
5533
5534 /* Value is non-zero if we found at least one overlay string. */
5535 return STRINGP (it->string);
5536 }
5537
5538
5539 \f
5540 /***********************************************************************
5541 Saving and restoring state
5542 ***********************************************************************/
5543
5544 /* Save current settings of IT on IT->stack. Called, for example,
5545 before setting up IT for an overlay string, to be able to restore
5546 IT's settings to what they were after the overlay string has been
5547 processed. If POSITION is non-NULL, it is the position to save on
5548 the stack instead of IT->position. */
5549
5550 static void
5551 push_it (struct it *it, struct text_pos *position)
5552 {
5553 struct iterator_stack_entry *p;
5554
5555 xassert (it->sp < IT_STACK_SIZE);
5556 p = it->stack + it->sp;
5557
5558 p->stop_charpos = it->stop_charpos;
5559 p->prev_stop = it->prev_stop;
5560 p->base_level_stop = it->base_level_stop;
5561 p->cmp_it = it->cmp_it;
5562 xassert (it->face_id >= 0);
5563 p->face_id = it->face_id;
5564 p->string = it->string;
5565 p->method = it->method;
5566 p->from_overlay = it->from_overlay;
5567 switch (p->method)
5568 {
5569 case GET_FROM_IMAGE:
5570 p->u.image.object = it->object;
5571 p->u.image.image_id = it->image_id;
5572 p->u.image.slice = it->slice;
5573 break;
5574 case GET_FROM_STRETCH:
5575 p->u.stretch.object = it->object;
5576 break;
5577 }
5578 p->position = position ? *position : it->position;
5579 p->current = it->current;
5580 p->end_charpos = it->end_charpos;
5581 p->string_nchars = it->string_nchars;
5582 p->area = it->area;
5583 p->multibyte_p = it->multibyte_p;
5584 p->avoid_cursor_p = it->avoid_cursor_p;
5585 p->space_width = it->space_width;
5586 p->font_height = it->font_height;
5587 p->voffset = it->voffset;
5588 p->string_from_display_prop_p = it->string_from_display_prop_p;
5589 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5590 p->display_ellipsis_p = 0;
5591 p->line_wrap = it->line_wrap;
5592 p->bidi_p = it->bidi_p;
5593 p->paragraph_embedding = it->paragraph_embedding;
5594 p->from_disp_prop_p = it->from_disp_prop_p;
5595 ++it->sp;
5596
5597 /* Save the state of the bidi iterator as well. */
5598 if (it->bidi_p)
5599 bidi_push_it (&it->bidi_it);
5600 }
5601
5602 static void
5603 iterate_out_of_display_property (struct it *it)
5604 {
5605 int buffer_p = BUFFERP (it->object);
5606 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5607 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5608
5609 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5610
5611 /* Maybe initialize paragraph direction. If we are at the beginning
5612 of a new paragraph, next_element_from_buffer may not have a
5613 chance to do that. */
5614 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5615 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5616 /* prev_stop can be zero, so check against BEGV as well. */
5617 while (it->bidi_it.charpos >= bob
5618 && it->prev_stop <= it->bidi_it.charpos
5619 && it->bidi_it.charpos < CHARPOS (it->position)
5620 && it->bidi_it.charpos < eob)
5621 bidi_move_to_visually_next (&it->bidi_it);
5622 /* Record the stop_pos we just crossed, for when we cross it
5623 back, maybe. */
5624 if (it->bidi_it.charpos > CHARPOS (it->position))
5625 it->prev_stop = CHARPOS (it->position);
5626 /* If we ended up not where pop_it put us, resync IT's
5627 positional members with the bidi iterator. */
5628 if (it->bidi_it.charpos != CHARPOS (it->position))
5629 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5630 if (buffer_p)
5631 it->current.pos = it->position;
5632 else
5633 it->current.string_pos = it->position;
5634 }
5635
5636 /* Restore IT's settings from IT->stack. Called, for example, when no
5637 more overlay strings must be processed, and we return to delivering
5638 display elements from a buffer, or when the end of a string from a
5639 `display' property is reached and we return to delivering display
5640 elements from an overlay string, or from a buffer. */
5641
5642 static void
5643 pop_it (struct it *it)
5644 {
5645 struct iterator_stack_entry *p;
5646 int from_display_prop = it->from_disp_prop_p;
5647
5648 xassert (it->sp > 0);
5649 --it->sp;
5650 p = it->stack + it->sp;
5651 it->stop_charpos = p->stop_charpos;
5652 it->prev_stop = p->prev_stop;
5653 it->base_level_stop = p->base_level_stop;
5654 it->cmp_it = p->cmp_it;
5655 it->face_id = p->face_id;
5656 it->current = p->current;
5657 it->position = p->position;
5658 it->string = p->string;
5659 it->from_overlay = p->from_overlay;
5660 if (NILP (it->string))
5661 SET_TEXT_POS (it->current.string_pos, -1, -1);
5662 it->method = p->method;
5663 switch (it->method)
5664 {
5665 case GET_FROM_IMAGE:
5666 it->image_id = p->u.image.image_id;
5667 it->object = p->u.image.object;
5668 it->slice = p->u.image.slice;
5669 break;
5670 case GET_FROM_STRETCH:
5671 it->object = p->u.stretch.object;
5672 break;
5673 case GET_FROM_BUFFER:
5674 it->object = it->w->buffer;
5675 break;
5676 case GET_FROM_STRING:
5677 it->object = it->string;
5678 break;
5679 case GET_FROM_DISPLAY_VECTOR:
5680 if (it->s)
5681 it->method = GET_FROM_C_STRING;
5682 else if (STRINGP (it->string))
5683 it->method = GET_FROM_STRING;
5684 else
5685 {
5686 it->method = GET_FROM_BUFFER;
5687 it->object = it->w->buffer;
5688 }
5689 }
5690 it->end_charpos = p->end_charpos;
5691 it->string_nchars = p->string_nchars;
5692 it->area = p->area;
5693 it->multibyte_p = p->multibyte_p;
5694 it->avoid_cursor_p = p->avoid_cursor_p;
5695 it->space_width = p->space_width;
5696 it->font_height = p->font_height;
5697 it->voffset = p->voffset;
5698 it->string_from_display_prop_p = p->string_from_display_prop_p;
5699 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5700 it->line_wrap = p->line_wrap;
5701 it->bidi_p = p->bidi_p;
5702 it->paragraph_embedding = p->paragraph_embedding;
5703 it->from_disp_prop_p = p->from_disp_prop_p;
5704 if (it->bidi_p)
5705 {
5706 bidi_pop_it (&it->bidi_it);
5707 /* Bidi-iterate until we get out of the portion of text, if any,
5708 covered by a `display' text property or by an overlay with
5709 `display' property. (We cannot just jump there, because the
5710 internal coherency of the bidi iterator state can not be
5711 preserved across such jumps.) We also must determine the
5712 paragraph base direction if the overlay we just processed is
5713 at the beginning of a new paragraph. */
5714 if (from_display_prop
5715 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5716 iterate_out_of_display_property (it);
5717
5718 xassert ((BUFFERP (it->object)
5719 && IT_CHARPOS (*it) == it->bidi_it.charpos
5720 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5721 || (STRINGP (it->object)
5722 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5723 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5724 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5725 }
5726 }
5727
5728
5729 \f
5730 /***********************************************************************
5731 Moving over lines
5732 ***********************************************************************/
5733
5734 /* Set IT's current position to the previous line start. */
5735
5736 static void
5737 back_to_previous_line_start (struct it *it)
5738 {
5739 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5740 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5741 }
5742
5743
5744 /* Move IT to the next line start.
5745
5746 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5747 we skipped over part of the text (as opposed to moving the iterator
5748 continuously over the text). Otherwise, don't change the value
5749 of *SKIPPED_P.
5750
5751 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5752 iterator on the newline, if it was found.
5753
5754 Newlines may come from buffer text, overlay strings, or strings
5755 displayed via the `display' property. That's the reason we can't
5756 simply use find_next_newline_no_quit.
5757
5758 Note that this function may not skip over invisible text that is so
5759 because of text properties and immediately follows a newline. If
5760 it would, function reseat_at_next_visible_line_start, when called
5761 from set_iterator_to_next, would effectively make invisible
5762 characters following a newline part of the wrong glyph row, which
5763 leads to wrong cursor motion. */
5764
5765 static int
5766 forward_to_next_line_start (struct it *it, int *skipped_p,
5767 struct bidi_it *bidi_it_prev)
5768 {
5769 ptrdiff_t old_selective;
5770 int newline_found_p, n;
5771 const int MAX_NEWLINE_DISTANCE = 500;
5772
5773 /* If already on a newline, just consume it to avoid unintended
5774 skipping over invisible text below. */
5775 if (it->what == IT_CHARACTER
5776 && it->c == '\n'
5777 && CHARPOS (it->position) == IT_CHARPOS (*it))
5778 {
5779 if (it->bidi_p && bidi_it_prev)
5780 *bidi_it_prev = it->bidi_it;
5781 set_iterator_to_next (it, 0);
5782 it->c = 0;
5783 return 1;
5784 }
5785
5786 /* Don't handle selective display in the following. It's (a)
5787 unnecessary because it's done by the caller, and (b) leads to an
5788 infinite recursion because next_element_from_ellipsis indirectly
5789 calls this function. */
5790 old_selective = it->selective;
5791 it->selective = 0;
5792
5793 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5794 from buffer text. */
5795 for (n = newline_found_p = 0;
5796 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5797 n += STRINGP (it->string) ? 0 : 1)
5798 {
5799 if (!get_next_display_element (it))
5800 return 0;
5801 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5802 if (newline_found_p && it->bidi_p && bidi_it_prev)
5803 *bidi_it_prev = it->bidi_it;
5804 set_iterator_to_next (it, 0);
5805 }
5806
5807 /* If we didn't find a newline near enough, see if we can use a
5808 short-cut. */
5809 if (!newline_found_p)
5810 {
5811 ptrdiff_t start = IT_CHARPOS (*it);
5812 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5813 Lisp_Object pos;
5814
5815 xassert (!STRINGP (it->string));
5816
5817 /* If there isn't any `display' property in sight, and no
5818 overlays, we can just use the position of the newline in
5819 buffer text. */
5820 if (it->stop_charpos >= limit
5821 || ((pos = Fnext_single_property_change (make_number (start),
5822 Qdisplay, Qnil,
5823 make_number (limit)),
5824 NILP (pos))
5825 && next_overlay_change (start) == ZV))
5826 {
5827 if (!it->bidi_p)
5828 {
5829 IT_CHARPOS (*it) = limit;
5830 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5831 }
5832 else
5833 {
5834 struct bidi_it bprev;
5835
5836 /* Help bidi.c avoid expensive searches for display
5837 properties and overlays, by telling it that there are
5838 none up to `limit'. */
5839 if (it->bidi_it.disp_pos < limit)
5840 {
5841 it->bidi_it.disp_pos = limit;
5842 it->bidi_it.disp_prop = 0;
5843 }
5844 do {
5845 bprev = it->bidi_it;
5846 bidi_move_to_visually_next (&it->bidi_it);
5847 } while (it->bidi_it.charpos != limit);
5848 IT_CHARPOS (*it) = limit;
5849 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5850 if (bidi_it_prev)
5851 *bidi_it_prev = bprev;
5852 }
5853 *skipped_p = newline_found_p = 1;
5854 }
5855 else
5856 {
5857 while (get_next_display_element (it)
5858 && !newline_found_p)
5859 {
5860 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5861 if (newline_found_p && it->bidi_p && bidi_it_prev)
5862 *bidi_it_prev = it->bidi_it;
5863 set_iterator_to_next (it, 0);
5864 }
5865 }
5866 }
5867
5868 it->selective = old_selective;
5869 return newline_found_p;
5870 }
5871
5872
5873 /* Set IT's current position to the previous visible line start. Skip
5874 invisible text that is so either due to text properties or due to
5875 selective display. Caution: this does not change IT->current_x and
5876 IT->hpos. */
5877
5878 static void
5879 back_to_previous_visible_line_start (struct it *it)
5880 {
5881 while (IT_CHARPOS (*it) > BEGV)
5882 {
5883 back_to_previous_line_start (it);
5884
5885 if (IT_CHARPOS (*it) <= BEGV)
5886 break;
5887
5888 /* If selective > 0, then lines indented more than its value are
5889 invisible. */
5890 if (it->selective > 0
5891 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5892 it->selective))
5893 continue;
5894
5895 /* Check the newline before point for invisibility. */
5896 {
5897 Lisp_Object prop;
5898 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5899 Qinvisible, it->window);
5900 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5901 continue;
5902 }
5903
5904 if (IT_CHARPOS (*it) <= BEGV)
5905 break;
5906
5907 {
5908 struct it it2;
5909 void *it2data = NULL;
5910 ptrdiff_t pos;
5911 ptrdiff_t beg, end;
5912 Lisp_Object val, overlay;
5913
5914 SAVE_IT (it2, *it, it2data);
5915
5916 /* If newline is part of a composition, continue from start of composition */
5917 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5918 && beg < IT_CHARPOS (*it))
5919 goto replaced;
5920
5921 /* If newline is replaced by a display property, find start of overlay
5922 or interval and continue search from that point. */
5923 pos = --IT_CHARPOS (it2);
5924 --IT_BYTEPOS (it2);
5925 it2.sp = 0;
5926 bidi_unshelve_cache (NULL, 0);
5927 it2.string_from_display_prop_p = 0;
5928 it2.from_disp_prop_p = 0;
5929 if (handle_display_prop (&it2) == HANDLED_RETURN
5930 && !NILP (val = get_char_property_and_overlay
5931 (make_number (pos), Qdisplay, Qnil, &overlay))
5932 && (OVERLAYP (overlay)
5933 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5934 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5935 {
5936 RESTORE_IT (it, it, it2data);
5937 goto replaced;
5938 }
5939
5940 /* Newline is not replaced by anything -- so we are done. */
5941 RESTORE_IT (it, it, it2data);
5942 break;
5943
5944 replaced:
5945 if (beg < BEGV)
5946 beg = BEGV;
5947 IT_CHARPOS (*it) = beg;
5948 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5949 }
5950 }
5951
5952 it->continuation_lines_width = 0;
5953
5954 xassert (IT_CHARPOS (*it) >= BEGV);
5955 xassert (IT_CHARPOS (*it) == BEGV
5956 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5957 CHECK_IT (it);
5958 }
5959
5960
5961 /* Reseat iterator IT at the previous visible line start. Skip
5962 invisible text that is so either due to text properties or due to
5963 selective display. At the end, update IT's overlay information,
5964 face information etc. */
5965
5966 void
5967 reseat_at_previous_visible_line_start (struct it *it)
5968 {
5969 back_to_previous_visible_line_start (it);
5970 reseat (it, it->current.pos, 1);
5971 CHECK_IT (it);
5972 }
5973
5974
5975 /* Reseat iterator IT on the next visible line start in the current
5976 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5977 preceding the line start. Skip over invisible text that is so
5978 because of selective display. Compute faces, overlays etc at the
5979 new position. Note that this function does not skip over text that
5980 is invisible because of text properties. */
5981
5982 static void
5983 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5984 {
5985 int newline_found_p, skipped_p = 0;
5986 struct bidi_it bidi_it_prev;
5987
5988 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5989
5990 /* Skip over lines that are invisible because they are indented
5991 more than the value of IT->selective. */
5992 if (it->selective > 0)
5993 while (IT_CHARPOS (*it) < ZV
5994 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5995 it->selective))
5996 {
5997 xassert (IT_BYTEPOS (*it) == BEGV
5998 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5999 newline_found_p =
6000 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6001 }
6002
6003 /* Position on the newline if that's what's requested. */
6004 if (on_newline_p && newline_found_p)
6005 {
6006 if (STRINGP (it->string))
6007 {
6008 if (IT_STRING_CHARPOS (*it) > 0)
6009 {
6010 if (!it->bidi_p)
6011 {
6012 --IT_STRING_CHARPOS (*it);
6013 --IT_STRING_BYTEPOS (*it);
6014 }
6015 else
6016 {
6017 /* We need to restore the bidi iterator to the state
6018 it had on the newline, and resync the IT's
6019 position with that. */
6020 it->bidi_it = bidi_it_prev;
6021 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6022 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6023 }
6024 }
6025 }
6026 else if (IT_CHARPOS (*it) > BEGV)
6027 {
6028 if (!it->bidi_p)
6029 {
6030 --IT_CHARPOS (*it);
6031 --IT_BYTEPOS (*it);
6032 }
6033 else
6034 {
6035 /* We need to restore the bidi iterator to the state it
6036 had on the newline and resync IT with that. */
6037 it->bidi_it = bidi_it_prev;
6038 IT_CHARPOS (*it) = it->bidi_it.charpos;
6039 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6040 }
6041 reseat (it, it->current.pos, 0);
6042 }
6043 }
6044 else if (skipped_p)
6045 reseat (it, it->current.pos, 0);
6046
6047 CHECK_IT (it);
6048 }
6049
6050
6051 \f
6052 /***********************************************************************
6053 Changing an iterator's position
6054 ***********************************************************************/
6055
6056 /* Change IT's current position to POS in current_buffer. If FORCE_P
6057 is non-zero, always check for text properties at the new position.
6058 Otherwise, text properties are only looked up if POS >=
6059 IT->check_charpos of a property. */
6060
6061 static void
6062 reseat (struct it *it, struct text_pos pos, int force_p)
6063 {
6064 ptrdiff_t original_pos = IT_CHARPOS (*it);
6065
6066 reseat_1 (it, pos, 0);
6067
6068 /* Determine where to check text properties. Avoid doing it
6069 where possible because text property lookup is very expensive. */
6070 if (force_p
6071 || CHARPOS (pos) > it->stop_charpos
6072 || CHARPOS (pos) < original_pos)
6073 {
6074 if (it->bidi_p)
6075 {
6076 /* For bidi iteration, we need to prime prev_stop and
6077 base_level_stop with our best estimations. */
6078 /* Implementation note: Of course, POS is not necessarily a
6079 stop position, so assigning prev_pos to it is a lie; we
6080 should have called compute_stop_backwards. However, if
6081 the current buffer does not include any R2L characters,
6082 that call would be a waste of cycles, because the
6083 iterator will never move back, and thus never cross this
6084 "fake" stop position. So we delay that backward search
6085 until the time we really need it, in next_element_from_buffer. */
6086 if (CHARPOS (pos) != it->prev_stop)
6087 it->prev_stop = CHARPOS (pos);
6088 if (CHARPOS (pos) < it->base_level_stop)
6089 it->base_level_stop = 0; /* meaning it's unknown */
6090 handle_stop (it);
6091 }
6092 else
6093 {
6094 handle_stop (it);
6095 it->prev_stop = it->base_level_stop = 0;
6096 }
6097
6098 }
6099
6100 CHECK_IT (it);
6101 }
6102
6103
6104 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6105 IT->stop_pos to POS, also. */
6106
6107 static void
6108 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6109 {
6110 /* Don't call this function when scanning a C string. */
6111 xassert (it->s == NULL);
6112
6113 /* POS must be a reasonable value. */
6114 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6115
6116 it->current.pos = it->position = pos;
6117 it->end_charpos = ZV;
6118 it->dpvec = NULL;
6119 it->current.dpvec_index = -1;
6120 it->current.overlay_string_index = -1;
6121 IT_STRING_CHARPOS (*it) = -1;
6122 IT_STRING_BYTEPOS (*it) = -1;
6123 it->string = Qnil;
6124 it->method = GET_FROM_BUFFER;
6125 it->object = it->w->buffer;
6126 it->area = TEXT_AREA;
6127 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6128 it->sp = 0;
6129 it->string_from_display_prop_p = 0;
6130 it->string_from_prefix_prop_p = 0;
6131
6132 it->from_disp_prop_p = 0;
6133 it->face_before_selective_p = 0;
6134 if (it->bidi_p)
6135 {
6136 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6137 &it->bidi_it);
6138 bidi_unshelve_cache (NULL, 0);
6139 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6140 it->bidi_it.string.s = NULL;
6141 it->bidi_it.string.lstring = Qnil;
6142 it->bidi_it.string.bufpos = 0;
6143 it->bidi_it.string.unibyte = 0;
6144 }
6145
6146 if (set_stop_p)
6147 {
6148 it->stop_charpos = CHARPOS (pos);
6149 it->base_level_stop = CHARPOS (pos);
6150 }
6151 }
6152
6153
6154 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6155 If S is non-null, it is a C string to iterate over. Otherwise,
6156 STRING gives a Lisp string to iterate over.
6157
6158 If PRECISION > 0, don't return more then PRECISION number of
6159 characters from the string.
6160
6161 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6162 characters have been returned. FIELD_WIDTH < 0 means an infinite
6163 field width.
6164
6165 MULTIBYTE = 0 means disable processing of multibyte characters,
6166 MULTIBYTE > 0 means enable it,
6167 MULTIBYTE < 0 means use IT->multibyte_p.
6168
6169 IT must be initialized via a prior call to init_iterator before
6170 calling this function. */
6171
6172 static void
6173 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6174 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6175 int multibyte)
6176 {
6177 /* No region in strings. */
6178 it->region_beg_charpos = it->region_end_charpos = -1;
6179
6180 /* No text property checks performed by default, but see below. */
6181 it->stop_charpos = -1;
6182
6183 /* Set iterator position and end position. */
6184 memset (&it->current, 0, sizeof it->current);
6185 it->current.overlay_string_index = -1;
6186 it->current.dpvec_index = -1;
6187 xassert (charpos >= 0);
6188
6189 /* If STRING is specified, use its multibyteness, otherwise use the
6190 setting of MULTIBYTE, if specified. */
6191 if (multibyte >= 0)
6192 it->multibyte_p = multibyte > 0;
6193
6194 /* Bidirectional reordering of strings is controlled by the default
6195 value of bidi-display-reordering. Don't try to reorder while
6196 loading loadup.el, as the necessary character property tables are
6197 not yet available. */
6198 it->bidi_p =
6199 NILP (Vpurify_flag)
6200 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6201
6202 if (s == NULL)
6203 {
6204 xassert (STRINGP (string));
6205 it->string = string;
6206 it->s = NULL;
6207 it->end_charpos = it->string_nchars = SCHARS (string);
6208 it->method = GET_FROM_STRING;
6209 it->current.string_pos = string_pos (charpos, string);
6210
6211 if (it->bidi_p)
6212 {
6213 it->bidi_it.string.lstring = string;
6214 it->bidi_it.string.s = NULL;
6215 it->bidi_it.string.schars = it->end_charpos;
6216 it->bidi_it.string.bufpos = 0;
6217 it->bidi_it.string.from_disp_str = 0;
6218 it->bidi_it.string.unibyte = !it->multibyte_p;
6219 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6220 FRAME_WINDOW_P (it->f), &it->bidi_it);
6221 }
6222 }
6223 else
6224 {
6225 it->s = (const unsigned char *) s;
6226 it->string = Qnil;
6227
6228 /* Note that we use IT->current.pos, not it->current.string_pos,
6229 for displaying C strings. */
6230 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6231 if (it->multibyte_p)
6232 {
6233 it->current.pos = c_string_pos (charpos, s, 1);
6234 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6235 }
6236 else
6237 {
6238 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6239 it->end_charpos = it->string_nchars = strlen (s);
6240 }
6241
6242 if (it->bidi_p)
6243 {
6244 it->bidi_it.string.lstring = Qnil;
6245 it->bidi_it.string.s = (const unsigned char *) s;
6246 it->bidi_it.string.schars = it->end_charpos;
6247 it->bidi_it.string.bufpos = 0;
6248 it->bidi_it.string.from_disp_str = 0;
6249 it->bidi_it.string.unibyte = !it->multibyte_p;
6250 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6251 &it->bidi_it);
6252 }
6253 it->method = GET_FROM_C_STRING;
6254 }
6255
6256 /* PRECISION > 0 means don't return more than PRECISION characters
6257 from the string. */
6258 if (precision > 0 && it->end_charpos - charpos > precision)
6259 {
6260 it->end_charpos = it->string_nchars = charpos + precision;
6261 if (it->bidi_p)
6262 it->bidi_it.string.schars = it->end_charpos;
6263 }
6264
6265 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6266 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6267 FIELD_WIDTH < 0 means infinite field width. This is useful for
6268 padding with `-' at the end of a mode line. */
6269 if (field_width < 0)
6270 field_width = INFINITY;
6271 /* Implementation note: We deliberately don't enlarge
6272 it->bidi_it.string.schars here to fit it->end_charpos, because
6273 the bidi iterator cannot produce characters out of thin air. */
6274 if (field_width > it->end_charpos - charpos)
6275 it->end_charpos = charpos + field_width;
6276
6277 /* Use the standard display table for displaying strings. */
6278 if (DISP_TABLE_P (Vstandard_display_table))
6279 it->dp = XCHAR_TABLE (Vstandard_display_table);
6280
6281 it->stop_charpos = charpos;
6282 it->prev_stop = charpos;
6283 it->base_level_stop = 0;
6284 if (it->bidi_p)
6285 {
6286 it->bidi_it.first_elt = 1;
6287 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6288 it->bidi_it.disp_pos = -1;
6289 }
6290 if (s == NULL && it->multibyte_p)
6291 {
6292 ptrdiff_t endpos = SCHARS (it->string);
6293 if (endpos > it->end_charpos)
6294 endpos = it->end_charpos;
6295 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6296 it->string);
6297 }
6298 CHECK_IT (it);
6299 }
6300
6301
6302 \f
6303 /***********************************************************************
6304 Iteration
6305 ***********************************************************************/
6306
6307 /* Map enum it_method value to corresponding next_element_from_* function. */
6308
6309 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6310 {
6311 next_element_from_buffer,
6312 next_element_from_display_vector,
6313 next_element_from_string,
6314 next_element_from_c_string,
6315 next_element_from_image,
6316 next_element_from_stretch
6317 };
6318
6319 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6320
6321
6322 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6323 (possibly with the following characters). */
6324
6325 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6326 ((IT)->cmp_it.id >= 0 \
6327 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6328 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6329 END_CHARPOS, (IT)->w, \
6330 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6331 (IT)->string)))
6332
6333
6334 /* Lookup the char-table Vglyphless_char_display for character C (-1
6335 if we want information for no-font case), and return the display
6336 method symbol. By side-effect, update it->what and
6337 it->glyphless_method. This function is called from
6338 get_next_display_element for each character element, and from
6339 x_produce_glyphs when no suitable font was found. */
6340
6341 Lisp_Object
6342 lookup_glyphless_char_display (int c, struct it *it)
6343 {
6344 Lisp_Object glyphless_method = Qnil;
6345
6346 if (CHAR_TABLE_P (Vglyphless_char_display)
6347 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6348 {
6349 if (c >= 0)
6350 {
6351 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6352 if (CONSP (glyphless_method))
6353 glyphless_method = FRAME_WINDOW_P (it->f)
6354 ? XCAR (glyphless_method)
6355 : XCDR (glyphless_method);
6356 }
6357 else
6358 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6359 }
6360
6361 retry:
6362 if (NILP (glyphless_method))
6363 {
6364 if (c >= 0)
6365 /* The default is to display the character by a proper font. */
6366 return Qnil;
6367 /* The default for the no-font case is to display an empty box. */
6368 glyphless_method = Qempty_box;
6369 }
6370 if (EQ (glyphless_method, Qzero_width))
6371 {
6372 if (c >= 0)
6373 return glyphless_method;
6374 /* This method can't be used for the no-font case. */
6375 glyphless_method = Qempty_box;
6376 }
6377 if (EQ (glyphless_method, Qthin_space))
6378 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6379 else if (EQ (glyphless_method, Qempty_box))
6380 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6381 else if (EQ (glyphless_method, Qhex_code))
6382 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6383 else if (STRINGP (glyphless_method))
6384 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6385 else
6386 {
6387 /* Invalid value. We use the default method. */
6388 glyphless_method = Qnil;
6389 goto retry;
6390 }
6391 it->what = IT_GLYPHLESS;
6392 return glyphless_method;
6393 }
6394
6395 /* Load IT's display element fields with information about the next
6396 display element from the current position of IT. Value is zero if
6397 end of buffer (or C string) is reached. */
6398
6399 static struct frame *last_escape_glyph_frame = NULL;
6400 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6401 static int last_escape_glyph_merged_face_id = 0;
6402
6403 struct frame *last_glyphless_glyph_frame = NULL;
6404 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6405 int last_glyphless_glyph_merged_face_id = 0;
6406
6407 static int
6408 get_next_display_element (struct it *it)
6409 {
6410 /* Non-zero means that we found a display element. Zero means that
6411 we hit the end of what we iterate over. Performance note: the
6412 function pointer `method' used here turns out to be faster than
6413 using a sequence of if-statements. */
6414 int success_p;
6415
6416 get_next:
6417 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6418
6419 if (it->what == IT_CHARACTER)
6420 {
6421 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6422 and only if (a) the resolved directionality of that character
6423 is R..." */
6424 /* FIXME: Do we need an exception for characters from display
6425 tables? */
6426 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6427 it->c = bidi_mirror_char (it->c);
6428 /* Map via display table or translate control characters.
6429 IT->c, IT->len etc. have been set to the next character by
6430 the function call above. If we have a display table, and it
6431 contains an entry for IT->c, translate it. Don't do this if
6432 IT->c itself comes from a display table, otherwise we could
6433 end up in an infinite recursion. (An alternative could be to
6434 count the recursion depth of this function and signal an
6435 error when a certain maximum depth is reached.) Is it worth
6436 it? */
6437 if (success_p && it->dpvec == NULL)
6438 {
6439 Lisp_Object dv;
6440 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6441 int nonascii_space_p = 0;
6442 int nonascii_hyphen_p = 0;
6443 int c = it->c; /* This is the character to display. */
6444
6445 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6446 {
6447 xassert (SINGLE_BYTE_CHAR_P (c));
6448 if (unibyte_display_via_language_environment)
6449 {
6450 c = DECODE_CHAR (unibyte, c);
6451 if (c < 0)
6452 c = BYTE8_TO_CHAR (it->c);
6453 }
6454 else
6455 c = BYTE8_TO_CHAR (it->c);
6456 }
6457
6458 if (it->dp
6459 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6460 VECTORP (dv)))
6461 {
6462 struct Lisp_Vector *v = XVECTOR (dv);
6463
6464 /* Return the first character from the display table
6465 entry, if not empty. If empty, don't display the
6466 current character. */
6467 if (v->header.size)
6468 {
6469 it->dpvec_char_len = it->len;
6470 it->dpvec = v->contents;
6471 it->dpend = v->contents + v->header.size;
6472 it->current.dpvec_index = 0;
6473 it->dpvec_face_id = -1;
6474 it->saved_face_id = it->face_id;
6475 it->method = GET_FROM_DISPLAY_VECTOR;
6476 it->ellipsis_p = 0;
6477 }
6478 else
6479 {
6480 set_iterator_to_next (it, 0);
6481 }
6482 goto get_next;
6483 }
6484
6485 if (! NILP (lookup_glyphless_char_display (c, it)))
6486 {
6487 if (it->what == IT_GLYPHLESS)
6488 goto done;
6489 /* Don't display this character. */
6490 set_iterator_to_next (it, 0);
6491 goto get_next;
6492 }
6493
6494 /* If `nobreak-char-display' is non-nil, we display
6495 non-ASCII spaces and hyphens specially. */
6496 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6497 {
6498 if (c == 0xA0)
6499 nonascii_space_p = 1;
6500 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6501 nonascii_hyphen_p = 1;
6502 }
6503
6504 /* Translate control characters into `\003' or `^C' form.
6505 Control characters coming from a display table entry are
6506 currently not translated because we use IT->dpvec to hold
6507 the translation. This could easily be changed but I
6508 don't believe that it is worth doing.
6509
6510 The characters handled by `nobreak-char-display' must be
6511 translated too.
6512
6513 Non-printable characters and raw-byte characters are also
6514 translated to octal form. */
6515 if (((c < ' ' || c == 127) /* ASCII control chars */
6516 ? (it->area != TEXT_AREA
6517 /* In mode line, treat \n, \t like other crl chars. */
6518 || (c != '\t'
6519 && it->glyph_row
6520 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6521 || (c != '\n' && c != '\t'))
6522 : (nonascii_space_p
6523 || nonascii_hyphen_p
6524 || CHAR_BYTE8_P (c)
6525 || ! CHAR_PRINTABLE_P (c))))
6526 {
6527 /* C is a control character, non-ASCII space/hyphen,
6528 raw-byte, or a non-printable character which must be
6529 displayed either as '\003' or as `^C' where the '\\'
6530 and '^' can be defined in the display table. Fill
6531 IT->ctl_chars with glyphs for what we have to
6532 display. Then, set IT->dpvec to these glyphs. */
6533 Lisp_Object gc;
6534 int ctl_len;
6535 int face_id;
6536 int lface_id = 0;
6537 int escape_glyph;
6538
6539 /* Handle control characters with ^. */
6540
6541 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6542 {
6543 int g;
6544
6545 g = '^'; /* default glyph for Control */
6546 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6547 if (it->dp
6548 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6549 {
6550 g = GLYPH_CODE_CHAR (gc);
6551 lface_id = GLYPH_CODE_FACE (gc);
6552 }
6553 if (lface_id)
6554 {
6555 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6556 }
6557 else if (it->f == last_escape_glyph_frame
6558 && it->face_id == last_escape_glyph_face_id)
6559 {
6560 face_id = last_escape_glyph_merged_face_id;
6561 }
6562 else
6563 {
6564 /* Merge the escape-glyph face into the current face. */
6565 face_id = merge_faces (it->f, Qescape_glyph, 0,
6566 it->face_id);
6567 last_escape_glyph_frame = it->f;
6568 last_escape_glyph_face_id = it->face_id;
6569 last_escape_glyph_merged_face_id = face_id;
6570 }
6571
6572 XSETINT (it->ctl_chars[0], g);
6573 XSETINT (it->ctl_chars[1], c ^ 0100);
6574 ctl_len = 2;
6575 goto display_control;
6576 }
6577
6578 /* Handle non-ascii space in the mode where it only gets
6579 highlighting. */
6580
6581 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6582 {
6583 /* Merge `nobreak-space' into the current face. */
6584 face_id = merge_faces (it->f, Qnobreak_space, 0,
6585 it->face_id);
6586 XSETINT (it->ctl_chars[0], ' ');
6587 ctl_len = 1;
6588 goto display_control;
6589 }
6590
6591 /* Handle sequences that start with the "escape glyph". */
6592
6593 /* the default escape glyph is \. */
6594 escape_glyph = '\\';
6595
6596 if (it->dp
6597 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6598 {
6599 escape_glyph = GLYPH_CODE_CHAR (gc);
6600 lface_id = GLYPH_CODE_FACE (gc);
6601 }
6602 if (lface_id)
6603 {
6604 /* The display table specified a face.
6605 Merge it into face_id and also into escape_glyph. */
6606 face_id = merge_faces (it->f, Qt, lface_id,
6607 it->face_id);
6608 }
6609 else if (it->f == last_escape_glyph_frame
6610 && it->face_id == last_escape_glyph_face_id)
6611 {
6612 face_id = last_escape_glyph_merged_face_id;
6613 }
6614 else
6615 {
6616 /* Merge the escape-glyph face into the current face. */
6617 face_id = merge_faces (it->f, Qescape_glyph, 0,
6618 it->face_id);
6619 last_escape_glyph_frame = it->f;
6620 last_escape_glyph_face_id = it->face_id;
6621 last_escape_glyph_merged_face_id = face_id;
6622 }
6623
6624 /* Draw non-ASCII hyphen with just highlighting: */
6625
6626 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6627 {
6628 XSETINT (it->ctl_chars[0], '-');
6629 ctl_len = 1;
6630 goto display_control;
6631 }
6632
6633 /* Draw non-ASCII space/hyphen with escape glyph: */
6634
6635 if (nonascii_space_p || nonascii_hyphen_p)
6636 {
6637 XSETINT (it->ctl_chars[0], escape_glyph);
6638 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6639 ctl_len = 2;
6640 goto display_control;
6641 }
6642
6643 {
6644 char str[10];
6645 int len, i;
6646
6647 if (CHAR_BYTE8_P (c))
6648 /* Display \200 instead of \17777600. */
6649 c = CHAR_TO_BYTE8 (c);
6650 len = sprintf (str, "%03o", c);
6651
6652 XSETINT (it->ctl_chars[0], escape_glyph);
6653 for (i = 0; i < len; i++)
6654 XSETINT (it->ctl_chars[i + 1], str[i]);
6655 ctl_len = len + 1;
6656 }
6657
6658 display_control:
6659 /* Set up IT->dpvec and return first character from it. */
6660 it->dpvec_char_len = it->len;
6661 it->dpvec = it->ctl_chars;
6662 it->dpend = it->dpvec + ctl_len;
6663 it->current.dpvec_index = 0;
6664 it->dpvec_face_id = face_id;
6665 it->saved_face_id = it->face_id;
6666 it->method = GET_FROM_DISPLAY_VECTOR;
6667 it->ellipsis_p = 0;
6668 goto get_next;
6669 }
6670 it->char_to_display = c;
6671 }
6672 else if (success_p)
6673 {
6674 it->char_to_display = it->c;
6675 }
6676 }
6677
6678 /* Adjust face id for a multibyte character. There are no multibyte
6679 character in unibyte text. */
6680 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6681 && it->multibyte_p
6682 && success_p
6683 && FRAME_WINDOW_P (it->f))
6684 {
6685 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6686
6687 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6688 {
6689 /* Automatic composition with glyph-string. */
6690 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6691
6692 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6693 }
6694 else
6695 {
6696 ptrdiff_t pos = (it->s ? -1
6697 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6698 : IT_CHARPOS (*it));
6699 int c;
6700
6701 if (it->what == IT_CHARACTER)
6702 c = it->char_to_display;
6703 else
6704 {
6705 struct composition *cmp = composition_table[it->cmp_it.id];
6706 int i;
6707
6708 c = ' ';
6709 for (i = 0; i < cmp->glyph_len; i++)
6710 /* TAB in a composition means display glyphs with
6711 padding space on the left or right. */
6712 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6713 break;
6714 }
6715 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6716 }
6717 }
6718
6719 done:
6720 /* Is this character the last one of a run of characters with
6721 box? If yes, set IT->end_of_box_run_p to 1. */
6722 if (it->face_box_p
6723 && it->s == NULL)
6724 {
6725 if (it->method == GET_FROM_STRING && it->sp)
6726 {
6727 int face_id = underlying_face_id (it);
6728 struct face *face = FACE_FROM_ID (it->f, face_id);
6729
6730 if (face)
6731 {
6732 if (face->box == FACE_NO_BOX)
6733 {
6734 /* If the box comes from face properties in a
6735 display string, check faces in that string. */
6736 int string_face_id = face_after_it_pos (it);
6737 it->end_of_box_run_p
6738 = (FACE_FROM_ID (it->f, string_face_id)->box
6739 == FACE_NO_BOX);
6740 }
6741 /* Otherwise, the box comes from the underlying face.
6742 If this is the last string character displayed, check
6743 the next buffer location. */
6744 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6745 && (it->current.overlay_string_index
6746 == it->n_overlay_strings - 1))
6747 {
6748 ptrdiff_t ignore;
6749 int next_face_id;
6750 struct text_pos pos = it->current.pos;
6751 INC_TEXT_POS (pos, it->multibyte_p);
6752
6753 next_face_id = face_at_buffer_position
6754 (it->w, CHARPOS (pos), it->region_beg_charpos,
6755 it->region_end_charpos, &ignore,
6756 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6757 -1);
6758 it->end_of_box_run_p
6759 = (FACE_FROM_ID (it->f, next_face_id)->box
6760 == FACE_NO_BOX);
6761 }
6762 }
6763 }
6764 else
6765 {
6766 int face_id = face_after_it_pos (it);
6767 it->end_of_box_run_p
6768 = (face_id != it->face_id
6769 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6770 }
6771 }
6772
6773 /* Value is 0 if end of buffer or string reached. */
6774 return success_p;
6775 }
6776
6777
6778 /* Move IT to the next display element.
6779
6780 RESEAT_P non-zero means if called on a newline in buffer text,
6781 skip to the next visible line start.
6782
6783 Functions get_next_display_element and set_iterator_to_next are
6784 separate because I find this arrangement easier to handle than a
6785 get_next_display_element function that also increments IT's
6786 position. The way it is we can first look at an iterator's current
6787 display element, decide whether it fits on a line, and if it does,
6788 increment the iterator position. The other way around we probably
6789 would either need a flag indicating whether the iterator has to be
6790 incremented the next time, or we would have to implement a
6791 decrement position function which would not be easy to write. */
6792
6793 void
6794 set_iterator_to_next (struct it *it, int reseat_p)
6795 {
6796 /* Reset flags indicating start and end of a sequence of characters
6797 with box. Reset them at the start of this function because
6798 moving the iterator to a new position might set them. */
6799 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6800
6801 switch (it->method)
6802 {
6803 case GET_FROM_BUFFER:
6804 /* The current display element of IT is a character from
6805 current_buffer. Advance in the buffer, and maybe skip over
6806 invisible lines that are so because of selective display. */
6807 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6808 reseat_at_next_visible_line_start (it, 0);
6809 else if (it->cmp_it.id >= 0)
6810 {
6811 /* We are currently getting glyphs from a composition. */
6812 int i;
6813
6814 if (! it->bidi_p)
6815 {
6816 IT_CHARPOS (*it) += it->cmp_it.nchars;
6817 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6818 if (it->cmp_it.to < it->cmp_it.nglyphs)
6819 {
6820 it->cmp_it.from = it->cmp_it.to;
6821 }
6822 else
6823 {
6824 it->cmp_it.id = -1;
6825 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6826 IT_BYTEPOS (*it),
6827 it->end_charpos, Qnil);
6828 }
6829 }
6830 else if (! it->cmp_it.reversed_p)
6831 {
6832 /* Composition created while scanning forward. */
6833 /* Update IT's char/byte positions to point to the first
6834 character of the next grapheme cluster, or to the
6835 character visually after the current composition. */
6836 for (i = 0; i < it->cmp_it.nchars; i++)
6837 bidi_move_to_visually_next (&it->bidi_it);
6838 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6839 IT_CHARPOS (*it) = it->bidi_it.charpos;
6840
6841 if (it->cmp_it.to < it->cmp_it.nglyphs)
6842 {
6843 /* Proceed to the next grapheme cluster. */
6844 it->cmp_it.from = it->cmp_it.to;
6845 }
6846 else
6847 {
6848 /* No more grapheme clusters in this composition.
6849 Find the next stop position. */
6850 ptrdiff_t stop = it->end_charpos;
6851 if (it->bidi_it.scan_dir < 0)
6852 /* Now we are scanning backward and don't know
6853 where to stop. */
6854 stop = -1;
6855 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6856 IT_BYTEPOS (*it), stop, Qnil);
6857 }
6858 }
6859 else
6860 {
6861 /* Composition created while scanning backward. */
6862 /* Update IT's char/byte positions to point to the last
6863 character of the previous grapheme cluster, or the
6864 character visually after the current composition. */
6865 for (i = 0; i < it->cmp_it.nchars; i++)
6866 bidi_move_to_visually_next (&it->bidi_it);
6867 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6868 IT_CHARPOS (*it) = it->bidi_it.charpos;
6869 if (it->cmp_it.from > 0)
6870 {
6871 /* Proceed to the previous grapheme cluster. */
6872 it->cmp_it.to = it->cmp_it.from;
6873 }
6874 else
6875 {
6876 /* No more grapheme clusters in this composition.
6877 Find the next stop position. */
6878 ptrdiff_t stop = it->end_charpos;
6879 if (it->bidi_it.scan_dir < 0)
6880 /* Now we are scanning backward and don't know
6881 where to stop. */
6882 stop = -1;
6883 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6884 IT_BYTEPOS (*it), stop, Qnil);
6885 }
6886 }
6887 }
6888 else
6889 {
6890 xassert (it->len != 0);
6891
6892 if (!it->bidi_p)
6893 {
6894 IT_BYTEPOS (*it) += it->len;
6895 IT_CHARPOS (*it) += 1;
6896 }
6897 else
6898 {
6899 int prev_scan_dir = it->bidi_it.scan_dir;
6900 /* If this is a new paragraph, determine its base
6901 direction (a.k.a. its base embedding level). */
6902 if (it->bidi_it.new_paragraph)
6903 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6904 bidi_move_to_visually_next (&it->bidi_it);
6905 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6906 IT_CHARPOS (*it) = it->bidi_it.charpos;
6907 if (prev_scan_dir != it->bidi_it.scan_dir)
6908 {
6909 /* As the scan direction was changed, we must
6910 re-compute the stop position for composition. */
6911 ptrdiff_t stop = it->end_charpos;
6912 if (it->bidi_it.scan_dir < 0)
6913 stop = -1;
6914 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6915 IT_BYTEPOS (*it), stop, Qnil);
6916 }
6917 }
6918 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6919 }
6920 break;
6921
6922 case GET_FROM_C_STRING:
6923 /* Current display element of IT is from a C string. */
6924 if (!it->bidi_p
6925 /* If the string position is beyond string's end, it means
6926 next_element_from_c_string is padding the string with
6927 blanks, in which case we bypass the bidi iterator,
6928 because it cannot deal with such virtual characters. */
6929 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6930 {
6931 IT_BYTEPOS (*it) += it->len;
6932 IT_CHARPOS (*it) += 1;
6933 }
6934 else
6935 {
6936 bidi_move_to_visually_next (&it->bidi_it);
6937 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6938 IT_CHARPOS (*it) = it->bidi_it.charpos;
6939 }
6940 break;
6941
6942 case GET_FROM_DISPLAY_VECTOR:
6943 /* Current display element of IT is from a display table entry.
6944 Advance in the display table definition. Reset it to null if
6945 end reached, and continue with characters from buffers/
6946 strings. */
6947 ++it->current.dpvec_index;
6948
6949 /* Restore face of the iterator to what they were before the
6950 display vector entry (these entries may contain faces). */
6951 it->face_id = it->saved_face_id;
6952
6953 if (it->dpvec + it->current.dpvec_index == it->dpend)
6954 {
6955 int recheck_faces = it->ellipsis_p;
6956
6957 if (it->s)
6958 it->method = GET_FROM_C_STRING;
6959 else if (STRINGP (it->string))
6960 it->method = GET_FROM_STRING;
6961 else
6962 {
6963 it->method = GET_FROM_BUFFER;
6964 it->object = it->w->buffer;
6965 }
6966
6967 it->dpvec = NULL;
6968 it->current.dpvec_index = -1;
6969
6970 /* Skip over characters which were displayed via IT->dpvec. */
6971 if (it->dpvec_char_len < 0)
6972 reseat_at_next_visible_line_start (it, 1);
6973 else if (it->dpvec_char_len > 0)
6974 {
6975 if (it->method == GET_FROM_STRING
6976 && it->n_overlay_strings > 0)
6977 it->ignore_overlay_strings_at_pos_p = 1;
6978 it->len = it->dpvec_char_len;
6979 set_iterator_to_next (it, reseat_p);
6980 }
6981
6982 /* Maybe recheck faces after display vector */
6983 if (recheck_faces)
6984 it->stop_charpos = IT_CHARPOS (*it);
6985 }
6986 break;
6987
6988 case GET_FROM_STRING:
6989 /* Current display element is a character from a Lisp string. */
6990 xassert (it->s == NULL && STRINGP (it->string));
6991 if (it->cmp_it.id >= 0)
6992 {
6993 int i;
6994
6995 if (! it->bidi_p)
6996 {
6997 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6998 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6999 if (it->cmp_it.to < it->cmp_it.nglyphs)
7000 it->cmp_it.from = it->cmp_it.to;
7001 else
7002 {
7003 it->cmp_it.id = -1;
7004 composition_compute_stop_pos (&it->cmp_it,
7005 IT_STRING_CHARPOS (*it),
7006 IT_STRING_BYTEPOS (*it),
7007 it->end_charpos, it->string);
7008 }
7009 }
7010 else if (! it->cmp_it.reversed_p)
7011 {
7012 for (i = 0; i < it->cmp_it.nchars; i++)
7013 bidi_move_to_visually_next (&it->bidi_it);
7014 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7015 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7016
7017 if (it->cmp_it.to < it->cmp_it.nglyphs)
7018 it->cmp_it.from = it->cmp_it.to;
7019 else
7020 {
7021 ptrdiff_t stop = it->end_charpos;
7022 if (it->bidi_it.scan_dir < 0)
7023 stop = -1;
7024 composition_compute_stop_pos (&it->cmp_it,
7025 IT_STRING_CHARPOS (*it),
7026 IT_STRING_BYTEPOS (*it), stop,
7027 it->string);
7028 }
7029 }
7030 else
7031 {
7032 for (i = 0; i < it->cmp_it.nchars; i++)
7033 bidi_move_to_visually_next (&it->bidi_it);
7034 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7035 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7036 if (it->cmp_it.from > 0)
7037 it->cmp_it.to = it->cmp_it.from;
7038 else
7039 {
7040 ptrdiff_t stop = it->end_charpos;
7041 if (it->bidi_it.scan_dir < 0)
7042 stop = -1;
7043 composition_compute_stop_pos (&it->cmp_it,
7044 IT_STRING_CHARPOS (*it),
7045 IT_STRING_BYTEPOS (*it), stop,
7046 it->string);
7047 }
7048 }
7049 }
7050 else
7051 {
7052 if (!it->bidi_p
7053 /* If the string position is beyond string's end, it
7054 means next_element_from_string is padding the string
7055 with blanks, in which case we bypass the bidi
7056 iterator, because it cannot deal with such virtual
7057 characters. */
7058 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7059 {
7060 IT_STRING_BYTEPOS (*it) += it->len;
7061 IT_STRING_CHARPOS (*it) += 1;
7062 }
7063 else
7064 {
7065 int prev_scan_dir = it->bidi_it.scan_dir;
7066
7067 bidi_move_to_visually_next (&it->bidi_it);
7068 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7069 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7070 if (prev_scan_dir != it->bidi_it.scan_dir)
7071 {
7072 ptrdiff_t stop = it->end_charpos;
7073
7074 if (it->bidi_it.scan_dir < 0)
7075 stop = -1;
7076 composition_compute_stop_pos (&it->cmp_it,
7077 IT_STRING_CHARPOS (*it),
7078 IT_STRING_BYTEPOS (*it), stop,
7079 it->string);
7080 }
7081 }
7082 }
7083
7084 consider_string_end:
7085
7086 if (it->current.overlay_string_index >= 0)
7087 {
7088 /* IT->string is an overlay string. Advance to the
7089 next, if there is one. */
7090 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7091 {
7092 it->ellipsis_p = 0;
7093 next_overlay_string (it);
7094 if (it->ellipsis_p)
7095 setup_for_ellipsis (it, 0);
7096 }
7097 }
7098 else
7099 {
7100 /* IT->string is not an overlay string. If we reached
7101 its end, and there is something on IT->stack, proceed
7102 with what is on the stack. This can be either another
7103 string, this time an overlay string, or a buffer. */
7104 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7105 && it->sp > 0)
7106 {
7107 pop_it (it);
7108 if (it->method == GET_FROM_STRING)
7109 goto consider_string_end;
7110 }
7111 }
7112 break;
7113
7114 case GET_FROM_IMAGE:
7115 case GET_FROM_STRETCH:
7116 /* The position etc with which we have to proceed are on
7117 the stack. The position may be at the end of a string,
7118 if the `display' property takes up the whole string. */
7119 xassert (it->sp > 0);
7120 pop_it (it);
7121 if (it->method == GET_FROM_STRING)
7122 goto consider_string_end;
7123 break;
7124
7125 default:
7126 /* There are no other methods defined, so this should be a bug. */
7127 abort ();
7128 }
7129
7130 xassert (it->method != GET_FROM_STRING
7131 || (STRINGP (it->string)
7132 && IT_STRING_CHARPOS (*it) >= 0));
7133 }
7134
7135 /* Load IT's display element fields with information about the next
7136 display element which comes from a display table entry or from the
7137 result of translating a control character to one of the forms `^C'
7138 or `\003'.
7139
7140 IT->dpvec holds the glyphs to return as characters.
7141 IT->saved_face_id holds the face id before the display vector--it
7142 is restored into IT->face_id in set_iterator_to_next. */
7143
7144 static int
7145 next_element_from_display_vector (struct it *it)
7146 {
7147 Lisp_Object gc;
7148
7149 /* Precondition. */
7150 xassert (it->dpvec && it->current.dpvec_index >= 0);
7151
7152 it->face_id = it->saved_face_id;
7153
7154 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7155 That seemed totally bogus - so I changed it... */
7156 gc = it->dpvec[it->current.dpvec_index];
7157
7158 if (GLYPH_CODE_P (gc))
7159 {
7160 it->c = GLYPH_CODE_CHAR (gc);
7161 it->len = CHAR_BYTES (it->c);
7162
7163 /* The entry may contain a face id to use. Such a face id is
7164 the id of a Lisp face, not a realized face. A face id of
7165 zero means no face is specified. */
7166 if (it->dpvec_face_id >= 0)
7167 it->face_id = it->dpvec_face_id;
7168 else
7169 {
7170 int lface_id = GLYPH_CODE_FACE (gc);
7171 if (lface_id > 0)
7172 it->face_id = merge_faces (it->f, Qt, lface_id,
7173 it->saved_face_id);
7174 }
7175 }
7176 else
7177 /* Display table entry is invalid. Return a space. */
7178 it->c = ' ', it->len = 1;
7179
7180 /* Don't change position and object of the iterator here. They are
7181 still the values of the character that had this display table
7182 entry or was translated, and that's what we want. */
7183 it->what = IT_CHARACTER;
7184 return 1;
7185 }
7186
7187 /* Get the first element of string/buffer in the visual order, after
7188 being reseated to a new position in a string or a buffer. */
7189 static void
7190 get_visually_first_element (struct it *it)
7191 {
7192 int string_p = STRINGP (it->string) || it->s;
7193 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7194 ptrdiff_t bob = (string_p ? 0 : BEGV);
7195
7196 if (STRINGP (it->string))
7197 {
7198 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7199 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7200 }
7201 else
7202 {
7203 it->bidi_it.charpos = IT_CHARPOS (*it);
7204 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7205 }
7206
7207 if (it->bidi_it.charpos == eob)
7208 {
7209 /* Nothing to do, but reset the FIRST_ELT flag, like
7210 bidi_paragraph_init does, because we are not going to
7211 call it. */
7212 it->bidi_it.first_elt = 0;
7213 }
7214 else if (it->bidi_it.charpos == bob
7215 || (!string_p
7216 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7217 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7218 {
7219 /* If we are at the beginning of a line/string, we can produce
7220 the next element right away. */
7221 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7222 bidi_move_to_visually_next (&it->bidi_it);
7223 }
7224 else
7225 {
7226 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7227
7228 /* We need to prime the bidi iterator starting at the line's or
7229 string's beginning, before we will be able to produce the
7230 next element. */
7231 if (string_p)
7232 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7233 else
7234 {
7235 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7236 -1);
7237 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7238 }
7239 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7240 do
7241 {
7242 /* Now return to buffer/string position where we were asked
7243 to get the next display element, and produce that. */
7244 bidi_move_to_visually_next (&it->bidi_it);
7245 }
7246 while (it->bidi_it.bytepos != orig_bytepos
7247 && it->bidi_it.charpos < eob);
7248 }
7249
7250 /* Adjust IT's position information to where we ended up. */
7251 if (STRINGP (it->string))
7252 {
7253 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7254 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7255 }
7256 else
7257 {
7258 IT_CHARPOS (*it) = it->bidi_it.charpos;
7259 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 }
7261
7262 if (STRINGP (it->string) || !it->s)
7263 {
7264 ptrdiff_t stop, charpos, bytepos;
7265
7266 if (STRINGP (it->string))
7267 {
7268 xassert (!it->s);
7269 stop = SCHARS (it->string);
7270 if (stop > it->end_charpos)
7271 stop = it->end_charpos;
7272 charpos = IT_STRING_CHARPOS (*it);
7273 bytepos = IT_STRING_BYTEPOS (*it);
7274 }
7275 else
7276 {
7277 stop = it->end_charpos;
7278 charpos = IT_CHARPOS (*it);
7279 bytepos = IT_BYTEPOS (*it);
7280 }
7281 if (it->bidi_it.scan_dir < 0)
7282 stop = -1;
7283 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7284 it->string);
7285 }
7286 }
7287
7288 /* Load IT with the next display element from Lisp string IT->string.
7289 IT->current.string_pos is the current position within the string.
7290 If IT->current.overlay_string_index >= 0, the Lisp string is an
7291 overlay string. */
7292
7293 static int
7294 next_element_from_string (struct it *it)
7295 {
7296 struct text_pos position;
7297
7298 xassert (STRINGP (it->string));
7299 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7300 xassert (IT_STRING_CHARPOS (*it) >= 0);
7301 position = it->current.string_pos;
7302
7303 /* With bidi reordering, the character to display might not be the
7304 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7305 that we were reseat()ed to a new string, whose paragraph
7306 direction is not known. */
7307 if (it->bidi_p && it->bidi_it.first_elt)
7308 {
7309 get_visually_first_element (it);
7310 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7311 }
7312
7313 /* Time to check for invisible text? */
7314 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7315 {
7316 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7317 {
7318 if (!(!it->bidi_p
7319 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7320 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7321 {
7322 /* With bidi non-linear iteration, we could find
7323 ourselves far beyond the last computed stop_charpos,
7324 with several other stop positions in between that we
7325 missed. Scan them all now, in buffer's logical
7326 order, until we find and handle the last stop_charpos
7327 that precedes our current position. */
7328 handle_stop_backwards (it, it->stop_charpos);
7329 return GET_NEXT_DISPLAY_ELEMENT (it);
7330 }
7331 else
7332 {
7333 if (it->bidi_p)
7334 {
7335 /* Take note of the stop position we just moved
7336 across, for when we will move back across it. */
7337 it->prev_stop = it->stop_charpos;
7338 /* If we are at base paragraph embedding level, take
7339 note of the last stop position seen at this
7340 level. */
7341 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7342 it->base_level_stop = it->stop_charpos;
7343 }
7344 handle_stop (it);
7345
7346 /* Since a handler may have changed IT->method, we must
7347 recurse here. */
7348 return GET_NEXT_DISPLAY_ELEMENT (it);
7349 }
7350 }
7351 else if (it->bidi_p
7352 /* If we are before prev_stop, we may have overstepped
7353 on our way backwards a stop_pos, and if so, we need
7354 to handle that stop_pos. */
7355 && IT_STRING_CHARPOS (*it) < it->prev_stop
7356 /* We can sometimes back up for reasons that have nothing
7357 to do with bidi reordering. E.g., compositions. The
7358 code below is only needed when we are above the base
7359 embedding level, so test for that explicitly. */
7360 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7361 {
7362 /* If we lost track of base_level_stop, we have no better
7363 place for handle_stop_backwards to start from than string
7364 beginning. This happens, e.g., when we were reseated to
7365 the previous screenful of text by vertical-motion. */
7366 if (it->base_level_stop <= 0
7367 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7368 it->base_level_stop = 0;
7369 handle_stop_backwards (it, it->base_level_stop);
7370 return GET_NEXT_DISPLAY_ELEMENT (it);
7371 }
7372 }
7373
7374 if (it->current.overlay_string_index >= 0)
7375 {
7376 /* Get the next character from an overlay string. In overlay
7377 strings, there is no field width or padding with spaces to
7378 do. */
7379 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7380 {
7381 it->what = IT_EOB;
7382 return 0;
7383 }
7384 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7385 IT_STRING_BYTEPOS (*it),
7386 it->bidi_it.scan_dir < 0
7387 ? -1
7388 : SCHARS (it->string))
7389 && next_element_from_composition (it))
7390 {
7391 return 1;
7392 }
7393 else if (STRING_MULTIBYTE (it->string))
7394 {
7395 const unsigned char *s = (SDATA (it->string)
7396 + IT_STRING_BYTEPOS (*it));
7397 it->c = string_char_and_length (s, &it->len);
7398 }
7399 else
7400 {
7401 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7402 it->len = 1;
7403 }
7404 }
7405 else
7406 {
7407 /* Get the next character from a Lisp string that is not an
7408 overlay string. Such strings come from the mode line, for
7409 example. We may have to pad with spaces, or truncate the
7410 string. See also next_element_from_c_string. */
7411 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7412 {
7413 it->what = IT_EOB;
7414 return 0;
7415 }
7416 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7417 {
7418 /* Pad with spaces. */
7419 it->c = ' ', it->len = 1;
7420 CHARPOS (position) = BYTEPOS (position) = -1;
7421 }
7422 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7423 IT_STRING_BYTEPOS (*it),
7424 it->bidi_it.scan_dir < 0
7425 ? -1
7426 : it->string_nchars)
7427 && next_element_from_composition (it))
7428 {
7429 return 1;
7430 }
7431 else if (STRING_MULTIBYTE (it->string))
7432 {
7433 const unsigned char *s = (SDATA (it->string)
7434 + IT_STRING_BYTEPOS (*it));
7435 it->c = string_char_and_length (s, &it->len);
7436 }
7437 else
7438 {
7439 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7440 it->len = 1;
7441 }
7442 }
7443
7444 /* Record what we have and where it came from. */
7445 it->what = IT_CHARACTER;
7446 it->object = it->string;
7447 it->position = position;
7448 return 1;
7449 }
7450
7451
7452 /* Load IT with next display element from C string IT->s.
7453 IT->string_nchars is the maximum number of characters to return
7454 from the string. IT->end_charpos may be greater than
7455 IT->string_nchars when this function is called, in which case we
7456 may have to return padding spaces. Value is zero if end of string
7457 reached, including padding spaces. */
7458
7459 static int
7460 next_element_from_c_string (struct it *it)
7461 {
7462 int success_p = 1;
7463
7464 xassert (it->s);
7465 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7466 it->what = IT_CHARACTER;
7467 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7468 it->object = Qnil;
7469
7470 /* With bidi reordering, the character to display might not be the
7471 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7472 we were reseated to a new string, whose paragraph direction is
7473 not known. */
7474 if (it->bidi_p && it->bidi_it.first_elt)
7475 get_visually_first_element (it);
7476
7477 /* IT's position can be greater than IT->string_nchars in case a
7478 field width or precision has been specified when the iterator was
7479 initialized. */
7480 if (IT_CHARPOS (*it) >= it->end_charpos)
7481 {
7482 /* End of the game. */
7483 it->what = IT_EOB;
7484 success_p = 0;
7485 }
7486 else if (IT_CHARPOS (*it) >= it->string_nchars)
7487 {
7488 /* Pad with spaces. */
7489 it->c = ' ', it->len = 1;
7490 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7491 }
7492 else if (it->multibyte_p)
7493 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7494 else
7495 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7496
7497 return success_p;
7498 }
7499
7500
7501 /* Set up IT to return characters from an ellipsis, if appropriate.
7502 The definition of the ellipsis glyphs may come from a display table
7503 entry. This function fills IT with the first glyph from the
7504 ellipsis if an ellipsis is to be displayed. */
7505
7506 static int
7507 next_element_from_ellipsis (struct it *it)
7508 {
7509 if (it->selective_display_ellipsis_p)
7510 setup_for_ellipsis (it, it->len);
7511 else
7512 {
7513 /* The face at the current position may be different from the
7514 face we find after the invisible text. Remember what it
7515 was in IT->saved_face_id, and signal that it's there by
7516 setting face_before_selective_p. */
7517 it->saved_face_id = it->face_id;
7518 it->method = GET_FROM_BUFFER;
7519 it->object = it->w->buffer;
7520 reseat_at_next_visible_line_start (it, 1);
7521 it->face_before_selective_p = 1;
7522 }
7523
7524 return GET_NEXT_DISPLAY_ELEMENT (it);
7525 }
7526
7527
7528 /* Deliver an image display element. The iterator IT is already
7529 filled with image information (done in handle_display_prop). Value
7530 is always 1. */
7531
7532
7533 static int
7534 next_element_from_image (struct it *it)
7535 {
7536 it->what = IT_IMAGE;
7537 it->ignore_overlay_strings_at_pos_p = 0;
7538 return 1;
7539 }
7540
7541
7542 /* Fill iterator IT with next display element from a stretch glyph
7543 property. IT->object is the value of the text property. Value is
7544 always 1. */
7545
7546 static int
7547 next_element_from_stretch (struct it *it)
7548 {
7549 it->what = IT_STRETCH;
7550 return 1;
7551 }
7552
7553 /* Scan backwards from IT's current position until we find a stop
7554 position, or until BEGV. This is called when we find ourself
7555 before both the last known prev_stop and base_level_stop while
7556 reordering bidirectional text. */
7557
7558 static void
7559 compute_stop_pos_backwards (struct it *it)
7560 {
7561 const int SCAN_BACK_LIMIT = 1000;
7562 struct text_pos pos;
7563 struct display_pos save_current = it->current;
7564 struct text_pos save_position = it->position;
7565 ptrdiff_t charpos = IT_CHARPOS (*it);
7566 ptrdiff_t where_we_are = charpos;
7567 ptrdiff_t save_stop_pos = it->stop_charpos;
7568 ptrdiff_t save_end_pos = it->end_charpos;
7569
7570 xassert (NILP (it->string) && !it->s);
7571 xassert (it->bidi_p);
7572 it->bidi_p = 0;
7573 do
7574 {
7575 it->end_charpos = min (charpos + 1, ZV);
7576 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7577 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7578 reseat_1 (it, pos, 0);
7579 compute_stop_pos (it);
7580 /* We must advance forward, right? */
7581 if (it->stop_charpos <= charpos)
7582 abort ();
7583 }
7584 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7585
7586 if (it->stop_charpos <= where_we_are)
7587 it->prev_stop = it->stop_charpos;
7588 else
7589 it->prev_stop = BEGV;
7590 it->bidi_p = 1;
7591 it->current = save_current;
7592 it->position = save_position;
7593 it->stop_charpos = save_stop_pos;
7594 it->end_charpos = save_end_pos;
7595 }
7596
7597 /* Scan forward from CHARPOS in the current buffer/string, until we
7598 find a stop position > current IT's position. Then handle the stop
7599 position before that. This is called when we bump into a stop
7600 position while reordering bidirectional text. CHARPOS should be
7601 the last previously processed stop_pos (or BEGV/0, if none were
7602 processed yet) whose position is less that IT's current
7603 position. */
7604
7605 static void
7606 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7607 {
7608 int bufp = !STRINGP (it->string);
7609 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7610 struct display_pos save_current = it->current;
7611 struct text_pos save_position = it->position;
7612 struct text_pos pos1;
7613 ptrdiff_t next_stop;
7614
7615 /* Scan in strict logical order. */
7616 xassert (it->bidi_p);
7617 it->bidi_p = 0;
7618 do
7619 {
7620 it->prev_stop = charpos;
7621 if (bufp)
7622 {
7623 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7624 reseat_1 (it, pos1, 0);
7625 }
7626 else
7627 it->current.string_pos = string_pos (charpos, it->string);
7628 compute_stop_pos (it);
7629 /* We must advance forward, right? */
7630 if (it->stop_charpos <= it->prev_stop)
7631 abort ();
7632 charpos = it->stop_charpos;
7633 }
7634 while (charpos <= where_we_are);
7635
7636 it->bidi_p = 1;
7637 it->current = save_current;
7638 it->position = save_position;
7639 next_stop = it->stop_charpos;
7640 it->stop_charpos = it->prev_stop;
7641 handle_stop (it);
7642 it->stop_charpos = next_stop;
7643 }
7644
7645 /* Load IT with the next display element from current_buffer. Value
7646 is zero if end of buffer reached. IT->stop_charpos is the next
7647 position at which to stop and check for text properties or buffer
7648 end. */
7649
7650 static int
7651 next_element_from_buffer (struct it *it)
7652 {
7653 int success_p = 1;
7654
7655 xassert (IT_CHARPOS (*it) >= BEGV);
7656 xassert (NILP (it->string) && !it->s);
7657 xassert (!it->bidi_p
7658 || (EQ (it->bidi_it.string.lstring, Qnil)
7659 && it->bidi_it.string.s == NULL));
7660
7661 /* With bidi reordering, the character to display might not be the
7662 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7663 we were reseat()ed to a new buffer position, which is potentially
7664 a different paragraph. */
7665 if (it->bidi_p && it->bidi_it.first_elt)
7666 {
7667 get_visually_first_element (it);
7668 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7669 }
7670
7671 if (IT_CHARPOS (*it) >= it->stop_charpos)
7672 {
7673 if (IT_CHARPOS (*it) >= it->end_charpos)
7674 {
7675 int overlay_strings_follow_p;
7676
7677 /* End of the game, except when overlay strings follow that
7678 haven't been returned yet. */
7679 if (it->overlay_strings_at_end_processed_p)
7680 overlay_strings_follow_p = 0;
7681 else
7682 {
7683 it->overlay_strings_at_end_processed_p = 1;
7684 overlay_strings_follow_p = get_overlay_strings (it, 0);
7685 }
7686
7687 if (overlay_strings_follow_p)
7688 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7689 else
7690 {
7691 it->what = IT_EOB;
7692 it->position = it->current.pos;
7693 success_p = 0;
7694 }
7695 }
7696 else if (!(!it->bidi_p
7697 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7698 || IT_CHARPOS (*it) == it->stop_charpos))
7699 {
7700 /* With bidi non-linear iteration, we could find ourselves
7701 far beyond the last computed stop_charpos, with several
7702 other stop positions in between that we missed. Scan
7703 them all now, in buffer's logical order, until we find
7704 and handle the last stop_charpos that precedes our
7705 current position. */
7706 handle_stop_backwards (it, it->stop_charpos);
7707 return GET_NEXT_DISPLAY_ELEMENT (it);
7708 }
7709 else
7710 {
7711 if (it->bidi_p)
7712 {
7713 /* Take note of the stop position we just moved across,
7714 for when we will move back across it. */
7715 it->prev_stop = it->stop_charpos;
7716 /* If we are at base paragraph embedding level, take
7717 note of the last stop position seen at this
7718 level. */
7719 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7720 it->base_level_stop = it->stop_charpos;
7721 }
7722 handle_stop (it);
7723 return GET_NEXT_DISPLAY_ELEMENT (it);
7724 }
7725 }
7726 else if (it->bidi_p
7727 /* If we are before prev_stop, we may have overstepped on
7728 our way backwards a stop_pos, and if so, we need to
7729 handle that stop_pos. */
7730 && IT_CHARPOS (*it) < it->prev_stop
7731 /* We can sometimes back up for reasons that have nothing
7732 to do with bidi reordering. E.g., compositions. The
7733 code below is only needed when we are above the base
7734 embedding level, so test for that explicitly. */
7735 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7736 {
7737 if (it->base_level_stop <= 0
7738 || IT_CHARPOS (*it) < it->base_level_stop)
7739 {
7740 /* If we lost track of base_level_stop, we need to find
7741 prev_stop by looking backwards. This happens, e.g., when
7742 we were reseated to the previous screenful of text by
7743 vertical-motion. */
7744 it->base_level_stop = BEGV;
7745 compute_stop_pos_backwards (it);
7746 handle_stop_backwards (it, it->prev_stop);
7747 }
7748 else
7749 handle_stop_backwards (it, it->base_level_stop);
7750 return GET_NEXT_DISPLAY_ELEMENT (it);
7751 }
7752 else
7753 {
7754 /* No face changes, overlays etc. in sight, so just return a
7755 character from current_buffer. */
7756 unsigned char *p;
7757 ptrdiff_t stop;
7758
7759 /* Maybe run the redisplay end trigger hook. Performance note:
7760 This doesn't seem to cost measurable time. */
7761 if (it->redisplay_end_trigger_charpos
7762 && it->glyph_row
7763 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7764 run_redisplay_end_trigger_hook (it);
7765
7766 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7767 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7768 stop)
7769 && next_element_from_composition (it))
7770 {
7771 return 1;
7772 }
7773
7774 /* Get the next character, maybe multibyte. */
7775 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7776 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7777 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7778 else
7779 it->c = *p, it->len = 1;
7780
7781 /* Record what we have and where it came from. */
7782 it->what = IT_CHARACTER;
7783 it->object = it->w->buffer;
7784 it->position = it->current.pos;
7785
7786 /* Normally we return the character found above, except when we
7787 really want to return an ellipsis for selective display. */
7788 if (it->selective)
7789 {
7790 if (it->c == '\n')
7791 {
7792 /* A value of selective > 0 means hide lines indented more
7793 than that number of columns. */
7794 if (it->selective > 0
7795 && IT_CHARPOS (*it) + 1 < ZV
7796 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7797 IT_BYTEPOS (*it) + 1,
7798 it->selective))
7799 {
7800 success_p = next_element_from_ellipsis (it);
7801 it->dpvec_char_len = -1;
7802 }
7803 }
7804 else if (it->c == '\r' && it->selective == -1)
7805 {
7806 /* A value of selective == -1 means that everything from the
7807 CR to the end of the line is invisible, with maybe an
7808 ellipsis displayed for it. */
7809 success_p = next_element_from_ellipsis (it);
7810 it->dpvec_char_len = -1;
7811 }
7812 }
7813 }
7814
7815 /* Value is zero if end of buffer reached. */
7816 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7817 return success_p;
7818 }
7819
7820
7821 /* Run the redisplay end trigger hook for IT. */
7822
7823 static void
7824 run_redisplay_end_trigger_hook (struct it *it)
7825 {
7826 Lisp_Object args[3];
7827
7828 /* IT->glyph_row should be non-null, i.e. we should be actually
7829 displaying something, or otherwise we should not run the hook. */
7830 xassert (it->glyph_row);
7831
7832 /* Set up hook arguments. */
7833 args[0] = Qredisplay_end_trigger_functions;
7834 args[1] = it->window;
7835 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7836 it->redisplay_end_trigger_charpos = 0;
7837
7838 /* Since we are *trying* to run these functions, don't try to run
7839 them again, even if they get an error. */
7840 it->w->redisplay_end_trigger = Qnil;
7841 Frun_hook_with_args (3, args);
7842
7843 /* Notice if it changed the face of the character we are on. */
7844 handle_face_prop (it);
7845 }
7846
7847
7848 /* Deliver a composition display element. Unlike the other
7849 next_element_from_XXX, this function is not registered in the array
7850 get_next_element[]. It is called from next_element_from_buffer and
7851 next_element_from_string when necessary. */
7852
7853 static int
7854 next_element_from_composition (struct it *it)
7855 {
7856 it->what = IT_COMPOSITION;
7857 it->len = it->cmp_it.nbytes;
7858 if (STRINGP (it->string))
7859 {
7860 if (it->c < 0)
7861 {
7862 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7863 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7864 return 0;
7865 }
7866 it->position = it->current.string_pos;
7867 it->object = it->string;
7868 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7869 IT_STRING_BYTEPOS (*it), it->string);
7870 }
7871 else
7872 {
7873 if (it->c < 0)
7874 {
7875 IT_CHARPOS (*it) += it->cmp_it.nchars;
7876 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7877 if (it->bidi_p)
7878 {
7879 if (it->bidi_it.new_paragraph)
7880 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7881 /* Resync the bidi iterator with IT's new position.
7882 FIXME: this doesn't support bidirectional text. */
7883 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7884 bidi_move_to_visually_next (&it->bidi_it);
7885 }
7886 return 0;
7887 }
7888 it->position = it->current.pos;
7889 it->object = it->w->buffer;
7890 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7891 IT_BYTEPOS (*it), Qnil);
7892 }
7893 return 1;
7894 }
7895
7896
7897 \f
7898 /***********************************************************************
7899 Moving an iterator without producing glyphs
7900 ***********************************************************************/
7901
7902 /* Check if iterator is at a position corresponding to a valid buffer
7903 position after some move_it_ call. */
7904
7905 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7906 ((it)->method == GET_FROM_STRING \
7907 ? IT_STRING_CHARPOS (*it) == 0 \
7908 : 1)
7909
7910
7911 /* Move iterator IT to a specified buffer or X position within one
7912 line on the display without producing glyphs.
7913
7914 OP should be a bit mask including some or all of these bits:
7915 MOVE_TO_X: Stop upon reaching x-position TO_X.
7916 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7917 Regardless of OP's value, stop upon reaching the end of the display line.
7918
7919 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7920 This means, in particular, that TO_X includes window's horizontal
7921 scroll amount.
7922
7923 The return value has several possible values that
7924 say what condition caused the scan to stop:
7925
7926 MOVE_POS_MATCH_OR_ZV
7927 - when TO_POS or ZV was reached.
7928
7929 MOVE_X_REACHED
7930 -when TO_X was reached before TO_POS or ZV were reached.
7931
7932 MOVE_LINE_CONTINUED
7933 - when we reached the end of the display area and the line must
7934 be continued.
7935
7936 MOVE_LINE_TRUNCATED
7937 - when we reached the end of the display area and the line is
7938 truncated.
7939
7940 MOVE_NEWLINE_OR_CR
7941 - when we stopped at a line end, i.e. a newline or a CR and selective
7942 display is on. */
7943
7944 static enum move_it_result
7945 move_it_in_display_line_to (struct it *it,
7946 ptrdiff_t to_charpos, int to_x,
7947 enum move_operation_enum op)
7948 {
7949 enum move_it_result result = MOVE_UNDEFINED;
7950 struct glyph_row *saved_glyph_row;
7951 struct it wrap_it, atpos_it, atx_it, ppos_it;
7952 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7953 void *ppos_data = NULL;
7954 int may_wrap = 0;
7955 enum it_method prev_method = it->method;
7956 ptrdiff_t prev_pos = IT_CHARPOS (*it);
7957 int saw_smaller_pos = prev_pos < to_charpos;
7958
7959 /* Don't produce glyphs in produce_glyphs. */
7960 saved_glyph_row = it->glyph_row;
7961 it->glyph_row = NULL;
7962
7963 /* Use wrap_it to save a copy of IT wherever a word wrap could
7964 occur. Use atpos_it to save a copy of IT at the desired buffer
7965 position, if found, so that we can scan ahead and check if the
7966 word later overshoots the window edge. Use atx_it similarly, for
7967 pixel positions. */
7968 wrap_it.sp = -1;
7969 atpos_it.sp = -1;
7970 atx_it.sp = -1;
7971
7972 /* Use ppos_it under bidi reordering to save a copy of IT for the
7973 position > CHARPOS that is the closest to CHARPOS. We restore
7974 that position in IT when we have scanned the entire display line
7975 without finding a match for CHARPOS and all the character
7976 positions are greater than CHARPOS. */
7977 if (it->bidi_p)
7978 {
7979 SAVE_IT (ppos_it, *it, ppos_data);
7980 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7981 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7982 SAVE_IT (ppos_it, *it, ppos_data);
7983 }
7984
7985 #define BUFFER_POS_REACHED_P() \
7986 ((op & MOVE_TO_POS) != 0 \
7987 && BUFFERP (it->object) \
7988 && (IT_CHARPOS (*it) == to_charpos \
7989 || ((!it->bidi_p \
7990 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7991 && IT_CHARPOS (*it) > to_charpos) \
7992 || (it->what == IT_COMPOSITION \
7993 && ((IT_CHARPOS (*it) > to_charpos \
7994 && to_charpos >= it->cmp_it.charpos) \
7995 || (IT_CHARPOS (*it) < to_charpos \
7996 && to_charpos <= it->cmp_it.charpos)))) \
7997 && (it->method == GET_FROM_BUFFER \
7998 || (it->method == GET_FROM_DISPLAY_VECTOR \
7999 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8000
8001 /* If there's a line-/wrap-prefix, handle it. */
8002 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8003 && it->current_y < it->last_visible_y)
8004 handle_line_prefix (it);
8005
8006 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8007 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8008
8009 while (1)
8010 {
8011 int x, i, ascent = 0, descent = 0;
8012
8013 /* Utility macro to reset an iterator with x, ascent, and descent. */
8014 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8015 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8016 (IT)->max_descent = descent)
8017
8018 /* Stop if we move beyond TO_CHARPOS (after an image or a
8019 display string or stretch glyph). */
8020 if ((op & MOVE_TO_POS) != 0
8021 && BUFFERP (it->object)
8022 && it->method == GET_FROM_BUFFER
8023 && (((!it->bidi_p
8024 /* When the iterator is at base embedding level, we
8025 are guaranteed that characters are delivered for
8026 display in strictly increasing order of their
8027 buffer positions. */
8028 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8029 && IT_CHARPOS (*it) > to_charpos)
8030 || (it->bidi_p
8031 && (prev_method == GET_FROM_IMAGE
8032 || prev_method == GET_FROM_STRETCH
8033 || prev_method == GET_FROM_STRING)
8034 /* Passed TO_CHARPOS from left to right. */
8035 && ((prev_pos < to_charpos
8036 && IT_CHARPOS (*it) > to_charpos)
8037 /* Passed TO_CHARPOS from right to left. */
8038 || (prev_pos > to_charpos
8039 && IT_CHARPOS (*it) < to_charpos)))))
8040 {
8041 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8042 {
8043 result = MOVE_POS_MATCH_OR_ZV;
8044 break;
8045 }
8046 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8047 /* If wrap_it is valid, the current position might be in a
8048 word that is wrapped. So, save the iterator in
8049 atpos_it and continue to see if wrapping happens. */
8050 SAVE_IT (atpos_it, *it, atpos_data);
8051 }
8052
8053 /* Stop when ZV reached.
8054 We used to stop here when TO_CHARPOS reached as well, but that is
8055 too soon if this glyph does not fit on this line. So we handle it
8056 explicitly below. */
8057 if (!get_next_display_element (it))
8058 {
8059 result = MOVE_POS_MATCH_OR_ZV;
8060 break;
8061 }
8062
8063 if (it->line_wrap == TRUNCATE)
8064 {
8065 if (BUFFER_POS_REACHED_P ())
8066 {
8067 result = MOVE_POS_MATCH_OR_ZV;
8068 break;
8069 }
8070 }
8071 else
8072 {
8073 if (it->line_wrap == WORD_WRAP)
8074 {
8075 if (IT_DISPLAYING_WHITESPACE (it))
8076 may_wrap = 1;
8077 else if (may_wrap)
8078 {
8079 /* We have reached a glyph that follows one or more
8080 whitespace characters. If the position is
8081 already found, we are done. */
8082 if (atpos_it.sp >= 0)
8083 {
8084 RESTORE_IT (it, &atpos_it, atpos_data);
8085 result = MOVE_POS_MATCH_OR_ZV;
8086 goto done;
8087 }
8088 if (atx_it.sp >= 0)
8089 {
8090 RESTORE_IT (it, &atx_it, atx_data);
8091 result = MOVE_X_REACHED;
8092 goto done;
8093 }
8094 /* Otherwise, we can wrap here. */
8095 SAVE_IT (wrap_it, *it, wrap_data);
8096 may_wrap = 0;
8097 }
8098 }
8099 }
8100
8101 /* Remember the line height for the current line, in case
8102 the next element doesn't fit on the line. */
8103 ascent = it->max_ascent;
8104 descent = it->max_descent;
8105
8106 /* The call to produce_glyphs will get the metrics of the
8107 display element IT is loaded with. Record the x-position
8108 before this display element, in case it doesn't fit on the
8109 line. */
8110 x = it->current_x;
8111
8112 PRODUCE_GLYPHS (it);
8113
8114 if (it->area != TEXT_AREA)
8115 {
8116 prev_method = it->method;
8117 if (it->method == GET_FROM_BUFFER)
8118 prev_pos = IT_CHARPOS (*it);
8119 set_iterator_to_next (it, 1);
8120 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8121 SET_TEXT_POS (this_line_min_pos,
8122 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8123 if (it->bidi_p
8124 && (op & MOVE_TO_POS)
8125 && IT_CHARPOS (*it) > to_charpos
8126 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8127 SAVE_IT (ppos_it, *it, ppos_data);
8128 continue;
8129 }
8130
8131 /* The number of glyphs we get back in IT->nglyphs will normally
8132 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8133 character on a terminal frame, or (iii) a line end. For the
8134 second case, IT->nglyphs - 1 padding glyphs will be present.
8135 (On X frames, there is only one glyph produced for a
8136 composite character.)
8137
8138 The behavior implemented below means, for continuation lines,
8139 that as many spaces of a TAB as fit on the current line are
8140 displayed there. For terminal frames, as many glyphs of a
8141 multi-glyph character are displayed in the current line, too.
8142 This is what the old redisplay code did, and we keep it that
8143 way. Under X, the whole shape of a complex character must
8144 fit on the line or it will be completely displayed in the
8145 next line.
8146
8147 Note that both for tabs and padding glyphs, all glyphs have
8148 the same width. */
8149 if (it->nglyphs)
8150 {
8151 /* More than one glyph or glyph doesn't fit on line. All
8152 glyphs have the same width. */
8153 int single_glyph_width = it->pixel_width / it->nglyphs;
8154 int new_x;
8155 int x_before_this_char = x;
8156 int hpos_before_this_char = it->hpos;
8157
8158 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8159 {
8160 new_x = x + single_glyph_width;
8161
8162 /* We want to leave anything reaching TO_X to the caller. */
8163 if ((op & MOVE_TO_X) && new_x > to_x)
8164 {
8165 if (BUFFER_POS_REACHED_P ())
8166 {
8167 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8168 goto buffer_pos_reached;
8169 if (atpos_it.sp < 0)
8170 {
8171 SAVE_IT (atpos_it, *it, atpos_data);
8172 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8173 }
8174 }
8175 else
8176 {
8177 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8178 {
8179 it->current_x = x;
8180 result = MOVE_X_REACHED;
8181 break;
8182 }
8183 if (atx_it.sp < 0)
8184 {
8185 SAVE_IT (atx_it, *it, atx_data);
8186 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8187 }
8188 }
8189 }
8190
8191 if (/* Lines are continued. */
8192 it->line_wrap != TRUNCATE
8193 && (/* And glyph doesn't fit on the line. */
8194 new_x > it->last_visible_x
8195 /* Or it fits exactly and we're on a window
8196 system frame. */
8197 || (new_x == it->last_visible_x
8198 && FRAME_WINDOW_P (it->f))))
8199 {
8200 if (/* IT->hpos == 0 means the very first glyph
8201 doesn't fit on the line, e.g. a wide image. */
8202 it->hpos == 0
8203 || (new_x == it->last_visible_x
8204 && FRAME_WINDOW_P (it->f)))
8205 {
8206 ++it->hpos;
8207 it->current_x = new_x;
8208
8209 /* The character's last glyph just barely fits
8210 in this row. */
8211 if (i == it->nglyphs - 1)
8212 {
8213 /* If this is the destination position,
8214 return a position *before* it in this row,
8215 now that we know it fits in this row. */
8216 if (BUFFER_POS_REACHED_P ())
8217 {
8218 if (it->line_wrap != WORD_WRAP
8219 || wrap_it.sp < 0)
8220 {
8221 it->hpos = hpos_before_this_char;
8222 it->current_x = x_before_this_char;
8223 result = MOVE_POS_MATCH_OR_ZV;
8224 break;
8225 }
8226 if (it->line_wrap == WORD_WRAP
8227 && atpos_it.sp < 0)
8228 {
8229 SAVE_IT (atpos_it, *it, atpos_data);
8230 atpos_it.current_x = x_before_this_char;
8231 atpos_it.hpos = hpos_before_this_char;
8232 }
8233 }
8234
8235 prev_method = it->method;
8236 if (it->method == GET_FROM_BUFFER)
8237 prev_pos = IT_CHARPOS (*it);
8238 set_iterator_to_next (it, 1);
8239 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8240 SET_TEXT_POS (this_line_min_pos,
8241 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8242 /* On graphical terminals, newlines may
8243 "overflow" into the fringe if
8244 overflow-newline-into-fringe is non-nil.
8245 On text-only terminals, newlines may
8246 overflow into the last glyph on the
8247 display line.*/
8248 if (!FRAME_WINDOW_P (it->f)
8249 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8250 {
8251 if (!get_next_display_element (it))
8252 {
8253 result = MOVE_POS_MATCH_OR_ZV;
8254 break;
8255 }
8256 if (BUFFER_POS_REACHED_P ())
8257 {
8258 if (ITERATOR_AT_END_OF_LINE_P (it))
8259 result = MOVE_POS_MATCH_OR_ZV;
8260 else
8261 result = MOVE_LINE_CONTINUED;
8262 break;
8263 }
8264 if (ITERATOR_AT_END_OF_LINE_P (it))
8265 {
8266 result = MOVE_NEWLINE_OR_CR;
8267 break;
8268 }
8269 }
8270 }
8271 }
8272 else
8273 IT_RESET_X_ASCENT_DESCENT (it);
8274
8275 if (wrap_it.sp >= 0)
8276 {
8277 RESTORE_IT (it, &wrap_it, wrap_data);
8278 atpos_it.sp = -1;
8279 atx_it.sp = -1;
8280 }
8281
8282 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8283 IT_CHARPOS (*it)));
8284 result = MOVE_LINE_CONTINUED;
8285 break;
8286 }
8287
8288 if (BUFFER_POS_REACHED_P ())
8289 {
8290 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8291 goto buffer_pos_reached;
8292 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8293 {
8294 SAVE_IT (atpos_it, *it, atpos_data);
8295 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8296 }
8297 }
8298
8299 if (new_x > it->first_visible_x)
8300 {
8301 /* Glyph is visible. Increment number of glyphs that
8302 would be displayed. */
8303 ++it->hpos;
8304 }
8305 }
8306
8307 if (result != MOVE_UNDEFINED)
8308 break;
8309 }
8310 else if (BUFFER_POS_REACHED_P ())
8311 {
8312 buffer_pos_reached:
8313 IT_RESET_X_ASCENT_DESCENT (it);
8314 result = MOVE_POS_MATCH_OR_ZV;
8315 break;
8316 }
8317 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8318 {
8319 /* Stop when TO_X specified and reached. This check is
8320 necessary here because of lines consisting of a line end,
8321 only. The line end will not produce any glyphs and we
8322 would never get MOVE_X_REACHED. */
8323 xassert (it->nglyphs == 0);
8324 result = MOVE_X_REACHED;
8325 break;
8326 }
8327
8328 /* Is this a line end? If yes, we're done. */
8329 if (ITERATOR_AT_END_OF_LINE_P (it))
8330 {
8331 /* If we are past TO_CHARPOS, but never saw any character
8332 positions smaller than TO_CHARPOS, return
8333 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8334 did. */
8335 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8336 {
8337 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8338 {
8339 if (IT_CHARPOS (ppos_it) < ZV)
8340 {
8341 RESTORE_IT (it, &ppos_it, ppos_data);
8342 result = MOVE_POS_MATCH_OR_ZV;
8343 }
8344 else
8345 goto buffer_pos_reached;
8346 }
8347 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8348 && IT_CHARPOS (*it) > to_charpos)
8349 goto buffer_pos_reached;
8350 else
8351 result = MOVE_NEWLINE_OR_CR;
8352 }
8353 else
8354 result = MOVE_NEWLINE_OR_CR;
8355 break;
8356 }
8357
8358 prev_method = it->method;
8359 if (it->method == GET_FROM_BUFFER)
8360 prev_pos = IT_CHARPOS (*it);
8361 /* The current display element has been consumed. Advance
8362 to the next. */
8363 set_iterator_to_next (it, 1);
8364 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8365 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8366 if (IT_CHARPOS (*it) < to_charpos)
8367 saw_smaller_pos = 1;
8368 if (it->bidi_p
8369 && (op & MOVE_TO_POS)
8370 && IT_CHARPOS (*it) >= to_charpos
8371 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8372 SAVE_IT (ppos_it, *it, ppos_data);
8373
8374 /* Stop if lines are truncated and IT's current x-position is
8375 past the right edge of the window now. */
8376 if (it->line_wrap == TRUNCATE
8377 && it->current_x >= it->last_visible_x)
8378 {
8379 if (!FRAME_WINDOW_P (it->f)
8380 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8381 {
8382 int at_eob_p = 0;
8383
8384 if ((at_eob_p = !get_next_display_element (it))
8385 || BUFFER_POS_REACHED_P ()
8386 /* If we are past TO_CHARPOS, but never saw any
8387 character positions smaller than TO_CHARPOS,
8388 return MOVE_POS_MATCH_OR_ZV, like the
8389 unidirectional display did. */
8390 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8391 && !saw_smaller_pos
8392 && IT_CHARPOS (*it) > to_charpos))
8393 {
8394 if (it->bidi_p
8395 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8396 RESTORE_IT (it, &ppos_it, ppos_data);
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 break;
8399 }
8400 if (ITERATOR_AT_END_OF_LINE_P (it))
8401 {
8402 result = MOVE_NEWLINE_OR_CR;
8403 break;
8404 }
8405 }
8406 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8407 && !saw_smaller_pos
8408 && IT_CHARPOS (*it) > to_charpos)
8409 {
8410 if (IT_CHARPOS (ppos_it) < ZV)
8411 RESTORE_IT (it, &ppos_it, ppos_data);
8412 result = MOVE_POS_MATCH_OR_ZV;
8413 break;
8414 }
8415 result = MOVE_LINE_TRUNCATED;
8416 break;
8417 }
8418 #undef IT_RESET_X_ASCENT_DESCENT
8419 }
8420
8421 #undef BUFFER_POS_REACHED_P
8422
8423 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8424 restore the saved iterator. */
8425 if (atpos_it.sp >= 0)
8426 RESTORE_IT (it, &atpos_it, atpos_data);
8427 else if (atx_it.sp >= 0)
8428 RESTORE_IT (it, &atx_it, atx_data);
8429
8430 done:
8431
8432 if (atpos_data)
8433 bidi_unshelve_cache (atpos_data, 1);
8434 if (atx_data)
8435 bidi_unshelve_cache (atx_data, 1);
8436 if (wrap_data)
8437 bidi_unshelve_cache (wrap_data, 1);
8438 if (ppos_data)
8439 bidi_unshelve_cache (ppos_data, 1);
8440
8441 /* Restore the iterator settings altered at the beginning of this
8442 function. */
8443 it->glyph_row = saved_glyph_row;
8444 return result;
8445 }
8446
8447 /* For external use. */
8448 void
8449 move_it_in_display_line (struct it *it,
8450 ptrdiff_t to_charpos, int to_x,
8451 enum move_operation_enum op)
8452 {
8453 if (it->line_wrap == WORD_WRAP
8454 && (op & MOVE_TO_X))
8455 {
8456 struct it save_it;
8457 void *save_data = NULL;
8458 int skip;
8459
8460 SAVE_IT (save_it, *it, save_data);
8461 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8462 /* When word-wrap is on, TO_X may lie past the end
8463 of a wrapped line. Then it->current is the
8464 character on the next line, so backtrack to the
8465 space before the wrap point. */
8466 if (skip == MOVE_LINE_CONTINUED)
8467 {
8468 int prev_x = max (it->current_x - 1, 0);
8469 RESTORE_IT (it, &save_it, save_data);
8470 move_it_in_display_line_to
8471 (it, -1, prev_x, MOVE_TO_X);
8472 }
8473 else
8474 bidi_unshelve_cache (save_data, 1);
8475 }
8476 else
8477 move_it_in_display_line_to (it, to_charpos, to_x, op);
8478 }
8479
8480
8481 /* Move IT forward until it satisfies one or more of the criteria in
8482 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8483
8484 OP is a bit-mask that specifies where to stop, and in particular,
8485 which of those four position arguments makes a difference. See the
8486 description of enum move_operation_enum.
8487
8488 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8489 screen line, this function will set IT to the next position that is
8490 displayed to the right of TO_CHARPOS on the screen. */
8491
8492 void
8493 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8494 {
8495 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8496 int line_height, line_start_x = 0, reached = 0;
8497 void *backup_data = NULL;
8498
8499 for (;;)
8500 {
8501 if (op & MOVE_TO_VPOS)
8502 {
8503 /* If no TO_CHARPOS and no TO_X specified, stop at the
8504 start of the line TO_VPOS. */
8505 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8506 {
8507 if (it->vpos == to_vpos)
8508 {
8509 reached = 1;
8510 break;
8511 }
8512 else
8513 skip = move_it_in_display_line_to (it, -1, -1, 0);
8514 }
8515 else
8516 {
8517 /* TO_VPOS >= 0 means stop at TO_X in the line at
8518 TO_VPOS, or at TO_POS, whichever comes first. */
8519 if (it->vpos == to_vpos)
8520 {
8521 reached = 2;
8522 break;
8523 }
8524
8525 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8526
8527 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8528 {
8529 reached = 3;
8530 break;
8531 }
8532 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8533 {
8534 /* We have reached TO_X but not in the line we want. */
8535 skip = move_it_in_display_line_to (it, to_charpos,
8536 -1, MOVE_TO_POS);
8537 if (skip == MOVE_POS_MATCH_OR_ZV)
8538 {
8539 reached = 4;
8540 break;
8541 }
8542 }
8543 }
8544 }
8545 else if (op & MOVE_TO_Y)
8546 {
8547 struct it it_backup;
8548
8549 if (it->line_wrap == WORD_WRAP)
8550 SAVE_IT (it_backup, *it, backup_data);
8551
8552 /* TO_Y specified means stop at TO_X in the line containing
8553 TO_Y---or at TO_CHARPOS if this is reached first. The
8554 problem is that we can't really tell whether the line
8555 contains TO_Y before we have completely scanned it, and
8556 this may skip past TO_X. What we do is to first scan to
8557 TO_X.
8558
8559 If TO_X is not specified, use a TO_X of zero. The reason
8560 is to make the outcome of this function more predictable.
8561 If we didn't use TO_X == 0, we would stop at the end of
8562 the line which is probably not what a caller would expect
8563 to happen. */
8564 skip = move_it_in_display_line_to
8565 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8566 (MOVE_TO_X | (op & MOVE_TO_POS)));
8567
8568 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8569 if (skip == MOVE_POS_MATCH_OR_ZV)
8570 reached = 5;
8571 else if (skip == MOVE_X_REACHED)
8572 {
8573 /* If TO_X was reached, we want to know whether TO_Y is
8574 in the line. We know this is the case if the already
8575 scanned glyphs make the line tall enough. Otherwise,
8576 we must check by scanning the rest of the line. */
8577 line_height = it->max_ascent + it->max_descent;
8578 if (to_y >= it->current_y
8579 && to_y < it->current_y + line_height)
8580 {
8581 reached = 6;
8582 break;
8583 }
8584 SAVE_IT (it_backup, *it, backup_data);
8585 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8586 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8587 op & MOVE_TO_POS);
8588 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8589 line_height = it->max_ascent + it->max_descent;
8590 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8591
8592 if (to_y >= it->current_y
8593 && to_y < it->current_y + line_height)
8594 {
8595 /* If TO_Y is in this line and TO_X was reached
8596 above, we scanned too far. We have to restore
8597 IT's settings to the ones before skipping. */
8598 RESTORE_IT (it, &it_backup, backup_data);
8599 reached = 6;
8600 }
8601 else
8602 {
8603 skip = skip2;
8604 if (skip == MOVE_POS_MATCH_OR_ZV)
8605 reached = 7;
8606 }
8607 }
8608 else
8609 {
8610 /* Check whether TO_Y is in this line. */
8611 line_height = it->max_ascent + it->max_descent;
8612 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8613
8614 if (to_y >= it->current_y
8615 && to_y < it->current_y + line_height)
8616 {
8617 /* When word-wrap is on, TO_X may lie past the end
8618 of a wrapped line. Then it->current is the
8619 character on the next line, so backtrack to the
8620 space before the wrap point. */
8621 if (skip == MOVE_LINE_CONTINUED
8622 && it->line_wrap == WORD_WRAP)
8623 {
8624 int prev_x = max (it->current_x - 1, 0);
8625 RESTORE_IT (it, &it_backup, backup_data);
8626 skip = move_it_in_display_line_to
8627 (it, -1, prev_x, MOVE_TO_X);
8628 }
8629 reached = 6;
8630 }
8631 }
8632
8633 if (reached)
8634 break;
8635 }
8636 else if (BUFFERP (it->object)
8637 && (it->method == GET_FROM_BUFFER
8638 || it->method == GET_FROM_STRETCH)
8639 && IT_CHARPOS (*it) >= to_charpos
8640 /* Under bidi iteration, a call to set_iterator_to_next
8641 can scan far beyond to_charpos if the initial
8642 portion of the next line needs to be reordered. In
8643 that case, give move_it_in_display_line_to another
8644 chance below. */
8645 && !(it->bidi_p
8646 && it->bidi_it.scan_dir == -1))
8647 skip = MOVE_POS_MATCH_OR_ZV;
8648 else
8649 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8650
8651 switch (skip)
8652 {
8653 case MOVE_POS_MATCH_OR_ZV:
8654 reached = 8;
8655 goto out;
8656
8657 case MOVE_NEWLINE_OR_CR:
8658 set_iterator_to_next (it, 1);
8659 it->continuation_lines_width = 0;
8660 break;
8661
8662 case MOVE_LINE_TRUNCATED:
8663 it->continuation_lines_width = 0;
8664 reseat_at_next_visible_line_start (it, 0);
8665 if ((op & MOVE_TO_POS) != 0
8666 && IT_CHARPOS (*it) > to_charpos)
8667 {
8668 reached = 9;
8669 goto out;
8670 }
8671 break;
8672
8673 case MOVE_LINE_CONTINUED:
8674 /* For continued lines ending in a tab, some of the glyphs
8675 associated with the tab are displayed on the current
8676 line. Since it->current_x does not include these glyphs,
8677 we use it->last_visible_x instead. */
8678 if (it->c == '\t')
8679 {
8680 it->continuation_lines_width += it->last_visible_x;
8681 /* When moving by vpos, ensure that the iterator really
8682 advances to the next line (bug#847, bug#969). Fixme:
8683 do we need to do this in other circumstances? */
8684 if (it->current_x != it->last_visible_x
8685 && (op & MOVE_TO_VPOS)
8686 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8687 {
8688 line_start_x = it->current_x + it->pixel_width
8689 - it->last_visible_x;
8690 set_iterator_to_next (it, 0);
8691 }
8692 }
8693 else
8694 it->continuation_lines_width += it->current_x;
8695 break;
8696
8697 default:
8698 abort ();
8699 }
8700
8701 /* Reset/increment for the next run. */
8702 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8703 it->current_x = line_start_x;
8704 line_start_x = 0;
8705 it->hpos = 0;
8706 it->current_y += it->max_ascent + it->max_descent;
8707 ++it->vpos;
8708 last_height = it->max_ascent + it->max_descent;
8709 last_max_ascent = it->max_ascent;
8710 it->max_ascent = it->max_descent = 0;
8711 }
8712
8713 out:
8714
8715 /* On text terminals, we may stop at the end of a line in the middle
8716 of a multi-character glyph. If the glyph itself is continued,
8717 i.e. it is actually displayed on the next line, don't treat this
8718 stopping point as valid; move to the next line instead (unless
8719 that brings us offscreen). */
8720 if (!FRAME_WINDOW_P (it->f)
8721 && op & MOVE_TO_POS
8722 && IT_CHARPOS (*it) == to_charpos
8723 && it->what == IT_CHARACTER
8724 && it->nglyphs > 1
8725 && it->line_wrap == WINDOW_WRAP
8726 && it->current_x == it->last_visible_x - 1
8727 && it->c != '\n'
8728 && it->c != '\t'
8729 && it->vpos < XFASTINT (it->w->window_end_vpos))
8730 {
8731 it->continuation_lines_width += it->current_x;
8732 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8733 it->current_y += it->max_ascent + it->max_descent;
8734 ++it->vpos;
8735 last_height = it->max_ascent + it->max_descent;
8736 last_max_ascent = it->max_ascent;
8737 }
8738
8739 if (backup_data)
8740 bidi_unshelve_cache (backup_data, 1);
8741
8742 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8743 }
8744
8745
8746 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8747
8748 If DY > 0, move IT backward at least that many pixels. DY = 0
8749 means move IT backward to the preceding line start or BEGV. This
8750 function may move over more than DY pixels if IT->current_y - DY
8751 ends up in the middle of a line; in this case IT->current_y will be
8752 set to the top of the line moved to. */
8753
8754 void
8755 move_it_vertically_backward (struct it *it, int dy)
8756 {
8757 int nlines, h;
8758 struct it it2, it3;
8759 void *it2data = NULL, *it3data = NULL;
8760 ptrdiff_t start_pos;
8761
8762 move_further_back:
8763 xassert (dy >= 0);
8764
8765 start_pos = IT_CHARPOS (*it);
8766
8767 /* Estimate how many newlines we must move back. */
8768 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8769
8770 /* Set the iterator's position that many lines back. */
8771 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8772 back_to_previous_visible_line_start (it);
8773
8774 /* Reseat the iterator here. When moving backward, we don't want
8775 reseat to skip forward over invisible text, set up the iterator
8776 to deliver from overlay strings at the new position etc. So,
8777 use reseat_1 here. */
8778 reseat_1 (it, it->current.pos, 1);
8779
8780 /* We are now surely at a line start. */
8781 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8782 reordering is in effect. */
8783 it->continuation_lines_width = 0;
8784
8785 /* Move forward and see what y-distance we moved. First move to the
8786 start of the next line so that we get its height. We need this
8787 height to be able to tell whether we reached the specified
8788 y-distance. */
8789 SAVE_IT (it2, *it, it2data);
8790 it2.max_ascent = it2.max_descent = 0;
8791 do
8792 {
8793 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8794 MOVE_TO_POS | MOVE_TO_VPOS);
8795 }
8796 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8797 /* If we are in a display string which starts at START_POS,
8798 and that display string includes a newline, and we are
8799 right after that newline (i.e. at the beginning of a
8800 display line), exit the loop, because otherwise we will
8801 infloop, since move_it_to will see that it is already at
8802 START_POS and will not move. */
8803 || (it2.method == GET_FROM_STRING
8804 && IT_CHARPOS (it2) == start_pos
8805 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8806 xassert (IT_CHARPOS (*it) >= BEGV);
8807 SAVE_IT (it3, it2, it3data);
8808
8809 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8810 xassert (IT_CHARPOS (*it) >= BEGV);
8811 /* H is the actual vertical distance from the position in *IT
8812 and the starting position. */
8813 h = it2.current_y - it->current_y;
8814 /* NLINES is the distance in number of lines. */
8815 nlines = it2.vpos - it->vpos;
8816
8817 /* Correct IT's y and vpos position
8818 so that they are relative to the starting point. */
8819 it->vpos -= nlines;
8820 it->current_y -= h;
8821
8822 if (dy == 0)
8823 {
8824 /* DY == 0 means move to the start of the screen line. The
8825 value of nlines is > 0 if continuation lines were involved,
8826 or if the original IT position was at start of a line. */
8827 RESTORE_IT (it, it, it2data);
8828 if (nlines > 0)
8829 move_it_by_lines (it, nlines);
8830 /* The above code moves us to some position NLINES down,
8831 usually to its first glyph (leftmost in an L2R line), but
8832 that's not necessarily the start of the line, under bidi
8833 reordering. We want to get to the character position
8834 that is immediately after the newline of the previous
8835 line. */
8836 if (it->bidi_p
8837 && !it->continuation_lines_width
8838 && !STRINGP (it->string)
8839 && IT_CHARPOS (*it) > BEGV
8840 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8841 {
8842 ptrdiff_t nl_pos =
8843 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8844
8845 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8846 }
8847 bidi_unshelve_cache (it3data, 1);
8848 }
8849 else
8850 {
8851 /* The y-position we try to reach, relative to *IT.
8852 Note that H has been subtracted in front of the if-statement. */
8853 int target_y = it->current_y + h - dy;
8854 int y0 = it3.current_y;
8855 int y1;
8856 int line_height;
8857
8858 RESTORE_IT (&it3, &it3, it3data);
8859 y1 = line_bottom_y (&it3);
8860 line_height = y1 - y0;
8861 RESTORE_IT (it, it, it2data);
8862 /* If we did not reach target_y, try to move further backward if
8863 we can. If we moved too far backward, try to move forward. */
8864 if (target_y < it->current_y
8865 /* This is heuristic. In a window that's 3 lines high, with
8866 a line height of 13 pixels each, recentering with point
8867 on the bottom line will try to move -39/2 = 19 pixels
8868 backward. Try to avoid moving into the first line. */
8869 && (it->current_y - target_y
8870 > min (window_box_height (it->w), line_height * 2 / 3))
8871 && IT_CHARPOS (*it) > BEGV)
8872 {
8873 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8874 target_y - it->current_y));
8875 dy = it->current_y - target_y;
8876 goto move_further_back;
8877 }
8878 else if (target_y >= it->current_y + line_height
8879 && IT_CHARPOS (*it) < ZV)
8880 {
8881 /* Should move forward by at least one line, maybe more.
8882
8883 Note: Calling move_it_by_lines can be expensive on
8884 terminal frames, where compute_motion is used (via
8885 vmotion) to do the job, when there are very long lines
8886 and truncate-lines is nil. That's the reason for
8887 treating terminal frames specially here. */
8888
8889 if (!FRAME_WINDOW_P (it->f))
8890 move_it_vertically (it, target_y - (it->current_y + line_height));
8891 else
8892 {
8893 do
8894 {
8895 move_it_by_lines (it, 1);
8896 }
8897 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8898 }
8899 }
8900 }
8901 }
8902
8903
8904 /* Move IT by a specified amount of pixel lines DY. DY negative means
8905 move backwards. DY = 0 means move to start of screen line. At the
8906 end, IT will be on the start of a screen line. */
8907
8908 void
8909 move_it_vertically (struct it *it, int dy)
8910 {
8911 if (dy <= 0)
8912 move_it_vertically_backward (it, -dy);
8913 else
8914 {
8915 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8916 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8917 MOVE_TO_POS | MOVE_TO_Y);
8918 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8919
8920 /* If buffer ends in ZV without a newline, move to the start of
8921 the line to satisfy the post-condition. */
8922 if (IT_CHARPOS (*it) == ZV
8923 && ZV > BEGV
8924 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8925 move_it_by_lines (it, 0);
8926 }
8927 }
8928
8929
8930 /* Move iterator IT past the end of the text line it is in. */
8931
8932 void
8933 move_it_past_eol (struct it *it)
8934 {
8935 enum move_it_result rc;
8936
8937 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8938 if (rc == MOVE_NEWLINE_OR_CR)
8939 set_iterator_to_next (it, 0);
8940 }
8941
8942
8943 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8944 negative means move up. DVPOS == 0 means move to the start of the
8945 screen line.
8946
8947 Optimization idea: If we would know that IT->f doesn't use
8948 a face with proportional font, we could be faster for
8949 truncate-lines nil. */
8950
8951 void
8952 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
8953 {
8954
8955 /* The commented-out optimization uses vmotion on terminals. This
8956 gives bad results, because elements like it->what, on which
8957 callers such as pos_visible_p rely, aren't updated. */
8958 /* struct position pos;
8959 if (!FRAME_WINDOW_P (it->f))
8960 {
8961 struct text_pos textpos;
8962
8963 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8964 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8965 reseat (it, textpos, 1);
8966 it->vpos += pos.vpos;
8967 it->current_y += pos.vpos;
8968 }
8969 else */
8970
8971 if (dvpos == 0)
8972 {
8973 /* DVPOS == 0 means move to the start of the screen line. */
8974 move_it_vertically_backward (it, 0);
8975 /* Let next call to line_bottom_y calculate real line height */
8976 last_height = 0;
8977 }
8978 else if (dvpos > 0)
8979 {
8980 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8981 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8982 {
8983 /* Only move to the next buffer position if we ended up in a
8984 string from display property, not in an overlay string
8985 (before-string or after-string). That is because the
8986 latter don't conceal the underlying buffer position, so
8987 we can ask to move the iterator to the exact position we
8988 are interested in. Note that, even if we are already at
8989 IT_CHARPOS (*it), the call below is not a no-op, as it
8990 will detect that we are at the end of the string, pop the
8991 iterator, and compute it->current_x and it->hpos
8992 correctly. */
8993 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
8994 -1, -1, -1, MOVE_TO_POS);
8995 }
8996 }
8997 else
8998 {
8999 struct it it2;
9000 void *it2data = NULL;
9001 ptrdiff_t start_charpos, i;
9002
9003 /* Start at the beginning of the screen line containing IT's
9004 position. This may actually move vertically backwards,
9005 in case of overlays, so adjust dvpos accordingly. */
9006 dvpos += it->vpos;
9007 move_it_vertically_backward (it, 0);
9008 dvpos -= it->vpos;
9009
9010 /* Go back -DVPOS visible lines and reseat the iterator there. */
9011 start_charpos = IT_CHARPOS (*it);
9012 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9013 back_to_previous_visible_line_start (it);
9014 reseat (it, it->current.pos, 1);
9015
9016 /* Move further back if we end up in a string or an image. */
9017 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9018 {
9019 /* First try to move to start of display line. */
9020 dvpos += it->vpos;
9021 move_it_vertically_backward (it, 0);
9022 dvpos -= it->vpos;
9023 if (IT_POS_VALID_AFTER_MOVE_P (it))
9024 break;
9025 /* If start of line is still in string or image,
9026 move further back. */
9027 back_to_previous_visible_line_start (it);
9028 reseat (it, it->current.pos, 1);
9029 dvpos--;
9030 }
9031
9032 it->current_x = it->hpos = 0;
9033
9034 /* Above call may have moved too far if continuation lines
9035 are involved. Scan forward and see if it did. */
9036 SAVE_IT (it2, *it, it2data);
9037 it2.vpos = it2.current_y = 0;
9038 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9039 it->vpos -= it2.vpos;
9040 it->current_y -= it2.current_y;
9041 it->current_x = it->hpos = 0;
9042
9043 /* If we moved too far back, move IT some lines forward. */
9044 if (it2.vpos > -dvpos)
9045 {
9046 int delta = it2.vpos + dvpos;
9047
9048 RESTORE_IT (&it2, &it2, it2data);
9049 SAVE_IT (it2, *it, it2data);
9050 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9051 /* Move back again if we got too far ahead. */
9052 if (IT_CHARPOS (*it) >= start_charpos)
9053 RESTORE_IT (it, &it2, it2data);
9054 else
9055 bidi_unshelve_cache (it2data, 1);
9056 }
9057 else
9058 RESTORE_IT (it, it, it2data);
9059 }
9060 }
9061
9062 /* Return 1 if IT points into the middle of a display vector. */
9063
9064 int
9065 in_display_vector_p (struct it *it)
9066 {
9067 return (it->method == GET_FROM_DISPLAY_VECTOR
9068 && it->current.dpvec_index > 0
9069 && it->dpvec + it->current.dpvec_index != it->dpend);
9070 }
9071
9072 \f
9073 /***********************************************************************
9074 Messages
9075 ***********************************************************************/
9076
9077
9078 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9079 to *Messages*. */
9080
9081 void
9082 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9083 {
9084 Lisp_Object args[3];
9085 Lisp_Object msg, fmt;
9086 char *buffer;
9087 ptrdiff_t len;
9088 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9089 USE_SAFE_ALLOCA;
9090
9091 /* Do nothing if called asynchronously. Inserting text into
9092 a buffer may call after-change-functions and alike and
9093 that would means running Lisp asynchronously. */
9094 if (handling_signal)
9095 return;
9096
9097 fmt = msg = Qnil;
9098 GCPRO4 (fmt, msg, arg1, arg2);
9099
9100 args[0] = fmt = build_string (format);
9101 args[1] = arg1;
9102 args[2] = arg2;
9103 msg = Fformat (3, args);
9104
9105 len = SBYTES (msg) + 1;
9106 SAFE_ALLOCA (buffer, char *, len);
9107 memcpy (buffer, SDATA (msg), len);
9108
9109 message_dolog (buffer, len - 1, 1, 0);
9110 SAFE_FREE ();
9111
9112 UNGCPRO;
9113 }
9114
9115
9116 /* Output a newline in the *Messages* buffer if "needs" one. */
9117
9118 void
9119 message_log_maybe_newline (void)
9120 {
9121 if (message_log_need_newline)
9122 message_dolog ("", 0, 1, 0);
9123 }
9124
9125
9126 /* Add a string M of length NBYTES to the message log, optionally
9127 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9128 nonzero, means interpret the contents of M as multibyte. This
9129 function calls low-level routines in order to bypass text property
9130 hooks, etc. which might not be safe to run.
9131
9132 This may GC (insert may run before/after change hooks),
9133 so the buffer M must NOT point to a Lisp string. */
9134
9135 void
9136 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9137 {
9138 const unsigned char *msg = (const unsigned char *) m;
9139
9140 if (!NILP (Vmemory_full))
9141 return;
9142
9143 if (!NILP (Vmessage_log_max))
9144 {
9145 struct buffer *oldbuf;
9146 Lisp_Object oldpoint, oldbegv, oldzv;
9147 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9148 ptrdiff_t point_at_end = 0;
9149 ptrdiff_t zv_at_end = 0;
9150 Lisp_Object old_deactivate_mark, tem;
9151 struct gcpro gcpro1;
9152
9153 old_deactivate_mark = Vdeactivate_mark;
9154 oldbuf = current_buffer;
9155 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9156 BVAR (current_buffer, undo_list) = Qt;
9157
9158 oldpoint = message_dolog_marker1;
9159 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9160 oldbegv = message_dolog_marker2;
9161 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9162 oldzv = message_dolog_marker3;
9163 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9164 GCPRO1 (old_deactivate_mark);
9165
9166 if (PT == Z)
9167 point_at_end = 1;
9168 if (ZV == Z)
9169 zv_at_end = 1;
9170
9171 BEGV = BEG;
9172 BEGV_BYTE = BEG_BYTE;
9173 ZV = Z;
9174 ZV_BYTE = Z_BYTE;
9175 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9176
9177 /* Insert the string--maybe converting multibyte to single byte
9178 or vice versa, so that all the text fits the buffer. */
9179 if (multibyte
9180 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9181 {
9182 ptrdiff_t i;
9183 int c, char_bytes;
9184 char work[1];
9185
9186 /* Convert a multibyte string to single-byte
9187 for the *Message* buffer. */
9188 for (i = 0; i < nbytes; i += char_bytes)
9189 {
9190 c = string_char_and_length (msg + i, &char_bytes);
9191 work[0] = (ASCII_CHAR_P (c)
9192 ? c
9193 : multibyte_char_to_unibyte (c));
9194 insert_1_both (work, 1, 1, 1, 0, 0);
9195 }
9196 }
9197 else if (! multibyte
9198 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9199 {
9200 ptrdiff_t i;
9201 int c, char_bytes;
9202 unsigned char str[MAX_MULTIBYTE_LENGTH];
9203 /* Convert a single-byte string to multibyte
9204 for the *Message* buffer. */
9205 for (i = 0; i < nbytes; i++)
9206 {
9207 c = msg[i];
9208 MAKE_CHAR_MULTIBYTE (c);
9209 char_bytes = CHAR_STRING (c, str);
9210 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9211 }
9212 }
9213 else if (nbytes)
9214 insert_1 (m, nbytes, 1, 0, 0);
9215
9216 if (nlflag)
9217 {
9218 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9219 printmax_t dups;
9220 insert_1 ("\n", 1, 1, 0, 0);
9221
9222 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9223 this_bol = PT;
9224 this_bol_byte = PT_BYTE;
9225
9226 /* See if this line duplicates the previous one.
9227 If so, combine duplicates. */
9228 if (this_bol > BEG)
9229 {
9230 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9231 prev_bol = PT;
9232 prev_bol_byte = PT_BYTE;
9233
9234 dups = message_log_check_duplicate (prev_bol_byte,
9235 this_bol_byte);
9236 if (dups)
9237 {
9238 del_range_both (prev_bol, prev_bol_byte,
9239 this_bol, this_bol_byte, 0);
9240 if (dups > 1)
9241 {
9242 char dupstr[sizeof " [ times]"
9243 + INT_STRLEN_BOUND (printmax_t)];
9244 int duplen;
9245
9246 /* If you change this format, don't forget to also
9247 change message_log_check_duplicate. */
9248 sprintf (dupstr, " [%"pMd" times]", dups);
9249 duplen = strlen (dupstr);
9250 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9251 insert_1 (dupstr, duplen, 1, 0, 1);
9252 }
9253 }
9254 }
9255
9256 /* If we have more than the desired maximum number of lines
9257 in the *Messages* buffer now, delete the oldest ones.
9258 This is safe because we don't have undo in this buffer. */
9259
9260 if (NATNUMP (Vmessage_log_max))
9261 {
9262 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9263 -XFASTINT (Vmessage_log_max) - 1, 0);
9264 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9265 }
9266 }
9267 BEGV = XMARKER (oldbegv)->charpos;
9268 BEGV_BYTE = marker_byte_position (oldbegv);
9269
9270 if (zv_at_end)
9271 {
9272 ZV = Z;
9273 ZV_BYTE = Z_BYTE;
9274 }
9275 else
9276 {
9277 ZV = XMARKER (oldzv)->charpos;
9278 ZV_BYTE = marker_byte_position (oldzv);
9279 }
9280
9281 if (point_at_end)
9282 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9283 else
9284 /* We can't do Fgoto_char (oldpoint) because it will run some
9285 Lisp code. */
9286 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9287 XMARKER (oldpoint)->bytepos);
9288
9289 UNGCPRO;
9290 unchain_marker (XMARKER (oldpoint));
9291 unchain_marker (XMARKER (oldbegv));
9292 unchain_marker (XMARKER (oldzv));
9293
9294 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9295 set_buffer_internal (oldbuf);
9296 if (NILP (tem))
9297 windows_or_buffers_changed = old_windows_or_buffers_changed;
9298 message_log_need_newline = !nlflag;
9299 Vdeactivate_mark = old_deactivate_mark;
9300 }
9301 }
9302
9303
9304 /* We are at the end of the buffer after just having inserted a newline.
9305 (Note: We depend on the fact we won't be crossing the gap.)
9306 Check to see if the most recent message looks a lot like the previous one.
9307 Return 0 if different, 1 if the new one should just replace it, or a
9308 value N > 1 if we should also append " [N times]". */
9309
9310 static intmax_t
9311 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9312 {
9313 ptrdiff_t i;
9314 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9315 int seen_dots = 0;
9316 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9317 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9318
9319 for (i = 0; i < len; i++)
9320 {
9321 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9322 seen_dots = 1;
9323 if (p1[i] != p2[i])
9324 return seen_dots;
9325 }
9326 p1 += len;
9327 if (*p1 == '\n')
9328 return 2;
9329 if (*p1++ == ' ' && *p1++ == '[')
9330 {
9331 char *pend;
9332 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9333 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9334 return n+1;
9335 }
9336 return 0;
9337 }
9338 \f
9339
9340 /* Display an echo area message M with a specified length of NBYTES
9341 bytes. The string may include null characters. If M is 0, clear
9342 out any existing message, and let the mini-buffer text show
9343 through.
9344
9345 This may GC, so the buffer M must NOT point to a Lisp string. */
9346
9347 void
9348 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9349 {
9350 /* First flush out any partial line written with print. */
9351 message_log_maybe_newline ();
9352 if (m)
9353 message_dolog (m, nbytes, 1, multibyte);
9354 message2_nolog (m, nbytes, multibyte);
9355 }
9356
9357
9358 /* The non-logging counterpart of message2. */
9359
9360 void
9361 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9362 {
9363 struct frame *sf = SELECTED_FRAME ();
9364 message_enable_multibyte = multibyte;
9365
9366 if (FRAME_INITIAL_P (sf))
9367 {
9368 if (noninteractive_need_newline)
9369 putc ('\n', stderr);
9370 noninteractive_need_newline = 0;
9371 if (m)
9372 fwrite (m, nbytes, 1, stderr);
9373 if (cursor_in_echo_area == 0)
9374 fprintf (stderr, "\n");
9375 fflush (stderr);
9376 }
9377 /* A null message buffer means that the frame hasn't really been
9378 initialized yet. Error messages get reported properly by
9379 cmd_error, so this must be just an informative message; toss it. */
9380 else if (INTERACTIVE
9381 && sf->glyphs_initialized_p
9382 && FRAME_MESSAGE_BUF (sf))
9383 {
9384 Lisp_Object mini_window;
9385 struct frame *f;
9386
9387 /* Get the frame containing the mini-buffer
9388 that the selected frame is using. */
9389 mini_window = FRAME_MINIBUF_WINDOW (sf);
9390 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9391
9392 FRAME_SAMPLE_VISIBILITY (f);
9393 if (FRAME_VISIBLE_P (sf)
9394 && ! FRAME_VISIBLE_P (f))
9395 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9396
9397 if (m)
9398 {
9399 set_message (m, Qnil, nbytes, multibyte);
9400 if (minibuffer_auto_raise)
9401 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9402 }
9403 else
9404 clear_message (1, 1);
9405
9406 do_pending_window_change (0);
9407 echo_area_display (1);
9408 do_pending_window_change (0);
9409 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9410 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9411 }
9412 }
9413
9414
9415 /* Display an echo area message M with a specified length of NBYTES
9416 bytes. The string may include null characters. If M is not a
9417 string, clear out any existing message, and let the mini-buffer
9418 text show through.
9419
9420 This function cancels echoing. */
9421
9422 void
9423 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9424 {
9425 struct gcpro gcpro1;
9426
9427 GCPRO1 (m);
9428 clear_message (1,1);
9429 cancel_echoing ();
9430
9431 /* First flush out any partial line written with print. */
9432 message_log_maybe_newline ();
9433 if (STRINGP (m))
9434 {
9435 char *buffer;
9436 USE_SAFE_ALLOCA;
9437
9438 SAFE_ALLOCA (buffer, char *, nbytes);
9439 memcpy (buffer, SDATA (m), nbytes);
9440 message_dolog (buffer, nbytes, 1, multibyte);
9441 SAFE_FREE ();
9442 }
9443 message3_nolog (m, nbytes, multibyte);
9444
9445 UNGCPRO;
9446 }
9447
9448
9449 /* The non-logging version of message3.
9450 This does not cancel echoing, because it is used for echoing.
9451 Perhaps we need to make a separate function for echoing
9452 and make this cancel echoing. */
9453
9454 void
9455 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9456 {
9457 struct frame *sf = SELECTED_FRAME ();
9458 message_enable_multibyte = multibyte;
9459
9460 if (FRAME_INITIAL_P (sf))
9461 {
9462 if (noninteractive_need_newline)
9463 putc ('\n', stderr);
9464 noninteractive_need_newline = 0;
9465 if (STRINGP (m))
9466 fwrite (SDATA (m), nbytes, 1, stderr);
9467 if (cursor_in_echo_area == 0)
9468 fprintf (stderr, "\n");
9469 fflush (stderr);
9470 }
9471 /* A null message buffer means that the frame hasn't really been
9472 initialized yet. Error messages get reported properly by
9473 cmd_error, so this must be just an informative message; toss it. */
9474 else if (INTERACTIVE
9475 && sf->glyphs_initialized_p
9476 && FRAME_MESSAGE_BUF (sf))
9477 {
9478 Lisp_Object mini_window;
9479 Lisp_Object frame;
9480 struct frame *f;
9481
9482 /* Get the frame containing the mini-buffer
9483 that the selected frame is using. */
9484 mini_window = FRAME_MINIBUF_WINDOW (sf);
9485 frame = XWINDOW (mini_window)->frame;
9486 f = XFRAME (frame);
9487
9488 FRAME_SAMPLE_VISIBILITY (f);
9489 if (FRAME_VISIBLE_P (sf)
9490 && !FRAME_VISIBLE_P (f))
9491 Fmake_frame_visible (frame);
9492
9493 if (STRINGP (m) && SCHARS (m) > 0)
9494 {
9495 set_message (NULL, m, nbytes, multibyte);
9496 if (minibuffer_auto_raise)
9497 Fraise_frame (frame);
9498 /* Assume we are not echoing.
9499 (If we are, echo_now will override this.) */
9500 echo_message_buffer = Qnil;
9501 }
9502 else
9503 clear_message (1, 1);
9504
9505 do_pending_window_change (0);
9506 echo_area_display (1);
9507 do_pending_window_change (0);
9508 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9509 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9510 }
9511 }
9512
9513
9514 /* Display a null-terminated echo area message M. If M is 0, clear
9515 out any existing message, and let the mini-buffer text show through.
9516
9517 The buffer M must continue to exist until after the echo area gets
9518 cleared or some other message gets displayed there. Do not pass
9519 text that is stored in a Lisp string. Do not pass text in a buffer
9520 that was alloca'd. */
9521
9522 void
9523 message1 (const char *m)
9524 {
9525 message2 (m, (m ? strlen (m) : 0), 0);
9526 }
9527
9528
9529 /* The non-logging counterpart of message1. */
9530
9531 void
9532 message1_nolog (const char *m)
9533 {
9534 message2_nolog (m, (m ? strlen (m) : 0), 0);
9535 }
9536
9537 /* Display a message M which contains a single %s
9538 which gets replaced with STRING. */
9539
9540 void
9541 message_with_string (const char *m, Lisp_Object string, int log)
9542 {
9543 CHECK_STRING (string);
9544
9545 if (noninteractive)
9546 {
9547 if (m)
9548 {
9549 if (noninteractive_need_newline)
9550 putc ('\n', stderr);
9551 noninteractive_need_newline = 0;
9552 fprintf (stderr, m, SDATA (string));
9553 if (!cursor_in_echo_area)
9554 fprintf (stderr, "\n");
9555 fflush (stderr);
9556 }
9557 }
9558 else if (INTERACTIVE)
9559 {
9560 /* The frame whose minibuffer we're going to display the message on.
9561 It may be larger than the selected frame, so we need
9562 to use its buffer, not the selected frame's buffer. */
9563 Lisp_Object mini_window;
9564 struct frame *f, *sf = SELECTED_FRAME ();
9565
9566 /* Get the frame containing the minibuffer
9567 that the selected frame is using. */
9568 mini_window = FRAME_MINIBUF_WINDOW (sf);
9569 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9570
9571 /* A null message buffer means that the frame hasn't really been
9572 initialized yet. Error messages get reported properly by
9573 cmd_error, so this must be just an informative message; toss it. */
9574 if (FRAME_MESSAGE_BUF (f))
9575 {
9576 Lisp_Object args[2], msg;
9577 struct gcpro gcpro1, gcpro2;
9578
9579 args[0] = build_string (m);
9580 args[1] = msg = string;
9581 GCPRO2 (args[0], msg);
9582 gcpro1.nvars = 2;
9583
9584 msg = Fformat (2, args);
9585
9586 if (log)
9587 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9588 else
9589 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9590
9591 UNGCPRO;
9592
9593 /* Print should start at the beginning of the message
9594 buffer next time. */
9595 message_buf_print = 0;
9596 }
9597 }
9598 }
9599
9600
9601 /* Dump an informative message to the minibuf. If M is 0, clear out
9602 any existing message, and let the mini-buffer text show through. */
9603
9604 static void
9605 vmessage (const char *m, va_list ap)
9606 {
9607 if (noninteractive)
9608 {
9609 if (m)
9610 {
9611 if (noninteractive_need_newline)
9612 putc ('\n', stderr);
9613 noninteractive_need_newline = 0;
9614 vfprintf (stderr, m, ap);
9615 if (cursor_in_echo_area == 0)
9616 fprintf (stderr, "\n");
9617 fflush (stderr);
9618 }
9619 }
9620 else if (INTERACTIVE)
9621 {
9622 /* The frame whose mini-buffer we're going to display the message
9623 on. It may be larger than the selected frame, so we need to
9624 use its buffer, not the selected frame's buffer. */
9625 Lisp_Object mini_window;
9626 struct frame *f, *sf = SELECTED_FRAME ();
9627
9628 /* Get the frame containing the mini-buffer
9629 that the selected frame is using. */
9630 mini_window = FRAME_MINIBUF_WINDOW (sf);
9631 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9632
9633 /* A null message buffer means that the frame hasn't really been
9634 initialized yet. Error messages get reported properly by
9635 cmd_error, so this must be just an informative message; toss
9636 it. */
9637 if (FRAME_MESSAGE_BUF (f))
9638 {
9639 if (m)
9640 {
9641 ptrdiff_t len;
9642
9643 len = doprnt (FRAME_MESSAGE_BUF (f),
9644 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9645
9646 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9647 }
9648 else
9649 message1 (0);
9650
9651 /* Print should start at the beginning of the message
9652 buffer next time. */
9653 message_buf_print = 0;
9654 }
9655 }
9656 }
9657
9658 void
9659 message (const char *m, ...)
9660 {
9661 va_list ap;
9662 va_start (ap, m);
9663 vmessage (m, ap);
9664 va_end (ap);
9665 }
9666
9667
9668 #if 0
9669 /* The non-logging version of message. */
9670
9671 void
9672 message_nolog (const char *m, ...)
9673 {
9674 Lisp_Object old_log_max;
9675 va_list ap;
9676 va_start (ap, m);
9677 old_log_max = Vmessage_log_max;
9678 Vmessage_log_max = Qnil;
9679 vmessage (m, ap);
9680 Vmessage_log_max = old_log_max;
9681 va_end (ap);
9682 }
9683 #endif
9684
9685
9686 /* Display the current message in the current mini-buffer. This is
9687 only called from error handlers in process.c, and is not time
9688 critical. */
9689
9690 void
9691 update_echo_area (void)
9692 {
9693 if (!NILP (echo_area_buffer[0]))
9694 {
9695 Lisp_Object string;
9696 string = Fcurrent_message ();
9697 message3 (string, SBYTES (string),
9698 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9699 }
9700 }
9701
9702
9703 /* Make sure echo area buffers in `echo_buffers' are live.
9704 If they aren't, make new ones. */
9705
9706 static void
9707 ensure_echo_area_buffers (void)
9708 {
9709 int i;
9710
9711 for (i = 0; i < 2; ++i)
9712 if (!BUFFERP (echo_buffer[i])
9713 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9714 {
9715 char name[30];
9716 Lisp_Object old_buffer;
9717 int j;
9718
9719 old_buffer = echo_buffer[i];
9720 sprintf (name, " *Echo Area %d*", i);
9721 echo_buffer[i] = Fget_buffer_create (build_string (name));
9722 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9723 /* to force word wrap in echo area -
9724 it was decided to postpone this*/
9725 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9726
9727 for (j = 0; j < 2; ++j)
9728 if (EQ (old_buffer, echo_area_buffer[j]))
9729 echo_area_buffer[j] = echo_buffer[i];
9730 }
9731 }
9732
9733
9734 /* Call FN with args A1..A4 with either the current or last displayed
9735 echo_area_buffer as current buffer.
9736
9737 WHICH zero means use the current message buffer
9738 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9739 from echo_buffer[] and clear it.
9740
9741 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9742 suitable buffer from echo_buffer[] and clear it.
9743
9744 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9745 that the current message becomes the last displayed one, make
9746 choose a suitable buffer for echo_area_buffer[0], and clear it.
9747
9748 Value is what FN returns. */
9749
9750 static int
9751 with_echo_area_buffer (struct window *w, int which,
9752 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9753 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9754 {
9755 Lisp_Object buffer;
9756 int this_one, the_other, clear_buffer_p, rc;
9757 ptrdiff_t count = SPECPDL_INDEX ();
9758
9759 /* If buffers aren't live, make new ones. */
9760 ensure_echo_area_buffers ();
9761
9762 clear_buffer_p = 0;
9763
9764 if (which == 0)
9765 this_one = 0, the_other = 1;
9766 else if (which > 0)
9767 this_one = 1, the_other = 0;
9768 else
9769 {
9770 this_one = 0, the_other = 1;
9771 clear_buffer_p = 1;
9772
9773 /* We need a fresh one in case the current echo buffer equals
9774 the one containing the last displayed echo area message. */
9775 if (!NILP (echo_area_buffer[this_one])
9776 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9777 echo_area_buffer[this_one] = Qnil;
9778 }
9779
9780 /* Choose a suitable buffer from echo_buffer[] is we don't
9781 have one. */
9782 if (NILP (echo_area_buffer[this_one]))
9783 {
9784 echo_area_buffer[this_one]
9785 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9786 ? echo_buffer[the_other]
9787 : echo_buffer[this_one]);
9788 clear_buffer_p = 1;
9789 }
9790
9791 buffer = echo_area_buffer[this_one];
9792
9793 /* Don't get confused by reusing the buffer used for echoing
9794 for a different purpose. */
9795 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9796 cancel_echoing ();
9797
9798 record_unwind_protect (unwind_with_echo_area_buffer,
9799 with_echo_area_buffer_unwind_data (w));
9800
9801 /* Make the echo area buffer current. Note that for display
9802 purposes, it is not necessary that the displayed window's buffer
9803 == current_buffer, except for text property lookup. So, let's
9804 only set that buffer temporarily here without doing a full
9805 Fset_window_buffer. We must also change w->pointm, though,
9806 because otherwise an assertions in unshow_buffer fails, and Emacs
9807 aborts. */
9808 set_buffer_internal_1 (XBUFFER (buffer));
9809 if (w)
9810 {
9811 w->buffer = buffer;
9812 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9813 }
9814
9815 BVAR (current_buffer, undo_list) = Qt;
9816 BVAR (current_buffer, read_only) = Qnil;
9817 specbind (Qinhibit_read_only, Qt);
9818 specbind (Qinhibit_modification_hooks, Qt);
9819
9820 if (clear_buffer_p && Z > BEG)
9821 del_range (BEG, Z);
9822
9823 xassert (BEGV >= BEG);
9824 xassert (ZV <= Z && ZV >= BEGV);
9825
9826 rc = fn (a1, a2, a3, a4);
9827
9828 xassert (BEGV >= BEG);
9829 xassert (ZV <= Z && ZV >= BEGV);
9830
9831 unbind_to (count, Qnil);
9832 return rc;
9833 }
9834
9835
9836 /* Save state that should be preserved around the call to the function
9837 FN called in with_echo_area_buffer. */
9838
9839 static Lisp_Object
9840 with_echo_area_buffer_unwind_data (struct window *w)
9841 {
9842 int i = 0;
9843 Lisp_Object vector, tmp;
9844
9845 /* Reduce consing by keeping one vector in
9846 Vwith_echo_area_save_vector. */
9847 vector = Vwith_echo_area_save_vector;
9848 Vwith_echo_area_save_vector = Qnil;
9849
9850 if (NILP (vector))
9851 vector = Fmake_vector (make_number (7), Qnil);
9852
9853 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9854 ASET (vector, i, Vdeactivate_mark); ++i;
9855 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9856
9857 if (w)
9858 {
9859 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9860 ASET (vector, i, w->buffer); ++i;
9861 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9862 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9863 }
9864 else
9865 {
9866 int end = i + 4;
9867 for (; i < end; ++i)
9868 ASET (vector, i, Qnil);
9869 }
9870
9871 xassert (i == ASIZE (vector));
9872 return vector;
9873 }
9874
9875
9876 /* Restore global state from VECTOR which was created by
9877 with_echo_area_buffer_unwind_data. */
9878
9879 static Lisp_Object
9880 unwind_with_echo_area_buffer (Lisp_Object vector)
9881 {
9882 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9883 Vdeactivate_mark = AREF (vector, 1);
9884 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9885
9886 if (WINDOWP (AREF (vector, 3)))
9887 {
9888 struct window *w;
9889 Lisp_Object buffer, charpos, bytepos;
9890
9891 w = XWINDOW (AREF (vector, 3));
9892 buffer = AREF (vector, 4);
9893 charpos = AREF (vector, 5);
9894 bytepos = AREF (vector, 6);
9895
9896 w->buffer = buffer;
9897 set_marker_both (w->pointm, buffer,
9898 XFASTINT (charpos), XFASTINT (bytepos));
9899 }
9900
9901 Vwith_echo_area_save_vector = vector;
9902 return Qnil;
9903 }
9904
9905
9906 /* Set up the echo area for use by print functions. MULTIBYTE_P
9907 non-zero means we will print multibyte. */
9908
9909 void
9910 setup_echo_area_for_printing (int multibyte_p)
9911 {
9912 /* If we can't find an echo area any more, exit. */
9913 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9914 Fkill_emacs (Qnil);
9915
9916 ensure_echo_area_buffers ();
9917
9918 if (!message_buf_print)
9919 {
9920 /* A message has been output since the last time we printed.
9921 Choose a fresh echo area buffer. */
9922 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9923 echo_area_buffer[0] = echo_buffer[1];
9924 else
9925 echo_area_buffer[0] = echo_buffer[0];
9926
9927 /* Switch to that buffer and clear it. */
9928 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9929 BVAR (current_buffer, truncate_lines) = Qnil;
9930
9931 if (Z > BEG)
9932 {
9933 ptrdiff_t count = SPECPDL_INDEX ();
9934 specbind (Qinhibit_read_only, Qt);
9935 /* Note that undo recording is always disabled. */
9936 del_range (BEG, Z);
9937 unbind_to (count, Qnil);
9938 }
9939 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9940
9941 /* Set up the buffer for the multibyteness we need. */
9942 if (multibyte_p
9943 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9944 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9945
9946 /* Raise the frame containing the echo area. */
9947 if (minibuffer_auto_raise)
9948 {
9949 struct frame *sf = SELECTED_FRAME ();
9950 Lisp_Object mini_window;
9951 mini_window = FRAME_MINIBUF_WINDOW (sf);
9952 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9953 }
9954
9955 message_log_maybe_newline ();
9956 message_buf_print = 1;
9957 }
9958 else
9959 {
9960 if (NILP (echo_area_buffer[0]))
9961 {
9962 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9963 echo_area_buffer[0] = echo_buffer[1];
9964 else
9965 echo_area_buffer[0] = echo_buffer[0];
9966 }
9967
9968 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9969 {
9970 /* Someone switched buffers between print requests. */
9971 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9972 BVAR (current_buffer, truncate_lines) = Qnil;
9973 }
9974 }
9975 }
9976
9977
9978 /* Display an echo area message in window W. Value is non-zero if W's
9979 height is changed. If display_last_displayed_message_p is
9980 non-zero, display the message that was last displayed, otherwise
9981 display the current message. */
9982
9983 static int
9984 display_echo_area (struct window *w)
9985 {
9986 int i, no_message_p, window_height_changed_p;
9987
9988 /* Temporarily disable garbage collections while displaying the echo
9989 area. This is done because a GC can print a message itself.
9990 That message would modify the echo area buffer's contents while a
9991 redisplay of the buffer is going on, and seriously confuse
9992 redisplay. */
9993 ptrdiff_t count = inhibit_garbage_collection ();
9994
9995 /* If there is no message, we must call display_echo_area_1
9996 nevertheless because it resizes the window. But we will have to
9997 reset the echo_area_buffer in question to nil at the end because
9998 with_echo_area_buffer will sets it to an empty buffer. */
9999 i = display_last_displayed_message_p ? 1 : 0;
10000 no_message_p = NILP (echo_area_buffer[i]);
10001
10002 window_height_changed_p
10003 = with_echo_area_buffer (w, display_last_displayed_message_p,
10004 display_echo_area_1,
10005 (intptr_t) w, Qnil, 0, 0);
10006
10007 if (no_message_p)
10008 echo_area_buffer[i] = Qnil;
10009
10010 unbind_to (count, Qnil);
10011 return window_height_changed_p;
10012 }
10013
10014
10015 /* Helper for display_echo_area. Display the current buffer which
10016 contains the current echo area message in window W, a mini-window,
10017 a pointer to which is passed in A1. A2..A4 are currently not used.
10018 Change the height of W so that all of the message is displayed.
10019 Value is non-zero if height of W was changed. */
10020
10021 static int
10022 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10023 {
10024 intptr_t i1 = a1;
10025 struct window *w = (struct window *) i1;
10026 Lisp_Object window;
10027 struct text_pos start;
10028 int window_height_changed_p = 0;
10029
10030 /* Do this before displaying, so that we have a large enough glyph
10031 matrix for the display. If we can't get enough space for the
10032 whole text, display the last N lines. That works by setting w->start. */
10033 window_height_changed_p = resize_mini_window (w, 0);
10034
10035 /* Use the starting position chosen by resize_mini_window. */
10036 SET_TEXT_POS_FROM_MARKER (start, w->start);
10037
10038 /* Display. */
10039 clear_glyph_matrix (w->desired_matrix);
10040 XSETWINDOW (window, w);
10041 try_window (window, start, 0);
10042
10043 return window_height_changed_p;
10044 }
10045
10046
10047 /* Resize the echo area window to exactly the size needed for the
10048 currently displayed message, if there is one. If a mini-buffer
10049 is active, don't shrink it. */
10050
10051 void
10052 resize_echo_area_exactly (void)
10053 {
10054 if (BUFFERP (echo_area_buffer[0])
10055 && WINDOWP (echo_area_window))
10056 {
10057 struct window *w = XWINDOW (echo_area_window);
10058 int resized_p;
10059 Lisp_Object resize_exactly;
10060
10061 if (minibuf_level == 0)
10062 resize_exactly = Qt;
10063 else
10064 resize_exactly = Qnil;
10065
10066 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10067 (intptr_t) w, resize_exactly,
10068 0, 0);
10069 if (resized_p)
10070 {
10071 ++windows_or_buffers_changed;
10072 ++update_mode_lines;
10073 redisplay_internal ();
10074 }
10075 }
10076 }
10077
10078
10079 /* Callback function for with_echo_area_buffer, when used from
10080 resize_echo_area_exactly. A1 contains a pointer to the window to
10081 resize, EXACTLY non-nil means resize the mini-window exactly to the
10082 size of the text displayed. A3 and A4 are not used. Value is what
10083 resize_mini_window returns. */
10084
10085 static int
10086 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10087 {
10088 intptr_t i1 = a1;
10089 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10090 }
10091
10092
10093 /* Resize mini-window W to fit the size of its contents. EXACT_P
10094 means size the window exactly to the size needed. Otherwise, it's
10095 only enlarged until W's buffer is empty.
10096
10097 Set W->start to the right place to begin display. If the whole
10098 contents fit, start at the beginning. Otherwise, start so as
10099 to make the end of the contents appear. This is particularly
10100 important for y-or-n-p, but seems desirable generally.
10101
10102 Value is non-zero if the window height has been changed. */
10103
10104 int
10105 resize_mini_window (struct window *w, int exact_p)
10106 {
10107 struct frame *f = XFRAME (w->frame);
10108 int window_height_changed_p = 0;
10109
10110 xassert (MINI_WINDOW_P (w));
10111
10112 /* By default, start display at the beginning. */
10113 set_marker_both (w->start, w->buffer,
10114 BUF_BEGV (XBUFFER (w->buffer)),
10115 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10116
10117 /* Don't resize windows while redisplaying a window; it would
10118 confuse redisplay functions when the size of the window they are
10119 displaying changes from under them. Such a resizing can happen,
10120 for instance, when which-func prints a long message while
10121 we are running fontification-functions. We're running these
10122 functions with safe_call which binds inhibit-redisplay to t. */
10123 if (!NILP (Vinhibit_redisplay))
10124 return 0;
10125
10126 /* Nil means don't try to resize. */
10127 if (NILP (Vresize_mini_windows)
10128 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10129 return 0;
10130
10131 if (!FRAME_MINIBUF_ONLY_P (f))
10132 {
10133 struct it it;
10134 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10135 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10136 int height;
10137 EMACS_INT max_height;
10138 int unit = FRAME_LINE_HEIGHT (f);
10139 struct text_pos start;
10140 struct buffer *old_current_buffer = NULL;
10141
10142 if (current_buffer != XBUFFER (w->buffer))
10143 {
10144 old_current_buffer = current_buffer;
10145 set_buffer_internal (XBUFFER (w->buffer));
10146 }
10147
10148 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10149
10150 /* Compute the max. number of lines specified by the user. */
10151 if (FLOATP (Vmax_mini_window_height))
10152 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10153 else if (INTEGERP (Vmax_mini_window_height))
10154 max_height = XINT (Vmax_mini_window_height);
10155 else
10156 max_height = total_height / 4;
10157
10158 /* Correct that max. height if it's bogus. */
10159 max_height = max (1, max_height);
10160 max_height = min (total_height, max_height);
10161
10162 /* Find out the height of the text in the window. */
10163 if (it.line_wrap == TRUNCATE)
10164 height = 1;
10165 else
10166 {
10167 last_height = 0;
10168 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10169 if (it.max_ascent == 0 && it.max_descent == 0)
10170 height = it.current_y + last_height;
10171 else
10172 height = it.current_y + it.max_ascent + it.max_descent;
10173 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10174 height = (height + unit - 1) / unit;
10175 }
10176
10177 /* Compute a suitable window start. */
10178 if (height > max_height)
10179 {
10180 height = max_height;
10181 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10182 move_it_vertically_backward (&it, (height - 1) * unit);
10183 start = it.current.pos;
10184 }
10185 else
10186 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10187 SET_MARKER_FROM_TEXT_POS (w->start, start);
10188
10189 if (EQ (Vresize_mini_windows, Qgrow_only))
10190 {
10191 /* Let it grow only, until we display an empty message, in which
10192 case the window shrinks again. */
10193 if (height > WINDOW_TOTAL_LINES (w))
10194 {
10195 int old_height = WINDOW_TOTAL_LINES (w);
10196 freeze_window_starts (f, 1);
10197 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10198 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10199 }
10200 else if (height < WINDOW_TOTAL_LINES (w)
10201 && (exact_p || BEGV == ZV))
10202 {
10203 int old_height = WINDOW_TOTAL_LINES (w);
10204 freeze_window_starts (f, 0);
10205 shrink_mini_window (w);
10206 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10207 }
10208 }
10209 else
10210 {
10211 /* Always resize to exact size needed. */
10212 if (height > WINDOW_TOTAL_LINES (w))
10213 {
10214 int old_height = WINDOW_TOTAL_LINES (w);
10215 freeze_window_starts (f, 1);
10216 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10217 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10218 }
10219 else if (height < WINDOW_TOTAL_LINES (w))
10220 {
10221 int old_height = WINDOW_TOTAL_LINES (w);
10222 freeze_window_starts (f, 0);
10223 shrink_mini_window (w);
10224
10225 if (height)
10226 {
10227 freeze_window_starts (f, 1);
10228 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10229 }
10230
10231 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10232 }
10233 }
10234
10235 if (old_current_buffer)
10236 set_buffer_internal (old_current_buffer);
10237 }
10238
10239 return window_height_changed_p;
10240 }
10241
10242
10243 /* Value is the current message, a string, or nil if there is no
10244 current message. */
10245
10246 Lisp_Object
10247 current_message (void)
10248 {
10249 Lisp_Object msg;
10250
10251 if (!BUFFERP (echo_area_buffer[0]))
10252 msg = Qnil;
10253 else
10254 {
10255 with_echo_area_buffer (0, 0, current_message_1,
10256 (intptr_t) &msg, Qnil, 0, 0);
10257 if (NILP (msg))
10258 echo_area_buffer[0] = Qnil;
10259 }
10260
10261 return msg;
10262 }
10263
10264
10265 static int
10266 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10267 {
10268 intptr_t i1 = a1;
10269 Lisp_Object *msg = (Lisp_Object *) i1;
10270
10271 if (Z > BEG)
10272 *msg = make_buffer_string (BEG, Z, 1);
10273 else
10274 *msg = Qnil;
10275 return 0;
10276 }
10277
10278
10279 /* Push the current message on Vmessage_stack for later restoration
10280 by restore_message. Value is non-zero if the current message isn't
10281 empty. This is a relatively infrequent operation, so it's not
10282 worth optimizing. */
10283
10284 int
10285 push_message (void)
10286 {
10287 Lisp_Object msg;
10288 msg = current_message ();
10289 Vmessage_stack = Fcons (msg, Vmessage_stack);
10290 return STRINGP (msg);
10291 }
10292
10293
10294 /* Restore message display from the top of Vmessage_stack. */
10295
10296 void
10297 restore_message (void)
10298 {
10299 Lisp_Object msg;
10300
10301 xassert (CONSP (Vmessage_stack));
10302 msg = XCAR (Vmessage_stack);
10303 if (STRINGP (msg))
10304 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10305 else
10306 message3_nolog (msg, 0, 0);
10307 }
10308
10309
10310 /* Handler for record_unwind_protect calling pop_message. */
10311
10312 Lisp_Object
10313 pop_message_unwind (Lisp_Object dummy)
10314 {
10315 pop_message ();
10316 return Qnil;
10317 }
10318
10319 /* Pop the top-most entry off Vmessage_stack. */
10320
10321 static void
10322 pop_message (void)
10323 {
10324 xassert (CONSP (Vmessage_stack));
10325 Vmessage_stack = XCDR (Vmessage_stack);
10326 }
10327
10328
10329 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10330 exits. If the stack is not empty, we have a missing pop_message
10331 somewhere. */
10332
10333 void
10334 check_message_stack (void)
10335 {
10336 if (!NILP (Vmessage_stack))
10337 abort ();
10338 }
10339
10340
10341 /* Truncate to NCHARS what will be displayed in the echo area the next
10342 time we display it---but don't redisplay it now. */
10343
10344 void
10345 truncate_echo_area (ptrdiff_t nchars)
10346 {
10347 if (nchars == 0)
10348 echo_area_buffer[0] = Qnil;
10349 /* A null message buffer means that the frame hasn't really been
10350 initialized yet. Error messages get reported properly by
10351 cmd_error, so this must be just an informative message; toss it. */
10352 else if (!noninteractive
10353 && INTERACTIVE
10354 && !NILP (echo_area_buffer[0]))
10355 {
10356 struct frame *sf = SELECTED_FRAME ();
10357 if (FRAME_MESSAGE_BUF (sf))
10358 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10359 }
10360 }
10361
10362
10363 /* Helper function for truncate_echo_area. Truncate the current
10364 message to at most NCHARS characters. */
10365
10366 static int
10367 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10368 {
10369 if (BEG + nchars < Z)
10370 del_range (BEG + nchars, Z);
10371 if (Z == BEG)
10372 echo_area_buffer[0] = Qnil;
10373 return 0;
10374 }
10375
10376
10377 /* Set the current message to a substring of S or STRING.
10378
10379 If STRING is a Lisp string, set the message to the first NBYTES
10380 bytes from STRING. NBYTES zero means use the whole string. If
10381 STRING is multibyte, the message will be displayed multibyte.
10382
10383 If S is not null, set the message to the first LEN bytes of S. LEN
10384 zero means use the whole string. MULTIBYTE_P non-zero means S is
10385 multibyte. Display the message multibyte in that case.
10386
10387 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10388 to t before calling set_message_1 (which calls insert).
10389 */
10390
10391 static void
10392 set_message (const char *s, Lisp_Object string,
10393 ptrdiff_t nbytes, int multibyte_p)
10394 {
10395 message_enable_multibyte
10396 = ((s && multibyte_p)
10397 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10398
10399 with_echo_area_buffer (0, -1, set_message_1,
10400 (intptr_t) s, string, nbytes, multibyte_p);
10401 message_buf_print = 0;
10402 help_echo_showing_p = 0;
10403 }
10404
10405
10406 /* Helper function for set_message. Arguments have the same meaning
10407 as there, with A1 corresponding to S and A2 corresponding to STRING
10408 This function is called with the echo area buffer being
10409 current. */
10410
10411 static int
10412 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10413 {
10414 intptr_t i1 = a1;
10415 const char *s = (const char *) i1;
10416 const unsigned char *msg = (const unsigned char *) s;
10417 Lisp_Object string = a2;
10418
10419 /* Change multibyteness of the echo buffer appropriately. */
10420 if (message_enable_multibyte
10421 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10422 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10423
10424 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10425 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10426 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10427
10428 /* Insert new message at BEG. */
10429 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10430
10431 if (STRINGP (string))
10432 {
10433 ptrdiff_t nchars;
10434
10435 if (nbytes == 0)
10436 nbytes = SBYTES (string);
10437 nchars = string_byte_to_char (string, nbytes);
10438
10439 /* This function takes care of single/multibyte conversion. We
10440 just have to ensure that the echo area buffer has the right
10441 setting of enable_multibyte_characters. */
10442 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10443 }
10444 else if (s)
10445 {
10446 if (nbytes == 0)
10447 nbytes = strlen (s);
10448
10449 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10450 {
10451 /* Convert from multi-byte to single-byte. */
10452 ptrdiff_t i;
10453 int c, n;
10454 char work[1];
10455
10456 /* Convert a multibyte string to single-byte. */
10457 for (i = 0; i < nbytes; i += n)
10458 {
10459 c = string_char_and_length (msg + i, &n);
10460 work[0] = (ASCII_CHAR_P (c)
10461 ? c
10462 : multibyte_char_to_unibyte (c));
10463 insert_1_both (work, 1, 1, 1, 0, 0);
10464 }
10465 }
10466 else if (!multibyte_p
10467 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10468 {
10469 /* Convert from single-byte to multi-byte. */
10470 ptrdiff_t i;
10471 int c, n;
10472 unsigned char str[MAX_MULTIBYTE_LENGTH];
10473
10474 /* Convert a single-byte string to multibyte. */
10475 for (i = 0; i < nbytes; i++)
10476 {
10477 c = msg[i];
10478 MAKE_CHAR_MULTIBYTE (c);
10479 n = CHAR_STRING (c, str);
10480 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10481 }
10482 }
10483 else
10484 insert_1 (s, nbytes, 1, 0, 0);
10485 }
10486
10487 return 0;
10488 }
10489
10490
10491 /* Clear messages. CURRENT_P non-zero means clear the current
10492 message. LAST_DISPLAYED_P non-zero means clear the message
10493 last displayed. */
10494
10495 void
10496 clear_message (int current_p, int last_displayed_p)
10497 {
10498 if (current_p)
10499 {
10500 echo_area_buffer[0] = Qnil;
10501 message_cleared_p = 1;
10502 }
10503
10504 if (last_displayed_p)
10505 echo_area_buffer[1] = Qnil;
10506
10507 message_buf_print = 0;
10508 }
10509
10510 /* Clear garbaged frames.
10511
10512 This function is used where the old redisplay called
10513 redraw_garbaged_frames which in turn called redraw_frame which in
10514 turn called clear_frame. The call to clear_frame was a source of
10515 flickering. I believe a clear_frame is not necessary. It should
10516 suffice in the new redisplay to invalidate all current matrices,
10517 and ensure a complete redisplay of all windows. */
10518
10519 static void
10520 clear_garbaged_frames (void)
10521 {
10522 if (frame_garbaged)
10523 {
10524 Lisp_Object tail, frame;
10525 int changed_count = 0;
10526
10527 FOR_EACH_FRAME (tail, frame)
10528 {
10529 struct frame *f = XFRAME (frame);
10530
10531 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10532 {
10533 if (f->resized_p)
10534 {
10535 Fredraw_frame (frame);
10536 f->force_flush_display_p = 1;
10537 }
10538 clear_current_matrices (f);
10539 changed_count++;
10540 f->garbaged = 0;
10541 f->resized_p = 0;
10542 }
10543 }
10544
10545 frame_garbaged = 0;
10546 if (changed_count)
10547 ++windows_or_buffers_changed;
10548 }
10549 }
10550
10551
10552 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10553 is non-zero update selected_frame. Value is non-zero if the
10554 mini-windows height has been changed. */
10555
10556 static int
10557 echo_area_display (int update_frame_p)
10558 {
10559 Lisp_Object mini_window;
10560 struct window *w;
10561 struct frame *f;
10562 int window_height_changed_p = 0;
10563 struct frame *sf = SELECTED_FRAME ();
10564
10565 mini_window = FRAME_MINIBUF_WINDOW (sf);
10566 w = XWINDOW (mini_window);
10567 f = XFRAME (WINDOW_FRAME (w));
10568
10569 /* Don't display if frame is invisible or not yet initialized. */
10570 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10571 return 0;
10572
10573 #ifdef HAVE_WINDOW_SYSTEM
10574 /* When Emacs starts, selected_frame may be the initial terminal
10575 frame. If we let this through, a message would be displayed on
10576 the terminal. */
10577 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10578 return 0;
10579 #endif /* HAVE_WINDOW_SYSTEM */
10580
10581 /* Redraw garbaged frames. */
10582 if (frame_garbaged)
10583 clear_garbaged_frames ();
10584
10585 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10586 {
10587 echo_area_window = mini_window;
10588 window_height_changed_p = display_echo_area (w);
10589 w->must_be_updated_p = 1;
10590
10591 /* Update the display, unless called from redisplay_internal.
10592 Also don't update the screen during redisplay itself. The
10593 update will happen at the end of redisplay, and an update
10594 here could cause confusion. */
10595 if (update_frame_p && !redisplaying_p)
10596 {
10597 int n = 0;
10598
10599 /* If the display update has been interrupted by pending
10600 input, update mode lines in the frame. Due to the
10601 pending input, it might have been that redisplay hasn't
10602 been called, so that mode lines above the echo area are
10603 garbaged. This looks odd, so we prevent it here. */
10604 if (!display_completed)
10605 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10606
10607 if (window_height_changed_p
10608 /* Don't do this if Emacs is shutting down. Redisplay
10609 needs to run hooks. */
10610 && !NILP (Vrun_hooks))
10611 {
10612 /* Must update other windows. Likewise as in other
10613 cases, don't let this update be interrupted by
10614 pending input. */
10615 ptrdiff_t count = SPECPDL_INDEX ();
10616 specbind (Qredisplay_dont_pause, Qt);
10617 windows_or_buffers_changed = 1;
10618 redisplay_internal ();
10619 unbind_to (count, Qnil);
10620 }
10621 else if (FRAME_WINDOW_P (f) && n == 0)
10622 {
10623 /* Window configuration is the same as before.
10624 Can do with a display update of the echo area,
10625 unless we displayed some mode lines. */
10626 update_single_window (w, 1);
10627 FRAME_RIF (f)->flush_display (f);
10628 }
10629 else
10630 update_frame (f, 1, 1);
10631
10632 /* If cursor is in the echo area, make sure that the next
10633 redisplay displays the minibuffer, so that the cursor will
10634 be replaced with what the minibuffer wants. */
10635 if (cursor_in_echo_area)
10636 ++windows_or_buffers_changed;
10637 }
10638 }
10639 else if (!EQ (mini_window, selected_window))
10640 windows_or_buffers_changed++;
10641
10642 /* Last displayed message is now the current message. */
10643 echo_area_buffer[1] = echo_area_buffer[0];
10644 /* Inform read_char that we're not echoing. */
10645 echo_message_buffer = Qnil;
10646
10647 /* Prevent redisplay optimization in redisplay_internal by resetting
10648 this_line_start_pos. This is done because the mini-buffer now
10649 displays the message instead of its buffer text. */
10650 if (EQ (mini_window, selected_window))
10651 CHARPOS (this_line_start_pos) = 0;
10652
10653 return window_height_changed_p;
10654 }
10655
10656
10657 \f
10658 /***********************************************************************
10659 Mode Lines and Frame Titles
10660 ***********************************************************************/
10661
10662 /* A buffer for constructing non-propertized mode-line strings and
10663 frame titles in it; allocated from the heap in init_xdisp and
10664 resized as needed in store_mode_line_noprop_char. */
10665
10666 static char *mode_line_noprop_buf;
10667
10668 /* The buffer's end, and a current output position in it. */
10669
10670 static char *mode_line_noprop_buf_end;
10671 static char *mode_line_noprop_ptr;
10672
10673 #define MODE_LINE_NOPROP_LEN(start) \
10674 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10675
10676 static enum {
10677 MODE_LINE_DISPLAY = 0,
10678 MODE_LINE_TITLE,
10679 MODE_LINE_NOPROP,
10680 MODE_LINE_STRING
10681 } mode_line_target;
10682
10683 /* Alist that caches the results of :propertize.
10684 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10685 static Lisp_Object mode_line_proptrans_alist;
10686
10687 /* List of strings making up the mode-line. */
10688 static Lisp_Object mode_line_string_list;
10689
10690 /* Base face property when building propertized mode line string. */
10691 static Lisp_Object mode_line_string_face;
10692 static Lisp_Object mode_line_string_face_prop;
10693
10694
10695 /* Unwind data for mode line strings */
10696
10697 static Lisp_Object Vmode_line_unwind_vector;
10698
10699 static Lisp_Object
10700 format_mode_line_unwind_data (struct buffer *obuf,
10701 Lisp_Object owin,
10702 int save_proptrans)
10703 {
10704 Lisp_Object vector, tmp;
10705
10706 /* Reduce consing by keeping one vector in
10707 Vwith_echo_area_save_vector. */
10708 vector = Vmode_line_unwind_vector;
10709 Vmode_line_unwind_vector = Qnil;
10710
10711 if (NILP (vector))
10712 vector = Fmake_vector (make_number (8), Qnil);
10713
10714 ASET (vector, 0, make_number (mode_line_target));
10715 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10716 ASET (vector, 2, mode_line_string_list);
10717 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10718 ASET (vector, 4, mode_line_string_face);
10719 ASET (vector, 5, mode_line_string_face_prop);
10720
10721 if (obuf)
10722 XSETBUFFER (tmp, obuf);
10723 else
10724 tmp = Qnil;
10725 ASET (vector, 6, tmp);
10726 ASET (vector, 7, owin);
10727
10728 return vector;
10729 }
10730
10731 static Lisp_Object
10732 unwind_format_mode_line (Lisp_Object vector)
10733 {
10734 mode_line_target = XINT (AREF (vector, 0));
10735 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10736 mode_line_string_list = AREF (vector, 2);
10737 if (! EQ (AREF (vector, 3), Qt))
10738 mode_line_proptrans_alist = AREF (vector, 3);
10739 mode_line_string_face = AREF (vector, 4);
10740 mode_line_string_face_prop = AREF (vector, 5);
10741
10742 if (!NILP (AREF (vector, 7)))
10743 /* Select window before buffer, since it may change the buffer. */
10744 Fselect_window (AREF (vector, 7), Qt);
10745
10746 if (!NILP (AREF (vector, 6)))
10747 {
10748 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10749 ASET (vector, 6, Qnil);
10750 }
10751
10752 Vmode_line_unwind_vector = vector;
10753 return Qnil;
10754 }
10755
10756
10757 /* Store a single character C for the frame title in mode_line_noprop_buf.
10758 Re-allocate mode_line_noprop_buf if necessary. */
10759
10760 static void
10761 store_mode_line_noprop_char (char c)
10762 {
10763 /* If output position has reached the end of the allocated buffer,
10764 increase the buffer's size. */
10765 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10766 {
10767 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10768 ptrdiff_t size = len;
10769 mode_line_noprop_buf =
10770 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10771 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10772 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10773 }
10774
10775 *mode_line_noprop_ptr++ = c;
10776 }
10777
10778
10779 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10780 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10781 characters that yield more columns than PRECISION; PRECISION <= 0
10782 means copy the whole string. Pad with spaces until FIELD_WIDTH
10783 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10784 pad. Called from display_mode_element when it is used to build a
10785 frame title. */
10786
10787 static int
10788 store_mode_line_noprop (const char *string, int field_width, int precision)
10789 {
10790 const unsigned char *str = (const unsigned char *) string;
10791 int n = 0;
10792 ptrdiff_t dummy, nbytes;
10793
10794 /* Copy at most PRECISION chars from STR. */
10795 nbytes = strlen (string);
10796 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10797 while (nbytes--)
10798 store_mode_line_noprop_char (*str++);
10799
10800 /* Fill up with spaces until FIELD_WIDTH reached. */
10801 while (field_width > 0
10802 && n < field_width)
10803 {
10804 store_mode_line_noprop_char (' ');
10805 ++n;
10806 }
10807
10808 return n;
10809 }
10810
10811 /***********************************************************************
10812 Frame Titles
10813 ***********************************************************************/
10814
10815 #ifdef HAVE_WINDOW_SYSTEM
10816
10817 /* Set the title of FRAME, if it has changed. The title format is
10818 Vicon_title_format if FRAME is iconified, otherwise it is
10819 frame_title_format. */
10820
10821 static void
10822 x_consider_frame_title (Lisp_Object frame)
10823 {
10824 struct frame *f = XFRAME (frame);
10825
10826 if (FRAME_WINDOW_P (f)
10827 || FRAME_MINIBUF_ONLY_P (f)
10828 || f->explicit_name)
10829 {
10830 /* Do we have more than one visible frame on this X display? */
10831 Lisp_Object tail;
10832 Lisp_Object fmt;
10833 ptrdiff_t title_start;
10834 char *title;
10835 ptrdiff_t len;
10836 struct it it;
10837 ptrdiff_t count = SPECPDL_INDEX ();
10838
10839 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10840 {
10841 Lisp_Object other_frame = XCAR (tail);
10842 struct frame *tf = XFRAME (other_frame);
10843
10844 if (tf != f
10845 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10846 && !FRAME_MINIBUF_ONLY_P (tf)
10847 && !EQ (other_frame, tip_frame)
10848 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10849 break;
10850 }
10851
10852 /* Set global variable indicating that multiple frames exist. */
10853 multiple_frames = CONSP (tail);
10854
10855 /* Switch to the buffer of selected window of the frame. Set up
10856 mode_line_target so that display_mode_element will output into
10857 mode_line_noprop_buf; then display the title. */
10858 record_unwind_protect (unwind_format_mode_line,
10859 format_mode_line_unwind_data
10860 (current_buffer, selected_window, 0));
10861
10862 Fselect_window (f->selected_window, Qt);
10863 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10864 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10865
10866 mode_line_target = MODE_LINE_TITLE;
10867 title_start = MODE_LINE_NOPROP_LEN (0);
10868 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10869 NULL, DEFAULT_FACE_ID);
10870 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10871 len = MODE_LINE_NOPROP_LEN (title_start);
10872 title = mode_line_noprop_buf + title_start;
10873 unbind_to (count, Qnil);
10874
10875 /* Set the title only if it's changed. This avoids consing in
10876 the common case where it hasn't. (If it turns out that we've
10877 already wasted too much time by walking through the list with
10878 display_mode_element, then we might need to optimize at a
10879 higher level than this.) */
10880 if (! STRINGP (f->name)
10881 || SBYTES (f->name) != len
10882 || memcmp (title, SDATA (f->name), len) != 0)
10883 x_implicitly_set_name (f, make_string (title, len), Qnil);
10884 }
10885 }
10886
10887 #endif /* not HAVE_WINDOW_SYSTEM */
10888
10889
10890
10891 \f
10892 /***********************************************************************
10893 Menu Bars
10894 ***********************************************************************/
10895
10896
10897 /* Prepare for redisplay by updating menu-bar item lists when
10898 appropriate. This can call eval. */
10899
10900 void
10901 prepare_menu_bars (void)
10902 {
10903 int all_windows;
10904 struct gcpro gcpro1, gcpro2;
10905 struct frame *f;
10906 Lisp_Object tooltip_frame;
10907
10908 #ifdef HAVE_WINDOW_SYSTEM
10909 tooltip_frame = tip_frame;
10910 #else
10911 tooltip_frame = Qnil;
10912 #endif
10913
10914 /* Update all frame titles based on their buffer names, etc. We do
10915 this before the menu bars so that the buffer-menu will show the
10916 up-to-date frame titles. */
10917 #ifdef HAVE_WINDOW_SYSTEM
10918 if (windows_or_buffers_changed || update_mode_lines)
10919 {
10920 Lisp_Object tail, frame;
10921
10922 FOR_EACH_FRAME (tail, frame)
10923 {
10924 f = XFRAME (frame);
10925 if (!EQ (frame, tooltip_frame)
10926 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10927 x_consider_frame_title (frame);
10928 }
10929 }
10930 #endif /* HAVE_WINDOW_SYSTEM */
10931
10932 /* Update the menu bar item lists, if appropriate. This has to be
10933 done before any actual redisplay or generation of display lines. */
10934 all_windows = (update_mode_lines
10935 || buffer_shared > 1
10936 || windows_or_buffers_changed);
10937 if (all_windows)
10938 {
10939 Lisp_Object tail, frame;
10940 ptrdiff_t count = SPECPDL_INDEX ();
10941 /* 1 means that update_menu_bar has run its hooks
10942 so any further calls to update_menu_bar shouldn't do so again. */
10943 int menu_bar_hooks_run = 0;
10944
10945 record_unwind_save_match_data ();
10946
10947 FOR_EACH_FRAME (tail, frame)
10948 {
10949 f = XFRAME (frame);
10950
10951 /* Ignore tooltip frame. */
10952 if (EQ (frame, tooltip_frame))
10953 continue;
10954
10955 /* If a window on this frame changed size, report that to
10956 the user and clear the size-change flag. */
10957 if (FRAME_WINDOW_SIZES_CHANGED (f))
10958 {
10959 Lisp_Object functions;
10960
10961 /* Clear flag first in case we get an error below. */
10962 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10963 functions = Vwindow_size_change_functions;
10964 GCPRO2 (tail, functions);
10965
10966 while (CONSP (functions))
10967 {
10968 if (!EQ (XCAR (functions), Qt))
10969 call1 (XCAR (functions), frame);
10970 functions = XCDR (functions);
10971 }
10972 UNGCPRO;
10973 }
10974
10975 GCPRO1 (tail);
10976 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10977 #ifdef HAVE_WINDOW_SYSTEM
10978 update_tool_bar (f, 0);
10979 #endif
10980 #ifdef HAVE_NS
10981 if (windows_or_buffers_changed
10982 && FRAME_NS_P (f))
10983 ns_set_doc_edited (f, Fbuffer_modified_p
10984 (XWINDOW (f->selected_window)->buffer));
10985 #endif
10986 UNGCPRO;
10987 }
10988
10989 unbind_to (count, Qnil);
10990 }
10991 else
10992 {
10993 struct frame *sf = SELECTED_FRAME ();
10994 update_menu_bar (sf, 1, 0);
10995 #ifdef HAVE_WINDOW_SYSTEM
10996 update_tool_bar (sf, 1);
10997 #endif
10998 }
10999 }
11000
11001
11002 /* Update the menu bar item list for frame F. This has to be done
11003 before we start to fill in any display lines, because it can call
11004 eval.
11005
11006 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11007
11008 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11009 already ran the menu bar hooks for this redisplay, so there
11010 is no need to run them again. The return value is the
11011 updated value of this flag, to pass to the next call. */
11012
11013 static int
11014 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11015 {
11016 Lisp_Object window;
11017 register struct window *w;
11018
11019 /* If called recursively during a menu update, do nothing. This can
11020 happen when, for instance, an activate-menubar-hook causes a
11021 redisplay. */
11022 if (inhibit_menubar_update)
11023 return hooks_run;
11024
11025 window = FRAME_SELECTED_WINDOW (f);
11026 w = XWINDOW (window);
11027
11028 if (FRAME_WINDOW_P (f)
11029 ?
11030 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11031 || defined (HAVE_NS) || defined (USE_GTK)
11032 FRAME_EXTERNAL_MENU_BAR (f)
11033 #else
11034 FRAME_MENU_BAR_LINES (f) > 0
11035 #endif
11036 : FRAME_MENU_BAR_LINES (f) > 0)
11037 {
11038 /* If the user has switched buffers or windows, we need to
11039 recompute to reflect the new bindings. But we'll
11040 recompute when update_mode_lines is set too; that means
11041 that people can use force-mode-line-update to request
11042 that the menu bar be recomputed. The adverse effect on
11043 the rest of the redisplay algorithm is about the same as
11044 windows_or_buffers_changed anyway. */
11045 if (windows_or_buffers_changed
11046 /* This used to test w->update_mode_line, but we believe
11047 there is no need to recompute the menu in that case. */
11048 || update_mode_lines
11049 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11050 < BUF_MODIFF (XBUFFER (w->buffer)))
11051 != !NILP (w->last_had_star))
11052 || ((!NILP (Vtransient_mark_mode)
11053 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11054 != !NILP (w->region_showing)))
11055 {
11056 struct buffer *prev = current_buffer;
11057 ptrdiff_t count = SPECPDL_INDEX ();
11058
11059 specbind (Qinhibit_menubar_update, Qt);
11060
11061 set_buffer_internal_1 (XBUFFER (w->buffer));
11062 if (save_match_data)
11063 record_unwind_save_match_data ();
11064 if (NILP (Voverriding_local_map_menu_flag))
11065 {
11066 specbind (Qoverriding_terminal_local_map, Qnil);
11067 specbind (Qoverriding_local_map, Qnil);
11068 }
11069
11070 if (!hooks_run)
11071 {
11072 /* Run the Lucid hook. */
11073 safe_run_hooks (Qactivate_menubar_hook);
11074
11075 /* If it has changed current-menubar from previous value,
11076 really recompute the menu-bar from the value. */
11077 if (! NILP (Vlucid_menu_bar_dirty_flag))
11078 call0 (Qrecompute_lucid_menubar);
11079
11080 safe_run_hooks (Qmenu_bar_update_hook);
11081
11082 hooks_run = 1;
11083 }
11084
11085 XSETFRAME (Vmenu_updating_frame, f);
11086 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11087
11088 /* Redisplay the menu bar in case we changed it. */
11089 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11090 || defined (HAVE_NS) || defined (USE_GTK)
11091 if (FRAME_WINDOW_P (f))
11092 {
11093 #if defined (HAVE_NS)
11094 /* All frames on Mac OS share the same menubar. So only
11095 the selected frame should be allowed to set it. */
11096 if (f == SELECTED_FRAME ())
11097 #endif
11098 set_frame_menubar (f, 0, 0);
11099 }
11100 else
11101 /* On a terminal screen, the menu bar is an ordinary screen
11102 line, and this makes it get updated. */
11103 w->update_mode_line = Qt;
11104 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11105 /* In the non-toolkit version, the menu bar is an ordinary screen
11106 line, and this makes it get updated. */
11107 w->update_mode_line = Qt;
11108 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11109
11110 unbind_to (count, Qnil);
11111 set_buffer_internal_1 (prev);
11112 }
11113 }
11114
11115 return hooks_run;
11116 }
11117
11118
11119 \f
11120 /***********************************************************************
11121 Output Cursor
11122 ***********************************************************************/
11123
11124 #ifdef HAVE_WINDOW_SYSTEM
11125
11126 /* EXPORT:
11127 Nominal cursor position -- where to draw output.
11128 HPOS and VPOS are window relative glyph matrix coordinates.
11129 X and Y are window relative pixel coordinates. */
11130
11131 struct cursor_pos output_cursor;
11132
11133
11134 /* EXPORT:
11135 Set the global variable output_cursor to CURSOR. All cursor
11136 positions are relative to updated_window. */
11137
11138 void
11139 set_output_cursor (struct cursor_pos *cursor)
11140 {
11141 output_cursor.hpos = cursor->hpos;
11142 output_cursor.vpos = cursor->vpos;
11143 output_cursor.x = cursor->x;
11144 output_cursor.y = cursor->y;
11145 }
11146
11147
11148 /* EXPORT for RIF:
11149 Set a nominal cursor position.
11150
11151 HPOS and VPOS are column/row positions in a window glyph matrix. X
11152 and Y are window text area relative pixel positions.
11153
11154 If this is done during an update, updated_window will contain the
11155 window that is being updated and the position is the future output
11156 cursor position for that window. If updated_window is null, use
11157 selected_window and display the cursor at the given position. */
11158
11159 void
11160 x_cursor_to (int vpos, int hpos, int y, int x)
11161 {
11162 struct window *w;
11163
11164 /* If updated_window is not set, work on selected_window. */
11165 if (updated_window)
11166 w = updated_window;
11167 else
11168 w = XWINDOW (selected_window);
11169
11170 /* Set the output cursor. */
11171 output_cursor.hpos = hpos;
11172 output_cursor.vpos = vpos;
11173 output_cursor.x = x;
11174 output_cursor.y = y;
11175
11176 /* If not called as part of an update, really display the cursor.
11177 This will also set the cursor position of W. */
11178 if (updated_window == NULL)
11179 {
11180 BLOCK_INPUT;
11181 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11182 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11183 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11184 UNBLOCK_INPUT;
11185 }
11186 }
11187
11188 #endif /* HAVE_WINDOW_SYSTEM */
11189
11190 \f
11191 /***********************************************************************
11192 Tool-bars
11193 ***********************************************************************/
11194
11195 #ifdef HAVE_WINDOW_SYSTEM
11196
11197 /* Where the mouse was last time we reported a mouse event. */
11198
11199 FRAME_PTR last_mouse_frame;
11200
11201 /* Tool-bar item index of the item on which a mouse button was pressed
11202 or -1. */
11203
11204 int last_tool_bar_item;
11205
11206
11207 static Lisp_Object
11208 update_tool_bar_unwind (Lisp_Object frame)
11209 {
11210 selected_frame = frame;
11211 return Qnil;
11212 }
11213
11214 /* Update the tool-bar item list for frame F. This has to be done
11215 before we start to fill in any display lines. Called from
11216 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11217 and restore it here. */
11218
11219 static void
11220 update_tool_bar (struct frame *f, int save_match_data)
11221 {
11222 #if defined (USE_GTK) || defined (HAVE_NS)
11223 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11224 #else
11225 int do_update = WINDOWP (f->tool_bar_window)
11226 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11227 #endif
11228
11229 if (do_update)
11230 {
11231 Lisp_Object window;
11232 struct window *w;
11233
11234 window = FRAME_SELECTED_WINDOW (f);
11235 w = XWINDOW (window);
11236
11237 /* If the user has switched buffers or windows, we need to
11238 recompute to reflect the new bindings. But we'll
11239 recompute when update_mode_lines is set too; that means
11240 that people can use force-mode-line-update to request
11241 that the menu bar be recomputed. The adverse effect on
11242 the rest of the redisplay algorithm is about the same as
11243 windows_or_buffers_changed anyway. */
11244 if (windows_or_buffers_changed
11245 || !NILP (w->update_mode_line)
11246 || update_mode_lines
11247 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11248 < BUF_MODIFF (XBUFFER (w->buffer)))
11249 != !NILP (w->last_had_star))
11250 || ((!NILP (Vtransient_mark_mode)
11251 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11252 != !NILP (w->region_showing)))
11253 {
11254 struct buffer *prev = current_buffer;
11255 ptrdiff_t count = SPECPDL_INDEX ();
11256 Lisp_Object frame, new_tool_bar;
11257 int new_n_tool_bar;
11258 struct gcpro gcpro1;
11259
11260 /* Set current_buffer to the buffer of the selected
11261 window of the frame, so that we get the right local
11262 keymaps. */
11263 set_buffer_internal_1 (XBUFFER (w->buffer));
11264
11265 /* Save match data, if we must. */
11266 if (save_match_data)
11267 record_unwind_save_match_data ();
11268
11269 /* Make sure that we don't accidentally use bogus keymaps. */
11270 if (NILP (Voverriding_local_map_menu_flag))
11271 {
11272 specbind (Qoverriding_terminal_local_map, Qnil);
11273 specbind (Qoverriding_local_map, Qnil);
11274 }
11275
11276 GCPRO1 (new_tool_bar);
11277
11278 /* We must temporarily set the selected frame to this frame
11279 before calling tool_bar_items, because the calculation of
11280 the tool-bar keymap uses the selected frame (see
11281 `tool-bar-make-keymap' in tool-bar.el). */
11282 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11283 XSETFRAME (frame, f);
11284 selected_frame = frame;
11285
11286 /* Build desired tool-bar items from keymaps. */
11287 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11288 &new_n_tool_bar);
11289
11290 /* Redisplay the tool-bar if we changed it. */
11291 if (new_n_tool_bar != f->n_tool_bar_items
11292 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11293 {
11294 /* Redisplay that happens asynchronously due to an expose event
11295 may access f->tool_bar_items. Make sure we update both
11296 variables within BLOCK_INPUT so no such event interrupts. */
11297 BLOCK_INPUT;
11298 f->tool_bar_items = new_tool_bar;
11299 f->n_tool_bar_items = new_n_tool_bar;
11300 w->update_mode_line = Qt;
11301 UNBLOCK_INPUT;
11302 }
11303
11304 UNGCPRO;
11305
11306 unbind_to (count, Qnil);
11307 set_buffer_internal_1 (prev);
11308 }
11309 }
11310 }
11311
11312
11313 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11314 F's desired tool-bar contents. F->tool_bar_items must have
11315 been set up previously by calling prepare_menu_bars. */
11316
11317 static void
11318 build_desired_tool_bar_string (struct frame *f)
11319 {
11320 int i, size, size_needed;
11321 struct gcpro gcpro1, gcpro2, gcpro3;
11322 Lisp_Object image, plist, props;
11323
11324 image = plist = props = Qnil;
11325 GCPRO3 (image, plist, props);
11326
11327 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11328 Otherwise, make a new string. */
11329
11330 /* The size of the string we might be able to reuse. */
11331 size = (STRINGP (f->desired_tool_bar_string)
11332 ? SCHARS (f->desired_tool_bar_string)
11333 : 0);
11334
11335 /* We need one space in the string for each image. */
11336 size_needed = f->n_tool_bar_items;
11337
11338 /* Reuse f->desired_tool_bar_string, if possible. */
11339 if (size < size_needed || NILP (f->desired_tool_bar_string))
11340 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11341 make_number (' '));
11342 else
11343 {
11344 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11345 Fremove_text_properties (make_number (0), make_number (size),
11346 props, f->desired_tool_bar_string);
11347 }
11348
11349 /* Put a `display' property on the string for the images to display,
11350 put a `menu_item' property on tool-bar items with a value that
11351 is the index of the item in F's tool-bar item vector. */
11352 for (i = 0; i < f->n_tool_bar_items; ++i)
11353 {
11354 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11355
11356 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11357 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11358 int hmargin, vmargin, relief, idx, end;
11359
11360 /* If image is a vector, choose the image according to the
11361 button state. */
11362 image = PROP (TOOL_BAR_ITEM_IMAGES);
11363 if (VECTORP (image))
11364 {
11365 if (enabled_p)
11366 idx = (selected_p
11367 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11368 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11369 else
11370 idx = (selected_p
11371 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11372 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11373
11374 xassert (ASIZE (image) >= idx);
11375 image = AREF (image, idx);
11376 }
11377 else
11378 idx = -1;
11379
11380 /* Ignore invalid image specifications. */
11381 if (!valid_image_p (image))
11382 continue;
11383
11384 /* Display the tool-bar button pressed, or depressed. */
11385 plist = Fcopy_sequence (XCDR (image));
11386
11387 /* Compute margin and relief to draw. */
11388 relief = (tool_bar_button_relief >= 0
11389 ? tool_bar_button_relief
11390 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11391 hmargin = vmargin = relief;
11392
11393 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11394 INT_MAX - max (hmargin, vmargin)))
11395 {
11396 hmargin += XFASTINT (Vtool_bar_button_margin);
11397 vmargin += XFASTINT (Vtool_bar_button_margin);
11398 }
11399 else if (CONSP (Vtool_bar_button_margin))
11400 {
11401 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11402 INT_MAX - hmargin))
11403 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11404
11405 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11406 INT_MAX - vmargin))
11407 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11408 }
11409
11410 if (auto_raise_tool_bar_buttons_p)
11411 {
11412 /* Add a `:relief' property to the image spec if the item is
11413 selected. */
11414 if (selected_p)
11415 {
11416 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11417 hmargin -= relief;
11418 vmargin -= relief;
11419 }
11420 }
11421 else
11422 {
11423 /* If image is selected, display it pressed, i.e. with a
11424 negative relief. If it's not selected, display it with a
11425 raised relief. */
11426 plist = Fplist_put (plist, QCrelief,
11427 (selected_p
11428 ? make_number (-relief)
11429 : make_number (relief)));
11430 hmargin -= relief;
11431 vmargin -= relief;
11432 }
11433
11434 /* Put a margin around the image. */
11435 if (hmargin || vmargin)
11436 {
11437 if (hmargin == vmargin)
11438 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11439 else
11440 plist = Fplist_put (plist, QCmargin,
11441 Fcons (make_number (hmargin),
11442 make_number (vmargin)));
11443 }
11444
11445 /* If button is not enabled, and we don't have special images
11446 for the disabled state, make the image appear disabled by
11447 applying an appropriate algorithm to it. */
11448 if (!enabled_p && idx < 0)
11449 plist = Fplist_put (plist, QCconversion, Qdisabled);
11450
11451 /* Put a `display' text property on the string for the image to
11452 display. Put a `menu-item' property on the string that gives
11453 the start of this item's properties in the tool-bar items
11454 vector. */
11455 image = Fcons (Qimage, plist);
11456 props = list4 (Qdisplay, image,
11457 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11458
11459 /* Let the last image hide all remaining spaces in the tool bar
11460 string. The string can be longer than needed when we reuse a
11461 previous string. */
11462 if (i + 1 == f->n_tool_bar_items)
11463 end = SCHARS (f->desired_tool_bar_string);
11464 else
11465 end = i + 1;
11466 Fadd_text_properties (make_number (i), make_number (end),
11467 props, f->desired_tool_bar_string);
11468 #undef PROP
11469 }
11470
11471 UNGCPRO;
11472 }
11473
11474
11475 /* Display one line of the tool-bar of frame IT->f.
11476
11477 HEIGHT specifies the desired height of the tool-bar line.
11478 If the actual height of the glyph row is less than HEIGHT, the
11479 row's height is increased to HEIGHT, and the icons are centered
11480 vertically in the new height.
11481
11482 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11483 count a final empty row in case the tool-bar width exactly matches
11484 the window width.
11485 */
11486
11487 static void
11488 display_tool_bar_line (struct it *it, int height)
11489 {
11490 struct glyph_row *row = it->glyph_row;
11491 int max_x = it->last_visible_x;
11492 struct glyph *last;
11493
11494 prepare_desired_row (row);
11495 row->y = it->current_y;
11496
11497 /* Note that this isn't made use of if the face hasn't a box,
11498 so there's no need to check the face here. */
11499 it->start_of_box_run_p = 1;
11500
11501 while (it->current_x < max_x)
11502 {
11503 int x, n_glyphs_before, i, nglyphs;
11504 struct it it_before;
11505
11506 /* Get the next display element. */
11507 if (!get_next_display_element (it))
11508 {
11509 /* Don't count empty row if we are counting needed tool-bar lines. */
11510 if (height < 0 && !it->hpos)
11511 return;
11512 break;
11513 }
11514
11515 /* Produce glyphs. */
11516 n_glyphs_before = row->used[TEXT_AREA];
11517 it_before = *it;
11518
11519 PRODUCE_GLYPHS (it);
11520
11521 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11522 i = 0;
11523 x = it_before.current_x;
11524 while (i < nglyphs)
11525 {
11526 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11527
11528 if (x + glyph->pixel_width > max_x)
11529 {
11530 /* Glyph doesn't fit on line. Backtrack. */
11531 row->used[TEXT_AREA] = n_glyphs_before;
11532 *it = it_before;
11533 /* If this is the only glyph on this line, it will never fit on the
11534 tool-bar, so skip it. But ensure there is at least one glyph,
11535 so we don't accidentally disable the tool-bar. */
11536 if (n_glyphs_before == 0
11537 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11538 break;
11539 goto out;
11540 }
11541
11542 ++it->hpos;
11543 x += glyph->pixel_width;
11544 ++i;
11545 }
11546
11547 /* Stop at line end. */
11548 if (ITERATOR_AT_END_OF_LINE_P (it))
11549 break;
11550
11551 set_iterator_to_next (it, 1);
11552 }
11553
11554 out:;
11555
11556 row->displays_text_p = row->used[TEXT_AREA] != 0;
11557
11558 /* Use default face for the border below the tool bar.
11559
11560 FIXME: When auto-resize-tool-bars is grow-only, there is
11561 no additional border below the possibly empty tool-bar lines.
11562 So to make the extra empty lines look "normal", we have to
11563 use the tool-bar face for the border too. */
11564 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11565 it->face_id = DEFAULT_FACE_ID;
11566
11567 extend_face_to_end_of_line (it);
11568 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11569 last->right_box_line_p = 1;
11570 if (last == row->glyphs[TEXT_AREA])
11571 last->left_box_line_p = 1;
11572
11573 /* Make line the desired height and center it vertically. */
11574 if ((height -= it->max_ascent + it->max_descent) > 0)
11575 {
11576 /* Don't add more than one line height. */
11577 height %= FRAME_LINE_HEIGHT (it->f);
11578 it->max_ascent += height / 2;
11579 it->max_descent += (height + 1) / 2;
11580 }
11581
11582 compute_line_metrics (it);
11583
11584 /* If line is empty, make it occupy the rest of the tool-bar. */
11585 if (!row->displays_text_p)
11586 {
11587 row->height = row->phys_height = it->last_visible_y - row->y;
11588 row->visible_height = row->height;
11589 row->ascent = row->phys_ascent = 0;
11590 row->extra_line_spacing = 0;
11591 }
11592
11593 row->full_width_p = 1;
11594 row->continued_p = 0;
11595 row->truncated_on_left_p = 0;
11596 row->truncated_on_right_p = 0;
11597
11598 it->current_x = it->hpos = 0;
11599 it->current_y += row->height;
11600 ++it->vpos;
11601 ++it->glyph_row;
11602 }
11603
11604
11605 /* Max tool-bar height. */
11606
11607 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11608 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11609
11610 /* Value is the number of screen lines needed to make all tool-bar
11611 items of frame F visible. The number of actual rows needed is
11612 returned in *N_ROWS if non-NULL. */
11613
11614 static int
11615 tool_bar_lines_needed (struct frame *f, int *n_rows)
11616 {
11617 struct window *w = XWINDOW (f->tool_bar_window);
11618 struct it it;
11619 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11620 the desired matrix, so use (unused) mode-line row as temporary row to
11621 avoid destroying the first tool-bar row. */
11622 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11623
11624 /* Initialize an iterator for iteration over
11625 F->desired_tool_bar_string in the tool-bar window of frame F. */
11626 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11627 it.first_visible_x = 0;
11628 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11629 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11630 it.paragraph_embedding = L2R;
11631
11632 while (!ITERATOR_AT_END_P (&it))
11633 {
11634 clear_glyph_row (temp_row);
11635 it.glyph_row = temp_row;
11636 display_tool_bar_line (&it, -1);
11637 }
11638 clear_glyph_row (temp_row);
11639
11640 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11641 if (n_rows)
11642 *n_rows = it.vpos > 0 ? it.vpos : -1;
11643
11644 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11645 }
11646
11647
11648 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11649 0, 1, 0,
11650 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11651 (Lisp_Object frame)
11652 {
11653 struct frame *f;
11654 struct window *w;
11655 int nlines = 0;
11656
11657 if (NILP (frame))
11658 frame = selected_frame;
11659 else
11660 CHECK_FRAME (frame);
11661 f = XFRAME (frame);
11662
11663 if (WINDOWP (f->tool_bar_window)
11664 && (w = XWINDOW (f->tool_bar_window),
11665 WINDOW_TOTAL_LINES (w) > 0))
11666 {
11667 update_tool_bar (f, 1);
11668 if (f->n_tool_bar_items)
11669 {
11670 build_desired_tool_bar_string (f);
11671 nlines = tool_bar_lines_needed (f, NULL);
11672 }
11673 }
11674
11675 return make_number (nlines);
11676 }
11677
11678
11679 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11680 height should be changed. */
11681
11682 static int
11683 redisplay_tool_bar (struct frame *f)
11684 {
11685 struct window *w;
11686 struct it it;
11687 struct glyph_row *row;
11688
11689 #if defined (USE_GTK) || defined (HAVE_NS)
11690 if (FRAME_EXTERNAL_TOOL_BAR (f))
11691 update_frame_tool_bar (f);
11692 return 0;
11693 #endif
11694
11695 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11696 do anything. This means you must start with tool-bar-lines
11697 non-zero to get the auto-sizing effect. Or in other words, you
11698 can turn off tool-bars by specifying tool-bar-lines zero. */
11699 if (!WINDOWP (f->tool_bar_window)
11700 || (w = XWINDOW (f->tool_bar_window),
11701 WINDOW_TOTAL_LINES (w) == 0))
11702 return 0;
11703
11704 /* Set up an iterator for the tool-bar window. */
11705 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11706 it.first_visible_x = 0;
11707 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11708 row = it.glyph_row;
11709
11710 /* Build a string that represents the contents of the tool-bar. */
11711 build_desired_tool_bar_string (f);
11712 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11713 /* FIXME: This should be controlled by a user option. But it
11714 doesn't make sense to have an R2L tool bar if the menu bar cannot
11715 be drawn also R2L, and making the menu bar R2L is tricky due
11716 toolkit-specific code that implements it. If an R2L tool bar is
11717 ever supported, display_tool_bar_line should also be augmented to
11718 call unproduce_glyphs like display_line and display_string
11719 do. */
11720 it.paragraph_embedding = L2R;
11721
11722 if (f->n_tool_bar_rows == 0)
11723 {
11724 int nlines;
11725
11726 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11727 nlines != WINDOW_TOTAL_LINES (w)))
11728 {
11729 Lisp_Object frame;
11730 int old_height = WINDOW_TOTAL_LINES (w);
11731
11732 XSETFRAME (frame, f);
11733 Fmodify_frame_parameters (frame,
11734 Fcons (Fcons (Qtool_bar_lines,
11735 make_number (nlines)),
11736 Qnil));
11737 if (WINDOW_TOTAL_LINES (w) != old_height)
11738 {
11739 clear_glyph_matrix (w->desired_matrix);
11740 fonts_changed_p = 1;
11741 return 1;
11742 }
11743 }
11744 }
11745
11746 /* Display as many lines as needed to display all tool-bar items. */
11747
11748 if (f->n_tool_bar_rows > 0)
11749 {
11750 int border, rows, height, extra;
11751
11752 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11753 border = XINT (Vtool_bar_border);
11754 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11755 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11756 else if (EQ (Vtool_bar_border, Qborder_width))
11757 border = f->border_width;
11758 else
11759 border = 0;
11760 if (border < 0)
11761 border = 0;
11762
11763 rows = f->n_tool_bar_rows;
11764 height = max (1, (it.last_visible_y - border) / rows);
11765 extra = it.last_visible_y - border - height * rows;
11766
11767 while (it.current_y < it.last_visible_y)
11768 {
11769 int h = 0;
11770 if (extra > 0 && rows-- > 0)
11771 {
11772 h = (extra + rows - 1) / rows;
11773 extra -= h;
11774 }
11775 display_tool_bar_line (&it, height + h);
11776 }
11777 }
11778 else
11779 {
11780 while (it.current_y < it.last_visible_y)
11781 display_tool_bar_line (&it, 0);
11782 }
11783
11784 /* It doesn't make much sense to try scrolling in the tool-bar
11785 window, so don't do it. */
11786 w->desired_matrix->no_scrolling_p = 1;
11787 w->must_be_updated_p = 1;
11788
11789 if (!NILP (Vauto_resize_tool_bars))
11790 {
11791 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11792 int change_height_p = 0;
11793
11794 /* If we couldn't display everything, change the tool-bar's
11795 height if there is room for more. */
11796 if (IT_STRING_CHARPOS (it) < it.end_charpos
11797 && it.current_y < max_tool_bar_height)
11798 change_height_p = 1;
11799
11800 row = it.glyph_row - 1;
11801
11802 /* If there are blank lines at the end, except for a partially
11803 visible blank line at the end that is smaller than
11804 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11805 if (!row->displays_text_p
11806 && row->height >= FRAME_LINE_HEIGHT (f))
11807 change_height_p = 1;
11808
11809 /* If row displays tool-bar items, but is partially visible,
11810 change the tool-bar's height. */
11811 if (row->displays_text_p
11812 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11813 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11814 change_height_p = 1;
11815
11816 /* Resize windows as needed by changing the `tool-bar-lines'
11817 frame parameter. */
11818 if (change_height_p)
11819 {
11820 Lisp_Object frame;
11821 int old_height = WINDOW_TOTAL_LINES (w);
11822 int nrows;
11823 int nlines = tool_bar_lines_needed (f, &nrows);
11824
11825 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11826 && !f->minimize_tool_bar_window_p)
11827 ? (nlines > old_height)
11828 : (nlines != old_height));
11829 f->minimize_tool_bar_window_p = 0;
11830
11831 if (change_height_p)
11832 {
11833 XSETFRAME (frame, f);
11834 Fmodify_frame_parameters (frame,
11835 Fcons (Fcons (Qtool_bar_lines,
11836 make_number (nlines)),
11837 Qnil));
11838 if (WINDOW_TOTAL_LINES (w) != old_height)
11839 {
11840 clear_glyph_matrix (w->desired_matrix);
11841 f->n_tool_bar_rows = nrows;
11842 fonts_changed_p = 1;
11843 return 1;
11844 }
11845 }
11846 }
11847 }
11848
11849 f->minimize_tool_bar_window_p = 0;
11850 return 0;
11851 }
11852
11853
11854 /* Get information about the tool-bar item which is displayed in GLYPH
11855 on frame F. Return in *PROP_IDX the index where tool-bar item
11856 properties start in F->tool_bar_items. Value is zero if
11857 GLYPH doesn't display a tool-bar item. */
11858
11859 static int
11860 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11861 {
11862 Lisp_Object prop;
11863 int success_p;
11864 int charpos;
11865
11866 /* This function can be called asynchronously, which means we must
11867 exclude any possibility that Fget_text_property signals an
11868 error. */
11869 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11870 charpos = max (0, charpos);
11871
11872 /* Get the text property `menu-item' at pos. The value of that
11873 property is the start index of this item's properties in
11874 F->tool_bar_items. */
11875 prop = Fget_text_property (make_number (charpos),
11876 Qmenu_item, f->current_tool_bar_string);
11877 if (INTEGERP (prop))
11878 {
11879 *prop_idx = XINT (prop);
11880 success_p = 1;
11881 }
11882 else
11883 success_p = 0;
11884
11885 return success_p;
11886 }
11887
11888 \f
11889 /* Get information about the tool-bar item at position X/Y on frame F.
11890 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11891 the current matrix of the tool-bar window of F, or NULL if not
11892 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11893 item in F->tool_bar_items. Value is
11894
11895 -1 if X/Y is not on a tool-bar item
11896 0 if X/Y is on the same item that was highlighted before.
11897 1 otherwise. */
11898
11899 static int
11900 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11901 int *hpos, int *vpos, int *prop_idx)
11902 {
11903 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11904 struct window *w = XWINDOW (f->tool_bar_window);
11905 int area;
11906
11907 /* Find the glyph under X/Y. */
11908 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11909 if (*glyph == NULL)
11910 return -1;
11911
11912 /* Get the start of this tool-bar item's properties in
11913 f->tool_bar_items. */
11914 if (!tool_bar_item_info (f, *glyph, prop_idx))
11915 return -1;
11916
11917 /* Is mouse on the highlighted item? */
11918 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11919 && *vpos >= hlinfo->mouse_face_beg_row
11920 && *vpos <= hlinfo->mouse_face_end_row
11921 && (*vpos > hlinfo->mouse_face_beg_row
11922 || *hpos >= hlinfo->mouse_face_beg_col)
11923 && (*vpos < hlinfo->mouse_face_end_row
11924 || *hpos < hlinfo->mouse_face_end_col
11925 || hlinfo->mouse_face_past_end))
11926 return 0;
11927
11928 return 1;
11929 }
11930
11931
11932 /* EXPORT:
11933 Handle mouse button event on the tool-bar of frame F, at
11934 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11935 0 for button release. MODIFIERS is event modifiers for button
11936 release. */
11937
11938 void
11939 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11940 int modifiers)
11941 {
11942 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11943 struct window *w = XWINDOW (f->tool_bar_window);
11944 int hpos, vpos, prop_idx;
11945 struct glyph *glyph;
11946 Lisp_Object enabled_p;
11947
11948 /* If not on the highlighted tool-bar item, return. */
11949 frame_to_window_pixel_xy (w, &x, &y);
11950 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11951 return;
11952
11953 /* If item is disabled, do nothing. */
11954 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11955 if (NILP (enabled_p))
11956 return;
11957
11958 if (down_p)
11959 {
11960 /* Show item in pressed state. */
11961 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11962 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11963 last_tool_bar_item = prop_idx;
11964 }
11965 else
11966 {
11967 Lisp_Object key, frame;
11968 struct input_event event;
11969 EVENT_INIT (event);
11970
11971 /* Show item in released state. */
11972 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11973 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11974
11975 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11976
11977 XSETFRAME (frame, f);
11978 event.kind = TOOL_BAR_EVENT;
11979 event.frame_or_window = frame;
11980 event.arg = frame;
11981 kbd_buffer_store_event (&event);
11982
11983 event.kind = TOOL_BAR_EVENT;
11984 event.frame_or_window = frame;
11985 event.arg = key;
11986 event.modifiers = modifiers;
11987 kbd_buffer_store_event (&event);
11988 last_tool_bar_item = -1;
11989 }
11990 }
11991
11992
11993 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11994 tool-bar window-relative coordinates X/Y. Called from
11995 note_mouse_highlight. */
11996
11997 static void
11998 note_tool_bar_highlight (struct frame *f, int x, int y)
11999 {
12000 Lisp_Object window = f->tool_bar_window;
12001 struct window *w = XWINDOW (window);
12002 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12003 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12004 int hpos, vpos;
12005 struct glyph *glyph;
12006 struct glyph_row *row;
12007 int i;
12008 Lisp_Object enabled_p;
12009 int prop_idx;
12010 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12011 int mouse_down_p, rc;
12012
12013 /* Function note_mouse_highlight is called with negative X/Y
12014 values when mouse moves outside of the frame. */
12015 if (x <= 0 || y <= 0)
12016 {
12017 clear_mouse_face (hlinfo);
12018 return;
12019 }
12020
12021 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12022 if (rc < 0)
12023 {
12024 /* Not on tool-bar item. */
12025 clear_mouse_face (hlinfo);
12026 return;
12027 }
12028 else if (rc == 0)
12029 /* On same tool-bar item as before. */
12030 goto set_help_echo;
12031
12032 clear_mouse_face (hlinfo);
12033
12034 /* Mouse is down, but on different tool-bar item? */
12035 mouse_down_p = (dpyinfo->grabbed
12036 && f == last_mouse_frame
12037 && FRAME_LIVE_P (f));
12038 if (mouse_down_p
12039 && last_tool_bar_item != prop_idx)
12040 return;
12041
12042 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12043 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12044
12045 /* If tool-bar item is not enabled, don't highlight it. */
12046 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12047 if (!NILP (enabled_p))
12048 {
12049 /* Compute the x-position of the glyph. In front and past the
12050 image is a space. We include this in the highlighted area. */
12051 row = MATRIX_ROW (w->current_matrix, vpos);
12052 for (i = x = 0; i < hpos; ++i)
12053 x += row->glyphs[TEXT_AREA][i].pixel_width;
12054
12055 /* Record this as the current active region. */
12056 hlinfo->mouse_face_beg_col = hpos;
12057 hlinfo->mouse_face_beg_row = vpos;
12058 hlinfo->mouse_face_beg_x = x;
12059 hlinfo->mouse_face_beg_y = row->y;
12060 hlinfo->mouse_face_past_end = 0;
12061
12062 hlinfo->mouse_face_end_col = hpos + 1;
12063 hlinfo->mouse_face_end_row = vpos;
12064 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12065 hlinfo->mouse_face_end_y = row->y;
12066 hlinfo->mouse_face_window = window;
12067 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12068
12069 /* Display it as active. */
12070 show_mouse_face (hlinfo, draw);
12071 hlinfo->mouse_face_image_state = draw;
12072 }
12073
12074 set_help_echo:
12075
12076 /* Set help_echo_string to a help string to display for this tool-bar item.
12077 XTread_socket does the rest. */
12078 help_echo_object = help_echo_window = Qnil;
12079 help_echo_pos = -1;
12080 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12081 if (NILP (help_echo_string))
12082 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12083 }
12084
12085 #endif /* HAVE_WINDOW_SYSTEM */
12086
12087
12088 \f
12089 /************************************************************************
12090 Horizontal scrolling
12091 ************************************************************************/
12092
12093 static int hscroll_window_tree (Lisp_Object);
12094 static int hscroll_windows (Lisp_Object);
12095
12096 /* For all leaf windows in the window tree rooted at WINDOW, set their
12097 hscroll value so that PT is (i) visible in the window, and (ii) so
12098 that it is not within a certain margin at the window's left and
12099 right border. Value is non-zero if any window's hscroll has been
12100 changed. */
12101
12102 static int
12103 hscroll_window_tree (Lisp_Object window)
12104 {
12105 int hscrolled_p = 0;
12106 int hscroll_relative_p = FLOATP (Vhscroll_step);
12107 int hscroll_step_abs = 0;
12108 double hscroll_step_rel = 0;
12109
12110 if (hscroll_relative_p)
12111 {
12112 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12113 if (hscroll_step_rel < 0)
12114 {
12115 hscroll_relative_p = 0;
12116 hscroll_step_abs = 0;
12117 }
12118 }
12119 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12120 {
12121 hscroll_step_abs = XINT (Vhscroll_step);
12122 if (hscroll_step_abs < 0)
12123 hscroll_step_abs = 0;
12124 }
12125 else
12126 hscroll_step_abs = 0;
12127
12128 while (WINDOWP (window))
12129 {
12130 struct window *w = XWINDOW (window);
12131
12132 if (WINDOWP (w->hchild))
12133 hscrolled_p |= hscroll_window_tree (w->hchild);
12134 else if (WINDOWP (w->vchild))
12135 hscrolled_p |= hscroll_window_tree (w->vchild);
12136 else if (w->cursor.vpos >= 0)
12137 {
12138 int h_margin;
12139 int text_area_width;
12140 struct glyph_row *current_cursor_row
12141 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12142 struct glyph_row *desired_cursor_row
12143 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12144 struct glyph_row *cursor_row
12145 = (desired_cursor_row->enabled_p
12146 ? desired_cursor_row
12147 : current_cursor_row);
12148 int row_r2l_p = cursor_row->reversed_p;
12149
12150 text_area_width = window_box_width (w, TEXT_AREA);
12151
12152 /* Scroll when cursor is inside this scroll margin. */
12153 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12154
12155 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12156 /* For left-to-right rows, hscroll when cursor is either
12157 (i) inside the right hscroll margin, or (ii) if it is
12158 inside the left margin and the window is already
12159 hscrolled. */
12160 && ((!row_r2l_p
12161 && ((XFASTINT (w->hscroll)
12162 && w->cursor.x <= h_margin)
12163 || (cursor_row->enabled_p
12164 && cursor_row->truncated_on_right_p
12165 && (w->cursor.x >= text_area_width - h_margin))))
12166 /* For right-to-left rows, the logic is similar,
12167 except that rules for scrolling to left and right
12168 are reversed. E.g., if cursor.x <= h_margin, we
12169 need to hscroll "to the right" unconditionally,
12170 and that will scroll the screen to the left so as
12171 to reveal the next portion of the row. */
12172 || (row_r2l_p
12173 && ((cursor_row->enabled_p
12174 /* FIXME: It is confusing to set the
12175 truncated_on_right_p flag when R2L rows
12176 are actually truncated on the left. */
12177 && cursor_row->truncated_on_right_p
12178 && w->cursor.x <= h_margin)
12179 || (XFASTINT (w->hscroll)
12180 && (w->cursor.x >= text_area_width - h_margin))))))
12181 {
12182 struct it it;
12183 ptrdiff_t hscroll;
12184 struct buffer *saved_current_buffer;
12185 ptrdiff_t pt;
12186 int wanted_x;
12187
12188 /* Find point in a display of infinite width. */
12189 saved_current_buffer = current_buffer;
12190 current_buffer = XBUFFER (w->buffer);
12191
12192 if (w == XWINDOW (selected_window))
12193 pt = PT;
12194 else
12195 {
12196 pt = marker_position (w->pointm);
12197 pt = max (BEGV, pt);
12198 pt = min (ZV, pt);
12199 }
12200
12201 /* Move iterator to pt starting at cursor_row->start in
12202 a line with infinite width. */
12203 init_to_row_start (&it, w, cursor_row);
12204 it.last_visible_x = INFINITY;
12205 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12206 current_buffer = saved_current_buffer;
12207
12208 /* Position cursor in window. */
12209 if (!hscroll_relative_p && hscroll_step_abs == 0)
12210 hscroll = max (0, (it.current_x
12211 - (ITERATOR_AT_END_OF_LINE_P (&it)
12212 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12213 : (text_area_width / 2))))
12214 / FRAME_COLUMN_WIDTH (it.f);
12215 else if ((!row_r2l_p
12216 && w->cursor.x >= text_area_width - h_margin)
12217 || (row_r2l_p && w->cursor.x <= h_margin))
12218 {
12219 if (hscroll_relative_p)
12220 wanted_x = text_area_width * (1 - hscroll_step_rel)
12221 - h_margin;
12222 else
12223 wanted_x = text_area_width
12224 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12225 - h_margin;
12226 hscroll
12227 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12228 }
12229 else
12230 {
12231 if (hscroll_relative_p)
12232 wanted_x = text_area_width * hscroll_step_rel
12233 + h_margin;
12234 else
12235 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12236 + h_margin;
12237 hscroll
12238 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12239 }
12240 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12241
12242 /* Don't prevent redisplay optimizations if hscroll
12243 hasn't changed, as it will unnecessarily slow down
12244 redisplay. */
12245 if (XFASTINT (w->hscroll) != hscroll)
12246 {
12247 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12248 w->hscroll = make_number (hscroll);
12249 hscrolled_p = 1;
12250 }
12251 }
12252 }
12253
12254 window = w->next;
12255 }
12256
12257 /* Value is non-zero if hscroll of any leaf window has been changed. */
12258 return hscrolled_p;
12259 }
12260
12261
12262 /* Set hscroll so that cursor is visible and not inside horizontal
12263 scroll margins for all windows in the tree rooted at WINDOW. See
12264 also hscroll_window_tree above. Value is non-zero if any window's
12265 hscroll has been changed. If it has, desired matrices on the frame
12266 of WINDOW are cleared. */
12267
12268 static int
12269 hscroll_windows (Lisp_Object window)
12270 {
12271 int hscrolled_p = hscroll_window_tree (window);
12272 if (hscrolled_p)
12273 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12274 return hscrolled_p;
12275 }
12276
12277
12278 \f
12279 /************************************************************************
12280 Redisplay
12281 ************************************************************************/
12282
12283 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12284 to a non-zero value. This is sometimes handy to have in a debugger
12285 session. */
12286
12287 #if GLYPH_DEBUG
12288
12289 /* First and last unchanged row for try_window_id. */
12290
12291 static int debug_first_unchanged_at_end_vpos;
12292 static int debug_last_unchanged_at_beg_vpos;
12293
12294 /* Delta vpos and y. */
12295
12296 static int debug_dvpos, debug_dy;
12297
12298 /* Delta in characters and bytes for try_window_id. */
12299
12300 static ptrdiff_t debug_delta, debug_delta_bytes;
12301
12302 /* Values of window_end_pos and window_end_vpos at the end of
12303 try_window_id. */
12304
12305 static ptrdiff_t debug_end_vpos;
12306
12307 /* Append a string to W->desired_matrix->method. FMT is a printf
12308 format string. If trace_redisplay_p is non-zero also printf the
12309 resulting string to stderr. */
12310
12311 static void debug_method_add (struct window *, char const *, ...)
12312 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12313
12314 static void
12315 debug_method_add (struct window *w, char const *fmt, ...)
12316 {
12317 char buffer[512];
12318 char *method = w->desired_matrix->method;
12319 int len = strlen (method);
12320 int size = sizeof w->desired_matrix->method;
12321 int remaining = size - len - 1;
12322 va_list ap;
12323
12324 va_start (ap, fmt);
12325 vsprintf (buffer, fmt, ap);
12326 va_end (ap);
12327 if (len && remaining)
12328 {
12329 method[len] = '|';
12330 --remaining, ++len;
12331 }
12332
12333 strncpy (method + len, buffer, remaining);
12334
12335 if (trace_redisplay_p)
12336 fprintf (stderr, "%p (%s): %s\n",
12337 w,
12338 ((BUFFERP (w->buffer)
12339 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12340 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12341 : "no buffer"),
12342 buffer);
12343 }
12344
12345 #endif /* GLYPH_DEBUG */
12346
12347
12348 /* Value is non-zero if all changes in window W, which displays
12349 current_buffer, are in the text between START and END. START is a
12350 buffer position, END is given as a distance from Z. Used in
12351 redisplay_internal for display optimization. */
12352
12353 static inline int
12354 text_outside_line_unchanged_p (struct window *w,
12355 ptrdiff_t start, ptrdiff_t end)
12356 {
12357 int unchanged_p = 1;
12358
12359 /* If text or overlays have changed, see where. */
12360 if (XFASTINT (w->last_modified) < MODIFF
12361 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12362 {
12363 /* Gap in the line? */
12364 if (GPT < start || Z - GPT < end)
12365 unchanged_p = 0;
12366
12367 /* Changes start in front of the line, or end after it? */
12368 if (unchanged_p
12369 && (BEG_UNCHANGED < start - 1
12370 || END_UNCHANGED < end))
12371 unchanged_p = 0;
12372
12373 /* If selective display, can't optimize if changes start at the
12374 beginning of the line. */
12375 if (unchanged_p
12376 && INTEGERP (BVAR (current_buffer, selective_display))
12377 && XINT (BVAR (current_buffer, selective_display)) > 0
12378 && (BEG_UNCHANGED < start || GPT <= start))
12379 unchanged_p = 0;
12380
12381 /* If there are overlays at the start or end of the line, these
12382 may have overlay strings with newlines in them. A change at
12383 START, for instance, may actually concern the display of such
12384 overlay strings as well, and they are displayed on different
12385 lines. So, quickly rule out this case. (For the future, it
12386 might be desirable to implement something more telling than
12387 just BEG/END_UNCHANGED.) */
12388 if (unchanged_p)
12389 {
12390 if (BEG + BEG_UNCHANGED == start
12391 && overlay_touches_p (start))
12392 unchanged_p = 0;
12393 if (END_UNCHANGED == end
12394 && overlay_touches_p (Z - end))
12395 unchanged_p = 0;
12396 }
12397
12398 /* Under bidi reordering, adding or deleting a character in the
12399 beginning of a paragraph, before the first strong directional
12400 character, can change the base direction of the paragraph (unless
12401 the buffer specifies a fixed paragraph direction), which will
12402 require to redisplay the whole paragraph. It might be worthwhile
12403 to find the paragraph limits and widen the range of redisplayed
12404 lines to that, but for now just give up this optimization. */
12405 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12406 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12407 unchanged_p = 0;
12408 }
12409
12410 return unchanged_p;
12411 }
12412
12413
12414 /* Do a frame update, taking possible shortcuts into account. This is
12415 the main external entry point for redisplay.
12416
12417 If the last redisplay displayed an echo area message and that message
12418 is no longer requested, we clear the echo area or bring back the
12419 mini-buffer if that is in use. */
12420
12421 void
12422 redisplay (void)
12423 {
12424 redisplay_internal ();
12425 }
12426
12427
12428 static Lisp_Object
12429 overlay_arrow_string_or_property (Lisp_Object var)
12430 {
12431 Lisp_Object val;
12432
12433 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12434 return val;
12435
12436 return Voverlay_arrow_string;
12437 }
12438
12439 /* Return 1 if there are any overlay-arrows in current_buffer. */
12440 static int
12441 overlay_arrow_in_current_buffer_p (void)
12442 {
12443 Lisp_Object vlist;
12444
12445 for (vlist = Voverlay_arrow_variable_list;
12446 CONSP (vlist);
12447 vlist = XCDR (vlist))
12448 {
12449 Lisp_Object var = XCAR (vlist);
12450 Lisp_Object val;
12451
12452 if (!SYMBOLP (var))
12453 continue;
12454 val = find_symbol_value (var);
12455 if (MARKERP (val)
12456 && current_buffer == XMARKER (val)->buffer)
12457 return 1;
12458 }
12459 return 0;
12460 }
12461
12462
12463 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12464 has changed. */
12465
12466 static int
12467 overlay_arrows_changed_p (void)
12468 {
12469 Lisp_Object vlist;
12470
12471 for (vlist = Voverlay_arrow_variable_list;
12472 CONSP (vlist);
12473 vlist = XCDR (vlist))
12474 {
12475 Lisp_Object var = XCAR (vlist);
12476 Lisp_Object val, pstr;
12477
12478 if (!SYMBOLP (var))
12479 continue;
12480 val = find_symbol_value (var);
12481 if (!MARKERP (val))
12482 continue;
12483 if (! EQ (COERCE_MARKER (val),
12484 Fget (var, Qlast_arrow_position))
12485 || ! (pstr = overlay_arrow_string_or_property (var),
12486 EQ (pstr, Fget (var, Qlast_arrow_string))))
12487 return 1;
12488 }
12489 return 0;
12490 }
12491
12492 /* Mark overlay arrows to be updated on next redisplay. */
12493
12494 static void
12495 update_overlay_arrows (int up_to_date)
12496 {
12497 Lisp_Object vlist;
12498
12499 for (vlist = Voverlay_arrow_variable_list;
12500 CONSP (vlist);
12501 vlist = XCDR (vlist))
12502 {
12503 Lisp_Object var = XCAR (vlist);
12504
12505 if (!SYMBOLP (var))
12506 continue;
12507
12508 if (up_to_date > 0)
12509 {
12510 Lisp_Object val = find_symbol_value (var);
12511 Fput (var, Qlast_arrow_position,
12512 COERCE_MARKER (val));
12513 Fput (var, Qlast_arrow_string,
12514 overlay_arrow_string_or_property (var));
12515 }
12516 else if (up_to_date < 0
12517 || !NILP (Fget (var, Qlast_arrow_position)))
12518 {
12519 Fput (var, Qlast_arrow_position, Qt);
12520 Fput (var, Qlast_arrow_string, Qt);
12521 }
12522 }
12523 }
12524
12525
12526 /* Return overlay arrow string to display at row.
12527 Return integer (bitmap number) for arrow bitmap in left fringe.
12528 Return nil if no overlay arrow. */
12529
12530 static Lisp_Object
12531 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12532 {
12533 Lisp_Object vlist;
12534
12535 for (vlist = Voverlay_arrow_variable_list;
12536 CONSP (vlist);
12537 vlist = XCDR (vlist))
12538 {
12539 Lisp_Object var = XCAR (vlist);
12540 Lisp_Object val;
12541
12542 if (!SYMBOLP (var))
12543 continue;
12544
12545 val = find_symbol_value (var);
12546
12547 if (MARKERP (val)
12548 && current_buffer == XMARKER (val)->buffer
12549 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12550 {
12551 if (FRAME_WINDOW_P (it->f)
12552 /* FIXME: if ROW->reversed_p is set, this should test
12553 the right fringe, not the left one. */
12554 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12555 {
12556 #ifdef HAVE_WINDOW_SYSTEM
12557 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12558 {
12559 int fringe_bitmap;
12560 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12561 return make_number (fringe_bitmap);
12562 }
12563 #endif
12564 return make_number (-1); /* Use default arrow bitmap */
12565 }
12566 return overlay_arrow_string_or_property (var);
12567 }
12568 }
12569
12570 return Qnil;
12571 }
12572
12573 /* Return 1 if point moved out of or into a composition. Otherwise
12574 return 0. PREV_BUF and PREV_PT are the last point buffer and
12575 position. BUF and PT are the current point buffer and position. */
12576
12577 static int
12578 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12579 struct buffer *buf, ptrdiff_t pt)
12580 {
12581 ptrdiff_t start, end;
12582 Lisp_Object prop;
12583 Lisp_Object buffer;
12584
12585 XSETBUFFER (buffer, buf);
12586 /* Check a composition at the last point if point moved within the
12587 same buffer. */
12588 if (prev_buf == buf)
12589 {
12590 if (prev_pt == pt)
12591 /* Point didn't move. */
12592 return 0;
12593
12594 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12595 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12596 && COMPOSITION_VALID_P (start, end, prop)
12597 && start < prev_pt && end > prev_pt)
12598 /* The last point was within the composition. Return 1 iff
12599 point moved out of the composition. */
12600 return (pt <= start || pt >= end);
12601 }
12602
12603 /* Check a composition at the current point. */
12604 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12605 && find_composition (pt, -1, &start, &end, &prop, buffer)
12606 && COMPOSITION_VALID_P (start, end, prop)
12607 && start < pt && end > pt);
12608 }
12609
12610
12611 /* Reconsider the setting of B->clip_changed which is displayed
12612 in window W. */
12613
12614 static inline void
12615 reconsider_clip_changes (struct window *w, struct buffer *b)
12616 {
12617 if (b->clip_changed
12618 && !NILP (w->window_end_valid)
12619 && w->current_matrix->buffer == b
12620 && w->current_matrix->zv == BUF_ZV (b)
12621 && w->current_matrix->begv == BUF_BEGV (b))
12622 b->clip_changed = 0;
12623
12624 /* If display wasn't paused, and W is not a tool bar window, see if
12625 point has been moved into or out of a composition. In that case,
12626 we set b->clip_changed to 1 to force updating the screen. If
12627 b->clip_changed has already been set to 1, we can skip this
12628 check. */
12629 if (!b->clip_changed
12630 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12631 {
12632 ptrdiff_t pt;
12633
12634 if (w == XWINDOW (selected_window))
12635 pt = PT;
12636 else
12637 pt = marker_position (w->pointm);
12638
12639 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12640 || pt != XINT (w->last_point))
12641 && check_point_in_composition (w->current_matrix->buffer,
12642 XINT (w->last_point),
12643 XBUFFER (w->buffer), pt))
12644 b->clip_changed = 1;
12645 }
12646 }
12647 \f
12648
12649 /* Select FRAME to forward the values of frame-local variables into C
12650 variables so that the redisplay routines can access those values
12651 directly. */
12652
12653 static void
12654 select_frame_for_redisplay (Lisp_Object frame)
12655 {
12656 Lisp_Object tail, tem;
12657 Lisp_Object old = selected_frame;
12658 struct Lisp_Symbol *sym;
12659
12660 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12661
12662 selected_frame = frame;
12663
12664 do {
12665 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12666 if (CONSP (XCAR (tail))
12667 && (tem = XCAR (XCAR (tail)),
12668 SYMBOLP (tem))
12669 && (sym = indirect_variable (XSYMBOL (tem)),
12670 sym->redirect == SYMBOL_LOCALIZED)
12671 && sym->val.blv->frame_local)
12672 /* Use find_symbol_value rather than Fsymbol_value
12673 to avoid an error if it is void. */
12674 find_symbol_value (tem);
12675 } while (!EQ (frame, old) && (frame = old, 1));
12676 }
12677
12678
12679 #define STOP_POLLING \
12680 do { if (! polling_stopped_here) stop_polling (); \
12681 polling_stopped_here = 1; } while (0)
12682
12683 #define RESUME_POLLING \
12684 do { if (polling_stopped_here) start_polling (); \
12685 polling_stopped_here = 0; } while (0)
12686
12687
12688 /* Perhaps in the future avoid recentering windows if it
12689 is not necessary; currently that causes some problems. */
12690
12691 static void
12692 redisplay_internal (void)
12693 {
12694 struct window *w = XWINDOW (selected_window);
12695 struct window *sw;
12696 struct frame *fr;
12697 int pending;
12698 int must_finish = 0;
12699 struct text_pos tlbufpos, tlendpos;
12700 int number_of_visible_frames;
12701 ptrdiff_t count, count1;
12702 struct frame *sf;
12703 int polling_stopped_here = 0;
12704 Lisp_Object old_frame = selected_frame;
12705
12706 /* Non-zero means redisplay has to consider all windows on all
12707 frames. Zero means, only selected_window is considered. */
12708 int consider_all_windows_p;
12709
12710 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12711
12712 /* No redisplay if running in batch mode or frame is not yet fully
12713 initialized, or redisplay is explicitly turned off by setting
12714 Vinhibit_redisplay. */
12715 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12716 || !NILP (Vinhibit_redisplay))
12717 return;
12718
12719 /* Don't examine these until after testing Vinhibit_redisplay.
12720 When Emacs is shutting down, perhaps because its connection to
12721 X has dropped, we should not look at them at all. */
12722 fr = XFRAME (w->frame);
12723 sf = SELECTED_FRAME ();
12724
12725 if (!fr->glyphs_initialized_p)
12726 return;
12727
12728 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12729 if (popup_activated ())
12730 return;
12731 #endif
12732
12733 /* I don't think this happens but let's be paranoid. */
12734 if (redisplaying_p)
12735 return;
12736
12737 /* Record a function that resets redisplaying_p to its old value
12738 when we leave this function. */
12739 count = SPECPDL_INDEX ();
12740 record_unwind_protect (unwind_redisplay,
12741 Fcons (make_number (redisplaying_p), selected_frame));
12742 ++redisplaying_p;
12743 specbind (Qinhibit_free_realized_faces, Qnil);
12744
12745 {
12746 Lisp_Object tail, frame;
12747
12748 FOR_EACH_FRAME (tail, frame)
12749 {
12750 struct frame *f = XFRAME (frame);
12751 f->already_hscrolled_p = 0;
12752 }
12753 }
12754
12755 retry:
12756 /* Remember the currently selected window. */
12757 sw = w;
12758
12759 if (!EQ (old_frame, selected_frame)
12760 && FRAME_LIVE_P (XFRAME (old_frame)))
12761 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12762 selected_frame and selected_window to be temporarily out-of-sync so
12763 when we come back here via `goto retry', we need to resync because we
12764 may need to run Elisp code (via prepare_menu_bars). */
12765 select_frame_for_redisplay (old_frame);
12766
12767 pending = 0;
12768 reconsider_clip_changes (w, current_buffer);
12769 last_escape_glyph_frame = NULL;
12770 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12771 last_glyphless_glyph_frame = NULL;
12772 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12773
12774 /* If new fonts have been loaded that make a glyph matrix adjustment
12775 necessary, do it. */
12776 if (fonts_changed_p)
12777 {
12778 adjust_glyphs (NULL);
12779 ++windows_or_buffers_changed;
12780 fonts_changed_p = 0;
12781 }
12782
12783 /* If face_change_count is non-zero, init_iterator will free all
12784 realized faces, which includes the faces referenced from current
12785 matrices. So, we can't reuse current matrices in this case. */
12786 if (face_change_count)
12787 ++windows_or_buffers_changed;
12788
12789 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12790 && FRAME_TTY (sf)->previous_frame != sf)
12791 {
12792 /* Since frames on a single ASCII terminal share the same
12793 display area, displaying a different frame means redisplay
12794 the whole thing. */
12795 windows_or_buffers_changed++;
12796 SET_FRAME_GARBAGED (sf);
12797 #ifndef DOS_NT
12798 set_tty_color_mode (FRAME_TTY (sf), sf);
12799 #endif
12800 FRAME_TTY (sf)->previous_frame = sf;
12801 }
12802
12803 /* Set the visible flags for all frames. Do this before checking
12804 for resized or garbaged frames; they want to know if their frames
12805 are visible. See the comment in frame.h for
12806 FRAME_SAMPLE_VISIBILITY. */
12807 {
12808 Lisp_Object tail, frame;
12809
12810 number_of_visible_frames = 0;
12811
12812 FOR_EACH_FRAME (tail, frame)
12813 {
12814 struct frame *f = XFRAME (frame);
12815
12816 FRAME_SAMPLE_VISIBILITY (f);
12817 if (FRAME_VISIBLE_P (f))
12818 ++number_of_visible_frames;
12819 clear_desired_matrices (f);
12820 }
12821 }
12822
12823 /* Notice any pending interrupt request to change frame size. */
12824 do_pending_window_change (1);
12825
12826 /* do_pending_window_change could change the selected_window due to
12827 frame resizing which makes the selected window too small. */
12828 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12829 {
12830 sw = w;
12831 reconsider_clip_changes (w, current_buffer);
12832 }
12833
12834 /* Clear frames marked as garbaged. */
12835 if (frame_garbaged)
12836 clear_garbaged_frames ();
12837
12838 /* Build menubar and tool-bar items. */
12839 if (NILP (Vmemory_full))
12840 prepare_menu_bars ();
12841
12842 if (windows_or_buffers_changed)
12843 update_mode_lines++;
12844
12845 /* Detect case that we need to write or remove a star in the mode line. */
12846 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12847 {
12848 w->update_mode_line = Qt;
12849 if (buffer_shared > 1)
12850 update_mode_lines++;
12851 }
12852
12853 /* Avoid invocation of point motion hooks by `current_column' below. */
12854 count1 = SPECPDL_INDEX ();
12855 specbind (Qinhibit_point_motion_hooks, Qt);
12856
12857 /* If %c is in the mode line, update it if needed. */
12858 if (!NILP (w->column_number_displayed)
12859 /* This alternative quickly identifies a common case
12860 where no change is needed. */
12861 && !(PT == XFASTINT (w->last_point)
12862 && XFASTINT (w->last_modified) >= MODIFF
12863 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12864 && (XFASTINT (w->column_number_displayed) != current_column ()))
12865 w->update_mode_line = Qt;
12866
12867 unbind_to (count1, Qnil);
12868
12869 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12870
12871 /* The variable buffer_shared is set in redisplay_window and
12872 indicates that we redisplay a buffer in different windows. See
12873 there. */
12874 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12875 || cursor_type_changed);
12876
12877 /* If specs for an arrow have changed, do thorough redisplay
12878 to ensure we remove any arrow that should no longer exist. */
12879 if (overlay_arrows_changed_p ())
12880 consider_all_windows_p = windows_or_buffers_changed = 1;
12881
12882 /* Normally the message* functions will have already displayed and
12883 updated the echo area, but the frame may have been trashed, or
12884 the update may have been preempted, so display the echo area
12885 again here. Checking message_cleared_p captures the case that
12886 the echo area should be cleared. */
12887 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12888 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12889 || (message_cleared_p
12890 && minibuf_level == 0
12891 /* If the mini-window is currently selected, this means the
12892 echo-area doesn't show through. */
12893 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12894 {
12895 int window_height_changed_p = echo_area_display (0);
12896 must_finish = 1;
12897
12898 /* If we don't display the current message, don't clear the
12899 message_cleared_p flag, because, if we did, we wouldn't clear
12900 the echo area in the next redisplay which doesn't preserve
12901 the echo area. */
12902 if (!display_last_displayed_message_p)
12903 message_cleared_p = 0;
12904
12905 if (fonts_changed_p)
12906 goto retry;
12907 else if (window_height_changed_p)
12908 {
12909 consider_all_windows_p = 1;
12910 ++update_mode_lines;
12911 ++windows_or_buffers_changed;
12912
12913 /* If window configuration was changed, frames may have been
12914 marked garbaged. Clear them or we will experience
12915 surprises wrt scrolling. */
12916 if (frame_garbaged)
12917 clear_garbaged_frames ();
12918 }
12919 }
12920 else if (EQ (selected_window, minibuf_window)
12921 && (current_buffer->clip_changed
12922 || XFASTINT (w->last_modified) < MODIFF
12923 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12924 && resize_mini_window (w, 0))
12925 {
12926 /* Resized active mini-window to fit the size of what it is
12927 showing if its contents might have changed. */
12928 must_finish = 1;
12929 /* FIXME: this causes all frames to be updated, which seems unnecessary
12930 since only the current frame needs to be considered. This function needs
12931 to be rewritten with two variables, consider_all_windows and
12932 consider_all_frames. */
12933 consider_all_windows_p = 1;
12934 ++windows_or_buffers_changed;
12935 ++update_mode_lines;
12936
12937 /* If window configuration was changed, frames may have been
12938 marked garbaged. Clear them or we will experience
12939 surprises wrt scrolling. */
12940 if (frame_garbaged)
12941 clear_garbaged_frames ();
12942 }
12943
12944
12945 /* If showing the region, and mark has changed, we must redisplay
12946 the whole window. The assignment to this_line_start_pos prevents
12947 the optimization directly below this if-statement. */
12948 if (((!NILP (Vtransient_mark_mode)
12949 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12950 != !NILP (w->region_showing))
12951 || (!NILP (w->region_showing)
12952 && !EQ (w->region_showing,
12953 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12954 CHARPOS (this_line_start_pos) = 0;
12955
12956 /* Optimize the case that only the line containing the cursor in the
12957 selected window has changed. Variables starting with this_ are
12958 set in display_line and record information about the line
12959 containing the cursor. */
12960 tlbufpos = this_line_start_pos;
12961 tlendpos = this_line_end_pos;
12962 if (!consider_all_windows_p
12963 && CHARPOS (tlbufpos) > 0
12964 && NILP (w->update_mode_line)
12965 && !current_buffer->clip_changed
12966 && !current_buffer->prevent_redisplay_optimizations_p
12967 && FRAME_VISIBLE_P (XFRAME (w->frame))
12968 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12969 /* Make sure recorded data applies to current buffer, etc. */
12970 && this_line_buffer == current_buffer
12971 && current_buffer == XBUFFER (w->buffer)
12972 && NILP (w->force_start)
12973 && NILP (w->optional_new_start)
12974 /* Point must be on the line that we have info recorded about. */
12975 && PT >= CHARPOS (tlbufpos)
12976 && PT <= Z - CHARPOS (tlendpos)
12977 /* All text outside that line, including its final newline,
12978 must be unchanged. */
12979 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12980 CHARPOS (tlendpos)))
12981 {
12982 if (CHARPOS (tlbufpos) > BEGV
12983 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12984 && (CHARPOS (tlbufpos) == ZV
12985 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12986 /* Former continuation line has disappeared by becoming empty. */
12987 goto cancel;
12988 else if (XFASTINT (w->last_modified) < MODIFF
12989 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12990 || MINI_WINDOW_P (w))
12991 {
12992 /* We have to handle the case of continuation around a
12993 wide-column character (see the comment in indent.c around
12994 line 1340).
12995
12996 For instance, in the following case:
12997
12998 -------- Insert --------
12999 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13000 J_I_ ==> J_I_ `^^' are cursors.
13001 ^^ ^^
13002 -------- --------
13003
13004 As we have to redraw the line above, we cannot use this
13005 optimization. */
13006
13007 struct it it;
13008 int line_height_before = this_line_pixel_height;
13009
13010 /* Note that start_display will handle the case that the
13011 line starting at tlbufpos is a continuation line. */
13012 start_display (&it, w, tlbufpos);
13013
13014 /* Implementation note: It this still necessary? */
13015 if (it.current_x != this_line_start_x)
13016 goto cancel;
13017
13018 TRACE ((stderr, "trying display optimization 1\n"));
13019 w->cursor.vpos = -1;
13020 overlay_arrow_seen = 0;
13021 it.vpos = this_line_vpos;
13022 it.current_y = this_line_y;
13023 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13024 display_line (&it);
13025
13026 /* If line contains point, is not continued,
13027 and ends at same distance from eob as before, we win. */
13028 if (w->cursor.vpos >= 0
13029 /* Line is not continued, otherwise this_line_start_pos
13030 would have been set to 0 in display_line. */
13031 && CHARPOS (this_line_start_pos)
13032 /* Line ends as before. */
13033 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13034 /* Line has same height as before. Otherwise other lines
13035 would have to be shifted up or down. */
13036 && this_line_pixel_height == line_height_before)
13037 {
13038 /* If this is not the window's last line, we must adjust
13039 the charstarts of the lines below. */
13040 if (it.current_y < it.last_visible_y)
13041 {
13042 struct glyph_row *row
13043 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13044 ptrdiff_t delta, delta_bytes;
13045
13046 /* We used to distinguish between two cases here,
13047 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13048 when the line ends in a newline or the end of the
13049 buffer's accessible portion. But both cases did
13050 the same, so they were collapsed. */
13051 delta = (Z
13052 - CHARPOS (tlendpos)
13053 - MATRIX_ROW_START_CHARPOS (row));
13054 delta_bytes = (Z_BYTE
13055 - BYTEPOS (tlendpos)
13056 - MATRIX_ROW_START_BYTEPOS (row));
13057
13058 increment_matrix_positions (w->current_matrix,
13059 this_line_vpos + 1,
13060 w->current_matrix->nrows,
13061 delta, delta_bytes);
13062 }
13063
13064 /* If this row displays text now but previously didn't,
13065 or vice versa, w->window_end_vpos may have to be
13066 adjusted. */
13067 if ((it.glyph_row - 1)->displays_text_p)
13068 {
13069 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13070 XSETINT (w->window_end_vpos, this_line_vpos);
13071 }
13072 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13073 && this_line_vpos > 0)
13074 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13075 w->window_end_valid = Qnil;
13076
13077 /* Update hint: No need to try to scroll in update_window. */
13078 w->desired_matrix->no_scrolling_p = 1;
13079
13080 #if GLYPH_DEBUG
13081 *w->desired_matrix->method = 0;
13082 debug_method_add (w, "optimization 1");
13083 #endif
13084 #ifdef HAVE_WINDOW_SYSTEM
13085 update_window_fringes (w, 0);
13086 #endif
13087 goto update;
13088 }
13089 else
13090 goto cancel;
13091 }
13092 else if (/* Cursor position hasn't changed. */
13093 PT == XFASTINT (w->last_point)
13094 /* Make sure the cursor was last displayed
13095 in this window. Otherwise we have to reposition it. */
13096 && 0 <= w->cursor.vpos
13097 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13098 {
13099 if (!must_finish)
13100 {
13101 do_pending_window_change (1);
13102 /* If selected_window changed, redisplay again. */
13103 if (WINDOWP (selected_window)
13104 && (w = XWINDOW (selected_window)) != sw)
13105 goto retry;
13106
13107 /* We used to always goto end_of_redisplay here, but this
13108 isn't enough if we have a blinking cursor. */
13109 if (w->cursor_off_p == w->last_cursor_off_p)
13110 goto end_of_redisplay;
13111 }
13112 goto update;
13113 }
13114 /* If highlighting the region, or if the cursor is in the echo area,
13115 then we can't just move the cursor. */
13116 else if (! (!NILP (Vtransient_mark_mode)
13117 && !NILP (BVAR (current_buffer, mark_active)))
13118 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13119 || highlight_nonselected_windows)
13120 && NILP (w->region_showing)
13121 && NILP (Vshow_trailing_whitespace)
13122 && !cursor_in_echo_area)
13123 {
13124 struct it it;
13125 struct glyph_row *row;
13126
13127 /* Skip from tlbufpos to PT and see where it is. Note that
13128 PT may be in invisible text. If so, we will end at the
13129 next visible position. */
13130 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13131 NULL, DEFAULT_FACE_ID);
13132 it.current_x = this_line_start_x;
13133 it.current_y = this_line_y;
13134 it.vpos = this_line_vpos;
13135
13136 /* The call to move_it_to stops in front of PT, but
13137 moves over before-strings. */
13138 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13139
13140 if (it.vpos == this_line_vpos
13141 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13142 row->enabled_p))
13143 {
13144 xassert (this_line_vpos == it.vpos);
13145 xassert (this_line_y == it.current_y);
13146 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13147 #if GLYPH_DEBUG
13148 *w->desired_matrix->method = 0;
13149 debug_method_add (w, "optimization 3");
13150 #endif
13151 goto update;
13152 }
13153 else
13154 goto cancel;
13155 }
13156
13157 cancel:
13158 /* Text changed drastically or point moved off of line. */
13159 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13160 }
13161
13162 CHARPOS (this_line_start_pos) = 0;
13163 consider_all_windows_p |= buffer_shared > 1;
13164 ++clear_face_cache_count;
13165 #ifdef HAVE_WINDOW_SYSTEM
13166 ++clear_image_cache_count;
13167 #endif
13168
13169 /* Build desired matrices, and update the display. If
13170 consider_all_windows_p is non-zero, do it for all windows on all
13171 frames. Otherwise do it for selected_window, only. */
13172
13173 if (consider_all_windows_p)
13174 {
13175 Lisp_Object tail, frame;
13176
13177 FOR_EACH_FRAME (tail, frame)
13178 XFRAME (frame)->updated_p = 0;
13179
13180 /* Recompute # windows showing selected buffer. This will be
13181 incremented each time such a window is displayed. */
13182 buffer_shared = 0;
13183
13184 FOR_EACH_FRAME (tail, frame)
13185 {
13186 struct frame *f = XFRAME (frame);
13187
13188 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13189 {
13190 if (! EQ (frame, selected_frame))
13191 /* Select the frame, for the sake of frame-local
13192 variables. */
13193 select_frame_for_redisplay (frame);
13194
13195 /* Mark all the scroll bars to be removed; we'll redeem
13196 the ones we want when we redisplay their windows. */
13197 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13198 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13199
13200 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13201 redisplay_windows (FRAME_ROOT_WINDOW (f));
13202
13203 /* The X error handler may have deleted that frame. */
13204 if (!FRAME_LIVE_P (f))
13205 continue;
13206
13207 /* Any scroll bars which redisplay_windows should have
13208 nuked should now go away. */
13209 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13210 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13211
13212 /* If fonts changed, display again. */
13213 /* ??? rms: I suspect it is a mistake to jump all the way
13214 back to retry here. It should just retry this frame. */
13215 if (fonts_changed_p)
13216 goto retry;
13217
13218 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13219 {
13220 /* See if we have to hscroll. */
13221 if (!f->already_hscrolled_p)
13222 {
13223 f->already_hscrolled_p = 1;
13224 if (hscroll_windows (f->root_window))
13225 goto retry;
13226 }
13227
13228 /* Prevent various kinds of signals during display
13229 update. stdio is not robust about handling
13230 signals, which can cause an apparent I/O
13231 error. */
13232 if (interrupt_input)
13233 unrequest_sigio ();
13234 STOP_POLLING;
13235
13236 /* Update the display. */
13237 set_window_update_flags (XWINDOW (f->root_window), 1);
13238 pending |= update_frame (f, 0, 0);
13239 f->updated_p = 1;
13240 }
13241 }
13242 }
13243
13244 if (!EQ (old_frame, selected_frame)
13245 && FRAME_LIVE_P (XFRAME (old_frame)))
13246 /* We played a bit fast-and-loose above and allowed selected_frame
13247 and selected_window to be temporarily out-of-sync but let's make
13248 sure this stays contained. */
13249 select_frame_for_redisplay (old_frame);
13250 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13251
13252 if (!pending)
13253 {
13254 /* Do the mark_window_display_accurate after all windows have
13255 been redisplayed because this call resets flags in buffers
13256 which are needed for proper redisplay. */
13257 FOR_EACH_FRAME (tail, frame)
13258 {
13259 struct frame *f = XFRAME (frame);
13260 if (f->updated_p)
13261 {
13262 mark_window_display_accurate (f->root_window, 1);
13263 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13264 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13265 }
13266 }
13267 }
13268 }
13269 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13270 {
13271 Lisp_Object mini_window;
13272 struct frame *mini_frame;
13273
13274 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13275 /* Use list_of_error, not Qerror, so that
13276 we catch only errors and don't run the debugger. */
13277 internal_condition_case_1 (redisplay_window_1, selected_window,
13278 list_of_error,
13279 redisplay_window_error);
13280
13281 /* Compare desired and current matrices, perform output. */
13282
13283 update:
13284 /* If fonts changed, display again. */
13285 if (fonts_changed_p)
13286 goto retry;
13287
13288 /* Prevent various kinds of signals during display update.
13289 stdio is not robust about handling signals,
13290 which can cause an apparent I/O error. */
13291 if (interrupt_input)
13292 unrequest_sigio ();
13293 STOP_POLLING;
13294
13295 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13296 {
13297 if (hscroll_windows (selected_window))
13298 goto retry;
13299
13300 XWINDOW (selected_window)->must_be_updated_p = 1;
13301 pending = update_frame (sf, 0, 0);
13302 }
13303
13304 /* We may have called echo_area_display at the top of this
13305 function. If the echo area is on another frame, that may
13306 have put text on a frame other than the selected one, so the
13307 above call to update_frame would not have caught it. Catch
13308 it here. */
13309 mini_window = FRAME_MINIBUF_WINDOW (sf);
13310 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13311
13312 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13313 {
13314 XWINDOW (mini_window)->must_be_updated_p = 1;
13315 pending |= update_frame (mini_frame, 0, 0);
13316 if (!pending && hscroll_windows (mini_window))
13317 goto retry;
13318 }
13319 }
13320
13321 /* If display was paused because of pending input, make sure we do a
13322 thorough update the next time. */
13323 if (pending)
13324 {
13325 /* Prevent the optimization at the beginning of
13326 redisplay_internal that tries a single-line update of the
13327 line containing the cursor in the selected window. */
13328 CHARPOS (this_line_start_pos) = 0;
13329
13330 /* Let the overlay arrow be updated the next time. */
13331 update_overlay_arrows (0);
13332
13333 /* If we pause after scrolling, some rows in the current
13334 matrices of some windows are not valid. */
13335 if (!WINDOW_FULL_WIDTH_P (w)
13336 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13337 update_mode_lines = 1;
13338 }
13339 else
13340 {
13341 if (!consider_all_windows_p)
13342 {
13343 /* This has already been done above if
13344 consider_all_windows_p is set. */
13345 mark_window_display_accurate_1 (w, 1);
13346
13347 /* Say overlay arrows are up to date. */
13348 update_overlay_arrows (1);
13349
13350 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13351 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13352 }
13353
13354 update_mode_lines = 0;
13355 windows_or_buffers_changed = 0;
13356 cursor_type_changed = 0;
13357 }
13358
13359 /* Start SIGIO interrupts coming again. Having them off during the
13360 code above makes it less likely one will discard output, but not
13361 impossible, since there might be stuff in the system buffer here.
13362 But it is much hairier to try to do anything about that. */
13363 if (interrupt_input)
13364 request_sigio ();
13365 RESUME_POLLING;
13366
13367 /* If a frame has become visible which was not before, redisplay
13368 again, so that we display it. Expose events for such a frame
13369 (which it gets when becoming visible) don't call the parts of
13370 redisplay constructing glyphs, so simply exposing a frame won't
13371 display anything in this case. So, we have to display these
13372 frames here explicitly. */
13373 if (!pending)
13374 {
13375 Lisp_Object tail, frame;
13376 int new_count = 0;
13377
13378 FOR_EACH_FRAME (tail, frame)
13379 {
13380 int this_is_visible = 0;
13381
13382 if (XFRAME (frame)->visible)
13383 this_is_visible = 1;
13384 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13385 if (XFRAME (frame)->visible)
13386 this_is_visible = 1;
13387
13388 if (this_is_visible)
13389 new_count++;
13390 }
13391
13392 if (new_count != number_of_visible_frames)
13393 windows_or_buffers_changed++;
13394 }
13395
13396 /* Change frame size now if a change is pending. */
13397 do_pending_window_change (1);
13398
13399 /* If we just did a pending size change, or have additional
13400 visible frames, or selected_window changed, redisplay again. */
13401 if ((windows_or_buffers_changed && !pending)
13402 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13403 goto retry;
13404
13405 /* Clear the face and image caches.
13406
13407 We used to do this only if consider_all_windows_p. But the cache
13408 needs to be cleared if a timer creates images in the current
13409 buffer (e.g. the test case in Bug#6230). */
13410
13411 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13412 {
13413 clear_face_cache (0);
13414 clear_face_cache_count = 0;
13415 }
13416
13417 #ifdef HAVE_WINDOW_SYSTEM
13418 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13419 {
13420 clear_image_caches (Qnil);
13421 clear_image_cache_count = 0;
13422 }
13423 #endif /* HAVE_WINDOW_SYSTEM */
13424
13425 end_of_redisplay:
13426 unbind_to (count, Qnil);
13427 RESUME_POLLING;
13428 }
13429
13430
13431 /* Redisplay, but leave alone any recent echo area message unless
13432 another message has been requested in its place.
13433
13434 This is useful in situations where you need to redisplay but no
13435 user action has occurred, making it inappropriate for the message
13436 area to be cleared. See tracking_off and
13437 wait_reading_process_output for examples of these situations.
13438
13439 FROM_WHERE is an integer saying from where this function was
13440 called. This is useful for debugging. */
13441
13442 void
13443 redisplay_preserve_echo_area (int from_where)
13444 {
13445 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13446
13447 if (!NILP (echo_area_buffer[1]))
13448 {
13449 /* We have a previously displayed message, but no current
13450 message. Redisplay the previous message. */
13451 display_last_displayed_message_p = 1;
13452 redisplay_internal ();
13453 display_last_displayed_message_p = 0;
13454 }
13455 else
13456 redisplay_internal ();
13457
13458 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13459 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13460 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13461 }
13462
13463
13464 /* Function registered with record_unwind_protect in
13465 redisplay_internal. Reset redisplaying_p to the value it had
13466 before redisplay_internal was called, and clear
13467 prevent_freeing_realized_faces_p. It also selects the previously
13468 selected frame, unless it has been deleted (by an X connection
13469 failure during redisplay, for example). */
13470
13471 static Lisp_Object
13472 unwind_redisplay (Lisp_Object val)
13473 {
13474 Lisp_Object old_redisplaying_p, old_frame;
13475
13476 old_redisplaying_p = XCAR (val);
13477 redisplaying_p = XFASTINT (old_redisplaying_p);
13478 old_frame = XCDR (val);
13479 if (! EQ (old_frame, selected_frame)
13480 && FRAME_LIVE_P (XFRAME (old_frame)))
13481 select_frame_for_redisplay (old_frame);
13482 return Qnil;
13483 }
13484
13485
13486 /* Mark the display of window W as accurate or inaccurate. If
13487 ACCURATE_P is non-zero mark display of W as accurate. If
13488 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13489 redisplay_internal is called. */
13490
13491 static void
13492 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13493 {
13494 if (BUFFERP (w->buffer))
13495 {
13496 struct buffer *b = XBUFFER (w->buffer);
13497
13498 w->last_modified
13499 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13500 w->last_overlay_modified
13501 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13502 w->last_had_star
13503 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13504
13505 if (accurate_p)
13506 {
13507 b->clip_changed = 0;
13508 b->prevent_redisplay_optimizations_p = 0;
13509
13510 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13511 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13512 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13513 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13514
13515 w->current_matrix->buffer = b;
13516 w->current_matrix->begv = BUF_BEGV (b);
13517 w->current_matrix->zv = BUF_ZV (b);
13518
13519 w->last_cursor = w->cursor;
13520 w->last_cursor_off_p = w->cursor_off_p;
13521
13522 if (w == XWINDOW (selected_window))
13523 w->last_point = make_number (BUF_PT (b));
13524 else
13525 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13526 }
13527 }
13528
13529 if (accurate_p)
13530 {
13531 w->window_end_valid = w->buffer;
13532 w->update_mode_line = Qnil;
13533 }
13534 }
13535
13536
13537 /* Mark the display of windows in the window tree rooted at WINDOW as
13538 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13539 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13540 be redisplayed the next time redisplay_internal is called. */
13541
13542 void
13543 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13544 {
13545 struct window *w;
13546
13547 for (; !NILP (window); window = w->next)
13548 {
13549 w = XWINDOW (window);
13550 mark_window_display_accurate_1 (w, accurate_p);
13551
13552 if (!NILP (w->vchild))
13553 mark_window_display_accurate (w->vchild, accurate_p);
13554 if (!NILP (w->hchild))
13555 mark_window_display_accurate (w->hchild, accurate_p);
13556 }
13557
13558 if (accurate_p)
13559 {
13560 update_overlay_arrows (1);
13561 }
13562 else
13563 {
13564 /* Force a thorough redisplay the next time by setting
13565 last_arrow_position and last_arrow_string to t, which is
13566 unequal to any useful value of Voverlay_arrow_... */
13567 update_overlay_arrows (-1);
13568 }
13569 }
13570
13571
13572 /* Return value in display table DP (Lisp_Char_Table *) for character
13573 C. Since a display table doesn't have any parent, we don't have to
13574 follow parent. Do not call this function directly but use the
13575 macro DISP_CHAR_VECTOR. */
13576
13577 Lisp_Object
13578 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13579 {
13580 Lisp_Object val;
13581
13582 if (ASCII_CHAR_P (c))
13583 {
13584 val = dp->ascii;
13585 if (SUB_CHAR_TABLE_P (val))
13586 val = XSUB_CHAR_TABLE (val)->contents[c];
13587 }
13588 else
13589 {
13590 Lisp_Object table;
13591
13592 XSETCHAR_TABLE (table, dp);
13593 val = char_table_ref (table, c);
13594 }
13595 if (NILP (val))
13596 val = dp->defalt;
13597 return val;
13598 }
13599
13600
13601 \f
13602 /***********************************************************************
13603 Window Redisplay
13604 ***********************************************************************/
13605
13606 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13607
13608 static void
13609 redisplay_windows (Lisp_Object window)
13610 {
13611 while (!NILP (window))
13612 {
13613 struct window *w = XWINDOW (window);
13614
13615 if (!NILP (w->hchild))
13616 redisplay_windows (w->hchild);
13617 else if (!NILP (w->vchild))
13618 redisplay_windows (w->vchild);
13619 else if (!NILP (w->buffer))
13620 {
13621 displayed_buffer = XBUFFER (w->buffer);
13622 /* Use list_of_error, not Qerror, so that
13623 we catch only errors and don't run the debugger. */
13624 internal_condition_case_1 (redisplay_window_0, window,
13625 list_of_error,
13626 redisplay_window_error);
13627 }
13628
13629 window = w->next;
13630 }
13631 }
13632
13633 static Lisp_Object
13634 redisplay_window_error (Lisp_Object ignore)
13635 {
13636 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13637 return Qnil;
13638 }
13639
13640 static Lisp_Object
13641 redisplay_window_0 (Lisp_Object window)
13642 {
13643 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13644 redisplay_window (window, 0);
13645 return Qnil;
13646 }
13647
13648 static Lisp_Object
13649 redisplay_window_1 (Lisp_Object window)
13650 {
13651 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13652 redisplay_window (window, 1);
13653 return Qnil;
13654 }
13655 \f
13656
13657 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13658 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13659 which positions recorded in ROW differ from current buffer
13660 positions.
13661
13662 Return 0 if cursor is not on this row, 1 otherwise. */
13663
13664 static int
13665 set_cursor_from_row (struct window *w, struct glyph_row *row,
13666 struct glyph_matrix *matrix,
13667 ptrdiff_t delta, ptrdiff_t delta_bytes,
13668 int dy, int dvpos)
13669 {
13670 struct glyph *glyph = row->glyphs[TEXT_AREA];
13671 struct glyph *end = glyph + row->used[TEXT_AREA];
13672 struct glyph *cursor = NULL;
13673 /* The last known character position in row. */
13674 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13675 int x = row->x;
13676 ptrdiff_t pt_old = PT - delta;
13677 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13678 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13679 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13680 /* A glyph beyond the edge of TEXT_AREA which we should never
13681 touch. */
13682 struct glyph *glyphs_end = end;
13683 /* Non-zero means we've found a match for cursor position, but that
13684 glyph has the avoid_cursor_p flag set. */
13685 int match_with_avoid_cursor = 0;
13686 /* Non-zero means we've seen at least one glyph that came from a
13687 display string. */
13688 int string_seen = 0;
13689 /* Largest and smallest buffer positions seen so far during scan of
13690 glyph row. */
13691 ptrdiff_t bpos_max = pos_before;
13692 ptrdiff_t bpos_min = pos_after;
13693 /* Last buffer position covered by an overlay string with an integer
13694 `cursor' property. */
13695 ptrdiff_t bpos_covered = 0;
13696 /* Non-zero means the display string on which to display the cursor
13697 comes from a text property, not from an overlay. */
13698 int string_from_text_prop = 0;
13699
13700 /* Don't even try doing anything if called for a mode-line or
13701 header-line row, since the rest of the code isn't prepared to
13702 deal with such calamities. */
13703 xassert (!row->mode_line_p);
13704 if (row->mode_line_p)
13705 return 0;
13706
13707 /* Skip over glyphs not having an object at the start and the end of
13708 the row. These are special glyphs like truncation marks on
13709 terminal frames. */
13710 if (row->displays_text_p)
13711 {
13712 if (!row->reversed_p)
13713 {
13714 while (glyph < end
13715 && INTEGERP (glyph->object)
13716 && glyph->charpos < 0)
13717 {
13718 x += glyph->pixel_width;
13719 ++glyph;
13720 }
13721 while (end > glyph
13722 && INTEGERP ((end - 1)->object)
13723 /* CHARPOS is zero for blanks and stretch glyphs
13724 inserted by extend_face_to_end_of_line. */
13725 && (end - 1)->charpos <= 0)
13726 --end;
13727 glyph_before = glyph - 1;
13728 glyph_after = end;
13729 }
13730 else
13731 {
13732 struct glyph *g;
13733
13734 /* If the glyph row is reversed, we need to process it from back
13735 to front, so swap the edge pointers. */
13736 glyphs_end = end = glyph - 1;
13737 glyph += row->used[TEXT_AREA] - 1;
13738
13739 while (glyph > end + 1
13740 && INTEGERP (glyph->object)
13741 && glyph->charpos < 0)
13742 {
13743 --glyph;
13744 x -= glyph->pixel_width;
13745 }
13746 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13747 --glyph;
13748 /* By default, in reversed rows we put the cursor on the
13749 rightmost (first in the reading order) glyph. */
13750 for (g = end + 1; g < glyph; g++)
13751 x += g->pixel_width;
13752 while (end < glyph
13753 && INTEGERP ((end + 1)->object)
13754 && (end + 1)->charpos <= 0)
13755 ++end;
13756 glyph_before = glyph + 1;
13757 glyph_after = end;
13758 }
13759 }
13760 else if (row->reversed_p)
13761 {
13762 /* In R2L rows that don't display text, put the cursor on the
13763 rightmost glyph. Case in point: an empty last line that is
13764 part of an R2L paragraph. */
13765 cursor = end - 1;
13766 /* Avoid placing the cursor on the last glyph of the row, where
13767 on terminal frames we hold the vertical border between
13768 adjacent windows. */
13769 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13770 && !WINDOW_RIGHTMOST_P (w)
13771 && cursor == row->glyphs[LAST_AREA] - 1)
13772 cursor--;
13773 x = -1; /* will be computed below, at label compute_x */
13774 }
13775
13776 /* Step 1: Try to find the glyph whose character position
13777 corresponds to point. If that's not possible, find 2 glyphs
13778 whose character positions are the closest to point, one before
13779 point, the other after it. */
13780 if (!row->reversed_p)
13781 while (/* not marched to end of glyph row */
13782 glyph < end
13783 /* glyph was not inserted by redisplay for internal purposes */
13784 && !INTEGERP (glyph->object))
13785 {
13786 if (BUFFERP (glyph->object))
13787 {
13788 ptrdiff_t dpos = glyph->charpos - pt_old;
13789
13790 if (glyph->charpos > bpos_max)
13791 bpos_max = glyph->charpos;
13792 if (glyph->charpos < bpos_min)
13793 bpos_min = glyph->charpos;
13794 if (!glyph->avoid_cursor_p)
13795 {
13796 /* If we hit point, we've found the glyph on which to
13797 display the cursor. */
13798 if (dpos == 0)
13799 {
13800 match_with_avoid_cursor = 0;
13801 break;
13802 }
13803 /* See if we've found a better approximation to
13804 POS_BEFORE or to POS_AFTER. Note that we want the
13805 first (leftmost) glyph of all those that are the
13806 closest from below, and the last (rightmost) of all
13807 those from above. */
13808 if (0 > dpos && dpos > pos_before - pt_old)
13809 {
13810 pos_before = glyph->charpos;
13811 glyph_before = glyph;
13812 }
13813 else if (0 < dpos && dpos <= pos_after - pt_old)
13814 {
13815 pos_after = glyph->charpos;
13816 glyph_after = glyph;
13817 }
13818 }
13819 else if (dpos == 0)
13820 match_with_avoid_cursor = 1;
13821 }
13822 else if (STRINGP (glyph->object))
13823 {
13824 Lisp_Object chprop;
13825 ptrdiff_t glyph_pos = glyph->charpos;
13826
13827 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13828 glyph->object);
13829 if (!NILP (chprop))
13830 {
13831 /* If the string came from a `display' text property,
13832 look up the buffer position of that property and
13833 use that position to update bpos_max, as if we
13834 actually saw such a position in one of the row's
13835 glyphs. This helps with supporting integer values
13836 of `cursor' property on the display string in
13837 situations where most or all of the row's buffer
13838 text is completely covered by display properties,
13839 so that no glyph with valid buffer positions is
13840 ever seen in the row. */
13841 ptrdiff_t prop_pos =
13842 string_buffer_position_lim (glyph->object, pos_before,
13843 pos_after, 0);
13844
13845 if (prop_pos >= pos_before)
13846 bpos_max = prop_pos - 1;
13847 }
13848 if (INTEGERP (chprop))
13849 {
13850 bpos_covered = bpos_max + XINT (chprop);
13851 /* If the `cursor' property covers buffer positions up
13852 to and including point, we should display cursor on
13853 this glyph. Note that, if a `cursor' property on one
13854 of the string's characters has an integer value, we
13855 will break out of the loop below _before_ we get to
13856 the position match above. IOW, integer values of
13857 the `cursor' property override the "exact match for
13858 point" strategy of positioning the cursor. */
13859 /* Implementation note: bpos_max == pt_old when, e.g.,
13860 we are in an empty line, where bpos_max is set to
13861 MATRIX_ROW_START_CHARPOS, see above. */
13862 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13863 {
13864 cursor = glyph;
13865 break;
13866 }
13867 }
13868
13869 string_seen = 1;
13870 }
13871 x += glyph->pixel_width;
13872 ++glyph;
13873 }
13874 else if (glyph > end) /* row is reversed */
13875 while (!INTEGERP (glyph->object))
13876 {
13877 if (BUFFERP (glyph->object))
13878 {
13879 ptrdiff_t dpos = glyph->charpos - pt_old;
13880
13881 if (glyph->charpos > bpos_max)
13882 bpos_max = glyph->charpos;
13883 if (glyph->charpos < bpos_min)
13884 bpos_min = glyph->charpos;
13885 if (!glyph->avoid_cursor_p)
13886 {
13887 if (dpos == 0)
13888 {
13889 match_with_avoid_cursor = 0;
13890 break;
13891 }
13892 if (0 > dpos && dpos > pos_before - pt_old)
13893 {
13894 pos_before = glyph->charpos;
13895 glyph_before = glyph;
13896 }
13897 else if (0 < dpos && dpos <= pos_after - pt_old)
13898 {
13899 pos_after = glyph->charpos;
13900 glyph_after = glyph;
13901 }
13902 }
13903 else if (dpos == 0)
13904 match_with_avoid_cursor = 1;
13905 }
13906 else if (STRINGP (glyph->object))
13907 {
13908 Lisp_Object chprop;
13909 ptrdiff_t glyph_pos = glyph->charpos;
13910
13911 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13912 glyph->object);
13913 if (!NILP (chprop))
13914 {
13915 ptrdiff_t prop_pos =
13916 string_buffer_position_lim (glyph->object, pos_before,
13917 pos_after, 0);
13918
13919 if (prop_pos >= pos_before)
13920 bpos_max = prop_pos - 1;
13921 }
13922 if (INTEGERP (chprop))
13923 {
13924 bpos_covered = bpos_max + XINT (chprop);
13925 /* If the `cursor' property covers buffer positions up
13926 to and including point, we should display cursor on
13927 this glyph. */
13928 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13929 {
13930 cursor = glyph;
13931 break;
13932 }
13933 }
13934 string_seen = 1;
13935 }
13936 --glyph;
13937 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13938 {
13939 x--; /* can't use any pixel_width */
13940 break;
13941 }
13942 x -= glyph->pixel_width;
13943 }
13944
13945 /* Step 2: If we didn't find an exact match for point, we need to
13946 look for a proper place to put the cursor among glyphs between
13947 GLYPH_BEFORE and GLYPH_AFTER. */
13948 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13949 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13950 && bpos_covered < pt_old)
13951 {
13952 /* An empty line has a single glyph whose OBJECT is zero and
13953 whose CHARPOS is the position of a newline on that line.
13954 Note that on a TTY, there are more glyphs after that, which
13955 were produced by extend_face_to_end_of_line, but their
13956 CHARPOS is zero or negative. */
13957 int empty_line_p =
13958 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13959 && INTEGERP (glyph->object) && glyph->charpos > 0;
13960
13961 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13962 {
13963 ptrdiff_t ellipsis_pos;
13964
13965 /* Scan back over the ellipsis glyphs. */
13966 if (!row->reversed_p)
13967 {
13968 ellipsis_pos = (glyph - 1)->charpos;
13969 while (glyph > row->glyphs[TEXT_AREA]
13970 && (glyph - 1)->charpos == ellipsis_pos)
13971 glyph--, x -= glyph->pixel_width;
13972 /* That loop always goes one position too far, including
13973 the glyph before the ellipsis. So scan forward over
13974 that one. */
13975 x += glyph->pixel_width;
13976 glyph++;
13977 }
13978 else /* row is reversed */
13979 {
13980 ellipsis_pos = (glyph + 1)->charpos;
13981 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13982 && (glyph + 1)->charpos == ellipsis_pos)
13983 glyph++, x += glyph->pixel_width;
13984 x -= glyph->pixel_width;
13985 glyph--;
13986 }
13987 }
13988 else if (match_with_avoid_cursor)
13989 {
13990 cursor = glyph_after;
13991 x = -1;
13992 }
13993 else if (string_seen)
13994 {
13995 int incr = row->reversed_p ? -1 : +1;
13996
13997 /* Need to find the glyph that came out of a string which is
13998 present at point. That glyph is somewhere between
13999 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14000 positioned between POS_BEFORE and POS_AFTER in the
14001 buffer. */
14002 struct glyph *start, *stop;
14003 ptrdiff_t pos = pos_before;
14004
14005 x = -1;
14006
14007 /* If the row ends in a newline from a display string,
14008 reordering could have moved the glyphs belonging to the
14009 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14010 in this case we extend the search to the last glyph in
14011 the row that was not inserted by redisplay. */
14012 if (row->ends_in_newline_from_string_p)
14013 {
14014 glyph_after = end;
14015 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14016 }
14017
14018 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14019 correspond to POS_BEFORE and POS_AFTER, respectively. We
14020 need START and STOP in the order that corresponds to the
14021 row's direction as given by its reversed_p flag. If the
14022 directionality of characters between POS_BEFORE and
14023 POS_AFTER is the opposite of the row's base direction,
14024 these characters will have been reordered for display,
14025 and we need to reverse START and STOP. */
14026 if (!row->reversed_p)
14027 {
14028 start = min (glyph_before, glyph_after);
14029 stop = max (glyph_before, glyph_after);
14030 }
14031 else
14032 {
14033 start = max (glyph_before, glyph_after);
14034 stop = min (glyph_before, glyph_after);
14035 }
14036 for (glyph = start + incr;
14037 row->reversed_p ? glyph > stop : glyph < stop; )
14038 {
14039
14040 /* Any glyphs that come from the buffer are here because
14041 of bidi reordering. Skip them, and only pay
14042 attention to glyphs that came from some string. */
14043 if (STRINGP (glyph->object))
14044 {
14045 Lisp_Object str;
14046 ptrdiff_t tem;
14047 /* If the display property covers the newline, we
14048 need to search for it one position farther. */
14049 ptrdiff_t lim = pos_after
14050 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14051
14052 string_from_text_prop = 0;
14053 str = glyph->object;
14054 tem = string_buffer_position_lim (str, pos, lim, 0);
14055 if (tem == 0 /* from overlay */
14056 || pos <= tem)
14057 {
14058 /* If the string from which this glyph came is
14059 found in the buffer at point, or at position
14060 that is closer to point than pos_after, then
14061 we've found the glyph we've been looking for.
14062 If it comes from an overlay (tem == 0), and
14063 it has the `cursor' property on one of its
14064 glyphs, record that glyph as a candidate for
14065 displaying the cursor. (As in the
14066 unidirectional version, we will display the
14067 cursor on the last candidate we find.) */
14068 if (tem == 0
14069 || tem == pt_old
14070 || (tem - pt_old > 0 && tem < pos_after))
14071 {
14072 /* The glyphs from this string could have
14073 been reordered. Find the one with the
14074 smallest string position. Or there could
14075 be a character in the string with the
14076 `cursor' property, which means display
14077 cursor on that character's glyph. */
14078 ptrdiff_t strpos = glyph->charpos;
14079
14080 if (tem)
14081 {
14082 cursor = glyph;
14083 string_from_text_prop = 1;
14084 }
14085 for ( ;
14086 (row->reversed_p ? glyph > stop : glyph < stop)
14087 && EQ (glyph->object, str);
14088 glyph += incr)
14089 {
14090 Lisp_Object cprop;
14091 ptrdiff_t gpos = glyph->charpos;
14092
14093 cprop = Fget_char_property (make_number (gpos),
14094 Qcursor,
14095 glyph->object);
14096 if (!NILP (cprop))
14097 {
14098 cursor = glyph;
14099 break;
14100 }
14101 if (tem && glyph->charpos < strpos)
14102 {
14103 strpos = glyph->charpos;
14104 cursor = glyph;
14105 }
14106 }
14107
14108 if (tem == pt_old
14109 || (tem - pt_old > 0 && tem < pos_after))
14110 goto compute_x;
14111 }
14112 if (tem)
14113 pos = tem + 1; /* don't find previous instances */
14114 }
14115 /* This string is not what we want; skip all of the
14116 glyphs that came from it. */
14117 while ((row->reversed_p ? glyph > stop : glyph < stop)
14118 && EQ (glyph->object, str))
14119 glyph += incr;
14120 }
14121 else
14122 glyph += incr;
14123 }
14124
14125 /* If we reached the end of the line, and END was from a string,
14126 the cursor is not on this line. */
14127 if (cursor == NULL
14128 && (row->reversed_p ? glyph <= end : glyph >= end)
14129 && STRINGP (end->object)
14130 && row->continued_p)
14131 return 0;
14132 }
14133 /* A truncated row may not include PT among its character positions.
14134 Setting the cursor inside the scroll margin will trigger
14135 recalculation of hscroll in hscroll_window_tree. But if a
14136 display string covers point, defer to the string-handling
14137 code below to figure this out. */
14138 else if (row->truncated_on_left_p && pt_old < bpos_min)
14139 {
14140 cursor = glyph_before;
14141 x = -1;
14142 }
14143 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14144 /* Zero-width characters produce no glyphs. */
14145 || (!empty_line_p
14146 && (row->reversed_p
14147 ? glyph_after > glyphs_end
14148 : glyph_after < glyphs_end)))
14149 {
14150 cursor = glyph_after;
14151 x = -1;
14152 }
14153 }
14154
14155 compute_x:
14156 if (cursor != NULL)
14157 glyph = cursor;
14158 if (x < 0)
14159 {
14160 struct glyph *g;
14161
14162 /* Need to compute x that corresponds to GLYPH. */
14163 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14164 {
14165 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14166 abort ();
14167 x += g->pixel_width;
14168 }
14169 }
14170
14171 /* ROW could be part of a continued line, which, under bidi
14172 reordering, might have other rows whose start and end charpos
14173 occlude point. Only set w->cursor if we found a better
14174 approximation to the cursor position than we have from previously
14175 examined candidate rows belonging to the same continued line. */
14176 if (/* we already have a candidate row */
14177 w->cursor.vpos >= 0
14178 /* that candidate is not the row we are processing */
14179 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14180 /* Make sure cursor.vpos specifies a row whose start and end
14181 charpos occlude point, and it is valid candidate for being a
14182 cursor-row. This is because some callers of this function
14183 leave cursor.vpos at the row where the cursor was displayed
14184 during the last redisplay cycle. */
14185 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14186 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14187 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14188 {
14189 struct glyph *g1 =
14190 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14191
14192 /* Don't consider glyphs that are outside TEXT_AREA. */
14193 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14194 return 0;
14195 /* Keep the candidate whose buffer position is the closest to
14196 point or has the `cursor' property. */
14197 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14198 w->cursor.hpos >= 0
14199 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14200 && ((BUFFERP (g1->object)
14201 && (g1->charpos == pt_old /* an exact match always wins */
14202 || (BUFFERP (glyph->object)
14203 && eabs (g1->charpos - pt_old)
14204 < eabs (glyph->charpos - pt_old))))
14205 /* previous candidate is a glyph from a string that has
14206 a non-nil `cursor' property */
14207 || (STRINGP (g1->object)
14208 && (!NILP (Fget_char_property (make_number (g1->charpos),
14209 Qcursor, g1->object))
14210 /* previous candidate is from the same display
14211 string as this one, and the display string
14212 came from a text property */
14213 || (EQ (g1->object, glyph->object)
14214 && string_from_text_prop)
14215 /* this candidate is from newline and its
14216 position is not an exact match */
14217 || (INTEGERP (glyph->object)
14218 && glyph->charpos != pt_old)))))
14219 return 0;
14220 /* If this candidate gives an exact match, use that. */
14221 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14222 /* If this candidate is a glyph created for the
14223 terminating newline of a line, and point is on that
14224 newline, it wins because it's an exact match. */
14225 || (!row->continued_p
14226 && INTEGERP (glyph->object)
14227 && glyph->charpos == 0
14228 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14229 /* Otherwise, keep the candidate that comes from a row
14230 spanning less buffer positions. This may win when one or
14231 both candidate positions are on glyphs that came from
14232 display strings, for which we cannot compare buffer
14233 positions. */
14234 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14235 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14236 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14237 return 0;
14238 }
14239 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14240 w->cursor.x = x;
14241 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14242 w->cursor.y = row->y + dy;
14243
14244 if (w == XWINDOW (selected_window))
14245 {
14246 if (!row->continued_p
14247 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14248 && row->x == 0)
14249 {
14250 this_line_buffer = XBUFFER (w->buffer);
14251
14252 CHARPOS (this_line_start_pos)
14253 = MATRIX_ROW_START_CHARPOS (row) + delta;
14254 BYTEPOS (this_line_start_pos)
14255 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14256
14257 CHARPOS (this_line_end_pos)
14258 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14259 BYTEPOS (this_line_end_pos)
14260 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14261
14262 this_line_y = w->cursor.y;
14263 this_line_pixel_height = row->height;
14264 this_line_vpos = w->cursor.vpos;
14265 this_line_start_x = row->x;
14266 }
14267 else
14268 CHARPOS (this_line_start_pos) = 0;
14269 }
14270
14271 return 1;
14272 }
14273
14274
14275 /* Run window scroll functions, if any, for WINDOW with new window
14276 start STARTP. Sets the window start of WINDOW to that position.
14277
14278 We assume that the window's buffer is really current. */
14279
14280 static inline struct text_pos
14281 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14282 {
14283 struct window *w = XWINDOW (window);
14284 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14285
14286 if (current_buffer != XBUFFER (w->buffer))
14287 abort ();
14288
14289 if (!NILP (Vwindow_scroll_functions))
14290 {
14291 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14292 make_number (CHARPOS (startp)));
14293 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14294 /* In case the hook functions switch buffers. */
14295 if (current_buffer != XBUFFER (w->buffer))
14296 set_buffer_internal_1 (XBUFFER (w->buffer));
14297 }
14298
14299 return startp;
14300 }
14301
14302
14303 /* Make sure the line containing the cursor is fully visible.
14304 A value of 1 means there is nothing to be done.
14305 (Either the line is fully visible, or it cannot be made so,
14306 or we cannot tell.)
14307
14308 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14309 is higher than window.
14310
14311 A value of 0 means the caller should do scrolling
14312 as if point had gone off the screen. */
14313
14314 static int
14315 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14316 {
14317 struct glyph_matrix *matrix;
14318 struct glyph_row *row;
14319 int window_height;
14320
14321 if (!make_cursor_line_fully_visible_p)
14322 return 1;
14323
14324 /* It's not always possible to find the cursor, e.g, when a window
14325 is full of overlay strings. Don't do anything in that case. */
14326 if (w->cursor.vpos < 0)
14327 return 1;
14328
14329 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14330 row = MATRIX_ROW (matrix, w->cursor.vpos);
14331
14332 /* If the cursor row is not partially visible, there's nothing to do. */
14333 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14334 return 1;
14335
14336 /* If the row the cursor is in is taller than the window's height,
14337 it's not clear what to do, so do nothing. */
14338 window_height = window_box_height (w);
14339 if (row->height >= window_height)
14340 {
14341 if (!force_p || MINI_WINDOW_P (w)
14342 || w->vscroll || w->cursor.vpos == 0)
14343 return 1;
14344 }
14345 return 0;
14346 }
14347
14348
14349 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14350 non-zero means only WINDOW is redisplayed in redisplay_internal.
14351 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14352 in redisplay_window to bring a partially visible line into view in
14353 the case that only the cursor has moved.
14354
14355 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14356 last screen line's vertical height extends past the end of the screen.
14357
14358 Value is
14359
14360 1 if scrolling succeeded
14361
14362 0 if scrolling didn't find point.
14363
14364 -1 if new fonts have been loaded so that we must interrupt
14365 redisplay, adjust glyph matrices, and try again. */
14366
14367 enum
14368 {
14369 SCROLLING_SUCCESS,
14370 SCROLLING_FAILED,
14371 SCROLLING_NEED_LARGER_MATRICES
14372 };
14373
14374 /* If scroll-conservatively is more than this, never recenter.
14375
14376 If you change this, don't forget to update the doc string of
14377 `scroll-conservatively' and the Emacs manual. */
14378 #define SCROLL_LIMIT 100
14379
14380 static int
14381 try_scrolling (Lisp_Object window, int just_this_one_p,
14382 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14383 int temp_scroll_step, int last_line_misfit)
14384 {
14385 struct window *w = XWINDOW (window);
14386 struct frame *f = XFRAME (w->frame);
14387 struct text_pos pos, startp;
14388 struct it it;
14389 int this_scroll_margin, scroll_max, rc, height;
14390 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14391 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14392 Lisp_Object aggressive;
14393 /* We will never try scrolling more than this number of lines. */
14394 int scroll_limit = SCROLL_LIMIT;
14395
14396 #if GLYPH_DEBUG
14397 debug_method_add (w, "try_scrolling");
14398 #endif
14399
14400 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14401
14402 /* Compute scroll margin height in pixels. We scroll when point is
14403 within this distance from the top or bottom of the window. */
14404 if (scroll_margin > 0)
14405 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14406 * FRAME_LINE_HEIGHT (f);
14407 else
14408 this_scroll_margin = 0;
14409
14410 /* Force arg_scroll_conservatively to have a reasonable value, to
14411 avoid scrolling too far away with slow move_it_* functions. Note
14412 that the user can supply scroll-conservatively equal to
14413 `most-positive-fixnum', which can be larger than INT_MAX. */
14414 if (arg_scroll_conservatively > scroll_limit)
14415 {
14416 arg_scroll_conservatively = scroll_limit + 1;
14417 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14418 }
14419 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14420 /* Compute how much we should try to scroll maximally to bring
14421 point into view. */
14422 scroll_max = (max (scroll_step,
14423 max (arg_scroll_conservatively, temp_scroll_step))
14424 * FRAME_LINE_HEIGHT (f));
14425 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14426 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14427 /* We're trying to scroll because of aggressive scrolling but no
14428 scroll_step is set. Choose an arbitrary one. */
14429 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14430 else
14431 scroll_max = 0;
14432
14433 too_near_end:
14434
14435 /* Decide whether to scroll down. */
14436 if (PT > CHARPOS (startp))
14437 {
14438 int scroll_margin_y;
14439
14440 /* Compute the pixel ypos of the scroll margin, then move IT to
14441 either that ypos or PT, whichever comes first. */
14442 start_display (&it, w, startp);
14443 scroll_margin_y = it.last_visible_y - this_scroll_margin
14444 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14445 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14446 (MOVE_TO_POS | MOVE_TO_Y));
14447
14448 if (PT > CHARPOS (it.current.pos))
14449 {
14450 int y0 = line_bottom_y (&it);
14451 /* Compute how many pixels below window bottom to stop searching
14452 for PT. This avoids costly search for PT that is far away if
14453 the user limited scrolling by a small number of lines, but
14454 always finds PT if scroll_conservatively is set to a large
14455 number, such as most-positive-fixnum. */
14456 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14457 int y_to_move = it.last_visible_y + slack;
14458
14459 /* Compute the distance from the scroll margin to PT or to
14460 the scroll limit, whichever comes first. This should
14461 include the height of the cursor line, to make that line
14462 fully visible. */
14463 move_it_to (&it, PT, -1, y_to_move,
14464 -1, MOVE_TO_POS | MOVE_TO_Y);
14465 dy = line_bottom_y (&it) - y0;
14466
14467 if (dy > scroll_max)
14468 return SCROLLING_FAILED;
14469
14470 if (dy > 0)
14471 scroll_down_p = 1;
14472 }
14473 }
14474
14475 if (scroll_down_p)
14476 {
14477 /* Point is in or below the bottom scroll margin, so move the
14478 window start down. If scrolling conservatively, move it just
14479 enough down to make point visible. If scroll_step is set,
14480 move it down by scroll_step. */
14481 if (arg_scroll_conservatively)
14482 amount_to_scroll
14483 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14484 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14485 else if (scroll_step || temp_scroll_step)
14486 amount_to_scroll = scroll_max;
14487 else
14488 {
14489 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14490 height = WINDOW_BOX_TEXT_HEIGHT (w);
14491 if (NUMBERP (aggressive))
14492 {
14493 double float_amount = XFLOATINT (aggressive) * height;
14494 amount_to_scroll = float_amount;
14495 if (amount_to_scroll == 0 && float_amount > 0)
14496 amount_to_scroll = 1;
14497 /* Don't let point enter the scroll margin near top of
14498 the window. */
14499 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14500 amount_to_scroll = height - 2*this_scroll_margin + dy;
14501 }
14502 }
14503
14504 if (amount_to_scroll <= 0)
14505 return SCROLLING_FAILED;
14506
14507 start_display (&it, w, startp);
14508 if (arg_scroll_conservatively <= scroll_limit)
14509 move_it_vertically (&it, amount_to_scroll);
14510 else
14511 {
14512 /* Extra precision for users who set scroll-conservatively
14513 to a large number: make sure the amount we scroll
14514 the window start is never less than amount_to_scroll,
14515 which was computed as distance from window bottom to
14516 point. This matters when lines at window top and lines
14517 below window bottom have different height. */
14518 struct it it1;
14519 void *it1data = NULL;
14520 /* We use a temporary it1 because line_bottom_y can modify
14521 its argument, if it moves one line down; see there. */
14522 int start_y;
14523
14524 SAVE_IT (it1, it, it1data);
14525 start_y = line_bottom_y (&it1);
14526 do {
14527 RESTORE_IT (&it, &it, it1data);
14528 move_it_by_lines (&it, 1);
14529 SAVE_IT (it1, it, it1data);
14530 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14531 }
14532
14533 /* If STARTP is unchanged, move it down another screen line. */
14534 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14535 move_it_by_lines (&it, 1);
14536 startp = it.current.pos;
14537 }
14538 else
14539 {
14540 struct text_pos scroll_margin_pos = startp;
14541
14542 /* See if point is inside the scroll margin at the top of the
14543 window. */
14544 if (this_scroll_margin)
14545 {
14546 start_display (&it, w, startp);
14547 move_it_vertically (&it, this_scroll_margin);
14548 scroll_margin_pos = it.current.pos;
14549 }
14550
14551 if (PT < CHARPOS (scroll_margin_pos))
14552 {
14553 /* Point is in the scroll margin at the top of the window or
14554 above what is displayed in the window. */
14555 int y0, y_to_move;
14556
14557 /* Compute the vertical distance from PT to the scroll
14558 margin position. Move as far as scroll_max allows, or
14559 one screenful, or 10 screen lines, whichever is largest.
14560 Give up if distance is greater than scroll_max. */
14561 SET_TEXT_POS (pos, PT, PT_BYTE);
14562 start_display (&it, w, pos);
14563 y0 = it.current_y;
14564 y_to_move = max (it.last_visible_y,
14565 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14566 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14567 y_to_move, -1,
14568 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14569 dy = it.current_y - y0;
14570 if (dy > scroll_max)
14571 return SCROLLING_FAILED;
14572
14573 /* Compute new window start. */
14574 start_display (&it, w, startp);
14575
14576 if (arg_scroll_conservatively)
14577 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14578 max (scroll_step, temp_scroll_step));
14579 else if (scroll_step || temp_scroll_step)
14580 amount_to_scroll = scroll_max;
14581 else
14582 {
14583 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14584 height = WINDOW_BOX_TEXT_HEIGHT (w);
14585 if (NUMBERP (aggressive))
14586 {
14587 double float_amount = XFLOATINT (aggressive) * height;
14588 amount_to_scroll = float_amount;
14589 if (amount_to_scroll == 0 && float_amount > 0)
14590 amount_to_scroll = 1;
14591 amount_to_scroll -=
14592 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14593 /* Don't let point enter the scroll margin near
14594 bottom of the window. */
14595 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14596 amount_to_scroll = height - 2*this_scroll_margin + dy;
14597 }
14598 }
14599
14600 if (amount_to_scroll <= 0)
14601 return SCROLLING_FAILED;
14602
14603 move_it_vertically_backward (&it, amount_to_scroll);
14604 startp = it.current.pos;
14605 }
14606 }
14607
14608 /* Run window scroll functions. */
14609 startp = run_window_scroll_functions (window, startp);
14610
14611 /* Display the window. Give up if new fonts are loaded, or if point
14612 doesn't appear. */
14613 if (!try_window (window, startp, 0))
14614 rc = SCROLLING_NEED_LARGER_MATRICES;
14615 else if (w->cursor.vpos < 0)
14616 {
14617 clear_glyph_matrix (w->desired_matrix);
14618 rc = SCROLLING_FAILED;
14619 }
14620 else
14621 {
14622 /* Maybe forget recorded base line for line number display. */
14623 if (!just_this_one_p
14624 || current_buffer->clip_changed
14625 || BEG_UNCHANGED < CHARPOS (startp))
14626 w->base_line_number = Qnil;
14627
14628 /* If cursor ends up on a partially visible line,
14629 treat that as being off the bottom of the screen. */
14630 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14631 /* It's possible that the cursor is on the first line of the
14632 buffer, which is partially obscured due to a vscroll
14633 (Bug#7537). In that case, avoid looping forever . */
14634 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14635 {
14636 clear_glyph_matrix (w->desired_matrix);
14637 ++extra_scroll_margin_lines;
14638 goto too_near_end;
14639 }
14640 rc = SCROLLING_SUCCESS;
14641 }
14642
14643 return rc;
14644 }
14645
14646
14647 /* Compute a suitable window start for window W if display of W starts
14648 on a continuation line. Value is non-zero if a new window start
14649 was computed.
14650
14651 The new window start will be computed, based on W's width, starting
14652 from the start of the continued line. It is the start of the
14653 screen line with the minimum distance from the old start W->start. */
14654
14655 static int
14656 compute_window_start_on_continuation_line (struct window *w)
14657 {
14658 struct text_pos pos, start_pos;
14659 int window_start_changed_p = 0;
14660
14661 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14662
14663 /* If window start is on a continuation line... Window start may be
14664 < BEGV in case there's invisible text at the start of the
14665 buffer (M-x rmail, for example). */
14666 if (CHARPOS (start_pos) > BEGV
14667 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14668 {
14669 struct it it;
14670 struct glyph_row *row;
14671
14672 /* Handle the case that the window start is out of range. */
14673 if (CHARPOS (start_pos) < BEGV)
14674 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14675 else if (CHARPOS (start_pos) > ZV)
14676 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14677
14678 /* Find the start of the continued line. This should be fast
14679 because scan_buffer is fast (newline cache). */
14680 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14681 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14682 row, DEFAULT_FACE_ID);
14683 reseat_at_previous_visible_line_start (&it);
14684
14685 /* If the line start is "too far" away from the window start,
14686 say it takes too much time to compute a new window start. */
14687 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14688 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14689 {
14690 int min_distance, distance;
14691
14692 /* Move forward by display lines to find the new window
14693 start. If window width was enlarged, the new start can
14694 be expected to be > the old start. If window width was
14695 decreased, the new window start will be < the old start.
14696 So, we're looking for the display line start with the
14697 minimum distance from the old window start. */
14698 pos = it.current.pos;
14699 min_distance = INFINITY;
14700 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14701 distance < min_distance)
14702 {
14703 min_distance = distance;
14704 pos = it.current.pos;
14705 move_it_by_lines (&it, 1);
14706 }
14707
14708 /* Set the window start there. */
14709 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14710 window_start_changed_p = 1;
14711 }
14712 }
14713
14714 return window_start_changed_p;
14715 }
14716
14717
14718 /* Try cursor movement in case text has not changed in window WINDOW,
14719 with window start STARTP. Value is
14720
14721 CURSOR_MOVEMENT_SUCCESS if successful
14722
14723 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14724
14725 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14726 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14727 we want to scroll as if scroll-step were set to 1. See the code.
14728
14729 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14730 which case we have to abort this redisplay, and adjust matrices
14731 first. */
14732
14733 enum
14734 {
14735 CURSOR_MOVEMENT_SUCCESS,
14736 CURSOR_MOVEMENT_CANNOT_BE_USED,
14737 CURSOR_MOVEMENT_MUST_SCROLL,
14738 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14739 };
14740
14741 static int
14742 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14743 {
14744 struct window *w = XWINDOW (window);
14745 struct frame *f = XFRAME (w->frame);
14746 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14747
14748 #if GLYPH_DEBUG
14749 if (inhibit_try_cursor_movement)
14750 return rc;
14751 #endif
14752
14753 /* Handle case where text has not changed, only point, and it has
14754 not moved off the frame. */
14755 if (/* Point may be in this window. */
14756 PT >= CHARPOS (startp)
14757 /* Selective display hasn't changed. */
14758 && !current_buffer->clip_changed
14759 /* Function force-mode-line-update is used to force a thorough
14760 redisplay. It sets either windows_or_buffers_changed or
14761 update_mode_lines. So don't take a shortcut here for these
14762 cases. */
14763 && !update_mode_lines
14764 && !windows_or_buffers_changed
14765 && !cursor_type_changed
14766 /* Can't use this case if highlighting a region. When a
14767 region exists, cursor movement has to do more than just
14768 set the cursor. */
14769 && !(!NILP (Vtransient_mark_mode)
14770 && !NILP (BVAR (current_buffer, mark_active)))
14771 && NILP (w->region_showing)
14772 && NILP (Vshow_trailing_whitespace)
14773 /* Right after splitting windows, last_point may be nil. */
14774 && INTEGERP (w->last_point)
14775 /* This code is not used for mini-buffer for the sake of the case
14776 of redisplaying to replace an echo area message; since in
14777 that case the mini-buffer contents per se are usually
14778 unchanged. This code is of no real use in the mini-buffer
14779 since the handling of this_line_start_pos, etc., in redisplay
14780 handles the same cases. */
14781 && !EQ (window, minibuf_window)
14782 /* When splitting windows or for new windows, it happens that
14783 redisplay is called with a nil window_end_vpos or one being
14784 larger than the window. This should really be fixed in
14785 window.c. I don't have this on my list, now, so we do
14786 approximately the same as the old redisplay code. --gerd. */
14787 && INTEGERP (w->window_end_vpos)
14788 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14789 && (FRAME_WINDOW_P (f)
14790 || !overlay_arrow_in_current_buffer_p ()))
14791 {
14792 int this_scroll_margin, top_scroll_margin;
14793 struct glyph_row *row = NULL;
14794
14795 #if GLYPH_DEBUG
14796 debug_method_add (w, "cursor movement");
14797 #endif
14798
14799 /* Scroll if point within this distance from the top or bottom
14800 of the window. This is a pixel value. */
14801 if (scroll_margin > 0)
14802 {
14803 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14804 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14805 }
14806 else
14807 this_scroll_margin = 0;
14808
14809 top_scroll_margin = this_scroll_margin;
14810 if (WINDOW_WANTS_HEADER_LINE_P (w))
14811 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14812
14813 /* Start with the row the cursor was displayed during the last
14814 not paused redisplay. Give up if that row is not valid. */
14815 if (w->last_cursor.vpos < 0
14816 || w->last_cursor.vpos >= w->current_matrix->nrows)
14817 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14818 else
14819 {
14820 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14821 if (row->mode_line_p)
14822 ++row;
14823 if (!row->enabled_p)
14824 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14825 }
14826
14827 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14828 {
14829 int scroll_p = 0, must_scroll = 0;
14830 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14831
14832 if (PT > XFASTINT (w->last_point))
14833 {
14834 /* Point has moved forward. */
14835 while (MATRIX_ROW_END_CHARPOS (row) < PT
14836 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14837 {
14838 xassert (row->enabled_p);
14839 ++row;
14840 }
14841
14842 /* If the end position of a row equals the start
14843 position of the next row, and PT is at that position,
14844 we would rather display cursor in the next line. */
14845 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14846 && MATRIX_ROW_END_CHARPOS (row) == PT
14847 && row < w->current_matrix->rows
14848 + w->current_matrix->nrows - 1
14849 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14850 && !cursor_row_p (row))
14851 ++row;
14852
14853 /* If within the scroll margin, scroll. Note that
14854 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14855 the next line would be drawn, and that
14856 this_scroll_margin can be zero. */
14857 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14858 || PT > MATRIX_ROW_END_CHARPOS (row)
14859 /* Line is completely visible last line in window
14860 and PT is to be set in the next line. */
14861 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14862 && PT == MATRIX_ROW_END_CHARPOS (row)
14863 && !row->ends_at_zv_p
14864 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14865 scroll_p = 1;
14866 }
14867 else if (PT < XFASTINT (w->last_point))
14868 {
14869 /* Cursor has to be moved backward. Note that PT >=
14870 CHARPOS (startp) because of the outer if-statement. */
14871 while (!row->mode_line_p
14872 && (MATRIX_ROW_START_CHARPOS (row) > PT
14873 || (MATRIX_ROW_START_CHARPOS (row) == PT
14874 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14875 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14876 row > w->current_matrix->rows
14877 && (row-1)->ends_in_newline_from_string_p))))
14878 && (row->y > top_scroll_margin
14879 || CHARPOS (startp) == BEGV))
14880 {
14881 xassert (row->enabled_p);
14882 --row;
14883 }
14884
14885 /* Consider the following case: Window starts at BEGV,
14886 there is invisible, intangible text at BEGV, so that
14887 display starts at some point START > BEGV. It can
14888 happen that we are called with PT somewhere between
14889 BEGV and START. Try to handle that case. */
14890 if (row < w->current_matrix->rows
14891 || row->mode_line_p)
14892 {
14893 row = w->current_matrix->rows;
14894 if (row->mode_line_p)
14895 ++row;
14896 }
14897
14898 /* Due to newlines in overlay strings, we may have to
14899 skip forward over overlay strings. */
14900 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14901 && MATRIX_ROW_END_CHARPOS (row) == PT
14902 && !cursor_row_p (row))
14903 ++row;
14904
14905 /* If within the scroll margin, scroll. */
14906 if (row->y < top_scroll_margin
14907 && CHARPOS (startp) != BEGV)
14908 scroll_p = 1;
14909 }
14910 else
14911 {
14912 /* Cursor did not move. So don't scroll even if cursor line
14913 is partially visible, as it was so before. */
14914 rc = CURSOR_MOVEMENT_SUCCESS;
14915 }
14916
14917 if (PT < MATRIX_ROW_START_CHARPOS (row)
14918 || PT > MATRIX_ROW_END_CHARPOS (row))
14919 {
14920 /* if PT is not in the glyph row, give up. */
14921 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14922 must_scroll = 1;
14923 }
14924 else if (rc != CURSOR_MOVEMENT_SUCCESS
14925 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14926 {
14927 struct glyph_row *row1;
14928
14929 /* If rows are bidi-reordered and point moved, back up
14930 until we find a row that does not belong to a
14931 continuation line. This is because we must consider
14932 all rows of a continued line as candidates for the
14933 new cursor positioning, since row start and end
14934 positions change non-linearly with vertical position
14935 in such rows. */
14936 /* FIXME: Revisit this when glyph ``spilling'' in
14937 continuation lines' rows is implemented for
14938 bidi-reordered rows. */
14939 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14940 MATRIX_ROW_CONTINUATION_LINE_P (row);
14941 --row)
14942 {
14943 /* If we hit the beginning of the displayed portion
14944 without finding the first row of a continued
14945 line, give up. */
14946 if (row <= row1)
14947 {
14948 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14949 break;
14950 }
14951 xassert (row->enabled_p);
14952 }
14953 }
14954 if (must_scroll)
14955 ;
14956 else if (rc != CURSOR_MOVEMENT_SUCCESS
14957 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14958 /* Make sure this isn't a header line by any chance, since
14959 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
14960 && !row->mode_line_p
14961 && make_cursor_line_fully_visible_p)
14962 {
14963 if (PT == MATRIX_ROW_END_CHARPOS (row)
14964 && !row->ends_at_zv_p
14965 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14966 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14967 else if (row->height > window_box_height (w))
14968 {
14969 /* If we end up in a partially visible line, let's
14970 make it fully visible, except when it's taller
14971 than the window, in which case we can't do much
14972 about it. */
14973 *scroll_step = 1;
14974 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14975 }
14976 else
14977 {
14978 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14979 if (!cursor_row_fully_visible_p (w, 0, 1))
14980 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14981 else
14982 rc = CURSOR_MOVEMENT_SUCCESS;
14983 }
14984 }
14985 else if (scroll_p)
14986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14987 else if (rc != CURSOR_MOVEMENT_SUCCESS
14988 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14989 {
14990 /* With bidi-reordered rows, there could be more than
14991 one candidate row whose start and end positions
14992 occlude point. We need to let set_cursor_from_row
14993 find the best candidate. */
14994 /* FIXME: Revisit this when glyph ``spilling'' in
14995 continuation lines' rows is implemented for
14996 bidi-reordered rows. */
14997 int rv = 0;
14998
14999 do
15000 {
15001 int at_zv_p = 0, exact_match_p = 0;
15002
15003 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15004 && PT <= MATRIX_ROW_END_CHARPOS (row)
15005 && cursor_row_p (row))
15006 rv |= set_cursor_from_row (w, row, w->current_matrix,
15007 0, 0, 0, 0);
15008 /* As soon as we've found the exact match for point,
15009 or the first suitable row whose ends_at_zv_p flag
15010 is set, we are done. */
15011 at_zv_p =
15012 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15013 if (rv && !at_zv_p
15014 && w->cursor.hpos >= 0
15015 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15016 w->cursor.vpos))
15017 {
15018 struct glyph_row *candidate =
15019 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15020 struct glyph *g =
15021 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15022 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15023
15024 exact_match_p =
15025 (BUFFERP (g->object) && g->charpos == PT)
15026 || (INTEGERP (g->object)
15027 && (g->charpos == PT
15028 || (g->charpos == 0 && endpos - 1 == PT)));
15029 }
15030 if (rv && (at_zv_p || exact_match_p))
15031 {
15032 rc = CURSOR_MOVEMENT_SUCCESS;
15033 break;
15034 }
15035 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15036 break;
15037 ++row;
15038 }
15039 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15040 || row->continued_p)
15041 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15042 || (MATRIX_ROW_START_CHARPOS (row) == PT
15043 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15044 /* If we didn't find any candidate rows, or exited the
15045 loop before all the candidates were examined, signal
15046 to the caller that this method failed. */
15047 if (rc != CURSOR_MOVEMENT_SUCCESS
15048 && !(rv
15049 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15050 && !row->continued_p))
15051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15052 else if (rv)
15053 rc = CURSOR_MOVEMENT_SUCCESS;
15054 }
15055 else
15056 {
15057 do
15058 {
15059 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15060 {
15061 rc = CURSOR_MOVEMENT_SUCCESS;
15062 break;
15063 }
15064 ++row;
15065 }
15066 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15067 && MATRIX_ROW_START_CHARPOS (row) == PT
15068 && cursor_row_p (row));
15069 }
15070 }
15071 }
15072
15073 return rc;
15074 }
15075
15076 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15077 static
15078 #endif
15079 void
15080 set_vertical_scroll_bar (struct window *w)
15081 {
15082 ptrdiff_t start, end, whole;
15083
15084 /* Calculate the start and end positions for the current window.
15085 At some point, it would be nice to choose between scrollbars
15086 which reflect the whole buffer size, with special markers
15087 indicating narrowing, and scrollbars which reflect only the
15088 visible region.
15089
15090 Note that mini-buffers sometimes aren't displaying any text. */
15091 if (!MINI_WINDOW_P (w)
15092 || (w == XWINDOW (minibuf_window)
15093 && NILP (echo_area_buffer[0])))
15094 {
15095 struct buffer *buf = XBUFFER (w->buffer);
15096 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15097 start = marker_position (w->start) - BUF_BEGV (buf);
15098 /* I don't think this is guaranteed to be right. For the
15099 moment, we'll pretend it is. */
15100 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15101
15102 if (end < start)
15103 end = start;
15104 if (whole < (end - start))
15105 whole = end - start;
15106 }
15107 else
15108 start = end = whole = 0;
15109
15110 /* Indicate what this scroll bar ought to be displaying now. */
15111 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15112 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15113 (w, end - start, whole, start);
15114 }
15115
15116
15117 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15118 selected_window is redisplayed.
15119
15120 We can return without actually redisplaying the window if
15121 fonts_changed_p is nonzero. In that case, redisplay_internal will
15122 retry. */
15123
15124 static void
15125 redisplay_window (Lisp_Object window, int just_this_one_p)
15126 {
15127 struct window *w = XWINDOW (window);
15128 struct frame *f = XFRAME (w->frame);
15129 struct buffer *buffer = XBUFFER (w->buffer);
15130 struct buffer *old = current_buffer;
15131 struct text_pos lpoint, opoint, startp;
15132 int update_mode_line;
15133 int tem;
15134 struct it it;
15135 /* Record it now because it's overwritten. */
15136 int current_matrix_up_to_date_p = 0;
15137 int used_current_matrix_p = 0;
15138 /* This is less strict than current_matrix_up_to_date_p.
15139 It indicates that the buffer contents and narrowing are unchanged. */
15140 int buffer_unchanged_p = 0;
15141 int temp_scroll_step = 0;
15142 ptrdiff_t count = SPECPDL_INDEX ();
15143 int rc;
15144 int centering_position = -1;
15145 int last_line_misfit = 0;
15146 ptrdiff_t beg_unchanged, end_unchanged;
15147
15148 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15149 opoint = lpoint;
15150
15151 /* W must be a leaf window here. */
15152 xassert (!NILP (w->buffer));
15153 #if GLYPH_DEBUG
15154 *w->desired_matrix->method = 0;
15155 #endif
15156
15157 restart:
15158 reconsider_clip_changes (w, buffer);
15159
15160 /* Has the mode line to be updated? */
15161 update_mode_line = (!NILP (w->update_mode_line)
15162 || update_mode_lines
15163 || buffer->clip_changed
15164 || buffer->prevent_redisplay_optimizations_p);
15165
15166 if (MINI_WINDOW_P (w))
15167 {
15168 if (w == XWINDOW (echo_area_window)
15169 && !NILP (echo_area_buffer[0]))
15170 {
15171 if (update_mode_line)
15172 /* We may have to update a tty frame's menu bar or a
15173 tool-bar. Example `M-x C-h C-h C-g'. */
15174 goto finish_menu_bars;
15175 else
15176 /* We've already displayed the echo area glyphs in this window. */
15177 goto finish_scroll_bars;
15178 }
15179 else if ((w != XWINDOW (minibuf_window)
15180 || minibuf_level == 0)
15181 /* When buffer is nonempty, redisplay window normally. */
15182 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15183 /* Quail displays non-mini buffers in minibuffer window.
15184 In that case, redisplay the window normally. */
15185 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15186 {
15187 /* W is a mini-buffer window, but it's not active, so clear
15188 it. */
15189 int yb = window_text_bottom_y (w);
15190 struct glyph_row *row;
15191 int y;
15192
15193 for (y = 0, row = w->desired_matrix->rows;
15194 y < yb;
15195 y += row->height, ++row)
15196 blank_row (w, row, y);
15197 goto finish_scroll_bars;
15198 }
15199
15200 clear_glyph_matrix (w->desired_matrix);
15201 }
15202
15203 /* Otherwise set up data on this window; select its buffer and point
15204 value. */
15205 /* Really select the buffer, for the sake of buffer-local
15206 variables. */
15207 set_buffer_internal_1 (XBUFFER (w->buffer));
15208
15209 current_matrix_up_to_date_p
15210 = (!NILP (w->window_end_valid)
15211 && !current_buffer->clip_changed
15212 && !current_buffer->prevent_redisplay_optimizations_p
15213 && XFASTINT (w->last_modified) >= MODIFF
15214 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15215
15216 /* Run the window-bottom-change-functions
15217 if it is possible that the text on the screen has changed
15218 (either due to modification of the text, or any other reason). */
15219 if (!current_matrix_up_to_date_p
15220 && !NILP (Vwindow_text_change_functions))
15221 {
15222 safe_run_hooks (Qwindow_text_change_functions);
15223 goto restart;
15224 }
15225
15226 beg_unchanged = BEG_UNCHANGED;
15227 end_unchanged = END_UNCHANGED;
15228
15229 SET_TEXT_POS (opoint, PT, PT_BYTE);
15230
15231 specbind (Qinhibit_point_motion_hooks, Qt);
15232
15233 buffer_unchanged_p
15234 = (!NILP (w->window_end_valid)
15235 && !current_buffer->clip_changed
15236 && XFASTINT (w->last_modified) >= MODIFF
15237 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15238
15239 /* When windows_or_buffers_changed is non-zero, we can't rely on
15240 the window end being valid, so set it to nil there. */
15241 if (windows_or_buffers_changed)
15242 {
15243 /* If window starts on a continuation line, maybe adjust the
15244 window start in case the window's width changed. */
15245 if (XMARKER (w->start)->buffer == current_buffer)
15246 compute_window_start_on_continuation_line (w);
15247
15248 w->window_end_valid = Qnil;
15249 }
15250
15251 /* Some sanity checks. */
15252 CHECK_WINDOW_END (w);
15253 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15254 abort ();
15255 if (BYTEPOS (opoint) < CHARPOS (opoint))
15256 abort ();
15257
15258 /* If %c is in mode line, update it if needed. */
15259 if (!NILP (w->column_number_displayed)
15260 /* This alternative quickly identifies a common case
15261 where no change is needed. */
15262 && !(PT == XFASTINT (w->last_point)
15263 && XFASTINT (w->last_modified) >= MODIFF
15264 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15265 && (XFASTINT (w->column_number_displayed) != current_column ()))
15266 update_mode_line = 1;
15267
15268 /* Count number of windows showing the selected buffer. An indirect
15269 buffer counts as its base buffer. */
15270 if (!just_this_one_p)
15271 {
15272 struct buffer *current_base, *window_base;
15273 current_base = current_buffer;
15274 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15275 if (current_base->base_buffer)
15276 current_base = current_base->base_buffer;
15277 if (window_base->base_buffer)
15278 window_base = window_base->base_buffer;
15279 if (current_base == window_base)
15280 buffer_shared++;
15281 }
15282
15283 /* Point refers normally to the selected window. For any other
15284 window, set up appropriate value. */
15285 if (!EQ (window, selected_window))
15286 {
15287 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15288 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15289 if (new_pt < BEGV)
15290 {
15291 new_pt = BEGV;
15292 new_pt_byte = BEGV_BYTE;
15293 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15294 }
15295 else if (new_pt > (ZV - 1))
15296 {
15297 new_pt = ZV;
15298 new_pt_byte = ZV_BYTE;
15299 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15300 }
15301
15302 /* We don't use SET_PT so that the point-motion hooks don't run. */
15303 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15304 }
15305
15306 /* If any of the character widths specified in the display table
15307 have changed, invalidate the width run cache. It's true that
15308 this may be a bit late to catch such changes, but the rest of
15309 redisplay goes (non-fatally) haywire when the display table is
15310 changed, so why should we worry about doing any better? */
15311 if (current_buffer->width_run_cache)
15312 {
15313 struct Lisp_Char_Table *disptab = buffer_display_table ();
15314
15315 if (! disptab_matches_widthtab (disptab,
15316 XVECTOR (BVAR (current_buffer, width_table))))
15317 {
15318 invalidate_region_cache (current_buffer,
15319 current_buffer->width_run_cache,
15320 BEG, Z);
15321 recompute_width_table (current_buffer, disptab);
15322 }
15323 }
15324
15325 /* If window-start is screwed up, choose a new one. */
15326 if (XMARKER (w->start)->buffer != current_buffer)
15327 goto recenter;
15328
15329 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15330
15331 /* If someone specified a new starting point but did not insist,
15332 check whether it can be used. */
15333 if (!NILP (w->optional_new_start)
15334 && CHARPOS (startp) >= BEGV
15335 && CHARPOS (startp) <= ZV)
15336 {
15337 w->optional_new_start = Qnil;
15338 start_display (&it, w, startp);
15339 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15340 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15341 if (IT_CHARPOS (it) == PT)
15342 w->force_start = Qt;
15343 /* IT may overshoot PT if text at PT is invisible. */
15344 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15345 w->force_start = Qt;
15346 }
15347
15348 force_start:
15349
15350 /* Handle case where place to start displaying has been specified,
15351 unless the specified location is outside the accessible range. */
15352 if (!NILP (w->force_start)
15353 || w->frozen_window_start_p)
15354 {
15355 /* We set this later on if we have to adjust point. */
15356 int new_vpos = -1;
15357
15358 w->force_start = Qnil;
15359 w->vscroll = 0;
15360 w->window_end_valid = Qnil;
15361
15362 /* Forget any recorded base line for line number display. */
15363 if (!buffer_unchanged_p)
15364 w->base_line_number = Qnil;
15365
15366 /* Redisplay the mode line. Select the buffer properly for that.
15367 Also, run the hook window-scroll-functions
15368 because we have scrolled. */
15369 /* Note, we do this after clearing force_start because
15370 if there's an error, it is better to forget about force_start
15371 than to get into an infinite loop calling the hook functions
15372 and having them get more errors. */
15373 if (!update_mode_line
15374 || ! NILP (Vwindow_scroll_functions))
15375 {
15376 update_mode_line = 1;
15377 w->update_mode_line = Qt;
15378 startp = run_window_scroll_functions (window, startp);
15379 }
15380
15381 w->last_modified = make_number (0);
15382 w->last_overlay_modified = make_number (0);
15383 if (CHARPOS (startp) < BEGV)
15384 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15385 else if (CHARPOS (startp) > ZV)
15386 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15387
15388 /* Redisplay, then check if cursor has been set during the
15389 redisplay. Give up if new fonts were loaded. */
15390 /* We used to issue a CHECK_MARGINS argument to try_window here,
15391 but this causes scrolling to fail when point begins inside
15392 the scroll margin (bug#148) -- cyd */
15393 if (!try_window (window, startp, 0))
15394 {
15395 w->force_start = Qt;
15396 clear_glyph_matrix (w->desired_matrix);
15397 goto need_larger_matrices;
15398 }
15399
15400 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15401 {
15402 /* If point does not appear, try to move point so it does
15403 appear. The desired matrix has been built above, so we
15404 can use it here. */
15405 new_vpos = window_box_height (w) / 2;
15406 }
15407
15408 if (!cursor_row_fully_visible_p (w, 0, 0))
15409 {
15410 /* Point does appear, but on a line partly visible at end of window.
15411 Move it back to a fully-visible line. */
15412 new_vpos = window_box_height (w);
15413 }
15414
15415 /* If we need to move point for either of the above reasons,
15416 now actually do it. */
15417 if (new_vpos >= 0)
15418 {
15419 struct glyph_row *row;
15420
15421 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15422 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15423 ++row;
15424
15425 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15426 MATRIX_ROW_START_BYTEPOS (row));
15427
15428 if (w != XWINDOW (selected_window))
15429 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15430 else if (current_buffer == old)
15431 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15432
15433 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15434
15435 /* If we are highlighting the region, then we just changed
15436 the region, so redisplay to show it. */
15437 if (!NILP (Vtransient_mark_mode)
15438 && !NILP (BVAR (current_buffer, mark_active)))
15439 {
15440 clear_glyph_matrix (w->desired_matrix);
15441 if (!try_window (window, startp, 0))
15442 goto need_larger_matrices;
15443 }
15444 }
15445
15446 #if GLYPH_DEBUG
15447 debug_method_add (w, "forced window start");
15448 #endif
15449 goto done;
15450 }
15451
15452 /* Handle case where text has not changed, only point, and it has
15453 not moved off the frame, and we are not retrying after hscroll.
15454 (current_matrix_up_to_date_p is nonzero when retrying.) */
15455 if (current_matrix_up_to_date_p
15456 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15457 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15458 {
15459 switch (rc)
15460 {
15461 case CURSOR_MOVEMENT_SUCCESS:
15462 used_current_matrix_p = 1;
15463 goto done;
15464
15465 case CURSOR_MOVEMENT_MUST_SCROLL:
15466 goto try_to_scroll;
15467
15468 default:
15469 abort ();
15470 }
15471 }
15472 /* If current starting point was originally the beginning of a line
15473 but no longer is, find a new starting point. */
15474 else if (!NILP (w->start_at_line_beg)
15475 && !(CHARPOS (startp) <= BEGV
15476 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15477 {
15478 #if GLYPH_DEBUG
15479 debug_method_add (w, "recenter 1");
15480 #endif
15481 goto recenter;
15482 }
15483
15484 /* Try scrolling with try_window_id. Value is > 0 if update has
15485 been done, it is -1 if we know that the same window start will
15486 not work. It is 0 if unsuccessful for some other reason. */
15487 else if ((tem = try_window_id (w)) != 0)
15488 {
15489 #if GLYPH_DEBUG
15490 debug_method_add (w, "try_window_id %d", tem);
15491 #endif
15492
15493 if (fonts_changed_p)
15494 goto need_larger_matrices;
15495 if (tem > 0)
15496 goto done;
15497
15498 /* Otherwise try_window_id has returned -1 which means that we
15499 don't want the alternative below this comment to execute. */
15500 }
15501 else if (CHARPOS (startp) >= BEGV
15502 && CHARPOS (startp) <= ZV
15503 && PT >= CHARPOS (startp)
15504 && (CHARPOS (startp) < ZV
15505 /* Avoid starting at end of buffer. */
15506 || CHARPOS (startp) == BEGV
15507 || (XFASTINT (w->last_modified) >= MODIFF
15508 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15509 {
15510 int d1, d2, d3, d4, d5, d6;
15511
15512 /* If first window line is a continuation line, and window start
15513 is inside the modified region, but the first change is before
15514 current window start, we must select a new window start.
15515
15516 However, if this is the result of a down-mouse event (e.g. by
15517 extending the mouse-drag-overlay), we don't want to select a
15518 new window start, since that would change the position under
15519 the mouse, resulting in an unwanted mouse-movement rather
15520 than a simple mouse-click. */
15521 if (NILP (w->start_at_line_beg)
15522 && NILP (do_mouse_tracking)
15523 && CHARPOS (startp) > BEGV
15524 && CHARPOS (startp) > BEG + beg_unchanged
15525 && CHARPOS (startp) <= Z - end_unchanged
15526 /* Even if w->start_at_line_beg is nil, a new window may
15527 start at a line_beg, since that's how set_buffer_window
15528 sets it. So, we need to check the return value of
15529 compute_window_start_on_continuation_line. (See also
15530 bug#197). */
15531 && XMARKER (w->start)->buffer == current_buffer
15532 && compute_window_start_on_continuation_line (w)
15533 /* It doesn't make sense to force the window start like we
15534 do at label force_start if it is already known that point
15535 will not be visible in the resulting window, because
15536 doing so will move point from its correct position
15537 instead of scrolling the window to bring point into view.
15538 See bug#9324. */
15539 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15540 {
15541 w->force_start = Qt;
15542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15543 goto force_start;
15544 }
15545
15546 #if GLYPH_DEBUG
15547 debug_method_add (w, "same window start");
15548 #endif
15549
15550 /* Try to redisplay starting at same place as before.
15551 If point has not moved off frame, accept the results. */
15552 if (!current_matrix_up_to_date_p
15553 /* Don't use try_window_reusing_current_matrix in this case
15554 because a window scroll function can have changed the
15555 buffer. */
15556 || !NILP (Vwindow_scroll_functions)
15557 || MINI_WINDOW_P (w)
15558 || !(used_current_matrix_p
15559 = try_window_reusing_current_matrix (w)))
15560 {
15561 IF_DEBUG (debug_method_add (w, "1"));
15562 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15563 /* -1 means we need to scroll.
15564 0 means we need new matrices, but fonts_changed_p
15565 is set in that case, so we will detect it below. */
15566 goto try_to_scroll;
15567 }
15568
15569 if (fonts_changed_p)
15570 goto need_larger_matrices;
15571
15572 if (w->cursor.vpos >= 0)
15573 {
15574 if (!just_this_one_p
15575 || current_buffer->clip_changed
15576 || BEG_UNCHANGED < CHARPOS (startp))
15577 /* Forget any recorded base line for line number display. */
15578 w->base_line_number = Qnil;
15579
15580 if (!cursor_row_fully_visible_p (w, 1, 0))
15581 {
15582 clear_glyph_matrix (w->desired_matrix);
15583 last_line_misfit = 1;
15584 }
15585 /* Drop through and scroll. */
15586 else
15587 goto done;
15588 }
15589 else
15590 clear_glyph_matrix (w->desired_matrix);
15591 }
15592
15593 try_to_scroll:
15594
15595 w->last_modified = make_number (0);
15596 w->last_overlay_modified = make_number (0);
15597
15598 /* Redisplay the mode line. Select the buffer properly for that. */
15599 if (!update_mode_line)
15600 {
15601 update_mode_line = 1;
15602 w->update_mode_line = Qt;
15603 }
15604
15605 /* Try to scroll by specified few lines. */
15606 if ((scroll_conservatively
15607 || emacs_scroll_step
15608 || temp_scroll_step
15609 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15610 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15611 && CHARPOS (startp) >= BEGV
15612 && CHARPOS (startp) <= ZV)
15613 {
15614 /* The function returns -1 if new fonts were loaded, 1 if
15615 successful, 0 if not successful. */
15616 int ss = try_scrolling (window, just_this_one_p,
15617 scroll_conservatively,
15618 emacs_scroll_step,
15619 temp_scroll_step, last_line_misfit);
15620 switch (ss)
15621 {
15622 case SCROLLING_SUCCESS:
15623 goto done;
15624
15625 case SCROLLING_NEED_LARGER_MATRICES:
15626 goto need_larger_matrices;
15627
15628 case SCROLLING_FAILED:
15629 break;
15630
15631 default:
15632 abort ();
15633 }
15634 }
15635
15636 /* Finally, just choose a place to start which positions point
15637 according to user preferences. */
15638
15639 recenter:
15640
15641 #if GLYPH_DEBUG
15642 debug_method_add (w, "recenter");
15643 #endif
15644
15645 /* w->vscroll = 0; */
15646
15647 /* Forget any previously recorded base line for line number display. */
15648 if (!buffer_unchanged_p)
15649 w->base_line_number = Qnil;
15650
15651 /* Determine the window start relative to point. */
15652 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15653 it.current_y = it.last_visible_y;
15654 if (centering_position < 0)
15655 {
15656 int margin =
15657 scroll_margin > 0
15658 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15659 : 0;
15660 ptrdiff_t margin_pos = CHARPOS (startp);
15661 Lisp_Object aggressive;
15662 int scrolling_up;
15663
15664 /* If there is a scroll margin at the top of the window, find
15665 its character position. */
15666 if (margin
15667 /* Cannot call start_display if startp is not in the
15668 accessible region of the buffer. This can happen when we
15669 have just switched to a different buffer and/or changed
15670 its restriction. In that case, startp is initialized to
15671 the character position 1 (BEGV) because we did not yet
15672 have chance to display the buffer even once. */
15673 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15674 {
15675 struct it it1;
15676 void *it1data = NULL;
15677
15678 SAVE_IT (it1, it, it1data);
15679 start_display (&it1, w, startp);
15680 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15681 margin_pos = IT_CHARPOS (it1);
15682 RESTORE_IT (&it, &it, it1data);
15683 }
15684 scrolling_up = PT > margin_pos;
15685 aggressive =
15686 scrolling_up
15687 ? BVAR (current_buffer, scroll_up_aggressively)
15688 : BVAR (current_buffer, scroll_down_aggressively);
15689
15690 if (!MINI_WINDOW_P (w)
15691 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15692 {
15693 int pt_offset = 0;
15694
15695 /* Setting scroll-conservatively overrides
15696 scroll-*-aggressively. */
15697 if (!scroll_conservatively && NUMBERP (aggressive))
15698 {
15699 double float_amount = XFLOATINT (aggressive);
15700
15701 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15702 if (pt_offset == 0 && float_amount > 0)
15703 pt_offset = 1;
15704 if (pt_offset && margin > 0)
15705 margin -= 1;
15706 }
15707 /* Compute how much to move the window start backward from
15708 point so that point will be displayed where the user
15709 wants it. */
15710 if (scrolling_up)
15711 {
15712 centering_position = it.last_visible_y;
15713 if (pt_offset)
15714 centering_position -= pt_offset;
15715 centering_position -=
15716 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15717 + WINDOW_HEADER_LINE_HEIGHT (w);
15718 /* Don't let point enter the scroll margin near top of
15719 the window. */
15720 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15721 centering_position = margin * FRAME_LINE_HEIGHT (f);
15722 }
15723 else
15724 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15725 }
15726 else
15727 /* Set the window start half the height of the window backward
15728 from point. */
15729 centering_position = window_box_height (w) / 2;
15730 }
15731 move_it_vertically_backward (&it, centering_position);
15732
15733 xassert (IT_CHARPOS (it) >= BEGV);
15734
15735 /* The function move_it_vertically_backward may move over more
15736 than the specified y-distance. If it->w is small, e.g. a
15737 mini-buffer window, we may end up in front of the window's
15738 display area. Start displaying at the start of the line
15739 containing PT in this case. */
15740 if (it.current_y <= 0)
15741 {
15742 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15743 move_it_vertically_backward (&it, 0);
15744 it.current_y = 0;
15745 }
15746
15747 it.current_x = it.hpos = 0;
15748
15749 /* Set the window start position here explicitly, to avoid an
15750 infinite loop in case the functions in window-scroll-functions
15751 get errors. */
15752 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15753
15754 /* Run scroll hooks. */
15755 startp = run_window_scroll_functions (window, it.current.pos);
15756
15757 /* Redisplay the window. */
15758 if (!current_matrix_up_to_date_p
15759 || windows_or_buffers_changed
15760 || cursor_type_changed
15761 /* Don't use try_window_reusing_current_matrix in this case
15762 because it can have changed the buffer. */
15763 || !NILP (Vwindow_scroll_functions)
15764 || !just_this_one_p
15765 || MINI_WINDOW_P (w)
15766 || !(used_current_matrix_p
15767 = try_window_reusing_current_matrix (w)))
15768 try_window (window, startp, 0);
15769
15770 /* If new fonts have been loaded (due to fontsets), give up. We
15771 have to start a new redisplay since we need to re-adjust glyph
15772 matrices. */
15773 if (fonts_changed_p)
15774 goto need_larger_matrices;
15775
15776 /* If cursor did not appear assume that the middle of the window is
15777 in the first line of the window. Do it again with the next line.
15778 (Imagine a window of height 100, displaying two lines of height
15779 60. Moving back 50 from it->last_visible_y will end in the first
15780 line.) */
15781 if (w->cursor.vpos < 0)
15782 {
15783 if (!NILP (w->window_end_valid)
15784 && PT >= Z - XFASTINT (w->window_end_pos))
15785 {
15786 clear_glyph_matrix (w->desired_matrix);
15787 move_it_by_lines (&it, 1);
15788 try_window (window, it.current.pos, 0);
15789 }
15790 else if (PT < IT_CHARPOS (it))
15791 {
15792 clear_glyph_matrix (w->desired_matrix);
15793 move_it_by_lines (&it, -1);
15794 try_window (window, it.current.pos, 0);
15795 }
15796 else
15797 {
15798 /* Not much we can do about it. */
15799 }
15800 }
15801
15802 /* Consider the following case: Window starts at BEGV, there is
15803 invisible, intangible text at BEGV, so that display starts at
15804 some point START > BEGV. It can happen that we are called with
15805 PT somewhere between BEGV and START. Try to handle that case. */
15806 if (w->cursor.vpos < 0)
15807 {
15808 struct glyph_row *row = w->current_matrix->rows;
15809 if (row->mode_line_p)
15810 ++row;
15811 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15812 }
15813
15814 if (!cursor_row_fully_visible_p (w, 0, 0))
15815 {
15816 /* If vscroll is enabled, disable it and try again. */
15817 if (w->vscroll)
15818 {
15819 w->vscroll = 0;
15820 clear_glyph_matrix (w->desired_matrix);
15821 goto recenter;
15822 }
15823
15824 /* Users who set scroll-conservatively to a large number want
15825 point just above/below the scroll margin. If we ended up
15826 with point's row partially visible, move the window start to
15827 make that row fully visible and out of the margin. */
15828 if (scroll_conservatively > SCROLL_LIMIT)
15829 {
15830 int margin =
15831 scroll_margin > 0
15832 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15833 : 0;
15834 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15835
15836 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15837 clear_glyph_matrix (w->desired_matrix);
15838 if (1 == try_window (window, it.current.pos,
15839 TRY_WINDOW_CHECK_MARGINS))
15840 goto done;
15841 }
15842
15843 /* If centering point failed to make the whole line visible,
15844 put point at the top instead. That has to make the whole line
15845 visible, if it can be done. */
15846 if (centering_position == 0)
15847 goto done;
15848
15849 clear_glyph_matrix (w->desired_matrix);
15850 centering_position = 0;
15851 goto recenter;
15852 }
15853
15854 done:
15855
15856 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15857 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15858 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15859 ? Qt : Qnil);
15860
15861 /* Display the mode line, if we must. */
15862 if ((update_mode_line
15863 /* If window not full width, must redo its mode line
15864 if (a) the window to its side is being redone and
15865 (b) we do a frame-based redisplay. This is a consequence
15866 of how inverted lines are drawn in frame-based redisplay. */
15867 || (!just_this_one_p
15868 && !FRAME_WINDOW_P (f)
15869 && !WINDOW_FULL_WIDTH_P (w))
15870 /* Line number to display. */
15871 || INTEGERP (w->base_line_pos)
15872 /* Column number is displayed and different from the one displayed. */
15873 || (!NILP (w->column_number_displayed)
15874 && (XFASTINT (w->column_number_displayed) != current_column ())))
15875 /* This means that the window has a mode line. */
15876 && (WINDOW_WANTS_MODELINE_P (w)
15877 || WINDOW_WANTS_HEADER_LINE_P (w)))
15878 {
15879 display_mode_lines (w);
15880
15881 /* If mode line height has changed, arrange for a thorough
15882 immediate redisplay using the correct mode line height. */
15883 if (WINDOW_WANTS_MODELINE_P (w)
15884 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15885 {
15886 fonts_changed_p = 1;
15887 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15888 = DESIRED_MODE_LINE_HEIGHT (w);
15889 }
15890
15891 /* If header line height has changed, arrange for a thorough
15892 immediate redisplay using the correct header line height. */
15893 if (WINDOW_WANTS_HEADER_LINE_P (w)
15894 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15895 {
15896 fonts_changed_p = 1;
15897 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15898 = DESIRED_HEADER_LINE_HEIGHT (w);
15899 }
15900
15901 if (fonts_changed_p)
15902 goto need_larger_matrices;
15903 }
15904
15905 if (!line_number_displayed
15906 && !BUFFERP (w->base_line_pos))
15907 {
15908 w->base_line_pos = Qnil;
15909 w->base_line_number = Qnil;
15910 }
15911
15912 finish_menu_bars:
15913
15914 /* When we reach a frame's selected window, redo the frame's menu bar. */
15915 if (update_mode_line
15916 && EQ (FRAME_SELECTED_WINDOW (f), window))
15917 {
15918 int redisplay_menu_p = 0;
15919
15920 if (FRAME_WINDOW_P (f))
15921 {
15922 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15923 || defined (HAVE_NS) || defined (USE_GTK)
15924 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15925 #else
15926 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15927 #endif
15928 }
15929 else
15930 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15931
15932 if (redisplay_menu_p)
15933 display_menu_bar (w);
15934
15935 #ifdef HAVE_WINDOW_SYSTEM
15936 if (FRAME_WINDOW_P (f))
15937 {
15938 #if defined (USE_GTK) || defined (HAVE_NS)
15939 if (FRAME_EXTERNAL_TOOL_BAR (f))
15940 redisplay_tool_bar (f);
15941 #else
15942 if (WINDOWP (f->tool_bar_window)
15943 && (FRAME_TOOL_BAR_LINES (f) > 0
15944 || !NILP (Vauto_resize_tool_bars))
15945 && redisplay_tool_bar (f))
15946 ignore_mouse_drag_p = 1;
15947 #endif
15948 }
15949 #endif
15950 }
15951
15952 #ifdef HAVE_WINDOW_SYSTEM
15953 if (FRAME_WINDOW_P (f)
15954 && update_window_fringes (w, (just_this_one_p
15955 || (!used_current_matrix_p && !overlay_arrow_seen)
15956 || w->pseudo_window_p)))
15957 {
15958 update_begin (f);
15959 BLOCK_INPUT;
15960 if (draw_window_fringes (w, 1))
15961 x_draw_vertical_border (w);
15962 UNBLOCK_INPUT;
15963 update_end (f);
15964 }
15965 #endif /* HAVE_WINDOW_SYSTEM */
15966
15967 /* We go to this label, with fonts_changed_p nonzero,
15968 if it is necessary to try again using larger glyph matrices.
15969 We have to redeem the scroll bar even in this case,
15970 because the loop in redisplay_internal expects that. */
15971 need_larger_matrices:
15972 ;
15973 finish_scroll_bars:
15974
15975 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15976 {
15977 /* Set the thumb's position and size. */
15978 set_vertical_scroll_bar (w);
15979
15980 /* Note that we actually used the scroll bar attached to this
15981 window, so it shouldn't be deleted at the end of redisplay. */
15982 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15983 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15984 }
15985
15986 /* Restore current_buffer and value of point in it. The window
15987 update may have changed the buffer, so first make sure `opoint'
15988 is still valid (Bug#6177). */
15989 if (CHARPOS (opoint) < BEGV)
15990 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15991 else if (CHARPOS (opoint) > ZV)
15992 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15993 else
15994 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15995
15996 set_buffer_internal_1 (old);
15997 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15998 shorter. This can be caused by log truncation in *Messages*. */
15999 if (CHARPOS (lpoint) <= ZV)
16000 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16001
16002 unbind_to (count, Qnil);
16003 }
16004
16005
16006 /* Build the complete desired matrix of WINDOW with a window start
16007 buffer position POS.
16008
16009 Value is 1 if successful. It is zero if fonts were loaded during
16010 redisplay which makes re-adjusting glyph matrices necessary, and -1
16011 if point would appear in the scroll margins.
16012 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16013 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16014 set in FLAGS.) */
16015
16016 int
16017 try_window (Lisp_Object window, struct text_pos pos, int flags)
16018 {
16019 struct window *w = XWINDOW (window);
16020 struct it it;
16021 struct glyph_row *last_text_row = NULL;
16022 struct frame *f = XFRAME (w->frame);
16023
16024 /* Make POS the new window start. */
16025 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16026
16027 /* Mark cursor position as unknown. No overlay arrow seen. */
16028 w->cursor.vpos = -1;
16029 overlay_arrow_seen = 0;
16030
16031 /* Initialize iterator and info to start at POS. */
16032 start_display (&it, w, pos);
16033
16034 /* Display all lines of W. */
16035 while (it.current_y < it.last_visible_y)
16036 {
16037 if (display_line (&it))
16038 last_text_row = it.glyph_row - 1;
16039 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16040 return 0;
16041 }
16042
16043 /* Don't let the cursor end in the scroll margins. */
16044 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16045 && !MINI_WINDOW_P (w))
16046 {
16047 int this_scroll_margin;
16048
16049 if (scroll_margin > 0)
16050 {
16051 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16052 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16053 }
16054 else
16055 this_scroll_margin = 0;
16056
16057 if ((w->cursor.y >= 0 /* not vscrolled */
16058 && w->cursor.y < this_scroll_margin
16059 && CHARPOS (pos) > BEGV
16060 && IT_CHARPOS (it) < ZV)
16061 /* rms: considering make_cursor_line_fully_visible_p here
16062 seems to give wrong results. We don't want to recenter
16063 when the last line is partly visible, we want to allow
16064 that case to be handled in the usual way. */
16065 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16066 {
16067 w->cursor.vpos = -1;
16068 clear_glyph_matrix (w->desired_matrix);
16069 return -1;
16070 }
16071 }
16072
16073 /* If bottom moved off end of frame, change mode line percentage. */
16074 if (XFASTINT (w->window_end_pos) <= 0
16075 && Z != IT_CHARPOS (it))
16076 w->update_mode_line = Qt;
16077
16078 /* Set window_end_pos to the offset of the last character displayed
16079 on the window from the end of current_buffer. Set
16080 window_end_vpos to its row number. */
16081 if (last_text_row)
16082 {
16083 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16084 w->window_end_bytepos
16085 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16086 w->window_end_pos
16087 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16088 w->window_end_vpos
16089 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16090 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16091 ->displays_text_p);
16092 }
16093 else
16094 {
16095 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16096 w->window_end_pos = make_number (Z - ZV);
16097 w->window_end_vpos = make_number (0);
16098 }
16099
16100 /* But that is not valid info until redisplay finishes. */
16101 w->window_end_valid = Qnil;
16102 return 1;
16103 }
16104
16105
16106 \f
16107 /************************************************************************
16108 Window redisplay reusing current matrix when buffer has not changed
16109 ************************************************************************/
16110
16111 /* Try redisplay of window W showing an unchanged buffer with a
16112 different window start than the last time it was displayed by
16113 reusing its current matrix. Value is non-zero if successful.
16114 W->start is the new window start. */
16115
16116 static int
16117 try_window_reusing_current_matrix (struct window *w)
16118 {
16119 struct frame *f = XFRAME (w->frame);
16120 struct glyph_row *bottom_row;
16121 struct it it;
16122 struct run run;
16123 struct text_pos start, new_start;
16124 int nrows_scrolled, i;
16125 struct glyph_row *last_text_row;
16126 struct glyph_row *last_reused_text_row;
16127 struct glyph_row *start_row;
16128 int start_vpos, min_y, max_y;
16129
16130 #if GLYPH_DEBUG
16131 if (inhibit_try_window_reusing)
16132 return 0;
16133 #endif
16134
16135 if (/* This function doesn't handle terminal frames. */
16136 !FRAME_WINDOW_P (f)
16137 /* Don't try to reuse the display if windows have been split
16138 or such. */
16139 || windows_or_buffers_changed
16140 || cursor_type_changed)
16141 return 0;
16142
16143 /* Can't do this if region may have changed. */
16144 if ((!NILP (Vtransient_mark_mode)
16145 && !NILP (BVAR (current_buffer, mark_active)))
16146 || !NILP (w->region_showing)
16147 || !NILP (Vshow_trailing_whitespace))
16148 return 0;
16149
16150 /* If top-line visibility has changed, give up. */
16151 if (WINDOW_WANTS_HEADER_LINE_P (w)
16152 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16153 return 0;
16154
16155 /* Give up if old or new display is scrolled vertically. We could
16156 make this function handle this, but right now it doesn't. */
16157 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16158 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16159 return 0;
16160
16161 /* The variable new_start now holds the new window start. The old
16162 start `start' can be determined from the current matrix. */
16163 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16164 start = start_row->minpos;
16165 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16166
16167 /* Clear the desired matrix for the display below. */
16168 clear_glyph_matrix (w->desired_matrix);
16169
16170 if (CHARPOS (new_start) <= CHARPOS (start))
16171 {
16172 /* Don't use this method if the display starts with an ellipsis
16173 displayed for invisible text. It's not easy to handle that case
16174 below, and it's certainly not worth the effort since this is
16175 not a frequent case. */
16176 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16177 return 0;
16178
16179 IF_DEBUG (debug_method_add (w, "twu1"));
16180
16181 /* Display up to a row that can be reused. The variable
16182 last_text_row is set to the last row displayed that displays
16183 text. Note that it.vpos == 0 if or if not there is a
16184 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16185 start_display (&it, w, new_start);
16186 w->cursor.vpos = -1;
16187 last_text_row = last_reused_text_row = NULL;
16188
16189 while (it.current_y < it.last_visible_y
16190 && !fonts_changed_p)
16191 {
16192 /* If we have reached into the characters in the START row,
16193 that means the line boundaries have changed. So we
16194 can't start copying with the row START. Maybe it will
16195 work to start copying with the following row. */
16196 while (IT_CHARPOS (it) > CHARPOS (start))
16197 {
16198 /* Advance to the next row as the "start". */
16199 start_row++;
16200 start = start_row->minpos;
16201 /* If there are no more rows to try, or just one, give up. */
16202 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16203 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16204 || CHARPOS (start) == ZV)
16205 {
16206 clear_glyph_matrix (w->desired_matrix);
16207 return 0;
16208 }
16209
16210 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16211 }
16212 /* If we have reached alignment, we can copy the rest of the
16213 rows. */
16214 if (IT_CHARPOS (it) == CHARPOS (start)
16215 /* Don't accept "alignment" inside a display vector,
16216 since start_row could have started in the middle of
16217 that same display vector (thus their character
16218 positions match), and we have no way of telling if
16219 that is the case. */
16220 && it.current.dpvec_index < 0)
16221 break;
16222
16223 if (display_line (&it))
16224 last_text_row = it.glyph_row - 1;
16225
16226 }
16227
16228 /* A value of current_y < last_visible_y means that we stopped
16229 at the previous window start, which in turn means that we
16230 have at least one reusable row. */
16231 if (it.current_y < it.last_visible_y)
16232 {
16233 struct glyph_row *row;
16234
16235 /* IT.vpos always starts from 0; it counts text lines. */
16236 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16237
16238 /* Find PT if not already found in the lines displayed. */
16239 if (w->cursor.vpos < 0)
16240 {
16241 int dy = it.current_y - start_row->y;
16242
16243 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16244 row = row_containing_pos (w, PT, row, NULL, dy);
16245 if (row)
16246 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16247 dy, nrows_scrolled);
16248 else
16249 {
16250 clear_glyph_matrix (w->desired_matrix);
16251 return 0;
16252 }
16253 }
16254
16255 /* Scroll the display. Do it before the current matrix is
16256 changed. The problem here is that update has not yet
16257 run, i.e. part of the current matrix is not up to date.
16258 scroll_run_hook will clear the cursor, and use the
16259 current matrix to get the height of the row the cursor is
16260 in. */
16261 run.current_y = start_row->y;
16262 run.desired_y = it.current_y;
16263 run.height = it.last_visible_y - it.current_y;
16264
16265 if (run.height > 0 && run.current_y != run.desired_y)
16266 {
16267 update_begin (f);
16268 FRAME_RIF (f)->update_window_begin_hook (w);
16269 FRAME_RIF (f)->clear_window_mouse_face (w);
16270 FRAME_RIF (f)->scroll_run_hook (w, &run);
16271 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16272 update_end (f);
16273 }
16274
16275 /* Shift current matrix down by nrows_scrolled lines. */
16276 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16277 rotate_matrix (w->current_matrix,
16278 start_vpos,
16279 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16280 nrows_scrolled);
16281
16282 /* Disable lines that must be updated. */
16283 for (i = 0; i < nrows_scrolled; ++i)
16284 (start_row + i)->enabled_p = 0;
16285
16286 /* Re-compute Y positions. */
16287 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16288 max_y = it.last_visible_y;
16289 for (row = start_row + nrows_scrolled;
16290 row < bottom_row;
16291 ++row)
16292 {
16293 row->y = it.current_y;
16294 row->visible_height = row->height;
16295
16296 if (row->y < min_y)
16297 row->visible_height -= min_y - row->y;
16298 if (row->y + row->height > max_y)
16299 row->visible_height -= row->y + row->height - max_y;
16300 if (row->fringe_bitmap_periodic_p)
16301 row->redraw_fringe_bitmaps_p = 1;
16302
16303 it.current_y += row->height;
16304
16305 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16306 last_reused_text_row = row;
16307 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16308 break;
16309 }
16310
16311 /* Disable lines in the current matrix which are now
16312 below the window. */
16313 for (++row; row < bottom_row; ++row)
16314 row->enabled_p = row->mode_line_p = 0;
16315 }
16316
16317 /* Update window_end_pos etc.; last_reused_text_row is the last
16318 reused row from the current matrix containing text, if any.
16319 The value of last_text_row is the last displayed line
16320 containing text. */
16321 if (last_reused_text_row)
16322 {
16323 w->window_end_bytepos
16324 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16325 w->window_end_pos
16326 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16327 w->window_end_vpos
16328 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16329 w->current_matrix));
16330 }
16331 else if (last_text_row)
16332 {
16333 w->window_end_bytepos
16334 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16335 w->window_end_pos
16336 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16337 w->window_end_vpos
16338 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16339 }
16340 else
16341 {
16342 /* This window must be completely empty. */
16343 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16344 w->window_end_pos = make_number (Z - ZV);
16345 w->window_end_vpos = make_number (0);
16346 }
16347 w->window_end_valid = Qnil;
16348
16349 /* Update hint: don't try scrolling again in update_window. */
16350 w->desired_matrix->no_scrolling_p = 1;
16351
16352 #if GLYPH_DEBUG
16353 debug_method_add (w, "try_window_reusing_current_matrix 1");
16354 #endif
16355 return 1;
16356 }
16357 else if (CHARPOS (new_start) > CHARPOS (start))
16358 {
16359 struct glyph_row *pt_row, *row;
16360 struct glyph_row *first_reusable_row;
16361 struct glyph_row *first_row_to_display;
16362 int dy;
16363 int yb = window_text_bottom_y (w);
16364
16365 /* Find the row starting at new_start, if there is one. Don't
16366 reuse a partially visible line at the end. */
16367 first_reusable_row = start_row;
16368 while (first_reusable_row->enabled_p
16369 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16370 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16371 < CHARPOS (new_start)))
16372 ++first_reusable_row;
16373
16374 /* Give up if there is no row to reuse. */
16375 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16376 || !first_reusable_row->enabled_p
16377 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16378 != CHARPOS (new_start)))
16379 return 0;
16380
16381 /* We can reuse fully visible rows beginning with
16382 first_reusable_row to the end of the window. Set
16383 first_row_to_display to the first row that cannot be reused.
16384 Set pt_row to the row containing point, if there is any. */
16385 pt_row = NULL;
16386 for (first_row_to_display = first_reusable_row;
16387 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16388 ++first_row_to_display)
16389 {
16390 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16391 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16392 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16393 && first_row_to_display->ends_at_zv_p
16394 && pt_row == NULL)))
16395 pt_row = first_row_to_display;
16396 }
16397
16398 /* Start displaying at the start of first_row_to_display. */
16399 xassert (first_row_to_display->y < yb);
16400 init_to_row_start (&it, w, first_row_to_display);
16401
16402 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16403 - start_vpos);
16404 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16405 - nrows_scrolled);
16406 it.current_y = (first_row_to_display->y - first_reusable_row->y
16407 + WINDOW_HEADER_LINE_HEIGHT (w));
16408
16409 /* Display lines beginning with first_row_to_display in the
16410 desired matrix. Set last_text_row to the last row displayed
16411 that displays text. */
16412 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16413 if (pt_row == NULL)
16414 w->cursor.vpos = -1;
16415 last_text_row = NULL;
16416 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16417 if (display_line (&it))
16418 last_text_row = it.glyph_row - 1;
16419
16420 /* If point is in a reused row, adjust y and vpos of the cursor
16421 position. */
16422 if (pt_row)
16423 {
16424 w->cursor.vpos -= nrows_scrolled;
16425 w->cursor.y -= first_reusable_row->y - start_row->y;
16426 }
16427
16428 /* Give up if point isn't in a row displayed or reused. (This
16429 also handles the case where w->cursor.vpos < nrows_scrolled
16430 after the calls to display_line, which can happen with scroll
16431 margins. See bug#1295.) */
16432 if (w->cursor.vpos < 0)
16433 {
16434 clear_glyph_matrix (w->desired_matrix);
16435 return 0;
16436 }
16437
16438 /* Scroll the display. */
16439 run.current_y = first_reusable_row->y;
16440 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16441 run.height = it.last_visible_y - run.current_y;
16442 dy = run.current_y - run.desired_y;
16443
16444 if (run.height)
16445 {
16446 update_begin (f);
16447 FRAME_RIF (f)->update_window_begin_hook (w);
16448 FRAME_RIF (f)->clear_window_mouse_face (w);
16449 FRAME_RIF (f)->scroll_run_hook (w, &run);
16450 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16451 update_end (f);
16452 }
16453
16454 /* Adjust Y positions of reused rows. */
16455 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16456 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16457 max_y = it.last_visible_y;
16458 for (row = first_reusable_row; row < first_row_to_display; ++row)
16459 {
16460 row->y -= dy;
16461 row->visible_height = row->height;
16462 if (row->y < min_y)
16463 row->visible_height -= min_y - row->y;
16464 if (row->y + row->height > max_y)
16465 row->visible_height -= row->y + row->height - max_y;
16466 if (row->fringe_bitmap_periodic_p)
16467 row->redraw_fringe_bitmaps_p = 1;
16468 }
16469
16470 /* Scroll the current matrix. */
16471 xassert (nrows_scrolled > 0);
16472 rotate_matrix (w->current_matrix,
16473 start_vpos,
16474 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16475 -nrows_scrolled);
16476
16477 /* Disable rows not reused. */
16478 for (row -= nrows_scrolled; row < bottom_row; ++row)
16479 row->enabled_p = 0;
16480
16481 /* Point may have moved to a different line, so we cannot assume that
16482 the previous cursor position is valid; locate the correct row. */
16483 if (pt_row)
16484 {
16485 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16486 row < bottom_row
16487 && PT >= MATRIX_ROW_END_CHARPOS (row)
16488 && !row->ends_at_zv_p;
16489 row++)
16490 {
16491 w->cursor.vpos++;
16492 w->cursor.y = row->y;
16493 }
16494 if (row < bottom_row)
16495 {
16496 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16497 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16498
16499 /* Can't use this optimization with bidi-reordered glyph
16500 rows, unless cursor is already at point. */
16501 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16502 {
16503 if (!(w->cursor.hpos >= 0
16504 && w->cursor.hpos < row->used[TEXT_AREA]
16505 && BUFFERP (glyph->object)
16506 && glyph->charpos == PT))
16507 return 0;
16508 }
16509 else
16510 for (; glyph < end
16511 && (!BUFFERP (glyph->object)
16512 || glyph->charpos < PT);
16513 glyph++)
16514 {
16515 w->cursor.hpos++;
16516 w->cursor.x += glyph->pixel_width;
16517 }
16518 }
16519 }
16520
16521 /* Adjust window end. A null value of last_text_row means that
16522 the window end is in reused rows which in turn means that
16523 only its vpos can have changed. */
16524 if (last_text_row)
16525 {
16526 w->window_end_bytepos
16527 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16528 w->window_end_pos
16529 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16530 w->window_end_vpos
16531 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16532 }
16533 else
16534 {
16535 w->window_end_vpos
16536 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16537 }
16538
16539 w->window_end_valid = Qnil;
16540 w->desired_matrix->no_scrolling_p = 1;
16541
16542 #if GLYPH_DEBUG
16543 debug_method_add (w, "try_window_reusing_current_matrix 2");
16544 #endif
16545 return 1;
16546 }
16547
16548 return 0;
16549 }
16550
16551
16552 \f
16553 /************************************************************************
16554 Window redisplay reusing current matrix when buffer has changed
16555 ************************************************************************/
16556
16557 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16558 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16559 ptrdiff_t *, ptrdiff_t *);
16560 static struct glyph_row *
16561 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16562 struct glyph_row *);
16563
16564
16565 /* Return the last row in MATRIX displaying text. If row START is
16566 non-null, start searching with that row. IT gives the dimensions
16567 of the display. Value is null if matrix is empty; otherwise it is
16568 a pointer to the row found. */
16569
16570 static struct glyph_row *
16571 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16572 struct glyph_row *start)
16573 {
16574 struct glyph_row *row, *row_found;
16575
16576 /* Set row_found to the last row in IT->w's current matrix
16577 displaying text. The loop looks funny but think of partially
16578 visible lines. */
16579 row_found = NULL;
16580 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16581 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16582 {
16583 xassert (row->enabled_p);
16584 row_found = row;
16585 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16586 break;
16587 ++row;
16588 }
16589
16590 return row_found;
16591 }
16592
16593
16594 /* Return the last row in the current matrix of W that is not affected
16595 by changes at the start of current_buffer that occurred since W's
16596 current matrix was built. Value is null if no such row exists.
16597
16598 BEG_UNCHANGED us the number of characters unchanged at the start of
16599 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16600 first changed character in current_buffer. Characters at positions <
16601 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16602 when the current matrix was built. */
16603
16604 static struct glyph_row *
16605 find_last_unchanged_at_beg_row (struct window *w)
16606 {
16607 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16608 struct glyph_row *row;
16609 struct glyph_row *row_found = NULL;
16610 int yb = window_text_bottom_y (w);
16611
16612 /* Find the last row displaying unchanged text. */
16613 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16614 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16615 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16616 ++row)
16617 {
16618 if (/* If row ends before first_changed_pos, it is unchanged,
16619 except in some case. */
16620 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16621 /* When row ends in ZV and we write at ZV it is not
16622 unchanged. */
16623 && !row->ends_at_zv_p
16624 /* When first_changed_pos is the end of a continued line,
16625 row is not unchanged because it may be no longer
16626 continued. */
16627 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16628 && (row->continued_p
16629 || row->exact_window_width_line_p))
16630 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16631 needs to be recomputed, so don't consider this row as
16632 unchanged. This happens when the last line was
16633 bidi-reordered and was killed immediately before this
16634 redisplay cycle. In that case, ROW->end stores the
16635 buffer position of the first visual-order character of
16636 the killed text, which is now beyond ZV. */
16637 && CHARPOS (row->end.pos) <= ZV)
16638 row_found = row;
16639
16640 /* Stop if last visible row. */
16641 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16642 break;
16643 }
16644
16645 return row_found;
16646 }
16647
16648
16649 /* Find the first glyph row in the current matrix of W that is not
16650 affected by changes at the end of current_buffer since the
16651 time W's current matrix was built.
16652
16653 Return in *DELTA the number of chars by which buffer positions in
16654 unchanged text at the end of current_buffer must be adjusted.
16655
16656 Return in *DELTA_BYTES the corresponding number of bytes.
16657
16658 Value is null if no such row exists, i.e. all rows are affected by
16659 changes. */
16660
16661 static struct glyph_row *
16662 find_first_unchanged_at_end_row (struct window *w,
16663 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16664 {
16665 struct glyph_row *row;
16666 struct glyph_row *row_found = NULL;
16667
16668 *delta = *delta_bytes = 0;
16669
16670 /* Display must not have been paused, otherwise the current matrix
16671 is not up to date. */
16672 eassert (!NILP (w->window_end_valid));
16673
16674 /* A value of window_end_pos >= END_UNCHANGED means that the window
16675 end is in the range of changed text. If so, there is no
16676 unchanged row at the end of W's current matrix. */
16677 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16678 return NULL;
16679
16680 /* Set row to the last row in W's current matrix displaying text. */
16681 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16682
16683 /* If matrix is entirely empty, no unchanged row exists. */
16684 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16685 {
16686 /* The value of row is the last glyph row in the matrix having a
16687 meaningful buffer position in it. The end position of row
16688 corresponds to window_end_pos. This allows us to translate
16689 buffer positions in the current matrix to current buffer
16690 positions for characters not in changed text. */
16691 ptrdiff_t Z_old =
16692 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16693 ptrdiff_t Z_BYTE_old =
16694 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16695 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16696 struct glyph_row *first_text_row
16697 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16698
16699 *delta = Z - Z_old;
16700 *delta_bytes = Z_BYTE - Z_BYTE_old;
16701
16702 /* Set last_unchanged_pos to the buffer position of the last
16703 character in the buffer that has not been changed. Z is the
16704 index + 1 of the last character in current_buffer, i.e. by
16705 subtracting END_UNCHANGED we get the index of the last
16706 unchanged character, and we have to add BEG to get its buffer
16707 position. */
16708 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16709 last_unchanged_pos_old = last_unchanged_pos - *delta;
16710
16711 /* Search backward from ROW for a row displaying a line that
16712 starts at a minimum position >= last_unchanged_pos_old. */
16713 for (; row > first_text_row; --row)
16714 {
16715 /* This used to abort, but it can happen.
16716 It is ok to just stop the search instead here. KFS. */
16717 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16718 break;
16719
16720 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16721 row_found = row;
16722 }
16723 }
16724
16725 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16726
16727 return row_found;
16728 }
16729
16730
16731 /* Make sure that glyph rows in the current matrix of window W
16732 reference the same glyph memory as corresponding rows in the
16733 frame's frame matrix. This function is called after scrolling W's
16734 current matrix on a terminal frame in try_window_id and
16735 try_window_reusing_current_matrix. */
16736
16737 static void
16738 sync_frame_with_window_matrix_rows (struct window *w)
16739 {
16740 struct frame *f = XFRAME (w->frame);
16741 struct glyph_row *window_row, *window_row_end, *frame_row;
16742
16743 /* Preconditions: W must be a leaf window and full-width. Its frame
16744 must have a frame matrix. */
16745 xassert (NILP (w->hchild) && NILP (w->vchild));
16746 xassert (WINDOW_FULL_WIDTH_P (w));
16747 xassert (!FRAME_WINDOW_P (f));
16748
16749 /* If W is a full-width window, glyph pointers in W's current matrix
16750 have, by definition, to be the same as glyph pointers in the
16751 corresponding frame matrix. Note that frame matrices have no
16752 marginal areas (see build_frame_matrix). */
16753 window_row = w->current_matrix->rows;
16754 window_row_end = window_row + w->current_matrix->nrows;
16755 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16756 while (window_row < window_row_end)
16757 {
16758 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16759 struct glyph *end = window_row->glyphs[LAST_AREA];
16760
16761 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16762 frame_row->glyphs[TEXT_AREA] = start;
16763 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16764 frame_row->glyphs[LAST_AREA] = end;
16765
16766 /* Disable frame rows whose corresponding window rows have
16767 been disabled in try_window_id. */
16768 if (!window_row->enabled_p)
16769 frame_row->enabled_p = 0;
16770
16771 ++window_row, ++frame_row;
16772 }
16773 }
16774
16775
16776 /* Find the glyph row in window W containing CHARPOS. Consider all
16777 rows between START and END (not inclusive). END null means search
16778 all rows to the end of the display area of W. Value is the row
16779 containing CHARPOS or null. */
16780
16781 struct glyph_row *
16782 row_containing_pos (struct window *w, ptrdiff_t charpos,
16783 struct glyph_row *start, struct glyph_row *end, int dy)
16784 {
16785 struct glyph_row *row = start;
16786 struct glyph_row *best_row = NULL;
16787 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16788 int last_y;
16789
16790 /* If we happen to start on a header-line, skip that. */
16791 if (row->mode_line_p)
16792 ++row;
16793
16794 if ((end && row >= end) || !row->enabled_p)
16795 return NULL;
16796
16797 last_y = window_text_bottom_y (w) - dy;
16798
16799 while (1)
16800 {
16801 /* Give up if we have gone too far. */
16802 if (end && row >= end)
16803 return NULL;
16804 /* This formerly returned if they were equal.
16805 I think that both quantities are of a "last plus one" type;
16806 if so, when they are equal, the row is within the screen. -- rms. */
16807 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16808 return NULL;
16809
16810 /* If it is in this row, return this row. */
16811 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16812 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16813 /* The end position of a row equals the start
16814 position of the next row. If CHARPOS is there, we
16815 would rather display it in the next line, except
16816 when this line ends in ZV. */
16817 && !row->ends_at_zv_p
16818 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16819 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16820 {
16821 struct glyph *g;
16822
16823 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16824 || (!best_row && !row->continued_p))
16825 return row;
16826 /* In bidi-reordered rows, there could be several rows
16827 occluding point, all of them belonging to the same
16828 continued line. We need to find the row which fits
16829 CHARPOS the best. */
16830 for (g = row->glyphs[TEXT_AREA];
16831 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16832 g++)
16833 {
16834 if (!STRINGP (g->object))
16835 {
16836 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16837 {
16838 mindif = eabs (g->charpos - charpos);
16839 best_row = row;
16840 /* Exact match always wins. */
16841 if (mindif == 0)
16842 return best_row;
16843 }
16844 }
16845 }
16846 }
16847 else if (best_row && !row->continued_p)
16848 return best_row;
16849 ++row;
16850 }
16851 }
16852
16853
16854 /* Try to redisplay window W by reusing its existing display. W's
16855 current matrix must be up to date when this function is called,
16856 i.e. window_end_valid must not be nil.
16857
16858 Value is
16859
16860 1 if display has been updated
16861 0 if otherwise unsuccessful
16862 -1 if redisplay with same window start is known not to succeed
16863
16864 The following steps are performed:
16865
16866 1. Find the last row in the current matrix of W that is not
16867 affected by changes at the start of current_buffer. If no such row
16868 is found, give up.
16869
16870 2. Find the first row in W's current matrix that is not affected by
16871 changes at the end of current_buffer. Maybe there is no such row.
16872
16873 3. Display lines beginning with the row + 1 found in step 1 to the
16874 row found in step 2 or, if step 2 didn't find a row, to the end of
16875 the window.
16876
16877 4. If cursor is not known to appear on the window, give up.
16878
16879 5. If display stopped at the row found in step 2, scroll the
16880 display and current matrix as needed.
16881
16882 6. Maybe display some lines at the end of W, if we must. This can
16883 happen under various circumstances, like a partially visible line
16884 becoming fully visible, or because newly displayed lines are displayed
16885 in smaller font sizes.
16886
16887 7. Update W's window end information. */
16888
16889 static int
16890 try_window_id (struct window *w)
16891 {
16892 struct frame *f = XFRAME (w->frame);
16893 struct glyph_matrix *current_matrix = w->current_matrix;
16894 struct glyph_matrix *desired_matrix = w->desired_matrix;
16895 struct glyph_row *last_unchanged_at_beg_row;
16896 struct glyph_row *first_unchanged_at_end_row;
16897 struct glyph_row *row;
16898 struct glyph_row *bottom_row;
16899 int bottom_vpos;
16900 struct it it;
16901 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16902 int dvpos, dy;
16903 struct text_pos start_pos;
16904 struct run run;
16905 int first_unchanged_at_end_vpos = 0;
16906 struct glyph_row *last_text_row, *last_text_row_at_end;
16907 struct text_pos start;
16908 ptrdiff_t first_changed_charpos, last_changed_charpos;
16909
16910 #if GLYPH_DEBUG
16911 if (inhibit_try_window_id)
16912 return 0;
16913 #endif
16914
16915 /* This is handy for debugging. */
16916 #if 0
16917 #define GIVE_UP(X) \
16918 do { \
16919 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16920 return 0; \
16921 } while (0)
16922 #else
16923 #define GIVE_UP(X) return 0
16924 #endif
16925
16926 SET_TEXT_POS_FROM_MARKER (start, w->start);
16927
16928 /* Don't use this for mini-windows because these can show
16929 messages and mini-buffers, and we don't handle that here. */
16930 if (MINI_WINDOW_P (w))
16931 GIVE_UP (1);
16932
16933 /* This flag is used to prevent redisplay optimizations. */
16934 if (windows_or_buffers_changed || cursor_type_changed)
16935 GIVE_UP (2);
16936
16937 /* Verify that narrowing has not changed.
16938 Also verify that we were not told to prevent redisplay optimizations.
16939 It would be nice to further
16940 reduce the number of cases where this prevents try_window_id. */
16941 if (current_buffer->clip_changed
16942 || current_buffer->prevent_redisplay_optimizations_p)
16943 GIVE_UP (3);
16944
16945 /* Window must either use window-based redisplay or be full width. */
16946 if (!FRAME_WINDOW_P (f)
16947 && (!FRAME_LINE_INS_DEL_OK (f)
16948 || !WINDOW_FULL_WIDTH_P (w)))
16949 GIVE_UP (4);
16950
16951 /* Give up if point is known NOT to appear in W. */
16952 if (PT < CHARPOS (start))
16953 GIVE_UP (5);
16954
16955 /* Another way to prevent redisplay optimizations. */
16956 if (XFASTINT (w->last_modified) == 0)
16957 GIVE_UP (6);
16958
16959 /* Verify that window is not hscrolled. */
16960 if (XFASTINT (w->hscroll) != 0)
16961 GIVE_UP (7);
16962
16963 /* Verify that display wasn't paused. */
16964 if (NILP (w->window_end_valid))
16965 GIVE_UP (8);
16966
16967 /* Can't use this if highlighting a region because a cursor movement
16968 will do more than just set the cursor. */
16969 if (!NILP (Vtransient_mark_mode)
16970 && !NILP (BVAR (current_buffer, mark_active)))
16971 GIVE_UP (9);
16972
16973 /* Likewise if highlighting trailing whitespace. */
16974 if (!NILP (Vshow_trailing_whitespace))
16975 GIVE_UP (11);
16976
16977 /* Likewise if showing a region. */
16978 if (!NILP (w->region_showing))
16979 GIVE_UP (10);
16980
16981 /* Can't use this if overlay arrow position and/or string have
16982 changed. */
16983 if (overlay_arrows_changed_p ())
16984 GIVE_UP (12);
16985
16986 /* When word-wrap is on, adding a space to the first word of a
16987 wrapped line can change the wrap position, altering the line
16988 above it. It might be worthwhile to handle this more
16989 intelligently, but for now just redisplay from scratch. */
16990 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16991 GIVE_UP (21);
16992
16993 /* Under bidi reordering, adding or deleting a character in the
16994 beginning of a paragraph, before the first strong directional
16995 character, can change the base direction of the paragraph (unless
16996 the buffer specifies a fixed paragraph direction), which will
16997 require to redisplay the whole paragraph. It might be worthwhile
16998 to find the paragraph limits and widen the range of redisplayed
16999 lines to that, but for now just give up this optimization and
17000 redisplay from scratch. */
17001 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17002 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17003 GIVE_UP (22);
17004
17005 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17006 only if buffer has really changed. The reason is that the gap is
17007 initially at Z for freshly visited files. The code below would
17008 set end_unchanged to 0 in that case. */
17009 if (MODIFF > SAVE_MODIFF
17010 /* This seems to happen sometimes after saving a buffer. */
17011 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17012 {
17013 if (GPT - BEG < BEG_UNCHANGED)
17014 BEG_UNCHANGED = GPT - BEG;
17015 if (Z - GPT < END_UNCHANGED)
17016 END_UNCHANGED = Z - GPT;
17017 }
17018
17019 /* The position of the first and last character that has been changed. */
17020 first_changed_charpos = BEG + BEG_UNCHANGED;
17021 last_changed_charpos = Z - END_UNCHANGED;
17022
17023 /* If window starts after a line end, and the last change is in
17024 front of that newline, then changes don't affect the display.
17025 This case happens with stealth-fontification. Note that although
17026 the display is unchanged, glyph positions in the matrix have to
17027 be adjusted, of course. */
17028 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17029 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17030 && ((last_changed_charpos < CHARPOS (start)
17031 && CHARPOS (start) == BEGV)
17032 || (last_changed_charpos < CHARPOS (start) - 1
17033 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17034 {
17035 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17036 struct glyph_row *r0;
17037
17038 /* Compute how many chars/bytes have been added to or removed
17039 from the buffer. */
17040 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17041 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17042 Z_delta = Z - Z_old;
17043 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17044
17045 /* Give up if PT is not in the window. Note that it already has
17046 been checked at the start of try_window_id that PT is not in
17047 front of the window start. */
17048 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17049 GIVE_UP (13);
17050
17051 /* If window start is unchanged, we can reuse the whole matrix
17052 as is, after adjusting glyph positions. No need to compute
17053 the window end again, since its offset from Z hasn't changed. */
17054 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17055 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17056 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17057 /* PT must not be in a partially visible line. */
17058 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17059 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17060 {
17061 /* Adjust positions in the glyph matrix. */
17062 if (Z_delta || Z_delta_bytes)
17063 {
17064 struct glyph_row *r1
17065 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17066 increment_matrix_positions (w->current_matrix,
17067 MATRIX_ROW_VPOS (r0, current_matrix),
17068 MATRIX_ROW_VPOS (r1, current_matrix),
17069 Z_delta, Z_delta_bytes);
17070 }
17071
17072 /* Set the cursor. */
17073 row = row_containing_pos (w, PT, r0, NULL, 0);
17074 if (row)
17075 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17076 else
17077 abort ();
17078 return 1;
17079 }
17080 }
17081
17082 /* Handle the case that changes are all below what is displayed in
17083 the window, and that PT is in the window. This shortcut cannot
17084 be taken if ZV is visible in the window, and text has been added
17085 there that is visible in the window. */
17086 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17087 /* ZV is not visible in the window, or there are no
17088 changes at ZV, actually. */
17089 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17090 || first_changed_charpos == last_changed_charpos))
17091 {
17092 struct glyph_row *r0;
17093
17094 /* Give up if PT is not in the window. Note that it already has
17095 been checked at the start of try_window_id that PT is not in
17096 front of the window start. */
17097 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17098 GIVE_UP (14);
17099
17100 /* If window start is unchanged, we can reuse the whole matrix
17101 as is, without changing glyph positions since no text has
17102 been added/removed in front of the window end. */
17103 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17104 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17105 /* PT must not be in a partially visible line. */
17106 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17107 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17108 {
17109 /* We have to compute the window end anew since text
17110 could have been added/removed after it. */
17111 w->window_end_pos
17112 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17113 w->window_end_bytepos
17114 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17115
17116 /* Set the cursor. */
17117 row = row_containing_pos (w, PT, r0, NULL, 0);
17118 if (row)
17119 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17120 else
17121 abort ();
17122 return 2;
17123 }
17124 }
17125
17126 /* Give up if window start is in the changed area.
17127
17128 The condition used to read
17129
17130 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17131
17132 but why that was tested escapes me at the moment. */
17133 if (CHARPOS (start) >= first_changed_charpos
17134 && CHARPOS (start) <= last_changed_charpos)
17135 GIVE_UP (15);
17136
17137 /* Check that window start agrees with the start of the first glyph
17138 row in its current matrix. Check this after we know the window
17139 start is not in changed text, otherwise positions would not be
17140 comparable. */
17141 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17142 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17143 GIVE_UP (16);
17144
17145 /* Give up if the window ends in strings. Overlay strings
17146 at the end are difficult to handle, so don't try. */
17147 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17148 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17149 GIVE_UP (20);
17150
17151 /* Compute the position at which we have to start displaying new
17152 lines. Some of the lines at the top of the window might be
17153 reusable because they are not displaying changed text. Find the
17154 last row in W's current matrix not affected by changes at the
17155 start of current_buffer. Value is null if changes start in the
17156 first line of window. */
17157 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17158 if (last_unchanged_at_beg_row)
17159 {
17160 /* Avoid starting to display in the middle of a character, a TAB
17161 for instance. This is easier than to set up the iterator
17162 exactly, and it's not a frequent case, so the additional
17163 effort wouldn't really pay off. */
17164 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17165 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17166 && last_unchanged_at_beg_row > w->current_matrix->rows)
17167 --last_unchanged_at_beg_row;
17168
17169 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17170 GIVE_UP (17);
17171
17172 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17173 GIVE_UP (18);
17174 start_pos = it.current.pos;
17175
17176 /* Start displaying new lines in the desired matrix at the same
17177 vpos we would use in the current matrix, i.e. below
17178 last_unchanged_at_beg_row. */
17179 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17180 current_matrix);
17181 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17182 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17183
17184 xassert (it.hpos == 0 && it.current_x == 0);
17185 }
17186 else
17187 {
17188 /* There are no reusable lines at the start of the window.
17189 Start displaying in the first text line. */
17190 start_display (&it, w, start);
17191 it.vpos = it.first_vpos;
17192 start_pos = it.current.pos;
17193 }
17194
17195 /* Find the first row that is not affected by changes at the end of
17196 the buffer. Value will be null if there is no unchanged row, in
17197 which case we must redisplay to the end of the window. delta
17198 will be set to the value by which buffer positions beginning with
17199 first_unchanged_at_end_row have to be adjusted due to text
17200 changes. */
17201 first_unchanged_at_end_row
17202 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17203 IF_DEBUG (debug_delta = delta);
17204 IF_DEBUG (debug_delta_bytes = delta_bytes);
17205
17206 /* Set stop_pos to the buffer position up to which we will have to
17207 display new lines. If first_unchanged_at_end_row != NULL, this
17208 is the buffer position of the start of the line displayed in that
17209 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17210 that we don't stop at a buffer position. */
17211 stop_pos = 0;
17212 if (first_unchanged_at_end_row)
17213 {
17214 xassert (last_unchanged_at_beg_row == NULL
17215 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17216
17217 /* If this is a continuation line, move forward to the next one
17218 that isn't. Changes in lines above affect this line.
17219 Caution: this may move first_unchanged_at_end_row to a row
17220 not displaying text. */
17221 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17222 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17223 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17224 < it.last_visible_y))
17225 ++first_unchanged_at_end_row;
17226
17227 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17228 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17229 >= it.last_visible_y))
17230 first_unchanged_at_end_row = NULL;
17231 else
17232 {
17233 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17234 + delta);
17235 first_unchanged_at_end_vpos
17236 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17237 xassert (stop_pos >= Z - END_UNCHANGED);
17238 }
17239 }
17240 else if (last_unchanged_at_beg_row == NULL)
17241 GIVE_UP (19);
17242
17243
17244 #if GLYPH_DEBUG
17245
17246 /* Either there is no unchanged row at the end, or the one we have
17247 now displays text. This is a necessary condition for the window
17248 end pos calculation at the end of this function. */
17249 xassert (first_unchanged_at_end_row == NULL
17250 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17251
17252 debug_last_unchanged_at_beg_vpos
17253 = (last_unchanged_at_beg_row
17254 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17255 : -1);
17256 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17257
17258 #endif /* GLYPH_DEBUG != 0 */
17259
17260
17261 /* Display new lines. Set last_text_row to the last new line
17262 displayed which has text on it, i.e. might end up as being the
17263 line where the window_end_vpos is. */
17264 w->cursor.vpos = -1;
17265 last_text_row = NULL;
17266 overlay_arrow_seen = 0;
17267 while (it.current_y < it.last_visible_y
17268 && !fonts_changed_p
17269 && (first_unchanged_at_end_row == NULL
17270 || IT_CHARPOS (it) < stop_pos))
17271 {
17272 if (display_line (&it))
17273 last_text_row = it.glyph_row - 1;
17274 }
17275
17276 if (fonts_changed_p)
17277 return -1;
17278
17279
17280 /* Compute differences in buffer positions, y-positions etc. for
17281 lines reused at the bottom of the window. Compute what we can
17282 scroll. */
17283 if (first_unchanged_at_end_row
17284 /* No lines reused because we displayed everything up to the
17285 bottom of the window. */
17286 && it.current_y < it.last_visible_y)
17287 {
17288 dvpos = (it.vpos
17289 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17290 current_matrix));
17291 dy = it.current_y - first_unchanged_at_end_row->y;
17292 run.current_y = first_unchanged_at_end_row->y;
17293 run.desired_y = run.current_y + dy;
17294 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17295 }
17296 else
17297 {
17298 delta = delta_bytes = dvpos = dy
17299 = run.current_y = run.desired_y = run.height = 0;
17300 first_unchanged_at_end_row = NULL;
17301 }
17302 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17303
17304
17305 /* Find the cursor if not already found. We have to decide whether
17306 PT will appear on this window (it sometimes doesn't, but this is
17307 not a very frequent case.) This decision has to be made before
17308 the current matrix is altered. A value of cursor.vpos < 0 means
17309 that PT is either in one of the lines beginning at
17310 first_unchanged_at_end_row or below the window. Don't care for
17311 lines that might be displayed later at the window end; as
17312 mentioned, this is not a frequent case. */
17313 if (w->cursor.vpos < 0)
17314 {
17315 /* Cursor in unchanged rows at the top? */
17316 if (PT < CHARPOS (start_pos)
17317 && last_unchanged_at_beg_row)
17318 {
17319 row = row_containing_pos (w, PT,
17320 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17321 last_unchanged_at_beg_row + 1, 0);
17322 if (row)
17323 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17324 }
17325
17326 /* Start from first_unchanged_at_end_row looking for PT. */
17327 else if (first_unchanged_at_end_row)
17328 {
17329 row = row_containing_pos (w, PT - delta,
17330 first_unchanged_at_end_row, NULL, 0);
17331 if (row)
17332 set_cursor_from_row (w, row, w->current_matrix, delta,
17333 delta_bytes, dy, dvpos);
17334 }
17335
17336 /* Give up if cursor was not found. */
17337 if (w->cursor.vpos < 0)
17338 {
17339 clear_glyph_matrix (w->desired_matrix);
17340 return -1;
17341 }
17342 }
17343
17344 /* Don't let the cursor end in the scroll margins. */
17345 {
17346 int this_scroll_margin, cursor_height;
17347
17348 this_scroll_margin =
17349 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17350 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17351 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17352
17353 if ((w->cursor.y < this_scroll_margin
17354 && CHARPOS (start) > BEGV)
17355 /* Old redisplay didn't take scroll margin into account at the bottom,
17356 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17357 || (w->cursor.y + (make_cursor_line_fully_visible_p
17358 ? cursor_height + this_scroll_margin
17359 : 1)) > it.last_visible_y)
17360 {
17361 w->cursor.vpos = -1;
17362 clear_glyph_matrix (w->desired_matrix);
17363 return -1;
17364 }
17365 }
17366
17367 /* Scroll the display. Do it before changing the current matrix so
17368 that xterm.c doesn't get confused about where the cursor glyph is
17369 found. */
17370 if (dy && run.height)
17371 {
17372 update_begin (f);
17373
17374 if (FRAME_WINDOW_P (f))
17375 {
17376 FRAME_RIF (f)->update_window_begin_hook (w);
17377 FRAME_RIF (f)->clear_window_mouse_face (w);
17378 FRAME_RIF (f)->scroll_run_hook (w, &run);
17379 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17380 }
17381 else
17382 {
17383 /* Terminal frame. In this case, dvpos gives the number of
17384 lines to scroll by; dvpos < 0 means scroll up. */
17385 int from_vpos
17386 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17387 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17388 int end = (WINDOW_TOP_EDGE_LINE (w)
17389 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17390 + window_internal_height (w));
17391
17392 #if defined (HAVE_GPM) || defined (MSDOS)
17393 x_clear_window_mouse_face (w);
17394 #endif
17395 /* Perform the operation on the screen. */
17396 if (dvpos > 0)
17397 {
17398 /* Scroll last_unchanged_at_beg_row to the end of the
17399 window down dvpos lines. */
17400 set_terminal_window (f, end);
17401
17402 /* On dumb terminals delete dvpos lines at the end
17403 before inserting dvpos empty lines. */
17404 if (!FRAME_SCROLL_REGION_OK (f))
17405 ins_del_lines (f, end - dvpos, -dvpos);
17406
17407 /* Insert dvpos empty lines in front of
17408 last_unchanged_at_beg_row. */
17409 ins_del_lines (f, from, dvpos);
17410 }
17411 else if (dvpos < 0)
17412 {
17413 /* Scroll up last_unchanged_at_beg_vpos to the end of
17414 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17415 set_terminal_window (f, end);
17416
17417 /* Delete dvpos lines in front of
17418 last_unchanged_at_beg_vpos. ins_del_lines will set
17419 the cursor to the given vpos and emit |dvpos| delete
17420 line sequences. */
17421 ins_del_lines (f, from + dvpos, dvpos);
17422
17423 /* On a dumb terminal insert dvpos empty lines at the
17424 end. */
17425 if (!FRAME_SCROLL_REGION_OK (f))
17426 ins_del_lines (f, end + dvpos, -dvpos);
17427 }
17428
17429 set_terminal_window (f, 0);
17430 }
17431
17432 update_end (f);
17433 }
17434
17435 /* Shift reused rows of the current matrix to the right position.
17436 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17437 text. */
17438 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17439 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17440 if (dvpos < 0)
17441 {
17442 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17443 bottom_vpos, dvpos);
17444 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17445 bottom_vpos, 0);
17446 }
17447 else if (dvpos > 0)
17448 {
17449 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17450 bottom_vpos, dvpos);
17451 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17452 first_unchanged_at_end_vpos + dvpos, 0);
17453 }
17454
17455 /* For frame-based redisplay, make sure that current frame and window
17456 matrix are in sync with respect to glyph memory. */
17457 if (!FRAME_WINDOW_P (f))
17458 sync_frame_with_window_matrix_rows (w);
17459
17460 /* Adjust buffer positions in reused rows. */
17461 if (delta || delta_bytes)
17462 increment_matrix_positions (current_matrix,
17463 first_unchanged_at_end_vpos + dvpos,
17464 bottom_vpos, delta, delta_bytes);
17465
17466 /* Adjust Y positions. */
17467 if (dy)
17468 shift_glyph_matrix (w, current_matrix,
17469 first_unchanged_at_end_vpos + dvpos,
17470 bottom_vpos, dy);
17471
17472 if (first_unchanged_at_end_row)
17473 {
17474 first_unchanged_at_end_row += dvpos;
17475 if (first_unchanged_at_end_row->y >= it.last_visible_y
17476 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17477 first_unchanged_at_end_row = NULL;
17478 }
17479
17480 /* If scrolling up, there may be some lines to display at the end of
17481 the window. */
17482 last_text_row_at_end = NULL;
17483 if (dy < 0)
17484 {
17485 /* Scrolling up can leave for example a partially visible line
17486 at the end of the window to be redisplayed. */
17487 /* Set last_row to the glyph row in the current matrix where the
17488 window end line is found. It has been moved up or down in
17489 the matrix by dvpos. */
17490 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17491 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17492
17493 /* If last_row is the window end line, it should display text. */
17494 xassert (last_row->displays_text_p);
17495
17496 /* If window end line was partially visible before, begin
17497 displaying at that line. Otherwise begin displaying with the
17498 line following it. */
17499 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17500 {
17501 init_to_row_start (&it, w, last_row);
17502 it.vpos = last_vpos;
17503 it.current_y = last_row->y;
17504 }
17505 else
17506 {
17507 init_to_row_end (&it, w, last_row);
17508 it.vpos = 1 + last_vpos;
17509 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17510 ++last_row;
17511 }
17512
17513 /* We may start in a continuation line. If so, we have to
17514 get the right continuation_lines_width and current_x. */
17515 it.continuation_lines_width = last_row->continuation_lines_width;
17516 it.hpos = it.current_x = 0;
17517
17518 /* Display the rest of the lines at the window end. */
17519 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17520 while (it.current_y < it.last_visible_y
17521 && !fonts_changed_p)
17522 {
17523 /* Is it always sure that the display agrees with lines in
17524 the current matrix? I don't think so, so we mark rows
17525 displayed invalid in the current matrix by setting their
17526 enabled_p flag to zero. */
17527 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17528 if (display_line (&it))
17529 last_text_row_at_end = it.glyph_row - 1;
17530 }
17531 }
17532
17533 /* Update window_end_pos and window_end_vpos. */
17534 if (first_unchanged_at_end_row
17535 && !last_text_row_at_end)
17536 {
17537 /* Window end line if one of the preserved rows from the current
17538 matrix. Set row to the last row displaying text in current
17539 matrix starting at first_unchanged_at_end_row, after
17540 scrolling. */
17541 xassert (first_unchanged_at_end_row->displays_text_p);
17542 row = find_last_row_displaying_text (w->current_matrix, &it,
17543 first_unchanged_at_end_row);
17544 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17545
17546 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17547 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17548 w->window_end_vpos
17549 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17550 xassert (w->window_end_bytepos >= 0);
17551 IF_DEBUG (debug_method_add (w, "A"));
17552 }
17553 else if (last_text_row_at_end)
17554 {
17555 w->window_end_pos
17556 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17557 w->window_end_bytepos
17558 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17559 w->window_end_vpos
17560 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17561 xassert (w->window_end_bytepos >= 0);
17562 IF_DEBUG (debug_method_add (w, "B"));
17563 }
17564 else if (last_text_row)
17565 {
17566 /* We have displayed either to the end of the window or at the
17567 end of the window, i.e. the last row with text is to be found
17568 in the desired matrix. */
17569 w->window_end_pos
17570 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17571 w->window_end_bytepos
17572 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17573 w->window_end_vpos
17574 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17575 xassert (w->window_end_bytepos >= 0);
17576 }
17577 else if (first_unchanged_at_end_row == NULL
17578 && last_text_row == NULL
17579 && last_text_row_at_end == NULL)
17580 {
17581 /* Displayed to end of window, but no line containing text was
17582 displayed. Lines were deleted at the end of the window. */
17583 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17584 int vpos = XFASTINT (w->window_end_vpos);
17585 struct glyph_row *current_row = current_matrix->rows + vpos;
17586 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17587
17588 for (row = NULL;
17589 row == NULL && vpos >= first_vpos;
17590 --vpos, --current_row, --desired_row)
17591 {
17592 if (desired_row->enabled_p)
17593 {
17594 if (desired_row->displays_text_p)
17595 row = desired_row;
17596 }
17597 else if (current_row->displays_text_p)
17598 row = current_row;
17599 }
17600
17601 xassert (row != NULL);
17602 w->window_end_vpos = make_number (vpos + 1);
17603 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17604 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17605 xassert (w->window_end_bytepos >= 0);
17606 IF_DEBUG (debug_method_add (w, "C"));
17607 }
17608 else
17609 abort ();
17610
17611 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17612 debug_end_vpos = XFASTINT (w->window_end_vpos));
17613
17614 /* Record that display has not been completed. */
17615 w->window_end_valid = Qnil;
17616 w->desired_matrix->no_scrolling_p = 1;
17617 return 3;
17618
17619 #undef GIVE_UP
17620 }
17621
17622
17623 \f
17624 /***********************************************************************
17625 More debugging support
17626 ***********************************************************************/
17627
17628 #if GLYPH_DEBUG
17629
17630 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17631 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17632 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17633
17634
17635 /* Dump the contents of glyph matrix MATRIX on stderr.
17636
17637 GLYPHS 0 means don't show glyph contents.
17638 GLYPHS 1 means show glyphs in short form
17639 GLYPHS > 1 means show glyphs in long form. */
17640
17641 void
17642 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17643 {
17644 int i;
17645 for (i = 0; i < matrix->nrows; ++i)
17646 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17647 }
17648
17649
17650 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17651 the glyph row and area where the glyph comes from. */
17652
17653 void
17654 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17655 {
17656 if (glyph->type == CHAR_GLYPH)
17657 {
17658 fprintf (stderr,
17659 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17660 glyph - row->glyphs[TEXT_AREA],
17661 'C',
17662 glyph->charpos,
17663 (BUFFERP (glyph->object)
17664 ? 'B'
17665 : (STRINGP (glyph->object)
17666 ? 'S'
17667 : '-')),
17668 glyph->pixel_width,
17669 glyph->u.ch,
17670 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17671 ? glyph->u.ch
17672 : '.'),
17673 glyph->face_id,
17674 glyph->left_box_line_p,
17675 glyph->right_box_line_p);
17676 }
17677 else if (glyph->type == STRETCH_GLYPH)
17678 {
17679 fprintf (stderr,
17680 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17681 glyph - row->glyphs[TEXT_AREA],
17682 'S',
17683 glyph->charpos,
17684 (BUFFERP (glyph->object)
17685 ? 'B'
17686 : (STRINGP (glyph->object)
17687 ? 'S'
17688 : '-')),
17689 glyph->pixel_width,
17690 0,
17691 '.',
17692 glyph->face_id,
17693 glyph->left_box_line_p,
17694 glyph->right_box_line_p);
17695 }
17696 else if (glyph->type == IMAGE_GLYPH)
17697 {
17698 fprintf (stderr,
17699 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17700 glyph - row->glyphs[TEXT_AREA],
17701 'I',
17702 glyph->charpos,
17703 (BUFFERP (glyph->object)
17704 ? 'B'
17705 : (STRINGP (glyph->object)
17706 ? 'S'
17707 : '-')),
17708 glyph->pixel_width,
17709 glyph->u.img_id,
17710 '.',
17711 glyph->face_id,
17712 glyph->left_box_line_p,
17713 glyph->right_box_line_p);
17714 }
17715 else if (glyph->type == COMPOSITE_GLYPH)
17716 {
17717 fprintf (stderr,
17718 " %5td %4c %6"pI"d %c %3d 0x%05x",
17719 glyph - row->glyphs[TEXT_AREA],
17720 '+',
17721 glyph->charpos,
17722 (BUFFERP (glyph->object)
17723 ? 'B'
17724 : (STRINGP (glyph->object)
17725 ? 'S'
17726 : '-')),
17727 glyph->pixel_width,
17728 glyph->u.cmp.id);
17729 if (glyph->u.cmp.automatic)
17730 fprintf (stderr,
17731 "[%d-%d]",
17732 glyph->slice.cmp.from, glyph->slice.cmp.to);
17733 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17734 glyph->face_id,
17735 glyph->left_box_line_p,
17736 glyph->right_box_line_p);
17737 }
17738 }
17739
17740
17741 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17742 GLYPHS 0 means don't show glyph contents.
17743 GLYPHS 1 means show glyphs in short form
17744 GLYPHS > 1 means show glyphs in long form. */
17745
17746 void
17747 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17748 {
17749 if (glyphs != 1)
17750 {
17751 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17752 fprintf (stderr, "======================================================================\n");
17753
17754 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17755 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17756 vpos,
17757 MATRIX_ROW_START_CHARPOS (row),
17758 MATRIX_ROW_END_CHARPOS (row),
17759 row->used[TEXT_AREA],
17760 row->contains_overlapping_glyphs_p,
17761 row->enabled_p,
17762 row->truncated_on_left_p,
17763 row->truncated_on_right_p,
17764 row->continued_p,
17765 MATRIX_ROW_CONTINUATION_LINE_P (row),
17766 row->displays_text_p,
17767 row->ends_at_zv_p,
17768 row->fill_line_p,
17769 row->ends_in_middle_of_char_p,
17770 row->starts_in_middle_of_char_p,
17771 row->mouse_face_p,
17772 row->x,
17773 row->y,
17774 row->pixel_width,
17775 row->height,
17776 row->visible_height,
17777 row->ascent,
17778 row->phys_ascent);
17779 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17780 row->end.overlay_string_index,
17781 row->continuation_lines_width);
17782 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17783 CHARPOS (row->start.string_pos),
17784 CHARPOS (row->end.string_pos));
17785 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17786 row->end.dpvec_index);
17787 }
17788
17789 if (glyphs > 1)
17790 {
17791 int area;
17792
17793 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17794 {
17795 struct glyph *glyph = row->glyphs[area];
17796 struct glyph *glyph_end = glyph + row->used[area];
17797
17798 /* Glyph for a line end in text. */
17799 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17800 ++glyph_end;
17801
17802 if (glyph < glyph_end)
17803 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17804
17805 for (; glyph < glyph_end; ++glyph)
17806 dump_glyph (row, glyph, area);
17807 }
17808 }
17809 else if (glyphs == 1)
17810 {
17811 int area;
17812
17813 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17814 {
17815 char *s = (char *) alloca (row->used[area] + 1);
17816 int i;
17817
17818 for (i = 0; i < row->used[area]; ++i)
17819 {
17820 struct glyph *glyph = row->glyphs[area] + i;
17821 if (glyph->type == CHAR_GLYPH
17822 && glyph->u.ch < 0x80
17823 && glyph->u.ch >= ' ')
17824 s[i] = glyph->u.ch;
17825 else
17826 s[i] = '.';
17827 }
17828
17829 s[i] = '\0';
17830 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17831 }
17832 }
17833 }
17834
17835
17836 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17837 Sdump_glyph_matrix, 0, 1, "p",
17838 doc: /* Dump the current matrix of the selected window to stderr.
17839 Shows contents of glyph row structures. With non-nil
17840 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17841 glyphs in short form, otherwise show glyphs in long form. */)
17842 (Lisp_Object glyphs)
17843 {
17844 struct window *w = XWINDOW (selected_window);
17845 struct buffer *buffer = XBUFFER (w->buffer);
17846
17847 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17848 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17849 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17850 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17851 fprintf (stderr, "=============================================\n");
17852 dump_glyph_matrix (w->current_matrix,
17853 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17854 return Qnil;
17855 }
17856
17857
17858 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17859 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17860 (void)
17861 {
17862 struct frame *f = XFRAME (selected_frame);
17863 dump_glyph_matrix (f->current_matrix, 1);
17864 return Qnil;
17865 }
17866
17867
17868 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17869 doc: /* Dump glyph row ROW to stderr.
17870 GLYPH 0 means don't dump glyphs.
17871 GLYPH 1 means dump glyphs in short form.
17872 GLYPH > 1 or omitted means dump glyphs in long form. */)
17873 (Lisp_Object row, Lisp_Object glyphs)
17874 {
17875 struct glyph_matrix *matrix;
17876 EMACS_INT vpos;
17877
17878 CHECK_NUMBER (row);
17879 matrix = XWINDOW (selected_window)->current_matrix;
17880 vpos = XINT (row);
17881 if (vpos >= 0 && vpos < matrix->nrows)
17882 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17883 vpos,
17884 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17885 return Qnil;
17886 }
17887
17888
17889 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17890 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17891 GLYPH 0 means don't dump glyphs.
17892 GLYPH 1 means dump glyphs in short form.
17893 GLYPH > 1 or omitted means dump glyphs in long form. */)
17894 (Lisp_Object row, Lisp_Object glyphs)
17895 {
17896 struct frame *sf = SELECTED_FRAME ();
17897 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17898 EMACS_INT vpos;
17899
17900 CHECK_NUMBER (row);
17901 vpos = XINT (row);
17902 if (vpos >= 0 && vpos < m->nrows)
17903 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17904 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17905 return Qnil;
17906 }
17907
17908
17909 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17910 doc: /* Toggle tracing of redisplay.
17911 With ARG, turn tracing on if and only if ARG is positive. */)
17912 (Lisp_Object arg)
17913 {
17914 if (NILP (arg))
17915 trace_redisplay_p = !trace_redisplay_p;
17916 else
17917 {
17918 arg = Fprefix_numeric_value (arg);
17919 trace_redisplay_p = XINT (arg) > 0;
17920 }
17921
17922 return Qnil;
17923 }
17924
17925
17926 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17927 doc: /* Like `format', but print result to stderr.
17928 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17929 (ptrdiff_t nargs, Lisp_Object *args)
17930 {
17931 Lisp_Object s = Fformat (nargs, args);
17932 fprintf (stderr, "%s", SDATA (s));
17933 return Qnil;
17934 }
17935
17936 #endif /* GLYPH_DEBUG */
17937
17938
17939 \f
17940 /***********************************************************************
17941 Building Desired Matrix Rows
17942 ***********************************************************************/
17943
17944 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17945 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17946
17947 static struct glyph_row *
17948 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17949 {
17950 struct frame *f = XFRAME (WINDOW_FRAME (w));
17951 struct buffer *buffer = XBUFFER (w->buffer);
17952 struct buffer *old = current_buffer;
17953 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17954 int arrow_len = SCHARS (overlay_arrow_string);
17955 const unsigned char *arrow_end = arrow_string + arrow_len;
17956 const unsigned char *p;
17957 struct it it;
17958 int multibyte_p;
17959 int n_glyphs_before;
17960
17961 set_buffer_temp (buffer);
17962 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17963 it.glyph_row->used[TEXT_AREA] = 0;
17964 SET_TEXT_POS (it.position, 0, 0);
17965
17966 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17967 p = arrow_string;
17968 while (p < arrow_end)
17969 {
17970 Lisp_Object face, ilisp;
17971
17972 /* Get the next character. */
17973 if (multibyte_p)
17974 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17975 else
17976 {
17977 it.c = it.char_to_display = *p, it.len = 1;
17978 if (! ASCII_CHAR_P (it.c))
17979 it.char_to_display = BYTE8_TO_CHAR (it.c);
17980 }
17981 p += it.len;
17982
17983 /* Get its face. */
17984 ilisp = make_number (p - arrow_string);
17985 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17986 it.face_id = compute_char_face (f, it.char_to_display, face);
17987
17988 /* Compute its width, get its glyphs. */
17989 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17990 SET_TEXT_POS (it.position, -1, -1);
17991 PRODUCE_GLYPHS (&it);
17992
17993 /* If this character doesn't fit any more in the line, we have
17994 to remove some glyphs. */
17995 if (it.current_x > it.last_visible_x)
17996 {
17997 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17998 break;
17999 }
18000 }
18001
18002 set_buffer_temp (old);
18003 return it.glyph_row;
18004 }
18005
18006
18007 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18008 glyphs are only inserted for terminal frames since we can't really
18009 win with truncation glyphs when partially visible glyphs are
18010 involved. Which glyphs to insert is determined by
18011 produce_special_glyphs. */
18012
18013 static void
18014 insert_left_trunc_glyphs (struct it *it)
18015 {
18016 struct it truncate_it;
18017 struct glyph *from, *end, *to, *toend;
18018
18019 xassert (!FRAME_WINDOW_P (it->f));
18020
18021 /* Get the truncation glyphs. */
18022 truncate_it = *it;
18023 truncate_it.current_x = 0;
18024 truncate_it.face_id = DEFAULT_FACE_ID;
18025 truncate_it.glyph_row = &scratch_glyph_row;
18026 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18027 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18028 truncate_it.object = make_number (0);
18029 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18030
18031 /* Overwrite glyphs from IT with truncation glyphs. */
18032 if (!it->glyph_row->reversed_p)
18033 {
18034 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18035 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18036 to = it->glyph_row->glyphs[TEXT_AREA];
18037 toend = to + it->glyph_row->used[TEXT_AREA];
18038
18039 while (from < end)
18040 *to++ = *from++;
18041
18042 /* There may be padding glyphs left over. Overwrite them too. */
18043 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18044 {
18045 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18046 while (from < end)
18047 *to++ = *from++;
18048 }
18049
18050 if (to > toend)
18051 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18052 }
18053 else
18054 {
18055 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18056 that back to front. */
18057 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18058 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18059 toend = it->glyph_row->glyphs[TEXT_AREA];
18060 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18061
18062 while (from >= end && to >= toend)
18063 *to-- = *from--;
18064 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18065 {
18066 from =
18067 truncate_it.glyph_row->glyphs[TEXT_AREA]
18068 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18069 while (from >= end && to >= toend)
18070 *to-- = *from--;
18071 }
18072 if (from >= end)
18073 {
18074 /* Need to free some room before prepending additional
18075 glyphs. */
18076 int move_by = from - end + 1;
18077 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18078 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18079
18080 for ( ; g >= g0; g--)
18081 g[move_by] = *g;
18082 while (from >= end)
18083 *to-- = *from--;
18084 it->glyph_row->used[TEXT_AREA] += move_by;
18085 }
18086 }
18087 }
18088
18089 /* Compute the hash code for ROW. */
18090 unsigned
18091 row_hash (struct glyph_row *row)
18092 {
18093 int area, k;
18094 unsigned hashval = 0;
18095
18096 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18097 for (k = 0; k < row->used[area]; ++k)
18098 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18099 + row->glyphs[area][k].u.val
18100 + row->glyphs[area][k].face_id
18101 + row->glyphs[area][k].padding_p
18102 + (row->glyphs[area][k].type << 2));
18103
18104 return hashval;
18105 }
18106
18107 /* Compute the pixel height and width of IT->glyph_row.
18108
18109 Most of the time, ascent and height of a display line will be equal
18110 to the max_ascent and max_height values of the display iterator
18111 structure. This is not the case if
18112
18113 1. We hit ZV without displaying anything. In this case, max_ascent
18114 and max_height will be zero.
18115
18116 2. We have some glyphs that don't contribute to the line height.
18117 (The glyph row flag contributes_to_line_height_p is for future
18118 pixmap extensions).
18119
18120 The first case is easily covered by using default values because in
18121 these cases, the line height does not really matter, except that it
18122 must not be zero. */
18123
18124 static void
18125 compute_line_metrics (struct it *it)
18126 {
18127 struct glyph_row *row = it->glyph_row;
18128
18129 if (FRAME_WINDOW_P (it->f))
18130 {
18131 int i, min_y, max_y;
18132
18133 /* The line may consist of one space only, that was added to
18134 place the cursor on it. If so, the row's height hasn't been
18135 computed yet. */
18136 if (row->height == 0)
18137 {
18138 if (it->max_ascent + it->max_descent == 0)
18139 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18140 row->ascent = it->max_ascent;
18141 row->height = it->max_ascent + it->max_descent;
18142 row->phys_ascent = it->max_phys_ascent;
18143 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18144 row->extra_line_spacing = it->max_extra_line_spacing;
18145 }
18146
18147 /* Compute the width of this line. */
18148 row->pixel_width = row->x;
18149 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18150 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18151
18152 xassert (row->pixel_width >= 0);
18153 xassert (row->ascent >= 0 && row->height > 0);
18154
18155 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18156 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18157
18158 /* If first line's physical ascent is larger than its logical
18159 ascent, use the physical ascent, and make the row taller.
18160 This makes accented characters fully visible. */
18161 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18162 && row->phys_ascent > row->ascent)
18163 {
18164 row->height += row->phys_ascent - row->ascent;
18165 row->ascent = row->phys_ascent;
18166 }
18167
18168 /* Compute how much of the line is visible. */
18169 row->visible_height = row->height;
18170
18171 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18172 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18173
18174 if (row->y < min_y)
18175 row->visible_height -= min_y - row->y;
18176 if (row->y + row->height > max_y)
18177 row->visible_height -= row->y + row->height - max_y;
18178 }
18179 else
18180 {
18181 row->pixel_width = row->used[TEXT_AREA];
18182 if (row->continued_p)
18183 row->pixel_width -= it->continuation_pixel_width;
18184 else if (row->truncated_on_right_p)
18185 row->pixel_width -= it->truncation_pixel_width;
18186 row->ascent = row->phys_ascent = 0;
18187 row->height = row->phys_height = row->visible_height = 1;
18188 row->extra_line_spacing = 0;
18189 }
18190
18191 /* Compute a hash code for this row. */
18192 row->hash = row_hash (row);
18193
18194 it->max_ascent = it->max_descent = 0;
18195 it->max_phys_ascent = it->max_phys_descent = 0;
18196 }
18197
18198
18199 /* Append one space to the glyph row of iterator IT if doing a
18200 window-based redisplay. The space has the same face as
18201 IT->face_id. Value is non-zero if a space was added.
18202
18203 This function is called to make sure that there is always one glyph
18204 at the end of a glyph row that the cursor can be set on under
18205 window-systems. (If there weren't such a glyph we would not know
18206 how wide and tall a box cursor should be displayed).
18207
18208 At the same time this space let's a nicely handle clearing to the
18209 end of the line if the row ends in italic text. */
18210
18211 static int
18212 append_space_for_newline (struct it *it, int default_face_p)
18213 {
18214 if (FRAME_WINDOW_P (it->f))
18215 {
18216 int n = it->glyph_row->used[TEXT_AREA];
18217
18218 if (it->glyph_row->glyphs[TEXT_AREA] + n
18219 < it->glyph_row->glyphs[1 + TEXT_AREA])
18220 {
18221 /* Save some values that must not be changed.
18222 Must save IT->c and IT->len because otherwise
18223 ITERATOR_AT_END_P wouldn't work anymore after
18224 append_space_for_newline has been called. */
18225 enum display_element_type saved_what = it->what;
18226 int saved_c = it->c, saved_len = it->len;
18227 int saved_char_to_display = it->char_to_display;
18228 int saved_x = it->current_x;
18229 int saved_face_id = it->face_id;
18230 struct text_pos saved_pos;
18231 Lisp_Object saved_object;
18232 struct face *face;
18233
18234 saved_object = it->object;
18235 saved_pos = it->position;
18236
18237 it->what = IT_CHARACTER;
18238 memset (&it->position, 0, sizeof it->position);
18239 it->object = make_number (0);
18240 it->c = it->char_to_display = ' ';
18241 it->len = 1;
18242
18243 /* If the default face was remapped, be sure to use the
18244 remapped face for the appended newline. */
18245 if (default_face_p)
18246 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18247 else if (it->face_before_selective_p)
18248 it->face_id = it->saved_face_id;
18249 face = FACE_FROM_ID (it->f, it->face_id);
18250 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18251
18252 PRODUCE_GLYPHS (it);
18253
18254 it->override_ascent = -1;
18255 it->constrain_row_ascent_descent_p = 0;
18256 it->current_x = saved_x;
18257 it->object = saved_object;
18258 it->position = saved_pos;
18259 it->what = saved_what;
18260 it->face_id = saved_face_id;
18261 it->len = saved_len;
18262 it->c = saved_c;
18263 it->char_to_display = saved_char_to_display;
18264 return 1;
18265 }
18266 }
18267
18268 return 0;
18269 }
18270
18271
18272 /* Extend the face of the last glyph in the text area of IT->glyph_row
18273 to the end of the display line. Called from display_line. If the
18274 glyph row is empty, add a space glyph to it so that we know the
18275 face to draw. Set the glyph row flag fill_line_p. If the glyph
18276 row is R2L, prepend a stretch glyph to cover the empty space to the
18277 left of the leftmost glyph. */
18278
18279 static void
18280 extend_face_to_end_of_line (struct it *it)
18281 {
18282 struct face *face, *default_face;
18283 struct frame *f = it->f;
18284
18285 /* If line is already filled, do nothing. Non window-system frames
18286 get a grace of one more ``pixel'' because their characters are
18287 1-``pixel'' wide, so they hit the equality too early. This grace
18288 is needed only for R2L rows that are not continued, to produce
18289 one extra blank where we could display the cursor. */
18290 if (it->current_x >= it->last_visible_x
18291 + (!FRAME_WINDOW_P (f)
18292 && it->glyph_row->reversed_p
18293 && !it->glyph_row->continued_p))
18294 return;
18295
18296 /* The default face, possibly remapped. */
18297 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18298
18299 /* Face extension extends the background and box of IT->face_id
18300 to the end of the line. If the background equals the background
18301 of the frame, we don't have to do anything. */
18302 if (it->face_before_selective_p)
18303 face = FACE_FROM_ID (f, it->saved_face_id);
18304 else
18305 face = FACE_FROM_ID (f, it->face_id);
18306
18307 if (FRAME_WINDOW_P (f)
18308 && it->glyph_row->displays_text_p
18309 && face->box == FACE_NO_BOX
18310 && face->background == FRAME_BACKGROUND_PIXEL (f)
18311 && !face->stipple
18312 && !it->glyph_row->reversed_p)
18313 return;
18314
18315 /* Set the glyph row flag indicating that the face of the last glyph
18316 in the text area has to be drawn to the end of the text area. */
18317 it->glyph_row->fill_line_p = 1;
18318
18319 /* If current character of IT is not ASCII, make sure we have the
18320 ASCII face. This will be automatically undone the next time
18321 get_next_display_element returns a multibyte character. Note
18322 that the character will always be single byte in unibyte
18323 text. */
18324 if (!ASCII_CHAR_P (it->c))
18325 {
18326 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18327 }
18328
18329 if (FRAME_WINDOW_P (f))
18330 {
18331 /* If the row is empty, add a space with the current face of IT,
18332 so that we know which face to draw. */
18333 if (it->glyph_row->used[TEXT_AREA] == 0)
18334 {
18335 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18336 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18337 it->glyph_row->used[TEXT_AREA] = 1;
18338 }
18339 #ifdef HAVE_WINDOW_SYSTEM
18340 if (it->glyph_row->reversed_p)
18341 {
18342 /* Prepend a stretch glyph to the row, such that the
18343 rightmost glyph will be drawn flushed all the way to the
18344 right margin of the window. The stretch glyph that will
18345 occupy the empty space, if any, to the left of the
18346 glyphs. */
18347 struct font *font = face->font ? face->font : FRAME_FONT (f);
18348 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18349 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18350 struct glyph *g;
18351 int row_width, stretch_ascent, stretch_width;
18352 struct text_pos saved_pos;
18353 int saved_face_id, saved_avoid_cursor;
18354
18355 for (row_width = 0, g = row_start; g < row_end; g++)
18356 row_width += g->pixel_width;
18357 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18358 if (stretch_width > 0)
18359 {
18360 stretch_ascent =
18361 (((it->ascent + it->descent)
18362 * FONT_BASE (font)) / FONT_HEIGHT (font));
18363 saved_pos = it->position;
18364 memset (&it->position, 0, sizeof it->position);
18365 saved_avoid_cursor = it->avoid_cursor_p;
18366 it->avoid_cursor_p = 1;
18367 saved_face_id = it->face_id;
18368 /* The last row's stretch glyph should get the default
18369 face, to avoid painting the rest of the window with
18370 the region face, if the region ends at ZV. */
18371 if (it->glyph_row->ends_at_zv_p)
18372 it->face_id = default_face->id;
18373 else
18374 it->face_id = face->id;
18375 append_stretch_glyph (it, make_number (0), stretch_width,
18376 it->ascent + it->descent, stretch_ascent);
18377 it->position = saved_pos;
18378 it->avoid_cursor_p = saved_avoid_cursor;
18379 it->face_id = saved_face_id;
18380 }
18381 }
18382 #endif /* HAVE_WINDOW_SYSTEM */
18383 }
18384 else
18385 {
18386 /* Save some values that must not be changed. */
18387 int saved_x = it->current_x;
18388 struct text_pos saved_pos;
18389 Lisp_Object saved_object;
18390 enum display_element_type saved_what = it->what;
18391 int saved_face_id = it->face_id;
18392
18393 saved_object = it->object;
18394 saved_pos = it->position;
18395
18396 it->what = IT_CHARACTER;
18397 memset (&it->position, 0, sizeof it->position);
18398 it->object = make_number (0);
18399 it->c = it->char_to_display = ' ';
18400 it->len = 1;
18401 /* The last row's blank glyphs should get the default face, to
18402 avoid painting the rest of the window with the region face,
18403 if the region ends at ZV. */
18404 if (it->glyph_row->ends_at_zv_p)
18405 it->face_id = default_face->id;
18406 else
18407 it->face_id = face->id;
18408
18409 PRODUCE_GLYPHS (it);
18410
18411 while (it->current_x <= it->last_visible_x)
18412 PRODUCE_GLYPHS (it);
18413
18414 /* Don't count these blanks really. It would let us insert a left
18415 truncation glyph below and make us set the cursor on them, maybe. */
18416 it->current_x = saved_x;
18417 it->object = saved_object;
18418 it->position = saved_pos;
18419 it->what = saved_what;
18420 it->face_id = saved_face_id;
18421 }
18422 }
18423
18424
18425 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18426 trailing whitespace. */
18427
18428 static int
18429 trailing_whitespace_p (ptrdiff_t charpos)
18430 {
18431 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18432 int c = 0;
18433
18434 while (bytepos < ZV_BYTE
18435 && (c = FETCH_CHAR (bytepos),
18436 c == ' ' || c == '\t'))
18437 ++bytepos;
18438
18439 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18440 {
18441 if (bytepos != PT_BYTE)
18442 return 1;
18443 }
18444 return 0;
18445 }
18446
18447
18448 /* Highlight trailing whitespace, if any, in ROW. */
18449
18450 static void
18451 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18452 {
18453 int used = row->used[TEXT_AREA];
18454
18455 if (used)
18456 {
18457 struct glyph *start = row->glyphs[TEXT_AREA];
18458 struct glyph *glyph = start + used - 1;
18459
18460 if (row->reversed_p)
18461 {
18462 /* Right-to-left rows need to be processed in the opposite
18463 direction, so swap the edge pointers. */
18464 glyph = start;
18465 start = row->glyphs[TEXT_AREA] + used - 1;
18466 }
18467
18468 /* Skip over glyphs inserted to display the cursor at the
18469 end of a line, for extending the face of the last glyph
18470 to the end of the line on terminals, and for truncation
18471 and continuation glyphs. */
18472 if (!row->reversed_p)
18473 {
18474 while (glyph >= start
18475 && glyph->type == CHAR_GLYPH
18476 && INTEGERP (glyph->object))
18477 --glyph;
18478 }
18479 else
18480 {
18481 while (glyph <= start
18482 && glyph->type == CHAR_GLYPH
18483 && INTEGERP (glyph->object))
18484 ++glyph;
18485 }
18486
18487 /* If last glyph is a space or stretch, and it's trailing
18488 whitespace, set the face of all trailing whitespace glyphs in
18489 IT->glyph_row to `trailing-whitespace'. */
18490 if ((row->reversed_p ? glyph <= start : glyph >= start)
18491 && BUFFERP (glyph->object)
18492 && (glyph->type == STRETCH_GLYPH
18493 || (glyph->type == CHAR_GLYPH
18494 && glyph->u.ch == ' '))
18495 && trailing_whitespace_p (glyph->charpos))
18496 {
18497 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18498 if (face_id < 0)
18499 return;
18500
18501 if (!row->reversed_p)
18502 {
18503 while (glyph >= start
18504 && BUFFERP (glyph->object)
18505 && (glyph->type == STRETCH_GLYPH
18506 || (glyph->type == CHAR_GLYPH
18507 && glyph->u.ch == ' ')))
18508 (glyph--)->face_id = face_id;
18509 }
18510 else
18511 {
18512 while (glyph <= start
18513 && BUFFERP (glyph->object)
18514 && (glyph->type == STRETCH_GLYPH
18515 || (glyph->type == CHAR_GLYPH
18516 && glyph->u.ch == ' ')))
18517 (glyph++)->face_id = face_id;
18518 }
18519 }
18520 }
18521 }
18522
18523
18524 /* Value is non-zero if glyph row ROW should be
18525 used to hold the cursor. */
18526
18527 static int
18528 cursor_row_p (struct glyph_row *row)
18529 {
18530 int result = 1;
18531
18532 if (PT == CHARPOS (row->end.pos)
18533 || PT == MATRIX_ROW_END_CHARPOS (row))
18534 {
18535 /* Suppose the row ends on a string.
18536 Unless the row is continued, that means it ends on a newline
18537 in the string. If it's anything other than a display string
18538 (e.g., a before-string from an overlay), we don't want the
18539 cursor there. (This heuristic seems to give the optimal
18540 behavior for the various types of multi-line strings.)
18541 One exception: if the string has `cursor' property on one of
18542 its characters, we _do_ want the cursor there. */
18543 if (CHARPOS (row->end.string_pos) >= 0)
18544 {
18545 if (row->continued_p)
18546 result = 1;
18547 else
18548 {
18549 /* Check for `display' property. */
18550 struct glyph *beg = row->glyphs[TEXT_AREA];
18551 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18552 struct glyph *glyph;
18553
18554 result = 0;
18555 for (glyph = end; glyph >= beg; --glyph)
18556 if (STRINGP (glyph->object))
18557 {
18558 Lisp_Object prop
18559 = Fget_char_property (make_number (PT),
18560 Qdisplay, Qnil);
18561 result =
18562 (!NILP (prop)
18563 && display_prop_string_p (prop, glyph->object));
18564 /* If there's a `cursor' property on one of the
18565 string's characters, this row is a cursor row,
18566 even though this is not a display string. */
18567 if (!result)
18568 {
18569 Lisp_Object s = glyph->object;
18570
18571 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18572 {
18573 ptrdiff_t gpos = glyph->charpos;
18574
18575 if (!NILP (Fget_char_property (make_number (gpos),
18576 Qcursor, s)))
18577 {
18578 result = 1;
18579 break;
18580 }
18581 }
18582 }
18583 break;
18584 }
18585 }
18586 }
18587 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18588 {
18589 /* If the row ends in middle of a real character,
18590 and the line is continued, we want the cursor here.
18591 That's because CHARPOS (ROW->end.pos) would equal
18592 PT if PT is before the character. */
18593 if (!row->ends_in_ellipsis_p)
18594 result = row->continued_p;
18595 else
18596 /* If the row ends in an ellipsis, then
18597 CHARPOS (ROW->end.pos) will equal point after the
18598 invisible text. We want that position to be displayed
18599 after the ellipsis. */
18600 result = 0;
18601 }
18602 /* If the row ends at ZV, display the cursor at the end of that
18603 row instead of at the start of the row below. */
18604 else if (row->ends_at_zv_p)
18605 result = 1;
18606 else
18607 result = 0;
18608 }
18609
18610 return result;
18611 }
18612
18613 \f
18614
18615 /* Push the property PROP so that it will be rendered at the current
18616 position in IT. Return 1 if PROP was successfully pushed, 0
18617 otherwise. Called from handle_line_prefix to handle the
18618 `line-prefix' and `wrap-prefix' properties. */
18619
18620 static int
18621 push_prefix_prop (struct it *it, Lisp_Object prop)
18622 {
18623 struct text_pos pos =
18624 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18625
18626 xassert (it->method == GET_FROM_BUFFER
18627 || it->method == GET_FROM_DISPLAY_VECTOR
18628 || it->method == GET_FROM_STRING);
18629
18630 /* We need to save the current buffer/string position, so it will be
18631 restored by pop_it, because iterate_out_of_display_property
18632 depends on that being set correctly, but some situations leave
18633 it->position not yet set when this function is called. */
18634 push_it (it, &pos);
18635
18636 if (STRINGP (prop))
18637 {
18638 if (SCHARS (prop) == 0)
18639 {
18640 pop_it (it);
18641 return 0;
18642 }
18643
18644 it->string = prop;
18645 it->string_from_prefix_prop_p = 1;
18646 it->multibyte_p = STRING_MULTIBYTE (it->string);
18647 it->current.overlay_string_index = -1;
18648 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18649 it->end_charpos = it->string_nchars = SCHARS (it->string);
18650 it->method = GET_FROM_STRING;
18651 it->stop_charpos = 0;
18652 it->prev_stop = 0;
18653 it->base_level_stop = 0;
18654
18655 /* Force paragraph direction to be that of the parent
18656 buffer/string. */
18657 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18658 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18659 else
18660 it->paragraph_embedding = L2R;
18661
18662 /* Set up the bidi iterator for this display string. */
18663 if (it->bidi_p)
18664 {
18665 it->bidi_it.string.lstring = it->string;
18666 it->bidi_it.string.s = NULL;
18667 it->bidi_it.string.schars = it->end_charpos;
18668 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18669 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18670 it->bidi_it.string.unibyte = !it->multibyte_p;
18671 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18672 }
18673 }
18674 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18675 {
18676 it->method = GET_FROM_STRETCH;
18677 it->object = prop;
18678 }
18679 #ifdef HAVE_WINDOW_SYSTEM
18680 else if (IMAGEP (prop))
18681 {
18682 it->what = IT_IMAGE;
18683 it->image_id = lookup_image (it->f, prop);
18684 it->method = GET_FROM_IMAGE;
18685 }
18686 #endif /* HAVE_WINDOW_SYSTEM */
18687 else
18688 {
18689 pop_it (it); /* bogus display property, give up */
18690 return 0;
18691 }
18692
18693 return 1;
18694 }
18695
18696 /* Return the character-property PROP at the current position in IT. */
18697
18698 static Lisp_Object
18699 get_it_property (struct it *it, Lisp_Object prop)
18700 {
18701 Lisp_Object position;
18702
18703 if (STRINGP (it->object))
18704 position = make_number (IT_STRING_CHARPOS (*it));
18705 else if (BUFFERP (it->object))
18706 position = make_number (IT_CHARPOS (*it));
18707 else
18708 return Qnil;
18709
18710 return Fget_char_property (position, prop, it->object);
18711 }
18712
18713 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18714
18715 static void
18716 handle_line_prefix (struct it *it)
18717 {
18718 Lisp_Object prefix;
18719
18720 if (it->continuation_lines_width > 0)
18721 {
18722 prefix = get_it_property (it, Qwrap_prefix);
18723 if (NILP (prefix))
18724 prefix = Vwrap_prefix;
18725 }
18726 else
18727 {
18728 prefix = get_it_property (it, Qline_prefix);
18729 if (NILP (prefix))
18730 prefix = Vline_prefix;
18731 }
18732 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18733 {
18734 /* If the prefix is wider than the window, and we try to wrap
18735 it, it would acquire its own wrap prefix, and so on till the
18736 iterator stack overflows. So, don't wrap the prefix. */
18737 it->line_wrap = TRUNCATE;
18738 it->avoid_cursor_p = 1;
18739 }
18740 }
18741
18742 \f
18743
18744 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18745 only for R2L lines from display_line and display_string, when they
18746 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18747 the line/string needs to be continued on the next glyph row. */
18748 static void
18749 unproduce_glyphs (struct it *it, int n)
18750 {
18751 struct glyph *glyph, *end;
18752
18753 xassert (it->glyph_row);
18754 xassert (it->glyph_row->reversed_p);
18755 xassert (it->area == TEXT_AREA);
18756 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18757
18758 if (n > it->glyph_row->used[TEXT_AREA])
18759 n = it->glyph_row->used[TEXT_AREA];
18760 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18761 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18762 for ( ; glyph < end; glyph++)
18763 glyph[-n] = *glyph;
18764 }
18765
18766 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18767 and ROW->maxpos. */
18768 static void
18769 find_row_edges (struct it *it, struct glyph_row *row,
18770 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18771 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18772 {
18773 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18774 lines' rows is implemented for bidi-reordered rows. */
18775
18776 /* ROW->minpos is the value of min_pos, the minimal buffer position
18777 we have in ROW, or ROW->start.pos if that is smaller. */
18778 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18779 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18780 else
18781 /* We didn't find buffer positions smaller than ROW->start, or
18782 didn't find _any_ valid buffer positions in any of the glyphs,
18783 so we must trust the iterator's computed positions. */
18784 row->minpos = row->start.pos;
18785 if (max_pos <= 0)
18786 {
18787 max_pos = CHARPOS (it->current.pos);
18788 max_bpos = BYTEPOS (it->current.pos);
18789 }
18790
18791 /* Here are the various use-cases for ending the row, and the
18792 corresponding values for ROW->maxpos:
18793
18794 Line ends in a newline from buffer eol_pos + 1
18795 Line is continued from buffer max_pos + 1
18796 Line is truncated on right it->current.pos
18797 Line ends in a newline from string max_pos + 1(*)
18798 (*) + 1 only when line ends in a forward scan
18799 Line is continued from string max_pos
18800 Line is continued from display vector max_pos
18801 Line is entirely from a string min_pos == max_pos
18802 Line is entirely from a display vector min_pos == max_pos
18803 Line that ends at ZV ZV
18804
18805 If you discover other use-cases, please add them here as
18806 appropriate. */
18807 if (row->ends_at_zv_p)
18808 row->maxpos = it->current.pos;
18809 else if (row->used[TEXT_AREA])
18810 {
18811 int seen_this_string = 0;
18812 struct glyph_row *r1 = row - 1;
18813
18814 /* Did we see the same display string on the previous row? */
18815 if (STRINGP (it->object)
18816 /* this is not the first row */
18817 && row > it->w->desired_matrix->rows
18818 /* previous row is not the header line */
18819 && !r1->mode_line_p
18820 /* previous row also ends in a newline from a string */
18821 && r1->ends_in_newline_from_string_p)
18822 {
18823 struct glyph *start, *end;
18824
18825 /* Search for the last glyph of the previous row that came
18826 from buffer or string. Depending on whether the row is
18827 L2R or R2L, we need to process it front to back or the
18828 other way round. */
18829 if (!r1->reversed_p)
18830 {
18831 start = r1->glyphs[TEXT_AREA];
18832 end = start + r1->used[TEXT_AREA];
18833 /* Glyphs inserted by redisplay have an integer (zero)
18834 as their object. */
18835 while (end > start
18836 && INTEGERP ((end - 1)->object)
18837 && (end - 1)->charpos <= 0)
18838 --end;
18839 if (end > start)
18840 {
18841 if (EQ ((end - 1)->object, it->object))
18842 seen_this_string = 1;
18843 }
18844 else
18845 /* If all the glyphs of the previous row were inserted
18846 by redisplay, it means the previous row was
18847 produced from a single newline, which is only
18848 possible if that newline came from the same string
18849 as the one which produced this ROW. */
18850 seen_this_string = 1;
18851 }
18852 else
18853 {
18854 end = r1->glyphs[TEXT_AREA] - 1;
18855 start = end + r1->used[TEXT_AREA];
18856 while (end < start
18857 && INTEGERP ((end + 1)->object)
18858 && (end + 1)->charpos <= 0)
18859 ++end;
18860 if (end < start)
18861 {
18862 if (EQ ((end + 1)->object, it->object))
18863 seen_this_string = 1;
18864 }
18865 else
18866 seen_this_string = 1;
18867 }
18868 }
18869 /* Take note of each display string that covers a newline only
18870 once, the first time we see it. This is for when a display
18871 string includes more than one newline in it. */
18872 if (row->ends_in_newline_from_string_p && !seen_this_string)
18873 {
18874 /* If we were scanning the buffer forward when we displayed
18875 the string, we want to account for at least one buffer
18876 position that belongs to this row (position covered by
18877 the display string), so that cursor positioning will
18878 consider this row as a candidate when point is at the end
18879 of the visual line represented by this row. This is not
18880 required when scanning back, because max_pos will already
18881 have a much larger value. */
18882 if (CHARPOS (row->end.pos) > max_pos)
18883 INC_BOTH (max_pos, max_bpos);
18884 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18885 }
18886 else if (CHARPOS (it->eol_pos) > 0)
18887 SET_TEXT_POS (row->maxpos,
18888 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18889 else if (row->continued_p)
18890 {
18891 /* If max_pos is different from IT's current position, it
18892 means IT->method does not belong to the display element
18893 at max_pos. However, it also means that the display
18894 element at max_pos was displayed in its entirety on this
18895 line, which is equivalent to saying that the next line
18896 starts at the next buffer position. */
18897 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18898 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18899 else
18900 {
18901 INC_BOTH (max_pos, max_bpos);
18902 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18903 }
18904 }
18905 else if (row->truncated_on_right_p)
18906 /* display_line already called reseat_at_next_visible_line_start,
18907 which puts the iterator at the beginning of the next line, in
18908 the logical order. */
18909 row->maxpos = it->current.pos;
18910 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18911 /* A line that is entirely from a string/image/stretch... */
18912 row->maxpos = row->minpos;
18913 else
18914 abort ();
18915 }
18916 else
18917 row->maxpos = it->current.pos;
18918 }
18919
18920 /* Construct the glyph row IT->glyph_row in the desired matrix of
18921 IT->w from text at the current position of IT. See dispextern.h
18922 for an overview of struct it. Value is non-zero if
18923 IT->glyph_row displays text, as opposed to a line displaying ZV
18924 only. */
18925
18926 static int
18927 display_line (struct it *it)
18928 {
18929 struct glyph_row *row = it->glyph_row;
18930 Lisp_Object overlay_arrow_string;
18931 struct it wrap_it;
18932 void *wrap_data = NULL;
18933 int may_wrap = 0, wrap_x IF_LINT (= 0);
18934 int wrap_row_used = -1;
18935 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18936 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18937 int wrap_row_extra_line_spacing IF_LINT (= 0);
18938 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18939 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18940 int cvpos;
18941 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
18942 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18943
18944 /* We always start displaying at hpos zero even if hscrolled. */
18945 xassert (it->hpos == 0 && it->current_x == 0);
18946
18947 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18948 >= it->w->desired_matrix->nrows)
18949 {
18950 it->w->nrows_scale_factor++;
18951 fonts_changed_p = 1;
18952 return 0;
18953 }
18954
18955 /* Is IT->w showing the region? */
18956 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18957
18958 /* Clear the result glyph row and enable it. */
18959 prepare_desired_row (row);
18960
18961 row->y = it->current_y;
18962 row->start = it->start;
18963 row->continuation_lines_width = it->continuation_lines_width;
18964 row->displays_text_p = 1;
18965 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18966 it->starts_in_middle_of_char_p = 0;
18967
18968 /* Arrange the overlays nicely for our purposes. Usually, we call
18969 display_line on only one line at a time, in which case this
18970 can't really hurt too much, or we call it on lines which appear
18971 one after another in the buffer, in which case all calls to
18972 recenter_overlay_lists but the first will be pretty cheap. */
18973 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18974
18975 /* Move over display elements that are not visible because we are
18976 hscrolled. This may stop at an x-position < IT->first_visible_x
18977 if the first glyph is partially visible or if we hit a line end. */
18978 if (it->current_x < it->first_visible_x)
18979 {
18980 this_line_min_pos = row->start.pos;
18981 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18982 MOVE_TO_POS | MOVE_TO_X);
18983 /* Record the smallest positions seen while we moved over
18984 display elements that are not visible. This is needed by
18985 redisplay_internal for optimizing the case where the cursor
18986 stays inside the same line. The rest of this function only
18987 considers positions that are actually displayed, so
18988 RECORD_MAX_MIN_POS will not otherwise record positions that
18989 are hscrolled to the left of the left edge of the window. */
18990 min_pos = CHARPOS (this_line_min_pos);
18991 min_bpos = BYTEPOS (this_line_min_pos);
18992 }
18993 else
18994 {
18995 /* We only do this when not calling `move_it_in_display_line_to'
18996 above, because move_it_in_display_line_to calls
18997 handle_line_prefix itself. */
18998 handle_line_prefix (it);
18999 }
19000
19001 /* Get the initial row height. This is either the height of the
19002 text hscrolled, if there is any, or zero. */
19003 row->ascent = it->max_ascent;
19004 row->height = it->max_ascent + it->max_descent;
19005 row->phys_ascent = it->max_phys_ascent;
19006 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19007 row->extra_line_spacing = it->max_extra_line_spacing;
19008
19009 /* Utility macro to record max and min buffer positions seen until now. */
19010 #define RECORD_MAX_MIN_POS(IT) \
19011 do \
19012 { \
19013 int composition_p = !STRINGP ((IT)->string) \
19014 && ((IT)->what == IT_COMPOSITION); \
19015 ptrdiff_t current_pos = \
19016 composition_p ? (IT)->cmp_it.charpos \
19017 : IT_CHARPOS (*(IT)); \
19018 ptrdiff_t current_bpos = \
19019 composition_p ? CHAR_TO_BYTE (current_pos) \
19020 : IT_BYTEPOS (*(IT)); \
19021 if (current_pos < min_pos) \
19022 { \
19023 min_pos = current_pos; \
19024 min_bpos = current_bpos; \
19025 } \
19026 if (IT_CHARPOS (*it) > max_pos) \
19027 { \
19028 max_pos = IT_CHARPOS (*it); \
19029 max_bpos = IT_BYTEPOS (*it); \
19030 } \
19031 } \
19032 while (0)
19033
19034 /* Loop generating characters. The loop is left with IT on the next
19035 character to display. */
19036 while (1)
19037 {
19038 int n_glyphs_before, hpos_before, x_before;
19039 int x, nglyphs;
19040 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19041
19042 /* Retrieve the next thing to display. Value is zero if end of
19043 buffer reached. */
19044 if (!get_next_display_element (it))
19045 {
19046 /* Maybe add a space at the end of this line that is used to
19047 display the cursor there under X. Set the charpos of the
19048 first glyph of blank lines not corresponding to any text
19049 to -1. */
19050 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19051 row->exact_window_width_line_p = 1;
19052 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19053 || row->used[TEXT_AREA] == 0)
19054 {
19055 row->glyphs[TEXT_AREA]->charpos = -1;
19056 row->displays_text_p = 0;
19057
19058 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19059 && (!MINI_WINDOW_P (it->w)
19060 || (minibuf_level && EQ (it->window, minibuf_window))))
19061 row->indicate_empty_line_p = 1;
19062 }
19063
19064 it->continuation_lines_width = 0;
19065 row->ends_at_zv_p = 1;
19066 /* A row that displays right-to-left text must always have
19067 its last face extended all the way to the end of line,
19068 even if this row ends in ZV, because we still write to
19069 the screen left to right. We also need to extend the
19070 last face if the default face is remapped to some
19071 different face, otherwise the functions that clear
19072 portions of the screen will clear with the default face's
19073 background color. */
19074 if (row->reversed_p
19075 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19076 extend_face_to_end_of_line (it);
19077 break;
19078 }
19079
19080 /* Now, get the metrics of what we want to display. This also
19081 generates glyphs in `row' (which is IT->glyph_row). */
19082 n_glyphs_before = row->used[TEXT_AREA];
19083 x = it->current_x;
19084
19085 /* Remember the line height so far in case the next element doesn't
19086 fit on the line. */
19087 if (it->line_wrap != TRUNCATE)
19088 {
19089 ascent = it->max_ascent;
19090 descent = it->max_descent;
19091 phys_ascent = it->max_phys_ascent;
19092 phys_descent = it->max_phys_descent;
19093
19094 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19095 {
19096 if (IT_DISPLAYING_WHITESPACE (it))
19097 may_wrap = 1;
19098 else if (may_wrap)
19099 {
19100 SAVE_IT (wrap_it, *it, wrap_data);
19101 wrap_x = x;
19102 wrap_row_used = row->used[TEXT_AREA];
19103 wrap_row_ascent = row->ascent;
19104 wrap_row_height = row->height;
19105 wrap_row_phys_ascent = row->phys_ascent;
19106 wrap_row_phys_height = row->phys_height;
19107 wrap_row_extra_line_spacing = row->extra_line_spacing;
19108 wrap_row_min_pos = min_pos;
19109 wrap_row_min_bpos = min_bpos;
19110 wrap_row_max_pos = max_pos;
19111 wrap_row_max_bpos = max_bpos;
19112 may_wrap = 0;
19113 }
19114 }
19115 }
19116
19117 PRODUCE_GLYPHS (it);
19118
19119 /* If this display element was in marginal areas, continue with
19120 the next one. */
19121 if (it->area != TEXT_AREA)
19122 {
19123 row->ascent = max (row->ascent, it->max_ascent);
19124 row->height = max (row->height, it->max_ascent + it->max_descent);
19125 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19126 row->phys_height = max (row->phys_height,
19127 it->max_phys_ascent + it->max_phys_descent);
19128 row->extra_line_spacing = max (row->extra_line_spacing,
19129 it->max_extra_line_spacing);
19130 set_iterator_to_next (it, 1);
19131 continue;
19132 }
19133
19134 /* Does the display element fit on the line? If we truncate
19135 lines, we should draw past the right edge of the window. If
19136 we don't truncate, we want to stop so that we can display the
19137 continuation glyph before the right margin. If lines are
19138 continued, there are two possible strategies for characters
19139 resulting in more than 1 glyph (e.g. tabs): Display as many
19140 glyphs as possible in this line and leave the rest for the
19141 continuation line, or display the whole element in the next
19142 line. Original redisplay did the former, so we do it also. */
19143 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19144 hpos_before = it->hpos;
19145 x_before = x;
19146
19147 if (/* Not a newline. */
19148 nglyphs > 0
19149 /* Glyphs produced fit entirely in the line. */
19150 && it->current_x < it->last_visible_x)
19151 {
19152 it->hpos += nglyphs;
19153 row->ascent = max (row->ascent, it->max_ascent);
19154 row->height = max (row->height, it->max_ascent + it->max_descent);
19155 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19156 row->phys_height = max (row->phys_height,
19157 it->max_phys_ascent + it->max_phys_descent);
19158 row->extra_line_spacing = max (row->extra_line_spacing,
19159 it->max_extra_line_spacing);
19160 if (it->current_x - it->pixel_width < it->first_visible_x)
19161 row->x = x - it->first_visible_x;
19162 /* Record the maximum and minimum buffer positions seen so
19163 far in glyphs that will be displayed by this row. */
19164 if (it->bidi_p)
19165 RECORD_MAX_MIN_POS (it);
19166 }
19167 else
19168 {
19169 int i, new_x;
19170 struct glyph *glyph;
19171
19172 for (i = 0; i < nglyphs; ++i, x = new_x)
19173 {
19174 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19175 new_x = x + glyph->pixel_width;
19176
19177 if (/* Lines are continued. */
19178 it->line_wrap != TRUNCATE
19179 && (/* Glyph doesn't fit on the line. */
19180 new_x > it->last_visible_x
19181 /* Or it fits exactly on a window system frame. */
19182 || (new_x == it->last_visible_x
19183 && FRAME_WINDOW_P (it->f))))
19184 {
19185 /* End of a continued line. */
19186
19187 if (it->hpos == 0
19188 || (new_x == it->last_visible_x
19189 && FRAME_WINDOW_P (it->f)))
19190 {
19191 /* Current glyph is the only one on the line or
19192 fits exactly on the line. We must continue
19193 the line because we can't draw the cursor
19194 after the glyph. */
19195 row->continued_p = 1;
19196 it->current_x = new_x;
19197 it->continuation_lines_width += new_x;
19198 ++it->hpos;
19199 if (i == nglyphs - 1)
19200 {
19201 /* If line-wrap is on, check if a previous
19202 wrap point was found. */
19203 if (wrap_row_used > 0
19204 /* Even if there is a previous wrap
19205 point, continue the line here as
19206 usual, if (i) the previous character
19207 was a space or tab AND (ii) the
19208 current character is not. */
19209 && (!may_wrap
19210 || IT_DISPLAYING_WHITESPACE (it)))
19211 goto back_to_wrap;
19212
19213 /* Record the maximum and minimum buffer
19214 positions seen so far in glyphs that will be
19215 displayed by this row. */
19216 if (it->bidi_p)
19217 RECORD_MAX_MIN_POS (it);
19218 set_iterator_to_next (it, 1);
19219 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19220 {
19221 if (!get_next_display_element (it))
19222 {
19223 row->exact_window_width_line_p = 1;
19224 it->continuation_lines_width = 0;
19225 row->continued_p = 0;
19226 row->ends_at_zv_p = 1;
19227 }
19228 else if (ITERATOR_AT_END_OF_LINE_P (it))
19229 {
19230 row->continued_p = 0;
19231 row->exact_window_width_line_p = 1;
19232 }
19233 }
19234 }
19235 else if (it->bidi_p)
19236 RECORD_MAX_MIN_POS (it);
19237 }
19238 else if (CHAR_GLYPH_PADDING_P (*glyph)
19239 && !FRAME_WINDOW_P (it->f))
19240 {
19241 /* A padding glyph that doesn't fit on this line.
19242 This means the whole character doesn't fit
19243 on the line. */
19244 if (row->reversed_p)
19245 unproduce_glyphs (it, row->used[TEXT_AREA]
19246 - n_glyphs_before);
19247 row->used[TEXT_AREA] = n_glyphs_before;
19248
19249 /* Fill the rest of the row with continuation
19250 glyphs like in 20.x. */
19251 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19252 < row->glyphs[1 + TEXT_AREA])
19253 produce_special_glyphs (it, IT_CONTINUATION);
19254
19255 row->continued_p = 1;
19256 it->current_x = x_before;
19257 it->continuation_lines_width += x_before;
19258
19259 /* Restore the height to what it was before the
19260 element not fitting on the line. */
19261 it->max_ascent = ascent;
19262 it->max_descent = descent;
19263 it->max_phys_ascent = phys_ascent;
19264 it->max_phys_descent = phys_descent;
19265 }
19266 else if (wrap_row_used > 0)
19267 {
19268 back_to_wrap:
19269 if (row->reversed_p)
19270 unproduce_glyphs (it,
19271 row->used[TEXT_AREA] - wrap_row_used);
19272 RESTORE_IT (it, &wrap_it, wrap_data);
19273 it->continuation_lines_width += wrap_x;
19274 row->used[TEXT_AREA] = wrap_row_used;
19275 row->ascent = wrap_row_ascent;
19276 row->height = wrap_row_height;
19277 row->phys_ascent = wrap_row_phys_ascent;
19278 row->phys_height = wrap_row_phys_height;
19279 row->extra_line_spacing = wrap_row_extra_line_spacing;
19280 min_pos = wrap_row_min_pos;
19281 min_bpos = wrap_row_min_bpos;
19282 max_pos = wrap_row_max_pos;
19283 max_bpos = wrap_row_max_bpos;
19284 row->continued_p = 1;
19285 row->ends_at_zv_p = 0;
19286 row->exact_window_width_line_p = 0;
19287 it->continuation_lines_width += x;
19288
19289 /* Make sure that a non-default face is extended
19290 up to the right margin of the window. */
19291 extend_face_to_end_of_line (it);
19292 }
19293 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19294 {
19295 /* A TAB that extends past the right edge of the
19296 window. This produces a single glyph on
19297 window system frames. We leave the glyph in
19298 this row and let it fill the row, but don't
19299 consume the TAB. */
19300 it->continuation_lines_width += it->last_visible_x;
19301 row->ends_in_middle_of_char_p = 1;
19302 row->continued_p = 1;
19303 glyph->pixel_width = it->last_visible_x - x;
19304 it->starts_in_middle_of_char_p = 1;
19305 }
19306 else
19307 {
19308 /* Something other than a TAB that draws past
19309 the right edge of the window. Restore
19310 positions to values before the element. */
19311 if (row->reversed_p)
19312 unproduce_glyphs (it, row->used[TEXT_AREA]
19313 - (n_glyphs_before + i));
19314 row->used[TEXT_AREA] = n_glyphs_before + i;
19315
19316 /* Display continuation glyphs. */
19317 if (!FRAME_WINDOW_P (it->f))
19318 produce_special_glyphs (it, IT_CONTINUATION);
19319 row->continued_p = 1;
19320
19321 it->current_x = x_before;
19322 it->continuation_lines_width += x;
19323 extend_face_to_end_of_line (it);
19324
19325 if (nglyphs > 1 && i > 0)
19326 {
19327 row->ends_in_middle_of_char_p = 1;
19328 it->starts_in_middle_of_char_p = 1;
19329 }
19330
19331 /* Restore the height to what it was before the
19332 element not fitting on the line. */
19333 it->max_ascent = ascent;
19334 it->max_descent = descent;
19335 it->max_phys_ascent = phys_ascent;
19336 it->max_phys_descent = phys_descent;
19337 }
19338
19339 break;
19340 }
19341 else if (new_x > it->first_visible_x)
19342 {
19343 /* Increment number of glyphs actually displayed. */
19344 ++it->hpos;
19345
19346 /* Record the maximum and minimum buffer positions
19347 seen so far in glyphs that will be displayed by
19348 this row. */
19349 if (it->bidi_p)
19350 RECORD_MAX_MIN_POS (it);
19351
19352 if (x < it->first_visible_x)
19353 /* Glyph is partially visible, i.e. row starts at
19354 negative X position. */
19355 row->x = x - it->first_visible_x;
19356 }
19357 else
19358 {
19359 /* Glyph is completely off the left margin of the
19360 window. This should not happen because of the
19361 move_it_in_display_line at the start of this
19362 function, unless the text display area of the
19363 window is empty. */
19364 xassert (it->first_visible_x <= it->last_visible_x);
19365 }
19366 }
19367 /* Even if this display element produced no glyphs at all,
19368 we want to record its position. */
19369 if (it->bidi_p && nglyphs == 0)
19370 RECORD_MAX_MIN_POS (it);
19371
19372 row->ascent = max (row->ascent, it->max_ascent);
19373 row->height = max (row->height, it->max_ascent + it->max_descent);
19374 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19375 row->phys_height = max (row->phys_height,
19376 it->max_phys_ascent + it->max_phys_descent);
19377 row->extra_line_spacing = max (row->extra_line_spacing,
19378 it->max_extra_line_spacing);
19379
19380 /* End of this display line if row is continued. */
19381 if (row->continued_p || row->ends_at_zv_p)
19382 break;
19383 }
19384
19385 at_end_of_line:
19386 /* Is this a line end? If yes, we're also done, after making
19387 sure that a non-default face is extended up to the right
19388 margin of the window. */
19389 if (ITERATOR_AT_END_OF_LINE_P (it))
19390 {
19391 int used_before = row->used[TEXT_AREA];
19392
19393 row->ends_in_newline_from_string_p = STRINGP (it->object);
19394
19395 /* Add a space at the end of the line that is used to
19396 display the cursor there. */
19397 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19398 append_space_for_newline (it, 0);
19399
19400 /* Extend the face to the end of the line. */
19401 extend_face_to_end_of_line (it);
19402
19403 /* Make sure we have the position. */
19404 if (used_before == 0)
19405 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19406
19407 /* Record the position of the newline, for use in
19408 find_row_edges. */
19409 it->eol_pos = it->current.pos;
19410
19411 /* Consume the line end. This skips over invisible lines. */
19412 set_iterator_to_next (it, 1);
19413 it->continuation_lines_width = 0;
19414 break;
19415 }
19416
19417 /* Proceed with next display element. Note that this skips
19418 over lines invisible because of selective display. */
19419 set_iterator_to_next (it, 1);
19420
19421 /* If we truncate lines, we are done when the last displayed
19422 glyphs reach past the right margin of the window. */
19423 if (it->line_wrap == TRUNCATE
19424 && (FRAME_WINDOW_P (it->f)
19425 ? (it->current_x >= it->last_visible_x)
19426 : (it->current_x > it->last_visible_x)))
19427 {
19428 /* Maybe add truncation glyphs. */
19429 if (!FRAME_WINDOW_P (it->f))
19430 {
19431 int i, n;
19432
19433 if (!row->reversed_p)
19434 {
19435 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19436 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19437 break;
19438 }
19439 else
19440 {
19441 for (i = 0; i < row->used[TEXT_AREA]; i++)
19442 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19443 break;
19444 /* Remove any padding glyphs at the front of ROW, to
19445 make room for the truncation glyphs we will be
19446 adding below. The loop below always inserts at
19447 least one truncation glyph, so also remove the
19448 last glyph added to ROW. */
19449 unproduce_glyphs (it, i + 1);
19450 /* Adjust i for the loop below. */
19451 i = row->used[TEXT_AREA] - (i + 1);
19452 }
19453
19454 for (n = row->used[TEXT_AREA]; i < n; ++i)
19455 {
19456 row->used[TEXT_AREA] = i;
19457 produce_special_glyphs (it, IT_TRUNCATION);
19458 }
19459 }
19460 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19461 {
19462 /* Don't truncate if we can overflow newline into fringe. */
19463 if (!get_next_display_element (it))
19464 {
19465 it->continuation_lines_width = 0;
19466 row->ends_at_zv_p = 1;
19467 row->exact_window_width_line_p = 1;
19468 break;
19469 }
19470 if (ITERATOR_AT_END_OF_LINE_P (it))
19471 {
19472 row->exact_window_width_line_p = 1;
19473 goto at_end_of_line;
19474 }
19475 }
19476
19477 row->truncated_on_right_p = 1;
19478 it->continuation_lines_width = 0;
19479 reseat_at_next_visible_line_start (it, 0);
19480 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19481 it->hpos = hpos_before;
19482 it->current_x = x_before;
19483 break;
19484 }
19485 }
19486
19487 if (wrap_data)
19488 bidi_unshelve_cache (wrap_data, 1);
19489
19490 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19491 at the left window margin. */
19492 if (it->first_visible_x
19493 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19494 {
19495 if (!FRAME_WINDOW_P (it->f))
19496 insert_left_trunc_glyphs (it);
19497 row->truncated_on_left_p = 1;
19498 }
19499
19500 /* Remember the position at which this line ends.
19501
19502 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19503 cannot be before the call to find_row_edges below, since that is
19504 where these positions are determined. */
19505 row->end = it->current;
19506 if (!it->bidi_p)
19507 {
19508 row->minpos = row->start.pos;
19509 row->maxpos = row->end.pos;
19510 }
19511 else
19512 {
19513 /* ROW->minpos and ROW->maxpos must be the smallest and
19514 `1 + the largest' buffer positions in ROW. But if ROW was
19515 bidi-reordered, these two positions can be anywhere in the
19516 row, so we must determine them now. */
19517 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19518 }
19519
19520 /* If the start of this line is the overlay arrow-position, then
19521 mark this glyph row as the one containing the overlay arrow.
19522 This is clearly a mess with variable size fonts. It would be
19523 better to let it be displayed like cursors under X. */
19524 if ((row->displays_text_p || !overlay_arrow_seen)
19525 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19526 !NILP (overlay_arrow_string)))
19527 {
19528 /* Overlay arrow in window redisplay is a fringe bitmap. */
19529 if (STRINGP (overlay_arrow_string))
19530 {
19531 struct glyph_row *arrow_row
19532 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19533 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19534 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19535 struct glyph *p = row->glyphs[TEXT_AREA];
19536 struct glyph *p2, *end;
19537
19538 /* Copy the arrow glyphs. */
19539 while (glyph < arrow_end)
19540 *p++ = *glyph++;
19541
19542 /* Throw away padding glyphs. */
19543 p2 = p;
19544 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19545 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19546 ++p2;
19547 if (p2 > p)
19548 {
19549 while (p2 < end)
19550 *p++ = *p2++;
19551 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19552 }
19553 }
19554 else
19555 {
19556 xassert (INTEGERP (overlay_arrow_string));
19557 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19558 }
19559 overlay_arrow_seen = 1;
19560 }
19561
19562 /* Highlight trailing whitespace. */
19563 if (!NILP (Vshow_trailing_whitespace))
19564 highlight_trailing_whitespace (it->f, it->glyph_row);
19565
19566 /* Compute pixel dimensions of this line. */
19567 compute_line_metrics (it);
19568
19569 /* Implementation note: No changes in the glyphs of ROW or in their
19570 faces can be done past this point, because compute_line_metrics
19571 computes ROW's hash value and stores it within the glyph_row
19572 structure. */
19573
19574 /* Record whether this row ends inside an ellipsis. */
19575 row->ends_in_ellipsis_p
19576 = (it->method == GET_FROM_DISPLAY_VECTOR
19577 && it->ellipsis_p);
19578
19579 /* Save fringe bitmaps in this row. */
19580 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19581 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19582 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19583 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19584
19585 it->left_user_fringe_bitmap = 0;
19586 it->left_user_fringe_face_id = 0;
19587 it->right_user_fringe_bitmap = 0;
19588 it->right_user_fringe_face_id = 0;
19589
19590 /* Maybe set the cursor. */
19591 cvpos = it->w->cursor.vpos;
19592 if ((cvpos < 0
19593 /* In bidi-reordered rows, keep checking for proper cursor
19594 position even if one has been found already, because buffer
19595 positions in such rows change non-linearly with ROW->VPOS,
19596 when a line is continued. One exception: when we are at ZV,
19597 display cursor on the first suitable glyph row, since all
19598 the empty rows after that also have their position set to ZV. */
19599 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19600 lines' rows is implemented for bidi-reordered rows. */
19601 || (it->bidi_p
19602 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19603 && PT >= MATRIX_ROW_START_CHARPOS (row)
19604 && PT <= MATRIX_ROW_END_CHARPOS (row)
19605 && cursor_row_p (row))
19606 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19607
19608 /* Prepare for the next line. This line starts horizontally at (X
19609 HPOS) = (0 0). Vertical positions are incremented. As a
19610 convenience for the caller, IT->glyph_row is set to the next
19611 row to be used. */
19612 it->current_x = it->hpos = 0;
19613 it->current_y += row->height;
19614 SET_TEXT_POS (it->eol_pos, 0, 0);
19615 ++it->vpos;
19616 ++it->glyph_row;
19617 /* The next row should by default use the same value of the
19618 reversed_p flag as this one. set_iterator_to_next decides when
19619 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19620 the flag accordingly. */
19621 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19622 it->glyph_row->reversed_p = row->reversed_p;
19623 it->start = row->end;
19624 return row->displays_text_p;
19625
19626 #undef RECORD_MAX_MIN_POS
19627 }
19628
19629 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19630 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19631 doc: /* Return paragraph direction at point in BUFFER.
19632 Value is either `left-to-right' or `right-to-left'.
19633 If BUFFER is omitted or nil, it defaults to the current buffer.
19634
19635 Paragraph direction determines how the text in the paragraph is displayed.
19636 In left-to-right paragraphs, text begins at the left margin of the window
19637 and the reading direction is generally left to right. In right-to-left
19638 paragraphs, text begins at the right margin and is read from right to left.
19639
19640 See also `bidi-paragraph-direction'. */)
19641 (Lisp_Object buffer)
19642 {
19643 struct buffer *buf = current_buffer;
19644 struct buffer *old = buf;
19645
19646 if (! NILP (buffer))
19647 {
19648 CHECK_BUFFER (buffer);
19649 buf = XBUFFER (buffer);
19650 }
19651
19652 if (NILP (BVAR (buf, bidi_display_reordering))
19653 || NILP (BVAR (buf, enable_multibyte_characters))
19654 /* When we are loading loadup.el, the character property tables
19655 needed for bidi iteration are not yet available. */
19656 || !NILP (Vpurify_flag))
19657 return Qleft_to_right;
19658 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19659 return BVAR (buf, bidi_paragraph_direction);
19660 else
19661 {
19662 /* Determine the direction from buffer text. We could try to
19663 use current_matrix if it is up to date, but this seems fast
19664 enough as it is. */
19665 struct bidi_it itb;
19666 ptrdiff_t pos = BUF_PT (buf);
19667 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19668 int c;
19669 void *itb_data = bidi_shelve_cache ();
19670
19671 set_buffer_temp (buf);
19672 /* bidi_paragraph_init finds the base direction of the paragraph
19673 by searching forward from paragraph start. We need the base
19674 direction of the current or _previous_ paragraph, so we need
19675 to make sure we are within that paragraph. To that end, find
19676 the previous non-empty line. */
19677 if (pos >= ZV && pos > BEGV)
19678 {
19679 pos--;
19680 bytepos = CHAR_TO_BYTE (pos);
19681 }
19682 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19683 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19684 {
19685 while ((c = FETCH_BYTE (bytepos)) == '\n'
19686 || c == ' ' || c == '\t' || c == '\f')
19687 {
19688 if (bytepos <= BEGV_BYTE)
19689 break;
19690 bytepos--;
19691 pos--;
19692 }
19693 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19694 bytepos--;
19695 }
19696 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19697 itb.paragraph_dir = NEUTRAL_DIR;
19698 itb.string.s = NULL;
19699 itb.string.lstring = Qnil;
19700 itb.string.bufpos = 0;
19701 itb.string.unibyte = 0;
19702 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19703 bidi_unshelve_cache (itb_data, 0);
19704 set_buffer_temp (old);
19705 switch (itb.paragraph_dir)
19706 {
19707 case L2R:
19708 return Qleft_to_right;
19709 break;
19710 case R2L:
19711 return Qright_to_left;
19712 break;
19713 default:
19714 abort ();
19715 }
19716 }
19717 }
19718
19719
19720 \f
19721 /***********************************************************************
19722 Menu Bar
19723 ***********************************************************************/
19724
19725 /* Redisplay the menu bar in the frame for window W.
19726
19727 The menu bar of X frames that don't have X toolkit support is
19728 displayed in a special window W->frame->menu_bar_window.
19729
19730 The menu bar of terminal frames is treated specially as far as
19731 glyph matrices are concerned. Menu bar lines are not part of
19732 windows, so the update is done directly on the frame matrix rows
19733 for the menu bar. */
19734
19735 static void
19736 display_menu_bar (struct window *w)
19737 {
19738 struct frame *f = XFRAME (WINDOW_FRAME (w));
19739 struct it it;
19740 Lisp_Object items;
19741 int i;
19742
19743 /* Don't do all this for graphical frames. */
19744 #ifdef HAVE_NTGUI
19745 if (FRAME_W32_P (f))
19746 return;
19747 #endif
19748 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19749 if (FRAME_X_P (f))
19750 return;
19751 #endif
19752
19753 #ifdef HAVE_NS
19754 if (FRAME_NS_P (f))
19755 return;
19756 #endif /* HAVE_NS */
19757
19758 #ifdef USE_X_TOOLKIT
19759 xassert (!FRAME_WINDOW_P (f));
19760 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19761 it.first_visible_x = 0;
19762 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19763 #else /* not USE_X_TOOLKIT */
19764 if (FRAME_WINDOW_P (f))
19765 {
19766 /* Menu bar lines are displayed in the desired matrix of the
19767 dummy window menu_bar_window. */
19768 struct window *menu_w;
19769 xassert (WINDOWP (f->menu_bar_window));
19770 menu_w = XWINDOW (f->menu_bar_window);
19771 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19772 MENU_FACE_ID);
19773 it.first_visible_x = 0;
19774 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19775 }
19776 else
19777 {
19778 /* This is a TTY frame, i.e. character hpos/vpos are used as
19779 pixel x/y. */
19780 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19781 MENU_FACE_ID);
19782 it.first_visible_x = 0;
19783 it.last_visible_x = FRAME_COLS (f);
19784 }
19785 #endif /* not USE_X_TOOLKIT */
19786
19787 /* FIXME: This should be controlled by a user option. See the
19788 comments in redisplay_tool_bar and display_mode_line about
19789 this. */
19790 it.paragraph_embedding = L2R;
19791
19792 if (! mode_line_inverse_video)
19793 /* Force the menu-bar to be displayed in the default face. */
19794 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19795
19796 /* Clear all rows of the menu bar. */
19797 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19798 {
19799 struct glyph_row *row = it.glyph_row + i;
19800 clear_glyph_row (row);
19801 row->enabled_p = 1;
19802 row->full_width_p = 1;
19803 }
19804
19805 /* Display all items of the menu bar. */
19806 items = FRAME_MENU_BAR_ITEMS (it.f);
19807 for (i = 0; i < ASIZE (items); i += 4)
19808 {
19809 Lisp_Object string;
19810
19811 /* Stop at nil string. */
19812 string = AREF (items, i + 1);
19813 if (NILP (string))
19814 break;
19815
19816 /* Remember where item was displayed. */
19817 ASET (items, i + 3, make_number (it.hpos));
19818
19819 /* Display the item, pad with one space. */
19820 if (it.current_x < it.last_visible_x)
19821 display_string (NULL, string, Qnil, 0, 0, &it,
19822 SCHARS (string) + 1, 0, 0, -1);
19823 }
19824
19825 /* Fill out the line with spaces. */
19826 if (it.current_x < it.last_visible_x)
19827 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19828
19829 /* Compute the total height of the lines. */
19830 compute_line_metrics (&it);
19831 }
19832
19833
19834 \f
19835 /***********************************************************************
19836 Mode Line
19837 ***********************************************************************/
19838
19839 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19840 FORCE is non-zero, redisplay mode lines unconditionally.
19841 Otherwise, redisplay only mode lines that are garbaged. Value is
19842 the number of windows whose mode lines were redisplayed. */
19843
19844 static int
19845 redisplay_mode_lines (Lisp_Object window, int force)
19846 {
19847 int nwindows = 0;
19848
19849 while (!NILP (window))
19850 {
19851 struct window *w = XWINDOW (window);
19852
19853 if (WINDOWP (w->hchild))
19854 nwindows += redisplay_mode_lines (w->hchild, force);
19855 else if (WINDOWP (w->vchild))
19856 nwindows += redisplay_mode_lines (w->vchild, force);
19857 else if (force
19858 || FRAME_GARBAGED_P (XFRAME (w->frame))
19859 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19860 {
19861 struct text_pos lpoint;
19862 struct buffer *old = current_buffer;
19863
19864 /* Set the window's buffer for the mode line display. */
19865 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19866 set_buffer_internal_1 (XBUFFER (w->buffer));
19867
19868 /* Point refers normally to the selected window. For any
19869 other window, set up appropriate value. */
19870 if (!EQ (window, selected_window))
19871 {
19872 struct text_pos pt;
19873
19874 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19875 if (CHARPOS (pt) < BEGV)
19876 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19877 else if (CHARPOS (pt) > (ZV - 1))
19878 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19879 else
19880 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19881 }
19882
19883 /* Display mode lines. */
19884 clear_glyph_matrix (w->desired_matrix);
19885 if (display_mode_lines (w))
19886 {
19887 ++nwindows;
19888 w->must_be_updated_p = 1;
19889 }
19890
19891 /* Restore old settings. */
19892 set_buffer_internal_1 (old);
19893 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19894 }
19895
19896 window = w->next;
19897 }
19898
19899 return nwindows;
19900 }
19901
19902
19903 /* Display the mode and/or header line of window W. Value is the
19904 sum number of mode lines and header lines displayed. */
19905
19906 static int
19907 display_mode_lines (struct window *w)
19908 {
19909 Lisp_Object old_selected_window, old_selected_frame;
19910 int n = 0;
19911
19912 old_selected_frame = selected_frame;
19913 selected_frame = w->frame;
19914 old_selected_window = selected_window;
19915 XSETWINDOW (selected_window, w);
19916
19917 /* These will be set while the mode line specs are processed. */
19918 line_number_displayed = 0;
19919 w->column_number_displayed = Qnil;
19920
19921 if (WINDOW_WANTS_MODELINE_P (w))
19922 {
19923 struct window *sel_w = XWINDOW (old_selected_window);
19924
19925 /* Select mode line face based on the real selected window. */
19926 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19927 BVAR (current_buffer, mode_line_format));
19928 ++n;
19929 }
19930
19931 if (WINDOW_WANTS_HEADER_LINE_P (w))
19932 {
19933 display_mode_line (w, HEADER_LINE_FACE_ID,
19934 BVAR (current_buffer, header_line_format));
19935 ++n;
19936 }
19937
19938 selected_frame = old_selected_frame;
19939 selected_window = old_selected_window;
19940 return n;
19941 }
19942
19943
19944 /* Display mode or header line of window W. FACE_ID specifies which
19945 line to display; it is either MODE_LINE_FACE_ID or
19946 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19947 display. Value is the pixel height of the mode/header line
19948 displayed. */
19949
19950 static int
19951 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19952 {
19953 struct it it;
19954 struct face *face;
19955 ptrdiff_t count = SPECPDL_INDEX ();
19956
19957 init_iterator (&it, w, -1, -1, NULL, face_id);
19958 /* Don't extend on a previously drawn mode-line.
19959 This may happen if called from pos_visible_p. */
19960 it.glyph_row->enabled_p = 0;
19961 prepare_desired_row (it.glyph_row);
19962
19963 it.glyph_row->mode_line_p = 1;
19964
19965 if (! mode_line_inverse_video)
19966 /* Force the mode-line to be displayed in the default face. */
19967 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19968
19969 /* FIXME: This should be controlled by a user option. But
19970 supporting such an option is not trivial, since the mode line is
19971 made up of many separate strings. */
19972 it.paragraph_embedding = L2R;
19973
19974 record_unwind_protect (unwind_format_mode_line,
19975 format_mode_line_unwind_data (NULL, Qnil, 0));
19976
19977 mode_line_target = MODE_LINE_DISPLAY;
19978
19979 /* Temporarily make frame's keyboard the current kboard so that
19980 kboard-local variables in the mode_line_format will get the right
19981 values. */
19982 push_kboard (FRAME_KBOARD (it.f));
19983 record_unwind_save_match_data ();
19984 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19985 pop_kboard ();
19986
19987 unbind_to (count, Qnil);
19988
19989 /* Fill up with spaces. */
19990 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19991
19992 compute_line_metrics (&it);
19993 it.glyph_row->full_width_p = 1;
19994 it.glyph_row->continued_p = 0;
19995 it.glyph_row->truncated_on_left_p = 0;
19996 it.glyph_row->truncated_on_right_p = 0;
19997
19998 /* Make a 3D mode-line have a shadow at its right end. */
19999 face = FACE_FROM_ID (it.f, face_id);
20000 extend_face_to_end_of_line (&it);
20001 if (face->box != FACE_NO_BOX)
20002 {
20003 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20004 + it.glyph_row->used[TEXT_AREA] - 1);
20005 last->right_box_line_p = 1;
20006 }
20007
20008 return it.glyph_row->height;
20009 }
20010
20011 /* Move element ELT in LIST to the front of LIST.
20012 Return the updated list. */
20013
20014 static Lisp_Object
20015 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20016 {
20017 register Lisp_Object tail, prev;
20018 register Lisp_Object tem;
20019
20020 tail = list;
20021 prev = Qnil;
20022 while (CONSP (tail))
20023 {
20024 tem = XCAR (tail);
20025
20026 if (EQ (elt, tem))
20027 {
20028 /* Splice out the link TAIL. */
20029 if (NILP (prev))
20030 list = XCDR (tail);
20031 else
20032 Fsetcdr (prev, XCDR (tail));
20033
20034 /* Now make it the first. */
20035 Fsetcdr (tail, list);
20036 return tail;
20037 }
20038 else
20039 prev = tail;
20040 tail = XCDR (tail);
20041 QUIT;
20042 }
20043
20044 /* Not found--return unchanged LIST. */
20045 return list;
20046 }
20047
20048 /* Contribute ELT to the mode line for window IT->w. How it
20049 translates into text depends on its data type.
20050
20051 IT describes the display environment in which we display, as usual.
20052
20053 DEPTH is the depth in recursion. It is used to prevent
20054 infinite recursion here.
20055
20056 FIELD_WIDTH is the number of characters the display of ELT should
20057 occupy in the mode line, and PRECISION is the maximum number of
20058 characters to display from ELT's representation. See
20059 display_string for details.
20060
20061 Returns the hpos of the end of the text generated by ELT.
20062
20063 PROPS is a property list to add to any string we encounter.
20064
20065 If RISKY is nonzero, remove (disregard) any properties in any string
20066 we encounter, and ignore :eval and :propertize.
20067
20068 The global variable `mode_line_target' determines whether the
20069 output is passed to `store_mode_line_noprop',
20070 `store_mode_line_string', or `display_string'. */
20071
20072 static int
20073 display_mode_element (struct it *it, int depth, int field_width, int precision,
20074 Lisp_Object elt, Lisp_Object props, int risky)
20075 {
20076 int n = 0, field, prec;
20077 int literal = 0;
20078
20079 tail_recurse:
20080 if (depth > 100)
20081 elt = build_string ("*too-deep*");
20082
20083 depth++;
20084
20085 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20086 {
20087 case Lisp_String:
20088 {
20089 /* A string: output it and check for %-constructs within it. */
20090 unsigned char c;
20091 ptrdiff_t offset = 0;
20092
20093 if (SCHARS (elt) > 0
20094 && (!NILP (props) || risky))
20095 {
20096 Lisp_Object oprops, aelt;
20097 oprops = Ftext_properties_at (make_number (0), elt);
20098
20099 /* If the starting string's properties are not what
20100 we want, translate the string. Also, if the string
20101 is risky, do that anyway. */
20102
20103 if (NILP (Fequal (props, oprops)) || risky)
20104 {
20105 /* If the starting string has properties,
20106 merge the specified ones onto the existing ones. */
20107 if (! NILP (oprops) && !risky)
20108 {
20109 Lisp_Object tem;
20110
20111 oprops = Fcopy_sequence (oprops);
20112 tem = props;
20113 while (CONSP (tem))
20114 {
20115 oprops = Fplist_put (oprops, XCAR (tem),
20116 XCAR (XCDR (tem)));
20117 tem = XCDR (XCDR (tem));
20118 }
20119 props = oprops;
20120 }
20121
20122 aelt = Fassoc (elt, mode_line_proptrans_alist);
20123 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20124 {
20125 /* AELT is what we want. Move it to the front
20126 without consing. */
20127 elt = XCAR (aelt);
20128 mode_line_proptrans_alist
20129 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20130 }
20131 else
20132 {
20133 Lisp_Object tem;
20134
20135 /* If AELT has the wrong props, it is useless.
20136 so get rid of it. */
20137 if (! NILP (aelt))
20138 mode_line_proptrans_alist
20139 = Fdelq (aelt, mode_line_proptrans_alist);
20140
20141 elt = Fcopy_sequence (elt);
20142 Fset_text_properties (make_number (0), Flength (elt),
20143 props, elt);
20144 /* Add this item to mode_line_proptrans_alist. */
20145 mode_line_proptrans_alist
20146 = Fcons (Fcons (elt, props),
20147 mode_line_proptrans_alist);
20148 /* Truncate mode_line_proptrans_alist
20149 to at most 50 elements. */
20150 tem = Fnthcdr (make_number (50),
20151 mode_line_proptrans_alist);
20152 if (! NILP (tem))
20153 XSETCDR (tem, Qnil);
20154 }
20155 }
20156 }
20157
20158 offset = 0;
20159
20160 if (literal)
20161 {
20162 prec = precision - n;
20163 switch (mode_line_target)
20164 {
20165 case MODE_LINE_NOPROP:
20166 case MODE_LINE_TITLE:
20167 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20168 break;
20169 case MODE_LINE_STRING:
20170 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20171 break;
20172 case MODE_LINE_DISPLAY:
20173 n += display_string (NULL, elt, Qnil, 0, 0, it,
20174 0, prec, 0, STRING_MULTIBYTE (elt));
20175 break;
20176 }
20177
20178 break;
20179 }
20180
20181 /* Handle the non-literal case. */
20182
20183 while ((precision <= 0 || n < precision)
20184 && SREF (elt, offset) != 0
20185 && (mode_line_target != MODE_LINE_DISPLAY
20186 || it->current_x < it->last_visible_x))
20187 {
20188 ptrdiff_t last_offset = offset;
20189
20190 /* Advance to end of string or next format specifier. */
20191 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20192 ;
20193
20194 if (offset - 1 != last_offset)
20195 {
20196 ptrdiff_t nchars, nbytes;
20197
20198 /* Output to end of string or up to '%'. Field width
20199 is length of string. Don't output more than
20200 PRECISION allows us. */
20201 offset--;
20202
20203 prec = c_string_width (SDATA (elt) + last_offset,
20204 offset - last_offset, precision - n,
20205 &nchars, &nbytes);
20206
20207 switch (mode_line_target)
20208 {
20209 case MODE_LINE_NOPROP:
20210 case MODE_LINE_TITLE:
20211 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20212 break;
20213 case MODE_LINE_STRING:
20214 {
20215 ptrdiff_t bytepos = last_offset;
20216 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20217 ptrdiff_t endpos = (precision <= 0
20218 ? string_byte_to_char (elt, offset)
20219 : charpos + nchars);
20220
20221 n += store_mode_line_string (NULL,
20222 Fsubstring (elt, make_number (charpos),
20223 make_number (endpos)),
20224 0, 0, 0, Qnil);
20225 }
20226 break;
20227 case MODE_LINE_DISPLAY:
20228 {
20229 ptrdiff_t bytepos = last_offset;
20230 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20231
20232 if (precision <= 0)
20233 nchars = string_byte_to_char (elt, offset) - charpos;
20234 n += display_string (NULL, elt, Qnil, 0, charpos,
20235 it, 0, nchars, 0,
20236 STRING_MULTIBYTE (elt));
20237 }
20238 break;
20239 }
20240 }
20241 else /* c == '%' */
20242 {
20243 ptrdiff_t percent_position = offset;
20244
20245 /* Get the specified minimum width. Zero means
20246 don't pad. */
20247 field = 0;
20248 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20249 field = field * 10 + c - '0';
20250
20251 /* Don't pad beyond the total padding allowed. */
20252 if (field_width - n > 0 && field > field_width - n)
20253 field = field_width - n;
20254
20255 /* Note that either PRECISION <= 0 or N < PRECISION. */
20256 prec = precision - n;
20257
20258 if (c == 'M')
20259 n += display_mode_element (it, depth, field, prec,
20260 Vglobal_mode_string, props,
20261 risky);
20262 else if (c != 0)
20263 {
20264 int multibyte;
20265 ptrdiff_t bytepos, charpos;
20266 const char *spec;
20267 Lisp_Object string;
20268
20269 bytepos = percent_position;
20270 charpos = (STRING_MULTIBYTE (elt)
20271 ? string_byte_to_char (elt, bytepos)
20272 : bytepos);
20273 spec = decode_mode_spec (it->w, c, field, &string);
20274 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20275
20276 switch (mode_line_target)
20277 {
20278 case MODE_LINE_NOPROP:
20279 case MODE_LINE_TITLE:
20280 n += store_mode_line_noprop (spec, field, prec);
20281 break;
20282 case MODE_LINE_STRING:
20283 {
20284 Lisp_Object tem = build_string (spec);
20285 props = Ftext_properties_at (make_number (charpos), elt);
20286 /* Should only keep face property in props */
20287 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20288 }
20289 break;
20290 case MODE_LINE_DISPLAY:
20291 {
20292 int nglyphs_before, nwritten;
20293
20294 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20295 nwritten = display_string (spec, string, elt,
20296 charpos, 0, it,
20297 field, prec, 0,
20298 multibyte);
20299
20300 /* Assign to the glyphs written above the
20301 string where the `%x' came from, position
20302 of the `%'. */
20303 if (nwritten > 0)
20304 {
20305 struct glyph *glyph
20306 = (it->glyph_row->glyphs[TEXT_AREA]
20307 + nglyphs_before);
20308 int i;
20309
20310 for (i = 0; i < nwritten; ++i)
20311 {
20312 glyph[i].object = elt;
20313 glyph[i].charpos = charpos;
20314 }
20315
20316 n += nwritten;
20317 }
20318 }
20319 break;
20320 }
20321 }
20322 else /* c == 0 */
20323 break;
20324 }
20325 }
20326 }
20327 break;
20328
20329 case Lisp_Symbol:
20330 /* A symbol: process the value of the symbol recursively
20331 as if it appeared here directly. Avoid error if symbol void.
20332 Special case: if value of symbol is a string, output the string
20333 literally. */
20334 {
20335 register Lisp_Object tem;
20336
20337 /* If the variable is not marked as risky to set
20338 then its contents are risky to use. */
20339 if (NILP (Fget (elt, Qrisky_local_variable)))
20340 risky = 1;
20341
20342 tem = Fboundp (elt);
20343 if (!NILP (tem))
20344 {
20345 tem = Fsymbol_value (elt);
20346 /* If value is a string, output that string literally:
20347 don't check for % within it. */
20348 if (STRINGP (tem))
20349 literal = 1;
20350
20351 if (!EQ (tem, elt))
20352 {
20353 /* Give up right away for nil or t. */
20354 elt = tem;
20355 goto tail_recurse;
20356 }
20357 }
20358 }
20359 break;
20360
20361 case Lisp_Cons:
20362 {
20363 register Lisp_Object car, tem;
20364
20365 /* A cons cell: five distinct cases.
20366 If first element is :eval or :propertize, do something special.
20367 If first element is a string or a cons, process all the elements
20368 and effectively concatenate them.
20369 If first element is a negative number, truncate displaying cdr to
20370 at most that many characters. If positive, pad (with spaces)
20371 to at least that many characters.
20372 If first element is a symbol, process the cadr or caddr recursively
20373 according to whether the symbol's value is non-nil or nil. */
20374 car = XCAR (elt);
20375 if (EQ (car, QCeval))
20376 {
20377 /* An element of the form (:eval FORM) means evaluate FORM
20378 and use the result as mode line elements. */
20379
20380 if (risky)
20381 break;
20382
20383 if (CONSP (XCDR (elt)))
20384 {
20385 Lisp_Object spec;
20386 spec = safe_eval (XCAR (XCDR (elt)));
20387 n += display_mode_element (it, depth, field_width - n,
20388 precision - n, spec, props,
20389 risky);
20390 }
20391 }
20392 else if (EQ (car, QCpropertize))
20393 {
20394 /* An element of the form (:propertize ELT PROPS...)
20395 means display ELT but applying properties PROPS. */
20396
20397 if (risky)
20398 break;
20399
20400 if (CONSP (XCDR (elt)))
20401 n += display_mode_element (it, depth, field_width - n,
20402 precision - n, XCAR (XCDR (elt)),
20403 XCDR (XCDR (elt)), risky);
20404 }
20405 else if (SYMBOLP (car))
20406 {
20407 tem = Fboundp (car);
20408 elt = XCDR (elt);
20409 if (!CONSP (elt))
20410 goto invalid;
20411 /* elt is now the cdr, and we know it is a cons cell.
20412 Use its car if CAR has a non-nil value. */
20413 if (!NILP (tem))
20414 {
20415 tem = Fsymbol_value (car);
20416 if (!NILP (tem))
20417 {
20418 elt = XCAR (elt);
20419 goto tail_recurse;
20420 }
20421 }
20422 /* Symbol's value is nil (or symbol is unbound)
20423 Get the cddr of the original list
20424 and if possible find the caddr and use that. */
20425 elt = XCDR (elt);
20426 if (NILP (elt))
20427 break;
20428 else if (!CONSP (elt))
20429 goto invalid;
20430 elt = XCAR (elt);
20431 goto tail_recurse;
20432 }
20433 else if (INTEGERP (car))
20434 {
20435 register int lim = XINT (car);
20436 elt = XCDR (elt);
20437 if (lim < 0)
20438 {
20439 /* Negative int means reduce maximum width. */
20440 if (precision <= 0)
20441 precision = -lim;
20442 else
20443 precision = min (precision, -lim);
20444 }
20445 else if (lim > 0)
20446 {
20447 /* Padding specified. Don't let it be more than
20448 current maximum. */
20449 if (precision > 0)
20450 lim = min (precision, lim);
20451
20452 /* If that's more padding than already wanted, queue it.
20453 But don't reduce padding already specified even if
20454 that is beyond the current truncation point. */
20455 field_width = max (lim, field_width);
20456 }
20457 goto tail_recurse;
20458 }
20459 else if (STRINGP (car) || CONSP (car))
20460 {
20461 Lisp_Object halftail = elt;
20462 int len = 0;
20463
20464 while (CONSP (elt)
20465 && (precision <= 0 || n < precision))
20466 {
20467 n += display_mode_element (it, depth,
20468 /* Do padding only after the last
20469 element in the list. */
20470 (! CONSP (XCDR (elt))
20471 ? field_width - n
20472 : 0),
20473 precision - n, XCAR (elt),
20474 props, risky);
20475 elt = XCDR (elt);
20476 len++;
20477 if ((len & 1) == 0)
20478 halftail = XCDR (halftail);
20479 /* Check for cycle. */
20480 if (EQ (halftail, elt))
20481 break;
20482 }
20483 }
20484 }
20485 break;
20486
20487 default:
20488 invalid:
20489 elt = build_string ("*invalid*");
20490 goto tail_recurse;
20491 }
20492
20493 /* Pad to FIELD_WIDTH. */
20494 if (field_width > 0 && n < field_width)
20495 {
20496 switch (mode_line_target)
20497 {
20498 case MODE_LINE_NOPROP:
20499 case MODE_LINE_TITLE:
20500 n += store_mode_line_noprop ("", field_width - n, 0);
20501 break;
20502 case MODE_LINE_STRING:
20503 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20504 break;
20505 case MODE_LINE_DISPLAY:
20506 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20507 0, 0, 0);
20508 break;
20509 }
20510 }
20511
20512 return n;
20513 }
20514
20515 /* Store a mode-line string element in mode_line_string_list.
20516
20517 If STRING is non-null, display that C string. Otherwise, the Lisp
20518 string LISP_STRING is displayed.
20519
20520 FIELD_WIDTH is the minimum number of output glyphs to produce.
20521 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20522 with spaces. FIELD_WIDTH <= 0 means don't pad.
20523
20524 PRECISION is the maximum number of characters to output from
20525 STRING. PRECISION <= 0 means don't truncate the string.
20526
20527 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20528 properties to the string.
20529
20530 PROPS are the properties to add to the string.
20531 The mode_line_string_face face property is always added to the string.
20532 */
20533
20534 static int
20535 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20536 int field_width, int precision, Lisp_Object props)
20537 {
20538 ptrdiff_t len;
20539 int n = 0;
20540
20541 if (string != NULL)
20542 {
20543 len = strlen (string);
20544 if (precision > 0 && len > precision)
20545 len = precision;
20546 lisp_string = make_string (string, len);
20547 if (NILP (props))
20548 props = mode_line_string_face_prop;
20549 else if (!NILP (mode_line_string_face))
20550 {
20551 Lisp_Object face = Fplist_get (props, Qface);
20552 props = Fcopy_sequence (props);
20553 if (NILP (face))
20554 face = mode_line_string_face;
20555 else
20556 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20557 props = Fplist_put (props, Qface, face);
20558 }
20559 Fadd_text_properties (make_number (0), make_number (len),
20560 props, lisp_string);
20561 }
20562 else
20563 {
20564 len = XFASTINT (Flength (lisp_string));
20565 if (precision > 0 && len > precision)
20566 {
20567 len = precision;
20568 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20569 precision = -1;
20570 }
20571 if (!NILP (mode_line_string_face))
20572 {
20573 Lisp_Object face;
20574 if (NILP (props))
20575 props = Ftext_properties_at (make_number (0), lisp_string);
20576 face = Fplist_get (props, Qface);
20577 if (NILP (face))
20578 face = mode_line_string_face;
20579 else
20580 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20581 props = Fcons (Qface, Fcons (face, Qnil));
20582 if (copy_string)
20583 lisp_string = Fcopy_sequence (lisp_string);
20584 }
20585 if (!NILP (props))
20586 Fadd_text_properties (make_number (0), make_number (len),
20587 props, lisp_string);
20588 }
20589
20590 if (len > 0)
20591 {
20592 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20593 n += len;
20594 }
20595
20596 if (field_width > len)
20597 {
20598 field_width -= len;
20599 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20600 if (!NILP (props))
20601 Fadd_text_properties (make_number (0), make_number (field_width),
20602 props, lisp_string);
20603 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20604 n += field_width;
20605 }
20606
20607 return n;
20608 }
20609
20610
20611 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20612 1, 4, 0,
20613 doc: /* Format a string out of a mode line format specification.
20614 First arg FORMAT specifies the mode line format (see `mode-line-format'
20615 for details) to use.
20616
20617 By default, the format is evaluated for the currently selected window.
20618
20619 Optional second arg FACE specifies the face property to put on all
20620 characters for which no face is specified. The value nil means the
20621 default face. The value t means whatever face the window's mode line
20622 currently uses (either `mode-line' or `mode-line-inactive',
20623 depending on whether the window is the selected window or not).
20624 An integer value means the value string has no text
20625 properties.
20626
20627 Optional third and fourth args WINDOW and BUFFER specify the window
20628 and buffer to use as the context for the formatting (defaults
20629 are the selected window and the WINDOW's buffer). */)
20630 (Lisp_Object format, Lisp_Object face,
20631 Lisp_Object window, Lisp_Object buffer)
20632 {
20633 struct it it;
20634 int len;
20635 struct window *w;
20636 struct buffer *old_buffer = NULL;
20637 int face_id;
20638 int no_props = INTEGERP (face);
20639 ptrdiff_t count = SPECPDL_INDEX ();
20640 Lisp_Object str;
20641 int string_start = 0;
20642
20643 if (NILP (window))
20644 window = selected_window;
20645 CHECK_WINDOW (window);
20646 w = XWINDOW (window);
20647
20648 if (NILP (buffer))
20649 buffer = w->buffer;
20650 CHECK_BUFFER (buffer);
20651
20652 /* Make formatting the modeline a non-op when noninteractive, otherwise
20653 there will be problems later caused by a partially initialized frame. */
20654 if (NILP (format) || noninteractive)
20655 return empty_unibyte_string;
20656
20657 if (no_props)
20658 face = Qnil;
20659
20660 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20661 : EQ (face, Qt) ? (EQ (window, selected_window)
20662 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20663 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20664 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20665 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20666 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20667 : DEFAULT_FACE_ID;
20668
20669 if (XBUFFER (buffer) != current_buffer)
20670 old_buffer = current_buffer;
20671
20672 /* Save things including mode_line_proptrans_alist,
20673 and set that to nil so that we don't alter the outer value. */
20674 record_unwind_protect (unwind_format_mode_line,
20675 format_mode_line_unwind_data
20676 (old_buffer, selected_window, 1));
20677 mode_line_proptrans_alist = Qnil;
20678
20679 Fselect_window (window, Qt);
20680 if (old_buffer)
20681 set_buffer_internal_1 (XBUFFER (buffer));
20682
20683 init_iterator (&it, w, -1, -1, NULL, face_id);
20684
20685 if (no_props)
20686 {
20687 mode_line_target = MODE_LINE_NOPROP;
20688 mode_line_string_face_prop = Qnil;
20689 mode_line_string_list = Qnil;
20690 string_start = MODE_LINE_NOPROP_LEN (0);
20691 }
20692 else
20693 {
20694 mode_line_target = MODE_LINE_STRING;
20695 mode_line_string_list = Qnil;
20696 mode_line_string_face = face;
20697 mode_line_string_face_prop
20698 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20699 }
20700
20701 push_kboard (FRAME_KBOARD (it.f));
20702 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20703 pop_kboard ();
20704
20705 if (no_props)
20706 {
20707 len = MODE_LINE_NOPROP_LEN (string_start);
20708 str = make_string (mode_line_noprop_buf + string_start, len);
20709 }
20710 else
20711 {
20712 mode_line_string_list = Fnreverse (mode_line_string_list);
20713 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20714 empty_unibyte_string);
20715 }
20716
20717 unbind_to (count, Qnil);
20718 return str;
20719 }
20720
20721 /* Write a null-terminated, right justified decimal representation of
20722 the positive integer D to BUF using a minimal field width WIDTH. */
20723
20724 static void
20725 pint2str (register char *buf, register int width, register ptrdiff_t d)
20726 {
20727 register char *p = buf;
20728
20729 if (d <= 0)
20730 *p++ = '0';
20731 else
20732 {
20733 while (d > 0)
20734 {
20735 *p++ = d % 10 + '0';
20736 d /= 10;
20737 }
20738 }
20739
20740 for (width -= (int) (p - buf); width > 0; --width)
20741 *p++ = ' ';
20742 *p-- = '\0';
20743 while (p > buf)
20744 {
20745 d = *buf;
20746 *buf++ = *p;
20747 *p-- = d;
20748 }
20749 }
20750
20751 /* Write a null-terminated, right justified decimal and "human
20752 readable" representation of the nonnegative integer D to BUF using
20753 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20754
20755 static const char power_letter[] =
20756 {
20757 0, /* no letter */
20758 'k', /* kilo */
20759 'M', /* mega */
20760 'G', /* giga */
20761 'T', /* tera */
20762 'P', /* peta */
20763 'E', /* exa */
20764 'Z', /* zetta */
20765 'Y' /* yotta */
20766 };
20767
20768 static void
20769 pint2hrstr (char *buf, int width, ptrdiff_t d)
20770 {
20771 /* We aim to represent the nonnegative integer D as
20772 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20773 ptrdiff_t quotient = d;
20774 int remainder = 0;
20775 /* -1 means: do not use TENTHS. */
20776 int tenths = -1;
20777 int exponent = 0;
20778
20779 /* Length of QUOTIENT.TENTHS as a string. */
20780 int length;
20781
20782 char * psuffix;
20783 char * p;
20784
20785 if (1000 <= quotient)
20786 {
20787 /* Scale to the appropriate EXPONENT. */
20788 do
20789 {
20790 remainder = quotient % 1000;
20791 quotient /= 1000;
20792 exponent++;
20793 }
20794 while (1000 <= quotient);
20795
20796 /* Round to nearest and decide whether to use TENTHS or not. */
20797 if (quotient <= 9)
20798 {
20799 tenths = remainder / 100;
20800 if (50 <= remainder % 100)
20801 {
20802 if (tenths < 9)
20803 tenths++;
20804 else
20805 {
20806 quotient++;
20807 if (quotient == 10)
20808 tenths = -1;
20809 else
20810 tenths = 0;
20811 }
20812 }
20813 }
20814 else
20815 if (500 <= remainder)
20816 {
20817 if (quotient < 999)
20818 quotient++;
20819 else
20820 {
20821 quotient = 1;
20822 exponent++;
20823 tenths = 0;
20824 }
20825 }
20826 }
20827
20828 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20829 if (tenths == -1 && quotient <= 99)
20830 if (quotient <= 9)
20831 length = 1;
20832 else
20833 length = 2;
20834 else
20835 length = 3;
20836 p = psuffix = buf + max (width, length);
20837
20838 /* Print EXPONENT. */
20839 *psuffix++ = power_letter[exponent];
20840 *psuffix = '\0';
20841
20842 /* Print TENTHS. */
20843 if (tenths >= 0)
20844 {
20845 *--p = '0' + tenths;
20846 *--p = '.';
20847 }
20848
20849 /* Print QUOTIENT. */
20850 do
20851 {
20852 int digit = quotient % 10;
20853 *--p = '0' + digit;
20854 }
20855 while ((quotient /= 10) != 0);
20856
20857 /* Print leading spaces. */
20858 while (buf < p)
20859 *--p = ' ';
20860 }
20861
20862 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20863 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20864 type of CODING_SYSTEM. Return updated pointer into BUF. */
20865
20866 static unsigned char invalid_eol_type[] = "(*invalid*)";
20867
20868 static char *
20869 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20870 {
20871 Lisp_Object val;
20872 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20873 const unsigned char *eol_str;
20874 int eol_str_len;
20875 /* The EOL conversion we are using. */
20876 Lisp_Object eoltype;
20877
20878 val = CODING_SYSTEM_SPEC (coding_system);
20879 eoltype = Qnil;
20880
20881 if (!VECTORP (val)) /* Not yet decided. */
20882 {
20883 if (multibyte)
20884 *buf++ = '-';
20885 if (eol_flag)
20886 eoltype = eol_mnemonic_undecided;
20887 /* Don't mention EOL conversion if it isn't decided. */
20888 }
20889 else
20890 {
20891 Lisp_Object attrs;
20892 Lisp_Object eolvalue;
20893
20894 attrs = AREF (val, 0);
20895 eolvalue = AREF (val, 2);
20896
20897 if (multibyte)
20898 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20899
20900 if (eol_flag)
20901 {
20902 /* The EOL conversion that is normal on this system. */
20903
20904 if (NILP (eolvalue)) /* Not yet decided. */
20905 eoltype = eol_mnemonic_undecided;
20906 else if (VECTORP (eolvalue)) /* Not yet decided. */
20907 eoltype = eol_mnemonic_undecided;
20908 else /* eolvalue is Qunix, Qdos, or Qmac. */
20909 eoltype = (EQ (eolvalue, Qunix)
20910 ? eol_mnemonic_unix
20911 : (EQ (eolvalue, Qdos) == 1
20912 ? eol_mnemonic_dos : eol_mnemonic_mac));
20913 }
20914 }
20915
20916 if (eol_flag)
20917 {
20918 /* Mention the EOL conversion if it is not the usual one. */
20919 if (STRINGP (eoltype))
20920 {
20921 eol_str = SDATA (eoltype);
20922 eol_str_len = SBYTES (eoltype);
20923 }
20924 else if (CHARACTERP (eoltype))
20925 {
20926 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20927 int c = XFASTINT (eoltype);
20928 eol_str_len = CHAR_STRING (c, tmp);
20929 eol_str = tmp;
20930 }
20931 else
20932 {
20933 eol_str = invalid_eol_type;
20934 eol_str_len = sizeof (invalid_eol_type) - 1;
20935 }
20936 memcpy (buf, eol_str, eol_str_len);
20937 buf += eol_str_len;
20938 }
20939
20940 return buf;
20941 }
20942
20943 /* Return a string for the output of a mode line %-spec for window W,
20944 generated by character C. FIELD_WIDTH > 0 means pad the string
20945 returned with spaces to that value. Return a Lisp string in
20946 *STRING if the resulting string is taken from that Lisp string.
20947
20948 Note we operate on the current buffer for most purposes,
20949 the exception being w->base_line_pos. */
20950
20951 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20952
20953 static const char *
20954 decode_mode_spec (struct window *w, register int c, int field_width,
20955 Lisp_Object *string)
20956 {
20957 Lisp_Object obj;
20958 struct frame *f = XFRAME (WINDOW_FRAME (w));
20959 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20960 struct buffer *b = current_buffer;
20961
20962 obj = Qnil;
20963 *string = Qnil;
20964
20965 switch (c)
20966 {
20967 case '*':
20968 if (!NILP (BVAR (b, read_only)))
20969 return "%";
20970 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20971 return "*";
20972 return "-";
20973
20974 case '+':
20975 /* This differs from %* only for a modified read-only buffer. */
20976 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20977 return "*";
20978 if (!NILP (BVAR (b, read_only)))
20979 return "%";
20980 return "-";
20981
20982 case '&':
20983 /* This differs from %* in ignoring read-only-ness. */
20984 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20985 return "*";
20986 return "-";
20987
20988 case '%':
20989 return "%";
20990
20991 case '[':
20992 {
20993 int i;
20994 char *p;
20995
20996 if (command_loop_level > 5)
20997 return "[[[... ";
20998 p = decode_mode_spec_buf;
20999 for (i = 0; i < command_loop_level; i++)
21000 *p++ = '[';
21001 *p = 0;
21002 return decode_mode_spec_buf;
21003 }
21004
21005 case ']':
21006 {
21007 int i;
21008 char *p;
21009
21010 if (command_loop_level > 5)
21011 return " ...]]]";
21012 p = decode_mode_spec_buf;
21013 for (i = 0; i < command_loop_level; i++)
21014 *p++ = ']';
21015 *p = 0;
21016 return decode_mode_spec_buf;
21017 }
21018
21019 case '-':
21020 {
21021 register int i;
21022
21023 /* Let lots_of_dashes be a string of infinite length. */
21024 if (mode_line_target == MODE_LINE_NOPROP ||
21025 mode_line_target == MODE_LINE_STRING)
21026 return "--";
21027 if (field_width <= 0
21028 || field_width > sizeof (lots_of_dashes))
21029 {
21030 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21031 decode_mode_spec_buf[i] = '-';
21032 decode_mode_spec_buf[i] = '\0';
21033 return decode_mode_spec_buf;
21034 }
21035 else
21036 return lots_of_dashes;
21037 }
21038
21039 case 'b':
21040 obj = BVAR (b, name);
21041 break;
21042
21043 case 'c':
21044 /* %c and %l are ignored in `frame-title-format'.
21045 (In redisplay_internal, the frame title is drawn _before_ the
21046 windows are updated, so the stuff which depends on actual
21047 window contents (such as %l) may fail to render properly, or
21048 even crash emacs.) */
21049 if (mode_line_target == MODE_LINE_TITLE)
21050 return "";
21051 else
21052 {
21053 ptrdiff_t col = current_column ();
21054 w->column_number_displayed = make_number (col);
21055 pint2str (decode_mode_spec_buf, field_width, col);
21056 return decode_mode_spec_buf;
21057 }
21058
21059 case 'e':
21060 #ifndef SYSTEM_MALLOC
21061 {
21062 if (NILP (Vmemory_full))
21063 return "";
21064 else
21065 return "!MEM FULL! ";
21066 }
21067 #else
21068 return "";
21069 #endif
21070
21071 case 'F':
21072 /* %F displays the frame name. */
21073 if (!NILP (f->title))
21074 return SSDATA (f->title);
21075 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21076 return SSDATA (f->name);
21077 return "Emacs";
21078
21079 case 'f':
21080 obj = BVAR (b, filename);
21081 break;
21082
21083 case 'i':
21084 {
21085 ptrdiff_t size = ZV - BEGV;
21086 pint2str (decode_mode_spec_buf, field_width, size);
21087 return decode_mode_spec_buf;
21088 }
21089
21090 case 'I':
21091 {
21092 ptrdiff_t size = ZV - BEGV;
21093 pint2hrstr (decode_mode_spec_buf, field_width, size);
21094 return decode_mode_spec_buf;
21095 }
21096
21097 case 'l':
21098 {
21099 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21100 ptrdiff_t topline, nlines, height;
21101 ptrdiff_t junk;
21102
21103 /* %c and %l are ignored in `frame-title-format'. */
21104 if (mode_line_target == MODE_LINE_TITLE)
21105 return "";
21106
21107 startpos = XMARKER (w->start)->charpos;
21108 startpos_byte = marker_byte_position (w->start);
21109 height = WINDOW_TOTAL_LINES (w);
21110
21111 /* If we decided that this buffer isn't suitable for line numbers,
21112 don't forget that too fast. */
21113 if (EQ (w->base_line_pos, w->buffer))
21114 goto no_value;
21115 /* But do forget it, if the window shows a different buffer now. */
21116 else if (BUFFERP (w->base_line_pos))
21117 w->base_line_pos = Qnil;
21118
21119 /* If the buffer is very big, don't waste time. */
21120 if (INTEGERP (Vline_number_display_limit)
21121 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21122 {
21123 w->base_line_pos = Qnil;
21124 w->base_line_number = Qnil;
21125 goto no_value;
21126 }
21127
21128 if (INTEGERP (w->base_line_number)
21129 && INTEGERP (w->base_line_pos)
21130 && XFASTINT (w->base_line_pos) <= startpos)
21131 {
21132 line = XFASTINT (w->base_line_number);
21133 linepos = XFASTINT (w->base_line_pos);
21134 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21135 }
21136 else
21137 {
21138 line = 1;
21139 linepos = BUF_BEGV (b);
21140 linepos_byte = BUF_BEGV_BYTE (b);
21141 }
21142
21143 /* Count lines from base line to window start position. */
21144 nlines = display_count_lines (linepos_byte,
21145 startpos_byte,
21146 startpos, &junk);
21147
21148 topline = nlines + line;
21149
21150 /* Determine a new base line, if the old one is too close
21151 or too far away, or if we did not have one.
21152 "Too close" means it's plausible a scroll-down would
21153 go back past it. */
21154 if (startpos == BUF_BEGV (b))
21155 {
21156 w->base_line_number = make_number (topline);
21157 w->base_line_pos = make_number (BUF_BEGV (b));
21158 }
21159 else if (nlines < height + 25 || nlines > height * 3 + 50
21160 || linepos == BUF_BEGV (b))
21161 {
21162 ptrdiff_t limit = BUF_BEGV (b);
21163 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21164 ptrdiff_t position;
21165 ptrdiff_t distance =
21166 (height * 2 + 30) * line_number_display_limit_width;
21167
21168 if (startpos - distance > limit)
21169 {
21170 limit = startpos - distance;
21171 limit_byte = CHAR_TO_BYTE (limit);
21172 }
21173
21174 nlines = display_count_lines (startpos_byte,
21175 limit_byte,
21176 - (height * 2 + 30),
21177 &position);
21178 /* If we couldn't find the lines we wanted within
21179 line_number_display_limit_width chars per line,
21180 give up on line numbers for this window. */
21181 if (position == limit_byte && limit == startpos - distance)
21182 {
21183 w->base_line_pos = w->buffer;
21184 w->base_line_number = Qnil;
21185 goto no_value;
21186 }
21187
21188 w->base_line_number = make_number (topline - nlines);
21189 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21190 }
21191
21192 /* Now count lines from the start pos to point. */
21193 nlines = display_count_lines (startpos_byte,
21194 PT_BYTE, PT, &junk);
21195
21196 /* Record that we did display the line number. */
21197 line_number_displayed = 1;
21198
21199 /* Make the string to show. */
21200 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21201 return decode_mode_spec_buf;
21202 no_value:
21203 {
21204 char* p = decode_mode_spec_buf;
21205 int pad = field_width - 2;
21206 while (pad-- > 0)
21207 *p++ = ' ';
21208 *p++ = '?';
21209 *p++ = '?';
21210 *p = '\0';
21211 return decode_mode_spec_buf;
21212 }
21213 }
21214 break;
21215
21216 case 'm':
21217 obj = BVAR (b, mode_name);
21218 break;
21219
21220 case 'n':
21221 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21222 return " Narrow";
21223 break;
21224
21225 case 'p':
21226 {
21227 ptrdiff_t pos = marker_position (w->start);
21228 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21229
21230 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21231 {
21232 if (pos <= BUF_BEGV (b))
21233 return "All";
21234 else
21235 return "Bottom";
21236 }
21237 else if (pos <= BUF_BEGV (b))
21238 return "Top";
21239 else
21240 {
21241 if (total > 1000000)
21242 /* Do it differently for a large value, to avoid overflow. */
21243 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21244 else
21245 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21246 /* We can't normally display a 3-digit number,
21247 so get us a 2-digit number that is close. */
21248 if (total == 100)
21249 total = 99;
21250 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21251 return decode_mode_spec_buf;
21252 }
21253 }
21254
21255 /* Display percentage of size above the bottom of the screen. */
21256 case 'P':
21257 {
21258 ptrdiff_t toppos = marker_position (w->start);
21259 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21260 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21261
21262 if (botpos >= BUF_ZV (b))
21263 {
21264 if (toppos <= BUF_BEGV (b))
21265 return "All";
21266 else
21267 return "Bottom";
21268 }
21269 else
21270 {
21271 if (total > 1000000)
21272 /* Do it differently for a large value, to avoid overflow. */
21273 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21274 else
21275 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21276 /* We can't normally display a 3-digit number,
21277 so get us a 2-digit number that is close. */
21278 if (total == 100)
21279 total = 99;
21280 if (toppos <= BUF_BEGV (b))
21281 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21282 else
21283 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21284 return decode_mode_spec_buf;
21285 }
21286 }
21287
21288 case 's':
21289 /* status of process */
21290 obj = Fget_buffer_process (Fcurrent_buffer ());
21291 if (NILP (obj))
21292 return "no process";
21293 #ifndef MSDOS
21294 obj = Fsymbol_name (Fprocess_status (obj));
21295 #endif
21296 break;
21297
21298 case '@':
21299 {
21300 ptrdiff_t count = inhibit_garbage_collection ();
21301 Lisp_Object val = call1 (intern ("file-remote-p"),
21302 BVAR (current_buffer, directory));
21303 unbind_to (count, Qnil);
21304
21305 if (NILP (val))
21306 return "-";
21307 else
21308 return "@";
21309 }
21310
21311 case 't': /* indicate TEXT or BINARY */
21312 return "T";
21313
21314 case 'z':
21315 /* coding-system (not including end-of-line format) */
21316 case 'Z':
21317 /* coding-system (including end-of-line type) */
21318 {
21319 int eol_flag = (c == 'Z');
21320 char *p = decode_mode_spec_buf;
21321
21322 if (! FRAME_WINDOW_P (f))
21323 {
21324 /* No need to mention EOL here--the terminal never needs
21325 to do EOL conversion. */
21326 p = decode_mode_spec_coding (CODING_ID_NAME
21327 (FRAME_KEYBOARD_CODING (f)->id),
21328 p, 0);
21329 p = decode_mode_spec_coding (CODING_ID_NAME
21330 (FRAME_TERMINAL_CODING (f)->id),
21331 p, 0);
21332 }
21333 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21334 p, eol_flag);
21335
21336 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21337 #ifdef subprocesses
21338 obj = Fget_buffer_process (Fcurrent_buffer ());
21339 if (PROCESSP (obj))
21340 {
21341 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21342 p, eol_flag);
21343 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21344 p, eol_flag);
21345 }
21346 #endif /* subprocesses */
21347 #endif /* 0 */
21348 *p = 0;
21349 return decode_mode_spec_buf;
21350 }
21351 }
21352
21353 if (STRINGP (obj))
21354 {
21355 *string = obj;
21356 return SSDATA (obj);
21357 }
21358 else
21359 return "";
21360 }
21361
21362
21363 /* Count up to COUNT lines starting from START_BYTE.
21364 But don't go beyond LIMIT_BYTE.
21365 Return the number of lines thus found (always nonnegative).
21366
21367 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21368
21369 static ptrdiff_t
21370 display_count_lines (ptrdiff_t start_byte,
21371 ptrdiff_t limit_byte, ptrdiff_t count,
21372 ptrdiff_t *byte_pos_ptr)
21373 {
21374 register unsigned char *cursor;
21375 unsigned char *base;
21376
21377 register ptrdiff_t ceiling;
21378 register unsigned char *ceiling_addr;
21379 ptrdiff_t orig_count = count;
21380
21381 /* If we are not in selective display mode,
21382 check only for newlines. */
21383 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21384 && !INTEGERP (BVAR (current_buffer, selective_display)));
21385
21386 if (count > 0)
21387 {
21388 while (start_byte < limit_byte)
21389 {
21390 ceiling = BUFFER_CEILING_OF (start_byte);
21391 ceiling = min (limit_byte - 1, ceiling);
21392 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21393 base = (cursor = BYTE_POS_ADDR (start_byte));
21394 while (1)
21395 {
21396 if (selective_display)
21397 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21398 ;
21399 else
21400 while (*cursor != '\n' && ++cursor != ceiling_addr)
21401 ;
21402
21403 if (cursor != ceiling_addr)
21404 {
21405 if (--count == 0)
21406 {
21407 start_byte += cursor - base + 1;
21408 *byte_pos_ptr = start_byte;
21409 return orig_count;
21410 }
21411 else
21412 if (++cursor == ceiling_addr)
21413 break;
21414 }
21415 else
21416 break;
21417 }
21418 start_byte += cursor - base;
21419 }
21420 }
21421 else
21422 {
21423 while (start_byte > limit_byte)
21424 {
21425 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21426 ceiling = max (limit_byte, ceiling);
21427 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21428 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21429 while (1)
21430 {
21431 if (selective_display)
21432 while (--cursor != ceiling_addr
21433 && *cursor != '\n' && *cursor != 015)
21434 ;
21435 else
21436 while (--cursor != ceiling_addr && *cursor != '\n')
21437 ;
21438
21439 if (cursor != ceiling_addr)
21440 {
21441 if (++count == 0)
21442 {
21443 start_byte += cursor - base + 1;
21444 *byte_pos_ptr = start_byte;
21445 /* When scanning backwards, we should
21446 not count the newline posterior to which we stop. */
21447 return - orig_count - 1;
21448 }
21449 }
21450 else
21451 break;
21452 }
21453 /* Here we add 1 to compensate for the last decrement
21454 of CURSOR, which took it past the valid range. */
21455 start_byte += cursor - base + 1;
21456 }
21457 }
21458
21459 *byte_pos_ptr = limit_byte;
21460
21461 if (count < 0)
21462 return - orig_count + count;
21463 return orig_count - count;
21464
21465 }
21466
21467
21468 \f
21469 /***********************************************************************
21470 Displaying strings
21471 ***********************************************************************/
21472
21473 /* Display a NUL-terminated string, starting with index START.
21474
21475 If STRING is non-null, display that C string. Otherwise, the Lisp
21476 string LISP_STRING is displayed. There's a case that STRING is
21477 non-null and LISP_STRING is not nil. It means STRING is a string
21478 data of LISP_STRING. In that case, we display LISP_STRING while
21479 ignoring its text properties.
21480
21481 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21482 FACE_STRING. Display STRING or LISP_STRING with the face at
21483 FACE_STRING_POS in FACE_STRING:
21484
21485 Display the string in the environment given by IT, but use the
21486 standard display table, temporarily.
21487
21488 FIELD_WIDTH is the minimum number of output glyphs to produce.
21489 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21490 with spaces. If STRING has more characters, more than FIELD_WIDTH
21491 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21492
21493 PRECISION is the maximum number of characters to output from
21494 STRING. PRECISION < 0 means don't truncate the string.
21495
21496 This is roughly equivalent to printf format specifiers:
21497
21498 FIELD_WIDTH PRECISION PRINTF
21499 ----------------------------------------
21500 -1 -1 %s
21501 -1 10 %.10s
21502 10 -1 %10s
21503 20 10 %20.10s
21504
21505 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21506 display them, and < 0 means obey the current buffer's value of
21507 enable_multibyte_characters.
21508
21509 Value is the number of columns displayed. */
21510
21511 static int
21512 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21513 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21514 int field_width, int precision, int max_x, int multibyte)
21515 {
21516 int hpos_at_start = it->hpos;
21517 int saved_face_id = it->face_id;
21518 struct glyph_row *row = it->glyph_row;
21519 ptrdiff_t it_charpos;
21520
21521 /* Initialize the iterator IT for iteration over STRING beginning
21522 with index START. */
21523 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21524 precision, field_width, multibyte);
21525 if (string && STRINGP (lisp_string))
21526 /* LISP_STRING is the one returned by decode_mode_spec. We should
21527 ignore its text properties. */
21528 it->stop_charpos = it->end_charpos;
21529
21530 /* If displaying STRING, set up the face of the iterator from
21531 FACE_STRING, if that's given. */
21532 if (STRINGP (face_string))
21533 {
21534 ptrdiff_t endptr;
21535 struct face *face;
21536
21537 it->face_id
21538 = face_at_string_position (it->w, face_string, face_string_pos,
21539 0, it->region_beg_charpos,
21540 it->region_end_charpos,
21541 &endptr, it->base_face_id, 0);
21542 face = FACE_FROM_ID (it->f, it->face_id);
21543 it->face_box_p = face->box != FACE_NO_BOX;
21544 }
21545
21546 /* Set max_x to the maximum allowed X position. Don't let it go
21547 beyond the right edge of the window. */
21548 if (max_x <= 0)
21549 max_x = it->last_visible_x;
21550 else
21551 max_x = min (max_x, it->last_visible_x);
21552
21553 /* Skip over display elements that are not visible. because IT->w is
21554 hscrolled. */
21555 if (it->current_x < it->first_visible_x)
21556 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21557 MOVE_TO_POS | MOVE_TO_X);
21558
21559 row->ascent = it->max_ascent;
21560 row->height = it->max_ascent + it->max_descent;
21561 row->phys_ascent = it->max_phys_ascent;
21562 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21563 row->extra_line_spacing = it->max_extra_line_spacing;
21564
21565 if (STRINGP (it->string))
21566 it_charpos = IT_STRING_CHARPOS (*it);
21567 else
21568 it_charpos = IT_CHARPOS (*it);
21569
21570 /* This condition is for the case that we are called with current_x
21571 past last_visible_x. */
21572 while (it->current_x < max_x)
21573 {
21574 int x_before, x, n_glyphs_before, i, nglyphs;
21575
21576 /* Get the next display element. */
21577 if (!get_next_display_element (it))
21578 break;
21579
21580 /* Produce glyphs. */
21581 x_before = it->current_x;
21582 n_glyphs_before = row->used[TEXT_AREA];
21583 PRODUCE_GLYPHS (it);
21584
21585 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21586 i = 0;
21587 x = x_before;
21588 while (i < nglyphs)
21589 {
21590 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21591
21592 if (it->line_wrap != TRUNCATE
21593 && x + glyph->pixel_width > max_x)
21594 {
21595 /* End of continued line or max_x reached. */
21596 if (CHAR_GLYPH_PADDING_P (*glyph))
21597 {
21598 /* A wide character is unbreakable. */
21599 if (row->reversed_p)
21600 unproduce_glyphs (it, row->used[TEXT_AREA]
21601 - n_glyphs_before);
21602 row->used[TEXT_AREA] = n_glyphs_before;
21603 it->current_x = x_before;
21604 }
21605 else
21606 {
21607 if (row->reversed_p)
21608 unproduce_glyphs (it, row->used[TEXT_AREA]
21609 - (n_glyphs_before + i));
21610 row->used[TEXT_AREA] = n_glyphs_before + i;
21611 it->current_x = x;
21612 }
21613 break;
21614 }
21615 else if (x + glyph->pixel_width >= it->first_visible_x)
21616 {
21617 /* Glyph is at least partially visible. */
21618 ++it->hpos;
21619 if (x < it->first_visible_x)
21620 row->x = x - it->first_visible_x;
21621 }
21622 else
21623 {
21624 /* Glyph is off the left margin of the display area.
21625 Should not happen. */
21626 abort ();
21627 }
21628
21629 row->ascent = max (row->ascent, it->max_ascent);
21630 row->height = max (row->height, it->max_ascent + it->max_descent);
21631 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21632 row->phys_height = max (row->phys_height,
21633 it->max_phys_ascent + it->max_phys_descent);
21634 row->extra_line_spacing = max (row->extra_line_spacing,
21635 it->max_extra_line_spacing);
21636 x += glyph->pixel_width;
21637 ++i;
21638 }
21639
21640 /* Stop if max_x reached. */
21641 if (i < nglyphs)
21642 break;
21643
21644 /* Stop at line ends. */
21645 if (ITERATOR_AT_END_OF_LINE_P (it))
21646 {
21647 it->continuation_lines_width = 0;
21648 break;
21649 }
21650
21651 set_iterator_to_next (it, 1);
21652 if (STRINGP (it->string))
21653 it_charpos = IT_STRING_CHARPOS (*it);
21654 else
21655 it_charpos = IT_CHARPOS (*it);
21656
21657 /* Stop if truncating at the right edge. */
21658 if (it->line_wrap == TRUNCATE
21659 && it->current_x >= it->last_visible_x)
21660 {
21661 /* Add truncation mark, but don't do it if the line is
21662 truncated at a padding space. */
21663 if (it_charpos < it->string_nchars)
21664 {
21665 if (!FRAME_WINDOW_P (it->f))
21666 {
21667 int ii, n;
21668
21669 if (it->current_x > it->last_visible_x)
21670 {
21671 if (!row->reversed_p)
21672 {
21673 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21674 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21675 break;
21676 }
21677 else
21678 {
21679 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21680 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21681 break;
21682 unproduce_glyphs (it, ii + 1);
21683 ii = row->used[TEXT_AREA] - (ii + 1);
21684 }
21685 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21686 {
21687 row->used[TEXT_AREA] = ii;
21688 produce_special_glyphs (it, IT_TRUNCATION);
21689 }
21690 }
21691 produce_special_glyphs (it, IT_TRUNCATION);
21692 }
21693 row->truncated_on_right_p = 1;
21694 }
21695 break;
21696 }
21697 }
21698
21699 /* Maybe insert a truncation at the left. */
21700 if (it->first_visible_x
21701 && it_charpos > 0)
21702 {
21703 if (!FRAME_WINDOW_P (it->f))
21704 insert_left_trunc_glyphs (it);
21705 row->truncated_on_left_p = 1;
21706 }
21707
21708 it->face_id = saved_face_id;
21709
21710 /* Value is number of columns displayed. */
21711 return it->hpos - hpos_at_start;
21712 }
21713
21714
21715 \f
21716 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21717 appears as an element of LIST or as the car of an element of LIST.
21718 If PROPVAL is a list, compare each element against LIST in that
21719 way, and return 1/2 if any element of PROPVAL is found in LIST.
21720 Otherwise return 0. This function cannot quit.
21721 The return value is 2 if the text is invisible but with an ellipsis
21722 and 1 if it's invisible and without an ellipsis. */
21723
21724 int
21725 invisible_p (register Lisp_Object propval, Lisp_Object list)
21726 {
21727 register Lisp_Object tail, proptail;
21728
21729 for (tail = list; CONSP (tail); tail = XCDR (tail))
21730 {
21731 register Lisp_Object tem;
21732 tem = XCAR (tail);
21733 if (EQ (propval, tem))
21734 return 1;
21735 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21736 return NILP (XCDR (tem)) ? 1 : 2;
21737 }
21738
21739 if (CONSP (propval))
21740 {
21741 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21742 {
21743 Lisp_Object propelt;
21744 propelt = XCAR (proptail);
21745 for (tail = list; CONSP (tail); tail = XCDR (tail))
21746 {
21747 register Lisp_Object tem;
21748 tem = XCAR (tail);
21749 if (EQ (propelt, tem))
21750 return 1;
21751 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21752 return NILP (XCDR (tem)) ? 1 : 2;
21753 }
21754 }
21755 }
21756
21757 return 0;
21758 }
21759
21760 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21761 doc: /* Non-nil if the property makes the text invisible.
21762 POS-OR-PROP can be a marker or number, in which case it is taken to be
21763 a position in the current buffer and the value of the `invisible' property
21764 is checked; or it can be some other value, which is then presumed to be the
21765 value of the `invisible' property of the text of interest.
21766 The non-nil value returned can be t for truly invisible text or something
21767 else if the text is replaced by an ellipsis. */)
21768 (Lisp_Object pos_or_prop)
21769 {
21770 Lisp_Object prop
21771 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21772 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21773 : pos_or_prop);
21774 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21775 return (invis == 0 ? Qnil
21776 : invis == 1 ? Qt
21777 : make_number (invis));
21778 }
21779
21780 /* Calculate a width or height in pixels from a specification using
21781 the following elements:
21782
21783 SPEC ::=
21784 NUM - a (fractional) multiple of the default font width/height
21785 (NUM) - specifies exactly NUM pixels
21786 UNIT - a fixed number of pixels, see below.
21787 ELEMENT - size of a display element in pixels, see below.
21788 (NUM . SPEC) - equals NUM * SPEC
21789 (+ SPEC SPEC ...) - add pixel values
21790 (- SPEC SPEC ...) - subtract pixel values
21791 (- SPEC) - negate pixel value
21792
21793 NUM ::=
21794 INT or FLOAT - a number constant
21795 SYMBOL - use symbol's (buffer local) variable binding.
21796
21797 UNIT ::=
21798 in - pixels per inch *)
21799 mm - pixels per 1/1000 meter *)
21800 cm - pixels per 1/100 meter *)
21801 width - width of current font in pixels.
21802 height - height of current font in pixels.
21803
21804 *) using the ratio(s) defined in display-pixels-per-inch.
21805
21806 ELEMENT ::=
21807
21808 left-fringe - left fringe width in pixels
21809 right-fringe - right fringe width in pixels
21810
21811 left-margin - left margin width in pixels
21812 right-margin - right margin width in pixels
21813
21814 scroll-bar - scroll-bar area width in pixels
21815
21816 Examples:
21817
21818 Pixels corresponding to 5 inches:
21819 (5 . in)
21820
21821 Total width of non-text areas on left side of window (if scroll-bar is on left):
21822 '(space :width (+ left-fringe left-margin scroll-bar))
21823
21824 Align to first text column (in header line):
21825 '(space :align-to 0)
21826
21827 Align to middle of text area minus half the width of variable `my-image'
21828 containing a loaded image:
21829 '(space :align-to (0.5 . (- text my-image)))
21830
21831 Width of left margin minus width of 1 character in the default font:
21832 '(space :width (- left-margin 1))
21833
21834 Width of left margin minus width of 2 characters in the current font:
21835 '(space :width (- left-margin (2 . width)))
21836
21837 Center 1 character over left-margin (in header line):
21838 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21839
21840 Different ways to express width of left fringe plus left margin minus one pixel:
21841 '(space :width (- (+ left-fringe left-margin) (1)))
21842 '(space :width (+ left-fringe left-margin (- (1))))
21843 '(space :width (+ left-fringe left-margin (-1)))
21844
21845 */
21846
21847 #define NUMVAL(X) \
21848 ((INTEGERP (X) || FLOATP (X)) \
21849 ? XFLOATINT (X) \
21850 : - 1)
21851
21852 static int
21853 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21854 struct font *font, int width_p, int *align_to)
21855 {
21856 double pixels;
21857
21858 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21859 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21860
21861 if (NILP (prop))
21862 return OK_PIXELS (0);
21863
21864 xassert (FRAME_LIVE_P (it->f));
21865
21866 if (SYMBOLP (prop))
21867 {
21868 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21869 {
21870 char *unit = SSDATA (SYMBOL_NAME (prop));
21871
21872 if (unit[0] == 'i' && unit[1] == 'n')
21873 pixels = 1.0;
21874 else if (unit[0] == 'm' && unit[1] == 'm')
21875 pixels = 25.4;
21876 else if (unit[0] == 'c' && unit[1] == 'm')
21877 pixels = 2.54;
21878 else
21879 pixels = 0;
21880 if (pixels > 0)
21881 {
21882 double ppi;
21883 #ifdef HAVE_WINDOW_SYSTEM
21884 if (FRAME_WINDOW_P (it->f)
21885 && (ppi = (width_p
21886 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21887 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21888 ppi > 0))
21889 return OK_PIXELS (ppi / pixels);
21890 #endif
21891
21892 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21893 || (CONSP (Vdisplay_pixels_per_inch)
21894 && (ppi = (width_p
21895 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21896 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21897 ppi > 0)))
21898 return OK_PIXELS (ppi / pixels);
21899
21900 return 0;
21901 }
21902 }
21903
21904 #ifdef HAVE_WINDOW_SYSTEM
21905 if (EQ (prop, Qheight))
21906 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21907 if (EQ (prop, Qwidth))
21908 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21909 #else
21910 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21911 return OK_PIXELS (1);
21912 #endif
21913
21914 if (EQ (prop, Qtext))
21915 return OK_PIXELS (width_p
21916 ? window_box_width (it->w, TEXT_AREA)
21917 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21918
21919 if (align_to && *align_to < 0)
21920 {
21921 *res = 0;
21922 if (EQ (prop, Qleft))
21923 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21924 if (EQ (prop, Qright))
21925 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21926 if (EQ (prop, Qcenter))
21927 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21928 + window_box_width (it->w, TEXT_AREA) / 2);
21929 if (EQ (prop, Qleft_fringe))
21930 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21931 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21932 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21933 if (EQ (prop, Qright_fringe))
21934 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21935 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21936 : window_box_right_offset (it->w, TEXT_AREA));
21937 if (EQ (prop, Qleft_margin))
21938 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21939 if (EQ (prop, Qright_margin))
21940 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21941 if (EQ (prop, Qscroll_bar))
21942 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21943 ? 0
21944 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21945 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21946 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21947 : 0)));
21948 }
21949 else
21950 {
21951 if (EQ (prop, Qleft_fringe))
21952 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21953 if (EQ (prop, Qright_fringe))
21954 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21955 if (EQ (prop, Qleft_margin))
21956 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21957 if (EQ (prop, Qright_margin))
21958 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21959 if (EQ (prop, Qscroll_bar))
21960 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21961 }
21962
21963 prop = Fbuffer_local_value (prop, it->w->buffer);
21964 }
21965
21966 if (INTEGERP (prop) || FLOATP (prop))
21967 {
21968 int base_unit = (width_p
21969 ? FRAME_COLUMN_WIDTH (it->f)
21970 : FRAME_LINE_HEIGHT (it->f));
21971 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21972 }
21973
21974 if (CONSP (prop))
21975 {
21976 Lisp_Object car = XCAR (prop);
21977 Lisp_Object cdr = XCDR (prop);
21978
21979 if (SYMBOLP (car))
21980 {
21981 #ifdef HAVE_WINDOW_SYSTEM
21982 if (FRAME_WINDOW_P (it->f)
21983 && valid_image_p (prop))
21984 {
21985 ptrdiff_t id = lookup_image (it->f, prop);
21986 struct image *img = IMAGE_FROM_ID (it->f, id);
21987
21988 return OK_PIXELS (width_p ? img->width : img->height);
21989 }
21990 #endif
21991 if (EQ (car, Qplus) || EQ (car, Qminus))
21992 {
21993 int first = 1;
21994 double px;
21995
21996 pixels = 0;
21997 while (CONSP (cdr))
21998 {
21999 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22000 font, width_p, align_to))
22001 return 0;
22002 if (first)
22003 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22004 else
22005 pixels += px;
22006 cdr = XCDR (cdr);
22007 }
22008 if (EQ (car, Qminus))
22009 pixels = -pixels;
22010 return OK_PIXELS (pixels);
22011 }
22012
22013 car = Fbuffer_local_value (car, it->w->buffer);
22014 }
22015
22016 if (INTEGERP (car) || FLOATP (car))
22017 {
22018 double fact;
22019 pixels = XFLOATINT (car);
22020 if (NILP (cdr))
22021 return OK_PIXELS (pixels);
22022 if (calc_pixel_width_or_height (&fact, it, cdr,
22023 font, width_p, align_to))
22024 return OK_PIXELS (pixels * fact);
22025 return 0;
22026 }
22027
22028 return 0;
22029 }
22030
22031 return 0;
22032 }
22033
22034 \f
22035 /***********************************************************************
22036 Glyph Display
22037 ***********************************************************************/
22038
22039 #ifdef HAVE_WINDOW_SYSTEM
22040
22041 #if GLYPH_DEBUG
22042
22043 void
22044 dump_glyph_string (struct glyph_string *s)
22045 {
22046 fprintf (stderr, "glyph string\n");
22047 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22048 s->x, s->y, s->width, s->height);
22049 fprintf (stderr, " ybase = %d\n", s->ybase);
22050 fprintf (stderr, " hl = %d\n", s->hl);
22051 fprintf (stderr, " left overhang = %d, right = %d\n",
22052 s->left_overhang, s->right_overhang);
22053 fprintf (stderr, " nchars = %d\n", s->nchars);
22054 fprintf (stderr, " extends to end of line = %d\n",
22055 s->extends_to_end_of_line_p);
22056 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22057 fprintf (stderr, " bg width = %d\n", s->background_width);
22058 }
22059
22060 #endif /* GLYPH_DEBUG */
22061
22062 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22063 of XChar2b structures for S; it can't be allocated in
22064 init_glyph_string because it must be allocated via `alloca'. W
22065 is the window on which S is drawn. ROW and AREA are the glyph row
22066 and area within the row from which S is constructed. START is the
22067 index of the first glyph structure covered by S. HL is a
22068 face-override for drawing S. */
22069
22070 #ifdef HAVE_NTGUI
22071 #define OPTIONAL_HDC(hdc) HDC hdc,
22072 #define DECLARE_HDC(hdc) HDC hdc;
22073 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22074 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22075 #endif
22076
22077 #ifndef OPTIONAL_HDC
22078 #define OPTIONAL_HDC(hdc)
22079 #define DECLARE_HDC(hdc)
22080 #define ALLOCATE_HDC(hdc, f)
22081 #define RELEASE_HDC(hdc, f)
22082 #endif
22083
22084 static void
22085 init_glyph_string (struct glyph_string *s,
22086 OPTIONAL_HDC (hdc)
22087 XChar2b *char2b, struct window *w, struct glyph_row *row,
22088 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22089 {
22090 memset (s, 0, sizeof *s);
22091 s->w = w;
22092 s->f = XFRAME (w->frame);
22093 #ifdef HAVE_NTGUI
22094 s->hdc = hdc;
22095 #endif
22096 s->display = FRAME_X_DISPLAY (s->f);
22097 s->window = FRAME_X_WINDOW (s->f);
22098 s->char2b = char2b;
22099 s->hl = hl;
22100 s->row = row;
22101 s->area = area;
22102 s->first_glyph = row->glyphs[area] + start;
22103 s->height = row->height;
22104 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22105 s->ybase = s->y + row->ascent;
22106 }
22107
22108
22109 /* Append the list of glyph strings with head H and tail T to the list
22110 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22111
22112 static inline void
22113 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22114 struct glyph_string *h, struct glyph_string *t)
22115 {
22116 if (h)
22117 {
22118 if (*head)
22119 (*tail)->next = h;
22120 else
22121 *head = h;
22122 h->prev = *tail;
22123 *tail = t;
22124 }
22125 }
22126
22127
22128 /* Prepend the list of glyph strings with head H and tail T to the
22129 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22130 result. */
22131
22132 static inline void
22133 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22134 struct glyph_string *h, struct glyph_string *t)
22135 {
22136 if (h)
22137 {
22138 if (*head)
22139 (*head)->prev = t;
22140 else
22141 *tail = t;
22142 t->next = *head;
22143 *head = h;
22144 }
22145 }
22146
22147
22148 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22149 Set *HEAD and *TAIL to the resulting list. */
22150
22151 static inline void
22152 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22153 struct glyph_string *s)
22154 {
22155 s->next = s->prev = NULL;
22156 append_glyph_string_lists (head, tail, s, s);
22157 }
22158
22159
22160 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22161 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22162 make sure that X resources for the face returned are allocated.
22163 Value is a pointer to a realized face that is ready for display if
22164 DISPLAY_P is non-zero. */
22165
22166 static inline struct face *
22167 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22168 XChar2b *char2b, int display_p)
22169 {
22170 struct face *face = FACE_FROM_ID (f, face_id);
22171
22172 if (face->font)
22173 {
22174 unsigned code = face->font->driver->encode_char (face->font, c);
22175
22176 if (code != FONT_INVALID_CODE)
22177 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22178 else
22179 STORE_XCHAR2B (char2b, 0, 0);
22180 }
22181
22182 /* Make sure X resources of the face are allocated. */
22183 #ifdef HAVE_X_WINDOWS
22184 if (display_p)
22185 #endif
22186 {
22187 xassert (face != NULL);
22188 PREPARE_FACE_FOR_DISPLAY (f, face);
22189 }
22190
22191 return face;
22192 }
22193
22194
22195 /* Get face and two-byte form of character glyph GLYPH on frame F.
22196 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22197 a pointer to a realized face that is ready for display. */
22198
22199 static inline struct face *
22200 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22201 XChar2b *char2b, int *two_byte_p)
22202 {
22203 struct face *face;
22204
22205 xassert (glyph->type == CHAR_GLYPH);
22206 face = FACE_FROM_ID (f, glyph->face_id);
22207
22208 if (two_byte_p)
22209 *two_byte_p = 0;
22210
22211 if (face->font)
22212 {
22213 unsigned code;
22214
22215 if (CHAR_BYTE8_P (glyph->u.ch))
22216 code = CHAR_TO_BYTE8 (glyph->u.ch);
22217 else
22218 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22219
22220 if (code != FONT_INVALID_CODE)
22221 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22222 else
22223 STORE_XCHAR2B (char2b, 0, 0);
22224 }
22225
22226 /* Make sure X resources of the face are allocated. */
22227 xassert (face != NULL);
22228 PREPARE_FACE_FOR_DISPLAY (f, face);
22229 return face;
22230 }
22231
22232
22233 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22234 Return 1 if FONT has a glyph for C, otherwise return 0. */
22235
22236 static inline int
22237 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22238 {
22239 unsigned code;
22240
22241 if (CHAR_BYTE8_P (c))
22242 code = CHAR_TO_BYTE8 (c);
22243 else
22244 code = font->driver->encode_char (font, c);
22245
22246 if (code == FONT_INVALID_CODE)
22247 return 0;
22248 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22249 return 1;
22250 }
22251
22252
22253 /* Fill glyph string S with composition components specified by S->cmp.
22254
22255 BASE_FACE is the base face of the composition.
22256 S->cmp_from is the index of the first component for S.
22257
22258 OVERLAPS non-zero means S should draw the foreground only, and use
22259 its physical height for clipping. See also draw_glyphs.
22260
22261 Value is the index of a component not in S. */
22262
22263 static int
22264 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22265 int overlaps)
22266 {
22267 int i;
22268 /* For all glyphs of this composition, starting at the offset
22269 S->cmp_from, until we reach the end of the definition or encounter a
22270 glyph that requires the different face, add it to S. */
22271 struct face *face;
22272
22273 xassert (s);
22274
22275 s->for_overlaps = overlaps;
22276 s->face = NULL;
22277 s->font = NULL;
22278 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22279 {
22280 int c = COMPOSITION_GLYPH (s->cmp, i);
22281
22282 /* TAB in a composition means display glyphs with padding space
22283 on the left or right. */
22284 if (c != '\t')
22285 {
22286 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22287 -1, Qnil);
22288
22289 face = get_char_face_and_encoding (s->f, c, face_id,
22290 s->char2b + i, 1);
22291 if (face)
22292 {
22293 if (! s->face)
22294 {
22295 s->face = face;
22296 s->font = s->face->font;
22297 }
22298 else if (s->face != face)
22299 break;
22300 }
22301 }
22302 ++s->nchars;
22303 }
22304 s->cmp_to = i;
22305
22306 if (s->face == NULL)
22307 {
22308 s->face = base_face->ascii_face;
22309 s->font = s->face->font;
22310 }
22311
22312 /* All glyph strings for the same composition has the same width,
22313 i.e. the width set for the first component of the composition. */
22314 s->width = s->first_glyph->pixel_width;
22315
22316 /* If the specified font could not be loaded, use the frame's
22317 default font, but record the fact that we couldn't load it in
22318 the glyph string so that we can draw rectangles for the
22319 characters of the glyph string. */
22320 if (s->font == NULL)
22321 {
22322 s->font_not_found_p = 1;
22323 s->font = FRAME_FONT (s->f);
22324 }
22325
22326 /* Adjust base line for subscript/superscript text. */
22327 s->ybase += s->first_glyph->voffset;
22328
22329 /* This glyph string must always be drawn with 16-bit functions. */
22330 s->two_byte_p = 1;
22331
22332 return s->cmp_to;
22333 }
22334
22335 static int
22336 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22337 int start, int end, int overlaps)
22338 {
22339 struct glyph *glyph, *last;
22340 Lisp_Object lgstring;
22341 int i;
22342
22343 s->for_overlaps = overlaps;
22344 glyph = s->row->glyphs[s->area] + start;
22345 last = s->row->glyphs[s->area] + end;
22346 s->cmp_id = glyph->u.cmp.id;
22347 s->cmp_from = glyph->slice.cmp.from;
22348 s->cmp_to = glyph->slice.cmp.to + 1;
22349 s->face = FACE_FROM_ID (s->f, face_id);
22350 lgstring = composition_gstring_from_id (s->cmp_id);
22351 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22352 glyph++;
22353 while (glyph < last
22354 && glyph->u.cmp.automatic
22355 && glyph->u.cmp.id == s->cmp_id
22356 && s->cmp_to == glyph->slice.cmp.from)
22357 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22358
22359 for (i = s->cmp_from; i < s->cmp_to; i++)
22360 {
22361 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22362 unsigned code = LGLYPH_CODE (lglyph);
22363
22364 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22365 }
22366 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22367 return glyph - s->row->glyphs[s->area];
22368 }
22369
22370
22371 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22372 See the comment of fill_glyph_string for arguments.
22373 Value is the index of the first glyph not in S. */
22374
22375
22376 static int
22377 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22378 int start, int end, int overlaps)
22379 {
22380 struct glyph *glyph, *last;
22381 int voffset;
22382
22383 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22384 s->for_overlaps = overlaps;
22385 glyph = s->row->glyphs[s->area] + start;
22386 last = s->row->glyphs[s->area] + end;
22387 voffset = glyph->voffset;
22388 s->face = FACE_FROM_ID (s->f, face_id);
22389 s->font = s->face->font;
22390 s->nchars = 1;
22391 s->width = glyph->pixel_width;
22392 glyph++;
22393 while (glyph < last
22394 && glyph->type == GLYPHLESS_GLYPH
22395 && glyph->voffset == voffset
22396 && glyph->face_id == face_id)
22397 {
22398 s->nchars++;
22399 s->width += glyph->pixel_width;
22400 glyph++;
22401 }
22402 s->ybase += voffset;
22403 return glyph - s->row->glyphs[s->area];
22404 }
22405
22406
22407 /* Fill glyph string S from a sequence of character glyphs.
22408
22409 FACE_ID is the face id of the string. START is the index of the
22410 first glyph to consider, END is the index of the last + 1.
22411 OVERLAPS non-zero means S should draw the foreground only, and use
22412 its physical height for clipping. See also draw_glyphs.
22413
22414 Value is the index of the first glyph not in S. */
22415
22416 static int
22417 fill_glyph_string (struct glyph_string *s, int face_id,
22418 int start, int end, int overlaps)
22419 {
22420 struct glyph *glyph, *last;
22421 int voffset;
22422 int glyph_not_available_p;
22423
22424 xassert (s->f == XFRAME (s->w->frame));
22425 xassert (s->nchars == 0);
22426 xassert (start >= 0 && end > start);
22427
22428 s->for_overlaps = overlaps;
22429 glyph = s->row->glyphs[s->area] + start;
22430 last = s->row->glyphs[s->area] + end;
22431 voffset = glyph->voffset;
22432 s->padding_p = glyph->padding_p;
22433 glyph_not_available_p = glyph->glyph_not_available_p;
22434
22435 while (glyph < last
22436 && glyph->type == CHAR_GLYPH
22437 && glyph->voffset == voffset
22438 /* Same face id implies same font, nowadays. */
22439 && glyph->face_id == face_id
22440 && glyph->glyph_not_available_p == glyph_not_available_p)
22441 {
22442 int two_byte_p;
22443
22444 s->face = get_glyph_face_and_encoding (s->f, glyph,
22445 s->char2b + s->nchars,
22446 &two_byte_p);
22447 s->two_byte_p = two_byte_p;
22448 ++s->nchars;
22449 xassert (s->nchars <= end - start);
22450 s->width += glyph->pixel_width;
22451 if (glyph++->padding_p != s->padding_p)
22452 break;
22453 }
22454
22455 s->font = s->face->font;
22456
22457 /* If the specified font could not be loaded, use the frame's font,
22458 but record the fact that we couldn't load it in
22459 S->font_not_found_p so that we can draw rectangles for the
22460 characters of the glyph string. */
22461 if (s->font == NULL || glyph_not_available_p)
22462 {
22463 s->font_not_found_p = 1;
22464 s->font = FRAME_FONT (s->f);
22465 }
22466
22467 /* Adjust base line for subscript/superscript text. */
22468 s->ybase += voffset;
22469
22470 xassert (s->face && s->face->gc);
22471 return glyph - s->row->glyphs[s->area];
22472 }
22473
22474
22475 /* Fill glyph string S from image glyph S->first_glyph. */
22476
22477 static void
22478 fill_image_glyph_string (struct glyph_string *s)
22479 {
22480 xassert (s->first_glyph->type == IMAGE_GLYPH);
22481 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22482 xassert (s->img);
22483 s->slice = s->first_glyph->slice.img;
22484 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22485 s->font = s->face->font;
22486 s->width = s->first_glyph->pixel_width;
22487
22488 /* Adjust base line for subscript/superscript text. */
22489 s->ybase += s->first_glyph->voffset;
22490 }
22491
22492
22493 /* Fill glyph string S from a sequence of stretch glyphs.
22494
22495 START is the index of the first glyph to consider,
22496 END is the index of the last + 1.
22497
22498 Value is the index of the first glyph not in S. */
22499
22500 static int
22501 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22502 {
22503 struct glyph *glyph, *last;
22504 int voffset, face_id;
22505
22506 xassert (s->first_glyph->type == STRETCH_GLYPH);
22507
22508 glyph = s->row->glyphs[s->area] + start;
22509 last = s->row->glyphs[s->area] + end;
22510 face_id = glyph->face_id;
22511 s->face = FACE_FROM_ID (s->f, face_id);
22512 s->font = s->face->font;
22513 s->width = glyph->pixel_width;
22514 s->nchars = 1;
22515 voffset = glyph->voffset;
22516
22517 for (++glyph;
22518 (glyph < last
22519 && glyph->type == STRETCH_GLYPH
22520 && glyph->voffset == voffset
22521 && glyph->face_id == face_id);
22522 ++glyph)
22523 s->width += glyph->pixel_width;
22524
22525 /* Adjust base line for subscript/superscript text. */
22526 s->ybase += voffset;
22527
22528 /* The case that face->gc == 0 is handled when drawing the glyph
22529 string by calling PREPARE_FACE_FOR_DISPLAY. */
22530 xassert (s->face);
22531 return glyph - s->row->glyphs[s->area];
22532 }
22533
22534 static struct font_metrics *
22535 get_per_char_metric (struct font *font, XChar2b *char2b)
22536 {
22537 static struct font_metrics metrics;
22538 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22539
22540 if (! font || code == FONT_INVALID_CODE)
22541 return NULL;
22542 font->driver->text_extents (font, &code, 1, &metrics);
22543 return &metrics;
22544 }
22545
22546 /* EXPORT for RIF:
22547 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22548 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22549 assumed to be zero. */
22550
22551 void
22552 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22553 {
22554 *left = *right = 0;
22555
22556 if (glyph->type == CHAR_GLYPH)
22557 {
22558 struct face *face;
22559 XChar2b char2b;
22560 struct font_metrics *pcm;
22561
22562 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22563 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22564 {
22565 if (pcm->rbearing > pcm->width)
22566 *right = pcm->rbearing - pcm->width;
22567 if (pcm->lbearing < 0)
22568 *left = -pcm->lbearing;
22569 }
22570 }
22571 else if (glyph->type == COMPOSITE_GLYPH)
22572 {
22573 if (! glyph->u.cmp.automatic)
22574 {
22575 struct composition *cmp = composition_table[glyph->u.cmp.id];
22576
22577 if (cmp->rbearing > cmp->pixel_width)
22578 *right = cmp->rbearing - cmp->pixel_width;
22579 if (cmp->lbearing < 0)
22580 *left = - cmp->lbearing;
22581 }
22582 else
22583 {
22584 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22585 struct font_metrics metrics;
22586
22587 composition_gstring_width (gstring, glyph->slice.cmp.from,
22588 glyph->slice.cmp.to + 1, &metrics);
22589 if (metrics.rbearing > metrics.width)
22590 *right = metrics.rbearing - metrics.width;
22591 if (metrics.lbearing < 0)
22592 *left = - metrics.lbearing;
22593 }
22594 }
22595 }
22596
22597
22598 /* Return the index of the first glyph preceding glyph string S that
22599 is overwritten by S because of S's left overhang. Value is -1
22600 if no glyphs are overwritten. */
22601
22602 static int
22603 left_overwritten (struct glyph_string *s)
22604 {
22605 int k;
22606
22607 if (s->left_overhang)
22608 {
22609 int x = 0, i;
22610 struct glyph *glyphs = s->row->glyphs[s->area];
22611 int first = s->first_glyph - glyphs;
22612
22613 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22614 x -= glyphs[i].pixel_width;
22615
22616 k = i + 1;
22617 }
22618 else
22619 k = -1;
22620
22621 return k;
22622 }
22623
22624
22625 /* Return the index of the first glyph preceding glyph string S that
22626 is overwriting S because of its right overhang. Value is -1 if no
22627 glyph in front of S overwrites S. */
22628
22629 static int
22630 left_overwriting (struct glyph_string *s)
22631 {
22632 int i, k, x;
22633 struct glyph *glyphs = s->row->glyphs[s->area];
22634 int first = s->first_glyph - glyphs;
22635
22636 k = -1;
22637 x = 0;
22638 for (i = first - 1; i >= 0; --i)
22639 {
22640 int left, right;
22641 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22642 if (x + right > 0)
22643 k = i;
22644 x -= glyphs[i].pixel_width;
22645 }
22646
22647 return k;
22648 }
22649
22650
22651 /* Return the index of the last glyph following glyph string S that is
22652 overwritten by S because of S's right overhang. Value is -1 if
22653 no such glyph is found. */
22654
22655 static int
22656 right_overwritten (struct glyph_string *s)
22657 {
22658 int k = -1;
22659
22660 if (s->right_overhang)
22661 {
22662 int x = 0, i;
22663 struct glyph *glyphs = s->row->glyphs[s->area];
22664 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22665 int end = s->row->used[s->area];
22666
22667 for (i = first; i < end && s->right_overhang > x; ++i)
22668 x += glyphs[i].pixel_width;
22669
22670 k = i;
22671 }
22672
22673 return k;
22674 }
22675
22676
22677 /* Return the index of the last glyph following glyph string S that
22678 overwrites S because of its left overhang. Value is negative
22679 if no such glyph is found. */
22680
22681 static int
22682 right_overwriting (struct glyph_string *s)
22683 {
22684 int i, k, x;
22685 int end = s->row->used[s->area];
22686 struct glyph *glyphs = s->row->glyphs[s->area];
22687 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22688
22689 k = -1;
22690 x = 0;
22691 for (i = first; i < end; ++i)
22692 {
22693 int left, right;
22694 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22695 if (x - left < 0)
22696 k = i;
22697 x += glyphs[i].pixel_width;
22698 }
22699
22700 return k;
22701 }
22702
22703
22704 /* Set background width of glyph string S. START is the index of the
22705 first glyph following S. LAST_X is the right-most x-position + 1
22706 in the drawing area. */
22707
22708 static inline void
22709 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22710 {
22711 /* If the face of this glyph string has to be drawn to the end of
22712 the drawing area, set S->extends_to_end_of_line_p. */
22713
22714 if (start == s->row->used[s->area]
22715 && s->area == TEXT_AREA
22716 && ((s->row->fill_line_p
22717 && (s->hl == DRAW_NORMAL_TEXT
22718 || s->hl == DRAW_IMAGE_RAISED
22719 || s->hl == DRAW_IMAGE_SUNKEN))
22720 || s->hl == DRAW_MOUSE_FACE))
22721 s->extends_to_end_of_line_p = 1;
22722
22723 /* If S extends its face to the end of the line, set its
22724 background_width to the distance to the right edge of the drawing
22725 area. */
22726 if (s->extends_to_end_of_line_p)
22727 s->background_width = last_x - s->x + 1;
22728 else
22729 s->background_width = s->width;
22730 }
22731
22732
22733 /* Compute overhangs and x-positions for glyph string S and its
22734 predecessors, or successors. X is the starting x-position for S.
22735 BACKWARD_P non-zero means process predecessors. */
22736
22737 static void
22738 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22739 {
22740 if (backward_p)
22741 {
22742 while (s)
22743 {
22744 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22745 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22746 x -= s->width;
22747 s->x = x;
22748 s = s->prev;
22749 }
22750 }
22751 else
22752 {
22753 while (s)
22754 {
22755 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22756 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22757 s->x = x;
22758 x += s->width;
22759 s = s->next;
22760 }
22761 }
22762 }
22763
22764
22765
22766 /* The following macros are only called from draw_glyphs below.
22767 They reference the following parameters of that function directly:
22768 `w', `row', `area', and `overlap_p'
22769 as well as the following local variables:
22770 `s', `f', and `hdc' (in W32) */
22771
22772 #ifdef HAVE_NTGUI
22773 /* On W32, silently add local `hdc' variable to argument list of
22774 init_glyph_string. */
22775 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22776 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22777 #else
22778 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22779 init_glyph_string (s, char2b, w, row, area, start, hl)
22780 #endif
22781
22782 /* Add a glyph string for a stretch glyph to the list of strings
22783 between HEAD and TAIL. START is the index of the stretch glyph in
22784 row area AREA of glyph row ROW. END is the index of the last glyph
22785 in that glyph row area. X is the current output position assigned
22786 to the new glyph string constructed. HL overrides that face of the
22787 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22788 is the right-most x-position of the drawing area. */
22789
22790 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22791 and below -- keep them on one line. */
22792 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22793 do \
22794 { \
22795 s = (struct glyph_string *) alloca (sizeof *s); \
22796 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22797 START = fill_stretch_glyph_string (s, START, END); \
22798 append_glyph_string (&HEAD, &TAIL, s); \
22799 s->x = (X); \
22800 } \
22801 while (0)
22802
22803
22804 /* Add a glyph string for an image glyph to the list of strings
22805 between HEAD and TAIL. START is the index of the image glyph in
22806 row area AREA of glyph row ROW. END is the index of the last glyph
22807 in that glyph row area. X is the current output position assigned
22808 to the new glyph string constructed. HL overrides that face of the
22809 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22810 is the right-most x-position of the drawing area. */
22811
22812 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22813 do \
22814 { \
22815 s = (struct glyph_string *) alloca (sizeof *s); \
22816 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22817 fill_image_glyph_string (s); \
22818 append_glyph_string (&HEAD, &TAIL, s); \
22819 ++START; \
22820 s->x = (X); \
22821 } \
22822 while (0)
22823
22824
22825 /* Add a glyph string for a sequence of character glyphs to the list
22826 of strings between HEAD and TAIL. START is the index of the first
22827 glyph in row area AREA of glyph row ROW that is part of the new
22828 glyph string. END is the index of the last glyph in that glyph row
22829 area. X is the current output position assigned to the new glyph
22830 string constructed. HL overrides that face of the glyph; e.g. it
22831 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22832 right-most x-position of the drawing area. */
22833
22834 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22835 do \
22836 { \
22837 int face_id; \
22838 XChar2b *char2b; \
22839 \
22840 face_id = (row)->glyphs[area][START].face_id; \
22841 \
22842 s = (struct glyph_string *) alloca (sizeof *s); \
22843 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22844 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22845 append_glyph_string (&HEAD, &TAIL, s); \
22846 s->x = (X); \
22847 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22848 } \
22849 while (0)
22850
22851
22852 /* Add a glyph string for a composite sequence to the list of strings
22853 between HEAD and TAIL. START is the index of the first glyph in
22854 row area AREA of glyph row ROW that is part of the new glyph
22855 string. END is the index of the last glyph in that glyph row area.
22856 X is the current output position assigned to the new glyph string
22857 constructed. HL overrides that face of the glyph; e.g. it is
22858 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22859 x-position of the drawing area. */
22860
22861 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22862 do { \
22863 int face_id = (row)->glyphs[area][START].face_id; \
22864 struct face *base_face = FACE_FROM_ID (f, face_id); \
22865 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22866 struct composition *cmp = composition_table[cmp_id]; \
22867 XChar2b *char2b; \
22868 struct glyph_string *first_s = NULL; \
22869 int n; \
22870 \
22871 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22872 \
22873 /* Make glyph_strings for each glyph sequence that is drawable by \
22874 the same face, and append them to HEAD/TAIL. */ \
22875 for (n = 0; n < cmp->glyph_len;) \
22876 { \
22877 s = (struct glyph_string *) alloca (sizeof *s); \
22878 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22879 append_glyph_string (&(HEAD), &(TAIL), s); \
22880 s->cmp = cmp; \
22881 s->cmp_from = n; \
22882 s->x = (X); \
22883 if (n == 0) \
22884 first_s = s; \
22885 n = fill_composite_glyph_string (s, base_face, overlaps); \
22886 } \
22887 \
22888 ++START; \
22889 s = first_s; \
22890 } while (0)
22891
22892
22893 /* Add a glyph string for a glyph-string sequence to the list of strings
22894 between HEAD and TAIL. */
22895
22896 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22897 do { \
22898 int face_id; \
22899 XChar2b *char2b; \
22900 Lisp_Object gstring; \
22901 \
22902 face_id = (row)->glyphs[area][START].face_id; \
22903 gstring = (composition_gstring_from_id \
22904 ((row)->glyphs[area][START].u.cmp.id)); \
22905 s = (struct glyph_string *) alloca (sizeof *s); \
22906 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22907 * LGSTRING_GLYPH_LEN (gstring)); \
22908 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22909 append_glyph_string (&(HEAD), &(TAIL), s); \
22910 s->x = (X); \
22911 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22912 } while (0)
22913
22914
22915 /* Add a glyph string for a sequence of glyphless character's glyphs
22916 to the list of strings between HEAD and TAIL. The meanings of
22917 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22918
22919 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22920 do \
22921 { \
22922 int face_id; \
22923 \
22924 face_id = (row)->glyphs[area][START].face_id; \
22925 \
22926 s = (struct glyph_string *) alloca (sizeof *s); \
22927 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22928 append_glyph_string (&HEAD, &TAIL, s); \
22929 s->x = (X); \
22930 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22931 overlaps); \
22932 } \
22933 while (0)
22934
22935
22936 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22937 of AREA of glyph row ROW on window W between indices START and END.
22938 HL overrides the face for drawing glyph strings, e.g. it is
22939 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22940 x-positions of the drawing area.
22941
22942 This is an ugly monster macro construct because we must use alloca
22943 to allocate glyph strings (because draw_glyphs can be called
22944 asynchronously). */
22945
22946 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22947 do \
22948 { \
22949 HEAD = TAIL = NULL; \
22950 while (START < END) \
22951 { \
22952 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22953 switch (first_glyph->type) \
22954 { \
22955 case CHAR_GLYPH: \
22956 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22957 HL, X, LAST_X); \
22958 break; \
22959 \
22960 case COMPOSITE_GLYPH: \
22961 if (first_glyph->u.cmp.automatic) \
22962 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22963 HL, X, LAST_X); \
22964 else \
22965 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22966 HL, X, LAST_X); \
22967 break; \
22968 \
22969 case STRETCH_GLYPH: \
22970 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22971 HL, X, LAST_X); \
22972 break; \
22973 \
22974 case IMAGE_GLYPH: \
22975 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22976 HL, X, LAST_X); \
22977 break; \
22978 \
22979 case GLYPHLESS_GLYPH: \
22980 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22981 HL, X, LAST_X); \
22982 break; \
22983 \
22984 default: \
22985 abort (); \
22986 } \
22987 \
22988 if (s) \
22989 { \
22990 set_glyph_string_background_width (s, START, LAST_X); \
22991 (X) += s->width; \
22992 } \
22993 } \
22994 } while (0)
22995
22996
22997 /* Draw glyphs between START and END in AREA of ROW on window W,
22998 starting at x-position X. X is relative to AREA in W. HL is a
22999 face-override with the following meaning:
23000
23001 DRAW_NORMAL_TEXT draw normally
23002 DRAW_CURSOR draw in cursor face
23003 DRAW_MOUSE_FACE draw in mouse face.
23004 DRAW_INVERSE_VIDEO draw in mode line face
23005 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23006 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23007
23008 If OVERLAPS is non-zero, draw only the foreground of characters and
23009 clip to the physical height of ROW. Non-zero value also defines
23010 the overlapping part to be drawn:
23011
23012 OVERLAPS_PRED overlap with preceding rows
23013 OVERLAPS_SUCC overlap with succeeding rows
23014 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23015 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23016
23017 Value is the x-position reached, relative to AREA of W. */
23018
23019 static int
23020 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23021 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23022 enum draw_glyphs_face hl, int overlaps)
23023 {
23024 struct glyph_string *head, *tail;
23025 struct glyph_string *s;
23026 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23027 int i, j, x_reached, last_x, area_left = 0;
23028 struct frame *f = XFRAME (WINDOW_FRAME (w));
23029 DECLARE_HDC (hdc);
23030
23031 ALLOCATE_HDC (hdc, f);
23032
23033 /* Let's rather be paranoid than getting a SEGV. */
23034 end = min (end, row->used[area]);
23035 start = max (0, start);
23036 start = min (end, start);
23037
23038 /* Translate X to frame coordinates. Set last_x to the right
23039 end of the drawing area. */
23040 if (row->full_width_p)
23041 {
23042 /* X is relative to the left edge of W, without scroll bars
23043 or fringes. */
23044 area_left = WINDOW_LEFT_EDGE_X (w);
23045 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23046 }
23047 else
23048 {
23049 area_left = window_box_left (w, area);
23050 last_x = area_left + window_box_width (w, area);
23051 }
23052 x += area_left;
23053
23054 /* Build a doubly-linked list of glyph_string structures between
23055 head and tail from what we have to draw. Note that the macro
23056 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23057 the reason we use a separate variable `i'. */
23058 i = start;
23059 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23060 if (tail)
23061 x_reached = tail->x + tail->background_width;
23062 else
23063 x_reached = x;
23064
23065 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23066 the row, redraw some glyphs in front or following the glyph
23067 strings built above. */
23068 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23069 {
23070 struct glyph_string *h, *t;
23071 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23072 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23073 int check_mouse_face = 0;
23074 int dummy_x = 0;
23075
23076 /* If mouse highlighting is on, we may need to draw adjacent
23077 glyphs using mouse-face highlighting. */
23078 if (area == TEXT_AREA && row->mouse_face_p)
23079 {
23080 struct glyph_row *mouse_beg_row, *mouse_end_row;
23081
23082 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23083 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23084
23085 if (row >= mouse_beg_row && row <= mouse_end_row)
23086 {
23087 check_mouse_face = 1;
23088 mouse_beg_col = (row == mouse_beg_row)
23089 ? hlinfo->mouse_face_beg_col : 0;
23090 mouse_end_col = (row == mouse_end_row)
23091 ? hlinfo->mouse_face_end_col
23092 : row->used[TEXT_AREA];
23093 }
23094 }
23095
23096 /* Compute overhangs for all glyph strings. */
23097 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23098 for (s = head; s; s = s->next)
23099 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23100
23101 /* Prepend glyph strings for glyphs in front of the first glyph
23102 string that are overwritten because of the first glyph
23103 string's left overhang. The background of all strings
23104 prepended must be drawn because the first glyph string
23105 draws over it. */
23106 i = left_overwritten (head);
23107 if (i >= 0)
23108 {
23109 enum draw_glyphs_face overlap_hl;
23110
23111 /* If this row contains mouse highlighting, attempt to draw
23112 the overlapped glyphs with the correct highlight. This
23113 code fails if the overlap encompasses more than one glyph
23114 and mouse-highlight spans only some of these glyphs.
23115 However, making it work perfectly involves a lot more
23116 code, and I don't know if the pathological case occurs in
23117 practice, so we'll stick to this for now. --- cyd */
23118 if (check_mouse_face
23119 && mouse_beg_col < start && mouse_end_col > i)
23120 overlap_hl = DRAW_MOUSE_FACE;
23121 else
23122 overlap_hl = DRAW_NORMAL_TEXT;
23123
23124 j = i;
23125 BUILD_GLYPH_STRINGS (j, start, h, t,
23126 overlap_hl, dummy_x, last_x);
23127 start = i;
23128 compute_overhangs_and_x (t, head->x, 1);
23129 prepend_glyph_string_lists (&head, &tail, h, t);
23130 clip_head = head;
23131 }
23132
23133 /* Prepend glyph strings for glyphs in front of the first glyph
23134 string that overwrite that glyph string because of their
23135 right overhang. For these strings, only the foreground must
23136 be drawn, because it draws over the glyph string at `head'.
23137 The background must not be drawn because this would overwrite
23138 right overhangs of preceding glyphs for which no glyph
23139 strings exist. */
23140 i = left_overwriting (head);
23141 if (i >= 0)
23142 {
23143 enum draw_glyphs_face overlap_hl;
23144
23145 if (check_mouse_face
23146 && mouse_beg_col < start && mouse_end_col > i)
23147 overlap_hl = DRAW_MOUSE_FACE;
23148 else
23149 overlap_hl = DRAW_NORMAL_TEXT;
23150
23151 clip_head = head;
23152 BUILD_GLYPH_STRINGS (i, start, h, t,
23153 overlap_hl, dummy_x, last_x);
23154 for (s = h; s; s = s->next)
23155 s->background_filled_p = 1;
23156 compute_overhangs_and_x (t, head->x, 1);
23157 prepend_glyph_string_lists (&head, &tail, h, t);
23158 }
23159
23160 /* Append glyphs strings for glyphs following the last glyph
23161 string tail that are overwritten by tail. The background of
23162 these strings has to be drawn because tail's foreground draws
23163 over it. */
23164 i = right_overwritten (tail);
23165 if (i >= 0)
23166 {
23167 enum draw_glyphs_face overlap_hl;
23168
23169 if (check_mouse_face
23170 && mouse_beg_col < i && mouse_end_col > end)
23171 overlap_hl = DRAW_MOUSE_FACE;
23172 else
23173 overlap_hl = DRAW_NORMAL_TEXT;
23174
23175 BUILD_GLYPH_STRINGS (end, i, h, t,
23176 overlap_hl, x, last_x);
23177 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23178 we don't have `end = i;' here. */
23179 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23180 append_glyph_string_lists (&head, &tail, h, t);
23181 clip_tail = tail;
23182 }
23183
23184 /* Append glyph strings for glyphs following the last glyph
23185 string tail that overwrite tail. The foreground of such
23186 glyphs has to be drawn because it writes into the background
23187 of tail. The background must not be drawn because it could
23188 paint over the foreground of following glyphs. */
23189 i = right_overwriting (tail);
23190 if (i >= 0)
23191 {
23192 enum draw_glyphs_face overlap_hl;
23193 if (check_mouse_face
23194 && mouse_beg_col < i && mouse_end_col > end)
23195 overlap_hl = DRAW_MOUSE_FACE;
23196 else
23197 overlap_hl = DRAW_NORMAL_TEXT;
23198
23199 clip_tail = tail;
23200 i++; /* We must include the Ith glyph. */
23201 BUILD_GLYPH_STRINGS (end, i, h, t,
23202 overlap_hl, x, last_x);
23203 for (s = h; s; s = s->next)
23204 s->background_filled_p = 1;
23205 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23206 append_glyph_string_lists (&head, &tail, h, t);
23207 }
23208 if (clip_head || clip_tail)
23209 for (s = head; s; s = s->next)
23210 {
23211 s->clip_head = clip_head;
23212 s->clip_tail = clip_tail;
23213 }
23214 }
23215
23216 /* Draw all strings. */
23217 for (s = head; s; s = s->next)
23218 FRAME_RIF (f)->draw_glyph_string (s);
23219
23220 #ifndef HAVE_NS
23221 /* When focus a sole frame and move horizontally, this sets on_p to 0
23222 causing a failure to erase prev cursor position. */
23223 if (area == TEXT_AREA
23224 && !row->full_width_p
23225 /* When drawing overlapping rows, only the glyph strings'
23226 foreground is drawn, which doesn't erase a cursor
23227 completely. */
23228 && !overlaps)
23229 {
23230 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23231 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23232 : (tail ? tail->x + tail->background_width : x));
23233 x0 -= area_left;
23234 x1 -= area_left;
23235
23236 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23237 row->y, MATRIX_ROW_BOTTOM_Y (row));
23238 }
23239 #endif
23240
23241 /* Value is the x-position up to which drawn, relative to AREA of W.
23242 This doesn't include parts drawn because of overhangs. */
23243 if (row->full_width_p)
23244 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23245 else
23246 x_reached -= area_left;
23247
23248 RELEASE_HDC (hdc, f);
23249
23250 return x_reached;
23251 }
23252
23253 /* Expand row matrix if too narrow. Don't expand if area
23254 is not present. */
23255
23256 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23257 { \
23258 if (!fonts_changed_p \
23259 && (it->glyph_row->glyphs[area] \
23260 < it->glyph_row->glyphs[area + 1])) \
23261 { \
23262 it->w->ncols_scale_factor++; \
23263 fonts_changed_p = 1; \
23264 } \
23265 }
23266
23267 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23268 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23269
23270 static inline void
23271 append_glyph (struct it *it)
23272 {
23273 struct glyph *glyph;
23274 enum glyph_row_area area = it->area;
23275
23276 xassert (it->glyph_row);
23277 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23278
23279 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23280 if (glyph < it->glyph_row->glyphs[area + 1])
23281 {
23282 /* If the glyph row is reversed, we need to prepend the glyph
23283 rather than append it. */
23284 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23285 {
23286 struct glyph *g;
23287
23288 /* Make room for the additional glyph. */
23289 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23290 g[1] = *g;
23291 glyph = it->glyph_row->glyphs[area];
23292 }
23293 glyph->charpos = CHARPOS (it->position);
23294 glyph->object = it->object;
23295 if (it->pixel_width > 0)
23296 {
23297 glyph->pixel_width = it->pixel_width;
23298 glyph->padding_p = 0;
23299 }
23300 else
23301 {
23302 /* Assure at least 1-pixel width. Otherwise, cursor can't
23303 be displayed correctly. */
23304 glyph->pixel_width = 1;
23305 glyph->padding_p = 1;
23306 }
23307 glyph->ascent = it->ascent;
23308 glyph->descent = it->descent;
23309 glyph->voffset = it->voffset;
23310 glyph->type = CHAR_GLYPH;
23311 glyph->avoid_cursor_p = it->avoid_cursor_p;
23312 glyph->multibyte_p = it->multibyte_p;
23313 glyph->left_box_line_p = it->start_of_box_run_p;
23314 glyph->right_box_line_p = it->end_of_box_run_p;
23315 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23316 || it->phys_descent > it->descent);
23317 glyph->glyph_not_available_p = it->glyph_not_available_p;
23318 glyph->face_id = it->face_id;
23319 glyph->u.ch = it->char_to_display;
23320 glyph->slice.img = null_glyph_slice;
23321 glyph->font_type = FONT_TYPE_UNKNOWN;
23322 if (it->bidi_p)
23323 {
23324 glyph->resolved_level = it->bidi_it.resolved_level;
23325 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23326 abort ();
23327 glyph->bidi_type = it->bidi_it.type;
23328 }
23329 else
23330 {
23331 glyph->resolved_level = 0;
23332 glyph->bidi_type = UNKNOWN_BT;
23333 }
23334 ++it->glyph_row->used[area];
23335 }
23336 else
23337 IT_EXPAND_MATRIX_WIDTH (it, area);
23338 }
23339
23340 /* Store one glyph for the composition IT->cmp_it.id in
23341 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23342 non-null. */
23343
23344 static inline void
23345 append_composite_glyph (struct it *it)
23346 {
23347 struct glyph *glyph;
23348 enum glyph_row_area area = it->area;
23349
23350 xassert (it->glyph_row);
23351
23352 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23353 if (glyph < it->glyph_row->glyphs[area + 1])
23354 {
23355 /* If the glyph row is reversed, we need to prepend the glyph
23356 rather than append it. */
23357 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23358 {
23359 struct glyph *g;
23360
23361 /* Make room for the new glyph. */
23362 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23363 g[1] = *g;
23364 glyph = it->glyph_row->glyphs[it->area];
23365 }
23366 glyph->charpos = it->cmp_it.charpos;
23367 glyph->object = it->object;
23368 glyph->pixel_width = it->pixel_width;
23369 glyph->ascent = it->ascent;
23370 glyph->descent = it->descent;
23371 glyph->voffset = it->voffset;
23372 glyph->type = COMPOSITE_GLYPH;
23373 if (it->cmp_it.ch < 0)
23374 {
23375 glyph->u.cmp.automatic = 0;
23376 glyph->u.cmp.id = it->cmp_it.id;
23377 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23378 }
23379 else
23380 {
23381 glyph->u.cmp.automatic = 1;
23382 glyph->u.cmp.id = it->cmp_it.id;
23383 glyph->slice.cmp.from = it->cmp_it.from;
23384 glyph->slice.cmp.to = it->cmp_it.to - 1;
23385 }
23386 glyph->avoid_cursor_p = it->avoid_cursor_p;
23387 glyph->multibyte_p = it->multibyte_p;
23388 glyph->left_box_line_p = it->start_of_box_run_p;
23389 glyph->right_box_line_p = it->end_of_box_run_p;
23390 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23391 || it->phys_descent > it->descent);
23392 glyph->padding_p = 0;
23393 glyph->glyph_not_available_p = 0;
23394 glyph->face_id = it->face_id;
23395 glyph->font_type = FONT_TYPE_UNKNOWN;
23396 if (it->bidi_p)
23397 {
23398 glyph->resolved_level = it->bidi_it.resolved_level;
23399 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23400 abort ();
23401 glyph->bidi_type = it->bidi_it.type;
23402 }
23403 ++it->glyph_row->used[area];
23404 }
23405 else
23406 IT_EXPAND_MATRIX_WIDTH (it, area);
23407 }
23408
23409
23410 /* Change IT->ascent and IT->height according to the setting of
23411 IT->voffset. */
23412
23413 static inline void
23414 take_vertical_position_into_account (struct it *it)
23415 {
23416 if (it->voffset)
23417 {
23418 if (it->voffset < 0)
23419 /* Increase the ascent so that we can display the text higher
23420 in the line. */
23421 it->ascent -= it->voffset;
23422 else
23423 /* Increase the descent so that we can display the text lower
23424 in the line. */
23425 it->descent += it->voffset;
23426 }
23427 }
23428
23429
23430 /* Produce glyphs/get display metrics for the image IT is loaded with.
23431 See the description of struct display_iterator in dispextern.h for
23432 an overview of struct display_iterator. */
23433
23434 static void
23435 produce_image_glyph (struct it *it)
23436 {
23437 struct image *img;
23438 struct face *face;
23439 int glyph_ascent, crop;
23440 struct glyph_slice slice;
23441
23442 xassert (it->what == IT_IMAGE);
23443
23444 face = FACE_FROM_ID (it->f, it->face_id);
23445 xassert (face);
23446 /* Make sure X resources of the face is loaded. */
23447 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23448
23449 if (it->image_id < 0)
23450 {
23451 /* Fringe bitmap. */
23452 it->ascent = it->phys_ascent = 0;
23453 it->descent = it->phys_descent = 0;
23454 it->pixel_width = 0;
23455 it->nglyphs = 0;
23456 return;
23457 }
23458
23459 img = IMAGE_FROM_ID (it->f, it->image_id);
23460 xassert (img);
23461 /* Make sure X resources of the image is loaded. */
23462 prepare_image_for_display (it->f, img);
23463
23464 slice.x = slice.y = 0;
23465 slice.width = img->width;
23466 slice.height = img->height;
23467
23468 if (INTEGERP (it->slice.x))
23469 slice.x = XINT (it->slice.x);
23470 else if (FLOATP (it->slice.x))
23471 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23472
23473 if (INTEGERP (it->slice.y))
23474 slice.y = XINT (it->slice.y);
23475 else if (FLOATP (it->slice.y))
23476 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23477
23478 if (INTEGERP (it->slice.width))
23479 slice.width = XINT (it->slice.width);
23480 else if (FLOATP (it->slice.width))
23481 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23482
23483 if (INTEGERP (it->slice.height))
23484 slice.height = XINT (it->slice.height);
23485 else if (FLOATP (it->slice.height))
23486 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23487
23488 if (slice.x >= img->width)
23489 slice.x = img->width;
23490 if (slice.y >= img->height)
23491 slice.y = img->height;
23492 if (slice.x + slice.width >= img->width)
23493 slice.width = img->width - slice.x;
23494 if (slice.y + slice.height > img->height)
23495 slice.height = img->height - slice.y;
23496
23497 if (slice.width == 0 || slice.height == 0)
23498 return;
23499
23500 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23501
23502 it->descent = slice.height - glyph_ascent;
23503 if (slice.y == 0)
23504 it->descent += img->vmargin;
23505 if (slice.y + slice.height == img->height)
23506 it->descent += img->vmargin;
23507 it->phys_descent = it->descent;
23508
23509 it->pixel_width = slice.width;
23510 if (slice.x == 0)
23511 it->pixel_width += img->hmargin;
23512 if (slice.x + slice.width == img->width)
23513 it->pixel_width += img->hmargin;
23514
23515 /* It's quite possible for images to have an ascent greater than
23516 their height, so don't get confused in that case. */
23517 if (it->descent < 0)
23518 it->descent = 0;
23519
23520 it->nglyphs = 1;
23521
23522 if (face->box != FACE_NO_BOX)
23523 {
23524 if (face->box_line_width > 0)
23525 {
23526 if (slice.y == 0)
23527 it->ascent += face->box_line_width;
23528 if (slice.y + slice.height == img->height)
23529 it->descent += face->box_line_width;
23530 }
23531
23532 if (it->start_of_box_run_p && slice.x == 0)
23533 it->pixel_width += eabs (face->box_line_width);
23534 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23535 it->pixel_width += eabs (face->box_line_width);
23536 }
23537
23538 take_vertical_position_into_account (it);
23539
23540 /* Automatically crop wide image glyphs at right edge so we can
23541 draw the cursor on same display row. */
23542 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23543 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23544 {
23545 it->pixel_width -= crop;
23546 slice.width -= crop;
23547 }
23548
23549 if (it->glyph_row)
23550 {
23551 struct glyph *glyph;
23552 enum glyph_row_area area = it->area;
23553
23554 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23555 if (glyph < it->glyph_row->glyphs[area + 1])
23556 {
23557 glyph->charpos = CHARPOS (it->position);
23558 glyph->object = it->object;
23559 glyph->pixel_width = it->pixel_width;
23560 glyph->ascent = glyph_ascent;
23561 glyph->descent = it->descent;
23562 glyph->voffset = it->voffset;
23563 glyph->type = IMAGE_GLYPH;
23564 glyph->avoid_cursor_p = it->avoid_cursor_p;
23565 glyph->multibyte_p = it->multibyte_p;
23566 glyph->left_box_line_p = it->start_of_box_run_p;
23567 glyph->right_box_line_p = it->end_of_box_run_p;
23568 glyph->overlaps_vertically_p = 0;
23569 glyph->padding_p = 0;
23570 glyph->glyph_not_available_p = 0;
23571 glyph->face_id = it->face_id;
23572 glyph->u.img_id = img->id;
23573 glyph->slice.img = slice;
23574 glyph->font_type = FONT_TYPE_UNKNOWN;
23575 if (it->bidi_p)
23576 {
23577 glyph->resolved_level = it->bidi_it.resolved_level;
23578 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23579 abort ();
23580 glyph->bidi_type = it->bidi_it.type;
23581 }
23582 ++it->glyph_row->used[area];
23583 }
23584 else
23585 IT_EXPAND_MATRIX_WIDTH (it, area);
23586 }
23587 }
23588
23589
23590 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23591 of the glyph, WIDTH and HEIGHT are the width and height of the
23592 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23593
23594 static void
23595 append_stretch_glyph (struct it *it, Lisp_Object object,
23596 int width, int height, int ascent)
23597 {
23598 struct glyph *glyph;
23599 enum glyph_row_area area = it->area;
23600
23601 xassert (ascent >= 0 && ascent <= height);
23602
23603 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23604 if (glyph < it->glyph_row->glyphs[area + 1])
23605 {
23606 /* If the glyph row is reversed, we need to prepend the glyph
23607 rather than append it. */
23608 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23609 {
23610 struct glyph *g;
23611
23612 /* Make room for the additional glyph. */
23613 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23614 g[1] = *g;
23615 glyph = it->glyph_row->glyphs[area];
23616 }
23617 glyph->charpos = CHARPOS (it->position);
23618 glyph->object = object;
23619 glyph->pixel_width = width;
23620 glyph->ascent = ascent;
23621 glyph->descent = height - ascent;
23622 glyph->voffset = it->voffset;
23623 glyph->type = STRETCH_GLYPH;
23624 glyph->avoid_cursor_p = it->avoid_cursor_p;
23625 glyph->multibyte_p = it->multibyte_p;
23626 glyph->left_box_line_p = it->start_of_box_run_p;
23627 glyph->right_box_line_p = it->end_of_box_run_p;
23628 glyph->overlaps_vertically_p = 0;
23629 glyph->padding_p = 0;
23630 glyph->glyph_not_available_p = 0;
23631 glyph->face_id = it->face_id;
23632 glyph->u.stretch.ascent = ascent;
23633 glyph->u.stretch.height = height;
23634 glyph->slice.img = null_glyph_slice;
23635 glyph->font_type = FONT_TYPE_UNKNOWN;
23636 if (it->bidi_p)
23637 {
23638 glyph->resolved_level = it->bidi_it.resolved_level;
23639 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23640 abort ();
23641 glyph->bidi_type = it->bidi_it.type;
23642 }
23643 else
23644 {
23645 glyph->resolved_level = 0;
23646 glyph->bidi_type = UNKNOWN_BT;
23647 }
23648 ++it->glyph_row->used[area];
23649 }
23650 else
23651 IT_EXPAND_MATRIX_WIDTH (it, area);
23652 }
23653
23654 #endif /* HAVE_WINDOW_SYSTEM */
23655
23656 /* Produce a stretch glyph for iterator IT. IT->object is the value
23657 of the glyph property displayed. The value must be a list
23658 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23659 being recognized:
23660
23661 1. `:width WIDTH' specifies that the space should be WIDTH *
23662 canonical char width wide. WIDTH may be an integer or floating
23663 point number.
23664
23665 2. `:relative-width FACTOR' specifies that the width of the stretch
23666 should be computed from the width of the first character having the
23667 `glyph' property, and should be FACTOR times that width.
23668
23669 3. `:align-to HPOS' specifies that the space should be wide enough
23670 to reach HPOS, a value in canonical character units.
23671
23672 Exactly one of the above pairs must be present.
23673
23674 4. `:height HEIGHT' specifies that the height of the stretch produced
23675 should be HEIGHT, measured in canonical character units.
23676
23677 5. `:relative-height FACTOR' specifies that the height of the
23678 stretch should be FACTOR times the height of the characters having
23679 the glyph property.
23680
23681 Either none or exactly one of 4 or 5 must be present.
23682
23683 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23684 of the stretch should be used for the ascent of the stretch.
23685 ASCENT must be in the range 0 <= ASCENT <= 100. */
23686
23687 void
23688 produce_stretch_glyph (struct it *it)
23689 {
23690 /* (space :width WIDTH :height HEIGHT ...) */
23691 Lisp_Object prop, plist;
23692 int width = 0, height = 0, align_to = -1;
23693 int zero_width_ok_p = 0;
23694 int ascent = 0;
23695 double tem;
23696 struct face *face = NULL;
23697 struct font *font = NULL;
23698
23699 #ifdef HAVE_WINDOW_SYSTEM
23700 int zero_height_ok_p = 0;
23701
23702 if (FRAME_WINDOW_P (it->f))
23703 {
23704 face = FACE_FROM_ID (it->f, it->face_id);
23705 font = face->font ? face->font : FRAME_FONT (it->f);
23706 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23707 }
23708 #endif
23709
23710 /* List should start with `space'. */
23711 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23712 plist = XCDR (it->object);
23713
23714 /* Compute the width of the stretch. */
23715 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23716 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23717 {
23718 /* Absolute width `:width WIDTH' specified and valid. */
23719 zero_width_ok_p = 1;
23720 width = (int)tem;
23721 }
23722 #ifdef HAVE_WINDOW_SYSTEM
23723 else if (FRAME_WINDOW_P (it->f)
23724 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23725 {
23726 /* Relative width `:relative-width FACTOR' specified and valid.
23727 Compute the width of the characters having the `glyph'
23728 property. */
23729 struct it it2;
23730 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23731
23732 it2 = *it;
23733 if (it->multibyte_p)
23734 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23735 else
23736 {
23737 it2.c = it2.char_to_display = *p, it2.len = 1;
23738 if (! ASCII_CHAR_P (it2.c))
23739 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23740 }
23741
23742 it2.glyph_row = NULL;
23743 it2.what = IT_CHARACTER;
23744 x_produce_glyphs (&it2);
23745 width = NUMVAL (prop) * it2.pixel_width;
23746 }
23747 #endif /* HAVE_WINDOW_SYSTEM */
23748 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23749 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23750 {
23751 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23752 align_to = (align_to < 0
23753 ? 0
23754 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23755 else if (align_to < 0)
23756 align_to = window_box_left_offset (it->w, TEXT_AREA);
23757 width = max (0, (int)tem + align_to - it->current_x);
23758 zero_width_ok_p = 1;
23759 }
23760 else
23761 /* Nothing specified -> width defaults to canonical char width. */
23762 width = FRAME_COLUMN_WIDTH (it->f);
23763
23764 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23765 width = 1;
23766
23767 #ifdef HAVE_WINDOW_SYSTEM
23768 /* Compute height. */
23769 if (FRAME_WINDOW_P (it->f))
23770 {
23771 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23772 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23773 {
23774 height = (int)tem;
23775 zero_height_ok_p = 1;
23776 }
23777 else if (prop = Fplist_get (plist, QCrelative_height),
23778 NUMVAL (prop) > 0)
23779 height = FONT_HEIGHT (font) * NUMVAL (prop);
23780 else
23781 height = FONT_HEIGHT (font);
23782
23783 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23784 height = 1;
23785
23786 /* Compute percentage of height used for ascent. If
23787 `:ascent ASCENT' is present and valid, use that. Otherwise,
23788 derive the ascent from the font in use. */
23789 if (prop = Fplist_get (plist, QCascent),
23790 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23791 ascent = height * NUMVAL (prop) / 100.0;
23792 else if (!NILP (prop)
23793 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23794 ascent = min (max (0, (int)tem), height);
23795 else
23796 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23797 }
23798 else
23799 #endif /* HAVE_WINDOW_SYSTEM */
23800 height = 1;
23801
23802 if (width > 0 && it->line_wrap != TRUNCATE
23803 && it->current_x + width > it->last_visible_x)
23804 {
23805 width = it->last_visible_x - it->current_x;
23806 #ifdef HAVE_WINDOW_SYSTEM
23807 /* Subtract one more pixel from the stretch width, but only on
23808 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23809 width -= FRAME_WINDOW_P (it->f);
23810 #endif
23811 }
23812
23813 if (width > 0 && height > 0 && it->glyph_row)
23814 {
23815 Lisp_Object o_object = it->object;
23816 Lisp_Object object = it->stack[it->sp - 1].string;
23817 int n = width;
23818
23819 if (!STRINGP (object))
23820 object = it->w->buffer;
23821 #ifdef HAVE_WINDOW_SYSTEM
23822 if (FRAME_WINDOW_P (it->f))
23823 append_stretch_glyph (it, object, width, height, ascent);
23824 else
23825 #endif
23826 {
23827 it->object = object;
23828 it->char_to_display = ' ';
23829 it->pixel_width = it->len = 1;
23830 while (n--)
23831 tty_append_glyph (it);
23832 it->object = o_object;
23833 }
23834 }
23835
23836 it->pixel_width = width;
23837 #ifdef HAVE_WINDOW_SYSTEM
23838 if (FRAME_WINDOW_P (it->f))
23839 {
23840 it->ascent = it->phys_ascent = ascent;
23841 it->descent = it->phys_descent = height - it->ascent;
23842 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23843 take_vertical_position_into_account (it);
23844 }
23845 else
23846 #endif
23847 it->nglyphs = width;
23848 }
23849
23850 #ifdef HAVE_WINDOW_SYSTEM
23851
23852 /* Calculate line-height and line-spacing properties.
23853 An integer value specifies explicit pixel value.
23854 A float value specifies relative value to current face height.
23855 A cons (float . face-name) specifies relative value to
23856 height of specified face font.
23857
23858 Returns height in pixels, or nil. */
23859
23860
23861 static Lisp_Object
23862 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23863 int boff, int override)
23864 {
23865 Lisp_Object face_name = Qnil;
23866 int ascent, descent, height;
23867
23868 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23869 return val;
23870
23871 if (CONSP (val))
23872 {
23873 face_name = XCAR (val);
23874 val = XCDR (val);
23875 if (!NUMBERP (val))
23876 val = make_number (1);
23877 if (NILP (face_name))
23878 {
23879 height = it->ascent + it->descent;
23880 goto scale;
23881 }
23882 }
23883
23884 if (NILP (face_name))
23885 {
23886 font = FRAME_FONT (it->f);
23887 boff = FRAME_BASELINE_OFFSET (it->f);
23888 }
23889 else if (EQ (face_name, Qt))
23890 {
23891 override = 0;
23892 }
23893 else
23894 {
23895 int face_id;
23896 struct face *face;
23897
23898 face_id = lookup_named_face (it->f, face_name, 0);
23899 if (face_id < 0)
23900 return make_number (-1);
23901
23902 face = FACE_FROM_ID (it->f, face_id);
23903 font = face->font;
23904 if (font == NULL)
23905 return make_number (-1);
23906 boff = font->baseline_offset;
23907 if (font->vertical_centering)
23908 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23909 }
23910
23911 ascent = FONT_BASE (font) + boff;
23912 descent = FONT_DESCENT (font) - boff;
23913
23914 if (override)
23915 {
23916 it->override_ascent = ascent;
23917 it->override_descent = descent;
23918 it->override_boff = boff;
23919 }
23920
23921 height = ascent + descent;
23922
23923 scale:
23924 if (FLOATP (val))
23925 height = (int)(XFLOAT_DATA (val) * height);
23926 else if (INTEGERP (val))
23927 height *= XINT (val);
23928
23929 return make_number (height);
23930 }
23931
23932
23933 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23934 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23935 and only if this is for a character for which no font was found.
23936
23937 If the display method (it->glyphless_method) is
23938 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23939 length of the acronym or the hexadecimal string, UPPER_XOFF and
23940 UPPER_YOFF are pixel offsets for the upper part of the string,
23941 LOWER_XOFF and LOWER_YOFF are for the lower part.
23942
23943 For the other display methods, LEN through LOWER_YOFF are zero. */
23944
23945 static void
23946 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23947 short upper_xoff, short upper_yoff,
23948 short lower_xoff, short lower_yoff)
23949 {
23950 struct glyph *glyph;
23951 enum glyph_row_area area = it->area;
23952
23953 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23954 if (glyph < it->glyph_row->glyphs[area + 1])
23955 {
23956 /* If the glyph row is reversed, we need to prepend the glyph
23957 rather than append it. */
23958 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23959 {
23960 struct glyph *g;
23961
23962 /* Make room for the additional glyph. */
23963 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23964 g[1] = *g;
23965 glyph = it->glyph_row->glyphs[area];
23966 }
23967 glyph->charpos = CHARPOS (it->position);
23968 glyph->object = it->object;
23969 glyph->pixel_width = it->pixel_width;
23970 glyph->ascent = it->ascent;
23971 glyph->descent = it->descent;
23972 glyph->voffset = it->voffset;
23973 glyph->type = GLYPHLESS_GLYPH;
23974 glyph->u.glyphless.method = it->glyphless_method;
23975 glyph->u.glyphless.for_no_font = for_no_font;
23976 glyph->u.glyphless.len = len;
23977 glyph->u.glyphless.ch = it->c;
23978 glyph->slice.glyphless.upper_xoff = upper_xoff;
23979 glyph->slice.glyphless.upper_yoff = upper_yoff;
23980 glyph->slice.glyphless.lower_xoff = lower_xoff;
23981 glyph->slice.glyphless.lower_yoff = lower_yoff;
23982 glyph->avoid_cursor_p = it->avoid_cursor_p;
23983 glyph->multibyte_p = it->multibyte_p;
23984 glyph->left_box_line_p = it->start_of_box_run_p;
23985 glyph->right_box_line_p = it->end_of_box_run_p;
23986 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23987 || it->phys_descent > it->descent);
23988 glyph->padding_p = 0;
23989 glyph->glyph_not_available_p = 0;
23990 glyph->face_id = face_id;
23991 glyph->font_type = FONT_TYPE_UNKNOWN;
23992 if (it->bidi_p)
23993 {
23994 glyph->resolved_level = it->bidi_it.resolved_level;
23995 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23996 abort ();
23997 glyph->bidi_type = it->bidi_it.type;
23998 }
23999 ++it->glyph_row->used[area];
24000 }
24001 else
24002 IT_EXPAND_MATRIX_WIDTH (it, area);
24003 }
24004
24005
24006 /* Produce a glyph for a glyphless character for iterator IT.
24007 IT->glyphless_method specifies which method to use for displaying
24008 the character. See the description of enum
24009 glyphless_display_method in dispextern.h for the detail.
24010
24011 FOR_NO_FONT is nonzero if and only if this is for a character for
24012 which no font was found. ACRONYM, if non-nil, is an acronym string
24013 for the character. */
24014
24015 static void
24016 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24017 {
24018 int face_id;
24019 struct face *face;
24020 struct font *font;
24021 int base_width, base_height, width, height;
24022 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24023 int len;
24024
24025 /* Get the metrics of the base font. We always refer to the current
24026 ASCII face. */
24027 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24028 font = face->font ? face->font : FRAME_FONT (it->f);
24029 it->ascent = FONT_BASE (font) + font->baseline_offset;
24030 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24031 base_height = it->ascent + it->descent;
24032 base_width = font->average_width;
24033
24034 /* Get a face ID for the glyph by utilizing a cache (the same way as
24035 done for `escape-glyph' in get_next_display_element). */
24036 if (it->f == last_glyphless_glyph_frame
24037 && it->face_id == last_glyphless_glyph_face_id)
24038 {
24039 face_id = last_glyphless_glyph_merged_face_id;
24040 }
24041 else
24042 {
24043 /* Merge the `glyphless-char' face into the current face. */
24044 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24045 last_glyphless_glyph_frame = it->f;
24046 last_glyphless_glyph_face_id = it->face_id;
24047 last_glyphless_glyph_merged_face_id = face_id;
24048 }
24049
24050 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24051 {
24052 it->pixel_width = THIN_SPACE_WIDTH;
24053 len = 0;
24054 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24055 }
24056 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24057 {
24058 width = CHAR_WIDTH (it->c);
24059 if (width == 0)
24060 width = 1;
24061 else if (width > 4)
24062 width = 4;
24063 it->pixel_width = base_width * width;
24064 len = 0;
24065 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24066 }
24067 else
24068 {
24069 char buf[7];
24070 const char *str;
24071 unsigned int code[6];
24072 int upper_len;
24073 int ascent, descent;
24074 struct font_metrics metrics_upper, metrics_lower;
24075
24076 face = FACE_FROM_ID (it->f, face_id);
24077 font = face->font ? face->font : FRAME_FONT (it->f);
24078 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24079
24080 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24081 {
24082 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24083 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24084 if (CONSP (acronym))
24085 acronym = XCAR (acronym);
24086 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24087 }
24088 else
24089 {
24090 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24091 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24092 str = buf;
24093 }
24094 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24095 code[len] = font->driver->encode_char (font, str[len]);
24096 upper_len = (len + 1) / 2;
24097 font->driver->text_extents (font, code, upper_len,
24098 &metrics_upper);
24099 font->driver->text_extents (font, code + upper_len, len - upper_len,
24100 &metrics_lower);
24101
24102
24103
24104 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24105 width = max (metrics_upper.width, metrics_lower.width) + 4;
24106 upper_xoff = upper_yoff = 2; /* the typical case */
24107 if (base_width >= width)
24108 {
24109 /* Align the upper to the left, the lower to the right. */
24110 it->pixel_width = base_width;
24111 lower_xoff = base_width - 2 - metrics_lower.width;
24112 }
24113 else
24114 {
24115 /* Center the shorter one. */
24116 it->pixel_width = width;
24117 if (metrics_upper.width >= metrics_lower.width)
24118 lower_xoff = (width - metrics_lower.width) / 2;
24119 else
24120 {
24121 /* FIXME: This code doesn't look right. It formerly was
24122 missing the "lower_xoff = 0;", which couldn't have
24123 been right since it left lower_xoff uninitialized. */
24124 lower_xoff = 0;
24125 upper_xoff = (width - metrics_upper.width) / 2;
24126 }
24127 }
24128
24129 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24130 top, bottom, and between upper and lower strings. */
24131 height = (metrics_upper.ascent + metrics_upper.descent
24132 + metrics_lower.ascent + metrics_lower.descent) + 5;
24133 /* Center vertically.
24134 H:base_height, D:base_descent
24135 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24136
24137 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24138 descent = D - H/2 + h/2;
24139 lower_yoff = descent - 2 - ld;
24140 upper_yoff = lower_yoff - la - 1 - ud; */
24141 ascent = - (it->descent - (base_height + height + 1) / 2);
24142 descent = it->descent - (base_height - height) / 2;
24143 lower_yoff = descent - 2 - metrics_lower.descent;
24144 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24145 - metrics_upper.descent);
24146 /* Don't make the height shorter than the base height. */
24147 if (height > base_height)
24148 {
24149 it->ascent = ascent;
24150 it->descent = descent;
24151 }
24152 }
24153
24154 it->phys_ascent = it->ascent;
24155 it->phys_descent = it->descent;
24156 if (it->glyph_row)
24157 append_glyphless_glyph (it, face_id, for_no_font, len,
24158 upper_xoff, upper_yoff,
24159 lower_xoff, lower_yoff);
24160 it->nglyphs = 1;
24161 take_vertical_position_into_account (it);
24162 }
24163
24164
24165 /* RIF:
24166 Produce glyphs/get display metrics for the display element IT is
24167 loaded with. See the description of struct it in dispextern.h
24168 for an overview of struct it. */
24169
24170 void
24171 x_produce_glyphs (struct it *it)
24172 {
24173 int extra_line_spacing = it->extra_line_spacing;
24174
24175 it->glyph_not_available_p = 0;
24176
24177 if (it->what == IT_CHARACTER)
24178 {
24179 XChar2b char2b;
24180 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24181 struct font *font = face->font;
24182 struct font_metrics *pcm = NULL;
24183 int boff; /* baseline offset */
24184
24185 if (font == NULL)
24186 {
24187 /* When no suitable font is found, display this character by
24188 the method specified in the first extra slot of
24189 Vglyphless_char_display. */
24190 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24191
24192 xassert (it->what == IT_GLYPHLESS);
24193 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24194 goto done;
24195 }
24196
24197 boff = font->baseline_offset;
24198 if (font->vertical_centering)
24199 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24200
24201 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24202 {
24203 int stretched_p;
24204
24205 it->nglyphs = 1;
24206
24207 if (it->override_ascent >= 0)
24208 {
24209 it->ascent = it->override_ascent;
24210 it->descent = it->override_descent;
24211 boff = it->override_boff;
24212 }
24213 else
24214 {
24215 it->ascent = FONT_BASE (font) + boff;
24216 it->descent = FONT_DESCENT (font) - boff;
24217 }
24218
24219 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24220 {
24221 pcm = get_per_char_metric (font, &char2b);
24222 if (pcm->width == 0
24223 && pcm->rbearing == 0 && pcm->lbearing == 0)
24224 pcm = NULL;
24225 }
24226
24227 if (pcm)
24228 {
24229 it->phys_ascent = pcm->ascent + boff;
24230 it->phys_descent = pcm->descent - boff;
24231 it->pixel_width = pcm->width;
24232 }
24233 else
24234 {
24235 it->glyph_not_available_p = 1;
24236 it->phys_ascent = it->ascent;
24237 it->phys_descent = it->descent;
24238 it->pixel_width = font->space_width;
24239 }
24240
24241 if (it->constrain_row_ascent_descent_p)
24242 {
24243 if (it->descent > it->max_descent)
24244 {
24245 it->ascent += it->descent - it->max_descent;
24246 it->descent = it->max_descent;
24247 }
24248 if (it->ascent > it->max_ascent)
24249 {
24250 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24251 it->ascent = it->max_ascent;
24252 }
24253 it->phys_ascent = min (it->phys_ascent, it->ascent);
24254 it->phys_descent = min (it->phys_descent, it->descent);
24255 extra_line_spacing = 0;
24256 }
24257
24258 /* If this is a space inside a region of text with
24259 `space-width' property, change its width. */
24260 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24261 if (stretched_p)
24262 it->pixel_width *= XFLOATINT (it->space_width);
24263
24264 /* If face has a box, add the box thickness to the character
24265 height. If character has a box line to the left and/or
24266 right, add the box line width to the character's width. */
24267 if (face->box != FACE_NO_BOX)
24268 {
24269 int thick = face->box_line_width;
24270
24271 if (thick > 0)
24272 {
24273 it->ascent += thick;
24274 it->descent += thick;
24275 }
24276 else
24277 thick = -thick;
24278
24279 if (it->start_of_box_run_p)
24280 it->pixel_width += thick;
24281 if (it->end_of_box_run_p)
24282 it->pixel_width += thick;
24283 }
24284
24285 /* If face has an overline, add the height of the overline
24286 (1 pixel) and a 1 pixel margin to the character height. */
24287 if (face->overline_p)
24288 it->ascent += overline_margin;
24289
24290 if (it->constrain_row_ascent_descent_p)
24291 {
24292 if (it->ascent > it->max_ascent)
24293 it->ascent = it->max_ascent;
24294 if (it->descent > it->max_descent)
24295 it->descent = it->max_descent;
24296 }
24297
24298 take_vertical_position_into_account (it);
24299
24300 /* If we have to actually produce glyphs, do it. */
24301 if (it->glyph_row)
24302 {
24303 if (stretched_p)
24304 {
24305 /* Translate a space with a `space-width' property
24306 into a stretch glyph. */
24307 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24308 / FONT_HEIGHT (font));
24309 append_stretch_glyph (it, it->object, it->pixel_width,
24310 it->ascent + it->descent, ascent);
24311 }
24312 else
24313 append_glyph (it);
24314
24315 /* If characters with lbearing or rbearing are displayed
24316 in this line, record that fact in a flag of the
24317 glyph row. This is used to optimize X output code. */
24318 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24319 it->glyph_row->contains_overlapping_glyphs_p = 1;
24320 }
24321 if (! stretched_p && it->pixel_width == 0)
24322 /* We assure that all visible glyphs have at least 1-pixel
24323 width. */
24324 it->pixel_width = 1;
24325 }
24326 else if (it->char_to_display == '\n')
24327 {
24328 /* A newline has no width, but we need the height of the
24329 line. But if previous part of the line sets a height,
24330 don't increase that height */
24331
24332 Lisp_Object height;
24333 Lisp_Object total_height = Qnil;
24334
24335 it->override_ascent = -1;
24336 it->pixel_width = 0;
24337 it->nglyphs = 0;
24338
24339 height = get_it_property (it, Qline_height);
24340 /* Split (line-height total-height) list */
24341 if (CONSP (height)
24342 && CONSP (XCDR (height))
24343 && NILP (XCDR (XCDR (height))))
24344 {
24345 total_height = XCAR (XCDR (height));
24346 height = XCAR (height);
24347 }
24348 height = calc_line_height_property (it, height, font, boff, 1);
24349
24350 if (it->override_ascent >= 0)
24351 {
24352 it->ascent = it->override_ascent;
24353 it->descent = it->override_descent;
24354 boff = it->override_boff;
24355 }
24356 else
24357 {
24358 it->ascent = FONT_BASE (font) + boff;
24359 it->descent = FONT_DESCENT (font) - boff;
24360 }
24361
24362 if (EQ (height, Qt))
24363 {
24364 if (it->descent > it->max_descent)
24365 {
24366 it->ascent += it->descent - it->max_descent;
24367 it->descent = it->max_descent;
24368 }
24369 if (it->ascent > it->max_ascent)
24370 {
24371 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24372 it->ascent = it->max_ascent;
24373 }
24374 it->phys_ascent = min (it->phys_ascent, it->ascent);
24375 it->phys_descent = min (it->phys_descent, it->descent);
24376 it->constrain_row_ascent_descent_p = 1;
24377 extra_line_spacing = 0;
24378 }
24379 else
24380 {
24381 Lisp_Object spacing;
24382
24383 it->phys_ascent = it->ascent;
24384 it->phys_descent = it->descent;
24385
24386 if ((it->max_ascent > 0 || it->max_descent > 0)
24387 && face->box != FACE_NO_BOX
24388 && face->box_line_width > 0)
24389 {
24390 it->ascent += face->box_line_width;
24391 it->descent += face->box_line_width;
24392 }
24393 if (!NILP (height)
24394 && XINT (height) > it->ascent + it->descent)
24395 it->ascent = XINT (height) - it->descent;
24396
24397 if (!NILP (total_height))
24398 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24399 else
24400 {
24401 spacing = get_it_property (it, Qline_spacing);
24402 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24403 }
24404 if (INTEGERP (spacing))
24405 {
24406 extra_line_spacing = XINT (spacing);
24407 if (!NILP (total_height))
24408 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24409 }
24410 }
24411 }
24412 else /* i.e. (it->char_to_display == '\t') */
24413 {
24414 if (font->space_width > 0)
24415 {
24416 int tab_width = it->tab_width * font->space_width;
24417 int x = it->current_x + it->continuation_lines_width;
24418 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24419
24420 /* If the distance from the current position to the next tab
24421 stop is less than a space character width, use the
24422 tab stop after that. */
24423 if (next_tab_x - x < font->space_width)
24424 next_tab_x += tab_width;
24425
24426 it->pixel_width = next_tab_x - x;
24427 it->nglyphs = 1;
24428 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24429 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24430
24431 if (it->glyph_row)
24432 {
24433 append_stretch_glyph (it, it->object, it->pixel_width,
24434 it->ascent + it->descent, it->ascent);
24435 }
24436 }
24437 else
24438 {
24439 it->pixel_width = 0;
24440 it->nglyphs = 1;
24441 }
24442 }
24443 }
24444 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24445 {
24446 /* A static composition.
24447
24448 Note: A composition is represented as one glyph in the
24449 glyph matrix. There are no padding glyphs.
24450
24451 Important note: pixel_width, ascent, and descent are the
24452 values of what is drawn by draw_glyphs (i.e. the values of
24453 the overall glyphs composed). */
24454 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24455 int boff; /* baseline offset */
24456 struct composition *cmp = composition_table[it->cmp_it.id];
24457 int glyph_len = cmp->glyph_len;
24458 struct font *font = face->font;
24459
24460 it->nglyphs = 1;
24461
24462 /* If we have not yet calculated pixel size data of glyphs of
24463 the composition for the current face font, calculate them
24464 now. Theoretically, we have to check all fonts for the
24465 glyphs, but that requires much time and memory space. So,
24466 here we check only the font of the first glyph. This may
24467 lead to incorrect display, but it's very rare, and C-l
24468 (recenter-top-bottom) can correct the display anyway. */
24469 if (! cmp->font || cmp->font != font)
24470 {
24471 /* Ascent and descent of the font of the first character
24472 of this composition (adjusted by baseline offset).
24473 Ascent and descent of overall glyphs should not be less
24474 than these, respectively. */
24475 int font_ascent, font_descent, font_height;
24476 /* Bounding box of the overall glyphs. */
24477 int leftmost, rightmost, lowest, highest;
24478 int lbearing, rbearing;
24479 int i, width, ascent, descent;
24480 int left_padded = 0, right_padded = 0;
24481 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24482 XChar2b char2b;
24483 struct font_metrics *pcm;
24484 int font_not_found_p;
24485 ptrdiff_t pos;
24486
24487 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24488 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24489 break;
24490 if (glyph_len < cmp->glyph_len)
24491 right_padded = 1;
24492 for (i = 0; i < glyph_len; i++)
24493 {
24494 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24495 break;
24496 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24497 }
24498 if (i > 0)
24499 left_padded = 1;
24500
24501 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24502 : IT_CHARPOS (*it));
24503 /* If no suitable font is found, use the default font. */
24504 font_not_found_p = font == NULL;
24505 if (font_not_found_p)
24506 {
24507 face = face->ascii_face;
24508 font = face->font;
24509 }
24510 boff = font->baseline_offset;
24511 if (font->vertical_centering)
24512 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24513 font_ascent = FONT_BASE (font) + boff;
24514 font_descent = FONT_DESCENT (font) - boff;
24515 font_height = FONT_HEIGHT (font);
24516
24517 cmp->font = (void *) font;
24518
24519 pcm = NULL;
24520 if (! font_not_found_p)
24521 {
24522 get_char_face_and_encoding (it->f, c, it->face_id,
24523 &char2b, 0);
24524 pcm = get_per_char_metric (font, &char2b);
24525 }
24526
24527 /* Initialize the bounding box. */
24528 if (pcm)
24529 {
24530 width = cmp->glyph_len > 0 ? pcm->width : 0;
24531 ascent = pcm->ascent;
24532 descent = pcm->descent;
24533 lbearing = pcm->lbearing;
24534 rbearing = pcm->rbearing;
24535 }
24536 else
24537 {
24538 width = cmp->glyph_len > 0 ? font->space_width : 0;
24539 ascent = FONT_BASE (font);
24540 descent = FONT_DESCENT (font);
24541 lbearing = 0;
24542 rbearing = width;
24543 }
24544
24545 rightmost = width;
24546 leftmost = 0;
24547 lowest = - descent + boff;
24548 highest = ascent + boff;
24549
24550 if (! font_not_found_p
24551 && font->default_ascent
24552 && CHAR_TABLE_P (Vuse_default_ascent)
24553 && !NILP (Faref (Vuse_default_ascent,
24554 make_number (it->char_to_display))))
24555 highest = font->default_ascent + boff;
24556
24557 /* Draw the first glyph at the normal position. It may be
24558 shifted to right later if some other glyphs are drawn
24559 at the left. */
24560 cmp->offsets[i * 2] = 0;
24561 cmp->offsets[i * 2 + 1] = boff;
24562 cmp->lbearing = lbearing;
24563 cmp->rbearing = rbearing;
24564
24565 /* Set cmp->offsets for the remaining glyphs. */
24566 for (i++; i < glyph_len; i++)
24567 {
24568 int left, right, btm, top;
24569 int ch = COMPOSITION_GLYPH (cmp, i);
24570 int face_id;
24571 struct face *this_face;
24572
24573 if (ch == '\t')
24574 ch = ' ';
24575 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24576 this_face = FACE_FROM_ID (it->f, face_id);
24577 font = this_face->font;
24578
24579 if (font == NULL)
24580 pcm = NULL;
24581 else
24582 {
24583 get_char_face_and_encoding (it->f, ch, face_id,
24584 &char2b, 0);
24585 pcm = get_per_char_metric (font, &char2b);
24586 }
24587 if (! pcm)
24588 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24589 else
24590 {
24591 width = pcm->width;
24592 ascent = pcm->ascent;
24593 descent = pcm->descent;
24594 lbearing = pcm->lbearing;
24595 rbearing = pcm->rbearing;
24596 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24597 {
24598 /* Relative composition with or without
24599 alternate chars. */
24600 left = (leftmost + rightmost - width) / 2;
24601 btm = - descent + boff;
24602 if (font->relative_compose
24603 && (! CHAR_TABLE_P (Vignore_relative_composition)
24604 || NILP (Faref (Vignore_relative_composition,
24605 make_number (ch)))))
24606 {
24607
24608 if (- descent >= font->relative_compose)
24609 /* One extra pixel between two glyphs. */
24610 btm = highest + 1;
24611 else if (ascent <= 0)
24612 /* One extra pixel between two glyphs. */
24613 btm = lowest - 1 - ascent - descent;
24614 }
24615 }
24616 else
24617 {
24618 /* A composition rule is specified by an integer
24619 value that encodes global and new reference
24620 points (GREF and NREF). GREF and NREF are
24621 specified by numbers as below:
24622
24623 0---1---2 -- ascent
24624 | |
24625 | |
24626 | |
24627 9--10--11 -- center
24628 | |
24629 ---3---4---5--- baseline
24630 | |
24631 6---7---8 -- descent
24632 */
24633 int rule = COMPOSITION_RULE (cmp, i);
24634 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24635
24636 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24637 grefx = gref % 3, nrefx = nref % 3;
24638 grefy = gref / 3, nrefy = nref / 3;
24639 if (xoff)
24640 xoff = font_height * (xoff - 128) / 256;
24641 if (yoff)
24642 yoff = font_height * (yoff - 128) / 256;
24643
24644 left = (leftmost
24645 + grefx * (rightmost - leftmost) / 2
24646 - nrefx * width / 2
24647 + xoff);
24648
24649 btm = ((grefy == 0 ? highest
24650 : grefy == 1 ? 0
24651 : grefy == 2 ? lowest
24652 : (highest + lowest) / 2)
24653 - (nrefy == 0 ? ascent + descent
24654 : nrefy == 1 ? descent - boff
24655 : nrefy == 2 ? 0
24656 : (ascent + descent) / 2)
24657 + yoff);
24658 }
24659
24660 cmp->offsets[i * 2] = left;
24661 cmp->offsets[i * 2 + 1] = btm + descent;
24662
24663 /* Update the bounding box of the overall glyphs. */
24664 if (width > 0)
24665 {
24666 right = left + width;
24667 if (left < leftmost)
24668 leftmost = left;
24669 if (right > rightmost)
24670 rightmost = right;
24671 }
24672 top = btm + descent + ascent;
24673 if (top > highest)
24674 highest = top;
24675 if (btm < lowest)
24676 lowest = btm;
24677
24678 if (cmp->lbearing > left + lbearing)
24679 cmp->lbearing = left + lbearing;
24680 if (cmp->rbearing < left + rbearing)
24681 cmp->rbearing = left + rbearing;
24682 }
24683 }
24684
24685 /* If there are glyphs whose x-offsets are negative,
24686 shift all glyphs to the right and make all x-offsets
24687 non-negative. */
24688 if (leftmost < 0)
24689 {
24690 for (i = 0; i < cmp->glyph_len; i++)
24691 cmp->offsets[i * 2] -= leftmost;
24692 rightmost -= leftmost;
24693 cmp->lbearing -= leftmost;
24694 cmp->rbearing -= leftmost;
24695 }
24696
24697 if (left_padded && cmp->lbearing < 0)
24698 {
24699 for (i = 0; i < cmp->glyph_len; i++)
24700 cmp->offsets[i * 2] -= cmp->lbearing;
24701 rightmost -= cmp->lbearing;
24702 cmp->rbearing -= cmp->lbearing;
24703 cmp->lbearing = 0;
24704 }
24705 if (right_padded && rightmost < cmp->rbearing)
24706 {
24707 rightmost = cmp->rbearing;
24708 }
24709
24710 cmp->pixel_width = rightmost;
24711 cmp->ascent = highest;
24712 cmp->descent = - lowest;
24713 if (cmp->ascent < font_ascent)
24714 cmp->ascent = font_ascent;
24715 if (cmp->descent < font_descent)
24716 cmp->descent = font_descent;
24717 }
24718
24719 if (it->glyph_row
24720 && (cmp->lbearing < 0
24721 || cmp->rbearing > cmp->pixel_width))
24722 it->glyph_row->contains_overlapping_glyphs_p = 1;
24723
24724 it->pixel_width = cmp->pixel_width;
24725 it->ascent = it->phys_ascent = cmp->ascent;
24726 it->descent = it->phys_descent = cmp->descent;
24727 if (face->box != FACE_NO_BOX)
24728 {
24729 int thick = face->box_line_width;
24730
24731 if (thick > 0)
24732 {
24733 it->ascent += thick;
24734 it->descent += thick;
24735 }
24736 else
24737 thick = - thick;
24738
24739 if (it->start_of_box_run_p)
24740 it->pixel_width += thick;
24741 if (it->end_of_box_run_p)
24742 it->pixel_width += thick;
24743 }
24744
24745 /* If face has an overline, add the height of the overline
24746 (1 pixel) and a 1 pixel margin to the character height. */
24747 if (face->overline_p)
24748 it->ascent += overline_margin;
24749
24750 take_vertical_position_into_account (it);
24751 if (it->ascent < 0)
24752 it->ascent = 0;
24753 if (it->descent < 0)
24754 it->descent = 0;
24755
24756 if (it->glyph_row && cmp->glyph_len > 0)
24757 append_composite_glyph (it);
24758 }
24759 else if (it->what == IT_COMPOSITION)
24760 {
24761 /* A dynamic (automatic) composition. */
24762 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24763 Lisp_Object gstring;
24764 struct font_metrics metrics;
24765
24766 it->nglyphs = 1;
24767
24768 gstring = composition_gstring_from_id (it->cmp_it.id);
24769 it->pixel_width
24770 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24771 &metrics);
24772 if (it->glyph_row
24773 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24774 it->glyph_row->contains_overlapping_glyphs_p = 1;
24775 it->ascent = it->phys_ascent = metrics.ascent;
24776 it->descent = it->phys_descent = metrics.descent;
24777 if (face->box != FACE_NO_BOX)
24778 {
24779 int thick = face->box_line_width;
24780
24781 if (thick > 0)
24782 {
24783 it->ascent += thick;
24784 it->descent += thick;
24785 }
24786 else
24787 thick = - thick;
24788
24789 if (it->start_of_box_run_p)
24790 it->pixel_width += thick;
24791 if (it->end_of_box_run_p)
24792 it->pixel_width += thick;
24793 }
24794 /* If face has an overline, add the height of the overline
24795 (1 pixel) and a 1 pixel margin to the character height. */
24796 if (face->overline_p)
24797 it->ascent += overline_margin;
24798 take_vertical_position_into_account (it);
24799 if (it->ascent < 0)
24800 it->ascent = 0;
24801 if (it->descent < 0)
24802 it->descent = 0;
24803
24804 if (it->glyph_row)
24805 append_composite_glyph (it);
24806 }
24807 else if (it->what == IT_GLYPHLESS)
24808 produce_glyphless_glyph (it, 0, Qnil);
24809 else if (it->what == IT_IMAGE)
24810 produce_image_glyph (it);
24811 else if (it->what == IT_STRETCH)
24812 produce_stretch_glyph (it);
24813
24814 done:
24815 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24816 because this isn't true for images with `:ascent 100'. */
24817 xassert (it->ascent >= 0 && it->descent >= 0);
24818 if (it->area == TEXT_AREA)
24819 it->current_x += it->pixel_width;
24820
24821 if (extra_line_spacing > 0)
24822 {
24823 it->descent += extra_line_spacing;
24824 if (extra_line_spacing > it->max_extra_line_spacing)
24825 it->max_extra_line_spacing = extra_line_spacing;
24826 }
24827
24828 it->max_ascent = max (it->max_ascent, it->ascent);
24829 it->max_descent = max (it->max_descent, it->descent);
24830 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24831 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24832 }
24833
24834 /* EXPORT for RIF:
24835 Output LEN glyphs starting at START at the nominal cursor position.
24836 Advance the nominal cursor over the text. The global variable
24837 updated_window contains the window being updated, updated_row is
24838 the glyph row being updated, and updated_area is the area of that
24839 row being updated. */
24840
24841 void
24842 x_write_glyphs (struct glyph *start, int len)
24843 {
24844 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24845
24846 xassert (updated_window && updated_row);
24847 /* When the window is hscrolled, cursor hpos can legitimately be out
24848 of bounds, but we draw the cursor at the corresponding window
24849 margin in that case. */
24850 if (!updated_row->reversed_p && chpos < 0)
24851 chpos = 0;
24852 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24853 chpos = updated_row->used[TEXT_AREA] - 1;
24854
24855 BLOCK_INPUT;
24856
24857 /* Write glyphs. */
24858
24859 hpos = start - updated_row->glyphs[updated_area];
24860 x = draw_glyphs (updated_window, output_cursor.x,
24861 updated_row, updated_area,
24862 hpos, hpos + len,
24863 DRAW_NORMAL_TEXT, 0);
24864
24865 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24866 if (updated_area == TEXT_AREA
24867 && updated_window->phys_cursor_on_p
24868 && updated_window->phys_cursor.vpos == output_cursor.vpos
24869 && chpos >= hpos
24870 && chpos < hpos + len)
24871 updated_window->phys_cursor_on_p = 0;
24872
24873 UNBLOCK_INPUT;
24874
24875 /* Advance the output cursor. */
24876 output_cursor.hpos += len;
24877 output_cursor.x = x;
24878 }
24879
24880
24881 /* EXPORT for RIF:
24882 Insert LEN glyphs from START at the nominal cursor position. */
24883
24884 void
24885 x_insert_glyphs (struct glyph *start, int len)
24886 {
24887 struct frame *f;
24888 struct window *w;
24889 int line_height, shift_by_width, shifted_region_width;
24890 struct glyph_row *row;
24891 struct glyph *glyph;
24892 int frame_x, frame_y;
24893 ptrdiff_t hpos;
24894
24895 xassert (updated_window && updated_row);
24896 BLOCK_INPUT;
24897 w = updated_window;
24898 f = XFRAME (WINDOW_FRAME (w));
24899
24900 /* Get the height of the line we are in. */
24901 row = updated_row;
24902 line_height = row->height;
24903
24904 /* Get the width of the glyphs to insert. */
24905 shift_by_width = 0;
24906 for (glyph = start; glyph < start + len; ++glyph)
24907 shift_by_width += glyph->pixel_width;
24908
24909 /* Get the width of the region to shift right. */
24910 shifted_region_width = (window_box_width (w, updated_area)
24911 - output_cursor.x
24912 - shift_by_width);
24913
24914 /* Shift right. */
24915 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24916 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24917
24918 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24919 line_height, shift_by_width);
24920
24921 /* Write the glyphs. */
24922 hpos = start - row->glyphs[updated_area];
24923 draw_glyphs (w, output_cursor.x, row, updated_area,
24924 hpos, hpos + len,
24925 DRAW_NORMAL_TEXT, 0);
24926
24927 /* Advance the output cursor. */
24928 output_cursor.hpos += len;
24929 output_cursor.x += shift_by_width;
24930 UNBLOCK_INPUT;
24931 }
24932
24933
24934 /* EXPORT for RIF:
24935 Erase the current text line from the nominal cursor position
24936 (inclusive) to pixel column TO_X (exclusive). The idea is that
24937 everything from TO_X onward is already erased.
24938
24939 TO_X is a pixel position relative to updated_area of
24940 updated_window. TO_X == -1 means clear to the end of this area. */
24941
24942 void
24943 x_clear_end_of_line (int to_x)
24944 {
24945 struct frame *f;
24946 struct window *w = updated_window;
24947 int max_x, min_y, max_y;
24948 int from_x, from_y, to_y;
24949
24950 xassert (updated_window && updated_row);
24951 f = XFRAME (w->frame);
24952
24953 if (updated_row->full_width_p)
24954 max_x = WINDOW_TOTAL_WIDTH (w);
24955 else
24956 max_x = window_box_width (w, updated_area);
24957 max_y = window_text_bottom_y (w);
24958
24959 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24960 of window. For TO_X > 0, truncate to end of drawing area. */
24961 if (to_x == 0)
24962 return;
24963 else if (to_x < 0)
24964 to_x = max_x;
24965 else
24966 to_x = min (to_x, max_x);
24967
24968 to_y = min (max_y, output_cursor.y + updated_row->height);
24969
24970 /* Notice if the cursor will be cleared by this operation. */
24971 if (!updated_row->full_width_p)
24972 notice_overwritten_cursor (w, updated_area,
24973 output_cursor.x, -1,
24974 updated_row->y,
24975 MATRIX_ROW_BOTTOM_Y (updated_row));
24976
24977 from_x = output_cursor.x;
24978
24979 /* Translate to frame coordinates. */
24980 if (updated_row->full_width_p)
24981 {
24982 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24983 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24984 }
24985 else
24986 {
24987 int area_left = window_box_left (w, updated_area);
24988 from_x += area_left;
24989 to_x += area_left;
24990 }
24991
24992 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24993 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24994 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24995
24996 /* Prevent inadvertently clearing to end of the X window. */
24997 if (to_x > from_x && to_y > from_y)
24998 {
24999 BLOCK_INPUT;
25000 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25001 to_x - from_x, to_y - from_y);
25002 UNBLOCK_INPUT;
25003 }
25004 }
25005
25006 #endif /* HAVE_WINDOW_SYSTEM */
25007
25008
25009 \f
25010 /***********************************************************************
25011 Cursor types
25012 ***********************************************************************/
25013
25014 /* Value is the internal representation of the specified cursor type
25015 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25016 of the bar cursor. */
25017
25018 static enum text_cursor_kinds
25019 get_specified_cursor_type (Lisp_Object arg, int *width)
25020 {
25021 enum text_cursor_kinds type;
25022
25023 if (NILP (arg))
25024 return NO_CURSOR;
25025
25026 if (EQ (arg, Qbox))
25027 return FILLED_BOX_CURSOR;
25028
25029 if (EQ (arg, Qhollow))
25030 return HOLLOW_BOX_CURSOR;
25031
25032 if (EQ (arg, Qbar))
25033 {
25034 *width = 2;
25035 return BAR_CURSOR;
25036 }
25037
25038 if (CONSP (arg)
25039 && EQ (XCAR (arg), Qbar)
25040 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25041 {
25042 *width = XINT (XCDR (arg));
25043 return BAR_CURSOR;
25044 }
25045
25046 if (EQ (arg, Qhbar))
25047 {
25048 *width = 2;
25049 return HBAR_CURSOR;
25050 }
25051
25052 if (CONSP (arg)
25053 && EQ (XCAR (arg), Qhbar)
25054 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25055 {
25056 *width = XINT (XCDR (arg));
25057 return HBAR_CURSOR;
25058 }
25059
25060 /* Treat anything unknown as "hollow box cursor".
25061 It was bad to signal an error; people have trouble fixing
25062 .Xdefaults with Emacs, when it has something bad in it. */
25063 type = HOLLOW_BOX_CURSOR;
25064
25065 return type;
25066 }
25067
25068 /* Set the default cursor types for specified frame. */
25069 void
25070 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25071 {
25072 int width = 1;
25073 Lisp_Object tem;
25074
25075 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25076 FRAME_CURSOR_WIDTH (f) = width;
25077
25078 /* By default, set up the blink-off state depending on the on-state. */
25079
25080 tem = Fassoc (arg, Vblink_cursor_alist);
25081 if (!NILP (tem))
25082 {
25083 FRAME_BLINK_OFF_CURSOR (f)
25084 = get_specified_cursor_type (XCDR (tem), &width);
25085 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25086 }
25087 else
25088 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25089 }
25090
25091
25092 #ifdef HAVE_WINDOW_SYSTEM
25093
25094 /* Return the cursor we want to be displayed in window W. Return
25095 width of bar/hbar cursor through WIDTH arg. Return with
25096 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25097 (i.e. if the `system caret' should track this cursor).
25098
25099 In a mini-buffer window, we want the cursor only to appear if we
25100 are reading input from this window. For the selected window, we
25101 want the cursor type given by the frame parameter or buffer local
25102 setting of cursor-type. If explicitly marked off, draw no cursor.
25103 In all other cases, we want a hollow box cursor. */
25104
25105 static enum text_cursor_kinds
25106 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25107 int *active_cursor)
25108 {
25109 struct frame *f = XFRAME (w->frame);
25110 struct buffer *b = XBUFFER (w->buffer);
25111 int cursor_type = DEFAULT_CURSOR;
25112 Lisp_Object alt_cursor;
25113 int non_selected = 0;
25114
25115 *active_cursor = 1;
25116
25117 /* Echo area */
25118 if (cursor_in_echo_area
25119 && FRAME_HAS_MINIBUF_P (f)
25120 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25121 {
25122 if (w == XWINDOW (echo_area_window))
25123 {
25124 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25125 {
25126 *width = FRAME_CURSOR_WIDTH (f);
25127 return FRAME_DESIRED_CURSOR (f);
25128 }
25129 else
25130 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25131 }
25132
25133 *active_cursor = 0;
25134 non_selected = 1;
25135 }
25136
25137 /* Detect a nonselected window or nonselected frame. */
25138 else if (w != XWINDOW (f->selected_window)
25139 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25140 {
25141 *active_cursor = 0;
25142
25143 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25144 return NO_CURSOR;
25145
25146 non_selected = 1;
25147 }
25148
25149 /* Never display a cursor in a window in which cursor-type is nil. */
25150 if (NILP (BVAR (b, cursor_type)))
25151 return NO_CURSOR;
25152
25153 /* Get the normal cursor type for this window. */
25154 if (EQ (BVAR (b, cursor_type), Qt))
25155 {
25156 cursor_type = FRAME_DESIRED_CURSOR (f);
25157 *width = FRAME_CURSOR_WIDTH (f);
25158 }
25159 else
25160 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25161
25162 /* Use cursor-in-non-selected-windows instead
25163 for non-selected window or frame. */
25164 if (non_selected)
25165 {
25166 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25167 if (!EQ (Qt, alt_cursor))
25168 return get_specified_cursor_type (alt_cursor, width);
25169 /* t means modify the normal cursor type. */
25170 if (cursor_type == FILLED_BOX_CURSOR)
25171 cursor_type = HOLLOW_BOX_CURSOR;
25172 else if (cursor_type == BAR_CURSOR && *width > 1)
25173 --*width;
25174 return cursor_type;
25175 }
25176
25177 /* Use normal cursor if not blinked off. */
25178 if (!w->cursor_off_p)
25179 {
25180 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25181 {
25182 if (cursor_type == FILLED_BOX_CURSOR)
25183 {
25184 /* Using a block cursor on large images can be very annoying.
25185 So use a hollow cursor for "large" images.
25186 If image is not transparent (no mask), also use hollow cursor. */
25187 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25188 if (img != NULL && IMAGEP (img->spec))
25189 {
25190 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25191 where N = size of default frame font size.
25192 This should cover most of the "tiny" icons people may use. */
25193 if (!img->mask
25194 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25195 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25196 cursor_type = HOLLOW_BOX_CURSOR;
25197 }
25198 }
25199 else if (cursor_type != NO_CURSOR)
25200 {
25201 /* Display current only supports BOX and HOLLOW cursors for images.
25202 So for now, unconditionally use a HOLLOW cursor when cursor is
25203 not a solid box cursor. */
25204 cursor_type = HOLLOW_BOX_CURSOR;
25205 }
25206 }
25207 return cursor_type;
25208 }
25209
25210 /* Cursor is blinked off, so determine how to "toggle" it. */
25211
25212 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25213 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25214 return get_specified_cursor_type (XCDR (alt_cursor), width);
25215
25216 /* Then see if frame has specified a specific blink off cursor type. */
25217 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25218 {
25219 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25220 return FRAME_BLINK_OFF_CURSOR (f);
25221 }
25222
25223 #if 0
25224 /* Some people liked having a permanently visible blinking cursor,
25225 while others had very strong opinions against it. So it was
25226 decided to remove it. KFS 2003-09-03 */
25227
25228 /* Finally perform built-in cursor blinking:
25229 filled box <-> hollow box
25230 wide [h]bar <-> narrow [h]bar
25231 narrow [h]bar <-> no cursor
25232 other type <-> no cursor */
25233
25234 if (cursor_type == FILLED_BOX_CURSOR)
25235 return HOLLOW_BOX_CURSOR;
25236
25237 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25238 {
25239 *width = 1;
25240 return cursor_type;
25241 }
25242 #endif
25243
25244 return NO_CURSOR;
25245 }
25246
25247
25248 /* Notice when the text cursor of window W has been completely
25249 overwritten by a drawing operation that outputs glyphs in AREA
25250 starting at X0 and ending at X1 in the line starting at Y0 and
25251 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25252 the rest of the line after X0 has been written. Y coordinates
25253 are window-relative. */
25254
25255 static void
25256 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25257 int x0, int x1, int y0, int y1)
25258 {
25259 int cx0, cx1, cy0, cy1;
25260 struct glyph_row *row;
25261
25262 if (!w->phys_cursor_on_p)
25263 return;
25264 if (area != TEXT_AREA)
25265 return;
25266
25267 if (w->phys_cursor.vpos < 0
25268 || w->phys_cursor.vpos >= w->current_matrix->nrows
25269 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25270 !(row->enabled_p && row->displays_text_p)))
25271 return;
25272
25273 if (row->cursor_in_fringe_p)
25274 {
25275 row->cursor_in_fringe_p = 0;
25276 draw_fringe_bitmap (w, row, row->reversed_p);
25277 w->phys_cursor_on_p = 0;
25278 return;
25279 }
25280
25281 cx0 = w->phys_cursor.x;
25282 cx1 = cx0 + w->phys_cursor_width;
25283 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25284 return;
25285
25286 /* The cursor image will be completely removed from the
25287 screen if the output area intersects the cursor area in
25288 y-direction. When we draw in [y0 y1[, and some part of
25289 the cursor is at y < y0, that part must have been drawn
25290 before. When scrolling, the cursor is erased before
25291 actually scrolling, so we don't come here. When not
25292 scrolling, the rows above the old cursor row must have
25293 changed, and in this case these rows must have written
25294 over the cursor image.
25295
25296 Likewise if part of the cursor is below y1, with the
25297 exception of the cursor being in the first blank row at
25298 the buffer and window end because update_text_area
25299 doesn't draw that row. (Except when it does, but
25300 that's handled in update_text_area.) */
25301
25302 cy0 = w->phys_cursor.y;
25303 cy1 = cy0 + w->phys_cursor_height;
25304 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25305 return;
25306
25307 w->phys_cursor_on_p = 0;
25308 }
25309
25310 #endif /* HAVE_WINDOW_SYSTEM */
25311
25312 \f
25313 /************************************************************************
25314 Mouse Face
25315 ************************************************************************/
25316
25317 #ifdef HAVE_WINDOW_SYSTEM
25318
25319 /* EXPORT for RIF:
25320 Fix the display of area AREA of overlapping row ROW in window W
25321 with respect to the overlapping part OVERLAPS. */
25322
25323 void
25324 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25325 enum glyph_row_area area, int overlaps)
25326 {
25327 int i, x;
25328
25329 BLOCK_INPUT;
25330
25331 x = 0;
25332 for (i = 0; i < row->used[area];)
25333 {
25334 if (row->glyphs[area][i].overlaps_vertically_p)
25335 {
25336 int start = i, start_x = x;
25337
25338 do
25339 {
25340 x += row->glyphs[area][i].pixel_width;
25341 ++i;
25342 }
25343 while (i < row->used[area]
25344 && row->glyphs[area][i].overlaps_vertically_p);
25345
25346 draw_glyphs (w, start_x, row, area,
25347 start, i,
25348 DRAW_NORMAL_TEXT, overlaps);
25349 }
25350 else
25351 {
25352 x += row->glyphs[area][i].pixel_width;
25353 ++i;
25354 }
25355 }
25356
25357 UNBLOCK_INPUT;
25358 }
25359
25360
25361 /* EXPORT:
25362 Draw the cursor glyph of window W in glyph row ROW. See the
25363 comment of draw_glyphs for the meaning of HL. */
25364
25365 void
25366 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25367 enum draw_glyphs_face hl)
25368 {
25369 /* If cursor hpos is out of bounds, don't draw garbage. This can
25370 happen in mini-buffer windows when switching between echo area
25371 glyphs and mini-buffer. */
25372 if ((row->reversed_p
25373 ? (w->phys_cursor.hpos >= 0)
25374 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25375 {
25376 int on_p = w->phys_cursor_on_p;
25377 int x1;
25378 int hpos = w->phys_cursor.hpos;
25379
25380 /* When the window is hscrolled, cursor hpos can legitimately be
25381 out of bounds, but we draw the cursor at the corresponding
25382 window margin in that case. */
25383 if (!row->reversed_p && hpos < 0)
25384 hpos = 0;
25385 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25386 hpos = row->used[TEXT_AREA] - 1;
25387
25388 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25389 hl, 0);
25390 w->phys_cursor_on_p = on_p;
25391
25392 if (hl == DRAW_CURSOR)
25393 w->phys_cursor_width = x1 - w->phys_cursor.x;
25394 /* When we erase the cursor, and ROW is overlapped by other
25395 rows, make sure that these overlapping parts of other rows
25396 are redrawn. */
25397 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25398 {
25399 w->phys_cursor_width = x1 - w->phys_cursor.x;
25400
25401 if (row > w->current_matrix->rows
25402 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25403 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25404 OVERLAPS_ERASED_CURSOR);
25405
25406 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25407 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25408 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25409 OVERLAPS_ERASED_CURSOR);
25410 }
25411 }
25412 }
25413
25414
25415 /* EXPORT:
25416 Erase the image of a cursor of window W from the screen. */
25417
25418 void
25419 erase_phys_cursor (struct window *w)
25420 {
25421 struct frame *f = XFRAME (w->frame);
25422 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25423 int hpos = w->phys_cursor.hpos;
25424 int vpos = w->phys_cursor.vpos;
25425 int mouse_face_here_p = 0;
25426 struct glyph_matrix *active_glyphs = w->current_matrix;
25427 struct glyph_row *cursor_row;
25428 struct glyph *cursor_glyph;
25429 enum draw_glyphs_face hl;
25430
25431 /* No cursor displayed or row invalidated => nothing to do on the
25432 screen. */
25433 if (w->phys_cursor_type == NO_CURSOR)
25434 goto mark_cursor_off;
25435
25436 /* VPOS >= active_glyphs->nrows means that window has been resized.
25437 Don't bother to erase the cursor. */
25438 if (vpos >= active_glyphs->nrows)
25439 goto mark_cursor_off;
25440
25441 /* If row containing cursor is marked invalid, there is nothing we
25442 can do. */
25443 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25444 if (!cursor_row->enabled_p)
25445 goto mark_cursor_off;
25446
25447 /* If line spacing is > 0, old cursor may only be partially visible in
25448 window after split-window. So adjust visible height. */
25449 cursor_row->visible_height = min (cursor_row->visible_height,
25450 window_text_bottom_y (w) - cursor_row->y);
25451
25452 /* If row is completely invisible, don't attempt to delete a cursor which
25453 isn't there. This can happen if cursor is at top of a window, and
25454 we switch to a buffer with a header line in that window. */
25455 if (cursor_row->visible_height <= 0)
25456 goto mark_cursor_off;
25457
25458 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25459 if (cursor_row->cursor_in_fringe_p)
25460 {
25461 cursor_row->cursor_in_fringe_p = 0;
25462 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25463 goto mark_cursor_off;
25464 }
25465
25466 /* This can happen when the new row is shorter than the old one.
25467 In this case, either draw_glyphs or clear_end_of_line
25468 should have cleared the cursor. Note that we wouldn't be
25469 able to erase the cursor in this case because we don't have a
25470 cursor glyph at hand. */
25471 if ((cursor_row->reversed_p
25472 ? (w->phys_cursor.hpos < 0)
25473 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25474 goto mark_cursor_off;
25475
25476 /* When the window is hscrolled, cursor hpos can legitimately be out
25477 of bounds, but we draw the cursor at the corresponding window
25478 margin in that case. */
25479 if (!cursor_row->reversed_p && hpos < 0)
25480 hpos = 0;
25481 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25482 hpos = cursor_row->used[TEXT_AREA] - 1;
25483
25484 /* If the cursor is in the mouse face area, redisplay that when
25485 we clear the cursor. */
25486 if (! NILP (hlinfo->mouse_face_window)
25487 && coords_in_mouse_face_p (w, hpos, vpos)
25488 /* Don't redraw the cursor's spot in mouse face if it is at the
25489 end of a line (on a newline). The cursor appears there, but
25490 mouse highlighting does not. */
25491 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25492 mouse_face_here_p = 1;
25493
25494 /* Maybe clear the display under the cursor. */
25495 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25496 {
25497 int x, y, left_x;
25498 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25499 int width;
25500
25501 cursor_glyph = get_phys_cursor_glyph (w);
25502 if (cursor_glyph == NULL)
25503 goto mark_cursor_off;
25504
25505 width = cursor_glyph->pixel_width;
25506 left_x = window_box_left_offset (w, TEXT_AREA);
25507 x = w->phys_cursor.x;
25508 if (x < left_x)
25509 width -= left_x - x;
25510 width = min (width, window_box_width (w, TEXT_AREA) - x);
25511 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25512 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25513
25514 if (width > 0)
25515 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25516 }
25517
25518 /* Erase the cursor by redrawing the character underneath it. */
25519 if (mouse_face_here_p)
25520 hl = DRAW_MOUSE_FACE;
25521 else
25522 hl = DRAW_NORMAL_TEXT;
25523 draw_phys_cursor_glyph (w, cursor_row, hl);
25524
25525 mark_cursor_off:
25526 w->phys_cursor_on_p = 0;
25527 w->phys_cursor_type = NO_CURSOR;
25528 }
25529
25530
25531 /* EXPORT:
25532 Display or clear cursor of window W. If ON is zero, clear the
25533 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25534 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25535
25536 void
25537 display_and_set_cursor (struct window *w, int on,
25538 int hpos, int vpos, int x, int y)
25539 {
25540 struct frame *f = XFRAME (w->frame);
25541 int new_cursor_type;
25542 int new_cursor_width;
25543 int active_cursor;
25544 struct glyph_row *glyph_row;
25545 struct glyph *glyph;
25546
25547 /* This is pointless on invisible frames, and dangerous on garbaged
25548 windows and frames; in the latter case, the frame or window may
25549 be in the midst of changing its size, and x and y may be off the
25550 window. */
25551 if (! FRAME_VISIBLE_P (f)
25552 || FRAME_GARBAGED_P (f)
25553 || vpos >= w->current_matrix->nrows
25554 || hpos >= w->current_matrix->matrix_w)
25555 return;
25556
25557 /* If cursor is off and we want it off, return quickly. */
25558 if (!on && !w->phys_cursor_on_p)
25559 return;
25560
25561 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25562 /* If cursor row is not enabled, we don't really know where to
25563 display the cursor. */
25564 if (!glyph_row->enabled_p)
25565 {
25566 w->phys_cursor_on_p = 0;
25567 return;
25568 }
25569
25570 glyph = NULL;
25571 if (!glyph_row->exact_window_width_line_p
25572 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25573 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25574
25575 xassert (interrupt_input_blocked);
25576
25577 /* Set new_cursor_type to the cursor we want to be displayed. */
25578 new_cursor_type = get_window_cursor_type (w, glyph,
25579 &new_cursor_width, &active_cursor);
25580
25581 /* If cursor is currently being shown and we don't want it to be or
25582 it is in the wrong place, or the cursor type is not what we want,
25583 erase it. */
25584 if (w->phys_cursor_on_p
25585 && (!on
25586 || w->phys_cursor.x != x
25587 || w->phys_cursor.y != y
25588 || new_cursor_type != w->phys_cursor_type
25589 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25590 && new_cursor_width != w->phys_cursor_width)))
25591 erase_phys_cursor (w);
25592
25593 /* Don't check phys_cursor_on_p here because that flag is only set
25594 to zero in some cases where we know that the cursor has been
25595 completely erased, to avoid the extra work of erasing the cursor
25596 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25597 still not be visible, or it has only been partly erased. */
25598 if (on)
25599 {
25600 w->phys_cursor_ascent = glyph_row->ascent;
25601 w->phys_cursor_height = glyph_row->height;
25602
25603 /* Set phys_cursor_.* before x_draw_.* is called because some
25604 of them may need the information. */
25605 w->phys_cursor.x = x;
25606 w->phys_cursor.y = glyph_row->y;
25607 w->phys_cursor.hpos = hpos;
25608 w->phys_cursor.vpos = vpos;
25609 }
25610
25611 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25612 new_cursor_type, new_cursor_width,
25613 on, active_cursor);
25614 }
25615
25616
25617 /* Switch the display of W's cursor on or off, according to the value
25618 of ON. */
25619
25620 static void
25621 update_window_cursor (struct window *w, int on)
25622 {
25623 /* Don't update cursor in windows whose frame is in the process
25624 of being deleted. */
25625 if (w->current_matrix)
25626 {
25627 int hpos = w->phys_cursor.hpos;
25628 int vpos = w->phys_cursor.vpos;
25629 struct glyph_row *row;
25630
25631 if (vpos >= w->current_matrix->nrows
25632 || hpos >= w->current_matrix->matrix_w)
25633 return;
25634
25635 row = MATRIX_ROW (w->current_matrix, vpos);
25636
25637 /* When the window is hscrolled, cursor hpos can legitimately be
25638 out of bounds, but we draw the cursor at the corresponding
25639 window margin in that case. */
25640 if (!row->reversed_p && hpos < 0)
25641 hpos = 0;
25642 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25643 hpos = row->used[TEXT_AREA] - 1;
25644
25645 BLOCK_INPUT;
25646 display_and_set_cursor (w, on, hpos, vpos,
25647 w->phys_cursor.x, w->phys_cursor.y);
25648 UNBLOCK_INPUT;
25649 }
25650 }
25651
25652
25653 /* Call update_window_cursor with parameter ON_P on all leaf windows
25654 in the window tree rooted at W. */
25655
25656 static void
25657 update_cursor_in_window_tree (struct window *w, int on_p)
25658 {
25659 while (w)
25660 {
25661 if (!NILP (w->hchild))
25662 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25663 else if (!NILP (w->vchild))
25664 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25665 else
25666 update_window_cursor (w, on_p);
25667
25668 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25669 }
25670 }
25671
25672
25673 /* EXPORT:
25674 Display the cursor on window W, or clear it, according to ON_P.
25675 Don't change the cursor's position. */
25676
25677 void
25678 x_update_cursor (struct frame *f, int on_p)
25679 {
25680 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25681 }
25682
25683
25684 /* EXPORT:
25685 Clear the cursor of window W to background color, and mark the
25686 cursor as not shown. This is used when the text where the cursor
25687 is about to be rewritten. */
25688
25689 void
25690 x_clear_cursor (struct window *w)
25691 {
25692 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25693 update_window_cursor (w, 0);
25694 }
25695
25696 #endif /* HAVE_WINDOW_SYSTEM */
25697
25698 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25699 and MSDOS. */
25700 static void
25701 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25702 int start_hpos, int end_hpos,
25703 enum draw_glyphs_face draw)
25704 {
25705 #ifdef HAVE_WINDOW_SYSTEM
25706 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25707 {
25708 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25709 return;
25710 }
25711 #endif
25712 #if defined (HAVE_GPM) || defined (MSDOS)
25713 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25714 #endif
25715 }
25716
25717 /* Display the active region described by mouse_face_* according to DRAW. */
25718
25719 static void
25720 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25721 {
25722 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25723 struct frame *f = XFRAME (WINDOW_FRAME (w));
25724
25725 if (/* If window is in the process of being destroyed, don't bother
25726 to do anything. */
25727 w->current_matrix != NULL
25728 /* Don't update mouse highlight if hidden */
25729 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25730 /* Recognize when we are called to operate on rows that don't exist
25731 anymore. This can happen when a window is split. */
25732 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25733 {
25734 int phys_cursor_on_p = w->phys_cursor_on_p;
25735 struct glyph_row *row, *first, *last;
25736
25737 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25738 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25739
25740 for (row = first; row <= last && row->enabled_p; ++row)
25741 {
25742 int start_hpos, end_hpos, start_x;
25743
25744 /* For all but the first row, the highlight starts at column 0. */
25745 if (row == first)
25746 {
25747 /* R2L rows have BEG and END in reversed order, but the
25748 screen drawing geometry is always left to right. So
25749 we need to mirror the beginning and end of the
25750 highlighted area in R2L rows. */
25751 if (!row->reversed_p)
25752 {
25753 start_hpos = hlinfo->mouse_face_beg_col;
25754 start_x = hlinfo->mouse_face_beg_x;
25755 }
25756 else if (row == last)
25757 {
25758 start_hpos = hlinfo->mouse_face_end_col;
25759 start_x = hlinfo->mouse_face_end_x;
25760 }
25761 else
25762 {
25763 start_hpos = 0;
25764 start_x = 0;
25765 }
25766 }
25767 else if (row->reversed_p && row == last)
25768 {
25769 start_hpos = hlinfo->mouse_face_end_col;
25770 start_x = hlinfo->mouse_face_end_x;
25771 }
25772 else
25773 {
25774 start_hpos = 0;
25775 start_x = 0;
25776 }
25777
25778 if (row == last)
25779 {
25780 if (!row->reversed_p)
25781 end_hpos = hlinfo->mouse_face_end_col;
25782 else if (row == first)
25783 end_hpos = hlinfo->mouse_face_beg_col;
25784 else
25785 {
25786 end_hpos = row->used[TEXT_AREA];
25787 if (draw == DRAW_NORMAL_TEXT)
25788 row->fill_line_p = 1; /* Clear to end of line */
25789 }
25790 }
25791 else if (row->reversed_p && row == first)
25792 end_hpos = hlinfo->mouse_face_beg_col;
25793 else
25794 {
25795 end_hpos = row->used[TEXT_AREA];
25796 if (draw == DRAW_NORMAL_TEXT)
25797 row->fill_line_p = 1; /* Clear to end of line */
25798 }
25799
25800 if (end_hpos > start_hpos)
25801 {
25802 draw_row_with_mouse_face (w, start_x, row,
25803 start_hpos, end_hpos, draw);
25804
25805 row->mouse_face_p
25806 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25807 }
25808 }
25809
25810 #ifdef HAVE_WINDOW_SYSTEM
25811 /* When we've written over the cursor, arrange for it to
25812 be displayed again. */
25813 if (FRAME_WINDOW_P (f)
25814 && phys_cursor_on_p && !w->phys_cursor_on_p)
25815 {
25816 int hpos = w->phys_cursor.hpos;
25817
25818 /* When the window is hscrolled, cursor hpos can legitimately be
25819 out of bounds, but we draw the cursor at the corresponding
25820 window margin in that case. */
25821 if (!row->reversed_p && hpos < 0)
25822 hpos = 0;
25823 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25824 hpos = row->used[TEXT_AREA] - 1;
25825
25826 BLOCK_INPUT;
25827 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25828 w->phys_cursor.x, w->phys_cursor.y);
25829 UNBLOCK_INPUT;
25830 }
25831 #endif /* HAVE_WINDOW_SYSTEM */
25832 }
25833
25834 #ifdef HAVE_WINDOW_SYSTEM
25835 /* Change the mouse cursor. */
25836 if (FRAME_WINDOW_P (f))
25837 {
25838 if (draw == DRAW_NORMAL_TEXT
25839 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25840 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25841 else if (draw == DRAW_MOUSE_FACE)
25842 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25843 else
25844 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25845 }
25846 #endif /* HAVE_WINDOW_SYSTEM */
25847 }
25848
25849 /* EXPORT:
25850 Clear out the mouse-highlighted active region.
25851 Redraw it un-highlighted first. Value is non-zero if mouse
25852 face was actually drawn unhighlighted. */
25853
25854 int
25855 clear_mouse_face (Mouse_HLInfo *hlinfo)
25856 {
25857 int cleared = 0;
25858
25859 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25860 {
25861 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25862 cleared = 1;
25863 }
25864
25865 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25866 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25867 hlinfo->mouse_face_window = Qnil;
25868 hlinfo->mouse_face_overlay = Qnil;
25869 return cleared;
25870 }
25871
25872 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25873 within the mouse face on that window. */
25874 static int
25875 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25876 {
25877 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25878
25879 /* Quickly resolve the easy cases. */
25880 if (!(WINDOWP (hlinfo->mouse_face_window)
25881 && XWINDOW (hlinfo->mouse_face_window) == w))
25882 return 0;
25883 if (vpos < hlinfo->mouse_face_beg_row
25884 || vpos > hlinfo->mouse_face_end_row)
25885 return 0;
25886 if (vpos > hlinfo->mouse_face_beg_row
25887 && vpos < hlinfo->mouse_face_end_row)
25888 return 1;
25889
25890 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25891 {
25892 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25893 {
25894 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25895 return 1;
25896 }
25897 else if ((vpos == hlinfo->mouse_face_beg_row
25898 && hpos >= hlinfo->mouse_face_beg_col)
25899 || (vpos == hlinfo->mouse_face_end_row
25900 && hpos < hlinfo->mouse_face_end_col))
25901 return 1;
25902 }
25903 else
25904 {
25905 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25906 {
25907 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25908 return 1;
25909 }
25910 else if ((vpos == hlinfo->mouse_face_beg_row
25911 && hpos <= hlinfo->mouse_face_beg_col)
25912 || (vpos == hlinfo->mouse_face_end_row
25913 && hpos > hlinfo->mouse_face_end_col))
25914 return 1;
25915 }
25916 return 0;
25917 }
25918
25919
25920 /* EXPORT:
25921 Non-zero if physical cursor of window W is within mouse face. */
25922
25923 int
25924 cursor_in_mouse_face_p (struct window *w)
25925 {
25926 int hpos = w->phys_cursor.hpos;
25927 int vpos = w->phys_cursor.vpos;
25928 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25929
25930 /* When the window is hscrolled, cursor hpos can legitimately be out
25931 of bounds, but we draw the cursor at the corresponding window
25932 margin in that case. */
25933 if (!row->reversed_p && hpos < 0)
25934 hpos = 0;
25935 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25936 hpos = row->used[TEXT_AREA] - 1;
25937
25938 return coords_in_mouse_face_p (w, hpos, vpos);
25939 }
25940
25941
25942 \f
25943 /* Find the glyph rows START_ROW and END_ROW of window W that display
25944 characters between buffer positions START_CHARPOS and END_CHARPOS
25945 (excluding END_CHARPOS). DISP_STRING is a display string that
25946 covers these buffer positions. This is similar to
25947 row_containing_pos, but is more accurate when bidi reordering makes
25948 buffer positions change non-linearly with glyph rows. */
25949 static void
25950 rows_from_pos_range (struct window *w,
25951 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
25952 Lisp_Object disp_string,
25953 struct glyph_row **start, struct glyph_row **end)
25954 {
25955 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25956 int last_y = window_text_bottom_y (w);
25957 struct glyph_row *row;
25958
25959 *start = NULL;
25960 *end = NULL;
25961
25962 while (!first->enabled_p
25963 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25964 first++;
25965
25966 /* Find the START row. */
25967 for (row = first;
25968 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25969 row++)
25970 {
25971 /* A row can potentially be the START row if the range of the
25972 characters it displays intersects the range
25973 [START_CHARPOS..END_CHARPOS). */
25974 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25975 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25976 /* See the commentary in row_containing_pos, for the
25977 explanation of the complicated way to check whether
25978 some position is beyond the end of the characters
25979 displayed by a row. */
25980 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25981 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25982 && !row->ends_at_zv_p
25983 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25984 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25985 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25986 && !row->ends_at_zv_p
25987 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25988 {
25989 /* Found a candidate row. Now make sure at least one of the
25990 glyphs it displays has a charpos from the range
25991 [START_CHARPOS..END_CHARPOS).
25992
25993 This is not obvious because bidi reordering could make
25994 buffer positions of a row be 1,2,3,102,101,100, and if we
25995 want to highlight characters in [50..60), we don't want
25996 this row, even though [50..60) does intersect [1..103),
25997 the range of character positions given by the row's start
25998 and end positions. */
25999 struct glyph *g = row->glyphs[TEXT_AREA];
26000 struct glyph *e = g + row->used[TEXT_AREA];
26001
26002 while (g < e)
26003 {
26004 if (((BUFFERP (g->object) || INTEGERP (g->object))
26005 && start_charpos <= g->charpos && g->charpos < end_charpos)
26006 /* A glyph that comes from DISP_STRING is by
26007 definition to be highlighted. */
26008 || EQ (g->object, disp_string))
26009 *start = row;
26010 g++;
26011 }
26012 if (*start)
26013 break;
26014 }
26015 }
26016
26017 /* Find the END row. */
26018 if (!*start
26019 /* If the last row is partially visible, start looking for END
26020 from that row, instead of starting from FIRST. */
26021 && !(row->enabled_p
26022 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26023 row = first;
26024 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26025 {
26026 struct glyph_row *next = row + 1;
26027 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26028
26029 if (!next->enabled_p
26030 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26031 /* The first row >= START whose range of displayed characters
26032 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26033 is the row END + 1. */
26034 || (start_charpos < next_start
26035 && end_charpos < next_start)
26036 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26037 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26038 && !next->ends_at_zv_p
26039 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26040 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26041 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26042 && !next->ends_at_zv_p
26043 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26044 {
26045 *end = row;
26046 break;
26047 }
26048 else
26049 {
26050 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26051 but none of the characters it displays are in the range, it is
26052 also END + 1. */
26053 struct glyph *g = next->glyphs[TEXT_AREA];
26054 struct glyph *s = g;
26055 struct glyph *e = g + next->used[TEXT_AREA];
26056
26057 while (g < e)
26058 {
26059 if (((BUFFERP (g->object) || INTEGERP (g->object))
26060 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26061 /* If the buffer position of the first glyph in
26062 the row is equal to END_CHARPOS, it means
26063 the last character to be highlighted is the
26064 newline of ROW, and we must consider NEXT as
26065 END, not END+1. */
26066 || (((!next->reversed_p && g == s)
26067 || (next->reversed_p && g == e - 1))
26068 && (g->charpos == end_charpos
26069 /* Special case for when NEXT is an
26070 empty line at ZV. */
26071 || (g->charpos == -1
26072 && !row->ends_at_zv_p
26073 && next_start == end_charpos)))))
26074 /* A glyph that comes from DISP_STRING is by
26075 definition to be highlighted. */
26076 || EQ (g->object, disp_string))
26077 break;
26078 g++;
26079 }
26080 if (g == e)
26081 {
26082 *end = row;
26083 break;
26084 }
26085 /* The first row that ends at ZV must be the last to be
26086 highlighted. */
26087 else if (next->ends_at_zv_p)
26088 {
26089 *end = next;
26090 break;
26091 }
26092 }
26093 }
26094 }
26095
26096 /* This function sets the mouse_face_* elements of HLINFO, assuming
26097 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26098 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26099 for the overlay or run of text properties specifying the mouse
26100 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26101 before-string and after-string that must also be highlighted.
26102 DISP_STRING, if non-nil, is a display string that may cover some
26103 or all of the highlighted text. */
26104
26105 static void
26106 mouse_face_from_buffer_pos (Lisp_Object window,
26107 Mouse_HLInfo *hlinfo,
26108 ptrdiff_t mouse_charpos,
26109 ptrdiff_t start_charpos,
26110 ptrdiff_t end_charpos,
26111 Lisp_Object before_string,
26112 Lisp_Object after_string,
26113 Lisp_Object disp_string)
26114 {
26115 struct window *w = XWINDOW (window);
26116 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26117 struct glyph_row *r1, *r2;
26118 struct glyph *glyph, *end;
26119 ptrdiff_t ignore, pos;
26120 int x;
26121
26122 xassert (NILP (disp_string) || STRINGP (disp_string));
26123 xassert (NILP (before_string) || STRINGP (before_string));
26124 xassert (NILP (after_string) || STRINGP (after_string));
26125
26126 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26127 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26128 if (r1 == NULL)
26129 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26130 /* If the before-string or display-string contains newlines,
26131 rows_from_pos_range skips to its last row. Move back. */
26132 if (!NILP (before_string) || !NILP (disp_string))
26133 {
26134 struct glyph_row *prev;
26135 while ((prev = r1 - 1, prev >= first)
26136 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26137 && prev->used[TEXT_AREA] > 0)
26138 {
26139 struct glyph *beg = prev->glyphs[TEXT_AREA];
26140 glyph = beg + prev->used[TEXT_AREA];
26141 while (--glyph >= beg && INTEGERP (glyph->object));
26142 if (glyph < beg
26143 || !(EQ (glyph->object, before_string)
26144 || EQ (glyph->object, disp_string)))
26145 break;
26146 r1 = prev;
26147 }
26148 }
26149 if (r2 == NULL)
26150 {
26151 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26152 hlinfo->mouse_face_past_end = 1;
26153 }
26154 else if (!NILP (after_string))
26155 {
26156 /* If the after-string has newlines, advance to its last row. */
26157 struct glyph_row *next;
26158 struct glyph_row *last
26159 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26160
26161 for (next = r2 + 1;
26162 next <= last
26163 && next->used[TEXT_AREA] > 0
26164 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26165 ++next)
26166 r2 = next;
26167 }
26168 /* The rest of the display engine assumes that mouse_face_beg_row is
26169 either above mouse_face_end_row or identical to it. But with
26170 bidi-reordered continued lines, the row for START_CHARPOS could
26171 be below the row for END_CHARPOS. If so, swap the rows and store
26172 them in correct order. */
26173 if (r1->y > r2->y)
26174 {
26175 struct glyph_row *tem = r2;
26176
26177 r2 = r1;
26178 r1 = tem;
26179 }
26180
26181 hlinfo->mouse_face_beg_y = r1->y;
26182 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26183 hlinfo->mouse_face_end_y = r2->y;
26184 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26185
26186 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26187 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26188 could be anywhere in the row and in any order. The strategy
26189 below is to find the leftmost and the rightmost glyph that
26190 belongs to either of these 3 strings, or whose position is
26191 between START_CHARPOS and END_CHARPOS, and highlight all the
26192 glyphs between those two. This may cover more than just the text
26193 between START_CHARPOS and END_CHARPOS if the range of characters
26194 strides the bidi level boundary, e.g. if the beginning is in R2L
26195 text while the end is in L2R text or vice versa. */
26196 if (!r1->reversed_p)
26197 {
26198 /* This row is in a left to right paragraph. Scan it left to
26199 right. */
26200 glyph = r1->glyphs[TEXT_AREA];
26201 end = glyph + r1->used[TEXT_AREA];
26202 x = r1->x;
26203
26204 /* Skip truncation glyphs at the start of the glyph row. */
26205 if (r1->displays_text_p)
26206 for (; glyph < end
26207 && INTEGERP (glyph->object)
26208 && glyph->charpos < 0;
26209 ++glyph)
26210 x += glyph->pixel_width;
26211
26212 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26213 or DISP_STRING, and the first glyph from buffer whose
26214 position is between START_CHARPOS and END_CHARPOS. */
26215 for (; glyph < end
26216 && !INTEGERP (glyph->object)
26217 && !EQ (glyph->object, disp_string)
26218 && !(BUFFERP (glyph->object)
26219 && (glyph->charpos >= start_charpos
26220 && glyph->charpos < end_charpos));
26221 ++glyph)
26222 {
26223 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26224 are present at buffer positions between START_CHARPOS and
26225 END_CHARPOS, or if they come from an overlay. */
26226 if (EQ (glyph->object, before_string))
26227 {
26228 pos = string_buffer_position (before_string,
26229 start_charpos);
26230 /* If pos == 0, it means before_string came from an
26231 overlay, not from a buffer position. */
26232 if (!pos || (pos >= start_charpos && pos < end_charpos))
26233 break;
26234 }
26235 else if (EQ (glyph->object, after_string))
26236 {
26237 pos = string_buffer_position (after_string, end_charpos);
26238 if (!pos || (pos >= start_charpos && pos < end_charpos))
26239 break;
26240 }
26241 x += glyph->pixel_width;
26242 }
26243 hlinfo->mouse_face_beg_x = x;
26244 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26245 }
26246 else
26247 {
26248 /* This row is in a right to left paragraph. Scan it right to
26249 left. */
26250 struct glyph *g;
26251
26252 end = r1->glyphs[TEXT_AREA] - 1;
26253 glyph = end + r1->used[TEXT_AREA];
26254
26255 /* Skip truncation glyphs at the start of the glyph row. */
26256 if (r1->displays_text_p)
26257 for (; glyph > end
26258 && INTEGERP (glyph->object)
26259 && glyph->charpos < 0;
26260 --glyph)
26261 ;
26262
26263 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26264 or DISP_STRING, and the first glyph from buffer whose
26265 position is between START_CHARPOS and END_CHARPOS. */
26266 for (; glyph > end
26267 && !INTEGERP (glyph->object)
26268 && !EQ (glyph->object, disp_string)
26269 && !(BUFFERP (glyph->object)
26270 && (glyph->charpos >= start_charpos
26271 && glyph->charpos < end_charpos));
26272 --glyph)
26273 {
26274 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26275 are present at buffer positions between START_CHARPOS and
26276 END_CHARPOS, or if they come from an overlay. */
26277 if (EQ (glyph->object, before_string))
26278 {
26279 pos = string_buffer_position (before_string, start_charpos);
26280 /* If pos == 0, it means before_string came from an
26281 overlay, not from a buffer position. */
26282 if (!pos || (pos >= start_charpos && pos < end_charpos))
26283 break;
26284 }
26285 else if (EQ (glyph->object, after_string))
26286 {
26287 pos = string_buffer_position (after_string, end_charpos);
26288 if (!pos || (pos >= start_charpos && pos < end_charpos))
26289 break;
26290 }
26291 }
26292
26293 glyph++; /* first glyph to the right of the highlighted area */
26294 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26295 x += g->pixel_width;
26296 hlinfo->mouse_face_beg_x = x;
26297 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26298 }
26299
26300 /* If the highlight ends in a different row, compute GLYPH and END
26301 for the end row. Otherwise, reuse the values computed above for
26302 the row where the highlight begins. */
26303 if (r2 != r1)
26304 {
26305 if (!r2->reversed_p)
26306 {
26307 glyph = r2->glyphs[TEXT_AREA];
26308 end = glyph + r2->used[TEXT_AREA];
26309 x = r2->x;
26310 }
26311 else
26312 {
26313 end = r2->glyphs[TEXT_AREA] - 1;
26314 glyph = end + r2->used[TEXT_AREA];
26315 }
26316 }
26317
26318 if (!r2->reversed_p)
26319 {
26320 /* Skip truncation and continuation glyphs near the end of the
26321 row, and also blanks and stretch glyphs inserted by
26322 extend_face_to_end_of_line. */
26323 while (end > glyph
26324 && INTEGERP ((end - 1)->object))
26325 --end;
26326 /* Scan the rest of the glyph row from the end, looking for the
26327 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26328 DISP_STRING, or whose position is between START_CHARPOS
26329 and END_CHARPOS */
26330 for (--end;
26331 end > glyph
26332 && !INTEGERP (end->object)
26333 && !EQ (end->object, disp_string)
26334 && !(BUFFERP (end->object)
26335 && (end->charpos >= start_charpos
26336 && end->charpos < end_charpos));
26337 --end)
26338 {
26339 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26340 are present at buffer positions between START_CHARPOS and
26341 END_CHARPOS, or if they come from an overlay. */
26342 if (EQ (end->object, before_string))
26343 {
26344 pos = string_buffer_position (before_string, start_charpos);
26345 if (!pos || (pos >= start_charpos && pos < end_charpos))
26346 break;
26347 }
26348 else if (EQ (end->object, after_string))
26349 {
26350 pos = string_buffer_position (after_string, end_charpos);
26351 if (!pos || (pos >= start_charpos && pos < end_charpos))
26352 break;
26353 }
26354 }
26355 /* Find the X coordinate of the last glyph to be highlighted. */
26356 for (; glyph <= end; ++glyph)
26357 x += glyph->pixel_width;
26358
26359 hlinfo->mouse_face_end_x = x;
26360 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26361 }
26362 else
26363 {
26364 /* Skip truncation and continuation glyphs near the end of the
26365 row, and also blanks and stretch glyphs inserted by
26366 extend_face_to_end_of_line. */
26367 x = r2->x;
26368 end++;
26369 while (end < glyph
26370 && INTEGERP (end->object))
26371 {
26372 x += end->pixel_width;
26373 ++end;
26374 }
26375 /* Scan the rest of the glyph row from the end, looking for the
26376 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26377 DISP_STRING, or whose position is between START_CHARPOS
26378 and END_CHARPOS */
26379 for ( ;
26380 end < glyph
26381 && !INTEGERP (end->object)
26382 && !EQ (end->object, disp_string)
26383 && !(BUFFERP (end->object)
26384 && (end->charpos >= start_charpos
26385 && end->charpos < end_charpos));
26386 ++end)
26387 {
26388 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26389 are present at buffer positions between START_CHARPOS and
26390 END_CHARPOS, or if they come from an overlay. */
26391 if (EQ (end->object, before_string))
26392 {
26393 pos = string_buffer_position (before_string, start_charpos);
26394 if (!pos || (pos >= start_charpos && pos < end_charpos))
26395 break;
26396 }
26397 else if (EQ (end->object, after_string))
26398 {
26399 pos = string_buffer_position (after_string, end_charpos);
26400 if (!pos || (pos >= start_charpos && pos < end_charpos))
26401 break;
26402 }
26403 x += end->pixel_width;
26404 }
26405 /* If we exited the above loop because we arrived at the last
26406 glyph of the row, and its buffer position is still not in
26407 range, it means the last character in range is the preceding
26408 newline. Bump the end column and x values to get past the
26409 last glyph. */
26410 if (end == glyph
26411 && BUFFERP (end->object)
26412 && (end->charpos < start_charpos
26413 || end->charpos >= end_charpos))
26414 {
26415 x += end->pixel_width;
26416 ++end;
26417 }
26418 hlinfo->mouse_face_end_x = x;
26419 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26420 }
26421
26422 hlinfo->mouse_face_window = window;
26423 hlinfo->mouse_face_face_id
26424 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26425 mouse_charpos + 1,
26426 !hlinfo->mouse_face_hidden, -1);
26427 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26428 }
26429
26430 /* The following function is not used anymore (replaced with
26431 mouse_face_from_string_pos), but I leave it here for the time
26432 being, in case someone would. */
26433
26434 #if 0 /* not used */
26435
26436 /* Find the position of the glyph for position POS in OBJECT in
26437 window W's current matrix, and return in *X, *Y the pixel
26438 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26439
26440 RIGHT_P non-zero means return the position of the right edge of the
26441 glyph, RIGHT_P zero means return the left edge position.
26442
26443 If no glyph for POS exists in the matrix, return the position of
26444 the glyph with the next smaller position that is in the matrix, if
26445 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26446 exists in the matrix, return the position of the glyph with the
26447 next larger position in OBJECT.
26448
26449 Value is non-zero if a glyph was found. */
26450
26451 static int
26452 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26453 int *hpos, int *vpos, int *x, int *y, int right_p)
26454 {
26455 int yb = window_text_bottom_y (w);
26456 struct glyph_row *r;
26457 struct glyph *best_glyph = NULL;
26458 struct glyph_row *best_row = NULL;
26459 int best_x = 0;
26460
26461 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26462 r->enabled_p && r->y < yb;
26463 ++r)
26464 {
26465 struct glyph *g = r->glyphs[TEXT_AREA];
26466 struct glyph *e = g + r->used[TEXT_AREA];
26467 int gx;
26468
26469 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26470 if (EQ (g->object, object))
26471 {
26472 if (g->charpos == pos)
26473 {
26474 best_glyph = g;
26475 best_x = gx;
26476 best_row = r;
26477 goto found;
26478 }
26479 else if (best_glyph == NULL
26480 || ((eabs (g->charpos - pos)
26481 < eabs (best_glyph->charpos - pos))
26482 && (right_p
26483 ? g->charpos < pos
26484 : g->charpos > pos)))
26485 {
26486 best_glyph = g;
26487 best_x = gx;
26488 best_row = r;
26489 }
26490 }
26491 }
26492
26493 found:
26494
26495 if (best_glyph)
26496 {
26497 *x = best_x;
26498 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26499
26500 if (right_p)
26501 {
26502 *x += best_glyph->pixel_width;
26503 ++*hpos;
26504 }
26505
26506 *y = best_row->y;
26507 *vpos = best_row - w->current_matrix->rows;
26508 }
26509
26510 return best_glyph != NULL;
26511 }
26512 #endif /* not used */
26513
26514 /* Find the positions of the first and the last glyphs in window W's
26515 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26516 (assumed to be a string), and return in HLINFO's mouse_face_*
26517 members the pixel and column/row coordinates of those glyphs. */
26518
26519 static void
26520 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26521 Lisp_Object object,
26522 ptrdiff_t startpos, ptrdiff_t endpos)
26523 {
26524 int yb = window_text_bottom_y (w);
26525 struct glyph_row *r;
26526 struct glyph *g, *e;
26527 int gx;
26528 int found = 0;
26529
26530 /* Find the glyph row with at least one position in the range
26531 [STARTPOS..ENDPOS], and the first glyph in that row whose
26532 position belongs to that range. */
26533 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26534 r->enabled_p && r->y < yb;
26535 ++r)
26536 {
26537 if (!r->reversed_p)
26538 {
26539 g = r->glyphs[TEXT_AREA];
26540 e = g + r->used[TEXT_AREA];
26541 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26542 if (EQ (g->object, object)
26543 && startpos <= g->charpos && g->charpos <= endpos)
26544 {
26545 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26546 hlinfo->mouse_face_beg_y = r->y;
26547 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26548 hlinfo->mouse_face_beg_x = gx;
26549 found = 1;
26550 break;
26551 }
26552 }
26553 else
26554 {
26555 struct glyph *g1;
26556
26557 e = r->glyphs[TEXT_AREA];
26558 g = e + r->used[TEXT_AREA];
26559 for ( ; g > e; --g)
26560 if (EQ ((g-1)->object, object)
26561 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26562 {
26563 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26564 hlinfo->mouse_face_beg_y = r->y;
26565 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26566 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26567 gx += g1->pixel_width;
26568 hlinfo->mouse_face_beg_x = gx;
26569 found = 1;
26570 break;
26571 }
26572 }
26573 if (found)
26574 break;
26575 }
26576
26577 if (!found)
26578 return;
26579
26580 /* Starting with the next row, look for the first row which does NOT
26581 include any glyphs whose positions are in the range. */
26582 for (++r; r->enabled_p && r->y < yb; ++r)
26583 {
26584 g = r->glyphs[TEXT_AREA];
26585 e = g + r->used[TEXT_AREA];
26586 found = 0;
26587 for ( ; g < e; ++g)
26588 if (EQ (g->object, object)
26589 && startpos <= g->charpos && g->charpos <= endpos)
26590 {
26591 found = 1;
26592 break;
26593 }
26594 if (!found)
26595 break;
26596 }
26597
26598 /* The highlighted region ends on the previous row. */
26599 r--;
26600
26601 /* Set the end row and its vertical pixel coordinate. */
26602 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26603 hlinfo->mouse_face_end_y = r->y;
26604
26605 /* Compute and set the end column and the end column's horizontal
26606 pixel coordinate. */
26607 if (!r->reversed_p)
26608 {
26609 g = r->glyphs[TEXT_AREA];
26610 e = g + r->used[TEXT_AREA];
26611 for ( ; e > g; --e)
26612 if (EQ ((e-1)->object, object)
26613 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26614 break;
26615 hlinfo->mouse_face_end_col = e - g;
26616
26617 for (gx = r->x; g < e; ++g)
26618 gx += g->pixel_width;
26619 hlinfo->mouse_face_end_x = gx;
26620 }
26621 else
26622 {
26623 e = r->glyphs[TEXT_AREA];
26624 g = e + r->used[TEXT_AREA];
26625 for (gx = r->x ; e < g; ++e)
26626 {
26627 if (EQ (e->object, object)
26628 && startpos <= e->charpos && e->charpos <= endpos)
26629 break;
26630 gx += e->pixel_width;
26631 }
26632 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26633 hlinfo->mouse_face_end_x = gx;
26634 }
26635 }
26636
26637 #ifdef HAVE_WINDOW_SYSTEM
26638
26639 /* See if position X, Y is within a hot-spot of an image. */
26640
26641 static int
26642 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26643 {
26644 if (!CONSP (hot_spot))
26645 return 0;
26646
26647 if (EQ (XCAR (hot_spot), Qrect))
26648 {
26649 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26650 Lisp_Object rect = XCDR (hot_spot);
26651 Lisp_Object tem;
26652 if (!CONSP (rect))
26653 return 0;
26654 if (!CONSP (XCAR (rect)))
26655 return 0;
26656 if (!CONSP (XCDR (rect)))
26657 return 0;
26658 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26659 return 0;
26660 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26661 return 0;
26662 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26663 return 0;
26664 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26665 return 0;
26666 return 1;
26667 }
26668 else if (EQ (XCAR (hot_spot), Qcircle))
26669 {
26670 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26671 Lisp_Object circ = XCDR (hot_spot);
26672 Lisp_Object lr, lx0, ly0;
26673 if (CONSP (circ)
26674 && CONSP (XCAR (circ))
26675 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26676 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26677 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26678 {
26679 double r = XFLOATINT (lr);
26680 double dx = XINT (lx0) - x;
26681 double dy = XINT (ly0) - y;
26682 return (dx * dx + dy * dy <= r * r);
26683 }
26684 }
26685 else if (EQ (XCAR (hot_spot), Qpoly))
26686 {
26687 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26688 if (VECTORP (XCDR (hot_spot)))
26689 {
26690 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26691 Lisp_Object *poly = v->contents;
26692 ptrdiff_t n = v->header.size;
26693 ptrdiff_t i;
26694 int inside = 0;
26695 Lisp_Object lx, ly;
26696 int x0, y0;
26697
26698 /* Need an even number of coordinates, and at least 3 edges. */
26699 if (n < 6 || n & 1)
26700 return 0;
26701
26702 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26703 If count is odd, we are inside polygon. Pixels on edges
26704 may or may not be included depending on actual geometry of the
26705 polygon. */
26706 if ((lx = poly[n-2], !INTEGERP (lx))
26707 || (ly = poly[n-1], !INTEGERP (lx)))
26708 return 0;
26709 x0 = XINT (lx), y0 = XINT (ly);
26710 for (i = 0; i < n; i += 2)
26711 {
26712 int x1 = x0, y1 = y0;
26713 if ((lx = poly[i], !INTEGERP (lx))
26714 || (ly = poly[i+1], !INTEGERP (ly)))
26715 return 0;
26716 x0 = XINT (lx), y0 = XINT (ly);
26717
26718 /* Does this segment cross the X line? */
26719 if (x0 >= x)
26720 {
26721 if (x1 >= x)
26722 continue;
26723 }
26724 else if (x1 < x)
26725 continue;
26726 if (y > y0 && y > y1)
26727 continue;
26728 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26729 inside = !inside;
26730 }
26731 return inside;
26732 }
26733 }
26734 return 0;
26735 }
26736
26737 Lisp_Object
26738 find_hot_spot (Lisp_Object map, int x, int y)
26739 {
26740 while (CONSP (map))
26741 {
26742 if (CONSP (XCAR (map))
26743 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26744 return XCAR (map);
26745 map = XCDR (map);
26746 }
26747
26748 return Qnil;
26749 }
26750
26751 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26752 3, 3, 0,
26753 doc: /* Lookup in image map MAP coordinates X and Y.
26754 An image map is an alist where each element has the format (AREA ID PLIST).
26755 An AREA is specified as either a rectangle, a circle, or a polygon:
26756 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26757 pixel coordinates of the upper left and bottom right corners.
26758 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26759 and the radius of the circle; r may be a float or integer.
26760 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26761 vector describes one corner in the polygon.
26762 Returns the alist element for the first matching AREA in MAP. */)
26763 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26764 {
26765 if (NILP (map))
26766 return Qnil;
26767
26768 CHECK_NUMBER (x);
26769 CHECK_NUMBER (y);
26770
26771 return find_hot_spot (map,
26772 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26773 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26774 }
26775
26776
26777 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26778 static void
26779 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26780 {
26781 /* Do not change cursor shape while dragging mouse. */
26782 if (!NILP (do_mouse_tracking))
26783 return;
26784
26785 if (!NILP (pointer))
26786 {
26787 if (EQ (pointer, Qarrow))
26788 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26789 else if (EQ (pointer, Qhand))
26790 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26791 else if (EQ (pointer, Qtext))
26792 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26793 else if (EQ (pointer, intern ("hdrag")))
26794 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26795 #ifdef HAVE_X_WINDOWS
26796 else if (EQ (pointer, intern ("vdrag")))
26797 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26798 #endif
26799 else if (EQ (pointer, intern ("hourglass")))
26800 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26801 else if (EQ (pointer, Qmodeline))
26802 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26803 else
26804 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26805 }
26806
26807 if (cursor != No_Cursor)
26808 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26809 }
26810
26811 #endif /* HAVE_WINDOW_SYSTEM */
26812
26813 /* Take proper action when mouse has moved to the mode or header line
26814 or marginal area AREA of window W, x-position X and y-position Y.
26815 X is relative to the start of the text display area of W, so the
26816 width of bitmap areas and scroll bars must be subtracted to get a
26817 position relative to the start of the mode line. */
26818
26819 static void
26820 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26821 enum window_part area)
26822 {
26823 struct window *w = XWINDOW (window);
26824 struct frame *f = XFRAME (w->frame);
26825 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26826 #ifdef HAVE_WINDOW_SYSTEM
26827 Display_Info *dpyinfo;
26828 #endif
26829 Cursor cursor = No_Cursor;
26830 Lisp_Object pointer = Qnil;
26831 int dx, dy, width, height;
26832 ptrdiff_t charpos;
26833 Lisp_Object string, object = Qnil;
26834 Lisp_Object pos, help;
26835
26836 Lisp_Object mouse_face;
26837 int original_x_pixel = x;
26838 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26839 struct glyph_row *row;
26840
26841 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26842 {
26843 int x0;
26844 struct glyph *end;
26845
26846 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26847 returns them in row/column units! */
26848 string = mode_line_string (w, area, &x, &y, &charpos,
26849 &object, &dx, &dy, &width, &height);
26850
26851 row = (area == ON_MODE_LINE
26852 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26853 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26854
26855 /* Find the glyph under the mouse pointer. */
26856 if (row->mode_line_p && row->enabled_p)
26857 {
26858 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26859 end = glyph + row->used[TEXT_AREA];
26860
26861 for (x0 = original_x_pixel;
26862 glyph < end && x0 >= glyph->pixel_width;
26863 ++glyph)
26864 x0 -= glyph->pixel_width;
26865
26866 if (glyph >= end)
26867 glyph = NULL;
26868 }
26869 }
26870 else
26871 {
26872 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26873 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26874 returns them in row/column units! */
26875 string = marginal_area_string (w, area, &x, &y, &charpos,
26876 &object, &dx, &dy, &width, &height);
26877 }
26878
26879 help = Qnil;
26880
26881 #ifdef HAVE_WINDOW_SYSTEM
26882 if (IMAGEP (object))
26883 {
26884 Lisp_Object image_map, hotspot;
26885 if ((image_map = Fplist_get (XCDR (object), QCmap),
26886 !NILP (image_map))
26887 && (hotspot = find_hot_spot (image_map, dx, dy),
26888 CONSP (hotspot))
26889 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26890 {
26891 Lisp_Object plist;
26892
26893 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26894 If so, we could look for mouse-enter, mouse-leave
26895 properties in PLIST (and do something...). */
26896 hotspot = XCDR (hotspot);
26897 if (CONSP (hotspot)
26898 && (plist = XCAR (hotspot), CONSP (plist)))
26899 {
26900 pointer = Fplist_get (plist, Qpointer);
26901 if (NILP (pointer))
26902 pointer = Qhand;
26903 help = Fplist_get (plist, Qhelp_echo);
26904 if (!NILP (help))
26905 {
26906 help_echo_string = help;
26907 /* Is this correct? ++kfs */
26908 XSETWINDOW (help_echo_window, w);
26909 help_echo_object = w->buffer;
26910 help_echo_pos = charpos;
26911 }
26912 }
26913 }
26914 if (NILP (pointer))
26915 pointer = Fplist_get (XCDR (object), QCpointer);
26916 }
26917 #endif /* HAVE_WINDOW_SYSTEM */
26918
26919 if (STRINGP (string))
26920 {
26921 pos = make_number (charpos);
26922 /* If we're on a string with `help-echo' text property, arrange
26923 for the help to be displayed. This is done by setting the
26924 global variable help_echo_string to the help string. */
26925 if (NILP (help))
26926 {
26927 help = Fget_text_property (pos, Qhelp_echo, string);
26928 if (!NILP (help))
26929 {
26930 help_echo_string = help;
26931 XSETWINDOW (help_echo_window, w);
26932 help_echo_object = string;
26933 help_echo_pos = charpos;
26934 }
26935 }
26936
26937 #ifdef HAVE_WINDOW_SYSTEM
26938 if (FRAME_WINDOW_P (f))
26939 {
26940 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26941 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26942 if (NILP (pointer))
26943 pointer = Fget_text_property (pos, Qpointer, string);
26944
26945 /* Change the mouse pointer according to what is under X/Y. */
26946 if (NILP (pointer)
26947 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26948 {
26949 Lisp_Object map;
26950 map = Fget_text_property (pos, Qlocal_map, string);
26951 if (!KEYMAPP (map))
26952 map = Fget_text_property (pos, Qkeymap, string);
26953 if (!KEYMAPP (map))
26954 cursor = dpyinfo->vertical_scroll_bar_cursor;
26955 }
26956 }
26957 #endif
26958
26959 /* Change the mouse face according to what is under X/Y. */
26960 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26961 if (!NILP (mouse_face)
26962 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26963 && glyph)
26964 {
26965 Lisp_Object b, e;
26966
26967 struct glyph * tmp_glyph;
26968
26969 int gpos;
26970 int gseq_length;
26971 int total_pixel_width;
26972 ptrdiff_t begpos, endpos, ignore;
26973
26974 int vpos, hpos;
26975
26976 b = Fprevious_single_property_change (make_number (charpos + 1),
26977 Qmouse_face, string, Qnil);
26978 if (NILP (b))
26979 begpos = 0;
26980 else
26981 begpos = XINT (b);
26982
26983 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26984 if (NILP (e))
26985 endpos = SCHARS (string);
26986 else
26987 endpos = XINT (e);
26988
26989 /* Calculate the glyph position GPOS of GLYPH in the
26990 displayed string, relative to the beginning of the
26991 highlighted part of the string.
26992
26993 Note: GPOS is different from CHARPOS. CHARPOS is the
26994 position of GLYPH in the internal string object. A mode
26995 line string format has structures which are converted to
26996 a flattened string by the Emacs Lisp interpreter. The
26997 internal string is an element of those structures. The
26998 displayed string is the flattened string. */
26999 tmp_glyph = row_start_glyph;
27000 while (tmp_glyph < glyph
27001 && (!(EQ (tmp_glyph->object, glyph->object)
27002 && begpos <= tmp_glyph->charpos
27003 && tmp_glyph->charpos < endpos)))
27004 tmp_glyph++;
27005 gpos = glyph - tmp_glyph;
27006
27007 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27008 the highlighted part of the displayed string to which
27009 GLYPH belongs. Note: GSEQ_LENGTH is different from
27010 SCHARS (STRING), because the latter returns the length of
27011 the internal string. */
27012 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27013 tmp_glyph > glyph
27014 && (!(EQ (tmp_glyph->object, glyph->object)
27015 && begpos <= tmp_glyph->charpos
27016 && tmp_glyph->charpos < endpos));
27017 tmp_glyph--)
27018 ;
27019 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27020
27021 /* Calculate the total pixel width of all the glyphs between
27022 the beginning of the highlighted area and GLYPH. */
27023 total_pixel_width = 0;
27024 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27025 total_pixel_width += tmp_glyph->pixel_width;
27026
27027 /* Pre calculation of re-rendering position. Note: X is in
27028 column units here, after the call to mode_line_string or
27029 marginal_area_string. */
27030 hpos = x - gpos;
27031 vpos = (area == ON_MODE_LINE
27032 ? (w->current_matrix)->nrows - 1
27033 : 0);
27034
27035 /* If GLYPH's position is included in the region that is
27036 already drawn in mouse face, we have nothing to do. */
27037 if ( EQ (window, hlinfo->mouse_face_window)
27038 && (!row->reversed_p
27039 ? (hlinfo->mouse_face_beg_col <= hpos
27040 && hpos < hlinfo->mouse_face_end_col)
27041 /* In R2L rows we swap BEG and END, see below. */
27042 : (hlinfo->mouse_face_end_col <= hpos
27043 && hpos < hlinfo->mouse_face_beg_col))
27044 && hlinfo->mouse_face_beg_row == vpos )
27045 return;
27046
27047 if (clear_mouse_face (hlinfo))
27048 cursor = No_Cursor;
27049
27050 if (!row->reversed_p)
27051 {
27052 hlinfo->mouse_face_beg_col = hpos;
27053 hlinfo->mouse_face_beg_x = original_x_pixel
27054 - (total_pixel_width + dx);
27055 hlinfo->mouse_face_end_col = hpos + gseq_length;
27056 hlinfo->mouse_face_end_x = 0;
27057 }
27058 else
27059 {
27060 /* In R2L rows, show_mouse_face expects BEG and END
27061 coordinates to be swapped. */
27062 hlinfo->mouse_face_end_col = hpos;
27063 hlinfo->mouse_face_end_x = original_x_pixel
27064 - (total_pixel_width + dx);
27065 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27066 hlinfo->mouse_face_beg_x = 0;
27067 }
27068
27069 hlinfo->mouse_face_beg_row = vpos;
27070 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27071 hlinfo->mouse_face_beg_y = 0;
27072 hlinfo->mouse_face_end_y = 0;
27073 hlinfo->mouse_face_past_end = 0;
27074 hlinfo->mouse_face_window = window;
27075
27076 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27077 charpos,
27078 0, 0, 0,
27079 &ignore,
27080 glyph->face_id,
27081 1);
27082 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27083
27084 if (NILP (pointer))
27085 pointer = Qhand;
27086 }
27087 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27088 clear_mouse_face (hlinfo);
27089 }
27090 #ifdef HAVE_WINDOW_SYSTEM
27091 if (FRAME_WINDOW_P (f))
27092 define_frame_cursor1 (f, cursor, pointer);
27093 #endif
27094 }
27095
27096
27097 /* EXPORT:
27098 Take proper action when the mouse has moved to position X, Y on
27099 frame F as regards highlighting characters that have mouse-face
27100 properties. Also de-highlighting chars where the mouse was before.
27101 X and Y can be negative or out of range. */
27102
27103 void
27104 note_mouse_highlight (struct frame *f, int x, int y)
27105 {
27106 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27107 enum window_part part = ON_NOTHING;
27108 Lisp_Object window;
27109 struct window *w;
27110 Cursor cursor = No_Cursor;
27111 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27112 struct buffer *b;
27113
27114 /* When a menu is active, don't highlight because this looks odd. */
27115 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27116 if (popup_activated ())
27117 return;
27118 #endif
27119
27120 if (NILP (Vmouse_highlight)
27121 || !f->glyphs_initialized_p
27122 || f->pointer_invisible)
27123 return;
27124
27125 hlinfo->mouse_face_mouse_x = x;
27126 hlinfo->mouse_face_mouse_y = y;
27127 hlinfo->mouse_face_mouse_frame = f;
27128
27129 if (hlinfo->mouse_face_defer)
27130 return;
27131
27132 if (gc_in_progress)
27133 {
27134 hlinfo->mouse_face_deferred_gc = 1;
27135 return;
27136 }
27137
27138 /* Which window is that in? */
27139 window = window_from_coordinates (f, x, y, &part, 1);
27140
27141 /* If displaying active text in another window, clear that. */
27142 if (! EQ (window, hlinfo->mouse_face_window)
27143 /* Also clear if we move out of text area in same window. */
27144 || (!NILP (hlinfo->mouse_face_window)
27145 && !NILP (window)
27146 && part != ON_TEXT
27147 && part != ON_MODE_LINE
27148 && part != ON_HEADER_LINE))
27149 clear_mouse_face (hlinfo);
27150
27151 /* Not on a window -> return. */
27152 if (!WINDOWP (window))
27153 return;
27154
27155 /* Reset help_echo_string. It will get recomputed below. */
27156 help_echo_string = Qnil;
27157
27158 /* Convert to window-relative pixel coordinates. */
27159 w = XWINDOW (window);
27160 frame_to_window_pixel_xy (w, &x, &y);
27161
27162 #ifdef HAVE_WINDOW_SYSTEM
27163 /* Handle tool-bar window differently since it doesn't display a
27164 buffer. */
27165 if (EQ (window, f->tool_bar_window))
27166 {
27167 note_tool_bar_highlight (f, x, y);
27168 return;
27169 }
27170 #endif
27171
27172 /* Mouse is on the mode, header line or margin? */
27173 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27174 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27175 {
27176 note_mode_line_or_margin_highlight (window, x, y, part);
27177 return;
27178 }
27179
27180 #ifdef HAVE_WINDOW_SYSTEM
27181 if (part == ON_VERTICAL_BORDER)
27182 {
27183 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27184 help_echo_string = build_string ("drag-mouse-1: resize");
27185 }
27186 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27187 || part == ON_SCROLL_BAR)
27188 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27189 else
27190 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27191 #endif
27192
27193 /* Are we in a window whose display is up to date?
27194 And verify the buffer's text has not changed. */
27195 b = XBUFFER (w->buffer);
27196 if (part == ON_TEXT
27197 && EQ (w->window_end_valid, w->buffer)
27198 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27199 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27200 {
27201 int hpos, vpos, dx, dy, area = LAST_AREA;
27202 ptrdiff_t pos;
27203 struct glyph *glyph;
27204 Lisp_Object object;
27205 Lisp_Object mouse_face = Qnil, position;
27206 Lisp_Object *overlay_vec = NULL;
27207 ptrdiff_t i, noverlays;
27208 struct buffer *obuf;
27209 ptrdiff_t obegv, ozv;
27210 int same_region;
27211
27212 /* Find the glyph under X/Y. */
27213 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27214
27215 #ifdef HAVE_WINDOW_SYSTEM
27216 /* Look for :pointer property on image. */
27217 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27218 {
27219 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27220 if (img != NULL && IMAGEP (img->spec))
27221 {
27222 Lisp_Object image_map, hotspot;
27223 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27224 !NILP (image_map))
27225 && (hotspot = find_hot_spot (image_map,
27226 glyph->slice.img.x + dx,
27227 glyph->slice.img.y + dy),
27228 CONSP (hotspot))
27229 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27230 {
27231 Lisp_Object plist;
27232
27233 /* Could check XCAR (hotspot) to see if we enter/leave
27234 this hot-spot.
27235 If so, we could look for mouse-enter, mouse-leave
27236 properties in PLIST (and do something...). */
27237 hotspot = XCDR (hotspot);
27238 if (CONSP (hotspot)
27239 && (plist = XCAR (hotspot), CONSP (plist)))
27240 {
27241 pointer = Fplist_get (plist, Qpointer);
27242 if (NILP (pointer))
27243 pointer = Qhand;
27244 help_echo_string = Fplist_get (plist, Qhelp_echo);
27245 if (!NILP (help_echo_string))
27246 {
27247 help_echo_window = window;
27248 help_echo_object = glyph->object;
27249 help_echo_pos = glyph->charpos;
27250 }
27251 }
27252 }
27253 if (NILP (pointer))
27254 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27255 }
27256 }
27257 #endif /* HAVE_WINDOW_SYSTEM */
27258
27259 /* Clear mouse face if X/Y not over text. */
27260 if (glyph == NULL
27261 || area != TEXT_AREA
27262 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27263 /* Glyph's OBJECT is an integer for glyphs inserted by the
27264 display engine for its internal purposes, like truncation
27265 and continuation glyphs and blanks beyond the end of
27266 line's text on text terminals. If we are over such a
27267 glyph, we are not over any text. */
27268 || INTEGERP (glyph->object)
27269 /* R2L rows have a stretch glyph at their front, which
27270 stands for no text, whereas L2R rows have no glyphs at
27271 all beyond the end of text. Treat such stretch glyphs
27272 like we do with NULL glyphs in L2R rows. */
27273 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27274 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27275 && glyph->type == STRETCH_GLYPH
27276 && glyph->avoid_cursor_p))
27277 {
27278 if (clear_mouse_face (hlinfo))
27279 cursor = No_Cursor;
27280 #ifdef HAVE_WINDOW_SYSTEM
27281 if (FRAME_WINDOW_P (f) && NILP (pointer))
27282 {
27283 if (area != TEXT_AREA)
27284 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27285 else
27286 pointer = Vvoid_text_area_pointer;
27287 }
27288 #endif
27289 goto set_cursor;
27290 }
27291
27292 pos = glyph->charpos;
27293 object = glyph->object;
27294 if (!STRINGP (object) && !BUFFERP (object))
27295 goto set_cursor;
27296
27297 /* If we get an out-of-range value, return now; avoid an error. */
27298 if (BUFFERP (object) && pos > BUF_Z (b))
27299 goto set_cursor;
27300
27301 /* Make the window's buffer temporarily current for
27302 overlays_at and compute_char_face. */
27303 obuf = current_buffer;
27304 current_buffer = b;
27305 obegv = BEGV;
27306 ozv = ZV;
27307 BEGV = BEG;
27308 ZV = Z;
27309
27310 /* Is this char mouse-active or does it have help-echo? */
27311 position = make_number (pos);
27312
27313 if (BUFFERP (object))
27314 {
27315 /* Put all the overlays we want in a vector in overlay_vec. */
27316 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27317 /* Sort overlays into increasing priority order. */
27318 noverlays = sort_overlays (overlay_vec, noverlays, w);
27319 }
27320 else
27321 noverlays = 0;
27322
27323 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27324
27325 if (same_region)
27326 cursor = No_Cursor;
27327
27328 /* Check mouse-face highlighting. */
27329 if (! same_region
27330 /* If there exists an overlay with mouse-face overlapping
27331 the one we are currently highlighting, we have to
27332 check if we enter the overlapping overlay, and then
27333 highlight only that. */
27334 || (OVERLAYP (hlinfo->mouse_face_overlay)
27335 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27336 {
27337 /* Find the highest priority overlay with a mouse-face. */
27338 Lisp_Object overlay = Qnil;
27339 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27340 {
27341 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27342 if (!NILP (mouse_face))
27343 overlay = overlay_vec[i];
27344 }
27345
27346 /* If we're highlighting the same overlay as before, there's
27347 no need to do that again. */
27348 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27349 goto check_help_echo;
27350 hlinfo->mouse_face_overlay = overlay;
27351
27352 /* Clear the display of the old active region, if any. */
27353 if (clear_mouse_face (hlinfo))
27354 cursor = No_Cursor;
27355
27356 /* If no overlay applies, get a text property. */
27357 if (NILP (overlay))
27358 mouse_face = Fget_text_property (position, Qmouse_face, object);
27359
27360 /* Next, compute the bounds of the mouse highlighting and
27361 display it. */
27362 if (!NILP (mouse_face) && STRINGP (object))
27363 {
27364 /* The mouse-highlighting comes from a display string
27365 with a mouse-face. */
27366 Lisp_Object s, e;
27367 ptrdiff_t ignore;
27368
27369 s = Fprevious_single_property_change
27370 (make_number (pos + 1), Qmouse_face, object, Qnil);
27371 e = Fnext_single_property_change
27372 (position, Qmouse_face, object, Qnil);
27373 if (NILP (s))
27374 s = make_number (0);
27375 if (NILP (e))
27376 e = make_number (SCHARS (object) - 1);
27377 mouse_face_from_string_pos (w, hlinfo, object,
27378 XINT (s), XINT (e));
27379 hlinfo->mouse_face_past_end = 0;
27380 hlinfo->mouse_face_window = window;
27381 hlinfo->mouse_face_face_id
27382 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27383 glyph->face_id, 1);
27384 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27385 cursor = No_Cursor;
27386 }
27387 else
27388 {
27389 /* The mouse-highlighting, if any, comes from an overlay
27390 or text property in the buffer. */
27391 Lisp_Object buffer IF_LINT (= Qnil);
27392 Lisp_Object disp_string IF_LINT (= Qnil);
27393
27394 if (STRINGP (object))
27395 {
27396 /* If we are on a display string with no mouse-face,
27397 check if the text under it has one. */
27398 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27399 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27400 pos = string_buffer_position (object, start);
27401 if (pos > 0)
27402 {
27403 mouse_face = get_char_property_and_overlay
27404 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27405 buffer = w->buffer;
27406 disp_string = object;
27407 }
27408 }
27409 else
27410 {
27411 buffer = object;
27412 disp_string = Qnil;
27413 }
27414
27415 if (!NILP (mouse_face))
27416 {
27417 Lisp_Object before, after;
27418 Lisp_Object before_string, after_string;
27419 /* To correctly find the limits of mouse highlight
27420 in a bidi-reordered buffer, we must not use the
27421 optimization of limiting the search in
27422 previous-single-property-change and
27423 next-single-property-change, because
27424 rows_from_pos_range needs the real start and end
27425 positions to DTRT in this case. That's because
27426 the first row visible in a window does not
27427 necessarily display the character whose position
27428 is the smallest. */
27429 Lisp_Object lim1 =
27430 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27431 ? Fmarker_position (w->start)
27432 : Qnil;
27433 Lisp_Object lim2 =
27434 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27435 ? make_number (BUF_Z (XBUFFER (buffer))
27436 - XFASTINT (w->window_end_pos))
27437 : Qnil;
27438
27439 if (NILP (overlay))
27440 {
27441 /* Handle the text property case. */
27442 before = Fprevious_single_property_change
27443 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27444 after = Fnext_single_property_change
27445 (make_number (pos), Qmouse_face, buffer, lim2);
27446 before_string = after_string = Qnil;
27447 }
27448 else
27449 {
27450 /* Handle the overlay case. */
27451 before = Foverlay_start (overlay);
27452 after = Foverlay_end (overlay);
27453 before_string = Foverlay_get (overlay, Qbefore_string);
27454 after_string = Foverlay_get (overlay, Qafter_string);
27455
27456 if (!STRINGP (before_string)) before_string = Qnil;
27457 if (!STRINGP (after_string)) after_string = Qnil;
27458 }
27459
27460 mouse_face_from_buffer_pos (window, hlinfo, pos,
27461 NILP (before)
27462 ? 1
27463 : XFASTINT (before),
27464 NILP (after)
27465 ? BUF_Z (XBUFFER (buffer))
27466 : XFASTINT (after),
27467 before_string, after_string,
27468 disp_string);
27469 cursor = No_Cursor;
27470 }
27471 }
27472 }
27473
27474 check_help_echo:
27475
27476 /* Look for a `help-echo' property. */
27477 if (NILP (help_echo_string)) {
27478 Lisp_Object help, overlay;
27479
27480 /* Check overlays first. */
27481 help = overlay = Qnil;
27482 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27483 {
27484 overlay = overlay_vec[i];
27485 help = Foverlay_get (overlay, Qhelp_echo);
27486 }
27487
27488 if (!NILP (help))
27489 {
27490 help_echo_string = help;
27491 help_echo_window = window;
27492 help_echo_object = overlay;
27493 help_echo_pos = pos;
27494 }
27495 else
27496 {
27497 Lisp_Object obj = glyph->object;
27498 ptrdiff_t charpos = glyph->charpos;
27499
27500 /* Try text properties. */
27501 if (STRINGP (obj)
27502 && charpos >= 0
27503 && charpos < SCHARS (obj))
27504 {
27505 help = Fget_text_property (make_number (charpos),
27506 Qhelp_echo, obj);
27507 if (NILP (help))
27508 {
27509 /* If the string itself doesn't specify a help-echo,
27510 see if the buffer text ``under'' it does. */
27511 struct glyph_row *r
27512 = MATRIX_ROW (w->current_matrix, vpos);
27513 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27514 ptrdiff_t p = string_buffer_position (obj, start);
27515 if (p > 0)
27516 {
27517 help = Fget_char_property (make_number (p),
27518 Qhelp_echo, w->buffer);
27519 if (!NILP (help))
27520 {
27521 charpos = p;
27522 obj = w->buffer;
27523 }
27524 }
27525 }
27526 }
27527 else if (BUFFERP (obj)
27528 && charpos >= BEGV
27529 && charpos < ZV)
27530 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27531 obj);
27532
27533 if (!NILP (help))
27534 {
27535 help_echo_string = help;
27536 help_echo_window = window;
27537 help_echo_object = obj;
27538 help_echo_pos = charpos;
27539 }
27540 }
27541 }
27542
27543 #ifdef HAVE_WINDOW_SYSTEM
27544 /* Look for a `pointer' property. */
27545 if (FRAME_WINDOW_P (f) && NILP (pointer))
27546 {
27547 /* Check overlays first. */
27548 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27549 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27550
27551 if (NILP (pointer))
27552 {
27553 Lisp_Object obj = glyph->object;
27554 ptrdiff_t charpos = glyph->charpos;
27555
27556 /* Try text properties. */
27557 if (STRINGP (obj)
27558 && charpos >= 0
27559 && charpos < SCHARS (obj))
27560 {
27561 pointer = Fget_text_property (make_number (charpos),
27562 Qpointer, obj);
27563 if (NILP (pointer))
27564 {
27565 /* If the string itself doesn't specify a pointer,
27566 see if the buffer text ``under'' it does. */
27567 struct glyph_row *r
27568 = MATRIX_ROW (w->current_matrix, vpos);
27569 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27570 ptrdiff_t p = string_buffer_position (obj, start);
27571 if (p > 0)
27572 pointer = Fget_char_property (make_number (p),
27573 Qpointer, w->buffer);
27574 }
27575 }
27576 else if (BUFFERP (obj)
27577 && charpos >= BEGV
27578 && charpos < ZV)
27579 pointer = Fget_text_property (make_number (charpos),
27580 Qpointer, obj);
27581 }
27582 }
27583 #endif /* HAVE_WINDOW_SYSTEM */
27584
27585 BEGV = obegv;
27586 ZV = ozv;
27587 current_buffer = obuf;
27588 }
27589
27590 set_cursor:
27591
27592 #ifdef HAVE_WINDOW_SYSTEM
27593 if (FRAME_WINDOW_P (f))
27594 define_frame_cursor1 (f, cursor, pointer);
27595 #else
27596 /* This is here to prevent a compiler error, about "label at end of
27597 compound statement". */
27598 return;
27599 #endif
27600 }
27601
27602
27603 /* EXPORT for RIF:
27604 Clear any mouse-face on window W. This function is part of the
27605 redisplay interface, and is called from try_window_id and similar
27606 functions to ensure the mouse-highlight is off. */
27607
27608 void
27609 x_clear_window_mouse_face (struct window *w)
27610 {
27611 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27612 Lisp_Object window;
27613
27614 BLOCK_INPUT;
27615 XSETWINDOW (window, w);
27616 if (EQ (window, hlinfo->mouse_face_window))
27617 clear_mouse_face (hlinfo);
27618 UNBLOCK_INPUT;
27619 }
27620
27621
27622 /* EXPORT:
27623 Just discard the mouse face information for frame F, if any.
27624 This is used when the size of F is changed. */
27625
27626 void
27627 cancel_mouse_face (struct frame *f)
27628 {
27629 Lisp_Object window;
27630 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27631
27632 window = hlinfo->mouse_face_window;
27633 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27634 {
27635 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27636 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27637 hlinfo->mouse_face_window = Qnil;
27638 }
27639 }
27640
27641
27642 \f
27643 /***********************************************************************
27644 Exposure Events
27645 ***********************************************************************/
27646
27647 #ifdef HAVE_WINDOW_SYSTEM
27648
27649 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27650 which intersects rectangle R. R is in window-relative coordinates. */
27651
27652 static void
27653 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27654 enum glyph_row_area area)
27655 {
27656 struct glyph *first = row->glyphs[area];
27657 struct glyph *end = row->glyphs[area] + row->used[area];
27658 struct glyph *last;
27659 int first_x, start_x, x;
27660
27661 if (area == TEXT_AREA && row->fill_line_p)
27662 /* If row extends face to end of line write the whole line. */
27663 draw_glyphs (w, 0, row, area,
27664 0, row->used[area],
27665 DRAW_NORMAL_TEXT, 0);
27666 else
27667 {
27668 /* Set START_X to the window-relative start position for drawing glyphs of
27669 AREA. The first glyph of the text area can be partially visible.
27670 The first glyphs of other areas cannot. */
27671 start_x = window_box_left_offset (w, area);
27672 x = start_x;
27673 if (area == TEXT_AREA)
27674 x += row->x;
27675
27676 /* Find the first glyph that must be redrawn. */
27677 while (first < end
27678 && x + first->pixel_width < r->x)
27679 {
27680 x += first->pixel_width;
27681 ++first;
27682 }
27683
27684 /* Find the last one. */
27685 last = first;
27686 first_x = x;
27687 while (last < end
27688 && x < r->x + r->width)
27689 {
27690 x += last->pixel_width;
27691 ++last;
27692 }
27693
27694 /* Repaint. */
27695 if (last > first)
27696 draw_glyphs (w, first_x - start_x, row, area,
27697 first - row->glyphs[area], last - row->glyphs[area],
27698 DRAW_NORMAL_TEXT, 0);
27699 }
27700 }
27701
27702
27703 /* Redraw the parts of the glyph row ROW on window W intersecting
27704 rectangle R. R is in window-relative coordinates. Value is
27705 non-zero if mouse-face was overwritten. */
27706
27707 static int
27708 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27709 {
27710 xassert (row->enabled_p);
27711
27712 if (row->mode_line_p || w->pseudo_window_p)
27713 draw_glyphs (w, 0, row, TEXT_AREA,
27714 0, row->used[TEXT_AREA],
27715 DRAW_NORMAL_TEXT, 0);
27716 else
27717 {
27718 if (row->used[LEFT_MARGIN_AREA])
27719 expose_area (w, row, r, LEFT_MARGIN_AREA);
27720 if (row->used[TEXT_AREA])
27721 expose_area (w, row, r, TEXT_AREA);
27722 if (row->used[RIGHT_MARGIN_AREA])
27723 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27724 draw_row_fringe_bitmaps (w, row);
27725 }
27726
27727 return row->mouse_face_p;
27728 }
27729
27730
27731 /* Redraw those parts of glyphs rows during expose event handling that
27732 overlap other rows. Redrawing of an exposed line writes over parts
27733 of lines overlapping that exposed line; this function fixes that.
27734
27735 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27736 row in W's current matrix that is exposed and overlaps other rows.
27737 LAST_OVERLAPPING_ROW is the last such row. */
27738
27739 static void
27740 expose_overlaps (struct window *w,
27741 struct glyph_row *first_overlapping_row,
27742 struct glyph_row *last_overlapping_row,
27743 XRectangle *r)
27744 {
27745 struct glyph_row *row;
27746
27747 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27748 if (row->overlapping_p)
27749 {
27750 xassert (row->enabled_p && !row->mode_line_p);
27751
27752 row->clip = r;
27753 if (row->used[LEFT_MARGIN_AREA])
27754 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27755
27756 if (row->used[TEXT_AREA])
27757 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27758
27759 if (row->used[RIGHT_MARGIN_AREA])
27760 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27761 row->clip = NULL;
27762 }
27763 }
27764
27765
27766 /* Return non-zero if W's cursor intersects rectangle R. */
27767
27768 static int
27769 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27770 {
27771 XRectangle cr, result;
27772 struct glyph *cursor_glyph;
27773 struct glyph_row *row;
27774
27775 if (w->phys_cursor.vpos >= 0
27776 && w->phys_cursor.vpos < w->current_matrix->nrows
27777 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27778 row->enabled_p)
27779 && row->cursor_in_fringe_p)
27780 {
27781 /* Cursor is in the fringe. */
27782 cr.x = window_box_right_offset (w,
27783 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27784 ? RIGHT_MARGIN_AREA
27785 : TEXT_AREA));
27786 cr.y = row->y;
27787 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27788 cr.height = row->height;
27789 return x_intersect_rectangles (&cr, r, &result);
27790 }
27791
27792 cursor_glyph = get_phys_cursor_glyph (w);
27793 if (cursor_glyph)
27794 {
27795 /* r is relative to W's box, but w->phys_cursor.x is relative
27796 to left edge of W's TEXT area. Adjust it. */
27797 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27798 cr.y = w->phys_cursor.y;
27799 cr.width = cursor_glyph->pixel_width;
27800 cr.height = w->phys_cursor_height;
27801 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27802 I assume the effect is the same -- and this is portable. */
27803 return x_intersect_rectangles (&cr, r, &result);
27804 }
27805 /* If we don't understand the format, pretend we're not in the hot-spot. */
27806 return 0;
27807 }
27808
27809
27810 /* EXPORT:
27811 Draw a vertical window border to the right of window W if W doesn't
27812 have vertical scroll bars. */
27813
27814 void
27815 x_draw_vertical_border (struct window *w)
27816 {
27817 struct frame *f = XFRAME (WINDOW_FRAME (w));
27818
27819 /* We could do better, if we knew what type of scroll-bar the adjacent
27820 windows (on either side) have... But we don't :-(
27821 However, I think this works ok. ++KFS 2003-04-25 */
27822
27823 /* Redraw borders between horizontally adjacent windows. Don't
27824 do it for frames with vertical scroll bars because either the
27825 right scroll bar of a window, or the left scroll bar of its
27826 neighbor will suffice as a border. */
27827 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27828 return;
27829
27830 if (!WINDOW_RIGHTMOST_P (w)
27831 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27832 {
27833 int x0, x1, y0, y1;
27834
27835 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27836 y1 -= 1;
27837
27838 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27839 x1 -= 1;
27840
27841 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27842 }
27843 else if (!WINDOW_LEFTMOST_P (w)
27844 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27845 {
27846 int x0, x1, y0, y1;
27847
27848 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27849 y1 -= 1;
27850
27851 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27852 x0 -= 1;
27853
27854 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27855 }
27856 }
27857
27858
27859 /* Redraw the part of window W intersection rectangle FR. Pixel
27860 coordinates in FR are frame-relative. Call this function with
27861 input blocked. Value is non-zero if the exposure overwrites
27862 mouse-face. */
27863
27864 static int
27865 expose_window (struct window *w, XRectangle *fr)
27866 {
27867 struct frame *f = XFRAME (w->frame);
27868 XRectangle wr, r;
27869 int mouse_face_overwritten_p = 0;
27870
27871 /* If window is not yet fully initialized, do nothing. This can
27872 happen when toolkit scroll bars are used and a window is split.
27873 Reconfiguring the scroll bar will generate an expose for a newly
27874 created window. */
27875 if (w->current_matrix == NULL)
27876 return 0;
27877
27878 /* When we're currently updating the window, display and current
27879 matrix usually don't agree. Arrange for a thorough display
27880 later. */
27881 if (w == updated_window)
27882 {
27883 SET_FRAME_GARBAGED (f);
27884 return 0;
27885 }
27886
27887 /* Frame-relative pixel rectangle of W. */
27888 wr.x = WINDOW_LEFT_EDGE_X (w);
27889 wr.y = WINDOW_TOP_EDGE_Y (w);
27890 wr.width = WINDOW_TOTAL_WIDTH (w);
27891 wr.height = WINDOW_TOTAL_HEIGHT (w);
27892
27893 if (x_intersect_rectangles (fr, &wr, &r))
27894 {
27895 int yb = window_text_bottom_y (w);
27896 struct glyph_row *row;
27897 int cursor_cleared_p, phys_cursor_on_p;
27898 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27899
27900 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27901 r.x, r.y, r.width, r.height));
27902
27903 /* Convert to window coordinates. */
27904 r.x -= WINDOW_LEFT_EDGE_X (w);
27905 r.y -= WINDOW_TOP_EDGE_Y (w);
27906
27907 /* Turn off the cursor. */
27908 if (!w->pseudo_window_p
27909 && phys_cursor_in_rect_p (w, &r))
27910 {
27911 x_clear_cursor (w);
27912 cursor_cleared_p = 1;
27913 }
27914 else
27915 cursor_cleared_p = 0;
27916
27917 /* If the row containing the cursor extends face to end of line,
27918 then expose_area might overwrite the cursor outside the
27919 rectangle and thus notice_overwritten_cursor might clear
27920 w->phys_cursor_on_p. We remember the original value and
27921 check later if it is changed. */
27922 phys_cursor_on_p = w->phys_cursor_on_p;
27923
27924 /* Update lines intersecting rectangle R. */
27925 first_overlapping_row = last_overlapping_row = NULL;
27926 for (row = w->current_matrix->rows;
27927 row->enabled_p;
27928 ++row)
27929 {
27930 int y0 = row->y;
27931 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27932
27933 if ((y0 >= r.y && y0 < r.y + r.height)
27934 || (y1 > r.y && y1 < r.y + r.height)
27935 || (r.y >= y0 && r.y < y1)
27936 || (r.y + r.height > y0 && r.y + r.height < y1))
27937 {
27938 /* A header line may be overlapping, but there is no need
27939 to fix overlapping areas for them. KFS 2005-02-12 */
27940 if (row->overlapping_p && !row->mode_line_p)
27941 {
27942 if (first_overlapping_row == NULL)
27943 first_overlapping_row = row;
27944 last_overlapping_row = row;
27945 }
27946
27947 row->clip = fr;
27948 if (expose_line (w, row, &r))
27949 mouse_face_overwritten_p = 1;
27950 row->clip = NULL;
27951 }
27952 else if (row->overlapping_p)
27953 {
27954 /* We must redraw a row overlapping the exposed area. */
27955 if (y0 < r.y
27956 ? y0 + row->phys_height > r.y
27957 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27958 {
27959 if (first_overlapping_row == NULL)
27960 first_overlapping_row = row;
27961 last_overlapping_row = row;
27962 }
27963 }
27964
27965 if (y1 >= yb)
27966 break;
27967 }
27968
27969 /* Display the mode line if there is one. */
27970 if (WINDOW_WANTS_MODELINE_P (w)
27971 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27972 row->enabled_p)
27973 && row->y < r.y + r.height)
27974 {
27975 if (expose_line (w, row, &r))
27976 mouse_face_overwritten_p = 1;
27977 }
27978
27979 if (!w->pseudo_window_p)
27980 {
27981 /* Fix the display of overlapping rows. */
27982 if (first_overlapping_row)
27983 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27984 fr);
27985
27986 /* Draw border between windows. */
27987 x_draw_vertical_border (w);
27988
27989 /* Turn the cursor on again. */
27990 if (cursor_cleared_p
27991 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27992 update_window_cursor (w, 1);
27993 }
27994 }
27995
27996 return mouse_face_overwritten_p;
27997 }
27998
27999
28000
28001 /* Redraw (parts) of all windows in the window tree rooted at W that
28002 intersect R. R contains frame pixel coordinates. Value is
28003 non-zero if the exposure overwrites mouse-face. */
28004
28005 static int
28006 expose_window_tree (struct window *w, XRectangle *r)
28007 {
28008 struct frame *f = XFRAME (w->frame);
28009 int mouse_face_overwritten_p = 0;
28010
28011 while (w && !FRAME_GARBAGED_P (f))
28012 {
28013 if (!NILP (w->hchild))
28014 mouse_face_overwritten_p
28015 |= expose_window_tree (XWINDOW (w->hchild), r);
28016 else if (!NILP (w->vchild))
28017 mouse_face_overwritten_p
28018 |= expose_window_tree (XWINDOW (w->vchild), r);
28019 else
28020 mouse_face_overwritten_p |= expose_window (w, r);
28021
28022 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28023 }
28024
28025 return mouse_face_overwritten_p;
28026 }
28027
28028
28029 /* EXPORT:
28030 Redisplay an exposed area of frame F. X and Y are the upper-left
28031 corner of the exposed rectangle. W and H are width and height of
28032 the exposed area. All are pixel values. W or H zero means redraw
28033 the entire frame. */
28034
28035 void
28036 expose_frame (struct frame *f, int x, int y, int w, int h)
28037 {
28038 XRectangle r;
28039 int mouse_face_overwritten_p = 0;
28040
28041 TRACE ((stderr, "expose_frame "));
28042
28043 /* No need to redraw if frame will be redrawn soon. */
28044 if (FRAME_GARBAGED_P (f))
28045 {
28046 TRACE ((stderr, " garbaged\n"));
28047 return;
28048 }
28049
28050 /* If basic faces haven't been realized yet, there is no point in
28051 trying to redraw anything. This can happen when we get an expose
28052 event while Emacs is starting, e.g. by moving another window. */
28053 if (FRAME_FACE_CACHE (f) == NULL
28054 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28055 {
28056 TRACE ((stderr, " no faces\n"));
28057 return;
28058 }
28059
28060 if (w == 0 || h == 0)
28061 {
28062 r.x = r.y = 0;
28063 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28064 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28065 }
28066 else
28067 {
28068 r.x = x;
28069 r.y = y;
28070 r.width = w;
28071 r.height = h;
28072 }
28073
28074 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28075 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28076
28077 if (WINDOWP (f->tool_bar_window))
28078 mouse_face_overwritten_p
28079 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28080
28081 #ifdef HAVE_X_WINDOWS
28082 #ifndef MSDOS
28083 #ifndef USE_X_TOOLKIT
28084 if (WINDOWP (f->menu_bar_window))
28085 mouse_face_overwritten_p
28086 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28087 #endif /* not USE_X_TOOLKIT */
28088 #endif
28089 #endif
28090
28091 /* Some window managers support a focus-follows-mouse style with
28092 delayed raising of frames. Imagine a partially obscured frame,
28093 and moving the mouse into partially obscured mouse-face on that
28094 frame. The visible part of the mouse-face will be highlighted,
28095 then the WM raises the obscured frame. With at least one WM, KDE
28096 2.1, Emacs is not getting any event for the raising of the frame
28097 (even tried with SubstructureRedirectMask), only Expose events.
28098 These expose events will draw text normally, i.e. not
28099 highlighted. Which means we must redo the highlight here.
28100 Subsume it under ``we love X''. --gerd 2001-08-15 */
28101 /* Included in Windows version because Windows most likely does not
28102 do the right thing if any third party tool offers
28103 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28104 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28105 {
28106 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28107 if (f == hlinfo->mouse_face_mouse_frame)
28108 {
28109 int mouse_x = hlinfo->mouse_face_mouse_x;
28110 int mouse_y = hlinfo->mouse_face_mouse_y;
28111 clear_mouse_face (hlinfo);
28112 note_mouse_highlight (f, mouse_x, mouse_y);
28113 }
28114 }
28115 }
28116
28117
28118 /* EXPORT:
28119 Determine the intersection of two rectangles R1 and R2. Return
28120 the intersection in *RESULT. Value is non-zero if RESULT is not
28121 empty. */
28122
28123 int
28124 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28125 {
28126 XRectangle *left, *right;
28127 XRectangle *upper, *lower;
28128 int intersection_p = 0;
28129
28130 /* Rearrange so that R1 is the left-most rectangle. */
28131 if (r1->x < r2->x)
28132 left = r1, right = r2;
28133 else
28134 left = r2, right = r1;
28135
28136 /* X0 of the intersection is right.x0, if this is inside R1,
28137 otherwise there is no intersection. */
28138 if (right->x <= left->x + left->width)
28139 {
28140 result->x = right->x;
28141
28142 /* The right end of the intersection is the minimum of
28143 the right ends of left and right. */
28144 result->width = (min (left->x + left->width, right->x + right->width)
28145 - result->x);
28146
28147 /* Same game for Y. */
28148 if (r1->y < r2->y)
28149 upper = r1, lower = r2;
28150 else
28151 upper = r2, lower = r1;
28152
28153 /* The upper end of the intersection is lower.y0, if this is inside
28154 of upper. Otherwise, there is no intersection. */
28155 if (lower->y <= upper->y + upper->height)
28156 {
28157 result->y = lower->y;
28158
28159 /* The lower end of the intersection is the minimum of the lower
28160 ends of upper and lower. */
28161 result->height = (min (lower->y + lower->height,
28162 upper->y + upper->height)
28163 - result->y);
28164 intersection_p = 1;
28165 }
28166 }
28167
28168 return intersection_p;
28169 }
28170
28171 #endif /* HAVE_WINDOW_SYSTEM */
28172
28173 \f
28174 /***********************************************************************
28175 Initialization
28176 ***********************************************************************/
28177
28178 void
28179 syms_of_xdisp (void)
28180 {
28181 Vwith_echo_area_save_vector = Qnil;
28182 staticpro (&Vwith_echo_area_save_vector);
28183
28184 Vmessage_stack = Qnil;
28185 staticpro (&Vmessage_stack);
28186
28187 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28188
28189 message_dolog_marker1 = Fmake_marker ();
28190 staticpro (&message_dolog_marker1);
28191 message_dolog_marker2 = Fmake_marker ();
28192 staticpro (&message_dolog_marker2);
28193 message_dolog_marker3 = Fmake_marker ();
28194 staticpro (&message_dolog_marker3);
28195
28196 #if GLYPH_DEBUG
28197 defsubr (&Sdump_frame_glyph_matrix);
28198 defsubr (&Sdump_glyph_matrix);
28199 defsubr (&Sdump_glyph_row);
28200 defsubr (&Sdump_tool_bar_row);
28201 defsubr (&Strace_redisplay);
28202 defsubr (&Strace_to_stderr);
28203 #endif
28204 #ifdef HAVE_WINDOW_SYSTEM
28205 defsubr (&Stool_bar_lines_needed);
28206 defsubr (&Slookup_image_map);
28207 #endif
28208 defsubr (&Sformat_mode_line);
28209 defsubr (&Sinvisible_p);
28210 defsubr (&Scurrent_bidi_paragraph_direction);
28211
28212 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28213 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28214 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28215 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28216 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28217 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28218 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28219 DEFSYM (Qeval, "eval");
28220 DEFSYM (QCdata, ":data");
28221 DEFSYM (Qdisplay, "display");
28222 DEFSYM (Qspace_width, "space-width");
28223 DEFSYM (Qraise, "raise");
28224 DEFSYM (Qslice, "slice");
28225 DEFSYM (Qspace, "space");
28226 DEFSYM (Qmargin, "margin");
28227 DEFSYM (Qpointer, "pointer");
28228 DEFSYM (Qleft_margin, "left-margin");
28229 DEFSYM (Qright_margin, "right-margin");
28230 DEFSYM (Qcenter, "center");
28231 DEFSYM (Qline_height, "line-height");
28232 DEFSYM (QCalign_to, ":align-to");
28233 DEFSYM (QCrelative_width, ":relative-width");
28234 DEFSYM (QCrelative_height, ":relative-height");
28235 DEFSYM (QCeval, ":eval");
28236 DEFSYM (QCpropertize, ":propertize");
28237 DEFSYM (QCfile, ":file");
28238 DEFSYM (Qfontified, "fontified");
28239 DEFSYM (Qfontification_functions, "fontification-functions");
28240 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28241 DEFSYM (Qescape_glyph, "escape-glyph");
28242 DEFSYM (Qnobreak_space, "nobreak-space");
28243 DEFSYM (Qimage, "image");
28244 DEFSYM (Qtext, "text");
28245 DEFSYM (Qboth, "both");
28246 DEFSYM (Qboth_horiz, "both-horiz");
28247 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28248 DEFSYM (QCmap, ":map");
28249 DEFSYM (QCpointer, ":pointer");
28250 DEFSYM (Qrect, "rect");
28251 DEFSYM (Qcircle, "circle");
28252 DEFSYM (Qpoly, "poly");
28253 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28254 DEFSYM (Qgrow_only, "grow-only");
28255 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28256 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28257 DEFSYM (Qposition, "position");
28258 DEFSYM (Qbuffer_position, "buffer-position");
28259 DEFSYM (Qobject, "object");
28260 DEFSYM (Qbar, "bar");
28261 DEFSYM (Qhbar, "hbar");
28262 DEFSYM (Qbox, "box");
28263 DEFSYM (Qhollow, "hollow");
28264 DEFSYM (Qhand, "hand");
28265 DEFSYM (Qarrow, "arrow");
28266 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28267
28268 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28269 Fcons (intern_c_string ("void-variable"), Qnil)),
28270 Qnil);
28271 staticpro (&list_of_error);
28272
28273 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28274 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28275 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28276 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28277
28278 echo_buffer[0] = echo_buffer[1] = Qnil;
28279 staticpro (&echo_buffer[0]);
28280 staticpro (&echo_buffer[1]);
28281
28282 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28283 staticpro (&echo_area_buffer[0]);
28284 staticpro (&echo_area_buffer[1]);
28285
28286 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28287 staticpro (&Vmessages_buffer_name);
28288
28289 mode_line_proptrans_alist = Qnil;
28290 staticpro (&mode_line_proptrans_alist);
28291 mode_line_string_list = Qnil;
28292 staticpro (&mode_line_string_list);
28293 mode_line_string_face = Qnil;
28294 staticpro (&mode_line_string_face);
28295 mode_line_string_face_prop = Qnil;
28296 staticpro (&mode_line_string_face_prop);
28297 Vmode_line_unwind_vector = Qnil;
28298 staticpro (&Vmode_line_unwind_vector);
28299
28300 help_echo_string = Qnil;
28301 staticpro (&help_echo_string);
28302 help_echo_object = Qnil;
28303 staticpro (&help_echo_object);
28304 help_echo_window = Qnil;
28305 staticpro (&help_echo_window);
28306 previous_help_echo_string = Qnil;
28307 staticpro (&previous_help_echo_string);
28308 help_echo_pos = -1;
28309
28310 DEFSYM (Qright_to_left, "right-to-left");
28311 DEFSYM (Qleft_to_right, "left-to-right");
28312
28313 #ifdef HAVE_WINDOW_SYSTEM
28314 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28315 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28316 For example, if a block cursor is over a tab, it will be drawn as
28317 wide as that tab on the display. */);
28318 x_stretch_cursor_p = 0;
28319 #endif
28320
28321 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28322 doc: /* Non-nil means highlight trailing whitespace.
28323 The face used for trailing whitespace is `trailing-whitespace'. */);
28324 Vshow_trailing_whitespace = Qnil;
28325
28326 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28327 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28328 If the value is t, Emacs highlights non-ASCII chars which have the
28329 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28330 or `escape-glyph' face respectively.
28331
28332 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28333 U+2011 (non-breaking hyphen) are affected.
28334
28335 Any other non-nil value means to display these characters as a escape
28336 glyph followed by an ordinary space or hyphen.
28337
28338 A value of nil means no special handling of these characters. */);
28339 Vnobreak_char_display = Qt;
28340
28341 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28342 doc: /* The pointer shape to show in void text areas.
28343 A value of nil means to show the text pointer. Other options are `arrow',
28344 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28345 Vvoid_text_area_pointer = Qarrow;
28346
28347 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28348 doc: /* Non-nil means don't actually do any redisplay.
28349 This is used for internal purposes. */);
28350 Vinhibit_redisplay = Qnil;
28351
28352 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28353 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28354 Vglobal_mode_string = Qnil;
28355
28356 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28357 doc: /* Marker for where to display an arrow on top of the buffer text.
28358 This must be the beginning of a line in order to work.
28359 See also `overlay-arrow-string'. */);
28360 Voverlay_arrow_position = Qnil;
28361
28362 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28363 doc: /* String to display as an arrow in non-window frames.
28364 See also `overlay-arrow-position'. */);
28365 Voverlay_arrow_string = make_pure_c_string ("=>");
28366
28367 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28368 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28369 The symbols on this list are examined during redisplay to determine
28370 where to display overlay arrows. */);
28371 Voverlay_arrow_variable_list
28372 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28373
28374 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28375 doc: /* The number of lines to try scrolling a window by when point moves out.
28376 If that fails to bring point back on frame, point is centered instead.
28377 If this is zero, point is always centered after it moves off frame.
28378 If you want scrolling to always be a line at a time, you should set
28379 `scroll-conservatively' to a large value rather than set this to 1. */);
28380
28381 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28382 doc: /* Scroll up to this many lines, to bring point back on screen.
28383 If point moves off-screen, redisplay will scroll by up to
28384 `scroll-conservatively' lines in order to bring point just barely
28385 onto the screen again. If that cannot be done, then redisplay
28386 recenters point as usual.
28387
28388 If the value is greater than 100, redisplay will never recenter point,
28389 but will always scroll just enough text to bring point into view, even
28390 if you move far away.
28391
28392 A value of zero means always recenter point if it moves off screen. */);
28393 scroll_conservatively = 0;
28394
28395 DEFVAR_INT ("scroll-margin", scroll_margin,
28396 doc: /* Number of lines of margin at the top and bottom of a window.
28397 Recenter the window whenever point gets within this many lines
28398 of the top or bottom of the window. */);
28399 scroll_margin = 0;
28400
28401 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28402 doc: /* Pixels per inch value for non-window system displays.
28403 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28404 Vdisplay_pixels_per_inch = make_float (72.0);
28405
28406 #if GLYPH_DEBUG
28407 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28408 #endif
28409
28410 DEFVAR_LISP ("truncate-partial-width-windows",
28411 Vtruncate_partial_width_windows,
28412 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28413 For an integer value, truncate lines in each window narrower than the
28414 full frame width, provided the window width is less than that integer;
28415 otherwise, respect the value of `truncate-lines'.
28416
28417 For any other non-nil value, truncate lines in all windows that do
28418 not span the full frame width.
28419
28420 A value of nil means to respect the value of `truncate-lines'.
28421
28422 If `word-wrap' is enabled, you might want to reduce this. */);
28423 Vtruncate_partial_width_windows = make_number (50);
28424
28425 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28426 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28427 Any other value means to use the appropriate face, `mode-line',
28428 `header-line', or `menu' respectively. */);
28429 mode_line_inverse_video = 1;
28430
28431 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28432 doc: /* Maximum buffer size for which line number should be displayed.
28433 If the buffer is bigger than this, the line number does not appear
28434 in the mode line. A value of nil means no limit. */);
28435 Vline_number_display_limit = Qnil;
28436
28437 DEFVAR_INT ("line-number-display-limit-width",
28438 line_number_display_limit_width,
28439 doc: /* Maximum line width (in characters) for line number display.
28440 If the average length of the lines near point is bigger than this, then the
28441 line number may be omitted from the mode line. */);
28442 line_number_display_limit_width = 200;
28443
28444 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28445 doc: /* Non-nil means highlight region even in nonselected windows. */);
28446 highlight_nonselected_windows = 0;
28447
28448 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28449 doc: /* Non-nil if more than one frame is visible on this display.
28450 Minibuffer-only frames don't count, but iconified frames do.
28451 This variable is not guaranteed to be accurate except while processing
28452 `frame-title-format' and `icon-title-format'. */);
28453
28454 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28455 doc: /* Template for displaying the title bar of visible frames.
28456 \(Assuming the window manager supports this feature.)
28457
28458 This variable has the same structure as `mode-line-format', except that
28459 the %c and %l constructs are ignored. It is used only on frames for
28460 which no explicit name has been set \(see `modify-frame-parameters'). */);
28461
28462 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28463 doc: /* Template for displaying the title bar of an iconified frame.
28464 \(Assuming the window manager supports this feature.)
28465 This variable has the same structure as `mode-line-format' (which see),
28466 and is used only on frames for which no explicit name has been set
28467 \(see `modify-frame-parameters'). */);
28468 Vicon_title_format
28469 = Vframe_title_format
28470 = pure_cons (intern_c_string ("multiple-frames"),
28471 pure_cons (make_pure_c_string ("%b"),
28472 pure_cons (pure_cons (empty_unibyte_string,
28473 pure_cons (intern_c_string ("invocation-name"),
28474 pure_cons (make_pure_c_string ("@"),
28475 pure_cons (intern_c_string ("system-name"),
28476 Qnil)))),
28477 Qnil)));
28478
28479 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28480 doc: /* Maximum number of lines to keep in the message log buffer.
28481 If nil, disable message logging. If t, log messages but don't truncate
28482 the buffer when it becomes large. */);
28483 Vmessage_log_max = make_number (100);
28484
28485 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28486 doc: /* Functions called before redisplay, if window sizes have changed.
28487 The value should be a list of functions that take one argument.
28488 Just before redisplay, for each frame, if any of its windows have changed
28489 size since the last redisplay, or have been split or deleted,
28490 all the functions in the list are called, with the frame as argument. */);
28491 Vwindow_size_change_functions = Qnil;
28492
28493 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28494 doc: /* List of functions to call before redisplaying a window with scrolling.
28495 Each function is called with two arguments, the window and its new
28496 display-start position. Note that these functions are also called by
28497 `set-window-buffer'. Also note that the value of `window-end' is not
28498 valid when these functions are called.
28499
28500 Warning: Do not use this feature to alter the way the window
28501 is scrolled. It is not designed for that, and such use probably won't
28502 work. */);
28503 Vwindow_scroll_functions = Qnil;
28504
28505 DEFVAR_LISP ("window-text-change-functions",
28506 Vwindow_text_change_functions,
28507 doc: /* Functions to call in redisplay when text in the window might change. */);
28508 Vwindow_text_change_functions = Qnil;
28509
28510 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28511 doc: /* Functions called when redisplay of a window reaches the end trigger.
28512 Each function is called with two arguments, the window and the end trigger value.
28513 See `set-window-redisplay-end-trigger'. */);
28514 Vredisplay_end_trigger_functions = Qnil;
28515
28516 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28517 doc: /* Non-nil means autoselect window with mouse pointer.
28518 If nil, do not autoselect windows.
28519 A positive number means delay autoselection by that many seconds: a
28520 window is autoselected only after the mouse has remained in that
28521 window for the duration of the delay.
28522 A negative number has a similar effect, but causes windows to be
28523 autoselected only after the mouse has stopped moving. \(Because of
28524 the way Emacs compares mouse events, you will occasionally wait twice
28525 that time before the window gets selected.\)
28526 Any other value means to autoselect window instantaneously when the
28527 mouse pointer enters it.
28528
28529 Autoselection selects the minibuffer only if it is active, and never
28530 unselects the minibuffer if it is active.
28531
28532 When customizing this variable make sure that the actual value of
28533 `focus-follows-mouse' matches the behavior of your window manager. */);
28534 Vmouse_autoselect_window = Qnil;
28535
28536 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28537 doc: /* Non-nil means automatically resize tool-bars.
28538 This dynamically changes the tool-bar's height to the minimum height
28539 that is needed to make all tool-bar items visible.
28540 If value is `grow-only', the tool-bar's height is only increased
28541 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28542 Vauto_resize_tool_bars = Qt;
28543
28544 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28545 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28546 auto_raise_tool_bar_buttons_p = 1;
28547
28548 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28549 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28550 make_cursor_line_fully_visible_p = 1;
28551
28552 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28553 doc: /* Border below tool-bar in pixels.
28554 If an integer, use it as the height of the border.
28555 If it is one of `internal-border-width' or `border-width', use the
28556 value of the corresponding frame parameter.
28557 Otherwise, no border is added below the tool-bar. */);
28558 Vtool_bar_border = Qinternal_border_width;
28559
28560 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28561 doc: /* Margin around tool-bar buttons in pixels.
28562 If an integer, use that for both horizontal and vertical margins.
28563 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28564 HORZ specifying the horizontal margin, and VERT specifying the
28565 vertical margin. */);
28566 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28567
28568 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28569 doc: /* Relief thickness of tool-bar buttons. */);
28570 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28571
28572 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28573 doc: /* Tool bar style to use.
28574 It can be one of
28575 image - show images only
28576 text - show text only
28577 both - show both, text below image
28578 both-horiz - show text to the right of the image
28579 text-image-horiz - show text to the left of the image
28580 any other - use system default or image if no system default. */);
28581 Vtool_bar_style = Qnil;
28582
28583 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28584 doc: /* Maximum number of characters a label can have to be shown.
28585 The tool bar style must also show labels for this to have any effect, see
28586 `tool-bar-style'. */);
28587 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28588
28589 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28590 doc: /* List of functions to call to fontify regions of text.
28591 Each function is called with one argument POS. Functions must
28592 fontify a region starting at POS in the current buffer, and give
28593 fontified regions the property `fontified'. */);
28594 Vfontification_functions = Qnil;
28595 Fmake_variable_buffer_local (Qfontification_functions);
28596
28597 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28598 unibyte_display_via_language_environment,
28599 doc: /* Non-nil means display unibyte text according to language environment.
28600 Specifically, this means that raw bytes in the range 160-255 decimal
28601 are displayed by converting them to the equivalent multibyte characters
28602 according to the current language environment. As a result, they are
28603 displayed according to the current fontset.
28604
28605 Note that this variable affects only how these bytes are displayed,
28606 but does not change the fact they are interpreted as raw bytes. */);
28607 unibyte_display_via_language_environment = 0;
28608
28609 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28610 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28611 If a float, it specifies a fraction of the mini-window frame's height.
28612 If an integer, it specifies a number of lines. */);
28613 Vmax_mini_window_height = make_float (0.25);
28614
28615 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28616 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28617 A value of nil means don't automatically resize mini-windows.
28618 A value of t means resize them to fit the text displayed in them.
28619 A value of `grow-only', the default, means let mini-windows grow only;
28620 they return to their normal size when the minibuffer is closed, or the
28621 echo area becomes empty. */);
28622 Vresize_mini_windows = Qgrow_only;
28623
28624 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28625 doc: /* Alist specifying how to blink the cursor off.
28626 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28627 `cursor-type' frame-parameter or variable equals ON-STATE,
28628 comparing using `equal', Emacs uses OFF-STATE to specify
28629 how to blink it off. ON-STATE and OFF-STATE are values for
28630 the `cursor-type' frame parameter.
28631
28632 If a frame's ON-STATE has no entry in this list,
28633 the frame's other specifications determine how to blink the cursor off. */);
28634 Vblink_cursor_alist = Qnil;
28635
28636 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28637 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28638 If non-nil, windows are automatically scrolled horizontally to make
28639 point visible. */);
28640 automatic_hscrolling_p = 1;
28641 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28642
28643 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28644 doc: /* How many columns away from the window edge point is allowed to get
28645 before automatic hscrolling will horizontally scroll the window. */);
28646 hscroll_margin = 5;
28647
28648 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28649 doc: /* How many columns to scroll the window when point gets too close to the edge.
28650 When point is less than `hscroll-margin' columns from the window
28651 edge, automatic hscrolling will scroll the window by the amount of columns
28652 determined by this variable. If its value is a positive integer, scroll that
28653 many columns. If it's a positive floating-point number, it specifies the
28654 fraction of the window's width to scroll. If it's nil or zero, point will be
28655 centered horizontally after the scroll. Any other value, including negative
28656 numbers, are treated as if the value were zero.
28657
28658 Automatic hscrolling always moves point outside the scroll margin, so if
28659 point was more than scroll step columns inside the margin, the window will
28660 scroll more than the value given by the scroll step.
28661
28662 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28663 and `scroll-right' overrides this variable's effect. */);
28664 Vhscroll_step = make_number (0);
28665
28666 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28667 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28668 Bind this around calls to `message' to let it take effect. */);
28669 message_truncate_lines = 0;
28670
28671 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28672 doc: /* Normal hook run to update the menu bar definitions.
28673 Redisplay runs this hook before it redisplays the menu bar.
28674 This is used to update submenus such as Buffers,
28675 whose contents depend on various data. */);
28676 Vmenu_bar_update_hook = Qnil;
28677
28678 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28679 doc: /* Frame for which we are updating a menu.
28680 The enable predicate for a menu binding should check this variable. */);
28681 Vmenu_updating_frame = Qnil;
28682
28683 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28684 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28685 inhibit_menubar_update = 0;
28686
28687 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28688 doc: /* Prefix prepended to all continuation lines at display time.
28689 The value may be a string, an image, or a stretch-glyph; it is
28690 interpreted in the same way as the value of a `display' text property.
28691
28692 This variable is overridden by any `wrap-prefix' text or overlay
28693 property.
28694
28695 To add a prefix to non-continuation lines, use `line-prefix'. */);
28696 Vwrap_prefix = Qnil;
28697 DEFSYM (Qwrap_prefix, "wrap-prefix");
28698 Fmake_variable_buffer_local (Qwrap_prefix);
28699
28700 DEFVAR_LISP ("line-prefix", Vline_prefix,
28701 doc: /* Prefix prepended to all non-continuation lines at display time.
28702 The value may be a string, an image, or a stretch-glyph; it is
28703 interpreted in the same way as the value of a `display' text property.
28704
28705 This variable is overridden by any `line-prefix' text or overlay
28706 property.
28707
28708 To add a prefix to continuation lines, use `wrap-prefix'. */);
28709 Vline_prefix = Qnil;
28710 DEFSYM (Qline_prefix, "line-prefix");
28711 Fmake_variable_buffer_local (Qline_prefix);
28712
28713 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28714 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28715 inhibit_eval_during_redisplay = 0;
28716
28717 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28718 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28719 inhibit_free_realized_faces = 0;
28720
28721 #if GLYPH_DEBUG
28722 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28723 doc: /* Inhibit try_window_id display optimization. */);
28724 inhibit_try_window_id = 0;
28725
28726 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28727 doc: /* Inhibit try_window_reusing display optimization. */);
28728 inhibit_try_window_reusing = 0;
28729
28730 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28731 doc: /* Inhibit try_cursor_movement display optimization. */);
28732 inhibit_try_cursor_movement = 0;
28733 #endif /* GLYPH_DEBUG */
28734
28735 DEFVAR_INT ("overline-margin", overline_margin,
28736 doc: /* Space between overline and text, in pixels.
28737 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28738 margin to the character height. */);
28739 overline_margin = 2;
28740
28741 DEFVAR_INT ("underline-minimum-offset",
28742 underline_minimum_offset,
28743 doc: /* Minimum distance between baseline and underline.
28744 This can improve legibility of underlined text at small font sizes,
28745 particularly when using variable `x-use-underline-position-properties'
28746 with fonts that specify an UNDERLINE_POSITION relatively close to the
28747 baseline. The default value is 1. */);
28748 underline_minimum_offset = 1;
28749
28750 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28751 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28752 This feature only works when on a window system that can change
28753 cursor shapes. */);
28754 display_hourglass_p = 1;
28755
28756 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28757 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28758 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28759
28760 hourglass_atimer = NULL;
28761 hourglass_shown_p = 0;
28762
28763 DEFSYM (Qglyphless_char, "glyphless-char");
28764 DEFSYM (Qhex_code, "hex-code");
28765 DEFSYM (Qempty_box, "empty-box");
28766 DEFSYM (Qthin_space, "thin-space");
28767 DEFSYM (Qzero_width, "zero-width");
28768
28769 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28770 /* Intern this now in case it isn't already done.
28771 Setting this variable twice is harmless.
28772 But don't staticpro it here--that is done in alloc.c. */
28773 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28774 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28775
28776 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28777 doc: /* Char-table defining glyphless characters.
28778 Each element, if non-nil, should be one of the following:
28779 an ASCII acronym string: display this string in a box
28780 `hex-code': display the hexadecimal code of a character in a box
28781 `empty-box': display as an empty box
28782 `thin-space': display as 1-pixel width space
28783 `zero-width': don't display
28784 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28785 display method for graphical terminals and text terminals respectively.
28786 GRAPHICAL and TEXT should each have one of the values listed above.
28787
28788 The char-table has one extra slot to control the display of a character for
28789 which no font is found. This slot only takes effect on graphical terminals.
28790 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28791 `thin-space'. The default is `empty-box'. */);
28792 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28793 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28794 Qempty_box);
28795 }
28796
28797
28798 /* Initialize this module when Emacs starts. */
28799
28800 void
28801 init_xdisp (void)
28802 {
28803 current_header_line_height = current_mode_line_height = -1;
28804
28805 CHARPOS (this_line_start_pos) = 0;
28806
28807 if (!noninteractive)
28808 {
28809 struct window *m = XWINDOW (minibuf_window);
28810 Lisp_Object frame = m->frame;
28811 struct frame *f = XFRAME (frame);
28812 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28813 struct window *r = XWINDOW (root);
28814 int i;
28815
28816 echo_area_window = minibuf_window;
28817
28818 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28819 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28820 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28821 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28822 XSETFASTINT (m->total_lines, 1);
28823 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28824
28825 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28826 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28827 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28828
28829 /* The default ellipsis glyphs `...'. */
28830 for (i = 0; i < 3; ++i)
28831 default_invis_vector[i] = make_number ('.');
28832 }
28833
28834 {
28835 /* Allocate the buffer for frame titles.
28836 Also used for `format-mode-line'. */
28837 int size = 100;
28838 mode_line_noprop_buf = (char *) xmalloc (size);
28839 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28840 mode_line_noprop_ptr = mode_line_noprop_buf;
28841 mode_line_target = MODE_LINE_DISPLAY;
28842 }
28843
28844 help_echo_showing_p = 0;
28845 }
28846
28847 /* Since w32 does not support atimers, it defines its own implementation of
28848 the following three functions in w32fns.c. */
28849 #ifndef WINDOWSNT
28850
28851 /* Platform-independent portion of hourglass implementation. */
28852
28853 /* Return non-zero if hourglass timer has been started or hourglass is
28854 shown. */
28855 int
28856 hourglass_started (void)
28857 {
28858 return hourglass_shown_p || hourglass_atimer != NULL;
28859 }
28860
28861 /* Cancel a currently active hourglass timer, and start a new one. */
28862 void
28863 start_hourglass (void)
28864 {
28865 #if defined (HAVE_WINDOW_SYSTEM)
28866 EMACS_TIME delay;
28867 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
28868
28869 cancel_hourglass ();
28870
28871 if (NUMBERP (Vhourglass_delay))
28872 {
28873 double duration = extract_float (Vhourglass_delay);
28874 if (0 < duration)
28875 duration_to_sec_usec (duration, &secs, &usecs);
28876 }
28877
28878 EMACS_SET_SECS_USECS (delay, secs, usecs);
28879 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28880 show_hourglass, NULL);
28881 #endif
28882 }
28883
28884
28885 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28886 shown. */
28887 void
28888 cancel_hourglass (void)
28889 {
28890 #if defined (HAVE_WINDOW_SYSTEM)
28891 if (hourglass_atimer)
28892 {
28893 cancel_atimer (hourglass_atimer);
28894 hourglass_atimer = NULL;
28895 }
28896
28897 if (hourglass_shown_p)
28898 hide_hourglass ();
28899 #endif
28900 }
28901 #endif /* ! WINDOWSNT */