Fix bug #11417 with infloop when left-fringe/right-fringe spec is used on TTY.
[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 EMACS_INT 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, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
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 EMACS_INT, EMACS_INT);
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 *, EMACS_INT);
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 (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
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, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void iterate_out_of_display_property (struct it *);
843 static void pop_it (struct it *);
844 static void sync_frame_with_window_matrix_rows (struct window *);
845 static void select_frame_for_redisplay (Lisp_Object);
846 static void redisplay_internal (void);
847 static int echo_area_display (int);
848 static void redisplay_windows (Lisp_Object);
849 static void redisplay_window (Lisp_Object, int);
850 static Lisp_Object redisplay_window_error (Lisp_Object);
851 static Lisp_Object redisplay_window_0 (Lisp_Object);
852 static Lisp_Object redisplay_window_1 (Lisp_Object);
853 static int set_cursor_from_row (struct window *, struct glyph_row *,
854 struct glyph_matrix *, EMACS_INT, EMACS_INT,
855 int, int);
856 static int update_menu_bar (struct frame *, int, int);
857 static int try_window_reusing_current_matrix (struct window *);
858 static int try_window_id (struct window *);
859 static int display_line (struct it *);
860 static int display_mode_lines (struct window *);
861 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
862 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
863 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
864 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
865 static void display_menu_bar (struct window *);
866 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
867 EMACS_INT *);
868 static int display_string (const char *, Lisp_Object, Lisp_Object,
869 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
870 static void compute_line_metrics (struct it *);
871 static void run_redisplay_end_trigger_hook (struct it *);
872 static int get_overlay_strings (struct it *, EMACS_INT);
873 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
874 static void next_overlay_string (struct it *);
875 static void reseat (struct it *, struct text_pos, int);
876 static void reseat_1 (struct it *, struct text_pos, int);
877 static void back_to_previous_visible_line_start (struct it *);
878 void reseat_at_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, EMACS_INT);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, EMACS_INT, int,
896 enum move_operation_enum);
897 void move_it_vertically_backward (struct it *, int);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, EMACS_INT);
906 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
907 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
908 static EMACS_INT number_of_chars (const char *, int);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static EMACS_INT next_overlay_change (EMACS_INT);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, EMACS_INT, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, EMACS_INT, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
922
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925
926 #ifdef HAVE_WINDOW_SYSTEM
927
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
939
940
941 #endif /* HAVE_WINDOW_SYSTEM */
942
943 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
944 static int coords_in_mouse_face_p (struct window *, int, int);
945
946
947 \f
948 /***********************************************************************
949 Window display dimensions
950 ***********************************************************************/
951
952 /* Return the bottom boundary y-position for text lines in window W.
953 This is the first y position at which a line cannot start.
954 It is relative to the top of the window.
955
956 This is the height of W minus the height of a mode line, if any. */
957
958 int
959 window_text_bottom_y (struct window *w)
960 {
961 int height = WINDOW_TOTAL_HEIGHT (w);
962
963 if (WINDOW_WANTS_MODELINE_P (w))
964 height -= CURRENT_MODE_LINE_HEIGHT (w);
965 return height;
966 }
967
968 /* Return the pixel width of display area AREA of window W. AREA < 0
969 means return the total width of W, not including fringes to
970 the left and right of the window. */
971
972 int
973 window_box_width (struct window *w, int area)
974 {
975 int cols = XFASTINT (w->total_cols);
976 int pixels = 0;
977
978 if (!w->pseudo_window_p)
979 {
980 cols -= WINDOW_SCROLL_BAR_COLS (w);
981
982 if (area == TEXT_AREA)
983 {
984 if (INTEGERP (w->left_margin_cols))
985 cols -= XFASTINT (w->left_margin_cols);
986 if (INTEGERP (w->right_margin_cols))
987 cols -= XFASTINT (w->right_margin_cols);
988 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
989 }
990 else if (area == LEFT_MARGIN_AREA)
991 {
992 cols = (INTEGERP (w->left_margin_cols)
993 ? XFASTINT (w->left_margin_cols) : 0);
994 pixels = 0;
995 }
996 else if (area == RIGHT_MARGIN_AREA)
997 {
998 cols = (INTEGERP (w->right_margin_cols)
999 ? XFASTINT (w->right_margin_cols) : 0);
1000 pixels = 0;
1001 }
1002 }
1003
1004 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1005 }
1006
1007
1008 /* Return the pixel height of the display area of window W, not
1009 including mode lines of W, if any. */
1010
1011 int
1012 window_box_height (struct window *w)
1013 {
1014 struct frame *f = XFRAME (w->frame);
1015 int height = WINDOW_TOTAL_HEIGHT (w);
1016
1017 xassert (height >= 0);
1018
1019 /* Note: the code below that determines the mode-line/header-line
1020 height is essentially the same as that contained in the macro
1021 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1022 the appropriate glyph row has its `mode_line_p' flag set,
1023 and if it doesn't, uses estimate_mode_line_height instead. */
1024
1025 if (WINDOW_WANTS_MODELINE_P (w))
1026 {
1027 struct glyph_row *ml_row
1028 = (w->current_matrix && w->current_matrix->rows
1029 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1030 : 0);
1031 if (ml_row && ml_row->mode_line_p)
1032 height -= ml_row->height;
1033 else
1034 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1035 }
1036
1037 if (WINDOW_WANTS_HEADER_LINE_P (w))
1038 {
1039 struct glyph_row *hl_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (hl_row && hl_row->mode_line_p)
1044 height -= hl_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1047 }
1048
1049 /* With a very small font and a mode-line that's taller than
1050 default, we might end up with a negative height. */
1051 return max (0, height);
1052 }
1053
1054 /* Return the window-relative coordinate of the left edge of display
1055 area AREA of window W. AREA < 0 means return the left edge of the
1056 whole window, to the right of the left fringe of W. */
1057
1058 int
1059 window_box_left_offset (struct window *w, int area)
1060 {
1061 int x;
1062
1063 if (w->pseudo_window_p)
1064 return 0;
1065
1066 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1067
1068 if (area == TEXT_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA));
1071 else if (area == RIGHT_MARGIN_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA)
1074 + window_box_width (w, TEXT_AREA)
1075 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1076 ? 0
1077 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1078 else if (area == LEFT_MARGIN_AREA
1079 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1081
1082 return x;
1083 }
1084
1085
1086 /* Return the window-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the right edge of the
1088 whole window, to the left of the right fringe of W. */
1089
1090 int
1091 window_box_right_offset (struct window *w, int area)
1092 {
1093 return window_box_left_offset (w, area) + window_box_width (w, area);
1094 }
1095
1096 /* Return the frame-relative coordinate of the left edge of display
1097 area AREA of window W. AREA < 0 means return the left edge of the
1098 whole window, to the right of the left fringe of W. */
1099
1100 int
1101 window_box_left (struct window *w, int area)
1102 {
1103 struct frame *f = XFRAME (w->frame);
1104 int x;
1105
1106 if (w->pseudo_window_p)
1107 return FRAME_INTERNAL_BORDER_WIDTH (f);
1108
1109 x = (WINDOW_LEFT_EDGE_X (w)
1110 + window_box_left_offset (w, area));
1111
1112 return x;
1113 }
1114
1115
1116 /* Return the frame-relative coordinate of the right edge of display
1117 area AREA of window W. AREA < 0 means return the right edge of the
1118 whole window, to the left of the right fringe of W. */
1119
1120 int
1121 window_box_right (struct window *w, int area)
1122 {
1123 return window_box_left (w, area) + window_box_width (w, area);
1124 }
1125
1126 /* Get the bounding box of the display area AREA of window W, without
1127 mode lines, in frame-relative coordinates. AREA < 0 means the
1128 whole window, not including the left and right fringes of
1129 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1130 coordinates of the upper-left corner of the box. Return in
1131 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1132
1133 void
1134 window_box (struct window *w, int area, int *box_x, int *box_y,
1135 int *box_width, int *box_height)
1136 {
1137 if (box_width)
1138 *box_width = window_box_width (w, area);
1139 if (box_height)
1140 *box_height = window_box_height (w);
1141 if (box_x)
1142 *box_x = window_box_left (w, area);
1143 if (box_y)
1144 {
1145 *box_y = WINDOW_TOP_EDGE_Y (w);
1146 if (WINDOW_WANTS_HEADER_LINE_P (w))
1147 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1148 }
1149 }
1150
1151
1152 /* Get the bounding box of the display area AREA of window W, without
1153 mode lines. AREA < 0 means the whole window, not including the
1154 left and right fringe of the window. Return in *TOP_LEFT_X
1155 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1156 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1157 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1158 box. */
1159
1160 static inline void
1161 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1162 int *bottom_right_x, int *bottom_right_y)
1163 {
1164 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1165 bottom_right_y);
1166 *bottom_right_x += *top_left_x;
1167 *bottom_right_y += *top_left_y;
1168 }
1169
1170
1171 \f
1172 /***********************************************************************
1173 Utilities
1174 ***********************************************************************/
1175
1176 /* Return the bottom y-position of the line the iterator IT is in.
1177 This can modify IT's settings. */
1178
1179 int
1180 line_bottom_y (struct it *it)
1181 {
1182 int line_height = it->max_ascent + it->max_descent;
1183 int line_top_y = it->current_y;
1184
1185 if (line_height == 0)
1186 {
1187 if (last_height)
1188 line_height = last_height;
1189 else if (IT_CHARPOS (*it) < ZV)
1190 {
1191 move_it_by_lines (it, 1);
1192 line_height = (it->max_ascent || it->max_descent
1193 ? it->max_ascent + it->max_descent
1194 : last_height);
1195 }
1196 else
1197 {
1198 struct glyph_row *row = it->glyph_row;
1199
1200 /* Use the default character height. */
1201 it->glyph_row = NULL;
1202 it->what = IT_CHARACTER;
1203 it->c = ' ';
1204 it->len = 1;
1205 PRODUCE_GLYPHS (it);
1206 line_height = it->ascent + it->descent;
1207 it->glyph_row = row;
1208 }
1209 }
1210
1211 return line_top_y + line_height;
1212 }
1213
1214 /* Subroutine of pos_visible_p below. Extracts a display string, if
1215 any, from the display spec given as its argument. */
1216 static Lisp_Object
1217 string_from_display_spec (Lisp_Object spec)
1218 {
1219 if (CONSP (spec))
1220 {
1221 while (CONSP (spec))
1222 {
1223 if (STRINGP (XCAR (spec)))
1224 return XCAR (spec);
1225 spec = XCDR (spec);
1226 }
1227 }
1228 else if (VECTORP (spec))
1229 {
1230 ptrdiff_t i;
1231
1232 for (i = 0; i < ASIZE (spec); i++)
1233 {
1234 if (STRINGP (AREF (spec, i)))
1235 return AREF (spec, i);
1236 }
1237 return Qnil;
1238 }
1239
1240 return spec;
1241 }
1242
1243 /* Return 1 if position CHARPOS is visible in window W.
1244 CHARPOS < 0 means return info about WINDOW_END position.
1245 If visible, set *X and *Y to pixel coordinates of top left corner.
1246 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1247 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1248
1249 int
1250 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1251 int *rtop, int *rbot, int *rowh, int *vpos)
1252 {
1253 struct it it;
1254 void *itdata = bidi_shelve_cache ();
1255 struct text_pos top;
1256 int visible_p = 0;
1257 struct buffer *old_buffer = NULL;
1258
1259 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1260 return visible_p;
1261
1262 if (XBUFFER (w->buffer) != current_buffer)
1263 {
1264 old_buffer = current_buffer;
1265 set_buffer_internal_1 (XBUFFER (w->buffer));
1266 }
1267
1268 SET_TEXT_POS_FROM_MARKER (top, w->start);
1269 /* Scrolling a minibuffer window via scroll bar when the echo area
1270 shows long text sometimes resets the minibuffer contents behind
1271 our backs. */
1272 if (CHARPOS (top) > ZV)
1273 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1274
1275 /* Compute exact mode line heights. */
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 BVAR (current_buffer, mode_line_format));
1280
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 BVAR (current_buffer, header_line_format));
1285
1286 start_display (&it, w, top);
1287 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1288 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1289
1290 if (charpos >= 0
1291 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1292 && IT_CHARPOS (it) >= charpos)
1293 /* When scanning backwards under bidi iteration, move_it_to
1294 stops at or _before_ CHARPOS, because it stops at or to
1295 the _right_ of the character at CHARPOS. */
1296 || (it.bidi_p && it.bidi_it.scan_dir == -1
1297 && IT_CHARPOS (it) <= charpos)))
1298 {
1299 /* We have reached CHARPOS, or passed it. How the call to
1300 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1301 or covered by a display property, move_it_to stops at the end
1302 of the invisible text, to the right of CHARPOS. (ii) If
1303 CHARPOS is in a display vector, move_it_to stops on its last
1304 glyph. */
1305 int top_x = it.current_x;
1306 int top_y = it.current_y;
1307 enum it_method it_method = it.method;
1308 /* Calling line_bottom_y may change it.method, it.position, etc. */
1309 int bottom_y = (last_height = 0, line_bottom_y (&it));
1310 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1311
1312 if (top_y < window_top_y)
1313 visible_p = bottom_y > window_top_y;
1314 else if (top_y < it.last_visible_y)
1315 visible_p = 1;
1316 if (visible_p)
1317 {
1318 if (it_method == GET_FROM_DISPLAY_VECTOR)
1319 {
1320 /* We stopped on the last glyph of a display vector.
1321 Try and recompute. Hack alert! */
1322 if (charpos < 2 || top.charpos >= charpos)
1323 top_x = it.glyph_row->x;
1324 else
1325 {
1326 struct it it2;
1327 start_display (&it2, w, top);
1328 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1329 get_next_display_element (&it2);
1330 PRODUCE_GLYPHS (&it2);
1331 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1332 || it2.current_x > it2.last_visible_x)
1333 top_x = it.glyph_row->x;
1334 else
1335 {
1336 top_x = it2.current_x;
1337 top_y = it2.current_y;
1338 }
1339 }
1340 }
1341 else if (IT_CHARPOS (it) != charpos)
1342 {
1343 Lisp_Object cpos = make_number (charpos);
1344 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1345 Lisp_Object string = string_from_display_spec (spec);
1346 int newline_in_string = 0;
1347
1348 if (STRINGP (string))
1349 {
1350 const char *s = SSDATA (string);
1351 const char *e = s + SBYTES (string);
1352 while (s < e)
1353 {
1354 if (*s++ == '\n')
1355 {
1356 newline_in_string = 1;
1357 break;
1358 }
1359 }
1360 }
1361 /* The tricky code below is needed because there's a
1362 discrepancy between move_it_to and how we set cursor
1363 when the display line ends in a newline from a
1364 display string. move_it_to will stop _after_ such
1365 display strings, whereas set_cursor_from_row
1366 conspires with cursor_row_p to place the cursor on
1367 the first glyph produced from the display string. */
1368
1369 /* We have overshoot PT because it is covered by a
1370 display property whose value is a string. If the
1371 string includes embedded newlines, we are also in the
1372 wrong display line. Backtrack to the correct line,
1373 where the display string begins. */
1374 if (newline_in_string)
1375 {
1376 Lisp_Object startpos, endpos;
1377 EMACS_INT start, end;
1378 struct it it3;
1379 int it3_moved;
1380
1381 /* Find the first and the last buffer positions
1382 covered by the display string. */
1383 endpos =
1384 Fnext_single_char_property_change (cpos, Qdisplay,
1385 Qnil, Qnil);
1386 startpos =
1387 Fprevious_single_char_property_change (endpos, Qdisplay,
1388 Qnil, Qnil);
1389 start = XFASTINT (startpos);
1390 end = XFASTINT (endpos);
1391 /* Move to the last buffer position before the
1392 display property. */
1393 start_display (&it3, w, top);
1394 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1395 /* Move forward one more line if the position before
1396 the display string is a newline or if it is the
1397 rightmost character on a line that is
1398 continued or word-wrapped. */
1399 if (it3.method == GET_FROM_BUFFER
1400 && it3.c == '\n')
1401 move_it_by_lines (&it3, 1);
1402 else if (move_it_in_display_line_to (&it3, -1,
1403 it3.current_x
1404 + it3.pixel_width,
1405 MOVE_TO_X)
1406 == MOVE_LINE_CONTINUED)
1407 {
1408 move_it_by_lines (&it3, 1);
1409 /* When we are under word-wrap, the #$@%!
1410 move_it_by_lines moves 2 lines, so we need to
1411 fix that up. */
1412 if (it3.line_wrap == WORD_WRAP)
1413 move_it_by_lines (&it3, -1);
1414 }
1415
1416 /* Record the vertical coordinate of the display
1417 line where we wound up. */
1418 top_y = it3.current_y;
1419 if (it3.bidi_p)
1420 {
1421 /* When characters are reordered for display,
1422 the character displayed to the left of the
1423 display string could be _after_ the display
1424 property in the logical order. Use the
1425 smallest vertical position of these two. */
1426 start_display (&it3, w, top);
1427 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1428 if (it3.current_y < top_y)
1429 top_y = it3.current_y;
1430 }
1431 /* Move from the top of the window to the beginning
1432 of the display line where the display string
1433 begins. */
1434 start_display (&it3, w, top);
1435 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1436 /* If it3_moved stays zero after the 'while' loop
1437 below, that means we already were at a newline
1438 before the loop (e.g., the display string begins
1439 with a newline), so we don't need to (and cannot)
1440 inspect the glyphs of it3.glyph_row, because
1441 PRODUCE_GLYPHS will not produce anything for a
1442 newline, and thus it3.glyph_row stays at its
1443 stale content it got at top of the window. */
1444 it3_moved = 0;
1445 /* Finally, advance the iterator until we hit the
1446 first display element whose character position is
1447 CHARPOS, or until the first newline from the
1448 display string, which signals the end of the
1449 display line. */
1450 while (get_next_display_element (&it3))
1451 {
1452 PRODUCE_GLYPHS (&it3);
1453 if (IT_CHARPOS (it3) == charpos
1454 || ITERATOR_AT_END_OF_LINE_P (&it3))
1455 break;
1456 it3_moved = 1;
1457 set_iterator_to_next (&it3, 0);
1458 }
1459 top_x = it3.current_x - it3.pixel_width;
1460 /* Normally, we would exit the above loop because we
1461 found the display element whose character
1462 position is CHARPOS. For the contingency that we
1463 didn't, and stopped at the first newline from the
1464 display string, move back over the glyphs
1465 produced from the string, until we find the
1466 rightmost glyph not from the string. */
1467 if (it3_moved
1468 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1469 {
1470 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1471 + it3.glyph_row->used[TEXT_AREA];
1472
1473 while (EQ ((g - 1)->object, string))
1474 {
1475 --g;
1476 top_x -= g->pixel_width;
1477 }
1478 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1479 + it3.glyph_row->used[TEXT_AREA]);
1480 }
1481 }
1482 }
1483
1484 *x = top_x;
1485 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1486 *rtop = max (0, window_top_y - top_y);
1487 *rbot = max (0, bottom_y - it.last_visible_y);
1488 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1489 - max (top_y, window_top_y)));
1490 *vpos = it.vpos;
1491 }
1492 }
1493 else
1494 {
1495 /* We were asked to provide info about WINDOW_END. */
1496 struct it it2;
1497 void *it2data = NULL;
1498
1499 SAVE_IT (it2, it, it2data);
1500 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1501 move_it_by_lines (&it, 1);
1502 if (charpos < IT_CHARPOS (it)
1503 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1504 {
1505 visible_p = 1;
1506 RESTORE_IT (&it2, &it2, it2data);
1507 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1508 *x = it2.current_x;
1509 *y = it2.current_y + it2.max_ascent - it2.ascent;
1510 *rtop = max (0, -it2.current_y);
1511 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1512 - it.last_visible_y));
1513 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1514 it.last_visible_y)
1515 - max (it2.current_y,
1516 WINDOW_HEADER_LINE_HEIGHT (w))));
1517 *vpos = it2.vpos;
1518 }
1519 else
1520 bidi_unshelve_cache (it2data, 1);
1521 }
1522 bidi_unshelve_cache (itdata, 0);
1523
1524 if (old_buffer)
1525 set_buffer_internal_1 (old_buffer);
1526
1527 current_header_line_height = current_mode_line_height = -1;
1528
1529 if (visible_p && XFASTINT (w->hscroll) > 0)
1530 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1531
1532 #if 0
1533 /* Debugging code. */
1534 if (visible_p)
1535 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1536 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1537 else
1538 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1539 #endif
1540
1541 return visible_p;
1542 }
1543
1544
1545 /* Return the next character from STR. Return in *LEN the length of
1546 the character. This is like STRING_CHAR_AND_LENGTH but never
1547 returns an invalid character. If we find one, we return a `?', but
1548 with the length of the invalid character. */
1549
1550 static inline int
1551 string_char_and_length (const unsigned char *str, int *len)
1552 {
1553 int c;
1554
1555 c = STRING_CHAR_AND_LENGTH (str, *len);
1556 if (!CHAR_VALID_P (c))
1557 /* We may not change the length here because other places in Emacs
1558 don't use this function, i.e. they silently accept invalid
1559 characters. */
1560 c = '?';
1561
1562 return c;
1563 }
1564
1565
1566
1567 /* Given a position POS containing a valid character and byte position
1568 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1569
1570 static struct text_pos
1571 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1572 {
1573 xassert (STRINGP (string) && nchars >= 0);
1574
1575 if (STRING_MULTIBYTE (string))
1576 {
1577 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1578 int len;
1579
1580 while (nchars--)
1581 {
1582 string_char_and_length (p, &len);
1583 p += len;
1584 CHARPOS (pos) += 1;
1585 BYTEPOS (pos) += len;
1586 }
1587 }
1588 else
1589 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1590
1591 return pos;
1592 }
1593
1594
1595 /* Value is the text position, i.e. character and byte position,
1596 for character position CHARPOS in STRING. */
1597
1598 static inline struct text_pos
1599 string_pos (EMACS_INT charpos, Lisp_Object string)
1600 {
1601 struct text_pos pos;
1602 xassert (STRINGP (string));
1603 xassert (charpos >= 0);
1604 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1605 return pos;
1606 }
1607
1608
1609 /* Value is a text position, i.e. character and byte position, for
1610 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1611 means recognize multibyte characters. */
1612
1613 static struct text_pos
1614 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1615 {
1616 struct text_pos pos;
1617
1618 xassert (s != NULL);
1619 xassert (charpos >= 0);
1620
1621 if (multibyte_p)
1622 {
1623 int len;
1624
1625 SET_TEXT_POS (pos, 0, 0);
1626 while (charpos--)
1627 {
1628 string_char_and_length ((const unsigned char *) s, &len);
1629 s += len;
1630 CHARPOS (pos) += 1;
1631 BYTEPOS (pos) += len;
1632 }
1633 }
1634 else
1635 SET_TEXT_POS (pos, charpos, charpos);
1636
1637 return pos;
1638 }
1639
1640
1641 /* Value is the number of characters in C string S. MULTIBYTE_P
1642 non-zero means recognize multibyte characters. */
1643
1644 static EMACS_INT
1645 number_of_chars (const char *s, int multibyte_p)
1646 {
1647 EMACS_INT nchars;
1648
1649 if (multibyte_p)
1650 {
1651 EMACS_INT rest = strlen (s);
1652 int len;
1653 const unsigned char *p = (const unsigned char *) s;
1654
1655 for (nchars = 0; rest > 0; ++nchars)
1656 {
1657 string_char_and_length (p, &len);
1658 rest -= len, p += len;
1659 }
1660 }
1661 else
1662 nchars = strlen (s);
1663
1664 return nchars;
1665 }
1666
1667
1668 /* Compute byte position NEWPOS->bytepos corresponding to
1669 NEWPOS->charpos. POS is a known position in string STRING.
1670 NEWPOS->charpos must be >= POS.charpos. */
1671
1672 static void
1673 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1674 {
1675 xassert (STRINGP (string));
1676 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1677
1678 if (STRING_MULTIBYTE (string))
1679 *newpos = string_pos_nchars_ahead (pos, string,
1680 CHARPOS (*newpos) - CHARPOS (pos));
1681 else
1682 BYTEPOS (*newpos) = CHARPOS (*newpos);
1683 }
1684
1685 /* EXPORT:
1686 Return an estimation of the pixel height of mode or header lines on
1687 frame F. FACE_ID specifies what line's height to estimate. */
1688
1689 int
1690 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1691 {
1692 #ifdef HAVE_WINDOW_SYSTEM
1693 if (FRAME_WINDOW_P (f))
1694 {
1695 int height = FONT_HEIGHT (FRAME_FONT (f));
1696
1697 /* This function is called so early when Emacs starts that the face
1698 cache and mode line face are not yet initialized. */
1699 if (FRAME_FACE_CACHE (f))
1700 {
1701 struct face *face = FACE_FROM_ID (f, face_id);
1702 if (face)
1703 {
1704 if (face->font)
1705 height = FONT_HEIGHT (face->font);
1706 if (face->box_line_width > 0)
1707 height += 2 * face->box_line_width;
1708 }
1709 }
1710
1711 return height;
1712 }
1713 #endif
1714
1715 return 1;
1716 }
1717
1718 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1719 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1720 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1721 not force the value into range. */
1722
1723 void
1724 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1725 int *x, int *y, NativeRectangle *bounds, int noclip)
1726 {
1727
1728 #ifdef HAVE_WINDOW_SYSTEM
1729 if (FRAME_WINDOW_P (f))
1730 {
1731 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1732 even for negative values. */
1733 if (pix_x < 0)
1734 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1735 if (pix_y < 0)
1736 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1737
1738 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1739 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1740
1741 if (bounds)
1742 STORE_NATIVE_RECT (*bounds,
1743 FRAME_COL_TO_PIXEL_X (f, pix_x),
1744 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1745 FRAME_COLUMN_WIDTH (f) - 1,
1746 FRAME_LINE_HEIGHT (f) - 1);
1747
1748 if (!noclip)
1749 {
1750 if (pix_x < 0)
1751 pix_x = 0;
1752 else if (pix_x > FRAME_TOTAL_COLS (f))
1753 pix_x = FRAME_TOTAL_COLS (f);
1754
1755 if (pix_y < 0)
1756 pix_y = 0;
1757 else if (pix_y > FRAME_LINES (f))
1758 pix_y = FRAME_LINES (f);
1759 }
1760 }
1761 #endif
1762
1763 *x = pix_x;
1764 *y = pix_y;
1765 }
1766
1767
1768 /* Find the glyph under window-relative coordinates X/Y in window W.
1769 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1770 strings. Return in *HPOS and *VPOS the row and column number of
1771 the glyph found. Return in *AREA the glyph area containing X.
1772 Value is a pointer to the glyph found or null if X/Y is not on
1773 text, or we can't tell because W's current matrix is not up to
1774 date. */
1775
1776 static
1777 struct glyph *
1778 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1779 int *dx, int *dy, int *area)
1780 {
1781 struct glyph *glyph, *end;
1782 struct glyph_row *row = NULL;
1783 int x0, i;
1784
1785 /* Find row containing Y. Give up if some row is not enabled. */
1786 for (i = 0; i < w->current_matrix->nrows; ++i)
1787 {
1788 row = MATRIX_ROW (w->current_matrix, i);
1789 if (!row->enabled_p)
1790 return NULL;
1791 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1792 break;
1793 }
1794
1795 *vpos = i;
1796 *hpos = 0;
1797
1798 /* Give up if Y is not in the window. */
1799 if (i == w->current_matrix->nrows)
1800 return NULL;
1801
1802 /* Get the glyph area containing X. */
1803 if (w->pseudo_window_p)
1804 {
1805 *area = TEXT_AREA;
1806 x0 = 0;
1807 }
1808 else
1809 {
1810 if (x < window_box_left_offset (w, TEXT_AREA))
1811 {
1812 *area = LEFT_MARGIN_AREA;
1813 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1814 }
1815 else if (x < window_box_right_offset (w, TEXT_AREA))
1816 {
1817 *area = TEXT_AREA;
1818 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1819 }
1820 else
1821 {
1822 *area = RIGHT_MARGIN_AREA;
1823 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1824 }
1825 }
1826
1827 /* Find glyph containing X. */
1828 glyph = row->glyphs[*area];
1829 end = glyph + row->used[*area];
1830 x -= x0;
1831 while (glyph < end && x >= glyph->pixel_width)
1832 {
1833 x -= glyph->pixel_width;
1834 ++glyph;
1835 }
1836
1837 if (glyph == end)
1838 return NULL;
1839
1840 if (dx)
1841 {
1842 *dx = x;
1843 *dy = y - (row->y + row->ascent - glyph->ascent);
1844 }
1845
1846 *hpos = glyph - row->glyphs[*area];
1847 return glyph;
1848 }
1849
1850 /* Convert frame-relative x/y to coordinates relative to window W.
1851 Takes pseudo-windows into account. */
1852
1853 static void
1854 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1855 {
1856 if (w->pseudo_window_p)
1857 {
1858 /* A pseudo-window is always full-width, and starts at the
1859 left edge of the frame, plus a frame border. */
1860 struct frame *f = XFRAME (w->frame);
1861 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1862 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1863 }
1864 else
1865 {
1866 *x -= WINDOW_LEFT_EDGE_X (w);
1867 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1868 }
1869 }
1870
1871 #ifdef HAVE_WINDOW_SYSTEM
1872
1873 /* EXPORT:
1874 Return in RECTS[] at most N clipping rectangles for glyph string S.
1875 Return the number of stored rectangles. */
1876
1877 int
1878 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1879 {
1880 XRectangle r;
1881
1882 if (n <= 0)
1883 return 0;
1884
1885 if (s->row->full_width_p)
1886 {
1887 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1888 r.x = WINDOW_LEFT_EDGE_X (s->w);
1889 r.width = WINDOW_TOTAL_WIDTH (s->w);
1890
1891 /* Unless displaying a mode or menu bar line, which are always
1892 fully visible, clip to the visible part of the row. */
1893 if (s->w->pseudo_window_p)
1894 r.height = s->row->visible_height;
1895 else
1896 r.height = s->height;
1897 }
1898 else
1899 {
1900 /* This is a text line that may be partially visible. */
1901 r.x = window_box_left (s->w, s->area);
1902 r.width = window_box_width (s->w, s->area);
1903 r.height = s->row->visible_height;
1904 }
1905
1906 if (s->clip_head)
1907 if (r.x < s->clip_head->x)
1908 {
1909 if (r.width >= s->clip_head->x - r.x)
1910 r.width -= s->clip_head->x - r.x;
1911 else
1912 r.width = 0;
1913 r.x = s->clip_head->x;
1914 }
1915 if (s->clip_tail)
1916 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1917 {
1918 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1919 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1920 else
1921 r.width = 0;
1922 }
1923
1924 /* If S draws overlapping rows, it's sufficient to use the top and
1925 bottom of the window for clipping because this glyph string
1926 intentionally draws over other lines. */
1927 if (s->for_overlaps)
1928 {
1929 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1930 r.height = window_text_bottom_y (s->w) - r.y;
1931
1932 /* Alas, the above simple strategy does not work for the
1933 environments with anti-aliased text: if the same text is
1934 drawn onto the same place multiple times, it gets thicker.
1935 If the overlap we are processing is for the erased cursor, we
1936 take the intersection with the rectangle of the cursor. */
1937 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1938 {
1939 XRectangle rc, r_save = r;
1940
1941 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1942 rc.y = s->w->phys_cursor.y;
1943 rc.width = s->w->phys_cursor_width;
1944 rc.height = s->w->phys_cursor_height;
1945
1946 x_intersect_rectangles (&r_save, &rc, &r);
1947 }
1948 }
1949 else
1950 {
1951 /* Don't use S->y for clipping because it doesn't take partially
1952 visible lines into account. For example, it can be negative for
1953 partially visible lines at the top of a window. */
1954 if (!s->row->full_width_p
1955 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1956 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1957 else
1958 r.y = max (0, s->row->y);
1959 }
1960
1961 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1962
1963 /* If drawing the cursor, don't let glyph draw outside its
1964 advertised boundaries. Cleartype does this under some circumstances. */
1965 if (s->hl == DRAW_CURSOR)
1966 {
1967 struct glyph *glyph = s->first_glyph;
1968 int height, max_y;
1969
1970 if (s->x > r.x)
1971 {
1972 r.width -= s->x - r.x;
1973 r.x = s->x;
1974 }
1975 r.width = min (r.width, glyph->pixel_width);
1976
1977 /* If r.y is below window bottom, ensure that we still see a cursor. */
1978 height = min (glyph->ascent + glyph->descent,
1979 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1980 max_y = window_text_bottom_y (s->w) - height;
1981 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1982 if (s->ybase - glyph->ascent > max_y)
1983 {
1984 r.y = max_y;
1985 r.height = height;
1986 }
1987 else
1988 {
1989 /* Don't draw cursor glyph taller than our actual glyph. */
1990 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1991 if (height < r.height)
1992 {
1993 max_y = r.y + r.height;
1994 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1995 r.height = min (max_y - r.y, height);
1996 }
1997 }
1998 }
1999
2000 if (s->row->clip)
2001 {
2002 XRectangle r_save = r;
2003
2004 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2005 r.width = 0;
2006 }
2007
2008 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2009 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2010 {
2011 #ifdef CONVERT_FROM_XRECT
2012 CONVERT_FROM_XRECT (r, *rects);
2013 #else
2014 *rects = r;
2015 #endif
2016 return 1;
2017 }
2018 else
2019 {
2020 /* If we are processing overlapping and allowed to return
2021 multiple clipping rectangles, we exclude the row of the glyph
2022 string from the clipping rectangle. This is to avoid drawing
2023 the same text on the environment with anti-aliasing. */
2024 #ifdef CONVERT_FROM_XRECT
2025 XRectangle rs[2];
2026 #else
2027 XRectangle *rs = rects;
2028 #endif
2029 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2030
2031 if (s->for_overlaps & OVERLAPS_PRED)
2032 {
2033 rs[i] = r;
2034 if (r.y + r.height > row_y)
2035 {
2036 if (r.y < row_y)
2037 rs[i].height = row_y - r.y;
2038 else
2039 rs[i].height = 0;
2040 }
2041 i++;
2042 }
2043 if (s->for_overlaps & OVERLAPS_SUCC)
2044 {
2045 rs[i] = r;
2046 if (r.y < row_y + s->row->visible_height)
2047 {
2048 if (r.y + r.height > row_y + s->row->visible_height)
2049 {
2050 rs[i].y = row_y + s->row->visible_height;
2051 rs[i].height = r.y + r.height - rs[i].y;
2052 }
2053 else
2054 rs[i].height = 0;
2055 }
2056 i++;
2057 }
2058
2059 n = i;
2060 #ifdef CONVERT_FROM_XRECT
2061 for (i = 0; i < n; i++)
2062 CONVERT_FROM_XRECT (rs[i], rects[i]);
2063 #endif
2064 return n;
2065 }
2066 }
2067
2068 /* EXPORT:
2069 Return in *NR the clipping rectangle for glyph string S. */
2070
2071 void
2072 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2073 {
2074 get_glyph_string_clip_rects (s, nr, 1);
2075 }
2076
2077
2078 /* EXPORT:
2079 Return the position and height of the phys cursor in window W.
2080 Set w->phys_cursor_width to width of phys cursor.
2081 */
2082
2083 void
2084 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2085 struct glyph *glyph, int *xp, int *yp, int *heightp)
2086 {
2087 struct frame *f = XFRAME (WINDOW_FRAME (w));
2088 int x, y, wd, h, h0, y0;
2089
2090 /* Compute the width of the rectangle to draw. If on a stretch
2091 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2092 rectangle as wide as the glyph, but use a canonical character
2093 width instead. */
2094 wd = glyph->pixel_width - 1;
2095 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2096 wd++; /* Why? */
2097 #endif
2098
2099 x = w->phys_cursor.x;
2100 if (x < 0)
2101 {
2102 wd += x;
2103 x = 0;
2104 }
2105
2106 if (glyph->type == STRETCH_GLYPH
2107 && !x_stretch_cursor_p)
2108 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2109 w->phys_cursor_width = wd;
2110
2111 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2112
2113 /* If y is below window bottom, ensure that we still see a cursor. */
2114 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2115
2116 h = max (h0, glyph->ascent + glyph->descent);
2117 h0 = min (h0, glyph->ascent + glyph->descent);
2118
2119 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2120 if (y < y0)
2121 {
2122 h = max (h - (y0 - y) + 1, h0);
2123 y = y0 - 1;
2124 }
2125 else
2126 {
2127 y0 = window_text_bottom_y (w) - h0;
2128 if (y > y0)
2129 {
2130 h += y - y0;
2131 y = y0;
2132 }
2133 }
2134
2135 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2136 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2137 *heightp = h;
2138 }
2139
2140 /*
2141 * Remember which glyph the mouse is over.
2142 */
2143
2144 void
2145 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2146 {
2147 Lisp_Object window;
2148 struct window *w;
2149 struct glyph_row *r, *gr, *end_row;
2150 enum window_part part;
2151 enum glyph_row_area area;
2152 int x, y, width, height;
2153
2154 /* Try to determine frame pixel position and size of the glyph under
2155 frame pixel coordinates X/Y on frame F. */
2156
2157 if (!f->glyphs_initialized_p
2158 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2159 NILP (window)))
2160 {
2161 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2162 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2163 goto virtual_glyph;
2164 }
2165
2166 w = XWINDOW (window);
2167 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2168 height = WINDOW_FRAME_LINE_HEIGHT (w);
2169
2170 x = window_relative_x_coord (w, part, gx);
2171 y = gy - WINDOW_TOP_EDGE_Y (w);
2172
2173 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2174 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2175
2176 if (w->pseudo_window_p)
2177 {
2178 area = TEXT_AREA;
2179 part = ON_MODE_LINE; /* Don't adjust margin. */
2180 goto text_glyph;
2181 }
2182
2183 switch (part)
2184 {
2185 case ON_LEFT_MARGIN:
2186 area = LEFT_MARGIN_AREA;
2187 goto text_glyph;
2188
2189 case ON_RIGHT_MARGIN:
2190 area = RIGHT_MARGIN_AREA;
2191 goto text_glyph;
2192
2193 case ON_HEADER_LINE:
2194 case ON_MODE_LINE:
2195 gr = (part == ON_HEADER_LINE
2196 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2197 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2198 gy = gr->y;
2199 area = TEXT_AREA;
2200 goto text_glyph_row_found;
2201
2202 case ON_TEXT:
2203 area = TEXT_AREA;
2204
2205 text_glyph:
2206 gr = 0; gy = 0;
2207 for (; r <= end_row && r->enabled_p; ++r)
2208 if (r->y + r->height > y)
2209 {
2210 gr = r; gy = r->y;
2211 break;
2212 }
2213
2214 text_glyph_row_found:
2215 if (gr && gy <= y)
2216 {
2217 struct glyph *g = gr->glyphs[area];
2218 struct glyph *end = g + gr->used[area];
2219
2220 height = gr->height;
2221 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2222 if (gx + g->pixel_width > x)
2223 break;
2224
2225 if (g < end)
2226 {
2227 if (g->type == IMAGE_GLYPH)
2228 {
2229 /* Don't remember when mouse is over image, as
2230 image may have hot-spots. */
2231 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2232 return;
2233 }
2234 width = g->pixel_width;
2235 }
2236 else
2237 {
2238 /* Use nominal char spacing at end of line. */
2239 x -= gx;
2240 gx += (x / width) * width;
2241 }
2242
2243 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2244 gx += window_box_left_offset (w, area);
2245 }
2246 else
2247 {
2248 /* Use nominal line height at end of window. */
2249 gx = (x / width) * width;
2250 y -= gy;
2251 gy += (y / height) * height;
2252 }
2253 break;
2254
2255 case ON_LEFT_FRINGE:
2256 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2257 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2258 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2259 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2260 goto row_glyph;
2261
2262 case ON_RIGHT_FRINGE:
2263 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2264 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2265 : window_box_right_offset (w, TEXT_AREA));
2266 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2267 goto row_glyph;
2268
2269 case ON_SCROLL_BAR:
2270 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2271 ? 0
2272 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2273 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2274 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2275 : 0)));
2276 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2277
2278 row_glyph:
2279 gr = 0, gy = 0;
2280 for (; r <= end_row && r->enabled_p; ++r)
2281 if (r->y + r->height > y)
2282 {
2283 gr = r; gy = r->y;
2284 break;
2285 }
2286
2287 if (gr && gy <= y)
2288 height = gr->height;
2289 else
2290 {
2291 /* Use nominal line height at end of window. */
2292 y -= gy;
2293 gy += (y / height) * height;
2294 }
2295 break;
2296
2297 default:
2298 ;
2299 virtual_glyph:
2300 /* If there is no glyph under the mouse, then we divide the screen
2301 into a grid of the smallest glyph in the frame, and use that
2302 as our "glyph". */
2303
2304 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2305 round down even for negative values. */
2306 if (gx < 0)
2307 gx -= width - 1;
2308 if (gy < 0)
2309 gy -= height - 1;
2310
2311 gx = (gx / width) * width;
2312 gy = (gy / height) * height;
2313
2314 goto store_rect;
2315 }
2316
2317 gx += WINDOW_LEFT_EDGE_X (w);
2318 gy += WINDOW_TOP_EDGE_Y (w);
2319
2320 store_rect:
2321 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2322
2323 /* Visible feedback for debugging. */
2324 #if 0
2325 #if HAVE_X_WINDOWS
2326 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2327 f->output_data.x->normal_gc,
2328 gx, gy, width, height);
2329 #endif
2330 #endif
2331 }
2332
2333
2334 #endif /* HAVE_WINDOW_SYSTEM */
2335
2336 \f
2337 /***********************************************************************
2338 Lisp form evaluation
2339 ***********************************************************************/
2340
2341 /* Error handler for safe_eval and safe_call. */
2342
2343 static Lisp_Object
2344 safe_eval_handler (Lisp_Object arg)
2345 {
2346 add_to_log ("Error during redisplay: %S", arg, Qnil);
2347 return Qnil;
2348 }
2349
2350
2351 /* Evaluate SEXPR and return the result, or nil if something went
2352 wrong. Prevent redisplay during the evaluation. */
2353
2354 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2355 Return the result, or nil if something went wrong. Prevent
2356 redisplay during the evaluation. */
2357
2358 Lisp_Object
2359 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2360 {
2361 Lisp_Object val;
2362
2363 if (inhibit_eval_during_redisplay)
2364 val = Qnil;
2365 else
2366 {
2367 int count = SPECPDL_INDEX ();
2368 struct gcpro gcpro1;
2369
2370 GCPRO1 (args[0]);
2371 gcpro1.nvars = nargs;
2372 specbind (Qinhibit_redisplay, Qt);
2373 /* Use Qt to ensure debugger does not run,
2374 so there is no possibility of wanting to redisplay. */
2375 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2376 safe_eval_handler);
2377 UNGCPRO;
2378 val = unbind_to (count, val);
2379 }
2380
2381 return val;
2382 }
2383
2384
2385 /* Call function FN with one argument ARG.
2386 Return the result, or nil if something went wrong. */
2387
2388 Lisp_Object
2389 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2390 {
2391 Lisp_Object args[2];
2392 args[0] = fn;
2393 args[1] = arg;
2394 return safe_call (2, args);
2395 }
2396
2397 static Lisp_Object Qeval;
2398
2399 Lisp_Object
2400 safe_eval (Lisp_Object sexpr)
2401 {
2402 return safe_call1 (Qeval, sexpr);
2403 }
2404
2405 /* Call function FN with one argument ARG.
2406 Return the result, or nil if something went wrong. */
2407
2408 Lisp_Object
2409 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2410 {
2411 Lisp_Object args[3];
2412 args[0] = fn;
2413 args[1] = arg1;
2414 args[2] = arg2;
2415 return safe_call (3, args);
2416 }
2417
2418
2419 \f
2420 /***********************************************************************
2421 Debugging
2422 ***********************************************************************/
2423
2424 #if 0
2425
2426 /* Define CHECK_IT to perform sanity checks on iterators.
2427 This is for debugging. It is too slow to do unconditionally. */
2428
2429 static void
2430 check_it (struct it *it)
2431 {
2432 if (it->method == GET_FROM_STRING)
2433 {
2434 xassert (STRINGP (it->string));
2435 xassert (IT_STRING_CHARPOS (*it) >= 0);
2436 }
2437 else
2438 {
2439 xassert (IT_STRING_CHARPOS (*it) < 0);
2440 if (it->method == GET_FROM_BUFFER)
2441 {
2442 /* Check that character and byte positions agree. */
2443 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2444 }
2445 }
2446
2447 if (it->dpvec)
2448 xassert (it->current.dpvec_index >= 0);
2449 else
2450 xassert (it->current.dpvec_index < 0);
2451 }
2452
2453 #define CHECK_IT(IT) check_it ((IT))
2454
2455 #else /* not 0 */
2456
2457 #define CHECK_IT(IT) (void) 0
2458
2459 #endif /* not 0 */
2460
2461
2462 #if GLYPH_DEBUG && XASSERTS
2463
2464 /* Check that the window end of window W is what we expect it
2465 to be---the last row in the current matrix displaying text. */
2466
2467 static void
2468 check_window_end (struct window *w)
2469 {
2470 if (!MINI_WINDOW_P (w)
2471 && !NILP (w->window_end_valid))
2472 {
2473 struct glyph_row *row;
2474 xassert ((row = MATRIX_ROW (w->current_matrix,
2475 XFASTINT (w->window_end_vpos)),
2476 !row->enabled_p
2477 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2478 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2479 }
2480 }
2481
2482 #define CHECK_WINDOW_END(W) check_window_end ((W))
2483
2484 #else
2485
2486 #define CHECK_WINDOW_END(W) (void) 0
2487
2488 #endif
2489
2490
2491 \f
2492 /***********************************************************************
2493 Iterator initialization
2494 ***********************************************************************/
2495
2496 /* Initialize IT for displaying current_buffer in window W, starting
2497 at character position CHARPOS. CHARPOS < 0 means that no buffer
2498 position is specified which is useful when the iterator is assigned
2499 a position later. BYTEPOS is the byte position corresponding to
2500 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2501
2502 If ROW is not null, calls to produce_glyphs with IT as parameter
2503 will produce glyphs in that row.
2504
2505 BASE_FACE_ID is the id of a base face to use. It must be one of
2506 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2507 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2508 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2509
2510 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2511 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2512 will be initialized to use the corresponding mode line glyph row of
2513 the desired matrix of W. */
2514
2515 void
2516 init_iterator (struct it *it, struct window *w,
2517 EMACS_INT charpos, EMACS_INT bytepos,
2518 struct glyph_row *row, enum face_id base_face_id)
2519 {
2520 int highlight_region_p;
2521 enum face_id remapped_base_face_id = base_face_id;
2522
2523 /* Some precondition checks. */
2524 xassert (w != NULL && it != NULL);
2525 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2526 && charpos <= ZV));
2527
2528 /* If face attributes have been changed since the last redisplay,
2529 free realized faces now because they depend on face definitions
2530 that might have changed. Don't free faces while there might be
2531 desired matrices pending which reference these faces. */
2532 if (face_change_count && !inhibit_free_realized_faces)
2533 {
2534 face_change_count = 0;
2535 free_all_realized_faces (Qnil);
2536 }
2537
2538 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2539 if (! NILP (Vface_remapping_alist))
2540 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2541
2542 /* Use one of the mode line rows of W's desired matrix if
2543 appropriate. */
2544 if (row == NULL)
2545 {
2546 if (base_face_id == MODE_LINE_FACE_ID
2547 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2548 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2549 else if (base_face_id == HEADER_LINE_FACE_ID)
2550 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2551 }
2552
2553 /* Clear IT. */
2554 memset (it, 0, sizeof *it);
2555 it->current.overlay_string_index = -1;
2556 it->current.dpvec_index = -1;
2557 it->base_face_id = remapped_base_face_id;
2558 it->string = Qnil;
2559 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2560 it->paragraph_embedding = L2R;
2561 it->bidi_it.string.lstring = Qnil;
2562 it->bidi_it.string.s = NULL;
2563 it->bidi_it.string.bufpos = 0;
2564
2565 /* The window in which we iterate over current_buffer: */
2566 XSETWINDOW (it->window, w);
2567 it->w = w;
2568 it->f = XFRAME (w->frame);
2569
2570 it->cmp_it.id = -1;
2571
2572 /* Extra space between lines (on window systems only). */
2573 if (base_face_id == DEFAULT_FACE_ID
2574 && FRAME_WINDOW_P (it->f))
2575 {
2576 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2577 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2578 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2579 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2580 * FRAME_LINE_HEIGHT (it->f));
2581 else if (it->f->extra_line_spacing > 0)
2582 it->extra_line_spacing = it->f->extra_line_spacing;
2583 it->max_extra_line_spacing = 0;
2584 }
2585
2586 /* If realized faces have been removed, e.g. because of face
2587 attribute changes of named faces, recompute them. When running
2588 in batch mode, the face cache of the initial frame is null. If
2589 we happen to get called, make a dummy face cache. */
2590 if (FRAME_FACE_CACHE (it->f) == NULL)
2591 init_frame_faces (it->f);
2592 if (FRAME_FACE_CACHE (it->f)->used == 0)
2593 recompute_basic_faces (it->f);
2594
2595 /* Current value of the `slice', `space-width', and 'height' properties. */
2596 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2597 it->space_width = Qnil;
2598 it->font_height = Qnil;
2599 it->override_ascent = -1;
2600
2601 /* Are control characters displayed as `^C'? */
2602 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2603
2604 /* -1 means everything between a CR and the following line end
2605 is invisible. >0 means lines indented more than this value are
2606 invisible. */
2607 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2608 ? XINT (BVAR (current_buffer, selective_display))
2609 : (!NILP (BVAR (current_buffer, selective_display))
2610 ? -1 : 0));
2611 it->selective_display_ellipsis_p
2612 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2613
2614 /* Display table to use. */
2615 it->dp = window_display_table (w);
2616
2617 /* Are multibyte characters enabled in current_buffer? */
2618 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2619
2620 /* Non-zero if we should highlight the region. */
2621 highlight_region_p
2622 = (!NILP (Vtransient_mark_mode)
2623 && !NILP (BVAR (current_buffer, mark_active))
2624 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2625
2626 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2627 start and end of a visible region in window IT->w. Set both to
2628 -1 to indicate no region. */
2629 if (highlight_region_p
2630 /* Maybe highlight only in selected window. */
2631 && (/* Either show region everywhere. */
2632 highlight_nonselected_windows
2633 /* Or show region in the selected window. */
2634 || w == XWINDOW (selected_window)
2635 /* Or show the region if we are in the mini-buffer and W is
2636 the window the mini-buffer refers to. */
2637 || (MINI_WINDOW_P (XWINDOW (selected_window))
2638 && WINDOWP (minibuf_selected_window)
2639 && w == XWINDOW (minibuf_selected_window))))
2640 {
2641 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2642 it->region_beg_charpos = min (PT, markpos);
2643 it->region_end_charpos = max (PT, markpos);
2644 }
2645 else
2646 it->region_beg_charpos = it->region_end_charpos = -1;
2647
2648 /* Get the position at which the redisplay_end_trigger hook should
2649 be run, if it is to be run at all. */
2650 if (MARKERP (w->redisplay_end_trigger)
2651 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2652 it->redisplay_end_trigger_charpos
2653 = marker_position (w->redisplay_end_trigger);
2654 else if (INTEGERP (w->redisplay_end_trigger))
2655 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2656
2657 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2658
2659 /* Are lines in the display truncated? */
2660 if (base_face_id != DEFAULT_FACE_ID
2661 || XINT (it->w->hscroll)
2662 || (! WINDOW_FULL_WIDTH_P (it->w)
2663 && ((!NILP (Vtruncate_partial_width_windows)
2664 && !INTEGERP (Vtruncate_partial_width_windows))
2665 || (INTEGERP (Vtruncate_partial_width_windows)
2666 && (WINDOW_TOTAL_COLS (it->w)
2667 < XINT (Vtruncate_partial_width_windows))))))
2668 it->line_wrap = TRUNCATE;
2669 else if (NILP (BVAR (current_buffer, truncate_lines)))
2670 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2671 ? WINDOW_WRAP : WORD_WRAP;
2672 else
2673 it->line_wrap = TRUNCATE;
2674
2675 /* Get dimensions of truncation and continuation glyphs. These are
2676 displayed as fringe bitmaps under X, so we don't need them for such
2677 frames. */
2678 if (!FRAME_WINDOW_P (it->f))
2679 {
2680 if (it->line_wrap == TRUNCATE)
2681 {
2682 /* We will need the truncation glyph. */
2683 xassert (it->glyph_row == NULL);
2684 produce_special_glyphs (it, IT_TRUNCATION);
2685 it->truncation_pixel_width = it->pixel_width;
2686 }
2687 else
2688 {
2689 /* We will need the continuation glyph. */
2690 xassert (it->glyph_row == NULL);
2691 produce_special_glyphs (it, IT_CONTINUATION);
2692 it->continuation_pixel_width = it->pixel_width;
2693 }
2694
2695 /* Reset these values to zero because the produce_special_glyphs
2696 above has changed them. */
2697 it->pixel_width = it->ascent = it->descent = 0;
2698 it->phys_ascent = it->phys_descent = 0;
2699 }
2700
2701 /* Set this after getting the dimensions of truncation and
2702 continuation glyphs, so that we don't produce glyphs when calling
2703 produce_special_glyphs, above. */
2704 it->glyph_row = row;
2705 it->area = TEXT_AREA;
2706
2707 /* Forget any previous info about this row being reversed. */
2708 if (it->glyph_row)
2709 it->glyph_row->reversed_p = 0;
2710
2711 /* Get the dimensions of the display area. The display area
2712 consists of the visible window area plus a horizontally scrolled
2713 part to the left of the window. All x-values are relative to the
2714 start of this total display area. */
2715 if (base_face_id != DEFAULT_FACE_ID)
2716 {
2717 /* Mode lines, menu bar in terminal frames. */
2718 it->first_visible_x = 0;
2719 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2720 }
2721 else
2722 {
2723 it->first_visible_x
2724 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2725 it->last_visible_x = (it->first_visible_x
2726 + window_box_width (w, TEXT_AREA));
2727
2728 /* If we truncate lines, leave room for the truncator glyph(s) at
2729 the right margin. Otherwise, leave room for the continuation
2730 glyph(s). Truncation and continuation glyphs are not inserted
2731 for window-based redisplay. */
2732 if (!FRAME_WINDOW_P (it->f))
2733 {
2734 if (it->line_wrap == TRUNCATE)
2735 it->last_visible_x -= it->truncation_pixel_width;
2736 else
2737 it->last_visible_x -= it->continuation_pixel_width;
2738 }
2739
2740 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2741 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2742 }
2743
2744 /* Leave room for a border glyph. */
2745 if (!FRAME_WINDOW_P (it->f)
2746 && !WINDOW_RIGHTMOST_P (it->w))
2747 it->last_visible_x -= 1;
2748
2749 it->last_visible_y = window_text_bottom_y (w);
2750
2751 /* For mode lines and alike, arrange for the first glyph having a
2752 left box line if the face specifies a box. */
2753 if (base_face_id != DEFAULT_FACE_ID)
2754 {
2755 struct face *face;
2756
2757 it->face_id = remapped_base_face_id;
2758
2759 /* If we have a boxed mode line, make the first character appear
2760 with a left box line. */
2761 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2762 if (face->box != FACE_NO_BOX)
2763 it->start_of_box_run_p = 1;
2764 }
2765
2766 /* If a buffer position was specified, set the iterator there,
2767 getting overlays and face properties from that position. */
2768 if (charpos >= BUF_BEG (current_buffer))
2769 {
2770 it->end_charpos = ZV;
2771 IT_CHARPOS (*it) = charpos;
2772
2773 /* We will rely on `reseat' to set this up properly, via
2774 handle_face_prop. */
2775 it->face_id = it->base_face_id;
2776
2777 /* Compute byte position if not specified. */
2778 if (bytepos < charpos)
2779 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2780 else
2781 IT_BYTEPOS (*it) = bytepos;
2782
2783 it->start = it->current;
2784 /* Do we need to reorder bidirectional text? Not if this is a
2785 unibyte buffer: by definition, none of the single-byte
2786 characters are strong R2L, so no reordering is needed. And
2787 bidi.c doesn't support unibyte buffers anyway. Also, don't
2788 reorder while we are loading loadup.el, since the tables of
2789 character properties needed for reordering are not yet
2790 available. */
2791 it->bidi_p =
2792 NILP (Vpurify_flag)
2793 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2794 && it->multibyte_p;
2795
2796 /* If we are to reorder bidirectional text, init the bidi
2797 iterator. */
2798 if (it->bidi_p)
2799 {
2800 /* Note the paragraph direction that this buffer wants to
2801 use. */
2802 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2803 Qleft_to_right))
2804 it->paragraph_embedding = L2R;
2805 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2806 Qright_to_left))
2807 it->paragraph_embedding = R2L;
2808 else
2809 it->paragraph_embedding = NEUTRAL_DIR;
2810 bidi_unshelve_cache (NULL, 0);
2811 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2812 &it->bidi_it);
2813 }
2814
2815 /* Compute faces etc. */
2816 reseat (it, it->current.pos, 1);
2817 }
2818
2819 CHECK_IT (it);
2820 }
2821
2822
2823 /* Initialize IT for the display of window W with window start POS. */
2824
2825 void
2826 start_display (struct it *it, struct window *w, struct text_pos pos)
2827 {
2828 struct glyph_row *row;
2829 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2830
2831 row = w->desired_matrix->rows + first_vpos;
2832 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2833 it->first_vpos = first_vpos;
2834
2835 /* Don't reseat to previous visible line start if current start
2836 position is in a string or image. */
2837 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2838 {
2839 int start_at_line_beg_p;
2840 int first_y = it->current_y;
2841
2842 /* If window start is not at a line start, skip forward to POS to
2843 get the correct continuation lines width. */
2844 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2845 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2846 if (!start_at_line_beg_p)
2847 {
2848 int new_x;
2849
2850 reseat_at_previous_visible_line_start (it);
2851 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2852
2853 new_x = it->current_x + it->pixel_width;
2854
2855 /* If lines are continued, this line may end in the middle
2856 of a multi-glyph character (e.g. a control character
2857 displayed as \003, or in the middle of an overlay
2858 string). In this case move_it_to above will not have
2859 taken us to the start of the continuation line but to the
2860 end of the continued line. */
2861 if (it->current_x > 0
2862 && it->line_wrap != TRUNCATE /* Lines are continued. */
2863 && (/* And glyph doesn't fit on the line. */
2864 new_x > it->last_visible_x
2865 /* Or it fits exactly and we're on a window
2866 system frame. */
2867 || (new_x == it->last_visible_x
2868 && FRAME_WINDOW_P (it->f))))
2869 {
2870 if ((it->current.dpvec_index >= 0
2871 || it->current.overlay_string_index >= 0)
2872 /* If we are on a newline from a display vector or
2873 overlay string, then we are already at the end of
2874 a screen line; no need to go to the next line in
2875 that case, as this line is not really continued.
2876 (If we do go to the next line, C-e will not DTRT.) */
2877 && it->c != '\n')
2878 {
2879 set_iterator_to_next (it, 1);
2880 move_it_in_display_line_to (it, -1, -1, 0);
2881 }
2882
2883 it->continuation_lines_width += it->current_x;
2884 }
2885 /* If the character at POS is displayed via a display
2886 vector, move_it_to above stops at the final glyph of
2887 IT->dpvec. To make the caller redisplay that character
2888 again (a.k.a. start at POS), we need to reset the
2889 dpvec_index to the beginning of IT->dpvec. */
2890 else if (it->current.dpvec_index >= 0)
2891 it->current.dpvec_index = 0;
2892
2893 /* We're starting a new display line, not affected by the
2894 height of the continued line, so clear the appropriate
2895 fields in the iterator structure. */
2896 it->max_ascent = it->max_descent = 0;
2897 it->max_phys_ascent = it->max_phys_descent = 0;
2898
2899 it->current_y = first_y;
2900 it->vpos = 0;
2901 it->current_x = it->hpos = 0;
2902 }
2903 }
2904 }
2905
2906
2907 /* Return 1 if POS is a position in ellipses displayed for invisible
2908 text. W is the window we display, for text property lookup. */
2909
2910 static int
2911 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2912 {
2913 Lisp_Object prop, window;
2914 int ellipses_p = 0;
2915 EMACS_INT charpos = CHARPOS (pos->pos);
2916
2917 /* If POS specifies a position in a display vector, this might
2918 be for an ellipsis displayed for invisible text. We won't
2919 get the iterator set up for delivering that ellipsis unless
2920 we make sure that it gets aware of the invisible text. */
2921 if (pos->dpvec_index >= 0
2922 && pos->overlay_string_index < 0
2923 && CHARPOS (pos->string_pos) < 0
2924 && charpos > BEGV
2925 && (XSETWINDOW (window, w),
2926 prop = Fget_char_property (make_number (charpos),
2927 Qinvisible, window),
2928 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2929 {
2930 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2931 window);
2932 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2933 }
2934
2935 return ellipses_p;
2936 }
2937
2938
2939 /* Initialize IT for stepping through current_buffer in window W,
2940 starting at position POS that includes overlay string and display
2941 vector/ control character translation position information. Value
2942 is zero if there are overlay strings with newlines at POS. */
2943
2944 static int
2945 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2946 {
2947 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2948 int i, overlay_strings_with_newlines = 0;
2949
2950 /* If POS specifies a position in a display vector, this might
2951 be for an ellipsis displayed for invisible text. We won't
2952 get the iterator set up for delivering that ellipsis unless
2953 we make sure that it gets aware of the invisible text. */
2954 if (in_ellipses_for_invisible_text_p (pos, w))
2955 {
2956 --charpos;
2957 bytepos = 0;
2958 }
2959
2960 /* Keep in mind: the call to reseat in init_iterator skips invisible
2961 text, so we might end up at a position different from POS. This
2962 is only a problem when POS is a row start after a newline and an
2963 overlay starts there with an after-string, and the overlay has an
2964 invisible property. Since we don't skip invisible text in
2965 display_line and elsewhere immediately after consuming the
2966 newline before the row start, such a POS will not be in a string,
2967 but the call to init_iterator below will move us to the
2968 after-string. */
2969 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2970
2971 /* This only scans the current chunk -- it should scan all chunks.
2972 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2973 to 16 in 22.1 to make this a lesser problem. */
2974 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2975 {
2976 const char *s = SSDATA (it->overlay_strings[i]);
2977 const char *e = s + SBYTES (it->overlay_strings[i]);
2978
2979 while (s < e && *s != '\n')
2980 ++s;
2981
2982 if (s < e)
2983 {
2984 overlay_strings_with_newlines = 1;
2985 break;
2986 }
2987 }
2988
2989 /* If position is within an overlay string, set up IT to the right
2990 overlay string. */
2991 if (pos->overlay_string_index >= 0)
2992 {
2993 int relative_index;
2994
2995 /* If the first overlay string happens to have a `display'
2996 property for an image, the iterator will be set up for that
2997 image, and we have to undo that setup first before we can
2998 correct the overlay string index. */
2999 if (it->method == GET_FROM_IMAGE)
3000 pop_it (it);
3001
3002 /* We already have the first chunk of overlay strings in
3003 IT->overlay_strings. Load more until the one for
3004 pos->overlay_string_index is in IT->overlay_strings. */
3005 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3006 {
3007 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3008 it->current.overlay_string_index = 0;
3009 while (n--)
3010 {
3011 load_overlay_strings (it, 0);
3012 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3013 }
3014 }
3015
3016 it->current.overlay_string_index = pos->overlay_string_index;
3017 relative_index = (it->current.overlay_string_index
3018 % OVERLAY_STRING_CHUNK_SIZE);
3019 it->string = it->overlay_strings[relative_index];
3020 xassert (STRINGP (it->string));
3021 it->current.string_pos = pos->string_pos;
3022 it->method = GET_FROM_STRING;
3023 }
3024
3025 if (CHARPOS (pos->string_pos) >= 0)
3026 {
3027 /* Recorded position is not in an overlay string, but in another
3028 string. This can only be a string from a `display' property.
3029 IT should already be filled with that string. */
3030 it->current.string_pos = pos->string_pos;
3031 xassert (STRINGP (it->string));
3032 }
3033
3034 /* Restore position in display vector translations, control
3035 character translations or ellipses. */
3036 if (pos->dpvec_index >= 0)
3037 {
3038 if (it->dpvec == NULL)
3039 get_next_display_element (it);
3040 xassert (it->dpvec && it->current.dpvec_index == 0);
3041 it->current.dpvec_index = pos->dpvec_index;
3042 }
3043
3044 CHECK_IT (it);
3045 return !overlay_strings_with_newlines;
3046 }
3047
3048
3049 /* Initialize IT for stepping through current_buffer in window W
3050 starting at ROW->start. */
3051
3052 static void
3053 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3054 {
3055 init_from_display_pos (it, w, &row->start);
3056 it->start = row->start;
3057 it->continuation_lines_width = row->continuation_lines_width;
3058 CHECK_IT (it);
3059 }
3060
3061
3062 /* Initialize IT for stepping through current_buffer in window W
3063 starting in the line following ROW, i.e. starting at ROW->end.
3064 Value is zero if there are overlay strings with newlines at ROW's
3065 end position. */
3066
3067 static int
3068 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3069 {
3070 int success = 0;
3071
3072 if (init_from_display_pos (it, w, &row->end))
3073 {
3074 if (row->continued_p)
3075 it->continuation_lines_width
3076 = row->continuation_lines_width + row->pixel_width;
3077 CHECK_IT (it);
3078 success = 1;
3079 }
3080
3081 return success;
3082 }
3083
3084
3085
3086 \f
3087 /***********************************************************************
3088 Text properties
3089 ***********************************************************************/
3090
3091 /* Called when IT reaches IT->stop_charpos. Handle text property and
3092 overlay changes. Set IT->stop_charpos to the next position where
3093 to stop. */
3094
3095 static void
3096 handle_stop (struct it *it)
3097 {
3098 enum prop_handled handled;
3099 int handle_overlay_change_p;
3100 struct props *p;
3101
3102 it->dpvec = NULL;
3103 it->current.dpvec_index = -1;
3104 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3105 it->ignore_overlay_strings_at_pos_p = 0;
3106 it->ellipsis_p = 0;
3107
3108 /* Use face of preceding text for ellipsis (if invisible) */
3109 if (it->selective_display_ellipsis_p)
3110 it->saved_face_id = it->face_id;
3111
3112 do
3113 {
3114 handled = HANDLED_NORMALLY;
3115
3116 /* Call text property handlers. */
3117 for (p = it_props; p->handler; ++p)
3118 {
3119 handled = p->handler (it);
3120
3121 if (handled == HANDLED_RECOMPUTE_PROPS)
3122 break;
3123 else if (handled == HANDLED_RETURN)
3124 {
3125 /* We still want to show before and after strings from
3126 overlays even if the actual buffer text is replaced. */
3127 if (!handle_overlay_change_p
3128 || it->sp > 1
3129 /* Don't call get_overlay_strings_1 if we already
3130 have overlay strings loaded, because doing so
3131 will load them again and push the iterator state
3132 onto the stack one more time, which is not
3133 expected by the rest of the code that processes
3134 overlay strings. */
3135 || (it->n_overlay_strings <= 0
3136 ? !get_overlay_strings_1 (it, 0, 0)
3137 : 0))
3138 {
3139 if (it->ellipsis_p)
3140 setup_for_ellipsis (it, 0);
3141 /* When handling a display spec, we might load an
3142 empty string. In that case, discard it here. We
3143 used to discard it in handle_single_display_spec,
3144 but that causes get_overlay_strings_1, above, to
3145 ignore overlay strings that we must check. */
3146 if (STRINGP (it->string) && !SCHARS (it->string))
3147 pop_it (it);
3148 return;
3149 }
3150 else if (STRINGP (it->string) && !SCHARS (it->string))
3151 pop_it (it);
3152 else
3153 {
3154 it->ignore_overlay_strings_at_pos_p = 1;
3155 it->string_from_display_prop_p = 0;
3156 it->from_disp_prop_p = 0;
3157 handle_overlay_change_p = 0;
3158 }
3159 handled = HANDLED_RECOMPUTE_PROPS;
3160 break;
3161 }
3162 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3163 handle_overlay_change_p = 0;
3164 }
3165
3166 if (handled != HANDLED_RECOMPUTE_PROPS)
3167 {
3168 /* Don't check for overlay strings below when set to deliver
3169 characters from a display vector. */
3170 if (it->method == GET_FROM_DISPLAY_VECTOR)
3171 handle_overlay_change_p = 0;
3172
3173 /* Handle overlay changes.
3174 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3175 if it finds overlays. */
3176 if (handle_overlay_change_p)
3177 handled = handle_overlay_change (it);
3178 }
3179
3180 if (it->ellipsis_p)
3181 {
3182 setup_for_ellipsis (it, 0);
3183 break;
3184 }
3185 }
3186 while (handled == HANDLED_RECOMPUTE_PROPS);
3187
3188 /* Determine where to stop next. */
3189 if (handled == HANDLED_NORMALLY)
3190 compute_stop_pos (it);
3191 }
3192
3193
3194 /* Compute IT->stop_charpos from text property and overlay change
3195 information for IT's current position. */
3196
3197 static void
3198 compute_stop_pos (struct it *it)
3199 {
3200 register INTERVAL iv, next_iv;
3201 Lisp_Object object, limit, position;
3202 EMACS_INT charpos, bytepos;
3203
3204 if (STRINGP (it->string))
3205 {
3206 /* Strings are usually short, so don't limit the search for
3207 properties. */
3208 it->stop_charpos = it->end_charpos;
3209 object = it->string;
3210 limit = Qnil;
3211 charpos = IT_STRING_CHARPOS (*it);
3212 bytepos = IT_STRING_BYTEPOS (*it);
3213 }
3214 else
3215 {
3216 EMACS_INT pos;
3217
3218 /* If end_charpos is out of range for some reason, such as a
3219 misbehaving display function, rationalize it (Bug#5984). */
3220 if (it->end_charpos > ZV)
3221 it->end_charpos = ZV;
3222 it->stop_charpos = it->end_charpos;
3223
3224 /* If next overlay change is in front of the current stop pos
3225 (which is IT->end_charpos), stop there. Note: value of
3226 next_overlay_change is point-max if no overlay change
3227 follows. */
3228 charpos = IT_CHARPOS (*it);
3229 bytepos = IT_BYTEPOS (*it);
3230 pos = next_overlay_change (charpos);
3231 if (pos < it->stop_charpos)
3232 it->stop_charpos = pos;
3233
3234 /* If showing the region, we have to stop at the region
3235 start or end because the face might change there. */
3236 if (it->region_beg_charpos > 0)
3237 {
3238 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3239 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3240 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3241 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3242 }
3243
3244 /* Set up variables for computing the stop position from text
3245 property changes. */
3246 XSETBUFFER (object, current_buffer);
3247 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3248 }
3249
3250 /* Get the interval containing IT's position. Value is a null
3251 interval if there isn't such an interval. */
3252 position = make_number (charpos);
3253 iv = validate_interval_range (object, &position, &position, 0);
3254 if (!NULL_INTERVAL_P (iv))
3255 {
3256 Lisp_Object values_here[LAST_PROP_IDX];
3257 struct props *p;
3258
3259 /* Get properties here. */
3260 for (p = it_props; p->handler; ++p)
3261 values_here[p->idx] = textget (iv->plist, *p->name);
3262
3263 /* Look for an interval following iv that has different
3264 properties. */
3265 for (next_iv = next_interval (iv);
3266 (!NULL_INTERVAL_P (next_iv)
3267 && (NILP (limit)
3268 || XFASTINT (limit) > next_iv->position));
3269 next_iv = next_interval (next_iv))
3270 {
3271 for (p = it_props; p->handler; ++p)
3272 {
3273 Lisp_Object new_value;
3274
3275 new_value = textget (next_iv->plist, *p->name);
3276 if (!EQ (values_here[p->idx], new_value))
3277 break;
3278 }
3279
3280 if (p->handler)
3281 break;
3282 }
3283
3284 if (!NULL_INTERVAL_P (next_iv))
3285 {
3286 if (INTEGERP (limit)
3287 && next_iv->position >= XFASTINT (limit))
3288 /* No text property change up to limit. */
3289 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3290 else
3291 /* Text properties change in next_iv. */
3292 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3293 }
3294 }
3295
3296 if (it->cmp_it.id < 0)
3297 {
3298 EMACS_INT stoppos = it->end_charpos;
3299
3300 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3301 stoppos = -1;
3302 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3303 stoppos, it->string);
3304 }
3305
3306 xassert (STRINGP (it->string)
3307 || (it->stop_charpos >= BEGV
3308 && it->stop_charpos >= IT_CHARPOS (*it)));
3309 }
3310
3311
3312 /* Return the position of the next overlay change after POS in
3313 current_buffer. Value is point-max if no overlay change
3314 follows. This is like `next-overlay-change' but doesn't use
3315 xmalloc. */
3316
3317 static EMACS_INT
3318 next_overlay_change (EMACS_INT pos)
3319 {
3320 ptrdiff_t i, noverlays;
3321 EMACS_INT endpos;
3322 Lisp_Object *overlays;
3323
3324 /* Get all overlays at the given position. */
3325 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3326
3327 /* If any of these overlays ends before endpos,
3328 use its ending point instead. */
3329 for (i = 0; i < noverlays; ++i)
3330 {
3331 Lisp_Object oend;
3332 EMACS_INT oendpos;
3333
3334 oend = OVERLAY_END (overlays[i]);
3335 oendpos = OVERLAY_POSITION (oend);
3336 endpos = min (endpos, oendpos);
3337 }
3338
3339 return endpos;
3340 }
3341
3342 /* How many characters forward to search for a display property or
3343 display string. Searching too far forward makes the bidi display
3344 sluggish, especially in small windows. */
3345 #define MAX_DISP_SCAN 250
3346
3347 /* Return the character position of a display string at or after
3348 position specified by POSITION. If no display string exists at or
3349 after POSITION, return ZV. A display string is either an overlay
3350 with `display' property whose value is a string, or a `display'
3351 text property whose value is a string. STRING is data about the
3352 string to iterate; if STRING->lstring is nil, we are iterating a
3353 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3354 on a GUI frame. DISP_PROP is set to zero if we searched
3355 MAX_DISP_SCAN characters forward without finding any display
3356 strings, non-zero otherwise. It is set to 2 if the display string
3357 uses any kind of `(space ...)' spec that will produce a stretch of
3358 white space in the text area. */
3359 EMACS_INT
3360 compute_display_string_pos (struct text_pos *position,
3361 struct bidi_string_data *string,
3362 int frame_window_p, int *disp_prop)
3363 {
3364 /* OBJECT = nil means current buffer. */
3365 Lisp_Object object =
3366 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3367 Lisp_Object pos, spec, limpos;
3368 int string_p = (string && (STRINGP (string->lstring) || string->s));
3369 EMACS_INT eob = string_p ? string->schars : ZV;
3370 EMACS_INT begb = string_p ? 0 : BEGV;
3371 EMACS_INT bufpos, charpos = CHARPOS (*position);
3372 EMACS_INT lim =
3373 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3374 struct text_pos tpos;
3375 int rv = 0;
3376
3377 *disp_prop = 1;
3378
3379 if (charpos >= eob
3380 /* We don't support display properties whose values are strings
3381 that have display string properties. */
3382 || string->from_disp_str
3383 /* C strings cannot have display properties. */
3384 || (string->s && !STRINGP (object)))
3385 {
3386 *disp_prop = 0;
3387 return eob;
3388 }
3389
3390 /* If the character at CHARPOS is where the display string begins,
3391 return CHARPOS. */
3392 pos = make_number (charpos);
3393 if (STRINGP (object))
3394 bufpos = string->bufpos;
3395 else
3396 bufpos = charpos;
3397 tpos = *position;
3398 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3399 && (charpos <= begb
3400 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3401 object),
3402 spec))
3403 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3404 frame_window_p)))
3405 {
3406 if (rv == 2)
3407 *disp_prop = 2;
3408 return charpos;
3409 }
3410
3411 /* Look forward for the first character with a `display' property
3412 that will replace the underlying text when displayed. */
3413 limpos = make_number (lim);
3414 do {
3415 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3416 CHARPOS (tpos) = XFASTINT (pos);
3417 if (CHARPOS (tpos) >= lim)
3418 {
3419 *disp_prop = 0;
3420 break;
3421 }
3422 if (STRINGP (object))
3423 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3424 else
3425 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3426 spec = Fget_char_property (pos, Qdisplay, object);
3427 if (!STRINGP (object))
3428 bufpos = CHARPOS (tpos);
3429 } while (NILP (spec)
3430 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3431 bufpos, frame_window_p)));
3432 if (rv == 2)
3433 *disp_prop = 2;
3434
3435 return CHARPOS (tpos);
3436 }
3437
3438 /* Return the character position of the end of the display string that
3439 started at CHARPOS. If there's no display string at CHARPOS,
3440 return -1. A display string is either an overlay with `display'
3441 property whose value is a string or a `display' text property whose
3442 value is a string. */
3443 EMACS_INT
3444 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3445 {
3446 /* OBJECT = nil means current buffer. */
3447 Lisp_Object object =
3448 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3449 Lisp_Object pos = make_number (charpos);
3450 EMACS_INT eob =
3451 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3452
3453 if (charpos >= eob || (string->s && !STRINGP (object)))
3454 return eob;
3455
3456 /* It could happen that the display property or overlay was removed
3457 since we found it in compute_display_string_pos above. One way
3458 this can happen is if JIT font-lock was called (through
3459 handle_fontified_prop), and jit-lock-functions remove text
3460 properties or overlays from the portion of buffer that includes
3461 CHARPOS. Muse mode is known to do that, for example. In this
3462 case, we return -1 to the caller, to signal that no display
3463 string is actually present at CHARPOS. See bidi_fetch_char for
3464 how this is handled.
3465
3466 An alternative would be to never look for display properties past
3467 it->stop_charpos. But neither compute_display_string_pos nor
3468 bidi_fetch_char that calls it know or care where the next
3469 stop_charpos is. */
3470 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3471 return -1;
3472
3473 /* Look forward for the first character where the `display' property
3474 changes. */
3475 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3476
3477 return XFASTINT (pos);
3478 }
3479
3480
3481 \f
3482 /***********************************************************************
3483 Fontification
3484 ***********************************************************************/
3485
3486 /* Handle changes in the `fontified' property of the current buffer by
3487 calling hook functions from Qfontification_functions to fontify
3488 regions of text. */
3489
3490 static enum prop_handled
3491 handle_fontified_prop (struct it *it)
3492 {
3493 Lisp_Object prop, pos;
3494 enum prop_handled handled = HANDLED_NORMALLY;
3495
3496 if (!NILP (Vmemory_full))
3497 return handled;
3498
3499 /* Get the value of the `fontified' property at IT's current buffer
3500 position. (The `fontified' property doesn't have a special
3501 meaning in strings.) If the value is nil, call functions from
3502 Qfontification_functions. */
3503 if (!STRINGP (it->string)
3504 && it->s == NULL
3505 && !NILP (Vfontification_functions)
3506 && !NILP (Vrun_hooks)
3507 && (pos = make_number (IT_CHARPOS (*it)),
3508 prop = Fget_char_property (pos, Qfontified, Qnil),
3509 /* Ignore the special cased nil value always present at EOB since
3510 no amount of fontifying will be able to change it. */
3511 NILP (prop) && IT_CHARPOS (*it) < Z))
3512 {
3513 int count = SPECPDL_INDEX ();
3514 Lisp_Object val;
3515 struct buffer *obuf = current_buffer;
3516 int begv = BEGV, zv = ZV;
3517 int old_clip_changed = current_buffer->clip_changed;
3518
3519 val = Vfontification_functions;
3520 specbind (Qfontification_functions, Qnil);
3521
3522 xassert (it->end_charpos == ZV);
3523
3524 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3525 safe_call1 (val, pos);
3526 else
3527 {
3528 Lisp_Object fns, fn;
3529 struct gcpro gcpro1, gcpro2;
3530
3531 fns = Qnil;
3532 GCPRO2 (val, fns);
3533
3534 for (; CONSP (val); val = XCDR (val))
3535 {
3536 fn = XCAR (val);
3537
3538 if (EQ (fn, Qt))
3539 {
3540 /* A value of t indicates this hook has a local
3541 binding; it means to run the global binding too.
3542 In a global value, t should not occur. If it
3543 does, we must ignore it to avoid an endless
3544 loop. */
3545 for (fns = Fdefault_value (Qfontification_functions);
3546 CONSP (fns);
3547 fns = XCDR (fns))
3548 {
3549 fn = XCAR (fns);
3550 if (!EQ (fn, Qt))
3551 safe_call1 (fn, pos);
3552 }
3553 }
3554 else
3555 safe_call1 (fn, pos);
3556 }
3557
3558 UNGCPRO;
3559 }
3560
3561 unbind_to (count, Qnil);
3562
3563 /* Fontification functions routinely call `save-restriction'.
3564 Normally, this tags clip_changed, which can confuse redisplay
3565 (see discussion in Bug#6671). Since we don't perform any
3566 special handling of fontification changes in the case where
3567 `save-restriction' isn't called, there's no point doing so in
3568 this case either. So, if the buffer's restrictions are
3569 actually left unchanged, reset clip_changed. */
3570 if (obuf == current_buffer)
3571 {
3572 if (begv == BEGV && zv == ZV)
3573 current_buffer->clip_changed = old_clip_changed;
3574 }
3575 /* There isn't much we can reasonably do to protect against
3576 misbehaving fontification, but here's a fig leaf. */
3577 else if (!NILP (BVAR (obuf, name)))
3578 set_buffer_internal_1 (obuf);
3579
3580 /* The fontification code may have added/removed text.
3581 It could do even a lot worse, but let's at least protect against
3582 the most obvious case where only the text past `pos' gets changed',
3583 as is/was done in grep.el where some escapes sequences are turned
3584 into face properties (bug#7876). */
3585 it->end_charpos = ZV;
3586
3587 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3588 something. This avoids an endless loop if they failed to
3589 fontify the text for which reason ever. */
3590 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3591 handled = HANDLED_RECOMPUTE_PROPS;
3592 }
3593
3594 return handled;
3595 }
3596
3597
3598 \f
3599 /***********************************************************************
3600 Faces
3601 ***********************************************************************/
3602
3603 /* Set up iterator IT from face properties at its current position.
3604 Called from handle_stop. */
3605
3606 static enum prop_handled
3607 handle_face_prop (struct it *it)
3608 {
3609 int new_face_id;
3610 EMACS_INT next_stop;
3611
3612 if (!STRINGP (it->string))
3613 {
3614 new_face_id
3615 = face_at_buffer_position (it->w,
3616 IT_CHARPOS (*it),
3617 it->region_beg_charpos,
3618 it->region_end_charpos,
3619 &next_stop,
3620 (IT_CHARPOS (*it)
3621 + TEXT_PROP_DISTANCE_LIMIT),
3622 0, it->base_face_id);
3623
3624 /* Is this a start of a run of characters with box face?
3625 Caveat: this can be called for a freshly initialized
3626 iterator; face_id is -1 in this case. We know that the new
3627 face will not change until limit, i.e. if the new face has a
3628 box, all characters up to limit will have one. But, as
3629 usual, we don't know whether limit is really the end. */
3630 if (new_face_id != it->face_id)
3631 {
3632 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3633
3634 /* If new face has a box but old face has not, this is
3635 the start of a run of characters with box, i.e. it has
3636 a shadow on the left side. The value of face_id of the
3637 iterator will be -1 if this is the initial call that gets
3638 the face. In this case, we have to look in front of IT's
3639 position and see whether there is a face != new_face_id. */
3640 it->start_of_box_run_p
3641 = (new_face->box != FACE_NO_BOX
3642 && (it->face_id >= 0
3643 || IT_CHARPOS (*it) == BEG
3644 || new_face_id != face_before_it_pos (it)));
3645 it->face_box_p = new_face->box != FACE_NO_BOX;
3646 }
3647 }
3648 else
3649 {
3650 int base_face_id;
3651 EMACS_INT bufpos;
3652 int i;
3653 Lisp_Object from_overlay
3654 = (it->current.overlay_string_index >= 0
3655 ? it->string_overlays[it->current.overlay_string_index]
3656 : Qnil);
3657
3658 /* See if we got to this string directly or indirectly from
3659 an overlay property. That includes the before-string or
3660 after-string of an overlay, strings in display properties
3661 provided by an overlay, their text properties, etc.
3662
3663 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3664 if (! NILP (from_overlay))
3665 for (i = it->sp - 1; i >= 0; i--)
3666 {
3667 if (it->stack[i].current.overlay_string_index >= 0)
3668 from_overlay
3669 = it->string_overlays[it->stack[i].current.overlay_string_index];
3670 else if (! NILP (it->stack[i].from_overlay))
3671 from_overlay = it->stack[i].from_overlay;
3672
3673 if (!NILP (from_overlay))
3674 break;
3675 }
3676
3677 if (! NILP (from_overlay))
3678 {
3679 bufpos = IT_CHARPOS (*it);
3680 /* For a string from an overlay, the base face depends
3681 only on text properties and ignores overlays. */
3682 base_face_id
3683 = face_for_overlay_string (it->w,
3684 IT_CHARPOS (*it),
3685 it->region_beg_charpos,
3686 it->region_end_charpos,
3687 &next_stop,
3688 (IT_CHARPOS (*it)
3689 + TEXT_PROP_DISTANCE_LIMIT),
3690 0,
3691 from_overlay);
3692 }
3693 else
3694 {
3695 bufpos = 0;
3696
3697 /* For strings from a `display' property, use the face at
3698 IT's current buffer position as the base face to merge
3699 with, so that overlay strings appear in the same face as
3700 surrounding text, unless they specify their own
3701 faces. */
3702 base_face_id = it->string_from_prefix_prop_p
3703 ? DEFAULT_FACE_ID
3704 : underlying_face_id (it);
3705 }
3706
3707 new_face_id = face_at_string_position (it->w,
3708 it->string,
3709 IT_STRING_CHARPOS (*it),
3710 bufpos,
3711 it->region_beg_charpos,
3712 it->region_end_charpos,
3713 &next_stop,
3714 base_face_id, 0);
3715
3716 /* Is this a start of a run of characters with box? Caveat:
3717 this can be called for a freshly allocated iterator; face_id
3718 is -1 is this case. We know that the new face will not
3719 change until the next check pos, i.e. if the new face has a
3720 box, all characters up to that position will have a
3721 box. But, as usual, we don't know whether that position
3722 is really the end. */
3723 if (new_face_id != it->face_id)
3724 {
3725 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3726 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3727
3728 /* If new face has a box but old face hasn't, this is the
3729 start of a run of characters with box, i.e. it has a
3730 shadow on the left side. */
3731 it->start_of_box_run_p
3732 = new_face->box && (old_face == NULL || !old_face->box);
3733 it->face_box_p = new_face->box != FACE_NO_BOX;
3734 }
3735 }
3736
3737 it->face_id = new_face_id;
3738 return HANDLED_NORMALLY;
3739 }
3740
3741
3742 /* Return the ID of the face ``underlying'' IT's current position,
3743 which is in a string. If the iterator is associated with a
3744 buffer, return the face at IT's current buffer position.
3745 Otherwise, use the iterator's base_face_id. */
3746
3747 static int
3748 underlying_face_id (struct it *it)
3749 {
3750 int face_id = it->base_face_id, i;
3751
3752 xassert (STRINGP (it->string));
3753
3754 for (i = it->sp - 1; i >= 0; --i)
3755 if (NILP (it->stack[i].string))
3756 face_id = it->stack[i].face_id;
3757
3758 return face_id;
3759 }
3760
3761
3762 /* Compute the face one character before or after the current position
3763 of IT, in the visual order. BEFORE_P non-zero means get the face
3764 in front (to the left in L2R paragraphs, to the right in R2L
3765 paragraphs) of IT's screen position. Value is the ID of the face. */
3766
3767 static int
3768 face_before_or_after_it_pos (struct it *it, int before_p)
3769 {
3770 int face_id, limit;
3771 EMACS_INT next_check_charpos;
3772 struct it it_copy;
3773 void *it_copy_data = NULL;
3774
3775 xassert (it->s == NULL);
3776
3777 if (STRINGP (it->string))
3778 {
3779 EMACS_INT bufpos, charpos;
3780 int base_face_id;
3781
3782 /* No face change past the end of the string (for the case
3783 we are padding with spaces). No face change before the
3784 string start. */
3785 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3786 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3787 return it->face_id;
3788
3789 if (!it->bidi_p)
3790 {
3791 /* Set charpos to the position before or after IT's current
3792 position, in the logical order, which in the non-bidi
3793 case is the same as the visual order. */
3794 if (before_p)
3795 charpos = IT_STRING_CHARPOS (*it) - 1;
3796 else if (it->what == IT_COMPOSITION)
3797 /* For composition, we must check the character after the
3798 composition. */
3799 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3800 else
3801 charpos = IT_STRING_CHARPOS (*it) + 1;
3802 }
3803 else
3804 {
3805 if (before_p)
3806 {
3807 /* With bidi iteration, the character before the current
3808 in the visual order cannot be found by simple
3809 iteration, because "reverse" reordering is not
3810 supported. Instead, we need to use the move_it_*
3811 family of functions. */
3812 /* Ignore face changes before the first visible
3813 character on this display line. */
3814 if (it->current_x <= it->first_visible_x)
3815 return it->face_id;
3816 SAVE_IT (it_copy, *it, it_copy_data);
3817 /* Implementation note: Since move_it_in_display_line
3818 works in the iterator geometry, and thinks the first
3819 character is always the leftmost, even in R2L lines,
3820 we don't need to distinguish between the R2L and L2R
3821 cases here. */
3822 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3823 it_copy.current_x - 1, MOVE_TO_X);
3824 charpos = IT_STRING_CHARPOS (it_copy);
3825 RESTORE_IT (it, it, it_copy_data);
3826 }
3827 else
3828 {
3829 /* Set charpos to the string position of the character
3830 that comes after IT's current position in the visual
3831 order. */
3832 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3833
3834 it_copy = *it;
3835 while (n--)
3836 bidi_move_to_visually_next (&it_copy.bidi_it);
3837
3838 charpos = it_copy.bidi_it.charpos;
3839 }
3840 }
3841 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3842
3843 if (it->current.overlay_string_index >= 0)
3844 bufpos = IT_CHARPOS (*it);
3845 else
3846 bufpos = 0;
3847
3848 base_face_id = underlying_face_id (it);
3849
3850 /* Get the face for ASCII, or unibyte. */
3851 face_id = face_at_string_position (it->w,
3852 it->string,
3853 charpos,
3854 bufpos,
3855 it->region_beg_charpos,
3856 it->region_end_charpos,
3857 &next_check_charpos,
3858 base_face_id, 0);
3859
3860 /* Correct the face for charsets different from ASCII. Do it
3861 for the multibyte case only. The face returned above is
3862 suitable for unibyte text if IT->string is unibyte. */
3863 if (STRING_MULTIBYTE (it->string))
3864 {
3865 struct text_pos pos1 = string_pos (charpos, it->string);
3866 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3867 int c, len;
3868 struct face *face = FACE_FROM_ID (it->f, face_id);
3869
3870 c = string_char_and_length (p, &len);
3871 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3872 }
3873 }
3874 else
3875 {
3876 struct text_pos pos;
3877
3878 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3879 || (IT_CHARPOS (*it) <= BEGV && before_p))
3880 return it->face_id;
3881
3882 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3883 pos = it->current.pos;
3884
3885 if (!it->bidi_p)
3886 {
3887 if (before_p)
3888 DEC_TEXT_POS (pos, it->multibyte_p);
3889 else
3890 {
3891 if (it->what == IT_COMPOSITION)
3892 {
3893 /* For composition, we must check the position after
3894 the composition. */
3895 pos.charpos += it->cmp_it.nchars;
3896 pos.bytepos += it->len;
3897 }
3898 else
3899 INC_TEXT_POS (pos, it->multibyte_p);
3900 }
3901 }
3902 else
3903 {
3904 if (before_p)
3905 {
3906 /* With bidi iteration, the character before the current
3907 in the visual order cannot be found by simple
3908 iteration, because "reverse" reordering is not
3909 supported. Instead, we need to use the move_it_*
3910 family of functions. */
3911 /* Ignore face changes before the first visible
3912 character on this display line. */
3913 if (it->current_x <= it->first_visible_x)
3914 return it->face_id;
3915 SAVE_IT (it_copy, *it, it_copy_data);
3916 /* Implementation note: Since move_it_in_display_line
3917 works in the iterator geometry, and thinks the first
3918 character is always the leftmost, even in R2L lines,
3919 we don't need to distinguish between the R2L and L2R
3920 cases here. */
3921 move_it_in_display_line (&it_copy, ZV,
3922 it_copy.current_x - 1, MOVE_TO_X);
3923 pos = it_copy.current.pos;
3924 RESTORE_IT (it, it, it_copy_data);
3925 }
3926 else
3927 {
3928 /* Set charpos to the buffer position of the character
3929 that comes after IT's current position in the visual
3930 order. */
3931 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3932
3933 it_copy = *it;
3934 while (n--)
3935 bidi_move_to_visually_next (&it_copy.bidi_it);
3936
3937 SET_TEXT_POS (pos,
3938 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3939 }
3940 }
3941 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3942
3943 /* Determine face for CHARSET_ASCII, or unibyte. */
3944 face_id = face_at_buffer_position (it->w,
3945 CHARPOS (pos),
3946 it->region_beg_charpos,
3947 it->region_end_charpos,
3948 &next_check_charpos,
3949 limit, 0, -1);
3950
3951 /* Correct the face for charsets different from ASCII. Do it
3952 for the multibyte case only. The face returned above is
3953 suitable for unibyte text if current_buffer is unibyte. */
3954 if (it->multibyte_p)
3955 {
3956 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3957 struct face *face = FACE_FROM_ID (it->f, face_id);
3958 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3959 }
3960 }
3961
3962 return face_id;
3963 }
3964
3965
3966 \f
3967 /***********************************************************************
3968 Invisible text
3969 ***********************************************************************/
3970
3971 /* Set up iterator IT from invisible properties at its current
3972 position. Called from handle_stop. */
3973
3974 static enum prop_handled
3975 handle_invisible_prop (struct it *it)
3976 {
3977 enum prop_handled handled = HANDLED_NORMALLY;
3978
3979 if (STRINGP (it->string))
3980 {
3981 Lisp_Object prop, end_charpos, limit, charpos;
3982
3983 /* Get the value of the invisible text property at the
3984 current position. Value will be nil if there is no such
3985 property. */
3986 charpos = make_number (IT_STRING_CHARPOS (*it));
3987 prop = Fget_text_property (charpos, Qinvisible, it->string);
3988
3989 if (!NILP (prop)
3990 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3991 {
3992 EMACS_INT endpos;
3993
3994 handled = HANDLED_RECOMPUTE_PROPS;
3995
3996 /* Get the position at which the next change of the
3997 invisible text property can be found in IT->string.
3998 Value will be nil if the property value is the same for
3999 all the rest of IT->string. */
4000 XSETINT (limit, SCHARS (it->string));
4001 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4002 it->string, limit);
4003
4004 /* Text at current position is invisible. The next
4005 change in the property is at position end_charpos.
4006 Move IT's current position to that position. */
4007 if (INTEGERP (end_charpos)
4008 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4009 {
4010 struct text_pos old;
4011 EMACS_INT oldpos;
4012
4013 old = it->current.string_pos;
4014 oldpos = CHARPOS (old);
4015 if (it->bidi_p)
4016 {
4017 if (it->bidi_it.first_elt
4018 && it->bidi_it.charpos < SCHARS (it->string))
4019 bidi_paragraph_init (it->paragraph_embedding,
4020 &it->bidi_it, 1);
4021 /* Bidi-iterate out of the invisible text. */
4022 do
4023 {
4024 bidi_move_to_visually_next (&it->bidi_it);
4025 }
4026 while (oldpos <= it->bidi_it.charpos
4027 && it->bidi_it.charpos < endpos);
4028
4029 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4030 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4031 if (IT_CHARPOS (*it) >= endpos)
4032 it->prev_stop = endpos;
4033 }
4034 else
4035 {
4036 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4037 compute_string_pos (&it->current.string_pos, old, it->string);
4038 }
4039 }
4040 else
4041 {
4042 /* The rest of the string is invisible. If this is an
4043 overlay string, proceed with the next overlay string
4044 or whatever comes and return a character from there. */
4045 if (it->current.overlay_string_index >= 0)
4046 {
4047 next_overlay_string (it);
4048 /* Don't check for overlay strings when we just
4049 finished processing them. */
4050 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4051 }
4052 else
4053 {
4054 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4055 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4056 }
4057 }
4058 }
4059 }
4060 else
4061 {
4062 int invis_p;
4063 EMACS_INT newpos, next_stop, start_charpos, tem;
4064 Lisp_Object pos, prop, overlay;
4065
4066 /* First of all, is there invisible text at this position? */
4067 tem = start_charpos = IT_CHARPOS (*it);
4068 pos = make_number (tem);
4069 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4070 &overlay);
4071 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4072
4073 /* If we are on invisible text, skip over it. */
4074 if (invis_p && start_charpos < it->end_charpos)
4075 {
4076 /* Record whether we have to display an ellipsis for the
4077 invisible text. */
4078 int display_ellipsis_p = invis_p == 2;
4079
4080 handled = HANDLED_RECOMPUTE_PROPS;
4081
4082 /* Loop skipping over invisible text. The loop is left at
4083 ZV or with IT on the first char being visible again. */
4084 do
4085 {
4086 /* Try to skip some invisible text. Return value is the
4087 position reached which can be equal to where we start
4088 if there is nothing invisible there. This skips both
4089 over invisible text properties and overlays with
4090 invisible property. */
4091 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4092
4093 /* If we skipped nothing at all we weren't at invisible
4094 text in the first place. If everything to the end of
4095 the buffer was skipped, end the loop. */
4096 if (newpos == tem || newpos >= ZV)
4097 invis_p = 0;
4098 else
4099 {
4100 /* We skipped some characters but not necessarily
4101 all there are. Check if we ended up on visible
4102 text. Fget_char_property returns the property of
4103 the char before the given position, i.e. if we
4104 get invis_p = 0, this means that the char at
4105 newpos is visible. */
4106 pos = make_number (newpos);
4107 prop = Fget_char_property (pos, Qinvisible, it->window);
4108 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4109 }
4110
4111 /* If we ended up on invisible text, proceed to
4112 skip starting with next_stop. */
4113 if (invis_p)
4114 tem = next_stop;
4115
4116 /* If there are adjacent invisible texts, don't lose the
4117 second one's ellipsis. */
4118 if (invis_p == 2)
4119 display_ellipsis_p = 1;
4120 }
4121 while (invis_p);
4122
4123 /* The position newpos is now either ZV or on visible text. */
4124 if (it->bidi_p)
4125 {
4126 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4127 int on_newline =
4128 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4129 int after_newline =
4130 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4131
4132 /* If the invisible text ends on a newline or on a
4133 character after a newline, we can avoid the costly,
4134 character by character, bidi iteration to NEWPOS, and
4135 instead simply reseat the iterator there. That's
4136 because all bidi reordering information is tossed at
4137 the newline. This is a big win for modes that hide
4138 complete lines, like Outline, Org, etc. */
4139 if (on_newline || after_newline)
4140 {
4141 struct text_pos tpos;
4142 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4143
4144 SET_TEXT_POS (tpos, newpos, bpos);
4145 reseat_1 (it, tpos, 0);
4146 /* If we reseat on a newline/ZV, we need to prep the
4147 bidi iterator for advancing to the next character
4148 after the newline/EOB, keeping the current paragraph
4149 direction (so that PRODUCE_GLYPHS does TRT wrt
4150 prepending/appending glyphs to a glyph row). */
4151 if (on_newline)
4152 {
4153 it->bidi_it.first_elt = 0;
4154 it->bidi_it.paragraph_dir = pdir;
4155 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4156 it->bidi_it.nchars = 1;
4157 it->bidi_it.ch_len = 1;
4158 }
4159 }
4160 else /* Must use the slow method. */
4161 {
4162 /* With bidi iteration, the region of invisible text
4163 could start and/or end in the middle of a
4164 non-base embedding level. Therefore, we need to
4165 skip invisible text using the bidi iterator,
4166 starting at IT's current position, until we find
4167 ourselves outside of the invisible text.
4168 Skipping invisible text _after_ bidi iteration
4169 avoids affecting the visual order of the
4170 displayed text when invisible properties are
4171 added or removed. */
4172 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4173 {
4174 /* If we were `reseat'ed to a new paragraph,
4175 determine the paragraph base direction. We
4176 need to do it now because
4177 next_element_from_buffer may not have a
4178 chance to do it, if we are going to skip any
4179 text at the beginning, which resets the
4180 FIRST_ELT flag. */
4181 bidi_paragraph_init (it->paragraph_embedding,
4182 &it->bidi_it, 1);
4183 }
4184 do
4185 {
4186 bidi_move_to_visually_next (&it->bidi_it);
4187 }
4188 while (it->stop_charpos <= it->bidi_it.charpos
4189 && it->bidi_it.charpos < newpos);
4190 IT_CHARPOS (*it) = it->bidi_it.charpos;
4191 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4192 /* If we overstepped NEWPOS, record its position in
4193 the iterator, so that we skip invisible text if
4194 later the bidi iteration lands us in the
4195 invisible region again. */
4196 if (IT_CHARPOS (*it) >= newpos)
4197 it->prev_stop = newpos;
4198 }
4199 }
4200 else
4201 {
4202 IT_CHARPOS (*it) = newpos;
4203 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4204 }
4205
4206 /* If there are before-strings at the start of invisible
4207 text, and the text is invisible because of a text
4208 property, arrange to show before-strings because 20.x did
4209 it that way. (If the text is invisible because of an
4210 overlay property instead of a text property, this is
4211 already handled in the overlay code.) */
4212 if (NILP (overlay)
4213 && get_overlay_strings (it, it->stop_charpos))
4214 {
4215 handled = HANDLED_RECOMPUTE_PROPS;
4216 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4217 }
4218 else if (display_ellipsis_p)
4219 {
4220 /* Make sure that the glyphs of the ellipsis will get
4221 correct `charpos' values. If we would not update
4222 it->position here, the glyphs would belong to the
4223 last visible character _before_ the invisible
4224 text, which confuses `set_cursor_from_row'.
4225
4226 We use the last invisible position instead of the
4227 first because this way the cursor is always drawn on
4228 the first "." of the ellipsis, whenever PT is inside
4229 the invisible text. Otherwise the cursor would be
4230 placed _after_ the ellipsis when the point is after the
4231 first invisible character. */
4232 if (!STRINGP (it->object))
4233 {
4234 it->position.charpos = newpos - 1;
4235 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4236 }
4237 it->ellipsis_p = 1;
4238 /* Let the ellipsis display before
4239 considering any properties of the following char.
4240 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4241 handled = HANDLED_RETURN;
4242 }
4243 }
4244 }
4245
4246 return handled;
4247 }
4248
4249
4250 /* Make iterator IT return `...' next.
4251 Replaces LEN characters from buffer. */
4252
4253 static void
4254 setup_for_ellipsis (struct it *it, int len)
4255 {
4256 /* Use the display table definition for `...'. Invalid glyphs
4257 will be handled by the method returning elements from dpvec. */
4258 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4259 {
4260 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4261 it->dpvec = v->contents;
4262 it->dpend = v->contents + v->header.size;
4263 }
4264 else
4265 {
4266 /* Default `...'. */
4267 it->dpvec = default_invis_vector;
4268 it->dpend = default_invis_vector + 3;
4269 }
4270
4271 it->dpvec_char_len = len;
4272 it->current.dpvec_index = 0;
4273 it->dpvec_face_id = -1;
4274
4275 /* Remember the current face id in case glyphs specify faces.
4276 IT's face is restored in set_iterator_to_next.
4277 saved_face_id was set to preceding char's face in handle_stop. */
4278 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4279 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4280
4281 it->method = GET_FROM_DISPLAY_VECTOR;
4282 it->ellipsis_p = 1;
4283 }
4284
4285
4286 \f
4287 /***********************************************************************
4288 'display' property
4289 ***********************************************************************/
4290
4291 /* Set up iterator IT from `display' property at its current position.
4292 Called from handle_stop.
4293 We return HANDLED_RETURN if some part of the display property
4294 overrides the display of the buffer text itself.
4295 Otherwise we return HANDLED_NORMALLY. */
4296
4297 static enum prop_handled
4298 handle_display_prop (struct it *it)
4299 {
4300 Lisp_Object propval, object, overlay;
4301 struct text_pos *position;
4302 EMACS_INT bufpos;
4303 /* Nonzero if some property replaces the display of the text itself. */
4304 int display_replaced_p = 0;
4305
4306 if (STRINGP (it->string))
4307 {
4308 object = it->string;
4309 position = &it->current.string_pos;
4310 bufpos = CHARPOS (it->current.pos);
4311 }
4312 else
4313 {
4314 XSETWINDOW (object, it->w);
4315 position = &it->current.pos;
4316 bufpos = CHARPOS (*position);
4317 }
4318
4319 /* Reset those iterator values set from display property values. */
4320 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4321 it->space_width = Qnil;
4322 it->font_height = Qnil;
4323 it->voffset = 0;
4324
4325 /* We don't support recursive `display' properties, i.e. string
4326 values that have a string `display' property, that have a string
4327 `display' property etc. */
4328 if (!it->string_from_display_prop_p)
4329 it->area = TEXT_AREA;
4330
4331 propval = get_char_property_and_overlay (make_number (position->charpos),
4332 Qdisplay, object, &overlay);
4333 if (NILP (propval))
4334 return HANDLED_NORMALLY;
4335 /* Now OVERLAY is the overlay that gave us this property, or nil
4336 if it was a text property. */
4337
4338 if (!STRINGP (it->string))
4339 object = it->w->buffer;
4340
4341 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4342 position, bufpos,
4343 FRAME_WINDOW_P (it->f));
4344
4345 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4346 }
4347
4348 /* Subroutine of handle_display_prop. Returns non-zero if the display
4349 specification in SPEC is a replacing specification, i.e. it would
4350 replace the text covered by `display' property with something else,
4351 such as an image or a display string. If SPEC includes any kind or
4352 `(space ...) specification, the value is 2; this is used by
4353 compute_display_string_pos, which see.
4354
4355 See handle_single_display_spec for documentation of arguments.
4356 frame_window_p is non-zero if the window being redisplayed is on a
4357 GUI frame; this argument is used only if IT is NULL, see below.
4358
4359 IT can be NULL, if this is called by the bidi reordering code
4360 through compute_display_string_pos, which see. In that case, this
4361 function only examines SPEC, but does not otherwise "handle" it, in
4362 the sense that it doesn't set up members of IT from the display
4363 spec. */
4364 static int
4365 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4366 Lisp_Object overlay, struct text_pos *position,
4367 EMACS_INT bufpos, int frame_window_p)
4368 {
4369 int replacing_p = 0;
4370 int rv;
4371
4372 if (CONSP (spec)
4373 /* Simple specifications. */
4374 && !EQ (XCAR (spec), Qimage)
4375 && !EQ (XCAR (spec), Qspace)
4376 && !EQ (XCAR (spec), Qwhen)
4377 && !EQ (XCAR (spec), Qslice)
4378 && !EQ (XCAR (spec), Qspace_width)
4379 && !EQ (XCAR (spec), Qheight)
4380 && !EQ (XCAR (spec), Qraise)
4381 /* Marginal area specifications. */
4382 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4383 && !EQ (XCAR (spec), Qleft_fringe)
4384 && !EQ (XCAR (spec), Qright_fringe)
4385 && !NILP (XCAR (spec)))
4386 {
4387 for (; CONSP (spec); spec = XCDR (spec))
4388 {
4389 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4390 overlay, position, bufpos,
4391 replacing_p, frame_window_p)))
4392 {
4393 replacing_p = rv;
4394 /* If some text in a string is replaced, `position' no
4395 longer points to the position of `object'. */
4396 if (!it || STRINGP (object))
4397 break;
4398 }
4399 }
4400 }
4401 else if (VECTORP (spec))
4402 {
4403 int i;
4404 for (i = 0; i < ASIZE (spec); ++i)
4405 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4406 overlay, position, bufpos,
4407 replacing_p, frame_window_p)))
4408 {
4409 replacing_p = rv;
4410 /* If some text in a string is replaced, `position' no
4411 longer points to the position of `object'. */
4412 if (!it || STRINGP (object))
4413 break;
4414 }
4415 }
4416 else
4417 {
4418 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4419 position, bufpos, 0,
4420 frame_window_p)))
4421 replacing_p = rv;
4422 }
4423
4424 return replacing_p;
4425 }
4426
4427 /* Value is the position of the end of the `display' property starting
4428 at START_POS in OBJECT. */
4429
4430 static struct text_pos
4431 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4432 {
4433 Lisp_Object end;
4434 struct text_pos end_pos;
4435
4436 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4437 Qdisplay, object, Qnil);
4438 CHARPOS (end_pos) = XFASTINT (end);
4439 if (STRINGP (object))
4440 compute_string_pos (&end_pos, start_pos, it->string);
4441 else
4442 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4443
4444 return end_pos;
4445 }
4446
4447
4448 /* Set up IT from a single `display' property specification SPEC. OBJECT
4449 is the object in which the `display' property was found. *POSITION
4450 is the position in OBJECT at which the `display' property was found.
4451 BUFPOS is the buffer position of OBJECT (different from POSITION if
4452 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4453 previously saw a display specification which already replaced text
4454 display with something else, for example an image; we ignore such
4455 properties after the first one has been processed.
4456
4457 OVERLAY is the overlay this `display' property came from,
4458 or nil if it was a text property.
4459
4460 If SPEC is a `space' or `image' specification, and in some other
4461 cases too, set *POSITION to the position where the `display'
4462 property ends.
4463
4464 If IT is NULL, only examine the property specification in SPEC, but
4465 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4466 is intended to be displayed in a window on a GUI frame.
4467
4468 Value is non-zero if something was found which replaces the display
4469 of buffer or string text. */
4470
4471 static int
4472 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4473 Lisp_Object overlay, struct text_pos *position,
4474 EMACS_INT bufpos, int display_replaced_p,
4475 int frame_window_p)
4476 {
4477 Lisp_Object form;
4478 Lisp_Object location, value;
4479 struct text_pos start_pos = *position;
4480 int valid_p;
4481
4482 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4483 If the result is non-nil, use VALUE instead of SPEC. */
4484 form = Qt;
4485 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4486 {
4487 spec = XCDR (spec);
4488 if (!CONSP (spec))
4489 return 0;
4490 form = XCAR (spec);
4491 spec = XCDR (spec);
4492 }
4493
4494 if (!NILP (form) && !EQ (form, Qt))
4495 {
4496 int count = SPECPDL_INDEX ();
4497 struct gcpro gcpro1;
4498
4499 /* Bind `object' to the object having the `display' property, a
4500 buffer or string. Bind `position' to the position in the
4501 object where the property was found, and `buffer-position'
4502 to the current position in the buffer. */
4503
4504 if (NILP (object))
4505 XSETBUFFER (object, current_buffer);
4506 specbind (Qobject, object);
4507 specbind (Qposition, make_number (CHARPOS (*position)));
4508 specbind (Qbuffer_position, make_number (bufpos));
4509 GCPRO1 (form);
4510 form = safe_eval (form);
4511 UNGCPRO;
4512 unbind_to (count, Qnil);
4513 }
4514
4515 if (NILP (form))
4516 return 0;
4517
4518 /* Handle `(height HEIGHT)' specifications. */
4519 if (CONSP (spec)
4520 && EQ (XCAR (spec), Qheight)
4521 && CONSP (XCDR (spec)))
4522 {
4523 if (it)
4524 {
4525 if (!FRAME_WINDOW_P (it->f))
4526 return 0;
4527
4528 it->font_height = XCAR (XCDR (spec));
4529 if (!NILP (it->font_height))
4530 {
4531 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4532 int new_height = -1;
4533
4534 if (CONSP (it->font_height)
4535 && (EQ (XCAR (it->font_height), Qplus)
4536 || EQ (XCAR (it->font_height), Qminus))
4537 && CONSP (XCDR (it->font_height))
4538 && INTEGERP (XCAR (XCDR (it->font_height))))
4539 {
4540 /* `(+ N)' or `(- N)' where N is an integer. */
4541 int steps = XINT (XCAR (XCDR (it->font_height)));
4542 if (EQ (XCAR (it->font_height), Qplus))
4543 steps = - steps;
4544 it->face_id = smaller_face (it->f, it->face_id, steps);
4545 }
4546 else if (FUNCTIONP (it->font_height))
4547 {
4548 /* Call function with current height as argument.
4549 Value is the new height. */
4550 Lisp_Object height;
4551 height = safe_call1 (it->font_height,
4552 face->lface[LFACE_HEIGHT_INDEX]);
4553 if (NUMBERP (height))
4554 new_height = XFLOATINT (height);
4555 }
4556 else if (NUMBERP (it->font_height))
4557 {
4558 /* Value is a multiple of the canonical char height. */
4559 struct face *f;
4560
4561 f = FACE_FROM_ID (it->f,
4562 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4563 new_height = (XFLOATINT (it->font_height)
4564 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4565 }
4566 else
4567 {
4568 /* Evaluate IT->font_height with `height' bound to the
4569 current specified height to get the new height. */
4570 int count = SPECPDL_INDEX ();
4571
4572 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4573 value = safe_eval (it->font_height);
4574 unbind_to (count, Qnil);
4575
4576 if (NUMBERP (value))
4577 new_height = XFLOATINT (value);
4578 }
4579
4580 if (new_height > 0)
4581 it->face_id = face_with_height (it->f, it->face_id, new_height);
4582 }
4583 }
4584
4585 return 0;
4586 }
4587
4588 /* Handle `(space-width WIDTH)'. */
4589 if (CONSP (spec)
4590 && EQ (XCAR (spec), Qspace_width)
4591 && CONSP (XCDR (spec)))
4592 {
4593 if (it)
4594 {
4595 if (!FRAME_WINDOW_P (it->f))
4596 return 0;
4597
4598 value = XCAR (XCDR (spec));
4599 if (NUMBERP (value) && XFLOATINT (value) > 0)
4600 it->space_width = value;
4601 }
4602
4603 return 0;
4604 }
4605
4606 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4607 if (CONSP (spec)
4608 && EQ (XCAR (spec), Qslice))
4609 {
4610 Lisp_Object tem;
4611
4612 if (it)
4613 {
4614 if (!FRAME_WINDOW_P (it->f))
4615 return 0;
4616
4617 if (tem = XCDR (spec), CONSP (tem))
4618 {
4619 it->slice.x = XCAR (tem);
4620 if (tem = XCDR (tem), CONSP (tem))
4621 {
4622 it->slice.y = XCAR (tem);
4623 if (tem = XCDR (tem), CONSP (tem))
4624 {
4625 it->slice.width = XCAR (tem);
4626 if (tem = XCDR (tem), CONSP (tem))
4627 it->slice.height = XCAR (tem);
4628 }
4629 }
4630 }
4631 }
4632
4633 return 0;
4634 }
4635
4636 /* Handle `(raise FACTOR)'. */
4637 if (CONSP (spec)
4638 && EQ (XCAR (spec), Qraise)
4639 && CONSP (XCDR (spec)))
4640 {
4641 if (it)
4642 {
4643 if (!FRAME_WINDOW_P (it->f))
4644 return 0;
4645
4646 #ifdef HAVE_WINDOW_SYSTEM
4647 value = XCAR (XCDR (spec));
4648 if (NUMBERP (value))
4649 {
4650 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4651 it->voffset = - (XFLOATINT (value)
4652 * (FONT_HEIGHT (face->font)));
4653 }
4654 #endif /* HAVE_WINDOW_SYSTEM */
4655 }
4656
4657 return 0;
4658 }
4659
4660 /* Don't handle the other kinds of display specifications
4661 inside a string that we got from a `display' property. */
4662 if (it && it->string_from_display_prop_p)
4663 return 0;
4664
4665 /* Characters having this form of property are not displayed, so
4666 we have to find the end of the property. */
4667 if (it)
4668 {
4669 start_pos = *position;
4670 *position = display_prop_end (it, object, start_pos);
4671 }
4672 value = Qnil;
4673
4674 /* Stop the scan at that end position--we assume that all
4675 text properties change there. */
4676 if (it)
4677 it->stop_charpos = position->charpos;
4678
4679 /* Handle `(left-fringe BITMAP [FACE])'
4680 and `(right-fringe BITMAP [FACE])'. */
4681 if (CONSP (spec)
4682 && (EQ (XCAR (spec), Qleft_fringe)
4683 || EQ (XCAR (spec), Qright_fringe))
4684 && CONSP (XCDR (spec)))
4685 {
4686 int fringe_bitmap;
4687
4688 if (it)
4689 {
4690 if (!FRAME_WINDOW_P (it->f))
4691 /* If we return here, POSITION has been advanced
4692 across the text with this property. */
4693 {
4694 /* Synchronize the bidi iterator with POSITION. This is
4695 needed because we are not going to push the iterator
4696 on behalf of this display property, so there will be
4697 no pop_it call to do this synchronization for us. */
4698 if (it->bidi_p)
4699 {
4700 it->position = *position;
4701 iterate_out_of_display_property (it);
4702 *position = it->position;
4703 }
4704 return 1;
4705 }
4706 }
4707 else if (!frame_window_p)
4708 return 1;
4709
4710 #ifdef HAVE_WINDOW_SYSTEM
4711 value = XCAR (XCDR (spec));
4712 if (!SYMBOLP (value)
4713 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4714 /* If we return here, POSITION has been advanced
4715 across the text with this property. */
4716 {
4717 if (it && it->bidi_p)
4718 {
4719 it->position = *position;
4720 iterate_out_of_display_property (it);
4721 *position = it->position;
4722 }
4723 return 1;
4724 }
4725
4726 if (it)
4727 {
4728 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4729
4730 if (CONSP (XCDR (XCDR (spec))))
4731 {
4732 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4733 int face_id2 = lookup_derived_face (it->f, face_name,
4734 FRINGE_FACE_ID, 0);
4735 if (face_id2 >= 0)
4736 face_id = face_id2;
4737 }
4738
4739 /* Save current settings of IT so that we can restore them
4740 when we are finished with the glyph property value. */
4741 push_it (it, position);
4742
4743 it->area = TEXT_AREA;
4744 it->what = IT_IMAGE;
4745 it->image_id = -1; /* no image */
4746 it->position = start_pos;
4747 it->object = NILP (object) ? it->w->buffer : object;
4748 it->method = GET_FROM_IMAGE;
4749 it->from_overlay = Qnil;
4750 it->face_id = face_id;
4751 it->from_disp_prop_p = 1;
4752
4753 /* Say that we haven't consumed the characters with
4754 `display' property yet. The call to pop_it in
4755 set_iterator_to_next will clean this up. */
4756 *position = start_pos;
4757
4758 if (EQ (XCAR (spec), Qleft_fringe))
4759 {
4760 it->left_user_fringe_bitmap = fringe_bitmap;
4761 it->left_user_fringe_face_id = face_id;
4762 }
4763 else
4764 {
4765 it->right_user_fringe_bitmap = fringe_bitmap;
4766 it->right_user_fringe_face_id = face_id;
4767 }
4768 }
4769 #endif /* HAVE_WINDOW_SYSTEM */
4770 return 1;
4771 }
4772
4773 /* Prepare to handle `((margin left-margin) ...)',
4774 `((margin right-margin) ...)' and `((margin nil) ...)'
4775 prefixes for display specifications. */
4776 location = Qunbound;
4777 if (CONSP (spec) && CONSP (XCAR (spec)))
4778 {
4779 Lisp_Object tem;
4780
4781 value = XCDR (spec);
4782 if (CONSP (value))
4783 value = XCAR (value);
4784
4785 tem = XCAR (spec);
4786 if (EQ (XCAR (tem), Qmargin)
4787 && (tem = XCDR (tem),
4788 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4789 (NILP (tem)
4790 || EQ (tem, Qleft_margin)
4791 || EQ (tem, Qright_margin))))
4792 location = tem;
4793 }
4794
4795 if (EQ (location, Qunbound))
4796 {
4797 location = Qnil;
4798 value = spec;
4799 }
4800
4801 /* After this point, VALUE is the property after any
4802 margin prefix has been stripped. It must be a string,
4803 an image specification, or `(space ...)'.
4804
4805 LOCATION specifies where to display: `left-margin',
4806 `right-margin' or nil. */
4807
4808 valid_p = (STRINGP (value)
4809 #ifdef HAVE_WINDOW_SYSTEM
4810 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4811 && valid_image_p (value))
4812 #endif /* not HAVE_WINDOW_SYSTEM */
4813 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4814
4815 if (valid_p && !display_replaced_p)
4816 {
4817 int retval = 1;
4818
4819 if (!it)
4820 {
4821 /* Callers need to know whether the display spec is any kind
4822 of `(space ...)' spec that is about to affect text-area
4823 display. */
4824 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4825 retval = 2;
4826 return retval;
4827 }
4828
4829 /* Save current settings of IT so that we can restore them
4830 when we are finished with the glyph property value. */
4831 push_it (it, position);
4832 it->from_overlay = overlay;
4833 it->from_disp_prop_p = 1;
4834
4835 if (NILP (location))
4836 it->area = TEXT_AREA;
4837 else if (EQ (location, Qleft_margin))
4838 it->area = LEFT_MARGIN_AREA;
4839 else
4840 it->area = RIGHT_MARGIN_AREA;
4841
4842 if (STRINGP (value))
4843 {
4844 it->string = value;
4845 it->multibyte_p = STRING_MULTIBYTE (it->string);
4846 it->current.overlay_string_index = -1;
4847 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4848 it->end_charpos = it->string_nchars = SCHARS (it->string);
4849 it->method = GET_FROM_STRING;
4850 it->stop_charpos = 0;
4851 it->prev_stop = 0;
4852 it->base_level_stop = 0;
4853 it->string_from_display_prop_p = 1;
4854 /* Say that we haven't consumed the characters with
4855 `display' property yet. The call to pop_it in
4856 set_iterator_to_next will clean this up. */
4857 if (BUFFERP (object))
4858 *position = start_pos;
4859
4860 /* Force paragraph direction to be that of the parent
4861 object. If the parent object's paragraph direction is
4862 not yet determined, default to L2R. */
4863 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4864 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4865 else
4866 it->paragraph_embedding = L2R;
4867
4868 /* Set up the bidi iterator for this display string. */
4869 if (it->bidi_p)
4870 {
4871 it->bidi_it.string.lstring = it->string;
4872 it->bidi_it.string.s = NULL;
4873 it->bidi_it.string.schars = it->end_charpos;
4874 it->bidi_it.string.bufpos = bufpos;
4875 it->bidi_it.string.from_disp_str = 1;
4876 it->bidi_it.string.unibyte = !it->multibyte_p;
4877 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4878 }
4879 }
4880 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4881 {
4882 it->method = GET_FROM_STRETCH;
4883 it->object = value;
4884 *position = it->position = start_pos;
4885 retval = 1 + (it->area == TEXT_AREA);
4886 }
4887 #ifdef HAVE_WINDOW_SYSTEM
4888 else
4889 {
4890 it->what = IT_IMAGE;
4891 it->image_id = lookup_image (it->f, value);
4892 it->position = start_pos;
4893 it->object = NILP (object) ? it->w->buffer : object;
4894 it->method = GET_FROM_IMAGE;
4895
4896 /* Say that we haven't consumed the characters with
4897 `display' property yet. The call to pop_it in
4898 set_iterator_to_next will clean this up. */
4899 *position = start_pos;
4900 }
4901 #endif /* HAVE_WINDOW_SYSTEM */
4902
4903 return retval;
4904 }
4905
4906 /* Invalid property or property not supported. Restore
4907 POSITION to what it was before. */
4908 *position = start_pos;
4909 return 0;
4910 }
4911
4912 /* Check if PROP is a display property value whose text should be
4913 treated as intangible. OVERLAY is the overlay from which PROP
4914 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4915 specify the buffer position covered by PROP. */
4916
4917 int
4918 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4919 EMACS_INT charpos, EMACS_INT bytepos)
4920 {
4921 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4922 struct text_pos position;
4923
4924 SET_TEXT_POS (position, charpos, bytepos);
4925 return handle_display_spec (NULL, prop, Qnil, overlay,
4926 &position, charpos, frame_window_p);
4927 }
4928
4929
4930 /* Return 1 if PROP is a display sub-property value containing STRING.
4931
4932 Implementation note: this and the following function are really
4933 special cases of handle_display_spec and
4934 handle_single_display_spec, and should ideally use the same code.
4935 Until they do, these two pairs must be consistent and must be
4936 modified in sync. */
4937
4938 static int
4939 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4940 {
4941 if (EQ (string, prop))
4942 return 1;
4943
4944 /* Skip over `when FORM'. */
4945 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4946 {
4947 prop = XCDR (prop);
4948 if (!CONSP (prop))
4949 return 0;
4950 /* Actually, the condition following `when' should be eval'ed,
4951 like handle_single_display_spec does, and we should return
4952 zero if it evaluates to nil. However, this function is
4953 called only when the buffer was already displayed and some
4954 glyph in the glyph matrix was found to come from a display
4955 string. Therefore, the condition was already evaluated, and
4956 the result was non-nil, otherwise the display string wouldn't
4957 have been displayed and we would have never been called for
4958 this property. Thus, we can skip the evaluation and assume
4959 its result is non-nil. */
4960 prop = XCDR (prop);
4961 }
4962
4963 if (CONSP (prop))
4964 /* Skip over `margin LOCATION'. */
4965 if (EQ (XCAR (prop), Qmargin))
4966 {
4967 prop = XCDR (prop);
4968 if (!CONSP (prop))
4969 return 0;
4970
4971 prop = XCDR (prop);
4972 if (!CONSP (prop))
4973 return 0;
4974 }
4975
4976 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4977 }
4978
4979
4980 /* Return 1 if STRING appears in the `display' property PROP. */
4981
4982 static int
4983 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4984 {
4985 if (CONSP (prop)
4986 && !EQ (XCAR (prop), Qwhen)
4987 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4988 {
4989 /* A list of sub-properties. */
4990 while (CONSP (prop))
4991 {
4992 if (single_display_spec_string_p (XCAR (prop), string))
4993 return 1;
4994 prop = XCDR (prop);
4995 }
4996 }
4997 else if (VECTORP (prop))
4998 {
4999 /* A vector of sub-properties. */
5000 int i;
5001 for (i = 0; i < ASIZE (prop); ++i)
5002 if (single_display_spec_string_p (AREF (prop, i), string))
5003 return 1;
5004 }
5005 else
5006 return single_display_spec_string_p (prop, string);
5007
5008 return 0;
5009 }
5010
5011 /* Look for STRING in overlays and text properties in the current
5012 buffer, between character positions FROM and TO (excluding TO).
5013 BACK_P non-zero means look back (in this case, TO is supposed to be
5014 less than FROM).
5015 Value is the first character position where STRING was found, or
5016 zero if it wasn't found before hitting TO.
5017
5018 This function may only use code that doesn't eval because it is
5019 called asynchronously from note_mouse_highlight. */
5020
5021 static EMACS_INT
5022 string_buffer_position_lim (Lisp_Object string,
5023 EMACS_INT from, EMACS_INT to, int back_p)
5024 {
5025 Lisp_Object limit, prop, pos;
5026 int found = 0;
5027
5028 pos = make_number (max (from, BEGV));
5029
5030 if (!back_p) /* looking forward */
5031 {
5032 limit = make_number (min (to, ZV));
5033 while (!found && !EQ (pos, limit))
5034 {
5035 prop = Fget_char_property (pos, Qdisplay, Qnil);
5036 if (!NILP (prop) && display_prop_string_p (prop, string))
5037 found = 1;
5038 else
5039 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5040 limit);
5041 }
5042 }
5043 else /* looking back */
5044 {
5045 limit = make_number (max (to, BEGV));
5046 while (!found && !EQ (pos, limit))
5047 {
5048 prop = Fget_char_property (pos, Qdisplay, Qnil);
5049 if (!NILP (prop) && display_prop_string_p (prop, string))
5050 found = 1;
5051 else
5052 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5053 limit);
5054 }
5055 }
5056
5057 return found ? XINT (pos) : 0;
5058 }
5059
5060 /* Determine which buffer position in current buffer STRING comes from.
5061 AROUND_CHARPOS is an approximate position where it could come from.
5062 Value is the buffer position or 0 if it couldn't be determined.
5063
5064 This function is necessary because we don't record buffer positions
5065 in glyphs generated from strings (to keep struct glyph small).
5066 This function may only use code that doesn't eval because it is
5067 called asynchronously from note_mouse_highlight. */
5068
5069 static EMACS_INT
5070 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5071 {
5072 const int MAX_DISTANCE = 1000;
5073 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5074 around_charpos + MAX_DISTANCE,
5075 0);
5076
5077 if (!found)
5078 found = string_buffer_position_lim (string, around_charpos,
5079 around_charpos - MAX_DISTANCE, 1);
5080 return found;
5081 }
5082
5083
5084 \f
5085 /***********************************************************************
5086 `composition' property
5087 ***********************************************************************/
5088
5089 /* Set up iterator IT from `composition' property at its current
5090 position. Called from handle_stop. */
5091
5092 static enum prop_handled
5093 handle_composition_prop (struct it *it)
5094 {
5095 Lisp_Object prop, string;
5096 EMACS_INT pos, pos_byte, start, end;
5097
5098 if (STRINGP (it->string))
5099 {
5100 unsigned char *s;
5101
5102 pos = IT_STRING_CHARPOS (*it);
5103 pos_byte = IT_STRING_BYTEPOS (*it);
5104 string = it->string;
5105 s = SDATA (string) + pos_byte;
5106 it->c = STRING_CHAR (s);
5107 }
5108 else
5109 {
5110 pos = IT_CHARPOS (*it);
5111 pos_byte = IT_BYTEPOS (*it);
5112 string = Qnil;
5113 it->c = FETCH_CHAR (pos_byte);
5114 }
5115
5116 /* If there's a valid composition and point is not inside of the
5117 composition (in the case that the composition is from the current
5118 buffer), draw a glyph composed from the composition components. */
5119 if (find_composition (pos, -1, &start, &end, &prop, string)
5120 && COMPOSITION_VALID_P (start, end, prop)
5121 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5122 {
5123 if (start < pos)
5124 /* As we can't handle this situation (perhaps font-lock added
5125 a new composition), we just return here hoping that next
5126 redisplay will detect this composition much earlier. */
5127 return HANDLED_NORMALLY;
5128 if (start != pos)
5129 {
5130 if (STRINGP (it->string))
5131 pos_byte = string_char_to_byte (it->string, start);
5132 else
5133 pos_byte = CHAR_TO_BYTE (start);
5134 }
5135 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5136 prop, string);
5137
5138 if (it->cmp_it.id >= 0)
5139 {
5140 it->cmp_it.ch = -1;
5141 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5142 it->cmp_it.nglyphs = -1;
5143 }
5144 }
5145
5146 return HANDLED_NORMALLY;
5147 }
5148
5149
5150 \f
5151 /***********************************************************************
5152 Overlay strings
5153 ***********************************************************************/
5154
5155 /* The following structure is used to record overlay strings for
5156 later sorting in load_overlay_strings. */
5157
5158 struct overlay_entry
5159 {
5160 Lisp_Object overlay;
5161 Lisp_Object string;
5162 int priority;
5163 int after_string_p;
5164 };
5165
5166
5167 /* Set up iterator IT from overlay strings at its current position.
5168 Called from handle_stop. */
5169
5170 static enum prop_handled
5171 handle_overlay_change (struct it *it)
5172 {
5173 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5174 return HANDLED_RECOMPUTE_PROPS;
5175 else
5176 return HANDLED_NORMALLY;
5177 }
5178
5179
5180 /* Set up the next overlay string for delivery by IT, if there is an
5181 overlay string to deliver. Called by set_iterator_to_next when the
5182 end of the current overlay string is reached. If there are more
5183 overlay strings to display, IT->string and
5184 IT->current.overlay_string_index are set appropriately here.
5185 Otherwise IT->string is set to nil. */
5186
5187 static void
5188 next_overlay_string (struct it *it)
5189 {
5190 ++it->current.overlay_string_index;
5191 if (it->current.overlay_string_index == it->n_overlay_strings)
5192 {
5193 /* No more overlay strings. Restore IT's settings to what
5194 they were before overlay strings were processed, and
5195 continue to deliver from current_buffer. */
5196
5197 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5198 pop_it (it);
5199 xassert (it->sp > 0
5200 || (NILP (it->string)
5201 && it->method == GET_FROM_BUFFER
5202 && it->stop_charpos >= BEGV
5203 && it->stop_charpos <= it->end_charpos));
5204 it->current.overlay_string_index = -1;
5205 it->n_overlay_strings = 0;
5206 it->overlay_strings_charpos = -1;
5207 /* If there's an empty display string on the stack, pop the
5208 stack, to resync the bidi iterator with IT's position. Such
5209 empty strings are pushed onto the stack in
5210 get_overlay_strings_1. */
5211 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5212 pop_it (it);
5213
5214 /* If we're at the end of the buffer, record that we have
5215 processed the overlay strings there already, so that
5216 next_element_from_buffer doesn't try it again. */
5217 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5218 it->overlay_strings_at_end_processed_p = 1;
5219 }
5220 else
5221 {
5222 /* There are more overlay strings to process. If
5223 IT->current.overlay_string_index has advanced to a position
5224 where we must load IT->overlay_strings with more strings, do
5225 it. We must load at the IT->overlay_strings_charpos where
5226 IT->n_overlay_strings was originally computed; when invisible
5227 text is present, this might not be IT_CHARPOS (Bug#7016). */
5228 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5229
5230 if (it->current.overlay_string_index && i == 0)
5231 load_overlay_strings (it, it->overlay_strings_charpos);
5232
5233 /* Initialize IT to deliver display elements from the overlay
5234 string. */
5235 it->string = it->overlay_strings[i];
5236 it->multibyte_p = STRING_MULTIBYTE (it->string);
5237 SET_TEXT_POS (it->current.string_pos, 0, 0);
5238 it->method = GET_FROM_STRING;
5239 it->stop_charpos = 0;
5240 if (it->cmp_it.stop_pos >= 0)
5241 it->cmp_it.stop_pos = 0;
5242 it->prev_stop = 0;
5243 it->base_level_stop = 0;
5244
5245 /* Set up the bidi iterator for this overlay string. */
5246 if (it->bidi_p)
5247 {
5248 it->bidi_it.string.lstring = it->string;
5249 it->bidi_it.string.s = NULL;
5250 it->bidi_it.string.schars = SCHARS (it->string);
5251 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5252 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5253 it->bidi_it.string.unibyte = !it->multibyte_p;
5254 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5255 }
5256 }
5257
5258 CHECK_IT (it);
5259 }
5260
5261
5262 /* Compare two overlay_entry structures E1 and E2. Used as a
5263 comparison function for qsort in load_overlay_strings. Overlay
5264 strings for the same position are sorted so that
5265
5266 1. All after-strings come in front of before-strings, except
5267 when they come from the same overlay.
5268
5269 2. Within after-strings, strings are sorted so that overlay strings
5270 from overlays with higher priorities come first.
5271
5272 2. Within before-strings, strings are sorted so that overlay
5273 strings from overlays with higher priorities come last.
5274
5275 Value is analogous to strcmp. */
5276
5277
5278 static int
5279 compare_overlay_entries (const void *e1, const void *e2)
5280 {
5281 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5282 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5283 int result;
5284
5285 if (entry1->after_string_p != entry2->after_string_p)
5286 {
5287 /* Let after-strings appear in front of before-strings if
5288 they come from different overlays. */
5289 if (EQ (entry1->overlay, entry2->overlay))
5290 result = entry1->after_string_p ? 1 : -1;
5291 else
5292 result = entry1->after_string_p ? -1 : 1;
5293 }
5294 else if (entry1->after_string_p)
5295 /* After-strings sorted in order of decreasing priority. */
5296 result = entry2->priority - entry1->priority;
5297 else
5298 /* Before-strings sorted in order of increasing priority. */
5299 result = entry1->priority - entry2->priority;
5300
5301 return result;
5302 }
5303
5304
5305 /* Load the vector IT->overlay_strings with overlay strings from IT's
5306 current buffer position, or from CHARPOS if that is > 0. Set
5307 IT->n_overlays to the total number of overlay strings found.
5308
5309 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5310 a time. On entry into load_overlay_strings,
5311 IT->current.overlay_string_index gives the number of overlay
5312 strings that have already been loaded by previous calls to this
5313 function.
5314
5315 IT->add_overlay_start contains an additional overlay start
5316 position to consider for taking overlay strings from, if non-zero.
5317 This position comes into play when the overlay has an `invisible'
5318 property, and both before and after-strings. When we've skipped to
5319 the end of the overlay, because of its `invisible' property, we
5320 nevertheless want its before-string to appear.
5321 IT->add_overlay_start will contain the overlay start position
5322 in this case.
5323
5324 Overlay strings are sorted so that after-string strings come in
5325 front of before-string strings. Within before and after-strings,
5326 strings are sorted by overlay priority. See also function
5327 compare_overlay_entries. */
5328
5329 static void
5330 load_overlay_strings (struct it *it, EMACS_INT charpos)
5331 {
5332 Lisp_Object overlay, window, str, invisible;
5333 struct Lisp_Overlay *ov;
5334 EMACS_INT start, end;
5335 int size = 20;
5336 int n = 0, i, j, invis_p;
5337 struct overlay_entry *entries
5338 = (struct overlay_entry *) alloca (size * sizeof *entries);
5339
5340 if (charpos <= 0)
5341 charpos = IT_CHARPOS (*it);
5342
5343 /* Append the overlay string STRING of overlay OVERLAY to vector
5344 `entries' which has size `size' and currently contains `n'
5345 elements. AFTER_P non-zero means STRING is an after-string of
5346 OVERLAY. */
5347 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5348 do \
5349 { \
5350 Lisp_Object priority; \
5351 \
5352 if (n == size) \
5353 { \
5354 int new_size = 2 * size; \
5355 struct overlay_entry *old = entries; \
5356 entries = \
5357 (struct overlay_entry *) alloca (new_size \
5358 * sizeof *entries); \
5359 memcpy (entries, old, size * sizeof *entries); \
5360 size = new_size; \
5361 } \
5362 \
5363 entries[n].string = (STRING); \
5364 entries[n].overlay = (OVERLAY); \
5365 priority = Foverlay_get ((OVERLAY), Qpriority); \
5366 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5367 entries[n].after_string_p = (AFTER_P); \
5368 ++n; \
5369 } \
5370 while (0)
5371
5372 /* Process overlay before the overlay center. */
5373 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5374 {
5375 XSETMISC (overlay, ov);
5376 xassert (OVERLAYP (overlay));
5377 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5378 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5379
5380 if (end < charpos)
5381 break;
5382
5383 /* Skip this overlay if it doesn't start or end at IT's current
5384 position. */
5385 if (end != charpos && start != charpos)
5386 continue;
5387
5388 /* Skip this overlay if it doesn't apply to IT->w. */
5389 window = Foverlay_get (overlay, Qwindow);
5390 if (WINDOWP (window) && XWINDOW (window) != it->w)
5391 continue;
5392
5393 /* If the text ``under'' the overlay is invisible, both before-
5394 and after-strings from this overlay are visible; start and
5395 end position are indistinguishable. */
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 /* Process overlays after the overlay center. */
5413 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5414 {
5415 XSETMISC (overlay, ov);
5416 xassert (OVERLAYP (overlay));
5417 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5418 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5419
5420 if (start > charpos)
5421 break;
5422
5423 /* Skip this overlay if it doesn't start or end at IT's current
5424 position. */
5425 if (end != charpos && start != charpos)
5426 continue;
5427
5428 /* Skip this overlay if it doesn't apply to IT->w. */
5429 window = Foverlay_get (overlay, Qwindow);
5430 if (WINDOWP (window) && XWINDOW (window) != it->w)
5431 continue;
5432
5433 /* If the text ``under'' the overlay is invisible, it has a zero
5434 dimension, and both before- and after-strings apply. */
5435 invisible = Foverlay_get (overlay, Qinvisible);
5436 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5437
5438 /* If overlay has a non-empty before-string, record it. */
5439 if ((start == charpos || (end == charpos && invis_p))
5440 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5441 && SCHARS (str))
5442 RECORD_OVERLAY_STRING (overlay, str, 0);
5443
5444 /* If overlay has a non-empty after-string, record it. */
5445 if ((end == charpos || (start == charpos && invis_p))
5446 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5447 && SCHARS (str))
5448 RECORD_OVERLAY_STRING (overlay, str, 1);
5449 }
5450
5451 #undef RECORD_OVERLAY_STRING
5452
5453 /* Sort entries. */
5454 if (n > 1)
5455 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5456
5457 /* Record number of overlay strings, and where we computed it. */
5458 it->n_overlay_strings = n;
5459 it->overlay_strings_charpos = charpos;
5460
5461 /* IT->current.overlay_string_index is the number of overlay strings
5462 that have already been consumed by IT. Copy some of the
5463 remaining overlay strings to IT->overlay_strings. */
5464 i = 0;
5465 j = it->current.overlay_string_index;
5466 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5467 {
5468 it->overlay_strings[i] = entries[j].string;
5469 it->string_overlays[i++] = entries[j++].overlay;
5470 }
5471
5472 CHECK_IT (it);
5473 }
5474
5475
5476 /* Get the first chunk of overlay strings at IT's current buffer
5477 position, or at CHARPOS if that is > 0. Value is non-zero if at
5478 least one overlay string was found. */
5479
5480 static int
5481 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5482 {
5483 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5484 process. This fills IT->overlay_strings with strings, and sets
5485 IT->n_overlay_strings to the total number of strings to process.
5486 IT->pos.overlay_string_index has to be set temporarily to zero
5487 because load_overlay_strings needs this; it must be set to -1
5488 when no overlay strings are found because a zero value would
5489 indicate a position in the first overlay string. */
5490 it->current.overlay_string_index = 0;
5491 load_overlay_strings (it, charpos);
5492
5493 /* If we found overlay strings, set up IT to deliver display
5494 elements from the first one. Otherwise set up IT to deliver
5495 from current_buffer. */
5496 if (it->n_overlay_strings)
5497 {
5498 /* Make sure we know settings in current_buffer, so that we can
5499 restore meaningful values when we're done with the overlay
5500 strings. */
5501 if (compute_stop_p)
5502 compute_stop_pos (it);
5503 xassert (it->face_id >= 0);
5504
5505 /* Save IT's settings. They are restored after all overlay
5506 strings have been processed. */
5507 xassert (!compute_stop_p || it->sp == 0);
5508
5509 /* When called from handle_stop, there might be an empty display
5510 string loaded. In that case, don't bother saving it. But
5511 don't use this optimization with the bidi iterator, since we
5512 need the corresponding pop_it call to resync the bidi
5513 iterator's position with IT's position, after we are done
5514 with the overlay strings. (The corresponding call to pop_it
5515 in case of an empty display string is in
5516 next_overlay_string.) */
5517 if (!(!it->bidi_p
5518 && STRINGP (it->string) && !SCHARS (it->string)))
5519 push_it (it, NULL);
5520
5521 /* Set up IT to deliver display elements from the first overlay
5522 string. */
5523 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5524 it->string = it->overlay_strings[0];
5525 it->from_overlay = Qnil;
5526 it->stop_charpos = 0;
5527 xassert (STRINGP (it->string));
5528 it->end_charpos = SCHARS (it->string);
5529 it->prev_stop = 0;
5530 it->base_level_stop = 0;
5531 it->multibyte_p = STRING_MULTIBYTE (it->string);
5532 it->method = GET_FROM_STRING;
5533 it->from_disp_prop_p = 0;
5534
5535 /* Force paragraph direction to be that of the parent
5536 buffer. */
5537 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5538 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5539 else
5540 it->paragraph_embedding = L2R;
5541
5542 /* Set up the bidi iterator for this overlay string. */
5543 if (it->bidi_p)
5544 {
5545 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5546
5547 it->bidi_it.string.lstring = it->string;
5548 it->bidi_it.string.s = NULL;
5549 it->bidi_it.string.schars = SCHARS (it->string);
5550 it->bidi_it.string.bufpos = pos;
5551 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5552 it->bidi_it.string.unibyte = !it->multibyte_p;
5553 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5554 }
5555 return 1;
5556 }
5557
5558 it->current.overlay_string_index = -1;
5559 return 0;
5560 }
5561
5562 static int
5563 get_overlay_strings (struct it *it, EMACS_INT charpos)
5564 {
5565 it->string = Qnil;
5566 it->method = GET_FROM_BUFFER;
5567
5568 (void) get_overlay_strings_1 (it, charpos, 1);
5569
5570 CHECK_IT (it);
5571
5572 /* Value is non-zero if we found at least one overlay string. */
5573 return STRINGP (it->string);
5574 }
5575
5576
5577 \f
5578 /***********************************************************************
5579 Saving and restoring state
5580 ***********************************************************************/
5581
5582 /* Save current settings of IT on IT->stack. Called, for example,
5583 before setting up IT for an overlay string, to be able to restore
5584 IT's settings to what they were after the overlay string has been
5585 processed. If POSITION is non-NULL, it is the position to save on
5586 the stack instead of IT->position. */
5587
5588 static void
5589 push_it (struct it *it, struct text_pos *position)
5590 {
5591 struct iterator_stack_entry *p;
5592
5593 xassert (it->sp < IT_STACK_SIZE);
5594 p = it->stack + it->sp;
5595
5596 p->stop_charpos = it->stop_charpos;
5597 p->prev_stop = it->prev_stop;
5598 p->base_level_stop = it->base_level_stop;
5599 p->cmp_it = it->cmp_it;
5600 xassert (it->face_id >= 0);
5601 p->face_id = it->face_id;
5602 p->string = it->string;
5603 p->method = it->method;
5604 p->from_overlay = it->from_overlay;
5605 switch (p->method)
5606 {
5607 case GET_FROM_IMAGE:
5608 p->u.image.object = it->object;
5609 p->u.image.image_id = it->image_id;
5610 p->u.image.slice = it->slice;
5611 break;
5612 case GET_FROM_STRETCH:
5613 p->u.stretch.object = it->object;
5614 break;
5615 }
5616 p->position = position ? *position : it->position;
5617 p->current = it->current;
5618 p->end_charpos = it->end_charpos;
5619 p->string_nchars = it->string_nchars;
5620 p->area = it->area;
5621 p->multibyte_p = it->multibyte_p;
5622 p->avoid_cursor_p = it->avoid_cursor_p;
5623 p->space_width = it->space_width;
5624 p->font_height = it->font_height;
5625 p->voffset = it->voffset;
5626 p->string_from_display_prop_p = it->string_from_display_prop_p;
5627 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5628 p->display_ellipsis_p = 0;
5629 p->line_wrap = it->line_wrap;
5630 p->bidi_p = it->bidi_p;
5631 p->paragraph_embedding = it->paragraph_embedding;
5632 p->from_disp_prop_p = it->from_disp_prop_p;
5633 ++it->sp;
5634
5635 /* Save the state of the bidi iterator as well. */
5636 if (it->bidi_p)
5637 bidi_push_it (&it->bidi_it);
5638 }
5639
5640 static void
5641 iterate_out_of_display_property (struct it *it)
5642 {
5643 int buffer_p = !STRINGP (it->string);
5644 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5645 EMACS_INT bob = (buffer_p ? BEGV : 0);
5646
5647 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5648
5649 /* Maybe initialize paragraph direction. If we are at the beginning
5650 of a new paragraph, next_element_from_buffer may not have a
5651 chance to do that. */
5652 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5653 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5654 /* prev_stop can be zero, so check against BEGV as well. */
5655 while (it->bidi_it.charpos >= bob
5656 && it->prev_stop <= it->bidi_it.charpos
5657 && it->bidi_it.charpos < CHARPOS (it->position)
5658 && it->bidi_it.charpos < eob)
5659 bidi_move_to_visually_next (&it->bidi_it);
5660 /* Record the stop_pos we just crossed, for when we cross it
5661 back, maybe. */
5662 if (it->bidi_it.charpos > CHARPOS (it->position))
5663 it->prev_stop = CHARPOS (it->position);
5664 /* If we ended up not where pop_it put us, resync IT's
5665 positional members with the bidi iterator. */
5666 if (it->bidi_it.charpos != CHARPOS (it->position))
5667 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5668 if (buffer_p)
5669 it->current.pos = it->position;
5670 else
5671 it->current.string_pos = it->position;
5672 }
5673
5674 /* Restore IT's settings from IT->stack. Called, for example, when no
5675 more overlay strings must be processed, and we return to delivering
5676 display elements from a buffer, or when the end of a string from a
5677 `display' property is reached and we return to delivering display
5678 elements from an overlay string, or from a buffer. */
5679
5680 static void
5681 pop_it (struct it *it)
5682 {
5683 struct iterator_stack_entry *p;
5684 int from_display_prop = it->from_disp_prop_p;
5685
5686 xassert (it->sp > 0);
5687 --it->sp;
5688 p = it->stack + it->sp;
5689 it->stop_charpos = p->stop_charpos;
5690 it->prev_stop = p->prev_stop;
5691 it->base_level_stop = p->base_level_stop;
5692 it->cmp_it = p->cmp_it;
5693 it->face_id = p->face_id;
5694 it->current = p->current;
5695 it->position = p->position;
5696 it->string = p->string;
5697 it->from_overlay = p->from_overlay;
5698 if (NILP (it->string))
5699 SET_TEXT_POS (it->current.string_pos, -1, -1);
5700 it->method = p->method;
5701 switch (it->method)
5702 {
5703 case GET_FROM_IMAGE:
5704 it->image_id = p->u.image.image_id;
5705 it->object = p->u.image.object;
5706 it->slice = p->u.image.slice;
5707 break;
5708 case GET_FROM_STRETCH:
5709 it->object = p->u.stretch.object;
5710 break;
5711 case GET_FROM_BUFFER:
5712 it->object = it->w->buffer;
5713 break;
5714 case GET_FROM_STRING:
5715 it->object = it->string;
5716 break;
5717 case GET_FROM_DISPLAY_VECTOR:
5718 if (it->s)
5719 it->method = GET_FROM_C_STRING;
5720 else if (STRINGP (it->string))
5721 it->method = GET_FROM_STRING;
5722 else
5723 {
5724 it->method = GET_FROM_BUFFER;
5725 it->object = it->w->buffer;
5726 }
5727 }
5728 it->end_charpos = p->end_charpos;
5729 it->string_nchars = p->string_nchars;
5730 it->area = p->area;
5731 it->multibyte_p = p->multibyte_p;
5732 it->avoid_cursor_p = p->avoid_cursor_p;
5733 it->space_width = p->space_width;
5734 it->font_height = p->font_height;
5735 it->voffset = p->voffset;
5736 it->string_from_display_prop_p = p->string_from_display_prop_p;
5737 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5738 it->line_wrap = p->line_wrap;
5739 it->bidi_p = p->bidi_p;
5740 it->paragraph_embedding = p->paragraph_embedding;
5741 it->from_disp_prop_p = p->from_disp_prop_p;
5742 if (it->bidi_p)
5743 {
5744 bidi_pop_it (&it->bidi_it);
5745 /* Bidi-iterate until we get out of the portion of text, if any,
5746 covered by a `display' text property or by an overlay with
5747 `display' property. (We cannot just jump there, because the
5748 internal coherency of the bidi iterator state can not be
5749 preserved across such jumps.) We also must determine the
5750 paragraph base direction if the overlay we just processed is
5751 at the beginning of a new paragraph. */
5752 if (from_display_prop
5753 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5754 iterate_out_of_display_property (it);
5755
5756 xassert ((BUFFERP (it->object)
5757 && IT_CHARPOS (*it) == it->bidi_it.charpos
5758 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5759 || (STRINGP (it->object)
5760 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5761 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5762 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5763 }
5764 }
5765
5766
5767 \f
5768 /***********************************************************************
5769 Moving over lines
5770 ***********************************************************************/
5771
5772 /* Set IT's current position to the previous line start. */
5773
5774 static void
5775 back_to_previous_line_start (struct it *it)
5776 {
5777 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5778 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5779 }
5780
5781
5782 /* Move IT to the next line start.
5783
5784 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5785 we skipped over part of the text (as opposed to moving the iterator
5786 continuously over the text). Otherwise, don't change the value
5787 of *SKIPPED_P.
5788
5789 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5790 iterator on the newline, if it was found.
5791
5792 Newlines may come from buffer text, overlay strings, or strings
5793 displayed via the `display' property. That's the reason we can't
5794 simply use find_next_newline_no_quit.
5795
5796 Note that this function may not skip over invisible text that is so
5797 because of text properties and immediately follows a newline. If
5798 it would, function reseat_at_next_visible_line_start, when called
5799 from set_iterator_to_next, would effectively make invisible
5800 characters following a newline part of the wrong glyph row, which
5801 leads to wrong cursor motion. */
5802
5803 static int
5804 forward_to_next_line_start (struct it *it, int *skipped_p,
5805 struct bidi_it *bidi_it_prev)
5806 {
5807 EMACS_INT old_selective;
5808 int newline_found_p, n;
5809 const int MAX_NEWLINE_DISTANCE = 500;
5810
5811 /* If already on a newline, just consume it to avoid unintended
5812 skipping over invisible text below. */
5813 if (it->what == IT_CHARACTER
5814 && it->c == '\n'
5815 && CHARPOS (it->position) == IT_CHARPOS (*it))
5816 {
5817 if (it->bidi_p && bidi_it_prev)
5818 *bidi_it_prev = it->bidi_it;
5819 set_iterator_to_next (it, 0);
5820 it->c = 0;
5821 return 1;
5822 }
5823
5824 /* Don't handle selective display in the following. It's (a)
5825 unnecessary because it's done by the caller, and (b) leads to an
5826 infinite recursion because next_element_from_ellipsis indirectly
5827 calls this function. */
5828 old_selective = it->selective;
5829 it->selective = 0;
5830
5831 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5832 from buffer text. */
5833 for (n = newline_found_p = 0;
5834 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5835 n += STRINGP (it->string) ? 0 : 1)
5836 {
5837 if (!get_next_display_element (it))
5838 return 0;
5839 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5840 if (newline_found_p && it->bidi_p && bidi_it_prev)
5841 *bidi_it_prev = it->bidi_it;
5842 set_iterator_to_next (it, 0);
5843 }
5844
5845 /* If we didn't find a newline near enough, see if we can use a
5846 short-cut. */
5847 if (!newline_found_p)
5848 {
5849 EMACS_INT start = IT_CHARPOS (*it);
5850 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5851 Lisp_Object pos;
5852
5853 xassert (!STRINGP (it->string));
5854
5855 /* If there isn't any `display' property in sight, and no
5856 overlays, we can just use the position of the newline in
5857 buffer text. */
5858 if (it->stop_charpos >= limit
5859 || ((pos = Fnext_single_property_change (make_number (start),
5860 Qdisplay, Qnil,
5861 make_number (limit)),
5862 NILP (pos))
5863 && next_overlay_change (start) == ZV))
5864 {
5865 if (!it->bidi_p)
5866 {
5867 IT_CHARPOS (*it) = limit;
5868 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5869 }
5870 else
5871 {
5872 struct bidi_it bprev;
5873
5874 /* Help bidi.c avoid expensive searches for display
5875 properties and overlays, by telling it that there are
5876 none up to `limit'. */
5877 if (it->bidi_it.disp_pos < limit)
5878 {
5879 it->bidi_it.disp_pos = limit;
5880 it->bidi_it.disp_prop = 0;
5881 }
5882 do {
5883 bprev = it->bidi_it;
5884 bidi_move_to_visually_next (&it->bidi_it);
5885 } while (it->bidi_it.charpos != limit);
5886 IT_CHARPOS (*it) = limit;
5887 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5888 if (bidi_it_prev)
5889 *bidi_it_prev = bprev;
5890 }
5891 *skipped_p = newline_found_p = 1;
5892 }
5893 else
5894 {
5895 while (get_next_display_element (it)
5896 && !newline_found_p)
5897 {
5898 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5899 if (newline_found_p && it->bidi_p && bidi_it_prev)
5900 *bidi_it_prev = it->bidi_it;
5901 set_iterator_to_next (it, 0);
5902 }
5903 }
5904 }
5905
5906 it->selective = old_selective;
5907 return newline_found_p;
5908 }
5909
5910
5911 /* Set IT's current position to the previous visible line start. Skip
5912 invisible text that is so either due to text properties or due to
5913 selective display. Caution: this does not change IT->current_x and
5914 IT->hpos. */
5915
5916 static void
5917 back_to_previous_visible_line_start (struct it *it)
5918 {
5919 while (IT_CHARPOS (*it) > BEGV)
5920 {
5921 back_to_previous_line_start (it);
5922
5923 if (IT_CHARPOS (*it) <= BEGV)
5924 break;
5925
5926 /* If selective > 0, then lines indented more than its value are
5927 invisible. */
5928 if (it->selective > 0
5929 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5930 it->selective))
5931 continue;
5932
5933 /* Check the newline before point for invisibility. */
5934 {
5935 Lisp_Object prop;
5936 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5937 Qinvisible, it->window);
5938 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5939 continue;
5940 }
5941
5942 if (IT_CHARPOS (*it) <= BEGV)
5943 break;
5944
5945 {
5946 struct it it2;
5947 void *it2data = NULL;
5948 EMACS_INT pos;
5949 EMACS_INT beg, end;
5950 Lisp_Object val, overlay;
5951
5952 SAVE_IT (it2, *it, it2data);
5953
5954 /* If newline is part of a composition, continue from start of composition */
5955 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5956 && beg < IT_CHARPOS (*it))
5957 goto replaced;
5958
5959 /* If newline is replaced by a display property, find start of overlay
5960 or interval and continue search from that point. */
5961 pos = --IT_CHARPOS (it2);
5962 --IT_BYTEPOS (it2);
5963 it2.sp = 0;
5964 bidi_unshelve_cache (NULL, 0);
5965 it2.string_from_display_prop_p = 0;
5966 it2.from_disp_prop_p = 0;
5967 if (handle_display_prop (&it2) == HANDLED_RETURN
5968 && !NILP (val = get_char_property_and_overlay
5969 (make_number (pos), Qdisplay, Qnil, &overlay))
5970 && (OVERLAYP (overlay)
5971 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5972 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5973 {
5974 RESTORE_IT (it, it, it2data);
5975 goto replaced;
5976 }
5977
5978 /* Newline is not replaced by anything -- so we are done. */
5979 RESTORE_IT (it, it, it2data);
5980 break;
5981
5982 replaced:
5983 if (beg < BEGV)
5984 beg = BEGV;
5985 IT_CHARPOS (*it) = beg;
5986 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5987 }
5988 }
5989
5990 it->continuation_lines_width = 0;
5991
5992 xassert (IT_CHARPOS (*it) >= BEGV);
5993 xassert (IT_CHARPOS (*it) == BEGV
5994 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5995 CHECK_IT (it);
5996 }
5997
5998
5999 /* Reseat iterator IT at the previous visible line start. Skip
6000 invisible text that is so either due to text properties or due to
6001 selective display. At the end, update IT's overlay information,
6002 face information etc. */
6003
6004 void
6005 reseat_at_previous_visible_line_start (struct it *it)
6006 {
6007 back_to_previous_visible_line_start (it);
6008 reseat (it, it->current.pos, 1);
6009 CHECK_IT (it);
6010 }
6011
6012
6013 /* Reseat iterator IT on the next visible line start in the current
6014 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6015 preceding the line start. Skip over invisible text that is so
6016 because of selective display. Compute faces, overlays etc at the
6017 new position. Note that this function does not skip over text that
6018 is invisible because of text properties. */
6019
6020 static void
6021 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6022 {
6023 int newline_found_p, skipped_p = 0;
6024 struct bidi_it bidi_it_prev;
6025
6026 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6027
6028 /* Skip over lines that are invisible because they are indented
6029 more than the value of IT->selective. */
6030 if (it->selective > 0)
6031 while (IT_CHARPOS (*it) < ZV
6032 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6033 it->selective))
6034 {
6035 xassert (IT_BYTEPOS (*it) == BEGV
6036 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6037 newline_found_p =
6038 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6039 }
6040
6041 /* Position on the newline if that's what's requested. */
6042 if (on_newline_p && newline_found_p)
6043 {
6044 if (STRINGP (it->string))
6045 {
6046 if (IT_STRING_CHARPOS (*it) > 0)
6047 {
6048 if (!it->bidi_p)
6049 {
6050 --IT_STRING_CHARPOS (*it);
6051 --IT_STRING_BYTEPOS (*it);
6052 }
6053 else
6054 {
6055 /* We need to restore the bidi iterator to the state
6056 it had on the newline, and resync the IT's
6057 position with that. */
6058 it->bidi_it = bidi_it_prev;
6059 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6060 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6061 }
6062 }
6063 }
6064 else if (IT_CHARPOS (*it) > BEGV)
6065 {
6066 if (!it->bidi_p)
6067 {
6068 --IT_CHARPOS (*it);
6069 --IT_BYTEPOS (*it);
6070 }
6071 else
6072 {
6073 /* We need to restore the bidi iterator to the state it
6074 had on the newline and resync IT with that. */
6075 it->bidi_it = bidi_it_prev;
6076 IT_CHARPOS (*it) = it->bidi_it.charpos;
6077 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6078 }
6079 reseat (it, it->current.pos, 0);
6080 }
6081 }
6082 else if (skipped_p)
6083 reseat (it, it->current.pos, 0);
6084
6085 CHECK_IT (it);
6086 }
6087
6088
6089 \f
6090 /***********************************************************************
6091 Changing an iterator's position
6092 ***********************************************************************/
6093
6094 /* Change IT's current position to POS in current_buffer. If FORCE_P
6095 is non-zero, always check for text properties at the new position.
6096 Otherwise, text properties are only looked up if POS >=
6097 IT->check_charpos of a property. */
6098
6099 static void
6100 reseat (struct it *it, struct text_pos pos, int force_p)
6101 {
6102 EMACS_INT original_pos = IT_CHARPOS (*it);
6103
6104 reseat_1 (it, pos, 0);
6105
6106 /* Determine where to check text properties. Avoid doing it
6107 where possible because text property lookup is very expensive. */
6108 if (force_p
6109 || CHARPOS (pos) > it->stop_charpos
6110 || CHARPOS (pos) < original_pos)
6111 {
6112 if (it->bidi_p)
6113 {
6114 /* For bidi iteration, we need to prime prev_stop and
6115 base_level_stop with our best estimations. */
6116 /* Implementation note: Of course, POS is not necessarily a
6117 stop position, so assigning prev_pos to it is a lie; we
6118 should have called compute_stop_backwards. However, if
6119 the current buffer does not include any R2L characters,
6120 that call would be a waste of cycles, because the
6121 iterator will never move back, and thus never cross this
6122 "fake" stop position. So we delay that backward search
6123 until the time we really need it, in next_element_from_buffer. */
6124 if (CHARPOS (pos) != it->prev_stop)
6125 it->prev_stop = CHARPOS (pos);
6126 if (CHARPOS (pos) < it->base_level_stop)
6127 it->base_level_stop = 0; /* meaning it's unknown */
6128 handle_stop (it);
6129 }
6130 else
6131 {
6132 handle_stop (it);
6133 it->prev_stop = it->base_level_stop = 0;
6134 }
6135
6136 }
6137
6138 CHECK_IT (it);
6139 }
6140
6141
6142 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6143 IT->stop_pos to POS, also. */
6144
6145 static void
6146 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6147 {
6148 /* Don't call this function when scanning a C string. */
6149 xassert (it->s == NULL);
6150
6151 /* POS must be a reasonable value. */
6152 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6153
6154 it->current.pos = it->position = pos;
6155 it->end_charpos = ZV;
6156 it->dpvec = NULL;
6157 it->current.dpvec_index = -1;
6158 it->current.overlay_string_index = -1;
6159 IT_STRING_CHARPOS (*it) = -1;
6160 IT_STRING_BYTEPOS (*it) = -1;
6161 it->string = Qnil;
6162 it->method = GET_FROM_BUFFER;
6163 it->object = it->w->buffer;
6164 it->area = TEXT_AREA;
6165 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6166 it->sp = 0;
6167 it->string_from_display_prop_p = 0;
6168 it->string_from_prefix_prop_p = 0;
6169
6170 it->from_disp_prop_p = 0;
6171 it->face_before_selective_p = 0;
6172 if (it->bidi_p)
6173 {
6174 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6175 &it->bidi_it);
6176 bidi_unshelve_cache (NULL, 0);
6177 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6178 it->bidi_it.string.s = NULL;
6179 it->bidi_it.string.lstring = Qnil;
6180 it->bidi_it.string.bufpos = 0;
6181 it->bidi_it.string.unibyte = 0;
6182 }
6183
6184 if (set_stop_p)
6185 {
6186 it->stop_charpos = CHARPOS (pos);
6187 it->base_level_stop = CHARPOS (pos);
6188 }
6189 }
6190
6191
6192 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6193 If S is non-null, it is a C string to iterate over. Otherwise,
6194 STRING gives a Lisp string to iterate over.
6195
6196 If PRECISION > 0, don't return more then PRECISION number of
6197 characters from the string.
6198
6199 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6200 characters have been returned. FIELD_WIDTH < 0 means an infinite
6201 field width.
6202
6203 MULTIBYTE = 0 means disable processing of multibyte characters,
6204 MULTIBYTE > 0 means enable it,
6205 MULTIBYTE < 0 means use IT->multibyte_p.
6206
6207 IT must be initialized via a prior call to init_iterator before
6208 calling this function. */
6209
6210 static void
6211 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6212 EMACS_INT charpos, EMACS_INT precision, int field_width,
6213 int multibyte)
6214 {
6215 /* No region in strings. */
6216 it->region_beg_charpos = it->region_end_charpos = -1;
6217
6218 /* No text property checks performed by default, but see below. */
6219 it->stop_charpos = -1;
6220
6221 /* Set iterator position and end position. */
6222 memset (&it->current, 0, sizeof it->current);
6223 it->current.overlay_string_index = -1;
6224 it->current.dpvec_index = -1;
6225 xassert (charpos >= 0);
6226
6227 /* If STRING is specified, use its multibyteness, otherwise use the
6228 setting of MULTIBYTE, if specified. */
6229 if (multibyte >= 0)
6230 it->multibyte_p = multibyte > 0;
6231
6232 /* Bidirectional reordering of strings is controlled by the default
6233 value of bidi-display-reordering. Don't try to reorder while
6234 loading loadup.el, as the necessary character property tables are
6235 not yet available. */
6236 it->bidi_p =
6237 NILP (Vpurify_flag)
6238 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6239
6240 if (s == NULL)
6241 {
6242 xassert (STRINGP (string));
6243 it->string = string;
6244 it->s = NULL;
6245 it->end_charpos = it->string_nchars = SCHARS (string);
6246 it->method = GET_FROM_STRING;
6247 it->current.string_pos = string_pos (charpos, string);
6248
6249 if (it->bidi_p)
6250 {
6251 it->bidi_it.string.lstring = string;
6252 it->bidi_it.string.s = NULL;
6253 it->bidi_it.string.schars = it->end_charpos;
6254 it->bidi_it.string.bufpos = 0;
6255 it->bidi_it.string.from_disp_str = 0;
6256 it->bidi_it.string.unibyte = !it->multibyte_p;
6257 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6258 FRAME_WINDOW_P (it->f), &it->bidi_it);
6259 }
6260 }
6261 else
6262 {
6263 it->s = (const unsigned char *) s;
6264 it->string = Qnil;
6265
6266 /* Note that we use IT->current.pos, not it->current.string_pos,
6267 for displaying C strings. */
6268 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6269 if (it->multibyte_p)
6270 {
6271 it->current.pos = c_string_pos (charpos, s, 1);
6272 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6273 }
6274 else
6275 {
6276 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6277 it->end_charpos = it->string_nchars = strlen (s);
6278 }
6279
6280 if (it->bidi_p)
6281 {
6282 it->bidi_it.string.lstring = Qnil;
6283 it->bidi_it.string.s = (const unsigned char *) s;
6284 it->bidi_it.string.schars = it->end_charpos;
6285 it->bidi_it.string.bufpos = 0;
6286 it->bidi_it.string.from_disp_str = 0;
6287 it->bidi_it.string.unibyte = !it->multibyte_p;
6288 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6289 &it->bidi_it);
6290 }
6291 it->method = GET_FROM_C_STRING;
6292 }
6293
6294 /* PRECISION > 0 means don't return more than PRECISION characters
6295 from the string. */
6296 if (precision > 0 && it->end_charpos - charpos > precision)
6297 {
6298 it->end_charpos = it->string_nchars = charpos + precision;
6299 if (it->bidi_p)
6300 it->bidi_it.string.schars = it->end_charpos;
6301 }
6302
6303 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6304 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6305 FIELD_WIDTH < 0 means infinite field width. This is useful for
6306 padding with `-' at the end of a mode line. */
6307 if (field_width < 0)
6308 field_width = INFINITY;
6309 /* Implementation note: We deliberately don't enlarge
6310 it->bidi_it.string.schars here to fit it->end_charpos, because
6311 the bidi iterator cannot produce characters out of thin air. */
6312 if (field_width > it->end_charpos - charpos)
6313 it->end_charpos = charpos + field_width;
6314
6315 /* Use the standard display table for displaying strings. */
6316 if (DISP_TABLE_P (Vstandard_display_table))
6317 it->dp = XCHAR_TABLE (Vstandard_display_table);
6318
6319 it->stop_charpos = charpos;
6320 it->prev_stop = charpos;
6321 it->base_level_stop = 0;
6322 if (it->bidi_p)
6323 {
6324 it->bidi_it.first_elt = 1;
6325 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6326 it->bidi_it.disp_pos = -1;
6327 }
6328 if (s == NULL && it->multibyte_p)
6329 {
6330 EMACS_INT endpos = SCHARS (it->string);
6331 if (endpos > it->end_charpos)
6332 endpos = it->end_charpos;
6333 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6334 it->string);
6335 }
6336 CHECK_IT (it);
6337 }
6338
6339
6340 \f
6341 /***********************************************************************
6342 Iteration
6343 ***********************************************************************/
6344
6345 /* Map enum it_method value to corresponding next_element_from_* function. */
6346
6347 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6348 {
6349 next_element_from_buffer,
6350 next_element_from_display_vector,
6351 next_element_from_string,
6352 next_element_from_c_string,
6353 next_element_from_image,
6354 next_element_from_stretch
6355 };
6356
6357 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6358
6359
6360 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6361 (possibly with the following characters). */
6362
6363 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6364 ((IT)->cmp_it.id >= 0 \
6365 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6366 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6367 END_CHARPOS, (IT)->w, \
6368 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6369 (IT)->string)))
6370
6371
6372 /* Lookup the char-table Vglyphless_char_display for character C (-1
6373 if we want information for no-font case), and return the display
6374 method symbol. By side-effect, update it->what and
6375 it->glyphless_method. This function is called from
6376 get_next_display_element for each character element, and from
6377 x_produce_glyphs when no suitable font was found. */
6378
6379 Lisp_Object
6380 lookup_glyphless_char_display (int c, struct it *it)
6381 {
6382 Lisp_Object glyphless_method = Qnil;
6383
6384 if (CHAR_TABLE_P (Vglyphless_char_display)
6385 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6386 {
6387 if (c >= 0)
6388 {
6389 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6390 if (CONSP (glyphless_method))
6391 glyphless_method = FRAME_WINDOW_P (it->f)
6392 ? XCAR (glyphless_method)
6393 : XCDR (glyphless_method);
6394 }
6395 else
6396 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6397 }
6398
6399 retry:
6400 if (NILP (glyphless_method))
6401 {
6402 if (c >= 0)
6403 /* The default is to display the character by a proper font. */
6404 return Qnil;
6405 /* The default for the no-font case is to display an empty box. */
6406 glyphless_method = Qempty_box;
6407 }
6408 if (EQ (glyphless_method, Qzero_width))
6409 {
6410 if (c >= 0)
6411 return glyphless_method;
6412 /* This method can't be used for the no-font case. */
6413 glyphless_method = Qempty_box;
6414 }
6415 if (EQ (glyphless_method, Qthin_space))
6416 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6417 else if (EQ (glyphless_method, Qempty_box))
6418 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6419 else if (EQ (glyphless_method, Qhex_code))
6420 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6421 else if (STRINGP (glyphless_method))
6422 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6423 else
6424 {
6425 /* Invalid value. We use the default method. */
6426 glyphless_method = Qnil;
6427 goto retry;
6428 }
6429 it->what = IT_GLYPHLESS;
6430 return glyphless_method;
6431 }
6432
6433 /* Load IT's display element fields with information about the next
6434 display element from the current position of IT. Value is zero if
6435 end of buffer (or C string) is reached. */
6436
6437 static struct frame *last_escape_glyph_frame = NULL;
6438 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6439 static int last_escape_glyph_merged_face_id = 0;
6440
6441 struct frame *last_glyphless_glyph_frame = NULL;
6442 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6443 int last_glyphless_glyph_merged_face_id = 0;
6444
6445 static int
6446 get_next_display_element (struct it *it)
6447 {
6448 /* Non-zero means that we found a display element. Zero means that
6449 we hit the end of what we iterate over. Performance note: the
6450 function pointer `method' used here turns out to be faster than
6451 using a sequence of if-statements. */
6452 int success_p;
6453
6454 get_next:
6455 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6456
6457 if (it->what == IT_CHARACTER)
6458 {
6459 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6460 and only if (a) the resolved directionality of that character
6461 is R..." */
6462 /* FIXME: Do we need an exception for characters from display
6463 tables? */
6464 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6465 it->c = bidi_mirror_char (it->c);
6466 /* Map via display table or translate control characters.
6467 IT->c, IT->len etc. have been set to the next character by
6468 the function call above. If we have a display table, and it
6469 contains an entry for IT->c, translate it. Don't do this if
6470 IT->c itself comes from a display table, otherwise we could
6471 end up in an infinite recursion. (An alternative could be to
6472 count the recursion depth of this function and signal an
6473 error when a certain maximum depth is reached.) Is it worth
6474 it? */
6475 if (success_p && it->dpvec == NULL)
6476 {
6477 Lisp_Object dv;
6478 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6479 int nonascii_space_p = 0;
6480 int nonascii_hyphen_p = 0;
6481 int c = it->c; /* This is the character to display. */
6482
6483 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6484 {
6485 xassert (SINGLE_BYTE_CHAR_P (c));
6486 if (unibyte_display_via_language_environment)
6487 {
6488 c = DECODE_CHAR (unibyte, c);
6489 if (c < 0)
6490 c = BYTE8_TO_CHAR (it->c);
6491 }
6492 else
6493 c = BYTE8_TO_CHAR (it->c);
6494 }
6495
6496 if (it->dp
6497 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6498 VECTORP (dv)))
6499 {
6500 struct Lisp_Vector *v = XVECTOR (dv);
6501
6502 /* Return the first character from the display table
6503 entry, if not empty. If empty, don't display the
6504 current character. */
6505 if (v->header.size)
6506 {
6507 it->dpvec_char_len = it->len;
6508 it->dpvec = v->contents;
6509 it->dpend = v->contents + v->header.size;
6510 it->current.dpvec_index = 0;
6511 it->dpvec_face_id = -1;
6512 it->saved_face_id = it->face_id;
6513 it->method = GET_FROM_DISPLAY_VECTOR;
6514 it->ellipsis_p = 0;
6515 }
6516 else
6517 {
6518 set_iterator_to_next (it, 0);
6519 }
6520 goto get_next;
6521 }
6522
6523 if (! NILP (lookup_glyphless_char_display (c, it)))
6524 {
6525 if (it->what == IT_GLYPHLESS)
6526 goto done;
6527 /* Don't display this character. */
6528 set_iterator_to_next (it, 0);
6529 goto get_next;
6530 }
6531
6532 /* If `nobreak-char-display' is non-nil, we display
6533 non-ASCII spaces and hyphens specially. */
6534 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6535 {
6536 if (c == 0xA0)
6537 nonascii_space_p = 1;
6538 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6539 nonascii_hyphen_p = 1;
6540 }
6541
6542 /* Translate control characters into `\003' or `^C' form.
6543 Control characters coming from a display table entry are
6544 currently not translated because we use IT->dpvec to hold
6545 the translation. This could easily be changed but I
6546 don't believe that it is worth doing.
6547
6548 The characters handled by `nobreak-char-display' must be
6549 translated too.
6550
6551 Non-printable characters and raw-byte characters are also
6552 translated to octal form. */
6553 if (((c < ' ' || c == 127) /* ASCII control chars */
6554 ? (it->area != TEXT_AREA
6555 /* In mode line, treat \n, \t like other crl chars. */
6556 || (c != '\t'
6557 && it->glyph_row
6558 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6559 || (c != '\n' && c != '\t'))
6560 : (nonascii_space_p
6561 || nonascii_hyphen_p
6562 || CHAR_BYTE8_P (c)
6563 || ! CHAR_PRINTABLE_P (c))))
6564 {
6565 /* C is a control character, non-ASCII space/hyphen,
6566 raw-byte, or a non-printable character which must be
6567 displayed either as '\003' or as `^C' where the '\\'
6568 and '^' can be defined in the display table. Fill
6569 IT->ctl_chars with glyphs for what we have to
6570 display. Then, set IT->dpvec to these glyphs. */
6571 Lisp_Object gc;
6572 int ctl_len;
6573 int face_id;
6574 EMACS_INT lface_id = 0;
6575 int escape_glyph;
6576
6577 /* Handle control characters with ^. */
6578
6579 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6580 {
6581 int g;
6582
6583 g = '^'; /* default glyph for Control */
6584 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6585 if (it->dp
6586 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6587 && GLYPH_CODE_CHAR_VALID_P (gc))
6588 {
6589 g = GLYPH_CODE_CHAR (gc);
6590 lface_id = GLYPH_CODE_FACE (gc);
6591 }
6592 if (lface_id)
6593 {
6594 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6595 }
6596 else if (it->f == last_escape_glyph_frame
6597 && it->face_id == last_escape_glyph_face_id)
6598 {
6599 face_id = last_escape_glyph_merged_face_id;
6600 }
6601 else
6602 {
6603 /* Merge the escape-glyph face into the current face. */
6604 face_id = merge_faces (it->f, Qescape_glyph, 0,
6605 it->face_id);
6606 last_escape_glyph_frame = it->f;
6607 last_escape_glyph_face_id = it->face_id;
6608 last_escape_glyph_merged_face_id = face_id;
6609 }
6610
6611 XSETINT (it->ctl_chars[0], g);
6612 XSETINT (it->ctl_chars[1], c ^ 0100);
6613 ctl_len = 2;
6614 goto display_control;
6615 }
6616
6617 /* Handle non-ascii space in the mode where it only gets
6618 highlighting. */
6619
6620 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6621 {
6622 /* Merge `nobreak-space' into the current face. */
6623 face_id = merge_faces (it->f, Qnobreak_space, 0,
6624 it->face_id);
6625 XSETINT (it->ctl_chars[0], ' ');
6626 ctl_len = 1;
6627 goto display_control;
6628 }
6629
6630 /* Handle sequences that start with the "escape glyph". */
6631
6632 /* the default escape glyph is \. */
6633 escape_glyph = '\\';
6634
6635 if (it->dp
6636 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6637 && GLYPH_CODE_CHAR_VALID_P (gc))
6638 {
6639 escape_glyph = GLYPH_CODE_CHAR (gc);
6640 lface_id = GLYPH_CODE_FACE (gc);
6641 }
6642 if (lface_id)
6643 {
6644 /* The display table specified a face.
6645 Merge it into face_id and also into escape_glyph. */
6646 face_id = merge_faces (it->f, Qt, lface_id,
6647 it->face_id);
6648 }
6649 else if (it->f == last_escape_glyph_frame
6650 && it->face_id == last_escape_glyph_face_id)
6651 {
6652 face_id = last_escape_glyph_merged_face_id;
6653 }
6654 else
6655 {
6656 /* Merge the escape-glyph face into the current face. */
6657 face_id = merge_faces (it->f, Qescape_glyph, 0,
6658 it->face_id);
6659 last_escape_glyph_frame = it->f;
6660 last_escape_glyph_face_id = it->face_id;
6661 last_escape_glyph_merged_face_id = face_id;
6662 }
6663
6664 /* Draw non-ASCII hyphen with just highlighting: */
6665
6666 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6667 {
6668 XSETINT (it->ctl_chars[0], '-');
6669 ctl_len = 1;
6670 goto display_control;
6671 }
6672
6673 /* Draw non-ASCII space/hyphen with escape glyph: */
6674
6675 if (nonascii_space_p || nonascii_hyphen_p)
6676 {
6677 XSETINT (it->ctl_chars[0], escape_glyph);
6678 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6679 ctl_len = 2;
6680 goto display_control;
6681 }
6682
6683 {
6684 char str[10];
6685 int len, i;
6686
6687 if (CHAR_BYTE8_P (c))
6688 /* Display \200 instead of \17777600. */
6689 c = CHAR_TO_BYTE8 (c);
6690 len = sprintf (str, "%03o", c);
6691
6692 XSETINT (it->ctl_chars[0], escape_glyph);
6693 for (i = 0; i < len; i++)
6694 XSETINT (it->ctl_chars[i + 1], str[i]);
6695 ctl_len = len + 1;
6696 }
6697
6698 display_control:
6699 /* Set up IT->dpvec and return first character from it. */
6700 it->dpvec_char_len = it->len;
6701 it->dpvec = it->ctl_chars;
6702 it->dpend = it->dpvec + ctl_len;
6703 it->current.dpvec_index = 0;
6704 it->dpvec_face_id = face_id;
6705 it->saved_face_id = it->face_id;
6706 it->method = GET_FROM_DISPLAY_VECTOR;
6707 it->ellipsis_p = 0;
6708 goto get_next;
6709 }
6710 it->char_to_display = c;
6711 }
6712 else if (success_p)
6713 {
6714 it->char_to_display = it->c;
6715 }
6716 }
6717
6718 /* Adjust face id for a multibyte character. There are no multibyte
6719 character in unibyte text. */
6720 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6721 && it->multibyte_p
6722 && success_p
6723 && FRAME_WINDOW_P (it->f))
6724 {
6725 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6726
6727 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6728 {
6729 /* Automatic composition with glyph-string. */
6730 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6731
6732 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6733 }
6734 else
6735 {
6736 EMACS_INT pos = (it->s ? -1
6737 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6738 : IT_CHARPOS (*it));
6739 int c;
6740
6741 if (it->what == IT_CHARACTER)
6742 c = it->char_to_display;
6743 else
6744 {
6745 struct composition *cmp = composition_table[it->cmp_it.id];
6746 int i;
6747
6748 c = ' ';
6749 for (i = 0; i < cmp->glyph_len; i++)
6750 /* TAB in a composition means display glyphs with
6751 padding space on the left or right. */
6752 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6753 break;
6754 }
6755 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6756 }
6757 }
6758
6759 done:
6760 /* Is this character the last one of a run of characters with
6761 box? If yes, set IT->end_of_box_run_p to 1. */
6762 if (it->face_box_p
6763 && it->s == NULL)
6764 {
6765 if (it->method == GET_FROM_STRING && it->sp)
6766 {
6767 int face_id = underlying_face_id (it);
6768 struct face *face = FACE_FROM_ID (it->f, face_id);
6769
6770 if (face)
6771 {
6772 if (face->box == FACE_NO_BOX)
6773 {
6774 /* If the box comes from face properties in a
6775 display string, check faces in that string. */
6776 int string_face_id = face_after_it_pos (it);
6777 it->end_of_box_run_p
6778 = (FACE_FROM_ID (it->f, string_face_id)->box
6779 == FACE_NO_BOX);
6780 }
6781 /* Otherwise, the box comes from the underlying face.
6782 If this is the last string character displayed, check
6783 the next buffer location. */
6784 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6785 && (it->current.overlay_string_index
6786 == it->n_overlay_strings - 1))
6787 {
6788 EMACS_INT ignore;
6789 int next_face_id;
6790 struct text_pos pos = it->current.pos;
6791 INC_TEXT_POS (pos, it->multibyte_p);
6792
6793 next_face_id = face_at_buffer_position
6794 (it->w, CHARPOS (pos), it->region_beg_charpos,
6795 it->region_end_charpos, &ignore,
6796 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6797 -1);
6798 it->end_of_box_run_p
6799 = (FACE_FROM_ID (it->f, next_face_id)->box
6800 == FACE_NO_BOX);
6801 }
6802 }
6803 }
6804 else
6805 {
6806 int face_id = face_after_it_pos (it);
6807 it->end_of_box_run_p
6808 = (face_id != it->face_id
6809 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6810 }
6811 }
6812 /* If we reached the end of the object we've been iterating (e.g., a
6813 display string or an overlay string), and there's something on
6814 IT->stack, proceed with what's on the stack. It doesn't make
6815 sense to return zero if there's unprocessed stuff on the stack,
6816 because otherwise that stuff will never be displayed. */
6817 if (!success_p && it->sp > 0)
6818 {
6819 set_iterator_to_next (it, 0);
6820 success_p = get_next_display_element (it);
6821 }
6822
6823 /* Value is 0 if end of buffer or string reached. */
6824 return success_p;
6825 }
6826
6827
6828 /* Move IT to the next display element.
6829
6830 RESEAT_P non-zero means if called on a newline in buffer text,
6831 skip to the next visible line start.
6832
6833 Functions get_next_display_element and set_iterator_to_next are
6834 separate because I find this arrangement easier to handle than a
6835 get_next_display_element function that also increments IT's
6836 position. The way it is we can first look at an iterator's current
6837 display element, decide whether it fits on a line, and if it does,
6838 increment the iterator position. The other way around we probably
6839 would either need a flag indicating whether the iterator has to be
6840 incremented the next time, or we would have to implement a
6841 decrement position function which would not be easy to write. */
6842
6843 void
6844 set_iterator_to_next (struct it *it, int reseat_p)
6845 {
6846 /* Reset flags indicating start and end of a sequence of characters
6847 with box. Reset them at the start of this function because
6848 moving the iterator to a new position might set them. */
6849 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6850
6851 switch (it->method)
6852 {
6853 case GET_FROM_BUFFER:
6854 /* The current display element of IT is a character from
6855 current_buffer. Advance in the buffer, and maybe skip over
6856 invisible lines that are so because of selective display. */
6857 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6858 reseat_at_next_visible_line_start (it, 0);
6859 else if (it->cmp_it.id >= 0)
6860 {
6861 /* We are currently getting glyphs from a composition. */
6862 int i;
6863
6864 if (! it->bidi_p)
6865 {
6866 IT_CHARPOS (*it) += it->cmp_it.nchars;
6867 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6868 if (it->cmp_it.to < it->cmp_it.nglyphs)
6869 {
6870 it->cmp_it.from = it->cmp_it.to;
6871 }
6872 else
6873 {
6874 it->cmp_it.id = -1;
6875 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6876 IT_BYTEPOS (*it),
6877 it->end_charpos, Qnil);
6878 }
6879 }
6880 else if (! it->cmp_it.reversed_p)
6881 {
6882 /* Composition created while scanning forward. */
6883 /* Update IT's char/byte positions to point to the first
6884 character of the next grapheme cluster, or to the
6885 character visually after the current composition. */
6886 for (i = 0; i < it->cmp_it.nchars; i++)
6887 bidi_move_to_visually_next (&it->bidi_it);
6888 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6889 IT_CHARPOS (*it) = it->bidi_it.charpos;
6890
6891 if (it->cmp_it.to < it->cmp_it.nglyphs)
6892 {
6893 /* Proceed to the next grapheme cluster. */
6894 it->cmp_it.from = it->cmp_it.to;
6895 }
6896 else
6897 {
6898 /* No more grapheme clusters in this composition.
6899 Find the next stop position. */
6900 EMACS_INT stop = it->end_charpos;
6901 if (it->bidi_it.scan_dir < 0)
6902 /* Now we are scanning backward and don't know
6903 where to stop. */
6904 stop = -1;
6905 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6906 IT_BYTEPOS (*it), stop, Qnil);
6907 }
6908 }
6909 else
6910 {
6911 /* Composition created while scanning backward. */
6912 /* Update IT's char/byte positions to point to the last
6913 character of the previous grapheme cluster, or the
6914 character visually after the current composition. */
6915 for (i = 0; i < it->cmp_it.nchars; i++)
6916 bidi_move_to_visually_next (&it->bidi_it);
6917 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6918 IT_CHARPOS (*it) = it->bidi_it.charpos;
6919 if (it->cmp_it.from > 0)
6920 {
6921 /* Proceed to the previous grapheme cluster. */
6922 it->cmp_it.to = it->cmp_it.from;
6923 }
6924 else
6925 {
6926 /* No more grapheme clusters in this composition.
6927 Find the next stop position. */
6928 EMACS_INT stop = it->end_charpos;
6929 if (it->bidi_it.scan_dir < 0)
6930 /* Now we are scanning backward and don't know
6931 where to stop. */
6932 stop = -1;
6933 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6934 IT_BYTEPOS (*it), stop, Qnil);
6935 }
6936 }
6937 }
6938 else
6939 {
6940 xassert (it->len != 0);
6941
6942 if (!it->bidi_p)
6943 {
6944 IT_BYTEPOS (*it) += it->len;
6945 IT_CHARPOS (*it) += 1;
6946 }
6947 else
6948 {
6949 int prev_scan_dir = it->bidi_it.scan_dir;
6950 /* If this is a new paragraph, determine its base
6951 direction (a.k.a. its base embedding level). */
6952 if (it->bidi_it.new_paragraph)
6953 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6954 bidi_move_to_visually_next (&it->bidi_it);
6955 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6956 IT_CHARPOS (*it) = it->bidi_it.charpos;
6957 if (prev_scan_dir != it->bidi_it.scan_dir)
6958 {
6959 /* As the scan direction was changed, we must
6960 re-compute the stop position for composition. */
6961 EMACS_INT stop = it->end_charpos;
6962 if (it->bidi_it.scan_dir < 0)
6963 stop = -1;
6964 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6965 IT_BYTEPOS (*it), stop, Qnil);
6966 }
6967 }
6968 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6969 }
6970 break;
6971
6972 case GET_FROM_C_STRING:
6973 /* Current display element of IT is from a C string. */
6974 if (!it->bidi_p
6975 /* If the string position is beyond string's end, it means
6976 next_element_from_c_string is padding the string with
6977 blanks, in which case we bypass the bidi iterator,
6978 because it cannot deal with such virtual characters. */
6979 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6980 {
6981 IT_BYTEPOS (*it) += it->len;
6982 IT_CHARPOS (*it) += 1;
6983 }
6984 else
6985 {
6986 bidi_move_to_visually_next (&it->bidi_it);
6987 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6988 IT_CHARPOS (*it) = it->bidi_it.charpos;
6989 }
6990 break;
6991
6992 case GET_FROM_DISPLAY_VECTOR:
6993 /* Current display element of IT is from a display table entry.
6994 Advance in the display table definition. Reset it to null if
6995 end reached, and continue with characters from buffers/
6996 strings. */
6997 ++it->current.dpvec_index;
6998
6999 /* Restore face of the iterator to what they were before the
7000 display vector entry (these entries may contain faces). */
7001 it->face_id = it->saved_face_id;
7002
7003 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7004 {
7005 int recheck_faces = it->ellipsis_p;
7006
7007 if (it->s)
7008 it->method = GET_FROM_C_STRING;
7009 else if (STRINGP (it->string))
7010 it->method = GET_FROM_STRING;
7011 else
7012 {
7013 it->method = GET_FROM_BUFFER;
7014 it->object = it->w->buffer;
7015 }
7016
7017 it->dpvec = NULL;
7018 it->current.dpvec_index = -1;
7019
7020 /* Skip over characters which were displayed via IT->dpvec. */
7021 if (it->dpvec_char_len < 0)
7022 reseat_at_next_visible_line_start (it, 1);
7023 else if (it->dpvec_char_len > 0)
7024 {
7025 if (it->method == GET_FROM_STRING
7026 && it->n_overlay_strings > 0)
7027 it->ignore_overlay_strings_at_pos_p = 1;
7028 it->len = it->dpvec_char_len;
7029 set_iterator_to_next (it, reseat_p);
7030 }
7031
7032 /* Maybe recheck faces after display vector */
7033 if (recheck_faces)
7034 it->stop_charpos = IT_CHARPOS (*it);
7035 }
7036 break;
7037
7038 case GET_FROM_STRING:
7039 /* Current display element is a character from a Lisp string. */
7040 xassert (it->s == NULL && STRINGP (it->string));
7041 /* Don't advance past string end. These conditions are true
7042 when set_iterator_to_next is called at the end of
7043 get_next_display_element, in which case the Lisp string is
7044 already exhausted, and all we want is pop the iterator
7045 stack. */
7046 if (it->current.overlay_string_index >= 0)
7047 {
7048 /* This is an overlay string, so there's no padding with
7049 spaces, and the number of characters in the string is
7050 where the string ends. */
7051 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7052 goto consider_string_end;
7053 }
7054 else
7055 {
7056 /* Not an overlay string. There could be padding, so test
7057 against it->end_charpos . */
7058 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7059 goto consider_string_end;
7060 }
7061 if (it->cmp_it.id >= 0)
7062 {
7063 int i;
7064
7065 if (! it->bidi_p)
7066 {
7067 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7068 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7069 if (it->cmp_it.to < it->cmp_it.nglyphs)
7070 it->cmp_it.from = it->cmp_it.to;
7071 else
7072 {
7073 it->cmp_it.id = -1;
7074 composition_compute_stop_pos (&it->cmp_it,
7075 IT_STRING_CHARPOS (*it),
7076 IT_STRING_BYTEPOS (*it),
7077 it->end_charpos, it->string);
7078 }
7079 }
7080 else if (! it->cmp_it.reversed_p)
7081 {
7082 for (i = 0; i < it->cmp_it.nchars; i++)
7083 bidi_move_to_visually_next (&it->bidi_it);
7084 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7085 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7086
7087 if (it->cmp_it.to < it->cmp_it.nglyphs)
7088 it->cmp_it.from = it->cmp_it.to;
7089 else
7090 {
7091 EMACS_INT stop = it->end_charpos;
7092 if (it->bidi_it.scan_dir < 0)
7093 stop = -1;
7094 composition_compute_stop_pos (&it->cmp_it,
7095 IT_STRING_CHARPOS (*it),
7096 IT_STRING_BYTEPOS (*it), stop,
7097 it->string);
7098 }
7099 }
7100 else
7101 {
7102 for (i = 0; i < it->cmp_it.nchars; i++)
7103 bidi_move_to_visually_next (&it->bidi_it);
7104 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7105 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7106 if (it->cmp_it.from > 0)
7107 it->cmp_it.to = it->cmp_it.from;
7108 else
7109 {
7110 EMACS_INT stop = it->end_charpos;
7111 if (it->bidi_it.scan_dir < 0)
7112 stop = -1;
7113 composition_compute_stop_pos (&it->cmp_it,
7114 IT_STRING_CHARPOS (*it),
7115 IT_STRING_BYTEPOS (*it), stop,
7116 it->string);
7117 }
7118 }
7119 }
7120 else
7121 {
7122 if (!it->bidi_p
7123 /* If the string position is beyond string's end, it
7124 means next_element_from_string is padding the string
7125 with blanks, in which case we bypass the bidi
7126 iterator, because it cannot deal with such virtual
7127 characters. */
7128 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7129 {
7130 IT_STRING_BYTEPOS (*it) += it->len;
7131 IT_STRING_CHARPOS (*it) += 1;
7132 }
7133 else
7134 {
7135 int prev_scan_dir = it->bidi_it.scan_dir;
7136
7137 bidi_move_to_visually_next (&it->bidi_it);
7138 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7139 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7140 if (prev_scan_dir != it->bidi_it.scan_dir)
7141 {
7142 EMACS_INT stop = it->end_charpos;
7143
7144 if (it->bidi_it.scan_dir < 0)
7145 stop = -1;
7146 composition_compute_stop_pos (&it->cmp_it,
7147 IT_STRING_CHARPOS (*it),
7148 IT_STRING_BYTEPOS (*it), stop,
7149 it->string);
7150 }
7151 }
7152 }
7153
7154 consider_string_end:
7155
7156 if (it->current.overlay_string_index >= 0)
7157 {
7158 /* IT->string is an overlay string. Advance to the
7159 next, if there is one. */
7160 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7161 {
7162 it->ellipsis_p = 0;
7163 next_overlay_string (it);
7164 if (it->ellipsis_p)
7165 setup_for_ellipsis (it, 0);
7166 }
7167 }
7168 else
7169 {
7170 /* IT->string is not an overlay string. If we reached
7171 its end, and there is something on IT->stack, proceed
7172 with what is on the stack. This can be either another
7173 string, this time an overlay string, or a buffer. */
7174 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7175 && it->sp > 0)
7176 {
7177 pop_it (it);
7178 if (it->method == GET_FROM_STRING)
7179 goto consider_string_end;
7180 }
7181 }
7182 break;
7183
7184 case GET_FROM_IMAGE:
7185 case GET_FROM_STRETCH:
7186 /* The position etc with which we have to proceed are on
7187 the stack. The position may be at the end of a string,
7188 if the `display' property takes up the whole string. */
7189 xassert (it->sp > 0);
7190 pop_it (it);
7191 if (it->method == GET_FROM_STRING)
7192 goto consider_string_end;
7193 break;
7194
7195 default:
7196 /* There are no other methods defined, so this should be a bug. */
7197 abort ();
7198 }
7199
7200 xassert (it->method != GET_FROM_STRING
7201 || (STRINGP (it->string)
7202 && IT_STRING_CHARPOS (*it) >= 0));
7203 }
7204
7205 /* Load IT's display element fields with information about the next
7206 display element which comes from a display table entry or from the
7207 result of translating a control character to one of the forms `^C'
7208 or `\003'.
7209
7210 IT->dpvec holds the glyphs to return as characters.
7211 IT->saved_face_id holds the face id before the display vector--it
7212 is restored into IT->face_id in set_iterator_to_next. */
7213
7214 static int
7215 next_element_from_display_vector (struct it *it)
7216 {
7217 Lisp_Object gc;
7218
7219 /* Precondition. */
7220 xassert (it->dpvec && it->current.dpvec_index >= 0);
7221
7222 it->face_id = it->saved_face_id;
7223
7224 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7225 That seemed totally bogus - so I changed it... */
7226 gc = it->dpvec[it->current.dpvec_index];
7227
7228 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7229 {
7230 it->c = GLYPH_CODE_CHAR (gc);
7231 it->len = CHAR_BYTES (it->c);
7232
7233 /* The entry may contain a face id to use. Such a face id is
7234 the id of a Lisp face, not a realized face. A face id of
7235 zero means no face is specified. */
7236 if (it->dpvec_face_id >= 0)
7237 it->face_id = it->dpvec_face_id;
7238 else
7239 {
7240 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7241 if (lface_id > 0)
7242 it->face_id = merge_faces (it->f, Qt, lface_id,
7243 it->saved_face_id);
7244 }
7245 }
7246 else
7247 /* Display table entry is invalid. Return a space. */
7248 it->c = ' ', it->len = 1;
7249
7250 /* Don't change position and object of the iterator here. They are
7251 still the values of the character that had this display table
7252 entry or was translated, and that's what we want. */
7253 it->what = IT_CHARACTER;
7254 return 1;
7255 }
7256
7257 /* Get the first element of string/buffer in the visual order, after
7258 being reseated to a new position in a string or a buffer. */
7259 static void
7260 get_visually_first_element (struct it *it)
7261 {
7262 int string_p = STRINGP (it->string) || it->s;
7263 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7264 EMACS_INT bob = (string_p ? 0 : BEGV);
7265
7266 if (STRINGP (it->string))
7267 {
7268 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7269 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7270 }
7271 else
7272 {
7273 it->bidi_it.charpos = IT_CHARPOS (*it);
7274 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7275 }
7276
7277 if (it->bidi_it.charpos == eob)
7278 {
7279 /* Nothing to do, but reset the FIRST_ELT flag, like
7280 bidi_paragraph_init does, because we are not going to
7281 call it. */
7282 it->bidi_it.first_elt = 0;
7283 }
7284 else if (it->bidi_it.charpos == bob
7285 || (!string_p
7286 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7287 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7288 {
7289 /* If we are at the beginning of a line/string, we can produce
7290 the next element right away. */
7291 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7292 bidi_move_to_visually_next (&it->bidi_it);
7293 }
7294 else
7295 {
7296 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7297
7298 /* We need to prime the bidi iterator starting at the line's or
7299 string's beginning, before we will be able to produce the
7300 next element. */
7301 if (string_p)
7302 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7303 else
7304 {
7305 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7306 -1);
7307 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7308 }
7309 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7310 do
7311 {
7312 /* Now return to buffer/string position where we were asked
7313 to get the next display element, and produce that. */
7314 bidi_move_to_visually_next (&it->bidi_it);
7315 }
7316 while (it->bidi_it.bytepos != orig_bytepos
7317 && it->bidi_it.charpos < eob);
7318 }
7319
7320 /* Adjust IT's position information to where we ended up. */
7321 if (STRINGP (it->string))
7322 {
7323 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7324 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7325 }
7326 else
7327 {
7328 IT_CHARPOS (*it) = it->bidi_it.charpos;
7329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7330 }
7331
7332 if (STRINGP (it->string) || !it->s)
7333 {
7334 EMACS_INT stop, charpos, bytepos;
7335
7336 if (STRINGP (it->string))
7337 {
7338 xassert (!it->s);
7339 stop = SCHARS (it->string);
7340 if (stop > it->end_charpos)
7341 stop = it->end_charpos;
7342 charpos = IT_STRING_CHARPOS (*it);
7343 bytepos = IT_STRING_BYTEPOS (*it);
7344 }
7345 else
7346 {
7347 stop = it->end_charpos;
7348 charpos = IT_CHARPOS (*it);
7349 bytepos = IT_BYTEPOS (*it);
7350 }
7351 if (it->bidi_it.scan_dir < 0)
7352 stop = -1;
7353 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7354 it->string);
7355 }
7356 }
7357
7358 /* Load IT with the next display element from Lisp string IT->string.
7359 IT->current.string_pos is the current position within the string.
7360 If IT->current.overlay_string_index >= 0, the Lisp string is an
7361 overlay string. */
7362
7363 static int
7364 next_element_from_string (struct it *it)
7365 {
7366 struct text_pos position;
7367
7368 xassert (STRINGP (it->string));
7369 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7370 xassert (IT_STRING_CHARPOS (*it) >= 0);
7371 position = it->current.string_pos;
7372
7373 /* With bidi reordering, the character to display might not be the
7374 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7375 that we were reseat()ed to a new string, whose paragraph
7376 direction is not known. */
7377 if (it->bidi_p && it->bidi_it.first_elt)
7378 {
7379 get_visually_first_element (it);
7380 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7381 }
7382
7383 /* Time to check for invisible text? */
7384 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7385 {
7386 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7387 {
7388 if (!(!it->bidi_p
7389 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7390 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7391 {
7392 /* With bidi non-linear iteration, we could find
7393 ourselves far beyond the last computed stop_charpos,
7394 with several other stop positions in between that we
7395 missed. Scan them all now, in buffer's logical
7396 order, until we find and handle the last stop_charpos
7397 that precedes our current position. */
7398 handle_stop_backwards (it, it->stop_charpos);
7399 return GET_NEXT_DISPLAY_ELEMENT (it);
7400 }
7401 else
7402 {
7403 if (it->bidi_p)
7404 {
7405 /* Take note of the stop position we just moved
7406 across, for when we will move back across it. */
7407 it->prev_stop = it->stop_charpos;
7408 /* If we are at base paragraph embedding level, take
7409 note of the last stop position seen at this
7410 level. */
7411 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7412 it->base_level_stop = it->stop_charpos;
7413 }
7414 handle_stop (it);
7415
7416 /* Since a handler may have changed IT->method, we must
7417 recurse here. */
7418 return GET_NEXT_DISPLAY_ELEMENT (it);
7419 }
7420 }
7421 else if (it->bidi_p
7422 /* If we are before prev_stop, we may have overstepped
7423 on our way backwards a stop_pos, and if so, we need
7424 to handle that stop_pos. */
7425 && IT_STRING_CHARPOS (*it) < it->prev_stop
7426 /* We can sometimes back up for reasons that have nothing
7427 to do with bidi reordering. E.g., compositions. The
7428 code below is only needed when we are above the base
7429 embedding level, so test for that explicitly. */
7430 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7431 {
7432 /* If we lost track of base_level_stop, we have no better
7433 place for handle_stop_backwards to start from than string
7434 beginning. This happens, e.g., when we were reseated to
7435 the previous screenful of text by vertical-motion. */
7436 if (it->base_level_stop <= 0
7437 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7438 it->base_level_stop = 0;
7439 handle_stop_backwards (it, it->base_level_stop);
7440 return GET_NEXT_DISPLAY_ELEMENT (it);
7441 }
7442 }
7443
7444 if (it->current.overlay_string_index >= 0)
7445 {
7446 /* Get the next character from an overlay string. In overlay
7447 strings, there is no field width or padding with spaces to
7448 do. */
7449 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7450 {
7451 it->what = IT_EOB;
7452 return 0;
7453 }
7454 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7455 IT_STRING_BYTEPOS (*it),
7456 it->bidi_it.scan_dir < 0
7457 ? -1
7458 : SCHARS (it->string))
7459 && next_element_from_composition (it))
7460 {
7461 return 1;
7462 }
7463 else if (STRING_MULTIBYTE (it->string))
7464 {
7465 const unsigned char *s = (SDATA (it->string)
7466 + IT_STRING_BYTEPOS (*it));
7467 it->c = string_char_and_length (s, &it->len);
7468 }
7469 else
7470 {
7471 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7472 it->len = 1;
7473 }
7474 }
7475 else
7476 {
7477 /* Get the next character from a Lisp string that is not an
7478 overlay string. Such strings come from the mode line, for
7479 example. We may have to pad with spaces, or truncate the
7480 string. See also next_element_from_c_string. */
7481 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7482 {
7483 it->what = IT_EOB;
7484 return 0;
7485 }
7486 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7487 {
7488 /* Pad with spaces. */
7489 it->c = ' ', it->len = 1;
7490 CHARPOS (position) = BYTEPOS (position) = -1;
7491 }
7492 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7493 IT_STRING_BYTEPOS (*it),
7494 it->bidi_it.scan_dir < 0
7495 ? -1
7496 : it->string_nchars)
7497 && next_element_from_composition (it))
7498 {
7499 return 1;
7500 }
7501 else if (STRING_MULTIBYTE (it->string))
7502 {
7503 const unsigned char *s = (SDATA (it->string)
7504 + IT_STRING_BYTEPOS (*it));
7505 it->c = string_char_and_length (s, &it->len);
7506 }
7507 else
7508 {
7509 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7510 it->len = 1;
7511 }
7512 }
7513
7514 /* Record what we have and where it came from. */
7515 it->what = IT_CHARACTER;
7516 it->object = it->string;
7517 it->position = position;
7518 return 1;
7519 }
7520
7521
7522 /* Load IT with next display element from C string IT->s.
7523 IT->string_nchars is the maximum number of characters to return
7524 from the string. IT->end_charpos may be greater than
7525 IT->string_nchars when this function is called, in which case we
7526 may have to return padding spaces. Value is zero if end of string
7527 reached, including padding spaces. */
7528
7529 static int
7530 next_element_from_c_string (struct it *it)
7531 {
7532 int success_p = 1;
7533
7534 xassert (it->s);
7535 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7536 it->what = IT_CHARACTER;
7537 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7538 it->object = Qnil;
7539
7540 /* With bidi reordering, the character to display might not be the
7541 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7542 we were reseated to a new string, whose paragraph direction is
7543 not known. */
7544 if (it->bidi_p && it->bidi_it.first_elt)
7545 get_visually_first_element (it);
7546
7547 /* IT's position can be greater than IT->string_nchars in case a
7548 field width or precision has been specified when the iterator was
7549 initialized. */
7550 if (IT_CHARPOS (*it) >= it->end_charpos)
7551 {
7552 /* End of the game. */
7553 it->what = IT_EOB;
7554 success_p = 0;
7555 }
7556 else if (IT_CHARPOS (*it) >= it->string_nchars)
7557 {
7558 /* Pad with spaces. */
7559 it->c = ' ', it->len = 1;
7560 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7561 }
7562 else if (it->multibyte_p)
7563 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7564 else
7565 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7566
7567 return success_p;
7568 }
7569
7570
7571 /* Set up IT to return characters from an ellipsis, if appropriate.
7572 The definition of the ellipsis glyphs may come from a display table
7573 entry. This function fills IT with the first glyph from the
7574 ellipsis if an ellipsis is to be displayed. */
7575
7576 static int
7577 next_element_from_ellipsis (struct it *it)
7578 {
7579 if (it->selective_display_ellipsis_p)
7580 setup_for_ellipsis (it, it->len);
7581 else
7582 {
7583 /* The face at the current position may be different from the
7584 face we find after the invisible text. Remember what it
7585 was in IT->saved_face_id, and signal that it's there by
7586 setting face_before_selective_p. */
7587 it->saved_face_id = it->face_id;
7588 it->method = GET_FROM_BUFFER;
7589 it->object = it->w->buffer;
7590 reseat_at_next_visible_line_start (it, 1);
7591 it->face_before_selective_p = 1;
7592 }
7593
7594 return GET_NEXT_DISPLAY_ELEMENT (it);
7595 }
7596
7597
7598 /* Deliver an image display element. The iterator IT is already
7599 filled with image information (done in handle_display_prop). Value
7600 is always 1. */
7601
7602
7603 static int
7604 next_element_from_image (struct it *it)
7605 {
7606 it->what = IT_IMAGE;
7607 it->ignore_overlay_strings_at_pos_p = 0;
7608 return 1;
7609 }
7610
7611
7612 /* Fill iterator IT with next display element from a stretch glyph
7613 property. IT->object is the value of the text property. Value is
7614 always 1. */
7615
7616 static int
7617 next_element_from_stretch (struct it *it)
7618 {
7619 it->what = IT_STRETCH;
7620 return 1;
7621 }
7622
7623 /* Scan backwards from IT's current position until we find a stop
7624 position, or until BEGV. This is called when we find ourself
7625 before both the last known prev_stop and base_level_stop while
7626 reordering bidirectional text. */
7627
7628 static void
7629 compute_stop_pos_backwards (struct it *it)
7630 {
7631 const int SCAN_BACK_LIMIT = 1000;
7632 struct text_pos pos;
7633 struct display_pos save_current = it->current;
7634 struct text_pos save_position = it->position;
7635 EMACS_INT charpos = IT_CHARPOS (*it);
7636 EMACS_INT where_we_are = charpos;
7637 EMACS_INT save_stop_pos = it->stop_charpos;
7638 EMACS_INT save_end_pos = it->end_charpos;
7639
7640 xassert (NILP (it->string) && !it->s);
7641 xassert (it->bidi_p);
7642 it->bidi_p = 0;
7643 do
7644 {
7645 it->end_charpos = min (charpos + 1, ZV);
7646 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7647 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7648 reseat_1 (it, pos, 0);
7649 compute_stop_pos (it);
7650 /* We must advance forward, right? */
7651 if (it->stop_charpos <= charpos)
7652 abort ();
7653 }
7654 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7655
7656 if (it->stop_charpos <= where_we_are)
7657 it->prev_stop = it->stop_charpos;
7658 else
7659 it->prev_stop = BEGV;
7660 it->bidi_p = 1;
7661 it->current = save_current;
7662 it->position = save_position;
7663 it->stop_charpos = save_stop_pos;
7664 it->end_charpos = save_end_pos;
7665 }
7666
7667 /* Scan forward from CHARPOS in the current buffer/string, until we
7668 find a stop position > current IT's position. Then handle the stop
7669 position before that. This is called when we bump into a stop
7670 position while reordering bidirectional text. CHARPOS should be
7671 the last previously processed stop_pos (or BEGV/0, if none were
7672 processed yet) whose position is less that IT's current
7673 position. */
7674
7675 static void
7676 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7677 {
7678 int bufp = !STRINGP (it->string);
7679 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7680 struct display_pos save_current = it->current;
7681 struct text_pos save_position = it->position;
7682 struct text_pos pos1;
7683 EMACS_INT next_stop;
7684
7685 /* Scan in strict logical order. */
7686 xassert (it->bidi_p);
7687 it->bidi_p = 0;
7688 do
7689 {
7690 it->prev_stop = charpos;
7691 if (bufp)
7692 {
7693 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7694 reseat_1 (it, pos1, 0);
7695 }
7696 else
7697 it->current.string_pos = string_pos (charpos, it->string);
7698 compute_stop_pos (it);
7699 /* We must advance forward, right? */
7700 if (it->stop_charpos <= it->prev_stop)
7701 abort ();
7702 charpos = it->stop_charpos;
7703 }
7704 while (charpos <= where_we_are);
7705
7706 it->bidi_p = 1;
7707 it->current = save_current;
7708 it->position = save_position;
7709 next_stop = it->stop_charpos;
7710 it->stop_charpos = it->prev_stop;
7711 handle_stop (it);
7712 it->stop_charpos = next_stop;
7713 }
7714
7715 /* Load IT with the next display element from current_buffer. Value
7716 is zero if end of buffer reached. IT->stop_charpos is the next
7717 position at which to stop and check for text properties or buffer
7718 end. */
7719
7720 static int
7721 next_element_from_buffer (struct it *it)
7722 {
7723 int success_p = 1;
7724
7725 xassert (IT_CHARPOS (*it) >= BEGV);
7726 xassert (NILP (it->string) && !it->s);
7727 xassert (!it->bidi_p
7728 || (EQ (it->bidi_it.string.lstring, Qnil)
7729 && it->bidi_it.string.s == NULL));
7730
7731 /* With bidi reordering, the character to display might not be the
7732 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7733 we were reseat()ed to a new buffer position, which is potentially
7734 a different paragraph. */
7735 if (it->bidi_p && it->bidi_it.first_elt)
7736 {
7737 get_visually_first_element (it);
7738 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7739 }
7740
7741 if (IT_CHARPOS (*it) >= it->stop_charpos)
7742 {
7743 if (IT_CHARPOS (*it) >= it->end_charpos)
7744 {
7745 int overlay_strings_follow_p;
7746
7747 /* End of the game, except when overlay strings follow that
7748 haven't been returned yet. */
7749 if (it->overlay_strings_at_end_processed_p)
7750 overlay_strings_follow_p = 0;
7751 else
7752 {
7753 it->overlay_strings_at_end_processed_p = 1;
7754 overlay_strings_follow_p = get_overlay_strings (it, 0);
7755 }
7756
7757 if (overlay_strings_follow_p)
7758 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7759 else
7760 {
7761 it->what = IT_EOB;
7762 it->position = it->current.pos;
7763 success_p = 0;
7764 }
7765 }
7766 else if (!(!it->bidi_p
7767 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7768 || IT_CHARPOS (*it) == it->stop_charpos))
7769 {
7770 /* With bidi non-linear iteration, we could find ourselves
7771 far beyond the last computed stop_charpos, with several
7772 other stop positions in between that we missed. Scan
7773 them all now, in buffer's logical order, until we find
7774 and handle the last stop_charpos that precedes our
7775 current position. */
7776 handle_stop_backwards (it, it->stop_charpos);
7777 return GET_NEXT_DISPLAY_ELEMENT (it);
7778 }
7779 else
7780 {
7781 if (it->bidi_p)
7782 {
7783 /* Take note of the stop position we just moved across,
7784 for when we will move back across it. */
7785 it->prev_stop = it->stop_charpos;
7786 /* If we are at base paragraph embedding level, take
7787 note of the last stop position seen at this
7788 level. */
7789 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7790 it->base_level_stop = it->stop_charpos;
7791 }
7792 handle_stop (it);
7793 return GET_NEXT_DISPLAY_ELEMENT (it);
7794 }
7795 }
7796 else if (it->bidi_p
7797 /* If we are before prev_stop, we may have overstepped on
7798 our way backwards a stop_pos, and if so, we need to
7799 handle that stop_pos. */
7800 && IT_CHARPOS (*it) < it->prev_stop
7801 /* We can sometimes back up for reasons that have nothing
7802 to do with bidi reordering. E.g., compositions. The
7803 code below is only needed when we are above the base
7804 embedding level, so test for that explicitly. */
7805 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7806 {
7807 if (it->base_level_stop <= 0
7808 || IT_CHARPOS (*it) < it->base_level_stop)
7809 {
7810 /* If we lost track of base_level_stop, we need to find
7811 prev_stop by looking backwards. This happens, e.g., when
7812 we were reseated to the previous screenful of text by
7813 vertical-motion. */
7814 it->base_level_stop = BEGV;
7815 compute_stop_pos_backwards (it);
7816 handle_stop_backwards (it, it->prev_stop);
7817 }
7818 else
7819 handle_stop_backwards (it, it->base_level_stop);
7820 return GET_NEXT_DISPLAY_ELEMENT (it);
7821 }
7822 else
7823 {
7824 /* No face changes, overlays etc. in sight, so just return a
7825 character from current_buffer. */
7826 unsigned char *p;
7827 EMACS_INT stop;
7828
7829 /* Maybe run the redisplay end trigger hook. Performance note:
7830 This doesn't seem to cost measurable time. */
7831 if (it->redisplay_end_trigger_charpos
7832 && it->glyph_row
7833 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7834 run_redisplay_end_trigger_hook (it);
7835
7836 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7837 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7838 stop)
7839 && next_element_from_composition (it))
7840 {
7841 return 1;
7842 }
7843
7844 /* Get the next character, maybe multibyte. */
7845 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7846 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7847 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7848 else
7849 it->c = *p, it->len = 1;
7850
7851 /* Record what we have and where it came from. */
7852 it->what = IT_CHARACTER;
7853 it->object = it->w->buffer;
7854 it->position = it->current.pos;
7855
7856 /* Normally we return the character found above, except when we
7857 really want to return an ellipsis for selective display. */
7858 if (it->selective)
7859 {
7860 if (it->c == '\n')
7861 {
7862 /* A value of selective > 0 means hide lines indented more
7863 than that number of columns. */
7864 if (it->selective > 0
7865 && IT_CHARPOS (*it) + 1 < ZV
7866 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7867 IT_BYTEPOS (*it) + 1,
7868 it->selective))
7869 {
7870 success_p = next_element_from_ellipsis (it);
7871 it->dpvec_char_len = -1;
7872 }
7873 }
7874 else if (it->c == '\r' && it->selective == -1)
7875 {
7876 /* A value of selective == -1 means that everything from the
7877 CR to the end of the line is invisible, with maybe an
7878 ellipsis displayed for it. */
7879 success_p = next_element_from_ellipsis (it);
7880 it->dpvec_char_len = -1;
7881 }
7882 }
7883 }
7884
7885 /* Value is zero if end of buffer reached. */
7886 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7887 return success_p;
7888 }
7889
7890
7891 /* Run the redisplay end trigger hook for IT. */
7892
7893 static void
7894 run_redisplay_end_trigger_hook (struct it *it)
7895 {
7896 Lisp_Object args[3];
7897
7898 /* IT->glyph_row should be non-null, i.e. we should be actually
7899 displaying something, or otherwise we should not run the hook. */
7900 xassert (it->glyph_row);
7901
7902 /* Set up hook arguments. */
7903 args[0] = Qredisplay_end_trigger_functions;
7904 args[1] = it->window;
7905 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7906 it->redisplay_end_trigger_charpos = 0;
7907
7908 /* Since we are *trying* to run these functions, don't try to run
7909 them again, even if they get an error. */
7910 it->w->redisplay_end_trigger = Qnil;
7911 Frun_hook_with_args (3, args);
7912
7913 /* Notice if it changed the face of the character we are on. */
7914 handle_face_prop (it);
7915 }
7916
7917
7918 /* Deliver a composition display element. Unlike the other
7919 next_element_from_XXX, this function is not registered in the array
7920 get_next_element[]. It is called from next_element_from_buffer and
7921 next_element_from_string when necessary. */
7922
7923 static int
7924 next_element_from_composition (struct it *it)
7925 {
7926 it->what = IT_COMPOSITION;
7927 it->len = it->cmp_it.nbytes;
7928 if (STRINGP (it->string))
7929 {
7930 if (it->c < 0)
7931 {
7932 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7933 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7934 return 0;
7935 }
7936 it->position = it->current.string_pos;
7937 it->object = it->string;
7938 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7939 IT_STRING_BYTEPOS (*it), it->string);
7940 }
7941 else
7942 {
7943 if (it->c < 0)
7944 {
7945 IT_CHARPOS (*it) += it->cmp_it.nchars;
7946 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7947 if (it->bidi_p)
7948 {
7949 if (it->bidi_it.new_paragraph)
7950 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7951 /* Resync the bidi iterator with IT's new position.
7952 FIXME: this doesn't support bidirectional text. */
7953 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7954 bidi_move_to_visually_next (&it->bidi_it);
7955 }
7956 return 0;
7957 }
7958 it->position = it->current.pos;
7959 it->object = it->w->buffer;
7960 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7961 IT_BYTEPOS (*it), Qnil);
7962 }
7963 return 1;
7964 }
7965
7966
7967 \f
7968 /***********************************************************************
7969 Moving an iterator without producing glyphs
7970 ***********************************************************************/
7971
7972 /* Check if iterator is at a position corresponding to a valid buffer
7973 position after some move_it_ call. */
7974
7975 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7976 ((it)->method == GET_FROM_STRING \
7977 ? IT_STRING_CHARPOS (*it) == 0 \
7978 : 1)
7979
7980
7981 /* Move iterator IT to a specified buffer or X position within one
7982 line on the display without producing glyphs.
7983
7984 OP should be a bit mask including some or all of these bits:
7985 MOVE_TO_X: Stop upon reaching x-position TO_X.
7986 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7987 Regardless of OP's value, stop upon reaching the end of the display line.
7988
7989 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7990 This means, in particular, that TO_X includes window's horizontal
7991 scroll amount.
7992
7993 The return value has several possible values that
7994 say what condition caused the scan to stop:
7995
7996 MOVE_POS_MATCH_OR_ZV
7997 - when TO_POS or ZV was reached.
7998
7999 MOVE_X_REACHED
8000 -when TO_X was reached before TO_POS or ZV were reached.
8001
8002 MOVE_LINE_CONTINUED
8003 - when we reached the end of the display area and the line must
8004 be continued.
8005
8006 MOVE_LINE_TRUNCATED
8007 - when we reached the end of the display area and the line is
8008 truncated.
8009
8010 MOVE_NEWLINE_OR_CR
8011 - when we stopped at a line end, i.e. a newline or a CR and selective
8012 display is on. */
8013
8014 static enum move_it_result
8015 move_it_in_display_line_to (struct it *it,
8016 EMACS_INT to_charpos, int to_x,
8017 enum move_operation_enum op)
8018 {
8019 enum move_it_result result = MOVE_UNDEFINED;
8020 struct glyph_row *saved_glyph_row;
8021 struct it wrap_it, atpos_it, atx_it, ppos_it;
8022 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8023 void *ppos_data = NULL;
8024 int may_wrap = 0;
8025 enum it_method prev_method = it->method;
8026 EMACS_INT prev_pos = IT_CHARPOS (*it);
8027 int saw_smaller_pos = prev_pos < to_charpos;
8028
8029 /* Don't produce glyphs in produce_glyphs. */
8030 saved_glyph_row = it->glyph_row;
8031 it->glyph_row = NULL;
8032
8033 /* Use wrap_it to save a copy of IT wherever a word wrap could
8034 occur. Use atpos_it to save a copy of IT at the desired buffer
8035 position, if found, so that we can scan ahead and check if the
8036 word later overshoots the window edge. Use atx_it similarly, for
8037 pixel positions. */
8038 wrap_it.sp = -1;
8039 atpos_it.sp = -1;
8040 atx_it.sp = -1;
8041
8042 /* Use ppos_it under bidi reordering to save a copy of IT for the
8043 position > CHARPOS that is the closest to CHARPOS. We restore
8044 that position in IT when we have scanned the entire display line
8045 without finding a match for CHARPOS and all the character
8046 positions are greater than CHARPOS. */
8047 if (it->bidi_p)
8048 {
8049 SAVE_IT (ppos_it, *it, ppos_data);
8050 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8051 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8052 SAVE_IT (ppos_it, *it, ppos_data);
8053 }
8054
8055 #define BUFFER_POS_REACHED_P() \
8056 ((op & MOVE_TO_POS) != 0 \
8057 && BUFFERP (it->object) \
8058 && (IT_CHARPOS (*it) == to_charpos \
8059 || ((!it->bidi_p \
8060 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8061 && IT_CHARPOS (*it) > to_charpos) \
8062 || (it->what == IT_COMPOSITION \
8063 && ((IT_CHARPOS (*it) > to_charpos \
8064 && to_charpos >= it->cmp_it.charpos) \
8065 || (IT_CHARPOS (*it) < to_charpos \
8066 && to_charpos <= it->cmp_it.charpos)))) \
8067 && (it->method == GET_FROM_BUFFER \
8068 || (it->method == GET_FROM_DISPLAY_VECTOR \
8069 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8070
8071 /* If there's a line-/wrap-prefix, handle it. */
8072 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8073 && it->current_y < it->last_visible_y)
8074 handle_line_prefix (it);
8075
8076 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8077 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8078
8079 while (1)
8080 {
8081 int x, i, ascent = 0, descent = 0;
8082
8083 /* Utility macro to reset an iterator with x, ascent, and descent. */
8084 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8085 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8086 (IT)->max_descent = descent)
8087
8088 /* Stop if we move beyond TO_CHARPOS (after an image or a
8089 display string or stretch glyph). */
8090 if ((op & MOVE_TO_POS) != 0
8091 && BUFFERP (it->object)
8092 && it->method == GET_FROM_BUFFER
8093 && (((!it->bidi_p
8094 /* When the iterator is at base embedding level, we
8095 are guaranteed that characters are delivered for
8096 display in strictly increasing order of their
8097 buffer positions. */
8098 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8099 && IT_CHARPOS (*it) > to_charpos)
8100 || (it->bidi_p
8101 && (prev_method == GET_FROM_IMAGE
8102 || prev_method == GET_FROM_STRETCH
8103 || prev_method == GET_FROM_STRING)
8104 /* Passed TO_CHARPOS from left to right. */
8105 && ((prev_pos < to_charpos
8106 && IT_CHARPOS (*it) > to_charpos)
8107 /* Passed TO_CHARPOS from right to left. */
8108 || (prev_pos > to_charpos
8109 && IT_CHARPOS (*it) < to_charpos)))))
8110 {
8111 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8112 {
8113 result = MOVE_POS_MATCH_OR_ZV;
8114 break;
8115 }
8116 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8117 /* If wrap_it is valid, the current position might be in a
8118 word that is wrapped. So, save the iterator in
8119 atpos_it and continue to see if wrapping happens. */
8120 SAVE_IT (atpos_it, *it, atpos_data);
8121 }
8122
8123 /* Stop when ZV reached.
8124 We used to stop here when TO_CHARPOS reached as well, but that is
8125 too soon if this glyph does not fit on this line. So we handle it
8126 explicitly below. */
8127 if (!get_next_display_element (it))
8128 {
8129 result = MOVE_POS_MATCH_OR_ZV;
8130 break;
8131 }
8132
8133 if (it->line_wrap == TRUNCATE)
8134 {
8135 if (BUFFER_POS_REACHED_P ())
8136 {
8137 result = MOVE_POS_MATCH_OR_ZV;
8138 break;
8139 }
8140 }
8141 else
8142 {
8143 if (it->line_wrap == WORD_WRAP)
8144 {
8145 if (IT_DISPLAYING_WHITESPACE (it))
8146 may_wrap = 1;
8147 else if (may_wrap)
8148 {
8149 /* We have reached a glyph that follows one or more
8150 whitespace characters. If the position is
8151 already found, we are done. */
8152 if (atpos_it.sp >= 0)
8153 {
8154 RESTORE_IT (it, &atpos_it, atpos_data);
8155 result = MOVE_POS_MATCH_OR_ZV;
8156 goto done;
8157 }
8158 if (atx_it.sp >= 0)
8159 {
8160 RESTORE_IT (it, &atx_it, atx_data);
8161 result = MOVE_X_REACHED;
8162 goto done;
8163 }
8164 /* Otherwise, we can wrap here. */
8165 SAVE_IT (wrap_it, *it, wrap_data);
8166 may_wrap = 0;
8167 }
8168 }
8169 }
8170
8171 /* Remember the line height for the current line, in case
8172 the next element doesn't fit on the line. */
8173 ascent = it->max_ascent;
8174 descent = it->max_descent;
8175
8176 /* The call to produce_glyphs will get the metrics of the
8177 display element IT is loaded with. Record the x-position
8178 before this display element, in case it doesn't fit on the
8179 line. */
8180 x = it->current_x;
8181
8182 PRODUCE_GLYPHS (it);
8183
8184 if (it->area != TEXT_AREA)
8185 {
8186 prev_method = it->method;
8187 if (it->method == GET_FROM_BUFFER)
8188 prev_pos = IT_CHARPOS (*it);
8189 set_iterator_to_next (it, 1);
8190 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8191 SET_TEXT_POS (this_line_min_pos,
8192 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8193 if (it->bidi_p
8194 && (op & MOVE_TO_POS)
8195 && IT_CHARPOS (*it) > to_charpos
8196 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8197 SAVE_IT (ppos_it, *it, ppos_data);
8198 continue;
8199 }
8200
8201 /* The number of glyphs we get back in IT->nglyphs will normally
8202 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8203 character on a terminal frame, or (iii) a line end. For the
8204 second case, IT->nglyphs - 1 padding glyphs will be present.
8205 (On X frames, there is only one glyph produced for a
8206 composite character.)
8207
8208 The behavior implemented below means, for continuation lines,
8209 that as many spaces of a TAB as fit on the current line are
8210 displayed there. For terminal frames, as many glyphs of a
8211 multi-glyph character are displayed in the current line, too.
8212 This is what the old redisplay code did, and we keep it that
8213 way. Under X, the whole shape of a complex character must
8214 fit on the line or it will be completely displayed in the
8215 next line.
8216
8217 Note that both for tabs and padding glyphs, all glyphs have
8218 the same width. */
8219 if (it->nglyphs)
8220 {
8221 /* More than one glyph or glyph doesn't fit on line. All
8222 glyphs have the same width. */
8223 int single_glyph_width = it->pixel_width / it->nglyphs;
8224 int new_x;
8225 int x_before_this_char = x;
8226 int hpos_before_this_char = it->hpos;
8227
8228 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8229 {
8230 new_x = x + single_glyph_width;
8231
8232 /* We want to leave anything reaching TO_X to the caller. */
8233 if ((op & MOVE_TO_X) && new_x > to_x)
8234 {
8235 if (BUFFER_POS_REACHED_P ())
8236 {
8237 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8238 goto buffer_pos_reached;
8239 if (atpos_it.sp < 0)
8240 {
8241 SAVE_IT (atpos_it, *it, atpos_data);
8242 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8243 }
8244 }
8245 else
8246 {
8247 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8248 {
8249 it->current_x = x;
8250 result = MOVE_X_REACHED;
8251 break;
8252 }
8253 if (atx_it.sp < 0)
8254 {
8255 SAVE_IT (atx_it, *it, atx_data);
8256 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8257 }
8258 }
8259 }
8260
8261 if (/* Lines are continued. */
8262 it->line_wrap != TRUNCATE
8263 && (/* And glyph doesn't fit on the line. */
8264 new_x > it->last_visible_x
8265 /* Or it fits exactly and we're on a window
8266 system frame. */
8267 || (new_x == it->last_visible_x
8268 && FRAME_WINDOW_P (it->f))))
8269 {
8270 if (/* IT->hpos == 0 means the very first glyph
8271 doesn't fit on the line, e.g. a wide image. */
8272 it->hpos == 0
8273 || (new_x == it->last_visible_x
8274 && FRAME_WINDOW_P (it->f)))
8275 {
8276 ++it->hpos;
8277 it->current_x = new_x;
8278
8279 /* The character's last glyph just barely fits
8280 in this row. */
8281 if (i == it->nglyphs - 1)
8282 {
8283 /* If this is the destination position,
8284 return a position *before* it in this row,
8285 now that we know it fits in this row. */
8286 if (BUFFER_POS_REACHED_P ())
8287 {
8288 if (it->line_wrap != WORD_WRAP
8289 || wrap_it.sp < 0)
8290 {
8291 it->hpos = hpos_before_this_char;
8292 it->current_x = x_before_this_char;
8293 result = MOVE_POS_MATCH_OR_ZV;
8294 break;
8295 }
8296 if (it->line_wrap == WORD_WRAP
8297 && atpos_it.sp < 0)
8298 {
8299 SAVE_IT (atpos_it, *it, atpos_data);
8300 atpos_it.current_x = x_before_this_char;
8301 atpos_it.hpos = hpos_before_this_char;
8302 }
8303 }
8304
8305 prev_method = it->method;
8306 if (it->method == GET_FROM_BUFFER)
8307 prev_pos = IT_CHARPOS (*it);
8308 set_iterator_to_next (it, 1);
8309 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8310 SET_TEXT_POS (this_line_min_pos,
8311 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8312 /* On graphical terminals, newlines may
8313 "overflow" into the fringe if
8314 overflow-newline-into-fringe is non-nil.
8315 On text-only terminals, newlines may
8316 overflow into the last glyph on the
8317 display line.*/
8318 if (!FRAME_WINDOW_P (it->f)
8319 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8320 {
8321 if (!get_next_display_element (it))
8322 {
8323 result = MOVE_POS_MATCH_OR_ZV;
8324 break;
8325 }
8326 if (BUFFER_POS_REACHED_P ())
8327 {
8328 if (ITERATOR_AT_END_OF_LINE_P (it))
8329 result = MOVE_POS_MATCH_OR_ZV;
8330 else
8331 result = MOVE_LINE_CONTINUED;
8332 break;
8333 }
8334 if (ITERATOR_AT_END_OF_LINE_P (it))
8335 {
8336 result = MOVE_NEWLINE_OR_CR;
8337 break;
8338 }
8339 }
8340 }
8341 }
8342 else
8343 IT_RESET_X_ASCENT_DESCENT (it);
8344
8345 if (wrap_it.sp >= 0)
8346 {
8347 RESTORE_IT (it, &wrap_it, wrap_data);
8348 atpos_it.sp = -1;
8349 atx_it.sp = -1;
8350 }
8351
8352 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8353 IT_CHARPOS (*it)));
8354 result = MOVE_LINE_CONTINUED;
8355 break;
8356 }
8357
8358 if (BUFFER_POS_REACHED_P ())
8359 {
8360 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8361 goto buffer_pos_reached;
8362 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8363 {
8364 SAVE_IT (atpos_it, *it, atpos_data);
8365 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8366 }
8367 }
8368
8369 if (new_x > it->first_visible_x)
8370 {
8371 /* Glyph is visible. Increment number of glyphs that
8372 would be displayed. */
8373 ++it->hpos;
8374 }
8375 }
8376
8377 if (result != MOVE_UNDEFINED)
8378 break;
8379 }
8380 else if (BUFFER_POS_REACHED_P ())
8381 {
8382 buffer_pos_reached:
8383 IT_RESET_X_ASCENT_DESCENT (it);
8384 result = MOVE_POS_MATCH_OR_ZV;
8385 break;
8386 }
8387 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8388 {
8389 /* Stop when TO_X specified and reached. This check is
8390 necessary here because of lines consisting of a line end,
8391 only. The line end will not produce any glyphs and we
8392 would never get MOVE_X_REACHED. */
8393 xassert (it->nglyphs == 0);
8394 result = MOVE_X_REACHED;
8395 break;
8396 }
8397
8398 /* Is this a line end? If yes, we're done. */
8399 if (ITERATOR_AT_END_OF_LINE_P (it))
8400 {
8401 /* If we are past TO_CHARPOS, but never saw any character
8402 positions smaller than TO_CHARPOS, return
8403 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8404 did. */
8405 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8406 {
8407 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8408 {
8409 if (IT_CHARPOS (ppos_it) < ZV)
8410 {
8411 RESTORE_IT (it, &ppos_it, ppos_data);
8412 result = MOVE_POS_MATCH_OR_ZV;
8413 }
8414 else
8415 goto buffer_pos_reached;
8416 }
8417 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8418 && IT_CHARPOS (*it) > to_charpos)
8419 goto buffer_pos_reached;
8420 else
8421 result = MOVE_NEWLINE_OR_CR;
8422 }
8423 else
8424 result = MOVE_NEWLINE_OR_CR;
8425 break;
8426 }
8427
8428 prev_method = it->method;
8429 if (it->method == GET_FROM_BUFFER)
8430 prev_pos = IT_CHARPOS (*it);
8431 /* The current display element has been consumed. Advance
8432 to the next. */
8433 set_iterator_to_next (it, 1);
8434 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8435 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8436 if (IT_CHARPOS (*it) < to_charpos)
8437 saw_smaller_pos = 1;
8438 if (it->bidi_p
8439 && (op & MOVE_TO_POS)
8440 && IT_CHARPOS (*it) >= to_charpos
8441 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8442 SAVE_IT (ppos_it, *it, ppos_data);
8443
8444 /* Stop if lines are truncated and IT's current x-position is
8445 past the right edge of the window now. */
8446 if (it->line_wrap == TRUNCATE
8447 && it->current_x >= it->last_visible_x)
8448 {
8449 if (!FRAME_WINDOW_P (it->f)
8450 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8451 {
8452 int at_eob_p = 0;
8453
8454 if ((at_eob_p = !get_next_display_element (it))
8455 || BUFFER_POS_REACHED_P ()
8456 /* If we are past TO_CHARPOS, but never saw any
8457 character positions smaller than TO_CHARPOS,
8458 return MOVE_POS_MATCH_OR_ZV, like the
8459 unidirectional display did. */
8460 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8461 && !saw_smaller_pos
8462 && IT_CHARPOS (*it) > to_charpos))
8463 {
8464 if (it->bidi_p
8465 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8466 RESTORE_IT (it, &ppos_it, ppos_data);
8467 result = MOVE_POS_MATCH_OR_ZV;
8468 break;
8469 }
8470 if (ITERATOR_AT_END_OF_LINE_P (it))
8471 {
8472 result = MOVE_NEWLINE_OR_CR;
8473 break;
8474 }
8475 }
8476 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8477 && !saw_smaller_pos
8478 && IT_CHARPOS (*it) > to_charpos)
8479 {
8480 if (IT_CHARPOS (ppos_it) < ZV)
8481 RESTORE_IT (it, &ppos_it, ppos_data);
8482 result = MOVE_POS_MATCH_OR_ZV;
8483 break;
8484 }
8485 result = MOVE_LINE_TRUNCATED;
8486 break;
8487 }
8488 #undef IT_RESET_X_ASCENT_DESCENT
8489 }
8490
8491 #undef BUFFER_POS_REACHED_P
8492
8493 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8494 restore the saved iterator. */
8495 if (atpos_it.sp >= 0)
8496 RESTORE_IT (it, &atpos_it, atpos_data);
8497 else if (atx_it.sp >= 0)
8498 RESTORE_IT (it, &atx_it, atx_data);
8499
8500 done:
8501
8502 if (atpos_data)
8503 bidi_unshelve_cache (atpos_data, 1);
8504 if (atx_data)
8505 bidi_unshelve_cache (atx_data, 1);
8506 if (wrap_data)
8507 bidi_unshelve_cache (wrap_data, 1);
8508 if (ppos_data)
8509 bidi_unshelve_cache (ppos_data, 1);
8510
8511 /* Restore the iterator settings altered at the beginning of this
8512 function. */
8513 it->glyph_row = saved_glyph_row;
8514 return result;
8515 }
8516
8517 /* For external use. */
8518 void
8519 move_it_in_display_line (struct it *it,
8520 EMACS_INT to_charpos, int to_x,
8521 enum move_operation_enum op)
8522 {
8523 if (it->line_wrap == WORD_WRAP
8524 && (op & MOVE_TO_X))
8525 {
8526 struct it save_it;
8527 void *save_data = NULL;
8528 int skip;
8529
8530 SAVE_IT (save_it, *it, save_data);
8531 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8532 /* When word-wrap is on, TO_X may lie past the end
8533 of a wrapped line. Then it->current is the
8534 character on the next line, so backtrack to the
8535 space before the wrap point. */
8536 if (skip == MOVE_LINE_CONTINUED)
8537 {
8538 int prev_x = max (it->current_x - 1, 0);
8539 RESTORE_IT (it, &save_it, save_data);
8540 move_it_in_display_line_to
8541 (it, -1, prev_x, MOVE_TO_X);
8542 }
8543 else
8544 bidi_unshelve_cache (save_data, 1);
8545 }
8546 else
8547 move_it_in_display_line_to (it, to_charpos, to_x, op);
8548 }
8549
8550
8551 /* Move IT forward until it satisfies one or more of the criteria in
8552 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8553
8554 OP is a bit-mask that specifies where to stop, and in particular,
8555 which of those four position arguments makes a difference. See the
8556 description of enum move_operation_enum.
8557
8558 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8559 screen line, this function will set IT to the next position that is
8560 displayed to the right of TO_CHARPOS on the screen. */
8561
8562 void
8563 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8564 {
8565 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8566 int line_height, line_start_x = 0, reached = 0;
8567 void *backup_data = NULL;
8568
8569 for (;;)
8570 {
8571 if (op & MOVE_TO_VPOS)
8572 {
8573 /* If no TO_CHARPOS and no TO_X specified, stop at the
8574 start of the line TO_VPOS. */
8575 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8576 {
8577 if (it->vpos == to_vpos)
8578 {
8579 reached = 1;
8580 break;
8581 }
8582 else
8583 skip = move_it_in_display_line_to (it, -1, -1, 0);
8584 }
8585 else
8586 {
8587 /* TO_VPOS >= 0 means stop at TO_X in the line at
8588 TO_VPOS, or at TO_POS, whichever comes first. */
8589 if (it->vpos == to_vpos)
8590 {
8591 reached = 2;
8592 break;
8593 }
8594
8595 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8596
8597 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8598 {
8599 reached = 3;
8600 break;
8601 }
8602 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8603 {
8604 /* We have reached TO_X but not in the line we want. */
8605 skip = move_it_in_display_line_to (it, to_charpos,
8606 -1, MOVE_TO_POS);
8607 if (skip == MOVE_POS_MATCH_OR_ZV)
8608 {
8609 reached = 4;
8610 break;
8611 }
8612 }
8613 }
8614 }
8615 else if (op & MOVE_TO_Y)
8616 {
8617 struct it it_backup;
8618
8619 if (it->line_wrap == WORD_WRAP)
8620 SAVE_IT (it_backup, *it, backup_data);
8621
8622 /* TO_Y specified means stop at TO_X in the line containing
8623 TO_Y---or at TO_CHARPOS if this is reached first. The
8624 problem is that we can't really tell whether the line
8625 contains TO_Y before we have completely scanned it, and
8626 this may skip past TO_X. What we do is to first scan to
8627 TO_X.
8628
8629 If TO_X is not specified, use a TO_X of zero. The reason
8630 is to make the outcome of this function more predictable.
8631 If we didn't use TO_X == 0, we would stop at the end of
8632 the line which is probably not what a caller would expect
8633 to happen. */
8634 skip = move_it_in_display_line_to
8635 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8636 (MOVE_TO_X | (op & MOVE_TO_POS)));
8637
8638 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8639 if (skip == MOVE_POS_MATCH_OR_ZV)
8640 reached = 5;
8641 else if (skip == MOVE_X_REACHED)
8642 {
8643 /* If TO_X was reached, we want to know whether TO_Y is
8644 in the line. We know this is the case if the already
8645 scanned glyphs make the line tall enough. Otherwise,
8646 we must check by scanning the rest of the line. */
8647 line_height = it->max_ascent + it->max_descent;
8648 if (to_y >= it->current_y
8649 && to_y < it->current_y + line_height)
8650 {
8651 reached = 6;
8652 break;
8653 }
8654 SAVE_IT (it_backup, *it, backup_data);
8655 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8656 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8657 op & MOVE_TO_POS);
8658 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8659 line_height = it->max_ascent + it->max_descent;
8660 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8661
8662 if (to_y >= it->current_y
8663 && to_y < it->current_y + line_height)
8664 {
8665 /* If TO_Y is in this line and TO_X was reached
8666 above, we scanned too far. We have to restore
8667 IT's settings to the ones before skipping. */
8668 RESTORE_IT (it, &it_backup, backup_data);
8669 reached = 6;
8670 }
8671 else
8672 {
8673 skip = skip2;
8674 if (skip == MOVE_POS_MATCH_OR_ZV)
8675 reached = 7;
8676 }
8677 }
8678 else
8679 {
8680 /* Check whether TO_Y is in this line. */
8681 line_height = it->max_ascent + it->max_descent;
8682 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8683
8684 if (to_y >= it->current_y
8685 && to_y < it->current_y + line_height)
8686 {
8687 /* When word-wrap is on, TO_X may lie past the end
8688 of a wrapped line. Then it->current is the
8689 character on the next line, so backtrack to the
8690 space before the wrap point. */
8691 if (skip == MOVE_LINE_CONTINUED
8692 && it->line_wrap == WORD_WRAP)
8693 {
8694 int prev_x = max (it->current_x - 1, 0);
8695 RESTORE_IT (it, &it_backup, backup_data);
8696 skip = move_it_in_display_line_to
8697 (it, -1, prev_x, MOVE_TO_X);
8698 }
8699 reached = 6;
8700 }
8701 }
8702
8703 if (reached)
8704 break;
8705 }
8706 else if (BUFFERP (it->object)
8707 && (it->method == GET_FROM_BUFFER
8708 || it->method == GET_FROM_STRETCH)
8709 && IT_CHARPOS (*it) >= to_charpos
8710 /* Under bidi iteration, a call to set_iterator_to_next
8711 can scan far beyond to_charpos if the initial
8712 portion of the next line needs to be reordered. In
8713 that case, give move_it_in_display_line_to another
8714 chance below. */
8715 && !(it->bidi_p
8716 && it->bidi_it.scan_dir == -1))
8717 skip = MOVE_POS_MATCH_OR_ZV;
8718 else
8719 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8720
8721 switch (skip)
8722 {
8723 case MOVE_POS_MATCH_OR_ZV:
8724 reached = 8;
8725 goto out;
8726
8727 case MOVE_NEWLINE_OR_CR:
8728 set_iterator_to_next (it, 1);
8729 it->continuation_lines_width = 0;
8730 break;
8731
8732 case MOVE_LINE_TRUNCATED:
8733 it->continuation_lines_width = 0;
8734 reseat_at_next_visible_line_start (it, 0);
8735 if ((op & MOVE_TO_POS) != 0
8736 && IT_CHARPOS (*it) > to_charpos)
8737 {
8738 reached = 9;
8739 goto out;
8740 }
8741 break;
8742
8743 case MOVE_LINE_CONTINUED:
8744 /* For continued lines ending in a tab, some of the glyphs
8745 associated with the tab are displayed on the current
8746 line. Since it->current_x does not include these glyphs,
8747 we use it->last_visible_x instead. */
8748 if (it->c == '\t')
8749 {
8750 it->continuation_lines_width += it->last_visible_x;
8751 /* When moving by vpos, ensure that the iterator really
8752 advances to the next line (bug#847, bug#969). Fixme:
8753 do we need to do this in other circumstances? */
8754 if (it->current_x != it->last_visible_x
8755 && (op & MOVE_TO_VPOS)
8756 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8757 {
8758 line_start_x = it->current_x + it->pixel_width
8759 - it->last_visible_x;
8760 set_iterator_to_next (it, 0);
8761 }
8762 }
8763 else
8764 it->continuation_lines_width += it->current_x;
8765 break;
8766
8767 default:
8768 abort ();
8769 }
8770
8771 /* Reset/increment for the next run. */
8772 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8773 it->current_x = line_start_x;
8774 line_start_x = 0;
8775 it->hpos = 0;
8776 it->current_y += it->max_ascent + it->max_descent;
8777 ++it->vpos;
8778 last_height = it->max_ascent + it->max_descent;
8779 last_max_ascent = it->max_ascent;
8780 it->max_ascent = it->max_descent = 0;
8781 }
8782
8783 out:
8784
8785 /* On text terminals, we may stop at the end of a line in the middle
8786 of a multi-character glyph. If the glyph itself is continued,
8787 i.e. it is actually displayed on the next line, don't treat this
8788 stopping point as valid; move to the next line instead (unless
8789 that brings us offscreen). */
8790 if (!FRAME_WINDOW_P (it->f)
8791 && op & MOVE_TO_POS
8792 && IT_CHARPOS (*it) == to_charpos
8793 && it->what == IT_CHARACTER
8794 && it->nglyphs > 1
8795 && it->line_wrap == WINDOW_WRAP
8796 && it->current_x == it->last_visible_x - 1
8797 && it->c != '\n'
8798 && it->c != '\t'
8799 && it->vpos < XFASTINT (it->w->window_end_vpos))
8800 {
8801 it->continuation_lines_width += it->current_x;
8802 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8803 it->current_y += it->max_ascent + it->max_descent;
8804 ++it->vpos;
8805 last_height = it->max_ascent + it->max_descent;
8806 last_max_ascent = it->max_ascent;
8807 }
8808
8809 if (backup_data)
8810 bidi_unshelve_cache (backup_data, 1);
8811
8812 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8813 }
8814
8815
8816 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8817
8818 If DY > 0, move IT backward at least that many pixels. DY = 0
8819 means move IT backward to the preceding line start or BEGV. This
8820 function may move over more than DY pixels if IT->current_y - DY
8821 ends up in the middle of a line; in this case IT->current_y will be
8822 set to the top of the line moved to. */
8823
8824 void
8825 move_it_vertically_backward (struct it *it, int dy)
8826 {
8827 int nlines, h;
8828 struct it it2, it3;
8829 void *it2data = NULL, *it3data = NULL;
8830 EMACS_INT start_pos;
8831
8832 move_further_back:
8833 xassert (dy >= 0);
8834
8835 start_pos = IT_CHARPOS (*it);
8836
8837 /* Estimate how many newlines we must move back. */
8838 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8839
8840 /* Set the iterator's position that many lines back. */
8841 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8842 back_to_previous_visible_line_start (it);
8843
8844 /* Reseat the iterator here. When moving backward, we don't want
8845 reseat to skip forward over invisible text, set up the iterator
8846 to deliver from overlay strings at the new position etc. So,
8847 use reseat_1 here. */
8848 reseat_1 (it, it->current.pos, 1);
8849
8850 /* We are now surely at a line start. */
8851 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8852 reordering is in effect. */
8853 it->continuation_lines_width = 0;
8854
8855 /* Move forward and see what y-distance we moved. First move to the
8856 start of the next line so that we get its height. We need this
8857 height to be able to tell whether we reached the specified
8858 y-distance. */
8859 SAVE_IT (it2, *it, it2data);
8860 it2.max_ascent = it2.max_descent = 0;
8861 do
8862 {
8863 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8864 MOVE_TO_POS | MOVE_TO_VPOS);
8865 }
8866 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8867 /* If we are in a display string which starts at START_POS,
8868 and that display string includes a newline, and we are
8869 right after that newline (i.e. at the beginning of a
8870 display line), exit the loop, because otherwise we will
8871 infloop, since move_it_to will see that it is already at
8872 START_POS and will not move. */
8873 || (it2.method == GET_FROM_STRING
8874 && IT_CHARPOS (it2) == start_pos
8875 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8876 xassert (IT_CHARPOS (*it) >= BEGV);
8877 SAVE_IT (it3, it2, it3data);
8878
8879 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8880 xassert (IT_CHARPOS (*it) >= BEGV);
8881 /* H is the actual vertical distance from the position in *IT
8882 and the starting position. */
8883 h = it2.current_y - it->current_y;
8884 /* NLINES is the distance in number of lines. */
8885 nlines = it2.vpos - it->vpos;
8886
8887 /* Correct IT's y and vpos position
8888 so that they are relative to the starting point. */
8889 it->vpos -= nlines;
8890 it->current_y -= h;
8891
8892 if (dy == 0)
8893 {
8894 /* DY == 0 means move to the start of the screen line. The
8895 value of nlines is > 0 if continuation lines were involved,
8896 or if the original IT position was at start of a line. */
8897 RESTORE_IT (it, it, it2data);
8898 if (nlines > 0)
8899 move_it_by_lines (it, nlines);
8900 /* The above code moves us to some position NLINES down,
8901 usually to its first glyph (leftmost in an L2R line), but
8902 that's not necessarily the start of the line, under bidi
8903 reordering. We want to get to the character position
8904 that is immediately after the newline of the previous
8905 line. */
8906 if (it->bidi_p
8907 && !it->continuation_lines_width
8908 && !STRINGP (it->string)
8909 && IT_CHARPOS (*it) > BEGV
8910 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8911 {
8912 EMACS_INT nl_pos =
8913 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8914
8915 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8916 }
8917 bidi_unshelve_cache (it3data, 1);
8918 }
8919 else
8920 {
8921 /* The y-position we try to reach, relative to *IT.
8922 Note that H has been subtracted in front of the if-statement. */
8923 int target_y = it->current_y + h - dy;
8924 int y0 = it3.current_y;
8925 int y1;
8926 int line_height;
8927
8928 RESTORE_IT (&it3, &it3, it3data);
8929 y1 = line_bottom_y (&it3);
8930 line_height = y1 - y0;
8931 RESTORE_IT (it, it, it2data);
8932 /* If we did not reach target_y, try to move further backward if
8933 we can. If we moved too far backward, try to move forward. */
8934 if (target_y < it->current_y
8935 /* This is heuristic. In a window that's 3 lines high, with
8936 a line height of 13 pixels each, recentering with point
8937 on the bottom line will try to move -39/2 = 19 pixels
8938 backward. Try to avoid moving into the first line. */
8939 && (it->current_y - target_y
8940 > min (window_box_height (it->w), line_height * 2 / 3))
8941 && IT_CHARPOS (*it) > BEGV)
8942 {
8943 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8944 target_y - it->current_y));
8945 dy = it->current_y - target_y;
8946 goto move_further_back;
8947 }
8948 else if (target_y >= it->current_y + line_height
8949 && IT_CHARPOS (*it) < ZV)
8950 {
8951 /* Should move forward by at least one line, maybe more.
8952
8953 Note: Calling move_it_by_lines can be expensive on
8954 terminal frames, where compute_motion is used (via
8955 vmotion) to do the job, when there are very long lines
8956 and truncate-lines is nil. That's the reason for
8957 treating terminal frames specially here. */
8958
8959 if (!FRAME_WINDOW_P (it->f))
8960 move_it_vertically (it, target_y - (it->current_y + line_height));
8961 else
8962 {
8963 do
8964 {
8965 move_it_by_lines (it, 1);
8966 }
8967 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8968 }
8969 }
8970 }
8971 }
8972
8973
8974 /* Move IT by a specified amount of pixel lines DY. DY negative means
8975 move backwards. DY = 0 means move to start of screen line. At the
8976 end, IT will be on the start of a screen line. */
8977
8978 void
8979 move_it_vertically (struct it *it, int dy)
8980 {
8981 if (dy <= 0)
8982 move_it_vertically_backward (it, -dy);
8983 else
8984 {
8985 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8986 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8987 MOVE_TO_POS | MOVE_TO_Y);
8988 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8989
8990 /* If buffer ends in ZV without a newline, move to the start of
8991 the line to satisfy the post-condition. */
8992 if (IT_CHARPOS (*it) == ZV
8993 && ZV > BEGV
8994 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8995 move_it_by_lines (it, 0);
8996 }
8997 }
8998
8999
9000 /* Move iterator IT past the end of the text line it is in. */
9001
9002 void
9003 move_it_past_eol (struct it *it)
9004 {
9005 enum move_it_result rc;
9006
9007 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9008 if (rc == MOVE_NEWLINE_OR_CR)
9009 set_iterator_to_next (it, 0);
9010 }
9011
9012
9013 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9014 negative means move up. DVPOS == 0 means move to the start of the
9015 screen line.
9016
9017 Optimization idea: If we would know that IT->f doesn't use
9018 a face with proportional font, we could be faster for
9019 truncate-lines nil. */
9020
9021 void
9022 move_it_by_lines (struct it *it, int dvpos)
9023 {
9024
9025 /* The commented-out optimization uses vmotion on terminals. This
9026 gives bad results, because elements like it->what, on which
9027 callers such as pos_visible_p rely, aren't updated. */
9028 /* struct position pos;
9029 if (!FRAME_WINDOW_P (it->f))
9030 {
9031 struct text_pos textpos;
9032
9033 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9034 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9035 reseat (it, textpos, 1);
9036 it->vpos += pos.vpos;
9037 it->current_y += pos.vpos;
9038 }
9039 else */
9040
9041 if (dvpos == 0)
9042 {
9043 /* DVPOS == 0 means move to the start of the screen line. */
9044 move_it_vertically_backward (it, 0);
9045 /* Let next call to line_bottom_y calculate real line height */
9046 last_height = 0;
9047 }
9048 else if (dvpos > 0)
9049 {
9050 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9051 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9052 {
9053 /* Only move to the next buffer position if we ended up in a
9054 string from display property, not in an overlay string
9055 (before-string or after-string). That is because the
9056 latter don't conceal the underlying buffer position, so
9057 we can ask to move the iterator to the exact position we
9058 are interested in. Note that, even if we are already at
9059 IT_CHARPOS (*it), the call below is not a no-op, as it
9060 will detect that we are at the end of the string, pop the
9061 iterator, and compute it->current_x and it->hpos
9062 correctly. */
9063 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9064 -1, -1, -1, MOVE_TO_POS);
9065 }
9066 }
9067 else
9068 {
9069 struct it it2;
9070 void *it2data = NULL;
9071 EMACS_INT start_charpos, i;
9072
9073 /* Start at the beginning of the screen line containing IT's
9074 position. This may actually move vertically backwards,
9075 in case of overlays, so adjust dvpos accordingly. */
9076 dvpos += it->vpos;
9077 move_it_vertically_backward (it, 0);
9078 dvpos -= it->vpos;
9079
9080 /* Go back -DVPOS visible lines and reseat the iterator there. */
9081 start_charpos = IT_CHARPOS (*it);
9082 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9083 back_to_previous_visible_line_start (it);
9084 reseat (it, it->current.pos, 1);
9085
9086 /* Move further back if we end up in a string or an image. */
9087 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9088 {
9089 /* First try to move to start of display line. */
9090 dvpos += it->vpos;
9091 move_it_vertically_backward (it, 0);
9092 dvpos -= it->vpos;
9093 if (IT_POS_VALID_AFTER_MOVE_P (it))
9094 break;
9095 /* If start of line is still in string or image,
9096 move further back. */
9097 back_to_previous_visible_line_start (it);
9098 reseat (it, it->current.pos, 1);
9099 dvpos--;
9100 }
9101
9102 it->current_x = it->hpos = 0;
9103
9104 /* Above call may have moved too far if continuation lines
9105 are involved. Scan forward and see if it did. */
9106 SAVE_IT (it2, *it, it2data);
9107 it2.vpos = it2.current_y = 0;
9108 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9109 it->vpos -= it2.vpos;
9110 it->current_y -= it2.current_y;
9111 it->current_x = it->hpos = 0;
9112
9113 /* If we moved too far back, move IT some lines forward. */
9114 if (it2.vpos > -dvpos)
9115 {
9116 int delta = it2.vpos + dvpos;
9117
9118 RESTORE_IT (&it2, &it2, it2data);
9119 SAVE_IT (it2, *it, it2data);
9120 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9121 /* Move back again if we got too far ahead. */
9122 if (IT_CHARPOS (*it) >= start_charpos)
9123 RESTORE_IT (it, &it2, it2data);
9124 else
9125 bidi_unshelve_cache (it2data, 1);
9126 }
9127 else
9128 RESTORE_IT (it, it, it2data);
9129 }
9130 }
9131
9132 /* Return 1 if IT points into the middle of a display vector. */
9133
9134 int
9135 in_display_vector_p (struct it *it)
9136 {
9137 return (it->method == GET_FROM_DISPLAY_VECTOR
9138 && it->current.dpvec_index > 0
9139 && it->dpvec + it->current.dpvec_index != it->dpend);
9140 }
9141
9142 \f
9143 /***********************************************************************
9144 Messages
9145 ***********************************************************************/
9146
9147
9148 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9149 to *Messages*. */
9150
9151 void
9152 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9153 {
9154 Lisp_Object args[3];
9155 Lisp_Object msg, fmt;
9156 char *buffer;
9157 EMACS_INT len;
9158 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9159 USE_SAFE_ALLOCA;
9160
9161 /* Do nothing if called asynchronously. Inserting text into
9162 a buffer may call after-change-functions and alike and
9163 that would means running Lisp asynchronously. */
9164 if (handling_signal)
9165 return;
9166
9167 fmt = msg = Qnil;
9168 GCPRO4 (fmt, msg, arg1, arg2);
9169
9170 args[0] = fmt = build_string (format);
9171 args[1] = arg1;
9172 args[2] = arg2;
9173 msg = Fformat (3, args);
9174
9175 len = SBYTES (msg) + 1;
9176 SAFE_ALLOCA (buffer, char *, len);
9177 memcpy (buffer, SDATA (msg), len);
9178
9179 message_dolog (buffer, len - 1, 1, 0);
9180 SAFE_FREE ();
9181
9182 UNGCPRO;
9183 }
9184
9185
9186 /* Output a newline in the *Messages* buffer if "needs" one. */
9187
9188 void
9189 message_log_maybe_newline (void)
9190 {
9191 if (message_log_need_newline)
9192 message_dolog ("", 0, 1, 0);
9193 }
9194
9195
9196 /* Add a string M of length NBYTES to the message log, optionally
9197 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9198 nonzero, means interpret the contents of M as multibyte. This
9199 function calls low-level routines in order to bypass text property
9200 hooks, etc. which might not be safe to run.
9201
9202 This may GC (insert may run before/after change hooks),
9203 so the buffer M must NOT point to a Lisp string. */
9204
9205 void
9206 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9207 {
9208 const unsigned char *msg = (const unsigned char *) m;
9209
9210 if (!NILP (Vmemory_full))
9211 return;
9212
9213 if (!NILP (Vmessage_log_max))
9214 {
9215 struct buffer *oldbuf;
9216 Lisp_Object oldpoint, oldbegv, oldzv;
9217 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9218 EMACS_INT point_at_end = 0;
9219 EMACS_INT zv_at_end = 0;
9220 Lisp_Object old_deactivate_mark, tem;
9221 struct gcpro gcpro1;
9222
9223 old_deactivate_mark = Vdeactivate_mark;
9224 oldbuf = current_buffer;
9225 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9226 BVAR (current_buffer, undo_list) = Qt;
9227
9228 oldpoint = message_dolog_marker1;
9229 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9230 oldbegv = message_dolog_marker2;
9231 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9232 oldzv = message_dolog_marker3;
9233 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9234 GCPRO1 (old_deactivate_mark);
9235
9236 if (PT == Z)
9237 point_at_end = 1;
9238 if (ZV == Z)
9239 zv_at_end = 1;
9240
9241 BEGV = BEG;
9242 BEGV_BYTE = BEG_BYTE;
9243 ZV = Z;
9244 ZV_BYTE = Z_BYTE;
9245 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9246
9247 /* Insert the string--maybe converting multibyte to single byte
9248 or vice versa, so that all the text fits the buffer. */
9249 if (multibyte
9250 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9251 {
9252 EMACS_INT i;
9253 int c, char_bytes;
9254 char work[1];
9255
9256 /* Convert a multibyte string to single-byte
9257 for the *Message* buffer. */
9258 for (i = 0; i < nbytes; i += char_bytes)
9259 {
9260 c = string_char_and_length (msg + i, &char_bytes);
9261 work[0] = (ASCII_CHAR_P (c)
9262 ? c
9263 : multibyte_char_to_unibyte (c));
9264 insert_1_both (work, 1, 1, 1, 0, 0);
9265 }
9266 }
9267 else if (! multibyte
9268 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9269 {
9270 EMACS_INT i;
9271 int c, char_bytes;
9272 unsigned char str[MAX_MULTIBYTE_LENGTH];
9273 /* Convert a single-byte string to multibyte
9274 for the *Message* buffer. */
9275 for (i = 0; i < nbytes; i++)
9276 {
9277 c = msg[i];
9278 MAKE_CHAR_MULTIBYTE (c);
9279 char_bytes = CHAR_STRING (c, str);
9280 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9281 }
9282 }
9283 else if (nbytes)
9284 insert_1 (m, nbytes, 1, 0, 0);
9285
9286 if (nlflag)
9287 {
9288 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9289 printmax_t dups;
9290 insert_1 ("\n", 1, 1, 0, 0);
9291
9292 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9293 this_bol = PT;
9294 this_bol_byte = PT_BYTE;
9295
9296 /* See if this line duplicates the previous one.
9297 If so, combine duplicates. */
9298 if (this_bol > BEG)
9299 {
9300 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9301 prev_bol = PT;
9302 prev_bol_byte = PT_BYTE;
9303
9304 dups = message_log_check_duplicate (prev_bol_byte,
9305 this_bol_byte);
9306 if (dups)
9307 {
9308 del_range_both (prev_bol, prev_bol_byte,
9309 this_bol, this_bol_byte, 0);
9310 if (dups > 1)
9311 {
9312 char dupstr[sizeof " [ times]"
9313 + INT_STRLEN_BOUND (printmax_t)];
9314 int duplen;
9315
9316 /* If you change this format, don't forget to also
9317 change message_log_check_duplicate. */
9318 sprintf (dupstr, " [%"pMd" times]", dups);
9319 duplen = strlen (dupstr);
9320 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9321 insert_1 (dupstr, duplen, 1, 0, 1);
9322 }
9323 }
9324 }
9325
9326 /* If we have more than the desired maximum number of lines
9327 in the *Messages* buffer now, delete the oldest ones.
9328 This is safe because we don't have undo in this buffer. */
9329
9330 if (NATNUMP (Vmessage_log_max))
9331 {
9332 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9333 -XFASTINT (Vmessage_log_max) - 1, 0);
9334 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9335 }
9336 }
9337 BEGV = XMARKER (oldbegv)->charpos;
9338 BEGV_BYTE = marker_byte_position (oldbegv);
9339
9340 if (zv_at_end)
9341 {
9342 ZV = Z;
9343 ZV_BYTE = Z_BYTE;
9344 }
9345 else
9346 {
9347 ZV = XMARKER (oldzv)->charpos;
9348 ZV_BYTE = marker_byte_position (oldzv);
9349 }
9350
9351 if (point_at_end)
9352 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9353 else
9354 /* We can't do Fgoto_char (oldpoint) because it will run some
9355 Lisp code. */
9356 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9357 XMARKER (oldpoint)->bytepos);
9358
9359 UNGCPRO;
9360 unchain_marker (XMARKER (oldpoint));
9361 unchain_marker (XMARKER (oldbegv));
9362 unchain_marker (XMARKER (oldzv));
9363
9364 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9365 set_buffer_internal (oldbuf);
9366 if (NILP (tem))
9367 windows_or_buffers_changed = old_windows_or_buffers_changed;
9368 message_log_need_newline = !nlflag;
9369 Vdeactivate_mark = old_deactivate_mark;
9370 }
9371 }
9372
9373
9374 /* We are at the end of the buffer after just having inserted a newline.
9375 (Note: We depend on the fact we won't be crossing the gap.)
9376 Check to see if the most recent message looks a lot like the previous one.
9377 Return 0 if different, 1 if the new one should just replace it, or a
9378 value N > 1 if we should also append " [N times]". */
9379
9380 static intmax_t
9381 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9382 {
9383 EMACS_INT i;
9384 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9385 int seen_dots = 0;
9386 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9387 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9388
9389 for (i = 0; i < len; i++)
9390 {
9391 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9392 seen_dots = 1;
9393 if (p1[i] != p2[i])
9394 return seen_dots;
9395 }
9396 p1 += len;
9397 if (*p1 == '\n')
9398 return 2;
9399 if (*p1++ == ' ' && *p1++ == '[')
9400 {
9401 char *pend;
9402 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9403 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9404 return n+1;
9405 }
9406 return 0;
9407 }
9408 \f
9409
9410 /* Display an echo area message M with a specified length of NBYTES
9411 bytes. The string may include null characters. If M is 0, clear
9412 out any existing message, and let the mini-buffer text show
9413 through.
9414
9415 This may GC, so the buffer M must NOT point to a Lisp string. */
9416
9417 void
9418 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9419 {
9420 /* First flush out any partial line written with print. */
9421 message_log_maybe_newline ();
9422 if (m)
9423 message_dolog (m, nbytes, 1, multibyte);
9424 message2_nolog (m, nbytes, multibyte);
9425 }
9426
9427
9428 /* The non-logging counterpart of message2. */
9429
9430 void
9431 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9432 {
9433 struct frame *sf = SELECTED_FRAME ();
9434 message_enable_multibyte = multibyte;
9435
9436 if (FRAME_INITIAL_P (sf))
9437 {
9438 if (noninteractive_need_newline)
9439 putc ('\n', stderr);
9440 noninteractive_need_newline = 0;
9441 if (m)
9442 fwrite (m, nbytes, 1, stderr);
9443 if (cursor_in_echo_area == 0)
9444 fprintf (stderr, "\n");
9445 fflush (stderr);
9446 }
9447 /* A null message buffer means that the frame hasn't really been
9448 initialized yet. Error messages get reported properly by
9449 cmd_error, so this must be just an informative message; toss it. */
9450 else if (INTERACTIVE
9451 && sf->glyphs_initialized_p
9452 && FRAME_MESSAGE_BUF (sf))
9453 {
9454 Lisp_Object mini_window;
9455 struct frame *f;
9456
9457 /* Get the frame containing the mini-buffer
9458 that the selected frame is using. */
9459 mini_window = FRAME_MINIBUF_WINDOW (sf);
9460 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9461
9462 FRAME_SAMPLE_VISIBILITY (f);
9463 if (FRAME_VISIBLE_P (sf)
9464 && ! FRAME_VISIBLE_P (f))
9465 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9466
9467 if (m)
9468 {
9469 set_message (m, Qnil, nbytes, multibyte);
9470 if (minibuffer_auto_raise)
9471 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9472 }
9473 else
9474 clear_message (1, 1);
9475
9476 do_pending_window_change (0);
9477 echo_area_display (1);
9478 do_pending_window_change (0);
9479 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9480 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9481 }
9482 }
9483
9484
9485 /* Display an echo area message M with a specified length of NBYTES
9486 bytes. The string may include null characters. If M is not a
9487 string, clear out any existing message, and let the mini-buffer
9488 text show through.
9489
9490 This function cancels echoing. */
9491
9492 void
9493 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9494 {
9495 struct gcpro gcpro1;
9496
9497 GCPRO1 (m);
9498 clear_message (1,1);
9499 cancel_echoing ();
9500
9501 /* First flush out any partial line written with print. */
9502 message_log_maybe_newline ();
9503 if (STRINGP (m))
9504 {
9505 char *buffer;
9506 USE_SAFE_ALLOCA;
9507
9508 SAFE_ALLOCA (buffer, char *, nbytes);
9509 memcpy (buffer, SDATA (m), nbytes);
9510 message_dolog (buffer, nbytes, 1, multibyte);
9511 SAFE_FREE ();
9512 }
9513 message3_nolog (m, nbytes, multibyte);
9514
9515 UNGCPRO;
9516 }
9517
9518
9519 /* The non-logging version of message3.
9520 This does not cancel echoing, because it is used for echoing.
9521 Perhaps we need to make a separate function for echoing
9522 and make this cancel echoing. */
9523
9524 void
9525 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9526 {
9527 struct frame *sf = SELECTED_FRAME ();
9528 message_enable_multibyte = multibyte;
9529
9530 if (FRAME_INITIAL_P (sf))
9531 {
9532 if (noninteractive_need_newline)
9533 putc ('\n', stderr);
9534 noninteractive_need_newline = 0;
9535 if (STRINGP (m))
9536 fwrite (SDATA (m), nbytes, 1, stderr);
9537 if (cursor_in_echo_area == 0)
9538 fprintf (stderr, "\n");
9539 fflush (stderr);
9540 }
9541 /* A null message buffer means that the frame hasn't really been
9542 initialized yet. Error messages get reported properly by
9543 cmd_error, so this must be just an informative message; toss it. */
9544 else if (INTERACTIVE
9545 && sf->glyphs_initialized_p
9546 && FRAME_MESSAGE_BUF (sf))
9547 {
9548 Lisp_Object mini_window;
9549 Lisp_Object frame;
9550 struct frame *f;
9551
9552 /* Get the frame containing the mini-buffer
9553 that the selected frame is using. */
9554 mini_window = FRAME_MINIBUF_WINDOW (sf);
9555 frame = XWINDOW (mini_window)->frame;
9556 f = XFRAME (frame);
9557
9558 FRAME_SAMPLE_VISIBILITY (f);
9559 if (FRAME_VISIBLE_P (sf)
9560 && !FRAME_VISIBLE_P (f))
9561 Fmake_frame_visible (frame);
9562
9563 if (STRINGP (m) && SCHARS (m) > 0)
9564 {
9565 set_message (NULL, m, nbytes, multibyte);
9566 if (minibuffer_auto_raise)
9567 Fraise_frame (frame);
9568 /* Assume we are not echoing.
9569 (If we are, echo_now will override this.) */
9570 echo_message_buffer = Qnil;
9571 }
9572 else
9573 clear_message (1, 1);
9574
9575 do_pending_window_change (0);
9576 echo_area_display (1);
9577 do_pending_window_change (0);
9578 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9579 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9580 }
9581 }
9582
9583
9584 /* Display a null-terminated echo area message M. If M is 0, clear
9585 out any existing message, and let the mini-buffer text show through.
9586
9587 The buffer M must continue to exist until after the echo area gets
9588 cleared or some other message gets displayed there. Do not pass
9589 text that is stored in a Lisp string. Do not pass text in a buffer
9590 that was alloca'd. */
9591
9592 void
9593 message1 (const char *m)
9594 {
9595 message2 (m, (m ? strlen (m) : 0), 0);
9596 }
9597
9598
9599 /* The non-logging counterpart of message1. */
9600
9601 void
9602 message1_nolog (const char *m)
9603 {
9604 message2_nolog (m, (m ? strlen (m) : 0), 0);
9605 }
9606
9607 /* Display a message M which contains a single %s
9608 which gets replaced with STRING. */
9609
9610 void
9611 message_with_string (const char *m, Lisp_Object string, int log)
9612 {
9613 CHECK_STRING (string);
9614
9615 if (noninteractive)
9616 {
9617 if (m)
9618 {
9619 if (noninteractive_need_newline)
9620 putc ('\n', stderr);
9621 noninteractive_need_newline = 0;
9622 fprintf (stderr, m, SDATA (string));
9623 if (!cursor_in_echo_area)
9624 fprintf (stderr, "\n");
9625 fflush (stderr);
9626 }
9627 }
9628 else if (INTERACTIVE)
9629 {
9630 /* The frame whose minibuffer we're going to display the message on.
9631 It may be larger than the selected frame, so we need
9632 to use its buffer, not the selected frame's buffer. */
9633 Lisp_Object mini_window;
9634 struct frame *f, *sf = SELECTED_FRAME ();
9635
9636 /* Get the frame containing the minibuffer
9637 that the selected frame is using. */
9638 mini_window = FRAME_MINIBUF_WINDOW (sf);
9639 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9640
9641 /* A null message buffer means that the frame hasn't really been
9642 initialized yet. Error messages get reported properly by
9643 cmd_error, so this must be just an informative message; toss it. */
9644 if (FRAME_MESSAGE_BUF (f))
9645 {
9646 Lisp_Object args[2], msg;
9647 struct gcpro gcpro1, gcpro2;
9648
9649 args[0] = build_string (m);
9650 args[1] = msg = string;
9651 GCPRO2 (args[0], msg);
9652 gcpro1.nvars = 2;
9653
9654 msg = Fformat (2, args);
9655
9656 if (log)
9657 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9658 else
9659 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9660
9661 UNGCPRO;
9662
9663 /* Print should start at the beginning of the message
9664 buffer next time. */
9665 message_buf_print = 0;
9666 }
9667 }
9668 }
9669
9670
9671 /* Dump an informative message to the minibuf. If M is 0, clear out
9672 any existing message, and let the mini-buffer text show through. */
9673
9674 static void
9675 vmessage (const char *m, va_list ap)
9676 {
9677 if (noninteractive)
9678 {
9679 if (m)
9680 {
9681 if (noninteractive_need_newline)
9682 putc ('\n', stderr);
9683 noninteractive_need_newline = 0;
9684 vfprintf (stderr, m, ap);
9685 if (cursor_in_echo_area == 0)
9686 fprintf (stderr, "\n");
9687 fflush (stderr);
9688 }
9689 }
9690 else if (INTERACTIVE)
9691 {
9692 /* The frame whose mini-buffer we're going to display the message
9693 on. It may be larger than the selected frame, so we need to
9694 use its buffer, not the selected frame's buffer. */
9695 Lisp_Object mini_window;
9696 struct frame *f, *sf = SELECTED_FRAME ();
9697
9698 /* Get the frame containing the mini-buffer
9699 that the selected frame is using. */
9700 mini_window = FRAME_MINIBUF_WINDOW (sf);
9701 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9702
9703 /* A null message buffer means that the frame hasn't really been
9704 initialized yet. Error messages get reported properly by
9705 cmd_error, so this must be just an informative message; toss
9706 it. */
9707 if (FRAME_MESSAGE_BUF (f))
9708 {
9709 if (m)
9710 {
9711 ptrdiff_t len;
9712
9713 len = doprnt (FRAME_MESSAGE_BUF (f),
9714 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9715
9716 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9717 }
9718 else
9719 message1 (0);
9720
9721 /* Print should start at the beginning of the message
9722 buffer next time. */
9723 message_buf_print = 0;
9724 }
9725 }
9726 }
9727
9728 void
9729 message (const char *m, ...)
9730 {
9731 va_list ap;
9732 va_start (ap, m);
9733 vmessage (m, ap);
9734 va_end (ap);
9735 }
9736
9737
9738 #if 0
9739 /* The non-logging version of message. */
9740
9741 void
9742 message_nolog (const char *m, ...)
9743 {
9744 Lisp_Object old_log_max;
9745 va_list ap;
9746 va_start (ap, m);
9747 old_log_max = Vmessage_log_max;
9748 Vmessage_log_max = Qnil;
9749 vmessage (m, ap);
9750 Vmessage_log_max = old_log_max;
9751 va_end (ap);
9752 }
9753 #endif
9754
9755
9756 /* Display the current message in the current mini-buffer. This is
9757 only called from error handlers in process.c, and is not time
9758 critical. */
9759
9760 void
9761 update_echo_area (void)
9762 {
9763 if (!NILP (echo_area_buffer[0]))
9764 {
9765 Lisp_Object string;
9766 string = Fcurrent_message ();
9767 message3 (string, SBYTES (string),
9768 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9769 }
9770 }
9771
9772
9773 /* Make sure echo area buffers in `echo_buffers' are live.
9774 If they aren't, make new ones. */
9775
9776 static void
9777 ensure_echo_area_buffers (void)
9778 {
9779 int i;
9780
9781 for (i = 0; i < 2; ++i)
9782 if (!BUFFERP (echo_buffer[i])
9783 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9784 {
9785 char name[30];
9786 Lisp_Object old_buffer;
9787 int j;
9788
9789 old_buffer = echo_buffer[i];
9790 sprintf (name, " *Echo Area %d*", i);
9791 echo_buffer[i] = Fget_buffer_create (build_string (name));
9792 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9793 /* to force word wrap in echo area -
9794 it was decided to postpone this*/
9795 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9796
9797 for (j = 0; j < 2; ++j)
9798 if (EQ (old_buffer, echo_area_buffer[j]))
9799 echo_area_buffer[j] = echo_buffer[i];
9800 }
9801 }
9802
9803
9804 /* Call FN with args A1..A4 with either the current or last displayed
9805 echo_area_buffer as current buffer.
9806
9807 WHICH zero means use the current message buffer
9808 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9809 from echo_buffer[] and clear it.
9810
9811 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9812 suitable buffer from echo_buffer[] and clear it.
9813
9814 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9815 that the current message becomes the last displayed one, make
9816 choose a suitable buffer for echo_area_buffer[0], and clear it.
9817
9818 Value is what FN returns. */
9819
9820 static int
9821 with_echo_area_buffer (struct window *w, int which,
9822 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9823 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9824 {
9825 Lisp_Object buffer;
9826 int this_one, the_other, clear_buffer_p, rc;
9827 int count = SPECPDL_INDEX ();
9828
9829 /* If buffers aren't live, make new ones. */
9830 ensure_echo_area_buffers ();
9831
9832 clear_buffer_p = 0;
9833
9834 if (which == 0)
9835 this_one = 0, the_other = 1;
9836 else if (which > 0)
9837 this_one = 1, the_other = 0;
9838 else
9839 {
9840 this_one = 0, the_other = 1;
9841 clear_buffer_p = 1;
9842
9843 /* We need a fresh one in case the current echo buffer equals
9844 the one containing the last displayed echo area message. */
9845 if (!NILP (echo_area_buffer[this_one])
9846 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9847 echo_area_buffer[this_one] = Qnil;
9848 }
9849
9850 /* Choose a suitable buffer from echo_buffer[] is we don't
9851 have one. */
9852 if (NILP (echo_area_buffer[this_one]))
9853 {
9854 echo_area_buffer[this_one]
9855 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9856 ? echo_buffer[the_other]
9857 : echo_buffer[this_one]);
9858 clear_buffer_p = 1;
9859 }
9860
9861 buffer = echo_area_buffer[this_one];
9862
9863 /* Don't get confused by reusing the buffer used for echoing
9864 for a different purpose. */
9865 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9866 cancel_echoing ();
9867
9868 record_unwind_protect (unwind_with_echo_area_buffer,
9869 with_echo_area_buffer_unwind_data (w));
9870
9871 /* Make the echo area buffer current. Note that for display
9872 purposes, it is not necessary that the displayed window's buffer
9873 == current_buffer, except for text property lookup. So, let's
9874 only set that buffer temporarily here without doing a full
9875 Fset_window_buffer. We must also change w->pointm, though,
9876 because otherwise an assertions in unshow_buffer fails, and Emacs
9877 aborts. */
9878 set_buffer_internal_1 (XBUFFER (buffer));
9879 if (w)
9880 {
9881 w->buffer = buffer;
9882 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9883 }
9884
9885 BVAR (current_buffer, undo_list) = Qt;
9886 BVAR (current_buffer, read_only) = Qnil;
9887 specbind (Qinhibit_read_only, Qt);
9888 specbind (Qinhibit_modification_hooks, Qt);
9889
9890 if (clear_buffer_p && Z > BEG)
9891 del_range (BEG, Z);
9892
9893 xassert (BEGV >= BEG);
9894 xassert (ZV <= Z && ZV >= BEGV);
9895
9896 rc = fn (a1, a2, a3, a4);
9897
9898 xassert (BEGV >= BEG);
9899 xassert (ZV <= Z && ZV >= BEGV);
9900
9901 unbind_to (count, Qnil);
9902 return rc;
9903 }
9904
9905
9906 /* Save state that should be preserved around the call to the function
9907 FN called in with_echo_area_buffer. */
9908
9909 static Lisp_Object
9910 with_echo_area_buffer_unwind_data (struct window *w)
9911 {
9912 int i = 0;
9913 Lisp_Object vector, tmp;
9914
9915 /* Reduce consing by keeping one vector in
9916 Vwith_echo_area_save_vector. */
9917 vector = Vwith_echo_area_save_vector;
9918 Vwith_echo_area_save_vector = Qnil;
9919
9920 if (NILP (vector))
9921 vector = Fmake_vector (make_number (7), Qnil);
9922
9923 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9924 ASET (vector, i, Vdeactivate_mark); ++i;
9925 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9926
9927 if (w)
9928 {
9929 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9930 ASET (vector, i, w->buffer); ++i;
9931 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9932 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9933 }
9934 else
9935 {
9936 int end = i + 4;
9937 for (; i < end; ++i)
9938 ASET (vector, i, Qnil);
9939 }
9940
9941 xassert (i == ASIZE (vector));
9942 return vector;
9943 }
9944
9945
9946 /* Restore global state from VECTOR which was created by
9947 with_echo_area_buffer_unwind_data. */
9948
9949 static Lisp_Object
9950 unwind_with_echo_area_buffer (Lisp_Object vector)
9951 {
9952 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9953 Vdeactivate_mark = AREF (vector, 1);
9954 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9955
9956 if (WINDOWP (AREF (vector, 3)))
9957 {
9958 struct window *w;
9959 Lisp_Object buffer, charpos, bytepos;
9960
9961 w = XWINDOW (AREF (vector, 3));
9962 buffer = AREF (vector, 4);
9963 charpos = AREF (vector, 5);
9964 bytepos = AREF (vector, 6);
9965
9966 w->buffer = buffer;
9967 set_marker_both (w->pointm, buffer,
9968 XFASTINT (charpos), XFASTINT (bytepos));
9969 }
9970
9971 Vwith_echo_area_save_vector = vector;
9972 return Qnil;
9973 }
9974
9975
9976 /* Set up the echo area for use by print functions. MULTIBYTE_P
9977 non-zero means we will print multibyte. */
9978
9979 void
9980 setup_echo_area_for_printing (int multibyte_p)
9981 {
9982 /* If we can't find an echo area any more, exit. */
9983 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9984 Fkill_emacs (Qnil);
9985
9986 ensure_echo_area_buffers ();
9987
9988 if (!message_buf_print)
9989 {
9990 /* A message has been output since the last time we printed.
9991 Choose a fresh echo area buffer. */
9992 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9993 echo_area_buffer[0] = echo_buffer[1];
9994 else
9995 echo_area_buffer[0] = echo_buffer[0];
9996
9997 /* Switch to that buffer and clear it. */
9998 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9999 BVAR (current_buffer, truncate_lines) = Qnil;
10000
10001 if (Z > BEG)
10002 {
10003 int count = SPECPDL_INDEX ();
10004 specbind (Qinhibit_read_only, Qt);
10005 /* Note that undo recording is always disabled. */
10006 del_range (BEG, Z);
10007 unbind_to (count, Qnil);
10008 }
10009 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10010
10011 /* Set up the buffer for the multibyteness we need. */
10012 if (multibyte_p
10013 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10014 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10015
10016 /* Raise the frame containing the echo area. */
10017 if (minibuffer_auto_raise)
10018 {
10019 struct frame *sf = SELECTED_FRAME ();
10020 Lisp_Object mini_window;
10021 mini_window = FRAME_MINIBUF_WINDOW (sf);
10022 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10023 }
10024
10025 message_log_maybe_newline ();
10026 message_buf_print = 1;
10027 }
10028 else
10029 {
10030 if (NILP (echo_area_buffer[0]))
10031 {
10032 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10033 echo_area_buffer[0] = echo_buffer[1];
10034 else
10035 echo_area_buffer[0] = echo_buffer[0];
10036 }
10037
10038 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10039 {
10040 /* Someone switched buffers between print requests. */
10041 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10042 BVAR (current_buffer, truncate_lines) = Qnil;
10043 }
10044 }
10045 }
10046
10047
10048 /* Display an echo area message in window W. Value is non-zero if W's
10049 height is changed. If display_last_displayed_message_p is
10050 non-zero, display the message that was last displayed, otherwise
10051 display the current message. */
10052
10053 static int
10054 display_echo_area (struct window *w)
10055 {
10056 int i, no_message_p, window_height_changed_p, count;
10057
10058 /* Temporarily disable garbage collections while displaying the echo
10059 area. This is done because a GC can print a message itself.
10060 That message would modify the echo area buffer's contents while a
10061 redisplay of the buffer is going on, and seriously confuse
10062 redisplay. */
10063 count = inhibit_garbage_collection ();
10064
10065 /* If there is no message, we must call display_echo_area_1
10066 nevertheless because it resizes the window. But we will have to
10067 reset the echo_area_buffer in question to nil at the end because
10068 with_echo_area_buffer will sets it to an empty buffer. */
10069 i = display_last_displayed_message_p ? 1 : 0;
10070 no_message_p = NILP (echo_area_buffer[i]);
10071
10072 window_height_changed_p
10073 = with_echo_area_buffer (w, display_last_displayed_message_p,
10074 display_echo_area_1,
10075 (intptr_t) w, Qnil, 0, 0);
10076
10077 if (no_message_p)
10078 echo_area_buffer[i] = Qnil;
10079
10080 unbind_to (count, Qnil);
10081 return window_height_changed_p;
10082 }
10083
10084
10085 /* Helper for display_echo_area. Display the current buffer which
10086 contains the current echo area message in window W, a mini-window,
10087 a pointer to which is passed in A1. A2..A4 are currently not used.
10088 Change the height of W so that all of the message is displayed.
10089 Value is non-zero if height of W was changed. */
10090
10091 static int
10092 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10093 {
10094 intptr_t i1 = a1;
10095 struct window *w = (struct window *) i1;
10096 Lisp_Object window;
10097 struct text_pos start;
10098 int window_height_changed_p = 0;
10099
10100 /* Do this before displaying, so that we have a large enough glyph
10101 matrix for the display. If we can't get enough space for the
10102 whole text, display the last N lines. That works by setting w->start. */
10103 window_height_changed_p = resize_mini_window (w, 0);
10104
10105 /* Use the starting position chosen by resize_mini_window. */
10106 SET_TEXT_POS_FROM_MARKER (start, w->start);
10107
10108 /* Display. */
10109 clear_glyph_matrix (w->desired_matrix);
10110 XSETWINDOW (window, w);
10111 try_window (window, start, 0);
10112
10113 return window_height_changed_p;
10114 }
10115
10116
10117 /* Resize the echo area window to exactly the size needed for the
10118 currently displayed message, if there is one. If a mini-buffer
10119 is active, don't shrink it. */
10120
10121 void
10122 resize_echo_area_exactly (void)
10123 {
10124 if (BUFFERP (echo_area_buffer[0])
10125 && WINDOWP (echo_area_window))
10126 {
10127 struct window *w = XWINDOW (echo_area_window);
10128 int resized_p;
10129 Lisp_Object resize_exactly;
10130
10131 if (minibuf_level == 0)
10132 resize_exactly = Qt;
10133 else
10134 resize_exactly = Qnil;
10135
10136 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10137 (intptr_t) w, resize_exactly,
10138 0, 0);
10139 if (resized_p)
10140 {
10141 ++windows_or_buffers_changed;
10142 ++update_mode_lines;
10143 redisplay_internal ();
10144 }
10145 }
10146 }
10147
10148
10149 /* Callback function for with_echo_area_buffer, when used from
10150 resize_echo_area_exactly. A1 contains a pointer to the window to
10151 resize, EXACTLY non-nil means resize the mini-window exactly to the
10152 size of the text displayed. A3 and A4 are not used. Value is what
10153 resize_mini_window returns. */
10154
10155 static int
10156 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10157 {
10158 intptr_t i1 = a1;
10159 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10160 }
10161
10162
10163 /* Resize mini-window W to fit the size of its contents. EXACT_P
10164 means size the window exactly to the size needed. Otherwise, it's
10165 only enlarged until W's buffer is empty.
10166
10167 Set W->start to the right place to begin display. If the whole
10168 contents fit, start at the beginning. Otherwise, start so as
10169 to make the end of the contents appear. This is particularly
10170 important for y-or-n-p, but seems desirable generally.
10171
10172 Value is non-zero if the window height has been changed. */
10173
10174 int
10175 resize_mini_window (struct window *w, int exact_p)
10176 {
10177 struct frame *f = XFRAME (w->frame);
10178 int window_height_changed_p = 0;
10179
10180 xassert (MINI_WINDOW_P (w));
10181
10182 /* By default, start display at the beginning. */
10183 set_marker_both (w->start, w->buffer,
10184 BUF_BEGV (XBUFFER (w->buffer)),
10185 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10186
10187 /* Don't resize windows while redisplaying a window; it would
10188 confuse redisplay functions when the size of the window they are
10189 displaying changes from under them. Such a resizing can happen,
10190 for instance, when which-func prints a long message while
10191 we are running fontification-functions. We're running these
10192 functions with safe_call which binds inhibit-redisplay to t. */
10193 if (!NILP (Vinhibit_redisplay))
10194 return 0;
10195
10196 /* Nil means don't try to resize. */
10197 if (NILP (Vresize_mini_windows)
10198 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10199 return 0;
10200
10201 if (!FRAME_MINIBUF_ONLY_P (f))
10202 {
10203 struct it it;
10204 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10205 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10206 int height, max_height;
10207 int unit = FRAME_LINE_HEIGHT (f);
10208 struct text_pos start;
10209 struct buffer *old_current_buffer = NULL;
10210
10211 if (current_buffer != XBUFFER (w->buffer))
10212 {
10213 old_current_buffer = current_buffer;
10214 set_buffer_internal (XBUFFER (w->buffer));
10215 }
10216
10217 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10218
10219 /* Compute the max. number of lines specified by the user. */
10220 if (FLOATP (Vmax_mini_window_height))
10221 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10222 else if (INTEGERP (Vmax_mini_window_height))
10223 max_height = XINT (Vmax_mini_window_height);
10224 else
10225 max_height = total_height / 4;
10226
10227 /* Correct that max. height if it's bogus. */
10228 max_height = max (1, max_height);
10229 max_height = min (total_height, max_height);
10230
10231 /* Find out the height of the text in the window. */
10232 if (it.line_wrap == TRUNCATE)
10233 height = 1;
10234 else
10235 {
10236 last_height = 0;
10237 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10238 if (it.max_ascent == 0 && it.max_descent == 0)
10239 height = it.current_y + last_height;
10240 else
10241 height = it.current_y + it.max_ascent + it.max_descent;
10242 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10243 height = (height + unit - 1) / unit;
10244 }
10245
10246 /* Compute a suitable window start. */
10247 if (height > max_height)
10248 {
10249 height = max_height;
10250 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10251 move_it_vertically_backward (&it, (height - 1) * unit);
10252 start = it.current.pos;
10253 }
10254 else
10255 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10256 SET_MARKER_FROM_TEXT_POS (w->start, start);
10257
10258 if (EQ (Vresize_mini_windows, Qgrow_only))
10259 {
10260 /* Let it grow only, until we display an empty message, in which
10261 case the window shrinks again. */
10262 if (height > WINDOW_TOTAL_LINES (w))
10263 {
10264 int old_height = WINDOW_TOTAL_LINES (w);
10265 freeze_window_starts (f, 1);
10266 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10267 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10268 }
10269 else if (height < WINDOW_TOTAL_LINES (w)
10270 && (exact_p || BEGV == ZV))
10271 {
10272 int old_height = WINDOW_TOTAL_LINES (w);
10273 freeze_window_starts (f, 0);
10274 shrink_mini_window (w);
10275 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10276 }
10277 }
10278 else
10279 {
10280 /* Always resize to exact size needed. */
10281 if (height > WINDOW_TOTAL_LINES (w))
10282 {
10283 int old_height = WINDOW_TOTAL_LINES (w);
10284 freeze_window_starts (f, 1);
10285 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10286 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10287 }
10288 else if (height < WINDOW_TOTAL_LINES (w))
10289 {
10290 int old_height = WINDOW_TOTAL_LINES (w);
10291 freeze_window_starts (f, 0);
10292 shrink_mini_window (w);
10293
10294 if (height)
10295 {
10296 freeze_window_starts (f, 1);
10297 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10298 }
10299
10300 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10301 }
10302 }
10303
10304 if (old_current_buffer)
10305 set_buffer_internal (old_current_buffer);
10306 }
10307
10308 return window_height_changed_p;
10309 }
10310
10311
10312 /* Value is the current message, a string, or nil if there is no
10313 current message. */
10314
10315 Lisp_Object
10316 current_message (void)
10317 {
10318 Lisp_Object msg;
10319
10320 if (!BUFFERP (echo_area_buffer[0]))
10321 msg = Qnil;
10322 else
10323 {
10324 with_echo_area_buffer (0, 0, current_message_1,
10325 (intptr_t) &msg, Qnil, 0, 0);
10326 if (NILP (msg))
10327 echo_area_buffer[0] = Qnil;
10328 }
10329
10330 return msg;
10331 }
10332
10333
10334 static int
10335 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10336 {
10337 intptr_t i1 = a1;
10338 Lisp_Object *msg = (Lisp_Object *) i1;
10339
10340 if (Z > BEG)
10341 *msg = make_buffer_string (BEG, Z, 1);
10342 else
10343 *msg = Qnil;
10344 return 0;
10345 }
10346
10347
10348 /* Push the current message on Vmessage_stack for later restoration
10349 by restore_message. Value is non-zero if the current message isn't
10350 empty. This is a relatively infrequent operation, so it's not
10351 worth optimizing. */
10352
10353 int
10354 push_message (void)
10355 {
10356 Lisp_Object msg;
10357 msg = current_message ();
10358 Vmessage_stack = Fcons (msg, Vmessage_stack);
10359 return STRINGP (msg);
10360 }
10361
10362
10363 /* Restore message display from the top of Vmessage_stack. */
10364
10365 void
10366 restore_message (void)
10367 {
10368 Lisp_Object msg;
10369
10370 xassert (CONSP (Vmessage_stack));
10371 msg = XCAR (Vmessage_stack);
10372 if (STRINGP (msg))
10373 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10374 else
10375 message3_nolog (msg, 0, 0);
10376 }
10377
10378
10379 /* Handler for record_unwind_protect calling pop_message. */
10380
10381 Lisp_Object
10382 pop_message_unwind (Lisp_Object dummy)
10383 {
10384 pop_message ();
10385 return Qnil;
10386 }
10387
10388 /* Pop the top-most entry off Vmessage_stack. */
10389
10390 static void
10391 pop_message (void)
10392 {
10393 xassert (CONSP (Vmessage_stack));
10394 Vmessage_stack = XCDR (Vmessage_stack);
10395 }
10396
10397
10398 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10399 exits. If the stack is not empty, we have a missing pop_message
10400 somewhere. */
10401
10402 void
10403 check_message_stack (void)
10404 {
10405 if (!NILP (Vmessage_stack))
10406 abort ();
10407 }
10408
10409
10410 /* Truncate to NCHARS what will be displayed in the echo area the next
10411 time we display it---but don't redisplay it now. */
10412
10413 void
10414 truncate_echo_area (EMACS_INT nchars)
10415 {
10416 if (nchars == 0)
10417 echo_area_buffer[0] = Qnil;
10418 /* A null message buffer means that the frame hasn't really been
10419 initialized yet. Error messages get reported properly by
10420 cmd_error, so this must be just an informative message; toss it. */
10421 else if (!noninteractive
10422 && INTERACTIVE
10423 && !NILP (echo_area_buffer[0]))
10424 {
10425 struct frame *sf = SELECTED_FRAME ();
10426 if (FRAME_MESSAGE_BUF (sf))
10427 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10428 }
10429 }
10430
10431
10432 /* Helper function for truncate_echo_area. Truncate the current
10433 message to at most NCHARS characters. */
10434
10435 static int
10436 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10437 {
10438 if (BEG + nchars < Z)
10439 del_range (BEG + nchars, Z);
10440 if (Z == BEG)
10441 echo_area_buffer[0] = Qnil;
10442 return 0;
10443 }
10444
10445
10446 /* Set the current message to a substring of S or STRING.
10447
10448 If STRING is a Lisp string, set the message to the first NBYTES
10449 bytes from STRING. NBYTES zero means use the whole string. If
10450 STRING is multibyte, the message will be displayed multibyte.
10451
10452 If S is not null, set the message to the first LEN bytes of S. LEN
10453 zero means use the whole string. MULTIBYTE_P non-zero means S is
10454 multibyte. Display the message multibyte in that case.
10455
10456 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10457 to t before calling set_message_1 (which calls insert).
10458 */
10459
10460 static void
10461 set_message (const char *s, Lisp_Object string,
10462 EMACS_INT nbytes, int multibyte_p)
10463 {
10464 message_enable_multibyte
10465 = ((s && multibyte_p)
10466 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10467
10468 with_echo_area_buffer (0, -1, set_message_1,
10469 (intptr_t) s, string, nbytes, multibyte_p);
10470 message_buf_print = 0;
10471 help_echo_showing_p = 0;
10472 }
10473
10474
10475 /* Helper function for set_message. Arguments have the same meaning
10476 as there, with A1 corresponding to S and A2 corresponding to STRING
10477 This function is called with the echo area buffer being
10478 current. */
10479
10480 static int
10481 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10482 {
10483 intptr_t i1 = a1;
10484 const char *s = (const char *) i1;
10485 const unsigned char *msg = (const unsigned char *) s;
10486 Lisp_Object string = a2;
10487
10488 /* Change multibyteness of the echo buffer appropriately. */
10489 if (message_enable_multibyte
10490 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10491 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10492
10493 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10494 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10495 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10496
10497 /* Insert new message at BEG. */
10498 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10499
10500 if (STRINGP (string))
10501 {
10502 EMACS_INT nchars;
10503
10504 if (nbytes == 0)
10505 nbytes = SBYTES (string);
10506 nchars = string_byte_to_char (string, nbytes);
10507
10508 /* This function takes care of single/multibyte conversion. We
10509 just have to ensure that the echo area buffer has the right
10510 setting of enable_multibyte_characters. */
10511 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10512 }
10513 else if (s)
10514 {
10515 if (nbytes == 0)
10516 nbytes = strlen (s);
10517
10518 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10519 {
10520 /* Convert from multi-byte to single-byte. */
10521 EMACS_INT i;
10522 int c, n;
10523 char work[1];
10524
10525 /* Convert a multibyte string to single-byte. */
10526 for (i = 0; i < nbytes; i += n)
10527 {
10528 c = string_char_and_length (msg + i, &n);
10529 work[0] = (ASCII_CHAR_P (c)
10530 ? c
10531 : multibyte_char_to_unibyte (c));
10532 insert_1_both (work, 1, 1, 1, 0, 0);
10533 }
10534 }
10535 else if (!multibyte_p
10536 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10537 {
10538 /* Convert from single-byte to multi-byte. */
10539 EMACS_INT i;
10540 int c, n;
10541 unsigned char str[MAX_MULTIBYTE_LENGTH];
10542
10543 /* Convert a single-byte string to multibyte. */
10544 for (i = 0; i < nbytes; i++)
10545 {
10546 c = msg[i];
10547 MAKE_CHAR_MULTIBYTE (c);
10548 n = CHAR_STRING (c, str);
10549 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10550 }
10551 }
10552 else
10553 insert_1 (s, nbytes, 1, 0, 0);
10554 }
10555
10556 return 0;
10557 }
10558
10559
10560 /* Clear messages. CURRENT_P non-zero means clear the current
10561 message. LAST_DISPLAYED_P non-zero means clear the message
10562 last displayed. */
10563
10564 void
10565 clear_message (int current_p, int last_displayed_p)
10566 {
10567 if (current_p)
10568 {
10569 echo_area_buffer[0] = Qnil;
10570 message_cleared_p = 1;
10571 }
10572
10573 if (last_displayed_p)
10574 echo_area_buffer[1] = Qnil;
10575
10576 message_buf_print = 0;
10577 }
10578
10579 /* Clear garbaged frames.
10580
10581 This function is used where the old redisplay called
10582 redraw_garbaged_frames which in turn called redraw_frame which in
10583 turn called clear_frame. The call to clear_frame was a source of
10584 flickering. I believe a clear_frame is not necessary. It should
10585 suffice in the new redisplay to invalidate all current matrices,
10586 and ensure a complete redisplay of all windows. */
10587
10588 static void
10589 clear_garbaged_frames (void)
10590 {
10591 if (frame_garbaged)
10592 {
10593 Lisp_Object tail, frame;
10594 int changed_count = 0;
10595
10596 FOR_EACH_FRAME (tail, frame)
10597 {
10598 struct frame *f = XFRAME (frame);
10599
10600 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10601 {
10602 if (f->resized_p)
10603 {
10604 Fredraw_frame (frame);
10605 f->force_flush_display_p = 1;
10606 }
10607 clear_current_matrices (f);
10608 changed_count++;
10609 f->garbaged = 0;
10610 f->resized_p = 0;
10611 }
10612 }
10613
10614 frame_garbaged = 0;
10615 if (changed_count)
10616 ++windows_or_buffers_changed;
10617 }
10618 }
10619
10620
10621 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10622 is non-zero update selected_frame. Value is non-zero if the
10623 mini-windows height has been changed. */
10624
10625 static int
10626 echo_area_display (int update_frame_p)
10627 {
10628 Lisp_Object mini_window;
10629 struct window *w;
10630 struct frame *f;
10631 int window_height_changed_p = 0;
10632 struct frame *sf = SELECTED_FRAME ();
10633
10634 mini_window = FRAME_MINIBUF_WINDOW (sf);
10635 w = XWINDOW (mini_window);
10636 f = XFRAME (WINDOW_FRAME (w));
10637
10638 /* Don't display if frame is invisible or not yet initialized. */
10639 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10640 return 0;
10641
10642 #ifdef HAVE_WINDOW_SYSTEM
10643 /* When Emacs starts, selected_frame may be the initial terminal
10644 frame. If we let this through, a message would be displayed on
10645 the terminal. */
10646 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10647 return 0;
10648 #endif /* HAVE_WINDOW_SYSTEM */
10649
10650 /* Redraw garbaged frames. */
10651 if (frame_garbaged)
10652 clear_garbaged_frames ();
10653
10654 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10655 {
10656 echo_area_window = mini_window;
10657 window_height_changed_p = display_echo_area (w);
10658 w->must_be_updated_p = 1;
10659
10660 /* Update the display, unless called from redisplay_internal.
10661 Also don't update the screen during redisplay itself. The
10662 update will happen at the end of redisplay, and an update
10663 here could cause confusion. */
10664 if (update_frame_p && !redisplaying_p)
10665 {
10666 int n = 0;
10667
10668 /* If the display update has been interrupted by pending
10669 input, update mode lines in the frame. Due to the
10670 pending input, it might have been that redisplay hasn't
10671 been called, so that mode lines above the echo area are
10672 garbaged. This looks odd, so we prevent it here. */
10673 if (!display_completed)
10674 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10675
10676 if (window_height_changed_p
10677 /* Don't do this if Emacs is shutting down. Redisplay
10678 needs to run hooks. */
10679 && !NILP (Vrun_hooks))
10680 {
10681 /* Must update other windows. Likewise as in other
10682 cases, don't let this update be interrupted by
10683 pending input. */
10684 int count = SPECPDL_INDEX ();
10685 specbind (Qredisplay_dont_pause, Qt);
10686 windows_or_buffers_changed = 1;
10687 redisplay_internal ();
10688 unbind_to (count, Qnil);
10689 }
10690 else if (FRAME_WINDOW_P (f) && n == 0)
10691 {
10692 /* Window configuration is the same as before.
10693 Can do with a display update of the echo area,
10694 unless we displayed some mode lines. */
10695 update_single_window (w, 1);
10696 FRAME_RIF (f)->flush_display (f);
10697 }
10698 else
10699 update_frame (f, 1, 1);
10700
10701 /* If cursor is in the echo area, make sure that the next
10702 redisplay displays the minibuffer, so that the cursor will
10703 be replaced with what the minibuffer wants. */
10704 if (cursor_in_echo_area)
10705 ++windows_or_buffers_changed;
10706 }
10707 }
10708 else if (!EQ (mini_window, selected_window))
10709 windows_or_buffers_changed++;
10710
10711 /* Last displayed message is now the current message. */
10712 echo_area_buffer[1] = echo_area_buffer[0];
10713 /* Inform read_char that we're not echoing. */
10714 echo_message_buffer = Qnil;
10715
10716 /* Prevent redisplay optimization in redisplay_internal by resetting
10717 this_line_start_pos. This is done because the mini-buffer now
10718 displays the message instead of its buffer text. */
10719 if (EQ (mini_window, selected_window))
10720 CHARPOS (this_line_start_pos) = 0;
10721
10722 return window_height_changed_p;
10723 }
10724
10725
10726 \f
10727 /***********************************************************************
10728 Mode Lines and Frame Titles
10729 ***********************************************************************/
10730
10731 /* A buffer for constructing non-propertized mode-line strings and
10732 frame titles in it; allocated from the heap in init_xdisp and
10733 resized as needed in store_mode_line_noprop_char. */
10734
10735 static char *mode_line_noprop_buf;
10736
10737 /* The buffer's end, and a current output position in it. */
10738
10739 static char *mode_line_noprop_buf_end;
10740 static char *mode_line_noprop_ptr;
10741
10742 #define MODE_LINE_NOPROP_LEN(start) \
10743 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10744
10745 static enum {
10746 MODE_LINE_DISPLAY = 0,
10747 MODE_LINE_TITLE,
10748 MODE_LINE_NOPROP,
10749 MODE_LINE_STRING
10750 } mode_line_target;
10751
10752 /* Alist that caches the results of :propertize.
10753 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10754 static Lisp_Object mode_line_proptrans_alist;
10755
10756 /* List of strings making up the mode-line. */
10757 static Lisp_Object mode_line_string_list;
10758
10759 /* Base face property when building propertized mode line string. */
10760 static Lisp_Object mode_line_string_face;
10761 static Lisp_Object mode_line_string_face_prop;
10762
10763
10764 /* Unwind data for mode line strings */
10765
10766 static Lisp_Object Vmode_line_unwind_vector;
10767
10768 static Lisp_Object
10769 format_mode_line_unwind_data (struct buffer *obuf,
10770 Lisp_Object owin,
10771 int save_proptrans)
10772 {
10773 Lisp_Object vector, tmp;
10774
10775 /* Reduce consing by keeping one vector in
10776 Vwith_echo_area_save_vector. */
10777 vector = Vmode_line_unwind_vector;
10778 Vmode_line_unwind_vector = Qnil;
10779
10780 if (NILP (vector))
10781 vector = Fmake_vector (make_number (8), Qnil);
10782
10783 ASET (vector, 0, make_number (mode_line_target));
10784 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10785 ASET (vector, 2, mode_line_string_list);
10786 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10787 ASET (vector, 4, mode_line_string_face);
10788 ASET (vector, 5, mode_line_string_face_prop);
10789
10790 if (obuf)
10791 XSETBUFFER (tmp, obuf);
10792 else
10793 tmp = Qnil;
10794 ASET (vector, 6, tmp);
10795 ASET (vector, 7, owin);
10796
10797 return vector;
10798 }
10799
10800 static Lisp_Object
10801 unwind_format_mode_line (Lisp_Object vector)
10802 {
10803 mode_line_target = XINT (AREF (vector, 0));
10804 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10805 mode_line_string_list = AREF (vector, 2);
10806 if (! EQ (AREF (vector, 3), Qt))
10807 mode_line_proptrans_alist = AREF (vector, 3);
10808 mode_line_string_face = AREF (vector, 4);
10809 mode_line_string_face_prop = AREF (vector, 5);
10810
10811 if (!NILP (AREF (vector, 7)))
10812 /* Select window before buffer, since it may change the buffer. */
10813 Fselect_window (AREF (vector, 7), Qt);
10814
10815 if (!NILP (AREF (vector, 6)))
10816 {
10817 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10818 ASET (vector, 6, Qnil);
10819 }
10820
10821 Vmode_line_unwind_vector = vector;
10822 return Qnil;
10823 }
10824
10825
10826 /* Store a single character C for the frame title in mode_line_noprop_buf.
10827 Re-allocate mode_line_noprop_buf if necessary. */
10828
10829 static void
10830 store_mode_line_noprop_char (char c)
10831 {
10832 /* If output position has reached the end of the allocated buffer,
10833 increase the buffer's size. */
10834 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10835 {
10836 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10837 ptrdiff_t size = len;
10838 mode_line_noprop_buf =
10839 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10840 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10841 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10842 }
10843
10844 *mode_line_noprop_ptr++ = c;
10845 }
10846
10847
10848 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10849 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10850 characters that yield more columns than PRECISION; PRECISION <= 0
10851 means copy the whole string. Pad with spaces until FIELD_WIDTH
10852 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10853 pad. Called from display_mode_element when it is used to build a
10854 frame title. */
10855
10856 static int
10857 store_mode_line_noprop (const char *string, int field_width, int precision)
10858 {
10859 const unsigned char *str = (const unsigned char *) string;
10860 int n = 0;
10861 EMACS_INT dummy, nbytes;
10862
10863 /* Copy at most PRECISION chars from STR. */
10864 nbytes = strlen (string);
10865 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10866 while (nbytes--)
10867 store_mode_line_noprop_char (*str++);
10868
10869 /* Fill up with spaces until FIELD_WIDTH reached. */
10870 while (field_width > 0
10871 && n < field_width)
10872 {
10873 store_mode_line_noprop_char (' ');
10874 ++n;
10875 }
10876
10877 return n;
10878 }
10879
10880 /***********************************************************************
10881 Frame Titles
10882 ***********************************************************************/
10883
10884 #ifdef HAVE_WINDOW_SYSTEM
10885
10886 /* Set the title of FRAME, if it has changed. The title format is
10887 Vicon_title_format if FRAME is iconified, otherwise it is
10888 frame_title_format. */
10889
10890 static void
10891 x_consider_frame_title (Lisp_Object frame)
10892 {
10893 struct frame *f = XFRAME (frame);
10894
10895 if (FRAME_WINDOW_P (f)
10896 || FRAME_MINIBUF_ONLY_P (f)
10897 || f->explicit_name)
10898 {
10899 /* Do we have more than one visible frame on this X display? */
10900 Lisp_Object tail;
10901 Lisp_Object fmt;
10902 ptrdiff_t title_start;
10903 char *title;
10904 ptrdiff_t len;
10905 struct it it;
10906 int count = SPECPDL_INDEX ();
10907
10908 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10909 {
10910 Lisp_Object other_frame = XCAR (tail);
10911 struct frame *tf = XFRAME (other_frame);
10912
10913 if (tf != f
10914 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10915 && !FRAME_MINIBUF_ONLY_P (tf)
10916 && !EQ (other_frame, tip_frame)
10917 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10918 break;
10919 }
10920
10921 /* Set global variable indicating that multiple frames exist. */
10922 multiple_frames = CONSP (tail);
10923
10924 /* Switch to the buffer of selected window of the frame. Set up
10925 mode_line_target so that display_mode_element will output into
10926 mode_line_noprop_buf; then display the title. */
10927 record_unwind_protect (unwind_format_mode_line,
10928 format_mode_line_unwind_data
10929 (current_buffer, selected_window, 0));
10930
10931 Fselect_window (f->selected_window, Qt);
10932 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10933 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10934
10935 mode_line_target = MODE_LINE_TITLE;
10936 title_start = MODE_LINE_NOPROP_LEN (0);
10937 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10938 NULL, DEFAULT_FACE_ID);
10939 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10940 len = MODE_LINE_NOPROP_LEN (title_start);
10941 title = mode_line_noprop_buf + title_start;
10942 unbind_to (count, Qnil);
10943
10944 /* Set the title only if it's changed. This avoids consing in
10945 the common case where it hasn't. (If it turns out that we've
10946 already wasted too much time by walking through the list with
10947 display_mode_element, then we might need to optimize at a
10948 higher level than this.) */
10949 if (! STRINGP (f->name)
10950 || SBYTES (f->name) != len
10951 || memcmp (title, SDATA (f->name), len) != 0)
10952 x_implicitly_set_name (f, make_string (title, len), Qnil);
10953 }
10954 }
10955
10956 #endif /* not HAVE_WINDOW_SYSTEM */
10957
10958
10959
10960 \f
10961 /***********************************************************************
10962 Menu Bars
10963 ***********************************************************************/
10964
10965
10966 /* Prepare for redisplay by updating menu-bar item lists when
10967 appropriate. This can call eval. */
10968
10969 void
10970 prepare_menu_bars (void)
10971 {
10972 int all_windows;
10973 struct gcpro gcpro1, gcpro2;
10974 struct frame *f;
10975 Lisp_Object tooltip_frame;
10976
10977 #ifdef HAVE_WINDOW_SYSTEM
10978 tooltip_frame = tip_frame;
10979 #else
10980 tooltip_frame = Qnil;
10981 #endif
10982
10983 /* Update all frame titles based on their buffer names, etc. We do
10984 this before the menu bars so that the buffer-menu will show the
10985 up-to-date frame titles. */
10986 #ifdef HAVE_WINDOW_SYSTEM
10987 if (windows_or_buffers_changed || update_mode_lines)
10988 {
10989 Lisp_Object tail, frame;
10990
10991 FOR_EACH_FRAME (tail, frame)
10992 {
10993 f = XFRAME (frame);
10994 if (!EQ (frame, tooltip_frame)
10995 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10996 x_consider_frame_title (frame);
10997 }
10998 }
10999 #endif /* HAVE_WINDOW_SYSTEM */
11000
11001 /* Update the menu bar item lists, if appropriate. This has to be
11002 done before any actual redisplay or generation of display lines. */
11003 all_windows = (update_mode_lines
11004 || buffer_shared > 1
11005 || windows_or_buffers_changed);
11006 if (all_windows)
11007 {
11008 Lisp_Object tail, frame;
11009 int count = SPECPDL_INDEX ();
11010 /* 1 means that update_menu_bar has run its hooks
11011 so any further calls to update_menu_bar shouldn't do so again. */
11012 int menu_bar_hooks_run = 0;
11013
11014 record_unwind_save_match_data ();
11015
11016 FOR_EACH_FRAME (tail, frame)
11017 {
11018 f = XFRAME (frame);
11019
11020 /* Ignore tooltip frame. */
11021 if (EQ (frame, tooltip_frame))
11022 continue;
11023
11024 /* If a window on this frame changed size, report that to
11025 the user and clear the size-change flag. */
11026 if (FRAME_WINDOW_SIZES_CHANGED (f))
11027 {
11028 Lisp_Object functions;
11029
11030 /* Clear flag first in case we get an error below. */
11031 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11032 functions = Vwindow_size_change_functions;
11033 GCPRO2 (tail, functions);
11034
11035 while (CONSP (functions))
11036 {
11037 if (!EQ (XCAR (functions), Qt))
11038 call1 (XCAR (functions), frame);
11039 functions = XCDR (functions);
11040 }
11041 UNGCPRO;
11042 }
11043
11044 GCPRO1 (tail);
11045 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11046 #ifdef HAVE_WINDOW_SYSTEM
11047 update_tool_bar (f, 0);
11048 #endif
11049 #ifdef HAVE_NS
11050 if (windows_or_buffers_changed
11051 && FRAME_NS_P (f))
11052 ns_set_doc_edited (f, Fbuffer_modified_p
11053 (XWINDOW (f->selected_window)->buffer));
11054 #endif
11055 UNGCPRO;
11056 }
11057
11058 unbind_to (count, Qnil);
11059 }
11060 else
11061 {
11062 struct frame *sf = SELECTED_FRAME ();
11063 update_menu_bar (sf, 1, 0);
11064 #ifdef HAVE_WINDOW_SYSTEM
11065 update_tool_bar (sf, 1);
11066 #endif
11067 }
11068 }
11069
11070
11071 /* Update the menu bar item list for frame F. This has to be done
11072 before we start to fill in any display lines, because it can call
11073 eval.
11074
11075 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11076
11077 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11078 already ran the menu bar hooks for this redisplay, so there
11079 is no need to run them again. The return value is the
11080 updated value of this flag, to pass to the next call. */
11081
11082 static int
11083 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11084 {
11085 Lisp_Object window;
11086 register struct window *w;
11087
11088 /* If called recursively during a menu update, do nothing. This can
11089 happen when, for instance, an activate-menubar-hook causes a
11090 redisplay. */
11091 if (inhibit_menubar_update)
11092 return hooks_run;
11093
11094 window = FRAME_SELECTED_WINDOW (f);
11095 w = XWINDOW (window);
11096
11097 if (FRAME_WINDOW_P (f)
11098 ?
11099 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11100 || defined (HAVE_NS) || defined (USE_GTK)
11101 FRAME_EXTERNAL_MENU_BAR (f)
11102 #else
11103 FRAME_MENU_BAR_LINES (f) > 0
11104 #endif
11105 : FRAME_MENU_BAR_LINES (f) > 0)
11106 {
11107 /* If the user has switched buffers or windows, we need to
11108 recompute to reflect the new bindings. But we'll
11109 recompute when update_mode_lines is set too; that means
11110 that people can use force-mode-line-update to request
11111 that the menu bar be recomputed. The adverse effect on
11112 the rest of the redisplay algorithm is about the same as
11113 windows_or_buffers_changed anyway. */
11114 if (windows_or_buffers_changed
11115 /* This used to test w->update_mode_line, but we believe
11116 there is no need to recompute the menu in that case. */
11117 || update_mode_lines
11118 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11119 < BUF_MODIFF (XBUFFER (w->buffer)))
11120 != !NILP (w->last_had_star))
11121 || ((!NILP (Vtransient_mark_mode)
11122 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11123 != !NILP (w->region_showing)))
11124 {
11125 struct buffer *prev = current_buffer;
11126 int count = SPECPDL_INDEX ();
11127
11128 specbind (Qinhibit_menubar_update, Qt);
11129
11130 set_buffer_internal_1 (XBUFFER (w->buffer));
11131 if (save_match_data)
11132 record_unwind_save_match_data ();
11133 if (NILP (Voverriding_local_map_menu_flag))
11134 {
11135 specbind (Qoverriding_terminal_local_map, Qnil);
11136 specbind (Qoverriding_local_map, Qnil);
11137 }
11138
11139 if (!hooks_run)
11140 {
11141 /* Run the Lucid hook. */
11142 safe_run_hooks (Qactivate_menubar_hook);
11143
11144 /* If it has changed current-menubar from previous value,
11145 really recompute the menu-bar from the value. */
11146 if (! NILP (Vlucid_menu_bar_dirty_flag))
11147 call0 (Qrecompute_lucid_menubar);
11148
11149 safe_run_hooks (Qmenu_bar_update_hook);
11150
11151 hooks_run = 1;
11152 }
11153
11154 XSETFRAME (Vmenu_updating_frame, f);
11155 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11156
11157 /* Redisplay the menu bar in case we changed it. */
11158 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11159 || defined (HAVE_NS) || defined (USE_GTK)
11160 if (FRAME_WINDOW_P (f))
11161 {
11162 #if defined (HAVE_NS)
11163 /* All frames on Mac OS share the same menubar. So only
11164 the selected frame should be allowed to set it. */
11165 if (f == SELECTED_FRAME ())
11166 #endif
11167 set_frame_menubar (f, 0, 0);
11168 }
11169 else
11170 /* On a terminal screen, the menu bar is an ordinary screen
11171 line, and this makes it get updated. */
11172 w->update_mode_line = Qt;
11173 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11174 /* In the non-toolkit version, the menu bar is an ordinary screen
11175 line, and this makes it get updated. */
11176 w->update_mode_line = Qt;
11177 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11178
11179 unbind_to (count, Qnil);
11180 set_buffer_internal_1 (prev);
11181 }
11182 }
11183
11184 return hooks_run;
11185 }
11186
11187
11188 \f
11189 /***********************************************************************
11190 Output Cursor
11191 ***********************************************************************/
11192
11193 #ifdef HAVE_WINDOW_SYSTEM
11194
11195 /* EXPORT:
11196 Nominal cursor position -- where to draw output.
11197 HPOS and VPOS are window relative glyph matrix coordinates.
11198 X and Y are window relative pixel coordinates. */
11199
11200 struct cursor_pos output_cursor;
11201
11202
11203 /* EXPORT:
11204 Set the global variable output_cursor to CURSOR. All cursor
11205 positions are relative to updated_window. */
11206
11207 void
11208 set_output_cursor (struct cursor_pos *cursor)
11209 {
11210 output_cursor.hpos = cursor->hpos;
11211 output_cursor.vpos = cursor->vpos;
11212 output_cursor.x = cursor->x;
11213 output_cursor.y = cursor->y;
11214 }
11215
11216
11217 /* EXPORT for RIF:
11218 Set a nominal cursor position.
11219
11220 HPOS and VPOS are column/row positions in a window glyph matrix. X
11221 and Y are window text area relative pixel positions.
11222
11223 If this is done during an update, updated_window will contain the
11224 window that is being updated and the position is the future output
11225 cursor position for that window. If updated_window is null, use
11226 selected_window and display the cursor at the given position. */
11227
11228 void
11229 x_cursor_to (int vpos, int hpos, int y, int x)
11230 {
11231 struct window *w;
11232
11233 /* If updated_window is not set, work on selected_window. */
11234 if (updated_window)
11235 w = updated_window;
11236 else
11237 w = XWINDOW (selected_window);
11238
11239 /* Set the output cursor. */
11240 output_cursor.hpos = hpos;
11241 output_cursor.vpos = vpos;
11242 output_cursor.x = x;
11243 output_cursor.y = y;
11244
11245 /* If not called as part of an update, really display the cursor.
11246 This will also set the cursor position of W. */
11247 if (updated_window == NULL)
11248 {
11249 BLOCK_INPUT;
11250 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11251 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11252 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11253 UNBLOCK_INPUT;
11254 }
11255 }
11256
11257 #endif /* HAVE_WINDOW_SYSTEM */
11258
11259 \f
11260 /***********************************************************************
11261 Tool-bars
11262 ***********************************************************************/
11263
11264 #ifdef HAVE_WINDOW_SYSTEM
11265
11266 /* Where the mouse was last time we reported a mouse event. */
11267
11268 FRAME_PTR last_mouse_frame;
11269
11270 /* Tool-bar item index of the item on which a mouse button was pressed
11271 or -1. */
11272
11273 int last_tool_bar_item;
11274
11275
11276 static Lisp_Object
11277 update_tool_bar_unwind (Lisp_Object frame)
11278 {
11279 selected_frame = frame;
11280 return Qnil;
11281 }
11282
11283 /* Update the tool-bar item list for frame F. This has to be done
11284 before we start to fill in any display lines. Called from
11285 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11286 and restore it here. */
11287
11288 static void
11289 update_tool_bar (struct frame *f, int save_match_data)
11290 {
11291 #if defined (USE_GTK) || defined (HAVE_NS)
11292 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11293 #else
11294 int do_update = WINDOWP (f->tool_bar_window)
11295 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11296 #endif
11297
11298 if (do_update)
11299 {
11300 Lisp_Object window;
11301 struct window *w;
11302
11303 window = FRAME_SELECTED_WINDOW (f);
11304 w = XWINDOW (window);
11305
11306 /* If the user has switched buffers or windows, we need to
11307 recompute to reflect the new bindings. But we'll
11308 recompute when update_mode_lines is set too; that means
11309 that people can use force-mode-line-update to request
11310 that the menu bar be recomputed. The adverse effect on
11311 the rest of the redisplay algorithm is about the same as
11312 windows_or_buffers_changed anyway. */
11313 if (windows_or_buffers_changed
11314 || !NILP (w->update_mode_line)
11315 || update_mode_lines
11316 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11317 < BUF_MODIFF (XBUFFER (w->buffer)))
11318 != !NILP (w->last_had_star))
11319 || ((!NILP (Vtransient_mark_mode)
11320 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11321 != !NILP (w->region_showing)))
11322 {
11323 struct buffer *prev = current_buffer;
11324 int count = SPECPDL_INDEX ();
11325 Lisp_Object frame, new_tool_bar;
11326 int new_n_tool_bar;
11327 struct gcpro gcpro1;
11328
11329 /* Set current_buffer to the buffer of the selected
11330 window of the frame, so that we get the right local
11331 keymaps. */
11332 set_buffer_internal_1 (XBUFFER (w->buffer));
11333
11334 /* Save match data, if we must. */
11335 if (save_match_data)
11336 record_unwind_save_match_data ();
11337
11338 /* Make sure that we don't accidentally use bogus keymaps. */
11339 if (NILP (Voverriding_local_map_menu_flag))
11340 {
11341 specbind (Qoverriding_terminal_local_map, Qnil);
11342 specbind (Qoverriding_local_map, Qnil);
11343 }
11344
11345 GCPRO1 (new_tool_bar);
11346
11347 /* We must temporarily set the selected frame to this frame
11348 before calling tool_bar_items, because the calculation of
11349 the tool-bar keymap uses the selected frame (see
11350 `tool-bar-make-keymap' in tool-bar.el). */
11351 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11352 XSETFRAME (frame, f);
11353 selected_frame = frame;
11354
11355 /* Build desired tool-bar items from keymaps. */
11356 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11357 &new_n_tool_bar);
11358
11359 /* Redisplay the tool-bar if we changed it. */
11360 if (new_n_tool_bar != f->n_tool_bar_items
11361 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11362 {
11363 /* Redisplay that happens asynchronously due to an expose event
11364 may access f->tool_bar_items. Make sure we update both
11365 variables within BLOCK_INPUT so no such event interrupts. */
11366 BLOCK_INPUT;
11367 f->tool_bar_items = new_tool_bar;
11368 f->n_tool_bar_items = new_n_tool_bar;
11369 w->update_mode_line = Qt;
11370 UNBLOCK_INPUT;
11371 }
11372
11373 UNGCPRO;
11374
11375 unbind_to (count, Qnil);
11376 set_buffer_internal_1 (prev);
11377 }
11378 }
11379 }
11380
11381
11382 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11383 F's desired tool-bar contents. F->tool_bar_items must have
11384 been set up previously by calling prepare_menu_bars. */
11385
11386 static void
11387 build_desired_tool_bar_string (struct frame *f)
11388 {
11389 int i, size, size_needed;
11390 struct gcpro gcpro1, gcpro2, gcpro3;
11391 Lisp_Object image, plist, props;
11392
11393 image = plist = props = Qnil;
11394 GCPRO3 (image, plist, props);
11395
11396 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11397 Otherwise, make a new string. */
11398
11399 /* The size of the string we might be able to reuse. */
11400 size = (STRINGP (f->desired_tool_bar_string)
11401 ? SCHARS (f->desired_tool_bar_string)
11402 : 0);
11403
11404 /* We need one space in the string for each image. */
11405 size_needed = f->n_tool_bar_items;
11406
11407 /* Reuse f->desired_tool_bar_string, if possible. */
11408 if (size < size_needed || NILP (f->desired_tool_bar_string))
11409 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11410 make_number (' '));
11411 else
11412 {
11413 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11414 Fremove_text_properties (make_number (0), make_number (size),
11415 props, f->desired_tool_bar_string);
11416 }
11417
11418 /* Put a `display' property on the string for the images to display,
11419 put a `menu_item' property on tool-bar items with a value that
11420 is the index of the item in F's tool-bar item vector. */
11421 for (i = 0; i < f->n_tool_bar_items; ++i)
11422 {
11423 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11424
11425 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11426 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11427 int hmargin, vmargin, relief, idx, end;
11428
11429 /* If image is a vector, choose the image according to the
11430 button state. */
11431 image = PROP (TOOL_BAR_ITEM_IMAGES);
11432 if (VECTORP (image))
11433 {
11434 if (enabled_p)
11435 idx = (selected_p
11436 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11437 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11438 else
11439 idx = (selected_p
11440 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11441 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11442
11443 xassert (ASIZE (image) >= idx);
11444 image = AREF (image, idx);
11445 }
11446 else
11447 idx = -1;
11448
11449 /* Ignore invalid image specifications. */
11450 if (!valid_image_p (image))
11451 continue;
11452
11453 /* Display the tool-bar button pressed, or depressed. */
11454 plist = Fcopy_sequence (XCDR (image));
11455
11456 /* Compute margin and relief to draw. */
11457 relief = (tool_bar_button_relief >= 0
11458 ? tool_bar_button_relief
11459 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11460 hmargin = vmargin = relief;
11461
11462 if (INTEGERP (Vtool_bar_button_margin)
11463 && XINT (Vtool_bar_button_margin) > 0)
11464 {
11465 hmargin += XFASTINT (Vtool_bar_button_margin);
11466 vmargin += XFASTINT (Vtool_bar_button_margin);
11467 }
11468 else if (CONSP (Vtool_bar_button_margin))
11469 {
11470 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11471 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11472 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11473
11474 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11475 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11476 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11477 }
11478
11479 if (auto_raise_tool_bar_buttons_p)
11480 {
11481 /* Add a `:relief' property to the image spec if the item is
11482 selected. */
11483 if (selected_p)
11484 {
11485 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11486 hmargin -= relief;
11487 vmargin -= relief;
11488 }
11489 }
11490 else
11491 {
11492 /* If image is selected, display it pressed, i.e. with a
11493 negative relief. If it's not selected, display it with a
11494 raised relief. */
11495 plist = Fplist_put (plist, QCrelief,
11496 (selected_p
11497 ? make_number (-relief)
11498 : make_number (relief)));
11499 hmargin -= relief;
11500 vmargin -= relief;
11501 }
11502
11503 /* Put a margin around the image. */
11504 if (hmargin || vmargin)
11505 {
11506 if (hmargin == vmargin)
11507 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11508 else
11509 plist = Fplist_put (plist, QCmargin,
11510 Fcons (make_number (hmargin),
11511 make_number (vmargin)));
11512 }
11513
11514 /* If button is not enabled, and we don't have special images
11515 for the disabled state, make the image appear disabled by
11516 applying an appropriate algorithm to it. */
11517 if (!enabled_p && idx < 0)
11518 plist = Fplist_put (plist, QCconversion, Qdisabled);
11519
11520 /* Put a `display' text property on the string for the image to
11521 display. Put a `menu-item' property on the string that gives
11522 the start of this item's properties in the tool-bar items
11523 vector. */
11524 image = Fcons (Qimage, plist);
11525 props = list4 (Qdisplay, image,
11526 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11527
11528 /* Let the last image hide all remaining spaces in the tool bar
11529 string. The string can be longer than needed when we reuse a
11530 previous string. */
11531 if (i + 1 == f->n_tool_bar_items)
11532 end = SCHARS (f->desired_tool_bar_string);
11533 else
11534 end = i + 1;
11535 Fadd_text_properties (make_number (i), make_number (end),
11536 props, f->desired_tool_bar_string);
11537 #undef PROP
11538 }
11539
11540 UNGCPRO;
11541 }
11542
11543
11544 /* Display one line of the tool-bar of frame IT->f.
11545
11546 HEIGHT specifies the desired height of the tool-bar line.
11547 If the actual height of the glyph row is less than HEIGHT, the
11548 row's height is increased to HEIGHT, and the icons are centered
11549 vertically in the new height.
11550
11551 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11552 count a final empty row in case the tool-bar width exactly matches
11553 the window width.
11554 */
11555
11556 static void
11557 display_tool_bar_line (struct it *it, int height)
11558 {
11559 struct glyph_row *row = it->glyph_row;
11560 int max_x = it->last_visible_x;
11561 struct glyph *last;
11562
11563 prepare_desired_row (row);
11564 row->y = it->current_y;
11565
11566 /* Note that this isn't made use of if the face hasn't a box,
11567 so there's no need to check the face here. */
11568 it->start_of_box_run_p = 1;
11569
11570 while (it->current_x < max_x)
11571 {
11572 int x, n_glyphs_before, i, nglyphs;
11573 struct it it_before;
11574
11575 /* Get the next display element. */
11576 if (!get_next_display_element (it))
11577 {
11578 /* Don't count empty row if we are counting needed tool-bar lines. */
11579 if (height < 0 && !it->hpos)
11580 return;
11581 break;
11582 }
11583
11584 /* Produce glyphs. */
11585 n_glyphs_before = row->used[TEXT_AREA];
11586 it_before = *it;
11587
11588 PRODUCE_GLYPHS (it);
11589
11590 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11591 i = 0;
11592 x = it_before.current_x;
11593 while (i < nglyphs)
11594 {
11595 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11596
11597 if (x + glyph->pixel_width > max_x)
11598 {
11599 /* Glyph doesn't fit on line. Backtrack. */
11600 row->used[TEXT_AREA] = n_glyphs_before;
11601 *it = it_before;
11602 /* If this is the only glyph on this line, it will never fit on the
11603 tool-bar, so skip it. But ensure there is at least one glyph,
11604 so we don't accidentally disable the tool-bar. */
11605 if (n_glyphs_before == 0
11606 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11607 break;
11608 goto out;
11609 }
11610
11611 ++it->hpos;
11612 x += glyph->pixel_width;
11613 ++i;
11614 }
11615
11616 /* Stop at line end. */
11617 if (ITERATOR_AT_END_OF_LINE_P (it))
11618 break;
11619
11620 set_iterator_to_next (it, 1);
11621 }
11622
11623 out:;
11624
11625 row->displays_text_p = row->used[TEXT_AREA] != 0;
11626
11627 /* Use default face for the border below the tool bar.
11628
11629 FIXME: When auto-resize-tool-bars is grow-only, there is
11630 no additional border below the possibly empty tool-bar lines.
11631 So to make the extra empty lines look "normal", we have to
11632 use the tool-bar face for the border too. */
11633 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11634 it->face_id = DEFAULT_FACE_ID;
11635
11636 extend_face_to_end_of_line (it);
11637 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11638 last->right_box_line_p = 1;
11639 if (last == row->glyphs[TEXT_AREA])
11640 last->left_box_line_p = 1;
11641
11642 /* Make line the desired height and center it vertically. */
11643 if ((height -= it->max_ascent + it->max_descent) > 0)
11644 {
11645 /* Don't add more than one line height. */
11646 height %= FRAME_LINE_HEIGHT (it->f);
11647 it->max_ascent += height / 2;
11648 it->max_descent += (height + 1) / 2;
11649 }
11650
11651 compute_line_metrics (it);
11652
11653 /* If line is empty, make it occupy the rest of the tool-bar. */
11654 if (!row->displays_text_p)
11655 {
11656 row->height = row->phys_height = it->last_visible_y - row->y;
11657 row->visible_height = row->height;
11658 row->ascent = row->phys_ascent = 0;
11659 row->extra_line_spacing = 0;
11660 }
11661
11662 row->full_width_p = 1;
11663 row->continued_p = 0;
11664 row->truncated_on_left_p = 0;
11665 row->truncated_on_right_p = 0;
11666
11667 it->current_x = it->hpos = 0;
11668 it->current_y += row->height;
11669 ++it->vpos;
11670 ++it->glyph_row;
11671 }
11672
11673
11674 /* Max tool-bar height. */
11675
11676 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11677 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11678
11679 /* Value is the number of screen lines needed to make all tool-bar
11680 items of frame F visible. The number of actual rows needed is
11681 returned in *N_ROWS if non-NULL. */
11682
11683 static int
11684 tool_bar_lines_needed (struct frame *f, int *n_rows)
11685 {
11686 struct window *w = XWINDOW (f->tool_bar_window);
11687 struct it it;
11688 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11689 the desired matrix, so use (unused) mode-line row as temporary row to
11690 avoid destroying the first tool-bar row. */
11691 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11692
11693 /* Initialize an iterator for iteration over
11694 F->desired_tool_bar_string in the tool-bar window of frame F. */
11695 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11696 it.first_visible_x = 0;
11697 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11698 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11699 it.paragraph_embedding = L2R;
11700
11701 while (!ITERATOR_AT_END_P (&it))
11702 {
11703 clear_glyph_row (temp_row);
11704 it.glyph_row = temp_row;
11705 display_tool_bar_line (&it, -1);
11706 }
11707 clear_glyph_row (temp_row);
11708
11709 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11710 if (n_rows)
11711 *n_rows = it.vpos > 0 ? it.vpos : -1;
11712
11713 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11714 }
11715
11716
11717 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11718 0, 1, 0,
11719 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11720 (Lisp_Object frame)
11721 {
11722 struct frame *f;
11723 struct window *w;
11724 int nlines = 0;
11725
11726 if (NILP (frame))
11727 frame = selected_frame;
11728 else
11729 CHECK_FRAME (frame);
11730 f = XFRAME (frame);
11731
11732 if (WINDOWP (f->tool_bar_window)
11733 && (w = XWINDOW (f->tool_bar_window),
11734 WINDOW_TOTAL_LINES (w) > 0))
11735 {
11736 update_tool_bar (f, 1);
11737 if (f->n_tool_bar_items)
11738 {
11739 build_desired_tool_bar_string (f);
11740 nlines = tool_bar_lines_needed (f, NULL);
11741 }
11742 }
11743
11744 return make_number (nlines);
11745 }
11746
11747
11748 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11749 height should be changed. */
11750
11751 static int
11752 redisplay_tool_bar (struct frame *f)
11753 {
11754 struct window *w;
11755 struct it it;
11756 struct glyph_row *row;
11757
11758 #if defined (USE_GTK) || defined (HAVE_NS)
11759 if (FRAME_EXTERNAL_TOOL_BAR (f))
11760 update_frame_tool_bar (f);
11761 return 0;
11762 #endif
11763
11764 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11765 do anything. This means you must start with tool-bar-lines
11766 non-zero to get the auto-sizing effect. Or in other words, you
11767 can turn off tool-bars by specifying tool-bar-lines zero. */
11768 if (!WINDOWP (f->tool_bar_window)
11769 || (w = XWINDOW (f->tool_bar_window),
11770 WINDOW_TOTAL_LINES (w) == 0))
11771 return 0;
11772
11773 /* Set up an iterator for the tool-bar window. */
11774 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11775 it.first_visible_x = 0;
11776 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11777 row = it.glyph_row;
11778
11779 /* Build a string that represents the contents of the tool-bar. */
11780 build_desired_tool_bar_string (f);
11781 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11782 /* FIXME: This should be controlled by a user option. But it
11783 doesn't make sense to have an R2L tool bar if the menu bar cannot
11784 be drawn also R2L, and making the menu bar R2L is tricky due
11785 toolkit-specific code that implements it. If an R2L tool bar is
11786 ever supported, display_tool_bar_line should also be augmented to
11787 call unproduce_glyphs like display_line and display_string
11788 do. */
11789 it.paragraph_embedding = L2R;
11790
11791 if (f->n_tool_bar_rows == 0)
11792 {
11793 int nlines;
11794
11795 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11796 nlines != WINDOW_TOTAL_LINES (w)))
11797 {
11798 Lisp_Object frame;
11799 int old_height = WINDOW_TOTAL_LINES (w);
11800
11801 XSETFRAME (frame, f);
11802 Fmodify_frame_parameters (frame,
11803 Fcons (Fcons (Qtool_bar_lines,
11804 make_number (nlines)),
11805 Qnil));
11806 if (WINDOW_TOTAL_LINES (w) != old_height)
11807 {
11808 clear_glyph_matrix (w->desired_matrix);
11809 fonts_changed_p = 1;
11810 return 1;
11811 }
11812 }
11813 }
11814
11815 /* Display as many lines as needed to display all tool-bar items. */
11816
11817 if (f->n_tool_bar_rows > 0)
11818 {
11819 int border, rows, height, extra;
11820
11821 if (INTEGERP (Vtool_bar_border))
11822 border = XINT (Vtool_bar_border);
11823 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11824 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11825 else if (EQ (Vtool_bar_border, Qborder_width))
11826 border = f->border_width;
11827 else
11828 border = 0;
11829 if (border < 0)
11830 border = 0;
11831
11832 rows = f->n_tool_bar_rows;
11833 height = max (1, (it.last_visible_y - border) / rows);
11834 extra = it.last_visible_y - border - height * rows;
11835
11836 while (it.current_y < it.last_visible_y)
11837 {
11838 int h = 0;
11839 if (extra > 0 && rows-- > 0)
11840 {
11841 h = (extra + rows - 1) / rows;
11842 extra -= h;
11843 }
11844 display_tool_bar_line (&it, height + h);
11845 }
11846 }
11847 else
11848 {
11849 while (it.current_y < it.last_visible_y)
11850 display_tool_bar_line (&it, 0);
11851 }
11852
11853 /* It doesn't make much sense to try scrolling in the tool-bar
11854 window, so don't do it. */
11855 w->desired_matrix->no_scrolling_p = 1;
11856 w->must_be_updated_p = 1;
11857
11858 if (!NILP (Vauto_resize_tool_bars))
11859 {
11860 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11861 int change_height_p = 0;
11862
11863 /* If we couldn't display everything, change the tool-bar's
11864 height if there is room for more. */
11865 if (IT_STRING_CHARPOS (it) < it.end_charpos
11866 && it.current_y < max_tool_bar_height)
11867 change_height_p = 1;
11868
11869 row = it.glyph_row - 1;
11870
11871 /* If there are blank lines at the end, except for a partially
11872 visible blank line at the end that is smaller than
11873 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11874 if (!row->displays_text_p
11875 && row->height >= FRAME_LINE_HEIGHT (f))
11876 change_height_p = 1;
11877
11878 /* If row displays tool-bar items, but is partially visible,
11879 change the tool-bar's height. */
11880 if (row->displays_text_p
11881 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11882 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11883 change_height_p = 1;
11884
11885 /* Resize windows as needed by changing the `tool-bar-lines'
11886 frame parameter. */
11887 if (change_height_p)
11888 {
11889 Lisp_Object frame;
11890 int old_height = WINDOW_TOTAL_LINES (w);
11891 int nrows;
11892 int nlines = tool_bar_lines_needed (f, &nrows);
11893
11894 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11895 && !f->minimize_tool_bar_window_p)
11896 ? (nlines > old_height)
11897 : (nlines != old_height));
11898 f->minimize_tool_bar_window_p = 0;
11899
11900 if (change_height_p)
11901 {
11902 XSETFRAME (frame, f);
11903 Fmodify_frame_parameters (frame,
11904 Fcons (Fcons (Qtool_bar_lines,
11905 make_number (nlines)),
11906 Qnil));
11907 if (WINDOW_TOTAL_LINES (w) != old_height)
11908 {
11909 clear_glyph_matrix (w->desired_matrix);
11910 f->n_tool_bar_rows = nrows;
11911 fonts_changed_p = 1;
11912 return 1;
11913 }
11914 }
11915 }
11916 }
11917
11918 f->minimize_tool_bar_window_p = 0;
11919 return 0;
11920 }
11921
11922
11923 /* Get information about the tool-bar item which is displayed in GLYPH
11924 on frame F. Return in *PROP_IDX the index where tool-bar item
11925 properties start in F->tool_bar_items. Value is zero if
11926 GLYPH doesn't display a tool-bar item. */
11927
11928 static int
11929 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11930 {
11931 Lisp_Object prop;
11932 int success_p;
11933 int charpos;
11934
11935 /* This function can be called asynchronously, which means we must
11936 exclude any possibility that Fget_text_property signals an
11937 error. */
11938 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11939 charpos = max (0, charpos);
11940
11941 /* Get the text property `menu-item' at pos. The value of that
11942 property is the start index of this item's properties in
11943 F->tool_bar_items. */
11944 prop = Fget_text_property (make_number (charpos),
11945 Qmenu_item, f->current_tool_bar_string);
11946 if (INTEGERP (prop))
11947 {
11948 *prop_idx = XINT (prop);
11949 success_p = 1;
11950 }
11951 else
11952 success_p = 0;
11953
11954 return success_p;
11955 }
11956
11957 \f
11958 /* Get information about the tool-bar item at position X/Y on frame F.
11959 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11960 the current matrix of the tool-bar window of F, or NULL if not
11961 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11962 item in F->tool_bar_items. Value is
11963
11964 -1 if X/Y is not on a tool-bar item
11965 0 if X/Y is on the same item that was highlighted before.
11966 1 otherwise. */
11967
11968 static int
11969 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11970 int *hpos, int *vpos, int *prop_idx)
11971 {
11972 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11973 struct window *w = XWINDOW (f->tool_bar_window);
11974 int area;
11975
11976 /* Find the glyph under X/Y. */
11977 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11978 if (*glyph == NULL)
11979 return -1;
11980
11981 /* Get the start of this tool-bar item's properties in
11982 f->tool_bar_items. */
11983 if (!tool_bar_item_info (f, *glyph, prop_idx))
11984 return -1;
11985
11986 /* Is mouse on the highlighted item? */
11987 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11988 && *vpos >= hlinfo->mouse_face_beg_row
11989 && *vpos <= hlinfo->mouse_face_end_row
11990 && (*vpos > hlinfo->mouse_face_beg_row
11991 || *hpos >= hlinfo->mouse_face_beg_col)
11992 && (*vpos < hlinfo->mouse_face_end_row
11993 || *hpos < hlinfo->mouse_face_end_col
11994 || hlinfo->mouse_face_past_end))
11995 return 0;
11996
11997 return 1;
11998 }
11999
12000
12001 /* EXPORT:
12002 Handle mouse button event on the tool-bar of frame F, at
12003 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12004 0 for button release. MODIFIERS is event modifiers for button
12005 release. */
12006
12007 void
12008 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12009 unsigned int modifiers)
12010 {
12011 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12012 struct window *w = XWINDOW (f->tool_bar_window);
12013 int hpos, vpos, prop_idx;
12014 struct glyph *glyph;
12015 Lisp_Object enabled_p;
12016
12017 /* If not on the highlighted tool-bar item, return. */
12018 frame_to_window_pixel_xy (w, &x, &y);
12019 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12020 return;
12021
12022 /* If item is disabled, do nothing. */
12023 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12024 if (NILP (enabled_p))
12025 return;
12026
12027 if (down_p)
12028 {
12029 /* Show item in pressed state. */
12030 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12031 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12032 last_tool_bar_item = prop_idx;
12033 }
12034 else
12035 {
12036 Lisp_Object key, frame;
12037 struct input_event event;
12038 EVENT_INIT (event);
12039
12040 /* Show item in released state. */
12041 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12042 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12043
12044 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12045
12046 XSETFRAME (frame, f);
12047 event.kind = TOOL_BAR_EVENT;
12048 event.frame_or_window = frame;
12049 event.arg = frame;
12050 kbd_buffer_store_event (&event);
12051
12052 event.kind = TOOL_BAR_EVENT;
12053 event.frame_or_window = frame;
12054 event.arg = key;
12055 event.modifiers = modifiers;
12056 kbd_buffer_store_event (&event);
12057 last_tool_bar_item = -1;
12058 }
12059 }
12060
12061
12062 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12063 tool-bar window-relative coordinates X/Y. Called from
12064 note_mouse_highlight. */
12065
12066 static void
12067 note_tool_bar_highlight (struct frame *f, int x, int y)
12068 {
12069 Lisp_Object window = f->tool_bar_window;
12070 struct window *w = XWINDOW (window);
12071 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12072 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12073 int hpos, vpos;
12074 struct glyph *glyph;
12075 struct glyph_row *row;
12076 int i;
12077 Lisp_Object enabled_p;
12078 int prop_idx;
12079 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12080 int mouse_down_p, rc;
12081
12082 /* Function note_mouse_highlight is called with negative X/Y
12083 values when mouse moves outside of the frame. */
12084 if (x <= 0 || y <= 0)
12085 {
12086 clear_mouse_face (hlinfo);
12087 return;
12088 }
12089
12090 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12091 if (rc < 0)
12092 {
12093 /* Not on tool-bar item. */
12094 clear_mouse_face (hlinfo);
12095 return;
12096 }
12097 else if (rc == 0)
12098 /* On same tool-bar item as before. */
12099 goto set_help_echo;
12100
12101 clear_mouse_face (hlinfo);
12102
12103 /* Mouse is down, but on different tool-bar item? */
12104 mouse_down_p = (dpyinfo->grabbed
12105 && f == last_mouse_frame
12106 && FRAME_LIVE_P (f));
12107 if (mouse_down_p
12108 && last_tool_bar_item != prop_idx)
12109 return;
12110
12111 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12112 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12113
12114 /* If tool-bar item is not enabled, don't highlight it. */
12115 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12116 if (!NILP (enabled_p))
12117 {
12118 /* Compute the x-position of the glyph. In front and past the
12119 image is a space. We include this in the highlighted area. */
12120 row = MATRIX_ROW (w->current_matrix, vpos);
12121 for (i = x = 0; i < hpos; ++i)
12122 x += row->glyphs[TEXT_AREA][i].pixel_width;
12123
12124 /* Record this as the current active region. */
12125 hlinfo->mouse_face_beg_col = hpos;
12126 hlinfo->mouse_face_beg_row = vpos;
12127 hlinfo->mouse_face_beg_x = x;
12128 hlinfo->mouse_face_beg_y = row->y;
12129 hlinfo->mouse_face_past_end = 0;
12130
12131 hlinfo->mouse_face_end_col = hpos + 1;
12132 hlinfo->mouse_face_end_row = vpos;
12133 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12134 hlinfo->mouse_face_end_y = row->y;
12135 hlinfo->mouse_face_window = window;
12136 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12137
12138 /* Display it as active. */
12139 show_mouse_face (hlinfo, draw);
12140 hlinfo->mouse_face_image_state = draw;
12141 }
12142
12143 set_help_echo:
12144
12145 /* Set help_echo_string to a help string to display for this tool-bar item.
12146 XTread_socket does the rest. */
12147 help_echo_object = help_echo_window = Qnil;
12148 help_echo_pos = -1;
12149 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12150 if (NILP (help_echo_string))
12151 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12152 }
12153
12154 #endif /* HAVE_WINDOW_SYSTEM */
12155
12156
12157 \f
12158 /************************************************************************
12159 Horizontal scrolling
12160 ************************************************************************/
12161
12162 static int hscroll_window_tree (Lisp_Object);
12163 static int hscroll_windows (Lisp_Object);
12164
12165 /* For all leaf windows in the window tree rooted at WINDOW, set their
12166 hscroll value so that PT is (i) visible in the window, and (ii) so
12167 that it is not within a certain margin at the window's left and
12168 right border. Value is non-zero if any window's hscroll has been
12169 changed. */
12170
12171 static int
12172 hscroll_window_tree (Lisp_Object window)
12173 {
12174 int hscrolled_p = 0;
12175 int hscroll_relative_p = FLOATP (Vhscroll_step);
12176 int hscroll_step_abs = 0;
12177 double hscroll_step_rel = 0;
12178
12179 if (hscroll_relative_p)
12180 {
12181 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12182 if (hscroll_step_rel < 0)
12183 {
12184 hscroll_relative_p = 0;
12185 hscroll_step_abs = 0;
12186 }
12187 }
12188 else if (INTEGERP (Vhscroll_step))
12189 {
12190 hscroll_step_abs = XINT (Vhscroll_step);
12191 if (hscroll_step_abs < 0)
12192 hscroll_step_abs = 0;
12193 }
12194 else
12195 hscroll_step_abs = 0;
12196
12197 while (WINDOWP (window))
12198 {
12199 struct window *w = XWINDOW (window);
12200
12201 if (WINDOWP (w->hchild))
12202 hscrolled_p |= hscroll_window_tree (w->hchild);
12203 else if (WINDOWP (w->vchild))
12204 hscrolled_p |= hscroll_window_tree (w->vchild);
12205 else if (w->cursor.vpos >= 0)
12206 {
12207 int h_margin;
12208 int text_area_width;
12209 struct glyph_row *current_cursor_row
12210 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12211 struct glyph_row *desired_cursor_row
12212 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12213 struct glyph_row *cursor_row
12214 = (desired_cursor_row->enabled_p
12215 ? desired_cursor_row
12216 : current_cursor_row);
12217 int row_r2l_p = cursor_row->reversed_p;
12218
12219 text_area_width = window_box_width (w, TEXT_AREA);
12220
12221 /* Scroll when cursor is inside this scroll margin. */
12222 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12223
12224 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12225 /* For left-to-right rows, hscroll when cursor is either
12226 (i) inside the right hscroll margin, or (ii) if it is
12227 inside the left margin and the window is already
12228 hscrolled. */
12229 && ((!row_r2l_p
12230 && ((XFASTINT (w->hscroll)
12231 && w->cursor.x <= h_margin)
12232 || (cursor_row->enabled_p
12233 && cursor_row->truncated_on_right_p
12234 && (w->cursor.x >= text_area_width - h_margin))))
12235 /* For right-to-left rows, the logic is similar,
12236 except that rules for scrolling to left and right
12237 are reversed. E.g., if cursor.x <= h_margin, we
12238 need to hscroll "to the right" unconditionally,
12239 and that will scroll the screen to the left so as
12240 to reveal the next portion of the row. */
12241 || (row_r2l_p
12242 && ((cursor_row->enabled_p
12243 /* FIXME: It is confusing to set the
12244 truncated_on_right_p flag when R2L rows
12245 are actually truncated on the left. */
12246 && cursor_row->truncated_on_right_p
12247 && w->cursor.x <= h_margin)
12248 || (XFASTINT (w->hscroll)
12249 && (w->cursor.x >= text_area_width - h_margin))))))
12250 {
12251 struct it it;
12252 int hscroll;
12253 struct buffer *saved_current_buffer;
12254 EMACS_INT pt;
12255 int wanted_x;
12256
12257 /* Find point in a display of infinite width. */
12258 saved_current_buffer = current_buffer;
12259 current_buffer = XBUFFER (w->buffer);
12260
12261 if (w == XWINDOW (selected_window))
12262 pt = PT;
12263 else
12264 {
12265 pt = marker_position (w->pointm);
12266 pt = max (BEGV, pt);
12267 pt = min (ZV, pt);
12268 }
12269
12270 /* Move iterator to pt starting at cursor_row->start in
12271 a line with infinite width. */
12272 init_to_row_start (&it, w, cursor_row);
12273 it.last_visible_x = INFINITY;
12274 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12275 current_buffer = saved_current_buffer;
12276
12277 /* Position cursor in window. */
12278 if (!hscroll_relative_p && hscroll_step_abs == 0)
12279 hscroll = max (0, (it.current_x
12280 - (ITERATOR_AT_END_OF_LINE_P (&it)
12281 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12282 : (text_area_width / 2))))
12283 / FRAME_COLUMN_WIDTH (it.f);
12284 else if ((!row_r2l_p
12285 && w->cursor.x >= text_area_width - h_margin)
12286 || (row_r2l_p && w->cursor.x <= h_margin))
12287 {
12288 if (hscroll_relative_p)
12289 wanted_x = text_area_width * (1 - hscroll_step_rel)
12290 - h_margin;
12291 else
12292 wanted_x = text_area_width
12293 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12294 - h_margin;
12295 hscroll
12296 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12297 }
12298 else
12299 {
12300 if (hscroll_relative_p)
12301 wanted_x = text_area_width * hscroll_step_rel
12302 + h_margin;
12303 else
12304 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12305 + h_margin;
12306 hscroll
12307 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12308 }
12309 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12310
12311 /* Don't prevent redisplay optimizations if hscroll
12312 hasn't changed, as it will unnecessarily slow down
12313 redisplay. */
12314 if (XFASTINT (w->hscroll) != hscroll)
12315 {
12316 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12317 w->hscroll = make_number (hscroll);
12318 hscrolled_p = 1;
12319 }
12320 }
12321 }
12322
12323 window = w->next;
12324 }
12325
12326 /* Value is non-zero if hscroll of any leaf window has been changed. */
12327 return hscrolled_p;
12328 }
12329
12330
12331 /* Set hscroll so that cursor is visible and not inside horizontal
12332 scroll margins for all windows in the tree rooted at WINDOW. See
12333 also hscroll_window_tree above. Value is non-zero if any window's
12334 hscroll has been changed. If it has, desired matrices on the frame
12335 of WINDOW are cleared. */
12336
12337 static int
12338 hscroll_windows (Lisp_Object window)
12339 {
12340 int hscrolled_p = hscroll_window_tree (window);
12341 if (hscrolled_p)
12342 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12343 return hscrolled_p;
12344 }
12345
12346
12347 \f
12348 /************************************************************************
12349 Redisplay
12350 ************************************************************************/
12351
12352 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12353 to a non-zero value. This is sometimes handy to have in a debugger
12354 session. */
12355
12356 #if GLYPH_DEBUG
12357
12358 /* First and last unchanged row for try_window_id. */
12359
12360 static int debug_first_unchanged_at_end_vpos;
12361 static int debug_last_unchanged_at_beg_vpos;
12362
12363 /* Delta vpos and y. */
12364
12365 static int debug_dvpos, debug_dy;
12366
12367 /* Delta in characters and bytes for try_window_id. */
12368
12369 static EMACS_INT debug_delta, debug_delta_bytes;
12370
12371 /* Values of window_end_pos and window_end_vpos at the end of
12372 try_window_id. */
12373
12374 static EMACS_INT debug_end_vpos;
12375
12376 /* Append a string to W->desired_matrix->method. FMT is a printf
12377 format string. If trace_redisplay_p is non-zero also printf the
12378 resulting string to stderr. */
12379
12380 static void debug_method_add (struct window *, char const *, ...)
12381 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12382
12383 static void
12384 debug_method_add (struct window *w, char const *fmt, ...)
12385 {
12386 char buffer[512];
12387 char *method = w->desired_matrix->method;
12388 int len = strlen (method);
12389 int size = sizeof w->desired_matrix->method;
12390 int remaining = size - len - 1;
12391 va_list ap;
12392
12393 va_start (ap, fmt);
12394 vsprintf (buffer, fmt, ap);
12395 va_end (ap);
12396 if (len && remaining)
12397 {
12398 method[len] = '|';
12399 --remaining, ++len;
12400 }
12401
12402 strncpy (method + len, buffer, remaining);
12403
12404 if (trace_redisplay_p)
12405 fprintf (stderr, "%p (%s): %s\n",
12406 w,
12407 ((BUFFERP (w->buffer)
12408 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12409 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12410 : "no buffer"),
12411 buffer);
12412 }
12413
12414 #endif /* GLYPH_DEBUG */
12415
12416
12417 /* Value is non-zero if all changes in window W, which displays
12418 current_buffer, are in the text between START and END. START is a
12419 buffer position, END is given as a distance from Z. Used in
12420 redisplay_internal for display optimization. */
12421
12422 static inline int
12423 text_outside_line_unchanged_p (struct window *w,
12424 EMACS_INT start, EMACS_INT end)
12425 {
12426 int unchanged_p = 1;
12427
12428 /* If text or overlays have changed, see where. */
12429 if (XFASTINT (w->last_modified) < MODIFF
12430 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12431 {
12432 /* Gap in the line? */
12433 if (GPT < start || Z - GPT < end)
12434 unchanged_p = 0;
12435
12436 /* Changes start in front of the line, or end after it? */
12437 if (unchanged_p
12438 && (BEG_UNCHANGED < start - 1
12439 || END_UNCHANGED < end))
12440 unchanged_p = 0;
12441
12442 /* If selective display, can't optimize if changes start at the
12443 beginning of the line. */
12444 if (unchanged_p
12445 && INTEGERP (BVAR (current_buffer, selective_display))
12446 && XINT (BVAR (current_buffer, selective_display)) > 0
12447 && (BEG_UNCHANGED < start || GPT <= start))
12448 unchanged_p = 0;
12449
12450 /* If there are overlays at the start or end of the line, these
12451 may have overlay strings with newlines in them. A change at
12452 START, for instance, may actually concern the display of such
12453 overlay strings as well, and they are displayed on different
12454 lines. So, quickly rule out this case. (For the future, it
12455 might be desirable to implement something more telling than
12456 just BEG/END_UNCHANGED.) */
12457 if (unchanged_p)
12458 {
12459 if (BEG + BEG_UNCHANGED == start
12460 && overlay_touches_p (start))
12461 unchanged_p = 0;
12462 if (END_UNCHANGED == end
12463 && overlay_touches_p (Z - end))
12464 unchanged_p = 0;
12465 }
12466
12467 /* Under bidi reordering, adding or deleting a character in the
12468 beginning of a paragraph, before the first strong directional
12469 character, can change the base direction of the paragraph (unless
12470 the buffer specifies a fixed paragraph direction), which will
12471 require to redisplay the whole paragraph. It might be worthwhile
12472 to find the paragraph limits and widen the range of redisplayed
12473 lines to that, but for now just give up this optimization. */
12474 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12475 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12476 unchanged_p = 0;
12477 }
12478
12479 return unchanged_p;
12480 }
12481
12482
12483 /* Do a frame update, taking possible shortcuts into account. This is
12484 the main external entry point for redisplay.
12485
12486 If the last redisplay displayed an echo area message and that message
12487 is no longer requested, we clear the echo area or bring back the
12488 mini-buffer if that is in use. */
12489
12490 void
12491 redisplay (void)
12492 {
12493 redisplay_internal ();
12494 }
12495
12496
12497 static Lisp_Object
12498 overlay_arrow_string_or_property (Lisp_Object var)
12499 {
12500 Lisp_Object val;
12501
12502 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12503 return val;
12504
12505 return Voverlay_arrow_string;
12506 }
12507
12508 /* Return 1 if there are any overlay-arrows in current_buffer. */
12509 static int
12510 overlay_arrow_in_current_buffer_p (void)
12511 {
12512 Lisp_Object vlist;
12513
12514 for (vlist = Voverlay_arrow_variable_list;
12515 CONSP (vlist);
12516 vlist = XCDR (vlist))
12517 {
12518 Lisp_Object var = XCAR (vlist);
12519 Lisp_Object val;
12520
12521 if (!SYMBOLP (var))
12522 continue;
12523 val = find_symbol_value (var);
12524 if (MARKERP (val)
12525 && current_buffer == XMARKER (val)->buffer)
12526 return 1;
12527 }
12528 return 0;
12529 }
12530
12531
12532 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12533 has changed. */
12534
12535 static int
12536 overlay_arrows_changed_p (void)
12537 {
12538 Lisp_Object vlist;
12539
12540 for (vlist = Voverlay_arrow_variable_list;
12541 CONSP (vlist);
12542 vlist = XCDR (vlist))
12543 {
12544 Lisp_Object var = XCAR (vlist);
12545 Lisp_Object val, pstr;
12546
12547 if (!SYMBOLP (var))
12548 continue;
12549 val = find_symbol_value (var);
12550 if (!MARKERP (val))
12551 continue;
12552 if (! EQ (COERCE_MARKER (val),
12553 Fget (var, Qlast_arrow_position))
12554 || ! (pstr = overlay_arrow_string_or_property (var),
12555 EQ (pstr, Fget (var, Qlast_arrow_string))))
12556 return 1;
12557 }
12558 return 0;
12559 }
12560
12561 /* Mark overlay arrows to be updated on next redisplay. */
12562
12563 static void
12564 update_overlay_arrows (int up_to_date)
12565 {
12566 Lisp_Object vlist;
12567
12568 for (vlist = Voverlay_arrow_variable_list;
12569 CONSP (vlist);
12570 vlist = XCDR (vlist))
12571 {
12572 Lisp_Object var = XCAR (vlist);
12573
12574 if (!SYMBOLP (var))
12575 continue;
12576
12577 if (up_to_date > 0)
12578 {
12579 Lisp_Object val = find_symbol_value (var);
12580 Fput (var, Qlast_arrow_position,
12581 COERCE_MARKER (val));
12582 Fput (var, Qlast_arrow_string,
12583 overlay_arrow_string_or_property (var));
12584 }
12585 else if (up_to_date < 0
12586 || !NILP (Fget (var, Qlast_arrow_position)))
12587 {
12588 Fput (var, Qlast_arrow_position, Qt);
12589 Fput (var, Qlast_arrow_string, Qt);
12590 }
12591 }
12592 }
12593
12594
12595 /* Return overlay arrow string to display at row.
12596 Return integer (bitmap number) for arrow bitmap in left fringe.
12597 Return nil if no overlay arrow. */
12598
12599 static Lisp_Object
12600 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12601 {
12602 Lisp_Object vlist;
12603
12604 for (vlist = Voverlay_arrow_variable_list;
12605 CONSP (vlist);
12606 vlist = XCDR (vlist))
12607 {
12608 Lisp_Object var = XCAR (vlist);
12609 Lisp_Object val;
12610
12611 if (!SYMBOLP (var))
12612 continue;
12613
12614 val = find_symbol_value (var);
12615
12616 if (MARKERP (val)
12617 && current_buffer == XMARKER (val)->buffer
12618 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12619 {
12620 if (FRAME_WINDOW_P (it->f)
12621 /* FIXME: if ROW->reversed_p is set, this should test
12622 the right fringe, not the left one. */
12623 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12624 {
12625 #ifdef HAVE_WINDOW_SYSTEM
12626 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12627 {
12628 int fringe_bitmap;
12629 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12630 return make_number (fringe_bitmap);
12631 }
12632 #endif
12633 return make_number (-1); /* Use default arrow bitmap */
12634 }
12635 return overlay_arrow_string_or_property (var);
12636 }
12637 }
12638
12639 return Qnil;
12640 }
12641
12642 /* Return 1 if point moved out of or into a composition. Otherwise
12643 return 0. PREV_BUF and PREV_PT are the last point buffer and
12644 position. BUF and PT are the current point buffer and position. */
12645
12646 static int
12647 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12648 struct buffer *buf, EMACS_INT pt)
12649 {
12650 EMACS_INT start, end;
12651 Lisp_Object prop;
12652 Lisp_Object buffer;
12653
12654 XSETBUFFER (buffer, buf);
12655 /* Check a composition at the last point if point moved within the
12656 same buffer. */
12657 if (prev_buf == buf)
12658 {
12659 if (prev_pt == pt)
12660 /* Point didn't move. */
12661 return 0;
12662
12663 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12664 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12665 && COMPOSITION_VALID_P (start, end, prop)
12666 && start < prev_pt && end > prev_pt)
12667 /* The last point was within the composition. Return 1 iff
12668 point moved out of the composition. */
12669 return (pt <= start || pt >= end);
12670 }
12671
12672 /* Check a composition at the current point. */
12673 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12674 && find_composition (pt, -1, &start, &end, &prop, buffer)
12675 && COMPOSITION_VALID_P (start, end, prop)
12676 && start < pt && end > pt);
12677 }
12678
12679
12680 /* Reconsider the setting of B->clip_changed which is displayed
12681 in window W. */
12682
12683 static inline void
12684 reconsider_clip_changes (struct window *w, struct buffer *b)
12685 {
12686 if (b->clip_changed
12687 && !NILP (w->window_end_valid)
12688 && w->current_matrix->buffer == b
12689 && w->current_matrix->zv == BUF_ZV (b)
12690 && w->current_matrix->begv == BUF_BEGV (b))
12691 b->clip_changed = 0;
12692
12693 /* If display wasn't paused, and W is not a tool bar window, see if
12694 point has been moved into or out of a composition. In that case,
12695 we set b->clip_changed to 1 to force updating the screen. If
12696 b->clip_changed has already been set to 1, we can skip this
12697 check. */
12698 if (!b->clip_changed
12699 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12700 {
12701 EMACS_INT pt;
12702
12703 if (w == XWINDOW (selected_window))
12704 pt = PT;
12705 else
12706 pt = marker_position (w->pointm);
12707
12708 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12709 || pt != XINT (w->last_point))
12710 && check_point_in_composition (w->current_matrix->buffer,
12711 XINT (w->last_point),
12712 XBUFFER (w->buffer), pt))
12713 b->clip_changed = 1;
12714 }
12715 }
12716 \f
12717
12718 /* Select FRAME to forward the values of frame-local variables into C
12719 variables so that the redisplay routines can access those values
12720 directly. */
12721
12722 static void
12723 select_frame_for_redisplay (Lisp_Object frame)
12724 {
12725 Lisp_Object tail, tem;
12726 Lisp_Object old = selected_frame;
12727 struct Lisp_Symbol *sym;
12728
12729 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12730
12731 selected_frame = frame;
12732
12733 do {
12734 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12735 if (CONSP (XCAR (tail))
12736 && (tem = XCAR (XCAR (tail)),
12737 SYMBOLP (tem))
12738 && (sym = indirect_variable (XSYMBOL (tem)),
12739 sym->redirect == SYMBOL_LOCALIZED)
12740 && sym->val.blv->frame_local)
12741 /* Use find_symbol_value rather than Fsymbol_value
12742 to avoid an error if it is void. */
12743 find_symbol_value (tem);
12744 } while (!EQ (frame, old) && (frame = old, 1));
12745 }
12746
12747
12748 #define STOP_POLLING \
12749 do { if (! polling_stopped_here) stop_polling (); \
12750 polling_stopped_here = 1; } while (0)
12751
12752 #define RESUME_POLLING \
12753 do { if (polling_stopped_here) start_polling (); \
12754 polling_stopped_here = 0; } while (0)
12755
12756
12757 /* Perhaps in the future avoid recentering windows if it
12758 is not necessary; currently that causes some problems. */
12759
12760 static void
12761 redisplay_internal (void)
12762 {
12763 struct window *w = XWINDOW (selected_window);
12764 struct window *sw;
12765 struct frame *fr;
12766 int pending;
12767 int must_finish = 0;
12768 struct text_pos tlbufpos, tlendpos;
12769 int number_of_visible_frames;
12770 int count, count1;
12771 struct frame *sf;
12772 int polling_stopped_here = 0;
12773 Lisp_Object old_frame = selected_frame;
12774
12775 /* Non-zero means redisplay has to consider all windows on all
12776 frames. Zero means, only selected_window is considered. */
12777 int consider_all_windows_p;
12778
12779 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12780
12781 /* No redisplay if running in batch mode or frame is not yet fully
12782 initialized, or redisplay is explicitly turned off by setting
12783 Vinhibit_redisplay. */
12784 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12785 || !NILP (Vinhibit_redisplay))
12786 return;
12787
12788 /* Don't examine these until after testing Vinhibit_redisplay.
12789 When Emacs is shutting down, perhaps because its connection to
12790 X has dropped, we should not look at them at all. */
12791 fr = XFRAME (w->frame);
12792 sf = SELECTED_FRAME ();
12793
12794 if (!fr->glyphs_initialized_p)
12795 return;
12796
12797 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12798 if (popup_activated ())
12799 return;
12800 #endif
12801
12802 /* I don't think this happens but let's be paranoid. */
12803 if (redisplaying_p)
12804 return;
12805
12806 /* Record a function that resets redisplaying_p to its old value
12807 when we leave this function. */
12808 count = SPECPDL_INDEX ();
12809 record_unwind_protect (unwind_redisplay,
12810 Fcons (make_number (redisplaying_p), selected_frame));
12811 ++redisplaying_p;
12812 specbind (Qinhibit_free_realized_faces, Qnil);
12813
12814 {
12815 Lisp_Object tail, frame;
12816
12817 FOR_EACH_FRAME (tail, frame)
12818 {
12819 struct frame *f = XFRAME (frame);
12820 f->already_hscrolled_p = 0;
12821 }
12822 }
12823
12824 retry:
12825 /* Remember the currently selected window. */
12826 sw = w;
12827
12828 if (!EQ (old_frame, selected_frame)
12829 && FRAME_LIVE_P (XFRAME (old_frame)))
12830 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12831 selected_frame and selected_window to be temporarily out-of-sync so
12832 when we come back here via `goto retry', we need to resync because we
12833 may need to run Elisp code (via prepare_menu_bars). */
12834 select_frame_for_redisplay (old_frame);
12835
12836 pending = 0;
12837 reconsider_clip_changes (w, current_buffer);
12838 last_escape_glyph_frame = NULL;
12839 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12840 last_glyphless_glyph_frame = NULL;
12841 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12842
12843 /* If new fonts have been loaded that make a glyph matrix adjustment
12844 necessary, do it. */
12845 if (fonts_changed_p)
12846 {
12847 adjust_glyphs (NULL);
12848 ++windows_or_buffers_changed;
12849 fonts_changed_p = 0;
12850 }
12851
12852 /* If face_change_count is non-zero, init_iterator will free all
12853 realized faces, which includes the faces referenced from current
12854 matrices. So, we can't reuse current matrices in this case. */
12855 if (face_change_count)
12856 ++windows_or_buffers_changed;
12857
12858 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12859 && FRAME_TTY (sf)->previous_frame != sf)
12860 {
12861 /* Since frames on a single ASCII terminal share the same
12862 display area, displaying a different frame means redisplay
12863 the whole thing. */
12864 windows_or_buffers_changed++;
12865 SET_FRAME_GARBAGED (sf);
12866 #ifndef DOS_NT
12867 set_tty_color_mode (FRAME_TTY (sf), sf);
12868 #endif
12869 FRAME_TTY (sf)->previous_frame = sf;
12870 }
12871
12872 /* Set the visible flags for all frames. Do this before checking
12873 for resized or garbaged frames; they want to know if their frames
12874 are visible. See the comment in frame.h for
12875 FRAME_SAMPLE_VISIBILITY. */
12876 {
12877 Lisp_Object tail, frame;
12878
12879 number_of_visible_frames = 0;
12880
12881 FOR_EACH_FRAME (tail, frame)
12882 {
12883 struct frame *f = XFRAME (frame);
12884
12885 FRAME_SAMPLE_VISIBILITY (f);
12886 if (FRAME_VISIBLE_P (f))
12887 ++number_of_visible_frames;
12888 clear_desired_matrices (f);
12889 }
12890 }
12891
12892 /* Notice any pending interrupt request to change frame size. */
12893 do_pending_window_change (1);
12894
12895 /* do_pending_window_change could change the selected_window due to
12896 frame resizing which makes the selected window too small. */
12897 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12898 {
12899 sw = w;
12900 reconsider_clip_changes (w, current_buffer);
12901 }
12902
12903 /* Clear frames marked as garbaged. */
12904 if (frame_garbaged)
12905 clear_garbaged_frames ();
12906
12907 /* Build menubar and tool-bar items. */
12908 if (NILP (Vmemory_full))
12909 prepare_menu_bars ();
12910
12911 if (windows_or_buffers_changed)
12912 update_mode_lines++;
12913
12914 /* Detect case that we need to write or remove a star in the mode line. */
12915 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12916 {
12917 w->update_mode_line = Qt;
12918 if (buffer_shared > 1)
12919 update_mode_lines++;
12920 }
12921
12922 /* Avoid invocation of point motion hooks by `current_column' below. */
12923 count1 = SPECPDL_INDEX ();
12924 specbind (Qinhibit_point_motion_hooks, Qt);
12925
12926 /* If %c is in the mode line, update it if needed. */
12927 if (!NILP (w->column_number_displayed)
12928 /* This alternative quickly identifies a common case
12929 where no change is needed. */
12930 && !(PT == XFASTINT (w->last_point)
12931 && XFASTINT (w->last_modified) >= MODIFF
12932 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12933 && (XFASTINT (w->column_number_displayed) != current_column ()))
12934 w->update_mode_line = Qt;
12935
12936 unbind_to (count1, Qnil);
12937
12938 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12939
12940 /* The variable buffer_shared is set in redisplay_window and
12941 indicates that we redisplay a buffer in different windows. See
12942 there. */
12943 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12944 || cursor_type_changed);
12945
12946 /* If specs for an arrow have changed, do thorough redisplay
12947 to ensure we remove any arrow that should no longer exist. */
12948 if (overlay_arrows_changed_p ())
12949 consider_all_windows_p = windows_or_buffers_changed = 1;
12950
12951 /* Normally the message* functions will have already displayed and
12952 updated the echo area, but the frame may have been trashed, or
12953 the update may have been preempted, so display the echo area
12954 again here. Checking message_cleared_p captures the case that
12955 the echo area should be cleared. */
12956 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12957 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12958 || (message_cleared_p
12959 && minibuf_level == 0
12960 /* If the mini-window is currently selected, this means the
12961 echo-area doesn't show through. */
12962 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12963 {
12964 int window_height_changed_p = echo_area_display (0);
12965 must_finish = 1;
12966
12967 /* If we don't display the current message, don't clear the
12968 message_cleared_p flag, because, if we did, we wouldn't clear
12969 the echo area in the next redisplay which doesn't preserve
12970 the echo area. */
12971 if (!display_last_displayed_message_p)
12972 message_cleared_p = 0;
12973
12974 if (fonts_changed_p)
12975 goto retry;
12976 else if (window_height_changed_p)
12977 {
12978 consider_all_windows_p = 1;
12979 ++update_mode_lines;
12980 ++windows_or_buffers_changed;
12981
12982 /* If window configuration was changed, frames may have been
12983 marked garbaged. Clear them or we will experience
12984 surprises wrt scrolling. */
12985 if (frame_garbaged)
12986 clear_garbaged_frames ();
12987 }
12988 }
12989 else if (EQ (selected_window, minibuf_window)
12990 && (current_buffer->clip_changed
12991 || XFASTINT (w->last_modified) < MODIFF
12992 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12993 && resize_mini_window (w, 0))
12994 {
12995 /* Resized active mini-window to fit the size of what it is
12996 showing if its contents might have changed. */
12997 must_finish = 1;
12998 /* FIXME: this causes all frames to be updated, which seems unnecessary
12999 since only the current frame needs to be considered. This function needs
13000 to be rewritten with two variables, consider_all_windows and
13001 consider_all_frames. */
13002 consider_all_windows_p = 1;
13003 ++windows_or_buffers_changed;
13004 ++update_mode_lines;
13005
13006 /* If window configuration was changed, frames may have been
13007 marked garbaged. Clear them or we will experience
13008 surprises wrt scrolling. */
13009 if (frame_garbaged)
13010 clear_garbaged_frames ();
13011 }
13012
13013
13014 /* If showing the region, and mark has changed, we must redisplay
13015 the whole window. The assignment to this_line_start_pos prevents
13016 the optimization directly below this if-statement. */
13017 if (((!NILP (Vtransient_mark_mode)
13018 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13019 != !NILP (w->region_showing))
13020 || (!NILP (w->region_showing)
13021 && !EQ (w->region_showing,
13022 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13023 CHARPOS (this_line_start_pos) = 0;
13024
13025 /* Optimize the case that only the line containing the cursor in the
13026 selected window has changed. Variables starting with this_ are
13027 set in display_line and record information about the line
13028 containing the cursor. */
13029 tlbufpos = this_line_start_pos;
13030 tlendpos = this_line_end_pos;
13031 if (!consider_all_windows_p
13032 && CHARPOS (tlbufpos) > 0
13033 && NILP (w->update_mode_line)
13034 && !current_buffer->clip_changed
13035 && !current_buffer->prevent_redisplay_optimizations_p
13036 && FRAME_VISIBLE_P (XFRAME (w->frame))
13037 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13038 /* Make sure recorded data applies to current buffer, etc. */
13039 && this_line_buffer == current_buffer
13040 && current_buffer == XBUFFER (w->buffer)
13041 && NILP (w->force_start)
13042 && NILP (w->optional_new_start)
13043 /* Point must be on the line that we have info recorded about. */
13044 && PT >= CHARPOS (tlbufpos)
13045 && PT <= Z - CHARPOS (tlendpos)
13046 /* All text outside that line, including its final newline,
13047 must be unchanged. */
13048 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13049 CHARPOS (tlendpos)))
13050 {
13051 if (CHARPOS (tlbufpos) > BEGV
13052 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13053 && (CHARPOS (tlbufpos) == ZV
13054 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13055 /* Former continuation line has disappeared by becoming empty. */
13056 goto cancel;
13057 else if (XFASTINT (w->last_modified) < MODIFF
13058 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13059 || MINI_WINDOW_P (w))
13060 {
13061 /* We have to handle the case of continuation around a
13062 wide-column character (see the comment in indent.c around
13063 line 1340).
13064
13065 For instance, in the following case:
13066
13067 -------- Insert --------
13068 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13069 J_I_ ==> J_I_ `^^' are cursors.
13070 ^^ ^^
13071 -------- --------
13072
13073 As we have to redraw the line above, we cannot use this
13074 optimization. */
13075
13076 struct it it;
13077 int line_height_before = this_line_pixel_height;
13078
13079 /* Note that start_display will handle the case that the
13080 line starting at tlbufpos is a continuation line. */
13081 start_display (&it, w, tlbufpos);
13082
13083 /* Implementation note: It this still necessary? */
13084 if (it.current_x != this_line_start_x)
13085 goto cancel;
13086
13087 TRACE ((stderr, "trying display optimization 1\n"));
13088 w->cursor.vpos = -1;
13089 overlay_arrow_seen = 0;
13090 it.vpos = this_line_vpos;
13091 it.current_y = this_line_y;
13092 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13093 display_line (&it);
13094
13095 /* If line contains point, is not continued,
13096 and ends at same distance from eob as before, we win. */
13097 if (w->cursor.vpos >= 0
13098 /* Line is not continued, otherwise this_line_start_pos
13099 would have been set to 0 in display_line. */
13100 && CHARPOS (this_line_start_pos)
13101 /* Line ends as before. */
13102 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13103 /* Line has same height as before. Otherwise other lines
13104 would have to be shifted up or down. */
13105 && this_line_pixel_height == line_height_before)
13106 {
13107 /* If this is not the window's last line, we must adjust
13108 the charstarts of the lines below. */
13109 if (it.current_y < it.last_visible_y)
13110 {
13111 struct glyph_row *row
13112 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13113 EMACS_INT delta, delta_bytes;
13114
13115 /* We used to distinguish between two cases here,
13116 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13117 when the line ends in a newline or the end of the
13118 buffer's accessible portion. But both cases did
13119 the same, so they were collapsed. */
13120 delta = (Z
13121 - CHARPOS (tlendpos)
13122 - MATRIX_ROW_START_CHARPOS (row));
13123 delta_bytes = (Z_BYTE
13124 - BYTEPOS (tlendpos)
13125 - MATRIX_ROW_START_BYTEPOS (row));
13126
13127 increment_matrix_positions (w->current_matrix,
13128 this_line_vpos + 1,
13129 w->current_matrix->nrows,
13130 delta, delta_bytes);
13131 }
13132
13133 /* If this row displays text now but previously didn't,
13134 or vice versa, w->window_end_vpos may have to be
13135 adjusted. */
13136 if ((it.glyph_row - 1)->displays_text_p)
13137 {
13138 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13139 XSETINT (w->window_end_vpos, this_line_vpos);
13140 }
13141 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13142 && this_line_vpos > 0)
13143 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13144 w->window_end_valid = Qnil;
13145
13146 /* Update hint: No need to try to scroll in update_window. */
13147 w->desired_matrix->no_scrolling_p = 1;
13148
13149 #if GLYPH_DEBUG
13150 *w->desired_matrix->method = 0;
13151 debug_method_add (w, "optimization 1");
13152 #endif
13153 #ifdef HAVE_WINDOW_SYSTEM
13154 update_window_fringes (w, 0);
13155 #endif
13156 goto update;
13157 }
13158 else
13159 goto cancel;
13160 }
13161 else if (/* Cursor position hasn't changed. */
13162 PT == XFASTINT (w->last_point)
13163 /* Make sure the cursor was last displayed
13164 in this window. Otherwise we have to reposition it. */
13165 && 0 <= w->cursor.vpos
13166 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13167 {
13168 if (!must_finish)
13169 {
13170 do_pending_window_change (1);
13171 /* If selected_window changed, redisplay again. */
13172 if (WINDOWP (selected_window)
13173 && (w = XWINDOW (selected_window)) != sw)
13174 goto retry;
13175
13176 /* We used to always goto end_of_redisplay here, but this
13177 isn't enough if we have a blinking cursor. */
13178 if (w->cursor_off_p == w->last_cursor_off_p)
13179 goto end_of_redisplay;
13180 }
13181 goto update;
13182 }
13183 /* If highlighting the region, or if the cursor is in the echo area,
13184 then we can't just move the cursor. */
13185 else if (! (!NILP (Vtransient_mark_mode)
13186 && !NILP (BVAR (current_buffer, mark_active)))
13187 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13188 || highlight_nonselected_windows)
13189 && NILP (w->region_showing)
13190 && NILP (Vshow_trailing_whitespace)
13191 && !cursor_in_echo_area)
13192 {
13193 struct it it;
13194 struct glyph_row *row;
13195
13196 /* Skip from tlbufpos to PT and see where it is. Note that
13197 PT may be in invisible text. If so, we will end at the
13198 next visible position. */
13199 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13200 NULL, DEFAULT_FACE_ID);
13201 it.current_x = this_line_start_x;
13202 it.current_y = this_line_y;
13203 it.vpos = this_line_vpos;
13204
13205 /* The call to move_it_to stops in front of PT, but
13206 moves over before-strings. */
13207 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13208
13209 if (it.vpos == this_line_vpos
13210 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13211 row->enabled_p))
13212 {
13213 xassert (this_line_vpos == it.vpos);
13214 xassert (this_line_y == it.current_y);
13215 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13216 #if GLYPH_DEBUG
13217 *w->desired_matrix->method = 0;
13218 debug_method_add (w, "optimization 3");
13219 #endif
13220 goto update;
13221 }
13222 else
13223 goto cancel;
13224 }
13225
13226 cancel:
13227 /* Text changed drastically or point moved off of line. */
13228 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13229 }
13230
13231 CHARPOS (this_line_start_pos) = 0;
13232 consider_all_windows_p |= buffer_shared > 1;
13233 ++clear_face_cache_count;
13234 #ifdef HAVE_WINDOW_SYSTEM
13235 ++clear_image_cache_count;
13236 #endif
13237
13238 /* Build desired matrices, and update the display. If
13239 consider_all_windows_p is non-zero, do it for all windows on all
13240 frames. Otherwise do it for selected_window, only. */
13241
13242 if (consider_all_windows_p)
13243 {
13244 Lisp_Object tail, frame;
13245
13246 FOR_EACH_FRAME (tail, frame)
13247 XFRAME (frame)->updated_p = 0;
13248
13249 /* Recompute # windows showing selected buffer. This will be
13250 incremented each time such a window is displayed. */
13251 buffer_shared = 0;
13252
13253 FOR_EACH_FRAME (tail, frame)
13254 {
13255 struct frame *f = XFRAME (frame);
13256
13257 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13258 {
13259 if (! EQ (frame, selected_frame))
13260 /* Select the frame, for the sake of frame-local
13261 variables. */
13262 select_frame_for_redisplay (frame);
13263
13264 /* Mark all the scroll bars to be removed; we'll redeem
13265 the ones we want when we redisplay their windows. */
13266 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13267 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13268
13269 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13270 redisplay_windows (FRAME_ROOT_WINDOW (f));
13271
13272 /* The X error handler may have deleted that frame. */
13273 if (!FRAME_LIVE_P (f))
13274 continue;
13275
13276 /* Any scroll bars which redisplay_windows should have
13277 nuked should now go away. */
13278 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13279 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13280
13281 /* If fonts changed, display again. */
13282 /* ??? rms: I suspect it is a mistake to jump all the way
13283 back to retry here. It should just retry this frame. */
13284 if (fonts_changed_p)
13285 goto retry;
13286
13287 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13288 {
13289 /* See if we have to hscroll. */
13290 if (!f->already_hscrolled_p)
13291 {
13292 f->already_hscrolled_p = 1;
13293 if (hscroll_windows (f->root_window))
13294 goto retry;
13295 }
13296
13297 /* Prevent various kinds of signals during display
13298 update. stdio is not robust about handling
13299 signals, which can cause an apparent I/O
13300 error. */
13301 if (interrupt_input)
13302 unrequest_sigio ();
13303 STOP_POLLING;
13304
13305 /* Update the display. */
13306 set_window_update_flags (XWINDOW (f->root_window), 1);
13307 pending |= update_frame (f, 0, 0);
13308 f->updated_p = 1;
13309 }
13310 }
13311 }
13312
13313 if (!EQ (old_frame, selected_frame)
13314 && FRAME_LIVE_P (XFRAME (old_frame)))
13315 /* We played a bit fast-and-loose above and allowed selected_frame
13316 and selected_window to be temporarily out-of-sync but let's make
13317 sure this stays contained. */
13318 select_frame_for_redisplay (old_frame);
13319 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13320
13321 if (!pending)
13322 {
13323 /* Do the mark_window_display_accurate after all windows have
13324 been redisplayed because this call resets flags in buffers
13325 which are needed for proper redisplay. */
13326 FOR_EACH_FRAME (tail, frame)
13327 {
13328 struct frame *f = XFRAME (frame);
13329 if (f->updated_p)
13330 {
13331 mark_window_display_accurate (f->root_window, 1);
13332 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13333 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13334 }
13335 }
13336 }
13337 }
13338 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13339 {
13340 Lisp_Object mini_window;
13341 struct frame *mini_frame;
13342
13343 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13344 /* Use list_of_error, not Qerror, so that
13345 we catch only errors and don't run the debugger. */
13346 internal_condition_case_1 (redisplay_window_1, selected_window,
13347 list_of_error,
13348 redisplay_window_error);
13349
13350 /* Compare desired and current matrices, perform output. */
13351
13352 update:
13353 /* If fonts changed, display again. */
13354 if (fonts_changed_p)
13355 goto retry;
13356
13357 /* Prevent various kinds of signals during display update.
13358 stdio is not robust about handling signals,
13359 which can cause an apparent I/O error. */
13360 if (interrupt_input)
13361 unrequest_sigio ();
13362 STOP_POLLING;
13363
13364 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13365 {
13366 if (hscroll_windows (selected_window))
13367 goto retry;
13368
13369 XWINDOW (selected_window)->must_be_updated_p = 1;
13370 pending = update_frame (sf, 0, 0);
13371 }
13372
13373 /* We may have called echo_area_display at the top of this
13374 function. If the echo area is on another frame, that may
13375 have put text on a frame other than the selected one, so the
13376 above call to update_frame would not have caught it. Catch
13377 it here. */
13378 mini_window = FRAME_MINIBUF_WINDOW (sf);
13379 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13380
13381 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13382 {
13383 XWINDOW (mini_window)->must_be_updated_p = 1;
13384 pending |= update_frame (mini_frame, 0, 0);
13385 if (!pending && hscroll_windows (mini_window))
13386 goto retry;
13387 }
13388 }
13389
13390 /* If display was paused because of pending input, make sure we do a
13391 thorough update the next time. */
13392 if (pending)
13393 {
13394 /* Prevent the optimization at the beginning of
13395 redisplay_internal that tries a single-line update of the
13396 line containing the cursor in the selected window. */
13397 CHARPOS (this_line_start_pos) = 0;
13398
13399 /* Let the overlay arrow be updated the next time. */
13400 update_overlay_arrows (0);
13401
13402 /* If we pause after scrolling, some rows in the current
13403 matrices of some windows are not valid. */
13404 if (!WINDOW_FULL_WIDTH_P (w)
13405 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13406 update_mode_lines = 1;
13407 }
13408 else
13409 {
13410 if (!consider_all_windows_p)
13411 {
13412 /* This has already been done above if
13413 consider_all_windows_p is set. */
13414 mark_window_display_accurate_1 (w, 1);
13415
13416 /* Say overlay arrows are up to date. */
13417 update_overlay_arrows (1);
13418
13419 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13420 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13421 }
13422
13423 update_mode_lines = 0;
13424 windows_or_buffers_changed = 0;
13425 cursor_type_changed = 0;
13426 }
13427
13428 /* Start SIGIO interrupts coming again. Having them off during the
13429 code above makes it less likely one will discard output, but not
13430 impossible, since there might be stuff in the system buffer here.
13431 But it is much hairier to try to do anything about that. */
13432 if (interrupt_input)
13433 request_sigio ();
13434 RESUME_POLLING;
13435
13436 /* If a frame has become visible which was not before, redisplay
13437 again, so that we display it. Expose events for such a frame
13438 (which it gets when becoming visible) don't call the parts of
13439 redisplay constructing glyphs, so simply exposing a frame won't
13440 display anything in this case. So, we have to display these
13441 frames here explicitly. */
13442 if (!pending)
13443 {
13444 Lisp_Object tail, frame;
13445 int new_count = 0;
13446
13447 FOR_EACH_FRAME (tail, frame)
13448 {
13449 int this_is_visible = 0;
13450
13451 if (XFRAME (frame)->visible)
13452 this_is_visible = 1;
13453 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13454 if (XFRAME (frame)->visible)
13455 this_is_visible = 1;
13456
13457 if (this_is_visible)
13458 new_count++;
13459 }
13460
13461 if (new_count != number_of_visible_frames)
13462 windows_or_buffers_changed++;
13463 }
13464
13465 /* Change frame size now if a change is pending. */
13466 do_pending_window_change (1);
13467
13468 /* If we just did a pending size change, or have additional
13469 visible frames, or selected_window changed, redisplay again. */
13470 if ((windows_or_buffers_changed && !pending)
13471 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13472 goto retry;
13473
13474 /* Clear the face and image caches.
13475
13476 We used to do this only if consider_all_windows_p. But the cache
13477 needs to be cleared if a timer creates images in the current
13478 buffer (e.g. the test case in Bug#6230). */
13479
13480 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13481 {
13482 clear_face_cache (0);
13483 clear_face_cache_count = 0;
13484 }
13485
13486 #ifdef HAVE_WINDOW_SYSTEM
13487 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13488 {
13489 clear_image_caches (Qnil);
13490 clear_image_cache_count = 0;
13491 }
13492 #endif /* HAVE_WINDOW_SYSTEM */
13493
13494 end_of_redisplay:
13495 unbind_to (count, Qnil);
13496 RESUME_POLLING;
13497 }
13498
13499
13500 /* Redisplay, but leave alone any recent echo area message unless
13501 another message has been requested in its place.
13502
13503 This is useful in situations where you need to redisplay but no
13504 user action has occurred, making it inappropriate for the message
13505 area to be cleared. See tracking_off and
13506 wait_reading_process_output for examples of these situations.
13507
13508 FROM_WHERE is an integer saying from where this function was
13509 called. This is useful for debugging. */
13510
13511 void
13512 redisplay_preserve_echo_area (int from_where)
13513 {
13514 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13515
13516 if (!NILP (echo_area_buffer[1]))
13517 {
13518 /* We have a previously displayed message, but no current
13519 message. Redisplay the previous message. */
13520 display_last_displayed_message_p = 1;
13521 redisplay_internal ();
13522 display_last_displayed_message_p = 0;
13523 }
13524 else
13525 redisplay_internal ();
13526
13527 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13528 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13529 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13530 }
13531
13532
13533 /* Function registered with record_unwind_protect in
13534 redisplay_internal. Reset redisplaying_p to the value it had
13535 before redisplay_internal was called, and clear
13536 prevent_freeing_realized_faces_p. It also selects the previously
13537 selected frame, unless it has been deleted (by an X connection
13538 failure during redisplay, for example). */
13539
13540 static Lisp_Object
13541 unwind_redisplay (Lisp_Object val)
13542 {
13543 Lisp_Object old_redisplaying_p, old_frame;
13544
13545 old_redisplaying_p = XCAR (val);
13546 redisplaying_p = XFASTINT (old_redisplaying_p);
13547 old_frame = XCDR (val);
13548 if (! EQ (old_frame, selected_frame)
13549 && FRAME_LIVE_P (XFRAME (old_frame)))
13550 select_frame_for_redisplay (old_frame);
13551 return Qnil;
13552 }
13553
13554
13555 /* Mark the display of window W as accurate or inaccurate. If
13556 ACCURATE_P is non-zero mark display of W as accurate. If
13557 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13558 redisplay_internal is called. */
13559
13560 static void
13561 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13562 {
13563 if (BUFFERP (w->buffer))
13564 {
13565 struct buffer *b = XBUFFER (w->buffer);
13566
13567 w->last_modified
13568 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13569 w->last_overlay_modified
13570 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13571 w->last_had_star
13572 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13573
13574 if (accurate_p)
13575 {
13576 b->clip_changed = 0;
13577 b->prevent_redisplay_optimizations_p = 0;
13578
13579 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13580 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13581 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13582 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13583
13584 w->current_matrix->buffer = b;
13585 w->current_matrix->begv = BUF_BEGV (b);
13586 w->current_matrix->zv = BUF_ZV (b);
13587
13588 w->last_cursor = w->cursor;
13589 w->last_cursor_off_p = w->cursor_off_p;
13590
13591 if (w == XWINDOW (selected_window))
13592 w->last_point = make_number (BUF_PT (b));
13593 else
13594 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13595 }
13596 }
13597
13598 if (accurate_p)
13599 {
13600 w->window_end_valid = w->buffer;
13601 w->update_mode_line = Qnil;
13602 }
13603 }
13604
13605
13606 /* Mark the display of windows in the window tree rooted at WINDOW as
13607 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13608 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13609 be redisplayed the next time redisplay_internal is called. */
13610
13611 void
13612 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13613 {
13614 struct window *w;
13615
13616 for (; !NILP (window); window = w->next)
13617 {
13618 w = XWINDOW (window);
13619 mark_window_display_accurate_1 (w, accurate_p);
13620
13621 if (!NILP (w->vchild))
13622 mark_window_display_accurate (w->vchild, accurate_p);
13623 if (!NILP (w->hchild))
13624 mark_window_display_accurate (w->hchild, accurate_p);
13625 }
13626
13627 if (accurate_p)
13628 {
13629 update_overlay_arrows (1);
13630 }
13631 else
13632 {
13633 /* Force a thorough redisplay the next time by setting
13634 last_arrow_position and last_arrow_string to t, which is
13635 unequal to any useful value of Voverlay_arrow_... */
13636 update_overlay_arrows (-1);
13637 }
13638 }
13639
13640
13641 /* Return value in display table DP (Lisp_Char_Table *) for character
13642 C. Since a display table doesn't have any parent, we don't have to
13643 follow parent. Do not call this function directly but use the
13644 macro DISP_CHAR_VECTOR. */
13645
13646 Lisp_Object
13647 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13648 {
13649 Lisp_Object val;
13650
13651 if (ASCII_CHAR_P (c))
13652 {
13653 val = dp->ascii;
13654 if (SUB_CHAR_TABLE_P (val))
13655 val = XSUB_CHAR_TABLE (val)->contents[c];
13656 }
13657 else
13658 {
13659 Lisp_Object table;
13660
13661 XSETCHAR_TABLE (table, dp);
13662 val = char_table_ref (table, c);
13663 }
13664 if (NILP (val))
13665 val = dp->defalt;
13666 return val;
13667 }
13668
13669
13670 \f
13671 /***********************************************************************
13672 Window Redisplay
13673 ***********************************************************************/
13674
13675 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13676
13677 static void
13678 redisplay_windows (Lisp_Object window)
13679 {
13680 while (!NILP (window))
13681 {
13682 struct window *w = XWINDOW (window);
13683
13684 if (!NILP (w->hchild))
13685 redisplay_windows (w->hchild);
13686 else if (!NILP (w->vchild))
13687 redisplay_windows (w->vchild);
13688 else if (!NILP (w->buffer))
13689 {
13690 displayed_buffer = XBUFFER (w->buffer);
13691 /* Use list_of_error, not Qerror, so that
13692 we catch only errors and don't run the debugger. */
13693 internal_condition_case_1 (redisplay_window_0, window,
13694 list_of_error,
13695 redisplay_window_error);
13696 }
13697
13698 window = w->next;
13699 }
13700 }
13701
13702 static Lisp_Object
13703 redisplay_window_error (Lisp_Object ignore)
13704 {
13705 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13706 return Qnil;
13707 }
13708
13709 static Lisp_Object
13710 redisplay_window_0 (Lisp_Object window)
13711 {
13712 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13713 redisplay_window (window, 0);
13714 return Qnil;
13715 }
13716
13717 static Lisp_Object
13718 redisplay_window_1 (Lisp_Object window)
13719 {
13720 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13721 redisplay_window (window, 1);
13722 return Qnil;
13723 }
13724 \f
13725
13726 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13727 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13728 which positions recorded in ROW differ from current buffer
13729 positions.
13730
13731 Return 0 if cursor is not on this row, 1 otherwise. */
13732
13733 static int
13734 set_cursor_from_row (struct window *w, struct glyph_row *row,
13735 struct glyph_matrix *matrix,
13736 EMACS_INT delta, EMACS_INT delta_bytes,
13737 int dy, int dvpos)
13738 {
13739 struct glyph *glyph = row->glyphs[TEXT_AREA];
13740 struct glyph *end = glyph + row->used[TEXT_AREA];
13741 struct glyph *cursor = NULL;
13742 /* The last known character position in row. */
13743 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13744 int x = row->x;
13745 EMACS_INT pt_old = PT - delta;
13746 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13747 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13748 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13749 /* A glyph beyond the edge of TEXT_AREA which we should never
13750 touch. */
13751 struct glyph *glyphs_end = end;
13752 /* Non-zero means we've found a match for cursor position, but that
13753 glyph has the avoid_cursor_p flag set. */
13754 int match_with_avoid_cursor = 0;
13755 /* Non-zero means we've seen at least one glyph that came from a
13756 display string. */
13757 int string_seen = 0;
13758 /* Largest and smallest buffer positions seen so far during scan of
13759 glyph row. */
13760 EMACS_INT bpos_max = pos_before;
13761 EMACS_INT bpos_min = pos_after;
13762 /* Last buffer position covered by an overlay string with an integer
13763 `cursor' property. */
13764 EMACS_INT bpos_covered = 0;
13765 /* Non-zero means the display string on which to display the cursor
13766 comes from a text property, not from an overlay. */
13767 int string_from_text_prop = 0;
13768
13769 /* Don't even try doing anything if called for a mode-line or
13770 header-line row, since the rest of the code isn't prepared to
13771 deal with such calamities. */
13772 xassert (!row->mode_line_p);
13773 if (row->mode_line_p)
13774 return 0;
13775
13776 /* Skip over glyphs not having an object at the start and the end of
13777 the row. These are special glyphs like truncation marks on
13778 terminal frames. */
13779 if (row->displays_text_p)
13780 {
13781 if (!row->reversed_p)
13782 {
13783 while (glyph < end
13784 && INTEGERP (glyph->object)
13785 && glyph->charpos < 0)
13786 {
13787 x += glyph->pixel_width;
13788 ++glyph;
13789 }
13790 while (end > glyph
13791 && INTEGERP ((end - 1)->object)
13792 /* CHARPOS is zero for blanks and stretch glyphs
13793 inserted by extend_face_to_end_of_line. */
13794 && (end - 1)->charpos <= 0)
13795 --end;
13796 glyph_before = glyph - 1;
13797 glyph_after = end;
13798 }
13799 else
13800 {
13801 struct glyph *g;
13802
13803 /* If the glyph row is reversed, we need to process it from back
13804 to front, so swap the edge pointers. */
13805 glyphs_end = end = glyph - 1;
13806 glyph += row->used[TEXT_AREA] - 1;
13807
13808 while (glyph > end + 1
13809 && INTEGERP (glyph->object)
13810 && glyph->charpos < 0)
13811 {
13812 --glyph;
13813 x -= glyph->pixel_width;
13814 }
13815 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13816 --glyph;
13817 /* By default, in reversed rows we put the cursor on the
13818 rightmost (first in the reading order) glyph. */
13819 for (g = end + 1; g < glyph; g++)
13820 x += g->pixel_width;
13821 while (end < glyph
13822 && INTEGERP ((end + 1)->object)
13823 && (end + 1)->charpos <= 0)
13824 ++end;
13825 glyph_before = glyph + 1;
13826 glyph_after = end;
13827 }
13828 }
13829 else if (row->reversed_p)
13830 {
13831 /* In R2L rows that don't display text, put the cursor on the
13832 rightmost glyph. Case in point: an empty last line that is
13833 part of an R2L paragraph. */
13834 cursor = end - 1;
13835 /* Avoid placing the cursor on the last glyph of the row, where
13836 on terminal frames we hold the vertical border between
13837 adjacent windows. */
13838 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13839 && !WINDOW_RIGHTMOST_P (w)
13840 && cursor == row->glyphs[LAST_AREA] - 1)
13841 cursor--;
13842 x = -1; /* will be computed below, at label compute_x */
13843 }
13844
13845 /* Step 1: Try to find the glyph whose character position
13846 corresponds to point. If that's not possible, find 2 glyphs
13847 whose character positions are the closest to point, one before
13848 point, the other after it. */
13849 if (!row->reversed_p)
13850 while (/* not marched to end of glyph row */
13851 glyph < end
13852 /* glyph was not inserted by redisplay for internal purposes */
13853 && !INTEGERP (glyph->object))
13854 {
13855 if (BUFFERP (glyph->object))
13856 {
13857 EMACS_INT dpos = glyph->charpos - pt_old;
13858
13859 if (glyph->charpos > bpos_max)
13860 bpos_max = glyph->charpos;
13861 if (glyph->charpos < bpos_min)
13862 bpos_min = glyph->charpos;
13863 if (!glyph->avoid_cursor_p)
13864 {
13865 /* If we hit point, we've found the glyph on which to
13866 display the cursor. */
13867 if (dpos == 0)
13868 {
13869 match_with_avoid_cursor = 0;
13870 break;
13871 }
13872 /* See if we've found a better approximation to
13873 POS_BEFORE or to POS_AFTER. Note that we want the
13874 first (leftmost) glyph of all those that are the
13875 closest from below, and the last (rightmost) of all
13876 those from above. */
13877 if (0 > dpos && dpos > pos_before - pt_old)
13878 {
13879 pos_before = glyph->charpos;
13880 glyph_before = glyph;
13881 }
13882 else if (0 < dpos && dpos <= pos_after - pt_old)
13883 {
13884 pos_after = glyph->charpos;
13885 glyph_after = glyph;
13886 }
13887 }
13888 else if (dpos == 0)
13889 match_with_avoid_cursor = 1;
13890 }
13891 else if (STRINGP (glyph->object))
13892 {
13893 Lisp_Object chprop;
13894 EMACS_INT glyph_pos = glyph->charpos;
13895
13896 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13897 glyph->object);
13898 if (!NILP (chprop))
13899 {
13900 /* If the string came from a `display' text property,
13901 look up the buffer position of that property and
13902 use that position to update bpos_max, as if we
13903 actually saw such a position in one of the row's
13904 glyphs. This helps with supporting integer values
13905 of `cursor' property on the display string in
13906 situations where most or all of the row's buffer
13907 text is completely covered by display properties,
13908 so that no glyph with valid buffer positions is
13909 ever seen in the row. */
13910 EMACS_INT prop_pos =
13911 string_buffer_position_lim (glyph->object, pos_before,
13912 pos_after, 0);
13913
13914 if (prop_pos >= pos_before)
13915 bpos_max = prop_pos - 1;
13916 }
13917 if (INTEGERP (chprop))
13918 {
13919 bpos_covered = bpos_max + XINT (chprop);
13920 /* If the `cursor' property covers buffer positions up
13921 to and including point, we should display cursor on
13922 this glyph. Note that, if a `cursor' property on one
13923 of the string's characters has an integer value, we
13924 will break out of the loop below _before_ we get to
13925 the position match above. IOW, integer values of
13926 the `cursor' property override the "exact match for
13927 point" strategy of positioning the cursor. */
13928 /* Implementation note: bpos_max == pt_old when, e.g.,
13929 we are in an empty line, where bpos_max is set to
13930 MATRIX_ROW_START_CHARPOS, see above. */
13931 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13932 {
13933 cursor = glyph;
13934 break;
13935 }
13936 }
13937
13938 string_seen = 1;
13939 }
13940 x += glyph->pixel_width;
13941 ++glyph;
13942 }
13943 else if (glyph > end) /* row is reversed */
13944 while (!INTEGERP (glyph->object))
13945 {
13946 if (BUFFERP (glyph->object))
13947 {
13948 EMACS_INT dpos = glyph->charpos - pt_old;
13949
13950 if (glyph->charpos > bpos_max)
13951 bpos_max = glyph->charpos;
13952 if (glyph->charpos < bpos_min)
13953 bpos_min = glyph->charpos;
13954 if (!glyph->avoid_cursor_p)
13955 {
13956 if (dpos == 0)
13957 {
13958 match_with_avoid_cursor = 0;
13959 break;
13960 }
13961 if (0 > dpos && dpos > pos_before - pt_old)
13962 {
13963 pos_before = glyph->charpos;
13964 glyph_before = glyph;
13965 }
13966 else if (0 < dpos && dpos <= pos_after - pt_old)
13967 {
13968 pos_after = glyph->charpos;
13969 glyph_after = glyph;
13970 }
13971 }
13972 else if (dpos == 0)
13973 match_with_avoid_cursor = 1;
13974 }
13975 else if (STRINGP (glyph->object))
13976 {
13977 Lisp_Object chprop;
13978 EMACS_INT glyph_pos = glyph->charpos;
13979
13980 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13981 glyph->object);
13982 if (!NILP (chprop))
13983 {
13984 EMACS_INT prop_pos =
13985 string_buffer_position_lim (glyph->object, pos_before,
13986 pos_after, 0);
13987
13988 if (prop_pos >= pos_before)
13989 bpos_max = prop_pos - 1;
13990 }
13991 if (INTEGERP (chprop))
13992 {
13993 bpos_covered = bpos_max + XINT (chprop);
13994 /* If the `cursor' property covers buffer positions up
13995 to and including point, we should display cursor on
13996 this glyph. */
13997 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13998 {
13999 cursor = glyph;
14000 break;
14001 }
14002 }
14003 string_seen = 1;
14004 }
14005 --glyph;
14006 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14007 {
14008 x--; /* can't use any pixel_width */
14009 break;
14010 }
14011 x -= glyph->pixel_width;
14012 }
14013
14014 /* Step 2: If we didn't find an exact match for point, we need to
14015 look for a proper place to put the cursor among glyphs between
14016 GLYPH_BEFORE and GLYPH_AFTER. */
14017 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14018 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14019 && bpos_covered < pt_old)
14020 {
14021 /* An empty line has a single glyph whose OBJECT is zero and
14022 whose CHARPOS is the position of a newline on that line.
14023 Note that on a TTY, there are more glyphs after that, which
14024 were produced by extend_face_to_end_of_line, but their
14025 CHARPOS is zero or negative. */
14026 int empty_line_p =
14027 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14028 && INTEGERP (glyph->object) && glyph->charpos > 0;
14029
14030 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14031 {
14032 EMACS_INT ellipsis_pos;
14033
14034 /* Scan back over the ellipsis glyphs. */
14035 if (!row->reversed_p)
14036 {
14037 ellipsis_pos = (glyph - 1)->charpos;
14038 while (glyph > row->glyphs[TEXT_AREA]
14039 && (glyph - 1)->charpos == ellipsis_pos)
14040 glyph--, x -= glyph->pixel_width;
14041 /* That loop always goes one position too far, including
14042 the glyph before the ellipsis. So scan forward over
14043 that one. */
14044 x += glyph->pixel_width;
14045 glyph++;
14046 }
14047 else /* row is reversed */
14048 {
14049 ellipsis_pos = (glyph + 1)->charpos;
14050 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14051 && (glyph + 1)->charpos == ellipsis_pos)
14052 glyph++, x += glyph->pixel_width;
14053 x -= glyph->pixel_width;
14054 glyph--;
14055 }
14056 }
14057 else if (match_with_avoid_cursor)
14058 {
14059 cursor = glyph_after;
14060 x = -1;
14061 }
14062 else if (string_seen)
14063 {
14064 int incr = row->reversed_p ? -1 : +1;
14065
14066 /* Need to find the glyph that came out of a string which is
14067 present at point. That glyph is somewhere between
14068 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14069 positioned between POS_BEFORE and POS_AFTER in the
14070 buffer. */
14071 struct glyph *start, *stop;
14072 EMACS_INT pos = pos_before;
14073
14074 x = -1;
14075
14076 /* If the row ends in a newline from a display string,
14077 reordering could have moved the glyphs belonging to the
14078 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14079 in this case we extend the search to the last glyph in
14080 the row that was not inserted by redisplay. */
14081 if (row->ends_in_newline_from_string_p)
14082 {
14083 glyph_after = end;
14084 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14085 }
14086
14087 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14088 correspond to POS_BEFORE and POS_AFTER, respectively. We
14089 need START and STOP in the order that corresponds to the
14090 row's direction as given by its reversed_p flag. If the
14091 directionality of characters between POS_BEFORE and
14092 POS_AFTER is the opposite of the row's base direction,
14093 these characters will have been reordered for display,
14094 and we need to reverse START and STOP. */
14095 if (!row->reversed_p)
14096 {
14097 start = min (glyph_before, glyph_after);
14098 stop = max (glyph_before, glyph_after);
14099 }
14100 else
14101 {
14102 start = max (glyph_before, glyph_after);
14103 stop = min (glyph_before, glyph_after);
14104 }
14105 for (glyph = start + incr;
14106 row->reversed_p ? glyph > stop : glyph < stop; )
14107 {
14108
14109 /* Any glyphs that come from the buffer are here because
14110 of bidi reordering. Skip them, and only pay
14111 attention to glyphs that came from some string. */
14112 if (STRINGP (glyph->object))
14113 {
14114 Lisp_Object str;
14115 EMACS_INT tem;
14116 /* If the display property covers the newline, we
14117 need to search for it one position farther. */
14118 EMACS_INT lim = pos_after
14119 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14120
14121 string_from_text_prop = 0;
14122 str = glyph->object;
14123 tem = string_buffer_position_lim (str, pos, lim, 0);
14124 if (tem == 0 /* from overlay */
14125 || pos <= tem)
14126 {
14127 /* If the string from which this glyph came is
14128 found in the buffer at point, then we've
14129 found the glyph we've been looking for. If
14130 it comes from an overlay (tem == 0), and it
14131 has the `cursor' property on one of its
14132 glyphs, record that glyph as a candidate for
14133 displaying the cursor. (As in the
14134 unidirectional version, we will display the
14135 cursor on the last candidate we find.) */
14136 if (tem == 0 || tem == pt_old)
14137 {
14138 /* The glyphs from this string could have
14139 been reordered. Find the one with the
14140 smallest string position. Or there could
14141 be a character in the string with the
14142 `cursor' property, which means display
14143 cursor on that character's glyph. */
14144 EMACS_INT strpos = glyph->charpos;
14145
14146 if (tem)
14147 {
14148 cursor = glyph;
14149 string_from_text_prop = 1;
14150 }
14151 for ( ;
14152 (row->reversed_p ? glyph > stop : glyph < stop)
14153 && EQ (glyph->object, str);
14154 glyph += incr)
14155 {
14156 Lisp_Object cprop;
14157 EMACS_INT gpos = glyph->charpos;
14158
14159 cprop = Fget_char_property (make_number (gpos),
14160 Qcursor,
14161 glyph->object);
14162 if (!NILP (cprop))
14163 {
14164 cursor = glyph;
14165 break;
14166 }
14167 if (tem && glyph->charpos < strpos)
14168 {
14169 strpos = glyph->charpos;
14170 cursor = glyph;
14171 }
14172 }
14173
14174 if (tem == pt_old)
14175 goto compute_x;
14176 }
14177 if (tem)
14178 pos = tem + 1; /* don't find previous instances */
14179 }
14180 /* This string is not what we want; skip all of the
14181 glyphs that came from it. */
14182 while ((row->reversed_p ? glyph > stop : glyph < stop)
14183 && EQ (glyph->object, str))
14184 glyph += incr;
14185 }
14186 else
14187 glyph += incr;
14188 }
14189
14190 /* If we reached the end of the line, and END was from a string,
14191 the cursor is not on this line. */
14192 if (cursor == NULL
14193 && (row->reversed_p ? glyph <= end : glyph >= end)
14194 && STRINGP (end->object)
14195 && row->continued_p)
14196 return 0;
14197 }
14198 /* A truncated row may not include PT among its character positions.
14199 Setting the cursor inside the scroll margin will trigger
14200 recalculation of hscroll in hscroll_window_tree. But if a
14201 display string covers point, defer to the string-handling
14202 code below to figure this out. */
14203 else if (row->truncated_on_left_p && pt_old < bpos_min)
14204 {
14205 cursor = glyph_before;
14206 x = -1;
14207 }
14208 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14209 /* Zero-width characters produce no glyphs. */
14210 || (!empty_line_p
14211 && (row->reversed_p
14212 ? glyph_after > glyphs_end
14213 : glyph_after < glyphs_end)))
14214 {
14215 cursor = glyph_after;
14216 x = -1;
14217 }
14218 }
14219
14220 compute_x:
14221 if (cursor != NULL)
14222 glyph = cursor;
14223 if (x < 0)
14224 {
14225 struct glyph *g;
14226
14227 /* Need to compute x that corresponds to GLYPH. */
14228 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14229 {
14230 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14231 abort ();
14232 x += g->pixel_width;
14233 }
14234 }
14235
14236 /* ROW could be part of a continued line, which, under bidi
14237 reordering, might have other rows whose start and end charpos
14238 occlude point. Only set w->cursor if we found a better
14239 approximation to the cursor position than we have from previously
14240 examined candidate rows belonging to the same continued line. */
14241 if (/* we already have a candidate row */
14242 w->cursor.vpos >= 0
14243 /* that candidate is not the row we are processing */
14244 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14245 /* Make sure cursor.vpos specifies a row whose start and end
14246 charpos occlude point, and it is valid candidate for being a
14247 cursor-row. This is because some callers of this function
14248 leave cursor.vpos at the row where the cursor was displayed
14249 during the last redisplay cycle. */
14250 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14251 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14252 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14253 {
14254 struct glyph *g1 =
14255 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14256
14257 /* Don't consider glyphs that are outside TEXT_AREA. */
14258 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14259 return 0;
14260 /* Keep the candidate whose buffer position is the closest to
14261 point or has the `cursor' property. */
14262 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14263 w->cursor.hpos >= 0
14264 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14265 && ((BUFFERP (g1->object)
14266 && (g1->charpos == pt_old /* an exact match always wins */
14267 || (BUFFERP (glyph->object)
14268 && eabs (g1->charpos - pt_old)
14269 < eabs (glyph->charpos - pt_old))))
14270 /* previous candidate is a glyph from a string that has
14271 a non-nil `cursor' property */
14272 || (STRINGP (g1->object)
14273 && (!NILP (Fget_char_property (make_number (g1->charpos),
14274 Qcursor, g1->object))
14275 /* previous candidate is from the same display
14276 string as this one, and the display string
14277 came from a text property */
14278 || (EQ (g1->object, glyph->object)
14279 && string_from_text_prop)
14280 /* this candidate is from newline and its
14281 position is not an exact match */
14282 || (INTEGERP (glyph->object)
14283 && glyph->charpos != pt_old)))))
14284 return 0;
14285 /* If this candidate gives an exact match, use that. */
14286 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14287 /* If this candidate is a glyph created for the
14288 terminating newline of a line, and point is on that
14289 newline, it wins because it's an exact match. */
14290 || (!row->continued_p
14291 && INTEGERP (glyph->object)
14292 && glyph->charpos == 0
14293 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14294 /* Otherwise, keep the candidate that comes from a row
14295 spanning less buffer positions. This may win when one or
14296 both candidate positions are on glyphs that came from
14297 display strings, for which we cannot compare buffer
14298 positions. */
14299 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14300 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14301 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14302 return 0;
14303 }
14304 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14305 w->cursor.x = x;
14306 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14307 w->cursor.y = row->y + dy;
14308
14309 if (w == XWINDOW (selected_window))
14310 {
14311 if (!row->continued_p
14312 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14313 && row->x == 0)
14314 {
14315 this_line_buffer = XBUFFER (w->buffer);
14316
14317 CHARPOS (this_line_start_pos)
14318 = MATRIX_ROW_START_CHARPOS (row) + delta;
14319 BYTEPOS (this_line_start_pos)
14320 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14321
14322 CHARPOS (this_line_end_pos)
14323 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14324 BYTEPOS (this_line_end_pos)
14325 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14326
14327 this_line_y = w->cursor.y;
14328 this_line_pixel_height = row->height;
14329 this_line_vpos = w->cursor.vpos;
14330 this_line_start_x = row->x;
14331 }
14332 else
14333 CHARPOS (this_line_start_pos) = 0;
14334 }
14335
14336 return 1;
14337 }
14338
14339
14340 /* Run window scroll functions, if any, for WINDOW with new window
14341 start STARTP. Sets the window start of WINDOW to that position.
14342
14343 We assume that the window's buffer is really current. */
14344
14345 static inline struct text_pos
14346 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14347 {
14348 struct window *w = XWINDOW (window);
14349 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14350
14351 if (current_buffer != XBUFFER (w->buffer))
14352 abort ();
14353
14354 if (!NILP (Vwindow_scroll_functions))
14355 {
14356 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14357 make_number (CHARPOS (startp)));
14358 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14359 /* In case the hook functions switch buffers. */
14360 if (current_buffer != XBUFFER (w->buffer))
14361 set_buffer_internal_1 (XBUFFER (w->buffer));
14362 }
14363
14364 return startp;
14365 }
14366
14367
14368 /* Make sure the line containing the cursor is fully visible.
14369 A value of 1 means there is nothing to be done.
14370 (Either the line is fully visible, or it cannot be made so,
14371 or we cannot tell.)
14372
14373 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14374 is higher than window.
14375
14376 A value of 0 means the caller should do scrolling
14377 as if point had gone off the screen. */
14378
14379 static int
14380 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14381 {
14382 struct glyph_matrix *matrix;
14383 struct glyph_row *row;
14384 int window_height;
14385
14386 if (!make_cursor_line_fully_visible_p)
14387 return 1;
14388
14389 /* It's not always possible to find the cursor, e.g, when a window
14390 is full of overlay strings. Don't do anything in that case. */
14391 if (w->cursor.vpos < 0)
14392 return 1;
14393
14394 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14395 row = MATRIX_ROW (matrix, w->cursor.vpos);
14396
14397 /* If the cursor row is not partially visible, there's nothing to do. */
14398 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14399 return 1;
14400
14401 /* If the row the cursor is in is taller than the window's height,
14402 it's not clear what to do, so do nothing. */
14403 window_height = window_box_height (w);
14404 if (row->height >= window_height)
14405 {
14406 if (!force_p || MINI_WINDOW_P (w)
14407 || w->vscroll || w->cursor.vpos == 0)
14408 return 1;
14409 }
14410 return 0;
14411 }
14412
14413
14414 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14415 non-zero means only WINDOW is redisplayed in redisplay_internal.
14416 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14417 in redisplay_window to bring a partially visible line into view in
14418 the case that only the cursor has moved.
14419
14420 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14421 last screen line's vertical height extends past the end of the screen.
14422
14423 Value is
14424
14425 1 if scrolling succeeded
14426
14427 0 if scrolling didn't find point.
14428
14429 -1 if new fonts have been loaded so that we must interrupt
14430 redisplay, adjust glyph matrices, and try again. */
14431
14432 enum
14433 {
14434 SCROLLING_SUCCESS,
14435 SCROLLING_FAILED,
14436 SCROLLING_NEED_LARGER_MATRICES
14437 };
14438
14439 /* If scroll-conservatively is more than this, never recenter.
14440
14441 If you change this, don't forget to update the doc string of
14442 `scroll-conservatively' and the Emacs manual. */
14443 #define SCROLL_LIMIT 100
14444
14445 static int
14446 try_scrolling (Lisp_Object window, int just_this_one_p,
14447 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14448 int temp_scroll_step, int last_line_misfit)
14449 {
14450 struct window *w = XWINDOW (window);
14451 struct frame *f = XFRAME (w->frame);
14452 struct text_pos pos, startp;
14453 struct it it;
14454 int this_scroll_margin, scroll_max, rc, height;
14455 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14456 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14457 Lisp_Object aggressive;
14458 /* We will never try scrolling more than this number of lines. */
14459 int scroll_limit = SCROLL_LIMIT;
14460
14461 #if GLYPH_DEBUG
14462 debug_method_add (w, "try_scrolling");
14463 #endif
14464
14465 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14466
14467 /* Compute scroll margin height in pixels. We scroll when point is
14468 within this distance from the top or bottom of the window. */
14469 if (scroll_margin > 0)
14470 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14471 * FRAME_LINE_HEIGHT (f);
14472 else
14473 this_scroll_margin = 0;
14474
14475 /* Force arg_scroll_conservatively to have a reasonable value, to
14476 avoid scrolling too far away with slow move_it_* functions. Note
14477 that the user can supply scroll-conservatively equal to
14478 `most-positive-fixnum', which can be larger than INT_MAX. */
14479 if (arg_scroll_conservatively > scroll_limit)
14480 {
14481 arg_scroll_conservatively = scroll_limit + 1;
14482 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14483 }
14484 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14485 /* Compute how much we should try to scroll maximally to bring
14486 point into view. */
14487 scroll_max = (max (scroll_step,
14488 max (arg_scroll_conservatively, temp_scroll_step))
14489 * FRAME_LINE_HEIGHT (f));
14490 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14491 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14492 /* We're trying to scroll because of aggressive scrolling but no
14493 scroll_step is set. Choose an arbitrary one. */
14494 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14495 else
14496 scroll_max = 0;
14497
14498 too_near_end:
14499
14500 /* Decide whether to scroll down. */
14501 if (PT > CHARPOS (startp))
14502 {
14503 int scroll_margin_y;
14504
14505 /* Compute the pixel ypos of the scroll margin, then move IT to
14506 either that ypos or PT, whichever comes first. */
14507 start_display (&it, w, startp);
14508 scroll_margin_y = it.last_visible_y - this_scroll_margin
14509 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14510 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14511 (MOVE_TO_POS | MOVE_TO_Y));
14512
14513 if (PT > CHARPOS (it.current.pos))
14514 {
14515 int y0 = line_bottom_y (&it);
14516 /* Compute how many pixels below window bottom to stop searching
14517 for PT. This avoids costly search for PT that is far away if
14518 the user limited scrolling by a small number of lines, but
14519 always finds PT if scroll_conservatively is set to a large
14520 number, such as most-positive-fixnum. */
14521 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14522 int y_to_move = it.last_visible_y + slack;
14523
14524 /* Compute the distance from the scroll margin to PT or to
14525 the scroll limit, whichever comes first. This should
14526 include the height of the cursor line, to make that line
14527 fully visible. */
14528 move_it_to (&it, PT, -1, y_to_move,
14529 -1, MOVE_TO_POS | MOVE_TO_Y);
14530 dy = line_bottom_y (&it) - y0;
14531
14532 if (dy > scroll_max)
14533 return SCROLLING_FAILED;
14534
14535 if (dy > 0)
14536 scroll_down_p = 1;
14537 }
14538 }
14539
14540 if (scroll_down_p)
14541 {
14542 /* Point is in or below the bottom scroll margin, so move the
14543 window start down. If scrolling conservatively, move it just
14544 enough down to make point visible. If scroll_step is set,
14545 move it down by scroll_step. */
14546 if (arg_scroll_conservatively)
14547 amount_to_scroll
14548 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14549 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14550 else if (scroll_step || temp_scroll_step)
14551 amount_to_scroll = scroll_max;
14552 else
14553 {
14554 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14555 height = WINDOW_BOX_TEXT_HEIGHT (w);
14556 if (NUMBERP (aggressive))
14557 {
14558 double float_amount = XFLOATINT (aggressive) * height;
14559 amount_to_scroll = float_amount;
14560 if (amount_to_scroll == 0 && float_amount > 0)
14561 amount_to_scroll = 1;
14562 /* Don't let point enter the scroll margin near top of
14563 the window. */
14564 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14565 amount_to_scroll = height - 2*this_scroll_margin + dy;
14566 }
14567 }
14568
14569 if (amount_to_scroll <= 0)
14570 return SCROLLING_FAILED;
14571
14572 start_display (&it, w, startp);
14573 if (arg_scroll_conservatively <= scroll_limit)
14574 move_it_vertically (&it, amount_to_scroll);
14575 else
14576 {
14577 /* Extra precision for users who set scroll-conservatively
14578 to a large number: make sure the amount we scroll
14579 the window start is never less than amount_to_scroll,
14580 which was computed as distance from window bottom to
14581 point. This matters when lines at window top and lines
14582 below window bottom have different height. */
14583 struct it it1;
14584 void *it1data = NULL;
14585 /* We use a temporary it1 because line_bottom_y can modify
14586 its argument, if it moves one line down; see there. */
14587 int start_y;
14588
14589 SAVE_IT (it1, it, it1data);
14590 start_y = line_bottom_y (&it1);
14591 do {
14592 RESTORE_IT (&it, &it, it1data);
14593 move_it_by_lines (&it, 1);
14594 SAVE_IT (it1, it, it1data);
14595 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14596 }
14597
14598 /* If STARTP is unchanged, move it down another screen line. */
14599 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14600 move_it_by_lines (&it, 1);
14601 startp = it.current.pos;
14602 }
14603 else
14604 {
14605 struct text_pos scroll_margin_pos = startp;
14606
14607 /* See if point is inside the scroll margin at the top of the
14608 window. */
14609 if (this_scroll_margin)
14610 {
14611 start_display (&it, w, startp);
14612 move_it_vertically (&it, this_scroll_margin);
14613 scroll_margin_pos = it.current.pos;
14614 }
14615
14616 if (PT < CHARPOS (scroll_margin_pos))
14617 {
14618 /* Point is in the scroll margin at the top of the window or
14619 above what is displayed in the window. */
14620 int y0, y_to_move;
14621
14622 /* Compute the vertical distance from PT to the scroll
14623 margin position. Move as far as scroll_max allows, or
14624 one screenful, or 10 screen lines, whichever is largest.
14625 Give up if distance is greater than scroll_max. */
14626 SET_TEXT_POS (pos, PT, PT_BYTE);
14627 start_display (&it, w, pos);
14628 y0 = it.current_y;
14629 y_to_move = max (it.last_visible_y,
14630 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14631 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14632 y_to_move, -1,
14633 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14634 dy = it.current_y - y0;
14635 if (dy > scroll_max)
14636 return SCROLLING_FAILED;
14637
14638 /* Compute new window start. */
14639 start_display (&it, w, startp);
14640
14641 if (arg_scroll_conservatively)
14642 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14643 max (scroll_step, temp_scroll_step));
14644 else if (scroll_step || temp_scroll_step)
14645 amount_to_scroll = scroll_max;
14646 else
14647 {
14648 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14649 height = WINDOW_BOX_TEXT_HEIGHT (w);
14650 if (NUMBERP (aggressive))
14651 {
14652 double float_amount = XFLOATINT (aggressive) * height;
14653 amount_to_scroll = float_amount;
14654 if (amount_to_scroll == 0 && float_amount > 0)
14655 amount_to_scroll = 1;
14656 amount_to_scroll -=
14657 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14658 /* Don't let point enter the scroll margin near
14659 bottom of the window. */
14660 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14661 amount_to_scroll = height - 2*this_scroll_margin + dy;
14662 }
14663 }
14664
14665 if (amount_to_scroll <= 0)
14666 return SCROLLING_FAILED;
14667
14668 move_it_vertically_backward (&it, amount_to_scroll);
14669 startp = it.current.pos;
14670 }
14671 }
14672
14673 /* Run window scroll functions. */
14674 startp = run_window_scroll_functions (window, startp);
14675
14676 /* Display the window. Give up if new fonts are loaded, or if point
14677 doesn't appear. */
14678 if (!try_window (window, startp, 0))
14679 rc = SCROLLING_NEED_LARGER_MATRICES;
14680 else if (w->cursor.vpos < 0)
14681 {
14682 clear_glyph_matrix (w->desired_matrix);
14683 rc = SCROLLING_FAILED;
14684 }
14685 else
14686 {
14687 /* Maybe forget recorded base line for line number display. */
14688 if (!just_this_one_p
14689 || current_buffer->clip_changed
14690 || BEG_UNCHANGED < CHARPOS (startp))
14691 w->base_line_number = Qnil;
14692
14693 /* If cursor ends up on a partially visible line,
14694 treat that as being off the bottom of the screen. */
14695 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14696 /* It's possible that the cursor is on the first line of the
14697 buffer, which is partially obscured due to a vscroll
14698 (Bug#7537). In that case, avoid looping forever . */
14699 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14700 {
14701 clear_glyph_matrix (w->desired_matrix);
14702 ++extra_scroll_margin_lines;
14703 goto too_near_end;
14704 }
14705 rc = SCROLLING_SUCCESS;
14706 }
14707
14708 return rc;
14709 }
14710
14711
14712 /* Compute a suitable window start for window W if display of W starts
14713 on a continuation line. Value is non-zero if a new window start
14714 was computed.
14715
14716 The new window start will be computed, based on W's width, starting
14717 from the start of the continued line. It is the start of the
14718 screen line with the minimum distance from the old start W->start. */
14719
14720 static int
14721 compute_window_start_on_continuation_line (struct window *w)
14722 {
14723 struct text_pos pos, start_pos;
14724 int window_start_changed_p = 0;
14725
14726 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14727
14728 /* If window start is on a continuation line... Window start may be
14729 < BEGV in case there's invisible text at the start of the
14730 buffer (M-x rmail, for example). */
14731 if (CHARPOS (start_pos) > BEGV
14732 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14733 {
14734 struct it it;
14735 struct glyph_row *row;
14736
14737 /* Handle the case that the window start is out of range. */
14738 if (CHARPOS (start_pos) < BEGV)
14739 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14740 else if (CHARPOS (start_pos) > ZV)
14741 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14742
14743 /* Find the start of the continued line. This should be fast
14744 because scan_buffer is fast (newline cache). */
14745 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14746 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14747 row, DEFAULT_FACE_ID);
14748 reseat_at_previous_visible_line_start (&it);
14749
14750 /* If the line start is "too far" away from the window start,
14751 say it takes too much time to compute a new window start. */
14752 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14753 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14754 {
14755 int min_distance, distance;
14756
14757 /* Move forward by display lines to find the new window
14758 start. If window width was enlarged, the new start can
14759 be expected to be > the old start. If window width was
14760 decreased, the new window start will be < the old start.
14761 So, we're looking for the display line start with the
14762 minimum distance from the old window start. */
14763 pos = it.current.pos;
14764 min_distance = INFINITY;
14765 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14766 distance < min_distance)
14767 {
14768 min_distance = distance;
14769 pos = it.current.pos;
14770 move_it_by_lines (&it, 1);
14771 }
14772
14773 /* Set the window start there. */
14774 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14775 window_start_changed_p = 1;
14776 }
14777 }
14778
14779 return window_start_changed_p;
14780 }
14781
14782
14783 /* Try cursor movement in case text has not changed in window WINDOW,
14784 with window start STARTP. Value is
14785
14786 CURSOR_MOVEMENT_SUCCESS if successful
14787
14788 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14789
14790 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14791 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14792 we want to scroll as if scroll-step were set to 1. See the code.
14793
14794 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14795 which case we have to abort this redisplay, and adjust matrices
14796 first. */
14797
14798 enum
14799 {
14800 CURSOR_MOVEMENT_SUCCESS,
14801 CURSOR_MOVEMENT_CANNOT_BE_USED,
14802 CURSOR_MOVEMENT_MUST_SCROLL,
14803 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14804 };
14805
14806 static int
14807 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14808 {
14809 struct window *w = XWINDOW (window);
14810 struct frame *f = XFRAME (w->frame);
14811 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14812
14813 #if GLYPH_DEBUG
14814 if (inhibit_try_cursor_movement)
14815 return rc;
14816 #endif
14817
14818 /* Handle case where text has not changed, only point, and it has
14819 not moved off the frame. */
14820 if (/* Point may be in this window. */
14821 PT >= CHARPOS (startp)
14822 /* Selective display hasn't changed. */
14823 && !current_buffer->clip_changed
14824 /* Function force-mode-line-update is used to force a thorough
14825 redisplay. It sets either windows_or_buffers_changed or
14826 update_mode_lines. So don't take a shortcut here for these
14827 cases. */
14828 && !update_mode_lines
14829 && !windows_or_buffers_changed
14830 && !cursor_type_changed
14831 /* Can't use this case if highlighting a region. When a
14832 region exists, cursor movement has to do more than just
14833 set the cursor. */
14834 && !(!NILP (Vtransient_mark_mode)
14835 && !NILP (BVAR (current_buffer, mark_active)))
14836 && NILP (w->region_showing)
14837 && NILP (Vshow_trailing_whitespace)
14838 /* Right after splitting windows, last_point may be nil. */
14839 && INTEGERP (w->last_point)
14840 /* This code is not used for mini-buffer for the sake of the case
14841 of redisplaying to replace an echo area message; since in
14842 that case the mini-buffer contents per se are usually
14843 unchanged. This code is of no real use in the mini-buffer
14844 since the handling of this_line_start_pos, etc., in redisplay
14845 handles the same cases. */
14846 && !EQ (window, minibuf_window)
14847 /* When splitting windows or for new windows, it happens that
14848 redisplay is called with a nil window_end_vpos or one being
14849 larger than the window. This should really be fixed in
14850 window.c. I don't have this on my list, now, so we do
14851 approximately the same as the old redisplay code. --gerd. */
14852 && INTEGERP (w->window_end_vpos)
14853 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14854 && (FRAME_WINDOW_P (f)
14855 || !overlay_arrow_in_current_buffer_p ()))
14856 {
14857 int this_scroll_margin, top_scroll_margin;
14858 struct glyph_row *row = NULL;
14859
14860 #if GLYPH_DEBUG
14861 debug_method_add (w, "cursor movement");
14862 #endif
14863
14864 /* Scroll if point within this distance from the top or bottom
14865 of the window. This is a pixel value. */
14866 if (scroll_margin > 0)
14867 {
14868 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14869 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14870 }
14871 else
14872 this_scroll_margin = 0;
14873
14874 top_scroll_margin = this_scroll_margin;
14875 if (WINDOW_WANTS_HEADER_LINE_P (w))
14876 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14877
14878 /* Start with the row the cursor was displayed during the last
14879 not paused redisplay. Give up if that row is not valid. */
14880 if (w->last_cursor.vpos < 0
14881 || w->last_cursor.vpos >= w->current_matrix->nrows)
14882 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14883 else
14884 {
14885 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14886 if (row->mode_line_p)
14887 ++row;
14888 if (!row->enabled_p)
14889 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14890 }
14891
14892 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14893 {
14894 int scroll_p = 0, must_scroll = 0;
14895 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14896
14897 if (PT > XFASTINT (w->last_point))
14898 {
14899 /* Point has moved forward. */
14900 while (MATRIX_ROW_END_CHARPOS (row) < PT
14901 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14902 {
14903 xassert (row->enabled_p);
14904 ++row;
14905 }
14906
14907 /* If the end position of a row equals the start
14908 position of the next row, and PT is at that position,
14909 we would rather display cursor in the next line. */
14910 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14911 && MATRIX_ROW_END_CHARPOS (row) == PT
14912 && row < w->current_matrix->rows
14913 + w->current_matrix->nrows - 1
14914 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14915 && !cursor_row_p (row))
14916 ++row;
14917
14918 /* If within the scroll margin, scroll. Note that
14919 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14920 the next line would be drawn, and that
14921 this_scroll_margin can be zero. */
14922 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14923 || PT > MATRIX_ROW_END_CHARPOS (row)
14924 /* Line is completely visible last line in window
14925 and PT is to be set in the next line. */
14926 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14927 && PT == MATRIX_ROW_END_CHARPOS (row)
14928 && !row->ends_at_zv_p
14929 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14930 scroll_p = 1;
14931 }
14932 else if (PT < XFASTINT (w->last_point))
14933 {
14934 /* Cursor has to be moved backward. Note that PT >=
14935 CHARPOS (startp) because of the outer if-statement. */
14936 while (!row->mode_line_p
14937 && (MATRIX_ROW_START_CHARPOS (row) > PT
14938 || (MATRIX_ROW_START_CHARPOS (row) == PT
14939 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14940 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14941 row > w->current_matrix->rows
14942 && (row-1)->ends_in_newline_from_string_p))))
14943 && (row->y > top_scroll_margin
14944 || CHARPOS (startp) == BEGV))
14945 {
14946 xassert (row->enabled_p);
14947 --row;
14948 }
14949
14950 /* Consider the following case: Window starts at BEGV,
14951 there is invisible, intangible text at BEGV, so that
14952 display starts at some point START > BEGV. It can
14953 happen that we are called with PT somewhere between
14954 BEGV and START. Try to handle that case. */
14955 if (row < w->current_matrix->rows
14956 || row->mode_line_p)
14957 {
14958 row = w->current_matrix->rows;
14959 if (row->mode_line_p)
14960 ++row;
14961 }
14962
14963 /* Due to newlines in overlay strings, we may have to
14964 skip forward over overlay strings. */
14965 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14966 && MATRIX_ROW_END_CHARPOS (row) == PT
14967 && !cursor_row_p (row))
14968 ++row;
14969
14970 /* If within the scroll margin, scroll. */
14971 if (row->y < top_scroll_margin
14972 && CHARPOS (startp) != BEGV)
14973 scroll_p = 1;
14974 }
14975 else
14976 {
14977 /* Cursor did not move. So don't scroll even if cursor line
14978 is partially visible, as it was so before. */
14979 rc = CURSOR_MOVEMENT_SUCCESS;
14980 }
14981
14982 if (PT < MATRIX_ROW_START_CHARPOS (row)
14983 || PT > MATRIX_ROW_END_CHARPOS (row))
14984 {
14985 /* if PT is not in the glyph row, give up. */
14986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14987 must_scroll = 1;
14988 }
14989 else if (rc != CURSOR_MOVEMENT_SUCCESS
14990 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14991 {
14992 struct glyph_row *row1;
14993
14994 /* If rows are bidi-reordered and point moved, back up
14995 until we find a row that does not belong to a
14996 continuation line. This is because we must consider
14997 all rows of a continued line as candidates for the
14998 new cursor positioning, since row start and end
14999 positions change non-linearly with vertical position
15000 in such rows. */
15001 /* FIXME: Revisit this when glyph ``spilling'' in
15002 continuation lines' rows is implemented for
15003 bidi-reordered rows. */
15004 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15005 MATRIX_ROW_CONTINUATION_LINE_P (row);
15006 --row)
15007 {
15008 /* If we hit the beginning of the displayed portion
15009 without finding the first row of a continued
15010 line, give up. */
15011 if (row <= row1)
15012 {
15013 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15014 break;
15015 }
15016 xassert (row->enabled_p);
15017 }
15018 }
15019 if (must_scroll)
15020 ;
15021 else if (rc != CURSOR_MOVEMENT_SUCCESS
15022 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15023 /* Make sure this isn't a header line by any chance, since
15024 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15025 && !row->mode_line_p
15026 && make_cursor_line_fully_visible_p)
15027 {
15028 if (PT == MATRIX_ROW_END_CHARPOS (row)
15029 && !row->ends_at_zv_p
15030 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15031 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15032 else if (row->height > window_box_height (w))
15033 {
15034 /* If we end up in a partially visible line, let's
15035 make it fully visible, except when it's taller
15036 than the window, in which case we can't do much
15037 about it. */
15038 *scroll_step = 1;
15039 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15040 }
15041 else
15042 {
15043 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15044 if (!cursor_row_fully_visible_p (w, 0, 1))
15045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15046 else
15047 rc = CURSOR_MOVEMENT_SUCCESS;
15048 }
15049 }
15050 else if (scroll_p)
15051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15052 else if (rc != CURSOR_MOVEMENT_SUCCESS
15053 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15054 {
15055 /* With bidi-reordered rows, there could be more than
15056 one candidate row whose start and end positions
15057 occlude point. We need to let set_cursor_from_row
15058 find the best candidate. */
15059 /* FIXME: Revisit this when glyph ``spilling'' in
15060 continuation lines' rows is implemented for
15061 bidi-reordered rows. */
15062 int rv = 0;
15063
15064 do
15065 {
15066 int at_zv_p = 0, exact_match_p = 0;
15067
15068 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15069 && PT <= MATRIX_ROW_END_CHARPOS (row)
15070 && cursor_row_p (row))
15071 rv |= set_cursor_from_row (w, row, w->current_matrix,
15072 0, 0, 0, 0);
15073 /* As soon as we've found the exact match for point,
15074 or the first suitable row whose ends_at_zv_p flag
15075 is set, we are done. */
15076 at_zv_p =
15077 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15078 if (rv && !at_zv_p
15079 && w->cursor.hpos >= 0
15080 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15081 w->cursor.vpos))
15082 {
15083 struct glyph_row *candidate =
15084 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15085 struct glyph *g =
15086 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15087 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15088
15089 exact_match_p =
15090 (BUFFERP (g->object) && g->charpos == PT)
15091 || (INTEGERP (g->object)
15092 && (g->charpos == PT
15093 || (g->charpos == 0 && endpos - 1 == PT)));
15094 }
15095 if (rv && (at_zv_p || exact_match_p))
15096 {
15097 rc = CURSOR_MOVEMENT_SUCCESS;
15098 break;
15099 }
15100 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15101 break;
15102 ++row;
15103 }
15104 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15105 || row->continued_p)
15106 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15107 || (MATRIX_ROW_START_CHARPOS (row) == PT
15108 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15109 /* If we didn't find any candidate rows, or exited the
15110 loop before all the candidates were examined, signal
15111 to the caller that this method failed. */
15112 if (rc != CURSOR_MOVEMENT_SUCCESS
15113 && !(rv
15114 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15115 && !row->continued_p))
15116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15117 else if (rv)
15118 rc = CURSOR_MOVEMENT_SUCCESS;
15119 }
15120 else
15121 {
15122 do
15123 {
15124 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15125 {
15126 rc = CURSOR_MOVEMENT_SUCCESS;
15127 break;
15128 }
15129 ++row;
15130 }
15131 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15132 && MATRIX_ROW_START_CHARPOS (row) == PT
15133 && cursor_row_p (row));
15134 }
15135 }
15136 }
15137
15138 return rc;
15139 }
15140
15141 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15142 static
15143 #endif
15144 void
15145 set_vertical_scroll_bar (struct window *w)
15146 {
15147 EMACS_INT start, end, whole;
15148
15149 /* Calculate the start and end positions for the current window.
15150 At some point, it would be nice to choose between scrollbars
15151 which reflect the whole buffer size, with special markers
15152 indicating narrowing, and scrollbars which reflect only the
15153 visible region.
15154
15155 Note that mini-buffers sometimes aren't displaying any text. */
15156 if (!MINI_WINDOW_P (w)
15157 || (w == XWINDOW (minibuf_window)
15158 && NILP (echo_area_buffer[0])))
15159 {
15160 struct buffer *buf = XBUFFER (w->buffer);
15161 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15162 start = marker_position (w->start) - BUF_BEGV (buf);
15163 /* I don't think this is guaranteed to be right. For the
15164 moment, we'll pretend it is. */
15165 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15166
15167 if (end < start)
15168 end = start;
15169 if (whole < (end - start))
15170 whole = end - start;
15171 }
15172 else
15173 start = end = whole = 0;
15174
15175 /* Indicate what this scroll bar ought to be displaying now. */
15176 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15177 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15178 (w, end - start, whole, start);
15179 }
15180
15181
15182 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15183 selected_window is redisplayed.
15184
15185 We can return without actually redisplaying the window if
15186 fonts_changed_p is nonzero. In that case, redisplay_internal will
15187 retry. */
15188
15189 static void
15190 redisplay_window (Lisp_Object window, int just_this_one_p)
15191 {
15192 struct window *w = XWINDOW (window);
15193 struct frame *f = XFRAME (w->frame);
15194 struct buffer *buffer = XBUFFER (w->buffer);
15195 struct buffer *old = current_buffer;
15196 struct text_pos lpoint, opoint, startp;
15197 int update_mode_line;
15198 int tem;
15199 struct it it;
15200 /* Record it now because it's overwritten. */
15201 int current_matrix_up_to_date_p = 0;
15202 int used_current_matrix_p = 0;
15203 /* This is less strict than current_matrix_up_to_date_p.
15204 It indicates that the buffer contents and narrowing are unchanged. */
15205 int buffer_unchanged_p = 0;
15206 int temp_scroll_step = 0;
15207 int count = SPECPDL_INDEX ();
15208 int rc;
15209 int centering_position = -1;
15210 int last_line_misfit = 0;
15211 EMACS_INT beg_unchanged, end_unchanged;
15212
15213 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15214 opoint = lpoint;
15215
15216 /* W must be a leaf window here. */
15217 xassert (!NILP (w->buffer));
15218 #if GLYPH_DEBUG
15219 *w->desired_matrix->method = 0;
15220 #endif
15221
15222 restart:
15223 reconsider_clip_changes (w, buffer);
15224
15225 /* Has the mode line to be updated? */
15226 update_mode_line = (!NILP (w->update_mode_line)
15227 || update_mode_lines
15228 || buffer->clip_changed
15229 || buffer->prevent_redisplay_optimizations_p);
15230
15231 if (MINI_WINDOW_P (w))
15232 {
15233 if (w == XWINDOW (echo_area_window)
15234 && !NILP (echo_area_buffer[0]))
15235 {
15236 if (update_mode_line)
15237 /* We may have to update a tty frame's menu bar or a
15238 tool-bar. Example `M-x C-h C-h C-g'. */
15239 goto finish_menu_bars;
15240 else
15241 /* We've already displayed the echo area glyphs in this window. */
15242 goto finish_scroll_bars;
15243 }
15244 else if ((w != XWINDOW (minibuf_window)
15245 || minibuf_level == 0)
15246 /* When buffer is nonempty, redisplay window normally. */
15247 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15248 /* Quail displays non-mini buffers in minibuffer window.
15249 In that case, redisplay the window normally. */
15250 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15251 {
15252 /* W is a mini-buffer window, but it's not active, so clear
15253 it. */
15254 int yb = window_text_bottom_y (w);
15255 struct glyph_row *row;
15256 int y;
15257
15258 for (y = 0, row = w->desired_matrix->rows;
15259 y < yb;
15260 y += row->height, ++row)
15261 blank_row (w, row, y);
15262 goto finish_scroll_bars;
15263 }
15264
15265 clear_glyph_matrix (w->desired_matrix);
15266 }
15267
15268 /* Otherwise set up data on this window; select its buffer and point
15269 value. */
15270 /* Really select the buffer, for the sake of buffer-local
15271 variables. */
15272 set_buffer_internal_1 (XBUFFER (w->buffer));
15273
15274 current_matrix_up_to_date_p
15275 = (!NILP (w->window_end_valid)
15276 && !current_buffer->clip_changed
15277 && !current_buffer->prevent_redisplay_optimizations_p
15278 && XFASTINT (w->last_modified) >= MODIFF
15279 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15280
15281 /* Run the window-bottom-change-functions
15282 if it is possible that the text on the screen has changed
15283 (either due to modification of the text, or any other reason). */
15284 if (!current_matrix_up_to_date_p
15285 && !NILP (Vwindow_text_change_functions))
15286 {
15287 safe_run_hooks (Qwindow_text_change_functions);
15288 goto restart;
15289 }
15290
15291 beg_unchanged = BEG_UNCHANGED;
15292 end_unchanged = END_UNCHANGED;
15293
15294 SET_TEXT_POS (opoint, PT, PT_BYTE);
15295
15296 specbind (Qinhibit_point_motion_hooks, Qt);
15297
15298 buffer_unchanged_p
15299 = (!NILP (w->window_end_valid)
15300 && !current_buffer->clip_changed
15301 && XFASTINT (w->last_modified) >= MODIFF
15302 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15303
15304 /* When windows_or_buffers_changed is non-zero, we can't rely on
15305 the window end being valid, so set it to nil there. */
15306 if (windows_or_buffers_changed)
15307 {
15308 /* If window starts on a continuation line, maybe adjust the
15309 window start in case the window's width changed. */
15310 if (XMARKER (w->start)->buffer == current_buffer)
15311 compute_window_start_on_continuation_line (w);
15312
15313 w->window_end_valid = Qnil;
15314 }
15315
15316 /* Some sanity checks. */
15317 CHECK_WINDOW_END (w);
15318 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15319 abort ();
15320 if (BYTEPOS (opoint) < CHARPOS (opoint))
15321 abort ();
15322
15323 /* If %c is in mode line, update it if needed. */
15324 if (!NILP (w->column_number_displayed)
15325 /* This alternative quickly identifies a common case
15326 where no change is needed. */
15327 && !(PT == XFASTINT (w->last_point)
15328 && XFASTINT (w->last_modified) >= MODIFF
15329 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15330 && (XFASTINT (w->column_number_displayed) != current_column ()))
15331 update_mode_line = 1;
15332
15333 /* Count number of windows showing the selected buffer. An indirect
15334 buffer counts as its base buffer. */
15335 if (!just_this_one_p)
15336 {
15337 struct buffer *current_base, *window_base;
15338 current_base = current_buffer;
15339 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15340 if (current_base->base_buffer)
15341 current_base = current_base->base_buffer;
15342 if (window_base->base_buffer)
15343 window_base = window_base->base_buffer;
15344 if (current_base == window_base)
15345 buffer_shared++;
15346 }
15347
15348 /* Point refers normally to the selected window. For any other
15349 window, set up appropriate value. */
15350 if (!EQ (window, selected_window))
15351 {
15352 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15353 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15354 if (new_pt < BEGV)
15355 {
15356 new_pt = BEGV;
15357 new_pt_byte = BEGV_BYTE;
15358 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15359 }
15360 else if (new_pt > (ZV - 1))
15361 {
15362 new_pt = ZV;
15363 new_pt_byte = ZV_BYTE;
15364 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15365 }
15366
15367 /* We don't use SET_PT so that the point-motion hooks don't run. */
15368 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15369 }
15370
15371 /* If any of the character widths specified in the display table
15372 have changed, invalidate the width run cache. It's true that
15373 this may be a bit late to catch such changes, but the rest of
15374 redisplay goes (non-fatally) haywire when the display table is
15375 changed, so why should we worry about doing any better? */
15376 if (current_buffer->width_run_cache)
15377 {
15378 struct Lisp_Char_Table *disptab = buffer_display_table ();
15379
15380 if (! disptab_matches_widthtab (disptab,
15381 XVECTOR (BVAR (current_buffer, width_table))))
15382 {
15383 invalidate_region_cache (current_buffer,
15384 current_buffer->width_run_cache,
15385 BEG, Z);
15386 recompute_width_table (current_buffer, disptab);
15387 }
15388 }
15389
15390 /* If window-start is screwed up, choose a new one. */
15391 if (XMARKER (w->start)->buffer != current_buffer)
15392 goto recenter;
15393
15394 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15395
15396 /* If someone specified a new starting point but did not insist,
15397 check whether it can be used. */
15398 if (!NILP (w->optional_new_start)
15399 && CHARPOS (startp) >= BEGV
15400 && CHARPOS (startp) <= ZV)
15401 {
15402 w->optional_new_start = Qnil;
15403 start_display (&it, w, startp);
15404 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15405 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15406 if (IT_CHARPOS (it) == PT)
15407 w->force_start = Qt;
15408 /* IT may overshoot PT if text at PT is invisible. */
15409 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15410 w->force_start = Qt;
15411 }
15412
15413 force_start:
15414
15415 /* Handle case where place to start displaying has been specified,
15416 unless the specified location is outside the accessible range. */
15417 if (!NILP (w->force_start)
15418 || w->frozen_window_start_p)
15419 {
15420 /* We set this later on if we have to adjust point. */
15421 int new_vpos = -1;
15422
15423 w->force_start = Qnil;
15424 w->vscroll = 0;
15425 w->window_end_valid = Qnil;
15426
15427 /* Forget any recorded base line for line number display. */
15428 if (!buffer_unchanged_p)
15429 w->base_line_number = Qnil;
15430
15431 /* Redisplay the mode line. Select the buffer properly for that.
15432 Also, run the hook window-scroll-functions
15433 because we have scrolled. */
15434 /* Note, we do this after clearing force_start because
15435 if there's an error, it is better to forget about force_start
15436 than to get into an infinite loop calling the hook functions
15437 and having them get more errors. */
15438 if (!update_mode_line
15439 || ! NILP (Vwindow_scroll_functions))
15440 {
15441 update_mode_line = 1;
15442 w->update_mode_line = Qt;
15443 startp = run_window_scroll_functions (window, startp);
15444 }
15445
15446 w->last_modified = make_number (0);
15447 w->last_overlay_modified = make_number (0);
15448 if (CHARPOS (startp) < BEGV)
15449 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15450 else if (CHARPOS (startp) > ZV)
15451 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15452
15453 /* Redisplay, then check if cursor has been set during the
15454 redisplay. Give up if new fonts were loaded. */
15455 /* We used to issue a CHECK_MARGINS argument to try_window here,
15456 but this causes scrolling to fail when point begins inside
15457 the scroll margin (bug#148) -- cyd */
15458 if (!try_window (window, startp, 0))
15459 {
15460 w->force_start = Qt;
15461 clear_glyph_matrix (w->desired_matrix);
15462 goto need_larger_matrices;
15463 }
15464
15465 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15466 {
15467 /* If point does not appear, try to move point so it does
15468 appear. The desired matrix has been built above, so we
15469 can use it here. */
15470 new_vpos = window_box_height (w) / 2;
15471 }
15472
15473 if (!cursor_row_fully_visible_p (w, 0, 0))
15474 {
15475 /* Point does appear, but on a line partly visible at end of window.
15476 Move it back to a fully-visible line. */
15477 new_vpos = window_box_height (w);
15478 }
15479
15480 /* If we need to move point for either of the above reasons,
15481 now actually do it. */
15482 if (new_vpos >= 0)
15483 {
15484 struct glyph_row *row;
15485
15486 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15487 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15488 ++row;
15489
15490 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15491 MATRIX_ROW_START_BYTEPOS (row));
15492
15493 if (w != XWINDOW (selected_window))
15494 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15495 else if (current_buffer == old)
15496 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15497
15498 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15499
15500 /* If we are highlighting the region, then we just changed
15501 the region, so redisplay to show it. */
15502 if (!NILP (Vtransient_mark_mode)
15503 && !NILP (BVAR (current_buffer, mark_active)))
15504 {
15505 clear_glyph_matrix (w->desired_matrix);
15506 if (!try_window (window, startp, 0))
15507 goto need_larger_matrices;
15508 }
15509 }
15510
15511 #if GLYPH_DEBUG
15512 debug_method_add (w, "forced window start");
15513 #endif
15514 goto done;
15515 }
15516
15517 /* Handle case where text has not changed, only point, and it has
15518 not moved off the frame, and we are not retrying after hscroll.
15519 (current_matrix_up_to_date_p is nonzero when retrying.) */
15520 if (current_matrix_up_to_date_p
15521 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15522 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15523 {
15524 switch (rc)
15525 {
15526 case CURSOR_MOVEMENT_SUCCESS:
15527 used_current_matrix_p = 1;
15528 goto done;
15529
15530 case CURSOR_MOVEMENT_MUST_SCROLL:
15531 goto try_to_scroll;
15532
15533 default:
15534 abort ();
15535 }
15536 }
15537 /* If current starting point was originally the beginning of a line
15538 but no longer is, find a new starting point. */
15539 else if (!NILP (w->start_at_line_beg)
15540 && !(CHARPOS (startp) <= BEGV
15541 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15542 {
15543 #if GLYPH_DEBUG
15544 debug_method_add (w, "recenter 1");
15545 #endif
15546 goto recenter;
15547 }
15548
15549 /* Try scrolling with try_window_id. Value is > 0 if update has
15550 been done, it is -1 if we know that the same window start will
15551 not work. It is 0 if unsuccessful for some other reason. */
15552 else if ((tem = try_window_id (w)) != 0)
15553 {
15554 #if GLYPH_DEBUG
15555 debug_method_add (w, "try_window_id %d", tem);
15556 #endif
15557
15558 if (fonts_changed_p)
15559 goto need_larger_matrices;
15560 if (tem > 0)
15561 goto done;
15562
15563 /* Otherwise try_window_id has returned -1 which means that we
15564 don't want the alternative below this comment to execute. */
15565 }
15566 else if (CHARPOS (startp) >= BEGV
15567 && CHARPOS (startp) <= ZV
15568 && PT >= CHARPOS (startp)
15569 && (CHARPOS (startp) < ZV
15570 /* Avoid starting at end of buffer. */
15571 || CHARPOS (startp) == BEGV
15572 || (XFASTINT (w->last_modified) >= MODIFF
15573 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15574 {
15575 int d1, d2, d3, d4, d5, d6;
15576
15577 /* If first window line is a continuation line, and window start
15578 is inside the modified region, but the first change is before
15579 current window start, we must select a new window start.
15580
15581 However, if this is the result of a down-mouse event (e.g. by
15582 extending the mouse-drag-overlay), we don't want to select a
15583 new window start, since that would change the position under
15584 the mouse, resulting in an unwanted mouse-movement rather
15585 than a simple mouse-click. */
15586 if (NILP (w->start_at_line_beg)
15587 && NILP (do_mouse_tracking)
15588 && CHARPOS (startp) > BEGV
15589 && CHARPOS (startp) > BEG + beg_unchanged
15590 && CHARPOS (startp) <= Z - end_unchanged
15591 /* Even if w->start_at_line_beg is nil, a new window may
15592 start at a line_beg, since that's how set_buffer_window
15593 sets it. So, we need to check the return value of
15594 compute_window_start_on_continuation_line. (See also
15595 bug#197). */
15596 && XMARKER (w->start)->buffer == current_buffer
15597 && compute_window_start_on_continuation_line (w)
15598 /* It doesn't make sense to force the window start like we
15599 do at label force_start if it is already known that point
15600 will not be visible in the resulting window, because
15601 doing so will move point from its correct position
15602 instead of scrolling the window to bring point into view.
15603 See bug#9324. */
15604 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15605 {
15606 w->force_start = Qt;
15607 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15608 goto force_start;
15609 }
15610
15611 #if GLYPH_DEBUG
15612 debug_method_add (w, "same window start");
15613 #endif
15614
15615 /* Try to redisplay starting at same place as before.
15616 If point has not moved off frame, accept the results. */
15617 if (!current_matrix_up_to_date_p
15618 /* Don't use try_window_reusing_current_matrix in this case
15619 because a window scroll function can have changed the
15620 buffer. */
15621 || !NILP (Vwindow_scroll_functions)
15622 || MINI_WINDOW_P (w)
15623 || !(used_current_matrix_p
15624 = try_window_reusing_current_matrix (w)))
15625 {
15626 IF_DEBUG (debug_method_add (w, "1"));
15627 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15628 /* -1 means we need to scroll.
15629 0 means we need new matrices, but fonts_changed_p
15630 is set in that case, so we will detect it below. */
15631 goto try_to_scroll;
15632 }
15633
15634 if (fonts_changed_p)
15635 goto need_larger_matrices;
15636
15637 if (w->cursor.vpos >= 0)
15638 {
15639 if (!just_this_one_p
15640 || current_buffer->clip_changed
15641 || BEG_UNCHANGED < CHARPOS (startp))
15642 /* Forget any recorded base line for line number display. */
15643 w->base_line_number = Qnil;
15644
15645 if (!cursor_row_fully_visible_p (w, 1, 0))
15646 {
15647 clear_glyph_matrix (w->desired_matrix);
15648 last_line_misfit = 1;
15649 }
15650 /* Drop through and scroll. */
15651 else
15652 goto done;
15653 }
15654 else
15655 clear_glyph_matrix (w->desired_matrix);
15656 }
15657
15658 try_to_scroll:
15659
15660 w->last_modified = make_number (0);
15661 w->last_overlay_modified = make_number (0);
15662
15663 /* Redisplay the mode line. Select the buffer properly for that. */
15664 if (!update_mode_line)
15665 {
15666 update_mode_line = 1;
15667 w->update_mode_line = Qt;
15668 }
15669
15670 /* Try to scroll by specified few lines. */
15671 if ((scroll_conservatively
15672 || emacs_scroll_step
15673 || temp_scroll_step
15674 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15675 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15676 && CHARPOS (startp) >= BEGV
15677 && CHARPOS (startp) <= ZV)
15678 {
15679 /* The function returns -1 if new fonts were loaded, 1 if
15680 successful, 0 if not successful. */
15681 int ss = try_scrolling (window, just_this_one_p,
15682 scroll_conservatively,
15683 emacs_scroll_step,
15684 temp_scroll_step, last_line_misfit);
15685 switch (ss)
15686 {
15687 case SCROLLING_SUCCESS:
15688 goto done;
15689
15690 case SCROLLING_NEED_LARGER_MATRICES:
15691 goto need_larger_matrices;
15692
15693 case SCROLLING_FAILED:
15694 break;
15695
15696 default:
15697 abort ();
15698 }
15699 }
15700
15701 /* Finally, just choose a place to start which positions point
15702 according to user preferences. */
15703
15704 recenter:
15705
15706 #if GLYPH_DEBUG
15707 debug_method_add (w, "recenter");
15708 #endif
15709
15710 /* w->vscroll = 0; */
15711
15712 /* Forget any previously recorded base line for line number display. */
15713 if (!buffer_unchanged_p)
15714 w->base_line_number = Qnil;
15715
15716 /* Determine the window start relative to point. */
15717 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15718 it.current_y = it.last_visible_y;
15719 if (centering_position < 0)
15720 {
15721 int margin =
15722 scroll_margin > 0
15723 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15724 : 0;
15725 EMACS_INT margin_pos = CHARPOS (startp);
15726 Lisp_Object aggressive;
15727 int scrolling_up;
15728
15729 /* If there is a scroll margin at the top of the window, find
15730 its character position. */
15731 if (margin
15732 /* Cannot call start_display if startp is not in the
15733 accessible region of the buffer. This can happen when we
15734 have just switched to a different buffer and/or changed
15735 its restriction. In that case, startp is initialized to
15736 the character position 1 (BEGV) because we did not yet
15737 have chance to display the buffer even once. */
15738 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15739 {
15740 struct it it1;
15741 void *it1data = NULL;
15742
15743 SAVE_IT (it1, it, it1data);
15744 start_display (&it1, w, startp);
15745 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15746 margin_pos = IT_CHARPOS (it1);
15747 RESTORE_IT (&it, &it, it1data);
15748 }
15749 scrolling_up = PT > margin_pos;
15750 aggressive =
15751 scrolling_up
15752 ? BVAR (current_buffer, scroll_up_aggressively)
15753 : BVAR (current_buffer, scroll_down_aggressively);
15754
15755 if (!MINI_WINDOW_P (w)
15756 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15757 {
15758 int pt_offset = 0;
15759
15760 /* Setting scroll-conservatively overrides
15761 scroll-*-aggressively. */
15762 if (!scroll_conservatively && NUMBERP (aggressive))
15763 {
15764 double float_amount = XFLOATINT (aggressive);
15765
15766 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15767 if (pt_offset == 0 && float_amount > 0)
15768 pt_offset = 1;
15769 if (pt_offset && margin > 0)
15770 margin -= 1;
15771 }
15772 /* Compute how much to move the window start backward from
15773 point so that point will be displayed where the user
15774 wants it. */
15775 if (scrolling_up)
15776 {
15777 centering_position = it.last_visible_y;
15778 if (pt_offset)
15779 centering_position -= pt_offset;
15780 centering_position -=
15781 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15782 + WINDOW_HEADER_LINE_HEIGHT (w);
15783 /* Don't let point enter the scroll margin near top of
15784 the window. */
15785 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15786 centering_position = margin * FRAME_LINE_HEIGHT (f);
15787 }
15788 else
15789 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15790 }
15791 else
15792 /* Set the window start half the height of the window backward
15793 from point. */
15794 centering_position = window_box_height (w) / 2;
15795 }
15796 move_it_vertically_backward (&it, centering_position);
15797
15798 xassert (IT_CHARPOS (it) >= BEGV);
15799
15800 /* The function move_it_vertically_backward may move over more
15801 than the specified y-distance. If it->w is small, e.g. a
15802 mini-buffer window, we may end up in front of the window's
15803 display area. Start displaying at the start of the line
15804 containing PT in this case. */
15805 if (it.current_y <= 0)
15806 {
15807 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15808 move_it_vertically_backward (&it, 0);
15809 it.current_y = 0;
15810 }
15811
15812 it.current_x = it.hpos = 0;
15813
15814 /* Set the window start position here explicitly, to avoid an
15815 infinite loop in case the functions in window-scroll-functions
15816 get errors. */
15817 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15818
15819 /* Run scroll hooks. */
15820 startp = run_window_scroll_functions (window, it.current.pos);
15821
15822 /* Redisplay the window. */
15823 if (!current_matrix_up_to_date_p
15824 || windows_or_buffers_changed
15825 || cursor_type_changed
15826 /* Don't use try_window_reusing_current_matrix in this case
15827 because it can have changed the buffer. */
15828 || !NILP (Vwindow_scroll_functions)
15829 || !just_this_one_p
15830 || MINI_WINDOW_P (w)
15831 || !(used_current_matrix_p
15832 = try_window_reusing_current_matrix (w)))
15833 try_window (window, startp, 0);
15834
15835 /* If new fonts have been loaded (due to fontsets), give up. We
15836 have to start a new redisplay since we need to re-adjust glyph
15837 matrices. */
15838 if (fonts_changed_p)
15839 goto need_larger_matrices;
15840
15841 /* If cursor did not appear assume that the middle of the window is
15842 in the first line of the window. Do it again with the next line.
15843 (Imagine a window of height 100, displaying two lines of height
15844 60. Moving back 50 from it->last_visible_y will end in the first
15845 line.) */
15846 if (w->cursor.vpos < 0)
15847 {
15848 if (!NILP (w->window_end_valid)
15849 && PT >= Z - XFASTINT (w->window_end_pos))
15850 {
15851 clear_glyph_matrix (w->desired_matrix);
15852 move_it_by_lines (&it, 1);
15853 try_window (window, it.current.pos, 0);
15854 }
15855 else if (PT < IT_CHARPOS (it))
15856 {
15857 clear_glyph_matrix (w->desired_matrix);
15858 move_it_by_lines (&it, -1);
15859 try_window (window, it.current.pos, 0);
15860 }
15861 else
15862 {
15863 /* Not much we can do about it. */
15864 }
15865 }
15866
15867 /* Consider the following case: Window starts at BEGV, there is
15868 invisible, intangible text at BEGV, so that display starts at
15869 some point START > BEGV. It can happen that we are called with
15870 PT somewhere between BEGV and START. Try to handle that case. */
15871 if (w->cursor.vpos < 0)
15872 {
15873 struct glyph_row *row = w->current_matrix->rows;
15874 if (row->mode_line_p)
15875 ++row;
15876 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15877 }
15878
15879 if (!cursor_row_fully_visible_p (w, 0, 0))
15880 {
15881 /* If vscroll is enabled, disable it and try again. */
15882 if (w->vscroll)
15883 {
15884 w->vscroll = 0;
15885 clear_glyph_matrix (w->desired_matrix);
15886 goto recenter;
15887 }
15888
15889 /* Users who set scroll-conservatively to a large number want
15890 point just above/below the scroll margin. If we ended up
15891 with point's row partially visible, move the window start to
15892 make that row fully visible and out of the margin. */
15893 if (scroll_conservatively > SCROLL_LIMIT)
15894 {
15895 int margin =
15896 scroll_margin > 0
15897 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15898 : 0;
15899 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15900
15901 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15902 clear_glyph_matrix (w->desired_matrix);
15903 if (1 == try_window (window, it.current.pos,
15904 TRY_WINDOW_CHECK_MARGINS))
15905 goto done;
15906 }
15907
15908 /* If centering point failed to make the whole line visible,
15909 put point at the top instead. That has to make the whole line
15910 visible, if it can be done. */
15911 if (centering_position == 0)
15912 goto done;
15913
15914 clear_glyph_matrix (w->desired_matrix);
15915 centering_position = 0;
15916 goto recenter;
15917 }
15918
15919 done:
15920
15921 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15922 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15923 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15924 ? Qt : Qnil);
15925
15926 /* Display the mode line, if we must. */
15927 if ((update_mode_line
15928 /* If window not full width, must redo its mode line
15929 if (a) the window to its side is being redone and
15930 (b) we do a frame-based redisplay. This is a consequence
15931 of how inverted lines are drawn in frame-based redisplay. */
15932 || (!just_this_one_p
15933 && !FRAME_WINDOW_P (f)
15934 && !WINDOW_FULL_WIDTH_P (w))
15935 /* Line number to display. */
15936 || INTEGERP (w->base_line_pos)
15937 /* Column number is displayed and different from the one displayed. */
15938 || (!NILP (w->column_number_displayed)
15939 && (XFASTINT (w->column_number_displayed) != current_column ())))
15940 /* This means that the window has a mode line. */
15941 && (WINDOW_WANTS_MODELINE_P (w)
15942 || WINDOW_WANTS_HEADER_LINE_P (w)))
15943 {
15944 display_mode_lines (w);
15945
15946 /* If mode line height has changed, arrange for a thorough
15947 immediate redisplay using the correct mode line height. */
15948 if (WINDOW_WANTS_MODELINE_P (w)
15949 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15950 {
15951 fonts_changed_p = 1;
15952 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15953 = DESIRED_MODE_LINE_HEIGHT (w);
15954 }
15955
15956 /* If header line height has changed, arrange for a thorough
15957 immediate redisplay using the correct header line height. */
15958 if (WINDOW_WANTS_HEADER_LINE_P (w)
15959 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15960 {
15961 fonts_changed_p = 1;
15962 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15963 = DESIRED_HEADER_LINE_HEIGHT (w);
15964 }
15965
15966 if (fonts_changed_p)
15967 goto need_larger_matrices;
15968 }
15969
15970 if (!line_number_displayed
15971 && !BUFFERP (w->base_line_pos))
15972 {
15973 w->base_line_pos = Qnil;
15974 w->base_line_number = Qnil;
15975 }
15976
15977 finish_menu_bars:
15978
15979 /* When we reach a frame's selected window, redo the frame's menu bar. */
15980 if (update_mode_line
15981 && EQ (FRAME_SELECTED_WINDOW (f), window))
15982 {
15983 int redisplay_menu_p = 0;
15984
15985 if (FRAME_WINDOW_P (f))
15986 {
15987 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15988 || defined (HAVE_NS) || defined (USE_GTK)
15989 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15990 #else
15991 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15992 #endif
15993 }
15994 else
15995 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15996
15997 if (redisplay_menu_p)
15998 display_menu_bar (w);
15999
16000 #ifdef HAVE_WINDOW_SYSTEM
16001 if (FRAME_WINDOW_P (f))
16002 {
16003 #if defined (USE_GTK) || defined (HAVE_NS)
16004 if (FRAME_EXTERNAL_TOOL_BAR (f))
16005 redisplay_tool_bar (f);
16006 #else
16007 if (WINDOWP (f->tool_bar_window)
16008 && (FRAME_TOOL_BAR_LINES (f) > 0
16009 || !NILP (Vauto_resize_tool_bars))
16010 && redisplay_tool_bar (f))
16011 ignore_mouse_drag_p = 1;
16012 #endif
16013 }
16014 #endif
16015 }
16016
16017 #ifdef HAVE_WINDOW_SYSTEM
16018 if (FRAME_WINDOW_P (f)
16019 && update_window_fringes (w, (just_this_one_p
16020 || (!used_current_matrix_p && !overlay_arrow_seen)
16021 || w->pseudo_window_p)))
16022 {
16023 update_begin (f);
16024 BLOCK_INPUT;
16025 if (draw_window_fringes (w, 1))
16026 x_draw_vertical_border (w);
16027 UNBLOCK_INPUT;
16028 update_end (f);
16029 }
16030 #endif /* HAVE_WINDOW_SYSTEM */
16031
16032 /* We go to this label, with fonts_changed_p nonzero,
16033 if it is necessary to try again using larger glyph matrices.
16034 We have to redeem the scroll bar even in this case,
16035 because the loop in redisplay_internal expects that. */
16036 need_larger_matrices:
16037 ;
16038 finish_scroll_bars:
16039
16040 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16041 {
16042 /* Set the thumb's position and size. */
16043 set_vertical_scroll_bar (w);
16044
16045 /* Note that we actually used the scroll bar attached to this
16046 window, so it shouldn't be deleted at the end of redisplay. */
16047 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16048 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16049 }
16050
16051 /* Restore current_buffer and value of point in it. The window
16052 update may have changed the buffer, so first make sure `opoint'
16053 is still valid (Bug#6177). */
16054 if (CHARPOS (opoint) < BEGV)
16055 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16056 else if (CHARPOS (opoint) > ZV)
16057 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16058 else
16059 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16060
16061 set_buffer_internal_1 (old);
16062 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16063 shorter. This can be caused by log truncation in *Messages*. */
16064 if (CHARPOS (lpoint) <= ZV)
16065 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16066
16067 unbind_to (count, Qnil);
16068 }
16069
16070
16071 /* Build the complete desired matrix of WINDOW with a window start
16072 buffer position POS.
16073
16074 Value is 1 if successful. It is zero if fonts were loaded during
16075 redisplay which makes re-adjusting glyph matrices necessary, and -1
16076 if point would appear in the scroll margins.
16077 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16078 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16079 set in FLAGS.) */
16080
16081 int
16082 try_window (Lisp_Object window, struct text_pos pos, int flags)
16083 {
16084 struct window *w = XWINDOW (window);
16085 struct it it;
16086 struct glyph_row *last_text_row = NULL;
16087 struct frame *f = XFRAME (w->frame);
16088
16089 /* Make POS the new window start. */
16090 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16091
16092 /* Mark cursor position as unknown. No overlay arrow seen. */
16093 w->cursor.vpos = -1;
16094 overlay_arrow_seen = 0;
16095
16096 /* Initialize iterator and info to start at POS. */
16097 start_display (&it, w, pos);
16098
16099 /* Display all lines of W. */
16100 while (it.current_y < it.last_visible_y)
16101 {
16102 if (display_line (&it))
16103 last_text_row = it.glyph_row - 1;
16104 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16105 return 0;
16106 }
16107
16108 /* Don't let the cursor end in the scroll margins. */
16109 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16110 && !MINI_WINDOW_P (w))
16111 {
16112 int this_scroll_margin;
16113
16114 if (scroll_margin > 0)
16115 {
16116 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16117 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16118 }
16119 else
16120 this_scroll_margin = 0;
16121
16122 if ((w->cursor.y >= 0 /* not vscrolled */
16123 && w->cursor.y < this_scroll_margin
16124 && CHARPOS (pos) > BEGV
16125 && IT_CHARPOS (it) < ZV)
16126 /* rms: considering make_cursor_line_fully_visible_p here
16127 seems to give wrong results. We don't want to recenter
16128 when the last line is partly visible, we want to allow
16129 that case to be handled in the usual way. */
16130 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16131 {
16132 w->cursor.vpos = -1;
16133 clear_glyph_matrix (w->desired_matrix);
16134 return -1;
16135 }
16136 }
16137
16138 /* If bottom moved off end of frame, change mode line percentage. */
16139 if (XFASTINT (w->window_end_pos) <= 0
16140 && Z != IT_CHARPOS (it))
16141 w->update_mode_line = Qt;
16142
16143 /* Set window_end_pos to the offset of the last character displayed
16144 on the window from the end of current_buffer. Set
16145 window_end_vpos to its row number. */
16146 if (last_text_row)
16147 {
16148 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16149 w->window_end_bytepos
16150 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16151 w->window_end_pos
16152 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16153 w->window_end_vpos
16154 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16155 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16156 ->displays_text_p);
16157 }
16158 else
16159 {
16160 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16161 w->window_end_pos = make_number (Z - ZV);
16162 w->window_end_vpos = make_number (0);
16163 }
16164
16165 /* But that is not valid info until redisplay finishes. */
16166 w->window_end_valid = Qnil;
16167 return 1;
16168 }
16169
16170
16171 \f
16172 /************************************************************************
16173 Window redisplay reusing current matrix when buffer has not changed
16174 ************************************************************************/
16175
16176 /* Try redisplay of window W showing an unchanged buffer with a
16177 different window start than the last time it was displayed by
16178 reusing its current matrix. Value is non-zero if successful.
16179 W->start is the new window start. */
16180
16181 static int
16182 try_window_reusing_current_matrix (struct window *w)
16183 {
16184 struct frame *f = XFRAME (w->frame);
16185 struct glyph_row *bottom_row;
16186 struct it it;
16187 struct run run;
16188 struct text_pos start, new_start;
16189 int nrows_scrolled, i;
16190 struct glyph_row *last_text_row;
16191 struct glyph_row *last_reused_text_row;
16192 struct glyph_row *start_row;
16193 int start_vpos, min_y, max_y;
16194
16195 #if GLYPH_DEBUG
16196 if (inhibit_try_window_reusing)
16197 return 0;
16198 #endif
16199
16200 if (/* This function doesn't handle terminal frames. */
16201 !FRAME_WINDOW_P (f)
16202 /* Don't try to reuse the display if windows have been split
16203 or such. */
16204 || windows_or_buffers_changed
16205 || cursor_type_changed)
16206 return 0;
16207
16208 /* Can't do this if region may have changed. */
16209 if ((!NILP (Vtransient_mark_mode)
16210 && !NILP (BVAR (current_buffer, mark_active)))
16211 || !NILP (w->region_showing)
16212 || !NILP (Vshow_trailing_whitespace))
16213 return 0;
16214
16215 /* If top-line visibility has changed, give up. */
16216 if (WINDOW_WANTS_HEADER_LINE_P (w)
16217 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16218 return 0;
16219
16220 /* Give up if old or new display is scrolled vertically. We could
16221 make this function handle this, but right now it doesn't. */
16222 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16223 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16224 return 0;
16225
16226 /* The variable new_start now holds the new window start. The old
16227 start `start' can be determined from the current matrix. */
16228 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16229 start = start_row->minpos;
16230 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16231
16232 /* Clear the desired matrix for the display below. */
16233 clear_glyph_matrix (w->desired_matrix);
16234
16235 if (CHARPOS (new_start) <= CHARPOS (start))
16236 {
16237 /* Don't use this method if the display starts with an ellipsis
16238 displayed for invisible text. It's not easy to handle that case
16239 below, and it's certainly not worth the effort since this is
16240 not a frequent case. */
16241 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16242 return 0;
16243
16244 IF_DEBUG (debug_method_add (w, "twu1"));
16245
16246 /* Display up to a row that can be reused. The variable
16247 last_text_row is set to the last row displayed that displays
16248 text. Note that it.vpos == 0 if or if not there is a
16249 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16250 start_display (&it, w, new_start);
16251 w->cursor.vpos = -1;
16252 last_text_row = last_reused_text_row = NULL;
16253
16254 while (it.current_y < it.last_visible_y
16255 && !fonts_changed_p)
16256 {
16257 /* If we have reached into the characters in the START row,
16258 that means the line boundaries have changed. So we
16259 can't start copying with the row START. Maybe it will
16260 work to start copying with the following row. */
16261 while (IT_CHARPOS (it) > CHARPOS (start))
16262 {
16263 /* Advance to the next row as the "start". */
16264 start_row++;
16265 start = start_row->minpos;
16266 /* If there are no more rows to try, or just one, give up. */
16267 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16268 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16269 || CHARPOS (start) == ZV)
16270 {
16271 clear_glyph_matrix (w->desired_matrix);
16272 return 0;
16273 }
16274
16275 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16276 }
16277 /* If we have reached alignment, we can copy the rest of the
16278 rows. */
16279 if (IT_CHARPOS (it) == CHARPOS (start)
16280 /* Don't accept "alignment" inside a display vector,
16281 since start_row could have started in the middle of
16282 that same display vector (thus their character
16283 positions match), and we have no way of telling if
16284 that is the case. */
16285 && it.current.dpvec_index < 0)
16286 break;
16287
16288 if (display_line (&it))
16289 last_text_row = it.glyph_row - 1;
16290
16291 }
16292
16293 /* A value of current_y < last_visible_y means that we stopped
16294 at the previous window start, which in turn means that we
16295 have at least one reusable row. */
16296 if (it.current_y < it.last_visible_y)
16297 {
16298 struct glyph_row *row;
16299
16300 /* IT.vpos always starts from 0; it counts text lines. */
16301 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16302
16303 /* Find PT if not already found in the lines displayed. */
16304 if (w->cursor.vpos < 0)
16305 {
16306 int dy = it.current_y - start_row->y;
16307
16308 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16309 row = row_containing_pos (w, PT, row, NULL, dy);
16310 if (row)
16311 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16312 dy, nrows_scrolled);
16313 else
16314 {
16315 clear_glyph_matrix (w->desired_matrix);
16316 return 0;
16317 }
16318 }
16319
16320 /* Scroll the display. Do it before the current matrix is
16321 changed. The problem here is that update has not yet
16322 run, i.e. part of the current matrix is not up to date.
16323 scroll_run_hook will clear the cursor, and use the
16324 current matrix to get the height of the row the cursor is
16325 in. */
16326 run.current_y = start_row->y;
16327 run.desired_y = it.current_y;
16328 run.height = it.last_visible_y - it.current_y;
16329
16330 if (run.height > 0 && run.current_y != run.desired_y)
16331 {
16332 update_begin (f);
16333 FRAME_RIF (f)->update_window_begin_hook (w);
16334 FRAME_RIF (f)->clear_window_mouse_face (w);
16335 FRAME_RIF (f)->scroll_run_hook (w, &run);
16336 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16337 update_end (f);
16338 }
16339
16340 /* Shift current matrix down by nrows_scrolled lines. */
16341 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16342 rotate_matrix (w->current_matrix,
16343 start_vpos,
16344 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16345 nrows_scrolled);
16346
16347 /* Disable lines that must be updated. */
16348 for (i = 0; i < nrows_scrolled; ++i)
16349 (start_row + i)->enabled_p = 0;
16350
16351 /* Re-compute Y positions. */
16352 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16353 max_y = it.last_visible_y;
16354 for (row = start_row + nrows_scrolled;
16355 row < bottom_row;
16356 ++row)
16357 {
16358 row->y = it.current_y;
16359 row->visible_height = row->height;
16360
16361 if (row->y < min_y)
16362 row->visible_height -= min_y - row->y;
16363 if (row->y + row->height > max_y)
16364 row->visible_height -= row->y + row->height - max_y;
16365 if (row->fringe_bitmap_periodic_p)
16366 row->redraw_fringe_bitmaps_p = 1;
16367
16368 it.current_y += row->height;
16369
16370 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16371 last_reused_text_row = row;
16372 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16373 break;
16374 }
16375
16376 /* Disable lines in the current matrix which are now
16377 below the window. */
16378 for (++row; row < bottom_row; ++row)
16379 row->enabled_p = row->mode_line_p = 0;
16380 }
16381
16382 /* Update window_end_pos etc.; last_reused_text_row is the last
16383 reused row from the current matrix containing text, if any.
16384 The value of last_text_row is the last displayed line
16385 containing text. */
16386 if (last_reused_text_row)
16387 {
16388 w->window_end_bytepos
16389 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16390 w->window_end_pos
16391 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16392 w->window_end_vpos
16393 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16394 w->current_matrix));
16395 }
16396 else if (last_text_row)
16397 {
16398 w->window_end_bytepos
16399 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16400 w->window_end_pos
16401 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16402 w->window_end_vpos
16403 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16404 }
16405 else
16406 {
16407 /* This window must be completely empty. */
16408 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16409 w->window_end_pos = make_number (Z - ZV);
16410 w->window_end_vpos = make_number (0);
16411 }
16412 w->window_end_valid = Qnil;
16413
16414 /* Update hint: don't try scrolling again in update_window. */
16415 w->desired_matrix->no_scrolling_p = 1;
16416
16417 #if GLYPH_DEBUG
16418 debug_method_add (w, "try_window_reusing_current_matrix 1");
16419 #endif
16420 return 1;
16421 }
16422 else if (CHARPOS (new_start) > CHARPOS (start))
16423 {
16424 struct glyph_row *pt_row, *row;
16425 struct glyph_row *first_reusable_row;
16426 struct glyph_row *first_row_to_display;
16427 int dy;
16428 int yb = window_text_bottom_y (w);
16429
16430 /* Find the row starting at new_start, if there is one. Don't
16431 reuse a partially visible line at the end. */
16432 first_reusable_row = start_row;
16433 while (first_reusable_row->enabled_p
16434 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16435 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16436 < CHARPOS (new_start)))
16437 ++first_reusable_row;
16438
16439 /* Give up if there is no row to reuse. */
16440 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16441 || !first_reusable_row->enabled_p
16442 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16443 != CHARPOS (new_start)))
16444 return 0;
16445
16446 /* We can reuse fully visible rows beginning with
16447 first_reusable_row to the end of the window. Set
16448 first_row_to_display to the first row that cannot be reused.
16449 Set pt_row to the row containing point, if there is any. */
16450 pt_row = NULL;
16451 for (first_row_to_display = first_reusable_row;
16452 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16453 ++first_row_to_display)
16454 {
16455 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16456 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16457 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16458 && first_row_to_display->ends_at_zv_p
16459 && pt_row == NULL)))
16460 pt_row = first_row_to_display;
16461 }
16462
16463 /* Start displaying at the start of first_row_to_display. */
16464 xassert (first_row_to_display->y < yb);
16465 init_to_row_start (&it, w, first_row_to_display);
16466
16467 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16468 - start_vpos);
16469 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16470 - nrows_scrolled);
16471 it.current_y = (first_row_to_display->y - first_reusable_row->y
16472 + WINDOW_HEADER_LINE_HEIGHT (w));
16473
16474 /* Display lines beginning with first_row_to_display in the
16475 desired matrix. Set last_text_row to the last row displayed
16476 that displays text. */
16477 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16478 if (pt_row == NULL)
16479 w->cursor.vpos = -1;
16480 last_text_row = NULL;
16481 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16482 if (display_line (&it))
16483 last_text_row = it.glyph_row - 1;
16484
16485 /* If point is in a reused row, adjust y and vpos of the cursor
16486 position. */
16487 if (pt_row)
16488 {
16489 w->cursor.vpos -= nrows_scrolled;
16490 w->cursor.y -= first_reusable_row->y - start_row->y;
16491 }
16492
16493 /* Give up if point isn't in a row displayed or reused. (This
16494 also handles the case where w->cursor.vpos < nrows_scrolled
16495 after the calls to display_line, which can happen with scroll
16496 margins. See bug#1295.) */
16497 if (w->cursor.vpos < 0)
16498 {
16499 clear_glyph_matrix (w->desired_matrix);
16500 return 0;
16501 }
16502
16503 /* Scroll the display. */
16504 run.current_y = first_reusable_row->y;
16505 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16506 run.height = it.last_visible_y - run.current_y;
16507 dy = run.current_y - run.desired_y;
16508
16509 if (run.height)
16510 {
16511 update_begin (f);
16512 FRAME_RIF (f)->update_window_begin_hook (w);
16513 FRAME_RIF (f)->clear_window_mouse_face (w);
16514 FRAME_RIF (f)->scroll_run_hook (w, &run);
16515 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16516 update_end (f);
16517 }
16518
16519 /* Adjust Y positions of reused rows. */
16520 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16521 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16522 max_y = it.last_visible_y;
16523 for (row = first_reusable_row; row < first_row_to_display; ++row)
16524 {
16525 row->y -= dy;
16526 row->visible_height = row->height;
16527 if (row->y < min_y)
16528 row->visible_height -= min_y - row->y;
16529 if (row->y + row->height > max_y)
16530 row->visible_height -= row->y + row->height - max_y;
16531 if (row->fringe_bitmap_periodic_p)
16532 row->redraw_fringe_bitmaps_p = 1;
16533 }
16534
16535 /* Scroll the current matrix. */
16536 xassert (nrows_scrolled > 0);
16537 rotate_matrix (w->current_matrix,
16538 start_vpos,
16539 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16540 -nrows_scrolled);
16541
16542 /* Disable rows not reused. */
16543 for (row -= nrows_scrolled; row < bottom_row; ++row)
16544 row->enabled_p = 0;
16545
16546 /* Point may have moved to a different line, so we cannot assume that
16547 the previous cursor position is valid; locate the correct row. */
16548 if (pt_row)
16549 {
16550 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16551 row < bottom_row
16552 && PT >= MATRIX_ROW_END_CHARPOS (row)
16553 && !row->ends_at_zv_p;
16554 row++)
16555 {
16556 w->cursor.vpos++;
16557 w->cursor.y = row->y;
16558 }
16559 if (row < bottom_row)
16560 {
16561 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16562 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16563
16564 /* Can't use this optimization with bidi-reordered glyph
16565 rows, unless cursor is already at point. */
16566 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16567 {
16568 if (!(w->cursor.hpos >= 0
16569 && w->cursor.hpos < row->used[TEXT_AREA]
16570 && BUFFERP (glyph->object)
16571 && glyph->charpos == PT))
16572 return 0;
16573 }
16574 else
16575 for (; glyph < end
16576 && (!BUFFERP (glyph->object)
16577 || glyph->charpos < PT);
16578 glyph++)
16579 {
16580 w->cursor.hpos++;
16581 w->cursor.x += glyph->pixel_width;
16582 }
16583 }
16584 }
16585
16586 /* Adjust window end. A null value of last_text_row means that
16587 the window end is in reused rows which in turn means that
16588 only its vpos can have changed. */
16589 if (last_text_row)
16590 {
16591 w->window_end_bytepos
16592 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16593 w->window_end_pos
16594 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16595 w->window_end_vpos
16596 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16597 }
16598 else
16599 {
16600 w->window_end_vpos
16601 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16602 }
16603
16604 w->window_end_valid = Qnil;
16605 w->desired_matrix->no_scrolling_p = 1;
16606
16607 #if GLYPH_DEBUG
16608 debug_method_add (w, "try_window_reusing_current_matrix 2");
16609 #endif
16610 return 1;
16611 }
16612
16613 return 0;
16614 }
16615
16616
16617 \f
16618 /************************************************************************
16619 Window redisplay reusing current matrix when buffer has changed
16620 ************************************************************************/
16621
16622 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16623 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16624 EMACS_INT *, EMACS_INT *);
16625 static struct glyph_row *
16626 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16627 struct glyph_row *);
16628
16629
16630 /* Return the last row in MATRIX displaying text. If row START is
16631 non-null, start searching with that row. IT gives the dimensions
16632 of the display. Value is null if matrix is empty; otherwise it is
16633 a pointer to the row found. */
16634
16635 static struct glyph_row *
16636 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16637 struct glyph_row *start)
16638 {
16639 struct glyph_row *row, *row_found;
16640
16641 /* Set row_found to the last row in IT->w's current matrix
16642 displaying text. The loop looks funny but think of partially
16643 visible lines. */
16644 row_found = NULL;
16645 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16646 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16647 {
16648 xassert (row->enabled_p);
16649 row_found = row;
16650 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16651 break;
16652 ++row;
16653 }
16654
16655 return row_found;
16656 }
16657
16658
16659 /* Return the last row in the current matrix of W that is not affected
16660 by changes at the start of current_buffer that occurred since W's
16661 current matrix was built. Value is null if no such row exists.
16662
16663 BEG_UNCHANGED us the number of characters unchanged at the start of
16664 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16665 first changed character in current_buffer. Characters at positions <
16666 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16667 when the current matrix was built. */
16668
16669 static struct glyph_row *
16670 find_last_unchanged_at_beg_row (struct window *w)
16671 {
16672 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16673 struct glyph_row *row;
16674 struct glyph_row *row_found = NULL;
16675 int yb = window_text_bottom_y (w);
16676
16677 /* Find the last row displaying unchanged text. */
16678 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16679 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16680 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16681 ++row)
16682 {
16683 if (/* If row ends before first_changed_pos, it is unchanged,
16684 except in some case. */
16685 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16686 /* When row ends in ZV and we write at ZV it is not
16687 unchanged. */
16688 && !row->ends_at_zv_p
16689 /* When first_changed_pos is the end of a continued line,
16690 row is not unchanged because it may be no longer
16691 continued. */
16692 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16693 && (row->continued_p
16694 || row->exact_window_width_line_p))
16695 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16696 needs to be recomputed, so don't consider this row as
16697 unchanged. This happens when the last line was
16698 bidi-reordered and was killed immediately before this
16699 redisplay cycle. In that case, ROW->end stores the
16700 buffer position of the first visual-order character of
16701 the killed text, which is now beyond ZV. */
16702 && CHARPOS (row->end.pos) <= ZV)
16703 row_found = row;
16704
16705 /* Stop if last visible row. */
16706 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16707 break;
16708 }
16709
16710 return row_found;
16711 }
16712
16713
16714 /* Find the first glyph row in the current matrix of W that is not
16715 affected by changes at the end of current_buffer since the
16716 time W's current matrix was built.
16717
16718 Return in *DELTA the number of chars by which buffer positions in
16719 unchanged text at the end of current_buffer must be adjusted.
16720
16721 Return in *DELTA_BYTES the corresponding number of bytes.
16722
16723 Value is null if no such row exists, i.e. all rows are affected by
16724 changes. */
16725
16726 static struct glyph_row *
16727 find_first_unchanged_at_end_row (struct window *w,
16728 EMACS_INT *delta, EMACS_INT *delta_bytes)
16729 {
16730 struct glyph_row *row;
16731 struct glyph_row *row_found = NULL;
16732
16733 *delta = *delta_bytes = 0;
16734
16735 /* Display must not have been paused, otherwise the current matrix
16736 is not up to date. */
16737 eassert (!NILP (w->window_end_valid));
16738
16739 /* A value of window_end_pos >= END_UNCHANGED means that the window
16740 end is in the range of changed text. If so, there is no
16741 unchanged row at the end of W's current matrix. */
16742 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16743 return NULL;
16744
16745 /* Set row to the last row in W's current matrix displaying text. */
16746 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16747
16748 /* If matrix is entirely empty, no unchanged row exists. */
16749 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16750 {
16751 /* The value of row is the last glyph row in the matrix having a
16752 meaningful buffer position in it. The end position of row
16753 corresponds to window_end_pos. This allows us to translate
16754 buffer positions in the current matrix to current buffer
16755 positions for characters not in changed text. */
16756 EMACS_INT Z_old =
16757 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16758 EMACS_INT Z_BYTE_old =
16759 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16760 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16761 struct glyph_row *first_text_row
16762 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16763
16764 *delta = Z - Z_old;
16765 *delta_bytes = Z_BYTE - Z_BYTE_old;
16766
16767 /* Set last_unchanged_pos to the buffer position of the last
16768 character in the buffer that has not been changed. Z is the
16769 index + 1 of the last character in current_buffer, i.e. by
16770 subtracting END_UNCHANGED we get the index of the last
16771 unchanged character, and we have to add BEG to get its buffer
16772 position. */
16773 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16774 last_unchanged_pos_old = last_unchanged_pos - *delta;
16775
16776 /* Search backward from ROW for a row displaying a line that
16777 starts at a minimum position >= last_unchanged_pos_old. */
16778 for (; row > first_text_row; --row)
16779 {
16780 /* This used to abort, but it can happen.
16781 It is ok to just stop the search instead here. KFS. */
16782 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16783 break;
16784
16785 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16786 row_found = row;
16787 }
16788 }
16789
16790 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16791
16792 return row_found;
16793 }
16794
16795
16796 /* Make sure that glyph rows in the current matrix of window W
16797 reference the same glyph memory as corresponding rows in the
16798 frame's frame matrix. This function is called after scrolling W's
16799 current matrix on a terminal frame in try_window_id and
16800 try_window_reusing_current_matrix. */
16801
16802 static void
16803 sync_frame_with_window_matrix_rows (struct window *w)
16804 {
16805 struct frame *f = XFRAME (w->frame);
16806 struct glyph_row *window_row, *window_row_end, *frame_row;
16807
16808 /* Preconditions: W must be a leaf window and full-width. Its frame
16809 must have a frame matrix. */
16810 xassert (NILP (w->hchild) && NILP (w->vchild));
16811 xassert (WINDOW_FULL_WIDTH_P (w));
16812 xassert (!FRAME_WINDOW_P (f));
16813
16814 /* If W is a full-width window, glyph pointers in W's current matrix
16815 have, by definition, to be the same as glyph pointers in the
16816 corresponding frame matrix. Note that frame matrices have no
16817 marginal areas (see build_frame_matrix). */
16818 window_row = w->current_matrix->rows;
16819 window_row_end = window_row + w->current_matrix->nrows;
16820 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16821 while (window_row < window_row_end)
16822 {
16823 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16824 struct glyph *end = window_row->glyphs[LAST_AREA];
16825
16826 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16827 frame_row->glyphs[TEXT_AREA] = start;
16828 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16829 frame_row->glyphs[LAST_AREA] = end;
16830
16831 /* Disable frame rows whose corresponding window rows have
16832 been disabled in try_window_id. */
16833 if (!window_row->enabled_p)
16834 frame_row->enabled_p = 0;
16835
16836 ++window_row, ++frame_row;
16837 }
16838 }
16839
16840
16841 /* Find the glyph row in window W containing CHARPOS. Consider all
16842 rows between START and END (not inclusive). END null means search
16843 all rows to the end of the display area of W. Value is the row
16844 containing CHARPOS or null. */
16845
16846 struct glyph_row *
16847 row_containing_pos (struct window *w, EMACS_INT charpos,
16848 struct glyph_row *start, struct glyph_row *end, int dy)
16849 {
16850 struct glyph_row *row = start;
16851 struct glyph_row *best_row = NULL;
16852 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16853 int last_y;
16854
16855 /* If we happen to start on a header-line, skip that. */
16856 if (row->mode_line_p)
16857 ++row;
16858
16859 if ((end && row >= end) || !row->enabled_p)
16860 return NULL;
16861
16862 last_y = window_text_bottom_y (w) - dy;
16863
16864 while (1)
16865 {
16866 /* Give up if we have gone too far. */
16867 if (end && row >= end)
16868 return NULL;
16869 /* This formerly returned if they were equal.
16870 I think that both quantities are of a "last plus one" type;
16871 if so, when they are equal, the row is within the screen. -- rms. */
16872 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16873 return NULL;
16874
16875 /* If it is in this row, return this row. */
16876 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16877 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16878 /* The end position of a row equals the start
16879 position of the next row. If CHARPOS is there, we
16880 would rather display it in the next line, except
16881 when this line ends in ZV. */
16882 && !row->ends_at_zv_p
16883 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16884 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16885 {
16886 struct glyph *g;
16887
16888 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16889 || (!best_row && !row->continued_p))
16890 return row;
16891 /* In bidi-reordered rows, there could be several rows
16892 occluding point, all of them belonging to the same
16893 continued line. We need to find the row which fits
16894 CHARPOS the best. */
16895 for (g = row->glyphs[TEXT_AREA];
16896 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16897 g++)
16898 {
16899 if (!STRINGP (g->object))
16900 {
16901 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16902 {
16903 mindif = eabs (g->charpos - charpos);
16904 best_row = row;
16905 /* Exact match always wins. */
16906 if (mindif == 0)
16907 return best_row;
16908 }
16909 }
16910 }
16911 }
16912 else if (best_row && !row->continued_p)
16913 return best_row;
16914 ++row;
16915 }
16916 }
16917
16918
16919 /* Try to redisplay window W by reusing its existing display. W's
16920 current matrix must be up to date when this function is called,
16921 i.e. window_end_valid must not be nil.
16922
16923 Value is
16924
16925 1 if display has been updated
16926 0 if otherwise unsuccessful
16927 -1 if redisplay with same window start is known not to succeed
16928
16929 The following steps are performed:
16930
16931 1. Find the last row in the current matrix of W that is not
16932 affected by changes at the start of current_buffer. If no such row
16933 is found, give up.
16934
16935 2. Find the first row in W's current matrix that is not affected by
16936 changes at the end of current_buffer. Maybe there is no such row.
16937
16938 3. Display lines beginning with the row + 1 found in step 1 to the
16939 row found in step 2 or, if step 2 didn't find a row, to the end of
16940 the window.
16941
16942 4. If cursor is not known to appear on the window, give up.
16943
16944 5. If display stopped at the row found in step 2, scroll the
16945 display and current matrix as needed.
16946
16947 6. Maybe display some lines at the end of W, if we must. This can
16948 happen under various circumstances, like a partially visible line
16949 becoming fully visible, or because newly displayed lines are displayed
16950 in smaller font sizes.
16951
16952 7. Update W's window end information. */
16953
16954 static int
16955 try_window_id (struct window *w)
16956 {
16957 struct frame *f = XFRAME (w->frame);
16958 struct glyph_matrix *current_matrix = w->current_matrix;
16959 struct glyph_matrix *desired_matrix = w->desired_matrix;
16960 struct glyph_row *last_unchanged_at_beg_row;
16961 struct glyph_row *first_unchanged_at_end_row;
16962 struct glyph_row *row;
16963 struct glyph_row *bottom_row;
16964 int bottom_vpos;
16965 struct it it;
16966 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16967 int dvpos, dy;
16968 struct text_pos start_pos;
16969 struct run run;
16970 int first_unchanged_at_end_vpos = 0;
16971 struct glyph_row *last_text_row, *last_text_row_at_end;
16972 struct text_pos start;
16973 EMACS_INT first_changed_charpos, last_changed_charpos;
16974
16975 #if GLYPH_DEBUG
16976 if (inhibit_try_window_id)
16977 return 0;
16978 #endif
16979
16980 /* This is handy for debugging. */
16981 #if 0
16982 #define GIVE_UP(X) \
16983 do { \
16984 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16985 return 0; \
16986 } while (0)
16987 #else
16988 #define GIVE_UP(X) return 0
16989 #endif
16990
16991 SET_TEXT_POS_FROM_MARKER (start, w->start);
16992
16993 /* Don't use this for mini-windows because these can show
16994 messages and mini-buffers, and we don't handle that here. */
16995 if (MINI_WINDOW_P (w))
16996 GIVE_UP (1);
16997
16998 /* This flag is used to prevent redisplay optimizations. */
16999 if (windows_or_buffers_changed || cursor_type_changed)
17000 GIVE_UP (2);
17001
17002 /* Verify that narrowing has not changed.
17003 Also verify that we were not told to prevent redisplay optimizations.
17004 It would be nice to further
17005 reduce the number of cases where this prevents try_window_id. */
17006 if (current_buffer->clip_changed
17007 || current_buffer->prevent_redisplay_optimizations_p)
17008 GIVE_UP (3);
17009
17010 /* Window must either use window-based redisplay or be full width. */
17011 if (!FRAME_WINDOW_P (f)
17012 && (!FRAME_LINE_INS_DEL_OK (f)
17013 || !WINDOW_FULL_WIDTH_P (w)))
17014 GIVE_UP (4);
17015
17016 /* Give up if point is known NOT to appear in W. */
17017 if (PT < CHARPOS (start))
17018 GIVE_UP (5);
17019
17020 /* Another way to prevent redisplay optimizations. */
17021 if (XFASTINT (w->last_modified) == 0)
17022 GIVE_UP (6);
17023
17024 /* Verify that window is not hscrolled. */
17025 if (XFASTINT (w->hscroll) != 0)
17026 GIVE_UP (7);
17027
17028 /* Verify that display wasn't paused. */
17029 if (NILP (w->window_end_valid))
17030 GIVE_UP (8);
17031
17032 /* Can't use this if highlighting a region because a cursor movement
17033 will do more than just set the cursor. */
17034 if (!NILP (Vtransient_mark_mode)
17035 && !NILP (BVAR (current_buffer, mark_active)))
17036 GIVE_UP (9);
17037
17038 /* Likewise if highlighting trailing whitespace. */
17039 if (!NILP (Vshow_trailing_whitespace))
17040 GIVE_UP (11);
17041
17042 /* Likewise if showing a region. */
17043 if (!NILP (w->region_showing))
17044 GIVE_UP (10);
17045
17046 /* Can't use this if overlay arrow position and/or string have
17047 changed. */
17048 if (overlay_arrows_changed_p ())
17049 GIVE_UP (12);
17050
17051 /* When word-wrap is on, adding a space to the first word of a
17052 wrapped line can change the wrap position, altering the line
17053 above it. It might be worthwhile to handle this more
17054 intelligently, but for now just redisplay from scratch. */
17055 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17056 GIVE_UP (21);
17057
17058 /* Under bidi reordering, adding or deleting a character in the
17059 beginning of a paragraph, before the first strong directional
17060 character, can change the base direction of the paragraph (unless
17061 the buffer specifies a fixed paragraph direction), which will
17062 require to redisplay the whole paragraph. It might be worthwhile
17063 to find the paragraph limits and widen the range of redisplayed
17064 lines to that, but for now just give up this optimization and
17065 redisplay from scratch. */
17066 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17067 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17068 GIVE_UP (22);
17069
17070 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17071 only if buffer has really changed. The reason is that the gap is
17072 initially at Z for freshly visited files. The code below would
17073 set end_unchanged to 0 in that case. */
17074 if (MODIFF > SAVE_MODIFF
17075 /* This seems to happen sometimes after saving a buffer. */
17076 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17077 {
17078 if (GPT - BEG < BEG_UNCHANGED)
17079 BEG_UNCHANGED = GPT - BEG;
17080 if (Z - GPT < END_UNCHANGED)
17081 END_UNCHANGED = Z - GPT;
17082 }
17083
17084 /* The position of the first and last character that has been changed. */
17085 first_changed_charpos = BEG + BEG_UNCHANGED;
17086 last_changed_charpos = Z - END_UNCHANGED;
17087
17088 /* If window starts after a line end, and the last change is in
17089 front of that newline, then changes don't affect the display.
17090 This case happens with stealth-fontification. Note that although
17091 the display is unchanged, glyph positions in the matrix have to
17092 be adjusted, of course. */
17093 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17094 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17095 && ((last_changed_charpos < CHARPOS (start)
17096 && CHARPOS (start) == BEGV)
17097 || (last_changed_charpos < CHARPOS (start) - 1
17098 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17099 {
17100 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17101 struct glyph_row *r0;
17102
17103 /* Compute how many chars/bytes have been added to or removed
17104 from the buffer. */
17105 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17106 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17107 Z_delta = Z - Z_old;
17108 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17109
17110 /* Give up if PT is not in the window. Note that it already has
17111 been checked at the start of try_window_id that PT is not in
17112 front of the window start. */
17113 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17114 GIVE_UP (13);
17115
17116 /* If window start is unchanged, we can reuse the whole matrix
17117 as is, after adjusting glyph positions. No need to compute
17118 the window end again, since its offset from Z hasn't changed. */
17119 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17120 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17121 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17122 /* PT must not be in a partially visible line. */
17123 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17124 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17125 {
17126 /* Adjust positions in the glyph matrix. */
17127 if (Z_delta || Z_delta_bytes)
17128 {
17129 struct glyph_row *r1
17130 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17131 increment_matrix_positions (w->current_matrix,
17132 MATRIX_ROW_VPOS (r0, current_matrix),
17133 MATRIX_ROW_VPOS (r1, current_matrix),
17134 Z_delta, Z_delta_bytes);
17135 }
17136
17137 /* Set the cursor. */
17138 row = row_containing_pos (w, PT, r0, NULL, 0);
17139 if (row)
17140 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17141 else
17142 abort ();
17143 return 1;
17144 }
17145 }
17146
17147 /* Handle the case that changes are all below what is displayed in
17148 the window, and that PT is in the window. This shortcut cannot
17149 be taken if ZV is visible in the window, and text has been added
17150 there that is visible in the window. */
17151 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17152 /* ZV is not visible in the window, or there are no
17153 changes at ZV, actually. */
17154 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17155 || first_changed_charpos == last_changed_charpos))
17156 {
17157 struct glyph_row *r0;
17158
17159 /* Give up if PT is not in the window. Note that it already has
17160 been checked at the start of try_window_id that PT is not in
17161 front of the window start. */
17162 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17163 GIVE_UP (14);
17164
17165 /* If window start is unchanged, we can reuse the whole matrix
17166 as is, without changing glyph positions since no text has
17167 been added/removed in front of the window end. */
17168 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17169 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17170 /* PT must not be in a partially visible line. */
17171 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17172 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17173 {
17174 /* We have to compute the window end anew since text
17175 could have been added/removed after it. */
17176 w->window_end_pos
17177 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17178 w->window_end_bytepos
17179 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17180
17181 /* Set the cursor. */
17182 row = row_containing_pos (w, PT, r0, NULL, 0);
17183 if (row)
17184 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17185 else
17186 abort ();
17187 return 2;
17188 }
17189 }
17190
17191 /* Give up if window start is in the changed area.
17192
17193 The condition used to read
17194
17195 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17196
17197 but why that was tested escapes me at the moment. */
17198 if (CHARPOS (start) >= first_changed_charpos
17199 && CHARPOS (start) <= last_changed_charpos)
17200 GIVE_UP (15);
17201
17202 /* Check that window start agrees with the start of the first glyph
17203 row in its current matrix. Check this after we know the window
17204 start is not in changed text, otherwise positions would not be
17205 comparable. */
17206 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17207 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17208 GIVE_UP (16);
17209
17210 /* Give up if the window ends in strings. Overlay strings
17211 at the end are difficult to handle, so don't try. */
17212 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17213 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17214 GIVE_UP (20);
17215
17216 /* Compute the position at which we have to start displaying new
17217 lines. Some of the lines at the top of the window might be
17218 reusable because they are not displaying changed text. Find the
17219 last row in W's current matrix not affected by changes at the
17220 start of current_buffer. Value is null if changes start in the
17221 first line of window. */
17222 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17223 if (last_unchanged_at_beg_row)
17224 {
17225 /* Avoid starting to display in the middle of a character, a TAB
17226 for instance. This is easier than to set up the iterator
17227 exactly, and it's not a frequent case, so the additional
17228 effort wouldn't really pay off. */
17229 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17230 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17231 && last_unchanged_at_beg_row > w->current_matrix->rows)
17232 --last_unchanged_at_beg_row;
17233
17234 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17235 GIVE_UP (17);
17236
17237 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17238 GIVE_UP (18);
17239 start_pos = it.current.pos;
17240
17241 /* Start displaying new lines in the desired matrix at the same
17242 vpos we would use in the current matrix, i.e. below
17243 last_unchanged_at_beg_row. */
17244 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17245 current_matrix);
17246 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17247 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17248
17249 xassert (it.hpos == 0 && it.current_x == 0);
17250 }
17251 else
17252 {
17253 /* There are no reusable lines at the start of the window.
17254 Start displaying in the first text line. */
17255 start_display (&it, w, start);
17256 it.vpos = it.first_vpos;
17257 start_pos = it.current.pos;
17258 }
17259
17260 /* Find the first row that is not affected by changes at the end of
17261 the buffer. Value will be null if there is no unchanged row, in
17262 which case we must redisplay to the end of the window. delta
17263 will be set to the value by which buffer positions beginning with
17264 first_unchanged_at_end_row have to be adjusted due to text
17265 changes. */
17266 first_unchanged_at_end_row
17267 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17268 IF_DEBUG (debug_delta = delta);
17269 IF_DEBUG (debug_delta_bytes = delta_bytes);
17270
17271 /* Set stop_pos to the buffer position up to which we will have to
17272 display new lines. If first_unchanged_at_end_row != NULL, this
17273 is the buffer position of the start of the line displayed in that
17274 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17275 that we don't stop at a buffer position. */
17276 stop_pos = 0;
17277 if (first_unchanged_at_end_row)
17278 {
17279 xassert (last_unchanged_at_beg_row == NULL
17280 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17281
17282 /* If this is a continuation line, move forward to the next one
17283 that isn't. Changes in lines above affect this line.
17284 Caution: this may move first_unchanged_at_end_row to a row
17285 not displaying text. */
17286 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17287 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17288 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17289 < it.last_visible_y))
17290 ++first_unchanged_at_end_row;
17291
17292 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17293 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17294 >= it.last_visible_y))
17295 first_unchanged_at_end_row = NULL;
17296 else
17297 {
17298 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17299 + delta);
17300 first_unchanged_at_end_vpos
17301 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17302 xassert (stop_pos >= Z - END_UNCHANGED);
17303 }
17304 }
17305 else if (last_unchanged_at_beg_row == NULL)
17306 GIVE_UP (19);
17307
17308
17309 #if GLYPH_DEBUG
17310
17311 /* Either there is no unchanged row at the end, or the one we have
17312 now displays text. This is a necessary condition for the window
17313 end pos calculation at the end of this function. */
17314 xassert (first_unchanged_at_end_row == NULL
17315 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17316
17317 debug_last_unchanged_at_beg_vpos
17318 = (last_unchanged_at_beg_row
17319 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17320 : -1);
17321 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17322
17323 #endif /* GLYPH_DEBUG != 0 */
17324
17325
17326 /* Display new lines. Set last_text_row to the last new line
17327 displayed which has text on it, i.e. might end up as being the
17328 line where the window_end_vpos is. */
17329 w->cursor.vpos = -1;
17330 last_text_row = NULL;
17331 overlay_arrow_seen = 0;
17332 while (it.current_y < it.last_visible_y
17333 && !fonts_changed_p
17334 && (first_unchanged_at_end_row == NULL
17335 || IT_CHARPOS (it) < stop_pos))
17336 {
17337 if (display_line (&it))
17338 last_text_row = it.glyph_row - 1;
17339 }
17340
17341 if (fonts_changed_p)
17342 return -1;
17343
17344
17345 /* Compute differences in buffer positions, y-positions etc. for
17346 lines reused at the bottom of the window. Compute what we can
17347 scroll. */
17348 if (first_unchanged_at_end_row
17349 /* No lines reused because we displayed everything up to the
17350 bottom of the window. */
17351 && it.current_y < it.last_visible_y)
17352 {
17353 dvpos = (it.vpos
17354 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17355 current_matrix));
17356 dy = it.current_y - first_unchanged_at_end_row->y;
17357 run.current_y = first_unchanged_at_end_row->y;
17358 run.desired_y = run.current_y + dy;
17359 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17360 }
17361 else
17362 {
17363 delta = delta_bytes = dvpos = dy
17364 = run.current_y = run.desired_y = run.height = 0;
17365 first_unchanged_at_end_row = NULL;
17366 }
17367 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17368
17369
17370 /* Find the cursor if not already found. We have to decide whether
17371 PT will appear on this window (it sometimes doesn't, but this is
17372 not a very frequent case.) This decision has to be made before
17373 the current matrix is altered. A value of cursor.vpos < 0 means
17374 that PT is either in one of the lines beginning at
17375 first_unchanged_at_end_row or below the window. Don't care for
17376 lines that might be displayed later at the window end; as
17377 mentioned, this is not a frequent case. */
17378 if (w->cursor.vpos < 0)
17379 {
17380 /* Cursor in unchanged rows at the top? */
17381 if (PT < CHARPOS (start_pos)
17382 && last_unchanged_at_beg_row)
17383 {
17384 row = row_containing_pos (w, PT,
17385 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17386 last_unchanged_at_beg_row + 1, 0);
17387 if (row)
17388 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17389 }
17390
17391 /* Start from first_unchanged_at_end_row looking for PT. */
17392 else if (first_unchanged_at_end_row)
17393 {
17394 row = row_containing_pos (w, PT - delta,
17395 first_unchanged_at_end_row, NULL, 0);
17396 if (row)
17397 set_cursor_from_row (w, row, w->current_matrix, delta,
17398 delta_bytes, dy, dvpos);
17399 }
17400
17401 /* Give up if cursor was not found. */
17402 if (w->cursor.vpos < 0)
17403 {
17404 clear_glyph_matrix (w->desired_matrix);
17405 return -1;
17406 }
17407 }
17408
17409 /* Don't let the cursor end in the scroll margins. */
17410 {
17411 int this_scroll_margin, cursor_height;
17412
17413 this_scroll_margin =
17414 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17415 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17416 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17417
17418 if ((w->cursor.y < this_scroll_margin
17419 && CHARPOS (start) > BEGV)
17420 /* Old redisplay didn't take scroll margin into account at the bottom,
17421 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17422 || (w->cursor.y + (make_cursor_line_fully_visible_p
17423 ? cursor_height + this_scroll_margin
17424 : 1)) > it.last_visible_y)
17425 {
17426 w->cursor.vpos = -1;
17427 clear_glyph_matrix (w->desired_matrix);
17428 return -1;
17429 }
17430 }
17431
17432 /* Scroll the display. Do it before changing the current matrix so
17433 that xterm.c doesn't get confused about where the cursor glyph is
17434 found. */
17435 if (dy && run.height)
17436 {
17437 update_begin (f);
17438
17439 if (FRAME_WINDOW_P (f))
17440 {
17441 FRAME_RIF (f)->update_window_begin_hook (w);
17442 FRAME_RIF (f)->clear_window_mouse_face (w);
17443 FRAME_RIF (f)->scroll_run_hook (w, &run);
17444 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17445 }
17446 else
17447 {
17448 /* Terminal frame. In this case, dvpos gives the number of
17449 lines to scroll by; dvpos < 0 means scroll up. */
17450 int from_vpos
17451 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17452 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17453 int end = (WINDOW_TOP_EDGE_LINE (w)
17454 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17455 + window_internal_height (w));
17456
17457 #if defined (HAVE_GPM) || defined (MSDOS)
17458 x_clear_window_mouse_face (w);
17459 #endif
17460 /* Perform the operation on the screen. */
17461 if (dvpos > 0)
17462 {
17463 /* Scroll last_unchanged_at_beg_row to the end of the
17464 window down dvpos lines. */
17465 set_terminal_window (f, end);
17466
17467 /* On dumb terminals delete dvpos lines at the end
17468 before inserting dvpos empty lines. */
17469 if (!FRAME_SCROLL_REGION_OK (f))
17470 ins_del_lines (f, end - dvpos, -dvpos);
17471
17472 /* Insert dvpos empty lines in front of
17473 last_unchanged_at_beg_row. */
17474 ins_del_lines (f, from, dvpos);
17475 }
17476 else if (dvpos < 0)
17477 {
17478 /* Scroll up last_unchanged_at_beg_vpos to the end of
17479 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17480 set_terminal_window (f, end);
17481
17482 /* Delete dvpos lines in front of
17483 last_unchanged_at_beg_vpos. ins_del_lines will set
17484 the cursor to the given vpos and emit |dvpos| delete
17485 line sequences. */
17486 ins_del_lines (f, from + dvpos, dvpos);
17487
17488 /* On a dumb terminal insert dvpos empty lines at the
17489 end. */
17490 if (!FRAME_SCROLL_REGION_OK (f))
17491 ins_del_lines (f, end + dvpos, -dvpos);
17492 }
17493
17494 set_terminal_window (f, 0);
17495 }
17496
17497 update_end (f);
17498 }
17499
17500 /* Shift reused rows of the current matrix to the right position.
17501 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17502 text. */
17503 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17504 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17505 if (dvpos < 0)
17506 {
17507 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17508 bottom_vpos, dvpos);
17509 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17510 bottom_vpos, 0);
17511 }
17512 else if (dvpos > 0)
17513 {
17514 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17515 bottom_vpos, dvpos);
17516 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17517 first_unchanged_at_end_vpos + dvpos, 0);
17518 }
17519
17520 /* For frame-based redisplay, make sure that current frame and window
17521 matrix are in sync with respect to glyph memory. */
17522 if (!FRAME_WINDOW_P (f))
17523 sync_frame_with_window_matrix_rows (w);
17524
17525 /* Adjust buffer positions in reused rows. */
17526 if (delta || delta_bytes)
17527 increment_matrix_positions (current_matrix,
17528 first_unchanged_at_end_vpos + dvpos,
17529 bottom_vpos, delta, delta_bytes);
17530
17531 /* Adjust Y positions. */
17532 if (dy)
17533 shift_glyph_matrix (w, current_matrix,
17534 first_unchanged_at_end_vpos + dvpos,
17535 bottom_vpos, dy);
17536
17537 if (first_unchanged_at_end_row)
17538 {
17539 first_unchanged_at_end_row += dvpos;
17540 if (first_unchanged_at_end_row->y >= it.last_visible_y
17541 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17542 first_unchanged_at_end_row = NULL;
17543 }
17544
17545 /* If scrolling up, there may be some lines to display at the end of
17546 the window. */
17547 last_text_row_at_end = NULL;
17548 if (dy < 0)
17549 {
17550 /* Scrolling up can leave for example a partially visible line
17551 at the end of the window to be redisplayed. */
17552 /* Set last_row to the glyph row in the current matrix where the
17553 window end line is found. It has been moved up or down in
17554 the matrix by dvpos. */
17555 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17556 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17557
17558 /* If last_row is the window end line, it should display text. */
17559 xassert (last_row->displays_text_p);
17560
17561 /* If window end line was partially visible before, begin
17562 displaying at that line. Otherwise begin displaying with the
17563 line following it. */
17564 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17565 {
17566 init_to_row_start (&it, w, last_row);
17567 it.vpos = last_vpos;
17568 it.current_y = last_row->y;
17569 }
17570 else
17571 {
17572 init_to_row_end (&it, w, last_row);
17573 it.vpos = 1 + last_vpos;
17574 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17575 ++last_row;
17576 }
17577
17578 /* We may start in a continuation line. If so, we have to
17579 get the right continuation_lines_width and current_x. */
17580 it.continuation_lines_width = last_row->continuation_lines_width;
17581 it.hpos = it.current_x = 0;
17582
17583 /* Display the rest of the lines at the window end. */
17584 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17585 while (it.current_y < it.last_visible_y
17586 && !fonts_changed_p)
17587 {
17588 /* Is it always sure that the display agrees with lines in
17589 the current matrix? I don't think so, so we mark rows
17590 displayed invalid in the current matrix by setting their
17591 enabled_p flag to zero. */
17592 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17593 if (display_line (&it))
17594 last_text_row_at_end = it.glyph_row - 1;
17595 }
17596 }
17597
17598 /* Update window_end_pos and window_end_vpos. */
17599 if (first_unchanged_at_end_row
17600 && !last_text_row_at_end)
17601 {
17602 /* Window end line if one of the preserved rows from the current
17603 matrix. Set row to the last row displaying text in current
17604 matrix starting at first_unchanged_at_end_row, after
17605 scrolling. */
17606 xassert (first_unchanged_at_end_row->displays_text_p);
17607 row = find_last_row_displaying_text (w->current_matrix, &it,
17608 first_unchanged_at_end_row);
17609 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17610
17611 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17612 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17613 w->window_end_vpos
17614 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17615 xassert (w->window_end_bytepos >= 0);
17616 IF_DEBUG (debug_method_add (w, "A"));
17617 }
17618 else if (last_text_row_at_end)
17619 {
17620 w->window_end_pos
17621 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17622 w->window_end_bytepos
17623 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17624 w->window_end_vpos
17625 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17626 xassert (w->window_end_bytepos >= 0);
17627 IF_DEBUG (debug_method_add (w, "B"));
17628 }
17629 else if (last_text_row)
17630 {
17631 /* We have displayed either to the end of the window or at the
17632 end of the window, i.e. the last row with text is to be found
17633 in the desired matrix. */
17634 w->window_end_pos
17635 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17636 w->window_end_bytepos
17637 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17638 w->window_end_vpos
17639 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17640 xassert (w->window_end_bytepos >= 0);
17641 }
17642 else if (first_unchanged_at_end_row == NULL
17643 && last_text_row == NULL
17644 && last_text_row_at_end == NULL)
17645 {
17646 /* Displayed to end of window, but no line containing text was
17647 displayed. Lines were deleted at the end of the window. */
17648 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17649 int vpos = XFASTINT (w->window_end_vpos);
17650 struct glyph_row *current_row = current_matrix->rows + vpos;
17651 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17652
17653 for (row = NULL;
17654 row == NULL && vpos >= first_vpos;
17655 --vpos, --current_row, --desired_row)
17656 {
17657 if (desired_row->enabled_p)
17658 {
17659 if (desired_row->displays_text_p)
17660 row = desired_row;
17661 }
17662 else if (current_row->displays_text_p)
17663 row = current_row;
17664 }
17665
17666 xassert (row != NULL);
17667 w->window_end_vpos = make_number (vpos + 1);
17668 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17669 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17670 xassert (w->window_end_bytepos >= 0);
17671 IF_DEBUG (debug_method_add (w, "C"));
17672 }
17673 else
17674 abort ();
17675
17676 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17677 debug_end_vpos = XFASTINT (w->window_end_vpos));
17678
17679 /* Record that display has not been completed. */
17680 w->window_end_valid = Qnil;
17681 w->desired_matrix->no_scrolling_p = 1;
17682 return 3;
17683
17684 #undef GIVE_UP
17685 }
17686
17687
17688 \f
17689 /***********************************************************************
17690 More debugging support
17691 ***********************************************************************/
17692
17693 #if GLYPH_DEBUG
17694
17695 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17696 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17697 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17698
17699
17700 /* Dump the contents of glyph matrix MATRIX on stderr.
17701
17702 GLYPHS 0 means don't show glyph contents.
17703 GLYPHS 1 means show glyphs in short form
17704 GLYPHS > 1 means show glyphs in long form. */
17705
17706 void
17707 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17708 {
17709 int i;
17710 for (i = 0; i < matrix->nrows; ++i)
17711 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17712 }
17713
17714
17715 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17716 the glyph row and area where the glyph comes from. */
17717
17718 void
17719 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17720 {
17721 if (glyph->type == CHAR_GLYPH)
17722 {
17723 fprintf (stderr,
17724 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17725 glyph - row->glyphs[TEXT_AREA],
17726 'C',
17727 glyph->charpos,
17728 (BUFFERP (glyph->object)
17729 ? 'B'
17730 : (STRINGP (glyph->object)
17731 ? 'S'
17732 : '-')),
17733 glyph->pixel_width,
17734 glyph->u.ch,
17735 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17736 ? glyph->u.ch
17737 : '.'),
17738 glyph->face_id,
17739 glyph->left_box_line_p,
17740 glyph->right_box_line_p);
17741 }
17742 else if (glyph->type == STRETCH_GLYPH)
17743 {
17744 fprintf (stderr,
17745 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17746 glyph - row->glyphs[TEXT_AREA],
17747 'S',
17748 glyph->charpos,
17749 (BUFFERP (glyph->object)
17750 ? 'B'
17751 : (STRINGP (glyph->object)
17752 ? 'S'
17753 : '-')),
17754 glyph->pixel_width,
17755 0,
17756 '.',
17757 glyph->face_id,
17758 glyph->left_box_line_p,
17759 glyph->right_box_line_p);
17760 }
17761 else if (glyph->type == IMAGE_GLYPH)
17762 {
17763 fprintf (stderr,
17764 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17765 glyph - row->glyphs[TEXT_AREA],
17766 'I',
17767 glyph->charpos,
17768 (BUFFERP (glyph->object)
17769 ? 'B'
17770 : (STRINGP (glyph->object)
17771 ? 'S'
17772 : '-')),
17773 glyph->pixel_width,
17774 glyph->u.img_id,
17775 '.',
17776 glyph->face_id,
17777 glyph->left_box_line_p,
17778 glyph->right_box_line_p);
17779 }
17780 else if (glyph->type == COMPOSITE_GLYPH)
17781 {
17782 fprintf (stderr,
17783 " %5td %4c %6"pI"d %c %3d 0x%05x",
17784 glyph - row->glyphs[TEXT_AREA],
17785 '+',
17786 glyph->charpos,
17787 (BUFFERP (glyph->object)
17788 ? 'B'
17789 : (STRINGP (glyph->object)
17790 ? 'S'
17791 : '-')),
17792 glyph->pixel_width,
17793 glyph->u.cmp.id);
17794 if (glyph->u.cmp.automatic)
17795 fprintf (stderr,
17796 "[%d-%d]",
17797 glyph->slice.cmp.from, glyph->slice.cmp.to);
17798 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17799 glyph->face_id,
17800 glyph->left_box_line_p,
17801 glyph->right_box_line_p);
17802 }
17803 }
17804
17805
17806 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17807 GLYPHS 0 means don't show glyph contents.
17808 GLYPHS 1 means show glyphs in short form
17809 GLYPHS > 1 means show glyphs in long form. */
17810
17811 void
17812 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17813 {
17814 if (glyphs != 1)
17815 {
17816 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17817 fprintf (stderr, "======================================================================\n");
17818
17819 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17820 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17821 vpos,
17822 MATRIX_ROW_START_CHARPOS (row),
17823 MATRIX_ROW_END_CHARPOS (row),
17824 row->used[TEXT_AREA],
17825 row->contains_overlapping_glyphs_p,
17826 row->enabled_p,
17827 row->truncated_on_left_p,
17828 row->truncated_on_right_p,
17829 row->continued_p,
17830 MATRIX_ROW_CONTINUATION_LINE_P (row),
17831 row->displays_text_p,
17832 row->ends_at_zv_p,
17833 row->fill_line_p,
17834 row->ends_in_middle_of_char_p,
17835 row->starts_in_middle_of_char_p,
17836 row->mouse_face_p,
17837 row->x,
17838 row->y,
17839 row->pixel_width,
17840 row->height,
17841 row->visible_height,
17842 row->ascent,
17843 row->phys_ascent);
17844 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17845 row->end.overlay_string_index,
17846 row->continuation_lines_width);
17847 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17848 CHARPOS (row->start.string_pos),
17849 CHARPOS (row->end.string_pos));
17850 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17851 row->end.dpvec_index);
17852 }
17853
17854 if (glyphs > 1)
17855 {
17856 int area;
17857
17858 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17859 {
17860 struct glyph *glyph = row->glyphs[area];
17861 struct glyph *glyph_end = glyph + row->used[area];
17862
17863 /* Glyph for a line end in text. */
17864 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17865 ++glyph_end;
17866
17867 if (glyph < glyph_end)
17868 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17869
17870 for (; glyph < glyph_end; ++glyph)
17871 dump_glyph (row, glyph, area);
17872 }
17873 }
17874 else if (glyphs == 1)
17875 {
17876 int area;
17877
17878 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17879 {
17880 char *s = (char *) alloca (row->used[area] + 1);
17881 int i;
17882
17883 for (i = 0; i < row->used[area]; ++i)
17884 {
17885 struct glyph *glyph = row->glyphs[area] + i;
17886 if (glyph->type == CHAR_GLYPH
17887 && glyph->u.ch < 0x80
17888 && glyph->u.ch >= ' ')
17889 s[i] = glyph->u.ch;
17890 else
17891 s[i] = '.';
17892 }
17893
17894 s[i] = '\0';
17895 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17896 }
17897 }
17898 }
17899
17900
17901 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17902 Sdump_glyph_matrix, 0, 1, "p",
17903 doc: /* Dump the current matrix of the selected window to stderr.
17904 Shows contents of glyph row structures. With non-nil
17905 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17906 glyphs in short form, otherwise show glyphs in long form. */)
17907 (Lisp_Object glyphs)
17908 {
17909 struct window *w = XWINDOW (selected_window);
17910 struct buffer *buffer = XBUFFER (w->buffer);
17911
17912 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17913 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17914 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17915 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17916 fprintf (stderr, "=============================================\n");
17917 dump_glyph_matrix (w->current_matrix,
17918 NILP (glyphs) ? 0 : XINT (glyphs));
17919 return Qnil;
17920 }
17921
17922
17923 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17924 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17925 (void)
17926 {
17927 struct frame *f = XFRAME (selected_frame);
17928 dump_glyph_matrix (f->current_matrix, 1);
17929 return Qnil;
17930 }
17931
17932
17933 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17934 doc: /* Dump glyph row ROW to stderr.
17935 GLYPH 0 means don't dump glyphs.
17936 GLYPH 1 means dump glyphs in short form.
17937 GLYPH > 1 or omitted means dump glyphs in long form. */)
17938 (Lisp_Object row, Lisp_Object glyphs)
17939 {
17940 struct glyph_matrix *matrix;
17941 int vpos;
17942
17943 CHECK_NUMBER (row);
17944 matrix = XWINDOW (selected_window)->current_matrix;
17945 vpos = XINT (row);
17946 if (vpos >= 0 && vpos < matrix->nrows)
17947 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17948 vpos,
17949 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17950 return Qnil;
17951 }
17952
17953
17954 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17955 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17956 GLYPH 0 means don't dump glyphs.
17957 GLYPH 1 means dump glyphs in short form.
17958 GLYPH > 1 or omitted means dump glyphs in long form. */)
17959 (Lisp_Object row, Lisp_Object glyphs)
17960 {
17961 struct frame *sf = SELECTED_FRAME ();
17962 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17963 int vpos;
17964
17965 CHECK_NUMBER (row);
17966 vpos = XINT (row);
17967 if (vpos >= 0 && vpos < m->nrows)
17968 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17969 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17970 return Qnil;
17971 }
17972
17973
17974 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17975 doc: /* Toggle tracing of redisplay.
17976 With ARG, turn tracing on if and only if ARG is positive. */)
17977 (Lisp_Object arg)
17978 {
17979 if (NILP (arg))
17980 trace_redisplay_p = !trace_redisplay_p;
17981 else
17982 {
17983 arg = Fprefix_numeric_value (arg);
17984 trace_redisplay_p = XINT (arg) > 0;
17985 }
17986
17987 return Qnil;
17988 }
17989
17990
17991 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17992 doc: /* Like `format', but print result to stderr.
17993 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17994 (ptrdiff_t nargs, Lisp_Object *args)
17995 {
17996 Lisp_Object s = Fformat (nargs, args);
17997 fprintf (stderr, "%s", SDATA (s));
17998 return Qnil;
17999 }
18000
18001 #endif /* GLYPH_DEBUG */
18002
18003
18004 \f
18005 /***********************************************************************
18006 Building Desired Matrix Rows
18007 ***********************************************************************/
18008
18009 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18010 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18011
18012 static struct glyph_row *
18013 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18014 {
18015 struct frame *f = XFRAME (WINDOW_FRAME (w));
18016 struct buffer *buffer = XBUFFER (w->buffer);
18017 struct buffer *old = current_buffer;
18018 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18019 int arrow_len = SCHARS (overlay_arrow_string);
18020 const unsigned char *arrow_end = arrow_string + arrow_len;
18021 const unsigned char *p;
18022 struct it it;
18023 int multibyte_p;
18024 int n_glyphs_before;
18025
18026 set_buffer_temp (buffer);
18027 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18028 it.glyph_row->used[TEXT_AREA] = 0;
18029 SET_TEXT_POS (it.position, 0, 0);
18030
18031 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18032 p = arrow_string;
18033 while (p < arrow_end)
18034 {
18035 Lisp_Object face, ilisp;
18036
18037 /* Get the next character. */
18038 if (multibyte_p)
18039 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18040 else
18041 {
18042 it.c = it.char_to_display = *p, it.len = 1;
18043 if (! ASCII_CHAR_P (it.c))
18044 it.char_to_display = BYTE8_TO_CHAR (it.c);
18045 }
18046 p += it.len;
18047
18048 /* Get its face. */
18049 ilisp = make_number (p - arrow_string);
18050 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18051 it.face_id = compute_char_face (f, it.char_to_display, face);
18052
18053 /* Compute its width, get its glyphs. */
18054 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18055 SET_TEXT_POS (it.position, -1, -1);
18056 PRODUCE_GLYPHS (&it);
18057
18058 /* If this character doesn't fit any more in the line, we have
18059 to remove some glyphs. */
18060 if (it.current_x > it.last_visible_x)
18061 {
18062 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18063 break;
18064 }
18065 }
18066
18067 set_buffer_temp (old);
18068 return it.glyph_row;
18069 }
18070
18071
18072 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18073 glyphs are only inserted for terminal frames since we can't really
18074 win with truncation glyphs when partially visible glyphs are
18075 involved. Which glyphs to insert is determined by
18076 produce_special_glyphs. */
18077
18078 static void
18079 insert_left_trunc_glyphs (struct it *it)
18080 {
18081 struct it truncate_it;
18082 struct glyph *from, *end, *to, *toend;
18083
18084 xassert (!FRAME_WINDOW_P (it->f));
18085
18086 /* Get the truncation glyphs. */
18087 truncate_it = *it;
18088 truncate_it.current_x = 0;
18089 truncate_it.face_id = DEFAULT_FACE_ID;
18090 truncate_it.glyph_row = &scratch_glyph_row;
18091 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18092 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18093 truncate_it.object = make_number (0);
18094 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18095
18096 /* Overwrite glyphs from IT with truncation glyphs. */
18097 if (!it->glyph_row->reversed_p)
18098 {
18099 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18100 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18101 to = it->glyph_row->glyphs[TEXT_AREA];
18102 toend = to + it->glyph_row->used[TEXT_AREA];
18103
18104 while (from < end)
18105 *to++ = *from++;
18106
18107 /* There may be padding glyphs left over. Overwrite them too. */
18108 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18109 {
18110 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18111 while (from < end)
18112 *to++ = *from++;
18113 }
18114
18115 if (to > toend)
18116 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18117 }
18118 else
18119 {
18120 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18121 that back to front. */
18122 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18123 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18124 toend = it->glyph_row->glyphs[TEXT_AREA];
18125 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18126
18127 while (from >= end && to >= toend)
18128 *to-- = *from--;
18129 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18130 {
18131 from =
18132 truncate_it.glyph_row->glyphs[TEXT_AREA]
18133 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18134 while (from >= end && to >= toend)
18135 *to-- = *from--;
18136 }
18137 if (from >= end)
18138 {
18139 /* Need to free some room before prepending additional
18140 glyphs. */
18141 int move_by = from - end + 1;
18142 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18143 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18144
18145 for ( ; g >= g0; g--)
18146 g[move_by] = *g;
18147 while (from >= end)
18148 *to-- = *from--;
18149 it->glyph_row->used[TEXT_AREA] += move_by;
18150 }
18151 }
18152 }
18153
18154 /* Compute the hash code for ROW. */
18155 unsigned
18156 row_hash (struct glyph_row *row)
18157 {
18158 int area, k;
18159 unsigned hashval = 0;
18160
18161 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18162 for (k = 0; k < row->used[area]; ++k)
18163 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18164 + row->glyphs[area][k].u.val
18165 + row->glyphs[area][k].face_id
18166 + row->glyphs[area][k].padding_p
18167 + (row->glyphs[area][k].type << 2));
18168
18169 return hashval;
18170 }
18171
18172 /* Compute the pixel height and width of IT->glyph_row.
18173
18174 Most of the time, ascent and height of a display line will be equal
18175 to the max_ascent and max_height values of the display iterator
18176 structure. This is not the case if
18177
18178 1. We hit ZV without displaying anything. In this case, max_ascent
18179 and max_height will be zero.
18180
18181 2. We have some glyphs that don't contribute to the line height.
18182 (The glyph row flag contributes_to_line_height_p is for future
18183 pixmap extensions).
18184
18185 The first case is easily covered by using default values because in
18186 these cases, the line height does not really matter, except that it
18187 must not be zero. */
18188
18189 static void
18190 compute_line_metrics (struct it *it)
18191 {
18192 struct glyph_row *row = it->glyph_row;
18193
18194 if (FRAME_WINDOW_P (it->f))
18195 {
18196 int i, min_y, max_y;
18197
18198 /* The line may consist of one space only, that was added to
18199 place the cursor on it. If so, the row's height hasn't been
18200 computed yet. */
18201 if (row->height == 0)
18202 {
18203 if (it->max_ascent + it->max_descent == 0)
18204 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18205 row->ascent = it->max_ascent;
18206 row->height = it->max_ascent + it->max_descent;
18207 row->phys_ascent = it->max_phys_ascent;
18208 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18209 row->extra_line_spacing = it->max_extra_line_spacing;
18210 }
18211
18212 /* Compute the width of this line. */
18213 row->pixel_width = row->x;
18214 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18215 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18216
18217 xassert (row->pixel_width >= 0);
18218 xassert (row->ascent >= 0 && row->height > 0);
18219
18220 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18221 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18222
18223 /* If first line's physical ascent is larger than its logical
18224 ascent, use the physical ascent, and make the row taller.
18225 This makes accented characters fully visible. */
18226 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18227 && row->phys_ascent > row->ascent)
18228 {
18229 row->height += row->phys_ascent - row->ascent;
18230 row->ascent = row->phys_ascent;
18231 }
18232
18233 /* Compute how much of the line is visible. */
18234 row->visible_height = row->height;
18235
18236 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18237 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18238
18239 if (row->y < min_y)
18240 row->visible_height -= min_y - row->y;
18241 if (row->y + row->height > max_y)
18242 row->visible_height -= row->y + row->height - max_y;
18243 }
18244 else
18245 {
18246 row->pixel_width = row->used[TEXT_AREA];
18247 if (row->continued_p)
18248 row->pixel_width -= it->continuation_pixel_width;
18249 else if (row->truncated_on_right_p)
18250 row->pixel_width -= it->truncation_pixel_width;
18251 row->ascent = row->phys_ascent = 0;
18252 row->height = row->phys_height = row->visible_height = 1;
18253 row->extra_line_spacing = 0;
18254 }
18255
18256 /* Compute a hash code for this row. */
18257 row->hash = row_hash (row);
18258
18259 it->max_ascent = it->max_descent = 0;
18260 it->max_phys_ascent = it->max_phys_descent = 0;
18261 }
18262
18263
18264 /* Append one space to the glyph row of iterator IT if doing a
18265 window-based redisplay. The space has the same face as
18266 IT->face_id. Value is non-zero if a space was added.
18267
18268 This function is called to make sure that there is always one glyph
18269 at the end of a glyph row that the cursor can be set on under
18270 window-systems. (If there weren't such a glyph we would not know
18271 how wide and tall a box cursor should be displayed).
18272
18273 At the same time this space let's a nicely handle clearing to the
18274 end of the line if the row ends in italic text. */
18275
18276 static int
18277 append_space_for_newline (struct it *it, int default_face_p)
18278 {
18279 if (FRAME_WINDOW_P (it->f))
18280 {
18281 int n = it->glyph_row->used[TEXT_AREA];
18282
18283 if (it->glyph_row->glyphs[TEXT_AREA] + n
18284 < it->glyph_row->glyphs[1 + TEXT_AREA])
18285 {
18286 /* Save some values that must not be changed.
18287 Must save IT->c and IT->len because otherwise
18288 ITERATOR_AT_END_P wouldn't work anymore after
18289 append_space_for_newline has been called. */
18290 enum display_element_type saved_what = it->what;
18291 int saved_c = it->c, saved_len = it->len;
18292 int saved_char_to_display = it->char_to_display;
18293 int saved_x = it->current_x;
18294 int saved_face_id = it->face_id;
18295 struct text_pos saved_pos;
18296 Lisp_Object saved_object;
18297 struct face *face;
18298
18299 saved_object = it->object;
18300 saved_pos = it->position;
18301
18302 it->what = IT_CHARACTER;
18303 memset (&it->position, 0, sizeof it->position);
18304 it->object = make_number (0);
18305 it->c = it->char_to_display = ' ';
18306 it->len = 1;
18307
18308 /* If the default face was remapped, be sure to use the
18309 remapped face for the appended newline. */
18310 if (default_face_p)
18311 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18312 else if (it->face_before_selective_p)
18313 it->face_id = it->saved_face_id;
18314 face = FACE_FROM_ID (it->f, it->face_id);
18315 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18316
18317 PRODUCE_GLYPHS (it);
18318
18319 it->override_ascent = -1;
18320 it->constrain_row_ascent_descent_p = 0;
18321 it->current_x = saved_x;
18322 it->object = saved_object;
18323 it->position = saved_pos;
18324 it->what = saved_what;
18325 it->face_id = saved_face_id;
18326 it->len = saved_len;
18327 it->c = saved_c;
18328 it->char_to_display = saved_char_to_display;
18329 return 1;
18330 }
18331 }
18332
18333 return 0;
18334 }
18335
18336
18337 /* Extend the face of the last glyph in the text area of IT->glyph_row
18338 to the end of the display line. Called from display_line. If the
18339 glyph row is empty, add a space glyph to it so that we know the
18340 face to draw. Set the glyph row flag fill_line_p. If the glyph
18341 row is R2L, prepend a stretch glyph to cover the empty space to the
18342 left of the leftmost glyph. */
18343
18344 static void
18345 extend_face_to_end_of_line (struct it *it)
18346 {
18347 struct face *face, *default_face;
18348 struct frame *f = it->f;
18349
18350 /* If line is already filled, do nothing. Non window-system frames
18351 get a grace of one more ``pixel'' because their characters are
18352 1-``pixel'' wide, so they hit the equality too early. This grace
18353 is needed only for R2L rows that are not continued, to produce
18354 one extra blank where we could display the cursor. */
18355 if (it->current_x >= it->last_visible_x
18356 + (!FRAME_WINDOW_P (f)
18357 && it->glyph_row->reversed_p
18358 && !it->glyph_row->continued_p))
18359 return;
18360
18361 /* The default face, possibly remapped. */
18362 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18363
18364 /* Face extension extends the background and box of IT->face_id
18365 to the end of the line. If the background equals the background
18366 of the frame, we don't have to do anything. */
18367 if (it->face_before_selective_p)
18368 face = FACE_FROM_ID (f, it->saved_face_id);
18369 else
18370 face = FACE_FROM_ID (f, it->face_id);
18371
18372 if (FRAME_WINDOW_P (f)
18373 && it->glyph_row->displays_text_p
18374 && face->box == FACE_NO_BOX
18375 && face->background == FRAME_BACKGROUND_PIXEL (f)
18376 && !face->stipple
18377 && !it->glyph_row->reversed_p)
18378 return;
18379
18380 /* Set the glyph row flag indicating that the face of the last glyph
18381 in the text area has to be drawn to the end of the text area. */
18382 it->glyph_row->fill_line_p = 1;
18383
18384 /* If current character of IT is not ASCII, make sure we have the
18385 ASCII face. This will be automatically undone the next time
18386 get_next_display_element returns a multibyte character. Note
18387 that the character will always be single byte in unibyte
18388 text. */
18389 if (!ASCII_CHAR_P (it->c))
18390 {
18391 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18392 }
18393
18394 if (FRAME_WINDOW_P (f))
18395 {
18396 /* If the row is empty, add a space with the current face of IT,
18397 so that we know which face to draw. */
18398 if (it->glyph_row->used[TEXT_AREA] == 0)
18399 {
18400 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18401 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18402 it->glyph_row->used[TEXT_AREA] = 1;
18403 }
18404 #ifdef HAVE_WINDOW_SYSTEM
18405 if (it->glyph_row->reversed_p)
18406 {
18407 /* Prepend a stretch glyph to the row, such that the
18408 rightmost glyph will be drawn flushed all the way to the
18409 right margin of the window. The stretch glyph that will
18410 occupy the empty space, if any, to the left of the
18411 glyphs. */
18412 struct font *font = face->font ? face->font : FRAME_FONT (f);
18413 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18414 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18415 struct glyph *g;
18416 int row_width, stretch_ascent, stretch_width;
18417 struct text_pos saved_pos;
18418 int saved_face_id, saved_avoid_cursor;
18419
18420 for (row_width = 0, g = row_start; g < row_end; g++)
18421 row_width += g->pixel_width;
18422 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18423 if (stretch_width > 0)
18424 {
18425 stretch_ascent =
18426 (((it->ascent + it->descent)
18427 * FONT_BASE (font)) / FONT_HEIGHT (font));
18428 saved_pos = it->position;
18429 memset (&it->position, 0, sizeof it->position);
18430 saved_avoid_cursor = it->avoid_cursor_p;
18431 it->avoid_cursor_p = 1;
18432 saved_face_id = it->face_id;
18433 /* The last row's stretch glyph should get the default
18434 face, to avoid painting the rest of the window with
18435 the region face, if the region ends at ZV. */
18436 if (it->glyph_row->ends_at_zv_p)
18437 it->face_id = default_face->id;
18438 else
18439 it->face_id = face->id;
18440 append_stretch_glyph (it, make_number (0), stretch_width,
18441 it->ascent + it->descent, stretch_ascent);
18442 it->position = saved_pos;
18443 it->avoid_cursor_p = saved_avoid_cursor;
18444 it->face_id = saved_face_id;
18445 }
18446 }
18447 #endif /* HAVE_WINDOW_SYSTEM */
18448 }
18449 else
18450 {
18451 /* Save some values that must not be changed. */
18452 int saved_x = it->current_x;
18453 struct text_pos saved_pos;
18454 Lisp_Object saved_object;
18455 enum display_element_type saved_what = it->what;
18456 int saved_face_id = it->face_id;
18457
18458 saved_object = it->object;
18459 saved_pos = it->position;
18460
18461 it->what = IT_CHARACTER;
18462 memset (&it->position, 0, sizeof it->position);
18463 it->object = make_number (0);
18464 it->c = it->char_to_display = ' ';
18465 it->len = 1;
18466 /* The last row's blank glyphs should get the default face, to
18467 avoid painting the rest of the window with the region face,
18468 if the region ends at ZV. */
18469 if (it->glyph_row->ends_at_zv_p)
18470 it->face_id = default_face->id;
18471 else
18472 it->face_id = face->id;
18473
18474 PRODUCE_GLYPHS (it);
18475
18476 while (it->current_x <= it->last_visible_x)
18477 PRODUCE_GLYPHS (it);
18478
18479 /* Don't count these blanks really. It would let us insert a left
18480 truncation glyph below and make us set the cursor on them, maybe. */
18481 it->current_x = saved_x;
18482 it->object = saved_object;
18483 it->position = saved_pos;
18484 it->what = saved_what;
18485 it->face_id = saved_face_id;
18486 }
18487 }
18488
18489
18490 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18491 trailing whitespace. */
18492
18493 static int
18494 trailing_whitespace_p (EMACS_INT charpos)
18495 {
18496 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18497 int c = 0;
18498
18499 while (bytepos < ZV_BYTE
18500 && (c = FETCH_CHAR (bytepos),
18501 c == ' ' || c == '\t'))
18502 ++bytepos;
18503
18504 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18505 {
18506 if (bytepos != PT_BYTE)
18507 return 1;
18508 }
18509 return 0;
18510 }
18511
18512
18513 /* Highlight trailing whitespace, if any, in ROW. */
18514
18515 static void
18516 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18517 {
18518 int used = row->used[TEXT_AREA];
18519
18520 if (used)
18521 {
18522 struct glyph *start = row->glyphs[TEXT_AREA];
18523 struct glyph *glyph = start + used - 1;
18524
18525 if (row->reversed_p)
18526 {
18527 /* Right-to-left rows need to be processed in the opposite
18528 direction, so swap the edge pointers. */
18529 glyph = start;
18530 start = row->glyphs[TEXT_AREA] + used - 1;
18531 }
18532
18533 /* Skip over glyphs inserted to display the cursor at the
18534 end of a line, for extending the face of the last glyph
18535 to the end of the line on terminals, and for truncation
18536 and continuation glyphs. */
18537 if (!row->reversed_p)
18538 {
18539 while (glyph >= start
18540 && glyph->type == CHAR_GLYPH
18541 && INTEGERP (glyph->object))
18542 --glyph;
18543 }
18544 else
18545 {
18546 while (glyph <= start
18547 && glyph->type == CHAR_GLYPH
18548 && INTEGERP (glyph->object))
18549 ++glyph;
18550 }
18551
18552 /* If last glyph is a space or stretch, and it's trailing
18553 whitespace, set the face of all trailing whitespace glyphs in
18554 IT->glyph_row to `trailing-whitespace'. */
18555 if ((row->reversed_p ? glyph <= start : glyph >= start)
18556 && BUFFERP (glyph->object)
18557 && (glyph->type == STRETCH_GLYPH
18558 || (glyph->type == CHAR_GLYPH
18559 && glyph->u.ch == ' '))
18560 && trailing_whitespace_p (glyph->charpos))
18561 {
18562 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18563 if (face_id < 0)
18564 return;
18565
18566 if (!row->reversed_p)
18567 {
18568 while (glyph >= start
18569 && BUFFERP (glyph->object)
18570 && (glyph->type == STRETCH_GLYPH
18571 || (glyph->type == CHAR_GLYPH
18572 && glyph->u.ch == ' ')))
18573 (glyph--)->face_id = face_id;
18574 }
18575 else
18576 {
18577 while (glyph <= start
18578 && BUFFERP (glyph->object)
18579 && (glyph->type == STRETCH_GLYPH
18580 || (glyph->type == CHAR_GLYPH
18581 && glyph->u.ch == ' ')))
18582 (glyph++)->face_id = face_id;
18583 }
18584 }
18585 }
18586 }
18587
18588
18589 /* Value is non-zero if glyph row ROW should be
18590 used to hold the cursor. */
18591
18592 static int
18593 cursor_row_p (struct glyph_row *row)
18594 {
18595 int result = 1;
18596
18597 if (PT == CHARPOS (row->end.pos)
18598 || PT == MATRIX_ROW_END_CHARPOS (row))
18599 {
18600 /* Suppose the row ends on a string.
18601 Unless the row is continued, that means it ends on a newline
18602 in the string. If it's anything other than a display string
18603 (e.g., a before-string from an overlay), we don't want the
18604 cursor there. (This heuristic seems to give the optimal
18605 behavior for the various types of multi-line strings.)
18606 One exception: if the string has `cursor' property on one of
18607 its characters, we _do_ want the cursor there. */
18608 if (CHARPOS (row->end.string_pos) >= 0)
18609 {
18610 if (row->continued_p)
18611 result = 1;
18612 else
18613 {
18614 /* Check for `display' property. */
18615 struct glyph *beg = row->glyphs[TEXT_AREA];
18616 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18617 struct glyph *glyph;
18618
18619 result = 0;
18620 for (glyph = end; glyph >= beg; --glyph)
18621 if (STRINGP (glyph->object))
18622 {
18623 Lisp_Object prop
18624 = Fget_char_property (make_number (PT),
18625 Qdisplay, Qnil);
18626 result =
18627 (!NILP (prop)
18628 && display_prop_string_p (prop, glyph->object));
18629 /* If there's a `cursor' property on one of the
18630 string's characters, this row is a cursor row,
18631 even though this is not a display string. */
18632 if (!result)
18633 {
18634 Lisp_Object s = glyph->object;
18635
18636 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18637 {
18638 EMACS_INT gpos = glyph->charpos;
18639
18640 if (!NILP (Fget_char_property (make_number (gpos),
18641 Qcursor, s)))
18642 {
18643 result = 1;
18644 break;
18645 }
18646 }
18647 }
18648 break;
18649 }
18650 }
18651 }
18652 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18653 {
18654 /* If the row ends in middle of a real character,
18655 and the line is continued, we want the cursor here.
18656 That's because CHARPOS (ROW->end.pos) would equal
18657 PT if PT is before the character. */
18658 if (!row->ends_in_ellipsis_p)
18659 result = row->continued_p;
18660 else
18661 /* If the row ends in an ellipsis, then
18662 CHARPOS (ROW->end.pos) will equal point after the
18663 invisible text. We want that position to be displayed
18664 after the ellipsis. */
18665 result = 0;
18666 }
18667 /* If the row ends at ZV, display the cursor at the end of that
18668 row instead of at the start of the row below. */
18669 else if (row->ends_at_zv_p)
18670 result = 1;
18671 else
18672 result = 0;
18673 }
18674
18675 return result;
18676 }
18677
18678 \f
18679
18680 /* Push the property PROP so that it will be rendered at the current
18681 position in IT. Return 1 if PROP was successfully pushed, 0
18682 otherwise. Called from handle_line_prefix to handle the
18683 `line-prefix' and `wrap-prefix' properties. */
18684
18685 static int
18686 push_prefix_prop (struct it *it, Lisp_Object prop)
18687 {
18688 struct text_pos pos =
18689 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18690
18691 xassert (it->method == GET_FROM_BUFFER
18692 || it->method == GET_FROM_DISPLAY_VECTOR
18693 || it->method == GET_FROM_STRING);
18694
18695 /* We need to save the current buffer/string position, so it will be
18696 restored by pop_it, because iterate_out_of_display_property
18697 depends on that being set correctly, but some situations leave
18698 it->position not yet set when this function is called. */
18699 push_it (it, &pos);
18700
18701 if (STRINGP (prop))
18702 {
18703 if (SCHARS (prop) == 0)
18704 {
18705 pop_it (it);
18706 return 0;
18707 }
18708
18709 it->string = prop;
18710 it->string_from_prefix_prop_p = 1;
18711 it->multibyte_p = STRING_MULTIBYTE (it->string);
18712 it->current.overlay_string_index = -1;
18713 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18714 it->end_charpos = it->string_nchars = SCHARS (it->string);
18715 it->method = GET_FROM_STRING;
18716 it->stop_charpos = 0;
18717 it->prev_stop = 0;
18718 it->base_level_stop = 0;
18719
18720 /* Force paragraph direction to be that of the parent
18721 buffer/string. */
18722 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18723 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18724 else
18725 it->paragraph_embedding = L2R;
18726
18727 /* Set up the bidi iterator for this display string. */
18728 if (it->bidi_p)
18729 {
18730 it->bidi_it.string.lstring = it->string;
18731 it->bidi_it.string.s = NULL;
18732 it->bidi_it.string.schars = it->end_charpos;
18733 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18734 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18735 it->bidi_it.string.unibyte = !it->multibyte_p;
18736 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18737 }
18738 }
18739 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18740 {
18741 it->method = GET_FROM_STRETCH;
18742 it->object = prop;
18743 }
18744 #ifdef HAVE_WINDOW_SYSTEM
18745 else if (IMAGEP (prop))
18746 {
18747 it->what = IT_IMAGE;
18748 it->image_id = lookup_image (it->f, prop);
18749 it->method = GET_FROM_IMAGE;
18750 }
18751 #endif /* HAVE_WINDOW_SYSTEM */
18752 else
18753 {
18754 pop_it (it); /* bogus display property, give up */
18755 return 0;
18756 }
18757
18758 return 1;
18759 }
18760
18761 /* Return the character-property PROP at the current position in IT. */
18762
18763 static Lisp_Object
18764 get_it_property (struct it *it, Lisp_Object prop)
18765 {
18766 Lisp_Object position;
18767
18768 if (STRINGP (it->object))
18769 position = make_number (IT_STRING_CHARPOS (*it));
18770 else if (BUFFERP (it->object))
18771 position = make_number (IT_CHARPOS (*it));
18772 else
18773 return Qnil;
18774
18775 return Fget_char_property (position, prop, it->object);
18776 }
18777
18778 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18779
18780 static void
18781 handle_line_prefix (struct it *it)
18782 {
18783 Lisp_Object prefix;
18784
18785 if (it->continuation_lines_width > 0)
18786 {
18787 prefix = get_it_property (it, Qwrap_prefix);
18788 if (NILP (prefix))
18789 prefix = Vwrap_prefix;
18790 }
18791 else
18792 {
18793 prefix = get_it_property (it, Qline_prefix);
18794 if (NILP (prefix))
18795 prefix = Vline_prefix;
18796 }
18797 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18798 {
18799 /* If the prefix is wider than the window, and we try to wrap
18800 it, it would acquire its own wrap prefix, and so on till the
18801 iterator stack overflows. So, don't wrap the prefix. */
18802 it->line_wrap = TRUNCATE;
18803 it->avoid_cursor_p = 1;
18804 }
18805 }
18806
18807 \f
18808
18809 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18810 only for R2L lines from display_line and display_string, when they
18811 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18812 the line/string needs to be continued on the next glyph row. */
18813 static void
18814 unproduce_glyphs (struct it *it, int n)
18815 {
18816 struct glyph *glyph, *end;
18817
18818 xassert (it->glyph_row);
18819 xassert (it->glyph_row->reversed_p);
18820 xassert (it->area == TEXT_AREA);
18821 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18822
18823 if (n > it->glyph_row->used[TEXT_AREA])
18824 n = it->glyph_row->used[TEXT_AREA];
18825 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18826 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18827 for ( ; glyph < end; glyph++)
18828 glyph[-n] = *glyph;
18829 }
18830
18831 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18832 and ROW->maxpos. */
18833 static void
18834 find_row_edges (struct it *it, struct glyph_row *row,
18835 EMACS_INT min_pos, EMACS_INT min_bpos,
18836 EMACS_INT max_pos, EMACS_INT max_bpos)
18837 {
18838 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18839 lines' rows is implemented for bidi-reordered rows. */
18840
18841 /* ROW->minpos is the value of min_pos, the minimal buffer position
18842 we have in ROW, or ROW->start.pos if that is smaller. */
18843 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18844 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18845 else
18846 /* We didn't find buffer positions smaller than ROW->start, or
18847 didn't find _any_ valid buffer positions in any of the glyphs,
18848 so we must trust the iterator's computed positions. */
18849 row->minpos = row->start.pos;
18850 if (max_pos <= 0)
18851 {
18852 max_pos = CHARPOS (it->current.pos);
18853 max_bpos = BYTEPOS (it->current.pos);
18854 }
18855
18856 /* Here are the various use-cases for ending the row, and the
18857 corresponding values for ROW->maxpos:
18858
18859 Line ends in a newline from buffer eol_pos + 1
18860 Line is continued from buffer max_pos + 1
18861 Line is truncated on right it->current.pos
18862 Line ends in a newline from string max_pos + 1(*)
18863 (*) + 1 only when line ends in a forward scan
18864 Line is continued from string max_pos
18865 Line is continued from display vector max_pos
18866 Line is entirely from a string min_pos == max_pos
18867 Line is entirely from a display vector min_pos == max_pos
18868 Line that ends at ZV ZV
18869
18870 If you discover other use-cases, please add them here as
18871 appropriate. */
18872 if (row->ends_at_zv_p)
18873 row->maxpos = it->current.pos;
18874 else if (row->used[TEXT_AREA])
18875 {
18876 int seen_this_string = 0;
18877 struct glyph_row *r1 = row - 1;
18878
18879 /* Did we see the same display string on the previous row? */
18880 if (STRINGP (it->object)
18881 /* this is not the first row */
18882 && row > it->w->desired_matrix->rows
18883 /* previous row is not the header line */
18884 && !r1->mode_line_p
18885 /* previous row also ends in a newline from a string */
18886 && r1->ends_in_newline_from_string_p)
18887 {
18888 struct glyph *start, *end;
18889
18890 /* Search for the last glyph of the previous row that came
18891 from buffer or string. Depending on whether the row is
18892 L2R or R2L, we need to process it front to back or the
18893 other way round. */
18894 if (!r1->reversed_p)
18895 {
18896 start = r1->glyphs[TEXT_AREA];
18897 end = start + r1->used[TEXT_AREA];
18898 /* Glyphs inserted by redisplay have an integer (zero)
18899 as their object. */
18900 while (end > start
18901 && INTEGERP ((end - 1)->object)
18902 && (end - 1)->charpos <= 0)
18903 --end;
18904 if (end > start)
18905 {
18906 if (EQ ((end - 1)->object, it->object))
18907 seen_this_string = 1;
18908 }
18909 else
18910 /* If all the glyphs of the previous row were inserted
18911 by redisplay, it means the previous row was
18912 produced from a single newline, which is only
18913 possible if that newline came from the same string
18914 as the one which produced this ROW. */
18915 seen_this_string = 1;
18916 }
18917 else
18918 {
18919 end = r1->glyphs[TEXT_AREA] - 1;
18920 start = end + r1->used[TEXT_AREA];
18921 while (end < start
18922 && INTEGERP ((end + 1)->object)
18923 && (end + 1)->charpos <= 0)
18924 ++end;
18925 if (end < start)
18926 {
18927 if (EQ ((end + 1)->object, it->object))
18928 seen_this_string = 1;
18929 }
18930 else
18931 seen_this_string = 1;
18932 }
18933 }
18934 /* Take note of each display string that covers a newline only
18935 once, the first time we see it. This is for when a display
18936 string includes more than one newline in it. */
18937 if (row->ends_in_newline_from_string_p && !seen_this_string)
18938 {
18939 /* If we were scanning the buffer forward when we displayed
18940 the string, we want to account for at least one buffer
18941 position that belongs to this row (position covered by
18942 the display string), so that cursor positioning will
18943 consider this row as a candidate when point is at the end
18944 of the visual line represented by this row. This is not
18945 required when scanning back, because max_pos will already
18946 have a much larger value. */
18947 if (CHARPOS (row->end.pos) > max_pos)
18948 INC_BOTH (max_pos, max_bpos);
18949 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18950 }
18951 else if (CHARPOS (it->eol_pos) > 0)
18952 SET_TEXT_POS (row->maxpos,
18953 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18954 else if (row->continued_p)
18955 {
18956 /* If max_pos is different from IT's current position, it
18957 means IT->method does not belong to the display element
18958 at max_pos. However, it also means that the display
18959 element at max_pos was displayed in its entirety on this
18960 line, which is equivalent to saying that the next line
18961 starts at the next buffer position. */
18962 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18963 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18964 else
18965 {
18966 INC_BOTH (max_pos, max_bpos);
18967 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18968 }
18969 }
18970 else if (row->truncated_on_right_p)
18971 /* display_line already called reseat_at_next_visible_line_start,
18972 which puts the iterator at the beginning of the next line, in
18973 the logical order. */
18974 row->maxpos = it->current.pos;
18975 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18976 /* A line that is entirely from a string/image/stretch... */
18977 row->maxpos = row->minpos;
18978 else
18979 abort ();
18980 }
18981 else
18982 row->maxpos = it->current.pos;
18983 }
18984
18985 /* Construct the glyph row IT->glyph_row in the desired matrix of
18986 IT->w from text at the current position of IT. See dispextern.h
18987 for an overview of struct it. Value is non-zero if
18988 IT->glyph_row displays text, as opposed to a line displaying ZV
18989 only. */
18990
18991 static int
18992 display_line (struct it *it)
18993 {
18994 struct glyph_row *row = it->glyph_row;
18995 Lisp_Object overlay_arrow_string;
18996 struct it wrap_it;
18997 void *wrap_data = NULL;
18998 int may_wrap = 0, wrap_x IF_LINT (= 0);
18999 int wrap_row_used = -1;
19000 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19001 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19002 int wrap_row_extra_line_spacing IF_LINT (= 0);
19003 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19004 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19005 int cvpos;
19006 EMACS_INT min_pos = ZV + 1, max_pos = 0;
19007 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19008
19009 /* We always start displaying at hpos zero even if hscrolled. */
19010 xassert (it->hpos == 0 && it->current_x == 0);
19011
19012 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19013 >= it->w->desired_matrix->nrows)
19014 {
19015 it->w->nrows_scale_factor++;
19016 fonts_changed_p = 1;
19017 return 0;
19018 }
19019
19020 /* Is IT->w showing the region? */
19021 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19022
19023 /* Clear the result glyph row and enable it. */
19024 prepare_desired_row (row);
19025
19026 row->y = it->current_y;
19027 row->start = it->start;
19028 row->continuation_lines_width = it->continuation_lines_width;
19029 row->displays_text_p = 1;
19030 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19031 it->starts_in_middle_of_char_p = 0;
19032
19033 /* Arrange the overlays nicely for our purposes. Usually, we call
19034 display_line on only one line at a time, in which case this
19035 can't really hurt too much, or we call it on lines which appear
19036 one after another in the buffer, in which case all calls to
19037 recenter_overlay_lists but the first will be pretty cheap. */
19038 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19039
19040 /* Move over display elements that are not visible because we are
19041 hscrolled. This may stop at an x-position < IT->first_visible_x
19042 if the first glyph is partially visible or if we hit a line end. */
19043 if (it->current_x < it->first_visible_x)
19044 {
19045 this_line_min_pos = row->start.pos;
19046 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19047 MOVE_TO_POS | MOVE_TO_X);
19048 /* Record the smallest positions seen while we moved over
19049 display elements that are not visible. This is needed by
19050 redisplay_internal for optimizing the case where the cursor
19051 stays inside the same line. The rest of this function only
19052 considers positions that are actually displayed, so
19053 RECORD_MAX_MIN_POS will not otherwise record positions that
19054 are hscrolled to the left of the left edge of the window. */
19055 min_pos = CHARPOS (this_line_min_pos);
19056 min_bpos = BYTEPOS (this_line_min_pos);
19057 }
19058 else
19059 {
19060 /* We only do this when not calling `move_it_in_display_line_to'
19061 above, because move_it_in_display_line_to calls
19062 handle_line_prefix itself. */
19063 handle_line_prefix (it);
19064 }
19065
19066 /* Get the initial row height. This is either the height of the
19067 text hscrolled, if there is any, or zero. */
19068 row->ascent = it->max_ascent;
19069 row->height = it->max_ascent + it->max_descent;
19070 row->phys_ascent = it->max_phys_ascent;
19071 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19072 row->extra_line_spacing = it->max_extra_line_spacing;
19073
19074 /* Utility macro to record max and min buffer positions seen until now. */
19075 #define RECORD_MAX_MIN_POS(IT) \
19076 do \
19077 { \
19078 int composition_p = !STRINGP ((IT)->string) \
19079 && ((IT)->what == IT_COMPOSITION); \
19080 EMACS_INT current_pos = \
19081 composition_p ? (IT)->cmp_it.charpos \
19082 : IT_CHARPOS (*(IT)); \
19083 EMACS_INT current_bpos = \
19084 composition_p ? CHAR_TO_BYTE (current_pos) \
19085 : IT_BYTEPOS (*(IT)); \
19086 if (current_pos < min_pos) \
19087 { \
19088 min_pos = current_pos; \
19089 min_bpos = current_bpos; \
19090 } \
19091 if (IT_CHARPOS (*it) > max_pos) \
19092 { \
19093 max_pos = IT_CHARPOS (*it); \
19094 max_bpos = IT_BYTEPOS (*it); \
19095 } \
19096 } \
19097 while (0)
19098
19099 /* Loop generating characters. The loop is left with IT on the next
19100 character to display. */
19101 while (1)
19102 {
19103 int n_glyphs_before, hpos_before, x_before;
19104 int x, nglyphs;
19105 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19106
19107 /* Retrieve the next thing to display. Value is zero if end of
19108 buffer reached. */
19109 if (!get_next_display_element (it))
19110 {
19111 /* Maybe add a space at the end of this line that is used to
19112 display the cursor there under X. Set the charpos of the
19113 first glyph of blank lines not corresponding to any text
19114 to -1. */
19115 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19116 row->exact_window_width_line_p = 1;
19117 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19118 || row->used[TEXT_AREA] == 0)
19119 {
19120 row->glyphs[TEXT_AREA]->charpos = -1;
19121 row->displays_text_p = 0;
19122
19123 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19124 && (!MINI_WINDOW_P (it->w)
19125 || (minibuf_level && EQ (it->window, minibuf_window))))
19126 row->indicate_empty_line_p = 1;
19127 }
19128
19129 it->continuation_lines_width = 0;
19130 row->ends_at_zv_p = 1;
19131 /* A row that displays right-to-left text must always have
19132 its last face extended all the way to the end of line,
19133 even if this row ends in ZV, because we still write to
19134 the screen left to right. We also need to extend the
19135 last face if the default face is remapped to some
19136 different face, otherwise the functions that clear
19137 portions of the screen will clear with the default face's
19138 background color. */
19139 if (row->reversed_p
19140 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19141 extend_face_to_end_of_line (it);
19142 break;
19143 }
19144
19145 /* Now, get the metrics of what we want to display. This also
19146 generates glyphs in `row' (which is IT->glyph_row). */
19147 n_glyphs_before = row->used[TEXT_AREA];
19148 x = it->current_x;
19149
19150 /* Remember the line height so far in case the next element doesn't
19151 fit on the line. */
19152 if (it->line_wrap != TRUNCATE)
19153 {
19154 ascent = it->max_ascent;
19155 descent = it->max_descent;
19156 phys_ascent = it->max_phys_ascent;
19157 phys_descent = it->max_phys_descent;
19158
19159 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19160 {
19161 if (IT_DISPLAYING_WHITESPACE (it))
19162 may_wrap = 1;
19163 else if (may_wrap)
19164 {
19165 SAVE_IT (wrap_it, *it, wrap_data);
19166 wrap_x = x;
19167 wrap_row_used = row->used[TEXT_AREA];
19168 wrap_row_ascent = row->ascent;
19169 wrap_row_height = row->height;
19170 wrap_row_phys_ascent = row->phys_ascent;
19171 wrap_row_phys_height = row->phys_height;
19172 wrap_row_extra_line_spacing = row->extra_line_spacing;
19173 wrap_row_min_pos = min_pos;
19174 wrap_row_min_bpos = min_bpos;
19175 wrap_row_max_pos = max_pos;
19176 wrap_row_max_bpos = max_bpos;
19177 may_wrap = 0;
19178 }
19179 }
19180 }
19181
19182 PRODUCE_GLYPHS (it);
19183
19184 /* If this display element was in marginal areas, continue with
19185 the next one. */
19186 if (it->area != TEXT_AREA)
19187 {
19188 row->ascent = max (row->ascent, it->max_ascent);
19189 row->height = max (row->height, it->max_ascent + it->max_descent);
19190 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19191 row->phys_height = max (row->phys_height,
19192 it->max_phys_ascent + it->max_phys_descent);
19193 row->extra_line_spacing = max (row->extra_line_spacing,
19194 it->max_extra_line_spacing);
19195 set_iterator_to_next (it, 1);
19196 continue;
19197 }
19198
19199 /* Does the display element fit on the line? If we truncate
19200 lines, we should draw past the right edge of the window. If
19201 we don't truncate, we want to stop so that we can display the
19202 continuation glyph before the right margin. If lines are
19203 continued, there are two possible strategies for characters
19204 resulting in more than 1 glyph (e.g. tabs): Display as many
19205 glyphs as possible in this line and leave the rest for the
19206 continuation line, or display the whole element in the next
19207 line. Original redisplay did the former, so we do it also. */
19208 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19209 hpos_before = it->hpos;
19210 x_before = x;
19211
19212 if (/* Not a newline. */
19213 nglyphs > 0
19214 /* Glyphs produced fit entirely in the line. */
19215 && it->current_x < it->last_visible_x)
19216 {
19217 it->hpos += nglyphs;
19218 row->ascent = max (row->ascent, it->max_ascent);
19219 row->height = max (row->height, it->max_ascent + it->max_descent);
19220 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19221 row->phys_height = max (row->phys_height,
19222 it->max_phys_ascent + it->max_phys_descent);
19223 row->extra_line_spacing = max (row->extra_line_spacing,
19224 it->max_extra_line_spacing);
19225 if (it->current_x - it->pixel_width < it->first_visible_x)
19226 row->x = x - it->first_visible_x;
19227 /* Record the maximum and minimum buffer positions seen so
19228 far in glyphs that will be displayed by this row. */
19229 if (it->bidi_p)
19230 RECORD_MAX_MIN_POS (it);
19231 }
19232 else
19233 {
19234 int i, new_x;
19235 struct glyph *glyph;
19236
19237 for (i = 0; i < nglyphs; ++i, x = new_x)
19238 {
19239 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19240 new_x = x + glyph->pixel_width;
19241
19242 if (/* Lines are continued. */
19243 it->line_wrap != TRUNCATE
19244 && (/* Glyph doesn't fit on the line. */
19245 new_x > it->last_visible_x
19246 /* Or it fits exactly on a window system frame. */
19247 || (new_x == it->last_visible_x
19248 && FRAME_WINDOW_P (it->f))))
19249 {
19250 /* End of a continued line. */
19251
19252 if (it->hpos == 0
19253 || (new_x == it->last_visible_x
19254 && FRAME_WINDOW_P (it->f)))
19255 {
19256 /* Current glyph is the only one on the line or
19257 fits exactly on the line. We must continue
19258 the line because we can't draw the cursor
19259 after the glyph. */
19260 row->continued_p = 1;
19261 it->current_x = new_x;
19262 it->continuation_lines_width += new_x;
19263 ++it->hpos;
19264 if (i == nglyphs - 1)
19265 {
19266 /* If line-wrap is on, check if a previous
19267 wrap point was found. */
19268 if (wrap_row_used > 0
19269 /* Even if there is a previous wrap
19270 point, continue the line here as
19271 usual, if (i) the previous character
19272 was a space or tab AND (ii) the
19273 current character is not. */
19274 && (!may_wrap
19275 || IT_DISPLAYING_WHITESPACE (it)))
19276 goto back_to_wrap;
19277
19278 /* Record the maximum and minimum buffer
19279 positions seen so far in glyphs that will be
19280 displayed by this row. */
19281 if (it->bidi_p)
19282 RECORD_MAX_MIN_POS (it);
19283 set_iterator_to_next (it, 1);
19284 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19285 {
19286 if (!get_next_display_element (it))
19287 {
19288 row->exact_window_width_line_p = 1;
19289 it->continuation_lines_width = 0;
19290 row->continued_p = 0;
19291 row->ends_at_zv_p = 1;
19292 }
19293 else if (ITERATOR_AT_END_OF_LINE_P (it))
19294 {
19295 row->continued_p = 0;
19296 row->exact_window_width_line_p = 1;
19297 }
19298 }
19299 }
19300 else if (it->bidi_p)
19301 RECORD_MAX_MIN_POS (it);
19302 }
19303 else if (CHAR_GLYPH_PADDING_P (*glyph)
19304 && !FRAME_WINDOW_P (it->f))
19305 {
19306 /* A padding glyph that doesn't fit on this line.
19307 This means the whole character doesn't fit
19308 on the line. */
19309 if (row->reversed_p)
19310 unproduce_glyphs (it, row->used[TEXT_AREA]
19311 - n_glyphs_before);
19312 row->used[TEXT_AREA] = n_glyphs_before;
19313
19314 /* Fill the rest of the row with continuation
19315 glyphs like in 20.x. */
19316 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19317 < row->glyphs[1 + TEXT_AREA])
19318 produce_special_glyphs (it, IT_CONTINUATION);
19319
19320 row->continued_p = 1;
19321 it->current_x = x_before;
19322 it->continuation_lines_width += x_before;
19323
19324 /* Restore the height to what it was before the
19325 element not fitting on the line. */
19326 it->max_ascent = ascent;
19327 it->max_descent = descent;
19328 it->max_phys_ascent = phys_ascent;
19329 it->max_phys_descent = phys_descent;
19330 }
19331 else if (wrap_row_used > 0)
19332 {
19333 back_to_wrap:
19334 if (row->reversed_p)
19335 unproduce_glyphs (it,
19336 row->used[TEXT_AREA] - wrap_row_used);
19337 RESTORE_IT (it, &wrap_it, wrap_data);
19338 it->continuation_lines_width += wrap_x;
19339 row->used[TEXT_AREA] = wrap_row_used;
19340 row->ascent = wrap_row_ascent;
19341 row->height = wrap_row_height;
19342 row->phys_ascent = wrap_row_phys_ascent;
19343 row->phys_height = wrap_row_phys_height;
19344 row->extra_line_spacing = wrap_row_extra_line_spacing;
19345 min_pos = wrap_row_min_pos;
19346 min_bpos = wrap_row_min_bpos;
19347 max_pos = wrap_row_max_pos;
19348 max_bpos = wrap_row_max_bpos;
19349 row->continued_p = 1;
19350 row->ends_at_zv_p = 0;
19351 row->exact_window_width_line_p = 0;
19352 it->continuation_lines_width += x;
19353
19354 /* Make sure that a non-default face is extended
19355 up to the right margin of the window. */
19356 extend_face_to_end_of_line (it);
19357 }
19358 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19359 {
19360 /* A TAB that extends past the right edge of the
19361 window. This produces a single glyph on
19362 window system frames. We leave the glyph in
19363 this row and let it fill the row, but don't
19364 consume the TAB. */
19365 it->continuation_lines_width += it->last_visible_x;
19366 row->ends_in_middle_of_char_p = 1;
19367 row->continued_p = 1;
19368 glyph->pixel_width = it->last_visible_x - x;
19369 it->starts_in_middle_of_char_p = 1;
19370 }
19371 else
19372 {
19373 /* Something other than a TAB that draws past
19374 the right edge of the window. Restore
19375 positions to values before the element. */
19376 if (row->reversed_p)
19377 unproduce_glyphs (it, row->used[TEXT_AREA]
19378 - (n_glyphs_before + i));
19379 row->used[TEXT_AREA] = n_glyphs_before + i;
19380
19381 /* Display continuation glyphs. */
19382 if (!FRAME_WINDOW_P (it->f))
19383 produce_special_glyphs (it, IT_CONTINUATION);
19384 row->continued_p = 1;
19385
19386 it->current_x = x_before;
19387 it->continuation_lines_width += x;
19388 extend_face_to_end_of_line (it);
19389
19390 if (nglyphs > 1 && i > 0)
19391 {
19392 row->ends_in_middle_of_char_p = 1;
19393 it->starts_in_middle_of_char_p = 1;
19394 }
19395
19396 /* Restore the height to what it was before the
19397 element not fitting on the line. */
19398 it->max_ascent = ascent;
19399 it->max_descent = descent;
19400 it->max_phys_ascent = phys_ascent;
19401 it->max_phys_descent = phys_descent;
19402 }
19403
19404 break;
19405 }
19406 else if (new_x > it->first_visible_x)
19407 {
19408 /* Increment number of glyphs actually displayed. */
19409 ++it->hpos;
19410
19411 /* Record the maximum and minimum buffer positions
19412 seen so far in glyphs that will be displayed by
19413 this row. */
19414 if (it->bidi_p)
19415 RECORD_MAX_MIN_POS (it);
19416
19417 if (x < it->first_visible_x)
19418 /* Glyph is partially visible, i.e. row starts at
19419 negative X position. */
19420 row->x = x - it->first_visible_x;
19421 }
19422 else
19423 {
19424 /* Glyph is completely off the left margin of the
19425 window. This should not happen because of the
19426 move_it_in_display_line at the start of this
19427 function, unless the text display area of the
19428 window is empty. */
19429 xassert (it->first_visible_x <= it->last_visible_x);
19430 }
19431 }
19432 /* Even if this display element produced no glyphs at all,
19433 we want to record its position. */
19434 if (it->bidi_p && nglyphs == 0)
19435 RECORD_MAX_MIN_POS (it);
19436
19437 row->ascent = max (row->ascent, it->max_ascent);
19438 row->height = max (row->height, it->max_ascent + it->max_descent);
19439 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19440 row->phys_height = max (row->phys_height,
19441 it->max_phys_ascent + it->max_phys_descent);
19442 row->extra_line_spacing = max (row->extra_line_spacing,
19443 it->max_extra_line_spacing);
19444
19445 /* End of this display line if row is continued. */
19446 if (row->continued_p || row->ends_at_zv_p)
19447 break;
19448 }
19449
19450 at_end_of_line:
19451 /* Is this a line end? If yes, we're also done, after making
19452 sure that a non-default face is extended up to the right
19453 margin of the window. */
19454 if (ITERATOR_AT_END_OF_LINE_P (it))
19455 {
19456 int used_before = row->used[TEXT_AREA];
19457
19458 row->ends_in_newline_from_string_p = STRINGP (it->object);
19459
19460 /* Add a space at the end of the line that is used to
19461 display the cursor there. */
19462 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19463 append_space_for_newline (it, 0);
19464
19465 /* Extend the face to the end of the line. */
19466 extend_face_to_end_of_line (it);
19467
19468 /* Make sure we have the position. */
19469 if (used_before == 0)
19470 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19471
19472 /* Record the position of the newline, for use in
19473 find_row_edges. */
19474 it->eol_pos = it->current.pos;
19475
19476 /* Consume the line end. This skips over invisible lines. */
19477 set_iterator_to_next (it, 1);
19478 it->continuation_lines_width = 0;
19479 break;
19480 }
19481
19482 /* Proceed with next display element. Note that this skips
19483 over lines invisible because of selective display. */
19484 set_iterator_to_next (it, 1);
19485
19486 /* If we truncate lines, we are done when the last displayed
19487 glyphs reach past the right margin of the window. */
19488 if (it->line_wrap == TRUNCATE
19489 && (FRAME_WINDOW_P (it->f)
19490 ? (it->current_x >= it->last_visible_x)
19491 : (it->current_x > it->last_visible_x)))
19492 {
19493 /* Maybe add truncation glyphs. */
19494 if (!FRAME_WINDOW_P (it->f))
19495 {
19496 int i, n;
19497
19498 if (!row->reversed_p)
19499 {
19500 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19501 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19502 break;
19503 }
19504 else
19505 {
19506 for (i = 0; i < row->used[TEXT_AREA]; i++)
19507 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19508 break;
19509 /* Remove any padding glyphs at the front of ROW, to
19510 make room for the truncation glyphs we will be
19511 adding below. The loop below always inserts at
19512 least one truncation glyph, so also remove the
19513 last glyph added to ROW. */
19514 unproduce_glyphs (it, i + 1);
19515 /* Adjust i for the loop below. */
19516 i = row->used[TEXT_AREA] - (i + 1);
19517 }
19518
19519 for (n = row->used[TEXT_AREA]; i < n; ++i)
19520 {
19521 row->used[TEXT_AREA] = i;
19522 produce_special_glyphs (it, IT_TRUNCATION);
19523 }
19524 }
19525 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19526 {
19527 /* Don't truncate if we can overflow newline into fringe. */
19528 if (!get_next_display_element (it))
19529 {
19530 it->continuation_lines_width = 0;
19531 row->ends_at_zv_p = 1;
19532 row->exact_window_width_line_p = 1;
19533 break;
19534 }
19535 if (ITERATOR_AT_END_OF_LINE_P (it))
19536 {
19537 row->exact_window_width_line_p = 1;
19538 goto at_end_of_line;
19539 }
19540 }
19541
19542 row->truncated_on_right_p = 1;
19543 it->continuation_lines_width = 0;
19544 reseat_at_next_visible_line_start (it, 0);
19545 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19546 it->hpos = hpos_before;
19547 it->current_x = x_before;
19548 break;
19549 }
19550 }
19551
19552 if (wrap_data)
19553 bidi_unshelve_cache (wrap_data, 1);
19554
19555 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19556 at the left window margin. */
19557 if (it->first_visible_x
19558 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19559 {
19560 if (!FRAME_WINDOW_P (it->f))
19561 insert_left_trunc_glyphs (it);
19562 row->truncated_on_left_p = 1;
19563 }
19564
19565 /* Remember the position at which this line ends.
19566
19567 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19568 cannot be before the call to find_row_edges below, since that is
19569 where these positions are determined. */
19570 row->end = it->current;
19571 if (!it->bidi_p)
19572 {
19573 row->minpos = row->start.pos;
19574 row->maxpos = row->end.pos;
19575 }
19576 else
19577 {
19578 /* ROW->minpos and ROW->maxpos must be the smallest and
19579 `1 + the largest' buffer positions in ROW. But if ROW was
19580 bidi-reordered, these two positions can be anywhere in the
19581 row, so we must determine them now. */
19582 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19583 }
19584
19585 /* If the start of this line is the overlay arrow-position, then
19586 mark this glyph row as the one containing the overlay arrow.
19587 This is clearly a mess with variable size fonts. It would be
19588 better to let it be displayed like cursors under X. */
19589 if ((row->displays_text_p || !overlay_arrow_seen)
19590 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19591 !NILP (overlay_arrow_string)))
19592 {
19593 /* Overlay arrow in window redisplay is a fringe bitmap. */
19594 if (STRINGP (overlay_arrow_string))
19595 {
19596 struct glyph_row *arrow_row
19597 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19598 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19599 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19600 struct glyph *p = row->glyphs[TEXT_AREA];
19601 struct glyph *p2, *end;
19602
19603 /* Copy the arrow glyphs. */
19604 while (glyph < arrow_end)
19605 *p++ = *glyph++;
19606
19607 /* Throw away padding glyphs. */
19608 p2 = p;
19609 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19610 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19611 ++p2;
19612 if (p2 > p)
19613 {
19614 while (p2 < end)
19615 *p++ = *p2++;
19616 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19617 }
19618 }
19619 else
19620 {
19621 xassert (INTEGERP (overlay_arrow_string));
19622 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19623 }
19624 overlay_arrow_seen = 1;
19625 }
19626
19627 /* Highlight trailing whitespace. */
19628 if (!NILP (Vshow_trailing_whitespace))
19629 highlight_trailing_whitespace (it->f, it->glyph_row);
19630
19631 /* Compute pixel dimensions of this line. */
19632 compute_line_metrics (it);
19633
19634 /* Implementation note: No changes in the glyphs of ROW or in their
19635 faces can be done past this point, because compute_line_metrics
19636 computes ROW's hash value and stores it within the glyph_row
19637 structure. */
19638
19639 /* Record whether this row ends inside an ellipsis. */
19640 row->ends_in_ellipsis_p
19641 = (it->method == GET_FROM_DISPLAY_VECTOR
19642 && it->ellipsis_p);
19643
19644 /* Save fringe bitmaps in this row. */
19645 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19646 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19647 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19648 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19649
19650 it->left_user_fringe_bitmap = 0;
19651 it->left_user_fringe_face_id = 0;
19652 it->right_user_fringe_bitmap = 0;
19653 it->right_user_fringe_face_id = 0;
19654
19655 /* Maybe set the cursor. */
19656 cvpos = it->w->cursor.vpos;
19657 if ((cvpos < 0
19658 /* In bidi-reordered rows, keep checking for proper cursor
19659 position even if one has been found already, because buffer
19660 positions in such rows change non-linearly with ROW->VPOS,
19661 when a line is continued. One exception: when we are at ZV,
19662 display cursor on the first suitable glyph row, since all
19663 the empty rows after that also have their position set to ZV. */
19664 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19665 lines' rows is implemented for bidi-reordered rows. */
19666 || (it->bidi_p
19667 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19668 && PT >= MATRIX_ROW_START_CHARPOS (row)
19669 && PT <= MATRIX_ROW_END_CHARPOS (row)
19670 && cursor_row_p (row))
19671 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19672
19673 /* Prepare for the next line. This line starts horizontally at (X
19674 HPOS) = (0 0). Vertical positions are incremented. As a
19675 convenience for the caller, IT->glyph_row is set to the next
19676 row to be used. */
19677 it->current_x = it->hpos = 0;
19678 it->current_y += row->height;
19679 SET_TEXT_POS (it->eol_pos, 0, 0);
19680 ++it->vpos;
19681 ++it->glyph_row;
19682 /* The next row should by default use the same value of the
19683 reversed_p flag as this one. set_iterator_to_next decides when
19684 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19685 the flag accordingly. */
19686 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19687 it->glyph_row->reversed_p = row->reversed_p;
19688 it->start = row->end;
19689 return row->displays_text_p;
19690
19691 #undef RECORD_MAX_MIN_POS
19692 }
19693
19694 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19695 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19696 doc: /* Return paragraph direction at point in BUFFER.
19697 Value is either `left-to-right' or `right-to-left'.
19698 If BUFFER is omitted or nil, it defaults to the current buffer.
19699
19700 Paragraph direction determines how the text in the paragraph is displayed.
19701 In left-to-right paragraphs, text begins at the left margin of the window
19702 and the reading direction is generally left to right. In right-to-left
19703 paragraphs, text begins at the right margin and is read from right to left.
19704
19705 See also `bidi-paragraph-direction'. */)
19706 (Lisp_Object buffer)
19707 {
19708 struct buffer *buf = current_buffer;
19709 struct buffer *old = buf;
19710
19711 if (! NILP (buffer))
19712 {
19713 CHECK_BUFFER (buffer);
19714 buf = XBUFFER (buffer);
19715 }
19716
19717 if (NILP (BVAR (buf, bidi_display_reordering))
19718 || NILP (BVAR (buf, enable_multibyte_characters))
19719 /* When we are loading loadup.el, the character property tables
19720 needed for bidi iteration are not yet available. */
19721 || !NILP (Vpurify_flag))
19722 return Qleft_to_right;
19723 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19724 return BVAR (buf, bidi_paragraph_direction);
19725 else
19726 {
19727 /* Determine the direction from buffer text. We could try to
19728 use current_matrix if it is up to date, but this seems fast
19729 enough as it is. */
19730 struct bidi_it itb;
19731 EMACS_INT pos = BUF_PT (buf);
19732 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19733 int c;
19734 void *itb_data = bidi_shelve_cache ();
19735
19736 set_buffer_temp (buf);
19737 /* bidi_paragraph_init finds the base direction of the paragraph
19738 by searching forward from paragraph start. We need the base
19739 direction of the current or _previous_ paragraph, so we need
19740 to make sure we are within that paragraph. To that end, find
19741 the previous non-empty line. */
19742 if (pos >= ZV && pos > BEGV)
19743 {
19744 pos--;
19745 bytepos = CHAR_TO_BYTE (pos);
19746 }
19747 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19748 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19749 {
19750 while ((c = FETCH_BYTE (bytepos)) == '\n'
19751 || c == ' ' || c == '\t' || c == '\f')
19752 {
19753 if (bytepos <= BEGV_BYTE)
19754 break;
19755 bytepos--;
19756 pos--;
19757 }
19758 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19759 bytepos--;
19760 }
19761 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19762 itb.paragraph_dir = NEUTRAL_DIR;
19763 itb.string.s = NULL;
19764 itb.string.lstring = Qnil;
19765 itb.string.bufpos = 0;
19766 itb.string.unibyte = 0;
19767 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19768 bidi_unshelve_cache (itb_data, 0);
19769 set_buffer_temp (old);
19770 switch (itb.paragraph_dir)
19771 {
19772 case L2R:
19773 return Qleft_to_right;
19774 break;
19775 case R2L:
19776 return Qright_to_left;
19777 break;
19778 default:
19779 abort ();
19780 }
19781 }
19782 }
19783
19784
19785 \f
19786 /***********************************************************************
19787 Menu Bar
19788 ***********************************************************************/
19789
19790 /* Redisplay the menu bar in the frame for window W.
19791
19792 The menu bar of X frames that don't have X toolkit support is
19793 displayed in a special window W->frame->menu_bar_window.
19794
19795 The menu bar of terminal frames is treated specially as far as
19796 glyph matrices are concerned. Menu bar lines are not part of
19797 windows, so the update is done directly on the frame matrix rows
19798 for the menu bar. */
19799
19800 static void
19801 display_menu_bar (struct window *w)
19802 {
19803 struct frame *f = XFRAME (WINDOW_FRAME (w));
19804 struct it it;
19805 Lisp_Object items;
19806 int i;
19807
19808 /* Don't do all this for graphical frames. */
19809 #ifdef HAVE_NTGUI
19810 if (FRAME_W32_P (f))
19811 return;
19812 #endif
19813 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19814 if (FRAME_X_P (f))
19815 return;
19816 #endif
19817
19818 #ifdef HAVE_NS
19819 if (FRAME_NS_P (f))
19820 return;
19821 #endif /* HAVE_NS */
19822
19823 #ifdef USE_X_TOOLKIT
19824 xassert (!FRAME_WINDOW_P (f));
19825 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19826 it.first_visible_x = 0;
19827 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19828 #else /* not USE_X_TOOLKIT */
19829 if (FRAME_WINDOW_P (f))
19830 {
19831 /* Menu bar lines are displayed in the desired matrix of the
19832 dummy window menu_bar_window. */
19833 struct window *menu_w;
19834 xassert (WINDOWP (f->menu_bar_window));
19835 menu_w = XWINDOW (f->menu_bar_window);
19836 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19837 MENU_FACE_ID);
19838 it.first_visible_x = 0;
19839 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19840 }
19841 else
19842 {
19843 /* This is a TTY frame, i.e. character hpos/vpos are used as
19844 pixel x/y. */
19845 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19846 MENU_FACE_ID);
19847 it.first_visible_x = 0;
19848 it.last_visible_x = FRAME_COLS (f);
19849 }
19850 #endif /* not USE_X_TOOLKIT */
19851
19852 /* FIXME: This should be controlled by a user option. See the
19853 comments in redisplay_tool_bar and display_mode_line about
19854 this. */
19855 it.paragraph_embedding = L2R;
19856
19857 if (! mode_line_inverse_video)
19858 /* Force the menu-bar to be displayed in the default face. */
19859 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19860
19861 /* Clear all rows of the menu bar. */
19862 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19863 {
19864 struct glyph_row *row = it.glyph_row + i;
19865 clear_glyph_row (row);
19866 row->enabled_p = 1;
19867 row->full_width_p = 1;
19868 }
19869
19870 /* Display all items of the menu bar. */
19871 items = FRAME_MENU_BAR_ITEMS (it.f);
19872 for (i = 0; i < ASIZE (items); i += 4)
19873 {
19874 Lisp_Object string;
19875
19876 /* Stop at nil string. */
19877 string = AREF (items, i + 1);
19878 if (NILP (string))
19879 break;
19880
19881 /* Remember where item was displayed. */
19882 ASET (items, i + 3, make_number (it.hpos));
19883
19884 /* Display the item, pad with one space. */
19885 if (it.current_x < it.last_visible_x)
19886 display_string (NULL, string, Qnil, 0, 0, &it,
19887 SCHARS (string) + 1, 0, 0, -1);
19888 }
19889
19890 /* Fill out the line with spaces. */
19891 if (it.current_x < it.last_visible_x)
19892 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19893
19894 /* Compute the total height of the lines. */
19895 compute_line_metrics (&it);
19896 }
19897
19898
19899 \f
19900 /***********************************************************************
19901 Mode Line
19902 ***********************************************************************/
19903
19904 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19905 FORCE is non-zero, redisplay mode lines unconditionally.
19906 Otherwise, redisplay only mode lines that are garbaged. Value is
19907 the number of windows whose mode lines were redisplayed. */
19908
19909 static int
19910 redisplay_mode_lines (Lisp_Object window, int force)
19911 {
19912 int nwindows = 0;
19913
19914 while (!NILP (window))
19915 {
19916 struct window *w = XWINDOW (window);
19917
19918 if (WINDOWP (w->hchild))
19919 nwindows += redisplay_mode_lines (w->hchild, force);
19920 else if (WINDOWP (w->vchild))
19921 nwindows += redisplay_mode_lines (w->vchild, force);
19922 else if (force
19923 || FRAME_GARBAGED_P (XFRAME (w->frame))
19924 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19925 {
19926 struct text_pos lpoint;
19927 struct buffer *old = current_buffer;
19928
19929 /* Set the window's buffer for the mode line display. */
19930 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19931 set_buffer_internal_1 (XBUFFER (w->buffer));
19932
19933 /* Point refers normally to the selected window. For any
19934 other window, set up appropriate value. */
19935 if (!EQ (window, selected_window))
19936 {
19937 struct text_pos pt;
19938
19939 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19940 if (CHARPOS (pt) < BEGV)
19941 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19942 else if (CHARPOS (pt) > (ZV - 1))
19943 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19944 else
19945 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19946 }
19947
19948 /* Display mode lines. */
19949 clear_glyph_matrix (w->desired_matrix);
19950 if (display_mode_lines (w))
19951 {
19952 ++nwindows;
19953 w->must_be_updated_p = 1;
19954 }
19955
19956 /* Restore old settings. */
19957 set_buffer_internal_1 (old);
19958 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19959 }
19960
19961 window = w->next;
19962 }
19963
19964 return nwindows;
19965 }
19966
19967
19968 /* Display the mode and/or header line of window W. Value is the
19969 sum number of mode lines and header lines displayed. */
19970
19971 static int
19972 display_mode_lines (struct window *w)
19973 {
19974 Lisp_Object old_selected_window, old_selected_frame;
19975 int n = 0;
19976
19977 old_selected_frame = selected_frame;
19978 selected_frame = w->frame;
19979 old_selected_window = selected_window;
19980 XSETWINDOW (selected_window, w);
19981
19982 /* These will be set while the mode line specs are processed. */
19983 line_number_displayed = 0;
19984 w->column_number_displayed = Qnil;
19985
19986 if (WINDOW_WANTS_MODELINE_P (w))
19987 {
19988 struct window *sel_w = XWINDOW (old_selected_window);
19989
19990 /* Select mode line face based on the real selected window. */
19991 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19992 BVAR (current_buffer, mode_line_format));
19993 ++n;
19994 }
19995
19996 if (WINDOW_WANTS_HEADER_LINE_P (w))
19997 {
19998 display_mode_line (w, HEADER_LINE_FACE_ID,
19999 BVAR (current_buffer, header_line_format));
20000 ++n;
20001 }
20002
20003 selected_frame = old_selected_frame;
20004 selected_window = old_selected_window;
20005 return n;
20006 }
20007
20008
20009 /* Display mode or header line of window W. FACE_ID specifies which
20010 line to display; it is either MODE_LINE_FACE_ID or
20011 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20012 display. Value is the pixel height of the mode/header line
20013 displayed. */
20014
20015 static int
20016 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20017 {
20018 struct it it;
20019 struct face *face;
20020 int count = SPECPDL_INDEX ();
20021
20022 init_iterator (&it, w, -1, -1, NULL, face_id);
20023 /* Don't extend on a previously drawn mode-line.
20024 This may happen if called from pos_visible_p. */
20025 it.glyph_row->enabled_p = 0;
20026 prepare_desired_row (it.glyph_row);
20027
20028 it.glyph_row->mode_line_p = 1;
20029
20030 if (! mode_line_inverse_video)
20031 /* Force the mode-line to be displayed in the default face. */
20032 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20033
20034 /* FIXME: This should be controlled by a user option. But
20035 supporting such an option is not trivial, since the mode line is
20036 made up of many separate strings. */
20037 it.paragraph_embedding = L2R;
20038
20039 record_unwind_protect (unwind_format_mode_line,
20040 format_mode_line_unwind_data (NULL, Qnil, 0));
20041
20042 mode_line_target = MODE_LINE_DISPLAY;
20043
20044 /* Temporarily make frame's keyboard the current kboard so that
20045 kboard-local variables in the mode_line_format will get the right
20046 values. */
20047 push_kboard (FRAME_KBOARD (it.f));
20048 record_unwind_save_match_data ();
20049 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20050 pop_kboard ();
20051
20052 unbind_to (count, Qnil);
20053
20054 /* Fill up with spaces. */
20055 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20056
20057 compute_line_metrics (&it);
20058 it.glyph_row->full_width_p = 1;
20059 it.glyph_row->continued_p = 0;
20060 it.glyph_row->truncated_on_left_p = 0;
20061 it.glyph_row->truncated_on_right_p = 0;
20062
20063 /* Make a 3D mode-line have a shadow at its right end. */
20064 face = FACE_FROM_ID (it.f, face_id);
20065 extend_face_to_end_of_line (&it);
20066 if (face->box != FACE_NO_BOX)
20067 {
20068 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20069 + it.glyph_row->used[TEXT_AREA] - 1);
20070 last->right_box_line_p = 1;
20071 }
20072
20073 return it.glyph_row->height;
20074 }
20075
20076 /* Move element ELT in LIST to the front of LIST.
20077 Return the updated list. */
20078
20079 static Lisp_Object
20080 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20081 {
20082 register Lisp_Object tail, prev;
20083 register Lisp_Object tem;
20084
20085 tail = list;
20086 prev = Qnil;
20087 while (CONSP (tail))
20088 {
20089 tem = XCAR (tail);
20090
20091 if (EQ (elt, tem))
20092 {
20093 /* Splice out the link TAIL. */
20094 if (NILP (prev))
20095 list = XCDR (tail);
20096 else
20097 Fsetcdr (prev, XCDR (tail));
20098
20099 /* Now make it the first. */
20100 Fsetcdr (tail, list);
20101 return tail;
20102 }
20103 else
20104 prev = tail;
20105 tail = XCDR (tail);
20106 QUIT;
20107 }
20108
20109 /* Not found--return unchanged LIST. */
20110 return list;
20111 }
20112
20113 /* Contribute ELT to the mode line for window IT->w. How it
20114 translates into text depends on its data type.
20115
20116 IT describes the display environment in which we display, as usual.
20117
20118 DEPTH is the depth in recursion. It is used to prevent
20119 infinite recursion here.
20120
20121 FIELD_WIDTH is the number of characters the display of ELT should
20122 occupy in the mode line, and PRECISION is the maximum number of
20123 characters to display from ELT's representation. See
20124 display_string for details.
20125
20126 Returns the hpos of the end of the text generated by ELT.
20127
20128 PROPS is a property list to add to any string we encounter.
20129
20130 If RISKY is nonzero, remove (disregard) any properties in any string
20131 we encounter, and ignore :eval and :propertize.
20132
20133 The global variable `mode_line_target' determines whether the
20134 output is passed to `store_mode_line_noprop',
20135 `store_mode_line_string', or `display_string'. */
20136
20137 static int
20138 display_mode_element (struct it *it, int depth, int field_width, int precision,
20139 Lisp_Object elt, Lisp_Object props, int risky)
20140 {
20141 int n = 0, field, prec;
20142 int literal = 0;
20143
20144 tail_recurse:
20145 if (depth > 100)
20146 elt = build_string ("*too-deep*");
20147
20148 depth++;
20149
20150 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20151 {
20152 case Lisp_String:
20153 {
20154 /* A string: output it and check for %-constructs within it. */
20155 unsigned char c;
20156 EMACS_INT offset = 0;
20157
20158 if (SCHARS (elt) > 0
20159 && (!NILP (props) || risky))
20160 {
20161 Lisp_Object oprops, aelt;
20162 oprops = Ftext_properties_at (make_number (0), elt);
20163
20164 /* If the starting string's properties are not what
20165 we want, translate the string. Also, if the string
20166 is risky, do that anyway. */
20167
20168 if (NILP (Fequal (props, oprops)) || risky)
20169 {
20170 /* If the starting string has properties,
20171 merge the specified ones onto the existing ones. */
20172 if (! NILP (oprops) && !risky)
20173 {
20174 Lisp_Object tem;
20175
20176 oprops = Fcopy_sequence (oprops);
20177 tem = props;
20178 while (CONSP (tem))
20179 {
20180 oprops = Fplist_put (oprops, XCAR (tem),
20181 XCAR (XCDR (tem)));
20182 tem = XCDR (XCDR (tem));
20183 }
20184 props = oprops;
20185 }
20186
20187 aelt = Fassoc (elt, mode_line_proptrans_alist);
20188 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20189 {
20190 /* AELT is what we want. Move it to the front
20191 without consing. */
20192 elt = XCAR (aelt);
20193 mode_line_proptrans_alist
20194 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20195 }
20196 else
20197 {
20198 Lisp_Object tem;
20199
20200 /* If AELT has the wrong props, it is useless.
20201 so get rid of it. */
20202 if (! NILP (aelt))
20203 mode_line_proptrans_alist
20204 = Fdelq (aelt, mode_line_proptrans_alist);
20205
20206 elt = Fcopy_sequence (elt);
20207 Fset_text_properties (make_number (0), Flength (elt),
20208 props, elt);
20209 /* Add this item to mode_line_proptrans_alist. */
20210 mode_line_proptrans_alist
20211 = Fcons (Fcons (elt, props),
20212 mode_line_proptrans_alist);
20213 /* Truncate mode_line_proptrans_alist
20214 to at most 50 elements. */
20215 tem = Fnthcdr (make_number (50),
20216 mode_line_proptrans_alist);
20217 if (! NILP (tem))
20218 XSETCDR (tem, Qnil);
20219 }
20220 }
20221 }
20222
20223 offset = 0;
20224
20225 if (literal)
20226 {
20227 prec = precision - n;
20228 switch (mode_line_target)
20229 {
20230 case MODE_LINE_NOPROP:
20231 case MODE_LINE_TITLE:
20232 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20233 break;
20234 case MODE_LINE_STRING:
20235 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20236 break;
20237 case MODE_LINE_DISPLAY:
20238 n += display_string (NULL, elt, Qnil, 0, 0, it,
20239 0, prec, 0, STRING_MULTIBYTE (elt));
20240 break;
20241 }
20242
20243 break;
20244 }
20245
20246 /* Handle the non-literal case. */
20247
20248 while ((precision <= 0 || n < precision)
20249 && SREF (elt, offset) != 0
20250 && (mode_line_target != MODE_LINE_DISPLAY
20251 || it->current_x < it->last_visible_x))
20252 {
20253 EMACS_INT last_offset = offset;
20254
20255 /* Advance to end of string or next format specifier. */
20256 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20257 ;
20258
20259 if (offset - 1 != last_offset)
20260 {
20261 EMACS_INT nchars, nbytes;
20262
20263 /* Output to end of string or up to '%'. Field width
20264 is length of string. Don't output more than
20265 PRECISION allows us. */
20266 offset--;
20267
20268 prec = c_string_width (SDATA (elt) + last_offset,
20269 offset - last_offset, precision - n,
20270 &nchars, &nbytes);
20271
20272 switch (mode_line_target)
20273 {
20274 case MODE_LINE_NOPROP:
20275 case MODE_LINE_TITLE:
20276 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20277 break;
20278 case MODE_LINE_STRING:
20279 {
20280 EMACS_INT bytepos = last_offset;
20281 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20282 EMACS_INT endpos = (precision <= 0
20283 ? string_byte_to_char (elt, offset)
20284 : charpos + nchars);
20285
20286 n += store_mode_line_string (NULL,
20287 Fsubstring (elt, make_number (charpos),
20288 make_number (endpos)),
20289 0, 0, 0, Qnil);
20290 }
20291 break;
20292 case MODE_LINE_DISPLAY:
20293 {
20294 EMACS_INT bytepos = last_offset;
20295 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20296
20297 if (precision <= 0)
20298 nchars = string_byte_to_char (elt, offset) - charpos;
20299 n += display_string (NULL, elt, Qnil, 0, charpos,
20300 it, 0, nchars, 0,
20301 STRING_MULTIBYTE (elt));
20302 }
20303 break;
20304 }
20305 }
20306 else /* c == '%' */
20307 {
20308 EMACS_INT percent_position = offset;
20309
20310 /* Get the specified minimum width. Zero means
20311 don't pad. */
20312 field = 0;
20313 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20314 field = field * 10 + c - '0';
20315
20316 /* Don't pad beyond the total padding allowed. */
20317 if (field_width - n > 0 && field > field_width - n)
20318 field = field_width - n;
20319
20320 /* Note that either PRECISION <= 0 or N < PRECISION. */
20321 prec = precision - n;
20322
20323 if (c == 'M')
20324 n += display_mode_element (it, depth, field, prec,
20325 Vglobal_mode_string, props,
20326 risky);
20327 else if (c != 0)
20328 {
20329 int multibyte;
20330 EMACS_INT bytepos, charpos;
20331 const char *spec;
20332 Lisp_Object string;
20333
20334 bytepos = percent_position;
20335 charpos = (STRING_MULTIBYTE (elt)
20336 ? string_byte_to_char (elt, bytepos)
20337 : bytepos);
20338 spec = decode_mode_spec (it->w, c, field, &string);
20339 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20340
20341 switch (mode_line_target)
20342 {
20343 case MODE_LINE_NOPROP:
20344 case MODE_LINE_TITLE:
20345 n += store_mode_line_noprop (spec, field, prec);
20346 break;
20347 case MODE_LINE_STRING:
20348 {
20349 Lisp_Object tem = build_string (spec);
20350 props = Ftext_properties_at (make_number (charpos), elt);
20351 /* Should only keep face property in props */
20352 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20353 }
20354 break;
20355 case MODE_LINE_DISPLAY:
20356 {
20357 int nglyphs_before, nwritten;
20358
20359 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20360 nwritten = display_string (spec, string, elt,
20361 charpos, 0, it,
20362 field, prec, 0,
20363 multibyte);
20364
20365 /* Assign to the glyphs written above the
20366 string where the `%x' came from, position
20367 of the `%'. */
20368 if (nwritten > 0)
20369 {
20370 struct glyph *glyph
20371 = (it->glyph_row->glyphs[TEXT_AREA]
20372 + nglyphs_before);
20373 int i;
20374
20375 for (i = 0; i < nwritten; ++i)
20376 {
20377 glyph[i].object = elt;
20378 glyph[i].charpos = charpos;
20379 }
20380
20381 n += nwritten;
20382 }
20383 }
20384 break;
20385 }
20386 }
20387 else /* c == 0 */
20388 break;
20389 }
20390 }
20391 }
20392 break;
20393
20394 case Lisp_Symbol:
20395 /* A symbol: process the value of the symbol recursively
20396 as if it appeared here directly. Avoid error if symbol void.
20397 Special case: if value of symbol is a string, output the string
20398 literally. */
20399 {
20400 register Lisp_Object tem;
20401
20402 /* If the variable is not marked as risky to set
20403 then its contents are risky to use. */
20404 if (NILP (Fget (elt, Qrisky_local_variable)))
20405 risky = 1;
20406
20407 tem = Fboundp (elt);
20408 if (!NILP (tem))
20409 {
20410 tem = Fsymbol_value (elt);
20411 /* If value is a string, output that string literally:
20412 don't check for % within it. */
20413 if (STRINGP (tem))
20414 literal = 1;
20415
20416 if (!EQ (tem, elt))
20417 {
20418 /* Give up right away for nil or t. */
20419 elt = tem;
20420 goto tail_recurse;
20421 }
20422 }
20423 }
20424 break;
20425
20426 case Lisp_Cons:
20427 {
20428 register Lisp_Object car, tem;
20429
20430 /* A cons cell: five distinct cases.
20431 If first element is :eval or :propertize, do something special.
20432 If first element is a string or a cons, process all the elements
20433 and effectively concatenate them.
20434 If first element is a negative number, truncate displaying cdr to
20435 at most that many characters. If positive, pad (with spaces)
20436 to at least that many characters.
20437 If first element is a symbol, process the cadr or caddr recursively
20438 according to whether the symbol's value is non-nil or nil. */
20439 car = XCAR (elt);
20440 if (EQ (car, QCeval))
20441 {
20442 /* An element of the form (:eval FORM) means evaluate FORM
20443 and use the result as mode line elements. */
20444
20445 if (risky)
20446 break;
20447
20448 if (CONSP (XCDR (elt)))
20449 {
20450 Lisp_Object spec;
20451 spec = safe_eval (XCAR (XCDR (elt)));
20452 n += display_mode_element (it, depth, field_width - n,
20453 precision - n, spec, props,
20454 risky);
20455 }
20456 }
20457 else if (EQ (car, QCpropertize))
20458 {
20459 /* An element of the form (:propertize ELT PROPS...)
20460 means display ELT but applying properties PROPS. */
20461
20462 if (risky)
20463 break;
20464
20465 if (CONSP (XCDR (elt)))
20466 n += display_mode_element (it, depth, field_width - n,
20467 precision - n, XCAR (XCDR (elt)),
20468 XCDR (XCDR (elt)), risky);
20469 }
20470 else if (SYMBOLP (car))
20471 {
20472 tem = Fboundp (car);
20473 elt = XCDR (elt);
20474 if (!CONSP (elt))
20475 goto invalid;
20476 /* elt is now the cdr, and we know it is a cons cell.
20477 Use its car if CAR has a non-nil value. */
20478 if (!NILP (tem))
20479 {
20480 tem = Fsymbol_value (car);
20481 if (!NILP (tem))
20482 {
20483 elt = XCAR (elt);
20484 goto tail_recurse;
20485 }
20486 }
20487 /* Symbol's value is nil (or symbol is unbound)
20488 Get the cddr of the original list
20489 and if possible find the caddr and use that. */
20490 elt = XCDR (elt);
20491 if (NILP (elt))
20492 break;
20493 else if (!CONSP (elt))
20494 goto invalid;
20495 elt = XCAR (elt);
20496 goto tail_recurse;
20497 }
20498 else if (INTEGERP (car))
20499 {
20500 register int lim = XINT (car);
20501 elt = XCDR (elt);
20502 if (lim < 0)
20503 {
20504 /* Negative int means reduce maximum width. */
20505 if (precision <= 0)
20506 precision = -lim;
20507 else
20508 precision = min (precision, -lim);
20509 }
20510 else if (lim > 0)
20511 {
20512 /* Padding specified. Don't let it be more than
20513 current maximum. */
20514 if (precision > 0)
20515 lim = min (precision, lim);
20516
20517 /* If that's more padding than already wanted, queue it.
20518 But don't reduce padding already specified even if
20519 that is beyond the current truncation point. */
20520 field_width = max (lim, field_width);
20521 }
20522 goto tail_recurse;
20523 }
20524 else if (STRINGP (car) || CONSP (car))
20525 {
20526 Lisp_Object halftail = elt;
20527 int len = 0;
20528
20529 while (CONSP (elt)
20530 && (precision <= 0 || n < precision))
20531 {
20532 n += display_mode_element (it, depth,
20533 /* Do padding only after the last
20534 element in the list. */
20535 (! CONSP (XCDR (elt))
20536 ? field_width - n
20537 : 0),
20538 precision - n, XCAR (elt),
20539 props, risky);
20540 elt = XCDR (elt);
20541 len++;
20542 if ((len & 1) == 0)
20543 halftail = XCDR (halftail);
20544 /* Check for cycle. */
20545 if (EQ (halftail, elt))
20546 break;
20547 }
20548 }
20549 }
20550 break;
20551
20552 default:
20553 invalid:
20554 elt = build_string ("*invalid*");
20555 goto tail_recurse;
20556 }
20557
20558 /* Pad to FIELD_WIDTH. */
20559 if (field_width > 0 && n < field_width)
20560 {
20561 switch (mode_line_target)
20562 {
20563 case MODE_LINE_NOPROP:
20564 case MODE_LINE_TITLE:
20565 n += store_mode_line_noprop ("", field_width - n, 0);
20566 break;
20567 case MODE_LINE_STRING:
20568 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20569 break;
20570 case MODE_LINE_DISPLAY:
20571 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20572 0, 0, 0);
20573 break;
20574 }
20575 }
20576
20577 return n;
20578 }
20579
20580 /* Store a mode-line string element in mode_line_string_list.
20581
20582 If STRING is non-null, display that C string. Otherwise, the Lisp
20583 string LISP_STRING is displayed.
20584
20585 FIELD_WIDTH is the minimum number of output glyphs to produce.
20586 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20587 with spaces. FIELD_WIDTH <= 0 means don't pad.
20588
20589 PRECISION is the maximum number of characters to output from
20590 STRING. PRECISION <= 0 means don't truncate the string.
20591
20592 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20593 properties to the string.
20594
20595 PROPS are the properties to add to the string.
20596 The mode_line_string_face face property is always added to the string.
20597 */
20598
20599 static int
20600 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20601 int field_width, int precision, Lisp_Object props)
20602 {
20603 EMACS_INT len;
20604 int n = 0;
20605
20606 if (string != NULL)
20607 {
20608 len = strlen (string);
20609 if (precision > 0 && len > precision)
20610 len = precision;
20611 lisp_string = make_string (string, len);
20612 if (NILP (props))
20613 props = mode_line_string_face_prop;
20614 else if (!NILP (mode_line_string_face))
20615 {
20616 Lisp_Object face = Fplist_get (props, Qface);
20617 props = Fcopy_sequence (props);
20618 if (NILP (face))
20619 face = mode_line_string_face;
20620 else
20621 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20622 props = Fplist_put (props, Qface, face);
20623 }
20624 Fadd_text_properties (make_number (0), make_number (len),
20625 props, lisp_string);
20626 }
20627 else
20628 {
20629 len = XFASTINT (Flength (lisp_string));
20630 if (precision > 0 && len > precision)
20631 {
20632 len = precision;
20633 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20634 precision = -1;
20635 }
20636 if (!NILP (mode_line_string_face))
20637 {
20638 Lisp_Object face;
20639 if (NILP (props))
20640 props = Ftext_properties_at (make_number (0), lisp_string);
20641 face = Fplist_get (props, Qface);
20642 if (NILP (face))
20643 face = mode_line_string_face;
20644 else
20645 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20646 props = Fcons (Qface, Fcons (face, Qnil));
20647 if (copy_string)
20648 lisp_string = Fcopy_sequence (lisp_string);
20649 }
20650 if (!NILP (props))
20651 Fadd_text_properties (make_number (0), make_number (len),
20652 props, lisp_string);
20653 }
20654
20655 if (len > 0)
20656 {
20657 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20658 n += len;
20659 }
20660
20661 if (field_width > len)
20662 {
20663 field_width -= len;
20664 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20665 if (!NILP (props))
20666 Fadd_text_properties (make_number (0), make_number (field_width),
20667 props, lisp_string);
20668 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20669 n += field_width;
20670 }
20671
20672 return n;
20673 }
20674
20675
20676 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20677 1, 4, 0,
20678 doc: /* Format a string out of a mode line format specification.
20679 First arg FORMAT specifies the mode line format (see `mode-line-format'
20680 for details) to use.
20681
20682 By default, the format is evaluated for the currently selected window.
20683
20684 Optional second arg FACE specifies the face property to put on all
20685 characters for which no face is specified. The value nil means the
20686 default face. The value t means whatever face the window's mode line
20687 currently uses (either `mode-line' or `mode-line-inactive',
20688 depending on whether the window is the selected window or not).
20689 An integer value means the value string has no text
20690 properties.
20691
20692 Optional third and fourth args WINDOW and BUFFER specify the window
20693 and buffer to use as the context for the formatting (defaults
20694 are the selected window and the WINDOW's buffer). */)
20695 (Lisp_Object format, Lisp_Object face,
20696 Lisp_Object window, Lisp_Object buffer)
20697 {
20698 struct it it;
20699 int len;
20700 struct window *w;
20701 struct buffer *old_buffer = NULL;
20702 int face_id;
20703 int no_props = INTEGERP (face);
20704 int count = SPECPDL_INDEX ();
20705 Lisp_Object str;
20706 int string_start = 0;
20707
20708 if (NILP (window))
20709 window = selected_window;
20710 CHECK_WINDOW (window);
20711 w = XWINDOW (window);
20712
20713 if (NILP (buffer))
20714 buffer = w->buffer;
20715 CHECK_BUFFER (buffer);
20716
20717 /* Make formatting the modeline a non-op when noninteractive, otherwise
20718 there will be problems later caused by a partially initialized frame. */
20719 if (NILP (format) || noninteractive)
20720 return empty_unibyte_string;
20721
20722 if (no_props)
20723 face = Qnil;
20724
20725 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20726 : EQ (face, Qt) ? (EQ (window, selected_window)
20727 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20728 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20729 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20730 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20731 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20732 : DEFAULT_FACE_ID;
20733
20734 if (XBUFFER (buffer) != current_buffer)
20735 old_buffer = current_buffer;
20736
20737 /* Save things including mode_line_proptrans_alist,
20738 and set that to nil so that we don't alter the outer value. */
20739 record_unwind_protect (unwind_format_mode_line,
20740 format_mode_line_unwind_data
20741 (old_buffer, selected_window, 1));
20742 mode_line_proptrans_alist = Qnil;
20743
20744 Fselect_window (window, Qt);
20745 if (old_buffer)
20746 set_buffer_internal_1 (XBUFFER (buffer));
20747
20748 init_iterator (&it, w, -1, -1, NULL, face_id);
20749
20750 if (no_props)
20751 {
20752 mode_line_target = MODE_LINE_NOPROP;
20753 mode_line_string_face_prop = Qnil;
20754 mode_line_string_list = Qnil;
20755 string_start = MODE_LINE_NOPROP_LEN (0);
20756 }
20757 else
20758 {
20759 mode_line_target = MODE_LINE_STRING;
20760 mode_line_string_list = Qnil;
20761 mode_line_string_face = face;
20762 mode_line_string_face_prop
20763 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20764 }
20765
20766 push_kboard (FRAME_KBOARD (it.f));
20767 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20768 pop_kboard ();
20769
20770 if (no_props)
20771 {
20772 len = MODE_LINE_NOPROP_LEN (string_start);
20773 str = make_string (mode_line_noprop_buf + string_start, len);
20774 }
20775 else
20776 {
20777 mode_line_string_list = Fnreverse (mode_line_string_list);
20778 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20779 empty_unibyte_string);
20780 }
20781
20782 unbind_to (count, Qnil);
20783 return str;
20784 }
20785
20786 /* Write a null-terminated, right justified decimal representation of
20787 the positive integer D to BUF using a minimal field width WIDTH. */
20788
20789 static void
20790 pint2str (register char *buf, register int width, register EMACS_INT d)
20791 {
20792 register char *p = buf;
20793
20794 if (d <= 0)
20795 *p++ = '0';
20796 else
20797 {
20798 while (d > 0)
20799 {
20800 *p++ = d % 10 + '0';
20801 d /= 10;
20802 }
20803 }
20804
20805 for (width -= (int) (p - buf); width > 0; --width)
20806 *p++ = ' ';
20807 *p-- = '\0';
20808 while (p > buf)
20809 {
20810 d = *buf;
20811 *buf++ = *p;
20812 *p-- = d;
20813 }
20814 }
20815
20816 /* Write a null-terminated, right justified decimal and "human
20817 readable" representation of the nonnegative integer D to BUF using
20818 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20819
20820 static const char power_letter[] =
20821 {
20822 0, /* no letter */
20823 'k', /* kilo */
20824 'M', /* mega */
20825 'G', /* giga */
20826 'T', /* tera */
20827 'P', /* peta */
20828 'E', /* exa */
20829 'Z', /* zetta */
20830 'Y' /* yotta */
20831 };
20832
20833 static void
20834 pint2hrstr (char *buf, int width, EMACS_INT d)
20835 {
20836 /* We aim to represent the nonnegative integer D as
20837 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20838 EMACS_INT quotient = d;
20839 int remainder = 0;
20840 /* -1 means: do not use TENTHS. */
20841 int tenths = -1;
20842 int exponent = 0;
20843
20844 /* Length of QUOTIENT.TENTHS as a string. */
20845 int length;
20846
20847 char * psuffix;
20848 char * p;
20849
20850 if (1000 <= quotient)
20851 {
20852 /* Scale to the appropriate EXPONENT. */
20853 do
20854 {
20855 remainder = quotient % 1000;
20856 quotient /= 1000;
20857 exponent++;
20858 }
20859 while (1000 <= quotient);
20860
20861 /* Round to nearest and decide whether to use TENTHS or not. */
20862 if (quotient <= 9)
20863 {
20864 tenths = remainder / 100;
20865 if (50 <= remainder % 100)
20866 {
20867 if (tenths < 9)
20868 tenths++;
20869 else
20870 {
20871 quotient++;
20872 if (quotient == 10)
20873 tenths = -1;
20874 else
20875 tenths = 0;
20876 }
20877 }
20878 }
20879 else
20880 if (500 <= remainder)
20881 {
20882 if (quotient < 999)
20883 quotient++;
20884 else
20885 {
20886 quotient = 1;
20887 exponent++;
20888 tenths = 0;
20889 }
20890 }
20891 }
20892
20893 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20894 if (tenths == -1 && quotient <= 99)
20895 if (quotient <= 9)
20896 length = 1;
20897 else
20898 length = 2;
20899 else
20900 length = 3;
20901 p = psuffix = buf + max (width, length);
20902
20903 /* Print EXPONENT. */
20904 *psuffix++ = power_letter[exponent];
20905 *psuffix = '\0';
20906
20907 /* Print TENTHS. */
20908 if (tenths >= 0)
20909 {
20910 *--p = '0' + tenths;
20911 *--p = '.';
20912 }
20913
20914 /* Print QUOTIENT. */
20915 do
20916 {
20917 int digit = quotient % 10;
20918 *--p = '0' + digit;
20919 }
20920 while ((quotient /= 10) != 0);
20921
20922 /* Print leading spaces. */
20923 while (buf < p)
20924 *--p = ' ';
20925 }
20926
20927 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20928 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20929 type of CODING_SYSTEM. Return updated pointer into BUF. */
20930
20931 static unsigned char invalid_eol_type[] = "(*invalid*)";
20932
20933 static char *
20934 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20935 {
20936 Lisp_Object val;
20937 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20938 const unsigned char *eol_str;
20939 int eol_str_len;
20940 /* The EOL conversion we are using. */
20941 Lisp_Object eoltype;
20942
20943 val = CODING_SYSTEM_SPEC (coding_system);
20944 eoltype = Qnil;
20945
20946 if (!VECTORP (val)) /* Not yet decided. */
20947 {
20948 if (multibyte)
20949 *buf++ = '-';
20950 if (eol_flag)
20951 eoltype = eol_mnemonic_undecided;
20952 /* Don't mention EOL conversion if it isn't decided. */
20953 }
20954 else
20955 {
20956 Lisp_Object attrs;
20957 Lisp_Object eolvalue;
20958
20959 attrs = AREF (val, 0);
20960 eolvalue = AREF (val, 2);
20961
20962 if (multibyte)
20963 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20964
20965 if (eol_flag)
20966 {
20967 /* The EOL conversion that is normal on this system. */
20968
20969 if (NILP (eolvalue)) /* Not yet decided. */
20970 eoltype = eol_mnemonic_undecided;
20971 else if (VECTORP (eolvalue)) /* Not yet decided. */
20972 eoltype = eol_mnemonic_undecided;
20973 else /* eolvalue is Qunix, Qdos, or Qmac. */
20974 eoltype = (EQ (eolvalue, Qunix)
20975 ? eol_mnemonic_unix
20976 : (EQ (eolvalue, Qdos) == 1
20977 ? eol_mnemonic_dos : eol_mnemonic_mac));
20978 }
20979 }
20980
20981 if (eol_flag)
20982 {
20983 /* Mention the EOL conversion if it is not the usual one. */
20984 if (STRINGP (eoltype))
20985 {
20986 eol_str = SDATA (eoltype);
20987 eol_str_len = SBYTES (eoltype);
20988 }
20989 else if (CHARACTERP (eoltype))
20990 {
20991 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20992 int c = XFASTINT (eoltype);
20993 eol_str_len = CHAR_STRING (c, tmp);
20994 eol_str = tmp;
20995 }
20996 else
20997 {
20998 eol_str = invalid_eol_type;
20999 eol_str_len = sizeof (invalid_eol_type) - 1;
21000 }
21001 memcpy (buf, eol_str, eol_str_len);
21002 buf += eol_str_len;
21003 }
21004
21005 return buf;
21006 }
21007
21008 /* Return a string for the output of a mode line %-spec for window W,
21009 generated by character C. FIELD_WIDTH > 0 means pad the string
21010 returned with spaces to that value. Return a Lisp string in
21011 *STRING if the resulting string is taken from that Lisp string.
21012
21013 Note we operate on the current buffer for most purposes,
21014 the exception being w->base_line_pos. */
21015
21016 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21017
21018 static const char *
21019 decode_mode_spec (struct window *w, register int c, int field_width,
21020 Lisp_Object *string)
21021 {
21022 Lisp_Object obj;
21023 struct frame *f = XFRAME (WINDOW_FRAME (w));
21024 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21025 struct buffer *b = current_buffer;
21026
21027 obj = Qnil;
21028 *string = Qnil;
21029
21030 switch (c)
21031 {
21032 case '*':
21033 if (!NILP (BVAR (b, read_only)))
21034 return "%";
21035 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21036 return "*";
21037 return "-";
21038
21039 case '+':
21040 /* This differs from %* only for a modified read-only buffer. */
21041 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21042 return "*";
21043 if (!NILP (BVAR (b, read_only)))
21044 return "%";
21045 return "-";
21046
21047 case '&':
21048 /* This differs from %* in ignoring read-only-ness. */
21049 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21050 return "*";
21051 return "-";
21052
21053 case '%':
21054 return "%";
21055
21056 case '[':
21057 {
21058 int i;
21059 char *p;
21060
21061 if (command_loop_level > 5)
21062 return "[[[... ";
21063 p = decode_mode_spec_buf;
21064 for (i = 0; i < command_loop_level; i++)
21065 *p++ = '[';
21066 *p = 0;
21067 return decode_mode_spec_buf;
21068 }
21069
21070 case ']':
21071 {
21072 int i;
21073 char *p;
21074
21075 if (command_loop_level > 5)
21076 return " ...]]]";
21077 p = decode_mode_spec_buf;
21078 for (i = 0; i < command_loop_level; i++)
21079 *p++ = ']';
21080 *p = 0;
21081 return decode_mode_spec_buf;
21082 }
21083
21084 case '-':
21085 {
21086 register int i;
21087
21088 /* Let lots_of_dashes be a string of infinite length. */
21089 if (mode_line_target == MODE_LINE_NOPROP ||
21090 mode_line_target == MODE_LINE_STRING)
21091 return "--";
21092 if (field_width <= 0
21093 || field_width > sizeof (lots_of_dashes))
21094 {
21095 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21096 decode_mode_spec_buf[i] = '-';
21097 decode_mode_spec_buf[i] = '\0';
21098 return decode_mode_spec_buf;
21099 }
21100 else
21101 return lots_of_dashes;
21102 }
21103
21104 case 'b':
21105 obj = BVAR (b, name);
21106 break;
21107
21108 case 'c':
21109 /* %c and %l are ignored in `frame-title-format'.
21110 (In redisplay_internal, the frame title is drawn _before_ the
21111 windows are updated, so the stuff which depends on actual
21112 window contents (such as %l) may fail to render properly, or
21113 even crash emacs.) */
21114 if (mode_line_target == MODE_LINE_TITLE)
21115 return "";
21116 else
21117 {
21118 EMACS_INT col = current_column ();
21119 w->column_number_displayed = make_number (col);
21120 pint2str (decode_mode_spec_buf, field_width, col);
21121 return decode_mode_spec_buf;
21122 }
21123
21124 case 'e':
21125 #ifndef SYSTEM_MALLOC
21126 {
21127 if (NILP (Vmemory_full))
21128 return "";
21129 else
21130 return "!MEM FULL! ";
21131 }
21132 #else
21133 return "";
21134 #endif
21135
21136 case 'F':
21137 /* %F displays the frame name. */
21138 if (!NILP (f->title))
21139 return SSDATA (f->title);
21140 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21141 return SSDATA (f->name);
21142 return "Emacs";
21143
21144 case 'f':
21145 obj = BVAR (b, filename);
21146 break;
21147
21148 case 'i':
21149 {
21150 EMACS_INT size = ZV - BEGV;
21151 pint2str (decode_mode_spec_buf, field_width, size);
21152 return decode_mode_spec_buf;
21153 }
21154
21155 case 'I':
21156 {
21157 EMACS_INT size = ZV - BEGV;
21158 pint2hrstr (decode_mode_spec_buf, field_width, size);
21159 return decode_mode_spec_buf;
21160 }
21161
21162 case 'l':
21163 {
21164 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21165 EMACS_INT topline, nlines, height;
21166 EMACS_INT junk;
21167
21168 /* %c and %l are ignored in `frame-title-format'. */
21169 if (mode_line_target == MODE_LINE_TITLE)
21170 return "";
21171
21172 startpos = XMARKER (w->start)->charpos;
21173 startpos_byte = marker_byte_position (w->start);
21174 height = WINDOW_TOTAL_LINES (w);
21175
21176 /* If we decided that this buffer isn't suitable for line numbers,
21177 don't forget that too fast. */
21178 if (EQ (w->base_line_pos, w->buffer))
21179 goto no_value;
21180 /* But do forget it, if the window shows a different buffer now. */
21181 else if (BUFFERP (w->base_line_pos))
21182 w->base_line_pos = Qnil;
21183
21184 /* If the buffer is very big, don't waste time. */
21185 if (INTEGERP (Vline_number_display_limit)
21186 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21187 {
21188 w->base_line_pos = Qnil;
21189 w->base_line_number = Qnil;
21190 goto no_value;
21191 }
21192
21193 if (INTEGERP (w->base_line_number)
21194 && INTEGERP (w->base_line_pos)
21195 && XFASTINT (w->base_line_pos) <= startpos)
21196 {
21197 line = XFASTINT (w->base_line_number);
21198 linepos = XFASTINT (w->base_line_pos);
21199 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21200 }
21201 else
21202 {
21203 line = 1;
21204 linepos = BUF_BEGV (b);
21205 linepos_byte = BUF_BEGV_BYTE (b);
21206 }
21207
21208 /* Count lines from base line to window start position. */
21209 nlines = display_count_lines (linepos_byte,
21210 startpos_byte,
21211 startpos, &junk);
21212
21213 topline = nlines + line;
21214
21215 /* Determine a new base line, if the old one is too close
21216 or too far away, or if we did not have one.
21217 "Too close" means it's plausible a scroll-down would
21218 go back past it. */
21219 if (startpos == BUF_BEGV (b))
21220 {
21221 w->base_line_number = make_number (topline);
21222 w->base_line_pos = make_number (BUF_BEGV (b));
21223 }
21224 else if (nlines < height + 25 || nlines > height * 3 + 50
21225 || linepos == BUF_BEGV (b))
21226 {
21227 EMACS_INT limit = BUF_BEGV (b);
21228 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21229 EMACS_INT position;
21230 EMACS_INT distance =
21231 (height * 2 + 30) * line_number_display_limit_width;
21232
21233 if (startpos - distance > limit)
21234 {
21235 limit = startpos - distance;
21236 limit_byte = CHAR_TO_BYTE (limit);
21237 }
21238
21239 nlines = display_count_lines (startpos_byte,
21240 limit_byte,
21241 - (height * 2 + 30),
21242 &position);
21243 /* If we couldn't find the lines we wanted within
21244 line_number_display_limit_width chars per line,
21245 give up on line numbers for this window. */
21246 if (position == limit_byte && limit == startpos - distance)
21247 {
21248 w->base_line_pos = w->buffer;
21249 w->base_line_number = Qnil;
21250 goto no_value;
21251 }
21252
21253 w->base_line_number = make_number (topline - nlines);
21254 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21255 }
21256
21257 /* Now count lines from the start pos to point. */
21258 nlines = display_count_lines (startpos_byte,
21259 PT_BYTE, PT, &junk);
21260
21261 /* Record that we did display the line number. */
21262 line_number_displayed = 1;
21263
21264 /* Make the string to show. */
21265 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21266 return decode_mode_spec_buf;
21267 no_value:
21268 {
21269 char* p = decode_mode_spec_buf;
21270 int pad = field_width - 2;
21271 while (pad-- > 0)
21272 *p++ = ' ';
21273 *p++ = '?';
21274 *p++ = '?';
21275 *p = '\0';
21276 return decode_mode_spec_buf;
21277 }
21278 }
21279 break;
21280
21281 case 'm':
21282 obj = BVAR (b, mode_name);
21283 break;
21284
21285 case 'n':
21286 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21287 return " Narrow";
21288 break;
21289
21290 case 'p':
21291 {
21292 EMACS_INT pos = marker_position (w->start);
21293 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21294
21295 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21296 {
21297 if (pos <= BUF_BEGV (b))
21298 return "All";
21299 else
21300 return "Bottom";
21301 }
21302 else if (pos <= BUF_BEGV (b))
21303 return "Top";
21304 else
21305 {
21306 if (total > 1000000)
21307 /* Do it differently for a large value, to avoid overflow. */
21308 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21309 else
21310 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21311 /* We can't normally display a 3-digit number,
21312 so get us a 2-digit number that is close. */
21313 if (total == 100)
21314 total = 99;
21315 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21316 return decode_mode_spec_buf;
21317 }
21318 }
21319
21320 /* Display percentage of size above the bottom of the screen. */
21321 case 'P':
21322 {
21323 EMACS_INT toppos = marker_position (w->start);
21324 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21325 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21326
21327 if (botpos >= BUF_ZV (b))
21328 {
21329 if (toppos <= BUF_BEGV (b))
21330 return "All";
21331 else
21332 return "Bottom";
21333 }
21334 else
21335 {
21336 if (total > 1000000)
21337 /* Do it differently for a large value, to avoid overflow. */
21338 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21339 else
21340 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21341 /* We can't normally display a 3-digit number,
21342 so get us a 2-digit number that is close. */
21343 if (total == 100)
21344 total = 99;
21345 if (toppos <= BUF_BEGV (b))
21346 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21347 else
21348 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21349 return decode_mode_spec_buf;
21350 }
21351 }
21352
21353 case 's':
21354 /* status of process */
21355 obj = Fget_buffer_process (Fcurrent_buffer ());
21356 if (NILP (obj))
21357 return "no process";
21358 #ifndef MSDOS
21359 obj = Fsymbol_name (Fprocess_status (obj));
21360 #endif
21361 break;
21362
21363 case '@':
21364 {
21365 int count = inhibit_garbage_collection ();
21366 Lisp_Object val = call1 (intern ("file-remote-p"),
21367 BVAR (current_buffer, directory));
21368 unbind_to (count, Qnil);
21369
21370 if (NILP (val))
21371 return "-";
21372 else
21373 return "@";
21374 }
21375
21376 case 't': /* indicate TEXT or BINARY */
21377 return "T";
21378
21379 case 'z':
21380 /* coding-system (not including end-of-line format) */
21381 case 'Z':
21382 /* coding-system (including end-of-line type) */
21383 {
21384 int eol_flag = (c == 'Z');
21385 char *p = decode_mode_spec_buf;
21386
21387 if (! FRAME_WINDOW_P (f))
21388 {
21389 /* No need to mention EOL here--the terminal never needs
21390 to do EOL conversion. */
21391 p = decode_mode_spec_coding (CODING_ID_NAME
21392 (FRAME_KEYBOARD_CODING (f)->id),
21393 p, 0);
21394 p = decode_mode_spec_coding (CODING_ID_NAME
21395 (FRAME_TERMINAL_CODING (f)->id),
21396 p, 0);
21397 }
21398 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21399 p, eol_flag);
21400
21401 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21402 #ifdef subprocesses
21403 obj = Fget_buffer_process (Fcurrent_buffer ());
21404 if (PROCESSP (obj))
21405 {
21406 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21407 p, eol_flag);
21408 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21409 p, eol_flag);
21410 }
21411 #endif /* subprocesses */
21412 #endif /* 0 */
21413 *p = 0;
21414 return decode_mode_spec_buf;
21415 }
21416 }
21417
21418 if (STRINGP (obj))
21419 {
21420 *string = obj;
21421 return SSDATA (obj);
21422 }
21423 else
21424 return "";
21425 }
21426
21427
21428 /* Count up to COUNT lines starting from START_BYTE.
21429 But don't go beyond LIMIT_BYTE.
21430 Return the number of lines thus found (always nonnegative).
21431
21432 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21433
21434 static EMACS_INT
21435 display_count_lines (EMACS_INT start_byte,
21436 EMACS_INT limit_byte, EMACS_INT count,
21437 EMACS_INT *byte_pos_ptr)
21438 {
21439 register unsigned char *cursor;
21440 unsigned char *base;
21441
21442 register EMACS_INT ceiling;
21443 register unsigned char *ceiling_addr;
21444 EMACS_INT orig_count = count;
21445
21446 /* If we are not in selective display mode,
21447 check only for newlines. */
21448 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21449 && !INTEGERP (BVAR (current_buffer, selective_display)));
21450
21451 if (count > 0)
21452 {
21453 while (start_byte < limit_byte)
21454 {
21455 ceiling = BUFFER_CEILING_OF (start_byte);
21456 ceiling = min (limit_byte - 1, ceiling);
21457 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21458 base = (cursor = BYTE_POS_ADDR (start_byte));
21459 while (1)
21460 {
21461 if (selective_display)
21462 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21463 ;
21464 else
21465 while (*cursor != '\n' && ++cursor != ceiling_addr)
21466 ;
21467
21468 if (cursor != ceiling_addr)
21469 {
21470 if (--count == 0)
21471 {
21472 start_byte += cursor - base + 1;
21473 *byte_pos_ptr = start_byte;
21474 return orig_count;
21475 }
21476 else
21477 if (++cursor == ceiling_addr)
21478 break;
21479 }
21480 else
21481 break;
21482 }
21483 start_byte += cursor - base;
21484 }
21485 }
21486 else
21487 {
21488 while (start_byte > limit_byte)
21489 {
21490 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21491 ceiling = max (limit_byte, ceiling);
21492 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21493 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21494 while (1)
21495 {
21496 if (selective_display)
21497 while (--cursor != ceiling_addr
21498 && *cursor != '\n' && *cursor != 015)
21499 ;
21500 else
21501 while (--cursor != ceiling_addr && *cursor != '\n')
21502 ;
21503
21504 if (cursor != ceiling_addr)
21505 {
21506 if (++count == 0)
21507 {
21508 start_byte += cursor - base + 1;
21509 *byte_pos_ptr = start_byte;
21510 /* When scanning backwards, we should
21511 not count the newline posterior to which we stop. */
21512 return - orig_count - 1;
21513 }
21514 }
21515 else
21516 break;
21517 }
21518 /* Here we add 1 to compensate for the last decrement
21519 of CURSOR, which took it past the valid range. */
21520 start_byte += cursor - base + 1;
21521 }
21522 }
21523
21524 *byte_pos_ptr = limit_byte;
21525
21526 if (count < 0)
21527 return - orig_count + count;
21528 return orig_count - count;
21529
21530 }
21531
21532
21533 \f
21534 /***********************************************************************
21535 Displaying strings
21536 ***********************************************************************/
21537
21538 /* Display a NUL-terminated string, starting with index START.
21539
21540 If STRING is non-null, display that C string. Otherwise, the Lisp
21541 string LISP_STRING is displayed. There's a case that STRING is
21542 non-null and LISP_STRING is not nil. It means STRING is a string
21543 data of LISP_STRING. In that case, we display LISP_STRING while
21544 ignoring its text properties.
21545
21546 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21547 FACE_STRING. Display STRING or LISP_STRING with the face at
21548 FACE_STRING_POS in FACE_STRING:
21549
21550 Display the string in the environment given by IT, but use the
21551 standard display table, temporarily.
21552
21553 FIELD_WIDTH is the minimum number of output glyphs to produce.
21554 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21555 with spaces. If STRING has more characters, more than FIELD_WIDTH
21556 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21557
21558 PRECISION is the maximum number of characters to output from
21559 STRING. PRECISION < 0 means don't truncate the string.
21560
21561 This is roughly equivalent to printf format specifiers:
21562
21563 FIELD_WIDTH PRECISION PRINTF
21564 ----------------------------------------
21565 -1 -1 %s
21566 -1 10 %.10s
21567 10 -1 %10s
21568 20 10 %20.10s
21569
21570 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21571 display them, and < 0 means obey the current buffer's value of
21572 enable_multibyte_characters.
21573
21574 Value is the number of columns displayed. */
21575
21576 static int
21577 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21578 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21579 int field_width, int precision, int max_x, int multibyte)
21580 {
21581 int hpos_at_start = it->hpos;
21582 int saved_face_id = it->face_id;
21583 struct glyph_row *row = it->glyph_row;
21584 EMACS_INT it_charpos;
21585
21586 /* Initialize the iterator IT for iteration over STRING beginning
21587 with index START. */
21588 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21589 precision, field_width, multibyte);
21590 if (string && STRINGP (lisp_string))
21591 /* LISP_STRING is the one returned by decode_mode_spec. We should
21592 ignore its text properties. */
21593 it->stop_charpos = it->end_charpos;
21594
21595 /* If displaying STRING, set up the face of the iterator from
21596 FACE_STRING, if that's given. */
21597 if (STRINGP (face_string))
21598 {
21599 EMACS_INT endptr;
21600 struct face *face;
21601
21602 it->face_id
21603 = face_at_string_position (it->w, face_string, face_string_pos,
21604 0, it->region_beg_charpos,
21605 it->region_end_charpos,
21606 &endptr, it->base_face_id, 0);
21607 face = FACE_FROM_ID (it->f, it->face_id);
21608 it->face_box_p = face->box != FACE_NO_BOX;
21609 }
21610
21611 /* Set max_x to the maximum allowed X position. Don't let it go
21612 beyond the right edge of the window. */
21613 if (max_x <= 0)
21614 max_x = it->last_visible_x;
21615 else
21616 max_x = min (max_x, it->last_visible_x);
21617
21618 /* Skip over display elements that are not visible. because IT->w is
21619 hscrolled. */
21620 if (it->current_x < it->first_visible_x)
21621 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21622 MOVE_TO_POS | MOVE_TO_X);
21623
21624 row->ascent = it->max_ascent;
21625 row->height = it->max_ascent + it->max_descent;
21626 row->phys_ascent = it->max_phys_ascent;
21627 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21628 row->extra_line_spacing = it->max_extra_line_spacing;
21629
21630 if (STRINGP (it->string))
21631 it_charpos = IT_STRING_CHARPOS (*it);
21632 else
21633 it_charpos = IT_CHARPOS (*it);
21634
21635 /* This condition is for the case that we are called with current_x
21636 past last_visible_x. */
21637 while (it->current_x < max_x)
21638 {
21639 int x_before, x, n_glyphs_before, i, nglyphs;
21640
21641 /* Get the next display element. */
21642 if (!get_next_display_element (it))
21643 break;
21644
21645 /* Produce glyphs. */
21646 x_before = it->current_x;
21647 n_glyphs_before = row->used[TEXT_AREA];
21648 PRODUCE_GLYPHS (it);
21649
21650 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21651 i = 0;
21652 x = x_before;
21653 while (i < nglyphs)
21654 {
21655 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21656
21657 if (it->line_wrap != TRUNCATE
21658 && x + glyph->pixel_width > max_x)
21659 {
21660 /* End of continued line or max_x reached. */
21661 if (CHAR_GLYPH_PADDING_P (*glyph))
21662 {
21663 /* A wide character is unbreakable. */
21664 if (row->reversed_p)
21665 unproduce_glyphs (it, row->used[TEXT_AREA]
21666 - n_glyphs_before);
21667 row->used[TEXT_AREA] = n_glyphs_before;
21668 it->current_x = x_before;
21669 }
21670 else
21671 {
21672 if (row->reversed_p)
21673 unproduce_glyphs (it, row->used[TEXT_AREA]
21674 - (n_glyphs_before + i));
21675 row->used[TEXT_AREA] = n_glyphs_before + i;
21676 it->current_x = x;
21677 }
21678 break;
21679 }
21680 else if (x + glyph->pixel_width >= it->first_visible_x)
21681 {
21682 /* Glyph is at least partially visible. */
21683 ++it->hpos;
21684 if (x < it->first_visible_x)
21685 row->x = x - it->first_visible_x;
21686 }
21687 else
21688 {
21689 /* Glyph is off the left margin of the display area.
21690 Should not happen. */
21691 abort ();
21692 }
21693
21694 row->ascent = max (row->ascent, it->max_ascent);
21695 row->height = max (row->height, it->max_ascent + it->max_descent);
21696 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21697 row->phys_height = max (row->phys_height,
21698 it->max_phys_ascent + it->max_phys_descent);
21699 row->extra_line_spacing = max (row->extra_line_spacing,
21700 it->max_extra_line_spacing);
21701 x += glyph->pixel_width;
21702 ++i;
21703 }
21704
21705 /* Stop if max_x reached. */
21706 if (i < nglyphs)
21707 break;
21708
21709 /* Stop at line ends. */
21710 if (ITERATOR_AT_END_OF_LINE_P (it))
21711 {
21712 it->continuation_lines_width = 0;
21713 break;
21714 }
21715
21716 set_iterator_to_next (it, 1);
21717 if (STRINGP (it->string))
21718 it_charpos = IT_STRING_CHARPOS (*it);
21719 else
21720 it_charpos = IT_CHARPOS (*it);
21721
21722 /* Stop if truncating at the right edge. */
21723 if (it->line_wrap == TRUNCATE
21724 && it->current_x >= it->last_visible_x)
21725 {
21726 /* Add truncation mark, but don't do it if the line is
21727 truncated at a padding space. */
21728 if (it_charpos < it->string_nchars)
21729 {
21730 if (!FRAME_WINDOW_P (it->f))
21731 {
21732 int ii, n;
21733
21734 if (it->current_x > it->last_visible_x)
21735 {
21736 if (!row->reversed_p)
21737 {
21738 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21739 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21740 break;
21741 }
21742 else
21743 {
21744 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21745 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21746 break;
21747 unproduce_glyphs (it, ii + 1);
21748 ii = row->used[TEXT_AREA] - (ii + 1);
21749 }
21750 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21751 {
21752 row->used[TEXT_AREA] = ii;
21753 produce_special_glyphs (it, IT_TRUNCATION);
21754 }
21755 }
21756 produce_special_glyphs (it, IT_TRUNCATION);
21757 }
21758 row->truncated_on_right_p = 1;
21759 }
21760 break;
21761 }
21762 }
21763
21764 /* Maybe insert a truncation at the left. */
21765 if (it->first_visible_x
21766 && it_charpos > 0)
21767 {
21768 if (!FRAME_WINDOW_P (it->f))
21769 insert_left_trunc_glyphs (it);
21770 row->truncated_on_left_p = 1;
21771 }
21772
21773 it->face_id = saved_face_id;
21774
21775 /* Value is number of columns displayed. */
21776 return it->hpos - hpos_at_start;
21777 }
21778
21779
21780 \f
21781 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21782 appears as an element of LIST or as the car of an element of LIST.
21783 If PROPVAL is a list, compare each element against LIST in that
21784 way, and return 1/2 if any element of PROPVAL is found in LIST.
21785 Otherwise return 0. This function cannot quit.
21786 The return value is 2 if the text is invisible but with an ellipsis
21787 and 1 if it's invisible and without an ellipsis. */
21788
21789 int
21790 invisible_p (register Lisp_Object propval, Lisp_Object list)
21791 {
21792 register Lisp_Object tail, proptail;
21793
21794 for (tail = list; CONSP (tail); tail = XCDR (tail))
21795 {
21796 register Lisp_Object tem;
21797 tem = XCAR (tail);
21798 if (EQ (propval, tem))
21799 return 1;
21800 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21801 return NILP (XCDR (tem)) ? 1 : 2;
21802 }
21803
21804 if (CONSP (propval))
21805 {
21806 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21807 {
21808 Lisp_Object propelt;
21809 propelt = XCAR (proptail);
21810 for (tail = list; CONSP (tail); tail = XCDR (tail))
21811 {
21812 register Lisp_Object tem;
21813 tem = XCAR (tail);
21814 if (EQ (propelt, tem))
21815 return 1;
21816 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21817 return NILP (XCDR (tem)) ? 1 : 2;
21818 }
21819 }
21820 }
21821
21822 return 0;
21823 }
21824
21825 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21826 doc: /* Non-nil if the property makes the text invisible.
21827 POS-OR-PROP can be a marker or number, in which case it is taken to be
21828 a position in the current buffer and the value of the `invisible' property
21829 is checked; or it can be some other value, which is then presumed to be the
21830 value of the `invisible' property of the text of interest.
21831 The non-nil value returned can be t for truly invisible text or something
21832 else if the text is replaced by an ellipsis. */)
21833 (Lisp_Object pos_or_prop)
21834 {
21835 Lisp_Object prop
21836 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21837 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21838 : pos_or_prop);
21839 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21840 return (invis == 0 ? Qnil
21841 : invis == 1 ? Qt
21842 : make_number (invis));
21843 }
21844
21845 /* Calculate a width or height in pixels from a specification using
21846 the following elements:
21847
21848 SPEC ::=
21849 NUM - a (fractional) multiple of the default font width/height
21850 (NUM) - specifies exactly NUM pixels
21851 UNIT - a fixed number of pixels, see below.
21852 ELEMENT - size of a display element in pixels, see below.
21853 (NUM . SPEC) - equals NUM * SPEC
21854 (+ SPEC SPEC ...) - add pixel values
21855 (- SPEC SPEC ...) - subtract pixel values
21856 (- SPEC) - negate pixel value
21857
21858 NUM ::=
21859 INT or FLOAT - a number constant
21860 SYMBOL - use symbol's (buffer local) variable binding.
21861
21862 UNIT ::=
21863 in - pixels per inch *)
21864 mm - pixels per 1/1000 meter *)
21865 cm - pixels per 1/100 meter *)
21866 width - width of current font in pixels.
21867 height - height of current font in pixels.
21868
21869 *) using the ratio(s) defined in display-pixels-per-inch.
21870
21871 ELEMENT ::=
21872
21873 left-fringe - left fringe width in pixels
21874 right-fringe - right fringe width in pixels
21875
21876 left-margin - left margin width in pixels
21877 right-margin - right margin width in pixels
21878
21879 scroll-bar - scroll-bar area width in pixels
21880
21881 Examples:
21882
21883 Pixels corresponding to 5 inches:
21884 (5 . in)
21885
21886 Total width of non-text areas on left side of window (if scroll-bar is on left):
21887 '(space :width (+ left-fringe left-margin scroll-bar))
21888
21889 Align to first text column (in header line):
21890 '(space :align-to 0)
21891
21892 Align to middle of text area minus half the width of variable `my-image'
21893 containing a loaded image:
21894 '(space :align-to (0.5 . (- text my-image)))
21895
21896 Width of left margin minus width of 1 character in the default font:
21897 '(space :width (- left-margin 1))
21898
21899 Width of left margin minus width of 2 characters in the current font:
21900 '(space :width (- left-margin (2 . width)))
21901
21902 Center 1 character over left-margin (in header line):
21903 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21904
21905 Different ways to express width of left fringe plus left margin minus one pixel:
21906 '(space :width (- (+ left-fringe left-margin) (1)))
21907 '(space :width (+ left-fringe left-margin (- (1))))
21908 '(space :width (+ left-fringe left-margin (-1)))
21909
21910 */
21911
21912 #define NUMVAL(X) \
21913 ((INTEGERP (X) || FLOATP (X)) \
21914 ? XFLOATINT (X) \
21915 : - 1)
21916
21917 static int
21918 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21919 struct font *font, int width_p, int *align_to)
21920 {
21921 double pixels;
21922
21923 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21924 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21925
21926 if (NILP (prop))
21927 return OK_PIXELS (0);
21928
21929 xassert (FRAME_LIVE_P (it->f));
21930
21931 if (SYMBOLP (prop))
21932 {
21933 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21934 {
21935 char *unit = SSDATA (SYMBOL_NAME (prop));
21936
21937 if (unit[0] == 'i' && unit[1] == 'n')
21938 pixels = 1.0;
21939 else if (unit[0] == 'm' && unit[1] == 'm')
21940 pixels = 25.4;
21941 else if (unit[0] == 'c' && unit[1] == 'm')
21942 pixels = 2.54;
21943 else
21944 pixels = 0;
21945 if (pixels > 0)
21946 {
21947 double ppi;
21948 #ifdef HAVE_WINDOW_SYSTEM
21949 if (FRAME_WINDOW_P (it->f)
21950 && (ppi = (width_p
21951 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21952 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21953 ppi > 0))
21954 return OK_PIXELS (ppi / pixels);
21955 #endif
21956
21957 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21958 || (CONSP (Vdisplay_pixels_per_inch)
21959 && (ppi = (width_p
21960 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21961 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21962 ppi > 0)))
21963 return OK_PIXELS (ppi / pixels);
21964
21965 return 0;
21966 }
21967 }
21968
21969 #ifdef HAVE_WINDOW_SYSTEM
21970 if (EQ (prop, Qheight))
21971 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21972 if (EQ (prop, Qwidth))
21973 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21974 #else
21975 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21976 return OK_PIXELS (1);
21977 #endif
21978
21979 if (EQ (prop, Qtext))
21980 return OK_PIXELS (width_p
21981 ? window_box_width (it->w, TEXT_AREA)
21982 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21983
21984 if (align_to && *align_to < 0)
21985 {
21986 *res = 0;
21987 if (EQ (prop, Qleft))
21988 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21989 if (EQ (prop, Qright))
21990 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21991 if (EQ (prop, Qcenter))
21992 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21993 + window_box_width (it->w, TEXT_AREA) / 2);
21994 if (EQ (prop, Qleft_fringe))
21995 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21996 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21997 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21998 if (EQ (prop, Qright_fringe))
21999 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22000 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22001 : window_box_right_offset (it->w, TEXT_AREA));
22002 if (EQ (prop, Qleft_margin))
22003 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22004 if (EQ (prop, Qright_margin))
22005 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22006 if (EQ (prop, Qscroll_bar))
22007 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22008 ? 0
22009 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22010 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22011 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22012 : 0)));
22013 }
22014 else
22015 {
22016 if (EQ (prop, Qleft_fringe))
22017 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22018 if (EQ (prop, Qright_fringe))
22019 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22020 if (EQ (prop, Qleft_margin))
22021 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22022 if (EQ (prop, Qright_margin))
22023 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22024 if (EQ (prop, Qscroll_bar))
22025 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22026 }
22027
22028 prop = Fbuffer_local_value (prop, it->w->buffer);
22029 }
22030
22031 if (INTEGERP (prop) || FLOATP (prop))
22032 {
22033 int base_unit = (width_p
22034 ? FRAME_COLUMN_WIDTH (it->f)
22035 : FRAME_LINE_HEIGHT (it->f));
22036 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22037 }
22038
22039 if (CONSP (prop))
22040 {
22041 Lisp_Object car = XCAR (prop);
22042 Lisp_Object cdr = XCDR (prop);
22043
22044 if (SYMBOLP (car))
22045 {
22046 #ifdef HAVE_WINDOW_SYSTEM
22047 if (FRAME_WINDOW_P (it->f)
22048 && valid_image_p (prop))
22049 {
22050 ptrdiff_t id = lookup_image (it->f, prop);
22051 struct image *img = IMAGE_FROM_ID (it->f, id);
22052
22053 return OK_PIXELS (width_p ? img->width : img->height);
22054 }
22055 #endif
22056 if (EQ (car, Qplus) || EQ (car, Qminus))
22057 {
22058 int first = 1;
22059 double px;
22060
22061 pixels = 0;
22062 while (CONSP (cdr))
22063 {
22064 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22065 font, width_p, align_to))
22066 return 0;
22067 if (first)
22068 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22069 else
22070 pixels += px;
22071 cdr = XCDR (cdr);
22072 }
22073 if (EQ (car, Qminus))
22074 pixels = -pixels;
22075 return OK_PIXELS (pixels);
22076 }
22077
22078 car = Fbuffer_local_value (car, it->w->buffer);
22079 }
22080
22081 if (INTEGERP (car) || FLOATP (car))
22082 {
22083 double fact;
22084 pixels = XFLOATINT (car);
22085 if (NILP (cdr))
22086 return OK_PIXELS (pixels);
22087 if (calc_pixel_width_or_height (&fact, it, cdr,
22088 font, width_p, align_to))
22089 return OK_PIXELS (pixels * fact);
22090 return 0;
22091 }
22092
22093 return 0;
22094 }
22095
22096 return 0;
22097 }
22098
22099 \f
22100 /***********************************************************************
22101 Glyph Display
22102 ***********************************************************************/
22103
22104 #ifdef HAVE_WINDOW_SYSTEM
22105
22106 #if GLYPH_DEBUG
22107
22108 void
22109 dump_glyph_string (struct glyph_string *s)
22110 {
22111 fprintf (stderr, "glyph string\n");
22112 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22113 s->x, s->y, s->width, s->height);
22114 fprintf (stderr, " ybase = %d\n", s->ybase);
22115 fprintf (stderr, " hl = %d\n", s->hl);
22116 fprintf (stderr, " left overhang = %d, right = %d\n",
22117 s->left_overhang, s->right_overhang);
22118 fprintf (stderr, " nchars = %d\n", s->nchars);
22119 fprintf (stderr, " extends to end of line = %d\n",
22120 s->extends_to_end_of_line_p);
22121 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22122 fprintf (stderr, " bg width = %d\n", s->background_width);
22123 }
22124
22125 #endif /* GLYPH_DEBUG */
22126
22127 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22128 of XChar2b structures for S; it can't be allocated in
22129 init_glyph_string because it must be allocated via `alloca'. W
22130 is the window on which S is drawn. ROW and AREA are the glyph row
22131 and area within the row from which S is constructed. START is the
22132 index of the first glyph structure covered by S. HL is a
22133 face-override for drawing S. */
22134
22135 #ifdef HAVE_NTGUI
22136 #define OPTIONAL_HDC(hdc) HDC hdc,
22137 #define DECLARE_HDC(hdc) HDC hdc;
22138 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22139 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22140 #endif
22141
22142 #ifndef OPTIONAL_HDC
22143 #define OPTIONAL_HDC(hdc)
22144 #define DECLARE_HDC(hdc)
22145 #define ALLOCATE_HDC(hdc, f)
22146 #define RELEASE_HDC(hdc, f)
22147 #endif
22148
22149 static void
22150 init_glyph_string (struct glyph_string *s,
22151 OPTIONAL_HDC (hdc)
22152 XChar2b *char2b, struct window *w, struct glyph_row *row,
22153 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22154 {
22155 memset (s, 0, sizeof *s);
22156 s->w = w;
22157 s->f = XFRAME (w->frame);
22158 #ifdef HAVE_NTGUI
22159 s->hdc = hdc;
22160 #endif
22161 s->display = FRAME_X_DISPLAY (s->f);
22162 s->window = FRAME_X_WINDOW (s->f);
22163 s->char2b = char2b;
22164 s->hl = hl;
22165 s->row = row;
22166 s->area = area;
22167 s->first_glyph = row->glyphs[area] + start;
22168 s->height = row->height;
22169 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22170 s->ybase = s->y + row->ascent;
22171 }
22172
22173
22174 /* Append the list of glyph strings with head H and tail T to the list
22175 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22176
22177 static inline void
22178 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22179 struct glyph_string *h, struct glyph_string *t)
22180 {
22181 if (h)
22182 {
22183 if (*head)
22184 (*tail)->next = h;
22185 else
22186 *head = h;
22187 h->prev = *tail;
22188 *tail = t;
22189 }
22190 }
22191
22192
22193 /* Prepend the list of glyph strings with head H and tail T to the
22194 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22195 result. */
22196
22197 static inline void
22198 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22199 struct glyph_string *h, struct glyph_string *t)
22200 {
22201 if (h)
22202 {
22203 if (*head)
22204 (*head)->prev = t;
22205 else
22206 *tail = t;
22207 t->next = *head;
22208 *head = h;
22209 }
22210 }
22211
22212
22213 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22214 Set *HEAD and *TAIL to the resulting list. */
22215
22216 static inline void
22217 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22218 struct glyph_string *s)
22219 {
22220 s->next = s->prev = NULL;
22221 append_glyph_string_lists (head, tail, s, s);
22222 }
22223
22224
22225 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22226 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22227 make sure that X resources for the face returned are allocated.
22228 Value is a pointer to a realized face that is ready for display if
22229 DISPLAY_P is non-zero. */
22230
22231 static inline struct face *
22232 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22233 XChar2b *char2b, int display_p)
22234 {
22235 struct face *face = FACE_FROM_ID (f, face_id);
22236
22237 if (face->font)
22238 {
22239 unsigned code = face->font->driver->encode_char (face->font, c);
22240
22241 if (code != FONT_INVALID_CODE)
22242 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22243 else
22244 STORE_XCHAR2B (char2b, 0, 0);
22245 }
22246
22247 /* Make sure X resources of the face are allocated. */
22248 #ifdef HAVE_X_WINDOWS
22249 if (display_p)
22250 #endif
22251 {
22252 xassert (face != NULL);
22253 PREPARE_FACE_FOR_DISPLAY (f, face);
22254 }
22255
22256 return face;
22257 }
22258
22259
22260 /* Get face and two-byte form of character glyph GLYPH on frame F.
22261 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22262 a pointer to a realized face that is ready for display. */
22263
22264 static inline struct face *
22265 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22266 XChar2b *char2b, int *two_byte_p)
22267 {
22268 struct face *face;
22269
22270 xassert (glyph->type == CHAR_GLYPH);
22271 face = FACE_FROM_ID (f, glyph->face_id);
22272
22273 if (two_byte_p)
22274 *two_byte_p = 0;
22275
22276 if (face->font)
22277 {
22278 unsigned code;
22279
22280 if (CHAR_BYTE8_P (glyph->u.ch))
22281 code = CHAR_TO_BYTE8 (glyph->u.ch);
22282 else
22283 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22284
22285 if (code != FONT_INVALID_CODE)
22286 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22287 else
22288 STORE_XCHAR2B (char2b, 0, 0);
22289 }
22290
22291 /* Make sure X resources of the face are allocated. */
22292 xassert (face != NULL);
22293 PREPARE_FACE_FOR_DISPLAY (f, face);
22294 return face;
22295 }
22296
22297
22298 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22299 Return 1 if FONT has a glyph for C, otherwise return 0. */
22300
22301 static inline int
22302 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22303 {
22304 unsigned code;
22305
22306 if (CHAR_BYTE8_P (c))
22307 code = CHAR_TO_BYTE8 (c);
22308 else
22309 code = font->driver->encode_char (font, c);
22310
22311 if (code == FONT_INVALID_CODE)
22312 return 0;
22313 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22314 return 1;
22315 }
22316
22317
22318 /* Fill glyph string S with composition components specified by S->cmp.
22319
22320 BASE_FACE is the base face of the composition.
22321 S->cmp_from is the index of the first component for S.
22322
22323 OVERLAPS non-zero means S should draw the foreground only, and use
22324 its physical height for clipping. See also draw_glyphs.
22325
22326 Value is the index of a component not in S. */
22327
22328 static int
22329 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22330 int overlaps)
22331 {
22332 int i;
22333 /* For all glyphs of this composition, starting at the offset
22334 S->cmp_from, until we reach the end of the definition or encounter a
22335 glyph that requires the different face, add it to S. */
22336 struct face *face;
22337
22338 xassert (s);
22339
22340 s->for_overlaps = overlaps;
22341 s->face = NULL;
22342 s->font = NULL;
22343 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22344 {
22345 int c = COMPOSITION_GLYPH (s->cmp, i);
22346
22347 /* TAB in a composition means display glyphs with padding space
22348 on the left or right. */
22349 if (c != '\t')
22350 {
22351 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22352 -1, Qnil);
22353
22354 face = get_char_face_and_encoding (s->f, c, face_id,
22355 s->char2b + i, 1);
22356 if (face)
22357 {
22358 if (! s->face)
22359 {
22360 s->face = face;
22361 s->font = s->face->font;
22362 }
22363 else if (s->face != face)
22364 break;
22365 }
22366 }
22367 ++s->nchars;
22368 }
22369 s->cmp_to = i;
22370
22371 if (s->face == NULL)
22372 {
22373 s->face = base_face->ascii_face;
22374 s->font = s->face->font;
22375 }
22376
22377 /* All glyph strings for the same composition has the same width,
22378 i.e. the width set for the first component of the composition. */
22379 s->width = s->first_glyph->pixel_width;
22380
22381 /* If the specified font could not be loaded, use the frame's
22382 default font, but record the fact that we couldn't load it in
22383 the glyph string so that we can draw rectangles for the
22384 characters of the glyph string. */
22385 if (s->font == NULL)
22386 {
22387 s->font_not_found_p = 1;
22388 s->font = FRAME_FONT (s->f);
22389 }
22390
22391 /* Adjust base line for subscript/superscript text. */
22392 s->ybase += s->first_glyph->voffset;
22393
22394 /* This glyph string must always be drawn with 16-bit functions. */
22395 s->two_byte_p = 1;
22396
22397 return s->cmp_to;
22398 }
22399
22400 static int
22401 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22402 int start, int end, int overlaps)
22403 {
22404 struct glyph *glyph, *last;
22405 Lisp_Object lgstring;
22406 int i;
22407
22408 s->for_overlaps = overlaps;
22409 glyph = s->row->glyphs[s->area] + start;
22410 last = s->row->glyphs[s->area] + end;
22411 s->cmp_id = glyph->u.cmp.id;
22412 s->cmp_from = glyph->slice.cmp.from;
22413 s->cmp_to = glyph->slice.cmp.to + 1;
22414 s->face = FACE_FROM_ID (s->f, face_id);
22415 lgstring = composition_gstring_from_id (s->cmp_id);
22416 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22417 glyph++;
22418 while (glyph < last
22419 && glyph->u.cmp.automatic
22420 && glyph->u.cmp.id == s->cmp_id
22421 && s->cmp_to == glyph->slice.cmp.from)
22422 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22423
22424 for (i = s->cmp_from; i < s->cmp_to; i++)
22425 {
22426 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22427 unsigned code = LGLYPH_CODE (lglyph);
22428
22429 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22430 }
22431 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22432 return glyph - s->row->glyphs[s->area];
22433 }
22434
22435
22436 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22437 See the comment of fill_glyph_string for arguments.
22438 Value is the index of the first glyph not in S. */
22439
22440
22441 static int
22442 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22443 int start, int end, int overlaps)
22444 {
22445 struct glyph *glyph, *last;
22446 int voffset;
22447
22448 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22449 s->for_overlaps = overlaps;
22450 glyph = s->row->glyphs[s->area] + start;
22451 last = s->row->glyphs[s->area] + end;
22452 voffset = glyph->voffset;
22453 s->face = FACE_FROM_ID (s->f, face_id);
22454 s->font = s->face->font;
22455 s->nchars = 1;
22456 s->width = glyph->pixel_width;
22457 glyph++;
22458 while (glyph < last
22459 && glyph->type == GLYPHLESS_GLYPH
22460 && glyph->voffset == voffset
22461 && glyph->face_id == face_id)
22462 {
22463 s->nchars++;
22464 s->width += glyph->pixel_width;
22465 glyph++;
22466 }
22467 s->ybase += voffset;
22468 return glyph - s->row->glyphs[s->area];
22469 }
22470
22471
22472 /* Fill glyph string S from a sequence of character glyphs.
22473
22474 FACE_ID is the face id of the string. START is the index of the
22475 first glyph to consider, END is the index of the last + 1.
22476 OVERLAPS non-zero means S should draw the foreground only, and use
22477 its physical height for clipping. See also draw_glyphs.
22478
22479 Value is the index of the first glyph not in S. */
22480
22481 static int
22482 fill_glyph_string (struct glyph_string *s, int face_id,
22483 int start, int end, int overlaps)
22484 {
22485 struct glyph *glyph, *last;
22486 int voffset;
22487 int glyph_not_available_p;
22488
22489 xassert (s->f == XFRAME (s->w->frame));
22490 xassert (s->nchars == 0);
22491 xassert (start >= 0 && end > start);
22492
22493 s->for_overlaps = overlaps;
22494 glyph = s->row->glyphs[s->area] + start;
22495 last = s->row->glyphs[s->area] + end;
22496 voffset = glyph->voffset;
22497 s->padding_p = glyph->padding_p;
22498 glyph_not_available_p = glyph->glyph_not_available_p;
22499
22500 while (glyph < last
22501 && glyph->type == CHAR_GLYPH
22502 && glyph->voffset == voffset
22503 /* Same face id implies same font, nowadays. */
22504 && glyph->face_id == face_id
22505 && glyph->glyph_not_available_p == glyph_not_available_p)
22506 {
22507 int two_byte_p;
22508
22509 s->face = get_glyph_face_and_encoding (s->f, glyph,
22510 s->char2b + s->nchars,
22511 &two_byte_p);
22512 s->two_byte_p = two_byte_p;
22513 ++s->nchars;
22514 xassert (s->nchars <= end - start);
22515 s->width += glyph->pixel_width;
22516 if (glyph++->padding_p != s->padding_p)
22517 break;
22518 }
22519
22520 s->font = s->face->font;
22521
22522 /* If the specified font could not be loaded, use the frame's font,
22523 but record the fact that we couldn't load it in
22524 S->font_not_found_p so that we can draw rectangles for the
22525 characters of the glyph string. */
22526 if (s->font == NULL || glyph_not_available_p)
22527 {
22528 s->font_not_found_p = 1;
22529 s->font = FRAME_FONT (s->f);
22530 }
22531
22532 /* Adjust base line for subscript/superscript text. */
22533 s->ybase += voffset;
22534
22535 xassert (s->face && s->face->gc);
22536 return glyph - s->row->glyphs[s->area];
22537 }
22538
22539
22540 /* Fill glyph string S from image glyph S->first_glyph. */
22541
22542 static void
22543 fill_image_glyph_string (struct glyph_string *s)
22544 {
22545 xassert (s->first_glyph->type == IMAGE_GLYPH);
22546 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22547 xassert (s->img);
22548 s->slice = s->first_glyph->slice.img;
22549 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22550 s->font = s->face->font;
22551 s->width = s->first_glyph->pixel_width;
22552
22553 /* Adjust base line for subscript/superscript text. */
22554 s->ybase += s->first_glyph->voffset;
22555 }
22556
22557
22558 /* Fill glyph string S from a sequence of stretch glyphs.
22559
22560 START is the index of the first glyph to consider,
22561 END is the index of the last + 1.
22562
22563 Value is the index of the first glyph not in S. */
22564
22565 static int
22566 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22567 {
22568 struct glyph *glyph, *last;
22569 int voffset, face_id;
22570
22571 xassert (s->first_glyph->type == STRETCH_GLYPH);
22572
22573 glyph = s->row->glyphs[s->area] + start;
22574 last = s->row->glyphs[s->area] + end;
22575 face_id = glyph->face_id;
22576 s->face = FACE_FROM_ID (s->f, face_id);
22577 s->font = s->face->font;
22578 s->width = glyph->pixel_width;
22579 s->nchars = 1;
22580 voffset = glyph->voffset;
22581
22582 for (++glyph;
22583 (glyph < last
22584 && glyph->type == STRETCH_GLYPH
22585 && glyph->voffset == voffset
22586 && glyph->face_id == face_id);
22587 ++glyph)
22588 s->width += glyph->pixel_width;
22589
22590 /* Adjust base line for subscript/superscript text. */
22591 s->ybase += voffset;
22592
22593 /* The case that face->gc == 0 is handled when drawing the glyph
22594 string by calling PREPARE_FACE_FOR_DISPLAY. */
22595 xassert (s->face);
22596 return glyph - s->row->glyphs[s->area];
22597 }
22598
22599 static struct font_metrics *
22600 get_per_char_metric (struct font *font, XChar2b *char2b)
22601 {
22602 static struct font_metrics metrics;
22603 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22604
22605 if (! font || code == FONT_INVALID_CODE)
22606 return NULL;
22607 font->driver->text_extents (font, &code, 1, &metrics);
22608 return &metrics;
22609 }
22610
22611 /* EXPORT for RIF:
22612 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22613 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22614 assumed to be zero. */
22615
22616 void
22617 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22618 {
22619 *left = *right = 0;
22620
22621 if (glyph->type == CHAR_GLYPH)
22622 {
22623 struct face *face;
22624 XChar2b char2b;
22625 struct font_metrics *pcm;
22626
22627 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22628 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22629 {
22630 if (pcm->rbearing > pcm->width)
22631 *right = pcm->rbearing - pcm->width;
22632 if (pcm->lbearing < 0)
22633 *left = -pcm->lbearing;
22634 }
22635 }
22636 else if (glyph->type == COMPOSITE_GLYPH)
22637 {
22638 if (! glyph->u.cmp.automatic)
22639 {
22640 struct composition *cmp = composition_table[glyph->u.cmp.id];
22641
22642 if (cmp->rbearing > cmp->pixel_width)
22643 *right = cmp->rbearing - cmp->pixel_width;
22644 if (cmp->lbearing < 0)
22645 *left = - cmp->lbearing;
22646 }
22647 else
22648 {
22649 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22650 struct font_metrics metrics;
22651
22652 composition_gstring_width (gstring, glyph->slice.cmp.from,
22653 glyph->slice.cmp.to + 1, &metrics);
22654 if (metrics.rbearing > metrics.width)
22655 *right = metrics.rbearing - metrics.width;
22656 if (metrics.lbearing < 0)
22657 *left = - metrics.lbearing;
22658 }
22659 }
22660 }
22661
22662
22663 /* Return the index of the first glyph preceding glyph string S that
22664 is overwritten by S because of S's left overhang. Value is -1
22665 if no glyphs are overwritten. */
22666
22667 static int
22668 left_overwritten (struct glyph_string *s)
22669 {
22670 int k;
22671
22672 if (s->left_overhang)
22673 {
22674 int x = 0, i;
22675 struct glyph *glyphs = s->row->glyphs[s->area];
22676 int first = s->first_glyph - glyphs;
22677
22678 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22679 x -= glyphs[i].pixel_width;
22680
22681 k = i + 1;
22682 }
22683 else
22684 k = -1;
22685
22686 return k;
22687 }
22688
22689
22690 /* Return the index of the first glyph preceding glyph string S that
22691 is overwriting S because of its right overhang. Value is -1 if no
22692 glyph in front of S overwrites S. */
22693
22694 static int
22695 left_overwriting (struct glyph_string *s)
22696 {
22697 int i, k, x;
22698 struct glyph *glyphs = s->row->glyphs[s->area];
22699 int first = s->first_glyph - glyphs;
22700
22701 k = -1;
22702 x = 0;
22703 for (i = first - 1; i >= 0; --i)
22704 {
22705 int left, right;
22706 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22707 if (x + right > 0)
22708 k = i;
22709 x -= glyphs[i].pixel_width;
22710 }
22711
22712 return k;
22713 }
22714
22715
22716 /* Return the index of the last glyph following glyph string S that is
22717 overwritten by S because of S's right overhang. Value is -1 if
22718 no such glyph is found. */
22719
22720 static int
22721 right_overwritten (struct glyph_string *s)
22722 {
22723 int k = -1;
22724
22725 if (s->right_overhang)
22726 {
22727 int x = 0, i;
22728 struct glyph *glyphs = s->row->glyphs[s->area];
22729 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22730 int end = s->row->used[s->area];
22731
22732 for (i = first; i < end && s->right_overhang > x; ++i)
22733 x += glyphs[i].pixel_width;
22734
22735 k = i;
22736 }
22737
22738 return k;
22739 }
22740
22741
22742 /* Return the index of the last glyph following glyph string S that
22743 overwrites S because of its left overhang. Value is negative
22744 if no such glyph is found. */
22745
22746 static int
22747 right_overwriting (struct glyph_string *s)
22748 {
22749 int i, k, x;
22750 int end = s->row->used[s->area];
22751 struct glyph *glyphs = s->row->glyphs[s->area];
22752 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22753
22754 k = -1;
22755 x = 0;
22756 for (i = first; i < end; ++i)
22757 {
22758 int left, right;
22759 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22760 if (x - left < 0)
22761 k = i;
22762 x += glyphs[i].pixel_width;
22763 }
22764
22765 return k;
22766 }
22767
22768
22769 /* Set background width of glyph string S. START is the index of the
22770 first glyph following S. LAST_X is the right-most x-position + 1
22771 in the drawing area. */
22772
22773 static inline void
22774 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22775 {
22776 /* If the face of this glyph string has to be drawn to the end of
22777 the drawing area, set S->extends_to_end_of_line_p. */
22778
22779 if (start == s->row->used[s->area]
22780 && s->area == TEXT_AREA
22781 && ((s->row->fill_line_p
22782 && (s->hl == DRAW_NORMAL_TEXT
22783 || s->hl == DRAW_IMAGE_RAISED
22784 || s->hl == DRAW_IMAGE_SUNKEN))
22785 || s->hl == DRAW_MOUSE_FACE))
22786 s->extends_to_end_of_line_p = 1;
22787
22788 /* If S extends its face to the end of the line, set its
22789 background_width to the distance to the right edge of the drawing
22790 area. */
22791 if (s->extends_to_end_of_line_p)
22792 s->background_width = last_x - s->x + 1;
22793 else
22794 s->background_width = s->width;
22795 }
22796
22797
22798 /* Compute overhangs and x-positions for glyph string S and its
22799 predecessors, or successors. X is the starting x-position for S.
22800 BACKWARD_P non-zero means process predecessors. */
22801
22802 static void
22803 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22804 {
22805 if (backward_p)
22806 {
22807 while (s)
22808 {
22809 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22810 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22811 x -= s->width;
22812 s->x = x;
22813 s = s->prev;
22814 }
22815 }
22816 else
22817 {
22818 while (s)
22819 {
22820 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22821 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22822 s->x = x;
22823 x += s->width;
22824 s = s->next;
22825 }
22826 }
22827 }
22828
22829
22830
22831 /* The following macros are only called from draw_glyphs below.
22832 They reference the following parameters of that function directly:
22833 `w', `row', `area', and `overlap_p'
22834 as well as the following local variables:
22835 `s', `f', and `hdc' (in W32) */
22836
22837 #ifdef HAVE_NTGUI
22838 /* On W32, silently add local `hdc' variable to argument list of
22839 init_glyph_string. */
22840 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22841 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22842 #else
22843 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22844 init_glyph_string (s, char2b, w, row, area, start, hl)
22845 #endif
22846
22847 /* Add a glyph string for a stretch glyph to the list of strings
22848 between HEAD and TAIL. START is the index of the stretch glyph in
22849 row area AREA of glyph row ROW. END is the index of the last glyph
22850 in that glyph row area. X is the current output position assigned
22851 to the new glyph string constructed. HL overrides that face of the
22852 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22853 is the right-most x-position of the drawing area. */
22854
22855 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22856 and below -- keep them on one line. */
22857 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22858 do \
22859 { \
22860 s = (struct glyph_string *) alloca (sizeof *s); \
22861 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22862 START = fill_stretch_glyph_string (s, START, END); \
22863 append_glyph_string (&HEAD, &TAIL, s); \
22864 s->x = (X); \
22865 } \
22866 while (0)
22867
22868
22869 /* Add a glyph string for an image glyph to the list of strings
22870 between HEAD and TAIL. START is the index of the image glyph in
22871 row area AREA of glyph row ROW. END is the index of the last glyph
22872 in that glyph row area. X is the current output position assigned
22873 to the new glyph string constructed. HL overrides that face of the
22874 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22875 is the right-most x-position of the drawing area. */
22876
22877 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22878 do \
22879 { \
22880 s = (struct glyph_string *) alloca (sizeof *s); \
22881 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22882 fill_image_glyph_string (s); \
22883 append_glyph_string (&HEAD, &TAIL, s); \
22884 ++START; \
22885 s->x = (X); \
22886 } \
22887 while (0)
22888
22889
22890 /* Add a glyph string for a sequence of character glyphs to the list
22891 of strings between HEAD and TAIL. START is the index of the first
22892 glyph in row area AREA of glyph row ROW that is part of the new
22893 glyph string. END is the index of the last glyph in that glyph row
22894 area. X is the current output position assigned to the new glyph
22895 string constructed. HL overrides that face of the glyph; e.g. it
22896 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22897 right-most x-position of the drawing area. */
22898
22899 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22900 do \
22901 { \
22902 int face_id; \
22903 XChar2b *char2b; \
22904 \
22905 face_id = (row)->glyphs[area][START].face_id; \
22906 \
22907 s = (struct glyph_string *) alloca (sizeof *s); \
22908 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22909 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22910 append_glyph_string (&HEAD, &TAIL, s); \
22911 s->x = (X); \
22912 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22913 } \
22914 while (0)
22915
22916
22917 /* Add a glyph string for a composite sequence to the list of strings
22918 between HEAD and TAIL. START is the index of the first glyph in
22919 row area AREA of glyph row ROW that is part of the new glyph
22920 string. END is the index of the last glyph in that glyph row area.
22921 X is the current output position assigned to the new glyph string
22922 constructed. HL overrides that face of the glyph; e.g. it is
22923 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22924 x-position of the drawing area. */
22925
22926 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22927 do { \
22928 int face_id = (row)->glyphs[area][START].face_id; \
22929 struct face *base_face = FACE_FROM_ID (f, face_id); \
22930 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22931 struct composition *cmp = composition_table[cmp_id]; \
22932 XChar2b *char2b; \
22933 struct glyph_string *first_s = NULL; \
22934 int n; \
22935 \
22936 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22937 \
22938 /* Make glyph_strings for each glyph sequence that is drawable by \
22939 the same face, and append them to HEAD/TAIL. */ \
22940 for (n = 0; n < cmp->glyph_len;) \
22941 { \
22942 s = (struct glyph_string *) alloca (sizeof *s); \
22943 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22944 append_glyph_string (&(HEAD), &(TAIL), s); \
22945 s->cmp = cmp; \
22946 s->cmp_from = n; \
22947 s->x = (X); \
22948 if (n == 0) \
22949 first_s = s; \
22950 n = fill_composite_glyph_string (s, base_face, overlaps); \
22951 } \
22952 \
22953 ++START; \
22954 s = first_s; \
22955 } while (0)
22956
22957
22958 /* Add a glyph string for a glyph-string sequence to the list of strings
22959 between HEAD and TAIL. */
22960
22961 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22962 do { \
22963 int face_id; \
22964 XChar2b *char2b; \
22965 Lisp_Object gstring; \
22966 \
22967 face_id = (row)->glyphs[area][START].face_id; \
22968 gstring = (composition_gstring_from_id \
22969 ((row)->glyphs[area][START].u.cmp.id)); \
22970 s = (struct glyph_string *) alloca (sizeof *s); \
22971 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22972 * LGSTRING_GLYPH_LEN (gstring)); \
22973 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22974 append_glyph_string (&(HEAD), &(TAIL), s); \
22975 s->x = (X); \
22976 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22977 } while (0)
22978
22979
22980 /* Add a glyph string for a sequence of glyphless character's glyphs
22981 to the list of strings between HEAD and TAIL. The meanings of
22982 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22983
22984 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22985 do \
22986 { \
22987 int face_id; \
22988 \
22989 face_id = (row)->glyphs[area][START].face_id; \
22990 \
22991 s = (struct glyph_string *) alloca (sizeof *s); \
22992 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22993 append_glyph_string (&HEAD, &TAIL, s); \
22994 s->x = (X); \
22995 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22996 overlaps); \
22997 } \
22998 while (0)
22999
23000
23001 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23002 of AREA of glyph row ROW on window W between indices START and END.
23003 HL overrides the face for drawing glyph strings, e.g. it is
23004 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23005 x-positions of the drawing area.
23006
23007 This is an ugly monster macro construct because we must use alloca
23008 to allocate glyph strings (because draw_glyphs can be called
23009 asynchronously). */
23010
23011 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23012 do \
23013 { \
23014 HEAD = TAIL = NULL; \
23015 while (START < END) \
23016 { \
23017 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23018 switch (first_glyph->type) \
23019 { \
23020 case CHAR_GLYPH: \
23021 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23022 HL, X, LAST_X); \
23023 break; \
23024 \
23025 case COMPOSITE_GLYPH: \
23026 if (first_glyph->u.cmp.automatic) \
23027 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23028 HL, X, LAST_X); \
23029 else \
23030 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23031 HL, X, LAST_X); \
23032 break; \
23033 \
23034 case STRETCH_GLYPH: \
23035 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23036 HL, X, LAST_X); \
23037 break; \
23038 \
23039 case IMAGE_GLYPH: \
23040 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23041 HL, X, LAST_X); \
23042 break; \
23043 \
23044 case GLYPHLESS_GLYPH: \
23045 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23046 HL, X, LAST_X); \
23047 break; \
23048 \
23049 default: \
23050 abort (); \
23051 } \
23052 \
23053 if (s) \
23054 { \
23055 set_glyph_string_background_width (s, START, LAST_X); \
23056 (X) += s->width; \
23057 } \
23058 } \
23059 } while (0)
23060
23061
23062 /* Draw glyphs between START and END in AREA of ROW on window W,
23063 starting at x-position X. X is relative to AREA in W. HL is a
23064 face-override with the following meaning:
23065
23066 DRAW_NORMAL_TEXT draw normally
23067 DRAW_CURSOR draw in cursor face
23068 DRAW_MOUSE_FACE draw in mouse face.
23069 DRAW_INVERSE_VIDEO draw in mode line face
23070 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23071 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23072
23073 If OVERLAPS is non-zero, draw only the foreground of characters and
23074 clip to the physical height of ROW. Non-zero value also defines
23075 the overlapping part to be drawn:
23076
23077 OVERLAPS_PRED overlap with preceding rows
23078 OVERLAPS_SUCC overlap with succeeding rows
23079 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23080 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23081
23082 Value is the x-position reached, relative to AREA of W. */
23083
23084 static int
23085 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23086 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23087 enum draw_glyphs_face hl, int overlaps)
23088 {
23089 struct glyph_string *head, *tail;
23090 struct glyph_string *s;
23091 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23092 int i, j, x_reached, last_x, area_left = 0;
23093 struct frame *f = XFRAME (WINDOW_FRAME (w));
23094 DECLARE_HDC (hdc);
23095
23096 ALLOCATE_HDC (hdc, f);
23097
23098 /* Let's rather be paranoid than getting a SEGV. */
23099 end = min (end, row->used[area]);
23100 start = max (0, start);
23101 start = min (end, start);
23102
23103 /* Translate X to frame coordinates. Set last_x to the right
23104 end of the drawing area. */
23105 if (row->full_width_p)
23106 {
23107 /* X is relative to the left edge of W, without scroll bars
23108 or fringes. */
23109 area_left = WINDOW_LEFT_EDGE_X (w);
23110 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23111 }
23112 else
23113 {
23114 area_left = window_box_left (w, area);
23115 last_x = area_left + window_box_width (w, area);
23116 }
23117 x += area_left;
23118
23119 /* Build a doubly-linked list of glyph_string structures between
23120 head and tail from what we have to draw. Note that the macro
23121 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23122 the reason we use a separate variable `i'. */
23123 i = start;
23124 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23125 if (tail)
23126 x_reached = tail->x + tail->background_width;
23127 else
23128 x_reached = x;
23129
23130 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23131 the row, redraw some glyphs in front or following the glyph
23132 strings built above. */
23133 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23134 {
23135 struct glyph_string *h, *t;
23136 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23137 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23138 int check_mouse_face = 0;
23139 int dummy_x = 0;
23140
23141 /* If mouse highlighting is on, we may need to draw adjacent
23142 glyphs using mouse-face highlighting. */
23143 if (area == TEXT_AREA && row->mouse_face_p)
23144 {
23145 struct glyph_row *mouse_beg_row, *mouse_end_row;
23146
23147 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23148 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23149
23150 if (row >= mouse_beg_row && row <= mouse_end_row)
23151 {
23152 check_mouse_face = 1;
23153 mouse_beg_col = (row == mouse_beg_row)
23154 ? hlinfo->mouse_face_beg_col : 0;
23155 mouse_end_col = (row == mouse_end_row)
23156 ? hlinfo->mouse_face_end_col
23157 : row->used[TEXT_AREA];
23158 }
23159 }
23160
23161 /* Compute overhangs for all glyph strings. */
23162 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23163 for (s = head; s; s = s->next)
23164 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23165
23166 /* Prepend glyph strings for glyphs in front of the first glyph
23167 string that are overwritten because of the first glyph
23168 string's left overhang. The background of all strings
23169 prepended must be drawn because the first glyph string
23170 draws over it. */
23171 i = left_overwritten (head);
23172 if (i >= 0)
23173 {
23174 enum draw_glyphs_face overlap_hl;
23175
23176 /* If this row contains mouse highlighting, attempt to draw
23177 the overlapped glyphs with the correct highlight. This
23178 code fails if the overlap encompasses more than one glyph
23179 and mouse-highlight spans only some of these glyphs.
23180 However, making it work perfectly involves a lot more
23181 code, and I don't know if the pathological case occurs in
23182 practice, so we'll stick to this for now. --- cyd */
23183 if (check_mouse_face
23184 && mouse_beg_col < start && mouse_end_col > i)
23185 overlap_hl = DRAW_MOUSE_FACE;
23186 else
23187 overlap_hl = DRAW_NORMAL_TEXT;
23188
23189 j = i;
23190 BUILD_GLYPH_STRINGS (j, start, h, t,
23191 overlap_hl, dummy_x, last_x);
23192 start = i;
23193 compute_overhangs_and_x (t, head->x, 1);
23194 prepend_glyph_string_lists (&head, &tail, h, t);
23195 clip_head = head;
23196 }
23197
23198 /* Prepend glyph strings for glyphs in front of the first glyph
23199 string that overwrite that glyph string because of their
23200 right overhang. For these strings, only the foreground must
23201 be drawn, because it draws over the glyph string at `head'.
23202 The background must not be drawn because this would overwrite
23203 right overhangs of preceding glyphs for which no glyph
23204 strings exist. */
23205 i = left_overwriting (head);
23206 if (i >= 0)
23207 {
23208 enum draw_glyphs_face overlap_hl;
23209
23210 if (check_mouse_face
23211 && mouse_beg_col < start && mouse_end_col > i)
23212 overlap_hl = DRAW_MOUSE_FACE;
23213 else
23214 overlap_hl = DRAW_NORMAL_TEXT;
23215
23216 clip_head = head;
23217 BUILD_GLYPH_STRINGS (i, start, h, t,
23218 overlap_hl, dummy_x, last_x);
23219 for (s = h; s; s = s->next)
23220 s->background_filled_p = 1;
23221 compute_overhangs_and_x (t, head->x, 1);
23222 prepend_glyph_string_lists (&head, &tail, h, t);
23223 }
23224
23225 /* Append glyphs strings for glyphs following the last glyph
23226 string tail that are overwritten by tail. The background of
23227 these strings has to be drawn because tail's foreground draws
23228 over it. */
23229 i = right_overwritten (tail);
23230 if (i >= 0)
23231 {
23232 enum draw_glyphs_face overlap_hl;
23233
23234 if (check_mouse_face
23235 && mouse_beg_col < i && mouse_end_col > end)
23236 overlap_hl = DRAW_MOUSE_FACE;
23237 else
23238 overlap_hl = DRAW_NORMAL_TEXT;
23239
23240 BUILD_GLYPH_STRINGS (end, i, h, t,
23241 overlap_hl, x, last_x);
23242 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23243 we don't have `end = i;' here. */
23244 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23245 append_glyph_string_lists (&head, &tail, h, t);
23246 clip_tail = tail;
23247 }
23248
23249 /* Append glyph strings for glyphs following the last glyph
23250 string tail that overwrite tail. The foreground of such
23251 glyphs has to be drawn because it writes into the background
23252 of tail. The background must not be drawn because it could
23253 paint over the foreground of following glyphs. */
23254 i = right_overwriting (tail);
23255 if (i >= 0)
23256 {
23257 enum draw_glyphs_face overlap_hl;
23258 if (check_mouse_face
23259 && mouse_beg_col < i && mouse_end_col > end)
23260 overlap_hl = DRAW_MOUSE_FACE;
23261 else
23262 overlap_hl = DRAW_NORMAL_TEXT;
23263
23264 clip_tail = tail;
23265 i++; /* We must include the Ith glyph. */
23266 BUILD_GLYPH_STRINGS (end, i, h, t,
23267 overlap_hl, x, last_x);
23268 for (s = h; s; s = s->next)
23269 s->background_filled_p = 1;
23270 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23271 append_glyph_string_lists (&head, &tail, h, t);
23272 }
23273 if (clip_head || clip_tail)
23274 for (s = head; s; s = s->next)
23275 {
23276 s->clip_head = clip_head;
23277 s->clip_tail = clip_tail;
23278 }
23279 }
23280
23281 /* Draw all strings. */
23282 for (s = head; s; s = s->next)
23283 FRAME_RIF (f)->draw_glyph_string (s);
23284
23285 #ifndef HAVE_NS
23286 /* When focus a sole frame and move horizontally, this sets on_p to 0
23287 causing a failure to erase prev cursor position. */
23288 if (area == TEXT_AREA
23289 && !row->full_width_p
23290 /* When drawing overlapping rows, only the glyph strings'
23291 foreground is drawn, which doesn't erase a cursor
23292 completely. */
23293 && !overlaps)
23294 {
23295 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23296 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23297 : (tail ? tail->x + tail->background_width : x));
23298 x0 -= area_left;
23299 x1 -= area_left;
23300
23301 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23302 row->y, MATRIX_ROW_BOTTOM_Y (row));
23303 }
23304 #endif
23305
23306 /* Value is the x-position up to which drawn, relative to AREA of W.
23307 This doesn't include parts drawn because of overhangs. */
23308 if (row->full_width_p)
23309 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23310 else
23311 x_reached -= area_left;
23312
23313 RELEASE_HDC (hdc, f);
23314
23315 return x_reached;
23316 }
23317
23318 /* Expand row matrix if too narrow. Don't expand if area
23319 is not present. */
23320
23321 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23322 { \
23323 if (!fonts_changed_p \
23324 && (it->glyph_row->glyphs[area] \
23325 < it->glyph_row->glyphs[area + 1])) \
23326 { \
23327 it->w->ncols_scale_factor++; \
23328 fonts_changed_p = 1; \
23329 } \
23330 }
23331
23332 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23333 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23334
23335 static inline void
23336 append_glyph (struct it *it)
23337 {
23338 struct glyph *glyph;
23339 enum glyph_row_area area = it->area;
23340
23341 xassert (it->glyph_row);
23342 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23343
23344 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23345 if (glyph < it->glyph_row->glyphs[area + 1])
23346 {
23347 /* If the glyph row is reversed, we need to prepend the glyph
23348 rather than append it. */
23349 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23350 {
23351 struct glyph *g;
23352
23353 /* Make room for the additional glyph. */
23354 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23355 g[1] = *g;
23356 glyph = it->glyph_row->glyphs[area];
23357 }
23358 glyph->charpos = CHARPOS (it->position);
23359 glyph->object = it->object;
23360 if (it->pixel_width > 0)
23361 {
23362 glyph->pixel_width = it->pixel_width;
23363 glyph->padding_p = 0;
23364 }
23365 else
23366 {
23367 /* Assure at least 1-pixel width. Otherwise, cursor can't
23368 be displayed correctly. */
23369 glyph->pixel_width = 1;
23370 glyph->padding_p = 1;
23371 }
23372 glyph->ascent = it->ascent;
23373 glyph->descent = it->descent;
23374 glyph->voffset = it->voffset;
23375 glyph->type = CHAR_GLYPH;
23376 glyph->avoid_cursor_p = it->avoid_cursor_p;
23377 glyph->multibyte_p = it->multibyte_p;
23378 glyph->left_box_line_p = it->start_of_box_run_p;
23379 glyph->right_box_line_p = it->end_of_box_run_p;
23380 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23381 || it->phys_descent > it->descent);
23382 glyph->glyph_not_available_p = it->glyph_not_available_p;
23383 glyph->face_id = it->face_id;
23384 glyph->u.ch = it->char_to_display;
23385 glyph->slice.img = null_glyph_slice;
23386 glyph->font_type = FONT_TYPE_UNKNOWN;
23387 if (it->bidi_p)
23388 {
23389 glyph->resolved_level = it->bidi_it.resolved_level;
23390 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23391 abort ();
23392 glyph->bidi_type = it->bidi_it.type;
23393 }
23394 else
23395 {
23396 glyph->resolved_level = 0;
23397 glyph->bidi_type = UNKNOWN_BT;
23398 }
23399 ++it->glyph_row->used[area];
23400 }
23401 else
23402 IT_EXPAND_MATRIX_WIDTH (it, area);
23403 }
23404
23405 /* Store one glyph for the composition IT->cmp_it.id in
23406 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23407 non-null. */
23408
23409 static inline void
23410 append_composite_glyph (struct it *it)
23411 {
23412 struct glyph *glyph;
23413 enum glyph_row_area area = it->area;
23414
23415 xassert (it->glyph_row);
23416
23417 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23418 if (glyph < it->glyph_row->glyphs[area + 1])
23419 {
23420 /* If the glyph row is reversed, we need to prepend the glyph
23421 rather than append it. */
23422 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23423 {
23424 struct glyph *g;
23425
23426 /* Make room for the new glyph. */
23427 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23428 g[1] = *g;
23429 glyph = it->glyph_row->glyphs[it->area];
23430 }
23431 glyph->charpos = it->cmp_it.charpos;
23432 glyph->object = it->object;
23433 glyph->pixel_width = it->pixel_width;
23434 glyph->ascent = it->ascent;
23435 glyph->descent = it->descent;
23436 glyph->voffset = it->voffset;
23437 glyph->type = COMPOSITE_GLYPH;
23438 if (it->cmp_it.ch < 0)
23439 {
23440 glyph->u.cmp.automatic = 0;
23441 glyph->u.cmp.id = it->cmp_it.id;
23442 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23443 }
23444 else
23445 {
23446 glyph->u.cmp.automatic = 1;
23447 glyph->u.cmp.id = it->cmp_it.id;
23448 glyph->slice.cmp.from = it->cmp_it.from;
23449 glyph->slice.cmp.to = it->cmp_it.to - 1;
23450 }
23451 glyph->avoid_cursor_p = it->avoid_cursor_p;
23452 glyph->multibyte_p = it->multibyte_p;
23453 glyph->left_box_line_p = it->start_of_box_run_p;
23454 glyph->right_box_line_p = it->end_of_box_run_p;
23455 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23456 || it->phys_descent > it->descent);
23457 glyph->padding_p = 0;
23458 glyph->glyph_not_available_p = 0;
23459 glyph->face_id = it->face_id;
23460 glyph->font_type = FONT_TYPE_UNKNOWN;
23461 if (it->bidi_p)
23462 {
23463 glyph->resolved_level = it->bidi_it.resolved_level;
23464 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23465 abort ();
23466 glyph->bidi_type = it->bidi_it.type;
23467 }
23468 ++it->glyph_row->used[area];
23469 }
23470 else
23471 IT_EXPAND_MATRIX_WIDTH (it, area);
23472 }
23473
23474
23475 /* Change IT->ascent and IT->height according to the setting of
23476 IT->voffset. */
23477
23478 static inline void
23479 take_vertical_position_into_account (struct it *it)
23480 {
23481 if (it->voffset)
23482 {
23483 if (it->voffset < 0)
23484 /* Increase the ascent so that we can display the text higher
23485 in the line. */
23486 it->ascent -= it->voffset;
23487 else
23488 /* Increase the descent so that we can display the text lower
23489 in the line. */
23490 it->descent += it->voffset;
23491 }
23492 }
23493
23494
23495 /* Produce glyphs/get display metrics for the image IT is loaded with.
23496 See the description of struct display_iterator in dispextern.h for
23497 an overview of struct display_iterator. */
23498
23499 static void
23500 produce_image_glyph (struct it *it)
23501 {
23502 struct image *img;
23503 struct face *face;
23504 int glyph_ascent, crop;
23505 struct glyph_slice slice;
23506
23507 xassert (it->what == IT_IMAGE);
23508
23509 face = FACE_FROM_ID (it->f, it->face_id);
23510 xassert (face);
23511 /* Make sure X resources of the face is loaded. */
23512 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23513
23514 if (it->image_id < 0)
23515 {
23516 /* Fringe bitmap. */
23517 it->ascent = it->phys_ascent = 0;
23518 it->descent = it->phys_descent = 0;
23519 it->pixel_width = 0;
23520 it->nglyphs = 0;
23521 return;
23522 }
23523
23524 img = IMAGE_FROM_ID (it->f, it->image_id);
23525 xassert (img);
23526 /* Make sure X resources of the image is loaded. */
23527 prepare_image_for_display (it->f, img);
23528
23529 slice.x = slice.y = 0;
23530 slice.width = img->width;
23531 slice.height = img->height;
23532
23533 if (INTEGERP (it->slice.x))
23534 slice.x = XINT (it->slice.x);
23535 else if (FLOATP (it->slice.x))
23536 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23537
23538 if (INTEGERP (it->slice.y))
23539 slice.y = XINT (it->slice.y);
23540 else if (FLOATP (it->slice.y))
23541 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23542
23543 if (INTEGERP (it->slice.width))
23544 slice.width = XINT (it->slice.width);
23545 else if (FLOATP (it->slice.width))
23546 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23547
23548 if (INTEGERP (it->slice.height))
23549 slice.height = XINT (it->slice.height);
23550 else if (FLOATP (it->slice.height))
23551 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23552
23553 if (slice.x >= img->width)
23554 slice.x = img->width;
23555 if (slice.y >= img->height)
23556 slice.y = img->height;
23557 if (slice.x + slice.width >= img->width)
23558 slice.width = img->width - slice.x;
23559 if (slice.y + slice.height > img->height)
23560 slice.height = img->height - slice.y;
23561
23562 if (slice.width == 0 || slice.height == 0)
23563 return;
23564
23565 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23566
23567 it->descent = slice.height - glyph_ascent;
23568 if (slice.y == 0)
23569 it->descent += img->vmargin;
23570 if (slice.y + slice.height == img->height)
23571 it->descent += img->vmargin;
23572 it->phys_descent = it->descent;
23573
23574 it->pixel_width = slice.width;
23575 if (slice.x == 0)
23576 it->pixel_width += img->hmargin;
23577 if (slice.x + slice.width == img->width)
23578 it->pixel_width += img->hmargin;
23579
23580 /* It's quite possible for images to have an ascent greater than
23581 their height, so don't get confused in that case. */
23582 if (it->descent < 0)
23583 it->descent = 0;
23584
23585 it->nglyphs = 1;
23586
23587 if (face->box != FACE_NO_BOX)
23588 {
23589 if (face->box_line_width > 0)
23590 {
23591 if (slice.y == 0)
23592 it->ascent += face->box_line_width;
23593 if (slice.y + slice.height == img->height)
23594 it->descent += face->box_line_width;
23595 }
23596
23597 if (it->start_of_box_run_p && slice.x == 0)
23598 it->pixel_width += eabs (face->box_line_width);
23599 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23600 it->pixel_width += eabs (face->box_line_width);
23601 }
23602
23603 take_vertical_position_into_account (it);
23604
23605 /* Automatically crop wide image glyphs at right edge so we can
23606 draw the cursor on same display row. */
23607 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23608 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23609 {
23610 it->pixel_width -= crop;
23611 slice.width -= crop;
23612 }
23613
23614 if (it->glyph_row)
23615 {
23616 struct glyph *glyph;
23617 enum glyph_row_area area = it->area;
23618
23619 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23620 if (glyph < it->glyph_row->glyphs[area + 1])
23621 {
23622 glyph->charpos = CHARPOS (it->position);
23623 glyph->object = it->object;
23624 glyph->pixel_width = it->pixel_width;
23625 glyph->ascent = glyph_ascent;
23626 glyph->descent = it->descent;
23627 glyph->voffset = it->voffset;
23628 glyph->type = IMAGE_GLYPH;
23629 glyph->avoid_cursor_p = it->avoid_cursor_p;
23630 glyph->multibyte_p = it->multibyte_p;
23631 glyph->left_box_line_p = it->start_of_box_run_p;
23632 glyph->right_box_line_p = it->end_of_box_run_p;
23633 glyph->overlaps_vertically_p = 0;
23634 glyph->padding_p = 0;
23635 glyph->glyph_not_available_p = 0;
23636 glyph->face_id = it->face_id;
23637 glyph->u.img_id = img->id;
23638 glyph->slice.img = slice;
23639 glyph->font_type = FONT_TYPE_UNKNOWN;
23640 if (it->bidi_p)
23641 {
23642 glyph->resolved_level = it->bidi_it.resolved_level;
23643 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23644 abort ();
23645 glyph->bidi_type = it->bidi_it.type;
23646 }
23647 ++it->glyph_row->used[area];
23648 }
23649 else
23650 IT_EXPAND_MATRIX_WIDTH (it, area);
23651 }
23652 }
23653
23654
23655 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23656 of the glyph, WIDTH and HEIGHT are the width and height of the
23657 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23658
23659 static void
23660 append_stretch_glyph (struct it *it, Lisp_Object object,
23661 int width, int height, int ascent)
23662 {
23663 struct glyph *glyph;
23664 enum glyph_row_area area = it->area;
23665
23666 xassert (ascent >= 0 && ascent <= height);
23667
23668 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23669 if (glyph < it->glyph_row->glyphs[area + 1])
23670 {
23671 /* If the glyph row is reversed, we need to prepend the glyph
23672 rather than append it. */
23673 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23674 {
23675 struct glyph *g;
23676
23677 /* Make room for the additional glyph. */
23678 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23679 g[1] = *g;
23680 glyph = it->glyph_row->glyphs[area];
23681 }
23682 glyph->charpos = CHARPOS (it->position);
23683 glyph->object = object;
23684 glyph->pixel_width = width;
23685 glyph->ascent = ascent;
23686 glyph->descent = height - ascent;
23687 glyph->voffset = it->voffset;
23688 glyph->type = STRETCH_GLYPH;
23689 glyph->avoid_cursor_p = it->avoid_cursor_p;
23690 glyph->multibyte_p = it->multibyte_p;
23691 glyph->left_box_line_p = it->start_of_box_run_p;
23692 glyph->right_box_line_p = it->end_of_box_run_p;
23693 glyph->overlaps_vertically_p = 0;
23694 glyph->padding_p = 0;
23695 glyph->glyph_not_available_p = 0;
23696 glyph->face_id = it->face_id;
23697 glyph->u.stretch.ascent = ascent;
23698 glyph->u.stretch.height = height;
23699 glyph->slice.img = null_glyph_slice;
23700 glyph->font_type = FONT_TYPE_UNKNOWN;
23701 if (it->bidi_p)
23702 {
23703 glyph->resolved_level = it->bidi_it.resolved_level;
23704 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23705 abort ();
23706 glyph->bidi_type = it->bidi_it.type;
23707 }
23708 else
23709 {
23710 glyph->resolved_level = 0;
23711 glyph->bidi_type = UNKNOWN_BT;
23712 }
23713 ++it->glyph_row->used[area];
23714 }
23715 else
23716 IT_EXPAND_MATRIX_WIDTH (it, area);
23717 }
23718
23719 #endif /* HAVE_WINDOW_SYSTEM */
23720
23721 /* Produce a stretch glyph for iterator IT. IT->object is the value
23722 of the glyph property displayed. The value must be a list
23723 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23724 being recognized:
23725
23726 1. `:width WIDTH' specifies that the space should be WIDTH *
23727 canonical char width wide. WIDTH may be an integer or floating
23728 point number.
23729
23730 2. `:relative-width FACTOR' specifies that the width of the stretch
23731 should be computed from the width of the first character having the
23732 `glyph' property, and should be FACTOR times that width.
23733
23734 3. `:align-to HPOS' specifies that the space should be wide enough
23735 to reach HPOS, a value in canonical character units.
23736
23737 Exactly one of the above pairs must be present.
23738
23739 4. `:height HEIGHT' specifies that the height of the stretch produced
23740 should be HEIGHT, measured in canonical character units.
23741
23742 5. `:relative-height FACTOR' specifies that the height of the
23743 stretch should be FACTOR times the height of the characters having
23744 the glyph property.
23745
23746 Either none or exactly one of 4 or 5 must be present.
23747
23748 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23749 of the stretch should be used for the ascent of the stretch.
23750 ASCENT must be in the range 0 <= ASCENT <= 100. */
23751
23752 void
23753 produce_stretch_glyph (struct it *it)
23754 {
23755 /* (space :width WIDTH :height HEIGHT ...) */
23756 Lisp_Object prop, plist;
23757 int width = 0, height = 0, align_to = -1;
23758 int zero_width_ok_p = 0;
23759 int ascent = 0;
23760 double tem;
23761 struct face *face = NULL;
23762 struct font *font = NULL;
23763
23764 #ifdef HAVE_WINDOW_SYSTEM
23765 int zero_height_ok_p = 0;
23766
23767 if (FRAME_WINDOW_P (it->f))
23768 {
23769 face = FACE_FROM_ID (it->f, it->face_id);
23770 font = face->font ? face->font : FRAME_FONT (it->f);
23771 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23772 }
23773 #endif
23774
23775 /* List should start with `space'. */
23776 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23777 plist = XCDR (it->object);
23778
23779 /* Compute the width of the stretch. */
23780 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23781 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23782 {
23783 /* Absolute width `:width WIDTH' specified and valid. */
23784 zero_width_ok_p = 1;
23785 width = (int)tem;
23786 }
23787 #ifdef HAVE_WINDOW_SYSTEM
23788 else if (FRAME_WINDOW_P (it->f)
23789 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23790 {
23791 /* Relative width `:relative-width FACTOR' specified and valid.
23792 Compute the width of the characters having the `glyph'
23793 property. */
23794 struct it it2;
23795 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23796
23797 it2 = *it;
23798 if (it->multibyte_p)
23799 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23800 else
23801 {
23802 it2.c = it2.char_to_display = *p, it2.len = 1;
23803 if (! ASCII_CHAR_P (it2.c))
23804 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23805 }
23806
23807 it2.glyph_row = NULL;
23808 it2.what = IT_CHARACTER;
23809 x_produce_glyphs (&it2);
23810 width = NUMVAL (prop) * it2.pixel_width;
23811 }
23812 #endif /* HAVE_WINDOW_SYSTEM */
23813 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23814 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23815 {
23816 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23817 align_to = (align_to < 0
23818 ? 0
23819 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23820 else if (align_to < 0)
23821 align_to = window_box_left_offset (it->w, TEXT_AREA);
23822 width = max (0, (int)tem + align_to - it->current_x);
23823 zero_width_ok_p = 1;
23824 }
23825 else
23826 /* Nothing specified -> width defaults to canonical char width. */
23827 width = FRAME_COLUMN_WIDTH (it->f);
23828
23829 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23830 width = 1;
23831
23832 #ifdef HAVE_WINDOW_SYSTEM
23833 /* Compute height. */
23834 if (FRAME_WINDOW_P (it->f))
23835 {
23836 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23837 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23838 {
23839 height = (int)tem;
23840 zero_height_ok_p = 1;
23841 }
23842 else if (prop = Fplist_get (plist, QCrelative_height),
23843 NUMVAL (prop) > 0)
23844 height = FONT_HEIGHT (font) * NUMVAL (prop);
23845 else
23846 height = FONT_HEIGHT (font);
23847
23848 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23849 height = 1;
23850
23851 /* Compute percentage of height used for ascent. If
23852 `:ascent ASCENT' is present and valid, use that. Otherwise,
23853 derive the ascent from the font in use. */
23854 if (prop = Fplist_get (plist, QCascent),
23855 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23856 ascent = height * NUMVAL (prop) / 100.0;
23857 else if (!NILP (prop)
23858 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23859 ascent = min (max (0, (int)tem), height);
23860 else
23861 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23862 }
23863 else
23864 #endif /* HAVE_WINDOW_SYSTEM */
23865 height = 1;
23866
23867 if (width > 0 && it->line_wrap != TRUNCATE
23868 && it->current_x + width > it->last_visible_x)
23869 {
23870 width = it->last_visible_x - it->current_x;
23871 #ifdef HAVE_WINDOW_SYSTEM
23872 /* Subtract one more pixel from the stretch width, but only on
23873 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23874 width -= FRAME_WINDOW_P (it->f);
23875 #endif
23876 }
23877
23878 if (width > 0 && height > 0 && it->glyph_row)
23879 {
23880 Lisp_Object o_object = it->object;
23881 Lisp_Object object = it->stack[it->sp - 1].string;
23882 int n = width;
23883
23884 if (!STRINGP (object))
23885 object = it->w->buffer;
23886 #ifdef HAVE_WINDOW_SYSTEM
23887 if (FRAME_WINDOW_P (it->f))
23888 append_stretch_glyph (it, object, width, height, ascent);
23889 else
23890 #endif
23891 {
23892 it->object = object;
23893 it->char_to_display = ' ';
23894 it->pixel_width = it->len = 1;
23895 while (n--)
23896 tty_append_glyph (it);
23897 it->object = o_object;
23898 }
23899 }
23900
23901 it->pixel_width = width;
23902 #ifdef HAVE_WINDOW_SYSTEM
23903 if (FRAME_WINDOW_P (it->f))
23904 {
23905 it->ascent = it->phys_ascent = ascent;
23906 it->descent = it->phys_descent = height - it->ascent;
23907 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23908 take_vertical_position_into_account (it);
23909 }
23910 else
23911 #endif
23912 it->nglyphs = width;
23913 }
23914
23915 #ifdef HAVE_WINDOW_SYSTEM
23916
23917 /* Calculate line-height and line-spacing properties.
23918 An integer value specifies explicit pixel value.
23919 A float value specifies relative value to current face height.
23920 A cons (float . face-name) specifies relative value to
23921 height of specified face font.
23922
23923 Returns height in pixels, or nil. */
23924
23925
23926 static Lisp_Object
23927 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23928 int boff, int override)
23929 {
23930 Lisp_Object face_name = Qnil;
23931 int ascent, descent, height;
23932
23933 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23934 return val;
23935
23936 if (CONSP (val))
23937 {
23938 face_name = XCAR (val);
23939 val = XCDR (val);
23940 if (!NUMBERP (val))
23941 val = make_number (1);
23942 if (NILP (face_name))
23943 {
23944 height = it->ascent + it->descent;
23945 goto scale;
23946 }
23947 }
23948
23949 if (NILP (face_name))
23950 {
23951 font = FRAME_FONT (it->f);
23952 boff = FRAME_BASELINE_OFFSET (it->f);
23953 }
23954 else if (EQ (face_name, Qt))
23955 {
23956 override = 0;
23957 }
23958 else
23959 {
23960 int face_id;
23961 struct face *face;
23962
23963 face_id = lookup_named_face (it->f, face_name, 0);
23964 if (face_id < 0)
23965 return make_number (-1);
23966
23967 face = FACE_FROM_ID (it->f, face_id);
23968 font = face->font;
23969 if (font == NULL)
23970 return make_number (-1);
23971 boff = font->baseline_offset;
23972 if (font->vertical_centering)
23973 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23974 }
23975
23976 ascent = FONT_BASE (font) + boff;
23977 descent = FONT_DESCENT (font) - boff;
23978
23979 if (override)
23980 {
23981 it->override_ascent = ascent;
23982 it->override_descent = descent;
23983 it->override_boff = boff;
23984 }
23985
23986 height = ascent + descent;
23987
23988 scale:
23989 if (FLOATP (val))
23990 height = (int)(XFLOAT_DATA (val) * height);
23991 else if (INTEGERP (val))
23992 height *= XINT (val);
23993
23994 return make_number (height);
23995 }
23996
23997
23998 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23999 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24000 and only if this is for a character for which no font was found.
24001
24002 If the display method (it->glyphless_method) is
24003 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24004 length of the acronym or the hexadecimal string, UPPER_XOFF and
24005 UPPER_YOFF are pixel offsets for the upper part of the string,
24006 LOWER_XOFF and LOWER_YOFF are for the lower part.
24007
24008 For the other display methods, LEN through LOWER_YOFF are zero. */
24009
24010 static void
24011 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24012 short upper_xoff, short upper_yoff,
24013 short lower_xoff, short lower_yoff)
24014 {
24015 struct glyph *glyph;
24016 enum glyph_row_area area = it->area;
24017
24018 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24019 if (glyph < it->glyph_row->glyphs[area + 1])
24020 {
24021 /* If the glyph row is reversed, we need to prepend the glyph
24022 rather than append it. */
24023 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24024 {
24025 struct glyph *g;
24026
24027 /* Make room for the additional glyph. */
24028 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24029 g[1] = *g;
24030 glyph = it->glyph_row->glyphs[area];
24031 }
24032 glyph->charpos = CHARPOS (it->position);
24033 glyph->object = it->object;
24034 glyph->pixel_width = it->pixel_width;
24035 glyph->ascent = it->ascent;
24036 glyph->descent = it->descent;
24037 glyph->voffset = it->voffset;
24038 glyph->type = GLYPHLESS_GLYPH;
24039 glyph->u.glyphless.method = it->glyphless_method;
24040 glyph->u.glyphless.for_no_font = for_no_font;
24041 glyph->u.glyphless.len = len;
24042 glyph->u.glyphless.ch = it->c;
24043 glyph->slice.glyphless.upper_xoff = upper_xoff;
24044 glyph->slice.glyphless.upper_yoff = upper_yoff;
24045 glyph->slice.glyphless.lower_xoff = lower_xoff;
24046 glyph->slice.glyphless.lower_yoff = lower_yoff;
24047 glyph->avoid_cursor_p = it->avoid_cursor_p;
24048 glyph->multibyte_p = it->multibyte_p;
24049 glyph->left_box_line_p = it->start_of_box_run_p;
24050 glyph->right_box_line_p = it->end_of_box_run_p;
24051 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24052 || it->phys_descent > it->descent);
24053 glyph->padding_p = 0;
24054 glyph->glyph_not_available_p = 0;
24055 glyph->face_id = face_id;
24056 glyph->font_type = FONT_TYPE_UNKNOWN;
24057 if (it->bidi_p)
24058 {
24059 glyph->resolved_level = it->bidi_it.resolved_level;
24060 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24061 abort ();
24062 glyph->bidi_type = it->bidi_it.type;
24063 }
24064 ++it->glyph_row->used[area];
24065 }
24066 else
24067 IT_EXPAND_MATRIX_WIDTH (it, area);
24068 }
24069
24070
24071 /* Produce a glyph for a glyphless character for iterator IT.
24072 IT->glyphless_method specifies which method to use for displaying
24073 the character. See the description of enum
24074 glyphless_display_method in dispextern.h for the detail.
24075
24076 FOR_NO_FONT is nonzero if and only if this is for a character for
24077 which no font was found. ACRONYM, if non-nil, is an acronym string
24078 for the character. */
24079
24080 static void
24081 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24082 {
24083 int face_id;
24084 struct face *face;
24085 struct font *font;
24086 int base_width, base_height, width, height;
24087 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24088 int len;
24089
24090 /* Get the metrics of the base font. We always refer to the current
24091 ASCII face. */
24092 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24093 font = face->font ? face->font : FRAME_FONT (it->f);
24094 it->ascent = FONT_BASE (font) + font->baseline_offset;
24095 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24096 base_height = it->ascent + it->descent;
24097 base_width = font->average_width;
24098
24099 /* Get a face ID for the glyph by utilizing a cache (the same way as
24100 done for `escape-glyph' in get_next_display_element). */
24101 if (it->f == last_glyphless_glyph_frame
24102 && it->face_id == last_glyphless_glyph_face_id)
24103 {
24104 face_id = last_glyphless_glyph_merged_face_id;
24105 }
24106 else
24107 {
24108 /* Merge the `glyphless-char' face into the current face. */
24109 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24110 last_glyphless_glyph_frame = it->f;
24111 last_glyphless_glyph_face_id = it->face_id;
24112 last_glyphless_glyph_merged_face_id = face_id;
24113 }
24114
24115 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24116 {
24117 it->pixel_width = THIN_SPACE_WIDTH;
24118 len = 0;
24119 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24120 }
24121 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24122 {
24123 width = CHAR_WIDTH (it->c);
24124 if (width == 0)
24125 width = 1;
24126 else if (width > 4)
24127 width = 4;
24128 it->pixel_width = base_width * width;
24129 len = 0;
24130 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24131 }
24132 else
24133 {
24134 char buf[7];
24135 const char *str;
24136 unsigned int code[6];
24137 int upper_len;
24138 int ascent, descent;
24139 struct font_metrics metrics_upper, metrics_lower;
24140
24141 face = FACE_FROM_ID (it->f, face_id);
24142 font = face->font ? face->font : FRAME_FONT (it->f);
24143 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24144
24145 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24146 {
24147 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24148 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24149 if (CONSP (acronym))
24150 acronym = XCAR (acronym);
24151 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24152 }
24153 else
24154 {
24155 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24156 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24157 str = buf;
24158 }
24159 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24160 code[len] = font->driver->encode_char (font, str[len]);
24161 upper_len = (len + 1) / 2;
24162 font->driver->text_extents (font, code, upper_len,
24163 &metrics_upper);
24164 font->driver->text_extents (font, code + upper_len, len - upper_len,
24165 &metrics_lower);
24166
24167
24168
24169 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24170 width = max (metrics_upper.width, metrics_lower.width) + 4;
24171 upper_xoff = upper_yoff = 2; /* the typical case */
24172 if (base_width >= width)
24173 {
24174 /* Align the upper to the left, the lower to the right. */
24175 it->pixel_width = base_width;
24176 lower_xoff = base_width - 2 - metrics_lower.width;
24177 }
24178 else
24179 {
24180 /* Center the shorter one. */
24181 it->pixel_width = width;
24182 if (metrics_upper.width >= metrics_lower.width)
24183 lower_xoff = (width - metrics_lower.width) / 2;
24184 else
24185 {
24186 /* FIXME: This code doesn't look right. It formerly was
24187 missing the "lower_xoff = 0;", which couldn't have
24188 been right since it left lower_xoff uninitialized. */
24189 lower_xoff = 0;
24190 upper_xoff = (width - metrics_upper.width) / 2;
24191 }
24192 }
24193
24194 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24195 top, bottom, and between upper and lower strings. */
24196 height = (metrics_upper.ascent + metrics_upper.descent
24197 + metrics_lower.ascent + metrics_lower.descent) + 5;
24198 /* Center vertically.
24199 H:base_height, D:base_descent
24200 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24201
24202 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24203 descent = D - H/2 + h/2;
24204 lower_yoff = descent - 2 - ld;
24205 upper_yoff = lower_yoff - la - 1 - ud; */
24206 ascent = - (it->descent - (base_height + height + 1) / 2);
24207 descent = it->descent - (base_height - height) / 2;
24208 lower_yoff = descent - 2 - metrics_lower.descent;
24209 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24210 - metrics_upper.descent);
24211 /* Don't make the height shorter than the base height. */
24212 if (height > base_height)
24213 {
24214 it->ascent = ascent;
24215 it->descent = descent;
24216 }
24217 }
24218
24219 it->phys_ascent = it->ascent;
24220 it->phys_descent = it->descent;
24221 if (it->glyph_row)
24222 append_glyphless_glyph (it, face_id, for_no_font, len,
24223 upper_xoff, upper_yoff,
24224 lower_xoff, lower_yoff);
24225 it->nglyphs = 1;
24226 take_vertical_position_into_account (it);
24227 }
24228
24229
24230 /* RIF:
24231 Produce glyphs/get display metrics for the display element IT is
24232 loaded with. See the description of struct it in dispextern.h
24233 for an overview of struct it. */
24234
24235 void
24236 x_produce_glyphs (struct it *it)
24237 {
24238 int extra_line_spacing = it->extra_line_spacing;
24239
24240 it->glyph_not_available_p = 0;
24241
24242 if (it->what == IT_CHARACTER)
24243 {
24244 XChar2b char2b;
24245 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24246 struct font *font = face->font;
24247 struct font_metrics *pcm = NULL;
24248 int boff; /* baseline offset */
24249
24250 if (font == NULL)
24251 {
24252 /* When no suitable font is found, display this character by
24253 the method specified in the first extra slot of
24254 Vglyphless_char_display. */
24255 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24256
24257 xassert (it->what == IT_GLYPHLESS);
24258 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24259 goto done;
24260 }
24261
24262 boff = font->baseline_offset;
24263 if (font->vertical_centering)
24264 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24265
24266 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24267 {
24268 int stretched_p;
24269
24270 it->nglyphs = 1;
24271
24272 if (it->override_ascent >= 0)
24273 {
24274 it->ascent = it->override_ascent;
24275 it->descent = it->override_descent;
24276 boff = it->override_boff;
24277 }
24278 else
24279 {
24280 it->ascent = FONT_BASE (font) + boff;
24281 it->descent = FONT_DESCENT (font) - boff;
24282 }
24283
24284 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24285 {
24286 pcm = get_per_char_metric (font, &char2b);
24287 if (pcm->width == 0
24288 && pcm->rbearing == 0 && pcm->lbearing == 0)
24289 pcm = NULL;
24290 }
24291
24292 if (pcm)
24293 {
24294 it->phys_ascent = pcm->ascent + boff;
24295 it->phys_descent = pcm->descent - boff;
24296 it->pixel_width = pcm->width;
24297 }
24298 else
24299 {
24300 it->glyph_not_available_p = 1;
24301 it->phys_ascent = it->ascent;
24302 it->phys_descent = it->descent;
24303 it->pixel_width = font->space_width;
24304 }
24305
24306 if (it->constrain_row_ascent_descent_p)
24307 {
24308 if (it->descent > it->max_descent)
24309 {
24310 it->ascent += it->descent - it->max_descent;
24311 it->descent = it->max_descent;
24312 }
24313 if (it->ascent > it->max_ascent)
24314 {
24315 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24316 it->ascent = it->max_ascent;
24317 }
24318 it->phys_ascent = min (it->phys_ascent, it->ascent);
24319 it->phys_descent = min (it->phys_descent, it->descent);
24320 extra_line_spacing = 0;
24321 }
24322
24323 /* If this is a space inside a region of text with
24324 `space-width' property, change its width. */
24325 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24326 if (stretched_p)
24327 it->pixel_width *= XFLOATINT (it->space_width);
24328
24329 /* If face has a box, add the box thickness to the character
24330 height. If character has a box line to the left and/or
24331 right, add the box line width to the character's width. */
24332 if (face->box != FACE_NO_BOX)
24333 {
24334 int thick = face->box_line_width;
24335
24336 if (thick > 0)
24337 {
24338 it->ascent += thick;
24339 it->descent += thick;
24340 }
24341 else
24342 thick = -thick;
24343
24344 if (it->start_of_box_run_p)
24345 it->pixel_width += thick;
24346 if (it->end_of_box_run_p)
24347 it->pixel_width += thick;
24348 }
24349
24350 /* If face has an overline, add the height of the overline
24351 (1 pixel) and a 1 pixel margin to the character height. */
24352 if (face->overline_p)
24353 it->ascent += overline_margin;
24354
24355 if (it->constrain_row_ascent_descent_p)
24356 {
24357 if (it->ascent > it->max_ascent)
24358 it->ascent = it->max_ascent;
24359 if (it->descent > it->max_descent)
24360 it->descent = it->max_descent;
24361 }
24362
24363 take_vertical_position_into_account (it);
24364
24365 /* If we have to actually produce glyphs, do it. */
24366 if (it->glyph_row)
24367 {
24368 if (stretched_p)
24369 {
24370 /* Translate a space with a `space-width' property
24371 into a stretch glyph. */
24372 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24373 / FONT_HEIGHT (font));
24374 append_stretch_glyph (it, it->object, it->pixel_width,
24375 it->ascent + it->descent, ascent);
24376 }
24377 else
24378 append_glyph (it);
24379
24380 /* If characters with lbearing or rbearing are displayed
24381 in this line, record that fact in a flag of the
24382 glyph row. This is used to optimize X output code. */
24383 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24384 it->glyph_row->contains_overlapping_glyphs_p = 1;
24385 }
24386 if (! stretched_p && it->pixel_width == 0)
24387 /* We assure that all visible glyphs have at least 1-pixel
24388 width. */
24389 it->pixel_width = 1;
24390 }
24391 else if (it->char_to_display == '\n')
24392 {
24393 /* A newline has no width, but we need the height of the
24394 line. But if previous part of the line sets a height,
24395 don't increase that height */
24396
24397 Lisp_Object height;
24398 Lisp_Object total_height = Qnil;
24399
24400 it->override_ascent = -1;
24401 it->pixel_width = 0;
24402 it->nglyphs = 0;
24403
24404 height = get_it_property (it, Qline_height);
24405 /* Split (line-height total-height) list */
24406 if (CONSP (height)
24407 && CONSP (XCDR (height))
24408 && NILP (XCDR (XCDR (height))))
24409 {
24410 total_height = XCAR (XCDR (height));
24411 height = XCAR (height);
24412 }
24413 height = calc_line_height_property (it, height, font, boff, 1);
24414
24415 if (it->override_ascent >= 0)
24416 {
24417 it->ascent = it->override_ascent;
24418 it->descent = it->override_descent;
24419 boff = it->override_boff;
24420 }
24421 else
24422 {
24423 it->ascent = FONT_BASE (font) + boff;
24424 it->descent = FONT_DESCENT (font) - boff;
24425 }
24426
24427 if (EQ (height, Qt))
24428 {
24429 if (it->descent > it->max_descent)
24430 {
24431 it->ascent += it->descent - it->max_descent;
24432 it->descent = it->max_descent;
24433 }
24434 if (it->ascent > it->max_ascent)
24435 {
24436 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24437 it->ascent = it->max_ascent;
24438 }
24439 it->phys_ascent = min (it->phys_ascent, it->ascent);
24440 it->phys_descent = min (it->phys_descent, it->descent);
24441 it->constrain_row_ascent_descent_p = 1;
24442 extra_line_spacing = 0;
24443 }
24444 else
24445 {
24446 Lisp_Object spacing;
24447
24448 it->phys_ascent = it->ascent;
24449 it->phys_descent = it->descent;
24450
24451 if ((it->max_ascent > 0 || it->max_descent > 0)
24452 && face->box != FACE_NO_BOX
24453 && face->box_line_width > 0)
24454 {
24455 it->ascent += face->box_line_width;
24456 it->descent += face->box_line_width;
24457 }
24458 if (!NILP (height)
24459 && XINT (height) > it->ascent + it->descent)
24460 it->ascent = XINT (height) - it->descent;
24461
24462 if (!NILP (total_height))
24463 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24464 else
24465 {
24466 spacing = get_it_property (it, Qline_spacing);
24467 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24468 }
24469 if (INTEGERP (spacing))
24470 {
24471 extra_line_spacing = XINT (spacing);
24472 if (!NILP (total_height))
24473 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24474 }
24475 }
24476 }
24477 else /* i.e. (it->char_to_display == '\t') */
24478 {
24479 if (font->space_width > 0)
24480 {
24481 int tab_width = it->tab_width * font->space_width;
24482 int x = it->current_x + it->continuation_lines_width;
24483 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24484
24485 /* If the distance from the current position to the next tab
24486 stop is less than a space character width, use the
24487 tab stop after that. */
24488 if (next_tab_x - x < font->space_width)
24489 next_tab_x += tab_width;
24490
24491 it->pixel_width = next_tab_x - x;
24492 it->nglyphs = 1;
24493 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24494 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24495
24496 if (it->glyph_row)
24497 {
24498 append_stretch_glyph (it, it->object, it->pixel_width,
24499 it->ascent + it->descent, it->ascent);
24500 }
24501 }
24502 else
24503 {
24504 it->pixel_width = 0;
24505 it->nglyphs = 1;
24506 }
24507 }
24508 }
24509 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24510 {
24511 /* A static composition.
24512
24513 Note: A composition is represented as one glyph in the
24514 glyph matrix. There are no padding glyphs.
24515
24516 Important note: pixel_width, ascent, and descent are the
24517 values of what is drawn by draw_glyphs (i.e. the values of
24518 the overall glyphs composed). */
24519 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24520 int boff; /* baseline offset */
24521 struct composition *cmp = composition_table[it->cmp_it.id];
24522 int glyph_len = cmp->glyph_len;
24523 struct font *font = face->font;
24524
24525 it->nglyphs = 1;
24526
24527 /* If we have not yet calculated pixel size data of glyphs of
24528 the composition for the current face font, calculate them
24529 now. Theoretically, we have to check all fonts for the
24530 glyphs, but that requires much time and memory space. So,
24531 here we check only the font of the first glyph. This may
24532 lead to incorrect display, but it's very rare, and C-l
24533 (recenter-top-bottom) can correct the display anyway. */
24534 if (! cmp->font || cmp->font != font)
24535 {
24536 /* Ascent and descent of the font of the first character
24537 of this composition (adjusted by baseline offset).
24538 Ascent and descent of overall glyphs should not be less
24539 than these, respectively. */
24540 int font_ascent, font_descent, font_height;
24541 /* Bounding box of the overall glyphs. */
24542 int leftmost, rightmost, lowest, highest;
24543 int lbearing, rbearing;
24544 int i, width, ascent, descent;
24545 int left_padded = 0, right_padded = 0;
24546 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24547 XChar2b char2b;
24548 struct font_metrics *pcm;
24549 int font_not_found_p;
24550 EMACS_INT pos;
24551
24552 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24553 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24554 break;
24555 if (glyph_len < cmp->glyph_len)
24556 right_padded = 1;
24557 for (i = 0; i < glyph_len; i++)
24558 {
24559 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24560 break;
24561 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24562 }
24563 if (i > 0)
24564 left_padded = 1;
24565
24566 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24567 : IT_CHARPOS (*it));
24568 /* If no suitable font is found, use the default font. */
24569 font_not_found_p = font == NULL;
24570 if (font_not_found_p)
24571 {
24572 face = face->ascii_face;
24573 font = face->font;
24574 }
24575 boff = font->baseline_offset;
24576 if (font->vertical_centering)
24577 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24578 font_ascent = FONT_BASE (font) + boff;
24579 font_descent = FONT_DESCENT (font) - boff;
24580 font_height = FONT_HEIGHT (font);
24581
24582 cmp->font = (void *) font;
24583
24584 pcm = NULL;
24585 if (! font_not_found_p)
24586 {
24587 get_char_face_and_encoding (it->f, c, it->face_id,
24588 &char2b, 0);
24589 pcm = get_per_char_metric (font, &char2b);
24590 }
24591
24592 /* Initialize the bounding box. */
24593 if (pcm)
24594 {
24595 width = cmp->glyph_len > 0 ? pcm->width : 0;
24596 ascent = pcm->ascent;
24597 descent = pcm->descent;
24598 lbearing = pcm->lbearing;
24599 rbearing = pcm->rbearing;
24600 }
24601 else
24602 {
24603 width = cmp->glyph_len > 0 ? font->space_width : 0;
24604 ascent = FONT_BASE (font);
24605 descent = FONT_DESCENT (font);
24606 lbearing = 0;
24607 rbearing = width;
24608 }
24609
24610 rightmost = width;
24611 leftmost = 0;
24612 lowest = - descent + boff;
24613 highest = ascent + boff;
24614
24615 if (! font_not_found_p
24616 && font->default_ascent
24617 && CHAR_TABLE_P (Vuse_default_ascent)
24618 && !NILP (Faref (Vuse_default_ascent,
24619 make_number (it->char_to_display))))
24620 highest = font->default_ascent + boff;
24621
24622 /* Draw the first glyph at the normal position. It may be
24623 shifted to right later if some other glyphs are drawn
24624 at the left. */
24625 cmp->offsets[i * 2] = 0;
24626 cmp->offsets[i * 2 + 1] = boff;
24627 cmp->lbearing = lbearing;
24628 cmp->rbearing = rbearing;
24629
24630 /* Set cmp->offsets for the remaining glyphs. */
24631 for (i++; i < glyph_len; i++)
24632 {
24633 int left, right, btm, top;
24634 int ch = COMPOSITION_GLYPH (cmp, i);
24635 int face_id;
24636 struct face *this_face;
24637
24638 if (ch == '\t')
24639 ch = ' ';
24640 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24641 this_face = FACE_FROM_ID (it->f, face_id);
24642 font = this_face->font;
24643
24644 if (font == NULL)
24645 pcm = NULL;
24646 else
24647 {
24648 get_char_face_and_encoding (it->f, ch, face_id,
24649 &char2b, 0);
24650 pcm = get_per_char_metric (font, &char2b);
24651 }
24652 if (! pcm)
24653 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24654 else
24655 {
24656 width = pcm->width;
24657 ascent = pcm->ascent;
24658 descent = pcm->descent;
24659 lbearing = pcm->lbearing;
24660 rbearing = pcm->rbearing;
24661 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24662 {
24663 /* Relative composition with or without
24664 alternate chars. */
24665 left = (leftmost + rightmost - width) / 2;
24666 btm = - descent + boff;
24667 if (font->relative_compose
24668 && (! CHAR_TABLE_P (Vignore_relative_composition)
24669 || NILP (Faref (Vignore_relative_composition,
24670 make_number (ch)))))
24671 {
24672
24673 if (- descent >= font->relative_compose)
24674 /* One extra pixel between two glyphs. */
24675 btm = highest + 1;
24676 else if (ascent <= 0)
24677 /* One extra pixel between two glyphs. */
24678 btm = lowest - 1 - ascent - descent;
24679 }
24680 }
24681 else
24682 {
24683 /* A composition rule is specified by an integer
24684 value that encodes global and new reference
24685 points (GREF and NREF). GREF and NREF are
24686 specified by numbers as below:
24687
24688 0---1---2 -- ascent
24689 | |
24690 | |
24691 | |
24692 9--10--11 -- center
24693 | |
24694 ---3---4---5--- baseline
24695 | |
24696 6---7---8 -- descent
24697 */
24698 int rule = COMPOSITION_RULE (cmp, i);
24699 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24700
24701 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24702 grefx = gref % 3, nrefx = nref % 3;
24703 grefy = gref / 3, nrefy = nref / 3;
24704 if (xoff)
24705 xoff = font_height * (xoff - 128) / 256;
24706 if (yoff)
24707 yoff = font_height * (yoff - 128) / 256;
24708
24709 left = (leftmost
24710 + grefx * (rightmost - leftmost) / 2
24711 - nrefx * width / 2
24712 + xoff);
24713
24714 btm = ((grefy == 0 ? highest
24715 : grefy == 1 ? 0
24716 : grefy == 2 ? lowest
24717 : (highest + lowest) / 2)
24718 - (nrefy == 0 ? ascent + descent
24719 : nrefy == 1 ? descent - boff
24720 : nrefy == 2 ? 0
24721 : (ascent + descent) / 2)
24722 + yoff);
24723 }
24724
24725 cmp->offsets[i * 2] = left;
24726 cmp->offsets[i * 2 + 1] = btm + descent;
24727
24728 /* Update the bounding box of the overall glyphs. */
24729 if (width > 0)
24730 {
24731 right = left + width;
24732 if (left < leftmost)
24733 leftmost = left;
24734 if (right > rightmost)
24735 rightmost = right;
24736 }
24737 top = btm + descent + ascent;
24738 if (top > highest)
24739 highest = top;
24740 if (btm < lowest)
24741 lowest = btm;
24742
24743 if (cmp->lbearing > left + lbearing)
24744 cmp->lbearing = left + lbearing;
24745 if (cmp->rbearing < left + rbearing)
24746 cmp->rbearing = left + rbearing;
24747 }
24748 }
24749
24750 /* If there are glyphs whose x-offsets are negative,
24751 shift all glyphs to the right and make all x-offsets
24752 non-negative. */
24753 if (leftmost < 0)
24754 {
24755 for (i = 0; i < cmp->glyph_len; i++)
24756 cmp->offsets[i * 2] -= leftmost;
24757 rightmost -= leftmost;
24758 cmp->lbearing -= leftmost;
24759 cmp->rbearing -= leftmost;
24760 }
24761
24762 if (left_padded && cmp->lbearing < 0)
24763 {
24764 for (i = 0; i < cmp->glyph_len; i++)
24765 cmp->offsets[i * 2] -= cmp->lbearing;
24766 rightmost -= cmp->lbearing;
24767 cmp->rbearing -= cmp->lbearing;
24768 cmp->lbearing = 0;
24769 }
24770 if (right_padded && rightmost < cmp->rbearing)
24771 {
24772 rightmost = cmp->rbearing;
24773 }
24774
24775 cmp->pixel_width = rightmost;
24776 cmp->ascent = highest;
24777 cmp->descent = - lowest;
24778 if (cmp->ascent < font_ascent)
24779 cmp->ascent = font_ascent;
24780 if (cmp->descent < font_descent)
24781 cmp->descent = font_descent;
24782 }
24783
24784 if (it->glyph_row
24785 && (cmp->lbearing < 0
24786 || cmp->rbearing > cmp->pixel_width))
24787 it->glyph_row->contains_overlapping_glyphs_p = 1;
24788
24789 it->pixel_width = cmp->pixel_width;
24790 it->ascent = it->phys_ascent = cmp->ascent;
24791 it->descent = it->phys_descent = cmp->descent;
24792 if (face->box != FACE_NO_BOX)
24793 {
24794 int thick = face->box_line_width;
24795
24796 if (thick > 0)
24797 {
24798 it->ascent += thick;
24799 it->descent += thick;
24800 }
24801 else
24802 thick = - thick;
24803
24804 if (it->start_of_box_run_p)
24805 it->pixel_width += thick;
24806 if (it->end_of_box_run_p)
24807 it->pixel_width += thick;
24808 }
24809
24810 /* If face has an overline, add the height of the overline
24811 (1 pixel) and a 1 pixel margin to the character height. */
24812 if (face->overline_p)
24813 it->ascent += overline_margin;
24814
24815 take_vertical_position_into_account (it);
24816 if (it->ascent < 0)
24817 it->ascent = 0;
24818 if (it->descent < 0)
24819 it->descent = 0;
24820
24821 if (it->glyph_row && cmp->glyph_len > 0)
24822 append_composite_glyph (it);
24823 }
24824 else if (it->what == IT_COMPOSITION)
24825 {
24826 /* A dynamic (automatic) composition. */
24827 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24828 Lisp_Object gstring;
24829 struct font_metrics metrics;
24830
24831 it->nglyphs = 1;
24832
24833 gstring = composition_gstring_from_id (it->cmp_it.id);
24834 it->pixel_width
24835 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24836 &metrics);
24837 if (it->glyph_row
24838 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24839 it->glyph_row->contains_overlapping_glyphs_p = 1;
24840 it->ascent = it->phys_ascent = metrics.ascent;
24841 it->descent = it->phys_descent = metrics.descent;
24842 if (face->box != FACE_NO_BOX)
24843 {
24844 int thick = face->box_line_width;
24845
24846 if (thick > 0)
24847 {
24848 it->ascent += thick;
24849 it->descent += thick;
24850 }
24851 else
24852 thick = - thick;
24853
24854 if (it->start_of_box_run_p)
24855 it->pixel_width += thick;
24856 if (it->end_of_box_run_p)
24857 it->pixel_width += thick;
24858 }
24859 /* If face has an overline, add the height of the overline
24860 (1 pixel) and a 1 pixel margin to the character height. */
24861 if (face->overline_p)
24862 it->ascent += overline_margin;
24863 take_vertical_position_into_account (it);
24864 if (it->ascent < 0)
24865 it->ascent = 0;
24866 if (it->descent < 0)
24867 it->descent = 0;
24868
24869 if (it->glyph_row)
24870 append_composite_glyph (it);
24871 }
24872 else if (it->what == IT_GLYPHLESS)
24873 produce_glyphless_glyph (it, 0, Qnil);
24874 else if (it->what == IT_IMAGE)
24875 produce_image_glyph (it);
24876 else if (it->what == IT_STRETCH)
24877 produce_stretch_glyph (it);
24878
24879 done:
24880 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24881 because this isn't true for images with `:ascent 100'. */
24882 xassert (it->ascent >= 0 && it->descent >= 0);
24883 if (it->area == TEXT_AREA)
24884 it->current_x += it->pixel_width;
24885
24886 if (extra_line_spacing > 0)
24887 {
24888 it->descent += extra_line_spacing;
24889 if (extra_line_spacing > it->max_extra_line_spacing)
24890 it->max_extra_line_spacing = extra_line_spacing;
24891 }
24892
24893 it->max_ascent = max (it->max_ascent, it->ascent);
24894 it->max_descent = max (it->max_descent, it->descent);
24895 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24896 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24897 }
24898
24899 /* EXPORT for RIF:
24900 Output LEN glyphs starting at START at the nominal cursor position.
24901 Advance the nominal cursor over the text. The global variable
24902 updated_window contains the window being updated, updated_row is
24903 the glyph row being updated, and updated_area is the area of that
24904 row being updated. */
24905
24906 void
24907 x_write_glyphs (struct glyph *start, int len)
24908 {
24909 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24910
24911 xassert (updated_window && updated_row);
24912 /* When the window is hscrolled, cursor hpos can legitimately be out
24913 of bounds, but we draw the cursor at the corresponding window
24914 margin in that case. */
24915 if (!updated_row->reversed_p && chpos < 0)
24916 chpos = 0;
24917 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24918 chpos = updated_row->used[TEXT_AREA] - 1;
24919
24920 BLOCK_INPUT;
24921
24922 /* Write glyphs. */
24923
24924 hpos = start - updated_row->glyphs[updated_area];
24925 x = draw_glyphs (updated_window, output_cursor.x,
24926 updated_row, updated_area,
24927 hpos, hpos + len,
24928 DRAW_NORMAL_TEXT, 0);
24929
24930 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24931 if (updated_area == TEXT_AREA
24932 && updated_window->phys_cursor_on_p
24933 && updated_window->phys_cursor.vpos == output_cursor.vpos
24934 && chpos >= hpos
24935 && chpos < hpos + len)
24936 updated_window->phys_cursor_on_p = 0;
24937
24938 UNBLOCK_INPUT;
24939
24940 /* Advance the output cursor. */
24941 output_cursor.hpos += len;
24942 output_cursor.x = x;
24943 }
24944
24945
24946 /* EXPORT for RIF:
24947 Insert LEN glyphs from START at the nominal cursor position. */
24948
24949 void
24950 x_insert_glyphs (struct glyph *start, int len)
24951 {
24952 struct frame *f;
24953 struct window *w;
24954 int line_height, shift_by_width, shifted_region_width;
24955 struct glyph_row *row;
24956 struct glyph *glyph;
24957 int frame_x, frame_y;
24958 EMACS_INT hpos;
24959
24960 xassert (updated_window && updated_row);
24961 BLOCK_INPUT;
24962 w = updated_window;
24963 f = XFRAME (WINDOW_FRAME (w));
24964
24965 /* Get the height of the line we are in. */
24966 row = updated_row;
24967 line_height = row->height;
24968
24969 /* Get the width of the glyphs to insert. */
24970 shift_by_width = 0;
24971 for (glyph = start; glyph < start + len; ++glyph)
24972 shift_by_width += glyph->pixel_width;
24973
24974 /* Get the width of the region to shift right. */
24975 shifted_region_width = (window_box_width (w, updated_area)
24976 - output_cursor.x
24977 - shift_by_width);
24978
24979 /* Shift right. */
24980 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24981 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24982
24983 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24984 line_height, shift_by_width);
24985
24986 /* Write the glyphs. */
24987 hpos = start - row->glyphs[updated_area];
24988 draw_glyphs (w, output_cursor.x, row, updated_area,
24989 hpos, hpos + len,
24990 DRAW_NORMAL_TEXT, 0);
24991
24992 /* Advance the output cursor. */
24993 output_cursor.hpos += len;
24994 output_cursor.x += shift_by_width;
24995 UNBLOCK_INPUT;
24996 }
24997
24998
24999 /* EXPORT for RIF:
25000 Erase the current text line from the nominal cursor position
25001 (inclusive) to pixel column TO_X (exclusive). The idea is that
25002 everything from TO_X onward is already erased.
25003
25004 TO_X is a pixel position relative to updated_area of
25005 updated_window. TO_X == -1 means clear to the end of this area. */
25006
25007 void
25008 x_clear_end_of_line (int to_x)
25009 {
25010 struct frame *f;
25011 struct window *w = updated_window;
25012 int max_x, min_y, max_y;
25013 int from_x, from_y, to_y;
25014
25015 xassert (updated_window && updated_row);
25016 f = XFRAME (w->frame);
25017
25018 if (updated_row->full_width_p)
25019 max_x = WINDOW_TOTAL_WIDTH (w);
25020 else
25021 max_x = window_box_width (w, updated_area);
25022 max_y = window_text_bottom_y (w);
25023
25024 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25025 of window. For TO_X > 0, truncate to end of drawing area. */
25026 if (to_x == 0)
25027 return;
25028 else if (to_x < 0)
25029 to_x = max_x;
25030 else
25031 to_x = min (to_x, max_x);
25032
25033 to_y = min (max_y, output_cursor.y + updated_row->height);
25034
25035 /* Notice if the cursor will be cleared by this operation. */
25036 if (!updated_row->full_width_p)
25037 notice_overwritten_cursor (w, updated_area,
25038 output_cursor.x, -1,
25039 updated_row->y,
25040 MATRIX_ROW_BOTTOM_Y (updated_row));
25041
25042 from_x = output_cursor.x;
25043
25044 /* Translate to frame coordinates. */
25045 if (updated_row->full_width_p)
25046 {
25047 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25048 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25049 }
25050 else
25051 {
25052 int area_left = window_box_left (w, updated_area);
25053 from_x += area_left;
25054 to_x += area_left;
25055 }
25056
25057 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25058 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25059 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25060
25061 /* Prevent inadvertently clearing to end of the X window. */
25062 if (to_x > from_x && to_y > from_y)
25063 {
25064 BLOCK_INPUT;
25065 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25066 to_x - from_x, to_y - from_y);
25067 UNBLOCK_INPUT;
25068 }
25069 }
25070
25071 #endif /* HAVE_WINDOW_SYSTEM */
25072
25073
25074 \f
25075 /***********************************************************************
25076 Cursor types
25077 ***********************************************************************/
25078
25079 /* Value is the internal representation of the specified cursor type
25080 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25081 of the bar cursor. */
25082
25083 static enum text_cursor_kinds
25084 get_specified_cursor_type (Lisp_Object arg, int *width)
25085 {
25086 enum text_cursor_kinds type;
25087
25088 if (NILP (arg))
25089 return NO_CURSOR;
25090
25091 if (EQ (arg, Qbox))
25092 return FILLED_BOX_CURSOR;
25093
25094 if (EQ (arg, Qhollow))
25095 return HOLLOW_BOX_CURSOR;
25096
25097 if (EQ (arg, Qbar))
25098 {
25099 *width = 2;
25100 return BAR_CURSOR;
25101 }
25102
25103 if (CONSP (arg)
25104 && EQ (XCAR (arg), Qbar)
25105 && INTEGERP (XCDR (arg))
25106 && XINT (XCDR (arg)) >= 0)
25107 {
25108 *width = XINT (XCDR (arg));
25109 return BAR_CURSOR;
25110 }
25111
25112 if (EQ (arg, Qhbar))
25113 {
25114 *width = 2;
25115 return HBAR_CURSOR;
25116 }
25117
25118 if (CONSP (arg)
25119 && EQ (XCAR (arg), Qhbar)
25120 && INTEGERP (XCDR (arg))
25121 && XINT (XCDR (arg)) >= 0)
25122 {
25123 *width = XINT (XCDR (arg));
25124 return HBAR_CURSOR;
25125 }
25126
25127 /* Treat anything unknown as "hollow box cursor".
25128 It was bad to signal an error; people have trouble fixing
25129 .Xdefaults with Emacs, when it has something bad in it. */
25130 type = HOLLOW_BOX_CURSOR;
25131
25132 return type;
25133 }
25134
25135 /* Set the default cursor types for specified frame. */
25136 void
25137 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25138 {
25139 int width = 1;
25140 Lisp_Object tem;
25141
25142 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25143 FRAME_CURSOR_WIDTH (f) = width;
25144
25145 /* By default, set up the blink-off state depending on the on-state. */
25146
25147 tem = Fassoc (arg, Vblink_cursor_alist);
25148 if (!NILP (tem))
25149 {
25150 FRAME_BLINK_OFF_CURSOR (f)
25151 = get_specified_cursor_type (XCDR (tem), &width);
25152 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25153 }
25154 else
25155 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25156 }
25157
25158
25159 #ifdef HAVE_WINDOW_SYSTEM
25160
25161 /* Return the cursor we want to be displayed in window W. Return
25162 width of bar/hbar cursor through WIDTH arg. Return with
25163 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25164 (i.e. if the `system caret' should track this cursor).
25165
25166 In a mini-buffer window, we want the cursor only to appear if we
25167 are reading input from this window. For the selected window, we
25168 want the cursor type given by the frame parameter or buffer local
25169 setting of cursor-type. If explicitly marked off, draw no cursor.
25170 In all other cases, we want a hollow box cursor. */
25171
25172 static enum text_cursor_kinds
25173 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25174 int *active_cursor)
25175 {
25176 struct frame *f = XFRAME (w->frame);
25177 struct buffer *b = XBUFFER (w->buffer);
25178 int cursor_type = DEFAULT_CURSOR;
25179 Lisp_Object alt_cursor;
25180 int non_selected = 0;
25181
25182 *active_cursor = 1;
25183
25184 /* Echo area */
25185 if (cursor_in_echo_area
25186 && FRAME_HAS_MINIBUF_P (f)
25187 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25188 {
25189 if (w == XWINDOW (echo_area_window))
25190 {
25191 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25192 {
25193 *width = FRAME_CURSOR_WIDTH (f);
25194 return FRAME_DESIRED_CURSOR (f);
25195 }
25196 else
25197 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25198 }
25199
25200 *active_cursor = 0;
25201 non_selected = 1;
25202 }
25203
25204 /* Detect a nonselected window or nonselected frame. */
25205 else if (w != XWINDOW (f->selected_window)
25206 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25207 {
25208 *active_cursor = 0;
25209
25210 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25211 return NO_CURSOR;
25212
25213 non_selected = 1;
25214 }
25215
25216 /* Never display a cursor in a window in which cursor-type is nil. */
25217 if (NILP (BVAR (b, cursor_type)))
25218 return NO_CURSOR;
25219
25220 /* Get the normal cursor type for this window. */
25221 if (EQ (BVAR (b, cursor_type), Qt))
25222 {
25223 cursor_type = FRAME_DESIRED_CURSOR (f);
25224 *width = FRAME_CURSOR_WIDTH (f);
25225 }
25226 else
25227 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25228
25229 /* Use cursor-in-non-selected-windows instead
25230 for non-selected window or frame. */
25231 if (non_selected)
25232 {
25233 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25234 if (!EQ (Qt, alt_cursor))
25235 return get_specified_cursor_type (alt_cursor, width);
25236 /* t means modify the normal cursor type. */
25237 if (cursor_type == FILLED_BOX_CURSOR)
25238 cursor_type = HOLLOW_BOX_CURSOR;
25239 else if (cursor_type == BAR_CURSOR && *width > 1)
25240 --*width;
25241 return cursor_type;
25242 }
25243
25244 /* Use normal cursor if not blinked off. */
25245 if (!w->cursor_off_p)
25246 {
25247 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25248 {
25249 if (cursor_type == FILLED_BOX_CURSOR)
25250 {
25251 /* Using a block cursor on large images can be very annoying.
25252 So use a hollow cursor for "large" images.
25253 If image is not transparent (no mask), also use hollow cursor. */
25254 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25255 if (img != NULL && IMAGEP (img->spec))
25256 {
25257 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25258 where N = size of default frame font size.
25259 This should cover most of the "tiny" icons people may use. */
25260 if (!img->mask
25261 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25262 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25263 cursor_type = HOLLOW_BOX_CURSOR;
25264 }
25265 }
25266 else if (cursor_type != NO_CURSOR)
25267 {
25268 /* Display current only supports BOX and HOLLOW cursors for images.
25269 So for now, unconditionally use a HOLLOW cursor when cursor is
25270 not a solid box cursor. */
25271 cursor_type = HOLLOW_BOX_CURSOR;
25272 }
25273 }
25274 return cursor_type;
25275 }
25276
25277 /* Cursor is blinked off, so determine how to "toggle" it. */
25278
25279 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25280 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25281 return get_specified_cursor_type (XCDR (alt_cursor), width);
25282
25283 /* Then see if frame has specified a specific blink off cursor type. */
25284 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25285 {
25286 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25287 return FRAME_BLINK_OFF_CURSOR (f);
25288 }
25289
25290 #if 0
25291 /* Some people liked having a permanently visible blinking cursor,
25292 while others had very strong opinions against it. So it was
25293 decided to remove it. KFS 2003-09-03 */
25294
25295 /* Finally perform built-in cursor blinking:
25296 filled box <-> hollow box
25297 wide [h]bar <-> narrow [h]bar
25298 narrow [h]bar <-> no cursor
25299 other type <-> no cursor */
25300
25301 if (cursor_type == FILLED_BOX_CURSOR)
25302 return HOLLOW_BOX_CURSOR;
25303
25304 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25305 {
25306 *width = 1;
25307 return cursor_type;
25308 }
25309 #endif
25310
25311 return NO_CURSOR;
25312 }
25313
25314
25315 /* Notice when the text cursor of window W has been completely
25316 overwritten by a drawing operation that outputs glyphs in AREA
25317 starting at X0 and ending at X1 in the line starting at Y0 and
25318 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25319 the rest of the line after X0 has been written. Y coordinates
25320 are window-relative. */
25321
25322 static void
25323 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25324 int x0, int x1, int y0, int y1)
25325 {
25326 int cx0, cx1, cy0, cy1;
25327 struct glyph_row *row;
25328
25329 if (!w->phys_cursor_on_p)
25330 return;
25331 if (area != TEXT_AREA)
25332 return;
25333
25334 if (w->phys_cursor.vpos < 0
25335 || w->phys_cursor.vpos >= w->current_matrix->nrows
25336 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25337 !(row->enabled_p && row->displays_text_p)))
25338 return;
25339
25340 if (row->cursor_in_fringe_p)
25341 {
25342 row->cursor_in_fringe_p = 0;
25343 draw_fringe_bitmap (w, row, row->reversed_p);
25344 w->phys_cursor_on_p = 0;
25345 return;
25346 }
25347
25348 cx0 = w->phys_cursor.x;
25349 cx1 = cx0 + w->phys_cursor_width;
25350 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25351 return;
25352
25353 /* The cursor image will be completely removed from the
25354 screen if the output area intersects the cursor area in
25355 y-direction. When we draw in [y0 y1[, and some part of
25356 the cursor is at y < y0, that part must have been drawn
25357 before. When scrolling, the cursor is erased before
25358 actually scrolling, so we don't come here. When not
25359 scrolling, the rows above the old cursor row must have
25360 changed, and in this case these rows must have written
25361 over the cursor image.
25362
25363 Likewise if part of the cursor is below y1, with the
25364 exception of the cursor being in the first blank row at
25365 the buffer and window end because update_text_area
25366 doesn't draw that row. (Except when it does, but
25367 that's handled in update_text_area.) */
25368
25369 cy0 = w->phys_cursor.y;
25370 cy1 = cy0 + w->phys_cursor_height;
25371 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25372 return;
25373
25374 w->phys_cursor_on_p = 0;
25375 }
25376
25377 #endif /* HAVE_WINDOW_SYSTEM */
25378
25379 \f
25380 /************************************************************************
25381 Mouse Face
25382 ************************************************************************/
25383
25384 #ifdef HAVE_WINDOW_SYSTEM
25385
25386 /* EXPORT for RIF:
25387 Fix the display of area AREA of overlapping row ROW in window W
25388 with respect to the overlapping part OVERLAPS. */
25389
25390 void
25391 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25392 enum glyph_row_area area, int overlaps)
25393 {
25394 int i, x;
25395
25396 BLOCK_INPUT;
25397
25398 x = 0;
25399 for (i = 0; i < row->used[area];)
25400 {
25401 if (row->glyphs[area][i].overlaps_vertically_p)
25402 {
25403 int start = i, start_x = x;
25404
25405 do
25406 {
25407 x += row->glyphs[area][i].pixel_width;
25408 ++i;
25409 }
25410 while (i < row->used[area]
25411 && row->glyphs[area][i].overlaps_vertically_p);
25412
25413 draw_glyphs (w, start_x, row, area,
25414 start, i,
25415 DRAW_NORMAL_TEXT, overlaps);
25416 }
25417 else
25418 {
25419 x += row->glyphs[area][i].pixel_width;
25420 ++i;
25421 }
25422 }
25423
25424 UNBLOCK_INPUT;
25425 }
25426
25427
25428 /* EXPORT:
25429 Draw the cursor glyph of window W in glyph row ROW. See the
25430 comment of draw_glyphs for the meaning of HL. */
25431
25432 void
25433 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25434 enum draw_glyphs_face hl)
25435 {
25436 /* If cursor hpos is out of bounds, don't draw garbage. This can
25437 happen in mini-buffer windows when switching between echo area
25438 glyphs and mini-buffer. */
25439 if ((row->reversed_p
25440 ? (w->phys_cursor.hpos >= 0)
25441 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25442 {
25443 int on_p = w->phys_cursor_on_p;
25444 int x1;
25445 int hpos = w->phys_cursor.hpos;
25446
25447 /* When the window is hscrolled, cursor hpos can legitimately be
25448 out of bounds, but we draw the cursor at the corresponding
25449 window margin in that case. */
25450 if (!row->reversed_p && hpos < 0)
25451 hpos = 0;
25452 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25453 hpos = row->used[TEXT_AREA] - 1;
25454
25455 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25456 hl, 0);
25457 w->phys_cursor_on_p = on_p;
25458
25459 if (hl == DRAW_CURSOR)
25460 w->phys_cursor_width = x1 - w->phys_cursor.x;
25461 /* When we erase the cursor, and ROW is overlapped by other
25462 rows, make sure that these overlapping parts of other rows
25463 are redrawn. */
25464 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25465 {
25466 w->phys_cursor_width = x1 - w->phys_cursor.x;
25467
25468 if (row > w->current_matrix->rows
25469 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25470 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25471 OVERLAPS_ERASED_CURSOR);
25472
25473 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25474 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25475 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25476 OVERLAPS_ERASED_CURSOR);
25477 }
25478 }
25479 }
25480
25481
25482 /* EXPORT:
25483 Erase the image of a cursor of window W from the screen. */
25484
25485 void
25486 erase_phys_cursor (struct window *w)
25487 {
25488 struct frame *f = XFRAME (w->frame);
25489 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25490 int hpos = w->phys_cursor.hpos;
25491 int vpos = w->phys_cursor.vpos;
25492 int mouse_face_here_p = 0;
25493 struct glyph_matrix *active_glyphs = w->current_matrix;
25494 struct glyph_row *cursor_row;
25495 struct glyph *cursor_glyph;
25496 enum draw_glyphs_face hl;
25497
25498 /* No cursor displayed or row invalidated => nothing to do on the
25499 screen. */
25500 if (w->phys_cursor_type == NO_CURSOR)
25501 goto mark_cursor_off;
25502
25503 /* VPOS >= active_glyphs->nrows means that window has been resized.
25504 Don't bother to erase the cursor. */
25505 if (vpos >= active_glyphs->nrows)
25506 goto mark_cursor_off;
25507
25508 /* If row containing cursor is marked invalid, there is nothing we
25509 can do. */
25510 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25511 if (!cursor_row->enabled_p)
25512 goto mark_cursor_off;
25513
25514 /* If line spacing is > 0, old cursor may only be partially visible in
25515 window after split-window. So adjust visible height. */
25516 cursor_row->visible_height = min (cursor_row->visible_height,
25517 window_text_bottom_y (w) - cursor_row->y);
25518
25519 /* If row is completely invisible, don't attempt to delete a cursor which
25520 isn't there. This can happen if cursor is at top of a window, and
25521 we switch to a buffer with a header line in that window. */
25522 if (cursor_row->visible_height <= 0)
25523 goto mark_cursor_off;
25524
25525 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25526 if (cursor_row->cursor_in_fringe_p)
25527 {
25528 cursor_row->cursor_in_fringe_p = 0;
25529 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25530 goto mark_cursor_off;
25531 }
25532
25533 /* This can happen when the new row is shorter than the old one.
25534 In this case, either draw_glyphs or clear_end_of_line
25535 should have cleared the cursor. Note that we wouldn't be
25536 able to erase the cursor in this case because we don't have a
25537 cursor glyph at hand. */
25538 if ((cursor_row->reversed_p
25539 ? (w->phys_cursor.hpos < 0)
25540 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25541 goto mark_cursor_off;
25542
25543 /* When the window is hscrolled, cursor hpos can legitimately be out
25544 of bounds, but we draw the cursor at the corresponding window
25545 margin in that case. */
25546 if (!cursor_row->reversed_p && hpos < 0)
25547 hpos = 0;
25548 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25549 hpos = cursor_row->used[TEXT_AREA] - 1;
25550
25551 /* If the cursor is in the mouse face area, redisplay that when
25552 we clear the cursor. */
25553 if (! NILP (hlinfo->mouse_face_window)
25554 && coords_in_mouse_face_p (w, hpos, vpos)
25555 /* Don't redraw the cursor's spot in mouse face if it is at the
25556 end of a line (on a newline). The cursor appears there, but
25557 mouse highlighting does not. */
25558 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25559 mouse_face_here_p = 1;
25560
25561 /* Maybe clear the display under the cursor. */
25562 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25563 {
25564 int x, y, left_x;
25565 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25566 int width;
25567
25568 cursor_glyph = get_phys_cursor_glyph (w);
25569 if (cursor_glyph == NULL)
25570 goto mark_cursor_off;
25571
25572 width = cursor_glyph->pixel_width;
25573 left_x = window_box_left_offset (w, TEXT_AREA);
25574 x = w->phys_cursor.x;
25575 if (x < left_x)
25576 width -= left_x - x;
25577 width = min (width, window_box_width (w, TEXT_AREA) - x);
25578 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25579 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25580
25581 if (width > 0)
25582 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25583 }
25584
25585 /* Erase the cursor by redrawing the character underneath it. */
25586 if (mouse_face_here_p)
25587 hl = DRAW_MOUSE_FACE;
25588 else
25589 hl = DRAW_NORMAL_TEXT;
25590 draw_phys_cursor_glyph (w, cursor_row, hl);
25591
25592 mark_cursor_off:
25593 w->phys_cursor_on_p = 0;
25594 w->phys_cursor_type = NO_CURSOR;
25595 }
25596
25597
25598 /* EXPORT:
25599 Display or clear cursor of window W. If ON is zero, clear the
25600 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25601 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25602
25603 void
25604 display_and_set_cursor (struct window *w, int on,
25605 int hpos, int vpos, int x, int y)
25606 {
25607 struct frame *f = XFRAME (w->frame);
25608 int new_cursor_type;
25609 int new_cursor_width;
25610 int active_cursor;
25611 struct glyph_row *glyph_row;
25612 struct glyph *glyph;
25613
25614 /* This is pointless on invisible frames, and dangerous on garbaged
25615 windows and frames; in the latter case, the frame or window may
25616 be in the midst of changing its size, and x and y may be off the
25617 window. */
25618 if (! FRAME_VISIBLE_P (f)
25619 || FRAME_GARBAGED_P (f)
25620 || vpos >= w->current_matrix->nrows
25621 || hpos >= w->current_matrix->matrix_w)
25622 return;
25623
25624 /* If cursor is off and we want it off, return quickly. */
25625 if (!on && !w->phys_cursor_on_p)
25626 return;
25627
25628 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25629 /* If cursor row is not enabled, we don't really know where to
25630 display the cursor. */
25631 if (!glyph_row->enabled_p)
25632 {
25633 w->phys_cursor_on_p = 0;
25634 return;
25635 }
25636
25637 glyph = NULL;
25638 if (!glyph_row->exact_window_width_line_p
25639 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25640 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25641
25642 xassert (interrupt_input_blocked);
25643
25644 /* Set new_cursor_type to the cursor we want to be displayed. */
25645 new_cursor_type = get_window_cursor_type (w, glyph,
25646 &new_cursor_width, &active_cursor);
25647
25648 /* If cursor is currently being shown and we don't want it to be or
25649 it is in the wrong place, or the cursor type is not what we want,
25650 erase it. */
25651 if (w->phys_cursor_on_p
25652 && (!on
25653 || w->phys_cursor.x != x
25654 || w->phys_cursor.y != y
25655 || new_cursor_type != w->phys_cursor_type
25656 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25657 && new_cursor_width != w->phys_cursor_width)))
25658 erase_phys_cursor (w);
25659
25660 /* Don't check phys_cursor_on_p here because that flag is only set
25661 to zero in some cases where we know that the cursor has been
25662 completely erased, to avoid the extra work of erasing the cursor
25663 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25664 still not be visible, or it has only been partly erased. */
25665 if (on)
25666 {
25667 w->phys_cursor_ascent = glyph_row->ascent;
25668 w->phys_cursor_height = glyph_row->height;
25669
25670 /* Set phys_cursor_.* before x_draw_.* is called because some
25671 of them may need the information. */
25672 w->phys_cursor.x = x;
25673 w->phys_cursor.y = glyph_row->y;
25674 w->phys_cursor.hpos = hpos;
25675 w->phys_cursor.vpos = vpos;
25676 }
25677
25678 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25679 new_cursor_type, new_cursor_width,
25680 on, active_cursor);
25681 }
25682
25683
25684 /* Switch the display of W's cursor on or off, according to the value
25685 of ON. */
25686
25687 static void
25688 update_window_cursor (struct window *w, int on)
25689 {
25690 /* Don't update cursor in windows whose frame is in the process
25691 of being deleted. */
25692 if (w->current_matrix)
25693 {
25694 int hpos = w->phys_cursor.hpos;
25695 int vpos = w->phys_cursor.vpos;
25696 struct glyph_row *row;
25697
25698 if (vpos >= w->current_matrix->nrows
25699 || hpos >= w->current_matrix->matrix_w)
25700 return;
25701
25702 row = MATRIX_ROW (w->current_matrix, vpos);
25703
25704 /* When the window is hscrolled, cursor hpos can legitimately be
25705 out of bounds, but we draw the cursor at the corresponding
25706 window margin in that case. */
25707 if (!row->reversed_p && hpos < 0)
25708 hpos = 0;
25709 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25710 hpos = row->used[TEXT_AREA] - 1;
25711
25712 BLOCK_INPUT;
25713 display_and_set_cursor (w, on, hpos, vpos,
25714 w->phys_cursor.x, w->phys_cursor.y);
25715 UNBLOCK_INPUT;
25716 }
25717 }
25718
25719
25720 /* Call update_window_cursor with parameter ON_P on all leaf windows
25721 in the window tree rooted at W. */
25722
25723 static void
25724 update_cursor_in_window_tree (struct window *w, int on_p)
25725 {
25726 while (w)
25727 {
25728 if (!NILP (w->hchild))
25729 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25730 else if (!NILP (w->vchild))
25731 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25732 else
25733 update_window_cursor (w, on_p);
25734
25735 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25736 }
25737 }
25738
25739
25740 /* EXPORT:
25741 Display the cursor on window W, or clear it, according to ON_P.
25742 Don't change the cursor's position. */
25743
25744 void
25745 x_update_cursor (struct frame *f, int on_p)
25746 {
25747 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25748 }
25749
25750
25751 /* EXPORT:
25752 Clear the cursor of window W to background color, and mark the
25753 cursor as not shown. This is used when the text where the cursor
25754 is about to be rewritten. */
25755
25756 void
25757 x_clear_cursor (struct window *w)
25758 {
25759 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25760 update_window_cursor (w, 0);
25761 }
25762
25763 #endif /* HAVE_WINDOW_SYSTEM */
25764
25765 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25766 and MSDOS. */
25767 static void
25768 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25769 int start_hpos, int end_hpos,
25770 enum draw_glyphs_face draw)
25771 {
25772 #ifdef HAVE_WINDOW_SYSTEM
25773 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25774 {
25775 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25776 return;
25777 }
25778 #endif
25779 #if defined (HAVE_GPM) || defined (MSDOS)
25780 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25781 #endif
25782 }
25783
25784 /* Display the active region described by mouse_face_* according to DRAW. */
25785
25786 static void
25787 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25788 {
25789 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25790 struct frame *f = XFRAME (WINDOW_FRAME (w));
25791
25792 if (/* If window is in the process of being destroyed, don't bother
25793 to do anything. */
25794 w->current_matrix != NULL
25795 /* Don't update mouse highlight if hidden */
25796 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25797 /* Recognize when we are called to operate on rows that don't exist
25798 anymore. This can happen when a window is split. */
25799 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25800 {
25801 int phys_cursor_on_p = w->phys_cursor_on_p;
25802 struct glyph_row *row, *first, *last;
25803
25804 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25805 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25806
25807 for (row = first; row <= last && row->enabled_p; ++row)
25808 {
25809 int start_hpos, end_hpos, start_x;
25810
25811 /* For all but the first row, the highlight starts at column 0. */
25812 if (row == first)
25813 {
25814 /* R2L rows have BEG and END in reversed order, but the
25815 screen drawing geometry is always left to right. So
25816 we need to mirror the beginning and end of the
25817 highlighted area in R2L rows. */
25818 if (!row->reversed_p)
25819 {
25820 start_hpos = hlinfo->mouse_face_beg_col;
25821 start_x = hlinfo->mouse_face_beg_x;
25822 }
25823 else if (row == last)
25824 {
25825 start_hpos = hlinfo->mouse_face_end_col;
25826 start_x = hlinfo->mouse_face_end_x;
25827 }
25828 else
25829 {
25830 start_hpos = 0;
25831 start_x = 0;
25832 }
25833 }
25834 else if (row->reversed_p && row == last)
25835 {
25836 start_hpos = hlinfo->mouse_face_end_col;
25837 start_x = hlinfo->mouse_face_end_x;
25838 }
25839 else
25840 {
25841 start_hpos = 0;
25842 start_x = 0;
25843 }
25844
25845 if (row == last)
25846 {
25847 if (!row->reversed_p)
25848 end_hpos = hlinfo->mouse_face_end_col;
25849 else if (row == first)
25850 end_hpos = hlinfo->mouse_face_beg_col;
25851 else
25852 {
25853 end_hpos = row->used[TEXT_AREA];
25854 if (draw == DRAW_NORMAL_TEXT)
25855 row->fill_line_p = 1; /* Clear to end of line */
25856 }
25857 }
25858 else if (row->reversed_p && row == first)
25859 end_hpos = hlinfo->mouse_face_beg_col;
25860 else
25861 {
25862 end_hpos = row->used[TEXT_AREA];
25863 if (draw == DRAW_NORMAL_TEXT)
25864 row->fill_line_p = 1; /* Clear to end of line */
25865 }
25866
25867 if (end_hpos > start_hpos)
25868 {
25869 draw_row_with_mouse_face (w, start_x, row,
25870 start_hpos, end_hpos, draw);
25871
25872 row->mouse_face_p
25873 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25874 }
25875 }
25876
25877 #ifdef HAVE_WINDOW_SYSTEM
25878 /* When we've written over the cursor, arrange for it to
25879 be displayed again. */
25880 if (FRAME_WINDOW_P (f)
25881 && phys_cursor_on_p && !w->phys_cursor_on_p)
25882 {
25883 int hpos = w->phys_cursor.hpos;
25884
25885 /* When the window is hscrolled, cursor hpos can legitimately be
25886 out of bounds, but we draw the cursor at the corresponding
25887 window margin in that case. */
25888 if (!row->reversed_p && hpos < 0)
25889 hpos = 0;
25890 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25891 hpos = row->used[TEXT_AREA] - 1;
25892
25893 BLOCK_INPUT;
25894 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25895 w->phys_cursor.x, w->phys_cursor.y);
25896 UNBLOCK_INPUT;
25897 }
25898 #endif /* HAVE_WINDOW_SYSTEM */
25899 }
25900
25901 #ifdef HAVE_WINDOW_SYSTEM
25902 /* Change the mouse cursor. */
25903 if (FRAME_WINDOW_P (f))
25904 {
25905 if (draw == DRAW_NORMAL_TEXT
25906 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25907 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25908 else if (draw == DRAW_MOUSE_FACE)
25909 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25910 else
25911 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25912 }
25913 #endif /* HAVE_WINDOW_SYSTEM */
25914 }
25915
25916 /* EXPORT:
25917 Clear out the mouse-highlighted active region.
25918 Redraw it un-highlighted first. Value is non-zero if mouse
25919 face was actually drawn unhighlighted. */
25920
25921 int
25922 clear_mouse_face (Mouse_HLInfo *hlinfo)
25923 {
25924 int cleared = 0;
25925
25926 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25927 {
25928 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25929 cleared = 1;
25930 }
25931
25932 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25933 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25934 hlinfo->mouse_face_window = Qnil;
25935 hlinfo->mouse_face_overlay = Qnil;
25936 return cleared;
25937 }
25938
25939 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25940 within the mouse face on that window. */
25941 static int
25942 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25943 {
25944 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25945
25946 /* Quickly resolve the easy cases. */
25947 if (!(WINDOWP (hlinfo->mouse_face_window)
25948 && XWINDOW (hlinfo->mouse_face_window) == w))
25949 return 0;
25950 if (vpos < hlinfo->mouse_face_beg_row
25951 || vpos > hlinfo->mouse_face_end_row)
25952 return 0;
25953 if (vpos > hlinfo->mouse_face_beg_row
25954 && vpos < hlinfo->mouse_face_end_row)
25955 return 1;
25956
25957 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25958 {
25959 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25960 {
25961 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25962 return 1;
25963 }
25964 else if ((vpos == hlinfo->mouse_face_beg_row
25965 && hpos >= hlinfo->mouse_face_beg_col)
25966 || (vpos == hlinfo->mouse_face_end_row
25967 && hpos < hlinfo->mouse_face_end_col))
25968 return 1;
25969 }
25970 else
25971 {
25972 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25973 {
25974 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25975 return 1;
25976 }
25977 else if ((vpos == hlinfo->mouse_face_beg_row
25978 && hpos <= hlinfo->mouse_face_beg_col)
25979 || (vpos == hlinfo->mouse_face_end_row
25980 && hpos > hlinfo->mouse_face_end_col))
25981 return 1;
25982 }
25983 return 0;
25984 }
25985
25986
25987 /* EXPORT:
25988 Non-zero if physical cursor of window W is within mouse face. */
25989
25990 int
25991 cursor_in_mouse_face_p (struct window *w)
25992 {
25993 int hpos = w->phys_cursor.hpos;
25994 int vpos = w->phys_cursor.vpos;
25995 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25996
25997 /* When the window is hscrolled, cursor hpos can legitimately be out
25998 of bounds, but we draw the cursor at the corresponding window
25999 margin in that case. */
26000 if (!row->reversed_p && hpos < 0)
26001 hpos = 0;
26002 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26003 hpos = row->used[TEXT_AREA] - 1;
26004
26005 return coords_in_mouse_face_p (w, hpos, vpos);
26006 }
26007
26008
26009 \f
26010 /* Find the glyph rows START_ROW and END_ROW of window W that display
26011 characters between buffer positions START_CHARPOS and END_CHARPOS
26012 (excluding END_CHARPOS). DISP_STRING is a display string that
26013 covers these buffer positions. This is similar to
26014 row_containing_pos, but is more accurate when bidi reordering makes
26015 buffer positions change non-linearly with glyph rows. */
26016 static void
26017 rows_from_pos_range (struct window *w,
26018 EMACS_INT start_charpos, EMACS_INT end_charpos,
26019 Lisp_Object disp_string,
26020 struct glyph_row **start, struct glyph_row **end)
26021 {
26022 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26023 int last_y = window_text_bottom_y (w);
26024 struct glyph_row *row;
26025
26026 *start = NULL;
26027 *end = NULL;
26028
26029 while (!first->enabled_p
26030 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26031 first++;
26032
26033 /* Find the START row. */
26034 for (row = first;
26035 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26036 row++)
26037 {
26038 /* A row can potentially be the START row if the range of the
26039 characters it displays intersects the range
26040 [START_CHARPOS..END_CHARPOS). */
26041 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26042 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26043 /* See the commentary in row_containing_pos, for the
26044 explanation of the complicated way to check whether
26045 some position is beyond the end of the characters
26046 displayed by a row. */
26047 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26048 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26049 && !row->ends_at_zv_p
26050 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26051 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26052 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26053 && !row->ends_at_zv_p
26054 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26055 {
26056 /* Found a candidate row. Now make sure at least one of the
26057 glyphs it displays has a charpos from the range
26058 [START_CHARPOS..END_CHARPOS).
26059
26060 This is not obvious because bidi reordering could make
26061 buffer positions of a row be 1,2,3,102,101,100, and if we
26062 want to highlight characters in [50..60), we don't want
26063 this row, even though [50..60) does intersect [1..103),
26064 the range of character positions given by the row's start
26065 and end positions. */
26066 struct glyph *g = row->glyphs[TEXT_AREA];
26067 struct glyph *e = g + row->used[TEXT_AREA];
26068
26069 while (g < e)
26070 {
26071 if (((BUFFERP (g->object) || INTEGERP (g->object))
26072 && start_charpos <= g->charpos && g->charpos < end_charpos)
26073 /* A glyph that comes from DISP_STRING is by
26074 definition to be highlighted. */
26075 || EQ (g->object, disp_string))
26076 *start = row;
26077 g++;
26078 }
26079 if (*start)
26080 break;
26081 }
26082 }
26083
26084 /* Find the END row. */
26085 if (!*start
26086 /* If the last row is partially visible, start looking for END
26087 from that row, instead of starting from FIRST. */
26088 && !(row->enabled_p
26089 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26090 row = first;
26091 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26092 {
26093 struct glyph_row *next = row + 1;
26094 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26095
26096 if (!next->enabled_p
26097 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26098 /* The first row >= START whose range of displayed characters
26099 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26100 is the row END + 1. */
26101 || (start_charpos < next_start
26102 && end_charpos < next_start)
26103 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26104 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26105 && !next->ends_at_zv_p
26106 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26107 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26108 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26109 && !next->ends_at_zv_p
26110 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26111 {
26112 *end = row;
26113 break;
26114 }
26115 else
26116 {
26117 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26118 but none of the characters it displays are in the range, it is
26119 also END + 1. */
26120 struct glyph *g = next->glyphs[TEXT_AREA];
26121 struct glyph *s = g;
26122 struct glyph *e = g + next->used[TEXT_AREA];
26123
26124 while (g < e)
26125 {
26126 if (((BUFFERP (g->object) || INTEGERP (g->object))
26127 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26128 /* If the buffer position of the first glyph in
26129 the row is equal to END_CHARPOS, it means
26130 the last character to be highlighted is the
26131 newline of ROW, and we must consider NEXT as
26132 END, not END+1. */
26133 || (((!next->reversed_p && g == s)
26134 || (next->reversed_p && g == e - 1))
26135 && (g->charpos == end_charpos
26136 /* Special case for when NEXT is an
26137 empty line at ZV. */
26138 || (g->charpos == -1
26139 && !row->ends_at_zv_p
26140 && next_start == end_charpos)))))
26141 /* A glyph that comes from DISP_STRING is by
26142 definition to be highlighted. */
26143 || EQ (g->object, disp_string))
26144 break;
26145 g++;
26146 }
26147 if (g == e)
26148 {
26149 *end = row;
26150 break;
26151 }
26152 /* The first row that ends at ZV must be the last to be
26153 highlighted. */
26154 else if (next->ends_at_zv_p)
26155 {
26156 *end = next;
26157 break;
26158 }
26159 }
26160 }
26161 }
26162
26163 /* This function sets the mouse_face_* elements of HLINFO, assuming
26164 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26165 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26166 for the overlay or run of text properties specifying the mouse
26167 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26168 before-string and after-string that must also be highlighted.
26169 DISP_STRING, if non-nil, is a display string that may cover some
26170 or all of the highlighted text. */
26171
26172 static void
26173 mouse_face_from_buffer_pos (Lisp_Object window,
26174 Mouse_HLInfo *hlinfo,
26175 EMACS_INT mouse_charpos,
26176 EMACS_INT start_charpos,
26177 EMACS_INT end_charpos,
26178 Lisp_Object before_string,
26179 Lisp_Object after_string,
26180 Lisp_Object disp_string)
26181 {
26182 struct window *w = XWINDOW (window);
26183 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26184 struct glyph_row *r1, *r2;
26185 struct glyph *glyph, *end;
26186 EMACS_INT ignore, pos;
26187 int x;
26188
26189 xassert (NILP (disp_string) || STRINGP (disp_string));
26190 xassert (NILP (before_string) || STRINGP (before_string));
26191 xassert (NILP (after_string) || STRINGP (after_string));
26192
26193 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26194 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26195 if (r1 == NULL)
26196 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26197 /* If the before-string or display-string contains newlines,
26198 rows_from_pos_range skips to its last row. Move back. */
26199 if (!NILP (before_string) || !NILP (disp_string))
26200 {
26201 struct glyph_row *prev;
26202 while ((prev = r1 - 1, prev >= first)
26203 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26204 && prev->used[TEXT_AREA] > 0)
26205 {
26206 struct glyph *beg = prev->glyphs[TEXT_AREA];
26207 glyph = beg + prev->used[TEXT_AREA];
26208 while (--glyph >= beg && INTEGERP (glyph->object));
26209 if (glyph < beg
26210 || !(EQ (glyph->object, before_string)
26211 || EQ (glyph->object, disp_string)))
26212 break;
26213 r1 = prev;
26214 }
26215 }
26216 if (r2 == NULL)
26217 {
26218 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26219 hlinfo->mouse_face_past_end = 1;
26220 }
26221 else if (!NILP (after_string))
26222 {
26223 /* If the after-string has newlines, advance to its last row. */
26224 struct glyph_row *next;
26225 struct glyph_row *last
26226 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26227
26228 for (next = r2 + 1;
26229 next <= last
26230 && next->used[TEXT_AREA] > 0
26231 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26232 ++next)
26233 r2 = next;
26234 }
26235 /* The rest of the display engine assumes that mouse_face_beg_row is
26236 either above mouse_face_end_row or identical to it. But with
26237 bidi-reordered continued lines, the row for START_CHARPOS could
26238 be below the row for END_CHARPOS. If so, swap the rows and store
26239 them in correct order. */
26240 if (r1->y > r2->y)
26241 {
26242 struct glyph_row *tem = r2;
26243
26244 r2 = r1;
26245 r1 = tem;
26246 }
26247
26248 hlinfo->mouse_face_beg_y = r1->y;
26249 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26250 hlinfo->mouse_face_end_y = r2->y;
26251 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26252
26253 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26254 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26255 could be anywhere in the row and in any order. The strategy
26256 below is to find the leftmost and the rightmost glyph that
26257 belongs to either of these 3 strings, or whose position is
26258 between START_CHARPOS and END_CHARPOS, and highlight all the
26259 glyphs between those two. This may cover more than just the text
26260 between START_CHARPOS and END_CHARPOS if the range of characters
26261 strides the bidi level boundary, e.g. if the beginning is in R2L
26262 text while the end is in L2R text or vice versa. */
26263 if (!r1->reversed_p)
26264 {
26265 /* This row is in a left to right paragraph. Scan it left to
26266 right. */
26267 glyph = r1->glyphs[TEXT_AREA];
26268 end = glyph + r1->used[TEXT_AREA];
26269 x = r1->x;
26270
26271 /* Skip truncation glyphs at the start of the glyph row. */
26272 if (r1->displays_text_p)
26273 for (; glyph < end
26274 && INTEGERP (glyph->object)
26275 && glyph->charpos < 0;
26276 ++glyph)
26277 x += glyph->pixel_width;
26278
26279 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26280 or DISP_STRING, and the first glyph from buffer whose
26281 position is between START_CHARPOS and END_CHARPOS. */
26282 for (; glyph < end
26283 && !INTEGERP (glyph->object)
26284 && !EQ (glyph->object, disp_string)
26285 && !(BUFFERP (glyph->object)
26286 && (glyph->charpos >= start_charpos
26287 && glyph->charpos < end_charpos));
26288 ++glyph)
26289 {
26290 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26291 are present at buffer positions between START_CHARPOS and
26292 END_CHARPOS, or if they come from an overlay. */
26293 if (EQ (glyph->object, before_string))
26294 {
26295 pos = string_buffer_position (before_string,
26296 start_charpos);
26297 /* If pos == 0, it means before_string came from an
26298 overlay, not from a buffer position. */
26299 if (!pos || (pos >= start_charpos && pos < end_charpos))
26300 break;
26301 }
26302 else if (EQ (glyph->object, after_string))
26303 {
26304 pos = string_buffer_position (after_string, end_charpos);
26305 if (!pos || (pos >= start_charpos && pos < end_charpos))
26306 break;
26307 }
26308 x += glyph->pixel_width;
26309 }
26310 hlinfo->mouse_face_beg_x = x;
26311 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26312 }
26313 else
26314 {
26315 /* This row is in a right to left paragraph. Scan it right to
26316 left. */
26317 struct glyph *g;
26318
26319 end = r1->glyphs[TEXT_AREA] - 1;
26320 glyph = end + r1->used[TEXT_AREA];
26321
26322 /* Skip truncation glyphs at the start of the glyph row. */
26323 if (r1->displays_text_p)
26324 for (; glyph > end
26325 && INTEGERP (glyph->object)
26326 && glyph->charpos < 0;
26327 --glyph)
26328 ;
26329
26330 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26331 or DISP_STRING, and the first glyph from buffer whose
26332 position is between START_CHARPOS and END_CHARPOS. */
26333 for (; glyph > end
26334 && !INTEGERP (glyph->object)
26335 && !EQ (glyph->object, disp_string)
26336 && !(BUFFERP (glyph->object)
26337 && (glyph->charpos >= start_charpos
26338 && glyph->charpos < end_charpos));
26339 --glyph)
26340 {
26341 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26342 are present at buffer positions between START_CHARPOS and
26343 END_CHARPOS, or if they come from an overlay. */
26344 if (EQ (glyph->object, before_string))
26345 {
26346 pos = string_buffer_position (before_string, start_charpos);
26347 /* If pos == 0, it means before_string came from an
26348 overlay, not from a buffer position. */
26349 if (!pos || (pos >= start_charpos && pos < end_charpos))
26350 break;
26351 }
26352 else if (EQ (glyph->object, after_string))
26353 {
26354 pos = string_buffer_position (after_string, end_charpos);
26355 if (!pos || (pos >= start_charpos && pos < end_charpos))
26356 break;
26357 }
26358 }
26359
26360 glyph++; /* first glyph to the right of the highlighted area */
26361 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26362 x += g->pixel_width;
26363 hlinfo->mouse_face_beg_x = x;
26364 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26365 }
26366
26367 /* If the highlight ends in a different row, compute GLYPH and END
26368 for the end row. Otherwise, reuse the values computed above for
26369 the row where the highlight begins. */
26370 if (r2 != r1)
26371 {
26372 if (!r2->reversed_p)
26373 {
26374 glyph = r2->glyphs[TEXT_AREA];
26375 end = glyph + r2->used[TEXT_AREA];
26376 x = r2->x;
26377 }
26378 else
26379 {
26380 end = r2->glyphs[TEXT_AREA] - 1;
26381 glyph = end + r2->used[TEXT_AREA];
26382 }
26383 }
26384
26385 if (!r2->reversed_p)
26386 {
26387 /* Skip truncation and continuation glyphs near the end of the
26388 row, and also blanks and stretch glyphs inserted by
26389 extend_face_to_end_of_line. */
26390 while (end > glyph
26391 && INTEGERP ((end - 1)->object))
26392 --end;
26393 /* Scan the rest of the glyph row from the end, looking for the
26394 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26395 DISP_STRING, or whose position is between START_CHARPOS
26396 and END_CHARPOS */
26397 for (--end;
26398 end > glyph
26399 && !INTEGERP (end->object)
26400 && !EQ (end->object, disp_string)
26401 && !(BUFFERP (end->object)
26402 && (end->charpos >= start_charpos
26403 && end->charpos < end_charpos));
26404 --end)
26405 {
26406 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26407 are present at buffer positions between START_CHARPOS and
26408 END_CHARPOS, or if they come from an overlay. */
26409 if (EQ (end->object, before_string))
26410 {
26411 pos = string_buffer_position (before_string, start_charpos);
26412 if (!pos || (pos >= start_charpos && pos < end_charpos))
26413 break;
26414 }
26415 else if (EQ (end->object, after_string))
26416 {
26417 pos = string_buffer_position (after_string, end_charpos);
26418 if (!pos || (pos >= start_charpos && pos < end_charpos))
26419 break;
26420 }
26421 }
26422 /* Find the X coordinate of the last glyph to be highlighted. */
26423 for (; glyph <= end; ++glyph)
26424 x += glyph->pixel_width;
26425
26426 hlinfo->mouse_face_end_x = x;
26427 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26428 }
26429 else
26430 {
26431 /* Skip truncation and continuation glyphs near the end of the
26432 row, and also blanks and stretch glyphs inserted by
26433 extend_face_to_end_of_line. */
26434 x = r2->x;
26435 end++;
26436 while (end < glyph
26437 && INTEGERP (end->object))
26438 {
26439 x += end->pixel_width;
26440 ++end;
26441 }
26442 /* Scan the rest of the glyph row from the end, looking for the
26443 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26444 DISP_STRING, or whose position is between START_CHARPOS
26445 and END_CHARPOS */
26446 for ( ;
26447 end < glyph
26448 && !INTEGERP (end->object)
26449 && !EQ (end->object, disp_string)
26450 && !(BUFFERP (end->object)
26451 && (end->charpos >= start_charpos
26452 && end->charpos < end_charpos));
26453 ++end)
26454 {
26455 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26456 are present at buffer positions between START_CHARPOS and
26457 END_CHARPOS, or if they come from an overlay. */
26458 if (EQ (end->object, before_string))
26459 {
26460 pos = string_buffer_position (before_string, start_charpos);
26461 if (!pos || (pos >= start_charpos && pos < end_charpos))
26462 break;
26463 }
26464 else if (EQ (end->object, after_string))
26465 {
26466 pos = string_buffer_position (after_string, end_charpos);
26467 if (!pos || (pos >= start_charpos && pos < end_charpos))
26468 break;
26469 }
26470 x += end->pixel_width;
26471 }
26472 /* If we exited the above loop because we arrived at the last
26473 glyph of the row, and its buffer position is still not in
26474 range, it means the last character in range is the preceding
26475 newline. Bump the end column and x values to get past the
26476 last glyph. */
26477 if (end == glyph
26478 && BUFFERP (end->object)
26479 && (end->charpos < start_charpos
26480 || end->charpos >= end_charpos))
26481 {
26482 x += end->pixel_width;
26483 ++end;
26484 }
26485 hlinfo->mouse_face_end_x = x;
26486 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26487 }
26488
26489 hlinfo->mouse_face_window = window;
26490 hlinfo->mouse_face_face_id
26491 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26492 mouse_charpos + 1,
26493 !hlinfo->mouse_face_hidden, -1);
26494 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26495 }
26496
26497 /* The following function is not used anymore (replaced with
26498 mouse_face_from_string_pos), but I leave it here for the time
26499 being, in case someone would. */
26500
26501 #if 0 /* not used */
26502
26503 /* Find the position of the glyph for position POS in OBJECT in
26504 window W's current matrix, and return in *X, *Y the pixel
26505 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26506
26507 RIGHT_P non-zero means return the position of the right edge of the
26508 glyph, RIGHT_P zero means return the left edge position.
26509
26510 If no glyph for POS exists in the matrix, return the position of
26511 the glyph with the next smaller position that is in the matrix, if
26512 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26513 exists in the matrix, return the position of the glyph with the
26514 next larger position in OBJECT.
26515
26516 Value is non-zero if a glyph was found. */
26517
26518 static int
26519 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26520 int *hpos, int *vpos, int *x, int *y, int right_p)
26521 {
26522 int yb = window_text_bottom_y (w);
26523 struct glyph_row *r;
26524 struct glyph *best_glyph = NULL;
26525 struct glyph_row *best_row = NULL;
26526 int best_x = 0;
26527
26528 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26529 r->enabled_p && r->y < yb;
26530 ++r)
26531 {
26532 struct glyph *g = r->glyphs[TEXT_AREA];
26533 struct glyph *e = g + r->used[TEXT_AREA];
26534 int gx;
26535
26536 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26537 if (EQ (g->object, object))
26538 {
26539 if (g->charpos == pos)
26540 {
26541 best_glyph = g;
26542 best_x = gx;
26543 best_row = r;
26544 goto found;
26545 }
26546 else if (best_glyph == NULL
26547 || ((eabs (g->charpos - pos)
26548 < eabs (best_glyph->charpos - pos))
26549 && (right_p
26550 ? g->charpos < pos
26551 : g->charpos > pos)))
26552 {
26553 best_glyph = g;
26554 best_x = gx;
26555 best_row = r;
26556 }
26557 }
26558 }
26559
26560 found:
26561
26562 if (best_glyph)
26563 {
26564 *x = best_x;
26565 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26566
26567 if (right_p)
26568 {
26569 *x += best_glyph->pixel_width;
26570 ++*hpos;
26571 }
26572
26573 *y = best_row->y;
26574 *vpos = best_row - w->current_matrix->rows;
26575 }
26576
26577 return best_glyph != NULL;
26578 }
26579 #endif /* not used */
26580
26581 /* Find the positions of the first and the last glyphs in window W's
26582 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26583 (assumed to be a string), and return in HLINFO's mouse_face_*
26584 members the pixel and column/row coordinates of those glyphs. */
26585
26586 static void
26587 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26588 Lisp_Object object,
26589 EMACS_INT startpos, EMACS_INT endpos)
26590 {
26591 int yb = window_text_bottom_y (w);
26592 struct glyph_row *r;
26593 struct glyph *g, *e;
26594 int gx;
26595 int found = 0;
26596
26597 /* Find the glyph row with at least one position in the range
26598 [STARTPOS..ENDPOS], and the first glyph in that row whose
26599 position belongs to that range. */
26600 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26601 r->enabled_p && r->y < yb;
26602 ++r)
26603 {
26604 if (!r->reversed_p)
26605 {
26606 g = r->glyphs[TEXT_AREA];
26607 e = g + r->used[TEXT_AREA];
26608 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26609 if (EQ (g->object, object)
26610 && startpos <= g->charpos && g->charpos <= endpos)
26611 {
26612 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26613 hlinfo->mouse_face_beg_y = r->y;
26614 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26615 hlinfo->mouse_face_beg_x = gx;
26616 found = 1;
26617 break;
26618 }
26619 }
26620 else
26621 {
26622 struct glyph *g1;
26623
26624 e = r->glyphs[TEXT_AREA];
26625 g = e + r->used[TEXT_AREA];
26626 for ( ; g > e; --g)
26627 if (EQ ((g-1)->object, object)
26628 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26629 {
26630 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26631 hlinfo->mouse_face_beg_y = r->y;
26632 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26633 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26634 gx += g1->pixel_width;
26635 hlinfo->mouse_face_beg_x = gx;
26636 found = 1;
26637 break;
26638 }
26639 }
26640 if (found)
26641 break;
26642 }
26643
26644 if (!found)
26645 return;
26646
26647 /* Starting with the next row, look for the first row which does NOT
26648 include any glyphs whose positions are in the range. */
26649 for (++r; r->enabled_p && r->y < yb; ++r)
26650 {
26651 g = r->glyphs[TEXT_AREA];
26652 e = g + r->used[TEXT_AREA];
26653 found = 0;
26654 for ( ; g < e; ++g)
26655 if (EQ (g->object, object)
26656 && startpos <= g->charpos && g->charpos <= endpos)
26657 {
26658 found = 1;
26659 break;
26660 }
26661 if (!found)
26662 break;
26663 }
26664
26665 /* The highlighted region ends on the previous row. */
26666 r--;
26667
26668 /* Set the end row and its vertical pixel coordinate. */
26669 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26670 hlinfo->mouse_face_end_y = r->y;
26671
26672 /* Compute and set the end column and the end column's horizontal
26673 pixel coordinate. */
26674 if (!r->reversed_p)
26675 {
26676 g = r->glyphs[TEXT_AREA];
26677 e = g + r->used[TEXT_AREA];
26678 for ( ; e > g; --e)
26679 if (EQ ((e-1)->object, object)
26680 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26681 break;
26682 hlinfo->mouse_face_end_col = e - g;
26683
26684 for (gx = r->x; g < e; ++g)
26685 gx += g->pixel_width;
26686 hlinfo->mouse_face_end_x = gx;
26687 }
26688 else
26689 {
26690 e = r->glyphs[TEXT_AREA];
26691 g = e + r->used[TEXT_AREA];
26692 for (gx = r->x ; e < g; ++e)
26693 {
26694 if (EQ (e->object, object)
26695 && startpos <= e->charpos && e->charpos <= endpos)
26696 break;
26697 gx += e->pixel_width;
26698 }
26699 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26700 hlinfo->mouse_face_end_x = gx;
26701 }
26702 }
26703
26704 #ifdef HAVE_WINDOW_SYSTEM
26705
26706 /* See if position X, Y is within a hot-spot of an image. */
26707
26708 static int
26709 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26710 {
26711 if (!CONSP (hot_spot))
26712 return 0;
26713
26714 if (EQ (XCAR (hot_spot), Qrect))
26715 {
26716 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26717 Lisp_Object rect = XCDR (hot_spot);
26718 Lisp_Object tem;
26719 if (!CONSP (rect))
26720 return 0;
26721 if (!CONSP (XCAR (rect)))
26722 return 0;
26723 if (!CONSP (XCDR (rect)))
26724 return 0;
26725 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26726 return 0;
26727 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26728 return 0;
26729 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26730 return 0;
26731 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26732 return 0;
26733 return 1;
26734 }
26735 else if (EQ (XCAR (hot_spot), Qcircle))
26736 {
26737 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26738 Lisp_Object circ = XCDR (hot_spot);
26739 Lisp_Object lr, lx0, ly0;
26740 if (CONSP (circ)
26741 && CONSP (XCAR (circ))
26742 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26743 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26744 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26745 {
26746 double r = XFLOATINT (lr);
26747 double dx = XINT (lx0) - x;
26748 double dy = XINT (ly0) - y;
26749 return (dx * dx + dy * dy <= r * r);
26750 }
26751 }
26752 else if (EQ (XCAR (hot_spot), Qpoly))
26753 {
26754 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26755 if (VECTORP (XCDR (hot_spot)))
26756 {
26757 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26758 Lisp_Object *poly = v->contents;
26759 int n = v->header.size;
26760 int i;
26761 int inside = 0;
26762 Lisp_Object lx, ly;
26763 int x0, y0;
26764
26765 /* Need an even number of coordinates, and at least 3 edges. */
26766 if (n < 6 || n & 1)
26767 return 0;
26768
26769 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26770 If count is odd, we are inside polygon. Pixels on edges
26771 may or may not be included depending on actual geometry of the
26772 polygon. */
26773 if ((lx = poly[n-2], !INTEGERP (lx))
26774 || (ly = poly[n-1], !INTEGERP (lx)))
26775 return 0;
26776 x0 = XINT (lx), y0 = XINT (ly);
26777 for (i = 0; i < n; i += 2)
26778 {
26779 int x1 = x0, y1 = y0;
26780 if ((lx = poly[i], !INTEGERP (lx))
26781 || (ly = poly[i+1], !INTEGERP (ly)))
26782 return 0;
26783 x0 = XINT (lx), y0 = XINT (ly);
26784
26785 /* Does this segment cross the X line? */
26786 if (x0 >= x)
26787 {
26788 if (x1 >= x)
26789 continue;
26790 }
26791 else if (x1 < x)
26792 continue;
26793 if (y > y0 && y > y1)
26794 continue;
26795 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26796 inside = !inside;
26797 }
26798 return inside;
26799 }
26800 }
26801 return 0;
26802 }
26803
26804 Lisp_Object
26805 find_hot_spot (Lisp_Object map, int x, int y)
26806 {
26807 while (CONSP (map))
26808 {
26809 if (CONSP (XCAR (map))
26810 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26811 return XCAR (map);
26812 map = XCDR (map);
26813 }
26814
26815 return Qnil;
26816 }
26817
26818 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26819 3, 3, 0,
26820 doc: /* Lookup in image map MAP coordinates X and Y.
26821 An image map is an alist where each element has the format (AREA ID PLIST).
26822 An AREA is specified as either a rectangle, a circle, or a polygon:
26823 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26824 pixel coordinates of the upper left and bottom right corners.
26825 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26826 and the radius of the circle; r may be a float or integer.
26827 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26828 vector describes one corner in the polygon.
26829 Returns the alist element for the first matching AREA in MAP. */)
26830 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26831 {
26832 if (NILP (map))
26833 return Qnil;
26834
26835 CHECK_NUMBER (x);
26836 CHECK_NUMBER (y);
26837
26838 return find_hot_spot (map, XINT (x), XINT (y));
26839 }
26840
26841
26842 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26843 static void
26844 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26845 {
26846 /* Do not change cursor shape while dragging mouse. */
26847 if (!NILP (do_mouse_tracking))
26848 return;
26849
26850 if (!NILP (pointer))
26851 {
26852 if (EQ (pointer, Qarrow))
26853 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26854 else if (EQ (pointer, Qhand))
26855 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26856 else if (EQ (pointer, Qtext))
26857 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26858 else if (EQ (pointer, intern ("hdrag")))
26859 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26860 #ifdef HAVE_X_WINDOWS
26861 else if (EQ (pointer, intern ("vdrag")))
26862 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26863 #endif
26864 else if (EQ (pointer, intern ("hourglass")))
26865 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26866 else if (EQ (pointer, Qmodeline))
26867 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26868 else
26869 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26870 }
26871
26872 if (cursor != No_Cursor)
26873 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26874 }
26875
26876 #endif /* HAVE_WINDOW_SYSTEM */
26877
26878 /* Take proper action when mouse has moved to the mode or header line
26879 or marginal area AREA of window W, x-position X and y-position Y.
26880 X is relative to the start of the text display area of W, so the
26881 width of bitmap areas and scroll bars must be subtracted to get a
26882 position relative to the start of the mode line. */
26883
26884 static void
26885 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26886 enum window_part area)
26887 {
26888 struct window *w = XWINDOW (window);
26889 struct frame *f = XFRAME (w->frame);
26890 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26891 #ifdef HAVE_WINDOW_SYSTEM
26892 Display_Info *dpyinfo;
26893 #endif
26894 Cursor cursor = No_Cursor;
26895 Lisp_Object pointer = Qnil;
26896 int dx, dy, width, height;
26897 EMACS_INT charpos;
26898 Lisp_Object string, object = Qnil;
26899 Lisp_Object pos, help;
26900
26901 Lisp_Object mouse_face;
26902 int original_x_pixel = x;
26903 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26904 struct glyph_row *row;
26905
26906 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26907 {
26908 int x0;
26909 struct glyph *end;
26910
26911 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26912 returns them in row/column units! */
26913 string = mode_line_string (w, area, &x, &y, &charpos,
26914 &object, &dx, &dy, &width, &height);
26915
26916 row = (area == ON_MODE_LINE
26917 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26918 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26919
26920 /* Find the glyph under the mouse pointer. */
26921 if (row->mode_line_p && row->enabled_p)
26922 {
26923 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26924 end = glyph + row->used[TEXT_AREA];
26925
26926 for (x0 = original_x_pixel;
26927 glyph < end && x0 >= glyph->pixel_width;
26928 ++glyph)
26929 x0 -= glyph->pixel_width;
26930
26931 if (glyph >= end)
26932 glyph = NULL;
26933 }
26934 }
26935 else
26936 {
26937 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26938 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26939 returns them in row/column units! */
26940 string = marginal_area_string (w, area, &x, &y, &charpos,
26941 &object, &dx, &dy, &width, &height);
26942 }
26943
26944 help = Qnil;
26945
26946 #ifdef HAVE_WINDOW_SYSTEM
26947 if (IMAGEP (object))
26948 {
26949 Lisp_Object image_map, hotspot;
26950 if ((image_map = Fplist_get (XCDR (object), QCmap),
26951 !NILP (image_map))
26952 && (hotspot = find_hot_spot (image_map, dx, dy),
26953 CONSP (hotspot))
26954 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26955 {
26956 Lisp_Object plist;
26957
26958 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26959 If so, we could look for mouse-enter, mouse-leave
26960 properties in PLIST (and do something...). */
26961 hotspot = XCDR (hotspot);
26962 if (CONSP (hotspot)
26963 && (plist = XCAR (hotspot), CONSP (plist)))
26964 {
26965 pointer = Fplist_get (plist, Qpointer);
26966 if (NILP (pointer))
26967 pointer = Qhand;
26968 help = Fplist_get (plist, Qhelp_echo);
26969 if (!NILP (help))
26970 {
26971 help_echo_string = help;
26972 /* Is this correct? ++kfs */
26973 XSETWINDOW (help_echo_window, w);
26974 help_echo_object = w->buffer;
26975 help_echo_pos = charpos;
26976 }
26977 }
26978 }
26979 if (NILP (pointer))
26980 pointer = Fplist_get (XCDR (object), QCpointer);
26981 }
26982 #endif /* HAVE_WINDOW_SYSTEM */
26983
26984 if (STRINGP (string))
26985 {
26986 pos = make_number (charpos);
26987 /* If we're on a string with `help-echo' text property, arrange
26988 for the help to be displayed. This is done by setting the
26989 global variable help_echo_string to the help string. */
26990 if (NILP (help))
26991 {
26992 help = Fget_text_property (pos, Qhelp_echo, string);
26993 if (!NILP (help))
26994 {
26995 help_echo_string = help;
26996 XSETWINDOW (help_echo_window, w);
26997 help_echo_object = string;
26998 help_echo_pos = charpos;
26999 }
27000 }
27001
27002 #ifdef HAVE_WINDOW_SYSTEM
27003 if (FRAME_WINDOW_P (f))
27004 {
27005 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27006 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27007 if (NILP (pointer))
27008 pointer = Fget_text_property (pos, Qpointer, string);
27009
27010 /* Change the mouse pointer according to what is under X/Y. */
27011 if (NILP (pointer)
27012 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27013 {
27014 Lisp_Object map;
27015 map = Fget_text_property (pos, Qlocal_map, string);
27016 if (!KEYMAPP (map))
27017 map = Fget_text_property (pos, Qkeymap, string);
27018 if (!KEYMAPP (map))
27019 cursor = dpyinfo->vertical_scroll_bar_cursor;
27020 }
27021 }
27022 #endif
27023
27024 /* Change the mouse face according to what is under X/Y. */
27025 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27026 if (!NILP (mouse_face)
27027 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27028 && glyph)
27029 {
27030 Lisp_Object b, e;
27031
27032 struct glyph * tmp_glyph;
27033
27034 int gpos;
27035 int gseq_length;
27036 int total_pixel_width;
27037 EMACS_INT begpos, endpos, ignore;
27038
27039 int vpos, hpos;
27040
27041 b = Fprevious_single_property_change (make_number (charpos + 1),
27042 Qmouse_face, string, Qnil);
27043 if (NILP (b))
27044 begpos = 0;
27045 else
27046 begpos = XINT (b);
27047
27048 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27049 if (NILP (e))
27050 endpos = SCHARS (string);
27051 else
27052 endpos = XINT (e);
27053
27054 /* Calculate the glyph position GPOS of GLYPH in the
27055 displayed string, relative to the beginning of the
27056 highlighted part of the string.
27057
27058 Note: GPOS is different from CHARPOS. CHARPOS is the
27059 position of GLYPH in the internal string object. A mode
27060 line string format has structures which are converted to
27061 a flattened string by the Emacs Lisp interpreter. The
27062 internal string is an element of those structures. The
27063 displayed string is the flattened string. */
27064 tmp_glyph = row_start_glyph;
27065 while (tmp_glyph < glyph
27066 && (!(EQ (tmp_glyph->object, glyph->object)
27067 && begpos <= tmp_glyph->charpos
27068 && tmp_glyph->charpos < endpos)))
27069 tmp_glyph++;
27070 gpos = glyph - tmp_glyph;
27071
27072 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27073 the highlighted part of the displayed string to which
27074 GLYPH belongs. Note: GSEQ_LENGTH is different from
27075 SCHARS (STRING), because the latter returns the length of
27076 the internal string. */
27077 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27078 tmp_glyph > glyph
27079 && (!(EQ (tmp_glyph->object, glyph->object)
27080 && begpos <= tmp_glyph->charpos
27081 && tmp_glyph->charpos < endpos));
27082 tmp_glyph--)
27083 ;
27084 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27085
27086 /* Calculate the total pixel width of all the glyphs between
27087 the beginning of the highlighted area and GLYPH. */
27088 total_pixel_width = 0;
27089 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27090 total_pixel_width += tmp_glyph->pixel_width;
27091
27092 /* Pre calculation of re-rendering position. Note: X is in
27093 column units here, after the call to mode_line_string or
27094 marginal_area_string. */
27095 hpos = x - gpos;
27096 vpos = (area == ON_MODE_LINE
27097 ? (w->current_matrix)->nrows - 1
27098 : 0);
27099
27100 /* If GLYPH's position is included in the region that is
27101 already drawn in mouse face, we have nothing to do. */
27102 if ( EQ (window, hlinfo->mouse_face_window)
27103 && (!row->reversed_p
27104 ? (hlinfo->mouse_face_beg_col <= hpos
27105 && hpos < hlinfo->mouse_face_end_col)
27106 /* In R2L rows we swap BEG and END, see below. */
27107 : (hlinfo->mouse_face_end_col <= hpos
27108 && hpos < hlinfo->mouse_face_beg_col))
27109 && hlinfo->mouse_face_beg_row == vpos )
27110 return;
27111
27112 if (clear_mouse_face (hlinfo))
27113 cursor = No_Cursor;
27114
27115 if (!row->reversed_p)
27116 {
27117 hlinfo->mouse_face_beg_col = hpos;
27118 hlinfo->mouse_face_beg_x = original_x_pixel
27119 - (total_pixel_width + dx);
27120 hlinfo->mouse_face_end_col = hpos + gseq_length;
27121 hlinfo->mouse_face_end_x = 0;
27122 }
27123 else
27124 {
27125 /* In R2L rows, show_mouse_face expects BEG and END
27126 coordinates to be swapped. */
27127 hlinfo->mouse_face_end_col = hpos;
27128 hlinfo->mouse_face_end_x = original_x_pixel
27129 - (total_pixel_width + dx);
27130 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27131 hlinfo->mouse_face_beg_x = 0;
27132 }
27133
27134 hlinfo->mouse_face_beg_row = vpos;
27135 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27136 hlinfo->mouse_face_beg_y = 0;
27137 hlinfo->mouse_face_end_y = 0;
27138 hlinfo->mouse_face_past_end = 0;
27139 hlinfo->mouse_face_window = window;
27140
27141 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27142 charpos,
27143 0, 0, 0,
27144 &ignore,
27145 glyph->face_id,
27146 1);
27147 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27148
27149 if (NILP (pointer))
27150 pointer = Qhand;
27151 }
27152 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27153 clear_mouse_face (hlinfo);
27154 }
27155 #ifdef HAVE_WINDOW_SYSTEM
27156 if (FRAME_WINDOW_P (f))
27157 define_frame_cursor1 (f, cursor, pointer);
27158 #endif
27159 }
27160
27161
27162 /* EXPORT:
27163 Take proper action when the mouse has moved to position X, Y on
27164 frame F as regards highlighting characters that have mouse-face
27165 properties. Also de-highlighting chars where the mouse was before.
27166 X and Y can be negative or out of range. */
27167
27168 void
27169 note_mouse_highlight (struct frame *f, int x, int y)
27170 {
27171 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27172 enum window_part part = ON_NOTHING;
27173 Lisp_Object window;
27174 struct window *w;
27175 Cursor cursor = No_Cursor;
27176 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27177 struct buffer *b;
27178
27179 /* When a menu is active, don't highlight because this looks odd. */
27180 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27181 if (popup_activated ())
27182 return;
27183 #endif
27184
27185 if (NILP (Vmouse_highlight)
27186 || !f->glyphs_initialized_p
27187 || f->pointer_invisible)
27188 return;
27189
27190 hlinfo->mouse_face_mouse_x = x;
27191 hlinfo->mouse_face_mouse_y = y;
27192 hlinfo->mouse_face_mouse_frame = f;
27193
27194 if (hlinfo->mouse_face_defer)
27195 return;
27196
27197 if (gc_in_progress)
27198 {
27199 hlinfo->mouse_face_deferred_gc = 1;
27200 return;
27201 }
27202
27203 /* Which window is that in? */
27204 window = window_from_coordinates (f, x, y, &part, 1);
27205
27206 /* If displaying active text in another window, clear that. */
27207 if (! EQ (window, hlinfo->mouse_face_window)
27208 /* Also clear if we move out of text area in same window. */
27209 || (!NILP (hlinfo->mouse_face_window)
27210 && !NILP (window)
27211 && part != ON_TEXT
27212 && part != ON_MODE_LINE
27213 && part != ON_HEADER_LINE))
27214 clear_mouse_face (hlinfo);
27215
27216 /* Not on a window -> return. */
27217 if (!WINDOWP (window))
27218 return;
27219
27220 /* Reset help_echo_string. It will get recomputed below. */
27221 help_echo_string = Qnil;
27222
27223 /* Convert to window-relative pixel coordinates. */
27224 w = XWINDOW (window);
27225 frame_to_window_pixel_xy (w, &x, &y);
27226
27227 #ifdef HAVE_WINDOW_SYSTEM
27228 /* Handle tool-bar window differently since it doesn't display a
27229 buffer. */
27230 if (EQ (window, f->tool_bar_window))
27231 {
27232 note_tool_bar_highlight (f, x, y);
27233 return;
27234 }
27235 #endif
27236
27237 /* Mouse is on the mode, header line or margin? */
27238 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27239 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27240 {
27241 note_mode_line_or_margin_highlight (window, x, y, part);
27242 return;
27243 }
27244
27245 #ifdef HAVE_WINDOW_SYSTEM
27246 if (part == ON_VERTICAL_BORDER)
27247 {
27248 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27249 help_echo_string = build_string ("drag-mouse-1: resize");
27250 }
27251 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27252 || part == ON_SCROLL_BAR)
27253 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27254 else
27255 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27256 #endif
27257
27258 /* Are we in a window whose display is up to date?
27259 And verify the buffer's text has not changed. */
27260 b = XBUFFER (w->buffer);
27261 if (part == ON_TEXT
27262 && EQ (w->window_end_valid, w->buffer)
27263 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27264 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27265 {
27266 int hpos, vpos, dx, dy, area = LAST_AREA;
27267 EMACS_INT pos;
27268 struct glyph *glyph;
27269 Lisp_Object object;
27270 Lisp_Object mouse_face = Qnil, position;
27271 Lisp_Object *overlay_vec = NULL;
27272 ptrdiff_t i, noverlays;
27273 struct buffer *obuf;
27274 EMACS_INT obegv, ozv;
27275 int same_region;
27276
27277 /* Find the glyph under X/Y. */
27278 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27279
27280 #ifdef HAVE_WINDOW_SYSTEM
27281 /* Look for :pointer property on image. */
27282 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27283 {
27284 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27285 if (img != NULL && IMAGEP (img->spec))
27286 {
27287 Lisp_Object image_map, hotspot;
27288 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27289 !NILP (image_map))
27290 && (hotspot = find_hot_spot (image_map,
27291 glyph->slice.img.x + dx,
27292 glyph->slice.img.y + dy),
27293 CONSP (hotspot))
27294 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27295 {
27296 Lisp_Object plist;
27297
27298 /* Could check XCAR (hotspot) to see if we enter/leave
27299 this hot-spot.
27300 If so, we could look for mouse-enter, mouse-leave
27301 properties in PLIST (and do something...). */
27302 hotspot = XCDR (hotspot);
27303 if (CONSP (hotspot)
27304 && (plist = XCAR (hotspot), CONSP (plist)))
27305 {
27306 pointer = Fplist_get (plist, Qpointer);
27307 if (NILP (pointer))
27308 pointer = Qhand;
27309 help_echo_string = Fplist_get (plist, Qhelp_echo);
27310 if (!NILP (help_echo_string))
27311 {
27312 help_echo_window = window;
27313 help_echo_object = glyph->object;
27314 help_echo_pos = glyph->charpos;
27315 }
27316 }
27317 }
27318 if (NILP (pointer))
27319 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27320 }
27321 }
27322 #endif /* HAVE_WINDOW_SYSTEM */
27323
27324 /* Clear mouse face if X/Y not over text. */
27325 if (glyph == NULL
27326 || area != TEXT_AREA
27327 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27328 /* Glyph's OBJECT is an integer for glyphs inserted by the
27329 display engine for its internal purposes, like truncation
27330 and continuation glyphs and blanks beyond the end of
27331 line's text on text terminals. If we are over such a
27332 glyph, we are not over any text. */
27333 || INTEGERP (glyph->object)
27334 /* R2L rows have a stretch glyph at their front, which
27335 stands for no text, whereas L2R rows have no glyphs at
27336 all beyond the end of text. Treat such stretch glyphs
27337 like we do with NULL glyphs in L2R rows. */
27338 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27339 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27340 && glyph->type == STRETCH_GLYPH
27341 && glyph->avoid_cursor_p))
27342 {
27343 if (clear_mouse_face (hlinfo))
27344 cursor = No_Cursor;
27345 #ifdef HAVE_WINDOW_SYSTEM
27346 if (FRAME_WINDOW_P (f) && NILP (pointer))
27347 {
27348 if (area != TEXT_AREA)
27349 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27350 else
27351 pointer = Vvoid_text_area_pointer;
27352 }
27353 #endif
27354 goto set_cursor;
27355 }
27356
27357 pos = glyph->charpos;
27358 object = glyph->object;
27359 if (!STRINGP (object) && !BUFFERP (object))
27360 goto set_cursor;
27361
27362 /* If we get an out-of-range value, return now; avoid an error. */
27363 if (BUFFERP (object) && pos > BUF_Z (b))
27364 goto set_cursor;
27365
27366 /* Make the window's buffer temporarily current for
27367 overlays_at and compute_char_face. */
27368 obuf = current_buffer;
27369 current_buffer = b;
27370 obegv = BEGV;
27371 ozv = ZV;
27372 BEGV = BEG;
27373 ZV = Z;
27374
27375 /* Is this char mouse-active or does it have help-echo? */
27376 position = make_number (pos);
27377
27378 if (BUFFERP (object))
27379 {
27380 /* Put all the overlays we want in a vector in overlay_vec. */
27381 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27382 /* Sort overlays into increasing priority order. */
27383 noverlays = sort_overlays (overlay_vec, noverlays, w);
27384 }
27385 else
27386 noverlays = 0;
27387
27388 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27389
27390 if (same_region)
27391 cursor = No_Cursor;
27392
27393 /* Check mouse-face highlighting. */
27394 if (! same_region
27395 /* If there exists an overlay with mouse-face overlapping
27396 the one we are currently highlighting, we have to
27397 check if we enter the overlapping overlay, and then
27398 highlight only that. */
27399 || (OVERLAYP (hlinfo->mouse_face_overlay)
27400 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27401 {
27402 /* Find the highest priority overlay with a mouse-face. */
27403 Lisp_Object overlay = Qnil;
27404 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27405 {
27406 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27407 if (!NILP (mouse_face))
27408 overlay = overlay_vec[i];
27409 }
27410
27411 /* If we're highlighting the same overlay as before, there's
27412 no need to do that again. */
27413 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27414 goto check_help_echo;
27415 hlinfo->mouse_face_overlay = overlay;
27416
27417 /* Clear the display of the old active region, if any. */
27418 if (clear_mouse_face (hlinfo))
27419 cursor = No_Cursor;
27420
27421 /* If no overlay applies, get a text property. */
27422 if (NILP (overlay))
27423 mouse_face = Fget_text_property (position, Qmouse_face, object);
27424
27425 /* Next, compute the bounds of the mouse highlighting and
27426 display it. */
27427 if (!NILP (mouse_face) && STRINGP (object))
27428 {
27429 /* The mouse-highlighting comes from a display string
27430 with a mouse-face. */
27431 Lisp_Object s, e;
27432 EMACS_INT ignore;
27433
27434 s = Fprevious_single_property_change
27435 (make_number (pos + 1), Qmouse_face, object, Qnil);
27436 e = Fnext_single_property_change
27437 (position, Qmouse_face, object, Qnil);
27438 if (NILP (s))
27439 s = make_number (0);
27440 if (NILP (e))
27441 e = make_number (SCHARS (object) - 1);
27442 mouse_face_from_string_pos (w, hlinfo, object,
27443 XINT (s), XINT (e));
27444 hlinfo->mouse_face_past_end = 0;
27445 hlinfo->mouse_face_window = window;
27446 hlinfo->mouse_face_face_id
27447 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27448 glyph->face_id, 1);
27449 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27450 cursor = No_Cursor;
27451 }
27452 else
27453 {
27454 /* The mouse-highlighting, if any, comes from an overlay
27455 or text property in the buffer. */
27456 Lisp_Object buffer IF_LINT (= Qnil);
27457 Lisp_Object disp_string IF_LINT (= Qnil);
27458
27459 if (STRINGP (object))
27460 {
27461 /* If we are on a display string with no mouse-face,
27462 check if the text under it has one. */
27463 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27464 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27465 pos = string_buffer_position (object, start);
27466 if (pos > 0)
27467 {
27468 mouse_face = get_char_property_and_overlay
27469 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27470 buffer = w->buffer;
27471 disp_string = object;
27472 }
27473 }
27474 else
27475 {
27476 buffer = object;
27477 disp_string = Qnil;
27478 }
27479
27480 if (!NILP (mouse_face))
27481 {
27482 Lisp_Object before, after;
27483 Lisp_Object before_string, after_string;
27484 /* To correctly find the limits of mouse highlight
27485 in a bidi-reordered buffer, we must not use the
27486 optimization of limiting the search in
27487 previous-single-property-change and
27488 next-single-property-change, because
27489 rows_from_pos_range needs the real start and end
27490 positions to DTRT in this case. That's because
27491 the first row visible in a window does not
27492 necessarily display the character whose position
27493 is the smallest. */
27494 Lisp_Object lim1 =
27495 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27496 ? Fmarker_position (w->start)
27497 : Qnil;
27498 Lisp_Object lim2 =
27499 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27500 ? make_number (BUF_Z (XBUFFER (buffer))
27501 - XFASTINT (w->window_end_pos))
27502 : Qnil;
27503
27504 if (NILP (overlay))
27505 {
27506 /* Handle the text property case. */
27507 before = Fprevious_single_property_change
27508 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27509 after = Fnext_single_property_change
27510 (make_number (pos), Qmouse_face, buffer, lim2);
27511 before_string = after_string = Qnil;
27512 }
27513 else
27514 {
27515 /* Handle the overlay case. */
27516 before = Foverlay_start (overlay);
27517 after = Foverlay_end (overlay);
27518 before_string = Foverlay_get (overlay, Qbefore_string);
27519 after_string = Foverlay_get (overlay, Qafter_string);
27520
27521 if (!STRINGP (before_string)) before_string = Qnil;
27522 if (!STRINGP (after_string)) after_string = Qnil;
27523 }
27524
27525 mouse_face_from_buffer_pos (window, hlinfo, pos,
27526 NILP (before)
27527 ? 1
27528 : XFASTINT (before),
27529 NILP (after)
27530 ? BUF_Z (XBUFFER (buffer))
27531 : XFASTINT (after),
27532 before_string, after_string,
27533 disp_string);
27534 cursor = No_Cursor;
27535 }
27536 }
27537 }
27538
27539 check_help_echo:
27540
27541 /* Look for a `help-echo' property. */
27542 if (NILP (help_echo_string)) {
27543 Lisp_Object help, overlay;
27544
27545 /* Check overlays first. */
27546 help = overlay = Qnil;
27547 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27548 {
27549 overlay = overlay_vec[i];
27550 help = Foverlay_get (overlay, Qhelp_echo);
27551 }
27552
27553 if (!NILP (help))
27554 {
27555 help_echo_string = help;
27556 help_echo_window = window;
27557 help_echo_object = overlay;
27558 help_echo_pos = pos;
27559 }
27560 else
27561 {
27562 Lisp_Object obj = glyph->object;
27563 EMACS_INT charpos = glyph->charpos;
27564
27565 /* Try text properties. */
27566 if (STRINGP (obj)
27567 && charpos >= 0
27568 && charpos < SCHARS (obj))
27569 {
27570 help = Fget_text_property (make_number (charpos),
27571 Qhelp_echo, obj);
27572 if (NILP (help))
27573 {
27574 /* If the string itself doesn't specify a help-echo,
27575 see if the buffer text ``under'' it does. */
27576 struct glyph_row *r
27577 = MATRIX_ROW (w->current_matrix, vpos);
27578 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27579 EMACS_INT p = string_buffer_position (obj, start);
27580 if (p > 0)
27581 {
27582 help = Fget_char_property (make_number (p),
27583 Qhelp_echo, w->buffer);
27584 if (!NILP (help))
27585 {
27586 charpos = p;
27587 obj = w->buffer;
27588 }
27589 }
27590 }
27591 }
27592 else if (BUFFERP (obj)
27593 && charpos >= BEGV
27594 && charpos < ZV)
27595 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27596 obj);
27597
27598 if (!NILP (help))
27599 {
27600 help_echo_string = help;
27601 help_echo_window = window;
27602 help_echo_object = obj;
27603 help_echo_pos = charpos;
27604 }
27605 }
27606 }
27607
27608 #ifdef HAVE_WINDOW_SYSTEM
27609 /* Look for a `pointer' property. */
27610 if (FRAME_WINDOW_P (f) && NILP (pointer))
27611 {
27612 /* Check overlays first. */
27613 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27614 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27615
27616 if (NILP (pointer))
27617 {
27618 Lisp_Object obj = glyph->object;
27619 EMACS_INT charpos = glyph->charpos;
27620
27621 /* Try text properties. */
27622 if (STRINGP (obj)
27623 && charpos >= 0
27624 && charpos < SCHARS (obj))
27625 {
27626 pointer = Fget_text_property (make_number (charpos),
27627 Qpointer, obj);
27628 if (NILP (pointer))
27629 {
27630 /* If the string itself doesn't specify a pointer,
27631 see if the buffer text ``under'' it does. */
27632 struct glyph_row *r
27633 = MATRIX_ROW (w->current_matrix, vpos);
27634 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27635 EMACS_INT p = string_buffer_position (obj, start);
27636 if (p > 0)
27637 pointer = Fget_char_property (make_number (p),
27638 Qpointer, w->buffer);
27639 }
27640 }
27641 else if (BUFFERP (obj)
27642 && charpos >= BEGV
27643 && charpos < ZV)
27644 pointer = Fget_text_property (make_number (charpos),
27645 Qpointer, obj);
27646 }
27647 }
27648 #endif /* HAVE_WINDOW_SYSTEM */
27649
27650 BEGV = obegv;
27651 ZV = ozv;
27652 current_buffer = obuf;
27653 }
27654
27655 set_cursor:
27656
27657 #ifdef HAVE_WINDOW_SYSTEM
27658 if (FRAME_WINDOW_P (f))
27659 define_frame_cursor1 (f, cursor, pointer);
27660 #else
27661 /* This is here to prevent a compiler error, about "label at end of
27662 compound statement". */
27663 return;
27664 #endif
27665 }
27666
27667
27668 /* EXPORT for RIF:
27669 Clear any mouse-face on window W. This function is part of the
27670 redisplay interface, and is called from try_window_id and similar
27671 functions to ensure the mouse-highlight is off. */
27672
27673 void
27674 x_clear_window_mouse_face (struct window *w)
27675 {
27676 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27677 Lisp_Object window;
27678
27679 BLOCK_INPUT;
27680 XSETWINDOW (window, w);
27681 if (EQ (window, hlinfo->mouse_face_window))
27682 clear_mouse_face (hlinfo);
27683 UNBLOCK_INPUT;
27684 }
27685
27686
27687 /* EXPORT:
27688 Just discard the mouse face information for frame F, if any.
27689 This is used when the size of F is changed. */
27690
27691 void
27692 cancel_mouse_face (struct frame *f)
27693 {
27694 Lisp_Object window;
27695 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27696
27697 window = hlinfo->mouse_face_window;
27698 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27699 {
27700 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27701 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27702 hlinfo->mouse_face_window = Qnil;
27703 }
27704 }
27705
27706
27707 \f
27708 /***********************************************************************
27709 Exposure Events
27710 ***********************************************************************/
27711
27712 #ifdef HAVE_WINDOW_SYSTEM
27713
27714 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27715 which intersects rectangle R. R is in window-relative coordinates. */
27716
27717 static void
27718 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27719 enum glyph_row_area area)
27720 {
27721 struct glyph *first = row->glyphs[area];
27722 struct glyph *end = row->glyphs[area] + row->used[area];
27723 struct glyph *last;
27724 int first_x, start_x, x;
27725
27726 if (area == TEXT_AREA && row->fill_line_p)
27727 /* If row extends face to end of line write the whole line. */
27728 draw_glyphs (w, 0, row, area,
27729 0, row->used[area],
27730 DRAW_NORMAL_TEXT, 0);
27731 else
27732 {
27733 /* Set START_X to the window-relative start position for drawing glyphs of
27734 AREA. The first glyph of the text area can be partially visible.
27735 The first glyphs of other areas cannot. */
27736 start_x = window_box_left_offset (w, area);
27737 x = start_x;
27738 if (area == TEXT_AREA)
27739 x += row->x;
27740
27741 /* Find the first glyph that must be redrawn. */
27742 while (first < end
27743 && x + first->pixel_width < r->x)
27744 {
27745 x += first->pixel_width;
27746 ++first;
27747 }
27748
27749 /* Find the last one. */
27750 last = first;
27751 first_x = x;
27752 while (last < end
27753 && x < r->x + r->width)
27754 {
27755 x += last->pixel_width;
27756 ++last;
27757 }
27758
27759 /* Repaint. */
27760 if (last > first)
27761 draw_glyphs (w, first_x - start_x, row, area,
27762 first - row->glyphs[area], last - row->glyphs[area],
27763 DRAW_NORMAL_TEXT, 0);
27764 }
27765 }
27766
27767
27768 /* Redraw the parts of the glyph row ROW on window W intersecting
27769 rectangle R. R is in window-relative coordinates. Value is
27770 non-zero if mouse-face was overwritten. */
27771
27772 static int
27773 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27774 {
27775 xassert (row->enabled_p);
27776
27777 if (row->mode_line_p || w->pseudo_window_p)
27778 draw_glyphs (w, 0, row, TEXT_AREA,
27779 0, row->used[TEXT_AREA],
27780 DRAW_NORMAL_TEXT, 0);
27781 else
27782 {
27783 if (row->used[LEFT_MARGIN_AREA])
27784 expose_area (w, row, r, LEFT_MARGIN_AREA);
27785 if (row->used[TEXT_AREA])
27786 expose_area (w, row, r, TEXT_AREA);
27787 if (row->used[RIGHT_MARGIN_AREA])
27788 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27789 draw_row_fringe_bitmaps (w, row);
27790 }
27791
27792 return row->mouse_face_p;
27793 }
27794
27795
27796 /* Redraw those parts of glyphs rows during expose event handling that
27797 overlap other rows. Redrawing of an exposed line writes over parts
27798 of lines overlapping that exposed line; this function fixes that.
27799
27800 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27801 row in W's current matrix that is exposed and overlaps other rows.
27802 LAST_OVERLAPPING_ROW is the last such row. */
27803
27804 static void
27805 expose_overlaps (struct window *w,
27806 struct glyph_row *first_overlapping_row,
27807 struct glyph_row *last_overlapping_row,
27808 XRectangle *r)
27809 {
27810 struct glyph_row *row;
27811
27812 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27813 if (row->overlapping_p)
27814 {
27815 xassert (row->enabled_p && !row->mode_line_p);
27816
27817 row->clip = r;
27818 if (row->used[LEFT_MARGIN_AREA])
27819 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27820
27821 if (row->used[TEXT_AREA])
27822 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27823
27824 if (row->used[RIGHT_MARGIN_AREA])
27825 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27826 row->clip = NULL;
27827 }
27828 }
27829
27830
27831 /* Return non-zero if W's cursor intersects rectangle R. */
27832
27833 static int
27834 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27835 {
27836 XRectangle cr, result;
27837 struct glyph *cursor_glyph;
27838 struct glyph_row *row;
27839
27840 if (w->phys_cursor.vpos >= 0
27841 && w->phys_cursor.vpos < w->current_matrix->nrows
27842 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27843 row->enabled_p)
27844 && row->cursor_in_fringe_p)
27845 {
27846 /* Cursor is in the fringe. */
27847 cr.x = window_box_right_offset (w,
27848 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27849 ? RIGHT_MARGIN_AREA
27850 : TEXT_AREA));
27851 cr.y = row->y;
27852 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27853 cr.height = row->height;
27854 return x_intersect_rectangles (&cr, r, &result);
27855 }
27856
27857 cursor_glyph = get_phys_cursor_glyph (w);
27858 if (cursor_glyph)
27859 {
27860 /* r is relative to W's box, but w->phys_cursor.x is relative
27861 to left edge of W's TEXT area. Adjust it. */
27862 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27863 cr.y = w->phys_cursor.y;
27864 cr.width = cursor_glyph->pixel_width;
27865 cr.height = w->phys_cursor_height;
27866 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27867 I assume the effect is the same -- and this is portable. */
27868 return x_intersect_rectangles (&cr, r, &result);
27869 }
27870 /* If we don't understand the format, pretend we're not in the hot-spot. */
27871 return 0;
27872 }
27873
27874
27875 /* EXPORT:
27876 Draw a vertical window border to the right of window W if W doesn't
27877 have vertical scroll bars. */
27878
27879 void
27880 x_draw_vertical_border (struct window *w)
27881 {
27882 struct frame *f = XFRAME (WINDOW_FRAME (w));
27883
27884 /* We could do better, if we knew what type of scroll-bar the adjacent
27885 windows (on either side) have... But we don't :-(
27886 However, I think this works ok. ++KFS 2003-04-25 */
27887
27888 /* Redraw borders between horizontally adjacent windows. Don't
27889 do it for frames with vertical scroll bars because either the
27890 right scroll bar of a window, or the left scroll bar of its
27891 neighbor will suffice as a border. */
27892 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27893 return;
27894
27895 if (!WINDOW_RIGHTMOST_P (w)
27896 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27897 {
27898 int x0, x1, y0, y1;
27899
27900 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27901 y1 -= 1;
27902
27903 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27904 x1 -= 1;
27905
27906 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27907 }
27908 else if (!WINDOW_LEFTMOST_P (w)
27909 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27910 {
27911 int x0, x1, y0, y1;
27912
27913 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27914 y1 -= 1;
27915
27916 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27917 x0 -= 1;
27918
27919 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27920 }
27921 }
27922
27923
27924 /* Redraw the part of window W intersection rectangle FR. Pixel
27925 coordinates in FR are frame-relative. Call this function with
27926 input blocked. Value is non-zero if the exposure overwrites
27927 mouse-face. */
27928
27929 static int
27930 expose_window (struct window *w, XRectangle *fr)
27931 {
27932 struct frame *f = XFRAME (w->frame);
27933 XRectangle wr, r;
27934 int mouse_face_overwritten_p = 0;
27935
27936 /* If window is not yet fully initialized, do nothing. This can
27937 happen when toolkit scroll bars are used and a window is split.
27938 Reconfiguring the scroll bar will generate an expose for a newly
27939 created window. */
27940 if (w->current_matrix == NULL)
27941 return 0;
27942
27943 /* When we're currently updating the window, display and current
27944 matrix usually don't agree. Arrange for a thorough display
27945 later. */
27946 if (w == updated_window)
27947 {
27948 SET_FRAME_GARBAGED (f);
27949 return 0;
27950 }
27951
27952 /* Frame-relative pixel rectangle of W. */
27953 wr.x = WINDOW_LEFT_EDGE_X (w);
27954 wr.y = WINDOW_TOP_EDGE_Y (w);
27955 wr.width = WINDOW_TOTAL_WIDTH (w);
27956 wr.height = WINDOW_TOTAL_HEIGHT (w);
27957
27958 if (x_intersect_rectangles (fr, &wr, &r))
27959 {
27960 int yb = window_text_bottom_y (w);
27961 struct glyph_row *row;
27962 int cursor_cleared_p, phys_cursor_on_p;
27963 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27964
27965 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27966 r.x, r.y, r.width, r.height));
27967
27968 /* Convert to window coordinates. */
27969 r.x -= WINDOW_LEFT_EDGE_X (w);
27970 r.y -= WINDOW_TOP_EDGE_Y (w);
27971
27972 /* Turn off the cursor. */
27973 if (!w->pseudo_window_p
27974 && phys_cursor_in_rect_p (w, &r))
27975 {
27976 x_clear_cursor (w);
27977 cursor_cleared_p = 1;
27978 }
27979 else
27980 cursor_cleared_p = 0;
27981
27982 /* If the row containing the cursor extends face to end of line,
27983 then expose_area might overwrite the cursor outside the
27984 rectangle and thus notice_overwritten_cursor might clear
27985 w->phys_cursor_on_p. We remember the original value and
27986 check later if it is changed. */
27987 phys_cursor_on_p = w->phys_cursor_on_p;
27988
27989 /* Update lines intersecting rectangle R. */
27990 first_overlapping_row = last_overlapping_row = NULL;
27991 for (row = w->current_matrix->rows;
27992 row->enabled_p;
27993 ++row)
27994 {
27995 int y0 = row->y;
27996 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27997
27998 if ((y0 >= r.y && y0 < r.y + r.height)
27999 || (y1 > r.y && y1 < r.y + r.height)
28000 || (r.y >= y0 && r.y < y1)
28001 || (r.y + r.height > y0 && r.y + r.height < y1))
28002 {
28003 /* A header line may be overlapping, but there is no need
28004 to fix overlapping areas for them. KFS 2005-02-12 */
28005 if (row->overlapping_p && !row->mode_line_p)
28006 {
28007 if (first_overlapping_row == NULL)
28008 first_overlapping_row = row;
28009 last_overlapping_row = row;
28010 }
28011
28012 row->clip = fr;
28013 if (expose_line (w, row, &r))
28014 mouse_face_overwritten_p = 1;
28015 row->clip = NULL;
28016 }
28017 else if (row->overlapping_p)
28018 {
28019 /* We must redraw a row overlapping the exposed area. */
28020 if (y0 < r.y
28021 ? y0 + row->phys_height > r.y
28022 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28023 {
28024 if (first_overlapping_row == NULL)
28025 first_overlapping_row = row;
28026 last_overlapping_row = row;
28027 }
28028 }
28029
28030 if (y1 >= yb)
28031 break;
28032 }
28033
28034 /* Display the mode line if there is one. */
28035 if (WINDOW_WANTS_MODELINE_P (w)
28036 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28037 row->enabled_p)
28038 && row->y < r.y + r.height)
28039 {
28040 if (expose_line (w, row, &r))
28041 mouse_face_overwritten_p = 1;
28042 }
28043
28044 if (!w->pseudo_window_p)
28045 {
28046 /* Fix the display of overlapping rows. */
28047 if (first_overlapping_row)
28048 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28049 fr);
28050
28051 /* Draw border between windows. */
28052 x_draw_vertical_border (w);
28053
28054 /* Turn the cursor on again. */
28055 if (cursor_cleared_p
28056 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28057 update_window_cursor (w, 1);
28058 }
28059 }
28060
28061 return mouse_face_overwritten_p;
28062 }
28063
28064
28065
28066 /* Redraw (parts) of all windows in the window tree rooted at W that
28067 intersect R. R contains frame pixel coordinates. Value is
28068 non-zero if the exposure overwrites mouse-face. */
28069
28070 static int
28071 expose_window_tree (struct window *w, XRectangle *r)
28072 {
28073 struct frame *f = XFRAME (w->frame);
28074 int mouse_face_overwritten_p = 0;
28075
28076 while (w && !FRAME_GARBAGED_P (f))
28077 {
28078 if (!NILP (w->hchild))
28079 mouse_face_overwritten_p
28080 |= expose_window_tree (XWINDOW (w->hchild), r);
28081 else if (!NILP (w->vchild))
28082 mouse_face_overwritten_p
28083 |= expose_window_tree (XWINDOW (w->vchild), r);
28084 else
28085 mouse_face_overwritten_p |= expose_window (w, r);
28086
28087 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28088 }
28089
28090 return mouse_face_overwritten_p;
28091 }
28092
28093
28094 /* EXPORT:
28095 Redisplay an exposed area of frame F. X and Y are the upper-left
28096 corner of the exposed rectangle. W and H are width and height of
28097 the exposed area. All are pixel values. W or H zero means redraw
28098 the entire frame. */
28099
28100 void
28101 expose_frame (struct frame *f, int x, int y, int w, int h)
28102 {
28103 XRectangle r;
28104 int mouse_face_overwritten_p = 0;
28105
28106 TRACE ((stderr, "expose_frame "));
28107
28108 /* No need to redraw if frame will be redrawn soon. */
28109 if (FRAME_GARBAGED_P (f))
28110 {
28111 TRACE ((stderr, " garbaged\n"));
28112 return;
28113 }
28114
28115 /* If basic faces haven't been realized yet, there is no point in
28116 trying to redraw anything. This can happen when we get an expose
28117 event while Emacs is starting, e.g. by moving another window. */
28118 if (FRAME_FACE_CACHE (f) == NULL
28119 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28120 {
28121 TRACE ((stderr, " no faces\n"));
28122 return;
28123 }
28124
28125 if (w == 0 || h == 0)
28126 {
28127 r.x = r.y = 0;
28128 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28129 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28130 }
28131 else
28132 {
28133 r.x = x;
28134 r.y = y;
28135 r.width = w;
28136 r.height = h;
28137 }
28138
28139 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28140 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28141
28142 if (WINDOWP (f->tool_bar_window))
28143 mouse_face_overwritten_p
28144 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28145
28146 #ifdef HAVE_X_WINDOWS
28147 #ifndef MSDOS
28148 #ifndef USE_X_TOOLKIT
28149 if (WINDOWP (f->menu_bar_window))
28150 mouse_face_overwritten_p
28151 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28152 #endif /* not USE_X_TOOLKIT */
28153 #endif
28154 #endif
28155
28156 /* Some window managers support a focus-follows-mouse style with
28157 delayed raising of frames. Imagine a partially obscured frame,
28158 and moving the mouse into partially obscured mouse-face on that
28159 frame. The visible part of the mouse-face will be highlighted,
28160 then the WM raises the obscured frame. With at least one WM, KDE
28161 2.1, Emacs is not getting any event for the raising of the frame
28162 (even tried with SubstructureRedirectMask), only Expose events.
28163 These expose events will draw text normally, i.e. not
28164 highlighted. Which means we must redo the highlight here.
28165 Subsume it under ``we love X''. --gerd 2001-08-15 */
28166 /* Included in Windows version because Windows most likely does not
28167 do the right thing if any third party tool offers
28168 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28169 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28170 {
28171 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28172 if (f == hlinfo->mouse_face_mouse_frame)
28173 {
28174 int mouse_x = hlinfo->mouse_face_mouse_x;
28175 int mouse_y = hlinfo->mouse_face_mouse_y;
28176 clear_mouse_face (hlinfo);
28177 note_mouse_highlight (f, mouse_x, mouse_y);
28178 }
28179 }
28180 }
28181
28182
28183 /* EXPORT:
28184 Determine the intersection of two rectangles R1 and R2. Return
28185 the intersection in *RESULT. Value is non-zero if RESULT is not
28186 empty. */
28187
28188 int
28189 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28190 {
28191 XRectangle *left, *right;
28192 XRectangle *upper, *lower;
28193 int intersection_p = 0;
28194
28195 /* Rearrange so that R1 is the left-most rectangle. */
28196 if (r1->x < r2->x)
28197 left = r1, right = r2;
28198 else
28199 left = r2, right = r1;
28200
28201 /* X0 of the intersection is right.x0, if this is inside R1,
28202 otherwise there is no intersection. */
28203 if (right->x <= left->x + left->width)
28204 {
28205 result->x = right->x;
28206
28207 /* The right end of the intersection is the minimum of
28208 the right ends of left and right. */
28209 result->width = (min (left->x + left->width, right->x + right->width)
28210 - result->x);
28211
28212 /* Same game for Y. */
28213 if (r1->y < r2->y)
28214 upper = r1, lower = r2;
28215 else
28216 upper = r2, lower = r1;
28217
28218 /* The upper end of the intersection is lower.y0, if this is inside
28219 of upper. Otherwise, there is no intersection. */
28220 if (lower->y <= upper->y + upper->height)
28221 {
28222 result->y = lower->y;
28223
28224 /* The lower end of the intersection is the minimum of the lower
28225 ends of upper and lower. */
28226 result->height = (min (lower->y + lower->height,
28227 upper->y + upper->height)
28228 - result->y);
28229 intersection_p = 1;
28230 }
28231 }
28232
28233 return intersection_p;
28234 }
28235
28236 #endif /* HAVE_WINDOW_SYSTEM */
28237
28238 \f
28239 /***********************************************************************
28240 Initialization
28241 ***********************************************************************/
28242
28243 void
28244 syms_of_xdisp (void)
28245 {
28246 Vwith_echo_area_save_vector = Qnil;
28247 staticpro (&Vwith_echo_area_save_vector);
28248
28249 Vmessage_stack = Qnil;
28250 staticpro (&Vmessage_stack);
28251
28252 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28253
28254 message_dolog_marker1 = Fmake_marker ();
28255 staticpro (&message_dolog_marker1);
28256 message_dolog_marker2 = Fmake_marker ();
28257 staticpro (&message_dolog_marker2);
28258 message_dolog_marker3 = Fmake_marker ();
28259 staticpro (&message_dolog_marker3);
28260
28261 #if GLYPH_DEBUG
28262 defsubr (&Sdump_frame_glyph_matrix);
28263 defsubr (&Sdump_glyph_matrix);
28264 defsubr (&Sdump_glyph_row);
28265 defsubr (&Sdump_tool_bar_row);
28266 defsubr (&Strace_redisplay);
28267 defsubr (&Strace_to_stderr);
28268 #endif
28269 #ifdef HAVE_WINDOW_SYSTEM
28270 defsubr (&Stool_bar_lines_needed);
28271 defsubr (&Slookup_image_map);
28272 #endif
28273 defsubr (&Sformat_mode_line);
28274 defsubr (&Sinvisible_p);
28275 defsubr (&Scurrent_bidi_paragraph_direction);
28276
28277 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28278 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28279 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28280 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28281 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28282 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28283 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28284 DEFSYM (Qeval, "eval");
28285 DEFSYM (QCdata, ":data");
28286 DEFSYM (Qdisplay, "display");
28287 DEFSYM (Qspace_width, "space-width");
28288 DEFSYM (Qraise, "raise");
28289 DEFSYM (Qslice, "slice");
28290 DEFSYM (Qspace, "space");
28291 DEFSYM (Qmargin, "margin");
28292 DEFSYM (Qpointer, "pointer");
28293 DEFSYM (Qleft_margin, "left-margin");
28294 DEFSYM (Qright_margin, "right-margin");
28295 DEFSYM (Qcenter, "center");
28296 DEFSYM (Qline_height, "line-height");
28297 DEFSYM (QCalign_to, ":align-to");
28298 DEFSYM (QCrelative_width, ":relative-width");
28299 DEFSYM (QCrelative_height, ":relative-height");
28300 DEFSYM (QCeval, ":eval");
28301 DEFSYM (QCpropertize, ":propertize");
28302 DEFSYM (QCfile, ":file");
28303 DEFSYM (Qfontified, "fontified");
28304 DEFSYM (Qfontification_functions, "fontification-functions");
28305 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28306 DEFSYM (Qescape_glyph, "escape-glyph");
28307 DEFSYM (Qnobreak_space, "nobreak-space");
28308 DEFSYM (Qimage, "image");
28309 DEFSYM (Qtext, "text");
28310 DEFSYM (Qboth, "both");
28311 DEFSYM (Qboth_horiz, "both-horiz");
28312 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28313 DEFSYM (QCmap, ":map");
28314 DEFSYM (QCpointer, ":pointer");
28315 DEFSYM (Qrect, "rect");
28316 DEFSYM (Qcircle, "circle");
28317 DEFSYM (Qpoly, "poly");
28318 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28319 DEFSYM (Qgrow_only, "grow-only");
28320 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28321 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28322 DEFSYM (Qposition, "position");
28323 DEFSYM (Qbuffer_position, "buffer-position");
28324 DEFSYM (Qobject, "object");
28325 DEFSYM (Qbar, "bar");
28326 DEFSYM (Qhbar, "hbar");
28327 DEFSYM (Qbox, "box");
28328 DEFSYM (Qhollow, "hollow");
28329 DEFSYM (Qhand, "hand");
28330 DEFSYM (Qarrow, "arrow");
28331 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28332
28333 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28334 Fcons (intern_c_string ("void-variable"), Qnil)),
28335 Qnil);
28336 staticpro (&list_of_error);
28337
28338 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28339 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28340 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28341 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28342
28343 echo_buffer[0] = echo_buffer[1] = Qnil;
28344 staticpro (&echo_buffer[0]);
28345 staticpro (&echo_buffer[1]);
28346
28347 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28348 staticpro (&echo_area_buffer[0]);
28349 staticpro (&echo_area_buffer[1]);
28350
28351 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28352 staticpro (&Vmessages_buffer_name);
28353
28354 mode_line_proptrans_alist = Qnil;
28355 staticpro (&mode_line_proptrans_alist);
28356 mode_line_string_list = Qnil;
28357 staticpro (&mode_line_string_list);
28358 mode_line_string_face = Qnil;
28359 staticpro (&mode_line_string_face);
28360 mode_line_string_face_prop = Qnil;
28361 staticpro (&mode_line_string_face_prop);
28362 Vmode_line_unwind_vector = Qnil;
28363 staticpro (&Vmode_line_unwind_vector);
28364
28365 help_echo_string = Qnil;
28366 staticpro (&help_echo_string);
28367 help_echo_object = Qnil;
28368 staticpro (&help_echo_object);
28369 help_echo_window = Qnil;
28370 staticpro (&help_echo_window);
28371 previous_help_echo_string = Qnil;
28372 staticpro (&previous_help_echo_string);
28373 help_echo_pos = -1;
28374
28375 DEFSYM (Qright_to_left, "right-to-left");
28376 DEFSYM (Qleft_to_right, "left-to-right");
28377
28378 #ifdef HAVE_WINDOW_SYSTEM
28379 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28380 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28381 For example, if a block cursor is over a tab, it will be drawn as
28382 wide as that tab on the display. */);
28383 x_stretch_cursor_p = 0;
28384 #endif
28385
28386 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28387 doc: /* *Non-nil means highlight trailing whitespace.
28388 The face used for trailing whitespace is `trailing-whitespace'. */);
28389 Vshow_trailing_whitespace = Qnil;
28390
28391 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28392 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28393 If the value is t, Emacs highlights non-ASCII chars which have the
28394 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28395 or `escape-glyph' face respectively.
28396
28397 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28398 U+2011 (non-breaking hyphen) are affected.
28399
28400 Any other non-nil value means to display these characters as a escape
28401 glyph followed by an ordinary space or hyphen.
28402
28403 A value of nil means no special handling of these characters. */);
28404 Vnobreak_char_display = Qt;
28405
28406 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28407 doc: /* *The pointer shape to show in void text areas.
28408 A value of nil means to show the text pointer. Other options are `arrow',
28409 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28410 Vvoid_text_area_pointer = Qarrow;
28411
28412 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28413 doc: /* Non-nil means don't actually do any redisplay.
28414 This is used for internal purposes. */);
28415 Vinhibit_redisplay = Qnil;
28416
28417 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28418 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28419 Vglobal_mode_string = Qnil;
28420
28421 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28422 doc: /* Marker for where to display an arrow on top of the buffer text.
28423 This must be the beginning of a line in order to work.
28424 See also `overlay-arrow-string'. */);
28425 Voverlay_arrow_position = Qnil;
28426
28427 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28428 doc: /* String to display as an arrow in non-window frames.
28429 See also `overlay-arrow-position'. */);
28430 Voverlay_arrow_string = make_pure_c_string ("=>");
28431
28432 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28433 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28434 The symbols on this list are examined during redisplay to determine
28435 where to display overlay arrows. */);
28436 Voverlay_arrow_variable_list
28437 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28438
28439 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28440 doc: /* *The number of lines to try scrolling a window by when point moves out.
28441 If that fails to bring point back on frame, point is centered instead.
28442 If this is zero, point is always centered after it moves off frame.
28443 If you want scrolling to always be a line at a time, you should set
28444 `scroll-conservatively' to a large value rather than set this to 1. */);
28445
28446 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28447 doc: /* *Scroll up to this many lines, to bring point back on screen.
28448 If point moves off-screen, redisplay will scroll by up to
28449 `scroll-conservatively' lines in order to bring point just barely
28450 onto the screen again. If that cannot be done, then redisplay
28451 recenters point as usual.
28452
28453 If the value is greater than 100, redisplay will never recenter point,
28454 but will always scroll just enough text to bring point into view, even
28455 if you move far away.
28456
28457 A value of zero means always recenter point if it moves off screen. */);
28458 scroll_conservatively = 0;
28459
28460 DEFVAR_INT ("scroll-margin", scroll_margin,
28461 doc: /* *Number of lines of margin at the top and bottom of a window.
28462 Recenter the window whenever point gets within this many lines
28463 of the top or bottom of the window. */);
28464 scroll_margin = 0;
28465
28466 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28467 doc: /* Pixels per inch value for non-window system displays.
28468 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28469 Vdisplay_pixels_per_inch = make_float (72.0);
28470
28471 #if GLYPH_DEBUG
28472 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28473 #endif
28474
28475 DEFVAR_LISP ("truncate-partial-width-windows",
28476 Vtruncate_partial_width_windows,
28477 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28478 For an integer value, truncate lines in each window narrower than the
28479 full frame width, provided the window width is less than that integer;
28480 otherwise, respect the value of `truncate-lines'.
28481
28482 For any other non-nil value, truncate lines in all windows that do
28483 not span the full frame width.
28484
28485 A value of nil means to respect the value of `truncate-lines'.
28486
28487 If `word-wrap' is enabled, you might want to reduce this. */);
28488 Vtruncate_partial_width_windows = make_number (50);
28489
28490 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28491 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28492 Any other value means to use the appropriate face, `mode-line',
28493 `header-line', or `menu' respectively. */);
28494 mode_line_inverse_video = 1;
28495
28496 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28497 doc: /* *Maximum buffer size for which line number should be displayed.
28498 If the buffer is bigger than this, the line number does not appear
28499 in the mode line. A value of nil means no limit. */);
28500 Vline_number_display_limit = Qnil;
28501
28502 DEFVAR_INT ("line-number-display-limit-width",
28503 line_number_display_limit_width,
28504 doc: /* *Maximum line width (in characters) for line number display.
28505 If the average length of the lines near point is bigger than this, then the
28506 line number may be omitted from the mode line. */);
28507 line_number_display_limit_width = 200;
28508
28509 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28510 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28511 highlight_nonselected_windows = 0;
28512
28513 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28514 doc: /* Non-nil if more than one frame is visible on this display.
28515 Minibuffer-only frames don't count, but iconified frames do.
28516 This variable is not guaranteed to be accurate except while processing
28517 `frame-title-format' and `icon-title-format'. */);
28518
28519 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28520 doc: /* Template for displaying the title bar of visible frames.
28521 \(Assuming the window manager supports this feature.)
28522
28523 This variable has the same structure as `mode-line-format', except that
28524 the %c and %l constructs are ignored. It is used only on frames for
28525 which no explicit name has been set \(see `modify-frame-parameters'). */);
28526
28527 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28528 doc: /* Template for displaying the title bar of an iconified frame.
28529 \(Assuming the window manager supports this feature.)
28530 This variable has the same structure as `mode-line-format' (which see),
28531 and is used only on frames for which no explicit name has been set
28532 \(see `modify-frame-parameters'). */);
28533 Vicon_title_format
28534 = Vframe_title_format
28535 = pure_cons (intern_c_string ("multiple-frames"),
28536 pure_cons (make_pure_c_string ("%b"),
28537 pure_cons (pure_cons (empty_unibyte_string,
28538 pure_cons (intern_c_string ("invocation-name"),
28539 pure_cons (make_pure_c_string ("@"),
28540 pure_cons (intern_c_string ("system-name"),
28541 Qnil)))),
28542 Qnil)));
28543
28544 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28545 doc: /* Maximum number of lines to keep in the message log buffer.
28546 If nil, disable message logging. If t, log messages but don't truncate
28547 the buffer when it becomes large. */);
28548 Vmessage_log_max = make_number (100);
28549
28550 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28551 doc: /* Functions called before redisplay, if window sizes have changed.
28552 The value should be a list of functions that take one argument.
28553 Just before redisplay, for each frame, if any of its windows have changed
28554 size since the last redisplay, or have been split or deleted,
28555 all the functions in the list are called, with the frame as argument. */);
28556 Vwindow_size_change_functions = Qnil;
28557
28558 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28559 doc: /* List of functions to call before redisplaying a window with scrolling.
28560 Each function is called with two arguments, the window and its new
28561 display-start position. Note that these functions are also called by
28562 `set-window-buffer'. Also note that the value of `window-end' is not
28563 valid when these functions are called.
28564
28565 Warning: Do not use this feature to alter the way the window
28566 is scrolled. It is not designed for that, and such use probably won't
28567 work. */);
28568 Vwindow_scroll_functions = Qnil;
28569
28570 DEFVAR_LISP ("window-text-change-functions",
28571 Vwindow_text_change_functions,
28572 doc: /* Functions to call in redisplay when text in the window might change. */);
28573 Vwindow_text_change_functions = Qnil;
28574
28575 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28576 doc: /* Functions called when redisplay of a window reaches the end trigger.
28577 Each function is called with two arguments, the window and the end trigger value.
28578 See `set-window-redisplay-end-trigger'. */);
28579 Vredisplay_end_trigger_functions = Qnil;
28580
28581 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28582 doc: /* *Non-nil means autoselect window with mouse pointer.
28583 If nil, do not autoselect windows.
28584 A positive number means delay autoselection by that many seconds: a
28585 window is autoselected only after the mouse has remained in that
28586 window for the duration of the delay.
28587 A negative number has a similar effect, but causes windows to be
28588 autoselected only after the mouse has stopped moving. \(Because of
28589 the way Emacs compares mouse events, you will occasionally wait twice
28590 that time before the window gets selected.\)
28591 Any other value means to autoselect window instantaneously when the
28592 mouse pointer enters it.
28593
28594 Autoselection selects the minibuffer only if it is active, and never
28595 unselects the minibuffer if it is active.
28596
28597 When customizing this variable make sure that the actual value of
28598 `focus-follows-mouse' matches the behavior of your window manager. */);
28599 Vmouse_autoselect_window = Qnil;
28600
28601 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28602 doc: /* *Non-nil means automatically resize tool-bars.
28603 This dynamically changes the tool-bar's height to the minimum height
28604 that is needed to make all tool-bar items visible.
28605 If value is `grow-only', the tool-bar's height is only increased
28606 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28607 Vauto_resize_tool_bars = Qt;
28608
28609 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28610 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28611 auto_raise_tool_bar_buttons_p = 1;
28612
28613 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28614 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28615 make_cursor_line_fully_visible_p = 1;
28616
28617 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28618 doc: /* *Border below tool-bar in pixels.
28619 If an integer, use it as the height of the border.
28620 If it is one of `internal-border-width' or `border-width', use the
28621 value of the corresponding frame parameter.
28622 Otherwise, no border is added below the tool-bar. */);
28623 Vtool_bar_border = Qinternal_border_width;
28624
28625 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28626 doc: /* *Margin around tool-bar buttons in pixels.
28627 If an integer, use that for both horizontal and vertical margins.
28628 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28629 HORZ specifying the horizontal margin, and VERT specifying the
28630 vertical margin. */);
28631 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28632
28633 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28634 doc: /* *Relief thickness of tool-bar buttons. */);
28635 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28636
28637 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28638 doc: /* Tool bar style to use.
28639 It can be one of
28640 image - show images only
28641 text - show text only
28642 both - show both, text below image
28643 both-horiz - show text to the right of the image
28644 text-image-horiz - show text to the left of the image
28645 any other - use system default or image if no system default. */);
28646 Vtool_bar_style = Qnil;
28647
28648 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28649 doc: /* *Maximum number of characters a label can have to be shown.
28650 The tool bar style must also show labels for this to have any effect, see
28651 `tool-bar-style'. */);
28652 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28653
28654 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28655 doc: /* List of functions to call to fontify regions of text.
28656 Each function is called with one argument POS. Functions must
28657 fontify a region starting at POS in the current buffer, and give
28658 fontified regions the property `fontified'. */);
28659 Vfontification_functions = Qnil;
28660 Fmake_variable_buffer_local (Qfontification_functions);
28661
28662 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28663 unibyte_display_via_language_environment,
28664 doc: /* *Non-nil means display unibyte text according to language environment.
28665 Specifically, this means that raw bytes in the range 160-255 decimal
28666 are displayed by converting them to the equivalent multibyte characters
28667 according to the current language environment. As a result, they are
28668 displayed according to the current fontset.
28669
28670 Note that this variable affects only how these bytes are displayed,
28671 but does not change the fact they are interpreted as raw bytes. */);
28672 unibyte_display_via_language_environment = 0;
28673
28674 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28675 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28676 If a float, it specifies a fraction of the mini-window frame's height.
28677 If an integer, it specifies a number of lines. */);
28678 Vmax_mini_window_height = make_float (0.25);
28679
28680 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28681 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28682 A value of nil means don't automatically resize mini-windows.
28683 A value of t means resize them to fit the text displayed in them.
28684 A value of `grow-only', the default, means let mini-windows grow only;
28685 they return to their normal size when the minibuffer is closed, or the
28686 echo area becomes empty. */);
28687 Vresize_mini_windows = Qgrow_only;
28688
28689 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28690 doc: /* Alist specifying how to blink the cursor off.
28691 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28692 `cursor-type' frame-parameter or variable equals ON-STATE,
28693 comparing using `equal', Emacs uses OFF-STATE to specify
28694 how to blink it off. ON-STATE and OFF-STATE are values for
28695 the `cursor-type' frame parameter.
28696
28697 If a frame's ON-STATE has no entry in this list,
28698 the frame's other specifications determine how to blink the cursor off. */);
28699 Vblink_cursor_alist = Qnil;
28700
28701 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28702 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28703 If non-nil, windows are automatically scrolled horizontally to make
28704 point visible. */);
28705 automatic_hscrolling_p = 1;
28706 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28707
28708 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28709 doc: /* *How many columns away from the window edge point is allowed to get
28710 before automatic hscrolling will horizontally scroll the window. */);
28711 hscroll_margin = 5;
28712
28713 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28714 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28715 When point is less than `hscroll-margin' columns from the window
28716 edge, automatic hscrolling will scroll the window by the amount of columns
28717 determined by this variable. If its value is a positive integer, scroll that
28718 many columns. If it's a positive floating-point number, it specifies the
28719 fraction of the window's width to scroll. If it's nil or zero, point will be
28720 centered horizontally after the scroll. Any other value, including negative
28721 numbers, are treated as if the value were zero.
28722
28723 Automatic hscrolling always moves point outside the scroll margin, so if
28724 point was more than scroll step columns inside the margin, the window will
28725 scroll more than the value given by the scroll step.
28726
28727 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28728 and `scroll-right' overrides this variable's effect. */);
28729 Vhscroll_step = make_number (0);
28730
28731 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28732 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28733 Bind this around calls to `message' to let it take effect. */);
28734 message_truncate_lines = 0;
28735
28736 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28737 doc: /* Normal hook run to update the menu bar definitions.
28738 Redisplay runs this hook before it redisplays the menu bar.
28739 This is used to update submenus such as Buffers,
28740 whose contents depend on various data. */);
28741 Vmenu_bar_update_hook = Qnil;
28742
28743 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28744 doc: /* Frame for which we are updating a menu.
28745 The enable predicate for a menu binding should check this variable. */);
28746 Vmenu_updating_frame = Qnil;
28747
28748 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28749 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28750 inhibit_menubar_update = 0;
28751
28752 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28753 doc: /* Prefix prepended to all continuation lines at display time.
28754 The value may be a string, an image, or a stretch-glyph; it is
28755 interpreted in the same way as the value of a `display' text property.
28756
28757 This variable is overridden by any `wrap-prefix' text or overlay
28758 property.
28759
28760 To add a prefix to non-continuation lines, use `line-prefix'. */);
28761 Vwrap_prefix = Qnil;
28762 DEFSYM (Qwrap_prefix, "wrap-prefix");
28763 Fmake_variable_buffer_local (Qwrap_prefix);
28764
28765 DEFVAR_LISP ("line-prefix", Vline_prefix,
28766 doc: /* Prefix prepended to all non-continuation lines at display time.
28767 The value may be a string, an image, or a stretch-glyph; it is
28768 interpreted in the same way as the value of a `display' text property.
28769
28770 This variable is overridden by any `line-prefix' text or overlay
28771 property.
28772
28773 To add a prefix to continuation lines, use `wrap-prefix'. */);
28774 Vline_prefix = Qnil;
28775 DEFSYM (Qline_prefix, "line-prefix");
28776 Fmake_variable_buffer_local (Qline_prefix);
28777
28778 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28779 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28780 inhibit_eval_during_redisplay = 0;
28781
28782 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28783 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28784 inhibit_free_realized_faces = 0;
28785
28786 #if GLYPH_DEBUG
28787 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28788 doc: /* Inhibit try_window_id display optimization. */);
28789 inhibit_try_window_id = 0;
28790
28791 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28792 doc: /* Inhibit try_window_reusing display optimization. */);
28793 inhibit_try_window_reusing = 0;
28794
28795 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28796 doc: /* Inhibit try_cursor_movement display optimization. */);
28797 inhibit_try_cursor_movement = 0;
28798 #endif /* GLYPH_DEBUG */
28799
28800 DEFVAR_INT ("overline-margin", overline_margin,
28801 doc: /* *Space between overline and text, in pixels.
28802 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28803 margin to the character height. */);
28804 overline_margin = 2;
28805
28806 DEFVAR_INT ("underline-minimum-offset",
28807 underline_minimum_offset,
28808 doc: /* Minimum distance between baseline and underline.
28809 This can improve legibility of underlined text at small font sizes,
28810 particularly when using variable `x-use-underline-position-properties'
28811 with fonts that specify an UNDERLINE_POSITION relatively close to the
28812 baseline. The default value is 1. */);
28813 underline_minimum_offset = 1;
28814
28815 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28816 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28817 This feature only works when on a window system that can change
28818 cursor shapes. */);
28819 display_hourglass_p = 1;
28820
28821 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28822 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28823 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28824
28825 hourglass_atimer = NULL;
28826 hourglass_shown_p = 0;
28827
28828 DEFSYM (Qglyphless_char, "glyphless-char");
28829 DEFSYM (Qhex_code, "hex-code");
28830 DEFSYM (Qempty_box, "empty-box");
28831 DEFSYM (Qthin_space, "thin-space");
28832 DEFSYM (Qzero_width, "zero-width");
28833
28834 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28835 /* Intern this now in case it isn't already done.
28836 Setting this variable twice is harmless.
28837 But don't staticpro it here--that is done in alloc.c. */
28838 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28839 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28840
28841 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28842 doc: /* Char-table defining glyphless characters.
28843 Each element, if non-nil, should be one of the following:
28844 an ASCII acronym string: display this string in a box
28845 `hex-code': display the hexadecimal code of a character in a box
28846 `empty-box': display as an empty box
28847 `thin-space': display as 1-pixel width space
28848 `zero-width': don't display
28849 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28850 display method for graphical terminals and text terminals respectively.
28851 GRAPHICAL and TEXT should each have one of the values listed above.
28852
28853 The char-table has one extra slot to control the display of a character for
28854 which no font is found. This slot only takes effect on graphical terminals.
28855 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28856 `thin-space'. The default is `empty-box'. */);
28857 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28858 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28859 Qempty_box);
28860 }
28861
28862
28863 /* Initialize this module when Emacs starts. */
28864
28865 void
28866 init_xdisp (void)
28867 {
28868 current_header_line_height = current_mode_line_height = -1;
28869
28870 CHARPOS (this_line_start_pos) = 0;
28871
28872 if (!noninteractive)
28873 {
28874 struct window *m = XWINDOW (minibuf_window);
28875 Lisp_Object frame = m->frame;
28876 struct frame *f = XFRAME (frame);
28877 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28878 struct window *r = XWINDOW (root);
28879 int i;
28880
28881 echo_area_window = minibuf_window;
28882
28883 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28884 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28885 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28886 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28887 XSETFASTINT (m->total_lines, 1);
28888 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28889
28890 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28891 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28892 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28893
28894 /* The default ellipsis glyphs `...'. */
28895 for (i = 0; i < 3; ++i)
28896 default_invis_vector[i] = make_number ('.');
28897 }
28898
28899 {
28900 /* Allocate the buffer for frame titles.
28901 Also used for `format-mode-line'. */
28902 int size = 100;
28903 mode_line_noprop_buf = (char *) xmalloc (size);
28904 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28905 mode_line_noprop_ptr = mode_line_noprop_buf;
28906 mode_line_target = MODE_LINE_DISPLAY;
28907 }
28908
28909 help_echo_showing_p = 0;
28910 }
28911
28912 /* Since w32 does not support atimers, it defines its own implementation of
28913 the following three functions in w32fns.c. */
28914 #ifndef WINDOWSNT
28915
28916 /* Platform-independent portion of hourglass implementation. */
28917
28918 /* Return non-zero if hourglass timer has been started or hourglass is
28919 shown. */
28920 int
28921 hourglass_started (void)
28922 {
28923 return hourglass_shown_p || hourglass_atimer != NULL;
28924 }
28925
28926 /* Cancel a currently active hourglass timer, and start a new one. */
28927 void
28928 start_hourglass (void)
28929 {
28930 #if defined (HAVE_WINDOW_SYSTEM)
28931 EMACS_TIME delay;
28932 int secs, usecs = 0;
28933
28934 cancel_hourglass ();
28935
28936 if (INTEGERP (Vhourglass_delay)
28937 && XINT (Vhourglass_delay) > 0)
28938 secs = XFASTINT (Vhourglass_delay);
28939 else if (FLOATP (Vhourglass_delay)
28940 && XFLOAT_DATA (Vhourglass_delay) > 0)
28941 {
28942 Lisp_Object tem;
28943 tem = Ftruncate (Vhourglass_delay, Qnil);
28944 secs = XFASTINT (tem);
28945 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28946 }
28947 else
28948 secs = DEFAULT_HOURGLASS_DELAY;
28949
28950 EMACS_SET_SECS_USECS (delay, secs, usecs);
28951 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28952 show_hourglass, NULL);
28953 #endif
28954 }
28955
28956
28957 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28958 shown. */
28959 void
28960 cancel_hourglass (void)
28961 {
28962 #if defined (HAVE_WINDOW_SYSTEM)
28963 if (hourglass_atimer)
28964 {
28965 cancel_atimer (hourglass_atimer);
28966 hourglass_atimer = NULL;
28967 }
28968
28969 if (hourglass_shown_p)
28970 hide_hourglass ();
28971 #endif
28972 }
28973 #endif /* ! WINDOWSNT */